darrenarmstrong85 / scientist

q scientist library implementation
Apache License 2.0
0 stars 0 forks source link

Replace mechanism for creating new experiment handlers #6

Closed darrenarmstrong85 closed 7 years ago

darrenarmstrong85 commented 7 years ago

As a scientist library developer, avoiding use of string-based functions will improve maintainability of code

Current code in .scientist.new is a bit horrible and creates a string representation of the desired function and evaluates it to create a new function as required.

This allows it to take any legal number of arguments (0-8), but there's a fairly nasty code smell about it, and this is going to become harder and harder to maintain over time.

Done
darrenarmstrong85 commented 7 years ago

Started to put a branch together at: 6-replace-experiment-creation

darrenarmstrong85 commented 7 years ago

Could use function composition here? Random bit of pseudocode:

@[monadicFunc; (index;).]
darrenarmstrong85 commented 7 years ago

Not quite. But it's halfway there.

q)monadicFunc:{[p] -1 "index is; ",string first p; -1 "args are: ", -3!1_p;}
q)index:3
q)f:('[;]) over (monadicFunc; 0N!; (index;))
q)f[1]
3 1
index is; 3
args are: ,1
q)f[1;2]
'rank

Instead, we can use a magic property of enlist (which can take up to 8 parameters) to make a list of our args, and use function composition to pass them, as well as the index we need, to a monadic handler function. Something like:

q)enlist[1;2;3;4;5;6;7;8;9;10] / can take MANY arguments
1 2 3 4 5 6 7 8 9 10
q)f:('[;]) over (monadicFunc; (index;); enlist)
darrenarmstrong85 commented 7 years ago

This seems to work! Adding a sentinel means it shouldn't collapse down in unexpected ways, no matter how we specify index, either.

q)index:3
q)monadicFunc:{[withSentinel] p:1 _ withSentinel; -1 "index is; ",string first p; -1 "args are: ", -3!1_p;}
q)f:('[;]) over (monadicFunc;0N!;(::;index;);enlist)
q)f
{[withSentinel] p:1 _ withSentinel; -1 "index is; ",string first p; -1 "args are: ", -3!1_p;}![0N]enlist[::;3;]enlist
q)f[1;2;3;4;5;6;7;8]
(::;3;1 2 3 4 5 6 7 8)
index is; 3
args are: ,1 2 3 4 5 6 7 8