diff --git a/Dict.ark b/Dict.ark index e06a63e..1acebb7 100644 --- a/Dict.ark +++ b/Dict.ark @@ -22,6 +22,21 @@ # @author https://github.com/SuperFola (let add (fun (_D _key _value) (builtin__dict:add _D _key _value))) +# @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 +# @param _default default value in case the key isn't in the dictionary +# =begin +# (let data (dict "key" "value")) +# (print (dict:getOrElse data "key" "hello")) # value +# (print (dict:getOrElse data "count" 5)) # 5 +# =end +# @author https://github.com/SuperFola +(let getOrElse (fun (_D _key _default) + (if (contains _D _key) + (get _D _key) + _default))) + # @brief Updates an entry or create it from a default value # @details The dictionary is modified in place # @param _D dictionary diff --git a/Events.ark b/Events.ark index 1a6d3b2..9074eab 100644 --- a/Events.ark +++ b/Events.ark @@ -2,7 +2,7 @@ # @brief Allows to register events listeners and emit events # =begin -# (let em (manager:make)) +# (let em (events:manager:make)) # (em.on "myType" (fun (value) (print "This is a callback"))) # (em.emit "myType") # => prints "This is a callback" thanks to the registered listener # =end diff --git a/Lazy.ark b/Lazy.ark index 8a23aa2..d6bb446 100644 --- a/Lazy.ark +++ b/Lazy.ark @@ -4,7 +4,7 @@ # (let complex_stuff (fun () { # # do complex work in the function # 42 })) -# (let lazy (eval complex_stuff)) +# (let lazy (lazy:eval complex_stuff)) # (print (lazy)) # =end # @author https://github.com/SuperFola diff --git a/List.ark b/List.ark index cec864f..dd0d9b1 100644 --- a/List.ark +++ b/List.ark @@ -72,11 +72,9 @@ # @param _func the function to call on each element # @details The original list is not modified. # =begin -# (import std.List) # (let collection [1 2 5 12]) -# (forEach collection (fun (element) { -# (print element) -# })) +# (list:forEach collection (fun (element) { +# (print element) })) # =end # @author https://github.com/SuperFola (let forEach (fun (_L _func) { @@ -87,13 +85,30 @@ (_func _element) (set _index (+ 1 _index)) }) })) +# @brief Iterate over a given list and run a given function on every element, passing its index as well. +# @param _L the list to iterate over +# @param _func a binary function to call on each element with (index, element) +# @details The original list is not modified. +# =begin +# (let collection [1 2 5 12]) +# (list:enumerate collection (fun (idx element) { +# (print idx " " element) })) +# =end +# @author https://github.com/SuperFola +(let enumerate (fun (_L _func) { + (mut _index 0) + + (while (< _index (len _L)) { + (mut _element (@ _L _index)) + (_func _index _element) + (set _index (+ 1 _index)) }) })) + # @brief Iterate over a given list and multiply all the elements with the others. # @param _L the list to iterate over # @details The original list is not modified. # =begin -# (import std.List) # (let collection [1 2 5 12]) -# (let p (product collection)) # => 120 +# (let p (list:product collection)) # => 120 # =end # @author https://github.com/Unactived (let product (fun (_L) { @@ -109,9 +124,8 @@ # @param _L the list to iterate over # @details The original list is not modified. # =begin -# (import std.List) # (let collection [1 2 5 12]) -# (let p (sum collection)) # => 20 +# (let p (list:sum collection)) # => 20 # =end # @author https://github.com/Unactived (let sum (fun (_L) { @@ -144,7 +158,7 @@ # @param _L list of numbers # @details The original list is not modified. # =begin -# (let value (list:min [0 1 2 3 5 8])) # 8 +# (let value (list:max [0 1 2 3 5 8])) # 8 # =end # @author https://github.com/SuperFola (let max (fun (_L) { @@ -165,7 +179,7 @@ # @details The original list is not modified. # =begin # (let cool-stuff [1 2 3 4 5 6 7 8 9]) -# (print (drop cool-stuff 4)) # [5 6 7 8 9] +# (print (list:drop cool-stuff 4)) # [5 6 7 8 9] # =end # @author https://github.com/rstefanic, https://github.com/SuperFola (let drop (fun (_L _n) @@ -188,7 +202,7 @@ # @details The original list is not modified. # =begin # (let cool-stuff [1 2 3 4 5 6 7 8 9]) -# (print (dropWhile cool-stuff (fun (a) (< a 4)))) # [4 5 6 7 8 9] +# (print (list:dropWhile cool-stuff (fun (a) (< a 4)))) # [4 5 6 7 8 9] # =end # @author https://github.com/SuperFola (let dropWhile (fun (_L _f) { @@ -209,7 +223,7 @@ # @details The original list is not modified. # =begin # (import std.Math) -# (print (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) { @@ -226,7 +240,7 @@ # @param _f the function to apply to each element # @details The original list is not modified. # =begin -# (print (map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81] +# (print (list:map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81] # =end # @author https://github.com/rstefanic (let map (fun (_L _f) { @@ -244,7 +258,7 @@ # @details The original list is not modified. # =begin # (let cool [1 2 3 4 5 6 7 8 9]) -# (print (reduce cool (fun (a b) (+ a b)))) # 45 +# (print (list:reduce cool (fun (a b) (+ a b)))) # 45 # =end # @author https://github.com/Unactived (let reduce (fun (_L _f) { @@ -261,7 +275,7 @@ # @details The original list is not modified. # =begin # (let cool [[1 2 3] [4] 5 6 [7 8] 9]) -# (print (flatten cool)) # [1 2 3 4 5 6 7 8 9] +# (print (list:flatten cool)) # [1 2 3 4 5 6 7 8 9] # =end # @author https://github.com/SuperFola (let flatten (fun (_L) { @@ -283,7 +297,7 @@ # @details The original list is not modified. # =begin # (let cool [1 2 3 4]) -# (print (flatMap cool (fun (a) [a a]))) # [1 1 2 2 3 3 4 4] +# (print (list:flatMap cool (fun (a) [a a]))) # [1 1 2 2 3 3 4 4] # =end # @author https://github.com/SuperFola (let flatMap (fun (_L _f) { @@ -304,7 +318,7 @@ # @param _n the number of elements to take # @details The original list is not modified. # =begin -# (print (take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4] +# (print (list:take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4] # =end # @author https://github.com/rstefanic (let take (fun (_L _n) { @@ -322,7 +336,7 @@ # @param _f the predicate # @details The original list is not modified. # =begin -# (print (takeWhile [1 2 3 4 5 6 7 8 9 10] (fun (a) (< a 4)))) # [1 2 3] +# (print (list:takeWhile [1 2 3 4 5 6 7 8 9 10] (fun (a) (< a 4)))) # [1 2 3] # =end # @author https://github.com/rakista112 (let takeWhile (fun (_L _f) { @@ -366,7 +380,7 @@ # @details The original list is not modified. # =begin # (let zipped [[1 5] [2 6] [3 7] [4 8]]) -# (print (unzip zipped)) # [[1 2 3 4] [5 6 7 8]] +# (print (list:unzip zipped)) # [[1 2 3 4] [5 6 7 8]] # =end # @author https://github.com/Unactived (let unzip (fun (_L) { @@ -389,7 +403,7 @@ # =begin # (let a [1 2 3 4]) # (let b [5 6 7 8]) -# (print (zip a b)) # [[1 5] [2 6] [3 7] [4 8]] +# (print (list:zip a b)) # [[1 5] [2 6] [3 7] [4 8]] # =end # @author https://github.com/Unactived (let zip (fun (_a _b) { @@ -407,7 +421,7 @@ # @details The original list is not modified. # =begin # (let a [5 6 7 8]) -# (print (zipWithIndex a)) # [[0 5] [1 6] [2 7] [3 8]] +# (print (list:zipWithIndex a)) # [[0 5] [1 6] [2 7] [3 8]] # =end # @author https://github.com/SuperFola (let zipWithIndex (fun (_L) { @@ -426,7 +440,7 @@ # @details The original list is not modified. # =begin # (let a [1 2 3 4]) -# (print (foldLeft a 0 (fun (a b) (+ a b)))) # 10 +# (print (list:foldLeft a 0 (fun (a b) (+ a b)))) # 10 # =end # @author https://github.com/SuperFola (let foldLeft (fun (_L _init _f) { @@ -440,11 +454,11 @@ # @brief Check if a condition is verified for all elements of a list # @param _L the list to work on -# @param _f the conditon +# @param _f the condition # =begin # (let a [1 2 3 4]) # (let f (fun (e) (< e 5))) -# (print (forAll a f)) # true +# (print (list:forAll a f)) # true # =end # @author https://github.com/Gryfenfer97 (let forAll (fun (_L _f) { @@ -463,7 +477,7 @@ # =begin # (let a [1 2 3 4]) # (let f (fun (e) (< e 3))) -# (print (any a f)) # true +# (print (list:any a f)) # true # =end # @author https://github.com/Gryfenfer97 (let any (fun (_L _f) { @@ -476,13 +490,25 @@ (set _index (+ 1 _index)) }) _verified })) +# @brief Check if a condition can't be verified for any element of a list +# @param _L the list to work on +# @param _f the condition +# =begin +# (let a [1 2 3 4]) +# (let f (fun (e) (< e 3))) +# (print (list:none a f)) # false +# (print (list:none [4 5 6] f)) # true +# =end +# @author https://github.com/SuperFola +(let none (fun (_L _f) (not (any _L _f)))) + # @brief Count the number of elements in a list that match a condition # @param _L the list to work on # @param _f the condition # =begin # (let lst [1 2 3 4 5 6 7 8 9]) # (let is_even (fun (e) (= 0 (mod e 2)))) -# (print (countIf lst is_even)) # 4 +# (print (list:countIf lst is_even)) # 4 # =end # @author https://github.com/SuperFola (let countIf (fun (_L _f) { @@ -503,7 +529,7 @@ # @param _length the sequence length # =begin # (let f (fun (x) (+ 7 x))) -# (print (iterate 0 f 10)) # [0 7 14 21 28 35 42 49 56 63] +# (print (list:iterate 0 f 10)) # [0 7 14 21 28 35 42 49 56 63] # =end # @author https://github.com/SuperFola (let iterate (fun (_init _f _length) { @@ -522,7 +548,7 @@ # @param _init initial value of the sequence # @param _length the sequence length # =begin -# (print (iota 0 10)) # [0 1 2 3 4 5 6 7 8 9] +# (print (list:iota 0 10)) # [0 1 2 3 4 5 6 7 8 9] # =end # @author https://github.com/SuperFola (let iota (fun (_init _length) (iterate _init (fun (x) (+ 1 x)) _length))) diff --git a/Math.ark b/Math.ark index 4411887..337c08e 100644 --- a/Math.ark +++ b/Math.ark @@ -43,7 +43,7 @@ # @param value the Number # =begin # (math:NaN? 2) # false -# (math:NaN? nan) # true +# (math:NaN? math:NaN) # true # =end # @author https://github.com/SuperFola (let NaN? (fun (_x) (builtin__math:NaN? _x))) @@ -52,7 +52,7 @@ # @param value the Number # =begin # (math:Inf? 1) # false -# (math:Inf? nan) # false +# (math:Inf? math:NaN) # false # =end # @author https://github.com/SuperFola (let Inf? (fun (_x) (builtin__math:Inf? _x))) @@ -194,6 +194,16 @@ _a _b))) +# @brief Increment a given number by 1 +# @param _x number +# @author https://github.com/SuperFola +(let increment (fun (_x) (+ 1 _x))) + +# @brief Decrement a given number by 1 +# @param _x number +# @author https://github.com/SuperFola +(let decrement (fun (_x) (- _x 1))) + # @brief Get a number to a given power # @details Note that it's defined as exp(a * ln(x)), thus won't work for negative numbers # @param _x the number to pow @@ -210,9 +220,6 @@ # @brief Run the fibonacci function on a number # @param n the number # @author https://github.com/SuperFola -# =begin -# (fibo 45 0 1) -# =end (let fibo (fun (n) { (let impl (fun (n p c) (if (<= n 0) @@ -243,10 +250,10 @@ # @brief Returns the list of a number's divisors # @param n the number -# @author https://github.com/Wafelack # =begin -# (divs 6) # Returns [1 2 3 6] +# (math:divs 6) # Returns [1 2 3 6] # =end +# @author https://github.com/Wafelack (let divs (fun (n) { (assert (>= n 2) "divs: n must be greater or equal to 2") (mut i 2) @@ -262,10 +269,10 @@ # @brief Returns the logarithm base n of a number # @param x the number # @param n the base -# @author https://github.com/Gryfenfer97 # =begin -# (log 81 3) # Returns 4 +# (math:log 81 3) # Returns 4 # =end +# @author https://github.com/Gryfenfer97 (let log (fun (x n) { (assert (> x 0) "log: x must be greater than 0") (assert (>= n 1) "log: n must be greater or equal to 1") @@ -273,34 +280,34 @@ # @brief Returns the logarithm base 2 of a number # @param x the number -# @author https://github.com/Gryfenfer97 # =begin -# (log2 128) # Returns 7 +# (math:log2 128) # Returns 7 # =end +# @author https://github.com/Gryfenfer97 (let log2 (fun (x) (log x 2))) # @brief Returns the logarithm base 10 of a number # @param x the number -# @author https://github.com/Gryfenfer97 # =begin -# (log10 1000) # Returns 3 +# (math:log10 1000) # Returns 3 # =end +# @author https://github.com/Gryfenfer97 (let log10 (fun (x) (log x 10))) # @brief Returns the quotient of the euclidian division of a and b # @param a the dividend # @param b the divisor -# @author https://github.com/fabien-zoccola # =begin -# (floordiv 14 6) # Returns 2 +# (math:floordiv 14 6) # Returns 2 # =end +# @author https://github.com/fabien-zoccola (let floordiv (fun (a b) (floor (/ a b)))) # @brief Create a complex number # @param real the real part of the complex number # @param imag the imaginary value # =begin -# (let c (complex 1 2)) +# (let c (math:complex 1 2)) # (print c.real " " c.imag) # 1 2 # =end # @author https://github.com/SuperFola @@ -312,7 +319,7 @@ # @param _c0 the first complex number # @param _c1 the second complex number # =begin -# (let c (complex-add (complex 1 2) (complex 3 4))) +# (let c (math:complex-add (math:complex 1 2) (math:complex 3 4))) # (print c.real " " c.imag) # 4 6 # =end # @author https://github.com/SuperFola @@ -322,7 +329,7 @@ # @param _c0 the first complex number # @param _c1 the second complex number # =begin -# (let c (complex-sub (complex 1 2) (complex 3 4))) +# (let c (math:complex-sub (math:complex 1 2) (math:complex 3 4))) # (print c.real " " c.imag) # -2 -2 # =end # @author https://github.com/SuperFola @@ -332,7 +339,7 @@ # @param _c0 the first complex number # @param _c1 the second complex number # =begin -# (let c (complex-mul (complex 1 2) (complex 3 4))) +# (let c (math:complex-mul (math:complex 1 2) (math:complex 3 4))) # (print c.real " " c.imag) # -5 10 # =end # @author https://github.com/SuperFola @@ -341,7 +348,7 @@ # @brief Compute the conjugate of a complex number # @param _c the complex number # =begin -# (let c (complex-conjugate (complex 1 2))) +# (let c (math:complex-conjugate (math:complex 1 2))) # (print c.real " " c.imag) # 1 -2 # =end # @author https://github.com/SuperFola @@ -350,7 +357,7 @@ # @brief Compute the module of a complex number # @param _c the complex number # =begin -# (let c (complex-module (complex 1 2))) +# (let c (math:complex-module (math:complex 1 2))) # (print c) # 2.2360679774997896964... # =end # @author https://github.com/SuperFola @@ -360,7 +367,7 @@ # @param _c0 the first complex number # @param _c1 the second complex number # =begin -# (let c (complex-div (complex 1 2) (complex 3 4))) +# (let c (math:complex-div (math:complex 1 2) (math:complex 3 4))) # (print c.real " " c.imag) # 0.44 0.08 # =end # @author https://github.com/SuperFola @@ -375,8 +382,8 @@ # @param _min minimum # @param _max maximum # =begin -# (print (clamp 5 0 2)) # 2 -# (print (clamp 6 0 10)) # 6 +# (print (math:clamp 5 0 2)) # 2 +# (print (math:clamp 6 0 10)) # 6 # =end # @author https://github.com/SuperFola (let clamp (fun (_x _min _max) (max _min (min _x _max)))) @@ -386,7 +393,7 @@ # @param _v0 lower bound # @param _v1 upper bound # =begin -# (print (lerp 0.22 15 132)) # 40.74 +# (print (math:lerp 0.22 15 132)) # 40.74 # =end # @author https://github.com/SuperFola (let lerp (fun (_x _v0 _v1) (+ (* (- 1 _x) _v0) (* _x _v1)))) @@ -396,7 +403,7 @@ # @param _v1 vector 1 # @param _v2 vector 2 # =begin -# (print (dotProduct [1 2 3] [4 5 6])) # 32 +# (print (math:dotProduct [1 2 3] [4 5 6])) # 32 # =end # @author https://github.com/SuperFola (let dotProduct (fun (_v1 _v2) { diff --git a/README.md b/README.md index 386f83f..e5e9ffc 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,4 @@ python -m arkdoc . --html out cd out && python -m http.server ``` -Then, open your webbrowser to go to `http://localhost:8000/`. +Then, open your browser to go to `http://localhost:8000/`. diff --git a/Random.ark b/Random.ark index 0cee811..ad38f18 100644 --- a/Random.ark +++ b/Random.ark @@ -2,8 +2,8 @@ # @details If the list is empty, returns nil # @param _L list of elements # =begin -# (import std.Random) -# (print (random:choice [1 2 3])) +# (let data [1 2 3 4 5]) +# (print (random:choice data)) # =end # @author https://github.com/SuperFola (let choice (fun (_L) @@ -17,7 +17,6 @@ # @details The original list is not modified # @param _L list to shuffle # =begin -# (import std.Random) # (let data [1 2 3 4 5]) # (let randomized (random:shuffle data)) # =end diff --git a/Range.ark b/Range.ark index b4d6bf3..5e2cc0b 100644 --- a/Range.ark +++ b/Range.ark @@ -3,7 +3,7 @@ # @param _b the end of the range # @details Has a field `asList` to compute a list from the current state of the range, and another one `reset`. # =begin -# (let obj (range 1 10)) +# (let obj (range:range 1 10)) # (print (obj.asList)) # [1 2 3 4 5 6 7 8 9] # (while (not (nil? (obj))) # (print obj.i)) # print the current element @@ -42,8 +42,8 @@ # @param _f the function # @details The range is unmodified. # =begin -# (let obj (range 1 10)) -# (forEach obj (fun (e) (print e))) +# (let obj (range:range 1 10)) +# (range:forEach obj (fun (e) (print e))) # =end # @author https://github.com/SuperFola (let forEach (fun (_r _f) { @@ -59,8 +59,9 @@ # @param _fun the filter function # @details The range is unmodified. # =begin -# (let obj (range 1 10)) -# (print (filter obj math:even)) # [2 4 6 8] +# (import std.Math) +# (let obj (range:range 1 10)) +# (print (range:filter obj math:even)) # [2 4 6 8] # =end # @author https://github.com/SuperFola (let filter (fun (_range _fun) { @@ -79,8 +80,8 @@ # @param _fun the function to apply # @details The range is unmodified. # =begin -# (let obj (range 1 10)) -# (print (map obj (fun (e) (* e e)))) # [1 4 9 16 25 36 49 64 81] +# (let obj (range:range 1 10)) +# (print (range:map obj (fun (e) (* e e)))) # [1 4 9 16 25 36 49 64 81] # =end # @author https://github.com/SuperFola (let map (fun (_range _fun) { @@ -98,8 +99,8 @@ # @param _fun the reduction function # @details The range is unmodified. # =begin -# (let obj (range 1 10)) -# (print (reduce obj (fun (e) (+ e e)))) # 45 +# (let obj (range:range 1 10)) +# (print (range:reduce obj (fun (e) (+ e e)))) # 45 # =end # @author https://github.com/SuperFola (let reduce (fun (_range _fun) { diff --git a/String.ark b/String.ark index be06a4e..54896fe 100644 --- a/String.ark +++ b/String.ark @@ -54,8 +54,8 @@ # @param substr the substring to search for # @param startIndex index to start searching from # =begin -# (string:find "hello hello" "hello" 1) # 6 -# (string:find "hello world" "aworld" 0) # -1 +# (string:findAfter "hello hello" "hello" 1) # 6 +# (string:findAfter "hello world" "aworld" 0) # -1 # =end # @author https://github.com/SuperFola (let findAfter (fun (_str _sub _after) (builtin__string:find _str _sub _after))) @@ -100,6 +100,28 @@ # @author https://github.com/SuperFola (let setAt (fun (_str _index _x) (builtin__string:setAt _str _index _x))) +# @brief Check if a string is empty or only consists of whitespaces +# @param _str the string to check +# =begin +# (print (string:emtpyOrWhitespace? "hello")) # false +# (print (string:emtpyOrWhitespace? " \t")) # true +# =end +# @author https://github.com/SuperFola +(let emptyOrWhitespace? (fun (_str) + (if (empty? _str) + true + { + (mut _i 0) + (mut _all_spaces true) + (let _len (len _str)) + (while (< _i _len) { + (let _c (@ _str _i)) + (if (and (!= _c " ") (!= _c "\t") (!= _c "\n") (!= _c "\r")) { + (set _all_spaces false) + (set _i _len) }) + (set _i (+ 1 _i)) }) + _all_spaces }))) + # @brief Count the number of non-overlapping occurrences of a word in a string # @param _str string to search into # @param _word word to count occurrences of @@ -119,9 +141,8 @@ # @param _string the string to make lowercase # @details The original string is left unmodified. # =begin -# (import std.String) # (let message "HeLLo World, I like cheese") -# (let new (toLower message)) # => hello world, i like cheese +# (let new (string:toLower message)) # => hello world, i like cheese # =end # @author https://github.com/SuperFola (let toLower (fun (text) { @@ -144,9 +165,8 @@ # @param _string the string to make uppercase # @details The original string is left unmodified. # =begin -# (import std.String) # (let message "hello world, I like cheese") -# (let new (toUpper message)) # => HELLO WORLD, I LIKE CHEESE +# (let new (string:toUpper message)) # => HELLO WORLD, I LIKE CHEESE # =end # @author https://github.com/SuperFola (let toUpper (fun (_string) { @@ -169,9 +189,8 @@ # @param _string the string to reverse # @details The original string is left unmodified. # =begin -# (import std.String) # (let message "hello world, I like goats") -# (let reversed (reverse message)) # => staog ekil I ,dlrow olleh +# (let reversed (string:reverse message)) # => staog ekil I ,dlrow olleh # =end # @author https://github.com/Natendrtfm (let reverse (fun (_string) { @@ -187,7 +206,6 @@ # @param _string string to repeat # @param _count number of times to repeat said string # =begin -# (import std.String) # (print (string:repeat "a" 5)) # aaaaa # =end # @author https://github.com/SuperFola @@ -207,9 +225,8 @@ # @param _length the length of the slice # @details The original string is left unmodified. Example: # =begin -# (import std.String) # (let message "hello world, I like goats") -# (let slice (slice message 6 4)) # => worl +# (let slice (string:slice message 6 4)) # => worl # =end # @author https://github.com/Natendrtfm (let slice (fun (_string _startingIndex _length) @@ -236,9 +253,8 @@ # @param _separator the separator to use for splitting # @details Returns a list of strings. Example : # =begin -# (import std.String) # (let message "hello world, I like boats") -# (let splitted (split message " ")) +# (let splitted (string:split message " ")) # =end # @author https://github.com/Natendrtfm (let split (fun (_string _separator) { @@ -277,9 +293,8 @@ # @param _new string who must replace the pattern # @details The original string isn't modified. # =begin -# (import std.String) # (let message "hello XXX, do you like the name XXX?") -# (print (replace message "XXX" "Harry")) # hello Harry, do you like the name Harry? +# (print (string:replace message "XXX" "Harry")) # hello Harry, do you like the name Harry? # =end (let replace (fun (_string _pattern _new) { (mut _out _string) @@ -301,9 +316,8 @@ # @param _delim a string delimiter to be put between each element # @details The original list isn't modified # =begin -# (import std.String) # (let data [1 "hello" 3.14 true "world"]) -# (print (join data ";")) # 1;hello;3.14;true;world +# (print (string:join data ";")) # 1;hello;3.14;true;world # =end (let join (fun (_list _delim) { (mut _output "") @@ -311,14 +325,62 @@ (while (< _index (len _list)) { (set _output (+ - _output - (toString (@ _list _index)) - (if (!= _index (- (len _list) 1)) - _delim - ""))) + _output + (toString (@ _list _index)) + (if (!= _index (- (len _list) 1)) + _delim + ""))) (set _index (+ 1 _index)) }) _output })) +# _dir = 1 -> left to right, -1 -> right to left +(let __strip_in_dir (fun (_str _dir) + (if (emptyOrWhitespace? _str) + "" + { + (mut _i + (if (= 1 _dir) + 0 + (- (len _str) 1))) + (mut _whitespaces (or (= (@ _str _i) " ") (= (@ _str _i) "\t") (= (@ _str _i) "\n") (= (@ _str _i) "\r"))) + + (while (and _whitespaces (< _i (len _str)) (>= _i 0)) { + (let _c (@ _str _i)) + (if (and (!= _c " ") (!= _c "\t") (!= _c "\n") (!= _c "\r")) + (set _whitespaces false) + (set _i (+ _dir _i))) }) + + (if (= 1 _dir) + (slice _str _i (len _str)) + (slice _str 0 (+ 1 _i))) }))) + +# @brief Removes whitespaces from the left side of a string +# @details The original string isn't modified +# @param _str string to sanitize +# =begin +# (print (string:lstrip " a b c")) # "a b c" +# =end +# @author https://github.com/SuperFola +(let lstrip (fun (_str) (__strip_in_dir _str 1))) + +# @brief Removes whitespaces from the right side of a string +# @details The original string isn't modified +# @param _str string to sanitize +# =begin +# (print (string:rstrip " a b c ")) # " a b c" +# =end +# @author https://github.com/SuperFola +(let rstrip (fun (_str) (__strip_in_dir _str -1))) + +# @brief Removes whitespaces from both sides of a string +# @details The original string isn't modified +# @param _str string to sanitize +# =begin +# (print (string:strip " a b c ")) # "a b c" +# =end +# @author https://github.com/SuperFola +(let strip (fun (_str) (rstrip (lstrip _str)))) + # @brief Strip the margin of a multiline string # @param _str multiline string, margin is (space)*(|) # =begin diff --git a/Testing.ark b/Testing.ark index f2e2217..f4aef5f 100644 --- a/Testing.ark +++ b/Testing.ark @@ -237,7 +237,7 @@ # @param _body body of the test, a begin block # =begin # (test:suite name { -# (_suite.toggle_display_cases_success true) # default: false, when true, display all the cases names on success and failures +# (testing:_suite.toggle_display_cases_success true) # default: false, when true, display all the cases names on success and failures # (test:eq 6 (my_function 1 2 3)) # (test:eq 128 (* 8 16))}) # =end diff --git a/os.ark b/os.ark deleted file mode 100644 index 38852e9..0000000 --- a/os.ark +++ /dev/null @@ -1,13 +0,0 @@ -(import std.Sys :exec :platform) - -# @brief Returns the home dir of the current user -# @author https://github.com/Wafelack, https://github.com/SuperFola -(let home_dir (fun () - (if (or (= platform "Linux") (= platform "Mac OSX") (= platform "Unix")) - { - (let username (exec "whoami")) - - (if (= username "root\n") - "/root/" - (+ "/home/" username)) } - (if (= platform "Windows") (exec "echo|set /p=%userprofile%"))))) diff --git a/tests/dict-tests.ark b/tests/dict-tests.ark index a9db6df..8129543 100644 --- a/tests/dict-tests.ark +++ b/tests/dict-tests.ark @@ -44,6 +44,11 @@ (test:eq (dict:get d foo) "yes") (test:eq (dict:get d closure) foo) }) + (test:case "getOrElse" { + (test:eq (dict:getOrElse d "key" 5) "value") + (test:eq (dict:getOrElse d "non-existing-key" 5) 5) + (test:eq (dict:getOrElse empty "key" 0) 0) }) + (test:case "contains" { (test:expect (dict:contains d "key")) (test:expect (dict:contains d 5)) diff --git a/tests/list-tests.ark b/tests/list-tests.ark index 514e544..3565be7 100644 --- a/tests/list-tests.ark +++ b/tests/list-tests.ark @@ -30,7 +30,15 @@ (test:case "forEach" { (list:forEach a (fun (e) { # just assert we have something, basically it's just a while + @ - (test:neq e nil)})) }) + (test:neq e nil) })) }) + + (test:case "enumerate" { + (mut position 0) + (list:enumerate a (fun (idx e) { + (test:eq position idx) + (set position (+ 1 position)) + # just assert we have something, basically it's just a while + @ + (test:eq (type e) "Number") })) }) (test:case "product" { (test:eq (list:product b) (* 4 5 6)) @@ -118,13 +126,19 @@ (test:eq a [1 2 3]) (test:eq b [4 5 6]) - (test:case "forAll, any" { + (test:case "forAll, any, none" { (test:expect (list:forAll a (fun (e) (< e 4)))) (test:expect (not (list:forAll a (fun (e) (< e 2))))) (test:expect (list:forAll [] (fun (e) (= e 2)))) + (test:expect (list:any a (fun (e) (< e 2)))) (test:expect (not (list:any a (fun (e) (> e 8))))) - (test:expect (not (list:any [] (fun (e) (= e 8))))) }) + (test:expect (not (list:any [] (fun (e) (= e 8))))) + + (test:expect (list:none a (fun (e) (> e 10)))) + (test:expect (not (list:none a (fun (e) (< e 2))))) + (test:expect (list:none [] (fun (e) (= e 8)))) + (test:expect (list:none [4 5 6] (fun (e) (< e 3)))) }) (test:case "countIf" { (test:eq (list:countIf a (fun (e) (= 0 (mod e 2)))) 1) diff --git a/tests/math-tests.ark b/tests/math-tests.ark index 7e0a647..c822e75 100644 --- a/tests/math-tests.ark +++ b/tests/math-tests.ark @@ -43,6 +43,10 @@ (test:eq (math:max 1 2) 2) (test:eq (math:max 1 -2) 1) (test:eq (math:max 0.5 0.2) 0.5) + (test:eq (math:increment 1) 2) + (test:eq (math:increment -1) 0) + (test:eq (math:decrement 0) -1) + (test:eq (math:decrement 1) 0) (test:eq (math:pow 2 2) 4) (test:eq (math:pow 4 0.5) 2) (test:eq (math:clamp 5 0 2) 2) diff --git a/tests/string-tests.ark b/tests/string-tests.ark index ad89c3d..a02b899 100644 --- a/tests/string-tests.ark +++ b/tests/string-tests.ark @@ -9,6 +9,18 @@ (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:eq (string:emptyOrWhitespace? "") true) + (test:eq (string:emptyOrWhitespace? " ") true) + (test:eq (string:emptyOrWhitespace? " ") true) + (test:eq (string:emptyOrWhitespace? "\t") true) + (test:eq (string:emptyOrWhitespace? "\n") true) + (test:eq (string:emptyOrWhitespace? "\r") true) + (test:eq (string:emptyOrWhitespace? " \t\r\n") true) + (test:eq (string:emptyOrWhitespace? " \t a") false) + (test:eq (string:emptyOrWhitespace? "a") false) + (test:eq (string:emptyOrWhitespace? "a ") false) + (test:eq (string:emptyOrWhitespace? " a ") false) + (test:eq (builtin__string:ord "a") (string:ord "a")) (test:eq (builtin__string:chr 65) (string:chr 65)) @@ -49,6 +61,25 @@ (test:eq "hello" (string:join ["hello"] ";")) (test:eq "" (string:join [] ";;")) + (test:eq "abc" (string:lstrip "abc")) + (test:eq "abc" (string:lstrip " \t\n\rabc")) + (test:eq "abc \t\n\r" (string:lstrip "abc \t\n\r")) + (test:eq "" (string:lstrip "")) + (test:eq "" (string:lstrip " \t\n\r")) + + (test:eq "abc" (string:rstrip "abc")) + (test:eq "abc" (string:rstrip "abc \t\n\r")) + (test:eq " \t\n\rabc" (string:rstrip " \t\n\rabc")) + (test:eq "" (string:rstrip "")) + (test:eq "" (string:rstrip " \t\n\r")) + + (test:eq "abc" (string:strip "\t\n\r abc \t\n\r")) + (test:eq "" (string:strip "\t\n\r ")) + (test:eq "abc" (string:strip "abc")) + (test:eq "" (string:strip "")) + (test:eq "abc" (string:strip " \t\n\rabc")) + (test:eq "abc" (string:strip "abc \t\n\r")) + (test:eq "" (string:stripMargin "")) (test:eq " hello" (string:stripMargin " hello")) (test:eq "hello" (string:stripMargin " |hello"))