Back
rename-keys (clj)
(source)function
(rename-keys map kmap)
Returns the map with the keys in kmap renamed to the vals in kmap
Examples
clojure
(ns clojure.test-clojure.protocols
(:use clojure.test clojure.test-clojure.protocols.examples)
(:require [clojure.test-clojure.protocols.more-examples :as other]
[clojure.set :as set]
clojure.test-helper)
(:import [clojure.test_clojure.protocols.examples ExampleInterface]))
(deftest defrecord-acts-like-a-map
(let [rec (r 1 2)]
(is (.equals (r 1 3 {} {:c 4}) (merge rec {:b 3 :c 4})))
(is (.equals {:foo 1 :b 2} (set/rename-keys rec {:a :foo})))
(is (.equals {:a 11 :b 2 :c 10} (merge-with + rec {:a 10 :c 10})))))
clojure
(ns clojure.test-clojure.metadata
(:use clojure.test
[clojure.test-helper :only (eval-in-temp-ns)])
(:require [clojure.set :as set]))
(deftest interaction-of-def-with-metadata
(testing "initial def sets metadata"
(let [v (eval-in-temp-ns
(def ^{:a 1} foo 0)
#'foo)]
(is (= 1 (-> v meta :a)))))
(testing "const vars preserve metadata"
(let [[v1 v2] (eval-in-temp-ns
(def ^:const foo ^:foo [])
(def ^:const bar ^:foo [:bar])
[(meta foo) (meta bar)])]
(is (= {:foo true} v1))
(is (= {:foo true} v2))))
#_(testing "subsequent declare doesn't overwrite metadata"
(let [v (eval-in-temp-ns
(def ^{:b 2} bar 0)
(declare bar)
#'bar)]
(is (= 2 (-> v meta :b))))
(testing "when compiled"
(let [v (eval-in-temp-ns
(def ^{:c 3} bar 0)
(defn declare-bar []
(declare bar))
(declare-bar)
#'bar)]
(is (= 3 (-> v meta :c))))))
(testing "subsequent def with init-expr *does* overwrite metadata"
(let [v (eval-in-temp-ns
(def ^{:d 4} quux 0)
(def quux 1)
#'quux)]
(is (nil? (-> v meta :d))))
(testing "when compiled"
(let [v (eval-in-temp-ns
(def ^{:e 5} quux 0)
(defn def-quux []
(def quux 1))
(def-quux)
#'quux)]
(is (nil? (-> v meta :e))))))
(testing "IllegalArgumentException should not be thrown"
(testing "when defining var whose value is calculated with a primitive fn."
(testing "This case fails without a fix for CLJ-852"
(is (eval-in-temp-ns
(defn foo ^long [^long x] x)
(def x (inc (foo 10))))))
(testing "This case should pass even without a fix for CLJ-852"
(is (eval-in-temp-ns
(defn foo ^long [^long x] x)
(def x (foo (inc 10)))))))))
(deftest fns-preserve-metadata-on-maps
(let [xm {:a 1 :b -7}
x (with-meta {:foo 1 :bar 2} xm)
ym {:c "foo"}
y (with-meta {:baz 4 :guh x} ym)]
(is (= xm (meta (:guh y))))
(is (= xm (meta (reduce #(assoc %1 %2 (inc %2)) x (range 1000)))))
(is (= xm (meta (-> x (dissoc :foo) (dissoc :bar)))))
(let [z (assoc-in y [:guh :la] 18)]
(is (= ym (meta z)))
(is (= xm (meta (:guh z)))))
(let [z (update-in y [:guh :bar] inc)]
(is (= ym (meta z)))
(is (= xm (meta (:guh z)))))
(is (= xm (meta (get-in y [:guh]))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (merge x y))))
(is (= ym (meta (merge y x))))
(is (= xm (meta (merge-with + x y))))
(is (= ym (meta (merge-with + y x))))
(is (= xm (meta (select-keys x [:bar]))))
(is (= xm (meta (set/rename-keys x {:foo :new-foo}))))
;; replace returns a seq when given a set. Can seqs have
;; metadata?
;; TBD: rseq, subseq, and rsubseq returns seqs. If it is even
;; possible to put metadata on a seq, does it make sense that the
;; seqs returned by these functions should have the same metadata
;; as the sorted collection on which they are called?
))
(deftest fns-preserve-metadata-on-vectors
(let [xm {:a 1 :b -7}
x (with-meta [1 2 3] xm)
ym {:c "foo"}
y (with-meta [4 x 6] ym)]
(is (= xm (meta (y 1))))
(is (= xm (meta (assoc x 1 "one"))))
(is (= xm (meta (reduce #(conj %1 %2) x (range 1000)))))
(is (= xm (meta (pop (pop (pop x))))))
(let [z (assoc-in y [1 2] 18)]
(is (= ym (meta z)))
(is (= xm (meta (z 1)))))
(let [z (update-in y [1 2] inc)]
(is (= ym (meta z)))
(is (= xm (meta (z 1)))))
(is (= xm (meta (get-in y [1]))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (replace {2 "two"} x))))
(is (= [1 "two" 3] (replace {2 "two"} x)))
;; TBD: Currently subvec drops metadata. Should it preserve it?
;;(is (= xm (meta (subvec x 2 3))))
;; TBD: rseq returns a seq. If it is even possible to put
;; metadata on a seq, does it make sense that the seqs returned by
;; these functions should have the same metadata as the sorted
;; collection on which they are called?
))
(deftest fns-preserve-metadata-on-sets
;; TBD: Do tests independently for set, hash-set, and sorted-set,
;; perhaps with a loop here.
(let [xm {:a 1 :b -7}
x (with-meta #{1 2 3} xm)
ym {:c "foo"}
y (with-meta #{4 x 6} ym)]
(is (= xm (meta (y #{3 2 1}))))
(is (= xm (meta (reduce #(conj %1 %2) x (range 1000)))))
(is (= xm (meta (-> x (disj 1) (disj 2) (disj 3)))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (set/select even? x))))
(let [cow1m {:what "betsy cow"}
cow1 (with-meta {:name "betsy" :id 33} cow1m)
cow2m {:what "panda cow"}
cow2 (with-meta {:name "panda" :id 34} cow2m)
cowsm {:what "all the cows"}
cows (with-meta #{cow1 cow2} cowsm)
cow-names (set/project cows [:name])
renamed (set/rename cows {:id :number})]
(is (= cowsm (meta cow-names)))
(is (= cow1m (meta (first (filter #(= "betsy" (:name %)) cow-names)))))
(is (= cow2m (meta (first (filter #(= "panda" (:name %)) cow-names)))))
(is (= cowsm (meta renamed)))
(is (= cow1m (meta (first (filter #(= "betsy" (:name %)) renamed)))))
(is (= cow2m (meta (first (filter #(= "panda" (:name %)) renamed))))))
;; replace returns a seq when given a set. Can seqs have
;; metadata?
;; union: Currently returns the metadata of the largest input set.
;; This is an artifact of union's current implementation. I doubt
;; any explicit design decision was made to do so. Like join,
;; there doesn't seem to be much reason to prefer the metadata of
;; one input set over another, if at least two input sets are
;; given, but perhaps defining it to always return a set with the
;; metadata of the first input set would be reasonable?
;; intersection: Returns metadata of the smallest input set.
;; Otherwise similar to union.
;; difference: Seems to always return a set with metadata of first
;; input set. Seems reasonable. Not sure we want to add a test
;; for it, if it is an accident of the current implementation.
;; join, index, map-invert: Currently always returns a value with
;; no metadata. This seems reasonable.
))
clojure
(ns clojure.test-clojure.clojure-set
(:use clojure.test)
(:require [clojure.set :as set]))
(deftest test-rename-keys
(are [x y] (= x y)
(set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
(set/rename-keys {:a "one" :b "two"} {:a :z :c :y}) {:z "one" :b "two"}
(set/rename-keys {:a "one" :b "two" :c "three"} {:a :b :b :a}) {:a "two" :b "one" :c "three"}))
clojure/clojurescript
(ns clojure.set-test
(:require [clojure.test :refer [are deftest is]]
[clojure.set :as set]))
(deftest test-rename-keys
(are [x y] (= x y)
(set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
(set/rename-keys {:a "one" :b "two"} {:a :z :c :y}) {:z "one" :b "two"}
(set/rename-keys {:a "one" :b "two" :c "three"} {:a :b :b :a}) {:a "two" :b "one" :c "three"}))
arcadia-unity/Arcadia
(ns clojure.test-clojure.clojure-set
(:use clojure.test)
(:require [clojure.set :as set]))
(deftest test-rename-keys
(are [x y] (= x y)
(set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
(set/rename-keys {:a "one" :b "two"} {:a :z :c :y}) {:z "one" :b "two"}
(set/rename-keys {:a "one" :b "two" :c "three"} {:a :b :b :a}) {:a "two" :b "one" :c "three"}))
mcohen01/amazonica
(ns amazonica.test.s3transfer
(:require [amazonica.core :refer [with-credential defcredential]]
[amazonica.aws.s3 :as s3])
(:use [amazonica.aws.s3transfer]
[clojure.set]
[clojure.test]))
(def cred
(let [access "aws_access_key_id"
secret "aws_secret_access_key"
file "/.aws/credentials"
creds (-> "user.home"
System/getProperty
(str file)
^String slurp
(.split "\n"))]
(clojure.set/rename-keys
(reduce
(fn [m ^String e]
(let [pair (.split e "=")]
(if (some #{access secret} [(first pair)])
(apply assoc m pair)
m)))
{}
creds)
{access :access-key secret :secret-key})))
mcohen01/amazonica
(ns amazonica.test.s3
(:import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
com.amazonaws.services.s3.model.CORSRule
com.amazonaws.services.s3.model.ObjectListing
org.joda.time.DateTime
java.io.BufferedInputStream
java.io.File
java.io.FileInputStream
java.text.SimpleDateFormat
java.util.Date
java.util.UUID
java.security.KeyPairGenerator
java.security.SecureRandom)
(:require [clojure.string :as str])
(:use [clojure.test]
[clojure.set]
[clojure.pprint]
[amazonica.core]
[amazonica.aws.s3]))
(def cred
(let [access "aws_access_key_id"
secret "aws_secret_access_key"
file "/.aws/credentials"
creds (-> "user.home"
System/getProperty
(str file)
^String (slurp)
(.split "\n"))]
(clojure.set/rename-keys
(reduce
(fn [m ^String e]
(let [pair (.split e "=")]
(if (some #{access secret} [(first pair)])
(apply assoc m pair)
m)))
{}
creds)
{access :access-key secret :secret-key})))
borkdude/jet
(ns jet.query-test
(:require
[clojure.test :as test :refer [deftest is]]
[jet.data-readers]
[jet.query :refer [query]]
[clojure.set :as set]))
(deftest query-test
(is (= nil (query [] false)))
(is (= '1 (query {:a 1 :b 2} :a)))
(is (= '1 (query {1 1} 1)))
(is (= '1 (query {"1" 1} "1")))
(is (= '{:a 1} (query {:a 1 :b 2} '(select-keys [:a]))))
(is (= {:a #:a{:a 1}} (query {:a {:a/a 1 :a/b 2} :b 2} '[(select-keys [:a]) (update :a (select-keys [:a/a]))])))
(is (= {:a [#:a{:a 1}]} (query {:a [{:a/a 1 :a/b 2}] :b 2} '[(select-keys [:a]) (update :a (map (select-keys [:a/a])))])))
(is (= {:a 1, :c 3} (query {:a 1 :b 2 :c 3} '(dissoc :b))))
(is (= [1] (query [1 2 3] '(take #jet/lit 1))))
(is (= [2 3] (query [1 2 3] '(drop #jet/lit 1))))
(is (= {:a [1]} (query {:a [1 2 3]} '(update :a (take #jet/lit 1)))))
(is (= {:a 2} (query {:a [1 2 3]} '(update :a (nth #jet/lit 1)))))
(is (= {:a 1} (query {:a [1 2 3]} '(update :a first))))
(is (= 3 (query {:a [1 2 3]} '(last :a))))
(is (= {:a 3} (query {:a [1 2 3]} '(update :a last))))
(is (= {:foo [:bar]} (query {:foo {:bar 2}} '(update :foo keys))))
(is (= {:a [:a :b]} (query {:b {:a 1 :b 2}} '{:a (keys :b)})))
(is (= {:foo [2]} (query {:foo {:bar 2}} '(update :foo vals))))
(is (= [3 6] (query [[1 2 3] [4 5 6]] '(map last))))
(is (= [1 2] (query [{:a 1} {:a 2}] '(map :a))))
(is (= [1 2] (query [{:a 1} {:a 2}] '(map :a))))
(is (= [1 2] (query {:a 1 :b 2 :c 3} '[(dissoc :c) (vals)])))
(is (= {:foo 1 :bar 1}
(query {:foo {:a 1 :b 2} :bar {:a 1 :b 2}} '(map-vals :a))))
(is (= {:a 1 :b 2 :c 3}
(query {:keys [:a :b :c] :vals [1 2 3]} '[(juxt :keys :vals) (zipmap)])))
(is (= '[{:name foo :private true}]
(query '[{:name foo :private true}
{:name bar :private false}] '(filter :private))))
(is (= 1 (query '[{:name foo :private true}
{:name bar :private false}] '[(filter :private) (count)])))
(is (= '[{:name foo, :private true}]
(query '[{:name foo :private true}
{:name bar :private false}] '(filter (= :name (quote foo))))))
(is (= '[{:a 2} {:a 3}]
(query '[{:a 1} {:a 2} {:a 3}] '(filter (>= :a (quote 2))))))
(is (= '[{:a 1} {:a 2}]
(query '[{:a 1} {:a 2} {:a 3}] '(filter (<= :a (quote 2))))))
(is (= '[{:a 1} {:a 2}]
(query '[{:a 1} {:a 2} {:a 3}] '(filter (not= :a (quote 3))))))
(is (= '[{:a 1} {:a 2}]
(query '[{:a 1} {:a 2} {:a 3}] '(remove (= :a (quote 3))))))
(is (= '[{:a 1}]
(query '[{:a 1} [] []] '(filter first))))
(is (= '[{:a 1, :b 3}]
(query '[{:a 1 :b 2} {:a 1 :b 3}] '(filter (> :b #jet/lit 2)))))
(is (= false (query {:a 1 :b 1 :c 1} '(not= :a :b :c))))
(is (= '{:a 1 :b 2}
(query '{:a 1 :b 2 :c 3} '(select-keys [:a :b]))))
(is (= '{:c 3}
(query '{:a 1 :b 2 :c 3} '(dissoc :a :b))))
(is (= '{:b 1}
(query '{:a 1} '(set/rename-keys {:a :b}))))
(is (= '{:foo 1 :bar 2}
(query '{:a 1 :b 2} '(hash-map :foo :a :bar :b))))
(is (= '{:a 1 :b 3}
(query '{:a 1 :b 2} '(assoc :b (quote 3)))))
(is (= '{:a 1 :b 3}
(query '{:a 1 :b 2} '(assoc :b #jet/lit 3))))
(is (= '{:a 1}
(query nil '(hash-map :a (quote 1)))))
(is (= '{:a 1}
(query nil '{:a (quote 1)})))
(is (= '{:a 1 :b 1}
(query {:a 1} '(assoc :b :a))))
(is (= '{:a 1}
(query {:a {:b 1}} '(update :a :b))))
(is (= '{:a 1}
(query {:a [1 2 3]} '(update :a first))))
(is (= '{:a {:a 2} :b 2}
(query {:a {:a 1} :b 2} '(assoc-in [:a :a] :b))))
(is (= '{:a {:a 1}}
(query {:a {:a {:b 1}}} '(update-in [:a :a] :b))))
(is (= '{:a {:a 1}}
(query {:a {:a [1 2 3]}} '(update-in [:a :a] first))))
(is (= 1 (query {:a 1} '(get :a))))
(is (= 100 (query [100 1 2] '(get 0))))
(is (= 1 (query (list 1 2 3) '0)))
(is (= [1 2 3] (query {:a [1 1 2 2 3 3 1 1]} '[:a (distinct)])))
(is (= [1 2 3 1] (query {:a [1 1 2 2 3 3 1 1]} '[:a (dedupe)])))
(is (= "foo bar" (query {:a "foo bar" :b 2} '(if (re-find #jet/lit "foo" :a) :a :b))))
(is (= 2 (query {:a "foo bar" :b 2} '(if (re-find #jet/lit "baz" :a) :a :b))))
(is (= "1/2" (query {:a 1 :b 2} '(str :a #jet/lit "/" :b))))
(is (= {:input {:a 3, :b 2}, :product 6}
(query {:a 3 :b 2} '{:input (identity) :product (* :a :b)})))
(is (= 2 (query {:a 3 :b 2} '(identity :b))))
(is (= nil (query {:b 3} '(and :a :b))))
(is (= false (query {:a false} '(and :a :b))))
(is (= 2 (query {:a 1 :b 2} '(and :a :b))))
(is (= 3 (query {:b 3} '(or :a :b))))
(is (= true (query {:b 3} '(not :a))))
(is (= 4 (query {:b 3} '(inc :b))))
(is (= 2 (query {:b 3} '(dec :b))))
(is (= 3 (query [1 2 3] 'last)))
(is (= 4 (query '{(inc :a) 4} '(inc :a))))
(is (= {:a 1 :b 2} (query {:a 1 :b 2 :c 3 :d 4} '(select-keys [:a :b]))))
(is (= 10 (query 0 '(while (< id #jet/lit 10) (inc id)))))
(is (= [0 1 1 2 3 5 8 13 21 34 55]
(query {:fib0 0 :fib1 1 :n 0 :fib []}
'[(while (<= :n #jet/lit 10)
{:fib0 :fib1 :fib1 (+ :fib0 :fib1) :n (inc :n) :fib (conj :fib :fib0)})
:fib])))
(is (= [1 2 3 4 5 6] (query {:a [1 2 3] :b [4 5 6]} '(into :a :b))))
(is (= {:x 1 :y 2} (query {:a {:x 1} :b {:y 2}} '(into :a :b))))
(is (= "{:b {:c 10}}\n{:c 10}\n"
(with-out-str (query {:a {:b {:c 10}}} '[:a jet/debug :b jet/debug :c]))))
(is (= #{3} (query nil '(set/difference #jet/lit #{1 2 3} #jet/lit #{1 2}))))
(is (= #{3} (query #{1 2 3} '(set/difference #jet/lit #{1 2}))))
(is (= #{3} (query #{1 2 3} '(set/difference #jet/lit #{1 2}))))
(is (= (symbol "1.10") (query {:version "1.10"} '[:version symbol])))
(is (= [1 2 3 4] (query [1 2 3 4 5 1 2] '(take-while (< id #jet/lit 5)))))
(is (= [5 1 2] (query [1 2 3 4 5 1 2] '(drop-while (< id #jet/lit 5))))))