Fork me on GitHub
#beginners
<
2020-06-24
>
adam00:06:37

There was a discussion in HN a while ago where one guy was claiming Clojure is transpiled to Java classes and the other claiming it targets the JVM directly. Who is right?

seancorfield00:06:16

Clojure compiles everything to JVM bytecode -- which is classes at the compiled level. By default, Clojure compiles code in-memory, producing classes in memory. You can ask Clojure to write those classes to disk as .class files.

seancorfield00:06:53

So maybe the guy claiming Java classes meant JVM classes rather than Java source code?

adam01:06:36

What is the difference between JVM and Java classes?

seancorfield01:06:05

I'm trying to draw the distinction between "Java classes" possibly meaning "source code containing a class { ... } definition" and "JVM classes" meaning "bytecode that defines a class (in memory or on disk)".

seancorfield00:06:20

"Though I started by targeting both the JVM and CLR, via generation of Java and C# source, by the end of 2006 I had decided I wanted to accomplish twice as much rather than do everything twice, so I decided to target only the JVM moving forward. That choice was driven by the much stronger open source library ecosystem of Java and the more dynamic nature of the JVM, e.g., HotSpot optimizations [Click and Rose 2002]. The compiler was moved to Java and eventually (prior to release) generated bytecode directly" -- Rich Hickey, History of Clojure (from https://clojure.org/about/history )

adam01:06:15

First time I hear about JVM classes 🙂

seancorfield01:06:00

Clojure targets the JVM, not Java 🙂 The JVM has classes. Java (source) classes compile to JVM (bytecode) classes.

Ben Sless06:06:42

I'm still fascinated by the similarities between the JVM and lisp machines

seancorfield07:06:33

Yeah, the VM architecture was nothing new but folks didn't seem to take it seriously until Java broke out. I'd been working with VM systems for almost a decade before Java appeared.

Ben Sless07:06:13

Is it a commonality among VMs in general or the JVM and lisp machines in particular? From my understanding, some of the main features of lisp machines were tagged objects in memory, all memory space is shared, stack based, GCed. It looks very similar to the JVM with its objects, heap, stack and GC. Haven't looked into other VMs' architecture to know if this similarity runs across all of them. Which ones did you work with?

seancorfield15:06:58

UCSD P-System was one of them. I ported it to several new architectures (including Sun SPARC back in the day).

seancorfield15:06:24

I also worked with a VM-based COBOL system (again porting the compiler and runtime to new platforms).

seancorfield01:06:18

Clojure namespaces compile to JVM (bytecode) classes and each function also compiles to a class with a static .invoke() method etc.

adam01:06:40

Aha, got it. Thanks.

Mahran Yousef05:06:35

Hi all, I want to ask if it is possible to use libpython-clj in windows. I've been trying to figure out the :library-path for the (py/initialize!) function but it seems that there is no way to find it in windows.

seancorfield05:06:55

@myousef23 There's a #libpython-clj channel which might be able to help you.

Casey11:06:51

In CLJS, using clojure.test, I have a series of tests that need to use with-redefs (several vars), but also rely on a setup and teardown using use-fixtures. I'd rather not copy/paste the with-redefs inside every deftest. But when I wrap the deftest in with-redefs, the vars aren't actually redef'd.

seancorfield16:06:53

@U70QFSCG2 Can you share some code? I'm having a hard time visualizing what it looks like based on your description.

noisesmith17:06:36

sounds like (with-redefs [#'foo bar] (deftest ...)) which of course is a no-op, since deftest sees the real #'foo when it runs

noisesmith17:06:17

you can compose fixtures and set up redefs as a fixture even better, you can write code that can be tested without with-redefs

Casey11:06:25

Am I doomed to just repeat the with-redefs?

Vachi13:06:40

it is ok for pure functions to throw exceptions that enforce some business logic?

Timur Latypoff13:06:30

Division by zero throws an exception. I think throwing exception is fine as long as some preconditions for the function are not met (that is, the programmer made a mistake by using the function in a wrong place).

👍 1
agata_anastazja (she/her)13:06:31

Is anyone from the mentors here also active as a mentor on exercism?

adam22:06:01

How can I ask the REPL to revaluate everything? For example, I seed my dev DB with the following commands:

(in-ns 'myproj.db.seed)
(clojure.core/load-file "src/clj/myproj/db/seed.clj")
(process!)
When I make changes to myproj.db.seed, they are automatically picked if I rerun the above lines. But if I change myproj.db.reset NS which is used by myproj.db.seed, I have to load that file (`myproj.db.reset`) first for the REPL to detect the changes.

noisesmith22:06:50

instead of load-file, you can use (require 'myproj.db.seed :reload-all)

noisesmith22:06:07

there are tools to automate reloads, but that will get you pretty far as a beginner

adam22:06:21

It worked perfectly. Thanks. It was such a pain 🙂

dpsutton22:06:24

what most people do: edit code, hit the key command to send the top level definition to the repl. then you don't need to worry about reloading, in-ns, requiring, etc

adam22:06:48

Working on it. I just find Cursive key mappings all messed up. Maybe because I use other JetBrains IDEs and there are many conflicts. I am resetting them little by little.

noisesmith22:06:48

the automation is great, but I think there's a value to learning the clojure-only version first - having a deeper understanding of what part of your workflow is the language and which part is the third party dev time tool

dpsutton22:06:18

that's a very good point. being able to get around a simple clj repl and how to load, reload, doc, javadoc, etc. is quite nice

noisesmith22:06:05

and extremely useful when you get a weird prod bug and know you can just use an extra jvm system property to get a repl into the running process

noisesmith22:06:43

(while a coworker who only uses knows $EDITOR integration is stuck on how to run editor on prod or maybe tunnel but the prod image doesn't have nrepl etc. etc.)

noisesmith22:06:33

all that said, I use editor integration too these days, but I got away with avoiding it for a very long time

dpsutton22:06:50

i could live without it if there were a good send form to repl across processes.

noisesmith22:06:25

that's what the socket repl is for right?

noisesmith22:06:43

with that, you can use nc to send code and get a result back

dpsutton22:06:25

i mean a keybinding from emacs to send it to the running process. i did a big pass over inf-clojure to get it very close to that

noisesmith22:06:36

ahh - just call nc :D

noisesmith22:06:04

in vim it looks like :w !nc localhost port, which just does the right thing if there's a selected region, otherwise sends the whole file

noisesmith22:06:33

anyway, real editor integrations do things that doesn't of course, and some of those things are very nice

stopa23:06:00

Curious question: what are ya'll deploy workflows? I guess we use compute engine / ec2 + docker? k8s? What kind of infra do ya'll use? Any recommended resources?

noisesmith23:06:40

I use uberjar inside docker, I miss using bare uberjars + jsvc TBH

👍 1
noisesmith23:06:03

the docker image gets run in marathon but we are going to move to k8s

noisesmith23:06:06

I definitely see the advantage of docker if you need native deps, or want to mix clojure apps in with ruby / python etc.

noisesmith23:06:58

but if you use clojure everywhere without native deps, uberjar + jsvc just works

noisesmith23:06:24

though I haven't had to use cluster management like marathon with uberjars, maybe docker does make that simpler(?)

pmonks23:06:29

Follow on question (which I asked on the Discord yesterday, and got some info from, but interested in more perspectives) - any recommendations on free/cheap hosting providers for a simple 100% Clojure app that isn’t a webapp (it’s a once-every-24hr job that calls some REST APIs then posts a message to a chat server)?

slimslenderslacks20:06:36

hey @U0MDMDYR3, the java11 google cloud function support is out now and if you combine it with https://cloud.google.com/scheduler, that might be a good fit too.

👍 1
pmonks22:06:19

Awesome - thanks for the suggestion! I remembered my app will also have a (small) on-demand portion - it’s a chat bot that mostly sends timed chat messages, but I’m planning on adding a couple of responsive functions too. So in the end it made more sense to continue implementing it as a self-contained, long-running app that manages its own scheduled tasks, and in the end I just sprang for a “hobby” Heroku subscription ($7 / month).

pmonks22:06:11

I’ve used Heroku in the past, and it’s super simple to setup (no need to mess with docker or containers or any of that incidental complexity), so it was the path of least resistance. One slight gotcha that had me stuck for a little while, for anyone who happens to read this and decided to use Heroku: https://github.com/heroku/heroku-buildpack-clojure/issues/75

slimslenderslacks02:06:26

^ ya, looks like the clojure build pack doesn't seem too far off from supporting a deps.edn only repository.

slimslenderslacks02:06:17

@U0MDMDYR3 the company I work at actually does host clojurescript chatbots, but we only support Slack and MSTeams today. When users talk to the bot, or click buttons (or whatever other interactive elements you put into the message), we route these messages back into function so that we can continue to utilize serverless runtimes. I guess you're building for Discord though, eh? We don't support that unfortunately. But if you were building for either Slack or MSTeams, I'd certainly send you an invite. Beyond the interactive message stuff, we also have support for waking the bot up on a schedule. Seems like it might "almost" be a fit.

pmonks06:06:24

Yeah this bot is for Discord. The idea of routing messages to functions is awesome though!

slimslenderslacks14:06:23

I'll let you know if we end up supporting Discord.

💯 1
noisesmith23:06:26

for something standalone that you only run once a day, check out heroku, they do make clojure deploys easy (git based workflow) and that kind of usage shouldn't end up costing much

👍 1
pmonks23:06:43

Thanks - yeah I’ve already got a teeny little Clojure webapp deployed up on Heroku, and have found it easy to work with. My only concern is the “dyno sleep” behaviour on the free tier, and the fact that my job will be asleep 99.99% of the time.

pmonks23:06:07

Not quite sure how to wake the dyno up (currently the app runs all the time, and uses the chime library for job management).

valtteri05:06:56

Check Heroku scheduler add-on

👍 1
pmonks05:06:04

Thanks @U6N4HSMFW - that might be exactly what I’m after!

🙂 1
hiredman23:06:23

I would be tempted to create an aws step function that waits 24 hours, then invokes a lambda which spins up an ec2 instance, which starts your clojure code, then waits for your clojure code to exit, then shuts down the ec2 node, then loops

hiredman23:06:09

the step function and lambda would almost certainly end up in the free tier because they would be doing so little

pmonks00:06:02

Good idea. It may even be possible to implement the job itself as a lambda (don’t know much about that technology, except at a high level).

pmonks00:06:29

Though it does need disk access (it generates a PDF file from the results of the REST API calls, then posts that to a chat).

Hlodowig04:06:01

Not hard to access the filesystem within a lambda if required, or even to persist the file to an S3 bucket. As hiredman said, easily within the free tier if you don't run this thing too frequently.

👍 1
pmonks04:06:18

Thanks! And yeah right now it’s a once-in-24hrs thing, though it might involve into a “dozen times a day or so” type of thing.

naomarik23:06:19

I keep seeing people recommend https://www.netlify.com/ in clojurians

pmonks00:06:41

Thanks - I’ll check it out!