Skip to content

Instantly share code, notes, and snippets.

@jonpemby
Created May 9, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonpemby/5c178763d8635ae976c3ea182a49bda0 to your computer and use it in GitHub Desktop.
Save jonpemby/5c178763d8635ae976c3ea182a49bda0 to your computer and use it in GitHub Desktop.
Collection macro to peek backwards at the last value in a map.
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