# Arity

The number of arguments a function takes. This can be a fixed number or variable number of arguments. Simple polymorphism can also be used to have one function take different numbers of arguments.

``````
(defn single-arity [argument]
(str "I take 1 argument only"))

(defn triple-arity [argument1 argument2 argument3]
(str "I take 3 arguments only"))

(defn multi-arity
([argument]
(str "I match 1 argument only"))
([argument1 argument2]
(str "I match when 2 arguments are used")))

(defn variable-arity [argument & more-arguments]
(str "I assign the first argument to argument,
all other arguments to more-arguments"))
``````

# Higher Order Functions

A function for which both the input and the output are functions.

``````let greet = (name) => () => `Hello \${name}!`;
``````
``````greet("HOF")(); // Hello HOF!
``````

## Partial Application

The process of getting a function with lesser arity compared to the original function by fixing the number of arguments is known as partial application.

``````let sum = (a, b) => a + b;

// partially applying `a` to `40`
let partial = sum.bind(null, 40);

// Invoking it with `b`
partial(2); //=> 42
``````

## Currying

The process of converting a function with multiple arity into the same function with less arity.

``````let sum = (a,b) => a+b;

let curriedSum = function(a) {
return function(b) {
return a + b;
};
};

curriedSum(40)(2) // 42.
``````

## Purity

A function is said to be pure if the return value is only determined by its input values, without any side effects.

``````let greet = "yo";

greet.toUpperCase(); // YO;

greet // yo;
``````

## Idempotency

A function is said to be idempotent if it has no side-effects on multiple executions with the the same input parameters.

`f(f(x)) = f(x)`

`Math.abs(Math.abs(10))`

## Functor

Structure that can be mapped over.

Simplest functor in javascript is an `Array`

``````[2,3,4].map( function(n) {
return n + 2;
}); // [4,6,8]
``````

## Referential Transparency

An expression that can be replaced with its value without changing the behaviour of the program is said to be referential transparent.

Say we have function greet:

``````let greet = () => "Hello World!";
``````

Any invocation of `greet()` can be replaced with `Hello World!` hence greet is referential transparent.

## Lazy evalution

aka call-by-need is an evaluation machanism which delays the evaluation of an expression until its value is needed.

``````let rand = function*() {
while(1<2) {
yield Math.random();
}
}
``````
``````let randIter = random();
randIter.next(); // Each exectuion gives a random value, expression is evluated on need.
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.6761026,"math_prob":0.9853937,"size":2496,"snap":"2019-13-2019-22","text_gpt3_token_len":621,"char_repetition_ratio":0.14767255,"word_repetition_ratio":0.010050251,"special_character_ratio":0.26883012,"punctuation_ratio":0.121145375,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.996165,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-23T06:18:27Z\",\"WARC-Record-ID\":\"<urn:uuid:4cf643cd-042b-4ab8-877f-30714c1aa346>\",\"Content-Length\":\"76012\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6b034a58-b5a9-4469-bec6-bb83c20d42a6>\",\"WARC-Concurrent-To\":\"<urn:uuid:74b3537a-947d-4530-b137-b11659acff2b>\",\"WARC-IP-Address\":\"185.199.110.153\",\"WARC-Target-URI\":\"https://practicalli.github.io/clojure/GLOSSARY.html\",\"WARC-Payload-Digest\":\"sha1:HHSVRSEKZ3Z3QFFXCJ4KHP3OOEMBZX5A\",\"WARC-Block-Digest\":\"sha1:LPWUGPXSLAPSKCS36ZYUAH5U6V3EARUU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912202728.21_warc_CC-MAIN-20190323060839-20190323082839-00038.warc.gz\"}"}