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
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition)
It’s been a while since we first asked this, I...
New
Following https://github.com/tbrand/which_is_the_fastest |>
https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
Phoenix Live View is now publicly available on GitHub.
Here’s Chris McCord’s tweet announcing making it public.
New
I wanted to capitalize a string, and tried using String.capitalize().
That generally works well, until you try to capitalize a word like...
New
Got a question about when to concat vs. prepending items to list then reversing to achieve appending.
So i know lists boil down to [1 | ...
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
Other popular topics
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
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 the...
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
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










