Last active
August 14, 2020 07:33
-
-
Save theSherwood/e2e680e13d86bdd4dd2842b2af449f73 to your computer and use it in GitHub Desktop.
Python-like Series Comprehensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Description: Python-like Series Comprehensions | |
Date: 14-August-2020 | |
] | |
produce: func[ | |
src [series!] | |
/into | |
buffer [series!] | |
/local | |
mapping word wrapped-series filter series result | |
][ | |
; Unset `filter` as there may be no "if clause" in `src` and we don't | |
; want to reuse what we may have already set on a previous call. | |
unset 'filter | |
parse src [ | |
copy mapping any to 'for | |
skip | |
copy word any to 'in | |
skip | |
copy wrapped-series any to 'if | |
skip | |
copy filter any to end | |
] | |
series: do wrapped-series | |
; Match the `type? series` datatype by default. | |
result: either into [buffer][to type? series []] | |
either value? 'filter [ | |
foreach (first word) (series) [ | |
if do filter [append result do mapping] | |
] | |
][ | |
foreach (first word) (series) [append result do mapping] | |
] | |
result | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some examples: