SequenceOf and LazySequence - what is the difference and the intended use?

I'm trying to understand why in Swift there are two different structures that are almost the same.

  • Is SequenceOf not lazy?
  • What are the intended uses for each?

Edit: I think I still don't understand what β€œlazy” means in the sequence ... Fe take this code that uses SequenceOf, which is supposedly not lazy:

func myMap<S: SequenceType, V>(source: S, selector: S.Generator.Element -> V) -> SequenceOf<V> { let seq = SequenceOf { _ -> GeneratorOf<V> in var gen = source.generate() return GeneratorOf { let v = gen.next() println(v) return v == nil ? nil : selector(v!) } } return seq } 

Let me call him

  let a = myMap([1, 2, 3], { $0 * 2 }) var gen = a.generate() let v1 = gen.next() let v2 = gen.next() 

He is typing

Optional (1)

Optional (2)

It seems lazy to me ...

Edit # 2:

When using the card in a lazy sequence, it seems to be eagerly evaluating the elements:

 struct TransientView<S: SequenceType> : SequenceType { private let seq: S init(_ seq: S) { self.seq = seq } func generate() -> GeneratorOf<S.Generator.Element> { var g = seq.generate() return GeneratorOf { println("next") return g.next() } } } let seq = lazy(map(TransientView([1, 2, 3]), { $0 * 2 })) 

prints "next" 4 times ...

Change No. 3. Here is my current example: it would be wrong to say that "SequenceOf is not lazy." Most likely, "SequenceOf is expanded with lazy .map, .filter, etc.". It is possible to write lazy versions of maps, files, etc., which will apply to any sequence, including SequenceOf. But Apple decided to apply the β€œlazy” paradigm not only to sequence instances, but also to sequence elements, so if you specify a sequence variable as lazy, LazySequence is used and, since such extensions are lazy. I think this is the wrong approach, and a lazy declaration should not be passed on to elements - instead, elements should be kept lazy as long as possible. It is easy to turn lazy into impatient, but impossible.

+7
swift
source share
1 answer

Is SequenceOf not lazy?

Correctly. This is not lazy. That is why there are lazy forms.

What are the intended uses for each?

Airpseed Velocity probably has the most comprehensive discussion of the various types. SequenceOf provides simple ways to generate sequences from closures. It also provides a kind of "sequence of types" for converting one type of sequence to another.

LazySequence is mainly created using the lazy function. Airspeed Work on the lazy gives a nice introduction. Its purpose is to avoid generating elements before they are really needed, which is especially useful for infinite sequences, but useful in any case that you may not need all the elements, and they are not trivial to generate.

I mentioned that you should read airspeed if you want to dive deep into this material? But still, it is worth remembering that we only have what we can deduce from the headlines, select from discussions on disco form, and intuitively understand what we still know about Swift. The types of the lower level are still not well documented and are not part of any "official" documents, and they have changed several times in beta versions. Therefore, it is not always easy to know exactly what the Swift team intends for them in future versions. This is the best we know so far.


EDIT: As I noted in the comments, there is no map function that accepts and returns LazySequence in 6.1b3. In LazySequence , only the map method exists. But we can build one if we want, and maybe they will add it in the end:

 func map<S: SequenceType, U>(source: LazySequence<S>, transform: S.Generator.Element -> U) -> LazySequence<MapSequenceView<S, U>> { return source.map(transform) } 
+12
source share

All Articles