Fork me on GitHub
#beginners
<
2022-01-28
>
quan xing06:01:05

I saw the compojure wiki In Middleware chapter.

(defn wrap-current-user [handler database]
  (fn [request]
    (let [user-id (-> request :session :user-id)
          user    (get-user-by-id database user-id]
      (handler (assoc request :user user)))))
To solve this problem, we can use wrap-routes:
(-> (context "/user" {:keys [user]}
      (GET  "/current" ...)
      (POST "/current" ...))
    (wrap-routes wrap-current-user))
the wrap-current-user has two parameters that handler and database. the wrap-current-user function is called by wrap-routes, the 'database' argument is how to pass to the wrap-current-user

Arnav G12:01:06

What's the different between `::keyword` and `:keyword`?

(re-frame/reg-event-db
 ::initialize-db
 (fn [_ _]
   db/default-db))
vs
(re-frame/reg-event-db
 :initialize-db
 (fn [_ _]
   db/default-db))

agile_geek14:01:17

I'd try it like Alex suggests but :keyword is an un-namespace keyword whereas ::keyword is shorthand for :current-namespace/keyword

agile_geek14:01:11

P.S. the `::` shorthand is useful for referencing keywords that are namespaced in a namespace you require into your current namespace. For example,

(ns my-current-ns
  (require [my.longer.nested.other-ns :as other]))

(def foo [m x]
  (assoc m ::other/key-from-other x))

agile_geek14:01:23

In the example above ::other/key-from-other expands to :my.longer.nested.other-ns/key-from-other

Arnav G14:01:24

Got it! Thank you 🙂

maverick14:01:41

I am trying to add space before capital letters in string like this "HelloWorld, ThankYou" in clojurescript by using clojure.string/replace "HelloWorld, ThankYou" #"(\p{Ll})(\p{Lu})" "$1 $2" but it is not working.

manutter5114:01:22

I don’t usually use POSIX character classes, but from looking at the docs (https://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html), I get the impression you have to spell them out, e.g. \p{Lower} etc.

maverick14:01:21

Thanks, the lower one worked, and what to do if I want to reverse it like remove space?

manutter5115:01:02

I would just use either a literal space or the \s whitespace wildcard.

manutter5115:01:50

(let [s "This line   has  too        many      spaces."]
  (clojure.string/replace s #" +" " "))
=> "This line has too many spaces."

manutter5115:01:43

or

(let [s "This line   has  too        many      spaces."]
  (clojure.string/replace s #"\s+" " "))
=> "This line has too many spaces."

maverick15:01:48

Sorry, not space if I have an underscore between, and I need to replace it with space "TT_Hello_World, CC_Thank_You" but only need to replace combination of [a-z][_][A-Z]

manutter5115:01:28

Sure, just use literal underscore.

(let [line "TT_Hello_World, CC_Thank_You"]
  (clojure.string/replace line #"([a-z])_([A-Z])" "$1 $2"))
=> "TT_Hello World, CC_Thank You"

maverick15:01:06

Thanks, It worked

👍 1
manutter5114:01:39

(clojure.string/replace "HelloWorld, ThankYou" 
                        #"(\p{Lower})(\p{Upper})" "$1 $2")
=> "Hello World, Thank You"

manutter5114:01:23

Personally I’d just use #"([a-z])([A-Z])"

David Pham17:01:42

May I ask if tools.build will be published on maven/Clojure at some points?? Because of corporate policy we can’t pull deps from GitHub/gitlab?

seancorfield17:01:04

Does your company not allow open-source software to be used?

David Pham20:01:15

I can’t pull any repository from GitHub.

David Pham20:01:14

It is a bit incoherent since I could pull any docker images from docker hub and some others. So I could probably pull an image where Clojure Tools is included and search for the jar xD

seancorfield20:01:28

Such a dumb policy 😞

🎉 1
David Pham21:01:54

Better be safe than sorry, I work in a old industry with life threatening consequences on data leaks.

👀 1
Alex Miller (Clojure team)18:01:11

You could build it yourself too :)

David Pham18:01:34

It would bring me outside of my comfort zone :)

David Pham18:01:12

I guess I have been spoiled by tools.deps and everyone's work for too long :)

Alex Miller (Clojure team)18:01:47

It's just jarring the source dir :)

Alex Miller (Clojure team)18:01:35

I guess you'd need a pom too, but it's a few lines of tools.build :)

Alex Miller (Clojure team)18:01:20

(I know, I know, you don't have tools.build)

🙃 1
David Pham18:01:50

I guess depstar would also work xD.

David Pham18:01:31

Thanks a lot for all your work, if you make a Clojure Survey this year, you could ask if people want to have a official tools to be published in Clojure/maven if this is okay to have everything on GitHub xD (my corporate me would tell it makes things easier to have code in Clojure xD).

Alex Miller (Clojure team)18:01:37

I will probably get around to doing it, have just been waiting to see if demand

ghadi18:01:18

FYI @neo2551 depstar is deprecated

seancorfield18:01:23

☝️:skin-tone-2: Yeah, I'd be surprised if a company that doesn't allow you to download software from GitHub would allow you to use a deprecated and archived open source project?

dpsutton18:01:15

does the jar include the repo information? If not there’s no way to catch that with an automated policy right?

rmxm22:01:39

Hey all, Is there a nicer pattern for

(if true (do-some-update x) x)
I guess cond->? (cond-> x true (do-some update))

dpsutton22:01:50

(cond-> x (condition) (do-some-udpate) is a pattern i use often

rmxm22:01:02

yeah 🙂

dpsutton22:01:08

but sometimes there’s nothing wrong with (if (condition) (f x) x)