All Questions

Tagged with
Filter by
Sorted by
Tagged with
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> (-> ...
S4M's user avatar
  • 4,591
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, ...
user avatar
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 ...
Cray's user avatar
  • 2,416
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 ...
zcaudate's user avatar
  • 14.1k
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 ...
Alex's user avatar
  • 325
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 ...
xudifsd's user avatar
  • 800
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 ...
Ralph's user avatar
  • 32k
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 [^...
mikera's user avatar
  • 106k
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))) ...
Steven Devijver's user avatar
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?
Pedro Rolo's user avatar
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 ...
mikera's user avatar
  • 106k
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 ...
ToBeReplaced's user avatar
  • 3,434
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 ...
Tim Gilbert's user avatar
  • 5,811
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 ...
Ferdy's user avatar
  • 507
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-...
dirtyvagabond's user avatar
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 [...
Hugh Powell's user avatar
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 ...
amalloy's user avatar
  • 90.6k
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. ;(...
Al Chou's user avatar
  • 457
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 ...
pauldoo's user avatar
  • 18.3k
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 ...
fogus's user avatar
  • 6,166
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-...
Nick Orton's user avatar
  • 3,623
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 ...
Jason Baker's user avatar
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'...
JoeCamel's user avatar
  • 722
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 ...
MRocklin's user avatar
  • 56.5k
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 ...
Ralph's user avatar
  • 32k
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 ...
hawkeye's user avatar
  • 35.2k
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 ...
John Lawrence Aspden's user avatar
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[...
unj2's user avatar
  • 52.8k
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, ...
clartaq's user avatar
  • 5,352
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 ...
Alex Stoddard's user avatar
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 ...
hawkeye's user avatar
  • 35.2k
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 ...
dirtyvagabond's user avatar
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 ...
yazz.com's user avatar
  • 58k
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, ...
Matt Fenwick's user avatar
  • 48.6k
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....
mikera's user avatar
  • 106k
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 ...
mikera's user avatar
  • 106k
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, ...
bkirkbri's user avatar
  • 1,672
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 ...
Stephen Cagle's user avatar
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, ...
jtmoulia's user avatar
  • 660
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?
Ertuğrul Çetin's user avatar
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 ...
Tuomas Toivonen's user avatar
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 ...
ben's user avatar
  • 113
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-...
user1265564's user avatar
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 ...
greenoldman's user avatar
  • 20.5k
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 ...
Hanfei Sun's user avatar
  • 46.2k
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]]) ...
TG-T's user avatar
  • 2,174
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 ...
Philip Kamenarsky's user avatar
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 ...
Vi.'s user avatar
  • 37.8k
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 ...
Nick Klauer's user avatar
  • 5,923
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 [] ...) (...
Ken's user avatar
  • 2,944

1
2 3 4 5
11