alternative to passing parameters to functions

155 views
Skip to first unread message

AtKaaZ

unread,
Feb 17, 2013, 4:06:13 PM2/17/13
to clo...@googlegroups.com
Was there a library or some other way to pass ie. maps to functions
so that the order of the params isn't predefined, just in case I want to skip some passing parameters without actually having to pass some value for them, I could just refer to which params I'm passing by identifying them with a :key  ?

It's probably not hard at all to implement, but if there's a lib already, it would do a better job that I could.

--
Please correct me if I'm wrong or incomplete,
even if you think I'll subconsciously hate it.

vemv

unread,
Feb 17, 2013, 4:12:27 PM2/17/13
to clo...@googlegroups.com
fn's "keyword arguments" feature provide unrolled, optional key-value args:

(defn foo [& {:keys [a b c]}]
  [a b c])

(foo :c 4) ;; [nil nil 4]

Gunnar Völkel

unread,
Feb 18, 2013, 3:33:16 AM2/18/13
to clo...@googlegroups.com
You can checkout clojure.options (https://github.com/guv/clojure.options/).
One of its main features is documentation for options (even transitive ones in calls to other functions with options).

AtKaaZ

unread,
Feb 18, 2013, 9:16:14 PM2/18/13
to clo...@googlegroups.com
Thanks guys, I think I will go with this variant that vemv suggested and upon further exploring I realized it can do much more (still exploring currently):

(defn somefn
  [req1 req2
;required params
   & {:keys [a b c d e] ;optional params
      :or {a 1 ;optional params with preset default values other than the nil default
                  ; b takes nil if not specified on call
            c 3 ; c is 3 when not specified on call
            d 0 ; d is 0 --//--
                  ; e takes nil if not specified on call
           }
      :as mapOfParamsSpecifiedOnCall }]
  (println req1 req2 mapOfParamsSpecifiedOnCall a b c d e)
  )


=> (somefn 9 10 :b 2 :d 4)
9 10 {:b 2, :d 4} 1 2 3 4 nil
nil
=> (somefn)
ArityException Wrong number of args (0) passed to: funxions$somefn  clojure.lang.AFn.throwArity (AFn.java:437)
=> (somefn 9 10)
9 10 nil 1 nil 3 0 nil
nil
=> (somefn 9 10 :x 123)
9 10 {:x 123} 1 nil 3 0 nil
nil
=> (somefn 9 10 123)
IllegalArgumentException No value supplied for key: 123  clojure.lang.PersistentHashMap.create (PersistentHashMap.java:77)
=> (somefn 9 10 123 45)
9 10 {123 45} 1 nil 3 0 nil
nil



--
--
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/groups/opt_out.
 
 

Pablo Nussembaum

unread,
Feb 19, 2013, 9:46:12 AM2/19/13
to clo...@googlegroups.com
You can also check prismatic plumbing library where they a new function
definition type based on maps.

link: https://github.com/Prismatic/plumbing/tree/master/src/plumbing/fnk
Reply all
Reply to author
Forward
0 new messages