Coder Social home page Coder Social logo

meowtype / maybedart Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 0.0 40 KB

Some / Maybe / Option / Result / Nullable, like Haskel Maybe, Can avoid semantic confusion caused by returning null when returning a value

Home Page: https://pub.dev/packages/some

License: MIT License

Dart 100.00%
some maybe option result nullable dart none monad functor possibly

maybedart's Introduction

Some / Maybe / Option / Result / Nullable

Can avoid semantic confusion caused by returning null when returning a value

like Haskel Maybe


pub MIT


Usage

Import

import 'package:some/index.dart'; // import all

Create

Maybe<int> some = Some(1);
Maybe<int> none = None();

Switch (dart > 3.0.0)

Maybe<int> some = Some(1);

var a = switch (some) {
    Some(v: var v) => v,
    null => 0
};

Check has value

some.isSome
none.isNone
some.has

Get value

if(some.has) {
    some.val!
}

Monadic

some.map((v) => 'some');
some.then((v) => 'some');
some.flatMap((v) => Some('some'));
some.andThen((v) => Some('some'));

Logical

some.and(Some('some'));
some.andThen((v) => Some('some'));
some.or(Some('some'));
some.orElse(() => Some('some'));
some.xor(Some('some'));

Flatten

someSome.flatten();

Pattern matching

some.when(some: (v) => 'some', none: () => 'none');
some.some((v) => 'some');
none.none(() => 'none');
none.defaultVal('none');
none.defaultValFn(() => 'none');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.