nice idea. if it were implemented in source, it would have less overhead. but if general operator overloading was implemented instead, it would be way more flexible(ud be able to (re-)define whatever operators u liked,
|>,
<|>,
<*> etc), but it would have the same overhead as method calls do. regardless i think the latter would be preferable
gimmick with overloading
.Call()(though, ull have to write ur functions with currying in mind, or hack together an autocurrying function):
Code: Select all
#Requires AutoHotkey v2.0-beta.1
(Object.DefineProp)(String.Prototype, 'Call', {Call: pipe})
pipe(this, ArraysOrFuncs*) {
for Obj in ArraysOrFuncs
this := (Obj is Array)
? pipe(this, Obj*)
: Obj(this)
return this
}
Rotate(degree) => (this) => this .= A_Space degree
Flip(axis) => (this) => this .= A_Space axis
NewLine(this) => this .= '`n'
RotateVerticallyInstructions := [
Rotate(180),
Flip("V"),
Rotate(-180),
]
str := ""
MsgBox str(
RotateVerticallyInstructions,
NewLine,
Rotate(90),
Flip("H"),
Rotate(-90),
NewLine,
RotateVerticallyInstructions,
)
another gimmick with meta functions(which wont work unless the functions/lists ure trying to reference can resolve to
global var. so not very useful...):
Code: Select all
#Requires AutoHotkey v2.0-beta.1
(Object.DefineProp)(String.Prototype, '__Get', {Call: __Get})
(Object.DefineProp)(String.Prototype, '__Call', {Call: __Call})
__Get(this, propName, Params) {
ArrayOrFunc := %propName%
processArrayRecursionHelper(Arr) {
for Obj in Arr
this := (Obj is Array)
? processArrayRecursionHelper(Obj) ; its an Array, call recursively
: Obj(this) ; its a function, call it as is
return this
}
if (ArrayOrFunc is Array)
return processArrayRecursionHelper(ArrayOrFunc) ; its an Array, iterate over its elements
else
{
; otherwise its a function, call it with whatever args were provided(if any)
; since its a property square brackets .prop[arg1, arg2, ...] would have had to be used
; because parentheses .method(arg1, arg2, ...) would have invoked meta-call instead
return __Call(this, propName, Params)
}
}
__Call(this, methodName, Params) {
Function := %methodName%
; if u decide to stick with the currying approach
for param in Params
Function := Function(param)
return Function(this)
}
Rotate(degree) => (this) => this .= A_Space degree
Flip(axis) => (this) => this .= A_Space axis
NewLine(this) => this .= '`n'
RotateVerticallyInstructions := [
Rotate(180),
Flip("V"),
Rotate(-180),
]
str := ""
MsgBox str
.RotateVerticallyInstructions
.NewLine
.Rotate(90)
.Flip("H")
.Rotate(-90)
.NewLine
.RotateVerticallyInstructions
iseahound wrote: ↑07 Oct 2021, 16:18
edit: maybe apply should be renamed left fold? I know there's a name for the action of applying a list of functions to a single argument but its definitely not apply....
looks like a plain fold to me.. oh but who am i kidding, i know nothing about functional programming! its probably an endothermic copresheaf on the precategory of monozygotic burritos