> (defn str-sort[str]
> (if (nil? str)
> str
> (String. (into-array (. Character TYPE) (sort str)))))
>
(and str
(String. (into-array (. Character TYPE) (sort str))))
(if str is nil this will return nil)
> (defn anagram-add[anagrams akey word]
> (if (empty? (get anagrams akey))
> (assoc anagrams akey (hash-map :count 1, :words (list word)))
> (update-in (update-in anagrams [akey :count] inc) [akey :words]
> conj word)))
>
You could use a literal {:count 1 :words (list words)} instead of hash-map
I believe that (get anagrams akey) will give nil in case there is known.
If it is the case (or (get anagrams key) ...) will be more readable.
(update-in ...) =>
(-> anagrams
(update-in [akey :count] inc)
(update-in [akey :words] conj word))
I will have a look to the rest of the program later.
Best,
Nicolas.
On Thu, Aug 19, 2010 at 9:09 PM, Damon Snyder <drsn...@gmail.com> wrote:
> Hi Nicolas,
> Thanks for the suggestions. Regarding the first one: ah, I see. That
> is a nice compact way to test to see if the str is nil. I added that
I reckon that Meikel's suggestion of using when is surely better in
this situation (for a and).
But the (or something? default) or (or known? recompute?) can be useful.
> in because I was getting null pointer exceptions when the string was
> null. What is the difference between {:count 1 :words (list words)}
> and a hash-map? I was under the impression that {} created a hash.
>
I just find it easier to read because it looks like a value and not a
function call.
Am 19.08.2010 um 22:14 schrieb Nicolas Oury:
>> in because I was getting null pointer exceptions when the string was
>> null. What is the difference between {:count 1 :words (list words)}
>> and a hash-map? I was under the impression that {} created a hash.
>>
> I just find it easier to read because it looks like a value and not a
> function call.
Another difference is that in this case it creates not a hash map but an array map. Rule of thumb: up to 8 keys => array map, more than 8 => hash map. The array map is simply a short alist, not a full blown hash map. For such small maps this is faster than pulling at the hash cannon. So the difference is not limited to syntax alone.
Sincerely
Meikel