|
| 1 | +--- |
| 2 | +description: Use Flows for functional-style streaming transformations with methods like `map`, `mapPar`, `filter`, `groupBy` and many others. Flows are lazy, composable, and manage concurrency declaratively and efficiently. Flows only start processing data when run. |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Streaming: Prefer Flows Over Channels |
| 7 | + |
| 8 | +**Use `Flow[T]` for streaming data transformations** instead of low-level channel operations. Flows provide a functional, composable API with built-in concurrency management. |
| 9 | + |
| 10 | +## Flow Characteristics |
| 11 | + |
| 12 | +**Flows are lazy**: No processing happens until `.run*()` methods are called |
| 13 | +```scala |
| 14 | +import ox.flow.Flow |
| 15 | + |
| 16 | +// This just describes the pipeline - nothing executes yet |
| 17 | +val pipeline = Flow.fromValues(1, 2, 3) |
| 18 | + .map(_ * 2) |
| 19 | + .filter(_ > 2) |
| 20 | + |
| 21 | +// Processing happens here |
| 22 | +val result = pipeline.runToList() // List(4, 6) |
| 23 | +``` |
| 24 | + |
| 25 | +## Creating Flows |
| 26 | + |
| 27 | +**Multiple ways to create flows**: |
| 28 | +```scala |
| 29 | +import ox.flow.Flow |
| 30 | +import scala.concurrent.duration.* |
| 31 | + |
| 32 | +// From values |
| 33 | +Flow.fromValues(1, 2, 3) |
| 34 | + |
| 35 | +// Infinite flows |
| 36 | +Flow.tick(1.second, "heartbeat") |
| 37 | +Flow.iterate(0)(_ + 1) // natural numbers |
| 38 | + |
| 39 | +// From channels |
| 40 | +Flow.fromSource(channel) |
| 41 | + |
| 42 | +// Custom emission logic |
| 43 | +Flow.usingEmit: emit => |
| 44 | + emit(1) |
| 45 | + for i <- 4 to 50 do emit(i) |
| 46 | + if condition() then emit(42) |
| 47 | +``` |
| 48 | + |
| 49 | +## Functional Transformations |
| 50 | + |
| 51 | +**Rich transformation API** similar to Scala collections: |
| 52 | +```scala |
| 53 | +Flow.fromValues(1, 2, 3, 4, 5) |
| 54 | + .map(_ * 2) |
| 55 | + .filter(_ % 4 == 0) |
| 56 | + .take(3) |
| 57 | + .zip(Flow.repeat("item")) |
| 58 | + .interleave(Flow.fromValues((0, "other"))) |
| 59 | + .runForeach(println) |
| 60 | +``` |
| 61 | + |
| 62 | +## Built-in Concurrency Management |
| 63 | + |
| 64 | +**Flows handle concurrency declaratively**: |
| 65 | +```scala |
| 66 | +// Parallel processing with controlled concurrency |
| 67 | +Flow.fromValues(urls) |
| 68 | + .mapPar(4)(sendHttpRequest) // max 4 concurrent requests |
| 69 | + .filter(_.isSuccess) |
| 70 | + .runDrain() |
| 71 | + |
| 72 | +// Asynchronous boundaries with buffering |
| 73 | +Flow.fromSource(inputChannel) |
| 74 | + .buffer(capacity = 100) // explicit async boundary |
| 75 | + .mapPar(8)(process) |
| 76 | + .runToChannel() |
| 77 | +``` |
| 78 | + |
| 79 | +## When to Use Channels Directly |
| 80 | + |
| 81 | +**Use low-level channels only for**: |
| 82 | +- Go-like inter-thread communication patterns |
| 83 | +- Custom coordination with `select` operations |
| 84 | +- Bridging callback-based APIs |
| 85 | +- Building custom flow operations |
| 86 | + |
| 87 | +**Good**: Flow-based streaming |
| 88 | +```scala |
| 89 | +Flow.fromInputStream(inputStream) |
| 90 | + .linesUtf8 |
| 91 | + .mapPar(parallelism)(processLine) |
| 92 | + .runForeach(println) |
| 93 | +``` |
| 94 | + |
| 95 | +**Avoid**: Manual channel coordination for simple transformations |
| 96 | +```scala |
| 97 | +// Don't do this for simple transformations |
| 98 | +val ch1 = Channel.buffered[String](mdc:10) |
| 99 | +val ch2 = Channel.buffered[Int](mdc:10) |
| 100 | +fork: |
| 101 | + ch1.drain(): line => |
| 102 | + ch2.send(line.length) |
| 103 | + ch2.done() |
| 104 | +``` |
| 105 | + |
| 106 | +Flows provide **safety, composability, and performance** with a familiar functional API. |
0 commit comments