Friday, January 16, 2009

A forward pipe (“ |> ”) operator in Scala

One of the addictive things from F# is the |> operator defined by

but I've not yet found an equivalent in Scala, even though there are a ton of useful things given by the built-in APIs -- for example, I've not had to implement the usual abstraction to manage an (array, offset, length) combination, when there's Array.slice() there already.

So, just for fun...

with unit test

Refrigerator logic — I later realise that without actually adding any variance annotations the piped-to function can be one that takes any super-class of the value type. A little more thought suggests to me that type-inference does that automagically because it is able to reconcile all inputs into the types in that single expression, and will force the value to the supertype in order to make everything match up.

Thursday, January 08, 2009

Old-fashioned winter

After a dull near-freezing weekend, there was snow lying on Monday morning, in a day of sunshine and showers.

Tuesday, bright, sunny and sub-zero; so when we went on the weekly lunchtime walk, rather than staying on paved ways, we came to the bridlepath that has been too muddy all last year, and it was white with firm trodden-down snow. So we made an unexpected off-piste diversion.

Wednesday was back to about freezing, and dull, leaving lots of slush in shaded places; today, the same.

Links for 8-Jan

PoSh -- an effective foreach idiom.

PoSh -- Boots for WPF GUIs (inspired by Ruby Shoes).

C#3 and currying (series).

Ultimate List of Free Windows Software from Microsoft!

Monday, January 05, 2009

Timestamping F# assembly versions

Following up to the previous post, with this script Timestamp.fsx (unchanged by the Feb 2010 CTP, but enhanced to show the current year when it is no longer 2009):

suitably adjusted to taste in your main project folder, and a pre-build step of

cd ..\..
"C:\Program Files\FSharp-1.9.6.2\bin\fsi.exe" --exec Timestamp.fsx

or in the Feb 2010 CTP

cd ..\..
"C:\Program Files\FSharp-1.9.9.9\bin\fsi.exe" --exec Timestamp.fsx

you can get an automatically timestamped assembly, as per a C# 1.0.* version.

Note that using [< assembly: AssemblyVersion(\"1.0.*\") >] gives an assembly version of 0.0.0.0 in the DLL manifest, and no file version whatsoever.

You will have to bootstrap the process by creating a dummy AssemblyInfo.fs, and adding it to the project -- but of course not checking it in to source control or anything like that, since it is recreated every build.

Later: If the 4th facet exceeds 32k, it ends up set to zero, so change the factor to 3 in let revision= ((int fraction.TotalSeconds) / 3) for those end-of-UTC-day builds...

Links for 5-Jan

Just a small selection from the last week or so, and not even trying to keep up with PoSh v2CTP3 goodness -- I will be so pleased when that gets to go-live state!

AssemblyInfo.fs usage for F# DLLS and console executables. Note that the ability to default build and revision numbers as in [<assembly: AssemblyVersion("1.0.*")>] is not provided for you -- time-stamping has to be done externally e.g. by a pre-build step.

Generics in XAML.

Avoiding deadlocks when redirecting stdin/out/err for spawned processes in .Net code.

Speech recognition with .Net 3 and speech synthesis too!

Thursday, January 01, 2009

The first day of the rest of my life

After a good week last week, Karen's MS has really bitten her these last few days, both physically and mentally. On the bright side, it is the first real attack, rather than just slow progression, for a long while. Still, it looks like I'm going to have to pretty much everything about the house and more body-servant work while getting some formal assistance involved.

All this and work too, tomorrow.

2008 in media -- retrospect

Film : apart from the handful at the Film Festival, cinema seemed to be either the standard extruded product from the bowels of Hollywood, or English-miserablist. The only one I saw was when taking Karen to see Georgiana, and that I didn't rate.

Books : almost everything I read was software related -- for SF, see above about extruded product. My recommendation on the software front would be F# for Scientists, which did the basic teaching necessary to make the other books (Foundations of..., Expert...) somewhat more comprehensible. Foundations in particular is a poor introductory text as it hides the descriptions of syntax (as opposed to accidents of the examples) in solid paragraphs of text -- you need to have grasped the language before you can really get to grips with the book.

Anime : the year when Aniplex hired an outfit to send out DMCA notifications, where fansubs were slow, but some titles were for the first time officially simulsubbed or nearly so (of which only Strike Witches took my fancy). A fair number of OK-to-good series from the middle of the year, but nothing as outstanding as '07.

No real highlight, unless Mouryou no Hako delivers as it has begun, and only one that was so bad that I regretted having watched the one episode (Mnemosyne); though quite a lot that failed out of boredom (Spice & Wolf, Shigofumi Telepathy Girl Ran).

Other titles get separate mentions earlier, or get covered in spring and summer round-ups.

F# CTP and Silverlight 2 redux

Following up from here.

Units of measure are indeed useful, especially once one has twigged that the syntax needed to generalize over the units involved is like

and that you need to go the F# built-in math functions to have ones that are themselves so generalised.

So, here is the mostly essentially finished example applet (still needs to compute and draw the moon phases) with source bundled into the .xap file

Tuesday, December 30, 2008

Anime — Allison & Lillia

On a world that is not our Earth, two nations that share a continent have long been at odds, glaring across the separating river, and often breaking out into war. In a period akin to the inter-war years of Europe, these two nations have fallen into an uneasy armistice. Two young people, Wil and Allison, learn from an old man of a secret treasure that could end this seemingly eternal war.

That is a set-up that could last a whole series -- but that's just the first 4 episodes; and in the peace that follows there is skullduggery afoot seeking to reignite the flames of conflict. That provides for a series of escapades, including the involvement of our heroes with the royal family of the tiny statelet of Ixtova; then at the half-way mark, the torch passes to the next generation, Lillia, daughter of the original pair, and Trieze, incognito prince of Ixtova.

It's rather Tintin-esque in flavour; with firearms proving a threat but (except in one case of an old chap needing to exit the stage for plot reasons) never doing any harm. And there are magnificent old style train and aeroplanes.

There is a continual motif of the couples never realising each others' feelings, which one has come to expect from anime, but that never really gets in the way of a good Boys' Own style of adventure. The real weak point is the ending, which is, if you pardon the pun, a bit of a trainwreck -- the last episode is very poorly paced, and uneven in style.

Still, not bad in all -- the journey was fun, if a bit delayed.

Nice idea while it lasted…

To summarize -- if your Scala.Net project only uses .Net 1 features, then you can tweak the IL to use the .Net 2 libraries if you wish; but you can't build something that invokes full .Net 2 functionality as yet.

That means trying to build a Scala project against Silverlight assemblies is a non-starter (or at least not a trivial one) with the current build process. Using the out-of-the-box assemblies:

scalac-net.bat astroclock.scala -Xassem-path 
System.Core.dll;system.dll;System.Windows.Browser.dll;System.Windows.dll
12@(06 15 12 80 ad 02 12 80 b1 12 88 c8)
error: error while loading Control, type 'System.Windows.Controls.Control' is broken
(15@2 in (06 15 12 80 ad 02 12 80 b1 12 88 c8))
9@(06 15 12 80 b9 01 12 86 80)
error: error while loading FrameworkElement, type 'System.Windows.FrameworkElement' is broken
(15@2 in (06 15 12 80 b9 01 12 86 80))
11@(20 01 15 12 25 01 12 78 11 85 b4)
error: error while loading UIElement, type 'System.Windows.UIElement' is broken
(15@3 in (20 01 15 12 25 01 12 78 11 85 b4))
11@(06 15 12 80 ad 02 12 86 38 11 5c)
error: error while loading DependencyObject, type 'System.Windows.DependencyObject' is broken
(15@2 in (06 15 12 80 ad 02 12 86 38 11 5c))
9@(06 15 12 80 b9 01 12 86 10)
error: error while loading Application, type 'System.Windows.Application' is bro
ken
(15@2 in (06 15 12 80 b9 01 12 86 10))
astroclock.scala:90: error: System.Windows.Application does not have a constructor
class MyApp extends Application  {
                    ^
…

while rebuilding the assemblies against either .Net 2.0 or silverlight mscorlib and trying again gave the even more fundamental, but well known

scalac-net.bat astroclock.scala -Xassem-path 
System.Core.dll;system.dll;System.Windows.Browser.dll;System.Windows.dll
error: error while loading String, type 'System.String' is broken
(PEModule.getTypeDefOrRef(): TypeSpec)
one error found

So, to do this would mean upgrading the scala-net compiler to understand .Net 2.0 constructs.


Later:--

In constructs like (15@2 in (06 15 12 80 ad 02 12 80 b1 12 88 c8)), the 0x15 (at position 2) is the code ELEMENT_TYPE_GENERICINST, which is then followed by <an mdTypeDef metadata token> <argument Count> <arg1> ... <argN>. The type metadata turns out to be a type-spec; but the rest of it I've not yet unpicked. And then there'd be reverse-engineering the Scala MSIL decompiler...

The first culprit is DependencyObject (#22 in the typedef table), for a private field; but there are dependency properties on UIElement which suffer the same.

Monday, December 29, 2008

Scala-light?

I ordered a copy of Programming in Scala last Sunday -- and to my surprise, it arrived on my doorstep this morning. This, of course, deflected me from my F# and Silverlight activity today.

I think I shall have to try the experiment of putting Scala.Net into Silverlight, just as part of my usual "can I wedge this into that" play.

I would start with the recipe as before, in the library form; but to retro-fit a link against the Silverlight mscorlib which has the signature

rather than the mainline .Net 2.0 assembly.

What fun toys these are!