Fork me on GitHub
#beginners
<
2020-03-01
>
Luis C. Arbildo03:03:07

Hi How can I know when use :as or :default in require in clojurescript?

penryu11:03:22

@UUGUA0MGQ I haven't seen :default mentioned in relation to require except with shadow-cljs, so you might check over in #shadow-cljs

Johannes Lötzsch13:03:46

(:require ["module-name" :default defaultExport]) is the same as ES6 import defaultExport from "module-name"; You find it at https://shadow-cljs.github.io/docs/UsersGuide.html#_using_npm_packages

EmmanuelOga08:03:19

what's a nice way to convert a sequence to a map? Suppose I have a seq (1 2 3 4) and I want to return a map of transforming each value {1 (tx 1) 2 (tx 2) 3 (tx 3) 4 (tx 4) }

EmmanuelOga08:03:50

I can think of a few ways passing through intermediate values, like building lists like ([1 (tx 1)] [2 (tx 2)] ...) and then converting the list to a hash-map, but it feels wasteful

EmmanuelOga08:03:12

I could use transducers, but I'm sure I'm missing a simple way

kelveden08:03:08

Something like: (->> your-seq (map #(vector % (tx %))) (into {})) . Trying to do this on phone so might be a bit shonky.

kelveden08:03:49

So similar to your first suggestion.

kelveden08:03:19

No neater way springs to mind.

kelveden08:03:22

You could use a reduce but I'm not sure it'd be any more readable.

EmmanuelOga08:03:54

(let [c '(1 2 3)] (zipmap c (map #(* % % %) c)))

EmmanuelOga08:03:30

oh internally it is implemented with assoc and loop/recur

EmmanuelOga08:03:38

I thought it was gonna be something more fancy 😛

kelveden08:03:46

Ah yes, I forgot zipmap. That's what I get for trying to code when I've just woken up. 😀

😁 4
hindol09:03:08

How about (into {} (map (juxt identity f) '(1 2 3 4)))? f is your transformation.

penryu11:03:05

I think I like (zipmap your-seq (map tx your-seq)) for this.

penryu11:03:27

Like you had above

Phil Hunt13:03:58

Hi. I’ve played a bit with Clojure and know some Common Lisp. Does anyone have a view on whether the ClojureScript debugging experience is notably better than those nasty Java stacktraces you get in Clojure.?

Johannes Lötzsch13:03:53

Writing pure functions and directly evaluating them from Repl (or Editor) can provide a workflow making debugging in most cases simple. Usually it's not needed, but one can use the JS-Debugger of the Browsers Dev-Console. When logging debug-data, I recommend using the dev-tools-plugin in combination with js/console.log over prn, this allows to introspect nested datastructures very easily. If you like Javascript stacktrackes is your choice, but one can learn to read them and Chromium allows to click on line-numbers where the error occured and you see a marker directly in your cljs code — I think that's quite convenient

Phil Hunt13:03:59

Thanks for the quick answer. That sounds like it might be OK. I think I’m a bit spoiled from hacking around in CL with Slime/Emacs, but actually getting paid means dealing with JS and maybe Java codebases :)

Phil Hunt13:03:20

I’m thinking IntelliJ / Cursive might work better in a mixed environment, but I’m sort of liking Emacs now I’ve got the basics down.\

Phil Hunt15:03:33

Hmm well, I had a bit of a play with cljs ... tried boot and deps, tooling feels very JS (ie in a state of flux)

Aviv Kotek13:03:33

Hey, i'm having issues with loading files in /resourceswhile doing lein uberjarand running the jar I get FileNotFoundException. I have followed this guide https://stackoverflow.com/questions/8009829/resources-in-clojure-applications?rq=1 and using .getPath (io/resource "rsa") to load file. this works right locally (using stuart.sierra.component) but on prod I get: Caused by: .FileNotFoundException: /usr......./-standalone.jar!/rsa (No such file or directory) What could be the issue with the location of the file?

seancorfield17:03:51

You can run jar tvf /path/to/the-standalone.jar to verify what files are in your JAR file. That will confirm whether or not your rsa file is in the JAR. If it isn't in the JAR, you need to look at how your project.clj file is set up @aviv

Aviv Kotek07:03:04

Hey, I find it in the jar, weird.

Aviv Kotek08:03:12

Solved this by changing .getPath to clojure str method.

Baris Aydek16:03:04

Hi. I want to write specs for my map type. In the map I have two types ::a and ::b

{ ::type ::a, ::key1 1, ::key2 2 }
for type ::a I want ::key1 and ::key2 to be required. And for the type ::b
{ ::type ::b, ::key1 2, ::key3 3}
I want ::key2 and ::key3 to be required. I know i can write spec as a simple predicate function, but then I lose ability to use macros like s/keys. What I came so far:
(s/def ::mytake
  (fn [m] (let [type (::type m)] 
             (or 
               (and 
                 (= type ::a) 
                 (every? #(contains? m %) [::key1 ::key2]))
               (and
                 (= type ::b)
                 (every? #(contains? m %) [::key1 ::key3])))))) 
And when I test with
(s/valid? ::mytake {::type ::a, ::key1 1, ::key2 2})
(s/valid? ::mytake {::type ::b, ::key1 1, ::key3 3})
It works. But is there any better way?

Sam Heaton21:03:29

Hey folks. Scratching my head trying to get this function to work.

(defn table-strings->numbers [maps]
  (map (fn [x] update x :value (js/parseInt "25")) maps))
maps is a PersistentVector that contains PersistentHashMaps. I want to ultimately replace all of a specific value in each hashmap with the integer equivalent. The use case is sorting data that came over JSON and sorting the string representations of different numbers of digits does not work so well. However I can't get to first base - even just using (js/parseInt "25") as a placeholder to test the function but I am just getting back nil for the whole thing. Same whether I use update or assoc. Any clues?

Eddie21:03:11

You want to call update, right? I believe you are looking for the following

(defn table-strings->numbers [maps]
  (map (fn [x] (update x :value (js/parseInt "25"))) maps))
Simply missing some parens.

👍 4
Eddie21:03:20

Or use the syntax sugar for anonymous functions

(defn table-strings->numbers [maps]
  (map #(update % :value (js/parseInt "25")) maps))

👍 4
Sam Heaton21:03:58

I sent a reply but it’s disappeared. I can see here that I didn’t call update as a function and missed the parens. Thank you!

Sam Heaton21:03:39

This is ClojureScript btw

seancorfield23:03:28

@include (following on from #clojure ) What editors are you most used to already?

include23:03:45

@seancorfield so during the day I am more a vim guy but I can work with spacemacs with evilmode on

seancorfield23:03:50

OK, cool. So the Emacs portion of Brave & True isn't too much of an obstacle for you 🙂 and there is good support for Clojure in vim I believe (although I'm not a vim guy so I'd point you to #vim for assistance there! 🙂 )

include23:03:58

anyways I found intelij+ cursive also a good experience for noobs.

seancorfield23:03:29

Yup, I hear that a lot. I'm an Atom/Chlorine person these days (after years of Emacs).

include23:03:58

humm I thought Atom was slow.

include23:03:48

does Atom/Chlorine has something like parinfer?

seancorfield23:03:05

Sure. I have both parinfer and paredit installed in Atom.

seancorfield23:03:45

Atom is "slower" than Emacs but I find it fast enough... certainly it's usable for our 90,000 lines of Clojure at work 🙂