beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
cschep 2021-03-22T04:41:44.220500Z

(defn batting-average-rankings []
  (map #({% (team-batting-average %)}) fantasy-teams))

cschep 2021-03-22T04:41:52.220700Z

fantasy-teams is a vector of keywords

cschep 2021-03-22T04:42:07.221Z

Wrong number of args (0) passed to: clojure.lang.PersistentArrayMap

cschep 2021-03-22T04:42:15.221300Z

i’m trying to make a vector of maps out of a vector of keywords

cschep 2021-03-22T04:42:20.221500Z

feels like i’m missing something obvious

cschep 2021-03-22T04:42:34.221800Z

can you make a map in a function like that?

cschep 2021-03-22T04:44:09.222100Z

i’m getting a kondo that says map is called with 0 args but

cschep 2021-03-22T04:44:21.222400Z

i’m passing a function and fantasy-teams to map

cschep 2021-03-22T04:45:21.222600Z

ok

cschep 2021-03-22T04:45:23.223Z

(defn batting-average-rankings []
  (map (fn [fantasy-team] {fantasy-team (team-batting-average fantasy-team)}) fantasy-teams))

cschep 2021-03-22T04:45:26.223200Z

this works so

cschep 2021-03-22T04:45:33.223500Z

must be something wrong with my anonymous function syntax

dharrigan 2021-03-22T04:46:59.223700Z

(map #(team-batting-average %) fantasy-teams)

dharrigan 2021-03-22T04:47:07.223900Z

({:foo 5} {:bar 1} {:baz 2})

cschep 2021-03-22T04:48:57.224100Z

woah

cschep 2021-03-22T04:49:59.224300Z

if i do that i just get the values

cschep 2021-03-22T04:50:06.224600Z

not maps of team to value

dharrigan 2021-03-22T04:50:31.224900Z

I've defined my team-batting-average like this:

dharrigan 2021-03-22T04:50:33.225100Z

(defn team-batting-average [team] {team (rand-int 10)})

cschep 2021-03-22T04:50:47.225300Z

ah

cschep 2021-03-22T04:50:59.225600Z

mine is just the number

cschep 2021-03-22T04:51:04.225800Z

suppose i could tho

dharrigan 2021-03-22T04:51:43.226100Z

If you want it in-line, you could do this too:

dharrigan 2021-03-22T04:51:45.226300Z

(map #(hash-map % (rand-int 10)) fantasy-teams)

dharrigan 2021-03-22T04:51:54.226500Z

hash-map will create a new map

dharrigan 2021-03-22T04:52:13.226700Z

(map #(hash-map % (rand-int 10)) fantasy-teams)
({:foo 7} {:bar 5} {:baz 2})

cschep 2021-03-22T04:53:15.226900Z

yeah i’m reading why it doesn’t work

cschep 2021-03-22T04:53:20.227400Z

but that makes sense

cschep 2021-03-22T04:53:23.227600Z

thanks!

dharrigan 2021-03-22T04:53:30.227800Z

you're most welcome! 🙂

aratare 2021-03-22T05:00:30.230Z

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

cschep 2021-03-22T05:57:51.230300Z

totally, thanks! I should have paid more attention to what the #() form expands to

cschep 2021-03-22T05:57:54.230500Z

which is what you said

cschep 2021-03-22T05:57:54.230700Z

🙂

Christian 2021-03-22T11:18:23.233400Z

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.

seancorfield 2021-03-22T11:33:13.235200Z

@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?

seancorfield 2021-03-22T11:33:51.236Z

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

Christian 2021-03-22T12:17:26.236400Z

yes, that did what I wanted. thank you.

Christian 2021-03-22T12:26:31.238800Z

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)

2021-03-22T12:32:36.239Z

If you do not get an answer here on your Calva-specific question, there is a #calva channel, too.

2021-03-22T12:32:53.239200Z

Every IDE/editor has its own separate configuration options for things like this, and not all offer the same capabilities.

Tim Robinson 2021-03-22T12:47:21.240700Z

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

Tim Robinson 2021-03-23T13:40:32.327800Z

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.

😯 1
chepprey 2021-03-23T14:46:46.328400Z

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.

Tim Robinson 2021-03-23T15:09:19.328600Z

I've submitted a pull request earlier today

💫 1
lassemaatta 2021-03-22T12:52:15.240800Z

try yyyy instead of YYYY

javahippie 2021-03-22T12:52:34.241Z

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

Tim Robinson 2021-03-22T12:53:45.241300Z

😮 thanks, that worked

Tim Robinson 2021-03-22T13:24:37.241500Z

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)

pez 2021-03-22T14:40:33.241700Z

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.

pinealan 2021-03-22T14:54:08.245200Z

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?

tony 2021-03-22T14:54:56.245400Z

<https://github.com/ptaoussanis/sente>

sova-soars-the-sora 2021-03-22T15:29:00.245800Z

do you need some reliable way to do ajax or actual websockets

ordnungswidrig 2021-03-22T15:46:24.246Z

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:

sova-soars-the-sora 2021-03-22T15:47:07.246200Z

Because you could do cljs-ajax on a timer and ping an endpoint REST style (if i'm not misusing the term REST)

sova-soars-the-sora 2021-03-22T15:53:05.247700Z

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 ]

sova-soars-the-sora 2021-03-22T15:53:19.248100Z

there are more elements but I think the numerical placeholders are sufficient to get the idea across.

sova-soars-the-sora 2021-03-22T15:53:58.248700Z

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

sova-soars-the-sora 2021-03-22T15:54:27.249200Z

ought I use a for loop? or is there a way to do what I want with map or apply or reduce? thx

Jordan Gibson 2021-03-22T15:57:07.250400Z

(take lesson-num lesson-info-seq)

Jordan Gibson 2021-03-22T15:57:25.250800Z

I think that would work, https://clojuredocs.org/clojure.core/take. I'm a complete beginner though

Jordan Gibson 2021-03-22T15:57:53.251500Z

(take n coll) returns the first n elements of a sequence

sova-soars-the-sora 2021-03-22T16:04:37.255200Z

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 seq

🙌 1
sova-soars-the-sora 2021-03-22T16:06:36.256100Z

that 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

Jordan Gibson 2021-03-22T16:08:27.256400Z

Thanks for the explanation

sova-soars-the-sora 2021-03-22T16:15:29.257700Z

Sure! I just have a snag where I keep getting ([{stuff]}) and i need [{stuff}] so i'm fiddling with (into []) and (flatten coll)

dharrigan 2021-03-22T16:20:03.258200Z

It's generally recommended to avoid the use of flatten when it comes to sequences

dharrigan 2021-03-22T16:20:31.258800Z

Here's a reference for further research: <https://stuartsierra.com/2019/09/25/sequences-in-flatland>

sova-soars-the-sora 2021-03-22T16:20:33.258900Z

Ok, what would be good to use instead in this case?

dharrigan 2021-03-22T16:20:58.259200Z

mapcat, reduce things like that

sova-soars-the-sora 2021-03-22T16:21:31.259500Z

this is a final step before sending the data to the cljs client

sova-soars-the-sora 2021-03-22T16:21:36.259700Z

i will try and get it working with mapcat

dharrigan 2021-03-22T16:22:10.260200Z

btw, as a side note, I used to struggle with other versions of mapcat in other languages, as they are called, sometimes as flatmap

dharrigan 2021-03-22T16:22:22.260600Z

I never quite got that name. Whereas mapcat makes complete sense! 🙂

dharrigan 2021-03-22T16:22:53.261100Z

mapcat => you're map ping and con cat enating 🙂

1
sova-soars-the-sora 2021-03-22T16:23:35.261800Z

(into [] (mapcat seq ...)) did the trick! thank you 🙂

🙌 1
sova-soars-the-sora 2021-03-22T16:26:38.262100Z

Flatland... great book by the way.

1
sova-soars-the-sora 2021-03-22T16:27:54.263300Z

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

2021-03-22T18:07:54.264700Z

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_

2021-03-22T18:23:24.265200Z

nb. for is not a loop

2021-03-22T18:24:20.265400Z

you should be able to use (into [] cat ...) to do the same thing

2021-03-22T18:24:27.265600Z

(cat will call seq implicitly)

2021-03-22T18:25:10.265800Z

(ins)user=&gt; (into [] (mapcat seq [[1] [2 3] [4 5 6]]))
[1 2 3 4 5 6]
(cmd)user=&gt; (into [] cat [[1] [2 3] [4 5 6]])
[1 2 3 4 5 6]

😸 1
yiorgos 2021-03-22T19:58:05.268400Z

Is there a way to pass extra args to -main using the cli tool? For example I’ve got a -main function with [cmd &amp; {:keys [foo bar] :as args} and then I would like to invoke it like this clj -M:my-alias cmd :foo "value" :bar "val"

dpsutton 2021-03-22T20:06:44.269600Z

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

phronmophobic 2021-03-22T20:08:18.270Z

@g3o, I wrote https://github.com/phronmophobic/clirun for this purpose

phronmophobic 2021-03-22T20:09:05.271Z

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

yiorgos 2021-03-22T20:09:05.271100Z

ah nice, I’ll have a look

phronmophobic 2021-03-22T20:09:31.271500Z

there's not much code, so you can also just fork it to make it work the way you want.

yiorgos 2021-03-22T20:10:21.271600Z

what if the values are keyword too? do I need to wrap them in double quotes?

phronmophobic 2021-03-22T20:11:18.271800Z

nope!

phronmophobic 2021-03-22T20:11:37.272Z

It uses the same parsing rules as -X (unless those rules were updated since I checked)

phronmophobic 2021-03-22T20:11:52.272200Z

basically, each token is parsed as edn separately

yiorgos 2021-03-22T20:12:13.272400Z

👍 thanks

phronmophobic 2021-03-22T20:13:34.272600Z

actually the example should be:

clj -M:run my.ns/fn cmd :foo '"value"' :bar '"val"'

👍 1
yiorgos 2021-03-22T20:21:42.273Z

Can I pass a single param in the function? or the function should alway accept a map?

chepprey 2021-03-22T20:21:46.273200Z

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.

yiorgos 2021-03-22T20:22:21.273500Z

I am getting this error Key is missing value: foo

yiorgos 2021-03-22T20:22:51.273700Z

clj -X <http://path.to|path.to>.the/fn foo

chepprey 2021-03-22T20:23:54.273900Z

Same with dd and DD. Save a bookmark to those date format code docs.

dpsutton 2021-03-22T20:24:12.274100Z

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.

yiorgos 2021-03-22T20:25:20.274400Z

Oh I see, I have to think of another way then.

yiorgos 2021-03-22T20:25:24.274600Z

Thank you very much

cschep 2021-03-22T22:36:39.275300Z

is there a clever way in clojure to get from “A” to “B” like inc

cschep 2021-03-22T22:36:59.275800Z

in another language i just make the alphabet an array and indexed into it

ghadi 2021-03-22T22:38:48.276Z

user=&gt; (int \A)
65
user=&gt; (inc *1)
66
user=&gt; (char *1)
\B

cschep 2021-03-22T22:39:52.276600Z

oooh interesting

ghadi 2021-03-22T22:40:05.276900Z

@cschep chars can be cast to unicode codepoints, then incremented and cast back

ghadi 2021-03-22T22:40:58.277100Z

user=&gt; (apply str (map (comp char inc int) "hello world"))
"ifmmp!xpsme"

😹 2
sova-soars-the-sora 2021-03-22T22:50:02.277300Z

cat : D

chepprey 2021-03-22T23:03:43.278100Z

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.

cschep 2021-03-22T23:19:32.279200Z

ha, I remembered why I did this now.. I’m generating spreadsheet cell addresses and (inc Z) needs to equal AA

walterl 2021-03-22T23:48:50.280200Z

Then you're talking about a base-26 number system that use letters (no numbers) 🤓

🙂 1