Skip to content

Commit e31b6ff

Browse files
feat: Turtle.Repeat() ( Fixes PoshWeb#256 )
Improving performance with multiplication
1 parent ccac32a commit e31b6ff

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Types/Turtle/Repeat.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
}
3131
)
3232
~~~
33+
34+
Because repeat parses each step each time, repeat is likely to be one of the slower ways to repeat.
3335
.EXAMPLE
3436
turtle repeat 3 [rotate (360/3) forward 42] save ./tri.svg
3537
.EXAMPLE
@@ -50,7 +52,8 @@
5052
) forward 4.2]]] save ./r3.svg
5153
#>
5254
param(
53-
# The repeat count. This will be rounded down to the nearest integer.
55+
# The repeat count.
56+
# This will be rounded down to the nearest integer and converted into an absolute value.
5457
[double]
5558
$RepeatCount,
5659

@@ -64,9 +67,7 @@ $Command
6467
if (-not $RepeatCount) { return $this }
6568
$floorCount = [Math]::Abs([Math]::Floor($RepeatCount))
6669

67-
if ($floorCount -ge 1) {
68-
foreach ($repetition in 1..$floorCount) {
69-
$this = $this | turtle @command
70-
}
70+
if ($floorCount -ge 1) {
71+
$this = $this | turtle @($Command * $floorCount)
7172
}
7273
return $this

0 commit comments

Comments
 (0)