workspace
stringclasses
4 values
channel
stringclasses
4 values
text
stringlengths
1
3.93k
ts
stringlengths
26
26
user
stringlengths
2
11
clojurians
clojure
(it's almost 5am here, I'm trying to decide to sleep or to wait)
2017-12-17T06:53:18.000065
Berry
clojurians
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.
2017-12-17T07:01:09.000100
Elizbeth
clojurians
clojure
<@Verna>: how do we join the chat ?
2017-12-17T07:02:05.000067
Berry
clojurians
clojure
<@Elizbeth> you can definitely use loop/recur here, but I think you will need an accumulator parameter
2017-12-17T07:20:49.000006
Lucio
clojurians
clojure
ok, i'll give it a shot
2017-12-17T07:21:35.000023
Elizbeth
clojurians
clojure
it will be less sexy than the way you expressed it with recursion though…
2017-12-17T07:21:59.000026
Lucio
clojurians
clojure
as in, it will be less explicit as to what it is doing
2017-12-17T07:22:11.000058
Lucio
clojurians
clojure
but it should get rid of the stack overflow problem if you do it properly
2017-12-17T07:22:24.000027
Lucio
clojurians
clojure
the actual problem is navigating a branching maze, which is similar to the example
2017-12-17T07:22:39.000030
Elizbeth
clojurians
clojure
what about using a stack?
2017-12-17T07:23:01.000061
Lucio
clojurians
clojure
use two exta parameters, an accumulator (integer accumulating the count)
2017-12-17T07:23:11.000044
Lucio
clojurians
clojure
and a stack of things left to explore
2017-12-17T07:23:16.000027
Lucio
clojurians
clojure
basically modelling a breadth first / depth first traversal
2017-12-17T07:23:31.000049
Lucio
clojurians
clojure
there might be std lib functions for that actually, not sure…
2017-12-17T07:23:39.000058
Lucio
clojurians
clojure
not too experienced with B/DFS, will need to explore it a bit before i can answer your question
2017-12-17T07:24:01.000027
Elizbeth
clojurians
clojure
so you could start with an accumulator of 0 and a stack containing only the first node you need to explore
2017-12-17T07:24:03.000069
Lucio
clojurians
clojure
e.g. `[:a]`
2017-12-17T07:24:07.000039
Lucio
clojurians
clojure
stack would contain the ones i have left to explore?
2017-12-17T07:24:28.000074
Elizbeth
clojurians
clojure
as opposed to the ones i have already explored
2017-12-17T07:24:49.000051
Elizbeth
clojurians
clojure
yeah, left to explore
2017-12-17T07:24:57.000039
Lucio
clojurians
clojure
actually, instead of having an accumulator with th count, you could also just flatten the tree to a list
2017-12-17T07:25:10.000004
Lucio
clojurians
clojure
then count the numbr of elements in that list
2017-12-17T07:25:19.000086
Lucio
clojurians
clojure
hang on, I’ll write you an example
2017-12-17T07:25:29.000068
Lucio
clojurians
clojure
ok, the tree is a stupid example :slightly_smiling_face:
2017-12-17T07:25:30.000019
Elizbeth
clojurians
clojure
the stack of things left to explore sounds good for this tree problem, and for mazes which don't have loops
2017-12-17T07:26:01.000059
Elizbeth
clojurians
clojure
if i have loops i guess i'd have to store places already visited
2017-12-17T07:26:13.000081
Elizbeth
clojurians
clojure
yep, if you have loops it’s different :disappointed:
2017-12-17T07:26:13.000090
Lucio
clojurians
clojure
yeah
2017-12-17T07:26:18.000036
Lucio
clojurians
clojure
there might be more optimized algorithms then though, I am not familiar with graph traversal algos
2017-12-17T07:26:40.000031
Lucio
clojurians
clojure
if you were really worried about performance I am sure optimized versions exist
2017-12-17T07:27:05.000026
Lucio
clojurians
clojure
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”
2017-12-17T07:28:05.000054
Lucio
clojurians
clojure
i'm looking at the actual problem i had :smile:
2017-12-17T07:29:55.000075
Elizbeth
clojurians
clojure
trying to apply it
2017-12-17T07:30:00.000075
Elizbeth
clojurians
clojure
but personally I would still use the recursive approach (with stack overflow issues fo large inputs) if I know my inputs will be small
2017-12-17T07:30:10.000081
Lucio
clojurians
clojure
because it’s usually more expressive
2017-12-17T07:30:16.000098
Lucio
clojurians
clojure
at least to me
2017-12-17T07:30:20.000065
Lucio
clojurians
clojure
i like the recursion too
2017-12-17T07:30:23.000061
Elizbeth
clojurians
clojure
i was under the impression that it's not possible to (map recur ...)
2017-12-17T07:30:38.000019
Elizbeth
clojurians
clojure
yeah it’s not possible to do `(map (recur` directly
2017-12-17T07:30:55.000066
Lucio
clojurians
clojure
hang on, I’m writing you an example :slightly_smiling_face:
2017-12-17T07:31:36.000073
Lucio
clojurians
clojure
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
2017-12-17T07:39:37.000019
Elizbeth
clojurians
clojure
<@Elizbeth> <https://repl.it/repls/SaddlebrownLiquidMontanoceratops>
2017-12-17T07:40:19.000090
Lucio
clojurians
clojure
wow cool site
2017-12-17T07:41:07.000002
Elizbeth
clojurians
clojure
for the longest possible path yo might want to take a different approach if the graph is big though
2017-12-17T07:41:35.000026
Lucio
clojurians
clojure
yep
2017-12-17T07:41:58.000044
Elizbeth
clojurians
clojure
and yes <http://repl.it|repl.it> is cool :slightly_smiling_face:
2017-12-17T07:42:34.000013
Lucio
clojurians
clojure
interesting approach, thanks for that example
2017-12-17T07:42:56.000027
Elizbeth
clojurians
clojure
reducing it with set/union, makes sense
2017-12-17T07:44:03.000071
Elizbeth
clojurians
clojure
yeah; actually now that I think of it it’s unecessary if you have a tree structure
2017-12-17T07:44:44.000034
Lucio
clojurians
clojure
since the same node will never appear twice
2017-12-17T07:44:52.000022
Lucio
clojurians
clojure
so you have recur without loop, didn't know that was a thing
2017-12-17T07:45:02.000013
Elizbeth
clojurians
clojure
but if you had a DAG (directed acyclic graph), then that approach would still work
2017-12-17T07:45:08.000094
Lucio
clojurians
clojure
oh hang on
2017-12-17T07:45:27.000071
Lucio
clojurians
clojure
no it wouldn’t it would overcount some nodes…
2017-12-17T07:45:34.000044
Lucio
clojurians
clojure
yes you can recur on the whole function
2017-12-17T07:45:46.000052
Lucio
clojurians
clojure
I think it the doc it says that recur “recurs to a recursion point”
2017-12-17T07:46:35.000004
Lucio
clojurians
clojure
loop introduces a recursion point
2017-12-17T07:46:40.000017
Lucio
clojurians
clojure
but the function itself is also a recursion point
2017-12-17T07:46:49.000049
Lucio
clojurians
clojure
docs can sometimes be a bit terse
2017-12-17T07:46:51.000002
Elizbeth
clojurians
clojure
yes…
2017-12-17T07:46:55.000036
Lucio
clojurians
clojure
makes sense
2017-12-17T07:46:57.000080
Elizbeth
clojurians
clojure
<http://clojuredocs.org|clojuredocs.org> is nice for examples though
2017-12-17T07:47:10.000059
Elizbeth
clojurians
clojure
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
2017-12-17T07:49:34.000076
Lucio
clojurians
clojure
i usually use a list to preserve order
2017-12-17T07:50:00.000052
Elizbeth
clojurians
clojure
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
2017-12-17T07:50:03.000073
Lucio
clojurians
clojure
thanks, i think you've steered me in the right direction, i'll play around with it
2017-12-17T07:51:20.000077
Elizbeth
clojurians
clojure
good luck!
2017-12-17T07:51:30.000056
Lucio
clojurians
clojure
next year maybe i'll try racket to get some proper TCO :smile:
2017-12-17T07:52:09.000027
Elizbeth
clojurians
clojure
Please, someone could help me with this: ``` (-&gt;&gt; (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)`
2017-12-17T08:43:43.000073
Annemarie
clojurians
clojure
<@Berry> to join the chat, you need to be logged on Youtube using your google account.
2017-12-17T08:58:43.000096
Verna
clojurians
clojure
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).
2017-12-17T09:53:03.000033
Sandy
clojurians
clojure
<@Annemarie> 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
2017-12-17T11:01:34.000086
Margaret
clojurians
clojure
<@Annemarie> 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
2017-12-17T11:02:12.000077
Margaret
clojurians
clojure
<@Annemarie> 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
2017-12-17T11:03:24.000004
Margaret
clojurians
clojure
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
2017-12-17T11:09:55.000057
Margaret
clojurians
clojure
<@Alla> It turned out that I needed to make httpkit understand ring-async
2017-12-17T11:29:21.000127
Michelina
clojurians
clojure
Which requires a tiny wrapper
2017-12-17T11:29:49.000084
Michelina
clojurians
clojure
Hi, Is the Untangled Framework alive or abandoned?
2017-12-17T13:06:55.000066
Arnetta
clojurians
clojure
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.
2017-12-17T13:40:54.000038
Berry
clojurians
clojure
`#?(:clj (make-array Object 100) :cljs (make-array js/Object 100))`
2017-12-17T13:43:15.000005
Margaret
clojurians
clojure
`object-array`
2017-12-17T13:43:26.000004
Kareen
clojurians
clojure
oh, that's portable, nice
2017-12-17T13:43:50.000019
Margaret
clojurians
clojure
nice; thanks!
2017-12-17T13:44:34.000007
Berry
clojurians
clojure
<@Michelina> would be nice If the tiny wrappers were available for all. Needed for Aleph &amp; Immutant too. Best would be do PRs to all the servers to have the async? option
2017-12-17T16:38:57.000064
Alla
clojurians
clojure
Agreed. I may take a crack at the advice for httpkit offered here: <https://www.booleanknot.com/blog/2016/07/15/asynchronous-ring.html>
2017-12-17T16:40:07.000111
Michelina
clojurians
clojure
(that's also where i got the tiny wrapper, `ring-&gt;httpkit`)
2017-12-17T16:40:39.000016
Michelina
clojurians
clojure
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
2017-12-17T16:51:35.000035
Ahmad
clojurians
clojure
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"}`.
2017-12-17T16:53:52.000090
Ahmad
clojurians
clojure
you can make an s/conditional that looks into the keys
2017-12-17T17:08:54.000102
Margaret
clojurians
clojure
that is, a conditional that says apply schema1 if :something1 is present, otherwise schema2 if :something2 is present, otherwise some default
2017-12-17T17:09:35.000090
Margaret
clojurians
clojure
then schema1 and schema2 have the apropriate maps under those keys
2017-12-17T17:09:50.000173
Margaret
clojurians
clojure
You could also use keys with or, something like (s/keys :req-un [(or :something1 :something2)])
2017-12-17T17:47:55.000129
Daine
clojurians
clojure
thanks guys, i ended up using <@Margaret>'s tip cuz i'm in a rush here
2017-12-17T17:58:58.000014
Ahmad
clojurians
clojure
<@Daine> is there a keys function in schema? i couldn't find in the docs for the core namespace
2017-12-17T18:01:19.000014
Ahmad
clojurians
clojure
I think <@Daine> was thinking of spec
2017-12-17T18:15:20.000056
Margaret
clojurians
clojure
`(#(reverse (apply vector %&amp;)) :a :b :c)` is there a way to rewrite the function? (the part before the :a :stuck_out_tongue: :c)
2017-12-17T20:44:48.000053
Berry
clojurians
clojure
`(comp reverse vector)`
2017-12-17T20:51:05.000010
Toi
clojurians
clojure
actually, I think ikt's just #(reverse %&amp;) :slightly_smiling_face:
2017-12-17T20:51:30.000024
Berry
clojurians
clojure
sure
2017-12-17T20:51:59.000135
Toi
clojurians
clojure
``` (defmacro def-alias [lst] `(do ~(for [[k v] lst] `(def ~k ~v)))) (macroexpand-1 '(def-alias [[rev reverse] [ki keep-indexed] [pi persistent!] [ti transient] [gi get-in]])) (comment (do ((def rev reverse) (def ki keep-indexed) (def pi persistent!) (def ti transient) (def gi get-in)))) ``` what am I doing wrong in this macro?
2017-12-17T21:11:01.000200
Berry