This file contains 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
// P01 | |
def last[A](l: List[A]): A = l match{ | |
case n :: Nil => n | |
case n :: next => last(next) | |
case _ => throw new NoSuchElementException | |
} | |
// P02 | |
def penultimate[A](l: List[A]): A = l match{ | |
case n :: m :: Nil => n |
This file contains 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
// P11 | |
def encodeModified[A](ls: Seq[A]): Seq[Any] = encode(ls) map {t => if(t._1 == 1) t._2 else t} | |
// P12 | |
def decode[A](ls: Seq[(Int, A)]): Seq[A] = ls flatMap(n => List.fill(n._1)(n._2)) | |
// P13 | |
def encodeDirect[A](ls: Seq[A]): Seq[(Int, A)] = { | |
if (ls.isEmpty) Nil | |
else{ |
This file contains 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
// P21 | |
def insertAt[A](e: A, i: Int, ls: Seq[A]): Seq[A] = ls.take(i) ++ Seq(e) ++ ls.drop(i) | |
// P22 | |
def range(start: Int, end: Int): Seq[Int] = Seq.range(start, end+1) | |
def rangeR(start: Int, end: Int): Seq[Int] = { | |
def rangeRR(value: Int, result: Seq[Int]): Seq[Int] = { | |
if(value < start) result | |
else rangeRR(value-1, value +: result) |
This file contains 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
// P26 | |
// 大門.scala gist(https://gist.github.com/1183495)よりほぼ転載 | |
def sublists[A](ls: List[A]): List[List[A]] = ls match{ | |
case Nil => Nil | |
case _ :: tail => ls :: sublists(tail) | |
} | |
def combinations[A](n: Int, ls: List[A]): List[List[A]] = { | |
if (n == 0) List(Nil) | |
else{ |
This file contains 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
// P31 | |
case class P31(n: Int){ | |
lazy val isPrime: Boolean = Iterator.from(2).takeWhile(i => i*i <= n).forall(n%_ != 0) | |
} | |
implicit def fromIntToP31(n: Int): P31 = P31(n) | |
// P32 | |
def gcd(a: Int, b: Int): Int = if(b == 0) a else gcd(b, a%b) |
This file contains 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
// P46 | |
def and(a: Boolean, b: Boolean) = a && b | |
def or(a: Boolean, b: Boolean) = a || b | |
def nand(a: Boolean, b: Boolean) = !(a && b) | |
def nor(a: Boolean, b: Boolean) = !(a || b) | |
def impl(a: Boolean, b: Boolean) = a && b | |
def equ(a: Boolean, b: Boolean) = !a || b | |
def xor(a: Boolean, b: Boolean) = a ^ b | |
def not(a: Boolean) = !a |
This file contains 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
// P50 | |
abstract sealed class Node[A]{ | |
val freq: Int | |
} | |
case class Leaf[A](element: A, freq: Int) extends Node[A] | |
case class Branch[A](left: Node[A], right: Node[A]) extends Node[A]{ | |
val freq = left.freq + right.freq | |
} |
This file contains 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
// http://d.hatena.ne.jp/kmizushima/20090629/1246212207 | |
// 写経+α(関数少し変えただけ) | |
// 改行の練習になった気がする. | |
object Houses extends Enumeration{ | |
val red = Value("Red") | |
val white = Value("White") | |
val green = Value("Green") | |
val yellow = Value("Yellow") | |
val blue = Value("Blue") |
This file contains 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
#!/bin/sh | |
if [ $# -ne 1 ] ; then | |
echo "usage: sbtg project_name" 1>&2 | |
exit 1 | |
fi | |
echo "name := \"$1\"\n" > build.sbt | |
echo 'version := "1.0"\n' >> build.sbt | |
echo 'scalaVersion := "2.9.1"\n' >> build.sbt |
This file contains 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
name := "project_name" | |
version := "1.0" | |
scalaVersion := "2.9.1" | |
libraryDependencies += "org.scalatest" %% "scalatest" % "1.7.1" % "test" | |
/** Console */ | |
initialCommands in console := "import org.scalatest.FunSuite" |