Fork me on GitHub

If I am writing a cljs library to wrap a js component, should the library pull in the js component, or should I depend on the user to provide it?


I am worried that there might be performance implications to bundling it in my lib, since the user may want to pull in the dependency via cdn

Kenneth Cheung02:07:03

Hi all, Looking for feedback, which version of adjacencies? is more idomatic? I'm particularly wondering if it is acceptable to use a kind of state variable in my reduce ( prev ). Or is there a better way?

(defn adjacencies?
  "Returns true if there are consecutive characters that are the same in the
  given string"
  (loop [[x1 & x-rem :as xs] (seq s)
         prev                nil]
      (not xs)    false
      (= x1 prev) true
      :else       (recur x-rem x1))))

(defn adjacencies?
  "Returns true if there are consecutive characters that are the same in the
  given string"
    (reduce (fn [[result prev] x]
              (if (= x prev)
                (reduced [true x])
                [result x]))
            [false nil]

(adjacencies? "abcd") ;;=> false
(adjacencies? "abcdd") ;;=> true
(adjacencies? "") ;; => false


I would probably do something like:

(defn adjacencies? [s]
  (some identity (map = s (rest s))))


I think that's nicer than my partition version. I'd probably use (some true? ...) tho' to be a bit clearer about what it's looking for?

👍 3

one note regarding loop is that the destructuring in the bindings will cause extra seq calls so I usually use the following pattern to avoid the extra seq calls:

(defn adjacencies?
  "Returns true if there are consecutive characters that are the same in the
  given string"
  (loop [xs (seq s)
         prev nil]
    (if xs
      (let [x (first xs)]
        (if (= x prev)
          (recur (next xs) x)))


Ah, yours also returns nil for the "false" case so it isn't strictly a predicate, just like mine wasn't.

Kenneth Cheung03:07:04

@U7RJTCH6J so nice (some identity (map = s (rest s))) and ty for the loop pattern. New info to me!

👍 3
Kenneth Cheung05:07:54

@U7RJTCH6J wanted to clarify the loop binding comment Will moving the seq call outside of the loop remove those extra calls. Or did you mean under the hood, destructuring in a loop bind always calls seq

(when-let [sec (seq s)]
    (loop [[x1 & x-rem :as xs] sec
           prev                nil]
        (not xs)    false
        (= x1 prev) true
        :else       (recur x-rem x1))))


the second > destructuring in a loop bind always calls `seq`


it's not that big a deal. it will just call seq a few extra times

Kenneth Cheung04:07:56

! TY I think this is important, especially when I need to calculate big O.


I'm not sure which of those I would consider more idiomatic. For comparison

(defn adjacencies? [s] (some #(apply = %) (partition 2 1 s)))

Kenneth Cheung04:07:18

this is good too!


Technically that produces nil for the "false" case so it shouldn't have ? (unless it wrapped the some call in boolean to produce just true or false)

🙌 3
Alex Miller (Clojure team)02:07:29

you could also dedupe and see if the result is the same

Alex Miller (Clojure team)02:07:05

(defn adjacencies? [s] (not= (seq s) (seq (dedupe s))))

😮 3
🙌 3
Kenneth Cheung04:07:54

ty! I think this one i think uses the best existing abstraction. building on this for time performance I could use count `(apply not= (map count [s (dedupe s)]))` retracting this. count on a string and the sequence by dedupe is not constant

Alex Miller (Clojure team)12:07:11

One downside of this is it doesn’t early exit

Kevin Mungai07:07:09

Hi, hope everyone is safe. What is the purpose of the comment macro? I see that it ignores the body, but I don't understand why that would be useful.


People tend to use the REPL for development. And instead of typing directly in the REPL, one has their editor connected to the REPL, and using keyboard, shortcuts, you run pieces of code. Let's say you have defined a function f, and you want to try (f 42). You'd type it in the file you happened to be in and try it. This call may have side effects (like printing, HTTP calls etc.), and will happen when the file is loaded. So, you can put this REPL exploration in a comment form. You can even check-in this code. These are also called "Rich Comment forms". They're present in the Clojure standard lib as well. Example:

Kevin Mungai07:07:57

Thanks for the explanation. I have been commenting out the forms using ;; instead. Maybe I will try using comment for some of these instead.


comment is different from ;; in that it returns nil compared to ;; where everything on a line after ;; is ignored


you might also find the discard form #_ to be of interest too: <|>

Kevin Mungai08:07:14

So when is the most appropriate place to use comment?


personally i use it for the "rich comment form" situation - perhaps you might find this article relevant to get an idea of what some others think:

👍 3
Kevin Mungai08:07:16

Thanks so much, I see now that comment is neat way to provide a working example or thought process in code that isn't actually part of the code.

👍 6

He everyone. I do not understand what :bundle does in the latest version of ClojureScript. Anyone care to explain?


Hi everyone. It seems like clojurescript after version "1.10.597" and figwheel-sidecar "0.5.19" does not play nicely together. I upgraded my clojurescript version today and the cljs repl would never connect. It was resolved by upgrading figwheel-sidecar to 0.5.20, Anyone else encounter this issue?



(def qwq {:a {:a1 {:a11 11 :a12 12} :a2 {:a21 21 :a22 22}}})
Is there is simpler way to destructure qwq other than
((fn [{{{:keys [a11]} :a1} :a {{:keys [a21]} :a2} :a}]
   (str a11 " <=> " a21)) qwq)


Why not just use

(let [a11 (get-in qwq [:a :a1 :a11])
      a21 (get-in qwq [:a :a2 :a21])]
  (str a11 " <=> " a21))


I will mention here because your question took me far away rsrs .. I got thinking... "what if I wanted to support a custom rule for destructuring? How would I do this?" ... nice discussion here

🧠 3

@U017H3L9EHF In this case, we know nothing about the structure of or rather access to qwq (or might be unnecessary hack) but we are certain it is specd with a, a1 & a2 . We provided for a11, a21 ... so we could access it within fn to do something else...


@simon I can't think other way to destructure now, but if you have such nested data structures and need to operate on them, maybe you should consider using specter library

(ns examples.core
  (:require [com.rpl.specter :as specter]))

(def qwq {:a {:a1 {:a11 11 :a12 12} :a2 {:a21 21 :a22 22}}})

(specter/select-one [:a :a1 :a11] qwq)
;; => 11
(specter/select-one [:a :a2 :a21] qwq)
;; => 21


I will mention here because your question took me far away rsrs .. I got thinking... "what if I wanted to support a custom rule for destructuring? How would I do this?" ... nice discussion here

🧠 3

How do you mute logging from java libraries? I'm currently using Tika, wrapped by the Pantomime library, and the logging output is ultra-spammy, with many debug messages etc.. the messages look like this, and take 99% of the available CPU time. 19:56:27.335 [clojure-agent-send-off-pool-6] DEBUG o.a.f.util.autodetect.FontFileFinder - checkFontfile check .... 20:33:46.076 [Finalizer thread] DEBUG .ScratchFileBuffer - ScratchFileBuffer not closed! (the calls are started in a pmap) I've tried wrapping the parsing function that calls the library with with-out-str and another macro that does the same, but only on *err* . Neither gets any strings. I've been searching the web for any solutions, but I've yet to find one that works. Also, if I add

:jvm-opts ["-Dlog4j.debug=true"]
to my project.clj I do not get any information when starting the repl. So I guess log4j is not the culprit.. Any tips on where to continue my search for a solution?


Depends on the version of log4j, whether you also have slf4j, logback etc, but this example config file provided by clj-http might be a good starting point:


It tweaks things towards the debugging end, but setting the log level to error or warn might be just enough


found a nuclear solution for this 😄 . had zero effect for me, but in project.clj I added "-Dlogback.configurationFile=./resources/logcfg.xml" to :jvm-opts . Then in ./resources/logcfg.xml

    <root level="OFF">
Which is the horrible nuke. But it is quiet now. 🔕 So I am happy. Maybe I can experiment with more granular config for this at some later point in time.

Eric Ihli19:07:48

What's the best way to turn an alias of a namespace into a keyword? Right now I have this (with model coming from a (:require [foo.model :as model])

(keyword (ns-name ('model (ns-aliases *ns*))))


@ericihli Can you explain what problem you're trying to solve? Hard to tell whether there's a better way without the problem statement.


(and in particular, is the alias name fixed in this case or is it a variable you're getting from somewhere?)

Audrey Mae Moncada19:07:07

Thinking of a project, what project should I do?


@U017JAW40BB That's really far too open-ended a question for anyone to really answer. What sort of problems are you interested in? What sort of projects have you already done in Clojure? In other languages?

Eric Ihli19:07:23

I want to use the namespace as a key in a thing that expects keywords, not symbols or namespaces.


And you're getting the alias name from...?

Eric Ihli19:07:03

A require in the ns macro.


No, I mean in the code where you're using it. Is this just some hardcoded alias you're picking on?


I'm asking because ::model/x will auto-expand to :foo.model/x regardless of whether x exists.

Eric Ihli20:07:38

Ah yes to resolve this confusion, my goal is to to expand ::model/x without needing the x simply because the :foo.model keyword most accurately and succinctly describes what I want. Since the keyword represents a database table, or model type, it looked ugly to have :foo.model/model or :foo.model/type or :foo.model/table-name . I just wanted :foo.model but I wanted it to be expanded from an alias because the actual name is really long to have typed out in code.


(keyword (namespace ::model/x))


Or whatever dummy x you want.


(and of course if you are referring to a known column, just ::model/my_column would do)

Eric Ihli19:07:07

Ah yes. My thought was that this would be more convenient for refactoring and code navigation rather than having both the require + alias as well as hardcoded namespaced keywords that refer to the same thing.

Eric Ihli19:07:34

Trying to ensure the keywords always align with my directory structure + namespaces.


Could you explain? Are you trying to build a code refactoring tool?


(i.e., what problem are you trying to solve)

Eric Ihli19:07:17

No. This is part of some application-level schema/validation for a NoSQL database.

Eric Ihli19:07:35

I'm treating the namespaces as "tables".


OK, I'm still not really following you...


Qualified keywords do not have to correspond to actual namespaces.


:some-table/some-column is fine without (ns some-table ..) existing anywhere in your code.

Eric Ihli19:07:10

Sorry about that. I'm trying to balance the amount of detail with responding quickly. The root of what I'm trying to do can probably be done a hundred different ways and needing to turn namespaces into keywords is completely unnecessary. It occurred as an idea, then I got curious. It's not really necessary for anything in particular. The full chain of events that led to this: I'm thinking of having a my.models.user and my.models.accounts namespaces. Each will have a set of specs for validation. The documents I'm sending to the database will need to pass a spec. The documents will look like {:my.models.core/model :my.models.accounts, :my.models.accounts/id 1, :my.models.accounts/address "foo",,,} And then the specs would be something like:

(ns my.specs
  (:require [my.models.accounts :as accounts]))

(s/def ::account (s/and (s/keys :req [:my.models.core/model ,,,])
                        (fn [{:my.models.core/keys [model]}]
                         (= model (alias-to-kw accounts))))      ;; vs (= model :my.models.accounts)
I could use namespaced keywords to represent the table rather than turning the namespace into a keyword. {:my.models.core/model :my.models.accounts} could be {my.models.core/model :my.models.accounts/model} and then I could just use the namespaced keyword resolving :: with (= model ::accounts/model) . Or a number of other things I guess. The question was mostly curiosity.


you can use the specs from the my.models.accounts directly to validate the document:

(ns my.specs
  (:require [my.models.accounts :as accounts]))

(s/valid ::accounts/model your-data)
You do not need to 're-specify' the spec in another namespace. As far as I understand seems you are doing this.

🙏 3

Ow, I see you edited and included this option as well .. o/


Can we store and retrieve edn in postgresql ? Maybe converting it to json and then back to edn on retrieval, with a fn or by extending the db driver ?


you can but you will lose query leverage


EDN is also not the fastest ser/deser format