Randy, thanks for the reply. This has certainly cleared things up for
me.

I was only concerned about performance for an order of magnitude or
more; just checking I was on the right track, so to speak.

It seems I'll have to do a little research on transients now.

On Jul 25, 4:23 pm, Randy Hudson <randy_hud...@mac.com> wrote:
> This is a wholly appropriate use of reduce; it's the obvious function
> to use when you want to "accumulate" some calculation over a sequence.
>
> As you say, you can just produce a function that acts on the result,
> something like
>
> (defn fstep [c] (if (= c \<) pop #(conj % c))
>
> However, the obvious way of accumulating the application of these
> functions is ... reduce:
>
> (reduce #(%2 %1) [] (map fstep input-chars))
>
> You can gain some efficiency by using transient operations:
>
> (defn tstep [v c] (if (= c \<) (pop! v) (conj! v c)))   ;cf. step
>
> (defn run [cs] (persistent! (reduce tstep (transient []) cs))  ; cf.
> run-program
>
> On my machine this reduced the runtime of your 6e6 test about 20%
>
> On Jul 24, 6:16 pm, samnardoni <samnard...@googlemail.com> wrote:
>
>
>
> > I have a simple string (or list of characters to be precise) in a form
> > of: "1234<5678<<9".
>
> > I want to parse this string and end up with: "123569".
>
> > The "<" is essentially the same as a "backspace".
>
> > I managed to implement this fairly simply using the reduce function -
> > source:http://gist.github.com/489019.
>
> > Although it can be implemented easily using the reduce function, I was
> > concerned with the performance (especially with large inputs). As the
> > reduce function invokes the provided function with the previous
> > result, this list that is getting built is being passed around a huge
> > amount.
>
> > I'm not too sure as to what will be going on under the hood in this
> > sample. Whether the previous result of the function application is
> > really being passed around or not clear to me.
>
> > For the program, I know that when processing a character, I do not
> > need the previous result. All I need is the current character, and I
> > can return a function that acts on the result. I'm not sure if this is
> > simple to implement in a functional way.
>
> > So, I have 2 questions:
>
> > 1) Is this a correct use of the reduce function? (I know there is a
> > clue in the word "reduce", but it's also called "inject" and else in
> > other languages.)
>
> > 2) Is there a more efficient way of solving this?
>
> > I appreciate any replies (for such a trivial topic). Answers to this
> > will hopefully enlighten me to using functional languages more
> > effectively, or possibly to a more suitable high-order function.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to