Skip to content
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

Merged
merged 13 commits into from
Mar 29, 2023

Conversation

Wosin
Copy link
Contributor

@Wosin Wosin commented Feb 22, 2023

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.

@Wosin Wosin changed the base branch from series/3.x to series/3.4.x February 22, 2023 00:05
Copy link
Member

@djspiewak djspiewak left a 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 )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cell.evalModify{ requests =>
cell evalModify { requests =>

@Wosin
Copy link
Contributor Author

Wosin commented Feb 22, 2023

Hey, I've actually found simpler example, let me know if that works, I think RateLimiter was a little farfetched.

@Wosin Wosin requested a review from djspiewak February 23, 2023 14:37
docs/recipes.md Outdated Show resolved Hide resolved
docs/recipes.md Outdated Show resolved Hide resolved
docs/recipes.md Outdated Show resolved Hide resolved
docs/recipes.md Outdated Show resolved Hide resolved
Co-authored-by: Maksym Ochenashko <maxochenashko@gmail.com>
Copy link
Member

@djspiewak djspiewak left a 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

docs/recipes.md Outdated Show resolved Hide resolved
docs/recipes.md Outdated Show resolved Hide resolved
docs/recipes.md Outdated Show resolved Hide resolved
Co-authored-by: Daniel Spiewak <djspiewak@gmail.com>
docs/recipes.md Outdated Show resolved Hide resolved
Co-authored-by: Maksym Ochenashko <maxochenashko@gmail.com>
@Wosin Wosin requested a review from djspiewak March 12, 2023 21:19
docs/recipes.md Outdated Show resolved Hide resolved
Wosin and others added 4 commits March 14, 2023 01:24
Co-authored-by: Maksym Ochenashko <maxochenashko@gmail.com>
Copy link
Member

@djspiewak djspiewak left a 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!

@djspiewak djspiewak merged commit 08f51e0 into typelevel:series/3.4.x Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants