introduction part r - cs.auckland.ac.nz · orgwhy fpcomp c#comp f#run js introduction { part r radu...

27
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

Upload: lethu

Post on 29-Aug-2019

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Introduction – Part R

Radu NicolescuDepartment of Computer Science

University of Auckland

16 July 2018

1 / 32

Page 2: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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 running C#

4 Compiling and running F#

5 Running JS

2 / 32

Page 3: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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 communicationfrom teaching staff

• Questions: we are happy to take verbal questions (e.g. afterlectures, tutorials, or any time when we are free)

• Piazza is mostly for students discussion

4 / 32

Page 4: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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

Page 5: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Organisation

• Three lectures per week: handouts, slides, samples, additionalreadings – requires homework

• Three tutorials per week, repeating the same topics: highlyrecommended – 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 theteaching?

6 / 32

Page 6: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Contents – two parts

• Part R (weeks 1–2, 7–10): Introduction to functionalprogramming (FP) and its applications to REST services, dataXML 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 applicationperformance, Web application security, ...

7 / 32

Page 7: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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 heterogeneoussources (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

Page 8: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Why functional programming?

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

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

• Tzu-ssu responded, “Pssh, mine is almost a million linesalready.”

• Master Yuan-Ma said, “My best program has five hundredlines.”

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

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

10 / 32

Page 9: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Why functional programming?

• There are two ways of constructing a software design:

• One way is to make it so simple that there are obviously nodeficiencies,

• And the other way is to make it so complicated that there areno obvious deficiencies.

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

11 / 32

Page 10: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

What about object-oriented programming?

• The problem with object-oriented languages is they’ve got allthis implicit environment that they carry around with them.

• You wanted a banana but what you got was a gorilla holdingthe banana and the entire jungle.

Joe Armstrong, interviewed in Coders at Work(Marijn Haverbeke: Eloquent Javascript)

12 / 32

Page 11: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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 aheadof 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 advancedfunctional topics, including modern parallel programming

• JS: at its inception, JS was a functional language (based onLISP/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

Page 12: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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

Page 13: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Why C#?

• Another critical ingredient, “true” generics, i.e. generic typeswhich are kept at runtime

• C#: 2006 (!)

• Current Java: generic types are processed by the compiler andthen erased – thus not present at runtime

• Java: 201? Project Valhallahttp:

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

15 / 32

Page 14: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Why C#?

• The functional concepts and skills that we learn using C# areor will be, sooner or later, available in other languages (Java)

• C# is cross-platfrom, free and open sourcehttp://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 learningand developing small snippets (free version)http://www.linqpad.net/

• Visual Studio (2017) – a complex tool for large projects (freecommunity version; also, other tools freely available to compscistudents)https://www.cs.auckland.ac.nz/en/about/

technical-facilities/software.html

16 / 32

Page 15: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Compiling C#

• .CS – C# source file

Hello.cs

1 using System ;23 pub l i c c l a s s H e l l o {4 pub l i c s t a t i c void Main ( s t r i ng [ ] a r g s ) {5 C o n s o l e . W r i t e L i n e ( ” H e l l o ! ” ) ;6 }7 }

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

18 / 32

Page 16: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Compiling C# – CSC – cf PATH env variable

• Compiler output: .DLL – library; .EXE – program; collectivelyknown 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 H e l l o . c s

1 CSC / t a r g e t : exe /out : H e l l o . exe H e l l o . c s

https://docs.microsoft.com/en-us/dotnet/csharp/

language-reference/compiler-options/

command-line-building-with-csc-exe

19 / 32

Page 17: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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

Page 18: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Testing C# (and F#) – Linqpad

• .Dump: The best kept secret, fantastic for structured data

• Here an in-memory array of (anonymous) objects

21 / 32

Page 19: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Testing C#, F# – Linqpad

• SQL table: C# ⇒ SQL !

22 / 32

Page 20: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Testing SQL – Linqpad

• SQL select

23 / 32

Page 21: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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 compliedtogether)

• 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 forsmall learning compsci projects

• VS is a good way to INSTALL a compatible complete set ofcompilers, tools and libraries

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

24 / 32

Page 22: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Compiling F#

• .FS – F# source file

Hello.fs

1 // v12 p r i n t f n ” H e l l o ! ”34 // v25 open System6 C o n s o l e . W r i t e L i n e ” H e l l o ! ”78 // v39 l e t h e l l o ( ) =

10 p r i n t f n ” H e l l o ! ”11 h e l l o ( )

• Essentially, 3 × “real business” ,

26 / 32

Page 23: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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 H e l l o . f s

1 FSC / t a r g e t : exe /out : H e l l o . exe H e l l o . f s

https://docs.microsoft.com/en-us/dotnet/fsharp/

language-reference/compiler-options

27 / 32

Page 24: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Testing F# – Linqpad

• .LINQ – F# snippet for Linqpad

• LP automatically references most frequently used moduelsand namespaces

28 / 32

Page 25: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Running Node JS

• .JS – JS source file

Hello.js

1 c o n s o l e . out ( ” H e l l o ! ” )

• No easy access to .NET libraries

30 / 32

Page 26: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

Running JS under node – cf PATH env variable

• Install node and npm

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

• node pathC:\Program Files\nodejs

1 node H e l l o . j s

31 / 32

Page 27: Introduction Part R - cs.auckland.ac.nz · OrgWhy FPComp 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

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