Skip to content

Do Expressions #155

@hzoo

Description

@hzoo

Babel docs: http://babeljs.io/docs/plugins/transform-do-expressions
Stage 1 gist reproduced below: https://gist.github.com/dherman/1c97dfb25179fa34a41b5fff040f9879 by @dherman

In Babel

interface DoExpression <: Expression {
  type: "DoExpression";
  body: BlockStatement
}

Motivation

  • expression-oriented programming one of the great advances of FP
  • expressions plug together like legos, making more malleable programming experience in-the-small

Examples

Write in an expression-oriented style, scoping variables as locally as possible:

let x = do {
  let tmp = f();
  tmp * tmp + 1
};

Use conditional statements as expressions, instead of awkward nested ternaries:

let x = do {
  if (foo()) { f() }
  else if (bar()) { g() }
  else { h() }
};

Especially nice for templating languages like JSX:

return (
  <nav>
    <Home />
    {
      do {
        if (loggedIn) {
          <LogoutButton />
        } else {
          <LoginButton />
        }
      }
    }
  </nav>
)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions