Skip to content

Commit

Permalink
data.maybe - fromEither, fromValidation, ap (flow-typed#1475)
Browse files Browse the repository at this point in the history
* Fix swagger-schema-official version

Fix swagger-schema-official directory name in order to match prerelease version

* Definitions for data.either 1.x.x

* git ignore vscode workspace settings

* data.either: better definition and tests

* data.maybe: fromEither(), fromValidation(), ap() methods

* data.maybe improved tests

tests for:

- fromNullable (static constructor method)
- ap (applicative functor)
  • Loading branch information
StefanoMagrassi authored and gantoine committed Nov 6, 2017
1 parent 2bbd763 commit 0480256
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
// flow-typed signature: 9ed0f65c3fef605d7b40569f85a56b00
// flow-typed version: <<STUB>>/data.maybe_v1.2.2/flow_v0.35.0
// @flow

declare module 'data.maybe' {
import type Either from "data.either";

interface Functor<A> {
map<B>(f: (a: A) => B): Functor<B>;
}

interface Apply<A> extends Functor<A> {
ap<B>(fab: Apply<(a: A) => B>): Apply<B>;
}

interface Applicative<A> extends Apply<A> {
static of<B>(b: B): Applicative<B>;
}

interface Chain<A> extends Apply<A> {
chain<B>(f: (a: A) => Chain<B>): Chain<B>;
}

interface Monad<A> extends Applicative<A>, Chain<A> {}

interface Validation<F, S> extends Applicative<S> {}

declare module "data.maybe" {
declare class IMaybe<T> {
isNothing: boolean;
isJust: boolean;
Expand All @@ -10,14 +31,17 @@ declare module 'data.maybe' {
toJSON(): Object;
get(): T;
getOrElse(defaultValue: T): T;
map<B>(f: (v:T) => B): IMaybe<B>;
chain<B>(f: (v:T) => IMaybe<B>): IMaybe<B>;
map<B>(f: (v: T) => B): IMaybe<B>;
ap<B>(fb: IMaybe<B> | Applicative<B>): IMaybe<B>;
chain<B>(f: (v: T) => IMaybe<B>): IMaybe<B>;
orElse<B>(f: () => IMaybe<B>): IMaybe<B>;
cata<B>(patterns: {| Nothing: () => B, Just: (v:T) => B |}): B;
cata<B>(patterns: {| Nothing: () => B, Just: (v: T) => B |}): B;
static Nothing<T>(): IMaybe<T>;
static Just<T>(value: T): IMaybe<T>;
static of<T>(value: T): IMaybe<T>;
static fromNullable(value: ?T): IMaybe<T>;
static fromEither<L, R>(em: Either<L, R>): IMaybe<R>;
static fromValidation<F, S>(vm: Validation<F, S>): IMaybe<S>;
}

declare var exports: typeof IMaybe;
Expand Down
23 changes: 16 additions & 7 deletions definitions/npm/data.maybe_v1.x.x/test_data.maybe_v1.x.x.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// @flow
import Maybe from 'data.maybe';
import Maybe from "data.maybe";

const concat = x => y => x + y;


let a = Maybe.Nothing();
let b = Maybe.Just(5);
let c = Maybe.Just(10);
const a = Maybe.Nothing();
const b = Maybe.Just(5);
const c = Maybe.Just(10);
const d = Maybe.fromNullable(null);
const e = Maybe.of(concat);

b.map(x => x + 5);
b.chain(x => x < 5 ? Maybe.Just(x + 5) : Maybe.Nothing());
b.chain(x => (x < 5 ? Maybe.Just(x + 5) : Maybe.Nothing()));
d.map(x => `is ${x}`);

const x : number = b.getOrElse(10);
const x: number = b.getOrElse(10);
const y: string = d.getOrElse("boo");
const z: Maybe<string> = e.ap(Maybe.of("a")).ap(Maybe.of("b"));

// --- Errors
// $ExpectError
b.chain(x => x + x);

// $ExpectError
const _z = e.ap("a");

0 comments on commit 0480256

Please sign in to comment.