Anything you can do ...
@ 7sharp9 · Sunday, Jun 1, 2014 · 3 minute read · Update at Jun 1, 2014

For any of you that are aware of the newly updated Xamarin Web site, you may have seen the following:

Objective-C was ahead of its time 30 years ago. C# is ahead of its time today. Anything you can do in Objective-C or Java, you can do in C# with Xamarin—usually more succinctly and with fewer bugs.

What is also true is that F# is way ahead of its time, and you can produce even more succinct code with even fewer bugs than C#!

Take the code snippets from that page.

First up the Objective C version:

@interface Person : NSObject
@property (strong, nonatomic) NSString *name;
@end

@implementation Person
- (id)initWithName:(NSString *)name {
    self = [super init];
    if (self) {
        self.name = name;
    }
    return self;
}

+ (NSArray *)getNames
{
    NSArray *people = @[
      [[Person alloc] initWithName:@"David"],
      [[Person alloc] initWithName:@"Vinicius"],
      [[Person alloc] initWithName:@"Serena"],
    ];
    NSMutableArray *names = [NSMutableArray array];
    for (Person *person in people) {
        [names addObject:person.name];
    }
    return names;
}
@end

Heres the C# version:

class Person : NSObject {
  public string Name { get; set; }
  
  public static string[] GetNames() {
    var people = new[] {
        new Person { Name="David" },
        new Person { Name="Vinicius" },
        new Person { Name="Serena" },
    };
    return people.Select(person => person.Name).ToArray();
  }
}

And finally the F# version:

type Person() =
   inherit NSObject()
   member val Name = "" with get, set
   static member GetNames() =
      [| new Person(Name="David")
         new Person(Name="Vinicius")
         new Person(Name="Serena") |]
      |> Array.map(fun person -> person.Name)

You can see the F# version is doing exactly the same, although we are using the map function from the Array module rather than the Linq Select extension method.

Its not all about the lines of code though, using F# gives you all many advantages:

  • Using the type system to make sure the code is behaving how you expect before you even compile.
  • Pattern matching in F# is amazing! It can vastly simplify complex control logic, add Active patterns to that and you are ready to take on the world!
  • Problems are approached from a functional perspective which often leads to succinct functions that are easy to reason about, test, and compose.
  • F# emphasizes immutability and functional composition rather than inheritance, again this boils down to simplicity.
  • Features like Type providers can vastly simplify how you deal with data within your application, making access to data really easy and intuitive.

Theres are many areas that F# can really help productivity during development. I hope to write a few more short posts to really bring attention to these. I use F# all the time and often forget how awesome it is until I go back to another language thats missing those features.

Until next time!

Dave Thomas
7sharp9's blog
A cup of tea would restore my normality. - Douglas Adams
agents ast async concurrency csharp elixir elm fable fsharp functional-programming fuse games ios javascript mac mailboxprocessor metaprogramming microsoft mono monodevelop monogame monotouch myriad performance quotations running sockets threading tpl tpl-dataflow typeproviders videos xamarin youtube

© 2011 - 2020 7sharp9

Powered by Hugo with theme Dream.

About Me

Hi, my name is Dave Thomas and Im a software engineer from the Uk. I work primarily in F# but also like to tinker with pretty much any interesting language. Im drawn to more functional languages like F#, Swift and Rust.

Have questions or suggestions? Feel free to ask me on Twitter.

Thanks for reading!