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
1
vote
1 answer

C++11: how to accept user-supplied functions elegantly?

I am trying to offer users of my library to supply their own calculation functions, that need to maintain the rules: returns a float value may require 0 to 7 float arguments no exceptions possible I got a solution - see below - but it is bulky and…
MIq19
  • 13
  • 5
1
vote
1 answer

Specifying method based on arity in Erlang

I am having an Erlang module that exports two methods with the same name but different arity: proc/1 and proc/2. When using the methods in the form of MFA , how do you specify that the /2 should be used or /1? See for…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
1
vote
1 answer

Produce a list of Lucas/fibonacci sequence with Prolog

I am just fiddling with prolog a little an I've come across a problem I don't know how to solve. I want to create a procedure that returns the Lucas sequence of a given number In. I have got the code to return the actual number down, but it does…
abdcg
  • 137
  • 1
  • 11
1
vote
2 answers

How can I test a signature violation without calling a perl function?

Let's say I have, use experimental 'signatures'; sub foo ($bar) { return 0 }; Now if I call it without the right arity, I'll get an error. # Too few arguments for subroutine 'main::foo' foo() # Too many arguments for subroutine…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
1
vote
1 answer

In scala shapeless library, is it possible to write a generic arity function when the arity > 22 (presumably using one of shapeless macros)?

The following code is a typical demo of one of shapeless' use case: def getHList[P <: Product, F, L <: HList](p: P)(implicit gen: Generic.Aux[P, L]): L = { gen.to(p) } val v = getHList(1, 2, 3, 4, 5, 6, 7, 8, 9) This gives the proper…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
1
vote
1 answer

Lisp/Scheme/Racket: How to define a function with ellipsis

I want to define a function in Racket with undefined number of arguments so I use ellipsis, but it doesn't work: (define (f x ...) (printf x ...)) (f "~a ~a" "foo" "bar") Error: Arity mismatch How to define a function in Racket with ellipsis?
Dee
  • 7,455
  • 6
  • 36
  • 70
1
vote
1 answer

Python splat-in-the-middle

You can put splat arguments in the middle rather than at the end (only in python 3 it seems): import functools def wierd_sum(use_str_cat=False, *args, use_product=False): if use_str_cat: return ''.join([str(a) for a in args]) elif…
Kevin Kostlan
  • 3,311
  • 7
  • 29
  • 33
1
vote
2 answers

Function taking another function of arbitrary arity as argument

I have a series of functions that accept a function as an argument (func), and the only difference in them is that func accepts different numbers of parameters. def tryGet[T,A](func: (A) => T) def tryGet[T,A,B](func: (A,B) => T) ... …
Jethro
  • 3,029
  • 3
  • 27
  • 56
1
vote
0 answers

what is arity of query in database theory

What is the arity of a query in databases? for a realtion is the number of attributes. Is it the number of attributes in all the input relations, or number of attributes of the new query output relation? for eg. Input: a database D, a query Q/k ∈…
Studentguy
  • 11
  • 2
1
vote
1 answer

Passing a list to a multiple-arity function in DrRacket (Scheme)

I am trying to generate the least common multiple for all numbers from 1 to n as described in OEIS-A003418. In the DrRacket REPL I'm using the following code: (lcm (apply values (build-list 256 add1))) Which gives me a "result arity mismatch"…
hatch22
  • 797
  • 6
  • 18
1
vote
1 answer

Clojure protocols - dispatch only on 2-arity version of multi-arity function

Short Version I want a multi-arity function to dispatch on type for the 2-arity version, but I want the 1-arity version identical across all types. Long Version (With Example) I have a protocol that looks something like this (defprotocol foo (bar…
Lindon
  • 1,292
  • 1
  • 10
  • 21
1
vote
1 answer

Wrong number of args (2) passed to: core/first

I am new to Clojure and I have having some issues with iterating data. The code I have written is below : (defn save-monthly-targets "Parse Monthly Targets Data and Save" [monthlyTargets] (println "Save Monthly " monthlyTargets) (if (first…
Tanmay
  • 85
  • 2
  • 9
1
vote
1 answer

Get function arity in scala

Is there a way to retrieve the arity of a function? For instance, given the following function: def moop[I,O](func: I => O) = { val arity = I.??? ??? } How would I get the arity of func?
irundaia
  • 1,720
  • 17
  • 25
1
vote
2 answers

Function in Scheme / Racket returning functions of particular arities

How to define a function in Scheme / Racket that returns functions of particular arities? Currently I have in my code the following: (define (get-function n) (cond [(= n 1) (lambda (a) a)] [(= n 2) (lambda (a b) (+ a b))] [(= n 3)…
formalizm
  • 47
  • 4
1
vote
1 answer

abstract over scala functions without losing types

I'm making a function that takes a lambda, and uses .tupled on it if possible (arity 2+). For the compiler to allow using that, it needs to know if the lambda is really a Function2 (~ Function22). However, pattern-matching for Function2[Any,Any,Any]…
Kiara Grouwstra
  • 5,723
  • 4
  • 21
  • 36