dbioAction.sequence

425 views
Skip to first unread message

Naftoli Gugenheim

unread,
Apr 8, 2015, 10:25:27 AM4/8/15
to scala...@googlegroups.com
Hi, I've been using (and enjoying) Slick 3 a bit.

I find that I'm using DBIO.sequence often, on long DBIO-composing expressions. I think the code would read a lot better if I could write expr.sequence, e.g., act.flatMap(...).sequence. Of course not every DBIO can be sequenced, so you could either do it the way scala collections .toMap works, with implicit evidence, or you could provide it as an extension method with an implicit class.

Thoughts?


Also another thing. The following would give a type error:

xs.foldLeft(DBIO.successful(0)){ (a, b) => ??? : DBIO[Int] }

It's the same kind of problem as xs.foldLeft(Nil)((a, b) => List(1)). In this case the conflict is whether E is Effect or Effect.All. In any case with Nil you can instead write List.empty[Int]. It would be great if there was a short way to wrap values in DBIO that inferred a broader type (and was more concise).

Thanks!!
 

nafg

unread,
Apr 14, 2015, 2:45:33 PM4/14/15
to scala...@googlegroups.com


On Wednesday, April 8, 2015 at 10:25:27 AM UTC-4, nafg wrote:
Hi, I've been using (and enjoying) Slick 3 a bit.

I find that I'm using DBIO.sequence often, on long DBIO-composing expressions. I think the code would read a lot better if I could write expr.sequence, e.g., act.flatMap(...).sequence. Of course not every DBIO can be sequenced, so you could either do it the way scala collections .toMap works, with implicit evidence, or you could provide it as an extension method with an implicit class.

Thoughts?

Actually I would like to make a trade. `transactionally` should be a function DBIO=>DBIO like `sequence` is, and `sequence` should be a method like `transactionally` is (as above). I think both make could intention a lot clearer

Suppose I want to run 3 statements, 2 in a transaction. Which is clearer,

a >> (b >> c).transactionally

or

a >> transactionally(b >> c)


To go back to sequence, suppose I'm chaining a bunch of operations. Which is clearer,

DBIO.sequence(
  x
    .map(f)
    .flatMap(g) // g yields a DBIO[Seq[A]]
    .map(_ map h) // h is A => DBIO[B], now we have a Seq[DBIO[B]]
) // so we sequence the result

or

x
  .map(f)
  .flatMap(g)
  .map(_ map h)
  .sequence


Anyone have any opinions?

Naftoli Gugenheim

unread,
Apr 20, 2015, 4:19:28 PM4/20/15
to scala...@googlegroups.com, Stefan Zeiger
Stefan, could you comment?

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/d3570425-525d-418f-9adb-151b07011cf8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matan Safriel

unread,
Apr 23, 2015, 6:56:55 AM4/23/15
to scala...@googlegroups.com, stefan...@typesafe.com
I don't have an opinion, but I'm always for api engineered for usability. My unfortunate comment is that scala.concurrent takes the same approach in its "Future.sequence(..." api. Curious how would you personally rewrite sequence for accomplishing a chaining-styled api as in your suggestion?

Naftoli Gugenheim

unread,
Apr 23, 2015, 3:49:31 PM4/23/15
to scala...@googlegroups.com, stefan...@typesafe.com

It would either take implicit evidence that it's a type that can be sequenced, similar to Scala collections .toMap. Or it would be enriched on with an implicit class, so you just move the type constraints there.


Reply all
Reply to author
Forward
0 new messages