Questions tagged [defn]
15 questions
19
votes
1 answer
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 best use of macros).
I want to write a simple…

Tim Gilbert
- 5,721
- 5
- 33
- 28
15
votes
4 answers
How to defn a function from string in Clojure?
I'd like to do this (in REPL or anywhere)
(defn (symbol "print-string") [k] (println k))
and then be able to do
(print-string "lol")
Or, if there is any other way to create defn from custom strings in macroses, could you push me into the right…

mannicken
- 6,885
- 4
- 31
- 38
8
votes
3 answers
Retrieve Clojure function metadata dynamically
Environment: Clojure 1.4
I'm trying to pull function metadata dynamically from a vector of functions.
(defn #^{:tau-or-pi: :pi} funca "doc for func a" {:ans 42} [x] (* x x))
(defn #^{:tau-or-pi: :tau} funcb "doc for func b" {:ans 43} [x] (* x x…

G Seaton
- 83
- 2
5
votes
4 answers
How do I modify a :arglists to a Clojure fn or macro?
How do I modify the :arglist attribute for a clojure fn or macro?
(defn tripler ^{:arglists ([b])} [a] (* 3 a))
(defn ^{:arglists ([b])} quadrupler [a] (* 4 a))
% (meta #'tripler) =>
{:arglists ([a]), :ns #, :name…

Stephen Cagle
- 14,124
- 16
- 55
- 86
2
votes
1 answer
multi-arity defn in Clojure -- first match first serve?
To be concrete, what is supposed to happen in the following situation:
(defn avg
([] 0)
([& args] (/ (reduce + args) (count args))))
(avg)
i.e., can I rely on clojure to always return 0 rather than divide-by-zero?

manualcrank
- 93
- 1
- 5
1
vote
3 answers
Positional Destructing Functions
I'm new to clojure and I'm trying to understand as much as possible but the documentations are so vague
when you have a function
(fn [_ {:keys [kind]}] kind)
my understanding is that the function takes a vector map but only wants access to a key…

Kendall
- 5,065
- 10
- 45
- 70
1
vote
2 answers
What namespaces Clojure uses for def-ing
According to spec, def should intern the var in the current ns (i.e. *ns*). However, the following code does not look anything like it:
(ns namespace-b)
(defn def_something []
(ns namespace-a)
(println *ns*) ;prints namespace-a as it should
…

Tomas Kulich
- 14,388
- 4
- 30
- 35
0
votes
1 answer
Clojure - How to put a list and a function inside a defn function
I am a newbie and making some exercises. How can I put a def with a list of sentences and a randomizer function inside a defn function? How does that work?
(def list["test1", "test2", "test3"]) - works fine
(rand-nth list) - works fine
How do I put…

SSS
- 11
- 3
0
votes
2 answers
Return the args which are not passed as nil inside the function
(defn bar[{:keys [a b] :as args}] (prn "got" args))
If we are calling the above function as
(bar {:a 1})
it returns
{:a 1}
nil
I want to have
{:a 1 :b nil}

Ravi Bhanushali
- 145
- 2
- 9
0
votes
1 answer
In defn macro, what does an argument starting with caret before the name of a function mean?
In a function definition:
(defn ^boolean =
;;other arities omitted...
([x y]
(if (nil? x)
(nil? y)
(or (identical? x y)
^boolean (-equiv x y))))
what does the ^boolean part in function definition mean? Does it only…

rishat
- 8,206
- 4
- 44
- 69
0
votes
1 answer
Macro with a function definition including set! not working in Clojurescript
I'm trying to define a macro that should do the following:
Bind a 'variable' with an initial value
Create setter methods with a name based on the variable name
I have the following defined in a clojure file:
(defmacro defprop
[prop-name…

Sebastian
- 1,076
- 9
- 24
0
votes
2 answers
Clojure defn name as multiplier
Could any one explain why in the following code the name of the function acts in the first part as a * (multiplier)?
(defn bar
([a b] (bar a b 100))
([a b c] (* a b c)))
Giving bar two args (bar 2 3) yields (* 2 3 100)
user4813927
0
votes
1 answer
Clojure Defn returning empty parentheses just after displaying correct answer
I am trying to get only one zodiac name to past it down to different function, but I don not know why I ma getting empty parentheses after displayed zodiac. I am newbie with Clojure.
(defn miko []
(let [guess (read)]
…

user3297995
- 3
- 1
0
votes
1 answer
Clojure macro to process multiple function metadata
In Clojure, how do I make a library macro which processes supplied functions metadata and return some result? Amount of functions is unlimited and they should be passed without being boxed into a sequence ((my-macro fn1 fn2) instead of (my-macro…

endragor
- 368
- 2
- 9
-1
votes
1 answer
clojure - defn/def in function body or let statements
user=> (defn make-list [] '(1 2 3))
#'user/make-list
user=> (defn get-list [] (map #(str "foo" % ) make-list))
#'user/get-list
user=> (get-list)
IllegalArgumentException Don't know how to create ISeq from: user$make-list clojure.lang.RT.seqFrom…

Mond Raymond
- 320
- 1
- 4
- 9