|
31 | 31 |
|
32 | 32 | #### Protocols |
33 | 33 |
|
34 | | - iex> inspect Qex.new |
35 | | - "#Qex<[]>" |
| 34 | +```elixir |
| 35 | +iex> inspect Qex.new |
| 36 | +"#Qex<[]>" |
36 | 37 |
|
37 | | - iex> Enum.count Qex.new(1..5) |
38 | | - 5 |
| 38 | +iex> Enum.count Qex.new(1..5) |
| 39 | +5 |
39 | 40 |
|
40 | | - iex> Enum.empty? Qex.new |
41 | | - true |
| 41 | +iex> Enum.empty? Qex.new |
| 42 | +true |
42 | 43 |
|
43 | | - iex> Enum.map Qex.new([1, 2, 3]), &(&1 + 1) |
44 | | - [2, 3, 4] |
| 44 | +iex> Enum.map Qex.new([1, 2, 3]), &(&1 + 1) |
| 45 | +[2, 3, 4] |
45 | 46 |
|
46 | | - iex> inspect Enum.into(1..5, %Qex{}) |
47 | | - "#Qex<[1, 2, 3, 4, 5]>" |
| 47 | +iex> inspect Enum.into(1..5, %Qex{}) |
| 48 | +"#Qex<[1, 2, 3, 4, 5]>" |
| 49 | +``` |
48 | 50 |
|
49 | 51 | #### Create a new queue from a range |
50 | 52 |
|
51 | | - iex> inspect Qex.new(1..3) |
52 | | - "#Qex<[1, 2, 3]>" |
| 53 | +```elixir |
| 54 | +iex> inspect Qex.new(1..3) |
| 55 | +"#Qex<[1, 2, 3]>" |
| 56 | +``` |
53 | 57 |
|
54 | 58 | #### Create a new queue from a list |
55 | 59 |
|
56 | | - iex> inspect Qex.new([1, 2, 3]) |
57 | | - "#Qex<[1, 2, 3]>" |
| 60 | +```elixir |
| 61 | +iex> inspect Qex.new([1, 2, 3]) |
| 62 | +"#Qex<[1, 2, 3]>" |
| 63 | +``` |
58 | 64 |
|
59 | 65 | #### Add an element to the back of the queue |
60 | 66 |
|
61 | | - iex> q = Qex.new([:mid]) |
62 | | - iex> Enum.to_list Qex.push(q, :back) |
63 | | - [:mid, :back] |
| 67 | +```elixir |
| 68 | +iex> q = Qex.new([:mid]) |
| 69 | +iex> Enum.to_list Qex.push(q, :back) |
| 70 | +[:mid, :back] |
| 71 | +``` |
64 | 72 |
|
65 | 73 | #### Add an element to the front of the queue |
66 | 74 |
|
67 | | - iex> q = Qex.new([:mid]) |
68 | | - iex> Enum.to_list Qex.push_front(q, :front) |
69 | | - [:front, :mid] |
| 75 | +```elixir |
| 76 | +iex> q = Qex.new([:mid]) |
| 77 | +iex> Enum.to_list Qex.push_front(q, :front) |
| 78 | +[:front, :mid] |
| 79 | +``` |
70 | 80 |
|
71 | 81 | #### Get and remove an element from the front of the queue |
72 | 82 |
|
73 | | - iex> q = Qex.new([:front, :mid]) |
74 | | - iex> {{:value, item}, _q} = Qex.pop(q) |
75 | | - iex> item |
76 | | - :front |
77 | | - |
78 | | - iex> q = Qex.new |
79 | | - iex> {empty, _q} = Qex.pop(q) |
80 | | - iex> empty |
81 | | - :empty |
| 83 | +```elixir |
| 84 | +iex> q = Qex.new([:front, :mid]) |
| 85 | +iex> {{:value, item}, _q} = Qex.pop(q) |
| 86 | +iex> item |
| 87 | +:front |
| 88 | + |
| 89 | +iex> q = Qex.new |
| 90 | +iex> {empty, _q} = Qex.pop(q) |
| 91 | +iex> empty |
| 92 | +:empty |
| 93 | +``` |
82 | 94 |
|
83 | 95 | #### Get and remove an element from the back of the queue |
84 | 96 |
|
85 | | - iex> q = Qex.new([:mid, :back]) |
86 | | - iex> {{:value, item}, _q} = Qex.pop_back(q) |
87 | | - iex> item |
88 | | - :back |
89 | | - |
90 | | - iex> q = Qex.new |
91 | | - iex> {empty, _q} = Qex.pop_back(q) |
92 | | - iex> empty |
93 | | - :empty |
| 97 | +```elixir |
| 98 | +iex> q = Qex.new([:mid, :back]) |
| 99 | +iex> {{:value, item}, _q} = Qex.pop_back(q) |
| 100 | +iex> item |
| 101 | +:back |
| 102 | + |
| 103 | +iex> q = Qex.new |
| 104 | +iex> {empty, _q} = Qex.pop_back(q) |
| 105 | +iex> empty |
| 106 | +:empty |
| 107 | +``` |
94 | 108 |
|
95 | 109 | #### Reverse a queue |
96 | 110 |
|
97 | | - iex> q = Qex.new(1..3) |
98 | | - iex> Enum.to_list q |
99 | | - [1, 2, 3] |
100 | | - iex> Enum.to_list Qex.reverse(q) |
101 | | - [3, 2, 1] |
| 111 | +```elixir |
| 112 | +iex> q = Qex.new(1..3) |
| 113 | +iex> Enum.to_list q |
| 114 | +[1, 2, 3] |
| 115 | +iex> Enum.to_list Qex.reverse(q) |
| 116 | +[3, 2, 1] |
| 117 | +``` |
102 | 118 |
|
103 | 119 | #### Split a queue into two, the front n items are put in the first queue |
104 | 120 |
|
105 | | - iex> q = Qex.new 1..5 |
106 | | - iex> {q1, q2} = Qex.split(q, 3) |
107 | | - iex> Enum.to_list q1 |
108 | | - [1, 2, 3] |
109 | | - iex> Enum.to_list q2 |
110 | | - [4, 5] |
111 | | - |
| 121 | +```elixir |
| 122 | +iex> q = Qex.new 1..5 |
| 123 | +iex> {q1, q2} = Qex.split(q, 3) |
| 124 | +iex> Enum.to_list q1 |
| 125 | +[1, 2, 3] |
| 126 | +iex> Enum.to_list q2 |
| 127 | +[4, 5] |
| 128 | +``` |
112 | 129 |
|
113 | 130 | #### Join two queues together |
114 | 131 |
|
115 | | - iex> q1 = Qex.new 1..3 |
116 | | - iex> q2 = Qex.new 4..5 |
117 | | - iex> Enum.to_list Qex.join(q1, q2) |
118 | | - [1, 2, 3, 4, 5] |
119 | | - |
120 | | - |
| 132 | +```elixir |
| 133 | +iex> q1 = Qex.new 1..3 |
| 134 | +iex> q2 = Qex.new 4..5 |
| 135 | +iex> Enum.to_list Qex.join(q1, q2) |
| 136 | +[1, 2, 3, 4, 5] |
| 137 | +``` |
121 | 138 |
|
122 | 139 | ## Why not "Queue"? |
123 | 140 |
|
|
0 commit comments