使用 Shapeless 记录组合任意数量的状态变化函数 [英] Using Shapeless records to combine arbitrary number of state-changing functions

查看:41
本文介绍了使用 Shapeless 记录组合任意数量的状态变化函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to port combineReducers from Redux to Scala. The idea is that each function controls it's small part of the state and combineReducers creates a function that controls the whole state. I can't figure out required signature for the function that should work something like this:

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

}

推荐答案

尝试

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,因为编译器无法推断combineReducers 中的E.如果您将签名更改为

We should annotate reducer with explicit type because compiler cant't infer E in combineReducers. If you change signature to

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)

为了组合任意数量的 reducer,你可以创建类型类

For combining arbitrary number of reducers you can create type class

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)

这篇关于使用 Shapeless 记录组合任意数量的状态变化函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆