Last active
May 16, 2018 20:13
-
-
Save manjuraj/e1f3327cb198b4b64209 to your computer and use it in GitHub Desktop.
scalaz NonEmptyList (NEL)
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
// | |
// NonEmptyList (Nel) | |
// - A singly-linked list that is guaranteed to be non-empty | |
// - https://github.com/scalaz/scalaz/blob/series/7.2.x/core/src/main/scala/scalaz/NonEmptyList.scala | |
// | |
final class NonEmptyList[+A](val head: A, val tail: List[A]) { | |
... | |
} | |
// | |
// NonEmptyList builder | |
// | |
import scalaz._ | |
import syntax.nel._ | |
scala> NonEmptyList.nel(1, List(2, 3)) | |
res0: scalaz.NonEmptyList[Int] = NonEmptyList(1, 2, 3) | |
scala> NonEmptyList.nels(1, 2, 3) | |
res1: scalaz.NonEmptyList[Int] = NonEmptyList(1, | |
scala> 1.wrapNel | |
res2: scalaz.NonEmptyList[Int] = NonEmptyList(1) | |
// | |
// NonEmptyList methods | |
// | |
scala> 0 <:: NonEmptyList(1, 2, 3) | |
res0: scalaz.NonEmptyList[Int] = NonEmptyList(0, 1, 2, 3) | |
scala> NonEmptyList.nels(1, 2, 3).list | |
res1: List[Int] = List(1, 2, 3 | |
scala> NonEmptyList(1, 2, 3) :::> List(4, 5) | |
res2: scalaz.NonEmptyList[Int] = NonEmptyList(1, 2, 3, 4, 5) | |
scala> List(4, 5) <::: NonEmptyList(1, 2, 3) | |
res3: scalaz.NonEmptyList[Int] = NonEmptyList(4, 5, 1, 2, 3) | |
scala> NonEmptyList(1, 2, 3) append NonEmptyList(4, 5) | |
res4: scalaz.NonEmptyList[Int] = NonEmptyList(1, 2, 3, 4, 5) | |
// | |
// List -> Option[NonEmptyList] | |
// | |
import scalaz._ | |
import syntax.std.list._ | |
scala> List(1, 2, 3).toNel | |
res0: Option[scalaz.NonEmptyList[Int]] = Some(NonEmptyList(1, 2, 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment