2025-11-06 09:27:00 +01:00
|
|
|
// Copyright (c) 2023 - 2025 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 mostlyadequate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
A "github.com/IBM/fp-go/v2/array"
|
|
|
|
|
E "github.com/IBM/fp-go/v2/either"
|
|
|
|
|
"github.com/IBM/fp-go/v2/errors"
|
|
|
|
|
F "github.com/IBM/fp-go/v2/function"
|
2025-11-10 16:26:52 +01:00
|
|
|
"github.com/IBM/fp-go/v2/ioresult"
|
2025-11-06 09:27:00 +01:00
|
|
|
O "github.com/IBM/fp-go/v2/option"
|
|
|
|
|
P "github.com/IBM/fp-go/v2/predicate"
|
|
|
|
|
S "github.com/IBM/fp-go/v2/string"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
// httpGet :: Route -> Task Error JSON
|
|
|
|
|
httpGet = F.Flow2(
|
|
|
|
|
S.Format[string]("json for %s"),
|
2025-11-10 16:26:52 +01:00
|
|
|
ioresult.Of[string],
|
2025-11-06 09:27:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// routes :: Map Route Route
|
|
|
|
|
routes = map[string]string{
|
|
|
|
|
"/": "/",
|
|
|
|
|
"/about": "/about",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate :: Player -> Either error Player
|
|
|
|
|
validatePlayer = E.FromPredicate(P.ContraMap(Player.getName)(S.IsNonEmpty), F.Flow2(Player.getID, errors.OnSome[int]("player %d must have a name")))
|
|
|
|
|
|
|
|
|
|
// readfile :: String -> String -> Task Error String
|
2025-11-10 16:26:52 +01:00
|
|
|
readfile = F.Curry2(func(encoding, file string) IOResult[string] {
|
|
|
|
|
return ioresult.Of(fmt.Sprintf("content of %s (%s)", file, encoding))
|
2025-11-06 09:27:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// readdir :: String -> Task Error [String]
|
2025-11-10 16:26:52 +01:00
|
|
|
readdir = ioresult.Of(A.From("file1", "file2", "file3"))
|
2025-11-06 09:27:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Example_solution12A() {
|
|
|
|
|
// getJsons :: Map Route Route -> Task Error (Map Route JSON)
|
2025-11-10 16:26:52 +01:00
|
|
|
getJsons := ioresult.TraverseRecord[string](httpGet)
|
2025-11-06 09:27:00 +01:00
|
|
|
|
|
|
|
|
fmt.Println(getJsons(routes)())
|
|
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// Right[map[string]string](map[/:json for / /about:json for /about])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Example_solution12B() {
|
|
|
|
|
// startGame :: [Player] -> [Either Error String]
|
|
|
|
|
startGame := F.Flow2(
|
|
|
|
|
E.TraverseArray(validatePlayer),
|
|
|
|
|
E.MapTo[error, []Player]("Game started"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
fmt.Println(startGame(A.From(playerAlbert, playerTheresa)))
|
|
|
|
|
fmt.Println(startGame(A.From(playerAlbert, Player{Id: 4})))
|
|
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// Right[string](Game started)
|
|
|
|
|
// Left[*errors.errorString](player 4 must have a name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Example_solution12C() {
|
|
|
|
|
traverseO := O.Traverse[string](
|
2025-11-10 16:26:52 +01:00
|
|
|
ioresult.Of[Option[string]],
|
|
|
|
|
ioresult.Map[string, Option[string]],
|
2025-11-06 09:27:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// readFirst :: String -> Task Error (Maybe String)
|
|
|
|
|
readFirst := F.Pipe2(
|
|
|
|
|
readdir,
|
2025-11-10 16:26:52 +01:00
|
|
|
ioresult.Map(A.Head[string]),
|
|
|
|
|
ioresult.Chain(traverseO(readfile("utf-8"))),
|
2025-11-06 09:27:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
fmt.Println(readFirst())
|
|
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// Right[option.Option[string]](Some[string](content of file1 (utf-8)))
|
|
|
|
|
}
|