How would I make the following into the same function with dot notation?
print $ map (`elem` Emoji.table) keywords
How would I make the following into the same function with dot notation?
print $ map (`elem` Emoji.table) keywords
You can transform the expression a little at a time:
print $ map (`elem` Emoji.table) keywords
print (map (`elem` Emoji.table) keywords)
(print . map (`elem` Emoji.table)) keywords
print . map (`elem` Emoji.table) $ keywords
When you have f $ g x
, you have f (g x)
, which means you have (f . g) x
or f . g $ x
. In the example above, where f
is print
and g
is map ('elem' Emoji.table)
, it doesn't seem like there is anything to win from expressing the composition using .
, but it might be more readable in other cases.
For example, if you were writing a point-free function:
printWithEmojis :: Keywords -> IO ()
printWithEmojis keywords = print $ map (`elem` Emoji.table) keywords
then you could also eta-reduce:
printWithEmojis :: Keywords -> IO ()
printWithEmojis = print . map (`elem` Emoji.table)
(print . map (`elem` Emoji.table)) keywords
I replaced the map operator and list to try it out and used
(print . map (==1)) [1..2]
and
print $ map (==1) [1..2]
The $
operator adds brackets around everything that follows, e.g. foo bar $ baz qux
is equivalent to foo bar (baz qux)
The .
operator is shorthand for composition, e.g. (f . g) x
is the same as f (g x)