apple / swift-algorithms

Commonly used sequence and collection algorithms for Swift
Apache License 2.0
5.97k stars 443 forks source link

Provide a version of interspersed that accepts a closure. #160

Open kennycarruthers opened 3 years ago

kennycarruthers commented 3 years ago

When doing macOS or iOS programming, a common use-case I have for interspersed is to insert a separator or divider view in-between a bunch of other views.

Example, given an array of views, I'd like to insert a separator view in-between each one:

let views = [view1, view2, view3]

let allViews = views.interspersed(using: { SeparatorView() })

Expected:

[view1, separator1, view2, separator2, view3]

Because these are NSView, the inserted element needs to be a new instance each time, which the existing implementation does not appear to offer.