Updating built in Io code to use += instead of x = x + y
[io/quag.git] / libs / iovm / io / B_Sequence.io
blobca7662fe21092ce448f2ab5450cb0014ac6ae194
2 Vector := Sequence clone setItemType("float32") setEncoding("number")
3 vector := method(v := Vector clone; call evalArgs foreach(n, v append(n)); v)
5 Lobby Protos Core do(
6 ImmutableSequence := ""
7 String := ImmutableSequence
10 Sequence do(
11 setSlot("..", method(arg, self asString cloneAppendSeq(arg asString)))
13 docSlot("repeated(n)", "Returns a new sequence containing the receiver repeated n number of times.")
14 repeated := method(n,
15 s := Sequence clone
16 n repeat(s appendSeq(self))
20 docSlot("alignLeftInPlace(width, [padding])", "Same as align left but operation is performed on the receiver.")
21 alignLeftInPlace := method(width, padding,
22 originalSize := size
23 if(padding isNil or padding size == 0,
24 padding = " "
26 ((width - size) / padding size) ceil repeat(appendSeq(padding))
27 setSize(width max(originalSize))
30 docSlot("alignLeft(width, [padding])", """
31 Example:
32 <pre>
33 Io> "abc" alignLeft(10, "-")
34 ==> abc-------
35 Io> "abc" alignLeft(10, "-=")
36 ==> abc-=-=-=-
37 </pre>
38 """)
39 alignLeft := method(width, padding, asMutable alignLeftInPlace(width, padding))
41 docSlot("alignRight(width, [padding])", """
42 Example:
43 <pre>
44 Io> "abc" alignRight(10, "-")
45 ==> -------abc
46 Io> "abc" alignRight(10, "-=")
47 ==> -=-=-=-abc
48 </pre>
49 """)
50 alignRight := method(width, padding,
51 Sequence clone alignLeftInPlace(width - size, padding) appendSeq(self)
54 docSlot("alignCenter(width, [padding])", """
55 Example:
56 <pre>
57 Io> "abc" alignCenter(10, "-")
58 ==> ---abc----
59 Io> "abc" alignCenter(10, "-=")
60 ==> -=-abc-=-=
61 </pre>
62 """)
63 alignCenter := method(width, padding,
64 alignRight(((size + width) / 2) floor, padding) alignLeftInPlace(width, padding)
67 asSimpleString := method("\"" .. self asString .. "\"")
69 docSlot("split(optionalArg1, optionalArg2, ...)", """
70 Returns a list containing the non-empty sub-sequences of the receiver divided by the given arguments.
71 If no arguments are given the sequence is split on white space.
72 Examples:
73 <pre>
74 "a b c d" splitNoEmpties => list("a", "b", "c", "d")
75 "a***b**c*d" splitNoEmpties("*") => list("a", "b", "c", "d")
76 "a***b||c,d" splitNoEmpties("*", "|", ",") => list("a", "b", "c", "d")
77 </pre>
78 """)
80 splitNoEmpties := method(
81 self performWithArgList("split", call evalArgs) selectInPlace(size != 0)
84 docSlot("findNthSeq(aSequence, n)", "Returns a number with the nth occurence of aSequence")
85 findNthSeq := method(str, n,
86 num := self findSeq(str)
87 if(num isNil, return nil)
88 if(n == 1, return num)
89 num + self slice(num + 1, self size) findNthSeq(str, n - 1)
93 orderedSplit := method(
94 separators := call evalArgs
95 if(separators size == 0, return list(self))
96 i := 0
97 skipped := 0
98 r := list
99 separators foreach(separator,
100 j := findSeq(separator, i) ifNil(
101 skipped += 1
102 continue
104 r append(slice(i, j))
105 if(skipped > 0,
106 skipped repeat(r append(nil))
107 skipped = 0
109 i = j + separator size
111 if(size == 0,
112 r append(nil)
114 r append(slice(i))
116 skipped repeat(r append(nil))
120 docSlot("prependSeq(object1, object2, ...)", "Prepends given objects asString in reverse order to the receiver. Returns self.")
121 prependSeq := method(self atInsertSeq(0, call evalArgs join); self)
123 sequenceSets := Map clone do(
124 atPut("lowercaseSequence",
125 lst := list
126 "abcdefghijklmnopqrstuvwxyz" foreach(v, lst append(v))
129 atPut("uppercaseSequence",
130 lst := list
131 at("lowercaseSequence") foreach(v, lst append(v asUppercase))
134 atPut("digitSequence",
135 lst := list
136 "0123456789" foreach(v, lst append(v))
139 removeSlot("lst")
140 removeSlot("v")
143 docSlot("asHex", "Returns a hex string for the receiving sequence, e.g., \"abc\" asHex -> \"616263\".")
144 asHex := method(
145 r := Sequence clone
146 self foreach(c, r appendSeq(c asHex alignRight(2, "00")))