Skip to content

Instantly share code, notes, and snippets.

@hsavit1
hsavit1 / 1-Functor-and-Monad.md
Last active August 29, 2015 14:27 — forked from mbrandonw/1-Functor-and-Monad.md
Swift Functor and Monad

Copy and paste the swift code below into a playground to experiment.

This is a very close emulation of Functor and Monad typeclasses in swift. However, it is very fragile (i.e. easy to crash the compiler).

For example, instance methods of fmap will run fine, but attempting to use a globally defined fmap that acts on Functor types will cause a crash. Similarly for bind. Unfortunately this means we cannot define the nice infix operator versions of these functions.

@hsavit1
hsavit1 / HTTPGet.swift
Created October 14, 2015 03:47 — forked from jaisontj/HTTPGet.swift
HTTPGet from Google with mods for swift 2.0
import Foundation
func HTTPsendRequest(request: NSMutableURLRequest,callback: (String, String?) -> Void) {
let task = NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler :
{
data, response, error in
if error != nil {
callback("", (error!.localizedDescription) as String)
} else {
@hsavit1
hsavit1 / dining.rb
Created October 24, 2015 03:12 — forked from egonSchiele/dining.rb
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
@hsavit1
hsavit1 / dining_with_waiter.rb
Created October 24, 2015 03:13 — forked from egonSchiele/dining_with_waiter.rb
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
@hsavit1
hsavit1 / diners.hs
Created October 24, 2015 03:14 — forked from egonSchiele/diners.hs
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
@hsavit1
hsavit1 / reader.hs
Created October 24, 2015 05:45 — forked from egonSchiele/reader.hs
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
@hsavit1
hsavit1 / test.clj
Created October 25, 2015 03:18 — forked from adambard/test.clj
; Comments start with semicolons.
; Clojure is written in "forms", which are just
; lists of things inside parentheses, separated by whitespace.
;
; The clojure reader assumes that the first thing is a
; function or macro to call, and the rest are arguments.
;
; Here's a function that sets the current namespace:
(ns test)
@hsavit1
hsavit1 / array_structure.swift
Created October 29, 2015 04:33 — forked from austinzheng/array_structure.swift
A horrendous example of custom pattern matching in Swift
// Playground - noun: a place where people can play
import Cocoa
enum Wildcard : NilLiteralConvertible {
case Single
case FromBeginning
case ToEnd
case Range(Int)
case Literal(Int)
@hsavit1
hsavit1 / MyList.hs
Created October 29, 2015 18:35 — forked from akoskovacs/MyList.hs
A simple Haskell linked list implementation
module MyList where
data MyList a = Cons a (MyList a)
| MyNil deriving (Show, Eq)
{-
A simple linked list module
Some examples:
mylist = (Cons 10 (Cons 99 (Cons 11 (Cons 1 MyNil))))
myHead myList # => 10
myTail myList # => Cons 99 (Cons 11 (Cons 1 MyNil))
@hsavit1
hsavit1 / pattern-examples.swift
Created October 31, 2015 01:48 — forked from terhechte/pattern-examples.swift
Swift pattern examples for the swift, for, and guard keywords
import Foundation
// 1. Wildcard Pattern
func wildcard(a: String?) -> Bool {
guard case _? = a else { return false }
for case _? in [a] {
return true
}