This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-07-10
Channels
- # announcements (3)
- # beginners (67)
- # calva (4)
- # cider (3)
- # clj-kondo (58)
- # cljs-dev (4)
- # clojure (172)
- # clojure-berlin (4)
- # clojure-chicago (8)
- # clojure-europe (4)
- # clojure-greece (8)
- # clojure-italy (12)
- # clojure-nl (4)
- # clojure-spec (7)
- # clojure-uk (77)
- # clojurescript (13)
- # clojutre (16)
- # core-async (10)
- # cursive (3)
- # datomic (29)
- # figwheel-main (27)
- # fulcro (22)
- # garden (3)
- # jobs (2)
- # jobs-discuss (13)
- # juxt (5)
- # leiningen (14)
- # mount (4)
- # off-topic (28)
- # pathom (6)
- # pedestal (8)
- # portland-or (2)
- # re-frame (20)
- # remote-jobs (6)
- # shadow-cljs (13)
- # sql (74)
- # testing (17)
- # tools-deps (1)
- # vim (1)
- # xtdb (1)
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?
what is your expected output?
You need to add ' before the (1)
(map #((get {1 {:foo 2}} %) :foo) '(1))
or (map #((get {1 {:foo 2}} %) :foo) (list 1))
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.
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)
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: 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!
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)
.
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/char->setid
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?
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," #",")
["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”)user=> (clojure.string/split ",2" #"," -1)
["" "2"]
user=> (clojure.string/split "1," #"," -1)
["1" ""]
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?
Well, now. (map (fn [x] {:x (get {2 "asdf"} (x :bar))}) '({:bar 2}))
works.
What is this thing #ig/ref
inside duct
applications? I want to learn how this is defined and reasons behind it.
I mean general ideas.
so #ig/ref
is reader conditional?
sorry it's a "tagged literal" https://clojure.org/reference/reader#tagged_literals
oh right.
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
Just for curiosity. I mean it would be anything you like it to read from time to time
https://github.com/ring-clojure/ring is often cited as a great idiomatic example. Either following or even setting the stage for clojure idioms.
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.
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).
Did you disqualify Clojure for the Brave and True? https://www.braveclojure.com/clojure-for-the-brave-and-true/
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 https://www.amazon.com/gp/product/1680503006/ref=ppx_yo_dt_b_search_asin_image?ie=UTF8&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.