groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mehdi Elaoufir <melaou...@gmail.com>
Subject Re: Functional question : how to combine closures with same signature?
Date Wed, 19 Aug 2015 11:36:13 GMT
Thanks a lot Dinko!


On Wed, Aug 19, 2015 at 11:43 AM, Dinko Srko─Ź <dinko.srkoc@gmail.com> wrote:

>     class LogicallyChallenged {
>         static Closure and(Closure left, Closure right) {
>             { a, b -> left(a, b) && right(a, b) }
>         }
>
>         static Closure or(Closure left, Closure right) {
>             { a, b -> left(a, b) || right(a, b) }
>         }
>     }
>
>     def gt = { a, b -> a > b },
>         eq = { a, b -> a == b },
>         lt = { a, b -> a < b }
>
>     def equal = use (LogicallyChallenged) {
>         (gt | eq) & (lt | eq)
>     }
>
>     assert equal(1, 1)
>     assert equal('a', 'a')
>     assert !equal(1, 2)
>     assert !equal('a', 'b')
>

Mime
View raw message