Skip to content

Commit 1f4538e

Browse files
committed
Notes on Actions.
1 parent e72fe1e commit 1f4538e

3 files changed

Lines changed: 51 additions & 44 deletions

File tree

source/code/projects/Actions/Actions/CallingAction.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,14 @@ public static void Demo(Action actionP)
88
actionP();
99
Console.WriteLine("Done calling action.");
1010
}
11+
12+
public static void DemoT(Action<int> actionP)
13+
{
14+
Console.WriteLine("Now calling action with arguments ranging from 0 to 9:");
15+
for (int i = 0; i < 10; i++)
16+
{
17+
actionP(i);
18+
}
19+
Console.WriteLine("Done calling action.");
20+
}
1121
}

source/code/projects/Actions/Actions/Program.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,27 @@ class Program
44
{
55
static void Main()
66
{
7-
// We can call our
8-
// "Test" method directly:
7+
// We can call our methods directly:
98
ExampleActions.Test();
9+
ExampleActions.Display(3);
10+
ExampleActions<int>.DisplayArray(new int[] { 20, 30, 40 });
1011

11-
// Or we can store it
12-
// in a variable and then call it.
13-
14-
/* An action is a
15-
* method that has no parameters and
16-
* does not return a value.
17-
* https://learn.microsoft.com/en-us/dotnet/api/system.action?view=net-9.0
18-
*/
12+
// Or we can store them
13+
// in variables and then call them:
1914

2015
Action test_variable = ExampleActions.Test;
2116
test_variable();
2217

23-
// Similarly with arguments:
24-
ExampleActions.Display(3);
2518
Action<int> display_variable = ExampleActions.Display;
2619
display_variable(10);
2720

28-
// We can even have actions
29-
// from classes that uses generic
30-
// type parameters.
31-
32-
int[] arrayT = { 20, 30, 40 };
33-
ExampleActions<int>.DisplayArray(arrayT);
34-
3521
Action<int[]> display_array_variable = ExampleActions<int>.DisplayArray;
36-
display_array_variable(arrayT);
22+
display_array_variable(new int[] { 10, 20, 30 });
3723

38-
// Passing an Action as an argument:
24+
// Passing an action as an argument:
3925
CallingAction.Demo(ExampleActions.Test);
26+
CallingAction.DemoT(ExampleActions.Display);
27+
28+
// Done passing an action.
4029
}
4130
}

source/lectures/misc/actions.md

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,53 @@ tags:
77

88
## Motivation
99

10-
[Wikipedia](https://en.wikipedia.org/wiki/Sorting_algorithm) explains it very nicely: sorting is ubiquitous in Computer Sciences.
11-
It is a simple problem ("*How can I sort the following values the most efficiently?*") that has many solutions, but still offers open problems.
10+
[Higher-order programming](https://en.wikipedia.org/wiki/Higher-order_programming) allows to manipulate for example methods themselves.
11+
This can be useful for many purposes, and is called ["delegates"](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/) in C#.
12+
We explain its basics below, and refer to [the sorting lecture](./lectures/misc/sorting) for an example of how it can be used.
1213

13-
We only consider **correct** algorithms, i.e., one where their output is such that
14+
## Action -- In short
1415

15-
- each element is larger than or equal to the previous one, according to the order picked,
16-
- all the elements that were in the input are present in the output (with the same cardinality, if repetition is allowed).
16+
[An *action* is](https://learn.microsoft.com/en-us/dotnet/api/system.action?view=net-9.0)
1717

18-
There are many ways of "comparing" sorting algorithms.
19-
A sorting algorithm…
18+
> a method that has no parameters and does not return a value.
2019
21-
- has a best, worst and average case time complexity (measured in general in number of comparisons required),
22-
- has a best, worst and average case space complexity (i.e., "how much additional memory is required?"),
23-
- can be "stable" (i.e., equal values are not permutted),
24-
- uses *insertion*, *exchange*, *selection*, *merging* method,
25-
- is serial or parallel,
20+
An "Action<T>" is a method that has a single parameter (of type `T`) and does not return a value.
2621

27-
among other properties.
22+
Here are for example three actions:
2823

29-
# Insertion Sort Algorithm
24+
```
25+
!include code/projects/Actions/Actions/ExampleActions.cs
26+
```
27+
28+
- `Test`, `Display` and `DisplayArray` all have `void` as their return type,
29+
- `Test` does not take any argument,
30+
- `Display` takes an `int` as an argument,
31+
- `DisplayArray` takes "an array of `T`" (that is, a [generic type](./lectures/oop/generic_types) as an argument.
3032

31-
This algorithm is [nicely explained and illustrated on wikipedia](https://en.wikipedia.org/wiki/Insertion_sort), and can be implemented as follows:
33+
We can call those easily:
3234

3335
```
34-
!include`snippetStart="// Insertion Algorithm", snippetEnd="// Done with insertion Algorithm"` code/projects/Sorting/Sorting/Sorting.cs
36+
!include`snippetStart="// We can call our methods directly:", snippetEnd="// Or we can store them"` code/projects/Actions/Actions/Program.cs
3537
```
3638

37-
# Heapsort Algorithm
38-
39-
We first define some helper methods:
39+
We can also store them into variables and then call them:
4040

4141
```
42-
!include`snippetStart="// Helper methods for Heapsort", snippetEnd="// Done with helper methods for Heapsort"` code/projects/Sorting/Sorting/Sorting.cs
42+
!include`snippetStart="// in variables and then call them:", snippetEnd="// Passing an Action as an argument:"` code/projects/Actions/Actions/Program.cs
4343
```
4444

45-
and then leverage the heap structure to sort:
45+
As we can see, `ExampleActions.Display` is of type `Action<int>` since the `Display` method takes an `int` as argument.
46+
47+
## Action as Parameter
48+
49+
Method can take actions as parameter:
4650

4751
```
48-
!include`snippetStart="// Heapsort Algorithm", snippetEnd="// Done with heapsort Algorithm"` code/projects/Sorting/Sorting/Sorting.cs
52+
!include code/projects/Actions/Actions/CallingAction.cs
4953
```
5054

51-
Note that `PercDown` builds a *max heap*: once the values are "pre-sorted **greater value first**", removing the first one to move it to the *end* of the list makes the list sorted from smallest to greatest value once we are done.
55+
and then can be passed an action as an argument:
56+
57+
```
58+
!include`snippetStart="// Passing an action as an argument:", snippetEnd="// Done passing an action."`code/projects/Actions/Actions/Program.cs
59+
```

0 commit comments

Comments
 (0)