https://github.com/nikdon/scalaz-interpreter
Co-products for Free monads
https://github.com/nikdon/scalaz-interpreter
cats free-monad interpreter scala scalaz
Last synced: 30 days ago
JSON representation
Co-products for Free monads
- Host: GitHub
- URL: https://github.com/nikdon/scalaz-interpreter
- Owner: nikdon
- Created: 2016-02-11T18:16:06.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-02-14T10:27:27.000Z (over 9 years ago)
- Last Synced: 2025-03-05T11:25:48.503Z (over 1 year ago)
- Topics: cats, free-monad, interpreter, scala, scalaz
- Language: Scala
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# scalaz-interperter
[](https://travis-ci.org/nikdon/scalaz-interpreter)
[](https://jitpack.io/#nikdon/scalaz-interpreter)
[](https://codecov.io/github/nikdon/scalaz-interpreter?branch=master)
Interpreter implementation based on [**scalaz**](https://github.com/scalaz/scalaz) and [**cats**](https://github.com/typelevel/cats) inspired by Rúnar Bjarnason [Compositional Application Architecture With Reasonably Priced Monads](https://www.parleys.com/play/53a7d2c3e4b0543940d9e538/).
Code based on **scalaz** and **cats** is almost the same except dependencies. Full examples are in tests.
Programs for interpretation in general look like this one:
```scala
class ImplicitsTest extends FlatSpec with Matchers {
"Implicit conversions" should "be used in the building of a programs" in {
import com.github.nikdon.interpreter.scalaz.Implicits._
// import com.github.nikdon.interpreter.cats.Implicits._
def program[F[_]](implicit
T1: Inject[EnglishSyntax, F],
T2: Inject[JapaneseSyntax, F],
T3: Inject[ChineseSyntax, F]) = for {
_ ← English.Say("The one says: 'Hi!'")
_ ← Japanese.Say("Another one says: 'こんにちは'!")
_ ← Chinese.Say("And somebody says: '嗨!'")
} yield ()
type App[A] = Coproduct[EnglishSyntax, JapaneseSyntax, A]
type App1[A] = Coproduct[ChineseSyntax, App, A]
val prg: Free[App1, Unit] = program[App1]
val engJpInterpreter: App ~> Id = EnglishInterpreter or JapaneseInterpreter
val chEngJpInterpreter: App1 ~> Id = ChineseInterpreter or engJpInterpreter
prg.foldMap(chEngJpInterpreter)
}
}
```
Another way to create an interpreter is to use `HList` from [**shapeless**](https://github.com/milessabin/shapeless):
```scala
val interpreter: App1 ~> Id = Fold(
(ChineseInterpreter: ChineseSyntax ~> Id) ::
(EnglishInterpreter: EnglishSyntax ~> Id) ::
(JapaneseInterpreter: JapaneseSyntax ~> Id) :: HNil
)
```
Or `Fold.tupleN()` function:
```scala
val interpreter: App1 ~> Id = Fold.tupleN(
ChineseInterpreter: ChineseSyntax ~> Id,
EnglishInterpreter: EnglishSyntax ~> Id,
JapaneseInterpreter: JapaneseSyntax ~> Id
)
```
## Getting Started
```scala
resolvers += "jitpack" at "https://jitpack.io"
libraryDependencies += "com.github.nikdon" % "scalaz-interpreter" % "10630f3ca8"
```
[](https://github.com/igrigorik/ga-beacon)