This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-10
Channels
- # beginners (151)
- # cider (41)
- # cljdoc (7)
- # cljs-dev (6)
- # clojure (92)
- # clojure-dev (5)
- # clojure-italy (26)
- # clojure-losangeles (1)
- # clojure-nl (10)
- # clojure-russia (3)
- # clojure-spec (23)
- # clojure-uk (82)
- # clojurescript (56)
- # clojutre (1)
- # core-async (3)
- # cursive (15)
- # datomic (26)
- # editors (3)
- # emacs (3)
- # events (2)
- # figwheel-main (192)
- # fulcro (66)
- # leiningen (12)
- # mount (1)
- # off-topic (131)
- # portkey (6)
- # re-frame (38)
- # reagent (10)
- # reitit (7)
- # ring-swagger (55)
- # shadow-cljs (21)
- # spacemacs (11)
- # tools-deps (48)
Can I concisely take N elements from a list, each having a distinct value according to fn f?
e.g. (distinct-n-by-f coll 2 :age) ;; gives me 2 elements, and each will have a different :age
first, map f
onto coll
. (map f coll)
.
Then take 2 elements from it. (take 2 (map f coll))
absolutely
(take 2 (distinct (map f coll)))
take, distinct, map : they all compose lazily which means you'll only compute what's needed for those two elements
(comp (partial take n) distinct)
?
yes - I wasn't trying to make a transducer there but it is an option
something like (into [] (comp (map f) (distinct) (take n)) coll)
or do you need the original items that were distinct by f, and not f of the items?
Looks like the medley library has a function distinct-by
that lets you supply f
to it, but keeps the original items when there f
values are distinct: https://crossclj.info/fun/medley.core/distinct-by.html
What I really had in mind (and sorry for the confusion) is to "sort by distinctness" so to call it... e.g. given [1 1 1 2 2 3 3]
, return [1 2 3 1 2 3 1]
I don't really need to have it implemented for me, can eventually do myself. Although a pointer to prior art would be welcome
here's one approach
(defn distinct-by
[f coll]
(letfn [(step [seen [e :as xs]]
(lazy-seq
(let [match (f e)
s (seq xs)]
(when s
(if (contains? seen match)
(step seen (rest s))
(cons e
(step (conj seen match)
(rest s))))))))]
(step #{} coll)))
based on distinct
but sure, (comp (partial map first) keys (partial group-by f))
is another approach
Are Clojure Agents also called Agents (not talking about AI thing) in academia, or CS world. Found this article http://wiki.c2.com/?ActorVsAgent and am trying to understand what is written there, and under their definition of agent → communicate to an agent by modifying something that it is observing; you do not need to know that the agent exists What does this even mean, do not need to know that the agent exists? Are we talking about the same thing?
@denisgrebennicov They probably mean that the agent doing the modification doesn't need to know if anyone cares about the thing modified or not.
@henrik but we indeed are talking about the same thing. Meaning their definition of agent and definition on the http://clojure.org website are describing the same thing, right?
It would be really out of character for things in Clojure to use arbitrary nomenclature. But I know too little about either to answer for sure.
Well for example refs is just a Clojure concept, right? Refs or references are known to be something else in other programming languages, or am I wrong?
What's the stage of AI in clojure? Probably will have to fall back on Python, or?
advanced neural nets to be specific (convolutional, generative ,etc)
" The MXNet framework is known for its great scalability" oh nice
Neanderthal is awesome, but then you have to do everything manually and that would take forever
There is https://github.com/aria42/flare (seq analysis with RNN) and clj-autograd on top of Neanderthal. Also, this sort of question is likely to get more responses in #data-science
not at all. neanderthal does everything automatically, and you do not have to do (almost) nothing manually in the scope of linear algebra and vectorization. you may have to do things on your own when it comes to deep learning or something else, but neanderthal is not a deep learning library.
https://www.reddit.com/r/Clojure/comments/7w5ceh/machine_learning_with_clojure_in_2018/
I would like to do something that’s probably quite odd: run a functions juuuust before something is taken off a core.async channel.
So let’s say my producer offers a value. The consumer might come along to request a value later, maybe much later, so the thread blocks until this happens. In the meantime, the value might have expired, in which case I want to offer a different value rather than the one that was initially on offer, thus applying a function to the value just before it is consumed.
I tried to do this with (chan buffer (map func))
, but for all its laziness, map
seems very eager to run that function straight away rather than waiting for it to be realized.
I’m not sure there’s a way to do that in a transducer since it’s going to be unaware of the consumer showing up (afaik). You might have to just wrap the place of consumption so that you first poll the channel and drop its value if it’s not what you want. Maybe you could hide that behavior in a pipeline between two channels?
or maybe you could do it with a custom buffer? if a better replacement value shows up before you take from the buffer you could swap the value in the buffer?
but sounds like maybe you don’t have a “better replacement value” showing up. you just want to compute one at consumption? Maybe you could reorganize that to just put potential “refreshes” on the channel too and then use the custom buffer
Is Rich's paper "The Clojure programming language" available somewhere?
I don’t think that is a thing
if you are seeing it as a reference somewhere, it probably literally means “the Clojure programming language”
I've found it as reference in a paper today and also listed on ACM here https://dl.acm.org/citation.cfm?id=1408682 But it is actually a reference to a conference talk... ResearchGate has it in the right category. I admit it got me curious.
@rutledgepaulv Custom buffer is interesting, but yes, I have to execute code. An alternative I’ve thought of is that the consumer has to pass a return-channel to the producer over another channel. That way, the producer listens for incoming channels over the channel, and has the opportunity to produce the value fresh straight away, then put it on the return-channel that the consumer supplied. The reason I wanted to see if there’s another option is that this introduces at least two more channels, and I wanted to keep it more straightforward than that.
yeah. at a high level it sounds like you’re asking for a custom buffer to me. something that knows when values that have already been collected should be discarded and replaced with other values (however those arrive)
not sure how feasible that is without knowing more details of expiry / new values, etc
The general problem could be illustrated like this:
(defonce db-ch (chan))
(defonce db (atom nil))
(def reset-db? (atom true))
(go-loop []
(when @reset-db?
(reset! db (get-new-db))
(reset! reset-db? false))
(>! db-ch @db)
(recur))
So, the DB connection needs to be refreshed. With this model, it only gets refreshed after the next value is taken off the db-ch
.
just attach a watch to the atom that writes to the chan? and a sliding buffer on db-chan so it discards old values if unused
With a sliding buffer, will the loop still block, or will it go nuts throwing ever newer DBs into the void?
it’d go nuts if there’s no other reason to block
ideally you’d just block on whatever is deciding that the db needs to be reset. or if you just wanted a periodic thing you could use a timeout
but I think you could remove the loop with just a watch on the atom and use >!!
In this case, let’s say the DB may be requested many times between the time it is created and the time it is refreshed. So it has to keep supplying it to anyone who asks.
that sounds like a weird use for a channel. channels are more for data in motion than resupplying the same data to people who ask until it changes (imo)
why can’t the consumers just deref db in that case?
Yeah, it might be the wrong tool. The channel acts as a choke point, essentially, to manage the lifecycle of the DB.
is it important that once it’s refreshed, all consumers are immediately told about it? or just next time they ask?
Derefing is fine, but only one request to update the DB atom is allowed at any point in time.
if they’re asking, then yeah I wonder if just an atom by itself is okay without core.async. by using a watch on reset-db that only runs the reset! when the value of reset-db? transitions from false to true it should be atomic since watches run after the CAS
What might cause the exception java.lang.IncompatibleClassChangeError: Implementing class
when importing a class created with gen-class
?
ICCE gets thrown by the JVM under many bad linkage conditions... like calling a static method using a instance method bytecode/descriptor
https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-5.html https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-6.html
@dave.dixon got a sharable reproducing case?
can you dump the output of javap
on the compiled class?
It should match http://kafka.apache.org/20/javadoc/org/apache/kafka/connect/connector/Connector.html
@dave.dixon you also need to put classes
on your :paths
-- it's under the test alias right now
The exception does not give the class name. Added classes
to :paths
, same result. Output of javap
looks correct AFAICT.
:constructors
was not specified correctly. Fixed that, now only one constructor, same error on import.
Yes. Same result.
Tried just using Object
in :implements
, same error on import.
Turns out the problem is that I needed to use :extends
to implement an abstract class, not :implements
, which is only for interfaces.
you're going to have a bad time with setAccessible -- stuff like that will be denied in future JVMs
I was hung up on the "reference" docs at https://clojure.org/reference/compilation. When I went to the actual reference for the API, I found :exposes
which is what I really wanted to get at a protected field.
It's a little ugly that gen-class
will happily output a class that won't compile.
Is there a way to create a new Collection from Collections$UnmodifiableCollection?
Got it thank you 😛
not at all. neanderthal does everything automatically, and you do not have to do (almost) nothing manually in the scope of linear algebra and vectorization. you may have to do things on your own when it comes to deep learning or something else, but neanderthal is not a deep learning library.