hst337
Magic functions you probably didn't know about
Magic BIFs and NIFs in OTP and Elixir
For the last few weeks I was working on purity and effects analyzer of Elixir and Erlang projects, and I came across a bunch of interesting functions which were built-in but undocumented or hidden.
Please, do not use them in production systems because they’re a subject to change
But don’t hesitate to play with them to check how the erlang runtime works, or make some dark magic there
:erts_internal.cmp_termcompares two terms in oldschool-1,0,1fashion:erts_internal.term_type, function which returns a top-level type of the structure. Interesting thing is that for[]it returnsniland fornilit returns:atom. This function is really useful for debugging of binaries:erts_internal.map_to_tuple_keysto show keys tuple for flatmaps (maps with <= 32 keys):erts_internal.map_hashmap_childrento show the Tree representation of the hashmap (map with > 32 keys):erts_internal.ets_super_userto make the current process become a ETS process with maximum privileges. Process will have read/write access to any ets table (even protected and private ones):binary.referenced_byte_size(documented) to check a size of a referenced binary:prim_eval.recevewhich works like a normalreceive, but allows to decide whether to pick or not to pick the message from the queue to the closure rather then a pattern. It allows to peek in the head of the queue, without taking the value out of queue
try do: :prim_eval.receive(fn x -> throw(x) end, 0), catch: (x -> x)
:erts_debug.sameto check if the values are represented by the exactly same structure in memory (this means that pointers for heap values are equal):erts_debug.sizeand:erts_debug.flat_sizeto check the size with term reusage and flat size like if every term was referenced only once.
If you know any other magic functions, please post them in this thread ![]()
Most Liked
sabiwara
Elixir Core Team
This one is documented but :erlang.phash2/2 can be quite useful to hash any arbitrary term as an integer within a range.
iex> :erlang.phash2({:works, %{with: ["just", "anything"]}}, 1000)
915
# default range of 2^27
iex> :erlang.phash2({:works, %{with: ["just", "anything"]}})
28206459
7
evadne
I have a practical one:
:erlang.display_string '\a'
![]()
3
Popular in Discussions
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Following https://github.com/tbrand/which_is_the_fastest |>
https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
Erlang :list.nth simple, but 1 - based
nth(1, [H|_]) -> H;
nth(N, [_|T]) when N > 1 ->
nth(N - 1, T).
Elixir Enum.at … coo...
New
Please, let me know if this kind of discussion already took place in another topic
.
Hi all, how do you consider if is better to build ...
New
Hey everyone, this has been on my mind for some time and I’d love your input on it!
TLDR: I feel like maps are superioer for storing and...
New
A couple of days ago I was discussing with a friend about different approaches to write microservices.
He said that if he was going to w...
New
What configs will make sense to put to runtime.exs?
–
A bit of how I configure apps:
I have generic configs in config/config.exs,
dev...
New
If so I (and hopefully others!) might have some tips for you :slight_smile:
But first, please say which area you’re finding most challen...
New
Sobelow
Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









