This project uses [custom elements] to implement a simple algebra renderer.
This is work in progress and only four components have been implemented thus far:
<sas-expressiob>
which renders itsvalue
<sas-value>
which renders a value (e.g.2
orx
)<sas-sum>
which renders a sum<sas-product>
which renders a product
This exercise is not "complete" in the sense that it does not provide a user interface for manipulating the expression by drag-and-drop. The API couldn't really be simpler: you simply reorder the terms within a given array.
e.g.
2(x + 1) + 5y + z is represented thus:
['+', ['*', 2, ['+', 'x', 1]], ['*', 5, 'y'], 'z']
Every element comprises an array where the first item defines the type of element and the others are the terms. Reordering the terms works exactly as you would expect.
I should add that because this is all implemented as custom-elements, the individual
pieces can be directly interrogated and manipulated in the console. Similarly, because
b8r
is used to handle the application state and is exposed as a global (for convenience)
you can interrogate it directly in the console as well, e.g.
> b8r.reg.app.expression = ['+', 'x', 1]
Similarly, you could select the element and change its value
directly.)
cd
to the directorynpm install
npm run test
- open
localhost:8080
in your browser
['=', *expression*, *expression*]
Examples:
x + 17 = 21
['+', 'x', 17]
s = ut + 1/2 ⨉ at2
['=', 's', ['+', ['*', 'u', 't'], ['*', ['/', 1, 2], 'a', ['^', 't', 2]]]]
E = mc^1
['=', 'E', ['*', 'm', ['^', ['k', 'c'], 2]
['#', 17] // 17
['#-', 'x'] // -x
In general, a naked value (e.g. 17
or -11
is interchangeable with the structure above.
['k', 'π', 3.1415926535]
['k', 'i']
['k', 'e', 2.7182818285]
['k', 'c', '3E+8]
['k', 'g', 6.6743E-11]
The character merely represents the way in which the value will be depicted. The optional third value is used for computation.
['+', *expression*, ...]
['-', *expression*, ...]
['/', *expression*, *expression*]
['^', *expression*, *expression*]
['√', *expression*, *expression*]
The first value for each item can be "hydrated" with an object expressing all kinds of metadata for the object and dehydrated by reducing it to the specified character.
This would also allow for suggestions, etc. to be generated client/server-side (or both).