Exemple #1
0
 public function test_reduce()
 {
     $array = [1, 2, 3, 4];
     $sum = F\reduce('Tarsana\\Functional\\plus', 0);
     $this->assertEquals(10, $sum($array));
 }
Exemple #2
0
 protected function parseObject($text)
 {
     $default = null;
     if (F\head($text) == '[' && F\last($text) == ']') {
         $text = F\init(F\tail($text));
         $default = '';
     }
     $results = [];
     $count = preg_match_all('/^([a-zA-Z_-]*)\\{([^,a-zA-Z0-9\\[]+)?,?(.*)\\}$/', $text, $results);
     if ($count < 1) {
         return null;
     }
     $fields = [];
     if (trim($results[3][0]) != '') {
         $fields = F\chunks('(){}[]""', $this->fieldsSeparator, $results[3][0]);
         $fields = F\reduce(function ($results, $item) {
             $item = $this->doParse(trim($item));
             $results[$item->description()] = $item;
             return $results;
         }, [], $fields);
     }
     $separator = $results[2][0];
     if (empty($separator)) {
         $separator = $this->objectSeparator;
     }
     return S::obj($fields, $separator, $default, $results[1][0]);
 }