# Data Structures and Algorithms with Elixir

**URL:** <https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685>\
**Category:** Backend Chat/Discussions\
**Tags:** algorithms, elixir, data-structures\
**Created:** [26 November 2021 23:21 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685 "2021-11-26T23:21:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [26 November 2021 23:21 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/1 "2021-11-26T23:21:33Z")

</div>

This is going to be a long an frequently posted thread.

While talking to a friend of mine who has taken data structure and algorithm course, I realized, whenever I “think” about algorithms, Java is the first thing that comes to mind. While I did solve a lot of programming puzzles in functional programming languages, I don’t remember ever taking myself through the shoes of course takers of those courses in a functional (or even dynamic) languages.

So this is what I am doing, I am going to read about an algorithm or two per week, and then try to see it through Elixir. And share the code, and leave myself notes for here, unless I can organize those thoughts well enough to put up in my blog.

I already started this few days ago and made okay progress. Here’s the repository.

> **[GitHub - code-shoily/ex\_algo: Data Structures and Algorithms implemented with...](https://github.com/code-shoily/ex_algo)**
>
> Data Structures and Algorithms implemented with Elixir - GitHub - code-shoily/ex\_algo: Data Structures and Algorithms implemented with Elixir

I have a few naively implemented algorithms out there, will regularly revise them or update new ones.

Not using any book as reference yet, but when I will I would draw inspiration from the following books:

1. [A Common-Sense Guide to Data Structures and Algorithms, Second Edition: Level Up Your Core Programming Skills by Jay Wengrow (pragprog.com)](https://pragprog.com/titles/jwdsal2/a-common-sense-guide-to-data-structures-and-algorithms-second-edition/)
2. [Advanced Algorithms and Data Structures (manning.com)](https://www.manning.com/books/advanced-algorithms-and-data-structures)

And when I find the courage:

1. [Purely Functional Data Structures: Okasaki, Chris: 9780521663502: Books - Amazon.ca](https://www.amazon.ca/Purely-Functional-Structures-Chris-Okasaki/dp/0521663504)

I do not intend this to be used by anyone seriously as this will never be “production-ready”, this repository is to keep myself educated about algorithms and thinking through zippers and lenses.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.devtalk.com/user_avatar/forum.devtalk.com/astonj/32/7_2.png) [@AstonJ](https://forum.devtalk.com/u/AstonJ)\
**Post date:** [26 November 2021 23:24 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/2 "2021-11-26T23:24:54Z")

</div>

I look forward to following your progress Mafinar!

Sounds like a good title for a book too - who knows, maybe you’ll end up writing it one day!? :003:

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [26 November 2021 23:34 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/4 "2021-11-26T23:34:30Z")

</div>

Sure after a few years!

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [27 November 2021 14:11 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/5 "2021-11-27T14:11:37Z")

</div>

Dealt with `CircularList` yesterday, and while playing around with it in the REPL, realized an important issue I overlooked in the `LinkedList` implementation.

The most joyful part of the work so far was the property testing bit. Things like, “if I keep checking the `nth` element of a `CircularList` that goes beyond the length, the n-th element will really be n module length.” is something where I can just use `StreamData` and not waste my brain cycles coming up with examples.

Not using `PropEr` but `StreamData` as I found the latter to be friendlier to my Elixir mind (OMG I almost typed `Pythonic` here! The past year I’ve been doing Python and I think that is taking over my muscle memory now!)

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [27 November 2021 14:14 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/6 "2021-11-27T14:14:35Z")

</div>

Today I intend to do a `BidirectionalList` – Zipper if you will! I think Zipper is one of the best things happening to functional programmers. Every time I read [Huet’s paper](https://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf), I feel that aha moment I felt the first time, such a clever way to look into data structures!

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [27 November 2021 14:15 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/7 "2021-11-27T14:15:43Z")

</div>

I won’t lie, reading up all these blog posts and papers makes me want to understand Haskell.

---

<div class="post-metadata">

**Author:** ![DevotionGeo](https://forum.devtalk.com/user_avatar/forum.devtalk.com/devotiongeo/32/1140_2.png) [@DevotionGeo](https://forum.devtalk.com/u/DevotionGeo)\
**Post date:** [28 November 2021 14:19 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/8 "2021-11-28T14:19:24Z")

</div>

> [@AstonJ](#):
>
> Sounds like a good title for a book too

@mafinar write a book on Data Structure and Algorithms in Erlang/Elixir and we’ll be your first readers. 🙂

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [28 November 2021 16:26 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/9 "2021-11-28T16:26:52Z")

</div>

I just started reading Purely Function Data Structure. Such a good book, I had FUD around it for nothing. While not sure about my having to do anything wuth it- I agree we need an Elixir + Algorithm book.

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [19 December 2021 07:25 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/10 "2021-12-19T07:25:25Z")

</div>

Advent of Code was keeping me busy. Also, I am refactoring [this](https://github.com/code-shoily/covid19) which is keeping me busier. However, when I was solving an Advent of Code problem few minutes ago, which made me (lazily) use libgraph and its `Graph.strong_components`, I realized, I should be implementing that by hand when I have the time. Kosaraju’s algorithm comes to mind.

I also started reading Purely Functional Data Structures and I think it’s pretty well written and (thus far) accessible for all levels.

Some algorithms are pretty hard to think of from a functional point of view. For example, bubble sort is easiest sorting algorithm to implement but took me awhile to think of it without a swap and a for-loop. Same goes for Circular and Bidirectional Linked List. While I implemented them through Zippers (one of the best ways to think of things), it’s not your traditional “linked list” but more like a navigable list.

Going to start a new blog site from next year - dedicated to Elixir (not just algorithms but everything, I took huge inspiration from [https://fsharpforfunandprofit.com/](https://fsharpforfunandprofit.com/) and I want to do something similar (and if I’m having a good day, with 0.001% of the quality) for Elixir.

---

<div class="post-metadata">

**Author:** ![DevotionGeo](https://forum.devtalk.com/user_avatar/forum.devtalk.com/devotiongeo/32/1140_2.png) [@DevotionGeo](https://forum.devtalk.com/u/DevotionGeo)\
**Post date:** [19 December 2021 08:28 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/11 "2021-12-19T08:28:58Z")

</div>

> [@mafinar](#):
>
> For example, bubble sort is easiest sorting algorithm to implement but took me awhile to think of it without a swap and a for-loop.

In your blog posts, you can show different approaches and then **the proper functional way**. That will make it the best resource for DS&A in Elixir.

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [19 December 2021 08:32 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/12 "2021-12-19T08:32:53Z")

</div>

Yes, that’s the plan. Also, would make good use of Benchmarking and Property Based Testing while at it.

---

<div class="post-metadata">

**Author:** ![DevotionGeo](https://forum.devtalk.com/user_avatar/forum.devtalk.com/devotiongeo/32/1140_2.png) [@DevotionGeo](https://forum.devtalk.com/u/DevotionGeo)\
**Post date:** [19 December 2021 08:34 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/13 "2021-12-19T08:34:07Z")

</div>

The good thing is that if you write your blog posts the way you planned, you can later combine those blog posts and that will become a great book. 🙂

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [24 December 2021 00:40 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/14 "2021-12-24T00:40:15Z")

</div>

Got these two to accompany me throughout 2022 during this. Going to post my first one on Sunday.

 ![20211223_165640](https://forum.devtalk.com/uploads/default/original/2X/c/c8f01318f8f06ca3cb2c8ce552fda291b00e814a.jpeg)

---

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.devtalk.com/user_avatar/forum.devtalk.com/astonj/32/7_2.png) [@AstonJ](https://forum.devtalk.com/u/AstonJ)\
**Post date:** [24 December 2021 00:47 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/15 "2021-12-24T00:47:08Z")

</div>

Nice! I will be looking forward to reading what you think of them! :003:

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [7 March 2022 03:20 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/16 "2022-03-07T03:20:44Z")

</div>

**td;dr** I’m back and expect lots of algorithm + elixir posts here 🙂

So I was burned out and depressed since December. Which is why last year’s AoC thread saw me suddenly vanish, I was less chatty and i had almost 0 personal project contribution this year, until today 😃

Been feeling better since mid-feb and am fully motivated and energetic since last week. I started with a lighter data structure like Min/Max Stack and algorithm like Catalan number.

Planning on dealing with Disjoint Set, Lavenstein and starting off with Graph from next week. Stay tuned.

---

<div class="post-metadata">

**Author:** ![andrea](https://forum.devtalk.com/user_avatar/forum.devtalk.com/andrea/32/6677_2.png) [@andrea](https://forum.devtalk.com/u/andrea)\
**Post date:** [7 March 2022 06:42 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/17 "2022-03-07T06:42:12Z")

</div>

Yes, there should be a book on this using Elixir 🙂

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [7 March 2022 15:17 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/18 "2022-03-07T15:17:23Z")

</div>

Update March 7, 2022

So yesterday I implemented Catalan Number. It was quite fun. The recursive version choked after awhile but the dynamic programming version crunched away with a decent `O(nn)` complexity.

There’s more improved algorithm for this which I will visit in the future.

Today, I take on Union Find algorithm. Disjoint sets are fun and they can be prerequisite to future works like cycle detection or minimum spanning tree so looking forward to this. Also, I will probably try linking this library to my Advent of Code one (already ported some algorithms from that repository into here like the Chinese Remainder).

---

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.devtalk.com/user_avatar/forum.devtalk.com/astonj/32/7_2.png) [@AstonJ](https://forum.devtalk.com/u/AstonJ)\
**Post date:** [7 March 2022 16:18 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/19 "2022-03-07T16:18:25Z")

</div>

> [@mafinar](#):
>
> So I was burned out and depressed since December. Which is why last year’s AoC thread saw me suddenly vanish, I was less chatty and i had almost 0 personal project contribution this year, until today 😃

I wondered where you got to! Hope you’re feeling better now 🤗

We defo need to get some book clubs back on the go! (Currently in the middle of Prag Dave’s Elixir course, but Im definitely up for more once I’ve finished it)

---

<div class="post-metadata">

**Author:** ![mafinar](https://forum.devtalk.com/user_avatar/forum.devtalk.com/mafinar/32/723_2.png) [@mafinar](https://forum.devtalk.com/u/mafinar)\
**Post date:** [8 March 2022 00:57 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/20 "2022-03-08T00:57:39Z")

</div>

Implemented Union/Find. Going to move over to Graph Algorithms from this week onwards.

[DisjointSet](https://github.com/code-shoily/ex_algo/blob/main/lib/ex_algo/set/disjoint_set.ex)

This is so much fun. Missed these things.

@AstonJ looks like the idea of writing a book on the topic isn’t as out of the world as I thought. Maybe in 2023, depending on how it goes.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.devtalk.com/user_avatar/forum.devtalk.com/astonj/32/7_2.png) [@AstonJ](https://forum.devtalk.com/u/AstonJ)\
**Post date:** [8 March 2022 16:57 UTC](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685/21 "2022-03-08T16:57:36Z")

</div>

Awesome - I shall look forward to it :003:

[Next page](https://forum.devtalk.com/t/data-structures-and-algorithms-with-elixir/30685.md?page=2)
