Watch video

Video tutorial

SetK

beginner

SetK(Kinded Wrapper) is a higher kinded wrapper around the the Set collection interface.

It can be created from the Kotlin Set type with a convient k() function.

import arrow.*
import arrow.core.*
import arrow.data.*

setOf(1, 2, 5, 3, 2).k()
// SetK(set=[1, 2, 5, 3])

It can also be initialized with the following:

SetK(setOf(1, 2, 5, 3, 2))
// SetK(set=[1, 2, 5, 3])

or

SetK.just(1)
// SetK(set=[1])

given the following:

val oddNumbers = setOf( -11, 1, 3, 5, 7, 9).k()
val evenNumbers = setOf(-2, 4, 6, 8, 10).k()
val integers = setOf(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5).k()

SetK derives the following typeclasses:

Semigroup and SemigroupK:

val numbers = oddNumbers.combineK(evenNumbers.combineK(integers))
numbers
// SetK(set=[-11, 1, 3, 5, 7, 9, -2, 4, 6, 8, 10, -5, -4, -3, -1, 0, 2])
evenNumbers.combineK(integers).combineK(oddNumbers)
// SetK(set=[-2, 4, 6, 8, 10, -5, -4, -3, -1, 0, 1, 2, 3, 5, -11, 7, 9])

Monoid and MonoidK:

numbers.combineK(SetK.empty()) 
// SetK(set=[-11, 1, 3, 5, 7, 9, -2, 4, 6, 8, 10, -5, -4, -3, -1, 0, 2])

Foldable:

numbers.foldLeft(0) {sum, number -> sum + (number * number)}
// 561

Available Instances