In my previous article about Semigroup, Monoid, Equal, Order, Enum and Show. I covered what those are, what method they have and what derived and syntax sugar methods scalaz bring for them. Also I showed that there are implicit implementations of those for scala standard classes that once are brought into the scope you’ll have access to those. In this article I’m gonna show those methods in action for integer, string and boolean.
Integer
The output
anyVal.intInstance is Monoid, Enum (Enum is also Order and Order is Equal), Show
heaviside(1) 1
heaviside(-1) 0
MonoidOps
1 ⊹ 1 2
1 ⊹ ∅ 1
EnumOps
1.succ 2
1 -+- 2 3
1.pred 0
1 -+- 2 -1
1 |-> 10 List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
1 |--> (2, 10) List(1, 3, 5, 7, 9)
(List(1, 3, 5, 7, 9, 11), 1 |--> (2, 11) )
ShowOps
1.show 1
1
EqualOps
1 ≟ 1 true
1 ≠ 2 true
OrderOps
1 lt 2 false
2 lte 2 true
2 gt 1 true
2 gte 2 true
1 ?|? 2 LT
1 max 2 2
1 min 2 1
String
The output
stringInstance is Monoid, Show, Order (order is also Equal)
StringOps
"code".plural(0) codes
MonoidOps
"a" ⊹ "b" ab
"hello" ⊹ ∅ hello
ShowOps
"abc".show "abc"
"abc"
EqualOps
"a" ≟ "b" true
"a" ≠ "b" true
OrderOps
"a" lt "b" true
"b" lte "b" true
"b" gt "a" true
"b" gte "b" true
"a" ?|? "b" LT
Boolean
The output
StringOps
true ∧ true true
true ∧ false false
false ∧ true false
false ∧ false false
true ∨ true true
true ∨ false true
false ∨ true true
false ∨ false false
true !|| true false
true !|| false false
false !|| true false
false !|| false true
true !&& true false
true !&& false true
false !&& true true
false !&& false true
true --> true true
true --> false false
false --> true true
false --> false true
true <-- true true
true <-- false true
false <-- true false
false <-- false true
true <-- true true
true <-- false true
false <-- true false
false <-- false true
true ⇐ true true
true ⇐ false true
false ⇐ true false
false ⇐ false true
true ⇐ true true
true ⇐ false true
false ⇐ true false
false ⇐ false true
true ⇏ true false
true ⇏ false true
false ⇏ true false
false ⇏ false false
true ⇍ true false
true ⇍ false false
false ⇍ true true
false ⇍ false false
when
unless
true.fold("a", "b") a
false.fold("a", "b" b
false ? "a" | "b" b
true ? "a" | "b" a
false.option(1) None
true.option(1) Some(1)
false.lazyOption(1) LazyNone
true.lazyOption(1) LazySome(<function0>)
false either "a" or "b" -\/(b)
true either "a" or "b" \/-(a)
true ?? 1 1
false ?? 1 0
true !? 1 0
false !? 1 1
anyVal.booleanInstance extends Enum[Boolean] with Show[Boolean]
EnumOps
true.succ false
true -+- 2 true
true.pred false
true -+- 2 true
ShowOps
true.show true
true
Boolean isn't Monoid but BooleanDisjunction and BooleanConjunction are
Conjunction(true) ⊹ Conjunction(true) true
Conjunction(true) ⊹ Conjunction(false) false
Conjunction(false) ⊹ Conjunction(true) false
Conjunction(true) ⊹ Conjunction(false) false
∅[Conjunction] true
Disjunction(true) ⊹ Disjunction(true) true
Disjunction(true) ⊹ Disjunction(false) true
Disjunction(false) ⊹ Disjunction(true) true
Disjunction(true) ⊹ Disjunction(false) false
∅[Disjunction] true