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
We can then use the mapN functions to create zipN functions:
-- This already exists, but it would be nice to offer a more generic version:zip::forallabf. Functorf=>fa->fb->Tupleab
zip = map2 Tuple-- Likely needed to fill the gap between the seemingly-identical-- (but actually different) `Tuple` and nested `Tuple2`.zip2::forallabf. Functorf=>fa->fb->f (Tuple2ab)
zip2 = map2 Tuple2zip3::forallabcf. Functorf=>fa->fb->fc->f (Tuple3abc)
zip3 = map3 Tuple3
...
zip10:: ...
zip10 = map10 Tuple10
We probably need a different typeclass than Functor though. What about Zippable?
My understanding of the current best way to get map3 behavior is to take a detour though nested tuples and create a custom zip3 along the way. Lots of hoops to jump through and the dependencies seem inverted (zip3 should depend on map3).
moduleMainwhereimportPreludeimportData.Array (zip)
importData.Tuple.Nested (uncurry3, Tuple3)
importEffect (Effect)
importEffect.Console (logShow)
importTryPureScript (render, withConsole)
zip3::forallabc. Arraya->Arrayb->Arrayc->Array (Tuple3abc)
zip3 a b c = zip a $ zip b $ zip c $ map (const unit) a
map3::forallabcdf. (a->b->c->d) ->Arraya->Arrayb->Arrayc->Arrayd
map3 fn a b c = map (uncurry3 fn) $ zip3 a b c
arrA = [ 1, 2, 3 ] ::ArrayInt
arrB = [ 4, 5, 6 ] ::ArrayInt
arrC = [ 7, 8, 9 ] ::ArrayIntmyFunc::Int->Int->Int->Int
myFunc a b c = a * b + c
main::EffectUnit
main = render =<< withConsole do
logShow $ zip3 arrA arrB arrC
logShow $ map3 myFunc arrA arrB arrC
Zippable things are alternative Applicative instances, it's just that the chosen instance for List/Array does not zip, but models non-determinism, as that behavior also admits a Monad instance. The ZipList data type accomplishes general-purpose n-ary zipping for lists through it's Applicative instance (eg Tuple <$> zipList1 <*> zipList2).
purescript/purescript-arrays#163 See this ticket which mentions adding a ZipArray for arrays. This is possible, it just has not been implemented and PR'ed.
Activity
milesfrain commentedon Aug 27, 2020
My understanding of the current best way to get
map3behavior is to take a detour though nested tuples and create a customzip3along the way. Lots of hoops to jump through and the dependencies seem inverted (zip3should depend onmap3).https://try.ps.ai/?gist=ef39f1f6aa7aa187a3d775cbd30affee
natefaubion commentedon Aug 27, 2020
Zippablethings are alternativeApplicativeinstances, it's just that the chosen instance for List/Array does not zip, but models non-determinism, as that behavior also admits aMonadinstance. TheZipListdata type accomplishes general-purpose n-ary zipping for lists through it'sApplicativeinstance (egTuple <$> zipList1 <*> zipList2).natefaubion commentedon Aug 27, 2020
purescript/purescript-arrays#163 See this ticket which mentions adding a ZipArray for arrays. This is possible, it just has not been implemented and PR'ed.