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
scala> "encyclopedia" groupBy identity mapValues (_.size) | |
res2: scala.collection.immutable.Map[Char,Int] = Map(e -> 2, n -> 1, y -> 1, a -> 1, i -> 1, l -> 1, p -> 1, c -> 2, o -> 1, d -> 1) |
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
import scala.language.implicitConversions | |
// Vertices | |
case class A(l: List[Char]) | |
case class B(l: List[Char]) | |
case class C(l: List[Char]) | |
case class D(l: List[Char]) | |
case class E(l: List[Char]) | |
// Edges |
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
val a = List(2,3,4) | |
val b = List(1,2,3) | |
// O(n^2) and not very elegant. | |
(0 until a.size) forall (i => a(i) > b(i)) // true | |
// O(n) but creates tuples and a temporary list. Yet, more elegant. | |
a zip b forall (x=> x._1 > x._2) // true | |
// same as above but doesn't create a temporary list (lazy) |
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
val mirror = (11 to 0 by -1) | |
mirror map "Barack Obama" mkString //amabO kcaraB | |
// equivalent to | |
mirror.map(index => "Barack Obama"(index)) mkString //amabO kcaraB | |
val shuffle = List(7, 4, 10, 2, 3, 6, 5, 9, 8, 1) | |
shuffle map "Barack Obama" mkString // Ocmra kaba |
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
val codonTable = Map ( | |
"UUU" -> "F", "CUU" -> "L", "AUU" -> "I", "GUU" -> "V" | |
, "UUC" -> "F", "CUC" -> "L", "AUC" -> "I", "GUC" -> "V" | |
, "UUA" -> "L", "CUA" -> "L", "AUA" -> "I", "GUA" -> "V" | |
, "UUG" -> "L", "CUG" -> "L", "AUG" -> "M", "GUG" -> "V" | |
, "UCU" -> "S", "CCU" -> "P", "ACU" -> "T", "GCU" -> "A" | |
, "UCC" -> "S", "CCC" -> "P", "ACC" -> "T", "GCC" -> "A" | |
, "UCA" -> "S", "CCA" -> "P", "ACA" -> "T", "GCA" -> "A" | |
, "UCG" -> "S", "CCG" -> "P", "ACG" -> "T", "GCG" -> "A" | |
, "UAU" -> "Y", "CAU" -> "H", "AAU" -> "N", "GAU" -> "D" |
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
scala> :sh ls /tmp | |
res39: scala.tools.nsc.interpreter.ProcessResult = `ls /tmp` (20 lines, exit 0) | |
scala> res39.lines.maxBy(_.size) | |
res40: String = homebrew-brew-doctor-4Nsy |
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
implicit class pimpTuple[T](val tuple: (T, T)) extends AnyVal { | |
def haveSame(f: (T => Any)) = f(tuple._1) == f(tuple._2) | |
} | |
val (sentence1, sentence2) = ("To be or not to be","That is the question") | |
// Now we can do | |
(sentence1, sentence2) haveSame (_ split " " size) // false | |
// Instead of | |
sentence1.split(" ").size == sentence2.split(" ").size // false |
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
// a function with non-ambiguous type signature | |
def justDoIt(implicit p0: Char, p1: String, p2: Int) = p0 + p1 * p2 | |
// restrict the scope of implicits | |
{ | |
implicit val x = 3 | |
implicit val c = 'a' | |
implicit val s = "abc" | |
justDoIt // returns aabcabcabc | |
} |
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
scala> "nahman".reverse.tails.mkString(" ").reverse | |
// n na nah nahm nahma nahman |