Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit 9be01c1

Browse files
committed
Add new exercise CrazyLambdas.java
1 parent bb3ba20 commit 9be01c1

File tree

7 files changed

+273
-2
lines changed

7 files changed

+273
-2
lines changed

account-analytics/README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You're supposed to be familiar with Java 8
99

1010
### How to start :question:
1111
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
12-
* If you don't have enough knowladge about this domain, check out the [links below](#related-materials-information_source)
12+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
1313
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
1414

1515
### Related materials :information_source:

crazy-lambdas/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Crazy lambda exercise :muscle:
2+
Improve your lambda skills
3+
### Task
4+
`CrazyLambdas` class consists of static methods that return various functions, operations and predicates.
5+
Your job is to implement the *todo* section of that class using **Lambda expressions** and **method reference**.
6+
To verify your implementation, run `CrazyLambdasTest.java`
7+
8+
### Pre-conditions :heavy_exclamation_mark:
9+
You're supposed to be familiar with Java 8
10+
11+
### How to start :question:
12+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
13+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
14+
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
15+
16+
### Related materials :information_source:
17+
* [Lambda tutorial](https://github.com/bobocode-projects/java-8-tutorial/tree/master/lambdas) <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>
18+
* [State of lambda (JSR 335)](http://htmlpreview.github.io/?https://github.com/bobocode-projects/resources/blob/master/java8/lambda/sotl.html)
19+

crazy-lambdas/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-functional-features-exercises</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>crazy-lambdas</artifactId>
13+
14+
15+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.bobocode;
2+
3+
import java.math.BigDecimal;
4+
import java.util.function.*;
5+
6+
public class CrazyLambdas {
7+
8+
/**
9+
* Returns {@link Supplier} that always supply "Hello"
10+
*
11+
* @return a string supplier
12+
*/
13+
public static Supplier<String> helloSupplier() {
14+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
15+
}
16+
17+
/**
18+
* Returns a {@link Predicate} of string that checks if string is empty
19+
*
20+
* @return a string predicate
21+
*/
22+
public static Predicate<String> isEmptyPredicate() {
23+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
24+
}
25+
26+
/**
27+
* Returns a {@link Function} that converts a {@link BigDecimal} number into a {@link String} that start with
28+
* a dollar sign and then gets a value
29+
*
30+
* @return function that converts adds dollar sign
31+
*/
32+
public static Function<BigDecimal, String> toDollarStringFunction() {
33+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
34+
}
35+
36+
/**
37+
* Receives two parameter that represent a range and returns a {@link Predicate<String>} that verifies if string
38+
* length is in the specified range. E.g. min <= length < max
39+
*
40+
* @param min min length
41+
* @param max max length
42+
* @return a string predicate
43+
*/
44+
public static Predicate<String> lengthInRangePredicate(int min, int max) {
45+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
46+
}
47+
48+
/**
49+
* Returns a {@link Supplier} of random integers
50+
*
51+
* @return int supplier
52+
*/
53+
public static IntSupplier randomIntSupplier() {
54+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
55+
}
56+
57+
58+
/**
59+
* Returns an {@link IntUnaryOperator} that receives an int as a bound parameter, and returns a random int
60+
*
61+
* @return int operation
62+
*/
63+
public static IntUnaryOperator boundedRandomIntSupplier() {
64+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
65+
}
66+
67+
/**
68+
* Returns {@link IntUnaryOperator} that calculates an integer square
69+
*
70+
* @return square operation
71+
*/
72+
public static IntUnaryOperator intSquareOperation() {
73+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
74+
}
75+
76+
/**
77+
* Returns a {@link LongBinaryOperator} sum operation.
78+
*
79+
* @return binary sum operation
80+
*/
81+
public static LongBinaryOperator longSumOperation() {
82+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
83+
}
84+
85+
/**
86+
* Returns a {@link ToIntFunction<String>} that converts string to integer.
87+
*
88+
* @return string to int converter
89+
*/
90+
public static ToIntFunction<String> stringToIntConverter() {
91+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
92+
}
93+
94+
/**
95+
* Receives int parameter n, and returns a {@link Supplier} that supplies {@link IntUnaryOperator}
96+
* that is a function f(x) = n * x
97+
*
98+
* @param n a multiplier
99+
* @return a function supplier
100+
*/
101+
public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
102+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
103+
}
104+
105+
106+
}
107+
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.bobocode;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.JUnit4;
6+
7+
import java.math.BigDecimal;
8+
import java.util.function.*;
9+
10+
import static org.junit.Assert.*;
11+
12+
@RunWith(JUnit4.class)
13+
public class CrazyLambdasTest {
14+
15+
@Test
16+
public void testHelloSupplier() {
17+
Supplier<String> helloSupplier = CrazyLambdas.helloSupplier();
18+
19+
assertEquals("Hello", helloSupplier.get());
20+
}
21+
22+
23+
@Test
24+
public void testIsEmptyPredicate() {
25+
Predicate<String> isEmptyPredicate = CrazyLambdas.isEmptyPredicate();
26+
27+
boolean nonEmptyStringResult = isEmptyPredicate.test("fasdfa");
28+
boolean emptyStringResult = isEmptyPredicate.test("");
29+
30+
assertFalse(nonEmptyStringResult);
31+
assertTrue(emptyStringResult);
32+
}
33+
34+
@Test
35+
public void testToDollarStringFunction() {
36+
Function<BigDecimal, String> toDollarStringFunction = CrazyLambdas.toDollarStringFunction();
37+
String tenDollarStr = toDollarStringFunction.apply(BigDecimal.TEN.setScale(2));
38+
39+
assertEquals("$10.00", tenDollarStr);
40+
}
41+
42+
@Test
43+
public void testLengthInRangePredicate() {
44+
Predicate<String> lengthInRangePredicate = CrazyLambdas.lengthInRangePredicate(4, 10);
45+
46+
boolean twoLetterStringResult = lengthInRangePredicate.test("Hi");
47+
boolean fourLetterStringResult = lengthInRangePredicate.test("Hola");
48+
boolean fiveLetterStringResult = lengthInRangePredicate.test("Amigo");
49+
boolean eightLetterStringResult = lengthInRangePredicate.test("Lalaland");
50+
boolean thirteenLetterStringResult = lengthInRangePredicate.test("Lambda rocks!");
51+
52+
assertFalse(twoLetterStringResult);
53+
assertTrue(fourLetterStringResult);
54+
assertTrue(fiveLetterStringResult);
55+
assertTrue(eightLetterStringResult);
56+
assertFalse(thirteenLetterStringResult);
57+
}
58+
59+
@Test
60+
public void testRandomIntSupplier() {
61+
IntSupplier randomIntSupplier = CrazyLambdas.randomIntSupplier();
62+
63+
int firstValue = randomIntSupplier.getAsInt();
64+
int secondValue = randomIntSupplier.getAsInt();
65+
66+
assertNotEquals(firstValue, secondValue);
67+
}
68+
69+
@Test
70+
public void testBoundedRandomIntSupplier() {
71+
IntUnaryOperator boundedRandomIntSupplier = CrazyLambdas.boundedRandomIntSupplier();
72+
73+
int randomIntLessThan10 = boundedRandomIntSupplier.applyAsInt(10);
74+
int randomIntLessThan100 = boundedRandomIntSupplier.applyAsInt(100);
75+
int randomIntLessThan1000 = boundedRandomIntSupplier.applyAsInt(1000);
76+
int randomIntLessThan10000 = boundedRandomIntSupplier.applyAsInt(1000);
77+
78+
assertTrue(randomIntLessThan10 < 10);
79+
assertTrue(randomIntLessThan100 < 100);
80+
assertTrue(randomIntLessThan1000 < 1000);
81+
assertTrue(randomIntLessThan10000 < 10000);
82+
}
83+
84+
@Test
85+
public void testIntSquareOperation() {
86+
IntUnaryOperator squareOperation = CrazyLambdas.intSquareOperation();
87+
88+
int squareOfFour = squareOperation.applyAsInt(4);
89+
int squareOfZero = squareOperation.applyAsInt(0);
90+
91+
assertEquals(16, squareOfFour);
92+
assertEquals(0, squareOfZero);
93+
}
94+
95+
@Test
96+
public void testLongSumOperation() {
97+
LongBinaryOperator sumOperation = CrazyLambdas.longSumOperation();
98+
99+
100+
long sumOfSevenAndEight = sumOperation.applyAsLong(7, 8);
101+
long sumOfTenAndZero = sumOperation.applyAsLong(10, 0);
102+
long sumOfFiveAndMinusTen = sumOperation.applyAsLong(5, -10);
103+
104+
assertEquals(15, sumOfSevenAndEight);
105+
assertEquals(10, sumOfTenAndZero);
106+
assertEquals(-5, sumOfFiveAndMinusTen);
107+
}
108+
109+
@Test
110+
public void testStringToIntConverter() {
111+
ToIntFunction<String> stringToIntConverter = CrazyLambdas.stringToIntConverter();
112+
113+
int num = stringToIntConverter.applyAsInt("234");
114+
int negativeNum = stringToIntConverter.applyAsInt("-122");
115+
116+
assertEquals(234, num);
117+
assertEquals(-122, negativeNum);
118+
}
119+
120+
@Test
121+
public void testNMultiplyFunctionSupplier() {
122+
Supplier<IntUnaryOperator> fiveMultiplyFunctionSupplier = CrazyLambdas.nMultiplyFunctionSupplier(5);
123+
124+
IntUnaryOperator multiplyByFiveOperation = fiveMultiplyFunctionSupplier.get();
125+
int result = multiplyByFiveOperation.applyAsInt(11); // 11 * 5 = 55
126+
127+
assertEquals(55, result);
128+
}
129+
}

math-functions/README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ You're supposed to be familiar with Java 8
88

99
### How to start :question:
1010
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
11-
* If you don't have enough knowladge about this domain, check out the [links below](#related-materials-information_source)
11+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
1212
* Don't worry if you got stuck, checkout the [exercise/completed](https://github.com/bobocode-projects/tdd-exercises/tree/exercise/completed/binary-search-tree) branch and see the final implementation
1313

1414
### Related materials :information_source:

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<module>account-analytics</module>
1414
<module>account-data</module>
1515
<module>sum-of-squares</module>
16+
<module>crazy-lambdas</module>
1617
</modules>
1718

1819
<properties>

0 commit comments

Comments
 (0)