Fork me on GitHub
#clojure
<
2017-05-10
>
saaadhu01:05:40

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.

noisesmith01:05:26

@saaadhu try (macroexpand '#({:a %})) in your repl

noisesmith01:05:29

I'll try it here...

noisesmith01:05:06

haha, good enough - that's what it gives me for '#({:a %})

noisesmith01:05:22

notice there's extra parenthesis around the hash-map

saaadhu01:05:15

Yeah, the error makes sense now. I'll read more about writing anonymous functions - I thought they always needed a paren around the body

saaadhu01:05:15

I see it now - the shorthand form always puts the body inside parens, so I can't create a map that way. Thanks 🙂

john01:05:48

@saaadhu you could try something like (map #(do {:a %}) [1 3 4])

john01:05:14

anything to get the map off of the function position in the list

noisesmith01:05:32

spend three more characters and just use an (fn []) form

john01:05:52

hurts my eyes 🙂

saaadhu01:05:56

That map ended up with a bunch of other elements as well, so I switched to (fn[]) in the end.

saaadhu01:05:33

I'll inspect macroexpand's output next time before I ask here 🙂

lincpa02:05:14

@john (map #(hash-map :a %) [1 3 4])

john02:05:29

aye, that's better 🙂

andmed05:05:36

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

iku00088805:05:24

@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&amp;t=6s

andmed05:05:52

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

noisesmith16:05:39

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

noisesmith16:05:54

so modules not being accepted would be a very good thing for clojure

andmed22:05:03

Ok. Local guys say that modularity would speed up boot time (really dont know why, its mostly VAR loading what it takes). Thanks.

noisesmith22:05:49

the boot time is all in clojure compiler, it has nothing to do with java layer stuff

noisesmith22:05:23

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)

jlmr06:05:53

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?

leonoel06:05:57

(defn without-nth [n xs]
  (concat (take n xs) (drop (inc n) xs)))

jlmr07:05:25

@leonoel thanks! I guess n should be a random int?

leonoel07:05:34

@jlmr is this helpful ?

reborg07:05:27

@jlmr (rest (shuffle '(a a b b c c))) ?

jlmr08:05:03

@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?

leonoel08:05:32

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

leonoel08:05:24

also, concat is not stack-friendly so it may not be a good idea to do that many times on a big collection

jlmr08:05:53

@leonoel I will apply it to a lot of different small lists

jlmr08:05:18

so do I understand correctly that the concat version would be better?

leonoel08:05:38

hard to say without a benchmark

reborg08:05:11

@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?

jlmr08:05:12

@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

jlmr08:05:44

I hope that makes sense 🙂

reborg08:05:19

is it an option to have the input lists as vectors? Subvec would likely go faster to slice one element off

jlmr08:05:29

@reborg, yes I’m considering that as well. Will look into that a little later this week

jlmr08:05:02

@reborg and @leonoel thanks for the help!

Drew Verlee10:05:28

If your removing a random element you only need to shuffle it once unless it's changing

romain13:05:20

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 ?

romain13:05:38

Well, I answer to myself, I needed :? and :1 options

reborg13:05:33

@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)

romain14:05:59

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

martinklepsch14:05:42

Looking at that it seems that compojure should in fact handle bytearrayinputstreams correctly. Are you using compojure?

romain14:05:15

Yes I am, served by http-kit

romain14:05:39

so it might be the way I handle it in clojurescript

derwolfe14:05:48

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?

derwolfe14:05:21

(I want to get the source for amazonica.aws.sqs/send-message-batch fn)

bpiel15:05:11

@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

derwolfe15:05:15

@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

derwolfe15:05:31

available being pops up with tab-completion

lvh15:05:17

are you using an old version of amazonica

lvh15:05:29

I recently sent in a PR that removes private/protected methods

lvh15:05:47

oh, nope, send-message-batch is a real thing

lvh15:05:01

@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

lvh15:05:11

3) ask the cli docs because they’re really the same thing

lvh15:05:34

@bpiel you don’t see send-message-batch because it’s automatically generated via reflection

derwolfe15:05:52

lvh: thanks 🙂 I should essentially just mimic a kebab case version of sendMessageBatchRequest’s args

lvh15:05:14

@derwolfe yes and hope that it doesn’t have any keys that are kebab-cased wrong by the old algo

bpiel15:05:32

@lvh That's cool. (if you happen to know already) what code does that?

lvh15:05:39

(I fixed it for fn vars because you can, backwards-compatibly; but it’s too ugly to fix for keys)

lvh15:05:49

@bpiel check out amazonica.core

bpiel15:05:40

awesome thanks

lvh15:05:18

godspeed, good luck &c

derwolfe15:05:07

lvh: found (sqs/send-message-batch :queue-url qu :entries [{:id 2 :message-body "a"} {:id 1 :message-body "b"}])

derwolfe15:05:23

thanks for your help 😉

karol.adamiec15:05:31

‘‘’(set! print-namespace-maps false) => false print-namespace-maps => true

karol.adamiec15:05:56

is there something i do wrong? seems to me like the dynvar is not resetting to new value… ??

lvh15:05:42

@karol.adamiec try alter-var-root/var-set

karol.adamiec15:05:55

(var-set print-namespace-maps false) ClassCastException java.lang.Boolean cannot be cast to clojure.lang.Var clojure.core/var-set (core.clj:4285)

lvh15:05:28

var-set takes a var, you gave it a thing that evaluated to true

lvh15:05:51

try #'*print-namespace-maps*

lvh15:05:24

huh, weird; that still doesn’t work

karol.adamiec15:05:40

(var-set #‘print-namespace-maps false) => false print-namespace-maps => true

lvh15:05:12

this one doesn’t work either:

(alter-var-root #'*print-namespace-maps* (constantly false))

lvh15:05:15

ok, then I’m out of ideas 🙂

karol.adamiec15:05:23

thx for trying

karol.adamiec15:05:56

(binding [print-namespace-maps false] {:db/id 1 :db/ident :foo/bar}) => #:db{:id 1, :ident :foo/bar}

karol.adamiec15:05:24

has anyone forced clojure recently to drop that annoying namespaced maps??

schaueho16:05:51

My confusion mostly stems from reading the into on cgrands xforms library

darwin16:05:17

it is pretty advanced topic IMO

tbaldridge16:05:22

Yeah, to be honest xforms I haven't had the issues that xforms tries to solve.

tbaldridge16:05:56

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

schaueho16:05:33

Christophe has this notion of a "1-items transducer" on the page

tbaldridge17:05:17

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]

schaueho17:05:20

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.

tbaldridge17:05:08

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

tbaldridge17:05:51

now you can reuse mapping in a dozen different situations and we never have to define a mapping transducer again.

tbaldridge17:05:28

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.

weavejester17:05:47

@schaueho, there are some examples of 1-item transducers in the xforms README.

weavejester17:05:07

For example, combining a windowing transducer with a transducer that finds the average.

weavejester17:05:36

Or adding up the numbers in a partition

tbaldridge17:05:53

but the xform transducers are really advanced, I'd wait to try to understand them until you've mastered the ones in clojure.core

tbaldridge17:05:10

the implementations of map and filter are great places to start

weavejester17:05:08

The advantage of using the partition transducer with the reduce transducer, for instance, is that we can avoid creating collections in partition.

tbaldridge17:05:45

but why worry about that if you don't understand why you'd use a transducer in the first place?

schaueho17:05:37

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.

tbaldridge17:05:28

does your logic fit well as a map, mapcat, or something of that nature?

schaueho17:05:42

not "well", as I need to hold some state in between

tbaldridge17:05:28

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

tbaldridge17:05:46

(pipeline ... (map your-fn) ...) will hand the items to your-fn

weavejester17:05:28

@schaueho I’m actually not sure how 1-item transducers combine with streams. Let me take a look.

schaueho17:05:10

@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.

weavejester17:05:42

@schaueho you intend to produce a single map from a channel of inputs though?

weavejester17:05:55

Or some other streaming source

tbaldridge17:05:15

if so, there's core.async/reduce

schaueho17:05:48

ah, thanks, another piece to the puzzle 🙂

schaueho17:05:41

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.

weavejester17:05:23

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.

tbaldridge17:05:31

but you probably don't want to do that if the reducing function takes awhile to execute.

schaueho17:05:40

thanks for the input and more food for thought, @tbaldridge @weavejester

schaueho17:05:05

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 🙂

spei17:05:35

shouldn't the normalize divide every val by the sum(vals)?

schaueho17:05:28

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)

spei18:05:31

oh cool. i guess you'll also ned a way to calculate the document frequency

schaueho18:05:51

that's than the next piece I want to convert, yes

danp19:05:07

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?

kwladyka19:05:24

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

tbaldridge19:05:02

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

tbaldridge19:05:18

But what's the problem you're experiencing?

kwladyka19:05:49

it is not really loop but run something 100 times 😉

kwladyka19:05:16

just i was wonder to stop it when i want, like kill go

kwladyka19:05:38

i could do some atom to stop it with true/false

tbaldridge20:05:46

that works, or have it listen to a channel, (channels return nil when closed), or something of the like

tbaldridge20:05:17

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.

kwladyka20:05:21

i know i used it not in right way, just wanted test something fast 🙂

plins20:05:26

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?

noisesmith20:05:33

@plins (run! deref ps) - if ps is a list of derefables, it will return once all are finished

plins20:05:53

thank you! 🙂

noisesmith20:05:10

you can deref a promise, a delay, a future…

noisesmith20:05:40

often what you want is to combine the results, something like (reduce (fn [accumulated p] (combine accumulated @p)) {} (doall ps))

noisesmith20:05:57

where combine knows how to merge each realized result into your total answer

noisesmith20:05:16

see also the clojure.core.reducers namespace

dpsutton20:05:30

you could look into zach's manifold library

josh.freckleton20:05:17

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

wilkerlucio21:05:23

@josh.freckleton you can reference b directly on your namespace and do a with-redefs on that

josh.freckleton21:05:48

you mean require it in my test?

wilkerlucio21:05:22

yes, or use the full name of the function, eg some.namespace.bla/the-fn

josh.freckleton21:05:31

that does work, thanks! (I was kinda hoping i wouldn't need to maintain a "parity" between both imports but...

josh.freckleton21:05:36

oh ya, that makes sense too.

wilkerlucio21:05:46

no problem 🙂

steveb8n23:05:52

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