Skip to content
Merged
15 changes: 15 additions & 0 deletions Dict.ark
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
# @author https://github.com/SuperFola
(let add (fun (_D _key _value) (builtin__dict:add _D _key _value)))

# @brief Get a value from a given dictionary using a key, or a default value if it doesn't exist
# @param _D dictionary
# @param _key key to get
# @param _default default value in case the key isn't in the dictionary
# =begin
# (let data (dict "key" "value"))
# (print (dict:getOrElse data "key" "hello")) # value
# (print (dict:getOrElse data "count" 5)) # 5
# =end
# @author https://github.com/SuperFola
(let getOrElse (fun (_D _key _default)
(if (contains _D _key)
(get _D _key)
_default)))

# @brief Updates an entry or create it from a default value
# @details The dictionary is modified in place
# @param _D dictionary
Expand Down
2 changes: 1 addition & 1 deletion Events.ark
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# @brief Allows to register events listeners and emit events
# =begin
# (let em (manager:make))
# (let em (events:manager:make))
# (em.on "myType" (fun (value) (print "This is a callback")))
# (em.emit "myType") # => prints "This is a callback" thanks to the registered listener
# =end
Expand Down
2 changes: 1 addition & 1 deletion Lazy.ark
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# (let complex_stuff (fun () {
# # do complex work in the function
# 42 }))
# (let lazy (eval complex_stuff))
# (let lazy (lazy:eval complex_stuff))
# (print (lazy))
# =end
# @author https://github.com/SuperFola
Expand Down
82 changes: 54 additions & 28 deletions List.ark
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@
# @param _func the function to call on each element
# @details The original list is not modified.
# =begin
# (import std.List)
# (let collection [1 2 5 12])
# (forEach collection (fun (element) {
# (print element)
# }))
# (list:forEach collection (fun (element) {
# (print element) }))
# =end
# @author https://github.com/SuperFola
(let forEach (fun (_L _func) {
Expand All @@ -87,13 +85,30 @@
(_func _element)
(set _index (+ 1 _index)) }) }))

# @brief Iterate over a given list and run a given function on every element, passing its index as well.
# @param _L the list to iterate over
# @param _func a binary function to call on each element with (index, element)
# @details The original list is not modified.
# =begin
# (let collection [1 2 5 12])
# (list:enumerate collection (fun (idx element) {
# (print idx " " element) }))
# =end
# @author https://github.com/SuperFola
(let enumerate (fun (_L _func) {
(mut _index 0)

(while (< _index (len _L)) {
(mut _element (@ _L _index))
(_func _index _element)
(set _index (+ 1 _index)) }) }))

# @brief Iterate over a given list and multiply all the elements with the others.
# @param _L the list to iterate over
# @details The original list is not modified.
# =begin
# (import std.List)
# (let collection [1 2 5 12])
# (let p (product collection)) # => 120
# (let p (list:product collection)) # => 120
# =end
# @author https://github.com/Unactived
(let product (fun (_L) {
Expand All @@ -109,9 +124,8 @@
# @param _L the list to iterate over
# @details The original list is not modified.
# =begin
# (import std.List)
# (let collection [1 2 5 12])
# (let p (sum collection)) # => 20
# (let p (list:sum collection)) # => 20
# =end
# @author https://github.com/Unactived
(let sum (fun (_L) {
Expand Down Expand Up @@ -144,7 +158,7 @@
# @param _L list of numbers
# @details The original list is not modified.
# =begin
# (let value (list:min [0 1 2 3 5 8])) # 8
# (let value (list:max [0 1 2 3 5 8])) # 8
# =end
# @author https://github.com/SuperFola
(let max (fun (_L) {
Expand All @@ -165,7 +179,7 @@
# @details The original list is not modified.
# =begin
# (let cool-stuff [1 2 3 4 5 6 7 8 9])
# (print (drop cool-stuff 4)) # [5 6 7 8 9]
# (print (list:drop cool-stuff 4)) # [5 6 7 8 9]
# =end
# @author https://github.com/rstefanic, https://github.com/SuperFola
(let drop (fun (_L _n)
Expand All @@ -188,7 +202,7 @@
# @details The original list is not modified.
# =begin
# (let cool-stuff [1 2 3 4 5 6 7 8 9])
# (print (dropWhile cool-stuff (fun (a) (< a 4)))) # [4 5 6 7 8 9]
# (print (list:dropWhile cool-stuff (fun (a) (< a 4)))) # [4 5 6 7 8 9]
# =end
# @author https://github.com/SuperFola
(let dropWhile (fun (_L _f) {
Expand All @@ -209,7 +223,7 @@
# @details The original list is not modified.
# =begin
# (import std.Math)
# (print (filter [1 2 3 4 5 6 7 8 9] math:even)) # [2 4 6 8]
# (print (list:filter [1 2 3 4 5 6 7 8 9] math:even)) # [2 4 6 8]
# =end
# @author https://github.com/rstefanic
(let filter (fun (_L _f) {
Expand All @@ -226,7 +240,7 @@
# @param _f the function to apply to each element
# @details The original list is not modified.
# =begin
# (print (map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81]
# (print (list:map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81]
# =end
# @author https://github.com/rstefanic
(let map (fun (_L _f) {
Expand All @@ -244,7 +258,7 @@
# @details The original list is not modified.
# =begin
# (let cool [1 2 3 4 5 6 7 8 9])
# (print (reduce cool (fun (a b) (+ a b)))) # 45
# (print (list:reduce cool (fun (a b) (+ a b)))) # 45
# =end
# @author https://github.com/Unactived
(let reduce (fun (_L _f) {
Expand All @@ -261,7 +275,7 @@
# @details The original list is not modified.
# =begin
# (let cool [[1 2 3] [4] 5 6 [7 8] 9])
# (print (flatten cool)) # [1 2 3 4 5 6 7 8 9]
# (print (list:flatten cool)) # [1 2 3 4 5 6 7 8 9]
# =end
# @author https://github.com/SuperFola
(let flatten (fun (_L) {
Expand All @@ -283,7 +297,7 @@
# @details The original list is not modified.
# =begin
# (let cool [1 2 3 4])
# (print (flatMap cool (fun (a) [a a]))) # [1 1 2 2 3 3 4 4]
# (print (list:flatMap cool (fun (a) [a a]))) # [1 1 2 2 3 3 4 4]
# =end
# @author https://github.com/SuperFola
(let flatMap (fun (_L _f) {
Expand All @@ -304,7 +318,7 @@
# @param _n the number of elements to take
# @details The original list is not modified.
# =begin
# (print (take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4]
# (print (list:take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4]
# =end
# @author https://github.com/rstefanic
(let take (fun (_L _n) {
Expand All @@ -322,7 +336,7 @@
# @param _f the predicate
# @details The original list is not modified.
# =begin
# (print (takeWhile [1 2 3 4 5 6 7 8 9 10] (fun (a) (< a 4)))) # [1 2 3]
# (print (list:takeWhile [1 2 3 4 5 6 7 8 9 10] (fun (a) (< a 4)))) # [1 2 3]
# =end
# @author https://github.com/rakista112
(let takeWhile (fun (_L _f) {
Expand Down Expand Up @@ -366,7 +380,7 @@
# @details The original list is not modified.
# =begin
# (let zipped [[1 5] [2 6] [3 7] [4 8]])
# (print (unzip zipped)) # [[1 2 3 4] [5 6 7 8]]
# (print (list:unzip zipped)) # [[1 2 3 4] [5 6 7 8]]
# =end
# @author https://github.com/Unactived
(let unzip (fun (_L) {
Expand All @@ -389,7 +403,7 @@
# =begin
# (let a [1 2 3 4])
# (let b [5 6 7 8])
# (print (zip a b)) # [[1 5] [2 6] [3 7] [4 8]]
# (print (list:zip a b)) # [[1 5] [2 6] [3 7] [4 8]]
# =end
# @author https://github.com/Unactived
(let zip (fun (_a _b) {
Expand All @@ -407,7 +421,7 @@
# @details The original list is not modified.
# =begin
# (let a [5 6 7 8])
# (print (zipWithIndex a)) # [[0 5] [1 6] [2 7] [3 8]]
# (print (list:zipWithIndex a)) # [[0 5] [1 6] [2 7] [3 8]]
# =end
# @author https://github.com/SuperFola
(let zipWithIndex (fun (_L) {
Expand All @@ -426,7 +440,7 @@
# @details The original list is not modified.
# =begin
# (let a [1 2 3 4])
# (print (foldLeft a 0 (fun (a b) (+ a b)))) # 10
# (print (list:foldLeft a 0 (fun (a b) (+ a b)))) # 10
# =end
# @author https://github.com/SuperFola
(let foldLeft (fun (_L _init _f) {
Expand All @@ -440,11 +454,11 @@

# @brief Check if a condition is verified for all elements of a list
# @param _L the list to work on
# @param _f the conditon
# @param _f the condition
# =begin
# (let a [1 2 3 4])
# (let f (fun (e) (< e 5)))
# (print (forAll a f)) # true
# (print (list:forAll a f)) # true
# =end
# @author https://github.com/Gryfenfer97
(let forAll (fun (_L _f) {
Expand All @@ -463,7 +477,7 @@
# =begin
# (let a [1 2 3 4])
# (let f (fun (e) (< e 3)))
# (print (any a f)) # true
# (print (list:any a f)) # true
# =end
# @author https://github.com/Gryfenfer97
(let any (fun (_L _f) {
Expand All @@ -476,13 +490,25 @@
(set _index (+ 1 _index)) })
_verified }))

# @brief Check if a condition can't be verified for any element of a list
# @param _L the list to work on
# @param _f the condition
# =begin
# (let a [1 2 3 4])
# (let f (fun (e) (< e 3)))
# (print (list:none a f)) # false
# (print (list:none [4 5 6] f)) # true
# =end
# @author https://github.com/SuperFola
(let none (fun (_L _f) (not (any _L _f))))

# @brief Count the number of elements in a list that match a condition
# @param _L the list to work on
# @param _f the condition
# =begin
# (let lst [1 2 3 4 5 6 7 8 9])
# (let is_even (fun (e) (= 0 (mod e 2))))
# (print (countIf lst is_even)) # 4
# (print (list:countIf lst is_even)) # 4
# =end
# @author https://github.com/SuperFola
(let countIf (fun (_L _f) {
Expand All @@ -503,7 +529,7 @@
# @param _length the sequence length
# =begin
# (let f (fun (x) (+ 7 x)))
# (print (iterate 0 f 10)) # [0 7 14 21 28 35 42 49 56 63]
# (print (list:iterate 0 f 10)) # [0 7 14 21 28 35 42 49 56 63]
# =end
# @author https://github.com/SuperFola
(let iterate (fun (_init _f _length) {
Expand All @@ -522,7 +548,7 @@
# @param _init initial value of the sequence
# @param _length the sequence length
# =begin
# (print (iota 0 10)) # [0 1 2 3 4 5 6 7 8 9]
# (print (list:iota 0 10)) # [0 1 2 3 4 5 6 7 8 9]
# =end
# @author https://github.com/SuperFola
(let iota (fun (_init _length) (iterate _init (fun (x) (+ 1 x)) _length)))
Expand Down
Loading
Loading