Skip to content

Instantly share code, notes, and snippets.

View benchhash.red
Red []
clock: func [code /local t1 t2] [
t1: now/precise/time
loop 100 code
t2: now/precise/time
print [(t2 - t1) mold/flat code]
]
m1: make hash! []
View benchmap.red
Red []
clock: func [code /local t1 t2] [
t1: now/precise/time
loop 100 code
t2: now/precise/time
print [(t2 - t1) mold/flat code]
]
m1: #()
View benchhash2.red
Red []
clock: func [code /local t1 t2] [
t1: now/precise/time
loop 100 code
t2: now/precise/time
print [(t2 - t1) mold/flat code]
]
m1: make hash! []
View dot-chaining.red
Red []
; WOW! chain expressions with a dot to make them more readable!
; an inline transformation of a chain of function applications into a lisp-like pyramid of braces ;)
; (expr) . func args... => (func (expr) args...)
; e.g. [1 2 3 4 5] . skip 1 . change/part '- 3 . head => (head (change/part (skip ([1 2 3 4 5]) 1) '- 3))
#macro [skip '. [word! | path!]] func [[manual] s e /local a r p p2] [
p: s/3
a: either word? p [
View piping.red
Red []
; WOW! dialectic version of function piping!
; . with a cutest dot!
; . to make expressions more readable!
; . can be invoked recursively (pipe [ pipe [...] . ... ])
; e.g. [1 2 3 4 5] . skip 1 . change/part '- 3 . head
; => head change/part skip [1 2 3 4 5] 1 '- 3
@hiiamboris
hiiamboris / arity.red
Last active Apr 12, 2021
get arity of any word or path
View arity.red
arity?: func [p [word! path!] /local p2] [
either word? p [
preprocessor/func-arity? spec-of get :p
][
; path format: obj1/obj2.../func/ref1/ref2...
; have to find a point where in that path a function starts
; i.e. p2: obj1/obj2.../func
; and the call itself is: func/ref1/ref2...
p2: as path! clear [] ; reuse the same block over and over again
until [
@hiiamboris
hiiamboris / benchcollect.red
Created May 19, 2018
A `collect` mezzanine variant of improved performance and RAM footprint
View benchcollect.red
Red []
; outright improvement of collect mezzanine's performance and RAM footprint
; the compiled default "collect"
collect1: :collect
; the interpreted default "collect"
collect2: func spec-of :collect body-of :collect
@hiiamboris
hiiamboris / glob-test.red
Last active Jun 17, 2018
list files recursively, with a pattern
View glob-test.red
Red [title: "glob func test script"]
#include %glob.red
unless value? 'input [input: none]
root: %globtest-temp-dir.$$$
if exists? root [print ["please remove the" root "from" what-dir "first"] input quit]
change-dir make-dir root
@hiiamboris
hiiamboris / composite.red
Last active Jun 12, 2018 — forked from greggirwin/composite.red
Composite func (compose for strings) for Red
View composite.red
Red [
Author: [@greggirwin @endo @toomasv]
Purpose: "COMPOSE for strings"
Notes: {
TBD: Security model for eval'ing expressions
TBD: Decide if support for custom marker and eval contexts are worthwhile
TBD: Finalize refinement names
}
]
@hiiamboris
hiiamboris / parse-then-test.red
Created Jul 21, 2018
Test of the `then` keyword
View parse-then-test.red
Red []
idiom: [ [a (x: b) | reset (x: c)] x | reset d ] ; 1
impl: [ a then b | reset c | reset d ] ; 2
;impl: [ a b | reset c | reset d ] ; 2
;impl: [ (f: yes) a (f: no) b | reset if (f) c | reset d ] ; 1
;impl: [ a b | reset not a c | reset d ] ; 3
;impl: [ not a c | reset a b | reset d ] ; 3
;impl: [ [a fail b] | reset c | reset d ] ; 2, nightly build only
;impl: [ p: a [b | :p reset d |] | reset c | reset d ] ; 1