Fork me on GitHub
#clojure
<
2016-07-05
>
escherize04:07:31

Is there any info about ClojureWest 2017?

brao09:07:34

I have question about clojure.spec: Can the error messages reported by explain be customized ? I’m trying to use spec for validation

Macroz09:07:48

do you mean something like using explain-dataand then mapping the returned data structure to some of your own messages

brao09:07:52

@macroz: I don’t want to map the returned data structure to my messages. Instead I would like to define custom messages at predicate level itself

brao09:07:02

does it make sense ?

pesterhazy09:07:14

Is there a way to see if a future is failed? I can see it when I print it out: #future[{:status :failed, :val #error {

Macroz09:07:30

@brao what are you using the messages for?

pesterhazy09:07:32

but I couldn't find a failed? function

brao09:07:02

@macroz: I want to report it to the user, say in a HTTP response ?

Macroz09:07:30

@brao I find that always the app that actually shows the error message to the user must contain the mapping, the service can only return some kind of data that describes the error

Macroz09:07:02

therefore the actual (localized etc.) message and logic must reside in an app somewhere

Macroz09:07:14

I see spec's part as just reporting some kind of error

Macroz09:07:18

not generating the message

brao09:07:54

@macroz: that makes sense, the way you put it. Thinking..

Macroz09:07:50

also it depends a lot on what level of error reporting you want to do

Macroz09:07:58

maybe you just want to say that please check field X

Macroz09:07:12

it gets really difficult fast if you want more detail

Macroz09:07:39

but perhaps your use-case is straightforward

brao09:07:33

yes, pretty straightforward. I need to do things like - check if some keys exist in a map, they’re of the expected type and so on.

brao09:07:39

i have an example for you,

Macroz09:07:47

I have always made the validation -> error message mapping by hand, not trying to implement a clever system with "typing"

brao09:07:33

suppose I have a map in which a expected key is missing - explain gives me the predicate that failed - and it’s a partial function made out of contains

Macroz09:07:46

UX is often in conflict with typing and clean and simple code 🙂

Macroz09:07:51

I'm also interested if someone manages to do this differently

Macroz09:07:31

so far I have just checked for the existence of the keys without spec

Macroz09:07:48

but that may be unnecessary these days

Macroz09:07:02

sorry, haven't tried to do that yet myself so maybe someone else has an idea

brao09:07:37

yes, spec looks very promising. If only, at each spec level, I could also put some human readable message to report when validate failed, it would be perfect for what I have in mind

Alex Miller (Clojure team)09:07:31

@escherize: we haven't even started working on Clojure/west 2017 yet, so no. Also FYI there is #events

Alex Miller (Clojure team)09:07:23

@brao there is no facility in spec to customize errors other than taking the explain-data and producing what you want from it

Alex Miller (Clojure team)09:07:47

@pesterhazy: all the future related functions start with "future-" if you check the docs

brao09:07:42

@alexmiller: okay, but having it would be quite useful isn’t it ? Do you suggest any other way to do such a thing, other than interpreting explain-str ?

brao09:07:10

@alexmiller: is there a chance something like this will come in a future release ?

pesterhazy09:07:34

@alexmiller: there's future-done, but it looks like there's no way to retrieve the future's status (whether it failed with an exception) other than deref'ing it

Alex Miller (Clojure team)09:07:42

The output of explain-data is data - it's designed for this and sufficient

Alex Miller (Clojure team)10:07:10

Right now, I don't think anything more will be added

Alex Miller (Clojure team)10:07:09

@pesterhazy: is there some reason not to deref it? Don't want to block?

Alex Miller (Clojure team)10:07:44

The thing you get back from a future is a java.util.Future so check that api too

Macroz10:07:10

doesn't future throw an exception if it fails

Macroz10:07:26

so what sets the :status if you don't deref i.e. get() i.e. get the exception

Macroz10:07:41

didn't notice that code in core

pesterhazy10:07:03

@macroz: that's what I tried to figure out (what prints the #future string)

Macroz10:07:40

yes, java Future has no concept of failing like that

Macroz10:07:40

so if you want the status you need to deref it somewhere and catch the exception

Macroz10:07:06

but you may be waiting for some time depending how you deref

pesterhazy10:07:52

right, that's fine (that's what printing does as well)

pesterhazy10:07:03

so I'll do it similarly then:

(defn failed? [fu] (try
                          (deref fu)
                          false
                          (catch Throwable e
                            (log/error "Cached function previously failed:" e)
                            true)))

mjhamrick12:07:01

Is there a better way than

(read-string (str "'" &form)) 
to get the entire body of a macro call? I'm trying to put the entire body of the macro (as data) into the Metadata of the defn that it evaluates to.

mjhamrick12:07:04

Thank you. That works... Somehow.

bronsa14:07:22

@mjhamrick: that's just a fancy way of writing (list 'quote &form)

sandbags14:07:18

I’m having trouble thinking of the right way to approach a problem of transforming a nested tree structure. I’m wondering if maybe this is a time to reach for a zipper (never used them before so this might be shiny object syndrome) or missing some simple insight? My thinking so far involves a lot of finding indices and vector management suggesting I may be missing more Clojure way of thinking about this. I’ve written up the details https://gist.github.com/mmower/3cd21f55a80fa335b0fbd2defdf331a1 and would appreciate any thoughts.

ddellacosta15:07:33

@sandbags: zipper seems like a pretty good option. Also take a look at zip-visit, could simplify some of your logic if you do end up using a zipper: https://github.com/akhudek/zip-visit

ddellacosta15:07:22

haven't thought about your problem that hard but definitely seems like a vector zipper would fit the bill: https://clojure.github.io/clojure/clojure.zip-api.html#clojure.zip/vector-zip

sandbags15:07:54

@ddellacosta: thanks for the pointer, i wasn’t sure if i was reaching for an over-complicated tool

sandbags15:07:59

also zip-visit

ddellacosta15:07:38

on the contrary, think this is a way to simplify the problem

sandbags15:07:40

ah, zip-visit appears to answer my first question about using a zipper for this purpose (in that i have to manage some state)

acron15:07:26

I'm looking at :onyx/batch-timeout - is there a way to set this as infinite?

agile_geek15:07:48

@acron: might be worth asking this in #C051WKSP3 channel?

acron15:07:06

Oh crumbs, I thought I was facepalm

agile_geek15:07:51

@acron: np! See I'm still a help even after I've come back off busman's holiday!

kosecki16:07:41

hi, noob question, but how to put semicolon as character ? for instance to pass it as :separator in read-csv fn ?

bostonaholic16:07:17

user=> (csv/read-csv "foo;bar" :separator \;)
(["foo" "bar”])

lopalghost17:07:15

@kosecki: you're passing a sequence to read-csv--it should be either a string or a reader

kosecki17:07:49

@lopalghost: yep you are right, just noticed that when run in lein repl, proto-repl has some issues with semi-colon apparently

jasongilman18:07:14

@kosecki: can you try putting it in quotes? ";"

jasongilman18:07:46

I'll file an issue against Proto REPL for this.

spfeiffer18:07:34

Just FYI, i also run into problems with specific "exotic" character literals. You can resort to (char xx) anytime, with xx being the ASCII code. \; is (char 59)

shaunxcode18:07:24

does anyone recall seeing a project recently announced for persisting edn data to postgres?

kosecki19:07:08

@jasongilman: read-csv expects Character, error when passing the String

ezmiller19:07:52

what would be the best way to take a vector and then return a hash-map that maps the values of the vector to their index in the original vector?

aengelberg19:07:03

@ezmiller

(into {} (map-indexed vector v))

aengelberg19:07:25

oops, that would be the opposite (mapping indexes to their values)

ezmiller19:07:51

@aengelberg: that’s quite elegant looking. thanks! i figured clojure had a nice way to do this.

aengelberg19:07:10

@ezmiller actually it's more like this:

(into {} (map-indexed (fn [i value] [value i]) v))

ghadi19:07:46

or (into {} (map vector) v (range))

aengelberg19:07:52

or even better:

(zipmap v (range))

ghadi19:07:58

or that ^ 😃

aengelberg19:07:27

yay zipmap 🙂

ezmiller19:07:53

thanks much

novakboskov19:07:02

What is more elegant way to update vector item that satisfies a criteria? I know that I could use classic map for this but I'm searching for a method that don't naively iterate through all items but rather implement some smart search for a item using criteria.

aengelberg19:07:07

http://github.com/nathanmarz/specter may do what you want. what kind of criteria?

novakboskov19:07:54

@aengelberg:

(def data
  [{:id 1 :a 1 :b 2 :c 3} {:id 2 :a 11 :b 22 :c 33} {:id 3 :a 111 :b 222 :c 333}])
I wanna
UPDATE data SET :b="changed" WHERE :id=2

aengelberg19:07:42

(use 'com.rpl.specter.macros 'com.rpl.specter)
(setval [ALL (pred #(= (:id %) 2)) :b] "changed" data)

aengelberg19:07:59

pardon me, live-editing my code as I see new mistakes 😉

novakboskov19:07:11

Specter looks nice at the first sight... I think that I saw it somewhere... Thanks. 🙂

nathanmarz19:07:34

@aengelberg @novakboskov you actually don't need the pred, Specter recognizes anonymous functions and inserts it automatically. So you can write that as:

(setval [ALL #(= (:id %) 2) :b] "changed" data)

ezmiller19:07:14

let’s say I want to have a fn that imports data fro a csv that other fns will then work with (not mutate). what would be a sensible way to do this in clojure? can a fn load something into a global space of some sort?

novakboskov19:07:21

@nathanmarz: Thanks! Are there any examples that shows how to jack in it into clojurescript?

nathanmarz20:07:04

you just need to do refer-macros on the com.rpl.specter.macros namespace, otherwise it's the same

nathanmarz20:07:47

@novakboskov:

(require-macros '[com.rpl.specter.macros :refer [setval select transform]])
(require '[com.rpl.specter :as s])
(transform [s/ALL :a even?] inc [{:a 2 :b 1} {:a 3} {:a 4}])
;; => [{:a 3, :b 1} {:a 3} {:a 5}]

sandbags20:07:22

@ddellacosta: do you know the rationale for, for example, clojure.zip/right returning nil rather than :end when you go off the edge?

ddellacosta20:07:25

@sandbags: :end being some kind of token that you just used as an example of something it might return in that case? I don't see that in the docs otherwise

ddellacosta20:07:54

I would assume that is just so it matches the usual Clojure semantics for figuring if there is a value there or not

ddellacosta20:07:00

unless I don't understand your question

sandbags20:07:07

@ddellacosta: ah, i’m confusing the return value with the internal value in the zipper that end? uses

sandbags20:07:14

i guess my point is that I can’t depend on end?

sandbags20:07:31

since (-> z right end?) can give NPE

sandbags20:07:52

I suppose what I am really asking is why right et al. don’t return a valid zipper where (end? z) => true rather than nil

sandbags20:07:29

but i’m still trying to grok them, maybe there’s a perfectly reasonable reason for it

mj_langford20:07:26

Anyone happy with a library that makes it easy to post to insecure https?

mj_langford20:07:54

(this is a client app for a testing tool that has to target this as well)

ddellacosta21:07:37

@sandbags Okay, I had to give myself a little refresher in clojure.zip to be able to understand what you were asking

ddellacosta21:07:25

I think the thing here is that you need clojure.zip/next to traverse depth-first, per the docs, and the :end token (I see it now) which I guess is what end? is reading only occurs at the end of that

ddellacosta21:07:41

if you use right it's not going to behave the way you're expecting

sandbags21:07:54

@ddellacosta: yes, you have it .. i guess i didn’t see why the rules should be different for right/left from next

sandbags21:07:16

but it’s simple enough to switch nil? in for end?

sandbags21:07:17

i probbaly should read that paper, thanks for the link

shaun-mahood21:07:24

@mj_langford: Pretty sure I've used clj-http for that same purpose without any real problem. https://github.com/dakrone/clj-http#post Feel free to dm me with more specifics if you run into any problems.

hagmonk22:07:31

if I have a java method that expects an object implementing interface FooRunnable, is there any way I can "extend" clojure.lang.AFn to appear as if it implements this interface?

hagmonk22:07:21

rationale: that would allow me to pass (fn [] …) as the argument to that method, rather than a reified object implementing FooRunnable

seancorfield23:07:29

Loving how quickly all the Clojure 1.9.0 Alpha builds are coming out! We have Alpha 7 in production, Alpha 8 on QA, and Alpha 9 in DEV right now as we’re already leveraging some of the new predicates and just committing our first code built on clojure.spec!