Skip to content

Instantly share code, notes, and snippets.

@geekyi geekyi/fry.red

Last active May 16, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.