Skip to content

Instantly share code, notes, and snippets.

@maximvl
maximvl / .red
Created August 6, 2016 17:18
@steveGit's version of LC in Red
Red []
lc: function [rule] [
parse rule [
some [
s: word! 'in skip
(in: last reduce/into ['foreach s/1 s/3 make block! 4] in)
| 'if skip
(in: last reduce/into ['if to-paren s/2 make block! 4] in)
| skip '|
@toomasv
toomasv / flatten.red
Created August 28, 2017 19:58
Flatten up to certain level
flatten: func [tree [block!] /level lvl /local rule l][
(l: -1)
rule: [(l: l + 1) some [
ahead block! if (any [not level l < lvl]) into rule (l: l - 1)
| keep skip
| none
]]
parse tree [collect rule]
]
@toomasv
toomasv / matrix.red
Last active May 29, 2026 06:49
Little matrix DSL
Red [
Author: "Toomas Vooglaid"
Date: 7-9-2017
Last-update: 4-10-2017
]
mx: context [
ctx: self
mtx: object [
rows: cols: data: none
get-col: func [col][extract at data col cols]
@toomasv
toomasv / range.red
Last active May 29, 2026 06:55
Range function for multiple datatypes
Red [
Author: "Toomas Vooglaid"
Date: 26-11-2017
]
range: function [val1 val2 /step stp /limit /enbase ebase /debase dbase /unicode][
stp+?: none
stp: any [stp either any [percent? val1 percent? val2] [1%][1]]
if any [all [block? stp zero? stp/3] zero? stp] [return none]
case [
debase [
@toomasv
toomasv / lazy-range.red
Last active May 29, 2026 06:56
Range for on-demand evaluation
Red [
Author: "Toomas Vooglaid"
Date: 27-11-2017
]
lazy-range: function [
"Returns function that generates next value in (possibly infinite) range"
val [scalar!] "Initial value"
/step stp [scalar! block!] "By which to increment each next value"
/limit lim [integer!] "How many new values to generate"
/stop stopval [scalar! block!] "On which value to stop generation of new values; if block, then also after how may matches"
@toomasv
toomasv / incrementors.red
Created June 12, 2018 12:09
Some incrementation funcs
Red [
Original-idea: "Dave Andersen"
Source: https://gitter.im/red/red?at=5a2a4fb9a2be46682876463d
]
inc: func ['val][set val (get val) + 1]
dec: func ['val][set val (get val) - 1]
incr: func ['val step][set val (get val) + step]
decr: func ['val step][set val (get val) - step]
++: make op! :incr
--: make op! :decr
@hinjolicious
hinjolicious / matrix.red
Created May 29, 2026 06:49 — forked from toomasv/matrix.red
Little matrix DSL
Red [
Author: "Toomas Vooglaid"
Date: 7-9-2017
Last-update: 4-10-2017
]
mx: context [
ctx: self
mtx: object [
rows: cols: data: none
get-col: func [col][extract at data col cols]