Skip to content

Instantly share code, notes, and snippets.

@cassiel
Created May 18, 2011 09:20
Show Gist options
  • Save cassiel/978270 to your computer and use it in GitHub Desktop.
Save cassiel/978270 to your computer and use it in GitHub Desktop.
first hack at the monome arc using clojure
; Our legacy Java OSC library:
(def transmitter (net.loadbang.osc.comms.UDPTransmitter. (java.net.InetAddress/getByName "localhost") 10332))
; Seriously non-idiomatic code. I'm only 20% of the way through the Joy of Clojure book...
(defn xmit [enc buffer]
"Transmit to encoder number enc, buffer is a sequence of integers."
(let [m (doto (net.loadbang.osc.data.Message. "/example/ring/map") (.addInteger enc))]
(doseq [n buffer] (.addInteger m n))
(.transmit transmitter m)
))
; There must be a better way to do this bit!
(def buf (map (fn [_] 5) (vec (range 64))))
(xmit 1 buf)
@cassiel
Copy link
Author

cassiel commented May 18, 2011

The trick, I guess, is to isolate calls to imperative legacy code behind Clojure's Agent machinery. That'll be the next step.

The only head-scratching moment was trying to figure out why it didn't work with "for", until I tried "doseq". Lazy evaluation, I'm guessing?

@samaaron
Copy link

Absolutely - lazy evaluation can be a killer if you're relying on an explicit evaluation of all the iterations. I've been bitten by this quite a few times - particularly using map. You can often fix it by using a doall which will force evaluation.

@cassiel
Copy link
Author

cassiel commented May 18, 2011

It's all starting to make sense. The namespace machinery is still pretty unfamiliar, and I'm not sure how some of the macros operate (the "#()" function form is confusing me), but otherwise it's not too dissimilar from generic functional programming.

@samaaron
Copy link

If you're happy with (fn [foo] (baz foo)), then you can just see #() as a less explicit (in terms of argument definition) shortcut form: #(baz %) where % stands for the argument.

If you need more arguments:

(fn [a b c] (baz a b c)) ;=equivalent to=> #(baz %1 %2 %3)

However, using the anonymous #() with more than one argument is usually non-idiomatic.

So just see #() as a shortcut for (fn [] ...)

@cassiel
Copy link
Author

cassiel commented May 18, 2011 via email

@cassiel
Copy link
Author

cassiel commented May 18, 2011

Oh - right - it's doing([...]) as an evaluation. My Lisp is still a bit shaky...

@samaaron
Copy link

Mine was completely non-existent before Clojure :-)

And yep, that one got me too. If you're looking for a fn that returns its argument take a look at identity.

@cassiel
Copy link
Author

cassiel commented May 18, 2011

Oh, and as for GitHub's code markup in gist comments - can't get that to work at all. Obviously.

@samaaron
Copy link

:-) It seems to be Markdown which means you can either surround your code with backticks for inline code such as this

or you can indent by over four spaces

for something
that spans multiple
lines

@cassiel
Copy link
Author

cassiel commented May 18, 2011

The Markdown seems to be seriously on the blink...

@samaaron
Copy link

Here's how I formatted my previous comment:

:-) It seems to be Markdown which means you can either surround your code with backticks for inline code `such as this`

or you can indent by over four spaces

    for something
    that spans multiple
    lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment