Skip to content

Instantly share code, notes, and snippets.

@zobar
Created January 4, 2017 19:52
Show Gist options
  • Select an option

  • Save zobar/95f37b98970a294ff3530e8797d8963e to your computer and use it in GitHub Desktop.

Select an option

Save zobar/95f37b98970a294ff3530e8797d8963e to your computer and use it in GitHub Desktop.
module Iter = struct
type 'a t = {
fold: 'b. ('b -> 'a -> 'b) -> 'b -> 'b
}
let empty = {
fold = fun f accu -> accu
}
let pure a = {
fold = fun f accu -> f accu a
}
let fold f a b = b.fold f a
let append a b = {
fold = fun f accu -> fold f (fold f accu a) b
}
let map f a = {
fold = fun g accu -> fold (fun accu a -> g accu (f a)) accu a
}
let bind f a = {
fold = fun g accu -> fold (fun accu a -> fold g accu (f a)) accu a
}
let ap f a = {
fold = fun g accu -> fold (fun accu f -> fold g accu (map f a)) accu f
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment