Skip to content

Commit

Permalink
New Post: A Quick Pollen Macro (for/s)
Browse files Browse the repository at this point in the history
  • Loading branch information
otherjoel committed Jul 10, 2018
1 parent c383bfd commit deeb6fd
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions posts/a-quick-pollen-macro.poly.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#lang pollen

◊(define-meta title "A Quick Pollen Macro")
◊(define-meta published "2018-07-07")
◊(define-meta topics "Pollen")

One of Pollen’s
link["https://groups.google.com/d/msg/pollenpub/FdWdGgL9jSk/1VWpX_nsAAAJ"]{somewhat recent}
additions is the ◊link["http://docs.racket-lang.org/pollen/Core.html#%28form._%28%28lib._pollen%2Fcore..rkt%29._for%2Fsplice%29%29"]{◊code{for/splice}} function,
which lets you loop through one or more lists
and inject the results into a surrounding expression.

Here’s an example from the code for the home page on this blog:

◊blockcode[#:filename "index.html.pp"]{...
""for/splice[[(post (in-list (latest-posts 10)))]]{
<article>
""(hash-ref post 'header_html)
◊"◊"(hash-ref post 'html)
</article>
<hr>
}
...}

It’s very useful,
but I’m sure you’ve noticed something about this code:
◊emph{many many parentheses}
in the arguments list for ◊code{for/splice}.
Well, it is the way it is,
because the ◊code{for/splice} function
is a direct analogue
of Racket’s ◊link["https://docs.racket-lang.org/reference/for.html?q=for%2Flist#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29"]{◊code{for/list} function},
which supports looping through multiple lists at the same time.
It’s nice to have the flexibility.
But there’s no denying the readability of the code suffers
in the most common use cases.

If, like me, you usually only need to loop through a single list,
you can put a macro like the one below
in your ◊code{pollen.rkt}:

◊blockcode[#:filename "pollen.rkt"]{
(require pollen/core)

...

(provide for/s)
(define-syntax (for/s stx)
(syntax-case stx ()
[(_ thing listofthings result-expr ...)
#'(for/splice ([thing (in-list listofthings)]) result-expr ...)]))
}

This cuts down on the parentheses quite a bit:

◊blockcode[#:filename "index.html.pp"]{...
""for/s[post (latest-posts 10)]{
<article>
""(hash-ref post 'header_html)
◊"◊"(hash-ref post 'html)
</article>
<hr>
}
...}

0 comments on commit deeb6fd

Please sign in to comment.