Fork me on GitHub
#beginners
<
2022-04-10
>
Frederico Benevides00:04:55

Hi there. I have this code that is using a macro that I created called when-time-valid. It's a when but check the time and if is valid it's going to execute the body. The problem here when is not valid, it is creating a list full of nil: '(nil nil nil). So I can't use later in another function the function empty? to check if the list is empty. What is the best way to make this code return an empty list.

(defn load-accounts-to-run []
  (for [accounts (:accounts @schedules)]
    (let [account-id (:id accounts)
          time (:time accounts)
          step (:step accounts)]
      (when-time-valid
       time
       (log (str "Account-" account-id ": is ready to be run"))
       {:account-id account-id :step-index step}))))

hiredman00:04:31

For is not a loop

hiredman00:04:57

I would shift the let into the for using :let

hiredman00:04:14

Then not write a macro, and use :when in the for as well

Frederico Benevides00:04:26

I'll take a look at this, thanks

hiredman00:04:14

Usually if your question starts with "so I wrote this macro" the solution starts with not writing macros

hiredman00:04:46

For is also lazy, so if you are doing anything that is getting the current time inside the for, you are asking for a surprising answer

hiredman00:04:23

It is usually going to be better to get the current time outside of the for, and using that consistent basis for every item

☝️ 1
mister_m01:04:38

I find myself with a lot of XML on my hands: specifically RSS/ATOM feeds. Is data.xml the recommended way to pull data out? What I'd like to do is basically extract top level feed data, as well as RSS or ATOM entries into a map that I can later serialize. I'm not super familiar with XML parsing techniques but but it would be great if I could just convert the resulting clojure.data.xml.Element I get from xml/parse-str into a map where tag names are keys and the tag content are values so I can just mess around with a clojure map. What would be the path of least resistance for extracting all the <entry> tags in a feed like into a map?

mister_m01:04:06

Should I look into transducers for this?

didibus08:04:31

Use clojure.xml

didibus08:04:39

It returns a map of the XML

didibus09:04:09

data.xml is for more advanced XML processing, generally when you want to lazily process XML, for when your XML is really really big and you don't want it all loaded in memory.

didibus09:04:01

I think it would just be:

(ns my-ns
  (:require [clojure.xml :as xml]))

(xml/parse "")

xbrln08:04:02

hello 🙂, am using https://github.com/weavejester/cljfmt for code alignment and would like to have associative syntax alignment. Does anyone know if its is possible ? I see an old thread https://github.com/weavejester/cljfmt/issues/36 discussing about it, but not sure if it was added into the library as a configuration option.

lread12:04:52

Hiya @U025C8SL84T, the answer is currently no.

nottmey09:04:48

(keyword (str :db.type/long))
=> ::db.type/long
this is biting me 😕

nottmey09:04:38

do I really need to parse the string to remove the “:” before passing it back to keyword? Or am I missing something?

nottmey09:04:26

ahhhh this works:

(symbol (str :db.type/long))
=> :db.type/long
Edit: It doesn’t! The resulting symbol is no keyword! very weird 😅

nottmey09:04:46

you mean I should be stringify my keywords without str but with symbol and then str ?

nottmey09:04:22

yes, that roundtrip works too

(keyword (str (symbol :db.type/long)))
=> :db.type/long
but is it better?

Ben Sless10:04:11

Use name

☝️ 1
flowthing10:04:19

Or pr-str and read-string (since I'm assuming you need to retain the namespace part of the keyword):

(-> :db.type/long pr-str clojure.edn/read-string)
;;=> :db.type/long

nottmey10:04:17

yes, I need to retain the whole thing and have an indication that it’s a keyword not a plain symbol (with the leading “:”) ok, clojure.edn/read-string is a nice tool, thanks @U4ZDX466T

nottmey11:04:05

TIL: (symbol ":db.type/long") yields something that looks like a keyword but isn’t!

delaguardo11:04:38

read-string is dangerous because it can evaluate arbitrary input. https://clojuredocs.org/clojure.core/read#example-542692d5c026201cdc327056 I suggest using clojure.edn/read-string instead

delaguardo11:04:30

So this should be enough (clojure.edn/read-string (pr-str :some.ns/foo))

flowthing11:04:13

That's what I proposed above. 🙂 But yes, it's good to note clojure.core/read-string is unsafe by default.

delaguardo13:04:47

Ah, right, i read the first line where the read-string looked like the one from the core. My bad

didibus17:04:34

You want to use namespace and name on the keyword.

didibus17:04:17

(let [k :foo/bar]
  (keyword (namespace k) (name k)))

didibus17:04:12

If you look at the signature of keyword and symbol you see that it is kind of documented.

(keyword name) (keyword ns name)
The single arity overload takes the name of the keyword, so it's normal it returns a keyword with the string as the name. Same thing for symbol. But I admit it could be made more explicit.

nottmey20:04:15

@U0K064KQV yes, I considered that, but when I have a string “:db.type/long”, parsing, splitting and using namespace/name is a bit tideous compared to clojure.edn/read-string

didibus21:04:35

If you've serialized the keyword to a string, you need to use whatever you used to serialize it to also deserialize it, since how it was serialized is a matter of what you used to do so and only that will know the correct scheme for serializing/deserializing. If you did use EDN, then clojure.edn/read-string is the correct thing to use to get the keyword back.

👍 1
Eddie18:04:23

I have a need for very fast manipulation of 2D array-like structures containing bytes. My prototype implementation uses vectors of bytes. What are people’s thoughts on the best tool for making this fly? I am currently considering either transients or java byte arrays. Is there anything else I am missing?

hiredman18:04:32

Byte arrays will always be faster

hiredman18:04:12

There are purpose built matrix libraries, some either specifically for clojure, but I suspect those focus on doubles

Eddie18:04:08

Thanks! That is helpful. If I can relax the requirement of bytes and use doubles, do you think any of the matrix library will be faster than byte arrays? Each matrix is small but there are many of them.

hiredman19:04:35

Hard to say, it will depend how much your operations align with the libraries

hiredman19:04:25

I don't do much matrix stuff, but https://github.com/uncomplicate/neanderthal is the library I was thinking of

Eddie19:04:04

I have tried to get on board with neanderthal a few times in the past but I always fail to get it working in my development environment, which makes me scared about how easy deployment will be. Perhaps its time to try again.

dorab23:04:53

Might also want to look at https://github.com/techascent/tech.ml.dataset and see if that fits your needs.