Hi list
Lab419::functional contains an implementation of Perl6's junctions now
http://rubyforge.org/frs/?group_id=3824&release_id=33503
Enjoy
Junctions
A pure Ruby implementation of Junctions as planned for Perl6. [1]
Junctions are composite expressions that reply to methods as would
their elements.
E.g.
any(1, 2, 3) > 2 --> true
[ 1, 3, 5 ].all.odd? --> true
none( 1 ).zero? --> true
all() == nil --> true
any() == nil --> false
A popular use case is
if any( "--help", "-?", "-h" ) == param then
usage
Junctions can be constructed by either
* using the module methods any, all, none and one of Lab419::Junctions
or
* by using junction methods of enumerables (1..3).all > 0
or
* by including Lab419::Junctions
For details please see the references below.
[1] http://en.wikipedia.org/wiki/Perl_6#Junctions
http://search.cpan.org/dist/Perl6-Junction/lib/Perl6/Junction.pm
http://www.perl.com/pub/a/2003/07/29/exegesis6.html?page=4
http://www.programmersheaven.com/2/Perl6-FAQ-Junctions
|
|
0
|
|
|
|
Reply
|
robert.dober (2193)
|
4/17/2009 9:17:23 PM |
|
Hello,
Robert Dober wrote:
> Lab419::functional contains an implementation of Perl6's junctions now
It works fine, but I received warning message with -v option of ruby.
$ cat t.rb
require 'lab419/functional/junctions'
$ /usr/local/trunk/bin/ruby -v t.rb
ruby 1.9.2dev (2009-04-17) [i686-linux]
/usr/local/trunk/lib/ruby/site_ruby/1.9.1/lab419/functional/junctions/base.rb:83:
warning: method redefined; discarding old one?
/usr/local/trunk/lib/ruby/site_ruby/1.9.1/lab419/functional/junctions/base.rb:91:
warning: method redefined; discarding old none?
regards
Masaki Suketa
|
|
0
|
|
|
|
Reply
|
masaki.suketa (80)
|
4/18/2009 1:04:08 AM
|
|
On Sat, Apr 18, 2009 at 3:04 AM, Masaki Suketa
<masaki.suketa@nifty.ne.jp> wrote:
> Hello,
>
> Robert Dober wrote:
>
>> Lab419::functional contains an implementation of Perl6's junctions now
>
> It works fine, but I received warning message with -v option of ruby.
>
> $ cat t.rb
> require 'lab419/functional/junctions'
>
> $ /usr/local/trunk/bin/ruby -v t.rb
> ruby 1.9.2dev (2009-04-17) [i686-linux]
> /usr/local/trunk/lib/ruby/site_ruby/1.9.1/lab419/functional/junctions/bas=
e.rb:83:
> warning: method redefined; discarding old one?
> /usr/local/trunk/lib/ruby/site_ruby/1.9.1/lab419/functional/junctions/bas=
e.rb:91:
> warning: method redefined; discarding old none?
>
> =A0regards
> =A0Masaki Suketa
>
Thank you for reporting this, I will look into it right now.
Robert
|
|
0
|
|
|
|
Reply
|
robert.dober (2193)
|
4/18/2009 10:25:27 AM
|
|
> Enjoy
>
> Junctions
>
> A pure Ruby implementation of Junctions as planned for Perl6. [1]
Pretty sweet!
Did you look into how complex it would be to implement autothreading, like in Perl 6, too?
:)
|
|
0
|
|
|
|
Reply
|
dvdplm (25)
|
4/18/2009 2:58:27 PM
|
|
On Sat, Apr 18, 2009 at 4:58 PM, David Palm <dvdplm@gmail.com> wrote:
>> Enjoy
>>
>> Junctions
>>
>> A pure Ruby implementation of Junctions as planned for Perl6. [1]
>
> Pretty sweet!
>
> Did you look into how complex it would be to implement autothreading, lik=
e in Perl 6, too?
>
> :)
No but that's a nice idea.
Cheers
R.
Si tu veux construire un bateau ...
Ne rassemble pas des hommes pour aller chercher du bois, pr=E9parer des
outils, r=E9partir les t=E2ches, all=E9ger le travail=85 mais enseigne aux
gens la nostalgie de l=92infini de la mer.
If you want to build a ship, don=92t herd people together to collect
wood and don=92t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.
--
Antoine de Saint-Exup=E9ry
|
|
0
|
|
|
|
Reply
|
robert.dober (2193)
|
4/18/2009 3:04:10 PM
|
|
On Sat, Apr 18, 2009 at 8:58 AM, David Palm <dvdplm@gmail.com> wrote:
>> Enjoy
>>
>> Junctions
>>
>> A pure Ruby implementation of Junctions as planned for Perl6. [1]
>
> Pretty sweet!
>
> Did you look into how complex it would be to implement autothreading, like in Perl 6, too?
>
You should check out Ara T. Howard's threadify gem -- very similar to
autothreading.
Blessings,
TwP
|
|
0
|
|
|
|
Reply
|
tim.pease (469)
|
4/18/2009 3:38:02 PM
|
|
Robert Dober wrote:
> fixed
> http://rubyforge.org/frs/?group_id=3824
Thank you. It works fine without warning message
when running with -v option of ruby.
Regards
Masaki Suketa
|
|
0
|
|
|
|
Reply
|
masaki.suketa (80)
|
4/18/2009 6:12:52 PM
|
|
On Apr 17, 2009, at 11:17 PM, Robert Dober wrote:
> E.g.
>
> any(1, 2, 3) > 2 --> true
> [ 1, 3, 5 ].all.odd? --> true
> none( 1 ).zero? --> true
> all() == nil --> true
> any() == nil --> false
The examples you gave are equivalent to
[ 1, 2, 3 ].any? { |e| e > 2 }
[ 1, 3, 5 ].all? { |e| e.odd? }
![ 1 ].any? { |e| e.zero? }
[].all? { |e| e == nil }
[].any? { |e| e == nil }
What advantages does junctions in Ruby provide over #any? and #all? ?
Regards,
Denis
--
Denis Defreyne
denis.defreyne@stoneship.org
|
|
0
|
|
|
|
Reply
|
denis.defreyne (22)
|
4/18/2009 9:38:21 PM
|
|
On Sat, Apr 18, 2009 at 11:38 PM, Denis Defreyne
<denis.defreyne@stoneship.org> wrote:
> On Apr 17, 2009, at 11:17 PM, Robert Dober wrote:
>
>> E.g.
>>
>> =A0any(1, 2, 3) > 2 =A0 =A0 --> true
>> =A0[ 1, 3, 5 ].all.odd? --> true
>> =A0none( 1 ).zero? =A0 =A0 =A0--> true
>> =A0all() =3D=3D nil =A0 =A0 =A0 =A0 --> true
>> =A0any() =3D=3D nil =A0 =A0 =A0 =A0 --> false
>
> The examples you gave are equivalent to
>
> =A0 =A0 =A0 =A0[ 1, 2, 3 ].any? { |e| e > 2 }
> =A0 =A0 =A0 =A0[ 1, 3, 5 ].all? { |e| e.odd? }
> =A0 =A0 =A0 =A0![ 1 ].any? { |e| e.zero? }
> =A0 =A0 =A0 =A0[].all? { |e| e =3D=3D nil }
> =A0 =A0 =A0 =A0[].any? { |e| e =3D=3D nil }
>
> What advantages does junctions in Ruby provide over #any? and #all? ?
They are an abstraction of the code blocks, if you look at streams,
the second concept implemented in Lab419::functional we have exactly
the same pattern, delay is nothing more than a lambda.
As a matter of fact the main pleasure I have got from releasing code
in Ruby is that Ruby does all the work
and I can get the compliments, well I cannot because you have pointed
out that I have not done anything. Thank you very much ;)
ok seriously now: I believe that this kind of abstraction is useful,
makes code shorter and even more readable.
Is it worth a package? Well maybe not, but what are you going to tell
a perl6 guru if he asks you if Ruby got Junctions? (Proud in Ruby was
indeed a key motivation, maybe that is bad, I dunno )
Look at this example:
if a_set.any > b_set.all then
would be
if a_set.any?{ | x | b_set.all?{ | y | x > y } } then
I kind of prefer to maintain code written in the first style.
Cheers
Robert
|
|
0
|
|
|
|
Reply
|
robert.dober (2193)
|
4/19/2009 9:19:20 AM
|
|
On Apr 18, 2009, at 4:38 PM, Denis Defreyne wrote:
> On Apr 17, 2009, at 11:17 PM, Robert Dober wrote:
>
>> E.g.
>>
>> any(1, 2, 3) > 2 --> true
>> [ 1, 3, 5 ].all.odd? --> true
>> none( 1 ).zero? --> true
>> all() == nil --> true
>> any() == nil --> false
>
> The examples you gave are equivalent to
> ![ 1 ].any? { |e| e.zero? }
Or:
not [1].include? 0
> [].all? { |e| e == nil }
Or:
a = []
a.nitems == a.size
> [].any? { |e| e == nil }
Or:
a.nitems > 0
Ruby 1.9's Enumerable#none?() and Enumerable#one?() also help out with
tests like this.
James Edward Gray II
|
|
0
|
|
|
|
Reply
|
james8284 (4404)
|
4/19/2009 2:21:00 PM
|
|
On Apr 19, 2009, at 4:19 AM, Robert Dober wrote:
> Look at this example:
> if a_set.any > b_set.all then
> would be
> if a_set.any?{ | x | b_set.all?{ | y | x > y } } then
>
> I kind of prefer to maintain code written in the first style.
I imagine I would just do:
if a_set.max > b_set.max
# ...
end
James Edward Gray II
|
|
0
|
|
|
|
Reply
|
james8284 (4404)
|
4/19/2009 2:27:16 PM
|
|
On Sun, Apr 19, 2009 at 4:27 PM, James Gray <james@grayproductions.net> wro=
te:
> I imagine I would just do:
>
> =A0if a_set.max > b_set.max
> =A0 =A0# ...
> =A0end
If #<=3D> is defined I would do the same, but that is not necessarily
the case. ">" might implement something
where ! ( !(a>b) -> b>a) as e.g. superset, subset relationships. If
you are against such redefinitions, please read the example as
follows:
if a_set.any.superset?( b_set.all ) then
I wanted to make it clear that Junctions can be combined and thus
become a more powerful abstraction over relations.
Cheers
Robert
--=20
Si tu veux construire un bateau ...
Ne rassemble pas des hommes pour aller chercher du bois, pr=E9parer des
outils, r=E9partir les t=E2ches, all=E9ger le travail=85 mais enseigne aux
gens la nostalgie de l=92infini de la mer.
If you want to build a ship, don=92t herd people together to collect
wood and don=92t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.
--
Antoine de Saint-Exup=E9ry
|
|
0
|
|
|
|
Reply
|
robert.dober (2193)
|
4/19/2009 5:46:17 PM
|
|
On 4/19/09, Robert Dober <robert.dober@gmail.com> wrote:
>
> Look at this example:
> if a_set.any > b_set.all then
> would be
> if a_set.any?{ | x | b_set.all?{ | y | x > y } } then
Or, for the main case where this comparison makes sense:
if a_set.max > b_set.max
|
|
0
|
|
|
|
Reply
|
cmdicely (196)
|
4/19/2009 10:58:04 PM
|
|
|
12 Replies
29 Views
(page loaded in 0.441 seconds)
|