Skip to content

Conversation

@magik092
Copy link

I needed functionality similar to PHP's array_chunk() but for immutable Set collections. This inspired me to implement sliding() and grouped() methods, bringing Munus closer to feature parity with Vavr (the Java functional library).

What's Changed

  • ✅ Set::sliding(int $size, int $step): Set<Set> - Creates a sliding window over Set elements
  • ✅ Set::grouped(int $size): Set<Set> - Groups elements into fixed-size chunks (delegates to sliding($size, $size))

Implementation Details

  • Performance optimized: Uses array_slice() for efficient memory management with PHP's copy-on-write
  • Immutability preserved: Returns new Set instances without modifying the original
  • Comprehensive test coverage: 8 test cases covering various scenarios including:
    • Empty sets
    • Different window sizes and step values
    • Edge cases with remainder elements
    • Comparison between sliding() and grouped() behavior

API Examples

// Sliding window with overlap
$set = Set::of(1, 2, 3, 4, 5, 6, 7);
$result = $set->sliding(3, 1);
// Returns: Set(Set(1,2,3), Set(2,3,4), Set(3,4,5), Set(4,5,6), Set(5,6,7), Set(6,7), Set(7))
     
// Grouped chunks
$set = Set::of(1, 2, 3, 4, 5);
$result = $set->grouped(2);
// Returns: Set(Set(1,2), Set(3,4), Set(5))

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