jambit-ToiletPaper159-Icon_Scala's-for-comprehension-in-Typescript

Something like Scala's for comprehension in Typescript

Problem

Programming with monads such as Option, Result, Either or Try is a suitable way to write programs that clearly separate domain logic from handling of absence or error. However, the application of this approach to real-world programs often leads to large closures including deeply nested code blocks. Haskell's do notation or Scala's for comprehension are a nice way to solve this problem. For example, Scala's for comprehension looks like this:
val result: Either[String, Int] =
  for {
    dividend <- Right(42)
    divisor <- Right(2)
    divisorVerified <- if (divisor != 0) Right(divisor) else Left("Divisor must not be zero!")
  } yield dividend / divisor

println(result match {
  case Right(value) => value
  case Left(error) => error
})
However, there is no such thing in Typescript.

Solution

The library for-comprehension-ts provides a similar notation to support for comprehension in Typescript:
const result: Result<number, string> =
    For._("dividend", success(42))
       ._("divisor", success(2))
       ._("divisorVerified", ({divisor}) => divisor != 0 ? success(divisor) : failure("Divisor must not be zero!"))
       .yield(({dividend, divisorVerified}) => dividend / divisorVerified)

console.log(isSuccess(result) ? result.value : result.error)

Die Bibliothek beinhaltet auch eine asynchrone Variante, die eine nahtlose Integration von normalen und asynchronen Funktionen mit denselben Monaden erlaubt. Das funktioniert ganz ohne explizites Handling von Promises.

for-comprehension-ts kann mit allen Implementierungen eines Monad Interfaces verwendet werden, welches die Operationen map, flatMap und flatMapAsync umfasst. Die Bibliothek beinhaltet bereits Implementierungen für die folgenden Monaden:

jambit-ToiletPaper159-table

In contrast to pipes this syntax allows programs to be directed acyclic graphs (DAG) whose vertices are named values ( e.g., a = 3) and where edges are functions. The graph will be executed lazily, i.e., before yield is called, there is only a definition of a program. On calling yield, execution will be triggered. Thus, for-comprehension-ts programs can be duplicated, branched and repeated. Operations will only be executed until the first failure, error or absent value occurs. However, this behavioral aspect depends on the used monad.

jambit-ToiletPaper-159-Visual_Scala's-for-comprehension-in-Typescript

Contribution

for-comprehension-ts is developed by jambit but not used in production yet. So, please feel free to contribute either directly by adding new features or indirectly by just using it:

---

Author: André Petermann / Senior Software Architect / Office Leipzig

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now