0.1.0 • Published 8 years ago

m-lens v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

m-lens

m-lens provides 3 types of lenses and a corresponding view and update functions, that work directly with vector and hash map datastructures from mori.

Example

import m from 'mori';
import * as lenses from 'm-lens';

let data = m.hashMap(
    'nested', m.hashMap(
        'a', 1,
        'b', 2,
        'c', 3),
    'vector', m.vector(4, 5, 6));

let lens = m.comp(lenses.pull(m.vector('a', 'b')), lenses.at(m.vector('nested')));

console.log(lenses.view(data, lens));

console.log(lenses.update(data, lens, function (d) {
    return m.assoc(d, 'a', 10);
}));

{"a" 1, "b" 2}
{"nested" {"a" 10, "b" 2, "c" 3}, "vector" [4 5 6]}

API

view

view a datastructure through a lens

update

update a datastructure by applying a function over the foci of a lens

at

make a lens that focuses on the element at the given path of a structure

pull

make a lens that extracts a subtree from a datastructure

  • queries are provided in a simplified form of the datomic pull syntax (wildcards, limits, defaults, or recursion)

as

make a lens that presents the element at a given path at a different path

composition

lenses are functions that when composed, produce a new lens equivalent to applying the composed lenses in sequence

join

make a lens that merges the foci of the joined lenses

  • the behavior of joined lenses where structures collide (i.e. through pull & as) is of last wins when viewing, and undefined for updates