For fun, let's say you are programming in a language that doesn't allow variable assignments and you still want to make a recursive function. Although you can't assign variables, you can use functions (and enclosed function arguments). Can you make a function recursive without calling it by name?
Lets try implementing the factorial function. First with a function calling itself by name, then with a funtion that never calls itself by name
Here is the implementation of factorial that calls itself by name. It's a simple recursive function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# normal factorial | |
factorial = (x) -> | |
if x == 1 then return 1 | |
return x * factorial(x - 1) | |
console.log factorial(6) #720 | |
# new factorial that doesn't reference itself | |
factorialGenerator = (fac) -> | |
(x) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub mycro() | |
' | |
' mycro Macro | |
' | |
' Keyboard Shortcut: Ctrl+e | |
' | |
ActiveCell.FormulaR1C1 = "yo dawg" | |
Range("I9").Select | |
Dim ws As Worksheet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var withFakeDate = function (fn) { | |
var oldDate = Date; | |
var FakeDate = function () { | |
var self = new oldDate(); | |
self.fakeDateWasHere = true; | |
return self; | |
}; | |
var oldGetFullYear = Date.prototype.getFullYear; | |
Date.prototype.getFullYear = function () { | |
return 1996; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#first generate a key for your domain | |
openssl genrsa -out yourdomain.com.key 2048 | |
#then generate the request | |
openssl req -new -key yourdomain.com.key -out yourdomain.com.csr | |
#http://mikegrouchy.com/blog/setting-up-nginx-with-ssl-and-godaddy.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.setUpSocket(socket) -> | |
socket.on "data", (data) -> | |
server.allData.push(data) | |
#----------------- | |
server.setUpSocket(socket) -> | |
socket.on "data", _.bind(server.onSocketData, null, socket) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() { | |
var incMaker = () { | |
var x = 0; | |
var inc = () { | |
x += 1; | |
return x; | |
}; | |
return inc; | |
}; | |
var inc = incMaker(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point { | |
Point(this.x, this.y); | |
distanceTo(Point other) { | |
var dx = x - other.x; | |
var dy = y - other.y; | |
return Math.sqrt(dx * dx + dy * dy); | |
} | |
var x, y; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Presenter = function() { | |
this.initialize(); | |
} | |
Presenter.extend = Backbone.View.extend | |
_.extend(Presenter.prototype, Backbone.Events, { | |
initialize: function () {} | |
}) |