import sys
import yaml
with open(sys.argv[1]) as fp:
data = fp.read()
if not data.find("---") == 0:
# no head
print("NO YAML HEAD FOUND")
sys.exit(-1)
data = data[3:]
head_end = data.find("---")
head = data[0:head_end]
data = data[head_end+3:]
metadata = yaml.safe_load(head)
cats = metadata.pop('categories', None)
if cats != None:
if type(cats) == list:
tags = cats
elif type(cats) == str:
tags = cats.split()
tags = list(map(lambda t: t.lower(), tags))
metadata["tags"] = ", ".join(tags)
new_data = f"---\n{yaml.dump(metadata, default_flow_style=False)}---{data}"
# write it
print(f"coverted: categories to tags: {tags} - {sys.argv[1]}")
with open(sys.argv[1], "w") as fp:
fp.write(new_data)
sys.exit(0)
if not metadata.get("tags", None):
metadata["tags"] = "untagged"
new_data = f"---\n{yaml.dump(metadata, default_flow_style=False)}---{data}"
print(f"untagged: {sys.argv[1]}")
# write it
with open(sys.argv[1], "w") as fp:
fp.write(new_data)
sys.exit(0)
print("No changes needed")
701 B
701 B
comments, date, layout, tags, title
| comments | date | layout | tags | title |
|---|---|---|---|---|
| true | 2015-08-22 00:28 | post | haskell, recursion, fibonacci numbers | Recursion |
fibonacci :: [Integer]
fibonacci = 1:1:(zipWith (+) fibonacci (tail fibonacci))
Above is a simple function that generates an infinite stream of fibonacci numbers. Its written in haskell.
This is a piece of code that made me think a lot lately, it make clever use of recursion to define the stream and computes with a linear number of additions. I think its pretty damn sexy!
Notes:
- Many thanks to a co-worker who helped me figure this out.
- I hear that there are more efficient ways to compute fibonacci numbers (namley O(logn)). - should investigate this