-
Notifications
You must be signed in to change notification settings - Fork 5
/
CollectionFunctions.hs
154 lines (107 loc) · 5.61 KB
/
CollectionFunctions.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module CollectionFunctions where
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Char
import Data.List as ListUtils
-- | List
-- ------------
-- | Here we cover how we can create collections in Haskell
myEmptyList = []
myList = "Paul" : []
multiValueList = ["Paul", "John", "Esther", "Luis", "Jackson"] :: [String]
-- | Add an element to the end of a list.
addValueListOutput = multiValueList ++ ["More"]
-- | Add an element to the start of a list.
--addValueListOutput = "More" : multiValueList
-- | This function get the array as input and output the first element of the list or the default value
firstOrEmpty :: [String] -> String
firstOrEmpty [] = "default"
-- | This apeans I have a list myList and firstElement is the first element of that list. Let's put it in capital case
firstOrEmpty (firstElement:myList) = map toUpper firstElement
firstElementOutput = firstOrEmpty multiValueList
-- | If we want to change the order of the list we just need to use reverse operator
reverseListOutput = reverse multiValueList
-- | Get first element of list
firstListOutput = head multiValueList
-- | Get last element of list
lastListOutput = last multiValueList
-- | Find element by index
findElementByIndex = multiValueList !! 1
-- | Join two list together
joinListOutput = ["Lets", "join"] ++ ["two", "list"]
-- | Iterate the list and put all element of list in capital case
mapListOutput = map (\element -> map toUpper element) multiValueList
-- | Iterate the list and use filter operator to check the length of every element and just add it in a new
filterListOutput = filter (\element -> length element == 4) multiValueList
-- | Check if a element is part of the collection and return a boolean
isElementThere = "Paul" `elem` multiValueList
-- | Find operator from Data.List is handy to find an element in the list passing a function that does patter matching.
-- This operator it will return a Maybe type. Check Maybe module for more info
people = ["Paul", "Peter", "John", "Sussan"] :: [String]
outputFind = find isJohnFunc people
where
isJohnFunc = \person -> person == "John"
-- | In haskell is really easy to have List of functions
listFunc = [\a -> a * 10, \a -> a * 100, \a -> a * 1000] :: [Int -> Int]
outputListFunc = map (\func -> func 2) listFunc
-- | Map
-- ------------
-- | Use operator [singleton] nn case you want to create a single key->value and keep it clear in your code.
singletonMap = Map.singleton "single_key" "single_value" :: Map String String
-- | To create a simple map we use [fromList] operator to create a list of elements, with a tuple for (key,value)
fromListMap = Map.fromList [("key", "value"), ("Paul", "PaulValue")] :: Map [Char] [Char]
-- | Add a new key->value is so simple like use [insert] operator
newAppendMap = Map.insert "New_key" "New_value" fromListMap
-- |Size operator it will tell us the size of the map
mapSize = Map.size newAppendMap
-- |To get a value from a map we just need to use [lookup] operator. This operator it will return a Maybe type.
-- In this particular example "Just Paul"
getMapValueByKey = \name -> Map.lookup name newAppendMap
getMapValueByKeyOutput = getMapValueByKey "Paul"
isMemberPresent :: String -> Bool
isMemberPresent = \name -> Map.member name newAppendMap
isMemberPresentOutput = isMemberPresent "New_key"
-- |This operator [findWithDefault] it will try to find a key and in case cannot find it, it would use the default value passed
findWithDefaultFunc = \name -> Map.findWithDefault "Default value" "foo" newAppendMap
findWithDefaultFuncOutput = findWithDefaultFunc "FooPaul"
deleteKeyFunc = \name -> Map.delete name newAppendMap
newDeletedMap = deleteKeyFunc "Paul"
phoneBook = Map.fromList [(1234, "Erik"), (5678, "Patrik")]
mainMapFeatures :: IO ()
mainMapFeatures = do
print phoneBook
print $ Map.lookup 1234 phoneBook
print $ (Map.empty :: Map.Map Int Int)
print $ Map.singleton 3 5
print $ Map.insert 1 "abc" Map.empty
print $ Map.null phoneBook
print $ Map.size phoneBook
print $ Map.toList phoneBook
print $ Map.keys phoneBook
print $ Map.elems phoneBook
-- | Pipeline
transformValuesToUpperCase = Map.map (\element -> map toUpper element) newAppendMap
transformToUpperCaseByKey = Map.filterWithKey (\key -> \a -> key == "Paul") newAppendMap
transformToUpperCaseByValue = Map.filter (\value -> value == "PaulValue") newAppendMap
composeFunc :: Int -> String -> Map [Char] [Char]
composeFunc _length _key =
Map.map (\element -> map toUpper element) $
Map.filter (\value -> length value >= _length) $ Map.filterWithKey (\key -> \a -> key == _key) newAppendMap
mapFoundByKeyAndLength = composeFunc 5 "Paul"
mapNotFoundByKeyAndLength = composeFunc 10 "Paul"
-- | Zip
-- -------
-- |[zip] operator allow us to combine two list and return a list of tuples with values from every index.
list1 = [1, 2, 3, 4, 5] :: [Int]
list2 = [6, 7, 8, 9, 10] :: [Int]
zipList = zip list1 list2
-- |[zipWith] operator allow us something similar than zip but instead create a tuple from every index element we pass
-- a function that where we specify what we want to do with every two elements in every iteration.
zipWithOutput = zipWith (\a -> \b -> a + b) list1 list2
tupleList = [(1, 4), (2, 5), (3, 6)]
-- | [unzip] operator get every element of the tuple and put array in the corresponding index position.
unzipOutput = unzip tupleList
-- | Doing recursion we sum all the elements of a list, in every recursion call we pass the sum value and also the list with one element less
sumAllElements :: Double -> [Double] -> Double
sumAllElements first (x:xs) = sumAllElements (first + x) xs
sumAllElements first [] = first