When to prefer keywords as functions?

127 views
Skip to first unread message

gvim

unread,
Jul 3, 2014, 1:11:35 PM7/3/14
to clo...@googlegroups.com
I'm reading "Cloujure in Action" as an introduction to Clojure and,
although, I understand a keyword can be used as a function I don't
understand the difference between:

(ns org.currylogic.damages.http.expenses
(:require [clojure.data.json :as json-lib]
[clojure.xml :as xml-core]))

... and

(ns org.currylogic.damages.http.expenses
(require [clojure.data.json :as json-lib]
[clojure.xml :as xml-core]))


When is it idiomatic or even preferable to substitute a function with a
keyword equivalent?


gvim

James Reeves

unread,
Jul 3, 2014, 1:37:24 PM7/3/14
to clo...@googlegroups.com
The "ns" form is a macro that takes a special syntax and always uses the keyword form, as you have in your first example. Your second example is incorrect; I'd be surprised if it even ran.

The reason for this is to make it clear that you're not executing the require function directly, but instead passing options to the ns form. 

- James





gvim

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gvim

unread,
Jul 3, 2014, 2:24:51 PM7/3/14
to clo...@googlegroups.com
On 03/07/2014 18:36, James Reeves wrote:
>
> The reason for this is to make it clear that you're not executing the
> require function directly, but instead passing options to the ns form.
>

I don't understand "not executing the require function directly".

I've also seen the when function called as :when in the body of a let
statement so could you elaborate on this non-macro example? I need to be
clear on when it is preferable to use a keyword function rather than a
normal function call.

gvim

adrian...@mail.yu.edu

unread,
Jul 3, 2014, 2:39:14 PM7/3/14
to clo...@googlegroups.com
I believe you might have seen :when in the binding vector of either a for or deseq form. Their special usage is documented here: http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/for

In any event, James explained the distinction quite well. Macros are often used to create special syntax for domain specific functionality. In the ns macro, this is to simplify the specification for required libraries, classes, etc. in a given namespace. You noticed the similarity to the standalone 'require', 'use', and 'import' macros for a reason. The ns macro was designed to make adding those options to the ns form look and feel like the standalone forms.

In the case of the 'for' macro, it is to allow for powerfully expressive list comprehensions. This is tangential but I think you might enjoy seeing how far you can go with list comprehensions in Clojure: http://programming-puzzler.blogspot.com/2013/03/logic-programming-is-overrated.html

Mauricio Aldazosa

unread,
Jul 3, 2014, 2:41:08 PM7/3/14
to clo...@googlegroups.com
Keywords can be used as a function with a map, so the keyword will search for itself in the map. Take a look at http://clojure.org/data_structures#Data%20Structures-Keywords

In the case of the "ns" form, as James pointed out, what is happening is not a function call. ns is a macro and as such that form will be transformed before actual evaluation occurs. If you want, you can see the actual call that will be made when evaluating ns, you can do so by expanding the macro with:

(macroexpand-1 '(ns org.currylogic.damages.http.expenses

                  (:require [clojure.data.json :as json-lib]
                            [clojure.xml :as xml-core])))

Be aware though that the resulting form may be a little daunting :-)

Jony Hudson

unread,
Jul 3, 2014, 3:30:16 PM7/3/14
to clo...@googlegroups.com
One thing to note is that while it's true "keywords can be used as functions" they can only really do one thing, which is get values from maps. That is to say, the keyword-function :foo is equivalent to the function #(get % foo). The function :foo has no relationship with the function foo, if it is defined. I hope that makes sense!

So, to some approximation, only use keywords as functions as a shorthand for getting values from maps. As others have pointed out, although the keyword :require is in function position above, that code never actually gets executed - it's just data for the ns macro. This can be one of the more confusing things to get straight ...


Jony
Reply all
Reply to author
Forward
0 new messages