Skip to content
0:00 / 0:00

#doobie#scala#jdbc

SBTB 2015: Rob Norris, Programs as Values: JDBC Programming with Doobie

FunctionalTV

10K subscribers • 19 videos on Hallway

YouTube
  • Recommended by 6 people: 6 in engineering communities.
  • A classic. People kept recommending it from 2016 to 2023.
  • Presented by Rob Norris, creator of Doobie

Intermediate. Assumes working knowledge.

9.2K views11 years ago6 recommendations

Programming languagesDatabases and dataScalaFunctional programmingSQL

Rob Norris explains how to treat database programs as values using the Doobie library in Scala.

This talk video is a node in devreal.ai, the developer community graph. It connects to: Talk: Programs as Values: JDBC Programming wit... — devreal.ai/t/150815-programs-as-values-jdbc-programming-wit-… Speaker: Rob Norris — devreal.ai/n/rob-norris Project: Doobie — devreal.ai/p/doobie Event: Scala by the Bay — devreal.ai/e/150814-scala-by-the-bay-conference Explore these connections and related videos, photos, speakers’ blogs and podcasts, and other By the Bay networks. ----- FP is often glibly described as "programming with functions", but we can equivalently say that FP is about programming with *values*. Functions are values, failures are values, effects are values, and indeed programs themselves can be treated as values. This talk explores some consequences of this idea of programs-as-values by looking at *doobie*, a pure-functional database library for Scala. We will examine the low-level implementation via free monads over algebras of the JDBC types, and show how this naturally enables an expressive and safe high-level API with familiar, lightweight, and composable abstractions including streaming result sets, and trivial compatibility with other pure libraries like Remotely and HTTP4s. Even if you are not in the market for a database layer, this talk will provide a practical outline for a general solution to dealing with terrible APIs (like JDBC) in a principled way. By day Rob writes software for big telescopes, and by night he hacks on open-source FP libraries, mostly in Scala. He is a regular irritant on IRC and Twitter where he goes by the handle tpolecat. Rob lives in Portland with his wife, daughter, and cat, all of whom are very good sports.

Why it's here

6 people recommend this

What people say. Recommended as an excellent explanation of monads, free monads, and how to design functional DSLs for database access.

Moments people linked to

u/tpolecatTypelevelin r/scala • Recommends

On “What is the FP way to model an object that is unavoidably mutable, like an http connection or something.”

The dense answer is: create a data type defining the operations you would like to perform *if you had a db connection* or other dangerous thing, then write your program in terms of the free monad over that type. This gives you a pure API that prevents leakage and forbids operations that you don't wish to expose. You can then interpret this program into a Kleisli arrow (a function `Connection => MA` for some monad `M` (`Task` or `IO` typically)) and run it to actually perform your real work. This isolates direct handling of the managed resource. I gave a talk about this strategy a few years ago. You may find it helpful. Note that the Coyoneda encoding I mention isn't necessary anymore ……

u/mn-haskell-guyin r/haskell • Recommends

(youtube.com) SBTB 2015: Rob Norris, Programs as Values: JDBC Programming with Doobie

u/chuwy24in r/scala • Recommends

On “How much functional is enough?”

I'm totally agree with /u/caente. Just want to add few things. First of all it takes some time to get used to it (same as any concept in programming!), but when you actually start to "feel" it - you will be wondering how you lived without Monoids, Applicatives, State, Free Monad etc. At least that happened to me. For me also it was a long road (and I still cannot see its end), but I enjoyed this "clicking" moments when after fourth rereading of some article I at last understand some concept. So, it's worth it for me from both pleasure and practical point of view. I also think if you want to see benefits of pure FP approach you should start to use some libraries that embrace this approach.…

u/Froyoforeverin r/scala • Recommends

On “Building my first DSL. Query language + object manipulation.”

You might want to check out rob Norris's doobie for some inspiration link to this video

u/paultypesin r/scala • Recommends

On “Modular, immutable, but extensible application architecture?”

…, and write any program you can write with any scalaz monad. You can't _run_ it without an interpreter (which is just a "natural transformation," or `~>`, from the free monad to some other monad we can run, like `IO`). But it's mechanical; the compiler and library do the heavy lifting. Really, I'd go through Rob's slides first, or maybe watch his link to this video, which is awesome. Then go through Rúnar's presentation, which is layers of awesome on top of awesome. :-)…

u/ResidentAppointment5in r/scala • Recommends

On “Seriously: What is the best explanation of Monads for people who can't grasp it?”

What finally cracked them for me was Rob Norris excellent Programs as Values: JDBC With Doobie link to this video Slides here. The point of monads in purely functional programming is to express data dependencies on computations. When you write a `for`-comprehension with a series of expressions that produce `Option`s, any expression can refer to a value named above it. That’s what “data dependency” means. Now add the fact that some monads, such as cats-effect’s `IO`, can produce their result as a consequence of some effect, such as I/O. Why bother? Because monads are just one element of purely functional programming that enables algebraic reasoning about our code, making its behavior, and the corre…

u/tpolecatTypelevelin r/scala • Mentions

On “Can I make my DSL do this with an implicit?”

`writeData` is not in lexical scope where it's being used, so what you're expecting here won't work. One option would be to make it a method on `Channel` and then use the loaner pattern to allow something like `withChannel { c => c.writeData(...) }` or even `withChannel { implicit c => writeData(...) }` but you can't make `c` invisible this way yet … implicit function types in future Scala will allow this and, more importantly you can't prevent it from being leaked using this approach. A more principled way to do this is to hide the channel in a monad. This is how doobie and Slick pass database connections around without providing a direct reference that could be leaked. The free monad is…

u/paultypesin r/scala • Mentions

On “In my second blog post I wrote about monads. Hopefully it helps someone. Let me know what you think”

I was just going to link to the monad tutorial fallacy. I'm glad you beat me to it. :-) The one-sentence summary that works best for me is "monads are programmable semicolons," which is to say, they sequence operations. What operations? Any operations, hence the "programmable" part. I also think it might help to just move on from the question and ask a more "practical" one: if I want to _create_ a monad for myself or others to use, how do I do that? tpolecat's Programs as Values presentation does the best job of this I know, grounding everything in JDBC `ResultSet`s, giving us a monad for free (another ha-ha-only-serious: it's a free monad). He spends no time on the monad laws, burritos,…

u/laiebcrkabin r/scala • Mentions

On “In Scala what is the exact meaning of the word Algebra.”

…t, but here's one small example - you could create a DB library that uses an algebra. You could then run the algebra against an actual multi-TB database in production, and you could run the algebra against a mock interpreter when testing. Here's a section of a presentation that talks about using an algebra for a DB library and shows code examples: link to this video Some other related links: * https://softwareengineering.stackexchange.com/questions/242795/what-is-the-free-monad-interpreter-pattern * http://underscore.io/blog/posts/2015/04/14/free-monads-are-simple.html…
2View on Reddit