Skip to content

Instantly share code, notes, and snippets.

@geekyi
Last active May 16, 2017 13:18
Show Gist options
  • Save geekyi/844a15e506ce9c6b12a83e78df750295 to your computer and use it in GitHub Desktop.
Save geekyi/844a15e506ce9c6b12a83e78df750295 to your computer and use it in GitHub Desktop.
Red [
Title: "fry.red"
Author: ["Gregg Irwin" "Steeve"]
File: %fry.red
Purpose: {
Factor style fry combinators in Red
http://docs.factorcode.org/content/article-fry.html
works somewhat like a reverse collect/keep
}
]
; alternative names: [place put fill fry]
; Gregg's version
place1: function [
series [block!] ; or `holes`
items [block!]
][
items: copy items
_: [change '_ (take items)] ; alternatively, `_?`
; series: copy series
parse series [some [_ | skip]]
series
]
; Steeve's version
; https://gitter.im/red/red?at=57c55457f9d724c34acdc508
; built-in compiled replace doesn't work, so reinterpret
replace2: func spec-of :replace body-of :replace
place2: func [
series [block!]
items [block!]
/mark
marker [word!]
][
replace2/all copy series either mark [
marker
][
'_
]
does [take items]
]
place: :place2
; usage:
place [_ + _ * _] [a b c] ;== [a + b * c]
place [1 _ 2 _ 3] collect [1 keep ('+) 2 keep ('*) 3] ;== [1 + 2 * 3]
place [_ + _ * _] collect [keep 1 '+ keep 2 '* keep 3] ;== [1 + 2 * 3]
place [_ + _ * _] collect [(keep 1) + (keep 2) * (keep 3)] ;== [1 + 2 * 3]
; not what one might expect:
place [_ + _ * _] collect [keep (1) + keep (2) * keep (3)] ;== [3 + 6 * 7]
place/mark [! + ! + !] [10 20 30] '!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment