[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: What about a 'series' type?

seebs

6/7/2007 12:57:00 PM

In message <cd08f63ca2ad51e567d4288410f593da@ruby-forum.com>, Peter Marsh writes:
>But this isn't any good if you want a arithmetic [3,5,7,9] or geometric
>[2,6,18,54] series. So I suggest this:
>
>arithmetic_series = Arith.new(First_Term,Common_Difference)
>
>aritmetic_series[9] will then generate the 10th term of the sequence (0
>being the first to be consistent) and arithmetic_series[0..9] will
>return an array with the first to tenth terms. The same goes for a
>geometric series.
>
>gemometric_series = Geometric.new(First_Term,Common_Ratio)
>
>Now, there may be other ways to achieve this, but I think this way is
>nicer.

These seem like special cases of Series.new([Start Terms], Proc).

(Consider Fibbonacci sequences, for instance, or Pascal's Triangle.)

-s

11 Answers

SonOfLilit

6/7/2007 1:17:00 PM

0

Actually, the syntax:

a = Series.new(8) {|i| 5*i**3}

a.collect{|i| i + 1}.select{|i| i % 2 == 0} # i.e. a is an enumerable object

is quite cool and might be useful in some cases. The only problem I'm
trying to solve is that of providing syntax for persistence (e.g. try
to implement a series of Fibonacci numbers with the iterative
algorithm using this Series syntax). Perhaps an optional hash argument
that is used to give initial values to instance variables?

With persistence, this could be a very useful idiom in many cases, and
maybe even deserves to be stdlib.

Aur

On 6/7/07, Peter Seebach <seebs@seebs.net> wrote:
> In message <cd08f63ca2ad51e567d4288410f593da@ruby-forum.com>, Peter Marsh writes:
> >But this isn't any good if you want a arithmetic [3,5,7,9] or geometric
> >[2,6,18,54] series. So I suggest this:
> >
> >arithmetic_series = Arith.new(First_Term,Common_Difference)
> >
> >aritmetic_series[9] will then generate the 10th term of the sequence (0
> >being the first to be consistent) and arithmetic_series[0..9] will
> >return an array with the first to tenth terms. The same goes for a
> >geometric series.
> >
> >gemometric_series = Geometric.new(First_Term,Common_Ratio)
> >
> >Now, there may be other ways to achieve this, but I think this way is
> >nicer.
>
> These seem like special cases of Series.new([Start Terms], Proc).
>
> (Consider Fibbonacci sequences, for instance, or Pascal's Triangle.)
>
> -s
>
>

Peter Marsh

6/7/2007 2:48:00 PM

0

SonOfLilit wrote:
> Actually, the syntax:
>
> a = Series.new(8) {|i| 5*i**3}
>
> a.collect{|i| i + 1}.select{|i| i % 2 == 0} # i.e. a is an enumerable
> object
>
> is quite cool and might be useful in some cases. The only problem I'm
> trying to solve is that of providing syntax for persistence (e.g. try
> to implement a series of Fibonacci numbers with the iterative
> algorithm using this Series syntax). Perhaps an optional hash argument
> that is used to give initial values to instance variables?
>
> With persistence, this could be a very useful idiom in many cases, and
> maybe even deserves to be stdlib.
>
> Aur

The two examples I gave are fairly specific, I didn't really think about
that. However, I still think a 'series' type would be useful, even if
the implimentation is different to my example.

Taking prime numbers as an example using the current 'prime' class you
have to use:

require 'mathn'

primes = Prime.new

etc

With a seires type you could do something like this:

primes = Series.new(Starting_value,Some_block_to_define_serires)

primes[0] = 2
primes[0..2]
>> [2,3,5]

Giving definate advantages, I feel.

--
Posted via http://www.ruby-....

Peter Marsh

6/7/2007 2:52:00 PM

0


> primes = Series.new(Starting_value,Some_block_to_define_serires)

Just wanted to be a bit clearer about this, the second argument is a
block which can generate nth term in a series. This would probally take
a while for primes, but if it were recursive then it would be easier...

--
Posted via http://www.ruby-....

Robert Klemme

6/7/2007 4:32:00 PM

0

On 07.06.2007 16:52, Peter Marsh wrote:
>> primes = Series.new(Starting_value,Some_block_to_define_serires)
>
> Just wanted to be a bit clearer about this, the second argument is a
> block which can generate nth term in a series. This would probally take
> a while for primes, but if it were recursive then it would be easier...

IMHO for a series it would be more natural to let the block calculate
a[n+1] from a[n] wouldn't it? Of course, for Fibonacci this would only
work if you allow for multiple arguments.

Something like

#!ruby
class Serial
include Enumerable

def initialize(*init, &f)
@init = init
@f = f
end

def each(&b)
a = b.arity
current = @init
loop do
b[*current[0 ... a]]
current = Array(@f[*current])
end
self
end
end

s1 = Serial.new 0 do |x| x+1 end
s1.each {|x| p x; break if x > 10}

puts

s2 = Serial.new 0,1 do |a,b| [b,a+b] end
s2.each {|x| p x; break if x > 40}


Kind regards

robert

Robert Dober

6/7/2007 7:42:00 PM

0

On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 07.06.2007 16:52, Peter Marsh wrote:
> >> primes = Series.new(Starting_value,Some_block_to_define_serires)
> >
> > Just wanted to be a bit clearer about this, the second argument is a
> > block which can generate nth term in a series. This would probally take
> > a while for primes, but if it were recursive then it would be easier...
>
> IMHO for a series it would be more natural to let the block calculate
> a[n+1] from a[n] wouldn't it? Of course, for Fibonacci this would only
> work if you allow for multiple arguments.
>
> Something like
>
> #!ruby
> class Serial
> include Enumerable
>
> def initialize(*init, &f)
> @init = init
> @f = f
> end
>
> def each(&b)
> a = b.arity
> current = @init
> loop do
> b[*current[0 ... a]]
> current = Array(@f[*current])
> end
> self
> end
> end
>
> s1 = Serial.new 0 do |x| x+1 end
> s1.each {|x| p x; break if x > 10}
>
> puts
>
> s2 = Serial.new 0,1 do |a,b| [b,a+b] end
> s2.each {|x| p x; break if x > 40}
>

Robert I hope you do not mind my fantasy about/over/at??? your theme.

class Serial
include Enumerable

def initialize(*init, &f)
@init = init
@f = f
@arity = f.arity
end

def get_some( some = nil, &b)
@current = @init.dup
if some && b.nil? then
(0...some).inject([]) do |acc,|
compute_next
acc << @current.first
end
else
a = b.arity
loop do
break if some && ( some -= 1 ) < 0
b[*@current.first( a )]
compute_next
end
self
end
end

private
def compute_next
@current += Array( @f[*@current] )
@current = @current.last @arity
end
end

s1 = Serial.new 0 do |x| x+1 end
s1.get_some {|x| p x; break if x > 10}
puts "-"*42
puts s1.get_some(6)

puts "-"*42
f = Serial.new 1, 1 do |x, y| x + y end
f.get_some(6){ |x,| puts x }

puts "-"*42
a = Serial.new 1, 1, 1 do |x, y, z| x + y + z end
puts a.get_some(6)

Cheers
Robert



--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

fREW

6/7/2007 8:18:00 PM

0

On 6/7/07, Robert Dober <robert.dober@gmail.com> wrote:
> On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> > On 07.06.2007 16:52, Peter Marsh wrote:
> > >> primes = Series.new(Starting_value,Some_block_to_define_serires)
> > >
> > > Just wanted to be a bit clearer about this, the second argument is a
> > > block which can generate nth term in a series. This would probally take
> > > a while for primes, but if it were recursive then it would be easier...
> >
> > IMHO for a series it would be more natural to let the block calculate
> > a[n+1] from a[n] wouldn't it? Of course, for Fibonacci this would only
> > work if you allow for multiple arguments.
> >
> > Something like
> >
> > #!ruby
> > class Serial
> > include Enumerable
> >
> > def initialize(*init, &f)
> > @init = init
> > @f = f
> > end
> >
> > def each(&b)
> > a = b.arity
> > current = @init
> > loop do
> > b[*current[0 ... a]]
> > current = Array(@f[*current])
> > end
> > self
> > end
> > end
> >
> > s1 = Serial.new 0 do |x| x+1 end
> > s1.each {|x| p x; break if x > 10}
> >
> > puts
> >
> > s2 = Serial.new 0,1 do |a,b| [b,a+b] end
> > s2.each {|x| p x; break if x > 40}
> >
>
> Robert I hope you do not mind my fantasy about/over/at??? your theme.
>
> class Serial
> include Enumerable
>
> def initialize(*init, &f)
> @init = init
> @f = f
> @arity = f.arity
> end
>
> def get_some( some = nil, &b)
> @current = @init.dup
> if some && b.nil? then
> (0...some).inject([]) do |acc,|
> compute_next
> acc << @current.first
> end
> else
> a = b.arity
> loop do
> break if some && ( some -= 1 ) < 0
> b[*@current.first( a )]
> compute_next
> end
> self
> end
> end
>
> private
> def compute_next
> @current += Array( @f[*@current] )
> @current = @current.last @arity
> end
> end
>
> s1 = Serial.new 0 do |x| x+1 end
> s1.get_some {|x| p x; break if x > 10}
> puts "-"*42
> puts s1.get_some(6)
>
> puts "-"*42
> f = Serial.new 1, 1 do |x, y| x + y end
> f.get_some(6){ |x,| puts x }
>
> puts "-"*42
> a = Serial.new 1, 1, 1 do |x, y, z| x + y + z end
> puts a.get_some(6)
>
> Cheers
> Robert
>
>
>
> --
> You see things; and you say Why?
> But I dream things that never were; and I say Why not?
> -- George Bernard Shaw
>
>

Isn't ruby cool?

--
-fREW

Robert Klemme

6/7/2007 8:41:00 PM

0

On 07.06.2007 21:41, Robert Dober wrote:
> On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>
> Robert I hope you do not mind my fantasy about/over/at??? your theme.

Not at all. I had thought about the limit myself but did not want to
bother implementing it. Two remarks about your code:

You include Enumerable but do not provide #each which is required.

You use instance variables for the iteration which is a bad thing
because this needless restricts usability (namely in the light of
multiple threads). Array and other's also do not store the iteration
state in instance variables but in local variables in method #each. You
can try it with something like this which would not work if instance
variables would be used for storing iteration state

a=(1..10).to_a
2.times {|i| Thread.new(i) {|j| a.each {|x| puts "[#{j}-#{x}]"; sleep 0.5}}}

Kind regards

robert

Robert Dober

6/7/2007 9:12:00 PM

0

On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 07.06.2007 21:41, Robert Dober wrote:
> > On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >
> > Robert I hope you do not mind my fantasy about/over/at??? your theme.
>
> Not at all. I had thought about the limit myself but did not want to
> bother implementing it. Two remarks about your code:
>
> You include Enumerable but do not provide #each which is required.
Sure, bad error; that was a leftover of your code, I do not really
want to include it.
>
> You use instance variables for the iteration which is a bad thing
> because this needless restricts usability (namely in the light of
> multiple threads).
I am not sure I understand this point, but we can get rid of that by
expanding compute_next, but this will make the code much less readable
:(
The whole beast is not thread safe at all I guess, even without the
instance var.

It would be a nice challenge to make it thread safe, I guess we would
need to cache values and synchronize the computation part, sounds
*very* expansive, time and memory wise; probably not worth it .
>Array and other's also do not store the iteration
> state in instance variables but in local variables in method #each.
Would that not be for performance reasons?
> You
> can try it with something like this which would not work if instance
> variables would be used for storing iteration state
>
> a=(1..10).to_a
> 2.times {|i| Thread.new(i) {|j| a.each {|x| puts "[#{j}-#{x}]"; sleep 0.5}}}
I guess I do not know enough to understand this :(
How is concurrent access to the method not a problem with local variables?
However it would be sufficient to synchronize the access to the method
only and not the access to the ivar. Is that what you are worried
about?

Cheers
Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Klemme

6/8/2007 10:24:00 AM

0

On 07.06.2007 23:12, Robert Dober wrote:
> On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 07.06.2007 21:41, Robert Dober wrote:
>> > On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>> >
>> > Robert I hope you do not mind my fantasy about/over/at??? your theme.
>>
>> Not at all. I had thought about the limit myself but did not want to
>> bother implementing it. Two remarks about your code:
>>
>> You include Enumerable but do not provide #each which is required.
> Sure, bad error; that was a leftover of your code, I do not really
> want to include it.
>>
>> You use instance variables for the iteration which is a bad thing
>> because this needless restricts usability (namely in the light of
>> multiple threads).
> I am not sure I understand this point, but we can get rid of that by
> expanding compute_next, but this will make the code much less readable
> :(

You can as well add parameters and return values to compute_next.

> The whole beast is not thread safe at all I guess, even without the
> instance var.

Oh, it's perfectly thread safe if you change the use of instance variables.

> It would be a nice challenge to make it thread safe, I guess we would
> need to cache values and synchronize the computation part, sounds
> *very* expansive, time and memory wise; probably not worth it .

Caching is only needed if you want to make it faster or more efficient.
But for a general implementation I would not do it as it can have all
sorts of unwanted side effects.

>> Array and other's also do not store the iteration
>> state in instance variables but in local variables in method #each.
> Would that not be for performance reasons?

That's another advantage.

>> You
>> can try it with something like this which would not work if instance
>> variables would be used for storing iteration state
>>
>> a=(1..10).to_a
>> 2.times {|i| Thread.new(i) {|j| a.each {|x| puts "[#{j}-#{x}]"; sleep
>> 0.5}}}
> I guess I do not know enough to understand this :(
> How is concurrent access to the method not a problem with local variables?

Because then there is iteration state per method invocation. Compare
these two variants of #each:

class Foo
# a must be an Array
def initialize(a) @a=a.dup end

# thread safe
def each_1
for i in 0...@a.size
yield @a[i]
end
self
end

# not thread safe
def each_2
for @i in 0...@a.size
yield @a[@i]
end
self
end
end

Now, think about what happens if two threads invoke #each_1 and #each_2.

> However it would be sufficient to synchronize the access to the method
> only and not the access to the ivar.

That would work but it would limit usability - and there is no need to
do that.

Kind regards

robert

Robert Dober

6/8/2007 12:27:00 PM

0

On 6/8/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 07.06.2007 23:12, Robert Dober wrote:
> > On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >> On 07.06.2007 21:41, Robert Dober wrote:
> >> > On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >> >

First of all thx for your time but I am not a quick learner :(

Obviously I am (or rather was )missing something very basic and if I
understand correctly that very basic is that method_invocations are on
copies of the local data, in each thread, is this *really* true? Well
you already showed with an example.

Hopefully I can put this into work tonight, gotta go back to work
here, hopefully this weeks Ruby Quiz will be boooring, please James ;)

Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw