list-util

list-util-0.0.2 by tobyink

List utility functions inspired by Perl's List::Util family

Download .tar.gz

Package

Name
list-util
Version
0.0.2
Uploaded
2026-05-28 14:18:29
Metadata
zuzu-distribution.json
Archive
Download .tar.gz

list-util

list/util is a pure-Zuzu distribution of list helper functions inspired by Perl's List::Util, List::MoreUtils, and List::UtilsBy.

from list/util import grep, head, map, sortnum_by, sum, sortstr_by, ListUtil;

say( sum( [ 1, 2, 3 ] ) );
say( ListUtil.product( [ 2, 3, 4 ] ) );

let users := [
	{ name: "Zoe", age: 32, score: 87, active: true },
	{ name: "Ada", age: 41, score: 98, active: true },
	{ name: "Max", age: 27, score: 91, active: false },
	{ name: "Bea", age: 36, score: 93, active: true },
];

let by_name := sortstr_by( users, fn user → user{name} );
let oldest := ListUtil.max_by( users, fn user → user{age} );

let leaderboard := users
	▷ grep( ^^, fn user → user{active} )
	▷ sortnum_by( ^^, fn user → -user{score} )
	▷ head( ^^, 3 )
	▷ map( ^^, fn user → user{name} );

say( leaderboard );  // [ "Ada", "Bea", "Zoe" ]

Functions take the collection first. Predicate callbacks receive one value. Reducer callbacks receive (accumulator, value). Key callbacks for *_by functions receive one value and are called once per input value. Pair callbacks receive a Zuzu Pair object.

Collection Support

Array is the primary input type. Bag and Set are accepted by order-insensitive helpers. Their order is whatever to_Array returns.

Function Input Callback
reduce Array (accumulator, value)
reductions Array (accumulator, value)
any, all, none, notall Array, Bag, Set value
first, firstval, firstidx, lastval, lastidx, onlyidx Array value
onlyval Array, Bag, Set value
max, maxstr, min, minstr, sum, sum0, product Array, Bag, Set none
pairs flat Array none
unpairs, pairkeys, pairvalues Array of Pair none
pairfirst, pairgrep, pairmap Array of Pair Pair
sort Array (left, right)
sortnum, sortstr, reverse, shuffle, sample, uniq, uniqint, uniqnum, uniqstr Array, Bag, Set none
map, grep Array, Bag, Set value
head, tail, zip, mesh Array none
sort_by, sortnum_by, sortstr_by, max_by, maxnum_by, maxstr_by, min_by, minnum_by, minstr_by, uniq_by Array, Bag, Set value

Function Examples

reduce( [ 1, 2, 3 ], fn ( a, b ) → a + b );        // 6
reductions( [ 1, 2, 3 ], fn ( a, b ) → a + b );    // [ 1, 3, 6 ]

any( [ 1, 2, 3 ], fn x → x > 2 );                  // true
all( [ 2, 4, 6 ], fn x → x mod 2 = 0 );            // true
none( [ 1, 3, 5 ], fn x → x mod 2 = 0 );           // true
notall( [ 2, 4, 5 ], fn x → x mod 2 = 0 );         // true
first( [ 1, 2, 3 ], fn x → x > 1 );                // 2

max( [ 7, 2, 10 ] );                                // 10
maxstr( [ "b", "aa" ] );                            // "b"
min( [ 7, 2, 10 ] );                                // 2
minstr( [ "b", "aa" ] );                            // "aa"
product( [ 2, "3", 4 ] );                           // 24
sum( [ 1, "2", 3 ] );                               // 6
sum0( [] );                                         // 0

let ps := pairs( [ "a", 1, "b", 2 ] );
unpairs(ps);                                        // [ "a", 1, "b", 2 ]
pairkeys(ps);                                       // [ "a", "b" ]
pairvalues(ps);                                     // [ 1, 2 ]
pairfirst( ps, fn p → p.key eq "b" );
pairgrep( ps, fn p → p.value > 1 );
pairmap( ps, fn p → p.key _ "=" _ p.value );

sort( [ 1, 3, 2 ], fn ( a, b ) → a ≶ b );          // [ 1, 2, 3 ]
sortnum( [ "2", "10", "1" ] );                     // [ 1, 2, 10 ]
sortstr( [ "b", "aa" ] );                           // [ "aa", "b" ]
reverse( [ 1, 2, 3 ] );                              // [ 3, 2, 1 ]
map( [ 1, 2, 3 ], fn x → x × 2 );                   // [ 2, 4, 6 ]
grep( [ 1, 2, 3 ], fn x → x > 1 );                  // [ 2, 3 ]
shuffle( [ 1, 2, 3 ] );
sample( [ 1, 2, 3 ], 2 );
uniq( [ 1, 1, 2 ] );                                // [ 1, 2 ]
uniqint( [ 1.1, 1.9, 2.1 ] );                       // [ 1.1, 2.1 ]
uniqnum( [ "1", 1, 2 ] );                           // [ "1", 2 ]
uniqstr( [ 1, "1", 2 ] );                           // [ 1, 2 ]
head( [ 1, 2, 3 ], 2 );                             // [ 1, 2 ]
tail( [ 1, 2, 3 ], 2 );                             // [ 2, 3 ]
zip( [ "a", "b" ], [ 1, 2 ] );                      // rows
mesh( [ "a", "b" ], [ 1, 2 ] );                     // flat values

firstval( [ "a", "bb" ], fn x → length(x) > 1 );    // "bb"
firstidx( [ 1, 4, 9 ], fn x → x > 3 );             // 1
lastval( [ 1, 4, 9 ], fn x → x > 3 );              // 9
lastidx( [ 1, 4, 9 ], fn x → x > 3 );              // 2
onlyval( [ 1, 2, 3 ], fn x → x = 2 );              // 2
onlyidx( [ 1, 2, 3 ], fn x → x = 2 );              // 1

sort_by( users, fn user → user{age} );
sortnum_by( users, fn user → user{age} );
sortstr_by( users, fn user → user{name} );
max_by( users, fn user → user{age} );
maxnum_by( users, fn user → user{age} );
maxstr_by( users, fn user → user{name} );
min_by( users, fn user → user{age} );
minnum_by( users, fn user → user{age} );
minstr_by( users, fn user → user{name} );
uniq_by( users, fn user → user{name} );

ListUtil

Every exported function is also available as a static method.

from list/util import ListUtil;

say( ListUtil.sum( [ 1, 2, 3 ] ) );
say( ListUtil.head( [ "a", "b", "c" ], 2 ) );

Documentation