add prelude
parent
e6ac6d85fa
commit
ca9d8de37f
20
README.md
20
README.md
|
@ -1,13 +1,19 @@
|
||||||
# bad optics
|
# 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
|
## example
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use bad_optics::{
|
use bad_optics::{
|
||||||
lenses::{_0, _1},
|
lenses::{over, set},
|
||||||
*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -41,6 +47,12 @@ fn main() {
|
||||||
|
|
||||||
// you can also 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, 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
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use bad_optics::{lenses::*, prisms::*, traversals::*};
|
use bad_optics::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
struct MyStruct {
|
struct MyStruct {
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use bad_optics::lenses::*;
|
use bad_optics::{
|
||||||
|
lenses::{over, set},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = ((1, 2), 3);
|
let a = ((1, 2), 3);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use bad_optics::lenses::*;
|
use bad_optics::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
struct MyStruct {
|
struct MyStruct {
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -1,7 +1,15 @@
|
||||||
#![feature(unboxed_closures, fn_traits)]
|
#![feature(unboxed_closures, fn_traits)]
|
||||||
|
|
||||||
mod combinations;
|
pub mod combinations;
|
||||||
mod fns;
|
mod fns;
|
||||||
pub mod lenses;
|
pub mod lenses;
|
||||||
pub mod prisms;
|
pub mod prisms;
|
||||||
pub mod traversals;
|
pub mod traversals;
|
||||||
|
|
||||||
|
pub mod prelude {
|
||||||
|
pub use crate::combinations::*;
|
||||||
|
|
||||||
|
pub use crate::lenses::*;
|
||||||
|
pub use crate::prisms::*;
|
||||||
|
pub use crate::traversals::*;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue