Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 184 total  RSS 

R3 compatible MAP

; Not terrribly efficient, but it works.
    ; R3-compatible interface
    map: func ['word data [block!] body [block!]] [
        collect/only res compose/deep [
            repeat (word) data [res: do bind/copy body (to lit-word! word)]
        ]
    ]

MERGE two series

merge: func [
    "Merge A and B together, like a zipper, alternating elements"
    a [series!]
    b [series!]
    /only "Merge items as sub-blocks"
    /local res val
][
    res: make a length? a
    repeat i max length? a length? b [
        val: reduce [pick a i  pick b i]
        either only [append/only res val] [append res val]
    ]
    res
]

Does a file have a path?

; You could use split-path for this, but it's a bit non-
; orthogonal in what it returns when there's no path.
has-path?: func [file [file!]] [find file #"/"]

Get the name of the script that is running

script-name: does [system/options/script]

Get the path a script was started in

boot-path:   does [system/options/path]

Get the path to the REBOL interpreter a script is running under

REBOL-EXE:   does [system/options/boot]

Get user name (Windows)

username: does [get-env "USERNAME"]

Get machine ID

machine-id: does [read dns://]

enforce lit-word! param

To enforce passing a lit-word! param, you need to make the param a get-word!, so it isn't evaluated when the func is called. This won't work if you're dynamically creating the param of course.

incr: func [
    {Increment a value by 1.}
    :word [lit-word!]
    /by {Change by this amount}     ; /skip ?
        value
][
    set word add get word any [value 1]
]


;>> a: 0
;== 0
;>> incr a
;** Script Error: incr expected word argument of type: ;lit-word
;** Near: incr a
;>> incr 'a
;== 1

days-up-to-month - returns the number of days in a year, preceding the given month.

days-in-months: [31 28 31 30 31 30 31 31 30 31 30 31]
days-in-months-leap: head change at copy days-in-months 2 29

days-up-to-month: func [month /in year] [
    sum copy/part either leap-year?/with any [year now] [days-in-months-leap] [days-in-months] month - 1
]
« Newer Snippets
Older Snippets »
Showing 1-10 of 184 total  RSS