Skip to content

Instantly share code, notes, and snippets.

View letsbreelhere's full-sized avatar

Bree Elle Gardner letsbreelhere

  • Academia, Inc.
  • Oakland, CA
View GitHub Profile
import Data.Monoid ((<>))
import Data.Text (Text)
import Text.Parsec
import Text.Parsec.Text
import qualified Data.Text as T
-- Replace all successful parses with a text transformation. E.g.:
-- λ> replaceParsed (read <$> many1 digit) (T.pack . show . (+1)) "foo42bar99baz12quux"
-- "foo43bar100baz13quux"
replaceParsed :: Parser a -> (a -> Text) -> Text -> Text
escape :: [(Char, String)] -> String -> String
escape = foldr f id
where f (c,s) acc = acc . replaceOne (c,s)
replaceOne (c,s) = concatMap (\x -> if x == c then s else [x])
@letsbreelhere
letsbreelhere / gist:1ae5d72c0f6a5d0a2ee1
Last active August 29, 2015 14:10
Working Type Inferencer/Unifier
module Inference where
-- This is extremely rough (a good bit is a gross amount of confusing mutual
-- recursion, and a few pieces just work without my understanding), but I
-- thought it'd be a good idea to get this down in case my laptop is hit with a
-- bus.
import Control.Applicative
import Data.Char
import Data.List
@letsbreelhere
letsbreelhere / gist:960a5e74cf391405869f
Last active August 29, 2015 14:04
Sketch of nested strong Rails params
# Like this:
#
# params.allow_nested(
# user: must(
# name: must,
# age: maybe,
# article: must(
# title: must,
# publish_at: maybe
# ),
@letsbreelhere
letsbreelhere / gist:3708825
Created September 12, 2012 18:22
d3 stacked line graph
var width = 400;
var height = 300;
var len = 20;
var rand_data = function() {
return d3.range(len).map(function() { return 1 + Math.random() });
}
var stack = d3.select("#chart").
append("svg:svg").
require 'rubygems'
require 'sinatra'
require 'serialport'
port_str = "/dev/tty.usbmodemfa131"
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
>> module M
>> def foo
>> self.class::FOO
>> end
>> end
=> nil
>> class A; FOO = 1; extend M; end
=> A
>> class B; FOO = 42; extend M; end
=> B
# config/elastic_search.rb
ElasticSearchSettingsForUser = YAML.load_file('config/elastic_search_user.yml').with_indifferent_access
# user.rb
class User < ActiveRecord::Base
settings ElasticSearchSettingsForUser do
mapping do
[:name, :email, :company].each do |attribute|
indexes attribute, type: 'string', analyzer: 'ngram_analyzer'
end
@letsbreelhere
letsbreelhere / ngram_config.rb
Created June 8, 2012 20:14
tire ngram analyzer
settings analysis: {
filter: {
ngram_filter: {
type: "nGram",
min_gram: 3,
max_gram: 8
}
},
analyzer: {
ngram_analyzer: {
@letsbreelhere
letsbreelhere / user.rb
Created June 8, 2012 20:08
tire gem user.rb
class User < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
attr_accessible :name, :email, :company
#...
end