Friday, September 27, 2013

Implementations of Memoization in F#


Today let’s talk about memoization.

This is the definition of memoization in Wikipedia:

“memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs.”

To put it simple, memoization is a technique by remembering the previous result of the function results. This implies that memoization is best to use as recursive functions, because it needs to call itself again!

The need to store values means that it needs mechanism to store result value.

To implement this mechanism, of course we need another helper variable to store the result.

Don Syme, the original creator of F# has this simple sample of memoization on his blog.

Here’s his starting sample:

donsyme_memoization00

There’s no further explanation about how the code above works. We’ll try to dive it together.

In his first sample, type checking on x (as x in the “fun x –>”) will always be performed.

He used System.Collections.Generic.Dictionary of <_,_> to map x as the key and fill the value with the result of computation of f x.

This is the power of type inference in action, so we can expressively map Dictionary<_,_> to have two kinds of generic type without naming it.

F# then will infer the first “_” as the key, and the second “_” as the type of the value.

The signature of “memoize f” is “('a -> 'b) -> ('a -> 'b)”.

The Dictionary used then will be used as a cache to store result value.

He further update the memoization sample.

This is the update using unchecked type:

memoize_code01

And now using single mutable reference of Map:

memoixe02

The code above is more functional because it uses immutable F# Map instead of mutable System.Collections.Generic.Dictionary and also easier to understand, though using Unchecked is cleaner in some sense of C# perspective.

Tuesday, September 10, 2013

Another reasons to use F#: Why use F#

Wow! I really missed this gem!

There is a website dedicated for explaining why using F# is fun and also profitable.

The link is: http://fsharpforfunandprofit.com/why-use-fsharp/ and it’s very informational!

There you can find a series of 30 parts of defining “Why use F#” in a very short but concise.

The main reasons of Why use F# are:

  • Conciseness
  • Convenience
  • Correctness
  • Concurrency
  • Completeness

The nice additional feature is, they have syntax highlight!

website_whyuse_fsharp

Definitely one of the best F# resource to keep! Smile

Tuesday, August 20, 2013

KASKUS Programmer forum gathering on 18th August 2013

Hi all!

Now I got invited by Kaskus programmer forum participants to meet at gathering session at FX Plaza on 18th August, 2013. In this gathering, there were more audiences than previous gathering.

For those wondering what Kaskus is, Kaskus is simply a free social gathering of those that has the same interest. It is generally more relaxed as there’s no organizational chapter whatsoever. It has governed by nobody but the forum is strictly moderated.

My main focus at Kaskus is the Programmer forum, and we usually have gathering for every 6 months.

I got the chance to demo what is lazy evaluation in C#/VB and the proof of the magic behind it.

Lazy evaluation in form of yield iterator in C# and VB (although VB has it since Visual Studio 2012). Using yield will make the code that use the iterator will be executed later, rather than immediately. This can be regarded as on demand but yield has more machinery. It can be seen as having a state machine to keep the state that it will run only as needed, especially when it comes to iterate an IEnumerable.

This is the code in Visual Studio 2010:

csharp_select_yield_thumb_5931E421

Just put the breakpoint at the line in the foreach in the Main method and run it.

csharp_yield_breakpoint_thumb_35B53FBC

At first breakpoint, the code Select above it is not yet executed. After going stepping to “in” before procnames, then the Select method above will be executed.

You, my dear reader, might wonder what select is. Select comes interpreted from an extension method of Select (look at this keyword).

csharp_extensionmethod_thumb_5FA51817

The code above is basically also a demo of how we can implement our own Select as select in LINQ to object. In fact, I have no reference on System.Linq at all!

By the way, the untranslated code of calling Select is:

csharp_translated_select_thumb_3FD5D8CF

Now I can argue that using LINQ is better, isn’t it?

Not just .NET, we were also having discussion on Java, PHP, and many things about programming as well.

Who are those attending this gathering?

Before that, here’s the picture of all attendances:

kopdar_kaskus_forumprogramer_thumb_6FF06A03

For more information on all of the gathering, please visit this thread on KASKUS forum programmers: http://www.kaskus.co.id/thread/51fb2a208327cfde1400000a/kopdar-programmer/

Thanks to Felix (cpuclear2) and Habib (bluething) for the pictures!

Monday, July 15, 2013

Current state and product lifecycle of Visual Studio in July 2013

Hi, .NET developers!

In Microsoft terms, the no longer supported means the product has gone passing through mainstream support period. The Extended support period means that there's additional charge for additional support beyond mainstream support.

We shall put our focus on at least Visual Studio 2010 Professional or Ultimate, because Visual Studio 2008 has passed its mainstream support phase.

Visual Studio 2012 has Update 3 (consider it as "Service Pack 3" cumulative update).

Visual Studio 2013 is going to be released soon, the date is October 18 2013, the same date of the worldwide launch of Windows 8.1.

Visual Studio 2005 is no longer supported, although extended support is still available until 2016. Visual Studio 2008 is coupled with .NET 3.5 release, and both of these product has the same mainstream support end date.

For Visual Studio 2005 period is below:

http://support.microsoft.com/lifecycle/?p1=10441

vs2005_support_thumb_6873FCE8

Visual Studio 2005 SP1 has brought some life to Visual Studio 2005, but the support for SP1 is given after one year of mainstream support: (although Microsoft only said "end of product's support lifecycle")

http://support.microsoft.com/lifecycle/?p1=3041

vs2005_sp1_support_thumb_6E4EA081

Visual Studio 2008 Standard and Professional edition support:

http://support.microsoft.com/lifecycle/?p1=12913

vs2008_Pro_support_thumb_4CEEFAE5

Again, based on the table above, Visual Studio 2008 SP1 will extend the support slightly longer, but only one year after the release of the service pack.

What about Visual Studio 2010? Visual Studio 2010 still has the mainstream support:

http://support.microsoft.com/lifecycle/?p1=14048

vs2010_support_thumb_2065CAFF

We can still count on Visual Studio 2010 until July 2015.

The latest release, Visual Studio 2012, has the latest update named Update 3.

FYI, here's the support information for Visual Studio 2012:

http://support.microsoft.com/lifecycle/?p1=16677

vs2012_ultimate_support_thumb_62F56677

Please take note: there is no Service Pack 2 for Visual Studio 2010, as Microsoft is entering the trend of shorter cadence of Visual Studio and Windows.

This means that there will be new release of Visual Studio to be released in 12-14 months period!

For more information, please visit: http://visualstudiomagazine.com/articles/2013/05/10/new-visual-studio-release-cadence-begins.aspx

For me, this is the best opportunity as a developer, always learn something new and very quickly adopt it as needed.

Conclusion? We should focus our energy on Visual Studio 2012, and prepare for Visual Studio 2013!

VisualStudio2012_small_logo_thumb_76A2500B

Sounds agile for me!

Tuesday, June 18, 2013

Team Foundation Server 2010, Team Foundation Server 2012 and Team Foundation Service supports OData

Yes! The title means it, literally!

Now we could connect and query Team Foundation Server and Team Foundation Service work items and many more using OData! Using OData means we are using URLs to directly query.

OData quick background

OData has been around and it’s one of the cool openness initiatives from Microsoft. Originally named as Astoria, then WCF Data Service, now it’s called OData.

It’s simply a specification and libraries to produce data to be visible as URL, and also the consumer of the URLs.

The official website for OData is: http://www.odata.org/

In there, you’ll find numerous library supports for many platforms/OS, also as the server (OData producer) and the client (also called OData consumer). These producer and consumer really fits the nature of OData as web services, therefore it can be seen as open web API to access data.

These are current libraries and OS supports:

client_server_odata_797D144E

What about the real customers/users of OData? There are many producers today, and one of them is the famous Nerd Dinner. I call it famous, because it’s often used as samples of OData usages from Microsoft.

There was Netflix, but Netflix has ceased OData support. Here’s the announcement: http://developer.netflix.com/blog/read/Changes_to_the_Public_API_Program

The use of OData in Team Foundation Server and Team Foundation Service

To use OData on both TFS (the server and the service, what have you), we need to have library supports in the form of Nuget package and also tooling support.

The tooling support is available to download at: http://www.microsoft.com/en-us/download/details.aspx?id=39373

I will not dive into the detail of how to set up OData for TFS, as there are resources from Microsoft to have!

To get started, let’s visit this Brian Keller blog entry: http://blogs.msdn.com/b/briankel/archive/2013/01/07/odata-service-for-team-foundation-server-v2.aspx

This is the official Technet Wiki: http://social.technet.microsoft.com/wiki/contents/articles/15039.odata-service-for-team-foundation-server-v2.aspx

This is the video explanation from Brian Keller: http://channel9.msdn.com/Blogs/briankel/OData-Service-for-Team-Foundation-Server-2010

TFS on the cloud has provided simple OData documentation: https://tfsodata.visualstudio.com/

Now, download the “OData Service for Team Foundation Server v2” libraries for Visual Studio at this link: http://www.microsoft.com/en-us/download/details.aspx?id=36230

Then you'll set to have OData for TFS!

In the libraries above, you’ll see OData for TFS in action as Windows Store app:

7462.image_thumb_302BC854

Enjoy and happy coding!

Wednesday, May 22, 2013

Speaking for MUGI at University of Mercu Buana

Another speaking opportunity comes! This time an invitation was from University of Mercu Buana. They asked me to provide an introduction to SQL Server 2012.

Again, my main focus is Visual F# but I’m happy to talk about other topic as well.

The schedule was on May 18, 2013 and the venue was at Mosque auditorium inside Mercu Buana surroundings. This is cool, as far as I remember I was never invited to speak in a mosque.

The speaking went quite well, although the audiences were having many background and some of them were non IT.

speaking_mercubuana_may18

This provides a real challenge for me as well, to provide a speaking but with explanations as simple enough to be understood by most audiences.

The slides for this speaking is available on my SkyDrive, and I will release the slide to MUGI.

This is my Skydrive slide link: http://sdrv.ms/160RW5R

See you at the next speaking! :)

Thursday, April 18, 2013

My Indonesian KB article about Visual Studio 2012 Update 1 installation has been published for April 2013

Hi again, all!

Busy month again for March 2013! I have been contributing KB articles for you, my dear blog readers and my MUGI community readers!

Thanks to Min Seop, Clarisse Ng, now my KB articles in Indonesia has been published!

The topic is now “How to download and install Visual Studio 2012 Update 1 offline”. And the title in Indonesian is: “Bagaimana cara menginstal Visual Studio 2012 Update 1 secara offline”.

The URL is: http://support.microsoft.com/kb/2838372/id-id

Now, any updates to Visual Studio 2012 will be delivered as “Update” instead of Service Pack. These updates may contain new features, not just bug fixes.

The complete list and overview of Visual Studio 2012 Update 1 is available at: http://support.microsoft.com/kb/2797915