introduction ii extended
play

Introduction II (extended) Radu Nicolescu Department of Computer - PowerPoint PPT Presentation

A#1R Drive Lambdas Frs Read Err Args Introduction II (extended) Radu Nicolescu Department of Computer Science University of Auckland 19 July 2018 1 / 26 A#1R Drive Lambdas Frs Read Err Args 1 Assignment #1R 2 Driving FP 3 Lambdas


  1. A#1R Drive Lambdas Frs Read Err Args Introduction II (extended) Radu Nicolescu Department of Computer Science University of Auckland 19 July 2018 1 / 26

  2. A#1R Drive Lambdas Frs Read Err Args 1 Assignment #1R 2 Driving FP 3 Lambdas 4 Frequencies 5 Reading 6 Error handling 7 Command-line arguments 2 / 26

  3. A#1R Drive Lambdas Frs Read Err Args Outline 1 Assignment #1R 2 Driving FP 3 Lambdas 4 Frequencies 5 Reading 6 Error handling 7 Command-line arguments 3 / 26

  4. A#1R Drive Lambdas Frs Read Err Args Assignment #1R • Briefly, you have to write two programs that solve the stated problem: one in C# and the other in Node js. • Optionally, you may wish to develop an F# version, which may give you a bonus. • Samples in the tutorial. Please attend the tutorials! • How to check? First, check against the given test case! • Then, we should be able to develop our own test cases! • This is a requirement for all developers! The customers will not give us detailed test cases! • Not even detailed specs — in this case the specs are clear enough 4 / 26

  5. A#1R Drive Lambdas Frs Read Err Args Assignment #1R • Briefly, you have to write two programs that solve the stated problem: one in C# and the other in Node js. • Optionally, you may wish to develop an F# version, which may give you a bonus. • Samples in the tutorial. Please attend the tutorials! • How to check? First, check against the given test case! • Then, we should be able to develop our own test cases! • This is a requirement for all developers! The customers will not give us detailed test cases! • Not even detailed specs — in this case the specs are clear enough 4 / 26

  6. A#1R Drive Lambdas Frs Read Err Args Outline 1 Assignment #1R 2 Driving FP 3 Lambdas 4 Frequencies 5 Reading 6 Error handling 7 Command-line arguments 5 / 26

  7. A#1R Drive Lambdas Frs Read Err Args Driving FP • First, a few simple “driving” instructions for FP • Later, we will have a closer look at the under-the-hood magic • Terminology – high-level functions for processing sequences (lists, arrays, ...) • Traditional FP: map, filter, reduce, ... • LINQ (.NET): Select, Where, Aggregate, ... (Why?) • C#: LINQ Select ... • F#: LINQ Select ..., FP map ... (access to both worlds!) • JS: various packages. We use linq-es2015, very similar to LINQ! But we need to install it first with npm (if not already)! 6 / 26

  8. A#1R Drive Lambdas Frs Read Err Args Driving FP • First, a few simple “driving” instructions for FP • Later, we will have a closer look at the under-the-hood magic • Terminology – high-level functions for processing sequences (lists, arrays, ...) • Traditional FP: map, filter, reduce, ... • LINQ (.NET): Select, Where, Aggregate, ... (Why?) • C#: LINQ Select ... • F#: LINQ Select ..., FP map ... (access to both worlds!) • JS: various packages. We use linq-es2015, very similar to LINQ! But we need to install it first with npm (if not already)! 6 / 26

  9. A#1R Drive Lambdas Frs Read Err Args Driving FP • First, a few simple “driving” instructions for FP • Later, we will have a closer look at the under-the-hood magic • Terminology – high-level functions for processing sequences (lists, arrays, ...) • Traditional FP: map, filter, reduce, ... • LINQ (.NET): Select, Where, Aggregate, ... (Why?) • C#: LINQ Select ... • F#: LINQ Select ..., FP map ... (access to both worlds!) • JS: various packages. We use linq-es2015, very similar to LINQ! But we need to install it first with npm (if not already)! 6 / 26

  10. A#1R Drive Lambdas Frs Read Err Args Outline 1 Assignment #1R 2 Driving FP 3 Lambdas 4 Frequencies 5 Reading 6 Error handling 7 Command-line arguments 7 / 26

  11. A#1R Drive Lambdas Frs Read Err Args Lambdas – C# • Briefly, a lambda is an anonymous function with a crisp arrow notation and automatically inferred types (if possible): • The following Select uses a previously defined named function: 1 int f ( int x ) { return x+1; } 1 var a = new [ ] { 10 , 20 , 30 , } ; 2 var b = a . S e l e c t ( f ) ; // 11 , 21 , 31 • The following Select uses an equivalent lambda: 1 var a = new [ ] { 10 , 20 , 30 , } ; 2 var c = a . S e l e c t ( x = > x+1 ) ; // 11 , 21 , 31 8 / 26

  12. A#1R Drive Lambdas Frs Read Err Args Lambdas – C# • Briefly, a lambda is an anonymous function with a crisp arrow notation and automatically inferred types (if possible): • The following Select uses a previously defined named function: 1 int f ( int x ) { return x+1; } 1 var a = new [ ] { 10 , 20 , 30 , } ; 2 var b = a . S e l e c t ( f ) ; // 11 , 21 , 31 • The following Select uses an equivalent lambda: 1 var a = new [ ] { 10 , 20 , 30 , } ; 2 var c = a . S e l e c t ( x = > x+1 ) ; // 11 , 21 , 31 8 / 26

  13. A#1R Drive Lambdas Frs Read Err Args Outline 1 Assignment #1R 2 Driving FP 3 Lambdas 4 Frequencies 5 Reading 6 Error handling 7 Command-line arguments 9 / 26

  14. A#1R Drive Lambdas Frs Read Err Args Building frequencies – C# Pipeline of fluent method chaining (cascading) – no explicit loops! 1 IEnumerable < ( int , string ) > Frequencies ( string [ ] words ) 2 var f r s = words 3 . GroupBy ( s = > s . ToUpper ( ) ) // .Dump() 4 . S e l e c t ( g = > ( g . Count () , g . Key )) // .Dump() 5 . OrderByDescending ( kc = > kc . Item1 ) // .Dump() 6 // . . . 7 ; 8 f r s ; return 9 } 1 void Main () { 2 string [ ] words = { ”dd” , ”DD” , . . . } ; 3 var f r s = Frequencies ( words ) ; 4 f r s .Dump ( ” f r s ” ) ; // Linqpad 5 } 10 / 26

  15. A#1R Drive Lambdas Frs Read Err Args Building frequencies • GroupBy: groups the the given words into groups – 4 in our sample – according to their uppercase equivalent • Select: takes our 4 groups, maps each group into a pair • #1: count (word frequency) • #2: word in uppercase • OrderByDescending: orders on decreasing frequencies (counts) • Something remains to be done, right? • Maybe secondary ordering... and take only the required number of top rows? 11 / 26

  16. A#1R Drive Lambdas Frs Read Err Args Building frequencies • GroupBy: groups the the given words into groups – 4 in our sample – according to their uppercase equivalent • Select: takes our 4 groups, maps each group into a pair • #1: count (word frequency) • #2: word in uppercase • OrderByDescending: orders on decreasing frequencies (counts) • Something remains to be done, right? • Maybe secondary ordering... and take only the required number of top rows? 11 / 26

  17. A#1R Drive Lambdas Frs Read Err Args Building frequencies – LP Dump after GroupBy 12 / 26

  18. A#1R Drive Lambdas Frs Read Err Args Building frequencies – LP Dump after Select 13 / 26

  19. A#1R Drive Lambdas Frs Read Err Args Building frequencies – LP Dump after OrderByDescending 14 / 26

  20. A#1R Drive Lambdas Frs Read Err Args Building frequencies – JS + linq-es2015 1 var Enumerable = r e q u i r e ( ’ linq − es2015 ’ ) // LINQ for JS 1 function Frequencies ( words ) { 2 l e t f r s = Enumerable . asEnumerable ( words ) 3 . GroupBy ( s = > s . toUpperCase ( ) ) 4 . S e l e c t ( g = > [ g . length , g . key ] ) 5 . OrderByDescending ( kc = > kc [ 0 ] ) 6 // . . . 7 . ToArray () 8 return f r s 9 } 1 function Main () { 2 l e t words = [ ”dd” , ”DD” , . . . ] 3 l e t f r s = Frequencies ( words ) 4 f r s . forEach ( kc = > console . log ( kc )) 5 } 15 / 26

  21. A#1R Drive Lambdas Frs Read Err Args C# vs JS pairs • C#: has genuine pairs – here ValueTuple < int , string > , for short ( int , string ) 1 . . . ( g . Count () , g . Key ) 2 . . . kc . Item1 • C#: has also named pairs (if you prefer)... • JS: we can use arrays with two elements 1 . . . [ g . length , g . key ] 2 . . . kc [ 0 ] 16 / 26

  22. A#1R Drive Lambdas Frs Read Err Args C# vs JS pairs • C#: has genuine pairs – here ValueTuple < int , string > , for short ( int , string ) 1 . . . ( g . Count () , g . Key ) 2 . . . kc . Item1 • C#: has also named pairs (if you prefer)... • JS: we can use arrays with two elements 1 . . . [ g . length , g . key ] 2 . . . kc [ 0 ] 16 / 26

  23. A#1R Drive Lambdas Frs Read Err Args Outline 1 Assignment #1R 2 Driving FP 3 Lambdas 4 Frequencies 5 Reading 6 Error handling 7 Command-line arguments 17 / 26

  24. A#1R Drive Lambdas Frs Read Err Args Reading – C# • Assume that fname=”read-test.txt” (the given file) 1 To be , or not to be : \ r \ n 2 \ r \ n 3 That i s the question ! \ r \ n 4 \ r \ n • We can read all text into one single string: 1 t e x t = F i l e . ReadAllText ( fname ) ; var • We get: 1 ”To be , or not to be : \\ r \\ n \\ r \\ n . . . \ \ r \\ n \\ r \\ n” • Note extra spaces and new line characters • How to replace any seq of non-wanted chars by one single char? 18 / 26

Recommend


More recommend