Review Session 6

Report 4 Downloads 41 Views
Review Session 6 COMP110

Outline • for loops • Values vs References • Filter, Map, Reduce • Functional interfaces

Loops • Recall: while () { // do something }

let i: number = 0; while (i < 10) { print(i); i++; }

for Loops for () { // do something } Ex. let a: number[] = [2, 4, 7, 3, 9]; for (let i: number = 0; i < a.length; i++) { print(a[i]); }

for vs. while • Every while loop can be written as a for loop and vice versa • for loop has shorter syntax, in CS laziness is a virtue • Pros/Cons • for loops make it easier not to write infinite loops • for loops more concise • while loops are better when we don’t know exactly how many times we need to loop

Types of types • All types can be put into two categories: value and reference • Value types are “simple” types: (number, boolean, string) • Reference types are compositions of value types (WeatherRow, Message, Person, arrays, etc.) • We can define our own types • Remember, a class defines a type

Value Types • Value types can be assigned and stored directly by the computer let x: number = 5; let y: number = x; x = x + 5; print(x); x:5 y:5 print(y);

What get’s printed let Jeffrey: Person = new Person(); jeffrey.name = “Jeffrey Young”; let copy: Person = jeffrey; copy.name = “Jeff Young”; print(copy.name); print(jeffrey.name); class Person { name: string; }

Reference types • Reference types are stored as references in the computer Person jeffrey = new Person(); jeffrey.name = “Jeffrey Young”; Person copy = jeffrey; copy.name = “Jeff Young”; print(copy.name); print(jeffrey.name); class Person { name: string; }

Reference Types • When you make a direct assignment of an reference type, only the reference is copied • Modifying either reference will affect the underlying values • Always pay attention to this on worksheets, problem sets, exams!

Filter 1. Create empty array to hold output 2. Loop over each element in the input array a. Apply the given function to the element, if this function returns true, add the element to the output array

3. Return the output array

*Filter returns a smaller array than the one passed in

Filter

Map 1. Create empty array to hold output 2. Loop over each element in the input array a. Apply the given function to the element and add the result to the output array

3. Return the output array

*Map returns a an array that is the same size as the one passed in

Map

Reduce 1. Loop over each element in the input array a. Apply the given function to the element and store the return value

2. Return the return value

*Reduce returns a single value

Reduce

https://goo.gl/c6Bxz3

Functions as types • Functions can be stored as variables and passed as arguments to other functions • Remember, all variables and parameters in TS must have a type • A functions type consists of the number, order, and type of its parameters plus the return value

function foo (x: number, y: string): void { function bar (x: string, y: number): void { // elided // elided } }

Functional Interfaces • How do we give a name to these function types • Functional interfaces!

• Definition interface { (: , … ): ; }

Functional Interface • A functional interface defines a new type, just like defining a class • Variables can be typed as a functional interface • Functions can be passed as parameters to other functions!

interface Foo { (x: number): boolean; } function test (input: number): boolean { // elided } let fn: Foo = test; fn(5)

Generics • Waste of time to define a functional interface for every possible combination of types • Ex. interface FooNumber { (x: number): boolean; }

interface FooString { (x: string): boolean; }

interface FooBoolean { (x: boolean): boolean; }

Generics • Instead, specify a generic interface that satisfies all combinations interface Foo { (x: T): boolean; } interface FooNumber { interface FooString { (x: number): boolean; (x: string): boolean; } } interface FooBoolean { (x: boolean): boolean; }

Generics Usage function fooNumber(input: number): boolean { // elided } let fn: Foo = fooNumber;

interface Foo { (x: T): Boolean; }

Generics Usage function fooNumber(input: number, input2: string): boolean { // elided } let fn: Foo = fooNumber;

interface Foo { (x: T): boolean; }

Generics Usage function fooNumber(input: string): boolean { // elided } let fn: Foo = fooNumber;

interface Foo { (x: T): boolean; }

Revisiting filter, map, reduce • Remember, filter, map, reduce all take functions as arguments • When passing arguments, they must match the number of parameters defined for the function in both number and type

• So a natural question: what is the type of function that filter, map, and reduce take as an argument?

Filter • Function signature: function filter (fn: Predicate): T[] interface Predicate { (input: T): boolean; } function greaterThan10(x: number): boolean { return x > 10; }

Map • Function signature: function map (fn: Transform): U[] interface Transform { (input: T): U; } function square(x: number): number { return x * x; }

Reduce • Function signature: function reduce (fn: Reducer, memo: U): U interface Reducer { (memo: U, input: T): U; } function sum(memo: number, toAdd: number): number { return memo + toAdd; }

Pseudocode for reduce function reduce (fn: Reducer, memo: U): U { for (let i: number = 0; i < arr.length; i++) { memo = fn(memo, arr[i]); } return memo; }