1

How would I make the following into the same function with dot notation?

 print $ map (`elem` Emoji.table) keywords
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
EEE
  • 149
  • 1
  • 7
  • You can think of a `$` effectively saying put everything after this in a set of brackets. – ktzr May 12 '18 at 19:04
  • I've removed part of your question that was off-topic for this site (preferences and opinions vary a lot); the other half is not a great question in my opinion, but at least on-topic, so this way your question is less likely to get closed. – Daniel Wagner May 12 '18 at 19:04

2 Answers2

4

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)
sshine
  • 15,635
  • 1
  • 41
  • 66
2
(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)

ktzr
  • 1,625
  • 1
  • 11
  • 25