I'm interested in becoming more familiar with functional programming as a paradigm, so I'm trying to introduce a more functional style to some of my projects. I'm struggling to understand how to handle side effects with a database.
I have some functions that kind of look like this:
db query + db query | + | | v | +--------->a() v | f(type)+--+ | +--------->b()
The trouble is that both
a are non-pure functions because they need to do database queries. I've seen some functional projects that work by having all the state in a single place and the rest of the application takes bits and pieces of state as function parameters. I can replicate something like that here by putting all the queries in
f for example, but since
b doesn't need the database queries used by
a, this would be really inefficient.
Is there a pattern for handling database access in functional programs?