Live data from Hacker News

Why Clojure?

blog.cleancoder.com

61–70 of 202 posts

Re: Why Clojure?

#61
post #35

Earlier quoted context omitted.

> by far the best programming language I've ever used Would love to hear why? What is that make Clojure such a good experience for you?

Not original poster, but my take is: - Immutable data-structures with concise literals for lists, vectors, maps, and sets. Having pure functions and immutable data-structures makes code easier to reason about, easier to test, and thread-safe. (But clojure doesn't "force" you to be pure. The idea is that you write as much of your code in pure functions as you can, and push the IO and impure parts to the extremities. I…

> Macro system

Macros are actually my least favourite part of Clojure. Sure, its great to have them and there are some libraries that use them to excellent effect (instaparse, Hugsql, etc), but most of the time, I prefer tools that don't use macros. You can see by comparing libraries that were made in the earlier days of Clojure versus more recent ones: the earlier ones love to use macros while the newer ones prefer functions and data. The data-first libraries are, in my opinion, easier to test and easier to build complex things on top of. If I look at a readme and see that the expected way to interact with a library is a macro, I usually look for a data-first library instead and only use the macro one if I can't find an alternative.

Macros are technically cool, you can do some interesting things with them, but I find that in most cases where they're used, they are inferior to alternatives. Its technically very cool that core.async could be implemented as macros, but I feel that core.async greatly suffers from it versus being built into the runtime: you cannot use many operations in functions called by core.async/go because the macro can't see inside function calls. Also, I've had exceptions thrown by core.async where the stack trace did not mention any of my code. That was not fun to debug.

Don't get me wrong, macros do have their place: hugsql uses macros to parse the SQL file and generate functions for you, which is awesome! But for every great macro-based library, there are many more that I wish didn't use macros.

(This is user-facing macros in a libraries' API. I have no problem with using macros internally)

I do love Clojure overall, though.

Re: Why Clojure?

#62

Can someone explain me why they always (OK let's say almost always) use math formulas to show what you can do with a programming language ? I'm a desktop application programmer, not a mathematician. I don't need to print the first 25 squares of integers or Fibonacci whatever. In fact I think the hardest math I did at work was using modulo to get even and odd numbers ... Show me how you parse a csv file, how do you co…

Here is an example to massage/manipulate CSV data. Stolen from the README page for a clojure CSV parsing library ( https://github.com/clojure/data.csv ) (defn read-column [reader column-index] (let [data (read-csv reader)] (map #(nth % column-index) data))) (defn sum-second-column [filename] (with-open [reader (io/reader filename)] ; Read in the CSV file (streaming / lazily) (->> (read-column reader 1) ; Convert to j…

Using the clj-http library in clojure to fetch from a REST webservice:

    (client/get "http://example.com/resources/3" {:accept :json :query-params {"q" "foo, bar"}})
With the response, you can examine response headers, the body, etc.

Re: Why Clojure?

#63
post #38

My team owns a Clojure app, amongst a lot of other apps - my understanding is the current DRI (who inherited this app once the original one left for another org) doesn't like working in it though, and that has been the experience of other developers who have been recruited to the project over the past two years. Most developers I work with on a day to day basis don't have any interest in learning Clojure, and would r…

Ive been doing various methods of js dev with tons of different framworks and traspilers for 20 years, nothing is more pleasant to work with than ClojureScript and re-frame once you learn it. It's the best of all the worlds, and the DOM is very abstracted away via React. State transition is a breeze with immutable data structs. Theres only 1 language to write, no JSX ugliness.

Re: Why Clojure?

#64
post #38

My team owns a Clojure app, amongst a lot of other apps - my understanding is the current DRI (who inherited this app once the original one left for another org) doesn't like working in it though, and that has been the experience of other developers who have been recruited to the project over the past two years. Most developers I work with on a day to day basis don't have any interest in learning Clojure, and would r…

I can understand some reluctance. My current position has suffered a lot of churn as devs ran away from clojure. My boss was very glad I showed up when I did.

It does require a different way of thinking. I was lucky in that I had very little experience, academic or professional, when I picked it up. I had a lot less to unlearn.

But that doesn't mean it's limited to ultra-brainy lisp weenies. We have undergrads writing clojure, never having written clojure before, in their first programming job. They do just fine.

Re: Why Clojure?

#65

I played around with Closure for a while to learn its cost/benefits. It is basically the love child of Lisp, Haskell and Java. So it introduces nothing new to the world. However it is certainly an interesting mix that some developers like. And Rich Hickey is an entertaining speaker/presenter/seller of Clojure. Even if you don't care about Closure, watch his talks on YouTube.

Clojure has nothing at all to do with Haskell.

I can't think of two more opposed camps in programming.

Re: Why Clojure?

#66
post #38

My team owns a Clojure app, amongst a lot of other apps - my understanding is the current DRI (who inherited this app once the original one left for another org) doesn't like working in it though, and that has been the experience of other developers who have been recruited to the project over the past two years. Most developers I work with on a day to day basis don't have any interest in learning Clojure, and would r…

ClojureScript doesn't try to abstract away the DOM. It really depends on the front-end library used. Reagent (and re-frame) is pretty much react with goodies and you can easily integrate other react libraries (using shadow-cljs)

Re: Why Clojure?

#67

I played around with Closure for a while to learn its cost/benefits. It is basically the love child of Lisp, Haskell and Java. So it introduces nothing new to the world. However it is certainly an interesting mix that some developers like. And Rich Hickey is an entertaining speaker/presenter/seller of Clojure. Even if you don't care about Closure, watch his talks on YouTube.

Clojure has nothing at all to do with Haskell. I can't think of two more opposed camps in programming.

Well, they have immutable data and persistent collections in common. Otherwise, agreed.

Re: Why Clojure?

#68
Clojure is one of the better dynamic languages.

The rub however is that GHC can check the consistency of my software faster than I or my colleagues will ever be able to, and programmer time is expensive.

So that's why not Clojure.

Re: Why Clojure?

#69
post #48

This is all nice and exciting until you start to 1) Debug code, the high density of clojure code means that this is really painful. 2) Read code you wrote a while back. The high density of clojure code means that this is really painful.

For me, the balancing factor is that there is much less code to debug or read.

Re: Why Clojure?

#70

Earlier quoted context omitted.

Here is an example to massage/manipulate CSV data. Stolen from the README page for a clojure CSV parsing library ( https://github.com/clojure/data.csv ) (defn read-column [reader column-index] (let [data (read-csv reader)] (map #(nth % column-index) data))) (defn sum-second-column [filename] (with-open [reader (io/reader filename)] ; Read in the CSV file (streaming / lazily) (->> (read-column reader 1) ; Convert to j…

Using the clj-http library in clojure to fetch from a REST webservice: (client/get "http://example.com/resources/3" {:accept :json :query-params {"q" "foo, bar"}}) With the response, you can examine response headers, the body, etc.

What about JDBC?

Here's a few examples stolen from the doc page for the next.jdbc library (https://github.com/seancorfield/next-jdbc):

    > clj
    Clojure 1.10.1
    user=> (require '[next.jdbc :as jdbc])
    nil
    user=> (def db {:dbtype "h2" :dbname "example"})
    #'user/db
    user=> (def ds (jdbc/get-datasource db))
    #'user/ds
    user=> (jdbc/execute! ds ["
    create table address (
      id int auto_increment primary key,
      name varchar(32),
      email varchar(255)
    )"])
    [#:next.jdbc{:update-count 0}]
    user=> (jdbc/execute! ds ["
    insert into address(name,email)
      values('John Smith','john.smith@email.org')"])
    [#:next.jdbc{:update-count 1}]
    user=> (jdbc/execute! ds ["select * from address"])
    [#:ADDRESS{:ID 1, :NAME "John Smith", :EMAIL "john.smith@email.org"}]

That is an example REPL session to setup the library, setup a datasource, create a table, insert a row into the table, and then select from the table. The "user=>" part is the REPL command prompt. Essentially it's one statement for each SQL query executed, and 1 or 2 statements to setup the JDBC datasource. This is tight.

You can see the return from the select contains clojure native data-structures. It returns a list of rows, where each row is a map of fieldName to fieldValue. You can now go to town and manipulate lists and maps to your hearts content, convert the payload to HTML or JSON or whatever you like.

Post reply on HN