Imagine you are building a system to assign unique numbers to each resource that you manage. You want the IDs to be guaranteed unique i.e. no UUIDs. Since these ids are globally unique, each id can only be given out at most once.
Here is a summary of my solution.
def handle_call(:get_id, _from, %{node_id: node_id, counter: counter} = state) do
<<id::size(64)>> = <<node_id::10, timestamp()::47, counter::7>>
{:reply, id, %{state | counter: counter + 1}}
end
My talk at Empex 2019 is Live(View). Click on Read More to get the links to related articles and GitHub Repos.
LiveView for Phoenix on Elixir is definitely scratching an itch in the world of rich client apps, without having to go full-on client-side framework. Here’s a list of open source projects, some with online demos and other where you can (easily) run the code locally.
Screenshot | Description | References |
---|---|---|
Manipulating a SVG graphic for the 2019 Empex NY conference | Empex SVG Demo <br> Empex SVG Source |
Here’s a template for answering HackerRank in Elixir. This is based on the Two Character question
defmodule Solution do
def go() do
num = input(:int)
text = input(:string)
IO.puts("INPUTS #{num} and #{text}")
end
def input(:string), do: IO.read(:line) |> String.trim()
def input(:int), do: input(:string) |> String.to_integer()
end
# Now run your code
Solution.go()
Now go forth and HackerRank!!!
How can you have your LiveView login form update the user’s session across pages?
In your LiveView LEEX, you can add a script
tag and append a @tick
to the id
.
This will force for the MorphDOM differ to always re-render (aka re-run) that code on the client.
Today we are going explore how to write API clients in the Elixir language. This is a follow-up article to my presentation at OpenCamps 2017.
Want to automate your infrastructure leveraging the awesome Digital Ocean API V2, then the shell is your friend. Here we will learn about how to access the API from the command line using Elixir’s Escript tooling.
There is great power in having your tests always run, all the time when writing code, it stops the minor interruptions.
$ mix test.watch
Running tests...
..................................................
..................................................
....
Finished in 0.04 seconds (0.04s on load, 0.00s on tests)
104 tests, 0 failures
Randomized with seed 386800
Of course you don’t know anyone that actually stores user passwords in plaintext, or database passwords directly in a repository, so this is more for those theoretical developers to provide them with just a little bit more security; without adding much more complexity.
# Encrypt a password and store it in pwd
iex> pwd = Safetybox.encrypt("helloworld")
"fc5e038d38a57032085441e7fe7010b0"
# Later on, you can validate the user provided password
# against the encrypted stored password
# Oopses, not the same
iex> Safetybox.is_decrypted("goodbyeworld", pwd)
false
# Ok, validated!
iex> Safetybox.is_decrypted("helloworld", pwd)
true