[swift-evolution] Proposal: Add scan, takeWhile, dropWhile, and iterate to the stdlib

Kevin Ballard kevin at sb.org
Thu Jan 14 01:48:42 CST 2016


Oops, you're right on the skipWhile(), that snuck in there because I was
looking at the Rust documentation (and Rust uses the name skipWhile()).

As for dropUntil, all of the precedent I know of behaves like a
dropWhile, not a dropUntil. And dropUntil sounds weird to me; every
time I've wanted to use this functionality, I do in fact want to drop
the stuff I don't want, rather than dropping until some predicate
becomes true.

Language precedent:

Rust: skip_while() and take_while() Ruby: drop_while() and take_while()
Python: dropwhile() and takewhile() Haskell: dropWhile and takeWhile

-Kevin Ballard

On Wed, Jan 13, 2016, at 05:54 PM, Dany St-Amant wrote:
> The dropWhile sounds weird to me, I usually would see such
> functionality as a dropUntil; I discard stuff until I see what I want.
> Your example below doesn’t use dropWhile, but skipWhile; which sounds
> a bit better that dropWhile as one skip what he doesn’t want.
>
> What do the other languages use? A dropWhile, skipWhile or dropUntil
> concept?
>
> Dany
>
>
>> Le 11 janv. 2016 à 01:20, Kevin Ballard via swift-evolution <swift-
>> evolution at swift.org> a écrit :
>>
>> Here's a few toy examples, if it helps:
>>
>> // list of all powers of 2 below some limit iterate(1, apply: { $0 *
>> 2 }).takeWhile({ $0 < limit })
>>
>> // first "word" of a string, skipping whitespace let cs =
>> NSCharacterSet.whitespaceCharacterSet()
>> String(str.unicodeScalars.skipWhile({
>> cs.longCharacterIsMember($0.value) })
>> .takeWhile({ !cs.longCharacterIsMember($0.value) }))
>>
>> // running total of an array of numbers numbers.scan(, combine:
>> +).dropFirst()
>>
>> // infinite fibonacci sequence iterate((,1), apply: { ($1, $0+$1)
>> }).lazy.map({$1})
>>
>> -Kevin Ballard
>>
>>>>> On Mon, Dec 28, 2015, at 03:59 PM, Kevin Ballard wrote:
>>>>> >
>>>>> > ## Detailed design
>>>>> >
>>>>> > We add the following extension to SequenceType:
>>>>> >
>>>>> > extension SequenceType {     func scan<T>(initial: T, @noescape
>>>>> > combine: (T, Self.Generator.Element) throws -> T) rethrows ->
>>>>> > [T]     func dropWhile(@noescape dropElement:
>>>>> > (Self.Generator.Element) throws -> Bool) rethrows ->
>>>>> > [Self.Generator.Element]     func takeWhile(@noescape
>>>>> > takeElement: (Self.Generator.Element) throws -> Bool) rethrows
>>>>> > -> [Self.Generator.Element] }
>>>>> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160113/51b9700a/attachment.html>


More information about the swift-evolution mailing list