Fork me on GitHub

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?


I want better formatted output


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"}

Chris McCormick04:04:02

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))

Chris McCormick05:04:27

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 {}))

👍 8

@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] 
             (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))))
(count (take 2000 primes)) ;; 1789


Works on my system,

(count (take 2000 primes)) ;; => 2000


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


is there any way to avoid it


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.


Did that make sense?


I think there must be better generator for primes


Yes, there is. One that Rich Hickey wrote.


can you show me that,or link


wow,your primes is so quick


did you write euler-project too?


here is my sieve,i think it hits the same problem

(defn divsible? [a b]
  (zero? (mod a b)))

(defn sieve [stream]
   (cons (first stream)
         (sieve (filter #(not (divsible? % (first stream)))
                        (rest stream))))))
(def primes (sieve (iterate inc 2)))                    


No no, I am just solving some problems from project-euler.


have you solve any other problem in euler-project


I have solved close to 130 I think.


wow,where is your github for euler?


The same one that I shared. I don't share solutions in the git repo though.


It is just a collection of helper functions.


What's your euler alias?


so do i ,for i always forget that


I'm not sure where are you from,may i ask


Here is my key, you can add me. 68392_rXI1S5R6kIUPxgK21FELKaRqAJ8RVBVR


I am from India.


Nice. Add me when you get the time.


sure,but here I have network problem,so that i don't like github,but gitee made by we Chinese


That's my key for project euler.


do you know how to make private message in slack,we talk too much useless words


By the way, please read this section: 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.


It's in a thread, so that is okay.

Chris K09:04:43

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.

Chris K14:04:35

thxs for a great answer


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)]

(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))))

Alex Miller (Clojure team)13:04:43

the big thing here is remove boxing as much as possible (boxed math is about 100x unboxed math)

Alex Miller (Clojure team)13:04:27

(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.

Alex Miller (Clojure team)14:04:57

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)

Alex Miller (Clojure team)13:04:00

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.

Alex Miller (Clojure team)14:04:36

I think the implementation of 1 is 2


yup ^ takes the same amount of time given std dev on my machine

Jim Newton14:04:40

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})

Jim Newton14:04:16

iI see, I need an extra set of brackets. (map (fn [[key value]] ...) the-hash-map)


Yeah, that's because taking a sequence over a map gives you pairs.


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


ah i see, thanks

Michael Stokley17:04:18

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.

👍 4

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 countand 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

Michael Stokley17:04:43

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.

Alex Miller (Clojure team)18:04:11

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.

Alex Miller (Clojure team)18:04:37

you can just (doc reify) in your repl too

Alex Miller (Clojure team)18:04:57

some parts of Clojure related to interop mimic Java's syntax by putting the return type hint before the method

Kelvin Mai19:04:28

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?

Kelvin Mai19:04:21

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 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


the idea that .env files are standardized make me so mad


env is the worst


and I don't think .env files are even meant to work with env


they just happen to, in many cases


good, ditching env could maybe make things better


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


better to just use files 😉


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...)