Skip to content

Latest commit

 

History

History
55 lines (44 loc) · 2.55 KB

placeholder-partial.md

File metadata and controls

55 lines (44 loc) · 2.55 KB

Placeholder Partial

Pipelines and partial application are commonplace in functional programs. JavaScript is working toward an operator and syntax but is overdue with its arrival. They can be had, of course, with Babel plugins (operator and syntax), but this necessitates a build step, the very thing Atomic wants to avoid.

This is ideally what one wants to write:

  _.range(10)
    |> _.map(_.pipe(_.str("Number ", ?), _.lowerCase), ?)
    |> _.join(", ", ?)
    |> $.log;

Atomic provides chain for writing pipelines:

  _.chain(_.range(10),
    x => _.map(_.pipe(y => _.str("Number ", y), _.lowerCase), x),
    x => _.join(", ", x),
    $.log);

It's a good alternative to the pipeline operator, but since it's not point free it's downright ugly.

One can use partial, as underscore does, but it's too verbose to be practical for routine use:

  _.chain(_.range(10),
    _.partial(_.map, _.pipe(_.partial(_.str, "Number ", _), _.lowerCase), _),
    _.partial(_.join, ", ", _),
    $.log);

With placeholder partial the tacit aesthetic is restored:

  _.chain(_.range(10),
    _.map(_.pipe(_.str("Number ", _), _.lowerCase), _),
    _.join(", ", _),
    $.log);

If you write in the tacit style, your modules can be repackaged as shadow modules. These modules imbue exported functions with optional partial application via a feature called placeholder partial.

Atomic modules are reexported this way. The trailing underscore in the folder name makes the use explicit. When defaults are imported from shadow modules, the feature's available for use. When the modules are imported directly, it's not.

The reason for allowing the choice is the feature is made possible by decorating functions, which adds a little overhead to calling them. All this gets you partial application minus a build step. If you're building anyway, there's no point.

//placeholder partial wanted
import _ from "./libs/atomic_/core.js"; //the universal placeholder character
import $ from "./libs/atomic_/shell.js";
import dom from "./libs/atomic_/dom.js";

//placeholder partial not wanted
import * as _ from "./libs/atomic/core.js";
import * as $ from "./libs/atomic/shell.js";
import * as dom from "./libs/atomic/dom.js";