Compose lenses whilst preserving unfocussed data
This commit is contained in:
3 files changed
+12
No files matched your search
@@ -5,3 +5,7 @@ let lens ~view ~set = view, set
|
|||||||
let view (get, _) = get
|
let view (get, _) = get
|
||||||
let set (_, put) = put
|
let set (_, put) = put
|
||||||
let over (get, put) ~f source = put (f (get source)) source
|
let over (get, put) ~f source = put (f (get source)) source
|
||||||
|
|
||||||
|
let compose (outer_get, outer_put) (inner_get, inner_put) =
|
||||||
|
(fun source -> inner_get (outer_get source)),
|
||||||
|
(fun value source -> outer_put (inner_put value (outer_get source)) source)
|
||||||
@@ -5,3 +5,4 @@ val lens : view:('s -> 'a) -> set:('b -> 's -> 't) -> ('s, 't, 'a, 'b) lens
|
|||||||
val view : ('s, 't, 'a, 'b) lens -> 's -> 'a
|
val view : ('s, 't, 'a, 'b) lens -> 's -> 'a
|
||||||
val set : ('s, 't, 'a, 'b) lens -> 'b -> 's -> 't
|
val set : ('s, 't, 'a, 'b) lens -> 'b -> 's -> 't
|
||||||
val over : ('s, 't, 'a, 'b) lens -> f:('a -> 'b) -> 's -> 't
|
val over : ('s, 't, 'a, 'b) lens -> f:('a -> 'b) -> 's -> 't
|
||||||
|
val compose : ('s, 't, 'a, 'b) lens -> ('a, 'b, 'c, 'd) lens -> ('s, 't, 'c, 'd) lens
|
||||||
@@ -13,3 +13,10 @@ let () =
|
|||||||
let calls = ref 0 in
|
let calls = ref 0 in
|
||||||
assert (over sample ~f:(fun text -> incr calls; text ^ "s") source = ("colours", true));
|
assert (over sample ~f:(fun text -> incr calls; text ^ "s") source = ("colours", true));
|
||||||
assert (!calls = 1)
|
assert (!calls = 1)
|
||||||
|
|
||||||
|
let () =
|
||||||
|
let first = (fst, fun value (_, rest) -> value, rest) in
|
||||||
|
let second = (snd, fun value (rest, _) -> rest, value) in
|
||||||
|
let focus = compose first second in
|
||||||
|
assert (view focus ((true, "grey"), false) = "grey");
|
||||||
|
assert (set focus 9 ((true, "grey"), false) = ((true, 9), false))
|
||||||
Reference in new issue
Block a user