Skip to content

Instantly share code, notes, and snippets.

View fernandomora's full-sized avatar

Fernando Mora fernandomora

View GitHub Profile
@fernandomora
fernandomora / MonadSequence.scala
Last active October 14, 2015 17:52
Scala Monad sequence method for generic travesables using for-comprehensions and builder pattern
import scala.language.higherKinds
def sequence[A, M[X] <: TraversableOnce[X]](in: M[Monad[A]])(implicit cbf: CanBuildFrom[M[Monad[A]], A, M[A]]): Monad[M[A]] = {
val unit = Monad(cbf(in))
in.foldLeft(unit){
(mba, ma) => for (ba <- mba; a <- ma) yield ba += a
}.map(_.result())
}
@fernandomora
fernandomora / GenericSquare.scala
Created June 5, 2015 14:53
Generic function to square all the elements of a Iterable returning the original collection
def square[Element, IT[_] <: Iterable[Element], That](it: IT[Element])
(implicit bf: CanBuildFrom[IT[Element], Element, That],
num: Numeric[Element]): That = {
val b = bf.apply(it)
b.sizeHint(it)
it.foreach(elem => b += num.times(elem,elem) )
b.result()
}
\setenv LESS '-iMSx4FXRe'
\pset pager always
\pset null ¤
\pset linestyle unicode
\pset border 2
-- Returns query time cost with each order
\timing
-- Returns more information on errors