Fork me on GitHub
#beginners
<
2015-12-06
>
wei07:12:26

question- how would I translate the following into clojure?

socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

  @Override
  public void call(Object... args) {
    socket.emit("foo", "hi");
    socket.disconnect();
  }
}
  

wei07:12:43

I’m thinking reify or proxy

trancehime08:12:35

proxy might work better

agile_geek10:12:29

I would prefer reify to proxy in most cases. See this thread and answer by @kushalp for why: http://stackoverflow.com/questions/5821892/why-should-i-use-reify-instead-of-proxy-in-clojure

roelof12:12:27

How can I read the first part of this [:debet {:account "cash", :amount 100}] so I need :debet as output

eggsyntax14:12:38

I’m not sure I understand your question, but if you literally want to get :debet out of it, it’s (first [:debet {:account "cash", :amount 100}]). If that’s not what you’re asking, please clarify.

rcanepa14:12:16

Is there some good practice on primary key’s type?. I have seen in many examples the use of an UUID instead of a auto incremental integer. Is this related to concurrency?

rcanepa14:12:02

If anyone is interested, I found a good explanation here: https://github.com/clojure-cookbook/clojure-cookbook/blob/master/01_primitive-data/1-24_uuids.asciidoc. In short, they provide “uniqueness” along multiple databases and tables, which can be pretty useful in some situations. Also, they hide information about the amount of information that resides inside a table (I am not sure why this could be a good thing).

sveri14:12:37

@rcanepa: If you choose to use auto increment ints or bigints attacker may guess the id's of the next entry. This opens for some attacks that are not possible by choosing random ids like UUIDs

sveri14:12:18

They might also guess the ids of existing database entries

rcanepa15:12:08

@sveri: Thanks for the input on this. I wasn’t aware of those risks.

sveri15:12:12

@rcanepa: Yea, np. It's a rat race. I think such common knowledge should be bundled into libs or frameworks so that newcomers don't have to deal with it immediately.

roelof18:12:13

@eggsyntax: you are right . That is working but when I have this

(def transaction  [ {:debet :account "cash" :amount 100 }} { :credit {:account "bank" :amount 100}} ]
)
then this is not working : (first (first transaction) )) I see then [:debet {:account "cash", :amount 100}] instead of just :debet

eggsyntax18:12:07

@roelof: That's not valid clj, though. Is the second closing squiggly brace on the first map a typo? Or did you intent an opening squiggly before the first :account?

roelof18:12:51

@eggsyntax: I think the last ) is a left-over from closing the function

roelof18:12:41

so it will be (first (first transaction) )

eggsyntax18:12:05

No, I mean in the def. Will you reshape that into valid clojure & repost?

roelof19:12:53

oke, sorry : then it will be (def transaction [ {:debet {:account "cash" :amount 100 } } { :credit {:account "bank" :amount 100}} ]

roelof19:12:30

maybe that is the problem

eggsyntax19:12:50

OK. Note that in your original post you had [:debet ...], and here you have {:debet ...}. That is, you've changed the vector to a map.

eggsyntax19:12:02

If it were a vector, (first (first transaction)) would work fine.

eggsyntax19:12:15

If you want it to be a map, it gets a bit more awkward, but you could do (first (keys (first transation))).

eggsyntax19:12:54

It feels to me like maybe you haven't quite clarified in your mind what you're trying to represent.

roelof19:12:03

Thanks the keys are working

roelof19:12:32

@eggsyntax: im experimenting what is the best way to represent

eggsyntax19:12:22

If you want the transaction to consist, by definition, of a debit and a credit, I'd consider something like

(def transaction {:debit {:account "cash" :amount 100}
                  :credit {:account "bank" :amount 100}})

eggsyntax19:12:08

Then you don't have to pull out the name :debit, because you know it'll contain a debit, so you can just do (:debit transaction) to get the info about the debit.

roelof19:12:59

@thanks , I will try your suggestion

kyle_schmidt21:12:11

I am attempting to create an app that I upload a file to and read its contents before doing other things. I am getting the parameters back from my uploaded file and I am unsure how I would read the uploaded file from ring.middleware.multipart-params. The parameter map of a sample uploaded file is as follows: {:file {:size 70, :tempfile #<File /tmp/ring-multipart-4755435914544293619.tmp>, :content-type text/csv, :filename request_temp.csv}} 1. Do I use the :tempfile key or the :filename key? 2. Is there an extra step that I am missing?

codonnell23:12:00

@kyle_schmidt: it looks to me like you should use the :tempfile key, as its value is a File object.

kyle_schmidt23:12:30

@codonnell: I was trying that and it wasn't cooperating. I'll keep experimenting.

rcanepa23:12:35

Does make any sense to use protocols to define CRUD operations on different models/types? Or I will gain nothing because I will just move complexity from one place to another?… I am learning about multimethods and protocols and I am trying to come up with a situation in which they may useful.

rcanepa23:12:23

In other words, what question do I need to ask myself to evaluate if polymorphism will be useful in a particular case?