Fork me on GitHub
Dustin Paluch00:03:21

cljs.user> (.endsWith "hello" "o")
cljs.user> (.length "hello")

Execution error (TypeError) at (<cljs repl>:1).
"hello".length is not a function
Why does one work but not the other?


In javascript, .length is a property of an object, and not a function.

➜  ~ node
Welcome to Node.js v14.17.4.
Type ".help" for more information.
> "hello".endsWith("o")
> "hello".length
> "hello".length()
Uncaught TypeError: "hello".length is not a function

πŸ‘ 1

(.-length "hello")

πŸ™Œ 1
gratitude 1
Dustin Paluch02:03:00

Yeah, I was looking at the Java docs for String. Thanks!


tuple expects a vector, but the function parameter list isn't a vector:

(defn f
  [& args]
  (class args))
;;=> #'user/f

(f 1 2 3)
;;=> clojure.lang.ArraySeq
Also, you wouldn't be able to use tuple spec things like varargs. You probably want s/cat: (s/fdef hi :args (s/cat :number int?)).

πŸ™ 1
Nom Nom Mousse13:03:08

What is wrong with my specs? My minimal example fails


Is there a clj-file-path->edn?


Do you mean like (edn/read-string (slurp "/path/to/file.edn"))?


yup. I tried that and it returns only the first expression. how would you read the rest?


I don't think EDN supports multiple top-level values. If you have that, it should probably be wrapped in a top-level vector.


Or, if you want to hack it: (edn/read-string (str "[" (slurp "path/to/file.edn") "]"))


But you should probably fix the EDN


ha, that's a clever hack.:)


I need that utility in order to read a .clj file, so it won't have a top-level collection


I'm thinking perhaps a file can be read expression-after-expression via


Ah. If you're reading Clojure source, is the tool for the job


Thanks!:) I stumbled upon this piece of code for reading a Clojure program via read:

(with-open [in ( ( "foo.txt"))]
  (let [edn-seq (repeatedly (partial edn/read {:eof :theend} in))]
    (dorun (map println (take-while (partial not= :theend) edn-seq)))))
Looks good


(require '[ :as io]
         '[ :as edn]
         '[ :refer [push-back-reader]])

(with-open [rdr (-> "/path/to/edn/file"
  (loop [acc []]
    (let [exp (edn/read {:eof ::eof} rdr)]
      (if (= ::eof exp)
        (recur (conj acc exp))))))

πŸ‘ 1
❀️ 1

Can a Clojure program be read as edn? edn is a subset of Clojure data, as said so the rest of Clojure data set should fail to be captured as edn, but I can't think of what that rest may be.. I.e., what is clojure.set/difference(Clojure data, edn)?


@brownmoose3q not comprehensive but: regexes deref @ unquote splicing ` anonymous functions vars #? conditional forms

βœ… 1

Ahh, I see. Many of these exist at macro-time and result in some edn or run-time values. E.g., @ => (deref) E.g., #() =>"#function[clojure-multisets-sandbox.main/eval5976/fn--5977]"


Interestingly, reader conditionals can be preserved while reading via read-string

(read-string {:read-cond :preserve}
             "#?@(:clj [3 4] :cljs [5 6])")
=> #?@(:clj [3 4] :cljs [5 6])
as shown, at the bottom


I've found most of the reader macros cause issues. I first encountered this with regex literals.


@U051N6TTC same here when using edn readers, and then I stumbled upon and it seems to read all kinds of stuff just fine

(require '[ :as io]
         '[ :as edn]
         '[ :refer [push-back-reader]])
(require '[ :as r])

(with-open [rdr (-> "src/clj/clojure_multisets_sandbox/main.clj"
  (let [result (loop [acc []]
                 (let [exp (r/read {:eof ::eof} rdr)]
                   (println (pr-str exp))
                   (if (= ::eof exp)
                     (recur (conj acc exp)))))]
    (spit "derived/main.clj" result)))
I did just a small tweak to;cid=C053AK3F9 @U04V4KLKC kindly provided for reading edn and now it seems to munch Clojure programs! My tests were not exhaustive though, but it reads ns with that code no prob. (the tweak is edn/read => r/read)


namespaced maps/kws


(clojure.edn/read-string (pr-str #:ns{:key :val}))
=> #:ns{:key :val}

(clojure.edn/read-string (pr-str :ns/key))
=> :ns/key
hmm, seems edn preserve ns in maps and kws


some of this is accidental/undocumented


I see they use :my/fred as an example of keyword in edn doc,, perhaps ns is supported for keywords. :: is not allowed, however.


no mention of namespaced maps, indeed


namespaced keywords are ok, but not :: alias resolution


Given a seq of maps, is there a better way to return the first map where :x is :y than the (some #(when (...) %) s) thing?


some seems fine although I found it rare to need the first, in my experience usually it's a filter what are criteria of 'better'?


@brownmoose3q Seems like an fairly common operation and I'd expect a find-first thing somewhere in core, but for sure there's nothing stopping me from writing a little helper for it.


There's also filter and first

bb -e '(->> [{:x :x} {:x :y}] (filter #(= :y (:x %)) ) first)'
{:x :y}


I thought filter is lazy and will resolve values as they are being asked, one-by-one, but it seem to resolve not only the asked values, but all of them:

(->> (range 5)
     (filter (fn [el] (println el) true)) ;; if element is printed it indicates that it has been processed
     first ;; asking to resolve only first
;; => "0\n1\n2\n3\n4\n" ;; getting all of them resolved
;; whereas expected to get resolved only the first one, having result of "0\n"

❌ 1

But fret not, transducers to the resque!

(def xf (comp (take 1)
              (filter (fn [el] (println el) true))))

(with-out-str (transduce xf conj (range 5)))
;; => "0\n"
Not an overkill in the slightest. πŸ˜„

πŸ™‚ 1

you might be seeing chunking -- if filter was fully realizing the sequence, this shouldn't work!

bb -e '(->> (range) (filter #(< 10 %)) (take 20))'
(11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30)


Ah, it seems filter is lazy, it was fully resolved because (range 5), which I used to feed into filter, is not lazy! (whereas (range) is E.g.,

(with-out-str (str (take 1 (filter (fn [el] (println el) el) (range)))))
;; => "0\n"
;; resolved just one ^


@brownmoose3q (range 5) is also lazy. The effect you're seeing is an optimization called chunking where lazy seq realize 32 elements at a time. Try (range 50) .

πŸ’‘ 1
❀️ 1
Fredy Gadotti18:03:39

Hey guys, do you know other real world applications (not just libraries) with Clojure that has open source code? is a good example but I'd like to see other applications as well.

Drew Verlee02:03:27

Oynx is a big open source project.

πŸ™Œ 1
ahungry18:03:47 is a great clojure tool for infrastructure

πŸ™Œ 1

And of course, penpot and xtdb.


I'm not sure the code would live up to any reviewer expectations - I wrote it when new to clojure - but AHUBU is a web browser written in clojure I did a few years ago leveraging openJFX built in browser (I added ad filtering code + vim like keybindings and some other UI stuff to it):

πŸ‘ 4
πŸ™Œ 2

Hiya, not sure which channel to post in, but I want to get back into clojure, used it for a few years ending a year ago and miss it. Is there a user group (London or remote) or a project that I can help contribute to?

Drew Verlee02:03:24

This slack is the largest clojure community, just answering questions here goes a long way.

practicalli-johnny10:03:34 is a Clojure user group in London, although all our events are online during the pandemic (and many may remain online after, as they have been more popular online) There are several ways to get help from the community, although slack arguably has the large audience, with #beginners typically the most active for questions/answers (sizes does not guarantee quality though)


#london-clojurians is a pretty active bunch with a lot of meetups (and a lot of recordings already posted to YouTube). If you're in the UK, join #clojure-uk if you haven't already and you can find out where folks are near you and get local help/encouragement etc @ianrvaughan

πŸ‘ 1

I didn’t even search for those channels, thanks for that, I’ve not joined and will checkin with them, cheers.

John Bradens22:03:50

Hey I'm trying to follow this sidebar example, but when I click to open the html page, it's just a blank page with no sidebar. Any idea why it's not working for me? If you have any other suggestions for sidebar tutorials I'm open to new ideas as well.

βœ… 1

Does the console spit anything out?

John Bradens22:03:48

Good question. I'm going to try it with the source code. I did

cd Downloads/reagent-cookbook-master/recipes/simple-sidebar
lein clean
lein cljsbuild once prod

John Bradens22:03:04

I get

Compiling ["resources/public/js/compiled/app.js"] from ["src/cljs"]...
Mar 25, 2022 3:38:06 PM println
WARNING: /Users/username/Downloads/reagent-cookbook-master/recipes/simple-sidebar/target/cljsbuild-compiler-0/reagent/core.js:984: WARNING - [JSC_UNREACHABLE_CODE] unreachable code
return null;

Mar 25, 2022 3:38:06 PM printSummary
WARNING: 0 error(s), 1 warning(s)
Successfully compiled ["resources/public/js/compiled/app.js"] in 17.056 seconds.


the example has an error


change the menu-toggle-did-mount function like so:

(defn menu-toggle-did-mount [this]
  (.click (js/$ (rdom/dom-node this))
          (fn [e]
            (.preventDefault e)
            (.toggleClass (js/$ "#wrapper") "toggled") ;#wrapper will be the id of a div in our home component


note its rdom/dom-node instead of reagent/dom-node


if you are using Calva with VSCode or Cursive this should throw an linting error


@U01EFUL1A8M thanks for the correction, I think the recipe was made back when dom-node was in reagent ns

πŸ‘ 1

Looks like the example code is updated but the readme is outdated


Just updated the readme πŸ‘


The css download might be incompatible


Not the same as in the repo

John Bradens23:03:52

I changed rdom/dom-node to reagent/dom-node and now I get

WARNING: Use of undeclared Var reagent.core/dom-node at line 17 src/cljs/simple_sidebar/core.cljs
Any thoughts on this?

John Bradens23:03:51

I also saw the css download that's linked in bootstrap is different than what is in the repo, so I'm just using what's in the repo for now. I'm new to CSS as well

John Bradens23:03:53

I misread your earlier message @U01EFUL1A8M, I had drom/dom-node already. Once I got errors trying it on my own, I switched to trying a clone of the repo directly


Btw if you’re completely new to CSS and some of the other web fundamentals then I can highly recommend using the MDN docs. There are some really well made comprehensive guides there and a full blown reference with examples and links. The search is also very good. Similarly Iearned a lot of clojure just with also has a good search, links to the source, examples and comments.

John Bradens00:03:05

@U01EFUL1A8M Awesome resources, thanks for sharing!

John Bradens00:03:50

@U01EFUL1A8M @U0522BZ1R Any idea why I get a blank html page when I try to compile the simple-sidebar repo? I get the same error as before:

Compiling ["resources/public/js/compiled/app.js"] from ["src/cljs"]...
Mar 25, 2022 5:33:20 PM println
WARNING: /Users/username/Downloads/reagent-cookbook-master/recipes/simple-sidebar/target/cljsbuild-compiler-0/reagent/core.js:984: WARNING - [JSC_UNREACHABLE_CODE] unreachable code
return null;

Mar 25, 2022 5:33:20 PM printSummary
WARNING: 0 error(s), 1 warning(s)
Successfully compiled ["resources/public/js/compiled/app.js"] in 17.952 seconds.
not sure what it means or if I'm doing something wrong on my end. All I did was change directories, then lein clean, and lein cljsbuild once prod :thinking_face:


@U02387J8EKG I am away from a computer atm, so unfortunately can't look into this any further. Next time I am tho I'll see if I can reproduce what you are seeing

John Bradens00:03:55

That's great, thanks! @U0522BZ1R


@U02387J8EKG did you open the resources/public/index.html file in a browser? I did and it works for me. Note: I saw the same warning when compiling, but it looks like it works.

John Bradens23:03:17

Hey I just checked it and it works for me now! Not sure why it didn't before. Thanks for responding!

πŸ‘ 2