This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-14
Channels
- # announcements (40)
- # aws (9)
- # babashka (21)
- # beginners (75)
- # calva (56)
- # chlorine-clover (1)
- # cider (12)
- # circleci (1)
- # clj-kondo (7)
- # cljsrn (13)
- # clojars (3)
- # clojure (171)
- # clojure-dev (11)
- # clojure-europe (64)
- # clojure-nl (11)
- # clojure-spec (6)
- # clojure-uk (9)
- # clojurescript (31)
- # conjure (1)
- # cursive (7)
- # datascript (7)
- # datomic (9)
- # emacs (4)
- # fulcro (65)
- # introduce-yourself (1)
- # jobs-discuss (7)
- # kaocha (7)
- # lsp (39)
- # missionary (5)
- # off-topic (54)
- # pathom (10)
- # re-frame (6)
- # shadow-cljs (110)
- # tools-deps (41)
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?
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
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)
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
Referencing a private atom from another namespace, I need to use double at characters, @@#'some-ns/my-atom
. Why is that?
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
Yes, the atom is private, but I need to access it in a test.
as an aside, a namespace scope atom is effectively a singleton, and there are a bunch of reasons not to use singletons
especially an atom - it's global mutable state
if you think it's clearer (-> some-ns/my-atom (var) (var-get) (deref))
is equivalent to @@#'some-ns/my-atom
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
deref on a var is the same as var-get, but var-get won't work on atom, future, delay, promise, etc.
Thanks
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)
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
my-finction-1
is returning a function of one argument and you have to call it providing the value for data
I am calling using comp
(comp (my-function-1 args1 args2 args3) (my-function-2 args1))
(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.
yes, but ->
is a bit different
Is there a better way than using bean
2 times?
and is bean
acceptable in production?
(->> ".."
io/file
bean
:absoluteFile
bean
:path)
plain interop calls. Use -> not ->> since these are not collection ops.
(-> (io/file "..")
.getAbsoluteFile
.getPath)
or even ..
(.. (io/file "..") getAbsoluteFile getPath)
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.@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.
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))
yep, when I do not do it , I see this
Expected (= 0.0 (cars-assemble/production-rate 0)) but got (not (= 0.0 0))
@U0267SNCPEY what happens if you change the float
call to double
?
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))
(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
Also, I think that performing multiplication twice (`( ( 221 (success-rate speed) speed)`) leads to bigger compound precision error
(= 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
@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
also int
is pretty much only useful for host interop where a method needs Integer, use long
and double
for numeric type coercions
@U051SS2EU how is the first one identity ? I do not understand what you mean with it
(* x) is x for all Numbers x
look at where the parens are
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?
Fabulous. Thank you @U016JPB0Z27!