Difference between revisions of "Higher Order Functions Hall of Fame Assignment"
Jump to navigation
Jump to search
(→Test) |
|||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Code To Implement= | =Code To Implement= | ||
− | == | + | ==Higher-order Functions== |
− | + | {{JavaToImplement|Hof|filter<br/>foldLeft<br/>foldRight<br/>map<br/>find<br/>|hof.util.exercise}} | |
− | {{JavaToImplement| | ||
− | + | ===filter {{HofHof}}=== | |
+ | [https://smlfamily.github.io/Basis/list.html#SIG:LIST.filter:VAL SML List filter] | ||
− | + | <syntaxhighlight lang="java> | |
+ | public static <E> ImList<E> filter(Predicate<E> predicate, ImList<E> list) | ||
+ | </syntaxhighlight> | ||
− | == | + | ===foldLeft {{HofHof}}=== |
− | + | [https://smlfamily.github.io/Basis/list.html#SIG:LIST.foldl:VAL SML List foldl] | |
− | + | :<code>foldLeft(f, initial_value, [x1, x2, ..., xn])</code> | |
− | + | :returns <code>f(xn,...,f(x2, f(x1, initial_value))...)</code> or <code>initial_value</code> if the list is empty. | |
− | + | <syntaxhighlight lang="java> | |
+ | public static <E, R> R foldLeft(BiFunction<E, R, R> f, R acc, ImList<E> list) | ||
+ | </syntaxhighlight> | ||
+ | ===foldRight {{HofHof}}=== | ||
+ | [https://smlfamily.github.io/Basis/list.html#SIG:LIST.foldr:VAL SML List foldr] | ||
− | + | :<code>foldRight(f, initial_value, [x1, x2, ..., xn])</code> | |
− | + | :returns <code>f(x1, f(x2, ..., f(xn, init)...))</code> or <code>initial_value</code> if the list is empty. | |
− | = | + | <syntaxhighlight lang="java> |
− | === | + | public static <E, R> R foldRight(BiFunction<E, R, R> f, R acc, ImList<E> list) |
− | + | </syntaxhighlight> | |
+ | ===map {{HofHof}}=== | ||
+ | [https://smlfamily.github.io/Basis/list.html#SIG:LIST.map:VAL SML List map] | ||
− | + | <syntaxhighlight lang="java> | |
+ | public static <E, R> ImList<R> map(Function<E, R> f, ImList<E> list) | ||
+ | </syntaxhighlight> | ||
+ | ===find {{HofHof}}=== | ||
+ | [https://smlfamily.github.io/Basis/list.html#SIG:LIST.find:VAL SML List map] | ||
− | + | <syntaxhighlight lang="java> | |
+ | public static <E> Optional<E> find(Predicate<E> predicate, ImList<E> list) | ||
+ | </syntaxhighlight> | ||
− | == | + | ==Clients== |
− | {{JavaToImplement| | + | {{JavaToImplement|HofClients|toOnlyWordsWhichContainAllVowels<br>firstPalindrome<br/>sum<br>countBetweenMinAndMaxExclusive<br/>reverse<br>toLengths<br/>toStrictlyLessThan|hof.clients.exercise}} |
− | + | ===toOnlyWordsWhichContainAllVowels=== | |
+ | <syntaxhighlight lang="java> | ||
+ | public static ImList<String> toOnlyWordsWhichContainAllVowels(ImList<String> words) | ||
+ | </syntaxhighlight> | ||
− | + | ===toOnlyEvens=== | |
+ | <syntaxhighlight lang="java> | ||
+ | public static ImList<Integer> toOnlyEvens(ImList<Integer> xs) | ||
+ | </syntaxhighlight> | ||
− | == | + | ===toOnlyWordsWhichContainAllVowels=== |
− | < | + | <syntaxhighlight lang="java> |
− | + | public static ImList<String> toOnlyWordsWhichContainAllVowels(ImList<String> words) | |
+ | </syntaxhighlight> | ||
− | + | ===firstPalindrome=== | |
+ | <syntaxhighlight lang="java> | ||
+ | public static Optional<String> firstPalindrome(ImList<String> words) | ||
+ | </syntaxhighlight> | ||
− | + | ===sum=== | |
− | + | <syntaxhighlight lang="java> | |
− | + | public static int sum(ImList<Integer> xs) | |
− | + | </syntaxhighlight> | |
− | === | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
===countBetweenMinAndMaxExclusive=== | ===countBetweenMinAndMaxExclusive=== | ||
− | + | <syntaxhighlight lang="java> | |
− | + | public static int countBetweenMinAndMaxExclusive(int min, int maxExclusive, ImList<Integer> xs) | |
− | + | </syntaxhighlight> | |
− | |||
− | |||
− | + | ===reverse=== | |
− | + | <syntaxhighlight lang="java> | |
− | + | public static <E> ImList<E> reverse(ImList<E> xs) | |
+ | </syntaxhighlight> | ||
− | == | + | ===toLengths=== |
− | + | <syntaxhighlight lang="java> | |
+ | public static ImList<Integer> toLengths(ImList<String> texts) | ||
+ | </syntaxhighlight> | ||
− | === | + | ===toStrictlyLessThan=== |
− | + | <syntaxhighlight lang="java> | |
+ | public static ImList<Boolean> toStrictlyLessThan(ImList<Integer> xs, int threshold) | ||
+ | </syntaxhighlight> | ||
=Test= | =Test= | ||
{{TestSuite|HigherOrderFunctionTestSuite|hof}} | {{TestSuite|HigherOrderFunctionTestSuite|hof}} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 03:32, 12 October 2023
Code To Implement
Higher-order Functions
class: | Hof.java | |
methods: | filter foldLeft foldRight map find |
|
package: | hof.util.exercise | |
source folder: | src/main/java |
filter
public static <E> ImList<E> filter(Predicate<E> predicate, ImList<E> list)
foldLeft
foldLeft(f, initial_value, [x1, x2, ..., xn])
- returns
f(xn,...,f(x2, f(x1, initial_value))...)
orinitial_value
if the list is empty.
public static <E, R> R foldLeft(BiFunction<E, R, R> f, R acc, ImList<E> list)
foldRight
foldRight(f, initial_value, [x1, x2, ..., xn])
- returns
f(x1, f(x2, ..., f(xn, init)...))
orinitial_value
if the list is empty.
public static <E, R> R foldRight(BiFunction<E, R, R> f, R acc, ImList<E> list)
map
public static <E, R> ImList<R> map(Function<E, R> f, ImList<E> list)
find
public static <E> Optional<E> find(Predicate<E> predicate, ImList<E> list)
Clients
toOnlyWordsWhichContainAllVowels
public static ImList<String> toOnlyWordsWhichContainAllVowels(ImList<String> words)
toOnlyEvens
public static ImList<Integer> toOnlyEvens(ImList<Integer> xs)
toOnlyWordsWhichContainAllVowels
public static ImList<String> toOnlyWordsWhichContainAllVowels(ImList<String> words)
firstPalindrome
public static Optional<String> firstPalindrome(ImList<String> words)
sum
public static int sum(ImList<Integer> xs)
countBetweenMinAndMaxExclusive
public static int countBetweenMinAndMaxExclusive(int min, int maxExclusive, ImList<Integer> xs)
reverse
public static <E> ImList<E> reverse(ImList<E> xs)
toLengths
public static ImList<Integer> toLengths(ImList<String> texts)
toStrictlyLessThan
public static ImList<Boolean> toStrictlyLessThan(ImList<Integer> xs, int threshold)
Test
class: | HigherOrderFunctionTestSuite.java | |
package: | hof | |
source folder: | src/test/java |