Fork me on GitHub
#beginners
<
2020-06-27
>
William Steinberg02:06:35

Am looking for a guide to help me get started with ClojureScript and rapidly be productive. Will pay for the service. Need only a couple of hours of help. Anyone interested? <mailto:[email protected]|[email protected]>

William Steinberg03:06:30

Need to see an example where JavaScript code calls a ClojureScript function, and a ClojureScript function calls a JavaScript library/file.

Manuel Ceron10:06:45

Hi everyone, I have a code style question. I want to match a text line against some regex patterns and return different functions depending on what matches. For example:

(if-let [value (re-find pattern1 line)]
  (foo value)
  (if-let [value (re-find pattern2 line)]
    (bar value)
    (if-let [value (re-find pattern3 line)]
      (baz value)
      default-value)))
I dislike a bit the nested if-let style. So I'm wondering if there is a nice way of having the structure a bit flatter, like with cond , but in a way that allows me to capture the values.

bartuka13:06:11

Maybe the more straightforward way would be to move the re-find evaluations to a single let and use cond

(let [v1 (re-find pattern1 text)
      v2 (re-find pattern2 text)
      v3 (re-find pattern3 text)]
  (cond
    (some? v1) (bar v1)
    (some? v2) (foo v2)
    (some? v3) (baz v3)))

Manuel Ceron14:06:33

thanks, I think I'm going to do something like this

walterl15:06:26

That sounded like a nice macro writing exercise... so I wrote one šŸ™‚ https://github.com/walterl/clj-walterl/blob/master/src/clj_walterl/core.clj#L15

walterl15:06:40

The difference to @UBSREKQ5Q's solution being that all (potentially slow/expensive) tests don't have to be performed ahead of time.

walterl15:06:14

For simple regex on short inputs, that will be sufficient, though

bartuka15:06:16

If you want to avoid the potentially slow/expensive tests, you could do something like this:

(let [patterns [pattern1 pattern2 pattern3]
      fns [foo bar baz]]
  (loop [pattern patterns -fn fns]
    (when-not (empty? pattern)
      (if-let [value (re-find (first pattern) text)]
        ((first -fn) value)
        (recur (rest pattern) (rest -fn))))))

šŸ‘ 3
daboone7212:06:54

Well greetings all did my first Clojure weekly study yesterday.

daboone7212:06:38

Did LISP ages ago at university, looking forward to doing it it's style again while learning functional programming.

daboone7212:06:46

Just sanity checking Ring is a good way to go to write Rest things in Clojure?

bartuka13:06:17

Ring is great. šŸ˜ƒ

adam15:06:57

I like bidi better as it supports reverse routing: https://github.com/juxt/bidi

adam17:06:25

I am trying to deploy a Clojure app on Ubuntu 20.04. Is openjdk-11-jre-headless enough, or I should install default-jre?

jumar17:06:28

Thatā€™s fine I think; should be easy to try

salam17:06:27

itā€™s better to be explicit about the version of the runtime environment, so i would choose the former.

adam17:06:52

Ok thanks - my confusion stemmed from the version being labeled headless.

salam17:06:01

oh, ā€œheadlessā€ can be interpreted as ā€œno guiā€.

salam17:06:09

so unless your clojure application has gui, headless should be good enough.

šŸ†’ 3
adam17:06:55

Giving it a go now

salam17:06:27

itā€™s better to be explicit about the version of the runtime environment, so i would choose the former.

zhuxun219:06:49

If I have a list of instance methods (e.g. [length getName ...]), how do I apply them to a java object instance? This didn't work

(let [f (io/file "/bin/sh")]
  (map #(. f %) [getName length]))

phronmophobic19:06:05

without using macros:

(let [f ( "/bin/sh")]
  ((juxt #(.getName %)
         #(.length %))
   f))

šŸ‘ 3
phronmophobic19:06:52

the . is a special form, so it won't work dynamically

adam19:06:54

My uberjar is working normally on my local machine (Mac):

[~] ā¤‘ java -jar myproj-0.1.0-SNAPSHOT-standalone.jar
2020-06-27 14:36:32.117:INFO::main: Logging initialized @1412ms to org.eclipse.jetty.util.log.StdErrLog
WARNING: seqable? already refers to: #'clojure.core/seqable? in namespace: clojure.core.incubator, being replaced by: #'clojure.core.incubator/seqable?
2020-06-27 14:36:34.878:INFO:oejs.Server:main: jetty-9.4.28.v20200408; built: 2020-04-08T17:49:39.557Z; git: ac5acd51c9ab228e9164c738d7fa1fde9e5521f8; jvm 11.0.7+8-LTS
2020-06-27 14:36:34.929:INFO:oejs.AbstractConnector:main: Started ServerConnector@1ce45e18{HTTP/1.1, (http/1.1)}{0.0.0.0:6000}
2020-06-27 14:36:34.930:INFO:oejs.Server:main: Started @4225ms
Started server on port 6000
And when I go to localhost:6000 the app works normally. I scpā€™ed the same .jar file to my Ubuntu 20.04 server and when I run it it gets stuck somewhere without any indication of whatā€™s going on:
# sudo java -jar myproj-0.1.0-SNAPSHOT-standalone.jar
2020-06-27 18:56:48.117:INFO::main: Logging initialized @4487ms to org.eclipse.jetty.util.log.StdErrLog
WARNING: seqable? already refers to: #'clojure.core/seqable? in namespace: clojure.core.incubator, being replaced by: #'clojure.core.incubator/seqable?
How can I debug this?

dpsutton19:06:47

if you curl localhost:6000 while ssh'ed into the ubuntu machine what do you see?

adam19:06:44

curl: (7) Failed to connect to localhost port 6000: Connection refused

phronmophobic19:06:57

what version of java is on your mac? what version is on the server?

adam19:06:43

Mac:

java version "11.0.7" 2020-04-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.7+8-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.7+8-LTS, mixed mode)
Ubuntu:
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode)

adam19:06:17

Itā€™s superfluous, I am already using root

dpsutton19:06:28

can you install clj and run a regular repl and try it there on the server?

dpsutton19:06:53

how did you create the jar?

adam19:06:43

lein ring uberjar
:ring {:handler myproj.server/app}
  :main ^:skip-aot myproj.server
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all
                       :omit-source true
                       :env          {:cookie-secret-key "64du37483*h48b3t"} ; Must be exactly 16 characters (even in uberjar profile for the build to succeed).
                       :dependencies [[ring/ring-mock "0.4.0"]]}}) ; Build fails without ring/ring-mock for some reason!

adam19:06:58

> can you installĀ `clj`Ā and run a regular repl and try it there on the server? I donā€™t have the source code on the server but I can pull it and install the dev env if thatā€™s the only way to find out whatā€™s wrong

dpsutton19:06:21

not saying its the only way to find out what's wrong. but seems like a good candidate

dpsutton19:06:43

or if its using lein, just get the source and use lein ring server and see what happens

adam19:06:28

Got it, will try that and report back. Thanks.

adam22:06:34

Same problem. Even tried Oracle Java. I guess Iā€™ll need to reconstruct the app little by little šŸ˜ž

adam23:06:13

The problem is in my setup though. lein new compojure hello-world ā€¦ lein ring server-headless works

adam23:06:03

Oooh, I was omitting -headless from my setup. The server is a mess now. Will have to start over šŸ˜„

adam00:06:13

My uberjar was taking forever to launch because it couldnā€™t connect to Postgres :man-facepalming:

futurile22:06:48

I'm having a problem trying to use a Java dependency in Clojure. I have been playing with Lanterna. There's a bug I wanted to help with that requires me to install the git master version. I used lein-git-down to get it and install the master from github. But, I can't get it to import the library in the REPL, it keeps saying java.lang.ClassNotFoundException com.googlecode.lanterna.terminal.TerminalFactory. I've checked that lein-git-down compiled the lanterna-master.jar file correctly using jar -tvf, and it's showing a MANIFEST and classes etc. I've checked the classpath in the REPL and it can see the lanterna-master.jar file is there. Can anyone suggest any steps I could try?

phronmophobic22:06:45

what does lein classpath show?

phronmophobic22:06:04

what does your dependencies list look like?

futurile22:06:25

lein classpath shows it (there's a lot of output), but it does have: /home/steve/.m2/repository/lanterna/lanterna/master/lanterna-master.jar

futurile22:06:10

Dependencies is:

:dependencies [ [lanterna "master"] ]
:repositories [; specifically used by lein-git-down to enable github
                 ["public-github" {:url ""}]
                 ["private-github" {:url "" :protocol :ssh}]
:git-down {
            lanterna {:coordinates mabe02/lanterna}
           }

futurile22:06:10

Normally when I use it the dependency line is

;[com.googlecode.lanterna/lanterna "3.0.3"]
When I put that it, it grabs it from maven central and installs/works fine: I double checked it earlier in case I'd messed something else up. It's something I'm doing wrong in trying to use it from a git repo

phronmophobic22:06:58

com.googlecode.lanterna.terminal.TerminalFactory is an interface, not a class

phronmophobic22:06:06

can you import com.googlecode.lanterna.terminal.DefaultTerminalFactory

futurile22:06:48

Same error:

(import com.googlecode.lanterna.terminal.DefaultTerminalFactory)
java.lang.ClassNotFoundException: com.googlecode.lanterna.terminal.DefaultTerminalFactory

phronmophobic22:06:42

what if you do: (import 'com.googlecode.lanterna.terminal.DefaultTerminalFactory)

phronmophobic22:06:58

the import statement requires a quote before the class name

futurile22:06:58

I get:

(import 'com.googlecode.lanterna.terminal.DefaultTerminalFactory)
java.lang.ClassNotFoundException: com.googlecode.lanterna.terminal.DefaultTerminalFactory

futurile22:06:01

I think there's some part of the chain I don't understand. I don't really know Java. I thought I understood how it went together but I must be missing a step.

phronmophobic22:06:05

looks like you've done everything correct to me

phronmophobic22:06:23

how are you running the code?

phronmophobic22:06:10

is it even having issues when run from lein repl?

futurile22:06:49

I'm in the REP, (I use Rebel Readline). That's how I've been testing it up to this point - play in the REPL and then create my source and use lein run . This set-up works fine with the Maven released version 3.0.3. The minute I changed over to trying to use it from GitHub it won't work. I guess I will ask the lein-git-down maintainer - though I'm sure it's something I'm doing wrong that I don't understand.

futurile22:06:57

Thanks for confirming I haven't gone totally mad šŸ™‚

phronmophobic22:06:40

well, I would try importing under lein repl just to see if maybe it's something not getting picked up by REP

phronmophobic22:06:32

otherwise, I'm pretty stumped ĀÆ\(惄)/ĀÆ

phronmophobic22:06:12

one other thing to note is that it seems like lein-git-down is for targeting clojure libraries. it may be that it doesn't work with java libraries