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

时间:2018-10-09 15:41:10

标签: scala shapeless

我正在尝试将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

}

2 个答案:

答案 0 :(得分:2)

尝试

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中的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)

要组合任意数量的reducer,可以创建类型类

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)

答案 1 :(得分:2)

IMO,您仍然不需要Shapeless:D

您的reduce函数可以包含未更改的标签。 将标签设置为初始状态有点尴尬,但是您当前的解决方案存在相同的问题

object CombineStatesQ extends App {

  import cats._
  import cats.implicits._

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

  type Reducer[A] = (Event, A) => A

  implicit object ReducerAp extends InvariantSemigroupal[Reducer] {
    override def product[A, B](fa: Reducer[A], fb: Reducer[B]): Reducer[(A, B)] =
      (e: Event, ab:(A,B)) => (fa(e,ab._1), fb(e, ab._2))
    override def imap[A, B](fa: Reducer[A])(f: A => B)(g: B => A): Reducer[B] = (e, b) => f(fa(e, g(b)))
  }

  type Label = String
  val countReducer: Reducer[(Label, Int)] = (e, s) => (s._1, s._2 + 1)
  val stringReducer: Reducer[(Label, String)] = (e, s) => (s._1, s._2 + e.toString)
  val listReducer: Reducer[(Label, List[Event])] = (e, s) => (s._1, e:: s._2)

  val combinedReducer: Reducer[((Label, Int), (Label, String), (Label, List[Event]))] =
    (countReducer, stringReducer, listReducer).tupled

  val initialState: ((Label, Int), (Label, String), (Label, Nil.type)) =
    (("count", 0 ),("names",""), ("list", Nil))


  val state1 = combinedReducer(Create(),initialState)
  println(state1) // ((count,1),(names,Create()),(list,List(Create())))
  val state2 = combinedReducer(Save(), state1)
  println(state2) //((count,2),(names,Create()Save()),(list,List(Save(), Create())))
}