0
# Standard Library Integration
1
2
Scalaz provides type class instances for Scala and Java standard library types, enabling functional programming patterns with familiar types.
3
4
## Capabilities
5
6
### Collection Instances
7
8
#### List Instances
9
10
Type class instances for `List[A]`.
11
12
```scala { .api }
13
// Available via import scalaz.std.list._ or import scalaz.Scalaz._
14
15
implicit def listMonad: Monad[List] with Traverse[List] with MonadPlus[List] with Alt[List] with BindRec[List] with Zip[List] with Unzip[List] with Align[List] with IsEmpty[List] with Cobind[List]
16
17
implicit def listEqual[A: Equal]: Equal[List[A]]
18
implicit def listOrder[A: Order]: Order[List[A]]
19
implicit def listShow[A: Show]: Show[List[A]]
20
implicit def listMonoid[A]: Monoid[List[A]]
21
```
22
23
**Usage Examples:**
24
25
```scala
26
import scalaz._
27
import scalaz.std.list._
28
import scalaz.syntax.monad._
29
30
// Monadic operations
31
val result = List(1, 2) >>= (x => List(x, x * 2))
32
// Result: List(1, 2, 2, 4)
33
34
// Traverse with effects
35
import scalaz.std.option._
36
import scalaz.syntax.traverse._
37
val validated = List(1, 2, 3).traverse(x => if (x > 0) Some(x) else None)
38
// Result: Some(List(1, 2, 3))
39
```
40
41
#### Vector Instances
42
43
Type class instances for `Vector[A]`.
44
45
```scala { .api }
46
// Available via import scalaz.std.vector._ or import scalaz.Scalaz._
47
48
implicit def vectorMonad: Monad[Vector] with Traverse[Vector] with MonadPlus[Vector] with Alt[Vector] with BindRec[Vector] with Zip[Vector] with Unzip[Vector] with Align[Vector] with IsEmpty[Vector] with Cobind[Vector]
49
50
implicit def vectorEqual[A: Equal]: Equal[Vector[A]]
51
implicit def vectorOrder[A: Order]: Order[Vector[A]]
52
implicit def vectorShow[A: Show]: Show[Vector[A]]
53
implicit def vectorMonoid[A]: Monoid[Vector[A]]
54
```
55
56
#### Stream Instances
57
58
Type class instances for `Stream[A]`.
59
60
```scala { .api }
61
// Available via import scalaz.std.stream._ or import scalaz.Scalaz._
62
63
implicit def streamMonad: Monad[Stream] with Traverse[Stream] with MonadPlus[Stream] with Alt[Stream] with BindRec[Stream] with Zip[Stream] with Unzip[Stream] with Align[Stream] with IsEmpty[Stream] with Cobind[Stream]
64
65
implicit def streamEqual[A: Equal]: Equal[Stream[A]]
66
implicit def streamOrder[A: Order]: Order[Stream[A]]
67
implicit def streamShow[A: Show]: Show[Stream[A]]
68
implicit def streamMonoid[A]: Monoid[Stream[A]]
69
```
70
71
#### Option Instances
72
73
Type class instances for `Option[A]`.
74
75
```scala { .api }
76
// Available via import scalaz.std.option._ or import scalaz.Scalaz._
77
78
implicit def optionMonad: Monad[Option] with Traverse[Option] with MonadPlus[Option] with Alt[Option] with BindRec[Option] with Zip[Option] with Unzip[Option] with Align[Option] with IsEmpty[Option] with Cobind[Option] with Optional[Option]
79
80
implicit def optionEqual[A: Equal]: Equal[Option[A]]
81
implicit def optionOrder[A: Order]: Order[Option[A]]
82
implicit def optionShow[A: Show]: Show[Option[A]]
83
implicit def optionMonoid[A: Semigroup]: Monoid[Option[A]]
84
```
85
86
**Usage Examples:**
87
88
```scala
89
import scalaz._
90
import scalaz.std.option._
91
import scalaz.syntax.applicative._
92
93
// Applicative operations
94
val result = (some(1) |@| some(2) |@| some(3))(_ + _ + _)
95
// Result: Some(6)
96
97
// MonadPlus operations
98
import scalaz.syntax.monadPlus._
99
val filtered = some(5).filter(_ > 10)
100
// Result: None
101
```
102
103
### Map Instances
104
105
#### Map Instances
106
107
Type class instances for `Map[K, V]`.
108
109
```scala { .api }
110
// Available via import scalaz.std.map._ or import scalaz.Scalaz._
111
112
implicit def mapMonad[K]: Monad[Map[K, ?]] with Traverse[Map[K, ?]] with BindRec[Map[K, ?]]
113
114
implicit def mapEqual[K, V](implicit K: Equal[K], V: Equal[V]): Equal[Map[K, V]]
115
implicit def mapShow[K, V](implicit K: Show[K], V: Show[V]): Show[Map[K, V]]
116
implicit def mapMonoid[K, V]: Monoid[Map[K, V]]
117
118
// Functor instance for second type parameter
119
implicit def mapFunctor[K]: Functor[Map[K, ?]]
120
```
121
122
#### Set Instances
123
124
Type class instances for `Set[A]`.
125
126
```scala { .api }
127
// Available via import scalaz.std.set._ or import scalaz.Scalaz._
128
129
implicit def setMonoid[A]: Monoid[Set[A]]
130
implicit def setBand[A]: Band[Set[A]]
131
implicit def setEqual[A]: Equal[Set[A]]
132
implicit def setShow[A: Show]: Show[Set[A]]
133
134
// Foldable instance
135
implicit def setFoldable: Foldable[Set]
136
```
137
138
### Either Instances
139
140
#### Either Instances
141
142
Type class instances for `Either[A, B]`.
143
144
```scala { .api }
145
// Available via import scalaz.std.either._ or import scalaz.Scalaz._
146
147
implicit def eitherMonad[L]: Monad[Either[L, ?]] with Traverse[Either[L, ?]] with BindRec[Either[L, ?]]
148
149
implicit def eitherEqual[A: Equal, B: Equal]: Equal[Either[A, B]]
150
implicit def eitherShow[A: Show, B: Show]: Show[Either[A, B]]
151
implicit def eitherOrder[A: Order, B: Order]: Order[Either[A, B]]
152
153
// Bifunctor and Bitraverse instances
154
implicit def eitherBifunctor: Bifunctor[Either]
155
implicit def eitherBitraverse: Bitraverse[Either]
156
```
157
158
### Function Instances
159
160
#### Function Instances
161
162
Type class instances for functions `A => B`.
163
164
```scala { .api }
165
// Available via import scalaz.std.function._ or import scalaz.Scalaz._
166
167
// Monad instance for Reader (A => ?)
168
implicit def function1Monad[R]: Monad[R => ?] with BindRec[R => ?]
169
170
// Arrow instances
171
implicit def function1Arrow: Arrow[Function1] with Choice[Function1]
172
173
// Category instance
174
implicit def function1Category: Category[Function1]
175
176
// Contravariant functor for input type
177
implicit def function1Contravariant[R]: Contravariant[? => R]
178
179
// Various other instances
180
implicit def function1Equal[A, B](implicit B: Equal[B]): Equal[A => B]
181
implicit def function1Show[A, B](implicit B: Show[B]): Show[A => B]
182
implicit def function1Monoid[A, B](implicit B: Monoid[B]): Monoid[A => B]
183
```
184
185
**Usage Examples:**
186
187
```scala
188
import scalaz._
189
import scalaz.std.function._
190
import scalaz.syntax.monad._
191
192
// Reader monad
193
val computation = for {
194
x <- ((_: Int) * 2)
195
y <- ((_: Int) + 10)
196
} yield x + y
197
198
val result = computation(5) // (5 * 2) + (5 + 10) = 25
199
```
200
201
### Tuple Instances
202
203
#### Tuple2 Instances
204
205
Type class instances for `(A, B)`.
206
207
```scala { .api }
208
// Available via import scalaz.std.tuple._ or import scalaz.Scalaz._
209
210
// Monad instance for second element
211
implicit def tuple2Monad[A: Monoid]: Monad[(A, ?)] with Comonad[(A, ?)]
212
213
// Bifunctor and Bitraverse
214
implicit def tuple2Bifunctor: Bifunctor[Tuple2]
215
implicit def tuple2Bitraverse: Bitraverse[Tuple2]
216
217
// Equal, Order, Show instances
218
implicit def tuple2Equal[A: Equal, B: Equal]: Equal[(A, B)]
219
implicit def tuple2Order[A: Order, B: Order]: Order[(A, B)]
220
implicit def tuple2Show[A: Show, B: Show]: Show[(A, B)]
221
222
// Monoid instance
223
implicit def tuple2Monoid[A: Monoid, B: Monoid]: Monoid[(A, B)]
224
```
225
226
### Primitive Type Instances
227
228
#### Numeric Type Instances
229
230
Type class instances for numeric types.
231
232
```scala { .api }
233
// Available via import scalaz.std.anyVal._ or import scalaz.Scalaz._
234
235
// Int instances
236
implicit def intEqual: Equal[Int]
237
implicit def intOrder: Order[Int]
238
implicit def intShow: Show[Int]
239
implicit def intEnum: Enum[Int]
240
implicit def intAdditionMonoid: Monoid[Int @@ Tags.Addition]
241
implicit def intMultiplicationMonoid: Monoid[Int @@ Tags.Multiplication]
242
243
// Long instances
244
implicit def longEqual: Equal[Long]
245
implicit def longOrder: Order[Long]
246
implicit def longShow: Show[Long]
247
implicit def longEnum: Enum[Long]
248
249
// Double instances
250
implicit def doubleEqual: Equal[Double]
251
implicit def doubleOrder: Order[Double]
252
implicit def doubleShow: Show[Double]
253
254
// Float instances
255
implicit def floatEqual: Equal[Float]
256
implicit def floatOrder: Order[Float]
257
implicit def floatShow: Show[Float]
258
259
// Short instances
260
implicit def shortEqual: Equal[Short]
261
implicit def shortOrder: Order[Short]
262
implicit def shortShow: Show[Short]
263
264
// Byte instances
265
implicit def byteEqual: Equal[Byte]
266
implicit def byteOrder: Order[Byte]
267
implicit def byteShow: Show[Byte]
268
269
// Char instances
270
implicit def charEqual: Equal[Char]
271
implicit def charOrder: Order[Char]
272
implicit def charShow: Show[Char]
273
implicit def charEnum: Enum[Char]
274
```
275
276
#### Boolean Instances
277
278
Type class instances for `Boolean`.
279
280
```scala { .api }
281
// Available via import scalaz.std.anyVal._ or import scalaz.Scalaz._
282
283
implicit def booleanEqual: Equal[Boolean]
284
implicit def booleanOrder: Order[Boolean]
285
implicit def booleanShow: Show[Boolean]
286
implicit def booleanEnum: Enum[Boolean]
287
288
// Boolean algebra instances
289
implicit def booleanConjunctionMonoid: Monoid[Boolean @@ Tags.Conjunction]
290
implicit def booleanDisjunctionMonoid: Monoid[Boolean @@ Tags.Disjunction]
291
```
292
293
#### Unit Instances
294
295
Type class instances for `Unit`.
296
297
```scala { .api }
298
// Available via import scalaz.std.anyVal._ or import scalaz.Scalaz._
299
300
implicit def unitEqual: Equal[Unit]
301
implicit def unitOrder: Order[Unit]
302
implicit def unitShow: Show[Unit]
303
implicit def unitMonoid: Monoid[Unit]
304
```
305
306
### String Instances
307
308
#### String Instances
309
310
Type class instances for `String`.
311
312
```scala { .api }
313
// Available via import scalaz.std.string._ or import scalaz.Scalaz._
314
315
implicit def stringEqual: Equal[String]
316
implicit def stringOrder: Order[String]
317
implicit def stringShow: Show[String]
318
implicit def stringMonoid: Monoid[String]
319
320
// String as a list of characters
321
implicit def stringFoldable: Foldable[String]
322
```
323
324
**Usage Examples:**
325
326
```scala
327
import scalaz._
328
import scalaz.std.string._
329
import scalaz.syntax.monoid._
330
331
val combined = "Hello" |+| " " |+| "World"
332
// Result: "Hello World"
333
```
334
335
### Java Interop Instances
336
337
#### Java Collections
338
339
Type class instances for Java collection types.
340
341
```scala { .api }
342
// Available via import scalaz.std.java.util._ or import scalaz.Scalaz._
343
344
// ArrayList instances
345
implicit def arrayListMonad: Monad[ArrayList] with Traverse[ArrayList] with MonadPlus[ArrayList]
346
implicit def arrayListEqual[A: Equal]: Equal[ArrayList[A]]
347
348
// HashMap instances
349
implicit def hashMapEqual[K: Equal, V: Equal]: Equal[HashMap[K, V]]
350
implicit def hashMapMonoid[K, V]: Monoid[HashMap[K, V]]
351
352
// HashSet instances
353
implicit def hashSetEqual[A: Equal]: Equal[HashSet[A]]
354
implicit def hashSetMonoid[A]: Monoid[HashSet[A]]
355
```
356
357
#### Java Time (JVM only)
358
359
Type class instances for Java 8 time types.
360
361
```scala { .api }
362
// Available via import scalaz.std.java.time._ (JVM only)
363
364
implicit def instantEqual: Equal[Instant]
365
implicit def instantOrder: Order[Instant]
366
implicit def instantShow: Show[Instant]
367
368
implicit def durationEqual: Equal[Duration]
369
implicit def durationOrder: Order[Duration]
370
implicit def durationShow: Show[Duration]
371
implicit def durationMonoid: Monoid[Duration]
372
373
implicit def localDateEqual: Equal[LocalDate]
374
implicit def localDateOrder: Order[LocalDate]
375
implicit def localDateShow: Show[LocalDate]
376
377
implicit def localTimeEqual: Equal[LocalTime]
378
implicit def localTimeOrder: Order[LocalTime]
379
implicit def localTimeShow: Show[LocalTime]
380
381
implicit def localDateTimeEqual: Equal[LocalDateTime]
382
implicit def localDateTimeOrder: Order[LocalDateTime]
383
implicit def localDateTimeShow: Show[LocalDateTime]
384
```
385
386
### Try Instances
387
388
#### Try Instances
389
390
Type class instances for `scala.util.Try[A]`.
391
392
```scala { .api }
393
// Available via import scalaz.std.scalaUtil._ or import scalaz.Scalaz._
394
395
implicit def tryMonad: Monad[Try] with BindRec[Try] with MonadError[Try, Throwable] with Traverse[Try] with Cozip[Try]
396
397
implicit def tryEqual[A: Equal]: Equal[Try[A]]
398
implicit def tryShow[A: Show]: Show[Try[A]]
399
```
400
401
### Future Instances (Platform-specific)
402
403
#### Future Instances
404
405
Type class instances for `scala.concurrent.Future[A]`.
406
407
```scala { .api }
408
// Available via import scalaz.std.scalaFuture._ (JVM and some JS environments)
409
// Requires implicit ExecutionContext
410
411
implicit def futureMonad(implicit ec: ExecutionContext): Monad[Future] with BindRec[Future] with Nondeterminism[Future] with MonadError[Future, Throwable]
412
413
implicit def futureEqual[A: Equal](implicit ec: ExecutionContext, duration: Duration = Duration.Inf): Equal[Future[A]]
414
implicit def futureShow[A: Show](implicit ec: ExecutionContext, duration: Duration = Duration.Inf): Show[Future[A]]
415
```
416
417
### Import Strategies
418
419
#### Selective Imports
420
421
```scala { .api }
422
// Import specific type instances
423
import scalaz.std.option._ // Option instances only
424
import scalaz.std.list._ // List instances only
425
import scalaz.std.string._ // String instances only
426
427
// Import categories
428
import scalaz.std.anyVal._ // All primitive type instances
429
import scalaz.std.java.util._ // Java collection instances
430
```
431
432
#### Complete Import
433
434
```scala { .api }
435
// Import everything
436
import scalaz._
437
import Scalaz._ // Includes all std instances and syntax
438
439
// Or import all std instances
440
import scalaz.std.AllInstances._
441
// Or all std functions
442
import scalaz.std.AllFunctions._
443
```
444
445
**Usage Examples:**
446
447
```scala
448
import scalaz._
449
import Scalaz._
450
451
// All instances and syntax available
452
val result1 = List(1, 2, 3).map(_ * 2) // Functor
453
val result2 = (some(1) |@| some(2))(_ + _) // Applicative
454
val result3 = List(1, 2).flatMap(x => List(x, x * 2)) // Monad
455
val result4 = "Hello" |+| " " |+| "World" // Monoid
456
val result5 = 1 === 1 // Equal
457
val result6 = 5 > 3 // Order
458
459
// Type-safe equality and ordering
460
import scalaz.syntax.equal._
461
import scalaz.syntax.order._
462
val equal = 1 === 1 // Uses Equal[Int]
463
val greater = 5 ?|? 3 // Uses Order[Int], returns Ordering.GT
464
```