Add polymorphic tuple projections and replacements

This commit is contained in:
sneeker committed 2019-02-22 15:45:12 +00:00
1 parent e21b8d4b1e
commit 0377c2f0f4
3 files changed
+10

No files matched your search

+3
View File
@@ -9,3 +9,6 @@ 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)
let _1 = (fun (left, _) -> left), (fun left (_, right) -> left, right)
let _2 = (fun (_, right) -> right), (fun right (left, _) -> left, right)
+2
View File
@@ -6,3 +6,5 @@ val view : ('s, 't, 'a, 'b) lens -> 's -> 'a
val set : ('s, 't, 'a, 'b) lens -> '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
val _1 : ('a * 'c, 'b * 'c, 'a, 'b) lens
val _2 : ('c * 'a, 'c * 'b, 'a, 'b) lens
+5
View File
@@ -20,3 +20,8 @@ let () =
let focus = compose first second in
assert (view focus ((true, "grey"), false) = "grey");
assert (set focus 9 ((true, "grey"), false) = ((true, 9), false))
let () =
assert (set _1 "grey" (1, true) = ("grey", true));
assert (over _2 ~f:string_of_int (false, 9) = (false, "9"));
assert (view (compose _1 _2) ((true, "grey"), ()) = "grey")