INSPext (Or: Of Uselessness & Usefulness)

To cut things short, INSPext is here.

About a month ago I started a new programming project. It was a moment of feeling isolate at home, and so I went out to a near café and started scribbling on a piece of paper. I had no goal or idea where I was going, other than spending my time so the next day will start already.

In my battle against the Dark Lord of The Blues I started writing my thoughts. It’s a good exercise both for enhancing writing skill and for raising feelings and thoughts to the surface. And then a faint idea came in: wouldn’t it be nice to compile this text I’ve written and output it in a nice, elegant typeset fashion? Not because it would be useful to have my text nicely typeset, but because it would be useful to write a compiler. I was thinking at the time less of a practical tool I’d want and more of a tool I’d want to create.

  ASDF

Asdf as asdfing, asd F asdfed.  "Asdf!" asd asdfed.  "Asdf!".
"Asdf?" F asdfed, "ASDF?!".  "A, asdf".

Asdf asdf, asdfasdfion as asdfing as.  ...

...

It was easy and straightforward to see the pattern in my text. Maybe because I was free-writing, not writing a complex document such as a math-infected assignment or a term paper packed with tables and page numbers and table of contents and images. Just fluent text: nothing but headings and paragraphs. Yes, my headings are always indented with space and my paragraphs are always delimited with an empty line. Even my sentences are clearly separate from each other with two spaces. To some extent, I have a language there which I can parse!

That was the process in which INSPext was coming out to life. I quickly had a grammar written down that defined how I have always structured my text, in terms of headings and paragraphs. It was a simple grammar, composed of maybe 3 or 4 rules. And this, in fact, encouraged me tremendously to go right ahead and bang out the scanner and parser code.

I thought this would be a small project to spend the day. Nothing worth blogging about. But it’s been a month now that I’ve been playing with this little baby, and so I think it’s time I acknowledge its place among the living. So allow me to introduce: INSPext.

Posted in Uncategorized | Leave a comment

A Bit of Coding

Given a sequence of cards from a deck of cards (repetitions such as twice ace of hearts are allowed) and a subset of cards, write an algorithm that removes any occurence of any of the cards in the subset from the sequence.

For instance, given the sequence

[As,4s,6h,6h,7d,Kc,Ks]

and the subset

[As,6h]

the output should be

[4s,7d,Kc,Ks]

Posted in Uncategorized | Tagged | Leave a comment

Dynamic Library Binding Error on Mac OSX with Rails

While creating a rails application skeleton I ran into the following dyld complaint:

~$ rails exp
dyld: lazy symbol binding failed: Symbol not found: _rb_intern2
Referenced from: /Users/matan/usr/lib/ruby/1.9.1/gems/hpricot-0.8.1/lib/fast_xs.bundle
Expected in: flat namespace

dyld: Symbol not found: _rb_intern2
Referenced from: /Users/matan/usr/lib/ruby/1.9.1/gems/hpricot-0.8.1/lib/fast_xs.bundle
Expected in: flat namespace

Trace/BPT trap

If the above looks familiar to you try to unset GEM_HOME and see if it resolves your issue:

~$ unset GEM_HOME

Alternatively add $HOME/usr/lib/ruby/1.9.1/bin (or wherever you installed Ruby) to your PATH.

Posted in Uncategorized | Leave a comment

vimdiff

If you’re a command-line animal like I am you need to know vimdiff.

vimdiff in action

vimdiff in action

vimdiff is an interactive file comparison and merging utility for the command-line. It simply starts Vim in a special mode called the diff-mode. To get started with vimdiff from the command-line,

$ vimdiff oldfile newfile

2 pairs of commands will get you started:

  • Navigating: ]c and [c will take you to the next and previous diffs, respectively.
  • Merging: dp and do will merge the current diff from the current buffer into the other buffer, and from the other buffer into the current buffer — respectively. dp stands for diff-put and do stands for diff-obtain.
Posted in Uncategorized | Tagged | Leave a comment

My Makefile Snippet

I use SnippetsEmu for my snippets in Vim. Offered for download below are my snippets for makefiles for C and C++.  The C and C++ makefiles will expand on makec and makecc, respectively. Place the file in ~/.vim/after/ftplugin/.  Make sure it is named make_snippets.vim and you have SnippetsEmu installed.

make_snippets.vim

let st = g:snip_start_tag
let et = g:snip_end_tag
let cd = g:snip_elem_delim
exec “Snippet makecc # Specify the main target\r<BS><BS>TARGET = “.st.”bin”.et.st.et.”\r# Default build type\r<BS><BS>TYPE = debug\r# Which directories contain source files\r<BS><BS>DIRS = .\r# Which libraries are linked\r<BS><BS>LIBS =\r# Dynamic libraries\r<BS><BS>DLIBS =\r\r# The next blocks change some variables depending on the build type\r<BS><BS>ifeq ($(TYPE),debug)\r<BS>LDPARAM =\rCCPARAM = -Wall -g3\rMACROS =\rendif\r\rifeq ($(TYPE),profile)\r<BS>LDPARAM = -pg /lib/libc.so.5\rCCPARAM = -Wall -pg\rMACROS = NDEBUG\rendif\r\rifeq ($(TYPE), release)\r<BS>LDPARAM = -s\rCCPARAM = -Wall -O2\rMACROS = NDEBUG\rendif\r\r# Add directories to the include and library paths\r<BS><BS>INCPATH = . $(HOME)/Development/include\rLIBPATH =\r\r# Which files to add to backups, apart from the source code\r<BS><BS>EXTRA_FILES = Makefile\r# The compiler\r<BS><BS>CXX = g++\r\r# Where to store object and dependancy files.\r<BS><BS>STORE = .make-$(TYPE)\r# Makes a list of the source (.cc) files.\r<BS><BS>SOURCE := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.cc))\r# List of header files.\r<BS><BS>HEADERS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.hh))\r# Makes a list of the object files that will have to be created.\r<BS><BS>OBJECTS := $(addprefix $(STORE)/, $(SOURCE:.cc=.o))\r# Same for the .d (dependancy) files.\r<BS><BS>DFILES := $(addprefix $(STORE)/,$(SOURCE:.cc=.d))\r\r# Specify phony rules. These are rules that are not real files.\r<BS><BS>.PHONY: clean backup dirs\r\r# Main target. The @ in front of a command prevents make from displaying\r<BS><BS># it to the standard output.\r<BS><BS>$(TARGET): dirs $(OBJECTS)\r@echo \” LD $(TARGET)\”\r@$(CXX) -o $(TARGET) $(OBJECTS) $(LDPARAM) $(foreach LIBRARY, \\\r$(LIBS),-l$(LIBRARY)) $(foreach LIB,$(LIBPATH),-L$(LIB))\r\r# Rule for creating object file and .d file, the sed magic is to add\r<BS><BS># the object path at the start of the file because the files gcc\r<BS><BS># outputs assume it will be in the same dir as the source file.\r<BS><BS>$(STORE)/%.o: %.cc\r@echo \” CXX $?\”\r@$(CXX) -Wp,-MMD,$(STORE)/$*.dd $(CCPARAM) $(foreach INC,$(INCPATH),-I$(INC)) \\\r$(foreach MACRO,$(MACROS),-D$(MACRO)) -c make_snippets.vimXXlt; -o $@\r@sed -e ’1s/^\\(.*\\)$/$(subst /,\\/,$(dir $@))\\1/’ $(STORE)/$*.dd > $(STORE)/$*.d\r@rm -f $(STORE)/$*.dd\r\r# Empty rule to prevent problems when a header is deleted.\r<BS><BS>%.hh: ;\r\r# Cleans up the objects, .d files and executables.\r<BS><BS>clean:\r@echo Cleaning up\r@-rm -f $(foreach DIR,$(DIRS),$(STORE)/$(DIR)/*.d $(STORE)/$(DIR)/*.o)\r@-rm -f $(TARGET)\r\r# Backup the source files.\r<BS><BS>backup:\r@-if [ ! -e .backup ]; then mkdir .backup; fi;\r@zip .backup/backup_`date +%d-%m-%y_%H.%M`.zip $(SOURCE) $(HEADERS) $(EXTRA_FILES)\r\r# Create necessary directories\r<BS><BS>dirs:\r@-if [ ! -e $(STORE) ]; then mkdir $(STORE); fi;\r@-$(foreach DIR,$(DIRS), if [ ! -e $(STORE)/$(DIR) ]; \\\rthen mkdir $(STORE)/$(DIR); fi; )\r\r# Includes the .d files so it knows the exact dependencies for every\r<BS><BS># source.\r<BS><BS>-include $(DFILES)”
Posted in Uncategorized | Tagged | Leave a comment

How to Uninstall the OS X GTK+ Development Framework

  1. Delete the framework directories /Library/Frameworks/{Gtk,Cairo,GLib}.framework
  2. Delete the XCode User Template /Users/You/Library/Application Support/Developer/Shared/XCode/Project Templates/Application/GTK+ Application
Posted in Uncategorized | Tagged | Leave a comment

Parsing a Graph

In Ruby parsing a graph data-structure from an input stream is surprisingly easy if I represent the graph internally as a hash of node-to-neighbours. Consider the following graph data input:

a: [b, c, d]
b: [b, c]
c: [a]

Node a points to neighbour nodes b, c and d. Similarly node c points to neighbour node a. Here is the Ruby code which parses this graph, assuming it comes from stdin:

require 'yaml'
g = YAML::load(STDIN)

I can generate a dot file that visualizes the graph like so:

puts "digraph {"
g.each_key do |v|
  g[v].each { |n| puts "  #{v} -> #{n};" }
end
puts "}"
Visualization of Graph g

Visualization of Graph g by graphviz dot

If I want to add support for edge weights then I can do it by changing the input format:

a: {b: 3, c: 3, d: 2}
b: {b: 0, c: 1}
c: {a: 3}

All I’m doing is playing on YAML, so my parsing code need not change. I “added” a feature without changing a single line of code!

It is interesting to find an analogous technique in C++, where I parse a graph into a map. The following code will take a directed, unweighted graph from stdin:

#include<iostream>
#include<algorithm>
#include<iterator>
#include<map>
#include<set>
#include<sstream>
#include<string>

using namespace std;
typedef map<string,set<string> > graph;

int main(int argc, char* argv[])
{
    string line;
    graph g;
    while( getline(cin, line) ) {
        istringstream ss(line);
        string from, to;
        ss >> from;
        graph::mapped_type neighbours;
        copy(istream_iterator<string>(ss),
             istream_iterator<string>(),
             inserter(neighbours, neighbours.end()));
        g.insert(make_pair(from, neighbours));
    }

    return 0;
}

The standard C++ library doesn’t support YAML, but it does provide help for parsing whitespace-delimited input through the iostream library. That’s what the code above takes advantage of. Graph input for the above C++ program will thus look like so:

a b c d
b b c
c a

I leave it as an exercise for you to add support for parsing a weighted graph:

a b 3 c 4 d 5
b b 0 c 1
c a 4
Posted in Uncategorized | Tagged | Leave a comment

Quote

And I quote: “steenk, stuhnk, stawnk!”

Posted in Uncategorized | Tagged | Comments closed

Curious Laughs

There’s nothing more heart-warming and curiously funny than another person laughing. It makes me feel good, and I don’t even know why.

Posted in Uncategorized | Tagged | Comments closed

Posts[0]

Good day stranger, and welcome to my corner on these here interwebs.

Posted in Uncategorized | Leave a comment