Skip to content

Instantly share code, notes, and snippets.

View AverageMarcus's full-sized avatar
👋
¯\_(ツ)_/¯

Marcus Noble AverageMarcus

👋
¯\_(ツ)_/¯
View GitHub Profile
@AverageMarcus
AverageMarcus / AddIfNotExists.cs
Last active December 15, 2015 00:49
An extension to ICollection to provide a .AddIfNotExists() method to remove the need for constant checking.
/// <summary>
/// Adds an item to the list only if it doesn't already exist there.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="coll"></param>
/// <param name="item"></param>
public static void AddIfNotExists<T>(this ICollection<T> coll, T item) {
if (!coll.Contains(item))
{
coll.Add(item);
@AverageMarcus
AverageMarcus / LinqQuickSort.cs
Created March 16, 2013 14:27
Quickly sort a list using Linq
ratings.Sort((x, y) => x.Grade.CompareTo(y.Grade));
@AverageMarcus
AverageMarcus / TrimTrailingComma.cs
Created March 16, 2013 15:53
Remove any trailing commas from comma separated concatenated strings.
public static string TrimTrailingComma(string value)
{
if (value.Length >= 2)
{
if(value[value.Length-1].Equals(" ") && value[value.Length-2].Equals(","))
{
value = value.Substring(0, value.Length - 2);
}
else if (value[value.Length - 1].Equals(","))
{
@AverageMarcus
AverageMarcus / Polling.js
Created March 28, 2013 12:47
JavaScript polling every 5 seconds
(function pollForUpdate(){
setTimeout(function(){
$.post('/some/url/', function(data){
//process data
pollForUpdated();
});
}, 5000);
})();
@AverageMarcus
AverageMarcus / console.table.js
Created March 28, 2013 20:39
Implementing a console.table() like feature to help with debugging
function console_table(xs) {
function pad(n, s) {
var res = s;
for (var i = s.length; i < n; i++)
res += " ";
return res;
}
if (xs.length === 0)
// Pure CSS
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
// Less Mixin
.vertical-align {
position: relative;
@AverageMarcus
AverageMarcus / Object.extend.js
Created June 14, 2014 19:54
Extends an object with values from another object. Existing properties are ignored. Useful for ensuring options have default values.
Object.prototype.extend = function(obj){
if(typeof obj !== 'object') return this;
var current, prop,
i = 0,
length = arguments.length;
for(; i<length; i++){
current = arguments[i];
if(typeof current === 'object'){
for(prop in current){
@AverageMarcus
AverageMarcus / LiveEdit.css
Created June 14, 2014 20:15
Style textareas to look like paragraphs of text to provide inline editing of content.
textarea, textarea:focus {
width:100%;
height:100%;
margin: 0;
border:0;
border-color:transparent;
outline:none;
overflow-y:auto;
resize:none;
}
@AverageMarcus
AverageMarcus / ConwaysGameOfLife.html
Last active August 29, 2015 14:03
An example of Conway's Game of Life
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Marcus Noble - Conway's Game Of Life</title>
<style type="text/css">
* {
box-sizing:border;
}
body{
@AverageMarcus
AverageMarcus / SpaceInvader.ino
Last active August 29, 2015 14:16
Space Invader
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
void draw(void) {
u8g.drawPixel(53,11);
u8g.drawPixel(54,11);
u8g.drawPixel(54,12);
u8g.drawPixel(53,12);
u8g.drawPixel(55,11);
u8g.drawPixel(55,12);