Fork me on GitHub
Cyrus Malekani08:09:26

Goooooooood morning, is anybody available to help me step through some code? xD

Cyrus Malekani08:09:36

(defn balanced? "Parenthesis balance checker." {:test #(do (assert (true? (balanced? "()"))) (assert (true? (balanced? "()()"))) (assert (true? (balanced? "(()(()))"))) (assert (true? (balanced? "(h(e)j(h(o)p)p)"))) (assert (false? (balanced? "(he)j)h(opp"))) (assert (false? (balanced? "())("))))} [s] (->> s ;; remove non-paranthesis characters (filter #{\( \)}) ;; reduce down to an empty or non-empty vector (reduce (fn [stack item] (cond (#{ \( } item) (conj stack item) (and (#{ \( } (last stack)) (= ({ \) \( } item) (last stack))) (pop stack) :else (conj stack item))) []) ;; return whether we have any unbalanced brackets empty?))


minor, but (peek stack) has better performance than (last stack) for vectors

Cyrus Malekani08:09:57

I'm wondering about the first condition ...

Cyrus Malekani08:09:07

(#{ \( } item) (conj stack item) Specifically what kind of check does the condition do? I've tried to test it out but I'm not getting much wiser ... (#{ \( } item) <- ?

Leaf Garland09:09:27

\( is the syntax for a single character, in this case the ( character. #{} is the syntax for a set literal. #{\(} is a set with a single ( character in it. When we apply sets to a value they return the value if it is in the set, or nil if it is not. So this condition will be nil if item is not a (. I'm not an experienced clojure person but that looks like a really confusing way to check if item is (.

Cyrus Malekani09:09:52

nvm Got help otherwise ... Apparently (!) Sets can take an argument and they return the argument if they contain it.


Unexpected error (IncompatibleClassChangeError) macroexpanding clojure.core/gen-class at (com/github/benjaminasdf/idlelib/completion.clj:1:1). class com.intellij.codeInsight.completion.CompletionExtension has interface com.intellij.lang.LanguageExtension as super class does anybody have advice on this? Context is that I'm hacking around with adding local jars (intellij-idea installation libs) as deps. But I'm new to java so I did not run into common issues yet


Can you share the ns form?


seems like it is fixed by removing openapi from my deps (also needed to add another jar from the intellij lib)


how to download the deps mentioned in project.clj?

{:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring/ring-mock "0.3.2"]]}} 


I think lein deps should dl your deps, if that's what you are looking for. Running lein repl should dl too before it starts up your repl


Getting this issue in VScode, But it is able to run the ring server, strange!


What is it saying the actual error is? Is it just a warning? Might just be something to do with clj-kondo (the linter) which doesn't like :refer :all if I remember correctly


I've created a little full stack web app but am having difficulty connecting the backend to the frontend. When running shadow-cljs watch app I see the Hello world! message. But when starting up the backend server, it compiles and runs fine but the html is blank even though I think I'm telling it to serve the index.html file that calls the js generated by shadow-cljs. Would anyone be willing to look at the code real fast? It's small


When I evaluate the expressions in my index-handler function I am seeing it return the full html of index.html as a string which I think is how it should be returning.


Try replacing [["/"] {:get index-handler}] with ["/" {:get index-handler}]


Ahh, that did help! What a tough little type to spot. Thank you! Unfortunately, now it's on to a cljs issue as it is telling me that ai is undefined in the js script code where ai.core.main() is called


Did you run npm install ?


Yep. The frontend runs fine when I run the dev build under shadow-cljs watch app It also runs fine if I build a release build and run it with a basic http server application (like python's built in). There is just something I am doing wrong in connecting it to my own backend server


Is your backend server serving the js file?


I don't know. All my backend code is just in that repo setting up the handler and route and starting up the jetty server. The handler is pulling in the html from public/index.html . That html file is then calling the js which is also in that public folder


But the backend server is also responsible for serving that js file


And currently it doesn't know how to do that


If you replace app with

(def app
     ["/" {:get index-handler}]))
   (ring/create-file-handler {:path "/"})))


Ahh, yeah, this is where my misunderstanding must be. So I also need a way to tell the server to serve that js (and css) file?


The reason why it is working on the shadow-cljs dev server, is because it is already set up to server those resources


so helpful. thank you!


But on your own backend server, you must manually take care of this


This is also why your python http server worked, because it serves all files in the directory


and how does (ring/create-file-handler {:path "/"}) tell it to do so?


Is that just what that function does?


Cool. Yep, I have all those docs up but I was just trying to get the most basic thing working (for motivation haha) with some quick reading before I took a real deep dive. I will get to slow reading now. Thank you!


Yes, that is all the function does. Good luck! Getting over such hurdles is the joy and frustation of building things from scratch


Yep! I am so happy already that I ditched this huge Luminus template I started with and literally just started over with mkdir ... and went from there. I've probably learned more about professional web development in 2 days than the previous 2 months.


Hello Team, I was just going through one of the clojure video and found below points 1. Clojure has immutable data structure 2. When any element added to any datastructure,example (def a [ 1 2 4]) (def b (conj 5 a)) This will does not create any new vactor instead, memory will be point to the two location of new vector b? is this right? By this clojure handles any new element added Is my understanding is right?


I found this helpful in understanding how Clojure vectors are implemented:

💯 2

ahh, I was reading this!! So got this confusion 😄


I think you got the general idea correct. When adding a new element to a , a itself is never modified. Instead conj returns a new vector b , which has structural sharing with a


but it is not same as the one it is described in document right!


There are a lot of implementation details such that sometimes, for efficiency's sake, some copying is actually done


oh yeah it is the same! just assing reference in the node


I was trying to understand transient and I found few document are very useful, Transient are useful when there is large operation such as append and update on the collection, is my understanding right?


Yes, transients are used as a stand-in replacement for the usual persistent data structures, in places where the extra performance you can get out of them is really needed


cool! thaks


So in production code, does everyone use persistent data structure operations?


What do you mean by production code? As opposed to, say, libraries?


usually in my appicatin we have made use of assoc and conj functions,i am trying to ask is it better if we use assoc! in the code?


I don't know exactly what kind of code you are writing, but you are with high probability better off sticking to the usual persistent operations.


Transients are for very specific use cases. If after testing you find there's a bottleneck in your code, say a lot of assoc operations with the same map that you need to speed up, then transients might help improve that.


ok lets consider I have 1000 elements in the vector and I try to call reduce function on each element and doing assoc opeartion, what do you suggest in that case assoc or assoc! ?


This is just my opinion, but in such a case I would just use the usual persistent data structures. Then you don't have to worry about converting to and from transients, and remembering to use the transient operations everywhere.


If I have a performance issue, and switching to transients solves that, then it's a another story.


ah I understood! thanks


where we make use of assoc , conj, update we should make use of assoc!, conj!

Vlad Kryvokoniev18:09:00

hi guys, can u suggest me please how to make it more concise, readable and comprehensive?

(defn delete-clip
  (swap! app-state assoc :twitch-clips (filter #(not= clip-id (:id %)) (:twitch-clips @app-state))))


My suggestion:

(defn remove-by-id [coll id]
  (remove #(= id (:id %)) coll))

(defn delete-clip!
  (swap! app-state update :twitch-clips remove-by-id clip-id))


Depending on how general you want the code, make delete-clip also take in the app-state and the path, in this case :twitch-clips, as parameters

Alex Miller (Clojure team)18:09:29

you definitely don't want to @app-state in there - you are losing the atomicity you get with swap! doing that

Alex Miller (Clojure team)19:09:05

you have a data structure and a modification to it - make a function out of that - just immutable data -> immutable data

Alex Miller (Clojure team)19:09:43

then make a stateful function that applies that move from one immutable state to another to the app-state

Alex Miller (Clojure team)19:09:04

separating this means you can fully develop and test the immutable parts independent from the stateful parts

Alex Miller (Clojure team)19:09:53

any time you have an atom, you should treat it like red hot lava - isolate where you touch it to as few places as possible and make it clear that the functions that use it are stateful

Vlad Kryvokoniev19:09:59

@U024X3V2YN4, @U064X3EF3 thanks, makes sense. will do