Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Computer Science II Recursion Professor: Evan Korth New York University.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
VBA Modules, Functions, Variables, and Constants
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Social Web Design 社群網站設計 1 Darby Chang 張天豪 Social Web Design 社群網站設計.
Molecular Biomedical Informatics 分子生醫資訊實驗室 Social Web Design & Research 社群網站設計 & 研究 Social Web Design & Research 1.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
在職教師教育網路應用系統程式 分享與實作研習 ---C# Tutorial(Code C) 行政網路組 傅志雄.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Functional Programming in Python Abhishek Dasgupta Indian Institute of Science Education and Research, Kolkata.
CS314 – Section 5 Recitation 9
Functional Programming
Basic 1960s It was designed to emphasize ease of use. Became widespread on microcomputers It is relatively simple. Will make it easier for people with.
Lesson #6 Modular Programming and Functions.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
CS 326 Programming Languages, Concepts and Implementation
Lesson #6 Modular Programming and Functions.
CS 326 Programming Languages, Concepts and Implementation
Functions Declarations, Function Expressions and IIFEs
Functional Programming with Java
Lesson #6 Modular Programming and Functions.
The Metacircular Evaluator
T. Jumana Abu Shmais – AOU - Riyadh
CS5220 Advanced Topics in Web Programming JavaScript Basics
6.001 SICP Variations on a Scheme
Lesson #6 Modular Programming and Functions.
CSE 3302 Programming Languages
CSE 341 Lecture 11 b closures; scoping rules
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1

Functional programming 2 using JavaScript Web Programming 網際網路程式設計

Before that 3 You should learn JavaScript first A good reference at JavaScript GardenJavaScript Garden Web Programming 網際網路程式設計

Function You know function –function f1(){ console.log(1); } f1(); Function declaration Web Programming 網際網路程式設計 4

Function expression Functions in JavaScript are first class objects –they can be passed around like any other value –var f2 = function(){ console.log(2); } f2(); Web Programming 網際網路程式設計 5

Is Web Programming 網際網路程式設計 6 there any difference between f1 and f2

Hoisting JavaScript hoists declarations –both var statements and function declarations will be moved to the top of their enclosing scope –JavaScript-Garden: Scopes and NamespacesJavaScript-Garden: Scopes and Namespaces In fact, all function declarations are automatically converted to function expressions and hoisted –try dump f1 and f2 –the place matters (assignment is not hoisted) Web Programming 網際網路程式設計 7

After assignment They are the same –both can be used as a variable –function f3($){ $(); } f3(f1); f3(f2); Web Programming 網際網路程式設計 8

Anonymous function You should be familiar with –f3(function(){ console.log(3); }); –now you know the formal name, function expression Anonymous functions can be executed directly, –(function(){ console.log(null); })(); –note the first bracket pair –self-executing anonymous function Web Programming 網際網路程式設計 9

Parameter function f4($){ console.log($) } f4(4); var f5 = function($){ console.log($) }; f5(5); (function($){ console.log($) })(null); $ is a local variable, easy? function f6($){ var _ = 0; _ += $; console.log(_); } f6(6); f6(6); Web Programming 網際網路程式設計 10

What’s Web Programming 網際網路程式設計 11 the problem of using global variables

Web Programming 網際網路程式設計 12 Closure/ 閉包 function _f7(){ var _ = 0; function f($){ _ += $; console.log(_); } return f; } var f7 = _f7(); f7(7); f7(7); f is a closure, the return exposes it f7 might be called an exposed closure –in JS, it is a function _f7 is sometimes called a factory (of an object) _ is not accessible except using the exposed functions –a conceptually private variable of f7 to f, is _ local or global?

Free/bound variable 13 閉包( Closure ) JavaScript-Garden: Closures and References Web Programming 網際網路程式設計

More practical var f8 = (function(){ // * var _ = 0; return function($){ // anonymous f _ += $; console.log(_); } })(); f8(8); f8(8); * is anonymous and self-executed _f7 Web Programming 網際網路程式設計 14

Web Programming 網際網路程式設計 15 Even more practical var f9 = { add: (function(){ var _ = 0; return function($){ _ += $; console.log(_); return this; } })(), } f9.add(9).add(9); Utilize the return value of the closure Chaining –you may see this pattern in many advanced JS code (such as jQuery plugins) Note the extra comma –my personal preference

this In JS, this is different to most other programming languages There are exactly five different ways of binding this –this; // in the global scope, it simply refers to the global object –f(); // when, calling a function, it again refers to the global object –o.f(); // calling a method, it refers to the object (o here) Yes, two were skipped, see JavaScript-Garden: How this WorksJavaScript-Garden: How this Works Web Programming 網際網路程式設計 16

Intuitive? o.f = function() { function f() { // this refers to the global object } f(); } –a common misconception is that this inside of f refers to o; while in fact, it does not Functional scoping instead of block scoping –JavaScript-Garden: Scopes and NamespacesJavaScript-Garden: Scopes and Namespaces Web Programming 網際網路程式設計 17

Web Programming 網際網路程式設計 18 An object with two closures var f10 = { add: (function(){ var _ = 0; return function($){ _ += $; console.log(_); return this; } })(), sub: (function(){ var _ = 0; return function($){ _ -= $; console.log(_); return this; } })(), }; f10.add(10).sub(10); What’s the problem

Web Programming 網際網路程式設計 19 The complete sample var f11 = (function(){ var _ = 0; return { add: function($){ _ += $; console.log(_); return this; }, sub: function($){ _ -= $; console.log(_); return this; }, } // return })(); f11.add(11).sub(11); Note the place of the closure –you may see this pattern in many advanced JS code (such as jQuery plugins)

The this problem f12 = f11.add; f12(12); f12(12).sub(12); console.log(f11.add(11)); console.log(f12(12)); Anyway, this is really a problem in practice Web Programming 網際網路程式設計 20

Any Questions? Web Programming 網際網路程式設計 21

Functional programming (FP) Uses functions as the fundamental element to program (object-oriented programming uses objects instead) In FP, functions are as in Mathematics –always return the same results when given the same input In other programming, functions should be corrected as procedures (a series of actions) –prevent redundant code –separate code (for modulization/ 模組化 ) –may return different results when given the same input Web Programming 網際網路程式設計 22

FP limits us while programming You are familiar with x = x + 1 –x is a container (a nickname to a memory address) In Mathematics, x = x + 1 –no solution –you should use x1 = x + 1 –x and x1 are symbols, their values can be undetermined but cannot be changed In FP, variables can not vary Web Programming 網際網路程式設計 23

FP limits us while programming You are familiar with x = x + 1 –x is a container (a nickname to a memory address) In Mathematics, x = x + 1 –no solution –you should use x1 = x + 1 –x and x1 are symbols, their values can be undetermined but cannot be changed In FP, variables can not vary Web Programming 網際網路程式設計 24

Limitation does not suck “Variables can not vary” looks like a disadvantage This is somehow like structured programming ( 結構化程式設計 ) which forbids the goto statement This is an argument –powerful/low-end vs. development/high-end –concept/trend changes (FP is very old, 1950s) Web Programming 網際網路程式設計 25

Limitation prevents side-effects Large-scale/collaborative works favor abstraction, modulization… Mutable variable is a critical side-effect of parallel computing (cloud computing, big data…) race conditions (ticket system), synchronization… FP has no mutable variable 關於函數編程(一) On Functional Programming 關於函數編程(一) On Functional Programming Web Programming 網際網路程式設計 26

for for (var i = 1; i < 5; i++) { // do something } –i changed [1, 2, 3, 4].forEach(function(i){ // do something }); –no mutable variable –pure FP languages have better support for list Web Programming 網際網路程式設計 27

sum() var sum = function($){ if (0 == $.length) return 0; return $[0] + sum($.slice(1)); }; Yes, no loop, everything is recursive –most FP languages optimize tail-recursion with loop –What is tail-recursion?What is tail-recursion? Web Programming 網際網路程式設計 28

prod() and and() var prod = function($){ if (0 == $.length) return 1; return $[0] * prod($.slice(1)); }; var and = function($){ if (0 == $.length) return true; return $[0] && and($.slice(1)); }; All similar? Web Programming 網際網路程式設計 29

foldr() The above three functions traverse a list and combine them (two elements at a time) into a value –the only two differences are the combine function and the empty value –can you abstract them? var foldr = function(f, e){ return function _($){ // named function expression if (0 == $.length) return e; return f($[0], _($.slice(1))); } }; var sum = foldr(function(a,b){return a+b}, 0); var prod = foldr(function(a,b){return a*b}, 1); var and = foldr(function(a,b){return a&&b}, 1); Web Programming 網際網路程式設計 30

foldr() The above three functions traverse a list and combine them (two elements at a time) into a value –the only two differences are the combine function and the empty value –can you abstract them? var foldr = function(f, e){ return function _($){ // named function expression if (0 == $.length) return e; return f($[0], _($.slice(1))); } }; var sum = foldr(function(a,b){return a+b}, 0); var prod = foldr(function(a,b){return a*b}, 1); var and = foldr(function(a,b){return a&&b}, 1); Web Programming 網際網路程式設計 31

foldr() The above three functions traverse a list and combine them (two elements at a time) into a value –the only two differences are the combine function and the empty value –can you abstract them? var foldr = function(f, e){ return function _($){ // named function expression if (0 == $.length) return e; return f($[0], _($.slice(1))); } }; var sum = foldr(function(a,b){return a+b}, 0); var prod = foldr(function(a,b){return a*b}, 1); var and = foldr(function(a,b){return a&&b}, 1); Web Programming 網際網路程式設計 32

Abstraction Examples –from goto to for, while …  abstraction of control structure –sub  abstraction of procedure –encapsulation  abstraction of data –OOP, FP  abstraction of architecture foldr() is the famous fold/reduce operation –the above is for list, other structures such as binary tree have their own fold function –a higher-order function, which use functions as data 關於函數編程(二)摺疊與抽象化 Web Programming 網際網路程式設計 33

map Another common programming pattern map(square, [1, 2, 3, 4]) returns [1, 4, 9, 16] –var map = function(f, $){ if (0 == $.length) return []; return [f($[0])].concat(map(f, $.slice(1))); }; Similar to sum()? –var map = function(f, $){ return foldr(function(a,b){ return [f(a)].concat(b)}, [] )($); }; Web Programming 網際網路程式設計 34

map Another common programming pattern map(square, [1, 2, 3, 4]) returns [1, 4, 9, 16] –var map = function(f, $){ if (0 == $.length) return []; return [f($[0])].concat(map(f, $.slice(1))); }; Similar to sum() ? –var map = function(f, $){ return foldr(function(a,b){ return [f(a)].concat(b)}, [] )($); }; Web Programming 網際網路程式設計 35

map Another common programming pattern map(square, [1, 2, 3, 4]) returns [1, 4, 9, 16] –var map = function(f, $){ if (0 == $.length) return []; return [f($[0])].concat(map(f, $.slice(1))); }; Similar to sum() ? –var map = function(f, $){ return foldr(function(a,b){ return [f(a)].concat(b)}, [] )($); }; Web Programming 網際網路程式設計 36

Another map implementation map(square) returns a function that transform [1, 2, 3, 4] to [1, 4, 9, 16] –var map = function(f){ return function($){ if (0 == $.length) return []; return [f($[0])].concat(map(f)($.slice(1))); } }; Use foldr() –var map = function(f){ return foldr(function(a,b){ return [f(a)].concat(b) }, []); }; Web Programming 網際網路程式設計 37

Another map implementation map(square) returns a function that transform [1, 2, 3, 4] to [1, 4, 9, 16] –var map = function(f){ return function($){ if (0 == $.length) return []; return [f($[0])].concat(map(f)($.slice(1))); } }; Use foldr() –var map = function(f){ return foldr(function(a,b){ return [f(a)].concat(b) }, []); }; Web Programming 網際網路程式設計 38

Another map implementation map(square) returns a function that transform [1, 2, 3, 4] to [1, 4, 9, 16] –var map = function(f){ return function($){ if (0 == $.length) return []; return [f($[0])].concat(map(f)($.slice(1))); } }; Use foldr() –var map = function(f){ return foldr(function(a,b){ return [f(a)].concat(b) }, []); }; Web Programming 網際網路程式設計 39

With this abstraction 40 we can start to discuss the property of these programming patterns (e.g. fold(map) is a fold) 關於函數編程(三)摺疊 - 映射融合定理 Web Programming 網際網路程式設計

sumsq() var sumsq = function($){ return sum(map1(square, $)) }; var sumsq = function($){ return sum(map2(square)($)) }; Waste? –[1, 4, 9, 16] var sumsq = foldr(function(a,b){ return square(a)+b }, 0); Web Programming 網際網路程式設計 41

sumsq() var sumsq = function($){ return sum(map1(square, $)) }; var sumsq = function($){ return sum(map2(square)($)) }; Waste? –[1, 4, 9, 16] var sumsq = foldr(function(a,b){ return square(a)+b }, 0); Web Programming 網際網路程式設計 42

sumsq() var sumsq = function($){ return sum(map1(square, $)) }; var sumsq = function($){ return sum(map2(square)($)) }; Waste? –[1, 4, 9, 16] var sumsq = foldr(function(a,b){ return square(a)+b }, 0); Web Programming 網際網路程式設計 43

A more practical code Array.prototype.sumsq = function(){ return foldr(function(a,b){ return square(a)+b}, 0 )(this); }; [1, 2, 3, 4].sumsq(); Extend an object, like jQuery plugins do on the jQuery object JavaScript-Garden: The Prototype –JS does not feature a classical inheritance model; instead, it uses a prototypal one –knowing this helps OOP with JS Web Programming 網際網路程式設計 44

A more practical code Array.prototype.sumsq = function(){ return foldr(function(a,b){ return square(a)+b}, 0 )(this); }; [1, 2, 3, 4].sumsq(); Extend an object, like jQuery plugins do on the jQuery object –there are other ways, see jQuery plugin tutorials JavaScript-Garden: The Prototype –JS does not feature a classical inheritance model; instead, it uses a prototypal one –knowing this helps OOP with JS Web Programming 網際網路程式設計 45

Today’s assignment 今天的任務 Web Programming 網際網路程式設計 46

Prepare your final exhibition 47 Web Programming 網際網路程式設計