Skip to content

Instantly share code, notes, and snippets.

View russmatney's full-sized avatar

Russell Matney russmatney

View GitHub Profile
@russmatney
russmatney / pig-latin-filter.coffee
Created September 29, 2013 00:40
Basic CoffeeScript + AngularJS Pig-latin filter + unit tests. Only moves first letter at the moment. AngularJS filters are my favorite TDD.
'use strict';
angular.module('pig-latin', [])
.filter 'pig-latin', () ->
# input is handed into the filter
(input) ->
# equals itself or ''
input ||= ''
if input.length > 0
@russmatney
russmatney / Full JS test with mocked service
Last active August 29, 2015 13:56
Angular Controller Unit tests, featuring a mocked service
'use strict';
describe('Controller: ScenesCtrl', function(){
beforeEach(module('app'));
var scope, scenesCtrl;
var actId = '987';
var listOfScenes = [
{_id: '123', name:'scene one'},
{_id: '456', name:'scene two'}
];

Keybase proof

I hereby claim:

  • I am russmatney on github.
  • I am russmatney (https://keybase.io/russmatney) on keybase.
  • I have a public key whose fingerprint is 3378 B53A B12A 8811 99F2 0F0C 38B4 5CA1 46C8 CD72

To claim this, I am signing this object:

{-# LANGUAGE OverloadedStrings #-}
module LensCheatSheet where
import Control.Lens
import Data.Text
import qualified Data.Text as T
import qualified Data.HashMap.Lazy as HM
-- | A newtype wrapper to force UserNames to be labeled, and prevent us from
-- passing the wrong type of name around.
newtype UserName = UserName Text deriving (Show, Eq)
newtype PetName = PetName Text deriving (Show, Eq)
-- | A type alias to improve the readability of our types.
-- An inventory is a HashMap with a Text key and Item value.
type Inventory = HM.HashMap Text Item
-- | A User record.
-- | A lens from a User to Text.
--
-- Written quite explicitly with getter and setter helper functions to expose
-- Lens's nature.
userName :: Lens' User UserName
userName = lens getter setter
where
getter user = _userName user
setter user newName = user { _userName = newName }
viewExamples :: IO ()
viewExamples = do
let bob = User (UserName "Bob") 42 Nothing HM.empty
print "Bob's name is: "
print $ view userName bob
-- UserName "bob"
print $ bob ^. userName
-- UserName "bob"
composedViewExamples :: IO ()
composedViewExamples = do
let
bob = User (UserName "bob") 42 Nothing HM.empty
fitzgerald = Pet (PetName "Fitzgerald")
jeff = User (UserName "jeff") 42 (Just fitzgerald) HM.empty
print "Bob's pet's name is: "
print $ preview (pet . _Just . petName) bob
-- Nothing
previewExamples :: IO ()
previewExamples = do
let maybeIntA = Just 1
-- Have to tell the compiler this was a 'Maybe Int' for it to be printable
maybeIntB = Nothing :: Maybe Int
print "maybeIntA"
print $ maybeIntA ^? _Just
-- Just 1
print "maybeIntB"
setExamples :: IO ()
setExamples = do
let bob = User (UserName "bob") 0 Nothing HM.empty
print "Bob, with an updated score"
print $ set score 42 bob
-- User {_userName = UserName "bob", _userScore = 42, _userPet = Nothing, _userInventory = fromList []}
print $ (score .~ 42) bob
-- User {_userName = UserName "bob", _userScore = 42, _userPet = Nothing, _userInventory = fromList []}