How to call this method

R

Robert Dober

Hi list

the recent thread about all_indicies has somehow made me think that I
need a more general inject.
Here is my first shot on it:
http://pastie.org/597659
But how to call that beast, #inject_with is somehow not really what I like.
Actually I think #inject would be a nice name, but it is already taken.

Cheers
Robert
 
R

Robert Klemme

2009/8/28 Robert Dober said:
the recent thread about all_indicies has somehow made me think that I
need a more general inject.
Here is my first shot on it:
http://pastie.org/597659

I didn't know StopIteration yet. That's nice.

Your use case is a bit unclear to me. Also, I do not see why you need
to pass variables via the block. Somehow that looks suspiciously more
complex to me than it could be. A few ideas

class String
def indices rgx, idx = 0
r = []
idx = index rgx, idx

while idx
r << idx
idx = index rgx, idx.succ
end

r
end

def indices2 rgx, idx = 0
idx = index rgx, idx

while idx
yield idx
idx = index rgx, idx.succ
end

self
end

def indices3 rgx, idx = 0
scan rgx do
i = $`.length
yield i if i >= idx
end
end

def indices4 rgx, idx = 0
to_enum:)scan, rgx).inject [] do |r,|
i = $~.offset(0).first
yield i if i >= idx
end
end
end

irb(main):154:0* "abcabc".indices("a",1)
=> [3]
irb(main):155:0> "abcabc".indices2("a",1) {|i| p i}
"a3
bcabc".indices3("a",1) {|i| p i}
=> "abcabc"
irb(main):156:0> "abcabc".indices3("a",1) {|i| p i}
"abcabc".indices4("a",1) {|i| p i}
3
=> "abcabc"
irb(main):157:0> "abcabc".indices4("a",1) {|i| p i}
3
=> 3
irb(main):158:0>

Ok, I admit, I got carried away. :)
But how to call that beast, #inject_with is somehow not really what I like.
Actually I think #inject would be a nice name, but it is already taken.

Only few come to mind: roll, rotate, circulate

Kind regards

robert
 
R

Robert Dober

On Fri, Aug 28, 2009 at 3:17 PM, Robert
Klemme said:
Ok, I admit, I got carried away. :)
Yup, but I guess we all like to read your code. Well I do not care
about indicies, it was just the trigger for: "I want a thing that
passes its results into itself".

Maybe this usecase is more appealing?

http://pastie.org/597712

and of course a logical extension into Enumerable would be

http://pastie.org/597718

R.
 
B

Bertram Scharpf

Hi,

Am Freitag, 28. Aug 2009, 22:17:21 +0900 schrieb Robert Klemme:
I didn't know StopIteration yet. That's nice.

I didn't know it either. Is there anything that `break' cannot
do better?

Bertram
 
R

Rick DeNatale

J

James French

Hi,

I have a string literal in this form:

taxonomy =3D %q{
+AttributeControl
NMAttributeControl.*
+NetworkControl
NMNetworkControl*.*
+TimelineControl
NMTimelineControl*.*
+CurveControl
NMCurveControl*.*
*.*
}


Is it possible to somehow specify it such that there are (automatically) ne=
wline characters at the end of each line?

Cheers,
James
 
R

Robert Klemme

Am Freitag, 28. Aug 2009, 22:17:21 +0900 schrieb Robert Klemme:

I didn't know it either. Is there anything that `break' cannot
do better?

Multilevel exit like you can also do with catch throw (see Find.find).

Kind regards

robert
 
Q

Quintus

James said:
Hi,

I have a string literal in this form:

taxonomy = %q{
+AttributeControl
NMAttributeControl.*
+NetworkControl
NMNetworkControl*.*
+TimelineControl
NMTimelineControl*.*
+CurveControl
NMCurveControl*.*
*.*
}


Is it possible to somehow specify it such that there are (automatically) newline characters at the end of each line?

Cheers,
James
Hi,

there ARE already Newlines in your string:

puts taxonomy.scan(/\n/).size #=> 10

What's your problem?

Marvin
 
J

James French

Boy do I feel stupid. taxonomy.include?('\n') was returning false. Wrong qu=
oting... Cheers for the nudge.

-----Original Message-----
From: Quintus [mailto:[email protected]]
Sent: 28 August 2009 18:01
To: ruby-talk ML
Subject: Re: new lines in string literals
=20
James said:
Hi,

I have a string literal in this form:

taxonomy =3D %q{
+AttributeControl
NMAttributeControl.*
+NetworkControl
NMNetworkControl*.*
+TimelineControl
NMTimelineControl*.*
+CurveControl
NMCurveControl*.*
*.*
}


Is it possible to somehow specify it such that there are
(automatically) newline characters at the end of each line?
Cheers,
James
Hi,
=20
there ARE already Newlines in your string:
=20
puts taxonomy.scan(/\n/).size #=3D> 10
=20
What's your problem?
=20
Marvin
 
R

Robert Dober

Multilevel exit like you can also do with catch throw (see Find.find).
Yep, by using "raise StopIteration" I make my intent a little bit
clearer and it is less fragile
in case I am getting embedded into other blox. But that is not the
case here, I agree.

I think it might also having been built for Enumerator#next

loop do
...
an_enum.next # might raise StopIteration
...
end

HTH
R.
 
B

Bertram Scharpf

Hi,

Am Samstag, 29. Aug 2009, 01:55:26 +0900 schrieb Robert Klemme:
Multilevel exit

No, it exits just one level:

loop { puts "x" ; loop { puts "y" ; raise StopIteration } }

Bertram
 
R

Robert Dober

=A0loop { puts "x" ; loop { puts "y" ; raise StopIteration } }
Wait a second, break exits a block,
raise gets to the next,exception handler, in your case the one in the
inner loop; thus you need to do the following

loop do
puts "x"
lambda{ puts "y"; raise StopIteration}[]
end

to see what is going on

In other words you did

begin
while true
puts "x"
begin
while true
puts "y"
raise SI
end
rescue SI
end
end
rescue SI
end


R.
 
R

Robert Dober

Sorry for re-phrasing my question, but it just came back to me, from
where I had this idea:
Clojure's iterate

Do you think iterate is a good name for this?

Cheers
Robert
 
R

Robert Klemme

Hi,

Am Samstag, 29. Aug 2009, 01:55:26 +0900 schrieb Robert Klemme:

No, it exits just one level:

loop { puts "x" ; loop { puts "y" ; raise StopIteration } }

Well, we're both right: it won't exit multiple *loop* levels but it will
exit multiple *stack frames*, i.e. method and block calls (not
containing loops of course). That's what I had in mind.

Your point demonstrates though that catch throw might be superior
because you explicitly declare the point of return.

Kind regards

robert
 
R

Robert Dober

On 28.08.2009 19:43, Bertram Scharpf wrote:
Your point demonstrates though that catch throw might be superior because
you explicitly declare the point of return.
I just think they are two different tools for two different tasks.

raise SI: end an iteration / loop
throw/catch raise * : Whatever seems appropriate, non local exit
being a possibility

It depends heavily on the *.

I therefore reconsider my confirmation that SI is for non local exit
in general, Betram's examples have made that quite clear.

Cheers
R.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top