All Projects → aloisdeniel → dart_maybe

aloisdeniel / dart_maybe

Licence: MIT license
No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to dart maybe

tush
No description or website provided.
Stars: ✭ 23 (+15%)
Mutual labels:  functional
ekzo
💫 Functional Sass framework for rapid and painless development
Stars: ✭ 32 (+60%)
Mutual labels:  functional
fn
A functional web framework
Stars: ✭ 34 (+70%)
Mutual labels:  functional
ParsecSharp
The faster monadic parser combinator library for C#
Stars: ✭ 23 (+15%)
Mutual labels:  functional
justuse
Just use() code from anywhere - a functional import alternative with advanced features like inline version checks, autoreload, module globals injection before import and more.
Stars: ✭ 49 (+145%)
Mutual labels:  functional
pipe
Pipe operator for nim.
Stars: ✭ 51 (+155%)
Mutual labels:  functional
lunala
💎│ The official Lunala's source code! Yet a modern space exploration bot.
Stars: ✭ 24 (+20%)
Mutual labels:  functional
PartialFunctions.jl
A small package to simplify partial function application
Stars: ✭ 34 (+70%)
Mutual labels:  functional
sublime-coconut
Coconut syntax highlighting for Sublime Text and VSCode.
Stars: ✭ 18 (-10%)
Mutual labels:  functional
chip-8
A CHIP-8 Emulator written in Haskell
Stars: ✭ 34 (+70%)
Mutual labels:  functional
ugo
Simple and expressive toolbox written in Go
Stars: ✭ 27 (+35%)
Mutual labels:  functional
php-validation-dsl
A DSL for validating data in a functional fashion
Stars: ✭ 47 (+135%)
Mutual labels:  functional
swift-di-explorations
Functional DI explorations in Swift
Stars: ✭ 28 (+40%)
Mutual labels:  functional
typed-prelude
Reliable, standards-oriented software for browsers & Node.
Stars: ✭ 48 (+140%)
Mutual labels:  functional
dotvariant
A type-safe and space-efficient sum type for C# (comparable to discriminated unions in C or C++)
Stars: ✭ 52 (+160%)
Mutual labels:  functional
pen
The parallel, concurrent, and functional programming language for scalable software development
Stars: ✭ 394 (+1870%)
Mutual labels:  functional
nancy
How JavaScript Promise Works
Stars: ✭ 26 (+30%)
Mutual labels:  functional
SAFE-Stack.github.io
Website for Saturn + Azure + Fable + Elmish aka SAFE-Stack
Stars: ✭ 17 (-15%)
Mutual labels:  functional
babl
JSON templating on steroids
Stars: ✭ 29 (+45%)
Mutual labels:  functional
futils
Utilities for generic functional programming
Stars: ✭ 21 (+5%)
Mutual labels:  functional

maybe

Pub

No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).

Usage

The key is that you need to call the some or when to access your potential value so you are forced to check its status before using it.

Maybe<T>.nothing : creating an optional item that is empty

final maybe = Maybe<String>.nothing();

Maybe.some : creating an optional item with a value

final maybe = Maybe.some("hello world");
final isNothing = Maybe<String>.some(null); // By default `some` with a null value is converted to `nothing`
final isNotNothing = Maybe<String>.some(null, nullable: true);

some : extracting some value

final maybe = Maybe.some("hello world");
final value = some(maybe, "default"); // == "hello world"
final maybe = Maybe<String>.nothing();
final value = some(maybe, "default"); // == "default"

isNothing : testing if some value

final maybe = Maybe.some("hello world");
final value = isNothing(maybe); // false
final maybe = Maybe<String>.nothing();
final value = isNothing(maybe); // true

when : triggering an action

var maybe = Maybe.some("hello world");
when(maybe, some: (v) {
    print(v); // "hello world"
});

// Defining nothing
maybe = Maybe.nothing();
when(maybe, some: (v) {
    print(v); // not called!
});

// You can add a default value when nothing
maybe = Maybe<String>.some(null);
when(maybe, some: (v) {
        print(v); // "hello world"
    }, 
    defaultValue: () => "hello world");

mapSome : converts a value type to another

var maybe = Maybe.some("hello world");
var converted = mapSome<String,int>(maybe, (v) => v.length);
var value = some(converted,0); // == 11
var maybe = Maybe<String>.nothing();
var converted = mapSome<String,int>(maybe, (v) => v.length);
var value = some(converted, 0); // == 0

MaybeMap<K,V> : a map with optional values (aka Map<K, Maybe>)

var map = MaybeMap<String,String>();
map["test"] = Maybe.nothing(); // doesn't add value
map["test"] = Maybe.some("value"); // adds value
when(map["test"], some: (v) => print(v));

map["test"] = Maybe.nothing(); // deletes key
when(map["test"], isNothing: (v) => print("deleted :" + map.containsKey("test").toString()));
Map<String,String> maybeMap = {
    "test": "value",
};
var maybeMap = MaybeMap<String,String>.fromMap(maybeMap);
when(map["test"], some: (v) => print(v));

What about quiver's Optional ?

The Optional type has several similarities with Maybe, but there are several subtle differences.

Optional can be null

Let's take a quick example :

class Update {
  final Optional<String> title;
  final Optional<String> description;

  Update({Optional<String> title, Optional<String> description})
      : this.title = title ?? Optional<String>.absent(),
        this.description = description ?? Optional<String>.absent();
}

final update = Update(title: Optional.of('sample'));

update.title.ifPresent((v) {
    print('title: $v');
});

update.description.ifPresent((v) {
    print('description: $v');
});

Thanks to static functions, all can be replaced by :

class Update {
  final Maybe<String> title;
  final Maybe<String> description;

  Update({this.title this.description});
}

final update = Update(title: Maybe.some('sample'));

when(update.title, some: (v) {
    print('title: $v');
});

when(update.description, some: (v) {
    print('description: $v');
});

So, the critical part is that you can forget that Optional can be null itself and produce exceptions (update.title.ifPresent in our example). You are then forced to test its nullity and you come back to the initial problematic. This is where Maybe feels more robust to me.

absent is similar to null

With Maybe, values can be nullable.

In the following example, we explicitly say that the title should have a new null value.

class Update {
  final Maybe<String> title;
  final Maybe<String> description;

  Update({ this.title, this.description});
}

final update = Update(title: Maybe.some(null, nullable: true);

This is really different than having a nothing title, which significates that the title shouldn't be modified.

final update = Update(title: Maybe.nothing());
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].