Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure : Documentation syntax, regarding when-let and if-let

Tags:

syntax

clojure

I've been looking at the if-let and when-let macros, Im having trouble determining what exactly it is that they "do". In particular, the documentation sais :

clojure.core/when-let
([bindings & body])
Macro
  bindings => binding-form test

  When test is true, evaluates body with binding-form bound to the value of test

I thus am somewhat confused about the way macros are documented.

1) What does the "=>" symbol mean ?

2) What does "test" refer to ?

like image 771
jayunit100 Avatar asked Sep 02 '25 18:09

jayunit100


1 Answers

Direct answer to your questions:

  1. => means "expands to", as in BNF notation. In this case it means that you need two forms: binding-form and the test.
  2. "test" means anything that can be evaluated as bool.

By the way, I think that the docs are unclear or even maybe erroneous here. It is hard (or impossible) to deduce that the two forms constituting the bindings need to be enclosed in a vector. IMHO it should be either when-let ([[bindings] & body]) (the vector shown in the args) or bindings => [binding-form test] (the vector shown in the BNF-like expansion.)

like image 119
Rafał Dowgird Avatar answered Sep 04 '25 17:09

Rafał Dowgird