6 ms·
The example given isn't really functional programming is it? Or am I missing something? In those examples you start with an object, call a method on the object
by jemmyw 4y ago
The example given isn't really functional programming is it? Or am I missing something? In those examples you start with an object, call a method on the object returning a new object, then call a method on that and so on. I like this kind of chaining in OOP.
Languages like JavaScript and Rust (and Go?) have blurred the lines between purely functional and purely imperative. I think I prefer that compromise position myself.
- brundolf 4y agoMethod vs non-method calling syntax is a red herring; a method is just a function where you put the first argument on the left instead of inside the parenthesis Hanging functions off of objects/types isn't really anti-functional either, as long as they don't mutate the subject Functional programming is mainly about not changing state; you can do that with or without methods
- jemmyw 4y agoWell there's a certain anti functional feel to it because your functions are tied to the object rather than being able to process any given object or objects of different types. Difference between functional style and functional language?
- brundolf 4y ago> rather than being able to process any given object or objects of different types Not necessarily- the functions can still be polymorphic. Rust does a particularly cool thing here, where any struct or trait method can be called in non-method syntax (or even passed as a first-class function!). I believe Julia also has type-based polymorphism while still being considered a functional-adjacent programming language. And then there are other languages where any function in scope can be called in method syntax, without being explicitly associated with the value's type (there's a name for this feature but I can't remember it) > Difference between functional style and functional language? I don't know the precise definitions well enough to distinguish them (though I'm not sure they have precise definitions)
- jemmyw 4y agoI'm familiar with rust but not with Julia. One pitfall I can see with calling this method chaining style functional is that you might not be aware of when a mutation occurred. For example, I've made the mistake before of calling .sort() in js on an array chain and it mutates in place, which you don't really expect given the other methods. At least rust makes this explicit.
- brundolf 4y ago> you might not be aware of when a mutation occurred Yeah, this is a risk with any language that mixes some functional features/style in with non-functional features. Rust does a pretty good job of letting you mostly delineate the two, though there are still escape-hatches I've actually been toying with a language design that has both, but draws a hard distinction between them. We'll see how it works out :) But most of the time, if you're not using a totally pure functional language, functional programming is more a practice/style/ethos/feature-set than a hard constraint
- Hasnep 4y agoJulia has the convention that mutating functions have names that end with a bang, e.g. sort(x) returns the sorted object, but sort!(x) returns a "nothing" and mutates the object in place.
- valenterry 4y agoNo, those things are totally orthogonal and people just mix it up because OOP came with mutations. Bundling data and functions has pros and cons but has nothing to do with functional programming it. Also, there isn't really a good definition for either functional style or functional language. If anything, there is a good definition for pure functional programming, but that's another beast alltogether and still is orthogonal to bundling data and functions.
- sideeffffect 4y agoThere's nothing anti-functional in bundling related functions into an object. On the contrary, this helps with modularity, which is a good thing. You can then swap out the object for another object with the same interface, but where the functions (methods) have different implementations.
- galaxyLogic 4y agoAn often overlooked feature of OOP is the use of pseudo-variable 'this' (or 'self in Smalltalk). That is about name-scope. You can refer to your own method simply by its name via 'this', and when you call it you know it can access the state inside that particular object. Without 'this' an object in JavaScript and other OOP languages could not access its own properties including it (possibly mutable) state. Is there an equivalent to 'this' in purely FP languages?
- jemmyw 4y agoMan, the downvotes lol. If you disagree just write a reply, happy to be corrected or read others opinion
- Throwaway234590 4y ago[flagged]
- jemmyw 4y agoI didn't even disagree with anything, just asked a question! I'm not anti functional or anti imperative, they're both fine. My point was I don't think it's functional to chain method calls in JavaScript. Maybe it is, although I think you'd very easily run into an unintentional issue, i.e. call .sort() on the array which mutates it.
- Throwaway234590 4y agoIt's a hairpulling experience trying to write functionally in javascript. All those imperative apis like sort as you mention. I will sometimes copy as in [...xs] but I always feel like I am swimming against the current. Plenty more to say but it appears this node has been flagged, it appears HN censors criticism. Saddening.
- freedomben 4y agoBeing dead doesn't censor anything. After all, I'm here reading and replying 10 hours later. It just makes it invisible to people without "Show dead" enabled. Your post most assuredly got down voted to death for calling "HN" "toxic." That's not constructive at all, and it inflames emotions which is the opposite of HN's goals of technical discussion. I've taken many positions against the HN herd and yes many people do downvote on disagreement (which I agree they shouldn't), but given a bit of time it will typically even out. The new vouch system helps this even more.
- 4y ago
- deleted 4y ago[deleted]
- theteapot 4y ago> Languages like JavaScript and Rust (and Go?) have blurred the lines between purely functional and purely imperative. I think I prefer that compromise position myself. The line blurring ones are the ones taking the comprising position right? So you're saying you prefer Javascript, Rust and Go?
- jemmyw 4y agoYes I personally find those languages to be the sweet spot between very object structured like Java and C# (although I haven't used either for 15 years so I can't speak to how they're currently used) and FP languages. Javascript a little less so, it's just very flexible and Typescript adds the typing layer. Rust especially seems to take just enough of the FP style to get some nice benefits while also being a traditional imperative language. Just my personal opinion of course, I'm not an advocate for any particular style.
- kaba0 4y agoBoth C# and Java have incorporated many things from the FP paradigm.
- eyelidlessness 4y agoIf a “method” returns a value, is it really a method? Does it mutate “this”? What does it mean to be an object, if you’re not being changed at all?
- galaxyLogic 4y agoIt does not need to mutate this but it can: 1. Read the fields of 'this' 2. Write the fields of 'this' 3. Call other methods of 'this' (including "private" methods in many OOP languages) That is very characteristic of OOP languages. Does anybody know if FP languages have something like the 'this' and if not what do they use instead?
- Jtsummers 4y agoYes, it is a method. It would be a “query” if it causes no changes but returns a value.
- Jtsummers 4y agoI wrote that other comment just before dozing off for the night because I was surprised no one else had replied yet. But to expand on it: In Eiffel (which is an object oriented language), methods on objects are separated into two kinds: commands and queries. This is also known as the "Command-Query Separation" principle. The idea is that commands are the ones causing changes to the systems and queries only inform you about the system. `a_set.add(item)` changes the set, while `a_set.has(item)` tells you about the set but makes no changes to it. It's very useful, and very common. I mean, I'd hope that `some_collection.Size()` doesn't actually change its size, that's not what it exists to do. https://en.wikipedia.org/wiki/Command–query_separation https://en.wikipedia.org/wiki/Command–query_separation
- kaba0 4y agoThanks, I wasn’t familiar with the name of this principle! Though I feel these definitions of OOP are closing on the Actor model, so exact definitions are not really possible to give here.
- shadowgovt 4y ago
- ookdatnog 4y agoFunctional and object-oriented aren't two ends of a spectrum, they're orthogonal. Code can be both. To program in a functional style means no more or less than to construct your program by composing referentially transparent functions (that is, functions that have no side effects, and whose output is deterministic based on the inputs -- in other words: the mathematical notion of a function). The solutions that are marked as functional in the post fulfill this criterium, so it's correct to call them functional solutions. Whether your functions are written in method notation or not is not relevant to determining whether the program is written in a functional style.
- jstimpfle 4y agoOOP is not method notation. There's more to it (and arguably the notation part isn't required), and indeed you can't have both that and FP. OOP is the opposite of "referentially transparent".
- sideeffffect 4y agoThen it really depends what exactly you mean by OOP. Some people use the OOP features for modularity and structuring of the codebase, without any side effects/unmanaged mutation. Often they don't call it "OOP" then, but rather "a module system". But there isn't any theoretical distinction. For example, I do _both_ Pure Functional Programming _and_ OOP (in the "module system" way) in Scala daily. And it works very nice IMHO, I encourage everybody to give it a try. Use traits as interfaces for logical pieces of your business logic. Implement them in (case) classes which receive interfaces to other pieces of logic in their class constructor. Use a so called "Functional Effect System" like Cats Effect or ZIO to avoid raw side effects. And that's it, enjoy and profit.
- jstimpfle 4y agoIt's impossible to get people to agree on what OOP is exactly, but almost everyone agrees that hiding/isolating mutable state (inside objects) is an essential part of it. Just google "what are objects in programming".
- vram22 4y ago>In those examples you start with an object, call a method on the object returning a new object, then call a method on that and so on. I like this kind of chaining in OOP. Then this post from my blog might be of interest: Examples of method chaining in Python: https://jugad2.blogspot.com/2016/02/examples-of-method-chaining-in-python.html https://jugad2.blogspot.com/2016/02/examples-of-method-chain...
- jemmyw 4y agoNot really, I already know what it is and use it all the time. I mean, I just wrote about it. Plus I don't program in python. Honestly, I just opened your page and it's not a great example. Consider the method chaining in the linked article that operates on an array, it's much closer to real problems imo, and more succinct.
- vram22 4y agoWhich linked article?