Back

rename (clj)

(source)

function

(rename xrel kmap)
Returns a rel of the maps in xrel 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
  (are [x y] (= x y)
    (set/rename compositions {:name :title}) #{{:title "Art of the Fugue" :composer "J. S. Bach"}
                                               {:title "Musical Offering" :composer "J. S. Bach"}
                                               {:title "Requiem" :composer "Giuseppe Verdi"}
                                               {:title "Requiem" :composer "W. A. Mozart"}}
    (set/rename compositions {:year :decade}) #{{:name "Art of the Fugue" :composer "J. S. Bach"}
                                                {:name "Musical Offering" :composer "J. S. Bach"}
                                                {:name "Requiem" :composer "Giuseppe Verdi"}
                                                {:name "Requiem" :composer "W. A. Mozart"}}
    (set/rename #{{}} {:year :decade}) #{{}}))

(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"}))
borkdude/speculative
(ns speculative.set
  (:require [clojure.set :as set]
            [clojure.spec.alpha :as s]
            [speculative.specs :as ss]))

(s/fdef set/rename-keys
  :args (s/cat :map ::nilable-map
               :kmap ::nilable-map)
  :ret ::nilable-map)

(s/fdef set/rename
  :args (s/cat :xrel ::rel
               :kmap ::nilable-map)
  :ret ::ss/set)
clojure-finance/clojask
(ns clojask.classes.ColInfo
  (:require [clojure.set :as set]
            [clojask.utils :refer []]))

(definterface ColIntf
  (init [colNames])
  (operate [operation col])
  (operate [operation col newCol])
  (setType [operation col])
  (getDesc [] "get column description")
  (getType [] "get column type")
  (getKeys [] "get collection of keys")
  (getKeyIndex [] "get map with key = column name, value = index")
  (getIndexKey [] "get map with key = index, value = column name")
  (getDeletedCol [] "get indices of deleted columns")
  (setFormatter [format col])
  (getFormatter [])
  (delCol [col-to-del])
  (setColInfo [new-col-set])
  (renameColInfo [old-col new-col])
  (copy [] "copy all the information for rollback purpose")
  (rollback [] "undo the change making use of the copied")
  (commit [])
  )

  (renameColInfo
    [this old-col new-col]
    (.copy this)
    (set! col-keys (mapv (fn [_] (if (= _ old-col) new-col _)) col-keys))
    (let [index (get key-index old-col)]
      (set! key-index (set/rename-keys key-index {old-col new-col}))
      (set! index-key (update index-key index (fn [_] new-col)))))
clojure-finance/clojask
(ns clojask.classes.JoinedDataFrame
  (:require [clojure.set :as set]
            [clojask.classes.ColInfo :refer [->ColInfo]]
            [clojask.classes.RowInfo :refer [->RowInfo]]
            [clojask.classes.DataStat :refer [->DataStat]]
            [clojask.classes.MGroup :refer [->MGroup ->MGroupJoin ->MGroupJoinOuter]]
            [clojask.classes.DataFrame :refer [->DataFrame]]
            [clojask.onyx-comps :refer [start-onyx start-onyx-aggre-only start-onyx-groupby start-onyx-join]]
            ;; [clojask.aggregate.aggre-onyx-comps :refer [start-onyx-aggre]]
            [clojask.join.outer-onyx-comps :refer [start-onyx-outer]]
            [clojure.java.io :as io]
            [clojask.utils :as u])
  (:import
   [clojask.classes.ColInfo ColInfo]
   [clojask.classes.RowInfo RowInfo]
   [clojask.classes.DataStat DataStat]
   [clojask.classes.MGroup MGroup MGroupJoin MGroupJoinOuter]
   [clojask.classes.DataFrame GenDFIntf DataFrame]
   [com.clojask.exception TypeException OperationException]))

  (preview
    [this sample-size output-size format]
    (let [data-a (.preview a sample-size output-size format)
          data-b (.preview b sample-size output-size format)
          old-a (.getColNames a)
          old-b (.getColNames b)
          rep-key-a (zipmap old-a (take (count old-a) (.getColNames this)))
          rep-key-b (zipmap old-b (take-last (count old-b) (.getColNames this)))
          data-a (map #(set/rename-keys % rep-key-a) data-a)
          data-b (map #(set/rename-keys % rep-key-b) data-b)
          data (map (fn [row-a row-b] (merge row-a row-b)) data-a data-b)]
      data))

  (compute
    [this ^int num-worker ^String output-dir ^boolean exception ^boolean order select ifheader out inmemory]
    (let [select (if (coll? select) select [select])
          select (if (= select [nil])
                   (vec (take (+ (count (.getKeyIndex (.col-info a))) (count (.getKeyIndex (.col-info b)))) (iterate inc 0)))
                   (mapv (fn [key] (.indexOf (.getColNames this) key)) select))
          a-index (vec (apply sorted-set (remove (fn [num] (>= num (count (.getKeyIndex (.col-info a))))) select)))
          ;; a-write 
          b-index (mapv #(- % (count (.getKeyIndex (.col-info a)))) (apply sorted-set (remove (fn [num] (< num (count (.getKeyIndex (.col-info a))))) select)))
          b-index (if b-roll (vec (apply sorted-set (conj b-index b-roll))) b-index)
          b-roll (if b-roll (count (remove #(>= % b-roll) b-index)) nil)
          ;; b-write
          a-format (set/rename-keys (.getFormatter (.col-info a)) (zipmap a-index (iterate inc 0)))
          b-format (set/rename-keys (.getFormatter (.col-info b)) (zipmap b-index (iterate inc 0)))
          write-index (mapv (fn [num] (count (remove #(>= % num) (concat a-index (mapv #(+ % (count (.getKeyIndex (.col-info a)))) b-index))))) select)
          ;; test (println a-index b-index b-format write-index b-roll)
          mgroup-a (MGroupJoinOuter. (transient {}) (transient {}) false)
          mgroup-b (if (not= type 3) (MGroupJoin. (transient {}) (transient {}) (or (= 4 type) (= 5 type))) (MGroupJoinOuter. (transient {}) (transient {}) (or (= 4 type) (= 5 type))))
          ]
      ;; (u/init-file output-dir)
      ;; print column names
      (if (= ifheader true) (.printCol this output-dir select out))
      (cond
        (or (= type 0) (= type 1) (= type 2)) ;; inner left right join
        (do
          (if inmemory
            (start-onyx-groupby num-worker 10 b mgroup-b b-keys b-index exception)
            (start-onyx-groupby num-worker 10 b "./.clojask/join/b/" b-keys b-index exception :format true))
          (.final mgroup-b)
          (start-onyx-join num-worker 10 a b (if inmemory mgroup-b nil) output-dir exception a-keys b-keys a-roll b-roll type limit a-index (vec (take (count b-index) (iterate inc 0))) b-format write-index out))
        (= type 3) ;; outer join
        (do
          (if inmemory
            (do
              (start-onyx-groupby num-worker 10 a mgroup-a a-keys a-index exception)
              (start-onyx-groupby num-worker 10 b mgroup-b b-keys b-index exception)
              (.final mgroup-a)
              ;; (.final mgroup-b)
              )
            (do
              (start-onyx-groupby num-worker 10 a "./.clojask/join/a/" a-keys a-index exception :format true)
              (start-onyx-groupby num-worker 10 b "./.clojask/join/b/" b-keys b-index exception :format true)))
          (start-onyx-outer num-worker 10 a b (if inmemory mgroup-a nil) (if inmemory mgroup-b nil) output-dir exception a-index b-index a-format b-format write-index out))
        (or (= type 4) (= type 5))  ;; rolling join
        (do
          (if inmemory
            (start-onyx-groupby num-worker 10 b mgroup-b b-keys b-index exception)
            (start-onyx-groupby num-worker 10 b "./.clojask/join/b/" b-keys b-index exception))
          (.final mgroup-b)
          (start-onyx-join num-worker 10 a b (if inmemory mgroup-b nil) output-dir exception a-keys b-keys a-roll b-roll type limit a-index (vec (take (count b-index) (iterate inc 0))) b-format write-index out))))))
madvas/fractalify
(ns fractalify.fractals.schemas
  (:require [schema.core :as s]
            [fractalify.workers.schemas :as wch]
            [fractalify.users.schemas :as uch]
            [fractalify.main.schemas :as mch]
            [fractalify.utils :as u]
            [clojure.set :as set]))

(s/defschema RenderableCanvas
  (-> Canvas
      (set/rename-keys {(o :lines) :lines})
      (merge mch/FormErros)))
walkable-server/realworld-fulcro
(ns conduit.handler.mutations
  (:require [conduit.boundary.user :as user]
            [conduit.boundary.article :as article]
            [clojure.set :refer [rename-keys]]
            [conduit.util :as util]
            [buddy.sign.jwt :as jwt]
            [duct.logger :refer [log]]
            [com.wsscode.pathom.connect :as pc :refer [defmutation]]
            [com.fulcrologic.fulcro.algorithms.tempid :refer [tempid?]]))

(defmutation submit-comment
  [{:app/keys [db] current-user :app.auth/current-user} {:keys [article-id diff]}]
  {}
  (if current-user
    (let [[_ comment-id] (util/get-ident diff)
          comment-item   (-> (util/get-item diff)
                             (rename-keys remove-comment-namespace))]
      (if (tempid? comment-id)
        (let [new-id (article/create-comment db current-user article-id comment-item)]
          {:tempids {comment-id new-id}})
        (article/update-comment db current-user comment-id comment-item)))
    {}))