Skip to content

Instantly share code, notes, and snippets.

@toomasv
Created May 18, 2019 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toomasv/4980a6d4a7e6f97817d86ffcf32329ab to your computer and use it in GitHub Desktop.
Save toomasv/4980a6d4a7e6f97817d86ffcf32329ab to your computer and use it in GitHub Desktop.
Applies func(s) to elements of series
Red [
Description: {Applies function or sequence of functions
to block or collected output of previous functions}
Date: 18-May-2019
Author: "Toomas Vooglaid"
]
each*: make op! function [fn [any-function! block!] block [series!]][
{Applies a function to as many elements of the block as it has arguments
or a sequence of functions to the block or collected output of the previous function.
Returns block with the collected result of last function}
append fns: clear [] reduce :fn
foreach fn fns [
words: parse spec-of :fn [collect [
some [refinement! break | keep word! | skip]]
]
code: compose/deep [
block: collect [
foreach [(words)] block [
attempt [keep (compose either op? :fn [
[(words/1) fn (words/2)]
] [
[fn (words)]
])]
]
]
]
do code
]
]
;Simple `each`
each: make op! func [fn block][forall block [fn block/1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment