Skip to content

Instantly share code, notes, and snippets.

@hostilefork
Forked from JacobGood1/gist:c2b82879431f3b0f7b17
Last active August 29, 2015 14:14
Show Gist options
  • Save hostilefork/048d5e6593c51a91193e to your computer and use it in GitHub Desktop.
Save hostilefork/048d5e6593c51a91193e to your computer and use it in GitHub Desktop.
thread: function/with [
{
sends a value through a block of functions, the first argument of the last function
becomes the value. This value is then seeded by composing the functions together.
Ex: thread 1 + 1 [add 1 add 2] => [add add 2 2 1]
}
first-composed-value
code-block
/only "only return the code itself, do not evaluate it"
] [
each [arg code-arg rearranged-code] memoize [
if all [
first-composed-value = arg
code-arg = code-block
] [
code: rearranged-code
break
]
]
unless code [
code: copy []
append memoize first-composed-value
append/only memoize code-block
insert code first-composed-value
while [not empty? code-block][
fx: code-block/1
fx-arg-amount: length words-of get fx
insert code fx
append code copy/part next code-block (fx-arg-amount - 1)
code-block: skip code-block fx-arg-amount
]
append/only memoize code
]
return either only [code] [do code]
] [
memoize: []
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment