News for hackers. Stuff that matters.
Re:A few tips for writing macros in Clojure by jwr on Sunday May 08, 2016 @06:08PM (#11656204)
Excellent tips. I'd advise most inexperienced programmers (say under 10 years of lisp) to stick to tip #1. You very rarely need a macro in Clojure.
Re:A few tips for writing macros in Clojure by jcadam on Sunday May 08, 2016 @08:41PM (#11656826)
I've recently (about two weeks ago) picked up Clojure for use on a side project after getting frustrated with trying to use Haskell for something non-trivial -- not smart enough for Haskell, apparently :D

Haven't messed with a LISP language since college but clojure has grown on me considerably. I already like it more than Scala... though I did almost immediately find myself reaching for a monad library (cats seems good so far) :) Haven't felt the need to write my own macros yet, but certainly many of the libraries I'm using (compojure) make heavy use of them.

Too bad my day job is all Java, all the time :( My continual requests to use Scala have been shot down over the last couple of years -- "We want something maintainable, where am I going to find Scala devs when you leave?" So obviously Clojure isn't going to fly :)

Re:A few tips for writing macros in Clojure by andrewingram on Sunday May 08, 2016 @07:47PM (#11656610)
This is interesting, but also a little discouraging. Coming from a background of Python and JavaScript, and using a little bit of Elixir (which also supports macros) lately, the power of macros for defining DSLs is the main draw for me.

As an example, I've been using GraphQL quite heavily for the last 8 months, primarily with JavaScript and i've found it to be a fairly tedious experience. I tried out an Elixir GraphQL library that's heavily based on macros and was almost immediately more productive - despite not even knowing the language.

Similarly, coming up with a nice API for working with forms seems to be pretty much impossible with JavaScript. I've evaluated dozens, and not one comes close to the simplicity of Django's forms (which is only possible thanks to Python's metaclass magic).

Having sampled the power of languages with macros, I'm naturally drawn to Clojure, simply because it has a very decent JavaScript implementation. I'm sure there are other very good reasons for using the language, but right now macros are the motivation.

I'd like to understand why one would need 10 years of lisp experience before starting to author their own macros, it feels like a philosophy that's just going to stop people trying out lisp.

Re:A few tips for writing macros in Clojure by bendyorke on Sunday May 08, 2016 @09:25PM (#11656980)
The article says that you shouldn't write them unless you need to, not that you shouldn't write them. He lists quite a few scenarios where they should be used, one of which is DSLs. :)

Also, given that Clojure is a lisp, I think you'll find you need macros left often than a language like elixir. They're still incredibly powerful, but you can do so much in Clojure without them that you would otherwise need a macro to do.

Re:A few tips for writing macros in Clojure by asimuvPR on Sunday May 08, 2016 @08:44PM (#11656839)
It's OK to be discouraging to a certain degree. It helps stop people from cooking up "clever" solutions.
Re:A few tips for writing macros in Clojure by andrewingram on Sunday May 08, 2016 @09:01PM (#11656902)
Well I try to use the right tool for the job, and it's important that code can be understood easily when it's read by other developers. But I think the 10 years with Lisp argument needs to be better justified. :)
Re:A few tips for writing macros in Clojure by asimuvPR on Sunday May 08, 2016 @09:19PM (#11656957)
Oh, I was not making a personal comment about you. It was a comment in your first point. That being discouraged by others is a sign that one should thread lightly. I was not discouraged by anyone when macros appeared on my horizon. And boy did I write some bad code. :)
Re:A few tips for writing macros in Clojure by andrewingram on Sunday May 08, 2016 @09:29PM (#11657000)
Makes sense :)

I guess i've just heard for so many years how badly wrong macro usage can go, that i've not even been tempted to try languages that use them - until very recently.

Re:A few tips for writing macros in Clojure by asimuvPR on Sunday May 08, 2016 @10:22PM (#11657213)
As long as it's not production code: You should actively seek out and learn/try as many things possible. Don't let anyone stop you. I once worked on adding a "maybe" keyword to python because boredom. It was a disaster. Fun though.
Re:A few tips for writing macros in Clojure by cageface on Sunday May 08, 2016 @06:56PM (#11656404)
If it's true that you rarely need macros then isn't the awkwardness of s-expr syntax a very high price to pay just to have access to them?
Re:A few tips for writing macros in Clojure by jdminhbg on Sunday May 08, 2016 @07:15PM (#11656481)
I'd say it's more true that you rarely need to write a macro than it is that you rarely need to use them. Lots of very useful things across the language, from builtins like the threading macros (->, ->>) to libaries like Compojure, are built on macros.
Re:A few tips for writing macros in Clojure by bkirkbri on Sunday May 08, 2016 @07:56PM (#11656654)
I've found s-expressions to be one of my favorite things about Clojure. I understand that many people dislike them, and that many people get used to them, but I loved the syntax from the get go.
Re:A few tips for writing macros in Clojure by aaron-santos on Sunday May 08, 2016 @07:06PM (#11656441)
Sure. The syntax is distracting at first, but in my experience after a few weeks I saw past it. It's a non-issue for me at this point and I dearly miss it when writing Scala on another project.
Re:A few tips for writing macros in Clojure by nilliams on Sunday May 08, 2016 @07:14PM (#11656477)
What aspect do you dearly miss? (Honest question, I've never understood the S-expr trade-off, especially if macros are off the table).
Re:A few tips for writing macros in Clojure by pka on Sunday May 08, 2016 @07:35PM (#11656563)
Probably paredit. It's really so much better than non-structured editing. That said, there are structured editing plugins/modes/editors for other languages too.

However, looking back at Lisp/Clojure now I really don't like the syntax anymore. I don't care about the brackets, it's absolutely true that you learn to look past them in a matter of days, but the homogeneity and the "denselesness" really impact my ability to quickly parse an expression. Infix syntaxes make reading code much easier, imo.

Re:A few tips for writing macros in Clojure by roboguy12 on Monday May 09, 2016 @10:48PM (#11664862)
I thought that at first too about infixes being better (I've only been using Clojure for a few months), but I'm beginning to find the opposite. The only times when I miss an infix syntax is when doing math

    1 + (2 * 10) - (12 / 3)
is so much easier for me to read to read than

    (- (+ 1 (* 2 10) (/ 12 3)))
But when working with functions, I much prefer the standard Clojure syntax.
Re:A few tips for writing macros in Clojure by malloryerik on Tuesday May 10, 2016 @06:26AM (#11666154)
I believe the parens are off by one in the Clojure example and that it should be:

    (- (+ 1 (* 2 10)) (/ 12 3))   ;=> 17
I've found the threading operator can help in these cases.

    (-> (+ 1) (+ (* 2 10) (- (/ 12 3)))) 

Or, line breaks can clarify:

    (- (+ 1 (* 2 10))
       (/ 12 3))
or at the risk of overdoing it:

    (- 
      (+ 1 (* 2 10))
      (/ 12 3))
Now I know at first glance that I'm subtracting the results of those two lines from each other. Indeed the last two might be almost as clear as the infix, and are without any order of operations rules.

Also some don't like colored parens but I find them very clarifying.

Re:A few tips for writing macros in Clojure by pka on Wednesday May 11, 2016 @02:33PM (#11677942)
I have to disagree.

    f x . g
reads just so much easier than

    (comp (partial f x) g)
Re:A few tips for writing macros in Clojure by aaron-santos on Sunday May 08, 2016 @09:11PM (#11656936)
I took some time to think about this because it started off as a feeling and not a list of reasons.

For me it's mostly centered around control structures - if, for, switch, case (scala's pattern matching). Clojure's control structures are syntactically composed of ()'s and []'s. Scala's aren't. They are a coded into the language's grammar[1][2]. Clojure's ns forms parallel Scala's package and import statements, but are again Clojure's are syntactically composed of ()'s and []'s. I know it's a relatively small point, but control structures make up a significant portion of programs.

As a side note, I took a look at a Clojure grammar also specified for antlr[3]. My initial thought would be that Clojure's grammar would be appreciably smaller. It is, but not by as much as I was expecting. There are a lot of very little additions to the reader that add up [4][5][6].

[1] - Scala's syntax http://www.scala-lang.org/files/archive/spec/2.11/13-syntax-...

[2] - Examples https://github.com/lrlucena/grammars-v4/blob/master/scala/sc...

[3] - https://github.com/laurentpetit/ccw/blob/3738a4fd768bcb03996...

[4] - http://clojure.org/reference/reader#macrochars

[5] - https://yobriefca.se/blog/2014/05/19/the-weird-and-wonderful...

[6] - See "Special Characters" http://clojure.org/api/cheatsheet

Re:A few tips for writing macros in Clojure by d_t_w on Sunday May 08, 2016 @07:41PM (#11656585)
Structural editing, paredit, 100%.

I say that not being particularly accomplished with it. 15 years of programming Java, now 3 in Clojure, would dearly miss slurp and barf, the lesser appreciated virtues of homoiconicity.

Re:A few tips for writing macros in Clojure by reitzensteinm on Sunday May 08, 2016 @08:00PM (#11656675)
I have a pet theory that someone will put a Python skin on Clojure and have significant success with it.
Re:A few tips for writing macros in Clojure by e12e on Sunday May 08, 2016 @09:59PM (#11657112)
Well, there's already wisp/sfri-110 for scheme, it appears the preprocessor should mostly work with clojure -- although one would probably want to port it over, or at least try to get it working on the jvm in order to have it "work where clojure works" (I have no idea how viable it would be to get it running with jython and something like kawa scheme out of the box):

http://www.draketo.de/english/wisp#text-table-of-contents

[ed: For Racket, see also: https://github.com/takikawa/sweet-racket ]

[ed2: See also the "readable" package for Common Lisp and for Scheme: https://sourceforge.net/p/readable/wiki/Install-howto/ ]

Re:A few tips for writing macros in Clojure by SapphireSun on Sunday May 08, 2016 @08:54PM (#11656877)
You can't. The reason you can use macros so easily in LISP is because of homoiconicity: https://en.wikipedia.org/wiki/Homoiconicity

Your macros will break all over the place in languages that don't have this property.

Re:A few tips for writing macros in Clojure by e12e on Sunday May 08, 2016 @10:01PM (#11657121)
SRFI-110 says you can^1: "Sweet-expressions are general and homoiconic, and thus can be easily used with other constructs such as quasiquoting and macros. In short, if a capability can be accessed using s-expressions, then they can be accessed using sweet-expressions. Unlike Python, the notation is exactly the same in a REPL and a file, so people can switch between a REPL and files without issues. Fundamentally, sweet-expressions define a few additional abbreviations for s-expressions, in much the same way that 'x is an abbreviation for (quote x)."

http://srfi.schemers.org/srfi-110/srfi-110.html#wisp

^1 [ed: well, not "just python syntax", but if you wanted that, you would be better off using python. But python-like syntax/skin is absolutely doable. ]

Re:A few tips for writing macros in Clojure by SapphireSun on Monday May 09, 2016 @12:19AM (#11657594)
Thanks, I learned something today. :)
Re:A few tips for writing macros in Clojure by pjmlp on Sunday May 08, 2016 @10:58PM (#11657333)
Dylan also has macros.
Re:A few tips for writing macros in Clojure by groovy2shoes on Monday May 09, 2016 @12:23AM (#11657611)
As does Rust.
Re:A few tips for writing macros in Clojure by pjmlp on Monday May 09, 2016 @12:27AM (#11657622)
But Rust isn't a Lisp based on M-Expressions like Dylan is.
Re:A few tips for writing macros in Clojure by groovy2shoes on Monday May 09, 2016 @02:26AM (#11657934)
No, but it's also not a homoiconic language, and it supports syntactic macros just fine. Homoiconicity might make procedural macros convenient, but it's far from a requirement. And then pattern-template macros are just as convenient in a more "syntactically rich" language as they are in a homoiconic one.

See also: camlp4/camlp5.

And then there's the idea of pattern-match rewriting systems. iirc, GHC has something like that to rewrite certain expressions as optimizations, like:

    (map f) . (map g)  =>  map (f . g)
In a pure language, the two expressions are equivalent, but the latter is more efficient because it traverses the list only once rather than twice.

The Cat programming language had a similar sort of "macro" system, but its web site seems to have disappeared. Shame, it was a really neat little concatenative language.

Homoiconicity is certainly neat, but it's not about macros at all. It's about having a Lisp-style `read` function. That's it.

Also, Dylan's syntax wasn't based on M-expressions per se, at least not in the "classical" LISP 1.5 sense of the term. It was just an Algolesque syntax rather than a façade layered above S-expressions. If you can claim that Dylan was based on M-expressions, then you could just as truthfully claim that Lua, Ruby, JavaScript, etc. are based on M-expressions. I won't say that it's strictly false, but I don't think that's quite how most people familiar with M-expressions think of them.

Re:A few tips for writing macros in Clojure by pjmlp on Monday May 09, 2016 @03:28AM (#11658129)
> Also, Dylan's syntax wasn't based on M-expressions per se,

I know, my remark was more into the sense of being based as departure point, not as being a pure M-expression implementation.

Re:A few tips for writing macros in Clojure by spion on Sunday May 08, 2016 @11:36PM (#11657451)
That seems off by about 1 order of magnitude...
Re:A few tips for writing macros in Clojure by AimHere on Monday May 09, 2016 @03:08AM (#11658069)
Lisp is getting on a bit, but even the greyest of Lisp hackers haven't quite racked up a hundred years of Lisp...
Re:A few tips for writing macros in Clojure by dwighttk on Monday May 09, 2016 @11:08AM (#11660359)
maybe in the other direction? :D
Re:A few tips for writing macros in Clojure by spion on Monday May 09, 2016 @02:18PM (#11661912)
yes, I was thinking that a programmer with about 5-10 years experience in other (non-lisp) programming languages and 1 year of Clojure should be able to tackle macros effectively...