• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to implement : key + " = " + get(key)

 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys :

Im (cursed me) thinkin in java again. I have a map

{a 1.0, b 2.0}

and I want to format the key value pairs, and then apply a function to the formatted pairs , i.e.



Is this the idiomatic way to process the key / value in a clojure via the fmap function ?
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I found the solution to this. To iterate over keys and values of a map, you use the doseq function.

Doseq defines, as the first arg, the "type" of data that your iterator is generating, so for example, with a map , you have

(doseq [[k v] myMapGetter] #(println k v) )

It seems odd that [k v] are in scope however. how can the print statement see them ? They appear to be enclosed by ['s ?
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's called destructuring. Maps behave like sequences of vectors (with two elements - the key and the value). [k v] is a vector destructuring that binds k to the first element of the vector and v to the second element of the vector.

doseq is for when you're dealing with side effects. Normally you'd use map to apply a function to each element of a sequence: (map (fn [[k v]] (str k "=" v)) my-map) will produce the sequence ( "a=1.0" "b=2.0" ) based on your original data. You can turn that into a comma-delimited list with clojure.string/join: (clojure.string/join ", " (map (fn [[k v]] (str k "=" v)) my-map)).

BTW, you'll do better to try to work within the core language / libraries and avoid contrib if you can. The monolithic contrib of 1.2.0 is being broken up and only modules with active maintainers will go forward to 1.3.0 (new contrib modules are compatible with 1.2.0 and 1.3.0 so you can switch to the new modules first, then switch to 1.3.0). See Where did Clojure.Contrib go? for more details of the migration.
reply
    Bookmark Topic Watch Topic
  • New Topic