Skip to content

Instantly share code, notes, and snippets.

View callerobertsson's full-sized avatar
Converting coffee to code

Calle Robertsson callerobertsson

Converting coffee to code
View GitHub Profile
@callerobertsson
callerobertsson / compareObjects.js
Created April 28, 2013 22:18
Javascript: Object comparision with ignored fields. Original source can be found at http://www.sencha.com/forum/showthread.php?59240-Compare-javascript-objects but without ignored fields. #snippet
// The javascript compare objects function
function compareObjects(o1, o2, ignoreFields){
ignoreFields = ignoreFields || [];
//console.log('Ignore: ' + JSON.stringify(ignoreFields));
for(var p in o1) {
if (ignoreFields.indexOf(p) < 0) {
if(o1[p] !== o2[p]){
return false;
}
}
@callerobertsson
callerobertsson / filterFile.js
Created May 13, 2013 15:53
Javascript (NodeJS): A simple script for extracting parts in a textfile between two regex matching lines. #snippet
// Filter File
// a simple script for extracting parts in a textfile between two lines.
// by Calle Robertsson, [email protected], 2013.
var fs = require('fs');
var filename = process.argv[2];
if (!filename) {
throw new Error('Missing command line argument for file name.');
@callerobertsson
callerobertsson / page-template.html
Created May 14, 2013 06:51
HTML: Page template #snippet
<!DOCTYPE html>
<!-- by [email protected] -->
<html>
<head>
<title>TITLE</title>
<style>
@import url("reset.css");
@import url("style.css");
</style>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
@callerobertsson
callerobertsson / cans.hs
Last active August 29, 2015 14:13
Haskell: Calculates number of cans in a pyramid or sum of all cans in pyramids
-- cans.hs
-- Calculate number of cans in one pyramid
cansInPyramid :: Int -> Int
cansInPyramid 1 = 1
cansInPyramid n = n + cansInPyramid (n - 1)
-- Smaller solution
cansInPyramid' n = sum [1..n]
@callerobertsson
callerobertsson / LucasCarmichaelNumbers.hs
Created January 21, 2015 13:34
Haskell: Lucas Carmichael number generator
-- Lucas Carmichael Numbers
--
-- inspired by the Youtube channel Numberphile
-- episode https://www.youtube.com/watch?v=yfr3BIk6KFc
-- "Something special about 399 (and 2015)"
--
-- by [email protected], jan 2015
import Data.Numbers.Primes
@callerobertsson
callerobertsson / hanoi.hs
Created February 22, 2015 00:42
Haskell: Tower of Hanoi
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n - 1) a c b ++ [(a, c)] ++ hanoi (n - 1) b a c
main = do
print $ hanoi 1 "a" "b" "c"
print $ hanoi 2 "a" "b" "c"
@callerobertsson
callerobertsson / empty-string-is-a-number.js
Last active August 29, 2015 14:16
Javascript: Empty string is a number
if (isNaN("")) {
console.log('Empty string is not a number');
}
else {
console.log('Empty string IS a number!!!');
}
// => Empty string IS a number!!!
@callerobertsson
callerobertsson / pad.js
Created March 13, 2015 13:03
Javascript: Pad string but don't truncate if too long
// Javascript Pad String
// Inspired by http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/
function pad(filling, value) {
if (('' + value).length > filling.length) {
return value;
}
return (filling + value).slice(-1 * filling.length);
}
@callerobertsson
callerobertsson / downcast.cs
Last active August 19, 2016 08:48
C#: True down cast from child to parent using JSON
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace DownCaster
{
static class Program
{
static void Main()
@callerobertsson
callerobertsson / wc.hs
Last active February 16, 2017 08:31
Haskell: Simple wc command (no options)
module Main where
import Text.Printf
main = do
input <- getContents
printf "%8d%8d%8d\n"
(length (lines input))
(length (words input))
(length input)