3 ms·
Contrast, for a moment: var x = fib(a, b); with (let x (fib a b)) And count the delimiting symbols. I count 5 in the first example. In the second, I
by falcolas 9y ago
Contrast, for a moment:
var x = fib(a, b);
with
(let x (fib a b))
And count the delimiting symbols. I count 5 in the first example. In the second, I see 4.
Let's try something a bit harder.
function(a, b) {
return a * b;
}(6, 4);
vs.
((fn (a b)
(* a b)
) 6 4)
10 first, 8 second.
Yes, "why all the [symbols]" indeed.
- deleted 9y ago[deleted]
- bshimmin 9y agoFor your second example, your JavaScript is wrong. Let's compare the ES2015 JavaScript: ((a, b) => a * b)(6, 4); ...against the Clojure: ((fn [a b] (* a b)) 6 4) Not a lot in it, really, is there?
- lilactown 9y agoIf we're golfing... (#(* %1 %2) 6 4)
- smnplk 9y agoAnd a bit shorter version... 24 ;)
- mrkgnao 9y agoI wonder, does #(* %1) do anything sensible?
- kbp 9y ago> I wonder, does #(* %1) do anything sensible? It's a 1-argument function, so applying it to 2 arguments would give an error. Minus the arity error, 1-argument * is an identity.
- latte 9y agoClojure's * can accept any number of arguments, including zero (returns 1 in this case), one (acts as identity function on numbers) or more than two (returns the product of all arguments). This helps to simplify arithmetic expressions in some cases (e. g. polynominals) and allows to calculate a sum or product of any sequence, including empty one, using (reduce * xs)
- dragandj 9y agoThe clojure example is really (* 6 4) since * is already a function, not an operator as in most languages. On the other hand, you can trivially create a macro that would implement the exact es2015 syntax (or at least a very close match), but why would anyone do this?
- city41 9y agoQuantity is only one aspect. Structure and ability to mentally parse out patterns matters too. I don't mind Lisps and use Clojure regularly, yet I'd still argue your JS examples are easier to read than the Lisp ones. Heck even Clojure seems to agree, just the simple act of using vectors for various things (ie function parameters) helps with the readability ((fn [a b] (* a b) ) 6 4)
- falcolas 9y agoWell, vectors are a workaround for the challenges in creating homeoiconic lists that the JVM understands. And if I'm frank, the JS examples are easy to read because you're familiar with the syntax. When you first started to learn C style syntax, it was likely just as confusing. I've spent a fair bit of time with lisp now, and the parenthesis are no more or less confusing than any of the symbols used in other languages. They're just scope delimiters.
- city41 9y agoI disagree. The JS is easier to read because it has an easier to parse structure. I've been coding in clojure for several years now.