All Projects → gajus → pianola

gajus / pianola

Licence: other
A declarative function composition and evaluation engine.

Programming Languages

javascript
184084 projects - #8 most used programming language
Nearley
35 projects

Projects that are alternatives of or similar to pianola

vuo
A realtime visual programming language for interactive media.
Stars: ✭ 103 (+472.22%)
Mutual labels:  declarative
swift-declarative-configuration
Declarative configuration for your objects
Stars: ✭ 46 (+155.56%)
Mutual labels:  declarative
leibniz
Math expression parser and evaluator
Stars: ✭ 20 (+11.11%)
Mutual labels:  evaluator
sugar
Declarative HTTP client for Golang
Stars: ✭ 25 (+38.89%)
Mutual labels:  declarative
zerocode-hello-world
Zerocode YAML and JSON based declarative steps hello world rest api testing example - soap, database
Stars: ✭ 18 (+0%)
Mutual labels:  declarative
vue-size-provider
Declarative element size observer and provider
Stars: ✭ 55 (+205.56%)
Mutual labels:  declarative
bs-declaredom
Strongly typed declarative markup for the DOM and CSS
Stars: ✭ 66 (+266.67%)
Mutual labels:  declarative
carvel-secretgen-controller
secretgen-controller provides CRDs to specify what secrets need to be on Kubernetes cluster (to be generated or not)
Stars: ✭ 54 (+200%)
Mutual labels:  declarative
mpc
Secure Multi-Party Computation (MPC) with Go. This project implements secure two-party computation with Garbled circuit protocol.
Stars: ✭ 41 (+127.78%)
Mutual labels:  evaluator
ency
Enhanced concurrency primitives for Javascript.
Stars: ✭ 32 (+77.78%)
Mutual labels:  declarative
bem-react-boilerplate
DEPRECATED! A bare minimum frontend boilerplate based on create-react-app and bem-react-core.
Stars: ✭ 32 (+77.78%)
Mutual labels:  declarative
FeedbackTree
Unidirectional data flow architecture for Android
Stars: ✭ 14 (-22.22%)
Mutual labels:  declarative
ExcelUtil
Excel utility for Java to read and write data in declarative way.
Stars: ✭ 27 (+50%)
Mutual labels:  declarative
react-nonav
Experimental React Native declarative navigation
Stars: ✭ 58 (+222.22%)
Mutual labels:  declarative
evaluator
No description or website provided.
Stars: ✭ 35 (+94.44%)
Mutual labels:  evaluator
MoleculeJS
A library for creating fast and reactive Custom Elements
Stars: ✭ 39 (+116.67%)
Mutual labels:  declarative
ObservableComputations
Cross-platform .NET library for computations whose arguments and results are objects that implement INotifyPropertyChanged and INotifyCollectionChanged (ObservableCollection) interfaces.
Stars: ✭ 94 (+422.22%)
Mutual labels:  declarative
Processor
Ontology-driven Linked Data processor and server for SPARQL backends. Apache License.
Stars: ✭ 54 (+200%)
Mutual labels:  declarative
react-ratings-declarative
A customizable rating component for selecting x widgets or visualizing x widgets
Stars: ✭ 41 (+127.78%)
Mutual labels:  declarative
pyccolo
Declarative instrumentation for Python.
Stars: ✭ 70 (+288.89%)
Mutual labels:  declarative

Pianola

Travis build status Coveralls NPM version Canonical Code Style Twitter Follow

A declarative function composition and evaluation engine.

Use cases

  • Surgeon uses Pianola to extract information from HTML documents using a declarative API.

Configuration

Name Type Description Default value
bindle Object (optional) A user-defined object that is passed to every subroutine.
handleResult ResultHandlerType (optional) A function invoked after each subroutine with the result of the current subroutine and the subject value used to execute the subroutine. Use this method to throw an error. Return
subroutines $PropertyType<UserConfigurationType, 'subroutines'> User defined subroutines. See subroutines. N/A

Subroutines

A subroutine is a function used to advance the evaluator, e.g.

x('foo | bar baz', 'qux');

In the above example, Pianola expression uses two subroutines: foo and bar.

  • foo subroutine is invoked without values.
  • bar subroutine is executed with 1 value ("baz").

Subroutines are executed in the order in which they are defined – the result of the last subroutine is passed on to the next one. The first subroutine receives the value used to start the evaluator.

Multiple subroutines can be written as an array. The following example is equivalent to the earlier example.

x([
  'foo',
  'bar baz'
], 'qux');

Note:

These functions are called subroutines to emphasise the cross-platform nature of the declarative API.

Defining subroutines

Subroutines are defined using the subroutines configuration.

A subroutine is a function. A subroutine function is invoked with the following parameters:

Parameter name
Subject value, i.e. value used to start the evaluator or result of the parent subroutine.
An array of parameter values used in the expression.
Bindle. See subroutines configuration.

Example:

const x = pianola({
  subroutines: {
    mySubourtine: (subjectValue, [firstParameterValue, secondParameterValue]) => {
      console.log(subjectValue, firstParameterValue, secondParameterValue);

      return parseInt(subjectValue, 10) + 1;
    }
  }
});

x('mySubourtine foo bar | mySubourtine baz qux', 0);

The above example prints:

0 "foo" "bar"
1 "baz" "qux"

Inline subroutines

Subroutines can be inlined by adding a function to the instructions array, e.g.

x([
  'foo',
  'bar',
  (subjectValue) => {
    return subjectValue.toUpperCase();
  },
], 'qux');

Sentinels

FinalResultSentinel

FinalResultSentinel is used to signal the final value, i.e. it will interrupt the routine and return the value used to construct an instance of FinalResultSentinel.

Example:

import pianola, {
  FinalResultSentinel
} from 'pianola';

const x = pianola({
  subroutines: {
    a: () => {
      return new FinalResultSentinel(null);
    },
    b: () => {
      throw new Error('This method is never invoked.');
    }
  }
});

x('a | b', 0);

Expression reference

Pianola subroutines are described using expressions.

An expression is defined using the following pseudo-grammar:

subroutines ->
    subroutines _ ">|" _ subroutine
  | subroutines _ "|" _ subroutine
  | subroutine

subroutine ->
    subroutineName " " parameters
  | subroutineName

subroutineName ->
  [a-zA-Z0-9\-_]:+

parameters ->
    parameters " " parameter
  | parameter

Example:

x('foo bar baz', 'qux');

In this example,

  • Pianola expression evaluator (x) is invoked with foo bar baz expression and qux starting value.
  • The expression tells the expression evaluator to run foo subroutine with parameter values "bar" and "baz".
  • The expression evaluator runs foo subroutine with parameter values "bar" and "baz" and a subject value "qux".

Multiple subroutines can be combined using an array:

x([
  'foo bar baz',
  'corge grault garply'
], 'qux');

In this example,

  • Pianola expression evaluator (x) is invoked with two expressions (foo bar baz and corge grault garply).
  • The first subroutine is executed with the subject value "qux".
  • The second subroutine is executed with a value that is the result of the parent subroutine.

The result of the query is the result of the last subroutine.

Read define subroutines documentation for broader explanation of the role of the parameter values and the subject value.

The pipeline operator (|)

Multiple subroutines can be combined using the pipeline operator (|).

The pipeline operator tells expression evaluator to pass the result of the previous subroutine to the next subroutine. If the subroutine on the left-hand side returns an array, then the receiving subroutine is called for every value in the array.

The following examples are equivalent:

x([
  'foo bar baz',
  'qux quux quuz'
]);

x([
  'foo bar baz | foo bar baz'
]);

x('foo bar baz | foo bar baz');

The aggregate pipeline operator (>|)

Multiple subroutines can be combined using the aggregate pipeline operator (>|).

The pipeline operator tells expression evaluator to pass the result of the previous subroutine to the next subroutine. If the subroutine on the left-hand side returns an array, then the receiving subroutine is called with an array of values.

The following examples are equivalent:

x('foo >| bar');

Cookbook

Unless redefined, all examples assume the following initialisation:

import pianola from 'pianola';

/**
 * @param configuration {@see https://github.com/gajus/pianola#configuration}
 */
const x = pianola();

Map multiple results

When a subroutine results multiple results, then the rest of the expression is evaluated for each of the result.

const foo = () => {
  return [
    1,
    2,
    3
  ];
};

const bar = (value) => {
  if (value === 1) {
    return 'one';
  }

  if (value === 2) {
    return 'two';
  }

  if (value === 3) {
    return 'three';
  }
};

const x = pianola({
  subroutines: {
    bar,
    foo
  }
});

x('foo | bar');

// [
//   'one',
//   'two',
//   'three'
// ]

Name results

Use a QueryChildrenType object (a plain object whose values are Pianola expressions) to name the results.

const foo = (subjectValue, [name]) => {
  return name;
};

const x = pianola({
  subroutines: {
    foo
  }
});

x([
  {
    foo0: 'foo corge',
    foo1: 'foo grault',
    foo2: 'foo garply'
  }
]);

// [
//   {
//     name: 'corge'
//   },
//   {
//     name: 'grault'
//   },
//   {
//     name: 'garply'
//   }
// ]

Error handling

Pianola throws the following errors to indicate a predictable error state. All Pianola errors can be imported. Use instanceof operator to determine the error type.

Name Description
NotFoundError Used to indicate that a resource is not found, e.g. when a subroutine is not found.
PianolaError A generic error. All other Pianola errors extend from PianolaError.

Logging

This package is using roarr logger to log the program's state.

Export ROARR_LOG=true environment variable to enable log printing to stdout.

Use roarr-cli program to pretty-print the logs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].