Skip to content

Commit f68d4e6

Browse files
feat: implement head and headOption operators
The `headOption` operator returns the first element in `Source` wrapped in `Some` or `None` in case when source is empty or failed e.g.: Source.empty[Int].headOption() // None Source.fromValues(1, 2).headOption() // Some(1) The `head` operator returns the first element in `Source` or throws `NoSuchElementException` in case when it is either empty or `receive()` operation fails without error. In case when `receive()` fails with exception then this exception is re-thrown e.g.: Source.empty[Int].head() // throws NoSuchElementException("cannot obtain head from an empty source") Source.fromValues(1, 2).head() // 1 Note that neither `head` nor `headOption` are idempotent operations.
1 parent 7d7897a commit f68d4e6

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

core/src/main/scala/ox/channels/SourceOps.scala

+62
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ox.*
55
import java.util.concurrent.{CountDownLatch, Semaphore}
66
import scala.collection.{IterableOnce, mutable}
77
import scala.concurrent.duration.FiniteDuration
8+
import scala.util.Try
89

910
trait SourceOps[+T] { this: Source[T] =>
1011
// view ops (lazy)
@@ -513,6 +514,57 @@ trait SourceOps[+T] { this: Source[T] =>
513514
}
514515
}
515516
c
517+
518+
/** Returns the first element from this source wrapped in `Some` or `None` when the source is empty or fails during the receive operation.
519+
* Note that `headOption` is not an idempotent operation on source as it receives elements from it.
520+
*
521+
* @return
522+
* A `Some(first element)` if source is not empty or None` otherwise.
523+
* @example
524+
* {{{
525+
* import ox.*
526+
* import ox.channels.Source
527+
*
528+
* scoped {
529+
* Source.empty[Int].headOption() // None
530+
* val s = Source.fromValues(1, 2)
531+
* s.headOption() // Some(1)
532+
* s.headOption() // Some(2)
533+
* }
534+
* }}}
535+
*/
536+
def headOption(): Option[T] = Try(head()).toOption
537+
538+
/** Returns the first element from this source or throws `NoSuchElementException` when the source is empty or `receive()` operation fails
539+
* without error. In case when the `receive()` operation fails with exception that exception is re-thrown. Note that `headOption` is not
540+
* an idempotent operation on source as it receives elements from it.
541+
*
542+
* @return
543+
* A first element if source is not empty or throws otherwise.
544+
* @throws NoSuchElementException
545+
* When source is empty or `receive()` failed without error.
546+
* @throws exception
547+
* When `receive()` failed with exception then this exception is re-thrown.
548+
* @example
549+
* {{{
550+
* import ox.*
551+
* import ox.channels.Source
552+
*
553+
* scoped {
554+
* Source.empty[Int].head() // throws NoSuchElementException("cannot obtain head from an empty source")
555+
* val s = Source.fromValues(1, 2)
556+
* s.head() // 1
557+
* s.head() // 2
558+
* }
559+
* }}}
560+
*/
561+
def head(): T =
562+
supervised {
563+
receive() match
564+
case ChannelClosed.Done => throw new NoSuchElementException("cannot obtain head from an empty source")
565+
case ChannelClosed.Error(r) => throw r.getOrElse(new NoSuchElementException("getting head failed"))
566+
case t: T @unchecked => t
567+
}
516568
}
517569

518570
trait SourceCompanionOps:
@@ -740,3 +792,13 @@ trait SourceCompanionOps:
740792
val c = DirectChannel[T]()
741793
c.error(t)
742794
c
795+
796+
/** Creates a source that fails immediately
797+
*
798+
* @return
799+
* A source that would fail immediately
800+
*/
801+
def failed[T](): Source[T] =
802+
val c = DirectChannel[T]()
803+
c.error(None)
804+
c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ox.channels
2+
3+
import org.scalatest.OptionValues
4+
import org.scalatest.flatspec.AnyFlatSpec
5+
import org.scalatest.matchers.should.Matchers
6+
import ox.*
7+
8+
class SourceOpsHeadOptionTest extends AnyFlatSpec with Matchers with OptionValues {
9+
behavior of "Source.headOption"
10+
11+
it should "return None for the empty source" in supervised {
12+
Source.empty[Int].headOption() shouldBe None
13+
}
14+
15+
it should "return None for the failed source" in supervised {
16+
Source
17+
.failed(new RuntimeException("source is broken"))
18+
.headOption() shouldBe None
19+
}
20+
21+
it should "return Some element for the non-empty source" in supervised {
22+
Source.fromValues(1, 2).headOption().value shouldBe 1
23+
}
24+
25+
it should "be not idempotent operation" in supervised {
26+
val s = Source.fromValues(1, 2)
27+
s.headOption().value shouldBe 1
28+
s.headOption().value shouldBe 2
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ox.channels
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
import ox.*
6+
7+
class SourceOpsHeadTest extends AnyFlatSpec with Matchers {
8+
behavior of "Source.head"
9+
10+
it should "throw NoSuchElementException for the empty source" in supervised {
11+
the[NoSuchElementException] thrownBy {
12+
Source.empty[Int].head()
13+
} should have message "cannot obtain head from an empty source"
14+
}
15+
16+
it should "re-throw exception that was thrown during element retrieval" in supervised {
17+
the[RuntimeException] thrownBy {
18+
Source
19+
.failed(new RuntimeException("source is broken"))
20+
.head()
21+
} should have message "source is broken"
22+
}
23+
24+
it should "throw NoSuchElementException for source failed without exception" in supervised {
25+
the[NoSuchElementException] thrownBy {
26+
Source.failed[Int]().head()
27+
} should have message "getting head failed"
28+
}
29+
30+
it should "return first value from non empty source" in supervised {
31+
Source.fromValues(1, 2).head() shouldBe 1
32+
}
33+
34+
it should "be not idempotent operation" in supervised {
35+
val s = Source.fromValues(1, 2)
36+
s.head() shouldBe 1
37+
s.head() shouldBe 2
38+
}
39+
}

0 commit comments

Comments
 (0)