Skip to content

Instantly share code, notes, and snippets.

@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 / diners.hs
Last active April 23, 2022 17:29
Dining philosophers solution in Haskell using STM. Taken from http://rosettacode.org/wiki/Dining_philosophers#Haskell with some minor modifications.
import Control.Monad
import Control.Concurrent
import Control.Concurrent.STM
import System.Random
import Text.Printf
-- Forks
type Fork = TMVar Int
newFork :: Int -> IO Fork
@egonSchiele
egonSchiele / dining.rb
Last active September 22, 2018 12:23
Dining philosophers in Ruby with Celluloid. Modified from https://gist.github.com/bugant/4984042
require 'rubygems'
require 'celluloid'
class Waiter
include Celluloid
FORK_FREE = 0
FORK_USED = 1
attr_reader :philosophers
attr_reader :forks
attr_reader :eating
@egonSchiele
egonSchiele / dining_with_waiter.rb
Created May 16, 2013 18:20
Dining philosophers using locks in Ruby. This implements a Waiter who is in charge of forks.
require 'thread'
class Waiter
def initialize
@mutex = Mutex.new
end
def can_eat? philosopher
left = philosopher.left_fork
right = philosopher.right_fork
@egonSchiele
egonSchiele / reader.hs
Created June 10, 2013 20:51
Reader monad example
import Control.Monad.Reader
hello :: Reader String String
hello = do
name <- ask
return ("hello, " ++ name ++ "!")
bye :: Reader String String
bye = do
name <- ask
@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 / Mario.hs
Created July 15, 2013 01:41
Lens example using Mario
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Point = Point {
_x :: Double,
_y :: Double
} deriving (Show)
data Mario = Mario { _location :: Point } deriving (Show)
@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
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 / crazy.rb
Created July 29, 2013 05:10
can't kill this
trap("SIGINT") do
puts "goodbye!"
# exit
end
while true
end