Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Commit

Permalink
add docs for filterNullMap
Browse files Browse the repository at this point in the history
  • Loading branch information
tapegram committed Jul 24, 2020
1 parent 6193c83 commit 0a4f0e8
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions arrow-core-data/src/main/kotlin/arrow/core/ListK.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package arrow.core

import arrow.Kind
import arrow.core.Ior.*
import arrow.higherkind
import arrow.typeclasses.Applicative
import arrow.typeclasses.Show
Expand Down Expand Up @@ -171,6 +172,21 @@ data class ListK<out A>(private val list: List<A>) : ListKOf<A>, List<A> by list
fun <B> filterMap(f: (A) -> Option<B>): ListK<B> =
flatMap { a -> f(a).fold({ empty<B>() }, { just(it) }) }

/**
* Returns a [ListK] containing the transformed values from the original
* [ListK] as long as they are non-null.
*
* Example:
* ```kotlin:ank:playground
* import arrow.core.*
* listOf(1, 2).k().filterNullMap {
* when (it % 2 == 0) {
* true -> it.toString()
* else -> null
* }
* } // Result: listOf("2").k()
* ```
*/
fun <B> filterNullMap(f: (A) -> B?): ListK<B> =
flatMap { a -> mapN(f(a)) { just(it) } ?: empty<B>() }

Expand Down

0 comments on commit 0a4f0e8

Please sign in to comment.