Skip to content

Instantly share code, notes, and snippets.

@jb55
jb55 / gist:2253102
Created March 30, 2012 17:20
Lambda and arrow digraphs in vim
" lambda λ
imap <C-j> <C-k>l*
" right arrow →
imap <C-l> <C-k>->
" left arrow ←
imap <C-h> <C-k><-
" compose ∘
@jb55
jb55 / hello.rs
Created June 28, 2012 01:19
first rust program, written in style at 36k feet
fn main(args: [str]) {
let args = vec::tail(args);
for vec::each(args) {|a|
io::println(respond(a));
}
}
fn respond(s: str) -> str {
@jb55
jb55 / RPS.hs
Created July 6, 2012 18:45
teaching a friend Haskell with a nice game of rock paper scissors
import Control.Monad
import Control.Monad.Random
data RPS = Rock | Paper | Scissors
deriving (Show, Eq, Read)
data Result = Win | Lose | Tie
deriving (Show, Eq)
@jb55
jb55 / bquery_example.coffee
Created July 12, 2012 20:57
my first declarative backbone view with mixins
Mixins.bq = {}
Mixins.bq.mouseOvers = (overElem, toggleElem) ->
return (v) ->
elem = (that) ->
if _.isString toggleElem then that.$(toggleElem) else toggleElem
v.on "mouseover #{ overElem }", -> elem(@).show()
v.on "mouseout #{ overElem }", -> elem(@).hide()
Mixins.bq.initTemplate = (name) ->
return (v) ->
@jb55
jb55 / collection-view-example.coffee
Created July 13, 2012 19:28
Factor out a common backbone.js task with bQuery
#=----------------------------------------------------------------------------=#
# Mixins.bq.collection
# factor out some view logic. This mixin does the common task of adding
# elements to a view when things are added to the collection
#=----------------------------------------------------------------------------=#
Mixins.bq.collection = (o={}) ->
throw "missing collection model view constructor" unless o.createView
unless o.tag
throw "missing o.tag, this is where the elements will be added to"
return (v) ->
@jb55
jb55 / Thanks.hs
Created July 24, 2012 02:25
GHC suggests...
mapQ :: (MonadError M.Failure (PersistEntityBackend entity m),
PersistStore (PersistEntityBackend entity) m,
PersistEntity entity)
=> (Key (PersistEntityBackend entity) entity -> PersistEntityBackend entity m t)
-> [Key (PersistEntityBackend entity) entity]
-> PersistEntityBackend entity m [(Entity entity, t)]
mapQ f ids = forM ids $ \id' -> do
keyEnt <- get id'
xs <- f id'
case keyEnt of
@jb55
jb55 / Exile.hs
Created August 1, 2012 02:36
Path of Exile ladder most popular classes
import Data.CSV.Parser
import Control.Applicative
import GHC.Exts
import Control.Arrow
import Data.List (sortBy)
import Data.Function
type Parser a = FromCSV String a
data Class = Witch | DexInt | Templar | Ranger | Marauder | Duelist
@jb55
jb55 / concise.hs
Created August 13, 2012 07:23
Refactor weekend
web :: IO ()
web = scotty port $ do
post "/tracks" $ (parseBody :: ResponseM [Track]) >>= handleMany
post "/track" $ (parseBody :: ResponseM Track) >>= handleOne
post "/label" $ (parseBody :: ResponseM Label) >>= handleOne
-- post json >=> convert to persistent model >=> validate model >=> fix issues when possible >=> insert into database
-- responds with detailed error information from any part of the process
-- all refactored down to 1 line for each model with the help of typeclasses. It's beautiful ಥ_ಥ
@jb55
jb55 / bquery_rl_example.coffee
Created September 6, 2012 18:43
Large Backbone View, mostly composed of bquery mixins
TrackView =
bQuery.view()
.set("tagName", "div")
.set("className", "track-row")
.init((opts={}) ->
@artists = new ArtistCollection { unassigned: no }
@on "editing:start", => @isEditing = yes
@on "editing:end", => @isEditing = no
)
public static Maybe<object> GetProperty<T>(this T src, string prop) {
return from pi in typeof(T).GetProperty(prop).ToMaybe()
select pi.GetValue(src, null);
}
public static Maybe<IEnumerable<TResult>> GenericSelect<T, TResult>(this IEnumerable<T> ts, string property, Func<object, Maybe<TResult>> fn) {
var xs =
from t in ts
let prop = from x in t.GetProperty(property)
from y in fn(x)