Why does the code of line
generateScripts pb = (greet <$>) <$> (maybeName <$> pb
have an extra functor after greet
? Based on my understanding, I know that the LHS function will be used on the RHS but since there is an extra functor, I don't understand how it works.
maybeName :: (String, String) -> Maybe String
maybeName p = if length (snd p) == 10 then Just (fst p) else Nothing
generateScripts :: [(String, String)] -> [Maybe String]
generateScripts pb = (greet <$>) <$> (maybeName <$> pb)
where greet = ("Hello "++)
phonebook = [ ("Bob", "0178866524"), ("Fred", "01624556442"), ("Alice", "0188998533") ]
GHCi> phonebook = [ ("Bob", "0178866524"), ("Fred", "01624556442"), ("Alice", "0188998533") ]
GHCi> generateScripts phonebook
[Just "Hello Bob",Nothing,Just "Hello Alice"]
(greet <$>)
is a function which apply to each elements of type Maybe String, and the (greet <$>) <$>
apply to whole List, i.e. [Maybe String], as illustration
(greet <$>) <$> (maybeName <$> pb)
= (greet <$>) <$> [Just "Bob", Nothing, Just "Alice"]
= [greet <$> Just "Bob", greet <$> Nothing, greet <$> Just "Alice"]