|
@@ -1,208 +0,0 @@
|
|
-<html>
|
|
|
|
- <head>
|
|
|
|
- <meta charset="utf-8">
|
|
|
|
- <meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
- <title>Gravity: List</title>
|
|
|
|
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
|
|
|
|
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
|
|
|
|
- <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
|
|
|
|
- <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
|
|
|
|
- <link rel="stylesheet" href="stylesheets/styles.css">
|
|
|
|
- </head>
|
|
|
|
- <body>
|
|
|
|
-
|
|
|
|
- <!-- BEGIN NAVIGATION BAR -->
|
|
|
|
- <nav class="navbar navbar-default navbar-fixed-top">
|
|
|
|
- <div class="container">
|
|
|
|
- <div class="navbar-header">
|
|
|
|
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
|
|
|
|
- <span class="sr-only">Toggle navigation</span>
|
|
|
|
- <span class="icon-bar"></span>
|
|
|
|
- <span class="icon-bar"></span>
|
|
|
|
- <span class="icon-bar"></span>
|
|
|
|
- </button>
|
|
|
|
- <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
|
|
|
|
- </div>
|
|
|
|
- <div id="navbar" class="navbar-collapse collapse">
|
|
|
|
- <ul class="nav navbar-nav navbar-right">
|
|
|
|
- <li class="active"><a href="index.html">Gravity</a></li>
|
|
|
|
- <li><a href="internals/index.html">Internals</a></li>
|
|
|
|
- <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
|
|
|
|
- </ul>
|
|
|
|
- </div><!--/.nav-collapse -->
|
|
|
|
- </div>
|
|
|
|
- </nav>
|
|
|
|
- <!-- END NAVIGATION BAR -->
|
|
|
|
-
|
|
|
|
- <div class="container body-container">
|
|
|
|
- <div class="main-content">
|
|
|
|
- <div class="row">
|
|
|
|
-
|
|
|
|
- <!-- BEGIN SIDEBAR -->
|
|
|
|
- <div class="col-sm-3 border-right section-left">
|
|
|
|
- <div saveheight="1" class="sidebar-nav">
|
|
|
|
- <h4>INTRODUCTION</h4>
|
|
|
|
- <ul>
|
|
|
|
- <li><a href="index.html"><span>Introduction</span></a></li>
|
|
|
|
- <li><a href="getting-started.html"><span>Getting Started</span></a></li>
|
|
|
|
- </ul>
|
|
|
|
- <h4>LANGUAGE GUIDE</h4>
|
|
|
|
- <ul>
|
|
|
|
- <li><a href="syntax.html"><span>Syntax</span></a></li>
|
|
|
|
- <li><a href="operators.html"><span>Operators</span></a></li>
|
|
|
|
- <li><a href="types.html"><span>Types</span></a></li>
|
|
|
|
- <li><a href="lists.html" class="active"><span>Lists</span></a></li>
|
|
|
|
- <li><a href="maps.html"><span>Maps</span></a></li>
|
|
|
|
- <li><a href="enum.html"><span>Enum</span></a></li>
|
|
|
|
- <li><a href="functions.html"><span>Functions</span></a></li>
|
|
|
|
- <li><a href="closures.html"><span>Closures</span></a></li>
|
|
|
|
- <li><a href="classes.html"><span>Classes</span></a></li>
|
|
|
|
- <li><a href="control-flow.html"><span>Control Flow</span></a></li>
|
|
|
|
- <li><a href="loops.html"><span>Loops</span></a></li>
|
|
|
|
- <li><a href="fibers.html"><span>Fibers</span></a></li>
|
|
|
|
- </ul>
|
|
|
|
- <h4>ADVANCED</h4>
|
|
|
|
- <ul>
|
|
|
|
- <li><a href="api.html"><span>Embedding API</span></a></li>
|
|
|
|
- <li><a href="system.html"><span>System class</span></a></li>
|
|
|
|
- <li><a href="math.html"><span>Math class</span></a></li>
|
|
|
|
- <li><a href="unit-test.html"><span>Unit test</span></a></li>
|
|
|
|
- <li><a href="contributing.html"><span>Contributing</span></a></li>
|
|
|
|
- </ul>
|
|
|
|
- </div>
|
|
|
|
- </div>
|
|
|
|
- <!-- END SIDEBAR -->
|
|
|
|
-
|
|
|
|
- <div class="col-sm-9 border-left section-right">
|
|
|
|
- <h1 class="section-header">List</h1><hr>
|
|
|
|
-
|
|
|
|
- <p class="section-content">
|
|
|
|
- Lists (or arrays) are simple sequence of objects, their size is dynamic and their index starts always at 0. They provide fast random access to their elements. You can create a list by placing a sequence of comma-separated expressions inside square brackets:
|
|
|
|
- </p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var r = [1, 2, "Hello", 3.1415, true];
|
|
|
|
-
|
|
|
|
- // list has a count property
|
|
|
|
- var n = r.count; // n is 5
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">Accessing elements</h4>
|
|
|
|
- <p>You can access an element from a list by calling the subscript operator [] on it with the index of the element you want. Like most languages, indices start at 0:
|
|
|
|
- </p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var names = ["Mark", "Andrew", "Paul", "Ross", "Frank", "Max"];
|
|
|
|
- names[0]; // "Mark"
|
|
|
|
- names[2]; // "Paul"
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <p>Negative indices count backwards from the end:</p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var names = ["Mark", "Andrew", "Paul", "Ross", "Frank", "Max"];
|
|
|
|
- names[-1]; // "Max"
|
|
|
|
- names[-2]; // "Frank"
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">Iterating elements</h4>
|
|
|
|
- <p>The subscript operator works well for finding values when you know the key you’re looking for, but sometimes you want to see everything that’s in the list. Since the List class implements the iterator method, you can easily use it in a for loop:</p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var people = ["Mark", "Andrew", "Paul", "Ross", "Frank", "Max"];
|
|
|
|
- for (var name in people) {
|
|
|
|
- System.print("Current name is " + name);
|
|
|
|
- }
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">Adding elements</h4>
|
|
|
|
- <p>A List instance can be expanded by setting an index that is greater than the current size of the list:</p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var list = [10,20,30,40,50];
|
|
|
|
- list[30] = 22; // list contains now 31 elements (index 0...30)
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">List as a stack</h4>
|
|
|
|
- <p>The List class implements the push/pop methods as a convenient way to treat a list as a stack:</p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var list = [10,20,30,40,50];
|
|
|
|
- list.push(100); // add 100 to the list
|
|
|
|
- var v1 = list.pop(); // pop 100
|
|
|
|
- var v2 = list.pop(); // pop 50
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">List Contains</h4>
|
|
|
|
- <p>The List class implements the contains methods as a convenient way to check for the existence of a value in a list:</p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var list = [1, 2, "Hello", 3.1415, true];
|
|
|
|
- return list.contains(3.1415); // Returns: true
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">List Joins</h4>
|
|
|
|
- <p>The List class implements the join method as a convenient way to
|
|
|
|
- interpret a list as a string:</p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var list = [1,2,3,4,5];
|
|
|
|
- list.join(" + "); // Becomes: "1 + 2 + 3 + 4 + 5"
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">List Map</h4>
|
|
|
|
- <p>The List class implements the map method as a convenient way to
|
|
|
|
- create a new list using the current values of a list in some defined way:
|
|
|
|
- </p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var numbers = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
-
|
|
|
|
- var squared = numbers.map(func(num) {
|
|
|
|
- return num*num
|
|
|
|
- })
|
|
|
|
- // squared is now equal to [1,4,9,16,25,36,49,64,81,100]
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">List Filter</h4>
|
|
|
|
- <p>The List class implements the filter method as a convenient way to
|
|
|
|
- create a new list that contains the elements of the original list which
|
|
|
|
- passed a specified test:
|
|
|
|
- </p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var numbers = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
-
|
|
|
|
- var even = numbers.map(func(num) {
|
|
|
|
- return !(num % 2)
|
|
|
|
- })
|
|
|
|
- // even is now equal to [2,4,6,8,10]
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- <h4 class="section-h4">List Reduce</h4>
|
|
|
|
- <p>The List class implements the reduce method as a convenient way to
|
|
|
|
- create a new list reduces a list to a single value based on a provided
|
|
|
|
- callback:
|
|
|
|
- </p>
|
|
|
|
- <pre><code class="swift">
|
|
|
|
- var numbers = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
-
|
|
|
|
- var sum = numbers.reduce(0, func(num1, num2) {
|
|
|
|
- return num1+num2
|
|
|
|
- })
|
|
|
|
- // sum is now equal to 55
|
|
|
|
- </code></pre>
|
|
|
|
-
|
|
|
|
- </div> <!-- /row -->
|
|
|
|
- </div> <!-- /main-content -->
|
|
|
|
- </div> <!-- /container -->
|
|
|
|
-
|
|
|
|
- <!-- BEGIN FOOTER -->
|
|
|
|
- <footer class="navbar-fixed-bottom">
|
|
|
|
- <div class="container footer">
|
|
|
|
- <p>
|
|
|
|
- </p>
|
|
|
|
- </div>
|
|
|
|
- </footer>
|
|
|
|
- <!-- END FOOTER -->
|
|
|
|
-
|
|
|
|
- <!-- Bootstrap JS -->
|
|
|
|
- <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
|
|
|
|
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
|
|
|
|
-
|
|
|
|
- <!-- Highlights JS -->
|
|
|
|
- <script src="scripts/highlight/highlight.min.js"></script>
|
|
|
|
- <script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
-
|
|
|
|
- </body>
|
|
|
|
-</html>
|
|
|