Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 ->
class Foo
def self.wat
22
end
end
Foo.new.class.wat #22
@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
@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 / 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 / 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 / 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 / clump.py
Last active August 29, 2015 14:06
python clump
from random import shuffle
to_clump = [0, 0.2, 5, 9, 9.1, 100, 100.2]
shuffle(to_clump)
def clump_same(comparables, distance_threshold):
def do_clump(so_far, current):
last_clump = so_far[-1]
lowest_in_last_clump = last_clump[0]
if (current - lowest_in_last_clump) < distance_threshold: