Fork me on GitHub
#beginners
<
2016-10-05
>
naomarik02:10:31

what does req-un mean in clojure spec?

naomarik02:10:39

specifically the un

naomarik02:10:26

ahh unqualified

singen07:10:46

Is there a built-in way to explode a val into keyword and value, such that I would be able to insert it into a map? I seem to be repeating myself quite a lot. I want to go from (def x 10) to :x 10

rauh08:10:07

@singen Could you give an example? Not sure I understand

singen08:10:44

@rauh (conj {:a 1} {(keyword (name 'x)) x})

singen08:10:59

I guess I was looking for an idiomatic way to do that

rauh08:10:13

@singen Why not just (assoc {:a 1} :x x)? Maybe rethink your data structure? If you need to use x like that, I'd def it as (def params {:x 10})?

singen08:10:57

Well, usually x is an argument to a function and not a def

singen08:10:38

My data-structure come from JSON so there’s not much to re-think about

rauh08:10:54

Give a full example, how you'd call the function

singen08:10:24

(defn add-timestamp-and-id [entry id]
  (-> entry 
    add-timestamp
    (assoc :id id)))

singen08:10:44

Where entry is a map, of course

rauh08:10:58

That looks good. That's how I'd write it.

singen08:10:27

I have functions which have several assoc lines repeating the val and keyword names

singen08:10:24

It’s not a problem in any way, just looks a little ugly

rauh08:10:54

You could always do zipmap and merge instead.

rauh08:10:32

But dynamically generating a keyword from symbols is not idiomatic (though possible with macros) and I'd find it unnecessarily confusing code

singen08:10:04

That’s really the essence of the question

rauh08:10:14

Relying on the name of local symbols would lead to bugs as soon as somebody renames a local symbol

singen08:10:24

I have another question! If you are defining a protocol and the implementation is mostly functions (not data), is it proper form to use deftype as the implementation?

singen08:10:23

Clojuredocs seems to agree with that.

alexmiller12:10:02

You can just use reify

alexmiller12:10:46

Most uses don't call for deftype

singen14:10:16

I had to revert back, I couldn’t figure out how to import my functions neatly into namespaces

alexisvincent15:10:17

Hi guys, I have an issue with futures that I just can wrap my head around. Calling future-cancel on a future I have, refuses to set Thread/interupted to true. If im not mistaken this is the expected behaviour?

alexisvincent15:10:36

(future
            (try
              (do
                (while (not (Thread/interrupted))
                  (println (Thread/interrupted))
                  (let [tmp-buffer (byte-array 10000)
                        cnt (.read target-data-line tmp-buffer 0 (alength tmp-buffer))]
                    (when (> cnt 0)
                      (s/put! stream tmp-buffer))))
                ;(.write output-stream tmp-buffer 0 cnt)))

                (s/close! stream))
              ;(.close output-stream))
              (catch Exception e (println e))))

alexisvincent15:10:16

this is the future. But Thread/interrupted is perpetually true

alexisvincent15:10:06

future-cancelled? and future-done? both return true

michaelmrose18:10:49

if I understand it properly the reason it has to be in the tail position is that it is optimized into a loop wherein it just changes the values to avoid blowing the stack this means that you can't do anything outside the loop because that would entail keeping that stack frame and its data including stuff left to do after the function itself returns