Fork me on GitHub
#beginners
<
2021-03-22
>
cschep04:03:44

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

cschep04:03:52

fantasy-teams is a vector of keywords

cschep04:03:07

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

cschep04:03:15

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

cschep04:03:20

feels like i’m missing something obvious

cschep04:03:34

can you make a map in a function like that?

cschep04:03:09

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

cschep04:03:21

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

cschep04:03:23

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

cschep04:03:26

this works so

cschep04:03:33

must be something wrong with my anonymous function syntax

dharrigan04:03:59

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

dharrigan04:03:07

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

cschep04:03:59

if i do that i just get the values

cschep04:03:06

not maps of team to value

dharrigan04:03:31

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

dharrigan04:03:33

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

cschep04:03:59

mine is just the number

cschep04:03:04

suppose i could tho

dharrigan04:03:43

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

dharrigan04:03:45

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

dharrigan04:03:54

hash-map will create a new map

dharrigan04:03:13

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

cschep04:03:15

yeah i’m reading why it doesn’t work

cschep04:03:20

but that makes sense

dharrigan04:03:30

you're most welcome! 🙂

aratare05:03:30

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

cschep05:03:51

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

cschep05:03:54

which is what you said

Christian11:03:23

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.

seancorfield11:03:13

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

seancorfield11:03:51

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

Christian12:03:26

yes, that did what I wanted. thank you.

Christian12:03:31

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)

andy.fingerhut12:03:36

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

andy.fingerhut12:03:53

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

pez14:03:33

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.

Tim Robinson12:03:21

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

lassemaatta12:03:15

try yyyy instead of YYYY

javahippie12:03:34

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 Robinson12:03:45

😮 thanks, that worked

Tim Robinson13:03:37

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)

ordnungswidrig15:03:24

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:

chepprey20:03:46

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.

chepprey20:03:54

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

Tim Robinson13:03:32

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.

😯 3
chepprey14:03:46

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 Robinson15:03:19

I've submitted a pull request earlier today

💫 3
pinealan14:03:08

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?

sova-soars-the-sora15:03:00

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

sova-soars-the-sora15:03:07

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

chepprey23:03:43

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.

pinealan01:03:54

@U3ES97LAC I need actual websocket for connecting to third-party steamed data

pinealan01:03:15

@UHL84CDTP thanks, haslett looks like a good option

sova-soars-the-sora15:03:05

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-sora15:03:19

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

sova-soars-the-sora15:03:58

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-sora15:03:27

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

noisesmith18:03:24

nb. for is not a loop

Jordan Gibson15:03:07

(take lesson-num lesson-info-seq)

Jordan Gibson15:03:25

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

Jordan Gibson15:03:53

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

sova-soars-the-sora16:03:37

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

🙌 3
sova-soars-the-sora16:03:36

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 Gibson16:03:27

Thanks for the explanation

sova-soars-the-sora16:03:29

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

dharrigan16:03:03

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

dharrigan16:03:31

Here's a reference for further research:

sova-soars-the-sora16:03:33

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

dharrigan16:03:58

mapcat, reduce things like that

sova-soars-the-sora16:03:31

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

sova-soars-the-sora16:03:36

i will try and get it working with mapcat

dharrigan16:03:10

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

dharrigan16:03:22

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

dharrigan16:03:53

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

3
sova-soars-the-sora16:03:35

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

🙌 3
noisesmith18:03:20

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

noisesmith18:03:27

(cat will call seq implicitly)

noisesmith18:03:10

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

😸 3
sova-soars-the-sora22:03:57

how many clojure core functions are named after animals? e.g. cat

blak3mill3r00:03:14

I am pretty sure that clojure.core/cat is the only one "named after an animal" (although it really is not)

blak3mill3r00:03:32

but there are these vaguely zoologically named clojure.core fns '[ descendants ancestors aclone ]

blak3mill3r00:03:13

oh yeah, and lazy-cat

sova-soars-the-sora16:03:38

Flatland... great book by the way.

yes 3
sova-soars-the-sora16:03:54

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

afry18:03:54

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_

yiorgos19:03:05

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"

dpsutton20:03:44

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

yiorgos20:03:42

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

yiorgos20:03:21

I am getting this error Key is missing value: foo

yiorgos20:03:51

clj -X .the/fn foo

dpsutton20:03:12

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.

yiorgos20:03:20

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

yiorgos20:03:24

Thank you very much

phronmophobic20:03:05

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

yiorgos20:03:21

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

phronmophobic20:03:37

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

phronmophobic20:03:52

basically, each token is parsed as edn separately

yiorgos20:03:13

👍 thanks

phronmophobic20:03:34

actually the example should be:

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

👍 3
yiorgos20:03:05

ah nice, I’ll have a look

phronmophobic20:03:31

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

cschep22:03:39

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

cschep22:03:59

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

ghadi22:03:48

user=> (int \A)
65
user=> (inc *1)
66
user=> (char *1)
\B

cschep22:03:52

oooh interesting

ghadi22:03:05

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

ghadi22:03:58

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

😹 6
sova-soars-the-sora22:03:57

how many clojure core functions are named after animals? e.g. cat

cschep23:03:32

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

walterl23:03:50

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

🙂 3