CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-arrow-kt--arrow-core-iosx64

Functional companion to Kotlin's Standard Library providing type-safe error handling and functional programming constructs for iOS x64 target

Pending
Overview
Eval results
Files

optional-values.mddocs/

Optional Values

The Option type provides type-safe handling of nullable values, eliminating null pointer exceptions through functional programming patterns. It represents values that may or may not be present.

Core Types

sealed class Option<out A>

data class Some<out T>(val value: T) : Option<T>()

object None : Option<Nothing>

Construction

From Values

// Create Some
fun <A> A.some(): Option<A>

// Create None
fun <A> none(): Option<A>

// From nullable
fun <A> A?.toOption(): Option<A>

Static Constructors

companion object Option {
    fun <A> fromNullable(a: A?): Option<A>
    fun <A> invoke(a: A): Option<A>
    
    // Safe exception handling
    fun <A> catch(f: SingletonRaise<None>.() -> A): Option<A>
    fun <A> catch(recover: (Throwable) -> Option<A>, f: SingletonRaise<None>.() -> A): Option<A>
}

Inspection

// Type checking
fun isNone(): Boolean
fun isSome(): Boolean
fun isSome(predicate: (A) -> Boolean): Boolean

// Value extraction
fun getOrNull(): A?
fun getOrElse(default: () -> A): A

Transformation

// Map operations
fun <B> map(f: (A) -> B): Option<B>
fun <B> flatMap(f: (A) -> Option<B>): Option<B>

// Filtering
fun filter(predicate: (A) -> Boolean): Option<A>
fun filterNot(predicate: (A) -> Boolean): Option<A>

// Type filtering
inline fun <reified B> filterIsInstance(): Option<B>

Pattern Matching

// Fold operation
fun <R> fold(ifEmpty: () -> R, ifSome: (A) -> R): R

Side Effects

// Execute side effects
fun onNone(action: () -> Unit): Option<A>
fun onSome(action: (A) -> Unit): Option<A>

Conversion

// Convert to other types
fun <L> toEither(ifEmpty: () -> L): Either<L, A>
fun toList(): List<A>

Combining Options

// Combine two Options
fun combine(other: Option<A>, combine: (A, A) -> A): Option<A>

// Flatten nested Options
fun Option<Option<A>>.flatten(): Option<A>

Error Recovery

// Recover from None using Raise DSL
fun recover(recover: SingletonRaise<None>.() -> A): Option<A>

Comparison

// Compare Options (when A is Comparable)
operator fun <A : Comparable<A>> compareTo(other: Option<A>): Int

Interoperability

// Convert from Map operations  
fun <K, V> Option<Pair<K, V>>.toMap(): Map<K, V>

// With Effect types
suspend fun <A> Effect<None, A>.toOption(): Option<A>
fun <A> EagerEffect<None, A>.toOption(): Option<A>

Usage Examples

Basic Operations

import arrow.core.*

// Create Options
val someValue = "Hello".some()
val noneValue = none<String>()

// Transform values
val length = someValue.map { it.length } // Some(5)
val empty = noneValue.map { it.length }  // None

// Extract values
val result1 = someValue.getOrElse { "Default" } // "Hello" 
val result2 = noneValue.getOrElse { "Default" }  // "Default"

Chaining Operations

import arrow.core.*

data class User(val name: String, val email: String?)

fun findUser(id: Int): Option<User> = 
    if (id > 0) User("Alice", "alice@example.com").some() 
    else none()

fun validateEmail(email: String): Option<String> =
    if (email.contains('@')) email.some() else none()

val userEmail = findUser(1)
    .flatMap { user -> user.email.toOption() }
    .flatMap { email -> validateEmail(email) }
    .getOrElse { "No valid email" }

Error Handling

import arrow.core.*

// Safe division
fun safeDivide(x: Int, y: Int): Option<Int> = 
    Option.catch { x / y }

val result = safeDivide(10, 0) // None (catches ArithmeticException)
val success = safeDivide(10, 2) // Some(5)

Integration with Nullable Types

import arrow.core.*

// Convert nullable to Option
val nullable: String? = null
val option = nullable.toOption() // None

// Work with Optional chains
fun processUser(name: String?): String =
    name.toOption()
        .filter { it.isNotBlank() }
        .map { "Hello, $it!" }
        .getOrElse { "Invalid name" }

Install with Tessl CLI

npx tessl i tessl/maven-io-arrow-kt--arrow-core-iosx64

docs

collection-extensions.md

error-handling.md

index.md

optional-values.md

partial-results.md

product-types.md

raise-dsl.md

safe-collections.md

utility-functions.md

tile.json