This commit is contained in:
a 2022-03-26 21:21:48 -05:00
parent 6f3654e147
commit cd46986e57
1 changed files with 11 additions and 9 deletions

View File

@ -20,7 +20,8 @@ func (r *RequestContext[T]) String() string {
return fmt.Sprintf("%+v", r.ans) return fmt.Sprintf("%+v", r.ans)
} }
func do[T any](r *RequestContext[T]) *RequestContext[T] { func (r *RequestContext[T]) Do() func(r *RequestContext[T]) *RequestContext[T] {
return func(r *RequestContext[T]) *RequestContext[T] {
r.ans = *new(T) r.ans = *new(T)
res, err := http.DefaultClient.Do(&r.req) res, err := http.DefaultClient.Do(&r.req)
if err != nil { if err != nil {
@ -30,6 +31,7 @@ func do[T any](r *RequestContext[T]) *RequestContext[T] {
defer res.Body.Close() defer res.Body.Close()
r.err = json.NewDecoder(res.Body).Decode(&r.ans) r.err = json.NewDecoder(res.Body).Decode(&r.ans)
return r return r
}
} }
type someResult struct { type someResult struct {
@ -45,7 +47,7 @@ func main() {
req: *r, req: *r,
} }
} }
results := lambda.Filter(parallel.Map(requests, do[someResult], 4), func(r *RequestContext[someResult]) bool { results := lambda.Filter(parallel.Map(requests, requests[0].Do(), 4), func(r *RequestContext[someResult]) bool {
return r.err == nil return r.err == nil
}) })
for _, v := range results { for _, v := range results {