{"record":{"id":"194990a3524e05ae","repo":"xai-org/x-algorithm","slug":"cannot-pass-empty-collection-to-foldl1","errorCode":null,"errorMessage":"cannot pass empty collection to Foldl1()","messagePattern":"cannot pass empty collection to Foldl1\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"botmaker/src/java/com/twitter/botmaker/function/collection/Fold.java","lineNumber":178,"sourceCode":"      }\n\n      @Override\n      public Extractor<Runtime> toExtractor() {\n\n        Extractor funcExtractor = funcNode.toExtractor();\n        Extractor collectionExtractor = collectionNode.toExtractor();\n        ImmutableList<Extractor> children = ImmutableList.of(funcExtractor, collectionExtractor);\n\n        return new Extractor<Runtime>(this, children) {\n\n          @Override\n          public Future run(Context<Runtime> context) {\n            return context.run(collectionExtractor).flatMap((Object collectionObj) -> {\n              @SuppressWarnings(\"unchecked\")\n              Collection<Object> collection = (Collection<Object>) collectionObj;\n              Iterator<Object> iterator = collection.iterator();\n              if (!iterator.hasNext()) {\n                throw new RuntimeException(\"cannot pass empty collection to Foldl1()\");\n              }\n              return FoldLeftFunction.evaluate(\n                  context, Future.value(iterator.next()),\n                  varA, varB, iterator, funcExtractor);\n            }).rescue(Function.func((Throwable ex) ->\n                    Future.exception(FunctionFailure.wrap(ex, getAstNode(), context))));\n          }\n        };\n      }\n    };\n  }\n}\n","sourceCodeStart":160,"sourceCodeEnd":191,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/botmaker/src/java/com/twitter/botmaker/function/collection/Fold.java#L160-L191","documentation":"Foldl1() initializes its accumulator from the first element of the input collection; an empty collection has no initializer, so at runtime it throws RuntimeException 'cannot pass empty collection to Foldl1()'. This is caught and re-wrapped into FunctionFailure by the surrounding rescue.","triggerScenarios":"Calling Foldl1(func, emptyCollection) at runtime — e.g. filtering a list to nothing then folding, or folding an empty followers/ids list.","commonSituations":"Data-dependent emptiness: filters, lookups, or time-windowed aggregations producing zero elements only for some users/requests.","solutions":["Guard with IsEmpty/Count before folding and supply a default result for the empty case","Use Fold (with explicit initial accumulator) instead of Foldl1 when empty input is possible","Fix the upstream filter/lookup so it cannot produce an empty collection when data is expected"],"exampleFix":"// before\nFoldl1(add, items)\n// after\nIf(Equals(Count(items), 0), 0, Foldl1(add, items))","handlingStrategy":"validation","validationCode":"// rule-language: guard empty collections before Foldl1\nIf(Equals(Count(items), 0), defaultResult, Foldl1(fn, items))","typeGuard":"// rule-language\nIsEmpty(items) // use to branch before folding","tryCatchPattern":"// host code: FunctionFailure wrapping RuntimeException 'cannot pass empty collection to Foldl1()' is deterministic — do not retry; branch on emptiness instead","preventionTips":["Prefer Fold with an explicit initial accumulator when empty input is possible","Always pair Foldl1 with an emptiness check","Treat emptiness as expected data, not an error case"],"tags":["botmaker","runtime","empty-collection","fold"],"backgroundTag":"empty-collection-operation","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}