Revise using string interpolation.

This commit is contained in:
Stephen Chung
2021-04-04 23:22:45 +08:00
parent bc9c1ab850
commit 0807c474a1
16 changed files with 33 additions and 32 deletions

View File

@@ -21,9 +21,9 @@ for n in range(0, 5) {
result = fib(target);
}
print("Finished. Run time = " + now.elapsed + " seconds.");
print(`Finished. Run time = ${now.elapsed} seconds.`);
print("Fibonacci number #" + target + " = " + result);
print(`Fibonacci number #${target} = ${result}`);
if result != 317_811 {
print("The answer is WRONG! Should be 317,811!");

View File

@@ -4,7 +4,7 @@ let arr = [1, 2, 3, 4];
for a in arr {
for b in [10, 20] {
print(a + "," + b);
print(`${a}, ${b}`);
}
if a == 3 { break; }

View File

@@ -1,6 +1,6 @@
const MAX = 1_000_000;
print("Iterating an array with " + MAX + " items...");
print(`Iterating an array with ${MAX} items...`);
print("Ready... Go!");
@@ -18,5 +18,5 @@ for i in list {
sum += i;
}
print("Sum = " + sum);
print("Finished. Run time = " + now.elapsed + " seconds.");
print(`Sum = ${sum}`);
print(`Finished. Run time = ${now.elapsed} seconds.`);

View File

@@ -68,4 +68,4 @@ for i in range(0, SIZE) {
}
*/
print("Finished. Run time = " + now.elapsed + " seconds.");
print(`Finished. Run time = ${now.elapsed} seconds.`);

View File

@@ -7,7 +7,7 @@ let last_value = ();
let obj1 = #{
_data: 42, // data field
get_data: || this._data, // property getter
action: || print("Data=" + this._data), // method
action: || print(`Data=${this._data}`), // method
update: |x| { // property setter
this._data = x;
last_value = this._data; // capture 'last_value'
@@ -38,4 +38,4 @@ if obj2.get_data() > 0 { // property access
obj2.update(42); // call method
}
print("Should be 84: " + last_value);
print(`Should be 84: ${last_value}`);

View File

@@ -25,8 +25,8 @@ for p in range(2, MAX_NUMBER_TO_CHECK) {
}
}
print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK);
print("Run time = " + now.elapsed + " seconds.");
print(`Total ${total_primes_found} primes <= ${MAX_NUMBER_TO_CHECK}`);
print(`Run time = ${now.elapsed} seconds.`);
if total_primes_found != 78_498 {
print("The answer is WRONG! Should be 78,498!");

View File

@@ -10,4 +10,4 @@ while x > 0 {
x -= 1;
}
print("Finished. Run time = " + now.elapsed + " seconds.");
print(`Finished. Run time = ${now.elapsed} seconds.`);

View File

@@ -11,17 +11,18 @@ print("foo" >= "bar"); // string comparison
print("the answer is " + 42); // string building using non-string types
let s = "\u2764 hello, world! \U0001F603"; // string variable
print("length=" + s.len); // should be 17
print(`length=${s.len}`); // should be 17
s[s.len-3] = '?'; // change the string
print("Question: " + s); // should print 'Question: hello, world?'
print(`Question: ${s}`); // should print 'Question: hello, world?'
// Line continuation:
let s = "This is a long \
string constructed using \
line continuation";
print("One string: " + s);
// String interpolation
print(`One string: ${s}`);
// Multi-line literal string:
let s = `

View File

@@ -75,7 +75,7 @@ let keys = [];
for animal in animals {
for adjective in adjectives {
for adverb in adverbs {
keys.push(adverb + " " + adjective + " " + animal)
keys.push(`${adverb} ${adjective} ${animal}`)
}
}
}
@@ -99,5 +99,5 @@ for key in keys {
map.remove(key);
}
print("Sum = " + sum);
print("Finished. Run time = " + now.elapsed + " seconds.");
print(`Sum = ${sum}`);
print(`Finished. Run time = ${now.elapsed} seconds.`);