librelist archives

« back to archive

Advice about model:filter

Advice about model:filter

From:
Victor Young
Date:
2011-11-25 @ 04:20
If we want to get the instances that age is greater than 18 or gender
equals male,we use model:filter{'or',age=gt(18),gender='male'},
But if we want more complex condition such as (age is greater than 18 and
age is less than 40) or gender equals male, we cannot use model:filter() to
do this.
So, consider this way:
model:filter(function(instance) return (instance.age>18 and
instance.age<40) or instance.gender='male' end)
Use an anonymous function to do the judgement while iteration makes the
filter condition flexible and logical

Re: [bamboo] Advice about model:filter

From:
道哥
Date:
2011-11-25 @ 05:28
Yes, about the old design, it can not deal all logical cases. Your advice 
is very good, although some abundant...

At 2011-11-25 12:20:23,"Victor Young" <littlehaker@gmail.com> wrote:

If we want to get the instances that age is greater than 18 or gender 
equals male,we use model:filter{'or',age=gt(18),gender='male'},
But if we want more complex condition such as (age is greater than 18 and 
age is less than 40) or gender equals male, we cannot use model:filter() 
to do this.
So, consider this way:
model:filter(function(instance) return (instance.age>18 and 
instance.age<40) or instance.gender='male' end)
Use an anonymous function to do the judgement while iteration makes the 
filter condition flexible and logical

Re: [bamboo] Advice about model:filter

From:
Victor Young
Date:
2011-11-25 @ 06:21
If there is lambda expression in lua, it will be more brief.

2011/11/25 道哥 <daogangtang@163.com>

> Yes, about the old design, it can not deal all logical cases. Your advice
> is very good, although some abundant...
>
>
> At 2011-11-25 12:20:23,"Victor Young" <littlehaker@gmail.com> wrote:
>
> If we want to get the instances that age is greater than 18 or gender
> equals male,we use model:filter{'or',age=gt(18),gender='male'},
> But if we want more complex condition such as (age is greater than 18 and
> age is less than 40) or gender equals male, we cannot use model:filter() to
> do this.
> So, consider this way:
> model:filter(function(instance) return (instance.age>18 and
> instance.age<40) or instance.gender='male' end)
> Use an anonymous function to do the judgement while iteration makes the
> filter condition flexible and logical
>
>
>
>