Aug 13, 2022Kuina-chan


This is the reverse lookup dictionary 2 for exe execution environment of Kuin Programming Language, and is about how to handle arrays and strings.

Handling Arrays

Handling Array Of Array

Handling String

Index

1Handling Arrays

An array is a type that can handle multiple variables of the same type together.

1.1Create Array



An array in Kuin can be written as "[]Type Name" to represent an array of multiple values of that type (Figure 1-1).
  1. func main()
  2.   var array: []int
  3.   do array :: #[3]int {[0, 0, 0]}
  4.   do array[0] :: 5 {[5, 0, 0]}
  5.   do array :: [1, 2, 3, 4] {[1, 2, 3, 4]}
  6. end func
Figure 1-1: Array
If you write "[]int" as in the second line, the type will be an array of type int.
By writing "#[Number Of Elements]Type Name" as in the 3rd line, an instance of the array with the specified number of elements will be created. Each element contains the default value of its type, which is 0 for the int type.
By writing "Variable Name Of Array[Element Number]" as in the 4th line, you can read and write values to the elements of that array. The element number starts from 0 and becomes "number of elements-1" for the last element.
You can also create an instance by specifying the value of each element by writing "[Value 1,Value 2,...,Value N]" as shown in line 5.

1.2Get Number Of Elements In Array



The number of elements in an array can be obtained with the element count operator "^". Figure 1-2 shows a program that assigns to all the elements of an array in order.
  1. func main()
  2.   var array: []int :: #[10]int
  3.   for i(0, ^array - 1)
  4.     do array[i] :: i
  5.   end for
  6. end func
Figure 1-2: Number Of Array Elements
The 3rd line, "^array" returns 10. By the 4th line, array[0] will contain 0, array[1] will contain 1, and array[9] will contain 9.

1.3Combine Arrays



To combine two arrays, use the array concatenation operator "~" (Figure 1-3).
  1. func main()
  2.   var array1: []int :: [1, 3, 5]
  3.   var array2: []int :: [2, 4, 6]
  4.   var array3: []int :: array1 ~ array2 {[1, 3, 5, 2, 4, 6]}
  5. end func
Figure 1-3: Array Connection

1.4Fill Each Element Of Array With The Same Value



To fill each element of the array with the same value, use the .fill method (Figure 1-4).
  1. func main()
  2.   var array: []int
  3.   do array :: #[5]int {[0, 0, 0, 0, 0]}
  4.   do array.fill(123) {[123, 123, 123, 123, 123]}
  5. end func
Figure 1-4: fill Method

1.5Create New Array By Repeating Elements Of Array



To create a new array by repeating the elements of an array, use the .repeat method (Figure 1-5).
  1. func main()
  2.   var array: []int :: [3, 5] {[3, 5]}
  3.   var array2: []int :: array.repeat(4) {[3, 5, 3, 5, 3, 5, 3, 5]}
  4. end func
Figure 1-5: repeat Method

1.6Cut Out Portion Of Array



To cut out a part of an array, use the .sub method (Figure 1-6).
  1. func main()
  2.   var array: []int :: [1, 2, 3, 4, 5, 6] {[1, 2, 3, 4, 5, 6]}
  3.   var array2: []int :: array.sub(3, 2) {[4, 5]}
  4.   var array3: []int :: array.sub(2, -1) {[3, 4, 5, 6]}
  5. end func
Figure 1-6: sub Method
If you write ".sub(a, b)", the b number of elements will be cut out from the ath element. If -1 is specified for b, the array will be cut down to the last element.

1.7Sort Array In Ascending Or Descending Order



To sort the array in ascending order, use the .sort method.、To sort in descending order, use the .reverse method to reverse the order after sorting in ascending order (Figure 1-7).
  1. func main()
  2.   var array: []int :: [3, 6, 2, 4, 1, 2] {[3, 6, 2, 4, 1, 2]}
  3.   do array.sort() {[1, 2, 2, 3, 4, 6]}
  4.   do array.reverse() {[6, 4, 3, 2, 2, 1]}
  5. end func
Figure 1-7: Sorting
The .sort method can only be used when the types of the array elements can be compared.

1.8Sorting Arrays In Reverse Order



To sort the array in reverse order, use the .reverse method (Figure 1-8).
  1. func main()
  2.   var array: []int :: [3, 6, 2, 4, 1, 2] {[3, 6, 2, 4, 1, 2]}
  3.   do array.reverse() {[2, 1, 4, 2, 6, 3]}
  4. end func
Figure 1-8: Reverse Order

1.9Searching For Element In Array



To search for an element in an array, use the .find and .findLast methods (Figure 1-9).
  1. func main()
  2.   var array: []int :: [3, 6, 5, 4, 5, 1]
  3.   var pos: int
  4.   do pos :: array.find(5, -1) {2}
  5.   do pos :: array.find(5, 2) {2}
  6.   do pos :: array.find(5, 3) {4}
  7.   do pos :: array.find(5, 5) {-1}
  8.   do pos :: array.findLast(5, -1) {4}
  9.   do pos :: array.findLast(5, 4) {4}
  10.   do pos :: array.findLast(5, 3) {2}
  11.   do pos :: array.findLast(5, 1) {-1}
  12. end func
Figure 1-9: Searching For Array Element
If you write ".find(a, b)", it will look for a from the bth element and return the element index found. If not found, -1 is returned. ".findLast(a, b)" is the same process, but it searches from the end of the array to the beginning.
When the contents of the array are sorted in ascending order, the ".findBin" method can be used for a fast search (binary search). If you want to search the same array multiple times, you can use the .sort method to sort in ascending order, and then use .findBin (Figure 1-10).
  1. func main()
  2.   var array: []int :: [1, 2, 4, 6, 7] {Array in ascending order.}
  3.   var pos: int :: array.findBin(6) {3}
  4. end func
Figure 1-10: Binary Search Of Array Element
If found, the element index is returned; if not, -1 is returned. The .findBin method can only be used when the types of the array elements can be compared.

2Handling Array Of Array

2.1Handling Array Of Array



In Kuin, multidimensional arrays are handled as arrays of arrays. In other words, a two-dimensional array of type int is treated as an array of arrays, as in "[][]int" (Figure 2-1).
  1. func main()
  2.   var array: [][]int :: [[1, 2], [3, 4, 5], [6]]
  3. end func
Figure 2-1: Array Of Array
In this case, the elements are lined up as shown in Table 2-1.
Table 2-1: Example Of Array Of Array
1 2
3 4 5
6
When the number of elements in each dimension of a multidimensional array is fixed (i.e., rectangular in the case of two dimensions), an instance of the array can be created at once (Figure 2-2).
  1. func main()
  2.   var array: [][][]int :: #[3, 2, 5]int
  3. end func
Figure 2-2: Initializing Multidimensional Array
In this example, the elements array[0][0][0][0] through array[2][1][4] will be created. The value of each element will be the default value (0 for the int type).

3Handling String

3.1Handling String



In Kuin, strings are handled as []char, an array of character types. Therefore, all the functions provided for arrays can also be used for strings (Figure 3-1).
  1. func main()
  2.   var str1: []char :: "abc"
  3.   var str2: []char :: "def"
  4.   var str3: []char :: str1 ~ str2 {"abcdef"}
  5. end func
Figure 3-1: String Type
The term '"abc"' is equivalent to '['a', 'b', 'c']' and can only be used for string types.

3.2Converting From Another Type To String Type



To convert from another type, such as int, to a string type, use the .toStr method (Figure 3-2).
  1. func main()
  2.   var n: int :: 5
  3.   var x: float :: 1.23
  4.   var str1: []char :: n.toStr() {str1 = "5"}
  5.   var str2: []char :: x.toStr() {str2 = "1.23"}
  6. end func
Figure 3-2: toStr Method
The types that can use the .toStr method are int, float, char, bool, each bit type, []char, and class.

3.3Convert From String Type To int Or float Type



To convert from string type to int type or float type, use the .toInt and .toFloat methods, respectively (Figure 3-3).
  1. func main()
  2.   var str1: []char :: "5"
  3.   var str2: []char :: "1.23"
  4.   var existed: bool
  5.   var n: int
  6.   var x: float
  7.  
  8.   do n :: str1.toInt(&existed) {existed = true, n = 5}
  9.   do x :: str2.toFloat(&existed) {existed = true, x = 1.23}
  10. end func
Figure 3-3: toInt And toFloat Methods
To the arguments of the .toInt and .toFloat methods, true will be returned if the conversion succeeds, and false if it fails.

3.4Embedding Variable Value In String



To embed the value of a variable in a string, write "\{Variable}" (Figure 3-4).
  1. func main()
  2.   var n: int :: 1234
  3.   var str: []char :: "n = \{n}" {" n = 1234 "}
  4. end func
Figure 3-4: Embedding Variable
If you write '"\{n}"', it will be replaced by '"" ~ (n).toStr() ~ ""' at compile time. You can write arbitrary expressions such as '"\{1 + 2}"' as well as variables.

3.5Comparing Strings



Comparison operators such as "=" and "<" allow you to compare the order of strings in lexicographic order (the order in which they appear first in the dictionary). The order of each character is determined by the character code order. To compare whether string instances match or whether a string instance is null, use the reference comparison operators "=&" or "<>&" (Figure 3-5).
  1. func main()
  2.   var s1: []char :: "pen"
  3.   var s2: []char :: "pencil"
  4.   var s3: []char :: "pencil"
  5.   var s4: []char :: s2
  6.   var b: bool
  7.   do b :: s1 <= s2 {true}
  8.   do b :: s1 >= s2 {false}
  9.   do b :: s2 = s3 {true}
  10.   do b :: s2 =& s3 {false}
  11.   do b :: s2 =& s4 {true}
  12.   do b :: s2 =& null {false}
  13. end func
Figure 3-5: Comparing Strings

3.6Replacing String



To replace a part of a string with another string, use the .replace method (Figure 3-6).
  1. func main()
  2.   var s1: []char :: "abc<br>def<br>g"
  3.   var s2: []char :: s1.replace("<br>", "/") {"abc/def/g"}
  4. end func
Figure 3-6: Replacing String

3.7Split String With Delimiter String



To split a string into an array with a delimiter string, use the .split method (Figure 3-7).
  1. func main()
  2.   var s: []char :: "abc<br>def<br>g"
  3.   var ss: [][]char :: s.split("<br>") {["abc", "def", "g"]}
  4. end func
Figure 3-7: Splitting String

3.8Combine Arrays By Delimiter String



To join arrays into a single string with a delimiter string, use the .join method (Figure 3-8).
  1. func main()
  2.   var ss: [][]char :: ["abc", "def", "g"]
  3.   var s: []char :: ss.join("<br>") {"abc<br>def<br>g"}
  4. end func
Figure 3-8: Joining Strings

3.9Remove Left And Right Margins Of String



To remove the left and right margins of a string, use the .trim, .trimLeft, or .trimRight methods (Figure 3-9).
  1. func main()
  2.   var s1: []char :: "   a b c   "
  3.   var s2: []char
  4.   do s2 :: s1.trim() {"a b c"}
  5.   do s2 :: s1.trimLeft() {"a b c   "}
  6.   do s2 :: s1.trimRight() {"   a b c"}
  7. end func
Figure 3-9: Remove String Margins
The .trim, .trimLeft, and .trimRight methods will remove the left and right margins, the left side only, and the right side only, respectively. The margins to be deleted include tab characters, line feed characters, and half-width spaces (0x09 to 0x0D, 0x20, and 0xA0 in character code).

3.10Unify Letters In Uppercase And Lowercase



To unify letters in upper or lower case, use the .upper and .lower methods, respectively (Figure 3-10).
  1. func main()
  2.   var s1: []char :: "Kuin Programming Language"
  3.   var s2: []char
  4.   do s2 :: s1.upper() {"KUIN PROGRAMMING LANGUAGE"}
  5.   do s2 :: s1.lower() {"kuin programming language"}
  6. end func
Figure 3-10: Uppercase And Lowercase
1660380786enf