Skip to main content
Module

x/fun/option.ts

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
import * as fun from "https://deno.land/x/fun@v2.0.0-alpha.12/option.ts";

The Option type is generally considered functional programming's response to handling null or undefined. Sometimes Option is also called Maybe. Its purpose is to represent the possibility that some data is not available.

Variables

The canonical implementation of Applicable for Option.

The canonical implementation of Filterable for Option.

The canonical implementation of Flatmappable for Option.

The canonical implementation of Mappable for Option.

The cannonical implementation of the None type. Since all None values are equivalent there is no reason to construct more than one object instance.

The canonical implementation of Reducible for Option.

The canonical implementation of Traversable for Option.

The canonical implementation of Wrappable for Option.

Functions

Replace an first with second if first is None. This allows one to offer a a replacement or default.

Apply a value A wrapped in an option to a function (a: A) => I wrapped in an Option. If either the wrapped value or the wrapped function are None then the result is None, if they are both Some then the result is Some.

The constNone is a thunk that returns the canonical none instance.

Apply a predicate to the inner value of an Option, returning true if the option is Some and the predicate returns true, otherwise returning false.

Fail is an alias of constNone.

Apply a refinement or predicate to the inner value of an Option, returning the original option if the value exists and the predicate/refinement return true, otherwise returning None.

Apply a filter and mapping operation at the same time against an Option. This is equivalent to the flatmap function for Option.

Apply a function (a: A) => Option to the wrapped value of an Option if the wrapped value exists, flattening the application result into an Option. This is the equivalent of first mapping from Option to Option<Option> and then calling join to flatten the Options.

The fromNullable function takes a potentially null or undefined value and maps null or undefined to None and non-null and non-undefined values to Some<NonNullable>.

The fromPredicate function will test the value a with the predicate. If the predicate evaluates to false then the function will return a None, otherwise it will return the value wrapped in Some.

Create an instance of Comparable<Option> given an instance of Comparable.

Create an instance of Initializable<Option> given an instance of Initializable.

getOrElse operates like a simplified fold. One supplies a thunk that returns a default inner value of the Option for the cases where the option is None.

Create an instance of Showable for Option given an instance of Showable for A.

Create an instance of Sortable<Option> given an instance of Sortable.

Tests wether an Option is None, returning true if the passed option is None and false if it is Some.

Tests wether an Option is Some, returning true if the passed option is Some and false if it is None.

Apply the mapping function fai to the inner value of an Option if it exists. If the option is None then this function does nothing.

Apply a mapping function to an Option but if the mapping function returns null or undefined the null or undefined value is lifted into None.

The match functionis the standard catamorphism on an Option. It operates like a switch case operator over the two potential cases for an Option type. One supplies functions for handling the Some case and the None case with matching return types and fold calls the correct function for the given option.

Given a refinement or predicate, return a function that splits an Option into a Pair<Option, Option>. Due to the nature of the option type this will always return Pair<Some, None>, Pair<None, None>, or Pair<None, Some>.

Map and partition over the inner value of an Option at the same time. If the option passed is None then the result is [None, None], otherwise Right will result in [Some, None], and Left will result in [None, Some].

Reduce over an Option. Since an Option contains at most one value this function operates a lot like getOrElse. If the passed option is None then it returns the initial value, otherwise the reducer function is called with both the initial value and the inner A.

The some constructer takes any value and wraps it in the Some type.

toNullable returns either null or the inner value of an Option. This is useful for interacting with code that handles null but has no concept of the Option type.

toUndefined returns either undefined or the inner value of an Option. This is useful for interacting with code that handles undefined but has no concept of the Option type.

Traverse over an Option using the supplied Applicable. This allows one to turn an Option into Kind<V, Option>.

Take a function that can throw and wrap it in a try/catch block. Returns a new function that takes the same arguments as the original but returns the original value wrapped in an Option. If the function throws then the new function returns None, otherwise it returns Some.

Create an Option by wrapping any value A in Some.

Interfaces

Specifies Option as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any substitutions.

Type Aliases

The None type represents the non-existence of a value.

The Option represents a type A that may or may not exist. It's the functional progamming equivalent of A | undefined | null.

The Some type represents the existence of a value.