Unfortunately the documentation is not really great yet, but
https://github.com/rened/FunctionalData.jl#computing-map-and-friends-details
is a collection of functions that could be used for this, with optional
in-place operations and parallel processing. It is basically a generalization
of the pattern in your code: allocate result, and fill by iterating over the
input.
You can then write your task like this:
julia> f(x,n) = x.*(1:n)
f (generic function with 1 method)
julia> @p map 1:4 f 4
4x4 Array{Int64,2}:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
The second line is just syntax sugar for map(1:4, x->f(x,4))
(https://github.com/rened/FunctionalData.jl#pipeline-syntax-details)
It basically works on a "lists-of-items" concept, trying to keep data in
compact memory representations. In this case the map turns the list of scalars
(the unit range) into a list of column vectors (the resulting matrix).
Am 04.04.2015 um 17:04 schrieb Tamas Papp <[email protected]>:
> Hi,
>
> Suppose I have a function that maps an atom of type T, eg Float64, into
> Vector{T}, and takes another argument n that determines its length.
>
> What is the idiomatic/fast way of collecting the values in the columns
> of a matrix?
>
> Currently I am using this:
>
> @doc """Map elements of `x` into columns of a matrix using `f`.
> Result is assumed to have the same element type as `x`.""" ->
> function maptocols{T}(f,x::Vector{T},n)
> k = length(x)
> b = Array(T, n, k)
> for j = 1:k
> b[:,j] = f(x[j],n)
> end
> b
> end
>
> Eg
>
> julia> maptocols((x,n) -> x.*[1:n;], [1,2,3,4], 4)
> 4x4 Array{Int64,2}:
> 1 2 3 4
> 2 4 6 8
> 3 6 9 12
> 4 8 12 16
>
> which is OK but of course I would prefer a one-liner if there is one
> provided by the language (could not find it though).
>
> Best,
>
> Tamas