|
| 1 | +--# |
| 2 | +* @name toString |
| 3 | +* @brief Convert a value to a string |
| 4 | +* @param value anything |
| 5 | +* =begin |
| 6 | +* (print (toString "abc")) # "abc" |
| 7 | +* (print (toString 1)) # "1" |
| 8 | +* (print (toString (fun () ()))) # "Function@1" |
| 9 | +* (print (toString print)) # "CProcedure" |
| 10 | +* (print (toString [])) # "[]" |
| 11 | +* (let x 1) (print (toString (fun (&x) ()))) # "(.x=1)" |
| 12 | +* (print (toString (dict 1 2 3 4))) # "{1: 2, 3: 4}" |
| 13 | +* (print (toString nil)) # "nil" |
| 14 | +* (print (toString true)) # "true" |
| 15 | +* (print (toString false)) # "false" |
| 16 | +* =end |
| 17 | +#-- |
| 18 | + |
| 19 | +--# |
| 20 | +* @name type |
| 21 | +* @brief Get the type of a given value as a string |
| 22 | +* @param value anything |
| 23 | +* =begin |
| 24 | +* (print (type "abc")) # "String" |
| 25 | +* (print (type 1)) # "Number" |
| 26 | +* (print (type (fun () ()))) # "Function" |
| 27 | +* (print (type print)) # "CProc" |
| 28 | +* (print (type [])) # "List" |
| 29 | +* (let x 1) (print (type (fun (&x) ()))) # "Closure" |
| 30 | +* (print (type (dict 1 2 3 4))) # "Dict" |
| 31 | +* (print (type nil)) # "Nil" |
| 32 | +* (print (type true)) # "Bool" |
| 33 | +* (print (type false)) # "Bool" |
| 34 | +* =end |
| 35 | +#-- |
| 36 | + |
| 37 | +--# |
| 38 | +* @name nil? |
| 39 | +* @brief Check if a value is nil |
| 40 | +* @param value anything |
| 41 | +* =begin |
| 42 | +* (print (nil? "abc")) # false |
| 43 | +* (print (nil? 1)) # false |
| 44 | +* (print (nil? (fun () ()))) # false |
| 45 | +* (print (nil? print)) # false |
| 46 | +* (print (nil? [])) # false |
| 47 | +* (print (nil? (dict 1 2 3 4))) # false |
| 48 | +* (print (nil? nil)) # true |
| 49 | +* (print (nil? true)) # false |
| 50 | +* (print (nil? false)) # false |
| 51 | +* =end |
| 52 | +#-- |
| 53 | + |
| 54 | +--# |
| 55 | +* @name comparison |
| 56 | +* @brief Compare two values and return true or false |
| 57 | +* @details Comparing two values (using `<`, `>`, `<=`, `>=` and `=`) with a different type will always return false |
| 58 | +* @param a first value |
| 59 | +* @param b second value |
| 60 | +* =begin |
| 61 | +* (print (< "abc" "def")) # true, string are compared lexicographically |
| 62 | +* (print (< 2 1)) # false |
| 63 | +* (print (> 3 -5.5) # true |
| 64 | +* (print (> "Hello" "")) |
| 65 | +* (print (<= [] [1 2])) # true, lists are compared lexicographically |
| 66 | +* (print (<= [1 2] [1 0])) # false |
| 67 | +* (print (<= [1 2] [10])) # true |
| 68 | +* (print (>= 5 5)) # true |
| 69 | +* (print (= false 5)) # false |
| 70 | +* (print (!= false 5)) # true |
| 71 | +* =end |
| 72 | +#-- |
| 73 | + |
| 74 | +--# |
| 75 | +* @name not |
| 76 | +* @brief Convert a value to a boolean and invert it |
| 77 | +* @param value anything |
| 78 | +* =begin |
| 79 | +* (print (not "")) # true |
| 80 | +* (print (not "a")) # false |
| 81 | +* (print (not 0)) # true |
| 82 | +* (print (not 1)) # false |
| 83 | +* (print (not [])) # true |
| 84 | +* (print (not [1 2])) # false |
| 85 | +* (print (not nil)) # true |
| 86 | +* (print (not true)) # false |
| 87 | +* (print (not false)) # true |
| 88 | +* (print (not (dict))) # true |
| 89 | +* (print (not (dict "a" 1))) # false |
| 90 | +* =end |
| 91 | +#-- |
| 92 | + |
| 93 | +--# |
| 94 | +* @name hasField |
| 95 | +* @brief Check if a closure has a given field |
| 96 | +* @param c closure |
| 97 | +* @param field string, field name to look for |
| 98 | +* =begin |
| 99 | +* (let x 1) |
| 100 | +* (let b "hello") |
| 101 | +* (let closure (fun (&x &b) ())) |
| 102 | +* (print (hasField closure "x")) # true |
| 103 | +* (print (hasField closure "b")) # true |
| 104 | +* (print (hasField closure "B")) # false, field names are case-sensitive |
| 105 | +* =end |
| 106 | +#-- |
0 commit comments