Boolean logic

Functions that work with true and false values.

true

true
; returns 1

false

false
; returns 0

not

not true
; returns 0
not false
; returns 1

and

true .and true
; returns 1
false .and true
; returns 0

or

true .or true
; returns 1
false .or true
; returns 1
true .or false
; returns 1
false .or false
; returns 0

xor

true .xor true
; returns 0
false .xor true
; returns 1
true .xor false
; returns 1
false .xor false
; returns 0

all

all { 1 2 3 }
; returns 3
all { 1 0 3 }
; returns 0

any

any { 1 2 3 }
; returns 1
any { 1 0 3 }
; returns 1
any { 0 0 3 }
; returns 3

any\with

any\with 10 { + 10 , * 10 }
; returns 20

Numbers

Working with numbers, integers and decimals.

inc

inc 123
; returns 124
inc "123"
; correctly causes error:
; builtin `inc` requires argument 1 to be: Integer. 

is-positive

is-positive 123
; returns 1
is-positive -123
; returns 0
is-positive "123"
; correctly causes error:
; builtin `is-positive` requires argument 1 to be: Integer, Decimal. 

is-zero

is-zero 0
; returns 1
is-zero 123
; returns 0
is-zero "123"
; correctly causes error:
; builtin `is-zero` requires argument 1 to be: Integer, Decimal. 

multiple-of

10 .multiple-of 2
; returns 1
10 .multiple-of 3
; returns 0

odd

3 .odd
; returns 1
2 .odd
; returns 0

even

3 .even
; returns 0
2 .even
; returns 1

mod

4 .mod 2
; returns 0
5 .mod 2
; returns 1
5 .mod 3
; returns 2

random\integer

random\integer 2 |type?
; returns integer
random\integer 1 |< 2
; returns 1

inc!

a:: 123 inc! 'a a
; returns 124
inc! 123
; correctly causes error:
; builtin `inc!` requires argument 1 to be: Word. 

dec!

a:: 123 dec! 'a a
; returns 122
dec! 123
; correctly causes error:
; builtin `dec!` requires argument 1 to be: Word. 

_.

4 . .type?
; returns void

_+

1 + 1
; returns 2
3 + 4
; returns 7
5.6 + 7.8
; returns 13.399999999999999
"A" + "b"
; returns "Ab"
"A" + 1
; returns "A1"
{ 1 2 } + { 3 4 }
; returns { 1 2 3 4 }
dict { "a" 1 } |+ { "b" 2 }
; returns dict { a 1 b 2 }
dict { "a" 1 } |+ dict { "b" 2 }
; returns dict { a 1 b 2 }

_-

2 - 1
; returns 1
5 - 6
; returns -1

_*

4 * 2
; returns 8
2.5 * -2
; returns -5

_/

4 / 2
; returns 2
102 / 2
; returns 51

_//

5 // 2
; returns 2
102 // 5
; returns 20
7.99 // 2
; returns 3

_=

5 = 5
; returns 1
5 = 4
; returns 0

_>

6 > 5
; returns 1
5 > 5
; returns 0

_>=

5 >= 6
; returns 0
5 >= 5
; returns 1
6 >= 5
; returns 1

_<

5 < 6
; returns 1
5 < 5
; returns 0

_<=

5 <= 6
; returns 1
5 <= 5
; returns 1

Strings

newline

newline
; returns "
; "

ln

"123" .ln
; returns "123
; "

trim

trim " ASDF "
; returns "ASDF"

trim\

trim\ "__ASDF__" "_"
; returns "ASDF"

trim ight

trim\right "__ASDF__" "_"
; returns "__ASDF"

trim\left

trim\left "___ASDF__" "_"
; returns "ASDF__"

replace

replace "...xoxo..." "xo" "LoL"
; returns "...LoLLoL..."

substring

substring "...xoxo..." 3 7
; returns "xoxo"

contains

contains "...xoxo..." "xo"
; returns 1
contains "...xoxo..." "lol"
; returns 0

has-suffix

has-suffix "xoxo..." "xoxo"
; returns 0
has-suffix "...xoxo" "xoxo"
; returns 1

has-prefix

has-prefix "xoxo..." "xoxo"
; returns 1
has-prefix "...xoxo" "xoxo"
; returns 0

index?

index? "...xo..." "xo"
; returns 3

position?

position? "...xo..." "xo"
; returns 4

encode\base64

encode\base64 "abcd"
; returns "YWJjZA=="

decode\base64

decode\base64 encode\base64 "abcd"
; returns "abcd"

space

"abcd" .space
; returns "abcd "

capitalize

capitalize "abcde"
; returns "Abcde"

to-lower

to-lower "ABCDE"
; returns "abcde"

to-upper

to-upper "abcde"
; returns "ABCDE"

concat3

concat3 "aa" "BB" "cc"
; returns "aaBBcc"

join

join { "Mary" "Anne" }
; returns "MaryAnne"
join { "Spot" "Fido" "Rex" }
; returns "SpotFidoRex"

join\with

join\with { "Mary" "Anne" } " "
; returns "Mary Anne"
join\with { "Spot" "Fido" "Rex" } "/"
; returns "Spot/Fido/Rex"

split

split "a,b,c" ","
; returns { "a" "b" "c" }

split\quoted

split\quoted "`a,b`,c,d" "," "`"
; returns { "`a,b`" "c" "d" }

split\many

split\many "192.0.0.1" "."
; returns { "192" "0" "0" "1" }
split\many "abcd..e.q|bar" ".|"
; returns { "abcd" "e" "q" "bar" }

split\every

split\every "abcdefg" 3
; returns { "abc" "def" "g" }
split\every "abcdefg" 2
; returns { "ab" "cd" "ef" "g" }
split\every "abcdef" 2
; returns { "ab" "cd" "ef" }

Collections

random

random { 1 2 3 } |type?
; returns integer

unpack

unpack { { 123 } { 234 } }
; returns { 123 234 }
unpack { { { 123 } } { 234 } }
; returns { { 123 } 234 }

sample

sample { 1 2 3 4 } 2 |length?
; returns 2
sample { 123 123 123 123 } 3 |-> 0
; returns 123

max

max { 8 2 10 6 }
; returns 10
max list { 8 2 10 6 }
; returns 10
try { max { } } |type?
; returns error
try { max list { } } |type?
; returns error

min

min { 8 2 10 6 }
; returns 2
min list { 8 2 10 6 }
; returns 2
try { min { } } |type?
; returns error
try { min list { } } |type?
; returns error

avg

avg { 8 2 10 6 }
; returns 6.5
avg list { 8 2 10 6 }
; returns 6.5
try { avg { } } |type?
; returns error
try { avg list { } } |type?
; returns error

sum

sum { 8 2 10 6 }
; returns 26
sum { 8 2 10 6.5 }
; returns 26.5
sum { }
; returns 0
sum list { 8 2 10 6 }
; returns 26
sum list { 8 2 10 6.5 }
; returns 26.5
sum list { }
; returns 0

mul

mul { 1 2 3 4 5 }
; returns 120
mul { 1 2 3.3 4 5 }
; returns 132

first

first { 1 2 3 4 }
; returns 1
first "abcde"
; returns "a"
first list { 1 2 3 }
; returns 1

rest

rest { 1 2 3 4 }
; returns { 2 3 4 }
rest "abcde"
; returns "bcde"
rest list { 1 2 3 }
; returns list { 2 3 }

rest\from

rest\from { 1 2 3 4 5 6 } 3
; returns { 4 5 6 }
rest\from "abcdefg" 1
; returns "bcdefg"
rest\from list { 1 2 3 4 } 2
; returns list { 3 4 }

tail

tail { 1 2 3 4 5 6 7 } 3
; returns { 5 6 7 }
tail "abcdefg" 4
; returns "defg"
tail list { 1 2 3 4 } 1
; returns list { 4 }

second

second { 123 234 345 }
; returns 234

third

third { 123 234 345 }
; returns 345

last

last { 1 2 }
; returns 2
last "abcd"
; returns "d"
last list { 4 5 6 }
; returns 6

head

head { 4 5 6 7 } 3
; returns { 4 5 6 }
head "abcdefg" 2
; returns "ab"
head "abcdefg" 4
; returns "abcd"
head list { 10 20 30 40 } 2
; returns list { 10 20 }

nth

nth { 1 2 3 4 5 } 4
; returns 4
nth { "a" "b" "c" "d" "e" } 2
; returns "b"

values

dict { "a" 1 "b" 2 "c" 3 } |values
; returns list { 1 2 3 }

sort

sort { 6 12 1 }
; returns { 1 6 12 }
sort x: { 6 12 1 } x
; returns { 6 12 1 }
sort { "b" "c" "a" }
; returns { "a" "b" "c" }

sort!

x: { 6 12 1 } , sort! x
; correctly causes error:
; builtin `sort!` requires argument 1 to be: Block, List. 
x: ref { 1 6 12 } , sort! x , x
; returns { 1 6 12 }

sort\by

sort\by { 6 12 1 } fn { a b } { a < b }
; returns { 1 6 12 }
sort\by { 6 12 1 } fn { a b } { a > b }
; returns { 12 6 1 }
sort\by { { x 6 } { x 12 } { x 1 } } fn { a b } { second a |< second b }
; returns { { x 1 } { x 6 } { x 12 } }

unique

list { 3 2 3 5 3 2 } .unique |sort
; returns list { 2 3 5 }
unique list { 1 1 2 2 3 } |sort
; returns list { 1 2 3 }
unique list { 1 1 2 2 } |sort
; returns list { 1 2 }
unique { 1 1 2 2 3 } |sort
; returns { 1 2 3 }
unique { 1 1 2 2 } |sort
; returns { 1 2 }
unique "aabbc" |length?
; returns 3
unique "ab" |length?
; returns 2

reverse

reverse { 3 1 2 3 }
; returns { 3 2 1 3 }
reverse { 3 1 2 3 }
; returns { 3 2 1 3 }

reverse!

reverse! { 3 1 2 3 }
; correctly causes error:
; builtin `reverse!` requires argument 1 to be: Block, String, List. 
reverse! ref { 3 2 1 3 }
; returns { 3 2 1 3 }

concat

"abcd" .concat "cde"
; returns "abcdcde"
concat { 1 2 3 4 } { 2 4 5 }
; returns { 1 2 3 4 2 4 5 }

union

union { 1 2 3 4 } { 2 4 5 } |length?
; returns 5
union list { 1 2 3 4 } list { 2 4 5 } |length?
; returns 5
union { 8 2 } { 1 9 } |sort
; returns { 1 2 8 9 }
union { 1 2 } { } |sort
; returns { 1 2 }
union { } { 1 9 } |sort
; returns { 1 9 }
union { } { }
; returns { }
union list { 1 2 } list { 1 2 3 4 } |sort
; returns list { 1 2 3 4 }
union list { 1 2 } list { 1 } |sort
; returns list { 1 2 }
union list { 1 2 } list { } |sort
; returns list { 1 2 }
union list { } list { 1 2 } |sort
; returns list { 1 2 }
union list { } list { }
; returns list { }

range

range 1 5
; returns { 1 2 3 4 5 }

is-empty

{ } .is-empty
; returns 1
dict { } |is-empty
; returns 1
table { 'a 'b } { } |is-empty
; returns 1

length?

{ 1 2 3 } .length?
; returns 3
length? "abcd"
; returns 4
table { 'val } { 1 2 3 4 } |length?
; returns 4
vector { 10 20 30 } |length?
; returns 3

keys

dict { "a" 1 "b" 2 "c" 3 } |keys |length?
; returns 3
table { "a" "b" "c" } { 1 2 3 } |keys |length?
; returns 3

_->

{ 23 34 45 } |-> 1
; returns 34

_<-

0 <- { 23 34 45 }
; returns 23

_<~

2 <~ { 23 34 45 }
; returns 34

_~>

{ 23 34 45 } |~> 1
; returns 23

intersection

"abcd" .intersection "cde"
; returns "cd"
intersection { 1 2 3 4 } { 2 4 5 }
; returns { 2 4 }
intersection { 1 3 5 6 } { 2 3 4 5 }
; returns { 3 5 }
intersection { 1 2 3 } { }
; returns { }
intersection { } { 2 3 4 }
; returns { }
intersection { 1 2 3 } { 4 5 6 }
; returns { }
intersection { } { }
; returns { }
intersection list { 1 3 5 6 } list { 2 3 4 5 }
; returns list { 3 5 }
intersection list { 1 2 3 } list { }
; returns list { }
intersection list { } list { 2 3 4 }
; returns list { }
intersection list { 1 2 3 } list { 4 5 6 }
; returns list { }
intersection list { } list { }
; returns list { }

intersection\by

intersection\by "foobar" "fbx" fn { a b } { a .contains b }
; returns "fb"
intersection\by "fooBar" "Fbx" fn { a b } { a .to-lower .contains to-lower b }
; returns "fB"
intersection\by { "foo" 33 } { 33 33 } fn { a b } { a .contains b }
; returns { 33 }
intersection\by { "foo" "bar" 33 } { 42 } fn { a b } { map a { .type? } |contains b .type? }
; returns { 33 }
intersection\by { { "foo" x } { "bar" y } } { { "bar" z } } fn { a b } { map a { .first } |contains first b }
; returns { { "bar" y } }

difference

"abcde" .difference "cde"
; returns "ab"
difference { 1 2 3 4 } { 2 4 }
; returns { 1 3 }
difference list { "Bob" "Sal" "Joe" } list { "Joe" }
; returns list { "Bob" "Sal" }
difference "abc" "bc"
; returns "a"
difference "abc" "abc"
; returns ""
difference "abc" ""
; returns "abc"
difference "" ""
; returns ""
difference { 1 3 5 6 } { 2 3 4 5 }
; returns { 1 6 }
difference { 1 2 3 } { }
; returns { 1 2 3 }
difference { } { 2 3 4 }
; returns { }
difference { } { }
; returns { }
difference list { 1 3 5 6 } list { 2 3 4 5 }
; returns list { 1 6 }
difference list { 1 2 3 } list { }
; returns list { 1 2 3 }
difference list { } list { 2 3 4 }
; returns list { }
difference list { } list { }
; returns list { }

transpose

{ { 1 2 3 } { 4 5 6 } } .transpose
; returns { { 1 4 } { 2 5 } { 3 6 } }
{ { 1 4 } { 2 5 } { 3 6 } } .transpose
; returns { { 1 2 3 } { 4 5 6 } }

remove-last!

x: ref { 1 2 3 4 } remove-last! 'x x
; returns { 1 2 3 }
x: ref { 1 2 3 4 } remove-last! 'x
; returns { 1 2 3 }

append!

x: ref { 1 2 3 } append! 4 'x , x
; returns { 1 2 3 4 }

change th!

x: ref { 1 222 3 } change\nth! x 2 222 , x
; returns { 1 222 3 }

Contexts

Context related functions

raw-context

c: raw-context { x: 123 } c/x
; returns 123
y: 123 try { c: raw-context { x: y } } |type?
; returns error
try { c: raw-context { x: inc 10 } } |type?
; returns error

isolate

c: isolate { x: 123 } c/x
; returns 123
y: 123 c: isolate { x: y } c/x
; returns 123
c: isolate { x: inc 10 } c/x
; returns 11
y: 99 c: isolate { x: does { y } } try { c/x } |type?
; returns error
y: 99 c: isolate { t: ?try x: does { t { y } } } c/x |type?
; returns error

context

c: context { x: 123 } c/x
; returns 123
y: 123 c: context { x: y } c/x
; returns 123
c: context { x: inc 10 } c/x
; returns 11
y: 123 c: context { x: does { y } } c/x
; returns 123

private

private { x: 123 }
; returns 123
y: 123 private { x: y }
; returns 123
private { x: inc 10 }
; returns 11
y: 123 private { does { y } } :f f
; returns 123

private\

private\ "what are we doing here" { x: 234 1000 + x }
; returns 1234

extends

ct: context { p: 123 } cn: extends ct { r: p + 234 } cn/r
; returns 357

bind!

c: context { y: 123 } cc: bind! context { z: does { y + 234 } } c , cc/z
; returns 357

unbind

c: context { y: 123 } cc: bind! context { z: does { y + 234 } } c , unbind cc cc/z
; returns 357
[Opword: _+]
c: context { y: 123 } cc: bind! context { z: does { y + 234 } } c , dd: unbind cc dd/z
; correctly causes error:
; word not found: y 

pass

101 .pass { 202 }
; returns 101
101 .pass { 202 + 303 }
; returns 101

wrap

wrap { prn "*" } { prn "x" }
; prints "*x*"

keep

20 .keep { + 202 } { + 101 }
; returns 222

change!

x: 123 , change! 234 'x , x
; returns 234
a:: 123 change! 333 'a a
; returns 333
a:: 123 change! 124 'a
; returns 1
a:: 123 change! 123 'a
; returns 0

set!

set! { 123 234 } { a b } b
; returns 234

unset!

x: 1 unset! 'x x: 2
; returns 2

Functions

functions that create functions

does

does { 123 } |type?
; returns function
x: does { 123 } x
; returns 123

fn1

fn1 { .pass { } } |type?
; returns function
x: fn1 { } , x 123
; returns 123
x: fn1 { .pass { } } , x 123
; returns 123
x: fn1 { + 1 } , x 123
; returns 124

fn

fn { } { } |type?
; returns function
x: fn { } { 234 } , x
; returns 234
x: fn { x } { x } , x 123
; returns 123
x: fn { x } { + 123 } , x 123
; returns 246

pfn

pfn { } { } |type?
; returns function
x: pfn { x } { + 123 } , x 123
; returns 246
x: pfn { x } { .print } , x 123
; correctly causes error:
; word not found: print 

Values and Types

to-word

to-word "test"
; returns test
to-word 123
; correctly causes error:
; builtin `to-word` requires argument 1 to be: String, Word. 

to-integer

to-integer "123"
; returns 123
to-integer "abc"
; correctly causes error:
; strconv.Atoi: parsing "abc": invalid syntax in builtin to-integer. 

to-decimal

to-decimal "123.4"
; returns 123.4
to-decimal "abc"
; correctly causes error:
; strconv.ParseFloat: parsing "abc": invalid syntax in builtin to-decimal. 

to-string

to-string 'test
; returns "test"
to-string 123
; returns "123"
to-string 123.4
; returns "123.400000"
to-string "test"
; returns "test"

to-char

to-char 42
; returns "*"
to-char "*"
; correctly causes error:
; builtin `to-char` requires argument 1 to be: Integer. 

to-block

list [ 1 2 3 ] |to-block |type?
; returns block
list [ 1 2 3 ] |to-block |first
; returns 1

to-context

dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type?
; returns ctx

is-string

is-string "test"
; returns 1
is-string 'test
; returns 0
is-string 123
; returns 0

is-integer

is-integer 123
; returns 1
is-integer 123.4
; returns 0
is-integer "123"
; returns 0

is-decimal

is-decimal 123
; returns 1
is-decimal 123
; returns 0
is-decimal "123.4"
; returns 0

is-number

is-number 123
; returns 1
is-number 123.4
; returns 1
is-number "123"
; returns 0

to-uri

to-uri "https://example.com"
; returns https://example.com

to-file

to-file "example.txt"
; returns file://example.txt
to-file 123
; returns file://123

type?

type? "test"
; returns string
type? 123.4
; returns decimal

types?

types? { "test" 123 }
; returns { string integer }

dump

does { 1 } |dump
; returns "fn { } { 1 }"

mold

mold 123
; returns "123"
mold { 123 }
; returns "{ 123 }"

mold owrap

mold\nowrap 123
; returns "123"
mold\nowrap { 123 }
; returns "123"
mold\nowrap { 123 234 }
; returns "123 234"

doc!

x: private { doc! "some doc" doc? }
; returns "some doc"

doc?

x: private { doc! "some doc" doc? }
; returns "some doc"

doc\of?

x: context { doc! "some doc" } doc\of? x
; returns "some doc"

ref

is-ref ref { 1 2 3 }
; returns 1

deref

is-ref deref ref { 1 2 3 }
; returns 0

is-ref

ref { } |is-ref
; returns 1
{ } |is-ref
; returns 0

dict

dict { "a" 123 } |-> "a"
; returns 123

list

list { "a" 123 } |-> 1
; returns "a"

Printing

prns

prns "xy"
; prints "xy "

prn

prn "xy"
; prints "xy"

print

print "xy"
; prints "xy
; "

format

format 123 "num: %d"
; returns "num: 123"

prnf

prnf 123 "num: %d"
; prints "num: 123"

embed

embed 101 "val {}"
; returns "val 101"

prnv

prnv 101 "val {}"
; prints "val 101"

printv

printv 101 "val {}"
; prints "val 101
; "

print\ssv

print\ssv { 101 "asd" }
; prints "101 asd
; "

print\csv

print\csv { 101 "asd" }
; prints "101,asd
; "

probe

probe 101
; prints "[Integer: 101]
; "

inspect

inspect 101
; returns "[Integer: 101]"

esc

esc-val

load

load " 1 2 3 " |third
; returns 3
load "{ 1 2 3 }" |first |third
; returns 3

Flow control

if

if 1 { 222 }
; returns 222
if 0 { 333 }
; returns 0

^if

x: does { ^if 1 { 222 } 555 } x
; returns 222
x: does { ^if 0 { 333 } 444 } x
; returns 444

either

either 1 { 222 } { 333 }
; returns 222
either 0 { 222 } { 333 }
; returns 333

switch

switch 101 { 101 { 111 } 202 { 222 } }
; returns 111
switch 202 { 101 { 111 } 202 { 222 } }
; returns 222

cases

cases 0 { { 1 > 0 } { + 100 } { 2 > 1 } { + 1000 } }
; returns 1100
cases 0 { { 1 > 0 } { + 100 } { 2 < 1 } { + 1000 } }
; returns 100
cases 0 { { 1 < 0 } { + 100 } { 2 > 1 } { + 1000 } }
; returns 1000
cases 0 { { 1 < 0 } { + 100 } { 2 < 1 } { + 1000 } }
; returns 0
cases 1 { { 1 > 0 } { + 100 } { 2 < 1 } { + 1000 } _ { * 3 } }
; returns 101
cases 1 { { 1 < 0 } { + 100 } { 2 > 1 } { + 1000 } _ { * 3 } }
; returns 1001
cases 1 { { 1 < 0 } { + 100 } { 2 < 1 } { + 1000 } _ { * 3 } }
; returns 3

do

do { 123 + 123 }
; returns 246
do { 123 + }
; correctly causes error:
; argument 2 of 2 missing of builtin: 'Adds or joins two values together (Integers, Strings, Uri-s and Blocks) (core) (_+)' 

try

try { 123 + 123 }
; returns 246
try { 123 + "asd" } |type?
; returns error
try { 123 + } |type?
; returns error

with

with 100 { + 11 }
; returns 111
with 100 { + 11 , * 3 }
; returns 300

do\in

c: context { x: 100 } do\in c { x * 9.99 }
; returns 999
c: context { x: 100 } do\in c { inc! 'x }
; returns 101
c: context { x: 100 } do\in c { x:: 200 } c/x
; returns 200
c: context { x: 100 } do\in c { x:: 200 , x }
; returns 200

try\in

c: context { x: 100 } try\in c { x * 9.99 }
; returns 999
c: context { x: 100 } try\in c { inc! 'x }
; returns 101
c: context { x: 100 } try\in c { x:: 200 , x }
; returns 200
c: context { x: 100 } try\in c { x:: 200 } c/x
; returns 200
c: context { x: 100 } try\in c { inc! 'y } |type?
; returns error

do\par

c: context { x: 100 } do\par c { x * 9.99 }
; returns 999
c: context { x: 100 } do\par c { inc! 'x }
; returns 101
c: context { x: 100 } do\par c { x:: 200 , x }
; returns 200
c: context { x: 100 } do\par c { x:: 200 } c/x
; returns 100

capture-stdout

capture-stdout { print "hello" }
; returns "hello
; "
capture-stdout { loop 3 { prns "x" } }
; returns "x x x "

time-it

time-it { sleep 100 }
; returns 100

vals

x: 1 y: 2 vals { x y }
; returns { 1 2 }
x: 1 y: 2 vals { 1 y }
; returns { 1 2 }
x: 1 y: 2 try { vals { z y } } |type?
; returns error

vals\with

x: 1 y: 2 vals\with 10 { + x , * y }
; returns { 11 20 }
x: 1 y: 2 vals\with 100 { + 10 , * 8.9 }
; returns { 110 890 }

current

c: context { x: 9999 , incr: fn\in { } current { x:: inc x } } c/incr c/x
; returns 10000

parent

y: 99 c: context { incr: fn\in { } parent { y:: inc y } } c/incr y
; returns 100

parent\of

ct: context { p: 123 } parent\of ct |= current
; returns 1

Iteration

Iteration over collections

loop

3 .loop { prns "x" }
; prints "x x x "
3 .loop { + 1 }
; returns 4

produce

produce 5 0 { + 3 }
; returns 15
produce 3 ">" { + "x>" }
; returns ">x>x>x>"
produce 3 { } { .concat "x" }
; returns { "x" "x" "x" }
produce 3 { } { :x .concat length? x }
; returns { 0 1 2 }
produce 5 { 2 } { :acc .last :x * x |concat* acc }
; returns { 2 4 16 256 65536 4294967296 }

produce\while

x: 0 produce\while { x < 100 } 1 { * 2 :x }
; returns 64
x: 0 produce\while { x < 100 } 1 { * 2 :x .prns }
; prints "2 4 8 16 32 64 128 "

produce\

produce\ 5 1 'acc { * acc , + 1 }
; returns 1

forever

forever { "once" .prn .return }
; prints "once"
forever { "once" .return }
; returns "once"

forever\with

forever\with 1 { .prn .return }
; prints "1"

for

for { 1 2 3 } { prns "x" }
; prints "x x x "
{ "a" "b" "c" } .for { .prns }
; prints "a b c "

walk

walk { 1 2 3 } { .prns .rest }
; prints "1 2 3  2 3  3  "
x: 0 walk { 1 2 3 } { :b .first + x :x , b .rest } x
; returns 6

purge

purge { 1 3 3 } { .even }
; returns { 1 3 }
purge { } { .even }
; returns { }
purge list { 1 2 3 } { .even }
; returns list { 1 3 }
purge list { } { .even }
; returns list { }
purge "1234" { .to-integer .even }
; returns { "1" "3" }
purge "" { .to-integer .even }
; returns { }

purge!

{ 1 3 3 } :x purge! { .even } 'x , x
; returns { 1 3 }

map

map { 1 2 3 } { + 1 }
; returns { 2 3 4 }
map { } { + 1 }
; returns { }
map { "aaa" "bb" "c" } { .length? }
; returns { 3 2 1 }
map list { "aaa" "bb" "c" } { .length? }
; returns list { 3 2 1 }
map list { 3 4 5 6 } { .multiple-of 3 }
; returns list { 1 0 0 1 }
map list { } { + 1 }
; returns list { }
map "123" { .to-integer }
; returns { 1 2 3 }
map "123" ?to-integer
; returns { 1 2 3 }
map "" { + "-" }
; returns { }

map\pos

map\pos { 1 2 3 } 'i { + i }
; returns { 2 4 6 }
map\pos { } 'i { + i }
; returns { }
map\pos list { 1 2 3 } 'i { + i }
; returns list { 2 4 6 }
map\pos list { } 'i { + i }
; returns list { }
map\pos "abc" 'i { + i }
; returns { "a1" "b2" "c3" }
map\pos "" 'i { + i }
; returns { }

map\idx

map\idx { 1 2 3 } 'i { + i }
; returns { 1 3 5 }
map\idx { } 'i { + i }
; returns { }
map\idx list { 1 2 3 } 'i { + i }
; returns list { 1 3 5 }
map\idx list { } 'i { + i }
; returns list { }
map\idx "abc" 'i { + i }
; returns { "a0" "b1" "c2" }
map\idx "" 'i { + i }
; returns { }

reduce

reduce { 1 2 3 } 'acc { + acc }
; returns 6
reduce list { 1 2 3 } 'acc { + acc }
; returns 6
reduce "abc" 'acc { + acc }
; returns "cba"
try { reduce { } 'acc { + acc } } |type?
; returns error
try { reduce list { } 'acc { + acc } } |type?
; returns error
try { reduce "" 'acc { + acc } } |type?
; returns error

fold

fold { 1 2 3 } 'acc 1 { + acc }
; returns 7
fold { } 'acc 1 { + acc }
; returns 1
fold list { 1 2 3 } 'acc 1 { + acc }
; returns 7
fold list { } 'acc 1 { + acc }
; returns 1
fold "abc" 'acc "123" { + acc }
; returns "cba123"
fold "" 'acc "123" { + acc }
; returns "123"

partition

partition { 1 2 3 4 } { > 2 }
; returns { { 1 2 } { 3 4 } }
partition { "a" "b" 1 "c" "d" } { .is-integer }
; returns { { "a" "b" } { 1 } { "c" "d" } }
partition { "a" "b" 1 "c" "d" } ?is-integer
; returns { { "a" "b" } { 1 } { "c" "d" } }
partition { } { > 2 }
; returns { { } }
partition list { 1 2 3 4 } { > 2 }
; returns list { list { 1 2 } list { 3 4 } }
partition list { "a" "b" 1 "c" "d" } ?is-integer
; returns list { list { "a" "b" } list { 1 } list { "c" "d" } }
partition list { } { > 2 }
; returns list { list { } }
partition "aaabbccc" { , }
; returns list { "aaa" "bb" "ccc" }
partition "" { , }
; returns list { "" }
partition "aaabbccc" ?is-string
; returns list { "aaabbccc" }

group

{ } .list .group { .first }
; returns dict { }
try { { 1 2 3 4 } .group { .even } } |type?
; returns error

filter

filter { 1 2 3 4 } { .even }
; returns { 2 4 }
filter { 1 2 3 4 } ?even
; returns { 2 4 }
filter { } { .even }
; returns { }
filter list { 1 2 3 4 } { .even }
; returns list { 2 4 }
filter list { 1 2 3 4 } ?even
; returns list { 2 4 }
filter list { } { .even }
; returns list { }
filter "1234" { .to-integer .even }
; returns { "2" "4" }
filter "01234" ?to-integer
; returns { "1" "2" "3" "4" }
filter "" { .to-integer .even }
; returns { }

seek

seek { 1 2 3 4 } { .even }
; returns 2
seek list { 1 2 3 4 } { .even }
; returns 2
seek "1234" { .to-integer .even }
; returns "2"
try { seek { 1 2 3 4 } { > 5 } } |type?
; returns error
try { seek list { 1 2 3 4 } { > 5 } } |type?
; returns error
try { seek "1234" { .to-integer > 5 } } |type?
; returns error

Other

functions related to date and time

return

x: fn { } { return 101 202 } x
; returns 101

cmd

hello
cmd "echo "hello""
; returns 1

rye

rye .type?
; returns native

sleep

time-it { sleep 10 }
; returns 10

seconds

5 .seconds
; returns 5000000000

minutes

5 .minutes
; returns 300000000000

hours

5 .hours
; returns 18000000000000