Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active March 11, 2016 04:37
Show Gist options
  • Save Porges/5dedcd58d50d0a6ec1f5 to your computer and use it in GitHub Desktop.
Save Porges/5dedcd58d50d0a6ec1f5 to your computer and use it in GitHub Desktop.
open System
type IMonoid<'t> =
abstract Zero : 't
abstract Combine : 't -> 't -> 't
let sumMonoid : IMonoid<int> =
{ new IMonoid<int> with
member __.Zero = 0
member __.Combine x y = x + y }
let functionResultMonoid (resultMonoid : IMonoid<'r>) : IMonoid<'a -> 'r> =
{ new IMonoid<'a -> 'r> with
member __.Zero = fun _ -> resultMonoid.Zero
member __.Combine x y = fun k -> resultMonoid.Combine (x k) (y k) }
let endoMonoid : IMonoid<'a -> 'a> =
{ new IMonoid<'a -> 'a> with
member __.Zero = fun x -> x
member __.Combine f g = fun x -> f (g x) }
let functionOne : string -> int = fun (s : string) -> s.Length
let functionTwo : string -> int = fun (s : string) -> Seq.filter Char.IsDigit s |> Seq.length
[<EntryPoint>]
let main argv =
let result = (functionResultMonoid sumMonoid).Combine functionOne functionTwo
printfn "%A" (result "hello 123")
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment