Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
9
Task/Collections/Elixir/collections-1.elixir
Normal file
9
Task/Collections/Elixir/collections-1.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
empty_list = []
|
||||
list = [1,2,3,4,5]
|
||||
length(list) #=> 5
|
||||
[0 | list] #=> [0,1,2,3,4,5]
|
||||
hd(list) #=> 1
|
||||
tl(list) #=> [2,3,4,5]
|
||||
Enum.at(list,3) #=> 4
|
||||
list ++ [6,7] #=> [1,2,3,4,5,6,7]
|
||||
list -- [4,2] #=> [1,3,5]
|
||||
5
Task/Collections/Elixir/collections-2.elixir
Normal file
5
Task/Collections/Elixir/collections-2.elixir
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
empty_tuple = {} #=> {}
|
||||
tuple = {0,1,2,3,4} #=> {0, 1, 2, 3, 4}
|
||||
tuple_size(tuple) #=> 5
|
||||
elem(tuple, 2) #=> 2
|
||||
put_elem(tuple,3,:atom) #=> {0, 1, 2, :atom, 4}
|
||||
4
Task/Collections/Elixir/collections-3.elixir
Normal file
4
Task/Collections/Elixir/collections-3.elixir
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
list = [{:a,1},{:b,2}] #=> [a: 1, b: 2]
|
||||
list == [a: 1, b: 2] #=> true
|
||||
list[:a] #=> 1
|
||||
list ++ [c: 3, a: 5] #=> [a: 1, b: 2, c: 3, a: 5]
|
||||
15
Task/Collections/Elixir/collections-4.elixir
Normal file
15
Task/Collections/Elixir/collections-4.elixir
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
empty_map = Map.new #=> %{}
|
||||
kwlist = [x: 1, y: 2] # Key Word List
|
||||
Map.new(kwlist) #=> %{x: 1, y: 2}
|
||||
Map.new([{1,"A"}, {2,"B"}]) #=> %{1 => "A", 2 => "B"}
|
||||
map = %{:a => 1, 2 => :b} #=> %{2 => :b, :a => 1}
|
||||
map[:a] #=> 1
|
||||
map[2] #=> :b
|
||||
|
||||
# If you pass duplicate keys when creating a map, the last one wins:
|
||||
%{1 => 1, 1 => 2} #=> %{1 => 2}
|
||||
|
||||
# When all the keys in a map are atoms, you can use the keyword syntax for convenience:
|
||||
map = %{:a => 1, :b => 2} #=> %{a: 1, b: 2}
|
||||
map.a #=> 1
|
||||
%{map | :a => 2} #=> %{a: 2, b: 2} update only
|
||||
10
Task/Collections/Elixir/collections-5.elixir
Normal file
10
Task/Collections/Elixir/collections-5.elixir
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
empty_set = MapSet.new #=> #MapSet<[]>
|
||||
set1 = MapSet.new(1..4) #=> #MapSet<[1, 2, 3, 4]>
|
||||
MapSet.size(set1) #=> 4
|
||||
MapSet.member?(set1,3) #=> true
|
||||
MapSet.put(set1,9) #=> #MapSet<[1, 2, 3, 4, 9]>
|
||||
set2 = MapSet.new([6,4,2,0]) #=> #MapSet<[0, 2, 4, 6]>
|
||||
MapSet.union(set1,set2) #=> #MapSet<[0, 1, 2, 3, 4, 6]>
|
||||
MapSet.intersection(set1,set2) #=> #MapSet<[2, 4]>
|
||||
MapSet.difference(set1,set2) #=> #MapSet<[1, 3]>
|
||||
MapSet.subset?(set1,set2) #=> false
|
||||
9
Task/Collections/Elixir/collections-6.elixir
Normal file
9
Task/Collections/Elixir/collections-6.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
defmodule User do
|
||||
defstruct name: "john", age: 27
|
||||
end
|
||||
john = %User{} #=> %User{age: 27, name: "john"}
|
||||
john.name #=> "john"
|
||||
%User{age: age} = john # pattern matching
|
||||
age #=> 27
|
||||
meg = %User{name: "meg"} #=> %User{age: 27, name: "meg"}
|
||||
is_map(meg) #=> true
|
||||
Loading…
Add table
Add a link
Reference in a new issue