@tangle/strategy
A strategy is a way of describing "transformations" and how to apply them to one another
A valid strategy is an Object which provides the following:
identity
Function - an getter which return an element which represents the identity-transformation.isValid
Function - takes a transformation and tells you whether it is a valid for this strategyconcat
Function - takes two transformations (a, b)
and concatenates (applies) b
to a
. Note order mattersreify
Function - takes a transformation and maps it into a "real" state which can be used in e.g. human interfaces- ... more to follow
This module allows you compose larger strategies for transformations made up of transformations
Example Usage
const Strategy = require('@tangle/strategy')
const strategy = new Strategy({
title: require('@tangle/overwrite')(),
attendees: require('@tangle/simple-set')()
})
const T1 = {
title: { set: 'brunch' },
attendees: { mix: 1, alanna: 1 }
}
const T2 = {
title: { set: 'Brunch at Mixs' },
attendees: { alanna: -1, ben: 1 }
}
const T3 = strategy.concat(T1, T2)
// => {
// title: { set: 'Brunch at Mixs' },
// attendees: { mix: 1, ben: 1 }
// }
strategy.reify(T3)
// => {
// title: 'Brunch at Mixs',
// attendees: ['ben', 'mix']
// }
API
strategy.identity() => Object
strategy.isValid(T) => Boolean
If the last result was false
you can find an Error under strategy.isValid.error
strategy.concat(A, B) => C
strategy.reify(A) => a
strategy.properties => Array
Strategy Requirements
Rules for how a strategy must behave:
- have a unique
identity
element
concat(identity(), a) === a
concat(a, identity()) === a
- be associative
concat(concat(a, b), c) === concat(a, concat(b, c))
An identity
element is important because we need to be able to clearly communicate "I don't want to perform a change"
Associativity is important because in scuttlebutt we may have many contributions from different people across time, and being able to group chunks of transformations in a free-form way greatly increases our flexibility.
(e.g. it allows us to summarise a bunch of transformations and save that. this is something useful for writing merge messages)
p.s. a Set which has an associative concat and an identity element is called a "monoid"
BONUS: