Robert Shecter

My Best Elixir in Python

After using Elixir for a long time, Python’s APIs are difficult for me to use. Everything seems a little random.

So with the help of functoolz curry, flip, and pipe, I can write my HTML parsing code like this:

def parse_title(dom: XmlResponse) -> Title:
name = pipe(
dom
, html.xpath("//TITLE-TEXT")
, text.titleize
)
number = pipe(
dom
, html.xpath("//TITLE-NUM")
, text.split(" ")
, seq.get(1)
)
children = _parse_divisions_or_articles(number, dom)
url = _source_url(number)
return Title(name, number, children, url)
view raw parse_title.py hosted with ❤ by GitHub

I’m not re-writing any of these functions. I’m just wrapping each of them with a little code to give them a consistent interface. And once they each have this new API, they can be easily chained together.

Anyone else adapting other languages to the Elixir fluent API style?

Leave a comment