Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slides/185 simplify extended examples within the subprograms module #472

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion courses/fundamentals_of_ada/070_subprograms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ Subprograms
.. include:: 070_subprograms/07-function_specifics.rst
.. include:: 070_subprograms/08-expression_functions.rst
.. include:: 070_subprograms/09-potential_pitfalls.rst
.. include:: 070_subprograms/10-extended_examples.rst
.. include:: 070_subprograms/10-extended_example.rst
.. include:: labs/070_subprograms.lab.rst
.. include:: 070_subprograms/99-summary.rst
144 changes: 144 additions & 0 deletions courses/fundamentals_of_ada/070_subprograms/10-extended_example.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
===================
Extended Example
===================

-----------------------------
Implementing a Simple "Set"
-----------------------------

* We want to indicate which colors of the rainbow are in a **set**

* If you remember from the *Basic Types* module, a type is made up of values and primitive operations

* Our values will be

* Type indicating colors of the rainbow
* Type to group colors
* Mechanism to indicate which color is in our set

* Our primitive operations will be

* Create a set
* Add a color to the set
* Remove a color from the set
* Check if color is in set

--------------------
Values for the Set
--------------------

* Colors of the rainbow

.. code:: Ada

type Color_T is (Red, Orange, Yellow, Green,
Blue, Indigo, Violet,
White, Black);

* Group of colors

.. code:: Ada

type Group_Of_Colors_T is
array (Positive range <>) of Color_T;

* Mechanism indicating which color is in the set

.. code:: Ada

type Set_T is array (Color_T) of Boolean;
-- if array component at Color is True,
-- the color is in the set

----------------------------------
Primitive Operations for the Set
----------------------------------

* Create a set

.. code:: Ada

function Make (Colors : Group_Of_Colors_T) return Set_T;

* Add a color to the set

.. code:: Ada

procedure Add (Set : in out Set_T;
Color : Color_T);

* Remove a color from the set

.. code:: Ada

procedure Remove (Set : in out Set_T;
Color : Color_T);

* Check if color is in set

.. code:: Ada

function Contains (Set : Set_T;
Color : Color_T)
return Boolean;

--------------------------------------------
Implementation of the Primitive Operations
--------------------------------------------

* Implementation of the primitives is easy

* We could do operations directly on :ada:`Set_T`, but that's not flexible

.. code:: Ada

function Make (Colors : Group_Of_Colors_T) return Set_T is
Set : Set_T := (others => False);
begin
for Color of Colors loop
Set (Color) := True;
end loop;
return Set;
end Make;

procedure Add (Set : in out Set_T;
Color : Color_T) is
begin
Set (Color) := True;
end Add;

procedure Remove (Set : in out Set_T;
Color : Color_T) is
begin
Set (Color) := False;
end Remove;

function Contains (Set : Set_T;
Color : Color_T)
return Boolean is
(Set (Color));

-------------------------
Using our Set Construct
-------------------------

.. code:: Ada

Rgb : Set_T := Make ((Red, Green, Blue));
Light : Set_T := Make ((Red, Yellow, Green));

.. code:: Ada

if Contains (Rgb, Black) then
Remove (Rgb, Black);
else
Add (Rgb, Black);
end if;

*In addition, because of the operations available to arrays of Boolean, we can easily implement set operations*

.. code:: Ada

Union : Set_T := Rgb or Light;
Intersection : Set_T := Rgb and Light;
Difference : Set_T := Rgb xor Light;
137 changes: 0 additions & 137 deletions courses/fundamentals_of_ada/070_subprograms/10-extended_examples.rst

This file was deleted.

Loading