This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-05
Channels
- # 100-days-of-code (1)
- # announcements (9)
- # aws (1)
- # beginners (195)
- # braveandtrue (60)
- # calva (3)
- # cider (36)
- # cljs-dev (3)
- # clojure (124)
- # clojure-canada (13)
- # clojure-dev (18)
- # clojure-germany (2)
- # clojure-italy (2)
- # clojure-losangeles (1)
- # clojure-nl (9)
- # clojure-russia (1)
- # clojure-spec (20)
- # clojure-uk (109)
- # clojurescript (49)
- # core-logic (29)
- # cursive (7)
- # datomic (62)
- # defnpodcast (1)
- # devcards (11)
- # docker (3)
- # duct (6)
- # figwheel (13)
- # figwheel-main (57)
- # fulcro (2)
- # graphql (11)
- # hyperfiddle (3)
- # jobs (5)
- # jobs-discuss (9)
- # leiningen (2)
- # lumo (1)
- # off-topic (12)
- # onyx (2)
- # pedestal (1)
- # portkey (2)
- # rdf (1)
- # re-frame (39)
- # reitit (13)
- # remote-jobs (2)
- # rum (5)
- # shadow-cljs (82)
- # tools-deps (48)
- # unrepl (3)
- # vim (12)
- # yada (1)
I haven't been able to make my function which determines whether a number is even or not performant.
(defn is-even?
[x]
(let [i (atom 0)
even-or-maybe-odd (atom true)
return? (atom false)
result (atom nil)]
(while (not @return?)
(if (= @i (max x (- x)))
(cond (true? @even-or-maybe-odd) (do (swap! return? (fn [r] true)) (swap! result (fn [k] "Even")))
(false? @even-or-maybe-odd) (do (swap! return? (fn [r] true)) (swap! result (fn [k] "Odd"))))
(do (swap! i (fn [n] (+ n 1)))
(swap! even-or-maybe-odd (fn [n] (not n))))))
@result))
Seems unnecessarily stateful. Hereβs how even?
is defined in the core library:
(defn even?
"Returns true if n is even, throws an exception if n is not an integer"
{:added "1.0"
:static true}
[n] (if (integer? n)
(zero? (bit-and (clojure.lang.RT/uncheckedLongCast n) 1))
(throw (IllegalArgumentException. (str "Argument must be an integer: " n)))))
I have no idea what you are trying to do but a normally performant way to check for even or odd on integers is bitwise and
(defn- even?
[x]
(let [i (atom 0)
even-or-maybe-odd (atom true)
return? (atom false)
result (atom nil)]
(while (not @return?)
(if (= @i (max x (- x)))
(cond (true? @even-or-maybe-odd) (do (swap! return? (fn [r] true)) (swap! result (fn [k] true)))
(false? @even-or-maybe-odd) (do (swap! return? (fn [r] true)) (swap! result (fn [k] false))))
(do (swap! i (fn [n] (+ n 1)))
(swap! even-or-maybe-odd (fn [n] (not n))))))
@result))
(defn is-even?
[x]
(if (even? x) "Even" "Odd"))
But you didn't implement your own... you just wrapped the built in. edit: How did I look past the first fn? π
I am sorry I don't understand. I overrode the even?
with my own implementation in the snippet above right?
Oh... I'm so blind tonight. You're right... I glanced past the first, big function and just looked at 'is-even?' :shame:
You can use zero?
instead of (= 0 ...)
clj-fmt
I think.
There are plugins for Leiningen and Boot too I think.
BTW folks, for "noobs", you can usually find folks will be more attentive and go into more detail in #beginners (since folks opt-in there to help new Clojurians).
@mihir Your algorithm is linear in the size of the number which is why it is slow for larger numbers.
You also have a lot of unnecessary complexity in your code. (swap! my-atom (fn [_] value))
could just be (reset! my-atom value)
-- but that's all very procedural since you're using a lot of mutable state. Very non-idiomatic for Clojure...
Anyone familiar with https://github.com/dm3/clojure.java-time? Iβm trying to set the seconds, and ms of a local-time to 0 but itβs not clear to me how to do that.
@jayzawrotny Sounds like you want to truncate to minutes?
(jt/truncate-to (jt/local-time) :minutes)
is that what you want?I looked at that function
Truncates this entity to the specified time unit. Only works for units that
divide into the length of standard day without remainder (up to `:days`).
` made me think it canβt go more precise than days?@mihir I don't think threading helps readability there -- and a simple if
is going to be clearer than using a lookup map
(defn even? [n] (if (zero? (mod n 2)) "Even" "Odd"))
seems simpler to me"up to :days" means it can't be bigger than days -- but it can be smaller.
Oof it works π© I should have just tried it! Overthought it. So up to means from right to left in this case?
It means (from the smallest unit) up to :days π
Ah, right! So itβs because 1 day === 1000ms 60s 60m * 24h where as :weeks, :months, etcβ¦ would require some kind of calendar knowledge?
Yup, that's right!
We use java-time
a lot at work -- we're using it to replace a combination of date-clj
and clj-time
(the latter wraps Joda Time which is essentially deprecated since Java 8 added Java Time).
I really like java-time
.
First time working with time in Clojure, found clj-time but it was deprecated from the README so working with java-time. The API definitely makes more sense to me than clj-time.
Especially the format function
Yeah, I deprecated clj-time recently in favor of pushing people to java-time. Michael and I will continue to maintain clj-time for the foreseeable future... but I don't think it'll gain many new features at this point.
Ack! I had not recognized you were one of the authors, I didnβt mean to be so blunt. πΆ
Hahaha... I inherited clj-time so I'm not emotionally attached to it π I like that everything is UTC but sometimes the API can be very verbose... and Joda Time definitely have some quirks (but at least it's value-based).
I've sunsetted a bunch of my own libraries over the years as I've realized there are better ways to do stuff...
(and I've been doing OSS for nearly 30 years now so there's a long trail of abandoned stuff behind me!)
Hah thatβs pretty impressive! Iβm having trouble quantifying my experience. Been using github for about 8 years now but havenβt really been all that active in any particular OSS community or project. Quietly released a couple of libraries across a couple of languages but so far all my learning and experience has pulled me to Clojure so here I am.
If you donβt mind for the sake of conversation: Any libraries you are particularly proud of or would be upset to sunset?
clojure.java.jdbc
is probably the one I'm most invested in these days.
I recently took over core.cache
and core.memoize
and a while back I took over tools.cli
-- but I haven't really formed any attachment to those yet. Somewhere in the middle would be boot-new
and clj-new
(a port from Boot to clj
/ tools.deps
).
And HoneySQL
is another recent adoption. It is in need of some consistency love I think. Still figuring out how best to move forward with that one.
I've been maintaining java.jdbc
for about seven years now and it's really grown as I've grown with Clojure... so I've made a lot of mistakes along the way but I'm really happy with most of the library now and I think it's a lot more consistent (and a lot more powerful) than it was when I took it on (as clojure.contrib.sql
in the old, monolithic Clojure Contrib library back in the 1.2 days).
jdbc is a big deal. An insane amount of projects depend on that. Is that project stressful? Iβll have to check some of those out as I havenβt encountered them but what drew you to Clojure in 1.2?
Been writing a lot of Swift code lately. holds head in hands Just a thank you to @richhickey for values-first so I don't have to craft boilerplate abominations like this in Clojure just to do copy-on-write structs:
public struct PIDParams {
var kP: Double
var kI: Double
// ...etc.
}
protocol Cloneable { // uggghh
func clone() -> Self
}
extension PIDParams: Cloneable {
func clone() -> PIDParams {
return PIDParams(
kP: , kI: , kD: self.kD,
deadzone: self.deadzone,
minOutput: self.minOutput, maxOutput: self.minOutput,
minWindup: self.minWindup, maxWindup: self.maxWindup)
}
}
// in order to do convenient struct mutation:
let pid1 = PIDParams(kP: 0.5, kI: ...)
var pid2 = pid1.clone()
= 0.7
haha, I love it when your langauge can't deal with minX: self.x, minY: self.y
(* 1000) in any reasonable way
Hi all, somebody using clojure.jdbc? what method (eg: execute!
) should one use for a query like βinsert into .. returning idβ (postgres)?
Hello. I'm testing the new alpha7 but I cant understand this error message: https://gist.github.com/souenzzo/17ec14b0ecbf676b7cb9af79aff5409b
Syntax error macroexpanding clojure.core/ns at (1:1). Cause: Call to clojure.core/ns did not conform to spec.
((:import [org.agrona.concurrent UnsafeBuffer] [java.nio.ByteOrder])) - failed: Extra input spec: :clojure.core.specs.alpha/ns-form
and the issue is that the ns decl should be
(ns ^{:no-doc true} onyx.state.serializers.windowing-key-encoder
(:import [org.agrona.concurrent UnsafeBuffer]
java.nio.ByteOrder))
So (ns foo (:import [some.class])) is not working anymore
that never did work - it just silently did nothing
against the new core specs
is this intentional?
I'm using luminus and I did lein rollback and now lein migrate won't work... wth
@bronsa So I don't have to import anymore if don't need an alias?
great thanks for clarification
why does it say "Migration reserved by another instance. Ignoring." and now the databse is empty, this is madenning
im just going to create a new project and copy all the files over..
Is there a way with transducers to instead of using conj function to tell it to conj by 3, or partition within the tranducer expression, or would that just have to be done afterwards.
@theeternalpulse your profile picture is amazing.
thanks lol, I made it myself
@theeternalpulse there's a partition-all
transducer, if that's what you're looking for
bikeshedding question: When you have a boolean value in a config map, how do you name it?
(def option-a {:do-the-thing true})
(def option-b {:do-the-thing? true})
(def option-c {:should-do-the-thing? true})
If i'm going to be serializing to JSON I usually leave the question mark off. Otherwise I tend towards the middle option
+1 for {:the-thing? true}
(unless you're serializing to JSON -- leave the question mark off)
(and, yes, deliberately without verb/sentence prefixes)
Looks like option b (more or less) is the big winner, the guy doing my PR is going to be so pleased
some fun trivia I learned today
ledger.tools.event-ids-test=> (.keySet {:a 0 :b 1})
#{:a :b}
ledger.tools.event-ids-test=> (get (.keySet {:a 0 :b 1}) :a)
nil
.keySet seems pretty rarely used even with in the Clojure implementation itself -- a couple of places in the compiler. Did you have a reason for using it vs. (set (keys my-map))
?
yeah - it was more of a "wouldn't it be clever to use this built in method instead of (comp set keys)
" followed by WAT
and easy to replace it with (comp set keys)
given that result, of course :D
Oh, yeah. Another weirdness there, of course: (get (java.util.TreeSet. [1 2 3]) 1)
=> nil
There are other good reasons to avoid using mutable collections in Clojure, too.
ledger.tools.event-ids-test=> (.contains (.keySet {:a 0 :b 1}) :a)
true
- of coursee.g. clojure.core/hash is not consistent with clojure.core/=, so you typically cannot find mutable Java collections if you try to use them as set elements or map keys.
yeah, mutable items as keys only make sense with object identity
Hi. Does anyone know what is the status of clojure-in-clojure implementation? is it any usable in production?