/* * Copyright 2022 Jakob Schneider Villumsen * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */////// Trait for types that can be folded without regard to order, and its functions.///pubmod UnorderedFoldable {////// Trait for types that can be folded without regard to order.///pubtraitUnorderedFoldable[t: Type -> Type] {////// The associated effect of the UnorderedFoldable which represents the/// effect of accessing its elements.///typeAef[m]: Eff = {}////// Unordered fold of `t`.////// Applies `f` to all elements in `t` and combines the results/// i.e. `CommutativeMonoid.combine(f(a))` for all `a` in `t`.///pubdeffoldMap(f: a -> b \ ef, t: t[a]): b \ (ef + UnorderedFoldable.Aef[t]) withCommutativeMonoid[b]////// Returns the result of applying `CommutativeMonoid.combine` to all the elements in `t`.///pubdeffold(t: t[a]): a \ UnorderedFoldable.Aef[t] withCommutativeMonoid[a] =UnorderedFoldable.foldMap(identity, t)////// Returns true if and only if `t` is empty.///pubdefisEmpty(t: t[a]): Bool \ UnorderedFoldable.Aef[t] =UnorderedFoldable.forAll(_ -> false, t)////// Returns true if and only if `t` is non-empty.///pubdefnonEmpty(t: t[a]): Bool \ UnorderedFoldable.Aef[t] = notUnorderedFoldable.isEmpty(t)////// Returns `true` if and only if at least one element in `t` satisfies the predicate `f`.////// Returns `false` if `t` is empty.///pubdefexists(f: a -> Bool \ ef, t: t[a]): Bool \ (ef + UnorderedFoldable.Aef[t]) =use SemiGroup.Any.Any;let Any(x) = UnorderedFoldable.foldMap(a -> Any(f(a)), t);x////// Returns `true` if and only if all elements in `t` satisfy the predicate `f`.////// Returns `true` if `t` is empty.///pubdefforAll(f: a -> Bool \ ef, t: t[a]): Bool \ (ef + UnorderedFoldable.Aef[t]) =use SemiGroup.All.All;let All(x) = UnorderedFoldable.foldMap(a -> All(f(a)), t);x////// Returns `true` if and only if the element `x` is in `t`.///pubdefmemberOf(x: a, t: t[a]): Bool \ UnorderedFoldable.Aef[t] withEq[a] =UnorderedFoldable.exists(y -> x==y, t)////// Returns the number of elements in `t`.///pubdefsize(t: t[a]): Int32 \ UnorderedFoldable.Aef[t] =UnorderedFoldable.foldMap(_ -> 1, t)////// Returns the number of elements in `t` that satisfy the predicate `f`.///pubdefcount(f: a -> Bool \ ef, t: t[a]): Int32 \ (ef + UnorderedFoldable.Aef[t]) =UnorderedFoldable.foldMap(a -> if (f(a)) 1else0, t) }}