diff --git a/lagoon/desk/lib/lagoon.hoon b/lagoon/desk/lib/lagoon.hoon index e87432c..ee760ef 100644 --- a/lagoon/desk/lib/lagoon.hoon +++ b/lagoon/desk/lib/lagoon.hoon @@ -994,15 +994,23 @@ =/ rtol (fill meta.a data:(scale meta.a +.tol)) (lte (abs (sub a b)) (add atol (mul rtol (abs b)))) :: + :: Booleans use the numeric convention true=1, false=0 (see fun-scalar). + :: An element is truthy iff its encoded value is nonzero (also +0.0=0x0). + :: any: some element truthy <=> the max element is nonzero. + :: all: every element truthy <=> the min element is nonzero. + :: min/max reduce to an all-1s-shape scalar ray, so index with a + :: rank-matched zero index rather than assuming a fixed dimensionality. ++ any |= [a=ray] ^- ?(%.y %.n) - (^lth (get-item (cumsum a) ~[0]) (roll shape.meta.a ^mul)) + =/ dex (reap (lent shape.meta.a) 0) + !(=((get-item (max a) dex) 0)) :: ++ all |= [a=ray] ^- ?(%.y %.n) - =((get-item (cumsum a) ~[0]) 0) + =/ dex (reap (lent shape.meta.a) 0) + !(=((get-item (min a) dex) 0)) :: +$ ops $? %add %sub diff --git a/lagoon/desk/tests/lib/lagoon-array-utils.hoon b/lagoon/desk/tests/lib/lagoon-array-utils.hoon index 28141f0..8497e57 100644 --- a/lagoon/desk/tests/lib/lagoon-array-utils.hoon +++ b/lagoon/desk/tests/lib/lagoon-array-utils.hoon @@ -208,6 +208,21 @@ !>((get-item:la (max:la a) ~[0 0])) !>((snag (argmax:la a) (ravel:la a))) == +:: +:: any/all over boolean rays (numeric convention: true=1.0, false=0.0). +:: any = some element truthy; all = every element truthy. +++ test-any-all ^- tang + =/ all-true (en-ray:la [meta=[shape=~[1 3] bloq=5 kind=%i754 tail=~] baum=~[~[.1.0 .1.0 .1.0]]]) + =/ has-false (en-ray:la [meta=[shape=~[1 3] bloq=5 kind=%i754 tail=~] baum=~[~[.1.0 .0.0 .1.0]]]) + =/ all-false (en-ray:la [meta=[shape=~[1 3] bloq=5 kind=%i754 tail=~] baum=~[~[.0.0 .0.0 .0.0]]]) + ;: weld + %+ expect-eq !>(%.y) !>((all:la all-true)) + %+ expect-eq !>(%.y) !>((any:la all-true)) + %+ expect-eq !>(%.n) !>((all:la has-false)) + %+ expect-eq !>(%.y) !>((any:la has-false)) + %+ expect-eq !>(%.n) !>((all:la all-false)) + %+ expect-eq !>(%.n) !>((any:la all-false)) + == -- :: to-tank