Hi Tim,

Thanks for bringing this up.
Here are my thoughts on the change you’re proposing.

func sequence<T>(first: T, next: (T) -> T?) -> UnfoldFirstSequence<T>

To me the type of the function as it is tells a clear story of what’s going to 
happen: take the `first`, make it a head of the resulting sequence, and then 
try to produce the tail by a series of applications of `next`. The only thing 
that controls when the sequence generation terminates is the result of `next`.

If we change the type of `first` to an Optional<T>, it would make the 
termination condition non-trivial. After all, the only thing it would do is try 
to unwrap the `first`, before doing what it needs to, but we already have a 
`map` for that. One should be able to simply do the `first.map { 
sequence(first: $0, next: next) } ?? []` but that won’t work with the types 
very well, unfortunately.

As an alternative, `let first: Int? = ...; sequence(first: first, next: 
next).flatMap({$0})` (or even `.lazy.flatMap({$0})`) will do the right thing 
without making an API more complex.

I see the point of `sequence(first:next:)` to be precisely the "generate the 
non-empty sequence using a seed and a simple producer", for anything more than 
that, there is `sequence(state:next:)`.

What do you think?

Max

> On Aug 14, 2016, at 4:27 PM, Tim Vermeulen via swift-evolution 
> <[email protected]> wrote:
> 
> sequence(first:next:) takes a non-optional first argument. Is there a reason 
> for that? sequence(state:next:) allows empty sequences, and I don’t see why 
> sequence(first:next:) shouldn’t. The fix would be to simply add the `?` in 
> the function signature; no other changes are required to make it work.
> 
> I considered just filing a bug report, but since this is a change of the 
> public API...
> _______________________________________________
> swift-evolution mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to