diff --git a/Dict.ark b/Dict.ark index 1acebb7..7ced589 100644 --- a/Dict.ark +++ b/Dict.ark @@ -22,6 +22,27 @@ # @author https://github.com/SuperFola (let add (fun (_D _key _value) (builtin__dict:add _D _key _value))) +# @brief Checks if the dictionary has a given key +# @param _D dictionary +# =begin +# (let data (dict "key" "value")) +# (print (dict:contains? data "key")) # true +# (print (dict:contains? data "test")) # false +# =end +# @author https://github.com/SuperFola +(let contains? (fun (_D _key) (builtin__dict:contains _D _key))) + +# @brief Checks if the dictionary has a given key +# @details **Deprecated, use `contains?`** +# @param _D dictionary +# =begin +# (let data (dict "key" "value")) +# (print (dict:contains data "key")) # true +# (print (dict:contains data "test")) # false +# =end +# @author https://github.com/SuperFola +(let contains contains?) + # @brief Get a value from a given dictionary using a key, or a default value if it doesn't exist # @param _D dictionary # @param _key key to get @@ -53,16 +74,6 @@ (add _D _key (_f (get _D _key))) (add _D _key _default)))) -# @brief Checks if the dictionary has a given key -# @param _D dictionary -# =begin -# (let data (dict "key" "value")) -# (print (dict:contains data "key")) # true -# (print (dict:contains data "test")) # false -# =end -# @author https://github.com/SuperFola -(let contains (fun (_D _key) (builtin__dict:contains _D _key))) - # @brief Deletes an entry from a dictionary, given a key # @details The dictionary is modified in place # @param _D dictionary diff --git a/List.ark b/List.ark index dd0d9b1..c5a8afd 100644 --- a/List.ark +++ b/List.ark @@ -223,7 +223,7 @@ # @details The original list is not modified. # =begin # (import std.Math) -# (print (list:filter [1 2 3 4 5 6 7 8 9] math:even)) # [2 4 6 8] +# (print (list:filter [1 2 3 4 5 6 7 8 9] math:even?)) # [2 4 6 8] # =end # @author https://github.com/rstefanic (let filter (fun (_L _f) { diff --git a/Math.ark b/Math.ark index 337c08e..d2058a6 100644 --- a/Math.ark +++ b/Math.ark @@ -169,12 +169,24 @@ # @brief Return true if the number is even, false otherwise # @param _n the number # @author https://github.com/rstefanic -(let even (fun (_n) (= 0 (mod _n 2)))) +(let even? (fun (_n) (= 0 (mod _n 2)))) + +# @brief Return true if the number is even, false otherwise +# @details **Deprecated, use `even?`** +# @param _n the number +# @author https://github.com/rstefanic +(let even even?) + +# @brief Return true if the number is odd, false otherwise +# @param _n the number +# @author https://github.com/rstefanic +(let odd? (fun (_n) (= 1 (abs (mod _n 2))))) # @brief Return true if the number is odd, false otherwise +# @details **Deprecated, use `odd?`** # @param _n the number # @author https://github.com/rstefanic -(let odd (fun (_n) (= 1 (abs (mod _n 2))))) +(let odd odd?) # @brief Get the minimum between two numbers # @param _a the first number diff --git a/Range.ark b/Range.ark index 5e2cc0b..0a6b146 100644 --- a/Range.ark +++ b/Range.ark @@ -61,7 +61,7 @@ # =begin # (import std.Math) # (let obj (range:range 1 10)) -# (print (range:filter obj math:even)) # [2 4 6 8] +# (print (range:filter obj math:even?)) # [2 4 6 8] # =end # @author https://github.com/SuperFola (let filter (fun (_range _fun) { diff --git a/String.ark b/String.ark index 54896fe..6e30120 100644 --- a/String.ark +++ b/String.ark @@ -100,6 +100,31 @@ # @author https://github.com/SuperFola (let setAt (fun (_str _index _x) (builtin__string:setAt _str _index _x))) +# @brief Check if a string contains a word +# @param _str String where the lookup occurs +# @param _word Word to look up for +# @author https://github.com/SuperFola +(let contains? (fun (_str _word) + (!= -1 (find _str _word)))) + +# @brief Check if a string contains a word for a set of words +# @param _str String where the lookup occurs +# @param _set_of_words Words to look for +# =begin +# (let words ["hello" "world"]) +# (print (containsAnyOf "Hello, world! I like almonds" words)) # true +# (print (containsAnyOf "Hello, world!" string:punctuation)) # true +# =end +# @author https://github.com/SuperFola +(let containsAnyOf? (fun (_str _set_of_words) { + (mut _contains false) + (mut _i 0) + (while (and (not _contains) (< _i (len _set_of_words))) { + (if (!= -1 (find _str (@ _set_of_words _i))) + (set _contains true)) + (set _i (+ 1 _i)) }) + _contains })) + # @brief Check if a string is empty or only consists of whitespaces # @param _str the string to check # =begin @@ -254,7 +279,7 @@ # @details Returns a list of strings. Example : # =begin # (let message "hello world, I like boats") -# (let splitted (string:split message " ")) +# (let as-list (string:split message " ")) # =end # @author https://github.com/Natendrtfm (let split (fun (_string _separator) { diff --git a/Testing.ark b/Testing.ark index f4aef5f..ef06abc 100644 --- a/Testing.ark +++ b/Testing.ark @@ -203,8 +203,8 @@ (testing:_report_success)) }) # @brief Compare two values that should be equal and generate a test case -# @param _expected expected value # @param _expr computed value to test +# @param _expected expected value # @param _desc an optional description (string) for the test # =begin # (test:suite name { @@ -212,21 +212,21 @@ # (test:eq 5 (foo) "foo should return 5")}) # =end # @author https://github.com/SuperFola -(macro test:eq (_expected _expr ..._desc) { +(macro test:eq (_expr _expected ..._desc) { (if (= ($as-is _expected) ($as-is _expr)) (testing:_report_success) (testing:_report_error ($as-is _expected) ($as-is _expr) ($repr _expected) ($repr _expr) _desc)) }) # @brief Compare two values that should **not** be equal and generate a test case -# @param _unexpected the value we don't want # @param _value tested value +# @param _unexpected the value we don't want # @param _desc an optional description (string) for the test # =begin # (test:suite name { # (test:neq 0 (my_function 1 2 3))}) # =end # @author https://github.com/SuperFola -(macro test:neq (_unexpected _value ..._desc) { +(macro test:neq (_value _unexpected ..._desc) { (if (!= ($as-is _unexpected) ($as-is _value)) (testing:_report_success) (testing:_report_error ($as-is _unexpected) ($as-is _value) ($repr _unexpected) ($repr _value) _desc)) }) diff --git a/tests/dict-tests.ark b/tests/dict-tests.ark index 8129543..67ef81f 100644 --- a/tests/dict-tests.ark +++ b/tests/dict-tests.ark @@ -50,15 +50,15 @@ (test:eq (dict:getOrElse empty "key" 0) 0) }) (test:case "contains" { - (test:expect (dict:contains d "key")) - (test:expect (dict:contains d 5)) - (test:expect (dict:contains d true)) - (test:expect (dict:contains d false)) - (test:expect (dict:contains d foo)) - (test:expect (dict:contains d closure)) - (test:expect (not (dict:contains d -1))) - (test:expect (not (dict:contains d "hello"))) - (test:expect (not (dict:contains d d))) }) + (test:expect (dict:contains? d "key")) + (test:expect (dict:contains? d 5)) + (test:expect (dict:contains? d true)) + (test:expect (dict:contains? d false)) + (test:expect (dict:contains? d foo)) + (test:expect (dict:contains? d closure)) + (test:expect (not (dict:contains? d -1))) + (test:expect (not (dict:contains? d "hello"))) + (test:expect (not (dict:contains? d d))) }) (test:case "size" { (test:eq (dict:size d) 6) @@ -153,9 +153,9 @@ (test:case "filter!" { (dict:filter! d2 filter_odd) (test:eq (dict:size d2) 2) - (test:expect (dict:contains d2 "a")) - (test:expect (not (dict:contains d2 "b"))) - (test:expect (dict:contains d2 "c")) + (test:expect (dict:contains? d2 "a")) + (test:expect (not (dict:contains? d2 "b"))) + (test:expect (dict:contains? d2 "c")) (test:eq (dict:get d2 "a") 1) (test:eq (dict:get d2 "c") 3) diff --git a/tests/list-tests.ark b/tests/list-tests.ark index 3565be7..c0b12ce 100644 --- a/tests/list-tests.ark +++ b/tests/list-tests.ark @@ -1,5 +1,5 @@ (import std.List) -(import std.Math :even) +(import std.Math :even?) (import std.Testing) (let a [1 2 3]) @@ -69,7 +69,7 @@ (test:eq (list:dropWhile a (fun (c) (< c 5))) []) }) (test:case "filter" { - (test:eq (list:filter a math:even) [2]) + (test:eq (list:filter a math:even?) [2]) (test:eq (list:filter a (fun (e) (> e 100))) []) (test:eq (list:filter [] (fun (e) (> e 100))) []) }) diff --git a/tests/math-tests.ark b/tests/math-tests.ark index c822e75..3271bbc 100644 --- a/tests/math-tests.ark +++ b/tests/math-tests.ark @@ -33,10 +33,10 @@ (test:eq (math:abs -1) 1) (test:eq (math:abs 1) 1) - (test:expect (math:even 2)) - (test:expect (math:even -2)) - (test:expect (math:odd 1)) - (test:expect (math:odd -1)) + (test:expect (math:even? 2)) + (test:expect (math:even? -2)) + (test:expect (math:odd? 1)) + (test:expect (math:odd? -1)) (test:eq (math:min 1 2) 1) (test:eq (math:min 1 -2) -2) (test:eq (math:min 0.5 0.2) 0.2) diff --git a/tests/range-tests.ark b/tests/range-tests.ark index b570274..0267d9d 100644 --- a/tests/range-tests.ark +++ b/tests/range-tests.ark @@ -1,5 +1,5 @@ (import std.Range:*) -(import std.Math :even) +(import std.Math :even?) (import std.Testing) (test:suite range { @@ -17,7 +17,7 @@ (range:forEach r (fun (e) (test:neq e nil))) - (let filtered (range:filter r math:even)) + (let filtered (range:filter r math:even?)) (test:eq filtered [0 2 4 6 8]) (let mapped (range:map r (fun (e) (* e e)))) diff --git a/tests/string-tests.ark b/tests/string-tests.ark index a02b899..cbc3bfc 100644 --- a/tests/string-tests.ark +++ b/tests/string-tests.ark @@ -9,6 +9,17 @@ (test:eq (builtin__string:removeAt "abcd" 1) (string:removeAt "abcd" 1)) (test:eq (builtin__string:setAt "abcd" 1 "z") (string:setAt "abcd" 1 "z")) + (test:expect (string:contains? "hello, world" "h")) + (test:expect (string:contains? "hello, world" "hello")) + (test:expect (string:contains? "hello, world" "world")) + (test:expect (string:contains? "hello, world" ",")) + (test:expect (not (string:contains? "hello, world" "!"))) + + (test:expect (string:containsAnyOf? "Hello, world" ["world" "one"])) + (test:expect (string:containsAnyOf? "Hello, world" string:punctuation)) + (test:expect (not (string:containsAnyOf? "Hello, world" []))) + (test:expect (not (string:containsAnyOf? "Hello, world" ["nice" "cool"]))) + (test:eq (string:emptyOrWhitespace? "") true) (test:eq (string:emptyOrWhitespace? " ") true) (test:eq (string:emptyOrWhitespace? " ") true)