Monday, December 26, 2011

Everything is a function

Everything is a function. Associative arrays are just functions of their keys, lists are functions of the natural numbers, etc. These data structures are all groups of key/value pairs. There is only one problem: sometimes a value isn't given any key. In this case, we can automatically generate a key for it to turn it into a map:
(= (to-map #{1 2 3})
   {"#_2185" 3, "#_1873" 2, "#_0195" 1})
Now here is an example of the combination of a group of map structures:
(= (merge-maps {:x 10, :y 100} [1 2 3] #{1 2 3})
   {"#_0249" 1, "#_1847" 2, "#_212" 3, 0 1, 1 2, 2 3, :x 10, :y 100})
This shows how we can represent essentially any value as a map. This has the further advantage that we can easily add metadata to any object. Now a further concern comes when we want to describe links between these structures:
(add-edges [0 1] [1 2] [2 3] #{:x :y})
Any system of simple links can be represented in terms of adjacency matrices which are predicate functions on pairs on natural numbers:
[[0 1 1 0]
 [1 0 0 1]
 [1 0 0 1]
 [0 1 1 0]]

[[0]
 [1 0]
 [1 1 0]
 [1 1 1 0]]
Otherwise the links may have complicated metadata, such as labels. Furthermore, sometimes we want to deal with hypergraphs, which requires yet more complicated edge objects.

No comments:

Post a Comment