cs 596 functional programming and design fall semester ... · immutable.js tuesday, august 26, 14....

33
CS 596 Functional Programming and Design Fall Semester, 2014 Doc 1 Introduction Aug 26, 2014 Copyright ©, All rights reserved. 2014 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http:// www.opencontent.org/openpub/) license defines the copyright on this document. Tuesday, August 26, 14

Upload: others

Post on 04-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

CS 596 Functional Programming and DesignFall Semester, 2014Doc 1 Introduction

Aug 26, 2014Copyright ©, All rights reserved. 2014 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.

Tuesday, August 26, 14

Page 2: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

2

Course Overview

Tuesday, August 26, 14

Page 3: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

3

Course Issueshttp://www.eli.sdsu.edu/courses/index.html

CrashingCourse Web SiteWikiScreencastsPrerequisitesGradingClojure

Tuesday, August 26, 14

Page 4: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Crash Policy

4

Class limited to 70

Provide unofficial transcript

Hardcopy or softcopy via email

Available for free in SDSU portal

Need at least two hours before start of class

By seniority

Measured by SDSU credits on transcript

Alternate undergrad and graduate students

2 undegrad students for each graduate student

Tuesday, August 26, 14

Page 5: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Crash Policy

5

SDSU students have priority over Open University students

CS majors have priority over non-cs majors

Tuesday, August 26, 14

Page 6: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Crash Policy

6

Start adding students in class Thursday

So don't ask for add code an end of class today

No transcript by Thursday assume you have 0 SDSU units

Must be present to get add code

If miss a class then I drop you from crash list

Tuesday, August 26, 14

Page 7: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Crash List FAQ

7

Why not get a bigger room and admit everyone?

No Grader

No first hard assignment to scare people

Do you really want a 500 level class of 120 people?

Tuesday, August 26, 14

Page 8: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Crash List FAQ

8

Last day for regular students to add/drop classes

Sept 8

Open University students have lower priority than SDSU students

Tuesday, August 26, 14

Page 9: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Will this be a difficult course?

9

Yes

You need to change how you solve problems in code

very hard

publc float fizzle()x = y.foo();z = x.bar();if (z.cat() > 0)

bat = x.index();else

bat = 5;return bat;

}

f( g(x,y, h(z k(w))))

Data structures are immutable

Tuesday, August 26, 14

Page 10: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

For Thursday

10

Watch "Simplicity Matters" by Rich Hickey

http://www.youtube.com/watch?v=rI8tNMsozo0

ClojureScript Koans

Download Light Table

Tuesday, August 26, 14

Page 11: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Goal

11

Understand the common features of functional programming

Know how to use features of functional programming

Become comfortable using functional programming

Build well designed functional programs

Tuesday, August 26, 14

Page 12: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Growing Interest in Functional Programming

12

Blogs

Books

Banks & Start-up using Functional Programming

Languages adding functional features JavaC++Swift

Tuesday, August 26, 14

Page 13: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

John Carmack

13

No matter what language you work in, programming in a functional style provides benefits.

You should do it whenever it is convenient, and you should think hard about the decision when it isn’t convenient.

Tuesday, August 26, 14

http://www.altdev.co/2012/04/26/functional-programming-in-c/

Page 14: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Facebook

14

Backend of chat service - Erlang

React.js

Immutable.js

Tuesday, August 26, 14

Page 15: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Whats App

15

Bought by Facebook

Uses 1/3 number of engineers per user base

Uses Erlang for backend

Tuesday, August 26, 14

Page 16: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Erlang

16

Functional programming language

Developed by Ericsson

Used in phone switches

Ericsson achieved 99.9999999% reliability with Erlang

Machine started when your parents were born

Would have 1 second of downtime

You can't do that with Java

Tuesday, August 26, 14

Page 17: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Some Other Users of Erlang

17

Amazon's SimpleDB

Yahoo's Delicious

T-moble SMS

Tuesday, August 26, 14

Page 18: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Some Users of Clojure

18

NetflixCitigroup & Other BanksAkamaiWalmart

Lot of smaller companiesAppviseBerico TechnologiesCompass LabsDynamic Animations SystemsFactual, IncFlightCasterGeniInfinitely BetaKamaGames

http://dev.clojure.org/display/community/Clojure+Success+Stories

Tuesday, August 26, 14

Page 19: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Why the Interest in Functional Programming

19

Concurrency

One style of programming does not fit all situations

Object-Oriented programming has matured

Problems with Object-oriented programming

Tuesday, August 26, 14

Page 20: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Why Clojure

20

Practical language

Wanted pure functional languageNo imperative/oo programming

Clojure has access to JavaBut controlled access

Tuesday, August 26, 14

Page 21: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Clojure, Java & Assignments

21

Clojure has special syntax to call Java code

You can Create Java objectsCall methods on objects

So you can avoid Functional programming

Point of course is to learn Functional programming

You are not allowed to use Java code in assignments

Problems that use Java receive zero points

Tuesday, August 26, 14

Page 22: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

22

What is Functional Programming

Tuesday, August 26, 14

Page 23: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Elements of Functional Programming

23

Pure Functions

First Class Functions

Immutability

Higher-Order Functions

Recursion

Lazy Evaluation

Currying

Memoization

Destructuring

Collection Pipelines

List Compressions

Tuesday, August 26, 14

Page 24: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Pure Functions

24

Functions with no side-effects

Only depend on arguments

Don't change state

Why important

class Foo {int bar

public int notPure(int y) {return bar + y

}

public void alsoNotPure(int y) {bar = y

}

Easier to debugtestunderstand program

Tuesday, August 26, 14

Page 25: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

First Class Functions

25

Functions can be

Assigned to variables

Passed as arguments

Returned from functions

Anonymous functions

Lambdas

Closures

Why important

Flexibility

Generality

Tuesday, August 26, 14

Page 26: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Higher-Order Functions

26

Functions that operate on functions Why important

Fewer details/higher level logic

Concurrency

Tuesday, August 26, 14

Page 27: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Immutability

27

Data structures can not be modified

Like Java's Strings

Why important

Concurrency

Easier to debugtestunderstand program

Tuesday, August 26, 14

Page 28: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Lazy Evaluation

28

Operations & functions evaluated When usedNot when called

Why important

Simplifies logic

Tuesday, August 26, 14

Page 29: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Recursion

29

function factorial(n)if n = 1 return 1return n * factorial(n-1)

Tail recursion/Tail Call Optimization

When last statement is just the recursionCompiler can convert recursion into loop

Why important

Powerful tool

Tuesday, August 26, 14

Page 30: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Currying

30

Why importantfunction add(int x, int y) {return x + y;

}

addTen = add(10);

addTen(3) //returns 13

Tuesday, August 26, 14

Page 31: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Memoization

31

Why importantCache value of functions

memoize(factorial)

factorial(1000) //1000 recursive callsfactorial(1001) // 1 recursive call

Performance

Tuesday, August 26, 14

Page 32: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

Collection Pipelines

32

Why importantString[] words = {"a", "ab", "abc", "abcd", "bat"};List<String> wordList = Arrays.asList(words);List<String> longWords;longWords = wordList.stream() .filter( s -> s.length() > 2) .filter( s -> s.charAt(0) == 'a') .map( s -> s.toUpperCase()) .collect(Collectors.toList());

Higher level logic

Concurrency

Tuesday, August 26, 14

Page 33: CS 596 Functional Programming and Design Fall Semester ... · Immutable.js Tuesday, August 26, 14. Whats App 15 Bought by Facebook Uses 1/3 number of engineers per user base Uses

For Thursday

33

Watch "Simplicity Matters" by Rich Hickey

http://www.youtube.com/watch?v=rI8tNMsozo0

ClojureScript Koans

Download Light Table

Tuesday, August 26, 14