Code Monkey home page Code Monkey logo

destruct.jl's Introduction

Destruct.jl

Build Status

Destructuring arrays of tuples in Julia.

Overview

Using julia's 'dot-call' syntax on functions with multiple return arguments results in an array of tuples. Sometimes, you want the tuple of arrays instead, preserving array shape. This can be achieved using destruct, which converts an array of tuple to a tuple of arrays.

Works with any tuples (ie: with elements of different types).

This single function doesn't really require it's package, maybe you can find it a better home.

Example

julia> using Destruct; using BenchmarkTools
julia> f(a, b) = a+1im*b, a*b, convert(Int, round(a-b)); # some transform returing multiple values
julia> v = f.(rand(3,1), rand(1,4));
julia> typeof(v)
Array{Tuple{Complex{Float64},Float64,Int64},2}
julia> x, y, z = destruct(v);
julia> z
3ร—4 Array{Int64,2}:
 0  0  0  0
 1  0  1  1
 1  0  1  1
julia> v = f.(rand(500,1,1), rand(1,500,500));
julia> @btime destruct($v); # using BenchmarkTools
  1.396 s (7 allocations: 3.73 GiB)

Getting this out of the way:

julia> x, y, z = f.(rand(100,1,1), rand(1,100,100)) |> destruct;

Performance

A common way to unpack Arrays of tuples uses the broadcast dot-call:

unpack_broadcast(w::Array{<:Tuple}) = Tuple((v->v[i]).(w) for i=1:length(w[1]))

However, this approach suffers from two problems: it doesn't access the elements in the order they are stored in memory and has huge memory consumption for Tuples with varying types (Tuples instead of NTuples).

This "broadcast unpack" takes between 1.5x and 2x longer than destruct supplied here for arrays of NTuples. The performance gain is much larger for tuples of heterogenous types: in the 10x to 75x range, using 1/10th of the memory.

See timing scripts: timing.jl and comparative_timing.jl.

How does it work?

The destruct function uses macros from Base.Cartesian to allocate destination arrays and iterate over all the things. The alternative implementations using broadcast dot-call is available as Destruct.unpack_broadcast.

destruct.jl's People

Contributors

galenlynch avatar spalato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

destruct.jl's Issues

Deprecation warnings in Julia 0.7

(v0.7) pkg> test Destruct
   Testing Destruct
    Status `/tmp/tmpl0K5fY/Manifest.toml`
  [dde75ccc] Destruct v1.0.0
WARNING: Base.Test is deprecated, run `using Test` instead
  likely near /home/glynch/.julia/packages/Destruct/mKhWW/test/runtests.jl:2
WARNING: Base.AbstractRNG is deprecated: it has been moved to the standard library package `Random`.
Add `using Random` to your imports.
  likely near /home/glynch/.julia/packages/Destruct/mKhWW/test/runtests.jl:5
WARNING: Base.Complex128 is deprecated, use ComplexF64 instead.
  likely near /home/glynch/.julia/packages/Destruct/mKhWW/test/runtests.jl:6
Test Summary: | Pass  Total
NTuple        |   20     20
Test Summary: | Pass  Total
Tuple         |   48     48
Test Summary:     | Pass  Total
Tuple, Non square |   32     32
   Testing Destruct tests passed 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.