diff --git a/Exceptions.ark b/Exceptions.ark new file mode 100644 index 0000000..4079694 --- /dev/null +++ b/Exceptions.ark @@ -0,0 +1,45 @@ +### +# @meta Exceptions +# @brief throw takes a value as its argument and return it to be used by try +# --- +# (let error (throw "cannot divide by zero")) +# --- +# @param _x the value to return +# @author https://github.com/SuperFola +### +(let throw (fun (_x) + (fun (_injl _injr &_x) (_injl _x)) +)) + +### +# @meta Exceptions +# @brief return takes a value as its argument and return it to be used by try +# --- +# (let value (return (/ 1 x))) +# --- +# @param _x the value to return +# @author https://github.com/SuperFola +### +(let return (fun (_y) + (fun (_injl _injr &_y) (_injr _y)) +)) + +### +# @meta Exceptions +# @brief Takes a value either returned by throw or return and apply a given on it if it's an error or not +# --- +# (let invert (fun (x) +# (if (= x 0) +# (throw "cannot divide by zero") +# (return (/ 1 x))))) +# (try (invert 0) +# (fun (inverted) (print inverted)) +# (fun (err) (print err))) +# --- +# @param _either the value to test +# @param _continue the success handler +# @param _handle the error handler +# @author https://github.com/SuperFola +### +(let try (fun (_either _continue _handle) + (_either _handle _continue))) \ No newline at end of file diff --git a/Functional.ark b/Functional.ark new file mode 100644 index 0000000..27a775a --- /dev/null +++ b/Functional.ark @@ -0,0 +1,58 @@ +### +# @meta Functional +# @brief Compose function calls +# --- +# (let foo (fun (a) (* a a))) +# (let bar (fun (b) (+ b b))) +# (let composed (compose foo bar)) +# (print (composed 12)) # return value is (12 + 12) * (12 + 12) +# --- +# @param _f the first function +# @param _g the second function +# @author https://github.com/rstefanic +### +(let compose (fun (_f _g) + (fun (_y &_f &_g) (_f (_g _y))))) + +### +# @meta Functional +# @brief Take a value as its argument and return a function taking 2 arguments which will call the first function on the value +# --- +# (let val (left 12)) +# (val (fun (x) (print x " i am called")) (fun (x) (print x " i am NOT called"))) +# --- +# @param _x the value +# @author https://github.com/SuperFola +### +(let left (fun (_x) + (fun (_injl _injr &_x) (_injl _x)) +)) + +### +# @meta Functional +# @brief Take a value as its argument and return a function taking 2 arguments which will call the second function on the value +# --- +# (let val (right 12)) +# (val (fun (x) (print x " i am NOT called")) (fun (x) (print x " i am called"))) +# --- +# @param _y the value +# @author https://github.com/SuperFola +### +(let right (fun (_y) + (fun (_injl _injr &_y) (_injr _y)) +)) + +### +# @meta Functional +# @brief Flip the arguments of a function +# @details Returns a function taking 1 argument: the second argument of the function to flip +# --- +# (let foo (fun (a b) (- a b))) +# ((flip foo 14) 12) # will call (foo 12 14) instead of (foo 14 12) +# --- +# @param _f the function +# @param _a the first argument +# @author https://github.com/rstefanic +### +(let flip (fun (_f _a) + (fun (_b &_f &_a) (_f _b _a)))) \ No newline at end of file diff --git a/Functional/Compose.ark b/Functional/Compose.ark deleted file mode 100644 index 8a47746..0000000 --- a/Functional/Compose.ark +++ /dev/null @@ -1,2 +0,0 @@ -(let compose (fun (_f _g) - (fun (_y &_f &_g) (_f (_g _y))))) \ No newline at end of file diff --git a/Functional/Drop.ark b/Functional/Drop.ark deleted file mode 100644 index e52fbc5..0000000 --- a/Functional/Drop.ark +++ /dev/null @@ -1,9 +0,0 @@ -(let drop (fun (_n _L) { - (mut _index _n) - (mut _output []) - (while (< _index (len _L)) { - (set _output (append _output (@ _L _index))) - (set _index (+ 1 _index)) - }) - _output -})) \ No newline at end of file diff --git a/Functional/DropWhile.ark b/Functional/DropWhile.ark deleted file mode 100644 index 6f874ec..0000000 --- a/Functional/DropWhile.ark +++ /dev/null @@ -1,14 +0,0 @@ -(let dropWhile (fun (_f _L) { - (mut _index 0) - (mut _output []) - (mut _continue true) - - (while (and (< _index (len _L)) _continue) { - (if (_f (@ _L _index)) { - (set _index (+ 1 _index)) - (set _output (append _output (@ _L _index))) - } - (set _continue false)) - }) - _output -})) \ No newline at end of file diff --git a/Functional/Either.ark b/Functional/Either.ark deleted file mode 100644 index c0cb986..0000000 --- a/Functional/Either.ark +++ /dev/null @@ -1,8 +0,0 @@ -{ - (let left (fun (x) - (fun (_injl _injr &x) (_injl x)) - )) - (let right (fun (y) - (fun (_injl _injr &y) (_injr y)) - )) -} \ No newline at end of file diff --git a/Functional/Exceptions.ark b/Functional/Exceptions.ark deleted file mode 100644 index de5d943..0000000 --- a/Functional/Exceptions.ark +++ /dev/null @@ -1,8 +0,0 @@ -{ - (import "Either.ark") - - (let throw left) - (let return right) - (let try (fun (either continue handle) - (either handle continue))) -} \ No newline at end of file diff --git a/Functional/Filter.ark b/Functional/Filter.ark deleted file mode 100644 index ae71d35..0000000 --- a/Functional/Filter.ark +++ /dev/null @@ -1,11 +0,0 @@ -(let filter (fun (_f _L) { - (mut _index 0) - (mut _output []) - (while (< _index (len _L)) { - (if (_f (@ _L _index)) - (set _output (append _output (@ _L _index))) - nil) - (set _index (+ 1 _index)) - }) - _output -})) \ No newline at end of file diff --git a/Functional/Flip.ark b/Functional/Flip.ark deleted file mode 100644 index d1b78b8..0000000 --- a/Functional/Flip.ark +++ /dev/null @@ -1,2 +0,0 @@ -(let flip (fun (_f _a) - (fun (_b &_f &_a) (_f _b _a)))) \ No newline at end of file diff --git a/Functional/Functional.ark b/Functional/Functional.ark deleted file mode 100644 index cf9d01a..0000000 --- a/Functional/Functional.ark +++ /dev/null @@ -1,14 +0,0 @@ -{ - (import "Functional/Compose.ark") - (import "Functional/Drop.ark") - (import "Functional/DropWhile.ark") - (import "Functional/Either.ark") - (import "Functional/Filter.ark") - (import "Functional/Flip.ark") - (import "Functional/Map.ark") - (import "Functional/Reduce.ark") - (import "Functional/Take.ark") - (import "Functional/TakeWhile.ark") - (import "Functional/Unzip.ark") - (import "Functional/Zip.ark") -} \ No newline at end of file diff --git a/Functional/Map.ark b/Functional/Map.ark deleted file mode 100644 index 4d00c8d..0000000 --- a/Functional/Map.ark +++ /dev/null @@ -1,9 +0,0 @@ -(let map (fun (_f _L) { - (mut _index 0) - (mut _output []) - (while (< _index (len _L)) { - (set _output (append _output (_f (@ _L _index)))) - (set _index (+ 1 _index)) - }) - _output -})) \ No newline at end of file diff --git a/Functional/Reduce.ark b/Functional/Reduce.ark deleted file mode 100644 index 7dcc688..0000000 --- a/Functional/Reduce.ark +++ /dev/null @@ -1,9 +0,0 @@ -(let reduce (fun (function _L) { - (mut _index 1) - (mut _output (@ _L 0)) - (while (< _index (len _L)) { - (set _output (function _output (@ _L _index))) - (set _index (+ 1 _index)) - }) - _output -})) \ No newline at end of file diff --git a/Functional/Take.ark b/Functional/Take.ark deleted file mode 100644 index 077ca94..0000000 --- a/Functional/Take.ark +++ /dev/null @@ -1,9 +0,0 @@ -(let take (fun (_n _L) { - (mut _index 0) - (mut _output []) - (while (and (< _index _n) (< _index (len _L))) { - (set _output (append _output (@ _L _index))) - (set _index (+ 1 _index)) - }) - _output -})) \ No newline at end of file diff --git a/Functional/TakeWhile.ark b/Functional/TakeWhile.ark deleted file mode 100644 index d6e70ca..0000000 --- a/Functional/TakeWhile.ark +++ /dev/null @@ -1,15 +0,0 @@ -(let takeWhile (fun (_f _L) { - (mut _index 0) - (mut _output []) - (mut _continue true) - - (while (and (< _index (len _L)) _continue) { - (if (_f (@ _L _index)) { - (set _output (append _output (@ _L _index))) - (set _index (+ 1 _index)) - } - (set _continue false) - ) - }) - _output -})) \ No newline at end of file diff --git a/Functional/Unzip.ark b/Functional/Unzip.ark deleted file mode 100644 index 390670f..0000000 --- a/Functional/Unzip.ark +++ /dev/null @@ -1,13 +0,0 @@ -(let unzip (fun (_L) { - (let _m (len _L)) - (mut _list1 []) - (mut _list2 []) - (mut _index 0) - (while (< _index _m) { - (mut current (@ _L _index)) - (set _list1 (append _list1 (@ current 0))) - (set _list2 (append _list2 (@ current 1))) - (set _index (+ 1 _index)) - }) - [_list1 _list2] -})) \ No newline at end of file diff --git a/Functional/Zip.ark b/Functional/Zip.ark deleted file mode 100644 index f6ebb1c..0000000 --- a/Functional/Zip.ark +++ /dev/null @@ -1,14 +0,0 @@ -{ - (import "Math/Min.ark") - - (let zip (fun (_a _b) { - (let _m (min (len _a) (len _b) )) - (mut _c []) - (mut _index 0) - (while (< _index _m) { - (set _c (append _c [(@ _a _index) (@ _b _index)])) - (set _index (+ 1 _index)) - }) - _c - })) -} \ No newline at end of file diff --git a/List.ark b/List.ark new file mode 100644 index 0000000..a5ad143 --- /dev/null +++ b/List.ark @@ -0,0 +1,229 @@ +### +# @meta List +# @brief Iterate over a given list and run a given function on every element. +# @details The original list is left unmodified. Example: +# --- +# (import "List.ark") +# (let collection [1 2 5 12]) +# (let new (list:forEach collection (fun (element) { +# (print element) +# }))) +# --- +# @param _L the list to iterate over +# @param _func the function to call on each element +# @author https://github.com/SuperFola +### +(let list:forEach (fun (_L _func) { + (mut _index 0) + (while (< _index (len _L)) { + (mut _element (@ _L _index)) + (_func _element) + (set _index (+ 1 _index)) + }) +})) + +### +# @meta List +# @brief Iterate over a given list and multiply all the elements with the others. +# @details The original list is left unmodified. Example: +# --- +# (import "List.ark") +# (let collection [1 2 5 12]) +# (let p (list:product collection)) # => 120 +# --- +# @param _L the list to iterate over +# @author https://github.com/FrenchMasterSword +### +(let list:product (fun (_L) { + (mut _index 0) + (mut _output 1) + (while (< _index (len _L)) { + (set _output (* _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +### +# @meta List +# @brief Iterate over a given list and sum all the elements. +# @details The original list is left unmodified. Example: +# --- +# (import "List.ark") +# (let collection [1 2 5 12]) +# (let p (list:sum collection)) # => 20 +# --- +# @param _L the list to iterate over +# @author https://github.com/FrenchMasterSword +### +(let list:sum (fun (_L) { + (mut _index 0) + (mut _output 0) + (while (< _index (len _L)) { + (set _output (+ _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +### +# @meta List +# @brief Drop the first n elements of a list +# @details The original list is left unmodified. +# --- +# (let cool-stuff [1 2 3 4 5 6 7 8 9]) +# (print (list:drop cool-stuff 4)) # [5 6 7 8 9] +# --- +# @param _L the list to work on +# @param _n the number of elements to drop +# @author https://github.com/rstefanic +### +(let list:drop (fun (_L _n) { + (mut _index _n) + (mut _output []) + (while (< _index (len _L)) { + (set _output (append _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +### +# @meta List +# @brief Keep elements in a given list if they follow a predicate +# @details The original list is left unmodified. +# --- +# (import "Math.ark") +# (print (list:filter [1 2 3 4 5 6 7 8 9] math:even)) # [2 4 6 8] +# --- +# @param _L the list to work on +# @param _f the predicate +# @author https://github.com/rstefanic +### +(let list:filter (fun (_L _f) { + (mut _index 0) + (mut _output []) + (while (< _index (len _L)) { + (if (_f (@ _L _index)) + (set _output (append _output (@ _L _index)))) + (set _index (+ 1 _index)) + }) + _output +})) + +### +# @meta List +# @brief Applies a given function to each element of a list +# @details The original list is left unmodified. +# --- +# (print (list:map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81] +# --- +# @param _L the list to work on +# @param _f the function to apply to each element +# @author https://github.com/rstefanic +### +(let list:map (fun (_L _f) { + (mut _index 0) + (mut _output []) + (while (< _index (len _L)) { + (set _output (append _output (_f (@ _L _index)))) + (set _index (+ 1 _index)) + }) + _output +})) + +### +# @meta List +# @brief Apply a function to the elements of a list to reduce it +# @details The original list is left unmodified. +# --- +# (let cool [1 2 3 4 5 6 7 8 9]) +# (print (list:reduce cool (fun (a b) (+ a b)))) # 45 +# --- +# @param _L the list to work on +# @param _f the function to apply +# @author https://github.com/FrenchMasterSword +### +(let list:reduce (fun (_L _f) { + (mut _index 1) + (mut _output (@ _L 0)) + (while (< _index (len _L)) { + (set _output (_f _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +(import "Math.ark") # needed for (math:min a b) + +### +# @meta List +# @brief Take the first n elements of +# @details The original list is left unmodified. +# --- +# (print (list:take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4] +# --- +# @param _L the list to work on +# @param _n the number of elements to take +# @author https://github.com/rstefanic +### +(let list:take (fun (_L _n) { + (mut _index 0) + (mut _output []) + (set _n (math:min _n (len _L))) + + (while (< _index _n) { + (set _output (append _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +### +# @meta List +# @brief Unzip a list of [[a b] [c d]...] into [[a c ...] [b d ...]] +# @details The original list is left unmodified. +# --- +# (let zipped [[1 5] [2 6] [3 7] [4 8]]) +# (print (list:unzip zipped)) # [[1 2 3 4] [5 6 7 8]] +# --- +# @param _L the list to work on +# @author https://github.com/FrenchMasterSword +### +(let list:unzip (fun (_L) { + (let _m (len _L)) + (mut _list1 []) + (mut _list2 []) + (mut _index 0) + (while (< _index _m) { + (mut current (@ _L _index)) + (set _list1 (append _list1 (@ current 0))) + (set _list2 (append _list2 (@ current 1))) + (set _index (+ 1 _index)) + }) + [_list1 _list2] +})) + +### +# @meta List +# @brief Zip two lists into one: [1 2 3 4] and [5 6 7 8] will give [[1 5] [2 6] [3 7] [4 8]] +# @details The original lists are left unmodified. +# --- +# (let a [1 2 3 4]) +# (let b [5 6 7 8]) +# (print (list:zip a b)) # [[1 5] [2 6] [3 7] [4 8]] +# --- +# @param _a the first list to work on +# @param _b the second list to work on +# @author https://github.com/FrenchMasterSword +### +(let list:zip (fun (_a _b) { + (let _m (math:min (len _a) (len _b))) + (mut _c []) + (mut _index 0) + (while (< _index _m) { + (set _c (append _c [(@ _a _index) (@ _b _index)])) + (set _index (+ 1 _index)) + }) + _c +})) \ No newline at end of file diff --git a/List/ForEach.ark b/List/ForEach.ark deleted file mode 100644 index c71a557..0000000 --- a/List/ForEach.ark +++ /dev/null @@ -1,8 +0,0 @@ -(let forEach (fun (_L code) { - (mut _index 0) - (while (< _index (len _L)) { - (mut _element (@ _L _index)) - (code _element) - (set _index (+ 1 _index)) - }) -})) \ No newline at end of file diff --git a/List/Product.ark b/List/Product.ark deleted file mode 100644 index 2192cd1..0000000 --- a/List/Product.ark +++ /dev/null @@ -1,9 +0,0 @@ -(let product (fun (_L) { - (mut _index 0) - (mut _output 1) - (while (< _index (len _L)) { - (set _output (* _output (@ _L _index))) - (set _index (+ 1 _index)) - }) - _output -})) diff --git a/List/Sum.ark b/List/Sum.ark deleted file mode 100644 index 0f9aa9d..0000000 --- a/List/Sum.ark +++ /dev/null @@ -1,9 +0,0 @@ -(let sum (fun (_L) { - (mut _index 0) - (mut _output 0) - (while (< _index (len _L)) { - (set _output (+ _output (@ _L _index))) - (set _index (+ 1 _index)) - }) - _output -})) \ No newline at end of file diff --git a/Math.ark b/Math.ark new file mode 100644 index 0000000..1d8f959 --- /dev/null +++ b/Math.ark @@ -0,0 +1,65 @@ +### +# @meta Mathematics +# @brief Return the absolute value of a number +# @param _x the number to get the absolute value of +# @author https://github.com/rstefanic +### +(let math:abs (fun (_x) + (if (< _x 0) (* -1 _x) _x))) + +### +# @meta Mathematics +# @brief Return true if the number is even, false otherwise +# @param _n the number +# @author https://github.com/rstefanic +### +(let math:even (fun (_n) + (= 0 (mod _n 2)))) + +### +# @meta Mathematics +# @brief Return true if the number is odd, false otherwise +# @param _n the number +# @author https://github.com/rstefanic +### +(let math:odd (fun (_n) + (= 1 (math:abs (mod _n 2))))) + +### +# @meta Mathematics +# @brief Get the minimum between two numbers +# @param _a the first number +# @param _b the second number +# @author https://github.com/rstefanic +### +(let math:min (fun (_a _b) + (if (< _a _b) _a _b))) + +### +# @meta Mathematics +# @brief Get the maximum between two numbers +# @param _a the first number +# @param _b the second number +# @author https://github.com/rstefanic +### +(let math:max (fun (_a _b) + (if (> _a _b) _a _b))) + +### +# @meta Mathematics +# @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 +# @param _a the exponent +# @author https://github.com/SuperFola +### +(let math:pow (fun (_x _a) (math:exp (* _a (math:ln _x))))) + +### +# @meta Mathematics +# @brief Get the square root of a number +# @details Square roots can't be taken for negative numbers for obvious reasons. +# @param _x the number +# @author https://github.com/SuperFola +### +(let math:sqrt (fun (_x) (math:exp (* 0.5 (math:ln _x))))) \ No newline at end of file diff --git a/Math/Abs.ark b/Math/Abs.ark deleted file mode 100644 index 8229200..0000000 --- a/Math/Abs.ark +++ /dev/null @@ -1,5 +0,0 @@ -(let abs (fun (_x) - (if (< _x 0) - (* -1 _x) - _x - ))) \ No newline at end of file diff --git a/Math/Arithmetic.ark b/Math/Arithmetic.ark deleted file mode 100644 index 4507689..0000000 --- a/Math/Arithmetic.ark +++ /dev/null @@ -1,9 +0,0 @@ -{ - (import "Math/Abs.ark") - (import "Math/Even.ark") - (import "Math/Max.ark") - (import "Math/Min.ark") - (import "Math/Odd.ark") - (import "Math/Pow.ark") - (import "Math/Sqrt.ark") -} diff --git a/Math/Even.ark b/Math/Even.ark deleted file mode 100644 index bb8ab52..0000000 --- a/Math/Even.ark +++ /dev/null @@ -1,2 +0,0 @@ -(let even (fun (_n) - (= 0 (mod _n 2)))) \ No newline at end of file diff --git a/Math/Max.ark b/Math/Max.ark deleted file mode 100644 index 36ab955..0000000 --- a/Math/Max.ark +++ /dev/null @@ -1,5 +0,0 @@ -(let max (fun (_a _b) - (if (> _a _b) - _a - _b - ))) \ No newline at end of file diff --git a/Math/Min.ark b/Math/Min.ark deleted file mode 100644 index c8e90c2..0000000 --- a/Math/Min.ark +++ /dev/null @@ -1,5 +0,0 @@ -(let min (fun (_a _b) - (if (< _a _b) - _a - _b - ))) \ No newline at end of file diff --git a/Math/Odd.ark b/Math/Odd.ark deleted file mode 100644 index a8cfbe1..0000000 --- a/Math/Odd.ark +++ /dev/null @@ -1,6 +0,0 @@ -{ - (import "Math/Abs.ark") - - (let odd (fun (_n) - (= 1 (abs (mod _n 2))))) -} \ No newline at end of file diff --git a/Math/Pow.ark b/Math/Pow.ark deleted file mode 100644 index 044844c..0000000 --- a/Math/Pow.ark +++ /dev/null @@ -1 +0,0 @@ -(let pow (fun (x _a) (exp (* _a (ln x))))) \ No newline at end of file diff --git a/Math/Sqrt.ark b/Math/Sqrt.ark deleted file mode 100644 index 2a44508..0000000 --- a/Math/Sqrt.ark +++ /dev/null @@ -1 +0,0 @@ -(let sqrt (fun (x) (exp (* 0.5 (ln x))))) \ No newline at end of file diff --git a/Range.ark b/Range.ark index 15343f1..39e85c8 100644 --- a/Range.ark +++ b/Range.ark @@ -1,32 +1,132 @@ -{ - (let range (fun (_a _b) { - (let asList (fun () { - # _a and _b are going to be captured by the caller - (mut _output []) - (mut a_ _a) - (while (< a_ _b) { - (set _output (append _output a_)) - (set a_ (+ 1 a_)) - }) - _output - })) - - (fun (&_a &_b &asList) { - (if (< _a _b) { - (mut c _a) - (set _a (+ _a 1)) - c - } - nil - ) +### +# @meta Range +# @brief Create a ranged closure in interval [a, b[ +# @details Has a field `asList` to compute a list from the current state of the range, and another one `reset`. +# --- +# (let obj (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 +# (print (obj.asList)) # [], the range has been used +# (obj.reset) # the range is ready to be used again +# (print (obj.asList)) # [1 2 3 4 5 6 7 8 9] +# --- +# @param i the beginning of the range +# @param _b the end of the range +# @author https://github.com/SuperFola +### +(let range (fun (i _b) { + (let asList (fun () { + # i and _b are going to be captured by the caller + (mut _output []) + (mut a_ i) + (while (< a_ _b) { + (set _output (append _output a_)) + (set a_ (+ 1 a_)) }) + _output })) - (let forEachR (fun (r _f) { - (mut val (r)) - (while (= false (nil? val)) { - (_f val) - (set val (r)) - }) - })) -} \ No newline at end of file + (let _a i) + (let reset (fun () (set i _a))) + + (fun (&i &_a &_b &asList &reset) { + (if (< i _b) + { + (set i (+ i 1)) + (- i 1) + }) + }) +})) + +### +# @meta Range +# @brief Run a function on each element of the range +# @details The range is unmodified. +# --- +# (let obj (range 1 10)) +# (range:forEach obj (fun (e) (print e))) +# --- +# @param _r the range object +# @param _f the function +# @author https://github.com/SuperFola +### +(let range:forEach (fun (_r _f) { + (mut _val (_r)) + (while (not (nil? _val)) { + (_f _val) + (set _val (_r)) + }) + (_r.reset) +})) + +### +# @meta Range +# @brief Create a list based on a range and a filter function +# @details The range is unmodified. +# --- +# (let obj (range 1 10)) +# (print (range:filter obj math:even)) # [2 4 6 8] +# --- +# @param _range the range object +# @param _fun the filter function +# @author https://github.com/SuperFola +### +(let range:filter (fun (_range _fun) { + (mut _value (_range)) + (mut _output []) + (while (not (nil? _value)) { + (if (_fun _value) (set _output (append _output _value))) + (set _value (_range)) + }) + (_range.reset) + + _output +})) + +### +# @meta Range +# @brief Create a list based on a range and a function to apply to each elements +# @details The range is unmodified. +# --- +# (let obj (range 1 10)) +# (print (range:map obj (fun (e) (* e e)))) # [1 4 9 16 25 36 49 64 81] +# --- +# @param _range the range object +# @param _fun the function to apply +# @author https://github.com/SuperFola +### +(let range:map (fun (_range _fun) { + (mut _value (_range)) + (mut _output []) + (while (not (nil? _value)) { + (set _output (append _output (_fun _value))) + (set _value (_range)) + }) + (_range.reset) + + _output +})) + +### +# @meta Range +# @brief Create a reduced list based on a range and a reduction function +# @details The range is unmodified. +# --- +# (let obj (range 1 10)) +# (print (range:reduce obj (fun (e) (+ e e)))) # 45 +# --- +# @param _range the range object +# @param _fun the reduction function +# @author https://github.com/SuperFola +### +(let range:reduce (fun (_range _fun) { + (mut _output (_range)) + (mut _last (_range)) + (while (not (nil? _last)) { + (set _output (_fun _output _last)) + (set _last (_range)) + }) + (_range.reset) + _output +})) \ No newline at end of file diff --git a/String.ark b/String.ark new file mode 100644 index 0000000..616818e --- /dev/null +++ b/String.ark @@ -0,0 +1,252 @@ +### +# @meta String +# @brief Reverse a string. +# @details The original string is left unmodified. Example: +# --- +# (import "String.ark") +# (let message "HeLLo World, I like cheese") +# (let new (str:toLower message)) # => hello world, i like cheese +# --- +# @param _string the string to make lowercase +# @author https://github.com/Natendrtfm +### +(let str:toLower (fun (text) { + (mut _index 0) + (mut _e "") + (mut _output "") + (while (< _index (len text)) { + (set _e (@ text _index)) + #Conditions + (if (= _e "A") (set _e "a") + (if (= _e "B") (set _e "b") + (if (= _e "C") (set _e "c") + (if (= _e "D") (set _e "d") + (if (= _e "E") (set _e "e") + (if (= _e "F") (set _e "f") + (if (= _e "G") (set _e "g") + (if (= _e "H") (set _e "h") + (if (= _e "I") (set _e "i") + (if (= _e "J") (set _e "j") + (if (= _e "K") (set _e "k") + (if (= _e "L") (set _e "l") + (if (= _e "M") (set _e "m") + (if (= _e "N") (set _e "n") + (if (= _e "O") (set _e "o") + (if (= _e "P") (set _e "p") + (if (= _e "Q") (set _e "q") + (if (= _e "R") (set _e "r") + (if (= _e "S") (set _e "s") + (if (= _e "T") (set _e "t") + (if (= _e "U") (set _e "u") + (if (= _e "V") (set _e "v") + (if (= _e "W") (set _e "w") + (if (= _e "X") (set _e "x") + (if (= _e "Y") (set _e "y") + (if (= _e "Z") (set _e "z") + (if (= _e "Â") (set _e "â") + (if (= _e "À") (set _e "à") + (if (= _e "Á") (set _e "á") + (if (= _e "Ã") (set _e "ã") + (if (= _e "Ä") (set _e "ä") + (if (= _e "Å") (set _e "å") + (if (= _e "Æ") (set _e "æ") + (if (= _e "Ç") (set _e "ç") + (if (= _e "È") (set _e "è") + (if (= _e "É") (set _e "é") + (if (= _e "Ê") (set _e "ê") + (if (= _e "Ë") (set _e "ë") + (if (= _e "Ì") (set _e "ì") + (if (= _e "Í") (set _e "í") + (if (= _e "Î") (set _e "î") + (if (= _e "Ï") (set _e "ï") + (if (= _e "Ô") (set _e "ô") ()))))))))))))))))))))))))))))))))))))))))))) + # End conditions + (set _output (+ _output _e)) + (set _index (+ _index 1)) + }) + _output +})) + +### +# @meta String +# @brief Reverse a string. +# @details The original string is left unmodified. Example: +# --- +# (import "String.ark") +# (let message "hello world, I like cheese") +# (let new (str:toUpper message)) # => HELLO WORLD, I LIKE CHEESE +# --- +# @param _string the string to make uppercase +# @author https://github.com/Natendrtfm +### +(let str:toUpper (fun (_string) { + (mut _index 0) + (mut _e "") + (mut _output "") + (while (< _index (len _string)) { + (set _e (@ _string _index)) + #Conditions + (if (= _e "a") (set _e "A") + (if (= _e "b") (set _e "B") + (if (= _e "c") (set _e "C") + (if (= _e "d") (set _e "D") + (if (= _e "e") (set _e "E") + (if (= _e "f") (set _e "F") + (if (= _e "g") (set _e "G") + (if (= _e "h") (set _e "H") + (if (= _e "i") (set _e "I") + (if (= _e "j") (set _e "J") + (if (= _e "k") (set _e "K") + (if (= _e "l") (set _e "L") + (if (= _e "m") (set _e "M") + (if (= _e "n") (set _e "N") + (if (= _e "o") (set _e "O") + (if (= _e "p") (set _e "P") + (if (= _e "q") (set _e "Q") + (if (= _e "r") (set _e "R") + (if (= _e "s") (set _e "S") + (if (= _e "t") (set _e "T") + (if (= _e "u") (set _e "U") + (if (= _e "w") (set _e "W") + (if (= _e "v") (set _e "V") + (if (= _e "x") (set _e "X") + (if (= _e "y") (set _e "Y") + (if (= _e "z") (set _e "Z") + (if (= _e "a") (set _e "Â") + (if (= _e "à") (set _e "À") + (if (= _e "á") (set _e "Á") + (if (= _e "ã") (set _e "Ã") + (if (= _e "ä") (set _e "Ä") + (if (= _e "å") (set _e "Å") + (if (= _e "æ") (set _e "Æ") + (if (= _e "ç") (set _e "Ç") + (if (= _e "è") (set _e "È") + (if (= _e "é") (set _e "É") + (if (= _e "ê") (set _e "Ê") + (if (= _e "ë") (set _e "Ë") + (if (= _e "ì") (set _e "Ì") + (if (= _e "í") (set _e "Í") + (if (= _e "î") (set _e "Î") + (if (= _e "ï") (set _e "Ï") + (if (= _e "ô") (set _e "Ô") ()))))))))))))))))))))))))))))))))))))))))))) + #End of conditions + (set _output (+ _output _e)) + (set _index (+ _index 1)) + }) + _output +})) + +### +# @meta String +# @brief Reverse a string. +# @details The original string is left unmodified. Example: +# --- +# (import "String.ark") +# (let message "hello world, I like goats") +# (let reversed (str:reverse message)) # => staog ekil I ,dlrow olleh +# --- +# @param _string the string to reverse +# @author https://github.com/Natendrtfm +### +(let str:reverse (fun (_string) { + (mut _index (- (len _string) 1)) + (mut _returnedString "") + (while (> _index -1) { + (set _returnedString (+ _returnedString (@ _string _index))) + (set _index (- _index 1)) + }) + _returnedString +})) + +### +# @meta String +# @brief Get a slice of a given string, from a given index with a given length +# @details The original string is left unmodified. Example: +# --- +# (import "String.ark") +# (let message "hello world, I like goats") +# (let slice (str:slice message 6 4)) # => worl +# --- +# @param _string the string to get a slice of +# @param _startingIndex the index in the string where to start slicing +# @param _length the length of the slice +# @author https://github.com/Natendrtfm +### +(let str:slice (fun (_string _startingIndex _length) + (if (= _length 0) + "" + { + (assert (and (>= _startingIndex 0) (< _startingIndex (len _string))) "slice start index must be in range [0, string length[") + + (mut _returnedString "") + (mut _index _startingIndex) + (let _end (if (> _length (len _string)) (len _string) (+ _index _length))) + + (while (< _index _end) { + (set _returnedString (+ _returnedString (@ _string _index))) + (set _index (+ _index 1)) + }) + _returnedString + }) +)) + +### +# @meta String +# @brief Split a string in multiple substrings in a list, given a separator (single character) +# @details Returns a list of strings. Example : +# --- +# (import "String.ark") +# (let message "hello world, I like boats") +# (let splitted (str:split message " ")) +# --- +# @param _string the string to split +# @param _separator the separator to use for splitting (single char) +# @author https://github.com/Natendrtfm +### +(let str:split (fun (_string _separator) { + (assert (!= "" _separator) "Separator of split can not be empty") + (assert (= 1 (len _separator)) "Separator length must be equal to 1") + (mut _index 0) + (mut _word "") + (mut _letter "") + (mut _output []) + + (while (< _index (len _string)) { + (set _letter (@ _string _index)) + (if (= _letter _separator) + { + (set _output (append _output _word)) + (set _word "") + } + (set _word (+ _word _letter))) + (set _index (+ _index 1)) + }) + (if (empty? _word) + _output + (append _output _word)) +})) + +### +# @meta String +# @brief Replace a substring in a given string +# @details The original string isn't modified. Example: +# --- +# (import "String.ark") +# (let message "hello XXX, do you like the name XXX?") +# (print (str:replace message "XXX" "Harry")) # hello Harry, do you like the name Harry? +# --- +### +(let str:replace (fun (_string _pattern _new) { + (mut _out _string) + (mut _idx (str:find _out _pattern)) + (let _pattern_sz (len _pattern)) + + (while (!= -1 _idx) { + (set _out (+ + (str:slice _out 0 _idx) + _new + (str:slice _out (+ _idx _pattern_sz) (- (len _out) (+ _idx _pattern_sz))))) + (set _idx (str:find _out _pattern)) + }) + _out +})) \ No newline at end of file diff --git a/String/Case.ark b/String/Case.ark deleted file mode 100644 index 162fef0..0000000 --- a/String/Case.ark +++ /dev/null @@ -1,115 +0,0 @@ -{ - # Made with <3 by Natend (Natendrtfm on github) - (let toLowerCase (fun (text) { - (mut _index 0) - (mut _e "") - (mut _output "") - (while (< _index (len text)) { - (set _e (@ text _index)) - #Conditions - (if (= _e "A") (set _e "a") - (if (= _e "B") (set _e "b") - (if (= _e "C") (set _e "c") - (if (= _e "D") (set _e "d") - (if (= _e "E") (set _e "e") - (if (= _e "F") (set _e "f") - (if (= _e "G") (set _e "g") - (if (= _e "H") (set _e "h") - (if (= _e "I") (set _e "i") - (if (= _e "J") (set _e "j") - (if (= _e "K") (set _e "k") - (if (= _e "L") (set _e "l") - (if (= _e "M") (set _e "m") - (if (= _e "N") (set _e "n") - (if (= _e "O") (set _e "o") - (if (= _e "P") (set _e "p") - (if (= _e "Q") (set _e "q") - (if (= _e "R") (set _e "r") - (if (= _e "S") (set _e "s") - (if (= _e "T") (set _e "t") - (if (= _e "U") (set _e "u") - (if (= _e "V") (set _e "v") - (if (= _e "W") (set _e "w") - (if (= _e "X") (set _e "x") - (if (= _e "Z") (set _e "z") - (if (= _e "Â") (set _e "â") - (if (= _e "À") (set _e "à") - (if (= _e "Á") (set _e "á") - (if (= _e "Ã") (set _e "ã") - (if (= _e "Ä") (set _e "ä") - (if (= _e "Å") (set _e "å") - (if (= _e "Æ") (set _e "æ") - (if (= _e "Ç") (set _e "ç") - (if (= _e "È") (set _e "è") - (if (= _e "É") (set _e "é") - (if (= _e "Ê") (set _e "ê") - (if (= _e "Ë") (set _e "ë") - (if (= _e "Ì") (set _e "ì") - (if (= _e "Í") (set _e "í") - (if (= _e "Î") (set _e "î") - (if (= _e "Ï") (set _e "ï") - (if (= _e "Ô") (set _e "ô") ())))))))))))))))))))))))))))))))))))))))))) - # End conditions - (set _output (+ _output _e)) - (set _index (+ _index 1)) - }) - _output - })) - - (let toUpperCase (fun (text) { - (mut _index 0) - (mut _e "") - (mut _output "") - (while (< _index (len text)) { - (set _e (@ text _index)) - #Conditions - (if (= _e "a") (set _e "A") - (if (= _e "b") (set _e "B") - (if (= _e "c") (set _e "C") - (if (= _e "d") (set _e "D") - (if (= _e "e") (set _e "E") - (if (= _e "f") (set _e "F") - (if (= _e "g") (set _e "G") - (if (= _e "h") (set _e "H") - (if (= _e "i") (set _e "I") - (if (= _e "j") (set _e "J") - (if (= _e "k") (set _e "K") - (if (= _e "l") (set _e "L") - (if (= _e "m") (set _e "M") - (if (= _e "n") (set _e "N") - (if (= _e "o") (set _e "O") - (if (= _e "p") (set _e "P") - (if (= _e "q") (set _e "Q") - (if (= _e "r") (set _e "R") - (if (= _e "s") (set _e "S") - (if (= _e "t") (set _e "T") - (if (= _e "u") (set _e "U") - (if (= _e "w") (set _e "W") - (if (= _e "v") (set _e "V") - (if (= _e "x") (set _e "X") - (if (= _e "y") (set _e "Y") - (if (= _e "z") (set _e "Z") - (if (= _e "a") (set _e "Â") - (if (= _e "à") (set _e "À") - (if (= _e "á") (set _e "Á") - (if (= _e "ã") (set _e "Ã") - (if (= _e "ä") (set _e "Ä") - (if (= _e "å") (set _e "Å") - (if (= _e "æ") (set _e "Æ") - (if (= _e "ç") (set _e "Ç") - (if (= _e "è") (set _e "È") - (if (= _e "é") (set _e "É") - (if (= _e "ê") (set _e "Ê") - (if (= _e "ë") (set _e "Ë") - (if (= _e "ì") (set _e "Ì") - (if (= _e "í") (set _e "Í") - (if (= _e "î") (set _e "Î") - (if (= _e "ï") (set _e "Ï") - (if (= _e "ô") (set _e "Ô") ()))))))))))))))))))))))))))))))))))))))))))) - #End of conditions - (set _output (+ _output _e)) - (set _index (+ _index 1)) - }) - _output - })) -} \ No newline at end of file diff --git a/String/Reverse.ark b/String/Reverse.ark deleted file mode 100644 index 8e5d98a..0000000 --- a/String/Reverse.ark +++ /dev/null @@ -1,10 +0,0 @@ -# made by Natendrtfm (on Github) with <3 -(let reverseStr (fun (inputed){ - (mut _index (- (len inputed) 1)) - (mut returnedString "") - (while (> _index -1){ - (set returnedString (+ returnedString (@ inputed _index))) - (set _index (- _index 1)) - }) - returnedString -})) \ No newline at end of file diff --git a/String/Slice.ark b/String/Slice.ark deleted file mode 100644 index 775404f..0000000 --- a/String/Slice.ark +++ /dev/null @@ -1,15 +0,0 @@ -# made by Natendrtfm (on Github) with <3 -(let sliceStr (fun (inputed startingIndex length) { - (assert (>= length 1) "slice length must be greater or equal to 1") - (assert (and (>= startingIndex 0) (< startingIndex (len inputed))) "slice start index must be in range [0, string length[") - - (mut returnedString "") - (mut _index startingIndex) - (set length (if (> length (len inputed)) (len inputed) length)) - - (while (< _index length){ - (set returnedString (+ returnedString (@ inputed _index))) - (set _index (+ _index 1)) - }) - returnedString -})) diff --git a/String/Split.ark b/String/Split.ark deleted file mode 100644 index b17adbb..0000000 --- a/String/Split.ark +++ /dev/null @@ -1,21 +0,0 @@ -(let split (fun (_string _separator) { - (assert (!= "" _separator) "Separator of split can not be empty") - (assert (= 1 (len _separator)) "Separator length must be equal to 1") - (mut _index 0) - (mut _word "") - (mut _letter "") - (mut _output []) - - (while (< _index (len _string)) { - (set _letter (@ _string _index)) - (if (= _letter _separator) { - (set _output (append _output _word)) - (set _word "") - } - (set _word (+ _word _letter))) - (set _index (+ _index 1)) - }) - (if (empty? _word) - _output - (append _output _word)) -})) \ No newline at end of file diff --git a/Switch.ark b/Switch.ark index 390fb91..e1592d3 100644 --- a/Switch.ark +++ b/Switch.ark @@ -1,16 +1,29 @@ -(let switch (fun (value _tests) { - (mut acc 0) - (let end (len _tests)) +### +# @meta Switch +# @brief Takes a value to match against a list of [possible values, function to run if it matched] +# @details Once the value is matched, it stops and doesn't try any other values. +# --- +# (switch 12 [ +# [1 (fun () (print "the value is one"))] +# [12 '(print "quoted code blocks also works")] +# [12 '{ (let b "ok") (print b ", quoted begin blocks work as well") }] +# ]) +# --- +# @param +# @author https://github.com/SuperFola +### +(let switch (fun (_value _tests) { + (mut _acc 0) + (let _end (len _tests)) - (while (!= acc end) { - (mut r (@ _tests acc)) - (mut bis (@ r 0)) - (if (= bis value) { - ((@ r 1)) - (set acc (- end 1)) - } - () - ) - (set acc (+ 1 acc)) + (while (!= _acc _end) { + (mut _r (@ _tests _acc)) + (mut _bis (@ _r 0)) + (if (= _bis _value) + { + ((@ _r 1)) + (set _acc (- _end 1)) + }) + (set _acc (+ 1 _acc)) }) })) \ No newline at end of file