Skip to content

Instantly share code, notes, and snippets.

@jagbolanos
jagbolanos / dfsalgorithms.hs
Created March 5, 2011 23:37
Some DFS based graph algorithms in Haskell
--Proposed solutions to problems 87,88 and 89 of "99 Haskell Problems"
--Not optimal but they work
--If you know haskell and want to solve some problems there are some missing at:
--http://www.haskell.org/haskellwiki/99_questions/80_to_89
import Data.List
type Node = Int
type Edge = (Node,Node)
type Graph = ([Node],[Edge])
@markd2
markd2 / BNRDiggyDict.h
Created March 20, 2012 13:45
Objective-C Literals, part 2 support files.
#import <Foundation/Foundation.h>
@interface BNRDiggyDict : NSObject
// React to object indexing. Would be nice to have a @protocol for this
- (id) objectForKeyedSubscript: (id) key;
- (void) setObject: (id) thing forKeyedSubscript: (id<NSCopying>) key;
// And for fun, it also can react to scalar indexing.
// Returns the N'th key of the top-level collection.
@domenic
domenic / promises.md
Last active March 31, 2024 14:07
You're Missing the Point of Promises

This article has been given a more permanent home on my blog. Also, since it was first written, the development of the Promises/A+ specification has made the original emphasis on Promises/A seem somewhat outdated.

You're Missing the Point of Promises

Promises are a software abstraction that makes working with asynchronous operations much more pleasant. In the most basic definition, your code will move from continuation-passing style:

getTweetsFor("domenic", function (err, results) {
 // the rest of your code goes here.
@benjchristensen
benjchristensen / ObservableMethod.groovy
Created January 30, 2013 18:39
Observable Method that optionally uses a thread to fetch data if not available in memory. Either way the response type is an Observable callback that can be composed using Rx operators.
/**
* Non-blocking method that immediately returns the value
* if available or uses a thread to fetch the value and
* callback via `onNext()` when done.
*/
def Observable<T> getData(int id) {
if(availableInMemory) {
// if data available return immediately with data
return Observable.create({ observer ->
observer.onNext(valueFromMemory);
@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
@mzero
mzero / ghc-clang-wrapper
Created October 31, 2013 06:53
This wrapper script *should* enable GHC 7.* to work on systems with Xcode 5. To use it, drop this script somewhere, make it executable, and run.... Then follow the instructions it prints out. What it will do is, instruction you to put a copy in /usr/bin, then re-run it sudo. It will then find all your GHC 7 settings files, and patch them to make…
#!/bin/sh
inPreprocessorMode () {
hasE=0
hasU=0
hasT=0
for arg in "$@"
do
if [ 'x-E' = "x$arg" ]; then hasE=1; fi
if [ 'x-undef' = "x$arg" ]; then hasU=1; fi