beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
sova-soars-the-sora 2021-06-04T00:00:43.319300Z

thanks for the curl tip. more head scratching

sova-soars-the-sora 2021-06-04T00:03:42.319500Z

In my server.clj I have (route/resources "public") in my non-authenticated routes

sova-soars-the-sora 2021-06-04T00:04:11.319700Z

folders img and js both live there... img will load assets, js will give me the login page to the site o_O

sova-soars-the-sora 2021-06-04T00:11:31.319900Z

I figured route/files ought to help serve js assets as well... maybe I need to add a specific line for that?

seancorfield 2021-06-04T00:11:42.320100Z

You mentioned a CDN above?

seancorfield 2021-06-04T00:12:41.320300Z

Can you try curl directly against the server (without the CDN) and compare the results against the same calls through the CDN?

sova-soars-the-sora 2021-06-04T00:13:18.320500Z

yeah, I stopped using the CDN for now, to debug this...

sova-soars-the-sora 2021-06-04T00:13:31.320700Z

It just gives me the application login page, which is text/html of course

sova-soars-the-sora 2021-06-04T00:13:45.320900Z

so that makes sense, but why is it not serving /js/ files and instead acting like i'm in authenticated territory

seancorfield 2021-06-04T00:14:36.321100Z

And your JS and image folders are both under that public resources folder?

seancorfield 2021-06-04T00:15:18.321300Z

How are you applying the auth check in your code? Is it perhaps allowing various image file extensions through but not .js?

sova-soars-the-sora 2021-06-04T00:15:55.321500Z

good time for a sanity check as any.. šŸ˜„ yeah they are both under resources/public/ img | js

sova-soars-the-sora 2021-06-04T00:16:34.321700Z

hmm let me double check i actually uploaded them to the server d'oh

sova-soars-the-sora 2021-06-04T00:17:52.321900Z

omg i'm a fruitcake

sova-soars-the-sora 2021-06-04T00:17:58.322100Z

the assets weren't there! xD

sova-soars-the-sora 2021-06-04T00:18:01.322300Z

thanks Sean

sova-soars-the-sora 2021-06-04T00:18:16.322500Z

Haha sorry for the mixup. I appreciate you and your helpful nature! šŸ˜ƒ

seancorfield 2021-06-04T00:18:23.322700Z

:duckie:

šŸ¦¢ 1
sova-soars-the-sora 2021-06-04T00:18:42.322900Z

i plugged the plug into itself.. why is nothing turning on

šŸ’” 1
šŸ”Œ 1
EsakkiSundar Varatharajan 2021-06-04T02:13:47.325700Z

I've rich programming experience and getting started with Clojure. From the age we were taught Math, our brain is wired to write adding two numbers as (a+b). Now come to clojure it asks people to write (+ a b). Just imagine the strain people have to go through just to add two numbers. I have few queries below and would like your suggestion. 1) The learning curve seems to be too steep for people with prior programming experience, because programmer has to unlearn and re-learn. Could you pl let me know what business benefit Clojure is bringing to the organization? 2) Lets say a particular team in an organization choose to develop a system on Clojure. Team starts to see attritions due to higher pay package. Business wants to enhance the application. There is no resource in the market with Clojure. How can we deliver the project? 3) Is there any benchmark in run time comparison on the memory, cpu, disk io's etc that Clojure beats Java? 4) For people with prior programming experience and are frequently switching between Clojure and Java, what is your recommendation for them to be productive with both the language? 5) By adopting Clojure is there any benchmark that the time to market for bringing a new project has significantly improved when compared to other languages?

Jonas-Daima 2021-06-04T07:56:21.328600Z

To comment on the use of prefix over infix in Clojure. While it is a bit strange, to write (+ 1 2) instead ofĀ `(1 + 2)`, it is actually not as foreign as one might think. When we use a method in Java we usually call it using prefix notation. For example if we put a value into a map map.put("level", 3) the method put is prefix. Moreover, when we define a function in math we also write it is a prefix: plus(x,y)=x + y. To apply this function we would then write plus(1,2). Recognizing this, one does not need to relearn anything. One just needs to think of + like a function (or a method in Java), which it conveniently is. This became a lot longer than I meant it to be. While it didn't really answer any of your questions You can consider it an argument against the steep learning curve of Clojure.

2021-06-04T07:56:52.328800Z

I agree, i don't think (+ a b) is that hard to learn for an experienced programmer. Tons of math libraries use stuff like Math.pow(a,b) or Matrix.add(a,b) . I often find s-expression based math expressions easier to read, because the syntax is very clear and regular compared to the traditional notation with its precedence rules

2021-06-04T07:57:54.329Z

also i prefer (+ a b c d e) to a + b + c + d + e

2021-06-04T08:30:56.329200Z

1. There is no all round 'best' language, so it really depends on you business. Clojure is IMO really good for wrangling data, connecting services together, and exploratory programming. 2. The same could be said for any language i suppose. 3. In my experience Clojure is a little slower an memory hungry than Java, but faster to develop in, so it's worth the trade off to me. Usually bottlenecks can be fixed with interop, or writing parts in java 4. I didn't have any problem with switching. Knowing Java well helps my understanding of Clojure, knowing Clojure helps with programming in an immutable functional style in Java. 5. Hard to say, really depends on the type of project. As I said there is no all round 'best' language.

sova-soars-the-sora 2021-06-04T15:30:22.335300Z

1. The "business benefit" of Clojure is that I can look at code 6+ months later and quickly decipher or immediately know what it does. I cannot say the same for any other language.

kennytilton 2021-06-07T13:40:49.373800Z

" The learning curve seems to be too steep for people with prior programming experience, because programmer has to unlearn and re-learn." Really? So when I moved from Fortran to COBOL I was supposed to unlearn FORTRAN? Pro tip: don't hire these people you have in your imagination.

seancorfield 2021-06-04T02:19:35.325900Z

1. Depends on the programmer. Weā€™ve successfully cross-trained ColdFusion programmers to Clojure, but some really experienced Java developers seem to fail to learn Clojure. The more steeped you are in (Java-style) OOP, the harder I think FP in general is. The business benefits are many, however, and include a) ability to do more with fewer developers (because Clojure is a higher-level language) b) immutability and (mostly) pure functional approach means far fewer bugs around state or concurrency c) the language attracts a lot of really smart people who have produced some very advanced (open source) tech so you can ā€œstand on the shoulders of giantsā€

seancorfield 2021-06-04T02:21:04.326100Z

2. Not sure what youā€™re basing that on. Do Clojure devs get paid more than the ā€œaverageā€ XYZ dev? Maybe. But you can do more with less so a small team can do more than a large Java team (in general). Training up new developers in Clojure ā€” by which I mean genuinely junior folk ā€” seems to be a very productive approach for many companies.

seancorfield 2021-06-04T02:22:50.326300Z

3. Clojure is ā€œfast enoughā€ and is ā€œokā€ with memory so unless you are heavily resource-constrained and/or extremely performance-constrained, Clojure is a perfectly acceptable alternative to Java (and you can always drop down to raw interop for performance-sensitive code).

seancorfield 2021-06-04T02:23:37.326500Z

(the benefits of immutability often mean that you can get more throughput, more easily with Clojure than you might be able to achieve with Java based on reliability šŸ™‚ )

seancorfield 2021-06-04T02:24:47.326700Z

4. No idea. I havenā€™t used Java ā€œdailyā€ for a very long time. I went from Java to Groovy in the mid-ā€™00s and then to Scala and finally to Clojure a decade ago. And Iā€™ve never wanted to go back to Java.

seancorfield 2021-06-04T02:26:26.326900Z

5. The ā€œdo more with lessā€ has meant ā€” for us, at least ā€” that we can add/change features much faster than with previous tech (we have a small team of experienced Clojure devs so YMMY).

seancorfield 2021-06-04T02:27:05.327100Z

I hope thatā€™s useful feedback @esakki.sundar? Iā€™m sure other Clojure devs will have other opinions.

seancorfield 2021-06-04T02:28:22.327300Z

Certainly, Clojure is very ā€œalienā€ for dyed-in-the-wool Java developers, especially those of mediocre background/skill level ā€” Java provides a lot of guardrails and checksā€™nā€™balances that allow low-skill developers to be ā€œsuccessfulā€ writing large codebases in Javaā€¦

aratare 2021-06-04T06:11:37.327600Z

@esakki.sundar As someone who was (still is) quite heavily into Java, mostly due to its massive ecosystem, learning Clojure has been quite a ride. Indeed switching from (a + b) to (+ a b) will trip lots of people (myself included for quite some time), but now that Iā€™m used to Clojure, I actually prefer Clojureā€™s syntax over Java because it makes things so much cleaner and more straightforward. Though I wouldnā€™t classify my experience as ā€œunlearn and relearnā€ because itā€™s just me looking at a different way to programming. There is no law or rule to stop me from using what Iā€™ve learnt in Java in Clojure and vice versa. Itā€™s just that people who have learnt ā€œthe Clojure wayā€ like it enough to stick to it.

Erik B Good 2021-06-04T14:18:20.333Z

Hello, I was wondering how I could add the mathematical operators symbol to a list. Something like '('\ '+ '*) ? Then I could keep track of the operations I made on an initial number (for instance get the numbers of operations by using count on this list).

2021-06-04T14:35:07.334300Z

What are the main things someone switching from Clojure to ClojureScript should know? What are the main differences and key similarities?

alexmiller 2021-06-04T14:36:05.334900Z

https://clojurescript.org/about/differences

sova-soars-the-sora 2021-06-04T14:36:09.335100Z

pr-str and read-string are pretty important in cljs and were non-obvious starting out...

mathpunk 2021-06-04T18:38:59.340600Z

I'm trying to send messages to a datadog instance. I've proven that if from my shell I echo a well-formed message to >/dev/udp/localhost/8125 the message ends up where expected. But in a repl, something like (sh/sh "echo" (event {:title title, :text text, :tags tags}) ">/dev/udp/localhost/8125") has no effect (`event` is just a string in the expected datagram format), and when I tried to follow https://github.com/clojure-cookbook/clojure-cookbook/blob/master/05_network-io/5-11_udp.asciidoc#sec_network_io_tcp_client I must be misunderstanding what a 'socket' is, because when I try to connect one to localhost:8125 I get the message that it's already in use

2021-06-07T15:44:16.381700Z

can you share what worked in the end?

mathpunk 2021-06-07T16:32:11.382Z

it was pretty much exactly your doto suggestion

mathpunk 2021-06-07T16:33:49.382200Z

(import [<http://java.net|java.net> DatagramSocket
             DatagramPacket
             InetSocketAddress]))


(def dd-socket-address (InetSocketAddress. "localhost" 8125))

(def dd-conn (doto (DatagramSocket.) (.connect dd-socket-address)))

(defn send
  "Send a short textual message over a DatagramSocket to the specified
  host and port. If the string is over 512 bytes long, it will be
  truncated."
  [^DatagramSocket msg]
  (let [payload (.getBytes msg)
        length (min (alength payload) 512)
        address (InetSocketAddress. "localhost" 8125)
        packet (DatagramPacket. payload length address)]
    (.send dd-conn packet)))


#_(send (event {:title "e2e observability"
                :text "pulse check"
                :tags ["e2e" "test"]}))

2021-06-04T18:40:11.340900Z

shell/sh, despite the name, does not use a shell

šŸ¤Ŗ 1
2021-06-04T18:40:35.341200Z

so that ">" redirect is just a random input string to echo, and not actually a redirect

2021-06-04T18:41:02.341400Z

try (sh/sh "sh" "-c" (str "echo " ...)))

2021-06-04T18:41:24.341600Z

but it sounds like you could do this with spit

2021-06-04T18:41:44.341800Z

(spit "/dev/udp/localhost/8125" (event ...)) ā€¢ (fixed arg order)

mathpunk 2021-06-04T18:46:36.342Z

hm, elegant but no response

2021-06-04T18:46:59.342200Z

oh I flipped the arg order

2021-06-04T18:48:19.342600Z

what error did you get from the socket send?

2021-06-04T18:49:30.342800Z

I don't see how any of the objects/methods in the send example above could try to allocate an address (unlike TCP it's stateless and single ended)

mathpunk 2021-06-04T18:51:19.343Z

the error from spitting (fixing argument order) is /dev/udp/localhost/8125 (No such file or directory)

2021-06-04T18:52:24.343200Z

so the shell knows how to interact with the device file but spit doesn't I guess?

2021-06-04T18:52:59.343400Z

oh I meant what error from the cookbook udp example

mathpunk 2021-06-04T18:53:54.343600Z

I tried

(def socket-address
  (InetSocketAddress. "localhost" 8125))

(DatagramSocket. socket-address)
and got
1. Unhandled java.net.BindException
   Address already in use (Bind failed)

mathpunk 2021-06-04T18:54:15.343800Z

i don't THINK i have state lying around but I'll restart the repl b/c why not

2021-06-04T18:54:48.344Z

hmm...

mathpunk 2021-06-04T18:54:51.344200Z

nah, it's the same

mathpunk 2021-06-04T18:55:38.344400Z

I started datadog with some docker invocation and hence it's listening on that port. I assumed that "creating" a socket would mean making a Java class that was like, my end of that socket. I guess not!

2021-06-04T19:06:49.344700Z

that's kind of it, but the problem is that a socket isn't just an object that knows how to send or receive data, it also has a lot of ceremony and state, and that's the part that makes using it correctly difficult

mathpunk 2021-06-04T19:08:25.344900Z

maybe i need to 'connect' rather than making a new one with DatagramSocket. ?

mathpunk 2021-06-04T19:10:42.345100Z

oh right, I tried this but due to a little thing called "I have no idea what I'm doing" my connection is nil:

(def conn
  (.connect (DatagramSocket.) socket-address))

(type conn) =&gt; nil

2021-06-04T19:11:40.345300Z

connect returns nil, you need to retain a handle to the socket object

2021-06-04T19:12:23.345500Z

or use doto as a shorthand - (def conn (doto (DatagramSocket.) (.connect socket-address)))

2021-06-04T19:12:51.345700Z

the world of objects is kind of topsy-turvy isn't it

mathpunk 2021-06-04T19:13:18.346Z

ohhhhh ok

mathpunk 2021-06-04T20:15:59.346300Z

huzzah!

sova-soars-the-sora 2021-06-04T23:38:09.347100Z

I want to make a simple graphical game in cljs using the canvas ... are there any good projects I can use as a template?