Pixata Custom Controls
For Lightswitch

Recent Posts

Popular tags (# posts in brackets)

Anonymous types (3) ASP.NET (5) C# (3) C# tricks and tips (2) Computers (6) Design patterns (3) DomainDataSource (3) Dynamic data (4) Entity framework (3) Entity model framework (5) F# (3) LightSwitch (12) Linq (6) Microsoft (2) MVP (2) MVVM (2) Project Euler (2) RIA services (5) Silverlight (2) SQL Server (4) Unit testing (4) Visual Studio (7) WCF (3) WPF (3)

Gratuitous link to StackExchange

Archives


Categories


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Actually, as I'm self-employed, I guess that means that any views I expressed here aren't my own. That's confusing!


Acknowledgments

Theme modified from one by Tom Watts
C#/F# code styling by Manoli (for posts pre-2016) and Google code prettify (for post from Jan 2016 and beyond)


My rambling thoughts on exploring the .NET framework and related technologies

# Sunday, 07 February 2016

Up until now, I have been using Manoli’s code formatter to add code to this site. I’ve had a couple of issues with this, mainly that the line numbers it includes are part of the text, so if you copy the code and paste it into Visual Studio, LinqPad, etc, you get the line numbers as well.

I recently discovered that Google have a code formatter, which seems to do the job nicely, doesn’t require you to paste the code into a formatter before adding it to the site, and uses an order list HTML tag to create the line numbers. It also has a few extra themes, meaning I can colour the code to look more like the dark theme I use in VS.

Here is some C# formatted with the new method…

public long MaxProduct (string s, int n) {
  return Enumerable.Range (0, s.Length - n + 1)
      .Select (i => s.Substring (i, n))
      .Max (s1 => s1.ToCharArray().Aggregate (1, (long a, char c) => a * (c - '0')));
}

Hmm, judging by the preview in Live Writer, it looks pretty awful! Shame, it looked fine in a plain HTML file. Have to post it to the blog and see how it looks there.

Edit: Looked OK. Shame I can't read the code when editing! Let's try some F# and see...

// Find the sum of all primes below two million
let primes max = 
    let array = new BitArray(max, true);
    let lastp = Math.Sqrt(float max) |> int
    for p in 2..lastp+1 do
        if array.Get(p) then
            for pm in p*2..p..max-1 do
                array.Set(pm, false);
    seq { for i in 2..max-1 do if array.Get(i) then yield i }

primes 2000000 |> Seq.map (fun n -> bigint n) |> Seq.sum

OK, I’m convinced!

Sunday, 07 February 2016 17:00:00 (GMT Standard Time, UTC+00:00)
Comments are closed.