【发布时间】:2020-05-27 19:04:26
【问题描述】:
我有以下格式的数据。
'(({:nums {:test number?, :data (1)}})
{:Other ()}
({:nums {:test number?, :data (2)}})
({:nums {:test number?, :data (3 4)}}))
但我想获取以下格式的数据。
'(({:nums {:test number?, :data (1 2 3 4)}})
{:Other ()})
尝试了合并、联合功能,但没有奏效。 你能帮帮我吗,因为我是 Closure 的初学者。
对以下代码进行测试
(split '(1 2 (3 4 [1 2 3])) {:nums {:test number?}})
=>
((({:nums {:test number?, :data (1)}}) {:Other ()})
(({:nums {:test number?, :data (2)}}) {:Other ()})
(({:nums {:test number?, :data (3 4)}}) {:Other (([1 2 3]))}))
但我期望的目标结果如下
(split '(1 2 (3 4 [1 2 3])) {:nums {:test number?}})
=>
(({:nums {:test number?, :data (1 2 3 4)}})
{:Other ([1 2 3])})
使用的代码:
(defn test
[list1 filter1]
;(get-in (first (first (split list1 filter1))) [:nums :test])
(first (first (split list1 filter1))))
(defn split [list1 filter_map]
(if (empty? list1)
()
(cons (split_list
(if (sequential? (first list1))
(first list1)
(list (first list1)))
filter_map)
(split (rest list1) filter_map))))
(defn split_list [list1 filter_map]
(let [var_in_filter (remove nil? (in-filter filter_map))
var_out_filter (remove nil? (out-filter filter_map))]
;var_out_filter
(list (in_filter_recur list1 var_in_filter)
;(in_filter_recur list1 var_in_filter)
(hash-map :Other (remove nil?
(out_filter_recur list1 var_out_filter))))))
(defn in_filter_recur [lis in_filt]
(if (empty? in_filt)
nil
(cons (hash-map
(if ((first in_filt) 1)
:nums
;"{:nums {:test number?}"
(if ((first in_filt) 'a)
:syms
(if ((first in_filt) [1])
:vects
'others)))
;(set (merge [:data] (filter (first in_filt) lis))))
(hash-map :test
(if ((first in_filt) 1)
'number?
;"{:nums {:test number?}"
(if ((first in_filt) 'a)
'symbols?
(if ((first in_filt) [1])
'vector?
'others)))
:data (filter (first in_filt) lis)))
(in_filter_recur lis (rest in_filt)))))
(defn out_filter_recur [lis out_filt]
(if (empty? out_filt)
nil
(cons ;(hash-map nil
(if (empty? (filter (first out_filt) lis))
nil
(filter (first out_filt) lis))
(out_filter_recur lis (rest out_filt)))))
(defn out-filter [filt]
(difference
(set
(All-filter
{:nums {:test number?}
:syms {:test symbol?}
:vects {:test vector?}
:other {:test string?}})) (set (in-filter filt))))
(defn in-filter [filt]
(list (when
(and (> (count (str (get-in filt [:nums :test]))) 0)
((get-in filt [:nums :test]) 1))
number?)
(when
(and (> (count (str (get-in filt [:syms :test]))) 0)
((get-in filt [:syms :test]) 'a))
symbol?)
(when
(and (> (count (str (get-in filt [:vects :test]))) 0)
((get-in filt [:vects :test]) [1]))
vector?)))
(defn All-filter [filt]
(list (when
(and (> (count (str (get-in filt [:nums :test]))) 0)
((get-in filt [:nums :test]) 1))
number?)
(when
(and (> (count (str (get-in filt [:syms :test]))) 0)
((get-in filt [:syms :test]) 'a))
symbol?)
(when
(and (> (count (str (get-in filt [:vects :test]))) 0)
((get-in filt [:vects :test]) [1]))
vector?)
(when
(and (> (count (str (get-in filt [:other :test]))) 0)
((get-in filt [:other :test]) "1"))
string?)))
(defn difference
([s1] s1)
([s1 s2]
(if (< (count s1) (count s2))
(reduce (fn [result item]
(if (contains? s2 item)
(disj result item)
result))
s1 s1)
(reduce disj s1 s2)))
([s1 s2 & sets]
(reduce difference s1 (conj sets s2))))
【问题讨论】:
-
请添加您尝试过的代码以及失败的原因,以便我们从那里改进。
-
比这更重要的是,实际目标是什么,更好的规格。有许多潜在的算法可以从给定的输入中产生这种输出,对于不同的输入,它们的行为会有所不同。
-
嗨,Amalloy,我现在添加了代码。请建议我如何获得目标结果。
-
你能先解释一下为什么你的数据是这种奇怪的形状吗?可以改变它的形状吗?
标签: clojure