Why does stream split require a function or delegate?

← Streams and Pipelines · Ref: Q857

Stream split requires a predicate function that takes one argument and returns Boolean. Without a predicate, the stream cannot decide how to split elements.

See Q813 for stream rules.

Example

defines module qa.streams.split.needs.function

  defines constant

    SHORT_THRESHOLD <- 2

  defines function

    isShort() as pure
      -> item as String
      <- rtn as Boolean: length item < SHORT_THRESHOLD

  defines program

    StreamSplitDemo()
      collection <- List() of List of String
      cat ["A", "B", "C"] | split by isShort > collection
      require collection?

Common mistakes

E07860 — Stream split requires a predicate function. Without one, the stream cannot decide how to split. See ek9 -h E07860 for details.

Incorrect:

      cat ["A", "B", "C"] | split > collection

Correct:

      cat ["A", "B", "C"] | split by isShort > collection
Other ways to ask this
  • What triggers E07860 FUNCTION_OR_DELEGATE_REQUIRED?
  • Why can't I use split without a predicate in EK9 streams?
  • What does stream split need as an argument?

Coming from another language?

Java: no built-in split stream op. Python: itertools has no split. EK9: split requires predicate function.

Keywords: function, stream, pipeline, predicate, E07860, split, delegate