Questions tagged [arity]

The arity of a function or operation is the number of arguments or operands that the function takes.

The arity of a function or operation is the number of arguments or operands that the function takes.

The term "arity" is rarely employed in everyday usage. For example, rather than saying "the arity of the addition operation is 2" or "addition is an operation of arity 2" one usually says "addition is a binary operation". In general, the naming of functions or operators with a given arity follows a convention similar to the one used for n-based numeral systems such as binary and hexadecimal. One combines a Latin prefix with the -ary ending; for example:

  • A nullary function takes no arguments.
  • A unary function takes one argument.
  • A binary function takes two arguments.
  • A ternary function takes three arguments.
  • An n-ary function takes n arguments.
108 questions
66
votes
5 answers

Get a function's arity

In Javascript, how can one determine the number of formal parameters defined for a function? Note, this is not the arguments parameter when the function is called, but the number of named arguments the function was defined with. function zero() { …
JasonSmith
  • 72,674
  • 22
  • 123
  • 149
23
votes
6 answers

Haskell: Function to determine the arity of functions?

Is it possible to write a function arity :: a -> Integer to determine the arity of arbitrary functions, such that > arity map 2 > arity foldr 3 > arity id 1 > arity "hello" 0 ?
Frank S. Thomas
  • 4,725
  • 2
  • 28
  • 47
17
votes
4 answers

Scala: Unpacking tuple as part of argument list

I am trying to send the result of a method call's tuple, as part of the argument list for another method. Target method def printResult(title: String, result: Int, startTime: Long, endTime: Long) Return from method, partial argument list def…
Hanxue
  • 12,243
  • 18
  • 88
  • 130
16
votes
2 answers

Why is this Java method call considered ambiguous?

I've come across a strange error message that I believe may be incorrect. Consider the following code: public class Overloaded { public interface Supplier { int get(); } public interface Processor { String process(String…
16
votes
2 answers

Why would this code complain about "the arity of the generic type definition"?

I've got a generic type: class DictionaryComparer : IEqualityComparer> And a factory method that will (should) create an instance of this class for a given dictionary type. private static…
dkackman
  • 15,179
  • 13
  • 69
  • 123
15
votes
2 answers

Get function arity from template parameter

How can I get the arity of an arbitrary function type used as a template parameter? The function can be a normal function, a lambda or a functor. Example: template std::size_t getArity() { // ...? } template
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
14
votes
1 answer

How to pass a function of arity 2 as an argument in Elixir for Enum.map?

Let's say I have something like: Enum.map(list, fn(x) -> String.duplicate("a", someValue * x) end) But instead, I'd like to pass the String.duplicate/2 function as an argument in order to simplify my code: Enum.map(list, &String.duplicate/2) I…
Sasha Fonseca
  • 2,257
  • 23
  • 41
13
votes
2 answers

Are functions of arity-n really just an n-category due to currying? Can they be made into a 1-category?

This question has a longish prelude before I can actually ask it :) Let's say type A and B represent categories, then the function f :: B -> A is a morphism between the two categories. We can create a new category with A and B as objects and f as…
Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53
9
votes
4 answers

Ecto query and custom MySQL function with variable arity

I want to perform a query like the following one: SELECT id, name FROM mytable ORDER BY FIELD(name, 'B', 'A', 'D', 'E', 'C') FIELD is a MySQL specific function, and 'B', 'A', 'D', 'E', 'C' are values coming from a List. I tried using fragment, but…
Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
9
votes
1 answer

Get arity of a function

I'm a JavaScript developer who's learning Lua. I'm stuck with a problem of getting a function's arity in the Lua language. In JavaScript, it's simple: function test (a, b) {} console.log(test.length) // 2 How is it possible to do it this easily in…
Kosmetika
  • 20,774
  • 37
  • 108
  • 172
9
votes
2 answers

Find function's arity in Common Lisp

I've been doing some Genetic Programming and I've been separating functions into different function sets based on their arity; it's all rather complex. I'd like to know if there's a simpler way to to do it. For example, if there's a function that…
Johnny McKenzie
  • 197
  • 2
  • 9
8
votes
5 answers

Can I pass an arbitrary function to another function in Scala?

I'm new to Scala, and being able to pass functions to other functions is pretty neat-- but can I pass an arbitrary function reference to another function? The arity of said functional parameter will be fixed (that said, I'm also curious about…
Dan Barowy
  • 2,270
  • 24
  • 35
7
votes
2 answers

Elixir: function overloading with different arity

is there any way to define overload functions with different arity, e.g in C# I can just do: foo(bar) or foo(bar, baz) In Elixir, the only way to do that would be to put them in separate modules, which will get messy pretty quickly. Is there any…
tldr
  • 11,924
  • 15
  • 75
  • 120
7
votes
1 answer

Standard method for determining the arity and other traits of std::bind() result?

I've been pounding my head for a few days trying to figure out how to make a class have a nice clean public interface to perform registration of callback mechanisms. The callbacks can be C++11 lambdas, std::function,…
6
votes
1 answer

Dynamically find out how many inputs a function has, Racket

Is there a way to find out at runtime, how many inputs (arguments, parameters) a function has? Say, I want to: (define (my-function unknown-function) ... (number-of-necessary-arguments unknown-function) ...)
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
1
2 3 4 5 6 7 8