Skip to content

Instantly share code, notes, and snippets.

View JamesMcMahon's full-sized avatar

James F McMahon JamesMcMahon

  • VMWare
  • Chicago, IL
View GitHub Profile
@JamesMcMahon
JamesMcMahon / sieve_of_eratosthenes.rb
Last active September 24, 2016 20:44
Sieve of Eratosthenes in Ruby
#!/usr/bin/env ruby
def gen_primes(n)
sieve = Array.new(n) { |i| i > 1 }
(0...Math.sqrt(n)).each do |i|
next unless sieve[i]
(i * i...n).step(i) { |j| sieve[j] = false }
end
@JamesMcMahon
JamesMcMahon / headtail.rb
Last active September 2, 2016 20:02
Head and Tail in Ruby splats
# split head and tail
(head, *tail) = [1, 2, 3]
# head is 1
# tail is [2, 3]
@JamesMcMahon
JamesMcMahon / vidtogif.sh
Last active June 17, 2016 17:43 — forked from imkevinxu/vidtogif.sh
Convert an animated video to gif from http://chrismessina.me/b/13913393/mov-to-gif
#!/usr/bin/env bash
#
# Convert an animated video to gif
# Works best for videos with low color palettes like Dribbble shots
#
# @param $1 - video file name like `animation.mov`
# @param @optional $2 - resize parameter as widthxheight like `400x300`
#
# Example: vidtogif animation.mov 400x300
# Requirements: ffmpeg and gifsicle. Can be downloaded via homebrew
@JamesMcMahon
JamesMcMahon / TextPic.cs
Last active May 5, 2016 15:32
Unity inline image in text script. Could never get it to work properly on Android (didn't even test iOS). Quad placeholder would leave behind vector noise. Heavily modified from original at http://forum.unity3d.com/threads/sprite-icons-with-text-e-g-emoticons.265927/#post-2466522. Hopefully DMGregor (http://gamedev.stackexchange.com/q/88502/6968)…
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
// Usage: icon name in text is replaced by image
public class TextPic : Text
{
[System.Serializable]
@JamesMcMahon
JamesMcMahon / lerp.rb
Created May 4, 2016 19:43
Lerp code in Ruby, just for reference
def lerp(start, stop, step)
(stop * step) + (start * (1.0 - step))
end
@JamesMcMahon
JamesMcMahon / WrapperComponent.cs
Last active April 27, 2016 14:52
WrapperComponent for Entitas, https://github.com/sschmid/Entitas-CSharp/pull/88. Storing here just in case the branch gets deleted.
namespace Entitas {
/// Inherit from this class if you want a component that is just a wrapper for some type.
/// All attributes that work for normal components also work for components that inherit from this class.
/// The body of the inheriting class should remain empty.
/// Example use:
/// <example>
/// // definition
/// public class NameComponent : WrapperComponent&lt;string&gt; { }
#!/usr/bin/env bash
# find /Applications -path '*Autoupdate.app/Contents/Info.plist' -exec echo {} \; -exec grep -A1 CFBundleShortVersionString '{}' \; | grep -v CFBundleShortVersionString
find /Applications/ -path '*Sparkle.framework*/Info.plist' -exec echo {} \; -exec grep -A1 CFBundleShortVersionString '{}' \; | grep -v CFBundleShortVersionString
@JamesMcMahon
JamesMcMahon / max.hs
Created November 8, 2015 00:36
Haskell lazy performance test
#!/usr/bin/env runhaskell
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs) = max x (maximum' xs)
main :: IO ()
main = do
-- print (maximum' [3, 5, 7, 3, 6])
@JamesMcMahon
JamesMcMahon / fib.clj
Last active November 8, 2015 01:11
Fibonacci generator in Clojure. Playing with memoization.
; Need to forward declare this symbol to use it in fib
(declare memo-fib)
(defn fib
"Returns the fibonacci number for the given position."
[n]
(if (or 0 1)
n
(+' (memo-fib (-' n 1)) (memo-fib (-' n 2)))))
@JamesMcMahon
JamesMcMahon / pivotal_story_summary.rb
Last active August 29, 2015 14:14
Pivotal story summarizer (to save me some time on Sundays)
#!/usr/bin/env ruby
# encoding: utf-8
##
# Markdown summarizer for Pivotal stories
#
# Usage './pivotal_story_summary.rb <project-id> <username>'
#
#
# The MIT License (MIT)