This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Clojure destructuring

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 352
21
Mac OS X Monad Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you Sean!
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!