All Projects → mauro3 → UnPack.jl

mauro3 / UnPack.jl

Licence: MIT license
`@pack!` and `@unpack` macros

Programming Languages

julia
2034 projects

Projects that are alternatives of or similar to UnPack.jl

Google Ime Dictionary
日英変換・英語略語展開のための IME 追加辞書 📙 日本語から英語への和英変換や英語略語の展開を Google 日本語入力や ATOK などで可能にする IME 拡張辞書です
Stars: ✭ 30 (-59.46%)
Mutual labels:  dictionaries
Ipa Dict
Monolingual wordlists with pronunciation information in IPA
Stars: ✭ 139 (+87.84%)
Mutual labels:  dictionaries
Nspell
📝 Hunspell compatible spell-checker
Stars: ✭ 195 (+163.51%)
Mutual labels:  dictionaries
Memorize
🚀 Japanese-English-Mongolian dictionary. It lets you find words, kanji and more quickly and easily
Stars: ✭ 72 (-2.7%)
Mutual labels:  dictionaries
Box
Python dictionaries with advanced dot notation access
Stars: ✭ 1,804 (+2337.84%)
Mutual labels:  dictionaries
Sdcv
Stars: ✭ 171 (+131.08%)
Mutual labels:  dictionaries
Potchana
An open source worldwide languages dictionary plug-in for macOS. As features on major Thailand tech forums like MacThai.com
Stars: ✭ 10 (-86.49%)
Mutual labels:  dictionaries
Paroleitaliane
Liste di parole italiane
Stars: ✭ 227 (+206.76%)
Mutual labels:  dictionaries
Dictionaries
One dictionary to rule them all -- a browser extension to help you learn languages
Stars: ✭ 134 (+81.08%)
Mutual labels:  dictionaries
Xdxf makedict
XDXF — a dictionary format, that stores word definitions that are free from representation
Stars: ✭ 177 (+139.19%)
Mutual labels:  dictionaries
Glom
☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
Stars: ✭ 1,341 (+1712.16%)
Mutual labels:  dictionaries
Dynamictranslator
Instant translation application for windows in .NET 🎪
Stars: ✭ 131 (+77.03%)
Mutual labels:  dictionaries
Addict
The Python Dict that's better than heroin.
Stars: ✭ 2,141 (+2793.24%)
Mutual labels:  dictionaries
Goherokuname
[Mirror] Heroku-like Random Names in Go
Stars: ✭ 41 (-44.59%)
Mutual labels:  dictionaries
Dirstalk
Modern alternative to dirbuster/dirb
Stars: ✭ 210 (+183.78%)
Mutual labels:  dictionaries
Ext Saladict
🥗 All-in-one professional pop-up dictionary and page translator which supports multiple search modes, page translations, new word notebook and PDF selection searching.
Stars: ✭ 8,418 (+11275.68%)
Mutual labels:  dictionaries
Bitextor
Bitextor generates translation memories from multilingual websites.
Stars: ✭ 168 (+127.03%)
Mutual labels:  dictionaries
Meteor-Template-helpers
Template helpers for Session, logical operations and debug
Stars: ✭ 35 (-52.7%)
Mutual labels:  helpers
Emojipedia
MacOS X Dictionary containing Emoji and their meanings
Stars: ✭ 220 (+197.3%)
Mutual labels:  dictionaries
Emoji Ime Dictionary
日本語で絵文字入力をするための IME 追加辞書 📙 Google 日本語入力などで日本語から絵文字への変換を可能にする IME 拡張辞書です
Stars: ✭ 172 (+132.43%)
Mutual labels:  dictionaries

UnPack

Build Status Build Status Coverage pkgeval

deps version

It is often convenient to unpack some or all of the fields of a type, and pack, in the case of mutable datatypes (for immutables use Setfield.jl). This is often the case when a struct is passed into a function.

The @unpack and @pack! macros work to unpack types, modules, and dictionaries (and can be customized for other types too, see next section).

using UnPack

mutable struct Para
    a::Float64
    b::Int
end

function f!(var, pa::Para)
    @unpack a, b = pa # equivalent to: a,b = pa.a,pa.b
    out = var + a + b
    b = 77
    @pack! pa = b # equivalent to: pa.b = b
    return out, pa
end

out, pa = f!(7, Para(1,2)) # -> 10.0, Para(1.0, 77)

Example with a dictionary:

d = Dict{Symbol,Any}(:a=>5.0, :b=>2, :c=>"Hi!")
@unpack a, c = d
a == 5.0 #true
c == "Hi!" #true

d = Dict{String,Any}()
@pack! d = a, c
d # -> Dict{String,Any}("a"=>5.0,"c"=>"Hi!")

Customization of @unpack and @pack!

What happens during the (un-)packing of a particular datatype is determined by the functions UnPack.unpack and UnPack.pack!.

The UnPack.unpack function is invoked to unpack one entity of some DataType and has signature:

unpack(dt::Any, ::Val{property}) -> value of property

Note that unpack (and pack!) works with Base.getproperty. By default this means that all the fields of a type are unpacked but if getproperty is overloaded, then it will unpack accordingly.

Three method definitions are included in the package to unpack a composite type/module/NamedTuple, or a dictionary with Symbol or string keys:

@inline unpack(x, ::Val{f}) where {f} = getproperty(x, f)
@inline unpack(x::AbstractDict{Symbol}, ::Val{k}) where {k} = x[k]
@inline unpack(x::AbstractDict{<:AbstractString}, ::Val{k}) where {k} = x[string(k)]

The UnPack.pack! function is invoked to pack one entity into some DataType and has signature:

pack!(dt::Any, ::Val{field}, value) -> value

Three definitions are included in the package to pack into a mutable composite type or into a dictionary with Symbol or string keys:

@inline pack!(x, ::Val{f}, val) where {f} = setproperty!(x, f, val)
@inline pack!(x::AbstractDict{Symbol}, ::Val{k}, val) where {k} = x[k]=val
@inline pack!(x::AbstractDict{<:AbstractString}, ::Val{k}, val) where {k} = x[string(k)]=val

More methods can be added to unpack and pack! to allow for specialized unpacking/packing of datatypes. Here is a MWE of customizing unpack, so that it multiplies the values by 2:

using UnPack
struct Foo
    a
    b
end
p = Foo(1, 2)
@unpack a, b = p
a, b # gives (1, 2)

# Now we specialize unpack for our custom type, `Foo`
@inline UnPack.unpack(x::Foo, ::Val{f}) where {f} = 2 * getproperty(x, f)
@unpack a, b = p
a, b # now gives (2, 4)

Related

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].