ZipList is a wrapper around LazyList that gives sequences a zipping applicative: map2 combines elements at corresponding positions instead of producing every pair. section 12.4.1
Its applicative behavior follows two defining operations:
- unit creates an infinite sequence by repeating its argument with LazyList.continually. section 12.4.1
- map2 zips two sequences and maps the combining function over corresponding pairs. section 12.4.1
- The wrapper selects this behavior when ordinary LazyList also has a Cartesian-product applicative; no compatible flatMap or join can define the same behavior. section 12.4.1
Corresponding elements
For ZipList values containing 1, 2, 3 and 'a', 'b', map2 produces (1, 'a') and (2, 'b'); it does not produce all six combinations. section 12.4.1
opaque type ZipList[+A] = LazyList[A]
object ZipList:
def fromLazyList[A](la: LazyList[A]): ZipList[A] = la
extension [A](za: ZipList[A]) def toLazyList: LazyList[A] = za
given zipListApplicative: Applicative[ZipList] with
def unit[A](a: => A): ZipList[A] =
LazyList.continually(a)
extension [A](fa: ZipList[A])
override def map2[B, C](fb: ZipList[B])(f: (A, B) => C) =
fa.zip(fb).map(f.tupled)
The implementation shows the opaque wrapper, conversions to and from LazyList, and the given applicative that fixes zipping semantics. section 12.4.1
