Skip to content

Instantly share code, notes, and snippets.

View twopoint718's full-sized avatar
yo

Christopher Wilson twopoint718

yo
View GitHub Profile
module Reader
def [](state)
@state = state
end
def with_state
yield(@state)
end
end
@twopoint718
twopoint718 / lenses_101.lhs
Last active August 29, 2015 14:00
Taking a stab at lenses (here's my pitch)
> {-# LANGUAGE TemplateHaskell #-}
> module Main where
> import Control.Lens
I think a big reason why I'm so stoked about this is that it's a solid library
that's offering a lens-based API (something that I think will show up more and
more as time goes on). I'm still digging into them, but lenses are sort of
like the "." (dot) from Ruby, but implemented as a library and infinitely more
flexible:
@twopoint718
twopoint718 / parameterized_accessors.rb
Created May 16, 2014 00:44
Here's a weird idea I've been playing around with. A lot of the coupling of code in ruby comes from having to dig deeply into nested objects. By pulling out the accessors, effectively making the '.' into a function, we get to externally control a method's access. In testing this means that we don't need any mocks. We just pass in do-nothing func…
def average_transaction_amount(user)
purchases = user.purchases
total = purchases.map(&:amount).inject(&:+)
total / purchases.count
end
def parameterized_average_transaction_amount(user, purchase_acc, amount_acc)
purchases = purchase_acc.call(user)
total = purchases.map(&amount_acc).inject(&:+)
total / purchases.count
require 'minitest/autorun'
# type decls. ---------------------------------------------------------
class Person
attr_reader :first, :last
def initialize(f, l)
@first = f
@last = l
@twopoint718
twopoint718 / MergeSort.hs
Created September 16, 2014 02:17
An example from http://perl.plover.com/yak/typing/notes.html translated to Haskell
-- split :: [t] -> ([t], [t])
split [] = ([], [])
split [h] = ([h], [])
split (x:y:t) = let (s1, s2) = split t
in (x:s1, y:s2)
-- merge :: Ord a => ([a], [a]) -> [a]
merge ([], x) = x
merge (x, []) = x
merge (h1:t1, h2:t2) =
@twopoint718
twopoint718 / TimeUnits.hs
Created September 19, 2014 21:03
The time unit trick
{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
newtype TimeUnit = TimeUnit Integer
deriving (Eq, Show, Num)
instance Num (TimeUnit -> TimeUnit) where
fromInteger n = \(TimeUnit scale) -> TimeUnit (n * scale)
seconds, minutes, hours, days :: TimeUnit
#!/bin/sh
###############################################################################
#
# COMPUTE YOUR TRUE GIT POWER. CHALLENGE YOUR FRIENDS. ...THEY'LL SOON BE YOUR
# FORMER FRIENDS WHEN YOU HAVE MORE GITPOWER. SHUTUP, THEM! YOU DON'T NEED
# THOSE LOSERS PLACE REAL-MONEY BETS. GET SUPER RICH. TELL YOUR BOSS YOU'RE
# BETTING ON GIT! THEY'LL BE ALL LIKE "OKAY, THAT'S COOL"
#
###############################################################################
@twopoint718
twopoint718 / bonus.hs
Created January 31, 2015 18:16
From my "Hands-on Haskell 101" talk
module Main where
import Data.List (sort, intersperse, intercalate)
scrambled :: [(Int, String)]
scrambled = [(26,"as"), (20,"because"), (2,"cannot"), (24,"details"),
(29,"does"), (1,"i"), (23,"idle"), (7,"in"), (21,"it"), (25,"just"),
(28,"memory"), (12,"night"), (17,"night"), (10,"of"), (27,"our"),
(18,"pleases"), (9,"solitude"), (6,"suburbs"), (22,"suppresses"),
(15,"that"), (5,"the"), (8,"the"), (11,"the"), (16,"the"),
@twopoint718
twopoint718 / gist:80a05f2c7d6f683bf3da
Created February 13, 2015 22:00
Playing around with creating a pseudo-builder pattern. The idea is that the arguments to the constructor can be given one-by-one (using the `build` class method). Once the object is created it is immediately frozen and returned. The "real" constructor is made private so that callers can't easily use `Point.new` directly.
class Point
private_class_method :new
attr_reader :x, :y, :z
def self.build
->(x, y, z){ new(x, y, z).freeze }.curry
end
def initialize(x, y, z)
@x, @y, @z = x, y, z

Keybase proof

I hereby claim:

  • I am twopoint718 on github.
  • I am cjw (https://keybase.io/cjw) on keybase.
  • I have a public key whose fingerprint is 1AE3 0A0B 22CC FC2D 3AA7 A85E C40B DE79 6D77 35C5

To claim this, I am signing this object: