Skip to content

Instantly share code, notes, and snippets.

View richardTowers's full-sized avatar

Richard Towers richardTowers

View GitHub Profile
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-calculator/paper-calculator.html">
<polymer-element name="my-element">
<template>
<style>
:host {
position: absolute;
width: 100%;
@richardTowers
richardTowers / bindToSeparateDateFieldFactory.js
Created January 2, 2015 16:12
function which takes two expressions and binds them together (useful for date processing)
function bindToSeparateDateFieldFactory ($rootScope, $parse) {
return function (context, srcExpression, targetExpression) {
var getTarget = $parse(targetExpression),
setTarget = getTarget.assign,
getSrc = $parse(srcExpression),
setSrc = getSrc.assign;
$rootScope.$watch(
function () { return getTarget(context); },
function (newValue) { newValue && setSrc(context, newValue + 'T00:00:00.000Z'); }
@richardTowers
richardTowers / designer.html
Last active August 29, 2015 14:13
designer
<link rel="import" href="../core-icon-button/core-icon-button.html">
<link rel="import" href="../core-toolbar/core-toolbar.html">
<link rel="import" href="../core-header-panel/core-header-panel.html">
<link rel="import" href="../core-animated-pages/core-animated-pages.html">
<link rel="import" href="../core-animated-pages/transitions/hero-transition.html">
<link rel="import" href="../core-animated-pages/transitions/cross-fade.html">
<link rel="import" href="../core-animated-pages/transitions/slide-down.html">
<link rel="import" href="../core-animated-pages/transitions/slide-up.html">
<link rel="import" href="../core-animated-pages/transitions/tile-cascade.html">
var injector = angular.injector(['ng']);
injector.instantiate(function ($http) {
window.$http = $http;
});
$http.get('https://whatever.com');
@richardTowers
richardTowers / index.js
Created September 30, 2015 08:08
requirebin sketch
var Immutable = require('immutable');
var list = Immutable.List.of(1,2,3,4,5,6,7);
var result =
list
.map(function (x) { return x*x; })
.filter(function (x) { return x < 10 })
.reduce(function (acc, x) { return acc + x })
void Main()
{
const int listSize = 1000;
const int sampleSize = 10000;
var sortedList = Enumerable.Range(0,listSize).ToList();
var unsortedList = new List<int>(sortedList);
var sortedCount = 0;
sortedList.Sort((l,r) => {sortedCount++; return l - r;});
@richardTowers
richardTowers / angular-simple-matches-validator.js
Last active October 20, 2015 15:31
A simple directive to add "matches" validation, e.g. to check if a password matches a confirmation
function crMatches () {
return {
require: 'ngModel',
scope: {
crMatches: '='
},
link: function(scope, elem, attrs, ctrl) {
scope.$watch('crMatches', function (newValue) {
if (newValue) { ctrl.$setValidity('matches', newValue === elem.val()); }
});
@richardTowers
richardTowers / var.js
Created January 24, 2013 20:06
Eplanation of `var` in loops in javascript.
// Normal variable:
var x = 7;
// Used like:
console.log(x); // => 7
// Variable declared in a loop
for(var i = 0; i < 3; i++) {
// Used like:
console.log(i); // => 0,1,2
@richardTowers
richardTowers / MozartNotes.rb
Created February 1, 2013 22:50
Reads a music XML file and counts the notes that are C5.
require "rexml/document"
include REXML
file = File.new( "String Quartet No. 3 in G Major, K. 156.xml" )
doc = Document.new file
notes = XPath.match( doc, "//note/pitch[step='C' and octave='5' and not(alter)]" )
puts notes.length
@richardTowers
richardTowers / gist:5207061
Last active December 15, 2015 05:09
Quick demo of pitfalls of `this` in JavaScript
<!doctype html>
<html>
<head>
<title>this</title>
</head>
<body>
<div><a id="broken" href="#">Incorrectly bound click</a></div>
<div><a id="working" href="#">Correctly bound click</a></div>
<div><a id="working2" href="#">Also correctly bound click</a></div>
<script>