Functional companion to Kotlin's Standard Library - Core module with iosArm64 target support
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Arrow Core is the foundational module of the Arrow Kotlin library that provides essential data types and functional programming abstractions for Kotlin Multiplatform development. It offers type-safe alternatives to null values, railway-oriented programming for error handling, non-empty collections, and a powerful Raise DSL for typed error management.
implementation("io.arrow-kt:arrow-core:2.1.2")import arrow.core.*
import arrow.core.raise.*For specific imports:
import arrow.core.Either
import arrow.core.Option
import arrow.core.NonEmptyList
import arrow.core.raise.either
import arrow.core.raise.effectimport arrow.core.*
import arrow.core.raise.either
// Working with Optional values
val maybeUser: Option<String> = Some("Alice")
val greeting = maybeUser.map { "Hello, $it!" }.getOrElse { "Hello, Guest!" }
// Error handling with Either
val result: Either<String, Int> = either {
val input = "42"
input.toIntOrNull() ?: raise("Invalid number")
}
// Non-empty collections
val numbers = nonEmptyListOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
// Using the Raise DSL for error accumulation
val validation = either {
val name = validateName("") // raises error if empty
val age = validateAge(-1) // raises error if negative
User(name, age)
}Arrow Core is built around several key components:
Option<A> provides a safe alternative to nullable typesEither<A, B> enables clean error handling without exceptionsIor<A, B> allows collecting multiple errors while preserving successful computationsNonEmptyList and NonEmptySet prevent empty collection errors at compile timeEssential functional data types providing type-safe alternatives to nullable values, error handling, and guaranteed non-empty collections.
sealed class Either<out A, out B>
sealed class Option<out A>
sealed class Ior<out A, out B>
interface NonEmptyCollection<out E>: Collection<E>
data class NonEmptyList<out A>: NonEmptyCollection<A>
data class NonEmptySet<out A>: NonEmptyCollection<A>Composable typed error handling system with short-circuiting and error accumulation capabilities. Includes the Effect system for asynchronous computations and RaiseAccumulate for error collection. The foundation for all error handling in Arrow.
@RaiseDSL
interface Raise<in Error> {
fun raise(error: Error): Nothing
fun <A> Either<Error, A>.bind(): A
fun <A> Option<A>.bind(): A
}
class SingletonRaise<in Error>: Raise<Error>
class IorRaise<Error>: Raise<Error>
class RaiseAccumulate<Error>: Raise<NonEmptyList<Error>>
// Effect System types
typealias Effect<Error, A> = suspend Raise<Error>.() -> A
typealias EagerEffect<Error, A> = Raise<Error>.() -> A
suspend fun <Error, A> effect(block: suspend Raise<Error>.() -> A): Either<Error, A>
fun <Error, A> eagerEffect(block: Raise<Error>.() -> A): Either<Error, A>Suspended computations that can raise typed errors, providing a foundation for asynchronous and concurrent programming with proper error handling.
typealias Effect<Error, A> = suspend Raise<Error>.() -> A
typealias EagerEffect<Error, A> = Raise<Error>.() -> A
suspend fun <Error, A> effect(block: suspend Raise<Error>.() -> A): Either<Error, A>
fun <Error, A> eagerEffect(block: Raise<Error>.() -> A): Either<Error, A>
suspend fun <Error, A> Effect<Error, A>.fold(onError: (Error) -> A, onSuccess: (A) -> A): AFunctional programming extensions for Kotlin collections including multi-parameter zip operations, alignment operations, error accumulation, and safety utilities.
fun <A, B, C, R> Iterable<A>.zip(other1: Iterable<B>, other2: Iterable<C>, transform: (A, B, C) -> R): List<R>
fun <Error, A, B> RaiseAccumulate<Error>.Iterable<A>.mapOrAccumulate(transform: Raise<Error>.(A) -> B): List<B>
fun <A, B> Iterable<A>.align(other: Iterable<B>): List<Ior<A, B>>
fun <A> Iterable<A>.firstOrNone(): Option<A>Immutable tuple data types for grouping 4-9 related values with type safety and destructuring support.
data class Tuple4<out A, out B, out C, out D>(val first: A, val second: B, val third: C, val fourth: D)
data class Tuple5<out A, out B, out C, out D, out E>(val first: A, val second: B, val third: C, val fourth: D, val fifth: E)
// ... through Tuple9
data class TupleN<out A>(val values: List<A>)