Welcome to the walkthrough of std.lang and it’s interaction with the javascript runtime. Ideally, the reader should have at least some experience with both clojure and javascript in order to get the most out of the tutorial as the library allows for seamless interop between a clojure runtime and a javascript one - whether it is on the server side - node, quickjs, osascript - as well as on the browser and other embedded js environments.
Setup
Let us briefly explore the std.lang transpiler.
(ns stdlang-book.walkthrough-js
(:require [std.lang :as l]))
std.lang can be used in different ways: - generate code for different languages - run the code in different runtimes of those languages
To specify a way to use it, we use l/script. This will create a runtime for evaluation.
^:kind/println
(l/script :js
{:require [[xt.lang.base-lib :as k]
[xt.lang.base-iter :as it]]})
It is now possible to transpile lisp forms to code:
If more than one environment is required, l/script+ is a way to create an annex that
In For example, let us define the following two annexes, named :code and :live.
Here we define :code as a way to use the transpiler to generate Javascript code, but not use it in any runtime.
^:kind/println
(l/script+ [:code :js ]
{:require [[xt.lang.base-lib :as k]
[xt.lang.base-iter :as it]]})
Here we define :live as a way to use the transpiler go generate Javascript code, and run it in a Node.js runtime.
^:kind/println
(l/script+ [:live :js ]
{:runtime :basic
:require [[xt.lang.base-lib :as k]
[xt.lang.base-iter :as it]]})
[#rt.basic[:server/basic :js vbbupw8dnnee 53609 1 ]]
Let us now use these two ways for basic arithmetic.
[ ;; No runtime, just generating code:
(l/! [:code ] (+ 1 2 ))
;; Generating, running in Node.js:
(l/! [:live ] (+ 1 2 ))]
Types
Types - Primitives
The seven primitive data types in JavaScript are string, number, bigint, boolean, undefined, symbol, and null. We work with examples from: https://www.w3docs.com/learn-javascript/methods-of-primitives.html
From the Javascript Runtime perspective, primitives are extremely important to understand for designing fast programs. They offer the following traits:
Immutability: Once a primitive value is created, it cannot be altered. For instance, when you create a string, you cannot change its individual characters. Any operation that seems to change a primitive actually creates a new primitive. Example:
Memory Efficiency: Primitives are stored directly in the stack memory where the variable is located. This direct storage makes access to primitive values faster and more memory-efficient than objects. Example:
Simple and Fast: Primitives are straightforward in their representation, making them simpler and faster to process compared to objects. They don’t have the overhead of object properties and methods. Example:
Boolean
true
true ;
true
false
false ;
false
Strings
"3"
"3" ;
"3"
"Hello World"
"Hello World" ;
"Hello World"
Numbers
3
3 ;
3
1.5
1.5 ;
1.5
1.54444444444444
1.54444444444444 ;
1.54444444444444
NaN
NaN ;
nil
Infinity
Infinity ;
nil
(- Infinity)
- Infinity ;
"-Infinity"
Bigint
(. (BigInt "0x1fffffffffffff" ) (toString))
BigInt ("0x1fffffffffffff" ). toString ();
"9007199254740991"
Symbol
(. (Symbol "hello" ) (toString))
Symbol ("hello" ). toString ();
"Symbol(hello)"
Types - Additional
Regex
#"^[Hh]ello d$"
/ ^[Hh] ello d $ / ;
<RegExp>
/ ^[Hh]ello d$/
Types - Collection
Arrays
[1 2 3 4 ]
[1 , 2 , 3 , 4 ];
[1 2 3 4 ]
["hello" ["world" ]]
["hello" , ["world" ]];
["hello" ["world" ]]
Objects
{:a 1 , :b 2 , :c 3 }
{"a" : 1 , "b" : 2 , "c" : 3 };
{"a" 1 , "b" 2 , "c" 3 }
{:a {:b {:c 3 }}}
{"a" : {"b" : {"c" : 3 }}};
{"a" {"b" {"c" 3 }}}
Objects - tab
(tab ["a" 1 ] ["b" 2 ] ["c" 3 ])
{"a" : 1 , "b" : 2 , "c" : 3 };
{"a" 1 , "b" 2 , "c" 3 }
Types - Checks
Typeof
(typeof nil )
typeof null ;
"object"
(typeof undefined)
typeof undefined ;
"undefined"
(typeof NaN)
typeof NaN ;
"number"
(typeof 1 )
typeof 1 ;
"number"
(typeof true )
typeof true ;
"boolean"
(typeof "hello" )
typeof "hello" ;
"string"
(typeof (Symbol "hello" ))
typeof Symbol ("hello" );
"symbol"
(typeof (BigInt "0x1fffffffffffff" ))
typeof BigInt ("0x1fffffffffffff" );
"bigint"
(typeof #"^[Hh]ello d$" )
typeof / ^[Hh] ello d $ / ;
"object"
(typeof [1 2 3 ])
typeof [1 , 2 , 3 ];
"object"
(typeof {:a 1 })
typeof {"a" : 1 };
"object"
Instanceof
(instanceof #"^[Hh]ello d$" RegExp)
/ ^[Hh] ello d $ / instanceof RegExp ;
true
Operations
Operations - Assignment
Var
(do (var x 1 ) x)
let x = 1 ;
x;
1
Const
(do (const x 1 ) x)
const x = 1 ;
x;
1
Reassign
(do (var x 1 ) (:= x 10 ) x)
let x = 1 ;
x = 10 ;
x;
10
Operations - Logic
Negation
(not true )
! true ;
false
(not false )
! false ;
true
(not 1 )
! 1 ;
false
(not 0 )
! 0 ;
true
Or
(or 0 false )
0 || false ;
false
(or 1 false )
1 || false ;
1
(or 0 true )
0 || true ;
true
And
(and 0 false )
0 && false ;
0
(and 1 false )
1 && false ;
false
(and 0 true )
0 && true ;
0
Ternary
(:? true 1 2 )
true ? 1 : 2 ;
1
(:? (or 0 0 ) 1 2 )
(0 || 0 ) ? 1 : 2 ;
2
Operations - Math
Addition
(+ 1 2 3 )
1 + 2 + 3 ;
6
(+ 1 10 )
1 + 10 ;
11
Subtraction
(- 10 )
- 10 ;
-10
(- 10 1.1 )
10 - 1.1 ;
8.9
(- 4 3 2 1.1 )
4 - 3 - 2 - 1.1 ;
-2.1
Multiplication
(* 10 20 )
10 * 20 ;
200
(* 4 3.3 2.2 1.1 )
4 * 3.3 * 2.2 * 1.1 ;
31.944000000000003
Division
(/ 10 )
1 / 10 ;
0.1
(/ 10 20 )
10 / 20 ;
0.5
(/ 4 3.3 2.2 1.1 )
4 / 3.3 / 2.2 / 1.1 ;
0.5008765339343851
Pow
(pow 10 2 )
10 ^ 2 ;
8
(pow 0.5 0.2 )
0.5 ^ 0.2 ;
0
Mod
(mod 1123 7 )
1123 % 7 ;
3
(mod 1123 7.1 )
1123 % 7.1 ;
1.2000000000000561
Operations - Comparison
Equals
(== 1 1 )
1 == 1 ;
true
(== 1 "1" )
1 == "1" ;
true
(== "hello" "hello" )
"hello" == "hello" ;
true
Triple Equals
(=== 1 1 )
1 === 1 ;
true
(=== 1 "1" )
1 === "1" ;
false
(=== "hello" "hello" )
"hello" === "hello" ;
true
Not Equals
(not= 1 2 )
1 != 2 ;
true
(not= 1 "1" )
1 != "1" ;
false
(not= "hello" "hello" )
"hello" != "hello" ;
false
Less Than
(< 1 2 )
1 < 2 ;
true
(< 1 1 )
1 < 1 ;
false
(< 1 "2" )
1 < "2" ;
true
Less Than Equals
(<= 1 2 )
1 <= 2 ;
true
(<= 1 1 )
1 <= 1 ;
true
(<= 1 "1" )
1 <= "1" ;
true
Greater Than
(> 3 2 )
3 > 2 ;
true
(> 3 3 )
3 > 3 ;
false
(> 3 "2" )
3 > "2" ;
true
Greater Than Equals
(>= 3 2 )
3 >= 2 ;
true
(>= 3 3 )
3 >= 3 ;
true
(>= 3 "3" )
3 >= "3" ;
true
Operations - Counter
Increment
(do (var x 1 ) (:++ x) x)
let x = 1 ;
++ x;
x;
2
Increment By
(do (var x 1 ) (:+= x 10 ) x)
let x = 1 ;
x += 10 ;
x;
11
Decrement
(do (var x 5 ) (:-- x) x)
let x = 5 ;
-- x;
x;
4
Decrement By
(do (var x 5 ) (:- = x 50 ) x)
let x = 5 ;
x -= 50 ;
x;
-45
Multiply By
(do (var x 5 ) (:*= x 50 ) x)
let x = 5 ;
x *= 50 ;
x;
250
Operations - Bitwise
Bitwise Or
(b:| 7 8 )
7 | 8 ;
15
(b:| 7 7 )
7 | 7 ;
7
(b:| 7 0 )
7 | 0 ;
7
Bitwise And
(b:& 7 8 )
7 & 8 ;
0
(b:& 7 7 )
7 & 7 ;
7
(b:& 7 0 )
7 & 0 ;
0
Bitwise Xor
(b:xor 7 8 )
7 ^ 8 ;
15
(b:xor 7 7 )
7 ^ 7 ;
0
(b:xor 7 0 )
7 ^ 0 ;
7
Bitshift Right
(b:>> 128 3 )
128 >> 3 ;
16
Bitshift Left
(b:<< 128 3 )
128 << 3 ;
1024
Operations - Functions
(fn [x y] (return (+ x y)))
function (x, y){
return x + y;
}
<function>
function (x,y){
return x + y;
}
(do (var hello (fn [x y] (return (+ x y)))) (hello 1 2 ))
let hello = function (x, y){
return x + y;
};
hello (1 , 2 );
3
Operations - Blocks
if block
(do (var arr [1 2 3 4 5 ]) (var out) (if (< (x:len arr) 10 ) (:= out true ) (:= out false )) out)
let arr = [1 , 2 , 3 , 4 , 5 ];
let out = null ;
if (arr. length < 10 ){
out = true ;
}
else {
out = false ;
}
out;
true
cond block
(do (var arr [1 2 3 4 5 ]) (var out) (cond (< (x:len arr) 5 ) (:= out "1" ) (< (x:len arr) 10 ) (:= out "2" ) :else (:= out "3" )) out)
let arr = [1 , 2 , 3 , 4 , 5 ];
let out = null ;
if (arr. length < 5 ){
out = "1" ;
}
else if (arr. length < 10 ){
out = "2" ;
}
else {
out = "3" ;
}
out;
"2"
while block
(do (var x []) (var i 0 ) (while (< i 5 ) (x:arr-push x i) (:++ i)) x)
let x = [];
let i = 0 ;
while (i < 5 ){
x. push (i);
++ i;
}
x;
[0 1 2 3 4 ]
for block
(do (var arr []) (for [(var i 1 ) (< i 5 ) (:++ i)] (x:arr-push arr i)) arr)
let arr = [];
for (let i = 1 ; i < 5 ; ++ i){
arr. push (i);
}
arr;
[1 2 3 4 ]
case block
(do (var arr 1 ) (var out) (case arr 1 (do (:= out 1 ) (break)) 2 (do (:= out 2 ) (break))) out)
let arr = 1 ;
let out = null ;
switch (arr){
case 1 :
out = 1 ;
break ;
case 2 :
out = 2 ;
break ;
}
out;
1
try/catch block
(do (var out "hello" ) (try (throw 1 ) (catch e (:= out "world" ))) out)
let out = "hello" ;
try {
throw 1 ;
}
catch (e){
out = "world" ;
}
out;
"world"
Base Lib
Base Lib - For
for:array
(do (var out := []) (k/for:array [e [1 2 3 4 ]] (if (> e 3 ) (break)) (x:arr-push out e )) out)
let out = [];
for (let e of [1 , 2 , 3 , 4 ]){
if (e > 3 ){
break ;
}
out. push (e);
};
out;
[1 2 3 ]
for:object
(do (var out := []) (var obj := {:a 1 , :b 2 }) (k/for:object [[k v] obj] (x:arr-push out [k v])) out)
let out = [];
let obj = {"a" : 1 , "b" : 2 };
for (let [k, v] of Object . entries (obj)){
out. push ([k, v]);
};
out;
[["a" 1 ] ["b" 2 ]]
for:index
(do (var out := []) (k/for:index [i [0 10 2 ]] (x:arr-push out i)) out)
let out = [];
for (let i = 0 ; i < 10 ; i = (i + 2 )){
out. push (i);
};
out;
[0 2 4 6 8 ]
for:return
(do (var out) (var success (fn [cb] (cb nil "OK" ))) (k/for:return [[ret err] (success (x:callback ))] {:success (:= out ret), :error (:= out err)}) out)
let out = null ;
let success = function (cb){
cb (null , "OK" );
};
success (function (err, ret){
if (err){
out = err;
}
else {
out = ret;
}
});
out;
"OK"
for:try
(do (var out := nil ) (k/for:try [[ret err] (do:> (x:err "hello" ))] {:success (:= out ret), :error (:= out err)}) out)
let out = null ;
try {
let ret = (function (){
throw "hello" ;
})();
out = ret;
}
catch (err){
out = err;
};
out;
"hello"
for:async
(do (var out := nil ) (k/for:async [[ret err] (+ 1 2 3 )] {:success (:= out ret), :error (:= out err)}))
let out = null ;
new Promise (function (resolve, reject){
resolve (1 + 2 + 3 );
}). then (function (ret){
out = ret;
}). catch (function (err){
out = err;
});
<Promise>
[object Promise]
Base Lib - Util
invoke
(k/invoke k/add 1 2 )
1 + 2 ;
3
unpack
[(k/unpack [1 2 3 ]) (k/unpack [4 5 6 ])]
[... [1 , 2 , 3 ],... [4 , 5 , 6 ]];
[1 2 3 4 5 6 ]
apply
(k/apply (fn:> [a b] (+ a b)) [1 2 ])
(function (a, b){
return a + b;
}). apply (null , [1 , 2 ]);
3
eval
(k/eval "1+2" )
eval ("1+2" );
3
(k/apply k/eval ["1+2" ])
(function (s){
return eval (s);
}). apply (null , ["1+2" ]);
3
len
(k/len "1+2" )
("1+2" ). length ;
3
(k/apply k/len ["1+2" ])
(function (arr){
return (arr). length ;
}). apply (null , ["1+2" ]);
3
cat
(k/cat "1+2" "+3" )
"1+2" + "+3" ;
"1+2+3"
(k/apply k/cat ["1+2" "+3" ])
(function (... args) {return args. join ('' )}). apply (null , ["1+2" , "+3" ]);
"1+2+3"
x:del
(do (var out {:a 1 }) (k/x:del (. out ["a" ])) out)
let out = {"a" : 1 };
delete out["a" ];
out;
{}
x:shell
(do (var cb {}) (defn call [] (k/x:shell "ls" cb)) (call))
let cb = {};
function call (){
let p = require ("child_process" );
p. exec ("ls" , function (err, res){
if (err){
if (cb["error" ]){
return cb. error (err);
}
}
else {
if (cb["success" ]){
return cb. success (res);
}
}
});
return ["async" ];;
}
call ();
["async" ]
x:offset-rlen
(k/x:offset-rlen 10 )
10 ;
10
nil?
(k/nil? "hello" )
null == "hello" ;
false
(k/apply k/nil? ["hello" ])
(function (x){
return null == x;
}). apply (null , ["hello" ]);
false
not-nil?
(k/not-nil? "hello" )
null != "hello" ;
true
(k/apply k/not-nil? ["hello" ])
(function (x){
return null != x;
}). apply (null , ["hello" ]);
true
to-string
(k/to-string 1 )
String (1 );
"1"
(k/apply k/to-string [1 ])
(function (obj){
return String (obj);
}). apply (null , [1 ]);
"1"
to-number
(k/to-number "1.1" )
Number ("1.1" );
1.1
(k/apply k/to-number ["1.1" ])
(function (obj){
return Number (obj);
}). apply (null , ["1.1" ]);
1.1
is-string?
(k/is-string? "1.1" )
"string" == (typeof "1.1" );
true
(k/apply k/is-string? ["1.1" ])
(function (obj){
return "string" == (typeof obj);
}). apply (null , ["1.1" ]);
true
is-number?
(k/is-number? 1.1 )
"number" == (typeof 1.1 );
true
(k/apply k/is-number? [1.1 ])
(function (obj){
return "number" == (typeof obj);
}). apply (null , [1.1 ]);
true
is-integer?
(k/is-integer? 1000 )
Number . isInteger (1000 );
true
(k/apply k/is-integer? [1000 ])
(function (obj){
return Number . isInteger (obj);
}). apply (null , [1000 ]);
true
is-boolean?
(k/is-boolean? false )
"boolean" == (typeof false );
true
(k/apply k/is-boolean? [false ])
(function (obj){
return "boolean" == (typeof obj);
}). apply (null , [false ]);
true
is-function?
(k/is-function? (fn [] (return 1 )))
"function" == (typeof (function (){
return 1 ;
}));
true
(k/apply k/is-function? [(fn [] (return 1 ))])
(function (x){
return "function" == (typeof x);
}). apply (null , [
function (){
return 1 ;
}
]);
true
is-array?
(k/is-array? [1 2 3 4 5 ])
Array . isArray ([1 , 2 , 3 , 4 , 5 ]);
true
(k/apply k/is-array? [[1 2 3 4 5 ]])
(function (x){
return Array . isArray (x);
}). apply (null , [[1 , 2 , 3 , 4 , 5 ]]);
true
is-object?
(k/is-object? {:a 1 , :b 2 })
(null != {"a" : 1 , "b" : 2 }) && ("object" == (typeof {"a" : 1 , "b" : 2 })) && ! Array . isArray ({"a" : 1 , "b" : 2 });
true
(k/apply k/is-object? [{:a 1 , :b 2 }])
(function (x){
return (null != x) && ("object" == (typeof x)) && ! Array . isArray (x);
}). apply (null , [{"a" : 1 , "b" : 2 }]);
true
type-native
(do [(k/type-native {}) (k/type-native [1 ]) (k/type-native (fn [])) (k/type-native 1 ) (k/type-native "" ) (k/type-native true )])
[
k. type_native ({}),
k. type_native ([1 ]),
k. type_native (function (){
}),
k. type_native (1 ),
k. type_native ("" ),
k. type_native (true )
];
["object" "array" "function" "number" "string" "boolean" ]
type-class
(do [(k/type-class {}) (k/type-class [1 ]) (k/type-class (fn [])) (k/type-class 1 ) (k/type-class "" ) (k/type-class true )])
[
k. type_class ({}),
k. type_class ([1 ]),
k. type_class (function (){
}),
k. type_class (1 ),
k. type_class ("" ),
k. type_class (true )
];
["object" "array" "function" "number" "string" "boolean" ]
print
(k/print "hello" )
console . log ("hello" );
nil
(k/apply k/print ["hello" ])
console . log . apply (null , ["hello" ]);
nil
random
(k/random)
Math . random ();
0.28243072845178885
(k/apply k/random [])
(function (){
return Math . random ();
}). apply (null , []);
0.32931068676338504
now-ms
(k/now-ms)
Date . now ();
1745742148326
(k/apply k/now-ms [])
(function (){
return Date . now ();
}). apply (null , []);
1745742148343
Base Lib - Global
!:G
Accesses the global object
!:G
globalThis;
<Object>
[object global]
(!:G CUSTOM)
globalThis["CUSTOM" ];
nil
global-set
(do (k/global-set "HELLO" 1 ) (. !:G ["HELLO" ]))
globalThis["HELLO" ] = 1 ;
globalThis["HELLO" ];
1
global-has?
(do (k/global-set "HELLO" 1 ) (k/global-has? "HELLO" ))
globalThis["HELLO" ] = 1 ;
! (null == globalThis["HELLO" ]);
true
global-del
(do (k/global-del "HELLO" ) (k/global-has? "HELLO" ))
globalThis["HELLO" ] = null ;
! (null == globalThis["HELLO" ]);
false
Base Lib - String
get-char
(k/get-char "abc" 0 )
"abc" . charCodeAt (0 );
97
(k/apply k/get-char ["abc" 0 ])
(function (s, i){
return s. charCodeAt (i);
}). apply (null , ["abc" , 0 ]);
97
gt-string
[(k/gt-string "a" "b" ) (k/gt-string "A" "a" )]
[0 > "b" . localeCompare ("a" ), 0 > "a" . localeCompare ("A" )];
[false true ]
lt-string
[(k/lt-string "a" "b" ) (k/lt-string "A" "a" )]
[0 > "a" . localeCompare ("b" ), 0 > "A" . localeCompare ("a" )];
[true false ]
split
(k/split "hello/world" "/" )
"hello/world" . split ("/" );
["hello" "world" ]
(k/apply k/split ["hello/world" "/" ])
(function (s, tok){
return s. split (tok);
}). apply (null , ["hello/world" , "/" ]);
["hello" "world" ]
join
(k/join "/" ["hello" "world" ])
["hello" , "world" ]. join ("/" );
"hello/world"
(k/apply k/join ["/" ["hello" "world" ]])
(function (s, arr){
return arr. join (s);
}). apply (null , ["/" , ["hello" , "world" ]]);
"hello/world"
replace
(k/replace "hello/world" "/" "_" )
"hello/world" . replace (new RegExp ("/" , "g" ), "_" );
"hello_world"
(k/apply k/replace ["hello/world" "/" "_" ])
(function (s, tok, replacement){
return s. replace (new RegExp (tok, "g" ), replacement);
}). apply (null , ["hello/world" , "/" , "_" ]);
"hello_world"
index-of
(k/index-of "hello/world" "/" )
"hello/world" . indexOf ("/" ) - 0 ;
5
(k/apply k/index-of ["hello/world" "/" ])
(function (s, tok){
return s. indexOf (tok) - 0 ;
}). apply (null , ["hello/world" , "/" ]);
5
substring
[(k/substring "hello/world" 3 ) (k/substring "hello/world" 3 8 )]
["hello/world" . substring (3 ), "hello/world" . substring (3 , 8 )];
["lo/world" "lo/wo" ]
to-uppercase
(k/to-uppercase "hello" )
"hello" . toUpperCase ();
"HELLO"
(k/apply k/to-uppercase ["hello" ])
(function (s){
return s. toUpperCase ();
}). apply (null , ["hello" ]);
"HELLO"
to-lowercase
(k/to-lowercase "hello" )
"hello" . toLowerCase ();
"hello"
(k/apply k/to-lowercase ["hello" ])
(function (s){
return s. toLowerCase ();
}). apply (null , ["hello" ]);
"hello"
to-fixed
(k/to-fixed 1.2 3 )
1.2 . toFixed (3 );
"1.200"
(k/apply k/to-fixed [1.2 3 ])
(function (n, digits){
return n. toFixed (digits);
}). apply (null , [1.2 , 3 ]);
"1.200"
trim
(k/trim " \n hello \n " )
" \n hello \n " . trim ();
"hello"
(k/apply k/trim [" \n hello \n " ])
(function (s){
return s. trim ();
}). apply (null , [" \n hello \n " ]);
"hello"
trim-left
(k/trim-left " \n hello \n " )
" \n hello \n " . trimLeft ();
"hello \n "
(k/apply k/trim-left [" \n hello \n " ])
(function (s){
return s. trimLeft ();
}). apply (null , [" \n hello \n " ]);
"hello \n "
trim-right
(k/trim-right " \n hello \n " )
" \n hello \n " . trimRight ();
" \n hello"
(k/apply k/trim-right [" \n hello \n " ])
(function (s){
return s. trimRight ();
}). apply (null , [" \n hello \n " ]);
" \n hello"
starts-with?
(k/starts-with? "Foo Bar" "Foo" )
k. starts_withp ("Foo Bar" , "Foo" );
true
ends-with?
(k/ends-with? "Foo Bar" "Bar" )
k. ends_withp ("Foo Bar" , "Bar" );
true
capitalize
(k/capitalize "hello" )
k. capitalize ("hello" );
"Hello"
decapitalize
(k/decapitalize "HELLO" )
k. decapitalize ("HELLO" );
"hELLO"
pad-left
(k/pad-left "000" 5 "-" )
k. pad_left ("000" , 5 , "-" );
"--000"
pad-right
(k/pad-right "000" 5 "-" )
k. pad_right ("000" , 5 , "-" );
"000--"
pad-lines
(k/pad-lines (k/join " \n " ["hello" "world" ]) 2 " " )
k. pad_lines (["hello" , "world" ]. join (" \n " ), 2 , " " );
" hello \n world"
split-long
(k/split-long "1234567890123456789012345678901234567890123456789012345678901234567890" 4 )
k. split_long (
"1234567890123456789012345678901234567890123456789012345678901234567890" ,
4
);
["1234" "5678" "9012" "3456" "7890" "1234" "5678" "9012" "3456" "7890" "1234" "5678" "9012" "3456" "7890" "1234" "5678" "90" ]
Base Lib - Encode
b64-encode
(k/b64-encode "hello" )
btoa ("hello" );
"aGVsbG8="
(k/apply k/b64-encode ["hello" ])
(function (s){
return btoa (s);
}). apply (null , ["hello" ]);
"aGVsbG8="
b64-decode
(k/b64-decode "aGVsbG8=" )
atob ("aGVsbG8=" );
"hello"
(k/apply k/b64-decode ["aGVsbG8=" ])
(function (s){
return atob (s);
}). apply (null , ["aGVsbG8=" ]);
"hello"
uri-encode
(k/uri-encode "+. \n " )
encodeURIComponent ("+. \n " );
"%2B.%0A%20"
(k/apply k/uri-encode ["+. \n " ])
(function (s){
return encodeURIComponent (s);
}). apply (null , ["+. \n " ]);
"%2B.%0A%20"
uri-decode
(k/uri-decode "%2B.%0A%20" )
decodeURIComponent ("%2B.%0A%20" );
"+. \n "
(k/apply k/uri-decode ["%2B.%0A%20" ])
(function (s){
return decodeURIComponent (s);
}). apply (null , ["%2B.%0A%20" ]);
"+. \n "
js-encode
(k/js-encode [1 2 {:a [{:b 3 }]}])
JSON . stringify ([1 , 2 , {"a" : [{"b" : 3 }]}]);
"[1,2,{ \" a \" :[{ \" b \" :3}]}]"
(k/apply k/js-encode [[1 2 {:a [{:b 3 }]}]])
(function (obj){
return JSON . stringify (obj);
}). apply (null , [[1 , 2 , {"a" : [{"b" : 3 }]}]]);
"[1,2,{ \" a \" :[{ \" b \" :3}]}]"
js-decode
(k/js-decode "[1,2,{ \" a \" :[{ \" b \" :3}]}]" )
JSON . parse ("[1,2,{ \" a \" :[{ \" b \" :3}]}]" );
[1 2 {"a" [{"b" 3 }]}]
(k/apply k/js-decode ["[1,2,{ \" a \" :[{ \" b \" :3}]}]" ])
(function (s){
return JSON . parse (s);
}). apply (null , ["[1,2,{ \" a \" :[{ \" b \" :3}]}]" ]);
[1 2 {"a" [{"b" 3 }]}]
json-push
(k/json-push "[1,2,3]" "4" )
"[1,2,3]" . substring (0 , "[1,2,3]" . length - 1 ) + "," + "4" + "]" ;
"[1,2,3,4]"
(k/apply k/json-push ["[1,2,3]" "4" ])
(function (json, e){
return json. substring (0 , json. length - 1 ) + "," + e + "]" ;
}). apply (null , ["[1,2,3]" , "4" ]);
"[1,2,3,4]"
json-push-first
(k/json-push-first "[1,2,3]" "0" )
"[" + "0" + "," + "[1,2,3]" . substring (1 );
"[0,1,2,3]"
(k/apply k/json-push-first ["[1,2,3]" "0" ])
(function (json, e){
return "[" + e + "," + json. substring (1 );
}). apply (null , ["[1,2,3]" , "0" ]);
"[0,1,2,3]"
Base Lib - Symbol
sym-full
(k/sym-full "hello" "world" )
k. sym_full ("hello" , "world" );
"hello/world"
sym-name
(k/sym-name "hello/world" )
k. sym_name ("hello/world" );
"world"
sym-ns
[(k/sym-ns "hello/world" ) (k/sym-ns "hello" )]
k. sym_ns ("hello/world" ), k. sym_ns ("hello" );
nil
sym-pair
(k/sym-pair "hello/world" )
k. sym_pair ("hello/world" );
["hello" "world" ]
Base Lib - Math
Base Lib - Math Basic
eq
[(k/eq 2 2 ) (k/eq 2 1 )]
[2 == 2 , 2 == 1 ];
[true false ]
(k/apply k/eq [1 1 ])
(function (a, b){
return a == b;
}). apply (null , [1 , 1 ]);
true
neq
[(k/neq 2 2 ) (k/neq 2 1 )]
[2 != 2 , 2 != 1 ];
[false true ]
(k/apply k/neq [1 1 ])
(function (a, b){
return a != b;
}). apply (null , [1 , 1 ]);
false
add
(k/add 1 2 )
1 + 2 ;
3
(k/apply k/add [1 2 ])
(function (a, b){
return a + b;
}). apply (null , [1 , 2 ]);
4
sub
(k/sub 1 2 )
1 - 2 ;
-1
(k/apply k/sub [1 2 ])
(function (a, b){
return a - b;
}). apply (null , [1 , 2 ]);
-1
mul
(k/mul 10 10 )
10 * 10 ;
100
(k/apply k/mul [1 2 ])
(function (a, b){
return a * b;
}). apply (null , [1 , 2 ]);
2
div
(k/div 10 2 )
10 / 2 ;
5
(k/apply k/div [1 2 ])
(function (a, b){
return a / b;
}). apply (null , [1 , 2 ]);
0.5
gt
[(k/gt 2 2 ) (k/gt 2 1 )]
[2 > 2 , 2 > 1 ];
[false true ]
(k/apply k/gt [1 2 ])
(function (a, b){
return a > b;
}). apply (null , [1 , 2 ]);
false
lt
[(k/lt 2 2 ) (k/lt 1 2 )]
[2 < 2 , 1 < 2 ];
[false true ]
(k/apply k/lt [1 2 ])
(function (a, b){
return a < b;
}). apply (null , [1 , 2 ]);
true
gte
[(k/gte 2 2 ) (k/gte 2 1 )]
[2 >= 2 , 2 >= 1 ];
[true true ]
(k/apply k/gte [1 2 ])
(function (a, b){
return a >= b;
}). apply (null , [1 , 2 ]);
false
lte
[(k/lte 2 2 ) (k/lte 1 2 )]
[2 <= 2 , 1 <= 2 ];
[true true ]
(k/apply k/lte [1 2 ])
(function (a, b){
return a <= b;
}). apply (null , [1 , 2 ]);
true
neg
[(k/neg 1 ) (k/neg 0 ) (k/neg -1 )]
[- (1 ),- (0 ),- (- 1 )];
[-1 0 1 ]
(k/apply k/neg [1 ])
(function (x){
return - (x);
}). apply (null , [1 ]);
-1
inc
(k/inc 1 )
1 + 1 ;
2
(k/apply k/inc [1 ])
(function (x){
return x + 1 ;
}). apply (null , [1 ]);
2
dec
(k/dec 1 )
1 - 1 ;
0
(k/apply k/dec [1 ])
(function (x){
return x - 1 ;
}). apply (null , [1 ]);
0
pow
(k/pow 2 3 )
Math . pow (2 , 3 );
8
(k/apply k/pow [5 6 ])
(function (base, n){
return Math . pow (base, n);
}). apply (null , [5 , 6 ]);
15625
quot
(k/quot 20 3 )
Math . floor (20 / 3 );
6
(k/apply k/quot [50 6 ])
(function (base, n){
return Math . floor (base / n);
}). apply (null , [50 , 6 ]);
8
sqrt
[(k/sqrt -1 ) (k/sqrt 1 )]
[Math . sqrt (- 1 ), Math . sqrt (1 )];
[nil 1 ]
(k/apply k/sqrt [16 ])
(function (num){
return Math . sqrt (num);
}). apply (null , [16 ]);
4
exp
(k/exp 3 )
Math . exp (3 );
20.085536923187668
(k/apply k/exp [6 ])
(function (num){
return Math . exp (num);
}). apply (null , [6 ]);
403.4287934927351
loge
(k/loge 3 )
Math . log (3 );
1.0986122886681096
(k/apply k/loge [6 ])
(function (num){
return Math . log (num);
}). apply (null , [6 ]);
1.791759469228055
log10
(k/log10 3 )
Math . log10 (3 );
0.47712125471966244
(k/apply k/log10 [6 ])
(function (num){
return Math . log10 (num);
}). apply (null , [6 ]);
0.7781512503836436
mod
(k/mod 20 3 )
20 % 3 ;
2
(k/apply k/mod [50 6 ])
(function (num, denom){
return num % denom;
}). apply (null , [50 , 6 ]);
2
mod-pos
[(mod -11 10 ) (k/mod-pos -11 10 )]
[- 11 % 10 , k. mod_pos (- 11 , 10 )];
[-1 9 ]
mod-offset
[(k/mod-offset 20 280 360 ) (k/mod-offset 280 20 360 ) (k/mod-offset 280 -80 360 ) (k/mod-offset 20 -60 360 ) (k/mod-offset 60 30 360 )]
[
k. mod_offset (20 , 280 , 360 ),
k. mod_offset (280 , 20 , 360 ),
k. mod_offset (280 ,- 80 , 360 ),
k. mod_offset (20 ,- 60 , 360 ),
k. mod_offset (60 , 30 , 360 )
];
[-100 100 0 -80 -30 ]
Base Lib - Math Checks
zero?
[(k/zero? 1 ) (k/zero? 0 )]
[1 == 0 , 0 == 0 ];
[false true ]
(k/apply k/zero? [1 ])
(function (x){
return x == 0 ;
}). apply (null , [1 ]);
false
pos?
[(k/pos? 1 ) (k/pos? 0 )]
[1 > 0 , 0 > 0 ];
[true false ]
(k/apply k/pos? [-1 ])
(function (x){
return x > 0 ;
}). apply (null , [- 1 ]);
false
neg?
[(k/neg? -1 ) (k/neg? 0 )]
[- 1 < 0 , 0 < 0 ];
[true false ]
(k/apply k/neg? [-1 ])
(function (x){
return x < 0 ;
}). apply (null , [- 1 ]);
true
even?
[(k/even? 2 ) (k/even? 1 )]
[0 == (2 % 2 ), 0 == (1 % 2 )];
[true false ]
(k/apply k/even? [-1 ])
(function (x){
return 0 == (x % 2 );
}). apply (null , [- 1 ]);
false
odd?
[(k/odd? 2 ) (k/odd? 1 )]
[! (0 == (2 % 2 )),! (0 == (1 % 2 ))];
[false true ]
(k/apply k/odd? [-1 ])
(function (x){
return ! (0 == (x % 2 ));
}). apply (null , [- 1 ]);
true
Base Lib - Math Util
abs
[(k/abs -1 ) (k/abs 1 )]
[Math . abs (- 1 ), Math . abs (1 )];
[1 1 ]
(k/apply k/abs [-1 ])
(function (num){
return Math . abs (num);
}). apply (null , [- 1 ]);
1
max
(k/max 1 2 3 2 )
Math . max (1 , 2 , 3 , 2 );
3
(k/apply k/max [1 2 3 2 ])
Math . max . apply (null , [1 , 2 , 3 , 2 ]);
3
min
(k/min 1 2 3 2 )
Math . min (1 , 2 , 3 , 2 );
1
(k/apply k/min [1 2 3 2 ])
Math . max . apply (null , [1 , 2 , 3 , 2 ]);
3
ceil
[(k/ceil -1.1 ) (k/ceil 1.1 )]
[Math . ceil (- 1.1 ), Math . ceil (1.1 )];
[-1 2 ]
(k/apply k/ceil [-1.1 ])
(function (num){
return Math . ceil (num);
}). apply (null , [- 1.1 ]);
-1
floor
[(k/floor -1.1 ) (k/floor 1.1 )]
[Math . floor (- 1.1 ), Math . floor (1.1 )];
[-2 1 ]
(k/apply k/floor [-1.1 ])
(function (num){
return Math . floor (num);
}). apply (null , [- 1.1 ]);
-2
gcd
(k/gcd 10 6 )
k. gcd (10 , 6 );
2
lcm
(k/lcm 10 6 )
k. lcm (10 , 6 );
30
mix
(k/mix 100 20 0.1 )
k. mix (100 , 20 , 0.1 );
92
sign
[(k/sign -10 ) (k/sign 10 )]
[k. sign (- 10 ), k. sign (10 )];
[-1 1 ]
round
[(k/round 0.9 ) (k/round 1.1 ) (k/round 1.49 ) (k/round 1.51 )]
[k. round (0.9 ), k. round (1.1 ), k. round (1.49 ), k. round (1.51 )];
[1 1 1 2 ]
clamp
[(k/clamp 0 5 6 ) (k/clamp 0 5 -1 ) (k/clamp 0 5 4 )]
[k. clamp (0 , 5 , 6 ), k. clamp (0 , 5 ,- 1 ), k. clamp (0 , 5 , 4 )];
[5 0 4 ]
Base Lib - Math Bitwise
bit-and
(k/bit-and 7 4 )
7 & 4 ;
4
(k/apply k/bit-and [7 4 ])
(function (a, b){
return a & b;
}). apply (null , [7 , 4 ]);
4
bit-or
(k/bit-or 3 4 )
3 | 4 ;
7
(k/apply k/bit-or [3 4 ])
(function (a, b){
return a | b;
}). apply (null , [3 , 4 ]);
7
bit-xor
(k/bit-xor 3 5 )
3 ^ 5 ;
6
(k/apply k/bit-xor [3 5 ])
(function (a, b){
return a ^ b;
}). apply (null , [3 , 5 ]);
6
bit-lshift
(k/bit-lshift 7 1 )
7 << 1 ;
14
(k/apply k/bit-lshift [7 1 ])
(function (x, n){
return x << n;
}). apply (null , [7 , 1 ]);
14
bit-rshift
(k/bit-rshift 7 1 )
7 >> 1 ;
3
(k/apply k/bit-rshift [7 1 ])
(function (x, n){
return x >> n;
}). apply (null , [7 , 1 ]);
3
bit-count
[(k/bit-count 16 ) (k/bit-count 10 ) (k/bit-count 3 ) (k/bit-count 7 )]
[k. bit_count (16 ), k. bit_count (10 ), k. bit_count (3 ), k. bit_count (7 )];
[1 2 2 3 ]
Base Lib - Math Trigonometry
sin
[(k/sin (/ 3.14159 4 )) (k/sin (/ 3.14159 6 ))]
[Math . sin (3.14159 / 4 ), Math . sin (3.14159 / 6 )];
[0.7071063120935576 0.4999996169872557 ]
(k/apply k/sin [(/ 3.14159 4 )])
(function (num){
return Math . sin (num);
}). apply (null , [3.14159 / 4 ]);
0.7071063120935576
cos
[(k/cos (/ 3.14159 4 )) (k/cos (/ 3.14159 6 ))]
[Math . cos (3.14159 / 4 ), Math . cos (3.14159 / 6 )];
[0.7071072502792263 0.8660256249168368 ]
(k/apply k/cos [(/ 3.14159 4 )])
(function (num){
return Math . cos (num);
}). apply (null , [3.14159 / 4 ]);
0.7071072502792263
tan
[(k/tan (/ 3.14159 4 )) (k/tan (/ 3.14159 6 ))]
[Math . tan (3.14159 / 4 ), Math . tan (3.14159 / 6 )];
[0.9999986732059836 0.5773496795031555 ]
(k/apply k/tan [(/ 3.14159 4 )])
(function (num){
return Math . tan (num);
}). apply (null , [3.14159 / 4 ]);
0.9999986732059836
asin
[(k/asin 0.5 ) (k/asin 0.8 )]
[Math . asin (0.5 ), Math . asin (0.8 )];
[0.5235987755982989 0.9272952180016123 ]
(k/apply k/asin [0.5 ])
(function (num){
return Math . asin (num);
}). apply (null , [0.5 ]);
0.5235987755982989
acos
[(k/acos 0.5 ) (k/acos 0.8 )]
[Math . acos (0.5 ), Math . acos (0.8 )];
[1.0471975511965979 0.6435011087932843 ]
(k/apply k/acos [0.5 ])
(function (num){
return Math . acos (num);
}). apply (null , [0.5 ]);
1.0471975511965979
atan
[(k/atan 0.5 ) (k/atan 0.8 )]
[Math . atan (0.5 ), Math . atan (0.8 )];
[0.4636476090008061 0.6747409422235527 ]
(k/apply k/atan [0.5 ])
(function (num){
return Math . atan (num);
}). apply (null , [0.5 ]);
0.4636476090008061
sinh
[(k/sinh (/ 3.14159 4 )) (k/sinh (/ 3.14159 6 ))]
[Math . sinh (3.14159 / 4 ), Math . sinh (3.14159 / 6 )];
[0.8686700827439109 0.5478529696006316 ]
(k/apply k/sinh [(/ 3.14159 4 )])
(function (num){
return Math . sinh (num);
}). apply (null , [3.14159 / 4 ]);
0.8686700827439109
cosh
[(k/cosh (/ 3.14159 4 )) (k/cosh (/ 3.14159 6 ))]
[Math . cosh (3.14159 / 4 ), Math . cosh (3.14159 / 6 )];
[1.324608512978198 1.1402380787801425 ]
(k/apply k/cosh [(/ 3.14159 4 )])
(function (num){
return Math . cosh (num);
}). apply (null , [3.14159 / 4 ]);
1.324608512978198
tanh
[(k/tanh (/ 3.14159 4 )) (k/tanh (/ 3.14159 6 ))]
[Math . tanh (3.14159 / 4 ), Math . tanh (3.14159 / 6 )];
[0.6557938245397708 0.4804724379900902 ]
(k/apply k/tanh [(/ 3.14159 4 )])
(function (num){
return Math . tanh (num);
}). apply (null , [3.14159 / 4 ]);
0.6557938245397708
Base Lib - Collection
Base Lib - Sequence
first
(k/first [1 2 3 ])
([1 , 2 , 3 ])[0 ];
1
(k/apply [k/first [[1 2 3 ]]])
(function (arr){
return arr[0 ];
}). apply (null , [[1 , 2 , 3 ]]);
1
second
(k/second [1 2 3 ])
([1 , 2 , 3 ])[1 ];
2
(k/apply [k/second [[1 2 3 ]]])
(function (arr){
return arr[1 ];
}). apply (null , [[1 , 2 , 3 ]]);
2
nth
(k/nth [1 2 3 ] 2 )
([1 , 2 , 3 ])[2 ];
3
(k/apply [k/nth [[1 2 3 ] 2 ]])
(function (arr, i){
return arr[i];
}). apply (null , [[1 , 2 , 3 ], 2 ]);
3
last
(k/last [1 2 3 ])
([1 , 2 , 3 ])[[1 , 2 , 3 ]. length + - 1 ];
3
(k/apply [k/last [[1 2 3 ]]])
(function (arr){
return arr[arr. length + - 1 ];
}). apply (null , [[1 , 2 , 3 ]]);
3
second-last
(k/second-last [1 2 3 ])
([1 , 2 , 3 ])[[1 , 2 , 3 ]. length + - 2 ];
2
(k/apply [k/second-last [[1 2 3 ]]])
(function (arr){
return arr[arr. length + - 2 ];
}). apply (null , [[1 , 2 , 3 ]]);
2
get-idx
[(k/get-idx [1 2 3 ] 1 ) (k/get-idx [1 2 3 ] 2 )]
[([1 , 2 , 3 ])[1 ], ([1 , 2 , 3 ])[2 ]];
[2 3 ]
(k/apply k/get-idx [[1 2 3 ] 1 ])
(function (arr, i, d){
return arr[i] || d;
}). apply (null , [[1 , 2 , 3 ], 1 ]);
2
set-idx
(do (var out := [1 2 3 4 5 ]) (k/set-idx out 2 5 ) out)
let out = [1 , 2 , 3 , 4 , 5 ];
out[2 ] = 5 ;
out;
[1 2 5 4 5 ]
(do (var out := [1 2 3 4 5 ]) (k/apply k/set-idx [out 2 5 ]) out)
let out = [1 , 2 , 3 , 4 , 5 ];
(function (arr, i, val){
return arr[i] = val;
}). apply (null , [out, 2 , 5 ]);
out;
[1 2 5 4 5 ]
is-empty?
[(k/is-empty? nil ) (k/is-empty? "" ) (k/is-empty? "123" ) (k/is-empty? []) (k/is-empty? [1 2 3 ]) (k/is-empty? {}) (k/is-empty? {:a 1 , :b 2 })]
[
k. is_emptyp (null ),
k. is_emptyp ("" ),
k. is_emptyp ("123" ),
k. is_emptyp ([]),
k. is_emptyp ([1 , 2 , 3 ]),
k. is_emptyp ({}),
k. is_emptyp ({"a" : 1 , "b" : 2 })
];
[true true false true false true false ]
Base Lib - Keys
has-key?
[(k/has-key? {:a 1 } "a" ) (k/has-key? {:a 1 } "b" )]
[({"a" : 1 })["a" ] != null , ({"a" : 1 })["b" ] != null ];
[true false ]
(k/apply k/has-key? [{:a 1 } "a" ])
(function (obj, k){
return obj[k] != null ;
}). apply (null , [{"a" : 1 }, "a" ]);
true
del-key
(do (var out := {:a 1 , :b 2 }) (k/del-key out "a" ) out)
let out = {"a" : 1 , "b" : 2 };
delete out["a" ];
out;
{"b" 2 }
(do (var out := {:a 1 , :b 2 }) (k/apply k/del-key [out "a" ]) out)
let out = {"a" : 1 , "b" : 2 };
(function (obj, k){
return delete obj[k];
}). apply (null , [out, "a" ]);
out;
{"b" 2 }
get-key
[(k/get-key {:a 1 } "a" ) (k/get-key {:a 1 } "b" )]
[({"a" : 1 })["a" ], ({"a" : 1 })["b" ]];
[1 nil ]
(k/apply k/get-key [{:a 1 } "a" ])
(function (obj, k, d){
return obj[k] || d;
}). apply (null , [{"a" : 1 }, "a" ]);
1
(k/apply k/get-key [{:a 1 } "b" 2 ])
(function (obj, k, d){
return obj[k] || d;
}). apply (null , [{"a" : 1 }, "b" , 2 ]);
2
get-path
[(k/get-path {:a {:b {:c 1 }}} ["a" "b" "c" ]) (k/get-path {:a 1 } ["b" ] 2 )]
[({"a" : {"b" : {"c" : 1 }}})["a" ]["b" ]["c" ], ({"a" : 1 })["b" ] || 2 ];
[1 2 ]
set-key
(do (var out := {:a 1 , :b 2 }) (k/set-key out "a" 5 ) out)
let out = {"a" : 1 , "b" : 2 };
out["a" ] = 5 ;
out;
{"a" 5 , "b" 2 }
(do (var out := {:a 1 , :b 2 }) (k/apply k/set-key [out "a" 5 ]) out)
let out = {"a" : 1 , "b" : 2 };
(function (obj, k, val){
return obj[k] = val;
}). apply (null , [out, "a" , 5 ]);
out;
{"a" 5 , "b" 2 }
copy-key
(do (var out := {}) (k/copy-key out {:a 1 } "a" ) out)
let out = {};
out["a" ] = ({"a" : 1 })["a" ];
out;
{"a" 1 }
(do (var out := {}) (k/copy-key out {:a 1 } ["c" "a" ]) out)
let out = {};
out["c" ] = ({"a" : 1 })["a" ];
out;
{"c" 1 }
(do (var out := {}) (k/apply k/copy-key [out {:a 1 , :b 2 } "a" ]) out)
let out = {};
(function (obj, src, k_or_arr){
return obj[k_or_arr] = src[k_or_arr];
}). apply (null , [out, {"a" : 1 , "b" : 2 }, "a" ]);
out;
{"a" 1 }
swap-key
(do (var out := {:a 1 , :b 2 }) (k/swap-key out "a" k/inc) out)
let out = {"a" : 1 , "b" : 2 };
out["a" ] = (out["a" ] + 1 )
out;
{"a" 2 , "b" 2 }
Base Lib - Array
x:arr-push
(do (var out [1 2 3 ]) (k/x:arr-push out 4 ) out)
let out = [1 , 2 , 3 ];
out. push (4 );
out;
[1 2 3 4 ]
x:arr-pop
(do (var out [1 2 3 ]) (k/x:arr-pop out) out)
let out = [1 , 2 , 3 ];
out. pop ();
out;
[1 2 ]
x:arr-push-first
(do (var out [1 2 3 ]) (k/x:arr-push-first out 0 ) out)
let out = [1 , 2 , 3 ];
out. unshift (0 );
out;
[0 1 2 3 ]
x:arr-pop-first
(do (var out [1 2 3 ]) (k/x:arr-pop-first out) out)
let out = [1 , 2 , 3 ];
out. shift ();
out;
[2 3 ]
x:arr-insert
(do (var out [1 2 3 ]) (k/x:arr-insert out (x:offset 2 ) "a" ) out)
let out = [1 , 2 , 3 ];
out. splice (2 , 0 , "a" );
out;
[1 2 "a" 3 ]
arr-lookup
(k/arr-lookup ["a" "b" "c" ])
k. arr_lookup (["a" , "b" , "c" ]);
{"a" true , "b" true , "c" true }
arr-every
[(k/arr-every [1 2 3 ] k/odd?) (k/arr-every [1 3 ] k/odd?)]
[
k. arr_every ([1 , 2 , 3 ], function (x){
return ! (0 == (x % 2 ));
}),
k. arr_every ([1 , 3 ], function (x){
return ! (0 == (x % 2 ));
})
];
[false true ]
arr-some
[(k/arr-some [1 2 3 ] k/even?) (k/arr-some [1 3 ] k/even?)]
[
k. arr_some ([1 , 2 , 3 ], function (x){
return 0 == (x % 2 );
}),
k. arr_some ([1 , 3 ], function (x){
return 0 == (x % 2 );
})
];
[true false ]
arr-each
(do (var out []) (k/arr-each [1 2 3 4 5 ] (fn [e ] (x:arr-push out (+ 1 e )))) out)
let out = [];
k. arr_each ([1 , 2 , 3 , 4 , 5 ], function (e){
out. push (1 + e);
});
out;
[2 3 4 5 6 ]
arr-omit
(k/arr-omit ["a" "b" "c" "d" ] 2 )
k. arr_omit (["a" , "b" , "c" , "d" ], 2 );
["a" "b" "d" ]
arr-reverse
(k/arr-reverse [1 2 3 4 5 ])
k. arr_reverse ([1 , 2 , 3 , 4 , 5 ]);
[5 4 3 2 1 ]
arr-find
(k/arr-find [1 2 3 4 5 ] (fn:> [x] (== x 3 )))
k. arr_find ([1 , 2 , 3 , 4 , 5 ], function (x){
return x == 3 ;
});
2
arr-zip
(k/arr-zip ["a" "b" "c" ] [1 2 3 ])
k. arr_zip (["a" , "b" , "c" ], [1 , 2 , 3 ]);
{"a" 1 , "b" 2 , "c" 3 }
arr-map
(k/arr-map [1 2 3 4 5 ] k/inc)
k. arr_map ([1 , 2 , 3 , 4 , 5 ], function (x){
return x + 1 ;
});
[2 3 4 5 6 ]
arr-clone
(k/arr-clone [1 2 3 ])
k. arr_clone ([1 , 2 , 3 ]);
[1 2 3 ]
arr-append
(do (var out [1 2 3 ]) (k/arr-append out [4 5 ]) out)
let out = [1 , 2 , 3 ];
k. arr_append (out, [4 , 5 ]);
out;
[1 2 3 4 5 ]
arr-slice
(k/arr-slice [1 2 3 4 5 ] 1 3 )
k. arr_slice ([1 , 2 , 3 , 4 , 5 ], 1 , 3 );
[2 3 ]
arr-rslice
(k/arr-rslice [1 2 3 4 5 ] 1 3 )
k. arr_rslice ([1 , 2 , 3 , 4 , 5 ], 1 , 3 );
[3 2 ]
arr-tail
(k/arr-tail [1 2 3 4 5 ] 3 )
k. arr_tail ([1 , 2 , 3 , 4 , 5 ], 3 );
[5 4 3 ]
arr-mapcat
(k/arr-mapcat [1 2 3 ] (fn:> [k] [k k k]))
k. arr_mapcat ([1 , 2 , 3 ], function (k){
return [k, k, k];
});
[1 1 1 2 2 2 3 3 3 ]
arr-partition
(k/arr-partition [1 2 3 4 5 6 7 8 9 10 ] 3 )
k. arr_partition ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ], 3 );
[[1 2 3 ] [4 5 6 ] [7 8 9 ] [10 ]]
arr-filter
(k/arr-filter [1 2 3 4 5 ] k/odd?)
k. arr_filter ([1 , 2 , 3 , 4 , 5 ], function (x){
return ! (0 == (x % 2 ));
});
[1 3 5 ]
arr-keep
(k/arr-keep [1 2 3 4 5 ] (fn:> [x] (:? (k/odd? x) x)))
k. arr_keep ([1 , 2 , 3 , 4 , 5 ], function (x){
return ! (0 == (x % 2 )) ? x : null ;
});
[1 3 5 ]
arr-keepf
(k/arr-keepf [1 2 3 4 5 ] k/odd? k/identity)
k. arr_keepf ([1 , 2 , 3 , 4 , 5 ], function (x){
return ! (0 == (x % 2 ));
}, k. identity );
[1 3 5 ]
arr-juxt
(k/arr-juxt [["a" 1 ] ["b" 2 ] ["c" 3 ]] k/first k/second)
k. arr_juxt ([["a" , 1 ], ["b" , 2 ], ["c" , 3 ]], function (arr){
return arr[0 ];
}, function (arr){
return arr[1 ];
});
{"a" 1 , "b" 2 , "c" 3 }
arr-foldl
(k/arr-foldl [1 2 3 4 5 ] k/add 0 )
k. arr_foldl ([1 , 2 , 3 , 4 , 5 ], function (a, b){
return a + b;
}, 0 );
15
arr-foldr
(k/arr-foldr [1 2 3 4 5 ] k/step-push [])
k. arr_foldr ([1 , 2 , 3 , 4 , 5 ], k. step_push , []);
[5 4 3 2 1 ]
arr-pipel
(k/arr-pipel [(fn:> [x] (* x 10 )) (fn:> [x] (+ x 10 ))] 1 )
k. arr_pipel ([
function (x){
return x * 10 ;
},
function (x){
return x + 10 ;
}
], 1 );
20
arr-piper
(k/arr-piper [(fn:> [x] (* x 10 )) (fn:> [x] (+ x 10 ))] 1 )
k. arr_piper ([
function (x){
return x * 10 ;
},
function (x){
return x + 10 ;
}
], 1 );
110
arr-group-by
(k/arr-group-by [["a" 1 ] ["a" 2 ] ["b" 3 ] ["b" 4 ]] k/first k/second)
k. arr_group_by ([["a" , 1 ], ["a" , 2 ], ["b" , 3 ], ["b" , 4 ]], function (arr){
return arr[0 ];
}, function (arr){
return arr[1 ];
});
{"a" [1 2 ], "b" [3 4 ]}
arr-range
[(k/arr-range 10 ) (k/arr-range [10 ]) (k/arr-range [2 8 ]) (k/arr-range [2 9 2 ])]
[
k. arr_range (10 ),
k. arr_range ([10 ]),
k. arr_range ([2 , 8 ]),
k. arr_range ([2 , 9 , 2 ])
];
[[0 1 2 3 4 5 6 7 8 9 ] [0 1 2 3 4 5 6 7 8 9 ] [2 3 4 5 6 7 ] [2 4 6 8 ]]
arr-intersection
(k/arr-intersection ["a" "b" "c" "d" ] ["c" "d" "e" "f" ])
k. arr_intersection (["a" , "b" , "c" , "d" ], ["c" , "d" , "e" , "f" ]);
["c" "d" ]
arr-difference
(k/arr-difference ["a" "b" "c" "d" ] ["c" "d" "e" "f" ])
k. arr_difference (["a" , "b" , "c" , "d" ], ["c" , "d" , "e" , "f" ]);
["e" "f" ]
arr-union
(k/arr-union ["a" "b" "c" "d" ] ["c" "d" "e" "f" ])
k. arr_union (["a" , "b" , "c" , "d" ], ["c" , "d" , "e" , "f" ]);
["a" "b" "c" "d" "e" "f" ]
arr-sort
[(k/arr-sort [3 4 1 2 ] k/identity (fn:> [a b] (< a b))) (k/arr-sort [3 4 1 2 ] k/identity (fn:> [a b] (< b a))) (k/arr-sort [["c" 3 ] ["d" 4 ] ["a" 1 ] ["b" 2 ]] k/first (fn:> [a b] (x:arr-str-comp a b))) (k/arr-sort [["c" 3 ] ["d" 4 ] ["a" 1 ] ["b" 2 ]] k/second (fn:> [a b] (< a b)))]
[
k. arr_sort ([3 , 4 , 1 , 2 ], k. identity , function (a, b){
return a < b;
}),
k. arr_sort ([3 , 4 , 1 , 2 ], k. identity , function (a, b){
return b < a;
}),
k. arr_sort ([["c" , 3 ], ["d" , 4 ], ["a" , 1 ], ["b" , 2 ]], function (arr){
return arr[0 ];
}, function (a, b){
return 0 > a. localeCompare (b);
}),
k. arr_sort ([["c" , 3 ], ["d" , 4 ], ["a" , 1 ], ["b" , 2 ]], function (arr){
return arr[1 ];
}, function (a, b){
return a < b;
})
];
[[1 2 3 4 ] [4 3 2 1 ] [["a" 1 ] ["b" 2 ] ["c" 3 ] ["d" 4 ]] [["a" 1 ] ["b" 2 ] ["c" 3 ] ["d" 4 ]]]
arr-sorted-merge
[(k/arr-sorted-merge [1 2 3 ] [4 5 6 ] k/lt) (k/arr-sorted-merge [1 2 4 ] [3 5 6 ] k/lt) (k/arr-sorted-merge (k/arr-reverse [1 2 4 ]) (k/arr-reverse [3 5 6 ]) k/gt)]
[
k. arr_sorted_merge ([1 , 2 , 3 ], [4 , 5 , 6 ], function (a, b){
return a < b;
}),
k. arr_sorted_merge ([1 , 2 , 4 ], [3 , 5 , 6 ], function (a, b){
return a < b;
}),
k. arr_sorted_merge (k. arr_reverse ([1 , 2 , 4 ]), k. arr_reverse ([3 , 5 , 6 ]), function (a, b){
return a > b;
})
];
[[1 2 3 4 5 6 ] [1 2 3 4 5 6 ] [6 5 4 3 2 1 ]]
arr-shuffle
(k/arr-shuffle [1 2 3 4 5 ])
k. arr_shuffle ([1 , 2 , 3 , 4 , 5 ]);
[4 5 3 2 1 ]
arr-pushl
[(k/arr-pushl [1 2 3 4 ] 5 ) (k/arr-pushl [1 2 3 4 ] 5 4 )]
[k. arr_pushl ([1 , 2 , 3 , 4 ], 5 ), k. arr_pushl ([1 , 2 , 3 , 4 ], 5 , 4 )];
[[1 2 3 4 5 ] [2 3 4 5 ]]
arr-pushr
[(k/arr-pushr [1 2 3 4 ] 5 ) (k/arr-pushr [1 2 3 4 ] 5 4 )]
[k. arr_pushr ([1 , 2 , 3 , 4 ], 5 ), k. arr_pushr ([1 , 2 , 3 , 4 ], 5 , 4 )];
[[5 1 2 3 4 ] [5 1 2 3 ]]
arr-join
(k/arr-join ["1" "2" "3" "4" ] " " )
k. arr_join (["1" , "2" , "3" , "4" ], " " );
"1 2 3 4"
arr-interpose
(k/arr-interpose ["1" "2" "3" "4" ] "XX" )
k. arr_interpose (["1" , "2" , "3" , "4" ], "XX" );
["1" "XX" "2" "XX" "3" "XX" "4" ]
arr-repeat
[(k/arr-repeat "1" 4 ) (k/arr-repeat (k/inc-fn -1 ) 4 )]
[k. arr_repeat ("1" , 4 ), k. arr_repeat (k. inc_fn (- 1 ), 4 )];
[["1" "1" "1" "1" ] [0 1 2 3 ]]
arr-random
(k/arr-random [1 2 3 4 ])
k. arr_random ([1 , 2 , 3 , 4 ]);
1
arr-normalise
(k/arr-normalise [1 2 3 4 ])
k. arr_normalise ([1 , 2 , 3 , 4 ]);
[0.1 0.2 0.3 0.4 ]
arr-sample
(k/arr-sample ["left" "right" "up" "down" ] [0.1 0.2 0.3 0.4 ])
k. arr_sample (["left" , "right" , "up" , "down" ], [0.1 , 0.2 , 0.3 , 0.4 ]);
"down"
arrayify
[(k/arrayify 1 ) (k/arrayify [1 ])]
[k. arrayify (1 ), k. arrayify ([1 ])];
[[1 ] [1 ]]
Base Lib - Object
obj-empty?
[(k/obj-empty? {}) (k/obj-empty? {:a 1 })]
[k. obj_emptyp ({}), k. obj_emptyp ({"a" : 1 })];
[true false ]
obj-not-empty?
[(k/obj-not-empty? {}) (k/obj-not-empty? {:a 1 })]
[k. obj_not_emptyp ({}), k. obj_not_emptyp ({"a" : 1 })];
[false true ]
obj-first-key
(k/obj-first-key {:a 1 })
k. obj_first_key ({"a" : 1 });
"a"
obj-first-val
(k/obj-first-val {:a 1 })
k. obj_first_val ({"a" : 1 });
1
obj-keys
(k/obj-keys {:a 1 , :b 2 })
k. obj_keys ({"a" : 1 , "b" : 2 });
["a" "b" ]
obj-vals
(k/obj-vals {:a 1 , :b 2 })
k. obj_vals ({"a" : 1 , "b" : 2 });
[1 2 ]
obj-pairs
(k/obj-pairs {:a 1 , :b 2 , :c 2 })
k. obj_pairs ({"a" : 1 , "b" : 2 , "c" : 2 });
[["a" 1 ] ["b" 2 ] ["c" 2 ]]
obj-clone
(k/obj-clone {:a 1 , :b 2 , :c 3 })
k. obj_clone ({"a" : 1 , "b" : 2 , "c" : 3 });
{"a" 1 , "b" 2 , "c" 3 }
obj-assign
(do (var out := {:a 1 }) (k/obj-assign out {:b 2 , :c 3 }) out)
let out = {"a" : 1 };
k. obj_assign (out, {"b" : 2 , "c" : 3 });
out;
{"a" 1 , "b" 2 , "c" 3 }
obj-assign-nested
[(k/obj-assign-nested {:a 1 } {:b 2 }) (k/obj-assign-nested {:a {:b {:c 1 }}} {:a {:b {:d 1 }}})]
[
k. obj_assign_nested ({"a" : 1 }, {"b" : 2 }),
k. obj_assign_nested ({"a" : {"b" : {"c" : 1 }}}, {"a" : {"b" : {"d" : 1 }}})
];
[{"a" 1 , "b" 2 } {"a" {"b" {"d" 1 , "c" 1 }}}]
obj-assign-with
(k/obj-assign-with {:a {:b true }} {:a {:c true }} k/obj-assign)
k. obj_assign_with ({"a" : {"b" : true }}, {"a" : {"c" : true }}, k. obj_assign );
{"a" {"b" true , "c" true }}
obj-from-pairs
(k/obj-from-pairs [["a" 1 ] ["b" 2 ] ["c" 3 ]])
k. obj_from_pairs ([["a" , 1 ], ["b" , 2 ], ["c" , 3 ]]);
{"a" 1 , "b" 2 , "c" 3 }
obj-del
(k/obj-del {:a 1 , :b 2 , :c 3 } ["a" "b" ])
k. obj_del ({"a" : 1 , "b" : 2 , "c" : 3 }, ["a" , "b" ]);
{"c" 3 }
obj-del-all
(k/obj-del-all {:a 1 , :b 2 , :c 3 })
k. obj_del_all ({"a" : 1 , "b" : 2 , "c" : 3 });
{}
obj-pick
(k/obj-pick {:a 1 , :b 2 , :c 3 } ["a" "b" ])
k. obj_pick ({"a" : 1 , "b" : 2 , "c" : 3 }, ["a" , "b" ]);
{"a" 1 , "b" 2 }
obj-omit
(k/obj-omit {:a 1 , :b 2 , :c 3 } ["a" "b" ])
k. obj_omit ({"a" : 1 , "b" : 2 , "c" : 3 }, ["a" , "b" ]);
{"c" 3 }
obj-transpose
(k/obj-transpose {:a "x" , :b "y" , :c "z" })
k. obj_transpose ({"a" : "x" , "b" : "y" , "c" : "z" });
{"z" "c" , "x" "a" , "y" "b" }
obj-nest
(k/obj-nest ["a" "b" ] 1 )
k. obj_nest (["a" , "b" ], 1 );
{"a" {"b" 1 }}
obj-map
(k/obj-map {:a 1 , :b 2 , :c 3 } k/inc)
k. obj_map ({"a" : 1 , "b" : 2 , "c" : 3 }, function (x){
return x + 1 ;
});
{"a" 2 , "b" 3 , "c" 4 }
obj-filter
(k/obj-filter {:a 1 , :b 2 , :c 3 } k/odd?)
k. obj_filter ({"a" : 1 , "b" : 2 , "c" : 3 }, function (x){
return ! (0 == (x % 2 ));
});
{"a" 1 , "c" 3 }
obj-keep
(k/obj-keep {:a 1 , :b 2 , :c 3 } (fn:> [x] (:? (k/odd? x) x)))
k. obj_keep ({"a" : 1 , "b" : 2 , "c" : 3 }, function (x){
return ! (0 == (x % 2 )) ? x : null ;
});
{"a" 1 , "c" 3 }
obj-keepf
(k/obj-keepf {:a 1 , :b 2 , :c 3 } k/odd? k/identity)
k. obj_keepf ({"a" : 1 , "b" : 2 , "c" : 3 }, function (x){
return ! (0 == (x % 2 ));
}, k. identity );
{"a" 1 , "c" 3 }
obj-intersection
(k/obj-intersection {:a true , :b true } {:c true , :b true })
k. obj_intersection ({"a" : true , "b" : true }, {"c" : true , "b" : true });
["b" ]
obj-difference
[(k/obj-difference {:a true , :b true } {:c true , :b true }) (k/obj-difference {:c true , :b true } {:a true , :b true })]
[
k. obj_difference ({"a" : true , "b" : true }, {"c" : true , "b" : true }),
k. obj_difference ({"c" : true , "b" : true }, {"a" : true , "b" : true })
];
[["c" ] ["a" ]]
obj-keys-nested
(k/obj-keys-nested {:a {:b {:c 1 , :d 2 }, :e {:f 4 , :g 5 }}} [])
k. obj_keys_nested ({"a" : {"b" : {"c" : 1 , "d" : 2 }, "e" : {"f" : 4 , "g" : 5 }}}, []);
[[["a" "b" "c" ] 1 ] [["a" "b" "d" ] 2 ] [["a" "e" "f" ] 4 ] [["a" "e" "g" ] 5 ]]
to-flat
[(k/to-flat {:a 1 , :b 2 , :c 3 }) (k/to-flat (k/obj-pairs {:a 1 , :b 2 , :c 3 }))]
[
k. to_flat ({"a" : 1 , "b" : 2 , "c" : 3 }),
k. to_flat (k. obj_pairs ({"a" : 1 , "b" : 2 , "c" : 3 }))
];
[["a" 1 "b" 2 "c" 3 ] ["a" 1 "b" 2 "c" 3 ]]
from-flat
(k/from-flat ["a" 1 "b" 2 "c" 3 ] k/step-set-key {})
k. from_flat (["a" , 1 , "b" , 2 , "c" , 3 ], k. step_set_key , {});
{"a" 1 , "b" 2 , "c" 3 }
get-in
(k/get-in {:a {:b {:c 1 }}} ["a" "b" ])
k. get_in ({"a" : {"b" : {"c" : 1 }}}, ["a" , "b" ]);
{"c" 1 }
set-in
(do (var out {:a {:b {:c 1 }}}) (k/set-in out ["a" "b" ] 2 ) out)
let out = {"a" : {"b" : {"c" : 1 }}};
k. set_in (out, ["a" , "b" ], 2 );
out;
{"a" {"b" 2 }}
eq-nested
[(k/eq-nested {:a {:b {:c 1 }}} {:a {:b {:c 1 }}}) (k/eq-nested {:a {:b {:c 1 }}} {:a {:b {:c 2 }}}) (k/eq-nested 1 1 ) (k/eq-nested 1 2 ) (k/eq-nested [1 ] [1 ]) (k/eq-nested [1 ] [2 ]) (k/eq-nested {:a [{:b {:c 1 }}]} {:a [{:b {:c 1 }}]}) (k/eq-nested {:a [{:b {:c 1 }}]} {:a [{:b {:c 2 }}]})]
[
k. eq_nested ({"a" : {"b" : {"c" : 1 }}}, {"a" : {"b" : {"c" : 1 }}}),
k. eq_nested ({"a" : {"b" : {"c" : 1 }}}, {"a" : {"b" : {"c" : 2 }}}),
k. eq_nested (1 , 1 ),
k. eq_nested (1 , 2 ),
k. eq_nested ([1 ], [1 ]),
k. eq_nested ([1 ], [2 ]),
k. eq_nested ({"a" : [{"b" : {"c" : 1 }}]}, {"a" : [{"b" : {"c" : 1 }}]}),
k. eq_nested ({"a" : [{"b" : {"c" : 1 }}]}, {"a" : [{"b" : {"c" : 2 }}]})
];
[true false true false true false true false ]
obj-diff
(k/obj-diff {:a 1 , :b 2 } {:a 1 , :c 2 })
k. obj_diff ({"a" : 1 , "b" : 2 }, {"a" : 1 , "c" : 2 });
{"c" 2 }
obj-diff-nested
[(k/obj-diff-nested {:a 1 , :b 2 } {:a 1 , :c 2 }) (k/obj-diff-nested {:a 1 , :b {:c 3 }} {:a 1 , :b {:d 3 }}) (k/obj-diff-nested {:a 1 , :b {:c {:d 3 }}} {:a 1 , :b {:c {:e 3 }}})]
[
k. obj_diff_nested ({"a" : 1 , "b" : 2 }, {"a" : 1 , "c" : 2 }),
k. obj_diff_nested ({"a" : 1 , "b" : {"c" : 3 }}, {"a" : 1 , "b" : {"d" : 3 }}),
k. obj_diff_nested ({"a" : 1 , "b" : {"c" : {"d" : 3 }}}, {"a" : 1 , "b" : {"c" : {"e" : 3 }}})
];
[{"c" 2 } {"b" {"d" 3 }} {"b" {"c" {"e" 3 }}}]
objify
decodes object if string
(k/objify "{}" )
k. objify ("{}" );
{}
clone-nested
(k/clone-nested {:a [1 2 3 {:b [4 5 6 ]}]})
k. clone_nested ({"a" : [1 , 2 , 3 , {"b" : [4 , 5 , 6 ]}]});
{"a" [1 2 3 {"b" [4 5 6 ]}]}
walk
(k/walk [1 {:a {:b 3 }}] (fn [x] (return (:? (k/is-number? x) (+ x 1 ) x))) k/identity)
k. walk ([1 , {"a" : {"b" : 3 }}], function (x){
return ("number" == (typeof x)) ? (x + 1 ) : x;
}, k. identity );
[2 {"a" {"b" 4 }}]
get-data
(k/get-data {:a 1 , :b "hello" , :c {:d [1 2 (fn:>)], :e "hello" , :f {:g (fn:>), :h 2 }}})
k. get_data ({
"a" : 1 ,
"b" : "hello" ,
"c" : {
"d" : [
1 ,
2 ,
function (){
return null ;
}
],
"e" : "hello" ,
"f" : {
"g" : function (){
return null ;
},
"h" : 2
}
}
});
{"a" 1 , "b" "hello" , "c" {"d" [1 2 "<function>" ], "f" {"g" "<function>" , "h" 2 }, "e" "hello" }}
get-spec
(k/get-spec {:a 1 , :b "hello" , :c {:d [1 2 (fn:>)], :e "hello" , :f {:g (fn:>), :h 2 }}})
k. get_spec ({
"a" : 1 ,
"b" : "hello" ,
"c" : {
"d" : [
1 ,
2 ,
function (){
return null ;
}
],
"e" : "hello" ,
"f" : {
"g" : function (){
return null ;
},
"h" : 2
}
}
});
{"a" "number" , "b" "string" , "c" {"d" ["number" "number" "function" ], "f" {"g" "function" , "h" "number" }, "e" "string" }}
Iter Lib
Iter Lib - Util
for:iter
(do (var out []) (for:iter [e [1 2 3 4 ]] (x:arr-push out (* 2 e ))) out)
let out = [];
for (let e of [1 , 2 , 3 , 4 ]){
out. push (2 * e);
};
out;
[2 4 6 8 ]
iter-from-obj
(it/arr< (it/iter-from-obj {:a 1 , :b 2 }))
it. arr_lt (Object . entries ({"a" : 1 , "b" : 2 })[Symbol . iterator ]());
[["a" 1 ] ["b" 2 ]]
iter-from-arr
(it/arr< (it/iter-from-arr [1 2 3 4 5 ]))
it. arr_lt ([1 , 2 , 3 , 4 , 5 ][Symbol . iterator ]());
[1 2 3 4 5 ]
iter-from
(it/arr< (it/iter-from [1 2 3 4 5 ]))
it. arr_lt ([1 , 2 , 3 , 4 , 5 ][Symbol . iterator ]());
[1 2 3 4 5 ]
iter
(it/iter [1 2 3 4 5 ])
it. iter ([1 , 2 , 3 , 4 , 5 ]);
<Iterator>
[object Array Iterator]
iter?
(it/iter? (it/iter []))
it. iterp (it. iter ([]));
true
iter-next
(it/iter-next (it/iter [1 2 3 ]))
it. iter ([1 , 2 , 3 ]). next ();
{"value" 1 , "done" false }
iter-has?
[(it/iter-has? 123 ) (it/iter-has? [1 2 3 ])]
[
null != 123 [Symbol . iterator ],
null != [1 , 2 , 3 ][Symbol . iterator ]
];
[false true ]
iter-native?
[(it/iter-native? (it/iter [1 2 3 ])) (it/iter-native? 1 )]
[
"function" == (typeof it. iter ([1 , 2 , 3 ])["next" ]),
"function" == (typeof 1 ["next" ])
];
[true false ]
iter-eq
(do (var eq-fn (fn:> [a b] (== a b))) [(it/iter-eq (it/iter [1 2 4 4 ]) (it/iter [1 2 4 4 ]) eq-fn) (it/iter-eq (it/iter [1 2 4 4 ]) (it/iter [1 2 3 4 ]) eq-fn) (it/iter-eq (it/iter [1 2 4 ]) (it/iter [1 2 4 4 ]) eq-fn) (it/iter-eq (it/iter [1 2 4 4 ]) (it/iter [1 2 4 ]) eq-fn)])
let eq_fn = function (a, b){
return a == b;
};
[
it. iter_eq (it. iter ([1 , 2 , 4 , 4 ]), it. iter ([1 , 2 , 4 , 4 ]), eq_fn),
it. iter_eq (it. iter ([1 , 2 , 4 , 4 ]), it. iter ([1 , 2 , 3 , 4 ]), eq_fn),
it. iter_eq (it. iter ([1 , 2 , 4 ]), it. iter ([1 , 2 , 4 , 4 ]), eq_fn),
it. iter_eq (it. iter ([1 , 2 , 4 , 4 ]), it. iter ([1 , 2 , 4 ]), eq_fn)
];
[true false false false ]
iter-null
(it/arr< (it/iter-null))
it. arr_lt (it. iter_null ());
[]
collect
(it/collect [1 2 3 4 ] k/step-push [])
it. collect ([1 , 2 , 3 , 4 ], k. step_push , []);
[1 2 3 4 ]
nil<
(it/nil< (it/iter [1 2 3 4 ]))
it. nil_lt (it. iter ([1 , 2 , 3 , 4 ]));
nil
arr<
(it/arr< (it/iter [1 2 3 4 ]))
it. arr_lt (it. iter ([1 , 2 , 3 , 4 ]));
[1 2 3 4 ]
obj<
(it/obj< (it/iter [["a" 2 ] ["b" 4 ]]))
it. obj_lt (it. iter ([["a" , 2 ], ["b" , 4 ]]));
{"a" 2 , "b" 4 }
take
(it/arr< (it/take 4 (it/iter [1 2 3 4 5 6 7 ])))
it. arr_lt (it. take (4 , it. iter ([1 , 2 , 3 , 4 , 5 , 6 , 7 ])));
[1 2 3 4 ]
constantly
(it/arr< (it/take 4 (it/constantly 1 )))
it. arr_lt (it. take (4 , it. constantly (1 )));
[1 1 1 1 ]
iterate
(it/arr< (it/take 4 (it/iterate k/inc 11 )))
it. arr_lt (it. take (4 , it. iterate (function (x){
return x + 1 ;
}, 11 )));
[11 12 13 14 ]
repeatedly
(it/arr< (it/take 5 (it/repeatedly (fn [] (return 5 )))))
it. arr_lt (it. take (5 , it. repeatedly (function (){
return 5 ;
})));
[5 5 5 5 5 ]
cycle
(it/arr< (it/take 5 (it/cycle [1 2 3 ])))
it. arr_lt (it. take (5 , it. cycle ([1 , 2 , 3 ])));
[1 2 3 1 2 ]
range
(it/arr< (it/range [-10 -3 ]))
it. arr_lt (it. range ([- 10 ,- 3 ]));
[-10 -9 -8 -7 -6 -5 -4 ]
drop
(it/arr< (it/drop 3 (it/range 10 )))
it. arr_lt (it. drop (3 , it. range (10 )));
[3 4 5 6 7 8 9 ]
peek
(do (var out := []) (it/nil< (it/peek (fn [e ] (k/step-push out e )) [1 2 3 4 5 ])) out)
let out = [];
it. nil_lt (it. peek (function (e){
k. step_push (out, e);
}, [1 , 2 , 3 , 4 , 5 ]));
out;
[1 2 3 4 5 ]
map
(it/arr< (it/map k/inc [1 2 3 ]))
it. arr_lt (it. map (function (x){
return x + 1 ;
}, [1 , 2 , 3 ]));
[2 3 4 ]
mapcat
[(it/arr< (it/mapcat (fn:> [x] [x x]) [1 2 3 ])) (it/arr< (it/mapcat (fn:> [x] x) [[1 2 3 ] [4 5 6 ]])) (it/arr< (it/mapcat (fn:> [x] (it/range x)) (it/range 4 ))) (it/arr< (it/mapcat (fn:> [x] x) [(it/range 3 ) (it/range 3 )]))]
[
it. arr_lt (it. mapcat (function (x){
return [x, x];
}, [1 , 2 , 3 ])),
it. arr_lt (it. mapcat (function (x){
return x;
}, [[1 , 2 , 3 ], [4 , 5 , 6 ]])),
it. arr_lt (it. mapcat (function (x){
return it. range (x);
}, it. range (4 ))),
it. arr_lt (it. mapcat (function (x){
return x;
}, [it. range (3 ), it. range (3 )]))
];
[[1 1 2 2 3 3 ] [1 2 3 4 5 6 ] [0 0 1 0 1 2 ] [0 1 2 0 1 2 ]]
concat
(it/arr< (it/concat [(it/range 3 ) (it/range [4 6 ])]))
it. arr_lt (it. concat ([it. range (3 ), it. range ([4 , 6 ])]));
[0 1 2 4 5 ]
filter
(it/arr< (it/filter k/odd? [1 2 3 4 ]))
it. arr_lt (it. filter (function (x){
return ! (0 == (x % 2 ));
}, [1 , 2 , 3 , 4 ]));
[1 3 ]
keep
(it/arr< (it/keep (fn:> [x] (:? (k/odd? x) {:a x})) [1 2 3 4 ]))
it. arr_lt (it. keep (function (x){
return ! (0 == (x % 2 )) ? {"a" : x} : null ;
}, [1 , 2 , 3 , 4 ]));
[{"a" 1 } {"a" 3 }]
partition
(it/arr< (it/partition 3 (it/range 10 )))
it. arr_lt (it. partition (3 , it. range (10 )));
[[0 1 2 ] [4 5 6 ] [8 9 ]]
take-nth
[(it/arr< (it/take-nth 2 (it/range 10 ))) (it/arr< (it/take-nth 3 (it/range 10 ))) (it/arr< (it/take-nth 4 (it/drop 1 (it/range 10 ))))]
[
it. arr_lt (it. take_nth (2 , it. range (10 ))),
it. arr_lt (it. take_nth (3 , it. range (10 ))),
it. arr_lt (it. take_nth (4 , it. drop (1 , it. range (10 ))))
];
[[0 2 4 6 8 ] [0 3 6 9 ] [1 5 9 ]]