Fork me on GitHub
#clojure
<
2016-12-04
>
seylerius00:12:10

Hmm... Girlfriend suggested RosterBot

seylerius00:12:15

Think I like it.

weavejester00:12:21

Out of interest, has the return value from component/stop ever been useful for anyone?

futuro01:12:50

@alexmiller I've been digging around the internet trying to understand the differences between the goals of the socket server (and a socket server repl, by extension) and nREPL. So far it seems like the point is simply to have a built in way for any clojure program to listen on a particular port without having to bundle in more dependencies, and that there are no opinions on what function to use to handle any messages passed into that socket server. Is that understanding correct?

Alex Miller (Clojure team)02:12:13

The built in repl is useful, but use whatever you need

agile_geek10:12:17

@weavejester not in my limited experience...but you would have more experience than me!

weavejester10:12:22

@agile_geek: I never have, but I was wondering if someone knew of some edge case where it was useful 🙂

agile_geek10:12:18

@weavejester I can't think of anything...but I feel if there is an edge case it will be in the development workflow rather than production?

weavejester10:12:02

@agile_geek: Maybe. I’ll give it some more thought 🙂

agile_geek10:12:04

@weavejester I'll ask around and let you know any feedback

weavejester10:12:46

agile_geek: Oh, thanks a lot!

roelofw13:12:50

chips, I have a big problem. When doing  ⁠⁠⁠⁠(client/get {:as :json})⁠⁠⁠⁠ with cli-http  this part  

"makers": [      {        "name": "Rembrandt Harmensz. van Rijn",        "unFixedName": "Rembrandt Harmensz. van Rijn",        "placeOfBirth": "Leiden",        "dateOfBirth": "1606-07-15",        "dateOfBirthPrecision": null,        "dateOfDeath": "1669-10-08",        "dateOfDeathPrecision": null,        "placeOfDeath": "Amsterdam",        "occupation": [          "prentmaker",          "tekenaar",          "schilder"        ],  
is converted to []

roelofw13:12:58

So I loose data

roelofw13:12:11

can I better use chestire for this ?

roelofw13:12:42

This is a nested json file

roelofw13:12:13

oke, I needed another keyword

roelofw13:12:37

now find out how I get the name keyword out of this :

[{:placeOfDeath"Kampen", :occupation ["tekenaar"], :name "Hendrick Avercamp", :nationality nil, :roles ["schilder"], :placeOfBirth "Amsterdam", :unFixedName "Avercamp, Hendrick", :dateOfDeathPrecision nil, :dateOfBirth "1585", :dateOfDeath "1634", :dateOfBirthPrecision nil, :productionPlaces [], :qualification nil, :biography nil}]  

roelofw13:12:03

I tried (map :name) but then I see errors

roelofw13:12:02

Does not work on this code :

(defn read-data-painting
  "Reads the title, description, date , collection, colors and url of a image"
  [id]
  (let [art-objects (-> (str "" id "?key=14OGzuak&format=json&type=schilderij&toppieces=True")
                        (client/get {:as :json})
                        :body
                        :artObject)
        name        (-> art-objects
                        :principalMakers
                        (get-in :name))

        description (:description art-objects)
        date (get-in art-objects [:dating :year])
        collectie (first (:objectCollection art-objects))
        colors (:colors art-objects)

        ]
    {:id id :name  name :description description :date date :collectie collectie :colors colors}))  

m3nthal13:12:56

which part yields an error?

roelofw13:12:34

the get-in part

roelofw14:12:11

this part : (get-in :name))

roelofw14:12:09

solved it by doing this :

(defn read-data-painting
  "Reads the title, description, date , collection, colors and url of a image"
  [id]
  (let [art-objects (-> (str "" id "?key=14OGzuak&format=json&type=schilderij&toppieces=True")
                        (client/get {:as :json})
                        :body
                        :artObject)
        name        (-> art-objects
                        :principalMakers
                        first
                        :name
                       )

        description (:description art-objects)
        date (get-in art-objects [:dating :year])
        collectie (first (:objectCollection art-objects))
        colors (:colors art-objects)
        ]
    {:id id :name  name :description description :date date :collectie collectie :colors colors})) 

roelofw14:12:41

but now I see only the names and not the whole { :id ......... }

roelofw14:12:59

anyone who has a idea why and how to solve it

cjmurphy14:12:59

@roelofw - you can debug -> thread macro by instead using as->.

abdullahibra14:12:12

how can apply list of functions with unknown length in order which output of function 1 is input of function 2 and output of function 2 is input of function 3, i can use -> or ->> notations but the problem i don't know the length of functions list

abdullahibra14:12:34

for example function list [(apply +) inc] and want to apply it to [1 2 3]

abdullahibra14:12:55

maybe function list [(apply +) inc inc dec]

abdullahibra14:12:28

is there a neat way clojure has or i should write it

roelofw14:12:05

@cjmurphy so I have to change the -> to as->

cjmurphy14:12:25

You can, then where you want to see what the current value is use a function that returns its argument (i.e. identity function) but prints the value as a side effect.

cjmurphy14:12:20

(defn probe ([x] (println x) x))

roelofw14:12:09

Oke, I will try it . the problem started when I add the name (-> part

cjmurphy14:12:54

as-> is easier as can do the job of either -> or ->>.

roelofw14:12:44

Thanks, as I said I will look into it

pkova14:12:56

is there any way for me to avoid WARNING: boolean? already refers to: #'clojure.core/boolean? in namespace: clojure.core.typed.contract-utils, being replaced by: #'clojure.core.typed.contract-utils/boolean? as a library consumer?

pkova14:12:30

(:refer-clojure :exclude [boolean?]) in my ns form does not work

pkova14:12:36

im not using core.typed in my ns, but an upstream library is

weavejester15:12:32

I believe the only option is for the upstream library to be fixed.

mitchelkuijpers15:12:35

@roelofw it should be (get-in [:name])

kpsf16:12:38

XMLStreamException ParseError at [row,col]:[1,7957]
Message: XML document structures must start and end within the same entity.  com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next (XMLStreamReaderImpl.java:601)
that is the error, it only occurs when I close the streams in finally.

prakhar16:12:36

Hi, I had a question on sorting based on multiple keys

pesterhazy16:12:04

(->> x (sort-by reverse))?

prakhar16:12:54

what’s ms?

tdantas16:12:55

when I’m studying I always try to mimic the functions I’m using. I’ve implemented the comp

(defn my-comp [& fns]
  (reduce #(fn [& args] (%1 (apply %2 args))) fns ))
but when I saw the real comp it has a little differences
(defn comp
  "Takes a set of functions and returns a fn that is the composition
  of those fns.  The returned fn takes a variable number of args,
  applies the rightmost of fns to the args, the next
  fn (right-to-left) to the result, etc."
  {:added "1.0"
   :static true}
  ([] identity)
  ([f] f)
  ([f g] 
     (fn 
       ([] (f (g)))
       ([x] (f (g x)))
       ([x y] (f (g x y)))
       ([x y z] (f (g x y z)))
       ([x y z & args] (f (apply g x y z args)))))
  ([f g & fs]
     (reduce1 comp (list* f g fs))))
why not use only one reduce to everything ? ( like my my-comp is doing ) some performance concerns ? what is the point !

prakhar16:12:29

@pesterhazy : Doesn’t seem to work.. Can you try to explain what’s its doing?

prakhar16:12:12

the best I could get was

Alex Miller (Clojure team)16:12:46

@oliv: multi arity fns are often handled this way in core for perf

pesterhazy16:12:19

(->> x (map reverse) sort (map reverse))

pesterhazy16:12:36

or (->> x (sort-by (comp vec reverse)))

pesterhazy16:12:13

sorts by value first, then by key

pesterhazy16:12:02

doesn't match your expected output though; do you want to search be descending value, ascending key?

prakhar16:12:16

yes!.. sorry for not clearing that up

pesterhazy16:12:44

if you know the values are going to be numbers:

prakhar16:12:46

basically, providing two diff comparators for both key-fns

pesterhazy16:12:13

(->> x (sort-by (fn [[k v]] [(- v) k])))

tdantas16:12:52

@alexmiller any reason or just convention ? not related with performance or any other concern , right ?

tdantas16:12:06

because, one line is much simpler than the actual comp

tdantas16:12:22

( maybe i’m too noob yet )

prakhar16:12:52

@pesterhazy: getting an ArityException. And yes, i know that the values are numbers

pesterhazy16:12:07

edited. sorry, that's what I get from not trying it out 🙂

prakhar16:12:12

this is awesome! thanks so much @pesterhazy

Alex Miller (Clojure team)16:12:04

@oliv purely for performance. Many of the impls in core sacrifice readability for performance. The differences are small but when they end up in hot loops, it can matter. I wouldn't recommend writing your own code that way (until it's a proven hot spot)

prakhar16:12:02

@pesterhazy : so by returning a vector, sort-by automatically first sorts by first and then by second?

pesterhazy16:12:11

it always sorts based on a single value

pesterhazy16:12:23

but that value can itself be a vector

pesterhazy16:12:51

> is defined for collections like vectors as well as for primitives

prakhar16:12:39

Oh! but (> [1 2 3 4] [1 2 3 6]) doesnt’ work

pesterhazy16:12:37

boot.user=> (compare [1] [2])
-1

pesterhazy16:12:27

so compare is defined for vectors and strings and so forth; I guess > is restricted to numbers

roelofw16:12:42

can someone please help me figure out why I suddenly do not see any output here : https://www.refheap.com/124093

roelofw16:12:01

IM fighting for 2 days to make this work

tdantas16:12:32

I see @alexmiller , was just for study purpose. How the actual comp outpace my dumb implementation . Just to understand.

martinklepsch16:12:29

I have a function similar to deref but called react (reasons not important) and now want to implement a version of it that can, to explain it in deref terms, dereference multiple references. What would be a good name for that function? react-many, react-multi etc come to mind but not too excited about either...

roelofw16:12:25

Why is here name a object instead of a string :

(defn read-data-painting
  "Reads the title, description, date , collection, colors and url of a image"
  [id]
  (let [art-objects (-> (str "" id "?key=14OGzuak&format=json&type=schilderij&toppieces=True")
                        (client/get {:as :json})
                        :body
                        :artObject)
        name        (-> art-objects
                        :principalMakers
                        first )
        description (:description art-objects)
        date (get-in art-objects [:dating :year])
        collectie (first (:objectCollection art-objects))
        colors (:colors art-objects)
        painter (get-in name [:name])]
    {:id id :name painter :description description :date date :collectie collectie :colors colors})) 

martinklepsch16:12:31

(The react function returns a single thing whereas the "multiple-version" returns a map like {id dereferenced-value}. Because of that just making react variadic and not having another function seems confusing and I'd like to avoid it)

martinklepsch17:12:51

@roelofw maybe gist the JSON, probably related to it's structure

roelofw17:12:25

gist the whole json as the external api is returning or gist what cli-http is returning after converting it to a clojure object ?

martinklepsch17:12:59

Either would be good

martinklepsch17:12:36

you can also just post a link with a working ID I guess

roelofw17:12:06

I think the problem arise from the fact that I use art-objects which is a clojure object for the second macro

roelofw17:12:53

When I only do name (-> art-objects ) I get a object instead

martinklepsch17:12:30

So what's confusing you, that the name thing is an object? (in Clojure lingo they are usually called maps btw)

roelofw17:12:01

yes, I was expecting to see a string like "Joe Doe"

roelofw17:12:36

for the rest description and so on , it worked well

martinklepsch17:12:15

@roelofw Ok. So if you look at the JSON you can see that principalMakers is an array containing a map (or multiple I guess). These maps have a key :name which you want to access... What you're doing instead with this (-> art-objects :principalMakers first) is that you get the first item of the array/list.

martinklepsch17:12:56

(-> art-objects :principalMakers first :name) with this you could access the name of the first item of the array/list.

roelofw17:12:05

yes. I hope to get the { .....} part

martinklepsch17:12:26

later in your code you use (get-in name [:name]) which also returns the name "Hendrick Avercamp".

martinklepsch17:12:53

If you don't yet fully understand what -> does consider using just get and get-in.

martinklepsch17:12:27

For instance you could rewrite (-> art-objects :principalMakers first :name) as (get-in art-objects [:principalMakers 0 :name])

roelofw17:12:31

it do not work. I see this in my browser : :name #object[clojure.core$name 0x1410bd "clojure.core$name@1410bd"],

martinklepsch17:12:32

is your code still the same as above?

roelofw17:12:05

my code is now :

defn read-data-painting
  "Reads the title, description, date , collection, colors and url of a image"
  [id]
  (let [art-objects (-> (str "" id "?key=14OGzuak&format=json&type=schilderij&toppieces=True")
                        (client/get {:as :json})
                        :body
                        :artObject)
        name        (-> art-objects
                        :principalMakers
                        first
                        :name)
        description (:description art-objects)
        date (get-in art-objects [:dating :year])
        collectie (first (:objectCollection art-objects))
        colors (:colors art-objects)
        painter  (get-in name [:name])]

    {:id id :name painter :description description :date date :collectie collectie :colors colors}))  

roelofw17:12:28

as you said or I have misunderstood you elsewhere

martinklepsch17:12:56

Hm, I just tried your code and I don't see what you see. What you're describing would happen if you return the map {:name name} and don't have a local binding for name. In this case the Clojure function name is used as the value and will get printed as a string like you described. @roelofw

pesterhazy17:12:54

@martinklepsch why does there need to be a fn for dereffing many containers? Why not (map deref containers)?

martinklepsch17:12:26

@pesterhazy

(map deref [(if thing container-1 container-2) (when-not other-thing container-3)])

pesterhazy17:12:14

(map deref xs ys)?

martinklepsch17:12:43

@pesterhazy the list of containers might be dynamic and in this  case you would need to track indices in the same dynamic way. For this reason we want a different kind of return value that makes it easier to access the value of a specific container.

martinklepsch17:12:33

Lists suck when you want to rely on something being in the same spot every time

pesterhazy17:12:07

in that case it may be better to make your deref take a seq as an argument always

martinklepsch17:12:23

Is it clear what I mean? 😄

pesterhazy17:12:31

maybe not 🙂

martinklepsch17:12:31

;; react ~= deref
(react :some-thing) ;=> returns the value of a container known as :some-thing
(react-many :some-thing :other-thing) ;=> needs to return values of both containers
To have a predictable return value for the react-many variant the only sensible way seems to be a map.

martinklepsch17:12:52

{:some-thing 'val :other-thing 'val}

martinklepsch17:12:42

If we would return a list we would need special nil-handling to make sure indices match up etc.

martinklepsch17:12:43

Since we have a different return type for the react and react-many thing either way a map seems to be the best, most predictable, explicit option.

pesterhazy17:12:58

(reduce (fn [m k] (assoc m k (react k))) {} [:foo :bar])

martinklepsch17:12:30

Yes, that's pretty much what react-many does. Just wondering if there are better names 😄

pesterhazy17:12:34

so you want to package that ang give it a name?

martinklepsch17:12:11

Probably that was a large detour 😄

martinklepsch17:12:56

@pesterhazy yes. I was wondering if there might be any naming patterns in core or elsewhere for this kind of thing

pesterhazy17:12:59

yeah can't come up with a better name either

pesterhazy17:12:22

I think the convention is to not provide such fns and let people write their reduce manually

martinklepsch17:12:47

Yeah, maybe. Maybe I should have mentioned that when you call react-many without any args it will "react" all known containers... (another twist in the story lol)

martinklepsch17:12:21

This feels a bit complex, even to explain now, I guess I'll sleep on it for a bit 🙂

martinklepsch17:12:16

here's the impl of react-many to illustrate the no-args case:

(let [ks (or (seq ks) (-> state ::containers keys))]
  (zipmap ks (map #(react state %) ks)))

pesterhazy17:12:17

I like this style:

(->> containers (map (juxt identity react)) (into {}))

pesterhazy17:12:34

though reduce is probably faster

martinklepsch17:12:50

yeah the zipmap felt a bit weird but ¯\(ツ)

roelofw17:12:13

@martinklepsch so I can bette rename the name variable

martinklepsch17:12:59

To be honest every time I read juxt somewhere I have to think hard to follow the code. So I don't use it 😄

roelofw17:12:06

hmm. renaming everything does also not work

roelofw17:12:24

Still a object instead of a string 😞

roelofw17:12:50

@martinklepsch what did you see as output of my function then. I see this :

{:id"SK-A-1718", :name #object[clojure.core$name 0x3a1000 "clojure.core$name@3a1000"], :description "Winterlandschap met ijsvermaak. Dorpsgezicht met vele figuren op het ijs: schaatsers, kolfspelers, wandelaars. Rechts een arrenslede, links een kerk.", :date 1608, :collectie "schilderijen", :colors ["#B0AD92" " #64614A" " #D7D3BA" " #76806F" " #373B30" " #1F1C15" " #9E824E"], :tiles [{:x 0, :y 0, :url ""}]} 
 

roelofw18:12:36

it looks to me that -> art-objects makes a object instead of a seq of maps

roelofw18:12:45

So I cannot use another macro as argument of a macro ?

roelofw18:12:13

Even if I use this : painter (get-in art-objects [:principalMakers 0 :name]) ] . I see a object instead of a string

roelofw18:12:48

oke, renaming :name to :painter in the output did the trick

roelofw18:12:02

so :name cannot be used it seems

agile_geek19:12:56

@roelofw name is a clojure fn but if you are defining it in a let as a Var and using inside that let it should shadow the fn and still work. Maybe double check that the parens are in the right place to enclose the map in the let?

roelofw19:12:08

I have checked it several times

roelofw19:12:16

@agile_geek When I do :

(defn read-data-painting
  "Reads the title, description, date , collection, colors and url of a image"
  [id]
  (let [art-objects (-> (str "" id "?key=14OGzuak&format=json&type=schilderij&toppieces=True")
                        (client/get {:as :json})
                        :body
                        :artObject)
        description (:description art-objects)
        date (get-in art-objects [:dating :year])
        collectie (first (:objectCollection art-objects))
        colors (:colors art-objects)
        painter  (get-in art-objects [:principalMakers 0 :name]) ]
    {:id id :painter painter :description description :date date :collectie collectie :colors colors}))  

roelofw19:12:33

When I do :

(defn read-data-painting
  "Reads the title, description, date , collection, colors and url of a image"
  [id]
  (let [art-objects (-> (str "" id "?key=14OGzuak&format=json&type=schilderij&toppieces=True")
                        (client/get {:as :json})
                        :body
                        :artObject)
        description (:description art-objects)
        date (get-in art-objects [:dating :year])
        collectie (first (:objectCollection art-objects))
        colors (:colors art-objects)
        painter  (get-in art-objects [:principalMakers 0 :name]) ]
    {:id id :name painter :description description :date date :collectie collectie :colors colors})) 

roelofw19:12:07

I see the painter after :name as a object

agile_geek19:12:05

:name is just a keyword and has no special significance ...something else more subtle must be going on. I can't try out you code as I get 403 from the server with that key but if you grap the result of one call to client/get for one specific collection and put it in a def then share it I can try it out but might not be time tonight.

roelofw19:12:05

this one has a working key

roelofw19:12:30

and take your time if you can figure it out. I can work on with this challenge I made for myself

agile_geek19:12:56

Ok no time tonight but may take a look tomorrow if I have time at lunch

roelofw20:12:22

no problem. Take time

pvinis20:12:49

i want to use -> (or ->>) in a chain of functions that take the above thing in different places

pvinis20:12:33

so for example i have a string. then i want to split it, and split takes the string as first arg, then i want to map read-string to the list, and that list is the last argument

pvinis20:12:47

so split requires -> and map requires ->>

pvinis20:12:53

how can i manage that?

weavejester20:12:57

@pvinis you might want to take a look at as->

pvinis20:12:50

oh that looks promising

pvinis20:12:03

thanks. everyday, clojure has something new to teach me 😄

pvinis21:12:35

is there something opposite to partition?

pvinis21:12:49

i did (partition 3 bla)

pvinis21:12:58

can i reverse that?

fellshard21:12:54

I can't spell... hah.

pesterhazy21:12:40

apply concat

fellshard21:12:40

That'll ensure only the partitioned layer is flattened.

pvinis21:12:21

flatten was messing everything else up. i wanted only the partitioned layer

fellshard21:12:27

pesterhazy's is probably gonna be more suitable

fellshard21:12:50

takes the concat, leaves the unnecessary map

pvinis22:12:04

ah sorry yea

pvinis22:12:11

thats the one i used 😄

futuro23:12:45

@alexmiller Awesome, thank you!