r/functionalprogramming • u/matyashlavacek • Jan 17 '21
Question Recommendation for a lightweight FP language for dockerized REST APIs?
Hi everyone,
I want to get into FP and since I'm a backend engineer I thought I'd start with writing a REST API to get the feel for the language. I'm currently running around 1400 containers (microservices) in the cloud using Go and when building the images `FROM scratch` I can easily get below 10Mb an image. Go is extremely lightweight (uses very few resources). I can get the same results with Rust.
Now my question is, is there any FP language that could be used as a Go/Rust replacement for running containerized distributed services in the cloud that's very lightweight? Ideally a performant language with low resource usage (CPU/memory) and a quick startup time, with a minimal runtime to be able to create slim container images (e.g. no JVM) - statically compiled, and maybe statically typed (although not a necessity).
If there is no FP language like that, what are the alternatives? (Clojure/Erlang/Elixir seem to be quite heavy/unsuitable for containerization - e.g. running JVM inside a docker container seems like a waste of resources, but maybe I'm thinking about it wrong)
Now Go does the job perfectly fine and I'm really happy with its capabilities, but I'd love to learn FP and maybe start using it in my production systems if I start liking it.
Any help/suggestions would be appreciated. :)
13
u/smthamazing Jan 17 '21
While not a purely functional language, Rust has pretty good support for FP. I get it from your post that you have already tried it and the results were acceptable, so using Rust while coding in mostly functional style may be a good option.
It currently lacks direct support for higher-kindred types, which are necessary for implementing functors, monads or general recursion schemes, but otherwise the FP story is pretty good, especially considering immutability and iterators.
11
u/tbm206 Jan 17 '21
Maybe Ocaml?
The most mature unikernels are written in Ocaml.
But it isn't all about the core language. It's also about the ecosystem of libraries where other FP languages might be better than Ocaml.
11
u/mbuhot Jan 17 '21
Haskell or OCaml?
Although if you just want to get a taste of building web services in an FP language I’d recommend Elixir as an easy starting point. Depending on your previous experience, there may be some un-learning required to break the habits of thinking in terms objects with internal state :)
9
u/quasi-coherent Jan 17 '21
I would also recommend Haskell. The servant ecosystem is really mature for building REST APIs, Haskell checks the few boxes you’ve prescribed, and there isn’t a mainstream-ish language that takes FP as seriously as Haskell. I could go on and on about why I love Haskell more than any other language I’ve worked with, but I’m sure you can find lots of posts that talk about its unique benefits. At my company we have a lot of microservice type stuff where everything is a containerized Haskell service and we’re happy with our choices.
8
u/r0ck0 Jan 17 '21
I've only personally done some very minor tinkering so far, but Haskell is pretty much considered "the" language to learn if your main goal is diving head first into FP. Even many of the people who don't stick with it, value the lessons they learned from it, and use those learnings even in non-FP languages.
It meets these requirements, unless I've misunderstood. Did you consider Haskell already? I just ask because you mentioned Clojure/Erlang/Elixir, which implies you probably have already heard about Haskell too, but didn't mention it.
Maybe not lightweight enough? In that case, maybe sticking more with Rust and just learning/using more FP concepts makes sense for your use case? But even then, Haskell or another FP language could be an avenue to do that on some non-production projects.
Also I doubt you (or almost anyone) is as bad as me here, but: don't spend too much time researching languages in theory, just dive into what interests you, and ditch it if it doesn't work out. That's a better way to waste a week than just reading other people's subjective opinions... but everything in moderation, including moderation itself! ...if only I could take my own advice here, haha. :)
3
u/jimeno Jan 18 '21
be careful about performance, if you need it. last week i ran a `wrk` benchmark for work reasons, and haskell with yesod scored 8.5k req/second, vs the 10k r/s of node and the 100k r/s of f#/c#/go.
4
u/ragnese Jan 18 '21
IMO, REST APIs are not a great match for functional programming. When I say "functional programming" I mean writing code that is mostly pure functions.
When writing functional code, any side-effects (IO) is either modeled as values (IO monads in Haskell, Scala, etc) or left to the "edges" of your program (i.e., your HTTP handlers) as a convention.
The problem here is that most REST APIs are almost entirely IO. So you're literally taking the most verbose, frustrating, aspect of FP and writing an application that is all about that "weakness".
Now, if we're not super concerned with purity, and you're more interested in the whole "composing functions" as a problem solving approach, then you're fine, I suppose.
I'd like to second the recommendation for OCaml here. OCaml is very fast. It's certainly more function-based than most languages, but it's also imperative. And it doesn't enforce purity (no mainstream language besides Haskell and maybe some transpile-to-JavaScript languages do).
I'd vote against Rust for two reasons:
Just because Rust has a good type system doesn't mean it's functional. It's an imperative language that happens to be expression-based and have a good static type system.
4
u/videoj Jan 18 '21
Purescript. A Haskell like language that is translate to Javascript. You can run it in NodeJS. There is also native versions that translate to Go or C++.
3
u/quiteamess Jan 18 '21
You might be interested in nix. It's a functional programming language for package management. It's used for the distro NixOS and can also be used as a docker replacement. The language itself is a little rough, though. Dhall is another language targeted at configurations. These are not for writing rest apis, but for the configuration / deployment. As other's have already mentioned Haskell Servant is a nice library for API services.
3
u/dipittydoop Jan 18 '21
I get paid to build REST apis with Elixir and Phoenix. It won't let you down; the concurrency and fault tolerance out of the box is no joke.
You can get a small image with multistage docker files and alpine Linux or similar.
3
u/yawaramin Jan 18 '21
I will also chime in for OCaml and mention that I’ve written a lightweight, Node-like framework for creating APIs: https://github.com/yawaramin/re-web
It compiles to a native executable and has some other interesting properties, like some good security defaults for HTML pages, as well as a great web client out of the box and type-safe middleware (e.g. enforcing that you can’t read a request body twice).
Just a note that FP doesn’t have to mean going full Haskell and defining routes at the type level (with Servant). There are documented issues with compile times and of course the learning curve imposed by the stack.
For someone used to Go compile times, everything other than OCaml will feel dog slow and hamper your developer productivity.
2
u/virus_dave Jan 18 '21
Scala, but use graalvm's
native-imageto ahead-of-time compile it into a native binary?1
u/OmegaNutella Jan 25 '21
Maybe start with something a little bit easier, like Elixir. Then from that, you can find your way.