Julia Community 🟣

Arturo Erdély
Arturo Erdély

Posted on • Updated on

Dictionaries are onto functions

Dictionaries in Julia (and other programming languages) are onto functions, mathematically speaking. That is to say

f:ABf:A\rightarrow B
where ff is a function defined for every xA,x\in A, and also for every yBy\in B there exists at least one xAx\in A such that f(x)=y,f(x)=y, that is an onto (or surjective) function. The inverse image of ff is a function
where (B)\wp(B) is the collection of all the subsets of B,B, that is the power set of B,B, and where
f(1)(C)={xA:f(x)C}f^{(-1)}(C) = \{x \in A: f(x) \in C\}
for any CB.C \subseteq B. In other words, if ff is a function that maps the elements of a set AA onto a set BB and if CC is a subset of BB then the inverse image f(1)(C)f^{(-1)}(C) is a subset of AA with all the elements that are mapped by ff onto the set C.C.

If D is a Dict then D is the equivalent to ff where A=A = keys(D) and B=B = values(D). To define the inverse image function f(1)f^{(-1)} it is just necesary to code:

invImg(C::Set, D::Dict) = Set(filter(x -> D[x]  C, keys(D)))
Enter fullscreen mode Exit fullscreen mode

For example:

D = Dict([1,2,3,4,5,6,7] .=> ['a','a','b','a','b','a','c'])
C = Set(['a','c'])
invImg(C, D)
Enter fullscreen mode Exit fullscreen mode

obtaining f(1)(C)={1,2,4,6,7}f^{(-1)}(C) = \{1,2,4,6,7\} as expected.

Top comments (2)

logankilpatrick profile image
Logan Kilpatrick

I am not sure I fully grasp this, can you add some commentary for folks who don't have a rigoruous math background like myself?

aerdely profile image
Arturo Erdély

I added a comment «In other words...» I hope may help. Probably what I had in mind was more an invitation for mathematicians to see the advantages of using Julia to exemplify set theory concepts.