[ANN] Junctions for Ruby1.9 (Lab419::functional-0.1.2)

R

Robert Dober

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
 
M

Masaki Suketa

Hello,

Robert said:
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
 
R

Robert Dober

Hello,

Robert said:
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
 
D

David Palm

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?

:)
 
R

Robert Dober

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.
 
T

Tim Pease

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
 
D

Denis Defreyne

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
 
R

Robert Dober

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
 
J

James Gray

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
 
J

James Gray

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
 
R

Robert Dober

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.
 
C

Christopher Dicely

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
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top