Last active
December 21, 2019 14:28
-
-
Save jobjo/41cf3d9d9d5674db32f40afdbf29df18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* Functors and modular implicits | |
* opam switch 4.02.0+modular-implicits | |
* eval `opam config env` | |
*) | |
module type FUNCTOR = sig | |
type 'a t | |
val map : ('a -> 'b) -> 'a t -> 'b t | |
end | |
let map {F : FUNCTOR} = F.map | |
implicit module ComposeFunctor {A : FUNCTOR} {B : FUNCTOR } = struct | |
type 'a t = ('a B.t) A.t | |
let map f = A.map (B.map f) | |
end | |
implicit module ProdFunctor {A : FUNCTOR} {B : FUNCTOR } = struct | |
type 'a t = 'a A.t * 'a B.t | |
let map f (x,y) = (A.map f x, B.map f y) | |
end | |
implicit module ListFunctor = struct | |
type 'a t = 'a list | |
let map f = List.map f | |
end | |
implicit module OptionFunctor = struct | |
type 'a t = 'a option | |
let map f = function | |
| Some x -> Some (f x) | |
| None -> None | |
end | |
let e1 = map String.length ["list"; "of"; "strings"];; | |
let e2 = map sqrt (Some 9.);; | |
let e3 = map String.length [Some "foo"; None; Some "bar"];; | |
let e4 = map String.length (Some ["a"; "bc"; "def"]);; | |
let e4 = map String.length ([Some "apple"; None], Some "pear");; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment