Is there any good example of use cases for angular.identity()?
Asked Answered
F

4

14

According to the documentation

A function that returns its first argument. This function is useful when writing code in the functional style.

I am wondering where I can find a good example of such use case - writing code in functional style in an angular app. Thanks

Flannery answered 14/3, 2013 at 22:34 Comment(0)
S
15

Example from the AngularJS source code:

function transformer(transformationFn, value) {
  return (transformationFn || angular.identity)(value);
};

Explanation:

In case transformationFn is provided as first parameter, it will be called with value as it's own parameter. Otherwise identity function will be called with the same value.

As ng source code mentions:

This function is useful when writing code in the functional style.

What this means is that in functional programming there are no globals, so you always have to pass/inject in everything you need.

Sagittate answered 14/3, 2013 at 23:19 Comment(2)
I didn't understand why we call angular.identity() function (when transformationFn is undefined) and what it does on calling.Guiscard
It's basically used here as a default implementation - when no transformation function is supplied, just fall back to the identity (ie. no transformation).Catalog
F
9

Lets say we have these two below functions:

$scope.square = function(n) {
return n * n
};


$scope.multplybyTwo = function(n) {
return n * 2
};

To call this in a functional way:

$scope.givemeResult = function(fn, val) {
return (fn || angular.identity)(val);
};

Then You may use the above function something like below:

$scope.initVal = 5;
$scope.squareResult = $scope.givemeResult($scope.square, $scope.initVal);
$scope.intoTwo = $scope.givemeResult($scope.multplybyTwo, $scope.initVal);

You may follow the below link:

http://litutech.blogspot.in/2014/02/angularidentity-example.html

Freakish answered 19/2, 2014 at 13:18 Comment(1)
Welcome to Stack Exchange. This is a questions and answers site, not a link collection. Please include relevant content in your answer, not just a link to where the content may be. The link is nice to have in addition for reference or for further information. For more tips, see How to answer.Italy
O
7

Correct me if I'm wrong, but my understanding is that this

function transformer(transformationFn, value) {
  return (transformationFn || angular.identity)(value);
};

could be "un-refactored" to this

function transformer(transformationFn, value) {
  if (transformationFn) {
    return transformationFn(value);
  } else {
    return angular.identity(value);
  }
};

which would be functionally equivalent to this identity-less version:

function transformer(transformationFn, value) {
  if (transformationFn) {
    return transformationFn(value);
  } else {
    return value;
  }
};

So I guess the use case would be when you want to apply a certain transformation to a value when you're supplied with something that may or may not actually exist.

I wanted to better explain the identity function (as I understand it), although as I review my answer I don't think I really answered your question. Leaving my answer here anyway in case it's helpful.

Oversubscribe answered 30/4, 2015 at 14:0 Comment(1)
A clear explanation of what angular.identity acually does, thanks!Dirigible
F
4

angular.identity is useful for cases where you want to reference an object bound to a parent by passing it from a parent to its children.

An identity function is like the zero for functions. Kind of useless by itself, but occasionally useful as part of an expression using higher-order functions, where you can take a function as a parameter or return it as a result.

Here is an example for array values:

For example, you may bind a two-dimensional array to an initial selection, and then bind the contained inner arrays to each subselection. The values function in this case is the identity function: it is invoked for each group of child elements, being passed the data bound to the parent element, and returns this array of data.

Use it when it is necessary to pass a dummy function to:

  • a promise ($q)
  • a compiler ($compile)
  • a unit test (spy/mock)

which acts as a data source or pump for a pipe|filter.

By comparison, Angular.noop incorrectly handles resolving/rejecting promises correctly because it always returns undefined, whereas Angular.identity correctly handles resolving/rejecting promises because it always returns the argument passed to it( f(x) = x ).

In terms of constructors in Angular, it is relevant as such:

JavaScript engines only return the instance of a constructor if the constructor does not explicitly return an object (i.e. a value of the type object or function). Hence new identity(value) is always an object. Thus new identity(value) == value only returns true if value is an object. This is because the equality operators in JavaScript always check for identity when one of the operands is an object.

References

Fuchs answered 2/10, 2015 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.