Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed random module is deprecated compile warning #12

Merged
merged 2 commits into from Nov 25, 2016

Conversation

zhongwencool
Copy link
Owner

No description provided.

@zhongwencool zhongwencool changed the title fixed random module is deprecated compile warining fixed random module is deprecated compile warning Nov 24, 2016
@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.984% when pulling e0641f9 on random-deprecated-erlang-19 into 67549b5 on master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.984% when pulling cc6fc6b on random-deprecated-erlang-19 into 67549b5 on master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.984% when pulling 4ef460b on random-deprecated-erlang-19 into 67549b5 on master.

@secretworry
Copy link
Collaborator

Why don't we just implement the unique/2 like this

def unique(n) when n > 0, do: Enum.map(1..n, fn _ -> Enum.random(?a..?z) end) |> IO. iodata_to_binary
def unique(_), do: ""

The iodata_to_binary, which is a BIF, will save more memory, and be more effecient.

@zhongwencool
Copy link
Owner Author

zhongwencool commented Nov 25, 2016

@secretworry your solution just like:

defp unique(size, acc\\[])
defp unique(0, acc), do: acc |> IO.iodata_to_binary
defp unique(size, acc) do
    random = Enum.random(?a..?z)
    unique(size - 1, [random|acc])
end

the size only 16
so two method almost the same.
when the binary is small size , it's work just as list.
<<random, acc_binary::binary>> 's efficiency just as [random|acc_list]
So we don't need IO.iodata_to_binary/1

@secretworry secretworry merged commit 627751e into master Nov 25, 2016
@secretworry
Copy link
Collaborator

secretworry commented Nov 25, 2016

I did a little benchmark here, my solution is slower than the one you gave. The best would be list loop + IO.iodata_to_binary.

@zhongwencool
Copy link
Owner Author

secretworry/benchex#1
Happy to know, I will change it to list loop +:erlang.list_to_binary later.
@secretworry

@zhongwencool zhongwencool deleted the random-deprecated-erlang-19 branch November 25, 2016 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants