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
| def splitExpressions(lines: Seq[String]): (Seq[(String, Int)], Seq[(String, LineRange)]) = | |
| { | |
| val blank = (_: String).forall(isSpace) | |
| val isImport = firstNonSpaceIs("import ") | |
| val comment = firstNonSpaceIs("//") | |
| val blankOrComment = or(blank, comment) | |
| val importOrBlank = fstS(or(blankOrComment, isImport)) | |
| import scala.reflect.runtime._ | |
| import scala.reflect.runtime.universe._ |
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
| def mostCommon(l: ParSeq[Int]): Int = { | |
| type Freq = Map[Int, Int] | |
| def addToFreq(freq: Freq, v: Int, f: Int) = | |
| freq.updated(v, freq.getOrElse(v, 0) + f) | |
| def mapFreq(freq: Freq, v: Int) = | |
| addToFreq(freq, v, 1) | |
| def reduceFreq(freq1: Freq, freq2: Freq) = | |
| freq1.foldLeft(freq2) { case (freq, (v, f)) => addToFreq(freq, v, f) } | |
| val freq = l.aggregate(Map[Int, Int]())(mapFreq, reduceFreq) | |
| freq.toSeq.maxBy(_._2)._1 |
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
| Option(System.getenv("USER")) match { | |
| case Some(user) => | |
| println(s"USER=$user") | |
| case None => | |
| println("no USER defined") | |
| } |
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
| def p[A, B, C](a: A, f: (A, B) => C): B => C = f.curried(a) | |
| val fib = { | |
| def f(f0: Int, f1: Int): Stream[Int] = f0 #:: f(f1, f0 + f1) | |
| f(0, 1) | |
| } //> fib : Stream[Int] = Stream(0, ?) | |
| fib(7) //> res0: Int = 13 | |
| fib.take(8).toList //> res1: List[Int] = List(0, 1, 1, 2, 3, 5, 8, 13) |
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
| def drop[T](n: Int, ts: Seq[T]): Seq[T] = { | |
| @tailrec | |
| def go(i: Int, ts: Seq[T], acc: Seq[T]): Seq[T] = (i, ts) match { | |
| case (`n`, _ +: t) => go(0, t, acc) | |
| case (_, h +: t) => go(i + 1, t, h +: acc) | |
| case _ => acc.reverse | |
| } | |
| go(0, ts, Seq()) | |
| } |
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
| def filterNot[T](ts: Seq[T])(p: T => Boolean): Seq[T] = | |
| (ts :\ Seq[T]())((t, a) => if (!p(t)) t +: a else a) | |
| @tailrec | |
| def drop[T](ts: Seq[T])(n : Int): Seq[T] = (n, ts) match { | |
| case (0, _) => ts | |
| case (n, _ +: tts) if n > 0 => drop(tts)(n - 1) | |
| case _ => Seq() | |
| } |
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
| def grouped[T](ts : Seq[T], n: Int) = new Iterator[Seq[T]] { | |
| var cur : Seq[T] = ts | |
| def hasNext = !cur.isEmpty | |
| def next(): Seq[T] = { | |
| val (a, b) = cur.splitAt(n) | |
| cur = b | |
| a | |
| } | |
| } |
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
| def encodeDirect[T](ts: Seq[T]): Seq[(Int, T)] = { | |
| @tailrec | |
| def go(ts: Seq[T], acc: Seq[(Int, T)]): Seq[(Int, T)] = ts match { | |
| case Seq() => acc | |
| case head +: tail => | |
| val (same, different) = ts.span(_ == head) | |
| go(different, acc :+ (same.length, head)) | |
| } | |
| go(ts, Seq()) | |
| } |
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
| def pack[T](ts: Seq[T]): Seq[Seq[T]] = { | |
| @tailrec | |
| def go(ts: Seq[T], acc: Seq[Seq[T]]): Seq[Seq[T]] = ts match { | |
| case Seq() => acc | |
| case head +: tail => | |
| val (same, different) = ts.span(_ == head) // ts.partition(_ == head) | |
| go(different, acc :+ same) | |
| } | |
| go(ts, Seq()) // Seq(Seq()) | |
| } |
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
| def fib(a: Int, b: Int): Stream[Int] = a #:: fib(b, a + b) | |
| fib(1, 1).take(10).toList // == List(1, 1, 2, 3, 5, 8, 13, 21, 34, 55) | |
| fib(1, 1).takeWhile(_ < 4000000).filter(_ % 2 == 0).sum // == 4613732 | |
| // -------------------------------------------------------------- | |
| def euler1(l: Int) = | |
| (1 until l).toStream.filter(x => x % 3 == 0 || x % 5 == 0).sum | |
| def reverse1[T](ts: Seq[T]): Seq[T] = |