Created
May 9, 2018 19:42
-
-
Save jonpemby/5c178763d8635ae976c3ea182a49bda0 to your computer and use it in GitHub Desktop.
Collection macro to peek backwards at the last value in a map.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Collection::macro('peekMap', function (callable $callback) { | |
$lastValue = null; | |
return $this->map(function ($value, $key) use ($callback, $lastValue) { | |
$newValue = $callback($value, $key, $lastValue); | |
$lastValue = $newValue; | |
return $newValue; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment