Table of Contents
- Library Installation
- Partial Application
- Lazy Fields
- Known Limitations
- Further Reading
This library provides a number of utility decorators to enable the use of features commonly found in functional languages for use with classes.
As a dependency run the command:
npm install @final-hill/class-tools
You can also use a specific version:
npm install @email@example.com
For use in a webpage:
With a specific version:
@memo decorator memoizes
(caches) the results of the associated method call.
;fib.calc30 // 832040; 9msfib.calcMemo30 // 832040; less than 1ms
@curry decorator converts the associated method into a method
that supports currying the parameters.
;addOne = adder.add1;addOne3 // 4addOne3 // 4
@partial decorator converts the associated method into
one that supports partial application of its parameters
;;a.m1,2,3 === 6a.m_,2,31 === 6a.m1,_,32 === 6a.m1,2,_3 === 6a.m1,_,_2,3 === 6a.m_,2,_1,3 === 6a.m_,_,31,2 === 6a.m_,_,_1,2,3 === 6a.m_,_,__,2,_1,3 === 6
@lazy decorator converts the associated getter into a lazily
initialized field. Practically this means that the body of the getter
will only executed once on its first use. Subsequent usages will
return the cached result of the first call.
;;void foo.bar;void foo.bar;void foo.bar;Counter.usage // 1
@fix decorator can be assigned to methods in order to
control the behavior of its recursion and find its
It provides options to limit runaway recursion as well as
handle self-referential calls while returning a value.
bottom option defines the value to return when the recursive call
bottoms out. In other words, if the current method has been recursively
called with the same arguments then it is replaced with the value given.
;new Foo.bar === 0;
Recursive calls may always vary in their arguments leading
to runaway recursion in a different way. The
prevents infinite recursion by replacing the nth call with
the value defined by the
;new Foo.bar0 === 10;
bottom option can also be defined as a function if
a computed result is desired:
;new Foo.bar0 === 20;
When using TypeScript a decorator can not change the type of the associated class feature. This is a limitation of the language.
Depending on your usage you may need to perform explicit casting or utilize the
// @ts-ignore option. For example:
;;a.m1,2,3 === 6// @ts-ignorea.m_,2,31 === 6// @ts-ignorea.m1,_,32 === 6// @ts-ignorea.m1,2,_3 === 6