This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

in need of a real functional way  RSS feed

 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I have a List<Integer> initialState, and a List<Move> moves.
'Move' is an interface, having the method List<Integer> change(List<Integer> list),
with several implementations.

What I want to do is to go from the initialState to the endState, by applying
each move in the 'moves' list in succession to the initialState.

Now, a reduce seems ideal for this, but I have the requirement that I also
must store each in-between value.

First thing I tried was:



That worked, but I cannot call this very functional. So I tried this reduce:



That also works, but I find it still ugly. So my question is:

is there a true functional way to achieve what I need, which is also elegant?

Thanks!

Greetz,
Piet
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, never mind, I changed the program slightly so it is no longer relevant.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!