Dataflow: Multiple Fanout / Combination of N Arrays - google-cloud-dataflow

Say I have a data key1 = [1, 2, 3] and key2 = [a, b, c] where a and b are key value where value is an array. I need to flatMap this and produce [1,a], [2, a], [3, a], [1, b], [2, b], [3, b], [1, c], [2, c], [3, c]. What is the best way to achieve this in Dataflow if we were to create multi-level fanout (in other words, n way combination of arrays)?
Note: example is just over two arrays so I can do flatmap with map, but this need to handle n array combinations.

Related

What's the quickest way to swap values of 2d array in swift?

I'm trying to efficiently flip values of a 2d array from [[a, b], [a, b], [a, b]] to [[b, a], [b, a], [b, a]]
You can combine the map and reversed function.
As per Swift documentation
for map function time complexity is O(N)
for reversed - time complexity is O(1)
The total time complexity is = O(N)
var arr = [["a", "b"], ["a", "b"], ["a", "b"]]
arr = arr.map { $0.reversed() }
print(arr)

recursive function for 2D list

I'm very new to f# so please forgive my ignorance. I have a 2D list of int. I'm trying to make a function that will return another 2D list which will contain only the lowest tail items. Something like this:
[[2, 3]
[2, 4]
[2, 5] [[2, 3]
[3, 8] => [3, 2]
[3, 6] [4, 1]]
[3, 2]
[4, 1]]
I can find min value of 1D list with
let findMin items =
match items with
| [] -> ()
| head :: tail ->
let rec recMin minSoFar items =
match items with
| [] -> minSoFar
| head :: tail ->
if head < minSoFar then
recMin head tail
else
recMin minSoFar tail
recMin head tail
which I copied from this answer. Any help would be appreciated.
You are talking about 2D lists, but your sample input is actually a list of lists of tuples (where each list contains just a single tuple value). I assume this is a typo and you wanted to write ; rather than ; and create an actual 2D list:
let input =
[ [2; 3]
[2; 4]
[2; 5]
[3; 8]
[3; 6]
[3; 2]
[4; 1] ]
Now, the three items that you want to get as the result seem to be the three sub-lists with the smallest last element in the list. The easiest way for obtaining those would be to sort the list of lists by the last element of sub-lists and then take top 3:
input
|> List.sortBy List.last
|> List.take 3
This returns the three sub-lists you want, but not in the original order. If you wanted them in the original order, then you could first find the third smallest last element and then use that to filter the original list:
let last =
input
|> List.map List.last
|> List.sort
|> List.item 2
input
|> List.filter (fun l -> List.last l <= last)
Note that my code does not correctly handle cases such as input smaller than 3 or empty lists, but it should give you an idea about one way of solving this problem.

How to use whole list function argument in Erlang

I can understand most of the [H|T] examples I read in the docs. I usually means that I want to use either the H or the T part of the list. What if I want to use the whole list instead. Sample code:
-module(module_variable).
-export([main/0, list_suffix/1]).
variable() -> [1, 2, 3, 4, 5].
list_suffix([_H|T]) ->
lists:suffix(variable, T).
main() ->
io:fwrite("~p~n", [list_suffix([4, 5])]).
The error I get is:
6> module_variable:list_suffix([1,[4, 5]]).
** exception error: bad argument
in function length/1
called as length(variable)
in call from lists:suffix/2 (lists.erl, line 205)
Help is appreciated.
You can use multiple values from the front of the list. You cannot skip an arbitrary number of values in the middle. Since in your code you don't know how many elements from the head you want to match ahead of time, pattern matching can't do this for you.
Some examples:
Setup:
1> A = [1, 2, 3, 4, 5].
[1,2,3,4,5]
Matches first 2 elements of list
2> [1, 2 | _ ] = A.
[1,2,3,4,5]
% Can pattern match to extract values
3> [B, C | _ ] = A.
[1,2,3,4,5]
4> B.
1
5> C.
2
Can match some constant values and also assign
6> [1, 2, D | _ ] = A.
[1,2,3,4,5]
Can match whole list
7> [1, 2, 3, 4, 5] = A.
[1,2,3,4,5]
% Can't skip over elements in the middle
8> [1, 2| [4, 5]] = A.
** exception error: no match of right hand side value [1,2,3,4,5]
% This works, though not useful most of the time:
9> [1, 2, 3 | [4, 5]] = A.
[1,2,3,4,5]
% Can assign every element
10> [B, C, D, E, F] = A.
[1,2,3,4,5]
11> E.
4
12> F.
5
% If you don't use a pipe, the length has to match exactly
13> [B, C, D, E] = A.
** exception error: no match of right hand side value [1,2,3,4,5]

Evaluating expressions

I'm doing some past-papers and need to know if I am correct here.
Give step-by-step evaluations of the following expressions:
foo(0,[2,3,1])
foo(0,[4,0,1])
where foo is defined like this:
foo(_,[]) -> [];
foo(Y,[X|_]) when X==Y -> [X];
foo(Y,[X|Xs]) -> [X | foo(Y,Xs) ].
My answers:
1.
Foo(0, [2, 3, 1])
[2 | foo(0, 3, 1) ]
[2, 3| foo(0, 1) ]
[2, 3, 1 | foo (0)]
[2, 3, 1]
2.
Foo(0, [4, 0, 1])
[4 | foo(0, 0,1])
[4, 0]
Am I correct here?
At least the function parameter are wrong, I would say:
1.
foo(0,[2,3,1])
[2|foo(0,[3,1])] % 3rd clause
[2|[3|foo(0,[1])]] % 3rd clause
[2|[3|[1|foo(0,[])]]] % 3rd clause
[2|[3|[1|[]]]] % 1st clause
[2,3,1]
2.
foo(0,[4,0,1])
[4|foo(0,[0,1])] % 3rd clause
[4|[0]] % 2nd clause
[4, 0]

Erlang, Function which return the lenghts of the lists?

I'm trying to implement a function
llen(ListOfLists)
which returns a list containing the lengths of the lists in ListOfLists. Function should use lists:map/2.
Example:
1> funs:llen([[1, 2, 3], [], [4, 5]]).
[3, 0, 2]
I know how to calculate length for one list:
list([]) -> 0;
list([_|T]) -> 1+list(T).
but i don't know how to do this for multiple lists using funs and lists.
lists:map/2 is a higher order function that applies a function for each element of a list. erlang:length/1 is a function that returns the length of a list.
Apply erlang:length/1 on each element of your list using lists:map/2:
lists:map(fun erlang:length/1, [[1, 2, 3], [], [4, 5]])
And when you get to list comprehensions:
53> L = [[1, 2, 3], [], [4, 5]].
[[1,2,3],[],[4,5]]
54> [length(X) || X <- L].
[3,0,2]
A list comprehension is like a for-loop in other languages, and this one reads like:
length(X) for X in L
length(X) || X <- L
The outer [ ] serves to gather up all the results into a list.

Resources