InfoQ

News

Borrowing Functional APIs from F#

Posted by Jonathan Allen on Nov 21, 2007 06:42 AM

Community
.NET
Topics
Programming
Tags
Functional Programming ,
C# ,
Visual Basic.NET

The Common Language Specification ensures that any conforming .NET language can access libraries created by any other language. This means imperative languages like VB and C# can call functional libraries created primarily for F#. In fact, many can be converted directly into C# code.

Dustin Campbell illustrates this by showing how even simple C# expressions can be reduced in length. He starts with this code,

int[] a = new int[20];
for (int x = 0; x < a.Length; x++)
a[x] = x + 1;

Instead of a specific code recipe, this F# code says (in a more declarative fashion), "create an array of 20 elements, and use this function to initialize each element." An interesting feature of the F# version is that the type of the array is never declared. Because the compiler can infer that the result of the passed function (fun x -> x + 1) will be an int, "a" must be an int array.

To me, this code is beautiful. In addition, it is declarative instead of imperative; it describes what should be done but doesn't dictate exactly how it should be done. When I see such elegant code, I immediately start trying to figure out which of its aspects could be used to improve the code in my daily C# work.

Dustin goes on to show a C# function that allows for this syntax.

var a = ArrayEx.Create(20, x => x + 1);

It should be noted that these techniques work equally well in VB.

1 comment

Reply

No F# code in this post by Neil Bartlett Posted Nov 21, 2007 3:57 PM
  1. Back to top

    No F# code in this post

    Nov 21, 2007 3:57 PM by Neil Bartlett

    You've quoted the C# code where I think you intended to quote the F# code.

Exclusive Content

Book Except and Interview : Aptana RadRails, An IDE for Rails Development

Aptana RadRails: An IDE for Rails Development by Javier Ramírez discusses the latest Aptana RadRails IDE, a development environment for creating Ruby on Rails applications.

Fast Bytecodes for Funny Languages

Cliff Click discusses how to optimize generated bytecode for running on the JVM. Click analyzes and reports on several JVM languages and shows several places where they could increase performance.

Scott Ambler On Agile’s Present and Future

Scott Ambler, Practice Lead for Agile Development at IBM, speaks on the current status of the Agile community and practices having a look at the perspective of the Agile’s future.

Manager's Introduction to Test-Driven Development

Dave Nicolette and Karl Scotland try to introduce non-technical managers to one of the most popular Agile development techniques: Test-Driven Development (TDD).

Structured Event Streaming with Smooks

Smooks is best known for its transformation capabilities, but in this article Tom Fennelly describes how you can also use it for structured event streaming.

How to Work With Business Leaders to Manage Architectural Change

Successful architectures evolve over time to meet changing business requirements. Luke Hohmann presents how to collaborate with key members of your business to manage architectural changes.

Colors and the UI

In this article, Dr. Tobias Komischke explains how colors used in a GUI can influence our interaction with a computer and offers advice on using the appropriate colors for the interface.

Building your next service with the Atom Publishing Protocol

In his presentation, recorded at QCon San Francisco, MuleSource architect Dan Diephouse explores ways to use the Atom Publishing Protocol (AtomPub) when building services in a RESTful way.