Fork me on GitHub

Curious the best way to approach this: I have a hash-map where keys refers to a specific prime number, and the value is the number of times it shows up in a given factorization. So e.g. the number 10 would be represented in the hashmap as {2 2, 5 1} (i.e. 2 2 5 * 1, or "two 2s", and "one 5"). What I'd now like to do is raise the key to the val of the value, and then multiply those numbers together to get the correct figure (in the above case 2^2 * 5^1 = 10). I've been reading over the map and hashed-map docs, and can't seem to find info about this type of transformation. I defined my exponent function, but can't find a way to essentially (map exp(keys vals) {2 2, 5 1}) ;


map on a hash map will process it as a sequence of key/value pairs. You can call key on a key/value pair to get the key and val to get the value. So the mapping function you want is essentially (fn [pair] (exp (key pair) (val pair)))


But you can also use destructuring in the function arguments to extract the key and value: (fn [[k v]] (exp k v))


Or you can use an anonymous function without destructuring: #(exp (key %) (val %)) Whichever you find easiest to read.


However, since you want to process all those values down to a single result, you really want a reduce -- and for reducing a hash map, there's reduce-kv which automatically extracts the key and value for each pair.


So you could write

(reduce-kv (fn [acc k v] (+ acc (exp k v))) 0 data)


Amazing, thank you! Will play around with each option 🙂


Changed to:

(reduce-kv (fn [acc k v] (* acc (exp k v))) 1 data)
Works like a charm! Thank you!


Oops! That's what I get for not trying stuff in the REPL when I'm distracted watching TV 🙂 Glad you got there!


BTW, 2^2 = 4 and 4 * 5 = 20, not 10 so I think you mean 20 would be represented by {2 2 5 1} and 10 would be represented by {2 1 5 1}?

✔️ 1

Yes yes yes sorry

Adham Omran09:08:26

I have my first idea to use Clojure/ClojureScript. I want to create an application that opens a PDF file, allows you to click on a point and it will insert a code 39 barcode there. What I need help with is learning material. So far I've gathered from and shadow-clj Is re-frame appropriate for this? Can I even view pdf files? If so what library?


PDF viewing it not specific clojurescript feature. You can include any js library to you cjs project an use it. For start i recommend for you use reagent. It is full feature library but more easy than others.

Adham Omran13:08:47

An example of such a library would be PDF.js correct? If so I'll begin by learning how to include that in a reagent project


Sorry i don't have idea about which lib use, recommend you select any with active support(commits). About include. You must include lib not in reagent. Any lib(reagent, pdf and other) include in clojureacript dependencies and after include you can call it in you project

Adham Omran14:08:44

Understood. I'm still learning about how the project structure works for Clojure and ClojureScript.


But js lib you can include in you html in tag script. And call it directly like in js. Maybe it is more easy solution for you.


For js libs which not have clj wrapper i simple called their by js-invoke

Adham Omran05:08:46

Thank you for the links, I have so much to learn 😄


To be honest, this is a pretty difficult project. PDFs are very hard to work with and manipulate no matter what the language you use is, it's just inherent to PDFs.


Does anyone have a sample reitit router with a proper CORS handler? Somehow I can't seem to get this working at all no matter what I try.


Cors it is common http feature. For it correct working, you must return proper cors headers in response to browser. It should look like {:body "" :headers {"Access-Control-Allow-Origin" "*" "Access-Control-Allow-Methods" "*" "Access-Control-Allow-Headers" "*"}}


My example above allow for browsers send request from any domains to you domain


Is there a way to read a specific line range from a file using Clojure?

Ben Lieberman14:08:28

Not tested but you could do something like this:

user=> (take x (drop y (clojure.string/split-lines (slurp file))))

Martin Půda14:08:34

(subvec (-> (slurp "...")
        start end)

☝️ 1
Ben Lieberman14:08:19

that one's better though 😅


Will that be efficient for large files?


I don't think so. I'm guessing it's more efficient to just call out to sed?

Ben Lieberman14:08:56

this seems like a viable option instead of slurp

Ben Lieberman15:08:48

Though also I'm guessing the Java file reading utilities are pretty fast and efficient, but that is speculation and nothing more


Since line-seq returns a lazy sequence, does (take x (drop y ...) happen in constant time?

Ben Lieberman15:08:57

That's above my pay grade unfortunately. But they both return LazySeq


Okay, thanks guys. I'll try it out and compare against sed.

Ben Lieberman15:08:14

@UPWHQK562 a very ad-hoc experiment on my part but this example offered in the clojure docs is an order of magnitude faster than the code snippet I initially gave

(with-open [rdr ( "/tmp/foo.txt")]
    (reduce conj [] (line-seq rdr)))

Ben Lieberman15:08:39

you can modify this to grab which lines you want


Thanks @U03QBKTVA0N. There's a nice bit on that approach in this article, I just wasn't sure if skipping lines would be an efficient operation.


There is no efficient way to seek to a random line unless you know the byte offset of that line


@U050ECB92 do you know if doing it through Clojure is less efficient than calling sed?


Is your program doing more work than this step?


sed also has that same limitation, it finds lines by consuming from the beginning


(The limitation is in the problem, not the tool)


> Is your program doing more work than this step? Not sure what you mean exactly. > (The limitation is in the problem, not the tool) Okay, so unless you know the byte index of the starting line, there isn't a way to skip the correct amount of bytes by providing a line number, because each line is an unknown amount of bytes. That makes sense now.



(vec (line-seq rdr))
no need for reduce+conj or (into [] ..)


(keep in mind that making a vector will read all lines into memory)

gratitude-thank-you 1

You can't read only specific line in file. This is not depend on language. Because at first you need find end of line \n or \r\n symbols. It is possible only by sequentially read file bytes. For large file it is efficient use lazy api, it read from file only when you need access to particular part on file.

gratitude-thank-you 1
Muhammad Hamza Chippa16:08:19

I am trying to convert MUI Javascript code into clojurescript but have no clue how to use withStyles feature of MUI in clojurescript. How can I replicate this code into Clojurescript

const CssTextField = withStyles({
    root: {
        "& label": {
            fontSize: "16px" , 
            color: "rgba(255, 255, 255, 0.7)"
        "& label.Mui-focused": {
            fontSize: "16px" ,
            color: "#007FFF"
        "& .MuiOutlinedInput-root": {
            "& fieldset": {
                borderColor: "rgba(255, 255, 255, 0.7)"
            "&:hover fieldset": {
                borderColor: "white"
            "&.Mui-focused fieldset": {
                borderColor: "#007FFF"

const InputTextField = ({label}) => {
    return (
                style: { color: "white"  }


Isn't it just a function returning a function?


you can have a look at how it's done here: also: don't forget that #js does not work for nested objects, use clj->js or repeat #js (e.g.: #js {:a #js {:b 1}}

🙌 1

If you call js directly from clj by js-invoke, you must pass to js function js object, not clj object. Maybe it is you mistake. Below info about convert clj obj to js obj.

Davi Suga17:08:44

Hi everyone, I started a project using Lein and ring. When I run lein ring server-headless it starts the server as expected, but when I run lein uberjarand run the resulting jar it opens a repl :thinking_face:

Davi Suga17:08:29

It's also happening in the CI/CD environment


do you have the repo public anywhere?


and some background: uberjars need a main class declared and they run the -main function defined in that class. Do you know the main entrypoint you have declared for your uberjar?

Davi Suga20:08:16

Got, will try it

Davi Suga20:08:47

What should exactly be in the main function? The doesn't tell about it


Manifest-Version: 1.0
Created-By: Leiningen 2.9.5
Built-By: dan
Build-Jdk: 11.0.11
Leiningen-Project-ArtifactId: hello-world
Leiningen-Project-GroupId: hello-world
Leiningen-Project-Version: 0.1.0-SNAPSHOT
Main-Class: clojure.main
here's the manifest of your jar. Notice it defaulted to running clojure.main. And the docstring of clojure.main/main states: > With no options or args, runs an interactive Read-Eval-Print Loop


I see you have a lein-ring plugin but I don't see you starting a webserver in your code. As such your uberjar defines how to handle web requests but doesn't seem to hook the handler up to a running webserver

Davi Suga20:08:56

What is the recommended way to start the server?

Davi Suga20:08:10

It's not clear in the doc 😅

Davi Suga20:08:10

Oh, I was looking the wrong doc lol

Daniel Craig01:08:26

I thought Neptune supported Cypher natively now

Davi Suga11:08:36

Oh, I didn't know that. I just needed CosmosDB, which doesn't support, but I added Neptune just because its easy

Daniel Craig18:08:17

Cool project!