[swift-evolution] Lazy flatMap for Optionals

Dmitri Gribenko gribozavr at gmail.com
Sat Dec 5 01:54:25 CST 2015


On Fri, Dec 4, 2015 at 2:38 PM, Donnacha Oisín Kidney <
oisin.kidney at gmail.com> wrote:

> Currently, several of the methods on SequenceType in the standard library
> have lazy variants. flatMap, though, (seems) to have a version missing:
> while there’s a lazy version for nested sequences, there’s no lazy version
> for sequences of Optionals. Is there maybe a reason for this that I
> haven’t thought of? At any rate, here’s what I had in mind:
>
> public struct FlatMapOptionalGenerator<G: GeneratorType, Element>:
> GeneratorType {
>   private let f: G.Element -> Element?
>   private var g: G
>   public mutating func next() -> Element? {
>     while let x = g.next() {
>       if let y = f(x) {
>         return y
>       }
>     }
>     return nil
>   }
> }
>
> public struct FlatMapOptionalSequence<S: LazySequenceType, Element>:
> LazySequenceType {
>   private let f: S.Generator.Element -> Element?
>   private let s: S
>   public func generate() -> FlatMapOptionalGenerator<S.Generator, Element>
> {
>     return FlatMapOptionalGenerator(f: f, g: s.generate())
>   }
> }
>
> extension LazySequenceType {
>   public func flatMap<T>(transform: Generator.Element -> T?) ->
> FlatMapOptionalSequence<Self, T> {
>     return FlatMapOptionalSequence(f: transform, s: self)
>   }
> }
>
> Does this seem like a good idea?
>

Hi Oisin,

This is just an omission.  Please submit a formal proposal as a pull
request to the swift-evolution repository.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/81d1765a/attachment-0001.html>


More information about the swift-evolution mailing list