Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

JDD2014: Real life lambdas - Peter Lawrey

293 views

Published on

After working with lambdas for the last 5 months, from a code base originally written in C#, a number of real life patterns and anti-patterns have become apparent.
What are some of the common patterns we used, and what are some patterns which needed to be avoided?

Published in: Software
  • Be the first to comment

  • Be the first to like this

JDD2014: Real life lambdas - Peter Lawrey

  1. 1. Introduction For the last 6 months, Higher Frequency Trading has been porting a legacy C# application with over 25K lines of code to Java. We have translated many LINQ statements into Java 8 Stream + Lambda. What are some common patterns and anti-patterns we have seen?
  2. 2. Contains 2.0 if (list.stream().anyMatch(p -> p.getType() == Type.Cash)) {
  3. 3. Deep Copy List<Position> newPositions = classPos.stream() .map(Position::clone) .collect(toList())
  4. 4. Validate all entries positions.forEach(Position::validate); Validate throws an InvalidStateException if invalid
  5. 5. Summing BigDecimal BigDecimal sum = getResults().stream() .reduce(BigDecimal.ZERO, (bd, t) -> bd.add(t.getRequirement()), BigDecimal::add);
  6. 6. Sorting by multiple fields setTrades(getTrades().stream() .sorted(comparing(t -> t.getInfo().getDate()) .thenComparing(Position::getCUSIP) .thenComparing(Position::getQuantity).reversed()) .collect(Collectors.toList()));
  7. 7. Group By Map<String, List<Position>> positionBySymbol = positions.values().stream() .filter(p -> p.getQuantity() != 0) .collect(groupingBy(Position::getSymbol));
  8. 8. Streaming Maps pos.entrySet().stream() .filter(p -> p.getValue().getQuantity() != 0.0) .forEach(p -> pos2.put(p.getKey(), p.getValue()));
  9. 9. To collect or not (anti-pattern) getTrades().stream() .filter(t -> getDate().equals(t.getInfo().getDate())) .collect(toList()) .forEach(t -> trades.add(t.getInfo()));
  10. 10. To collect or not (solution) List<Trade> trades = getTrades().stream() .filter(t -> getDate().equals(t.getInfo().getDate())) .map(t → t.getInfo()) .collect(Collectors.toList());
  11. 11. Sort of sorted (anti pattern) Map<Date, List<Trade>> groupTrades = trades.stream() .sorted(comparing(Trade::getDate)) .collect(groupingBy(Trade::getDate));
  12. 12. Sort of sorted (solution) Map<Date, List<Trade>> groupTrades = trades.stream() .collect(groupingBy( TradeDetail::getTradeDate, TreeMap::new, toList()));
  13. 13. Multi-sorted (anti-pattern) return trade.stream() .filter(t -> !isExcluded(t)) .sorted(comparing(Trade::getDate)) .sorted(comparing(Trade::getCUSIP)) .sorted(comparing(Trade::getNetAmount)) .collect(toList());
  14. 14. Multi-sorted (solution) return trade.stream() .filter(t -> !isExcluded(t)) .sorted(comparing(Trade::getNetAmount) .thenComparing(Trade::getCUSIP) .thenComparing(Trade::getDate)) .collect(toList());
  15. 15. Top twenty words (Ugly) List<String> words = Files.lines(path).parallel() .flatMap(line -> Arrays.asList(line.split("b")).stream()) .collect(groupingBy(w -> w, counting())) .entrySet().stream() .sorted(comparing(Map.Entry<String, Long>::getValue).reversed()) .limit(20) .map(Map.Entry::getKey) .collect(Collectors.toList());
  16. 16. I must use streams (Ugly) combinedList.addAll( balances.stream().collect(Collectors.toList())); List<Trade> allTrades = new ArrayList<>(); trades1.forEach(t -> allTrades.add(t)); trades2.forEach(t -> allTrades.add(t));
  17. 17. Optional denial Position todayPos = newPos.stream() .filter(pos -> pos.getCUSIP().equals(p.getCUSIP())) .findFirst().orElse(null); if (todayPos != null) {
  18. 18. Optional denial Optional<MTrade> otodayTrade = trades.stream() .filter(t -> t.getCUSIP().equals(p.getCUSIP())).findFirst(); MTrade todayTrade = null; if (otodayTrade.isPresent()) todayTrade = otodayTrade.get(); if (todayTrade != null && todayTrade.getClosingPrice() != null) {
  19. 19. Q & A Peter Lawrey http://vanillajava.blogspot.com/ @PeterLawrey peter.lawrey@higherfrequencytrading.com

×