The case
macro is a similar conditional to cond/condp
.
It branches to multiple clauses.
The difference is that case
doesn’t evaluate branching expressions.
In case
, it should be a constant.
What we can do with case
looks like map (data structure)
.
The syntax is: (case e & clauses)
user> ( defn cases-to-do
[ temp ]
( case temp
:65-80 "I'll enjoy walking at a park."
:45-64 "I'll spend time at a cafe."
"I'll curl up in my bed" ))
# 'user/cases-to-do
user> ( cases-to-do :65-80 )
"I'll enjoy walking at a park."
user> ( cases-to-do :45-64 )
"I'll spend time at a cafe."
user> ( cases-to-do :30 )
"I'll curl up in my bed"
user> ( cases-to-do :100 ) ; also falls to default
"I'll curl up in my bed"
user> ; if we use map...
user> ( def my-cases { :64-80 "I'll enjoy walking at a park."
:45-64 "I'll spend time at a cafe."
:other "I'll curl up in my bed" })
# 'user/my-cases
user> ( my-cases :64-80 )
"I'll enjoy walking at a park."
user> ( my-cases :45-64 )
"I'll spend time at a cafe."
user> ( my-cases :other )
"I'll curl up in my bed"
user> ( my-cases :30 ) ; hash-map can't take a key that doesn't match anything
nil
References