Tuples, Lists and Arrays in Reason

    Nik GrafNik Graf

    ReasonML ships with a couple more useful data structures to store multiple elements. Tuple is one of them. It allows us to store a fixed number of values of any kind of type. While lists are homogeneous and immutable. Arrays are mutable.

    In this lesson we explore multiples ways on how to create, access and update each of these data structures.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    Instructor: 00:00 To create a tuple, we open a parenthesis. Add two or more values and close it with a parenthesis again. The result is an immutable tuple of two elements. A tuple can hold fixed number of values. These can be of same or different types. In addition, each element is identified by position, rather than by name.

    00:22 Let's create another one with an integer, two Booleans, and a string. As you can see, you can combine all sorts of types in there. That's the one law. This time, we're going to nest a tuple. We describe a circle with a point having X and Y position and the radius. It doesn't indicate that it is a circle, but we can create a tuple type and provide a type annotation for clarity.

    00:50 Let's see how we can access elements of a tuple. The standard library provides two convenience functions to access the first two elements. FST for first, to access the first item, and SND for second, to access the second one. In case you want to access any other element, we are required to use the structuring.

    01:14 So far, we have seen structuring only for records, but it also works for tuples using parentheses instead of curly braces. Value of three is not bound to the name C. In case you don't want all positions to be assigned, we can replace the ones to be ignored with an underscore.

    01:34 Keep in mind the order within tuples is fixed. Every tuple is immutable. This means we don't update tuple, we'd rather create a new one. For example, like this. You created another circle with the second element, the radius, and you replaced the six.

    01:55 In general, it's recommended to keep the usage of tuples local, update the structures that are long-living and passed around often. It's recommended to prefer records.

    02:05 Next, we want to focus on the typelists. Lists are homogeneous and immutable. Homogeneous means we can't have different types of items in a list. That said, we can't have a variance storing different types and create a list of constructors.

    02:27 Second trait of lists is that they're immutable. This means we can't append, replace, or prepend an item to the same list. We'd rather have to create a new one. Then, we'll do ways how to add new elements by creating a new list.

    02:41 For once, we use the append function on the lists module shipping with the standard library. The other way is to use Reason special operator to concatenate lists. The third one is using the spread operator. Keep in mind this only with prepending one or multiple elements, but not for appending elements.

    03:05 The recommended way to access a list is using the switch expression. We will cover this later extensively in the pattern matching lesson. To give you a glimpse of it, here's an example where we get out the first element of the list and print it as a string.

    03:34 As an alternative to access a list item, we can use list.end. Keep in mind, it will raise an exception in case the list is too short, and therefore it's rather recommended to rely on other strategies like switch expressions.

    03:52 To create a new list and manipulate each item, we have at our disposal. It accepts a function as the first parameter and the regional list as a second one. In the standard library, there are many more utility functions like find, filter, sort, exist, split and so on. We can use the API recommendation on the Reason website to figure out more.

    04:17 Let's move on to arrays. Arrays are declared the same way as lists, except the array's content is wrapped in pipes and brackets. Compared to lists, one significant difference is that array's immutable.

    04:32 You can access an item using brackets, but also assign a new value to this position using the equal sign. Same as of list, the standard library ships a couple of useful utility functions. For example,