- 
                Notifications
    
You must be signed in to change notification settings  - Fork 104
 
Add zipShortest function and make generic Zip safe #370
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
Conversation
| 
           I like your idea! I thought this was how List.zip in core worked already, but I was wrong. It throws exception if different sized lists are zipped. Unfortunately, F#+ seems a little inconsistent: 
 So for completeness, we should probably implement  I'm not sure if it's worth also implementing   | 
    
| 
           Oh now I notice the other pr! Well seems to me still makes sense to implement for non empty list... tryZip could be a separate thing.  | 
    
| 
           @adz , you're right. I've added implementation for   | 
    
          
 Again it worth clarifying is not our goal to fix FSharp.Core So these inconsistencies comes from F# core and we can't fix them. We should instead create different function which behave in a consistent way, take for instance the example of   | 
    
| 
           We could however, define our generic  But I definitely like the   | 
    
| 
           @gusty , I've introduced changes according to your remarks. now generic   | 
    
| let zipShortest (a1: ResizeArray<'T1>) (a2: ResizeArray<'T2>) = | ||
| let len = min a1.Count a2.Count | ||
| [| for i in 0..(len-1) -> a1.[i], a2.[i] |] | ||
| |> ResizeArray | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will accept it as it is, but not sure if this is the most efficient implementation in terms of allocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it to efficient version both here and in ResizeArray.
I've checked it with #time and implementations suggested by you are considerably faster. (even 2-3x on my machine)
Added safe zip function
zipShortest.It's behavior is equivalent to Haskell
zipwhich I think is quite reasonable.Naming is already used in .NET ecosystem in MoreLinq C# library [Source]