r/functionalprogramming • • May 29 '23
Question Do you do full-on FP in JavaScript? Want it?
I've watched a lot of talks, but it was Rich Hickey's which most captivated me and, ultimately, inspired big change in how I coded. After discovering Clojure I was so desiring FP (i.e. ClojureScript) in the browser without a build step and hoard of dependencies that I wrote my own library.
And this fascination has not been a fad. When I first discovered Ruby I was enamored with the language and equally impressed with what the community was doing. I learned a lot!
Clojure changed my perspective in a bigger way. It so clicked with my way of thinking it became my preferred methodology, even in JS.
I'm a web guy. And the browser is my canvas. So JavaScript is my bread and butter. But I've been dying to have more FP goodness in JavaScript. I've eagerly awaited pipelined and partial application (affording tacit style) and records and tuples (immutability all the things!). If it gets first-class protocols it'll be near full-on Clojure in the browser!
I've experienced firsthand that FP is a portable, language-agnostic paradigm. All a suitable language does is provide facilities. Clojure, like Ruby, could've been written for OOP, but Hickey favored immutability.
Well, I'm an FP guy who does mostly JS. But I carry that mindset into all my JS work. I'm wondering if there are other JS devs, who similarly carry the FP mindset into their work. I don't mean just a smattering of LINQ-style pipelines but a true separation of the pure from the impure. What do you bring to the browser (or Node/Deno/Bun) to get your FP on in JS!?
And quick aside. Command-Query Separation. This principle is esp. suited to FP, right? Is it something which plays heavily into how you think about and write code? For me, it's a resounding yes!
I'm aiming to propose Command Syntax in JS. It leans heavily on CQS and FP thinking, but in discussions with JS devs I rarely sense an affinity for FP. I feel like I'm speaking to a community with different cares.
I'd like some perspective from FP-minded JS devs. Understanding why this does or does not resonate with you will be especially valuable for my understanding.
Thank you.
12
u/toastal May 29 '23
If I want full-on functional programming, I choose a compile-to-JavaScript option that matches the ergonomics, idioms, and features for effective FP. There are hundreds of options from PureScript, js_of_ocaml, ReScript, derw, ClojureScript, LunarML, GHC has an experimental JS backend now, etc. etc. etc. Any Turing complete language can technically do any style so what matters is the ergonomics, where JavaScript (and TypeScript) hinder terse code with idioms that are easy for teammates to follow as well as yourself in the future.
10
u/josephjnk May 29 '23
I was die-hard into FP in JS for a few years, but I don’t think it’s a good idea to go all-out with it. I don’t think JS is generally a good FP language, even though FP approaches are valuable in it.
It sounds like you’re more interested in lisp-style, untyped FP using bags of values than Haskell-style algebraic FP using algebraic data types. The former is more doable in JS with library support, but I don’t personally enjoy hitting constant type errors or having no good way to document and design interfaces. Untyped JS also becomes very hard to maintain once a team has more than like 10 people on it.
I’m glad that you’re getting into FP. JS is a good on-ramp onto it, and Rich Hickey was also one of the main sources that got me into it originally. If you want to go all-in on FP, though, I don’t think JS is the right language to do it in.
6
u/imihnevich May 29 '23
I apply FP a lot, when I write something just for fun I use Haskell, if I write something for fun, but JS compatible I go with Purescript or Rescript. But when I work with a team, I just write things in TS as if I had to write it in Haskell
5
u/jherrlin May 29 '23
Eric Normand, a prominent person in the Clojure community wrote a book on FP and the book is using JavaScript to describe all the concepts. It’s a very good book in my opinion and a must read. https://www.manning.com/books/grokking-simplicity
4
u/hosspatrick May 29 '23
I love to apply a declarative functional approach to my JS when I can, but I can’t say I’ve been able to go “all in” within a JS codebase because of the teams I work on.
On personal projects, I’ve used Elm and I love it.
Im no fp expert, but my opinion is that, while I agree that even within a multi paradigm language, if you have a certain set of features you can write functional code. It just seems to me that going all in is just much more effort in a multi paradigm lang than a dedicated one, to the point that I feel like if you had the right circumstances to attempt all in FPJS (team buy in, the right sort of project, etc), you’d maybe be better off committing that effort to something compile-to-js.
With that said, I’d love to see how one might approach a vanilla JS, FP setup!
If you haven’t consumed any of his material, I’d definitely check out Kyle Simpson. Especially his FP based work. I thought his functional light JS book and FE Masters course we’re both phenomenal. He also has a vanilla js monad library that would surely be worth a look. He seems right up your alley as a vanilla js and FP inclined programmer.
4
u/eccentric_j May 31 '23 edited May 31 '23
I feel very similar to you! I love FP and keep waiting for the pipeline operator to get ratified. ClojurScript is my favorite language currently and I would highly recommend giving node-babashka a shot since it is like a ClojureScript node interpreter that doesn’t require a build setp. Then there’s scittle which uses a similar core but supports writing browser ClojureScript also without a build step.
That said, still hoping to see more FP concepts make their way into JS.
EDIT: Have not looked at atomic before. Gonna have to give it a try!
3
u/Accomplished_Play254 May 29 '23
I don't know half the terminologies being used in this thread, but I'm highly interested to learn. I have a general idea of functional programming like pure functions, side effects, did some proofs in F# in uni and that's it. Would love to drive deep... any directions?
3
u/martingronlund May 30 '23
FP in JS allocates more memory at runtime, meaning you will hit GC sweeps earlier and more often.
Additionally, it runs slower.
You may also get stack overflow during recursion because of a lack of tail call elimination.
The standard librar(ies?) are not built with FP in mind, so most of the time you have to wrap existing functionality in yet more functions, which increases code size and delays time to interactive (all JS must be parsed before execution may begin).
Please consider performance and how the computer runs the program.
JavaScript is just not a very well designed language. The reason it's popular is because the V8 team managed to make it fast, which enabled giving birth to Node.js etc. Please don't spoil that ungratefully.
If you can, compile an optimized functional language to WASM.
Don't use untranspiled JS. Including JSDoc types and whitespace etc just bloats code size and makes your application start slower. Optimize e.g. using Google closure compiler, like ClojureScript does. Minify and tree shake etc.
Bottom line, please consider the computer and the end user, and avoid self-absorbed navel-gazing.
2
u/[deleted] Jun 04 '23 edited Jun 17 '23
Te doctus fuisset eam, sale melius pertinacia et eam. Ex iudico sapientem vel. Etiam regione appareat nec cu
1
u/lingdocs Jul 06 '23
I try to do as functional as possible and work mostly in TypeScript. I love it. (And I could never go back to plain, dynamic JavaScript.) It's helped my tackle stuff like generating and forming sentences from syntax trees in a complex natural language. But recently I've learned a lot of Haskell and it makes me long for something a little cleaner. (Algebraic data types right up front instead of having to scrap around identifying and narrowing types based n what the structures look like, currying and passing around infix functions etc.)
0
u/[deleted] May 30 '23
[deleted] — view deleted comment
0
u/[deleted] Jun 06 '23
[deleted] — view deleted comment