You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
functionGet-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.
functionGet-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:
functionGet-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:
functionGet-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:
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.
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 that1..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:
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.
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
.The example usage below demonstrates that
Select-Object
stops the command processing: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:
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:All the best,
Chris
The text was updated successfully, but these errors were encountered: