Monday 26 November 2012

Java Lazy Evaluation

Lazy evaluation. This is where a piece of code is written which evaluates a value (usually requiring a relatively large amount of processing), but the code isn't executed until the moment it is needed (if ever).

For example, class A calls a method in class B. It passes some values (which take a long time to compute). If the values are passed lazily then class B can decide when and if to compute them. It is up to class A to implement the code for how to evaluate the value. So maybe there are 3 lazy values passed and class B decides it doesn't need to use value 1 but it does use values 2 and 3 (perhaps invoking each evaluation in separate threads, to maximise performance). The code to evaluate value 1 is never called, the code to evaluate values 2 and 3 will be called once and only once. Subsequent retrievals of a lazily evaluated value will return a cached copy of the value (unless the lazy evaluator is reset... see the code below).

Here is the class:

public abstract class Lazy<T>
{
  private boolean _evaluated = false;
  private T _value = null;

  public final T get()
  {
    if (!_evaluated)
    {
      _value = evaluate();
      _evaluated = true;
    }

    return _value;
  }

  public void reset()
  {
    _evaluated = false;
    _value = null;
  }

  protected abstract T evaluate();
}

... and here is how to use it:

  // Setup.
  final float height = 7.0f;

  // Create the lazy.
  Lazy<Float> halfHeight = new Lazy<Float>()
  {
    public Float evaluate()
    {
      return height / 2;
    }
  };

  // Get it's value.
  halfHeight.get();

And that's all you need. This code is completely free to use by anybody. It is held under the Do What You Want To Public License: http://tinyurl.com/DWYWTPL

Thursday 11 October 2012

Half-sort



Half-sort is a sorting algorithm. A copy of it can be found here:
https://www.box.com/s/p0pn15736599bv8rb8rx

It has the following features:

  • It is an in-place sort. (No extra data space needed.)
  • It works by "divide and conquer" recursion. (It is quite like Quick-sort and Merge-sort.)
  • It is not stable. (i.e. It does NOT keep the original order of identically valued items in the list.)


Here is some pseudo-code that explains how the Half-sort algorithm works:

  1. Calculate M to be a value that is half way between the smallest item and the biggest item in the collection. You may have to scan the entire collection to find this value, but it may already be known or can be easily calculated.
  2. L begins by pointing to the start of the collection and moves forwards until an item bigger than M has been found.
  3. R begins by pointing to the end of the collection and moves backwards until an item smaller than M is found.
  4. Swap the items at L and R.
  5. Carry on doing this until L and R meet in the middle. This would be the mean average middle, not the middle index position.
  6. Now repeat these steps on each side of collection, until it is fully sorted.



This code is completely free to use by anybody. It is held under the Do What You Want To Public License:
http://tinyurl.com/DWYWTPL

Wednesday 22 August 2012

Landscape Chess

I had a weekend of chess. Saturday, a couple of friends came over and we played some games. Next day my sister's family came, my nephew wanted to play chess too. So we had a few games.

Then our young daughter wanted to play and we had a good mess around, laughing our heads off as we came up with crazy ideas for new rules and ways that the pieces can move. Then out of the chaos some order arose and we found a fun new way to play chess.

Originally it had another name, but after doing some research we found another chess variant had that name. So to make this variant unique we decided on naming it Landscape Chess. At first we were somewhat disheartened to find somebody had already created a chess variant using a diagonal board, although we initially guessed someone probably would have. However, the other variants all seemed to muddy the beauty of chess. We think this variant complements chess, and even more so it actually brings something new to explore.

How to play Landscape Chess

Setting up

The chess board is turned diagonally, so that a black corner faces each player. The pieces are arranged as shown in the pictures (K is king and N is knight).




Movement

The pieces actually move in the same way as regular chess... except for the pawn, which moves pretty much in the usual manner, only diagonally.







Rules

There is no castling in Landscape Chess. When pawns get to either edge of the other side of the board they can be promoted to any other piece. The goal of the game hasn't changed from regular chess: you must try to get the opponent's king into check mate. 

Landscape Chess has one new rule not found in regular chess: any bishop on a white square can be moved to an adjacent black square, possibly taking an opponent piece in the process. The reason for this new rule is that all bishops begin on white squares at the beginning of the game. This new rule adds much to the strategy and actually makes the bishop a very formidable piece, if used wisely.

Saturday 21 July 2012

Random name generator

This is a random name generator written in Javascript. It includes a swear filter. I wrote the source code myself and have made it public domain. That is, anyone can use the code for any reason and edit it however they like.

Generated name:
 

You can download the source code here:
Random Name Generator.7z

This code is completely free to use by anybody. It is held under the Do What You Want To Public License:
http://tinyurl.com/DWYWTPL

Do Whatever You Want To Public License


DWYWTPL - DO WHATEVER YOU WANT TO PUBLIC LICENSE

When free means completely free!

The Do Whatever You Want To Public License (DWYWTPL) is a free software license.
When analysing whether a license is free or not, you usually check that it allows free usage, modification and redistribution. Then you check that the additional restrictions do not impair fundamental freedoms. The DWYWTPL renders this task trivial: it allows everything and has no additional restrictions. How could life be easier? You just DO WHATEVER YOU WANT TO.

The license text

DO WHATEVER YOU WANT TO PUBLIC LICENSE
Version 1, July 2012

Copyright (C) 2012 Chris Nash <cndefend-license@yahoo.co.uk>

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.

DO WHATEVER YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

1. You just DO WHATEVER YOU WANT TO.

NO WARRANTY CLAUSE FOR SOFTWARE

The DWYWTPL is an all-purpose license and does not cover only computer programs; it can be used for artwork, documentation and so on. As such, it only covers copying, distribution and modification. If you want to add a no warranty clause to software just insert the following into the source code as a comment and/or into the documentation, as you see fit:
This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do Whatever You Want To Public License, Version 1, as published by Chris Nash.
See http://tinyurl.com/DWYWTPL for more details.