All Questions
532
questions
44
votes
4
answers
6k
views
threading macro -> with anonymous functions
I understand the `-> theading macro in Clojure applies all the provided functions provided to a given argument. However, it doesn't seem to work with anonymous functions. For example:
user> (-> ...
32
votes
4
answers
3k
views
Generalized Threading Macro in Clojure
Note: this is NOT about concurrency. This is about the thread macro.
I know that -> puts the object at the 2nd position and ->> puts the argument at the last position.
Now, I'm curious, ...
29
votes
6
answers
6k
views
Good examples of Clojure macros usage which demonstrate advantages of the language over the mainstream?
I am thinking about learning Clojure, but coming from the c-syntax based (java, php, c#) world of imperative languages that's going to be a challenge, so one naturally asks oneself, is it really worth ...
27
votes
3
answers
7k
views
In Clojure, when should we use a monad instead of a macro and vice-versa?
There are too many tutorials out there on monads that say... "Look! here is a case where we can use a monad" or "This is what a monad is for". What I want to know is what are some of the steps that ...
26
votes
2
answers
4k
views
What is the purpose of ~' or '~ in Clojure?
I am learning about Clojure macros, and the code examples will sometimes have the constructs '~symbol or alternately ~'symbol. I know that (quote and ' prevent a form from being evaluated, and that ...
25
votes
2
answers
4k
views
multiple arity in defmacro of clojure
I encountered a strange problem relating to defmacro in Clojure, I have code like
(defmacro ttt
([] (ttt 1))
([a] (ttt a 2))
([a b] (ttt a b 3))
([a b c] `(println ~a ~b ~c)))
and I run with ...
25
votes
2
answers
3k
views
Clojure defmacro loses metadata
I am trying to create a little Clojure macro that defs a String with a type hint:
(defmacro def-string [name value]
`(def ^String ~name ~value))
(def-string db-host-option "db-host")
When I ...
22
votes
1
answer
2k
views
Generating Clojure code with macro containing type hints
I'm trying to generate some Clojure code with type hints, however the type hints seem to disappear whenever I build some code (they also don't function when the code is compiled)
e.g.
`(let [^...
21
votes
4
answers
5k
views
How to implement a Lisp macro system?
I've implemented my own Lisp on top of node.js, I can run s-expressions like this:
(assert (= 3 (+ 1 2)))
(def even? (fn [n] (= 0 (bit-and n 1))))
(assert (even? 4))
(assert (= false (even? 5)))
...
20
votes
5
answers
3k
views
What are the practical differences between special forms and macros?
Are there any practical differences between special forms and macros? In what do they differ?
20
votes
1
answer
3k
views
Controlling symbol generation in Clojure macros
I'm trying (as a self-learning exercise) to create a Clojure macro that will generate code to apply a function to a sequence of integers and sum the result, e.g.
f(0) + f(1) + f(2) + f(3)
This is ...
19
votes
5
answers
3k
views
When should I use the clojure arrow macro?
In trying to stay with the functional style, I am having difficulty understanding when I should prefer:
(-> [1 2 3] reverse last)
over:
(last (reverse [1 2 3]))
When I come across both styles ...
19
votes
1
answer
4k
views
Help me write a Clojure macro which automatically adds metadata to a function definition
I realize that the first rule of Macro Club is Don't Use Macros, so the following question is intended more as an exercise in learning Clojure than anything else (I realize this isn't necessarily the ...
18
votes
5
answers
11k
views
Macros, clojure vs common lisp
A few of my friends and I are working on a new platform and we want to build it in lisp. The main attraction are macros. We all use Common Lisp but I want to explore the option of Clojure.
When I ...
18
votes
1
answer
8k
views
How to implement lambda as a function called "lambda" in Clojure?
I'd like to be able to define lambdas using common Lisp syntax, in Clojure. For example:
(lambda (myarg)
(some-functions-that-refer-to myarg))
This needs to result in the same as:
#(some-...
17
votes
3
answers
3k
views
How do I unit test clojure.core.async go macros?
I'm trying to write unit tests when using core.async go macros. Writing the test naively, as follows, it appears that the code inside the go blocks don't get executed.
(ns app.core-test
(:require [...
17
votes
2
answers
750
views
Is a "transparent" macrolet possible?
I'd like to write a Clojure with-test-tags macro that wraps a bunch of forms, and adds some metadata to the name of each deftest form - specifically, add some stuff to a :tags key, so that I can play ...
16
votes
3
answers
3k
views
define a synonym for a Clojure macro
So following on from Clojure macro to create a synonym for a function , I discovered that def can't be used to define a synonym for a macro. Below are examples I tried that Clojure doesn't allow.
;(...
16
votes
3
answers
4k
views
Treat Clojure macro as a function
How can I cause a Clojure macro to act as a function, so I can pass it as an argument for example? I would expect to have to wrap it somehow.
I would not expect the wrapped version to behave exactly ...
16
votes
2
answers
923
views
First Lisp with macros?
McCarthy's original Lisp and some number of incarnations thereafter did not have a macro facility like we now have in Common Lisp, Clojure, Scheme, etc... This I know.
However, it is unclear to me ...
15
votes
1
answer
3k
views
How do I create a macro to define two functions in clojure
The code below doesn't behave as I would expect.
; given a function name, its args and body, create 2 versions:
; i.e., (double-it foo []) should create 2 functions: foo and foo*
(defmacro double-...
14
votes
5
answers
2k
views
Why does let require a vector?
I never really thought about this until I was explaining some clojure code to a coworker who wasn't familiar with clojure. I was explaining let to him when he asked why you use a vector to declare ...
14
votes
2
answers
2k
views
Apply-recur macro in Clojure
I'm not very familiar with Clojure/Lisp macros. I would like to write apply-recur macro which would have same meaning as (apply recur ...)
I guess there is no real need for such macro but I think it'...
14
votes
1
answer
2k
views
How can I map a macro in Clojure?
I tried to do the following in Clojure
(map future exprs)
To get a seq of future tokens. Unfortunately this errs because future is itself a macro, not a function.
Is there a way to make this work ...
14
votes
2
answers
2k
views
Clojure #= reader macro
I just "discovered" the #= reader macro from a post on Stackoverflow
and it solves a problem. How likely is this reader macro to become an
official (documented) part of the language? How about ...
13
votes
1
answer
2k
views
Is it possible to realize the benefits of dependent typing using macros in Lisp?
This is an honest question, not a a troll. I'm asking for your patience.
When Cedric talks about dependent types, the benefit he states is checking List lengths at compile time:
Having a list ...
13
votes
2
answers
2k
views
Simple Yet Compelling Macro Examples which are Not Already in Clojure
I'm trying to write a macro tutorial, and now I need some examples which are simple to understand, and yet compelling.
The problem is that a lot of the obvious things are already in clojure and ...
12
votes
1
answer
3k
views
How might I write a "defn" macro in Clojure?
I was practicing writing macros and I can't seem to get defn to work.
My syntax is: (my-define name parameter body)
Ignoring & parameters and recursive routines, How do I bind the name to a (fn[...
12
votes
3
answers
2k
views
Should I use a function or a macro to validate arguments in Clojure?
I have a group of numeric functions in Clojure that I want to validate the arguments for. There are numerous types of arguments expected by the functions, such as positive integers, percentages, ...
12
votes
3
answers
2k
views
Recommended macros to add functionality to Clojure's defrecord constructor?
defrecord in clojure allows for defining simple data containers with custom fields.
e.g.
user=> (defrecord Book [author title ISBN])
user.Book
The minimal constructor that results takes only ...
12
votes
2
answers
1k
views
Defining the defmacro function using only LISP primitives?
McCarthy's Elementary S-functions and predicates were atom, eq, car, cdr, cons
He then went on to add to his basic notation, to enable writing what he called S-functions: quote, cond, lambda, label
...
11
votes
4
answers
3k
views
How do I write a Clojure macro to create a regular expression from a String?
I'm creating a convenience macro. Part of the convenience is that a regular expression can be specified with just a String, rather than the #"re" notation.
The one part I can't figure out is how to ...
11
votes
4
answers
2k
views
Use of Clojure macros for DSLs
I am working on a Clojure project and I often find myself writing Clojure macros for DSLs, but I was watching a Clojure video of how a company uses Clojure in their real work and the speaker said that ...
11
votes
2
answers
5k
views
Explanation/documentation of leiningen's defproject macro
Leiningen's defproject macro is an important part of Clojure projects. However, information/documentation about it seems to be very sparse.
What are all of the options that defproject supports, ...
11
votes
5
answers
1k
views
Cross between "dotimes" and "for" functionality?
I frequently find myself wanting to efficiently run a Clojure function multiple times with an integer index (like "dotimes") but also get the results out as a ready-made sequence/list (like "for").
i....
11
votes
1
answer
2k
views
Inlining a function with Clojure macros
More out of curiousity that anything else (but with the expectation that it might occasionally be a useful trick for performance tuning), is it possible to use Clojure macros to "inline" an existing ...
11
votes
2
answers
1k
views
Clojure macro for calling Java setters based on a map?
I'm writing a Clojure wrapper for the Braintree Java library to provide a more concise and idiomatic interface. I'd like to provide functions to instantiate the Java objects quickly and concisely, ...
11
votes
1
answer
1k
views
How do I deal with required Clojurescript code from Clojurescript macros?
Let us say I have a X.clojurescript and a X.clojure namespace. Everything in X.clojurescript is Clojurescript code, everything in X.clojure is Clojure code. Unfortunately, I cannot define macros ...
11
votes
1
answer
704
views
Is there a clean way to add functions to a dynamically created namespace?
I am creating a noir webapp, and I need to dynamically create new views and models. I've been following the noir examples, in which the view and the controller for a resource have separate namespaces, ...
10
votes
2
answers
1k
views
What is the difference between macroexpand and macroexpand-1 in Clojure
I couldn't understand the difference between macroexpand and macroexpand-1.
Could you provide examples?
10
votes
1
answer
879
views
Clojure, can macros do something that couldn't be done with a function
I'm learning Clojure macros, and wonder why we can't use just functions for metaprogramming.
As far as I know the difference between macro and function is that arguments of macro are not evaluated ...
10
votes
1
answer
3k
views
can cljc single-file macro definitions to work with clojurescript?
I have clojurescript successfully importing macros from other namespaces. But I wonder whether a single-file construction is/should be possible with clojure 1.7, such that a macro can be defined and ...
10
votes
1
answer
843
views
Namespace confusion and macros
I want to write a macro that uses functions from the clj-time library. In one namespace I would like to call the macro like this:
(ns budget.account
(:require [budget.time]))
(budget.time/next-...
9
votes
5
answers
1k
views
Is homoiconicity really needed for having macros?
2012-02-04 is sponsored by word "homoiconicity" http://en.wikipedia.org/wiki/Homoiconicity .
Background: I am about to choose which book about Clojure to buy -- either "Clojure in ...
9
votes
3
answers
3k
views
In Clojure, what does `&` mean in function/macro signature?
I saw the usage of & in Clojure function signature like this (http://clojure.github.io/core.async/#clojure.core.async/thread):
(thread & body)
And this:
(doseq seq-exprs & body)
Does ...
9
votes
2
answers
626
views
Why is clojure adding namespace qualifiers to names inside a backquote?
I am trying to build datalog queries programmatically, but keep running into the problem that I will illustrate with an example function:
(defn test-expr [attribute]
`[?entity ~attribute ?value]])
...
9
votes
4
answers
907
views
How to avoid anaphoric macro in Clojure?
Take this (simplified) example:
(defmacro make [v & body]
`(let [~'nv ~(some-calc v)]
~(map #(if (= % :value) 'nv %) body)))
Right now the symbol nv is hardcoded. Is there a way to ...
9
votes
3
answers
1k
views
Clojure - How to make my macro expand before system macros?
If I do, for example:
(defmacro qqq [] '(toString [this] "Qqq"))
(reify Object (qqq))
it fails because of reify sees (qqq) instead of (toString [this] "Qqq").
The usual solution is a macro that ...
9
votes
2
answers
1k
views
How can you type hint within the threading (->) macro?
I have some Clojure code that is trying to interop through a couple layers of Java code (in this case, java.nio.Path by way of java.nio.file.WatchEvent<?>:
(defn unroll-event
[^WatchEvent ...
9
votes
2
answers
8k
views
IllegalStateException: Attempting to call unbound fn in macro
I'm trying to write a macro that calls some functions. The functions should only be used by the macro, so I put them inside of a letfn wrapping the macro. Pseudocode:
(letfn [(fn-a [] ...)
(...