Fork me on GitHub
#clojure
<
2016-11-15
>
fabrao03:11:41

hello, how do I call Java method with something like this

public final ExpectBuilder withInputs(InputStream... inputs) {
        this.inputs = inputs;
        return this;
    } 
(.withInputs (ExpectBuilder.) (.getInputStream canal) (.getExtInputStream canal))
is not working, why?

nickmbailey03:11:31

@fabrao you want to pass an array of input streams

tianshu05:11:02

Can i define a function with meta data in defmacro?

rmoehn07:11:12

@doglooksgood Do you want to set the metadata on the function itself or on the var that holds the function?

rmoehn07:11:07

(defn ^{:ethics :utilitarian} blu [x] x) sets metadata on the var:

rmoehn07:11:42

(:ethics (meta (var blu))) ;=> :utilitarian

rmoehn07:11:52

Here's an ugly way of setting metadata on the var. Not sure if there's a better one.

rmoehn07:11:48

(Click to expand the code to its full nine lines.) If you really want metadata on the fn itself:

p-himik08:11:40

Is there a reasonable way to reuse keys list in clojure.spec/keys? I.e. I want to register two specs - one with an optional set of keys [::a ::b ::c] and another with a required set of keys [::a ::b ::c ::d]. I'm not very familiar with macro, so all my attempts resulted in Assert failed: all keys must be namespace-qualified keywords.

mpenet08:11:07

You can use s/and or s/merge

p-himik08:11:46

Not sure I follow. How that would allow me to use :req-un for one spec and :opt-un for another on the same set of keys?

bfabry09:11:07

@p-himik you couldn't. you'd need a macro for that

p-himik09:11:15

Is it possible to write such macro that would expand every argument I pass to s/keys in 3-4 lines?

bfabry09:11:33

I'm not really the macro wiz to ask, but I'd suggest "maybe, but you probably shouldn't"

joost-diepenmaat09:11:11

@doglooksgood I would go for something like

abdullahibra09:11:56

are there any plans for tensorFlow + clojure ?

tianshu09:11:43

@joost-diepenmaat Good! I'll have a try!

tianshu09:11:48

thanks for help!

gtaylor10:11:44

Hey, any chance someone can help me work out how to do something in clojure?

gtaylor10:11:03

I have a few repeated when statements

gtaylor10:11:08

with the same test

gtaylor10:11:23

so I was wanting to refactor out the code repetition

gtaylor10:11:53

I think (partial when myCond) gets me what I am after

gtaylor10:11:17

but I am not sure of the clojury way of getting my hands on that function

mgrbyte10:11:20

I don't think you can use partial with when, since the later is a macro

mgrbyte10:11:04

Might need to wrap your expression in a defn, then call that.

gtaylor10:11:47

ah so I cant just treat macros like functions

bfabry11:11:16

no, macros break composition, it's one of the reasons to prefer functions over macros

yonatanel13:11:00

Is there anything like the threading macro that repeats the same function a number of times? Like a reduce with no collection

gfredericks13:11:34

@yonatanel repeatedly or iterate?

yonatanel13:11:48

@gfredericks iterate is what I was looking for. Thanks!

danielstockton14:11:01

Is there any advantage to using the ->DefRecord constructor, instead of (DefRecord.) ?

danielstockton14:11:28

Why would I prefer the first option, when it means typing an extra character?

mpenet14:11:35

I guess it's nicer with apply/comp/partial & co, other than that no difference I think

Alex Miller (Clojure team)15:11:33

Because it's portable to ClojureScript

Alex Miller (Clojure team)15:11:21

Also, the fact that defrecord creates a Java class is an implementation detail - you should always use the constructor function imo

danielstockton15:11:17

I somehow knew that was the case but forgot the reasoning

borkdude16:11:17

how do I type hint a byte-array result?

ghadi16:11:31

(that is so strange because it's the jvm internal descriptor for the type)

borkdude16:11:08

or ^bytes right?

borkdude16:11:35

(just found it myself)

borkdude16:11:17

are defrecords cheaper/faster to serialize than normal hashmaps?

ghadi16:11:46

oh yeah, bytes is a shortcut.

borkdude17:11:56

I guess it doesn’t really matter (the serialization)

Alex Miller (Clojure team)17:11:32

they are a class with fields and all that is optimized by the JVM. a PHM is a tree structure that has to be traversed to do anything.

Alex Miller (Clojure team)17:11:53

careful with ^bytes too - not going to work on vars (which evaluate meta)

borkdude17:11:18

@alexmiller yet you can associate extra values to it like you can with a hash-map… does that impact the performance?

Alex Miller (Clojure team)17:11:32

yes - those are stored in a field that is a map

borkdude17:11:39

(I just ^bytes to get rid of warning)

borkdude17:11:42

ah, I suspected that

borkdude17:11:33

deftypes are not serializable, why not?

Alex Miller (Clojure team)17:11:44

what deftypes do is up to you

Alex Miller (Clojure team)17:11:54

you can make them serializable if you want

borkdude17:11:25

so deftype comes more close to implementing your own class in Java and defrecord is just a performance optimization for hash-maps that have a known structure?

Alex Miller (Clojure team)17:11:46

I’d say it captures a common pattern with maps

Alex Miller (Clojure team)17:11:14

that has both semantic value and in some cases performance value (but it’s more subtle than just better/worse)

borkdude17:11:43

does it matter if you add type annotations like this: (defrecord MyThing [^String a ^String b])

borkdude17:11:18

I guess only if you have certain methods you implement that use interop on Strings?

Alex Miller (Clojure team)17:11:45

those field types can be used by interop calls inside methods defined within the defrecord

Alex Miller (Clojure team)17:11:11

but it’s not used as the field type in the generated class - those will be Object

Alex Miller (Clojure team)17:11:30

I would refer you to http://clojure.org/reference/datatypes for the “why” of records and deftypes

borkdude17:11:49

defstruct is kind of deprecated right?

mpenet18:11:24

Doesn't type hint will at least be applied to the constructor? I seem to recall having seen that used as poor mans validation (with the positional constructor or Foo. at least)

Alex Miller (Clojure team)19:11:03

the constructor function internally takes only Object, long, or double (like any other Clojure function)

borkdude20:11:37

any suggestions how to optimize for GC overhead by many intermediate small collections (as I’m typing this, I’m thinking transducers maybe)

bfabry20:11:16

transducers certainly sound like the quickest and easiest way to optimise that

borkdude20:11:21

I’m using group-by, so the whole thing has to exist in memory at some point..

hugesandwich20:11:00

beyond transducers, it would be helpful to know what you're doing on a higher level and when you are encountering intermediate collections. You might have mentioned something earlier.

hugesandwich20:11:41

I find as a general guideline for performance, I do a lot of things that aren't necessarily good practices for general use in clojure, but helpful for tight performance optimization, often GC related

hugesandwich20:11:27

One thing you can do for instance is to use transients if you are constantly mutating something, so as long as you are aware of the dangers of doing so and how to limit the exposure of it

hugesandwich20:11:00

object pooling is another decent option sometimes, so you can avoid re-allocations of both collections themselves and potentially the objects that are inside the collections

borkdude20:11:40

roughly: I’m iterating over a nested map and produce pairs of [String String]. Then I’m grouping on the first string, based a on function -> {String k [[String k1 String v1]}. Then I’m transforming that into something like {:foo k, :bar k1, :bazes [all the vs]}.

hugesandwich20:11:45

I also try to avoid laziness if I can in these situations (memory permitting), and I also favor using reduce directly over some of the things that internally call reduce or do other things to be more general (ex: support laziness). i.e. instead of using some higher order thing, you use reduce. You can combine that approach with transients quite easily for some savings and better performance, and of course with transducers and using things like into

liamd21:11:05

any good libraries for doing colored ascii output and terminal interfaces? couldn't find any after some quick googling

borkdude21:11:48

are optimizations for small vectors already in Clojure? or are there things like tuples? I came across this commit: https://github.com/clojure/clojure/commit/ae7acfeecda1e70cdba96bfa189b451ec999de2e

dvorme21:11:27

@liamd [io.aviso.ansi]

liamd21:11:49

@dvorme in your experience with this would it work to make a game with text graphics?

liamd21:11:22

ah, the column formatting looks like what i need

borkdude21:11:29

I am tempted to use MapEntry as a 2-tuple

dvorme22:11:44

@liamd I’ve only used it for log file coloring. Boot also uses it, so it will be maintained (or at least Boot’s fork of it). But I haven’t looked at its support for cursor positioning ansi sequences and so forth. Depending on what you are trying to do, that might or might not be important.

hugesandwich22:11:41

@liamd Firstly, this is a complicated answer. so sorry for the length. I also looked for the same thing. I don't have much spare time but I've been interested in terminal libraries for the purposes of building a BBS from scratch using netty to build out a sockets server/telnet/ssh support. I came to the conclusion in my terminal lib searches that I need to use something in Java, call to a FFI (C, C++), or I should write my own (leaning towards latter for various reasons). Pretty much 99% of terminal-related and parsing-related libraries out there are wrong, including most of what is in C, C++, Go, Rust, Python (particularly miserable), etc. Aviso.ansi is really about just coloring text for the most part, with some limited ability to parse escape sequences. I've looked over the code and while it does what it claims to do, it is by no means comprehensive or correct for parsing escape sequences, even limited to just colors (even notes inside the code about restricting this for cursive). Anyway, I suggest not to use it and definitely don't use any parser that doesn't understand that there's more to color than fg/bg SGR in 16 colors (ex: RGB support, iCE colors, palette differences between systems/client terminals (DOS, Amiga, Atari, Mac, Xterm, etc.). Also avoid anything that flat out converts the color for you as the intermediate representation is what you want since the client terminal needs to interpret the meaning. The one and only parser I can find in Clojure land that is even remotely correct is inside asciinema. The main thing about this implementation is it is based off an existing parser spec pretty directly from http://vt100.net. The overall problem is that most libraries ignore the full range of escape sequences, edge cases, transitions, encoding issues, etc. but this one covers most of them. This implementation has a few problems (extra iterations, memory usage, etc. in the name of being more functional) and could probably be optimized, but at least is pretty clean (mostly why it is not as optimized I think). There's various other issues I won't go into, but mostly related to my usage. https://github.com/asciinema/asciinema-player/blob/master/src/asciinema/player/vt.cljc

liamd22:11:07

thanks for the thorough answer, i'll take a look

liamd22:11:18

here i thought i was making my life simpler by not using graphics haha

hugesandwich22:11:09

Not at all. You need to represent the terminal as a virtual terminal. That said, if you don't really care about supporting a true terminal and just want to barf out basic colors around text, this will point you in the right direction.

hugesandwich22:11:32

For example there's a library in js you could easily port the meat of to clojure that does exactly this - chalk.js I think. It's also not correct, but for a simple purpose it is good enough that it would produce escape sequences that will work in any modern terminal. It just doesn't support the specs correctly, but 99% of people probably don't care for this purpose.

cschep22:11:02

if you’re looking for a cool looking terminal output as an aesthetic, you could probably emulate it in html/css quicker

cschep22:11:06

and your game would be more accessible 🙂

hugesandwich22:11:13

So if that makes sense, it depends if you just want to do what chalk is doing here (or aviso.ansi) and spitting out some sequences around text, or you want to actually produce terminal graphics that can support cursor moves, animation, baud-rate like stuff, etc.

bfabry22:11:40

is this a sensible pattern?

concurrent-jobs 5
        pipeline (async/pipeline-blocking
                   concurrent-jobs
                   (async/chan (async/dropping-buffer 1))
                   (mapcat #(do (run-maxwell-replay % replay-maxwell) []))
                   job-params-chan)]
(<!! pipeline)

bfabry22:11:00

ie, I don't care about the results of these tasks, but I do want to have 5 running in p// until they're done

bfabry22:11:28

the dropping-buffer 1 and mapcat do in particular seem a bit odd

bfabry22:11:43

though considering how much power I'm getting out of those few little lines of code I probably shouldn't complain

brycecovert23:11:54

@hugesandwich I started looking at pixie recently, which has a cool ffi interface. I started creating a wrapper for ncurses - https://github.com/brycecovert/bewitch . was toying around with creating a roguelike with it

adambros23:11:50

anyone having trouble using lein-cloverage and getting a java.lang.IllegalStateException: Attempting to call unbound fn: #‘clojure.tools.reader.impl.utils/tagged-literal?