Skip to content

Instantly share code, notes, and snippets.

@ahirschberg
ahirschberg / 1-peasant_multiplication.rb
Last active May 3, 2016 21:36
Peasant multiplication algorithm implemented in ruby. You can check this out in action at http://repl.it/onr
class PeasantMultiplication
## multiply using the Peasant Multiplication Algorithm
def self.multiply(number1, number2)
# convert number1 to binary and reverse it
# so that the most significant bit is last
n1_binary_str = number1.to_s(2).reverse
binary_array = n1_binary_str.split '' # convert string to array
add_amount = number2
# traverses the binary_array and calculates the total
@ahirschberg
ahirschberg / pubspec.yaml
Last active May 3, 2016 21:13
For dart angular2 issue #8439: [Dart Angular2-beta.17] platform_directives only apply when compiled to JavaScript, not in Dart
name: angular2_getting_started
description: Quickstart
version: 0.0.1
environment:
sdk: '>=1.13.0 <2.0.0'
dependencies:
angular2: 2.0.0-beta.17
browser: ^0.10.0
dart_to_js_script_rewriter: ^1.0.1
transformers:
@ahirschberg
ahirschberg / unused_args_var.js
Created August 9, 2015 14:04
Shows how old args keys could sometimes get left over in the code and cause confusion later on as to what was expected during instantiation.
function SearchResultsPageGenerator(args) {
'use strict';
var httpRequestHandler = args.httpRequestHandler,
noLongerNeededObj = args.noLongerNeededObj, // never referenced, always undefined
sr_chapters_data_handle = args.sr_chapters_data_handle;
// do stuff ...
}
// try 2
// indentation looks better, but now the style is inconsistent:
books.forEach(function (book) {
$('<li/>').html( // statement.method(... here
$('<a>') // statement[\n].method(...) here
.attr('href', 'book.html')
.append(
$('<h2/>').text(book.title)
)
).click(function () {
@ahirschberg
ahirschberg / app_instantiation_behemoth.js
Last active August 29, 2015 14:26
All the objects used in LibriFox
function createApp () {
'use strict';
// Create objects
var settings = new SettingsManager({
asyncStorage: asyncStorage
}),
mediaManager = new MediaManager(),
fileManager = new FileManager({
storage_device: lf_getDeviceStorage(),
@ahirschberg
ahirschberg / js_indentation_ugly_arguments.js
Last active August 29, 2015 14:26
What is the best way to indent when you have functions and arguments together?
// try 1
if (lf_getDeviceStorage()) {
LazyLoader.load(['js/lib/async_storage.js', 'js/lib/mediadb.js'], () => {
createApp()
});
}
// try 2
if (lf_getDeviceStorage()) {
LazyLoader.load(
@ahirschberg
ahirschberg / jquery_html_generation_try1.js
Last active August 29, 2015 14:26
Showing how javascript indentation can be kind of gross looking, especially when dynamically writing html
// try 1
books.forEach(function (book) {
$('<li/>')
.html(
$('<a>')
.attr('href', 'book.html')
.append(
$('<h2/>').text(book.title)
) // this click now applies to the <li>, not
).click(function () { // the <a>, which may be confusing or unclear
def multiply(number1, number2)
# convert number1 to binary and reverse it
# so that the most significant bit is last
n1_binary_str = number1.to_s(2).reverse
binary_array = n1_binary_str.split '' # convert string to array
add_amount = nil
total = 0
binary_array.each_with_index do |char, index|
# double the add_amount, or set it to number2 if it is nil
class TestClass
def test_scope
times_called = 0
other_func do # passes block into #other_func
times_called += 1
end
puts "Called #{times_called} times"
end
@ahirschberg
ahirschberg / js_callback_scope_ex.js
Last active August 29, 2015 14:20
Useful example of base variables referenced in a callback remaining in scope
// adapted from displayAppFiles() in app.js
function checkIfAnyFiles() {
var items_found = 0; // variable defined in outer function
var each_item = function(result) {
items_found++; // still in scope for the callback
}
var when_done = function () {
// Output depends on number of items traversed by each_item