Last active
June 12, 2020 09:50
-
-
Save zoetrope/f692d44f3625824fbde9283e7a77767e to your computer and use it in GitHub Desktop.
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 ( | |
"bufio" | |
"context" | |
"crypto/sha256" | |
"encoding/hex" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"sort" | |
"github.com/reactivex/rxgo/v2" | |
) | |
type line struct { | |
text string | |
num int | |
} | |
func main() { | |
flag.Parse() | |
if len(flag.Args()) != 1 { | |
log.Fatal("please input filepath") | |
} | |
file, err := os.Open(flag.Arg(0)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
lines := make(chan rxgo.Item) | |
go func() { | |
defer close(lines) | |
scanner := bufio.NewScanner(file) | |
for i := 0; scanner.Scan(); i++ { | |
lines <- rxgo.Of(line{scanner.Text(), i}) | |
} | |
}() | |
result, err := rxgo.FromChannel(lines).Map(func(ctx context.Context, i interface{}) (interface{}, error) { | |
l := i.(line) | |
sum := sha256.Sum256([]byte(l.text)) | |
return line{ | |
hex.EncodeToString(sum[:]), | |
l.num, | |
}, nil | |
}, rxgo.WithCPUPool()).ToSlice(0) | |
if err != nil { | |
log.Fatal(err) | |
} | |
sort.Slice(result, func(i, j int) bool { | |
return result[i].(line).num < result[j].(line).num | |
}) | |
for _, r := range result { | |
fmt.Printf("%d: %s\n", r.(line).num, r.(line).text) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment