This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-11
Channels
- # aws (15)
- # beginners (55)
- # boot (116)
- # bristol-clojurians (2)
- # cider (4)
- # cljs-dev (439)
- # cljsrn (14)
- # clojure (135)
- # clojure-argentina (3)
- # clojure-czech (4)
- # clojure-italy (60)
- # clojure-russia (1)
- # clojure-spec (48)
- # clojure-uk (42)
- # clojurescript (170)
- # cloverage (11)
- # core-async (19)
- # cursive (13)
- # datomic (48)
- # emacs (2)
- # graphql (3)
- # hoplon (8)
- # jobs (1)
- # jobs-discuss (5)
- # klipse (11)
- # luminus (5)
- # lumo (5)
- # mount (48)
- # off-topic (96)
- # om (17)
- # onyx (14)
- # parinfer (30)
- # protorepl (1)
- # re-frame (90)
- # reagent (2)
- # remote-jobs (1)
- # spacemacs (12)
- # specter (20)
- # uncomplicate (1)
- # untangled (65)
- # vim (2)
- # yada (8)
Any orthodox way to create a map whose keyed values are vectors, adding elements to the vectors as you find objects containing their keys? Something like building page lists for entries in a book's index, finding topic mentions as you read from front to back. In Common Lisp, I'd just do (push item (gethash key table)). I gather this isn't allowed in Clojure, because it would mean bashing the map (as a whole — not the individual vectors). So, what does a Clojure programmer do?
like if you start with map {:foo [1 2 3]}, you want to push 4 to the vector value? so it becomes {:foo [1 2 3 4]} ?
you can use the update
function to update map values.
so something like this (update m :foo conj 4)
if we use m from my example.
but you have to make sure the key and values exists before you update.
otherwise it tries to conj a 4 onto a nil.
if the key-value doesn't exist in the map
@scallions look at fnil
to get around that /cc @bschrag
oh, that's something I haven't considered. very elegant
(update m :foo (fnil conj []) 4)
-- that will handle (:foo m)
being nil
(update {} :foo (fnil conj []) 4)
=> {:foo [4]}
update
is new-ish (Clojure 1.7?) and works for a single key. Prior to that being added, you had to do (update-in m [:my-key] f args)
-- now you can do (update m :my-key f args)
So, update-in
for nested keys, update
for a top-level key. Like assoc-in
and assoc
.
(and, yeah, added in Clojure 1.7... just checked via (clojure.repl/source update)
)
How's your Clojure learning going so far?
@lilactown You can't
👍 as I'm reading more about the resources folder, i'm understanding that it's meant as a place for compile-time resources
@lilactown They aren't even real files when you bundle up your application in a jar. They're URLs. You shouldn't call io/file
on a resources either. Works during dev time but not at runtime
@bschrag also if all the input is available at one time you can use group-by
okay... as a dev coming from Python to Clojure (❤️ ) I feel really handicapped by my lack of understanding of Java for a lot of basic operational tasks. Things like unzipping files, etc, are all pretty foreign to me and a lot of that work gets farmed out to Java interop. Anytime I do an operation and a Java object is returned (i.e., file object), it's not obvious to me what the API for that object is. Is this one of those things you just have to look up one at a time, is there a fundamental set of Java knowledge one needs, or is there (hopefully) a systematic way of approaching situations where Java interop is required?
hahahaha :spock-hand: 😎 :spock-hand:
the straightforward thing that works for me 95% of the time is to call class
on the object, then use google to find the javadoc for that class
Java isn't too complicated of a language, you shouldn't fear it. I think Clojure is more powerful when seen as an extension to Java.
Having said that, have a look at https://github.com/Raynes/fs
I think if you have the basic Java constructs and conventions down, it’s not too hard to follow the online Java class docs. Maybe something like this would help? http://knuth.luther.edu/~bmiller/Java4Python/index.html
(you may need to (require '[clojure.java.javadoc :refer (javadoc)])
depending on your REPL/context)
beautiful, thanks @manutter51 @ghadi, great places to get started
Is there a concise way of destructuring two digit ints into individual numbers without converting them to chars first? thx!
concise usually means inline in a let binding and then it gets really cryptic. I'll bet you can spare a stackframe and make the name of the function descriptive
@ghadi No reason really, just keep having situations where I have to convert a number to str, destructure and then do js/parseInt
on it. Seems like I’m missing something. Pretty new to Clojure so thought I’d check. Is that what most people do?