sarat1669
Is there a better way to swap elements in a list?
Any inbuilt function or a library?
defmodule SwapElements do
def swap(list, first_index, second_index) do
{a, b} = split(list, first_index + 1)
{x, y} = split(b, second_index - first_index)
[h1 | t1] = a
[h2 | t2] = x
y
|> reverse_append([h1])
|> reverse_append(t2)
|> reverse_append([h2])
|> reverse_append(t1)
end
def split(list, n) do
split([], list, n)
end
def split(a, b, 0) do
{a, b}
end
def split(list, [h|t], n) do
split([h | list], t, n - 1)
end
def reverse_append(list, []) do
list
end
def reverse_append(list, [h | t]) do
reverse_append([h | list], t)
end
end
iex(27)> SwapElements.swap(Enum.to_list(0..10), 1, 4)
[0, 4, 2, 3, 1, 5, 6, 7, 8, 9, 10]
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sarat1669
Shower Thought:
If lists in beam were implemented as
XOR linked listThey can be be reversed in O(1)
al2o3cr
Here are two possible approaches using functions from
EnumandList:Beware that both of these (just like the one in your post) have
O(N)time-complexity since they have to traverse the entire list.benwilson512
Swap2 is definitely the most clear imho, nice stuff.
@sarat1669 It’s probably worth noting that if you want to perform a bunch of index based changes to a “list” you are probably better off using a map with the indices as keys rather than a list, which just really isn’t setup for efficient index based access or changes.
sarat1669
@al2o3cr Enum.split will be doing a Enum.reverse internally right?
I was trying to avoid that
Edit:
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/enum.ex#L2762
sarat1669
@benwilson512 I think the ordering will not be maintained in a Map.
It might be a good alternative for accessing and updating and not for the other cases.
egze
You could also use the
:arraymodule from Erlang.al2o3cr
It’s a tradeoff between reducing the constant factor of an
O(N)algorithm versus readability.If performance is a concern, the better solution is to pick a more-efficient data structure for the operations you want to do. For instance, the
:arraymodule mentioned by @egze uses a 10-way tree made of tuples. The source has some additional notes about why they chose 10:https://github.com/erlang/otp/blob/00503c64bc321fc1d2ff1233613debf2f579cedb/lib/stdlib/src/array.erl#L108-L126
Also note that
:array.from_listand:array.to_listboth have to traverse the whole list, so if you’re doing a lot of swaps you’ll want to keep your data in the:arraystructure.Qqwy
There are a couple of libraries implementing such higher-performance persistent sequential data structures. A couple of years back I wrote Arrays which has a single interface with pluggable backends for either
:arraysor “maps with indices as keys”, as well as implementing many useful protocols like Enumerable, Collectable, Access, etc. to allow you to keep your code idiomatic and easily change between one Enumerable backend and another.Other algorithms exist as well. For instance, there is a library called Hallux that has a sequential data structure with amortized O(1) element access based on finger trees, and PersistentVector based on 32-way tries.
al2o3cr
I don’t know when this would ever be useful, but here’s a version of
swapthat even works on infinite streams!This uses
Stream.transformwith a reducer function that implements a tiny state machine to handle the change in behavior when the two indexes are passed.It also chains:
While it’s a streaming algorithm, it still needs to hold at least
i2-i1intermediate elements in memory since it can’t produce thei1th element until it’s seen thei2th.Also beware:
Swap3.swapdoes weird things if the supplied indexes aren’t in order (i1 < i2) or are equal.dkuku
I tough recursion will be the most performant way here and I think it is but implementation is quite long.
It’s still O(n) but probably 2x as performant as twice using replace_at