Skip to content

Commit

Permalink
Merge pull request #1 from leowzukw/patch-1
Browse files Browse the repository at this point in the history
Colorized code is more user friendly
  • Loading branch information
pdonadeo committed Jun 22, 2015
2 parents d9955d7 + 8a25598 commit 8d1fcac
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ First load `Lens` in utop.

Given a couple of records

``` ocaml
type car = {
make : string;
model: string;
Expand All @@ -37,9 +38,11 @@ Given a couple of records
author: string;
editor: editor;
};;
```

Create a new nested record

``` ocaml
let scifi_novel = {
name = "Metro 2033";
author = "Dmitry Glukhovsky";
Expand All @@ -53,9 +56,11 @@ Create a new nested record
}
}
};;
```

Now to construct a few lenses to access some things

``` ocaml
let car_lens = {
get = (fun x -> x.car);
set = (fun v x -> { x with car = v })
Expand All @@ -71,16 +76,21 @@ Now to construct a few lenses to access some things
set = (fun v x -> { x with mileage = v })
};;
```

Using these lenses we can modify the mileage without having to unpack the record

``` ocaml
let a = compose mileage_lens (compose car_lens editor_lens) in
_set 10 scifi_novel a;;
```

Or using the `Infix` module we can do the same thing, only shorter.

``` ocaml
_set 10 scifi_novel (editor_lens |-- car_lens |-- mileage_lens);;
(* or *)
((editor_lens |-- car_lens |-- mileage_lens) ^= 10) @@ scifi_novel;;
```

0 comments on commit 8d1fcac

Please sign in to comment.