This shows you the differences between two versions of the page.
dss:workshop-01 [2019/08/14 11:07] eduard.staniloiu created |
dss:workshop-01 [2021/03/10 23:46] (current) eduard.staniloiu |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Workshop - Introduction to D ===== | + | ====== Highschool workshop - Introduction to D ====== |
+ | |||
+ | ===== Slides ===== | ||
+ | |||
+ | {{:dss:workshop-liceu.odp|Slides}} | ||
+ | |||
+ | ===== Exercises ===== | ||
+ | |||
+ | For these exercises you can use the [[https://run.dlang.io|D Online Editor]]. | ||
+ | If you wish to install the D compiler and Standard Library, go to the [[https://dlang.org/download.html|download]] page. | ||
+ | |||
+ | ==== 1. Array Median Element ==== | ||
+ | |||
+ | Compute [[https://www.geeksforgeeks.org/median/|the median]] element of an unsorted integer array. For this, you will have to: | ||
+ | |||
+ | - Sort the array: implement any [[https://en.wikipedia.org/wiki/Sorting_algorithm#Popular_sorting_algorithms|sorting algorithm]] you wish. At this [[https://github.com/TheAlgorithms/C/tree/master/searching|link]] you will find a series of searching algorithms, each implemented in its own C file. | ||
+ | - Eliminate the duplicates: once the array is sorted, eliminating the duplicates is trivial. The array operations discussed, and more, can be found at [[https://ocw.cs.pub.ro/courses/dss/laboratoare/01#arrays|this reference]]. | ||
+ | - Select the **(n+1)/2**th element. | ||
+ | - Write [[https://ocw.cs.pub.ro/courses/dss/laboratoare/01#unittests|unittests]] to validate your implementation. | ||
+ | |||
+ | <note tip> | ||
+ | Use small [[https://ocw.cs.pub.ro/courses/dss/laboratoare/01#functions|functions]] to increase code readability and testing. | ||
+ | </note> | ||
+ | |||
+ | <note> | ||
+ | Remember to pass the **-unittest** argument to the compiler, in order to enable the unittests. | ||
+ | </note> | ||
+ | |||
+ | ==== 2. The Standard Library ==== | ||
+ | |||
+ | D has a standard library called [[https://dlang.org/phobos/|Phobos]]. Implement exercise 1 using functions from the [[https://dlang.org/phobos/std_algorithm.html|std.algorithm]] package. Use Universal Function Call Syntax (**UFCS**) for an increase in expressiveness. | ||
+ | |||
+ | ==== 3. Dictionaries ==== | ||
+ | |||
+ | Using [[https://ocw.cs.pub.ro/courses/dss/laboratoare/01#associative_arrays_aa|associative arrays]]: | ||
+ | - Store mappings between the name of your team mates and their email address. | ||
+ | - Extend the previous point to also store their phone numbers. | ||
+ | |||
+ | <note tip> | ||
+ | For bullet 2, use a [[https://ocw.cs.pub.ro/courses/dss/laboratoare/01#struct_vs_class|struct]], let's call it ColleagueInfo, to wrap their //email// and //phone// number. | ||
+ | <code> | ||
+ | struct ColleagueInfo | ||
+ | { | ||
+ | string telefon; | ||
+ | string mail; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Now go ahead and create your <name, ColleagueInfo> dictionary | ||
+ | <code> | ||
+ | ColleagueInfo[string] d; | ||
+ | </code> | ||
+ | |||
+ | </note> | ||
+ | |||
+ | ==== 4. @safe ==== | ||
+ | |||
+ | Inspect the [[https://github.com/edi33416/D-Summer-School/blob/ipworkshop/hs-workshop/4-safe/safe.d|source file]]. | ||
+ | Compile and run the code. | ||
+ | |||
+ | - What does the code do? Why is it useful to take the address of a parameter? | ||
+ | - Add the [[http://ocw.cs.pub.ro/courses/dss/laboratoare/03?&#safe_functions|@safe]] attribute to the **main** function. What happens? | ||
+ | - How can we get rid of the error messages? | ||
+ | |||
+ | ==== 5. Function templates ==== | ||
+ | |||
+ | Write a simple templated function that computes the sum of the elements of an array. | ||
+ | Your function should be able to work on any numeric type. This is exactly what we demoed in the presentation. | ||
+ | |||
+ | <note> | ||
+ | If you want to read more about D meta-programming, start with this introductory [[https://ocw.cs.pub.ro/courses/dss/laboratoare/02|hands-on]]. | ||
+ | </note> |