arrow-core-data / arrow.core / ListK
data class ~~ListK~~<out A> :
ListKOf
<A>,
List
<A>
Deprecated: ListK is deprecated along side Higher Kinded Types in Arrow. Prefer to simply use kotlin.collections.List instead.Arrow provides extension functions on Iterable to cover all the behavior defined for ListK as extension functions
ListK wraps over the platform List
type to make it a type constructor(/docs/0.12/patterns/glossary/#type-constructors).
It can be created from Kotlin List type with a convenient k()
function.
import arrow.core.k
val value =
//sampleStart
listOf(1, 2, 3).k()
//sampleEnd
fun main() {
println(value)
}
For most use cases you will never use ListK
directly but List
directly with the extension functions that Arrow projects over it.
ListK implements operators from many useful typeclasses.
The @extension type class processor expands all type class combinators that ListK
provides automatically over List
For instance, it has combineK
from the SemigroupK(/docs/0.12/arrow/typeclasses/semigroupk/) typeclass.
It can be used to cheaply combine two lists:
import arrow.core.extensions.list.semigroupK.combineK
//sampleStart
val hello = listOf('h', 'e', 'l', 'l', 'o')
val commaSpace = listOf(',', ' ')
val world = listOf('w', 'o', 'r', 'l', 'd')
val combinedList = hello.combineK(commaSpace).combineK(world)
//sampleEnd
fun main() {
println("combinedList = $combinedList")
}
The functions traverse
and sequence
come from Traverse(/docs/0.12/apidocs/arrow-core-data/arrow.typeclasses/-traverse/).
Traversing a list creates a new container KindF,A(/docs/0.12/patterns/glossary/#type-constructors) by combining the result of a function applied to each element:
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.extensions.list.traverse.traverse
import arrow.core.extensions.option.applicative.applicative
//sampleStart
val numbers = listOf(Math.random(), Math.random(), Math.random())
val traversedList = numbers.traverse(Option.applicative(), { if (it > 0.5) Some(it) else None })
//sampleEnd
fun main() {
println("traversedList $traversedList")
}
and complements the convenient function sequence()
that converts a list of ListK<Kind<F, A>>
into a Kind<F, ListK<A>>
:
import arrow.core.Option
import arrow.core.Some
import arrow.core.extensions.list.traverse.sequence
import arrow.core.extensions.option.applicative.applicative
//sampleStart
val requests = listOf(Some(Math.random()), Some(Math.random()), Some(Math.random()))
val sequenceList = requests.sequence(Option.applicative())
//sampleEnd
fun main() {
println("sequenceList = $sequenceList")
}
If you want to aggregate the elements of a list into any other value you can use foldLeft
and foldRight
from Foldable(/docs/0.12/arrow/typeclasses/foldable).
Folding a list into a new value, String
in this case, starting with an initial value and a combine function:
import arrow.core.k
import arrow.core.extensions.list.foldable.foldLeft
val value =
//sampleStart
listOf('a', 'b', 'c', 'd', 'e').k().foldLeft("-> ") { x, y -> x + y }
//sampleEnd
fun main() {
println(value)
}
Or you can apply a list of transformations using ap
from Applicative(/docs/0.12/arrow/typeclasses/applicative/).
import arrow.core.extensions.list.apply.ap
val value =
//sampleStart
listOf(1, 2, 3).ap(listOf({ x: Int -> x + 10 }, { x: Int -> x * 2 }))
//sampleEnd
fun main() {
println(value)
}
<init> | ListK wraps over the platform List type to make it a type constructor(/docs/0.12/patterns/glossary/#type-constructors).ListK(list: List <A>) |
ap | fun <B> ap(ff: ListKOf <(A) -> B>): ListK <B> |
equals | fun equals(other: Any ?): Boolean |
filterMap | fun <B> ~~filterMap~~(f: (A) -> Option <B>): ListK <B> |
flatMap | fun <B> flatMap(f: (A) -> ListKOf <B>): ListK <B> |
foldLeft | fun <B> foldLeft(b: B, f: (B, A) -> B): B |
foldRight | fun <B> foldRight(lb: Eval <B>, f: (A, Eval <B>) -> Eval <B>): Eval <B> |
hashCode | fun hashCode(): Int |
leftPadZip | Returns a ListK containing the result of applying some transformation (A?, B) -> C on a zip, excluding all cases where the right value is null.fun <B, C> leftPadZip(other: ListK <B>, fab: (A?, B) -> C): ListK <C> Returns a ListKTuple2A?,B containing the zipped values of the two listKs with null for padding on the left. fun <B> leftPadZip(other: ListK <B>): ListK < Tuple2 <A?, B>> |
lpadZip | Left-padded zip.fun <B> ~~lpadZip~~(other: ListK <B>): ListK < Tuple2 < Option <A>, B>> |
lpadZipWith | Left-padded zipWith.fun <B, C> ~~lpadZipWith~~(other: ListK <B>, fab: ( Option <A>, B) -> C): ListK <C> |
map | fun <B> map(f: (A) -> B): ListK <B> |
map2 | fun <B, Z> map2(fb: ListKOf <B>, f: ( Tuple2 <A, B>) -> Z): ListK <Z> |
mapNotNull | Returns a ListK containing the transformed values from the original ListK filtering out any null value.fun <B> mapNotNull(f: (A) -> B?): ListK <B> |
padZip | Align two Lists as in zip, but filling in blanks with None.fun <B> ~~padZip~~(other: ListK <B>): ListK < Tuple2 < Option <A>, Option <B>>> Returns a ListK containing the result of applying some transformation (A?, B?) -> C on a zip.fun <B, C> padZip(other: ListK <B>, fa: (A?, B?) -> C): ListK <C> |
padZipWith | Align two Lists as in zipWith, but filling in blanks with None.fun <B, C> ~~padZipWith~~(other: ListK <B>, fa: ( Option <A>, Option <B>) -> C): ListK <C> |
padZipWithNull | Returns a ListKTuple2A?,B? containing the zipped values of the two listKs with null for padding.fun <B> padZipWithNull(other: ListK <B>): ListK < Tuple2 <A?, B?>> |
rightPadZip | Returns a ListK containing the result of applying some transformation (A, B?) -> C on a zip, excluding all cases where the left value is null.fun <B, C> rightPadZip(other: ListK <B>, fa: (A, B?) -> C): ListK <C> Returns a ListKTuple2A,B? containing the zipped values of the two listKs with null for padding on the right. fun <B> rightPadZip(other: ListK <B>): ListK < Tuple2 <A, B?>> |
rpadZip | Right-padded zip.fun <B> ~~rpadZip~~(other: ListK <B>): ListK < Tuple2 <A, Option <B>>> |
rpadZipWith | Right-padded zipWith.fun <B, C> ~~rpadZipWith~~(other: ListK <B>, fa: (A, Option <B>) -> C): ListK <C> |
show | fun ~~show~~(SA: Show <A>): String |
toString | fun toString(): String |
traverse | fun <G, B> traverse(GA: Applicative <G>, f: (A) -> Kind<G, B>): Kind<G, ListK <B>> |
align | fun <A, B> ~~align~~(a: ListK <A>, b: ListK <B>): ListK < Ior <A, B>> |
alignWith | fun <A, B, C> ~~alignWith~~(a: ListK <A>, b: ListK <B>, fa: ( Ior <A, B>) -> C): ListK <C> |
empty | fun <A> ~~empty~~(): ListK <A> |
just | fun <A> ~~just~~(a: A): ListK <A> |
tailRecM | fun <A, B> ~~tailRecM~~(a: A, f: (A) -> Kind< ForListK , Either <A, B>>): ListK <B> |
altFold | fun <T, F, A> Kind<T, A>.altFold(AF: Alternative <F>, FT: Foldable <T>): Kind<F, A> |
altSum | fun <T, F, A> Kind<T, Kind<F, A>>.altSum(AF: Alternative <F>, FT: Foldable <T>): Kind<F, A> |
combineK | fun <A> ListKOf <A>.~~combineK~~(y: ListKOf <A>): ListK <A> |
fix | fun <A> ListKOf <A>.~~fix~~(): ListK <A> |
Do you like Arrow?
✖