tmbb
Utility macro for non-deterministic decisions
I’ve written the following very simple utility macro to generate proabbilitic fake data for a project and thought other people might find it interesting. It doesn’t make sense to package such a simple macro but it might make sense to include it in a larger package such as Faker. The implementation speaks for itself:
defmodule ProbabilityDemo do
defmacro with_probability(p, [do: do_body, else: else_body]) do
quote do
if :rand.uniform() < unquote(p) do
unquote(do_body)
else
unquote(else_body)
end
end
end
end
You use it like this
iex(2)> import ProbabilityDemo
ProbabilityDemo
iex(3)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(4)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(5)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(6)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(7)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(8)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(9)> with_probability 0.3 do "Success!" else "Failure!" end
"Failure!"
iex(10)> with_probability 0.3 do "Success!" else "Failure!" end
"Success!"
iex(11)> with_probability 0.3 do "Success!" else "Failure!" end
"Success!"
It saves you from writing something like the following and makes the intent clearer
# Is the first outcome success or failure? should it be < or >?
if :rand.uniform() < prob do
# something in case of success
else
# something in case of failure
ned
In practice I’m using it for things like:
gender = with_probability 0.35 do :female else :male end
Popular in Discussions
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
Hi everyone, im working on find best language/framework/system for
high concurrency, high performance and stable performance
after wor...
New
Hi all,
I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
Background
A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell.
I...
New
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Hello,
Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things.
But I don’t think this is ...
New
Other popular topics
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
- #javascript
- #code-sync
- #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








