Skip to content

Instantly share code, notes, and snippets.

require "csv"
require 'action_controller/test_process'
file = `ls upload/*.csv | grep -v and-r`.chomp
info = CSV.read(ARGV.first || file)
info.shift
# TODO: set this list by eliminating the expected cols
properties = %w[Colour Material Size Weight Warranty] + ["Pole Diameter", "Fits Pole Size"]
@paulcc
paulcc / meta_search_hacks.rb
Created December 6, 2010 11:31
Import this to get searchlogic-style scopes from metasearch and rails 3
# by Paul Callaghan, Nov 2010.
# this uses method-missing to trigger generation and execution of the corresponding relation
# eg Product.name_equals("yay")
# TODO: M_S doesn't check for unexpected values, eg match failures in
# matches_attribute_method (so we trap exceptions)
module MetaSearchHacks
def self.build_rel(base_or_scope, method, *args)
@paulcc
paulcc / gist:907466
Created April 7, 2011 10:01
scottish ruby conf ex - counter
def counter(s = 0, i = 1)
c = s
lambda { o = c; c += i; o }
end
x = counter(10,2)
4.times { puts x.call }
y = counter(2,3)
6.times { puts y.call }
@paulcc
paulcc / gist:907556
Created April 7, 2011 10:57
another block exercise
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password
class AppServerConfig
attr_accessor :port, :admin_password
end
def app_server
yield(@app_server ||= AppServerConfig.new) if block_given?
@app_server
@paulcc
paulcc / sketch.rb
Created June 29, 2012 16:04 — forked from mattwynne/sketch.rb
pseudo-haskell
class Valid a where
valid :: a -> OkF Errors
class Valid a => Persist m a where
save :: a -> m (OkF a) -- or errors
data User = User {name :: String}
data Organisation = Organisation {name :: String, users :: [User]}
@paulcc
paulcc / group_on-full-example.hs
Created September 5, 2012 17:37
Example to accompany my types in haskell article
-- compiles with ghc(i) 7.4.1
import Data.Map (Map, empty, insertWith, toList)
import Data.List(groupBy, group, sortBy, sort)
{- To recap from the main article, we want to define "group_on" via "groupBy", so that its
type is something like (b -> b -> Bool) -> (a -> b) -> [a] -> [(b, [a])]
The method here is to work with a concrete example and gradually massage it
into the form we want - using the REPL
@paulcc
paulcc / gist:3733182
Created September 16, 2012 16:51
Code for the Word Chain kata, see the October edition of PragPub magazine
-- word list is at https://gist.github.com/3799331
import Data.Set(Set, fromDistinctAscList, member)
import System.IO.Unsafe(unsafePerformIO)
import Data.Tree
import Data.List(inits,tails)
--------------------
-- first step - generating valid next words
@paulcc
paulcc / gist:3799331
Created September 28, 2012 11:43
list of four-letter words to go with https://gist.github.com/3733182
aahs
abbe
abbr
abed
abet
able
ably
abut
acct
aced
@paulcc
paulcc / Files.hs
Created November 27, 2012 23:02
Async JS via Fay (Haskell) - see the December PragPub Magazine article
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
-- Example for December 2012 Prag Pub Magazine
-- expects Fay 0.10
--
-- Fay doesn't support type classes yet (hopefully soon) so this code contains
-- various workarounds and so is a bit more complex than it should be!
-- see below for commentary
makeUpTo :: Int -> [String] -> [String]
makeUpTo n = mlu []
where
mlu pre [] = [pre]
mlu [] (w:ws) = mlu w ws
mlu pre (w:ws) | length pre + 1 + length w <= n = mlu (pre ++ ' ' : w) ws
| otherwise = pre : mlu [] (w:ws)