From 405a92586fd4171cd14c8e17e0a983f9d4dedd06 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Wed, 24 Jun 2020 14:58:25 +0200 Subject: [PATCH 01/12] merging files and adding documentation --- Functional.ark | 0 List.ark | 67 ++++++++++++++ List/ForEach.ark | 8 -- List/Product.ark | 9 -- List/Sum.ark | 9 -- Math.ark | 0 String.ark | 223 +++++++++++++++++++++++++++++++++++++++++++++ String/Case.ark | 115 ----------------------- String/Reverse.ark | 10 -- String/Slice.ark | 15 --- String/Split.ark | 21 ----- 11 files changed, 290 insertions(+), 187 deletions(-) create mode 100644 Functional.ark create mode 100644 List.ark delete mode 100644 List/ForEach.ark delete mode 100644 List/Product.ark delete mode 100644 List/Sum.ark create mode 100644 Math.ark create mode 100644 String.ark delete mode 100644 String/Case.ark delete mode 100644 String/Reverse.ark delete mode 100644 String/Slice.ark delete mode 100644 String/Split.ark diff --git a/Functional.ark b/Functional.ark new file mode 100644 index 0000000..e69de29 diff --git a/List.ark b/List.ark new file mode 100644 index 0000000..26da58e --- /dev/null +++ b/List.ark @@ -0,0 +1,67 @@ +# (([List]index) +# !List +# \forEach Iterate over a given list and run a given function on every element. +# \forEach The original list is left unmodified. Example: +# ` +# (import "List.ark") +# (let collection [1 2 5 12]) +# (let new (forEach collection (fun (element) { +# (print element) +# }))) +# ` +# @_L the list to iterate over +# @_func the function to call on each element +# author: https://github.com/SuperFola +# ) +(let forEach (fun (_L _func) { + (mut _index 0) + (while (< _index (len _L)) { + (mut _element (@ _L _index)) + (_func _element) + (set _index (+ 1 _index)) + }) +})) + +# (([List]index) +# !List +# \product Iterate over a given list and multiply all the elements with the others. +# \product The original list is left unmodified. Example: +# ` +# (import "List.ark") +# (let collection [1 2 5 12]) +# (let p (product collection)) # => 120 +# ` +# @_L the list to iterate over +# author: https://github.com/FrenchMasterSword +# ) +(let product (fun (_L) { + (mut _index 0) + (mut _output 1) + (while (< _index (len _L)) { + (set _output (* _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +# (([List]index) +# !List +# \sum Iterate over a given list and sum all the elements. +# \sum The original list is left unmodified. Example: +# ` +# (import "List.ark") +# (let collection [1 2 5 12]) +# (let p (sum collection)) # => 20 +# ` +# @_L the list to iterate over +# author: https://github.com/FrenchMasterSword +# ) +(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/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..e69de29 diff --git a/String.ark b/String.ark new file mode 100644 index 0000000..faf8cba --- /dev/null +++ b/String.ark @@ -0,0 +1,223 @@ +# (([String]index) +# !String +# \toLowerCase Reverse a string. +# \toLowerCase The original string is left unmodified. Example: +# ` +# (import "String.ark") +# (let message "HeLLo World, I like cheese") +# (let new (toLowerCase message)) # => hello world, i like cheese +# ` +# @_string the string to make lowercase +# author: https://github.com/Natendrtfm +# ) +(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 +})) + +# (([String]index) +# !String +# \toUpperCase Reverse a string. +# \toUpperCase The original string is left unmodified. Example: +# ` +# (import "String.ark") +# (let message "hello world, I like cheese") +# (let new (toUpperCase message)) # => HELLO WORLD, I LIKE CHEESE +# ` +# @_string the string to make uppercase +# author: https://github.com/Natendrtfm +# ) +(let toUpperCase (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 +})) + +# (([String]index) +# !String +# \reverseStr Reverse a string. +# \reverseStr The original string is left unmodified. Example: +# ` +# (import "String.ark") +# (let message "hello world, I like goats") +# (let reversed (reverseStr message)) # => staog ekil I ,dlrow olleh +# ` +# @_string the string to reverse +# author: https://github.com/Natendrtfm +# ) +(let reverseStr (fun (_string) { + (mut _index (- (len _string) 1)) + (mut returnedString "") + (while (> _index -1) { + (set returnedString (+ returnedString (@ _string _index))) + (set _index (- _index 1)) + }) + returnedString +})) + +# (([String]index) +# !String +# \sliceStr Get a slice of a given string, from a given index with a given length +# \sliceStr The original string is left unmodified. Example: +# ` +# (import "String.ark") +# (let message "hello world, I like goats") +# (let slice (sliceStr message 6 4)) # => worl +# ` +# @_string the string to get a slice of +# @_startingIndex the index in the string where to start slicing +# @_length the length of the slice +# author: https://github.com/Natendrtfm +# ) +(let sliceStr (fun (_string _startingIndex _length) { + (assert (>= _length 1) "slice length must be greater or equal to 1") + (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 +})) + +# (([String]index) +# !String +# \split Split a string in multiple substrings in a list, given a separator (single character) +# \split Returns a list of strings. Example : +# ` +# (import "String.ark") +# (let message "hello world, I like boats") +# (let splitted (split message " ")) +# ` +# @_string the string to split +# @_separator the separator to use for splitting (single char) +# author: https://github.com/Natendrtfm +# ) +(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/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 From 06501059eabf18db2a98253e8887665b8df6fe08 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Tue, 30 Jun 2020 16:29:39 +0200 Subject: [PATCH 02/12] renaming functions, merging files, adding documentation --- Exceptions.ark | 6 ++ Functional.ark | 14 ++++ Functional/Compose.ark | 2 - Functional/Drop.ark | 9 --- Functional/DropWhile.ark | 14 ---- Functional/Either.ark | 8 --- Functional/Exceptions.ark | 8 --- Functional/Filter.ark | 11 --- Functional/Flip.ark | 2 - Functional/Functional.ark | 14 ---- Functional/Map.ark | 9 --- Functional/Reduce.ark | 9 --- Functional/Take.ark | 9 --- Functional/TakeWhile.ark | 15 ---- Functional/Unzip.ark | 13 ---- Functional/Zip.ark | 14 ---- List.ark | 142 ++++++++++++++++++++++++++++++++++---- Math.ark | 25 +++++++ Math/Abs.ark | 5 -- Math/Arithmetic.ark | 9 --- Math/Even.ark | 2 - Math/Max.ark | 5 -- Math/Min.ark | 5 -- Math/Odd.ark | 6 -- Math/Pow.ark | 1 - Math/Sqrt.ark | 1 - Range.ark | 54 +++++++-------- String.ark | 46 ++++++------ Switch.ark | 23 +++--- 29 files changed, 234 insertions(+), 247 deletions(-) create mode 100644 Exceptions.ark delete mode 100644 Functional/Compose.ark delete mode 100644 Functional/Drop.ark delete mode 100644 Functional/DropWhile.ark delete mode 100644 Functional/Either.ark delete mode 100644 Functional/Exceptions.ark delete mode 100644 Functional/Filter.ark delete mode 100644 Functional/Flip.ark delete mode 100644 Functional/Functional.ark delete mode 100644 Functional/Map.ark delete mode 100644 Functional/Reduce.ark delete mode 100644 Functional/Take.ark delete mode 100644 Functional/TakeWhile.ark delete mode 100644 Functional/Unzip.ark delete mode 100644 Functional/Zip.ark delete mode 100644 Math/Abs.ark delete mode 100644 Math/Arithmetic.ark delete mode 100644 Math/Even.ark delete mode 100644 Math/Max.ark delete mode 100644 Math/Min.ark delete mode 100644 Math/Odd.ark delete mode 100644 Math/Pow.ark delete mode 100644 Math/Sqrt.ark diff --git a/Exceptions.ark b/Exceptions.ark new file mode 100644 index 0000000..7650846 --- /dev/null +++ b/Exceptions.ark @@ -0,0 +1,6 @@ +(import "Functional.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.ark b/Functional.ark index e69de29..859c444 100644 --- a/Functional.ark +++ b/Functional.ark @@ -0,0 +1,14 @@ +# @author: https://github.com/rstefanic +(let compose (fun (_f _g) + (fun (_y &_f &_g) (_f (_g _y))))) + +(let left (fun (_x) + (fun (_injl _injr &_x) (_injl _x)) +)) +(let right (fun (_y) + (fun (_injl _injr &_y) (_injr _y)) +)) + +# @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 index 26da58e..7670ba0 100644 --- a/List.ark +++ b/List.ark @@ -1,11 +1,11 @@ # (([List]index) # !List -# \forEach Iterate over a given list and run a given function on every element. -# \forEach The original list is left unmodified. Example: +# \list:forEach Iterate over a given list and run a given function on every element. +# \list:forEach The original list is left unmodified. Example: # ` # (import "List.ark") # (let collection [1 2 5 12]) -# (let new (forEach collection (fun (element) { +# (let new (list:forEach collection (fun (element) { # (print element) # }))) # ` @@ -13,7 +13,7 @@ # @_func the function to call on each element # author: https://github.com/SuperFola # ) -(let forEach (fun (_L _func) { +(let list:forEach (fun (_L _func) { (mut _index 0) (while (< _index (len _L)) { (mut _element (@ _L _index)) @@ -24,17 +24,17 @@ # (([List]index) # !List -# \product Iterate over a given list and multiply all the elements with the others. -# \product The original list is left unmodified. Example: +# \list:product Iterate over a given list and multiply all the elements with the others. +# \list:product The original list is left unmodified. Example: # ` # (import "List.ark") # (let collection [1 2 5 12]) -# (let p (product collection)) # => 120 +# (let p (list:product collection)) # => 120 # ` # @_L the list to iterate over # author: https://github.com/FrenchMasterSword # ) -(let product (fun (_L) { +(let list:product (fun (_L) { (mut _index 0) (mut _output 1) (while (< _index (len _L)) { @@ -46,17 +46,17 @@ # (([List]index) # !List -# \sum Iterate over a given list and sum all the elements. -# \sum The original list is left unmodified. Example: +# \list:sum Iterate over a given list and sum all the elements. +# \list:sum The original list is left unmodified. Example: # ` # (import "List.ark") # (let collection [1 2 5 12]) -# (let p (sum collection)) # => 20 +# (let p (list:sum collection)) # => 20 # ` # @_L the list to iterate over # author: https://github.com/FrenchMasterSword # ) -(let sum (fun (_L) { +(let list:sum (fun (_L) { (mut _index 0) (mut _output 0) (while (< _index (len _L)) { @@ -64,4 +64,122 @@ (set _index (+ 1 _index)) }) _output +})) + +# @author: https://github.com/rstefanic +(let list:drop (fun (_n _L) { + (mut _index _n) + (mut _output []) + (while (< _index (len _L)) { + (set _output (append _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +# @author: https://github.com/rstefanic +(let list: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 +})) + +# @author: https://github.com/rstefanic +(let list:filter (fun (_f _L) { + (mut _index 0) + (mut _output []) + (while (< _index (len _L)) { + (if (_f (@ _L _index)) + (set _output (append _output (@ _L _index)))) + (set _index (+ 1 _index)) + }) + _output +})) + +# @author: https://github.com/rstefanic +(let list: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 +})) + +# @author: https://github.com/FrenchMasterSword +(let list:reduce (fun (_f _L) { + (mut _index 1) + (mut _output (@ _L 0)) + (while (< _index (len _L)) { + (set _output (_f _output (@ _L _index))) + (set _index (+ 1 _index)) + }) + _output +})) + +# @author: https://github.com/rstefanic +(let list: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 +})) + +# @author: https://github.com/rstefanic +(let list: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 +})) + +# @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] +})) + +(import "Math.ark") # needed for (math:min a b) + +# @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/Math.ark b/Math.ark index e69de29..dfb2af9 100644 --- a/Math.ark +++ b/Math.ark @@ -0,0 +1,25 @@ +# @author: https://github.com/rstefanic +(let math:abs (fun (_x) + (if (< _x 0) (* -1 _x) _x))) + +# @author: https://github.com/rstefanic +(let math:even (fun (_n) + (= 0 (mod _n 2)))) + +# @author: https://github.com/rstefanic +(let math:odd (fun (_n) + (= 1 (math:abs (mod _n 2))))) + +# @author: https://github.com/rstefanic +(let math:min (fun (_a _b) + (if (< _a _b) _a _b))) + +# @author: https://github.com/rstefanic +(let math:max (fun (_a _b) + (if (> _a _b) _a _b))) + +# @author: https://github.com/SuperFola +(let math:pow (fun (x _a) (exp (* _a (ln x))))) + +# @author: https://github.com/SuperFola +(let math:sqrt (fun (x) (exp (* 0.5 (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..5966919 100644 --- a/Range.ark +++ b/Range.ark @@ -1,32 +1,28 @@ -{ - (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 - ) +(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 })) - (let forEachR (fun (r _f) { - (mut val (r)) - (while (= false (nil? val)) { - (_f val) - (set val (r)) - }) - })) -} \ No newline at end of file + (fun (&_a &_b &asList) { + (if (< _a _b) + { + (set _a (+ _a 1)) + (- _a 1) + }) + }) +})) + +(let forEachR (fun (r _f) { + (mut _val (r)) + (while (not (nil?_ val)) { + (_f _val) + (set _val (r)) + }) +})) \ No newline at end of file diff --git a/String.ark b/String.ark index faf8cba..0fb56b5 100644 --- a/String.ark +++ b/String.ark @@ -1,16 +1,16 @@ # (([String]index) # !String -# \toLowerCase Reverse a string. -# \toLowerCase The original string is left unmodified. Example: +# \string:toLower Reverse a string. +# \string:toLower The original string is left unmodified. Example: # ` # (import "String.ark") # (let message "HeLLo World, I like cheese") -# (let new (toLowerCase message)) # => hello world, i like cheese +# (let new (string:toLower message)) # => hello world, i like cheese # ` # @_string the string to make lowercase # author: https://github.com/Natendrtfm # ) -(let toLowerCase (fun (text) { +(let string:toLower (fun (text) { (mut _index 0) (mut _e "") (mut _output "") @@ -68,17 +68,17 @@ # (([String]index) # !String -# \toUpperCase Reverse a string. -# \toUpperCase The original string is left unmodified. Example: +# \string:toUpper Reverse a string. +# \string:toUpper The original string is left unmodified. Example: # ` # (import "String.ark") # (let message "hello world, I like cheese") -# (let new (toUpperCase message)) # => HELLO WORLD, I LIKE CHEESE +# (let new (string:toUpper message)) # => HELLO WORLD, I LIKE CHEESE # ` # @_string the string to make uppercase # author: https://github.com/Natendrtfm # ) -(let toUpperCase (fun (_string) { +(let string:toUpper (fun (_string) { (mut _index 0) (mut _e "") (mut _output "") @@ -137,41 +137,41 @@ # (([String]index) # !String -# \reverseStr Reverse a string. -# \reverseStr The original string is left unmodified. Example: +# \string:reverse Reverse a string. +# \string:reverse The original string is left unmodified. Example: # ` # (import "String.ark") # (let message "hello world, I like goats") -# (let reversed (reverseStr message)) # => staog ekil I ,dlrow olleh +# (let reversed (string:reverse message)) # => staog ekil I ,dlrow olleh # ` # @_string the string to reverse # author: https://github.com/Natendrtfm # ) -(let reverseStr (fun (_string) { +(let string:reverse (fun (_string) { (mut _index (- (len _string) 1)) - (mut returnedString "") + (mut _returnedString "") (while (> _index -1) { - (set returnedString (+ returnedString (@ _string _index))) + (set _returnedString (+ _returnedString (@ _string _index))) (set _index (- _index 1)) }) - returnedString + _returnedString })) # (([String]index) # !String -# \sliceStr Get a slice of a given string, from a given index with a given length -# \sliceStr The original string is left unmodified. Example: +# \string:slice Get a slice of a given string, from a given index with a given length +# \string:slice The original string is left unmodified. Example: # ` # (import "String.ark") # (let message "hello world, I like goats") -# (let slice (sliceStr message 6 4)) # => worl +# (let slice (string:slice message 6 4)) # => worl # ` # @_string the string to get a slice of # @_startingIndex the index in the string where to start slicing # @_length the length of the slice # author: https://github.com/Natendrtfm # ) -(let sliceStr (fun (_string _startingIndex _length) { +(let string:slice (fun (_string _startingIndex _length) { (assert (>= _length 1) "slice length must be greater or equal to 1") (assert (and (>= _startingIndex 0) (< _startingIndex (len _string))) "slice start index must be in range [0, string length[") @@ -188,18 +188,18 @@ # (([String]index) # !String -# \split Split a string in multiple substrings in a list, given a separator (single character) -# \split Returns a list of strings. Example : +# \string:split Split a string in multiple substrings in a list, given a separator (single character) +# \string:split Returns a list of strings. Example : # ` # (import "String.ark") # (let message "hello world, I like boats") -# (let splitted (split message " ")) +# (let splitted (string:split message " ")) # ` # @_string the string to split # @_separator the separator to use for splitting (single char) # author: https://github.com/Natendrtfm # ) -(let split (fun (_string _separator) { +(let string: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) diff --git a/Switch.ark b/Switch.ark index 390fb91..28c1a02 100644 --- a/Switch.ark +++ b/Switch.ark @@ -1,16 +1,15 @@ (let switch (fun (value _tests) { - (mut acc 0) - (let end (len _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 From e924898087d8831fee53f6cadb2ac6a2f1c79998 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Fri, 17 Jul 2020 14:37:56 +0200 Subject: [PATCH 03/12] swapping parameters around in list.ark (list first is more logicial) ; adding functionalities to ranges --- List.ark | 14 +++++++------- Range.ark | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/List.ark b/List.ark index 7670ba0..576c526 100644 --- a/List.ark +++ b/List.ark @@ -67,7 +67,7 @@ })) # @author: https://github.com/rstefanic -(let list:drop (fun (_n _L) { +(let list:drop (fun (_L _n) { (mut _index _n) (mut _output []) (while (< _index (len _L)) { @@ -78,7 +78,7 @@ })) # @author: https://github.com/rstefanic -(let list:dropWhile (fun (_f _L) { +(let list:dropWhile (fun (_L _f) { (mut _index 0) (mut _output []) (mut _continue true) @@ -94,7 +94,7 @@ })) # @author: https://github.com/rstefanic -(let list:filter (fun (_f _L) { +(let list:filter (fun (_L _f) { (mut _index 0) (mut _output []) (while (< _index (len _L)) { @@ -106,7 +106,7 @@ })) # @author: https://github.com/rstefanic -(let list:map (fun (_f _L) { +(let list:map (fun (_L _f) { (mut _index 0) (mut _output []) (while (< _index (len _L)) { @@ -117,7 +117,7 @@ })) # @author: https://github.com/FrenchMasterSword -(let list:reduce (fun (_f _L) { +(let list:reduce (fun (_L _f) { (mut _index 1) (mut _output (@ _L 0)) (while (< _index (len _L)) { @@ -128,7 +128,7 @@ })) # @author: https://github.com/rstefanic -(let list:take (fun (_n _L) { +(let list:take (fun (_L _n) { (mut _index 0) (mut _output []) (while (and (< _index _n) (< _index (len _L))) { @@ -139,7 +139,7 @@ })) # @author: https://github.com/rstefanic -(let list:takeWhile (fun (_f _L) { +(let list:takeWhile (fun (_L _f) { (mut _index 0) (mut _output []) (mut _continue true) diff --git a/Range.ark b/Range.ark index 5966919..e41002c 100644 --- a/Range.ark +++ b/Range.ark @@ -19,10 +19,42 @@ }) })) -(let forEachR (fun (r _f) { - (mut _val (r)) +(let range:forEach (fun (_r _f) { + (mut _val (_r)) (while (not (nil?_ val)) { (_f _val) - (set _val (r)) + (set _val (_r)) }) +})) + +(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)) + }) + + _output +})) + +(let range:map (fun (_range _fun) { + (mut _value (_range)) + (mut _output []) + (while (not (nil? _value)) { + (set _output (append _output (_fun _value))) + (set _value (_range)) + }) + + _output +})) + +(let range:reduce (fun (_range _fun) { + (mut _output (_range)) + (mut _last (_range)) + (while (not (nil? _last)) { + (set _output (_fun _output _last)) + (set _last (_range)) + }) + _output })) \ No newline at end of file From 7253499c0361180bccce43596b6510d7df7f7b91 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Tue, 28 Jul 2020 22:55:28 +0200 Subject: [PATCH 04/12] fixing naming --- Math.ark | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Math.ark b/Math.ark index dfb2af9..96a4779 100644 --- a/Math.ark +++ b/Math.ark @@ -19,7 +19,7 @@ (if (> _a _b) _a _b))) # @author: https://github.com/SuperFola -(let math:pow (fun (x _a) (exp (* _a (ln x))))) +(let math:pow (fun (x _a) (math:exp (* _a (math:ln x))))) # @author: https://github.com/SuperFola -(let math:sqrt (fun (x) (exp (* 0.5 (ln x))))) \ No newline at end of file +(let math:sqrt (fun (x) (math:exp (* 0.5 (math:ln x))))) \ No newline at end of file From 41580adbfb2664f9b4fea1d5a866066d10e3be37 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Fri, 4 Sep 2020 17:54:10 +0200 Subject: [PATCH 05/12] working on the documentation --- Exceptions.ark | 45 ++++++++++++++- Functional.ark | 48 +++++++++++++++- List.ark | 146 ++++++++++++++++++++++++++++++++++++------------- String.ark | 96 ++++++++++++++++---------------- Switch.ark | 18 +++++- 5 files changed, 261 insertions(+), 92 deletions(-) diff --git a/Exceptions.ark b/Exceptions.ark index 7650846..65734ec 100644 --- a/Exceptions.ark +++ b/Exceptions.ark @@ -1,6 +1,45 @@ -(import "Functional.ark") +### +# @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)) +)) -(let throw left) -(let return right) +### +# @meta Exceptions +# @brief returns 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 index 859c444..efcf106 100644 --- a/Functional.ark +++ b/Functional.ark @@ -1,14 +1,58 @@ -# @author: https://github.com/rstefanic +### +# @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)) )) -# @author: https://github.com/rstefanic +### +# @meta Functional +# @brief flip 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/List.ark b/List.ark index 576c526..775915a 100644 --- a/List.ark +++ b/List.ark @@ -1,18 +1,18 @@ -# (([List]index) -# !List -# \list:forEach Iterate over a given list and run a given function on every element. -# \list:forEach The original list is left unmodified. Example: -# ` +### +# @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) # }))) -# ` -# @_L the list to iterate over -# @_func the function to call on each element -# author: https://github.com/SuperFola -# ) +# --- +# @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)) { @@ -22,18 +22,18 @@ }) })) -# (([List]index) -# !List -# \list:product Iterate over a given list and multiply all the elements with the others. -# \list:product The original list is left unmodified. Example: -# ` +### +# @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 -# ` -# @_L the list to iterate over -# author: https://github.com/FrenchMasterSword -# ) +# --- +# @param _L the list to iterate over +# @author https://github.com/FrenchMasterSword +### (let list:product (fun (_L) { (mut _index 0) (mut _output 1) @@ -44,18 +44,18 @@ _output })) -# (([List]index) -# !List -# \list:sum Iterate over a given list and sum all the elements. -# \list:sum The original list is left unmodified. Example: -# ` +### +# @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 -# ` -# @_L the list to iterate over -# author: https://github.com/FrenchMasterSword -# ) +# --- +# @param _L the list to iterate over +# @author https://github.com/FrenchMasterSword +### (let list:sum (fun (_L) { (mut _index 0) (mut _output 0) @@ -66,7 +66,15 @@ _output })) -# @author: https://github.com/rstefanic +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/rstefanic +### (let list:drop (fun (_L _n) { (mut _index _n) (mut _output []) @@ -77,7 +85,15 @@ _output })) -# @author: https://github.com/rstefanic +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/rstefanic +### (let list:dropWhile (fun (_L _f) { (mut _index 0) (mut _output []) @@ -93,7 +109,15 @@ _output })) -# @author: https://github.com/rstefanic +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/rstefanic +### (let list:filter (fun (_L _f) { (mut _index 0) (mut _output []) @@ -105,7 +129,15 @@ _output })) -# @author: https://github.com/rstefanic +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/rstefanic +### (let list:map (fun (_L _f) { (mut _index 0) (mut _output []) @@ -116,7 +148,15 @@ _output })) -# @author: https://github.com/FrenchMasterSword +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/FrenchMasterSword +### (let list:reduce (fun (_L _f) { (mut _index 1) (mut _output (@ _L 0)) @@ -127,7 +167,15 @@ _output })) -# @author: https://github.com/rstefanic +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/rstefanic +### (let list:take (fun (_L _n) { (mut _index 0) (mut _output []) @@ -138,7 +186,15 @@ _output })) -# @author: https://github.com/rstefanic +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/rstefanic +### (let list:takeWhile (fun (_L _f) { (mut _index 0) (mut _output []) @@ -155,7 +211,15 @@ _output })) -# @author: https://github.com/FrenchMasterSword +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the list to work on +# @author https://github.com/FrenchMasterSword +### (let list:unzip (fun (_L) { (let _m (len _L)) (mut _list1 []) @@ -172,7 +236,15 @@ (import "Math.ark") # needed for (math:min a b) -# @author: https://github.com/FrenchMasterSword +### +# @meta List +# @brief +# @details +# --- +# --- +# @param _L the 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 []) diff --git a/String.ark b/String.ark index 0fb56b5..73b4e57 100644 --- a/String.ark +++ b/String.ark @@ -1,15 +1,15 @@ -# (([String]index) -# !String -# \string:toLower Reverse a string. -# \string:toLower The original string is left unmodified. Example: -# ` +### +# @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 (string:toLower message)) # => hello world, i like cheese -# ` -# @_string the string to make lowercase -# author: https://github.com/Natendrtfm -# ) +# --- +# @param _string the string to make lowercase +# @author https://github.com/Natendrtfm +### (let string:toLower (fun (text) { (mut _index 0) (mut _e "") @@ -66,18 +66,18 @@ _output })) -# (([String]index) -# !String -# \string:toUpper Reverse a string. -# \string:toUpper The original string is left unmodified. Example: -# ` +### +# @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 (string:toUpper message)) # => HELLO WORLD, I LIKE CHEESE -# ` -# @_string the string to make uppercase -# author: https://github.com/Natendrtfm -# ) +# --- +# @param _string the string to make uppercase +# @author https://github.com/Natendrtfm +### (let string:toUpper (fun (_string) { (mut _index 0) (mut _e "") @@ -135,18 +135,18 @@ _output })) -# (([String]index) -# !String -# \string:reverse Reverse a string. -# \string:reverse The original string is left unmodified. Example: -# ` +### +# @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 (string:reverse message)) # => staog ekil I ,dlrow olleh -# ` -# @_string the string to reverse -# author: https://github.com/Natendrtfm -# ) +# --- +# @param _string the string to reverse +# @author https://github.com/Natendrtfm +### (let string:reverse (fun (_string) { (mut _index (- (len _string) 1)) (mut _returnedString "") @@ -157,20 +157,20 @@ _returnedString })) -# (([String]index) -# !String -# \string:slice Get a slice of a given string, from a given index with a given length -# \string:slice The original string is left unmodified. Example: -# ` +### +# @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 (string:slice message 6 4)) # => worl -# ` -# @_string the string to get a slice of -# @_startingIndex the index in the string where to start slicing -# @_length the length of the slice -# author: https://github.com/Natendrtfm -# ) +# --- +# @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 string:slice (fun (_string _startingIndex _length) { (assert (>= _length 1) "slice length must be greater or equal to 1") (assert (and (>= _startingIndex 0) (< _startingIndex (len _string))) "slice start index must be in range [0, string length[") @@ -186,19 +186,19 @@ _returnedString })) -# (([String]index) -# !String -# \string:split Split a string in multiple substrings in a list, given a separator (single character) -# \string:split Returns a list of strings. Example : -# ` +### +# @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 (string:split message " ")) -# ` -# @_string the string to split -# @_separator the separator to use for splitting (single char) -# author: https://github.com/Natendrtfm -# ) +# --- +# @param _string the string to split +# @param _separator the separator to use for splitting (single char) +# @author https://github.com/Natendrtfm +### (let string:split (fun (_string _separator) { (assert (!= "" _separator) "Separator of split can not be empty") (assert (= 1 (len _separator)) "Separator length must be equal to 1") diff --git a/Switch.ark b/Switch.ark index 28c1a02..684db6b 100644 --- a/Switch.ark +++ b/Switch.ark @@ -1,11 +1,25 @@ -(let switch (fun (value _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) + (if (= _bis _value) { ((@ _r 1)) (set _acc (- _end 1)) From 8c0ceeccba99fc521810ddf5cdafaac82270c9ed Mon Sep 17 00:00:00 2001 From: SuperFola Date: Fri, 4 Sep 2020 18:01:13 +0200 Subject: [PATCH 06/12] adding template for documentation, will need to fill them up --- Math.ark | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------- Range.ark | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/Math.ark b/Math.ark index 96a4779..2905821 100644 --- a/Math.ark +++ b/Math.ark @@ -1,25 +1,74 @@ -# @author: https://github.com/rstefanic +### +# @meta Mathematics +# @brief +# --- +# --- +# @param +# @author https://github.com/rstefanic +### (let math:abs (fun (_x) (if (< _x 0) (* -1 _x) _x))) -# @author: https://github.com/rstefanic +### +# @meta Mathematics +# @brief +# --- +# --- +# @param +# @author https://github.com/rstefanic +### (let math:even (fun (_n) (= 0 (mod _n 2)))) -# @author: https://github.com/rstefanic +### +# @meta Mathematics +# @brief +# --- +# --- +# @param +# @author https://github.com/rstefanic +### (let math:odd (fun (_n) (= 1 (math:abs (mod _n 2))))) -# @author: https://github.com/rstefanic +### +# @meta Mathematics +# @brief +# --- +# --- +# @param +# @author https://github.com/rstefanic +### (let math:min (fun (_a _b) (if (< _a _b) _a _b))) -# @author: https://github.com/rstefanic +### +# @meta Mathematics +# @brief +# --- +# --- +# @param +# @author https://github.com/rstefanic +### (let math:max (fun (_a _b) (if (> _a _b) _a _b))) -# @author: https://github.com/SuperFola +### +# @meta Mathematics +# @brief +# --- +# --- +# @param +# @author https://github.com/SuperFola +### (let math:pow (fun (x _a) (math:exp (* _a (math:ln x))))) -# @author: https://github.com/SuperFola +### +# @meta Mathematics +# @brief +# --- +# --- +# @param +# @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/Range.ark b/Range.ark index e41002c..858a262 100644 --- a/Range.ark +++ b/Range.ark @@ -1,3 +1,12 @@ +### +# @meta Range +# @brief +# @details +# --- +# --- +# @param +# @author https://github.com/SuperFola +### (let range (fun (_a _b) { (let asList (fun () { # _a and _b are going to be captured by the caller @@ -19,6 +28,15 @@ }) })) +### +# @meta Range +# @brief +# @details +# --- +# --- +# @param +# @author https://github.com/SuperFola +### (let range:forEach (fun (_r _f) { (mut _val (_r)) (while (not (nil?_ val)) { @@ -27,6 +45,15 @@ }) })) +### +# @meta Range +# @brief +# @details +# --- +# --- +# @param +# @author https://github.com/SuperFola +### (let range:filter (fun (_range _fun) { (mut _value (_range)) (mut _output []) @@ -38,6 +65,15 @@ _output })) +### +# @meta Range +# @brief +# @details +# --- +# --- +# @param +# @author https://github.com/SuperFola +### (let range:map (fun (_range _fun) { (mut _value (_range)) (mut _output []) @@ -49,6 +85,15 @@ _output })) +### +# @meta Range +# @brief +# @details +# --- +# --- +# @param +# @author https://github.com/SuperFola +### (let range:reduce (fun (_range _fun) { (mut _output (_range)) (mut _last (_range)) From f5e131aa3fa587656c9946f7220111bcba516244 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Sat, 5 Sep 2020 14:13:03 +0200 Subject: [PATCH 07/12] adding documentation for List.ark, dropping dropWhile an takeWhile --- List.ark | 106 ++++++++++++++++++++----------------------------------- 1 file changed, 39 insertions(+), 67 deletions(-) diff --git a/List.ark b/List.ark index 775915a..a5ad143 100644 --- a/List.ark +++ b/List.ark @@ -68,11 +68,14 @@ ### # @meta List -# @brief -# @details +# @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) { @@ -87,35 +90,14 @@ ### # @meta List -# @brief -# @details -# --- -# --- -# @param _L the list to work on -# @author https://github.com/rstefanic -### -(let list:dropWhile (fun (_L _f) { - (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 -})) - -### -# @meta List -# @brief -# @details +# @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) { @@ -131,11 +113,13 @@ ### # @meta List -# @brief -# @details +# @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) { @@ -150,11 +134,14 @@ ### # @meta List -# @brief -# @details +# @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) { @@ -167,19 +154,25 @@ _output })) +(import "Math.ark") # needed for (math:min a b) + ### # @meta List -# @brief -# @details +# @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 []) - (while (and (< _index _n) (< _index (len _L))) { + (set _n (math:min _n (len _L))) + + (while (< _index _n) { (set _output (append _output (@ _L _index))) (set _index (+ 1 _index)) }) @@ -188,34 +181,11 @@ ### # @meta List -# @brief -# @details -# --- -# --- -# @param _L the list to work on -# @author https://github.com/rstefanic -### -(let list:takeWhile (fun (_L _f) { - (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 -})) - -### -# @meta List -# @brief -# @details +# @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 @@ -234,15 +204,17 @@ [_list1 _list2] })) -(import "Math.ark") # needed for (math:min a b) - ### # @meta List -# @brief -# @details +# @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 _L the list to work on +# @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) { From b5e28e02b72e920a0996b9b02e01f1329df559f1 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Sat, 5 Sep 2020 14:13:30 +0200 Subject: [PATCH 08/12] updating documentations with uppercase letter at the begining of each sentence --- Exceptions.ark | 4 ++-- Functional.ark | 10 +++++----- Switch.ark | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Exceptions.ark b/Exceptions.ark index 65734ec..4079694 100644 --- a/Exceptions.ark +++ b/Exceptions.ark @@ -13,7 +13,7 @@ ### # @meta Exceptions -# @brief returns takes a value as its argument and return it to be used by try +# @brief return takes a value as its argument and return it to be used by try # --- # (let value (return (/ 1 x))) # --- @@ -26,7 +26,7 @@ ### # @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 +# @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) diff --git a/Functional.ark b/Functional.ark index efcf106..27a775a 100644 --- a/Functional.ark +++ b/Functional.ark @@ -1,6 +1,6 @@ ### # @meta Functional -# @brief compose function calls +# @brief Compose function calls # --- # (let foo (fun (a) (* a a))) # (let bar (fun (b) (+ b b))) @@ -16,7 +16,7 @@ ### # @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 +# @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"))) @@ -30,7 +30,7 @@ ### # @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 +# @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"))) @@ -44,8 +44,8 @@ ### # @meta Functional -# @brief flip arguments of a function -# @details returns a function taking 1 argument: the second argument of the function to flip +# @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) diff --git a/Switch.ark b/Switch.ark index 684db6b..e1592d3 100644 --- a/Switch.ark +++ b/Switch.ark @@ -1,7 +1,7 @@ ### # @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 +# @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"))] From 1ddb38e41f42d7b4ac57dfbc92d05c05022a0cfb Mon Sep 17 00:00:00 2001 From: SuperFola Date: Sat, 5 Sep 2020 14:20:13 +0200 Subject: [PATCH 09/12] adding Math.ark --- Math.ark | 51 +++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/Math.ark b/Math.ark index 2905821..1d8f959 100644 --- a/Math.ark +++ b/Math.ark @@ -1,9 +1,7 @@ ### # @meta Mathematics -# @brief -# --- -# --- -# @param +# @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) @@ -11,10 +9,8 @@ ### # @meta Mathematics -# @brief -# --- -# --- -# @param +# @brief Return true if the number is even, false otherwise +# @param _n the number # @author https://github.com/rstefanic ### (let math:even (fun (_n) @@ -22,10 +18,8 @@ ### # @meta Mathematics -# @brief -# --- -# --- -# @param +# @brief Return true if the number is odd, false otherwise +# @param _n the number # @author https://github.com/rstefanic ### (let math:odd (fun (_n) @@ -33,10 +27,9 @@ ### # @meta Mathematics -# @brief -# --- -# --- -# @param +# @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) @@ -44,10 +37,9 @@ ### # @meta Mathematics -# @brief -# --- -# --- -# @param +# @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) @@ -55,20 +47,19 @@ ### # @meta Mathematics -# @brief -# --- -# --- -# @param +# @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))))) +(let math:pow (fun (_x _a) (math:exp (* _a (math:ln _x))))) ### # @meta Mathematics -# @brief -# --- -# --- -# @param +# @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 +(let math:sqrt (fun (_x) (math:exp (* 0.5 (math:ln _x))))) \ No newline at end of file From 85b0297f9fbabdf41e3035c046d342dd7eefcbb0 Mon Sep 17 00:00:00 2001 From: SuperFola Date: Sat, 5 Sep 2020 14:36:02 +0200 Subject: [PATCH 10/12] adding documentation for range --- Range.ark | 73 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/Range.ark b/Range.ark index 858a262..39e85c8 100644 --- a/Range.ark +++ b/Range.ark @@ -1,17 +1,25 @@ ### # @meta Range -# @brief -# @details +# @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 +# @param i the beginning of the range +# @param _b the end of the range # @author https://github.com/SuperFola ### -(let range (fun (_a _b) { +(let range (fun (i _b) { (let asList (fun () { - # _a and _b are going to be captured by the caller + # i and _b are going to be captured by the caller (mut _output []) - (mut a_ _a) + (mut a_ i) (while (< a_ _b) { (set _output (append _output a_)) (set a_ (+ 1 a_)) @@ -19,39 +27,49 @@ _output })) - (fun (&_a &_b &asList) { - (if (< _a _b) + (let _a i) + (let reset (fun () (set i _a))) + + (fun (&i &_a &_b &asList &reset) { + (if (< i _b) { - (set _a (+ _a 1)) - (- _a 1) + (set i (+ i 1)) + (- i 1) }) }) })) ### # @meta Range -# @brief -# @details +# @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 +# @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)) { + (while (not (nil? _val)) { (_f _val) (set _val (_r)) }) + (_r.reset) })) ### # @meta Range -# @brief -# @details +# @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 +# @param _range the range object +# @param _fun the filter function # @author https://github.com/SuperFola ### (let range:filter (fun (_range _fun) { @@ -61,17 +79,21 @@ (if (_fun _value) (set _output (append _output _value))) (set _value (_range)) }) + (_range.reset) _output })) ### # @meta Range -# @brief -# @details +# @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 +# @param _range the range object +# @param _fun the function to apply # @author https://github.com/SuperFola ### (let range:map (fun (_range _fun) { @@ -81,17 +103,21 @@ (set _output (append _output (_fun _value))) (set _value (_range)) }) + (_range.reset) _output })) ### # @meta Range -# @brief -# @details +# @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 +# @param _range the range object +# @param _fun the reduction function # @author https://github.com/SuperFola ### (let range:reduce (fun (_range _fun) { @@ -101,5 +127,6 @@ (set _output (_fun _output _last)) (set _last (_range)) }) + (_range.reset) _output })) \ No newline at end of file From 5af195c494fb55ec863efecc738f841f974820ad Mon Sep 17 00:00:00 2001 From: SuperFola Date: Sat, 12 Sep 2020 16:23:44 +0200 Subject: [PATCH 11/12] updating string lib --- String.ark | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/String.ark b/String.ark index 73b4e57..bca3335 100644 --- a/String.ark +++ b/String.ark @@ -5,12 +5,12 @@ # --- # (import "String.ark") # (let message "HeLLo World, I like cheese") -# (let new (string:toLower 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 string:toLower (fun (text) { +(let str:toLower (fun (text) { (mut _index 0) (mut _e "") (mut _output "") @@ -73,12 +73,12 @@ # --- # (import "String.ark") # (let message "hello world, I like cheese") -# (let new (string:toUpper 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 string:toUpper (fun (_string) { +(let str:toUpper (fun (_string) { (mut _index 0) (mut _e "") (mut _output "") @@ -142,12 +142,12 @@ # --- # (import "String.ark") # (let message "hello world, I like goats") -# (let reversed (string:reverse message)) # => staog ekil I ,dlrow olleh +# (let reversed (str:reverse message)) # => staog ekil I ,dlrow olleh # --- # @param _string the string to reverse # @author https://github.com/Natendrtfm ### -(let string:reverse (fun (_string) { +(let str:reverse (fun (_string) { (mut _index (- (len _string) 1)) (mut _returnedString "") (while (> _index -1) { @@ -164,14 +164,14 @@ # --- # (import "String.ark") # (let message "hello world, I like goats") -# (let slice (string:slice message 6 4)) # => worl +# (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 string:slice (fun (_string _startingIndex _length) { +(let str:slice (fun (_string _startingIndex _length) { (assert (>= _length 1) "slice length must be greater or equal to 1") (assert (and (>= _startingIndex 0) (< _startingIndex (len _string))) "slice start index must be in range [0, string length[") @@ -193,13 +193,13 @@ # --- # (import "String.ark") # (let message "hello world, I like boats") -# (let splitted (string:split message " ")) +# (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 string:split (fun (_string _separator) { +(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) @@ -220,4 +220,29 @@ (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 From 908b85858473c220baed20ce974aa83e717800bd Mon Sep 17 00:00:00 2001 From: SuperFola Date: Mon, 14 Sep 2020 09:05:39 +0200 Subject: [PATCH 12/12] fixing str:toLower (missing Y); fixing str:slice as well to work with slices of length 0 --- String.ark | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/String.ark b/String.ark index bca3335..616818e 100644 --- a/String.ark +++ b/String.ark @@ -41,6 +41,7 @@ (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 "à") @@ -58,7 +59,7 @@ (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)) @@ -171,20 +172,23 @@ # @param _length the length of the slice # @author https://github.com/Natendrtfm ### -(let str:slice (fun (_string _startingIndex _length) { - (assert (>= _length 1) "slice length must be greater or equal to 1") - (assert (and (>= _startingIndex 0) (< _startingIndex (len _string))) "slice start index must be in range [0, string length[") +(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))) + (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 -})) + (while (< _index _end) { + (set _returnedString (+ _returnedString (@ _string _index))) + (set _index (+ _index 1)) + }) + _returnedString + }) +)) ### # @meta String