# Function with many arities

posted 7 years ago

- 0

I have 3 newbie questions:

part 1) Is the term arity (which I concluded to mean the number of method parameters) a mathematical term ? Lambda calculus ?

part 2) What is reason behind declaring Function traits for 0 to 22 arities ?

Function0

Function22

part 3) As a corollary to 2, how would your declare Functions of variable arities ? e.g. varargs

Regards,

Pho

part 1) Is the term arity (which I concluded to mean the number of method parameters) a mathematical term ? Lambda calculus ?

part 2) What is reason behind declaring Function traits for 0 to 22 arities ?

Function0

Function22

part 3) As a corollary to 2, how would your declare Functions of variable arities ? e.g. varargs

Regards,

Pho

Regards,

Pho

posted 7 years ago

- 0

Newbie questions are the best kind! Many of us are still exploring Scala and every newbie question helps us to find new frontiers we'd never even heard of before. Finding new frontiers is half the fun!

I wasn't really familiar with the term arity before but this was fun to explore.

According to wikipedia:

My understanding is that arity is used to define the most primitive mathematical concepts. If we had a function that computed 6 values as a-b*c/x-y*z, we'd actual have a bunch of binary arities and not a 6-arity function.

7 = nullary, no operators

-7 = unary, one operator that needs one parameter

7+7 = binary, one operator that needs two parameters

true?0:7 = ternary, one operator (though the syntax is two separate symbols) that needs three operators assuming you don't use the "Elvis" version

I found a terrific blog article on Scala function objects and the Function(n) traits. Really cool.

I'm not sure a varargs is necessary. My hunch is that the Scala strategy would be to put all your values in a List or tuple, which can then be passed as a single argument.

I wasn't really familiar with the term arity before but this was fun to explore.

According to wikipedia:

From a mathematical point of view, a function of n arguments can always be considered as a function of one single argument... The same is true for programming languages, where functions taking several arguments could always be defined as functions taking a single argument of some complex type such as a tuple, or in languages with higher-order functions, by currying.

My understanding is that arity is used to define the most primitive mathematical concepts. If we had a function that computed 6 values as a-b*c/x-y*z, we'd actual have a bunch of binary arities and not a 6-arity function.

7 = nullary, no operators

-7 = unary, one operator that needs one parameter

7+7 = binary, one operator that needs two parameters

true?0:7 = ternary, one operator (though the syntax is two separate symbols) that needs three operators assuming you don't use the "Elvis" version

I found a terrific blog article on Scala function objects and the Function(n) traits. Really cool.

I'm not sure a varargs is necessary. My hunch is that the Scala strategy would be to put all your values in a List or tuple, which can then be passed as a single argument.

A good workman is known by his tools.

Garrett Rowe

Ranch Hand

Posts: 1296

posted 7 years ago

- 0

Scala does have a variable argument syntax. You have to use the * notation.

The type of the varargs reference is a Seq.

In the given example args is a Seq[A]. This syntax is used in a number of places in the standard Scala libs, one of the most used is in the List object where the apply() method is defined to take a variable number of arguments.

The type of the varargs reference is a Seq.

In the given example args is a Seq[A]. This syntax is used in a number of places in the standard Scala libs, one of the most used is in the List object where the apply() method is defined to take a variable number of arguments.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

posted 7 years ago

- 0

@Marc, Thanks for the link to that blogpost. Good read.

My followup questions

Are FunctionN traits similar to Procs in Ruby ? Procs are those objects created by passing a block to a lambda method.

I'm trying to digest Partial functions right now. Doets and Van Eijck's book

Thanks all.

Pho

My followup questions

I'm trying to digest Partial functions right now. Doets and Van Eijck's book

**The Haskell Road to Logic, Math and programming**has a section dedicated to it. I'll post again if I'm stumped.Thanks all.

Pho

T. Morris

Greenhorn

Posts: 3

posted 7 years ago

- 0

Hi Pho,

I have heard the term Functor used like that before. Also, I have seen a discussion on the Scala mailing list about it. FYI, I call it a misuse and so do many others, but let's not get hung up on that.

Instead, here is a more concise definition. A functor is any implementation of the following interface:

...such that 2 laws satisfy:

identity. forall x. fmap(identity, x) == x composition. forall f g x. fmap(f, fmap(g, x)) == fmap(f compose g, x)

Notice the kind of the type constructor argument F (* -> *). There are several type constructors that satisfy this kind. Just to name a few:

scala.List scala.Option scala.Stream scala.Either.LeftProjection scala.Either.RightProjection scala.Function1#Apply[T] (i.e. curry the first type argument)

You might be keen to implement this interface with these type constructors just to observe each functor manifest

I have heard the term Functor used like that before. Also, I have seen a discussion on the Scala mailing list about it. FYI, I call it a misuse and so do many others, but let's not get hung up on that.

Instead, here is a more concise definition. A functor is any implementation of the following interface:

...such that 2 laws satisfy:

Notice the kind of the type constructor argument F (* -> *). There are several type constructors that satisfy this kind. Just to name a few:

You might be keen to implement this interface with these type constructors just to observe each functor manifest

It is sorta covered in the JavaRanch Style Guide. |