How to implement a reader monad to access a database

I am trying to implement the reader monad for the first time.

I want to use a monadic style to query a database.

usage example 1: the user has a one-to-one relationship with a colleague. Pseudo codegetUserById(getUserById(id).getColleague())

use case 2: get the list of users by id. Pseudo codeList(getUserById(id1), getUserById(id2))

These seem to be good use cases for monads. My goal is to see if I can use monads to improve my code.

PS: Please provide at least one answer without a scalp.

Here is the code:

package monad
import com.mongodb.casbah.Imports._

object Monad {
  type UserId = Int
  case class User(id: UserId, name: String, colleagueId: UserId)

  trait Reader[I, A] { self =>
    def run(id: I) : A
    def map[B](f: A => B) : Reader[I, B] = 
      new Reader[I, B] { def run(id: I) = f(self.run(id)) }
    def flatMap[B](f: A => Reader[I, B]) : Reader[I, B] =
      new Reader[I, B] { def run(id: I) = f(self.run(id)).run(id) }
  }

  def coll = MongoClient()("test")("user")

  def DBObject2User(o: DBObject) : User = User(o.as[Double]("id").toInt, o.as[String]("name"), o.as[Double]("colleague").toInt)

  // Strange design, id is not used…
  def User2Colleague(u: User) : Reader[UserId, DBObject] = 
    unit(coll.findOne(MongoDBObject("id" -> u.colleagueId)).get)

  def GetUserById : Reader[UserId, DBObject] = 
    Reader { id: UserId => coll.findOne(MongoDBObject("id" -> id)).get }

  def GetUserById2 : Reader[UserId, User] = GetUserById.map(DBObject2User)

  def unit[A](a: => A) = Reader { id: UserId => a }

  object Reader {
    def apply[I, A](f: I => A) = new Reader[I, A] { def run(i: I) = f(i) }
  }

  def main(args: Array[String]) {
    // I can do
    println(GetUserById2.run(1))

    // Same with for comprehension
    val userReader = for (io <- GetUserById2) yield io
    println(userReader.run(1))

    //Combination to explore one-to-one relation
    val user = GetUserById2.run(1)
    val colleague = GetUserById2.run(user.colleagueId)

    // Same with flatMap
    println(GetUserById2.flatMap(User2Colleague).run(1))

    // Same with for-comprehension but doesn't work
    val io = for {io  <- GetUserById2
                  io2 <- User2Colleague(io).map(DBObject2User)} yield io2
    println(io.run(1))

    //TODO: List[Reader] to Reader[List]
  }
}

Is this a good way? I have some doubts, cf my commentStrange design

How can I improve my code?

+4
source share
3 answers

, " ", , .

package monad
package object reader{
  type UserId = Int
}
package reader {

case class User(id: UserId, name: String, colleagueId: UserId)

import com.mongodb.casbah.Imports._
import com.mongodb.casbah


trait Reader[I, A] {
  self =>
  val run = apply _

  def apply(id:I):A

  def map[B](f: A => B): Reader[I, B] =
    new Reader[I, B] {
      def apply(id: I) = f(self.run(id))
    }

  def flatMap[B](f: A => Reader[I, B]): Reader[I, B] =
    new Reader[I, B] {
      def apply(id: I) = f(self(id)).run(id)
    }
}

object Reader {
  def unit[A](a: => A) = apply {
    id: UserId => a
  }

  def apply[I, A](f: I => A) = new Reader[I, A] {
    def apply(i: I) = f(i)
  }
}

object Users {

  def asUser(o: DBObject): User = User(o.as[Double]("id").toInt, o.as[String]("name"), o.as[Double]("colleague").toInt)

  def colleague(u: User): Reader[MongoCollection, User] =
    Reader{ 
      coll => asUser(coll.findOne(MongoDBObject("id" -> u.colleagueId)).get)
    }

  def getUserById(id:UserId): Reader[MongoCollection, User] =
    Reader {
      coll => asUser(coll.findOne(MongoDBObject("id" -> id)).get)
    }
}

object Client extends App {

  import Users._
  def coll: casbah.MongoCollection = MongoClient()("test")("user")

  // I can do
  println(getUserById(1)(coll))

  // Same with for comprehension
  val userReader = for (user <- getUserById(1)) yield user
  println(userReader(coll))

  //Combination to explore one-to-one relation
  val user = getUserById(1)(coll)
  val otherUser = getUserById(user.colleagueId)(coll)

  // Same with flatMap
  println(getUserById(1).flatMap(colleague)(coll))

  // Same with for-comprehension but doesn't work
  val coworkerReader = for {user <- getUserById(1)
                            coworker <- colleague(user)} yield coworker
  println(coworkerReader(coll))         
}

}

, , , (MongoCollection), . http://blog.originate.com/blog/2013/10/21/reader-monad-for-dependency-injection/ ( , )

+3

monad monad transformers + mongoDB, .

+3

, - , (, db) .

IO[I,A] Reader[I,A] [*]

User2Colleague(u: User) : IO[UserId, DBObject], , , : , unit!

def User2Colleague(u: User) : Reader[UserId, DBObject] = 
  unit(coll.findOne(MongoDBObject("id" -> u.colleagueId)).get)

def User2Colleague(u: User): DBObject = coll.findOne(MongoDBObject("id" -> u.colleagueId)).get
...
def main(args: Array[String]) {
  val mng = new Mongo()

  val io = for {
    io <- mng
    io2 <- unit(DBObject2User(io))
    io3 <- unit(User2Colleague(io2))
  } yield (DBObject2User(io3))

  println(io.run(1))  

(.. ), . , , unit ( )

[*] The usual IO-monad does not have an input type, it just has an output type and is usually intended for user input / output (console, printing, emailing, sending a rocket, etc.), that is, everything that has side effects external to the program itself.

+2
source

All Articles