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
2
votes
2 answers

Negative arity exception in recursive macro

I'm trying a coding challenge that requires you to create code that compiles infinitely. My first thought was a macro that expands to itself forever. I wrote up: (defmacro a [] (a)) (a) This doesn't actually produce anything, but I expected it…
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
2
votes
1 answer

Is `eval` the only way to create functions with dynamically determined arity in JavaScript?

I'm using a JavaScript spy library, simple-spy. I've found out that when spying on a given function, the resulting spy always has an arity of 0. This creates a problem with my use of this currying function. So I've submitted a pull-request that adds…
Shahar 'Dawn' Or
  • 2,713
  • 1
  • 26
  • 38
2
votes
2 answers

Call a function by its name and arity

I'm trying to write a function that calls a function with a specified name and arity: my_fun(FunctionName, Arity) -> %Call FunctionName/Arity . So that calling it like this: my_fun(foo, 3) should result in a call to function foo/3. The…
Sergey Ovchinnik
  • 472
  • 4
  • 16
2
votes
2 answers

Clojure: compose functions of arity 2 (or higher)

I am processing data with a variable number of functions, depending on parameters. Each of the processing functions would receive data from its predecessor, process it and pass it on to the next function. (defn example [data] (do-things-to…
2
votes
1 answer

Using Elixir, erlport with Python 2.7.9, receiving an arity error

I am trying to use Python with Elixir and I wrote the following functional code (you can find the repo I'm building here: https://github.com/arthurcolle/elixir_with_erlport) defmodule Snake do use Application def start(_type, _args) do …
Arthur Collé
  • 2,541
  • 5
  • 27
  • 39
2
votes
2 answers

Erlang/Elixir guards and arity

Is there a way to see a function's guards without seeing the source code? Given an example function (in Elixir): def divide(x, y) when y != 0 do x / y end How would one figure out that there is a guard on divide/2 without access to the source…
Jason G
  • 916
  • 9
  • 14
2
votes
1 answer

ArityException: Wrong number of args (2) passed

there are a few related questions on SO but I just can't seem to figure this out. I've got a very simple test code: (ns test (:gen-class) (:require [clojure.java.io :as io])) (defn tail-file [path handler-func] …
Mate Varga
  • 3,144
  • 2
  • 14
  • 17
2
votes
1 answer

multi-arity defn in Clojure -- first match first serve?

To be concrete, what is supposed to happen in the following situation: (defn avg ([] 0) ([& args] (/ (reduce + args) (count args)))) (avg) i.e., can I rely on clojure to always return 0 rather than divide-by-zero?
manualcrank
  • 93
  • 1
  • 5
2
votes
1 answer

recursive macro arityexception

I am trying to write a macro similar (I think) in function to the threading macro, however this would allow me to specify a keyword where the previous form's insertion would happen. I was planning on using clojure.walk/prewalk-replace, but I am…
DJD
  • 47
  • 4
2
votes
1 answer

Maximum arity of ruby function?

I am looking to make an efficient function to clear out a redis-based cache. I have a method call that returns a number of keys from redis: $redis.keys("foo:*") That returns all the keys that start with "foo:". Next, I'd like to delete all the…
tlehman
  • 5,125
  • 2
  • 33
  • 51
2
votes
2 answers

Arity Overloading in Clojure

Why is the following function not working in Clojure: (defn tests [] 0 [a b] 1) It gives the following error: clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: a in this context
Sibi
  • 47,472
  • 16
  • 95
  • 163
2
votes
2 answers

How to make java API wrapper work with different function arities?

I'm writing a small wrapper for a Java API, and I create a listener like this (defn conv-listener [f] (proxy [com.tulskiy.keymaster.common.HotKeyListener] [] (onHotKey [hotKey] (f)))) Is there a way in which I can make this work whether the…
Stian Håklev
  • 1,240
  • 2
  • 14
  • 26
2
votes
2 answers

Variable arity scheme

(define test (lambda args (if (= (length args) 1) (display (car args)) (begin (display (car args)) (test (cdr args)))))) I was looking for it on the net and didnt find the answer, I was able to get a variable number of arguments to…
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
1
vote
2 answers

Controlling function's arity

I was reading the answer to this question: Haskell: difference between . (dot) and $ (dollar sign) And the reply struck me as odd... What does he mean + has no input? And then I tried: ((+) 1) ((+) 1 1) ((+) 1 1 1) Whoops... sad news. But I'm sure…
user797257
1
vote
1 answer

Invoking variable-arity methods with Java reflection?

I would like to understand what might be going on with invoking variable-arity methods using Java reflection. Let's say we have a simple method: void doAllTheThings(Object ... things) { // ...which does something with all the things... } And we…
Joseph Weissman
  • 5,697
  • 5
  • 46
  • 75