Fork me on GitHub
#beginners
<
2019-01-07
>
adc1701:01:52

thanks @seancorfield @sova this looks like a handy resource. i like that templates exist for a bunch of libraries so you can quickly get a taste of their apis.

Ryan Haskell-Glatz03:01:54

Hey folks! I've seen way too many Rich Hickey tech talks and now I'm finally getting around to trying out Clojure / ClojureScript 😄

rich 5
Ryan Haskell-Glatz03:01:14

I'm trying to do the Quick Start (at https://clojurescript.org/guides/quick-start) on my Mac, and was wondering if anyone could help me out with an issue with cljs.main

seancorfield03:01:30

I don't do any cljs but I can try...

Ryan Haskell-Glatz03:01:24

Haha, thanks @seancorfield! I'm just running clj --main cljs.main --compile hello-world.core --repl and I'm getting a stack trace that starts with:

Ryan Haskell-Glatz03:01:26

Exception in thread "main" java.io.FileNotFoundException: Could not locate cljs/main__init.class, cljs/main.clj or cljs/main.cljc on classpath.

Ryan Haskell-Glatz03:01:02

And let me know if I should checkout the #clojurescript channel instead. I'm a beginner here in many senses of the word 😂

seancorfield03:01:54

What's in your deps.edn file? In the folder where you are running that command.

Ryan Haskell-Glatz04:01:03

I put my deps.edn in the wrong folder :man-facepalming:

dpsutton04:01:25

Rubber duck debugging is real :)

🦆 20
seancorfield04:01:50

Glad to help 🙂

seancorfield04:01:11

I was just stepping through the instructions on that page to make sure I'm on the same page as you

seancorfield04:01:33

Right now I'm on my Windows laptop and using WSL with Ubuntu … and I got the cljs welcome page in MS Edge and was able to run the js/alert in the WSL REPL and, sure enough, it popped up in Edge 🙂

Ryan Haskell-Glatz04:01:58

Things are working great now, thanks again!

henrik19:01:37

As a next step, you might try your hand at sticking Figwheel in there. It's a great tool for interactive programking. https://figwheel.org/ I personally tend towards Shadow Cljs, since it excels at a few things, such as making NPM package usage a ton easier. http://shadow-cljs.org/

seancorfield04:01:37

One day I'll get back into cljs -- I tried it about four years ago and decided to stick to Clojure and backend stuff, but it's improved a lot since then.

Ryan Haskell-Glatz04:01:52

A little background about me: I recently had a pleasant experience learning Elm coming from a Java/C#/JS background, and I'm looking for another mind-expanding thing to learn. Functional programming is really neat, and it simplified the way I think about building stuff!

👍 5
seancorfield04:01:22

Elm is pretty slick, yes. Evan really cares about how the experience works for folks not coming from an academic/Haskell sort of background and he's done a great job with it.

seancorfield04:01:09

You'll find folks here are always eager to help. You won't be put off by the JVM and Java ecosystem around Clojure so that's good.

seancorfield04:01:37

(I gotta go help with dinner but I'll be back later)

👍 5
seancorfield05:01:55

(back, just in case anyone else is around with questions)

Lennart Buit07:01:27

what, the JVM really rocks, how can that put people off

seancorfield07:01:58

@lennart.buit There are a lot of people who detest the JVM -- whether their complaints have basis or not is another matter.

Lennart Buit07:01:34

There is always people disliking things for sometimes valid reasons

Lennart Buit07:01:16

What I like about the JVM, is that any improvement of the JVM affects everyone equally without programmer intervension.

Lennart Buit07:01:54

Oh we found a better way to determine that objects don’t escape the stack? Here, have a slightly better optimizing JVM!

seancorfield07:01:44

Indeed. I've been full-time on the JVM for almost twenty years now. It's such a rock solid platform and so ubiquitous that I can't imagine giving it up. I love that I can be polyglot on the JVM and that so many languages have JVM-compatible versions, even when the original/reference implementation of that language is non-JVM. Sure, there are valid complaints: startup time, stacktraces… but there are workarounds for those and, to be honest, they're not a problem for "most" projects.

seancorfield07:01:10

But if you're coming to the JVM from a background of other languages, those complaints can seem pretty major.

Lennart Buit07:01:44

Right, as is often with software engineering, everything comes with a decent set of tradeoffs that can make or break something for your particular usecase ^^

Lennart Buit07:01:41

hah, the Graal impl of Python does support 3

mss15:01:12

if I wanted to call clojure.string/escape to escape a single quote in my text, what’s the proper way to reference a ' in the character map argument?

dpsutton15:01:51

(str/escape "hello guv'nuh" {\' "*"})

mss15:01:42

maybe it’s bc I’m on cljs but getting an error that \' is an unsupported escape character

dpsutton15:01:54

ah. didn't realize that was cljs.

mss15:01:07

yeah didn’t figure there would be a difference but there seems to be in this case

mss15:01:24

will try the cljs group, thanks for the help!

dpsutton15:01:31

this works for me in cljs ?

dpsutton15:01:46

what is your repl environment?

mss15:01:22

cljs 1.10.439 via figwheel 0.5.17

dpsutton15:01:25

in emacs? what's your environment

dpsutton15:01:41

in CIDER with figwheel this works for me as well as from a terminal

[dan@fedora clojurescript]$ script/repl
Clojure 1.9.0-alpha17
user=> (require '[clojure.string :as str])
nil
user=> (str/escape "hello guv'nuh" {\' "*"})
"hello guv*nuh"
user=> 

dangercoder15:01:47

Moving this to OT..

mss15:01:48

running in cursive/intellij

mss15:01:38

will try posting in the cursive channel

dpsutton15:01:44

@mss when its in a string you need two \s. "\\'"

dpsutton15:01:20

it's annoying but the slash is the escape character, so when you actually want that rather than the sigil you need to use two of them

mss15:01:12

yep that worked!

mss15:01:23

makes sense

mss15:01:45

really appreciate the help

JanisOlex19:01:07

Hi, people. Have qustion. What is Clojure take on different established Java technologies, like all the EE stuff and Spring? Those technologies rely heavy on annotation processing and different convention-over-config deployments etc. How Clojure can tackle into that? When some time ago I tried to figure out how one work with annotations, it doesn't seem to be good interop.

noisesmith19:01:51

aside from using metadata to create annotations which I already posted below, you can also use a small java class that uses clojure.lang.RT to look up Clojure code for its internal implementation - this is simpler than all-clojure interop sometimes

noisesmith19:01:17

example here - java code implementing the methods used by jsvc, using DI to find clojure namespaces to invoke via properties https://github.com/noisesmith/clj-jsvc-adapter/blob/master/src/java/org/noisesmith/Cljsvc.java

Vincent Cantin19:01:50

Clojure has meta-data, they are similar to annotations with the difference that you can dynamically build new values out of original ones with different meta-data.

noisesmith19:01:29

you can actually use metadata on forms to create annotations

noisesmith19:01:41

it's true that metadata can be used like annotations, but this doesn't help Spring, which only knows how to use your object via its annotations

Vincent Cantin19:01:10

You are right, I misread his question.

Ben Grabow19:01:07

I have a testing question. #testing looks a little dead so maybe I can find some help here? I have some test data (input and expected output) in a text file. I would like to parse the examples from this file and somehow get clojure.test to tell me which examples are failing. Should I try to do something like (doseq [...] (deftest ...)) or (deftest ... (are [x y] (= x y) ...)? I am also hoping to get the test runner to print the actual data from the files, rather than printing the code verbatim that I used to pull data from the files. E.g. I want (deftest file-parsing (is (= (parse "input-file.txt") (slurp "output-file.txt")))) to print the output file contents as the expected value rather than to print literally "(slurp \"output-file.txt\")".

Ben Grabow20:01:42

@seancorfield helped me figure out what I was trying to do in #testing if anyone is curious: https://clojurians.slack.com/archives/C08LK2DH7/p1546891035001000

eboersma20:01:08

I'm writing a library that uses https://github.com/dm3/clojure.java-time to do date/time management. When I load that library in, I get a bunch of warnings like so:

WARNING: contains? already refers to: #'clojure.core/contains? in namespace: net-promoter-score.nps-dates, being replaced by: #'java-time/contains?
[Other similar warnings]

eboersma20:01:41

Is there a way to make java-time play nicely with the standard ns?

seancorfield20:01:37

@eric.boersma Are you using :use or :require .. :refer :all? That's where you're introduce the conflicts that Clojure is warning you about. Much better to do :require .. :as .. and refer to the library functions through an alias.

eboersma20:01:50

Ah, I'm using a use

seancorfield20:01:05

(:require [java-time :as jt]) and then (jt/contains? ..) etc

seancorfield20:01:00

The warnings come from use which refers in all the symbols (as does require .. :refer :all) and you should avoid those as much as possible.

eboersma20:01:12

Thanks, that's good to know

eboersma20:01:27

Is the idiomatic way to use :as when importing a library in pretty much all files?

eboersma20:01:23

Thanks, that's a handy resource, I hadn't seen it before

nikola22:01:31

How can I see which parts of a function are taking most of the execution time? https://github.com/nikolakasev/clojure-playground/blob/advent-of-code-2018/src/advent_of_code_2018/dec11.clj for example, which parts are slow and have potential optimizations to be made?

Alex Miller (Clojure team)22:01:18

You’re using boxed math everywhere, which is about 100x slower than primitive math

Alex Miller (Clojure team)22:01:06

I don’t have time to go through the details, but that’s usually a good place to start (assuming there’s not an algorithmic complexity issue)

sova-soars-the-sora22:01:06

how can i update a nested value in an atom ? assoc-in?

jstaab22:01:49

update-in is nice if it's a more complex operation

jstaab22:01:06

(swap! a update-in [:x :y :z] inc)

sova-soars-the-sora22:01:52

i don't want to increment it, i just want to set it to a new value i have determined

dpsutton22:01:08

ignore the atom

dpsutton22:01:20

figure out how to do it whatever value is in the function

dpsutton22:01:48

so what does the value look like initially and what would you like it to look like

noisesmith22:01:54

I'd suggest using update-in, even if your function ends up being (constantly new-value) because with a compound type like a map here, using reset! to set a key is a race condition

5
noisesmith22:01:52

when you use update-in, changes to other keys come in automatically, if you use reset! with the whole new value, you can wipe out all concurrent changes

noisesmith22:01:00

and if there's any possibility of two threads touching the same key for update, use compare-and-set and check that the key doesn't change before assigning the new value

noisesmith22:01:30

(or swap! using a function that uses the key's contents instead of blindly overwiriting)

5
Alex Miller (Clojure team)22:01:14

maybe not the place to *start* :)

Alex Miller (Clojure team)22:01:22

surely there is some better writeup of using primitives somewhere but I do not have it at my fingertips

nikola22:01:25

I think it's algorithmic complexity of my naive implementation that is slow 🙂

nikola22:01:34

I'll check it out, thanks

sova-soars-the-sora22:01:48

;seek map and update rating on serverside
    (let [seek-tv-state (find-tv-item pid)
          seek-cm-state (find-cm-item pid)]
         (println "seek-tv " seek-tv-state)
         (println "seek-cm " seek-cm-state)
         (if (= nil seek-tv-state)
           (do
             (swap! nf-comments update-in [seek-cm-state :number-of-ratings] (constantly num-ratings))
             (swap! nf-comments update-in [seek-cm-state :ratings-total] (constantly total-score)))
          ;else
           (do
             (swap! tv-state update-in [seek-tv-state :number-of-ratings] (constantly num-ratings))
             (swap! tv-state update-in [seek-tv-state :ratings-total] (constantly total-score)))))

sova-soars-the-sora22:01:03

... my swap! lines give me the error " Key must be integer "

noisesmith22:01:51

that means it's a vector and not a hash-map usually

sova-soars-the-sora22:01:20

it's a vector of maps

dpsutton22:01:41

what is seek-cm-state

sova-soars-the-sora22:01:45

and i use seek-*-state to determine the array-index so-to-speak

dpsutton22:01:56

these need to be integers within the range of your vector

noisesmith22:01:20

also you can replace:

(do
             (swap! nf-comments update-in [seek-cm-state :number-of-ratings] (constantly num-ratings))
             (swap! nf-comments update-in [seek-cm-state :ratings-total] (constantly total-score))
with
(swap! nf-comments update seek-cm-state assoc :number-of-ratings num-ratings :ratings-total total-score)

mario-star 5
noisesmith22:01:50

(once you adress the vector vs. map issues we are discussing, of course)

sova-soars-the-sora22:01:52

i don't know what the issue is

sova-soars-the-sora22:01:13

"Key must be integer" ... it is

sova-soars-the-sora22:01:43

works nicely now ^_^

dpsutton22:01:05

was it an integer?

duncanmak22:01:18

@sova there’s also (nil? x) instead of (= nil x)

duncanmak22:01:50

and if you really want to write out the else, then you can alway use cond

vincentdm22:01:09

Hi! I am trying to extend a protocol for an integer in ClojureScript (browser). What should I use to name the type? I have tried Number, Int, Integer, Num, cljs.core/Integer,... but none of them seem to exist.

vincentdm22:01:42

I want to be able to call the protocol on numbers defined like this: (def some-number 5) (no decimals)

vincentdm22:01:37

I tried finding out by doing (type 1), but it returns #object[Number], and I have no idea how to use that information.

Lennart Buit22:01:57

why do you want to extend integer?

Lennart Buit22:01:19

not trying to be a smartass here, but it is generally ill advised to extend such fundamental objects

vincentdm22:01:40

I want to validate forms, and I use a special constant (`-1`) to indicate a missing value for ids. I want to be able to call the same missing? method on any field, and have the polymorphism help me figure out how to determine whether a real value is missing (e.g. for keywords I use another constant). I do not use nil for missing because it doesn't play nice with the form controls and easily lead to bugs (e.g. using empty string for missing text fields instead of nil avoids null exceptions).

vincentdm23:01:19

It's for a rapid mockup, so I don't really care about the code being idiomatic; I just need to know the word to type so I can extend js number 🙂

Lennart Buit23:01:11

Don’t have intimate knowledge but this: https://cljs.github.io/api/cljs.core/extend-type seems to have your answer

vincentdm23:01:54

Great, thanks! I was looking for an overview like that, but didn't find it. (I am using extend-protocol). Thanks!

vincentdm23:01:02

Turns out it's lowercase number

Lennart Buit23:01:34

right, same deal for extend-protocol I guess