underscore/transpose.go
Ruidy f33e86d502
feat: add Tap, Transpose, Unzip, ParallelReduce, and Replicate (#49)
* feat: add Tap, Transpose, Unzip, ParallelReduce, and Replicate

- Add Tap: for side effects/debugging in pipelines
- Add Transpose: flip matrix rows and columns
- Add Unzip: split tuple slice into two slices
- Add ParallelReduce: parallel reduction (experimental)
- Add Replicate: create n copies of a value

Comprehensive tests included for all functions.

Resolves Issues 21, 22, 23, 24, 25

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* test: improve ParallelReduce test coverage to 97.5%

Add comprehensive tests covering:
- Default workers (workers <= 0)
- Negative workers
- Error handling and propagation
- Context cancellation during execution
- Context timeout
- Single element processing
- Many workers (more workers than elements)
- Benchmark for performance validation

Coverage increased from 68.75% to 97.5%

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-16 09:02:47 +01:00

25 lines
614 B
Go

package underscore
// Transpose flips a matrix over its diagonal, swapping rows and columns.
// Returns an empty slice if the input is empty.
// Assumes all rows have the same length (uses the length of the first row).
//
// Example: Transpose([[1,2,3], [4,5,6]]) → [[1,4], [2,5], [3,6]]
func Transpose[T any](matrix [][]T) [][]T {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return [][]T{}
}
rows := len(matrix)
cols := len(matrix[0])
result := make([][]T, cols)
for i := range result {
result[i] = make([]T, rows)
for j := range matrix {
result[i][j] = matrix[j][i]
}
}
return result
}