This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-17
Channels
- # adventofcode (96)
- # beginners (49)
- # boot (3)
- # cider (3)
- # cljs-dev (3)
- # clojure (112)
- # clojure-austin (2)
- # clojure-greece (35)
- # clojure-india (2)
- # clojure-sanfrancisco (1)
- # clojure-spec (1)
- # clojure-sweden (1)
- # clojurescript (27)
- # cursive (4)
- # data-science (1)
- # datomic (33)
- # defnpodcast (1)
- # duct (2)
- # editors (1)
- # emacs (4)
- # events (2)
- # figwheel (4)
- # fulcro (4)
- # hoplon (29)
- # instaparse (1)
- # jobs (1)
- # keechma (4)
- # lein-figwheel (2)
- # om (1)
- # parinfer (4)
- # perun (23)
- # reitit (11)
- # shadow-cljs (8)
- # specter (23)
- # uncomplicate (16)
@xiongtx It's mostly to stop you from shooting yourself in the foot. There's some strange cases where you could cause cross-thread mutation with set!
and bindings. I don' have a REPL infront of me at the moment, but I think this would cause the error:
(def ^:dynamic myval nil)
(binding [myval 32]
(set! myval 42)
(future (set! myval 45)))
So that's the gist of it, we bound a dynamic var, set! a value locally, then tried to set it again in another thread. I think that will cause an error and that error occurs when you set an already set val from a different thread than the first set!. The thread that set the value is tracked in TBox.
I see, so it's to handle binding conveyance? B/c AFAICT that's the only way frames are shared b/t threads. Relevant code: https://github.com/clojure/clojure/blob/clojure-1.9.0/src/jvm/clojure/lang/Var.java#L219
But I'm actually not sure why the two set!
s is a problem? Each thread gets its own frames, so they won't interfere w/ each other.
Using TBox also improves performance a bit, since set! is then allocation free. Without TBox you'd have to do an assoc into the contents of the current Frame, and that would require an allocation.
Does anyone else here see a need for something like: https://github.com/aaronc/c-in-clj but targetting both webassembly (to be called from cljs) and cuda (to be calledc from clojure/jcuda)? I want to write high performance numerical code in something that is clojure-like, and be able to invoke it directly from clojure
@qqq Did you look at Ferret? https://github.com/nakkaya/ferret
https://ferret-lang.org/ looks pretty amazing
Is there a way, either using transient! vector or a native java array, to get CONSTANT TIME, not log_32 N time for following op: set n-th element of array/vector/object to :foo
sure, aset
on an object array is constant time. At least as constant as system memory allows (nothing is really constant time in modern hardware).
@seancorfield oh is it already available as an e-book?
Full TOC, some free downloadable chapters/excerpts, Beta e-book. I buy most of my tech books in Beta/Early Access direct from the publishers these days.
going to start in 10-15 minutes https://www.youtube.com/watch?v=cLL_5rETLWY
@igrishaev, we are waiting!
what is this? are you live casting using a library to control a web browser via clojure ?
hi! I'm having trouble with recursion when a function recurs multiple times per call, e.g. when mapping itself over some list I don't know how to avoid blowing the stack for larger inputs here's a (over)simplified example, a function which counts the nodes in a tree: https://gist.github.com/ihabunek/927e8534bef011dd9c14f0284153e678 AFAIK, loop/recur is not viable here any hints would be appreciated.
@igrishaev: how do we join the chat ?
@ihabunek you can definitely use loop/recur here, but I think you will need an accumulator parameter
not too experienced with B/DFS, will need to explore it a bit before i can answer your question
so you could start with an accumulator of 0 and a stack containing only the first node you need to explore
actually, instead of having an accumulator with th count, you could also just flatten the tree to a list
the stack of things left to explore sounds good for this tree problem, and for mazes which don't have loops
there might be more optimized algorithms then though, I am not familiar with graph traversal algos
but I think the basic answer to what you were asking is: “use an extra helper function with an ‘accumulator’ parameter, then use recur/loop”
but personally I would still use the recursive approach (with stack overflow issues fo large inputs) if I know my inputs will be small
here's my actual problem, i cleaned it up a bit https://github.com/ihabunek/aoc2016/blob/master/src/aoc2016/day17.clj#L53 this works well when the limit is under several hundred, but the second part of the assignemnt is to find the longest possible path and that's too much
for the longest possible path yo might want to take a different approach if the graph is big though
and yes http://repl.it is cool 🙂
http://clojuredocs.org is nice for examples though
just for the record: I shouldn’t have used set
in the example I gave you; at first sight it makes sense, but thinking a bit more about it it doesn’t seem to make sense at all
well, it makes some sense, but if you wanted it to behave well on DAGs the function would need to have extra logic to handle already visited nodes
@ihabunek I was trying to do a DFS in clojure recently and had a similar problem; it didn't seem natural to recur in tail call position. I found an answer in Stuart Sierra's dependency library, it's a little bit terse but way more elegant than anything I came up with.
(defn- transitive
"Recursively expands the set of dependency relationships starting
at (get neighbors x), for each x in node-set"
[neighbors node-set]
(loop [unexpanded (mapcat neighbors node-set)
expanded #{}]
(if-let [[node & more] (seq unexpanded)]
(if (contains? expanded node)
(recur more expanded)
(recur (concat more (neighbors node))
(conj expanded node)))
expanded)))
Oh I need to get that book, was tempted recently because the MIT Press had a 40% off black friday sale
Please, someone could help me with this:
(->> (iota/seq "war-and-peace-full-book.txt")
(r/filter identity)
(r/map (fn [line]
(re-seq #"\w+" line)))
(r/fold (fn
([] 0)
([a line]
(+ a (count line))))))
I’m trying this to count words in a file with parallel, that’s Ok with small files but with big txt files. I’m having this problem:
UnsupportedOperationException count not supported on this type: Long clojure.lang.RT.countFrom (RT.java:664)
@jcthalys r/fold takes an optional combining function which combines the results of multiple folds; your fold returns a number, if the combine function is not provided, it tries to use your reduce function
@jcthalys I think you can just tell it that your combine function is + so change (r/fold (fn ...))
into (r/fold + (fn ...))
and that should solve it
@jcthalys to be totally clear - it's trying to pass a number to your function because you told it that function could be used to combine the result of multiple folds
also, when you fix it by adding +
as an arg, your function no longer needs a 0 arity to be defined - only the combining function is called with 0 args
Is there a way to create an 'array' such that: 1. I can use aset on it 2. it works in cljc (i.e. both clojure and cljs) 3. it lets me store arbitrary object (i.e. not just float or int) I'm basically looking for 'array of references' Pre-emptive: why aren't you using a transient! vector? I want real O(1) aset, not O(log_32 n) assoc.
#?(:clj (make-array Object 100) :cljs (make-array js/Object 100))
oh, that's portable, nice
in plumatic/schema
, is there an option that would allow you to create a condition for a value based on its key in a map? from my understanding it seems that s/conditional
can only validate the value in which its placed
consider
[{:something1 {:foo 1 :bar 2}}
{:something2 {:span "2" :potato "3"}}]
then if the key is :something1
i'd like to use a certain schema to validate {:foo 1 :bar 2}
but if the key is :something2
i'd like to use another schema to validate {:span "2" :potato "3"}
.you can make an s/conditional that looks into the keys
that is, a conditional that says apply schema1 if :something1 is present, otherwise schema2 if :something2 is present, otherwise some default
then schema1 and schema2 have the apropriate maps under those keys
You could also use keys with or, something like (s/keys :req-un [(or :something1 :something2)])
thanks guys, i ended up using @noisesmith's tip cuz i'm in a rush here
@gklijs is there a keys function in schema? i couldn't find in the docs for the core namespace
I think @gklijs was thinking of spec