Fork me on GitHub
#beginners
<
2016-11-30
>
lmergen02:11:23

if i have a value a, that is either a string or a vector, and i want to conjoin that into vector b, what would be my best approach ?

lmergen02:11:48

i was thinking a combination of conj and flatten, but i suspect there is a single function that could handle this

olslash02:11:58

i guess you could use into and always wrap the string in a vector?

olslash02:11:22

there's probably a nicer way

lmergen02:11:16

hmm yeah this is pure bikeshed

olslash02:11:55

usually when i bikeshed small things like this i learn something valuable

olslash02:11:00

i just try not to do it during work hours

lmergen02:11:15

this is why i took vacation haha!

roelofw06:11:15

@seancorfield no more comments on my code ?

roelofw07:11:16

I have this json file :

{"levels":[{"name":"z3","width":1771,"height":1441,"tiles":[{"x":2,"y":1,"url":""},{"x":1,"y":1,"url":""},{"x":3,"y":2,"url":""},{"x":2,"y":0,"url":"  

roelofw07:11:12

now im looking for the entry which name is equal to z4 . Within the entry Im looking for the tile's part.

roelofw07:11:22

How can I do that ?

roelofw07:11:30

Can I use two times the find command for that ? or is there a better way ?

seancorfield07:11:47

@roelofw I'm not going to be at my "big" computer until at least Sunday -- I'm off to Clojure/conj tomorrow! 🙂

roelofw07:11:52

@seancorfield no problem. Maybe then I have the last part ready. Getting the image-url of a painting

akiroz07:11:05

@roelofw something like this? (let [[z4] (filter #(= (:name %) "z4") (:levels json))] (:tiles z4))

seancorfield07:11:35

I was just typing something like that 🙂

roelofw07:11:54

oke, and json is the file ?

akiroz07:11:04

json is the parsed datastructure

roelofw07:11:41

oke, then I have to adapt things but I think I will succeed in that

roelofw07:11:46

both thanks

roelofw07:11:10

@seancorfield much fun on the Clojure/conj

roelofw07:11:49

Why does this code never comes to a end :

(defn read-image_url
  "Reads the image-url"
  [id-list]
  (pmap (fn [id]
          (let [art-objects (-> (str "" id "/tiles?key=14OGzuak&format=json" )
                                (client/get {:as :json} )
                                :levels
                                )
                url          (filter #(= (:name %) "z4"))
                tiles        (:tiles url)
               ]
            {:id  id  :tiles tiles}  ))  id-list))




(read-image_url ["SK-C-5"])
 

roelofw07:11:12

bummer, the indentation is messed up

roelofw08:11:21

o, it finisched but no output

akiroz08:11:56

well, your code doesn't actually print anything so it's normal.

roelofw08:11:29

o, i was expecting that this was printed : {:id id :tiles tiles}

akiroz08:11:17

you probably want the :body part of the response object and filter returns a seq so you want to destruct the first element

roelofw08:11:10

I will experiment on to find the right answer

roelofw08:11:17

thanks for the help

olslash09:11:09

trying to figure out a nice way to represent a fixed set of keywords as a kind of enum type

olslash09:11:33

like i have a bunch of error reasons -- :missing :bad-request :server-error, whatever

olslash09:11:46

that i want to match against-- but it seems like i'd just have to know the keyword names in every location

curlyfry10:11:01

@olslash I'd just use a set of the keywords and then use that set as a function when matching

olslash10:11:00

this is probably an XY problem tbh

olslash10:11:18

a set and a multimethod that dispatches on it does pretty much what i want i think

olslash10:11:27

trying to define a bunch of error handlers that handle the same types of thing -- and in my head i was thinking i wanted the safety of something like a protocol which says like "Can't define method not in interface" if you mess up

roelofw11:11:00

How do I get the :tiles part out of this : ({:name z4, :width 885, :height 720, :tiles [{:x 0,

roelofw11:11:37

I tought I could do it with (:tiles ..... ) but then I get a empty seq

akiroz11:11:53

@roelofw see the snippet I posted above, destruct the seq then destruct the map.

agile_geek11:11:11

^^ @roelofw yeah you have a map inside a sequence so first you need to get the map from the sequence then extract the :title from the map

roelofw12:11:13

yes, that worked

roelofw12:11:54

last problem. making 1 vector of 2 seperate vectors

roelofw12:11:18

so this one : ({:id SK-C-5, :tiles [{:x 0, :y 1, :url .... }) and this one ({:id SK-C-5, :description Het korporaalschap van kapitein Frans Banninck Cocq en luitenant Willem van Ruytenburch, bekend als de 'Nachtwacht'. ]}) get merged

roelofw12:11:36

but first lunch and visitors

roelofw13:11:47

oke, I can use assoc but it looks I have to pull the part I want to update out of the big {}

roelofw13:11:26

so it looks it will be a 2 or more steps

roelofw13:11:49

1) pull the right {} out of the big seq

roelofw13:11:55

2) update it

roelofw13:11:11

3) make a new seq with the updated data

roelofw13:11:23

Is this a good plan ?

roelofw13:11:34

oke, I could use assoc-in if I knew if I know the index of the to updated map

roelofw13:11:17

and .indexOf to find the index . I can then do it in one step

akiroz14:11:20

@roelofw you can just call merge on the maps

roelofw14:11:48

@akiroz thanks for the tip

vandr0iy16:11:29

Has anyone ever done a benchmark of the clojure.core.match performance? How much is it better than, say, a cond?

manenko16:11:14

it depends on the data structure you use inside match, i.e. vector vs seq vs map vs list vs ...

alexmiller17:11:25

but of course, “it depends” :)

poooogles18:11:01

perfect timing, was about to test this myself...

gamecubate22:11:21

Hi all. When implementing a protocol with defrecord, how do I setup local bindings? I tried:

(defrecord Engine [canvas]
  (let [a 12]
    IEngine ;
      (init! [this] (log (str "initializing engine with a = " a)))
      (start [this] (log (str "starting engine with a = “ a)))
      (stop [this]  (log "stopping engine"))))
but, of course, an error.

ghadi22:11:53

@gamecubate you can't share a binding across multiple protocol methods

ghadi22:11:21

(the let needs to go in the init! / start / stop methods)