Archive

Posts Tagged ‘lambda’

JS 101 – Week 3

February 15, 2011 Leave a comment

In Javascript, functions are first class objects. What does it mean to be a first class object?

A function can be used anywhere an object can be used; in particular, you can pass functions as method arguments. (We did this in last week’s homework by passing the print function into the iterateAndOperate method). This is in contrast to a language like Java where functions are not first class objects, and the programmer must use interfaces and anonymous classes to get around this problem. For instance, in Java, to delay the invocation of a function, you might do the following:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        System.out.println("I was run!");
    }
})

whereas in Javascript, you could do something like

invokeLater(function() { print("I was run") });

assuming there was an invokeLater function.

Functions and variables share the same namespace. What does this mean and what important implication does this have?

This means that you must take care in not defining variables in the global namespace that have the same name as a function. For instance,

// global variable
x = 5;

function x(args) {
    alert(args);   
}

// The global variable is shadowing the function name;
// the function is not called
x(5);

Douglas Crockford equates Javascript functions with Lambdas, and also mentions that they are a secure construct. Can you do some research and reflect on what he means by ‘secure construct’?

A lambda basically means that you can pass a function as an argument to another function; this is due to the aforementioned first class nature of functions. They are a secure construct because the scope of the functions is such that private variables in function A cannot be accessed by function B when A is passed into B. In other words,

function outer(func) {
  // "undefined", since the ‘privateVariable’
  // field is scoped only to the inner function.
  return func.privateVariable;
}

function inner(a,b,c) {
  var privateVariable = 25;
  return a + ", " + b + ", " + c;
}

alert(outer(inner("cat","dog","bear")));

f

Can you explain the concept of a closure.

A closure means that an inner function continues to have access to variables defined inside of an outer function, even after the outer function has finished. I wrote an example you can view on jsfiddle.

What is the difference between a function and a method?

A method is a function that is bound to an object, and thus it has an implicit “self” reference.

In Javascript there is no implicit type checking of arguments passed to functions. This could lead to bugs if programmers are not careful about what they pass to functions. If you create a function, how would you protect it from well meaning but careless programmers?

You can do defensive checks within the function. For instance, if a method is supposed to take in a number, you could do the following:

function numericalFunction(x) {
    if (typeof(x) != "number") {
        throw("Expected numerical value for numericalFunction; got " + x);
    }
    // Proceed as normal
}

You can also use the arguments implicit variable in the function to check that the number of arguments the user passed in is equal to the number of arguments that the method expects.

Javascript functions have implicit access to something called this. this points to different things depending on how the function was created. Can you explain why we need this, and what it represents in different type of functions.

If a function is created globally, its this pointer will be to the DOMObject. If it’s created and bound to an object, the this pointer points to that object. The following illustrates this:

function f() {
  return "f's this: " + this;
}

function nested() {
  function inner() {
    return "inner's this:" + this;
  }
  return inner();
}

// o is an object; add a method do it
var o = {};
o.f = f;
o.nested = nested;

o.newFunc = function() { return "newFunc's this: " + this; };
o.nestedFunc = function() { 
  var inner = function() {
    return "nestedFunc's inner this: " + this;
  }
  return inner();
}



print(f());
print(nested());
print(o.f());
print(o.nested());
print(o.newFunc());
print(o.nestedFunc());


// prints
// f's this: [object DOMWindow]
// inner's this:[object DOMWindow]
// f's this: [object Object]
// inner's this:[object DOMWindow]
// newFunc's this: [object Object]
// nestedFunc's inner this: [object DOMWindow]
//

Note that only the non nested functions that have been bound to object o point to that object as opposed to the DOMWindow.

The reason we want the this pointer is to be able to introspect on the type and contents of the object that is calling the function. For instance, in my last assignment, I used the this variable to produce a nice representation of an object:

nick = {name:"Nick",age:23};

// This function returns a string representation of whatever
// is invoking it
function selfStringRep() {
  var str = "";
  for (var v in this) {
    // This is not an inherited property
    if (this.hasOwnProperty(v)) {
      str += "’" + v + "’: " + this[v] +"\n";
    }
  }
  return str;
}
Object.prototype.toString = selfStringRep;
print(nick.toString());
// prints
//’name’: Nick
//’age’: 23

4.1

The solution for the cat problem talks about a ‘set’ of names. A set is a collection of values in which no value may occur more than once. If names are strings, can you think of a way to use an object to represent a set of names?

Show how a name can be added to this set, how one can be removed, and how you can check whether a name occurs in it.

var cat_set = {};
// Add a cat
cat_set["Tigger"] = true;
// remove a cat
delete cat_set["Tigger"];

// Check if cat exists
cat_set["Frog"] != undefined

// Wrap it all up nicely: in an object with functions:
var cat_set = {};
cat_set.add_cat = function(cat) {
    this[cat] = true;
}
cat_set.remove_cat = function(cat) {
    delete this[cat];
}
cat_set.cat_exists = function(cat) {
    return this[cat] != undefined;
}

cat_set.add_cat("Frisky");
// prints "true"
show(cat_set.cat_exists("Frisky"))
cat_set.remove_cat("Frisky");
// "false"
show(cat_set.cat_exists("Frisky"))

4.2

Write a function range that takes one argument, a positive number, and returns an array containing all numbers from 0 up to and including the given number.

An empty array can be created by simply typing []. Also remember that adding properties to an object, and thus also to an array, can be done by assigning them a value with the = operator. The length property is automatically updated when elements are added.

// Assumes n >= 0
function range(n) {
    var array = [];
    for (var i = 0; i <= n; i++) {
        array[i] = i;
    }
    return array;
}

4.3

split and join are not precisely each other’s inverse. string.split(x).join(x) always produces the original value, but array.join(x).split(x) does not. Can you give an example of an array where .join(" ").split(" ") produces a different value?

[" ", " ", " "].join(" ").split(" ");
// equals ["", "", "", "", "", ""]

4.4

Write a function called startsWith that takes two arguments, both strings. It returns true when the first argument starts with the characters in the second argument, and false otherwise.

function startsWith(string, pattern) {
    return string.slice(0,pattern.length) === pattern;
}

4.5

Can you write a function catNames that takes a paragraph as an argument and returns an array of names?

Strings have an indexOf method that can be used to find the (first) position of a character or sub-string within that string. Also, when slice is given only one argument, it will return the part of the string from the given position all the way to the end.

It can be helpful to use the console to ‘explore’ functions. For example, type "foo: bar".indexOf(":") and see what you get.

// The sentence is in the form "Blah blah: cat1, cat2, …, catN."
function catNames(paragraph) {
    var colonIndex = paragraph.indexOf(":");
    // Skip the colon and the following space
    var catListString = paragraph.slice(colonIndex + 2);
    return catString.split(", ");
}

/*#p2pu-Jan2011-javascript101*/

Advertisements

Car Talk Puzzler #4: Flipping Ages

October 26, 2010 2 comments

RAY: This was sent in many weeks ago by Wendy Gladstone, and as usual I tweaked it a little bit.

She writes: “Recently I had a visit with my mom and we realized that the two digits that make up my age when reversed resulted in her age. For example, if she’s 73, I’m 37. We wondered how often this has happened over the years but we got sidetracked with other topics and we never came up with an answer.

“When I got home I figured out that the digits of our ages have been reversible six times so far. I also figured out that if we’re lucky it would happen again in a few years, and if we’re really lucky it would happen one more time after that. In other words, it would have happened 8 times over all. So the question is, how old am I now?”

Question
Answer

Here’s the fourth in my Car Talk Puzzler series; today I’m going to be using Python because it’s my current favorite language, and because it’s well suited to filtering, mapping, etc.  I won’t put too much commentary here.

#  Find all the ages such that the second age is the reverse of the first  age.  Don't worry that there are a lot of impossibilities; we'll fix it  through filtering
# Note that [::-1] is the slice operator that says iterate backwards through the string; this effectively reverses the list.
 matching_ages = map(lambda x:(x, int(str(x)[::-1])), range(0,100))
 matching_ages
 # OUT: [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7),  (8, 8), (9, 9), (10, 1), (11, 11), (12, 21), (13, 31), (14, 41), (15,  51), (16, 61), (17, 71), (18, 81), (19, 91), (20, 2), (21, 12), (22,  22), (23, 32), (24, 42), (25, 52), (26, 62), (27, 72), (28, 82), (29,  92), (30, 3), (31, 13), (32, 23), (33, 33), (34, 43), (35, 53), (36,  63), (37, 73), (38, 83), (39, 93), (40, 4), (41, 14), (42, 24), (43,  34), (44, 44), (45, 54), (46, 64), (47, 74), (48, 84), (49, 94), (50,  5), (51, 15), (52, 25), (53, 35), (54, 45), (55, 55), (56, 65), (57,  75), (58, 85), (59, 95), (60, 6), (61, 16), (62, 26), (63, 36), (64,  46), (65, 56), (66, 66), (67, 76), (68, 86), (69, 96), (70, 7), (71,  17), (72, 27), (73, 37), (74, 47), (75, 57), (76, 67), (77, 77), (78,  87), (79, 97), (80, 8), (81, 18), (82, 28), (83, 38), (84, 48), (85,  58), (86, 68), (87, 78), (88, 88), (89, 98), (90, 9), (91, 19), (92,  29), (93, 39), (94, 49), (95, 59), (96, 69), (97, 79), (98, 89), (99,  99)]

# Here we filter by only allowing matches in which the  mother's age is greater than that of the child.  Note the use of a  lambda expression, basically an anonymous function.
filtered1 = filter(lambda (mother,child):mother > child, matching_ages)
 filtered1
 # OUT: [(10, 1), (20, 2), (21, 12), (30, 3), (31, 13), (32, 23), (40,  4), (41, 14), (42, 24), (43, 34), (50, 5), (51, 15), (52, 25), (53, 35),  (54, 45), (60, 6), (61, 16), (62, 26), (63, 36), (64, 46), (65, 56),  (70, 7), (71, 17), (72, 27), (73, 37), (74, 47), (75, 57), (76, 67),  (80, 8), (81, 18), (82, 28), (83, 38), (84, 48), (85, 58), (86, 68),  (87, 78), (90, 9), (91, 19), (92, 29), (93, 39), (94, 49), (95, 59),  (96, 69), (97, 79), (98, 89)]

# Assume that the mother was at least 15 when she had the kid, and no more than 60
filtered2 = filter(lambda(mother, child):mother-child >= 15 and mother-child < 60, filtered1)
 filtered2
 # OUT: [(20, 2), (30, 3), (31, 13), (40, 4), (41, 14), (42, 24), (50,  5), (51, 15), (52, 25), (53, 35), (60, 6), (61, 16), (62, 26), (63, 36),  (64, 46), (71, 17), (72, 27), (73, 37), (74, 47), (75, 57), (82, 28),  (83, 38), (84, 48), (85, 58), (86, 68), (93, 39), (94, 49), (95, 59),  (96, 69), (97, 79)]
 len(filtered2)
 # OUT: 30

# Create a new list comprised of the differences in age between mother and child
 age_diff = map(lambda(mother,child):mother-child, filtered2)
 age_diff
 # OUT: [18, 27, 18, 36, 27, 18, 45, 36, 27, 18, 54, 45, 36, 27, 18, 54, 45, 36, 27, 18, 54, 45, 36, 27, 18]
 sorted(age_diff)
 # OUT: [18, 18, 18, 18, 18, 18, 18, 27, 27, 27, 27, 27, 27, 36, 36, 36, 36, 36, 45, 45, 45, 45, 54, 54, 54]

# The puzzler states that it's will happen a total of 8 times; that matches the age difference of 18 years

 filter(lambda(mother,child):mother-child == 18, filtered3)
# OUT: [(20, 2), (31, 13), (42, 24), (53, 35), (64, 46), (75, 57), (86, 68), (97, 79)]

Thus the mother is currently 75 years old and the daughter is 57.  Tada