Cons

beginner

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@262d3e2f

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))
// ListK(list=[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(Tuple2(a=1, b=ListK(list=[2, 3])))

emptyList<Int>().uncons()
// None

Data types

Module Data types
arrow.core ListK