Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions src/Loopinator.sol
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
// 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; }
}

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; }
}
}

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; }
}
Expand Down