I would also add that in case, if you *need* to destructure the `options`
map for some reason, like:
`(defn search
"Docstring"
[mapping-type & {:keys [option-1 option-2] :as options}]
(do-smth-with-option-1 ...)
(apply esd/search es-index mapping-type options))`
then you can use `mapply` to apply maps to functions that accept optional
args maps. The code of mapply is the fllowing:
`(defn mapply [f & args] (apply f (apply concat (butlast args) (last
args))))`
Combining it with partial, as adviced, could give you the functionality you
may sometimes need:
`(mapply (partial es/search es-index) your-options-map)`
понедельник, 9 сентября 2013 г., 12:42:57 UTC+4 пользователь Mark Mandel
написал:
>
> Hey all,
>
> Relatively new to Clojure, and I'm wondering if there is a better/simpler
> way to handle what I'm doing.
>
> I'm working with the Elastisch library for interacting with ElasticSearch,
> and it has the following function:
>
> http://reference.clojureelasticsearch.info/clojurewerkz.elastisch.rest.document.html#var-search
>
> (search index mapping-type & {:as options})
>
> That's all fine, except I want to call it from another function, to
> provide my default values for `index`, but still allow for the passthrough
> the options map for the `search` function.
>
> The solution I came up with was:
>
> (defn search
> "Search the mapping-type, with the given properties"
> [mapping-type & {:as options}]
> (apply esd/search (reduce (fn [coll [k, v]] (conj coll k v)) [es-index
> mapping-type] options)))
>
> Where `es-index` is already defined as a global def.
>
> So basically I break the options apart into a vector, and apply it over
> the top of the esd/search function (where esd is the elastisch function
> defined earlier).
>
> Does this make sense? Is there a better way?
>
> It works, but wondering if there is an improvement I could make,
> especially as I could see myself doing this quite often.
>
> Thanks in advance,
>
> Mark
>
>
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.