Fork me on GitHub
#beginners
<
2022-08-07
>
David00:08:39

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

seancorfield00:08:52

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

seancorfield00:08:43

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

seancorfield00:08:12

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

seancorfield00:08:34

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.

seancorfield00:08:36

So you could write

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

David00:08:42

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

David01:08:55

Changed to:

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

seancorfield01:08:11

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

seancorfield01:08:56

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
David02:08:23

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 https://day8.github.io/re-frame/ and shadow-clj Is re-frame appropriate for this? Can I even view pdf files? If so what library?

Alexander13:08:31

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. https://reagent-project.github.io/

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

Alexander14:08:31

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.

Alexander14:08:21

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.

Alexander14:08:01

For js libs which not have clj wrapper i simple called their by js-invoke https://cljs.github.io/api/cljs.core/js-invoke

Adham Omran05:08:46

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

didibus02:08:34

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.

dumrat11:08:58

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.

Alexander13:08:25

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

Alexander13:08:58

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

sheluchin14:08:06

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 "...")
            (s/split-lines))
        start end)

☝️ 1
Ben Lieberman14:08:19

that one's better though 😅

sheluchin14:08:26

Will that be efficient for large files?

sheluchin14:08:35

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 https://clojuredocs.org/clojure.core/line-seq

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

sheluchin15:08:40

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

sheluchin15:08:29

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

sheluchin15:08:39

Thanks @U03QBKTVA0N. There's a nice bit on that approach in this article https://blog.michielborkent.nl/transducing-text.html, I just wasn't sure if skipping lines would be an efficient operation.

ghadi16:08:18

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

sheluchin16:08:57

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

ghadi16:08:31

Is your program doing more work than this step?

ghadi16:08:07

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

ghadi16:08:31

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

sheluchin16:08:00

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

ghadi17:08:08

@U03QBKTVA0N

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

ghadi17:08:42

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

gratitude-thank-you 1
Alexander22:08:41

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. https://stackoverflow.com/a/25953671

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"
            }
        }
    }
})(TextField);

const InputTextField = ({label}) => {
    return (
        <CssTextField
            label={label}
            variant="outlined"
            InputProps={{
                style: { color: "white"  }
            }}
        />
    );
};

ahungry19:08:04

Isn't it just a function returning a function?

rolt19:08:53

you can have a look at how it's done here: https://github.com/arttuka/reagent-material-ui 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
Alexander23:08:05

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. https://cljs.github.io/api/cljs.core/clj-GTjs

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

dpsutton18:08:12

do you have the repo public anywhere?

dpsutton18:08:56

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 https://github.com/weavejester/compojure/wiki/Getting-Started doesn't tell about it

dpsutton20:08:03

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

dpsutton20:08:25

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!