From 30996082ac3d30cc37e1118fa7bb2e39b926cd46 Mon Sep 17 00:00:00 2001 From: annieversary Date: Fri, 5 Nov 2021 15:28:34 +0000 Subject: [PATCH] uh --- .gitignore | 1 + README.md | 17 ++++++++++++----- examples/main.rs | 13 ++++++++++--- src/lib.rs | 8 ++++++-- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 96ef6c0..16d5636 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target Cargo.lock +.DS_Store diff --git a/README.md b/README.md index b9ad22c..b19ed3a 100644 --- a/README.md +++ b/README.md @@ -6,21 +6,28 @@ ergonomic no-macro lenses for rust ```rust use bad_optics::{ - lenses::{_0, _1}, - *, +lenses::{_0, _1}, +*, }; fn main() { 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; // use the view function to access let res = view(lens, a); assert_eq!(res, 2); - // call the lens as a function + // you can also call the lens as a function let res = lens(a); assert_eq!(res, 2); @@ -32,7 +39,7 @@ fn main() { let a = set(lens, a, 5); 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); assert_eq!(res, ((1, 3), 3)); } diff --git a/examples/main.rs b/examples/main.rs index 1326e48..e9f4fc0 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -6,14 +6,21 @@ use bad_optics::{ fn main() { 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; // use the view function to access let res = view(lens, a); assert_eq!(res, 2); - // call the lens as a function + // you can also call the lens as a function let res = lens(a); assert_eq!(res, 2); @@ -25,7 +32,7 @@ fn main() { let a = set(lens, a, 5); 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); assert_eq!(res, ((1, 3), 3)); } diff --git a/src/lib.rs b/src/lib.rs index c200846..0682c68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,12 +58,12 @@ pub fn over>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L L::over(thing, f) } -// TODO add fn impls - // TODO add third_from_tuple, etc // TODO array traversals +// TODO something for structs + // TODO make over work with changing types mod combinations; @@ -163,6 +163,10 @@ mod tests { assert_eq!(_0(a, |v| v + 1), (2, 2)); let a = ((1, 2), 3); + + let res = _0(a); + assert_eq!(res, (1, 2)); + let lens = _0 + _1; let res = lens(a);