13 ms·
Can I do FP in my language?
- DylanDPC 9y agobullshit article
- squaredpants 9y agobullshit comment See? Easy to write :)
- uwu 9y agoyou forgot to say what's bullshit about it
- gaius 9y agoThe answer is "not if you don't have first-class functions" in your language.
- crimsonalucard 9y agoThis is not true at all. The only technical requirement for you to follow the functional paradigm is immutability. Keep in mind that passing a first class function into another function as a parameter is not that far off from passing the result of that function as a parameter.
- gaius 9y agoHow do you figure? map() for example, is similar to but not identical to for... next.
- crimsonalucard 9y agoYou can still use recursion to loop. First class functions are a convenience to functional programming not a requirement. Map and reduce and other higher order functions are also not a strict requirement for fp. Without first class functions an immutable program can still be turing complete. I would say that if a language only supports recursion and is immuteable, then that is all you need for the language to be part of the fp paradigm.
- fnord123 9y agoWhere did you get this definition?
- crimsonalucard 9y agoIntuition and experience. The definition naturally occurs to you over time, I've never seen the isomorphism between immutability and functional expressions illustrated though. However, it is explicit that every functional language is immutable as a requirement. I'm sorry to say that if you don't understand this, you haven't really understood what functional programming is... You're just a guy who understands higher order functions.
- jcelerier 9y ago> Intuition and experience. ... > I'm sorry to say that if you don't understand this, you haven't really understood what functional programming is... you can't be serious can you ?
- crimsonalucard 9y agoI am dead serious. If you don't understand this: >The only technical requirement for you to follow the functional paradigm is immutability. Then you don't understand functional programming. I can't take you seriously if you claim otherwise. Obviously, you highly disagree with me. Feel free to elaborate rather than waste my time with obviously rhetorical questions.
- jcelerier 9y ago> The only technical requirement for you to follow the functional paradigm is immutability. So are you saying that for instance both CaML and LISP aren't FP ? Because both are certainly not about immutability and you can write mutable messes in either.
- 9y ago
- pmoriarty 9y ago"passing a first class function into another function as a parameter is not that far off from passing the result of that function as a parameter" Not that far off unless you want to do partial application, currying, lazy evaluation, analyze, or change the code of the function before actually using it.
- steinuil 9y agoLike the article said, "functional programming" doesn't mean using maps and folds. Functional programming is meant to make you reason about state by always handling it very explicitly e.g. by using functions that take a state and produce a new one, so that your program is constructed by piping the result of a function into another's input, instead of implicitly.
- gaius 9y agoThat would appear to be a definition of functional programming that says nothing about functions themselves, which seems odd. Because you can't make any guarantees about state in a language that wasn't intended for FP because how do you know what side effects the built-in functions are having?
- steinuil 9y agoWell, clearly in a language that doesn't make a distinction between functions that have side effects and functions that don't you'll just have to pinky swear you won't put any in your functions, and maybe try to wrap the side effects the built-in functions do have (like errno in C) with your own functions. But that's not the point; a functional style can be achieved in any language as long as it has functions.
- crimsonalucard 9y agoYou can't even make that guarantee for functional languages. How do I know the built in operators for scala aren't producing side effects without reading the code of the compiler itself? Additionally you should know that functional programming is a lie. Your computer is an imperative machine with no understanding of functional programming. The compiler transforms your functional program into an imperative one.
- gaius 9y agoAdditionally you should know that functional programming is a lie. Your computer is an imperative machine with no understanding of functional programming Yet you program a register machine as if it were a stack machine all the time without batting an eyelid!
- randcraw 9y agoI'm very tempted to explore a coding style in C and C++ using a more functional idiom. I think both languages could benefit greatly if made more transparently 'intensional' -- more restricted use of in:out function arguments, explicit return variables, minimal pointer arithmetic, more explicit pointer expressions, etc. Has anyone attempted to devise a FP-style guide for non-FP languages? It could be fun to reimagine their now-antiquated idioms spoken in a more intuitive and/or legible accent.
- crimsonalucard 9y agoJust make every variable immutable. That is the only style guide you need to make the program part of the fp paradigm. The rest of the "functional patterns" flow naturally out of this restriction.
- joncampbelldev 9y agoIf the standard data structures in your language are mutable this will not save you. Luckily many languages like Java have nice third party persistent collection libraries
- crimsonalucard 9y agoYou can treat the variables as immuteable even if they aren't...
- whipoodle 9y ago> People say they're doing FP in JavaScript. What they mean is they sprinkle reduce and map around and they use some pure functions. There's a lot of value in that. But otherwise their code is procedural. They have not learned the paradigm, only the features. Yeah, but that's probably okay. I feel like there's a strong implication here that following the paradigm has inherent value but I'm not sure why I should think that's the case. Basically, I agree with the claim above that "there's a lot of value in that." :)
- crimsonalucard 9y agoI disagree with you. Map and reduce achieve the same result as a for loop so why not use a for loop? The end result is the same it's just a bit cooler or convenient to use map in place of a for loop, that's the only reason why you would use one instead of the other in a imperative program. The functional paradigm fundamentally alters the way the entire program is constructed, and you can do it without map reduce or filter. You really need to play with haskell or lisp to see the light.
- fnord123 9y agoThe result (return value) of a map is the list of results. A for loop has no result. So building up generic stream operators ((reduce (filter (map ...))) can't be done using for loops. It needs higher order functions or iterators.
- gaius 9y agoAdditionally the for construct implies that you want something done in a certain order, map implies all operations are independent and the order can be decided by the runtime itself. A program written in a language supporting map (which requires first class functions) can be trivially parallelised, whereas every for loop must be carefully analysed first.
- crimsonalucard 9y agoThen replace the for loop with a parallel construct that's imperative. In terms of the language itself, map, reduce and filter are just sugar... different ways to loop through collections. Using one does not mean you are using FP, using immutability in java means that you are. That is the difference.
- crimsonalucard 9y agoThis article is describing something that is very true but in the end it doesn't teach you how to do FP in an otherwise imperative language. This is surprising as it's just one single devastatingly simple rule you need to follow in order to do functional programming: After you declare and assign a value to a variable NEVER change it again. That's it. Immutability. Almost every other functional programming pattern flows naturally from this restriction. People already know the definition of functional programming and that is a functional program is a program that consists of a single expression and that the complexity of this single expression arises from the fact that this expression is the composition of other smaller functions... What they don't know is that if you take an imperative program and make every variable immutable and construct your program following this restriction the final result will be identical to a functional program! The two concepts are isomorphic! Try it if you don't believe me.
- kwhitefoot 9y agoI do this for my own code but 20 years of persuasion has failed to convince any of my colleagues.
- bradknowles 9y agoFP? Floating Point? I should hope you can do floating point math in just about any language. Yeesh. ;)