<commit_before>REBOL [
    System: "REBOL [R3] Language Interpreter and Run-time Environment"
    Title: "REBOL 3 Mezzanine: Series Helpers"
    Rights: {
        Copyright 2012 REBOL Technologies
        REBOL is a trademark of REBOL Technologies
    }
    License: {
        Licensed under the Apache License, Version 2.0
        See: http://www.apache.org/licenses/LICENSE-2.0
    }
]

empty?: func [
    {Returns TRUE if empty or NONE, or for series if index is at or beyond its tail.}
    series [any-series! object! gob! port! bitset! map! none!]
][
    tail? series
]

offset-of: func [
    "Returns the offset between two series positions."
    series1 [any-series!]
    series2 [any-series!]
][
    subtract index-of series2 index-of series1
]

found?: func [
    "Returns TRUE if value is not NONE."
    value
][
    not none? :value
]

last?: single?: func [
    "Returns TRUE if the series length is 1."
    series [any-series! port! map! tuple! bitset! object! gob! any-word!]
][
    1 = length series
]

extend: func [
    "Extend an object, map, or block type with word and value pair."
    obj [object! map! block! group!] {object to extend (modified)}
    word [any-word!]
    val
][
    if :val [append obj reduce [to-set-word word :val]]
    :val
]

rejoin: func [
    "Reduces and joins a block of values."
    block [block!] "Values to reduce and join"
    ;/with "separator"
][
    if empty? block: reduce block [return block]
    append either any-series? first block [copy first block][
        form first block
    ] next block
]

remold: func [
    {Reduces and converts a value to a REBOL-readable string.}
    value {The value to reduce and mold}
    /only {For a block value, mold only its contents, no outer []}
    /all  {Mold in serialized format}
    /flat {No indentation}
][
    mold/:only/:all/:flat reduce :value
]

charset: func [
    "Makes a bitset of chars for the parse function."
    chars [string! block! binary! char! integer!]
    /length "Preallocate this many bits"
    len [integer!] "Must be > 0"
    /local charset-length length-of
][
    ;-- CHARSET function historically has a refinement called /LENGTH, that
    ;-- is used to preallocate bits.  Yet the LENGTH? function has been
    ;-- changed to use just the word LENGTH.  We could change this to
    ;-- /CAPACITY SIZE or something similar, but keep it working for now.
    ;--
    charset-length: length      ; refinement passed in
    length-of: :lib/length      ; traditional LENGTH function
    unset 'length               ; helps avoid overlooking the ambiguity

    either charset-length [append make bitset! len chars] [make bitset! chars]
]

array: func [
    "Makes and initializes a series of a given size."
    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/opt 'size first size [
            cause-error 'script 'expect-arg reduce ['array 'size type-of :size]
        ]
    ]
    block: make block! size
    case [
        block? :rest [
            loop size [block: insert/only block array/initial rest :value]
        ]
        any-series? :value [
            loop size [block: insert/only block copy/deep value]
        ]
        function? :value [ ; So value can be a thunk :)
            loop size [block: insert/only block value] ; Called every time
        ]
	true [
            insert/dup block either initial [value][none] size
	]
    ]
    head block
]

replace: func [
    "Replaces a search value with the replace value within the target series."<commit_msg>Remove stray tab in %mezz-series.r<commit_after>REBOL [
    System: "REBOL [R3] Language Interpreter and Run-time Environment"
    Title: "REBOL 3 Mezzanine: Series Helpers"
    Rights: {
        Copyright 2012 REBOL Technologies
        REBOL is a trademark of REBOL Technologies
    }
    License: {
        Licensed under the Apache License, Version 2.0
        See: http://www.apache.org/licenses/LICENSE-2.0
    }
]

empty?: func [
    {Returns TRUE if empty or NONE, or for series if index is at or beyond its tail.}
    series [any-series! object! gob! port! bitset! map! none!]
][
    tail? series
]

offset-of: func [
    "Returns the offset between two series positions."
    series1 [any-series!]
    series2 [any-series!]
][
    subtract index-of series2 index-of series1
]

found?: func [
    "Returns TRUE if value is not NONE."
    value
][
    not none? :value
]

last?: single?: func [
    "Returns TRUE if the series length is 1."
    series [any-series! port! map! tuple! bitset! object! gob! any-word!]
][
    1 = length series
]

extend: func [
    "Extend an object, map, or block type with word and value pair."
    obj [object! map! block! group!] {object to extend (modified)}
    word [any-word!]
    val
][
    if :val [append obj reduce [to-set-word word :val]]
    :val
]

rejoin: func [
    "Reduces and joins a block of values."
    block [block!] "Values to reduce and join"
    ;/with "separator"
][
    if empty? block: reduce block [return block]
    append either any-series? first block [copy first block][
        form first block
    ] next block
]

remold: func [
    {Reduces and converts a value to a REBOL-readable string.}
    value {The value to reduce and mold}
    /only {For a block value, mold only its contents, no outer []}
    /all  {Mold in serialized format}
    /flat {No indentation}
][
    mold/:only/:all/:flat reduce :value
]

charset: func [
    "Makes a bitset of chars for the parse function."
    chars [string! block! binary! char! integer!]
    /length "Preallocate this many bits"
    len [integer!] "Must be > 0"
    /local charset-length length-of
][
    ;-- CHARSET function historically has a refinement called /LENGTH, that
    ;-- is used to preallocate bits.  Yet the LENGTH? function has been
    ;-- changed to use just the word LENGTH.  We could change this to
    ;-- /CAPACITY SIZE or something similar, but keep it working for now.
    ;--
    charset-length: length      ; refinement passed in
    length-of: :lib/length      ; traditional LENGTH function
    unset 'length               ; helps avoid overlooking the ambiguity

    either charset-length [append make bitset! len chars] [make bitset! chars]
]

array: func [
    "Makes and initializes a series of a given size."
    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/opt 'size first size [
            cause-error 'script 'expect-arg reduce ['array 'size type-of :size]
        ]
    ]
    block: make block! size
    case [
        block? :rest [
            loop size [block: insert/only block array/initial rest :value]
        ]
        any-series? :value [
            loop size [block: insert/only block copy/deep value]
        ]
        function? :value [ ; So value can be a thunk :)
            loop size [block: insert/only block value] ; Called every time
        ]
        'default [
            insert/dup block either initial [value][none] size
        ]
    ]
    head block
]

replace: func [
    "Replaces a search value with the replace value within the target series."
{'fraction_non_alphanumeric': 0.09561647463912065, 'fraction_numerical': 0.004502714872202357, 'mean_word_length': 2.5044083526682135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '477275', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2224, 'n_words': 933}