Matching false (== undefined, null) in ClojureScript core.match

944 views Asked by At

I'm trying to match a false value of map's key, which in javascript effectively means also undefined, null etc... It seems to me that core.match matches the value exactly. Is there any way to do this?

The other way would be to match "not true", but the docs don't give me any hint how to do this either.

1

There are 1 answers

0
Michał Marczyk On

You could use :or patterns:

(match [x]
  [(:or false nil)] true
  [_] false)

returns true if x is false or nil, false otherwise.

Alternatively, you could use :guard patterns:

(match [x]
  [(_ :when not)] true
  [_] false)

:when patterns would also work (with (defpred not)), but they can be tricky to get right and bring no benefit over guards in this case.