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


#1

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


#2

Hello,

Robert D. 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 S.


#3

On Sat, Apr 18, 2009 at 3:04 AM, Masaki S.
removed_email_address@domain.invalid wrote:

$ /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 S.

Thank you for reporting this, I will look into it right now.
Robert


#4

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?

:slight_smile:


#5

fixed
http://rubyforge.org/frs/?group_id=3824
Ty Masaki

INSTALLATION of patch if you do not want to download
gzip -d base.rb.gz
(a) mv base.rb lib./lab419/functional/junctions
[sudo ] ruby setup.rb install

(b) mv base.rb

Cheers
Robert


#6

On Sat, Apr 18, 2009 at 8:58 AM, David P. removed_email_address@domain.invalid 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


#7

Robert D. 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 S.


#8

On Sat, Apr 18, 2009 at 4:58 PM, David P. removed_email_address@domain.invalid wrote:

:slight_smile:
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éparer des
outils, répartir les tâches, alléger le travail… mais enseigne aux
gens la nostalgie de l’infini de la mer.

If you want to build a ship, don’t herd people together to collect
wood and don’t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.


#9

On Sat, Apr 18, 2009 at 11:38 PM, Denis D.
removed_email_address@domain.invalid wrote:

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? ?
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 :wink:

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


#10

On Apr 17, 2009, at 11:17 PM, Robert D. 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


#11

On Apr 19, 2009, at 4:19 AM, Robert D. 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 G. II


#12

On Sun, Apr 19, 2009 at 4:27 PM, James G. removed_email_address@domain.invalid
wrote:

I imagine I would just do:

if a_set.max > b_set.max

end

If #<=> 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


Si tu veux construire un bateau …
Ne rassemble pas des hommes pour aller chercher du bois, préparer des
outils, répartir les tâches, alléger le travail… mais enseigne aux
gens la nostalgie de l’infini de la mer.

If you want to build a ship, don’t herd people together to collect
wood and don’t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.


#13

On 4/19/09, Robert D. removed_email_address@domain.invalid 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


#14

On Apr 18, 2009, at 4:38 PM, Denis D. wrote:

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 G. II