Skip to content

Instantly share code, notes, and snippets.

@egonSchiele
egonSchiele / generate_valid_isbn.rb
Created August 15, 2013 20:29
Generate a valid ISBN number in ruby
def generate_valid_isbn
prefix = 978.to_s # must be 978 or 979
registration_group_element = rand(10).to_s
registrant_element = (rand(90000) + 10000).to_s
publication_element = (rand(900) + 100).to_s
_isbn = prefix + registration_group_element + registrant_element + publication_element
check_digit = 0
i = 0
_isbn.each_char do |letter|
i+= 1
@egonSchiele
egonSchiele / crazy.rb
Created July 29, 2013 05:10
can't kill this
trap("SIGINT") do
puts "goodbye!"
# exit
end
while true
end
import Control.Applicative
import Github.Gists
import Github.Users.Followers
import System.Directory
import Control.Monad
import qualified Data.Set as S
import Text.Printf
import System.Process
import System.IO.Unsafe
@egonSchiele
egonSchiele / Timer.hs
Created July 16, 2013 02:00
Drawing timer in Haskell
import Control.Concurrent
import System.Process
import System.Environment
say str = system $ "say '" ++ str ++ "'"
timer :: Int -> IO ()
timer 1 = do
say "one minute left! oh crap!"
threadDelay $ 30 * 1000 * 1000
@egonSchiele
egonSchiele / searchHackage.rb
Created June 19, 2013 23:22
Search hackage from vim
#!/usr/bin/env ruby
_import = $stdin.gets
import = _import.chomp.gsub("import", "").gsub("qualified", "").gsub(/as .*/, "")
cmd = "hoogle -i '#{import}'"
puts cmd
packages = `#{cmd}`
lines = packages.split("\n")
package = lines.find do |line|
@egonSchiele
egonSchiele / dining02.rb
Created May 13, 2013 01:53
Another implementation of the dining philosophers in Ruby using Celluloid. This time the forks are mutexes and we don't block to acquire them.
require 'rubygems'
require 'celluloid'
class Philosopher
include Celluloid
def initialize(name, left_fork, right_fork)
@name = name
@left_fork = left_fork
@right_fork = right_fork
self.think
@egonSchiele
egonSchiele / dining01.rb
Created May 13, 2013 01:50
A celluloid implementation of dining philosophers where the forks are actors too. Could cause deadlock since we wait on forks.
require 'rubygems'
require 'celluloid'
class Philosopher
include Celluloid
def initialize(name, left_fork, right_fork)
@name = name
@left_fork = left_fork
@right_fork = right_fork
self.think
@egonSchiele
egonSchiele / wrap_bench.rb
Created March 4, 2013 23:17
Time difference between wrapped methods and regular methods
require 'benchmark'
module Wrapper
def self.extended(klass)
klass.class_eval do
@@methods = {}
def self.methods
@@methods
end
def self.set_method k, v
@egonSchiele
egonSchiele / Main.hs
Last active November 12, 2015 23:25
kcombinations
{-# LANGUAGE MultiWayIf #-}
import Data.List
import Control.Monad
import Control.Monad.Trans.Writer
kcombinations n arr = snd . runWriter $ combos [] 1 n arr
combos :: (Eq a) => [a] -> Int -> Int -> [a] -> Writer [[a]] ()
combos acc step n array = forM_ (zip array [1..]) $ \(val, i) -> if
| val `elem` acc -> return ()
@egonSchiele
egonSchiele / notes_on_cameras.markdown
Created February 12, 2014 17:03
Notes on cameras

Notes on cameras

Lenses

Focal length

Prime (fixed) lens

If it says 17mm, it's fixed at that...you can't zoom in or out. Advantages: cheaper.