3

I want something like this:

def unequalZip[A, B](a: Iterable[A], b: Iterable[B]) = Iterable[(Option[A], Option[B])]

where the items from the shorter iterable is matched with items from longer iterable using Nones

Sibi
  • 47,472
  • 16
  • 95
  • 163
pathikrit
  • 32,469
  • 37
  • 142
  • 221

3 Answers3

8

You want

a.zipAll(b, None, None)

if you already have options, or

a.map(x => Option(x)).zipAll(b.map(x => Option(x)), None, None)

otherwise.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
2

def lift[A](a: Iterable[A]) = a map {Option.apply} def unequalZip[A, B](a: Iterable[A], b: Iterable[B]) = lift(a).zipAll(lift(b), None, None)

pathikrit
  • 32,469
  • 37
  • 142
  • 221
0

Take a look at zipAll.

/** Returns a $coll formed from this $coll and another iterable collection * by combining corresponding elements in pairs. * If one of the two collections is shorter than the other, * placeholder elements are used to extend the shorter collection to the length of the longer.

sksamuel
  • 16,154
  • 8
  • 60
  • 108