Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2025:scala:l02 [2025/03/16 01:18] cata_chiru |
pp:2025:scala:l02 [2025/03/16 01:35] (current) cata_chiru |
||
---|---|---|---|
Line 108: | Line 108: | ||
===== 2.5 Nice to know ===== | ===== 2.5 Nice to know ===== | ||
+ | |||
+ | In a computer, we would like the central processing unit / thread to have predominantly a managerial role. | ||
+ | |||
+ | As a result, by executing intense computation on this branch, we would delay other functionalities or would lock into a specific task instead of scheduling. | ||
+ | |||
+ | To solve this limitation, programmers have created ways ('''functional closures''') in which the main thread injects all the needed parameters to functions and then passes them to other threads, from where they can be unpacked and run. | ||
+ | |||
+ | Such an implementation for functional closures is represented by '''zero-parameter lambdas''' where the evaluation is delayed by currying such a function over our method: | ||
+ | |||
<code scala> | <code scala> | ||
// Variation 1 - using zero-parameter lambdas | // Variation 1 - using zero-parameter lambdas | ||
// the type of the target function | // the type of the target function | ||
type Result = Int | type Result = Int | ||
+ | |||
// a closure is represented as a function that takes zero parameters | // a closure is represented as a function that takes zero parameters | ||
type Closure = () => Result | type Closure = () => Result | ||
+ | |||
+ | // type of a demo function | ||
type TFun = (Int, Int) => Result | type TFun = (Int, Int) => Result | ||
// run in thread will call that function to evaluate | // run in thread will call that function to evaluate | ||
def run_in_thread(f: Closure): Result = f() | def run_in_thread(f: Closure): Result = f() | ||
+ | |||
+ | // Dummy method used to illustrate the power of currying | ||
+ | // "f" in curry_for_thread | ||
def process_data(x: Int, y: Int): Int = { | def process_data(x: Int, y: Int): Int = { | ||
println("Hello") | println("Hello") | ||
Line 124: | Line 139: | ||
// creates a lambda with zero parameters, thus "delaying" the call of f | // creates a lambda with zero parameters, thus "delaying" the call of f | ||
+ | // "f" in run_in_thread | ||
def curry_for_thread(f: TFun, somex: Int, somey: Int): Closure = | def curry_for_thread(f: TFun, somex: Int, somey: Int): Closure = | ||
() => f(somex, somey) | () => f(somex, somey) | ||
val close = curry_for_thread(process_data, 0, 1) | val close = curry_for_thread(process_data, 0, 1) | ||
+ | |||
+ | // If you run this code into a Scala Worksheet you will see that "Hello" gets printed when calling the function bellow. | ||
+ | // This is thanks to the 0 parameter lambda in curry_for_thread that "forces" | ||
+ | // another call of the function for it to execute its body. | ||
run_in_thread(close) | run_in_thread(close) | ||
</code> | </code> |