Skip to content

Instantly share code, notes, and snippets.

void MyClass::FetchMagicNumber(std::function<void(int)> callback) {
std::weak_ptr<MyClass> weak_this = shared_from_this();
auto fetch_results = [weak_this] {
auto shared_this = weak_this.lock();
if (!shared_this) return -1;
return shared_this->requester_->DoLongWebRequest();
};
system::Runtime::net_io.Post<int>(fetch_results, callback, system::Runtime::ui());
@toddbranch
toddbranch / binary.js
Last active April 12, 2020 06:59
Binary, Bitmasking, and Mouse Buttons!
// Computers represent numbers in binary (base 2), while humans represent them in decimal (base 10).
// In javascript, we can represent a binary number by putting "0b" in front of it
0b011 // evaluates to decimal 3!
// To convert a number from base 2 to base 10, we assign a value to each position
// _ _ _
// 4 2 1
// If the bit at that position is 1, that value is added to the sum. If it's 0, it isn't.
@toddbranch
toddbranch / canvas.html
Created December 8, 2016 23:13
Base for drawing on a <canvas>
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// use the context to draw!
</script>
// Event reference: https://developer.mozilla.org/en-US/docs/Web/Events
// Extended drawing example where we change colors on mouse click.
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// array of colors to rotate through on click
var colors = [
// Drawing example we used in class.
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.fillStyle = 'pink';
canvas.addEventListener('mousemove', function(event) {
@toddbranch
toddbranch / flatten.js
Created December 9, 2015 20:35
Flatten Array
// this implementation preserves the original arr
function flatten(arr) {
var result = [];
var queue = [];
var pointer = 0;
while(pointer < arr.length) {
queue.push(arr[pointer]);
pointer += 1;
@toddbranch
toddbranch / loop.js
Last active December 8, 2015 17:11
randomFromStream
function randomFromStream() {
var length = 0;
var result = null;
var potentialResult;
while(potentialResult = gfs()) {
length += 1;
if (Math.floor(Math.random() * length) === 0) {
result = potentialResult;
}
@toddbranch
toddbranch / randomFromStream.js
Created December 8, 2015 14:57
randomFromStream
function randomFromStream(currentResult, length) {
var potentialResult = gfs();
// no more numbers in stream
if (potentialResult === null) {
return currentResult;
}
length += 1;
@toddbranch
toddbranch / bootstrap.js
Created September 28, 2015 16:49
Angular Caching
var userEmail = $('.user-email').text();
var commonContactsWidget = document.createElement('common-contacts-widget');
commonContactsWidget.setAttribute('user-email', userEmail);
$('.some-area-of-the-page').append(commonContactsWidget);
angular.bootstrap(commonContactsWidget, ['ng', 'commonContactsModule']);