Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Monads but for Enumerating over data #4

Open
postmodern opened this issue Jan 16, 2022 · 0 comments
Open

Add Monads but for Enumerating over data #4

postmodern opened this issue Jan 16, 2022 · 0 comments
Assignees
Labels

Comments

@postmodern
Copy link
Owner

postmodern commented Jan 16, 2022

It would be useful to add Monad primitives for Enumerating over Sets/Lists. Unlike Ruby's built-in Array#combination and Array#permutation methods, these Monads would accept any Enumerable value and only execute when .each is called. This would allow building up complex nested combinatorial data structures that could be enumerated.

Enumerating Monads

  • Once(X)/Yield(X) - yields X, once. Basically the singleton of enumerating.
  • Either(X,Y) - enumerates over the elements in X, then enumerates over the elements in Y.
    Maybe also add Concat(X,Y,...) that yields each value in each enumerable, in order they are given?
  • Toggle(X) - yields nothing and then enumerates over the values in X.
    Note: could be implemented as Either(Yield(nil),X)?
  • Each(X) - yields every element in X.
  • Zip(X,Y,...) - yields each element from X, Y, etc, as if they were zipped together.

Combinatorial Monads

  • Choose(X, K) - enumerates over every choose of K elements in X, while respecting the order of elements.
  • Permute(X, R) - enumerates over every permutation of R elements in X, regardless of order.
  • CartesianProduct(X,Y,...) - enumerates over every combination of elements from X, Y, ..., in the order they are given.
  • PowerSet(X) - enumerates over every sub-set of elements within X.
  • Combination(X,Y,Z,...) - enumerates over every combination of elements from X, then X and Y, then Y and Z, then X Y and Z.
    Note: could be useful for fuzz testing where you need to test every combination of enabled features/bits.
    Note: this could probably be implemented as
PowerSet([X, Y, Z, ...]).each do |subset_of_enums|
  CartesianProduct(*subset_of_enums).each(&block)
end

These Monad enumerating primitives could be used to re-implement Array#list_comprehension or CartesianProduct::Mixin#cartesian_product.

@postmodern postmodern self-assigned this Jan 16, 2022
@postmodern postmodern changed the title Add Monads but for Enumerating Add Monads but for Enumerating over data Jan 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant