Fork me on GitHub
#beginners
<
2021-09-14
>
Benjamin10:09:43

(defrecord action [text f]) are single letter keywords "yay" or "nay" ?

Maravedis11:09:34

It's a preference, but I lean toward "nay". You're already doing something kind of verbose, defining a record. Why not go the extra mile and use a meaningful name?

👍 2
noisesmith17:09:49

btw arguments are not keywords - keywords are a data type :f - unlike symbols they never resolve to another value, they just stand for themselves, usually used as keys in hash maps

noisesmith17:09:48

I use f for a function args when there's no name that is more specific (when I'm writing a higher order function that isn't tied to any domain)

4
noisesmith17:09:14

so yeah, with a record, either your code is generic enough that you shouldn't be using a record, or it's specific enough that a name more specific than f is called for

michele mendel10:09:01

Referencing a private atom from another namespace, I need to use double at characters, @@#'some-ns/my-atom. Why is that?

Ed10:09:08

the @thing character is shorthand for (deref thing) ... in you example, you've var quoted the symbol with #' so you're referring to the var called my-atom in the some-ns namespace, which you then deref to get the atom stored in the var, and deref again to get the current state of the atom. Presumably you're doing that because the var is marked as ^:private ? Normally you would require some-ns and not var quote the symbol:

(require '[some-ns :as s])
@s/my-atom

michele mendel10:09:08

Yes, the atom is private, but I need to access it in a test.

noisesmith17:09:38

as an aside, a namespace scope atom is effectively a singleton, and there are a bunch of reasons not to use singletons

noisesmith17:09:01

especially an atom - it's global mutable state

noisesmith17:09:08

if you think it's clearer (-> some-ns/my-atom (var) (var-get) (deref)) is equivalent to @@#'some-ns/my-atom

michele mendel09:09:36

I think the @@ is clearer when writing code, but using the functions makes it easier to understand what's happening. According to your answer and the answer above, var-get is the same as deref

noisesmith16:09:24

deref on a var is the same as var-get, but var-get won't work on atom, future, delay, promise, etc.

popeye12:09:35

I have a function in my production code as below, what is the value passed to the data here?

(defn my-function-1
  [args1 args2 args3]
  (fn[data]
    (println (:country data))))


(my-function-1 args1 args2 args3)    

delaguardo13:09:11

there is only one form which is using data - (:country data) so I would guess data should be a map with that key at least

popeye13:09:56

i meant how map value get inserted to data

emccue13:09:04

there is nothing passed to data in what you showed

delaguardo13:09:08

my-finction-1 is returning a function of one argument and you have to call it providing the value for data

emccue13:09:18

it captures arg1 arg2 and arg3 as a closure, so it could use those

popeye13:09:29

I am calling using comp

(comp (my-function-1 args1 args2 args3) (my-function-2 args1)) 

popeye13:09:02

will it by my-function-2 ?

emak14:09:38

(my-function-1 args1 args2 args3) returns a function f3 (my-function-2 args1) must return also a function f4 if you want to be able to compose (comp f3 f4) which also returns a function.

popeye14:09:07

output of f4 will go as input of f3 right/

delaguardo14:09:37

yes, but -> is a bit different

emak14:09:43

if you then call ((comp f3 f4) x) it is hte same as (f3 (f4 x))

emak14:09:27

so output of (f4 x) is passed as data in f3

emak14:09:02

data is ((my-function-2 args1) x) or whatever is the signature for x

emak14:09:21

(= ((comp f g) x)
   (-> x g f)) ;; true

popeye15:09:22

will take a look and come back

Benjamin15:09:06

Is there a better way than using bean 2 times? and is bean acceptable in production?

(->> ".."
     io/file
     bean
     :absoluteFile
     bean
     :path)

ghadi15:09:02

plain interop calls. Use -> not ->> since these are not collection ops.

(-> (io/file "..")
    .getAbsoluteFile
    .getPath)

catjam 2
alexmiller16:09:17

(.. (io/file "..") getAbsoluteFile getPath)

Cora (she/her)16:09:44

what an absolute file 😎

👏 6
Edward Ciafardini19:09:33

Context: I am currently transferring long strings of code into hiccup and I have been using a python function that converts long strings into 80 char max-length strings for ease of transfer into cljs. I am trying to make a clojure version that I can run in my REPL. Code:

(defn wrap-line [size text]
  (re-seq (re-pattern (str ".{1," size "}\\s|.{1," size "}"))
          (clojure.string/replace text #"\n" " ")))

(defn splitter [text]
(doseq [line (wrap-line 78 text)]
  (println (str "\"" line " \""))))
Desired Output (from python script):
"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis "
"praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias "
"excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui "
"officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum "
"quidem rerum facilis est et expedita distinctio."
Weird Output (from clojure function):
"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis  "
"praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias  "
"excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui  "
"officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum  "
"quidem rerum facilis est et expedita  "
"distinctio. "
It works perfectly until the last line, where it often adds the last word as the final line.

Sampo Toiva20:09:08

@U01TLNH6ULD this happens because your input text occasionally doesn't have a newline in the end. re-seq returns you successive matches, and your regex's first part (before or) expects a space in the end. If there isn't a newline in the original text your code doesn't enter the space there causing the second part of the regex to match the last word.

roelof19:09:11

Who can explain this :

(ns cars-assemble)

  (defn success-rate
  "Gives back the succes rate at a certain speed"
  [speed]
  (cond 
    (== speed 0) 0
    (< speed 5) 1
    (< speed 9) 0.9
    (== speed 9) 0.8
    (== speed 10) 0.77 ))

(defn production-rate
  "Returns the assembly line's production rate per hour,
   taking into account its success rate"
  [speed]
  (float(*(* 221 (success-rate speed) speed))))

(defn working-items
  "Calculates how many working cars are produced per minute"
  [speed]
  (int(/ (production-rate speed) 60 )))

error: 

Expected (= 1392.3 (cars-assemble/production-rate 7)) but got (not (= 1392.3 1392.3))

roelof19:09:48

Those are two the same values and still the test says there are not equal

jussi19:09:36

Are you using float on purpose? Floats are problematic when testing for equality.

roelof19:09:25

yep, when I do not do it , I see this

Expected (= 0.0 (cars-assemble/production-rate 0)) but got (not (= 0.0 0))

jussi19:09:59

Maybe you could use with-precision?

roelof19:09:48

in my code? or in the test ? Im not allowed to change the tests

jussi19:09:35

I was thinking in the tests, since you would then limit the fractions in float.

schmee19:09:54

@U0267SNCPEY what happens if you change the float call to double?

roelof19:09:30

I was the person who ask

schmee19:09:51

ahh, my bad

roelof19:09:02

and when chancing it to a double I see this error message

Expected (= 1701.7 (cars-assemble/production-rate 10)) but got (not (= 1701.7 1701.7000000000003))

jussi19:09:36

(ns foo)

(defn success-rate
  "Gives back the succes rate at a certain speed"
  [speed]
  (cond
    (== speed 0) 0
    (< speed 5) 1
    (< speed 9) 0.9
    (== speed 9) 0.8
    (== speed 10) 0.77))

(defn production-rate
  "Returns the assembly line's production rate per hour,
   taking into account its success rate"
  [speed]
  (float (* (* 221 (success-rate speed) speed))))

(comment
  (= (float 1392.3) (production-rate 7)))
This works, but I'm coercing the expected value to float

jussi19:09:01

Do you have to return a float?

roelof19:09:01

he, so does ( 1 2 3) not multiply 1 2 * 3

roelof19:09:28

but must I do ( (1 2 ) 3)) `

roelof19:09:27

when I do (* (* 221 speed) (success-rate speed))) it works

roelof19:09:48

but when I do (* 221 speed) (success-rate speed)) it fails

roelof19:09:52

very wierd

Chase19:09:31

Yeah, just having (* 221.0 speed (success-rate speed)) seems to work

schmee19:09:55

this has to do with Java types, Float != Double

schmee19:09:10

if you use double, and round it to 1 decimal, it will work

jussi19:09:13

Does something coerce the value into double?

jussi19:09:38

Ah, the * result is double

jussi19:09:11

So yes, the problem is the conversion double->float

jussi20:09:26

Also, I think that performing multiplication twice (`( ( 221 (success-rate speed) speed)`) leads to bigger compound precision error

roelof20:09:45

I have no idea

noisesmith21:09:57

(= 0.0 0) is false but (== 0.0 0) is true, if you need floating point values, figure out a way to do your partitioning without equality checks

noisesmith21:09:56

@U0267SNCPEY in (* (* 221 (success-rate speed) speed)) (notice the close paren you missed) the first * is identity except it throws for an arg that is not an instance of Number

👍 2
noisesmith21:09:21

also int is pretty much only useful for host interop where a method needs Integer, use long and double for numeric type coercions

roelof16:09:56

@U051SS2EU how is the first one identity ? I do not understand what you mean with it

roelof16:09:18

I thought the first one was again multiplycation

noisesmith16:09:00

(* x) is x for all Numbers x

noisesmith16:09:27

look at where the parens are

SignSpice21:09:01

Is there a refactoring tool in emacs that would let me move a symbol from one namespace to another, and update all the references to it?

Frosku21:09:04

clj-refactor.el has move form, which I think does what you're looking for

SignSpice21:09:31

Fabulous. Thank you @U016JPB0Z27!