Last active
November 22, 2021 21:22
-
-
Save jordanorelli/ae7ce33bf6dab3efe947c046feb8e631 to your computer and use it in GitHub Desktop.
concurrent list evaluation with generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this runs and produces an output of
[200, 200, 200]
against this commit: https://github.com/jordanorelli/generic/blob/9a9508a22aa801919a108de1b1f409d08ec5a9ca/list/list.gothat 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.