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 iooption
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/IBM/fp-go/v2/internal/apply"
|
|
|
|
|
"github.com/IBM/fp-go/v2/internal/chain"
|
|
|
|
|
"github.com/IBM/fp-go/v2/internal/functor"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-06 16:18:15 +01:00
|
|
|
// Do creates an empty context of type [S] to be used with the [Bind] operation.
|
|
|
|
|
// This is the starting point for do-notation style composition.
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
//
|
|
|
|
|
// type State struct {
|
|
|
|
|
// Name string
|
|
|
|
|
// Age int
|
|
|
|
|
// }
|
|
|
|
|
// result := iooption.Do(State{})
|
2025-11-06 09:27:00 +01:00
|
|
|
func Do[S any](
|
|
|
|
|
empty S,
|
|
|
|
|
) IOOption[S] {
|
|
|
|
|
return Of(empty)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 16:18:15 +01:00
|
|
|
// Bind attaches the result of a computation to a context [S1] to produce a context [S2].
|
|
|
|
|
// This enables sequential composition where each step can depend on the results of previous steps.
|
|
|
|
|
//
|
|
|
|
|
// The setter function takes the result of the computation and returns a function that
|
|
|
|
|
// updates the context from S1 to S2.
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
//
|
|
|
|
|
// type State struct {
|
|
|
|
|
// Name string
|
|
|
|
|
// Age int
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// result := F.Pipe2(
|
|
|
|
|
// iooption.Do(State{}),
|
|
|
|
|
// iooption.Bind(
|
|
|
|
|
// func(name string) func(State) State {
|
|
|
|
|
// return func(s State) State { s.Name = name; return s }
|
|
|
|
|
// },
|
|
|
|
|
// func(s State) iooption.IOOption[string] {
|
|
|
|
|
// return iooption.FromIO(io.Of("Alice"))
|
|
|
|
|
// },
|
|
|
|
|
// ),
|
|
|
|
|
// iooption.Bind(
|
|
|
|
|
// func(age int) func(State) State {
|
|
|
|
|
// return func(s State) State { s.Age = age; return s }
|
|
|
|
|
// },
|
|
|
|
|
// func(s State) iooption.IOOption[int] {
|
|
|
|
|
// // This can access s.Name from the previous step
|
|
|
|
|
// return iooption.FromIO(io.Of(len(s.Name) * 10))
|
|
|
|
|
// },
|
|
|
|
|
// ),
|
|
|
|
|
// )
|
2025-11-06 09:27:00 +01:00
|
|
|
func Bind[S1, S2, T any](
|
|
|
|
|
setter func(T) func(S1) S2,
|
|
|
|
|
f func(S1) IOOption[T],
|
|
|
|
|
) func(IOOption[S1]) IOOption[S2] {
|
|
|
|
|
return chain.Bind(
|
|
|
|
|
Chain[S1, S2],
|
|
|
|
|
Map[T, S2],
|
|
|
|
|
setter,
|
|
|
|
|
f,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Let attaches the result of a computation to a context [S1] to produce a context [S2]
|
|
|
|
|
func Let[S1, S2, T any](
|
|
|
|
|
setter func(T) func(S1) S2,
|
|
|
|
|
f func(S1) T,
|
|
|
|
|
) func(IOOption[S1]) IOOption[S2] {
|
|
|
|
|
return functor.Let(
|
|
|
|
|
Map[S1, S2],
|
|
|
|
|
setter,
|
|
|
|
|
f,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LetTo attaches the a value to a context [S1] to produce a context [S2]
|
|
|
|
|
func LetTo[S1, S2, T any](
|
|
|
|
|
setter func(T) func(S1) S2,
|
|
|
|
|
b T,
|
|
|
|
|
) func(IOOption[S1]) IOOption[S2] {
|
|
|
|
|
return functor.LetTo(
|
|
|
|
|
Map[S1, S2],
|
|
|
|
|
setter,
|
|
|
|
|
b,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BindTo initializes a new state [S1] from a value [T]
|
|
|
|
|
func BindTo[S1, T any](
|
|
|
|
|
setter func(T) S1,
|
|
|
|
|
) func(IOOption[T]) IOOption[S1] {
|
|
|
|
|
return chain.BindTo(
|
|
|
|
|
Map[T, S1],
|
|
|
|
|
setter,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 16:08:35 +01:00
|
|
|
// ApS attaches a value to a context [S1] to produce a context [S2] by considering
|
|
|
|
|
// the context and the value concurrently (using Applicative rather than Monad).
|
|
|
|
|
// This allows independent computations to be combined without one depending on the result of the other.
|
|
|
|
|
//
|
|
|
|
|
// Unlike Bind, which sequences operations, ApS can be used when operations are independent
|
|
|
|
|
// and can conceptually run in parallel.
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
//
|
|
|
|
|
// type State struct {
|
|
|
|
|
// Name string
|
|
|
|
|
// Age int
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // These operations are independent and can be combined with ApS
|
|
|
|
|
// getName := iooption.Some("Alice")
|
|
|
|
|
// getAge := iooption.Some(30)
|
|
|
|
|
//
|
|
|
|
|
// result := F.Pipe2(
|
|
|
|
|
// iooption.Do(State{}),
|
|
|
|
|
// iooption.ApS(
|
|
|
|
|
// func(name string) func(State) State {
|
|
|
|
|
// return func(s State) State { s.Name = name; return s }
|
|
|
|
|
// },
|
|
|
|
|
// getName,
|
|
|
|
|
// ),
|
|
|
|
|
// iooption.ApS(
|
|
|
|
|
// func(age int) func(State) State {
|
|
|
|
|
// return func(s State) State { s.Age = age; return s }
|
|
|
|
|
// },
|
|
|
|
|
// getAge,
|
|
|
|
|
// ),
|
|
|
|
|
// )
|
2025-11-06 09:27:00 +01:00
|
|
|
func ApS[S1, S2, T any](
|
|
|
|
|
setter func(T) func(S1) S2,
|
|
|
|
|
fa IOOption[T],
|
|
|
|
|
) func(IOOption[S1]) IOOption[S2] {
|
|
|
|
|
return apply.ApS(
|
|
|
|
|
Ap[S2, T],
|
|
|
|
|
Map[S1, func(T) S2],
|
|
|
|
|
setter,
|
|
|
|
|
fa,
|
|
|
|
|
)
|
|
|
|
|
}
|