concept computation in category scala

appears as: computation, computations, computation, The computation
Get Programming with Scala

This is an excerpt from Manning's book Get Programming with Scala.

Assignments improve the readability of your code by breaking down the computation and by assigning meaningful names to it. They can also increase the performance of your program by saving intermediate results that you can reuse and share between processes. Values are the most commonly used type of assignments in Scala.

In the previous lessons, you have mastered the type Either. In this lesson, you’ll learn about the class Try to represent a computation that can fail. Throwing exceptions is a risky and unpredictable practice. Knowing which exceptions a method may throw is challenging, if not impossible, as they are often not annotated, and they are highly dependent on the specifics of their implementation. The class Try allows you to control a computation that may throw exceptions and it forces you to provide instruction for both the success and failure cases at compile time. Whenever possible, you should use Try instead of a try-catch statement. You’ll find lots of similarities with the type Either: you can see Try[T] as an alternative implementation for Either[Throwable, T]. You’ll learn about the purpose and structure of the type Try, and you’ll use a pattern matching statement to decompose its possible implementations. You’ll transform and manipulate its value using its map, flatten, and flatMap methods. You’ll combine multiple operations that can fail into one. You’ll also inquire about the success of some computation and retrieve its value. In the capstone, you will use the type Try to handle possible failures when parsing data from a CSV to create a book collection for your library.

Figure 40.1: Use the class Try[A] to represent a computation that can fail in producing a value of type A. It has only two possible implementations: Success and Failure. Success[A] represents a completed operation containing the created instance of type A, while Failure[A] represents a faulty execution containing the exception that caused it.
Functional Programming in Scala

This is an excerpt from Manning's book Functional Programming in Scala.

Referential transparency forces the invariant that everything a function does is represented by the value that it returns, according to the result type of the function. This constraint enables a simple and natural mode of reasoning about program evaluation called the substitution model. When expressions are referentially transparent, we can imagine that computation proceeds much like we’d solve an algebraic equation. We fully expand every part of an expression, replacing all variables with their referents, and then reduce it to its simplest form. At each step we replace a term with an equivalent one; computation proceeds by substituting equals for equals. In other words, RT enables equational reasoning about programs.

1 We’ll use the term logical thread somewhat informally throughout this chapter to mean a computation that runs concurrently with the main execution thread of our program. There need not be a one-to-one correspondence between logical threads and OS threads. We may have a large number of logical threads mapped onto a smaller number of OS threads via thread pooling, for instance.

2 Function arguments in Scala are strictly evaluated from left to right, so if unit delays execution until get is called, we will both spawn the parallel computation and wait for it to finish before spawning the second parallel computation. This means the computation is effectively sequential!

Listing 7.4. Basic sketch for an API for Par
def unit[A](a: A): Par[A] #1 def map2[A,B,C](a: Par[A], b: Par[B])(f: (A,B) => C): Par[C] #2 def fork[A](a: => Par[A]): Par[A] #3 def lazyUnit[A](a: => A): Par[A] = fork(unit(a)) #4 def run[A](a: Par[A]): A #5 #1: Creates a computation that immediately results in the value a. #2: Combines the results of two parallel computations with a binary function. #3: Marks a computation for concurrent evaluation by run. #4: Wraps the expression a for concurrent evaluation by run. #5: Fully evaluates a given Par, spawning parallel computations as requested by fork and extracting the resulting value.