tmbb

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

Where Next?

Popular in Discussions Top

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
laiboonh
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
Fl4m3Ph03n1x
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
crispinb
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
AstonJ
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
saif
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
restack_oslo
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
paulanthonywilson
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 Top

danschultzer
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...
548 29377 241
New
vertexbuffer
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
albydarned
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aesmail
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
gausby
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...
1207 39297 209
New
shijith.k
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lanycrost
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

We're in Beta

About us Mission Statement