arrow-core-data / arrow.core / ListK

ListK

@higherkind data class ListK<out A> : List<A>

beginner

ListK wraps over the platform List type to make it a type constructor.

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 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.

Traversing a list creates a new container Kind<F, A> 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.

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.


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)
}

Supported type classes

Module Type classes
arrow.aql Count, From, GroupBy, Max, Min, OrderBy, Select, Sum, Union, Where
arrow.optics.typeclasses Cons, Each, FilterIndex, Index, Snoc
arrow.recursion.typeclasses Birecursive, Corecursive, Recursive
arrow.typeclasses Alternative, Applicative, Apply, Eq, Foldable, Functor, FunctorFilter, Hash, Monad, MonadCombine, MonadFilter, Monoid, MonoidK, Monoidal, Semigroup, SemigroupK, Semigroupal, Show, Traverse

Constructors

<init> ListK(list: List<A>)

Functions

ap fun <B> ap(ff: <ERROR CLASS><(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) -> <ERROR CLASS><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
map fun <B> map(f: (A) -> B): ListK<B>
map2 fun <B, Z> map2(fb: <ERROR CLASS><B>, f: (Tuple2<A, B>) -> Z): ListK<Z>
traverse fun <G, B> traverse(GA: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, ListK<B>>

Companion Object Functions

empty fun <A> empty(): ListK<A>
just fun <A> just(a: A): ListK<A>
tailRecM fun <A, B> tailRecM(a: A, f: (A) -> Kind<<ERROR CLASS>, Either<A, B>>): ListK<B>

Type Class Hierarchy