Fork me on GitHub
#beginners
<
2017-07-15
>
zlrth04:07:43

I have some utility functions i’d like to use in my cljs repl no matter what namespace i’m in. i defined them in cljs.user. how do i make sure i can use them after switching to another namespace?

noisesmith04:07:48

the least complex thing is to run (use 'cljs.user) after switching namespaces - there's tools for jvm clojure that make certain definitions ubiquitous but honestly I think they are weird and they don't exist in cljs

noisesmith04:07:01

s/tools/hacks/

zlrth04:07:50

really? interesting. thanks. i guess i can set up an emacs macro to enter that or something. one of the things i find myself doing is all the time is (.keys js/Object lib). how do cljs devs explore libraries? i do .keys, .-defaultProps, and .-propTypes, but i find myself typing the same stuff over and over.

qle-guen07:07:57

hi, is there a way to act on the coll itself when using reduce? Like remove elements from it?

qle-guen08:07:21

I'm looking at transducers but that doesn't seem to be the right thing

akiroz08:07:08

@qle-guen If you want to remove stuff from a collection, you probably want to look at filter

qle-guen09:07:53

@akiroz well remove and filter are a bit similar aren't they?

akiroz09:07:44

Oh sorry, I misunderstood your question. Are you trying to implement remove using reduce?

qle-guen09:07:32

I want to write some kind of transducer of it so I can use reduce

akiroz09:07:36

what does that function actually do?

qle-guen09:07:05

it accumulates prime numbers with f

qle-guen09:07:11

but nvm I think I got it

qle-guen09:07:38

I actually just need to remember the last prime with a stateful transducer

qle-guen09:07:40

(ie (transduce prime-slieve-xf + 0 (range 2 10)) should output the sum of the primes from 2 to 9

qle-guen09:07:53

nvm can't work because I need to remember all the old primes so I'll need to store them

leonoel09:07:38

it works with a hand-crafted stateful transducer, but it may not be a smart use case for transducers

leonoel09:07:11

basic reduce looks more elegant to me

leonoel09:07:30

the lazy version is slightly longer

qle-guen10:07:14

@leonoel yep, I figured that this is not a transducer use case

qle-guen10:07:28

and that'd be a bad algorithm to find primes anyway

qle-guen10:07:00

I thought it'd be trivial but it's not, 2 million is big

dpsutton14:07:59

CIDER has an explore namespace that you might like. I'm not sure that this is hooked up for cljs though. give that a shot

zlrth15:07:47

@dpsutton works in cljs namespaces! thanks. i see how underdocumented my code is… curiously, doesn’t seem to work for cljsjs libraries. i require cljsjs.d3, but it isn’t picking it up.

dpsutton15:07:26

i think the cljs brains of that are outside of the standard cider-nrepl middleware in a project called cljs-tooling

zlrth15:07:38

ahh i’ll look that up. thanks again.

tech_hutch15:07:51

I have a question about core.async's go. I know it returns a channel, but what if the body returns a channel? Does it wrap the channel in a channel, or does it do like JavaScript's async functions, which only wrap in a promise if the return value isn't a promise?

noisesmith15:07:34

it does no automatic lifting - if the go block explicitly returns a channel, you get a channel that returns a channel

tech_hutch15:07:16

Okay, thanks.

tech_hutch15:07:40

I'm used to promises and async/await in JavaScript. It seems hard to do the same things in CLJS.

noisesmith15:07:19

well - you can actually use those things from cljs

noisesmith15:07:45

interop is easy, it's one of the good things about the language

tech_hutch15:07:47

Yeah. And I have promises extended to work as channels. It's just hard to wrap my head around how to write the similar things I write in JS, with regards to async/await.

noisesmith15:07:47

yeah - CSP is its own thing with its own rules, but those rules actually have nice consequences

noisesmith15:07:51

(if you use it right)

tech_hutch15:07:17

Specifically, I'm trying to make a function like this in CLJS:

const runShowPiece = async (client, msg, type, name, obj) => {
  if (type === "command") await sendHelpMessage(client, msg, name);
  return sendDebugMessage(client, msg, type, name, obj);
};

tech_hutch15:07:41

(Crap, I forget how code blocks work in Slack. I'm used to Discord)

tech_hutch15:07:18

This is what I have so far:

(defn run-show-piece [client msg type name obj]
  (take-as-promise!
   (go
     (if (= type "command")
       (<? (send-help-message client msg name)))
     (send-debug-message client msg type name obj)))
(I'm using the cljs-promises library.) (Both of those functions still return promises.) If I do this, though, then the promise that send-debug-message returns will be wrapped in a channel, which becomes a promise. I could use <? to await it, as well, but then that makes the function not return until it finishes, right?

tech_hutch15:07:59

Well, I guess the go (wrapped in take-as-promise!) would make the function return a promise right away, if I understand how that works

tech_hutch15:07:07

If anyone knows of any CLJS libraries that use promises a lot, that I could look at, that would probably help, to see what they do.

donyorm16:07:15

I'm a little confused about this kata on codewars: https://www.codewars.com/kata/55b3425df71c1201a800009c/train/clojure My code isn't outputting the right answer, but even when I try to do it manually it doesn't come out right. How does codewars get the average of [15, 47, 17, 32, 17] to equal 38? (first test case in bottom right code entry space).

noisesmith16:07:06

that's just the minutes field

noisesmith16:07:16

each one is hours/minutes/seconds encoding of a time

donyorm16:07:50

@noisesmith yes, but am I right that I'm supposed to take the average of the minutes field and put that in the middle of the result. So the result looks like Average: <average of hours>|<average of minutes>|<average of seconds> or am I missing something? It's not making sense to me.

noisesmith16:07:27

why would you average the fields instead of the times?

noisesmith16:07:55

if I read it correctly, you need to break hh|mm|ss into a time value, average the time values, then convert back to hh|mm|ss

donyorm16:07:06

right, that's what I'm missing

donyorm16:07:10

ok, sorry about that

noisesmith16:07:14

at least that seems like the only way the numbers would make sense

donyorm16:07:26

yeah, I knew I was thinking of it the wrong way