Skip to content

Instantly share code, notes, and snippets.

@kevbuchanan
Created January 15, 2014 01:05
Show Gist options
  • Save kevbuchanan/8428988 to your computer and use it in GitHub Desktop.
Save kevbuchanan/8428988 to your computer and use it in GitHub Desktop.
if-not-empty-let
(ns if-not-empty-let.core
(:require [clojure.test :refer [deftest is run-tests]]))
(defmacro if-not-empty-let
([binding then] `(if-not-empty-let ~binding ~then nil))
([binding then else]
(let [form (first binding)
sequence (binding 1)]
`(let [temp# ~sequence]
(if (not (empty? temp#))
(let [~form temp#]
~then)
~else)))))
(defn check-rest [x]
(if-not-empty-let [the-rest (rest x)]
(str the-rest " is not empty")
"empty"))
(defn check-even [x]
(if-not-empty-let [evens (filter even? x)]
(str "These are even " (vec evens))))
(deftest if-not-empty-let
(is (= "(2 3) is not empty" (check-rest [1 2 3])))
(is (= "empty" (check-rest [1])))
(is (= "These are even [2 4]" (check-even [2 3 4])))
(is (= nil (check-even [1 3 5])))
)
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment