This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-12-09
Channels
- # adventofcode (187)
- # aws (1)
- # aws-lambda (1)
- # beginners (162)
- # boot (64)
- # cljs-dev (6)
- # cljsjs (2)
- # cljsrn (32)
- # clojure (357)
- # clojure-greece (1)
- # clojure-korea (4)
- # clojure-russia (63)
- # clojure-sanfrancisco (3)
- # clojure-spec (91)
- # clojure-uk (63)
- # clojurescript (74)
- # clojurex (10)
- # code-reviews (55)
- # core-async (4)
- # core-typed (1)
- # cursive (17)
- # datascript (36)
- # datomic (43)
- # devcards (4)
- # dirac (3)
- # emacs (59)
- # hoplon (286)
- # jobs-discuss (399)
- # luminus (4)
- # mount (9)
- # off-topic (30)
- # onyx (53)
- # protorepl (3)
- # re-frame (88)
- # reagent (4)
- # spacemacs (1)
- # specter (14)
- # untangled (1)
- # vim (42)
I'm considering making a library for clojure that is used to generate spir-v code for targeting vulkan enabled gpus
like byte arrays?
what sort of functionality are you looking for? generic sugary erlang syntax?
i've done some byte fiddling in the past and i shot myself in the foot with big endian / little endian byte order and i just don't want to fool around with that part of it if i can avoid it
your output format is a file?
I can't imagine not needing to know about byte ordering
in any language
just because I imagine that's a fundamental aspect of the spec
it's like wanting to use json without having to worry about how the keys are spelled
I just googled "clojure erlang binary syntax sugar" and most of the results are about elixir
there is gloss
it would be a fun macro exercise to do something equivalent to erlang's syntax
here is what i'll be working on: https://www.khronos.org/registry/spir-v/specs/1.1/SPIRV.html#Binary
maybe one of the libraries already does that
Gloss is based on the errand stuff I think
i'm looking at https://github.com/clojurewerkz/buffy too
Hey all, looks like with the new predicates in clojure 1.9 core.match spits out
WARNING: boolean? already refers to: #'clojure.core/boolean? in namespace: clojure.tools.analyzer.utils, being replaced by: #'clojure.tools.analyzer.utils/boolean?
when included
Is there any way I can work around this? trying to make a command line application that returns json and this gets printed everytime preventing me from piping to jq
https://github.com/clojure/tools.analyzer/commit/6909fec3655ac194d9253191ffa9f1ecb76dc336
What does explain tell you?
user=> (s/explain (s/every-kv integer? string?) ["x"]) In: [\x] val: "x" fails predicate: vector? nil
Looks like it's expecting vector entries
I'm not honestly sure of the intention
based on the doc string, i expected it to treat the vector like an associative collection
are there plans to have vector namespace syntax similar to map namespace syntax - to better support datomic pull syntax, specifically attributes list in query?
Anyone know why ring is not setting cookies on response?
(-> (ok "Hello")
(assoc-in [:cookies "X-WEB-AUTH-USER"] {:value "Thing", :max-age 3600})
trace)
Produces:
{:status 200, :headers {}, :body "Hello", :cookies {"X-WEB-AUTH-USER" {:value "HALO", :max-age 3600}}}
which ns does ok
come from @johnnyillinois
(require '[ring.util.response :as res])
(-> (res/response "hello")
(assoc-in [:cookies "X-WEB-AUTH-USER"] {:value "Thing", :max-age 3600}))
>> {:status 200, :headers {}, :body "hello", :cookies {"X-WEB-AUTH-USER" {:value "Thing", :max-age 3600}}}
works fine for me
@johnnyillinois Make sure you use the ring.middleware.cookies/wrap-cookies
middleware.
You can also use ring.util.response/set-cookie
instead of assoc-in ...
.
Thanks!
@mak can't you just call (myfunction mylist)
?
my requiement is something like this, I want to take input from the user and pass each number to the list and confirm , if it is odd or even
once I take the all the 3 inputs from the user then only I want to pass that list to myfunction
@mak: what you mean is that you need to realize the full list before applying myfunction
to each element, correct ?
in this case, you can use doall
, e.g (map myfunction (doall (my-list)))
btw, if you're running myfunction
for side effects (e.g printing to the screen), it's preferable to use run!
instead of map
: (run! myfunction (doall (my-list)))
Wow! Thanks a lot @val_waeselynck
It's not printing it, its the return value
There has to be one.
It does not affect the execution of your program outside of the repl
Thanks again @val_waeselynck. It's a great help to me.
I was reading Rich's interview about spec and have found the following snippet: "But, yes, I'd like to see more namespaced keys. We're adding a little bit of syntax to Clojure to make that even easier, literally just easier so that people do more of it." -- what does he refer to?
@ambrosebs thank you
hey there - is there any way to use transducers without the reducing function? I have a composed set of filters, and I just want the filtered records; not reduced to a value...
Maybe use into?
@alexmiller Thanks! I just spotted that... Appreciate it
Can I test some functions that are depend on reponses of a external api and if so, can someone share some examples how I can make such a test
but if you have trouble creating a simple test for this, maybe don't dive in generators just yet
I hope I can find out how to mock the api server because I think test schould never use the real api
so I can validate a json response and test if the outcome of the parsing will be what I expect. ?
TIL about run!
"Today I Learned" @val_waeselynck mentioned it in a discussion about doall
etc. above
I have been using doall
and map
forever, and didn't know about the addition of run!
to clojure.core
haha, sorry
@jcromartie check out dorun
too
@roelofw I see a lot of questions up there 🙂
you want to mock an external service for tests?
what part are you stuck on?
I have read a lot about spec and other test frameworks but I never see a example of this
the simplest way is probably using with-redefs
you just redefine the function that calls the external service with a mock version
for the scope of your tests
keeping in mind that with-redefs
is not thread safe and updates the root binding of vars
but it's fine for tests
@roelofw you can also use something like http://www.mbtest.org/, it’s language agnostic and allows you to stand up mock http/https/smpt/tcp servers
oke, so I run the external api like this :
(client/get "" {:as :json :query-params {:key (env :key) :format "json" :type "schilderij
that get call should be inside a function
i.e. you've got a function somewhere that calls this API, yes?
so if it's (defn foo [] (client/get ...))
you showed me the detailed code that calls the API, so I don't assume you have it in a discrete function
here you see the whole function : https://github.com/rwobben/paintings/blob/master/src/clj/paintings2/api_get.clj
thanks
great
so in your tests you'd use (with-redefs [paintings2.api-get/read-numbers (constantly [1 2 3...])] your-test-code)
substituting whatever expected return value you want
yeah, just as an example
right
(constantly x) creates a function that just returns x
so you're substituting your functions that call the API with functions that just return test values
and if you need more state than that you can use an atom or something and make your redef functions more complicated
clojure.test
midge is nice
but if you don't know clojure.test then you should at least get familiar with it
if you ever wonder "which x should I use", and a core library is one of the options, learn the core library first
knowing clojure.test means you can contribute to most open source Clojure libs, and using it in your own lib means most devs can contribute to yours
This is a personal project. I do not thing many other clojure devs are joining my project
@jcromartie thanks for the help
sure thing
chips, something not right here :
(ns paintings2.test.get-api
(:require [clojure.test :refer :all]
[ring.mock.request :refer :all]
[paintings2.api-get :refer :all]))
(deftest test-app
(testing "get numbers"
(let [response (with-redefs [paintings2.api-get/read-numbers (constantly [{:id "1234"} {:id "abcd"} ])])
(is (= [ "1234" "abcd"] (map (get-in response [:body :artObjects] ) response)))])))
I see this error message : let requires an even number of forms in binding vector in paintings2.test.get-api:8
@roelofw that's just a 'syntax' error, you misplaced the closing bracket of your let
you're just looking at the object that your redefined function returned, rather than looking at the logic you have coded
there are a few issues here
let's slow down
I try to test if the code can take the id of a json response which a external api has returned and is parsed by the get
first: with-redefs
redefines a var only within the scope of with-redefs
itself, so since there's nothing in the body of with-redefs
your mock function is never called.
You need to put your test code that calls the redefined functions inside the body of with-redefs
second: it looks like you're expecting your function to return something much different from what you're returning using constantly
what is :body and :artObjects ?
forget about the API call for a moment and just focus on mocking your functions
so, it looks like your function read-numbers
returns values parsed from that response
not the response itself
so when you mock read-numbers
, you are just skipping the API call entirely and returning what you expect read-numbers
to return
right
where the response could look like this : [ { :id 123 :painter "me"} { :id 345 :painter "you}]
if you want to test your response parsing, then mock client/get and returns the response you expect
Is there a nil-safe implementation of Clojure maps? Like one that throws with (get {} :a)
the existing implementation is what I'd call "nil safe"
I can mock it up with : (with-redefs [paintings2.api-get/read-numbers (constantly [{:id "1234"}
I think
no problem
the most important question is: what are you trying to test?
so this part :
(defn read-numbers
"Reads the ids of the paintings"
[]
(->> (client/get "" {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True"}})
:body
:artObjects
(map :objectNumber)))
and remember, redefs is making a fake implementation of a function. so we cannot possibly fake the function we want to test
so you want to verify that the logic within read-numbers is correct?
@dpsutton that was my example, initially, because I thought he wanted to test code that used read-numbers
my mistake
@mpenet @jcromartie sorry probably messing up terminlogy. I want a thing that throws when theres no value for a given key
@martinklepsch just add a (when (= ::not-found ...) (throw ...)) and you get that
@martinklepsch no, clojure's associative data structure functions are rather forgiving
@martinklepsch alternatively contains?
@mpenet all this requires an extra fn/some wrapping. I want something that exposes the same interface as regular maps but throws in said case
@martinklepsch you'll just have to write it
@jcromartie haha thanks, the reason I asked is because maybe anyone knows of somebody else who already did 😄
@martinklepsch yup, or use something like potemkin and define your own map type (here be dragons)
oke, the call site is here :
(defn do-both-in-parallel [ids]
(let [paint-thread (future (pmap read-data-painting ids))
image-thread (future (pmap read-image-url ids))]
(map merge @paint-thread @image-thread)))
@mpenet ah neat, def-map-type seems handy didn't know potemkin has stuff like this
and that calls this one :
(defn read-data-painting
"Reads the title, description, date , collection, colors and url of a image"
[id]
(let [art-objects (-> (str " " id )
(client/get {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True"}})
: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}))
because this one is also using client/get which also can be refractor to get this out
I was afraid we might just be kicking the can down the road
it reads the numbers and do a client-get to get a new response to get more data of that painting
Is there a higher order function or idiomatic way to convert a sequence of maps to a map based on a key fn? Or is it just reduce
ex. (reduce #(assoc %1 (:id %2) %2) {} [{:id 1} {:id 2}]) ;;=> {1 {:id 1}, 2 {:id 2}}
if we pull the client/get out of read-numbers, where is the call site(s) that we would need to update
that is here :
ns paintings2.routes.home
(:require [paintings2.layout :as layout]
[compojure.core :refer [defroutes GET ]]
[ring.util.http-response :as response]
[ :as io]
[paintings2.api-get :as api]
[compojure.route :refer [resources]]))
(defn home-page []
(layout/render
"home.html" {:paintings (api/do-both-in-parallel(api/read-numbers)) }))
(defroutes home-routes
(GET "/" [] (home-page))
(resources "/") )
you just call your function with whatever data and make sure it spits out what you expect
@roelofw I can’t help but feel this really still belongs in #beginners ...
Thanks. Happy to help you in that channel.
@jrheard: what do you think about bifurcate
as a name for the function formerly known as winnow
?
@camdez I like that
@jcromartie Thanks for the feedback!
@jrheard: I’m trying to decide if it carries a hint of “two even parts”, but I don’t think it does. “Bisect” is also an option, but seems (to me) to have more of that “equal” implication.
"In geometry, bisection is the division of something into two equal or congruent parts.” (WP) Back to bifurcate
. 🙂
I’m trying to convert a Rubyist colleague to Clojure … I’ve tried the obvious points but I may need to admit defeat ... some people just want resources
Different strokes for different folks, but the best part about clojure v. ruby for me: in clojure, I can look at a fn call and know, by and large, exactly what is going to happen. In ruby, with all the mixins and patching monkeys? You really can’t know; you have to actually call it and see what happens (and hope all the effects are visible).
thanks @donaldball … I tried a similar tact … having built a concurrent program from ground up (not just use a web framework) before helps too
at my last gig, we built a distributed file system cache in Java ...
… so … yeah … been there done that
@tankthinks What do you mean by resources
?
@tankthinks have you looked at things like yada?
i had not looked into liberator or yada
I was simply using compojure and compojure-api
I think you can get a lot of the convenience of Rails’ resources with better HTTP compliance and all defined with data rather than a DSL.
but you’re probably right @camdez and @ddeaguiar … i think these are surface excuses for an underlying “don’t get the whole functional programming paradigm"
I just didn’ have quick answers to … yeah we’ve got an analog to your magic framework
Not sure how close some of the existing web dev libs come to rails but in my experience, explicitness is preferred over magic
that was my point exactly … i come from a deep java background … with too much spring magic
i don’t want magic … i want to know what the f is going on
fwiw, Luke Vanderhart is working on Arachne. It's going to be a framework for building apps with initial focus on web apps.
yup … i’ve read the #arachne blog p
hi - how do i feed the results of one pass of map to the next if mapping over a vector of vectors?
(map #(map (comp f1 f2) %) vec-of-vecs)
maybe?
that feels like it’s begging for reductions
if you want the intermediate steps and reduce
if you don’t.
@mnespor: now I get what we’re doing here. 🙂 http://adventofcode.com/2016/day/2
@oli: regarding your comment, there’s no overlap between the processing of each digit in the final code. Running your code, I get 1945, not 1985, which I believe is expected.
you're right. the (extremely long) inputs in the 'real' problem set just happen to eval to the right answer... 😛
which brings me back to how to i pass the result of processing one list in my list-of-lists to the next list to be processed...
@oli: I’ll say this, to avoid spoiling it: map
won’t allow you to retain any information from the previous digit, which is essential. You could use reduce
, and reach back into the accumulated list of digits every time (I just implemented this). But the cleanest solution is likely going to be to use loop
/ recur
.