From ca9d8de37fc016139a816139083474d6f497b8c2 Mon Sep 17 00:00:00 2001 From: annieversary Date: Fri, 12 Nov 2021 12:44:20 +0000 Subject: [PATCH] add prelude --- README.md | 20 ++++++++++++++++---- examples/convoluted.rs | 2 +- examples/main.rs | 5 ++++- examples/struct_fields.rs | 2 +- src/lib.rs | 10 +++++++++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b19ed3a..6d69c30 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,19 @@ # bad optics -ergonomic no-macro lenses for rust +ergonomic lenses in rust + +`bad-optics` implements the haskell concept of lenses, prisms, and traversals in rust + +it does *not* implement the operators, as it's not really a thing we can do in rust + +does bringing lenses into rust actually make sense? probably not, but it was fun to implement ## example ```rust use bad_optics::{ -lenses::{_0, _1}, -*, + lenses::{over, set}, + prelude::*, }; fn main() { @@ -41,6 +47,12 @@ fn main() { // 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)); + assert_eq!(res, ((1, 6), 3)); } ``` + +## how to use + +bad-optics provides some of the lenses, prisms, and traversals defined in `lens`. i'm still trying to add more, so if there's one you need and it's missing from here, feel free to open a PR + +if you don't know how lenses work, this is not really gonna be a tutorial, you should read [this](https://hackage.haskell.org/package/lens-tutorial-1.0.3/docs/Control-Lens-Tutorial.html) first instead. the general idea is that they are first-class getters and setters diff --git a/examples/convoluted.rs b/examples/convoluted.rs index badcf1c..8094ce1 100644 --- a/examples/convoluted.rs +++ b/examples/convoluted.rs @@ -1,4 +1,4 @@ -use bad_optics::{lenses::*, prisms::*, traversals::*}; +use bad_optics::prelude::*; #[derive(Debug, PartialEq, Clone)] struct MyStruct { diff --git a/examples/main.rs b/examples/main.rs index f85cc05..ecc7cef 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -1,4 +1,7 @@ -use bad_optics::lenses::*; +use bad_optics::{ + lenses::{over, set}, + prelude::*, +}; fn main() { let a = ((1, 2), 3); diff --git a/examples/struct_fields.rs b/examples/struct_fields.rs index a1c926e..011b7e9 100644 --- a/examples/struct_fields.rs +++ b/examples/struct_fields.rs @@ -1,4 +1,4 @@ -use bad_optics::lenses::*; +use bad_optics::prelude::*; #[derive(Debug, PartialEq, Clone)] struct MyStruct { diff --git a/src/lib.rs b/src/lib.rs index 616e1f0..490e4ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,15 @@ #![feature(unboxed_closures, fn_traits)] -mod combinations; +pub mod combinations; mod fns; pub mod lenses; pub mod prisms; pub mod traversals; + +pub mod prelude { + pub use crate::combinations::*; + + pub use crate::lenses::*; + pub use crate::prisms::*; + pub use crate::traversals::*; +}