Title: | Minimal implementation of functional lenses |
---|---|
Description: | Minimal implementation of functional lenses, inspired by the `lenses` package. |
Authors: | Albert Wang [aut, cre] |
Maintainer: | Albert Wang <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9009 |
Built: | 2024-11-14 23:22:16 UTC |
Source: | https://github.com/arbelt/tinylens |
The resulting lens first applies the left lens, then the right lens.
l %.% m
l %.% m
l |
First lens |
m |
Second lens |
A new lens
Lens into a named attribute of an object.
attr_l(name)
attr_l(name)
name |
Name of the attribute to lens into |
A lens that selects the specified attribute
Convenience function that mirrors purrr::pluck()
.
c_l(...)
c_l(...)
... |
A sequence of lenses and/or integers/logical vectors |
This function returns an illegal lens that filters according to the specified conditions.
filter_il(...)
filter_il(...)
... |
Conditions to filter by |
Conditions are evaluated in the context of the data frame.
A lens that filters the specified rows
Trivial identity lens: returns and sets the object itself.
id_l
id_l
An object of class tinylens::lens
(inherits from S7_object
) of length 1.
Lens into a single element of a list.
index_l(i)
index_l(i)
i |
Index of the element to lens into |
This lens performs indexing using double bracket notation, i.e., x[[i]]
.
A lens that selects the specified element
A lens is a pair of functions that can be used to view and set a value in an object.
lens(view = class_missing, set = class_missing)
lens(view = class_missing, set = class_missing)
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 |
This lens allows you to access and modify elements of a list or vector based on their position or a logical condition.
map_l(l, .ptype = NULL) map_df_l(l)
map_l(l, .ptype = NULL) map_df_l(l)
l |
A lens that selects the elements to lens into |
.ptype |
The prototype of the data structure to return |
A lens that selects the specified elements
Lens into the names
attribute of an object.
names_l
names_l
An object of class tinylens::lens
(inherits from S7_object
) of length 1.
This function applies a lens to a data structure and modifies the focused part.
over(d, l, f)
over(d, l, f)
d |
The data structure to modify |
l |
The lens to apply |
f |
The function to apply |
The modified data structure
Apply a function to each element of a list returned by a lens. Using over
in such cases would require a "lifted" function, which is often unergonomic.
over_map(d, l, f)
over_map(d, l, f)
d |
The data structure to modify |
l |
The list-returning lens to apply |
f |
The function to apply to each element of the list |
The modified data structure
This function returns a lens that selects the specified rows.
rows_l(idx)
rows_l(idx)
idx |
The rows to select |
A lens that selects the specified rows
This function returns a lens that selects the specified columns.
select_l(...)
select_l(...)
... |
Columns to select |
A lens that selects the specified columns
This function applies a lens to a data structure and sets the focused part.
set(d, l, x)
set(d, l, x)
d |
The data structure to set |
l |
The lens to apply |
x |
The value to set |
The modified data structure
Lens into a slice of a vector.
slice_l(idx)
slice_l(idx)
idx |
Indices of the elements to lens into |
This lens performs indexing using single bracket notation, i.e., x[idx]
.
A lens that selects the specified slice
Allows mutation of vector data while preserving attributes, e.g., labels or names.
vec_data_l
vec_data_l
An object of class tinylens::lens
(inherits from S7_object
) of length 1.
x <- c(a = "foo1", b = "bar2") view(x, vec_data_l) set(x, vec_data_l, c("foo2", "bar3"))
x <- c(a = "foo1", b = "bar2") view(x, vec_data_l) set(x, vec_data_l, c("foo2", "bar3"))
This function applies a lens to a data structure and returns the focused part.
view(d, l)
view(d, l)
d |
The data structure to view |
l |
The lens to apply |
The part of the data structure focused by the lens
Illegal lens into elements of a vector that satisfy a predicate.
where_il(p)
where_il(p)
p |
A predicate function |
A lens that selects the elements that satisfy the predicate