Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created December 24, 2011 09:32
Show Gist options
  • Save einblicker/1516993 to your computer and use it in GitHub Desktop.
Save einblicker/1516993 to your computer and use it in GitHub Desktop.
sameLength(4)
object Test extends App {
object Vec {
def fromList[A](l: List[A]): Vec[_, A] = Vec(l)
}
case class Vec[Tag1, A] private (private val l: List[A]) {
def toList = l
def zipWith[Tag2, B, C](v: Vec[Tag2, B])(f: (A, B) => C)(implicit ev: Tag1 =:= Tag2) =
Vec[Tag1, C](l.zip(v.l).map{case (a, b) => f(a, b)})
def sameLength[B, Tag2](b: Vec[Tag2, B]) =
if (l.length == b.l.length)
Some((this, Vec[Tag1, B](b.l)))
else
None
//こっちだと何故か動かない
def badSameLength[B, Tag2](b: Vec[Tag2, B]) =
if (l.length == b.l.length)
Some(Vec[Tag1, B](b.l))
else
None
}
def test = {
val v1 = Vec.fromList(List(1, 2, 3))
val v2 = Vec.fromList(List(1, 2, 3))
val v3 = Vec.fromList(List(1, 2, 3))
for {
(v1, v2) <- v1.sameLength(v2)
(v1, v3) <- v1.sameLength(v3)
//本当はこう書きたい
//v2 <- v1.badSameLength(v2)
//v3 <- v1.badSameLength(v3)
} {
val v4 = Vec.fromList(List(1, 2, 3))
val v5 = v3.zipWith(v2)((_, _))
//println(v4.zipWith(v5)((_, _))) //compile error
println(v1.zipWith(v5)((_, _)))
}
}
test
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment