Open masaeedu opened 5 years ago
I don't mean to dismiss your request, but Haskell's MaybeApply
will turn any Apply into an Applicative. Maybe you can incorporate the logic into your Obj
.
@nadameu The fact that MaybeApply
will turn any Apply
into an Applicative
is of about the same relevance as saying that Maybe
will turn any semigroup into a monoid. It is nevertheless useful to have Foldable1
, if for no other reason than that threading Just
/Nothing
and the corresponding match
through all your values (and similarly Either
for promoting your Apply
to an Applicative
) is quite inefficient.
Btw, there's a better way to use MaybeApply
than to mash it into Obj
. Here's a snippet from a few months ago that illustrates how you can compositionally combine MaybeApply
with an arbitrary Apply
functor: https://runkit.com/masaeedu/maybeapply.
It is often useful to be able to traverse a non-empty structure with respect to a functor that is not quite
Applicative
, but is neverthelessApply
. A common use case I run into is transposing a \<something> of objects into an object of \<something>s.The problem is that objects in JS have a straightforward implementation of
lift2
, but lawfully implementingpure
would require the ability to construct infinite objects with the same value at every possible key (this is possible with proxies, but let's not go there).Instead, we want a weakening of the constraints of
Traversable
so that it can work withApply
s. Conversely, the requirements on the traversable container are tightened; more things areTraversable
than areTraversable1
.Here is an example of what an instance might look like for non-empty arrays:
Similar instances exist for non-empty objects themselves, non-empty trees, non-empty sets, etc.
Perhaps obvious, but it's worth noting that all
Traversable1
s areTraversable
for free; since allApplicative
s are (at least)Apply
.