https://github.com/dreamingechoes/30-seconds-of-elixir-code
Curated collection of useful Elixir snippets that you can understand in 30 seconds or less.
https://github.com/dreamingechoes/30-seconds-of-elixir-code
code elixir-lang learning-resources snippets snippets-collection
Last synced: 10 months ago
JSON representation
Curated collection of useful Elixir snippets that you can understand in 30 seconds or less.
- Host: GitHub
- URL: https://github.com/dreamingechoes/30-seconds-of-elixir-code
- Owner: dreamingechoes
- License: mit
- Created: 2018-11-08T00:05:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-22T14:35:52.000Z (over 7 years ago)
- Last Synced: 2025-06-30T07:48:30.786Z (12 months ago)
- Topics: code, elixir-lang, learning-resources, snippets, snippets-collection
- Size: 54.7 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

# 30 Seconds Of Elixir Code
[](https://opensource.org/licenses/MIT) [](http://makeapullrequest.com)
> A curated collection of useful Elixir snippets that you can understand in 30 seconds or less.
**Note**: This project is inspired by [30 Seconds Of Code](https://github.com/30-seconds/30-seconds-of-code), but there's no affiliation of any kind with that project.
:sparkles: **Always work in progress** :sparkles:
## Table of Contents
### 📚 Array
View contents
* [`all_equal`](#all_equal)
* [`array_to_csv`](#array_to_csv)
* [`bifurcate`](#bifurcate)
---
## 📚 Array
### all_equal
Check if all the elements in an array are equal.
```elixir
def all_equal(arr), do: arr |> Enum.dedup() |> Enum.count() == 1
```
Examples
```elixir
all_equal([1, 2, 3, 4, 5, 6]) # false
all_equal([1, 1, 1]) # true
```
[⬆ Back to top](#table-of-contents)
### array_to_csv
Converts a 2D array to a comma-separated values (CSV) string.
```elixir
def array_to_csv(arr), do: arr |> Enum.join(",")
```
Examples
```elixir
array_to_csv([1, 2, 3, 4]) # "1,2,3,4"
```
[⬆ Back to top](#table-of-contents)
### bifurcate
Splits values into two groups. If an element in filter is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
```elixir
def bifurcate(arr, predicate) do
Enum.reduce(
arr,
%{a: [], b: []},
fn x, ac ->
if (predicate.(x)) do
%{a: [x] ++ ac[:a], b: ac[:b]}
else
%{a: ac[:a], b: [x] ++ ac[:b]}
end
end
)
end
```
Examples
```elixir
bifurcate([1, 2, 3, 4], fn x -> rem(x, 2) == 0 end)
# %{a: [4, 2], b: [3, 1]}
```
[⬆ Back to top](#table-of-contents)
## Contributing
You can contribute to this project by [sending a pull request](https://github.com/dreamingechoes/30-seconds-of-elixir-code/pull/new/master) with your new code snippets, or by [creating a new issue](https://github.com/dreamingechoes/30-seconds-of-elixir-code/issues/new) if you want that a new section or snippet is added. Here you have the alphabetical [list of contributors](CONTRIBUTORS.md) of this repository.
----------------------------
This project was developed by [dreamingechoes](https://github.com/dreamingechoes).
It adheres to its [code of conduct](https://github.com/dreamingechoes/base/blob/master/files/CODE_OF_CONDUCT.md) and
[contributing guidelines](https://github.com/dreamingechoes/base/blob/master/files/CONTRIBUTING.md), and uses an equivalent [license](https://github.com/dreamingechoes/base/blob/master/files/LICENSE).