Fork me on GitHub
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?


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.


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.


#((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.


I'm trying to take a list of random kanji and sort them into the different grades as seen here: where I'm stuck is how to go over multiple lists to see which one it's a part of, would appreciate any help!


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?


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

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


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


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


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.


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


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


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


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:


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


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


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


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


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}


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.


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


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


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?


closer, but still not quite there yet...


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


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


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.


Hey, I just noticed this behaviour

user=> (clojure.string/split ",2" #",")
["" "2"]
user=> (clojure.string/split "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

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?


a reader conditional is the clj/cljs thing

👍 4

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?


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


oh xD of course


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


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


For discovering new functions or way of doing

theeternalpulse20:07:04 is often cited as a great idiomatic example. Either following or even setting the stage for clojure idioms.


perhaps it's extension, compojure as well

clj 4
Savo20:07:04 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.


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.


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.


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;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.


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.


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