This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-12-04
Channels
- # beginners (25)
- # boot (14)
- # business (1)
- # cljsrn (2)
- # clojure (180)
- # clojure-korea (14)
- # clojure-russia (8)
- # clojure-spec (9)
- # clojure-uk (17)
- # clojurescript (110)
- # cursive (14)
- # datomic (18)
- # emacs (23)
- # events (4)
- # garden (7)
- # hoplon (44)
- # lein-figwheel (1)
- # liberator (4)
- # london-clojurians (1)
- # om (10)
- # om-next (2)
- # onyx (22)
- # protorepl (60)
- # re-frame (36)
- # reagent (11)
- # ring (10)
- # yada (5)
Out of interest, has the return value from component/stop ever been useful for anyone?
@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?
The built in repl is useful, but use whatever you need
@weavejester not in my limited experience...but you would have more experience than me!
@agile_geek: I never have, but I was wondering if someone knew of some edge case where it was useful 🙂
@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?
@agile_geek: Maybe. I’ll give it some more thought 🙂
@weavejester I'll ask around and let you know any feedback
agile_geek: Oh, thanks a lot!
no problem
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 []@roelofw try to validate json here http://jsonlint.com/
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}]
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}))
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}))
hi guys
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
for example function list [(apply +) inc] and want to apply it to [1 2 3]
maybe function list [(apply +) inc inc dec]
is there a neat way clojure has or i should write it
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.
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?
I believe the only option is for the upstream library to be fixed.
@roelofw it should be (get-in [:name])
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.(->> x (sort-by reverse))
?
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 !@pesterhazy : Doesn’t seem to work.. Can you try to explain what’s its doing?
@oliv: multi arity fns are often handled this way in core for perf
(->> x (map reverse) sort (map reverse))
or (->> x (sort-by (comp vec reverse)))
sorts by value first, then by key
doesn't match your expected output though; do you want to search be descending value, ascending key?
if you know the values are going to be numbers:
(->> x (sort-by (fn [[k v]] [(- v) k])))
@alexmiller any reason or just convention ? not related with performance or any other concern , right ?
@pesterhazy: getting an ArityException
. And yes, i know that the values are numbers
edited. sorry, that's what I get from not trying it out 🙂
this is awesome! thanks so much @pesterhazy
@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)
no problem
@pesterhazy : so by returning a vector, sort-by automatically first sorts by first and then by second?
it always sorts based on a single value
but that value can itself be a vector
>
is defined for collections like vectors as well as for primitives
boot.user=> (compare [1] [2])
-1
so compare
is defined for vectors and strings and so forth; I guess >
is restricted to numbers
can someone please help me figure out why I suddenly do not see any output here : https://www.refheap.com/124093
I see @alexmiller , was just for study purpose. How the actual comp outpace my dumb implementation . Just to understand.
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...
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}))
(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)
@roelofw maybe gist the JSON, probably related to it's structure
gist the whole json as the external api is returning or gist what cli-http is returning after converting it to a clojure object ?
Either would be good
you can also just post a link with a working ID I guess
for example this : https://www.rijksmuseum.nl/api/nl/collection/SK-A-1718?key=14OGzuak&format=json&type=schilderij&toppieces=True
I think the problem arise from the fact that I use art-objects which is a clojure object for the second macro
So what's confusing you, that the name
thing is an object? (in Clojure lingo they are usually called maps btw)
@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.
(-> art-objects :principalMakers first :name)
with this you could access the name of the first item of the array/list.
later in your code you use (get-in name [:name])
which also returns the name "Hendrick Avercamp"
.
If you don't yet fully understand what ->
does consider using just get
and get-in
.
For instance you could rewrite (-> art-objects :principalMakers first :name)
as (get-in art-objects [:principalMakers 0 :name])
it do not work. I see this in my browser : :name #object[clojure.core$name 0x1410bd "clojure.core$name@1410bd"],
is your code still the same as above?
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}))
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
@martinklepsch why does there need to be a fn for dereffing many containers? Why not (map deref containers)
?
(map deref [(if thing container-1 container-2) (when-not other-thing container-3)])
(map deref xs ys)
?
@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.
Lists suck when you want to rely on something being in the same spot every time
in that case it may be better to make your deref
take a seq as an argument always
Is it clear what I mean? 😄
maybe not 🙂
;; 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.{:some-thing 'val :other-thing 'val}
If we would return a list we would need special nil-handling to make sure indices match up etc.
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.
(reduce (fn [m k] (assoc m k (react k))) {} [:foo :bar])
Yes, that's pretty much what react-many
does. Just wondering if there are better names 😄
so you want to package that ang give it a name?
Probably that was a large detour 😄
@pesterhazy yes. I was wondering if there might be any naming patterns in core or elsewhere for this kind of thing
yeah can't come up with a better name either
I think the convention is to not provide such fns and let people write their reduce
manually
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)
This feels a bit complex, even to explain now, I guess I'll sleep on it for a bit 🙂
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)))
I like this style:
(->> containers (map (juxt identity react)) (into {}))
though reduce is probably faster
yeah the zipmap felt a bit weird but ¯\(ツ)/¯
@martinklepsch so I can bette rename the name variable
To be honest every time I read juxt
somewhere I have to think hard to follow the code. So I don't use it 😄
@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 ""}]}
Even if I use this : painter (get-in art-objects [:principalMakers 0 :name]) ]
. I see a object instead of a string
@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?
@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}))
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}))
: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.
for trying you can replace it with this url : https://www.rijksmuseum.nl/api/nl/collection/SK-A-1718?key=14OGzuak&format=json&type=schilderij&toppieces=True
and take your time if you can figure it out. I can work on with this challenge I made for myself
Ok no time tonight but may take a look tomorrow if I have time at lunch
i want to use ->
(or ->>
) in a chain of functions that take the above thing in different places
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
@pvinis you might want to take a look at as->
flatten?
apply concat
@fellshard yesss
thanks to @pesterhazy too
@alexmiller Awesome, thank you!