Witchcraft v1.0.0-beta.2 Witchcraft.Semigroup View Source

A semigroup is a structure describing data that can be appendenated with others of its type. That is to say that appending another list returns a list, appending one map to another returns a map, and appending two integers returns an integer, and so on.

These can be chained together an arbitrary number of times. For example:

1 <> 2 <> 3 <> 5 <> 7 == 18
[1, 2, 3] <> [4, 5, 6] <> [7, 8, 9] == [1, 2, 3, 4, 5, 6, 7, 8, 9]
"foo" <> " " <> "bar" == "foo bar"

This generalizes the idea of a monoid, as it does not require an empty version.

Type Class

An instance of Witchcraft.Semigroup must define Witchcraft.Semigroup.append/2.

Semigroup  [append/2]

Link to this section Summary

Functions

appendenate two data of the same type. Can be chained an arbitrary number of times. These can be chained together an arbitrary number of times. For example

Flatten a list of homogeneous semigroups to a single container

Repeat the contents of a semigroup a certain number of times

Link to this section Types

Link to this section Functions

appendenate two data of the same type. Can be chained an arbitrary number of times. These can be chained together an arbitrary number of times. For example:

iex> 1 |> append(2) |> append(3)
6

iex> [1, 2, 3]
...> |> append([4, 5, 6])
...> |> append([7, 8, 9])
[1, 2, 3, 4, 5, 6, 7, 8, 9]

iex> "foo" |> append(" ") |> append("bar")
"foo bar"

Operator

iex> use Witchcraft.Semigroup
...> 1 <> 2 <> 3 <> 5 <> 7
18

iex> use Witchcraft.Semigroup
...> [1, 2, 3] <> [4, 5, 6] <> [7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

iex> use Witchcraft.Semigroup
...> "foo" <> " " <> "bar"
"foo bar"

There is an operator alias a <> b. Since this conflicts with Kernel.<>/2, use Witchcraft,Semigroup will automatically exclude the Kernel operator. This is highly recommended, since <> behaves the same on bitstrings, but is now available on more datatypes.

Flatten a list of homogeneous semigroups to a single container

Example

iex> concat [
...>   [1, 2, 3],
...>   [4, 5, 6]
...> ]
[1, 2, 3, 4, 5, 6]
Link to this function repeat(to_repeat, list) View Source
repeat(Witchcraft.Semigroup.t, [{:times, non_neg_integer}]) :: Witchcraft.Semigroup.t

Repeat the contents of a semigroup a certain number of times

Examples

iex> [1, 2, 3] |> repeat(times: 3)
[1, 2, 3, 1, 2, 3, 1, 2, 3]