Get the latest tech news
Boilerplate busting in functional languages
This is the story of how I solved a problem (ugly, cumbersome boilerplate code) that I ran into while writing a program in a functional language (Elixir). Functional programming languages often pride themselves on expressiveness and elegance; but occasionally they are not amenable to the most obvious solutions to the problems we wish to solve. In this case, the simplest solution to my problem would have been to have a global mutable variable.
The code is messy because there are actually two competing concerns here: we have some main computation that we care about (transforming the tree) and some side information(the set of database queries) that we’d like to collect in parallel. 🤩 Our threading-related functions are actually generic enough that they don’t need to be about ASTs and lists of queries—as long as we are doing some main computation with a little extra data gathering on the side, this pattern should apply. Some of these (like the IO monad) are pretty specific to Haskell and other pure functional languages that don’t support the same kinds of control flow or constructs; others have wider application.
Or read this on Hacker News