Is there a standard functional name for this function? - node.js

I'm sure this function is likely common ( or possibly achievable other ways ) but I'm not sure of what it'd be called. I'm thinking of a sliding window of a certain size :-
let slidingMap = (arr,size, f) => {
r = []
arr.reduce((acc, n) => {
let b = acc.concat(n);
if(b.length > size) {
b.shift();
}
if(b.length == size) {
r.push(f(b))
}
return b;
},[])
return r;
}
so given slidingMap([1,2,3,4,5,6], 2, b => b)
you'd get [ [ 1, 2 ], [ 2, 3 ], [ 3, 4 ], [ 4, 5 ], [ 5, 6 ] ]
and slidingMap([1,2,3,4,5,6], 3, b => b)
you'd get [ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ] ]
or for calculating differences :-
slidingMap([1,2,3,7,5,6],2, b => b.reduceRight((a, n) => a? a-n : n))
you'd get [ 1, 1, 4, -2, 1 ]
or moving average :-
slidingMap([1,2,3,7,5,6],3, b => b.reduce((a, n) => a+n,0)/b.length)
you'd get [ 2, 4, 5, 6 ]
so, is there a commonly implemented function(s) that achieves this?
Update
Probablly better implemented as
let sliding = (arr,size) => {
r = []
arr.reduce((acc, n) => {
let b = acc.concat(n);
if(b.length > size) {
b.shift();
}
if(b.length == size) {
r.push(b)
}
return b;
},[])
return r;
}
then just use map
sliding([1,2,3,4,5],2).map(somefunc);
Or perhaps using zip and skip ( using lodash in this case )
let sliding = (arr, size) =>
_.zip(..._.range(size).map(i => arr.slice(i)))
.filter(a => !a.some(v => v == undefined))
only trick here is the zip will insert undefined when it has no match so they need to be filtered out.

Related

How to sort descending order with an Object result in NodeJs

I am using the below function to get number of duplicated values in an array.But i want to get this result sorted descending order with respect to the values.
function countRequirementIds() {
const counts = {};
const sampleArray = RIDS;
sampleArray.forEach(function(x) { counts[x] = (counts[x] || 0) + 1; });
console.log(typeof counts); //object
return counts
}
Output:
{
"1": 4,
"2": 5,
"4": 1,
"13": 4
}
required output:
{
"2": 5,
"1": 4,
"13": 4,
"4": 1,
}
Javascript object keys are unordered as explained here: Does JavaScript guarantee object property order?
So sorting objects by keys is impossible. However if order is of a matter for you I would suggest using array of tuples:
const arrayOfTuples = [
[ "1", 4],
[ "2", 5],
[ "4", 1],
[ "13", 4],
]
arrayOfTuples.sort((a,b) => b[1] - a[1]);
console.log(arrayOfTuples);
// => [ [ '2', 5 ], [ '1', 4 ], [ '13', 4 ], [ '4', 1 ] ]
The sort command. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort Arrays of objects can be sorted by comparing the value of one of their properties.

Erlang Extract from Map

The below is a object in a record of type
requirements = [] :: [term()],
We extract the Requirements object like below
Requirements = Records#record.requirements
And I need the lat, long, radius independently from any.latlong_radius tuple. How can we extract it out ?
"requirements": {
"supply_type": {
"$tuple": [1, {
"$tuple": ["mobile_app", "nil", "nil"]
}]
},
"has_app_name": true,
"any": {
"latlong_radius": {
"$tuple": [33.042334, -96.734884, 16093.4]
}
},
},
-module(my).
-compile(export_all).
-record(requirements, {supply_type,
has_app_name,
any} ).
get_requirements() ->
#requirements{
supply_type = #{"$tuple" => [1, 2, 3]},
has_app_name = true,
any = #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}.
go() ->
Requirements = get_requirements(),
io:format("requirements: ~p~n", [Requirements]),
Any = Requirements#requirements.any,
#{"latlong_radius" :=
#{"$tuple" := [Lat, Lon, Rad]}
} = Any,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Lon, Rad]).
In the shell:
51> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
52> my:go().
requirements: {requirements,#{"$tuple" => [1,2,3]},
true,
#{"latlong_radius" =>
#{"$tuple" =>
[33.042334,-96.734884,16093.4]}}}
Lat: 33.042334, Lon: -96.734884, Rad: 16093.4
ok
On the other hand, if your data structure is maps all the way down (which makes everything you posted about records and list specifications irrelevant):
-module(my).
-compile(export_all).
get_requirements() ->
#{"requirements" =>
#{
"supply_type" => #{"$tuple" => [1, 2, 3]},
"has_app_name" => true,
"any" => #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}
}.
go() ->
Requirements = get_requirements(),
io:format("requirements: ~p~n", [Requirements]),
#{"requirements" :=
#{
"any" :=
#{
"latlong_radius" :=
#{
"$tuple" := [Lat, Lon, Rad]
}
}
}
} = Requirements,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Lon, Rad]).
In the shell:
70> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
71> my:go().
requirements: #{"requirements" =>
#{"any" =>
#{"latlong_radius" =>
#{"$tuple" => [33.042334,-96.734884,16093.4]}},
"has_app_name" => true,
"supply_type" => #{"$tuple" => [1,2,3]}}}
Lat: 33.042334, Lon: -96.734884, Rad: 16093.4
ok
However, with deeply nested maps trying to get the pattern match syntax correct is just too error prone, so I would use maps:get/2 instead:
-module(my).
-compile(export_all).
get_requirements() ->
#{"requirements" =>
#{
"supply_type" => #{"$tuple" => [1, 2, 3]},
"has_app_name" => true,
"any" => #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}
}.
go() ->
Map = get_requirements(),
io:format("Map: ~p~n", [Map]),
Requirements = maps:get("requirements", Map),
Any = maps:get("any", Requirements),
LLR = maps:get("latlong_radius", Any),
#{"$tuple" := [Lat, Long, Radius]} = LLR,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Long, Radius]).
Much, much simpler.
Just assign variable with double quote '' like below:
Latlong_radius = Any#any.latlong_radius
Res = Latlong_radius#latlong_radius.'$tuple'

node_redis get zrange withscores

Does anybody know how can I get members with scores by node redis?
I tried something like this:
client.ZRANGE(key, 0, -1, withscores, function(err, replies) {
});
Thanks.
This code looks good. Check out the following link for retrieving what you want :
http://ricochen.wordpress.com/2012/02/28/example-sorted-set-functions-with-node-js-redis/
Added the code here from that link example in case it is ever removed.
var rc=require('redis').createClient();
var _=require('underscore');
rc.zincrby('myset', 1, 'usera');
rc.zincrby('myset', 5, 'userb');
rc.zincrby('myset', 3, 'userc');
rc.zrevrange('myset', 0, -1, 'withscores', function(err, members) {
// the resulting members would be something like
// ['userb', '5', 'userc', '3', 'usera', '1']
// use the following trick to convert to
// [ [ 'userb', '5' ], [ 'userc', '3' ], [ 'usera', '1' ] ]
// learned the trick from
// http://stackoverflow.com/questions/8566667/split-javascript-array-in-chunks-using-underscore-js
var lists=_.groupBy(members, function(a,b) {
return Math.floor(b/2);
});
console.log( _.toArray(lists) );
});
rc.quit();
Seems your code is right. The following is the syntax to get zrange.
without score:
redisClient.zrange(keyName,start,stop,function(err,result){
//result is array
// every index will give you member name
})
Ex :
redisClient.zrange("mySortedset",-1,-1,function(err,result){
//result is array
// every index will give you member name
})
with score:
redisClient.zrange(keyName,start,stop,'withscores',function(err,result){
//result is array
// here even index will hold member
// odd index will hold its score
})
Ex :
redisClient.zrange("mySortedset",-1,-1,'withscores',function(err,result){
//result is array
// here even index will hold member
// odd index will hold its score
})
I tried with the prior accepted answers but i could not get the result i want and later i tried with the following code and got appropriate result,
Original output:
[ 'player:522',
'19685',
'player:164',
'19594',
'player:807',
'19171',
'player:694',
'19165',
'player:905',
'19108',
'player:859',
'19087',
'player:432',
'18973',
'player:515',
'18831',
'player:163',
'18750',
'player:4',
'18552' ]
Expected output:
{
"player:522": "19685",
"player:164": "19594",
"player:807": "19171",
"player:694": "19165",
"player:905": "19108",
"player:859": "19087",
"player:432": "18973",
"player:515": "18831",
"player:163": "18750",
"player:4": "18552"
}
Solution:
redisClient.ZREVRANGE('daily', 1, 10, 'WITHSCORES', function(err, result) {
result = _.fromPairs(_.chunk(result, 2));
return res.status(200).json(result);
});
The right approach for versions ^2.0,
var args = [ key,to, from ];
redisClient.zrevrangebyscore(args,function(err,data){
Vanilla JS Solution
Redis call:
redisClient.zrange(keyName, start, stop, 'withscores', function(err, result) {
// result.reduce ... (See below)
}
Here is a Vanilla-JS solution that I came up with pretty quickly.
For me, personally, it does not make sense to import underscore or any other library to perform such an easy task:
result.reduce(function (a, c, i) {
var idx = i / 2 | 0;
if (i % 2) {
a[idx].score = c;
} else {
a[idx] = { id: c };
}
return a;
}, []);
Assuming this input:
['player1', 13, 'player2', 11, 'player4', 7, 'player3', 3, 'player5', 0]
This function yields:
[
{ id: 'player1', score: 13 },
{ id: 'player2', score: 11 },
{ id: 'player4', score: 7 },
{ id: 'player3', score: 3 },
{ id: 'player5', score: 0 }
]
Here is another one to transform the result into a two-dimensional array:
result.reduce(function (a, c, i) {
var idx = i / 2 | 0;
if (i % 2) {
a[idx].push(c);
} else {
a[idx] = [c];
}
return a;
}, []);
which produces the following array:
[
[ 'player1', 13 ],
[ 'player2', 11 ],
[ 'player4', 7 ],
[ 'player3', 3 ],
[ 'player5', 0 ]
]
One line lambda version:
result.reduce((a, c, i) => i % 2 ? (a[i / 2 | 0].data = c, a) : (a[i / 2 | 0] = { id: c }, a), []);
Vanilla JS reduce works well here.
const result = [
'player:522',
'19685',
'player:164',
'19594',
'player:807',
'19171',
'player:694',
'19165',
'player:905',
'19108',
'player:859',
'19087',
'player:432',
'18973',
'player:515',
'18831',
'player:163',
'18750',
'player:4',
'18552'
]
const map = result.reduce((map, k, i, res) => {
if (i % 2 !== 0) {
map[res[i - 1]] = Number(k);
}
return map;
}, {})
map is now:
{
'player:522': 19685,
'player:164': 19594,
'player:807': 19171,
'player:694': 19165,
'player:905': 19108,
'player:859': 19087,
'player:432': 18973,
'player:515': 18831,
'player:163': 18750.65468,
'player:4': 18552
}
var data = []
results.map((result, index) => {
if (index % 2 == 0) {
data.push(results[index] = { player: results[index], score: results[index+1] })
}
})
console.log(data)

Getting to a list of lists in Groovy

I have an Object MyObject which has a List of ThatObjects where each ThatObject is a list of ThoseObjects.
MyObject {
List<ThatObject> thatObjects;
}
ThatObject {
List<ThoseObject> thoseObjects
}
If I have a handle to MyObject, is it possible to get a handle to all thoseObjects in one list joined to together? Without have to iterate and make the joined list myself?
Thanks
Given:
class MyObject {
List thatObjects
}
class ThatObject {
List thoseObjects
}
We can make a test object of:
def o = new MyObject( thatObjects:[ new ThatObject( thoseObjects:[ 1, 2 ] ),
new ThatObject( thoseObjects:[ 3, 4 ] ) ] )
Then a simple walk through the properties gives us:
assert o.thatObjects.thoseObjects == [ [1, 2], [3, 4] ]
And call flatten to get a single list:
assert o.thatObjects.thoseObjects.flatten() == [ 1, 2, 3, 4 ]
Or, you could use collectMany
assert o.thatObjects.collectMany { it.thoseObjects } == [ 1, 2, 3, 4 ]

How can I find the connected components of a graph in Perl?

I have the following collection of nodes and edges. What I want to do is to find all the distinct graph from it.
my %connections=(36=>[31],10=>[3,4],31=>[30,22],30=>[20],22=>[20,8],20=>[1],8=>[5],5=>[2],2=>[1,20], 3=>[7]);
In this example it will yield:
my %all_graph = {
graph1 => {36=>[31],31=>[30,22],30=>[20],22=>[20,8],20=>[1],8=>[5],5=>[2],2=>[1,20]}.
graph2 => {10=>[3,4], 3=>[7]}
};
Is there any existing algorithms that does that?
Use the Graph module:
#!/usr/bin/perl
use strict; use warnings;
use Graph;
my %connections = (
36 => [ 31 ],
10 => [ 3, 4],
31 => [ 30, 22],
30 => [ 20 ],
22 => [ 20, 8],
20 => [ 1 ],
8 => [ 5 ],
5 => [ 2 ],
2 => [ 1, 20 ],
3 => [ 7 ]
);
my $g = Graph->new( undirected => 1 );
for my $src ( keys %connections ) {
for my $tgt ( #{ $connections{$src} } ) {
$g->add_edge($src, $tgt);
}
}
my #subgraphs = $g->connected_components;
my #allgraphs;
for my $subgraph ( #subgraphs ) {
push #allgraphs, {};
for my $node ( #$subgraph ) {
if ( exists $connections{ $node } ) {
$allgraphs[-1]{$node} = [ #{ $connections{$node} } ];
}
}
}
use YAML; print Dump \#allgraphs;
Output:
[sinan#archardy SO]$ ./g
---
- 2:
- 1
- 20
20:
- 1
22:
- 20
- 8
30:
- 20
31:
- 30
- 22
36:
- 31
5:
- 2
8:
- 5
- 10:
- 3
- 4
3:
- 7
To find the connected components of an undirected graph you just do a BFS or DFS (Breadth/Depth first search).
Here some sample BFS code
my %connections=(36=>[31],10=>[3,4],31=>[30,22],30=>[20],22=>[20,8]
,20=>[1],8=>[5],5=>[2],2=>[1,20], 3=>[7]);
my $full_connections = {}; # Build a REAL graph with full 2-way edge lists
foreach my $node (keys %connections) {
foreach my $node2 (#{ $connections{$node} }) {
print "$node, $node2\n";
$full_connections->{$node}->{$node2} = 1;
$full_connections->{$node2}->{$node} = 1;
}
}
my %all_graph = ();
my $current_graph = 0;
my %visited = ();
my #to_visit = ();
foreach my $node (keys %$full_connections) {
next if exists $visited{$node};
# start the next segment
$current_graph++;
#to_visit=($node);
while (#to_visit) {
$node_to_visit = shift #to_visit;
#next if $visited{$node_to_visit};
$visited{$node_to_visit} = $current_graph;
push #to_visit, grep { !exists $visited{$_} }
keys %{ $full_connections->{$node_to_visit} };
}
}
# Now reconstruct %all_graph from %visited - left as exercise for the reader
print Data::Dumper->Dump([\%visited]);
I'd suggest the following algorithm:
1.) Move all nodes into a working set N.
2.) Starting with an arbitrary node perform a graph search (depth-first or breadth-first). Add all visited nodes and edges to the first subgraph, remove visited nodes from N
3.) If N is non-empty, select the next starting node and go to step 2.) for the next subgraph.

Resources