Need help with lenses?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

DrBoolean
210 Stars 13 Forks 39 Commits 3 Opened issues

Description

Composable kmett style lenses

Services available

!
?

Need anything else?

Contributors list

# 7,331
Shell
specifi...
fantasy...
Perl
22 commits
# 378,865
Clojure
CSS
Shell
6 commits
# 47,385
jvm
Elixir
code-go...
Haxe
1 commit
# 57,222
specifi...
Shell
fantasy...
Java
1 commit

lenses.js

Functional lenses that compose and stuff

require('./src/lenses').expose(global);
var compose = require('pointfree-fantasy').compose;

// setup an easy test fn var toUpperCase = function(x) { return x.toUpperCase(); };

// here's the data structure var user = {name: "Bob", addresses: [{street: '99 Maple', zip: 94004, type: 'home'}, {street: '2302 Powell', zip: 94001, type: 'work'}]}

// make some lenses var L = makeLenses(['name', 'addresses', 'street']);

// compose the lenses var secondAddressesStreet = compose(L.addresses, L._num(2), L.street)

// mess with the user over(secondAddressesStreet, toUpperCase, user) // { name: 'Bob', addresses: [ { street: '99 Maple', zip: 94004, type: 'home' }, { street: '2302 POWELL', zip: 94001, type: 'work' } ] }

view(L.name, user) // 'Bob'

set(L.name, 'Kelly', user) // {name: "Kelly", addresses: [{street: '99 Maple', zip: 94004, type: 'home'}, {street: '2302 Powell', zip: 94001, type: 'work'}]}

For those not already familiar with lenses, a brief introduction can be found here

ROADMAP:

  • add traverses and folds
  • more combinators
  • prisms
  • this list is getting long...

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.