Yeah we should adopt the syntax that R and Python both use, in which default 
arguments are defined in the function definition.  

Primitive types such as ints and strings can be set in the function definition, 
and more complex types such as matrices can simply use a null value as the 
default in the function definition, followed by an actual assignment within the 
function body.

In R:
```
f <- function(x=3)
  x

f()  # 3
f(2)  # 2
```

```
f <- function(x=NULL) {
  if (is.null(x))
    x = matrix(4, 1, 10)
  x
}

f()  # matrix of 4's
f(matrix(2, 5, 12))  # matrix of 2's
```

Same thing in Python, except it uses `None` instead of `NULL`:
```
def f(x=3):
  return x

f()  # 3
f(2)  # 2
```

```
def f(x=None):
  if x is None:
    x = [1,2,3]
  return x

f()  # list [1,2,3]
f([4,5,6])  # list [4,5,6]
```


--

Mike Dusenberry
GitHub: github.com/dusenberrymw
LinkedIn: linkedin.com/in/mikedusenberry

Sent from my iPhone.


> On Apr 21, 2017, at 5:40 PM, Deron Eriksson <deroneriks...@gmail.com> wrote:
> 
> BTW, that is assuming our algorithms have been converted to functions.
> Deron
> 
> 
> On Fri, Apr 21, 2017 at 5:37 PM, Deron Eriksson <deroneriks...@gmail.com>
> wrote:
> 
>> Thank you Matthias. I highly agree with your idea about having a default
>> specification similar to R WRT the function signatures for default values.
>> 
>> This becomes a significant issue for some of our algorithms, where they
>> might take in 10 arguments but default values are should typically be used
>> for  6+ or 7+ of the arguments.
>> 
>> Deron
>> 
>> 
>> On Fri, Apr 21, 2017 at 5:25 PM, Matthias Boehm <mboe...@googlemail.com>
>> wrote:
>> 
>>> well, for arguments passed into dml scripts there is of course ifdef($b,
>>> 2)
>>> but for functions there is indeed no good support. At runtime level we
>>> still support default parameters for scalar arguments at the tail of the
>>> parameter list but I guess at one point the corresponding parser support
>>> was discontinued.
>>> 
>>> I personally would like a default specification similar to R in the
>>> function signature with the corresponding function calls that bind values
>>> to a subset of parameters.
>>> 
>>> Regards,
>>> Matthias
>>> 
>>> On Fri, Apr 21, 2017 at 4:18 PM, Deron Eriksson <deroneriks...@gmail.com>
>>> wrote:
>>> 
>>>> Is there a way to set default parameter values using DML? I believe
>>> both R
>>>> and Python offer this capability.
>>>> 
>>>> The only solution I could come up with using DML is to pass in a
>>> variable
>>>> that is NaN and cast this to a string and use this string in an if
>>>> conditional statement.
>>>> 
>>>> addone = function(double b) return (double a) {
>>>>    c = ''+b;
>>>>    if (c == 'NaN') {
>>>>        b = 2.0
>>>>    }
>>>>    a = b + 1;
>>>> }
>>>> 
>>>> z=0.0/0.0;
>>>> x = addone(z);
>>>> print(x);
>>>> y = addone(4.0);
>>>> print(y);
>>>> 
>>>> Is there a cleaner way to accomplish this, or is DML lacking this R
>>>> feature?
>>>> 
>>>> Deron
>>>> 
>>>> --
>>>> Deron Eriksson
>>>> Spark Technology Center
>>>> http://www.spark.tc/
>>>> 
>>> 
>> 
>> 
>> 
>> --
>> Deron Eriksson
>> Spark Technology Center
>> http://www.spark.tc/
>> 
>> 
> 
> 
> -- 
> Deron Eriksson
> Spark Technology Center
> http://www.spark.tc/

Reply via email to