|
| 1 | +# BYO‑F Examples |
| 2 | + |
| 3 | +This page demonstrates using FlowForge with different effect systems using the same core APIs. |
| 4 | + |
| 5 | +## IO (Cats‑Effect) |
| 6 | + |
| 7 | +```scala |
| 8 | +import cats.effect.IO |
| 9 | +import com.flowforge.core.algebra.EffectSystem |
| 10 | +import com.flowforge.core.instances.EffectInstances.catsEffectSystemInstance |
| 11 | +import com.flowforge.core.algebra.FlowforgeResource |
| 12 | + |
| 13 | +val F = EffectSystem[IO] |
| 14 | +val res: FlowforgeResource[IO, java.io.ByteArrayOutputStream] = |
| 15 | + FlowforgeResource.make(F.delay(new java.io.ByteArrayOutputStream()))(s => F.delay(s.close())) |
| 16 | + |
| 17 | +res.use { s => F.delay(s.write(1)) } |
| 18 | +``` |
| 19 | + |
| 20 | +## ZIO Task |
| 21 | + |
| 22 | +```scala |
| 23 | +import zio._ |
| 24 | +import com.flowforge.core.algebra.EffectSystem |
| 25 | +import com.flowforge.core.instances.EffectInstances.zioEffectSystemInstance |
| 26 | +import com.flowforge.core.algebra.FlowforgeResource |
| 27 | + |
| 28 | +val F = EffectSystem[Task] |
| 29 | +val r: FlowforgeResource[Task, java.io.ByteArrayInputStream] = |
| 30 | + FlowforgeResource.make(F.delay(new java.io.ByteArrayInputStream(Array[Byte](1,2,3))))(s => F.delay(s.close())) |
| 31 | + |
| 32 | +val prog: Task[Int] = r.use(is => F.delay(is.read())) |
| 33 | +``` |
| 34 | + |
| 35 | +## Pipeline with BYO‑F |
| 36 | + |
| 37 | +```scala |
| 38 | +import com.flowforge.core.PipelineBuilder |
| 39 | +import com.flowforge.core.algebra.EffectSystem |
| 40 | +import com.flowforge.core.types._ |
| 41 | +import com.flowforge.core.contracts.{ SchemaConforms, SchemaPolicy } |
| 42 | + |
| 43 | +final case class User(id: Long, email: String, age: Int) |
| 44 | +implicit val conforms: SchemaConforms[User, User, SchemaPolicy.Exact] = implicitly |
| 45 | + |
| 46 | +def buildPipeline[F[_]: EffectSystem](src: DataSource, snk: DataSink, dao: com.flowforge.core.algebra.DataAlgebra[F]) = |
| 47 | + PipelineBuilder[F]("byo-pipeline") |
| 48 | + .addTypedSource[User, User, SchemaPolicy.Exact](TypedSource(src), _ => EffectSystem[F].pure(User(0, "x@y", 18))) |
| 49 | + .addTransform[User](u => EffectSystem[F].pure(u.copy(age = u.age + 1))) |
| 50 | + .addTypedSink[User, SchemaPolicy.Exact](TypedSink(snk), (_, d) => dao.read[User](src).flatMap(ds => dao.write(ds, d)).void) |
| 51 | + .build() |
| 52 | +``` |
| 53 | + |
0 commit comments