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

map which returns tuples of vectors would also be nice #2

Open
pdeffebach opened this issue Dec 14, 2020 · 5 comments
Open

map which returns tuples of vectors would also be nice #2

pdeffebach opened this issue Dec 14, 2020 · 5 comments

Comments

@pdeffebach
Copy link

Thanks for making Unzip. I appreciate it.

My main use-case for unzip, though, is returning a tuple of vectors from map rather than a vector of tuples. Would this be possible to add to Unzip?

@pdeffebach pdeffebach changed the title map whic returns tuples of vectors would also be nice map which returns tuples of vectors would also be nice Dec 14, 2020
@bramtayl
Copy link
Owner

I think it should work already! You could even use Iterators.map for even more performance. Do you have an example?

@pdeffebach
Copy link
Author

pdeffebach commented Dec 14, 2020

No, I mean a version of map that does it automatically, i.e.

julia> function maptup(f, args...)
           out = Tuple.(map(f, args...))
           unzip(out)
       end
maptup (generic function with 1 method)

julia> a, b = maptup(rand(10)) do x
           (x + 1, x - 1)
       end
([1.4738536202542285, 1.865283772114408, 1.1367533401175127, 1.680097789641812, 1.6853770322606707, 1.6453793708506164, 1.8524757820092266, 1.4489005984590795, 1.693449394611504, 1.8753358407742735], [-0.5261463797457715, -0.134716227885592, -0.8632466598824873, -0.3199022103581881, -0.3146229677393293, -0.35462062914938364, -0.14752421799077342, -0.5510994015409205, -0.3065506053884961, -0.12466415922572649])

If people are so concerned about performance in the julia issue, then better to attack the problem directly by avoidingthe intermediate allocation of out.

This is my main use-case for wanting unzip, so maybe asking for unzip is a bit of an x-y problem

@bramtayl
Copy link
Owner

Right, this should work out of the box:

@btime a, b = unzip(Iterators.map(rand(10)) do x
    (x + 1, x - 1)
end)
# 216.867 ns (4 allocations: 720 bytes)

@pdeffebach
Copy link
Author

oh, cool.

One final motivation for having this in its own function (beyond saving typing), is that this could potentially also be a map! version of this as well. Where you fill in elements of a named tuple. This can probably be it's own package though.

@bramtayl
Copy link
Owner

Hmm, actually, I think that's also possible using some of the internals of this package.

using Unzip: Rows
vectors = Rows(((Vector{Int}(undef, 10), Vector{Int}(undef, 10))))
map!(x -> (x - 1, x + 1), vectors, 1:10)

I suppose I could write up a docstring for Rows and export it?

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

No branches or pull requests

2 participants