p-hatan/ mirror

pub repository

mirror.p-hatan.ir

option

0.2.2

The Option typeclass inspired by Scala's implementation and Haskell's Maybe monad. Option is a composable and more expressive alternative to things that may be null on absence of value.

Download archive

Readme

Option

The option typeclass inspired by scala's implementation and Haskell's Maybe monad. Option is a composable and more expressive alternative to things that may be null on absence of value.

Examples

import 'package:option/option.dart';

main() {
  final some   = new Some(42);
  final inner  = some.get();
  final mapped = some.map((n) => n / 2);
  final toSome = some.flatMap((n) => new Some(12));
  final toNone = some.flatMap((n) => new None<num>());
  final passed = some.filter((n) => (n % 2) == 0);
  final failed = some.filter((n) => (n % 2) != 0);
  final none   = new None();
  final alt1   = none.getOrElse(42);
  final alt2   = none.orElse(() => new Some(33));
  final alt3   = none.orElse(new Some(32));

  print('secret to all ${some.getOrElse(12)}');
}

Documentation

The core of this library exports two classes Some and None that both implement the Option interface. The Option interface is as follows

abstract class Option<T> {

  /**
   * Returns true if type is `None` returns false if type is `Some`
   *
   * @return {bool} - Result of the empty check
   */
  bool isEmpty();

  /**
   * Returns false if type is `None` returns true if type is `Some`
   *
   * @return {bool} - Result of the non-empty check
   */
  bool nonEmpty();

  /**
   * Returns the inner value of the `Option` when called with a `Some`.
   * When called with a `None` this method throws an exception.
   *
   * @return {T} - The inner value
   * @throws     - Throws when called on `None`
   */
  T get();

  /**
   * Returns the inner value if it exists, otherwise it returns the
   * computed value of `alternative`.
   *
   * @param {T|T()} alternative - The value or function of the alternative
   * @return {T}                - The inner value or `alternative`
   */
  T getOrElse(dynamic alternative);

  /**
   * Returns the current `Option` if it's called on a `Some`, otherwise it
   * returns the computed value of `alternative`.
   *
   * @param {Option<T>|Option<T>()} alternative - Value or function of alt
   * @return {Option<T>}                        - Original or alternative
   */
  Option<T> orElse(dynamic alternative);

  /**
   * Returns the inner value when called on `Some`. Returns `null`
   * when called on `None`
   *
   * @return {T|null} - Inner value on existence null otherwise
   */
  dynamic orNull();

  /**
   * Returns a `Left` projection of this `Option` type. If this `Option`
   * type is `Some` the inner value is returned wrapped in a `Left`. If
   * this `Option` type is `None` the supplied `right` value will be
   * returned wrapped in a `Right` type.
   *
   * @param {dynamic} right    - The right value if this option is empty
   * @return {Either<dynamic>} - The left projection
   */
  Either<dynamic, dynamic> toLeft(dynamic right);

  /**
   * Returns a `Right` projection of this `Option` type. If this `Option`
   * type is `Some` the inner value is returned wrapped in a `Right`. If
   * this `Option` type is `None` the supplied `left` value will be
   * returned wrapped in a `Left` type.
   *
   * @param {dynamic} right    - The left value if this option is empty
   * @return {Either<dynamic>} - The right projection
   */
  Either<dynamic, dynamic> toRight(dynamic left);

  /**
   * Applies the `mapper` to the inner value and wraps the result of
   * the mapper in a new `Option<T>` and returns the new `Option<T>`.
   * If the current `Option` is `None` this function just returns `this`
   *
   * @param {T(T n)}     - Mapper to apply to inner value if any.
   * @return {Option<T>} - The mapped result
   */
  Option<T> map(T mapper(T n));

  /**
   * Applies the `flatMapper` to the inner value and returns the new
   * `Option` returned by the `flatMapper`
   *
   * @param {Option<T>(T n)} flatMapper - Flat mapper to apply
   * @return {Option<T>}                - The result of the flat map
   */
  Option<T> flatMap(Option<T> flatMapper(T n));

  /**
   * Applies predicate to the inner value when called on `Some` and
   * if the predicate passes the current `Some` is returned but if
   * it fails it returns a new instance of `None`. When called on
   * `None` this function just returns an instance of `this`
   *
   * @param {bool(T n)} predicate - Predicate to apply
   * @return {Option<T>}          - Result of predicate test
   */
  Option<T> filter(bool predicate(T n));

}
Changelog

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add option
pubspec.yaml
name: option
version: 0.2.2
homepage: https://github.com/JosephMoniz/dart-option
description: >
  The Option typeclass inspired by Scala's implementation and Haskell's Maybe
  monad. Option is a composable and more expressive alternative to things that
  may be null on absence of value.
author: Joseph Moniz <joseph.moniz@gmail.com>
dependencies:
  either: 0.1.2

Version history

VersionPublishedArchiveSHA-256
0.1.02013-03-09T17:36:31.003570ZDownload9b1c2d08e957125706db122bdb299ea4b06183410fa4f6421e0846cb9fe00d7a
0.2.02013-03-09T18:19:01.939340ZDownloadbc85cd65fc921bb124cc487ffc5c8ba11654976f2c8dd3466f28075a5b04acf3
0.2.12013-03-16T18:11:13.977400ZDownloadb47470a785fa871ff5160eb95b357bc268cbf25b41b5a8522305ecb81ce75cef
0.2.22013-03-17T03:16:21.289690ZDownload1515681b7c76d09f6b3d1e577be350c45e99c4a572d9f0b0ea1b14d278940e72
0.2.32013-03-17T03:27:29.364790ZDownloadcf917aec3c93b01bef901429ac6217ea8cc71beca9b39747aa7751995a772e27
0.3.02013-04-01T03:56:02.763760ZDownloadbed59ed173b76c06cd2bb1794ef5a7c098d52e1ce9bc3fad2770bf4a5ab086a3
0.3.12013-04-13T03:27:34.451200ZDownloadbc8778e0f36f45ad6a33f6c96b8bd79f4868c8b986ec56a2f0dcb6c9e704b90d
0.3.22013-04-18T01:11:27.302800ZDownloadaec3170f4487921bd245f1e3165b80149aacb5cd637dca2cd1a1f05b71a68b46
0.3.32013-05-04T04:45:37.955250ZDownload7e2a6e41b0643e91edebd56f5a1f3682436ead6439724d6c3d389d71db582565
1.0.02014-08-16T02:46:24.742920ZDownloadb666b0c5c2676fa40fe7b4963dc5b7eb892bfd64082c31015c911dd231e73f25
1.0.22014-08-17T21:55:54.918560ZDownload0ebd41b7c9b0443447fc34b2cc996b8177b8698ed8e9589e7bb04af7bb77ddab
1.1.02014-08-19T02:00:55.812870ZDownload07b61c7b17882faeb5acc5cb4392b25ef442a355210e20485c93b7f4ff97b5d3
1.2.02016-04-23T17:38:25.708ZDownload43ce0b3489187a3f9cc342ac4bf0533fdba42d75e5012650680d3582c6b37396

Dependencies

either "0.1.2"