Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Problem with statelessness

 
Greenhorn
Posts: 26
1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble understanding statelessness and associativity of accumulators in the reduce() method for Streams. I'd appreciate some insight into it or even a link to some article which explains it properly. Thanks!
 
Marshal
Posts: 26377
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked at the API documentation for the Stream.reduce() method; it mentioned that the operation must be associative. And the word "associative" was a link to an explanation of why that is necessary and what the ramifications are. Did you read that? If not, do you still have questions after reading it?

Likewise with "stateless"; the word in the documentation is also a link to a section which explains further.
 
Vimugdh Lall
Greenhorn
Posts: 26
1
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for pointing those out. I just went through them. Got the associativity part nailed down. I've understood the statelessness too I guess. I'll go through some problems relating to those to solidify my understanding!
Cheers!  
 
Paul Clapham
Marshal
Posts: 26377
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!
 
Saloon Keeper
Posts: 12715
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example that violates statelessness:

Each time StringBuilder.append() is called, it modifies the state of the StringBuilder instance. This instance is also used when other elements of the stream are iterated over, potentially leading to issues. To be stateless, the operations you pass to the reduce() method must not call any mutators, and should always return the reduced result as a new immutable value.

The example above is associative though: (("a" + "b") + "c") yields the same result as ("a" + ("b" + "c")).

Here's an example that violates associativity:

It is stateless, but it's not associative because ((100 - 20) - 15) yields a different result than (100 - (20 - 15)).
 
Goodbye moon men. Hello tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic