I’ll Need a Currency Converter

Tags

, , , ,

The travel application I’ve been working on (really a set of locally-stored HTML, CSS and JavaScript files) requires a feature that enables quick currency conversions. It also needs to function independently of a working Internet connection.

The first pane has an H2 tag displaying what the exchange rate was when the application was last updated – this is hard-coded – and I’ve added a button that navigates the browser to the XE Currency Converter.

I figured a slider would be the quickest way of implementing a conversion feature in a UI, and it’s more efficient than trying to use a small touchscreen to type the values in. The user sees a price in Japanese currency, and moves the slider to get the approximate value in British pounds.
To make this, I used the onchange function to show the current selected slider value, and appended that to also show the value divided by 148.575 (or whatever the exchange rate happens to be).

Calculations with Values from Multiple Range Controls
Quickly it became evident that a user would find it difficult to get a precise enough conversion with a small slider representing such a large range. I needed to add a couple more sliders.

I spent several hours trying to craft some JavaScript that performs the conversion on the combined values from all three controls – who would have though this would be so tricky! HTML5 provides a workaround for this, with the ‘output‘ tag and the ‘oninput‘ form attribute.

The ‘oninput‘ attribute here contains JavaScript that performs the calculation:

form oninput="result.value=(parseInt(upperRange.value)+parseInt(centreRange.value)+parseInt(lowerRange.value))/(148.75)"

With an output just before the closing form tag:
output name="result"

Next, for the application to be usable, it also needs to display the current state of each slider. Fortunately we can chain both ‘onchange’ and ‘oninput’ within the same input tag for each slider:
The range tags now have the following attributes:

input type="range" id="centreRange" min="0" max="10000" value="0" step="1000" onchange="centreValue('JPY: ' + this.value + ' | ' + 'GBP: '+ this.value/148.475)" oninput="midRange(value)" /

Each form element is also assigned an output tag:
output for="centreRange" id="centreValue"

Plus some JavaScript to update the output tag:

function smallRange(yen)
{
document.querySelector('#centreValue').value = yen;
}

The code for the sliders would therefore look something like this:

Precise Currency Conversion Form
The JavaScript for the next currency conversion pane I borrowed from ProgLogic.com, which uses simple multiplication functions. This is tied to a set of form controls – text boxes and a submit button.

At the head of the HTML source, add the following function:

function yenConverter()
{
document.converter.dollar.value = document.converter.yen.value * 0.0089
document.converter.pound.value = document.converter.yen.value * 0.00673212
document.converter.euro.value = document.converter.yen.value * 0.00605
}

And in the main body, within a form element:

input type="text" class="form-control" name="pound" onChange="poundConverter()"
input type="text" class="form-control" name="yen" onChange="yenConverter()"
input type="button" class="btn btn-default" value="Convert"

When the button is pressed/clicked, the input fields are updated by poundConverter() and yenConverter() to whichever value multiplied by the exchange rate.

Showing and Hiding Conversion Tables
The other two panels I’ve added as conversion tables, in case the user simply needs a quick lookup to see roughly what the equivalent value is. This uses Bootstrap.csstable-striped‘ styling.

And a jQuery call to show/hide the quick conversion tables.


$(document).ready(function(){
$("#show-hide").click(function(){
$(".container-fluid").toggle();
});
});

And the HTML, using a Bootstrap.css button:
button type="button" id="show-hide" class="btn btn-default"

Advertisements

A Sexy Terminal Editor

Tags

, , ,

Getting the vim and nano editors to soft wrap lines has been a real pain, but I found another editor that does this nicely, and I really, really like it. XWPE is very easy to use, especially for anyone familiar with the Borland developer environments from the early 90s (the first ever IDE I’ve used, in fact). There isn’t any syntax highlighting to begin with, though.

The first thing I did was modify the environment colours. With the editor open, use ‘Alt+o‘ to open the options drop-down menu, and ‘Adjust Colors‘.

To make these changes persistent, select ‘Save Options’. The configuration is written to /home/[user]/.xwpe/xwperc. Opening and saving modified files is straightforward, using ‘Alt+f‘ and selecting ‘File Manager‘/’Save‘.

Stacks and Tiles
Another very useful feature of XWPE is the ability to open multiple files and switch between them in the same session. Windows will be stacked automatically if more than one file is open (up to 35 windows, according to the man page), but you can also get this by selecting ‘Cascade‘ in the Window drop-down menu. To navigate through the stack, press the F6 key.

Alternatively the windows in the stack can be tiled by selecting ‘Tile‘ in the Window menu. Again, the user can switch between these with ‘F6‘.

The only real problem with XWPE is that it tends to screw up the terminal after exiting the program, maybe because it was developed for use with X Windows or X11. To get round this, use the ‘reset‘ command to restore the terminal session.

Syntax Highlighting
There isn’t any default syntax highlighting for programming/scipting languages that aren’t listed in the configuration files, and even after modification it seems to work only when the program is launched with the ‘wpe‘ command.
The editor looks in /home/[user]/.xwpe for a syntax definition file, and if that doesn’t exist will attempt to read from /usr/local/lib/xwpe. The actual file path on a Debian system is /usr/share/xwpe/syntax_def.

Copy this to the /home/[user]/.xwpe directory, and set the normal user account as its owner:

#cp /usr/share/xwpe/syntax_def /home/[user]/.xwpe/syntax_def
#chown [username] /home/[user]/.xwpe/syntax_def

To this I’ve appended sections for C#, JavaScript and CSS.

An Examination of Koichi Tohei’s Ki Sayings

Tags

, , ,

Quite early into my study of Aikido I came across the scanned pages of Ki Sayings, which was published by Koichi Tohei in 1973. At the time I pretty much dismissed it as little more than a pseudo-religious text, such was the mystical language Tohei included. As it turns out, an explanation of this little book provides a fairly useful introduction to the concepts we discuss in Aikido, and why Tohei’s ideas were incorporated into what was a much harder style of Aikido.
According to Henry Ellis, in one of his many rants on the state of modern Aikido, Ki was never mentioned or discussed among junior grades at Abe Sensei’s dojo, and for understandable reasons. Aiki will always be something of a hidden secret, not because anyone choses to keep it a secret nowadays, but because an adequate understanding of it only comes gradually with years of practice. I don’t think the word ‘Ki‘ has a clear definition in English. A general description would be the phenomenon that manifests when everything is connected and in co-ordination with one’s centre of gravity. Ai-ki-jutsu, or Ai-ki-do, is the martial art based on this principle.

At the front of our dojo are displayed Koichi Tohei’s Four Major Principles to Unify Mind and Body:

  • 1. Keep one-point.
  • 2. Relax completely.
  • 3. Keep weight underside.
  • 4. Extend Ki.

Keeping ‘one point’ is to maintain awareness of your centre of gravity, and how your body is behaving in relation to it. To relax completely is to remove all tension within the body, so techniques aren’t about fighting strength with strength, but instead changing the direction of the opponent’s effort. To keep ‘weight underside’ is tricky to put into words, but very generally it means to keep your shoulders down and you body relaxed – it is much harder to fight against dead weight. Extending Ki is about practising techniques and throws with confidence, to project force. To this we’ve added a fifth principle: ‘Respect your partner’s Ki’. People do occasionally get injured when they over-estimate themselves or under-estimate their training partner.

So, all this background is important when reading Ki Sayings, because again Tohei used mystical descriptions that distracts from what needs to be learned. Tohei must have come to this realisation by the time he authored ‘Ki in Daily Life‘ (a pricey but strongly recommended book). Having progressed so far in Aikido, I know that Aiki can (and probably should) be taught without any reference to religion or spirituality. This, I think was one of the main things behind the disagreements that ended with Tohei’s departure from the Aikikai.

Unification of Mind and Body
Do not think that the power you have is only the power you ordinarily use and moan that you have little strength. The power you ordinarily use is like the small, visible segment of an iceberg.
This is at the heart of the Aikido we practice. Have you noticed how much easier it is to carry weight when it’s distributed around your body? Likewise, we can use more than 10% of our body when moving and manipulating objects. The hidden strength is manifested when the body works in co-ordination, and there are very useful self-defence applications of this, even beyond accepted Aikido techniques.

The One Point in the Lower Abdomen
When Tohei stated the ‘one point’ is the centre of the Universe, I think he meant that, from the Aikido practitioner’s perspective, the ‘one point’ is the centre of everything s/he interacts with. It is to make your centre of gravity the ‘unmoved mover’ within the dojo, so you’re the one directing the movements of opponents.

Living Calmness
Main point is to be relaxed, and not to react by transferring your body’s weight from your centre of gravity to a weaker position, not to react by focussing all your efforts through one part of the body.

The Subconscious
Doubt and the subconscious are the biggest obstacles to overcome in Aikido. Most of us started practising Aikido with the preconceptions that fights are won (or survived) using little more than strength, that resistance must be overcome by physical effort, that a smaller man cannot throw someone considerably bigger and more powerful, that relaxation is the worst response to imminent danger. Maybe you’re extremely skeptical that a man like Morihei Ueshiba once forcefully threw a Judo master to the ground with his little finger. The core of Aikido is counter-intuitive.

Ki Development Exercises
Often our sessions begin with 20 minutes’ Ki development exercises, though this probably isn’t really necessary since it’s developed alongside our competence with techniques and katas.
These exercises initially test the ability to relax completely, and progresses to a test of our ability to maintain that relaxation against physical resistance. For senior grades, the ability to move always with co-ordination is tested.

Ki Breathing Methods
Also essential to develop is the habit of breathing correctly, because people have a tendency to hold their breath as they perform a technique. This isn’t sustainable when continuously dealing with one opponent (or attacker) after another for several minutes – and you’d more than likely black out during a higher level grading.
Breathe deeply through your nose, as much as you can, and exhale slowly through the mouth. Breathe in as you receive the opponent’s entry and exhale as you throw the opponent.

I’ll end this post with two closing pages from the book.

Aikido and Miyamoto Musashi’s Book of Five Rings

Tags

, , , ,

The Book of Five Rings continues to inspire martial artists today, and is occasionally mentioned within the walls of our own dojo during one of Sensei’s digressions on weapons techniques. It was authored in 1645 by one of the most famous (maybe even the most famous) swordsmen in Japanese history, Miyamoto Musashi, then aged 60, as a reflection of what he learned throughout his lifetime. Musashi had fought and won about 60 duels, often against those who regarded themselves as highly skilled swordsmen, though it wasn’t until he was about age 50 that he discovered what he called the ‘Way of Strategy’.
I have been reading Victor Harris’ translation of this work, which introduces the main body of work with some background about Japanese society during the lifetime of Musashi.

Did Musashi contribute to the martial art that eventually became Aikido? While the pedigree of Daito-ryu Aiki-jutsu might be uncertain, it appeared in the public domain as a developed fighting system that appears to be in agreement with what he taught. In multiple places he appears to make oblique references to Aiki without actually revealing the secret. For example, he mentioned that by mastering the Way of Strategy ‘you can suddenly make your body like a rock, and ten thousand things cannot touch you‘, and that this was passed on through oral tradition – I cannot authoritively say whether this is widely taught outside my own style of Aikido, but the oldest among us were very close to Ueshiba in terms of pedigree.
Our sword techniques came to us, it seems, through Morihei Ueshiba’s study of the Kashima Shinto Ryu instead of from Takeda Sokaku (that is according to a study of Admiral Isamu Takeshita’s journal), and we handle the katana/bokken exactly as Musashi described, gripping only with the two fingers and never with the index finger (there are at least two technical reasons for this). Also, he refers to something called ‘Attitude-No Attitude’, that techniques are simply a means to learn something more fundamental, such as Aiki, that cannot easily be passed on through words.
In any case, I think there’s a lot in the Book of Five Rings of importance if we’re practising any martial art. It challenges us to question the utility of what we’re taught.

What is the Way of Strategy that Musashi referred to? Advocating simplicity and a utilitarian approach, his general point was: ‘The true Way of sword fencing is is the craft of defeating the enemy in a fight, and nothing other than this‘.
Strategically dealing with the adversary, his psychology and the circumstances is a surer way to success than merely fighting against his weapon, and the Way of Strategy suggests practical ways this could be done. In the Wind Book, he compares his Way with the schools that claim secret and obscure teachings, that place too much importance on traditions, and states: ‘the way to understanding is through experience, I do not speak of interior or gate‘.

The Ground Book
Defining ‘strategy’ and the ‘Way of Strategy’, Musashi compared it with the way tradespeople, such as carpenters, work to some design or plan. Followers of the Way of Strategy should be methodical when fighting adversaries, and make use of the conditions, circumstances and the environment.
Also important is timing, knowing the right moment to attack, and this comes only with much practice with a range of opponents. Determine the rhythm of the adversary and use it to your advantage.
Lastly there is perception, in terms of being able to see a wider and detailed picture, both in terms of studying martial arts and in terms of not paying attention solely to the opponent’s weapon in a fight.

The Water Book
This section contains something of a syllabus for sword fighting, and starts with at least two prerequisites. The ‘Stance in Strategy’, is a core principle of Aikido – stand upright, with shoulders lowered and weight underside. Musashi recommends practising this every day, making it the way you normally carry yourself. Of course, we also recommend making relaxation and co-ordination a habit outside our dojo.
As for gaze, I take Musashi’s words to mean developing peripheral vision and the ability to judge the body language and intention of the adversary instead of just the movement of the weapon. Strategy and timing isn’t possible without this, especially the strategy of interrupting an attack just as the opponent is about to move.

Though mastery of the katana is fundamental to the Way of Strategy, one shouldn’t be overly fixated on any particular weapon. Later on he warns that the ‘blade path is large, so the long sword is an encumberance‘ and a shorter sword might be more appropriate to a situation.
Musashi argued for the use of two swords in fighting – the katana and wakisashi in each hand, and against holding either with both hands unless it was necessary.

As with martial arts in general, there are a finite number of ways someone could attack, and consequently the number of entries is sufficiently finite to form the basis for any martial arts syllabus. The ‘Five Attitudes’ Musashi wrote about are counters to the five possible ways an adversary might attack using the sword – this is roughly the number of sword/bokken katas I practiced during my short time at an Iwama dojo, incidentally. When countering, one should use the whole body to defend, and immediately following up with something else.
Eventually, when the syllabus is mastered, there’s the realisation that there are no techniques or ‘attitudes’. Techniques are not Aikido, but rather the method for learning it. As Ueshiba himself one said: ‘There are no techniques. What you express each time is a technique.

Then there is the principle of ‘Crossing at a Ford’, which means crossing the metaphorical ‘ford’ where the path and conditions are easiest. For fighting, this means knowing the adversary’s strengths and weaknesses, and adjusting our techniques to them, so that we’re fighting along the path of least resistance. An example of this would be applying a technique into practice precisely when the opponent is off-balance, or redirecting the opponent’s attacks through the path of least resistance, or ‘taking the opponent’s mind’ before striking.

The Fire Book
Where the Water Book covers personal techniques and disposition, the Fire Book deals with the actual strategy, the ways of gaining advantages and exploiting weaknesses. Often strategy is determined by maintaining distance. This is understandable, as you want to keep your distance to avoid getting hit, while maybe looking for an opportunity to strike the opponent the moment he’s within range.
Instead, Musashi suggests basically rushing the opponent to prevent his gaining the opportunity to fight strategically, while chasing him into positions in which it’s awkward to fight. For example, it’s possible to affect the opponent’s visibility by standing with your back to the sun, or by chasing the opponent into places where his view and movements are obstructed, or by moving the opponent onto ground that makes it harder to move quickly.
It is also possible to manipulate a person’s psychology, perhaps by forcing a change in his timings, making him agitated, making him over-confident or impatient.

The Wind Book
In this section we find technical criticsms of what other martial art schools were teaching. In particular the focus on things he felt were unnecessary.

The Book of the Void
Wisdom is knowing that one knows nothing – the more learned we are, the more we’re aware of how little we know. This seems to be the main lesson in the Book of the Void. What don’t we know as we’re progressing in a martial art? What conceivable situations are we not training for? What can we adopt from the other martial arts?
For this reason Musashi recommended studying a wide range of martial arts and sword schools, learning what’s worth learning, while remaining true to the Way of Strategy.

The Art of War (Sun Tzu)

Tags

,

I came across this while browsing through the book stores in Newport: It’s the Arcturus 2017 edition of Sun Tzu’s The Art of War. Many full-colour illustrations adorn the glossy pages inside the hardback red silk cover.
The book was authored around 2,500 years ago, and it’s likely Sun Tzu recorded his observations throughout his military career and organised them into thirteen sections at a later point. It has been studied by world leaders throughout the 20th century, we’re told in the introduction. Like Miyamoto Musashi’s Book of Five Rings that came 2,000 years later, which deals more with actual combat and tactics, the Art of War is still used by modern-day strategists in warfare and business. I’ve seen and heard many a Sun Tzu quotation among the little quips of wisdom from SNCOs in the British Army.

Widely quoted and consulted as it is, I don’t think Sun Tzu actually offers much here that should be mistaken for a philosophy or deep insight into human nature that’s very usable to most of us. The closing chapter of this particular book addresses its applicability to business, politics and sport, but it doesn’t quite manage to make the parallels between those things and armed conflict.

You’ve probably heard it said that ‘success = preparation + opportunity’. I think main content of the Art of War can be summed up by a simple paragraph: Success is certain with the right amount of research, planning, preparation and execution. The plan should be developed based on what’s known of the environment, the disposition of allies and adversaries, the resources available, predictions about the adversary’s behaviour, and many other factors. By secrecy and deception, one could also prevent the adversary making calculated decisions.

But business managers are not military officers or warriors, and human relations in the civilian world, even between rivals, aren’t battlefields. Neither is warfare the default mode of human affairs, but something that happens after civility and diplomacy fails. However, I think the Art of War could provide us with another dimension of insight into notable historical events and the decisions made by world leaders today.

Sun Tzu and the Economy of Warfare
Sun Tzu argues that ‘there is no instance of a country having benefited from prolonged warfare‘. In the second chapter, ‘Waging War’, he explained that prolonged military campaigns impose economic demands on the state that could outweigh the benefits – these are immediate costs of mobilising soldiers and supporting infrastructure, and the costs of maintaining that deployment at an optimum fighting condition. Eventually weakened economically, and perhaps socially, the state could become less capable of defending itself against another adversary.

Intelligence and the Use of Agents
The final section deals with the use of spies and espionage, and I found this worth commenting on because of its parallels with today’s intelligence and espionage. Sun Tzu’s greatest concern was the economic and social costs of the ineffecient usage of military resources, of protracted deployments, and he viewed espionage as a solution to that very problem.
Sun Tzu defines five classes of spy, and the method for using them:
’21. The enemy’s spies who have come to spy on us must be sought out, tempted with bribes, led away and comfortably housed. Thus they will become converted spies and available for our service.
22. It is through the information brought by the converted spy that we are able to acquire and employ local and inward spies.’

Looks very familiar, doesn’t it? Sun Tzu is making a reference to the management of insiders who become agents through the exploitation of some character flaw. This is followed up by sending the ‘doomed spy’ to create disinformation and perhaps to provide a cover for the ‘surviving spy’.

’23. It is owing to his information, again, that we can cause the doomed spy to carry false tidings to the enemy.
24. Lastly, it is by his information that the surviving spy can be used on appointed occasions.’

We also find social engineering, starting with a recce to determine certain details about people associated with a target:
‘Whether the object be to crush an army, to storm a city, or to assassinate an individual, it is always necessary to begin by finding out the names of the attendants, the aides-de-camp, and door-keepers and sentries of the general in command. Our spies must be commissioned to ascertain these. […] It is through the information brought by the converted spy that we are able to acquire and employ local and inward spies.’