kandi background
Explore Kits

clojure | The Clojure programming language

 by   clojure Java Version: Current License: No License

 by   clojure Java Version: Current License: No License

Download this library from

kandi X-RAY | clojure Summary

clojure is a Java library. clojure has no vulnerabilities, it has build file available and it has high support. However clojure has 96 bugs. You can download it from GitHub, Maven.
The Clojure programming language
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • clojure has a highly active ecosystem.
  • It has 9419 star(s) with 1386 fork(s). There are 698 watchers for this library.
  • It had no major release in the last 12 months.
  • clojure has no issues reported. There are no pull requests.
  • It has a negative sentiment in the developer community.
  • The latest version of clojure is current.
clojure Support
Best in #Java
Average in #Java
clojure Support
Best in #Java
Average in #Java

quality kandi Quality

  • clojure has 96 bugs (1 blocker, 0 critical, 36 major, 59 minor) and 11810 code smells.
clojure Quality
Best in #Java
Average in #Java
clojure Quality
Best in #Java
Average in #Java

securitySecurity

  • clojure has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • clojure code analysis shows 0 unresolved vulnerabilities.
  • There are 1 security hotspots that need review.
clojure Security
Best in #Java
Average in #Java
clojure Security
Best in #Java
Average in #Java

license License

  • clojure does not have a standard license declared.
  • Check the repository for any license declaration and review the terms closely.
  • Without a license, all rights are reserved, and you cannot use the library in your applications.
clojure License
Best in #Java
Average in #Java
clojure License
Best in #Java
Average in #Java

buildReuse

  • clojure releases are not available. You will need to build from source code and install.
  • Deployable package is available in Maven.
  • Build file is available. You can build the component from source.
  • clojure saves you 21331 person hours of effort in developing the same functionality from scratch.
  • It has 41897 lines of code, 4083 functions and 183 files.
  • It has medium code complexity. Code complexity directly impacts maintainability of the code.
clojure Reuse
Best in #Java
Average in #Java
clojure Reuse
Best in #Java
Average in #Java
Top functions reviewed by kandi - BETA

kandi has reviewed clojure and discovered the below as its top functions. This is intended to give you an instant insight into clojure implemented functionality, and help decide if they suit your requirements.

  • Reads the code instructions .
  • Internal function .
  • Invokes the method on the supplied arguments .
  • Puts this descriptor into the given byte vector .
  • Return the size of this class file .
  • Prints an object .
  • Merge the given basic block into this frame .
  • Expand a macro .
  • Puts the field information .
  • Creates an array with all unique key associated with the given array .

clojure Key Features

The Clojure programming language

Clojure: overriding one function in a library

copy iconCopydownload iconDownload
(defn wrap-take-last-param []
  (fn [handler]
    (fn [req]
      (handler 
        (update req :params 
                (fn [params]
                  (into {} 
                     (for [[k v] params]
                       [k (if (string? v) v, (last v)]))))))))
-----------------------
(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (with-redefs [clojure.core/range (constantly "Bogus!")]
    (is= "Bogus!" (range 9))))

ClojureScript: How to access (not just print) the result of an HTTP GET request

copy iconCopydownload iconDownload
(defn get-request [str1 str2]
  (go (<! (http/get "http://localhost:3000"
                    {:query-params {"str1" str1
                                    "str2" str2}}))))


(defn read-response [response-chan]
  (go (let [resp (<! response-chan)]
        (prn resp))))  ; <- the only place where you can "touch" resp!

(read-response (get-request "a" "b"))
-----------------------
(def result-ref (atom nil))

(defn into-ref [chan]
  (go (reset! result-ref (<! chan))))

(into-ref
  (http/get "http://localhost:3000"
    {:query-params
     {"str1" str1
      "str2" str2}}))

@result-ref

How to suppress output of exception while running unit tests in Clojure

copy iconCopydownload iconDownload
(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (is= 5 (+ 2 3))
  (throws? (/ 1 0)))
-----------------------------------
   Clojure 1.10.3    Java 15.0.2
-----------------------------------

Testing tst.demo.core

Ran 2 tests containing 2 assertions.
0 failures, 0 errors.
(is (thrown? ArithmeticException (/ 1 0))) 
(defmacro throws? 
  "Use (throws? ...) instead of (is (thrown? ...)) for clojure.test. Usage:
     (throws? (/ 1 0))                      ; catches any Throwable"
  [& forms]
  `(clojure.test/is
     (try
       ~@forms
       false ; fail if no exception thrown
       (catch Throwable dummy#
         true)))) ; if anything is thrown, test succeeds
-----------------------
(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (is= 5 (+ 2 3))
  (throws? (/ 1 0)))
-----------------------------------
   Clojure 1.10.3    Java 15.0.2
-----------------------------------

Testing tst.demo.core

Ran 2 tests containing 2 assertions.
0 failures, 0 errors.
(is (thrown? ArithmeticException (/ 1 0))) 
(defmacro throws? 
  "Use (throws? ...) instead of (is (thrown? ...)) for clojure.test. Usage:
     (throws? (/ 1 0))                      ; catches any Throwable"
  [& forms]
  `(clojure.test/is
     (try
       ~@forms
       false ; fail if no exception thrown
       (catch Throwable dummy#
         true)))) ; if anything is thrown, test succeeds
-----------------------
(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (is= 5 (+ 2 3))
  (throws? (/ 1 0)))
-----------------------------------
   Clojure 1.10.3    Java 15.0.2
-----------------------------------

Testing tst.demo.core

Ran 2 tests containing 2 assertions.
0 failures, 0 errors.
(is (thrown? ArithmeticException (/ 1 0))) 
(defmacro throws? 
  "Use (throws? ...) instead of (is (thrown? ...)) for clojure.test. Usage:
     (throws? (/ 1 0))                      ; catches any Throwable"
  [& forms]
  `(clojure.test/is
     (try
       ~@forms
       false ; fail if no exception thrown
       (catch Throwable dummy#
         true)))) ; if anything is thrown, test succeeds
-----------------------
(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (is= 5 (+ 2 3))
  (throws? (/ 1 0)))
-----------------------------------
   Clojure 1.10.3    Java 15.0.2
-----------------------------------

Testing tst.demo.core

Ran 2 tests containing 2 assertions.
0 failures, 0 errors.
(is (thrown? ArithmeticException (/ 1 0))) 
(defmacro throws? 
  "Use (throws? ...) instead of (is (thrown? ...)) for clojure.test. Usage:
     (throws? (/ 1 0))                      ; catches any Throwable"
  [& forms]
  `(clojure.test/is
     (try
       ~@forms
       false ; fail if no exception thrown
       (catch Throwable dummy#
         true)))) ; if anything is thrown, test succeeds

Replace the UUIDs with Strings

copy iconCopydownload iconDownload
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [tupelo.string :as str]
    [tupelo.uuid :as uuid]
    ))

(defn replace-uuids
  [input-str]
  ; use `let-spy-pretty` below to see step-by-step results
  (let [segments       (str/split input-str #"/")
        pairs          (partition-all 2 segments)
        ; looks like:
        ;    [["" "v1"]
        ;     ["user-1" "4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964"]
        ;     ["user-2" "7badb866-44fc-43c7-9cf4-aa3c2f8cc964"]
        ;     ["user-3" "27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"]]
        version-pair   (first pairs)
        user-pairs     (rest pairs)
        user-pairs-mod (for [pair user-pairs]
                         (let [user-part (first pair)
                               uuid-part (second pair)]
                           (if (uuid/uuid-str? uuid-part)
                             [user-part (str user-part "-id")]
                             pair)))
        all-pairs-mod  (flatten [version-pair user-pairs-mod])
        result         (str/join (interpose "/" all-pairs-mod))]
    result))

(dotest
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"))
  )
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [schema.core :as s]
    [tupelo.string :as str]
    ))

(def full-regex
  #"(?x)              # expanded form
  /                   # a slash
  ([a-zA-Z0-9-_]+)    # repeated identifier char in a capture group
  /                   # a slash
  \p{XDigit}{8}       # 8 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{12}      # 12 hex digits
  ")

(s/defn replace-uuids :- s/Str
  [s :- s/Str]
  (str/replace s full-regex
    (fn [arg]
      (let ; use `let-spy` to see intermediate steps
        [x1     (second arg)
         result (str "/" x1 "/" x1 "-id")]
        result))))
(dotest
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car/tire"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car/tire"))
  )
-----------------------
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [tupelo.string :as str]
    [tupelo.uuid :as uuid]
    ))

(defn replace-uuids
  [input-str]
  ; use `let-spy-pretty` below to see step-by-step results
  (let [segments       (str/split input-str #"/")
        pairs          (partition-all 2 segments)
        ; looks like:
        ;    [["" "v1"]
        ;     ["user-1" "4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964"]
        ;     ["user-2" "7badb866-44fc-43c7-9cf4-aa3c2f8cc964"]
        ;     ["user-3" "27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"]]
        version-pair   (first pairs)
        user-pairs     (rest pairs)
        user-pairs-mod (for [pair user-pairs]
                         (let [user-part (first pair)
                               uuid-part (second pair)]
                           (if (uuid/uuid-str? uuid-part)
                             [user-part (str user-part "-id")]
                             pair)))
        all-pairs-mod  (flatten [version-pair user-pairs-mod])
        result         (str/join (interpose "/" all-pairs-mod))]
    result))

(dotest
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"))
  )
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [schema.core :as s]
    [tupelo.string :as str]
    ))

(def full-regex
  #"(?x)              # expanded form
  /                   # a slash
  ([a-zA-Z0-9-_]+)    # repeated identifier char in a capture group
  /                   # a slash
  \p{XDigit}{8}       # 8 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{12}      # 12 hex digits
  ")

(s/defn replace-uuids :- s/Str
  [s :- s/Str]
  (str/replace s full-regex
    (fn [arg]
      (let ; use `let-spy` to see intermediate steps
        [x1     (second arg)
         result (str "/" x1 "/" x1 "-id")]
        result))))
(dotest
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car/tire"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car/tire"))
  )
-----------------------
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [tupelo.string :as str]
    [tupelo.uuid :as uuid]
    ))

(defn replace-uuids
  [input-str]
  ; use `let-spy-pretty` below to see step-by-step results
  (let [segments       (str/split input-str #"/")
        pairs          (partition-all 2 segments)
        ; looks like:
        ;    [["" "v1"]
        ;     ["user-1" "4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964"]
        ;     ["user-2" "7badb866-44fc-43c7-9cf4-aa3c2f8cc964"]
        ;     ["user-3" "27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"]]
        version-pair   (first pairs)
        user-pairs     (rest pairs)
        user-pairs-mod (for [pair user-pairs]
                         (let [user-part (first pair)
                               uuid-part (second pair)]
                           (if (uuid/uuid-str? uuid-part)
                             [user-part (str user-part "-id")]
                             pair)))
        all-pairs-mod  (flatten [version-pair user-pairs-mod])
        result         (str/join (interpose "/" all-pairs-mod))]
    result))

(dotest
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"))
  )
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [schema.core :as s]
    [tupelo.string :as str]
    ))

(def full-regex
  #"(?x)              # expanded form
  /                   # a slash
  ([a-zA-Z0-9-_]+)    # repeated identifier char in a capture group
  /                   # a slash
  \p{XDigit}{8}       # 8 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{4}       # 4 hex digits
  -                   # hyphen
  \p{XDigit}{12}      # 12 hex digits
  ")

(s/defn replace-uuids :- s/Str
  [s :- s/Str]
  (str/replace s full-regex
    (fn [arg]
      (let ; use `let-spy` to see intermediate steps
        [x1     (second arg)
         result (str "/" x1 "/" x1 "-id")]
        result))))
(dotest
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"))
  (is= "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car/tire"
    (replace-uuids "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car/tire"))
  )
-----------------------
(require '[clojure.spec.alpha :as spec]
         '[clojure.string :as cljstr])

(spec/def ::fixkey-format
  (spec/cat :root #{""}
            :version #{"v1"}
            :pref1 #{"user-1"}
            :user-1 string?
            :pref2 #{"user-2"}
            :user-2 string?
            :pref3 #{"user-3"}
            :user-3 string?))

(defn replace-values [input new-values]
  (cljstr/join
   "/"
   (spec/unform
    ::fixkey-format
    (merge (spec/conform ::fixkey-format (cljstr/split input #"/")) new-values))))

(replace-values "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"
                {:user-1 "x"
                 :user-2 "y"
                 :user-3 "z"})
;; => "/v1/user-1/x/user-2/y/user-3/z"
-----------------------
(clojure.string/replace
 "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"
 #"/(user-\d+)/[0-9a-f\-]+(?=/|$)"
 "/$1/$1-id")

;;=> "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"

(defn normalize-path [path]
  (clojure.string/replace path #"/(user-\d+)/[0-9a-f\-]+(?=/|$)" "/$1/$1-id"))

(map normalize-path
     ["/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car/tire"])

;;=> ("/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;    "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;    "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car/tire")
(defn normalize-path [path]
  (clojure.string/replace
   path
   #"/(user-\d+)/[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}(?=/|$)"
   "/$1/$1-id"))

(map normalize-path
     ["/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car/tire"
      "/v2/user-101/asd-asd-asd/user-2/xxx"
      "/v3/user-404/aa44ddss-3333-4444-aaaa/user-505/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964"])

;; ("/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;  "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;  "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car/tire"
;;  "/v2/user-101/asd-asd-asd/user-2/xxx"
;;  "/v3/user-404/aa44ddss-3333-4444-aaaa/user-505/user-505-id")
-----------------------
(clojure.string/replace
 "/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964"
 #"/(user-\d+)/[0-9a-f\-]+(?=/|$)"
 "/$1/$1-id")

;;=> "/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id"

(defn normalize-path [path]
  (clojure.string/replace path #"/(user-\d+)/[0-9a-f\-]+(?=/|$)" "/$1/$1-id"))

(map normalize-path
     ["/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car/tire"])

;;=> ("/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;    "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;    "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car/tire")
(defn normalize-path [path]
  (clojure.string/replace
   path
   #"/(user-\d+)/[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}(?=/|$)"
   "/$1/$1-id"))

(map normalize-path
     ["/v1/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car"
      "/user-1/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964/user-2/7badb866-44fc-43c7-9cf4-aa3c2f8cc964/user-3/27ebd241-44fc-43c7-9cf4-aa3c2f8cc964/car/tire"
      "/v2/user-101/asd-asd-asd/user-2/xxx"
      "/v3/user-404/aa44ddss-3333-4444-aaaa/user-505/4bcbe877-44fc-43c7-9cf4-aa3c2f8cc964"])

;; ("/v1/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;  "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car"
;;  "/user-1/user-1-id/user-2/user-2-id/user-3/user-3-id/car/tire"
;;  "/v2/user-101/asd-asd-asd/user-2/xxx"
;;  "/v3/user-404/aa44ddss-3333-4444-aaaa/user-505/user-505-id")

How to generate Hiccup structures conditionally?

copy iconCopydownload iconDownload
(defn render-location-details
  [cur-location]
  (let [
        desc (get cur-location :Desc)
        address (get cur-location :Address)
        comment (get cur-location :Comment)
        address-title [:h4 "Address"]
        address-body [:p address]
        comment-hiccup (if (clojure.string/blank? comment)
                          nil
                          [:div
                           [:h4 "Comment"]
                           [:p comment]
                           ])

        ]
    [:div
      [:h3 desc]
      address-title
      address-body
      comment-hiccup
     ]
    )
  )

-----------------------
[nil]
[:h3 "hello"]
[:div nil]
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [hiccup.core :as h]))

(dotest
  (is= (h/html [:p "bar"]) "<p>bar</p>")
  (is= (h/html [:p nil]) "<p></p>")
  (is= (h/html nil) "")

  ; if uncommented, this creates an error during macroexpansion of `h/html`
  ; (println :err1 (h/html [nil]))  ; <=== line 12
  ;    `Syntax error macroexpanding h/html at (tst/demo/core.clj:12:18). 
  ;       java.lang.IllegalArgumentException:  is not a valid element name.`

  )
(defn render-location-details
  [cur-location]
  [:div
   [:h3 (:Desc cur-location)]
   [:h4 "Address"]
   [:p (:Address cur-location)]
   (when-not (clojure.string/blank? (:Comment cur-location))
     [:div
      [:h4 "Comment"]
      [:p (:Comment cur-location)]])])
-----------------------
[nil]
[:h3 "hello"]
[:div nil]
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [hiccup.core :as h]))

(dotest
  (is= (h/html [:p "bar"]) "<p>bar</p>")
  (is= (h/html [:p nil]) "<p></p>")
  (is= (h/html nil) "")

  ; if uncommented, this creates an error during macroexpansion of `h/html`
  ; (println :err1 (h/html [nil]))  ; <=== line 12
  ;    `Syntax error macroexpanding h/html at (tst/demo/core.clj:12:18). 
  ;       java.lang.IllegalArgumentException:  is not a valid element name.`

  )
(defn render-location-details
  [cur-location]
  [:div
   [:h3 (:Desc cur-location)]
   [:h4 "Address"]
   [:p (:Address cur-location)]
   (when-not (clojure.string/blank? (:Comment cur-location))
     [:div
      [:h4 "Comment"]
      [:p (:Comment cur-location)]])])
-----------------------
[nil]
[:h3 "hello"]
[:div nil]
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [hiccup.core :as h]))

(dotest
  (is= (h/html [:p "bar"]) "<p>bar</p>")
  (is= (h/html [:p nil]) "<p></p>")
  (is= (h/html nil) "")

  ; if uncommented, this creates an error during macroexpansion of `h/html`
  ; (println :err1 (h/html [nil]))  ; <=== line 12
  ;    `Syntax error macroexpanding h/html at (tst/demo/core.clj:12:18). 
  ;       java.lang.IllegalArgumentException:  is not a valid element name.`

  )
(defn render-location-details
  [cur-location]
  [:div
   [:h3 (:Desc cur-location)]
   [:h4 "Address"]
   [:p (:Address cur-location)]
   (when-not (clojure.string/blank? (:Comment cur-location))
     [:div
      [:h4 "Comment"]
      [:p (:Comment cur-location)]])])
-----------------------
[nil]
[:h3 "hello"]
[:div nil]
(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [hiccup.core :as h]))

(dotest
  (is= (h/html [:p "bar"]) "<p>bar</p>")
  (is= (h/html [:p nil]) "<p></p>")
  (is= (h/html nil) "")

  ; if uncommented, this creates an error during macroexpansion of `h/html`
  ; (println :err1 (h/html [nil]))  ; <=== line 12
  ;    `Syntax error macroexpanding h/html at (tst/demo/core.clj:12:18). 
  ;       java.lang.IllegalArgumentException:  is not a valid element name.`

  )
(defn render-location-details
  [cur-location]
  [:div
   [:h3 (:Desc cur-location)]
   [:h4 "Address"]
   [:p (:Address cur-location)]
   (when-not (clojure.string/blank? (:Comment cur-location))
     [:div
      [:h4 "Comment"]
      [:p (:Comment cur-location)]])])

How do I get to the Clojure metadata when the var is in a vector or a list?

copy iconCopydownload iconDownload
(def V [#'A #'B])
(meta (ns-resolve *ns* (first V)))
-----------------------
(def V [#'A #'B])
(meta (ns-resolve *ns* (first V)))

clojure, how to reduce repeated code (potentially using macro)

copy iconCopydownload iconDownload
(def job-inventory
  [{:name "add" :task (fn [] (println (+ 1 1))) :cron "0/5 * * ? * *"}
   {:name "multiply" :task (fn [] (println (* 4 5)))  :cron "0/3 * * ? * *"}])
;; require clojurewerkz.quartzite.conversion :as qc
(defjob proxy-job [ctx]
  (let [ctx (qc/from-job-data ctx)]
    ((ctx "proxied-fn"))))
(defn schedule [scheduler {:keys [:name :task :cron]}]
  (let [job (j/build
             (j/of-type proxy-job)
             (j/using-job-data {"proxied-fn" task})
             (j/with-identity (j/key (str "job." name))))
        trigger (t/build
                 (t/with-identity (t/key (str "trigger." name)))
                 (t/start-now)
                 (t/with-schedule (cron/schedule
                                   (cron/cron-schedule "0/3 * * ? * *"))))]
  (qs/schedule scheduler job trigger)
  scheduler)

(reduce scheduler schedule job-inventory)
-----------------------
(def job-inventory
  [{:name "add" :task (fn [] (println (+ 1 1))) :cron "0/5 * * ? * *"}
   {:name "multiply" :task (fn [] (println (* 4 5)))  :cron "0/3 * * ? * *"}])
;; require clojurewerkz.quartzite.conversion :as qc
(defjob proxy-job [ctx]
  (let [ctx (qc/from-job-data ctx)]
    ((ctx "proxied-fn"))))
(defn schedule [scheduler {:keys [:name :task :cron]}]
  (let [job (j/build
             (j/of-type proxy-job)
             (j/using-job-data {"proxied-fn" task})
             (j/with-identity (j/key (str "job." name))))
        trigger (t/build
                 (t/with-identity (t/key (str "trigger." name)))
                 (t/start-now)
                 (t/with-schedule (cron/schedule
                                   (cron/cron-schedule "0/3 * * ? * *"))))]
  (qs/schedule scheduler job trigger)
  scheduler)

(reduce scheduler schedule job-inventory)
-----------------------
(def job-inventory
  [{:name "add" :task (fn [] (println (+ 1 1))) :cron "0/5 * * ? * *"}
   {:name "multiply" :task (fn [] (println (* 4 5)))  :cron "0/3 * * ? * *"}])
;; require clojurewerkz.quartzite.conversion :as qc
(defjob proxy-job [ctx]
  (let [ctx (qc/from-job-data ctx)]
    ((ctx "proxied-fn"))))
(defn schedule [scheduler {:keys [:name :task :cron]}]
  (let [job (j/build
             (j/of-type proxy-job)
             (j/using-job-data {"proxied-fn" task})
             (j/with-identity (j/key (str "job." name))))
        trigger (t/build
                 (t/with-identity (t/key (str "trigger." name)))
                 (t/start-now)
                 (t/with-schedule (cron/schedule
                                   (cron/cron-schedule "0/3 * * ? * *"))))]
  (qs/schedule scheduler job trigger)
  scheduler)

(reduce scheduler schedule job-inventory)

How to create a Clojure function that returns a Hiccup structure?

copy iconCopydownload iconDownload
(let [locations ["one" "two" "three"]
      title-html [[:h2 "Locations"]]
      location-html (map (fn [location] [:h3 location]) locations)]
 (html (concat title-html location-html)))

"<h2>Locations</h2><h3>one</h3><h3>two</h3><h3>three</h3>"

Change the value of a Clojure var without touching the metadata

copy iconCopydownload iconDownload
-------------------------
clojure.core/alter-var-root
([v f & args])
  Atomically alters the root binding of var v by applying f to its
  current value plus any args
; Clojure 1.10.3
(def ^{:Metadata "metaA"} A 1)
; #'user/A
(def ^{:Metadata "metaB"} B 2)
; #'user/B
A
; 1
B
; 2
(meta #'A)
; {:Metadata "metaA", :line 1, :column 1, :file "NO_SOURCE_PATH", :name A, :ns #object[clojure.lang.Namespace 0x2189e7a7 "user"]}
(meta #'B)
; {:Metadata "metaB", :line 1, :column 1, :file "NO_SOURCE_PATH", :name B, :ns #object[clojure.lang.Namespace 0x2189e7a7 "user"]}
(alter-var-root #'B (constantly A))
; 1
B
; 1
(meta #'B)
; {:Metadata "metaB", :line 1, :column 1, :file "NO_SOURCE_PATH", :name B, :ns #object[clojure.lang.Namespace 0x2189e7a7 "user"]}
-----------------------
-------------------------
clojure.core/alter-var-root
([v f & args])
  Atomically alters the root binding of var v by applying f to its
  current value plus any args
; Clojure 1.10.3
(def ^{:Metadata "metaA"} A 1)
; #'user/A
(def ^{:Metadata "metaB"} B 2)
; #'user/B
A
; 1
B
; 2
(meta #'A)
; {:Metadata "metaA", :line 1, :column 1, :file "NO_SOURCE_PATH", :name A, :ns #object[clojure.lang.Namespace 0x2189e7a7 "user"]}
(meta #'B)
; {:Metadata "metaB", :line 1, :column 1, :file "NO_SOURCE_PATH", :name B, :ns #object[clojure.lang.Namespace 0x2189e7a7 "user"]}
(alter-var-root #'B (constantly A))
; 1
B
; 1
(meta #'B)
; {:Metadata "metaB", :line 1, :column 1, :file "NO_SOURCE_PATH", :name B, :ns #object[clojure.lang.Namespace 0x2189e7a7 "user"]}

Can't use async functions in clojurescript

copy iconCopydownload iconDownload
(ns test.foo
  (:require
    [clojure.core.async :as async] 
    [cljs.core.async.interop :refer (<p!)]
    ["systeminformation" :as systeminformation]))


(async/go
  (prn (<p! (systeminformation/cpu))))
(-> (systeminformation/cpu)
    (.then prn))
-----------------------
(ns test.foo
  (:require
    [clojure.core.async :as async] 
    [cljs.core.async.interop :refer (<p!)]
    ["systeminformation" :as systeminformation]))


(async/go
  (prn (<p! (systeminformation/cpu))))
(-> (systeminformation/cpu)
    (.then prn))

Community Discussions

Trending Discussions on clojure
  • Clojure: overriding one function in a library
  • ClojureScript: How to access (not just print) the result of an HTTP GET request
  • How to suppress output of exception while running unit tests in Clojure
  • Replace the UUIDs with Strings
  • How to generate Hiccup structures conditionally?
  • How to set and get a vector in local storage?
  • How do I get to the Clojure metadata when the var is in a vector or a list?
  • clojure, how to reduce repeated code (potentially using macro)
  • How to create a Clojure function that returns a Hiccup structure?
  • Change the value of a Clojure var without touching the metadata
Trending Discussions on clojure

QUESTION

Clojure: overriding one function in a library

Asked 2021-Jun-09 at 22:12

This question is off the back of a previous question I asked here a few days ago. One of the comments was that I should dispense with the Ring middleware for extracting query parameters and write my own. One alternative that I thought I'd play with was harnessing the existing one to get what I want and I've been doing some digging into the Ring source code. It does almost exactly what I want. If I write out how I understand it works:

  1. A middleware has the function wrap-params which calls params-request
  2. params-request adds a params map to the request map, calls assoc-query-params
  3. assoc-query-params eventually calls ring.util.codec/form-decode on the incoming query string to turn it into a map
  4. form-decode uses assoc-conj to merge values into an existing map via reduce
  5. assoc-conj's docstring says

Associate a key with a value in a map. If the key already exists in the map, a vector of values is associated with the key.

This last function is the one that is problematic in my previous question (TL;DR: I want the map's values to be consistent in class of either a string or a vector). With my object orientated hat on I would have easily solved this by subclassing and overriding the method that I need the behaviour changed. However for Clojure I cannot see how to just replace the one function without having to alter everything up the stack. Is this possible and is it easy, or should I be doing this another way? If it comes to it I could copy the entire middleware library and the codec one, but it seems a bit heavyweight to me.

ANSWER

Answered 2021-Jun-09 at 09:22

I disagree with the advice to not use Ring's param middleware. It gives you perfect information about the incoming parameters, so you if you don't like the default behavior of string-or-list, you can change the parameters however you want.

There are numerous ways to do this, but one obvious approach would be to write your own middleware, and insert it in between Ring's param middleware and your handlers.

(defn wrap-take-last-param []
  (fn [handler]
    (fn [req]
      (handler 
        (update req :params 
                (fn [params]
                  (into {} 
                     (for [[k v] params]
                       [k (if (string? v) v, (last v)]))))))))

You could write something fancier, like adding some arguments to the function to let you specify which parameters you want to receive only the last specified, and which you would like to always receive as a list. In that case you might not want to wrap it around your entire handler, but around each of your routes separately to specify their expected parameters.

Source https://stackoverflow.com/questions/67899108

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install clojure

You can download it from GitHub, Maven.
You can use clojure like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the clojure component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

Support

For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Share this Page

share link
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.