Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip function #3

Closed
kostiakoval opened this issue Jul 17, 2014 · 4 comments
Closed

Zip function #3

kostiakoval opened this issue Jul 17, 2014 · 4 comments

Comments

@kostiakoval
Copy link

I wanted to implement zip function
But i'm not sure if Tuples are designed to be used this way in Swift as in Haskell.

http://stackoverflow.com/questions/24487519/how-to-elegantly-compare-tuples-in-swift/24495112#24495112

Tuples are useful for temporary groups of related values. They are not suited to the creation of complex data structures. If your data structure is likely to persist beyond a temporary scope, model it as a class or structure, rather than as a tuple. For more information, see Classes and Structures.

What do you think ?

@RuiAAPeres
Copy link
Owner

I was actually basing my zip function on this one.

zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]  
zipWith' _ [] _ = []  
zipWith' _ _ [] = []  
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys

And the example:

ghci> zipWith' (+) [4,2,5,6] [2,6,2,3]  
[6,8,7,9]  

Something I was thinking was to pass a default function, in case we don't pass one. The default one, would be the same as your implementation: tuple creation. Something along the lines:

func zip<X,Y,Z>(listX : [X], listY : [Y], zipFunction : (X,Y)-> W = ... ) -> [W]

The ... would be replace by a default tuples creation function.

@RuiAAPeres
Copy link
Owner

@konstantinkoval I am good on changing the zip function to the official implementation, although I would like to keep the recursive piece.

@kostiakoval
Copy link
Author

There is Zip2 struct in Swift. Have a look on it. Maybe we could use it for zip

@RuiAAPeres
Copy link
Owner

@konstantinkoval

let zip = Zip2([1, 2, 3], [1, 2, 3])
let zippedArray = Array(myZip)

Result:

[(1, 1), (2, 2), (3, 3)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants