Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dss:workshop-01 [2019/08/15 15:15]
eduard.staniloiu old revision restored (2019/08/14 15:19)
dss:workshop-01 [2021/03/10 23:46] (current)
eduard.staniloiu
Line 1: Line 1:
 ====== Highschool workshop - Introduction to D ====== ====== Highschool workshop - Introduction to D ======
 +
 +===== Slides =====
 +
 +{{:​dss:​workshop-liceu.odp|Slides}}
  
 ===== Exercises ===== ===== Exercises =====
Line 61: Line 65:
 ==== 5. Function templates ==== ==== 5. Function templates ====
  
-<code D> +Write a simple templated function that computes the sum of the elements of an array. 
-T numbersSum(T)(T[] numbers) +Your function should be able to work on any numeric typeThis is exactly what we demoed in the presentation.
-+
-    T sum; +
-    ​foreach(num;​ numbers) +
-    { +
-        sum += num; +
-    } +
-    return sum; +
-}         +
-     +
-void main() +
-+
-    int[] arr = [1, 2, 3, 4, 5]; +
-    writeln(numbersSum!int(arr));​ // 15 +
-     +
-    float[] arr2 = [1.0, 2.0, 3.0, 4.0, 5.0]; +
-    writeln(numbersSum!float(arr2));​ // 15 +
-+
-</​code>​+
  
-<code D+<note
-T numbersSum(T)(T[] numbers) +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]]. 
-if (is(T == int) || is(T == float)) +</note>
-+
-    T sum; +
-    foreach(num;​ numbers) +
-    { +
-        sum += num; +
-    } +
-    return sum; +
-} +
-</code>+
  
-<code D> 
-import std.traits : isNumeric; 
- 
-T numbersSum(T)(T[] numbers) 
-if (isNumeric!T) 
-{ 
-    T sum; 
-    foreach(num;​ numbers) 
-    { 
-        sum += num; 
-    } 
-    return sum; 
-} 
-</​code>​ 
dss/workshop-01.1565871352.txt.gz · Last modified: 2019/08/15 15:15 by eduard.staniloiu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0