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

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

Example from the AngularJS source code:

function transformer(transformationFn, value) {
  return (transformationFn || 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.

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

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 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.