Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Comments

evildictaitor evildictait​or Devil's advocate
  • Erik Meijer: Functional Programming

    Brian is a genius, so you might find my example more simple to understand:

    static int doesSideEffects(string text, int a, int b){
      Console.Write(text);
      return a + b;
    }

    static void Main(){
      int result1, result2;

     // statement 1:
      result1 = doesSideEffects("first one!", 1, 2);
     // statement 2:
      result2 = doesSideEffects("second one!", 3, 4);

      Console.Write(result1 + result2);
    }


    Can we shuffle the statement 1 and 2? Can we do them at the same time? The answer is no, because we must guarrantee that the first statement's Console.Write() happens before the second one.

    static int noEffects(string text, int a, int b){
      Console.Write(text);
      return a + b;
    }

    static void Main(){
      int result1, result2;

     // statement 1:
      result1 = noEffects("first one!", 1, 2);
     // statement 2:
      result2 = noEffects("second one!", 3, 4);

      Console.Write(result1 + result2);
    }


    Can we shuffle the statement 1 and 2? Can we do them at the same time? The answer is yes, because result2 does not rely on result1, and the function call has no sideeffects, so it doesn't matter if result2 is computed before result1.



    Some things that a pure (or shall we say a function which is constant with respect to it's arguments) function have that a side-effecting function doesn't have include:

    Let A and B be one or more statements, which contain no side-effects.
    Let AB denote the instruction or set of instructions A followed by the instructions in B.

    1. AB has no side-effects.
    2. If B does not depend on A, then AB = BA
    3. If A is a function that takes arguments c1 ... cn, and c1... cn are known at the time of compilation, then the result of A can be fully determined at compile-time (i.e. never needs to be computed in the program, which makes your program faster).
    4. If B does not depend on A, then A and B can be executed on different threads at the same time.
    5. If A1 ... An are independent, non-side-efffecting blocks, then loops such as

    int[] arr = new int[10000];
    for(int i=0;i<arr.Length;i++){
      arr[i] = noEffectButLongFunction(i);
    }

    Can be split across a number of threads without penalty.

    6. If A is a constructor which is fully disposed before it is ever passed to, or assigned from a side-effecting function, it can be at least partly optimised away, e.g.

    class ComplexNumer {
      int re, im;
      [NoEffects]
      ComplexNumer(int re, int im){
        this.re = re; this.im = im;
      }
      [NoEffects]
      public static operator ComplexNumber +(ComplexNumber a, ComplexNumber b){
        return new ComplexNumber(a.re + b.re, a.im + b.im);
      }

      [NoEffects]
      public double Magnitude {
        // sqrt has no side-effects.
        return Math.Sqrt(re * re + im*im);
      }
    }

    public int Main(){
      double mag = (new ComplexNumber(1,0) + new ComplexNumber(4,3)).Magnitude;
    }

    What optimisations can we do to Main?

    Step1: Let's make everything explicit:

    int mag = Operator+( new ComplexNumber(1,0), new ComplexNumber(4, 3) );

    but because new ComplexNumber() is constant with respect to its arguments, we know at compile time the value of new ComplexNumber(1,0) and ComplexNumber(4,3).

    but Operator+ is constant with respect to its arguments, so we know that it's value can be fully determined at compile time, so we evaluate it to get

    new ComplexNumber(5, 3).Magnitude.

    But .Magnitude is constant with respect to it's arguments, and we know ComplexNumber(5,3) at compile time, so we can evaluate it to be Math.Sqrt(34). But Math.Sqrt is c wrt. args, so we can even evaluate it at compile time!

    So ultimately we have reduced a potentially costly expression like
    public int Main(){
      double mag = (new ComplexNumber(1,0) + new ComplexNumber(4,3)).Magnitude;
    }
    to
    public int Main(){
      double mag = 5.830952;
    }

    entirely at compile time, and thus your program will run faster, and more errors can be caught before you ever ship the program to your customers.

    And less errors for customers and more (provably correct) parrallized code means that your programs will be more reliable and faster. So to sum up, pure functions and functional programming techniques leads to more money and prestige for you. Tongue Out
  • Wandering Around London's Science Museum with Some Niners

    jason818_253.33 wrote:
    

    The brilliants of making a machine that does polynomial functions is fascinating! I can’t begin to imagine how one goes about making something like that. Even when I see it it’s hard to believe. I admittedly paused just to get a closer look at that human brain.



    How do you "do" a polynomial function? Do you evaluate it's zeros, or evaluate it numerically at a point, or what?
  • Happy Holidays Niners

    What, no harmony? I think you need some C# developers there to add layers of harmony, and get someone in the compilers team to back it up with bass; oh, and doesn't Microsoft have an orchestra? Tongue Out

    Good job! I particularly enjoyed the interview at the beginning! (We need more VB interviews Charles)
  • Brian Beckman Does Higher Algebra with Visual Basic

    Since when does linear algebra and vectorspaces count as higher algebra? They're first year stuff Tongue Out
  • IE 8: On the Path to Web Standards Compliance - ACID 2 Test Pass Complete

    The truly amazing thing is the comments on digg about Internet Explorer have practically no Microsoft or IE-bashing for this post.

    Seems that this was a good job well done by MSFT.
  • Niners on 9: Littleguru, Zeus and JasonOlson

    Yeah. I am absolutely 100% with Jason on the Going Deep videos. They should make one a day, and make them even more technical.
  • Choosing the Right LINQ Model for Your Application

    What? No download button?

    How woode.
  • Pat Brenner: New Updates to MFC in Visual Studio 2008

    Boomport wrote:
    The whole point of .NET is choosing the language of your choice.  I use C++ as little as possible, but I don't want them taking away C#, or VB.NET.  I'm sure the C++ guys feel the same.


    C# and C++ should like together, like man(aged) and wife (I'll get lynched by politically-correct people if I said native wife).

    There are things which C# can do which C++ struggles with. C# puts code to product quickly and well, it has an awesome IDE and it's easy to reason about automatically.

    There are things which C++ can do which C# struggles with. C++ uses vast, vast libraries the like of which C# has never seen, and has the rich full-bodied glory that only a language committee and nearly two decades of industry trial-and-error has given. C++ can interface easilly with the kernel, and works well on non-Microsoft OSes. If your codebase is primarilly C++ you also can't afford to move to C#.

    Sure you can make bad code in C++ with buffer overruns, but you can make nasty mistakes in C# as well, but not understanding the deletion order of the GC, or by playing with pointers through PInvoke in a way that you shouldn't.

    Point is that C++ and C# shouldn't be opposites. F# showed us that Haskell-like languages and C++-like languages can co-exist and can complement each other, rather than fighting in silly "my language is better than yours" wars. Hopefully someday we shall have a G#++, which combines the power, flexibility and interoperability of C++ with the language-independence, managed-ness and nice IDE-ness of C#.
  • Pat Brenner: New Updates to MFC in Visual Studio 2008

    SecretSoftware wrote:
    
    I Yearn for the day when Visual Studio will not have C++ in it anymore.

    The Visual Studio on this machine doesn't have C++ on it because I decided not to install it when I installed VS 2008. (My VS2005 has C++ on it).

    SecretSoftware wrote:
    
    OSes will be using managed code in the future.

    Way to show how little you know about OSes.

    SecretSoftware wrote:
    
    Games too.

    And games.

    SecretSoftware wrote:
    
    So lets just slowly get rid of C++ and focus on C#.

    C++ will stop being used when it is no longer useful. There is no single group that can force people to stop using C++, and it still remains the language of choice for most big applications. I might venture to point out that Vista, MS Office, Visual Studio and IE/Firefox are all written in C/C++, so it's a long time off a "big switch" to .NET languages.

    SecretSoftware wrote:
    
    C# is the future, and C++ brings back bad memories , of sleepless nights.

    Anyone for year of the C# desktop?

    SecretSoftware wrote:
    
    Customers who still use C++ should upgrade to C#. And that is that.

    Anyone who still uses C++ or C# should upgrade to Lisp/Perl and that is that. What gives you the right to overrule what someone else has deemed a good language for your product when you clearly know less about their product than you do.

    SecretSoftware wrote:
    
    Any one who likes C++ should just stay with VS 6. or previous builds of vs.

    And while they're at it, Steve Ballmer should state that Microsoft Office is now going to support documents written only in capitals because it gets the point across, and that IE8 will drop support for CSS and images because they "arn't useful". Seriously, stop being stupid.
  • Build UAC aware apps with VS2008

    rsclient wrote:
    It would be nice to know if an app can detect that it's elevated, or handle the user not running it. 


    public static class UAC {
      public static bool AmElevated(){
        return
    new WindowsPrincipal(WindowsIdentity.GetCurrent())
           .IsInRole(WindowsBuiltInRole.Administrator);

      }
      public static void Elevate(){
        if(AmElevated()) return;
       
    ShellExecute(IntPtr.Zero, "runas\0",
           Application.ExecutablePath + "\0", "\0", "\0", 1);

      }
    }

    ...

    public class Program {
      public static void Main(){
        if(!UAC.AmElevated()){
          MessageBox.Show("I'm not elevated, asking you to elevate...");
          UAC.Elevate();
        }else{
          MessageBox.Show("I'm elevated, yays!");
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
      }
    }