diff --git a/README.md b/README.md index a4ce2fb..c52dfe3 100644 --- a/README.md +++ b/README.md @@ -19,89 +19,25 @@ Rust inspired implementation of `Option[T]` and `Result[T]` for Go. // vanilla v, err := strconv.Atoi("1") if err != nil { - v = 1000 + v = 1000 } // shine -r := shine.NewResult(strconv.Atoi("1")) // Result[int] -v := r.UnwrapOr(1000) +v := shine.NewResult(strconv.Atoi("1")).UnwrapOr(1000) ``` #### Parsing URLs ```golang -// vanilla -u, err := url.Parse("https://github.com/") -if err != nil { - return err -} -hostname := u.Host - -// shine -r := shine.NewResult(url.Parse("https://github.com/")) // Result[*url.URL] -return shine.ResultMap(r, func(u *url.URL) string{ - return u.Host -}) -``` - -#### Reading JSON -```golang -// vanilla -func jsonRead(fname string) (map[string]any, error) { - f, err := os.Open(fname) +func ExampleVanilla() string { + u, err := url.Parse("https://github.com/") if err != nil { - return nil, err + return "" } - - defer f.Close() - dec := json.NewDecoder(f) - var res map[string]any - - return res, dec.Decode(&res) -} - - -// shine -func jsonRead(fname string) Result[map[string]any] { - r := shine.NewResult(os.Open(fname)) - - return shine.ResultAndThen(r, func(f *os.File) Result[map[string]any] { - defer f.Close() - - dec := json.NewDecoder(f) - var res map[string]any - - return shine.NewResult(res, dec.Decode(&res)) - }) -} -``` - -### Interoperability with `(T, error)` returns -```golang -func something() (string, error) { - return shine.NewOk("hello").UnwrapBoth() // (string, error) -} -``` - -## Performance -Since both `Result` and `Option` are wrapper-types and not natively built into the language like in Rust, they come with some overhead. -Given the following benchmark: -```golang -// vanilla -something, err := getSomethingErrornous() -if err == nil { - doSomething(something)// no-op -} else { - doSomethingElse(err)// no-op + return u.Hostname() } -// shine -r := shine.NewResult(getSomethingErrornous()) -if r.IsOk() { - doSomething(r.Unwrap())// no-op -} else { - doSomethingElse(r.UnwrapErr())// no-op +func ExampleShine() string { + return shine.ResMap(shine.NewResult(url.Parse("https://github.com/")), (*url.URL).Hostname).UnwrapOrDefault() } ``` - -shine is 3 times slower than vanilla, with vanilla being at `1.248 ns/op` and shine at `3.617 ns/op` (MacBook Pro 2021 M1 Pro)