Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

MapSets and DataStructures

mapsets_and_datastructures.livemd

MapSets and DataStructures

Section

# order + duplicates
[1, 2, 3, 4, 5]

# not ordered, only unique
MapSet.new(1..100)
MapSet.new(1..100) |> MapSet.to_list()
[%{}, {1, 2}]
[one: 1, two: 2]
[{:one, 1}, {:two, 2}]
mapset = MapSet.new([1, 2, 3])
mapset = MapSet.put(mapset, 4)
MapSet.delete(mapset, 2)
IO.inspect(mapset)
MapSet.filter(mapset, fn int -> int <= 2 end)
MapSet.member?(MapSet.new([1, 2, 3]), 5)
1 in [1, 2, 3]

Enum.member?([1, 2, 3], 1)
Enum.filter(MapSet.new([1, 2]), fn int -> int == 1 end)
Enum.member?(MapSet.new([1, 2]), 1)
# O(1) Constant
# pigeon carrying a usb
# 10gb/s

# O(log n)
# 10 -> 1
# 20 -> 2
# 40 -> 3
# 80 -> 4

# O(n)
# 10 -> 10
# 20 -> 20
# 40 -> 40
len = 10_000_000
list = Enum.to_list(1..len)
mapset = MapSet.new(1..len)
:timer.tc(fn -> Enum.member?(list, len) end) |> IO.inspect()
:timer.tc(fn -> MapSet.member?(mapset, len) end)

Lists Vs Keyword List

# list
[1, 2, 3, 1, 2, 3]
%{key: "value"}
[key: "value"]
# a list with key value pairs
[key: "value"]
[{:key, "value"}]
almost_keyword_list = [{:key, "value"}, {:key2, "value"}, 2]
# syntax sugar
keyword_list = [2, key1: "value", key2: "value"]

Keyword.keyword?(keyword_list)
[one: 1, one: 1]
large_list = Enum.to_list(1..10_000_000)
:timer.tc(fn -> Enum.at(large_list, 0) end)
Enum.reduce(1..10000, fn acc, elem ->
  Map.put(acc, elem, elem)
end)

%{one: 3, one: 2}
[2, key: "value"]
%{key: "value"}
map = %{:key => "value"}

map[:key]
map.key
map = %{%{} => "value", key: "value"}

# map["key"]
# map.key