Julia Community 🟣

Arturo Erdély
Arturo Erdély

Posted on • Edited on

Dictionaries are onto functions

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

f:A→Bf:A\rightarrow B
where ff is a function defined for every x∈A,x\in A, and also for every y∈By\in B there exists at least one x∈Ax\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
f(−1):℘(B)→℘(A)f^{(-1)}:\wp(B)\rightarrow\wp(A)
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)={x∈A:f(x)∈C}f^{(-1)}(C) = \{x \in A: f(x) \in C\}
for any C⊆B.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)

Collapse
 
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?

Collapse
 
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.