>> Welcome to this series on F#. We're going over
concepts to help you get started building F# applications.
In this video, we're going to be taking a look
at how you can create and work with collections in F#.
As a reminder, any code that you see in this video can be found at
aka.ms/f#-101-samples. Let's get started.
What are collections? Collections are a type
that contains a series of elements or items of the same type.
You can think of that as a list of numbers or a list of customers.
In this case, I'm using the word list as more of
a general term to refer to a collection of items.
But in F#, there's actually many different collection types,
one of which happens to be a list.
While they're all basically very similar in the sense
that they just contain items, they vary slightly.
Let's take a look at what some of those differences are.
First of all, you have a list
which is an ordered series of elements.
Then you have an array which is fixed
size and provides easy access.
You just need to refer to the index
of an element and that's what you're able to access it.
It can be multi-dimensional so you can have a two,
three or even a four-dimensional array.
Then you have a sequence whose elements
are generated as they're needed so it's going to be
a really good option when you're working with
very large collections. Then you have a map.
Now map contains key value pairs and in order to get its values,
you have to refer to them by their key.
Then finally, you have a set which is just a collection
that contains unique or distinct items.
Now, all of these collections are
immutable with the exception of the array.
With the array, you can update or
change the values inside of elements at a specific index.
Before we take a look at some code,
I just want to be familiar with some syntax.
Let's take a look at what the syntax for arrays looks like.
For this video, we're mainly going to be focusing on
the top or the first three from that list that we saw previously.
We're going to take a look at lists, arrays, and sequences.
A lot of examples are actually going to work mainly with lists.
The reason for that is a lot of the operations and the ways
that you work with these collections are very similar.
Let's take a look at some of the syntax and
how these are represented.
A list, you can see is represented by
square brackets and each of
the elements is separated by semicolon.
It really looks very similar except that
it also has a vertical bar.
Then finally you have a sequence which is
enclosed by the sequence expression,
which contains these curly braces.
That we have a general idea
of what the differences between these collections
are and what the syntax for
them looks like, let's take a look at some code.
To create collections,
you can use a built-in functions like init.
In this example, I'm creating a list of
10 integers using a Lambda expression to generate the values.
In this case, it's just a consecutive sequence of
numbers starting with zero and any with the size of my lists.
Inside the Lambda expression, you
can basically do anything you want.
For example, in a second list I create,
I multiply each of the values by two.
You can also initialize collections directly.
In this case, I just typed out all my elements.
For a number list,
a sure way to do that is by using
these two dots. Essentially it's the same.
Create a list starting with the number 1 and ending with 10.
Arrays work in a very similar way.
Notice however, I can also create multidimensional arrays.
Sequences are more of the same
except some elements are generous they're needed,
I can create a virtually infinite list to iterate through.
Now that you know how to create collections,
let's take a look at how you can access elements inside of them.
In this example, we'll work
with a list containing transaction record types.
These records will have a day when the transaction took place,
an Id for the customer,
and the amount of the transaction.
To access elements, you can use head and tail functions.
Head returns the first element of a list,
and tail returns the rest of the elements
with the exception of the first.
You can perform similar access using indices.
You already know that arrays are more
efficient at random access by index.
Using the find built-in functions,
you can try and find elements based on certain criteria.
In this example, it'll return
the first match for a customer with the Id of one.
When I use the same function of an element that
doesn't exist, I get an error.
To more gracefully handle missing data,
use a tryFind function which leverages the option type.
If an element is not found, it returns none,
and if it is found, it returns some with element.
You can use the append function to add
items to the beginning or to the end of your list.
If you need to convert between collections,
you can use the name of the collection you want to convert into,
followed by the collection you're converting from.
In this case, I'm converting a list into an array and a sequence.
Now that you know how to create, access,
and modify collections, let's take a look at
how you can use built-in functions to transform data.
For many of these, Lambda expressions are the key.
You can use name functions as well.
With the map function, you apply
transformations to each element in your collection.
Using a transactions example,
we can calculate tax information such as the original amount,
the tax amount,
and the total amount after-tax
and return it as an anonymous record.
Iter is like the map function,
except you don't expect to return a value or to be more specific,
your return value is of unit type.
In this example, I'm just printing out the customer Ids.
A common operation is to aggregate or get
summary statistics of your data like
total or average amount of something.
To do so, you can use functions like sumBy and averageBy where
you're able to specify which of the properties in
your elements you want to apply that operation to.
In this case, once you get the sum and average amounts.
Filtering data is another common operation.
Use a filter function and provide a condition to filter on.
In this case, the transactions later than January 31st.
Finally, ordering using the sort
by and sort by descending functions,
you can order your data in whichever order you want.
Also, remember that by using the pipe operator and composition,
you can create entire pipelines to transform your data.
These are just some of the basics,
but I would encourage you to take a look at
the documentation to see what other transformations are out there.
Now that you know what collections
are and how you can work with them in F#,
in the next video, we're going to take a look how you can use
loops to perform repetitive actions in your application,
how you can use conditional expressions to
perform different behavior based on certain criteria,
and how pattern-matching can take
your conditionals to the next level
by leveraging the power of the compiler.
See you in the next video.
[MUSIC]