F# Simple Twitter Update

A short while ago I posted some code for a C# twitter update.  I decided to move the same functionality / logic to F#.  Here is what I came up with.

   1:  namespace Server.Actions
   2:   
   3:  open System
   4:  open System.IO
   5:  open System.Net
   6:  open System.Text
   7:   
   8:  type public TwitterUpdate() = 
   9:   
  10:   //member variables
  11:   [<DefaultValue>] val mutable _body : string
  12:   [<DefaultValue>] val mutable _userName : string
  13:   [<DefaultValue>] val mutable _password : string
  14:   
  15:   //Properties
  16:   member this.Body with get() = this._body and set(value) = this._body <- value
  17:   member this.UserName with get() = this._userName and set(value) = this._userName <- value
  18:   member this.Password with get() = this._password and set(value) = this._password <- value
  19:   
  20:   //Methods
  21:   member this.Execute() = 
  22:      let login = String.Format("{0}:{1}", this._userName, this._password)
  23:      let creds = Convert.ToBase64String(Encoding.ASCII.GetBytes(login))
  24:      let tweet = Encoding.ASCII.GetBytes(String.Format("status={0}", this._body))
  25:      let request = WebRequest.Create("http://twitter.com/statuses/update.xml") :?> HttpWebRequest
  26:      
  27:      request.Method <- "POST"
  28:      request.ServicePoint.Expect100Continue <- false
  29:      request.Headers.Add("Authorization", String.Format("Basic {0}", creds))
  30:      request.ContentType <- "application/x-www-form-urlencoded"
  31:      request.ContentLength <- int64 tweet.Length
  32:      
  33:      let reqStream = request.GetRequestStream()
  34:      reqStream.Write(tweet, 0, tweet.Length)
  35:      reqStream.Close()
  36:   
  37:      let response = request.GetResponse() :?> HttpWebResponse
  38:   
  39:      match response.StatusCode with
  40:      | HttpStatusCode.OK -> true
  41:      | _ -> false

 

While the above seems to work, it feels to me like it is not taking advantage of some functional concepts.  Love to get some feedback as to how to make the above more “functional” in nature.  For example, I don’t like the mutable properties. 

Multiple Monitors

At my workplace .Net developers get pretty much the same equipment.

  • A decent Dell workstation / Desktop, mine is a Dell Precision 390. One dual core 2.40 GHz.
  • Eight GB RAM.
  • Windows 7 Enterprise 64-bit.
  • Two Dell 20.1 Monitors.

I'm happy with this.  The machine is about 3 years old but still runs with some decent speed. New developers are getting a Dell workstation with dual quad processors.

desktop

I just put in a request for myself and three other developers for an upgraded video card and two additional monitors, for a total of four monitors per person.  We suggested this card, BTW, mainly for the cost. 

The move from one monitor to two was fantastic (one might even say life (or work) changing) and truly did increase productivity. Now what about going from 2 monitors to 4?  I'm sure the change is not as dramatic as one to two, but I can't help but to think four monitors is better than two.  But if four is better than two, should we have asked for six?!?

Also what about mixing monitor types?  Right now my monitors are the older square type vs. wide-screen.  It's been rumored that we will be getting monitors out of current stock and they will be 22 inch wide-screens.  I understand this, recession and all.  2-20 inch square monitors with 2-22 inch wide-screen monitors...hmmmmm.  I'm thinking I'd rather get 2 additional 17 inch square monitors to put on each side of my 20's.

Also, a question was raised about the layout of four monitors. By default, my thought was I'll just put them all on my desk, kinda in a line. I've heard others say they want to stack them in a 2 x 2 square.

BTW, loving multi monitor support in Visual studio 2010!

I’d love some comments on your experience with one, two, four, or however many monitors from a developers perspective.

Playing with F#

Project Euler is a awesome site.   When working with a new language it can be tricky to find problems that need solving, that are more complex than "Hello World" and simpler than a full blown application. Project Euler gives use just that, cool and thought provoking problems that usually don't take days to solve. 

I've solved a number of questions with C# and some with Java.  BTW, I used Java because it had BigInteger support before .Net.

A couple weeks ago, back when winter had a firm grip on Columbus, OH, I began playing (researching) with F#.  I began with Problem #1 from Project Euler.  I started by looking at my solution in C#.

Here is my solution in C#.

   1:  using System;
   2:  using System.Collections.Generic;
   3:   
   4:  namespace Problem001
   5:  {
   6:      class Program
   7:      {
   8:          static void Main(string[] args)
   9:          {
  10:              List<int> values = new List<int>();
  11:   
  12:              for (int i = 1; i < 1000; i++)
  13:              {
  14:                  if (i % 3 == 0 || i % 5 == 0)
  15:                      values.Add(i);
  16:              }
  17:              int total = 0;
  18:   
  19:              values.ForEach(v => total += v);
  20:   
  21:              Console.WriteLine(total);
  22:              Console.ReadKey();
  23:          }
  24:      }
  25:  }

 

Now, after much tweaking and learning, here is my solution in F#.

 

   1:  open System
   2:   
   3:  let calc n = 
   4:      [1..n]
   5:      |> List.map (fun x -> if (x % 3 = 0 || x % 5 = 0) then x else 0)
   6:      |> List.sum
   7:   
   8:  let main() = 
   9:      calc 999
  10:      |> printfn "result = %d"
  11:      Console.ReadKey(true) |> ignore 
  12:   
  13:  main()

Just this little example highlights some cool things about F#.

  • Type inference. F# infers the type of a value.  In the C# code above we declare a number of variables, the list, and a couple ints.  F# does not require this, it infers the calc (a function) accepts a int and returns a int.
  • Great built in functionality for Lists.  List.map for example.

BTW, I don’t think I’m spilling the beans by giving away the code for Problem 1.  It by far is the easiest question, IMHO, solved by 92,000+ people.

Next I’ll look into writing a class library with F#.

C# Simple Twitter Update

For what it's worth a simple twitter update.

   1:  using System;
   2:  using System.IO;
   3:  using System.Net;
   4:  using System.Text;
   5:   
   6:  namespace Server.Actions
   7:  {
   8:      public class TwitterUpdate
   9:      {
  10:          public string Body { get; set; }
  11:          public string Login { get; set; }
  12:          public string Password { get; set; }
  13:   
  14:          public override void Execute()
  15:          {
  16:              try
  17:              {
  18:                  //encode user name and password 
  19:                  string creds = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", this.Login, this.Password)));
  20:   
  21:                  //encode tweet 
  22:                  byte[] tweet = Encoding.ASCII.GetBytes("status=" + this.Body);
  23:   
  24:                  //setup request 
  25:                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
  26:                  request.Method = "POST";
  27:                  request.ServicePoint.Expect100Continue = false;
  28:                  request.Headers.Add("Authorization", string.Format("Basic {0}", creds));
  29:                  request.ContentType = "application/x-www-form-urlencoded";
  30:                  request.ContentLength = tweet.Length;
  31:   
  32:                  //write to stream 
  33:                  Stream reqStream = request.GetRequestStream();
  34:                  reqStream.Write(tweet, 0, tweet.Length);
  35:                  reqStream.Close();
  36:   
  37:                  //check response 
  38:                  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  39:   
  40:                  //... 
  41:              }
  42:              catch (Exception e)
  43:              {
  44:                  //... 
  45:              }
  46:          }
  47:      }
  48:  }

 

BTW, this is my first blog post.  Nothing earth shattering, I admit, but I needed to figure out how to post formatted code.  In the past I’ve used Alex Gorbatchev’s Syntax Highlighter with great success, but here at GWB I couldn’t get it to work.

Windows Live Writer though, being a stand alone writer, worked with no problems.  For now, that’s what I’ll use.

«May»
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789