Drawn with Luxor.jl
Merry Christmas! I wrote a little Julia code.
The inspiration
Been getting frustrated with Python’s type hinting system. I usually start with loose and informal code, but eventually I specify types. And when I do, I want the language to check my work. I dislike relying on an external tool like MyPy that runs separately.
I’ve also been looking at Pandas a lot recently for work stuff. Okay, Pandas looks interesting to my non-data science brain. I mainly use it to filter Excel files for database updates. But I can’t help noticing how often the Julia programming language comes up in those posts about Data Science in Python.
So I check out Julia. It intrigues me. The type system and concurrency tools look nice, of course. But what’s this? Math code that looks more like math?
By way of contrast, this is Python’s equivalent of those two lines of Julia code.
It’s similar enough that I don’t feel massively disoriented. But the math stuff is just a little bit friendlier.
Time to run through the “is this language worth my time” checklist.
- Julia is well-documented
- even though scientific programming is Julia’s main niche, it includes a solid base and standard library for the general-purpose utility code I write
- the package ecosystem looks healthy
- I found at least one useful-looking Web framework
- I found at least one high-level library to interact with assorted database servers
- and — of course — somebody’s written a Static Site Generator in Julia, called Franklin.jl
So yeah. I can poke around a little more.
I love literate programming. One of the first things I did was look to see if someone in the Julia world did too. And they do!
There’s Literate.jl, which processes Markdown and code in Julia scripts. Weave.jl is more my style, processing Julia code in Markdown files. I can write my post and weave it into an ordinary-looking Markdown file. Hugo won’t have to know the difference.
The setup
Julia treats environment and package management as core functionality. Everything I need is in Pkg. Not to pick too much on Python — it really is a great language — but its environment management options are infamously byzantine.
To set up a package for my existing site, I drop into the REPL’s pkg
mode.
Here I can initialize my project and add dependencies.
Now I have Project.toml
and Manifest.toml
files describing my Hugo site’s new Julia needs. I can start writing this post.
Writing with Weave
Write the stuff you want to write, using Julia-flavored Markdown. Any code block fenceposted with triple backticks and labeled as “julia” gets evaluated by Weave.
Set different chunk options for the block if you want to tweak the code’s treatment.
Weave does its thing, and produces something interesting depending on what output options you use.
The script
AKA the point of this blog post. It looks in my content folder for recently modified .jmd
files. Anything found gets handed off to weave
, which does the hard work. Heck, weave
even has a hugo
option so I can generate Markdown specifically formatted to satisfy Hugo.
This is probably not idiomatic Julia. Maybe it’ll get there when I learn what idiomatic Julia even looks like.
Obviously there’s no error handlng of any kind. That can come later.
A few things I noticed:
- functions like
walkdir
end up making the flow look a bit like Python - I kept making my code more complicated than it needed to be, when both Julia and Weave were ready with reasonable defaults
- especially in regard to types; everything works fine without specifying details; I can find out what happens when I add details later
Okay. That’s great. I mean — all that so I could do a little math, but whatever.
That’s it?
Hey. Maybe we could do something cool. Make a cover image for this post with Luxor.
Let’s try it out. I’ll borrow heavily from the Luxor manual since I don’t really know what I’m doing,
NOTE
First off, PNG format works better than SVG when you’re drawing 500 random circles.
Second, I added an
eval = false
chunk option after the image was good enough. No point regenerating the cover every time I fix a typo.
That’s enough writing about writing with Julia. I have a couple other drafts I want to revisit now.
Besides, it’s Christmas! Christmas 2020. Which means my only regret is forgetting to order Christmas-themed face masks.
Backlinks
Got a comment? A question? More of a comment than a question?
Talk to me about this page on: mastodon
Added to vault 2024-01-15. Updated on 2024-02-01