Origami comics

Rewriting nested function calls

If nested function calls become hard to read, Origami’s expression dialect offers several alternatives.

posts.ori
// You have to read nested calls inside-out to understand the flow.

// Step 3: Apply template
Tree.map(
  // Step 2: Convert markdown to HTML
  Tree.map(
    // Step 1: Sort posts by date
    Tree.sort(markdown/, (post) => post.date),
    Origami.mdHtml
  ),
  post.ori.html
)

Origami’s pipe operator can clarify the flow of data from one function to the next function.

posts.ori
// Step 1: Sort posts by date
Tree.sort(markdown/, (post) => post.date)

// Step 2: Convert markdown to HTML
-> (sorted) => Tree.map(sorted, Origami.mdHtml)

// Step 3: Apply template
-> (html) => Tree.map(html, post.ori.html)

Maybe JavaScript will have this someday.

Origami object properties can also reference local keys, which you can use like temp variables.

posts.ori
{
  // Step 1: Sort posts by date
  sorted: Tree.sort(markdown/, (post) => post.date)

  // Step 2: Convert markdown to HTML
  html: Tree.map(sorted, Origami.mdHtml)

  // Step 3: Apply template
  result: Tree.map(html, post.ori.html)
}

You can do such calculations inside an object, then pluck out just the result.

posts.ori
{
  // Step 1: Sort posts by date
  sorted: Tree.sort(markdown/, (post) => post.date)

  // Step 2: Convert markdown to HTML
  html: Tree.map(sorted, Origami.mdHtml)

  // Step 3: Apply template
  result: Tree.map(html, post.ori.html)
  
}.result // Only expose final result

The last line does the trick.

Read more: Origami pipe operator

  Comic index