Cons provides a Prism between a structure S and its first element A and tail S. It provides a convenient way to attach or detach elements to the beginning side of a structure [S].

It can be constructed by providing the Prism.

import arrow.core.ListK
import arrow.optics.extensions.listk.cons.cons
import arrow.optics.typeclasses.Cons

val listFirst = ListK.cons<Int>().cons()
val instance = Cons(listFirst)
// arrow.optics.typeclasses.Cons$Companion$invoke$1@2bca183f

It defines two functions: cons and uncons.

cons prepends an element A to a structure S.

import arrow.optics.extensions.list.cons.cons

1.cons(listOf(2, 3))
// [1, 2, 3]

uncons detaches the first element A from a structure S.

import arrow.optics.extensions.list.cons.uncons

listOf(1, 2, 3).uncons()
// Some((1, [2, 3]))
// None

Data types

Module Data types
arrow.core ListK

Do you like Arrow?

Arrow Org