Fork me on GitHub
#clojure
<
2016-10-14
>
pythonruby03:10:48

Hi, I am using Clojure to process a time serial data file to find out all the lines whose minute(timestamp) is the same as the next line. What’s the idiomatic way to do that?With line-seq I need to kinda store the “state” (which is the current line), which seems to be bad.

hans03:10:17

(defn remove-dups [[head & tail]]
  (cons head
        (when (seq tail)
          (lazy-seq (remove-dups (drop-while (partial = head) tail))))))

pythonruby03:10:02

@hans is your code the answer to my question?

pythonruby03:10:10

I am really new to clojure

hans03:10:20

@pythonruby Not directly, but almost - You'll have to replace the = with your appropriate equality predicate

pythonruby03:10:48

Thanks @hans , let me “digest” your code.

abarylko03:10:42

maybe group-by timestamp?

hans03:10:48

@pythonruby What is does is close over the last element returned (head) so that duplicates are removed before the next one is returned. That is achieved by lazy-seq.

hans03:10:38

@abarylko that would remove the overall order and be less efficient because it would realize the whole sequence as map.

herbm03:10:07

#pythonruby #hans code is likely better but I'll give you a my newbie suggestion: one standard idiom for processing a sequence "2 at a time" (e.g., to send to map) is: (partition 2 1 [:a 😛 :c :d :e]) => ((:a :b) (:b :c) (:c :d) (:d :e))

herbm03:10:59

Stupid emoji, suposed to be : b keyword

hans03:10:09

use backticks to quote code.

pythonruby03:10:42

@herbm that seems neat. Thanks for the idea.

bcbradley05:10:25

I have a situation where I'm issuing an http request that includes an asynchronous callback, but the callback is optional. In case the user doesn't provide a callback, I still have to provide a callback to the library I'm using to issue the http request. What is the most idiomatic way to say "function that does nothing"?

bcbradley05:10:40

Here are some things I've considered, but I don't know which would seem most idiomatic:

bcbradley05:10:21

identity
(constantly nil)
{}
(fn [& _] nil)

seancorfield05:10:14

identity is normally the "function that does nothing"

eslachance06:10:33

What am I supposed to do if my repl is giving me an error Error Occured: java.lang.IllegalArgumentException: Key must be integer but doesn't tell me what line it is, or any stack at all for that matter? :thinking_face:

eslachance07:10:01

Oh my... I figured it out. instead of returning a list I was returning [listname] , so it made an array. Well then.

hans07:10:59

@pythonruby As it stands it is not, but it can be made lazy like my version.

hans07:10:18

@pythonruby Basically, it is doing the same thing without being lazy and also less efficient 🙂

ska08:10:13

Hi. Has anybody ever had any problems with shutting down an embedded Jetty which was started with ring.adapter.jetty/start-jetty? In my app, (.stop @jetty-server) does not return for reasons I do not understand.

pythonruby08:10:16

@ska: from repl or compiled jar?

kurt-yagram08:10:46

about styling: if one defines a constant ^:const, these values are supposed not to have a special styling. However, to me, it makes a lot of sense to know that these values are actually compiled into the resulting code. So, when clojure programmers use const, do they use any special syntax (like, e.g. capitalized names)?

pythonruby09:10:56

Any chance it's called from "do"?

ska09:10:10

I don't understand, @pythonruby

pythonruby09:10:27

I made a mistake before that I called my function from within the "do", which in compiled jar it's by default not realized.

pythonruby09:10:24

The lazy part in "do" is not realized.

pythonruby09:10:15

I had to use doall per the instruction from some pro from here.

pythonruby09:10:30

Your case may be different

ska09:10:51

No, this is not the case here. I have a pretty straight forward fn in my main namespace which I'll post in a second. There, the call to .stop never returns.

ska09:10:54

... hm, hang on, I'll test something...

mpenet09:10:12

more likely the deref that never completes because you didn't start the server with :join? false ?

ska09:10:55

Hm, interesting... I start the Jetty server last thing in my main with join? false. In my generic Thread/setDefaultUncaughtExceptionHandler I stop Jetty and everything else and want to restart it again. But now, the JVM exits, when I stop Jetty. Oh, and stopping Jetty works now. I had a swap! on an atom that I had already derefed in the current scope. Not a good idea.

pvinis11:10:58

some issues and prs on clojures jira are pretty old. is it because there are not enough people to check that stuff?

ashishnegi13:10:49

hi.. i have heard that being a lazy language and on jvm, we add a lot of garbage.. for faster garbage collection of intermediate collections we give some hints to GC in bytecode.. Can i get some reference to how it is done ? any help appreciated..

Alex Miller (Clojure team)13:10:35

@pvinis The reasons for that vary widely so it's hard to say one reason. Most patches are not good enough to apply and require additional work. In general, a lot of stuff is also gated by Rich's available time and attention.

Alex Miller (Clojure team)13:10:56

@ashishnegi: the compiler aggressively clears local references to make more objects available for GC

Alex Miller (Clojure team)13:10:49

That is, creates bytecode that clears references - a lot of that is done via the Util.ret calls

Alex Miller (Clojure team)13:10:15

One of the semi hidden hints is the ^:once hint which is leveraged in lazy-seqs for example

ashishnegi13:10:40

@alexmiller thanks for this useful information.. I looked in the code.. Is ^:once is being used here ?https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L5046

pvinis14:10:01

so for writing a simple web app like a Facebook bot, should I use compojure-api? what are people here using?

donaldball15:10:18

ring+jetty may be the most common substrate. If you need routing, bidi is pretty good.

donaldball15:10:47

Lots of people do like compojure-api. Myself, I dislike how it encourages routes to be expressed as vars.

pvinis15:10:57

I guess I need some basic routing

pvinis15:10:26

thanks I will check both of these out

ikitommi15:10:50

@donaldball as vars? You can do mostly the same stuff as with compojure.

pvinis15:10:39

I saw bidi has a nice getting started part. that's useful.

dominicm15:10:28

I really love bidi, because it's just data. Disclaimer: I work for JUXT, but I'd recommend it either way

robert-stuttaford15:10:53

data > functions > macros!

lwhorton15:10:22

Hmm, I’m confused about a namespace issue. If i

(defprotocol IFoo (my-func [this])
(extend-type string IFoo (my-func [x] (do something x)))
(in namespace A). From namespace B how do I invoke (my-func some-x)? is it the protocol I need to require into namespace B? Or is it the symbol my-func somehow?

robert-stuttaford15:10:47

lwhorton, what happens when you try use it like it’s a normal function?

lwhorton15:10:14

i get a warning: use of undeclared Var my-func

robert-stuttaford15:10:32

and if you alias the ns and call alias/my-func?

lwhorton15:10:25

aha, that works! I suppose that means I don’t understand at all how protocols are binding vars to a namespace

lwhorton15:10:11

I would’ve thought by extending a base type string with my-func I could just invoke that fn on any string willy-nilly, but it seems the compiler still needs to know where the original symbol is defined

Alex Miller (Clojure team)16:10:27

my-func is a var in namespace A and you should invoke it and/or refer just like any other function

Alex Miller (Clojure team)16:10:21

You do need to make sure you have loaded any namespace that extends the protocol prior to invoking a protocol function on an instance of that type too

didibus18:10:55

Why is it in my REPL I get Infinity for this: (* 2 (bigdec Double/MAX_VALUE))

enyert18:10:10

I’m getting this value 3.5953862697246314E+308M

enyert18:10:52

But remember you are multiplying by 2 the max value of Double

enyert18:10:41

It’s a really great number

didibus18:10:57

I know, but because I coerce to BigDecimal, that shouldn't be a problem

didibus18:10:16

Maybe its an issue with my REPL when displaying the number back to me

didibus18:10:45

Ya, that's what it is, since if I do: (str (* 2 (bigdec Double/MAX_VALUE))) I see the actual number

enyert19:10:39

Are you using lein repl?

enyert20:10:34

If you are using lein repl and you are working with a project with a project.clj file check this as a reference on which value could be affecting your repl https://github.com/technomancy/leiningen/blob/master/sample.project.clj