This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-04-27
Channels
- # aws (19)
- # babashka (47)
- # beginners (111)
- # boot (3)
- # bristol-clojurians (3)
- # chlorine-clover (2)
- # cider (13)
- # cljs-dev (8)
- # clojure (143)
- # clojure-europe (11)
- # clojure-germany (10)
- # clojure-italy (3)
- # clojure-losangeles (1)
- # clojure-nl (1)
- # clojure-spec (6)
- # clojure-survey (3)
- # clojure-uk (42)
- # clojurescript (229)
- # conjure (131)
- # cursive (21)
- # data-science (18)
- # datomic (4)
- # emacs (21)
- # events (2)
- # figwheel-main (12)
- # fulcro (18)
- # graalvm (1)
- # hoplon (40)
- # jobs (1)
- # joker (17)
- # kaocha (1)
- # lambdaisland (1)
- # off-topic (19)
- # rdf (7)
- # re-frame (31)
- # reagent (26)
- # reitit (20)
- # rum (4)
- # shadow-cljs (106)
- # sql (17)
- # testing (5)
- # vim (2)
I am playing around with the prepl and sending highlighted sections from kakoune to the repl for eval. How can I use libraries like pretty or glow with it?
what is a quick and easy way to convert this:
["Host: " "User-Agent: ExampleBrowser/1.0" "Accept: */*"
"Content-Type: application/x-www-form-urlencoded" "Content-Length: 21"]
into a map with keys and values:
{:Host "" :User-Agent "ExampleBrowser/1.0" :Accept "*/*" :Content-Type "application/x-www-form-urlencoded" :Content-Length "21"}
there is probably a simpler way but here's something you can use
(into {} (map #((juxt (fn [x] (keyword (first x))) second) (clojure.string/split % ": ")) vector-of-header-strings))
here's a slightly more concise version
(into {} (map #((juxt (comp keyword first) second) (clojure.string/split % ": ")) vector-of-header-strings))
An alternative that avoids juxt
(require '[clojure.string :as str])
;; coll is the input here.
(->> (map #(let [[k v] (str/split % ": ")]
[(keyword k) v]) coll)
(into {}))
@U883WCP5Z small nitpick the regexp should be #": "
đ
thanks for the solution
My bad đ
. I always expect str/split
to be able to split by string and character.
anybody can help? does my lazy-seq
program has any problem?
(def primes
(letfn [(primes-parts [part]
(lazy-seq
(when-let [[p & xs] (seq part)]
(let [cannot-div? #(not= 0 (mod % p))]
(cons p (primes-parts (filter cannot-div? xs)))))))]
(primes-parts (iterate inc 2))))
here
(count (take 2000 primes)) ;; 1789
Though I see your problem. It is possible to hit StackOverflowError.
(count (take 10000 primes)) ;; StackOverflowError
And since the def is cached, subsequent calls to count
will return values till the stack overflow, so you will see the behaviour you are seeing.
(count (take 10000 primes)) ;; 4153
The logic in your function is dense and I might be wrong, but I think you have the (iterate inc 2)
running through an increasing number of filters, so after you have calculated 1000 primes, you have 1000 filters piled up.
No, not unless you change your logic.
I adapted it, without the defvar etc. here, https://github.com/Hindol/euler/blob/master/src/com/github/hindol/euler/numeric.clj#L23
I did some prime sieves as well, benchmarking and perfecting. https://github.com/Hindol/euler/blob/master/src/com/github/hindol/euler/sieves.clj
here is my sieve,i think it hits the same problem
(defn divsible? [a b]
(zero? (mod a b)))
(defn sieve [stream]
(lazy-seq
(cons (first stream)
(sieve (filter #(not (divsible? % (first stream)))
(rest stream))))))
(def primes (sieve (iterate inc 2)))
sure,but here I have network problem,so that i don't like github,but gitee made by we Chinese
By the way, please read this section: https://clojure.org/reference/java_interop#primitives In short, Clojure uses boxed math most of the time and they are terribly slow. You will need to do primitive math and sometimes need to create Java primitive arrays to solve most of the problems fast.
I've read articles online where they said that if Clojure (or Lisp in general) was well optimized, it can run as fast as C. At first it was a joke maybe because I've always heard that there is no faster language than C and always had this thought that Lisps are not the fastest (maybe this is due to my lack of knowledge), but can anyone verify this? I've even heard that there is a interpreter written in Lisp that can read 30000 lines of code per second too and that sounds pretty darn fast.
That depends a whole lot on how you write your programs. Using Neanderthal or TVM, you can achieve great numerical performance. Also hard to decouple from the question of "is idiomatic code fast"? This is typically what you will hear from C or Rust people. https://neanderthal.uncomplicate.org/ https://github.com/techascent/tvm-clj http://clojure-goes-fast.com/
This is something that got argued about a lot on usenet back in the day. There are cases where fortran still performs better than c for example (where the c advocates retort that using a fortran lib if it's faster is idiomatic in c). Similar for assembler macros.
I think the cases where lisp or java are faster than c come down to their GC performing better than naiive usage of malloc, and on newer hardware that difference goes away because C uses cache lines better.
if you are looking for high level languages that rival c in performance (as your main criteria) check out OCaml (more high level / well designed, great perf if you don't need threads) or Rust (just plain fast, but less cleanly designed)
here,I want to optimize my solution for project euler,in problem 4 and 9,can anybody help me
(defn solution-4 [] ;; this solution has generator too many repetitive nums
(reduce max
(for [n1 (range 999 99 -1)
n2 (range 999 99 -1)
:let [num (* n1 n2)]
:when (is-plalindrome? num)]
num)))
(defn solution-9 [] ;; this solution solves too slow
(let [rng (range 1 1000)]
(first (for [a rng
b rng
c rng
:when (and (= 1000 (+ a b c))
(= (* c c)
(+ (* a a) (* b b))))]
(* a b c))))
the big thing here is remove boxing as much as possible (boxed math is about 100x unboxed math)
(set! *unchecked-math* true)
(defn solution-9 [] ;; this solution solves too slow
(let [rng (range 1 1000)]
(first (for [^long a rng
^long b rng
^long c rng
:when (and (= 1000 (+ a b c))
(= (* c c)
(+ (* a a) (* b b))))]
(* a b c)))))
The output of for is still a lazy seq, so the final output will be boxed right? Just clarifying. I get that the point is to avoid boxed math and not boxing entirely.
you could turn the for into a loop/recur with early exit on first success to avoid that (although I don't know that it would help you much)
is probably lots faster without any other more invasive changes
Let's take a hypothetical case. We have two boxed numbers, a and b. Which of the two is faster? 1. Multiply boxed. 2. Unbox, multiply and box again.
I think the implementation of 1 is 2
what is the correct way to call map
to iterate over a hashmap ?
(map (fn [kv] (prn (key kv) (val kv))) {:a :b})
you can just call it. also common to destructure (map (fn [[k v]] (prn k v)) {:a :b})
iI see, I need an extra set of brackets. (map (fn [[key value]] ...) the-hash-map)
Thanks
hey guys how do i remove a specific key from a list of maps? if have a list:
'({:a 1, :b 2} {:a 2, :b 3}) -> '({:b 2} {:b 3})
have you used map
?
basically what you need would be a one liner, using map and a function calling dissoc
is there a conventional way to avoid shadowing? for example, in python, you append an underscore to any name that would otherwise shadow.
many people shadow on purpose - you can't mutate someone else's scope out from under them except by using def (which is not used in application logic, only during development)
if you want a new derived value but don't want to shadow the old one, an idiom from math is x
then x'
then x''
etc.
which works just fine
but in most cases like these I find I can safely shadow, I no longer need the old value
shadowing things like key
and count
and val
is extremely common (many don't even know that key and val are functions in core)
that's a different kind of shadowing - just using the same name for an unrelated meaning
and I'd never use key'
to mean a key of something for example - the semantics don't fit!
I'd just use k
or key
and assume people realize it's not the one from core
fair enough. in some inner scope there may not be any real stakes if you shadow, i guess.
right - function calls inside the scope won't inherit your shadowing, there is little danger of invisible breakage
and in idiomatic clojure nearly all our bindings should be local to some small inner scope
reify
does not accept return type hint on the argument vector? I can only make it work when I place the hint before the method name.
did you read the docstring?
I just grepped for ^ on ClojureDocs. But now that I read the whole docstring, I see it. Many other forms support hint in both places and I read hint on the arg vec is preferred. I hoped that would work.
you can just (doc reify)
in your repl too
some parts of Clojure related to interop mimic Java's syntax by putting the return type hint before the method
for a deps.edn project. how do you load a .env
file?
I am not sure what an .env
file contains, or how you load it in a project that is not a deps.edn project. What other kind of project loads one?
I'm trying to use System/getenv
to get the environment variables. but it only reads global environment variables instead of ones in my project
Its worth to look at https://github.com/juxt/aero. Define all you need in your deps.edn by turning on profiles like dev or prod .. I find that having all your variables declared in your deps.edn simplifies things a lot .. especially when youâre trying to understand things like âoh I wonder what port is env.PORT in prodâ
.env comes with its own helper executable that reads and exports those variables. If you run java from the same shell, it will see those variables, otherwise it won't.
.env is a tooling convention for things like docker, I've found that in some cases I can use env $(<.env) clj
to get a repl with those settings applied
depending on the values in your .env, you might need to set a custom IFS too
env is a unix command that takes k/v pairs for env vars and then launches a command using those env vars
and I don't think .env files are even meant to work with env
they just happen to, in many cases
having spent sometime working on shell scripts that do things like dump the environment using env and then trying to reload it, nothing works, everything is broken, and it peeves me greatly
problem is there's nothing that provides environment variables from a file without some other infrastructure like docker
clj
itself has no mechanism to set env vars, as that's something a stock jvm can't do, it's non portable
(there are third party libs that can set in-process env vars, and eventually someone might make a clj task that does it... but support would be on a per OS basis)
@kelvin.mai002 also, if it doesn't need to be env vars (eg. if it could be system properties instead, which have a similar API in a running vm) those can be set with jvm args which you can define in a deps.edn alias
Ya, if you need to set configs in deps.edn, just use Jvm properties instead of env properties
Ha! Often I reply to a thread then come out and see that the same thing has been answered outside.
Hi, I have a question about ring sessions. Do I have to assoc-in the session values everytime I render a template in my web app?
you should only need to touch the session if you want to add or remove data for that user's session
for the in-memory session the data won't last across reboots, for the cookie store all the normal rules for cookies apply
Oh great! I thought I had to assoc-in the values everytime I was changing pages! Thanks!
the session is per-user not per-page
I was expecting it was like that, as it is in flask sessions in Python
(as long as yo aren't eg. using a reverse proxy to make the page show up as coming from different hosts...)