This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How when-let works.

 
Ranch Hand
Posts: 139
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand when-let and if-let in clojure. I was reading a blog post that said it's a combination of when and let.

and gave me the following example,


I understand, (str x) will be executed if the boolean test returns true, but how [x (+ 1 2 3 4)] returns true?

 
Marshal
Posts: 5119
319
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The signature of a Clojure when-let is "(when-let bindings & body)", and the following statement comprises the "bindings => binding-form test" part:

So the test is simply the "(+ 1 2 3 4)" part, which in clojure is considered "logical true". Have a read of the Clojure Docs for 'if' to learn about what is considered "logical true" and "logical false", which should help understand why the 'when-let' statement in that blog post behaves as it does.

As an aside, for reference can you link to the blog post you speak of please?
 
Quazi Irfan
Ranch Hand
Posts: 139
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tim. I got confused with the => symbol in the documentation(bindings => binding-form test). I didn't know I have to read => as 'replaced with'

So based on your explanation,

is equivalent to,

Am I correct?

This is the blog post I am reading. when-let is under the Control flow section.
 
Greenhorn
Posts: 27
Android Clojure Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanted to add a practical example in case this post shows up in a Google search for when-let

Lets say you have a function that takes a map.
If the passed in map has the key :username you want to do something with it. Otherwise you can return nil or do something else
If you want to return nil then use when-let if you want to do something else use if-let

Exercise for the reader:
1) if the map has the :username key return a greeting with the name, otherwise return a generic greeting
2) same as 1 except return nil if missing

These forms prevent repetition to make your code look cleaner.

Exercise 3: refactor this code
`(when (:count state-map) (let [count (:count state-map)] (str "count is :" count)))`
 
reply
    Bookmark Topic Watch Topic
  • New Topic