Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your usage is not that bad, what I find confusing is the idea that what's usually considered the configuration input for a reader (like the db connection data) is in this case the id of the user under inspection.</p> <p>First I'd change the <code>IO[I,A]</code> name to <code>Reader[I,A]</code> just for the sake of using a well known name for such operation[*]</p> <p>As for the <code>User2Colleague(u: User) : IO[UserId, DBObject]</code> method, it's not that unusual to discard the reader input to provide a constant value wrapped in the monad: this is exactly what your <code>unit</code> method does!</p> <p>In fact I'd change that to</p> <pre><code>def User2Colleague(u: User) : Reader[UserId, DBObject] = unit(coll.findOne(MongoDBObject("id" -&gt; u.colleagueId)).get) </code></pre> <p>or even more consistently with your client code usage</p> <pre><code>def User2Colleague(u: User): DBObject = coll.findOne(MongoDBObject("id" -&gt; u.colleagueId)).get ... def main(args: Array[String]) { val mng = new Mongo() val io = for { io &lt;- mng io2 &lt;- unit(DBObject2User(io)) io3 &lt;- unit(User2Colleague(io2)) } yield (DBObject2User(io3)) println(io.run(1)) </code></pre> <p>What I suggest is to write your code as <em>pure</em> as possible (i.e. with no monadic effect) unless required. This means doing your mappings in regular functions and then wrapping with <code>unit</code> only as needed (as to make composition typecheck within the for comprehension)</p> <p>[*] The usual IO monad has no input type, it just has an output type, and is usually targeted to do user I/O (console, printing, emailing, sending rockets, etc), i.e. anything which has side effects external to the program itself.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload