paragraph demo

This commit is contained in:
Mark Canlas 2022-12-04 13:55:33 -05:00
parent b2e21182d3
commit 178159f97e
4 changed files with 38 additions and 8 deletions

5
data/two-paragraphs.txt Normal file
View File

@ -0,0 +1,5 @@
foo
bar
alpha
bravo

View File

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

View File

@ -0,0 +1,26 @@
package com.htmlism.firepower.demo
/**
* An abstraction for groups of lines that are eventually separated by newlines, but between paragraphs there's an
* extra newline
*/
case class Paragraph(xs: List[String])
object Paragraph:
val blankLine: Paragraph =
Paragraph(List(""))
def apply(xs: String*): Paragraph =
Paragraph(xs.toList)
def mkLines(ps: List[Paragraph]): List[String] =
interlace(blankLine)(ps)
.flatMap(_.xs)
private def interlace[A](x: A)(xs: List[A]): List[A] =
xs match
case head :: tail =>
head :: tail.flatMap(a => List(x, a))
case Nil =>
Nil

View File

@ -13,9 +13,12 @@ object PrintPrograms extends ZIOAppDefault:
private val programs =
List[(String, String)](
"one-line.txt" -> "one line",
"two-lines.txt" -> (Line.mkString _)
.apply(List("foo", "bar").map(Line(_)))
"one-line.txt" -> "one line",
"two-lines.txt" -> (Line.mkString _)
.apply(List("foo", "bar")),
"two-paragraphs.txt" -> (Line.mkString _)
.compose(Paragraph.mkLines)
.apply(List(Paragraph("foo", "bar"), Paragraph("alpha", "bravo")))
)
def run: Task[Unit] =