This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-07
Channels
- # babashka (20)
- # beginners (72)
- # biff (7)
- # calva (15)
- # clj-kondo (36)
- # clj-on-windows (5)
- # cljs-dev (30)
- # clojure (27)
- # clojure-europe (2)
- # clojure-nl (1)
- # clojure-norway (6)
- # clojure-uk (2)
- # clojurescript (26)
- # conjure (14)
- # cursive (17)
- # events (1)
- # honeysql (5)
- # hyperfiddle (1)
- # leiningen (1)
- # off-topic (3)
- # reitit (5)
- # shadow-cljs (1)
- # xtdb (13)
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)
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}
?
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?
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/
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
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.
https://gist.github.com/joelturnbull/1624148 https://squirrel.pl/blog/2012/10/16/hello-clojurescript-with-jquery/
For js libs which not have clj wrapper i simple called their by js-invoke https://cljs.github.io/api/cljs.core/js-invoke
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" "*"}}
You can use Ring wrap-cors
middleware. See: https://github.com/prestancedesign/todo-backend-reitit/blob/master/src/todo_backend/core.clj#L61
Not tested but you could do something like this:
user=> (take x (drop y (clojure.string/split-lines (slurp file))))
that one's better though 😅
this seems like a viable option instead of slurp https://clojuredocs.org/clojure.core/line-seq
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?
That's above my pay grade unfortunately. But they both return LazySeq
@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)))
you can modify this to grab which lines you want
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.
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? 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.
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
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" }
}}
/>
);
};
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}}
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
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 uberjar
and run the resulting jar it opens a repl :thinking_face:
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?
What should exactly be in the main function? The https://github.com/weavejester/compojure/wiki/Getting-Started 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 LoopI 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
I thought Neptune supported Cypher natively now
Oh, I didn't know that. I just needed CosmosDB, which doesn't support, but I added Neptune just because its easy
Cool project!