function _pr_str($obj, $print_readably = True) { if (_list_Q($obj)) { $ret = array(); foreach ($obj as $e) { array_push($ret, _pr_str($e, $print_readably)); } return "(" . implode(" ", $ret) . ")"; } elseif (_vector_Q($obj)) { $ret = array(); foreach ($obj as $e) { array_push($ret, _pr_str($e, $print_readably)); } return "[" . implode(" ", $ret) . "]"; } elseif (_hash_map_Q($obj)) { $ret = array(); foreach (array_keys($obj->getArrayCopy()) as $k) { $ret[] = _pr_str($k, $print_readably); $ret[] = _pr_str($obj[$k], $print_readably); } return "{" . implode(" ", $ret) . "}"; } elseif (is_string($obj)) { if (strpos($obj, chr(0x7f)) === 0) { return ":" . substr($obj, 1); } elseif ($print_readably) { $obj = preg_replace('/"/', '\\"', preg_replace('/\\\\/', '\\\\\\\\', $obj)); return '"' . $obj . '"'; } else { return $obj; } } elseif (is_integer($obj)) { return $obj; } elseif ($obj === NULL) { return "nil"; } elseif ($obj === true) { return "true"; } elseif ($obj === false) { return "false"; } elseif (_symbol_Q($obj)) { return $obj->value; } elseif (_atom_Q($obj)) { return "(atom " . _pr_str($obj->value, $print_readably) . ")"; } elseif (_function_Q($obj)) { return "(fn* [...] ...)"; } elseif (is_callable($obj)) { // only step4 and below return "#<function ...>"; } else { throw new Exception("_pr_str unknown type: " . gettype($obj)); } }
function _sequential_Q($seq) { return _list_Q($seq) or _vector_Q($seq); }
}, '-' => function ($a, $b) { return intval($a - $b, 10); }, '*' => function ($a, $b) { return intval($a * $b, 10); }, '/' => function ($a, $b) { return intval($a / $b, 10); }, 'time-ms' => function () { return time_ms(); }, 'list' => function () { return call_user_func_array('_list', func_get_args()); }, 'list?' => function ($a) { return _list_Q($a); }, 'vector' => function () { return call_user_func_array('_vector', func_get_args()); }, 'vector?' => function ($a) { return _vector_Q($a); }, 'hash-map' => function () { return call_user_func_array('_hash_map', func_get_args()); }, 'map?' => function ($a) { return _hash_map_Q($a); }, 'assoc' => function () { return call_user_func_array('assoc', func_get_args()); }, 'dissoc' => function () { return call_user_func_array('dissoc', func_get_args()); }, 'get' => function ($a, $b) { return get($a, $b); }, 'contains?' => function ($a, $b) { return contains_Q($a, $b); }, 'keys' => function ($a) { return keys($a); }, 'vals' => function ($a) {
function seq($src) { if (_list_Q($src)) { if (count($src) == 0) { return NULL; } return $src; } elseif (_vector_Q($src)) { if (count($src) == 0) { return NULL; } $tmp = $src->getArrayCopy(); $s = new ListClass(); $s->exchangeArray($tmp); return $s; } elseif (_string_Q($src)) { if (strlen($src) == 0) { return NULL; } $tmp = str_split($src); $s = new ListClass(); $s->exchangeArray($tmp); return $s; } elseif (_nil_Q($src)) { return NULL; } else { throw new Exception("seq: called on non-sequence"); } return $s; }