Skip to content

Instantly share code, notes, and snippets.

@krrrr38
Created February 18, 2012 22:43
Show Gist options
  • Save krrrr38/1861225 to your computer and use it in GitHub Desktop.
Save krrrr38/1861225 to your computer and use it in GitHub Desktop.
Seqのappendの疑問
def insertAtErrorL[A](e: A, i: Int, ls: Seq[A]): Seq[A] = ls.take(i) :+ e ++ ls.drop(i)
// scala> <console>:7: error: value ++ is not a member of type parameter A
// def insertAtErrorL[A](e: A, i: Int, ls: Seq[A]): Seq[A] = ls.take(i) :+ e ++ ls.drop(i)
// ^
def insertAtErrorR[A](e: A, i: Int, ls: Seq[A]): Seq[A] = ls.take(i) ++ e +: ls.drop(i)
// scala> <console>:1: error: left- and right-associative operators with same precedence may not be mixed
// def insertAtErrorR[A](e: A, i: Int, ls: Seq[A]): Seq[A] = ls.take(i) ++ e +: ls.drop(i)
// ^
def insertAtL[A](e: A, i: Int, ls: Seq[A]): Seq[A] = (ls.take(i) :+ e) ++ ls.drop(i)
// scala> insertAtL: [A](e: A, i: Int, ls: Seq[A])Seq[A]
def insertAtR[A](e: A, i: Int, ls: Seq[A]): Seq[A] = ls.take(i) ++ (e +: ls.drop(i))
// scala> insertAtR: [A](e: A, i: Int, ls: Seq[A])Seq[A]
def insertAt[A](e: A, i: Int, ls: Seq[A]): Seq[A] = ls.take(i) ++ Seq(e) ++ ls.drop(i)
// scala> insertAt: [A](e: A, i: Int, ls: Seq[A])Seq[A]
// 左結合でinsertAtErrorLなら成立するんじゃないん?関係無い?
// まぁエラーの通り,優先度同じだから分からないって事か,確かに…
// ずれてたの修正.
// というか,値を末尾に付けようというのがナンセンスなのかな.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment