From f53366aedf3193fe6fd3a3d45acfb3ebabebd5d5 Mon Sep 17 00:00:00 2001 From: Vladislav <81467635+grGred@users.noreply.github.com> Date: Sun, 17 Jul 2022 15:10:00 +0300 Subject: [PATCH] gas optimization + prettier --- src/Loopinator.sol | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Loopinator.sol b/src/Loopinator.sol index 0c5eeb5..91738f2 100644 --- a/src/Loopinator.sol +++ b/src/Loopinator.sol @@ -1,15 +1,11 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.14; -function map( - uint256[] memory numbers, - function (uint256) pure returns (uint256) func -) pure returns (uint256[] memory) { +function map(uint256[] memory numbers, function(uint256) pure returns (uint256) func) pure returns (uint256[] memory) { uint256 length = numbers.length; uint256[] memory newNumbers = new uint256[](length); - uint256 i; - for (i; i < length; ) { + for (uint256 i; i < length; ) { newNumbers[i] = func(numbers[i]); unchecked { ++i; } } @@ -17,14 +13,10 @@ function map( return newNumbers; } -function forEach( - uint256[] memory numbers, - function (uint256) pure func -) pure { +function forEach(uint256[] memory numbers, function(uint256) pure func) pure { uint256 length = numbers.length; - uint256 i; - for (i; i < length; ) { + for (uint256 i; i < length; ) { func(numbers[i]); unchecked { ++i; } } @@ -32,14 +24,13 @@ function forEach( function reduce( uint256[] memory numbers, - function (uint256, uint256) pure returns (uint256) func, + function(uint256, uint256) pure returns (uint256) func, uint256 initialValue ) pure returns (uint256) { uint256 result = initialValue; uint256 length = numbers.length; - uint256 i; - for (i; i < length; ) { + for (uint256 i; i < length; ) { result = func(result, numbers[i]); unchecked { ++i; } }