option 0.3.1

  • README.md
  • Installing
  • Versions
  • --

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 {dynamic(T n)}     - Mapper to apply to inner value if any.
   * @return {Option<dynamic>} - The mapped result
   */
  Option<dynamic> map(dynamic mapper(T n));

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

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  option: ^0.3.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:option/option.dart';
  
Version Uploaded Documentation Archive
1.2.0 Apr 23, 2016 Go to the documentation of option 1.2.0 Download option 1.2.0 archive
1.1.0 Aug 19, 2014 Go to the documentation of option 1.1.0 Download option 1.1.0 archive
1.0.2 Aug 17, 2014 Go to the documentation of option 1.0.2 Download option 1.0.2 archive
1.0.0 Aug 16, 2014 Go to the documentation of option 1.0.0 Download option 1.0.0 archive
0.3.3 May 4, 2013 Go to the documentation of option 0.3.3 Download option 0.3.3 archive
0.3.2 Apr 18, 2013 Go to the documentation of option 0.3.2 Download option 0.3.2 archive
0.3.1 Apr 13, 2013 Go to the documentation of option 0.3.1 Download option 0.3.1 archive
0.3.0 Apr 1, 2013 Go to the documentation of option 0.3.0 Download option 0.3.0 archive
0.2.3 Mar 17, 2013 Go to the documentation of option 0.2.3 Download option 0.2.3 archive
0.2.2 Mar 17, 2013 Go to the documentation of option 0.2.2 Download option 0.2.2 archive

All 13 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
--
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
--
Learn more about scoring.

This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.