usage of function with paramenters

11 views
Skip to first unread message

Michal Shmueli-Scheuer

unread,
Mar 23, 2010, 8:10:26 AM3/23/10
to Jaql Users
Hi,

I have this 2 statements:
userCategoricalActress -> transform {$.userid, NumDocs: $.userNumDocs,
$.actress};
userCategoricalGenre -> transform {$.userid, NumDocs: $.userNumDocs,
$.genre};

I want to use function to define the statement, I did the following:

runCat = fn(userCategorical, attribute) (
userCategorical -> transform {$.userid, NumDocs: $.userNumDocs,
$.attribute}
);

and then did: runCat(userCategoricalAcress, "actress");
I'm getting:

runProfiler($userCategorical, "actress");
[
{
"userid": 101,
"NumDocs": 1
},
{
"userid": 101,
"NumDocs": 2
}]

whereas for: userCategorical -> transform {$.userid, NumDocs:
$.userNumDocs, $.actress};
[
{
"userid": 101,
"NumDocs": 1,
"actress": "Barber-Gillian-(I)"
},
{
"userid": 101,
"NumDocs": 2,
"actress": "Bradley-Lisa"
}]

It seems that it can't define the "attribute" on the fly.
Is there a way to do such thing?

thanks,
Michal

Kevin Beyer

unread,
Mar 23, 2010, 12:29:05 PM3/23/10
to jaql-...@googlegroups.com, Jaql Users
This was a little clearer when we required $ on all variables, but it
is still possible today. $.attribute is looking for a field called
"attribute" in the record of the variable "$". A longer way to write
this is $.("attribute") -- field names are strings and the parenthesis
indicates a computed field name, which here is a literal string
expression. What you want is $.(attribute), which computes the field
name from the attribute variable.

-K

On Mar 23, 2010, at 5:10 AM, Michal Shmueli-Scheuer <michal....@gmail.com
> wrote:

> --
> You received this message because you are subscribed to the Google
> Groups "Jaql Users" group.
> To post to this group, send email to jaql-...@googlegroups.com.
> To unsubscribe from this group, send email to jaql-users+...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/jaql-users?hl=en
> .
>

Michal Shmueli-Scheuer

unread,
Mar 23, 2010, 4:13:48 PM3/23/10
to Jaql Users
Hi Kevin,

thanks, it did seems to work out for the example that i gave, however,
I was trying to do the same trick with group by and it didn't work:

aggMovie = fn($userMovies, attribute1, attribute2, attribute3) (

$userMovies ->expand unroll $.(attribute1) ->
group by g = ({$.userid,$.(attribute2)}) into {g.*, userNumDocs:
count($[*].docid), numMovie: $[0].NumMovies, docs: $[*].docid}
);

aggMovie ($userMovies, "genres", "genres.genre") ;
the first line (exapand works fine), the group by didn't.
should i use other syntax for the group by?


(this is the original one:
$userMovies ->expand unroll $.genres ->
group by g = ({$.userid,$.genres.genre}) into {g.*, userNumDocs:
count($[*].docid), numMovie: $[0].NumMovies, docs:$[*].docid};

with output:
[
{
"genre": "Adventure",
"userid": 101,
"userNumDocs": 1,
"numMovie": 2,
"docs": [
1
]
)

thanks ,
Michal


On Mar 23, 6:29 pm, Kevin Beyer <kevin.beyer.j...@gmail.com> wrote:
> This was a little clearer when we required $ on all variables, but it  
> is still possible today. $.attribute is looking for a field called  
> "attribute" in the record of the variable "$". A longer way to write  
> this is $.("attribute") -- field names are strings and the parenthesis  
> indicates a computed field name, which here is a literal string  
> expression. What you want is $.(attribute), which computes the field  
> name from the attribute variable.
>
> -K
>

> On Mar 23, 2010, at 5:10 AM, Michal Shmueli-Scheuer <michal.shmu...@gmail.com

Michal Shmueli-Scheuer

unread,
Mar 23, 2010, 4:09:52 PM3/23/10
to Jaql Users
Hi Kevin,

thanks, this one seems to work fine now. However, I was trying to do
the same trick with group by with parameter:

gMovie = fn($userMovies, attribute1, attribute2) (

$userMovies ->expand unroll $.(attribute1) ->
group by g = ({$.userid,$.(attribute2)}) into {g.*, userNumDocs:
count($[*].docid), numMovie: $[0].NumMovies, docs: $[*].docid}
);

and it did work for the first line (the expand), but didn't work for
the group by. should i use something else?

(This is the original one:


$userMovies ->expand unroll $.genres->
group by g = ({$.userid,$.genres.genre}) into {g.*, userNumDocs:
count($[*].docid), numMovie: $[0].NumMovies, docs:$[*].docid};

and the output is:


[
{
"genre": "Adventure",
"userid": 101,
"userNumDocs": 1,
"numMovie": 2,
"docs": [
1
]
)

thanks again,


Michal
On Mar 23, 6:29 pm, Kevin Beyer <kevin.beyer.j...@gmail.com> wrote:

> This was a little clearer when we required $ on all variables, but it  
> is still possible today. $.attribute is looking for a field called  
> "attribute" in the record of the variable "$". A longer way to write  
> this is $.("attribute") -- field names are strings and the parenthesis  
> indicates a computed field name, which here is a literal string  
> expression. What you want is $.(attribute), which computes the field  
> name from the attribute variable.
>
> -K
>

> On Mar 23, 2010, at 5:10 AM, Michal Shmueli-Scheuer <michal.shmu...@gmail.com

Kevin Beyer

unread,
Mar 26, 2010, 5:49:47 PM3/26/10
to Jaql Users
The problem is a bit subtle: You are trying to pass a path
'genres.genre', but it is being treated as a single attribute name.
Field names in Jaql (and JSON) are arbitrary strings; Jaql just lets
you omit the quotes in some common special cases. You can solve your
problem by passing in a function that picks out your attribute. Let
me give some simple examples:

f1 = fn(data,attr) data -> transform { $.(attr) };

recs1 = [{a:{b:1},c:2},{a:{b:3},c:4}];

recs1 -> f1('a'); // [ {a: {b:1}}, {a: {b:3}} ]

recs1 -> f1('a.b'); // [ {}, {} ]

recs2 = [{a:{b:1},c:2,'a.b':5},{a:{b:3},c:4,'a.b':6}];

recs2 -> f1('a.b'); // [ {"a.b": 5}, {"a.b": 6} ]

f2 = fn(data,projfn) data -> transform { $.c, projfn($).* };

recs1 -> f2(fn(r) { r.a }); // [ {c: 2, a:{b: 1}}, {c: 4, a:{b: 3}} ]

recs1 -> f2(fn(r) { r.a.b }); // [ {c: 2, b: 1}, {c: 4, b: 3} ]

f3 = fn(data,setfn)
data
-> expand each r (
setfn(r) -> transform { r.c, i: $ }
)
;

recs3 = [{a:[{b:1},{b:2}],c:2},{a:[{b:3},{b:4}],c:4}];

recs3 -> f3(fn(r) r.a[*].b); // [c:2,i:1}, {c:2,i:2}, {c:4,i:3}, {c:
4,i:4} ]

Reply all
Reply to author
Forward
0 new messages