Last active
October 20, 2015 22:18
-
-
Save groveriffic/42f102203d7524692ad8 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
package main | |
import ( | |
"fmt" | |
"log" | |
) | |
// String Concatenation Monoid | |
var identity string = "" | |
var mappend = func(a, b string) string { | |
return a + b | |
} | |
func main() { | |
// s could be replaced with any value of type string | |
s := "foo" | |
identityVerified := mappend(identity, s) == s && mappend(s, identity) == s | |
if !identityVerified { | |
log.Fatal("invalid identity") | |
} | |
// a, b, and c could be any values of our given type | |
a := "foo" | |
b := "bar" | |
c := "baz" | |
associativityVerified := mappend(mappend(a, b), c) == mappend(a, mappend(b, c)) | |
if !associativityVerified { | |
log.Fatal("not associative") | |
} | |
fmt.Println("You've got a monoid") | |
strings := []string{"Hello", " Gophers!", | |
" Can", " I", " sell", " you", | |
" some", " monoids?"} | |
concatenated := identity | |
for _, s := range strings { | |
concatenated = mappend(concatenated, s) | |
} | |
fmt.Println(concatenated) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment