diff --git a/samples/getting-started-with-fp-ts/reader/example1/reader_test.go b/samples/getting-started-with-fp-ts/reader/example1/reader_test.go index 21ff42d..ac33e23 100644 --- a/samples/getting-started-with-fp-ts/reader/example1/reader_test.go +++ b/samples/getting-started-with-fp-ts/reader/example1/reader_test.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package reader implements the example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5] +// Package example1 implements the first example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5] package example1 import ( diff --git a/samples/getting-started-with-fp-ts/reader/example2/reader_test.go b/samples/getting-started-with-fp-ts/reader/example2/reader_test.go index 15a0ee6..2c89fa1 100644 --- a/samples/getting-started-with-fp-ts/reader/example2/reader_test.go +++ b/samples/getting-started-with-fp-ts/reader/example2/reader_test.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package reader implements the example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5] +// Package example2 implements the second example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5] package example2 import ( diff --git a/samples/readerioeither/example1/reader_test.go b/samples/readerioeither/example1/reader_test.go new file mode 100644 index 0000000..5baf39d --- /dev/null +++ b/samples/readerioeither/example1/reader_test.go @@ -0,0 +1,89 @@ +// Copyright (c) 2023 IBM Corp. +// All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package example1 + +import ( + "os" + + B "github.com/IBM/fp-go/bytes" + E "github.com/IBM/fp-go/either" + F "github.com/IBM/fp-go/function" + I "github.com/IBM/fp-go/identity" + IOE "github.com/IBM/fp-go/ioeither" + J "github.com/IBM/fp-go/json" + RIOE "github.com/IBM/fp-go/readerioeither" +) + +type ( + WriterType = func([]byte) IOE.IOEither[error, []byte] + + Dependencies struct { + Writer WriterType + } +) + +func getWriter(deps *Dependencies) WriterType { + return deps.Writer +} + +// SerializeToWriter marshals the input to JSON and persists the result via the [Writer] passed in via the [*Dependencies] +func SerializeToWriter[A any](data A) RIOE.ReaderIOEither[*Dependencies, error, []byte] { + return F.Pipe1( + RIOE.Ask[*Dependencies, error](), + RIOE.ChainIOEitherK[*Dependencies](F.Flow2( + getWriter, + F.Pipe2( + data, + J.Marshal[A], + E.Fold(F.Flow2( + IOE.Left[[]byte, error], + F.Constant1[WriterType, IOE.IOEither[error, []byte]], + ), I.Ap[IOE.IOEither[error, []byte], []byte]), + ), + )), + ) +} + +func ExampleReaderIOEither() { + + // writeToStdOut implements a writer to stdout + writeToStdOut := func(data []byte) IOE.IOEither[error, []byte] { + return IOE.TryCatchError(func() ([]byte, error) { + _, err := os.Stdout.Write(data) + return data, err + }) + } + + deps := Dependencies{ + Writer: writeToStdOut, + } + + data := map[string]string{ + "a": "b", + "c": "d", + } + + // writeData will persist to a configurable target + writeData := F.Pipe1( + SerializeToWriter(data), + RIOE.Map[*Dependencies, error](B.ToString), + ) + + _ = writeData(&deps)() + + // Output: + // {"a":"b","c":"d"} +}