Functional Programming in Java: How functional techniques improve your Java programs by Pierre-Yves Saumont
Author:Pierre-Yves Saumont
Language: eng
Format: mobi, epub
Publisher: Manning Publications
Published: 2017-02-02T15:09:21.809000+00:00
Is it possible to do it with a fold? Right or left?
Why is the explicit recursive version better?
Can you see a way to solve the problem?
Solution 8.12
The explicitly recursive solution is easy:
public Result<A> getAt(int index) {
return index < 0 || index >= length()
? Result.failure("Index out of bound")
: getAt_(this, index).eval();
}
private static <A> TailCall<Result<A>> getAt_(List<A> list, int index) {
return index == 0
? TailCall.ret(Result.success(list.head()))
: TailCall.sus(() -> getAt_(list.tail(), index - 1));
}
First, you can check the index to see if it’s positive and less than the list length. If it isn’t, just return a Failure. Otherwise, call the helper method to process the list recursively. This method checks whether the index is 0. If it is, it returns the head of the list. Otherwise, it calls itself recursively on the tail of the list with a decremented index.
This looks like the best possible recursive solution. Is it possible to use a fold? Yes, it is, and it should be a left fold. But the solution is tricky:
public Result<A> getAt(int index) {
Tuple<Result<A>, Integer> identity =
new Tuple<>(Result.failure("Index out of bound"), index);
Download
Functional Programming in Java: How functional techniques improve your Java programs by Pierre-Yves Saumont.epub
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7755)
Grails in Action by Glen Smith Peter Ledbrook(7673)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6270)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6231)
Running Windows Containers on AWS by Marcio Morales(5757)
Kotlin in Action by Dmitry Jemerov(5025)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(4754)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4402)
Combating Crime on the Dark Web by Nearchos Nearchou(4343)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4021)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4005)
The Age of Surveillance Capitalism by Shoshana Zuboff(3920)
Python for Security and Networking - Third Edition by José Manuel Ortega(3565)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3499)
Mastering Python for Networking and Security by José Manuel Ortega(3327)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3317)
Blockchain Basics by Daniel Drescher(3280)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3241)
TCP IP by Todd Lammle(2965)
