Haskell Monoids
You've seen that <>
syntax and noticed it is imported from Data.Monoid
?
I've always thought <>
was a pretty complex mathematical function and it was very odd that people were using it for Text
values, like "whatever " <> textValue <> " end."
.
It turns out Text
is a Monoid. That means it implements the Monoid class (or typeclass), that means it has a particular way of being concatenated. Any list could be a Monoid, any abstraction you can think of for which it makes sense to concatenate could be a Monoid, and it would use the same <>
syntax. What exactly <>
would do with that value when concatenating depends on its typeclass implementation of Monoid.
We can assume, for example, that Text
implements Monoid by just joining the text bytes, and now we can use <>
without getting puzzled about it.
Published at
2024-01-14 13:55:28 GMTEvent JSON
{
"id": "286e0d4f0e7a4d2f1bb8e42b80da021ac1b7f729f9bbcd425178fddd1197152e",
"pubkey": "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
"created_at": 1705240528,
"kind": 30023,
"tags": [
[
"d",
"1ccf609a"
],
[
"title",
"Haskell Monoids"
],
[
"published_at",
"1539302400"
]
],
"content": "\n# Haskell Monoids\n\nYou've seen that `\u003c\u003e` syntax and noticed it is imported from `Data.Monoid`?\n\nI've always thought `\u003c\u003e` was a pretty complex mathematical function and it was very odd that people were using it for `Text` values, like `\"whatever \" \u003c\u003e textValue \u003c\u003e \" end.\"`.\n\nIt turns out `Text` is a Monoid. That means it implements the Monoid class (or typeclass), that means it has a particular way of being concatenated. Any list could be a Monoid, any abstraction you can think of for which it makes sense to concatenate could be a Monoid, and it would use the same `\u003c\u003e` syntax. What exactly `\u003c\u003e` would do with that value when concatenating depends on its typeclass implementation of Monoid.\n\nWe can assume, for example, that `Text` implements Monoid by just joining the text bytes, and now we can use `\u003c\u003e` without getting puzzled about it.\n",
"sig": "3ccde876765eb49c3ea84af3daeff40c6ba46f0df00727b6f26ed356e1da15fd5846d1a73b0d502f169f360f53a00670e8375f5d4ba9e060af1805da2ca4dda0"
}