Example #1
0
/**
 * @param resource $handleInput
 * @param resource $handleOutputForJson
 */
function exampleCsvToJson($handleInput, $handleOutputForJson)
{
    $streamIterator = convertFileStreamToIterator($handleInput);
    fwrite($handleOutputForJson, "[\n");
    Sequence::make($streamIterator)->map(function ($line) {
        return str_getcsv($line);
    })->map(fnCallMapWithLastResult(function ($row, $key, $lastRow) {
        if ($lastRow) {
            $keys = array_keys($lastRow);
            // Use the keys from the last row
        } else {
            $keys = $row;
            // This is the first row, use it as the keys.
        }
        return array_combine($keys, $row);
        // Put them together and return.
    }))->map(fn\fnIdentity())->offset(1)->map(function ($row) {
        return json_encode($row);
    })->walk(fnJsonWriter($handleOutputForJson));
    fwrite($handleOutputForJson, "]");
}
Example #2
0
/**
 * @param array|\iterator $employees
 * @return array
 */
function exampleIdentity($employees)
{
    $values = Sequence::make($employees)->map(fn\fnIdentity())->to_a();
    return $values;
}
Example #3
0
 /**
  * Generate a function that returns the value given.
  *
  * @return callable
  */
 public static function fnIdentity()
 {
     return fn\fnIdentity();
 }