Skip to content

Instantly share code, notes, and snippets.

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! []
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: #()
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! []
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 [
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 April 12, 2021 17:50
get arity of any word or path
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 16:46
A `collect` mezzanine variant of improved performance and RAM footprint
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 June 17, 2018 20:37
list files recursively, with a pattern
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 June 12, 2018 21:49 — forked from greggirwin/composite.red
Composite func (compose for strings) for 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 July 21, 2018 20:53
Test of the `then` keyword
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