two line demo

This commit is contained in:
Mark Canlas 2022-12-04 13:26:52 -05:00
parent f3feb1ac06
commit b2e21182d3
4 changed files with 29 additions and 7 deletions

1
data/one-line.txt Normal file
View File

@ -0,0 +1 @@
one line

2
data/two-lines.txt Normal file
View File

@ -0,0 +1,2 @@
foo
bar

View File

@ -0,0 +1,10 @@
package com.htmlism.firepower.demo
case class Line(s: String)
object Line:
def mkString(xs: List[Line]): String =
xs
.iterator
.map(_.s)
.mkString("\n")

View File

@ -7,12 +7,21 @@ import java.io.IOException
import com.htmlism.rufio.withzio._
object PrintPrograms extends ZIOAppDefault:
def run: ZIO[Any, Throwable, Unit] =
for {
_ <- File("data/out.txt")
.writeLine("out")
private def writeLine(file: String)(s: String) =
File(s"data/$file.txt")
.writeLine(s)
_ <- File("data/example.txt")
.contents
.map(Console.printLine(_))
private val programs =
List[(String, String)](
"one-line.txt" -> "one line",
"two-lines.txt" -> (Line.mkString _)
.apply(List("foo", "bar").map(Line(_)))
)
def run: Task[Unit] =
for {
// just a traverse in slow motion...
_ <- programs
.map { case (f, s) => File(s"data/$f").writeLine(s) }
.foldLeft[Task[Unit]](ZIO.unit)((acc, z) => acc *> z)
} yield ()