Here's an example of how I think I'd do a varargs in J:

example=:3 :0
   assert. 2<:#y
   'a b'=.2 {.y
   rest=.2}.y
   ...

That said, it's quite often better to rewrite modules from first
principles and/or the underlying requirements.

-- 
Raul


On Mon, Jun 22, 2015 at 1:54 PM, 'Pascal Jasmin' via Programming
<[email protected]> wrote:
> Finally found a clean way to manipulate local assignments.
>
> First, I posted before how to manipulate default function parameters
>
> dflt =: ([`]@.(0 = #@>@[)"0 0) (1 : (':'; '(linearize (#y) {. x) u 
> y'))&(<^:(L. = 0:))
>
> it replaces null parameters with a default value
>
>   (11;a:) dflt 3;4
> ┌──┬─┐
> │11│4│
> └──┴─┘
>
>
> but it also cuts off input to be limited to the number of default parameters 
> (often a good thing)
>
>   (11;2;3) dflt 3 ; a:
> ┌──┬─┐
> │11│2│
> └──┴─┘
>
>
> varargs is a concept in many dynamic languages which allows a function 
> signature to capture all of the leftover function parameters into an array.
>
> lr =: 3 : '5!:5 < ''y'''
> ql =: 2 : '(quote m) , n'
>
> varargs1 =: 1 : '(m ql '' =. '' , lr@:dflt) ; [ }.~ #@]'
> do0ret1 =: [: empty^:(('<0$a:' -: lr@:{.) *. 1 = #) }. [ [: ". 0&{::
> varargs =: 1 : '[: do0ret1 m varargs1'
>
> the m parameter to the varargs adverb is the local names to assign to the 
> "fixed" default parameters.  After assigning, it returns the left over params.
>
> as a test function:
>
>
> tonull =: NULL"_^:(0 = #)
> tvarargs =: 3 : 'a + b * +/ tonull > rest =. (y ''a b'' varargs 3;4) '
>
>  +/ i.0 NB. reason for tonull
> 0
>  NULL -: +/i. 0 0
> 1
>
>
>  tvarargs 3;a:
> 3
>  tvarargs a:
> 3
>
>  tvarargs a:,7;2;3;1
> 45
> 3 + 7 * +/ 1 2 3
>
> tvarargs 4 5 ;3 2; <"1 i.4 2
> 40 37
>
> can also be used with tacit code, but needs a "late binder"
>
> lval_z_ =: 1 : 0
> 3 : m @]
> )
>
> ('a'lval + 'b'lval * [: +/@:tonull@:> (3;4) 'a b' varargs~ ]) a:,a:, <"1 i.4 2
> 51 67
> an explicit function that uses a more complex conversion for null 2nd 
> parameter
>
> tvarargs2 =: 3 : 0
>  rest =.  +/ tonull > y 'a b' varargs 3;a:
>  b =. (a + rest)"_^:(0 = #) b
>  a + b * rest
> )
> (basically first line leaves b null if null is passed to it)
>
>  tvarargs2 a:
> 3
>  tvarargs2 13;a:
> 13
>  tvarargs2 13;a:,2;3
> 103
>
>  13 + 18 * +/ 2 3
> 103
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to