[erlang-questions] Generating a list of lists containing random numbers
Raimo Niskanen
raimo+erlang-questions@REDACTED
Tue Jul 3 12:42:01 CEST 2018
On Tue, Jul 03, 2018 at 04:08:18PM +0900, zxq9@REDACTED wrote:
> On 2018年7月3日火曜日 8時33分12秒 JST Raimo Niskanen wrote:
: :
> > This may be a missing function for the lists module:
> >
> > buildr(N, Fun, Acc) ->
> > buildr(N, Fun, Acc, []).
> > %%
> > buildr(0, _Fun, Acc, R) ->
> > {R, Acc};
> > buildr(N, Fun, Acc, R) ->
> > {V, NewAcc} = Fun(Acc),
> > buildr(N - 1, Fun, NewAcc, [V|R]).
> >
> > Example usage:
> >
> > Base = 0,
> > Range = 1 bsl 32,
> > {ListOfLists, _NewSeed} =
> > buildr(
> > 1000,
> > fun (Seed) ->
> > buildr(
> > 1000,
> > %%fun rand:uniform_s/1,
> > fun (S) ->
> > {V, NewS} = rand:uniform_s(Range, S),
> > {Base + V - 1, NewS}
> > end,
> > Seed)
> > end,
> > rand:seed_s(exrop)),
> >
> >
>
> I think I'd rather have matrix operations in a matrix module.
This is not a matrix operation.
We have lists:seq/2,3 and lists:duplicate/2 but no such generating function
that takes a generator fun.
It could be named generate/3, gen/3 or build/3. I suggested buildr/3
because it builds from the right end of the list analogous to foldr/3.
A buildl/3 would be more inefficient since it should return {List,Acc}
which does not allow for a recursive variant so it would become build3/r
followed by reverse/1.
So: lists:buildr(N, GeneratorFun, Acc) -> {List, NewAcc}
with: GeneratorFun(Acc) -> {Value, NewAcc}
Might be useful enough to add...
>
> Every time I've needed to do matrix operations the need has either been:
>
> 1)
> Trivial, flat matrix operations over regular matrices of regular
> dimensions populated by scalar values & a well-defined _|_.
>
> OR
>
> 2)
> Matrices that are specialized enough that they really deserve a custom
> set of handlers all their own.
In Wings3D I wrote a matrix library that was optimized for sparse matrixes,
for the automatic U-V mapping algorithm.
Yet another specialized matrix library I guess...
/ Raimo
>
> In case 1 there are enough regular operations (especially against numeric
> values) that you could fill out a full-blown matrix_math module and still
> not cover everything. I wouldn't want that to clutter the lists module.
>
> In case 2 trying to generalize is futile because remembering the details
> of the abstractions involved is more trouble than writing a special handler,
> hence things like this:
> https://gitlab.com/zxq9/zxWidgets/blob/master/src/zxw.erl#L194
>
> I wouldn't want to attempt to generalize a text grid metawidget generator
> because the assumptions embedded in it will break in any other situation,
> but nonetheless, it is a matrix operation to generate, populate, and extract
> or modify data in it.
>
> I've got a naive matrix math module around here somewhere that I wrote as
> an example for a guy on SO a while back... but it only handles a few math
> operations, flips, inversions and rotations.
>
> Would people be interested in a matrix_math library?
>
> -Craig
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
--
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list