Cumulative sum in jq - jq

I have a series of [timestamp, count] pairs in an array and a want to compute the cumulative sum at each timestamp using jq. How could I do that?
Here a sample data set:
[
[1431047957699, 1],
[1431047958269, 1],
[1431047958901, 1],
[1431047959147, -1],
[1431047960164, 1]
]
And the expected result:
[1431047957699, 1],
[1431047958269, 2],
[1431047958901, 3],
[1431047959147, 2],
[1431047960164, 3]
Is it possible to do this with jq?

The following is quite general (e.g. it can be used with an array of objects):
def accumulate(f):
reduce .[1:][] as $row
([.[0]];
. as $x
| $x + [ $row | (f = ($x | .[length-1] | f) + ($row|f) ) ] );
accumulate(.[1])
If you are using a sufficiently recent version of jq, then "$x |
.[length-1]" can be simplified to "$x[-1]".
Solution using foreach
If your jq has foreach, then the following variant can be used. It would be particularly appropriate if a stream of values rather than array is wanted.
def accumulates(f):
foreach .[] as $row
(0;
. + ($row | f) ;
. as $x | $row | (f = $x));
Usage:
For a stream: accumulates(.[0])
For an array: [accumulates(.[0])

Take a functional approach to this and create an update function that will create the updated values with the cumulative sum.
def accumulate(acc):
select(length > 0) |
(.[0][1] + acc) as $next |
(.[0] | .[1] = $next), (.[1:] | accumulate($next))
;
[accumulate(0)]
Here, we break the array into "head" and "tail" updating the head with the current sum and recursively update the tail. The results are placed back into a new array.

Related

Understanding Erlang Basic recursion with function guards

I've been looking at some Erlang recursion examples asked earlier here on stackoverflow.
Specifically with this question Erlang basic recursion with guards
But I can't quite understand how the code works. So I have created a module to see the results it returns with a simple list with 3 elements.
-module(recursion).
-export([start/0]).
start() ->
List = [1,2,3],
Final = delete(1,List),
Final.
delete(_, []) ->
io:format("return []~n~n~n"),
[];
delete(Del, [Del|Xs]) ->
io:format("~p =:= ~p~n",[Del,Del]),
io:format("delete(~p, ~p)~n~n~n~n",[Del,Xs]),
delete(Del, Xs);
delete(Del, [X|Xs]) ->
io:format("~p =/= ~p~n",[Del,X]),
io:format(" [~p|delete(~p, ~p)]~n~n~n~n",[X,Del,Xs]),
[X|delete(Del, Xs)].
And this is the result of the log
1> recursion:start().
1 =:= 1
delete(1, [2,3])
1 =/= 2
[2|delete(1, [3])]
1 =/= 3
[3|delete(1, [])]
return [] The result of the 'Final' variable of the main function, shouldn't it be []?
bacause [3|delete(3, [])] in the last call matches with delete(_, []) -> []
or is it this way? [2,[3,[]]] -> [2,3]
[2,3]
2>
My question is:
Every time the program calls delete(Del, [X|Xs]) ->, is the function returning the value to the previous call?
Is it being stored somewhere?
or is it just something like that? [2,[3,[]]] -> [2,3]
edit:
I think I have found the solution in this link, about how the final result is built
https://learnyousomeerlang.com/starting-out-for-real#lists
where
13> List = [2,3,4].
[2,3,4]
14> NewList = [1|List].
[1,2,3,4]
So [2|[3|[]]] -> [2,3]
is that so?
Yes.
This is how the function works:
If the head of the list matches the first argument, 1 in this case, then the head of the list isn't saved anywhere, i.e. it's skipped, and the function is called again with the tail of the list:
delete(Del, [Del|Xs]) ->
delete(Del, Xs);
If the head of the list does NOT match the first argument, then the head of the list is saved by adding it to a result list:
[X|delete(Del, Xs)].
When the list is empty, the function returns [], which is very important when cons'ing elements together:
[3 | f(X) ]
if f(X) does not return a list at some point, then the list won't be a proper list. A proper list, such as:
[1, 2, 3]
is equivalent to:
[1 | [2 | [3 | [] ]]]
as you can see here:
2> [1 | [2 | [3 | [] ]]].
[1,2,3]
When you write:
[X|delete(Del, Xs)]
that's a little be tricky, and you need some experience to know how that works. You can understand things better, by writing out by hand what is happening:
delete(1, [1,2,3])
|
V
delete(1, [2, 3])
|
V
[2 | delete(1, [3]) ] %% Can't know the result here without determining the return value of delete(1, [3])
|
V
[3 | delete(1, []) ] %% Can't know the result here without determining the return value of delete(1, [])
|
V
[]
Once, you've got the return value [], because there are no more function calls in the result, now you can move upwards substituting:
delete(1, [1,2,3])
|
V
delete(1, [2, 3])
|
V
[2 | delete(1, [3]) ]
|
V
[3 | [] ]
And substituting again:
delete(1, [1,2,3])
|
V
delete(1, [2, 3])
|
V
[2 | [3 | [] ] ]
which is equivalent to:
[2, 3]
Here is a conceptually simpler version of delete():
start() ->
List = [1,2,3],
Final = delete(1,List),
Final.
delete(Term, List) ->
delete(Term, List, _Acc=[]).
delete(_, [], Acc) ->
Acc;
delete(Term, [Term|Xs], Acc) ->
delete(Term, Xs, Acc);
delete(Term, [X|Xs], Acc) ->
delete(Term, Xs, [X|Acc]).
However, the result is:
[3, 2]
So, when you use an accumulator variable, you need to reverse the final result:
delete(_, [], Acc) ->
lists:reverse(Acc);

Invalid_argument "String.sub / Bytes.sub"

I have a small problem on an exercice that i'm doing.
I try to recursively count vowels in a String but i have this strange error popping.
Can someone explain me why ?
let rec nb_voyelle = function chaine ->
if chaine == "" then
0
else
let length = (String.length chaine)-1 in
let p_length = String.sub chaine 0 length in
match chaine.[length] with
| 'a' | 'e' | 'i' | 'o' | 'u' | 'y' -> 1 + nb_voyelle p_length
| _ -> 0 + nb_voyelle p_length
;;
Answer is i used "==" to compare the string in my final case which is not the good pervasive to test equality between two elements.
As such, the case (String.sub "" 0 -1) happens and the function fail raising this error.

Julia way of searching tokens in integer arrays

Let's say I have buffer=Int[1,2,3,2,3] and token=[2,3].
Is there any preferred way of searching the occurrence of token in buffer to find [2,4] as the answer.
Or, perhaps, is there any split equivalent function for the integer arrays in julia?
(I know how I can perform this operation using 2 nested loops. However, I am especially interested if there is a more Julian way of doing this.)
Because Julia doesn't have conditionals in list comprehensions, I would personally use filter(). Thus if arr = Int64[1,2,3,4,5,2,3,6,2,3,3,2,2]:
filter(x -> arr[x] == 2 && arr[x + 1] == 3, 1 : length(arr) - 1)
=> [2,6,9]
To make it a little more reusable:
pat = [2,3]
filter(x -> arr[x : x + length(pat) - 1] == pat, 1 : length(arr) - length(pat) + 1)
=> [2,6,9]
Julia does have built-ins like find([fun], A), but there's no way that I'm aware of to use them to return indexes of an ordered sublist.
Of course it's arguably more legible to just
ndxs = Int64[]
for i = 1:length(arr)-1
if arr[i] == 2 && arr[i+1] == 3
push!(ndxs, i)
end
end
=> [2,6,9]
For practice I have also made trial-and-errors and the following patterns have worked for Julia0.4.0. With A = Int[1,2,3,2,3] and pat = Int[2,3], the first one is
x = Int[ A[i:i+1] == pat ? i : 0 for i=1:length(A)-1 ]
x[ x .> 0 ] # => [2,4]
the second one is
x = Int[]
[ A[i:i+1] == pat ? push!(x,i) : 0 for i=1:length(A)-1 ]
#show x # => [2,4]
and the third one is
find( [ A[i:i+1] == pat for i=1:length(A)-1 ] ) # => [2,4]
(where find() returns the index array of true elements). But personally, I feel these patterns are more like python than julia way...

Cant figure out what Error means and how to solve it

Here is the error code I have:
Characters 2004-2008
Error: The type constructor list expects 1 argument(s),
but is here applied to 0 argument(s)
Here is the Ocaml code I have written:
let createHuffmanTree (s:string) = match s with
| "" -> Empty (*Return an empty huffman tree ifstring is empty*)
| _ -> let rec ca (l: list) (a: huffman) = match l with (*ERROR SHOULD BE ON THIS LINE*)
| [] -> a (*If list is [] return the huffman tree*)
| x,_ -> fold_left (ca) l level(leaf(x),a) (*For all the ellement of the list create a level and corresponding leaf*)
in ca listFreq(explode(s)) Empty (*Start with an empty tree*)
Note:
explode take a string return it as a list of char
ex:
# explode "Test";;
- : char list = ['T'; 'e'; 's'; 't']
listFreq take the list from explode and return a pair of char * int being the char and the number of time it is found in the list return by explode
ex:
# listeFreq (explode "AABCAABADBACAAB");;
- : (char * int) list = [('A', 8); ('B', 4); ('C', 2); ('D', 1)]
The Huffman Tree I am trying to create take a string (for exemple "AAABBC")
and do this:
level
| |
v v
leaf A level
| |
v v
leaf B leaf C
The tree could also be Empty
Question:
I have no idea how to solve the error code. Could someone point out a solution?
I suppose that instead of (l: list), you should have something like (l: 'a list).

F# : Writing a function that builds a list of tuples recursively and change a mutable variable

This question is related to this previous thread.
I followed Tomas's suggestion using this piece code, and all works fine:
let GetSameColorNeighs (grid:Option<Ball>[,], row, col, color:Color) =
let rec loop (row, col) = seq {
if not (row < 0 || col < 0 || row > MaxLineNumber - 1
|| col > BallsPerLine - 1) then
let ball = grid.[row,col]
match ball with
| Some(ball) ->
if (!ball.visited = false || not <| ball.color.Equals(color)) then
// Not sure what you want here - yield items using 'yield'?
// [row , col]
else
ball.visited := true
yield row, col // Add single item to results
yield! loop(row + 1, col + 1) // Add all generated to results
yield! loop(row - 1, col - 1) // -- || --
| None -> () }
loop(row, col) |> Seq.toList
The code above iterate through an array 2d of "balls" and return a list of index of adjacent balls with the same color.
Now I have to modify the function in way that it returns also a boolean indicating if at least one ball of the list satisfy a certain condition. I changed the code this way but seems that I can't assign a mutable value inside that code:
let GetSameColorNeighs (grid:Option<Ball>[,], row, col, color:Color) : List<int * int> * bool =
let mutable b : bool = false
let rec loop (row, col) = seq {
if not (row < 0 || col < 0 || row > MaxLineNumber - 1
|| col > BallsPerLine - 1) then
let ball = grid.[row,col]
match ball with
| Some(ball) ->
if (ball.visited = true || not <| ball.color.Equals(color)) then
()
else
//HERE's THE PROBLEM
if (ball_satisfy_a_certain_condition) then
b <- true
ball.visited := true
yield row, col // Add single item to results
yield! loop(row + 1, col + 1) // Add all generated to results
yield! loop(row - 1, col - 1) // -- || --
| None -> () }
loop(row, col) |> Seq.toList, b
It seems that a mutable variable can't be acquired by a closure (I don't know what it means).
So I have 2 questions:
why is the above assignment to a mutable variable wrong?
How should I refactor my code to achieve this goal?
In short, you have to use ref variables instead of mutable variables.
While mutable variables are allocated on the stack, ref variables are heap-based. After each time your loop function is invoked, mutable values are wiped out when ref values are still there. Therefore, only ref values are valid to return in GetSameColorNeighs.
This question has been asked many times here. See The mutable variable 'i' is used in an invalid way.? and this blog post for more in-depth discussion.

Resources