• This repository has been archived on 25/Jul/2018
  • Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Composable, immutable getters and setters.

Fantasy Lenses

Lenses are composable, immutable getters and setters. Composable in that they allow updating of nested data structures. Immutable in that the setters return copies of the whole data structure.

Examples

Nested updating

var person = {
        name: "Brian McKenna",
        location: {
            number: 1006,
            street: "Pearl St",
            postcode: 80302
        }
    },
    objectLens = require('fantasy-lenses').Lens.objectLens,
    locationLens = objectLens('location'),
    numberLens = objectLens('number'),
    store = locationLens.andThen(numberLens).run(person);

console.log(store.get());
// 1006

console.log(store.set(1007));
// { name: 'Brian McKenna',
//   location: { number: 1007, street: 'Pearl St', postcode: 80302 } }

Accessing optional fields

var data = [{
        name: "First record",
        config: {
            type: 2
        }
    }, {
        description: "Hello world",
        config: {
            type: 3
        }
    }, {
        name: "Third record"
    }],
    objectLens = require('fantasy-lenses').PartialLens.objectLens,
    configLens = objectLens('config'),
    typeLens = objectLens('type'),
    configTypeLens = configLens.andThen(typeLens);

console.log(data.filter(function(o) {
    return configTypeLens.run(o).fold(
        function(store) {
            // Get the field's content
            return store.get() == 2;
        },
        function() {
            // Didn't find field
            return false;
        }
    );
}));
// [ { name: 'First record', config: { type: 2 } } ]

More Repositories

1

fantasy-land

Specification for interoperability of common algebraic structures in JavaScript
JavaScript
9,863
star
2

static-land

Specification for common algebraic structures in JavaScript based on Fantasy Land
JavaScript
771
star
3

daggy

Library for creating tagged constructors.
JavaScript
696
star
4

fantasy-promises

JavaScript
89
star
5

fantasy-birds

port of the haskell package Data.Aviary.Birds. Every thing for your combinatory needs.
JavaScript
85
star
6

fantasy-combinators

Common combinators.
JavaScript
66
star
7

fantasy-io

IO control structure.
JavaScript
37
star
8

fantasy-maybes

Option data structure.
JavaScript
36
star
9

sweet-fantasies

sweet.js macros for Fantasy Land compatible structures.
JavaScript
34
star
10

fantasy-laws

Property-based tests for FL-compatible ADTs
JavaScript
33
star
11

fantasy-monoids

A collection of monoids
JavaScript
28
star
12

fantasy-frees

Free monad
JavaScript
25
star
13

fantasy-readers

Reader control structure.
JavaScript
24
star
14

fantasy-eithers

Either data structure.
JavaScript
21
star
15

fantasy-identities

Identity data structure.
JavaScript
20
star
16

fantasy-sorcery

JavaScript
19
star
17

fantasy-check

QuickCheck Library using Fantasy-Land
JavaScript
18
star
18

fantasy-states

State control structure.
JavaScript
16
star
19

fantasy-examples

Example uses of Fantasy Land projects.
JavaScript
15
star
20

fantasy-tuples

Tuple data structures.
JavaScript
15
star
21

ECMAScript-proposals

Meta repository for discussions regarding ECMAScript proposals
12
star
22

fantasy-writers

Writer control structure.
JavaScript
10
star
23

fantasy-helpers

Helper functions
JavaScript
7
star
24

fantasy-validations

Validation data structure.
JavaScript
6
star
25

fantasy-stores

Store control structure.
JavaScript
5
star
26

unified-specification

Temporary repository for unified static-land and fantasy-land specification
5
star
27

function-prototype-map

4
star
28

fantasy-profunctors

Profunctor
JavaScript
3
star
29

fantasy-these

Data type isomorphic to α ∨ β ∨ (α ∧ β)
JavaScript
3
star
30

fantasy-equality

Library to enable equality of different types.
JavaScript
2
star
31

fantasy-consts

JavaScript
2
star
32

fantasy-cofrees

Cofree comonad structure.
JavaScript
2
star
33

fantasy-coproducts

Coproducts
JavaScript
2
star