Fork me on GitHub
Tony Vo17:11:30

Hi, I ran into this issues and couldn't figure it out

Tony Vo17:11:02

here's the prod code:

(defn get-neighbors [[x y]]
	#{[(dec x) (inc y)] [x (inc y)] [(inc x) (inc y)]
		[(dec x) y] [(inc x) y]
		[(dec x) (dec y)] [x (dec y)] [(inc x) (dec y)]})

(defn get-all-neighbors [world]
	(mapcat #(get-neighbors %) world))

Tony Vo17:11:30

and here's my test code:

(context "get-all-neighbors"
  (it "should return all neighbors"
    (should= ([1 0] [-1 0] [1 1] [-1 -1] [1 -1] [-1 1] [0 -1] [0 1] [0 0] [1 0] [-1 0] [1 1] [-1 2] [0 2] [-1 1] [1 2]) (get-all-neighbors [world-of-two])))

Tony Vo17:11:53

with the world-of-two define as:

(def world-of-two #{[0 0] [0 1]})

Tony Vo17:11:32

and herew's the error I get: 1) Game of life get-all-neighbors should return all neighbors nth not supported on this type: PersistentHashSet java.lang.UnsupportedOperationException: nth not supported on this type: PersistentHashSet ... 2 stack levels elided ... at game_of_life$get_neighbors.invokeStatic(game_of_life.clj:4) at game_of_life$get_neighbors.invoke(game_of_life.clj:4) at game_of_life$get_all_neighbors$fn__10467.invoke(game_of_life.clj:10) ... 9 stack levels elided ... at game_of_life$get_all_neighbors.invokeStatic(game_of_life.clj:10) at game_of_life$get_all_neighbors.invoke(game_of_life.clj:9)


Should (get-all-neighbors [world-of-two]) perhaps be (get-all-neighbors world-of-two)? It seems to fail at destructuring.

Tony Vo17:11:20

damn .. you're right

Tony Vo17:11:26

this is the second time I ran into this issue

Tony Vo17:11:45

keep forgetting I don't need to use [] on method invocation

Tony Vo17:11:53



haha, I'm curious where that habit comes from?

Tony Vo17:11:44

actually it still fail after removing that []


I ran your code without the [] and it seems to work for me: ([1 0] [-1 0] [1 1] [-1 -1] [1 -1] [-1 1] [0 -1] [0 1] [0 0] [1 0] [-1 0] [1 1] [-1 2] [0 2] [-1 1] [1 2])

Tony Vo17:11:16

yeah ... from repl .. that's what I print out for me

Tony Vo17:11:26

but from lein spec, it failed


can you try quoting the list after should=?


so it becomes (should= '(

Tony Vo17:11:28

damn .. quote works


No problem! Remember that lists will get evaluated if you don't quote them.

Tony Vo17:11:07

what do list evaluated to?


Like a function

Tony Vo17:11:31

so what does this evaluate to ? -> ([1 0] [-1 0] [1 1] [-1 -1] [1 -1] [-1 1] [0 -1] [0 1] [0 0] [1 0] [-1 0] [1 1] [-1 2] [0 2] [-1 1] [1 2])


So ([1 0] [-1 0] ... will basically try to call the first vector as a function, with everything else as arguments.

Tony Vo17:11:37

without the quote?


yes. I get Wrong number of args (15)


but if you had ([1 0] 0) it would return 1, since you can use vectors as functions with the index to get as an argument.


Also, lists and vectors are treated as equal in Clojure (= [] '()), so it's more common to use vector literals in tests like that

Tony Vo17:11:46

thanks ... this is deep


hehe, it will come naturally soon enough [=

Tony Vo18:11:06

thanks, it's my fervent hope and dream

šŸ‘ 1

How would you conj vals in equal keys in different maps to create a single map?

(def a {:foo [123] :bar [345] :buzz [45]})
(def b {:foo [888] :bar [777]})

; result should be {:foo [123 888] :bar [345 777] :buzz [45]}
I feel thereā€™s some combo of reduce and map involved here?


merge-with into might do what you need -- depending on what other types of values you have in the map.


user=> (merge-with into a b)
{:foo [123 888], :bar [345 777], :buzz [45]}


ah I knew there was an elegant solution for this instead of monkeying around šŸ˜„ yeah thatā€™s what I was looking for thanks!


Note: if you have values in the map that are not vectors, merge-with into will fail -- because into can only merge vectors.

Thomas Tay20:11:52

Is there a corelib function that is the equivalent of F#s Basically, I want to do nil punning for my functions. This function would take as input my function F, and return a function that, if has input nil, returns nil. If input is non nil, pass it to my function F Is there such a wrapper function? Or is this not something that clojurians do? I come from a F# background so this is how I was approaching this problem. Background: I have a function that takes in a string and does some processing. I want to map this function over a Map (for a given set of keys), but it is possible the Map doesn't have the key, so it might return nil. In this case, I want to punt the nil down the line, so to speak.


Or (map #(and % (f %)) data) depending on whether you just want to apply one function or, via some->, short-circuit a chain of function calls.


(note: and won't work if you values can be false -- you'd need some-> in that case)

Thomas Tay21:11:27

Thanks guys, that was what I was looking for but somehow Google failed me

Michaƫl Salihi21:11:45

Hi! I tried to implement this following Java code in Clojure and I have a question about this keyword:

package hello;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
public class DBusHelloWorldServer implements DBusHelloWorldInterface
    private DBusConnection dbusConnection;
    public boolean isRemote()
        return false;
    private boolean stop=false;
    public String helloWorld(String name)
        return "Hello World : "+name;
    public void start()
                dbusConnection = DBusConnection.getConnection(DBusConnection.SESSION);
                dbusConnection.exportObject("/Main", this);
                while (!stop)
                        try {
                        } catch (Exception e) {}
        catch (DBusException e)
    public static void main(String[] args)
        new DBusHelloWorldServer().start();

Michaƫl Salihi21:11:42

In the try block, the exportObject take 2 arguments. The second one is referencing the current class with this. How can I interop?

Michaƫl Salihi21:11:34

Currently I had:

(defn start []
  (let [dbus-conn (. DBusConnection getConnection DBusConnection/SESSION)]
    (doto dbus-conn
        (.requestBusName "mon.premier.bus")
        (.exportObject "/Main" ?))))

Michaƫl Salihi21:11:19

I don't know what to replace the question mark with.

Michaƫl Salihi21:11:43

Do I have to use reify?


In the Java code, the this is passing the instance of class DBusHelloWorldServer from which the call to the exportObject method is being made. Presumably that because the dBusConnection object is going to do something with that object reference. Do you know what it will do with it? e.g. will it call some method on that object later? Save it in some data structure? Something else?


From within a normal Clojure function defined at the top level, there is no this enclosing object that makes sense to refer to. But if you create an object, as you suggest using reify , then inside of the reify you can specify an implementation for any number of methods you want, and all of their bodies will have access to a this reference for that object.

Michaƫl Salihi21:11:00

Maybe I must also implement the extended Interface with reify and reference it.

import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusInterface;
public interface DBusHelloWorldInterface extends DBusInterface
  @DBus.Description("Petit message de HelloWorld avec un nom en paramĆØtre")
  public String helloWorld(String nom);

Michaƫl Salihi22:11:59

So yes, the object passed as the second argument needs to implement an interface which extends DBusInterface . You advise me to use gen-class or reify to extend an interface? How can I do that?


Or rather, you would assign the return value from that (reify ...) expression to some variable somewhere, perhaps at the top level with (def dbus-hello-world-server (reify ...)), or (let [dbus-hello-world-server (reify ...)] ... code here that looks like code in your Java code's start() method ...)

šŸ‘ 1

The code that you would write that looks like the Java's start method you showed would use dbus-hello-world-server instead of this