如何将源映射到另一个?
How to map a source onto another?
我有两个案例类
case class Color (name: String, shades: List[Shade] = List.empty)
case class Shade (shadeName: String)
我也有两个解析器:
object ColorParser {
def apply(
s: String): Either[List[SomethingElse], Color] = {
val values = s.split("\|", -1).map(_.trim).toList
validateColor(values).leftMap(xs => xs.toList).toEither
}
}
object ShadesParser {
def apply(s: String)
: Either[List[SomethingElse], Shade] = {
val values = s.split('|').map(_.trim).toList
validateShade(values).leftMap(xs => xs.toList).toEither
}
}
我有 Color
的来源和 Shade
的来源。
sourceForShade
.via (framing("\n"))
.map (_.utf8string)
.map (_.trim)
.map {
s => ShadesParser(s)
}
.collect {
case Right(shade) => shade
}
sourceForColor
.via(framing("\n"))
.map(_.utf8String)
.map(_.trim)
.map(s => ColorParser(s))
.collect {
case Right(color) => color
}
.map {color =>
//Here I want access to Color object that has the property shades list property set based on sourceForShade.
//At the moment it only has the name field but the List[Shade] property is empty.
}
问题
在 map
的评论部分,我如何才能访问一个颜色对象,该对象还根据 sourceForShade
[=18 填充了 shades: List[Shade]
属性 =]
一种方法是首先从第一个流中获取 Future[Seq[Shade]]
,然后将 Future
的结果应用于第二个流:
val shades: Future[Seq[Shade]] =
sourceForShade
.via(framing("\n"))
.map(_.utf8String.trim)
.map(ShadesParser)
.collect {
case Right(shade) => shade
}
.runWith(Sink.seq[Shade])
val colors: Future[Source[Color, _]] =
shades map { s =>
sourceForColor
.via(framing("\n"))
.map(_.utf8String.trim)
.map(ColorParser)
.collect {
case Right(color) => color
}
.map(c => c.copy(shades = s.toList))
}
val colorsWithShades: Source[Color, _] =
Source.fromFuture(colors).flatMapConcat(identity)
我有两个案例类
case class Color (name: String, shades: List[Shade] = List.empty)
case class Shade (shadeName: String)
我也有两个解析器:
object ColorParser {
def apply(
s: String): Either[List[SomethingElse], Color] = {
val values = s.split("\|", -1).map(_.trim).toList
validateColor(values).leftMap(xs => xs.toList).toEither
}
}
object ShadesParser {
def apply(s: String)
: Either[List[SomethingElse], Shade] = {
val values = s.split('|').map(_.trim).toList
validateShade(values).leftMap(xs => xs.toList).toEither
}
}
我有 Color
的来源和 Shade
的来源。
sourceForShade
.via (framing("\n"))
.map (_.utf8string)
.map (_.trim)
.map {
s => ShadesParser(s)
}
.collect {
case Right(shade) => shade
}
sourceForColor
.via(framing("\n"))
.map(_.utf8String)
.map(_.trim)
.map(s => ColorParser(s))
.collect {
case Right(color) => color
}
.map {color =>
//Here I want access to Color object that has the property shades list property set based on sourceForShade.
//At the moment it only has the name field but the List[Shade] property is empty.
}
问题
在 map
的评论部分,我如何才能访问一个颜色对象,该对象还根据 sourceForShade
[=18 填充了 shades: List[Shade]
属性 =]
一种方法是首先从第一个流中获取 Future[Seq[Shade]]
,然后将 Future
的结果应用于第二个流:
val shades: Future[Seq[Shade]] =
sourceForShade
.via(framing("\n"))
.map(_.utf8String.trim)
.map(ShadesParser)
.collect {
case Right(shade) => shade
}
.runWith(Sink.seq[Shade])
val colors: Future[Source[Color, _]] =
shades map { s =>
sourceForColor
.via(framing("\n"))
.map(_.utf8String.trim)
.map(ColorParser)
.collect {
case Right(color) => color
}
.map(c => c.copy(shades = s.toList))
}
val colorsWithShades: Source[Color, _] =
Source.fromFuture(colors).flatMapConcat(identity)