Fork me on GitHub
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)]
       (log (str "Account-" account-id ": is ready to be run"))
       {:account-id account-id :step-index step}))))


For is not a loop


I would shift the let into the for using :let


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


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


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


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

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


Should I look into transducers for this?


Use clojure.xml


It returns a map of the XML


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.


I think it would just be:

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

(xml/parse "")


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


Hiya @U025C8SL84T, the answer is currently no.


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


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


ahhhh this works:

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


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


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

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


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


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


read-string is dangerous because it can evaluate arbitrary input. I suggest using clojure.edn/read-string instead


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


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


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


You want to use namespace and name on the keyword.


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


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.


@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


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

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?


Byte arrays will always be faster


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


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.


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


I don't do much matrix stuff, but is the library I was thinking of


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.


Might also want to look at and see if that fits your needs.