@zio.dev/zio-blocks 0.0.1 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.md +143 -15
- package/package.json +5 -2
- package/reference/binding.md +8 -14
- package/reference/chunk.md +36 -36
- package/reference/docs.md +2 -2
- package/reference/dynamic-value.md +34 -34
- package/reference/formats.md +16 -16
- package/reference/json-schema.md +20 -20
- package/reference/json.md +47 -47
- package/reference/optics.md +51 -37
- package/reference/reflect.md +3 -3
- package/reference/registers.md +7 -7
- package/reference/schema.md +18 -18
- package/reference/syntax.md +16 -16
- package/scope.md +481 -161
- package/reference/modifier.md +0 -276
- package/reference/reflect-transform.md +0 -387
- package/reference/type-class-derivation-internals.md +0 -632
package/index.md
CHANGED
|
@@ -5,7 +5,7 @@ title: "ZIO Blocks"
|
|
|
5
5
|
|
|
6
6
|
**Modular, zero-dependency building blocks for modern Scala applications.**
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
[](https://github.com/zio/zio/wiki/Project-Stages)  [](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-blocks-next-schema_3/) [](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-blocks-next-schema_3/) [](https://javadoc.io/doc/dev.zio/zio-blocks-docs_3) [](https://github.com/zio/zio-blocks)
|
|
9
9
|
|
|
10
10
|
## What Is ZIO Blocks?
|
|
11
11
|
|
|
@@ -19,6 +19,7 @@ The philosophy is simple: **use what you need, nothing more**. Each block is ind
|
|
|
19
19
|
|-------|-------------|--------|
|
|
20
20
|
| **Schema** | Type-safe schemas with automatic codec derivation | ✅ Available |
|
|
21
21
|
| **Chunk** | High-performance immutable indexed sequences | ✅ Available |
|
|
22
|
+
| **Scope** | Compile-time safe resource management and DI | ✅ Available |
|
|
22
23
|
| **Docs** | GitHub Flavored Markdown parsing and rendering | ✅ Available |
|
|
23
24
|
| **TypeId** | Compile-time type identity with rich metadata | ✅ Available |
|
|
24
25
|
| **Context** | Type-indexed heterogeneous collections | ✅ Available |
|
|
@@ -79,14 +80,14 @@ val thriftCodec = Schema[Person].derive(ThriftFormat) // Thrift
|
|
|
79
80
|
### Installation
|
|
80
81
|
|
|
81
82
|
```scala
|
|
82
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-schema" % "
|
|
83
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-schema" % "0.0.21"
|
|
83
84
|
|
|
84
85
|
// Optional format modules:
|
|
85
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-schema-avro" % "
|
|
86
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-schema-toon" % "
|
|
87
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-schema-messagepack" % "
|
|
88
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-schema-thrift" % "
|
|
89
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-schema-bson" % "
|
|
86
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-schema-avro" % "0.0.21"
|
|
87
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-schema-toon" % "0.0.21"
|
|
88
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-schema-messagepack" % "0.0.21"
|
|
89
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-schema-thrift" % "0.0.21"
|
|
90
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-schema-bson" % "0.0.21"
|
|
90
91
|
```
|
|
91
92
|
|
|
92
93
|
### Example: Optics
|
|
@@ -141,7 +142,7 @@ Chunk is designed for:
|
|
|
141
142
|
### Installation
|
|
142
143
|
|
|
143
144
|
```scala
|
|
144
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-chunk" % "
|
|
145
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-chunk" % "0.0.21"
|
|
145
146
|
```
|
|
146
147
|
|
|
147
148
|
### Example
|
|
@@ -170,6 +171,132 @@ val head: Int = nonEmpty.head // Always safe, no Option needed
|
|
|
170
171
|
|
|
171
172
|
---
|
|
172
173
|
|
|
174
|
+
## Scope
|
|
175
|
+
|
|
176
|
+
Compile-time verified resource safety for synchronous Scala code. Scope prevents resource leaks at compile time by tagging values with an unnameable type-level identity—values allocated in a scope can only be used within that scope, and child scope values cannot escape to parent scopes.
|
|
177
|
+
|
|
178
|
+
### The Problem
|
|
179
|
+
|
|
180
|
+
Resource management in Scala is error-prone:
|
|
181
|
+
|
|
182
|
+
```scala
|
|
183
|
+
// Classic try/finally - verbose and easy to get wrong
|
|
184
|
+
val db = openDatabase()
|
|
185
|
+
try {
|
|
186
|
+
val tx = db.beginTransaction()
|
|
187
|
+
try {
|
|
188
|
+
doWork(tx)
|
|
189
|
+
tx.commit()
|
|
190
|
+
} finally tx.close() // What if commit() throws?
|
|
191
|
+
} finally db.close()
|
|
192
|
+
|
|
193
|
+
// Using - better, but doesn't prevent returning resources
|
|
194
|
+
Using(openDatabase()) { db =>
|
|
195
|
+
db // Oops! Returned the resource - use after close!
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### The Solution
|
|
200
|
+
|
|
201
|
+
Scope makes resource leaks a **compile error**, not a runtime bug:
|
|
202
|
+
|
|
203
|
+
```scala
|
|
204
|
+
import zio.blocks.scope._
|
|
205
|
+
|
|
206
|
+
Scope.global.scoped { scope =>
|
|
207
|
+
val db: Database @@ scope.Tag = scope.allocate(Resource(openDatabase()))
|
|
208
|
+
|
|
209
|
+
// Methods are hidden - can't call db.query() directly
|
|
210
|
+
// Must use scope $ to access:
|
|
211
|
+
val result = (scope $ db)(_.query("SELECT 1"))
|
|
212
|
+
|
|
213
|
+
// Trying to return `db` would be a compile error!
|
|
214
|
+
result // Only pure data escapes
|
|
215
|
+
}
|
|
216
|
+
// db.close() called automatically
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Key Features
|
|
220
|
+
|
|
221
|
+
- **Compile-Time Leak Prevention**: Values tagged with `A @@ S` can only be used with proof of scope access. Returning a scoped value from its scope is a type error.
|
|
222
|
+
- **Zero Runtime Overhead**: On the eager path the `@@` tag is erased—`A @@ S` is represented as just `A` when evaluated—while deferred/composed computations use a small wrapper/thunk.
|
|
223
|
+
- **Structured Scopes**: Child scopes nest within parents; resources clean up LIFO when scopes exit.
|
|
224
|
+
- **Built-in Dependency Injection**: Wire up your application with `Resource.from[T](wires*)` for automatic constructor-based DI.
|
|
225
|
+
- **AutoCloseable Integration**: Resources implementing `AutoCloseable` have `close()` registered automatically.
|
|
226
|
+
|
|
227
|
+
### Installation
|
|
228
|
+
|
|
229
|
+
```scala
|
|
230
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-scope" % "0.0.21"
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Example: Basic Resource Management
|
|
234
|
+
|
|
235
|
+
```scala
|
|
236
|
+
import zio.blocks.scope._
|
|
237
|
+
|
|
238
|
+
final class Database extends AutoCloseable {
|
|
239
|
+
def query(sql: String): String = s"Result: $sql"
|
|
240
|
+
def close(): Unit = println("Database closed")
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
Scope.global.scoped { scope =>
|
|
244
|
+
// Allocate returns Database @@ scope.Tag (scoped value)
|
|
245
|
+
val db = scope.allocate(Resource(new Database))
|
|
246
|
+
|
|
247
|
+
// Access via scope $ - result (String) escapes, db does not
|
|
248
|
+
val result = (scope $ db)(_.query("SELECT * FROM users"))
|
|
249
|
+
println(result)
|
|
250
|
+
}
|
|
251
|
+
// Output: Result: SELECT * FROM users
|
|
252
|
+
// Database closed
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Example: Dependency Injection
|
|
256
|
+
|
|
257
|
+
```scala
|
|
258
|
+
import zio.blocks.scope._
|
|
259
|
+
|
|
260
|
+
case class Config(dbUrl: String)
|
|
261
|
+
class Database(config: Config) extends AutoCloseable { ... }
|
|
262
|
+
class UserRepo(db: Database) { ... }
|
|
263
|
+
class UserService(repo: UserRepo) extends AutoCloseable { ... }
|
|
264
|
+
|
|
265
|
+
// Resource.from auto-wires the dependency graph
|
|
266
|
+
// Only provide leaf values - concrete classes are auto-wired
|
|
267
|
+
val serviceResource: Resource[UserService] = Resource.from[UserService](
|
|
268
|
+
Wire(Config("jdbc:postgresql://localhost/mydb"))
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
Scope.global.scoped { scope =>
|
|
272
|
+
val service = scope.allocate(serviceResource)
|
|
273
|
+
(scope $ service)(_.createUser("Alice"))
|
|
274
|
+
}
|
|
275
|
+
// Cleanup runs LIFO: UserService → Database (UserRepo has no cleanup)
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Example: Nested Scopes with Transactions
|
|
279
|
+
|
|
280
|
+
```scala
|
|
281
|
+
Scope.global.scoped { connScope =>
|
|
282
|
+
val conn = connScope.allocate(Resource.fromAutoCloseable(new Connection))
|
|
283
|
+
|
|
284
|
+
// Transaction lives in child scope - cleaned up before connection
|
|
285
|
+
val result = connScope.scoped { txScope =>
|
|
286
|
+
val tx = txScope.allocate(conn.beginTransaction()) // Returns Resource!
|
|
287
|
+
(txScope $ tx)(_.execute("INSERT INTO users VALUES (1, 'Alice')"))
|
|
288
|
+
(txScope $ tx)(_.commit())
|
|
289
|
+
"success"
|
|
290
|
+
}
|
|
291
|
+
// Transaction closed here, connection still open
|
|
292
|
+
|
|
293
|
+
println(result)
|
|
294
|
+
}
|
|
295
|
+
// Connection closed here
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
173
300
|
## Docs
|
|
174
301
|
|
|
175
302
|
A zero-dependency GitHub Flavored Markdown library for parsing, rendering, and programmatic construction of Markdown documents.
|
|
@@ -194,7 +321,7 @@ Generating documentation, README files, or any Markdown content programmatically
|
|
|
194
321
|
### Installation
|
|
195
322
|
|
|
196
323
|
```scala
|
|
197
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-docs" % "
|
|
324
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-docs" % "0.0.21"
|
|
198
325
|
```
|
|
199
326
|
|
|
200
327
|
### Example
|
|
@@ -278,7 +405,7 @@ Compile-time type identity with rich metadata. TypeId captures comprehensive inf
|
|
|
278
405
|
### Installation
|
|
279
406
|
|
|
280
407
|
```scala
|
|
281
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-typeid" % "
|
|
408
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-typeid" % "0.0.21"
|
|
282
409
|
```
|
|
283
410
|
|
|
284
411
|
### Example
|
|
@@ -321,7 +448,7 @@ A type-indexed heterogeneous collection that stores values by their types with c
|
|
|
321
448
|
### Installation
|
|
322
449
|
|
|
323
450
|
```scala
|
|
324
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-context" % "
|
|
451
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-context" % "0.0.21"
|
|
325
452
|
```
|
|
326
453
|
|
|
327
454
|
### Example
|
|
@@ -385,10 +512,10 @@ Each block has zero dependencies on effect systems. Use the blocks directly, or
|
|
|
385
512
|
|
|
386
513
|
ZIO Blocks supports **Scala 2.13** and **Scala 3.x** with full source compatibility. Write your code once and compile it against either version—migrate to Scala 3 when your team is ready, not when your dependencies force you.
|
|
387
514
|
|
|
388
|
-
| Platform | Schema | Chunk | Docs | TypeId | Context | Streams |
|
|
389
|
-
|
|
390
|
-
| JVM | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
391
|
-
| Scala.js | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
515
|
+
| Platform | Schema | Chunk | Scope | Docs | TypeId | Context | Streams |
|
|
516
|
+
|----------|--------|-------|-------|------|--------|---------|---------|
|
|
517
|
+
| JVM | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
518
|
+
| Scala.js | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
392
519
|
|
|
393
520
|
## Documentation
|
|
394
521
|
|
|
@@ -421,6 +548,7 @@ ZIO Blocks supports **Scala 2.13** and **Scala 3.x** with full source compatibil
|
|
|
421
548
|
### Other Blocks
|
|
422
549
|
|
|
423
550
|
- [Chunk](./reference/chunk.md) - High-performance immutable sequences
|
|
551
|
+
- [Scope](./scope.md) - Compile-time safe resource management and DI
|
|
424
552
|
- [TypeId](./reference/typeid.md) - Type identity and metadata
|
|
425
553
|
- [Context](./reference/context.md) - Type-indexed heterogeneous collections
|
|
426
554
|
- [Docs (Markdown)](./reference/docs.md) - Markdown parsing and rendering
|
package/package.json
CHANGED
package/reference/binding.md
CHANGED
|
@@ -66,7 +66,7 @@ final case class Record[A](
|
|
|
66
66
|
|
|
67
67
|
Here is an example of a `Binding.Record` for a simple `Person` case class:
|
|
68
68
|
|
|
69
|
-
```scala
|
|
69
|
+
```scala
|
|
70
70
|
import zio.blocks.schema.binding._
|
|
71
71
|
import zio.blocks.schema.binding.RegisterOffset._
|
|
72
72
|
|
|
@@ -115,13 +115,11 @@ final case class Variant[A](
|
|
|
115
115
|
- `Discriminator`: Given a value of a sum type, determine which case it belongs to by returning a numerical index.
|
|
116
116
|
- `Matchers`: Given a value and a case index, safely downcast the value to the specific case type, or return null if it doesn't match.
|
|
117
117
|
|
|
118
|
-
```scala
|
|
118
|
+
```scala
|
|
119
119
|
import zio.blocks.schema.binding._
|
|
120
120
|
|
|
121
121
|
sealed trait Shape extends Product with Serializable
|
|
122
|
-
|
|
123
122
|
case class Circle(radius: Double) extends Shape
|
|
124
|
-
|
|
125
123
|
case class Rectangle(width: Double, height: Double) extends Shape
|
|
126
124
|
|
|
127
125
|
val shapeBinding = Binding.Variant[Shape](
|
|
@@ -302,25 +300,21 @@ final case class Wrapper[A, B](
|
|
|
302
300
|
|
|
303
301
|
**Components:**
|
|
304
302
|
|
|
305
|
-
- `wrap`: Converts from the underlying type `B` to the wrapper type `A
|
|
306
|
-
- `unwrap`: Extracts the underlying `B` from an `A
|
|
303
|
+
- `wrap`: Converts from the underlying type `B` to the wrapper type `A`
|
|
304
|
+
- `unwrap`: Extracts the underlying `B` from an `A`
|
|
307
305
|
|
|
308
|
-
Here is an example of a `Binding.Wrapper` for an `Email` newtype
|
|
306
|
+
Here is an example of a `Binding.Wrapper` for an `Email` newtype:
|
|
309
307
|
|
|
310
|
-
```scala
|
|
308
|
+
```scala
|
|
311
309
|
import zio.blocks.schema._
|
|
312
310
|
import zio.blocks.schema.binding._
|
|
313
311
|
|
|
314
312
|
case class Email(value: String)
|
|
315
313
|
|
|
316
314
|
object Email {
|
|
317
|
-
private val EmailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$".r
|
|
318
315
|
new Binding.Wrapper[Email, String](
|
|
319
|
-
wrap =
|
|
320
|
-
|
|
321
|
-
case _ => Left(SchemaError.validationFailed("Expected valid email format"))
|
|
322
|
-
},
|
|
323
|
-
email => Right(email.value)
|
|
316
|
+
wrap = value => new Email(value),
|
|
317
|
+
unwrap = email => email.value
|
|
324
318
|
)
|
|
325
319
|
}
|
|
326
320
|
```
|
package/reference/chunk.md
CHANGED
|
@@ -46,7 +46,7 @@ Supported Scala versions: 2.13.x and 3.x
|
|
|
46
46
|
|
|
47
47
|
### From Varargs
|
|
48
48
|
|
|
49
|
-
```scala
|
|
49
|
+
```scala
|
|
50
50
|
import zio.blocks.chunk.Chunk
|
|
51
51
|
|
|
52
52
|
val numbers = Chunk(1, 2, 3, 4, 5)
|
|
@@ -56,7 +56,7 @@ val empty = Chunk.empty[Int]
|
|
|
56
56
|
|
|
57
57
|
### From a Single Element
|
|
58
58
|
|
|
59
|
-
```scala
|
|
59
|
+
```scala
|
|
60
60
|
import zio.blocks.chunk.Chunk
|
|
61
61
|
|
|
62
62
|
val single = Chunk.single(42)
|
|
@@ -67,7 +67,7 @@ val unit = Chunk.unit // Chunk(())
|
|
|
67
67
|
|
|
68
68
|
When you have an existing array, use `fromArray`. Note that the array should not be mutated after wrapping:
|
|
69
69
|
|
|
70
|
-
```scala
|
|
70
|
+
```scala
|
|
71
71
|
import zio.blocks.chunk.Chunk
|
|
72
72
|
|
|
73
73
|
val arr = Array(1, 2, 3)
|
|
@@ -76,7 +76,7 @@ val chunk = Chunk.fromArray(arr)
|
|
|
76
76
|
|
|
77
77
|
### From Iterables and Iterators
|
|
78
78
|
|
|
79
|
-
```scala
|
|
79
|
+
```scala
|
|
80
80
|
import zio.blocks.chunk.Chunk
|
|
81
81
|
|
|
82
82
|
val fromList = Chunk.fromIterable(List(1, 2, 3))
|
|
@@ -86,7 +86,7 @@ val fromIter = Chunk.fromIterator(Iterator.range(0, 10))
|
|
|
86
86
|
|
|
87
87
|
### From Java Collections
|
|
88
88
|
|
|
89
|
-
```scala
|
|
89
|
+
```scala
|
|
90
90
|
import zio.blocks.chunk.Chunk
|
|
91
91
|
import java.util
|
|
92
92
|
|
|
@@ -101,7 +101,7 @@ val chunk = Chunk.fromJavaIterable(javaList)
|
|
|
101
101
|
|
|
102
102
|
Chunk provides direct integration with Java NIO buffers:
|
|
103
103
|
|
|
104
|
-
```scala
|
|
104
|
+
```scala
|
|
105
105
|
import zio.blocks.chunk.Chunk
|
|
106
106
|
import java.nio.ByteBuffer
|
|
107
107
|
|
|
@@ -120,7 +120,7 @@ Available buffer constructors:
|
|
|
120
120
|
|
|
121
121
|
### Generator Functions
|
|
122
122
|
|
|
123
|
-
```scala
|
|
123
|
+
```scala
|
|
124
124
|
import zio.blocks.chunk.Chunk
|
|
125
125
|
|
|
126
126
|
val filled = Chunk.fill(5)("x") // Chunk("x", "x", "x", "x", "x")
|
|
@@ -132,7 +132,7 @@ val unfolded = Chunk.unfold(0)(n => if (n < 5) Some((n, n + 1)) else None)
|
|
|
132
132
|
|
|
133
133
|
### Element Access
|
|
134
134
|
|
|
135
|
-
```scala
|
|
135
|
+
```scala
|
|
136
136
|
import zio.blocks.chunk.Chunk
|
|
137
137
|
|
|
138
138
|
val chunk = Chunk(10, 20, 30, 40, 50)
|
|
@@ -149,7 +149,7 @@ val maybeLast = chunk.lastOption // Some(50)
|
|
|
149
149
|
|
|
150
150
|
For primitive chunks, specialized accessors avoid boxing:
|
|
151
151
|
|
|
152
|
-
```scala
|
|
152
|
+
```scala
|
|
153
153
|
import zio.blocks.chunk.Chunk
|
|
154
154
|
|
|
155
155
|
val ints = Chunk(1, 2, 3)
|
|
@@ -164,7 +164,7 @@ val d: Double = doubles.double(0) // unboxed access
|
|
|
164
164
|
|
|
165
165
|
### Transformations
|
|
166
166
|
|
|
167
|
-
```scala
|
|
167
|
+
```scala
|
|
168
168
|
import zio.blocks.chunk.Chunk
|
|
169
169
|
|
|
170
170
|
val chunk = Chunk(1, 2, 3, 4, 5)
|
|
@@ -179,7 +179,7 @@ val collected = chunk.collect { case n if n % 2 == 0 => n * 10 } // Chunk(20, 40
|
|
|
179
179
|
|
|
180
180
|
Concatenation is efficient—Chunk uses balanced tree structures to avoid copying:
|
|
181
181
|
|
|
182
|
-
```scala
|
|
182
|
+
```scala
|
|
183
183
|
import zio.blocks.chunk.Chunk
|
|
184
184
|
|
|
185
185
|
val a = Chunk(1, 2, 3)
|
|
@@ -194,7 +194,7 @@ val prepended = 0 +: a // Chunk(0, 1, 2, 3)
|
|
|
194
194
|
|
|
195
195
|
Slicing operations create views and don't copy data:
|
|
196
196
|
|
|
197
|
-
```scala
|
|
197
|
+
```scala
|
|
198
198
|
import zio.blocks.chunk.Chunk
|
|
199
199
|
|
|
200
200
|
val chunk = Chunk(1, 2, 3, 4, 5, 6, 7, 8)
|
|
@@ -209,7 +209,7 @@ val (left, right) = chunk.splitAt(4) // (Chunk(1,2,3,4), Chunk(5,6,7,8))
|
|
|
209
209
|
|
|
210
210
|
### Conditional Operations
|
|
211
211
|
|
|
212
|
-
```scala
|
|
212
|
+
```scala
|
|
213
213
|
import zio.blocks.chunk.Chunk
|
|
214
214
|
|
|
215
215
|
val chunk = Chunk(1, 2, 3, 4, 5, 6)
|
|
@@ -222,7 +222,7 @@ val dropUntilBig = chunk.dropUntil(_ > 3) // Chunk(5, 6)
|
|
|
222
222
|
|
|
223
223
|
### Folding and Reduction
|
|
224
224
|
|
|
225
|
-
```scala
|
|
225
|
+
```scala
|
|
226
226
|
import zio.blocks.chunk.Chunk
|
|
227
227
|
|
|
228
228
|
val chunk = Chunk(1, 2, 3, 4, 5)
|
|
@@ -236,7 +236,7 @@ val runningSum = chunk.foldWhile(0)(_ < 10)(_ + _) // 10 (1+2+3+4)
|
|
|
236
236
|
|
|
237
237
|
### Searching and Predicates
|
|
238
238
|
|
|
239
|
-
```scala
|
|
239
|
+
```scala
|
|
240
240
|
import zio.blocks.chunk.Chunk
|
|
241
241
|
|
|
242
242
|
val chunk = Chunk(1, 2, 3, 4, 5)
|
|
@@ -249,7 +249,7 @@ val index = chunk.indexWhere(_ > 3) // 3
|
|
|
249
249
|
|
|
250
250
|
### Zipping
|
|
251
251
|
|
|
252
|
-
```scala
|
|
252
|
+
```scala
|
|
253
253
|
import zio.blocks.chunk.Chunk
|
|
254
254
|
|
|
255
255
|
val as = Chunk("a", "b", "c")
|
|
@@ -265,7 +265,7 @@ val zipAll = as.zipAll(Chunk(1, 2)) // handles different lengths
|
|
|
265
265
|
|
|
266
266
|
Updates are immutable and use efficient buffering:
|
|
267
267
|
|
|
268
|
-
```scala
|
|
268
|
+
```scala
|
|
269
269
|
import zio.blocks.chunk.Chunk
|
|
270
270
|
|
|
271
271
|
val chunk = Chunk(1, 2, 3, 4, 5)
|
|
@@ -274,7 +274,7 @@ val updated = chunk.updated(2, 100) // Chunk(1, 2, 100, 4, 5)
|
|
|
274
274
|
|
|
275
275
|
### Deduplication and Sorting
|
|
276
276
|
|
|
277
|
-
```scala
|
|
277
|
+
```scala
|
|
278
278
|
import zio.blocks.chunk.Chunk
|
|
279
279
|
|
|
280
280
|
val withDupes = Chunk(1, 1, 2, 2, 2, 3, 3)
|
|
@@ -286,7 +286,7 @@ val sorted = unsorted.sorted // Chunk(1, 1, 3, 4, 5)
|
|
|
286
286
|
|
|
287
287
|
### Splitting
|
|
288
288
|
|
|
289
|
-
```scala
|
|
289
|
+
```scala
|
|
290
290
|
import zio.blocks.chunk.Chunk
|
|
291
291
|
|
|
292
292
|
val chunk = Chunk(1, 2, 3, 4, 5, 6)
|
|
@@ -297,7 +297,7 @@ val (before, after) = chunk.splitWhere(_ > 3) // splits at first element > 3
|
|
|
297
297
|
|
|
298
298
|
### String Conversion
|
|
299
299
|
|
|
300
|
-
```scala
|
|
300
|
+
```scala
|
|
301
301
|
import zio.blocks.chunk.Chunk
|
|
302
302
|
import java.nio.charset.StandardCharsets
|
|
303
303
|
|
|
@@ -315,7 +315,7 @@ val base64 = bytes.asBase64String // base64-encoded string
|
|
|
315
315
|
|
|
316
316
|
For complex operation chains, you can force materialization to an array-backed chunk:
|
|
317
317
|
|
|
318
|
-
```scala
|
|
318
|
+
```scala
|
|
319
319
|
import zio.blocks.chunk.Chunk
|
|
320
320
|
|
|
321
321
|
val complex = Chunk(1, 2, 3) ++ Chunk(4, 5) ++ Chunk(6, 7)
|
|
@@ -326,7 +326,7 @@ val materialized = complex.materialize // backed by a single array
|
|
|
326
326
|
|
|
327
327
|
`NonEmptyChunk[A]` is a chunk guaranteed to contain at least one element. This enables safe use of operations like `head` and `reduce`:
|
|
328
328
|
|
|
329
|
-
```scala
|
|
329
|
+
```scala
|
|
330
330
|
import zio.blocks.chunk.{Chunk, NonEmptyChunk}
|
|
331
331
|
|
|
332
332
|
val nec = NonEmptyChunk(1, 2, 3)
|
|
@@ -340,7 +340,7 @@ val flatMapped: NonEmptyChunk[Int] = nec.flatMap(n => NonEmptyChunk(n, n + 1))
|
|
|
340
340
|
|
|
341
341
|
### Creating NonEmptyChunk
|
|
342
342
|
|
|
343
|
-
```scala
|
|
343
|
+
```scala
|
|
344
344
|
import zio.blocks.chunk.{Chunk, NonEmptyChunk}
|
|
345
345
|
|
|
346
346
|
val fromValues = NonEmptyChunk(1, 2, 3)
|
|
@@ -354,7 +354,7 @@ val empty: Option[NonEmptyChunk[Int]] = NonEmptyChunk.fromChunk(Chunk.empty) //
|
|
|
354
354
|
|
|
355
355
|
### Converting Between Chunk and NonEmptyChunk
|
|
356
356
|
|
|
357
|
-
```scala
|
|
357
|
+
```scala
|
|
358
358
|
import zio.blocks.chunk.{Chunk, NonEmptyChunk}
|
|
359
359
|
|
|
360
360
|
val nec = NonEmptyChunk(1, 2, 3)
|
|
@@ -382,7 +382,7 @@ Operations that might produce empty results return `Chunk`:
|
|
|
382
382
|
|
|
383
383
|
`ChunkBuilder` is a mutable builder for creating chunks efficiently. It's specialized for primitives to avoid boxing:
|
|
384
384
|
|
|
385
|
-
```scala
|
|
385
|
+
```scala
|
|
386
386
|
import zio.blocks.chunk.{Chunk, ChunkBuilder}
|
|
387
387
|
|
|
388
388
|
val builder = ChunkBuilder.make[Int]()
|
|
@@ -396,7 +396,7 @@ val result: Chunk[Int] = builder.result() // Chunk(1, 2, 3, 4, 5)
|
|
|
396
396
|
|
|
397
397
|
For primitives, use specialized builders for best performance:
|
|
398
398
|
|
|
399
|
-
```scala
|
|
399
|
+
```scala
|
|
400
400
|
import zio.blocks.chunk.{Chunk, ChunkBuilder}
|
|
401
401
|
|
|
402
402
|
val intBuilder = new ChunkBuilder.Int
|
|
@@ -418,7 +418,7 @@ Chunk provides efficient bit-level operations for working with binary data:
|
|
|
418
418
|
|
|
419
419
|
### Converting to Bits
|
|
420
420
|
|
|
421
|
-
```scala
|
|
421
|
+
```scala
|
|
422
422
|
import zio.blocks.chunk.Chunk
|
|
423
423
|
|
|
424
424
|
val bytes = Chunk[Byte](0x0F, 0xF0.toByte)
|
|
@@ -433,7 +433,7 @@ val longBits = longs.asBitsLong(Chunk.BitChunk.Endianness.BigEndian)
|
|
|
433
433
|
|
|
434
434
|
### Bitwise Operations
|
|
435
435
|
|
|
436
|
-
```scala
|
|
436
|
+
```scala
|
|
437
437
|
import zio.blocks.chunk.Chunk
|
|
438
438
|
|
|
439
439
|
val a = Chunk(true, false, true, false)
|
|
@@ -447,7 +447,7 @@ val negated = a.negate // Chunk(false, true, false, true)
|
|
|
447
447
|
|
|
448
448
|
### Packing Booleans
|
|
449
449
|
|
|
450
|
-
```scala
|
|
450
|
+
```scala
|
|
451
451
|
import zio.blocks.chunk.Chunk
|
|
452
452
|
|
|
453
453
|
val bits = Chunk(true, false, true, false, true, true, true, true)
|
|
@@ -459,7 +459,7 @@ val packedLongs: Chunk[Long] = bits.toPackedLong(Chunk.BitChunk.Endianness.BigEn
|
|
|
459
459
|
|
|
460
460
|
### Binary String
|
|
461
461
|
|
|
462
|
-
```scala
|
|
462
|
+
```scala
|
|
463
463
|
import zio.blocks.chunk.Chunk
|
|
464
464
|
|
|
465
465
|
val bits = Chunk(true, false, true, true)
|
|
@@ -470,7 +470,7 @@ val binary: String = bits.toBinaryString // "1011"
|
|
|
470
470
|
|
|
471
471
|
`ChunkMap[K, V]` is an order-preserving immutable map backed by parallel chunks. It maintains insertion order during iteration:
|
|
472
472
|
|
|
473
|
-
```scala
|
|
473
|
+
```scala
|
|
474
474
|
import zio.blocks.chunk.{Chunk, ChunkMap}
|
|
475
475
|
|
|
476
476
|
val map = ChunkMap("a" -> 1, "b" -> 2, "c" -> 3)
|
|
@@ -482,7 +482,7 @@ val removed = map.removed("b")
|
|
|
482
482
|
|
|
483
483
|
### Creating ChunkMap
|
|
484
484
|
|
|
485
|
-
```scala
|
|
485
|
+
```scala
|
|
486
486
|
import zio.blocks.chunk.{Chunk, ChunkMap}
|
|
487
487
|
|
|
488
488
|
val empty = ChunkMap.empty[String, Int]
|
|
@@ -495,7 +495,7 @@ val fromChunks = ChunkMap.fromChunks(Chunk("a", "b"), Chunk(1, 2))
|
|
|
495
495
|
|
|
496
496
|
ChunkMap provides O(1) positional access:
|
|
497
497
|
|
|
498
|
-
```scala
|
|
498
|
+
```scala
|
|
499
499
|
import zio.blocks.chunk.{Chunk, ChunkMap}
|
|
500
500
|
|
|
501
501
|
val map = ChunkMap("z" -> 1, "a" -> 2, "m" -> 3)
|
|
@@ -512,7 +512,7 @@ val values: Chunk[Int] = map.valuesChunk
|
|
|
512
512
|
|
|
513
513
|
For frequent lookups, create an indexed version with O(1) key access:
|
|
514
514
|
|
|
515
|
-
```scala
|
|
515
|
+
```scala
|
|
516
516
|
import zio.blocks.chunk.ChunkMap
|
|
517
517
|
|
|
518
518
|
val map = ChunkMap("a" -> 1, "b" -> 2, "c" -> 3)
|
|
@@ -525,7 +525,7 @@ val value = indexed.get("b") // O(1) instead of O(n)
|
|
|
525
525
|
|
|
526
526
|
Chunk implements `IndexedSeq` and integrates seamlessly with Scala collections:
|
|
527
527
|
|
|
528
|
-
```scala
|
|
528
|
+
```scala
|
|
529
529
|
import zio.blocks.chunk.Chunk
|
|
530
530
|
|
|
531
531
|
val chunk = Chunk(1, 2, 3, 4, 5)
|
|
@@ -539,7 +539,7 @@ val fromSeq: Chunk[Int] = Chunk.from(Vector(1, 2, 3))
|
|
|
539
539
|
|
|
540
540
|
Standard collection operations work as expected:
|
|
541
541
|
|
|
542
|
-
```scala
|
|
542
|
+
```scala
|
|
543
543
|
import zio.blocks.chunk.Chunk
|
|
544
544
|
|
|
545
545
|
val chunk = Chunk(1, 2, 3)
|
package/reference/docs.md
CHANGED
|
@@ -10,7 +10,7 @@ Complete API reference for the zio-blocks-docs module - a zero-dependency GitHub
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
12
|
```scala
|
|
13
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-docs" % "
|
|
13
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-docs" % "0.0.21"
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
## Core Types
|
|
@@ -170,7 +170,7 @@ The parser supports all GitHub Flavored Markdown features:
|
|
|
170
170
|
- **Tables with alignment**
|
|
171
171
|
- **Inline formatting** (emphasis, strong, strikethrough, code)
|
|
172
172
|
- **Links and images**
|
|
173
|
-
- **Autolinks** (
|
|
173
|
+
- **Autolinks** (`<url>` or plain URLs)
|
|
174
174
|
- **HTML blocks and inline HTML**
|
|
175
175
|
|
|
176
176
|
### Not Supported
|