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 [
@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 / 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 / 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
@hiiamboris
hiiamboris / inspect.red
Created December 11, 2018 17:09
image inspector func
Red []
inspect: function [i] [
pixel: 10x10
? (also i': make image! i/size * pixel
x: y: repeat y i/size/y [
repeat x i/size/x [
p: as-pair x y
draw i' compose [
line-width 1 pen (i/:p) fill-pen (i/:p)
@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
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