A List of functions, iterated and streamed

The complete source of collectionTypes_list_of_functions.ek9, compiled by the EK9 compiler. Shown on collectionTypes.html.

#!ek9
defines module site.collectionTypes.listOfFunctions

  defines record

    //Designed to hold members of the Addams family only
    Addams
      name <- String()
      dob <- Date()

      //Sample of developer constructors, the no-argument one still gets auto generated.
      Addams() as pure
        -> dateOfBirth as Date
        dob :=? Date(dateOfBirth)

      Addams() as pure
        ->
          name as String
          dateOfBirth as Date
        this.name :=? String(name)
        dob :=? Date(dateOfBirth)

      operator $ as pure
        <- rtn as String: `${name} ${dob}`

      override operator ? as pure
        <- rtn as Boolean: name? and dob?

      operator #^ as pure
        <- rtn as String: $this

  defines function

    //Just convert Addams family member to a String representation
    addamsToString() as pure
      -> person as Addams
      <- rtn as String: `DOB: ${person.dob} Name: ${person.name}`

    //Polymorphic abstract function for getting Addams family members
    getAddams() as pure abstract
      <- rtn as Addams?

    //Using the full constructor.
    getMorticia() is getAddams as pure
      <- rtn as Addams: Addams("Morticia", 1965-01-03)

    //Use developer defined constructor then set the name
    getGomez() is getAddams as pure
      <- rtn as Addams: Addams(1963-06-08)
      rtn.name :=? "Gomez"

  //Concept to hold functions that can get Addams family members
  //Then when the time is right actually call the function to get the member.
  defines program

    ListFunctionIteratorExample()
      stdout <- Stdout()

      //Note this is a List of getAddams (function)
      //EK9 is smart enough to see they both have a common super (function signature)
      functionList <- [ getGomez, getMorticia ]
      iter <- functionList.iterator()

      //A bit long winded - see next stream pipeline example
      while iter?
        fn <- iter.next()
        //Now make the call and convert the returned value to a String for output
        stdout.println($fn())
        //Because Addams has the promote operator #^ to a String we could just do this
        stdout.println(fn())

    ListFunctionStreamExample()
      stdout <- Stdout()

      //Again a list of functions
      functionList <- [ getGomez, getMorticia ]

      //But here we pipe through to 'call' the function and get the return value
      //We use the fact that promotion can be used to convert to a default String
      cat functionList | call > stdout

      //Now if the calls to get members of the Addams family via the function
      //had to make remote or slow calls, you might be better off calling them concurrently
      //in an asynchronous manner - note the pipeline will wait until all the calls have completed.
      //The calls complete in any order, so sort them if the order matters.
      //But also in this case we want to control to conversion to a String by using a function.
      cat functionList | async | map with addamsToString | sort > stdout

      //It is also possible to just use this, rather than creating a variable to reference the list
      cat [ getGomez, getMorticia ] | async | map with addamsToString | sort > stdout

    ListFunctionsInShort()
      stdout <- Stdout()
      functionList <- [ getGomez, getMorticia ]

      iter <- functionList.iterator()
      while iter?
        fn <- iter.next()
        stdout.println(fn())

      cat functionList | call | map with addamsToString > stdout

      cat functionList | call > stdout

Download collectionTypes_list_of_functions.ek9

Output

ListFunctionIteratorExample

Gomez 1963-06-08
Gomez 1963-06-08
Morticia 1965-01-03
Morticia 1965-01-03

ListFunctionStreamExample

Gomez 1963-06-08
Morticia 1965-01-03
DOB: 1963-06-08 Name: Gomez
DOB: 1965-01-03 Name: Morticia
DOB: 1963-06-08 Name: Gomez
DOB: 1965-01-03 Name: Morticia

ListFunctionsInShort

Gomez 1963-06-08
Morticia 1965-01-03
DOB: 1963-06-08 Name: Gomez
DOB: 1965-01-03 Name: Morticia
Gomez 1963-06-08
Morticia 1965-01-03