POptional
Optional is an optic that allows to focus into a structure and querying or copy'ing an optional focus.
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.optics.Optional
data class User(val username: String, val email: Option<String>) {
companion object {
// can be out generated by @optics
val email: Optional<User, String> = Optional(User::email) { user, email ->
user.copy(email = Some(email))
}
}
}
fun main(args: Array<String>) {
val original = User("arrow-user", None)
val set = User.email.set(original, "arRoW-UsEr@arrow-Kt.IO")
val modified = User.email.modify(set, String::toLowerCase)
println("original: $original, set: $set, modified: $modified")
}
A (polymorphic) POptional is useful when setting or modifying a value for a type with a optional polymorphic focus i.e. POptional
A POptional can be seen as a weaker Lens and Prism and combines their weakest functions:
set: (S, B) -> T
meaning we can focus into anS
and set a valueB
for a targetA
and obtain a modified sourceT
getOrModify: (S) -> Either<T, A>
meaning it returns the focus of a POptional OR the original value
Parameters
the source of a POptional
the modified source of a POptional
the focus of a POptional
the modified focus of a POptional
Inheritors
Properties
DSL to compose a Optional with focus Either with a Prism with a focus of Either.Right<R>
DSL to compose a Traversal with focus Either with a Prism with a focus of Either.Right<R>
DSL to compose a Prism with focus arrow.core.Some with a Fold with a focus of Option<S>
DSL to compose a Prism with focus arrow.core.Some with a Optional with a focus of Option<S>
DSL to compose a Prism with focus arrow.core.Some with a Setter with a focus of Option<S>
DSL to compose a Prism with focus arrow.core.Some with a Traversal with a focus of Option<S>
Functions
Join two Fold with the same target
Join two POptionalGetter with the same focus
Join two PSetter with the same target
Compose a POptionalGetter with a POptionalGetter
Compose a PTraversal with a PTraversal
Find the first element matching the predicate, if one exists.
Get the first target or null
Get the focus of an OptionalGetter or null
if the is not there
Check if there is at least one target
Get the last target or null
Set the focus of a POptional with a value.