Fork me on GitHub
#beginners
<
2022-06-05
>
talentedleader07:06:42

Hello everyone! I am new here and already have a question 🙂

👋 2
talentedleader07:06:12

But when I run the code, I get an error: Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: deleted-files in this context, compiling:(dirdiff/core_test.clj:8:27)

talentedleader07:06:30

I couldnt get that solution working and I dont understand what I am doing wrong sadly 😞

talentedleader07:06:58

My code is here:

talentedleader07:06:13

(ns dirdiff.core
  (:gen-class))
(ns com.lispcast.my-ns
  (:require [clojure.set :as set]))

(defn no-prefix [prefix]
    (comp (filter #(clojure.string/starts-with? % prefix))
          (map #(subs % (count prefix)))))

(defn load-string-set [a file]
    (->> file
         slurp
         clojure.string/split-lines
         (into #{} a)))


(defn deleted-files
  ;"Die Funktion gibt einen String zurück, der beschreibt, welche Dateien gelöscht wurden.
   ;Um dies zu ermitteln, nimmt sie zwei mit dem Befehl find erstellten Dateien entgegen.
   ;prefix-davor bezeichnet das Prefix, welches von den Zeilen in der davor-Datei vor dem Vergleich entfernt werden soll.
   ;prefix-danach bezeichnet das Prefix, welches von den Zeilen in der danach-Datei vor dem Vergleich entfernt werden soll.
   ;davor bezeichnet die Datei als String, die den ersten Dateisystemzustand beschreibt.
   ;danach bezeichnet die Datei als String, die den zweiten Dateisystemzustand beschreibt."
  [prefix-davor prefix-danach davor danach]
  ; TODO
  (clojure.set/difference (load-string-set (no-prefix prefix-davor) davor)
                          (load-string-set (no-prefix prefix-danach) danach)))
  

(deleted-files "davor" "danach" "resources/davor.txt" "resources/danach.txt")

V09:06:33

Your problem seem to be that you have not loaded the peace of code/namespace into the repl

talentedleader09:06:06

How to do that, and what's namespace should I add?

talentedleader09:06:24

Is the problem occured due to this:

(ns com.lispcast.my-ns
  (:require [clojure.set :as set]))

V09:06:34

What editor are you using?

talentedleader09:06:48

I am using an online development environment

Eric12:06:56

I have a really basic/general question about how to structure Clojure programs. I’ve started out doing almost everything in my global project namespace like this:

;;; "Main"
...
(def window-x-min spad)
(def window-x-max (- swidth spad))
(def window-y-min spad)
(def window-y-max (- sheight spad))

;; Number of grid pixels to interpolate/render across each dimension
(def px-count 100)
;; Resulting size of each grid pixel
(def px-size (double (/ (- window-x-max window-x-min) px-count)))

(def data (load-csv "/Users/easy/Downloads/20220519082008-04213-data.csv"))

(def imported-meters-data (->> data
                               clean-csv-data
                               drop-header-row
                               csv-strings->numbers
                               project-WGS84->meters))

(def data-proportion (/ (- (get-max ALL-X) (get-min ALL-X))
                        (- (get-max ALL-Y) (get-min ALL-Y))))

(def scaled-data (scale-map-data data-proportion imported-meters-data))

(def grid-resolution 5)
(def scaled-grid (point-grid window-x-min window-y-min
                             window-x-max window-y-max
                             grid-resolution))
But I know this is wrong, because I’m not inside -main (everything just runs when I load the file in Calva) and every function that uses the globals I’m defining is dependent on the order of my functions.

Eric12:06:31

So my question is: How do I define constants as I go (in -main) without ending up writing all my code in a (let [])?

Eric12:06:57

I do have a few complex functions that build from many intermediate values and it looks like the bulk of the code ends up in a long let

Eric12:06:20

Maybe my functions are too big? They’re almost all less than 20 lines, though I see the style guide says they should be less than 10.

Ben Sless12:06:45

Stick them in a map, call it "default-options", top level function merges it with user provided options

Eric13:06:06

ok, thank you

Eric20:06:02

This approach was helpful. Thanks, @UK0810AQ2. Clojure/FP is so mind-bendingly new to me that I’m sometimes at a loss for guessing an idiomatic approach to even basic stuff. 🙂

Ben Sless03:06:20

Glad I could help. I recommend 4clojure and the Clojure Koans to get used to the language and idioms. Both immutability and working with data first can be new to programmers coming to Clojure. Keep at it and you'll be fluent in no time

1
Matt Rybin13:06:44

Hello 🙂 I’m creating a simple input component in reagent. I want to check if a validator function exist and also validate if it exist.

;; The validator output a string if there is a error or nil
(if
  (and (some? validator) (some? (validator @value)))
  (str "error"))
This code work but I wonder if there is a core function that both check if a function exist and then also run the function and return true if a string exist and false if the output is nil.

teodorlu21:06:52

Could you provide a default validator as (constantly true) or (constantly false)? Then you wouldn't have to check whether the validator is nil.

talentedleader16:06:46

How to turn the contents (which are strings) of a set to simple strings?

talentedleader16:06:28

#{"/Fotos/Mallorca-2017/dc-10.jpg" "/Fotos/Azoren-2018/buffet" "/Fotos/Azoren-2018/restaurant/dc-43.jpg" "/Fotos/Azoren-2018/restaurant/dc-42.jpg" "/Fotos/Mallorca-2017/dc-19.jpg" "/Fotos/Azoren-2018/restaurant/dc-41.jpg" "/Fotos/Mallorca-2017/dc-11.jpg" "/Fotos/Azoren-2018/restaurant/dc"} expected: (= expected actual) actual: (not (= "/Fotos/Azoren-2018/buffet\n/Fotos/Azoren-2018/restaurant/dc\n/Fotos/Azoren-2018/restaurant/dc-41.jpg\n/Fotos/Azoren-2018/restaurant/dc-42.jpg\n/Fotos/Azoren-2018/restaurant/dc-43.jpg\n/Fotos/Mallorca-2017/dc-10.jpg\n/Fotos/Mallorca-2017/dc-11.jpg\n/Fotos/Mallorca-2017/dc-19.jpg" #{"/Fotos/Mallorca-2017/dc-10.jpg" "/Fotos/Azoren-2018/buffet" "/Fotos/Azoren-2018/restaurant/dc-43.jpg" "/Fotos/Azoren-2018/restaurant/dc-42.jpg" "/Fotos/Mallorca-2017/dc-19.jpg" "/Fotos/Azoren-2018/restaurant/dc-41.jpg" "/Fotos/Mallorca-2017/dc-11.jpg" "/Fotos/Azoren-2018/restaurant/dc"}))

talentedleader16:06:50

I think the test is requesting from me, not a set but string versions with ", am I wrong?

talentedleader16:06:47

Can anyone help me what is happening in this code:

talentedleader16:06:14

(ns dirdiff.core
  (:gen-class))
(require '[clojure.set])

(defn no-prefix [prefix]
    (comp (filter #(clojure.string/starts-with? % prefix))
          (map #(subs % (count prefix)))))

(defn load-string-set [a file]
    (->> file
         slurp
         clojure.string/split-lines
         (into #{} a)))


(defn deleted-files
  "Die Funktion gibt einen String zurück, der beschreibt, welche Dateien gelöscht wurden.
   Um dies zu ermitteln, nimmt sie zwei mit dem Befehl find erstellten Dateien entgegen.
   prefix-davor bezeichnet das Prefix, welches von den Zeilen in der davor-Datei vor dem Vergleich entfernt werden soll.
   prefix-danach bezeichnet das Prefix, welches von den Zeilen in der danach-Datei vor dem Vergleich entfernt werden soll.
   davor bezeichnet die Datei als String, die den ersten Dateisystemzustand beschreibt.
   danach bezeichnet die Datei als String, die den zweiten Dateisystemzustand beschreibt."
  [prefix-davor prefix-danach davor danach]
  ; TODO
(disj (clojure.set/difference (load-string-set (no-prefix prefix-davor) davor)
                          (load-string-set (no-prefix prefix-danach) danach)) 
                      "/Fotos/Azoren-2018/buffet/dc-40.jpg"
                      "/Fotos/Azoren-2018/buffet/dc-41.jpg"
"/Fotos/Azoren-2018/buffet/dc-42.jpg"
"/Fotos/Azoren-2018/buffet/dc-43.jpg"
))

  
(deleted-files "davor" "danach" "resources/davor.txt" "resources/danach.txt")

talentedleader16:06:29

The bash line command window:

talentedleader16:06:45

cloud9.bewerber2:~/environment/dirdiff $ lein test
(#{/Fotos/Mallorca-2017/dc-10.jpg /Fotos/Azoren-2018/buffet /Fotos/Azoren-2018/restaurant/dc-43.jpg /Fotos/Azoren-2018/restaurant/dc-42.jpg /Fotos/Mallorca-2017/dc-19.jpg /Fotos/Azoren-2018/restaurant/dc-41.jpg /Fotos/Mallorca-2017/dc-11.jpg /Fotos/Azoren-2018/restaurant/dc})

lein test dirdiff.core-test
(#{/Fotos/Mallorca-2017/dc-10.jpg /Fotos/Azoren-2018/buffet /Fotos/Azoren-2018/restaurant/dc-43.jpg /Fotos/Azoren-2018/restaurant/dc-42.jpg /Fotos/Mallorca-2017/dc-19.jpg /Fotos/Azoren-2018/restaurant/dc-41.jpg /Fotos/Mallorca-2017/dc-11.jpg /Fotos/Azoren-2018/restaurant/dc})

lein test :only dirdiff.core-test/show-deleted-test

FAIL in (show-deleted-test) (core_test.clj:9)
Find deleted files
erwartet:
/Fotos/Azoren-2018/buffet
/Fotos/Azoren-2018/restaurant/dc
/Fotos/Azoren-2018/restaurant/dc-41.jpg
/Fotos/Azoren-2018/restaurant/dc-42.jpg
/Fotos/Azoren-2018/restaurant/dc-43.jpg
/Fotos/Mallorca-2017/dc-10.jpg
/Fotos/Mallorca-2017/dc-11.jpg
/Fotos/Mallorca-2017/dc-19.jpg

actual:

expected: (= expected actual)
  actual: (not (= "/Fotos/Azoren-2018/buffet\n/Fotos/Azoren-2018/restaurant/dc\n/Fotos/Azoren-2018/restaurant/dc-41.jpg\n/Fotos/Azoren-2018/restaurant/dc-42.jpg\n/Fotos/Azoren-2018/restaurant/dc-43.jpg\n/Fotos/Mallorca-2017/dc-10.jpg\n/Fotos/Mallorca-2017/dc-11.jpg\n/Fotos/Mallorca-2017/dc-19.jpg" nil))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Tests failed.

talentedleader16:06:15

It says failed, regarding the code above, but I dont understand how it fails?

hiredman16:06:22

You are expecting the return value to be the same as the printed output

talentedleader16:06:13

But where does it fail?

talentedleader16:06:15

It fails because I couldnt list it as strings like below, but returned them in a set?

/Fotos/Azoren-2018/buffet
/Fotos/Azoren-2018/restaurant/dc
/Fotos/Azoren-2018/restaurant/dc-41.jpg
/Fotos/Azoren-2018/restaurant/dc-42.jpg
/Fotos/Azoren-2018/restaurant/dc-43.jpg
/Fotos/Mallorca-2017/dc-10.jpg
/Fotos/Mallorca-2017/dc-11.jpg
/Fotos/Mallorca-2017/dc-19.jpg

plexus21:06:07

You're not showing us the code of the test so it's hard to say, but you are getting a nil somewhere where you're expecting a string

talentedleader08:06:39

(ns dirdiff.core-test
  (:require [clojure.test :refer :all]
            [dirdiff.core :refer :all]))

(deftest show-deleted-test
  (testing "Find deleted files"
           (let [expected (slurp "resources/erwartet.txt")
                 actual   (deleted-files "davor" "danach" "resources/davor.txt" "resources/danach.txt")]
             (is
               (= expected
                  actual)
               (str "erwartet:\n" expected "\n\nactual:\n" actual)))))

talentedleader08:06:42

here is the test

talentedleader08:06:22

I am still stuck...