1.2.0 permalink Arrow_down_16x16
  • (fcase compare-fn case-value & test-expr-clauses)
Generic switch/case macro. 'fcase' is short for 'function case'.

The 'compare-fn' is a fn of two arguments.

The 'test-expr-clauses' are value-expression pairs without
surrounding parentheses, like in Clojure's 'cond'.

The 'case-value' is evaluated once and cached. Then, 'compare-fn'
is called once for each clause, with the clause's test value as its
first argument and 'case-value' as its second argument. If
'compare-fn' returns logical true, the clause's expression is
evaluated and returned. If 'compare-fn' returns false/nil, we go to
the next test value.

If 'test-expr-clauses' contains an odd number of items, the last
item is the default expression evaluated if no case-value matches.
If there is no default expression and no case-value matches, fcase
returns nil.

See specific forms of this macro in 'case' and 're-case'.

The test expressions in 'fcase' are always evaluated linearly, in
order. For a large number of case expressions it may be more
efficient to use a hash lookup.

0 Examples top

Log in to add / edit an example.

See Also top

  • 0
    clojure.core/case

    Takes an expression, and a set of clauses. Each clause can take th

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/fcase.clj:40 top

(defmacro fcase
  "Generic switch/case macro.  'fcase' is short for 'function case'.

  The 'compare-fn' is a fn of two arguments.

  The 'test-expr-clauses' are value-expression pairs without
  surrounding parentheses, like in Clojure's 'cond'.

  The 'case-value' is evaluated once and cached.  Then, 'compare-fn'
  is called once for each clause, with the clause's test value as its
  first argument and 'case-value' as its second argument.  If
  'compare-fn' returns logical true, the clause's expression is
  evaluated and returned.  If 'compare-fn' returns false/nil, we go to
  the next test value.

  If 'test-expr-clauses' contains an odd number of items, the last
  item is the default expression evaluated if no case-value matches.
  If there is no default expression and no case-value matches, fcase
  returns nil.

  See specific forms of this macro in 'case' and 're-case'.

  The test expressions in 'fcase' are always evaluated linearly, in
  order.  For a large number of case expressions it may be more
  efficient to use a hash lookup."
  [compare-fn case-value &
   test-expr-clauses]
  (let [test-val-sym (gensym "test_val")
	test-fn-sym (gensym "test_fn")
	cond-loop (fn this [clauses]
		      (cond
		       (>= (count clauses) 2)
		       (list 'if (list test-fn-sym (first clauses) test-val-sym)
			     (second clauses)
			     (this (rest (rest clauses))))
		       (= (count clauses) 1) (first clauses)))]
    (list 'let [test-val-sym case-value, test-fn-sym compare-fn]
	  (cond-loop test-expr-clauses))))
Vars in clojure.contrib.fcase/fcase: defmacro let list
Used in 0 other vars

Comments top

No comments for fcase. Log in to add a comment.