The more I think about this problem, the more wrong it seems...
I have defined in my program something like a 'map constructor'. The idea behind this is that I have a generic map structure to handle some 'items' but I want to enforce some defaults for specific kind of items.
The problem that I have is that this 'map constructor' has a k-v pair, and that pair's value should be determined by the function that consumes this map (it might get clearer in the following example).
My first idea was to quote an expression in the value and then do an eval on it in the said function. The second idea was to replace the value with a fn, but this seems to return something similar to the quoted expression.
Let me try to depict the problem:
- The model resulting map should be something like {:a 1 :b 2 :c 3}
The constructor is something like
(defn cons-field [b] {:a (fn [name] (str name "!")) :b b :c "default"})The item is created
(def a-field (cons-field 5))The calling function that consumes the map is something like
(defn the-function [name field] (str (get-in field [:a])))
Now what I need is this :a's value to be a function of the parameter name in 'the-function'. Of course the last function is not working and I'm not sure if it's the correct approach anyway. The ':a' key is not always a fn; sometimes it's just a string literal. Any ideas?
Cheers!
assocto the default map? For the general problem, try to separate the logic from the data and think in terms of successive transformations on data. Can you provide more details or examples of what exactly you are trying to accomplish? - A. Webb:c), just don't provide:a. Then do a transformation on the map if the value of:aneeds to be determined by a function call. - Jeremycondon the caller to determine if it's a special case and then calls the function. :) - Dimitrios K.