Posts

More Thoughts On A JVM-Based Forth

Image
Wrapping up the fundamental design challenges for a Forth implementation on JVM. Introduction This post is the continuation of the ideas discussed earlier in my previous post Thoughts On JVM-based Forth Implementation (please read it if you haven't done so yet), in which a general execution model and an overall stack design was introduced. However, three major topics were either very shallowly discussed or were left untouched. They shall be covered in this post. Level of Abstraction Forth was designed in a period when RAM was an incredibly expensive resource to equip your system with; during the absolute reign of micro-controllers and custom-made boards/systems where 64KB of RAM would have been considered a luxury. Therefore it was designed with almost no abstractions over the raw hardware to allow full utilisation of it by the systems programmer. Nowadays, however, 2GB of RAM is a normal specification for even a cheap laptop [1] . And general programming focus, thanks to high...

Responsibly Upgraded Your Lisp Machine!

Image
=== ALL USERS PLEASE NOTE ======= Compiler optimizations have been made to macro expand LET into a WITHOUT-INTERRUPTS special form so that it can PUSH things into a stack in the LET-OPTIMIZATION area, SETQ the variables and then POP them back when it's done. Don't worry about this unless you use multiprocessing. Note that LET could have been defined by: (LET ((LET '`(LET ((LET ',LET)) ,LET))) `(LET ((LET ',LET)) ,LET)) This is believed to speed up execution by as much as a factor of 1.01 or 3.50 depending on whether you believe our friendly marketing representatives. This code was written by a new programmer here (we snatched him away from Itty Bitti Machines where he was writing COUGHBOL code) so to give him confidence we trusted his vows of "it works pretty well" and installed it. From Linux fortune cookies Image source billyfung2010.blogspot.com

Syntax Intuitiveness aka IQ Test

Image
A simple and real world lesson on programming language syntax intuitiveness. Introduction As a polyglot I have coded with dozens of languages, ranging from Lisp and Forth to ML and Java and even Ruby (yes, I confess!). I have seen many programming constructs and syntaxes that hardly anything surprises me (beyond Javascript). Most of the times I can somehow understand a code that is written in a language that I do not know (like Haskell). However, to put an end to my arrogance, the gods pushed me in a direction to see something such non-intuitive and mysterious that made me search the web to just roughly understand what it does. The Puzzle I was reading a piece of C++ (the almighty!) code and stumbled upon a line like below: int main() { //... aName otherName(...); //... } I tried to make sense out of line 3; I tried hard but got nowhere. "What on earth is that? Looks like a method definition but it's not....um...um...um...what the...!?" A search for aName in...

A Tale Of King and Evil or How I Made Peace With Ubuntu!

Image
In which I tell how Ubuntu won my trust back. Once Upon A Time... I used to be a fairly happy Ubuntu user from 2008 to 2011 (shh! I was a faithful FreeBSD and KDE 3 user before that). I could concentrate on my job and my desktop didn't get in my way. I didn't spend any of my time trying to configure things the sane way. A Kingdom Without An Heir But things changed by the double blow of Gnome 3 and Unity. Introduced at almost the same time, they ruined my IT life and destroyed my productivity. Seriously. Only gods know, how many hours (instead of happily coding) I spent fiddling Gnome 3 or Unity to have a responsive desktop that behaved like a desktop is supposed to: not get in my way! I switched to Ubuntu Gnome to find myself unsatisfied, and then back to KDE 4, and despite it being really cool, found it not for my type. Then tried XFCE and quickly learned it was a mistake. The Era Of Pain And Chaos This state of distress pushed me to the point that for almost 7 months I ...

Command Line Options: How To Parse In Bash Using “getopt”

Image
Use “getopt” in a Bash script to parse long and short command line options, enforce arguments for some options and spot dubious user input. Most of the times, when you need to automate system administration tasks on your Linux machine, writing a Bash script is your best bet. And sometimes, you need to be able to control the behaviour of your script at some point which leaves you with two choices: use environment variables (set before running the script) as sort of a flag or the better and more intuitive way to use command line arguments. What is "getopt"? getopt is a program that parses command line options in shell scripts. It is the enhanced version of older getopts and uses the getopt C library to do its job. It is compatible with getopts as long as GETOPT_COMPATIBLE environment variable is set, however some of it best features are not available in compatibility mode. An overview of command options Command line input is viewed in 3 categories by getopt: short ...

How To Enable Auto-Completion in Python Shell

Learn how to use TAB to auto-complete expressions in plain vannila Python shell. If you need to have auto completion in Python shell (almost the same way as iPython), it's very easy. Create a file named .pyreadline with the following content and put it in your home directory. #!python import rlcompleter, readline readline.parse_and_bind('tab: complete') Export the file as PYTHONSTARTUP variable; for example append it to .bashrc . echo "export PYTHONSTARTUP=~/.pyreadline" >> ~/.bashrc

How To Batch-set Environment Variables

Learn how to automatically (dump) set environment variables defined in a file. Sometimes you have a file (usually called .env or .environment ) with dozen of environment variables defined and you have to set those variables before running a command --for example this happens in Django projects with a .env file for settings. The solution is easy: export $(cat ENVIRONMENT_FILE | xargs) && YOUR_COMMAND

How To Add Logging To A Clojure Project

When deploying a Clojure application in a production environment, it is necessary to have logging enabled to be able to, in case of any failure, track down the problem using the logs later. Clojure inhertis serveral logging frameworks from Java like Log4j and it has its own Clojure'esque wrapper around Java logging API called clojure.tools.logging . This HowTo will help you get started with a working “loggable” Clojure project. Though the instructions assume that you're using Leiningen for build automation, it can be applied to any Maven based build too. Setting Up A New Project This creates a new empty project: $ lein new app logger1 Generating a project called logger1 based on the 'app' template. Now edit project.clj to add required dependencies: (defproject logger1 "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url ...

Thoughts On JVM-based Forth Implementation

Image
Is it possible and worth to implement Forth on top of JVM? Forth : A language which looks as weird as it is simple and powerful --and it looks extremely weird! I, first, came to know Forth with help of my good friend Michael a.k.a. ttmrichter and immediately Forth placed itself on top of the favourite language pyramid in my mind, alongside only Lisp family. Since then, I've been eager to do serious coding in Forth. However, there's a problem: most of Forth'ers work in the system/chip programming field which means that there are hardly any higher level libraries (e.g. UI toolkits or database connectors) for someone like me to try Forth in business applications. It's definitely possible to write all those libraries starting from the scratch but even the thought of it turns my stomach! Recently I've been thinking about implementing Forth on JVM. JVM Forth has several advantages: It will be platform independent. It will have access to tons of existing libraries...

Tornado Web Framework - Flash Messages

Implement flash messages in Tornado web framework. Problem Suppose you have a URL for editing an object under /OBJECT-ID/edit . Normally when user fills in the information on that page and presses the submit button, the request is sent to /OBJECT-ID/update which persists the data. But if the the data provided by user is not valid, you need to redirect user the to /OBJECT-ID/edit along with the provided data and validation errors, so s/he can correct it. The clean and preferred way to do this is by using flash cookies. Code All the source code provided in this post, is licensed under Apache License 2.0. import re import pickle from tornado.escape import to_unicode from tornado import web, escape class Flash(object): """ A flash message along with optional (form) data. """ def __init__(self, message, data=None): """ 'message': A string. 'data': Can be anything. ""...