Download - Programming paradigm

Transcript
Page 1: Programming paradigm

Programming ParadigmsBhavin Kamani

Page 2: Programming paradigm

Cuisine

Page 3: Programming paradigm

Music Genre

Page 4: Programming paradigm
Page 5: Programming paradigm

Imperative Programming

Instructional

Command

Variable State

Conditions

Loops

Page 6: Programming paradigm

Direct

Efficient

Inflexible

Side Effects

Page 7: Programming paradigm

Assembly, C

COBOL, Visual Basic

Page 8: Programming paradigm

Object OrientedProgramming

Page 9: Programming paradigm
Page 10: Programming paradigm

Abstraction

Maintainibility

Learning Curve

Over Engineering

Page 11: Programming paradigm

C++, Java, C#

Ruby, Python

Page 12: Programming paradigm

Declarative Programming

"What" instead of "How"

Reduced Side effects

Order of statements not crucial

Page 13: Programming paradigm

Domain Specific Languages

SQL

Page 14: Programming paradigm

Domain Specific Languages

Regular Expression

Page 15: Programming paradigm

Functional Programming

Page 16: Programming paradigm

Composability

Concurrency

Learning Curve

Main stream support

Page 17: Programming paradigm

LISP, Haskell, Erlang

Closure, Scala, F#

Page 18: Programming paradigm

Multi-Paradigm Programming

Page 19: Programming paradigm

http://www.99-bottles-of-beer.net

JavaScript

Page 20: Programming paradigm

Lyrics99 bottles of beer on the wall, 99 bottles of beer.Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.Take one down and pass it around, 96 bottles of beer on the wall.----------------------------------------------------------------------------------------------------------------------------------------------------

1 bottle of beer on the wall, 1 bottle of beer.Take one down and pass it around, no more bottles of beer on the wall.

No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.

Page 21: Programming paradigm

Imperative Style

var lyrics = [];

for (var bottles = 99; bottles > 0; bottles--) { lyrics.push(bottles + " bottles of beer on the wall, " + bottles + " bottles of beer"); var next_bottles = ((bottles-1) == 0 ? "no more" : (bottles-1)); lyrics.push("Take one down and pass it around, " + next_bottles + " bottles of beer on the wall."); } var zero_bottle = "No more"; lyrics.push(zero_bottle + " bottles of beer on the wall, " + zero_bottle + " bottles of beer"); lyrics.push("Go to the store and buy some more, " + "99 bottles of beer on the wall.");

document.writeln(lyrics.join("<BR>"));

Page 22: Programming paradigm

Object-Oriented Style var BottleSong = function(num_bottles, stanza){ this.num_bottles = num_bottles; this.stanza = stanza; }; BottleSong.prototype = { sing: function(separator){ var beer_form = " bottles of beer"; for (var bottles = this.num_bottles; bottles > 0; bottles--) { this.stanza.addLine(bottles + beer_form + " on the wall, " + bottles + beer_form); var next_bottles = ((bottles-1) == 0 ? "no more" : (bottles-1)); this.stanza.addLine("Take one down and pass it around, " + next_bottles + beer_form + " on the wall."); } var zero_bottle = "No more"; this.stanza.addLine(zero_bottle + beer_form + " on the wall, " + zero_bottle + beer_form); this.stanza.addLine("Go to the store and buy some more, " + this.num_bottles + beer_form + " on the wall."); return this.stanza.write(separator); } }

var Stanza = function(){ this.lines = []; }; Stanza.prototype = { addLine: function(line){ this.lines.push(line); },

write: function(separator) { return this.lines.join(separator); } }

var song = new BottleSong(99, new Stanza()); document.writeln(song.sing("<BR>"));

Page 23: Programming paradigm

Functional Style var BottleSong = function(num_bottles){ var bottles = function(n){ return (n == 0 ? "no more" : n) + " bottles"; }

this.num_bottles = num_bottles; this.last_stanza = function(){ return ["No more bottles of beer on the wall, no more bottles of beer.", "Go to the store and buy some more, 99 bottles of beer on the wall."]; } this.stanza = function(n) { var line = [bottles(n) + " of beer on the wall, " + bottles(n) + " of beer."]; line.push("Take one down and pass it around, " + bottles(n-1) + " of beer on the wall."); return line; } }

BottleSong.prototype = { sing: function(separator){ var bottles = _.range(this.num_bottles,0,-1) var that = this; return _.reduce(bottles, function(lyrics, n) { return lyrics.concat(that.verse_n(n)); },[]).concat(that.verse_0()).join(separator); } }

var song = new BottleSong(99); document.writeln(song.sing("<BR>"));

Page 24: Programming paradigm

bottles 0 = "no more bottles"bottles 1 = "1 bottle"bottles n = show n ++ " bottles"

verse 0 = "No more bottles of beer on the wall, no more bottles of beer.\n" ++ "Go to the store and buy some more, 99 bottles of beer on the wall."

verse n = bottles n ++ " of beer on the wall, " ++ bottles n ++ " of beer.\n" ++ "Take one down and pass it around, " ++ bottles (n-1) ++ " of beer on the wall.\n"

main = mapM (putStrLn . verse) [99,98..0]

Haskel Solution

Page 25: Programming paradigm

(defn bottles-str [n] (str (cond (= 0 n) "no more bottles" (= 1 n) "1 bottle" :else (format "%d bottles" n)) " of beer"))

(defn print-bottle [n] (println (format "%s on the wall, %s." (bottles-str n) (bottles-str n))) (println "Take one down and pass it around," (bottles-str (dec n)) "on the wall.")) (defn sing [n] (dorun (map print-bottle (reverse (range 1 (inc n))))) (println "No more bottles of beer on the wall, no more bottles of beer.") (println "Go to the store and buy some more," (bottles-str n) "on the wall.")) (sing 99)

Clojure Solution

Page 26: Programming paradigm

Piet Solution

Page 27: Programming paradigm

Whitespace Solution

Page 28: Programming paradigm

Paradigm Evolution

Page 29: Programming paradigm
Page 30: Programming paradigm
Page 31: Programming paradigm

Java 8

Lambda Expression

Page 32: Programming paradigm