@@ -7,11 +7,108 @@ import (
77 RIO "github.com/IBM/fp-go/v2/readerio"
88)
99
10+ // SequenceReader transforms a ReaderIO containing a Reader into a Reader containing a ReaderIO.
11+ // This "flips" the nested structure, allowing you to provide the Reader's environment first,
12+ // then get a ReaderIO that can be executed with a context.
13+ //
14+ // Type transformation:
15+ //
16+ // From: ReaderIO[Reader[R, A]]
17+ // = func(context.Context) func() func(R) A
18+ //
19+ // To: Reader[R, ReaderIO[A]]
20+ // = func(R) func(context.Context) func() A
21+ //
22+ // This is useful for point-free style programming where you want to partially apply
23+ // the Reader's environment before dealing with the context.
24+ //
25+ // Type Parameters:
26+ // - R: The environment type that the Reader depends on
27+ // - A: The value type
28+ //
29+ // Parameters:
30+ // - ma: A ReaderIO containing a Reader
31+ //
32+ // Returns:
33+ // - A Reader that produces a ReaderIO when given an environment
34+ //
35+ // Example:
36+ //
37+ // type Config struct {
38+ // Timeout int
39+ // }
40+ //
41+ // // A computation that produces a Reader
42+ // getMultiplier := func(ctx context.Context) IO[func(Config) int] {
43+ // return func() func(Config) int {
44+ // return func(cfg Config) int {
45+ // return cfg.Timeout * 2
46+ // }
47+ // }
48+ // }
49+ //
50+ // // Sequence it to apply Config first
51+ // sequenced := SequenceReader[Config, int](getMultiplier)
52+ // cfg := Config{Timeout: 30}
53+ // result := sequenced(cfg)(context.Background())() // Returns 60
54+ //
1055//go:inline
1156func SequenceReader [R , A any ](ma ReaderIO [Reader [R , A ]]) Reader [R , ReaderIO [A ]] {
1257 return RIO .SequenceReader (ma )
1358}
1459
60+ // TraverseReader applies a Reader-based transformation to a ReaderIO, introducing a new environment dependency.
61+ //
62+ // This function takes a Reader-based Kleisli arrow and returns a function that can transform
63+ // a ReaderIO. The result allows you to provide the Reader's environment (R) first, which then
64+ // produces a ReaderIO that depends on the context.
65+ //
66+ // Type transformation:
67+ //
68+ // From: ReaderIO[A]
69+ // = func(context.Context) func() A
70+ //
71+ // With: reader.Kleisli[R, A, B]
72+ // = func(A) func(R) B
73+ //
74+ // To: func(ReaderIO[A]) func(R) ReaderIO[B]
75+ // = func(ReaderIO[A]) func(R) func(context.Context) func() B
76+ //
77+ // This enables transforming values within a ReaderIO using environment-dependent logic.
78+ //
79+ // Type Parameters:
80+ // - R: The environment type that the Reader depends on
81+ // - A: The input value type
82+ // - B: The output value type
83+ //
84+ // Parameters:
85+ // - f: A Reader-based Kleisli arrow that transforms A to B using environment R
86+ //
87+ // Returns:
88+ // - A function that takes a ReaderIO[A] and returns a function from R to ReaderIO[B]
89+ //
90+ // Example:
91+ //
92+ // type Config struct {
93+ // Multiplier int
94+ // }
95+ //
96+ // // A Reader-based transformation
97+ // multiply := func(x int) func(Config) int {
98+ // return func(cfg Config) int {
99+ // return x * cfg.Multiplier
100+ // }
101+ // }
102+ //
103+ // // Apply TraverseReader
104+ // traversed := TraverseReader[Config, int, int](multiply)
105+ // computation := Of(10)
106+ // result := traversed(computation)
107+ //
108+ // // Provide Config to get final result
109+ // cfg := Config{Multiplier: 5}
110+ // finalResult := result(cfg)(context.Background())() // Returns 50
111+ //
15112//go:inline
16113func TraverseReader [R , A , B any ](
17114 f reader.Kleisli [R , A , B ],
0 commit comments