[Elixir Foundation] Composite Type: List

Keywords: Programming

Recently, I'm learning Elixir development because it's a function-oriented programming language. It's hard for people like me who have been doing object-oriented development to understand many things. So I intend to write a series of basic articles to consolidate my knowledge and to help other people who study this technology.

We will learn in the order of definitions, commonly used operators, methods, and performance.

Definition

Lists in Elixir are expressed in parentheses. List in Elixir is a high-performance linked list, which means that the inside of the List is made up of [head, tail]

iex(27)> [1, 2, true, 3]
iex(28)> [head | tail] = [1, 2, 3]
iex(29)> head
1
iex(30)> tail
[2, 3]

Note that in most cases, head is an element, while tail is an array of elements other than the first element. Because the interior is a linked list, adding an element to the head is very fast (constant time), and adding to the tail increases with the size of the List. Later we will demonstrate the performance of two ways of adding.

Common operators

# Additive elements
[1, 2] ++ [3] # [1, 2, 3]
# Delete elements
[1, 2, 3, 4] -- [3, 2] # [1, 4]
# Compare
[3, 2] === [3, 2] # true

Common methods

Basically look at List module and Enum module You can see most of the methods. Here are some of the commonly used ones.

# Get header elements
iex(81)> hd [1,2,3]
1
# Get tail arrays
iex(82)> tl [1,2,3]
[2, 3]
# Get the list length
iex(83)> length [1,2,3]
3
# Traversal element
iex(84)> Enum.each [1,2,3], fn(x) ->
...(84)>   IO.puts x
...(84)> end
1
2
3
# Access based on Subscripts
iex(86)> Enum.fetch([1, 2, 3], 2)
{:ok, 3}
# Delete an element
iex(106)> List.delete([1,2,3,4,"a"], "a")
[1, 2, 3, 4] #No affirmative deletion is guaranteed.
# Delete the specified subscript element
iex(110)> List.delete_at([1,2,3,4,"a"], 4)
[1, 2, 3, 4] #No affirmative deletion is guaranteed.
# Insert a new element at the specified location
iex(112)> List.insert_at([1,2,3,4,"a"], 4, 5)
[1, 2, 3, 4, 5, "a"]
# Delete duplicate elements
iex(88)> Enum.uniq([1, 2, 3, 3, 2, 1])
[1, 2, 3, 2, 1]
# Array elements are connected
iex(90)> Enum.join([1, 2, 3])
"123"
iex(91)> Enum.join([1, 2, 3], " = ")
"1 = 2 = 3"
# Random acquisition of an element
iex(92)> Enum.random [1,2,3,4,5]
1
iex(93)> Enum.random [1,2,3,4,5]
3
# Disorder in sequence
iex(94)> Enum.shuffle [1,2,3,4,5]
[4, 5, 3, 2, 1]
# Sort
iex(100)> Enum.shuffle([1,2,3,4,5]) |> Enum.sort
[1, 2, 3, 4, 5]
# Get n elements from the top
iex(101)> Enum.take([1,2,3,4,5], 3)
[1, 2, 3]
# Random acquisition of n elements
iex(102)> Enum.take_random([1,2,3,4,5], 3)
[5, 1, 4]
# Summing numbers in an array
iex(105)> Enum.sum 1..10
55

performance

The following program compares the performance of head and tail additions

defmodule Recursion do
  def prepend_elem(list, elem, n) when n <= 1 do
    list = elem ++ list
  end

  def prepend_elem(list, elem, n) do
    list = elem ++ list
    prepend_elem(list, elem, n - 1)
  end

  def append_elem(list, elem, n) when n <= 1 do
    list = list ++ elem
  end

  def append_elem(list, elem, n) do
    list = list ++ elem
    append_elem(list, elem, n - 1)
  end
end
IO.puts "start prepend"
prepend_start_time = Time.utc_now |> Time.to_string
list = Recursion.prepend_elem([], [1], 100000)
prepend_end_time = Time.utc_now |> Time.to_string
IO.puts "start at #{prepend_start_time}" 
IO.puts "end at #{prepend_end_time}"
IO.puts "------------------------"
IO.puts "start append"
append_start_time = Time.utc_now |> Time.to_string
list = Recursion.append_elem([], [1], 100000)
append_end_time = Time.utc_now |> Time.to_string
IO.puts "start at #{append_start_time}" 
IO.puts "end at #{append_end_time}"

start prepend
start at 15:10:24.463439
end at 15:10:24.474485
------------------------
start append
start at 15:10:24.483897
end at 15:10:48.547505

<span style="color: red"> Conclusion: Prepend 100,000 times: 10 milliseconds. Append 100,000 times: 24 seconds (the more elements, the slower), 2,400 times </span>

Posted by mwilson2 on Thu, 23 May 2019 16:23:34 -0700