Skip to content

Instantly share code, notes, and snippets.

// javascript
var app = angular.module("someApp", []);
app.controller("someController", function($scope) {
$scope.name = "Liam Neeson";
});
// html: two-way data binding ("name")
/*
Credits: https://www.clayallsopp.com/posts/from-backbone-to-react/
*/
ProfileView = Backbone.View.extend({
render: function() {
var template = _.template($("#profile-view-template").html());
$(this.el).html(template);
// Add a subview
/*
Credits: https://gabrieleromanato.name/spaghetti-code-in-jquery/
*/
$('#foo').addClass('bar').parent().find('li').addClass('item').click(function() {
//... some click handler. good luck maintaining this piece of code!
});
@ammarm08
ammarm08 / jquery-vs-vanilla.js
Created November 1, 2018 13:47
jQuery vs Vanilla Javascript
/*
Credits: https://www.codementor.io/brainyfarm/jquery-vs-vanilla-javascript-deciding-on-what-to-use-6b79xdmrv
*/
// jquery!
$(el).fadeIn();
// javascript!
function fadeIn(el) {
(defn transform [xs]
(if (empty? xs)
nil
(concat (map #'+ xs (rest xs))
(list 1))))
(defn pascal [n]
(if (zero? n)
nil
(cons 1 (transform (pascal (- n 1))))))
@ammarm08
ammarm08 / levenshtein_search.js
Last active February 23, 2017 23:28
reading about elastic search, going on tangents
'use strict';
const word_bank = ['alex', 'ammar', 'matt', 'jimmy', 'foo', 'bar', 'dab'];
// Calculate the min number of edits to transform string i into string j.
// Based on the Levenshtein Distance: https://en.wikipedia.org/wiki/Levenshtein_distance
const fuzzy = (i, j) => {
// if i or j has no length, return length of the other
if (!i.length) return j.length;
@ammarm08
ammarm08 / v8-performance.js
Last active February 8, 2017 02:24
Optimizing a very basic protocol parser.
'use strict';
/**
* Fake Protocol Parser
* V8 Optimization: http://mrale.ph/s3/nodecamp.eu/#15
*
* PROTOCOL:
* _ _ _ _ _ _ \u0000 _ _ _ _ _ _ _ ....
* DATA LENGTH PAYLOAD
*
@ammarm08
ammarm08 / curry.js
Last active January 26, 2017 20:11
Math and Javascript, Function Currying
// Inspiration: http://homepages.inf.ed.ac.uk/wadler/papers/frege/frege.pdf
// MATH
g(x, y) = x * x + y * y // generic sum-of-squares function
g(x, y) = (lambda(x), lambda(y): x * x + y * y (u))(v) for all values of x and y // lambda calculus representation
// Reduction sequence, given x = 3, and y = 4:
(lambda(x), lambda(y): x * x + y * y (3))(4) // apply 3 to x, then y to 4
(lambda(y): 3 * 3 + y * y)(4) // this function can be repeatedly used to compute 3 * 3 + y * y for any y
@ammarm08
ammarm08 / set-array.js
Last active January 12, 2017 17:18
quick and dirty set access vs array access time benchmarking
'use strict'
/*
Quick and dirty Set access vs. Array access time benchmarking
*/
// Create arrays of arbitrary length betwen 1 and n
const arbitrary_data = (n) => Array(Math.ceil(Math.random() * n)).fill().map(x => Math.random())
grep -o . input.txt | sort | uniq -c | sort -r | sed 's/[0-9]*//g' | tr -d '\n ' | cut -d _ -f 1
# 1) separate each char by newline (ex. a \n c \n f ...)
# 2) sort by char (ex. a \n a \n a \n c \n f \n f)
# 3) count frequency of each char (ex. 3 a \n 1 c \n 2 f)
# 4) sort by counts, descending (ex. 3 a \n 2 f \n 1 c)
# 5) remove all digits (ex. a \n f \n c)
# 6) remove all newline chars and white spaces (ex. afc)
# 7) keep all chars until the underscore