Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

CLOJURE: How to find date-diff in days and also optimized way of doing nested loop in Clojure

I am a newbie to Clojure and trying to do some simple validation to verify courses completed by a students are having gap in days, if so then how many days.

Wanted to iterate over the courses and find such courses where the difference between course1 and course2 are more than 20days, this will be done by finding the difference between (start-date of course2 - end-date of course1)

here is my test-data-set, where there is a gap of more than 20days between advanced-science-102 and datascience-science-101 courses.

[{:course-type "basic-science-101"
  :start-date "2020-01-01"
  :end-date "2020-05-01"} 
 {:course-type "advanced-science-102"
  :start-date "2020-05-15"
  :end-date "2020-10-01"}
 {:course-type "datascience-science-101"
  :start-date "2020-12-01"
  :end-date "2021-03-20"} 
 ]

I have this code so far where I am iterating through the courses and trying to find the difference between start-date and end-date of two courses to decide if there are any gaps between.

    (ns student-course-ruleset.courses-test
  (:require [java-time :as jt]))


(def mycourses [{:course-type "basic-science-101"
                 :start-date "2020-01-01"
                 :end-date "2020-05-01"}
                {:course-type "advanced-science-102"
                 :start-date "2020-05-20"
                 :end-date "2020-10-01"}
                {:course-type "datascience-science-101"
                 :start-date "2020-11-15"
                 :end-date "2021-01-20"}])


(defn select-values [map ks]
  (remove nil? (reduce #(conj %1 (map %2)) [] ks)))

(defn find-gap-in-course [mycourses]
  (loop [[course1 & mycourses] mycourses]
    (loop [[course2 & mycourses] mycourses]

      (when (and (not-empty course1) (not-empty course2))
        (println "Finding gap of courses : " course1 course2)
        (println "Current courses in comparison are : " (select-values course1 [:course-type])
                 (select-values course2 [:course-type]))
        
        (println "Finding Gap in courses:  ")
        ;; (jt/gap (select-values course2 [:start-date]) (select-values course1 [:end-date]))
        )
      )
    (if course1 (recur mycourses))
    ))


(find-gap-in-course mycourses)
question from:https://stackoverflow.com/questions/65876912/clojure-how-to-find-date-diff-in-days-and-also-optimized-way-of-doing-nested-lo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can test with (t/interval) from clj-time

https://github.com/clj-time/clj-time


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...