This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-09
Channels
- # aws (4)
- # bangalore-clj (1)
- # beginners (94)
- # boot (19)
- # cider (42)
- # cljs-dev (21)
- # cljsrn (4)
- # clojure (142)
- # clojure-austin (10)
- # clojure-greece (25)
- # clojure-italy (14)
- # clojure-russia (14)
- # clojure-serbia (13)
- # clojure-sg (6)
- # clojure-spec (74)
- # clojure-uk (69)
- # clojurescript (236)
- # consulting (1)
- # cursive (26)
- # data-science (6)
- # datascript (2)
- # datomic (31)
- # editors (5)
- # emacs (24)
- # funcool (5)
- # hoplon (8)
- # jobs-rus (1)
- # luminus (12)
- # lumo (17)
- # off-topic (90)
- # om (45)
- # onyx (5)
- # pedestal (2)
- # powderkeg (12)
- # protorepl (2)
- # re-frame (30)
- # remote-jobs (2)
- # ring-swagger (17)
- # rum (46)
- # slack-help (1)
- # test-check (2)
- # yada (62)
Make (def atom-2 (r/atom (vec (map-indexed vector @atom-1))))
dependent from (def atom-1 (r/atom some-data))
@crvst its not with atoms, but is ensuring consistency via ref
/`dosync` something that would work?
@crvst as michahasmith said, refs can ensure two updates across two states happen together (or both dont happen). If you just want to notify, then you can place a watcher on a state mechanism like an atom, which on change, will take some action.
r/atom is cljs (reagent) there are no refs in cljs
Does defmulit work off the order of the parameters? example (defmulti f [x] x) (defmethod f :inc [x y] y) (f :inc 10)
@grierson the methods have to match the arity of the dispatch function
I’m reading through https://github.com/roman01la/scrum and the defmulti function only takes one parameter but in some demethod take multiple.
ugh - ok that works in cljs because cljs doesn’t have arity errors
it’s not a correct usage though strictly speaking
anyway, to your actual question, the dispatch is an arbitrary function that has access to all args to the method
dispatch is always based on the return value of the dispatch function
@grierson yes, that would be correct
and you could easily write a dispatch that only looks at the third arg
or just generates a random number as the dispatch value, ignoring all args
it’s just a function
small question: is https://github.com/levand/immuconf or some other lib commonly used for configuration? or is there something in core language which is typically used for small-enough projects? for small projects I often just want to load a map from a file, so I am tempted not to use any library at all. Any idiomatic way other than just slurping the file?
Maybe https://github.com/weavejester/environ would suffice?
I don't even need environment vars in some small projects, only loading of a map. Nothing common to use in clojure core libs for that is there? I'll just slurp a file and confirm it is a map
@matan I like your idea of slurping in a map. In the past I've used both immuconf, and simple clojure.edn/read
edn/read is nice for config because you could do sophisticated things like tagged readers:
{:http {:port #env PORT}}
^ with that, you can use different handlers for values tagged with #env
. A simple impl could read from the System/getenv, a more sophisticated one could read config from a location in etcd or consul or zookeeper
(defproject cprop "0.1.10-SNAPSHOT"
:dependencies
[[org.clojure/clojure "1.8.0"]
[boot/core "2.5.1" :scope "provided"]
[adzerk/bootlaces "0.1.13" :scope "test"]
[adzerk/boot-test "1.0.6" :scope "test"]
[tolitius/boot-check "0.1.1" :scope "test"]]
:source-paths
["src"])
dpsutton: Using scopes is the maven/boot way of handling dependencies. None of these dependencies are pulled into your final project
well, “a?” doesn’t start with “?” and it isn’t all caps
you know that defn and def are macros? the usual advice is to only use them at top-level.
@noisesmith what happens with (def x ...) when response-for is applied? does it redefine x with the new arg?
right, def is totally wrong there, but it isn’t causing this particular behavior
the code works (thought not at all optimal thanks to def inside defn), he’s just not providing an input that would return non-nil
let me test
(ins)user=> (response-for "A?")
"Whoa, chill out!"
(ins)user=> (response-for "A")
"Whoa, chill out!"
(ins)user=> (response-for "a?")
nil
(ins)user=> (response-for "?a")
"Sure."
also I needed to add :refer [starts-with?] to the ns form
but yeah, don’t use def like that, use a let block
asking 'cause i know enuff to be dangerous. def/defn - they intern things. but interning is not immutable? do it again, and atuff changes, no?
with concurrency, the version that uses def will do bad things
@mobileink right - def/defn mutate containers that belong to namespaces (or create them if they don’t exist yet)
the tricky bit for me is what def does inside a defn - i guess it does the same thing it does at top level. iow just because it's inside a defn does not mean it's "scoped" - its args will be dealt with just as if it were top level? crap i dunno how to put it. embedded def acts kinda like a fn, you give it an arg you got from the enclosing defn and the intwrned var changes. so every time you call the enclosing fn the def'ed var changes. ?
right
also, trying it in a repl would probably take less time than typing out that question
haha, fair enough
right
You can't dynamically set the name of a def
ed thing though. So something like this won't work: (defn a [x] (def x "nope"))
oh it will - but it always defines x
which probably isn’t what you want
I was thinking of something like (defn a [x] (def (symbol (str x "-part2")) "nope"))
will complain about a list not being a name.
oh, yeah - there’s a way to do that properly but that’s not for beginners (and it’s usually a sign of bad design)
nope, there’s a function that’ll do it
checking…
nope, it doesn’t exist in cljs
I don’t mention it by name because my past experience is that I mention it and some new user is like “oh, OK I can use a namespace like a hashmap with this function…” and they end up in a mess because using namespaces like hashmaps is never a good idea
You can still do it with a macro in cljs, but yeah. Not a good idea to go doing that in anger.
but the macro version is less harmful, because the namespace and name must be known when compiling the code
so outside self-hosting, it’s constrained
@john oh, it’s still possible, the mechanism is different, I’ll message it to you 😄
but how do I define a new variable within a function if I can only use it at top level?
And yes I just realized I used startswith
when I to use ends with
, thanks @noisesmith xD
@vitruvia folks usually put their data in other atoms defined at he top level. Then other callers at whatever level can access the data in the atom. Was there another use-case you were looking for?