Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38187 > unrolled thread
| Started by | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| First post | 2018-03-16 19:26 +0100 |
| Last post | 2018-03-17 03:10 +0100 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Lambda dereference Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-16 19:26 +0100
Re: Lambda dereference Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-16 19:32 +0100
Re: Lambda dereference Andreas Leitgeb <avl@logic.at> - 2018-03-16 23:42 +0000
Re: Lambda dereference Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-17 03:10 +0100
| From | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| Date | 2018-03-16 19:26 +0100 |
| Subject | Lambda dereference |
| Message-ID | <p8h2a1$4f0$1@dont-email.me> |
interface Tagged {
boolean tag();
}
Stream<Tagged> stream = ...;
You can do this (exhibit A):
Predicate<Tagged> p = Tagged::tag;
Collection<Tagged> untagged = stream.filter( p.negate()
).collect(Collectors.toSet());
But you can't do this (exhibit B):
Collection<Tagged> untagged = stream.filter( (Tagged::tag).negate()
).collect(Collectors.toSet());
Why not? It would be pretty neat. Or am I missing something?
--
DF.
[toc] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| Date | 2018-03-16 19:32 +0100 |
| Message-ID | <p8h2lu$773$1@dont-email.me> |
| In reply to | #38187 |
On 2018-03-16 19:26, Daniele Futtorovic wrote:
> interface Tagged {
> boolean tag();
> }
>
> Stream<Tagged> stream = ...;
>
> You can do this (exhibit A):
> Predicate<Tagged> p = Tagged::tag;
> Collection<Tagged> untagged = stream.filter( p.negate()
> ).collect(Collectors.toSet());
>
> But you can't do this (exhibit B):
> Collection<Tagged> untagged = stream.filter( (Tagged::tag).negate()
> ).collect(Collectors.toSet());
>
> Why not? It would be pretty neat. Or am I missing something?
>
Nevermind, I'm an idiot. I had tried to cast it, and didn't seem to
manage, but must have screwed up somewhere.
This works:
Collection<Tagged> untagged = stream.filter(
((Predicate<Tagged>)Tagged::tag).negate() ).collect(Collectors.toSet());
... and it makes sense why it wouldn't accept it without the casting.
--
DF.
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@logic.at> |
|---|---|
| Date | 2018-03-16 23:42 +0000 |
| Message-ID | <slrnpaolja.5mg.avl@logic.at> |
| In reply to | #38188 |
Daniele Futtorovic <da.futt.news@laposte.net> wrote:
> On 2018-03-16 19:26, Daniele Futtorovic wrote:
>> interface Tagged {
>> boolean tag();
>> }
>> Stream<Tagged> stream = ...;
>> You can do this (exhibit A):
>> Predicate<Tagged> p = Tagged::tag;
>> Collection<Tagged> untagged = stream.filter( p.negate()
>> ).collect(Collectors.toSet());
>> But you can't do this (exhibit B):
>> Collection<Tagged> untagged = stream.filter( (Tagged::tag).negate()
>> ).collect(Collectors.toSet());
>> Why not? It would be pretty neat. Or am I missing something?
>
> This works:
> Collection<Tagged> untagged = stream.filter(
> ((Predicate<Tagged>)Tagged::tag).negate() ).collect(Collectors.toSet());
>
> ... and it makes sense why it wouldn't accept it without the casting.
Huh, why? Well, Java does have some "expressions" that only work as
initializers (like braced list of elements for initializing arrays), but
that wouldn't look to me like such a case - afterall one could use
Tagged::tag right there if it weren't for the ".negate()".
What would be the expression type of blank (Tagged::tag)?
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| Date | 2018-03-17 03:10 +0100 |
| Message-ID | <p8htfh$p2r$1@dont-email.me> |
| In reply to | #38189 |
On 2018-03-17 00:42, Andreas Leitgeb wrote:
> Daniele Futtorovic <da.futt.news@laposte.net> wrote:
>> On 2018-03-16 19:26, Daniele Futtorovic wrote:
>>> interface Tagged {
>>> boolean tag();
>>> }
>>> Stream<Tagged> stream = ...;
>>> You can do this (exhibit A):
>>> Predicate<Tagged> p = Tagged::tag;
>>> Collection<Tagged> untagged = stream.filter( p.negate()
>>> ).collect(Collectors.toSet());
>>> But you can't do this (exhibit B):
>>> Collection<Tagged> untagged = stream.filter( (Tagged::tag).negate()
>>> ).collect(Collectors.toSet());
>>> Why not? It would be pretty neat. Or am I missing something?
>>
>> This works:
>> Collection<Tagged> untagged = stream.filter(
>> ((Predicate<Tagged>)Tagged::tag).negate() ).collect(Collectors.toSet());
>>
>> ... and it makes sense why it wouldn't accept it without the casting.
>
> Huh, why? Well, Java does have some "expressions" that only work as
> initializers (like braced list of elements for initializing arrays), but
> that wouldn't look to me like such a case - afterall one could use
> Tagged::tag right there if it weren't for the ".negate()".
>
> What would be the expression type of blank (Tagged::tag)?
>
No, absolutely -- the compiler can't determine that Tagged::tag is a
Predicate without the context, and hence (Tagged::tag).negate() will
fail (parentheses just for clarification). I'd figured as much, but then
when I tried to add a cast, I didn't seem to be going anywhere. So I
thought, hey, that's something I could post to cljp! And when it was
posted, I thought, just a second, I'd better double-check some more.
And, oh fortuna, thou art a bitch, the cast worked.
Foot.values().forEach( Mouth.get()::insert ); :-/
--
DF.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web