Fork me on GitHub
#clojure
<
2016-11-22
>
gfredericks01:11:05

I am doing some casual stuff with prismatic/graph and am writing lots of functions like (fnk [a b c] (+ a b c)) and I appreciate not having anaphoric macros but the repetition there is a lot

gfredericks01:11:39

it makes me wonder if there's an intermediate option where you declare a symbol prefix; e.g., (crazy-fnk $ (+ $/a $/b $/c))

gfredericks01:11:01

I guess you can't write that macro cleanly without a good code-walker, but still

gfredericks01:11:53

maybe the real answer is a macro for the whole graph

hwk03:11:02

(fn [a _] (:text a)) <-- is there a way to write this as an anonymous function #(:text %1) <-- how do I tell it to expect a %2 but its not used?

Alex Miller (Clojure team)03:11:49

it creates the arity based on the % params you use

Alex Miller (Clojure team)03:11:08

so you’d have to use it uselessly like #(do %2 (:text %1))

Alex Miller (Clojure team)03:11:41

why not just use the fn form?

Alex Miller (Clojure team)03:11:45

you could do #(:text (first %&))

Alex Miller (Clojure team)03:11:09

that’s as clever as I’ve got :) but I wouldn’t use it for anything other than code golf

hwk03:11:56

@alexmiller: it's somethign that looks like (re-frame/reg-sub :equation (fn [db _] (:equation db))) I had thought making it using #% might be clearer, %& is very cute, but I guess I'll just use fn for now.

misha04:11:58

@hwk do you have a perl background by any chance? ; )

underplank05:11:03

Nothing to do with clojure. But chrome is giving me a certificate error for http://amazon.com? is anybody else getting that?

underplank05:11:57

hmm.. restarted chrome. dont have the issue anymore. Well then..

hwk05:11:57

@misha: no perl, coming from haskell

dotspy08:11:05

hi all, where can i see best practices for clojure project structure, namespaces and etc?

seancorfield08:11:15

@dotspy There really isn't any definitive "best practices" for that. It depends very much on your specific project.

seancorfield08:11:01

About the only guidance most people seem to agree on is "keep it simple" 🙂

hans08:11:18

dotspy: I think it is very common to use "lein new", maybe using the app template, to set up a basic skeleton. There are more involved leiningen skeleton templates available, but I've never used them.

pupeno08:11:45

I’m building several related ClojureScript apps and I need to share some code amongst them. What’s the best method? private maven repository? git submodules?

dotspy08:11:51

@hans Yeah i used lein to create new project. But now im stuck, should i use smth like java best practise for folder naming in my CRUD + REST basic app, or smth else.

hans08:11:56

dotspy: you could google for leiningen webapp templates

pupeno08:11:54

dotspy: for a very complete template of a web app, you can check Luminus.

asolovyov10:11:56

anybody knowing how riemann works here? I wrote a simple http server - https://paste.in.ua/2125/ - but if I start it as (http-server {:port 1234 :app some-fn}), nothing except for http-server itself is executed. Any ideas what I should do to make it work?

nikki11:11:47

It's just an ssh (actually mosh) into a mac so not too crazy :)

pradeepcheers11:11:04

@nikki thats cool. Is there an app?

pradeepcheers11:11:31

how did you manage to open the clojure repl?

wotbrew12:11:14

I want to serialize clojure data structures, such that 2 values that are .equals will emit the same bytes. Is there a library that already does this? I am interested in serializing the standard edn set of types. The reason for this is that I am interested in content-addressing clojure data-structures.

wotbrew12:11:10

It seems like data.fressian for example returns different bytes for 2 equivalent array maps (due to impl ordering)

chrisblom12:11:02

@danstone is converting all maps to sorted-maps an option?

chrisblom12:11:00

that would eliminate the differences due to ordering, and is easy to do (using clojure.walk)

chrisblom12:11:35

a downside is that it places some constraints on key types

wotbrew12:11:36

clojure.walk, to encode sorted versions of unsorted structures is my fallback if I can't get anywhere. The obvious problem is efficiency, you have to walk and reallocate the entire structure before you can start writing bytes.

chrisblom12:11:19

yeah, it's not very elegant in that respect

chrisblom12:11:59

i'm not familiar with fressian, but would it be possible to emit bytes for a map in the order of the keys?

chrisblom12:11:38

the order as defined by .compare

wotbrew12:11:49

It may be possible to override write handlers for particular types, I will look into it as well.

Alex Miller (Clojure team)13:11:01

I don't know of anything doing this, but it's a cool idea

wotbrew13:11:16

Ok, I'll look at doing it by extending fressian, I'll release something to the world if all goes well

wotbrew13:11:32

Turns out there is some subtlety in doing this. e.g bigints/longs, vectors/seq/list equivalence. Maybe I shouldn't be using .equals or = as the basis for output equality, rather best effort but limited to both type and equality. The alternative is to always return bigint for integer ambiguity, and vectors for sequence ambiguity. This seems like it may lead to bugs in particular regarding lists.

wotbrew13:11:42

Hopefully its still a good idea 😛

yonatanel13:11:23

Is there a kind of some-> threading macro or something similar that returns the first result to satisfy a given predicate?

patrkris14:11:39

(first (filter pred coll))?

mpenet14:11:09

or just some

yonatanel14:11:28

@patrkris @mpenet It needs to be a pipeline like thing, testing the result of the previous form against the predicate, and using it as first parameter of the next.

yonatanel14:11:58

Not sure I need it anymore, but here is my attempt (adjusted some->):

(defmacro pred->
  "When pred against x is truthy, threads x into the first form (via ->),
  and when pred against that result is truthy, through the next etc."
  [pred x & forms]
  (let [g (gensym)
        steps (map (fn [step] `(if (-> ~g ~pred) ~g (-> ~g ~step)))
                   forms)]
    `(let [~g ~x
           [email protected](interleave (repeat g) (butlast steps))]
       ~(if (empty? steps)
          g
          (last steps)))))
=> #'dev/pred->
(pred-> even? 1 inc inc)
=> 2

emileswarts14:11:31

Hi, I'm just trying out Clojure and was wondering what the best way is to check if a vector contains a map. Is there anything built in? Or do I dissect the data and compare against the key/value? [{:foo "bar"}] contains? {:foo "bar"}

yonatanel14:11:24

@emileswarts Do you mean any of these?

(some #{{:foo "bar"}} [{:foo "bar"}])
=> {:foo "bar"}
(some map? [{:foo "bar"}])
=> true

emileswarts14:11:04

That’s it! Thank you

joseph16:11:41

#compojure-api any have experience of using compojure-api to download file with byte-array format instead of default json?

juhoteperi16:11:34

@joseph What do yo mean with byte-array? You'll need a serialization format

joseph16:11:21

I mean I have a file in the Byte array format, and want to return it as body directly, but I found it's default converted to json format

juhoteperi16:11:49

Do you have :return key in the handler? That will enable serialization always

joseph16:11:32

:return key in the handler? you mean in the response?

juhoteperi16:11:07

(ok (io/file ...)) (ok (io/resource ...)) and (ok (byte-array ...)) should all work (though I guess you should also assoc content-type to the response)

juhoteperi16:11:26

@joseph I mean the Compojure-api handler metadata

theeternalpulse16:11:42

is the cider room private?

joseph16:11:26

sorry, I still do not understand what you mean :return key, but I print out the response, it's like this

{:status 200, :headers {\"Content-Disposition\" \"attachment; filename=\\\"dataset.dta\\\"\", \"Content-Type\" \"application/json; charset=utf-8\", \"Content-Length\" \"189362\", \"Access-Control-Allow-Origin\" \"*\", \"Access-Control-Allow-Methods\" \"POST\", \"Access-Control-Allow-Headers\" \"X-Requested-With,Content-Type,Cache-Control\"}, :body #object[java.io.BufferedInputStream 0xefcbd55 \"[email protected]\"], :compojure.api.meta/serializable? true}

joseph16:11:14

it contains the key :compojure.api.meta/serializable?, I guess it's serialized...

juhoteperi16:11:20

@joseph The problem is :compojure.api.meta/serializable? true and that is caused by your handler code

joseph16:11:05

@juhoteperi any idea to change it? as it as false?

juhoteperi16:11:27

Please paste the whole handler code somewhere

joseph16:11:47

@juhoteperi

(defmethod handle-request :download-dataset
  [request header]
  (let [dataset-type (-> request :input :type)
        download-command (assoc (dissoc (:input request) :type)
                                :command "download")
        csv-file (get-remote-file "/download-data" download-command header)
        metadata-file (when (or (= dataset-type "STATA")
                                (= dataset-type "SPSS"))
                        (get-remote-file "/download-documentation"
                                         (dissoc download-command :case-subsets)
                                         header))
        x-request-id (get header "X-REQUEST-ID")
        data (case dataset-type
               "CSV"   (.getBytes csv-file)
               "STATA" (convert-csv-file! csv-file metadata-file "dta" x-request-id)
               "SPSS"  (convert-csv-file! csv-file metadata-file "sav" x-request-id))
        suffix (case dataset-type
                 "CSV" "csv\""
                 "STATA" "dta\""
                 "SPSS" "sva\"")]
    (log/with-logging-context (merge header
                                     request
                                     {:success-code 200
                                      :version (helpers/project-version)})
                              (log/info "Succeed downloading dataset with format " dataset-type))
    (let [f (FileOutputStream. "dataset.dta")]
      (.write f data)
      (.close f))
    (assoc (ok (byte-array data))
         :headers
         {"Content-Disposition" (str "attachment; filename=\"dataset." suffix)})))

juhoteperi16:11:33

What about the compojure handler? (GET ...) part

joseph16:11:59

(context "/download" []
               :middleware [extract-request-header!]
              (POST "/" []
                    :summary   "The interfact to communicate with client."
                    :responses  {200 {:schema resp-v/transit-schema
                                      :description "Successful operation!"}
                                 400 {:schema error-v/bad-request-error-400-schema
                                      :description "Bad request!"}
                                 401 {:schema error-v/unauthorized-error-401-schema
                                      :description "Unauthorized!"}
                                 404 {:schema error-v/not-found-error-404-schema
                                      :description "Not found!"}
                                 500 {:schema error-v/system-error-500-schema
                                      :description "System error!"}}
                    :form-params [request :- s/Str]
                    (handler/wrap-exception! (read-string request) (:header @app-state) handler/handle-request)))

juhoteperi16:11:12

Okay, :responses causes the serialization

joseph16:11:19

here, in the bottom line, it calls the handle-request to handler the data

juhoteperi16:11:43

If you have set the response Schema using :return or :responses; the response is always serialized to JSON or such

joseph16:11:16

ah...that's the problem, ok, so, commenting it will work? trying it

joseph16:11:29

@juhoteperi hmm, it comes 500 error...

joseph16:11:05

and it prints out the url /download/...

joseph16:11:18

@juhoteperi I also added two middleware after the api, one is used to allow cross origin and another is to set the content type of response which contains byte-array file to application/octet-stream. I print the response in the middleware, the setting content-type one seems triggered twice

joseph16:11:40

here is the cosole:

joseph16:11:47

"resp: {:status 200, :headers {\"Content-Disposition\" \"attachment; filename=\\\"dataset.dta\\\"\", \"Access-Control-Allow-Origin\" \"*\", \"Access-Control-Allow-Methods\" \"POST\", \"Access-Control-Allow-Headers\" \"X-Requested-With,Content-Type,Cache-Control\", \"Content-Type\" \"application/octet-stream\"}, :body #object[\"[B\" 0x58198f69 \"[[email protected]\"]}"
"/download/"
"resp: {:headers {\"Access-Control-Allow-Origin\" \"*\", \"Access-Control-Allow-Methods\" \"POST\", \"Access-Control-Allow-Headers\" \"X-Requested-With,Content-Type,Cache-Control\"}}"

ccann17:11:24

can anyone recommend a lib (or libs) that can read .obj and .mtl files and render/export them as .svg or straight to raster?

jmp18:11:44

Anyone with experience with Machine Learning with Clojure?

nikki20:11:32

@pradeepcheers: its actually just an ssh into somewhere and im running emacs and cider and everything there haha

nikki20:11:43

the ipad itself is just a fancy terminal

raynes22:11:12

Refheap is back up and running, I'm not sure why it went down and didn't auto recover or how long it has been. Apologies friends.

raynes22:11:43

I need to rewrite it in node + express. 😉

raynes22:11:20

Also apologies for ignoring my 300 thousand Clojure projects, I've been hustling to find a job and there are two companies in Los Angeles using Clojure that I am aware of and none of the ones except Geni that I've worked and am going to work for are using it, but I hope to have a better work life balance at the job I'm about to join and ideally will start going through and updating my projects and releasing and such.

raynes22:11:46

I start December 5th, so between now and then I'll do what I can. Any projects anyone wants admin access to, to help manage, please let me know. I don't know what folks consider important and priority

jcsims22:11:19

I’ve used a couple of your libs @raynes, thanks for putting them out there, and the work that went into them 👍. Glad to hear your work situation is stabilizing.

raynes22:11:36

I've been contracting pulling my <expletive> together and kinda forgot to look for jobs for 7 months.

raynes22:11:47

Thanks man.

raynes22:11:53

More work will go into them. Seems like a lot of folks are really into raynes/fs. I think that needs to be entirely rewritten against Java 8.

raynes22:11:13

@juanmp If they let me slip in some Clojure, I work in data engineering and will be learning machine learning. Ask me in February 😛

dpsutton22:11:20

@raynes can you show your github profile?

raynes22:11:21

Most are Clojure projects I started when I was a teenager, but I've got projects in there in lots of languages. They cited that in the interview so it was the first interview I've done with no whiteboarding. Blew my mind. I did pair with my team lead on some non-trivial js problems for the first hour, two latter portions of the rxjs interactive tutorial.

raynes22:11:42

Embarrassingly forgetting how to use a mac keyboard having used a Surface Book for months.

raynes22:11:59

Oh, peronas is shutting down on the 30th. I need to add a new authentication mechanism to refheap. Any preferences? I'd default to github.

jmp23:11:36

Awesome @raynes ! I'll be sure to ping you then 🙂

michaeldrogalis23:11:53

@bfabry I don’t know what led you to believe that Onyx is a distributed implementation of CSP for Clojure, but that isn’t accurate.

bfabry23:11:02

I do not believe that. onyx is a distributed stream processing system for clojure

bfabry23:11:21

for/written in

michaeldrogalis23:11:31

It seems I should not have read these two statements as being related, then. 🙂

michaeldrogalis23:11:47

Sorry for the noise

bfabry23:11:21

yup. the first was pointing to onyx. the second was responding to what that person actually wanted. it's definitely ambiguous all good