This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-08
Channels
- # aws (9)
- # beginners (69)
- # boot (14)
- # cider (26)
- # cljs-dev (56)
- # cljsrn (9)
- # clojars (4)
- # clojure (229)
- # clojure-brasil (1)
- # clojure-france (11)
- # clojure-greece (2)
- # clojure-italy (4)
- # clojure-mke (6)
- # clojure-serbia (6)
- # clojure-spec (83)
- # clojure-uk (38)
- # clojurescript (171)
- # core-async (3)
- # cursive (11)
- # data-science (11)
- # datomic (27)
- # emacs (113)
- # funcool (6)
- # hoplon (4)
- # jobs (1)
- # luminus (13)
- # lumo (44)
- # off-topic (148)
- # onyx (5)
- # overtone (1)
- # pedestal (4)
- # powderkeg (1)
- # proton (2)
- # re-frame (150)
- # reagent (16)
- # ring-swagger (43)
- # spacemacs (4)
- # specter (36)
- # vim (4)
- # yada (10)
Hello, how efficiently update the table-like atomic data like this?
(def table [["Letter" "Number"]
["Aleph" 1]
["Bet" 1]
["Gimel" 2]
["Dalet" 3]
["He" 5]
["Vav" 8]
["Zayin" 13]
["Het" 21]])
@crvst Since the data is a vector of vectors you'd have to access the element by indices
Or if the table is contained in an atom that you want to swap:
(swap! table-atom (fn [table] (assoc-in table [1 0] "No longer called Aleph")))
@curlyfry Thank you. What is the idiomatic way to keep meta-data like vector indices within React component? Or is there any way to get bounded data?
you might even get away with (swap! table-atom assoc-in [1 0] "No longer called Aleph")
@crvst I'm not sure if I know exactly what you want to do, but maybe map-indexed
works?
@vitruvia (phrase)
calls phrase with no args, as if it were a function
the two errors here are using a vector containing a string as if it were a string, and calling a vector as if it were a function
@vitruvia if you are familiar with algol family languages like c/js/java/c# etc. calling (f) in clojure is like calling f() in one of those - it’s just moving the paren to the left
I'm not =( Actually I've solved all the exercises on the koans, but now I'm trying to solve stuff on exercism and I found I've forgotten much of the syntax
@vitruvia OK - just remember that (f) calls f - parens are not just for grouping, and in code that gets compiled, it needs a function
(or a method etc.)
you might see something like '(1 2 3)
in examples, the '
prevents evaluation so returns a literal list of the form
Guys, how to swap nested collection? Something like this #(swap! atom-coll assoc-in [y x] value)
But the vectors inside atom is in a lazy-seq, so before assoc-in
I need to get the vector istelf
lists and vectors are both associative via their index. So y
above, I believe, would index first into your list. x
would then index into one of the vectors.
Now I have the error for this: No protocol method IAssociative.-assoc defined for type cljs.core/LazySeq
assoc-in does not work with non-associative collections
vectors are associative, lazy-seqs are not
Any suggestions how how i can pass the cond condtionals in at run time?
(defn describe-number [n]
(cond-> []
(odd? n) (conj "odd")
(even? n) (conj "even")
(zero? n) (conj "zero")
(pos? n) (conj "positive")))
to something like
(defn describe-number [n conditionals]
(cond-> [] conditionals))
(describe-number 5 ...)
I want to be able to change them at run time. I feel like could use macros todo this, but i’m woundering if there isn’t a simpler way to achieve the sam functionality?sounds like something you could do with a reduce over a hash-map mapping conditional to data
something like (reduce (fn [description [pred? s]] (if (pred? o) (conj description s) description)) [] {odd? "odd" …})
where o is your n
But cond->
is a macro, so no. The form you give it must adhere to the defined structure, at compile time, not runtime.
Oh, I didn't see what you wrote below that. Yeah, you could probably create a macro that does that. Sounds hairy though.
@john I was thinking this might be the case. I starting to wonder if its wiser to not go looking farther then reduce for many tasks. Macros seems to introduce a lot of the same complaints i have about other langauges in terms of their inflexability, or i’m thinking about this wrong?
I suppose i should go look at the implementation of cond->
Right, macros have compile time / runtime constraints common to complaints found in other languages. We're only spoiled about it though because almost everything can be passed around transparently in Clojure. And the downsides of macros are offset by the fact that they can produce entirely new syntaxes that aren't possible in some other languages.
In my experience, macros are mostly for adding sugar to what already works. Build something that works first, then use macros to sugar over part of that, only when necessary. Often times you don't even need them.
otoh, the nice thing about sugar is the sweetness. describe-nbr-> could be a variant of cond-> . it's all about trade-offs - sometimes a macro can make code much more legible, even if strictly speaking you could get away without it.
the form on line 5 is a no-op
it can’t possibly do anything
perhaps you want to test two conditions in one form? cond is good for that
you test that, then return a value that nothing consumes
“Sure” is returned and thrown away, the form might as well not exist
by putting it in the last form in the function
eg. with cond you can test multiple conditions, and have a separate return value for each one
so the only way to make a function with multiple conditions is by using a switch case statement? (in this case cond)
@john common advice is to not use macros unless you cannot avoid it, or something like that. but i think better advice is, to paraphrase Samuel Beckett, "Never use a macro unless it is absolutely necessary or convenient to do so."
@vitruvia right case
works if you can dispatch on a single value, cond
works if you have a different test for each result, and nested if
technically works but that’s what cond
does anyway so just use cond
also, in case you don’t know yet, you can find documentation for all these nice functions and macros in the repl
> (doc cond)
-------------------------
clojure.core/cond
([& clauses])
Macro
Takes a set of test/expr pairs. It evaluates each test one at a
time. If a test returns logical true, cond evaluates and returns
the value of the corresponding expr and doesn't evaluate any of the
other tests or exprs. (cond) returns nil.
nil
@mobileink agreed. sweetness in moderation. 🙂
the black arts are strong magic - very useful but very dangerous in the wrong hands. 😉
http://www.arts-stew.com/wp-content/uploads/2013/02/Jackie-Gleason-How-Sweet-It-Is-400x237.jpg
lein test
works, but lein test-refresh
fails to find an import (that it doesn't need for tests)