Fork me on GitHub
#beginners
<
2017-01-26
>
eslachance03:01:01

Welp, I am back after 2 months of not touching any clojure code.

eslachance03:01:10

This is going to be fun

7h3kk1d03:01:31

That's the attitude!

eslachance03:01:51

So, I have an atom in which I have a :handler key, which contains the following:

{:MESSAGE_CREATE [#object[dtbot.core$handle_message 0x6f8ac246 "dtbot.core$handle_message@6f8ac246"]],
 :READY [#object[dtbot.core$on_ready 0xe8edfc6 "dtbot.core$on_ready@e8edfc6"]]}

eslachance03:01:59

I'm trying to dynamically call one of these functions

eslachance03:01:06

(get-in @session [:handlers :MESSAGE_CREATE]) works fine

eslachance03:01:39

But this does not: (get-in @session [:handlers (:t msg)]) (assume the :t key contains the string MESSAGE_CREATE)

eslachance03:01:41

what am I missing?

eslachance03:01:15

And yes @7h3kk1d I'm super happy to come back to it after months of mucking around with node.js ugh

eslachance03:01:16

I'm sure it's a super simple answer I'm looking for but my usual one-on-one expert is probably raiding right now 😞

7h3kk1d03:01:10

Does it just return nil?

7h3kk1d03:01:22

(I'm not very experienced) btw

eslachance03:01:31

yes, it's nil

7h3kk1d03:01:06

When you say the string MESSAGE_CREATE is it a string or a symbol?

7h3kk1d03:01:36

because here you used a symbol (get-in @session [:handlers :MESSAGE_CREATE])

eslachance03:01:47

so how do I convert a string to a symbol

7h3kk1d03:01:16

I think it's just (symbol "string")

7h3kk1d03:01:34

But you're typecasting it to a str there so maybe try just removing the str function.

eslachance03:01:46

yes sorry that was just a test

eslachance03:01:56

hmm

(get-in @session [:handlers (symbol (:t msg))])
=> nil

eslachance03:01:30

wait it's a keyword I need no?

7h3kk1d03:01:49

so (keyword "string")

7h3kk1d03:01:02

Glad I almost helped 🙂

eslachance03:01:10

I'm still missing some of that terminology. You did help with that, thank you 😄

eslachance03:01:53

Alright quick question. I can do a condition like (nil? func) but how do I "invert" it? in JS I would do if(!null) for example

eslachance03:01:05

Clojure docs are always confusing >.<

7h3kk1d03:01:41

(not (nil? func))

eslachance03:01:05

so (not (nil? func) ; do stuff) ?

7h3kk1d03:01:52

or that evaluates to the not

7h3kk1d03:01:58

if you want to do stuff you need to put it in an if

7h3kk1d03:01:02

you can also use if-not

eslachance03:01:34

(if-not (nil? func) func(argument)) then.

7h3kk1d03:01:59

(if-not (nil? func) (func argument))

eslachance03:01:07

excuse my JS leftovers

eslachance04:01:38

darn I'm getting an Arity exception... that function is defined as accepting 2 arguments

eslachance04:01:46

(defn handle-message [session message]
  (if (= (get message :content) "ping")
    (dithcord/send-message session "pong!" (get message :channel_id))
    )
  )

eslachance04:01:21

It's being printed as [#object[dtbot.core$handle_message 0x7f29d7f3 "dtbot.core$handle_message@7f29d7f3"]] in the repl so it looks like the right one

eslachance04:01:29

hmm. wait... that's an object.

eslachance04:01:23

Sometimes I'm smarter than my own good - I prepared for having more than one handler for incoming events and forgot ^_^

poooogles14:01:46

Sure I'm missing something here, but this isn't the behaviour I'm after...

user=> (instance? com.blah.evt.Events$Foo foo)
(instance? com.blah.evt.Events$Foo foo)
true
user=> (instance? com.blah.evt.Events foo)
(instance? com.blah.evt.Events foo)
false

poooogles14:01:15

I'm after the second one to also be true, any pointers?

dominicm14:01:07

@poooogles Because of class nesting, they aren't really related (other than sharing a hierarchy). No idea if there's a way to find the outer class for something.

dominicm14:01:06

Ignore my use of the word super, crossed wires.

Alex Miller (Clojure team)14:01:30

I mean (supers foo) might tell you something interesting

Alex Miller (Clojure team)14:01:19

You can probably ask those questions via reflection but they're probably not good questions to ask :)

dominicm14:01:43

Reflection answer seems to be this one: http://stackoverflow.com/a/15265900

poooogles15:01:08

@dominicm thanks. Not the answer I was looking for 😆

jcronk18:01:33

Hi, I have an 800Mb XML file that could contain illegal characters and needs to be sanitized. So I can (filter #(<= 32 (int %) 256) file-contents) but slurping the file gives me a heap space error…am I stuck writing the sanitized file back out, then reading it back in to parse as XML, or are there other options?

zirmite02:01:11

jcronk: perhaps this would be useful for this: https://github.com/thebusby/iota

donaldball18:01:23

Sorry, I’m a little stuck on 800Mb XML file

donaldball18:01:04

You probably want to work the bytes using good ol’ http://java.io InputStream and OutputStream

donaldball18:01:38

Read a buffer, write the good bytes, rinse, repeat until finished

donaldball18:01:47

You may yet have heap problems actually holding the parse tree in memory tho

jcronk18:01:18

I know 😕 It’s an XML dump from a mainframe.

jcronk19:01:32

data.xml is pretty good, though. I was able to use it to process a clean file that was almost 1Gb, and it does great. I’m just having problems here because of the invalid characters and not knowing Clojure or Java especially well.

manutter5119:01:53

@jcronk Are you trying to clean out all the characters outside the legal range or only if they appear as text elements inside xml tags?

manutter5119:01:56

If you don’t need to know whether the character is inside an xml tag, maybe just treat it like a raw binary file for filtering purposes: read a block, filter it, write a block.

jcronk19:01:49

Yeah, it’s any characters at all, regardless of where they are. I guess using streams and pretending the file is binary is the best approach. Thanks. 🙂