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

introduction ii extended
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 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 return f r s ; 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

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 17

A#1R Drive Lambdas Frs Read Err Args

Building frequencies – LP Dump after GroupBy

12 / 26

slide-18
SLIDE 18

A#1R Drive Lambdas Frs Read Err Args

Building frequencies – LP Dump after Select

13 / 26

slide-19
SLIDE 19

A#1R Drive Lambdas Frs Read Err Args

Building frequencies – LP Dump after OrderByDescending

14 / 26

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 24

A#1R Drive Lambdas Frs Read Err Args

Reading – C#

  • Assume that fname=”read-test.txt” (the given file)

1 To be ,

  • r 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 var t e x t = F i l e . ReadAllText ( fname ) ;

  • We get:

1 ”To be ,

  • r

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

slide-25
SLIDE 25

A#1R Drive Lambdas Frs Read Err Args

Reading

  • How to replace any seq of non-wanted chars by one single

char? Regular expressions!

  • @”\s+” = ”\\s+” means any sequence of one or more

whitespaces (spaces, newlines, tabs, ...)

1 Regex rgx = new Regex (@”\ s+” ) ; // ”\\ s+” 2 var words = 3 rgx . Replace ( text , ” ” ) . Trim ( ) . S p l i t ( ’ ’ ) ;

  • We get a”normalised” ”canonical” version:

1 ”To be ,

  • r

not to be : i s the question ! ”

  • Then Trim() removes all spaces before and after:

1 ”To be ,

  • r

not to be : i s the question ! ”

19 / 26

slide-26
SLIDE 26

A#1R Drive Lambdas Frs Read Err Args

Reading

  • Finally, Split() transforms this string into an array of strings,

using a single space char ’ ’ as delimiter

1 var words = 2 rgx . Replace ( text , ” ” ) . Trim ( ) . S p l i t ( ’ ’ ) ;

  • We get an array with following contents:

1 ”To” , ”be , ” , ” or ” , ” not ” , ” to ” , ”be : ” , 2 ”That” , ” i s ” , ” the ” , ” question ! ”

  • Attention, our simple regex removes only extra whitespace!
  • We need to do a bit more to remove any non ASCII letter

(A-Za-z)

20 / 26

slide-27
SLIDE 27

A#1R Drive Lambdas Frs Read Err Args

Reading

  • Finally, Split() transforms this string into an array of strings,

using a single space char ’ ’ as delimiter

1 var words = 2 rgx . Replace ( text , ” ” ) . Trim ( ) . S p l i t ( ’ ’ ) ;

  • We get an array with following contents:

1 ”To” , ”be , ” , ” or ” , ” not ” , ” to ” , ”be : ” , 2 ”That” , ” i s ” , ” the ” , ” question ! ”

  • Attention, our simple regex removes only extra whitespace!
  • We need to do a bit more to remove any non ASCII letter

(A-Za-z)

20 / 26

slide-28
SLIDE 28

A#1R Drive Lambdas Frs Read Err Args

Reading – JS

  • We use the builtin node module fs, so we don’t need to install

it with npm

  • But we still need to open it, i.e. require in JS parlance:

1 var f s = r e q u i r e ( ’ fs ’ )

  • Here we use the synchronous read version:

1 l e t t e x t = f s . r e a dF i l e Sy n c ( fname , ’ utf8 ’ ) 2 // not needed: . t o S t r i n g () 3 4 l e t words = 5 t e x t . r e p l a c e (/\ s+/g , ” ” ) . trim ( ) . s p l i t ( ’ ’)

  • In JS we need to specify that we want a global replace, i.e. of

all occurrences (not just the first one)

21 / 26

slide-29
SLIDE 29

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

22 / 26

slide-30
SLIDE 30

A#1R Drive Lambdas Frs Read Err Args

Error handling

  • C#:

1 try { 2 . . . 3 } catch ( Exception ex ) { 4 . . . 5 } f i n a l l y { 6 . . . 7 }

  • JS: similar, but no Exception type (JS untyped)

1 } catch ( ex ) {

  • In both cases, we should have at least one try block at the

topmost level, to ensure that we catch all possible errors!

23 / 26

slide-31
SLIDE 31

A#1R Drive Lambdas Frs Read Err Args

Error handling

  • C#:

1 try { 2 . . . 3 } catch ( Exception ex ) { 4 . . . 5 } f i n a l l y { 6 . . . 7 }

  • JS: similar, but no Exception type (JS untyped)

1 } catch ( ex ) {

  • In both cases, we should have at least one try block at the

topmost level, to ensure that we catch all possible errors!

23 / 26

slide-32
SLIDE 32

A#1R Drive Lambdas Frs Read Err Args

Error handling

  • C#:

1 try { 2 . . . 3 } catch ( Exception ex ) { 4 . . . 5 } f i n a l l y { 6 . . . 7 }

  • JS: similar, but no Exception type (JS untyped)

1 } catch ( ex ) {

  • In both cases, we should have at least one try block at the

topmost level, to ensure that we catch all possible errors!

23 / 26

slide-33
SLIDE 33

A#1R Drive Lambdas Frs Read Err Args

Error handling

  • Some exceptions are automatically raised by the system...
  • However, there may be cases when we have to manually throw

exceptions – how to detect such cases?

  • In JS, the not unary operator (!) covers most issues:

undefined, null, NaN, ...

  • NaN is ”not-a-number” in IEEE standard
  • C#: we can get NaN by:

1 var x = 0.0 / 0 . 0 ; 2 Console . WriteLine ( x ) ;

  • JS: additionally, we can also get NaN by:

1 l e t x = Number ( ” foo ” ) ; 2 console . log ( x ) ;

24 / 26

slide-34
SLIDE 34

A#1R Drive Lambdas Frs Read Err Args

Error handling

  • Some exceptions are automatically raised by the system...
  • However, there may be cases when we have to manually throw

exceptions – how to detect such cases?

  • In JS, the not unary operator (!) covers most issues:

undefined, null, NaN, ...

  • NaN is ”not-a-number” in IEEE standard
  • C#: we can get NaN by:

1 var x = 0.0 / 0 . 0 ; 2 Console . WriteLine ( x ) ;

  • JS: additionally, we can also get NaN by:

1 l e t x = Number ( ” foo ” ) ; 2 console . log ( x ) ;

24 / 26

slide-35
SLIDE 35

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

25 / 26

slide-36
SLIDE 36

A#1R Drive Lambdas Frs Read Err Args

Command-line arguments

  • C#: this command-line has two items:

1 jbon007 . exe fname

  • C#:

1 . . . Main ( string [ ] args )

  • C#:

1 var args = Environment . GetCommandLineArgs ()

  • Node JS: this command-line has three items:

1 node jbon007 . j s fname

  • Node JS:

1 l e t args = process . argv

26 / 26

slide-37
SLIDE 37

A#1R Drive Lambdas Frs Read Err Args

Command-line arguments

  • C#: this command-line has two items:

1 jbon007 . exe fname

  • C#:

1 . . . Main ( string [ ] args )

  • C#:

1 var args = Environment . GetCommandLineArgs ()

  • Node JS: this command-line has three items:

1 node jbon007 . j s fname

  • Node JS:

1 l e t args = process . argv

26 / 26