Fork me on GitHub
#admin-announcements
<
2015-09-02
>
seantempesta07:09:42

Does anyone know how to check for authentication during a sente handshake (ajax-get-or-ws-handshake-fn) and reject the connection if the auth params aren’t kosher?

seantempesta07:09:35

Oh wait. It’s just a ring request. So I guess I can wrap my own handler to check the headers and only call that function if things are good…interesting.

meow18:09:24

I think I'm finally starting to really understand the functional way of coding:

(defn fibonacci-sequence-simple
  "Returns a lazy sequence."
  []
  (let [rules {:axiom [0]
               0 [0 1]
               1 [0]}
        xform (mapcat #(or (rules %) [%]))
        rfunc conj
        trans (partial transduce xform rfunc)
        axiom (trans [:axiom])
        gener #(trans %)]
    (iterate gener axiom)))

(comment
  (take 5 (fibonacci-sequence-simple))
  )

meow18:09:02

And I finally figured out how to really leverage transducers in these L-Systems I've been working on.

meow18:09:19

Taken out of context this code is not a good example of fully refined code.

meow18:09:33

It's just what got me over the hump.

meow18:09:55

Now I can make it even more generic...

meow18:09:45

Here's a start:

(defn replace-xform
  "Returns a transducer that will apply the replacement rules to a word."
  [rules]
  (mapcat #(or (rules %) [%])))

(defn system
  "Returns an L-system."
  ([xform]
   (system xform conj))
  ([xform rfunc]
   (let [trans (partial transduce xform rfunc)
         axiom (trans [:axiom])
         gener #(trans %)]
     (iterate gener axiom))))

(defn fibonacci-sequence-simple
  "Returns a lazy sequence of Fibonacci integers OEIS A003849."
  []
  (let [rules {:axiom [0]
               0 [0 1]
               1 [0]}
        xform (replace-xform rules)]
    (system xform)))

dnolen18:09:27

Opportunity grants available for Clojure/conj http://clojure-conj.org/opportunity

exupero18:09:26

Why is it necessary to wrap trans in a lambda before passing it to iterate?

meow18:09:24

(defn system
  "Returns an L-system."
  ([xform]
   (system xform conj))
  ([xform rfunc]
   (let [gener (partial transduce xform rfunc)
         axiom (gener [:axiom])]
     (iterate gener axiom))))

val_waeselynck19:09:16

A friend of mine has to benchmark a JVM with 1 terabytes of heap. He'd like to do it in Clojure. What algorithm would you suggest?

meow19:09:18

Now things are really shaping up nicely:

(defn replace-xform
  "Returns a transducer that will apply the replacement rules to a word."
  [rules]
  (map #(or (rules %) [%])))

(defn call-without-arguments-xform
  "Returns a transducer that will call any function without passing arguments."
  []
  (map #(if (fn? %) (%) %)))

(defn system
  "Returns an L-system."
  ([xform]
   (system xform conj))
  ([xform rfunc]
   (let [gener (partial transduce (comp xform cat) rfunc)
         axiom (gener [:axiom])]
     (iterate gener axiom))))

meow19:09:18

Which lets us start to do more interesting things, like this (which is still simple but should give a hint about where I'm heading):

(defn fibonacci-sequence-stochastic
  "Returns a lazy sequence of Fibonacci integers starting with 0 or 1."
  []
  (let [rules {:axiom #(vec [(rand-int 2)])
               0 [0 1]
               1 [0]}
        xform (comp (replace-xform rules)
                    (call-without-arguments-xform))]
    (system xform)))

meow19:09:30

And now I've finally dealt with that nagging feeling that I just wasn't taking advantage of transducers the way I should have. Hurray! simple_smile

meow19:09:04

These things can really generate some seriously big sequences quickly:

(-> (fibonacci-sequence-simple) (nth 35) count)
=> 24157817

luxbock21:09:59

@meow: nice work 👍

meow21:09:20

@luxbock: thank you! I am so much happier with the code now. Check it out: https://github.com/decomplect/ion/blob/master/src/ion/ergo/l_system.cljc

meow21:09:56

The old code is still in the file at the bottom so its easy to compare the two. The old code supported the general API that I wanted, but the implementation was kind of ugly and rigid. The new code is what I had in mind all along and struggled to flesh out. What I wanted was really a toolkit of useful functions and transducers and some reference implementations for building L-systems and some examples that did crazy things that push the boundaries to open people's eyes to what is possible. I think the new code is coming much closer to what I imagined and was hoping to achieve.

teamaverage22:09:08

Is there something in the observation that transducers seem to pull all the functions to the left and leave a single seq/coll at the right? Is that even correct? Just trying to get my head around them ...