Last active
November 20, 2017 16:35
-
-
Save voronoipotato/d2162966603ba0a26da0231bc4e486c9 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
type Magma<'a> = | |
abstract member append : 'a -> 'a -> 'a | |
type Semigroup<'a> = | |
inherit Magma<'a> | |
type Monoid<'a> = | |
inherit Semigroup<'a> | |
abstract member empty : 'a | |
type addMonoid = | |
new () = {} | |
interface Monoid<int> with | |
member this.empty = 0 | |
member this.append (a: int) (b: int) = a + b | |
type andMonoid = | |
new () = {} | |
interface Monoid<bool> with | |
member this.empty = true | |
member this.append (a: bool) (b: bool) = a && b | |
let appendElseEmpty (m: Monoid<'a>) condition (x :'a) (y: 'a) = | |
if condition | |
then m.append x y | |
else m.empty | |
printfn "%A" (appendElseEmpty (new addMonoid()) true 1 2) | |
printfn "%A" (appendElseEmpty (new andMonoid()) true true false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment