24 ms·
Shall We Use Clojure?
- th0ma5 14y agoPretty nice intro / advocacy piece for Clojure. I have high hopes that once we're flooded with more example code people will start to see the simplicity of Clojure. Right now the reference is somewhat terse, but you can dig around GitHub quite a bit which is nice. Also, it seems that the combination of people working with contemporary problems and small and succinct generics that you wind up making in Lisps, there could be a lot of great reusable code for this language out there soon. I'm also curious about http://www.collaj.net/ http://www.collaj.net/ ... the Redfoot project for RDF / Python seems to remind me of this... that we're not paying enough attention to the "Google / Paste / Execute" loop.
- spacemanaki 14y agoThis example is missing the corresponding Clojure code, which is important for Java interop: object.method(arg) // Java (.method object arg) ; Clojure I'm not sure if focusing on syntax and succinctness is such a great approach though. When I've discussed Clojure with colleagues familiar with Java, they've been more interested in the concurrency support via immutable data structures and reference types, which really sets Clojure apart from other Lisps. I think it's a much more compelling sell that way, since Java programmers seem to be attached to Java's syntax and static typing, but are aware of how difficult it is to write correct concurrent code. YMMV of course.
- Tuna-Fish 14y agoI agree. A good way to sell Clojure to an experienced Java programmer is to ask him has he ever serialized data to a string for passing it around in a program, and why. Then when he explains how the semantics for all the composite Java types except for string suck, tell him that every type in Clojure just works right, like string does in Java.
- naradaellis 14y agoCould you explain what you mean, for someone who does not know much about java?
- tubes 14y agoWhy would anyone do that? We use immutable collections, immutable value objects, unmodifiable views and deep/shallow copies, but never serializing data to a string just for passing it around. I think most experienced Java developers would go for the type-safe alternatives.
- dllthomas 14y agoMmm, all the verbosity of java with all the headaches of sh...
- masklinn 14y ago> I'm not sure if focusing on syntax and succinctness is such a great approach though. Depends on the audience I'd guess, considering bug-proneness is an almost direct function of LOC (at equivalent skill levels) it can be a pretty interesting note. Though I'd agree that concurrency prowess should be higher on the list, especially in SOA companies.
- spacemanaki 14y agoSure, i's an interesting note, I just don't think one should lead with it. For instance even as someone who's a fan of Clojure, my immediate reaction to the example with Map<String, String> was to think "that's not really equivalent since the Java example is more typesafe". I just think it might invite unnecessary digression, especially since Java programmers have been hearing about more concise dynamic languages for years.
- edwinnathaniel 14y agoIt's definitely hard to sell a new tool based on an intermediate advantage (less LoC) alone. You have to sell the whole package (of experience). When Rails came out, it offers more than just RAD for building web but it also offers integration with the 2 most commonly tasks in building web-application: DB migration and Automated Testing. That strikes to the heart of any type of applications be it enterprise or one-man-show. Ruby community can't sell Ruby because of its syntax alone by saying that it looks like English (keep in mind that Ruby was invented somewhere in the mid 90's era and didn't exactly known until Rails). We're seeing the same trend here: promise of concurrency. I also would point out "leiningen" as a build/packaging tools as part of the selling point: there are some portion of Java developers that would swear by Maven (including me, despite its quirkiness/complexity) as the killer app/framework/tool for Java. Leiningen fills that gap for these individuals.
- z92 14y agoWhat about speed. If one writes, for example, a video processing library in Clojure will it run slower than if it had been written in pure Java?
- th0ma5 14y agoI'm going wax poetically and say that there are a lot of things like GStreamer or the Javax stuff that's just going to be another library, and depending on what you're doing in Clojure it will run just the same. If you're designing something where your processing is happening with Clojure, it depends on how the JIT observes it and optimizes it, so mostly that stuff might be slower, but it also might be the same. Now, if you're really trying to push things, you could be doing all of it on the GPU, in which case it doesn't matter what language you use, your code on the CPU is just going to be uploading or otherwise managing the GPU work, so using C++ to do such things might not even matter given that the GLSL or whatever you're doing the GPU is compiled and running over there anyway. But as far as Clojure speed goes, the general feel I get is that it's generally slower than Java (for say a single thread, and especially interactively with the REPL) but still faster than C Python, but it also may surprise you with its transparent threading.
- xaa 14y agoFor most tasks, it's slower, but within the same order of magnitude. Performant processing of numeric data, however, requires quite a few ugly hacks (just like almost all functional languages).
- swah 14y agoI was thinking of the same thing a few minutes ago, and for some tasks I think one is better off with Java (IMHO, I don't even do much Java - its just the C/Java style wins in some cases - I'm thinking of code with many array acesses). But as a high-level language for high-level tasks, for gluing pieces of code and calling libraries all around and for dynamic chancing stuff like web programming, Clojure really shines.
- regularfry 14y agoAlmost certainly. You could go on: would it be faster written in C? In ASM? The question shouldn't be "can it be faster," but rather "is it fast enough?" If you're worried about clojure not being fast enough where java is, you've always got the option of prototyping in clojure then dropping to java via clojure's interop where you have to.
- espeed 14y agoAndrew mentioned The Joy of Clojure (http://www.amazon.com/The-Joy-Clojure-Thinking-Way/dp/1935182641 http://www.amazon.com/The-Joy-Clojure-Thinking-Way/dp/193518...) and Clojure Programming (http://www.amazon.com/Clojure-Programming-Chas-Emerick/dp/1449394701/ http://www.amazon.com/Clojure-Programming-Chas-Emerick/dp/14...). Both books are good, and each has its strong points. The Joy of Clojure goes into to Clojure philosophy and explains the "Why?" of Clojure. Clojure Programming is divided into five parts, and parts III and IV provide a good overview of how to set up your environment and structure projects. But the best book for learning the language is Programming Clojure (http://www.amazon.com/Programming-Clojure-Stuart-Halloway/dp/1934356867/ http://www.amazon.com/Programming-Clojure-Stuart-Halloway/dp...) by Stu Halloway. Stu works alongside Rich and understands Clojure at a deep level, but he's still in tune to the beginner's mind and is able to clearly explain concepts and provide the context you need for the ideas to resonate.
- edwinnathaniel 14y agoThank you for pointing these amazing resources. I've had the fortune of being able to borrow The Joy of Clojure from my alma-mater library and I was surprised by the size of the book! Maybe there's life after Java after all!! :D
- andrewvc 14y agoI disagree, I found joy to be a much better text for learning than halloway's book. Halloway's book didn't have as good a flow from one section into the next and felt more disjointed
- cgag 14y agoI haven't read Halloway's, but I found the O'reilly book much more helpful for learning Clojure than The Joy of Clojure. They're both great, but I'd definitely read the O'Reilly book before Joy.
- runevault 14y agoI haven't read through 2nd edition of Programming Clojure yet, but comparing it and Joy is silly IMO, as Joy is built around the idea of being the book you read after you understand the basics of Clojure as taught by one of the other two books mentioned (or other resources). Joy is the one book I consider a necessity to getting a deep understanding of the language without spending as much time as you might attempting the same understanding on your own.
- gav 14y agoIf I was manager wanting to decide on a substantial technology change like Clojure with a presentation like that I'd have to answer "no". It doesn't answer some of the most important questions: 1) What level of support is there for the Clojure toolchain? Is this likely to be around in 5 years from now? 2) We are going to need to hire programmers in the future. Are there people with Clojure skills (or those that we can cross-train) around? In our geographic region? At the price point we can afford? In 5 years from now is it likely that this is going to be a marginalized technology that we have trouble finding skills for? These are the more important questions, not how succinct the language is. I've had first-hand experience of how technology choices affect companies and it can be very painful down the road. (Note: this isn't intended as a dig at Clojure, you could replace it with Scala, or any other up-and-coming language choice.)
- pka 14y agoAt the price point we can afford? Just a quick note: from personal experience, I've been a lot more productive in Clojure than in Java, and I have written a lot of Java code. Things like the REPL, STM or FP speed you up a lot, I would say by a factor of at least 2 - 3, maybe more. I have no data to back it up but this is my perception. So hiring one programmer instead of 3 and paying him 2x more is already good for you.
- neutronicus 14y agoEveryone claims this about their favorite language. If I were a manager, I sure as hell wouldn't believe it.
- pka 14y agoGood you're not a manager then. When I switched from C to Java, I experienced a considerable bump in development speed. Just the collections framework was a godsend, not to mention things like GC etc. Quite literally I would write things in days instead of weeks. Same thing goes for Java -> Clojure. And just to be clear, it is not my favorite language.
- JackC 14y agoThe other thing I think is worth considering in a move to Clojure is how different functional thinking is. I mean, if you're used to moving between C and Perl and Python and PHP and Java and Javascript and so on, you're used to language transitions within more or less the same paradigm. It feels like a categorically different problem to try to transition a team from one of those to a functional language. I'm thinking in particular of this section from O'Reilly's Clojure book: http://books.google.com/books?id=I8KdEKceCHAC&ots=wNiLN6Tce5&pg=PA138 http://books.google.com/books?id=I8KdEKceCHAC&ots=wNiLN6... They lay out a couple of algorithms in an imperative style, and then gradually morph them into a functional style. It's pretty wild: you go from a story, "do this, then do this, then do this" to a math problem: "your answer is a list composed of f() of g() of h() of another list". (Unfortunately you only get snippets from that link -- it's worth the money to get the whole thing if you're interested.) Anyway, having followed through their examples and done some of my own Clojure programming, I ended up not so convinced that people who are good at solving story-style imperative problems will necessarily be good at solving math-style functional problems. It felt like a different part of my brain. This is not at all a reason not to dabble with functional languages, just a potential pitfall to be aware of if you're thinking of bringing a whole team over. And maybe I'm overstating it? I haven't dabbled too much myself.
- JeanPierre 14y agoAnyway, having followed through their examples and done some of my own Clojure programming, I ended up not so convinced that people who are good at solving story-style imperative problems will necessarily be good at solving math-style functional problems. It felt like a different part of my brain. When you're coming to a new paradigm which is completely different from what you're currently using, then of course it will initially feel hard/different and that you're using a different part of your brain. That doesn't necessarily mean that it's harder for you to program in a functional style, it just means that you're more experienced in imperative programming.
- deleted 14y ago[deleted]
- deleted 14y ago[deleted]
- 16s 14y agoMany of these presentations come across as proselytizing. It's an immediate turn-off to some developers and most all managers. The proselytizer claims to know or be able to understand things that others on the team cannot. It's condescending. You wouldn't say to a co-worker... "I understand why we should use X, you do not, so I'm going to enlighten you and explain this to you in a way that you can understand and see the benefits of." The best way to gain traction and to get others to use your favorite tools is to write code and implement projects on time and under budget that are reliable and easy to maintain. If you do that, then the tools that you use will be in demand. Others will want to use them and they will speak for themselves.
- jenius 14y agoThis finally actually won me over. Starting the clojure book - let's make this happen.
- prakashk 14y agoTo the OP: If you have to take a cheap shot at Perl in order to demonstrate the benefits of Clojure, then you have failed. For the record, I am not implying that Perl is above criticism. But, showing a single arbitrary perl code segment, and touting that Clojure doesn't look like it is not fair comparison.
- DigitalJack 14y agoHe was anticipating the argument that succinctness leads to unreadable code. Perl is a language where you can choose to be so terse as to be nearly unreadable. I don't think he was trying to claim that was idiomatic perl. Rather he was saying that clojure is succinct and still very readable.
- prakashk 14y agoIf he chose his example without consideration of idiomatic Perl, then his attempt appears even worse. Further he chose to remove all spaces from the example, presumably to demonstrate that Perl code is "not clear". I have not written a single line of clojure, and don't know how idiomatic clojure looks like. I tried to translate his example to clojure using my very limited knowledge and looking at the examples on ClojureDocs site: (sort #(compare (second (re-matches #".*([aeiou]).*" %1)) (second (re-matches #".*([aeiou]).*" %2))) %) I am sure this is not idiomatic clojure, and probably a very bad way to write. If I cited this as an example of typical clojure code, then I'd be making a totally unfair judgment. The funny thing is, when I just add spaces to the Perl code segment, it looks quite readable to me. But then, I probably know more Perl than him. sort { ($a =~ /([aeoui])/)[0] cmp ($b =~ /([aeiou])/)[0] } @_
- columbo 14y ago-slightly off topic- I really like some of these languages. Clojure is something I've worked with but haven't built a complete product from, but I'd like to make something standalone. I just wish there was a single website devoted to re-creating the terribly boring "Employee Directory Website" app in each language of choice. It would be even better if the creators of said language would go through the time to make the example site so I could see how they imagine a CRUD layer working, or sessions, or REST/XML data, or the sql connection. For every "new" language it seems I can find a hundred examples of fibonacci sequencing (or more recently everyone seems to enjoy creating 'twitter in 5 lines of code!') but almost no real "boring web stack" examples.
- plaeremans 14y agoOk. Perhaps we should start an os project trying to build it. Where s an example in another language?
- fshen 14y agoThere are quite some interesting web apps implemented in clojure. Build web app with clojure is where clojure shines. I've built a few, some are for the company which pay me. One for myself, the source code https://github.com/shenfeng/rssminer https://github.com/shenfeng/rssminer. You can have a look, It did the things you mentioned
- dxbydt 14y agoI made a similar presentation a year ago to a bunch of fellow quants at the bank. I chose Scala, and my presentation was mostly code. Quant Finance tends to be one of those areas where Scala really excels. For example, a time series is just a running fold. So instead of having an imperative for block with various indices & guards, you can write a 1-line prefix scan to price a 5 year CDS. My examples also included bond pricing, vanilla calls and puts, binomial trees, monte carlo generators, portfolio optimization, risk calculations ( VaR & CVaR ) and a few graphics. My manager was very open-minded when it came to technology choice, and my fellow quants were somewhat tough on the 2 hour presentation, but the end result was what I believe is called a win-win. We've since pushed a bunch of apps coded in Scala into production, and the group has embraced the language quite enthusiastically.
- dons 14y ago> where Scala really excels. Functional programming in general. An awful lot of finance (that used to run in Excel) is just side-effect free, compute-centric transformations over data. Time series, for example, have a relatively nice algebra that makes it easy to manipulate in terms of maps, scans and folds. And anything that makes it easier to avoid off-by-one errors is a big win, especially if your traders and quants aren't trained software engineers.
- dxbydt 14y ago>An awful lot of finance (that used to run in Excel) is just side-effect free, compute-centric transformations over data true dat. For example, in Excel, checkout PV ( Click on Formulas->Financial->PV ) Given a $1K@par 10Y bond paying $50 annual coupon at 2% interest, Excel says the Present Value (PV) of the bond must be $1269.48 scala> (0.0 /: (1 to 10).map(x => 50/math.pow(1.02, x)))(_+_) + 1000/math.pow(1.02, 10) scala> 1269.477 Getting rid of the fold produces the list of discounted coupons and the discounted principal...just a trivial teaser example, but most quants look at that & go, wow if you could do all that in 1 line, imagine the possibilities if you are pricing a mortgage with tranches...you could get rid of 4 page spreadsheets & replace with a paragraph of straight math & it would just work...and they'd be right, too.
- 14y ago
- eragnew 14y agoClojure keeps looking more and more interesting. Noir, in particular, has caught my eye. Anyone have a link to a high-quality Noir tutorial? (high-quality in the sense of straightforward yet useful) Nice article, BTW. Good intro for those not familiar with Clojure yet.
- sailfast 14y agoI gather you've already seen these? http://webnoir.org/tutorials http://webnoir.org/tutorials They got me started, and then I went to the Hiccup docs (https://github.com/weavejester/hiccup/ https://github.com/weavejester/hiccup/) for more detailed information. I have yet to find a more comprehensive walk-through but I'll post again if I find one.
- eragnew 14y agoAh. Hiccup is new to me. Will have to play around with it. Thanks for posting that. Likewise, if I find a more comprehensive tutorial, will post as well.
- killnine 14y agoI have been studying that page, as well as the github pages for lein-noir, noir, leiningen, and Hiccup for a week now. I want to write web app in Clojure/ClojureScript. Compared to starting from c/php and learning python/ruby, this venture is particularly more of a pain, and I'm not sure it is clojure but instead the lack of tutorials out there. But the more time I spend learning/installing/configuring, the more I wonder, why? What is the great advantage of running a web app with clojure? Especially when considered versus php/python/ruby? I started to learn out of curiosity, (I have never written functional before) and honestly this post didn't help me answer that "why" any better.
- ryanpers 14y agoInteresting article, but its only skin deep... The larger issues are down the road, it's where the engineers and the sophomore programmers are separated.
- it 14y agoI really like Clojure as a language, but the JVM implementation takes over a second to start. This lack of responsiveness makes it a poor choice for many small scripts that should run quickly. If not for this issue, I would probably use Clojure on a regular basis.
- sgt 14y agoAs did java just a few years ago. Once it has been started once though, it's quicker.
- dedward 14y agoAs does java - it's all the same jvm. The VM is optimized for long-running applications, not brief scripts............ bang those out in whatever.
- sgt 14y agoAlthough you could argue the .NET CLR is also optimized for long-running applications, because it's directly competing with Java. Why is the CLR so much quicker? For interests sake, I typed "java -version" on my new MacBook Pro now and it took 0.9 seconds. Second time it took 0.11 seconds. It's still unacceptably slow, in my opinion.
- ConstantineXVI 14y agoIf ClojureScript ever becomes self-hosting (it's currently compiled via JVM-Clojure), it'd be much more attractive for scripting. (PS: should mention that it's fine if you don't anticipate editing said scripts often)
- 6ren 14y agoOne solution (a little awkward) is to keep a warmed-up JVM in the background, and have your scripts send the arguments to it. Some trouble to setup, and of course the send-code can't be in Java... but it is lightening fast. I'm not sure why this isn't a more common practice: if there was a standard server and (non-java) client, it wouldn't be awkward.
- leke 14y agoTo someone who is about to learn Java, would you recommend Clojure instead?
- strlen 14y agoLearn Java first before learning other JVM languages: one of their strengths is interoperability with Java libraries. If you want to learn functional programming _for its own sake_, learn Scheme (Little Schemer, SICP, Seasoned Schemer) , ML (either OCaml or SML would do), and optionally Haskell. Common Lisp is worth learning, but majority of Common Lisp code is not very functional: idiomatic Common Lisp code readily takes advantage of cons cell mutability and uses do/loop macros; macros and CLOS are the salient feature of Common Lisp that are worth knowing about.
- technomancy 14y agoTotally depends on why you want to learn Java.
- leke 14y agoI'm looking to expand my knowledge range and open a few more doors to different areas. I currently am fairly proficient with python, php, mysql, dhtml. Java seemed like a natural progression especially since I'm interested in mobile phone development, but I'm also interested in the strengths of the JVM and Java's libraries.
- technomancy 14y agoIf you want to learn for the sake of expanding your knowledge range, Java is a pretty bad choice; there's very little that's novel about Java. The two advantages it has over Clojure right now are are 0) it's much easier to find employment in and 1) Clojure isn't yet suitable for Android development.
- swah 14y agoI used Clojure and now I'm using a little bit of Java (and reading Effective Java) and I'm pretty glad I'm doing so: interfaces, generics, concurrency, data structures, mutable vs immutable objects, etc etc: most I just ignored when programming Clojure... and they are interesting to know IMO. (I intend to return to Clojure/Python on the next project because Java really makes CRUD style web programming a PITA).
- mey 14y agoNeeds a diagram "Number of parenthesis by language".
- javajosh 14y agoThis may seem superficial, but Clojure's usability is a problem. The source-code itself is hurt by the same thing that makes it strong: homogeneity. Even at a superficial level, skimming with the eye, closure code looks strange. It is exactly the same problem that markup that is all divs has when compared to markup that uses common top-level markup. It's hard for the eye to pick out what's important, and what's going on. In addition, indentation rules are not agreed upon and it's a bit stutter stop. (I actually think this is a worse problem then the "inside out" problem). Aha! I just had a really good idea! Let's call it "Clojure folding". It's like code-folding only instead of hiding lines, you pull up all the code into one line. Some of the internals may be elided - and revealed with a tooltip. I also like the idea of coloring the entire content of the parens, so that you can embed a large item and then clearly see that you are now back in the containing list. Indentation doesn't do a good enough job, nor do rainbow parens. Further, you'd get a good intuitive sense of place in the code if you have a consistent series of colors. Emotionally, you'd know that if you're typing read you're like 6 layers deep - and that's really deep, in the danger zone, and you probably want to pull some stuff out into definitions. Chris Granger, feel free to include these ideas in Light Table.
- jberryman 14y agoThese seem like standard criticisms of lisps that have been raised for decades. I dont know the responses or solutions, but surely they exist in abundance on the interwebs.
- tylerhobbs 14y agoWe have been using Clojure for a product for about 9 months now, and in my experience, I have no trouble identifying what different blocks of code do. It may depend on proper syntax highlighting, but I think one of the keys is that there is so little boilerplate in Clojure that all of the code is important. You're not skimming over getters and setters, assignment of constructor parameters to the object, or some of the other things your eyes tend to automatically skip when reading, say, Java or Python. It tends to be pure logic or pure data, with very little else in the way.
- 14y ago
- cbp 14y agoI do consulting for small businesses around where I live (Mexico). And since like 3 months ago I switched to clojure and I couldn't be happier. I used ruby then python before. Now there is one minisupermarket doing billing and inventories and backups and charging customers with clojure in Mexico and that number is looking to grow!
- gosub 14y agohttp://en.wikipedia.org/wiki/Betteridges_Law_of_Headlines http://en.wikipedia.org/wiki/Betteridges_Law_of_Headlines