Skip to content

petegleeson/mappable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mappable

Map anything and everything in the same way. A map function built for the Pipeline Operator.

Experimental: use this at your own risk ☢️

Installation

yarn add @mappable/map

Usage

Here are examples of mapping Javascript language types.

import map from '@mappable/map';

// mapping an array
[1, 2, 3] |> map(i => i + 1) |> map(i => i * 2);

// mapping a Promise
Promise.resolve(1) |> map(i => i + 1);

// mapping an object
{ value: 1 } |> map(i => i + 1);

// mapping a function
(x => x + 2) |> map(i => i + 1);

Here is an example of how you can map your own types.

import map from "@mappable/map";

// your own type
const tree = {
  value: 1,
  children: [{ value: 2, children: [] }, { value: 3, children: [] }]
};

// describe how to map your type
const treeMapper = fn => tree => {
  const { value, children } = tree;
  return {
    value: fn(value),
    children: children |> map(treeMapper(fn))
  };
};

// new tree with all values incremented
tree |> map(i => i + 1, treeFunctor);

About

Map anything and everything in the same way.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published