This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-10
Channels
- # aws (3)
- # beginners (186)
- # boot (25)
- # cider (2)
- # cljsrn (57)
- # clojure (161)
- # clojure-boston (1)
- # clojure-dusseldorf (11)
- # clojure-italy (5)
- # clojure-russia (20)
- # clojure-serbia (1)
- # clojure-spec (10)
- # clojure-uk (16)
- # clojurescript (207)
- # community-development (86)
- # core-async (2)
- # cursive (40)
- # datascript (1)
- # datomic (2)
- # editors (5)
- # emacs (8)
- # funcool (1)
- # gsoc (2)
- # hoplon (2)
- # jobs (5)
- # leiningen (3)
- # liberator (18)
- # luminus (18)
- # off-topic (106)
- # om (2)
- # onyx (5)
- # pedestal (7)
- # powderkeg (7)
- # re-frame (7)
- # remote-jobs (1)
- # ring-swagger (4)
- # rum (5)
- # slack-help (1)
- # untangled (11)
- # yada (48)
Newbie here, but I can't figure out why
(map #({:a %}) [1 3 4])
ArityException Wrong number of args (0) passed to: PersistentArrayMap clojure.lang.AFn.throwArity (AFn.java:429)
doesn't work, whereas
backpack.core> (map (fn [x] {:a x}) [1 3 4])
({:a 1} {:a 3} {:a 4})
backpack.core>
does.@saaadhu try (macroexpand '#({:a %})) in your repl
I'll try it here...
haha, good enough - that's what it gives me for '#({:a %})
notice there's extra parenthesis around the hash-map
Yeah, the error makes sense now. I'll read more about writing anonymous functions - I thought they always needed a paren around the body
I see it now - the shorthand form always puts the body inside parens, so I can't create a map that way. Thanks 🙂
spend three more characters and just use an (fn []) form
That map ended up with a bunch of other elements as well, so I switched to (fn[]) in the end.
is it a good or bad thing for Clojure that there will be no modules in java? https://jcp.org/en/jsr/results?id=5959
@andmed If I understand this talk correctly, its not that much beneficial and a bit of pain here and there to make things work correctly https://www.youtube.com/watch?v=4fevIDAxQAM&t=6s
its just I read different opinions, from "good" to "bad" (bad because of the way classloader and reflection in Clojure works), not qualified enough to form my own view
andmed: I’ve seen no evidence that java modules (as they were actually implemented) would help clojure at all, and there are many ways they break existing clojure code and tools
so modules not being accepted would be a very good thing for clojure
Ok. Local guys say that modularity would speed up boot time (really dont know why, its mostly VAR loading what it takes). Thanks.
the boot time is all in clojure compiler, it has nothing to do with java layer stuff
also start time is fine if you use clojure.core, the thing that takes a long time is lein and nrepl (those are common culprits)
Hi! Suppose I have list '(a a b b c c)
. How can I remove a random element without removing al identical elements as well?
@leonoel yes certainly. @reborg very nice short version. My program would have to do this quite often, any idea on the performance of both solutions?
depends of what you're trying to achieve as a whole, both have linear complexity but maybe you can just shuffle once at the beginning and then rest
is constant time
also, concat is not stack-friendly so it may not be a good idea to do that many times on a big collection
@jlmr what @leonoel says... 🙂 Can you post a snippet where you show what you're going to do with the random element? Is a one off or you are going to consume the entire list before shuffling again?
@reborg difficult to find a good snippets, code is still in very early stages (also my first Clojure program…). It’s part of an evolutionary algorithm, one option to mutate is by removing a random element from a list
is it an option to have the input lists as vectors? Subvec would likely go faster to slice one element off
If your removing a random element you only need to shuffle it once unless it's changing
@andmed Jigsaw is not approved yet. https://jcp.org/en/jsr/results?id=5959
When I try a simple SELECT query with HugSQL in REPL I always get a lazyseq instead of a map. On the official doc it should be a map returned. Any idea ?
@jlmr if you can have vectors, (pop (assoc v (rand-int (dec (count v))) (peek v)))
is prob the best. The shuffle
above is still O(n), this last one is O(1)
I go back with question. I have pictures in a postgres DB and I would like to retrieve and send them when I get a AJAX call. How should I process ? Currently my pictures are retrieved in bytearray format
@romain does this help maybe? (check the marked answer) http://stackoverflow.com/questions/3988486/serving-binary-files-from-the-database-with-compojure
Looking at that it seems that compojure should in fact handle bytearrayinputstreams correctly. Are you using compojure?
hi 🙂 I’m working with a client library that generates its source code via reflection from a java library. I’d like to peek at the source of one of these functions generated via reflection, but I’m finding it less than straightforward to do so. Should I be able to do this?
@derwolfe Looking at the github, I don't see any mention of send-message-batch
. Do you know where it comes from? Am I looking at the wrong library/version?
https://github.com/mcohen01/amazonica
@bpiel you are totally right 🙂 it is a function from the original java client. When using the repl, it is available, I just don’t know the api signature
@derwolfe There are there ways you can do this:
1) reflection, via :amazonica/methods metadata on the var
2) javadoc: install dash (if you do not have dash tell andy to pay for dash), install java sdk javadocs, look at ${operation}Request
class
@bpiel you don’t see send-message-batch because it’s automatically generated via reflection
lvh: thanks 🙂 I should essentially just mimic a kebab case version of sendMessageBatchRequest’s args
@derwolfe yes and hope that it doesn’t have any keys that are kebab-cased wrong by the old algo
(I fixed it for fn vars because you can, backwards-compatibly; but it’s too ugly to fix for keys)
right around here: https://github.com/mcohen01/amazonica/blob/f8d382075961ac2c6125649b922e5d06ef8fc626/src/amazonica/core.clj#L906
lvh: found (sqs/send-message-batch :queue-url qu :entries [{:id 2 :message-body "a"} {:id 1 :message-body "b"}])
‘‘’(set! print-namespace-maps false) => false print-namespace-maps => true
is there something i do wrong? seems to me like the dynvar is not resetting to new value… ??
@karol.adamiec try alter-var-root/var-set
(var-set print-namespace-maps false) ClassCastException java.lang.Boolean cannot be cast to clojure.lang.Var clojure.core/var-set (core.clj:4285)
(var-set #‘print-namespace-maps false) => false print-namespace-maps => true
this one doesn’t work either:
(alter-var-root #'*print-namespace-maps* (constantly false))
thx for trying
(binding [print-namespace-maps false] {:db/id 1 :db/ident :foo/bar}) => #:db{:id 1, :ident :foo/bar}
has anyone forced clojure recently to drop that annoying namespaced maps??
@karol.adamiec What editor?
cursive
Yeah know issue: https://github.com/cursive-ide/cursive/issues/1541
Yeah, to be honest xforms I haven't had the issues that xforms tries to solve.
But at a basic level transducers are simply a way of expressing transformation pipelines separate from the method in which the output collection is created
I've done some tutorials on them here https://www.youtube.com/watch?v=WkHdqg_DBBs
But the gist is this: assume a mapping reduce looks like this:
(defn mapping [f coll]
(reduce (fn [acc i]
(conj acc (f i)))
[]
coll))
Then (mapping inc [1 2 3]) => [2 3 4]
He has xforms/reduce there, but I was under the impression that you would typically (but not necessarily only) call (reduce xf ...) on a transducer and that a transducer itself cannot / should not be used for the reduction itself.
Transducers in a nut-shell
(defn map [f coll]
(reduce (fn [acc i]
(conj acc (f i)))
[]
coll))
;; refactor to pull out reduce
(defn mapping [f]
(fn [acc i]
(conj acc (f i)))
(reduce (mapping inc) [] [1 2 3]) => [2 3 4]
;; But this isn't good enough because our accumulator may not support `conj`
;; Refactor once more:
(defn mapping [f]
(fn [xf]
(fn [acc i]
(xf acc (f i)))))
(reduce ((mapping inc) conj) [] [1 2 3]) => [2 3 4]
(reduce ((mapping inc) +) 0 [1 2 3]) => 9
now you can reuse mapping
in a dozen different situations and we never have to define a mapping transducer again.
I'd stick with studying the transducers in clojure.core then move on to other developer's interpretations of them (including mine). Theres a lot of things that can be done with transducers. And a lot of those use cases may or may not be good depending on the context.
@schaueho, there are some examples of 1-item transducers in the xforms README.
For example, combining a windowing transducer with a transducer that finds the average.
Or adding up the numbers in a partition
but the xform transducers are really advanced, I'd wait to try to understand them until you've mastered the ones in clojure.core
the implementations of map
and filter
are great places to start
The advantage of using the partition
transducer with the reduce
transducer, for instance, is that we can avoid creating collections in partition
.
but why worry about that if you don't understand why you'd use a transducer in the first place?
To relate this confusion to a problem I'm trying to solve: I currently have some code which is happily reducing some sequencential data to a map. But I have a usecase where it would be helpful if the reducing function would actually operate on some sort of streaming input (potentially from a core.async channel). So I was wondering how I could approach this by moving out the central logic into some seam-like function(s) that I can hand over to e.g. pipeline
.
does your logic fit well as a map, mapcat, or something of that nature?
pipeline will run your code in parallel, so you'll probably need to think about how to handle that, but that's the basic idea
(pipeline ... (map your-fn) ...) will hand the items to your-fn
@schaueho I’m actually not sure how 1-item transducers combine with streams. Let me take a look.
@weavejester I wasn't concretly thinking of using xforms (remembering Christophe's talk at this year's clojureD), I was just looking at it and then got confused.
@schaueho you intend to produce a single map from a channel of inputs though?
Or some other streaming source
if so, there's core.async/reduce
I realize the more general issue I try to address (which is moving away from shuffling sequences and maps around sort of "manually" to a more streaming-like approach in which I have tiny computational pieces that I can tie together as needed) requires quite a bit of thinking about where and how different data elements need to be transformed.
Adding a 1-item transducer to a channel works the way you’d expect. The channel just produces one item just before it’s closed.
but you probably don't want to do that if the reducing function takes awhile to execute.
thanks for the input and more food for thought, @tbaldridge @weavejester
The two head-scratching pieces is the frequencies
call (which is reducing the sequence to a map) and the subsequent maxfreq
computation using it. Anyway, I'll scratch my head somemore 🙂
more or less, yes (there are some additional magic numbers involved, e.g. https://en.wikipedia.org/wiki/Tf%E2%80%93idf#Term_frequency_2)
Hi all, I set up lein on a cloud9 container a few days ago and was trying to run lein figwheel
. I've set :verbose true
in the project.clj compiler details, but I'm getting nothing more verbose than Subprocess failed
after Compiling resources/public/js/compiled/out/cljs/core/async.cljs
. Nothing seems to be output to figwheel_server.log, so was wondering if anybody knows of anywhere else it might be logged?
is it possible to “kill” async “go” in the REPL? not going into details i am testing something with random error and need to kill go
in the REPL
No, but go blocks should park at some point and then are open for garbage collection. Unless you're stuck in a loop, in which case that's a bit of an anipattern
But what's the problem you're experiencing?
that works, or have it listen to a channel, (channels return nil when closed), or something of the like
go blocks aren't first-class things, they're simply syntactic sugar over callbacks, so there's no reliable way to stop them at the system level, it has to be done in user code.
hello everyone, im building a microservice that is supposed to do http requests to several other http endpoints in a parallel/concurrent/async fashion .. ive been looking into async http clients in clojure and found that there are many ways to achieve this (callbacks, deferred, core.async & etc) initially i was thinking about using NODE.js for this task but js sucks … is there in clojure something similar to Promise.all in JS? I mean: do a list of concurrent tasks and notify me when ALLL have finished?
@plins (run! deref ps) - if ps is a list of derefables, it will return once all are finished
you can deref a promise, a delay, a future…
often what you want is to combine the results, something like (reduce (fn [accumulated p] (combine accumulated @p)) {} (doall ps))
where combine knows how to merge each realized result into your total answer
see also the clojure.core.reducers namespace
in a test, can I with-redefs
over my import's imports?
IE I have namespace a
that imports b
,
in a-test
, how can I with-redefs
over b/some-fn
@josh.freckleton you can reference b
directly on your namespace and do a with-redefs
on that
you mean require
it in my test?
yes, or use the full name of the function, eg some.namespace.bla/the-fn
that does work, thanks! (I was kinda hoping i wouldn't need to maintain a "parity" between both imports but...
oh ya, that makes sense too.
thank you!
no problem 🙂
I’d like to seed a discussion on “data macros” but on Google groups since Slack discussion are not archived. https://groups.google.com/forum/#!topic/clojure/XJqm4LCGSk0 If you have a moment, please jump in