Skip to content

Commit

Permalink
Move ToView into FromIterable (#536)
Browse files Browse the repository at this point in the history
Currently it is possible to convert `FromIterable` to collections other than `View`, therefore `ToView.FromIterable` does not make sense. On the other hand because  `ToView` should be always used with `FromIterable`, `FromIterable.ToView` makes sense.
  • Loading branch information
Atry committed Dec 15, 2021
1 parent ad734cf commit 6d1bd65
Show file tree
Hide file tree
Showing 18 changed files with 551 additions and 533 deletions.
2 changes: 1 addition & 1 deletion Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object Dsl extends LowPriorityDsl0 {
/** The AST returned from a `for`...`yield` or a `for`...`do` expression.
*
* Note that a [[For]] does not directly support !-notation.
* Instead, [[keywords.ToView]] is used to convert a [[For]] to a
* Instead, [[keywords.FromIterable.ToView]] is used to convert a [[For]] to a
* [[Keyword]] that supports !-notation.
*/
sealed trait For
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ We also provide some built-in keywords, including:
* The `NullSafe` keyword for the null safe operator, similar to the `?` operator in Kotlin and Groovy.
* The `NoneSafe` keyword for the `None` safe operator, similar to the `Maybe` monad in Haskell.

All the above keywords can be used together with each others. For example you can perform list comprehension to manipulate native resources in an asynchronous task by using `ToView.FromIterable`, `Using` and `Shift` together.
All the above keywords can be used together with each others. For example you can perform list comprehension to manipulate native resources in an asynchronous task by using `FromIterable`, `Using` and `Shift` together.

## Getting Started

Expand Down
15 changes: 7 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ lazy val `domains-Task` =
`keywords-Shift`,
reset,
`domains-Continuation`,
`keywords-ToView` % Test,
`keywords-FromIterable` % Test,
`keywords-Using` % Test,
`keywords-Yield` % Test,
`keywords-ToView` % Test
`keywords-FromIterable` % Test
)

lazy val `keywords-ToView` =
lazy val `keywords-FromIterable` =
crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.dependsOn(Dsl, reset, `keywords-Shift`)
Expand Down Expand Up @@ -116,9 +116,9 @@ lazy val `keywords-AsynchronousIo` =
.crossType(CrossType.Pure)
.dependsOn(
`keywords-Shift`,
`keywords-ToView` % Test,
`keywords-FromIterable` % Test,
`keywords-Using` % Test,
`keywords-ToView` % Test,
`keywords-FromIterable` % Test,
`domains-Task` % Test
)

Expand Down Expand Up @@ -150,11 +150,10 @@ lazy val `keywords-Await` =
`domains-Continuation`,
reset % Test,
`domains-Task` % Test,
`keywords-ToView` % Test,
`keywords-Get` % Test,
`keywords-Return` % Test,
`keywords-Yield` % Test,
`keywords-ToView` % Test
`keywords-FromIterable` % Test
)

lazy val `scala-async` =
Expand All @@ -168,7 +167,7 @@ lazy val `keywords-Yield` =
.dependsOn(
Dsl,
reset % Test,
`keywords-ToView` % Test,
`keywords-FromIterable` % Test,
`keywords-Shift` % Test,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.thoughtworks.dsl.Dsl.{!!}
import org.scalatest.Assertion
import scala.language.implicitConversions

import com.thoughtworks.dsl.keywords.{Using, ToView}
import com.thoughtworks.dsl.keywords.{Using, FromIterable}
import com.thoughtworks.dsl.domains._
import com.thoughtworks.dsl.keywords.Shift

Expand Down Expand Up @@ -43,7 +43,7 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
val task1: Task[Int] = Task.now(1)

val ts = *[Task]/* .join */ apply Seq {
!ToView.FromIterable(0 until 10) + !Shift(task1)
!FromIterable(0 until 10) + !Shift(task1)
}

!Shift(ts) should be(1 until 11)
Expand Down Expand Up @@ -183,11 +183,11 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
t0: Task[Seq[Task[Seq[Task[Seq[Task[Seq[Float]]]]]]]]
): Task[Seq[Seq[Seq[Seq[Float]]]]] = {
Task /*.join*/ apply Seq {
val t1 = !ToView.FromIterable(!Shift(t0))
val t1 = !FromIterable(!Shift(t0))
!Shift(Task /*.join*/ apply Seq {
val t2 = !ToView.FromIterable(!Shift(t1))
val t2 = !FromIterable(!Shift(t1))
!Shift(Task /*.join*/ apply Seq {
val t3 = !ToView.FromIterable(!Shift(t2))
val t3 = !FromIterable(!Shift(t2))
!Shift(t3)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.util.control.TailCalls.TailRec
*
* @template
* @example A [[Task]] can be created from `for`-comprehension,
* where [[keywords.ToView.FromIterable]] can be used together to asynchronously iterate collections.
* where [[keywords.FromIterable]] can be used together to asynchronously iterate collections.
*
* For example, the above `concatenateRemoteData` downloads and concatenates data from multiple URLs.
*
Expand All @@ -31,11 +31,11 @@ import scala.util.control.TailCalls.TailRec
* import com.thoughtworks.dsl.keywords._
* import com.thoughtworks.dsl.domains.Task
* import java.net.URL
* def concatenateRemoteData(urls: List[URL], downloader: URL => Task[Vector[Byte]]) = ToView {
* def concatenateRemoteData(urls: List[URL], downloader: URL => Task[Vector[Byte]]) = FromIterable.ToView {
* for {
* url <- ToView.FromIterable(urls)
* url <- FromIterable(urls)
* data <- Shift(downloader(url))
* byte <- ToView.FromIterable(data)
* byte <- FromIterable(data)
* } yield byte
* }.to[Task]
* }}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scala.util.control.NonFatal
* @example [[scalaz.Free.Trampoline]] is a monadic data type that performs tail call optimization.
* It can be built from a `@[[Dsl.reset reset]]` code block within some [[Dsl.Keyword#unary_$bang !-notation]],
* similar to the [[com.thoughtworks.each.Monadic.EachOps#each each]] method in
* [[https://github.com/ThoughtWorksInc/each ThoughtWorks ToView.FromIterable]].
* [[https://github.com/ThoughtWorksInc/each ThoughtWorks FromIterable]].
*
* {{{
* import _root_.scalaz.Trampoline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import scala.util.control.NonFatal
* and other keywords can be used together in the same `for` block.
*
* For example, the following `cat` function contains a single `for` block to concatenate file contents.
* It asynchronously iterates elements `Seq`, `ArrayBuffer` and `String` with the help of [[keywords.ToView.FromIterable]],
* It asynchronously iterates elements `Seq`, `ArrayBuffer` and `String` with the help of [[keywords.FromIterable]],
* managed native resources with the help of [[keywords.Using]],
* performs previously created `readAll` task with the help of [[keywords.Shift]],
* and finally converts the return type [[comprehension.ComprehensionOps.as as]] a `Task[Vector[Char]]`.
Expand All @@ -51,13 +51,13 @@ import scala.util.control.NonFatal
* import com.thoughtworks.dsl.domains.Task
* import com.thoughtworks.dsl.Dsl.to
* import java.net.URL
* def cat(paths: Path*) = ToView {
* def cat(paths: Path*) = FromIterable.ToView {
* for {
* path <- ToView.FromIterable(paths)
* path <- FromIterable(paths)
* channel <- Using(AsynchronousFileChannel.open(path))
* charBuffers <- Shift(readAll(channel))
* charBuffer <- ToView.FromIterable(charBuffers)
* char <- ToView.FromIterable(charBuffer.toString)
* charBuffer <- FromIterable(charBuffers)
* char <- FromIterable(charBuffer.toString)
* } yield char
* }.to[Task]
* }}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AwaitTest extends AsyncFreeSpec with Matchers with Inside {
type Id[A] = A
"testComprehension1" in {
def inner1 = for {
j <- ToView.FromIterable(0 until 3)
j <- FromIterable(0 until 3)
} yield 100 + j

val ast1 = Await(Future(1)).flatMap { i =>
Expand All @@ -44,7 +44,7 @@ class AwaitTest extends AsyncFreeSpec with Matchers with Inside {
Await[Int],
Int,
Dsl.For.Yield.Map[
ToView.FromIterable[Int],
FromIterable[Int],
Int,
Int
],
Expand All @@ -54,20 +54,20 @@ class AwaitTest extends AsyncFreeSpec with Matchers with Inside {

*[Future] {
(!Await(
ToView(ast1).to[Future]
FromIterable.ToView(ast1).to[Future]
)).toVector should be(Vector(100, 101, 102))
}
}

"testComprehension2" in {
import Dsl._
val inner2 = for {
j <- ToView.FromIterable(0 until 10)
j <- FromIterable(0 until 10)
} yield 111
summon[
inner2.type
<:<
Dsl.For.Yield.Map[ToView.FromIterable[Int], Int, Int]
Dsl.For.Yield.Map[FromIterable[Int], Int, Int]
]
val ast2 = Await(Future(1)).flatMap { i =>
inner2
Expand All @@ -79,7 +79,7 @@ class AwaitTest extends AsyncFreeSpec with Matchers with Inside {
Await[Int],
Int,
Dsl.For.Yield.Map[
ToView.FromIterable[Int],
FromIterable[Int],
Int,
Int
],
Expand All @@ -91,9 +91,9 @@ class AwaitTest extends AsyncFreeSpec with Matchers with Inside {

"testComprehension3" in {
import Dsl._
val ast3 = ToView.toKeyword(for {
val ast3 = FromIterable.ToView.toKeyword(for {
i <- Await(Future(1))
j <- ToView.FromIterable(0 until 10)
j <- FromIterable(0 until 10)
} yield 111)
summon[
ast3.type
Expand All @@ -102,7 +102,7 @@ class AwaitTest extends AsyncFreeSpec with Matchers with Inside {
Await[Int],
Int,
FlatMap[
ToView.FromIterable[Int],
FromIterable[Int],
Int,
Pure[collection.View[Int]]
]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 6d1bd65

Please sign in to comment.