annieversary 2021-11-05 15:28:34 +00:00
parent 5b47f90348
commit 30996082ac
4 changed files with 29 additions and 10 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target /target
Cargo.lock Cargo.lock
.DS_Store

View File

@ -6,21 +6,28 @@ ergonomic no-macro lenses for rust
```rust ```rust
use bad_optics::{ use bad_optics::{
lenses::{_0, _1}, lenses::{_0, _1},
*, *,
}; };
fn main() { fn main() {
let a = ((1, 2), 3); let a = ((1, 2), 3);
// combine lenses // use view to access inside the tuple
let res = view(_0, a);
assert_eq!(res, (1, 2));
let res = view(_1, a);
assert_eq!(res, 3);
// you can combine lenses
let lens = _0 + _1; let lens = _0 + _1;
// use the view function to access // use the view function to access
let res = view(lens, a); let res = view(lens, a);
assert_eq!(res, 2); assert_eq!(res, 2);
// call the lens as a function // you can also call the lens as a function
let res = lens(a); let res = lens(a);
assert_eq!(res, 2); assert_eq!(res, 2);
@ -32,7 +39,7 @@ fn main() {
let a = set(lens, a, 5); let a = set(lens, a, 5);
assert_eq!(a, ((1, 5), 3)); assert_eq!(a, ((1, 5), 3));
// call the lens as a function to modify the value // you can also call the lens as a function to modify the value
let res = lens(a, |v| v + 1); let res = lens(a, |v| v + 1);
assert_eq!(res, ((1, 3), 3)); assert_eq!(res, ((1, 3), 3));
} }

View File

@ -6,14 +6,21 @@ use bad_optics::{
fn main() { fn main() {
let a = ((1, 2), 3); let a = ((1, 2), 3);
// combine lenses // use view to access inside the tuple
let res = view(_0, a);
assert_eq!(res, (1, 2));
let res = view(_1, a);
assert_eq!(res, 3);
// you can combine lenses
let lens = _0 + _1; let lens = _0 + _1;
// use the view function to access // use the view function to access
let res = view(lens, a); let res = view(lens, a);
assert_eq!(res, 2); assert_eq!(res, 2);
// call the lens as a function // you can also call the lens as a function
let res = lens(a); let res = lens(a);
assert_eq!(res, 2); assert_eq!(res, 2);
@ -25,7 +32,7 @@ fn main() {
let a = set(lens, a, 5); let a = set(lens, a, 5);
assert_eq!(a, ((1, 5), 3)); assert_eq!(a, ((1, 5), 3));
// call the lens as a function to modify the value // you can also call the lens as a function to modify the value
let res = lens(a, |v| v + 1); let res = lens(a, |v| v + 1);
assert_eq!(res, ((1, 3), 3)); assert_eq!(res, ((1, 3), 3));
} }

View File

@ -58,12 +58,12 @@ pub fn over<T, L: LensOver<T>>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L
L::over(thing, f) L::over(thing, f)
} }
// TODO add fn impls
// TODO add third_from_tuple, etc // TODO add third_from_tuple, etc
// TODO array traversals // TODO array traversals
// TODO something for structs
// TODO make over work with changing types // TODO make over work with changing types
mod combinations; mod combinations;
@ -163,6 +163,10 @@ mod tests {
assert_eq!(_0(a, |v| v + 1), (2, 2)); assert_eq!(_0(a, |v| v + 1), (2, 2));
let a = ((1, 2), 3); let a = ((1, 2), 3);
let res = _0(a);
assert_eq!(res, (1, 2));
let lens = _0 + _1; let lens = _0 + _1;
let res = lens(a); let res = lens(a);