Recently, I have come into contact with several languages in my work, and when I go back and forth between multiple languages, the simple syntax may get confused.
For the sake of organizing my mind, this time I will look at how to count the number of elements in an array in some languages.
All of them are shallow, so I hope that professionals in each language will take a calm look. : pray:
▶︎ Go
The Go language provides len ()
as a built-in function.
Therefore, you can get the number of elements in the array in the form len (array)
.
Also, in Go, there are Array type and Slice type as array-like types, The Array type of Go has a fixed number of elements, and it seems that the Slice type is closer when used like an array in other languages. Therefore, Slice is also used in the following code example.
package main
import (
"fmt"
)
func main() {
languages := []string{"Go", "JavaScript", "PHP", "Python", "Ruby", "Swift"}
fmt.Println(len(languages)) // -> 6
}
Go Playground: https://play.golang.org/p/r6yPBOU5uC
(Author execution version: Runtime version of Go Playfround → https://play.golang.org/p/1VcPUlPk_3)
In addition to Array and Slice, map, String, Channel, etc. can be taken as the argument of len, and it seems that the length that matches the type is returned. (Array and map are the number of elements, String is the number of bytes, etc.)
https://golang.org/pkg/builtin/#len
▶︎ JavaScript
In JavaScript, Array has a length
property.
Therefore, you can get the number of elements with ʻarray.length`.
var languages = ["Go", "JavaScript", "PHP", "Python", "Ruby", "Swift"];
console.log(languages.length); // -> 6
It is the length
property of Array, but it seems that it is writable, and if you shorten the length, the elements in the array also change.
I knew this for the first time.
languages.length = 3;
console.log(languages); // -> ["Go", "JavaScript", "PHP"]
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/length
▶︎ PHP
PHP provides a function called count ()
.
Therefore, you can get the number of elements in the array in the form count ($ array)
.
$languages = array("Go", "JavaScript", "PHP", "Python", "Ruby", "Swift");
// PHP 5.After 4 it is OK to write as below
// $languages = ["Go", "JavaScript", "PHP", "Python", "Ruby", "Swift"];
echo(count($languages)); // -> 6
(Author running version: PHP 5.3.27) Old. Lol
There is also an alias for count ()
called sizeof ()
, so it's exactly the same.
It's similar to Go in passing an array as an argument to a built-in function, but unlike Go, passing a String doesn't return the number of bytes.
http://php.net/manual/ja/function.count.php
▶︎ Python
In Python, the word array is not used in the first place, and the equivalent of an array is called a list.
To get the number of elements in the list, use the built-in function len ()
.
languages = ["Go", "JavaScript", "PHP", "Python", "Ruby", "Swift"]
print len(languages) # -> 6
(Author execution version: Python 2.7.12)
In the reference
Arguments can be sequences (strings, bytes, tuples, lists, ranges, etc.) or collections (dictionaries, sets, frozen sets, etc.).
So, this is similar to Go, and you can see that it returns the length of each other than the list.
2 system http://docs.python.jp/2/library/functions.html#len 3 series http://docs.python.jp/3/library/functions.html#len
▶︎ Ruby
In Ruby, the Array class has a length
method and asize
method, both of which can get the number of elements.
(The size method seems to be an alias for the length method)
Also, since the Enumerable module included in the Array class has a count
method, you can get the number of elements in the array by using the count method with no arguments in the Array class as well.
languages = ["Go", "JavaScript", "PHP", "Python", "Ruby", "Swift"]
p languages.length # -> 6
p languages.size # -> 6
p languages.count # -> 6
(Author execution version: ruby 2.3.0)
I think the following article will be helpful for the difference between the length method and the count method. http://qiita.com/saino-katsutoshi/items/7d761c026563a649d046
http://ruby-doc.org/core-2.3.0/Array.html#method-i-length
▶︎ Swift
In Swift, the Array structure has a count
property, so you can get the number of elements from this count property.
let languages = ["Go", "JavaScript", "PHP", "Python", "Ruby", "Swift"]
print(languages.count) // -> 6
(Author execution version: Swift 3.0)
The Array structure conforms to the Collection
protocol, so the count property must be defined.
It's also similar to JavaScript's length
property in terms of properties, but Swift's count
property is read-only, so you can't rewrite its value.
https://developer.apple.com/reference/swift/array/1539982-count
First of all,
--Language where functions / properties that return the number of elements in the array class (etc.) are defined, such as ʻarray_object.count_method_or_property**, and --Language that has a function that returns the length of the object passed as an argument as a built-in function ** like
count_function (array_object)` **
There seems to be.
Furthermore, even if it is divided, "property or method" is different, and "whether the content of the value returned depends on the type passed to the argument" is different depending on the language.
It's interesting that it depends on the language whether or not the array object keeps track of the number of its elements.
Also, there are still many parts that I haven't learned deeply about each language, so I'll study little by little.