Having worked fairly extensively with Clojure, I came to appreciate the approach of building systems focused around data vs code. For example, if you are working on a business rules engine, rather than writing a complex collection of functions and structs that describe and execute the rules, it’s better to use a simple collection of composable functions that can operate on a data structure and let the data structure describe the rules. The major advantage of this approach is that there are many more tools to work with vanilla data types (composing, splitting, iterating, etc.) than there are with a custom collection of functions & types.
The only problem I’m coming across with Julia (as compared to Clojure) is that I haven’t been able to settle on a nice, clean, ergonomic approach for constructing large, complex data literals. For example, the following code in Clojure constructs a top-level dictionary with nested dictionaries that have a mix of vectors, strings, and number literals for values:
{ :foo { :bar [1 2 3]
:baz "Hello, world"
:qux 9000 }
:bohica { :bar [4 5 6]
:baz "Goodnight, moon"
:qux 2000 } }
So, what’s the best way to do the same in Julia? One option:
Dict(:foo => Dict(:bar => [1, 2, 3],
:baz => "Hello, world",
:qux => 9000),
:bohica => Dict(:bar => [4, 5, 6],
:baz => "Goodnight, moon",
:qux => 2000))
feels just a bit too verbose (especially if, as I’m looking to do, one wants to nest Dict
s within Dict
s within Dict
s). An alternative I’ve explored is to, instead, construct the data as a matrix and then later (via function or macro, doesn’t particularly matter) convert anything with eltype
of Pair
into a dict:
[:foo => [:bar => [1 2 3]
:baz => "Hello, world"
:qux => 9000]
:bohica => [:bar => [4 5 6]
:baz => "Goodnight, moon"
:qux => 2000]]
This is only just a bit more verbose than Clojure, which I like, but I wonder if using data literals like this is too alien to the Julia community. Thoughts?