By user2114063


2018-08-10 12:04:34 8 Comments

I am new to clojure, my function elligble voters is not returning the vector, where am i going wrong.

(def human-db
  [
   {:name "Kanishk" :age 28 :sex "male"}
   {:name "Kanishk1" :age 29 :sex "male"}
   {:name "Kanishk2" :age 0 :sex "female"}
   {:name "Kanishk3" :age 1 :sex "male"}
   {:name "Kanishk4" :age 3 :sex "female"}
   {:name "Kanishk5" :age 3 :sex "male"}
   {:name "Kanishk6" :age 3 :sex "female"}
   {:name "Kanishk7" :age 3 :sex "male"}
   {:name "Kanishk8" :age 3 :sex "female"}])


(defn elligble-voters
  [human-db]
  (reduce (fn
            [new-map map]
            (if (> (:age map) 18)
              (conj new-map map)))  [] human-db))
(elligble-voters human-db)

2 comments

@birdspider 2018-08-10 12:17:36

while reduce if capable of doing what you want, use something more suited for filtering a collection.

i.e. filter

clojure.core/filter ([pred] [pred coll])

Returns a lazy sequence of the items in coll for which (pred item) returns logical true. pred must be free of side-effects. Returns a transducer when no collection is provided.


(defn elligble-voters [human-db]
  (filter #(> (:age %) 18) human-db))

#(> (:age %) 18) is an shorthand for an anonymous function, equivalent to

(fn [x] 
  (> (:age x) 18))

@Marc O'Morain 2018-08-10 12:10:31

The problem is with your if expression - there is no else clause, so it is returning nil when the voter is aged less than 18.

The last item in human-db is aged 3, so the if returns nil, and thus reduce returns nil.

This works:

(defn elligble-voters [human-db]
  (reduce (fn [new-map map]
            (if (> (:age map) 18)
              (conj new-map map)
              new-map)) ;; <-- Added `new-map` here
          []
          human-db))

A neater way to express the same function would be like this:

(filter (fn [{:keys [age]}] (> age 18)) human-db)

@Thumbnail 2018-08-10 13:23:55

... or (filter #(-> % (:age) (> 18)) human-db).

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Ambiguity in Clojure attribute map

  • 2010-10-22 11:27:11
  • Ralph
  • 773 View
  • 3 Score
  • 1 Answer
  • Tags:   clojure

13 Answered Questions

2 Answered Questions

[SOLVED] How to quote an argument in clojure

1 Answered Questions

[SOLVED] clojure sort always return a list

  • 2016-02-18 22:55:31
  • Zhe Hu
  • 124 View
  • 2 Score
  • 1 Answer
  • Tags:   clojure

6 Answered Questions

[SOLVED] Scala vs. Groovy vs. Clojure

4 Answered Questions

[SOLVED] Put an element to the tail of a collection

  • 2011-04-20 17:45:22
  • Michiel Borkent
  • 38626 View
  • 46 Score
  • 4 Answer
  • Tags:   collections clojure

8 Answered Questions

[SOLVED] Common programming mistakes for Clojure developers to avoid

  • 2010-01-07 13:27:53
  • fogus
  • 20653 View
  • 92 Score
  • 8 Answer
  • Tags:   clojure

3 Answered Questions

[SOLVED] Integrating Clojure into an existing Java project?

  • 2010-02-02 02:51:11
  • anon
  • 3282 View
  • 12 Score
  • 3 Answer
  • Tags:   clojure

1 Answered Questions

2 Answered Questions

[SOLVED] Clojure Inconsistent results in clojure during executions of a function

  • 2011-10-21 08:24:10
  • kostas
  • 114 View
  • 0 Score
  • 2 Answer
  • Tags:   clojure

Sponsored Content