billylanchantin

billylanchantin

Have you seen any code that you think came from "The Book"?

If you’re not familiar with this fun bit of math lore,

Paul Erdős, the famously eccentric, peripatetic and prolific 20th-century mathematician, was fond of the idea that God has a celestial volume containing the perfect proof of every mathematical theorem. “This one is from The Book,” he would declare when he wanted to bestow his highest praise on a beautiful proof.

Have you come across any Elixir code that you think could’ve come from our version of “The Book”?

Most Liked

dimitarvp

dimitarvp

Quick sort comes to mind. It’s beautiful. Bisecting stuff just feels like magic; an universal algorithm to look for problems and zero in on them.

billylanchantin

billylanchantin

I’ll nominate the following way to generate all permutations:

def permutations([]), do: [[]]
def permutations(list), do: for x <- list, rest <- permutations(list -- [x]), do: [x | rest]

I expected it to be so much more complicated than it was!

billylanchantin

billylanchantin

It’s one of those things that sounds simple and has a solution that looks simple, but actually coming up with the solution is less obvious.

Yes! This is actually 100% analogous to a problem that I deal with frequently at work: do two time ranges overlap? So our codebase is littered with this kind of check.

This is also (partially) why I wrote CompareChain. This kind of bounds checking is so much more annoying when you can’t use the infix structural operators like >=.

While I’m at it, have you come across the Allen's interval algebra - Wikipedia? It’s a generalization of the Law of trichotomy - Wikipedia to 1D intervals. In interval-land, two intervals fall into one of 13 specific cases (or 26 if you allow 0-width intervals) instead of just 3. This isn’t from The Book per se, but the number 13 always struck me as surprising for some reason.

garrison

garrison

One-dimensional line intersection. It’s one of those things that sounds simple and has a solution that looks simple, but actually coming up with the solution is less obvious.

I actually got to use this one in database code not too long ago to intersect key ranges, so it’s not only for graphics!

def intersects?({a1, a2}, {b1, b2}) do
  a2 >= b1 and b2 >= a1
end

Incidentally I messed around with game engine/physics engine stuff a while ago and I was absolutely traumatized to learn that basically all 2D (and 3D) graphics stuff looks like this. I don’t know what I was actually expecting but somehow I thought there would be a more elegant solution for collision detection than “check if every single point is on the other side of every single line” and so on. But there isn’t!

We’re lucky computers are so fast.

Where Next?

Popular in Discussions Top

rump13
Hi everyone, I’ve been following Elixir since around 2016 and tinkering with it on and off, but I haven’t had the opportunity to use it ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
arcanemachine
Just wondering what the community currently thinks, prefers, and/or recommends for working with UI components. (e.g. daisyUI, MishkaChele...
New
derpycoder
So, anyone got a chance to look at this?!? I’m kind of glad this came along. We can just throw this into our Auth pages and won’t have t...
New
sym_num
I created a Forth processor in Elixir. This is my hobby project. https://github.com/sasagawa888/Forth
New
lessless
I wonder if it’s possible to mimic a simple “class reopening”/inhertinace-based SEAM in Elixir to alter a module’s behaviour without edit...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
travisf
In upgrading from Ecto 2 to Ecto 3 I’m dealing with a failing test. If I use ExMachina build(:address) I’ll get an error trying to put_e...
New
karlosmid
The |&gt; operator appears in many languages, mostly in the functional world. F# has essentially the exact same operator, as does OCaml. ...
New
isaacsanders
When I try to run my applications and I haven’t updated the dependencies, the output tells me to run mix deps.get. Why doesn’t the binar...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

We're in Beta

About us Mission Statement