Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Eff HandsOn

C6a8cb5e13aa716521d522471ec4e4cd?s=47 ma2k8
October 25, 2018

Eff HandsOn

C6a8cb5e13aa716521d522471ec4e4cd?s=128

ma2k8

October 25, 2018
Tweet

Transcript

  1. Eff hands on

  2. ༻ޠͷઆ໌ • Monad Transformer • Extensible effect (use FreeMonad) •

    ˒Freer effects (use FreerMonad, Efficient Freer) • جͷ࿦จͰ͸ more Extensible effectͱݺΜͰ͍Δ
  3. Ϟφτϥͱͷൺֱ(ࡶ)

  4. ߹੒ͷॱং • ϞφυτϥϯεϑΥʔϚʔ͸߹੒ͷॱংΛೖΕସ͑Δ͜ͱ ͕Ͱ͖ͳ͍ • Eff͸OpenUnionʹΑͬͯՄೳ

  5. ࡞༻ͷॱং • ϞφυτϥϯεϑΥʔϚʔ͸ධՁॱΛೖΕସ͑Δͱ݁Ռ͕ มΘΔ • Eff͸มΘΒͳ͍(Writer,Either౳ɺܕʹ௚઀࡞༻͢Δ΋ͷ͸ ී௨ʹॱংʹΑͬͯܕ͸มΘΔ͕ܭࢉͷ݁Ռ͸มΘΒͳ͍ • ʹαϯϓϧίʔυ͕͋Δ(Haskell͚ͩͲɾɾɾ) •

    https://konn-san.com/prog/haskell/extensible-effects.html
  6. ωετπϥΠͶΜ • EnglishͰ͸`Future[\/[E, A]]` ΛҾ͖ճ͢Α͏ʹ͠ɺͦΕҎ্ ͷෳࡶͳܕ͸ͳΔ΂͘࢖Θͳ͍Α͏ʹͯ͠ճආ͍ͯ͠Δ͕ɺ 3ͭҎ্ͷϞφυΛ߹੒͢Δ৔߹͸liftͰ্࣋ͪ͛ͯॲཧ͢ Δඞཁ͕͋ΔɻৗʹίϯςΩετΛҙࣝ͠ͳ͍ͱ͍͚ͣɺ ਓྨͷ೴Έͦʹ͸ͱͯ΋Ή͔͍ͣ͠ •

    EffͰ͸શͯϑϥοτͳforࣜͰճͤΔɻωετ͠ͳ͍ɻ
  7. ॏ͍ • Ϟφτϥ͸཯଎ɻܕ߹ΘͤͷͨΊʹ༨ܭͳ૚΋࡞ΒΕΔ • Future[\/[E,A]] • Future[\/[E,Future[\/[E, A]]]] • Future[\/[E,Future[\/[E,

    Future[\/[E, A]]]]]] • (Θ͔Γ΍͘͢ಉ͡ܕʹͨ͠ͷͰਖ਼֬Ͱ͸ͳ͍͕)Έ͍ͨͳײ͡Ͱελο Ϋ͍ͯ͘͠ɻੵΊ͹ੵΉ΄Ͳॏ͍ • Eff͸OpenUnion,EfficientFreerʹΑͬͯελοΫͷ਺ʹґଘ ͠ͳ͍ఆ਺࣌ؒͰͷॲཧ͕Մೳ
  8. ࢖͍ํ

  9. ͢΂ͯ͸EffʹͳΔ • ͍··ͰM[+_]ίϯςΩετͷதͰ`.right`ͯ͠`toEitherT`͠ ͍ͯͨͷ͕͢΂ͯEffʹͳΔ • Future,Either,DBIO,Reader,Writer,Task౳ɺ͢΂ͯͷMonad͸Eff ΁ม׵͢Δ͜ͱ͕Ͱ͖Δ • ࠓͷॴɺOptionͱTraversalܥ(List,Seq౳)Ҏ֎ΛEffʹ͢ΔΑ ͏ʹ͍ͯ͠Δ

  10. ͜ͷ΁ΜΛimport import org.atnos.eff.Eff import org.atnos.eff.syntax.all._ import org.atnos.eff.addon.scalaz.either._ import org.atnos.eff.addon.monix.task._ import

    jp.eigosapuri.es.shared.lib.eff.either.EsErrorEffect._ import jp.eigosapuri.es.shared.adapter.secondary.eff._ import jp.eigosapuri.es.shared.adapter.secondary.eff.push.PushIOEffect._ import jp.eigosapuri.es.shared.adapter.secondary.eff.db.DBIOEffect._
  11. ͜Μͳײ͡ͰEffԽ override def resolveById[R: _dbio]( id: CallPreparationId )(implicit ec: ExecutionContext):

    Eff[R, Option[CallPreparation]] = { for { resOpt <- fromDBIO(callPreparationStatusesDAO.ioFindById(id.value)) } yield resOpt.map(convertToDomainModel) } fromDBIOͰEffԽ͍ͯ͠Δ
  12. ؆୯ʹΈ͑Δ͔΋͕ͩ ͜Ε͸ΊͬͪΌࣗ༝౓ߴ͍

  13. for { x <- Option(7) y <- DBIO.successful(x) _ <-

    -\/(UseCaseError) } y ཁ͸ҧ͏ܕͷ߹੒͕࣮ݱͰ͖͍ͯΔ for { x <- fromOption(Option(7)) y <- fromDBIO(DBIO.successful(x)) _ <- fromEsError(-\/(UseCaseError)) } y
  14. ࠓޙɺFujiTask౳Λ௥Ճ͍ͯ͘͠

  15. ஫ҙ఺

  16. • ܕ߹Θͤήʔ͔Β͸։์͞Ε͍ͯ·͕͢ɺ·ͩ੔ཧͰ͖ͯ ͳͯ͘importήʔʹͳ͍ͬͯΔ • ࡞༻ͷλάͷ੔ཧ͕Ͱ͖͍ͯͳ͍

  17. importήʔ • EffܥͷύοέʔδΛimport͠ͳ͍ͱ͍͚ͳ͍ • Scalaz΍ɺxxExtensionͱಉ͡ϊϦͰ͢ • ΋͏ͪΐ͍੔ཧͯ͠ҰՕॴimport͢Ε͹͢΂͕͍ͯͭͯ͘ΔΑ͏ʹ͠·͢ • ۩ମతʹ͸ҎԼͷύοέʔδ import

    org.atnos.eff.Eff import org.atnos.eff.syntax.all._ import org.atnos.eff.addon.scalaz.either._ import org.atnos.eff.addon.monix.task._ import jp.eigosapuri.es.shared.lib.eff._ import jp.eigosapuri.es.shared.lib.eff.cache.CacheIOTypes._ import jp.eigosapuri.es.shared.lib.eff.push.PushIOTypes._ import jp.eigosapuri.es.shared.lib.eff.util.clock.joda.JodaTimeMEffect._ import jp.eigosapuri.es.shared.lib.eff.util.idGen.IdGenEffect._ import jp.eigosapuri.es.shared.lib.eff.either.EsErrorEffect._ import jp.eigosapuri.es.shared.adapter.secondary.eff._ import jp.eigosapuri.es.shared.adapter.secondary.eff.push.PushIOEffect._ import jp.eigosapuri.es.shared.adapter.secondary.eff.db.DBIOEffect._ import jp.eigosapuri.es.shared.adapter.secondary.eff.push.CacheIOInterpretationTypes._ import jp.eigosapuri.es.shared.adapter.secondary.eff.cache.interpreter.CacheIOInterpreter import jp.eigosapuri.es.shared.adapter.secondary.eff.db.interpreter.DBIOInterpreter import jp.eigosapuri.es.shared.adapter.secondary.eff.push.interpreter.PushIOInterpreter
  18. ࡞༻λάᶃ • EffͰ͸Ͳͷ࡞༻ΛؚΉ͔Λࢦఆ͢Δඞཁ͕͋Δ • ྫ͑͹ɺDBIOͱIdGen,JodaTime,PushΛར༻͢Δ৔߹͸ type R = FxAppend[DBIOStack, FxAppend[ModelApplyStack,

    PushIOStack]] • ͦΕͧΕͷத਎ type DBIOStack = Fx.fx3[DBIO, Task, ErrorEither] type ModelApplyStack = Fx.fx2[IdGen, JodaTimeM] type PushIOStack = Fx.fx1[PushIO] • ͜ͷΑ͏ʹ͋Δఔ౓ͷ୯ҐͰ·ͱΊΒΕͨStackΛFxAppendͰ͕ͬͪΌΜ͜ Ͱ͖Δɻޙ͸ͷΑ͏ʹUseCaseʹ౉࣮ͯ͠ߦ͢Δ͚ͩ startCallUseCase.execute[R](arg) • ಉ͡λάΛෳ਺ࢦఆ͢Δ͜ͱ͸Ͱ͖ͳ͍
  19. ࡞༻λάᶄ • ࢦఆͨ͠λά(࡞༻)ΛҰͭҰͭධՁ͍ͯ͘͠ɻ • DBIOStack, ModelApplyStack, PushIOStackͷධՁ͸ҎԼͷΑ͏ʹͳΔ useCaseRes.runTransactionDBIO .runIdGen .runJodaTimeM

    .runPushIO .runEsError .runAsync .runAsync .map(startCallPresenter.response) • ͜Ε΋ `run` ͚ͩͰࡁΉΑ͏࣮ݧத
  20. ࡞༻λάᶅ • ͜ͷλά͸ؔ਺ͷγάχνϟͱͯ͠Ҿ͖ճ͞ͳ͍ͱ͍͚ͳ ͍ def execute[R: _dbio: _task: _errorEither: _pushio:

    _idgen: _jodaTimem]( arg: StartCallUseCaseArgs )(implicit ec: ExecutionContext): Eff[R, StartCallUseCaseResult]
  21. ͜ΕΒ͸मਖ਼தͰ͢

  22. ˒YATTEIKI˒