Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Announcing Reg 0.4.0

Reply
Thread Tools

Announcing Reg 0.4.0

 
 
vikkous
Guest
Posts: n/a
 
      04-25-2005
Lyndon Samson wrote:
> It seems similar in spirit to JXPath for java which lets you use

XPath
> expressions to access objects, Hashs, Arrays, Maps etc which

otherwise
> is quite longwinded in java ( no snickering please ).
>
> http://jakarta.apache.org/comm ons/jxpath/


I took a skim thru this. It seems like XPath is all 'traversal of the
object graph' and thus very much like Reg's Object and Hash matchers.
The Array matcher does that and also matches regexp-like patterns
within arrays. Does XPath have that? I didn't see anything.

 
Reply With Quote
 
 
 
 
vikkous
Guest
Posts: n/a
 
      04-26-2005
itsme213 wrote:
> - can I bind variables to (parts of) matches


I guess what you're asking for is like the functionality of
backreferences, which aren't implemented yet. Binding a variable is
somewhat different, I guess but allows you to do the same sort of
thing. It could be implemented, not necessarily easily for global vars,
and maybe for others too using Binding.of_caller.

> - any convenient way to match "all" ... like r*(size..size)


Uhh... well, OB matches any single object and OBS matches 0 or more of
any object. One great thing about reg is that you can name your own
subexpressions if you happen to need a matcher that matches -[some long
reg that I dont want to type over and over], you can write:
foo=-[some long reg... etc]
and then use foo everywhere in your larger reg. This is a major
weakness of regexps, and was one of the great things about gema in
comparison.

For instance, modulo optimizations, the definitions of OB and OBS are:
OB=Object.reg
OBS=OB+0

 
Reply With Quote
 
 
 
 
vikkous
Guest
Posts: n/a
 
      04-26-2005
itsme213 said:
> - have you thought about the connection to duck typing?


Christian Neukirchen said:
> How does it compare to the ML style of argument matching, btw?


I think you're both talking about the same thing here; the ability to
dispatch to different methods depending on the types of method
parameters other than just the receiver. I have thought a good deal
about this, in fact, but not exactly in the context of Reg.

The best way to do this means extending the syntax, but here's a quick
and dirty way reg might be used to do it:

#warning! won't work yet; no substitutions in reg yet
module Scoundrels
def Scoundrels::bill(*args) #dispatcher for all the bills
send +[
-[Lockpick]>>:bill_the_picklock |
-[FakePassport,Cash,ShoePhone]>>:bill_the_spy |
-[Gun.reg+1, Knife]>>:bill_the_murderer |

-[Laptop,Oscilloscope.reg|CellPhone|Password.reg*5]>>:bill_the_hacker
].match(args.dup).first, args
end

#definitions of the various bills omitted
end

Then you could do:
Scoundrel.bill(LockPick.new) #invokes bill_the_picklock
Scoundrel.bill(Laptop.new, CellPhone.new) #invokes bill_the_hacker

Maybe this could be made easier. The syntax takes a little getting used
to, let us say.

This is great; people are coming up with angles I never thought of.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      04-26-2005
Hi --

On Tue, 26 Apr 2005, vikkous wrote:

> itsme213 said:
>> - have you thought about the connection to duck typing?

>
> Christian Neukirchen said:
>> How does it compare to the ML style of argument matching, btw?

>
> I think you're both talking about the same thing here; the ability to
> dispatch to different methods depending on the types of method
> parameters other than just the receiver. I have thought a good deal
> about this, in fact, but not exactly in the context of Reg.


I'm not sure about Christian, but I think itsme213 meant sort of the
opposite: how does your system relate to the duck-typing environment
of Ruby, where type != class (i.e., an object's capabilities and its
class's instance methods are not necessarily the same thing)? Are you
thinking of extending the system so that it could match, for example,
"an array of objects that respond to '[]'" (or something along those
lines)? One could imagine that being useful -- though on the other
hand, duck typing per se, as I understand it, really means just
requesting action from objects without a lot of preliminary querying
and measuring (whether it be is_a?, respond_to?, or whatever). So a
system like yours might be part of a fundamentally different way of
handling these things -- though respond_to?-awareness might be an nice
sort of middle ground.

At least I think that's what he meant, and even if not, it would be
interesting to hear your thoughts on it


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Saynatkari
Guest
Posts: n/a
 
      04-26-2005

Le 26/4/2005, "vikkous" <(E-Mail Removed)> a écrit:
>itsme213 said:
>> - have you thought about the connection to duck typing?

>
>Christian Neukirchen said:
>> How does it compare to the ML style of argument matching, btw?

>
>I think you're both talking about the same thing here; the ability to
>dispatch to different methods depending on the types of method
>parameters other than just the receiver. I have thought a good deal
>about this, in fact, but not exactly in the context of Reg.


I am not certain how flexible your framework is, but as a sidenote
the typical pattern matching (term used earlier) in functional
languages is well represented by things like this:

length [] = 0 -- Matches an empty list
length (xs) = 1 + length xs -- Matches&splits nonempty lists

length [1,2,3] -- returns 3
-- [1,2,3] -> [2,3] -> [3] -> []

Sorry. I just like writing Haskell

>The best way to do this means extending the syntax, but here's a quick
>and dirty way reg might be used to do it:
>
>#warning! won't work yet; no substitutions in reg yet
>module Scoundrels
> def Scoundrels::bill(*args) #dispatcher for all the bills
> send +[
> -[Lockpick]>>:bill_the_picklock |
> -[FakePassport,Cash,ShoePhone]>>:bill_the_spy |
> -[Gun.reg+1, Knife]>>:bill_the_murderer |
>
>-[Laptop,Oscilloscope.reg|CellPhone|Password.reg*5]>>:bill_the_hacker
> ].match(args.dup).first, args
> end
>
> #definitions of the various bills omitted
>end
>
>Then you could do:
> Scoundrel.bill(LockPick.new) #invokes bill_the_picklock
> Scoundrel.bill(Laptop.new, CellPhone.new) #invokes bill_the_hacker
>
>Maybe this could be made easier. The syntax takes a little getting used
>to, let us say.
>
>This is great; people are coming up with angles I never thought of.


E

--
template<typename duck>
void quack(duck& d) { d.quack(); }



 
Reply With Quote
 
Florian Groß
Guest
Posts: n/a
 
      04-26-2005
David A. Black wrote:

> extending the system so that it could match, for example,
> "an array of objects that respond to '[]'" (or something along those
> lines)?


I wonder if he could just support objects that implement === which would
give you Range and Module support and ruby-contract support for free.

But how is this related to your quote at all? ruby-contract offers a
Check::Quack[:message] adaptor that implements === via respond_to?() --
I wonder if it would be a good idea to define Symbol#=== which would be
used like this:

first = case obj
when :first then
obj.first
when :fetch then
obj.fetch(0)
when :at then
obj.at(0)
when :[] then
obj[0]
end

Or in combination with ruby-contract's signature():

class IO
def pretty_output(*objs)
objs.each do |obj|
puts obj.pretty
end
end

signature retty_output, :repeated => retty, :block => false
end



 
Reply With Quote
 
vikkous
Guest
Posts: n/a
 
      04-26-2005
> I'm not sure about Christian, but I think itsme213 meant
> sort of the opposite: how does your system relate to the
> duck-typing environment of Ruby, where type != class
> (i.e., an object's capabilities and its class's instance
> methods are not necessarily the same thing)? Are you
> thinking of extending the system so that it could match,
> for example, "an array of objects that respond to '[]'"


Oh! Ok, well that's easy, it would be something like:

+[ proceq{|x| x.respond_to? :[] }+1 ]

Being forced to use a proceq (gawd that's a terrible name) here is a
little ugly. Eventually, I want to be able to pass arguments to
property matchers, so the above could be:

+[ -{ [:respond_to?, :[]]=>true }+1 ]

> duck typing per se, as I understand it, really means just
> requesting action from objects without a lot of preliminary
> querying and measuring (whether it be is_a?, respond_to?,
> or whatever).


If you're doing something complicated, you occasionally have to
explicity request the type (whether duck- or class-) of objects you're
working with, in order to do the right thing with it. I know this isn't
polymorphic, but sometimes it is the right way...

 
Reply With Quote
 
vikkous
Guest
Posts: n/a
 
      04-26-2005
Florian Groß wrote:
> I wonder if he could just support objects that implement ===
> which would give you Range and Module support and ruby-
> contract support for free.


And in fact, this is exactly what I did. The idea is that users can
easily create their own (scalar) matchers by writing a matcher class
and implementing === in it. This also gives you automatic support for
all the library classes that have ===.

I'm going to have to check out this ruby-contract thing. Sounds very
interesting.

 
Reply With Quote
 
vikkous
Guest
Posts: n/a
 
      04-26-2005
Christian Neukirchen wrote:
> While I can see your intention, please don't do that. Often,
> people want to compare Symbols and Symbols (they are
> perfect for that). It is already very confusing that String
> !=== String (what is the most elegant way to case
> compare classes against classes, btw?).


The correct operator to use for comparison is ==, not ===. Despite the
similarity of names, === is for pattern-matching, not comparison. For
simple classes, pattern-matching and comparison are the same thing, but
when the receiver is a Regexp, or a Reg, things are different.

Btw,
:sym==:sym and Class==Class
work just fine.


> case obj.quack
> when :first
> when :fetch
> ...
> end
>
> would be nice to have, though.


hmmm... I haven't tried this, but....

require 'reg'

class Object
def quack
RegOr.new *methods
end
end

ought to do it

 
Reply With Quote
 
vikkous
Guest
Posts: n/a
 
      04-26-2005
vikkous said:
> require 'reg'
>
> class Object
> def quack
> RegOr.new *methods
> end
> end


Well, not quite. If Florian's Symbol#=== existed it would work,
otherwise it has to be like:


require 'reg'

class Object
def quack
RegOr.new *methods.map{|m| proceq{|x| x.respond_to? m } }
end
end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Announcing RouteMonitor 1.0 Beta pbopardi@gmail.com Cisco 2 07-08-2005 06:51 AM
Windows Reg Pro vs Tweak now Reg Cleaner vs Registry fix, vs RegCleaner jl Computer Support 3 05-31-2005 12:53 AM
Anyone know anything named DX? (was Re: Announcing PyCs) (was:Announcing PyCs, a new Python-like language on .Net) Mark Hahn Python 22 09-09-2004 12:14 PM
announcing routes to split network Philipp Specht Cisco 2 01-23-2004 11:05 AM
Announcing Limited Public Beta - XHTML 1.0 Strict Web Controls Peter L. ASP .Net 0 10-20-2003 08:14 PM



Advertisments