| 261 | |
| 262 | |
| 263 | def test_fold(): |
| 264 | c = b.fold(add) |
| 265 | assert c.compute() == sum(L) |
| 266 | assert c.key == b.fold(add).key |
| 267 | |
| 268 | c2 = b.fold(add, initial=10) |
| 269 | assert c2.key != c.key |
| 270 | assert c2.compute() == sum(L) + 10 * b.npartitions |
| 271 | assert c2.key == b.fold(add, initial=10).key |
| 272 | |
| 273 | c = db.from_sequence(range(5), npartitions=3) |
| 274 | |
| 275 | def binop(acc, x): |
| 276 | acc = acc.copy() |
| 277 | acc.add(x) |
| 278 | return acc |
| 279 | |
| 280 | d = c.fold(binop, set.union, initial=set()) |
| 281 | assert d.compute() == set(c) |
| 282 | assert d.key == c.fold(binop, set.union, initial=set()).key |
| 283 | |
| 284 | d = db.from_sequence("hello") |
| 285 | assert set(d.fold(lambda a, b: "".join([a, b]), initial="").compute()) == set( |
| 286 | "hello" |
| 287 | ) |
| 288 | |
| 289 | e = db.from_sequence([[1], [2], [3]], npartitions=2) |
| 290 | assert set(e.fold(add, initial=[]).compute(scheduler="sync")) == {1, 2, 3} |
| 291 | |
| 292 | |
| 293 | def test_fold_bag(): |