Sanctuary: Add S.zip and S.zipWith

Created on 15 Feb 2017  路  16Comments  路  Source: sanctuary-js/sanctuary

Create a type-safe version of zip and zipWith.

good first contribution

Most helpful comment

Let's reopen the discussion. The fact that it's now feasible to use Sanctuary without Ramda may tip the scales in favour of these functions' inclusion.

All 16 comments

222 contains many interesting thoughts on this matter.

I'd completely forgotten about that! Guess we should close this then. I still think it's a useful function, I was only using it last week and had to go back to Ramda.

Let's reopen the discussion. The fact that it's now feasible to use Sanctuary without Ramda may tip the scales in favour of these functions' inclusion.

How general do you want to go with this? A quick search of Hoogle shows a lot of zip related functions.

I think it would be okay to provide Array-specific functions:

zip :: Array a -> Array b -> Array (Pair a b)
zipWith :: (a -> b -> c) -> Array a -> Array b -> Array c

I'm not sure whether it's possible to provide these instead:

zip :: Foldable f => f a -> f b -> f (Pair a b)
zipWith :: Foldable f => (a -> b -> c) -> f a -> f b -> f c

I imagine this will require more than just Foldable. I haven't given the general form much thought.

There's also zipWithM to consider.

I think zip and zipWith could work withString as well.

Could you provide an example of applying zipWith to strings, @gabejohnson?

// zipWith :: (String -> String -> String) -> String -> String -> [String]
joinWith(':', zipWith(a => b => `${a}+${b}`, 'hello', 'world')); // 'h+w:e+o:l+r:l+l:o+d'

Don't ask me about the utility 馃槃

It's a different function, as it's monomorphic (we lost all the type variables). Let's not worry about the String-specific functions. ;)

More generally:

// zipWith :: (String -> String -> a) -> String -> String -> [a]
const parseNat = parseInt(10);
sequence(Maybe, zipWith(a => b => lift2(add, parseNat(a), parseNat(b)), '1234', '56789'))
// Just([6, 8, 10, 12])

@davidchambers I see your point. Was only fixing the type to illustrate.

Or even _more_ generally:

// zipWith :: (a -> b -> c) -> List a -> List b -> List c
const parseNat = parseInt(10);
sequence(Maybe, zipWith(a => b => lift2(add, parseNat(a), Just(b)), '1234', [5, 6, 7, 8, 9]))
// Just([6, 8, 10, 12])

you can just apply toList to string and feed to zipWith

//    toList :: String -> [String]
const toList = s => s.split('')

Similarly, I'd want zip for StrMaps. It feels like there's some specialization of functors for which you can generally implement zip. Maybe Traversable?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blackxored picture blackxored  路  10Comments

fnune picture fnune  路  3Comments

davidchambers picture davidchambers  路  5Comments

kurtmilam picture kurtmilam  路  11Comments

andrei-cacio picture andrei-cacio  路  8Comments