Skip to content

Instantly share code, notes, and snippets.

@chuwy
Created March 14, 2023 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuwy/37099128e977868b8899eda739c60243 to your computer and use it in GitHub Desktop.
Save chuwy/37099128e977868b8899eda739c60243 to your computer and use it in GitHub Desktop.
type SpanF[A, C] <: Tuple = (A, C) match
case ((as, bs), (a, b)) => (as *: a, bs *: b)
type Span[A <: Tuple] = Fold[A, (EmptyTuple, EmptyTuple), SpanF]
def span[T <: Tuple](t: Tuple): Option[Span[T]] =
def go[TT <: Tuple, A <: Tuple, B <: Tuple](remaining: TT, as: A, bs: B): Option[(A, B)] =
remaining match
case EmptyTuple => Some((as, bs))
case (a, b) *: tt => go(tt, a *: as, b *: bs).asInstanceOf[Option[(A, B)]]
case s => None
go(t, EmptyTuple, EmptyTuple).asInstanceOf[Option[Span[T]]]
val input: ((Int, String), (Boolean, Double), (List[Int], List[String])) = ???
val output: Option[((Int, Boolean, List[Int]), (String, Double, List[String]))] = span(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment