@@ -436,6 +436,11 @@ cond(l, d): l foldr(uncurry(if), d)
436
436
` "`juxt(f, g) - return function of `x` returning list of `f(x)` and g(x)`."
437
437
juxt(f, g, x): [x f, x g]
438
438
439
+ #
440
+ # Utilities
441
+ #
442
+ ` "`fnil(f, v, x)` - return a function equivalent to f except it sees `x` instead of `null` when null is passed."
443
+ fnil(f, v, x): if(x = null, f(v), f(x))
439
444
440
445
#
441
446
# Metadata basics
@@ -645,6 +650,28 @@ over-sliding-pairs(f, l): l window(2, 1) map(f uncurry)
645
650
` "differences(l) - calculate difference between each overlapping pair in list of numbers `l`"
646
651
differences: over-sliding-pairs(_1 - _0)
647
652
653
+ ` "`discriminate(pred, xs)` - return pair of `xs` for which `pred(_)` is true and `xs` for which `pred(_)` is false."
654
+ discriminate(pred, xs): {
655
+ acc(a, e): a if(e pred, bimap(cons(e), identity), bimap(identity, cons(e)))
656
+ }.(xs foldl(acc, [[], []]) bimap(reverse, reverse))
657
+
658
+ ` "`group-by(k, xs)` - group xs by key function returning block of key to subgroups, maintains order."
659
+ group-by(k, xs): {
660
+ acc(a, e): a update-value-or(e._k, cons(e._v), [e._v])
661
+ }.(xs map({ _k: k(•0), _v: •0 }) foldl(acc, {}) map-values(reverse))
662
+
663
+ ` "`qsort(lt, xs)` - sort `xs` using 'less-than' function `lt`"
664
+ qsort(lt, xs): if(xs nil?,
665
+ xs,
666
+ {
667
+ h: xs head
668
+ t: xs tail
669
+ less(x): lt(x, h)
670
+ partitions: t discriminate(less)
671
+ smaller: partitions first qsort(lt)
672
+ bigger: partitions second qsort(lt)
673
+ }.(smaller ++ [h] ++ bigger))
674
+
648
675
#
649
676
# Block library functions
650
677
#
0 commit comments