Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Last active November 22, 2021 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanorelli/ae7ce33bf6dab3efe947c046feb8e631 to your computer and use it in GitHub Desktop.
Save jordanorelli/ae7ce33bf6dab3efe947c046feb8e631 to your computer and use it in GitHub Desktop.
concurrent list evaluation with generics
package main
import (
"fmt"
"github.com/jordanorelli/generic/list"
"net/http"
"net/url"
)
func status(path string) int {
u, _ := url.Parse(path)
if u.Scheme == "" {
u.Scheme = "http"
}
res, _ := http.Get(u.String())
return res.StatusCode
}
func main() {
// urls if of type list.List[string]. Note that we don't have to supply
// the type parameter of string, the Go compiler infers it for us
urls := list.Make("google.com", "jackbox.com", "twitter.com")
// all is of type list.List[int]. The function `status` is run for
// every element of the urls list in its own goroutine and their
// results are collected into a new list. Note that we don't have to supply
// the type parameter of func(string) int, the compiler infers it for us.
all := list.Run(urls, status)
// since generics can define their own methods, they can satisfy
// interfaces; I made list.List satisfy stringer so it prints nicely.
// this prints [200, 200, 200].
fmt.Println(all)
}
@jordanorelli
Copy link
Author

this runs and produces an output of [200, 200, 200] against this commit: https://github.com/jordanorelli/generic/blob/9a9508a22aa801919a108de1b1f409d08ec5a9ca/list/list.go

that package is totally just me messing around and isn't expected to stay stable, I'm just trying to figure out how this stuff is going to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment