Just some comments
Opened this issue · 1 comments
Your repo was shared in the Linux channel of our discord (https://aka.ms/psdiscord), I wanted to add a few comments to help in the creation of this project :)
Please feel free to disregard / delete / destroy this. It is intended to help, but you are of course entitled to disagree with my ambition :)
One for the array indexes
You can use $array[1, 4, 9]
to access elements of an array. An extension of accessing a range of elements (considering that 1..3
yields an array of 1, 2, 3).
return
Return causes an invoked script block to immediately end. It can also emit, but it is not exclusive and anything else which yields output will also be emitted from a function. It is therefore not accurate to say it specifies explicitly what gets returned
as that implies it is all that gets returned.
The function below demonstrates how return can be considered misleading:
function Get-Something {
$intendedOutput = 1
'some-other-statement'
return $intendedOutput
}
This is a common cause of some confusion iin PowerShell for anyone entering from another language. In other languages this would be far more true, in PS it is only exclusive in PS class methods.
Get-Rolls
Consider revising the function to avoid continually resizing a fixed size array. This technique does not scale at all well and can be avoided.
All statements can be assigned in PowerShell, therefore the loop can be assigned. PowerShell will internally build the collection (as an ArrayList), assigning as a final action. The engine is better at this task that anything in PS code.
function Get-Rolls {
param (
[int]$NumberOfRolls = 10 # 10 is the default value now
)
$roles = for ($i = 0; $i -lt $NumberOfRolls; $i++){
Get-Random -InputObject @(1..10)
}
return $roles
}
Or in the case of a function, output can be immediately sent to the output pipeline. This provides much better support for pipeline commands like Select-Object -First 1
.
function Get-Rolls {
param (
[int]$NumberOfRolls = 10 # 10 is the default value now
)
for ($i = 0; $i -lt $NumberOfRolls; $i++){
Get-Random -InputObject @(1..10)
}
}
The example usage below demonstrates that Select-Object
stops the command processing:
function Get-Rolls {
param (
[int]$NumberOfRolls = 10 # 10 is the default value now
)
for ($i = 0; $i -lt $NumberOfRolls; $i++){
Get-Random -InputObject @(1..10)
Write-Host 'Looping'
}
}
Get-Rolls -NumberOfRolls 1000000 | Select-Object -First 2
This solution is inappropriate where clean-up actions are required after a loop, but a good fit in this example.
It is, of course, entirely feasible to create a more advanced collection type, however that is I think beyond the introductory topic you currently have. An example is below just in case:
function Get-Rolls {
param (
[int]$NumberOfRolls = 10 # 10 is the default value now
)
$rolls = [System.Collections.Generic.List[int]]::new()
for ($i = 0; $i -lt $NumberOfRolls; $i++){
$rolls.Add((Get-Random -InputObject @(1..10)))
}
return $rolls
}
Despite the List type, PS will enumerate the output when it emits. The output type will therefore still be Object[] as mentioned in some of your other documents.
Get-RollScore
Switch enumerates, you therefore do not need the foreach
loop in the example:
function Get-RollScore {
param(
[int[]]$Rolls
)
$total = 0
switch ($Rolls)
{
1 {$total+=10}
2 {$total+=10}
3 {$total+=1}
4 {$total+=5}
5 {$total+=9}
6 {$total+=6}
7 {$total+=4}
8 {$total+=8}
9 {$total+=9}
10 {$total+=2}
}
return $total
}
All the best,
Chris
Hi Chris,
You make some very good points!
The "problem" with PowerShell is that there are a million ways to do the same thing.
- Indexes for array
That's a good point! I'll definitely add that!
-
Return
Yeah... that's often been a major point in my rear. But I see your point, I will clarify. -
The rolls/score
This was really intended as a wrap-up on all the other topics. Your comments make me think that maybe I need more advanced versions of the previous topics like loops and switches. I'll definitely look into how to structure that.