(defn batting-average-rankings []
(map #({% (team-batting-average %)}) fantasy-teams))
fantasy-teams is a vector of keywords
Wrong number of args (0) passed to: clojure.lang.PersistentArrayMap
i’m trying to make a vector of maps out of a vector of keywords
feels like i’m missing something obvious
can you make a map in a function like that?
i’m getting a kondo that says map is called with 0 args but
i’m passing a function and fantasy-teams to map
ok
(defn batting-average-rankings []
(map (fn [fantasy-team] {fantasy-team (team-batting-average fantasy-team)}) fantasy-teams))
this works so
must be something wrong with my anonymous function syntax
(map #(team-batting-average %) fantasy-teams)
({:foo 5} {:bar 1} {:baz 2})
woah
if i do that i just get the values
not maps of team to value
I've defined my team-batting-average
like this:
(defn team-batting-average [team] {team (rand-int 10)})
ah
mine is just the number
suppose i could tho
If you want it in-line, you could do this too:
(map #(hash-map % (rand-int 10)) fantasy-teams)
hash-map
will create a new map
(map #(hash-map % (rand-int 10)) fantasy-teams)
({:foo 7} {:bar 5} {:baz 2})
yeah i’m reading why it doesn’t work
https://stackoverflow.com/questions/13204993/anonymous-function-shorthand
but that makes sense
thanks!
you're most welcome! 🙂
From my understanding, literal lambda will call the first thing as function, e.g. #(println %)
will call println
. So when you do #({:a %})
, it’s not returning a map but calling the map as function. And of course when calling a map as function like this you need to provide an argument as a lookup value, which in this case doesn’t exist, and hence the error Wrong number of args (0) passed to: clojure.lang.PersistentArrayMap
totally, thanks! I should have paid more attention to what the #()
form expands to
which is what you said
🙂
I want to refactor my code a bit. move different parts to different files. My problem is: how to I access symbols from my own files? I thought I can just use require and point to my other file. Like I have a core.clj and a loader.clj, which has a (def data ....) thing that I want to use in core.
@christiaaan So core.clj
would have (:require [project.loader :as loader])
in its ns
form (with project
replaced by whatever the name of your project is) and then you’d refer to loader/data
in core.clj
…? Yes, that should work. What did you try and what didn’t work?
(and you would need to eval the loader.clj
file into the REPL from your editor for those new def
’s etc to be picked up)
yes, that did what I wanted. thank you.
I use
(ns demo.core
(:use [demo.loader]))
and everything is fine, I can use it like it was defined in this file. For the demo I want it to be as straight as possible. I get all squiggly lines under the symbols from the loader in the core editor, though. That might be confusing for the audience. Do I have to do more to make it "known" as a "resolved symbol"? (I use calva)If you do not get an answer here on your Calva-specific question, there is a #calva channel, too.
Every IDE/editor has its own separate configuration options for things like this, and not all offer the same capabilities.
can anyone explain what's going on here?
=> (java-time/format "d MMM YYYY" (java-time/local-date "2022-01-01"))
"1 Jan 2021"
if I pass in "2022-01-01" or "2022-01-02", the year comes out as 2021, but if I pass in any date after that, the year comes out correctly as 2022
The current docstring as seen on http://dm3.github.io/clojure.java-time/java-time.html#var-formatter uses the example "YYYY/mm/DD" which manages to get them all wrong - it's so bad I wonder if it was deliberate 🙂. I see there has been a recent commit that fixes the month but not the day and year.
It's been a number of years since we learned that the hard way. Literally a $64,000 mistake, customer bled money for just over 2 days. MM vs mm. I might see of I can submit a doc change to the link you provided.
I've submitted a pull request earlier today
try yyyy
instead of YYYY
That’s a classic gotcha in Javas Time Formatting. Uppercase ‘Y’ is the “week-based-year”: https://dev.to/shane/yyyy-vs-yyyy-the-day-the-java-date-formatter-hurt-my-brain-4527
😮 thanks, that worked
think I might raise a bug on the java-time documentation. the date format used in the example is "YYYY/mm/DD" which is doubly confusing (even though I accept it's only an example)
The squiggle is from clj-kondo trying to persuade you to use require
instead. I’m not familiar with why, but I would hesitate to demo an anti-pattern.
Looking for websocket library for cljs, I know about sente but I’m under the impression that it’s an end-to-end solution that requires the server side to be using sente as well. Any suggestions on good projects out there? Or even perhaps I’m overlooking a very simple way to roll my own websockets without any library?
<https://github.com/ptaoussanis/sente>
do you need some reliable way to do ajax or actual websockets
I wonder when there will be something like (format-timestamp [:year "-" :month-numeric "-" :day-in-month "T" :hour24 "-" :minute "-" :second-with-ms :timezone-numeric] …)
:thinking_face:
Because you could do cljs-ajax on a timer and ping an endpoint REST style (if i'm not misusing the term REST)
Hello I have a sequence [ [0] [1] [2] [3] ] of lesson info and I want the result to be everything up to and including the user's current lesson so if the lesson number for the user was 2 i'd want the result to be [ 0 1 2 ]
there are more elements but I think the numerical placeholders are sufficient to get the idea across.
I can use (get @atom lesson-index) to get a specific one, but i'm not certain what the best way is to iterate over the collection and jam it all into a new coll
ought I use a for loop? or is there a way to do what I want with map or apply or reduce? thx
(take lesson-num lesson-info-seq)
I think that would work, https://clojuredocs.org/clojure.core/take. I'm a complete beginner though
(take n coll) returns the first n elements of a sequence
ight on. thank you. i think i can do it with (range lessons-count)
to get something like (0 1 2)
and then use (into [] (map (fn [lesson-number] (get @lesson-atom lesson-number))
Because somehow I gotta call
(get @lesson-atom 0)
(get @lesson-atom 1)
(get @lesson-atom 2)
and weld all the results together.
So getting the (range lessons-count)
gets me ( 0 1 2 )
and then i can (map #(get @lesson-atom %))
before shoving it all (into [])
a fresh seqthat might be a lot if you're new, but usually there is a way to use map / apply / reduce instead of a for-loop. (take n) would work if i knew how many elements to take, but if i'm counting the number of elements in each lesson slot, i might as well just grab them while there
Thanks for the explanation
Sure! I just have a snag where I keep getting ([{stuff]})
and i need [{stuff}]
so i'm fiddling with (into [])
and (flatten coll)
It's generally recommended to avoid the use of flatten
when it comes to sequences
Here's a reference for further research: <https://stuartsierra.com/2019/09/25/sequences-in-flatland>
Ok, what would be good to use instead in this case?
mapcat
, reduce
things like that
this is a final step before sending the data to the cljs client
i will try and get it working with mapcat
btw, as a side note, I used to struggle with other versions of mapcat
in other languages, as they are called, sometimes as flatmap
I never quite got that name. Whereas mapcat
makes complete sense! 🙂
mapcat
=> you're map
ping and con cat
enating 🙂
(into [] (mapcat seq ...))
did the trick! thank you 🙂
Flatland... great book by the way.
The people in the 2d-world have a "safe" that's just a rectangle in the plane, and a 3d-shaped-person-shape is able to simply "reach around" the boundaries of the safe and grab the insides. a 4d-alien would be able to reach into any of our 3d objects as if they were boundaryless
Hello fellow beginners! I'm going to be livestreaming the development of my startup in about 10 minutes. If you're new to Clojure and learn by watching, then you may enjoy checking out the stream. I do a lot of ClojureScript/Reagent work along with Ring backend server development, so it's a good overview of the whole stack. I start each stream with a 30-minute code warmup too. Ever since watching @sekao's https://www.youtube.com/watch?v=XONRaJJAhpA about his https://github.com/oakes/odoyle-rules, I've had my brain bent around the thing, and I want to learn how to use it. I'm going to attempt to get familiar with it via a relatively simple coding challenge: animating a bouncing SVG ball around a screen. No promises as to anything looking pretty or even working: the concept of the library is both simple and radical at the same time. All I will promise is a few error messages and a self-deprecating laugh or two 😛 Anyway, if you'd like to join the stream, here it is: https://www.twitch.tv/a_fry_
nb. for is not a loop
you should be able to use (into [] cat ...)
to do the same thing
(cat will call seq implicitly)
(ins)user=> (into [] (mapcat seq [[1] [2 3] [4 5 6]]))
[1 2 3 4 5 6]
(cmd)user=> (into [] cat [[1] [2 3] [4 5 6]])
[1 2 3 4 5 6]
Is there a way to pass extra args to -main using the cli tool? For example I’ve got a -main
function with [cmd & {:keys [foo bar] :as args}
and then I would like to invoke it like this clj -M:my-alias cmd :foo "value" :bar "val"
@g3o you're describing behavior that is close to the -X
way to invoke functions. https://clojure.org/reference/deps_and_cli#_executing_a_function . Note there are some differences (and benefits).
@g3o, I wrote https://github.com/phronmophobic/clirun for this purpose
although it would be `clj -M:run:my-alias cmd :foo '"value"' :bar '"val"'`
clj -M:run my.ns/fn cmd :foo '"value"' :bar '"val"'
ah nice, I’ll have a look
there's not much code, so you can also just fork it to make it work the way you want.
what if the values are keyword too? do I need to wrap them in double quotes?
nope!
It uses the same parsing rules as -X (unless those rules were updated since I checked)
basically, each token is parsed as edn separately
👍 thanks
actually the example should be:
clj -M:run my.ns/fn cmd :foo '"value"' :bar '"val"'
Can I pass a single param in the function? or the function should alway accept a map?
Also watch out for mm
vs MM.
One is month, one is minute. Can sometimes lie hidden and appear to work, until it causes catastrophic and costly damage to a customer's data.
I am getting this error Key is missing value: foo
clj -X <http://path.to|path.to>.the/fn foo
Same with dd
and DD.
Save a bookmark to those date format code docs.
you're behavior that you're wanting here is a bit between both worlds. -main
needs a signature of a collection of strings. -X
can call any function but the signature is a single map.
Oh I see, I have to think of another way then.
Thank you very much
is there a clever way in clojure to get from “A” to “B” like inc
in another language i just make the alphabet an array and indexed into it
user=> (int \A)
65
user=> (inc *1)
66
user=> (char *1)
\B
oooh interesting
@cschep chars can be cast to unicode codepoints, then incremented and cast back
user=> (apply str (map (comp char inc int) "hello world"))
"ifmmp!xpsme"
cat : D
I settled on https://github.com/weavejester/haslett , only in my cljs client. It not only gives you the websocket but it also ties the messages into core.async channels, which I happened to want.
ha, I remembered why I did this now.. I’m generating spreadsheet cell addresses and (inc Z)
needs to equal AA
Then you're talking about a base-26 number system that use letters (no numbers) 🤓