Swift Playgrounds: ability to use previous results as input

That's how it done in Wolfram Language with % symbol. For example:

RandomInteger[{120, 600}]^2 // 55225
Sqrt[%] // 255

It would be cool to have something similar in Swift playgrounds. A symbol for referring the previous answer without assigning it to a variable. For example $. Like:

func square(_ x: Int) -> Int {
    return x * x
}

square(Int.random(in: 120...600)) // 30625
sqrt($) // 175

If you don't understand why it's useful, you are probably use playground only for programming. But imagine using playground as a big calculator. Such feature is extremely helpful for playing with calculations. And also for education, when students need to do a big task separated into smaller parts.

2 Likes

Just make a var at the top and keep reusing it:

var x = ...
x = ...
...
1 Like

If you like to use swift as a big calculator, consider the REPL. It has that feature built in already

  1> 2 + 2
$R0: Int = 4
  2> print($R0)
4
  3> 77 * 111
$R1: Int = 8547
  4> print($R0 + $R1)
8551
5 Likes

This is not a bad idea, but Playgrounds are primarily an Apple feature—you may have more luck suggesting this through Feedback Assistant.