Fork me on GitHub
#beginners
<
2017-11-11
>
Lucas Barbosa10:11:43

Guys, I am playing with some Java NIO in Clojure. I want to create a function that receives a multiplexor (`java.nio.channels.Selector`), performs a select, collects all the selected keys and clear the selector (for the next selections). I have a prototype, but it is looking procedural. Is there a more idiomatic way of doing this?

(defn select-now!
  [multiplexor]
  (.selectNow multiplexor)
  (let [selected-keys (.selectedKeys multiplexor)
        it (.iterator selected-keys)]
    (loop [result nil]
      (if-not (.hasNext it)
        result
        (let [nxt (.next it)]
          (.remove it)
          (recur (conj result nxt)))))))

Lucas Barbosa10:11:43

I also tried using the selected-keys set as a seq, since it is Iterable, and doing a .clear on the whole set afterwards, but the same channels kept being selected, even though their data was already read

Lucas Barbosa10:11:17

Looks like you really have to use the it.remove()call

Lucas Barbosa10:11:44

Well yeah, according to the documentation, the only way to remove a SelectionKey from the selected keys set is using the .remove method, either from the set or from the iterator

Lucas Barbosa13:11:20

Did anyone here ever had this experience where they try to run test-project on cider (emacs) and no test is run?

Lucas Barbosa13:11:38

“No assertions (or no tests) were run. Did you forget to use ‘is’ in your tests?"

Lucas Barbosa14:11:04

My project is called “papo”. The test file is “test/papo/messaging_test.clj”. I have tried reloading/refreshing the repl, but the test-project command does not work. The test-namespace command works, tho.

eggsyntax21:11:32

I haven't had that experience, but I wonder whether you need to add test as a src dir in project.clj.

Lucas Barbosa11:11:39

I see. I am going to try that when I have a chance! I thought that following Leiningen’s conventions was good enough 🧐

Lucas Barbosa10:11:36

Didn’t work 😞

Lucas Barbosa10:11:26

I added :source-paths ["src" "test"] to my project.clj file explicitly

Lucas Barbosa10:11:07

lein test works normally, cider-test-run-project-tests doesn't

boldaslove15623:11:42

I've just read about transducer and couldn't quite understand it

boldaslove15623:11:15

So from what I understand, it is a better way to apply function on a collection in terms of perfomance and modularity

boldaslove15623:11:10

Shouldn't we just use always use transducer whenever we can? Am I missing something?

seancorfield23:11:29

@boldaslove156 One benefit of transducers comes when you have a series of operations to do on a collection. If you thread a collection through a series of map/filter/etc, each operation creates a new intermediate sequence result. If you compose the transducer versions of those operations, then no intermediate sequences are created.