Showing posts with label knowledge. Show all posts
Showing posts with label knowledge. Show all posts

Monday, July 11, 2011

A usefull Postscript Trick

In this blog post I will share a Postscript trick which I use a lot.

Postscript is best known as a page description language. It is a very powerful language, in fact, Postscript is Turing-complete. The language is stack-oriented so you will see a lot of stack operators like dup, exch and roll. These operators can be used to perform all necessary (and possible) stack operations. But it can be very tasking for a programmer to use them correctly.

For example, the following code draws a rectangle perpendicular to the coordinate axis with its lower left vertex at (x,y) and a width of w and height of h. In the comments an impression of the stack is given.

/rectangle {     % stack: h w x y
  moveto         % stack: h w
  dup            % stack: h w w
  3 -1 roll      % stack: w h w
  0 rlineto      % stack: w h
  0 exch rlineto % stack: w
  neg 0 rlineto  % stack:
  closepath      % stack:
  stroke         % stack:
} def
You can easily get lost in all these stack manipulations. Make one mistake and it is likely that your expectations are not met.

Luckily you can solve this problem within postscript. If you look at the above code again, you may notice that a definition is made. The symbol "/rectangle" is associated with the code-block by the def operation. One can associated other values with this operator. You can use it to simplify the above code.

/rectangle {      % stack: h w x y
  /y exch def     % stack: h w x
  /x exch def     % stack: h w
  /w exch def     % stack: h
  /h exch def     % stack: 
  x y moveto      % stack: 
  w 0 rlineto     % stack:
  0 h rlineto     % stack:
  w neg 0 rlineto % stack:
  closepath       % stack:
  stroke          % stack:
} def
There is one problem with this solution. It defines, and possibly overrides, associations for the symbols "/x", "/y", "/w" and "/h". This method leaks its variable in to the context in which it is called. Again, postscript can solve this problem. Initially the associations are made in the dictionary userdict. By using an other dictionary and throwing the dictionary away after it's service, we avoid polluting the userdict. (In the following code examples I will omit the impression of the stack.)
/rectangle {
  4 dict begin
    /y exch def
    /x exch def
    /w exch def
    /h exch def
    x y moveto 
    w 0 rlineto
    0 h rlineto
    w neg 0 rlineto
    closepath
    stroke
  end
} def
Here we first define a dictionary for four keys with 4 dict. Start using it with begin. When we are done we discard it with end. This nicely allows the usage a variable with out polluting the userdict. Because I used this trick so often I polished it up a little. I left out the "body" of this method so you can concentrate an the scaffolding.

/rectangle {
  [/y /x /w /h] dup length 0 add dict begin {exch def} forall
     ...
  end
} def
That one-liner signals the variable names used in this method, creates a dictionary and initialises it with values from the stack. The 0 add is there to easily add local variable. I the body of the method is using a local variable add one the the integer to ensure the size of the dictionary.

This wraps up this Postscript type. I explained how you can use variable in methods without polluting the userdict.

Saturday, August 14, 2010

Spreading Knowledge

In this post I will outline various forms of spreading knowledge.

I signed the manifesto for software craftsmanship and I take my responsibilities serious. Not only should you maintain a high standard for your self, you should help others in becoming software craftsman.
One way in doing this is by spreading knowledge.

There are various ways one can share and spread knowledge. Any form of communication with the intent of explanation is a form of spreading knowledge. Examples of communication forms abound. Let's group these forms according to the number of senders and receivers.

We will distinguish the following numbers: one and many. Below I have created a table listing the various combinations of senders and receivers.


#Senders#RecieversExamples
11One-on-one tutoring
1manyPresentation or blog
many1Open outcry or a forum
manymanyOpen discussion

In seeing these summary one can check off a list of participation in various forms of communication. For example I now realise that I do not often participate in a forums.
So I will challenge myself in actively seeking out opportunities to contribute to forums.