Fork me on GitHub
#clojure
<
2015-09-23
>
wqhhust02:09:59

in repl we could use *1 to get the last value. But after I feed a new express, then *1 changed to the new last value. in scala, it use res0 to get the first value, res1 for the second value, etc. and there is no limit about how many old values to retrieve, but *4 will throw error, only *3 is available, do we have other way to get more historical value in REPL?

danielcompton03:09:37

@wqhhust: I don’t think so, but you could look at something like gorilla-repl if you’re wanting to do more historical exploration

Alex Miller (Clojure team)03:09:26

however it is possible to customize the repl function to do that :)

Alex Miller (Clojure team)03:09:00

but it would be a lot of effort (for what you're getting)

Alex Miller (Clojure team)03:09:14

I rarely use much beyond *1 anyways

meow04:09:04

Are there any good references or implementations using Protocols and Records for working with points? I need something beyond the absolute basics, like figuring out if/when my functions should be written expecting an arg to be a Point vs. a vector containing an [x y] value, etc. And dealing with having sets of points and vectors of points, etc.

wqhhust04:09:22

for clj-webdriver, do way we have a way to find out how many rows a table have? in taxi api, it have many function, but none of them could get how many rows a table has or how many columns a table has.

meow04:09:20

And points with additional fields, like a Cell representation for cellular automata where a Cell might have fields for [x y age color neighbors]

meow04:09:07

How's this for generic?

([get-xf prep seed init]
   (letfn [(process [coll]
                    (lazy-seq
                      (when (seq coll)
                        (let [new-coll (into (init) (get-xf coll) (prep coll))]
                          (cons new-coll (process new-coll))))))]
     (process (into (init) (seed))))))

Alex Miller (Clojure team)04:09:43

the most important question is: do you care about primitive math? (boxed math is 100x slower)

Alex Miller (Clojure team)04:09:07

the clojure collections (vector, map, etc) are always boxed

Alex Miller (Clojure team)04:09:10

so if you want prim math, you must use either Java arrays or deftype/defrecord (where you can use ^long and ^double hints)

meow04:09:55

do you mean instead of sets, for example?

Alex Miller (Clojure team)04:09:14

same as vector or map, will always be boxed

meow04:09:18

I figured I'd have a Cell defrecord that implemented various protocols

meow04:09:47

my current CA implementation just uses sets of [x y] vector pairs

meow04:09:56

([get-xf prep seed]  ; Useful default for cellular automata.
   (produce get-xf prep seed #(set nil)))

meow04:09:09

So I need to figure out the protocols I need and the defrecord for Cell. Then I need to change the functions used in the transducing stack.

Alex Miller (Clojure team)04:09:24

I'm tired so I might be misspeaking, but I don't think protocol methods support primitives either

meow04:09:25

okay, I can look into that

Alex Miller (Clojure team)04:09:23

a good trick is to use definterface to create a Java interface with primitive type hints, then implement that in a deftype or defrecord

meow04:09:38

I need to figure out what to change so that my existing functions work with a Cell defrecord, or change the functions as need be, such as these:

(defn neighborhood-8 [[x y]]
  (map vector
       ((juxt inc inc identity dec dec dec identity inc) x)
       ((juxt identity inc inc inc identity dec dec dec) y)))

(defn prep-8
  "Returns a map of [x y] n pairs."
  [cells]
  (frequencies (mapcat neighborhood-8 cells)))

meow04:09:55

@alexmiller: oh, wow. Cool page. That should give me plenty of stuff to poke into. simple_smile

Alex Miller (Clojure team)04:09:21

I wrote that one. it took a long time. :)

Alex Miller (Clojure team)04:09:48

this is an area where performance optimizations fight with simplicity/readability

meow04:09:58

I bet. Yeah, I don't really want to lose too much simplicity if I can avoid it.

meow04:09:12

Baby steps.

meow04:09:56

I just figured even a simple defrecord or deftype has to be better than vectors with x,y pairs.

Alex Miller (Clojure team)04:09:00

you might look into some of the array libs too like hiphip (array!)

meow04:09:54

But what I haven't seen examples of is how a Point or Cell record behaves when put into collections like set. And I use set membership behavior for the CA logic, ala:

(defn exist?
  "Returns a cell if destiny will allow, or mother nature brings it to life."
  [birth? survive? cells [cell n]]
  (if (cells cell)
    (when (survive? n) cell)
    (when (birth? n) cell)))

Alex Miller (Clojure team)04:09:34

records behave like maps

Alex Miller (Clojure team)04:09:06

types behavior however you tell them to :)

Alex Miller (Clojure team)04:09:38

by "like maps" I mean "equality comparison by value", just like all the other Clojure collections

meow04:09:03

I suppose some of this code would just work, but what I really want is for (->Cell 5 6 :red) and (->Cell 5 6 :blue) to be considered equal since they have the same x,y values. Does that make sense?

meow04:09:14

so the x and y are the "primary key" in Relational terms

meow04:09:05

In Python I would customize the class and overide the hash__ method or something like that (can't remember any more, but I used to)

tcrayford04:09:26

@meow: you can do that with deftype, but not with defrecord

tcrayford04:09:43

and you'd override equals and iirc hashCode

meow04:09:54

So that attempting to put both of those cells into a set would fail for the second cell, but that only works if the hash value ignores the color field.

meow04:09:36

@tcrayford: read some of your blog posts today and watched the video of your presentation - good stuff.

meow04:09:24

@tcrayford: that's what I was thinking I would need to do

meow04:09:24

right, so I would need to use deftype because defrecord's implementation of map protocols would get in the way

tcrayford04:09:36

thanks simple_smile, and happy it was helpful

tcrayford04:09:40

yep, you got it

meow05:09:25

I'm also wondering how I should model a Cell. For example, I could make it an isa? of a Point, meaning something like deftype Cell [x y color age] or I could say that a Cell contains a Point and additional fields. But I don't know if that is possible or advisable.

tcrayford05:09:29

@meow: depending on performance concerns, I'd typically just use a map and write some functions that work on the map. Depends what you're doing though simple_smile

meow05:09:30

I'm guessing for performance reasons a simple, flat structure would be best. But the object/database modeler in me isn't sure.

tcrayford05:09:21

but, if you're concerned about perf, yeah, simple flat deftype seems like the best thing for that from my perspective

tcrayford05:09:57

it depends on the nature of the codebase and the problems you're trying to solve as to "is a nicer model more important than performance" 😉

meow05:09:03

@tcrayford: I've got a library for constructing L-systems and cellular automata where for the CA stuff the cells are just represented as [x y] vectors and I want to change it to use deftypes and protocols for two reasons: performance and so that the cell can have additional fields, like age and color and neighbor counts and whatever else someone using the toolkit might want to define.

meow05:09:55

The L-system is pretty much done and only needed a Protocol, so that was easy. The CA stuff needs to go from [x y] to a Cell deftype and then I don't know if my code should extract an [x y] out of the Cell and use that on the existing function, or whether the function should be rewritten to expect an object the satisfies? some protocol.

tcrayford05:09:44

@meow: another potential nice solution there: implement nth for your deftype, then all your existing list destructuring code will work

tcrayford05:09:50

Gotta run, apologies

meow05:09:04

@tcrayford: thanks for the help simple_smile

meow05:09:26

@alexmiller: thanks again, always a pleasure simple_smile

meow05:09:07

@tcrayford: aha! I think I know what you mean. You're saying if Cell is a deftype and implements nth and I leave x and y as the first two fields (or make it work out that way in the nth implementation) then if I pass a Cell as an arg to an existing function that does a [x y] destructuring that it will continue to work because behind the scenes the destructuring is calling nth on the object. Interesting. I hadn't really thought through how the destructuring sugar works. That's a great tip, thanks. simple_smile

meow05:09:29

And that means I wouldn't have to change any of the existing functions, so they can support either an [x y] pair or a Cell or anything that implements nth in a similar fashion. Sweet!

meow05:09:32

That was definitely a missing piece for me was how to make a new deftype not completely disrupt the existing code or force me to change the code so that it no longer worked with something that wasn't a Cell type.

meow05:09:44

cool, enough for today, time for sleep

Alex Miller (Clojure team)05:09:45

really, implementing Indexed, which has an nth method

lvh05:09:52

Does anyone have strong opinions on Pallet?

cddr07:09:45

@ognivo: I use manifold at work but never used lamina.

mbertheau09:09:08

I cannot write (fn [c] [(c 0) (c 1)]) with #(..), correct?

mikethompson09:09:34

#(vector (% 0) (% 1)) ?

xificurC13:09:18

is there a built-in that given an overloaded predicate and a seq returns a seq of 2 seqs, one for true-like values and one for false-like?

rauh13:09:19

Maybe group-by with some?

xificurC13:09:31

It's not group-by because the predicate doesn't return true and false, it returns something or nothing (false or nil). It's not filter because we want the false ones too. Something like juxt filter remove maybe?

xificurC13:09:00

@rauh: thanks that works

xificurC13:09:46

although it wouldn't work with a mixture of false and nil as I described

rauh13:09:15

Can you give an example? Not sure I understand the problem

xificurC13:09:44

sure. If you had a predicate that could return 1, 2, 3, false and nil and a seq on which the predicate works, make it so that the seq is divided into 2 - one for the "true" results like 1, 2 or 3 and one for the "false" results like false or nil

xificurC13:09:31

(_ my-pred my-seq) -> (my-seq-truthy my-seq-falsey)

meow13:09:51

there is keep as well, but that will include false in the results

rauh13:09:16

@xifi: Oh I see, then use boolean instead of some?

rauh13:09:42

So: (group-by (comp boolean your-pred?) your-seq)

xificurC13:09:04

@rauh: thanks. Composition ftw

rauh13:09:06

That'll take care of nil and false

joost-diepenmaat14:09:33

anyone got an idea what would trigger "ClassNotFoundException: java.sql.SQLType” on a clojure.java.jdbc INSERT

joost-diepenmaat14:09:02

I was trying to upgrade postgres-jdbc and c3p0

apviitanen14:09:12

@joost-diepenmaat: the obvious thing would be Java version. java.sql.SQLType was introduced in Java8, so if you’re using Java7 or older it won’t exist

joost-diepenmaat14:09:53

ah that may explain why it works in development but not in the production environment

joost-diepenmaat14:09:19

I thought I picked the correct version of the JDBC driver for 1.7 but maybe not… should 9.4-1203-jdbc41 not work with java 1.7?

joost-diepenmaat14:09:36

org.postgresql/postgresql "9.4-1203-jdbc41” I mean

apviitanen14:09:22

Yes, it should. jdbc 4.1 is for 1.7

joost-diepenmaat14:09:28

now to find a solution 😕

joost-diepenmaat14:09:52

maybe revert to 0.9.5

nooga16:09:37

anyone using Pulsar? I’m interested in using it in the next project because it looks awesome. I just don’t know what to expect in the long run.

nooga17:09:27

yup, seen that

nooga17:09:37

I’ve read about Pulsar actors and they look like a great architecture for what i’m planning

nooga17:09:54

+ they should work great with component

nooga17:09:47

my only fear is that Pulsar may turn out to be slow, bug ridden or not really maintained

ghadi17:09:40

nooga: they're definitely not slow. Ron Pressler is quite the engineer. His demo at at the jvm language summit a couple years ago was impressive

nooga17:09:25

yeah, I figured out that’s unlikely

thomas18:09:15

hi... any paredit users here?

thomas18:09:28

I have this... ((fun a b c))

thomas18:09:43

and want to get to (func a b c) ... but how?

colin.yates18:09:05

in emacs or Cursive?

colin.yates18:09:55

in emacs I would put the cursor between the last two brackets and execute sp-forward-barf-sexp

colin.yates18:09:08

then I would rename fun to func

colin.yates18:09:13

is that what you are asking?

ian18:09:05

M-s on the inner paren

ian18:09:31

On the inner open paren that is

thomas18:09:48

@ian that was it... thanks...

ghosss18:09:49

M-s should work anywhere in the sexp

ian18:09:28

Good to know. I suppose I never thought about that but it makes sense. Thanks @ghosss

colin.yates18:09:14

I never knew about M-s - good to know.

nooga18:09:00

M-) is useful as well… and the rest I don’t remember 😄

thomas18:09:33

thanks guys... just started using emacs...

thomas18:09:48

and finding it quite a learning curve

ghosss18:09:02

the rest of paredit can be expressed with just M-s, M-) and creating parens, so there's no need to learn any more 😉

colin.yates18:09:09

my golden hammers are C-) and C-}.

thomas18:09:03

and true as well I suspect

nooga18:09:04

@thomas: I’ve been using emacs for several months now and IIRC it felt natural after a week or two, the rest is exploring different commands and clever ways to do stuff

nooga18:09:34

I still have to start building my own config instead of modding emacs-live

thomas18:09:39

@nooga: thanks for the encouragement.

colin.yates18:09:49

@nooga - I think there is a bit of snobbery around building your own config. If it works for you, why rock the boat? Sure, there is learning etc. And the other perspective is that you have built your own config by modding emacs-live simple_smile

nooga18:09:35

well, sure, but there are things with modules that e-l does, which I don’t understand and I have to rely on the updates from its maintainers

colin.yates18:09:13

yeah, I didn’t last too long with e-l for a similar reason. I moved over to prelude and things were a lot smoother. My starting assumption is that I didn’t spend enough time so any issues with e-l are mine, but prelude config seemed closer to the metal, if you see what I mean

colin.yates18:09:24

(wondering if we should move this to the #C099W16KZ channel?)

nooga18:09:34

@yogthos: maybe you have some experience with Pulsar? (http://yogthos.net/posts/2015-06-17-Using-Pulsar.html) I’m planning to put it into use in my mission critical project and wondering if it’s a wise move.

yogthos18:09:21

@nooga: the guys making it are dogfooding pulsar, so it shouldn’t have any glaring problems simple_smile

yogthos18:09:38

I’ve used it only in a slight capacity myself and it worked as advertised

nooga18:09:51

it looks very promising

yogthos18:09:58

the main gotcha is that you have to package the agents, but that’s not too bard using lein

yogthos18:09:11

err not too bad even simple_smile

nooga18:09:16

yeah, I’ve read your post during my research

yogthos18:09:53

pulsar is as close as you can get to erlang with clojure I think simple_smile

nooga19:09:36

erlang always interested me but clj has much better syntax hehe

nooga19:09:52

and everything… IMO

yogthos19:09:13

erlang does have a very fancy vm

nooga19:09:46

thnks, I’ll start sketching with actors then 😆

tel19:09:45

how do pulsar and core.async relate?

tel19:09:50

so far in reading the pulsar docs they seem very similar

ghadi19:09:16

pulsar is probably larger in scope

ghadi19:09:46

actors aren't covered by core.async

ghadi19:09:53

pulsar (through quasar) can suspend across function boundaries, core.async cannot

bensu19:09:44

@tel: they follow two different concurrency models. core.async implements CSP and pulsar implements actors

bensu19:09:12

they are similar in the sense that they try to solve similar fundamental problems, but they provide very different abstractions for them

bensu19:09:16

which one is "better" depends on your application

bensu19:09:10

a Google groups thread about clojure on the Erlang VM has been recently revived.

tel19:09:09

I see. I always think of CSP as being a strict superset of actors, but there’s a lot here to make them more convenient

bensu20:09:05

if you mean superset as in "CSP can implement actors", I think that's right. But that might not be a good idea since you are passing on a bunch of benefits (mostly related to distribution and fault tolerance) that could be gained from actors.

tel20:09:46

I do mean that

tel20:09:14

the distribution bits are built atop CSP still so long as the messages are serializable

tel20:09:34

selective receive is something new though

tel20:09:30

a ha, and it’s got full linking

tel20:09:37

I wonder how that works

tel20:09:44

does java have asynchronous exceptions?

tel20:09:24

looks like it’s implemented that way at least

ghadi20:09:23

exceptions across thread boundaries can sometimes bite ya. A lot of folks I've seen turn them into data

ghadi20:09:35

clojure 1.7 has Throwable->map

ghadi20:09:43

which is stupid nice

tel20:09:05

what bites you that changing them to data fixes?

tel20:09:44

async exceptions are notoriously challenging, but pretty essential for the kind of distributed computing that erlang offers

tel20:09:15

but most of what I know that makes them challenging arises from them being able to be thrown halfway through important operations

ghadi20:09:15

exceptions can hold on to stack frames IIRC

ghadi20:09:46

async failure - Yes. async exceptions - Not sure

ddellacosta20:09:35

woah tel hangs out in #C03S1KBA2? Awesome. simple_smile

tel20:09:14

recently started a clojure project at work, so I’m here to learn and brush up things 😄

ddellacosta20:09:38

@tel: gotcha! I am a big fan of your random comments on HN and reddit about type theory and Haskell and math and whatnot…nice to see you hanging around here! And you sent me a wonderful response to an email once re: one of your blog posts

tel20:09:53

Ahhhhh! 😄 Now I see why your name was ringing bells

ddellacosta20:09:08

yeah, that one time…haha. simple_smile

ddellacosta20:09:23

anyways, interested to see how you use Clojure

tel20:09:27

simple_smile I appreciated it though

tel20:09:55

I’m currently working on a clj/cljs backend frontend web app

tel20:09:11

been sort of throwing it together on a deadline lately

tel20:09:36

but there’s some fun stuff to play with eventually

tel21:09:05

since it’s essentially going to boil down to clientside stream processing

beppu21:09:54

@nooga: One minor annoyance I bumped in to while trying pulsar out was that it doesn't get along with the refactor-nrepl plugin. https://github.com/puniverse/pulsar/issues/43

nooga21:09:11

@beppu: thanks, I use refactor so that might annoy me :}

nooga21:09:32

good you’ve found a workaround

beppu21:09:57

I had to give up refactor-nrepl, but at least I had a repl.