Hello,
I'm new to closure and functional programming and need some adviec/help with
my oldschool programming style.
I've written the following little program and hope someone could help me to
improve it.
The programm computes numbers through an iterative function , put the
numbers in a 'shift buffer' and computes some value from the entries
in that buffer.
I'm sure it's not optimal at all and could be improved a lot.
I've used a vector (and not a list) as buffer because I have to address and
use the elements using nth. Hope thats ok, but not sure of that.
I wish I could enter the new values at the start (left side) but found noe
efficient way to do it. With vector one should use conj to add new values
and
that could only be done from the right side of the buffer , right?
I'm sure there is a much better way to build such a 'shift buffer', any
ideas?
Another concern I have is the used of def's in the iteration loop.
Overwriting the x and the buffer may not be optimal, but I have no idea of
another (more functional) approch to that.
; defines my function
(defn myfunc [x]
(mod (+ (inc x) (rand-int 10) ) 10))
; creates buffer and sets initial values
(def buffer (vec (take 5 (repeat 0))) )
; creating a 'shift buffer': removes oldest entry and adds a new value at
the end
(defn in_buffer [buff x]
(conj (subvec buff 1) x))
; sets start value
(def x 0)
; iteration loop
(dorun(for [i (range 20)]
(do
(def x (myfunc x)) ; computes new value using myfunc and sets
new x value
(def buffer (in_buffer buffer x)); writes value into buffer and
sets new buffer
(println buffer) ;print buffer
(println (+ (nth buffer 1) (nth buffer 3)) )))) ; ;compute with
values from bufffer
Thank you or your help.
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.