Created
March 14, 2023 12:17
-
-
Save chuwy/37099128e977868b8899eda739c60243 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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