Given the sequence of mappings in Clojure, how do I filter the key value> (some value)?

I have a Clojure map sequence that looks something like this:

({:date "2000-01-01" :value 123} 
 {:date "2000-01-02" :value 487} 
 ... 
 {:date "2014-05-01" :value 17})

I want to build a function that returns a similarly structured sequence of cards, but contains only those cards where: date values ​​between, for example, "2005-11-14" and "2007-08-03".

In their current form, YYYY-MM-DD, the dates are comparable, so it (compare "2000-01-02" "2001-03-04")works as expected. However, I cannot figure out how to extract the values ​​from the date and compare them.

I have done this before (filter #(> 0 (compare (:date %)) "2099-12-09") data), but cannot get any more. Help!

+4
source share
1

Clojure, , , , . , x - , (x :date). .

(def data '({:date "2005-11-13", :value 0}
         {:date "2005-11-15", :value 1}
         {:date "2007-08-02", :value 2}
         {:date "2007-08-04", :value 3}))
(print (filter
         #(and
               (> (compare (% :date) "2005-11-14") 0)
               (< (compare (% :date) "2007-08-03") 0))
         data))

({:date 2005-11-15, :value 1} {:date 2007-08-02, :value 2})

:   , .

 (print (filter
         #(let [x (% :date)]
           (and
              (> (compare x "2005-11-14") 0)
              (< (compare x "2007-08-03") 0)))
         data))
+7

Source: https://habr.com/ru/post/1541992/


All Articles