
🔍
Additional general functions
| Name | Description / comments |
|---|---|
| assert_or[<cond>;<err or fn>] | Assert cond~1b, otherwise throw signal with err string or call fn[] |
| assert[<cond>;<err>] | Assert cond~1b, otherwise throw signal with message "--- Assert [err] Failed" |
| assert_eq[<expected>;<got>;<err>] | Assert lhs~rhs, otherwise throw signal with err message and differences |
| test[<name>;<expected>;<got>] | Function for generating tests |
| fc[<vec>;<size>] | Fill or cut |
| fmt[<string with %>; <list>] | Dynamic formatting |
| ltrim[<string>] | Trim leading spaces |
| rtrim[<string>] | Trim trailing spaces |
| trim[<string>] | Trim leading and trailing spaces |
| rcsv[<cols>;<sep>;<types>;<file>] | CSV file loading |
| wcsv[<tbl>;<cols>;<sep>;<file>] | CSV file write |
| peach[<fn>;<vec>] | Call fn for each from vec in parallel |
| xpeach[<fn>;<vec>] | Parallel each splits vec onto __cores__ - 1 pieces |
| con[&ldict1>;<dict2>] | Concatenates two dicts, duplicate keys concatenates their values |
| differ[<list] | Returns a boolean list indicating whеre consecutive pairs of items in x differ |
| lshift[<number>;<cnt>] | Left shift number by cnt bits |
| rshift[<number>;<cnt>] | Right shuft number by cnt bits |
| lrot[<number>;<cnt>] | Left rotate number by cnt bits |
| .o.cell[<globvar>] | Create cell to be used for synchronized access to a global variable from any task |
Examples
Testing and assertions:
o)load[getenv[`OHOME];"core"];
o)
o)// Run test - passes
o)test["addition";1+1;2]
--- Test [addition] Passed
o)
o)// Run test - fails
o)test["wrong";1+1;3]
--- Test [wrong] Failed
> Expected: 3
> Got: 2
---
o)
o)// Assert condition
o)assert[5>3;"five should be greater than three"]
o)
o)// Assert equality
o)assert_eq[10;5+5;"sum should equal 10"]
String manipulation:
o)// Trim whitespace
o)trim[" hello "]
"hello"
o)ltrim[" hello "]
"hello "
o)rtrim[" hello "]
" hello"
o)
o)// Dynamic formatting
o)fmt["Hello %, you are % years old";("Alice";"30")]
"Hello Alice, you are 30 years old"
Vector operations:
o)// Fill or cut vector to size
o)fc[1 2 3;5;0] // fill with 0
1 2 3 0 0
o)fc[1 2 3 4 5;3;0] // cut to size 3
1 2 3
o)
o)// Check where consecutive items differ
o)differ[1 1 2 2 3 3]
101010b
o)differ["AAABBBC"]
"0001001b"
Bit operations:
o)// Left shift
o)lshift[5;2] // 5 << 2 = 20
20
o)
o)// Right shift
o)rshift[20;2] // 20 >> 2 = 5
5
o)
o)// Left rotate
o)lrot[5;2]
20
CSV operations:
o)// Create table
o)t: +`name`age`city!(("Alice";"Bob");25 30;("NYC";"LA"));
o)
o)// Write CSV to file
o)wcsv[t;();",";`$":/tmp/data.csv"];
o)
o)// Read CSV from file (auto-detect columns)
o)t2: rcsv[();",";`symbol`long`symbol;`$":/tmp/data.csv"];
o)
o)// Read CSV with specific column names
o)t3: rcsv[`name`age`city;",";`symbol`long`symbol;`$":/tmp/data.csv"];
Parallel execution:
o)// Parallel each - run function in parallel
o)peach[{x*x};1 2 3 4 5]
1 4 9 16 25
o)
o)// Extended parallel each - splits work across cores
o)xpeach[{x*x};!1000] // Process 1000 items in parallel
Dictionary operations:
o)// Concatenate dictionaries
o)d1: `a`b!(1 2;3 4);
o)d2: `b`c!(5 6;7 8);
o)con[d1;d2]
a| 1 2
b| 3 4 5 6
c| 7 8
Thread-safe cell:
o)// Create cell for synchronized access
o)counter: 0;
o)cell: .o.cell[`counter];
o)
o)// Multiple threads can safely update counter through cell
o)spawn[{cell[{x+1}]};()] // Increment in thread
The
peach and xpeach functions require starting tachyon with -c N flag where N is the number of scheduler threads.