4 ms·
I want to take a leap and talk about 2 core concepts that I got after spending last week learning Clojure. I'd love some comments about them. I've been writing
by geuis 13y ago
I want to take a leap and talk about 2 core concepts that I got after spending last week learning Clojure. I'd love some comments about them.
I've been writing javascript for years but functional programming didn't make sense until I realized:
1) It's essentially inverted callbacks. You can read and reason about your code by working from the inside out. The results of a function are passed up to the outside.
2) When you stop modifying variables and just pass them around, it becomes a lot easier to think about code in abstract ways. There are of course some situations where modifying a variable is needed, but it really feels icky and you think twice about doing it.
The problem I found trying to learn this stuff is that once you learn to think functional its hard to think like someone who doesn't get it. That makes it hard to translate concepts in ways that non-functional people can get easier. I'm keeping mental tabs on ideas as I understand them to hopefully help with this problem.
Also, macros. Clojure people love talking about them. They aren't explained well most of the time though.
Simplest explanation is that you can write a bunch of your own functions, even ones that will override native functions, i.e macros. Since clojure is a compiled language, at compile time all the source code gets read in and pre-processed. When the compiler reaches some code that was defined in a macro, it keeps rewriting that code until all the macros are processed and the code has been re-written. Then everything is compiled to bytecode for the jvm.
- cgag 13y agoI think inverted callbacks is a strange way to describe just standard function calls. Unless you also call 'a.b' a callback, I don't think 'f a' could be called an inverted callback. It's just a standard function call without any special treatment of it's arguments (like self in most OO languages). I agree with you that it's hard to remember not thinking functionally. I've only been doing it for maybe 3 years and it's hard to remember what it felt like when I was first trying to wrap my head around it. With macro's, I think you mean to say your own special forms, not functions. They allow you to take in code as arguments, and determine when that code will get evaluated, letting you write things like "or" as macros, when it most languages they have to be implemented as built-in primitives (special forms).
- geuis 13y agoSee, the way you think and try to describe the problem is right. But the point I am making is that often giving someone the correct answer doesn't help them make the mental connections they need to get the Eureka! moment of understanding about the fundamental nature of the topic. Just reiterating the same definitions over and over to someone is somewhat like talking to someone in Farsi who grew up in rural China and only speaks a local dialect of Mandarin. Sometimes we have to point at things and make funny baby noises so people can ramp up their learning. So when I said functions instead of forms, I did that because a) I don't understand the difference yet and b) that's not the point. I'm documenting how I, as someone new to both clojure and a stricter sense of functional programming, am starting to learn these concepts. Eventually I'll grok the Matrix and all of this will seem hopelessly simpleminded. But for me right now, this is where I am on the road.
- johncagula 13y agoYour use of the language "passed up to the outside" is interesting... Up and outside imply some sort of intrinsic directionality. Considering that functional programming seems to capture the mathematically inclined, can anyone speak to the concept of topology applied to code structure or interpretation? For instance, does a closure imply some sort of filtration structure on the "code space?" (by filtration here, I mean the concept from algebraic topology)
- geuis 13y agoOh man, you lost me. I'm definitely not one of the mathematically inclined. My analogies are rough and largely wrong and the only thing I'm trying to relate is the particular way of thinking that at a critical moment helped me understand a little more. I wouldn't put too much credence on the veracity of "up to the outside". If it does hit on some underlying important point, it's entirely by accident, I swear.
- tel 13y agoI think yes probably if you look at the parts of the AST subject to substitution of a particular name.