Fork me on GitHub
#beginners
<
2017-12-14
>
dfcarpenter02:12:43

Hi everyone i'm somewhat of a newbie and wondering about database migrations. I have been spoiled with migrations using django which if you aren't familiar are pretty nice. I don't expect there to be as rich an option in clojure but can I get some opinions of some of the options like ragtime/joplin/migratus or other options?

admay04:12:18

@dfcarpenter I’m a fan of Migratus. The dude that wrote and maintains it, Yogthos, just builds incredibly good stuff. He’s also fairly active on Slack if you ever need any help. It’s the default for the Luminus framework (http://www.luminusweb.net/docs), another one of Yogtho’s creations. It works well. It’s efficient. It’s stable. And most importantly, it’s well documented.

admay04:12:50

It also comes with a Leiningen plug in, which is nice

admay04:12:22

I never got into Joplin or Ragtime because I found and stuck with Migratus via the Luminus template, so I can’t say anything about them.

admay04:12:03

However, their creators (Juxt for Joplin, Weavejester for Ragtime) are also very reliable, well known, and make great stuff!

grounded_sage07:12:38

Hi I would like to use this as a library locally. Is there any clear information online on how to do it. https://github.com/ghl3/dataframe

grounded_sage07:12:57

Basically want to create the package and then add it as a depenency

noman08:12:36

hey guys, what is your favorite clojure(script) IDE? I've been playing around mostly with IntelliJ + Cursive and it is quite nice, but heavy. any other things I should check out? One thing i feel like is quite necessary is the IDE doing all the indenting and parenthess/bracket management for you, which feels like it speeds up coding by an order of magnitude

michaels13:12:51

I use VS Code with two plugins: “Clojure” - Andrey Lisin - for repl, autocomplete, docs, and evaluation, and “Parinfer” - shaunlebron - for automatically managing my indentation/closing parens.

sam15:12:32

@noman I really like atom with these plugins: - proto-repl - let's you evaluate code in your IDE, awesome for interactive development -parainfer: Automatically infer closing parens based on indentation. An essential tool for writing Lisp code. proto-repl allows you to run a repl as a split tab, and loads your entire project, allowing you to access all your methods in all your defined namespaces. Links: https://atom.io/packages/proto-repl https://atom.io/packages/parinfer

noman08:12:50

are clojure people all using emacs or something?

schmee08:12:36

no, lots of people use other stuff. I use Vim, for instance.

schmee08:12:38

I can’t imagine writing clojure without it these day, and it is available for many editors

noman08:12:18

clojure is the best thing that's ever happened to me

grounded_sage08:12:57

I've used lein install but it's not ended up in my local Maven repository. How do I make sure it ends up there and require it as a dependency into my project

madstap14:12:24

You want to use a modified version of that lib in another project?

noisesmith17:12:31

lein install is the most reliable method - make sure you ar e looking for the right project name and version - checkouts don’t even work without running a proper lein install first

Empperi09:12:39

@noman Clojure people are using all kinds of things. Emacs, Vim, Intellij+Cursive, Lighttable, Atom…

Empperi09:12:45

nice thing about Clojure is that you can pretty easily do rudimentary support for it into any editor since it’s a Lisp and Lisps are trivial to parse. Also, due to nREPL and socket REPL it’s really easy to integrate all that to a functional REPL and when you have that you already have a pretty good development environment

Empperi09:12:53

paredit is easy to implement on top of S-expressions and since everything is so easy to parse it’s not that hard to implement stuff like automatic require inclusion and so forth

matan12:12:46

I seem to have forgotten how do you get the offending source file, when you get a java.lang.RuntimeException: EOF while reading without any of your sources showing

Paolo13:12:53

Hello everyone, using clojure.data.json pprint to pretty print a json, I'm getting this:

[{"A":
  {"B":"0001",
   "C":"0002"}},
 {"A":
  {"B":"0002",
   "C":"0002"}}]
when it suppose to indent as this:
[
 {
  "A":{
    "B":"0001",
    "C":"0002"
  }
 },
...
is there any formatting control to turn it into the correct formatting?

Empperi14:12:32

it’s interesting you say that the first one would be incorrect since I find that much more readable

Paolo14:12:04

I know this is a matter of interpretation. First one is correct for clojure, not for json as we see around

Paolo14:12:24

that's why I'm looking for a modifier

Paolo14:12:58

to avoid creating my own pprint method

Empperi14:12:28

I think the latter isn’t correct either if you look at the “JSON out there”

Empperi14:12:47

[
  {
    “A”: {
      “B”: “0001”,
      “C”: “0002”
    },
...

Empperi14:12:01

that would be more what I would expect to see from a random pretty printed JSON example

Empperi14:12:21

but this just proves the point that there is no such thing as “correct” JSON pretty printing

Paolo14:12:39

well, you have a point. Even so, I need to print the same output of command jq which does as you described(I did something wrong on my second example, it suppose to be like yours)

Paolo14:12:44

do you know how?

zlrth16:12:41

I might be under/over-thinking something. I'd like to make a mount state that, when started, makes a GET request every five seconds. This works:

(defn get-loop []
  (loop []
    (future (Thread/sleep 5000)
            (-> (http-request-for-blah)
                do-something-else)
            (recur))))
but I don't know how to write a mount/stop for this, i.e. kill a looping future. What's the idiomatic way to do a task every period-of-time, and be able to stop that task with another function?

schmee17:12:46

@mfm I don’t know if this is considered idiomatic but here’s one way to do it:

(defn get-loop []
  (let [running (atom true)
        stop-fn (fn [] (reset! running false))]
    (future
      (loop []
        (when @running
          (Thread/sleep 1000)
          (println "doing stuff")
          (recur))))
    stop-fn))

(defstate foo
  :start (get-loop)
  :stop (foo))

zlrth17:12:52

looks good to me, @schmee! thanks. makes sense.

donaldball17:12:49

FWIW the java.util.concurrent.ScheduledExecutorService is probably the most efficient and precise way to do this

zlrth17:12:45

I am interested, @donaldball! thanks. i'm not familiar with the effect on the JVM of having (future (loop (Thread/sleep ...))), so i am also interested in an efficient implementation.

donaldball17:12:43

You’re dedicating a thread that’s idle much of the time, and, particularly if your “doing stuff” takes meaningful time, your actual period is going to be longer than stated period. None of this may matter even a little bit for your use case, of course 🙂

noisesmith18:12:28

stopping single threads from the outside within the jvm is error prone, but future-cancel uses the most reliable method available - it can cancel paused io or sleeping threads at least

noisesmith18:12:23

if you care about thread count and overhead of sleeping threads, you could use a go block with async/timeout and launching the side effecting thing in async/thread

noisesmith18:12:00

waiting on N timeouts only requires the small number of threads core.async allocates on startup (nproc*4+2 iirc)

zlrth18:12:31

with something like

(go-loop []
 (<! (timeout 5000))
 (println "stuff)
 (recur))
how do i stop the go-loop?

noisesmith18:12:34

there’s also ScheduledThreadPoolExecutorService which is wrapped by at-at and is meant for periodically running tasks and has a straightforward api for stopping individual tasks

noisesmith18:12:08

@mfm the usual idiom in core.async is to use a channel that gets closed when you want the loop to exit

noisesmith18:12:08

also you need to put your stuff (whatever replaces the println) in async/thread if it uses any noticeable execution time (<! (async/thread …)) - because otherwise you can easily starve core.async and halt all async code

noisesmith18:12:24

so we end up with

(go-loop []
  (let  [[_ continue?] (alts! [(timeout time) cancel-chan)]
     (when-not (= continue? cancel-chan)
        (<! (async/thread (do-stuff)))
        (recur))))

noisesmith18:12:06

if cancel-chan is not closed, it does the thing, if cancel chan is closed it returns nil before the timeout completes, and is passed as the channel returned by alts! which is then detected etc.

noisesmith18:12:01

but returning a future and then calling future-cancel later is fine if you are OK with the overhead of a sleeping thread per recurring thing

zlrth18:12:06

wow. that is interesting to look at.

noisesmith18:12:11

future-cancel will stop things that sleep

zlrth18:12:47

thanks. i'll try this

noisesmith18:12:34

you can leave the <! off of the async/thread call if blowing up in that call shouldn’t stop the loop and you don’t want to add execution time to the timeout time (but consider what happens if you repeatedly do the call and it throws exceptions every time, or if the call does something that accidentally takes longer than the timeout time leading to snowballing resource usage)

noisesmith18:12:50

core.async makes async easier but parallelism and async are intrinsically complex

jrheard22:12:54

here’s a situation i’m in, and some code i’ve written - is there a better way to express this?

jrheard22:12:08

i’ve got a grid, which is a 2d vector of integer-or-nils

jrheard22:12:18

and a move, which is a set that looks like this

jrheard22:12:20

#{[[5 7] 4] [[7 7] 5] [[8 7] 2]}

jrheard22:12:34

it’s a series of [[x y] value] pairs

jrheard22:12:47

and so i’d like to apply each of the parts of the move to the grid

jrheard22:12:58

and i’ve just got this straightforward reduce:

(reduce (fn [grid [[x y] value]]
                                 (assoc-in grid [x y] value))
                               grid
                               move)

jrheard22:12:52

is that reduce the best way of expressing this, or is there some obvious better / more natural way that’s eluding me?

noisesmith22:12:57

that reduce looks fine to me - you don’t need to destructure [x y] btw - you could change it to [grid [coords value] (assoc-in grid coords value)

jrheard22:12:03

oh good call

jrheard22:12:10

cool, thanks for the sanity check + advice!

noisesmith22:12:40

you could change that function definition to (partial apply assoc-in)

noisesmith22:12:10

(reduce (partial apply assoc-in) grid move) looks great to me 😄

jrheard22:12:17

niiice, i’ll try that

jrheard22:12:27

that’s exactly the sort of thing i was looking for

jrheard22:12:55

i’ve somehow temporarily bricked my intellij since asking the question, but once i get things working again i’ll try that out and i’m sure it’ll be perfect - thanks again! 🙂

jrheard22:12:07

(it works!)

admay23:12:17

Hey, spec/namespace question. I have a bunch of specs defined in a file /src/foo/specs.clj but can’t figure out how to reference them in /src/foo/resolvers.clj

admay23:12:22

What am I missing?

admay23:12:01

Never miiiind … 😄