3 ms·
... or working in an environment or on a problem for which functional patterns apply. Suppose you are writing a "CRUD" app that writes to a relational databas
by PaulHoule 3y ago
... or working in an environment or on a problem for which functional patterns apply.
Suppose you are writing a "CRUD" app that writes to a relational database, how do you apply functional programming to that? The whole point of an application like that is that it makes side effects.
In some cases you can break those problems down into functional pieces. Consider Python drivers for a product like
https://www.arangodb.com/ https://www.arangodb.com/
One major problem is that you want drivers that work synchronously and asynchronously, the structure of the average api call is something like
def query(parameters):
encoded_parameters = encode_parameter(parameters)
url = generate_rest_url(configuration, parameters)
response = do_post(url, encoded_parameters)
return decode_response(response)
To make that async all you have to do is stick "async" before the def and put an "await" before the do_post. 95% of the API calls have a structure like the above. Most of the complexity lives in encode_parameter(), generate_rest_url() and decode_response() are all perfectly functional functions that are easy to test. Code gen the API stubs and you get sync and async switching almost for free. Contrast that to the option of mocking do_post.
- SoftTalker 3y agoI never really understood what a "monad" is but for a program like that I'd have all the "side effect" stuff separated from all the "data manipulation" stuff. It could all be organized as functions, which I generally do because I find it much easier to reason about mentally than objects. "Pure" functional programming is an extreme which probably isn't suitable for many real-world programming tasks. Functional style is typically what I do, I use lists and maps and folds quite a bit but still end up updating a database.
- PaulHoule 3y agoCode like the above can be restructured with monads in various ways. In general there is the pattern of building systems that do execution control such as the methods used to create control structures in Lisp, the Executor in Java, etc. One way to use monads is to accumulate a list of operations that need to be done and actually does the operations when you "unpack" the monad at the end. Such a monad could do many of the things a compiler does since it has the operation list at the outset and doesn't just blast from one statement to the next. This blends into the techniques used in Fluent DSLs.
- carlmr 3y agoUsually your database connection is a function that is passed as a parameter to the functions using it. That way you don't need to Mock, but you can input a "connection" during testing that does what you expect. If you say "well now I have to type extra parameters everywhere", this is usually solved with partial application. I.e. you create findings from your functions that have the Db Connection already baked inside. https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use-fsharp-at-work-4/ https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use... https://fsharpforfunandprofit.com/posts/partial-application/ https://fsharpforfunandprofit.com/posts/partial-application/
- quickthrower2 3y agoHaskell has the IO Monad which is like a magic type that is allow to have any side effect whatsoever. So you are free to write imperative code in Haskell. However what you might want to do (and this is based on IIRC!) is have a typeclass for your database interaction. Then the real DB type and the mock one can both implement that typeclass and you have basically dependency injection. There are other ways like free monads but I never got my head around that. You could also create a monad transformer which is a bit more straight forward. They let you compose monads, so you can chain little bits of side effect. Like “here is something that can talk to a database and write logs and that is all it will do” … but actually it is pure! It thinks it is doing those things but they can be mocked.