Risto-Stevcev / bastet

A ReasonML/Ocaml library for category theory and abstract algebra
BSD 3-Clause "New" or "Revised" License
211 stars 26 forks source link

MonadError, MonadZero, and Unfoldable #7

Closed texastoland closed 5 years ago

texastoland commented 6 years ago

MonadError

Direct port from PureScript Prelude (that's a good criteria for inclusion) with tests for any relevant laws from the ErrorControl article. That version isn't idiomatic OCaml and will be superseded by algebraic effects. I could see it in a future version of bs-effects however. See more in https://github.com/janestreet/ppx_let/issues/8#issuecomment-419695038.

MonadZero

Direct port from PureScript Prelude. The primary use case is filtering list/array comprehensions.

let factors n ->
  let%bind a = 0 -- n
  let%bind b = 0 -- a
  let%bind () = guard(a * b == n)
  (a, b)

Unfoldable and generic --

Direct port from purescript-unfoldable (related to Foldable in bs-abstract). PureScript implements a generic range for Unfoldable1 and .. operator for specialized versions. It might be nice to specialize for arrays and lists but we should benchmark if so. Belt also implements range for arrays (but not lists). The operator (inspired by Batteries) is simply let (--) = Belt.Array.range.