【发布时间】:2019-03-25 14:07:47
【问题描述】:
我是 clojure 的新手,一直在尝试解决一个问题,其中包含地图向量的问题
({:disease Asthma, :st-dt 2018-2-1, :en-dt 2018-4-1, :dose 0.25}
{:disease Asthma, :st-dt 2018-3-1, :en-dt 2018-6-5, :dose 0.65}
{:disease BP, :st-dt 2018-5-1, :en-dt 2018-9-1, :dose 0.75})
是给定的,我必须得到一个不重叠的数据,有点像
({:disease Asthma, :st-dt 2018-2-1, :en-dt 2018-2-28, :dose 0.25}
{:disease Asthma, :st-dt 2018-3-1, :en-dt 2018-4-1, :dose 0.25}
{:disease Asthma, :st-dt 2018-3-1, :en-dt 2018-4-1, :dose 0.65}
{:disease Asthma, :st-dt 2018-4-2, :en-dt 2018-4-30, :dose 0.65}
{:disease Asthma, :st-dt 2018-5-1, :en-dt 2018-6-5, :dose 0.65}
{:disease BP, :st-dt 2018-5-1, :en-dt 2018-6-5, :dose 0.75}
{:disease BP, :st-dt 2018-6-6, :en-dt 2018-9-1, :dose 0.75})
我尝试过使用循环和递归,但我认为在 if 的两种情况下都不可能递归。
(defn ab [x] (let [temp x olap (f/overlap (f/interval ((first temp ):st-dt) ((first temp ):en-dt))
(f/interval ((second temp):st-dt) ((second temp):en-dt) ))]
(if olap
(into [] (concat [{:med-type ((first temp ):med-type) :st-dt ((first temp ):st-dt)
:en-dt (f/minus ((second temp) :st-dt) (f/days 1)) :dose ((first temp):dose )}
{:med-type ((first temp ):med-type) :st-dt ((second temp ):st-dt)
:en-dt ((first temp) :en-dt) :dose ((first temp):dose )}
{:med-type ((second temp ):med-type) :st-dt ((second temp ):st-dt)
:en-dt ((first temp) :en-dt) :dose ((second temp):dose )}
{:med-type ((second temp ):med-type) :st-dt (f/plus ((first temp ):en-dt) (f/days 1))
:en-dt ((second temp) :en-dt) :dose ((second temp):dose )}]
(into [] (rest (rest x))))))))
【问题讨论】:
标签: recursion vector clojure tail-recursion