Source

scalaz-guide / ref-identity.rst

Reference

Some of the operations that Identity helps support are

The headers indicate the unicode symbol if any, followed by the ascii equivalent separated by a colon

η : pure

Wraps a value with an applicative functor. eg.

Related : Pure

scala> 1.pure[Tuple1]
res0: (Int,) = (1)

scala> 1.pure[List]
res1: List[Int] = List(1)

⊹ : |+|

This is the mappend operator

Related : Semigroup

scala> "abc" |+| "def"
res0: java.lang.String = abcdef

scala> List(1,2,3) |+| List(4,5,6)
res1: List[Int] = List(1, 2, 3, 4, 5, 6)

σ : dual

Returns the dual of the value. Given a type A that has an instance of the Semigroup type class, the type Dual[A] has a Semigroup instance that reverses the arguments to Semigroup.append. In other words it is the dual of a monoid as obtained by swapping the arguments of mappend.

Related : Dual

scala> List(1,2,3).dual |+| List(4,5,6).dual
res0: scalaz.Dual[List[Int]] = List(4, 5, 6, 1, 2, 3)

scala> "abc".dual |+| "def".dual
res1: scalaz.Dual[java.lang.String] = defabc

≟ : ===

Equality check between the two identities

Related : Equal

scala> "abc"  "abc"
res0: Boolean = true

scala> List[Int](1,2,3)  (List[Int](1,2)   List[Int](3))
res1: Boolean = true

scala> List[Int](1,2,3)  List[Int](1,2,4)
res2: Boolean = false

≠ : /==

Not equal operator

Related : Equal

??

Null check. Returns the value if non null, else returns a specified default.

Related : Option

scala> (null: String) ?? "default"
res0: String = default

scala> "abc" ?? "default"
res1: java.lang.String = abc

?|?

Ordering operator.

Related : Order

scala> 9 ?|? 8
res0: scalaz.Ordering = GT

scala> 9 ?|? 9
res1: scalaz.Ordering = EQ

scala> 9 ?|? 10
res2: scalaz.Ordering = LT

≤, ≥, ≨, ≩, ≮, ≯, ≰, ≱, lte, gte, lt, gt

Various comparison operators

Related : Order

min, max

min and max

Related : Order

scala> 9 min 5
res0: Int = 5

scala> 5 min 9
res1: Int = 5

scala> 7 max 12
res2: Int = 12

show, shows

show converts the value to a sequence of characters with interspersed spaces

shows converts the value into a printable string

An interesting characteristic is that as.show === as.shows.toList

Related : Show

scala> List(1,2,3).show
res0: List[Char] = List('[','1',',','2',',','3',']')

scala> "abc".show
res1: List[Char] = List(a, b, c)

scala> List(1,2,3).shows
res2: String = "[1,2,3]"

scala> "abc".shows
res3: String = abc

print, println

Output the value converted to a string via shows to a destination specifically the console. The return is a Unit since the primary purpose is only the side effects

Related : Show .. todo:: had to deliberately force quotes & single quotes to work around pygments syntax highlighting issues

scala> List(1,2,3).print
"[1,2,3]"

mapply

Applies the function (f: F[A => B]) over contents of a functor over type A F[A] to create a new functor of the same higher kind over type B F[B]

Related : Functor