5 ms·
The article picks on Javascript (of course) however you can write almost exclusively functional code in Javascript with the help of Rambda, fp-ts or the like. Y
by halfmatthalfcat 4y ago
The article picks on Javascript (of course) however you can write almost exclusively functional code in Javascript with the help of Rambda, fp-ts or the like. Yes, there is no "compiler" (outside of tsc) that will help you (yet) but stylistically, it's possible.
- chrisseaton 4y agoYou can write exclusively functional code in pretty much any language can't you?
- spion 4y agoGo is probably one popular exception.
- dottedmag 4y agoYou still can if you wish to. First-class functions are there.
- brabel 4y agoIt used to be impossible to do anything remotely like FP in Go before generics, but now I believe it's indeed quite possible.
- chrisseaton 4y agoBut everything you can do with generics you can do manually by copying and pasting code?
- brabel 4y agoOh that's true. But you're a terrible masochist if you consider that an option.
- spion 4y agoLets take `map` for example. Before generics, you would have to reimplement map for every type combination. Each implementation would be done with a for loop. Now, while I'm willing to look over having one `map` implementation being done in imperative code and then everything else using that, I'm not exactly comfortable calling reimplementing map with imperative code for every type combination functional programming.
- spion 4y agoOr lets look at persistent data structures, a staple of functional programming: https://github.com/tobgu/peds https://github.com/tobgu/peds Notice how you'd need to generate the DS for every type you'd like to use it with, which is not the case with built in mutable maps and slices. To make them type-safe, you need to generate them for every type you use. While this is technically possible, it does make the language quite hostile towards functional programming. With generics, this is rectified but the problem with non-composable multi-return-value functions still remains
- spion 4y agoI wish that were true. Unfortunately, functional composition breaks down when functions have multiple return values, as is the case for everything that returns an `err, res` pair. So even with generics added, Go is still hostile to functional programming. If Go gets a Result<T> generic type similar to the one in Rust to replace (err, res) that would work. That seems highly unlikely, however. The node.js community went through a similar migration from (err, res) callbacks to promises and it was quite painful, I think its highly unlikely this will happen in Go.
- dottedmag 4y ago(res, err) is Either monad, isn't it?
- spion 4y agoIts not a single value, so its not anything.
- spion 4y agoThat may've sounded overly harsh - but as a first step, try for example to use the output of a Go function that returns two values in another expression. e.g. assuming `g` returns (err, res), try writing `f` in such a way that you can call `f(g(x))`. This has implications on how higher order functions like `map`, `filter` etc can be written in a way that makes them usable with any value (including those that represent an error)
- dottedmag 4y agofunc composeE[T](fn1, fn2 func(T, error) (T, error)) func(T, error) (T, error) func liftE[T](func(t T) T) func(T, error) (T, error) ? I'm not a big fan of functional programming though.
- spion 4y agoI think you might want the opposite of liftE, i.e. to be able to convert any function with two returning values into a function with a single returning value. Then you can use those functions normally with standard FP tools such as `map` etc. Yes, the node.js community did something like that with (err, res) and it still does it. Its pretty awful to be unable to use any of the standard library or community library functions (that can error) in a... functional way without wrapping them.
- nuclearnice1 4y agoReminds of https://en.m.wikipedia.org/wiki/Greenspun%27s_tenth_rule https://en.m.wikipedia.org/wiki/Greenspun%27s_tenth_rule > Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.
- dmitriid 4y agoJavascript is already functional enough without these libraries. Rambda et al make simple code unnecessarily complex or verbose, and complex code unreadable
- jvanderbot 4y agoThere is nothing magical about functional programming, it is the elimination of non functional programming features that is important. A language that can do either is exactly the wrong thing, from the perspective of TFA
- jve 4y ago> There is nothing magical about functional programming How come not? I read that F# gives you compiler exception when you didn't match all possible values. Or when you didn't handle __maybe__ cases. JS even doesn't mind comparing strings with ints and incorrectly summing them together and not throwing a runtime exception less alone a compiler complaint. "1" == 1 true "1" + 1 '11'
- dmitriid 4y agoThese are peculiarities of type systems, not of functional or non-functional programming.
- jve 4y agoWell, we are talking about functional programming improving program correctness. Such behavior doesn't help. Better choose another language that is a fit for it.
- jvanderbot 4y agoYou can add error checking (e.g., compiler exceptions), but you cannot remove the parts of JS/other languages that cause that error checking to be important in the first place, not without fundamentally breaking the language/existing libraries. TFA describes a paradigm where those features don't exist, and so the error checking is not important. That's a truly safe language. It's also hypothetical at this point, IMHO.
- innocentoldguy 4y agoI view this as one of JavaScript’s myriad flaws. Adding “1” to 1 is nonsensical and the result of “11” is even more so.
- ramesh31 4y agoYou don't need any libraries to write functional JS. Modern React development is almost entirely purely functional.