Skip to content

Commit 4baabcd

Browse files
authored
Refactor Introduction Koans (#370)
* ♻️ 📝 update AboutAssertions * ♻️ 🎨 Refactor AboutBinary * 📝 Refactor AboutBooleans intro text * 📝 first pass of AboutArrays * ♻️ 🎨 Refactor AboutStrings * ♻️ 🎨 ✨ AboutNumbers - Some refactoring and rewriting of comments. - Added mention of suffixes and Long type.
1 parent 29a7068 commit 4baabcd

File tree

6 files changed

+304
-182
lines changed

6 files changed

+304
-182
lines changed

PSKoans/Koans/Foundations/AboutArrays.Koans.ps1

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,60 @@ param()
44
<#
55
Arrays and Iterable Collections
66
7-
Like many programming languages, PowerShell often uses arrays to keep collections
8-
of objects together. Arrays tie in closely with PowerShell's pipeline, which
9-
is one way to iterate over a collection with a good deal of efficiency.
7+
Like many programming languages, PowerShell often uses arrays to keep
8+
collections of objects together. Arrays tie in closely with PowerShell's
9+
pipeline, which is one way to iterate over a collection with a good deal of
10+
efficiency.
1011
1112
There are a few 'array-like' collection types available in PowerShell, all
12-
of which are rooted in .NET classes and data types, and behave much the
13-
same as they do in C# and VB.NET.
13+
of which are rooted in .NET classes and data types, and behave much the same
14+
as they do in other .NET languages.
1415
15-
Arrays in particular have a close relationship with the PowerShell pipeline,
16-
which will be covered a shortly.
16+
Arrays in particular are closely tied to the PowerShell pipeline, which are
17+
covered in another topic.
1718
#>
1819
Describe 'Arrays' {
1920

2021
It 'is useful for grouping related objects and values' {
21-
# The comma operator is used to create an array. Spaces are typically ignored.
22+
<#
23+
The comma operator is used to create an array.
24+
Spaces are typically ignored.
25+
#>
2226
$Ages = 12, 25, 18, 64
2327

2428
<#
25-
Individual elements of an array can be accessed with square-bracket index syntax.
26-
Arrays are zero-indexed; the first element is at index 0, the second at 1, etc.
29+
Individual elements of an array can be accessed with square-bracket
30+
index syntax. Arrays are zero-indexed; the first element is at index
31+
[0], the second at [1], etc.
2732
#>
2833
$Ages[0] | Should -Be 12
2934
__ | Should -Be $Ages[3]
3035
}
3136

3237
It 'can be created with the @() operator' {
3338
<#
34-
The array subexpression operator @() is used to create an array from multiple values
35-
or expressions. Within the parentheses, you can use commas, semicolons, and even line
36-
breaks to divide array elements.
39+
The array subexpression operator @() is used to create an array
40+
from multiple values or expressions. Within the parentheses, you can
41+
use commas, semicolons, or even line breaks to divide each element.
3742
#>
3843
$Names = @(
3944
'Steve'
4045
'John'; 'Jaime' # This is a messy way to do things, but it does work
4146
'Abigail', 'Serena', 'Kali'
42-
# Having everything on its own line would be much cleaner and is a common usage of this syntax.
47+
<#
48+
Having everything on its own line would be much cleaner and is a
49+
more common usage of this syntax.
50+
#>
4351
)
4452

4553
# Where is index 4 in the above array?
4654
__ | Should -Be $Names[4]
4755

4856
<#
49-
Although in many cases in PowerShell, an expression that only returns one value will
50-
not become an array, this operator forces the value or object to be wrapped in an array
51-
if the result is not already an array; it guarantes the result will be an array.
57+
Although in many cases in PowerShell, an expression that only
58+
returns one value will not become an array, this operator forces the
59+
value or object to be wrapped in an array if the result is not
60+
already an array; it guarantes the result will be an array.
5261
#>
5362
$Array = @( 10 )
5463

@@ -66,28 +75,32 @@ Describe 'Arrays' {
6675

6776
It 'can be created using the addition operator' {
6877
<#
69-
To add an element to a fixed size array, a new array must be created. PowerShell does this
70-
in the background when the addition operator is used.
78+
To add an element to a fixed size array, a new array must be
79+
created. PowerShell does this in the background when the addition
80+
operator is used.
7181
#>
7282

7383
$Ages = 12, 25, 18, 64
7484

7585
$Age = __
7686
$Ages = $Ages + $Age
7787

78-
# The operation above can be shortened using the Add-and-Assign operator.
88+
<#
89+
The operation above can be shortened using the addition and
90+
assignment combination operator.
91+
#>
7992

8093
$Age = __
8194
$Ages += $Age
8295

8396
$Ages | Should -Be 12, 25, 18, 64, 42, 59
8497

8598
<#
86-
The cost of adding an element to an array in PowerShell like this increases with the
87-
size of the array.
99+
The cost of adding an element to an array in PowerShell like this
100+
increases with the size of the array.
88101
89-
Each time a new element is added, the array must be copied to a new larger array to accommodate
90-
the new values.
102+
Each time a new element is added, the array must be copied to a new
103+
larger array to accommodate the new values.
91104
92105
The result of the operation is another fixed size array.
93106
#>
@@ -120,10 +133,11 @@ Describe 'Arrays' {
120133
__ | Should -Be $Others
121134

122135
<#
123-
If you know the contents of the array and want to skip specific elements, you can
124-
assign specific elements to $null to discard them. $null is one of PowerShell's
125-
automatic variable values, like $true and $false, and cannot be altered. Any data
126-
you attempt to assign to it will be ignored.
136+
If you know the contents of the array and want to skip specific
137+
elements, you can assign specific elements to $null to discard them.
138+
$null is one of PowerShell's automatic variable values, like $true
139+
and $false, and cannot be altered. Any data you attempt to assign to
140+
it will be ignored.
127141
#>
128142
$null, $Number1, $Number2 = $Others
129143
__ | Should -Be $Number1
@@ -164,7 +178,7 @@ Describe 'Arrays' {
164178

165179
$List.Count | Should -Be $Array.Count
166180

167-
# The List collection used above is covered in more detail in a later Koan.
181+
# The List collection used above will be explored more in a later topic.
168182
}
169183

170184
It 'allows use of negative indexes' {
@@ -177,9 +191,10 @@ Describe 'Arrays' {
177191

178192
$Index = __
179193
$Array[-3, $Index, -6] | Should -Be @(5, 1, 2)
194+
}
180195

181-
# You can make use of this to reverse an entire array
182-
$LastIndex = __ # Hint: needs to be a negative number!
196+
It 'can reverse an array' {
197+
$LastIndex = __
183198
$Array[-1..$LastIndex] | Should -Be @(7, 6, 5, 4, 3, 2, 1)
184199
}
185200

@@ -192,11 +207,16 @@ Describe 'Arrays' {
192207
# What about undefined negative indexes?
193208
__ | Should -Be $Array[-10]
194209

195-
# NOTE: The above will actually throw errors if you have PowerShell running in Strict Mode.
210+
<#
211+
📝 NOTE
212+
213+
The above will actually throw errors if you have PowerShell
214+
running in Strict Mode.
215+
#>
196216
}
197217

198218
It 'can create be created from a range' {
199-
# The .. notation used to reverse an array may be used to array.
219+
# The .. notation used to reverse an array can be used to copy an array.
200220

201221
$Array = 1, 2, 3, 4, 5
202222

@@ -209,26 +229,41 @@ Describe 'Arrays' {
209229
$lastLetter = '__'
210230

211231
if ($PSVersionTable.PSEdition -eq 'Core') {
212-
# PowerShell Core uses .. between the letters to create an array.
232+
# PowerShell 6.2+ can use .. between the letters to create an array.
213233

214234
$letters = $firstLetter..$lastLetter
215235

216236
$letters | Should -Be 'a', 'b', 'c', 'd'
217237
}
218238
else {
219-
# Windows PowerShell 5 and below have to work a lot harder to achieve the same thing.
239+
<#
240+
Windows PowerShell 5 and below have to work a lot harder to
241+
achieve the same thing.
242+
#>
220243

221-
$letters = ([Int][Char]$firstLetter)..([Int][Char]$lastLetter) -as [Char[]]
244+
$startIndex = [Int][Char]$firstLetter
245+
$endIndex = [Int][Char]$lastLetter
246+
$letters = ($startIndex)..($endIndex) -as [char[]]
222247

223248
$letters | Should -Be 'a', 'b', 'c', 'd'
224249
}
225250
}
226251

227252
It 'is usually of type Object[]' {
228253
<#
229-
Arrays in PowerShell are created with the type Object[]. An array of Objects.
230-
231-
The Object type can hold anything at all. A numeric value, a character, a Process, and so on.
254+
Arrays in PowerShell are created with the type Object[]. Object is
255+
the parent type of all .NET objects, and the [] suffix denotes that
256+
the type represents an array containing multiple items of that type.
257+
You may also occasionally see things like int[,] which denotes a two
258+
dimensional array containing integers.
259+
260+
In .NET, a child type can be "boxed" into its parent type and be
261+
treated the same (think of it like a child type being an add-on to
262+
the parent type; you can always cut away the additions and just work
263+
with the parent type if you really need to). Object is the sole
264+
parent type for all .NET objects, regardless of their type. As such,
265+
an array of type Object[] effectively has no restrictions on what
266+
can be inserted into one of its slots.
232267
#>
233268

234269
$Numbers = 1, 2, 3, 4
@@ -241,11 +276,12 @@ Describe 'Arrays' {
241276
'____' | Should -Be $Processes.GetType().Name
242277

243278
<#
244-
The base type of Object[], Char[], and other fixed size array types is the System.Array
245-
type, or [Array].
279+
The base type of Object[], Char[], and other fixed size array types
280+
is the System.Array type, or [Array].
246281
247-
The [Array] type describes the Length property (which is also aliased to Count in PowerShell),
248-
as well as other methods which can be used to work with the array.
282+
The [Array] type describes the Length property (which is also
283+
aliased to Count in PowerShell), as well as other methods which can
284+
be used to work with the array.
249285
#>
250286
}
251287

PSKoans/Koans/Introduction/AboutAssertions.Koans.ps1

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ param()
55
Getting Started
66
77
The PowerShell Koans are a set of exercises designed to get you familiar
8-
with PowerShell. By the time you're done, you'll have a basic
9-
understanding of the syntax of PoSH and learn a little more
10-
about scripting in general.
11-
12-
Answering Problems
13-
14-
This is where the fun begins! Each koan contains an example designed
15-
to teach you a lesson about PowerShell. If you execute the program
16-
defined in this project, you will get a message that the koan below
17-
has failed. Your job is to fill in the blanks (the __ or ____ symbols)
18-
to make it pass. Once you make the change, call Show-Karma to make sure
19-
the koan passes, and continue on to the next failing koan.
20-
With each passing koan, you'll learn more about PowerShell, and add
21-
another tool to your PowerShell scripting belt.
8+
with PowerShell. By the time you're done, you'll have a basic understanding
9+
of the syntax of PowerShell and learn a little more about scripting. This is
10+
where the fun begins! Each koan contains an example designed to teach you
11+
something about PowerShell.
12+
13+
Solving Problems
14+
15+
If you execute the `Show-Karma` command in your PowerShell console, you will
16+
get a message that one of the assertions below has failed. Your job is to
17+
fill in the blanks (the __ / ____ / $____ / '____' tokens) to make it pass.
18+
19+
Once you make the change(s) necessary and save the file, call `Show-Karma`
20+
to make sure the koan passes, and continue on to the next failing koan. With
21+
each passing koan, you'll learn more about PowerShell, and add another tool
22+
to your PowerShell scripting belt.
2223
#>
2324
Describe 'Equality' {
2425

0 commit comments

Comments
 (0)