Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
/ flow-algebra-rs Public archive

Flow implementations of Rust Option<T> / Result<T, E>

License

Notifications You must be signed in to change notification settings

tmtmtoo/flow-algebra-rs

Repository files navigation

flow-algebra-rs CircleCI

Flow implementations of Rust Option<T> Result<T, E>.

Install

$ npm install --save flow-algebra-rs

API implementation status

Note: flow-algebra-rs APIs are provided as camelCase.

Usage

// @flow

import { Some, Ok } from 'flow-algebra-rs'

import type { Option, Result } from 'flow-algebra-rs'

const option: Option<number> = Some.new(10)

const result: Result<number, string> = Ok.new(10)

Utility

Null-safe and Exception-safe programming helper utilities are available.

Option

// @flow

import { Optional } from 'flow-algebra-rs'

const nullableVaule: ?number = null

const value = Optional.new(nullableValue)
  .map(n => n * 2)
  .unwrapOr(0)

console.log(`value is ${value}`) // value is 0

Result

// @flow

import { Try } from 'flow-algebra-rs'

const invalidJSON = 'to be error'

const value = Try.new((): {n: number} => JSON.parse(invalidJSON))
  .map(json => json.n * 2)
  .unwrapOr(0)

console.log(`value is ${value}`) // value is 0