title | categories | ||
---|---|---|---|
There's more than 1000 ways to do it |
|
It's weird that in clojure there is more ways to do the same (at micro level) than in any other language I've found.
Let's see if in a map, both keys :a
and :b
are true.
(and (= true (:a h)) (= true (:b h))) ;
(= true (:a h) (:b h))
(apply = true [(:a h) (:b h)])
((over (partial every? true?) (juxt :a :b)) {:a 1 :b 2})
((comp (partial every? true?) (juxt :a :b)) {:a 1 :b 2})
(every? true? ((juxt :a :b) {:a true :b false}))
(->> {:a true :b true}
((juxt :a :b))
(every? true?))
(every? {:a true :b "a"} [:a :b])
over
is my attempt to have APL-y
over in clojure. But comp applies
there the same way. In fact, I'm thinking that variadic comp
does
the function of many many different combinators in
clojure. (blackbird is supposed to be
BBB, but it seems a
single comp
already does the job).
(defn over [g h]
(fn
([w] (g (h w)))
([a w] (g (h a) (h w)))))
On a similar topic, but now on Perl, where TIMTOWTDI is one of its mottos, I recently read The foreword to Learning Perl again, and how the language embraces the individuality and personality traits, and how they see the Perl community... I miss that nowadays.
Anyway, as another example of the approach to the language design, embracing little inconsistencies, but also embracing the amazing combinatoric power they give, here's a post about comma's precedence, and how it interacts with lists and parenthesis.
And I'm copypasting the comment from Aristotle here (in case blogs.perl.com disappears):
Well if someone really values consistency very highly, there is no way of selling them Perl and no point in trying. The design of the language does in fact – though it tries to avoid breaking consistency for no good reason – not treat it as a primary goal. If that is what you want in a programming language, well, then you won’t be very happy with Perl and why should I try to convince you that your misery is enjoyable?
OTOH, frankly, I don’t think Shawn’s position is very deeply reflected, which I was implying in the last comment. All languages have inconsistencies, and generally the popular ones strike a balance between convenience of various kinds and consistency of various kinds. (That choice is not necessarily conscious – just inevitable.) If consistency was that important, then nobody would be using PHP – yet it ate Perl’s big niche, in spite of being much worse in that (and many another) respect. (Oh so many others… so, so many.)
We can reasonably have a discussion about whether Perl draws the line too far in one direction or the other, and about whether any particular instance is out of balance with the rest of Perl. (I think it tries and very largely succeeds to strike a balance that few other languages attempt. That is what I can say to sell it!) But that requires cognizance of the fact that there is a spectrum and the extremes are rather sparsely populated – not just a binary.
And even then, again, if someone does value consistency much more highly than it is valued in the design of Perl, there is not much that can – or should! – be done to sell them on Perl. I’ve tried to work with Python, and I've tried to use Emacs. Both grate on me, they cut against my mental grain (in general ways with lots of very particular implications). Trying to sell them to me would be useless – I wouldn’t be won over, no matter what. (I’m fine using them if necessary, mind you: they are fine products and I can see their appeal. I just have peculiarities they are optimised against.) I’m keenly aware that likewise Perl and vi cut against other people’s mental grain.
That’s OK! I’m not of the opinion that every other language, editor, etc. has to lose in order for the ones I prefer to “win”.
I guess “maybe Perl wasn’t the best choice then” was also an invitation to reflect – admittedly, perhaps an overly coy one. “Will you be happy here then? And if you aren’t unhappy, how do you value consistency – truly?”