# var x: Collection[U]
# # ...
# for u in x do
-# # u is a U
-# # ...
+# # u is a U
+# # ...
# end
# ~~~
#
# var x: Collection[U]
# # ...
# var i = x.iterator
-# while i.is_ok do
+# while i.is_ok do
# var u = i.item # u is a U
# # ...
# i.next
# How many occurrences of `item` are in the collection?
# Comparisons are done with ==
#
- # assert [10,20,10].count(10) == 2
+ # assert [10,20,10].count(10) == 2
fun count(item: E): Int
do
var nb = 0
# Return the first item of the collection
#
- # assert [1,2,3].first == 1
+ # assert [1,2,3].first == 1
fun first: E
do
assert length > 0
# Does the collection contain at least each element of `other`?
#
- # assert [1,3,4,2].has_all([1..2]) == true
- # assert [1,3,4,2].has_all([1..5]) == false
+ # assert [1,3,4,2].has_all([1..2]) == true
+ # assert [1,3,4,2].has_all([1..5]) == false
#
# Repeated elements in the collections are not considered.
#
- # assert [1,1,1].has_all([1]) == true
- # assert [1..5].has_all([1,1,1]) == true
+ # assert [1,1,1].has_all([1]) == true
+ # assert [1..5].has_all([1,1,1]) == true
#
# Note that the default implementation is general and correct for any lawful Collections.
# It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
# The same elements must be present in both `self` and `other`,
# but the order of the elements in the collections are not considered.
#
- # assert [1..3].has_exactly([3,1,2]) == true # the same elements
- # assert [1..3].has_exactly([3,1]) == false # 2 is not in the array
- # assert [1..2].has_exactly([3,1,2]) == false # 3 is not in the range
+ # assert [1..3].has_exactly([3,1,2]) == true # the same elements
+ # assert [1..3].has_exactly([3,1]) == false # 2 is not in the array
+ # assert [1..2].has_exactly([3,1,2]) == false # 3 is not in the range
#
# Repeated elements must be present in both collections in the same amount.
# So basically it is a multi-set comparison.
#
- # assert [1,2,3,2].has_exactly([1,2,2,3]) == true # the same elements
- # assert [1,2,3,2].has_exactly([1,2,3]) == false # more 2 in the first array
- # assert [1,2,3].has_exactly([1,2,2,3]) == false # more 2 in the second array
+ # assert [1,2,3,2].has_exactly([1,2,2,3]) == true # the same elements
+ # assert [1,2,3,2].has_exactly([1,2,3]) == false # more 2 in the first array
+ # assert [1,2,3].has_exactly([1,2,2,3]) == false # more 2 in the second array
#
# Note that the default implementation is general and correct for any lawful Collections.
# It is memory-efficient but relies on `count` so may be CPU-inefficient for some kind of collections.
#
# returns a new array built by concatenating `self` `repeat` times.
#
- # var a = [1,2,3]
- # assert (a * 0).is_empty
- # assert a * 1 == [1,2,3]
- # assert a * 2 == [1,2,3,1,2,3]
- # assert (a * 10).length == 30
+ # var a = [1,2,3]
+ # assert (a * 0).is_empty
+ # assert a * 1 == [1,2,3]
+ # assert a * 2 == [1,2,3,1,2,3]
+ # assert (a * 10).length == 30
fun *(repeat: Int): Array[E]
do
assert repeat >= 0
redef class Int
# Returns the range from 0 to `self-1`, is used to do:
#
- # var s = new Array[String]
- # for i in 3.times do s.add "cool"
- # assert s.join(" ") == "cool cool cool"
+ # var s = new Array[String]
+ # for i in 3.times do s.add "cool"
+ # assert s.join(" ") == "cool cool cool"
#
- # s.clear
- # for i in 10.times do s.add(i.to_s)
- # assert s.to_s == "0123456789"
+ # s.clear
+ # for i in 10.times do s.add(i.to_s)
+ # assert s.to_s == "0123456789"
fun times: Range[Int] do return [0 .. self[
end
# The character whose ASCII value is `self`.
#
- # assert 65.ascii == 'A'
- # assert 10.ascii == '\n'
+ # assert 65.ascii == 'A'
+ # assert 10.ascii == '\n'
fun ascii: Char is intern
# Number of digits of an integer in base `b` (plus one if negative)
# Returns the result of a binary AND operation on `self` and `i`
#
- # assert 0x10.bin_and(0x01) == 0
+ # assert 0x10.bin_and(0x01) == 0
fun bin_and(i: Int): Int is extern "kernel_Int_Int_binand_0"
# Returns the result of a binary OR operation on `self` and `i`
#
- # assert 0x10.bin_or(0x01) == 0x11
+ # assert 0x10.bin_or(0x01) == 0x11
fun bin_or(i: Int): Int is extern "kernel_Int_Int_binor_0"
# Returns the result of a binary XOR operation on `self` and `i`
#
- # assert 0x101.bin_xor(0x110) == 0x11
+ # assert 0x101.bin_xor(0x110) == 0x11
fun bin_xor(i: Int): Int is extern "kernel_Int_Int_binxor_0"
# Returns the 1's complement of `self`
#
- # assert 0x2F.bin_not == -48
+ # assert 0x2F.bin_not == -48
fun bin_not: Int is extern "kernel_Int_Int_binnot_0"
# Returns the square root of `self`
#
- # assert 16.sqrt == 4
+ # assert 16.sqrt == 4
fun sqrt: Int `{ return sqrt(recv); `}
# Returns the greatest common divisor of `self` and `o`
# Is `self` even ?
#
- # assert 12.is_even
+ # assert 12.is_even
fun is_even: Bool do return self % 2 == 0
# Is `self` odd ?
#
- # assert not 13.is_even
+ # assert not 13.is_even
fun is_odd: Bool do return not is_even
# Returns the `self` raised to the power of `e`.
#
- # assert 2 ** 3 == 8
+ # assert 2 ** 3 == 8
fun **(e: Int): Int
do
return self.to_f.pow(e.to_f).to_i
#
# Returns `1 * 2 * 3 * ... * self-1 * self`
#
- # assert 0.factorial == 1 # by convention for an empty product
- # assert 1.factorial == 1
- # assert 4.factorial == 24
- # assert 9.factorial == 362880
+ # assert 0.factorial == 1 # by convention for an empty product
+ # assert 1.factorial == 1
+ # assert 4.factorial == 24
+ # assert 9.factorial == 362880
fun factorial: Int
do
assert self >= 0
#
# SEE : `Char::is_letter` for the definition of letter.
#
- # assert "jAVASCRIPT".capitalized == "Javascript"
- # assert "i am root".capitalized == "I Am Root"
- # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
+ # assert "jAVASCRIPT".capitalized == "Javascript"
+ # assert "i am root".capitalized == "I Am Root"
+ # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
fun capitalized: SELFTYPE do
if length == 0 then return self
#
# SEE: `Char::is_letter` for the definition of a letter.
#
- # var b = new FlatBuffer.from("jAVAsCriPt")
- # b.capitalize
- # assert b == "Javascript"
- # b = new FlatBuffer.from("i am root")
- # b.capitalize
- # assert b == "I Am Root"
- # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
- # b.capitalize
- # assert b == "Ab_C -Ab0C Ab\nC"
+ # var b = new FlatBuffer.from("jAVAsCriPt")
+ # b.capitalize
+ # assert b == "Javascript"
+ # b = new FlatBuffer.from("i am root")
+ # b.capitalize
+ # assert b == "I Am Root"
+ # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
+ # b.capitalize
+ # assert b == "Ab_C -Ab0C Ab\nC"
fun capitalize do
if length == 0 then return
var c = self[0].to_upper
# The class name of the object.
#
- # assert 5.class_name == "Int"
+ # assert 5.class_name == "Int"
fun class_name: String do return native_class_name.to_s
# Developer readable representation of `self`.
# Returns true if the char is a numerical digit
#
- # assert '0'.is_numeric
- # assert '9'.is_numeric
- # assert not 'a'.is_numeric
- # assert not '?'.is_numeric
+ # assert '0'.is_numeric
+ # assert '9'.is_numeric
+ # assert not 'a'.is_numeric
+ # assert not '?'.is_numeric
fun is_numeric: Bool
do
return self >= '0' and self <= '9'
# Returns true if the char is an alpha digit
#
- # assert 'a'.is_alpha
- # assert 'Z'.is_alpha
- # assert not '0'.is_alpha
- # assert not '?'.is_alpha
+ # assert 'a'.is_alpha
+ # assert 'Z'.is_alpha
+ # assert not '0'.is_alpha
+ # assert not '?'.is_alpha
fun is_alpha: Bool
do
return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
# Returns true if the char is an alpha or a numeric digit
#
- # assert 'a'.is_alphanumeric
- # assert 'Z'.is_alphanumeric
- # assert '0'.is_alphanumeric
- # assert '9'.is_alphanumeric
- # assert not '?'.is_alphanumeric
+ # assert 'a'.is_alphanumeric
+ # assert 'Z'.is_alphanumeric
+ # assert '0'.is_alphanumeric
+ # assert '9'.is_alphanumeric
+ # assert not '?'.is_alphanumeric
fun is_alphanumeric: Bool
do
return self.is_numeric or self.is_alpha