help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to improve the readability of (any) LISP or any highlevel functi


From: w_a_x_man
Subject: Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ?
Date: Sat, 1 Jan 2011 23:14:47 -0800 (PST)
User-agent: G2/1.0

On Jan 1, 11:36 pm, Nathan <nbeen...@gmail.com> wrote:
> Well, to answer the question simply, this code will provide the
> functionality you've been asking about in Ruby.
>
> my_list = [1, 1, 1, 1, 2, 3, 3, 1, 1, 4, 5, 5, 5, 5]
> print my_list.reduce([]){|x, y|
>   if x.empty? or x[x.length-1] != y then

Instead of x[x.size-1], use x[-1] or x.last.

>     x + [y]
>   else
>     x
>   end
>
> }

Another way using reduce:

%w(a a a b c c a a d e e e).reduce([]){|a,x| a[-1]==x ? a : a<<x}
    ==>["a", "b", "c", "a", "d", "e"]

This assumes nils aren't allowed in the source array.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]