Skip to content

Instantly share code, notes, and snippets.

@skatenerd
skatenerd / histogram.hs
Last active August 29, 2015 14:06
histogram. implement writer in terms of state monad?
first :: State [String] Int
first = state $ \initialstate -> (0, initialstate)
appender to_emit to_append = (state $ \lateststate -> (to_emit, (show to_append):lateststate))
histogram = do
firstanswer <- first
secondanswer <- appender "HI" firstanswer
appender 876 secondanswer
@skatenerd
skatenerd / poo.rb
Last active August 29, 2015 14:05
poo
class Album < ARModelSomething
class Presenter
def detailed(album):
{title: album.title,
other: album.stuff
}
end
def sumary(album):
{
fewer: things
@skatenerd
skatenerd / rtp.hs
Created August 6, 2014 22:32
return type polymorphism
class WithInt c where
combinate :: Integral i => i -> c -> [(i,c)]
class RTP c where
fromNothing :: Integral i => i -> [(i,c)]
data Wut = Wut deriving (Show)
data Foo = Foo deriving (Show)
@skatenerd
skatenerd / compileplz.hs
Created August 6, 2014 21:31
fails to compile. i'm not sure why - shouldn't "d" just represent *any type*, so 999 is legal to put in there?
class WithInt c where
combinate :: Integral i => i -> c -> [(i,c,d)]
data Wut = Wut deriving (Show)
instance WithInt Wut where
combinate i w = if (i == 0)
then []
else (i,w,999):combinate (i - 1) w
@skatenerd
skatenerd / list_predicate.rb
Created July 20, 2014 22:51
i would like for this to fali
describe "containment" do
it "checks a list for items matching a predicate" do
expect([1,2,3]).to include do |num|
num > 10
end
end
end
class Foo
def self.wat
22
end
end
Foo.new.class.wat #22
@skatenerd
skatenerd / indirection.hs
Last active August 29, 2015 14:02
indirection
import Control.Monad.Instances
addStuff = do
a <- (*2)
b <- (+10)
return (a+b)
addStuffTwo =
(* 2) >>= (\a ->
(+ 10) >>= (\b ->
@skatenerd
skatenerd / itemgetter.py
Last active August 29, 2015 14:01
item getter
input = [(1,2), (3,4)]
expected_output = [2,4]
def first_way(input):
def get_second_thing(tuple):
return tuple[1]
return map(get_second_thing, input)
def second_way(input):
return map(operator.itemgetter(1), input)
@skatenerd
skatenerd / searches.rb
Last active August 29, 2015 14:01
searching stuff
[2,4,6,8,10].find do |element|
element > 5
end
#-> 6
@skatenerd
skatenerd / nsa.rb
Last active August 29, 2015 14:00
NSA Factory Pattern
require 'json'
class SmartphoneSpy
def do_spying
recorded_activities = {bus_trip: 5.5, ate_meal: "chicken"}
message_to_send = recorded_activities.to_json
send_via_internet_to_HQ(message_to_send)
end
def send_via_internet_to_HQ(message_to_send)