[#16611] lambda, ->, haskell, and so on — Dave Thomas <dave@...>

This is one of those e-mails that I know from the start to be futile, =20=

148 messages 2008/05/01
[#16661] Re: lambda, ->, haskell, and so on — Paul Brannan <pbrannan@...> 2008/05/05

On Thu, May 01, 2008 at 12:26:47PM +0900, Dave Thomas wrote:

[#16662] Re: lambda, ->, haskell, and so on — "David A. Black" <dblack@...> 2008/05/05

Hi --

[#16663] Re: lambda, ->, haskell, and so on — ts <decoux@...> 2008/05/05

David A. Black wrote:

[#16664] Re: lambda, ->, haskell, and so on — "David A. Black" <dblack@...> 2008/05/05

Hi --

[#16682] Re: lambda, ->, haskell, and so on — ara howard <ara.t.howard@...> 2008/05/08

[#16684] Re: lambda, ->, haskell, and so on — Michael Neumann <mneumann@...> 2008/05/08

ara howard wrote:

[#16687] Re: lambda, ->, haskell, and so on — "David A. Black" <dblack@...> 2008/05/08

Hi --

[#16691] Re: lambda, ->, haskell, and so on — "ara.t.howard" <ara.t.howard@...> 2008/05/08

[#16692] Re: lambda, ->, haskell, and so on — "David A. Black" <dblack@...> 2008/05/08

Hi --

[#16695] Re: lambda, ->, haskell, and so on — "ara.t.howard" <ara.t.howard@...> 2008/05/08

[#16705] Re: lambda, ->, haskell, and so on — Evan Phoenix <evan@...> 2008/05/11

Not to throw the whole thread into a tizzy again, but why again is:

[#16708] Re: lambda, ->, haskell, and so on — Nobuyoshi Nakada <nobu@...> 2008/05/11

Hi,

[#16720] Re: lambda, ->, haskell, and so on — Yukihiro Matsumoto <matz@...> 2008/05/11

Hi,

[#16721] Re: lambda, ->, haskell, and so on — "David A. Black" <dblack@...> 2008/05/12

Hi --

[#16722] Re: lambda, ->, haskell, and so on — Yukihiro Matsumoto <matz@...> 2008/05/12

Hi,

[#16723] Re: lambda, ->, haskell, and so on — Evan Phoenix <evan@...> 2008/05/12

[#16724] Re: lambda, ->, haskell, and so on — Yukihiro Matsumoto <matz@...> 2008/05/12

Hi,

[#16726] Re: lambda, ->, haskell, and so on — Nathan Weizenbaum <nex342@...> 2008/05/12

What about "fn" or "fun", for "function"?

[#16728] Re: lambda, ->, haskell, and so on — Yukihiro Matsumoto <matz@...> 2008/05/12

Hi,

[#16731] Re: lambda, ->, haskell, and so on — Evan Phoenix <evan@...> 2008/05/12

[#16732] Re: lambda, ->, haskell, and so on — Yukihiro Matsumoto <matz@...> 2008/05/12

Hi,

[#16759] Re: lambda, ->, haskell, and so on — "David A. Black" <dblack@...> 2008/05/13

Hi --

[#16766] Re: lambda, ->, haskell, and so on — Yukihiro Matsumoto <matz@...> 2008/05/14

Hi,

[#16784] Re: lambda, ->, haskell, and so on — "David A. Black" <dblack@...> 2008/05/18

Hi --

[#16795] Re: lambda, ->, haskell, and so on — Nate_Wiger@... 2008/05/19

On Wed, 14 May 2008, David A. Black wrote:

[#16797] Re: lambda, ->, haskell, and so on — Yukihiro Matsumoto <matz@...> 2008/05/19

Hi,

[#16798] Re: lambda, ->, haskell, and so on — "Christopher Gill" <gilltots@...> 2008/05/19

how about an uppercase lambda (instead of the usual lowercase one)

[#16802] Re: lambda, ->, haskell, and so on — "Suraj N. Kurapati" <sunaku@...> 2008/05/20

Christopher Gill wrote:

[#16843] Re: lambda, ->, haskell, and so on — "Suraj N. Kurapati" <sunaku@...> 2008/05/22

Suraj N. Kurapati wrote:

[#16846] Re: lambda, ->, haskell, and so on — "Berger, Daniel" <Daniel.Berger@...> 2008/05/22

=20

[#16854] Re: lambda, ->, haskell, and so on — "=?ISO-8859-2?Q?Rados=B3aw_Bu=B3at?=" <radek.bulat@...> 2008/05/22

T24gVGh1LCBNYXkgMjIsIDIwMDggYXQgNTozNyBQTSwgQmVyZ2VyLCBEYW5pZWwgPERhbmllbC5C

[#16857] Re: lambda, ->, haskell, and so on — "Jeremy McAnally" <jeremymcanally@...> 2008/05/23

RXZlbiB0aG91Z2ggSSBzZWUgdGhlIHVzZWZ1bG5lc3MsIHRoYXQncyBqdXN0IHVnbHkuCgotLUpl

[#16874] Re: lambda, ->, haskell, and so on — Nate_Wiger@... 2008/05/23

"Jeremy McAnally" <jeremymcanally@gmail.com> wrote on 05/22/2008 05:35:01=20

[#16875] Re: lambda, ->, haskell, and so on — "Nikolai Weibull" <now@...> 2008/05/23

2008/5/23 <Nate_Wiger@playstation.sony.com>:

[#16886] lambda with normal block syntax — "Eric Mahurin" <eric.mahurin@...>

This patch is an independent but related one to my previous one. It can be

64 messages 2008/05/25
[#16895] Re: [PATCH] lambda with normal block syntax — Nobuyoshi Nakada <nobu@...> 2008/05/26

Hi,

[#16900] Re: [PATCH] lambda with normal block syntax — "Eric Mahurin" <eric.mahurin@...> 2008/05/26

On Sun, May 25, 2008 at 8:19 PM, Nobuyoshi Nakada <nobu@ruby-lang.org>

[#16901] Re: [PATCH] lambda with normal block syntax — Yukihiro Matsumoto <matz@...> 2008/05/26

Hi,

[#16902] Re: [PATCH] lambda with normal block syntax — "Suraj N. Kurapati" <sunaku@...> 2008/05/26

Hi,

[#16903] Re: [PATCH] lambda with normal block syntax — Yukihiro Matsumoto <matz@...> 2008/05/26

Hi,

[#16904] Re: [PATCH] lambda with normal block syntax — Dave Thomas <dave@...> 2008/05/26

[#16905] Re: [PATCH] lambda with normal block syntax — Yukihiro Matsumoto <matz@...> 2008/05/26

Hi,

[#16907] Re: [PATCH] lambda with normal block syntax — Dave Thomas <dave@...> 2008/05/26

[#16912] Re: [PATCH] lambda with normal block syntax — Yukihiro Matsumoto <matz@...> 2008/05/26

Hi,

[#16920] Re: [PATCH] lambda with normal block syntax — David Flanagan <david@...> 2008/05/26

If I may, here are two entries from the ChangeLog file:

[#16922] Re: [PATCH] lambda with normal block syntax — Dave Thomas <dave@...> 2008/05/26

[#16927] Re: [PATCH] lambda with normal block syntax — David Flanagan <david@...> 2008/05/26

Dave Thomas wrote:

[#16928] Re: [PATCH] lambda with normal block syntax — Dave Thomas <dave@...> 2008/05/26

[#16929] Re: [PATCH] lambda with normal block syntax — David Flanagan <david@...> 2008/05/26

Dave Thomas wrote:

[#16931] Re: [PATCH] lambda with normal block syntax — Dave Thomas <dave@...> 2008/05/27

[#16946] Re: [PATCH] lambda with normal block syntax — David Flanagan <david@...> 2008/05/27

Dave Thomas wrote:

[#16947] Re: [PATCH] lambda with normal block syntax — James Gray <james@...> 2008/05/27

On May 27, 2008, at 12:33 PM, David Flanagan wrote:

[#16949] Re: [PATCH] lambda with normal block syntax — David Flanagan <david@...> 2008/05/27

James Gray wrote:

Re: [PATCH] block args w/ defaults (updated)

From: "Eric Mahurin" <eric.mahurin@...>
Date: 2008-05-28 01:44:00 UTC
List: ruby-core #16958
MjAwOC81LzI3IFJhZG9zs2F3IEJ1s2F0IDxyYWRlay5idWxhdEBnbWFpbC5jb20+OgoKPiBDb3Vs
ZCB5b3Ugc2VuZCB0aGlzIHBhdGNoIGFzIGF0dGFjaG1lbnQgcGxlYXNlPyBXaGVuIEkgY29weSBp
dCBmcm9tCj4gbWFpbCB0aGVyZSBhcmUgc29tZSB3aGl0ZSBzcGFjZSBpc3N1ZSBhbmQgcGF0Y2gg
Y291bGRuJ3QgYmUgYXBwbGllZAo+IGF1dG9tYXRpY2FsbHkgKHRoZSBzYW1lIHdhcyB3aXRoIHBy
ZXZpb3VzIGJ1dCBhdHRhY2hlZCBwYXRjaCB3b3JrZWQKPiBmb3IgbWUpLgo+CgpJJ20gYXR0YWNo
aW5nIGl0IGFzIGEgLnR4dCBmaWxlIHNvIHRoYXQgaXQgaXMgdmlld2FibGUgKGdtYWlsIGFsd2F5
cyB1c2VzCmJhc2U2NCBmb3IgLnBhdGNoKS4K

Attachments (1)

block_optarg.patch.txt (7.54 KB, text/x-diff)
Index: sample/test.rb
===================================================================
--- sample/test.rb	(revision 16568)
+++ sample/test.rb	(working copy)
@@ -296,6 +296,61 @@
 test_ok(f.call([42,55]) == [[42,55]])
 test_ok(f.call(42,55) == [42,55])
 
+[method(:lambda), method(:proc), Proc.method(:new), lambda{|&b|b}].each do |lam|
+
+    f = lam.call { |a, b=42, *c| [a,b,c] }
+    test_ok(f.call(1      ) == [1,42,[  ]] )
+    test_ok(f.call(1,43   ) == [1,43,[  ]] )
+    test_ok(f.call(1,43,44) == [1,43,[44]] )
+    
+    f = lam.call { |a, b=(a|16), *c, &block| [a,b,c,block&&block[]] }
+    test_ok(f.call(8      )     == [8,24,[  ],nil] )
+    test_ok(f.call(8,43   )     == [8,43,[  ],nil] )
+    test_ok(f.call(8,43,44)     == [8,43,[44],nil] )
+    test_ok(f.call(8      ){45} == [8,24,[  ],45 ] )
+    test_ok(f.call(8,43   ){45} == [8,43,[  ],45 ] )
+    test_ok(f.call(8,43,44){45} == [8,43,[44],45 ] )
+    
+    f = lam.call { |a, b=42, *c, d| [a,b,c,d] }
+    test_ok(f.call(1      ,99) == [1,42,[  ],99] )
+    test_ok(f.call(1,43   ,99) == [1,43,[  ],99] )
+    test_ok(f.call(1,43,44,99) == [1,43,[44],99] )
+    
+    f = lam.call { |a, b=(a|16), &block| [a,b,block&&block[]] }
+    test_ok(f.call(8   )     == [8,24,nil] )
+    test_ok(f.call(8,43)     == [8,43,nil] )
+    test_ok(f.call(8   ){45} == [8,24,45 ] )
+    test_ok(f.call(8,43){45} == [8,43,45 ] )
+    
+    f = lam.call { |a, b=42, d| [a,b,d] }
+    test_ok(f.call(1   ,99) == [1,42,99] )
+    test_ok(f.call(1,43,99) == [1,43,99] )
+    
+    f = lam.call { |b=42, *c, &block| [b,c,block&&block[]] }
+    test_ok(f.call(     )     == [42,[  ],nil] )
+    test_ok(f.call(43   )     == [43,[  ],nil] )
+    test_ok(f.call(43,44)     == [43,[44],nil] )
+    test_ok(f.call(     ){45} == [42,[  ],45 ] )
+    test_ok(f.call(43   ){45} == [43,[  ],45 ] )
+    test_ok(f.call(43,44){45} == [43,[44],45 ] )
+    
+    f = lam.call { |b=42, *c, d| [b,c,d] }
+    test_ok(f.call(      99) == [42,[  ],99] )
+    test_ok(f.call(43   ,99) == [43,[  ],99] )
+    test_ok(f.call(43,44,99) == [43,[44],99] )
+    
+    f = lam.call { |b=42, &block| [b,block&&block[]] }
+    test_ok(f.call(  )     == [42,nil] )
+    test_ok(f.call(43)     == [43,nil] )
+    test_ok(f.call(  ){45} == [42,45 ] )
+    test_ok(f.call(43){45} == [43,45 ] )
+    
+    f = lam.call { |b=42, d| [b,d] }
+    test_ok(f.call(   99) == [42,99] )
+    test_ok(f.call(43,99) == [43,99] )
+
+end
+
 a,=*[1]
 test_ok(a == 1)
 a,=*[[1]]
Index: parse.y
===================================================================
--- parse.y	(revision 16568)
+++ parse.y	(working copy)
@@ -666,6 +666,7 @@
 %type <node> open_args paren_args opt_paren_args
 %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
 %type <node> mrhs superclass block_call block_command
+%type <node> f_block_optarg f_block_opt
 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
 %type <node> block_param opt_block_param block_param_def f_opt
@@ -3169,9 +3170,41 @@
 		    }
 		;
 
-block_param	: f_arg ',' f_rest_arg opt_f_block_arg
+block_param	: f_arg ',' f_block_optarg ',' f_rest_arg opt_f_block_arg
 		    {
 		    /*%%%*/
+			$$ = new_args($1, $3, $5, 0, $6);
+		    /*%
+			$$ = params_new($1, $3, $5, Qnil, escape_Qundef($6));
+		    %*/
+		    }
+		| f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
+		    {
+		    /*%%%*/
+			$$ = new_args($1, $3, $5, $7, $8);
+		    /*%
+			$$ = params_new($1, $3, $5, $7, escape_Qundef($8));
+		    %*/
+		    }
+		| f_arg ',' f_block_optarg opt_f_block_arg
+		    {
+		    /*%%%*/
+			$$ = new_args($1, $3, 0, 0, $4);
+		    /*%
+			$$ = params_new($1, $3, Qnil, Qnil, escape_Qundef($4));
+		    %*/
+		    }
+		| f_arg ',' f_block_optarg ',' f_arg opt_f_block_arg
+		    {
+		    /*%%%*/
+			$$ = new_args($1, $3, 0, $5, $6);
+		    /*%
+			$$ = params_new($1, $3, Qnil, $5, escape_Qundef($6));
+		    %*/
+		    }
+                | f_arg ',' f_rest_arg opt_f_block_arg
+		    {
+		    /*%%%*/
 			$$ = new_args($1, 0, $3, 0, $4);
 		    /*%
 			$$ = params_new($1, Qnil, $3, Qnil, escape_Qundef($4));
@@ -3202,6 +3235,38 @@
 			$$ = params_new($1, Qnil,Qnil, Qnil, escape_Qundef($2));
 		    %*/
 		    }
+		| f_block_optarg ',' f_rest_arg opt_f_block_arg
+		    {
+		    /*%%%*/
+			$$ = new_args(0, $1, $3, 0, $4);
+		    /*%
+			$$ = params_new(Qnil, $1, $3, Qnil, escape_Qundef($4));
+		    %*/
+		    }
+		| f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
+		    {
+		    /*%%%*/
+			$$ = new_args(0, $1, $3, $5, $6);
+		    /*%
+			$$ = params_new(Qnil, $1, $3, $5, escape_Qundef($6));
+		    %*/
+		    }
+		| f_block_optarg opt_f_block_arg
+		    {
+		    /*%%%*/
+			$$ = new_args(0, $1, 0, 0, $2);
+		    /*%
+			$$ = params_new(Qnil, $1, Qnil, Qnil,escape_Qundef($2));
+		    %*/
+		    }
+		| f_block_optarg ',' f_arg opt_f_block_arg
+		    {
+		    /*%%%*/
+			$$ = new_args(0, $1, 0, $3, $4);
+		    /*%
+			$$ = params_new(Qnil, $1, Qnil, $3, escape_Qundef($4));
+		    %*/
+		    }
 		| f_rest_arg opt_f_block_arg
 		    {
 		    /*%%%*/
@@ -4302,6 +4367,44 @@
 		    }
 		;
 
+f_block_opt	: tIDENTIFIER '=' primary_value
+		    {
+		    /*%%%*/
+			if (!is_local_id($1))
+			    yyerror("formal argument must be local variable");
+			shadowing_lvar($1);
+			arg_var($1);
+			$$ = NEW_OPT_ARG(0, assignable($1, $3));
+		    /*%
+			$$ = rb_assoc_new($1, $3);
+		    %*/
+		    }
+		;
+
+f_block_optarg	: f_block_opt
+		    {
+		    /*%%%*/
+			$$ = $1;
+		    /*%
+			$$ = rb_ary_new3(1, $1);
+		    %*/
+		    }
+		| f_block_optarg ',' f_block_opt
+		    {
+		    /*%%%*/
+			NODE *opts = $1;
+
+			while (opts->nd_next) {
+			    opts = opts->nd_next;
+			}
+			opts->nd_next = $3;
+			$$ = $1;
+		    /*%
+			$$ = rb_ary_push($1, $3);
+		    %*/
+		    }
+		;
+
 f_optarg	: f_opt
 		    {
 		    /*%%%*/
Index: vm_insnhelper.c
===================================================================
--- vm_insnhelper.c	(revision 16568)
+++ vm_insnhelper.c	(working copy)
@@ -699,7 +699,8 @@
     else {
 	int i;
 	int argc = orig_argc;
-	const int m = iseq->argc;
+	int m = iseq->argc;
+	int opt_pc = 0;
 
 	th->mark_stack_len = argc;
 
@@ -719,18 +720,39 @@
 	    MEMCPY(argv, RARRAY_PTR(ary), VALUE, argc);
 	}
 
+        if (iseq->arg_opts) {
+            const int opts = iseq->arg_opts - 1 /* no opt */;
+            int overrides = argc-(m + iseq->arg_post_len);
+            if (overrides > opts) overrides = opts;
+            else if (overrides < 0) overrides = 0;
+            opt_pc = iseq->arg_opt_table[overrides];
+            m += overrides;
+        }
+
 	for (i=argc; i<m; i++) {
 	    argv[i] = Qnil;
 	}
 
 	if (iseq->arg_rest == -1) {
-	    if (m < argc) {
+	    if ( (m + iseq->arg_post_len) < argc) {
 		/*
 		 * yield 1, 2
 		 * => {|a|} # truncate
 		 */
-		th->mark_stack_len = argc = m;
+		th->mark_stack_len = argc = m + iseq->arg_post_len;
 	    }
+            if (iseq->arg_post_len) {
+		int len = iseq->arg_post_len;
+		int start = iseq->arg_post_start;
+		int rsize = argc > m ? argc - m : 0;
+		int psize = rsize;
+
+		MEMMOVE(&argv[start], &argv[argc - psize], VALUE, psize);
+
+		for (i=psize; i<len; i++) {
+		    argv[start + i] = Qnil;
+		}
+            }
 	}
 	else {
 	    int r = iseq->arg_rest;
@@ -754,7 +776,7 @@
 		}
 
 		/* copy post argument */
-		MEMMOVE(&argv[start], &argv[r + rsize - psize], VALUE, psize);
+		MEMMOVE(&argv[start], &argv[argc - psize], VALUE, psize);
 
 		for (i=psize; i<len; i++) {
 		    argv[start + i] = Qnil;
@@ -791,7 +813,7 @@
 	}
 
 	th->mark_stack_len = 0;
-	return 0;
+	return opt_pc;
     }
 }
 

In This Thread