Fork me on GitHub
#beginners
<
2018-12-14
>
jaihindhreddy00:12:37

How do I manipulate #inst values? Calculate interval b/w two instants, compare time intervals and so on.

seancorfield00:12:22

@jaihindh.reddy You will want to look at Java Time for stuff like that I suspect.

jaihindhreddy00:12:40

I'm writing a script that takes data from a SQL database and ssh into servers, grep and parse logs....

jaihindhreddy00:12:48

^ The new java time thing? I see various names floating around, java.time, clojure.time, clojure.instant, joda etc and don't know what the consensus is (or if there is one)

jaihindhreddy00:12:26

java time seems to be good upon glancing the API. No blunders like mutable dates ๐Ÿ™‚

seancorfield00:12:33

Java Time is the "standard" way to do that sort of stuff ever since Java 8.

seancorfield00:12:43

It replaces Joda Time.

๐Ÿ‘ 4
seancorfield00:12:05

You might find the clojure.java-time wrapper easier to work with than raw Java interop.

seancorfield00:12:24

@ahmed1hsn I concur with @dpsutton That code compiles without error for me too -- so something you have at line 100 is where the error likely is, in your file, not in the 62 lines that come from that money.clj script.

stardiviner00:12:22

This code can't catch first exception java.net.ConnectException. But when I execute (redis/ping), it raise up this exception java.net.ConnectException.

hiredman00:12:53

because carmine is a pain

hiredman00:12:16

(I maybe be confusing carmine with faraday)

hiredman00:12:45

(assuming that is the redis library you are using)

sova-soars-the-sora02:12:35

@seancorfield i still think java time has an insufficient spec for interval types compared to joda time ;_;

seancorfield02:12:27

@sova I'm sure Stephen Colebourne has a good reason for that difference...

seancorfield02:12:16

(and there's always his org.threeten/threeten-extra library with https://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/Interval.html etc)

sova-soars-the-sora02:12:17

Ah! that's right. i forgot about the "extras" library

sova-soars-the-sora02:12:35

now that's what i'm talking about. thank you.

Daouda04:12:48

hey guys, i am trying to generate the war file of my compojure-api project but it keep generating a jar. help? please.

Daouda04:12:13

it's an api

stardiviner04:12:46

@hiredman I checked out that link source code, still don't understand why can't capture that exception.

jaihindhreddy06:12:11

I always have trouble using require the first time I add a new lib. The namespace name isn't always same as the artifact name or the library name. Can I reliably find this information (which nses it has) from the artifact itself somehow knowing just the :mvn/version of it without relying on documentation. Because once I require it, I can mostly be self sufficient using the stuff in clojure.repl (`find-doc`, doc, source et al)

alexmiller13:12:38

Unfortunately the honest answer is no. There is no relationship between an artifact group/name/version and the namespaces in it.

๐Ÿ‘ 4
๐Ÿ˜ 4
sova-soars-the-sora06:12:49

how to toggle class via :on-click in cljs?

Eccentric J06:12:41

What libraries or frameworks are you using in cljs?

sova-soars-the-sora07:12:36

which includes hiccup more or less

Eccentric J07:12:35

Forgive me if Iโ€™m unhelpful here but I would suspect https://github.com/tonsky/rum#components-local-state combined with a function to take relevant state and return a string of class names?

Eccentric J07:12:42

(defn get-classes
  [active?]
  (->> (keep some? ["my-class"
                    "other-class"
                    (when active? "active")])
       clojure.string/join " "))

jaihindhreddy08:12:00

1. Is there a way to configure clojure.java.jdbc to return java.time.Instants instead of java.sql.Timestamps? 2. Is there a way to make Clojure print java.time.Instants with #inst? 3. Is there a way to make Clojure read #insts as java.time.Instants? 4. Are the above bad ideas?

alexmiller17:12:40

Re 2, it should already do that with Java 8+

alexmiller17:12:42

They are good ideas, but 3 would likely be a breaking change for some

jaihindhreddy10:12:49

Thank you for your suggestion! I have looked at Joda time, and it seems like java.time largely replaces all other time stuff in the Java world, making clojure.java-time the go-to idiomatic Clojure wrapper of java.time. I was just trying to have minimal deps while also minimizing the amount of interop code.

restenb09:12:49

you can coerce to and from SQL timestamps with clj-time.coerce

restenb09:12:54

there should also be protocol extension in clj-time.jdbc for automatic coercion

restenb09:12:15

this converts to/from joda-time though but usually that's not an issue

JanisOlex09:12:47

Hi, friendly people. My question is somewhat n00b annoyance about organisation of namespace and project in general. According to Clojure namespace evaluation, it does read from top to bottom. Which means in normal case, the function have to be defined before it can be used. But that leads that my most abstract and high-level functions are at the bottom of file, and I can't program top-down. Looks like, the most generic functions like main game loop literary is the last function at the very bottom of my file. Is it as it supposed to be? I know one can declare functions ahead, but that sux.

JanisOlex09:12:06

How do you deal mentally with this rading text file from bottom?

JanisOlex09:12:44

Or the solution is divide namespaces by abstraction... most generic functions in one namespace which requires other namespaces? So it seems that programming would be like writing main function, fill it up with smaller functions, then extract those into another namespace and require :all that namespace

restenb09:12:49

i generally end up putting common/utility functions at the top and dividing out into more namespaces based what seems like a logical organization

restenb09:12:21

so if I end up needing a utility function in several namespaces I'll make a utils namespace and require that everywhere for example

restenb09:12:51

code organization in a purely functional language will generally be a looser than object-oriented languages though

restenb09:12:27

then my main namespaces are named & divided according to what functionality they provide, f.ex. formatters, imports, exports, user, sql, etc. etc.

restenb09:12:46

then try and keep namespaces short enough to be able to mentally "contain" them while working, which for me seems to be 2-300 codelines pr. namespace max

jaihindhreddy10:12:20

Using clojure.java-time I'm unable to do this simple thing: Given a java.time.Instant t, find the hour(s) the time range t-e to t+e falls on, where e is an epsilon value. For example, for t = "today 15:56" and e = "5 minutes" t-e would be "today 15:51" and t+e would be "today 16:01" making the output of the hypothetical function ["today 15" "today 16"]. How can I accomplish this?

dorab19:12:18

@jaihindh.reddy Have you looked at org.threeten/threeten-extra ? It has Intervals and a contains? function that might be helpful. This extra lib is mentioned at https://github.com/dm3/clojure.java-time#three-ten-extra

jaihindhreddy20:12:02

@dorab Thanks! This is exactly what I was looking for...

(defn record->hours
  [epsilon {:keys [addedon updatedon] :as record}]
  "Returns a seq of all hours that might have encompassed all activity in a DB record"
  (let [max-time (t/truncate-to (t/minus addedon epsilon) :hours)
        min-time (t/truncate-to (t/plus updatedon epsilon) :hours)]
    (->> (t/iterate t/plus min-time (t/duration 1 :hours))
         (take-while #(not (t/after? % max-time))))))

Eccentric J23:12:57

Is there a recommended way to make this less verbose?

clj -A:repl -m nrepl.cmdline --interactive
Could I just toss that in a repl.sh?

lilactown23:12:33

you could add the main nrepl.cmdline to an alias I think?

lilactown23:12:13

but I just use a couple of functions in my bash_profile / zshrc, yeah

Eccentric J23:12:02

{:aliases {:repl {:extra-deps {nrepl/nrepl {:mvn/version "0.5.3"}
                               proto-repl {:mvn/version "0.3.1"}}
                  :main-opts ["-m nrepl.cmdline --interactive"]}}}
Usage: clj -A:repl Works great, thanks!

hiredman23:12:27

if you don't use keyboard shortcuts for searching your shell history, you should. it is way better then using shell aliases / functions and keeping those things updated

Eccentric J23:12:15

Thatโ€™s a good point. I just started using fish shell which provides a lot of that automatically, but I felt like I was missing something about deps.edn.