-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package fpinscala.chapter5; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class Exercise_5_03 | ||
{ | ||
public static <S> Flow<S> takeWhile(Flow<S> flow, Predicate<S> p) | ||
{ | ||
if (flow.isEmpty()) | ||
return flow; | ||
if (!p.test(flow.head.get())) | ||
return Flow.empty(); | ||
return new Flow<>(flow.head, () -> takeWhile(flow.tail.get(), p)); | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
Flow<Integer> flow = Flow.of(1, 2, 3, 4); | ||
System.out.println(takeWhile(flow, i -> i < 3).toCons()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package fpinscala.chapter5; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class Exercise_5_04 | ||
{ | ||
public static <S> boolean exists(Flow<S> flow, Predicate<S> p) | ||
{ | ||
// The key point to understand is that the we need to "realize" the second argument of f(), | ||
// either directly (by calling get()) or indirectly (for example by returning it form f(), | ||
// since the foldRight() algorithm "realizes" the return value of f()), in order to proceed | ||
// with the iteration. If the second argument is not "realized", the iteration stops. | ||
// | ||
// In exists(), the predicate will fail until the element is found, therefore each time the | ||
// predicate fails, the iteration will continue. | ||
return flow.foldRight(false, (element, lazyResult) -> p.test(element) ? () -> true : lazyResult); | ||
|
||
// Another way of writing it, more similar to the Scala version. | ||
// Note how in this version the "realization" of the second argument is explicit. | ||
// return flow.foldRight(false, (element, lazyResult) -> () -> p.test(element) || lazyResult.get()); | ||
} | ||
|
||
public static <S> boolean forAll(Flow<S> flow, Predicate<S> p) | ||
{ | ||
// Like above, we need to "realize" the second argument to iterate. | ||
// When the predicate fails, iteration is stopped. | ||
return flow.foldRight(true, (element, lazyResult) -> p.test(element) ? lazyResult : () -> false); | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
Flow<Integer> flow = Flow.of(1, 2, 3, 4); | ||
System.out.println(exists(flow, i -> i == 3)); | ||
System.out.println(forAll(flow, i -> i < 2)); | ||
System.out.println(forAll(flow, i -> i < 5)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters