This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-04-24
Channels
- # beginners (64)
- # calva (40)
- # cider (76)
- # clara (13)
- # clojure (72)
- # clojure-dev (34)
- # clojure-italy (4)
- # clojure-nl (14)
- # clojure-poland (1)
- # clojure-uk (30)
- # clojurescript (58)
- # clr (10)
- # core-async (101)
- # cursive (31)
- # datomic (9)
- # emacs (20)
- # fulcro (2)
- # jackdaw (1)
- # jobs (3)
- # juxt (3)
- # luminus (4)
- # lumo (15)
- # mount (4)
- # nrepl (29)
- # nyc (1)
- # off-topic (27)
- # qlkit (1)
- # quil (5)
- # re-frame (19)
- # reitit (8)
- # remote-jobs (4)
- # rewrite-clj (5)
- # shadow-cljs (45)
- # spacemacs (22)
- # sql (9)
- # uncomplicate (1)
- # xtdb (14)
@dromar56 Also for
is lazy, so it will only iterate through the items if the result is consumed -- doseq
is eager and always iterates through the items.
for example:
(first (for [n (range 100)] (do (println n) n)))
only realizes the first chunk (32 items) since nothing beyond that is asked for(quite a few lazy things in Clojure work in chunks of 32 items for efficiency rather than being completely lazy)
I was actually wondering. Does the chunking also help reduce the amount of intermediate operations?
is it possible to modify an existing function, so that further usages use modified version?
user=> (defn f [] 1)
#'user/f
user=> (defn g [] (f))
#'user/g
user=> (g)
1
user=> (defn f [] 2)
#'user/f
user=> (g)
2
user=>
@joel380 you can make a thing to "shadow extend" functions. I did something like that in dispacio https://github.com/johnmn3/dispacio#function-extension
How do I get an ISO 8601 string from an instant?
(str #inst "2019-04-15T00:00:00.000-00:00")
gets me "Mon Apr 15 05:30:00 IST 2019"
I'm looking for "2019-04-15T00:00:00.000-00:00"
or "2019-04-15T00:00:00.000"
so clojure inst on jvm extends java.util.Date. if you're happy to format using java interop using builtin DateFormat classes, it can be done something like this (-> (java.text.SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ss.SSSZ") (.format #inst "2019-04-15T00:00:00.000-00:00"))
clojure itself does something like that to print the default representation of inst values, which you can see here if you're curious https://github.com/clojure/clojure/blob/28efe345d5e995dc152a0286fb0be81443a0d9ac/src/clj/clojure/instant.clj#L168
but the more general advice is if you want to play with dates, maybe use a library like clj-time 🙂
Thanks everyone!
Hi guys, I've been struggling trying to port some little test projects from Lein to deps.edn, I was using https://github.com/seancorfield/dot-clojure/blob/master/deps.edn and could get some things working, but seems like it doesn't play well with Atom and VSCode REPLs also looks like too hacky to use, at the end after spending a lot of time on it I'm wondering if it really worth the effort? With Lein all works fine and its widely supported and documented, why to use clj
& deps.edn
if they doesn't solve the whole development tooling cycle as Lein does?
is it needed to wait until it will become more standard then?
really?
I missed that then
hi all, i have a noob
question: i'm using cider
+ emacs
. Sofar i have compiled the buffer with C-x C-k
but in the cider-jack-in
REPL, i get unable to resolve symbol: ..
plus some java errors.. the code itself it is fine, but it seems to me that cider
cannot load my project namespace, because it prompts user
instead of the namespace..
If i want to do distributed data processing in clojure, what is the best trick / technique / library to do that ?
somehow onyx
is too complicated for idiot like me 😞
by distributed i mean across multiple nodes, not cores
For multiple reasons I really like Kafka. There isn't a recent client that I know of, but it's pretty easy to do Java interop. However it takes some knowledge to be able to run Kafka properly in production, also there are a lot of things you can configure, which can be overwhelming.
@pythonjokeun I don't think there's anything for Clojure that actually makes this easy or simple. There are various frameworks available but they all have gotchas and trade offs and complexities that aren't directly related to the task you are attempting.
I finally made it - lazy prime numbers generator (for ProjectEuler exercize #3). @tabidots, check it out: https://github.com/reflechant/project-euler-clojure/blob/master/src/ex03.clj
This looks pretty good! One small thing I would change is to define primes
as a variable instead of a function, since its value is always the same
also I did a quick benchmark, by switching to vectors and conj
/ peek
you can get it about 8x faster 🙂
(defn next-prime
([xs] (next-prime xs (inc (peek xs))))
([xs x]
(if (some #(zero? (rem x %)) xs)
(recur xs (inc x))
x)))
(defn primes []
(map first
(iterate (fn [xs]
(conj xs (next-prime xs)))
[2])))
(qb-stats [n 500]
(vec (take n (primes))))
;; => ["8.968601 ms" {:n 500}]
;; => ["5.522698 ms" {:n 400}]
;; => ["3.352999 ms" {:n 300}]
;; => ["1.485483 ms" {:n 200}]
;; => ["0.389093 ms" {:n 100}]
;; original definition
;; => ["62.149674 ms" {:n 500}]
;; => ["35.983781 ms" {:n 400}]
;; => ["19.734515 ms" {:n 300}]
;; => ["8.039685 ms" {:n 200}]
;; => ["1.778897 ms" {:n 100}]
here's probably how I would write it:
(def primes2
((fn gen-primes [xs x]
(if (some #(zero? (rem x %)) xs)
(recur xs (inc x)) ; x is composite
(lazy-seq (cons x ; x is prime - add to output
(gen-primes (conj xs x) (inc x))))))
[] 2))
What is this qb-stats thing? I tried to look for simple benchmarking library but couldn't find any
ah sorry, that's a little utility macro I wrote for myself, it uses Criterium under the hood
As for the vectors - I thought they ought to be faster but after numerous unsuccessfull attempts decided to make it work first then optimize. When I did it I wanted to change to vec
s right away but remembered that last
isn't good for vectors but peek
didn't come to my mind
Nice. You sure spent a lot of time on PE #3 😅 Any reason why you were so interested in implementing this as a lazy-seq though? I just checked against my unmemoized trial division filter and I’m getting roughly 2.35ms
for (time (count (take 500 (filter naive-prime? (range)))))
.
I shied away from using lazy-seqs for this application because the next value depends on ALL of the previous values
So it becomes unwieldy at a certain point. You’d be surprised how well trial division works (up to n = 1 million or so, don’t know how many primes that is). Beyond that I resorted to sieving using JVM arrays (I had to do that for #437, which requires primes up to n = 100 million, and a modified Sieve of Eratosthenes… ridiculous)
But good work, though. You probably understand lazy-seqs very well now! Much better than I do. I think I’ve managed to do Fibonacci, triangular, pentagonal, and hexagonal numbers with lazy-seqs. That’s about all I have bothered to use them for so far.
@UEQDV142J Hey, check out this guy’s lazy-seq implementation (another Eulerian, but I found his gist through Google, rather than a post of his on the forum) https://gist.github.com/kohyama/8e599b2e765ad4256f32
He managed to fold this into an extremely succinct prime factorization function: https://gist.github.com/kohyama/5b47be8255c1eb859f16
I have no idea what the performance is like though. I should test it later. I have spent the last 10 days working on factorization algos. Some simple, some ridiculously complex.
add-five there is a local binding (only valid inside the bodies of the function) that can be used to refer to the function itself
in that case even, the binding isn't needed, because the function never refers to itself
(macroexpand-1 '(defn add-five [x] (+ x 5)))
=> (def add-five (clojure.core/fn ([x] (+ x 5))))
defn is a macro that binds the anonymous fn
definition to the name passed
using def
Not only, but also for making error messages nicer.