Skip to content

Instantly share code, notes, and snippets.

View sw17ch's full-sized avatar

John VanEnk sw17ch

View GitHub Profile
@sw17ch
sw17ch / example_indented_input.txt
Created March 16, 2012 03:13
An example input to the list parser for my blog post about Text.Parsec.Indent from the indents package.
listName:
item1
item2
item3
@sw17ch
sw17ch / indented_parsec_example.lhs
Created March 16, 2012 04:31
A full example demonstrating the use of the indentation parser provided by the 'indents' package: http://hackage.haskell.org/package/indents
> module Main where
First, import all the needed modules.
> import Text.Parsec hiding (State)
> import Text.Parsec.Indent
> import Control.Monad.State
Next, define our new Parser type. This replaces the Identity monad
with the (State SourcePos) monad.
{-# LANGUAGE DeriveDataTypeable #-}
module Main where
-- Using strict imports so that we know where stuff comes from.
import Data.Generics (Data, Typeable)
import Text.JSON.Generic (JSValue, toJSON)
import Text.JSON.Pretty (render, pp_value)
data SuperHeroes = Batman { phrase :: String }
saveGrams :: (Serialize a, Serialize b) => DB -> [(a, b)] -> IO ()
saveGrams db pairs = mapM_ put' pairs
where
put' (gram, indicies) = put db [] (encode gram) (encode indicies)
{-# LANGUAGE DeriveDataTypeable #-}
module Main where
import Data.Generics (Data, Typeable)
import qualified Data.Binary as B
import Data.Binary.Generic -- binary-generic package
import Text.JSON.Generic (JSValue, toJSON)
import Text.JSON.Pretty (render, pp_value)
@sw17ch
sw17ch / internalized_queue.c
Created April 11, 2012 16:04
An example of an internalized queue structure that uses constant space and can be used to chain asynchronous events together in embedded systems.
/**
* A simple program to print a sequence of messages.
**/
#include <stdint.h>
#include <stdio.h>
/* A message holds the string we want to print and has a slot to store the next
* message to be printed (should one be queued up). */
typedef struct Message_S {
$ pry
[1] pry(main)> x = {}
=> {}
[2] pry(main)> x["hello"] = 1
=> 1
[3] pry(main)> x['hello'] = 2
=> 2
[4] pry(main)> x
=> {"hello"=>2}
@sw17ch
sw17ch / get_svn_files.rb
Created July 25, 2012 18:15
Get all files for a Gemspec using SVN.
# This function asks svn to list the status of all
# files in the repository. Then we filter out files
# that aren't versioned and aren't files.
# See also: svn help st
# $ svn help st
# status (stat, st): Print the status of working copy files and directories.
# ...
# With -v, print full revision information on every item.
# To install prerequisites for this sample code, run
# the following commands in a directory with all the
# files in this gist:
#
# > gem install bundler
# > bundler install
source "http://rubygems.org/"
gem "thor", "0.15.4"
@sw17ch
sw17ch / match_all.rb
Created August 10, 2012 14:54
String#match_all :: Like String#scan but returns array of MatchData objects instead.
class String
# This method will return an array of MatchData's rather than the
# array of strings returned by the vanilla `scan`.
def match_all(regex)
match_str = self
match_datas = []
while match_str.length > 0 do
md = match_str.match(regex)
break unless md
match_datas << md