使用 Shapeless 记录组合任意数量的状态更改函数

2024-05-17

我正在尝试移植combineReducers从 Redux 到 Scala。这个想法是每个函数控制它的一小部分状态并且combineReducers创建一个控制整个状态的函数。我无法找出应该像这样工作的函数所需的签名:

sealed trait Event

case class Create() extends Event
case class Save() extends Event

object Reducers {
  def combineReducers[E, S1, S2](fTag: String, f: (E, S1) => S1, gTag: String, g: (E, S2) => S2) = ???

  type IntState = Int
  type StringState = String

  def intReducer(e: Event, state: IntState): IntState = state + 1

  def stringReducer(e: Event, state: StringState): StringState = state + e.toString


  val reducer = combineReducers("count", intReducer, "names", stringReducer)

  val initialState = ("count" ->> 0 ) :: ("names" ->> "") :: HNil

  val newState = reducer(Create(), initialState) // returns ("count" ->> 1) :: ("names" ->> "Create()") :: HNil
  reducer(Save(), newState) // returns ("count" ->> 2) :: ("names" ->> "Create()Save()") :: HNil

}

Try

import shapeless.labelled.{FieldType, field}
import shapeless.{::, HNil}
import shapeless.syntax.singleton._

sealed trait Event

case class Create() extends Event
case class Save() extends Event

object Reducers {
  type Reducer[E, S] = (E, S) => S

  def combineReducers[K1 <: String, E, S1, K2 <: String, S2](
                                                              fTag: K1,
                                                              f: Reducer[E, S1],
                                                              gTag: K2,
                                                              g: Reducer[E, S2]
                                                            ): Reducer[E, FieldType[K1, S1] :: FieldType[K2, S2] :: HNil] =
    { case (e, v1 :: v2 :: HNil) => field[K1](f(e, v1)) :: field[K2](g(e, v2)) :: HNil }

  type IntState = Int
  type StringState = String

  def intReducer(e: Event, state: IntState): IntState = state + 1

  def stringReducer(e: Event, state: StringState): StringState = state + e.toString

  val reducer: Reducer[Event, FieldType[Witness.`"count"`.T, IntState] :: FieldType[Witness.`"names"`.T, StringState] :: HNil] =
    combineReducers("count".narrow, intReducer, "names".narrow, stringReducer)

  val initialState = ("count" ->> 0 ) :: ("names" ->> "") :: HNil

  val newState = reducer(Create(), initialState) // returns ("count" ->> 1) :: ("names" ->> "Create()") :: HNil
  val newState1 = reducer(Save(), newState) // returns ("count" ->> 2) :: ("names" ->> "Create()Save()") :: HNil
}

我们应该注释reducer使用显式类型,因为编译器无法推断E in combineReducers。如果您将签名更改为

def combineReducers[K1 <: String, S1, K2 <: String, S2](
                                                         fTag: K1,
                                                         f: Reducer[Event, S1],
                                                         gTag: K2,
                                                         g: Reducer[Event, S2]
                                                       ): Reducer[Event, FieldType[K1, S1] :: FieldType[K2, S2] :: HNil] = ...

那么你可以写

val reducer = combineReducers("count".narrow, intReducer, "names".narrow, stringReducer)

为了组合任意数量的减速器,您可以创建类型类

trait CombineReducers[Keys <: HList, Reducers <: HList] {
  type Record <: HList
  def apply(keys: Keys, reducers: Reducers): Reducer[Event, Record]
}
object CombineReducers {
  type Aux[Keys <: HList, Reducers <: HList, Record0 <: HList] = CombineReducers[Keys, Reducers] { type Record = Record0 }
  def instance[Keys <: HList, Reducers <: HList, Record0 <: HList](f: (Keys, Reducers) => Reducer[Event, Record0]): Aux[Keys, Reducers, Record0] = new CombineReducers[Keys, Reducers] {
    type Record = Record0
    override def apply(keys: Keys, reducers: Reducers): Reducer[Event, Record0] = f(keys, reducers)
  }

  implicit val hnilCombineReducers: Aux[HNil, HNil, HNil] = instance { case (HNil, HNil) => { case (_, HNil) => HNil }}
  implicit def hconsCombineReducers[K <: String, Ks <: HList, S, Rs <: HList](implicit
    cr: CombineReducers[Ks, Rs]): Aux[K :: Ks, Reducer[Event, S] :: Rs, FieldType[K, S] :: cr.Record] = instance {
    case (k :: ks, r :: rs) => {
      case (e, s :: ss) => field[K](r(e, s)) :: cr(ks, rs)(e, ss)
    }
  }
}

并使用它

def combineReducers[Keys <: HList, Reducers <: HList](keys: Keys, reducers: Reducers)(implicit
  cr: CombineReducers[Keys, Reducers]): Reducer[Event, cr.Record] = cr(keys, reducers)

val reducer =
  combineReducers("count".narrow :: "names".narrow :: HNil, (intReducer: (Event, IntState) => IntState) :: (stringReducer : (Event, StringState) => StringState) :: HNil)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Shapeless 记录组合任意数量的状态更改函数 的相关文章

  • 对列表中的相邻元素进行分组

    假设我想编写一个函数来执行此操作 输入 1 1 3 3 4 2 2 5 6 6 输出 1 1 3 3 4 2 2 5 6 6 它将相同的相邻元素分组 这个方法的名称应该是什么 此操作有标准名称吗 In 1 1 3 3 4 2 2 5 6 6
  • 如何使用 `ProjectRef` 来引用 sbt 1.x 中的本地项目?

    其他答案中有很多含糊不清的内容 或者涉及到更旧版本的 sbt 即 0 12 x 但似乎没有人真正回答这个问题 鉴于我有一个文件夹 并且我已经运行 sbt new scala scala seed g8 name Scala Seed Pro
  • sbt:编译测试时设置特定的 scalacOptions 选项

    通常我使用这组选项来编译 Scala 代码 scalacOptions Seq deprecation encoding UTF 8 feature unchecked language higherKinds language impli
  • Spark-shell 使用不同版本的 Scala。使用 homebrew 安装 scala 和 apache-spark

    我使用 homebrew 安装了 scala 和 apache spark 它安装了 scala 2 12 4 和 apache spark 2 2 0 但是 如果您结帐spark shell version它使用不同的 scala 版本
  • 内存泄漏在哪里?

    我使用 InetAddress 来解析 IP 地址 但现在如果 IP 不可用 则需要存储主机名 所以我介绍了一个班级Host case class Host name String ip InetAddress import Host ad
  • 手动排除sbt中的一些测试类

    我通常在 CI 中执行以下命令 清理更新编译测试发布 但是 我想从 sbt 命令行中排除 1 个 或几个 测试类 我怎样才能做到这一点 我不想更改我的代码以使用忽略等 两种可能的选择 test only See http www scala
  • Scala Sparkcollect_list() 与 array()

    有什么区别collect list and array 在 Spark 中使用 scala 我看到到处都有使用情况 但我不清楚用例来确定差异 尽管两者array https spark apache org docs latest api
  • 用 HashMap[Int, Vector[Int]] (Scala) 表示图(邻接列表)?

    我想知道如何 如果可能的话 我可以通过以下方式制作 可变 图的邻接列表表示HashMap Int Vector Int HashMap当然是可变的 目前我将其设置为HashMap Int ArrayBuffer Int 但我可以更改 Arr
  • Scala 条件列表构造

    我正在使用 Scala 2 9 2 并且想根据某些条件构建一个列表 考虑以下情况 其中 cond 是采用谓词 p 和类型 T 的值 在本例中为 t3 的某个函数 t1 t2 cond p t3 t4 我想要的行为如下 如果 p 为真 则应给
  • 如何在 akka actor 中测试公共方法?

    我有一个 akka 演员 class MyActor extends Actor def recieve def getCount id String Int do a lot of stuff proccess id do more st
  • Scala 中缺少多重集吗?

    我正在尝试 Scala 中的 Facebook Hacker Cup 2013 资格赛问题 对于第三个问题 我觉得需要一个有序的 Multiset 但在 scala 的 2 10 集合中找不到一个 scala 的集合中是否缺少此数据结构 会
  • Scala:将整个列表的 Either 与每个元素的 Either 组合

    我有一个 Either 列表 它代表错误 type ErrorType List String type FailFast A Either ErrorType A import cats syntax either val l List
  • 创建自定义 scala 集合,其中映射默认返回自定义集合?

    特质TraversableLike A Repr 允许人们在其中进行收藏some函数将返回一个Repr 而其他人则继续返回类型参数That在功能上 有没有办法定义一个CustomCollection A 其中函数如map 其他的默认That
  • SBT 对 Scala 类型感到困惑

    SBT 抛出以下错误 value split is not a member of String String error filter arg gt arg split delimiter length gt 2 对于以下代码块 impl
  • Scalaz 拆箱标记类型不会自动拆箱

    Reading http eed3si9n com learning scalaz Tagged type html http eed3si9n com learning scalaz Tagged type html并尝试示例代码 imp
  • Scala 匿名函数中的 return 语句

    为什么显式 return 语句 使用return关键字 在匿名函数中从封闭的命名函数返回 而不仅仅是从匿名函数本身返回 例如 以下程序会导致类型错误 def foo String x Integer gt return x foo 我知道建
  • 如何在 Spark 数据帧 groupBy 中执行 count(*)

    我的目的是做相当于基本sql的事情 select shipgrp shipstatus count cnt from shipstatus group by shipgrp shipstatus 我见过的 Spark 数据帧的示例包括其他列
  • 在 Scala 中反转地图的优雅方法

    目前正在学习Scala 需要反转Map 来进行一些反转值 gt 键查找 我一直在寻找一种简单的方法来做到这一点 但只想到了 Map origMap map kvp gt kvp 2 gt kvp 1 有人有更优雅的方法吗 假设值是唯一的 则
  • 如何在每行中添加行号?

    假设这些是我的数据 Maps and Reduces are two phases of solving a query in HDFS Map is responsible to read data from input location
  • Spark scala:大量列上的简单 UDF 会导致性能下降

    我有一个包含 1 亿行和约 10 000 列的数据框 这些列有两种类型 标准 C i 和动态 X i 这个dataframe是经过一些处理后得到的 性能很快 现在只剩下2步了 Goal 需要使用 C i 列的相同子集对每个 X i 执行特定

随机推荐