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

feat: introduce takeLast(n) operator #31

Merged
merged 1 commit into from
Oct 31, 2023
Merged

Conversation

geminicaprograms
Copy link
Contributor

@geminicaprograms geminicaprograms commented Oct 30, 2023

Returns the list of up to n last elements from this source. Less than n elements is returned when this source contains les elements than requested. The [[List.empty]] is returned when takeLast is called on an empty source.

Example:

  Source.empty[Int].takeLast(5)    // List.empty
  Source.fromValues(1).takeLast(0) // List.empty
  Source.fromValues(1).takeLast(2) // List(1)
  val s = Source.fromValues(1, 2, 3, 4)
  s.takeLast(2)                    // List(4, 5)
  s.receive()                      // ChannelClosed.Done

* }}}
*/
def takeLast(n: Int): List[T] =
require(n > 0, "n must be > 0")
Copy link
Contributor

Choose a reason for hiding this comment

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

thought: Isn't 0 a legal case as well, where we would return an empty list? We'd probably need to drain the source then to make the side effects "compatible" with calling takeLast with n > 0.

Copy link
Member

Choose a reason for hiding this comment

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

Akka Streams requires n > 0. On the other hand, I don't see anything wrong with your idea @rucek.

Copy link
Contributor

@rucek rucek Oct 31, 2023

Choose a reason for hiding this comment

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

OK, that was probably the inspiration. On the other hand, Scala collections use n.max(0) - although I'm aware that collections might have different semantics than streams/channels. My own issue with allowing 0 for takeLast is that it might require "artificially" draining the channel (so that it has a similar side effect on the channel to calling takeLast with n > 0) - I'm not sure if it's a correct approach.

@adamw any thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Nil + drain sounds most reasonable I guess :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the case when n == 0 to implementation and unit tests.

Returns the list of up to `n` last elements from this source. Less than
`n` elements is returned when this source contains les elements than requested.
The [[List.empty]] is returned when `takeLast` is called on an empty source.

Example:

  Source.empty[Int].takeLast(5) // List.empty
  Source.fromValues(1).takeLast(0) // List.empty
  Source.fromValues(1).takeLast(2) // List(1)
  val s = Source.fromValues(1, 2, 3, 4)
  s.takeLast(2) // List(4, 5)
  s.receive() // ChannelClosed.Done
@geminicaprograms geminicaprograms merged commit 053c486 into master Oct 31, 2023
4 checks passed
@geminicaprograms geminicaprograms deleted the feat_takeLast branch October 31, 2023 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants