Last active
February 17, 2019 07:59
-
-
Save alskipp/347045ddae03cbbfa847 to your computer and use it in GitHub Desktop.
A few Array extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
/* | |
'span' takes a predicate and returns a tuple where first element is longest prefix (possibly empty) | |
of elements of self that satisfy 'predicate' and second element is the remainder of self: | |
-- > [1,2,3,4,1,2,3,4].span { $0 < 3 } == ([1,2],[3,4,1,2,3,4]) | |
-- > [1,2,3].span { $0 < 9 } == ([1,2,3],[]) | |
-- > [1,2,3].span { $0 < 0 } == ([],[1,2,3]) | |
*/ | |
func span(@noescape predicate: Element -> Bool) -> ([Element], [Element]) { | |
return indexOf { !predicate($0) } | |
.map { i in (Array(prefixUpTo(i)), Array(suffixFrom(i))) } ?? (self, []) | |
} | |
/* | |
'partition' takes a predicate and returns a tuple of Arrays. Elements of first Array | |
match the predicate, elements of second Array don't match the predicate | |
-- > [1,2,3,4,1,2,3,4].partition { $0 < 3 } == ([1,2,1,2],[3,4,3,4]) | |
-- > [1,2,3].partition { $0 < 9 } == ([1,2,3],[]) | |
-- > [1,2,3].partition { $0 < 0 } == ([],[1,2,3]) | |
*/ | |
func partition(@noescape predicate: Element -> Bool) -> ([Element], [Element]) { | |
var (l,r) = ([Element](), [Element]()) | |
for e in self { | |
predicate(e) ? l.append(e) : r.append(e) | |
} | |
return (l,r) | |
} | |
// a quick implementation of 'scan' | |
// [1,2,3,4,5].scan(0, combine: +) == [1, 3, 6, 10, 15] | |
func scan<T>(var initial: T, combine: (T, Element) -> T) -> [T] { | |
var a = [T]() | |
for e in self { | |
initial = combine(initial, e) | |
a.append(initial) | |
} | |
return a | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment