Fork me on GitHub
#clojure
<
2017-01-16
>
escherize00:01:11

Is there something like defn but for a memoized function?

escherize00:01:40

I wrote

(defmacro defmemo [name args & body]
  `(def ~name
     (memoize (fn [~@args] ~@body))))
but would be curious to see something that's more through šŸ™‚

grounded_sage02:01:21

Is it possible to do a string replace using a symbol as the pattern to match?

joshjones05:01:27

@grounded_sage you mean where the name of the symbol is the pattern itself? i don't quite see the value, but yes you can convert a symbol to a string, and a string to a re-pattern:

(clojure.string/replace "123abc789" (-> abc quote str re-pattern) "456")
=> "123456789"

danielstockton07:01:26

If i'm implementing a tree with Node and Leaf deftypes, how can I write methods that access .-keys for each type of node without reflection? Should I write getter functions for these fields and use those?

mavbozo09:01:48

oracle jdk SE download page provides 2 version: 8u111 and 8u112. which I should download?

mavbozo09:01:24

what is oracle jdk even / odd numbered version means? is there any convention?

borkdude09:01:43

Is there a core function that partitions a collection in two parts based on a predicate?

borkdude09:01:57

I know group-by, but I donā€™t need a map

shooodooken09:01:30

@mavbozo try #beginners channel

mavbozo09:01:56

@shooodooken thank you. I will ask there

mpenet09:01:14

well it's not exactly "partition"

mpenet09:01:30

partition-by tho šŸ™‚

mpenet09:01:47

guess it depends on the kind of check you need to apply, might not be always suitable

borkdude09:01:05

I already looked at both but they donā€™t seem to do what I want. Group-by comes closest

borkdude09:01:43

Also group-by is less ambiguous and more flexible of course

borkdude09:01:08

E.g. what comes first, all results for which the pred is false or true? With group-by thatā€™s clear without looking at the docs

borkdude09:01:31

Anyway, happy blue monday!

rickmoynihan11:01:55

danielstockton: could probably use a protocolā€¦ it sound like youā€™re wanting some of the features of defrecord though- so might want to consider using that; otherwise youā€™ll need to implement those bits you need on your deftype

martinklepsch11:01:25

Letā€™s say I have some (def x (reify ....)) in a namespace ā€” what would the "fully qualified class nameā€ of x be? Tried $x but that dinā€™t work...

bronsa11:01:59

@martinklepsch reify creates an anonymous instance

bronsa11:01:05

the classname is unspecified

bronsa11:01:30

(as in, it's some munged random name you can't rely on)

martinklepsch11:01:47

Oh, ok šŸ˜„ would I use deftype then?

bronsa11:01:55

if you need a named class, yes

danielstockton11:01:58

I use a protocol @rickmoynihan which avoids reflection on methods but not fields apparently, which is why i'm wondering if i should just wrap the field in a lookup method.

martinklepsch11:01:21

Sometimes I think Iā€™m already quite proficient interacting with Java stuff but then things like this come along šŸ˜„

danielstockton11:01:27

defrecord is another option, i want it to be as performant as possible though

martinklepsch11:01:38

(where you have to pass the fully qualified name of a class as a string to something)

bronsa11:01:35

@danielstockton there's no perf difference between deftypes and defrecords -- a defrecord is just a deftype that implements a bunch of default interfaces to behave like a map

bronsa11:01:06

if you type-hint, (.-some-field ^ClassName the-type-or-record-instance) it will not use any reflection

danielstockton11:01:53

I know @bronsa but i don't know the class at compile time, just trying to remove the reflection warning

danielstockton11:01:56

im worried i may want different implementations of some of those default interfaces that defrecord already implements

bronsa11:01:55

how do you not know the class at compile time if you're doing field access? do you have multiple deftypes with the same field names? in that case a protocol function to access them should be the right abstraction. if using kw access using defrecords is out of the question

danielstockton11:01:23

yeah, multiple deftypes with same fields...protocol function it is then

martinklepsch11:01:20

so with a (deftype MyThing []) in com.my-abc would the fqn be com.my_abc$MyThing?

bronsa11:01:46

no, com.my_abc.MyThing

martinklepsch11:01:24

ha, should have just tried that. Remember something about inner classes and thought I need the `ā€¦ thanks @bronsa

martinklepsch11:01:53

(and sorry for asking really boring questions haha)

martinklepsch12:01:26

Another Java related question: How would I rewrite this to use reify ā€” and should I at all?

(proxy [KeyedPooledObjectFactory] []
                         (makeObject [k] (DefaultPooledObject. (make k)))
                         (destroyObject [k ^PooledObject o] (destroy k (.getObject o)))
                         (validateObject [k ^PooledObject o] (validate k (.getObject o)))
                         (activateObject [k ^PooledObject o] (activate k (.getObject o)))
                         (passivateObject [k ^PooledObject o] (passivate k (.getObject o))))

martinklepsch12:01:17

I tried something like this but it complains about wrong return types for destroyObject (should be void is Object)

(reify KeyedPooledObjectFactory
  (makeObject [_ k] (DefaultPooledObject. (mk-session k)))
  (destroyObject [_ k ^PooledObject o] nil)
  (validateObject [_ k ^PooledObject o] nil)
  (activateObject [_ k ^PooledObject o] nil)
  (passivateObject [_ k ^PooledObject o] nil))

bronsa12:01:18

(reify KeyedPooledObjectFactory (makeObject [this k] (DefaultPooledObject. (make k))) ..)

bronsa12:01:37

type hint the argvec

bronsa12:01:43

^void [_ k ..]

martinklepsch12:01:01

hm, same result for this

(destroyObject ^void [_ k ^PooledObject o] (destroy k (.getObject o)))

bronsa12:01:46

gah, try (^void destroyObject [..] ..)

martinklepsch12:01:25

that did the trick!

abhir00p12:01:39

I am trying to integrate jmx metrics into my Clojure app

abhir00p12:01:03

Is it possible to set the type of a custom mbean

abhir00p12:01:18

something like java.lang:type=Memory

leov13:01:24

um. do we have a rails analog presence function in clojure?

(presence nil) ; nil
(presence ""); nil
(presence "hi"); "hi"

(presence []); nil etc
to be used like so: (some->> city presence (str ", and the city is:"))

leov13:01:11

or in env var config (or (presence config-var "default"))

stijn13:01:21

(defn presence [x] (when (seq x) x))
=> #'user/presence
(presence nil)
=> nil
(presence "")
=> nil
(presence "hi")
=> "hi"
(presence [])
=> nil

stijn13:01:01

it satisfies the requirements, but probably you want it to work for Number too šŸ˜›

danielgrosse13:01:35

How can I compose a regex out of variables?

stijn13:01:50

re-pattern creates a regex from a string that represents the pattern

dottedmag13:01:30

Does subvec keep a reference to whole original vector or only to the relevant part?

dottedmag13:01:44

That is, can I subvec a needed small piece of data out of a huge vector and don't be worried about retaining the whole huge vector in memory?

urbanslug13:01:42

Is there something I can call to check CPU utilization on a clojure function?

dottedmag13:01:12

Answering myself after some REPL experimentation: subvec does retain whole original vector in memory.

danielstockton14:01:08

interesting @dottedmag, i guess it has to as there is no easy clue which parts can be GCed

danielstockton14:01:13

You think this is something specific to subvec? I'd expect it to be the same with conj for example.

dottedmag14:01:09

Yes, it's specific, because conj does not create a smaller structure.

dottedmag14:01:08

So, I suppose it's because subvec is supposed to be very fast, so there is no overhead of creating a new persistent data structure.

dottedmag14:01:54

So, if one wants to pick a subrange out of a large vector and discard it, it ought to be done via (vec (subvec largestuff N M)), which will build a new persistent data structure.

bronsa14:01:21

no it won't

bronsa14:01:33

the only way is not to use subvec at all

bronsa14:01:05

or to do like (vec (seq (subvec ov n m)))

dottedmag14:01:05

Interesting.

dottedmag14:01:46

I was wrong.

bronsa14:01:48

but at this point just do (into [] (comp (drop n) (take (- m n))) ov)

bronsa14:01:55

which will probably be faster (unless n is really big)

leov14:01:33

@stijn , thanks, that actually works, however it's tricky to understand that it works like that) also it needs to work only for {}, [] and strings, numbers are ok)

dottedmag14:01:07

@bronsa Why doesn't (into [] (subvec ov m n)) work? Is it due to fast path in into which says ah, it's a vector too, let's just not copy anything?

bronsa14:01:31

(into []) would work

bronsa14:01:36

it's vec that wouldn't

bronsa14:01:53

well defining "work" as in "would create a new DS w/o the old elements"

bronsa14:01:06

vec has the fast path check

dottedmag14:01:40

Interesting. Let me try again, (into []) kept the whole data structure in memory for me.

dottedmag14:01:54

Yes, PEBKAC.

dottedmag14:01:38

After (into [] (subvec a 1 100)) (ns-unmap 'user 'a) (System/gc) memory is released.

danielstockton14:01:14

@dottedmag side question, how are you measuring the memory consumption of these things?

dottedmag14:01:54

There's probably CLI utility as well, but this one works well for experimentation.

stijn14:01:44

@leov: it works for strings because a string is a sequence of characters, if it is empty, it will return nil

dottedmag14:01:45

@danielstockton Yes. It's a GUI tool which comes with JVM and creates nifty memory/CPU/whatever graphs.

sveri14:01:04

@dottedmag @danielstockton I dont want to be nitpicking here, but in case someone else reads this: The java mission control comes with the oracle JDK and is bound to the oracle license whereas it is not part of the OpenJDK.

dottedmag14:01:44

It uses JMX to collect metrics, which is in OpenJDK, so there are probably other similar clients.

sveri14:01:31

Yea, most probably šŸ™‚

isaac15:01:59

There has any way re-extend some protocol methods on record?

leov16:01:18

@stijn , turns out this function is named not-empty in clojure world

octahedrion17:01:05

in Spec, how do I say that a map must have :req keys and that 2 of their vals depend on eachother ? like, one of the map's vals must be a collection and another one must in a number in the range (count coll)

dottedmag17:01:46

@octo221: (s/and (s/keys ...) your-predicate-on-map)

octahedrion17:01:54

@dottedmag yep that's done it - thanks so much!

erikcw17:01:37

Iā€™m trying to connect to AWS Athena over JDBC (clojure.java.jdbc). Iā€™m getting an error saying Exception in thread "main" com.amazonaws.athena.jdbc.NotImplementedException: Method Connection.prepareStatement is not yet implemented, compiling:(/private/var/folders/h1/pt7t6l9s2bq2zblx4v2mmnnh0000gn/T/form-init4382409266245799902.clj:1:125) Is there a way to query jdbc without using prepareStatement? Iā€™m using (query db [ā€SELECTā€¦ā€] {:row-fn :cost})

brabster19:01:50

@erikcw Looking at the source, one way might be to try and use db-do-commands, looks like it uses Statement https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc.clj#L760

brabster19:01:37

Saying that though, maybe can't use that with a select because it uses executeBatch

brabster19:01:52

but the source can guide you on doing what you need to with plain Java interop?

seancorfield19:01:17

I would assume the Athena JDBC library provides a way to create a PreparedStatement object via some subclass? If so, you can pass that into query as part of the vector, so it doesnā€™t need to call prepareStatement itself.

seancorfield19:01:08

@brabster Yeah, youā€™re going to run foul of what you can / canā€™t do with the various underlying JDBC execute functions.

brabster19:01:14

This might be helpful... someone already did Athena from Clojure https://github.com/mikeplavsky/athena-cmd/blob/master/src/athena_cmd/core.clj

brabster19:01:35

@seancorfield good idea on looking for other ways to create a preparedstatement but I don't see anything in the jar that looks like one. I'm guessing it's just not implemented yet at all

brabster19:01:21

I would mercilessly copy from mikeplavsky's example and star him up on Github šŸ™‚

seancorfield19:01:40

Looks like it calls .createStatement rather than .prepareStatement so that would be the path Iā€™d suggest ā€” but that looks like a plain Statement rather than a PreparedStatement so Iā€™m not sure java.jdbc will recognize it.

seancorfield19:01:56

(since it specifically tests instance? PreparedStatement)

tjtolton19:01:39

does the rich hickey/ cognitect crew have anything to say on the subject of release management? I'm moving into the role of "release manager"/"please help us fix our release process" person at my company. I'm looking for role models for modern release management practices.

pesterhazy19:01:21

not sure if they're about release management per se, but in the vicinity for sure

mtnygard19:01:31

@tjtolton If you havenā€™t read Jez Humble & Dave Farleyā€™s book ā€œContinuous Deliveryā€ that would be a great place to start.

tjtolton20:01:56

I'll look into that book and your blog, thanks @mtnygard, and @pesterhazy!

nfisher20:01:17

@tjtolton one of the key principles is ā€œbuild-onceā€ deploy everywhere. The Google SRE book also has a few chapters you might find helpful although donā€™t get overwhelmed by it. Pick the things you think will fit and benefit your org most.

tjtolton20:01:52

google sre book

pesterhazy20:01:53

seconding michael's book, it's def worth reading

brabster21:01:27

What's the easiest way to pick up a build time env var and pack it up into my jar so that I can use it in a function?

brabster21:01:46

I'm using leiningen to build. Trying to build a URL using my Travis build number

hiredman21:01:59

characters are not strings

hiredman21:01:22

"1" is a string, \1 is a character

hiredman21:01:45

a java.lang.String and a java.lang.Character

greg21:01:51

thanksā€¦ oddly the char coercion function only operates on numbers

hiredman21:01:05

it operates on ints

greg21:01:27

is there a general function to return the type of a value? typeof or something?

hiredman21:01:35

because java characters need the range

hiredman21:01:40

type or class

greg21:01:13

thank you thank you

brabster21:01:45

It's just going to be write the build number to a file during build and read that at runtime isn't it :)

hiredman21:01:07

it is pretty common to generate a file containing build related information as part of the build

hiredman21:01:35

whatever tooling you are using may already do that in some way