Supplied-p parameter in clojure similar to lisp lambda lists

204 views
Skip to first unread message

Dave Tenny

unread,
Jun 20, 2014, 2:13:57 PM6/20/14
to clo...@googlegroups.com
What is the commonly accepted technique for declaring/using 'supplied-p' type lambda list functionality in clojure?

http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/sec_3-4-1.html


I have some clojure functions with a large number of keywords and various defaults, I want to know if a keyword was specified by the caller (rather than defaulted) in some cases.

Certainly I could implement my own destructuring macros that did this, but I'd like to avoid reinventing a wheel here if I can, and also to know the idiomatic clojure way to do it.

Thanks for any tips.


Jason Felice

unread,
Jun 21, 2014, 7:22:13 PM6/21/14
to clo...@googlegroups.com

If you destructure the parameters like this:
(defn f [& {:as a-map}] ...)

You can use map primitives on a-map.  But you can also supply defaults here.

--
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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave Tenny

unread,
Aug 17, 2014, 8:05:09 AM8/17/14
to clo...@googlegroups.com
Well, it took me a while to perhaps get what you were telling me here.

In my case I I had something like

(defn foo [ & {:keys [bar ... more keys ...] :or {bar 1}} ] ...)

and I wanted to know whether the user had explicilty invoked foo with :bar.

What wasn't clear to me was that :as solved this problem.  
Reading http://clojure.org/special_forms#Special Forms--Binding Forms (Destructuring)-Map binding destructuring
I guess I can see that it's telling me :as shows things that weren't in the init-form, but that's with hindsight.

So, to emulated common lisp 'supplied-p' semantics, you can check the :as form, which will **not**
contain :or values for keywords.

E.g.

user> (defn bar [ & {:keys [baz] :or {baz 'baz} :as all-keys} ] (println baz all-keys))
#'user/bar
user> (bar :bof 1)
baz {:bof 1}
nil

And not that the all-keys form does not show a binding for baz, and that's what I wanted.

Just fyi in case anybody searches topics for 'supplied-p' again.

dennis zhuang

unread,
Aug 17, 2014, 11:03:53 PM8/17/14
to Clojure
I think that adding a :p option to destructuring would be great:

(let [ {:keys [a b c] :p {a a-p}} params]
    (if a-p
        (println a)
        (println "a is not exists.")))




--
庄晓丹
Email:        killm...@gmail.com xzh...@avos.com
Site:           http://fnil.net
Twitter:      @killme2008


dennis zhuang

unread,
Aug 18, 2014, 3:17:45 AM8/18/14
to Clojure

Dave Tenny

unread,
Aug 18, 2014, 6:58:07 AM8/18/14
to clo...@googlegroups.com
I don't think that a :p feature is necessary, since all you need to do to emulate it is a
(:baz all-keys) to know if the user explicitly specified it.  I.e. I think the capability is already present in adequate form but the documentation on map destructuring could be improved.




You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/jWc51JOkvsA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

dennis zhuang

unread,
Aug 18, 2014, 10:42:28 AM8/18/14
to Clojure
Yep, it's an optional syntax sugar.
Indeed, you should use (contains? all-keys :baz) to check if :baz is present in options. If :baz is present but it's value is nil,then (:baz all-keys) returns nil too.
Reply all
Reply to author
Forward
0 new messages