Skip to content

Instantly share code, notes, and snippets.

View tomaszperek's full-sized avatar

Tomasz Perek tomaszperek

View GitHub Profile
@tomaszperek
tomaszperek / futureaAndFor.scala
Created October 10, 2015 11:17
For comprehension with futures
val futureSum: Future[Int] = for {
a <- futureA
b <- futureB
c <- futureC
} yield a + b + c
@tomaszperek
tomaszperek / FutureSequence.scala
Created October 10, 2015 11:18
How Future.sequence works
results: Future[Seq[Int]] = Future.sequence(Seq(futureA, futureB, futureC))
@tomaszperek
tomaszperek / FutureSeqenceMap.scala
Created October 10, 2015 11:19
Map over Future.sequence
Future.sequence(Seq(futureA, futureB, futureC)).map(_.sum)
@tomaszperek
tomaszperek / FutureZip.scala
Created October 10, 2015 11:20
How Future.zip works
val aAndB: Future[(A, B)] = futureA zip futureB
@tomaszperek
tomaszperek / zip3.scala
Created October 10, 2015 11:20
Future.zip with three futures
val aAndBAndC: Future[((A, B), C)] = futureA zip futureB zip futureC
@tomaszperek
tomaszperek / zip3.scala
Created October 10, 2015 11:21
Zip3 helper
def zip3[A, B, C](fA: Future[A], fB: Future[B], fc: Future[C]): Future[(A, B, C)] =
(fA zip fB zip fC) map { case ((a, b), c) => (a, b, c)}
@tomaszperek
tomaszperek / LimitSizes.scala
Last active October 12, 2015 12:31
Usual way to limit size of lists
def acceptSameSize[T](l: Seq[List[T]]): Unit = {
if (l.exists(_.size != 2)) throw new IllegalArgumentException("list has incorrect size!")
// do something
}
@tomaszperek
tomaszperek / ShapelessSized.scala
Created October 10, 2015 11:24
Use shapeless Sized
def acceptSameSize[T, N <: Nat](l: Seq[Sized[Seq[T], N]]) = {
//do something
}
@tomaszperek
tomaszperek / UseSized.scala
Created October 10, 2015 11:25
Use shapeless sized
val args = List(Sized("abc", "def"), Sized("ghi", "jkl"))
acceptSameSize(args)
@tomaszperek
tomaszperek / IncorrectUseSized.scala
Created October 10, 2015 11:26
Here the code won't compile
val args = List(Sized("abc", "def"), Sized("ghi", "jkl", "mno"))
acceptSameSize(args)