gpt4 book ai didi

object - 交错 block ,或从两个 block (字段名称和值)中创建对象

转载 作者:行者123 更新时间:2023-12-04 18:43:53 28 4
gpt4 key购买 nike

而不是通过编写创建对象:

obj: object [
name: "Fork"
id: 1020
]

我想写一些类似...
obj: something-or-another [name id] ["Fork" 1020]

...并得到相同的结果。理想的解决方案还允许:
obj: something-or-another [name: id:] ["Fork" 1020]

很容易写一个 something-or-another ,但这是否适合已经“在盒子里”的东西?

最佳答案

我不相信有一种成熟的方法可以做到这一点。不过不难:

func [words values][
set words: context append map-each word words [to set-word! word] none values
words
]

我想我可以稍微分解一下:
func [
"Creates an Object From a Block of Words, Assigns Values"
words [block!] "Words used to create object"
values "Value(s) to assign"
][
words: map-each word words [to set-word! word] ; The requisite set-words
append words none ; Initial value for all words
words: context words ; Create our object
set words values ; Assigns our value(s) to the object
words ; returns the object
]

您可以使用不同的方法来交错块,例如:
func [words [block!] values [block!]][
collect [
repeat index max length? words length? values [
keep words/:index
keep values/:index
]
]
]

关于object - 交错 block ,或从两个 block (字段名称和值)中创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18347892/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com