Introduction Part R Radu Nicolescu Department of Computer Science - - PowerPoint PPT Presentation

introduction part r
SMART_READER_LITE
LIVE PREVIEW

Introduction Part R Radu Nicolescu Department of Computer Science - - PowerPoint PPT Presentation

Org Why FP Comp C# Comp F# Run JS Introduction Part R Radu Nicolescu Department of Computer Science University of Auckland 16 July 2018 1 / 32 Org Why FP Comp C# Comp F# Run JS 1 Organisation and Contents 2 Why FP? 3 Compiling and


slide-1
SLIDE 1

Org Why FP Comp C# Comp F# Run JS

Introduction – Part R

Radu Nicolescu Department of Computer Science University of Auckland 16 July 2018

1 / 32

slide-2
SLIDE 2

Org Why FP Comp C# Comp F# Run JS

1 Organisation and Contents 2 Why FP? 3 Compiling and running C# 4 Compiling and running F# 5 Running JS

2 / 32

slide-3
SLIDE 3

Org Why FP Comp C# Comp F# Run JS

Outline

1 Organisation and Contents 2 Why FP? 3 Compiling and running C# 4 Compiling and running F# 5 Running JS

3 / 32

slide-4
SLIDE 4

Org Why FP Comp C# Comp F# Run JS

People

  • Part R
  • Lecturer: Radu r.nicolescu@
  • Tutor: James jcoo092@
  • Part M
  • Lecturer: Mano s.manoharan@
  • Tutor: Mano s.manoharan@
  • Canvas pages and announcements are official communication

from teaching staff

  • Questions: we are happy to take verbal questions (e.g. after

lectures, tutorials, or any time when we are free)

  • Piazza is mostly for students discussion

4 / 32

slide-5
SLIDE 5

Org Why FP Comp C# Comp F# Run JS

Course materials

  • All course materials in part R – lectures, assignments, links, ...

– are also available in one single location: https://www.cs.auckland.ac.nz/courses/ compsci335s2c/lectures/radu/

  • Of course, these are also linked from Canvas / Modules:
  • slides are for online study only
  • handouts are recommended for printing
  • samples are mandatory

5 / 32

slide-6
SLIDE 6

Org Why FP Comp C# Comp F# Run JS

Organisation

  • Three lectures per week: handouts, slides, samples, additional

readings – requires homework

  • Three tutorials per week, repeating the same topics: highly

recommended – immediately after the lectures

  • Assignments: 20% of total course marks, 10% for each part
  • Test: 20% of total course marks – likely MCQ
  • Exam: 60% of total course marks – likely MCQ
  • Office hours: by appointment or immediately after the

teaching?

6 / 32

slide-7
SLIDE 7

Org Why FP Comp C# Comp F# Run JS

Contents – two parts

  • Part R (weeks 1–2, 7–10): Introduction to functional

programming (FP) and its applications to REST services, data XML and SQL

  • We emphasize fundamental concepts
  • Concepts illustrated in languages such as: C#, Node/JS, F#

– synchronous features mostly

  • Time permitting: async/await concepts, currently in C#, F#;

expected in JS – out of the callback hell

  • Part M (weeks 3–6, 11–12): Web clients, Web application

performance, Web application security, ...

7 / 32

slide-8
SLIDE 8

Org Why FP Comp C# Comp F# Run JS

Contents – two parts

  • Part R (weeks 1–2, 7–10): Introduction to functional

programming (FP) and its applications to REST services, data XML and SQL

  • We emphasize fundamental concepts
  • Concepts illustrated in languages such as: C#, Node/JS, F#

– synchronous features mostly

  • Time permitting: async/await concepts, currently in C#, F#;

expected in JS – out of the callback hell

  • Part M (weeks 3–6, 11–12): Web clients, Web application

performance, Web application security, ...

7 / 32

slide-9
SLIDE 9

Org Why FP Comp C# Comp F# Run JS

Outline

1 Organisation and Contents 2 Why FP? 3 Compiling and running C# 4 Compiling and running F# 5 Running JS

8 / 32

slide-10
SLIDE 10

Org Why FP Comp C# Comp F# Run JS

Why functional programming?

  • Functional programming (FP) is a programming style

(paradigm) which often:

  • naturally leads to more concise and bug-free programs
  • enables a better separation of concerns
  • allows a uniform integration of data from heterogeneous

sources (e.g. objects, SQL, XML, REST/ODATA)

  • provides better support for concurrency (parallel programming)
  • will make you a better programmer
  • In fact, FP permeates all current languages and frameworks –

you already use it, without its concepts

  • Concepts flow (how to future proof):

FP (F#, Haskell) ⇒ OOP (C#) ⇒ JS, Java, C++

9 / 32

slide-11
SLIDE 11

Org Why FP Comp C# Comp F# Run JS

Why functional programming?

  • Functional programming (FP) is a programming style

(paradigm) which often:

  • naturally leads to more concise and bug-free programs
  • enables a better separation of concerns
  • allows a uniform integration of data from heterogeneous

sources (e.g. objects, SQL, XML, REST/ODATA)

  • provides better support for concurrency (parallel programming)
  • will make you a better programmer
  • In fact, FP permeates all current languages and frameworks –

you already use it, without its concepts

  • Concepts flow (how to future proof):

FP (F#, Haskell) ⇒ OOP (C#) ⇒ JS, Java, C++

9 / 32

slide-12
SLIDE 12

Org Why FP Comp C# Comp F# Run JS

Why functional programming?

  • Tzu-li and Tzu-ssu were boasting about the size of their latest

programs.

  • “Two-hundred thousand lines,” said Tzu-li, “not counting

comments!”

  • Tzu-ssu responded, “Pssh, mine is almost a million lines

already.”

  • Master Yuan-Ma said, “My best program has five hundred

lines.”

  • Hearing this, Tzu-li and Tzu-ssu were enlightened.

Master Yuan-Ma: The Book of Programming (Marijn Haverbeke: Eloquent Javascript)

10 / 32

slide-13
SLIDE 13

Org Why FP Comp C# Comp F# Run JS

Why functional programming?

  • Tzu-li and Tzu-ssu were boasting about the size of their latest

programs.

  • “Two-hundred thousand lines,” said Tzu-li, “not counting

comments!”

  • Tzu-ssu responded, “Pssh, mine is almost a million lines

already.”

  • Master Yuan-Ma said, “My best program has five hundred

lines.”

  • Hearing this, Tzu-li and Tzu-ssu were enlightened.

Master Yuan-Ma: The Book of Programming (Marijn Haverbeke: Eloquent Javascript)

10 / 32

slide-14
SLIDE 14

Org Why FP Comp C# Comp F# Run JS

Why functional programming?

  • There are two ways of constructing a software design:
  • One way is to make it so simple that there are obviously no

deficiencies,

  • And the other way is to make it so complicated that there are

no obvious deficiencies. C.A.R. Hoare, 1980 ACM Turing Award Lecture (Marijn Haverbeke: Eloquent Javascript)

11 / 32

slide-15
SLIDE 15

Org Why FP Comp C# Comp F# Run JS

Why functional programming?

  • There are two ways of constructing a software design:
  • One way is to make it so simple that there are obviously no

deficiencies,

  • And the other way is to make it so complicated that there are

no obvious deficiencies. C.A.R. Hoare, 1980 ACM Turing Award Lecture (Marijn Haverbeke: Eloquent Javascript)

11 / 32

slide-16
SLIDE 16

Org Why FP Comp C# Comp F# Run JS

What about object-oriented programming?

  • The problem with object-oriented languages is they’ve got all

this implicit environment that they carry around with them.

  • You wanted a banana but what you got was a gorilla holding

the banana and the entire jungle. Joe Armstrong, interviewed in Coders at Work (Marijn Haverbeke: Eloquent Javascript)

12 / 32

slide-17
SLIDE 17

Org Why FP Comp C# Comp F# Run JS

Why C#, F#, JS?

  • C# and most modern imperative/OO languages (C++, Java)

are becoming multi-paradigm languages

  • In this direction, C#, .NET and related tools are years ahead
  • f other imperative languages
  • We also look at F#’s basics, which is a few years ahead of C#
  • We further use F# in a grad course (734), to study advanced

functional topics, including modern parallel programming

  • JS: at its inception, JS was a functional language (based on

LISP/Scheme!), but disguised in C/Java like syntax

  • Long time was misused (abused) in imperative style
  • Recently, renewed focus on sound OOP (TypeScript) and FP
  • SQL: SQL SELECT is a functional element!

13 / 32

slide-18
SLIDE 18

Org Why FP Comp C# Comp F# Run JS

Why C#, F#, JS?

  • C# and most modern imperative/OO languages (C++, Java)

are becoming multi-paradigm languages

  • In this direction, C#, .NET and related tools are years ahead
  • f other imperative languages
  • We also look at F#’s basics, which is a few years ahead of C#
  • We further use F# in a grad course (734), to study advanced

functional topics, including modern parallel programming

  • JS: at its inception, JS was a functional language (based on

LISP/Scheme!), but disguised in C/Java like syntax

  • Long time was misused (abused) in imperative style
  • Recently, renewed focus on sound OOP (TypeScript) and FP
  • SQL: SQL SELECT is a functional element!

13 / 32

slide-19
SLIDE 19

Org Why FP Comp C# Comp F# Run JS

Why C#, F#, JS?

  • C# and most modern imperative/OO languages (C++, Java)

are becoming multi-paradigm languages

  • In this direction, C#, .NET and related tools are years ahead
  • f other imperative languages
  • We also look at F#’s basics, which is a few years ahead of C#
  • We further use F# in a grad course (734), to study advanced

functional topics, including modern parallel programming

  • JS: at its inception, JS was a functional language (based on

LISP/Scheme!), but disguised in C/Java like syntax

  • Long time was misused (abused) in imperative style
  • Recently, renewed focus on sound OOP (TypeScript) and FP
  • SQL: SQL SELECT is a functional element!

13 / 32

slide-20
SLIDE 20

Org Why FP Comp C# Comp F# Run JS

Why C#, F#, JS?

  • C# and most modern imperative/OO languages (C++, Java)

are becoming multi-paradigm languages

  • In this direction, C#, .NET and related tools are years ahead
  • f other imperative languages
  • We also look at F#’s basics, which is a few years ahead of C#
  • We further use F# in a grad course (734), to study advanced

functional topics, including modern parallel programming

  • JS: at its inception, JS was a functional language (based on

LISP/Scheme!), but disguised in C/Java like syntax

  • Long time was misused (abused) in imperative style
  • Recently, renewed focus on sound OOP (TypeScript) and FP
  • SQL: SQL SELECT is a functional element!

13 / 32

slide-21
SLIDE 21

Org Why FP Comp C# Comp F# Run JS

Why C#?

  • For example, a critical ingredient of functional programming –

lambda expressions

  • FP: since Big Bang (!)
  • C#: 2007 (!)
  • C++: 2011 (C++11 – Why Lambdas Rock)

http://www.cprogramming.com/c++11/c+ +11-lambda-closures.html

  • Java: 2014 (Java 8 is Revolutionary, Java is back)

http://www.techempower.com/blog/2013/03/26/ everything-about-java-8/ http://www.infoq.com/articles/javaone2013-roundup

14 / 32

slide-22
SLIDE 22

Org Why FP Comp C# Comp F# Run JS

Why C#?

  • For example, a critical ingredient of functional programming –

lambda expressions

  • FP: since Big Bang (!)
  • C#: 2007 (!)
  • C++: 2011 (C++11 – Why Lambdas Rock)

http://www.cprogramming.com/c++11/c+ +11-lambda-closures.html

  • Java: 2014 (Java 8 is Revolutionary, Java is back)

http://www.techempower.com/blog/2013/03/26/ everything-about-java-8/ http://www.infoq.com/articles/javaone2013-roundup

14 / 32

slide-23
SLIDE 23

Org Why FP Comp C# Comp F# Run JS

Why C#?

  • Another critical ingredient, “true” generics, i.e. generic types

which are kept at runtime

  • C#: 2006 (!)
  • Current Java: generic types are processed by the compiler and

then erased – thus not present at runtime

  • Java: 201? Project Valhalla

http: //www.infoq.com/news/2014/07/Project-Valhalla

15 / 32

slide-24
SLIDE 24

Org Why FP Comp C# Comp F# Run JS

Why C#?

  • Another critical ingredient, “true” generics, i.e. generic types

which are kept at runtime

  • C#: 2006 (!)
  • Current Java: generic types are processed by the compiler and

then erased – thus not present at runtime

  • Java: 201? Project Valhalla

http: //www.infoq.com/news/2014/07/Project-Valhalla

15 / 32

slide-25
SLIDE 25

Org Why FP Comp C# Comp F# Run JS

Why C#?

  • The functional concepts and skills that we learn using C# are
  • r will be, sooner or later, available in other languages (Java)
  • C# is cross-platfrom, free and open source

http://www.infoq.com/news/2014/04/roslyn_oss http://www.mono-project.com/

  • C# is supported by powerful tools
  • Linqpad – a lightweight interactive editor, excellent for learning

and developing small snippets (free version) http://www.linqpad.net/

  • Visual Studio (2017) – a complex tool for large projects (free

community version; also, other tools freely available to compsci students) https://www.cs.auckland.ac.nz/en/about/ technical-facilities/software.html

16 / 32

slide-26
SLIDE 26

Org Why FP Comp C# Comp F# Run JS

Outline

1 Organisation and Contents 2 Why FP? 3 Compiling and running C# 4 Compiling and running F# 5 Running JS

17 / 32

slide-27
SLIDE 27

Org Why FP Comp C# Comp F# Run JS

Compiling C#

  • .CS – C# source file

Hello.cs

1 using System ; 2 3 public class Hello { 4 public s t a t i c void Main ( string [ ] args ) { 5 Console . WriteLine ( ” Hello ! ” ) ; 6 } 7 }

  • Line 5 is the only “real business” line, hmm...

18 / 32

slide-28
SLIDE 28

Org Why FP Comp C# Comp F# Run JS

Compiling C#

  • .CS – C# source file

Hello.cs

1 using System ; 2 3 public class Hello { 4 public s t a t i c void Main ( string [ ] args ) { 5 Console . WriteLine ( ” Hello ! ” ) ; 6 } 7 }

  • Line 5 is the only “real business” line, hmm...

18 / 32

slide-29
SLIDE 29

Org Why FP Comp C# Comp F# Run JS

Compiling C# – CSC – cf PATH env variable

  • Compiler output: .DLL – library; .EXE – program; collectively

known as assemblies

  • CSC – C# compiler (VS 2017)

C:\Program Files (x86)\Microsoft Visual Studio\ 2017\Enterprise\MSBuild\15.0\Bin\Roslyn

  • \Enterprise\↔ \Professional\↔ \Community\

1 CSC Hello . cs 1 CSC / t a r g e t : exe /out : Hello . exe Hello . cs

https://docs.microsoft.com/en-us/dotnet/csharp/ language-reference/compiler-options/ command-line-building-with-csc-exe

19 / 32

slide-30
SLIDE 30

Org Why FP Comp C# Comp F# Run JS

Compiling C# – CSC – cf PATH env variable

  • Compiler output: .DLL – library; .EXE – program; collectively

known as assemblies

  • CSC – C# compiler (VS 2017)

C:\Program Files (x86)\Microsoft Visual Studio\ 2017\Enterprise\MSBuild\15.0\Bin\Roslyn

  • \Enterprise\↔ \Professional\↔ \Community\

1 CSC Hello . cs 1 CSC / t a r g e t : exe /out : Hello . exe Hello . cs

https://docs.microsoft.com/en-us/dotnet/csharp/ language-reference/compiler-options/ command-line-building-with-csc-exe

19 / 32

slide-31
SLIDE 31

Org Why FP Comp C# Comp F# Run JS

Testing C# (and F#, SQL) – Linqpad

  • .LINQ – C# snippet for Linqpad (also: F#, VB, SQL)
  • LP automatically references most frequently used libraries
  • LP automatically wraps your code into full classes
  • LP can easily manage SQL/DB and Web REST connections

20 / 32

slide-32
SLIDE 32

Org Why FP Comp C# Comp F# Run JS

Testing C# (and F#, SQL) – Linqpad

  • .LINQ – C# snippet for Linqpad (also: F#, VB, SQL)
  • LP automatically references most frequently used libraries
  • LP automatically wraps your code into full classes
  • LP can easily manage SQL/DB and Web REST connections

20 / 32

slide-33
SLIDE 33

Org Why FP Comp C# Comp F# Run JS

Testing C# (and F#) – Linqpad

  • .Dump: The best kept secret, fantastic for structured data
  • Here an in-memory array of (anonymous) objects

21 / 32

slide-34
SLIDE 34

Org Why FP Comp C# Comp F# Run JS

Testing C#, F# – Linqpad

  • SQL table: C# ⇒ SQL !

22 / 32

slide-35
SLIDE 35

Org Why FP Comp C# Comp F# Run JS

Testing SQL – Linqpad

  • SQL select

23 / 32

slide-36
SLIDE 36

Org Why FP Comp C# Comp F# Run JS

Compiling and debugging C# – VS

  • .SLN – VS solution files (i.e. a group of related projects)
  • .CSPROJ – VS project file (i.e. a group of sources complied

together)

  • Many other files and folders...
  • Has great tools and is excellent for large team projects
  • Hides too much of the complexity, so it is not so great for

small learning compsci projects

  • VS is a good way to INSTALL a compatible complete set of

compilers, tools and libraries

  • But DO NOT USE VS IN 335, unless specifically advised!

24 / 32

slide-37
SLIDE 37

Org Why FP Comp C# Comp F# Run JS

Compiling and debugging C# – VS

  • .SLN – VS solution files (i.e. a group of related projects)
  • .CSPROJ – VS project file (i.e. a group of sources complied

together)

  • Many other files and folders...
  • Has great tools and is excellent for large team projects
  • Hides too much of the complexity, so it is not so great for

small learning compsci projects

  • VS is a good way to INSTALL a compatible complete set of

compilers, tools and libraries

  • But DO NOT USE VS IN 335, unless specifically advised!

24 / 32

slide-38
SLIDE 38

Org Why FP Comp C# Comp F# Run JS

Outline

1 Organisation and Contents 2 Why FP? 3 Compiling and running C# 4 Compiling and running F# 5 Running JS

25 / 32

slide-39
SLIDE 39

Org Why FP Comp C# Comp F# Run JS

Compiling F#

  • .FS – F# source file

Hello.fs

1 // v1 2 printfn ” Hello ! ” 3 4 // v2 5

  • pen System

6 Console . WriteLine ” Hello ! ” 7 8 // v3 9 l e t h e l l o () = 10 printfn ” Hello ! ” 11 h e l l o ()

  • Essentially, 3 × “real business”

26 / 32

slide-40
SLIDE 40

Org Why FP Comp C# Comp F# Run JS

Compiling F# – FSC – cf PATH env variable

  • FSC – C# compiler (VS 2017)

C:\Program Files (x86)\Microsoft SDKs\F#\10.1\ Framework\v4.0\fsc.exe

1 FSC Hello . f s 1 FSC / t a r g e t : exe /out : Hello . exe Hello . f s

https://docs.microsoft.com/en-us/dotnet/fsharp/ language-reference/compiler-options

27 / 32

slide-41
SLIDE 41

Org Why FP Comp C# Comp F# Run JS

Testing F# – Linqpad

  • .LINQ – F# snippet for Linqpad
  • LP automatically references most frequently used moduels

and namespaces

28 / 32

slide-42
SLIDE 42

Org Why FP Comp C# Comp F# Run JS

Testing F# – Linqpad

  • .LINQ – F# snippet for Linqpad
  • LP automatically references most frequently used moduels

and namespaces

28 / 32

slide-43
SLIDE 43

Org Why FP Comp C# Comp F# Run JS

Outline

1 Organisation and Contents 2 Why FP? 3 Compiling and running C# 4 Compiling and running F# 5 Running JS

29 / 32

slide-44
SLIDE 44

Org Why FP Comp C# Comp F# Run JS

Running Node JS

  • .JS – JS source file

Hello.js

1 console . out ( ” Hello ! ” )

  • No easy access to .NET libraries

30 / 32

slide-45
SLIDE 45

Org Why FP Comp C# Comp F# Run JS

Running Node JS

  • .JS – JS source file

Hello.js

1 console . out ( ” Hello ! ” )

  • No easy access to .NET libraries

30 / 32

slide-46
SLIDE 46

Org Why FP Comp C# Comp F# Run JS

Running JS under node – cf PATH env variable

  • Install node and npm

https://nodejs.org/en/download/

  • node path

C:\Program Files\nodejs

1 node Hello . j s

31 / 32

slide-47
SLIDE 47

Org Why FP Comp C# Comp F# Run JS

Running JS under node – additional packages/modules

  • Typically installed with npm = node package manager
  • Local install (current folder)

1 npm i n s t a l l ramda

  • Global install (in your Roaming profile)

1 npm −g i n s t a l l ramda

  • Set NODE PATH → Roaming npm modules (if not already)

1 set NODE PATH =%AppData%\npm\ node modules

  • Packages we will use: fs, linq-es2015, ramda, fantasy-land, ...

32 / 32