This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-26
Channels
- # aws (7)
- # beginners (109)
- # boot (5)
- # carry (2)
- # cider (25)
- # clara (6)
- # cljs-dev (86)
- # cljs-experience (19)
- # cljsrn (1)
- # clojure (183)
- # clojure-dev (7)
- # clojure-dusseldorf (7)
- # clojure-gamedev (2)
- # clojure-greece (32)
- # clojure-italy (2)
- # clojure-norway (1)
- # clojure-russia (228)
- # clojure-sg (3)
- # clojure-spec (38)
- # clojure-uk (104)
- # clojurebridge (1)
- # clojurescript (29)
- # community-development (9)
- # core-async (118)
- # core-matrix (20)
- # cursive (5)
- # datomic (140)
- # emacs (25)
- # figwheel (1)
- # hoplon (21)
- # jobs (4)
- # lein-figwheel (2)
- # luminus (10)
- # lumo (35)
- # off-topic (137)
- # om (31)
- # onyx (62)
- # pedestal (6)
- # reagent (25)
- # remote-jobs (1)
- # ring-swagger (11)
- # spacemacs (2)
- # test-check (17)
- # uncomplicate (10)
- # unrepl (1)
- # untangled (20)
- # vim (4)
- # yada (3)
Would (remove nil?
work? Perhaps drop the recursion into a loop further down the function, rather than recurring back to the top level.
You snippet is broken. You maybe want to replace line 6 with (list (select (first a-seq)))
?
One solution to your problem is to not cons nil
to the result sequence
(defn my-filter [pred? a-seq]
(let [select (fn [x] (if (pred? x)
x
))]
(if (<= (count a-seq) 1)
(list (select (first a-seq)))
(if-let [e (select (first a-seq))]
(cons e (my-filter pred? (rest a-seq)))
(my-filter pred? (rest a-seq))))))
how might i go about getting more detailed information from an exception? for example, the following isn't very helpful:
(post ""
{:form-params {:query "<query model=genomic view=Gene.organism.name></query>"}})
ExceptionInfo clj-http: status 400 slingshot.support/stack-trace (support.clj:201)
i know the server is sending back a reason why it failed which i can see in my browser but not in the repl
@joshkh have you tried evaling *e
in the repl immediately after the exception (the repl connected to the server)
@psalaberria002 personally, I wouldn't try and do the condition in the threading macro. I would put the intermediate result in a var in the let.
@john Thanks, but I can't use acc
or remove
. I mean I could but I think this is meant to be solved with a recursion using cons
vitruvia: if the current element satisfies the predicate, you want to return the element cons to the rest of the filter output, otherwise you just want to return the rest of the filter output
@dpsutton Sort of... I do get the idea, I just don't know how to make it into clojure code using only cons
and conditionals.
(defun my-filter [pred? l]
(when (seq l)
(let [e (first l)]
(if (pred? e)
(cons e (my-filter pred? (rest l)))
(my-filter pred? (rest l))))))
"when there's more elements of the list, if the predicate applies, return this element and whatever else the function decides, or else just return the rest of the function
but if you check out the source,
([pred coll]
(lazy-seq
(when-let [s (seq coll)]
(if (chunked-seq? s)
(let [c (chunk-first s)
size (count c)
b (chunk-buffer size)]
(dotimes [i size]
(let [v (.nth c i)]
(when (pred v)
(chunk-append b v))))
(chunk-cons (chunk b) (filter pred (chunk-rest s))))
(let [f (first s) r (rest s)]
(if (pred f)
(cons f (filter pred r))
(filter pred r))))))))
ignore the then block of the if (chunked-seq? s)
and you'll see that they have done exactly what we did except with a lazy sequence
(when-let [s (seq col)]
(let [f (first s) r (rest s)]
(if (pred f)
(cons f (filter pred r))
(filter pred r)))))
but why doesn't the source code return an error when the seq is a singleton or empty? in this case you would call pred?
on nil
and it could return an error. For example even? nil
returns an error
What do you mean? the element e
is only bound to the first element of the seq, I still need to check whether it fulfills pred?
. That is what select
is for.
and in fact you really should only do it once, both since its extra work for no gain and someone could give you a function with side-effects even though you might specify they should give only pure functions
(but I didn’t use ->> here because it didn’t actually apply to this code)
I'm sure this is a silly question, but I'm having trouble figuring it out. I have two sequences of data, like this:
(55.9 55.0 54.4 53.8 53.6 53.5 53.0 52.9)
(3459 27554 8433 4177 22351 13752 3228 26071)
The data is related, so in this example the first item in the first sequence is a :rate and the first item in the second sequence is a :count. I'm trying to combine these into a sequence of maps, like this:
({:rate 55.9 :count 3459} {:rate 55.0 :count 27554} etc etc)
For some reason I'm blanking on how to do it, can anyone point me in the right direction?
(map #(hash-map :rate %1 :count %2) rates counts)
My brain must be fried, so simple. Thanks a bunch!
I have a sequence that I'd like to put onto a core.async channel, one element at a time. I'm sure there's an idiomatic way to do this, but I haven't been able to figure one out. I've tried onto-chan
, but if the sequence contains more than 1024 elements (the number of puts that can be cached), I get an error due to exceeding the put cache size. Any ideas or pointers to references most welcome!
Maybe you want a channel with a buffer large enough to contain your seq?
I suppose I could partition the sequence into chunks, and put the chunks on using onto-chan
Or you want a go-loop that will park its puts instead of barfing when the channel buffer is full
@donaldball yeah, I've thought using a buffered channel: I haven't used them much (or, at all, actually) so far. I've been trying to stay within the limits of what I currently know.
@donaldball thanks for confirming that I've likely been thinking in right way, just not getting the implementation right
to-chan
might also be more what you’re looking for
came across that one, too. I've got an existing channel I'm working with, otherwise I think that would be a similarly good fit
Maybe you need to start your reader before you start your writer?
Why does (map key {:a 1 :b 2}) => (:a :b)
but (key {:a 1})
throws a ClassCastException? Interestingly, (apply key {:a 1}) => :a
apply key works in that case because it turns your hash-map into a list of hash-entries
and key works on a hash-entry and luckily in that example there’s only one so key works
user> (source key)
(defn key
"Returns the key of the map entry."
{:added "1.0"
:static true}
[^java.util.Map$Entry e]
(. e (getKey)))
nil
@ssansovich when you map on a hash-map, your function gets called on each hash-entry individually
Thanks @noisesmith but why does (key {:a 1}) throw an exception? That’s what I’m missing. Maybe I’m being dense
(key {:a 1})
ClassCastException clojure.lang.PersistentArrayMap cannot be cast to java.util.Map$Entry clojure.core/key (core.clj:1518)
@ssansovich a hash-map is not a hash-entry
it’s a collection of hash-entries
the error explains it. it's a persistent array map. the type hint in key
dictates it must be a MapEntry
thanks @noisesmith and @dpsutton I’ll read up on the difference
@ssansovich remember that a hash-map can have 0 or more keys in it - it doesn’t make sense to ask what the key of {} is, or what they key of {:a 0 :b 1 :c 2} is
i see
the map is a collection of map entries. Just like [1 2 3]
is a collection of integers. Key works on the entry, not the collection of the entries the same way + works on numbers not the collection of them. however you can map + over a collection and apply it to a collection
makes sense. thanks very much!
I have quick question: what is the idiomatic way to map a function across two uneven sequences? I found some references to calling map with more than one collection as in (map + [1 2] [3 4]) => (4 6)
, but it looks like it halts processing based on the shortest input
never mind! in the process of typing up my question I must have stumbled on the correct wording to please google and found this stack overflow post: https://stackoverflow.com/questions/18940629/using-map-with-different-sized-collections-in-clojure
in core.async is there a way to subscribe to everything (e.g. a stats gatherer) or do I need mult the publications channel ?
gmercer: yeah, a mult on the same channel that you make the pub from is the simple way to do it
@noisesmith cheers - the stats gathering should pretty lightweight and is very unlikely to be the slowest consumer
I have another slightly related question .. I intend to have the stats running a go block and updating atoms does it seem reasonable to return a function that closes over those atoms for answering questions about the stats ?
why atoms if the data is closed over?
(that is, as opposed to updating the values as args to recur)
I ask because a have a pretty good idea of when I would close over values (they are only relevant in a specific scope and it makes sense to update them via recursion) and I have a good idea of when to use a global atom (multiple threads need to access the values and they don't "belong" to a specific process), but stuff that is in between seems like it's just cargo culting OO usually
minor thing, there's a bunch of extra parens that will lead to "long can't be cast to IFn" errors at runtime
see, the thing is you are simulating an object via closures, and we have actual objects if you want to do OO
the idiomatic thing would be to have functions that calculate the stats you want, based on the raw values, and put all the values in a single hash map that gets updated with a single swap per iteration
and return the atom
yeah, just wanted to make sure you were aware
so the normal way to do it would involve (atom {:size 0 :start (System/currentTimeMillis) :freq 0})
and swap! to update the :size and :freq keys each iteration
and instead of closing over the functions, just make them normal defs that a client can use
don't use hand-made message passing - I don't think this calls for OO, but if you need it we have defprotocol, deftype, defrecord etc. for making actual objects that have methods
but I thought it was more of a basis for using https://github.com/ztellman/potemkin#def-derived-map
thanks for the feedback - I am thinking the def-derived-map might get closer to what you were recommending (just return a map!) while letting me pop in some extra features - I can see the function option is pretty dodgy (kw -> any)
it's much more normal to hand the user data and a function - if you want extra features you don't need def-derived-map, defrecord suffices
but I don't think you even need the extra features