Skip to content

Commit ed2dc00

Browse files
committed
mention comparing
1 parent e06404e commit ed2dc00

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

source_md/modules.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,12 @@ ghci> sortBy (compare `on` length) xs
649649
Awesome!
650650
``compare `on` length`` ... man, that reads almost like real English!
651651
If you're not sure how exactly the `on` works here, ``compare `on` length`` is the equivalent of ``\x y -> length x `compare` length y``.
652-
When you're dealing with *By* functions that take an equality function, you usually do ``(==) `on` something`` and when you're dealing with *By* functions that take an ordering function, you usually do ``compare `on` something``.
652+
653+
Actually, we can do even better.
654+
The pattern ``compare `on` f`` is fairly common, so there is a shorthand for that as well: `comparing`{.label .function} from `Data.Ord`.
655+
With it, instead of using `on` we can write `sortBy (comparing length)`.
656+
657+
When you're dealing with *By* functions that take an equality function, you usually do ``(==) `on` something`` and when you're dealing with *By* functions that take an ordering function, you usually are `comparing something`.
653658

654659
## Data.Char {#data-char}
655660

0 commit comments

Comments
 (0)