Until recently, CL has had limited support for lists. Using the graph’s numberList
source, you could access elements of a list or pass them between graphs, but not much else. In this article, we’ll give you a preview of some upcoming features that make lists much more flexible.
These features aren’t complete and there may be breaking changes. They won’t enter the official documentation for some time. Nonetheless, if you’ve been wanting to do more with lists, read on!
Creating and accessing lists
If you want to make a list, you can use use brackets. For example, to make a list of five numbers you might enter:
myNumbers = [5, 3.14, 2.71, 19, -3]
After making a list, you can access its elements using the elementAt
function or brackets. In our example, myNumbers.elementAt(1)
and myNumbers[1]
would both be equal to 5
.
One very exciting change is that you can now have lists of almost any type! A list of strings might look like:
myStrings = ["Hello", "world."]
# This would be the string "Hello world."
myNewString = "${myStrings[1]} ${myStrings[2]}"
List functions
Just being able to define lists in CL unlocks a world of possibility, but there are also many new and powerful functions for working with them. In this article, we’ll provide an overview of those functions. Be on the lookout for more detailed examples in future articles!
Arrow functions
Many list functions use syntax that is new to CL to define arrow functions. Arrow functions look like (parameters) => expression
. The left side takes a comma separated list of parameters. The right side is an expression that defines the output of the function.
Arrow functions are similar to the mathematical functions you might be used to, but they can act on values of any type.
Jump to the details for any of these functions:
Lists.map()
The map()
function creates a new list in which each element is the result of calling the provided arrow function on every element of the calling list.
Variations of map
Example
# This would return the list [false, false, false, true, true].
myList = [1, 2, 3, 4, 5].map((el) => el > 3)
Lists.filter()
The filter()
function returns a copy of the calling list containing only the elements that return true for the given test function.
Variations of filter
Example
# This would return the list [4, 5].
myList = [1, 2, 3, 4, 5].filter((el) => el > 3)
Lists.first()
The first()
function returns the first element of a list that returns true for the given test function.
Variations of first
Example
# This would return the number 4.
myList = [1, 2, 3, 4, 5].first((el) => el > 3)
Lists.all() and Lists.any()
The all()
and any()
functions are used to check if all (or any) elements of a list return true for a given test function.
Variations
Example
# anyPositive would be true.
anyPositive = [1, 2, -3, 2, -9].any((el) => el > 0)
# allPositive would be false.
allPositive = [1, 2, -3, 2, -9].all((el) => el > 0)
range
Create a list of numbers starting at a lower bound and ending at an upper bound. A step size can be added.
Variations of range
Example
# This would return the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
list1 = range(10)
# This would return the list [5, 6, 7].
list2 = range(5, 7)
# This would return the list [0, 20, 40, 60, 80, 100].
list3 = range(0, 100, 20)
Lists.slice()
The slice()
function returns a copy of a subset of the given list. It operates similarly to the graphing calculator statement: [1, 2, 3, 4, 5][3…5].
Example
# This would return the list [2, 3, 4].
mySlice = [1, 2, 3, 4, 5].slice(2, 4)
Lists.join()
The join()
function joins a list to the end of another list. The lists must be the same type.
Example
# This would return the list [1, 2, 3, 4].
myList = [1, 2].join([3, 4])
Lists.reverse()
The reverse()
function reverses the order of elements in a list.
Example
# This would return the list [3, 2, 1].
myList = [1, 2, 3].reverse()
Lists.reduce()
The reduce()
function is used to reduce a list of values down to one value.
Variations of reduce
Example
# You can use reduce to find the the sum of a list of numbers.
# This would return the number 15.
totalNum = [1, 2, 3, 4, 5].reduce(
0, # The initial value for the accumulator
(accumulator, current) => numericValue(`${accumulator}+${current}`)
)
Keep in mind that these features are still under development and may change in the future. Now, go forth and make some cool things with lists!