1

I need help to understand below code which is from solidity offical documents "how to use internal function types". as it is very confusing for me.

  1. How to use library in solidity.
  2. Some one explain the function map in details as it is confusing for me.
  3. Why we use 'f' at end of function.

Please refer below code

pragma solidity >= 0.4 .16 < 0.6 .0;

library ArrayUtils {

  function map(uint[] memory self, function (uint) pure returns(uint) f)
    internal
    pure
    returns(uint[] memory r) 
  {
    r = new uint[](self.length);
    for (uint i = 0; i < self.length; i++) {
      r[i] = f(self[i]);
    }
  }

  function reduce(
    uint[] memory self,
    function (uint, uint) pure returns(uint) f)
    internal
    pure
    returns(uint r) 
  {
    r = self[0];
    for (uint i = 1; i < self.length; i++) {
      r = f(r, self[i]);
    }
  }
}
  • 1
    f is just the name of that parameter (which is a function). You may want to look up "functional programming" and "first-class functions." – smarx Dec 20 '18 at 18:01
  • Thanks@smarx for quick respons now i am little clear for ' f '. however I am still confuse on coding part which I mentioned above. – Abhijeet Samanta Dec 20 '18 at 18:16
1

I assume that you are referring this section of solidity documentation.

1. How to use library in solidity?

You can read about the use of libraries here. In short, we usually put the library in the same file and define its usage by using 'Using For'.The directive using A for B; can be used to attach library functions (from the library A) to any type (B).

2. Some one explain the function map in details as it is confusing for me.

map function is accepting two parameters. One is array self and another parameter is function type. I assume as per your question that you have gone through it.

function parameters of function type can be used to pass functions to and return functions from function calls

So function map accept a function as a parameter (which is possible in many other languages too).

r = new uint[](self.length);

Here map() creates a new array with same length as passed self array.

for (uint i = 0; i < self.length; i++) {
      r[i] = f(self[i]);
    }

Here is iterates over passed array and then invoke the passed function f by passing the parameter as an element of self array and storing the function result in similar index of array r.

3. Why we use 'f' at end of function?

As mentioned above, by using f, we are invoking passed function.

Additional Explanation:

    using ArrayUtils for *;
    function pyramid(uint l) public pure returns (uint) {
        return ArrayUtils.range(l).map(square).reduce(sum);
      }
      function square(uint x) internal pure returns (uint) {
        return x * x;
      }
      function sum(uint x, uint y) internal pure returns (uint) {
        return x + y;
      }

This is the way all library functions are being invoked in the example.

The effect of using A for *; is that the functions from the library A are attached to any type. So any object type can be used as the first parameter of the library function

ArrayUtils.range(l).map(square).reduce(sum);

In this line, it first calls ArrayUtils.range(l).

Suppose l is 5, it will return an array as [0,1,2,3,4]. SO next call will be [0,1,2,3,4].map(square) where [0,1,2,3,4] will work as the first parameter for the function.

Next map() will return an array r with square of each number as [0,1,4,9,16]. SO next call will be [0,1,4,9,16].reduce(sum) where [0,1,4,9,16] will work as a first parameter for the reduce function.

And finally it will call reduce and as per the passed function will return the sum of full array i.e 30.

Function type ensures the structure of passed function. I hope it answers your each question.

  • WOW thanks@A.K. for such good explanation of my query. Thanks lot. now I am getting things. – Abhijeet Samanta Dec 21 '18 at 15:05
  • Your welcome @AbhijeetSamanta – Aniket Dec 24 '18 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.