• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Clojure destructuring

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I have a hard time to understand the code used in the ring parameter example ("gist.github.com/weavejester/598020"). This is the part I am stuck with:
     (defn handler [{{name "name"} :params}]
           (-> (response (page name))
                 (content-type "text/html")))
So I get the first destructuring. {name "name"} will take the value that the keyword :params points to in the argument which should be a map. Now the value is also a map containing all parameters from the request (form and url). But what happens next? I think it is also some sort of destructuring but I do not see the logic behind. The end result is that it will give nil if there is no parameter named name as it is used in another fuction (page) which will display a certain HTML page depending on it being thruthy.

If anybody could shed some light on how it works would be great.

Thanks
Carol
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this sort of nested destructuring is pretty hard to read and not actually all that common in Clojure code. Also, part of the issue here is that the basic wrap-params middleware produces string keys so most folks use the wrap-keyword-params middleware instead.

If the keyword params middleware is used, you'd be more likely to see this:

The equivalent with the original wrap-params would be:

We do (my-map "string") rather than (:some-key my-map) -- although (my-map :some-key) would work, it's just not as idiomatic.

Note also that :use is frowned on these days -- that Gist is very old! -- and you'd normally see a :require .. :as .. and code would then use the namespace alias when referencing functions.

So, bottom line, that Gist is very old and pretty much no one writes Clojure like that (partly because it's harder to read).

 
carol stefan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sean!
 
It's exactly the same and completely different as this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic