Skip to content

Instantly share code, notes, and snippets.

@sshark
Last active December 13, 2015 22:58
Show Gist options
  • Save sshark/4988029 to your computer and use it in GitHub Desktop.
Save sshark/4988029 to your computer and use it in GitHub Desktop.
def minmax(values : Array[Int]) : (Int, Int) = {
def minmax_(values : Array[Int], n : (Int, Int)) : (Int, Int)= {
if (values.isEmpty) n
else {
values.head match {
case x if x < n._1 => minmax_(values.tail, (x, n._2))
case x if x > n._2 => minmax_(values.tail, (n._1, x))
case _ => minmax_(values.tail, n)
}
}
}
minmax_(values, (Integer.MAX_VALUE, Integer.MIN_VALUE))
}
minmax(Array(5,2,3,10))
val l = List('A → 100, 'B → 200, 'C → 400, 'A → 150, 'B → 250, 'B → 300,'C → 100, 'B → 300, 'C → 200, 'A → 150)
l.foldLeft(Map[Symbol, Int]()){case (m, e) => m.updated(e._1, e._2 + m.getOrElse(e._1, 0))}
val p = l.map(k => k._1 -> List(k._2)).foldLeft(Map[Symbol, List[Int]]()){case (m, e) => m.updated(e._1, e._2 ::: m.getOrElse(e._1, Nil))}
// p : Map[Symbol -> List[Int]]
p.foldLeft(Map[Symbol, (Int,Int,Float)]()){case (m, e) => m.updated(e._1, (e._2.min, e._2.max, e._2.sum / e._2.size))}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment