Skip to content

Add frequencies function#262

Merged
phanan merged 1 commit intomainfrom
feature/frequencies
Mar 21, 2026
Merged

Add frequencies function#262
phanan merged 1 commit intomainfrom
feature/frequencies

Conversation

@phanan
Copy link
Collaborator

@phanan phanan commented Mar 21, 2026

Summary

  • Adds frequencies($collection, ?callable $callback), which returns an array mapping each distinct value to the number of times it appears in the collection
  • An optional callback can be provided to count by a derived key (receives $element, $index, $collection)
  • Inspired by Clojure's frequencies and Ruby's Enumerable#tally
use function Functional\frequencies;

$data = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple'];
frequencies($data); // ['apple' => 3, 'banana' => 2, 'cherry' => 1]

// With callback
$words = ['hi', 'hello', 'hey', 'goodbye', 'go'];
frequencies($words, fn ($w) => strlen($w)); // [2 => 2, 5 => 2, 3 => 1, 7 => 1]

Test plan

  • Unit tests covering arrays, iterators, callbacks, empty collections, numeric values, invalid keys, and invalid arguments (7 tests, 19 assertions)

…umerable#tally`

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@phanan phanan merged commit c9d87fb into main Mar 21, 2026
10 checks passed
@phanan phanan deleted the feature/frequencies branch March 21, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant