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

jdumont
I could write forever about this, but I’ll do my best to keep it succinct. For anyone familiar with event sourcing, what is your opinion...
New
krainboltgreene
Today I noticed that when defining a embeds_many the new struct has a default of [] which is confusing since: You can have jsonb[] colu...
New
christopheradams
I was recently asked to step up and become the maintainer for the Elixir Style Guide. It was, I believe, the first, and is now the most p...
New
bartblast
Hologram provides a way to write your entire web application in Elixir, with automatic transpilation to JavaScript for the frontend. It h...
New
thiagomajesk
I came across this PKGX tool the other day, if anyone here is using it, could you share your experience? I’m wondering how useful it wou...
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
Crowdhailer
It is rare to use direct calls to send in elixir. The call is normally wrapped in a function which is responsible for sending the correct...
New
ronindev
Hey everyone! :waving_hand: I just wanted to recommend https://seenode.com/ for deploying Phoenix apps. I’ve been using it recently and ...
New
chrisliaw
Hi, I’m wondering is it my thinking process or this is the norm among the Elixir developer for the use of Struct and accessor functions ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement