Skip to content

Commit

Permalink
Merge pull request #506 from rintcius/foldable-doc
Browse files Browse the repository at this point in the history
more examples for Foldable
  • Loading branch information
fthomas committed Sep 3, 2015
2 parents 6476e60 + 9d4d98f commit 05deff3
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/src/main/tut/foldable.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,35 @@ import cats.std.all._
Foldable[List].fold(List("a", "b", "c"))
Foldable[List].foldMap(List(1, 2, 4))(_.toString)
Foldable[List].foldK(List(List(1,2,3), List(2,3,4)))
Foldable[List].reduceLeftToOption(List[Int]())(_.toString)((s,i) => s + i)
Foldable[List].reduceLeftToOption(List(1,2,3,4))(_.toString)((s,i) => s + i)
Foldable[List].reduceRightToOption(List(1,2,3,4))(_.toString)((i,s) => Later(s.value + i)).value
Foldable[List].reduceRightToOption(List[Int]())(_.toString)((i,s) => Later(s.value + i)).value
Foldable[Set].find(Set(1,2,3))(_ > 2)
Foldable[Set].exists(Set(1,2,3))(_ > 2)
Foldable[Set].forall(Set(1,2,3))(_ > 2)
Foldable[Set].forall(Set(1,2,3))(_ < 4)
Foldable[Vector].filter_(Vector(1,2,3))(_ < 3)
Foldable[List].isEmpty(List(1,2))
Foldable[Option].isEmpty(None)
Foldable[List].nonEmpty(List(1,2))
Foldable[Option].toList(Option(1))
Foldable[Option].toList(None)
def parseInt(s: String): Option[Int] = scala.util.Try(Integer.parseInt(s)).toOption
Foldable[List].traverse_(List("1", "2"))(parseInt)
Foldable[List].traverse_(List("1", "A"))(parseInt)
Foldable[List].sequence_(List(Option(1), Option(2)))
Foldable[List].sequence_(List(Option(1), None))
Foldable[List].dropWhile_(List[Int](2,4,5,6,7))(_ % 2 == 0)
Foldable[List].dropWhile_(List[Int](1,2,4,5,6,7))(_ % 2 == 0)
val FoldableListOption = Foldable[List].compose[Option]
FoldableListOption.fold(List(Option(1), Option(2), Option(3), Option(4)))
FoldableListOption.fold(List(Option(1), Option(2), Option(3), None))
FoldableListOption.fold(List(Option("1"), Option("2"), Option("3"), None))
```

Hence when defining some new data structure, if we can define a `foldLeft` and
Expand Down

0 comments on commit 05deff3

Please sign in to comment.