Tagged: Languages
Code Club first session
At last managed to lead my first “Code Club” session – it had a slightly chaotic start as none of the computers we were using had Scratch installed and nor did we have access to a login that allowed us to install Scratch in the Windows “Programs” directory – but once we worked around that we all had great fun.
From the start it was obvious that Scratch made sense to the kids – they immediately grasped that the endless loop control would set the actions it enclosed to run endlessly. Of course nobody (apart from Visual Basic users?) works with similar simple graphics tools when writing an industrial strength program, but that was not the point: this is about teaching loops, conditionals and branches and so on.
The lost time at the start meant it was all a bit hurried so I do not know how much of the programming the children took in – as opposed to just ensuring that their Scratch scripts matched those in the worksheet. But on the first time out – none of the children had used Scratch before – simply being able to manipulate the programming elements was probably more than enough.
In any case, all of them were hugely enthusiastic when I told them they could install Scratch on any computer they had at home and practise on it there.
Code Club feels like a huge success to me already.
Related articles
- Code Club Week 1 First Thoughts (antsict.wordpress.com)
- Raspberry Pi in schools (raspberrypi.org)
- Week 6 in ScratchPiBot Club (cymplecy.wordpress.com)
- Coding for kids is as easy as Pi (wired.co.uk)
- A nostalgic look at what a 13 year old can do with a C64 (hackaday.com)
- Codecademy’s Zach Sims is leading a movement; now can he build a business? [GigaOM] (gigaom.com)
- Week 4 – ShrimpingIT Maker Club (cymplecy.wordpress.com)
The expressive power of BASIC
As I have been working on BINSIC – my reimplementation of BASIC as a domain specific language via Groovy- I have been increasingly struct by how unbelievably awful BASIC is (at least in the ZX80/ZX81 dialect that I am basing all this on).
My memories of it are of a fun language where it was possible to quickly pull together some useful code to do the sorts of things 15, 16 and 17-year-old geeks were interested in.
But I really have to wonder now – it doesn’t even support user-defined functions: looking back I wonder why I wasn’t more enthused by PASCAL when I met it and its procedural programming paradigm at university: it certainly feels that I ought to have seen it as a fantastic improvement (though by then I was more into Z80 machine code than any high-level language).
But BASIC does have its strengths – as I have found out.
This piece of code is a prime example: 100 INPUT V
This means create the numeric variable called V and assign to it the value typed in by the user at the keyboard.
Trying to do this in Groovy/Java requires the creation of a whole new class just to handle the keyboard input, as well as mess about with thread synchronisation to ensure that the process waits for the input … what follows is just a part:
def waitOnInput()
{
def textIn = binsicEngine.binsicWindow.textIn
clearInputs(textIn)
def countDown = new CountDownLatch(1)
textIn.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
BinsicConstants.INPUT)
def inputAction = new BinsicInputAction(textIn, binsicEngine.preProc,
countDown)
textIn.getActionMap().put(BinsicConstants.INPUT, inputAction)
countDown.await()
return inputAction.result
}
Related articles
- Who owns the perk in Java? (economist.com)
- Just like 1980 (cartesianproduct.wordpress.com)
Is Groovy back in fashion?
Last year I was taught “Object Orientated Design and Programming” as part of my Birkbeck MSc, using Groovy, a dynamic functional language built on top of Java and running on the Java VM.
I enjoyed it and liked Groovy – I went on to write some pieces of software for my MSc project using it.
But it also gave the impression of being a dying language and there were some complaints from fellow students who thought C# or Java itself would have been a better bet for them jobs wise (to which one of the lecturers responded with admirable chutzpah with a suggestion of using Lisp in the future).
This last week I have again been dabbling in Groovy and I get a sense that the language is suddenly back in fashion and its community of users seems more energy charged than a year ago.
Nothing scientific to back that feeling up with, just my judgement.
Related articles
- A Quick Introduction to the Groovy Language (Part 1) (java.sys-con.com)
- The Big Three – Scala, Clojure and Groovy (thecodegeneral.wordpress.com)
- Advice on what to learn next (daniweb.com)
- DSLs with Groovy for Beginners (java.sys-con.com)
An example of the poor editing in O’Reilly’s “Programming Android”
OK, I don’t really want to sound like I am bashing this book – Programming Android: Java Programming for the New Generation of Mobile Devices – because, by its very nature, writing a technical book must be highly demanding in terms of accuracy and I see no signs of any mistakes – just what I think is poor editing. See if you agree…
So, the book is discussing how to serialize classes using Android’s Parcelable interface, and makes this, correct point about serializing an enum type:
“Be sure, though, to think about future changes to data when picking the serialized representation. Certainly it would have been much easier in this example to represent state as an int whose value was obtained by calling state.ordinal. Doing so, however, would make it much harder to maintain forward compatibility for the object. Suppose it becomes necessary at some point to add a new state … this trivial change makes new versions of the object completely incompatible with earlier versions.”
But then discussing de-serialization, the book states, without comment:
“The idiomatic way to do this is to read each piece of state from the Parcel in the exact same order it was written in writeToParcel (again, this is important), and then to call a constructor with the unmarshaled [sic] state.”
Now, technically, these passages are not in disagreement – but it is clearly the case that the de/serialization process is highly coupled with the data design – something that ought to be pointed out, especially if we are going to make a big deal of it on the serialization phase.
Related articles
- Making sense of Android’s complex development process (cartesianproduct.wordpress.com)
- Programming Android (O’Reilly) (i-programmer.info)
My first R program
Having used Groovy (which makes the scripting environment feel familiar) and some Scheme (via Structure and Interpretation of Computer Programs), R does feel completely alien, but it still feels like a steep learning curve.
But here’s my short script -
unpatched <- read.csv("~/unpatched.txt")
unpatchcons <- transform(unpatched, realm=realm*60 + reals)
attach(unpatchcons)
linelog<-lm(realm~size)
plot(size, realm, log="y")
abline(reg=linelog, untf=TRUE, col="blue",lty=3)
detach(unpatchcons)
And here’s the graph (of Linux kernel compile times) it generates – the blue line is obviously a very bad fit!
Related articles
- Number Representation in 2′s Power using PHP (demlaip.wordpress.com)
- Topcoder – MountainWalk (tuanwiki.wordpress.com)
- Web And Scripting Programming Language Job Trends – August 2011 (regulargeek.com)
- Introducing the lackeyml format (cartesianproduct.wordpress.com)
- Sicp (sicpebook.wordpress.com)
- New electronic version of SICP released ” SICP (sicpebook.wordpress.com)
- The price of syntactical sugar? (cartesianproduct.wordpress.com)
Writing more code to avoid writing any of the report?
I have managed to churn out 160 lines of working C today – which I think is quite good going, though, according to this, maybe I could have churned out 400 of C++ or even 960 of Perl (I love Perl but the mind boggles).
My program will tell you how pages pages are present or how many have been swapped (it has the ability to do a bit more too, but I have not exploited that even though it is essentially there) – you can fetch it from github here: valext (the name reflects the idea that this was going to be an extension to Valgrind but then I discovered/realised that was never going to work).
Anyway, what I now have to face up to is whether I am churning out code to avoid actually writing any of my MSc project report – I have a feeling that some of that is going on and think I need to start rereading Writing for Computer Science – which helped me greatly with the proposal.
Related articles
- Lint in Perl (technobeans.wordpress.com)
- Introducing the lackeyml format (cartesianproduct.wordpress.com)
- Perl System Command and Return Codes (gunnalag.wordpress.com)
Performance collapse in the Open JVM
Unfortunately, I do not have time to investigate this further myself, but others may do.
But yesterday I had a serious performance issue with the (open) JVM – though I was able to solve it with an algorithm change – swapping the problematic (integer) code for a lot of floating point maths: not the usual way to fix a performance issue but one that works.
My original code (in Groovy) appended many millions of integers to a list and then, once a loop was complete, calculated the average for the list (calculating the average working set size for a running process). When I was dealing with 2 – 3 million integers it worked well and performance, if anot exactly zipping along, was good. Push that up to 10 – 11 million and the first couple of times through the loop CPU utilisation dropped precepitatively (this was multithreaded – with runs through the loop operating in parallel) but the code was still visibly working but after that the intervals between loop completion grew to the point that the code seemed to have failed.
Even when I pre-allocated 0×1000000 items in what I now explicitly declared as an ArrayList the performance was little better – the first couple of iterations seemed a bit faster but performance then died.
I do not know what is going on – though excessive memory fragmentation perhaps coupled with poor garbage collection seem like the obvious answers: seems there is probably a brick wall for ArrayList size that sees whatever memory allocation algorithm operates inside the JVM fall over.
How did I fix it? Update the average in real time – in pseudo code below:
average = 0
previousInstructions = 0
loop [0, maxInstructions - 1)
{
currentInstructions++
if (change_in_working_set_size) {
average = ((average * previousInstructions) + workingSetSize() * (currentInstructions - previousInstructions))/currentInstructions
previousInstructions = currentInstructions
}
}
Like I say, floating point, but it works.
Related articles
- Twitter Shifting More Code to JVM, Citing Performance and Encapsulation (infoq.com)
- How Garbage Collection differs in the three big JVMs (dynatrace.com)
- Thomas Würthinger is a JVM hero (guidewiredevelopment.wordpress.com)
- Rhino is About to Get a Lot Faster (infoq.com)
- an experiment with generic arithmetic (blogs.sun.com)
- Not everything’s Groovy (usdlc.wordpress.com)
- Why OSCON Java? (radar.oreilly.com)
- Clojure: Lisp meets Java, with a side of Erlang (radar.oreilly.com)
Eighteen million lines of XML!
I have just finished a Groovy (the language) helper application to process the output of Valgrind‘s “lackey” package into an XML file for further analysis. More proof of the power of Groovy (a lanuage I am coming to like) – but also a sign that maybe I should get a more powerful computer – the XML file that came from the loading of xterm ran to over 18 million lines.
Related articles
- A Lightweight XML Parser – Rapid Xml (sfmlcoder.wordpress.com)
- Life.groovy now available (cartesianproduct.wordpress.com)
- No grammar constraints (DTD or XML schema) detected for the document. (argillander.wordpress.com)
- How can I do this in XSLT? (cartesianproduct.wordpress.com)
The “use” mix-in in Groovy
Here’s a code example:
Class StringMulti {
static def multi(def self, args) {
def z = self.toInteger()
args.each{
z *= it.toInteger()
}
return z
}
}
use (StringMulti) {
def y = ["343", "98", 98, 100]
def x
x = "1".multi y
println x
}
The “use” mix-in allows the static methods of the class specified in use to be mixed in to the classes – with the first parameter of the mix in replacing self in the example above.
Related articles
- Life.groovy now available (cartesianproduct.wordpress.com)
- Groovy 1.8 released (oio.de)
The joy of regex
A regex is, as any fule kno, a regular expression: a pretty fundamental concept in computing (the picture here shows an evaluation mechanism for one, albeit for a limited language) – computers, being deterministic, rely on “regular languages” for programs and so regexes are generally speaking widely used in parsing the languages.
But what has motivated me to write this blog (apart from the need to demote a story with the word “sexy” in the headline) is that they are the sort of thing that ought to be taught more widely than the low-quality stuff that seems to pass for ICT education in schools.
Because they link a computing fundamental to something that would be useful in everyday life. Think of it this way – you have a document which logs 20,000 library loans with a record tag and a 24 hr clock. You know about 500 of them are of interest to you because they record when, say, the system recorded “DVD borrowed” or “CD borrowed” (ie something like “DVD borrowed: 17:03:19″) – how can you extract just that?
A regex could do that in seconds – and while they can look complex, they are not that difficult to learn – at the risk of getting it all wrong (as I haven’t tested it) then the one for this could be (depending on the precise format):
(DVD|CD) borrowed:\t([0-1]?[0-9]|2[0-3])(:([0-5][0-9])){2}/)
Of course you have to have something with a regex engine available to crack that – though it’s actually quite a widely supported option in much free and proprietary software, it’s just nobody teaches you this hugely useful skill.
Related Articles
- The Joy of Regular Expressions [1] (blogs.sitepoint.com)
- Skype’s Crazy Regex Easter Egg (nyaruka.com)
- Regex generator: Give sample text, look for known patterns, gives a regex (txt2re.com)
- Benchmark of Regex Libraries (lh3lh3.users.sourceforge.net)
- Wrestling with Regular Expressions (blogs.sitepoint.com)



