All Projects → cbyad → either_option

cbyad / either_option

Licence: MIT license
A small typed and safe library for error handling with functionnal programming concept in Dart and flutter project

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to either option

tiinvo
Functions for tacit programming and functional types for TypeScript and JavaScript.
Stars: ✭ 36 (+5.88%)
Mutual labels:  option, result, either
common
A minimal library that defines primitive building blocks of PHP code.
Stars: ✭ 28 (-17.65%)
Mutual labels:  option, result
monas
🦋 Scala monads for javascript
Stars: ✭ 21 (-38.24%)
Mutual labels:  option, either
either
Elm Either
Stars: ✭ 24 (-29.41%)
Mutual labels:  result, either
ts-belt
🔧 Fast, modern, and practical utility library for FP in TypeScript.
Stars: ✭ 439 (+1191.18%)
Mutual labels:  option, result
rust-error-handle
detail rust error handle
Stars: ✭ 47 (+38.24%)
Mutual labels:  option, result
apropos
Fast strong typed 'Either' data structure for typescript and flow
Stars: ✭ 20 (-41.18%)
Mutual labels:  either
rocket-pipes
Powerful pipes for TypeScript, that chain Promise and ADT for you 🚌 -> ⛰️ -> 🚠 -> 🏂 -> 🚀
Stars: ✭ 18 (-47.06%)
Mutual labels:  either
typescript-monads
📚Write safer TypeScript using Maybe, List, Result, and Either monads.
Stars: ✭ 94 (+176.47%)
Mutual labels:  either-monad
Swift Argument Parser
Straightforward, type-safe argument parsing for Swift
Stars: ✭ 2,430 (+7047.06%)
Mutual labels:  option
akshare
AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库
Stars: ✭ 5,155 (+15061.76%)
Mutual labels:  option
Either.java
A right-biased implementation of "Either a b" for Java, using Java 8 for mapping/folding and type inference.
Stars: ✭ 53 (+55.88%)
Mutual labels:  either-monad
Esito
Esito ambition is to be your return type for suspending functions.
Stars: ✭ 58 (+70.59%)
Mutual labels:  result
set-config-resolver
[READ-ONLY] Loads configs to you with CLI --config, -c, --set, -s or sets parameter
Stars: ✭ 50 (+47.06%)
Mutual labels:  option
dart maybe
No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).
Stars: ✭ 20 (-41.18%)
Mutual labels:  option
php-slang
The place where PHP meets Functional Programming
Stars: ✭ 107 (+214.71%)
Mutual labels:  either-monad
Akshare
AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库
Stars: ✭ 4,334 (+12647.06%)
Mutual labels:  option
result
A lightweight C++11-compatible error-handling mechanism
Stars: ✭ 121 (+255.88%)
Mutual labels:  result
J-Curry
A Java library that enables applying Functional Programming concepts like currying and partial application for functions, also it supports types like Either, Try, etc... using RxJava 2 interfaces, compatible with Java 7 and above
Stars: ✭ 17 (-50%)
Mutual labels:  either
Forbind
Functional chaining and promises in Swift
Stars: ✭ 44 (+29.41%)
Mutual labels:  result

either_option

either_option is a simple library typed for easy and safe error handling with functional programming style in Dart. It aims to allow flutter/dart developpers to use the 2 most popular patterns and abstractions : Either and Option, mainly used in FP language like Scala, Haskell, OCaml,...

Installation

Prerelease versions that support null safety

Package link on pub either_option In your pubspec.yaml dependencies add

   either_option: ^2.0.0

Overview

Either

Either Represents a value of one of two possible types. By convention we put missing or error value in an instance of Left and expected success value in an instance of Right.

For example, we fetch an url from a repository repository to get an User details and use Either<ServerError,User> :

  Future<Either<ServerError, User>> getUser(int id) async {
    final baseUrl = "https://fakerestapi.azurewebsites.net/api/Users/$id";

    final res = await http.get(baseUrl);

    if (res.statusCode == 200) {
      dynamic body = json.decode(res.body);
      User user = User.fromJson(body);
      return Right(user);
    }
    if (res.statusCode == 404)
      return Left(ServerError("This user doesn't exist"));

    return Left(ServerError("Unknown server error"));
  }

So now to consume result we can use for example fold method and say what to do with value :

main() async {
  final Repository repository = Repository();

  final Either<ServerError, User> res = await repository.getUser(3);
  final defaultUser = User(id: 0, username: "ko", password: "ko");
  final userName = res.fold((_) => defaultUser.username, (user) => user.username);
  print(userName); // "User 3"
  
//if res was a Left, print(userName) would give "ko"
}

Option

Option Represents a value of one of two possible types. By convention we consider missing value as an instance of None and expected success value in an instance of Some.

  Future<Option<User>> getUserOpt(int id) async {
    final res = await http.get(baseUrl);
    return Option.cond(
        res.statusCode == 200, User.fromJson(json.decode(res.body)));
  }
  // -----------------------------------------
  main() async {
  final Repository repository = Repository();

  final Option<User> res = await repository.getUserOpt(3);
  final defaultUser = User(id: 0, username: "ko", password: "ko");
  final userName = res.getOrElse(defaultUser).username;
  print(userName); // "User 3"

  //if res was a None, print(userName) would give "ko"
}

Ressources & explanation

Medium article

Features

Functions available :

Option Either
fold 👍 👍
map 👍 👍
flatMap 👍 👍
getOrElse 👍
orElse 👍
toLeft 👍
toRight 👍
toEither 👍
Option.empty 👍
Option.of 👍
filter 👍
exists 👍
contains 👍
swap 👍
cond 👍 👍

Example

Tests

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].