Giter Site home page Giter Site logo

sharedexpensez's Introduction

eDSL using State monad from Scalaz

This is an example converted to Scala from Haskell. The original example:

http://lpenz.github.com/articles/hedsl-sharedexpenses/index.html

An example

val dexter = Person("Dexter")
val angel  = Person("Angel")
val debra  = Person("Debra")
val harry  = Person("Harry")

val trip = sharedexpenses ( for {
  _ <- dexter spent 5300
  _ <- angel  spent 2700
  _ <- debra  spent  800
  _ <- harry  spent 1900
  _ <- debra  spent 1700
  _ <- angel  spent 2200
  _ <- dexter gave  (harry, 2000)
  x <- angel  gave  (debra, 3200)
} yield x)

scala> println(solve(trip).mkString("\n"))

Debra pays 4350 to Angel
Harry pays 3650 to Dexter
Harry pays 100 to Angel

Conclusion

Scala's for-comprehension is not quite as nice as Haskell's do-notation in this example. There's some syntactic noise coming from required binding '_ <- ' and from required value yielding. It would be perhaps possible to remove those if Scala would add some sugar to its for-comprehension:

  • Do not require variable binding for M[Unit]
  • yield the value of a last expression if yield is missing

After that the example would look as:

val dexter = Person("Dexter")
val angel  = Person("Angel")
val debra  = Person("Debra")
val harry  = Person("Harry")

val trip = sharedexpenses ( for {
  dexter spent 5300
  angel  spent 2700
  debra  spent  800
  harry  spent 1900
  debra  spent 1700
  angel  spent 2200
  dexter gave  (harry, 2000)
  angel  gave  (debra, 3200)
})

Kudos for the original poster for a nice example!

sharedexpensez's People

Contributors

retronym avatar

Stargazers

Mauricio Scheffer avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.