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

前端 未结 4 1769
一整个雨季
一整个雨季 2021-02-19 10:03

According to the documentation

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

I

相关标签:
4条回答
  • 2021-02-19 10:09

    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.

    0 讨论(0)
  • 2021-02-19 10:09

    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.

    0 讨论(0)
  • 2021-02-19 10:12

    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

    0 讨论(0)
  • 2021-02-19 10:26

    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

    • Purpose of an 'Identity Function'?

    • What is Angular.noop used for?

    • What is the practical use of the identity function in R?

    • What do people use the identity function for?

    • Use of the identity function in JavaScript

    • Where and why is identity function useful?

    • How do I pretty print an XSLT result document with removed source elements?

    • d3 wiki: Selections

    • AngularJS Issue #4579: orderBy filter doesn't support sorting by the value itself

    • Promises and the danger of angular.noop

    • The Identity Monad

    • Function composition and the identity function

    • Cocoa Drawing Guide: Transforms - The Identity Transform

    • Understanding Fluent APIs

    • Please explain usage of _.identity(value) of underscore.js

    • https://softwareengineering.stackexchange.com/questions/256090/function-only-returns-unchanged-parameter-useless

    0 讨论(0)
提交回复
热议问题