This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-03
Channels
- # announcements (4)
- # aws (19)
- # babashka (55)
- # beginners (40)
- # biff (4)
- # calva (9)
- # cherry (3)
- # cider (8)
- # clj-kondo (26)
- # clj-yaml (3)
- # clojure (92)
- # clojure-austin (14)
- # clojure-europe (21)
- # clojure-nl (1)
- # clojure-norway (5)
- # clojure-portugal (3)
- # clojure-uk (2)
- # clojurescript (48)
- # conjure (19)
- # datalevin (14)
- # docker (13)
- # emacs (3)
- # fulcro (21)
- # gratitude (14)
- # improve-getting-started (1)
- # introduce-yourself (2)
- # joker (4)
- # juxt (2)
- # lsp (12)
- # malli (5)
- # meander (17)
- # off-topic (13)
- # re-frame (7)
- # scittle (2)
- # test-check (2)
I am trying to write function which is case sensitive returns count of matches for example I created a helper function to check if parameter 1 and 2 are equal. (ptnCount "bbbbb" "bb") should return 4 I don't think it's enough to complete the function but no idea what I need to do. I used (identical? a b)
This is my helpler function.
(defn isEqual [a b] (identical? a b))
I feel like I need another helper function to calculate matching count with right size also repeating part
In this case (ptnCount "bbbbb" "bb") second match.
(defn anotherHelper [a b]
)
See this: https://stackoverflow.com/questions/17971466/java-regex-overlapping-matches, then try: (count (re-seq #"(?=(bb))." "bbbbb"))
Thank you; however, I am trying to do it only using clojure. I am having hard time sub-string part like compare bbbb and bb. make bbbb as bb.
and then you can probably do something like that:
(->> (range (count a))
(map (fn [i] (subs a i)))
(filter (fn [s] (starts-with? s b)))
(count))
it would be more efficient to change subs with the end index, and simply use = in filter
using identical is a bad idea, you can have 2 string that are equal but not the same object
These are output I expect. (ptnCount "abababa" "aba") returns 3 (ptnCount "bbbb" "bb") returns 4 (ptnCount "Dabde" "dab") returns 0
Given that writing a function reference in a def
map won’t track when the function is redefined (so you must use #'
to get the indirection), why don’t function calls in function definitions operate the same way?
I don’t know in particular (because I’m not sure exactly what behavior you’re referring to), but in general, I would say that it’s because a reference in a def
is determined at the time of evaluating the form, as opposed to a defn
which doesn’t evaluate immediately. It’s effectively a def
/`fn` and anything inside the fn
won’t be evaluated until the fn
gets called.
the terms I'm using might be incorrect. the evaluation of a symbol return its value. the evaluation of the body of the function happens when the function is called (def a (println "hello)) (defn a [] (println "hello"))
ah yeah, good point. the symbol is resolved but it's not evaluated until the enclosing function is executed/evaluated.
This is the behavior I'm talking about:
user=> (defn adder [a] (+ 10 a))
#'user/adder
user=> (def example {:adder adder :adder-var #'adder})
#'user/example
user=> (defn example-fn [b] (adder b))
#'user/example-fn
user=> ((:adder example) 5)
15
user=> ((:adder-var example) 5)
15
user=> (example-fn 5)
15
user=> (defn adder [a] (+ 20 a))
#'user/adder
user=> ((:adder example) 5)
15
user=> ((:adder-var example) 5)
25
user=> (example-fn 5)
25
cool, thanks y'all
so by evaluating it, it has to look up the current binding for the var, rather than whatever value was in the var when the map was created
Right, it evaluates to the var object, which holds a reference to a function definition (aka java class)
not at all, thanks for the extended explanation
Unrelated to the original question… I really like that the name of a var can be used this way, because it lets me refer to things that are private.
e.g. The internal print function is clojure.core/pr-on
user=> (clojure.core/pr-on [1 2 3] *out*)
Syntax error (IllegalStateException) compiling clojure.core/pr-on at (REPL:1:1).
var: #'clojure.core/pr-on is not public
user=> (#'clojure.core/pr-on [1 2 3] *out*)
[1 2 3]nil
Yeah, that's a great feature.
I’m not sure if circumventing the principle of information hiding is what we should be calling a “feature”, but I love that I can 🙂
i like to think of it as a feature. I don't think fully locking access is ideal, as sometimes you need that access, but making it require an additional step keeps everyone honest about what they're doing (compared to, for example, Python that has no privacy at all)
i mean you're using a map but that's not even necessary:
(def x inc)
(def y x)
(y 1) => 2
(def x dec)
(y 1) => 2
(and with (def y #'x)
it "works")Oh yeah, good catch
There's similar problem with spec function instrumentation. It's a tradeoff between (at least) performance and dynamicity / developer friendliness
Apologies for posting in Beginners instead of a more specific channel, but I'm not entirely sure what's going wrong here and I need an explanation suitable for beginners. I've been working my way through the Luminus tutorial in Web Development with Clojure, 3rd ed. and I hit a wall when it came time to install re-frame-10x. After lots of mucking around to no avail, I decided it was time to give up and download the code from the book's website and see if I can't get that to work. This is the error I get when I try that:
The required JS dependency "react-highlight.js" is not available, it was required by "day8/re_frame_10x/view/components.cljs".
Dependency Trace:
day8/re_frame_10x/preload.cljs
day8/re_frame_10x.cljs
day8/re_frame_10x/styles.cljs
day8/re_frame_10x/view/app_db.cljs
day8/re_frame_10x/view/components.cljs
When I try to install react-highlight.js
via npm
I'm greeted with another error:
While resolving: [email protected]
Found: [email protected]
node_modules/react
peer react@"17.0.1" from [email protected]
node_modules/react-dom
react-dom@"17.0.1" from the root project
react@"17.0.1" from the root project
Could not resolve dependency:
peer react@"^0.14.0 || ^15.0.0 || ^16.0.0" from [email protected]
node_modules/react-highlight.js
react-highlight.js@"1.0.7" from the root project
Conflicting peer dependency: [email protected]
node_modules/react
peer react@"^0.14.0 || ^15.0.0 || ^16.0.0" from [email protected]
node_modules/react-highlight.js
react-highlight.js@"1.0.7" from the root project
I've also tried the most up-to-date versions of reagent, re-frame, and re-frame-10x, as opposed to the older versions in the book and slightly less older versions in the downloaded code; but I keep finding myself back here. I'm not particularly well versed in any of this, so I'm bouncing off of it hard. Any insight would be welcomed!you should not need to install react-hightlight manually. Do you still get this error with the latest version of re-frame-10x ? They do not depend on react-hightlight anymore, that lib is unmaintained
That seems to have helped! But when I try the latest version (1.5.0) of re-frame-10x, I get a different error. A build failure that starts like this:
------ ERROR -----
File: jar:file:/home/epidiah/.m2/repository/org/babashka/sci/0.2.8/sci-0.2.8.jar!/sci/impl/namespaces.cljc
failed to require macro-ns "sci.impl.namespaces", it was required by "sci.impl.namespaces"
Error in phase :compile-syntax-check
RuntimeException: No such var: edamame/normalize-opts
are you using lein, deps, or shadow-cljs ?
if you're using lein, try lein deps :tree
to see if their is a version conflict
you could probably even try to see if directly adding [borkdude/edamame "1.0.0"]
to force a recent version works, but it's still worth it to check what causes the conflcit to document it IMO
Thank you! The tutorial in the book uses lein and shadow-cljs. I did the lein deps :tree
trick and it looks like there were 2 dependencies depending on edamame. Adding the latest version of edamame to project.clj
seems to have solved that.
Now I'm shadow-cljs is telling me I have "Stale Output!" but I think that might be a story for another day.
Thank you for talking me through this!
Hi, I'm also still studying that book,
Back then when I was facing that problem, I messed around with installing npm install react
(edited, i forgot if i use the react-dom too or not, i don't remember it and still can't find the solution website) and react-highlight.js
recklessly based on the solution i got on the internet without think twice or upgrading to latest version in project.clj
(not recommended of course) , but you know what? voila!, it works haha.
The only problem i faced 'till now after i did that was just the dependency conflict since i use the react
newest version, but as long as it works, no problem (don't take it seriously haha.)
But thank you for addressing this issue, i learned a lot after reading the replies and will do that solutions in the future when i'm want to mastering that book.