Fork me on GitHub
#beginners
<
2018-12-20
>
David Pham09:12:58

Hello everyone :) I would be interested to know if there exist a guide to show how to share local lib in CLJS?

jakuzure13:12:26

hello, I mostly got down the basics of clojure, but I'm a bit lost at how to continue. I don't have many ideas for fun projects, but if I do come up with one I usually can't really implement it. any tips?

jakuzure14:12:02

@clojuregeek thanks very much, I'll have a look!

byrongibby16:12:47

Hi. A Java class method won't accept a reified Java interface on account of (it would seem) the interface having type "reify" and thus not having the type of the Java interface. What can I do to solve this?

jakuzure16:12:48

I'm trying to come up with a way to check which number in a list my number is closest to. like if I have 3.54 and my list consists of (2 4 6 etc.) how can I find out it's closest to 4 in a functional way? I keep coming up with loops which seems too imperative

chrisulloa16:12:24

map distance of 3.54 to every element in list, keep the minimum?

dpsutton16:12:26

@shin you can do something like the folowing:

(let [x 3.54
      values [2 4 6]
      distance (fn [z] (.abs js/Math (- z x)))]
  (->> values
       (map (juxt identity distance))
       (sort-by second)))

chrisulloa16:12:51

(first (sort-by #(abs (- 3.54 %)) [1 2 3 4]))

dpsutton16:12:55

you could also use a min-key application

āž• 8
Lennart Buit16:12:16

dpsuttonā€™s impl is using the JS runtime, be wary of that ^^

chrisulloa16:12:32

(defn abs [n] (max n (- n))) is what i used

dpsutton16:12:42

yes thanks. i had a cljs repl up right now

Lennart Buit16:12:19

not sure what the spread js/jvm users is actually

madstap16:12:17

Using (Math/abs ,,,) works in both clj and cljs

parens 4
Lennart Buit16:12:46

yeah, but (.abs js/Math ā€¦) certainly doesnā€™t šŸ˜›

chrisulloa16:12:05

min-key looks a lot better for this (apply min-key #(Math/abs (- % 3.54)) [-3 1 4])

āž• 4
Lennart Buit16:12:15

In core async, there is put! and >!! right, one being ā€œfire and forgetā€, the other being blocking if I understand correctly. What would be a downside of using put! except for the obvious ā€œnot knowing whether the message was processedā€?

Alex Miller (Clojure team)16:12:57

none - put! is considered lower level (>!! is built on it) but either is fine

Lennart Buit16:12:44

Right, so if I want ā€œfire and forgetā€, put! is also fine. Thanks!

Alex Miller (Clojure team)16:12:27

yep, thatā€™s what itā€™s for

hoopes20:12:01

if i do lein run, can i connect to it with lein repl :connect with the correct settings? I have a .nrepl-port file, but i'm getting Connection refused - something in the project.clj ?

seancorfield20:12:22

lein run does not start an nREPL server (unless your app starts one explicitly).

jaide20:12:05

Are you sure? Iā€™m pretty sure it does

jaide20:12:41

Nevermind. I confused lein run with lein repl. My mistake! šŸ˜ž

hiredman20:12:24

lein run doesn't run an nrepl server, it just runs your code

hoopes21:12:24

is there a way to attach to the running process with a repl? (or, in dev, how would i start the repl server?)

hoopes21:12:59

basically, i want my process to be running in a container off in my "bunch of docker container servers" stuff, and if needed, connect up a repl and operate via that within the context of the running app

hoopes21:12:04

(does that even make sense?)

seancorfield21:12:27

@hoopes Another approach to consider is to provide JVM options when starting your process that tell Clojure itself to start a Socket REPL on a specific port, and then you can either connect via telnet/netcat or use Unravel to connect (which provides a much nicer REPL experience).

seancorfield21:12:46

It depends whether you actually need nREPL in the mix.

hoopes21:12:05

awesome - thanks @trailcapital and @seancorfield - much appreciated

enforser21:12:16

I wish the socket REPL had a little bit wider support. I do almost all of my development with apps deployed in docker - then use vim-fireplace to connect to the open nREPL, which doesn't support socket REPL. Maybe one day!

seancorfield23:12:46

The Unrepl folks are working with the nREPL folks now so that may well become a reality -- I believe their goal is to allow an nREPL client to automatically upgrade a Socket REPL to nREPL...

šŸ‘ 4
dhruv122:12:06

hello, i have a question regarding namespaced hash-maps. looks like the namespace for a hash-map is only applied to the top level keys not applied to the child hash-maps.

dhruv122:12:13

an example: (get-in #:foo{:bar :eh} [:foo/bar])

dhruv122:12:00

(get-in #:foo{:bar :eh} [:bar]) ;; nil

dhruv122:12:02

goo so far

dhruv122:12:16

(get-in #:foo{:bar {:car :eh}} [:foo/bar :foo/car]) ;; nil

dhruv122:12:34

(get-in #:foo{:bar {:car :eh}} [:foo/bar :car]) ;; :eh

dhruv122:12:35

are the keys for a nested hash-map not namespaced on purpose or am i not using this feature correctly?

Alex Miller (Clojure team)22:12:54

just want to step back a second to make an important point. When you say ā€œnamespace for a hash-mapā€, hash maps do not have a namespace - this is syntax only. #:foo{:bar :eh} is the identical value to {:foo/bar :eh} - they are just two different syntaxes.

Alex Miller (Clojure team)22:12:58

but yes, the syntax only extends to the top level keys in the map

dhruv122:12:42

ah i see. thank you, i was missing the point "hash maps do not have a namespace"

sova-soars-the-sora22:12:50

i want to do POSTs from my CLJS app...

sova-soars-the-sora22:12:03

What's the best cure for my ailment?

sova-soars-the-sora23:12:10

Hi i has another question that is n00b worthy

sova-soars-the-sora23:12:24

{:status 200 :session (assoc session :uid user-id)} ... I want to associate more than just :uid user-id ... for example, i'd like to add :uid user-id and :auth-key $yag and :login-time 7164516721 .... Can I simply assoc the whole map?

andy.fingerhut23:12:32

You can make a single call to assoc that takes an arbitrary map, and one new key-value pair to add to it, or a list of several new key-value pairs to add.

andy.fingerhut23:12:53

If you have two arbitrary maps you want to combine in some way, merge or merge-with are worth looking into

andy.fingerhut23:12:54

I spoke imprecisely there - assoc does not take a list, but you can give multiple arguments with multiple key1, value1, key2, value2, etc. arguments

sova-soars-the-sora23:12:26

Oh that's just as good

sova-soars-the-sora23:12:32

Thank you. I was getting wrong number of args

sova-soars-the-sora23:12:28

maybe merge is the right move

Alex Miller (Clojure team)23:12:05

If any of those are conditional, then cond-> is great for that