Giter Club home page Giter Club logo

maybe.flow's Introduction

maybe.flow

npm travis downloads styled with prettier

This library provides means for representing values that may or may not exist. This can come handy for representing optional arguments, error handling, and records with optional fields.

Usage

Library represents values that may or may not exist via Maybe<a> type. Existing value is repsented as Just<a> and absense as Nothing:

type Maybe <a> =
  | Nothing
  | Just <a>

Given javascript / typescript semantics Nothing is just a type alias for union of primitives representing absence of value:

type Nothing = null | undefiend | void

For the same reason Just<a> is just a type alias for a:

type Just<a> = a

Chosen approach makes optional function argument & optional record fields of type a be fully compatible with Maybe<a>. There for all of the functions exposed are applicable.

This also avoids necessity to box existing values for the sake of typing.

Import

import * as Maybe from "maybe.flow"

toValue(fallback:a, maybe:Maybe<a>):a

Provide a fallback value, turning an optional value into a normal value.

Maybe.toValue(5, Maybe.just(9)) // => 9
Maybe.toValue(6, Maybe.nothing) // => 6

map(f:(input:a) => b, maybe:Maybe<a>):Maybe<b>

Transform a Maybe value with a given function:

Maybe.map(Math.sqrt, Maybe.just(9)) // => Maybe.just(3)
Maybe.map(Math.sqrt, Maybe.nothing) // => Maybe.nothing

chain(then:(input:a) => Maybe<b>, maybe:Maybe<a>):Maybe<b>

Utility to chain together two computations that may fail (return Nothing).

const makeGreeting = (name:string):string =>
  `Hello ${name}!`
 
const greet = (name:Maybe<string>):Maybe<string> =>
  Maybe.chain(makeGreeting, name)
 
greet(Maybe.nothing) // => Maybe.nothing
greet(Maybe.just('world')) // => Maybe.just('Hello world!')

and(left:Maybe<a>, right:Maybe<b>):Maybe<b>

Returns Nothing if the left Maybe is Nothing, otherwise returns the right Maybe.

Maybe.and(Maybe.just(2), Maybe.nothing) // => Maybe.nothing
Maybe.and(Maybe.nothing, Maybe.just('foo')) // => Maybe.nothing
Maybe.and(Maybe.just(2), Maybe.just('foo')) // => Maybe.just('foo')
Maybe.and(Maybe.nothing, Maybe.nothing) // => Maybe.nothing

or(left:Maybe<a>, right:Maybe<a>):Maybe<a>

Returns the left Maybe if it is a Just value, otherwise returns right Maybe.

Maybe.or(Maybe.just(2), Maybe.nothing) // => Maybe.just(2)
Maybe.or(Maybe.nothing, Maybe.just('foo')) // => Maybe.just('foo')
Maybe.or(Maybe.just(2), Maybe.just(100)) // => Maybe.just(100)
Maybe.or(Maybe.nothing, Maybe.nothing) // => Maybe.nothing

just(value:a) => Maybe<a>

Turns arbitrary value of type a into Maybe<a>. Not that in most cases you dont need to do this and value a can be passed in place for Maybe<a>. Still sometimes it's convinent to downcast Just<a> (which is alias for a) to Maybe<a> and this function does just that:

Maybe.just(4) // => 4

nothing:Maybe<*>

This is just an exported null and you can use null, or undefined or null|undefined|void|a in place for Maybe<a> but sometimes code will read and communiacte intent a lot better when you do use explicit names.

Install

yarn

yarn add --save maybe.flow

npm

npm install --save maybe.flow

maybe.flow's People

Contributors

gozala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

dwillie sanohin

maybe.flow's Issues

Maybe.map(a => Maybe<b>, Maybe<a>):Maybe<Maybe<b>>

Got this report in my email:

Hi,

I found your lib maybe.flow which looks pretty interesting.
I am new to flow and I would like not to write tons of checks when I have many optional properties:

   if (someObj) {
      const { someKey } = someObj;
      if (someKey) { ....
  }

I found that I can use your map for that checks though It doesn't work as I expected. Can I do something like that (the failing case): https://pastebin.com/YH5L8jaw

Thank you for help and time

Inline code from https://pastebin.com/YH5L8jaw inline below for convenience

import * as Maybe from './maybe';
 
type Params = {
    age: number
};
 
type User = {
    name: string,
    params: Maybe.Maybe<Params>
};
 
const user: User = { name: 'Soname', params: { age: 99 } };
 
const params = Maybe.map(v => v.params, Maybe.just(user));
 
const error = Maybe.map(v => v.age, params); // Fails
 
const ok = Maybe.map(v => (v ? v.age : v), params);

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.