You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 nevertheless Apply. 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 implementing pure 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 with Applys. Conversely, the requirements on the traversable container are tightened; more things are Traversable than are Traversable1.
Here is an example of what an instance might look like for non-empty arrays:
// :: type Apply f = (Functor f) & { lift2: (a -> b -> c) -> f a -> f b -> f c }// :: type Traversable1 t = { traverse1: Apply f -> (a -> f b) -> t a -> f (t b) }// :: Traversable1 Array1constArr1=(()=>{constsnoc=xs=>x=>[...xs,x];consttraverse1=A=>f=>([x, ...xs])=>xs.reduce((p,c)=>A.lift2(snoc)(p)(f(c)),A.map(x=>[x])(f(x)));return{ snoc, traverse1 };})();// :: String[] -> String[] -> String[]constintersection=s1=>s2=>{consts2_=newSet(s2);returns1.filter(x=>s2_.has(x));};// :: Apply ObjconstObj=(()=>{// :: Obj v -> Stringconstkeys=o=>Object.keys(o);constzipWith=f=>o1=>o2=>{constk1=keys(o1);constk2=keys(o2);constks=intersection(k1)(k2);returnks.reduce((p,k)=>({ ...p,[k]: f(o1[k])(o2[k])}),{});};constmap=f=>o=>keys(o).reduce((p,k)=>({ ...p,[k]: f(o[k])}),{});constlift2=zipWith;return{ zipWith, map, lift2 };})();// :: Traversable1 t -> Apply f -> t (f a) -> f (t a)constsequence1=T1=>A=>xs=>T1.traverse1(A)(x=>x)(xs);// :: Array1 (Obj x) -> Obj (Array1 x)consts=sequence1(Arr1)(Obj);// :: Array1 (Obj x)constinput=[{x: 1,y: 2},{x: 3,y: 4}];console.log(s(input));// => { x: [1, 3], y: [2, 4] }
Similar instances exist for non-empty objects themselves, non-empty trees, non-empty sets, etc.
Perhaps obvious, but it's worth noting that all Traversable1s are Traversable for free; since all Applicatives are (at least) Apply.
The text was updated successfully, but these errors were encountered:
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
.The text was updated successfully, but these errors were encountered: