1
1
2
2
// Import Expr and some extension methods
3
3
import scala .quoted ._
4
- import scala .quoted .staging .{run , withQuoteContext , Toolbox }
4
+ import scala .quoted .staging .{run , withQuotes , Toolbox }
5
5
6
6
object Main {
7
7
@@ -24,21 +24,21 @@ object Main {
24
24
25
25
def stagedPower (n : Int ): Double => Double = {
26
26
// Code representing the labmda where the recursion is unrolled based on the value of n
27
- def code (using QuoteContext ) = ' { (x : Double ) => \ $ { powerCode(n, ' x ) } }
27
+ def code (using Quotes ) = ' { (x : Double ) => \ $ { powerCode(n, ' x ) } }
28
28
29
29
println(s " staged power for n= " + n + " :" )
30
- println(withQuoteContext (code.show))
30
+ println(withQuotes (code.show))
31
31
32
32
// Evaluate the contents of the code and return it's value
33
33
run(code)
34
34
}
35
35
36
- def powerCode (n : Int , x : Expr [Double ])(using ctx : QuoteContext ): Expr [Double ] =
37
- if (n == 0 ) Expr (1.0 ) // Expr() lifts 1.0 to '{1.0}
38
- else if (n == 1 ) x // optimization to not generate x * 1
39
- else if (n < 0 ) throw new Exception (" Negative powers not implemented. Left as a small exercise. Dont be shy, try it out." )
40
- else if (n == 2 ) ' { \ $x * \ $x } // optimization to not generate { val y = x; y * y }
41
- else if (n % 2 == 1 ) ' { \ $x * \ $ { powerCode(n - 1 , x) } }
42
- else ' { val y = \ $x * \ $x; \ $ { powerCode(n / 2 , ' y ) } }
36
+ def powerCode (n : Int , x : Expr [Double ])(using Quotes ): Expr [Double ] =
37
+ if (n == 0 ) Expr (1.0 ) // Expr() lifts 1.0 to '{1.0}
38
+ else if (n == 1 ) x // optimization to not generate x * 1
39
+ else if (n < 0 ) throw new Exception (" Negative powers not implemented. Left as a small exercise. Dont be shy, try it out." )
40
+ else if (n == 2 ) ' { \ $x * \ $x } // optimization to not generate { val y = x; y * y }
41
+ else if (n % 2 == 1 ) ' { \ $x * \ $ { powerCode(n - 1 , x) } }
42
+ else ' { val y = \ $x * \ $x; \ $ { powerCode(n / 2 , ' y ) } }
43
43
44
44
}
0 commit comments