Browse Source

Flip argument order of str_append and str_prepend functions

Mads Møller Jensen 11 months ago
parent
commit
7f3cb640d6

+ 1 - 1
book/src/list-functions-lists.md

@@ -257,7 +257,7 @@ fn foldl<A, B>(f: Fn[(A, B) -> A], acc: A, xs: List<B>) -> A
 <summary>Examples</summary>
 
 Join a list of strings by folding.
-<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=foldl%28str%5Fprepend%2C%20%22%22%2C%20%5B%22Num%22%2C%20%22bat%22%2C%20%22%21%22%5D%29')""></button></div><code class="language-nbt hljs numbat">foldl(str_prepend, "", ["Num", "bat", "!"])
+<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=foldl%28str%5Fappend%2C%20%22%22%2C%20%5B%22Num%22%2C%20%22bat%22%2C%20%22%21%22%5D%29')""></button></div><code class="language-nbt hljs numbat">foldl(str_append, "", ["Num", "bat", "!"])
 
     = "Numbat!"    [String]
 </code></pre>

+ 4 - 4
book/src/list-functions-strings.md

@@ -108,7 +108,7 @@ fn uppercase(s: String) -> String
 Concatenate two strings.
 
 ```nbt
-fn str_append(a: String, str: String) -> String
+fn str_append(a: String, b: String) -> String
 ```
 
 <details>
@@ -116,7 +116,7 @@ fn str_append(a: String, str: String) -> String
 
 <pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=%22Numbat%22%20%7C%3E%20str%5Fappend%28%22%21%22%29')""></button></div><code class="language-nbt hljs numbat">"Numbat" |> str_append("!")
 
-    = "Numbat!"    [String]
+    = "!Numbat"    [String]
 </code></pre>
 
 </details>
@@ -125,7 +125,7 @@ fn str_append(a: String, str: String) -> String
 Concatenate two strings.
 
 ```nbt
-fn str_prepend(a: String, str: String) -> String
+fn str_prepend(a: String, b: String) -> String
 ```
 
 <details>
@@ -133,7 +133,7 @@ fn str_prepend(a: String, str: String) -> String
 
 <pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=%22%21%22%20%7C%3E%20str%5Fprepend%28%22Numbat%22%29')""></button></div><code class="language-nbt hljs numbat">"!" |> str_prepend("Numbat")
 
-    = "Numbat!"    [String]
+    = "!Numbat"    [String]
 </code></pre>
 
 </details>

+ 2 - 2
examples/tests/strings.nbt

@@ -14,8 +14,8 @@ assert_eq(ord("A"), 65)
 assert_eq(ord("a"), 97)
 assert_eq(ord("❤"), 0x2764)
 
-assert_eq(str_append("bar", "foo"), "foobar")
-assert_eq(str_prepend("foo", "bar"), "foobar")
+assert_eq(str_append("foo", "bar"), "foobar")
+assert_eq(str_prepend("bar", "foo"), "foobar")
 
 assert(str_contains("hello", "hello world"))
 assert(str_contains("world", "hello world"))

+ 1 - 1
numbat/modules/core/lists.nbt

@@ -87,7 +87,7 @@ fn filter<A>(p: Fn[(A) -> Bool], xs: List<A>) -> List<A> =
       else filter(p, tail(xs))
 
 @description("Fold a function over a list")
-@example("foldl(str_prepend, \"\", [\"Num\", \"bat\", \"!\"])", "Join a list of strings by folding.")
+@example("foldl(str_append, \"\", [\"Num\", \"bat\", \"!\"])", "Join a list of strings by folding.")
 fn foldl<A, B>(f: Fn[(A, B) -> A], acc: A, xs: List<B>) -> A =
   if is_empty(xs)
     then acc

+ 6 - 6
numbat/modules/core/strings.nbt

@@ -28,11 +28,11 @@ fn uppercase(s: String) -> String
 
 @description("Concatenate two strings")
 @example("\"Numbat\" |> str_append(\"!\")")
-fn str_append(a: String, str: String) -> String = "{str}{a}"
+fn str_append(a: String, b: String) -> String = "{a}{b}"
 
 @description("Concatenate two strings")
 @example("\"!\" |> str_prepend(\"Numbat\")")
-fn str_prepend(a: String, str: String) -> String = "{a}{str}"
+fn str_prepend(a: String, b: String) -> String = "{b}{a}"
 
 @description("Find the first occurrence of a substring in a string")
 @example("str_find(\"typed\", \"Numbat is a statically typed programming language.\")")
@@ -59,8 +59,8 @@ fn str_replace(pattern: String, replacement: String, s: String) -> String =
     then s
     else if str_contains(pattern, s)
       then if str_slice(0, pattern_length, s) == pattern
-          then (s |> str_slice(pattern_length, s_length) |> str_replace(pattern, replacement) |> str_prepend(replacement))
-          else (s |> str_slice(             1, s_length) |> str_replace(pattern, replacement) |> str_prepend(str_slice(0, 1, s)))
+          then (s |> str_slice(pattern_length, s_length) |> str_replace(pattern, replacement) |> str_append(replacement))
+          else (s |> str_slice(             1, s_length) |> str_replace(pattern, replacement) |> str_append(str_slice(0, 1, s)))
       else s
   where s_length = str_length(s)
     and pattern_length = str_length(pattern)
@@ -69,7 +69,7 @@ fn str_replace(pattern: String, replacement: String, s: String) -> String =
 @example("str_repeat(4, \"abc\")")
 fn str_repeat(n: Scalar, a: String) -> String =
   if n > 0
-    then str_prepend(a, str_repeat(n - 1, a))
+    then str_append(a, str_repeat(n - 1, a))
     else ""
 
 fn _bin_digit(x: Scalar) -> String =
@@ -100,7 +100,7 @@ fn base(b: Scalar, x: Scalar) -> String =
     then "-{base(b, -x)}"
     else if x < b
       then _digit_in_base(b, x)
-      else str_prepend(base(b, floor(x / b)), _digit_in_base(b, mod(x, b)))
+      else str_append(base(b, floor(x / b)), _digit_in_base(b, mod(x, b)))
 
 @description("Get a binary representation of a number.")
 @example("42 -> bin")

+ 1 - 1
numbat/modules/datetime/human.nbt

@@ -36,7 +36,7 @@ fn _human_recurse(t: Time, result: String, time_unit: String) -> String =
 fn _year_month_approx(t: Time) -> String = _human_join(the_years -> _human_years, t - the_years -> _human_months)
   where the_years = t |> floor_in(year)
 
-fn _human_manage_past(str: String, time: Time) = str_prepend(str, if time < 0 s then " ago" else "")
+fn _human_manage_past(str: String, time: Time) = str_append(str, if time < 0 s then " ago" else "")
 
 fn _human_for_long_duration(human_days: String, human_years: String) -> String =
   "{human_days} (approx. {human_years})"

+ 1 - 1
numbat/modules/extra/color.nbt

@@ -40,7 +40,7 @@ fn color_hex(color: Color) -> String =
   "{color -> _color_to_scalar -> hex:>8}" |> 
     str_replace("0x", "") |> 
     str_replace(" ", "0") |> 
-    str_prepend("#")
+    str_append("#")
 
 let black: Color = rgb(0, 0, 0)
 let white: Color = rgb(255, 255, 255)