Fork me on GitHub
#beginners
<
2021-02-13
>
aratare11:02:36

Hi there. I'm not sure if this question belongs here but I can't find another proper place for it. I'm working on a CLI Clojure app which has a hot-reload feature for certain files it's working with. I'm using https://github.com/wkf/hawk and it seems that every time I change a file there are two events fired instead of one. Looking around on StackOverflow it seems that Java's WatchService is fired twice when the content is changed and when the modification timestamp is changed. Has anyone encountered anything like this in their work? Doing double work is not really a biggie but def something I'd like to avoid. Thanks in advance.

aratare11:02:53

It seems that only Windows is throwing double events and that's by design?

aratare11:02:14

Anyway found a Java library to handle this for me.

Claudio Ferreira13:02:12

Understand deeply OOP is a pre-requisite to understand and code proficiently in funcional paradigm? A beginner that is gonna work with functional languages HAS to learn deeply OOP?

andy.fingerhut14:02:10

I do not believe someone must deeply learn OOP as a prerequisite to learn Clojure.

andy.fingerhut14:02:59

It is useful to know a tiny bit about Java APIs, for times when you want to use a Java library, but that is nothing like "deeply learning OOP"

Stuart15:02:00

I'd argue java isn't even really oop anyway, or at least the java / c# code bases I've seen aren't. Its mostly just imperative code wrapped in classes.

Tim Robinson16:02:47

I've read that those with no OOP background can find it easier to pick up functional programming, but I can't speak from personal experience. as Andy says though, familiarity with Java would be a benefit for clojure

andy.fingerhut17:02:46

I have heard people who have done years of Java and object-oriented programming say that they felt when learning Clojure that it took time for them to "unlearn" some habits they had from object-oriented languages, and learn more functional/immutable-data-structure styles of doing things.

andy.fingerhut17:02:17

(Not really "unlearn" -- just learn different/new patterns and habits.)

grazfather16:02:04

easy/dumb question: How can I break out of an infinite loop in the repl?

Claudio Ferreira11:02:22

Just click that red button when u are evaluating: Did i answered your question?

tws13:02:00

with Emacs & Cider: C-c C-b

Tim Robinson16:02:02

hmm i had that problem just yesterday and had to kill the java process

grazfather20:02:38

Yep, that’s annoying. I’d like to be able to gracefully shutdown via signal or ctrl c

dpsutton16:02:34

Some repls offer a way to attempt this. What’s your repl setup?

andy.fingerhut16:02:40

Some REPLs you can type Ctrl-C and it will kill the whole JVM process. There is a stop() JVM API method that can be used to kill a thread, but it is deprecated because in the general case it can lead to a state of your JVM where locks are held and never released on objects, and leave them in an intermediate bad state. ( https://www.baeldung.com/java-thread-stop for some details and alternatives to using the stop() method, but they require that your loops that might go into an infinite loop to be modified).

noisesmith16:02:56

in nrepl, control+c kills the current thread's computation, but doesn't kill the process

andy.fingerhut16:02:48

Using the stop() method probably? i.e. with its warnings about possible bad state being left behind?

andy.fingerhut16:02:42

Those warnings are less commonly important the way Clojure code is usually written, I believe, but in general if you are calling arbitrary Java libraries in the thread being stopped, it could be wonky.

noisesmith16:02:43

maybe the fact that they know what state is there (the state made by a REPL thread) means they can use it safely?

noisesmith16:02:11

the REPL will be in a namespace, killing that thread doesn't change scope of anything in that namespace

noisesmith16:02:43

the only gotcha would be if something in your looping / stuck code left an object in a bad state, but you'd be unlikely to see that happen with clojure apis themselves

noisesmith16:02:01

I guess? I'm realizing that's a lot of speculation and I should just read their code.

andy.fingerhut16:02:26

Yeah, I can't see using stop on a thread in a JVM running Clojure causing corruption of any of your defined functions, nor immutable collections. It is mainly the mutable stuff that you might be manipulating via Java libraries that would be most at risk.

noisesmith16:02:37

the one gotcha I'm considering is if you left your IO in a bad state, with nrepl you'd be leaving that open if you don't exit the client...

andy.fingerhut16:02:07

If that killed thread were manipulating a transient collection, it might be left in a bad intermediate state, but most likely the fact that the thread was killed means probably no other thread has a reference to the transient, anyway.

noisesmith16:02:55

right - transients have a big "only use from one thread" warning on them :biohazard_sign:

Faris16:02:40

I’m playing around with Clojure by doing deaf aunty, basically it gets input from the user and returns a response depending on if the string is fully capitalised or not. To leave the conversation with “aunty” you need to say “BYE” 3 times. My code is as below

(ns deaf-aunty.core)
(require '[clojure.string :as str])


(defn is-all-capitalized? [string]
  (= string (str/upper-case string)))

(defn talk-to-aunty [string]
  (if (is-all-capitalized? string) " NO, NOT SINCE 1938!" "HUH?! SPEAK UP, SONNY!"))

(def consecutive-byes (atom 0))

(defn deaf-aunty-run []
  (case (read-line)
    "BYE"   (if (= @consecutive-byes 3)
              (println "Okay BYE!")
              (do
                (println "What!? You want to leave????")
                (reset! consecutive-byes (+ @consecutive-byes 1))
                (recur)))  
    (do
    (reset! consecutive-byes 0)  
    (println (talk-to-aunty read-line))  (recur)
    )
    )
  )

(deaf-aunty-run)
So my question is with this part
"BYE"   (if (= @consecutive-byes 3)
              (println "Okay BYE!")
              (do
                (println "What!? You want to leave????")
                (reset! consecutive-byes (+ @consecutive-byes 1))
                (recur)))  
The Rubyist in me is itching to extract that out into a function, but not sure how to run recur in the extracted function so that it reruns deaf-aunty-run. Feel free to comment/criticise other parts as well!

noisesmith16:02:33

you might want to cross post this to #code-reviews for starters, regarding readability, it would help a lot if you followed community conventions (no trailing ) on their own line, always indent children further than parent forms)

Faris16:02:22

Okay! I actually didn’t know that channel existed, thanks!

noisesmith16:02:45

the normal way would be for the function you run to take a parameter (defn foo [x] ... (recur (f x)) ...)

noisesmith16:02:55

then the same parameter can be passed to a helper function. also, using an atom means that your code is impossible to use with threads

andy.fingerhut16:02:14

If you do use an atom, it is idiomatic to use (swap! consecutive-byes inc) instead of the call to reset! you show in your code.

andy.fingerhut16:02:33

But noisesmith's comments are spot on for how to avoid the use of an atom altogether.

noisesmith16:02:45

there's a proof somewhere I've misplaced that the usage of a mutable value can always be replaced with a parameter taken by and returned from each function touching it (though this requires some amount of code restructuring to pull off in the general case, here it's trivial)

Faris17:02:51

I see, okay lemme try and rewrite it, thanks for the tips!

grazfather19:02:03

Does anyone here know vega[lite]? I am trying to chart some data using oz, and I don’t know how to get it to recognize timestamps as quantifiable values

jumar19:02:29

#data-science may be a good place to ask

phronmophobic22:02:04

what's the recommended way to run a zero arity function with clj? clj -X my.ns/my-fn will give Wrong number of args (1) passed to: my.ns/my-fn

seancorfield22:02:16

-X only runs functions with a single argument, which must be a hash map.

seancorfield22:02:02

You could run it with -e instead but you'll need to require/resolve it -e "((requiring-resolve,'my.ns/my-fn))"

phronmophobic22:02:44

I have several zero arity functions that would be nice to be able to invoke from the command line and I don't necessarily want to change them to just to run with -X. I can always make something to put in my deps.edn, but I was just wondering if there was already a recommended approach

dpsutton22:02:24

you could make a single arity function which accepts information about which zero arity fn to call?

👍 3
dpsutton22:02:08

requires setting up a single function rather than changing all of your zero arity versions

phronmophobic22:02:13

doesn't sound too hard. I basically just want something like -X with the same parsing rules, but instead of producing a single map to pass to a 1 arity exec function, it produces a vector to produces to apply to an exec function. Just wanted to double check that this functionality didn't already exist.