Fork me on GitHub
#clojure
<
2016-12-24
>
ddav01:12:19

Is there a way to track hosted nrepl server state changes like namespace creation or var binding without explicitly messing with op's code and tools.analyzer?

ddav01:12:31

What i'd like to achieve is the clojure ref behaviour(add/remove-watch) for entire nrepl state

steverob06:12:31

Just started learning Clojure

steverob06:12:37

using the brave and true book 🙂

steverob06:12:53

When generating the app I forget to specify the app template 😄

steverob06:12:57

And ran into a lot of issues 🙂

steverob06:12:26

I managed to get the hello world to run and the jar too and finally figured out I did not specify the proper template 🙂

seancorfield06:12:18

@steverob Welcome to Clojure! You'll probably enjoy the #beginners channel where folks are very, very helpful and happy to work through step-by-step on issues. What's your background prior to Clojure?

steverob06:12:24

Thanks @seancorfield 🙂

steverob06:12:32

I'll head there whenever I need help.

steverob06:12:50

I've been building web apps . mostly Ruby / Rails.

steverob06:12:15

Wanted to learn Lisp for a while - so wanted to do that with Clojure.

seancorfield06:12:19

A lot of Rubyists seem to move to Clojure -- but there are a lot of stark differences: Clojure doesn't do frameworks, but instead it favors composable libraries, which you may find a big surprise after Rails.

seancorfield06:12:38

(and of course you're going from an almost OO-everywhere language to an almost FP-everywhere language 🙂 )

roelof09:12:14

First spec problem. I have wrote this code :

defn ->long [s] (try (Long/parseLong s) (catch Exception _ ::s/invalid)))

(s/def ::page (s/and (s/conformer ->long) (s/int-in 1 471)))

(s/def ::optional-page (s/nilable ::page))

(defn home-page [page]
  (let [page-num (or (read-string page) 1)
        [page nil page-num (or (s/conform ::optional-page page) 1)] page-num
        url ""
        options {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True" :p page-num :ps 10}}]
    (layout/render
      (if (s/invalid? page-num)
        (println "pagenumber is not right")
        "home.html" {:paintings (-> (client/get url options)
                                  api/read-numbers
                                  api/fetch-paintings-and-images-front-page)}))))  

roelof09:12:15

what I try to do it that if someone enters a false pagenumber like "a" then I see a error message else the code will continue and some paintings are shown.

roelof09:12:55

But now I see this error :

Caused by: clojure.lang.ExceptionInfo: Call to clojure.core/let did not conform to spec:
In: [0 2] val: [page nil page-num (or (s/conform :paintings2.routes.home/optional-page page) 1)] fails spec: :clojure.core.specs/local-name at: [:args :bindings :binding :sym] predicate: simple-symbol?
In: [0 2 3] val: ((or (s/conform :paintings2.routes.home/optional-page page) 1)) fails spec: :clojure.core.specs/seq-binding-form at: [:args :bindings :binding :seq] predicate: (cat :elems (* :clojure.core.specs/binding-form) :rest (? (cat :amp #{(quote &)} :form :clojure.core.specs/binding-form)) :as (? (cat :as #{:as} :sym :clojure.core.specs/local-name))),  Extra input
In: [0 2 0] val: page fails spec: :clojure.core.specs/map-binding at: [:args :bindings :binding :map :mb] predicate: vector?
In: [0 2 0] val: page fails spec: :clojure.core.specs/ns-keys at: [:args :bindings :binding :map :nsk] predicate: vector?
In: [0 2 0] val: page fails spec: :clojure.core.specs/map-bindings at: [:args :bindings :binding :map :msb] predicate: vector?
In: [0 2 1] val: nil fails spec: :clojure.core.specs/map-binding at: [:args :bindings :binding :map :mb] predicate: vector?
In: [0 2 1] val: nil fails spec: :clojure.core.specs/ns-keys at: [:args :bindings :binding :map :nsk] predicate: vector?
In: [0 2 1] val: nil fails spec: :clojure.core.specs/map-bindings at: [:args :bindings :binding :map :msb] predicate: vector?
 

roelof09:12:13

which part did I not understand about using spec ?

roelof09:12:58

I did try to use this code in repl and saw this when starting up the repl

sveri10:12:01

@roelof this has only slightly to do with spec, as spec is what generates the error output you are seeing. Also the error message says what is wrong: Call to clojure.core/let did not conform to spec. So your let macro is messed up. I would advise you again to turn on parinfer.

bfabry10:12:53

@roelof you can't shadow the keyword nil in a let destructuring

roelof10:12:07

chips. I want to test if someone do not give a parameter on the url.

sveri10:12:11

I am trying to make sense of that whole line: [page nil page-num (or (s/conform ::optional-page page) 1)] page-num

bfabry10:12:27

actually you can't do any of that stuff

bfabry10:12:41

lhs of a let should only be simple symbols or destructuring forms

roelof10:12:51

I copied it from this code from seanconferfiels :

boot.user=> (require '[clojure.spec :as s])nilboot.user=> (defn ->long [s] (try (Long/parseLong s) (catch Exception _ ::s/invalid)))#'boot.user/->longboot.user=> (s/def ::page (s/and (s/conformer ->long) (s/int-in 1 471))):boot.user/pageboot.user=> (s/def ::optional-page (s/nilable ::page)):boot.user/optional-pageboot.user=> (let [page nil page-num (or (s/conform ::optional-page page) 1)] page-num)1boot.user=> (let [page "42" page-num (or (s/conform ::optional-page page) 1)] page-num)42boot.user=> (let [page "a" page-num (or (s/conform ::optional-page page) 1)] page-num):clojure.spec/invalidboot.user=> (let [page "500" page-num (or (s/conform ::optional-page page) 1)] page-num):clojure.spec/invalid  

sveri10:12:15

at least type it

sveri10:12:22

and try to understand while typing

bfabry10:12:10

sean's let only has simple symbols on the lhs

roelof10:12:33

here a better layout :

boot.user=> (require '[clojure.spec :as s])
nil
boot.user=>  (defn ->long [s] (try (Long/parseLong s) (catch Exception _ ::s/invalid)))
#
boot.user/->longboot.user=> (s/def ::page (s/and (s/conformer ->long) (s/int-in 1 471)))
:boot.user/pageb
boot.user=> (s/def ::optional-page (s/nilable ::page)):boot.user/optional-page
boot.user=> (let [page nil page-num (or (s/conform ::optional-page page) 1)] page-num)
1
boot.user=> (let [page "42" page-num (or (s/conform ::optional-page page) 1)] page-num)
42
boot.user=> (let [page "a" page-num (or (s/conform ::optional-page page) 1)] page-num)
:clojure.spec/invalid
boot.user=> (let [page "500" page-num (or (s/conform ::optional-page page) 1)] page-num)
:clojure.spec/invalid
 

roelof10:12:05

I try to make this work on a function instead of the repl

bfabry10:12:31

@roelof this line, is wrong.

(let [page-num (or (read-string page) 1)
        [page nil page-num (or (s/conform ::optional-page page) 1)] page-num

roelof10:12:35

oke, what I try to do is test if the page is not given then the pagenumber must be 1

roelof10:12:01

or can I make this one like this

(let ] [page nil page (or (s/conform ::optional-page page) 1)] page   

roelof10:12:33

but then I shadow the variable page which is also not good 😞

ezmiller10:12:13

I’m trying to understand a convention I’ve not seen before:

(defn
  ^{:macchiato/middleware
    {:id :wrap-restful-format}}
  wrap-restful-format
  …
  …
  …)
 
The part I don’t understand is the part before the function name wrap-restful-format that starts with the ^. Can anyone explain what that segment is doing?

ezmiller10:12:28

(I’ve excluded most of the function just to keep things brief…)

bfabry10:12:58

there's nothing "wrong" with shadowing variables in a let, other than the person who comes along to read the code might give you a kick. that code would work (minus the extra ])

roelof10:12:17

@bfabry I do not like to be kicked 😞

bfabry10:12:33

so just use a new name

roelof10:12:01

oke, I will try to make it work but I think spec is at this moment more then I can chew

bfabry10:12:51

spec is not the issue you're having here, you're misunderstanding how a basic clojure form (let) works. you're just getting a reasonable error about it from spec

bfabry10:12:28

in a prior version of clojure you'd just get "unsupported binding form"

roelof10:12:33

I know how a basic let form works

roelof10:12:08

( let [ b 5] ) takes care that b has now the value of 5

pesterhazy10:12:20

@ezmiller that attaches metadata to a fn: http://clojure.org/reference/metadata

ezmiller10:12:26

thanks @pesterhazy. and hello!

pesterhazy10:12:11

cljs.user=> (defn ^{:a :b} foo []) (-> #'foo meta :a)
#'cljs.user/foo
:b

pesterhazy10:12:15

hello ethan 🙂

pesterhazy10:12:28

actually the metadata is attached to the var, not the fn

lvh11:12:36

related (asked yesterday but nobody knew):

#{
    ^{:reason "Bogus nullary argspec; also general market metadata."}
    #'amazonica.aws.ec2/describe-spot-datafeed-subscription
}
What is the :reason metadata attached to? It’s not the var.

lvh11:12:10

current hypothesis: lost in the void

bronsa11:12:06

it's only available at compile time as metadata attached to the (var amazonica..) list

bronsa11:12:32

so yeah, lost in the void at runtime

bronsa11:12:27

it's accessible from macros tho

lvh11:12:49

bronsa: thanks! that’s what I assumed

lvh11:12:06

I wonder what I’ll do with it; I’d like it to be available for introspection, but I guess maybe comments are good enough

ezmiller11:12:15

another convention I am not sure about: (req->map node-client-request node-server-response opts). What’s the -> doing here in req->map?

ezmiller11:12:20

oh never mind: it seems req->map is a fn name. the -> threw me off.

lmergen11:12:49

it's a common idiom in Clojure for conversion of data

pesterhazy11:12:12

it's an arrow and means "to" 🙂

nooga13:12:46

Are there, by any chance, any pixie-lang contributors around?

mpenet16:12:37

@nooga contributors or maintainers?

mpenet16:12:59

(I did contribute a few tidbits/libs in the past)

nooga16:12:38

@mpenet hi! I’ve seen your stuff! I’m trying to write a HTTP server in pixie, basing on node’s http-parser lib. I’m stuck on the ffi part, or “writing C in pixie”. Since the maintainers are unavailable, I was looking for someone who could explain few things to me.

mpenet16:12:19

I think I recall a few people did just that (or at least attempted)

mpenet16:12:38

but tbh I barely recall how adv. ffi stuff works in pixie. Your best bet is probably to dig into the source, or maybe try to ping @tbaldridge

nooga18:12:51

@mpenet haha, catching tbaldridge seems improbable 🙂

tbaldridge18:12:32

@nooga I'm here

nooga18:12:40

@tbaldridge 😮

nooga18:12:30

@tbaldridge I’m trying to figure out how to copy n bytes from CCharP and make that a pixie string or buffer

nooga18:12:06

also, is it possible to address bit fields in C structs from pixie?

tbaldridge18:12:41

@nooga pixie strings are unicode, so there's some conversion that has to take place, but you can look at the code for spit and slurp in that file to see how it's donw

nooga18:12:38

What about copying memory? C land calls callbacks of type typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); and I need to copy length bytes from at.

tbaldridge18:12:37

@nooga most of the code in pixie makes its own buffers, I'm sure it's possible to copy, but it's been way too long since I've been in this code.

nooga18:12:06

@tbaldridge Yup, that’s what I noticed. That’s why I couldn’t figure out how to copy from pre-allocated buffers just by reading the sources 🙂

nooga18:12:58

I can always write some kind of C layer I guess...

nooga20:12:48

well, I can just grab memcpy from the libc 😄

kah0ona21:12:42

I have a data-analysis question. I have a CSV of timestamped power output measurements. So each row is a timestamp + milliwatts tuple. I want to detect 'types of peaks', ie. cluster them in an unsupervised manner. Ideally i would like to have like 8 groups that i then can label them. The signal data is from a 'smart plug' to which an electrical device is connected. I want to detect what is happening in the electrical device (ie. what function is running) and measure the total usage time of each function. There is some variance in the spikes, and some functions have quite similar-ish output. Anyone got any pointers? I know this is not a clojure question, but I will implement this in clojure 🙂

kah0ona21:12:53

I looked at k-means algorithm, but not sure if that is a good fit

kah0ona21:12:09

anyone got any pointers?