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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-17 23:18
(Received via mailing list)
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/Per...
    http://www.perl.com/pub/a/2003/07/29/exegesis6.html?page=4
    http://www.programmersheaven.com/2/Perl6-FAQ-Junctions
E97181961afa48102e3a28daab69b904?d=identicon&s=25 Masaki Suketa (Guest)
on 2009-04-18 03:04
(Received via mailing list)
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-18 12:26
(Received via mailing list)
On Sat, Apr 18, 2009 at 3:04 AM, Masaki Suketa
<masaki.suketa@nifty.ne.jp> 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 Suketa
>
Thank you for reporting this, I will look into it right now.
Robert
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-18 13:10
(Received via mailing list)
Attachment: base.rb.gz (2 KB)
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 <you know where the old one has been installed>


Cheers
Robert
6bed507c0085d39447171b95c515a890?d=identicon&s=25 David Palm (Guest)
on 2009-04-18 16:59
(Received via mailing list)
> 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?

:)
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-18 17:05
(Received via mailing list)
On Sat, Apr 18, 2009 at 4:58 PM, David Palm <dvdplm@gmail.com> wrote:
> :)
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.
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2009-04-18 17:38
(Received via mailing list)
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
E97181961afa48102e3a28daab69b904?d=identicon&s=25 Masaki Suketa (Guest)
on 2009-04-18 20:13
(Received via mailing list)
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
Be732ee41fd3038aa98a0a7e7b7be081?d=identicon&s=25 Denis Defreyne (Guest)
on 2009-04-18 23:39
(Received via mailing list)
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-19 11:19
(Received via mailing list)
On Sat, Apr 18, 2009 at 11:38 PM, Denis Defreyne
<denis.defreyne@stoneship.org> 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 ;)

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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-04-19 16:23
(Received via mailing list)
On Apr 18, 2009, at 4:38 PM, Denis Defreyne 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 Gray II
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-04-19 16:27
(Received via mailing list)
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-19 19:47
(Received via mailing list)
On Sun, Apr 19, 2009 at 4:27 PM, James Gray <james@grayproductions.net>
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.
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-04-20 01:00
(Received via mailing list)
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
This topic is locked and can not be replied to.