This file contains 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
// lambda lifting (http://en.wikipedia.org/wiki/Lambda_lifting) | |
// in GNU C; use inner functions which are not closures | |
// of course, but have access to free variables | |
// | |
// by Dmitry A. Soshnikov | |
#include <stdio.h> | |
// a pointer to a function type | |
typedef int (*funcPtr)(int); |
This file contains 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
/** | |
* Short notation for expression funargs - λ | |
* (not for production use, but just for fun, | |
* since closures are not supported -> only | |
* operations on passed argument "x") | |
* | |
* by Dmitry A. Soshnikov | |
*/ | |
function λ(code) { |
This file contains 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
# Python does not save not used bindings in | |
# the closured environment. However, even | |
# `eval` doesn't help to save them. | |
# | |
# In contrast, ECMAScript having environments | |
# frames, normaly find variable "x", see | |
# the same ES example here: https://gist.github.com/734485 | |
# | |
# by Dmitry A. Soshnikov | |
# |
This file contains 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
/** | |
* ECMAScript closures all environment frames | |
* (by the spec), so closured "x" variable is | |
* available in the dynamic `eval`. | |
* | |
* This example is made to show the difference | |
* of the Python's closures implementation, | |
* see it here: https://gist.github.com/734482 | |
* | |
* by Dmitry A. Soshnikov |
This file contains 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
# for closured vars | |
# we may use `nonlocal` keyword | |
def foo(): | |
a = 10 | |
b = 20 | |
def bar(): | |
nonlocal b # use parent "b" |
This file contains 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 a = ["1", "2", "3"]; | |
// try to get mapped array | |
// containing numbers | |
console.log(a.map(parseInt)); // but get [1, NaN, NaN] ! | |
// Explanation: | |
// per step 8.c.ii of the 15.4.4.9 |
This file contains 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 exports = this; | |
function require(module) { | |
load("lib/" + module.substr(2) + ".js"); | |
} | |
require("./definitions"); | |
require("./lexer"); | |
require("./parser"); | |
require("./decompiler"); |
This file contains 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
// | |
// by Dmitry Soshnikov <dmitry.soshnikov@gmail.com> | |
// MIT Style License | |
// see also: http://wiki.ecmascript.org/doku.php?id=strawman:iterators | |
// | |
// --------------------------------------- | |
// 1. Iteration via for-in loop | |
// --------------------------------------- |
This file contains 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
/** | |
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com> | |
*/ | |
// infinite objects generator | |
let g = new function () { | |
while (true) { | |
yield; | |
} | |
}; |
This file contains 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
/** | |
* Test for module imports | |
* Tested in Narcissus | |
* by Dmitry Soshnikov | |
*/ | |
module M { | |
export var x = 10; | |
export var y = 20; | |
} |