-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Learning fp-go question #112
Comments
Hi @franchb I am not familiar with neither the fx package nor the jetstream package. From what I understand your code is doing the following:
My first advice would be to split this one dependency into four:
Each of the The advantage is that now it's in the responsability of the None of these aspects is directly related to functional programming though. In terms of fp-go I suggest that the signature for import (
RIOE "github.com/IBM/fp-go/context/readerioeither"
"github.com/nats-io/nats.go/jetstream"
F "github.com/IBM/fp-go/function"
)
func GetCreateEventsStream(cfg jetstream.StreamConfig) func(js jetstream.JetStream) RIOE.ReaderIOEither[jetstream.Stream] {
return func(js jetstream.JetStream) RIOE.ReaderIOEither[jetstream.Stream] {
return RIOE.Eitherize1(js.CreateStream)(cfg)
}
} and you can call this as: import (
"context"
RIOE "github.com/IBM/fp-go/context/readerioeither"
F "github.com/IBM/fp-go/function"
I "github.com/IBM/fp-go/identity"
IO "github.com/IBM/fp-go/io"
IOE "github.com/IBM/fp-go/ioeither"
"github.com/nats-io/nats.go/jetstream"
"go.uber.org/fx"
)
func Handler(lc fx.Lifecycle, js jetstream.JetStream) {
name := "mystream"
stream := F.Pipe2(
CreateStream,
I.Ap[func(js jetstream.JetStream) RIOE.ReaderIOEither[jetstream.Stream]](jetstream.StreamConfig{Name: name}),
I.Ap[RIOE.ReaderIOEither[jetstream.Stream]](js),
)
lc.Append(fx.Hook{OnStart: func(ctx context.Context) error {
return E.ToError(stream(ctx)())
},
OnStop: F.Bind2nd(js.DeleteStream, name),
})
} Stream creation looks a bit awkward because of the type hints we still have to give to go, it's in its canonical form, a curried function, then a sequence of You can also write stream := CreateStream(jetstream.StreamConfig{Name: name})(js) An alternative to OnStart: func(ctx context.Context) error {
return E.ToError(stream(ctx)())
} which is not as idiomatic but it looks nicer :-) I must admit though, that the functional syntax does not look convincing for this usecase. Let me ponder about this a bit more ... Note: from a functional perspective the method |
Much appreciated, @CarstenLeue! I plan to experiment with the sample you provided and consider sharing my insights in a blog post. |
Hello!
I'm currently in the process of learning
fp-go
and am aiming to apply functional programming principles in Go. To get hands-on experience, I've taken a straightforward task: rewriting a Go function that initializes a NATS JetStream connection and subsequently creates multiple streams.Here's the original code for context:
Could someone guide me on the idiomatic way to rewrite this function using
fp-go
? I'm looking for a functional approach that would align with the paradigms and best practices of the library.Thank you!
The text was updated successfully, but these errors were encountered: