পৃষ্ঠাসমূহ

Search Your Article

CS

Pageviews

Wednesday, February 1, 2017

F# - Maps

In F#, a map is a special kind of set that associates the values with key. A map is created in a similar way as sets are created.

Creating Maps

Maps are created by creating an empty map using Map.empty and adding items using the Add function. The following example demonstrates this −

F# - Discriminated Unions

Unions, or discriminated unions allows you to build up complex data structures representing well-defined set of choices. For example, you need to build an implementation of a choice variable, which has two values yes and no. Using the Unions tool, you can design this.

F# - Mutable Data

Variables in F# are immutable, which means once a variable is bound to a value, it can’t be changed. They are actually compiled as static read-only properties.
The following example demonstrates this.

F# - Arrays

Arrays are fixed-size, zero-based, mutable collections of consecutive data elements that are all of the same type.

Creating Arrays

You can create arrays using various syntaxes and ways or by using the functions from the Array module. In this section, we will discuss creating arrays without using the module functions.
There are three syntactical ways of creating arrays without functions −

F# - Mutable Lists

The List<'T> class represents a strongly typed list of objects that can be accessed by index.
It is a mutable counterpart of the List class. It is similar to arrays, as it can be accessed by an index, however, unlike arrays, lists can be resized. Therefore you need not specify a size during declaration.

F# - Mutable Dictionary

The Dictionary<'TKey, 'TValue> class is the mutable analog of the F# map data structure and contains many of the same functions.
Recapitulating from the Map chapter in F#, a map is a special kind of set that associates the values with key.

F# - Basic I/O

Basic Input Output includes −
  • Reading from and writing into console.
  • Reading from and writing into file.

Core.Printf Module

We have used the printf and the printfn functions for writing into the console. In this section, we will look into the details of the Printf module of F#.

F# - Generics

Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.

F# - Delegates

A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. F# delegates are similar to pointers to functions, in C or C++.

F# - Enumerations

An enumeration is a set of named integer constants.
In F#, enumerations, also known as enums, are integral types where labels are assigned to a subset of the values. You can use them in place of literals to make code more readable and maintainable.

F# - Pattern Matching

Pattern matching allows you to “compare data with a logical structure or structures, decompose data into constituent parts, or extract information from data in various ways”.
In other terms, it provides a more flexible and powerful way of testing data against a series of conditions and performing some computations based on the condition met.
Conceptually, it is like a series of if… then statements.

F# - Exception Handling

An exception is a problem that arises during the execution of a program. An F# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. F# exception handling provides the following constructs −