Skip to content

Instantly share code, notes, and snippets.

View why-jay's full-sized avatar

YJ Yang why-jay

View GitHub Profile
// domElems is an array of DOM elements of length 5
for (var i = 0; i < domElems.length; ++i) {
((j) => {
domElems[j].addEventListener('click', () => {
console.log(j);
});
})(i);
}
domElems.forEach((elem, i) => {
elem.addEventListener('click', () = {
console.log(i);
});
});
for (let i = 0; i < domElems.length; ++i) {
domElems[i].addEventListener('click', () => {
console.log(i);
});
}
const Bluebird = require('bluebird');
const fs = Bluebird.promisifyAll(require('fs'));
const getFilenamesContainingMichaelAsync = Bluebird.coroutine(function* (path) {
const filenames = yield fs.readdirAsync(path);
return filenames.filter(name => name.indexOf('Michael') !== -1);
});
Bluebird.coroutine(function* () {
const filenamesContainingMichael = yield getFilenamesContainingMichaelAsync('~/some_dir');
// immutable version
const map = arr =>
arr.reduce((prev, cur) => prev.concat(cur), []);
// mutable version
const map = arr =>
arr.reduce((prev, cur) => {
prev.push(cur);
return prev;
}, []);
List<Action> actions = new List<Action>();
for (int counter = 0; counter < 10; counter++)
{
int copy = counter; // block-scoping in action!!
actions.Add(() => Console.WriteLine(copy));
}
List<Runnable> actions = new ArrayList<Runnable>();
for (int counter = 0; counter < 10; counter++)
{
final int copy = counter; // notice the `final` keyword!
actions.add(() -> { System.out.println(copy); });
}
List<Runnable> actions = new ArrayList<Runnable>();
for (int counter = 0; counter < 10; counter++) {
final int copy = counter; // notice the `final` keyword!
actions.add(new Runnable() {
public void run() { System.out.println(copy); }
});
}
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
int main()
{
vector<thread> workers;
for (int i = 0; i < 5; ++i)
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
int main() {
vector<thread> workers;