Id

data. Id

new Id()

The Id data type. Id acts like a wrapper for any value to provide it with a bunch of interfaces

Version:
  • 3.0.0
Source:
Example
const {Id} = require('futils').data;

Id(1); // -> Id(1)

Id(1).value; // -> 1

Extends

Methods

(static) from(a) → {Id}

Lifts a value into a Id. Similiar to Id.of

Source:
Parameters:
Name Type Description
a any

The value to lift

Returns:
Type:
Id

The value wrapped in a Id

Example
const {Id} = require('futils').data;

Id.from(1); // -> Id(1)

(static) fromEither(a) → {Id}

A natural transformation from a Either.Left or Either.Right into a Id

Source:
Parameters:
Name Type Description
a Left | Right

The Either to transform

Returns:
Type:
Id

A new Id

Example
const {Id, Either} = require('futils').data;

const r = Either.Right('a right');
const l = Either.Left('a left');

Id.fromEither(r); // -> Id('a right')
Id.fromEither(l); // -> Id('a left')

(static) fromList(a) → {Id}

A natural transformation from a List into a Id. Please note that this transformation looses data, because only the first element of the list is taken

Source:
Parameters:
Name Type Description
a List

The List to transform

Returns:
Type:
Id

A new Id

Example
const {Id, List} = require('futils').data;

const ls = List.of(2).cons(1);

Id.fromList(ls); // -> Id(1)

(static) fromMaybe(a) → {Id}

A natural transformation from a Maybe.Some or Maybe.None into a Id

Source:
Parameters:
Name Type Description
a Some | None

The Maybe to transform

Returns:
Type:
Id

A new Id

Example
const {Id, Maybe} = require('futils').data;

const some = Maybe.Some('a value');
const none = Maybe.None();

Id.fromMaybe(some); // -> Id('a value');
Id.fromMaybe(none); // -> Id(null)

(static) of(a) → {Id}

Lifts a value into the Id

Source:
Parameters:
Name Type Description
a any

The value to lift

Returns:
Type:
Id

The value wrapped in a Id

Example
const {Id} = require('futils').data;

Id.of(1); // -> Id(1)

ap(a) → {Id}

Applies a function in a Id to a value in another Id

Source:
Parameters:
Name Type Description
a Id

The Id that holds the value

Returns:
Type:
Id

Id which contains the result of applying the function

Example
const {Id} = require('futils').data;

const id = Id(1);

const mInc = Id.of((n) => n + 1);

mInc.ap(id); // -> Id(2)

concat(a) → {Id}

Concatenates a Id with another. Please note, that the inner values have to be part of a Semigroup as well for concatenation to succeed

Source:
Parameters:
Name Type Description
a Id

The Id instance to concatenate with

Returns:
Type:
Id

A new Id

Example
const {Id} = require('futils').data;

const id = Id('hello');

id.concat(Id('world')); // -> Id('helloworld')

extend(f) → {Id}

If given a function that takes a Id and returns a value, returns an Id

Source:
Parameters:
Name Type Description
f function

A function taking a Id

Returns:
Type:
Id

A new Id

Example
const {Id} = require('futils').data;

Id.of(1).extend(({value}) => value + 1); // -> Id(2)

extract() → {any}

Extracts the value from a Id

Source:
Returns:
Type:
any

The value

Example
const {Id} = require('futils').data;

Id.of(1).extract(); // -> 1

flat() → {Id}

Flattens a nested Id one level

Source:
Returns:
Type:
Id

A flat Id

Example
const {Id} = require('futils').data;

const id = Id.of(Id.of(1));

id.flat(); // -> Id(1)

flatMap(f) → {Id}

Maps a Id returning function over a Id and flattens the result

Source:
Parameters:
Name Type Description
f function

A Id returning function to map

Returns:
Type:
Id

A new Id

Example
const {Id} = require('futils').data;

const id = Id(2);

const even = (n) => Id(n % 2 === 0);

id.flatMap(even); // -> Id(true)

map(f) → {Id}

Maps a function over the inner value and wraps the result in a new Id

Source:
Parameters:
Name Type Description
f function

The function to map

Returns:
Type:
Id

A new Id

Example
const {Id} = require('futils').data;

const id = Id('a');

const upperCase = (v) => v.toUpperCase();

id.map(upperCase); // -> Id('A')

reduce(f, x) → {any}

Works much like the Array.reduce method. If given a function and an initial value, calls the function with the initial value and the current value of the Id and returns the result

Source:
Parameters:
Name Type Description
f function

The function to reduce with

x any

The seed value to reduce into

Returns:
Type:
any

Whatever the reducer function returned

Example
const {Id} = require('futils').data;

const id = Id(1);

const reducer = (a, b) => a + b;

id.reduce(reducer, 1); // -> 2

sequence(A) → {Applicative|Array}

Sequences a Id into another Applicative type

Source:
Parameters:
Name Type Description
A Applicative | Array

A constructor with of and ap methods

Returns:
Type:
Applicative | Array

A Id wrapped in the Applicative

Example
const {Id} = require('futils').data;

const id = Id([1]);

id.sequence(Array); // -> [Id(1)]

traverse(f, A) → {Applicative|Array}

Takes a function with the signature (Applicative f) => a -> f a and an Applicative constructor and traverses the Id into the Applicative

Source:
Parameters:
Name Type Description
f function

Function to traverse with

A Applicative | Array

A constructor with of and ap methods

Returns:
Type:
Applicative | Array

A Id wrapped in the Applicative

Example
const {Id} = require('futils').data;

const id = Id(1);

const fn = (n) => [n]

id.traverse(fn, Array); // -> [Id(1)]