Skip to content

Instantly share code, notes, and snippets.

@koddsson
Created July 6, 2014 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koddsson/d41acc0ed7c8a5b3de70 to your computer and use it in GitHub Desktop.
Save koddsson/d41acc0ed7c8a5b3de70 to your computer and use it in GitHub Desktop.
Running into problems with Clojure return types
(ns com.koddsson.for-clojure
(:use [clojure.test :only [is deftest run-tests]]))
(defn my-flatten
([x] (if (not (and (seq? x) (vector? x)))
x ; If x is not a sequence nor a vector
(map my-flatten x)))) ; else recursivly apply the flatten function
(deftest test28
(is (= (my-flatten '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6)))
(is (= (my-flatten ["a" ["b"] "c"]) '("a" "b" "c")))
(is (= (my-flatten '((((:a))))) '(:a))))
(run-tests)
λ bubblegum 20 → λ git master* → lein exec -p 28.clj
Testing com.koddsson.for-clojure
FAIL in (test28) (28.clj:10)
expected: (= (my-flatten (quote ((1 2) 3 [4 [5 6]]))) (quote (1 2 3 4 5 6)))
actual: (not (= ((1 2) 3 [4 [5 6]]) (1 2 3 4 5 6)))
FAIL in (test28) (28.clj:11)
expected: (= (my-flatten ["a" ["b"] "c"]) (quote ("a" "b" "c")))
actual: (not (= ["a" ["b"] "c"] ("a" "b" "c")))
FAIL in (test28) (28.clj:12)
expected: (= (my-flatten (quote ((((:a)))))) (quote (:a)))
actual: (not (= ((((:a)))) (:a)))
Ran 1 tests containing 3 assertions.
3 failures, 0 errors.
@vijaykiran
Copy link

Try this:

(defn my-flatten
  [x]
  (if-not (or (seq? x) (vector? x))
    [x]
    (mapcat my-flatten x)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment