Javascript: making functions at runtime - javascript

update
solution works in foreach loop but not in for loop
function x(number){
return number - 10;
}
var i = 0
var runtimefunctions = {};
var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"}
for (var key in allLevels) {
runtimefunctions[i] = function() { return x(i); };
i++;
};
console.log(runtimefunctions[1]()); // -6
console.log(runtimefunctions[2]()); // -6
console.log(runtimefunctions[3]()); // -6
tried hard to make functions but it's first time to create such thing so cant understand the proper way...
I have a function..
function x(number){
return number - 10;
}
runtimefunctions = {};
now I have a loop to run
[1,2,3].forEach(function(y){
//here I want to create a function.. which will make a function x(y) -- like this
runtimefunctions[x] = new Function("return function x_" + levelIterator + "(levelIterator){ console.log(levelIterator); x(" + y + ") }")();
});
so basically..want to make functions like this.
runtimefunctions= {
"1": x(1),
"2": x(2),
and so on
}

Is this what you need?
function x(number){
return number - 10;
}
var runtimefunctions = {};
[1,2,3].forEach(function(y){
runtimefunctions[y] = function() { return x(y); };
});
console.log(runtimefunctions[1]()); // -9
console.log(runtimefunctions[2]()); // -8
console.log(runtimefunctions[3]()); // -7
To satisfy your next (for-in) requirement, you need to closure the index variable with additional function call:
var runtimefunctions = {}, i = 0;
var allLevels = {"1":"State","2":"Educational_Services","3":"Principal_Networks","4":"Schools"}
for (var key in allLevels) {
runtimefunctions[i] = function(index){ return function() { return x(index); } }(i++);
};

It is much easier.
For example:
const createFunctionWith = (x) => {
return (param) => console.log(x, param)
}
let a = [1,2,3].map(x => createFunctionWith(x));
console.log(a[1]("bebe")); // 2, "bebe"
https://jsfiddle.net/muLxoxLd/

You could do something like this
// Found in your code
var x = (a) => {
console.log(a)
};
var runtimefunctions = {};
[1, 2, 3].forEach(function(y) {
//Create a function with a parameter named "levelIterator"
runtimefunctions[y] = Function("levelIterator", "{ console.log(levelIterator); x(" + y + ") }");
});
runtimefunctions[1]('test')

Related

Is this possible to design a function which takes a parameter and returns an object which holds multiple properties or functions

For example:
var resultList = [];
var objectName = (userName) => {
};
objectName.rowCount; // return overall count
objectName.fetchNext(); // updated data will be available in resultList
I have tried multiple solutions with no result like
var resultList = [];
var objectName = (userName) => {
var rowCount = 0;
init() {
// make call to server and read data
rowCount = 5; // setting dummy data
};
fetchNext = function(){
// logic here
resultList = [] // new data
};
init();
};
EDIT
Another attempt
var x = function(){
var a = function(){console.log('a');};
var b = function(){console.log('b');};
return {a: a, b: b};
}
x.a(); // not able to call this function
You can't use an arrow function as a constructor, so tyou could change your code to use a traditional function:
function objectName(userName) {
var rowCount = 0;
init = function() {
// make call to server and read data
rowCount = 5; // setting dummy data
};
this.fetchNext = function(){
// logic here
const resultList = [] // new data
return resultList;
};
init();
};
var myObj = new objectName("foo");
console.log(myObj.fetchNext());
Or, you can return an object from your arrow function
var objectName = (userName) => {
var rowCount = 0;
function init() {
// make call to server and read data
rowCount = 5; // setting dummy data
};
init();
return {
fetchNext: function(){
// logic here
const resultList = [] // new data
return resultList;
}
}
};
var myObj = objectName("Foo");
console.log(myObj.fetchNext());
Related:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
For completeness, the reason your edit did not work is that you defined x but never executed the function. This works:
var x = function(){
var a = function(){console.log('a');};
var b = function(){console.log('b');};
return {a: a, b: b};
}
x().a(); // must execute x to get the result
And is esssentially the same as my second example above
In addition to the other answer, if you want to use an arrow function you can use it like this by wrapping the object in paranthesis:
const objectName = (userName) => ({
rowCount: 0,
resultList: [],
init() {
this.rowCount = 5;
},
fetchNext() {
// fetch results
this.resultList = [1, 2, 3, 4];
}
});
const results = objectName('kyroath');
console.log('before init():', results.rowCount); // 0
results.init()
console.log('after init():', results.rowCount); // 5
console.log('before fetchNext():', results.resultList); // []
results.fetchNext()
console.log('after fetchNext():', results.resultList); // [1, 2, 3, 4]
Are you just looking for a Class?
class Obj {
constructor() {
this.rowCount = 0;
this.resultList = [];
}
init() {
// make call to server and read data
this.rowCount = 5; // setting dummy data
}
fetchNext() {
// logic here
this.resultList.push(1); // new data
return this.resultList;
}
}
const obj = new Obj("userName");
obj.init();
const row = obj.rowCount;
const res = obj.fetchNext();
console.log(row);
console.log(res);

Variable not persisting when using a constructor

So I'm using a constructor like this
const RPNCalculator = function () {
let methods = {
numberList: [],
calc: 0,
push(num) {
this.numberList.push(num);
},
plus() {
for (let i = 0; i <= this.numberList.length; i++) {
console.log('before:' + this.calc);
this.calc = this.calc + this.numberList[i];
}
console.log('after:' + this.calc);
this.numberList = [];
}
};
return methods;
}
const rpnCalculatorInstance = new RPNCalculator;
The fist console.log prints correctly and adds the elements but the second console.log prints NaN. I've used this pattern before with Object.create but for some reason the this.calc variable isn't persisting when using a constructor.
Any help is appreciated!
you can use reduce to sum up an array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
run snippet below
class RpnCalculator{
constructor(){
this.numberList = [];
this.push = (num) => { this.numberList = [...this.numberList, num ]}
this.sum = () => {return this.numberList.reduce(( a, c) => a + c, 0);}
}
}
const rpnCalculator = new RpnCalculator();
rpnCalculator.push(1)
rpnCalculator.push(2)
rpnCalculator.push(3)
console.log(rpnCalculator.sum());
Apparently with the dataset I was given the last item in the array was an undefined element. I fixed it by using
if (typeof (this.numberList[i]) === 'number') {
console.log('before:' + this.calc);
this.calc = this.calc + this.numberList[i];
}

Confusion understanding closures

Conceptually closures make a bit of sense to what's happening, but in practice, I have no idea what's going on. For the problem I am dealing with I want to count the occurrences of numbers I read in and store it in a histogram. This is supposed to happen in the countOccurences function, but the histogram array never gets updated.
var LinkedList = function() {
this.head = null;
}
LinkedList.prototype.add = function(val) {
this.head = {data: val, next: this.head};
};
LinkedList.prototype.forEach = function(action) {
for (var temp = this.head; temp; temp = temp.next) {
action(temp.data);
}
};
// This is where the closure concept should take effect
var countOccurrences = function(histogram) {
return function(val){ // Not working as expected, The val should equal
// a number found in the list. Take for example if
// 1 is found then histogram[1]++. Meaning we've
// seen the number 1 once in the list so far. This
// continues until the entire list has been processed.
histogram[val]++;
};
}
function printHistogram(histogram) {
for (var i in histogram) {
if (histogram[i]) {
println("(#" + i + ":" + histogram[i] +")")
}
}
}
var main = function() {
var list = new LinkedList(); //Creates empty linkedlist
var histogram = [];
while (ln = readln().trim()) { //Reads in numbers from stdin
list.add(ln)
}
list.forEach(countOccurrences(histogram))
printHistogram(histogram)
}; main()
The actual closure was going wrong because of the lack of a return statement initially. However, after that, I found another bug that was unrelated to closure. The problem was in the function because I was incrementing a number that wasn't initialized. So the fix is as follows:
var countOccurrences = function(histogram) {
return function(val) {
if(histogram[val])
histogram[val]++;
else
histogram[val] = 1;
};
}
Regardless, much apperciation for the help.
Your closure is not actually returning a function. Not exactly sure how you are counting or what those values will be but you definitely need to start by returning your function from your closure.
var LinkedList = function() {
this.head = null;
}
LinkedList.prototype.add = function(val) {
this.head = {
data: val,
next: this.head
};
};
LinkedList.prototype.forEach = function(action) {
for (var temp = this.head; temp; temp = temp.next) {
action(temp.data);
}
};
// This is where the closure concept should take effect
var countOccurrences = function(histogram) {
return function(val) { // Not working as expected, I'm tring to get val to
// be the data passed in from forEach which will
// then be incremeted accordingly in the histogram
// array
histogram[val] = histogram[val] ? histogram[val] + 1 : 1;
};
}
function printHistogram(histogram) {
for (var i in histogram) {
if (histogram[i]) {
println("(#" + i + ":" + histogram[i] + ")")
}
}
}
var main = function() {
var list = new LinkedList(); //Creates empty linkedlist
var histogram = [];
while (ln = readln().trim()) { //Reads in numbers from stdin
list.add(ln)
}
list.forEach(countOccurrences(histogram))
printHistogram(histogram)
};
main()

Calling functions insight function / arguments

I am trying to write a function that takes functions as arguments (as many as it gets) and returns them. The function funcArg should return 'Called me'. I used Array.prototype.slice.call(arguments); to create an array but I don't know how to call die functions in that array. Any ideas? Thanks!!
var caller = function() {
return "Called ";
};
var adder = function() {
return " me";
};
var funcArgs = function() {
var myArray = Array.prototype.slice.call(arguments);
}
funcArgs(caller);
funcArgs(calleradder);
You can do this using reduce.
var funcArgs = function() {
var functions = Array.prototype.slice.call(arguments);
return functions.reduce(function(total, f) {
return total + f();
}, '');
};
The way this works if you start off with an array of functions. We then go through each function one at a time. We then call that function and append it to the result of the previous function. Breaking this down into simpler code would look like this:
var funcArgs = function() {
var functions = [caller, adder];
var result = '';
result += functions[0](); // caller();
result += functions[1](); // adder();
return result;
};
If you have an array of functions you can loop over them with forEach.
var caller = function() {
return "Called "
}
var adder = function() {
return " me"
}
var funcArgs = function() {
var myArray = Array.prototype.slice.call(arguments);
myArray.forEach(function (fn) {
console.log(fn())
})
}
funcArgs(caller, adder); // "Called me"
If you want to actually return the values, rather than just console.log them, you can use reduce to return the strings concatenated (or whatever else)
var funcArgs = function() {
var myArray = Array.prototype.slice.call(arguments);
return myArray.reduce(function (acc, fn) {
return acc + fn()
}, '')
}

Lazy.js based fibonacci to map Bacon.js interval?

I have a code to generate fib sequences with lazy.js
var fibF = function()
{
var seq = []; //build sequence array in this closure
var f = function(n)
{
var val;
if (n <= 1)
{
val = 1; // as the Fib definition in Math
}
else
{
val = seq[n - 2] + seq[n - 1]; // as the Fib definition in Math
}
seq[n] = val;
return val;
};
return f;
}();
var fibSequence = _.generate(fibF);
/* just for test
var fib_1000 =
fibSequence
.take(1000) 
.toArray(); 
console.log(fib_1000);
//[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...........,4.346655768693743e+208 ]
*/
At the same time, I have a code of timer with Bacon.js
var B = require('baconjs');
var timeSequence = B
.interval(1000); //every second
timeSequence
.onValue(function()
{
console.log(require('moment')().format('MMMM Do YYYY, h:mm:ss'));
// print timestamps every second
});
Then,
I want to map the the fibSequence onto timeSequence such as
var mySequence = fibSequence.map(timeSequence);
or
var mySequence = timeSequence.map(fibSequence);
Is it possible?
If so, please show me the way.
Any workaround solution is welcome.
Thanks.
EDIT working code:
//to simplify use Natrual, instead of Fib
var _ = require('lazy.js');
var __ = require('baconjs');
var natural = function(n)
{
return n;
};
var _natural = _.generate(natural); //natural numbers
var __timer = __.interval(1000); //every second
var map_to__ = function(_seq, __seq)
{
var it = _seq.getIterator();
var sequence =
__seq
.map(function()
{
it.moveNext();
return it.current();
});
return sequence;
};
var __mappedTimer = map_to__(_natural, __timer);
__mappedTimer
.onValue(function(x)
{
console.log(x); // print every second
});
I'm not sure whether this is the intended use of iterators, but it should work:
var it = fibSequence.getIterator()
var mySequence = timeSequence.map(function() {
return it.moveNext() && it.current();
});

Categories