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

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();
});

Related

Second Greatest Value Displaying As "undefined"

All variables below are integer values of a range input slider. The code below should log the second greatest value of the sliders to the console. For example, if sliderA = 2, sliderB = 3, and sliderC = 4, the number 3 should be logged to the console. Instead, I'm getting the output "undefined". The code below works when the function is called with integers instead of variables. Does anyone know how to solve this problem?
var AS = Number(getNumber("animalsSlide"));
var MS = Number(getNumber("mathSlide"));
var CS = Number(getNumber("cookingSlide"));
var BS = Number(getNumber("biologySlide"));
var PS = Number(getNumber("performingSlide"));
var WS = Number(getNumber("writingSlide"));
var CAS = Number(getNumber("creativeSlide"));
var IS = Number(getNumber("inventingSlide"));
function secondGreatest(arr_num) {
arr_num.sort(function(x,y) {
return x-y;
});
var uniqa = [arr_num[0]];
var result = [];
for(var j=1; j < arr_num.length; j++) {
if(arr_num[j-1] !== arr_num[j]) {
uniqa.push(arr_num[j]);
}
}
result.push(uniqa[uniqa.length-2]);
return result.join(',');
}
console.log(secondGreatest([AS,MS,CS,BS,PS,WS,CAS,IS]));
You could search for the second large value of the sorted array.
const
values = [5, 5, 5, 4, 4, 3, 2, 1],
second = values.find(((last, count = 0) => value => {
if (last !== value) {
count++;
last = value;
}
return count === 2;
})());
console.log(second);
since it works with integers and not strings, I assume the problem is with the getNumber function.
do you have code there that finds the slider by id?
something like this:
let getNumber = (sliderId) => {
return +document.getElementById(sliderId).value;
}
since the name of the function is getNumber it's easy to assume it returns a number and not a string, so that's what I did here.. and then you won't need to parse to number outside
var AS = getNumber("animalsSlide");
var MS = getNumber("mathSlide");
var CS = getNumber("cookingSlide");
var BS = getNumber("biologySlide");
var PS = getNumber("performingSlide");
var WS = getNumber("writingSlide");
var CAS = getNumber("creativeSlide");
var IS = getNumber("inventingSlide");

Javascript: making functions at runtime

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')

Fastest way to double the length of an array in JavaScript?

[1,2,3,4,5].duplicate(); // [1,2,3,4,5,1,2,3,4,5]
Maybe something like:
var array = [1,2,3,4,5];
array.push(array);
But what's the fastest method?
You can use concat and replace the original array with the new one:
array = array.concat(array);
Or push with apply (modifies the original array):
[].push.apply(array, array);
Or push with the spread operator (modifies the original array):
array.push(...array);
The FASTEST way to do something in JS is usually using simple C-like native statements.
I think this will be the fastest one:
function duplicate(arr) {
for(var i=0,len=arr.length;i<len;i++)
arr[i+len]=arr[i];
}
A slower but more elegant one would be this:
arr=arr.concat(arr);
Or this one:
[].push.apply(arr,arr);
EcmaScript 6 also allows you to do the same using the spread operator. This operator just puts all the values of the array for you in the place you have written it, so this var arr = [0,1,2,3];console.log(...arr) turns into var arr = [0,1,2,3];console.log(0,1,2,3).
arr.push(...arr);
However, EcmaScript 6 is not yet widely supported (and this feature neither) so if I were you I wouldn't use it. Although ES 6 has just been approved & released (as #LyeFish said in the comments) most of browsers are still working on it.
EcmaScript-262 6th edition officially released 5 days ago (thanks to #LyeFish)!
http://www.ecma-international.org/news/index.html
According to my test results, the winners are concat followed by push methods. I also tested looping method and my own newly-introduced splice routine.
Here's the testing "infrastructure" code that was used:
var toArray = Function.prototype.call.bind(Array.prototype.slice);
Function.prototype.testerTimes = function(number) {
var func = this;
return function() {
var args = toArray(arguments), i = 0;
for( ; i < number; func.apply(this, args), i++);
};
};
Function.prototype.testerTime = function(units) {
var func = this;
return function() {
var start = Date.now(), diff;
func.apply(this, toArray(arguments));
diff = Date.now() - start;
return units === "s" ? diff / 1000 : units === "m" ? diff / 60000 : units === "h" ? diff / 3600000 : diff;
};
};
Function.prototype.testerToConsole = function(prefix, message) {
var func = this;
return function() {
console.log(prefix + message + func.apply(this, toArray(arguments)));
};
};
Function.prototype.makeTestReady = function(times, units, prefix, message) {
return this.testerTimes(times).testerTime(units).testerToConsole(prefix, message);
};
Here's the test code:
function genArray(num) {
for(var i = 0, arr = []; i < num; arr.push(++i));
return arr;
};
var numberOfRuns = 1000000;
var timeUnit = "s";
var messagePrefix = " ";
var funcs = [
function duplicateConcat(arr) {
var arrCopy = arr.slice(0);
return arrCopy.concat(arrCopy);
},
function duplicatePush(arr) {
var arrCopy = arr.slice(0);
arrCopy.push.apply(arrCopy, arrCopy);
return arrCopy;
},
function duplicateLoop(arr) {
var arrCopy = arr.slice(0);
for(var i = 0, len = arrCopy.length; i < len; i++) {
arrCopy[len + i] = arrCopy[i];
}
return arrCopy;
},
function duplicateSplice(arr) {
var arrCopy = arr.slice(0);
arrCopy.splice.apply(arrCopy, [arrCopy.length, 0].concat(arrCopy));
return arrCopy;
}
].map(function(func, index, arr) {
return func.makeTestReady(numberOfRuns, timeUnit, messagePrefix, func.name + ": ");
});
for(var i = 5; i < 25; i+= 5) {
console.log(i + "-element array:");
funcs.forEach(function(func) {
func(genArray(i));
});
}
And, here are the results of 1,000,000 runs of each function for arrays of sizes 5, 10, 15, and 20:
5-element array:
duplicateConcat: 0.236
duplicatePush: 0.228
duplicateLoop: 0.372
duplicateSplice: 0.45
10-element array:
duplicateConcat: 0.241
duplicatePush: 0.273
duplicateLoop: 0.433
duplicateSplice: 0.48
15-element array:
duplicateConcat: 0.261
duplicatePush: 0.293
duplicateLoop: 0.5
duplicateSplice: 0.522
20-element array:
duplicateConcat: 0.24
duplicatePush: 0.311
duplicateLoop: 0.602
duplicateSplice: 0.558

seprate values from a string

I have 2 string like this
var a= '12,13,14,15,16';
var b='p,q,q,p,q';
I just need like this 12,15 represents p
and 13,14,16 represents q
How can I do this in Jquery/javascript.
var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
var as = a.split(",");
var bs = b.split(",");
if(as.length == bs.length)
{
var map = {};
for(var i = 0; i < as.length; ++i)
{
var asv = as[i];
var bsv = bs[i];
map[asv] = bsv;
}
console.log(map['13']); //q
}
or:
var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
var as = a.split(",");
var bs = b.split(",");
if(as.length == bs.length)
{
var map = {};
as.map(function(asv,idx){
return {asv:asv, bsv:bs[idx]};
})
.forEach(function(x){
map[x.asv] = x.bsv;
});
console.log(map['13']); //q
}
In answer to your comment, perhaps something like this would be better:
var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
var as = a.split(",");
var bs = b.split(",");
if(as.length == bs.length)
{
var map = {};
as.map(function(asv,idx){
return {asv:asv,bsv:bs[idx]};
})
.forEach(function(x){
if(!map[x.bsv])
{
map[x.bsv]=[];
}
map[x.bsv].push(x.asv);
});
console.log(map['q']); //["13", "14", "16"]
console.log(map['q'].join(",")); //"13,14,16"
}
Simple as this:
var a= '12,13,14,15,16';
var b='p,q,q,p,q';
var pValues=[], qValues=[]; //where to store the results
b.split(',').forEach(function(value, index){ //split the values from the b var
var aa=a.split(','); //split the values from the a var
if(value=='p') pValues.push(aa[index]);
if(value=='q') qValues.push(aa[index]);
});
console.log("pValues: "+pValues);
console.log("qValues: "+qValues);
var a= '12,13,14,15,16';
var b='p,q,q,p,q';
function getRepresentative(srcA, srcB, valA)
{
var mapIndexA = srcA && srcA.split(',').indexOf(valA);
var mapB = srcB.split(',');
return mapB && mapB[mapIndexA] || -1;
}
console.log(getRepresentative(a,b, '15'));
The function returns -1 if no corresponding map between A and B is found..
The following function takes the two strings and splits them with the comma, then iterates over the symbol-token pairings one by one. Whenever a new symbol is discovered, it gets added to symbols and an empty array is added to symbolToNumbers so that numbers for this symbol can be pushed onto the array.
At the end, we can iterate over symbols to display the list of numbers for each symbol. This code will work for any variety of symbols, not just 'p' and 'q'.
function findElements(numbers, symbols) {
var numberTokens = numbers.split(','),
symbolTokens = symbols.split(','),
symbolToNumbers = {},
symbols = [],
n = numberTokens.length;
if (n != symbolTokens.length) {
console.log('error: the lists must have the same length');
return;
}
for (var i = 0; i < n; ++i) {
var number = numberTokens[i],
symbol = symbolTokens[i];
if (symbolToNumbers[symbol] === undefined) {
symbols.push(symbol);
symbolToNumbers[symbol] = [];
}
symbolToNumbers[symbol].push(number);
}
for (var i = 0; i < symbols.length; ++i) {
var symbol = symbols[i],
numbers = symbolToNumbers[symbol];
console.log(symbol+': '+numbers.join(', '));
}
}
var a = '12,13,14,15,16';
var b = 'p,q,q,p,q';
findElements(a, b);
See this code running on JSFiddle: http://jsfiddle.net/0e1g2ryf/1/

Create an array [n,[v,..,z]] from a list of key-value pairs

I have this input sample:
var c1 = "s_A_3";
var c2 = "s_B_10";
var c3 = "s_B_9";
var c4 = "s_C_18";
var c5 = "s_C_19";
var c6 = "s_C_20";
Which can easily be concatenated to:
var keypairs = ["A_3","B_10","B_9","C_18","C_19","C_20"];
And I want to convert this to a multidimensional array like this:
var groupArray = [["A",[3]],["B",[10,9]],["C",[18,19,20]]];
It's like a kind of card-sorting. How can I achieve this?
Maybe something like this:
function makeGroups(arr) {
var result = [], prev;
for(var i = 0; i < arr.length; i++) {
var x = arr[i].split("_");
if (prev !== x[0]) {
prev = x[0];
result.push([prev, []]);
}
result[result.length - 1][1].push(x[1]); // or .push(parseInt(x[1], 10))
}
return result;
}
var keypairs = ["A_3","B_10","B_9","C_18","C_19","C_20"];
console.log(makeGroups(keypairs));
// [["A",["3"]],["B",["10","9"]],["C",["18","19","20"]]]
Demonstration
The above method assumes the groups will be contiguous (e.g. all B_ elements appear together). In case your input may be out of order, you can tweak this algorithm to still group all elements together regardless of where they appear in the input:
function makeGroups(arr) {
var result = [], keys = {};
for(var i = 0; i < arr.length; i++) {
var x = arr[i].split("_");
if (!(x[0] in keys)) {
keys[x[0]] = [];
result.push([x[0], keys[x[0]]]);
}
keys[x[0]].push(x[1]); // or .push(parseInt(x[1], 10))
}
return result;
}
var keypairs = ["A_3","B_10","C_18","C_19","C_20","B_9"];
console.log(makeGroups(keypairs));
// [["A",["3"]],["B",["10","9"]],["C",["18","19","20"]]]
Demonstration
When you need to mention "key value pairs" in a JS program, it's usually most appropriate to use... key value pairs =D.
function solution(input) {
var kvp = {},
result = [];
input.forEach(function (el) {
var cut = el.split("_"),
alpha = cut[0],
numeric = cut[1],
elsWithSameAlpha = kvp[alpha] = kvp[alpha] || [];
elsWithSameAlpha.push(numeric);
});
Object.keys(kvp).forEach(function (key) {
result.push([key, kvp[key]]);
});
return result;
}

Categories