5 ms·
Or how about in Clojure? (map (partial reduce +) (partition 7 7 daily)) Edit: Possibly that should be partition-all from seq-utils to sum the remainder days i
by cubix 17y ago
Or how about in Clojure?
(map (partial reduce +) (partition 7 7 daily))
Edit: Possibly that should be partition-all from seq-utils to sum the remainder days if the length of daily isn't divisible by 7.
- tybris 17y agoThat's pretty unreadable for someone who doesn't know clojure.
- anc2020 17y agoIs this better? map(partial(reduce, +), partition(7, 7, daily)) Personally I'd be much much more happy to be using this code than the snippet in the article. In fact, I don't program Python regularly and it did take quite a while to figure out what was going on in the code. weekly = [sum(daily[j:j+7]) for j in range(0, len(daily), 7)] Now if you look back at the top snippet, its just normal function calls, so you can be happy to know what's going on after reading documentation for the names "partition" and "daily". If you don't know what "map", "partial", "reduce" and "+" mean, sure you have to look up their documentation too, but they occur frequently enough that you can remember what they do after that. The Clojure version is much more high level and less likely to introduce bugs. Edit: Oh and it should be said that "sum" is equivalent to partial(reduce, +) so I can now say: map(sum, partition(7, 7, daily)) See? The code is just melting away before your eyes. Also note how I have literally no idea what either of the snippets do but am already happily changing one of them.
- Confusion 17y agoJust because you already happen to have a certain function at your disposal doesn't mean your language is suddenly superior. def partition(n, step, coll): for i in range(0, len(coll), step): yield coll[i:i+n] allows weekly = [sum(week) for week in partition(7, 7, daily)] Using Python's map() function you can even do map(sum, partition(7, 7, daily)) making it exactly the same as your example. And no, I probably haven't written more Python than you in my life.
- itistoday 17y ago> Just because you already happen to have a certain function at your disposal doesn't mean your language is suddenly superior. You are certainly correct in that. That's not what makes Lisps superior to all other languages though, "it's the syntax stupid." Edit: Sorry, I know that comment appears trollish (and it is), what I meant to say is that unlike Python, Lisp(s) have virtually no syntax, and are more powerful because they treat code as data. It would take me a blog post to explain why that's important, but if you give the language a shot you will see why that is (and compare how long it takes you to learn it to how long it took you to learn Python!).
- jshen 17y agoIsn't there more to power than that? For example, in clojure you can't change the binding of a function in a namespace after it has been compiled. In python or ruby you do have the power to do that.
- itistoday 17y agoI'm not a Clojure expert, so I can't directly address that, but from the little that I know of Clojure though, I think you can do that by binding a function to a var, and then changing the binding of the var. (Can any Clojure experts exlaborate?) Regardless, every Lisp that I am able to comment on (newLISP, elisp, Scheme, CL) can do that easily, so I doubt that Clojure would have difficulty with this.
- jshen 17y agoIt does have difficulty with this, check the mailing list. Just because something using s-expressions doesn't mean you can override functions in a namespace. http://groups.google.com/group/clojure/browse_thread/thread/95c8ab4f60945451/09f5f27cb24ba0ae?q=dependency+injection&lnk=ol& http://groups.google.com/group/clojure/browse_thread/thread/...;
- itistoday 17y ago
- stefano 17y agoThat could be said for any language. It's just that the first language someone usually learns borrows more from algol than from lisp. This leads to the idea that Python/Ruby/PHP/Javascript are easier to learn than Clojure/Common Lisp/Scheme.
- timr 17y agoAlgol languages are taught first for good reason: Algol-derived languages resemble English; Lisp isn't natural or intuitive to anyone who doesn't already know Lisp.
- timr 17y agoOh, come on, folks. It's a perfectly reasonable comment -- Lisp is powerful and expressive, but it is not an intuitive language for beginners.
- jcl 17y agoPerhaps it seems that way to you because you are more familiar with Algol syntax than Lisp syntax, or because you have gone on to learn the more advanced Lisp features. But Scheme syntax and semantics, for example, are arguably at least as simple as that of an Algol language. Scheme has been used successfully as a first programming language in high school computer science courses: http://www.teach-scheme.org/ http://www.teach-scheme.org/
- timr 17y agoYou're making a bad assumption about my skill and preferences, and trying to use it as an argument. That's a logical fallacy. My argument doesn't depend on my personal experience: Algol-derived languages resemble English, and are therefore easier for new programmers to learn. While I am sure that some high school courses may start with Scheme, it's certainly not a common beginner's language. I'm sure if I searched hard enough, I could find a high school class learning assembly -- but that doesn't make assembly a good beginner's language, either.
- _pius 17y agoThat's pretty unreadable for someone who doesn't know clojure. And that Python is pretty unreadable for someone who doesn't know Python.
- yummyfajitas 17y agoThat specific python example is very readable for someone who understands basic math (sets and functions). The clojure example, not so much.
- deleted 17y ago[deleted]
- crc 17y ago> Possibly that should be partition-all from seq-utils As of June 20th, there is now an optional padding argument in clojure.core/partition.
- cubix 17y agoGood to know, thanks.