Showing posts with label f#. Show all posts
Showing posts with label f#. Show all posts
Tuesday, 5 March 2013
Span part 2 - this time the file is XML
Oh ho! It transpires that the Australian Stock Exchange (ASX) publishes their Span data in XML. I haven't found the schema description but, because XML is human-readable, I was able to build a parser. This time I did it using discriminated unions. Much better, although disappointingly more difficult to query after construction. That merits further investigation. (edit: URL at ASX which explains what Span is all about: ASX) and here is a link to the input files used by the parser: Risk parameter files
Go and check out Span2 on Google Code
Aha! I've found the schema definition. It's here
Span (or SPAN?) - they use it for margin calculations but I need to build a parser
London Clearing House provide daily Span files at LCH which list, as far as I can determine, a large number of contracts and the inter-contract spreads. These inter-contract spreads may apply to 2, 3, 4 or more particular contracts and they are applied in a specific order. Therefore, if the portfolio contains contracts AA, BB and CC and there is a listed spread between AA and BB and another between BB and CC then we take the one with the highest priority and calculate margin for the remaining contract by itself. For example, if AA - BB has priority 1 and BB - CC has priority 2 we will be left with CC in our portfolio on which we must calculate the full amount margin. AA and BB are mutually (although not necessarily completely) offsetting.
The Span files given at the LCH URL above are fixed-format. Ewwww! When was the last time you had to build a parser for a fixed-format file? Especially one containing hierarchical data. Moreover, when I started this the specific column format of the file was unclear and required reverse-engineering. Hmm, nice. Luckily, before I had mistaken too much error as progress, I found a specification for the format. Phew!
The Span v4 file format specification
There it is. What a beauty! It seems that the format specification is incomplete. That is, it is sufficient for files received from LCH but for the general case, there is a fuller description. This format is specified at the CME and this is it.
So I set about creating a parser for this little gem. Oh, and I should mention that I did it in F#.
Check it out at Google Code in the Span directory. Yes, I know it's in the wrong place but it'll do. So go and look at it. Note that the fixed-format parser does not currently support the fuller format from CME, just the one supplied by LCH.
Lovely. Now, assuming you've made it this far you'll be thinking that that code is not very nice. Well, you'd be right! I'm not afraid to admit that it's pretty evil stuff. In my defense, however, I would like to say that it was done quite quickly with a view to being rewritten later and that it's been quite a while since I wrote any F#.
Ho hum ...
Tuesday, 25 October 2011
F# - Active patterns
I like active patterns. I have yet to explore their possibilities completely but one pattern which crops up all the time in our code is checking whether a string is empty and acting accordingly.
In C# we have
Clearly, the
We could, of course, transliterate this directly into F# and that is the first step in the conversion process. Thus we have
So what? I hear you say. Well, this is a step in the right direction. Functional programmers prefer pattern matching to indiscriminate use of the
First, a single-valued active pattern:
which could be used in this manner:
I think the syntax for active patterns leaves a little to be desired so let me explain what is happening here. Clearly the match is being performed on
A multi-valued pattern is similar. For example:
Notice that this pattern looks much more like a discriminated union. Also, whilst it is not necessary to return a value in the 'constructor', if you will, in either case, we do so here for illustrative purposes. This pattern could be used as follows:
Thus
Partial active patterns are a slight change again:
Note that I have changed the specified portion of the pattern to
The partial pattern is used similarly:
As you can see, the construct is powerful and I haven't demonstrated how to pass additional parameters or match multiple values. Check Wikibooks for further information and then try the broader t'internet.
In C# we have
if(!String.IsNullOrEmpty(myString)) { doSomethingWithTheString(myString); } else { doSomethingInNullCase(); }
Clearly, the
doSomething...()methods will usually be more than a simple procedure, often involving local variables and transformations thereof, but the essence of the pattern is captured above.
We could, of course, transliterate this directly into F# and that is the first step in the conversion process. Thus we have
if(not <| String.IsNullOrEmpty myString) then doSomethingWithTheString(myString) else doSomethingInNullCase()
So what? I hear you say. Well, this is a step in the right direction. Functional programmers prefer pattern matching to indiscriminate use of the
ifstatement though. The question, therefore, is what pattern can we match to achieve the end and how. Crucially, in F# one cannot match against a general function. Instead one uses what is known as an Active Pattern. There are three forms of Active Patterns: single-valued, partial and multi-valued. The names are mine but they are similar to the official names. I think of 'partial' active patterns as slightly different from the other two.
First, a single-valued active pattern:
let (|EmptyString|) str = String.IsNullOrEmpty str
which could be used in this manner:
match myString with | EmptyString true -> "Empty" | _ -> "Not empty"
I think the syntax for active patterns leaves a little to be desired so let me explain what is happening here. Clearly the match is being performed on
myString. The match criterion first considered is
EmptyString. All bar the final parameter (in this case, there are no additional parameters in the match criterion) are evaluated in the context of
myString. The final parameter in the matching pattern is compared with the return value of the active pattern. In other words, the criterion is
EmptyString myStringwhich returns a
boolagainst which we match
true.
A multi-valued pattern is similar. For example:
let (|EmptyString|String|) str = if String.IsNullOrEmpty str then EmptyString else String(str)
Notice that this pattern looks much more like a discriminated union. Also, whilst it is not necessary to return a value in the 'constructor', if you will, in either case, we do so here for illustrative purposes. This pattern could be used as follows:
match String.Format("Is {0} empty?", myString) with | EmptyString -> "It's empty" | String str -> str
Thus
strholds the string that was successfully matched.
Partial active patterns are a slight change again:
let (|NotEmpty|_|) s = if String.IsNullOrEmpty s then None else Some(s)
Note that I have changed the specified portion of the pattern to
NotEmptyso that we can return the string that was matched successfully. Note also that this pattern returns an
optiontype where the multi-valued pattern returns a
Choice.
The partial pattern is used similarly:
match String.Format("Is {0} empty?", myString) with | NotEmpty str -> str | _ -> "It's empty"
As you can see, the construct is powerful and I haven't demonstrated how to pass additional parameters or match multiple values. Check Wikibooks for further information and then try the broader t'internet.
Wednesday, 12 October 2011
Creating a class with multiple constructors in F#
Using the implicit type syntax couldn't be easier. For example:
Easy! But class A only has one constructor. Suppose we want another? The obvious guess would be
but this doesn't work for a number of reasons. You can't have
Whew! Complicated! And repetitive, potentially! It is possible to call functions in the
It's more fiddly in the presence of inheritance unfortunately. In this case the record initialiser has to include inherit Cbase(a).
Here's one I've used in production code, anonymised...
type A (a,b) as this =
let c = "sum"
do printfn "The sum is " (a+b)
Easy! But class A only has one constructor. Suppose we want another? The obvious guess would be
type B (a,b) as this =
let c = "sum"
do printfn "The sum is " (a+b)
new (d) = B(a,0)
but this doesn't work for a number of reasons. You can't have
let
or do
sections in your type if you have multiple constructors. So this means that a
, b
and c
have to be defined in the new
function. This means, of course, that you need to use explicit class syntax. The action can then follow the initialisation in a then
clause.
type C =
val a :float
val b :int
val c :string
new (_a,_b) = { a=_a; b=_b; c="sum"} then printfn "The sum is " (a+(float b))
new (_a) = { a=_a; b=0; (* Yes, you have to initialise all the class members *) c="sum"} then printfn "The sum is " (a+(float b))
Whew! Complicated! And repetitive, potentially! It is possible to call functions in the
then
clause but you still have to initialise all the members in the record clause.It's more fiddly in the presence of inheritance unfortunately. In this case the record initialiser has to include inherit Cbase(a).
Here's one I've used in production code, anonymised...
type drv = class
inherit bse
val _parser : Parser
val _columnExtractors : List
new (configuration:ExpConfig ) as x =
{inherit bse(configuration); _parser = new Parser(configuration); _columnExtractors = new List()}
then x.Init()
new (configuration:ExpConfig, parser:Parser) as this =
{inherit bse(configuration); _parser = parser; _columnExtractors = new List()}
then this.Init()
member private x.Init() =
printfn "blah blah blah"
end
Monday, 3 October 2011
F# - Abstract class
It's possible to create an abstract class, but it's not obvious how to do it. You need to create a type, as normal. Then place an abstract method declaration within. At this point, though, the compiler still grumbles. Now you have to tag the type with
First attempt:
Now apply the annotation and, hey presto, the compiler responds with:
But why the compiler can't work it out for itself I don't know.
[<AbstractClass>]
.First attempt:
> type C =
inherit Exception
new () = {inherit Exception()}
abstract print : unit -> unit
;;
type C =
-----^
stdin(14,6): error FS0365: No implementation was given for 'abstract member C.print : unit -> unit'
Now apply the annotation and, hey presto, the compiler responds with:
type C =
class
inherit Exception
new : unit -> C
abstract member print : unit -> unit
end
But why the compiler can't work it out for itself I don't know.
Overloaded constructors and inheritance - F#
I've been struggling vainly to create my own Exception class but the obvious fails to compile:
with the following error message:
After some head-scratching, I guessed that the problem might be the default constructor, and lo, this works.
It's not clear to me why I need to explicitly define the default constructor, but the compiler has stopped complaining now.
type MyException1 () =
inherit Exception ()
new (msg:string) = {inherit Exception(msg)}
with the following error message:
new (msg:string) = {inherit Exception(msg)} -----------------------^^^^^^^^^^^^^^^^^^^^^^^^ stdin(9,24): error FS0762: Constructors for the type 'MyException1' must directly or indirectly call its implicit object constructor. Use a call to the implicit object constructor instead of a record expression.
After some head-scratching, I guessed that the problem might be the default constructor, and lo, this works.
type MyException =
inherit Exception
new () = {inherit Exception()}
new (msg:string) = {inherit Exception(msg)}
It's not clear to me why I need to explicitly define the default constructor, but the compiler has stopped complaining now.
Subscribe to:
Posts (Atom)