Commits

Anonymous committed 9d0f21f

more on monoids

Comments (0)

Files changed (3)

+module List
+ =
+  struct
+
+    include List;
+
+    type t 'a = list 'a;
+
+    open Cd_Ops;
+
+    value reduce : Monoid.t 'a -> t 'a -> 'a
+    = fun mon lst ->
+        fold_left mon#plus mon#zero lst
+    ;
+
+    value map_reduce : ('a -> 'b) -> Monoid.t 'b -> t 'a -> 'b
+    = fun f mon lst ->
+        let plus = mon#plus in
+        fold_left
+          (fun acc item ->
+             plus acc (f item)
+          )
+          mon#zero
+          lst
+    ;
+
+  end
+;
+module String
+ =
+  struct
+
+    include String;
+
+    value monoid_concat = new Monoid.t "" ( ^ );
+
+  end
+;
+(* Monoid is:
+     type 'a
+     value 0 : 'a
+     value ( + ) : 'a -> 'a -> 'a
+   , so that for all x:'a, y:'a,
+     0 + x = x = x + 0
+     (x + y) + z = x + (y + z)
+   every class Monoid.t instance should obey these laws.
+   Note that monoid can be non-commutative: in general case,
+     x + y <> y + x
+   (see Cd_String.String.monoid_concat for example).
+ *)
+
 class t ['a] z p =
   object
     method zero : 'a = z;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.