walking through 3 code snippets

64
Walking Through 3 Code Snippets AKA: Pointers and Scope are important

Upload: perspectivezoom

Post on 20-Feb-2017

324 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Walking through 3 Code Snippets

Walking Through 3 Code Snippets

AKA: Pointers and Scope are important

Page 2: Walking through 3 Code Snippets

Code to Run Through

• Tanner’s Array#Each• Oleg’s Default Hash Modification• Marcus’ Blorgotron Closure Scope

Page 3: Walking through 3 Code Snippets

Tanner’s Array#Each

fruits = %w{ orange apple strawberry banana mango }

fruits.count.times { |i| fruits[i] += " is delicious" }

fruits.each { |fruit| fruit += " is delicious" }

from http://bitsandbytesandallthingsnice.tumblr.com/post/25137722066/array-each-returns-same-array

Given this array

This works

But this doesn’t

Page 4: Walking through 3 Code Snippets

Tanner’s Array#Each

fruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" endfruits.each do |fruit| fruit += " is “delicious”end

Rewritten Slightly…

Page 5: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" endfruits.each do |fruit| fruit += " is delicious" }end

Track Variables Here

(AKA The Global Environment)

Page 6: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" endfruits.each do |fruit| fruit += " is delicious" }end

Variables Names Working Memory

Page 7: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

Page 8: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

[] 0 1 2

Page 9: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

[] 0 1 2

fruits

Page 10: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

[] 0 1 2

fruits

Sidenote: Order of OperationsIn Math• Parentheses First• Multiplication / Division• Addition / Subtraction

(5 + 3) + 12 * 4 / 2

8 + 48 / 2

8 + 24

32

Page 11: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

[] 0 1 2

fruits

Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Left to right• Assignment (=) is last

x = 3.add(5 + 2)

x = 3.add(7)

x = 10

Page 12: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

[] 0 1 2

fruits

Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Exceptions• Logic

• Left to right• Assignment (=) is last

Page 13: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

[] 0 1 2

fruits

Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Exceptions• Logic

• Left to right• Assignment (=) is last

puts (“something true” || something_illegal)

Page 14: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }

orangeapplemango

[] 0 1 2

fruits

Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Exceptions• Logic• Function / Block Definitions

• Left to right• Assignment (=) is last

Page 15: Walking through 3 Code Snippets

Tanner’s Array#Each

fruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" end

Snippet 1: The one that works

Page 16: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" end

orangeapplemango

[] 0 1 2

fruits

Page 17: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" end

orangeapplemango

[] 0 1 2

fruits

Page 18: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] += " is delicious" end

orangeapplemango

[] 0 1 2

fruits

Page 19: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

0

Page 20: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

0

i

Page 21: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

i

orange

0

Page 22: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

i

orange “ is delicious"

0

Page 23: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

i

orange “ is delicious"

0

orange is delicious

Page 24: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious"end

orangeapplemango

[] 0 1 2

fruits

i

orange is delicious

0

Page 25: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

i orange is delicious

i apple is delicious

i mango is delicious

0

1

2

Page 26: Walking through 3 Code Snippets

Tanner’s Array#EachSnippet 2: The one that doesn’t work

fruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end

Page 27: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end

Page 28: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

Page 29: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

orange

Page 30: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

orange

fruit

Page 31: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious"

end

orangeapplemango

[] 0 1 2

fruits

orange

fruit

orange is delicious

Page 32: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

orange

fruit

orange is delicious

Page 33: Walking through 3 Code Snippets

Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end

orangeapplemango

[] 0 1 2

fruits

fruit orange is delicious

fruit apple is delicious

fruit mango is delicious

Page 34: Walking through 3 Code Snippets

Tanner’s Array#Each

Let’s compare the two ways

Page 35: Walking through 3 Code Snippets

Tanner’s Array#Each

This example does the work, but the work is unreachablefruits.each { |fruit| fruit += fruit " is delicious" }

orangeapplemango

[] 0 1 2

fruits

This example worksfruits.count.times {|i| fruits[i] += " is delicious" }

[] 0 1 2

fruits orange is delicious

apple is delicious

mango is delicious

fruit orange is delicious

fruit apple is delicious

fruit mango is delicious

Unreacha

ble!

Page 36: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]Hsh[“other_key”]

Page 37: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

stuff

Page 38: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}else

stuff

Page 39: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}else

stuffhsh

Page 40: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}else

stuffhsh

Page 41: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}else

stuffhsh

stuff

Page 42: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}else

stuffhsh

stuff

<<

Page 43: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}else

stuffhsh stuff

Page 44: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}else

stuffhsh stuff

something else

Page 45: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}keyelse

stuffhsh stuff

something else

Page 46: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}keyelse

stuffhsh stuff

something else

Page 47: Walking through 3 Code Snippets

Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]

{}keyelse

stuffhsh stuff

something else

Page 48: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scopevar Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

Page 49: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scopevar Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

Page 50: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scopevar Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

Page 51: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

Page 52: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

Page 53: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

function(){return secret;}function(){secret++;}

Page 54: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

Page 55: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg

Page 56: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg

Page 57: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53secret

var Blorgotron = function() { secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg

blorg.get()

blorg[“get”]()

{get: function(){ return secret;}, inc: function(){secret++;} } }[“get”]()

function(){ return secret;}()

Evaluating blorg.get()

Page 58: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

0.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg

Page 59: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

1.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg

Page 60: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

1.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg

Page 61: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

1.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg 0.24secret{}getinc

function(){return secret;}function(){secret++;}

Page 62: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

1.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg 0.24secret{}getinc

function(){return secret;}function(){secret++;}

blorg2

Page 63: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

1.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg 1.24secret{}getinc

function(){return secret;}function(){secret++;}

blorg2

Page 64: Walking through 3 Code Snippets

Marcus’ Blorgotron Closure Scope

1.53secret

var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }

blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()

blorg2 = Blorgotron()blorg2.inc()

{}getinc

function(){return secret;}function(){secret++;}

blorg 1.24secret{}getinc

function(){return secret;}function(){secret++;}

blorg2