-
Notifications
You must be signed in to change notification settings - Fork 508
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
Add Recipe presenting real world use case #3451
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we discuss the modify
+ flatten
idiom as well?
docs/recipes.md
Outdated
|
||
```scala mdoc:silent | ||
def update(input: Int): IO[Int] = | ||
IO.defer(input + 1 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IO.defer(input + 1 ) | |
IO(input + 1 ) |
:-) I think you meant delay
, but apply
is even more concise.
docs/recipes.md
Outdated
```scala mdoc:silent | ||
class Server(atomicCell: AtomicCell[IO, Int]) { | ||
def update(input: Int): IO[Int] = | ||
IO.defer(input + 1 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IO.defer(input + 1 ) | |
IO(input + 1 ) |
docs/recipes.md
Outdated
So, let's assume that we want to implement very simple rate limiting proxy, that will only allow some specific number of requests and then return failure if this limit is surpassed. | ||
|
||
The code could look like below: | ||
```scala mdoc:silet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```scala mdoc:silet | |
```scala mdoc:silent |
docs/recipes.md
Outdated
class RateLimiterProxy(limit: Int, cell: AtomicCell[IO, Int], service: Service){ | ||
def limit[A](request: IO[Request]): IO[Response] = { | ||
cell.evalModify{ requests => | ||
if(requests >= limit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(requests >= limit) | |
if (requests >= limit) |
docs/recipes.md
Outdated
|
||
class RateLimiterProxy(limit: Int, cell: AtomicCell[IO, Int], service: Service){ | ||
def limit[A](request: IO[Request]): IO[Response] = { | ||
cell.evalModify{ requests => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cell.evalModify{ requests => | |
cell evalModify { requests => |
a272a9a
to
f04fe3d
Compare
Hey, I've actually found simpler example, let me know if that works, I think RateLimiter was a little farfetched. |
Co-authored-by: Maksym Ochenashko <maxochenashko@gmail.com>
fc1031c
to
a8ccac8
Compare
a8ccac8
to
c5ea23f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good! Just a couple minor cleanups and then ready to go
Co-authored-by: Daniel Spiewak <djspiewak@gmail.com>
Co-authored-by: Maksym Ochenashko <maxochenashko@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this example quite a bit. Nicely done!
I was thinking about something that presents a concept in a simple manner but also captures the essence of update based on the effectful result, but not too sure if this example is the best.