5 ms·
In part 1 they talk about everything being a function including react components and closures. But I come from the "closure is a poor mans object and a function
by discreteevent 6y ago
In part 1 they talk about everything being a function including react components and closures. But I come from the "closure is a poor mans object and a function is a poor mans closure" perspective. In other words everything is an object. This is most likely because I started programming in the early 90s when first-class functions were not that common in popular languages (C had function pointers but it wasn't pretty)
But either way it works fine. Everything is a first class 'thing' that has properties that can be parameterised. If the 'thing' is an object the properties are alive and can be mutated (if mutable) for the lifetime of the object. If the 'thing' is a function the properties (arguments and local vars) are alive for the duration of the call.
When first class functions became mainstream though, I couldn't really understand the idea that they were fundamentally new compared to objects (in fairness this was only put forth by a few people)
- obedm 6y agoThank you for reading both articles! (author here). When I write these sometimes-overly-simplified-articles I'm terrified of explaining something wrong, or that my knowledge may be incorrect on a very specific point. I really like the way you express "everything is a first class 'thing'", I never thought of it that way.
- jlelonm 6y agoFWIW I thought they were both pretty spot on. In fact, you mentioned this about DNS: "As I read articles and tried stuff out (and broke my server config more than once) I started to grasp the system, to get glimpses into how it all worked, until eventually it “clicked” and I felt comfortable working with it." Have you written about your mental models for DNS? Color me interested!
- obedm 6y agoThat's something I hadn't considered before. That's probably something that a lot of people would find valuable. Out of all the questions my students ask me, there's always confusion about the way servers talk to each other, or what they even are. I'll definitely write about this now haha. Thank you for the idea :)
- pacala 6y ago"closure is a poor mans object and an object is a poor mans closure" is true in principle. Devil's in the details. If the language forces the declaration of the object equivalent of a closure in a different scope, or even a different file, then massaging captured variables by hand, it makes objects a whole lot more laborious to use, especially for those using rapid iteration to get a feel of the design space.
- jayd16 6y agoI can see why objects and closures are reducible to each other. As a Java dev who's written listener objects, closures seem like simple sugar to me. They hold private state or references to state. Functions can be passed references to mutable data so they are equivalent as well. However, there is the concept of a pure function (one that is unaffected by and causes no side effects) which I think can be argued is fundamentally different.
- Quekid5 6y agoI lean towards closures having primacy. FTR, I learned OOP way before I'd ever heard of functional programming. My reasoning is based on two things: Firstly, objects have identity and this is a superfluous notion for most cases. Functions just "do things" to their inputs and return the result. Secondly, OOP is fundamentally predicated on the notion of mutation[0], but I think that is totally antithetical to local reasoning (chunking). The latter may just be a preference, but IME it seems to bear out. Mutation seems to be a consequence of the focus on identity, now that I think about it after writing that footnote. > I couldn't really understand the idea that they were fundamentally new compared to objects (in fairness this was only put forth by a few people) Just a historical note: First-class functions were invented waaaaay before OOP was a gleam in somebody's eye. [0] The only non-mutating OOP system I know of is O'Camls version of OOP, but it's not very popular because it's just... unnecessary. It also does away with "identity", now that I think about it.
- 7786655 6y agoIn Javascript, closures both have identity and are mutable, which is a huge pain when using React.
- Quekid5 6y agoI realize I was quite off-putting in my original reply, but "have identity" in what way, exactly? A memory address?
- Quekid5 6y agoJS... the "winner" of the Accidental Complexity game. (It was made in a couple of weeks, so it's excusable, but... it just hurts so much that this was the language that conquered the web :| )
- eitland 6y agoYes, and realizing there is probably a larhe subset of people who'd say they love JS but hate PHP can make a man who know both too well confused. Or at least aware of the hypocrisy ;-)