Learn R Programming

tinylens (version 0.1.0)

lens: Create a lens

Description

A lens is a pair of functions that can be used to view and set a value in an object. Lenses are implemented as S7 classes.

Usage

lens(view, set = NULL)

Value

A lens with the specified view and set functions

Arguments

view

A function that takes an object and returns a value

set

A function that takes an object and a value and returns a new object

Details

A "proper" lens should satisfy the following so-called "lens laws":

  • View-Set: set(d, l, view(d, l)) == d

  • Set-View: view(set(d, l, x), l) == x

  • Set-Set: set(set(d, l, x), l, y) == set(d, l, y)

These laws are not enforced by tinylens, but you should strive to follow them when creating your own lenses.

A best effort has been made to ensure that these laws hold for the lenses provided by tinylens, but this is trickier than it might seem because of how R handles subset assignments.

Examples

Run this code
# create a trivial identity lens
l <- lens(view = function(x) x, set = function(x, value) value)

Run the code above in your browser using DataLab