2.2.86 • Published 7 days ago

@thi.ng/transducers-fsm v2.2.86

Weekly downloads
157
License
Apache-2.0
Repository
github
Last release
7 days ago

transducers-fsm

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Transducer-based Finite State Machine transformer. This is a support package for @thi.ng/transducers.

This package provides a single function, a general purpose Finite State Machine transducer, which acts as useful & lightweight mechanism to provide context-sensitive processing capabilities as part of a transducer transformation pipeline.

Status

DEPRECATED - no further development planned

Search or submit any issues for this package

This package might be merged with (or deprecated by) the newer @thi.ng/parse package.

Related packages

  • @thi.ng/fsm - Composable primitives for building declarative, transducer based Finite-State Machines & matchers for arbitrary data streams
  • @thi.ng/sax - Transducer-based, SAX-like, non-validating, speedy & tiny XML parser

Installation

yarn add @thi.ng/transducers-fsm

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/transducers-fsm"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const transducersFsm = await import("@thi.ng/transducers-fsm");

Package sizes (gzipped, pre-treeshake): ESM: 234 bytes

Dependencies

Usage examples

For a real world example, the @thi.ng/sax package provides a SAX-like XML parser transducer, built around the FSM provided here.

3-state FSM

The following example defines a simple FSM with 3 states:

  • skip
  • take
  • done

The FSM always starts in the skip state.

The FSM alternates between skipping or consuming (passing through) 5 inputs as long as each input is < 20. Once an input is >= 20, the FSM switches into the done state, which has been declared as a terminal state and once entered will cause processing to terminate (also see API description further below).

import { fsm } from '@thi.ng/transducers-fsm'
import * as tx from '@thi.ng/transducers'
import { isOdd } from '@thi.ng/checks'

const testFSM = fsm({

    // initial state initializer
    // (called before processing 1st input)
    init: () => ({ state: "skip", count: 0 }),

    // terminal state ID
    terminate: "done",

    // individual state handlers
    states: {
        // skip state
        skip: (state, x) => {
            if (x < 20) {
                if (++state.count > 5) {
                    state.state = "take";
                    state.count = 1;
                    return [x];
                }
            } else {
                state.state = "done";
            }
        },

        // take state
        take: (state, x) => {
            if (x < 20) {
                if (++state.count > 5) {
                    state.state = "skip";
                    state.count = 1;
                } else {
                    return [x];
                }
            } else {
                state.state = "done";
            }
        },

        // terminal state, ignore inputs
        done: () => { },
    },
});

[...tx.iterator(testFSM, tx.range(100))]
// [ 5, 6, 7, 8, 9, 15, 16, 17, 18, 19 ]

// Use FSM as part of composed transducers...

[...tx.iterator(tx.comp(tx.takeNth(2), testFSM), tx.range(100))]
// [ 10, 12, 14, 16, 18 ]

[
    ...tx.iterator(
        tx.comp(
            tx.mapcat((x) => x.split(/[,\s]+/g)),
            tx.map((x) => parseInt(x)),
            testFSM,
            tx.filter(isOdd)
        ),
        ["9,8,7,6", "14 1 0 17 15 16", "19,23,12,42,4"]
    )
]
// [ 1, 17, 15 ]

API

Generated API docs

fsm transducer

fsm<T extends FSMState, A, B>(opts: FSMOpts<T, A, B[]>): Transducer<A, B>

Finite State Machine transducer. Takes an FSM configuration object and returns a transducer, which processes inputs using the provided state handler functions, which in turn can produce any number of outputs per consumed input.

Before processing the first input, the FSM state is initialized by calling the user provided init() function, which MUST return a state object with at least a state key, whose value is used for dynamic (i.e. stateful) dispatch during input processing. This state object is passed with each input value to the current state handler, which is expected to mutate this object, e.g. to cause state changes based on given inputs.

If a state handler needs to "emit" results for downstream processing, it can return an array of values. Any such values are passed on (individually, not as array) to the next reducer in the chain. If a state handler returns null or undefined, further downstream processing of the current input is skipped.

Regardless of return value, if a state handler has caused a state change to the configured terminate state, processing is terminated (by calling ensureReduced()) and no further inputs will be consumed.

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-transducers-fsm,
  title = "@thi.ng/transducers-fsm",
  author = "Karsten Schmidt",
  note = "https://thi.ng/transducers-fsm",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

2.2.86

7 days ago

2.2.85

10 days ago

2.2.84

12 days ago

2.2.83

21 days ago

2.2.82

24 days ago

2.2.81

1 month ago

2.2.80

1 month ago

2.2.79

1 month ago

2.2.78

1 month ago

2.2.77

2 months ago

2.2.76

2 months ago

2.2.75

2 months ago

2.2.74

2 months ago

2.2.73

2 months ago

2.2.72

2 months ago

2.2.71

2 months ago

2.2.68

2 months ago

2.2.69

2 months ago

2.2.70

2 months ago

2.2.66

2 months ago

2.2.67

2 months ago

2.2.65

3 months ago

2.2.64

3 months ago

2.2.63

3 months ago

2.2.59

3 months ago

2.2.62

3 months ago

2.2.60

3 months ago

2.2.61

3 months ago

2.2.58

3 months ago

2.2.57

3 months ago

2.2.55

4 months ago

2.2.56

4 months ago

2.2.53

5 months ago

2.2.54

5 months ago

2.2.52

5 months ago

2.2.51

5 months ago

2.2.50

5 months ago

2.2.28

8 months ago

2.2.29

8 months ago

2.2.26

9 months ago

2.2.27

8 months ago

2.2.24

9 months ago

2.2.25

9 months ago

2.2.22

9 months ago

2.2.23

9 months ago

2.2.20

10 months ago

2.2.21

9 months ago

2.2.39

6 months ago

2.2.37

7 months ago

2.2.38

7 months ago

2.2.35

7 months ago

2.2.36

7 months ago

2.2.33

8 months ago

2.2.34

8 months ago

2.2.31

8 months ago

2.2.32

8 months ago

2.2.30

8 months ago

2.2.48

5 months ago

2.2.49

5 months ago

2.2.46

6 months ago

2.2.47

6 months ago

2.2.44

6 months ago

2.2.42

6 months ago

2.2.43

6 months ago

2.2.40

6 months ago

2.2.41

6 months ago

2.2.19

11 months ago

2.2.18

12 months ago

2.2.17

1 year ago

2.2.16

1 year ago

2.2.15

1 year ago

2.2.13

1 year ago

2.2.14

1 year ago

2.2.11

1 year ago

2.2.12

1 year ago

2.2.10

1 year ago

2.2.9

1 year ago

2.2.8

1 year ago

2.2.5

1 year ago

2.2.7

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.2.3

1 year ago

2.2.2

1 year ago

2.2.4

1 year ago

2.1.27

2 years ago

2.1.28

1 year ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.29

1 year ago

2.1.30

1 year ago

2.1.19

2 years ago

2.1.20

2 years ago

2.1.18

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.14

2 years ago

2.1.15

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.11

2 years ago

2.1.9

2 years ago

2.1.10

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.0.8

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.7

2 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.73

3 years ago

1.1.72

3 years ago

1.1.71

3 years ago

1.1.70

3 years ago

1.1.69

3 years ago

1.1.68

3 years ago

1.1.67

3 years ago

1.1.66

3 years ago

1.1.65

3 years ago

1.1.64

3 years ago

1.1.63

3 years ago

1.1.62

3 years ago

1.1.61

3 years ago

1.1.60

3 years ago

1.1.59

3 years ago

1.1.58

3 years ago

1.1.57

3 years ago

1.1.56

3 years ago

1.1.55

3 years ago

1.1.54

3 years ago

1.1.50

3 years ago

1.1.49

3 years ago

1.1.48

3 years ago

1.1.47

3 years ago

1.1.46

3 years ago

1.1.45

3 years ago

1.1.44

3 years ago

1.1.43

3 years ago

1.1.42

3 years ago

1.1.41

3 years ago

1.1.40

3 years ago

1.1.39

4 years ago

1.1.38

4 years ago

1.1.37

4 years ago

1.1.36

4 years ago

1.1.35

4 years ago

1.1.34

4 years ago

1.1.33

4 years ago

1.1.32

4 years ago

1.1.31

4 years ago

1.1.30

4 years ago

1.1.29

4 years ago

1.1.28

4 years ago

1.1.27

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.24

4 years ago

1.1.23

4 years ago

1.1.22

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.36

5 years ago

0.2.35

5 years ago

0.2.34

5 years ago

0.2.33

5 years ago

0.2.32

5 years ago

0.2.31

5 years ago

0.2.30

5 years ago

0.2.29

5 years ago

0.2.28

5 years ago

0.2.27

5 years ago

0.2.26

6 years ago

0.2.25

6 years ago

0.2.24

6 years ago

0.2.23

6 years ago

0.2.22

6 years ago

0.2.21

6 years ago

0.2.20

6 years ago

0.2.20-alpha.1

6 years ago

0.2.20-alpha.0

6 years ago

0.2.19

6 years ago

0.2.18

6 years ago

0.2.17

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago