Skip to content

Commit

Permalink
Fixed index-of for clojure.
Browse files Browse the repository at this point in the history
  • Loading branch information
DWiechert committed Aug 26, 2016
1 parent b1d3e6e commit 1c7614d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
19 changes: 8 additions & 11 deletions src/main/clojure/lists/ClojureLists.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:require [clojure.core])
(:gen-class))


(defn my-sum [& args]
(reduce + args))

Expand All @@ -18,14 +17,12 @@
(defn exists [e elements]
(> (my-count e elements) 0))

; Currentlu this doesn't work, but should try to figure it out
;(defn- index-of-with-index
; ([e i] (do (println e i) -1))
; ([e i x] (do (println e i x) (if (= e x) i -1)))
; ([e i x & more] (do (println e i x more) (if (= e x) i (index-of-with-index e (+ i 1) more)))))
;
;(defn index-of [e & args]
; (index-of-with-index e 0 args))
(defn- index-of-with-index [e i elements]
(if (empty? elements)
-1
(if (= e (first elements))
i
(index-of-with-index e (+ 1 i) (rest elements)))))

(defn index-of2 [e & more]
(.indexOf more e))
(defn index-of [e elements]
(index-of-with-index e 0 elements))
20 changes: 10 additions & 10 deletions src/test/clojure/lists/ClojureListsTest.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
(testing "my-max"
(is (= 3 (my-max 1 2 3)))))

(deftest my-count-found
(testing "my-count-found"
(is (= 2 (my-count "a" (seq ["a" "a" "b"]))))))

(deftest my-count-not-found
(testing "my-count-not-found"
(is (= 0 (my-count 4 (seq [1 2 3]))))))

(deftest exists-true
(testing "exists-true"
(is (exists "a" (seq ["b" "b" "a"])))))
Expand All @@ -20,16 +28,8 @@

(deftest index-of-found
(testing "index-of-found"
(is (= 1 (index-of2 "b" "a" "b")))))
(is (= 3 (index-of "d" (seq ["a" "b" "c" "d"]))))))

(deftest index-of-not-found
(testing "index-of-not-found"
(is (= -1 (index-of2 4 1 2 3)))))

(deftest my-count-found
(testing "my-count-found"
(is (= 2 (my-count "a" (seq ["a" "a" "b"]))))))

(deftest my-count-not-found
(testing "my-count-not-found"
(is (= 0 (my-count 4 (seq [1 2 3]))))))
(is (= -1 (index-of "e" (seq ["a" "b" "c" "d"]))))))

0 comments on commit 1c7614d

Please sign in to comment.