Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
F# High Performance

You're reading from   F# High Performance Increase your F# programming productivity and focus on performance optimization with best practices, expert techniques, and more

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781786468079
Length 338 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Eriawan Kusumawardhono Eriawan Kusumawardhono
Author Profile Icon Eriawan Kusumawardhono
Eriawan Kusumawardhono
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Performing Common Optimizations in F# FREE CHAPTER 2. Performance Measurement 3. Optimizing Data Structures 4. Introduction to Concurrency in F# 5. Advanced Concurrency Support in F# 6. Optimizing Type Provider 7. Language Features and Constructs Optimization 8. Optimizing Computation Expressions

Overview of F# tooling in Visual Studio

F# has been supported in Visual Studio since Visual Studio 2010, and in Visual Studio 2015 the support has improved with better syntax colorizations than Visual Studio 2010, not just IDE. This F# IDE support is officially called Visual F#.

This tooling is available as open source from Microsoft and it is available to be downloaded from GitHub at https://github.com/Microsoft/visualfsharp/.

And the F# compiler itself is open source and it is also available from GitHub (including the design proposal discussions) at https://github.com/fsharp.

The tooling is under the governance of Microsoft, but it is welcoming community contributions as it is available on GitHub. All of the community participations of tooling, compilers and the language specifications are under the governance of the F# Software Foundation (FSSF).

We can also support FSSF directly. For more information about FSSF, please visit http://fsharp.org/.

Note

The F# community projects are also managed by FSSF, and it is welcoming contributions as well. FSSF is an independent entity and it is not tied to Microsoft.

Visual F# in Visual Studio 2015 has mainly the following capabilities:

  • Project template support, including the NuGet package addition and references to other projects in the same solution file. The other projects can be VB or C# projects, not just F#.
  • AssemblyInfo support in a separate file. This feature has been available since Visual Studio 2015. Previously it was only available in C# and VB projects.
  • The F# compiler, FSC, is used to compile F# into .NET executable and libraries in the form of DLL.
  • Integration of the F# compiler, MSBuild infrastructure, and also Intellisense.
  • F# libraries, a foundation to the functional programming constructs of F# and F# unique concurrency features such as asynchronous workflow and MailboxProcessor. It also contains many useful functions to further interoperate with C#/VB, including interoperating with .NET delegates.
  • Interactive support for the F# interactive (FSI) prompt in the IDE.

For more information about F# tooling, this is the official MSDN Library link: 

https://msdn.microsoft.com/visualfsharpdocs/conceptual/visual-fsharp

It is recommended to always consult the Visual F# documentation on GitHub first, then combine it with the online MSDN Library section of F#.

Microsoft has planned to rebuild all of the online MSDN Library to use the GitHub participation model, so developers can submit a pull request to modify or to amend any documentation page. Visual F# is now being reworked on as well, but there are some pages still left behind inside Visual F# GitHub repo.

To always check the latest development of F# documentation on GitHub, visit https://github.com/Microsoft/visualfsharpdocs.

Interactive support for F# interactive

F# interactive is a tool to interpret your F# code and run it immediately. It will also process and show the results, the types, and the syntax errors. The code can be run in the interactive prompt or taken from the source code and then run into F# interactive.

Note

The concept of having interpreted code, executing it, and seeing the results is called REPL. REPL is abbreviated from Read-Eval-Print-Loop, and it was first available as the system's command-line prompt. There is nothing new about this REPL concept, as other programming languages such as Python already have had REPL before F#.

Compared to C# and VB, F# interactive is the first to have interactive REPL support since Visual Studio 2010. Unfortunately, there is no Visual Studio's Intellisense support for F# interactive yet.

There are two ways to use F# interactive:

  • In Visual Studio IDE
  • In Command Prompt

The most common usage of F# interactive is within Visual Studio IDE.

We have to set up the F# Interactive window to be displayed in order to use F# interactive.

These are the steps to display the F# Interactive window:

  1. Open the View menu from the Visual Studio main menu.
  2. Choose Other Windows.. and then choose F# Interactive.

A window that hosts the F# Interactive within Visual Studio will appear and it will be ready to interpret our F# code:

Interactive support for F# interactive

As a starter, type #help followed by ;; to mark as closing statements to be evaluated. We now see some further options:

Interactive support for F# interactive

F# interactive can be used to not only interpret and run F# code but also as a way to see immediate results of a calculation.

Type 5 * 25;; and press Enter.

We now see the result of that calculation:

Interactive support for F# interactive

We can also execute codes in the Visual Studio editor when we are opening F# source code file.

For example, create a new project using the F# tutorial project template:

Interactive support for F# interactive

You may find that your display of Visual Studio is different from the previous screenshot. Actually, the aforementioned display depends on what Visual Studio edition we have. For Visual Studio Enterprise, more templates are available for us to use, such as the Modeling Projects to create UML.

For the purpose of F#, the project templates of the F# projects are the same for the Community Edition and above.

After creating the project, an F# project contains Tutorial.fsx.

Before we use F# interactive, we should turn on the option for displaying line numbers. It is also recommended to have this option always turned on, as it will provide easier navigation to the code we write:

  1. Go to the Tools menu and choose Options. It is available in F# options in the Options dialog:

    Interactive support for F# interactive

  2. Now double-click Tutorial.fsx, and highlight lines 44 to 61:

    Interactive support for F# interactive

  3. Then press Alt + Enter. F# interprets the code. We can see the result of the interpretation in F# Interactive:

    Interactive support for F# interactive

We have tried F# interactive from within the Visual Studio IDE. Let's use F# interactive from Command Prompt.

Note

We can also use Ctrl + Alt + F to activate or open F# Interactive.

To use F# interactive from Command Prompt, we call the executable FSI directly from Command Prompt.

The best way to run FSI is from Visual Studio's developer Command Prompt. This developer Command Prompt is available under the Visual Studio 2015 folder on the start menu of the Windows desktop menu bar.

Select it, and now we have the Developer Command Prompt for VS2015:

Interactive support for F# interactive

Type FSI and press Enter.

We can try to write some code to evaluate, such as:

let anynumber = 5 * 25;; 

Press Enter. The immediate result will be displayed:

Interactive support for F# interactive

To quit the FSI, type #quit;; and press Enter.

Using F# interactive from Command Prompt is faster but it is also not quite so user-friendly because we cannot evaluate multiple lines of code easily. It is easier to evaluate this in Visual Studio IDE.

Note

For simplicity and ease of use, the rest of this book will always use FSI within the Visual Studio IDE.

For more information about F# FSI, consult the FSI reference from the MSDN Library at  https://msdn.microsoft.com/visualfsharpdocs/conceptual/fsharp-interactive-%5bfsi.exe%5d-reference.

FSI is also configurable. We can configure FSI further by leveraging the FSI class library in the Microsoft.FSharp.Compiler.Interactive namespace. More information on this library is also available at the F# FSI URL mentioned previously.

You have been reading a chapter from
F# High Performance
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781786468079
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime