Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31689 > unrolled thread

Puzzler: Fun with Streams.

Started byDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
First post2015-05-12 19:01 -0700
Last post2015-06-10 03:19 -0700
Articles 6 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Puzzler: Fun with Streams. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2015-05-12 19:01 -0700
    Re: Puzzler: Fun with Streams. Jeff Higgins <jeff@invalid.invalid> - 2015-05-13 08:21 -0400
      Re: Puzzler: Fun with Streams. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2015-05-13 12:49 -0700
    Re: Puzzler: Fun with Streams. scattered <rescattered@gmail.com> - 2015-05-18 05:49 -0700
      Re: Puzzler: Fun with Streams. scattered <rescattered@gmail.com> - 2015-05-18 06:29 -0700
    Re: Puzzler: Fun with Streams. javadiscover@gmail.com - 2015-06-10 03:19 -0700

#31689 — Puzzler: Fun with Streams.

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2015-05-12 19:01 -0700
SubjectPuzzler: Fun with Streams.
Message-ID<Nfy4x.122164$566.49072@fx20.iad>
It occurred to me that I could write this little snippet someplace, and 
it would work.

     return stream.filter(o->o<-o);

stream is an object which implements java.util.stream.BaseStream, though 
I won't tell you which one

Might be obvious to everyone, but I thought it was a good little puzzler.

What input do you think this will work with, and what is the result?

[toc] | [next] | [standalone]


#31693

FromJeff Higgins <jeff@invalid.invalid>
Date2015-05-13 08:21 -0400
Message-ID<mivfm2$hit$1@dont-email.me>
In reply to#31689
On 05/12/2015 10:01 PM, Daniel Pitts wrote:
> It occurred to me that I could write this little snippet someplace, and
> it would work.
>
>      return stream.filter(o->o<-o);
>
> stream is an object which implements java.util.stream.BaseStream, though
> I won't tell you which one
>
> Might be obvious to everyone, but I thought it was a good little puzzler.
>
> What input do you think this will work with, and what is the result?

package example;

import java.util.Arrays;
import java.util.stream.IntStream;

public class FilterTest {

   public static void main(String[] args) {
     int[] test = new int[]{-2,-1,0,1,2};
     filter(test).forEach(p -> System.out.println(p));

   }
   static IntStream filter(int[] t) {
     IntStream stream = Arrays.stream(t);
     return stream.filter(o->o<-o);
   }

}

[toc] | [prev] | [next] | [standalone]


#31701

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2015-05-13 12:49 -0700
Message-ID<IUN4x.35814$yw3.5574@fx29.iad>
In reply to#31693
On 5/13/15 5:21 AM, Jeff Higgins wrote:
> On 05/12/2015 10:01 PM, Daniel Pitts wrote:
>> It occurred to me that I could write this little snippet someplace, and
>> it would work.
>>
>>      return stream.filter(o->o<-o);
>>
>> stream is an object which implements java.util.stream.BaseStream, though
>> I won't tell you which one
>>
>> Might be obvious to everyone, but I thought it was a good little puzzler.
>>
>> What input do you think this will work with, and what is the result?
>
> package example;
>
> import java.util.Arrays;
> import java.util.stream.IntStream;
>
> public class FilterTest {
>
>    public static void main(String[] args) {
>      int[] test = new int[]{-2,-1,0,1,2};
>      filter(test).forEach(p -> System.out.println(p));
>
>    }
>    static IntStream filter(int[] t) {
>      IntStream stream = Arrays.stream(t);
>      return stream.filter(o->o<-o);
>    }
>
> }

Yep. though you should use forEachOrdered, and you can use 
System.out::println

    filter(test).forEachOrdered(System.out::println);

Though I prefer to use collect, to keep parallelism support:

    System.out.println(filter(test).collect(Collectors.joining("\n")));

[toc] | [prev] | [next] | [standalone]


#31719

Fromscattered <rescattered@gmail.com>
Date2015-05-18 05:49 -0700
Message-ID<b4eb4b14-d6bd-41f9-bd6b-5a6af5c7c0a9@googlegroups.com>
In reply to#31689
On Tuesday, May 12, 2015 at 10:01:28 PM UTC-4, Daniel Pitts wrote:
> It occurred to me that I could write this little snippet someplace, and 
> it would work.
> 
>      return stream.filter(o->o<-o);
> 
> stream is an object which implements java.util.stream.BaseStream, though 
> I won't tell you which one
> 
> Might be obvious to everyone, but I thought it was a good little puzzler.
> 
> What input do you think this will work with, and what is the result?

Interesting. At first I thought that you had a letter o and the number 0 (which would also work for something like o->0<-o) but then I noticed that they were all letters, which of course also works.

variations: find contexts for the lambda expressions:
1) o->oo<-o

2) o->0_0<-o

3) x->0x0<-x

(2 and 3 are essentially identical)

A full-fledged functional language like ML would allow for things like

x->xx->xx<-x (lambda expressions returning lambda expressions, with right-associativity of ->)

but I can't seem to make that work in Java8

[toc] | [prev] | [next] | [standalone]


#31720

Fromscattered <rescattered@gmail.com>
Date2015-05-18 06:29 -0700
Message-ID<3d5ec0f1-c5ba-4b35-9bba-0e679f811bdf@googlegroups.com>
In reply to#31719
On Monday, May 18, 2015 at 8:49:19 AM UTC-4, scattered wrote:
> On Tuesday, May 12, 2015 at 10:01:28 PM UTC-4, Daniel Pitts wrote:
> > It occurred to me that I could write this little snippet someplace, and 
> > it would work.
> > 
> >      return stream.filter(o->o<-o);
> > 
> > stream is an object which implements java.util.stream.BaseStream, though 
> > I won't tell you which one
> > 
> > Might be obvious to everyone, but I thought it was a good little puzzler.
> > 
> > What input do you think this will work with, and what is the result?
> 
> Interesting. At first I thought that you had a letter o and the number 0 (which would also work for something like o->0<-o) but then I noticed that they were all letters, which of course also works.
> 
> variations: find contexts for the lambda expressions:
> 1) o->oo<-o
> 
> 2) o->0_0<-o
> 
> 3) x->0x0<-x
> 
> (2 and 3 are essentially identical)
> 
> A full-fledged functional language like ML would allow for things like
> 
> x->xx->xx<-x (lambda expressions returning lambda expressions, with right-associativity of ->)
> 
> but I can't seem to make that work in Java8

still another:

o->--o-o--<-o

describe what survives when an int stream is filtered by the above

o->o--+--o<-o is also fun

doubtless you can get even weirder expressions when you bring shift operators into play.

Puzzle: call these things executable palindromes. What is the most obscure executable palindrome? (ill defined puzzle since there is no natural limit to obscurity, as anybody who has seen entries to the obfuscated C contest knows)

[toc] | [prev] | [next] | [standalone]


#31786

Fromjavadiscover@gmail.com
Date2015-06-10 03:19 -0700
Message-ID<69de3a5f-1a3d-4e07-9ca3-176bd94c187b@googlegroups.com>
In reply to#31689
On Wednesday, May 13, 2015 at 7:31:28 AM UTC+5:30, Daniel Pitts wrote:
> It occurred to me that I could write this little snippet someplace, and 
> it would work.
> 
>      return stream.filter(o->o<-o);
> 
> stream is an object which implements java.util.stream.BaseStream, though 
> I won't tell you which one
> 
> Might be obvious to everyone, but I thought it was a good little puzzler.
> 
> What input do you think this will work with, and what is the result?

--------------------------


visit for more java interview questions and programs - http://javadiscover.blogspot.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web