S-99を解く。List.makeを使ったらdeprecation warningsがでたので、fillにしてみたんだけどこれでいいのだろうか?
scala> List.make(5,3) warning: there were 1 deprecation warnings; re-run with -deprecation for details res5: List[Int] = List(3, 3, 3, 3, 3)
pythonのenumerateはscalaではzipWithIndexでこれを使えばp16は
ls.zipWithIndex filter { v => (v._2 + 1) % n != 0 } map { _._1 }
splitAtメソッド使えばリストを2つに分割(P17)できるが、僕はtakeとdrop使ってた。
Scalaスケーラブルプログラミング[コンセプト&コーディング] (Programming in Scala)
Martin Odersky
インプレスジャパン / 4830円 ( 2009-08-21 )
Martin Odersky
インプレスジャパン / 4830円 ( 2009-08-21 )
//11 def pack[A](ls:List[A]): List[List[A]] = ls match { case Nil => Nil case head::tail => ls.takeWhile(_ == head) :: pack(tail.dropWhile(_ == head)) } def encodeModified[A](ls: List[A]): List[Any] = pack(ls) map { e => if (e.length == 1) e.head else (e.length, e.head) } println("s11: " + encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))) //12 //scala> List.make(5,3) //warning: there were 1 deprecation warnings; re-run with -deprecation for details //res5: List[Int] = List(3, 3, 3, 3, 3) def decode[A](ls:List[(Int, A)]): List[A] = ls flatMap {e => List.fill(e._1)(e._2)} println("s12: " + decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e)))) //13 def encodeDirect[A](ls:List[A]): List[(Int, A)] = ls match { case Nil => Nil case head::tail => (ls.takeWhile(_ == head).length, head) :: encodeDirect(tail.dropWhile(_ == head)) } println("s13: " + encodeDirect(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))) //14 def duplicate[A](ls:List[A]): List[A] = ls match { case Nil => Nil case h::t => h::h::duplicate(t) } println("s14: " + duplicate(List('a, 'b, 'c, 'c, 'd))) //15 def duplicateN[A](n:Int, ls:List[A]): List[A] = ls flatMap {e => List.fill(n)(e) } println("s15: " + duplicateN(3, List('a, 'b, 'c, 'c, 'd))) //16 // // Functional. // def dropFunctional[A](n: Int, ls: List[A]): List[A] = // ls.zipWithIndex filter { v => (v._2 + 1) % n != 0 } map { _._1 } //} def drop[A](n:Int, ls:List[A]): List[A] = { def drop2[A](n:Int, ls:List[A]): List[A] = ls match { case Nil => Nil case _ => ls.take(n-1) ::: drop2(n, ls.drop(n)) } drop2(n,ls) } println("s16: " + drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))) //17 // Builtin. // def splitBuiltin[A](n: Int, ls: List[A]): (List[A], List[A]) = ls.splitAt(n) def split[A](n:Int, ls:List[A]): (List[A],List[A]) = (ls.take(n), ls.drop(n)) println("s17: " + split(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))) //18 def slice[A](s:Int, e:Int, ls:List[A]): List[A] = ls.take(e).drop(s) println("s18: " + slice(3, 7, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))) //19 def rotate[A](n:Int, ls:List[A]): List[A] = { if (n >= 0) ls.drop(n) ::: ls.take(n) else ls.drop(ls.length + n) ::: ls.take(ls.length + n) } println("s19: " + rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))) println("s19: " + rotate(-2, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))) //20 def removeAt[A](n:Int, ls:List[A]): (List[A],A) = { val v = ls.splitAt(n) (v._1 ::: v._2.tail, v._2.head) } println("s20: " + removeAt(1, List('a, 'b, 'c, 'd)))