Skip to content

Commit 15ea02a

Browse files
committed
chore: fix invariants and prefer using append! over append!
1 parent 4f2e195 commit 15ea02a

File tree

5 files changed

+51
-54
lines changed

5 files changed

+51
-54
lines changed

List.ark

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@
487487
[_list1 _list2] }))
488488

489489
# @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]]
490+
# @details Lists must have the same size. Otherwise, only the first (min (len _a) (len _b)) elements will be used.
490491
# @param _a the first list to work on
491492
# @param _b the second list to work on
492493
# @details The original lists are not modified.
@@ -523,9 +524,15 @@
523524
(mut _index 0)
524525

525526
(while (< _index _m) {
526-
(append! _c [
527-
(if (< _index (len _a)) (@ _a _index) _filler)
528-
(if (< _index (len _b)) (@ _b _index) _filler)])
527+
(append!
528+
_c
529+
[
530+
(if (< _index (len _a))
531+
(@ _a _index)
532+
_filler)
533+
(if (< _index (len _b))
534+
(@ _b _index)
535+
_filler)])
529536
(set _index (+ 1 _index)) })
530537
_c }))
531538

@@ -743,7 +750,7 @@
743750
_early }))
744751

745752
# @brief Transpose a list of lists or list of strings
746-
# @details The original list is not modified. Each element should have the same length
753+
# @details The original list is not modified. Elements should have the same length
747754
# @param _L list of lists/strings to transpose
748755
# =begin
749756
# (let data [[1 2 3] [4 5 6] [7 8 9])
@@ -800,8 +807,7 @@
800807
(mut _i 0)
801808
(while (< _i (len _indices)) {
802809
(let _idx (@ _indices _i))
803-
(if (< _idx (len _L))
804-
(append! _output (@ _L _idx)))
810+
(if (< _idx (len _L)) (append! _output (@ _L _idx)))
805811
(set _i (+ 1 _i)) })
806812
_output }))
807813

@@ -831,9 +837,11 @@
831837
(while _continue {
832838
(mut _i nil)
833839
(if
834-
(forEach _reversed_indices (fun (_val) {
835-
(set _i _val)
836-
(if (!= (@ _indices _i) (+ _i _len (* -1 _r))) stopIteration) }))
840+
(forEach
841+
_reversed_indices
842+
(fun (_val) {
843+
(set _i _val)
844+
(if (!= (@ _indices _i) (+ _i _len (* -1 _r))) stopIteration) }))
837845
{
838846
(@= _indices _i (+ 1 (@ _indices _i)))
839847
(mut _j (+ 1 _i))
@@ -872,9 +880,11 @@
872880
(while _continue {
873881
(mut _i nil)
874882
(if
875-
(forEach _reversed_range (fun (_val) {
876-
(set _i _val)
877-
(if (!= (@ _indices _i) (- _len 1)) stopIteration) }))
883+
(forEach
884+
_reversed_range
885+
(fun (_val) {
886+
(set _i _val)
887+
(if (!= (@ _indices _i) (- _len 1)) stopIteration) }))
878888
{
879889
(mut _j _i)
880890
(let _val (+ 1 (@ _indices _i)))

Macros.ark

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
(__replace_placeholders (tail replacements) ...xs) }
7272
{
7373
x
74-
7574
(__replace_placeholders replacements ...xs) })) })
7675

7776
# @brief Create a partial function with prefilled arguments, allowing some arguments to be skipped

Math.ark

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237
0
238238
(exp (* 0.5 (ln _x))))))
239239

240-
# @brief Run the fibonacci function on a number
240+
# @brief Compute the nth fibonacci term
241241
# @param n the number
242242
# @author https://github.com/SuperFola
243243
(let fibo (fun (n) {
@@ -275,14 +275,13 @@
275275
# =end
276276
# @author https://github.com/Wafelack
277277
(let divs (fun (n) {
278-
(assert (>= n 2) "divs: n must be greater or equal to 2")
279-
(mut i 2)
280-
(mut divisors [1])
278+
(assert (>= n 1) "divs: n must be greater or equal to 1")
279+
(mut i 1)
280+
(mut divisors [])
281281
(let top (ceil (/ n 2)))
282282

283283
(while (and (<= i top) (!= top n)) {
284-
(if (= (mod n i) 0)
285-
(set divisors (append divisors i)))
284+
(if (= (mod n i) 0) (append! divisors i))
286285
(set i (+ i 1)) })
287286
(append divisors n) }))
288287

@@ -294,8 +293,7 @@
294293
# =end
295294
# @author https://github.com/Gryfenfer97
296295
(let log (fun (x n) {
297-
(assert (> x 0) "log: x must be greater than 0")
298-
(assert (>= n 1) "log: n must be greater or equal to 1")
296+
(assert (and (> x 0) (>= n 1)) "log: x must be greater than 0, and n must be greater or equal to 1")
299297
(round (/ (ln x) (ln n))) }))
300298

301299
# @brief Returns the logarithm base 2 of a number
@@ -444,7 +442,7 @@
444442
# (let base 55) # something takes 55ms to run
445443
# (let new 43) # now it takes 43ms
446444
# (print (math:improvementRatioPercentage base new)) # 27.9069767442
447-
# # 'base' is 27%~ slower than 'new'
445+
# # 'new' is 27%~ faster than 'base'
448446
# =end
449447
# @author https://github.com/SuperFola
450448
(let improvementRatioPercentage (fun (_a _b) (* 100 (- (/ _a _b) 1))))
@@ -467,17 +465,15 @@
467465
# (print (math:radians 90)) # pi/2
468466
# =end
469467
# @author https://github.com/SuperFola
470-
(let radians (fun (_degrees)
471-
(/ (* _degrees pi) 180)))
468+
(let radians (fun (_degrees) (/ (* _degrees pi) 180)))
472469

473470
# @brief Convert an angle in radians to degrees
474471
# @param _radians angle in radians
475472
# =begin
476473
# (print (math:radians (/ math:pi 2))) # 90
477474
# =end
478475
# @author https://github.com/SuperFola
479-
(let degrees (fun (_radians)
480-
(/ (* 180 _radians) pi)))
476+
(let degrees (fun (_radians) (/ (* 180 _radians) pi)))
481477

482478
# @brief Check if a given number is an integer
483479
# @param _x number
@@ -486,8 +482,7 @@
486482
# (print (math:integer 1.000001)) # false
487483
# =end
488484
# @author https://github.com/SuperFola
489-
(let integer? (fun (_x)
490-
(= (ceil _x) (floor _x))))
485+
(let integer? (fun (_x) (= (ceil _x) (floor _x))))
491486

492487
# @brief Compute the factorial of a number
493488
# @param _n integer
@@ -512,11 +507,7 @@
512507
# @author https://github.com/SuperFola
513508
(let binomialCoeff (fun (_n _k)
514509
(if (<= _k _n)
515-
(/
516-
(factorial _n)
517-
(*
518-
(factorial _k)
519-
(factorial (- _n _k))))
510+
(/ (factorial _n) (* (factorial _k) (factorial (- _n _k))))
520511
0)))
521512

522513
# @brief Compute the number of ways to choose k items from n items without repetition and with order
@@ -526,9 +517,7 @@
526517
# @author https://github.com/SuperFola
527518
(let permutations (fun (_n _k)
528519
(if (<= _k _n)
529-
(/
530-
(factorial _n)
531-
(factorial (- _n _k)))
520+
(/ (factorial _n) (factorial (- _n _k)))
532521
0)))
533522

534523
# @brief Compare two real numbers and return true if the first one is near the second one (1e-7 precision)
@@ -537,7 +526,7 @@
537526
# @author https://github.com/SuperFola
538527
(let close? (fun (_n _target) {
539528
# todo: make epsilon configurable
540-
(let _epsilon 1e-7)
529+
(let _epsilon 1e-07)
541530
(and (< (- _target _epsilon) _n) (< _n (+ _target _epsilon))) }))
542531

543532
# @brief Compute the euclidean distance between two vectors of the same size
@@ -579,7 +568,4 @@
579568
# (print (math:lcm 4 6)) # 12
580569
# =end
581570
# @author https://github.com/SuperFola
582-
(let lcm (fun (_a _b)
583-
(*
584-
(abs _a)
585-
(/ (abs _b) (gcd _a _b)))))
571+
(let lcm (fun (_a _b) (* (abs _a) (/ (abs _b) (gcd _a _b)))))

Range.ark

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
(mut _output [])
6767

6868
(while (not (nil? _value)) {
69-
(if (_fun _value)
70-
(set _output (append _output _value)))
69+
(if (_fun _value) (append! _output _value))
7170
(set _value (_range)) })
7271
(_range.reset)
7372
_output }))
@@ -86,7 +85,7 @@
8685
(mut _output [])
8786

8887
(while (not (nil? _value)) {
89-
(set _output (append _output (_fun _value)))
88+
(append! _output (_fun _value))
9089
(set _value (_range)) })
9190
(_range.reset)
9291
_output }))

String.ark

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,7 @@
440440
# (print (string:startsWith? "Hello, world" ", world")) # false
441441
# =end
442442
# @author https://github.com/SuperFola
443-
(let startsWith? (fun (_str _prefix)
444-
(= 0 (find _str _prefix))))
443+
(let startsWith? (fun (_str _prefix) (= 0 (find _str _prefix))))
445444

446445
# @brief Check if a string ends with a given suffix
447446
# @param _str string
@@ -451,8 +450,9 @@
451450
# (print (string:endsWith? "Hello, world" "worl")) # false
452451
# =end
453452
# @author https://github.com/SuperFola
454-
(let endsWith? (fun (_str _suffix)
455-
(= 0 (find (reverse _str) (reverse _suffix)))))
453+
(let endsWith? (fun (_str _suffix) {
454+
(let _end (- (len _str) (len _suffix)))
455+
(= _end (findAfter _str _suffix _end)) }))
456456

457457
# @brief Return a string filled with '0' digits to make a string of length _n
458458
# @param _str string to left fill
@@ -478,12 +478,15 @@
478478
_str
479479
{
480480
(let _fill (- _len (len _str)))
481-
(let _fill_left (if (= 1 (mod _fill 2)) (/ (- _fill 1) 2) (/ _fill 2)))
482-
(let _fill_right (if (= 1 (mod _fill 2)) (/ (+ _fill 1) 2) (/ _fill 2)))
483-
(+
484-
(repeat " " _fill_left)
485-
_str
486-
(repeat " " _fill_right)) })))
481+
(let _fill_left
482+
(if (= 1 (mod _fill 2))
483+
(/ (- _fill 1) 2)
484+
(/ _fill 2)))
485+
(let _fill_right
486+
(if (= 1 (mod _fill 2))
487+
(/ (+ _fill 1) 2)
488+
(/ _fill 2)))
489+
(+ (repeat " " _fill_left) _str (repeat " " _fill_right)) })))
487490

488491
# @brief If a string starts with a given prefix, remove it
489492
# @param _str string

0 commit comments

Comments
 (0)