mirror of
https://github.com/mcanlas/6502-opcodes.git
synced 2025-08-09 07:25:08 +00:00
inter flatmap
This commit is contained in:
@@ -7,20 +7,16 @@ package com.htmlism.firepower.demo.str
|
|||||||
case class Paragraph(xs: List[String])
|
case class Paragraph(xs: List[String])
|
||||||
|
|
||||||
object Paragraph:
|
object Paragraph:
|
||||||
val blankLine: Paragraph =
|
|
||||||
Paragraph(List(""))
|
|
||||||
|
|
||||||
def apply(xs: String*): Paragraph =
|
def apply(xs: String*): Paragraph =
|
||||||
Paragraph(xs.toList)
|
Paragraph(xs.toList)
|
||||||
|
|
||||||
def mkLines(ps: List[Paragraph]): List[String] =
|
def mkLines(ps: List[Paragraph]): List[String] =
|
||||||
interlace(blankLine)(ps)
|
interFlatMap(ps)("", _.xs)
|
||||||
.flatMap(_.xs)
|
|
||||||
|
|
||||||
private def interlace[A](x: A)(xs: List[A]): List[A] =
|
def interFlatMap[A, B](xs: List[A])(x: B, f: A => List[B]): List[B] =
|
||||||
xs match
|
xs match
|
||||||
case head :: tail =>
|
case head :: tail =>
|
||||||
head :: tail.flatMap(a => List(x, a))
|
f(head) ::: tail.flatMap(a => x :: f(a))
|
||||||
|
|
||||||
case Nil =>
|
case Nil =>
|
||||||
Nil
|
Nil
|
||||||
|
Reference in New Issue
Block a user