hazardfn
List_to_port in OTP < 20.0
Hey all,
I recently needed to get a port() from a string representation of it for…reasons. I discovered a handy little function in OTP 20.0 called list_to_port but quickly found out that ONLY works in 20.0+.
I mashed together an elixir version of term_to_port() from recon which I thought might be useful to others (not sure if this is even a common problem, common enough to be added to OTP I suppose):
def term_to_port(<<"#Port<0.", id :: binary>> ) do
n = id |> String.trim_trailing(">") |> String.to_integer()
term_to_port(n)
end
def term_to_port(n) when is_integer(n) do
name = Node.self() |> Atom.to_charlist() |> :erlang.iolist_to_binary()
length = :erlang.iolist_size(name)
self = :erlang.term_to_binary(self())
vsn = :binary.last(self)
<<131, 102, 100,
length :: size(2)-unit(8),
name :: size(length)-binary,
n :: size(4)-unit(8),
vsn :: size(8)>>
|> :erlang.binary_to_term
end
It’s very rough around the edges but useful nonetheless.
Popular in Questions
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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 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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New







