Skip to content

Instantly share code, notes, and snippets.

@drewlesueur
drewlesueur / need_y_combinator.coffee
Created June 26, 2012 20:34
need_y_combinator.coffee
# 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) ->
@drewlesueur
drewlesueur / y_combinator.md
Created June 27, 2012 06:21
How to generate the Y-Combinator in coffeescript

How to Generate the Y-Combinator in CoffeeScript

Problem

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

@drewlesueur
drewlesueur / file.md
Created November 28, 2012 22:00
test md

this is a title

foo

// code taken from visionmedias node-querystring
var qs = (function () {
var qs = {}
/**
* Object#toString() ref for stringify().
*/
var toString = Object.prototype.toString;
/**
// alternate to imagecreatefrom...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$image = curl_exec($ch);
curl_close($ch);
@drewlesueur
drewlesueur / javascripts_this.js
Last active December 18, 2015 18:09
Javascript's this
var a = {name: "a", fun: function () { return this; }};
console.log(a.fun());
//=> {name "a", fun: ...}
var bFunc = function () { return this };
var b = {name: "b", fun: bFunc};
console.log(b.fun());
//=> {name: "b", fun: ...}
// not some global object
var ifs = function (args, i) {
var i = i || 0;
if (i == args.length - 1) { return args[i] }
else if (i > args.length - 1) { return undefined }
if (args[i]) { return args[i + 1] }
else { return ifs(args, i + 2) }
}
function scale(x, realMin, realMax, scaledMin, scaledMax) {
var realDiff = realMax - realMin
var scaledDiff = scaledMax - scaledMin
var xOffset = x - realMin
var ratio = xOffset / realDiff
var appliedValue = ratio * scaledDiff
var ret = scaledMin + appliedValue
return ret
}
<!doctype html>
<html>
<head>
</head>
<body>
<div style="height: 400px; border: 1px solid green; width: 300px; display: flex; flex-flow: column nowrap; ">
<div style="padding: 20px; background-color: orange; ">I am a header</div>
<div style="flex: 1 1 auto; display: flex; flex-flow: column nowrap; overflow: auto;">
https://github.com/drewlesueur/php-src/tree/7568605b55189cbc5fedd6dff3da074f9abf1d6d