go-04-map-concurrent
1.000
Proposed solution
```go
package challenge
import (
"sync"
)
func MapConcurrent(inputs []int, workers int, fn func(int) int) []int {
if inputs == nil || len(inputs) == 0 {
return []int{}
}
results := make([]int, len(inputs))
var wg sync.WaitGroup
semaphore := make(chan struct{}, workers)
for i, input := range inputs {
wg.Add(1)
semaphore <- struct{}{} // Acquire a slot in the semaphore
go func(i, input int) {
defer wg.Done()
results[i] = fn(input)
<-semaphore // Release the slot
}(i, input)
}
wg.Wait()
return results
}
```Test output (stdout)
81783+02:00","Action":"run","Package":"challenge","Test":"TestMapConcurrentMatchesSequential/negatives"}
{"Time":"2026-06-30T05:53:24.263183133+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentMatchesSequential/negatives","Output":"=== RUN TestMapConcurrentMatchesSequential/negatives\n"}
{"Time":"2026-06-30T05:53:24.263184303+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentMatchesSequential/negatives","Output":"--- PASS: TestMapConcurrentMatchesSequential/negatives (0.00s)\n"}
{"Time":"2026-06-30T05:53:24.263185413+02:00","Action":"pass","Package":"challenge","Test":"TestMapConcurrentMatchesSequential/negatives","Elapsed":0}
{"Time":"2026-06-30T05:53:24.263186463+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentMatchesSequential","Output":"--- PASS: TestMapConcurrentMatchesSequential (0.00s)\n"}
{"Time":"2026-06-30T05:53:24.263187773+02:00","Action":"pass","Package":"challenge","Test":"TestMapConcurrentMatchesSequential","Elapsed":0}
{"Time":"2026-06-30T05:53:24.263188743+02:00","Action":"run","Package":"challenge","Test":"TestMapConcurrentOrderPreserved"}
{"Time":"2026-06-30T05:53:24.263189873+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentOrderPreserved","Output":"=== RUN TestMapConcurrentOrderPreserved\n"}
{"Time":"2026-06-30T05:53:24.263303134+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentOrderPreserved","Output":"--- PASS: TestMapConcurrentOrderPreserved (0.00s)\n"}
{"Time":"2026-06-30T05:53:24.263304564+02:00","Action":"pass","Package":"challenge","Test":"TestMapConcurrentOrderPreserved","Elapsed":0}
{"Time":"2026-06-30T05:53:24.263315404+02:00","Action":"run","Package":"challenge","Test":"TestMapConcurrentEmptyReturnsLenZero"}
{"Time":"2026-06-30T05:53:24.263316795+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentEmptyReturnsLenZero","Output":"=== RUN TestMapConcurrentEmptyReturnsLenZero\n"}
{"Time":"2026-06-30T05:53:24.263329865+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentEmptyReturnsLenZero","Output":"--- PASS: TestMapConcurrentEmptyReturnsLenZero (0.00s)\n"}
{"Time":"2026-06-30T05:53:24.263331145+02:00","Action":"pass","Package":"challenge","Test":"TestMapConcurrentEmptyReturnsLenZero","Elapsed":0}
{"Time":"2026-06-30T05:53:24.263332315+02:00","Action":"run","Package":"challenge","Test":"TestMapConcurrentRespectsWorkerLimit"}
{"Time":"2026-06-30T05:53:24.263333105+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentRespectsWorkerLimit","Output":"=== RUN TestMapConcurrentRespectsWorkerLimit\n"}
{"Time":"2026-06-30T05:53:24.263430526+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentRespectsWorkerLimit","Output":"--- PASS: TestMapConcurrentRespectsWorkerLimit (0.00s)\n"}
{"Time":"2026-06-30T05:53:24.263446566+02:00","Action":"pass","Package":"challenge","Test":"TestMapConcurrentRespectsWorkerLimit","Elapsed":0}
{"Time":"2026-06-30T05:53:24.263450486+02:00","Action":"run","Package":"challenge","Test":"TestMapConcurrentCallsFnOncePerElement"}
{"Time":"2026-06-30T05:53:24.263451706+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentCallsFnOncePerElement","Output":"=== RUN TestMapConcurrentCallsFnOncePerElement\n"}
{"Time":"2026-06-30T05:53:24.263453446+02:00","Action":"output","Package":"challenge","Test":"TestMapConcurrentCallsFnOncePerElement","Output":"--- PASS: TestMapConcurrentCallsFnOncePerElement (0.00s)\n"}
{"Time":"2026-06-30T05:53:24.263459126+02:00","Action":"pass","Package":"challenge","Test":"TestMapConcurrentCallsFnOncePerElement","Elapsed":0}
{"Time":"2026-06-30T05:53:24.263460796+02:00","Action":"output","Package":"challenge","Output":"PASS\n"}
{"Time":"2026-06-30T05:53:24.263539337+02:00","Action":"output","Package":"challenge","Output":"ok \tchallenge\t0.001s\n"}
{"Time":"2026-06-30T05:53:24.263551827+02:00","Action":"pass","Package":"challenge","Elapsed":0.001}
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.