Fork me on GitHub
#beginners
<
2019-07-10
>
Kari Pahula05:07:17

I can do (#((get {1 {:foo 2}} %) :foo) 1). But (map #((get {1 {:foo 2}} %) :foo) (1)) gives a ClassCastException. How would I use map, a function like that and a list together?

Benny kach05:07:18

what is your expected output?

Benny kach05:07:18

You need to add ' before the (1)

Benny kach05:07:25

(map #((get {1 {:foo 2}} %) :foo) '(1))

Benny kach05:07:39

or (map #((get {1 {:foo 2}} %) :foo) (list 1))

Kari Pahula06:07:09

I was hoping to get a (2). Using '(1) works in that example but what should I use if the (1) was passed along in a ->> chain?

Crispin06:07:49

doing (1) is going to try to call the function 1 and pass it no arguments. But 1 is not a function. This is the error you get. If you want a list with one item, and that item is 1, as @U8W3NTQSD said, you could use '(1) or you could use (list 1) or you could use a vector [1]. There are many options. Once you have the list '(1), you can pass it though the threading macros like anything else.

Kari Pahula06:07:16

I don't know what to do if (1) is what the last step in my threading macro passed along.

Crispin06:07:35

that should work:

cljs.user> (->> (list 1 2 3)
                (map inc)
                (filter even?))
(2 4)
cljs.user> (->> '(1 2 3)
                (map inc)
                (filter even?))
(2 4)

Kari Pahula06:07:37

I'll see if I can write my code some other way.

jaihindhreddy08:07:23

#((get {1 {:foo 2}} %) :foo) can also be written as #(get-in {1 {:foo 2}} [% :foo]). This has the additional benefit of not throwing when the thing associated with 1 isn't a map.

jakuzure10:07:16

I'm trying to take a list of random kanji and sort them into the different grades as seen here: https://de.wikipedia.org/wiki/J%C5%8Dy%C5%8D-Kanji where I'm stuck is how to go over multiple lists to see which one it's a part of, would appreciate any help!

andy.fingerhut10:07:01

Checking to see if I understand what you are trying to do: You have some input strings, containing perhaps Unicode character sequences, as Java and Clojure on Java support out of the box, and you want to determine for each of those characters, which of several sets it is in?

andy.fingerhut10:07:54

If that is correct, one way to do this would be to create a Clojure map, where each key is a single character, and the associated value for a character C is the identifier of the set it lies within, e.g. perhaps a number identifying the set, or a string that is the name of that set of characters. If you create a Clojure map named m, you can then look up any object x in it, and get the associated value, via one of several possible expressions. One is simply (m x).

🙂 4
andy.fingerhut10:07:59

If that is on the right track at all, then you might have questions about good ways to create such a Clojure map.

jakuzure10:07:35

that's right! I was thinking about more of a brute-force attempt, making maps/sets for each grade and go through them, since making one big map with the corresponding map seems like more initial effort

andy.fingerhut10:07:18

More initial effort in terms of the amount of code to write? Or more compute time for the computer?

andy.fingerhut10:07:06

If you will be categorizing thousands of characters, for example, then the compute time to create the map is probably not going to be terribly significant.

jakuzure10:07:40

more like it seems harder to me to create a map with all the values than creating a map for each grade with just the characters in it

andy.fingerhut10:07:40

Let me show a sample of a bit of Clojure code, using characters from the English alphabet to make the example easier for me 🙂

andy.fingerhut10:07:25

Suppose we want all of the characters in the string "abc" to be set number 1, and all characters in "def" to be set number 2

andy.fingerhut10:07:59

Here is a way to turn the first string into a sequence of pairs, where each pair is one character followed by the number 1:

andy.fingerhut10:07:25

user=> (for [ch "abc"] [ch 1])
([\a 1] [\b 1] [\c 1])

andy.fingerhut10:07:44

That is not a map, but it is easy to turn such a sequence of pairs into a map with into, like this:

andy.fingerhut10:07:57

user=> (into {} (for [ch "abc"] [ch 1]))
{\a 1, \b 1, \c 1}

andy.fingerhut10:07:20

If you have two maps, you can combine them together with merge, like this:

andy.fingerhut10:07:54

user=> (merge (into {} (for [ch "abc"] [ch 1])) (into {} (for [ch "def"] [ch 2])))
{\a 1, \b 1, \c 1, \d 2, \e 2, \f 2}

andy.fingerhut10:07:48

Feel free to try that out in a REPL, with variations of your own, or ask questions if any of those don't make sense.

andy.fingerhut10:07:37

If you get comfortable with that, you can try these out, too:

andy.fingerhut10:07:43

user=> (def char->setid (merge (into {} (for [ch "abc"] [ch 1])) (into {} (for [ch "def"] [ch 2]))))
#'user/char->setid
user=> (map char->setid "deadcafe")
(2 2 1 2 1 1 2 2)

jakuzure13:07:45

I tried doing it in a more scalable way instead of doing it one by one, but I didn't quite get it to work, do I need to use map to get it working?

jakuzure14:07:12

closer, but still not quite there yet...

jakuzure10:07:48

that does seem more manageable 🙂 need to wrap my head around char->setid later, will definitely play around with it in a repl! thanks for the help so far

andy.fingerhut10:07:36

char->setid is just a name of a symbol here. It works just as well if you call it m instead.

andy.fingerhut10:07:42

some people like to throw a few unusual characters into their names of things, and foo->bar is sometimes used by people as a name of a function, or a map, which takes a 'foo' kind of thing, and gives you back a 'bar'. You can leave out the weird characters if they throw you off.

tzzh10:07:55

Hey, I just noticed this behaviour

user=> (clojure.string/split ",2" #",")
["" "2"]
user=> (clojure.string/split "1," #",")
["1"]
whereas I know is some languages (eg Python or JS) you would get ["" "1"] in the second case, so I am wondering what’s the explanation for this behaviour as I find it counter intuitive. More a philosophical question than a technical one (also I am guessing the answer is probably “because that’s the way it is in Java”)

Alex Miller (Clojure team)11:07:03

Because that’s the way it is in Java

đź‘Ť 4
lread11:07:30

user=> (clojure.string/split ",2" #"," -1)
["" "2"]
user=> (clojure.string/split "1," #"," -1)
["1" ""]

đź‘Ť 4
Kari Pahula12:07:37

This works: (map #(get {2 "asdf"} (% :bar)) '({:bar 2})) and I get a ("asdf") from it. I'd like to get something like ({:x "asdf"}). I'm trying to do something like (map #({:x (get {2 "asdf"} (% :bar))}) '({:bar 2})) but I get an error about having a wrong number of args. Could someone please point out my error?

Kari Pahula12:07:24

Well, now. (map (fn [x] {:x (get {2 "asdf"} (x :bar))}) '({:bar 2})) works.

Ahmed Hassan15:07:47

What is this thing #ig/ref inside duct applications? I want to learn how this is defined and reasons behind it.

Ahmed Hassan15:07:06

I mean general ideas.

Ahmed Hassan15:07:24

so #ig/ref is reader conditional?

joelsanchez15:07:37

yes

đź‘Ť 4
joelsanchez15:07:44

a reader conditional is the clj/cljs thing

đź‘Ť 4
Casey15:07:41

is there some core function that is similar to when, but when the arg is true, returns the arg, else will return a default value?

Casey15:07:41

something like (??? nil :default) ;;=> :default and (??? :whatever :default) ;;=> :whatever

Casey15:07:23

oh xD of course

dmaiocchi20:07:11

Which codebase would you recommend to read for it clarity and idiomatic way? J

dmaiocchi20:07:19

Just for curiosity. I mean it would be anything you like it to read from time to time

dmaiocchi20:07:40

For discovering new functions or way of doing

theeternalpulse20:07:04

https://github.com/ring-clojure/ring is often cited as a great idiomatic example. Either following or even setting the stage for clojure idioms.

theeternalpulse20:07:07

perhaps it's extension, compojure as well

clj 4
Savo20:07:04

https://funcool.github.io/clojurescript-unraveled/ Hey, can I ask more experienced programmers here, what do you think of this book, ClojureScript unraveled? I am currently at the beginning of 3.9 section, and it seems to me that so far the explanations are pretty clear, with good examples (so far). P.S. If you know more appropriate channel where I should ask this question - feel free to suggest it, so I know for the next time.

andy.fingerhut20:07:18

beginners is a good channel for such questions. I have no idea how many people you will find who have read that book - first time I have heard of it.

Savo20:07:23

I found it while searching for a beginner Clojure (or clojurescript) book. Most of the books out there assume that the reader is already proficient in at least one language, and too often there are numerous references to that other language when introducing new terms (e.g. "now we'll take a look at . Remember how in Java/Python/C it's like ? Here it's the same, the only difference is "). I'd much more prefer to have a book that doesn't rely on the reader knowing something from other languages (although i know that's rare, since seldom will people choose Clojure(script) as a first language).

Eric Ervin23:07:09

My personal favorite is Getting Clojure, but unlike C for the B and T it costs money.

theeternalpulse01:07:23

CftBaT is great, I generally don't like "funny" programming books, but this one was great. When you step up a bit, I've found https://www.amazon.com/gp/product/1680503006/ref=ppx_yo_dt_b_search_asin_image?ie=UTF8&amp;psc=1 to be great. Very to the point and great to "get" clojure. Reading Applied clojure next, a bit more advanced, and a bit outdated, but I think it's the next step. I also read through a great deal of Joy of clojure, but it's very much a reference book.

Savo17:07:48

Thanks for answers. Yeah, I'll definitely try clojure for the brave and true, since most people are recommending it for beginners. I think that when i tried it some time ago that it had the references to another languages, and sometimes I found that confusing, and maybe i'll do things little slower and take my time until i understand the concepts well enough to move to the next chapter.

theeternalpulse17:07:14

I think that without some context it is fairly difficult to push through a book about functional programming in lisp.