[%]
xor-dissocds
츄럴(164.125)
2021-04-07 18:37
추천 0
;; NOTE: if key is ##NaN, it can't dissoc. - can be a bug
;; (xor-dissocds {0 0, 1 0, ##NaN 0} [1]) ;=> ({0 0, ##NaN 0, 1 0})
(
s/fdef xor-dissocds
:args (
s/with-gen (
s/cat :m map? :ks (
s/spec (
s/* any?)))
(
fn []
ug/map-doubtful-ks-noNaN))
:ret (
s/* map?)
:fn (
s/and
#(
ub/=count (
intersection (
-> % :args :m keys set)
(
-> % :args :ks set))
(
:ret %))
(
fn [{{
m :m ks :ks}
:args,
ret-ms :ret}]
(
let [
m-kset (
set (
keys m))
kset (
set ks)
m-diff-ks (
difference m-kset kset)]
(
= (
intersection m-kset kset)
(
set (
map #(
first (
keys (
apply dissoc % m-diff-ks)))
ret-ms)))))))
(
defn xor-dissocds
"Returns a sequence of maps with only one key taken from xor-keys.
Among xor-keys, those that are not map keys are ignored.
example)
m = {:1 2 :3 4 :a 0 :b 1 :c 2} xor-keys = [:a :b :c]
ret = ({:1 2, :3 4, :c 2}
{:1 2, :3 4, :b 1}
{:1 2, :3 4, :a 0})"
[
m xor-keys]
(
let [
contain-ks (
intersection (
set (
keys m)) (
set xor-keys))]
(
map (
fn [
k m]
(
assoc (
apply dissoc m contain-ks)
k (
m k)))
contain-ks (
repeat m))))
https://gall.dcinside.com/mgallery/board/view/?id=github&no=20446&page=2
고맙읍니다
key가 NaN 이전에 float이 될 수 있는겁니까 정말 끔직하네요
이건 모든 타입에 대해서 general하게 작동하는 함수라서 고려해야 함니다
놀라운게 key가 nan이면 dict에서 key 빼는 함수가 작동을 안함.. 저도 몰랐는데 PBT 하다가 알게 됬으요
이... 이게 뭐꼬
마 이기 바로 말로만 듣던 근로저란기라 함 무바라