Fork me on GitHub
#clojure
<
2016-01-27
>
amacdougall02:01:10

@mikeb: I haven't actually tried hooking it up yet, but Foundation's dynamic queries look like they'll do exactly what I want, while preserving the advantages of SQL templates. Good stuff! I'll see if I can't implement it next time I sit down to hack on this project.

jethroksy02:01:28

@jaen usually you look to multimethods when you're doing value-based dispatch. Protocols are quite a fair bit faster, so they are preferred where applicable

Alex Miller (Clojure team)04:01:23

protocols are about 5x faster for type-based dispatch (but that's all they do)

Alex Miller (Clojure team)04:01:50

I have a repo that benchmarks some value and type-based dispatch options at https://github.com/puredanger/poly-timing/blob/master/src/poly_timing/core.clj - just clone and "lein run"

jaen09:01:18

Yeah, I know they are faster. Hard for them not to be, since they compile down to Java's virtual dispatch and that's been optimised for last 20 years (didn't see that benchmark before though, thanks for the link), but I just feel kinda weird that I have to go to nominal, OO-like style in a language where it's usually not like that. Was mostly wondering what advantages protocols have over the alternatives (philosophically maybe). If it's just the performance I don't feel that compelled to start using them over multimethods (though I imagine using extend-type is not as unreadable as extending in-line in the defrecord), unless I actually have performance problems.

jaen09:01:28

Maybe there are other advantages apart from that I'm missing?

octahedrion10:01:21

@jaen @alexmiller quite often I use ordinary maps to lookup functions, 14x slower than protocols but quick and easy to write

octahedrion10:01:25

sorry more like 2x as slow

octahedrion11:01:14

actually, using criterium to benchmark, there's not much in it

mpenet11:01:35

if it's closed you can also just use "case", might be a tiny bit faster

mpenet11:01:50

but that wont work with all type of values

mpenet11:01:37

anyway, personnally I find protocols kinda nice, reminds more of haskell typeclasses than "OO-like" stuff

chedgren11:01:46

so,basically protocols over multimethods for most things?

chedgren11:01:53

most of the time

jethroksy12:01:26

I would think all the time where applicable

jethroksy12:01:50

Multimethods are really meant for value based dispatch

jethroksy12:01:30

Protocols can't do that

octahedrion12:01:12

what would be nice is an alternative representation to protocols where you can write multiple type-based implementations of a function, with the same semantics as arity-based function defs

jethroksy12:01:51

There's reify...

jaen12:01:05

I suppose you could do something like this with extend-type:

(extend-type MyType Countable
  (cnt [c] ...)

(extend-type MyType Foo
  (bar [x y] ...)
  (baz ([x] ...) ([x y & zs] ...)))

jaen12:01:17

Certainly more readable than stuffing everything into defrecord.

jaen12:01:34

Though you can't close over fields that way for convenience, so it's a trade-off.

meow12:01:35

I wish more was written about protocols as I really like them but it took a while to understand and appreciate them.

meow12:01:13

When used well they really are effective: core.matrix does a good job of this, as does some of http://thi.ng, like http://thi.ng color

jethroksy12:01:57

Had the pleasure of attending @mikera 's talk on core matrix yesterday

jethroksy12:01:23

I brought some friends completely new to clojure, and they were really impressed

meow12:01:42

I have some nitpicky things I don't like about core.matrix, such as inconsistent and too long lines in docstrings (would be nice if they were all 80chars) but overall it is very well organized and docstringed and such.

meow12:01:09

And @mikera was very generous in helping me to convert to core.matrix.

mpenet13:01:41

jaen: correct me if I am wrong, but also when you "inline" the functions in the record definition it actually compiles to java methods to that record, unlike with extend-type

mpenet13:01:50

so it's might not only be "nicer", but also faster

Alex Miller (Clojure team)13:01:58

@meow: we wrote a lot about protocols in Clojure Applied

Alex Miller (Clojure team)13:01:54

@mpenet that's true although due to the site cache that protocols make there's not as much perf difference as you'd think

mpenet13:01:37

never bothered profiling this kind of stuff, if you need to go that far one would probably choose something else than clojure in the first place 😄

Alex Miller (Clojure team)13:01:13

What fun would that be?

meow13:01:48

@alexmiller: You know I have nothing but the utmost respect for you. I just think things like protocols need to be written about outside of books that not everyone is able or willing to pay for.

mpenet13:01:04

I don't enjoy waiting on criterium runs personaly 😆

lopalghost13:01:51

I can recommend paying for Clojure Applied--truly a great general overview on how to build a Clojure app

Alex Miller (Clojure team)13:01:14

@meow: if you have specific topics, would love to have a clojure-site issue for things to add

lopalghost13:01:18

That said, I agree advanced features need better docs

lopalghost13:01:25

I just purchased http://howtoclojure.com on a whim, and plan to write up some tutorials and examples when I get a chance

Alex Miller (Clojure team)13:01:09

It took a couple years to write Clojure Applied - takes a lot of time to write stuff

jaen13:01:35

@mpenet: that would would be "less nice but more performant" to me; I kind of don't like how defrecords with many methods look. Feels like - ugh - Java.

jaen13:01:54

But good point, I didn't know extend-type compiles differently to extending in the body of defrecord.

meow13:01:26

I contributed to a Python book once so I know they take a ton of time and you only make peanuts for doing so.

meow13:01:05

What's the repo for the new site for issues? And the new site looks great, btw.

jplaza15:01:06

Really nice redesign.. now I can show the lang page to UI devs 😄

shayanjm16:01:40

Does anyone here have any recommendations for particularly fast HTML parsers in clj?

ghadi17:01:25

chiming in on the previous discussion re: protocols. multimethods and protocols are related but not interchangeable

ghadi17:01:58

performance is nowhere near the top of my "Reasons to Use Protocols" list

ghadi17:01:45

probably the top is having discipline programming to interfaces and not concretions

jaen17:01:21

But in a dynamically typed language you can do that as well without interfaces, though.

jethroksy17:01:09

@shayanjm clojure.xml can parse HTML

jethroksy17:01:20

HTML is essentially XML

jjttjj17:01:21

is there anything considered current best practice regarding namespace naming shemes? it seems like some projects use a reverse domain name scheme like (defproject com.jjttjj/my-lib ...) then (ns jjttjj.my-lib.core) and others just do (defproject incanter...) (ns incanter.main...) ... is either one considered strictly better these days?

jjttjj17:01:21

also do i use the "com." prefix in the directory structure/namespace name? some things seem to use that in the project name but nowhere else

ghadi17:01:58

jjttjj: you should have a prefix so that it is unambiguous, wouldn't conflict with another project

ghadi17:01:47

lots of people violate this because they don't care or think it's a silly Java-ism

jjttjj17:01:25

ghadi: ok cool thanks simple_smile that's kinda what i figured

ghadi17:01:36

as for the other side of the name -- I like to keep it semantically accurate. something.core is just the leiningen default

ghadi17:01:41

lots of people hate the name core

jjttjj17:01:19

thanks. i feel like i use this slack to ask the dumbest kind of irrelevant style questions but this one doesn't really seem to be talked about anywhere and i'm gearing up to release my first open source thing and i figure i might as well ask around and get these things right on the first release

mbertheau18:01:10

What's the most concise way to make a vector [a b] or [a] or [b] or [] out of a and b, where each value should be in the vector only if it is truthy? I have (vec (concat (when a [a]) (when b [b]))) now, which seems awfully verbose.

markbillie18:01:56

This might be the wrong place to ask this question, and I apologize if so. I am wondering if it is possible to issue a single command from the console that starts lein repl and also executes a couple of commands once the repl starts... similar to ruby's irb -e "puts :started_irb"

donaldball18:01:34

(into [] (filter identity [a b])) ?

manderson18:01:39

4 characters shorter simple_smile (into [] (remove not [a b]))

mbertheau18:01:21

Thanks! I'll go with (into [] (remove nil? [a b])); seems the most readable to me.

manderson18:01:45

@mbertheau: careful as nil? will only remove nil, not false

mbertheau18:01:37

@manderson: Thanks for the heads-up. nil? is sufficient in my case.

manderson18:01:53

heh, clojurebot is slow...

ghadi18:01:56

if you're on 1.7 move the parenthesis over:

ghadi18:01:29

i guess clojurebot isn't on 1.7

ghadi18:01:57

(into now accepts a transducer in the middle)

benzn22:01:29

Can someone help me understand why the two statements produce different results?

=> (reduce + (map bigint (seq (byte-array (range 0 400)))))
6072N
=> (reduce + (map bigint (range 0 400)))
79800N

benzn22:01:45

clearly the first is overflowing

benzn22:01:06

but i feel like it... should have everything upcast to a bigint?

bfabry22:01:45

benzn: 400 can't fit in a byte

bfabry22:01:10

@benzn: user=> (last (seq (byte-array (range 0 400)))) -113

ghadi22:01:23

it overflows before the cast

benzn22:01:32

that was obvious

benzn22:01:34

thank you!

benzn22:01:45

heh yes, exactly my thoughts simple_smile

bfabry22:01:45

hahaha, yes, very rubber duck

sstawecki22:01:07

Hi Clojurians, I would like to know the meaning of this symbol: ^ but is not easy to find on google. I saw a code like (fn [^Character x] ...

bfabry22:01:11

@sstawecki: it tells the clojure compiler the java type of the thing to the right, so that it doesn't have to perform reflection for java method calls where that type could change what method is called

pguillebert22:01:44

for your googling : it’s called “type hints"