Cons

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)
instance
// arrow.optics.typeclasses.Cons$Companion$invoke$1@7d472af0

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()
// Option.Some((1, [2, 3]))
emptyList<Int>().uncons()
// Option.None

Do you like Arrow?

Arrow Org
<