If I have the array data and the array of functions [fn1, fn2, fn3], what's the right way with Ramda to get
[fn1(data[0]), fn2(data[1], fn3(data[2]), ...]
Basically, I want to call each function with the value that shares an array index in the data as its parameter, and get an array of the results.
You'll want to use zipWith with call:
R.zipWith(R.call, fns, data)
Related
I'm having some issues getting expected results from set_difference(). I assumed I was comparing two dynamic arrays, but I'm not sure where the gap is. The only additional insight I have is that when I compare the two arrays using the gettype() function, I get the following:
First array
Created using a make_list aggregation, e.g.
| summarize inv_list = make_list(Date)
When I run gettype() on the array:
"type_inv_list": array
Second array
Created through a scalar function
let period_check_range = todynamic(range(make_datetime(start_date), datetime_add('day',8,make_datetime(start_date)),1d));
When I run gettype() on the array:
"type_range___scalar_90e56a216d8942f28e6797e5abc35dd9": array
Any guidance on how to make these arrays work so I can use the set_difference() function?
You're missing toscalar() (see doc) in the first array. When you run | summarize ... you get a table as a result, but what you actually want is a single scalar, what's why toscalar() is needed.
Here's how to achieve what you want:
let StartDate = ago(10d);
let Array1 = toscalar(MyTable | summarize make_set(Timestamp));
let Array2 = todynamic(range(make_datetime(StartDate), datetime_add('day',8,make_datetime(StartDate)),1d));
print set_difference(Array2, Array1)
By the way, you probably want to use make_set and not make_list as you're not interested in duplicate values.
I wanted to make a function that looks at every column of a DataFrame and return a boolean, so I end up with an array of booleans. Here is the code
# some random dataframe
df = DataFrame([1:3, 4:6])
# a function that returns an array of boolean
function some_bool_fn(df)::Array{Bool}
array_of_arrays = colwise(df) do sdd3
# for illustration only
return true
end
array = [a[1] for a in array_of_arrays]
return array
end
# calling the function
some_bool_fn(dd3)
This works except I find the line
array = [a[1] for a in array_of_arrays]
a bit wasteful. Basically I get an array of arrays as the output of colwise, so I then had to put the array of arrays into a simple array of bools. Is there a way to write the code so I can avoid this line of code?
As #Gnimuc commented this behaviour is changing.
If you look at master branch: https://github.com/JuliaData/DataFrames.jl/blob/master/src/groupeddataframe/grouping.jl#L241 you'll see another version. You could probably copy it:
mycolwise(f, d::AbstractDataFrame) = [f(d[i]) for i in 1:ncol(d)]
Is there a way to access dictionary keys in Swift 2 using an index?
var dict = ["item1":1, "item2":2, "item3":3]
dict.keys[0]
results in the error:
15:29: note: overloads for 'subscript' exist with these partially
matching parameter lists: (Base.Index), (Range),
(Self.Index)
print("key=" + dict.keys[i])
I saw an examples from August (Swift: dictionary access via index) doing this:
dict.keys.array[0]
At least in Swift 2, there isn't an array object on dictionary keys.
In Swift 2 the equivalent of dict.keys.array would be Array(dict.keys):
let dict = ["item1":1, "item2":2, "item3":3]
let firstKey = Array(dict.keys)[0] // "item3"
Note: of course, as dictionaries are unordered collections, "first key" of the resulting array may not have a predictable value.
do not rely on order of items in a dictonary, using array directly would be better in your case, you can also handle key/value in array with making array units objects.
I'm using the latest(0.117) Presto and trying to execute CROSS JOIN UNNEST with complex JSON array like this.
[{"id": 1, "value":"xxx"}, {"id":2, "value":"yy"}, ...]
To do that, first I tried to make an ARRAY with the values of id by
SELECT CAST(JSON_EXTRACT('[{"id": 1, "value":"xxx"}, {"id":2, "value":"yy"}]', '$..id') AS ARRAY<BIGINT>)
but it doesn't work.
What is the best JSON Path to extract the values of id?
This will solve your problem. It is more generic cast to an ARRAY of json (less prone to errors given an arbitrary map structure):
select
TRANSFORM(CAST(JSON_PARSE(arr1) AS ARRAY<JSON>),
x -> JSON_EXTRACT_SCALAR(x, '$.id'))
from
(values ('[{"id": 1, "value":"xxx"}, {"id":2, "value":"yy"}]')) t(arr1)
Output in presto:
[1,2]
... I ran into a situation where a list of jsons was nested within a json. My list of jsons had an ambiguous nested map structure. The following code returns an array of values given a specific key in a list of jsons.
Extract the list using JSON EXTRACT
Cast the list as an array of jsons
Loop through the json elements in the array using the TRANSFORM function and extract the value of the key that you are interested in.
>
TRANSFORM(CAST(JSON_EXTRACT(json, '$.path.toListOfJSONs') AS ARRAY<JSON>),
x -> JSON_EXTRACT_SCALAR(x, '$.id')) as id
You can cast the JSON into an ARRAY of MAP, and use transform lambda function to extract the "id" key:
select
TRANSFORM(CAST(JSON_PARSE(arr1) AS ARRAY<MAP<VARCHAR, VARCHAR>>), entry->entry['id'])
from
(values ('[{"id": 1, "value":"xxx"}, {"id":2, "value":"yy"}]')) t(arr1)
output:
[1, 2]
Now, you can use presto-third-functions , It provide json_array_extract function, you can extract json array info like this:
select
json_array_extract_scalar(arr1, '$.book.id')
from
(values ('[{"book":{"id":"12"}}, {"book":{"id":"14"}}]')) t(arr1)
output is:
[12, 14]
I finally gave up finding a simple JSON Path to extract them.
Instead, I wrote a redundant dirty query like the following to make the task done.
SELECT
...
FROM
(
SELECT
SLICE(ARRAY[
JSON_EXTRACT(json_column, '$[0].id'),
JSON_EXTRACT(json_column, '$[1].id'),
JSON_EXTRACT(json_column, '$[2].id'),
...
], JSON_ARRAY_LENGTH(json_column)) ids
FROM
the.table
) t1
CROSS JOIN UNNEST(ids) AS t2(id)
WHERE
...
I still want to know the best practice if you know another good way to CROSS JOIN them!
Let's say I have a function which internally creates a new Platform::Collections::Vector^ and adds a few items to it. If the function returns a Platform::Collections::VectorView^ which was fetched by calling the GetView() function on the Vector, could:
The Vector be garbage collected (that is, would it's ref-count go down to 0) when the function returns? Or would calling GetView() increase the ref-count by 1?
If so, what happens to the VectorView that got returned? Does it hold a copy of all the items from the Vector that was used to create the VectorView?
In other words, is the function below safe?
Platform::Collections::VectorView^ MyClass::MyFunction()
{
Platform::Collections::Vector<Platform::String^>^ vec =
ref new Platform::Collections::Vector<Platform::String^>();
vec->Append(L"Hello");
vec->Append(L"World");
return vec->GetView();
}