Skip to content

Instantly share code, notes, and snippets.

@sshark
Last active May 12, 2016 10:41
Show Gist options
  • Select an option

  • Save sshark/e3711d8e04f7c776adaa to your computer and use it in GitHub Desktop.

Select an option

Save sshark/e3711d8e04f7c776adaa to your computer and use it in GitHub Desktop.
Inspired by a video on Introduction to Haskell for Scala developers, https://skillsmatter.com/skillscasts/7017-intro-to-haskell-for-scala-devs
val fizz: Stream[Option[String]] = List(None,None,Some("Fizz")).toStream #::: fizz
val buzz: Stream[Option[String]] = List(None,None,None,None,Some("Buzz")).toStream #::: buzz
// Stream or laziness is not supported by tupleN's zipped method. Therefore,
// a fix range must be specified i.e. (1 to 30)
((1 to 30).toList, fizz, buzz).zipped.map {
case (n, None, None) => n.toString
case (_, Some(x), None) => x
case (_, None, Some(x)) => x
case (_, Some(x), Some(y)) => x + y
}
import scala.language.postfixOps
Stream.from(1) zip fizz zip buzz map {
case ((n, None), None) => n.toString
case ((_, Some(x)), None) => x
case ((_, None), Some(x)) => x
case ((_, Some(x)), Some(y)) => x + y
} take 30 toList
val foo: Stream[String] = "" #:: "" #:: "Foo" #:: foo
val bar: Stream[String] = "" #:: "" #:: "" #:: "" #:: "Bar" #:: bar
val foobar = (Stream.from(1) zip foo zip bar).map {
case ((w, x), y) => if ((x + y).isEmpty) w.toString else x + y
}
foobar.take(30).toList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment