Coder Social home page Coder Social logo

loopinator's Introduction

Loopinator

Array looping functions for Solidity.

Current Implementation

uint256[].map

The map function takes a uint256[] memory and a function that takes a uint256 and returns a uint256.

This will loop over the array, applying the function to each element, and returning the new array.

Usage:

// import
import { map } from "./Loopinator.sol";

// apply to use `array.map` syntax
using { map } for uint256[]

// define function for each iteration
function double(uint256 a) pure returns (uint256) {
    return a * 2;
}

contract MyContract {

    function doubleTheNumbers(uint256[] memory numbers) public pure returns (uint256[] memory) {

        return numbers.map(double);

    }

}

uint256[].forEach

The forEach function takes a uint256[] memory and a function that takes a uint256.

This will loop over the array, executing the function on each element.

Usage:

// import
import { forEach } from "./Loopinator.sol";

// apply to use `array.forEach` syntax
using { forEach } for uint256[]

// define function for each iteration
function notZero(uint256 a) pure returns (uint256) {
    require(a != 0, "number is invalid");
}

contract MyContract {

    function allMustBeNonZero(uint256[] memory numbers) public pure {

        numbers.forEach(notZero);

    }

}

uint256[].reduce

The reduce function takes uint256[] memory, a function that takes two uint256 values (the reduced value and the current value, respectively) and returns a uint256 representing the new reduced value, and an initial value.

This will loop over the array, executing the function on the reduced value and each element, and finally returning the reduced value.

Usage:

// import
import { reduce } from "./Loopinator.sol";

// apply to use `array.reduce` syntax
using { reduce } for uint256[]

// define function for each iteration
function sum(uint256 lastValue, uint256 element) internal pure returns (uint256) {
    return lastValue + element;
}

contract MyContract {

    function sumTheNumbers(uint256[] memory numbers) public pure returns (uint256) {

        uint256 initialValue;

        return numbers.reduce(sum, initialValue);

    }

}

loopinator's People

Contributors

grgred avatar jtriley-eth avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.