Lenses - No Mutes

Getters y Setters correctamente

var game = 
{ 
  start: new Date(),
  board: { 
    cells: [{c: 0, d: 1}, {c: 1, d: 2}, {c: 0, d: 1}, {c: 1, d: 1}],
    captures: [{c: 1}, {c: 1}],
  },
  player: {
    name: "John",
    lives: 3,
    stats: {
      wins: 1,
      high_score: 123
    }
  }
}



game.player.stats.wins++

Que esta mal aquí?

import Task from 'data.task'
import Maybe from 'data.maybe'
import {lensProp, lensIndex,compose, map, toUpper, reverse, replace} from 'ramda'
import {mapped, over, view, set, lens, iso, from} from 'ramda-lens'
import {Map, List} from 'immutable'

const user = {id: 3, name: 'Pedro Sanchez', addresses: [
  {street: 'Calle 5', zip: '04821'}, 
  {street: 'Ave. 27 de Febrero', zip: '08082'}
]}

const name = lensProp('name')

view(name, user)
// Pedro Sanchez

set(name, 'Juan Guzman', user)
// { id: 3, name: 'Juan Guzman', ...

over(name, toUpper, user)
// { id: 3, name: 'PEDRO SANCHEZ', ...

Get, Set, Map

const user = {id: 3, name: 'Pedro Sanchez', addresses: [
  {street: 'Calle 5', zip: '04821'}, 
  {street: 'Ave. 27 de Febrero', zip: '08082'}
]}
const first = lensIndex(0)

const firstStreet = compose(addresses, first, street)

view(firstStreet, user)
// 99 Walnut Dr.


over(firstStreet, reverse, user)
// ???

Composición

const arrayIso = iso(x => x.toJS(), x => List.of.apply(List, x))

// spliceAndReturn :: [a] -> [a]
const spliceAndReturn = xs => {
  xs.splice(0,1)
  return xs
}

over(arrayIso, spliceAndReturn, List.of(1,2,3,4,5))
// List [2,3,4,5]
//
over(from(arrayIso), x => x.take(1), [1,2,3,4,5])
// [1]

Iso

Óptica

  • https://bartoszmilewski.com/2015/07/13/from-lenses-to-yoneda-embedding/
  • https://hackage.haskell.org/package/lens
  • http://julien-truffaut.github.io/Monocle/
  • http://julien-truffaut.github.io/Monocle/docs/learning_resources.html
  • https://medium.com/@drboolean/lenses-with-immutable-js-9bda85674780#.15hzbk60h

Referencias

Rodolfo Hansen @kryptt

Lenses

By Rodolfo Hansen

Lenses

Getters y Setters correctamente

  • 108