Fork me on GitHub
#clojure
<
2018-08-06
>
seancorfield00:08:19

https://github.com/trptcolin/reply/issues/184 -- just in case Colin feels like adding deps.edn to REPL-y (or someone wants to send a PR).

👍 4
mx200002:08:20

Is this a bug in clojure? (inc nil) returns NullpointerException and the Stacktrace is missing

dpsutton03:08:16

it does the same thing as (+ nil nil). this is a garbage in garbage out situation i believe. I get a stacktrace in CIDER. I don't know what environment you are using

bajrachar03:08:52

@mx2000 - you might need to do a (println *e) to get a full stacktrace - if you haven't tried it already on the repl.

andy.fingerhut05:08:57

@mx2000 If you are doing (pst *e) or something similar to try to look at the exception, and the stack trace is one line long or empty, that is unusual, but I believe possible depending upon optimization and other command-line options when you started the JVM. More details here than I know myself: https://stackoverflow.com/questions/2411487/nullpointerexception-in-java-with-no-stacktrace

andy.fingerhut05:08:25

If you are just doing (inc nil) and seeing only one line mentioning the NullPointerException, that is normal for a REPL to only show that much information when an exception occurs.

puzzler06:08:39

I know that functions only support primitive type hints with four or fewer args. Does loop-recur form have the same restriction?

puzzler06:08:04

I'm getting a weird boxing on loop warning that I think may be due to that.

puzzler06:08:52

The other possible explanation for the warning is that a function I'm calling with a primitive type hint on the return type isn't getting called properly. What I mean by that is when I look at the decompiled Java code for the function with the primitive type hint, I see that it has an invokeStatic and invokePrim method that return a primitive double, but it also has a regular invoke that returns a boxed Double. When I look at the decompiled Java code produced at the call site, I see that it is calling invoke and thus getting an Object back, not a double. Any idea why the call site would not be invoking the primitive return version of my function? Any restrictions on using functions with primitive return types that I need to know about? It's not being called as a higher-order function, which is the main thing I would think might cause the primitive version to be ignored.

puzzler07:08:36

Yes, I'm now certain that at the call site, it's not invoking the compiled version of the function with the primitive return type. What's the trick to getting Clojure to actually use the version of the function with the primitive return type?

puzzler07:08:39

I think I figured it out. I had incorrectly placed the return type hint on the function name rather than the arg list.

andy.fingerhut07:08:59

Eastwood has some warnings about ineffective or erroneous Clojure type hints. Not sure if it would have caught that for you or not.

puzzler08:08:26

If I do (defrecord R [foo bar]) and then I want to type hint something in the same namespace as an R, I can do ^R. From another namespace, however, it doesn't seem so user-friendly. I am able to do ^myns.core.R as a type hint, but if I've aliased myns.core as n, I can't do ^n/R. Is there any way to refer to the type of a defrecord, deftype, definterface through a namespace alias?

bronsa08:08:02

not through an alias

bronsa08:08:07

you can only import the class

bronsa08:08:53

(ns my-ns (:require [my-record-ns :refer [..]]) (:import my_record_ns.R)) and then you can type hint using ^R

puzzler08:08:55

That's unfortunate, but thanks for the info.

dabrazhe13:08:44

What is the proper way to cycle a string: eg beatles --> sbeatle, esbeatl, lesbeat etc ?

andy.fingerhut13:08:28

Maybe close to what you want: (map #(apply str %) (partition (count "beatles") 1 (concat (seq "beatles") (seq "beatles"))))

4
dominicm13:08:30

(apply str (last s) (butlast s))

andy.fingerhut13:08:25

But @domonicm answer may lead to something in the order you requested.

andy.fingerhut13:08:35

(defn last-to-front [s] (apply str (last s) (butlast s)))

andy.fingerhut13:08:51

Followed by (take (count "beatles") (iterate last-to-front "beatles"))

👍 8
Bravi17:08:03

hi everyone. I’m experimenting with time in clojure. i’m using clj-time at the moment. I’m trying to create a script, that will change the logo on my website, based on the period of time. so I have these holidays

(def holidays [{:from  '(12 23)
                :to    '(12 27)
                :label "Christmas"
                :logo  "/img/logos/christmas.png"}
               {:from  '(10 27)
                :to    '(11 3)
                :label "Halloween"
                :logo  "/img/logos/halloween.png"}])
and what I want to do is to create a function that accepts the current year and then loops over that vector and checks, if the current date is between the range of from and to of a holiday period, I want it to return the holiday map. I thought I would get the current year somehow and then use cons to add that year to those lists in the beginning and then use the whole thing with t/date-time to parse dates

Bravi17:08:23

so my question is - how can I get the current year in clj-time?

jgh17:08:05

there's within?

Bravi17:08:15

yeah, I’ll need to use that too

jgh17:08:33

i think you just need to use reduce with within? and grab the year from t/now

jgh17:08:12

i would be more helpful but i have a tasty looking plate of indian food waiting for me!

Bravi18:08:06

ok I think I got it

(ns logo-chooser.core
  (:require [clj-time.core :as t])
  (:gen-class))

(def holidays [{:from  [12 23]
                :to    [12 27]
                :label "Christmas"
                :logo  "/img/logos/christmas.png"}
               {:from  [10 27]
                :to    [11 3]
                :label "Halloween"
                :logo  "/img/logos/halloween.png"}
               {:from  [1 1]
                :to    [12 31]
                :label "Default"
                :logo  "/img/logos/default.png"}])

(defn- check-holiday-date-range
  [{:keys [to from]} current-date]
  (let [current-year (t/year current-date)
        interval     (->> [from to]
                          (map (comp #(apply t/date-time %) #(cons current-year %)))
                          (apply t/interval))]
    (t/within? interval current-date)))

(defn- get-current-holiday
  [holidays current-date]
  (first (filter #(check-holiday-date-range % current-date) holidays)))

(defn -main
  [& args]
  (println (get-current-holiday holidays (t/now))))

seancorfield19:08:45

@bravilogy If you're on Java 8 or later, you should probably use Java Time instead of Joda Time -- unless you have a specific reason to use the latter?

Bravi20:08:55

thank you. will have a look at that :)

seancorfield19:08:16

Using Java Time directly from Clojure isn't bad, but you could also look at clojure/java-time as an idiomatic wrapper for it.

seancorfield19:08:43

(note: I'm one of the maintainers of clj-time and I'm trying to encouraging people to migrate away from it now that we have Java Time built-in)

zaphodious21:08:40

Spec question- are s/merge and s/multi-spec composable? As in, can you s/merge a normal s/keys and a s/multi-spec?

puzzler21:08:29

Is Clojure faster or slower on Java 1.10 vs 1.8?

ghadi21:08:39

garbage collection is way better on 1.10

puzzler21:08:32

Any special flags to set, or that's out-of-the-box?

ghadi21:08:29

@puzzler out of the box the default GC is now G1GC... also CompactString is huge (and on by default)