[swift-evolution] [Review] Add a Lazy flatMap for Sequences of Optionals
Donnacha Oisín Kidney
oisin.kidney at gmail.com
Wed Dec 16 09:10:28 CST 2015
Now that I think about it, of course CollectionOfZeroOrOne would (or Optional) have a random access index. I’ll update the proposal to include that.
I really like the CollectionOfZeroOrOne solution. The semantics and types certainly make more sense. I’m not sure that it reduces the amount of code, though: the three extensions are still needed, plus an extra struct. (I suppose that’s not counting the extra version of LazyFilterCollection. Am I right in saying that that is intended to be added?)
Here’s what I have for the collection (I tried to mimic the standard library’s CollectionOfOne):
public struct CollectionOfZeroOrOne<Element> : CollectionType {
public typealias Index = Bit
public init(_ element: Element?) {
self.element = element
}
public var startIndex: Index {
return .Zero
}
public var endIndex: Index {
switch element {
case .Some: return .One
case .None: return .Zero
}
}
public func generate() -> GeneratorOfOne<Element> {
return GeneratorOfOne(element)
}
public subscript(position: Index) -> Element {
if case .Zero = position, let result = element {
return result
} else {
fatalError("Index out of range")
}
}
let element: Element?
}
Does that seem reasonable?
> On 16 Dec 2015, at 02:29, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
>
> First, I’d like to thank Oisin <https://github.com/oisdk> for his proposal. It’s great to see people filling in gaps.
>
>> * What is your evaluation of the proposal?
>
> The basic idea is solid and obviously appropriate. I have some quibbles with the proposed solution.
>
>> * Is the problem being addressed significant enough to warrant a change to Swift?
>
> Yes; it’s a non-uniformity, nonuniformities create complexity for users.
>
>> * Does this proposal fit well with the feel and direction of Swift?
>
> Yes.
>
>> * How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
>
> A quick reading, but I’m deeply familiar with the domain, having written the lazy facilities we have in the stdlib.
>
> I don’t see why the proposal states, “Optional probably wouldn't have a BidirectionalIndexType“. It seems to me that as a collection, an optional’s index should be random access, just like CollectionOfOne’s.
>
> I’d like to suggest a different implementation approach that not only handles the bidirectional issue but also probably reduces the amount of code involved: create a generic CollectionOfZeroOrOne<T> that wraps a T?, and implement the x.flatmap(f) where f returns an optional as x.flatmap { CollectionOfZeroOrOne(f($0)) }
>
> What do you think?
>
> -Dave
>
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/8f99adff/attachment.html>
More information about the swift-evolution
mailing list