Skip to content

Commit e915694

Browse files
committed
Merge branch 'master' into lenses
2 parents 2d746f9 + 80a0fdb commit e915694

6 files changed

Lines changed: 97 additions & 27 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ Add the following dependency to your:
4646
<dependency>
4747
<groupId>com.jnape.palatable</groupId>
4848
<artifactId>lambda</artifactId>
49-
<version>1.2</version>
49+
<version>1.3</version>
5050
</dependency>
5151
```
5252

5353
`build.gradle` ([Gradle](https://docs.gradle.org/current/userguide/dependency_management.html)):
5454

5555
```gradle
56-
compile group: 'com.jnape.palatable', name: 'lambda', version: '1.2'
56+
compile group: 'com.jnape.palatable', name: 'lambda', version: '1.3'
5757
```
5858

5959

@@ -114,7 +114,7 @@ Let's compose two functions:
114114

115115
Fn1<Integer, Integer> noOp = add.then(subtract);
116116
// same as
117-
Fn1<Integer, Integer> alsoNoOp = subtract.fmap(add);
117+
Fn1<Integer, Integer> alsoNoOp = subtract.compose(subtract);
118118
```
119119

120120
And partially apply some:

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>lambda</artifactId>
13-
<version>1.4-SNAPSHOT</version>
13+
<version>1.5-SNAPSHOT</version>
1414
<packaging>jar</packaging>
1515

1616
<name>Lambda</name>

‎src/main/java/com/jnape/palatable/lambda/adt/Either.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.function.Supplier;
1313

1414
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
15+
import static com.jnape.palatable.lambda.functions.builtin.fn3.FoldLeft.foldLeft;
16+
import static java.util.Arrays.asList;
1517

1618
/**
1719
* The binary tagged union. General semantics tend to connote "success" values via the right value and "failure" values
@@ -120,22 +122,24 @@ public final <L2, R2> Either<L2, R2> flatMap(Function<? super L, ? extends Eithe
120122
}
121123

122124
/**
123-
* Given two binary operators over L and R, merge two <code>Either&lt;L, R&gt;</code>s into a single
125+
* Given two binary operators over L and R, merge multiple <code>Either&lt;L, R&gt;</code>s into a single
124126
* <code>Either&lt;L, R&gt;</code>. Note that <code>merge</code> biases towards left values; that is, if any left
125127
* value exists, the result will be a left value, such that only unanimous right values result in an ultimate right
126128
* value.
127129
*
128130
* @param leftFn the binary operator for L
129131
* @param rightFn the binary operator for R
130-
* @param other the other Either
132+
* @param others the other Eithers to merge into this one
131133
* @return the merged Either
132134
*/
135+
@SafeVarargs
133136
public final Either<L, R> merge(BiFunction<? super L, ? super L, ? extends L> leftFn,
134137
BiFunction<? super R, ? super R, ? extends R> rightFn,
135-
Either<L, R> other) {
136-
return this.match(
137-
l1 -> other.match(l2 -> left(leftFn.apply(l1, l2)), r -> left(l1)),
138-
r1 -> other.match(Either::left, r2 -> right(rightFn.apply(r1, r2))));
138+
Either<L, R>... others) {
139+
return foldLeft((x, y) -> x.match(l1 -> y.<Either<L, R>>match(l2 -> left(leftFn.apply(l1, l2)), r -> left(l1)),
140+
r1 -> y.<Either<L, R>>match(Either::left, r2 -> right(rightFn.apply(r1, r2)))),
141+
this,
142+
asList(others));
139143
}
140144

141145
/**

‎src/main/java/com/jnape/palatable/lambda/adt/hlist/HList.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ public static <_1, _2, _3, _4, _5> Tuple5<_1, _2, _3, _4, _5> tuple(_1 _1, _2 _2
134134
return tuple(_2, _3, _4, _5).cons(_1);
135135
}
136136

137+
/**
138+
* The consing of a head element to a tail <code>HList</code>.
139+
*
140+
* @param <Head> the head element type
141+
* @param <Tail> the HList tail type
142+
*/
137143
public static class HCons<Head, Tail extends HList<?, ?>> extends HList<Head, Tail> {
138144
private final Head head;
139145
private final Tail tail;
@@ -143,10 +149,20 @@ public static class HCons<Head, Tail extends HList<?, ?>> extends HList<Head, Ta
143149
this.tail = tail;
144150
}
145151

152+
/**
153+
* The head element of the <code>HList</code>.
154+
*
155+
* @return the head element
156+
*/
146157
public Head head() {
147158
return head;
148159
}
149160

161+
/**
162+
* The remaining tail of the <code>HList</code>; returns an HNil if this is the last element.
163+
*
164+
* @return the tail
165+
*/
150166
public Tail tail() {
151167
return tail;
152168
}
@@ -180,6 +196,9 @@ public final String toString() {
180196
}
181197
}
182198

199+
/**
200+
* The empty <code>HList</code>.
201+
*/
183202
public static final class HNil extends HList<Void, HNil> {
184203
private static final HNil INSTANCE = new HNil();
185204

‎src/main/java/com/jnape/palatable/lambda/adt/hmap/HMap.java‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class HMap implements Iterable<Tuple2<TypeSafeKey, Object>> {
2727

2828
private final Map<TypeSafeKey, Object> table;
2929

30-
HMap(Map<TypeSafeKey, Object> table) {
30+
private HMap(Map<TypeSafeKey, Object> table) {
3131
this.table = table;
3232
}
3333

@@ -70,7 +70,7 @@ public <V> HMap put(TypeSafeKey<V> key, V value) {
7070
/**
7171
* Store all the key/value mappings in <code>hMap</code> in this HMap.
7272
*
73-
* @param hMap the other hMap
73+
* @param hMap the other HMap
7474
* @return the updated HMap
7575
*/
7676
public HMap putAll(HMap hMap) {
@@ -87,6 +87,26 @@ public boolean containsKey(TypeSafeKey key) {
8787
return table.containsKey(key);
8888
}
8989

90+
/**
91+
* Remove a mapping from this HMap.
92+
*
93+
* @param key the key
94+
* @return the updated HMap
95+
*/
96+
public HMap remove(TypeSafeKey key) {
97+
return alter(t -> t.remove(key));
98+
}
99+
100+
/**
101+
* Remove all the key/value mappings in <code>hMap</code> from this HMap.
102+
*
103+
* @param hMap the other HMap
104+
* @return the updated HMap
105+
*/
106+
public HMap removeAll(HMap hMap) {
107+
return alter(t -> t.keySet().removeAll(hMap.table.keySet()));
108+
}
109+
90110
/**
91111
* Retrieve all the mapped keys.
92112
*

‎src/test/java/com/jnape/palatable/lambda/adt/hmap/HMapTest.java‎

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import static com.jnape.palatable.lambda.adt.hmap.HMap.hMap;
1111
import static com.jnape.palatable.lambda.adt.hmap.HMap.singletonHMap;
1212
import static com.jnape.palatable.lambda.adt.hmap.TypeSafeKey.typeSafeKey;
13-
import static java.util.Collections.emptyMap;
14-
import static java.util.Collections.singletonMap;
1513
import static org.junit.Assert.assertEquals;
1614
import static org.junit.Assert.assertFalse;
1715
import static org.junit.Assert.assertNotEquals;
@@ -26,29 +24,30 @@ public class HMapTest {
2624
public void getForPresentKey() {
2725
TypeSafeKey<String> stringKey = typeSafeKey();
2826
assertEquals(Optional.of("string value"),
29-
new HMap(singletonMap(stringKey, "string value")).get(stringKey));
27+
singletonHMap(stringKey, "string value").get(stringKey));
3028
}
3129

3230
@Test
3331
public void getForAbsentKey() {
3432
assertEquals(Optional.empty(),
35-
new HMap(singletonMap(typeSafeKey(), "string value")).<String>get(typeSafeKey()));
33+
singletonHMap(typeSafeKey(), "string value")
34+
.get(typeSafeKey()));
3635
}
3736

3837
@Test
3938
public void getForPresentKeyWithNullValue() {
4039
TypeSafeKey<String> stringKey = typeSafeKey();
4140
assertEquals(Optional.empty(),
42-
new HMap(singletonMap(stringKey, null)).get(stringKey));
41+
singletonHMap(stringKey, null).get(stringKey));
4342
}
4443

4544
@Test
4645
public void put() {
4746
TypeSafeKey<String> stringKey = typeSafeKey();
48-
assertEquals(new HMap(singletonMap(stringKey, "string value")),
47+
assertEquals(singletonHMap(stringKey, "string value"),
4948
emptyHMap().put(stringKey, "string value"));
5049

51-
assertEquals(new HMap(singletonMap(stringKey, "new value")),
50+
assertEquals(singletonHMap(stringKey, "new value"),
5251
emptyHMap()
5352
.put(stringKey, "string value")
5453
.put(stringKey, "new value"));
@@ -75,6 +74,35 @@ public void putAll() {
7574
right.putAll(left));
7675
}
7776

77+
@Test
78+
public void remove() {
79+
TypeSafeKey<String> stringKey1 = typeSafeKey();
80+
TypeSafeKey<String> stringKey2 = typeSafeKey();
81+
assertEquals(emptyHMap(),
82+
emptyHMap()
83+
.put(stringKey1, "string value")
84+
.remove(stringKey1));
85+
86+
assertEquals(singletonHMap(stringKey2, "another string value"),
87+
emptyHMap()
88+
.put(stringKey1, "string value")
89+
.put(stringKey2, "another string value")
90+
.remove(stringKey1));
91+
}
92+
93+
@Test
94+
public void removeAll() {
95+
TypeSafeKey<String> stringKey1 = typeSafeKey();
96+
TypeSafeKey<String> stringKey2 = typeSafeKey();
97+
98+
HMap hMap1 = hMap(stringKey1, "foo",
99+
stringKey2, "bar");
100+
HMap hMap2 = singletonHMap(stringKey1, "foo");
101+
102+
assertEquals(singletonHMap(stringKey2, "bar"),
103+
hMap1.removeAll(hMap2));
104+
}
105+
78106
@Test
79107
public void containsKey() {
80108
TypeSafeKey<String> stringKey1 = typeSafeKey();
@@ -127,8 +155,8 @@ public void convenienceStaticFactoryMethods() {
127155
TypeSafeKey<String> stringKey = typeSafeKey();
128156
TypeSafeKey<Integer> intKey = typeSafeKey();
129157
TypeSafeKey<Float> floatKey = typeSafeKey();
130-
assertEquals(new HMap(emptyMap()), HMap.emptyHMap());
131-
assertEquals(new HMap(singletonMap(stringKey, "string value")), HMap.singletonHMap(stringKey, "string value"));
158+
assertEquals(emptyHMap().put(stringKey, "string value"),
159+
singletonHMap(stringKey, "string value"));
132160
assertEquals(emptyHMap().put(stringKey, "string value").put(intKey, 1),
133161
hMap(stringKey, "string value",
134162
intKey, 1));
@@ -157,13 +185,12 @@ public void equality() {
157185
public void hashCodeUsesDecentDistribution() {
158186
assertEquals(emptyHMap().hashCode(), emptyHMap().hashCode());
159187
TypeSafeKey<String> stringKey = typeSafeKey();
160-
assertEquals(new HMap(singletonMap(stringKey, "string value")).hashCode(),
161-
new HMap(singletonMap(stringKey, "string value")).hashCode());
162-
163-
assertNotEquals(emptyHMap(), new HMap(singletonMap(stringKey, "string value")));
164-
assertNotEquals(new HMap(singletonMap(stringKey, "string value")),
165-
new HMap(singletonMap(stringKey, "another string value")));
188+
assertEquals(singletonHMap(stringKey, "string value").hashCode(),
189+
singletonHMap(stringKey, "string value").hashCode());
166190

191+
assertNotEquals(emptyHMap(), singletonHMap(stringKey, "string value"));
192+
assertNotEquals(singletonHMap(stringKey, "string value"),
193+
singletonHMap(stringKey, "another string value"));
167194
}
168195

169196
@Test

0 commit comments

Comments
 (0)