Fork me on GitHub
#beginners
<
2019-09-19
>
mario.cordova.86205:09:12

Is there a succinct way of reducing an array of maps into a single map where all the values of the maps are summed?

mario.cordova.86205:09:46

[{:a 1} {:a 1} {:a 1}] -> {:a 3}

mario.cordova.86205:09:31

merge-with seems like it will do the trick

mattias50406:09:33

So, after a bunch of searching I still can’t figure out where standard out ends up. I’m doing a small web app thing with Lein, Ring, Compojure, Hiccup. Not sure how to track it - does Lein or Ring configure it? I’m trying to do the old print statements in my handlers.

lennart.buit06:09:37

You can redirect stdout I believe by binding https://clojuredocs.org/clojure.core/*out*

lennart.buit06:09:43

so maybe any of those libraries do?

dpsutton06:09:13

@mattias504 and just (prn “hi”) doesn’t end up in the lein repl out? How are you starting your repl?

mattias50406:09:48

Well...it turns out, I should have kept quiet a while longer. Thanks for replying, but this was, of course, just another case of... programmer error. Turns out, even with few lines of code confusion can arise. What can I say, years of compilers complaining of types has made me weak 😩

afshan.shokath07:09:03

Hey, I am a beginner in clojure. Have worked on one simple project. Can someone let me know what possible questions can a beginner be asked during a clojure interview?

gerred11:09:50

#jobs-discuss might be a good place for that.

abhi18av11:09:46

Guys, how do we assign the value returned by a go block to a variable?

gerred11:09:57

you need to receive it on a channel and bind it, either by swapping it to an atom or receiving in a let binding.

gerred11:09:12

<!! will let you do a blocking receive on the channel returned by the go block

gerred11:09:08

though I might consider just using an output channel you can pass around to that go block function, and use the return channels more for synchronization.

gerred11:09:24

(let [x (<!! (go [] (Thread/sleep 1000) (+ 1 2)))] x) => 3

gerred11:09:37

just as a quick and dirty example.

gerred11:09:09

though that is still a blocking receive, so all you've done is blocked that let statement until the go form sends the return value through the channel.

abhi18av13:09:03

Thanks @! But how to do this in ClojureScript which has no <!!

gerred14:09:45

that's correct, you can't block in clojurescript. 🙂 you'll need to pass in a channel and maybe use promises, so check out promesa

abhi18av14:09:35

Ahh, I see - thanks @ :+1:

noisesmith17:09:50

another option here is take! which is async, and has an optional callback arg to be invoked when the value comes in

abhi18av12:09:53

Thanks @ and @ - I’m working on the getting it going and these suggestions lead me to good resources for sure :+1:

kalaneje11:09:23

how to start a Leiningen process in debug JVM mode?

dazld14:09:57

what’s the idiomatic way of taking a bunch of expressions that might be nil, and getting a list of only non-nils?

dazld14:09:07

(list 1 (when false :foo)) => (1 nil)

dazld14:09:13

for example, this is a bit annoying

dazld14:09:38

I know i could filter, but.. bit icky

dpsutton14:09:53

(remove nil? coll) doesn't work for you?

dpsutton14:09:47

not following the ickiness i guess

dazld14:09:29

comes from doing some reagent work

dazld14:09:34

but - yes

dazld14:09:53

(defn foo [thing & others]
  (prn thing)
  (when (seq others)
    (prn others)))

(foo 1 (when false 2))

dpsutton14:09:55

what's the problem you're solving?

dazld14:09:10

I have a reagent component that can take children

dazld14:09:20

however, some children are conditionally present

dazld14:09:24

the first one is always there

dpsutton14:09:43

i think reagent is good at throwing nil children out of the render tree

dazld14:09:48

seq not so much

dazld14:09:12

and i want to perform logic based on whether we have [first-el & rest-children]

dazld14:09:38

but, rest-children with conditional args as above is always either (nil) or a value

dpsutton14:09:33

you could just (defn foo [components] (when (next components) ...))

dazld14:09:51

[some-holder-comp
  [first-child]
  (when condition
    [other-child])]

dpsutton14:09:14

is it just two items?

dazld14:09:20

not necessarily

dazld14:09:22

in this case, yes

dazld14:09:36

if there’s more it’s interesting too

dazld14:09:42

i could just destructure

dazld14:09:30

@dpsutton (when (next rest-children) ...) was a great suggestion

dpsutton14:09:34

calling next on rest-children sounds like a bug. Since (next [:a]) returns nil, your rest-children could have valid values and not render in that way.

dpsutton14:09:51

It sounds like you're got some confusing logic somewhere. I imagine there's a way out of this

dazld14:09:44

yeah! i misunderstood

dazld14:09:52

actually wanted first

dazld14:09:57

but that’s the same area, this helped

dazld14:09:31

thank you

dpsutton14:09:33

i'm not seeing how you get (nil)

dpsutton14:09:42

(defn f [x & m]
  [x m])
#'duplicates/f
duplicates> (f 1)
[1 nil]

dazld14:09:20

=> nil
(defn foo [thing & others]
  (prn thing)
  (when (seq others)
    (prn others)))
=> #'fy.repl/foo
(foo 1 nil)
1
(nil)
=> nil

dpsutton14:09:35

well you're calling it with nil. you're sending it a value

dazld14:09:27

(defn foo [el1 & others]
  (if (first others)
    :foo
    :bar))

dazld14:09:29

ended up with this

dazld14:09:36

thanks though, that was helpful

sova16:09:16

For compojure routing...

sova16:09:25

There must be something to the ordering of entries

sova16:09:03

(GET "/S:section&L:lesson&P:part" [section lesson part])
  (GET "/S:section&L:lesson" [section lesson])
  (GET "/S:section&L:lesson&:drill" [section lesson pre-strdrill])
i'm not able to access /S1&L1&particle like I expect with that last line, moving it to the top doesn't catch the pattern any longer either it seems... any help?

hiredman16:09:12

routes are tried in order from top to bottom

hiredman16:09:56

a compojure "route" is actually just a handler function that returns nil if it doesn't match

hiredman16:09:08

your middle route there will capture everything after L and bind it to lesson

sebastiano.barrera_cl20:09:08

On ClojureScript specifically: can anybody briefly explain what is the semantics of #object[Constructor ...] compared to #js ... ? Like, can an #object[...] (btw, is it called a native object?) be used like a map or vector? How does one work with it in cljs?

sebastiano.barrera_cl20:09:44

(btw, I'm glad to have joined this community... I can't wait to start contributing code and answers. Thanks in advance!)

hiredman20:09:28

It is just the default way things print

hiredman20:09:03

If the printing system (which is a little different in cljs from clojure, so I don't know the specifics) doesn't have a rule like "print seqs as ( then coma separated values then )" that matches when asked to print some random thing, it prints them like that

hiredman20:09:18

it used to be the case that those would print as #<...> which was sometimes referred to as unreadable objects, but there were changed to print that way which matches up more with the tagged literal syntax, which you can fiddle with to read those objects (but good luck reading that getting the "original" object back), so you can't really call them unreadable objects any more

sebastiano.barrera_cl20:09:14

Ok, I understand it more now

david.folkner20:09:33

#js {} also is a Javascript interop to say its a javascript object not a clojure one.

sebastiano.barrera_cl20:09:35

But those can't be used like cljs maps right?

sebastiano.barrera_cl20:09:50

sorry, i'm talking about #object [...]

sebastiano.barrera_cl21:09:08

I know about js->clj and clj->js, that's a little clearer

sebastiano.barrera_cl21:09:30

they convert into one another... but there's nothing like object->clj, right?

hiredman21:09:45

#object [] doesn't tell you anything about the object except that there is no custom printer defined for it

david.folkner21:09:51

The #object I've always thought was a js object. So you had to use the js interops like (.func Obj_name)

david.folkner21:09:22

But I think his answer is better.

hiredman21:09:41

so you cannot use it to answer any question about the object except "does this have a custom printer defined for it"

sebastiano.barrera_cl21:09:15

Ok... So, if I understand correctly, a JS object is printed as #js ... if its prototype is Object, while objects from other constructor are printed as #object [Constructor ...]

sebastiano.barrera_cl21:09:23

And there is no other (practical) difference

sebastiano.barrera_cl21:09:28

Does this ring true?

sebastiano.barrera_cl21:09:45

last question, if I can bother you two minutes longer:

sebastiano.barrera_cl21:09:26

if I have a JS object obj, I can access its property name as obj.name... CLJS-side, it looks like #object [Person ... ] but how do I get its name? (.name obj) doesn't seem to work (it looks for a function named cljs.user.obj.name)

skuttleman21:09:17

(.-name obj)

sebastiano.barrera_cl21:09:31

That's a particular CLJS syntax right? not a reader macro

lilactown21:09:04

(.foo obj) calls a method called foo. It compiles to: obj.foo()

lilactown21:09:38

(.-foo obj) accesses the property foo. it compiles to: obj.foo

sebastiano.barrera_cl21:09:11

damn, how did I miss that in the cheatsheet

sova21:09:26

error as in first blok*

michael74021:09:52

can I use clojure to implement a java interface, then generate a jar, then have a foreign java library use that jar, as a kind of plugin?

michael74021:09:10

reason i'm asking is because the docs say the resulting class might be "anonymous"

michael74021:09:44

if it's anonymous, and a foreign java library is expecting a class that implements a given interface, will that work out?

alexmiller22:09:24

yes, you could do this with deftype (would probably want to use the :load-ns option in deftype too)

alexmiller22:09:32

deftype will generate a known class, not an anonymous one

alexmiller22:09:40

I guess, also via gen-class

michael74022:09:23

thank you, @alexmiller!