6 ms·
How to deal with dirty side effects in your pure functional JavaScript
- tempodox 8y agoAs long as JS lacks partial application and the ability to define infix operators (for monadic `bind`, etc), it's always going to be ugly. Not that JS would be a good language for that to begin with. We need a serious functional language that compiles to WASM.
- lucideer 8y ago> We need a serious functional language that compiles to WASM. Aren't there already a few? OCaml? What's "serious" in this context?
- TheAsprngHacker 8y agoReally? If OCaml has a compiler to Wasm, I’m not aware of it. I’m only aware that it compiles to JS (via js_of_ocaml or Bucklescript). Could I please have a link to the Wasm compiler if you have it? I read somewhere that an OCaml to LLVM compiler was attempted, but people had problems with the GC aspect. I don’t know if this claim is true, though.
- lucideer 8y ago> I once read that an OCaml to LLVM compiler was attempted, but people had problems with the GC aspect. I can’t confirm this claim though. I was thinking of the same, but I hadn't/haven't used it so you may be right it never came to fruition.
- sanderre 8y agoWhenever I manage to find time to hack on it: https://github.com/SanderSpies/ocaml/tree/wasm-backend https://github.com/SanderSpies/ocaml/tree/wasm-backend. It compiles to wasm object files and links files with LLVM's LLD. My current challenge is ensuring that the stdlib correctly translates to wasm. The C parts are not translated yet - which means: no gc, no exceptions, no tail calls.
- Tade0 8y ago"Lacks partial application"? What about: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Meanwhile infix operators are just syntax sugar and can be emulated in other ways.
- beaconstudios 8y ago`Function.bind` is a fairly poor match to partial application because A) it will not execute the function if you provide enough arguments and B) it also binds `this`, meaning you have to pass NULL or some other value every time you use it. https://ramdajs.com/docs/#curry https://ramdajs.com/docs/#curry would be a better option, as JS' metaprogramming features actually do somewhat support partial application.
- Boulth 8y agoPureScript compiles to JS but it's quite readable JS: http://www.purescript.org/ http://www.purescript.org/
- k__ 8y agoHow come that PureScript feels so mathematical to me and Reason/OCaml don't? Aren't they both FP languages?
- chenglou 8y agoJust hard work. Making it look natural and pragmatic is a non-trivial discipline and dare I say, rather thankless work. (I’m assuming you meant to convey a slightly negative connotation through “mathematical”. Though I myself do like PS too)
- foxes 8y agoPeople are working on making Haskell compile to web assembly. [0] https://www.tweag.io/posts/2018-05-29-hello-asterius.html https://www.tweag.io/posts/2018-05-29-hello-asterius.html [1] https://github.com/tweag/asterius https://github.com/tweag/asterius
- lalos 8y agoThis is actually a pretty good introduction to functional programming, was not expecting it from the title. Awesome.
- ulucs 8y agoI was left wondering “Isn’t what he calls Effect really similar a Haskell Monad?” for a large part of the article. I was really glad to see my novice FP insticts were right!
- xg15 8y agoPure functions have of course a lot of benefits - if the goal is computing a value. However, why would you even want to compose an interactive application (in javascript) fully out of pure functions? E.g., suppose you have an SPA that pulls data from a number of Rest endpoints, then does some transforms on that data, produces some HTML and eventually updates the page DOM accordingly. It makes sense model the "transform" and "produce HTML" steps as pure functions, because they are effectively side-effect free calculations. I think it might also make sense to cheat and treat the Rest calls as pure functions, because this will let you find out quickly in which order you should do them and which of them you can run in parallel. But why would you want to model the eventual update step itself as a pure function?
- jarym 8y ago'cos all the kewl kids are doing it! I agree with you, functional programming and pure functions have a lot to offer but they have a place and as soon as you start pushing them into every corner of an app you end up just creating problems for the future.
- zsck 8y agoI don’t think that’s entirely true. A lot of the ideas talked about in this article and that most people have already become familiar with are not very abstract. If you tried to build a large application out of nothing but pure function application, of course your code would become complex. We need more frameworks or libraries to take some of these ideas and bring them together in a way that scales. Elm, Vue and React are examples of tech that are doing this and, I think, make functional programming extremely appealing.
- jarym 8y agoBetter tooling can help, but so can 'not' forcing everything to be side-effect free. Honestly, I'm getting downvotes for this but probably from the same types who thought CQRS or micros-services should be taken to the extreme and ended up totally crashing and burning. Why can't techies see an approach, appreciate where it can help, and apply it where its useful? Why does it seemingly have to always be an all-in-or-nothing proposition?
- joeberon 8y agoPlease change the font style
- almostarockstar 8y agoYeah, it was interesting for about 20 seconds and then I got to the first code block and my brow furrowed.
- frou_dh 8y agoTell him, not us.
- scns 8y agouse reader mode?
- kyleperik 8y agoI disagree with the notion that functional programs must never contain side effects. I think we should just be reasonable and only produce side effects when explicit, and never mix side effect code with data processing. We shouldn't have to rely on loopholes to satisfy any hard and fast rules. A lot of writing good code is just being reasonable and explicit. If you call a function to launch a missile, you'd expect that it's just going to launch a missile and do nothing else. So why are we so concerned that it contains side effects? What other option do we have? Logging is a bit different. For many, logging is probably a necessity to allowing you to look back and and understand context when things go wrong. But when you're programming functionally, I find that it's much easier to get a grasp on things compared to procedural code, which can become spaghetti much quicker. I also find that bugs are much less common, or otherwise much more obvious in functional code, negating the need for logging in the first place (for functional code).
- kyleperik 8y agoconst fOne = fIncrement(zero); const fTwo = fIncrement(one); const fThree = fIncrement(two); // And so on… Seems like this implies that each number relies on the previous. A bit confusing?
- carapace 8y agoOne thing you can do is expand your universe to include what would otherwise be "external" effects. Instead of reading from opaque file handles use three-tuple of (hash, offset, length). Now you have pure input. Instead of printing to a stream, modify a git repo. Now you have pure output.
- beders 8y agoLet's not forget that if you are not passing immutable data-structures into your pure functions, all bets are off. Unless you code very defensively, there's no way you can guarantee purity, especially in async programming. Given f(x) = y, if x is a regular JS object, you can't guarantee purity. It works many times because of the single-threaded nature of most JS engines, but the moment you do something async, you can't guarantee x hasn't changed. That's one of the many reasons I like ClojureScript. It offers interesting guarantees that let me write robust code.