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.
Proposing we add these
mapN
functions, and make them available to more than justList
.We can then use the
mapN
functions to createzipN
functions:We probably need a different typeclass than
Functor
though. What aboutZippable
?A motivation for adding these functions is to get closer to Elm's usability in this area:
https://package.elm-lang.org/packages/elm/core/latest/List#map2
The text was updated successfully, but these errors were encountered: