Fork me on GitHub
#beginners
<
2022-10-03
>
Yu09:10:05

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)

Yu09:10:44

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]

)

Yu10:10:40

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.

rolt10:10:51

(defn starts-with? [a b] (= (subs a 0 (min (count a) (count b))) b))

rolt10:10:52

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))

rolt10:10:53

it would be more efficient to change subs with the end index, and simply use = in filter

rolt10:10:48

using identical is a bad idea, you can have 2 string that are equal but not the same object

rolt10:10:50

(identical? "bb" (subs "bbb" 0 2)) => false (= "bb" (subs "bbb" 0 2)) => true

Yu22:10:35

These are output I expect. (ptnCount "abababa" "aba") returns 3 (ptnCount "bbbb" "bb") returns 4 (ptnCount "Dabde" "dab") returns 0

Yu00:10:53

At the moment, I am thinking to use subs or partitions to get substrings.

tschady16:10:50

partition by length of second string, step 1

🙌 1
Noah Bogart13:10:33

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?

quoll14:10:57

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.

👍 2
rolt14:10:16

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"))

👍 1
Noah Bogart14:10:20

ah yeah, good point. the symbol is resolved but it's not evaluated until the enclosing function is executed/evaluated.

Noah Bogart14:10:03

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

quoll14:10:06

I suspected that you were referring to this

quoll14:10:27

Yes, it’s a time-of-evaluation issue

Noah Bogart14:10:57

cool, thanks y'all

quoll14:10:57

Note that :adder-var is not the function, it’s a reference to the name

quoll14:10:34

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

👍 1
quoll14:10:52

(I hope that didn’t read like circular reasoning)

Noah Bogart14:10:01

Right, it evaluates to the var object, which holds a reference to a function definition (aka java class)

Noah Bogart14:10:57

not at all, thanks for the extended explanation

quoll14:10:45

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

Noah Bogart14:10:04

Yeah, that's a great feature.

quoll14:10:04

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 🙂

Noah Bogart14:10:18

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)

rolt15:10:55

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")

👍 1
Noah Bogart15:10:54

Oh yeah, good catch

jumar06:10:54

There's similar problem with spec function instrumentation. It's a tradeoff between (at least) performance and dynamicity / developer friendliness

Epidiah Ravachol14:10:27

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!

rolt16:10:26

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

👍 1
Epidiah Ravachol17:10:35

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

rolt17:10:02

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

rolt17:10:44

and if 2 dependencies depend on edaname try forcing the most recent one

rolt17:10:40

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

Epidiah Ravachol18:10:14

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!

Erlangga06:10:13

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.

👍 1