hy, php, powershell, scala: Add number?, fn?, macro?
authorVasilij Schneidermann <mail@vasilij.de>
Mon, 23 Oct 2017 07:06:26 +0000 (09:06 +0200)
committerVasilij Schneidermann <mail@vasilij.de>
Mon, 23 Oct 2017 07:06:26 +0000 (09:06 +0200)
hy/core.hy
php/core.php
php/types.php
powershell/core.psm1
powershell/types.psm1
scala/core.scala

index d4984a7..37ce466 100644 (file)
    "nil?"     none?
    "true?"    (fn [a] (and (instance? bool a) (= a True)))
    "false?"   (fn [a] (and (instance? bool a) (= a False)))
+   "number?"  (fn [a] (instance? int a))
    "string?"  (fn [a] (and (string? a) (not (keyword? a))))
    "symbol"   (fn [a] (Sym a))
    "symbol?"  (fn [a] (instance? Sym a))
    "keyword"  (fn [a] (Keyword (if (keyword? a) a (+ ":" a))))
    "keyword?" (fn [a] (keyword? a))
+   "fn?"      (fn [a] (and (callable a) (or (not (hasattr a "macro"))
+                                            (not a.macro))))
+   "macro?"   (fn [a] (and (callable a) (and (hasattr a "macro") a.macro)))
 
    "pr-str"  (fn [&rest a] (Str (.join " " (map (fn [e] (pr-str e True)) a))))
    "str"     (fn [&rest a] (Str (.join "" (map (fn [e] (pr-str e False)) a))))
index 49c6324..245b938 100644 (file)
@@ -209,12 +209,15 @@ $core_ns = array(
     'nil?'=>   function ($a) { return _nil_Q($a); },
     'true?'=>  function ($a) { return _true_Q($a); },
     'false?'=> function ($a) { return _false_Q($a); },
+    'number?'=> function ($a) { return _number_Q($a); },
     'symbol'=> function () { return call_user_func_array('_symbol', func_get_args()); },
     'symbol?'=> function ($a) { return _symbol_Q($a); },
     'keyword'=> function () { return call_user_func_array('_keyword', func_get_args()); },
     'keyword?'=> function ($a) { return _keyword_Q($a); },
 
     'string?'=> function ($a) { return _string_Q($a); },
+    'fn?'=>    function($a) { return _fn_Q($a) || (_function_Q($a) && !$a->ismacro ); },
+    'macro?'=> function($a) { return _function_Q($a) && $a->ismacro; },
     'pr-str'=> function () { return call_user_func_array('pr_str', func_get_args()); },
     'str'=>    function () { return call_user_func_array('str', func_get_args()); },
     'prn'=>    function () { return call_user_func_array('prn', func_get_args()); },
index 1279f88..57157b3 100644 (file)
@@ -50,6 +50,7 @@ function _false_Q($obj) { return $obj === false; }
 function _string_Q($obj) {
     return is_string($obj) && strpos($obj, chr(0x7f)) !== 0;
 }
+function _number_Q($obj) { return is_int($obj); }
 
 
 // Symbols
@@ -114,6 +115,7 @@ function _function($func, $type='platform',
     return new FunctionClass($func, $type, $ast, $env, $params, $ismacro);
 }
 function _function_Q($obj) { return $obj instanceof FunctionClass; }
+function _fn_Q($obj) { return $obj instanceof Closure; }
 
 
 // Parent class of list, vector, hash-map
index f95464c..b2e592d 100644 (file)
@@ -103,11 +103,15 @@ $core_ns = @{
     "nil?"        = { param($a); $a -eq $null };
     "true?"       = { param($a); $a -eq $true };
     "false?"      = { param($a); $a -eq $false };
+    "number?"     = { param($a); $a -is [int32] };
     "string?"     = { param($a); string? $a };
     "symbol"      = Get-Command new-symbol;
     "symbol?"     = { param($a); symbol? $a };
     "keyword"     = Get-Command new-keyword;
     "keyword?"    = { param($a); keyword? $a };
+    "fn?"         = { param($a); (fn? $a) -or ((malfunc? $a) -and
+                                               (-not $a.macro)) };
+    "macro?"      = { param($a); (malfunc? $a) -and $a.macro };
 
     "pr-str"      = { pr_seq $args $true  " " };
     "str"         = { pr_seq $args $false "" };
index e2fb735..7eeaa37 100644 (file)
@@ -271,6 +271,10 @@ function new-malfunc($ast, $params, $env, $fn, $macro, $meta) {
 function malfunc?($obj) {
     $obj -is [MalFunc]
 }
+
+function fn?($obj) {
+    $obj -is [System.Management.Automation.ScriptBlock]
+}
 #
 # General functions
 #
index f92e660..c56851b 100644 (file)
@@ -31,6 +31,21 @@ object core {
     }
   }
 
+  def fn_Q(a: List[Any]) = {
+    a(0) match {
+      case s: Func => true
+      case s: MalFunction => !s.asInstanceOf[MalFunction].ismacro
+      case _ => false
+    }
+  }
+
+  def macro_Q(a: List[Any]) = {
+    a(0) match {
+      case s: MalFunction => s.asInstanceOf[MalFunction].ismacro
+      case _ => false
+    }
+  }
+
   // number functions
   def _bool_op(a: List[Any], op: (Long, Long) => Boolean) = {
     op(a(0).asInstanceOf[Long],a(1).asInstanceOf[Long])
@@ -40,6 +55,10 @@ object core {
     op(a(0).asInstanceOf[Long],a(1).asInstanceOf[Long])
   }
 
+  def number_Q(a: List[Any]) = {
+    a(0).isInstanceOf[Long] || a(0).isInstanceOf[Double]
+  }
+
 
   // string functions
   def read_string(a: List[Any]) = {
@@ -232,11 +251,14 @@ object core {
     "nil?" -> ((a: List[Any]) => a(0) == null),
     "true?" -> ((a: List[Any]) => a(0) == true),
     "false?" -> ((a: List[Any]) => a(0) == false),
+    "number?" -> number_Q _,
     "string?" -> string_Q _,
     "symbol" -> ((a: List[Any]) => Symbol(a(0).asInstanceOf[String])),
     "symbol?" -> ((a: List[Any]) => a(0).isInstanceOf[Symbol]),
     "keyword" -> keyword _,
     "keyword?" -> keyword_Q _,
+    "fn?" -> fn_Q,
+    "macro?" -> macro_Q,
 
     "pr-str" -> ((a: List[Any]) => _pr_list(a, true, " ")),
     "str" -> ((a: List[Any]) => _pr_list(a, false, "")),