跳转到内容

Rebol 编程/数组

来自维基教科书,开放世界中的开放书籍
ARRAY size /initial value 

创建并初始化给定大小的序列。

ARRAY 是一个函数值。

  • 大小 -- 每个维度的大小或大小块 (类型: 整数块)
  • /initial -- 为所有元素指定一个初始值
    • -- 初始值 (如果为函数,则每次调用时都会调用) (类型: 任何)

(特殊属性)

[编辑 | 编辑源代码]
  • 捕获

源代码

[编辑 | 编辑源代码]
array: func [
    "Makes and initializes a series of a given size." 
    [catch] 
    size [integer! block!] "Size or block of sizes for each dimension" 
    /initial "Specify an initial value for all elements" 
    value {Initial value (will be called each time if a function)} 
    /local block rest
][
    if block? size [
        if tail? rest: next size [rest: none] 
        unless integer? set/any 'size pick size 1 [
            throw-error 'script 'expect-arg reduce ['array 'size type? get/any 'size]
        ]
    ] 
    block: make block! size 
    case [
        block? rest [
            loop size [block: insert/only block array/initial rest :value]
        ] 
        series? :value [
            loop size [block: insert/only block copy/deep value]
        ] 
        any-function? :value [
            loop size [block: insert/only block value]
        ] 
        insert/dup block value size
    ] 
    head block
]
华夏公益教科书