3 ms·
There aren't many languages that have radically fewer parentheses than Clojure / LISP. Clojure: (defn blub-extra [a b] (blub (inc a) (inc b))) 8
by afandian 12y ago
There aren't many languages that have radically fewer parentheses than Clojure / LISP.
Clojure:
(defn blub-extra [a b]
(blub (inc a) (inc b)))
8 parens + 2 brackets
Scala:
def blubExtra(a: Int, b: Int): Int {
blub(inc(a), inc(b))
}
8 parens + 2 braces
Java:
Integer blubExtra(Integer a, Integer b) {
return blub(inc(a), inc(b));
}
8 parens + 2 braces
Ruby:
def blubExtra(a, b)
blub(inc(a), inc(b))
end
8 parens
Python:
def blubExtra(a, b):
return blub(inc(a), inc(b))
8 parens, one colon
C:
int blubExtra(int a, int b) {
return blub(inc(a), inc(b));
}
8 parens + 2 braces
It's roughly the same numbers of brackets (or equivalent) in Clojure, Scala, C and Java. A bit less in Python and Ruby.
- forki23 12y agoas someone already pointed out on twitter: F#: let blubExtra a b = blub (inc a) (inc b) 4 parens.
- forki23 12y agoUsually we would write it as: let blubExtra a b = inc b |> blub (inc a) which 2 parens less, but at the cost of using the (very idiomatic) pipe operator.
- bjz_ 12y agoI think it's the positioning of them that makes them stand out to people, plus the fact that they tend to bunch up at the end of things.
- afandian 12y agoOf course, I know. But most people express it as 'too many parens', without (I believe) actually thinking too hard. I was just trying to provoke a few thoughts! The position of parens is something to get used to, but then again so is Python's indentation, Scala's type system, etc etc. Every language has something unique to get used to.
- gleenn 12y agoYou can do it in Ruby with 4 parens, or even 0 if inc is really just +1. I wouldn't ever write it with so few but if we're really going down this rabbit hole then we ahould get it right.
- afandian 12y agoPlease feel free to contribute a Ruby example (as long as it's normal and idiomatic, this isn't a competition)! I've not written Ruby for years. (inc was just a random function for the sake of showing invocation so it's not really fair to use operators)
- virtualwhys 12y agoScala example is wrong, wouldn't even compile; should be: def blubExtra(a: Int, b: Int): Int = blub(inc(a), inc(b)) The original would need an equals thrown in there, otherwise it's procedural syntax (IIRC, has been deprecated or will be in the next release) which has a return type of Unit, thus not compiling when specifying a return type of Int. def blubExtra(a: Int, b: Int): Int = { blub(inc(a), inc(b)) }
- afandian 12y agoThanks for the typo spot. I've not written Scala for about 5 years. Unfortunately I can't go back and edit my post.