This file is a concatenation of [Learn X in Y minutes](https://github.com/adambard/learnxinyminutes-docs) for testing purposes.

It is published under the original project license ([Creative Commons Attribution-ShareAlike 3.0 Unported](http://creativecommons.org/licenses/by-sa/3.0/deed.en_US)).

------
category: tool
tool: amd
contributors:
    - ["Frederik Ring", "https://github.com/m90"]
filename: learnamd.js
---

## Getting Started with AMD

The **Asynchronous Module Definition** API specifies a mechanism for defining
JavaScript modules such that the module and its dependencies can be asynchronously
loaded. This is particularly well suited for the browser environment where
synchronous loading of modules incurs performance, usability, debugging, and
cross-domain access problems.

### Basic concept
```javascript
// The basic AMD API consists of nothing but two methods: `define` and `require`
// and is all about module definition and consumption:
// `define(id?, dependencies?, factory)` defines a module
// `require(dependencies, callback)` imports a set of dependencies and
// consumes them in the passed callback

// Let's start by using define to define a new named module
// that has no dependencies. We'll do so by passing a name
// and a factory function to define:
define('awesomeAMD', function(){
  var isAMDAwesome = function(){
    return true;
  };
  // The return value of a module's factory function is
  // what other modules or require calls will receive when
  // requiring our `awesomeAMD` module.
  // The exported value can be anything, (constructor) functions,
  // objects, primitives, even undefined (although that won't help too much).
  return isAMDAwesome;
});

// Now, let's define another module that depends upon our `awesomeAMD` module.
// Notice that there's an additional argument defining our
// module's dependencies now:
define('loudmouth', ['awesomeAMD'], function(awesomeAMD){
  // dependencies will be passed to the factory's arguments
  // in the order they are specified
  var tellEveryone = function(){
    if (awesomeAMD()){
      alert('This is sOoOo rad!');
    } else {
      alert('Pretty dull, isn\'t it?');
    }
  };
  return tellEveryone;
});

// As we do know how to use define now, let's use `require` to
// kick off our program. `require`'s signature is `(arrayOfDependencies, callback)`.
require(['loudmouth'], function(loudmouth){
  loudmouth();
});

// To make this tutorial run code, let's implement a very basic
// (non-asynchronous) version of AMD right here on the spot:
function define(name, deps, factory){
  // notice how modules without dependencies are handled
  define[name] = require(factory ? deps : [], factory || deps);
}

function require(deps, callback){
  var args = [];
  // first let's retrieve all the dependencies needed
  // by the require call
  for (var i = 0; i < deps.length; i++){
    args[i] = define[deps[i]];
  }
  // satisfy all the callback's dependencies
  return callback.apply(null, args);
}
// you can see this code in action here: http://jsfiddle.net/qap949pd/
```

### Real-world usage with require.js

In contrast to the introductory example, `require.js` (the most popular AMD library) actually implements the **A** in **AMD**, enabling you to load modules and their dependencies asynchronously via XHR:

```javascript
/* file: app/main.js */
require(['modules/someClass'], function(SomeClass){
  // the callback is deferred until the dependency is loaded
  var thing = new SomeClass();
});
console.log('So here we are, waiting!'); // this will run first
```

By convention, you usually store one module in one file. `require.js` can resolve module names based on file paths, so you don't have to name your modules, but can simply reference them using their location. In the example `someClass` is assumed to be in the `modules` folder, relative to your configuration's `baseUrl`:

* app/
  * main.js
  * modules/
    * someClass.js
    * someHelpers.js
    * ...
  * daos/
    * things.js
    * ...

This means we can define `someClass` without specifying a module id:

```javascript
/* file: app/modules/someClass.js */
define(['daos/things', 'modules/someHelpers'], function(thingsDao, helpers){
  // module definition, of course, will also happen asynchronously
  function SomeClass(){
    this.method = function(){/**/};
    // ...
  }
  return SomeClass;
});
```
To alter the default path mapping behavior use `requirejs.config(configObj)` in your `main.js`:

```javascript
/* file: main.js */
requirejs.config({
  baseUrl : 'app',
  paths : {
    // you can also load modules from other locations
    jquery : '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
});
require(['jquery', 'coolLibFromBower', 'modules/someHelpers'], function($, coolLib, helpers){
  // a `main` file needs to call require at least once,
  // otherwise no code will ever run
  coolLib.doFancyStuffWith(helpers.transform($('#foo')));
});
```
`require.js`-based apps will usually have a single entry point (`main.js`) that is passed to the `require.js` script tag as a data-attribute. It will be automatically loaded and executed on pageload:

```html
<!DOCTYPE html>
<html>
<head>
  <title>A hundred script tags? Never again!</title>
</head>
<body>
  <script src="require.js" data-main="app/main"></script>
</body>
</html>
```

### Optimizing a whole project using r.js

Many people prefer using AMD for sane code organization during development, but still want to ship a single script file in production instead of performing hundreds of XHRs on page load.

`require.js` comes with a script called `r.js` (that you will probably run in node.js, although Rhino is supported too) that can analyse your project's dependency graph, and build a single file containing all your modules (properly named), minified and ready for consumption.

Install it using `npm`:
```shell
$ npm install requirejs -g
```

Now you can feed it with a configuration file:
```shell
$ r.js -o app.build.js
```

For our above example the configuration might look like:
```javascript
/* file : app.build.js */
({
  name : 'main', // name of the entry point
  out : 'main-built.js', // name of the file to write the output to
  baseUrl : 'app',
  paths : {
    // `empty:` tells r.js that this should still be loaded from the CDN, using
    // the location specified in `main.js`
    jquery : 'empty:',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
})
```

To use the built file in production, simply swap `data-main`:
```html
<script src="require.js" data-main="app/main-built"></script>
```

An incredibly detailed [overview of build options](https://github.com/jrburke/r.js/blob/master/build/example.build.js) is available in the GitHub repo.

### Topics not covered in this tutorial
* [Loader plugins / transforms](http://requirejs.org/docs/plugins.html)
* [CommonJS style loading and exporting](http://requirejs.org/docs/commonjs.html)
* [Advanced configuration](http://requirejs.org/docs/api.html#config)
* [Shim configuration (loading non-AMD modules)](http://requirejs.org/docs/api.html#config-shim)
* [CSS loading and optimizing with require.js](http://requirejs.org/docs/optimization.html#onecss)
* [Using almond.js for builds](https://github.com/jrburke/almond)

### Further reading:

* [Official Spec](https://github.com/amdjs/amdjs-api/wiki/AMD)
* [Why AMD?](http://requirejs.org/docs/whyamd.html)
* [Universal Module Definition](https://github.com/umdjs/umd)

### Implementations:

* [require.js](http://requirejs.org)
* [dojo toolkit](http://dojotoolkit.org/documentation/tutorials/1.9/modules/)
* [cujo.js](http://cujojs.com/)
* [curl.js](https://github.com/cujojs/curl)
* [lsjs](https://github.com/zazl/lsjs)
* [mmd](https://github.com/alexlawrence/mmd)
---
category: tool
tool: AngularJS
contributors:
    - ["Walter Cordero", "http://waltercordero.com"]
filename: learnangular.html
---

## AngularJS Tutorial.

AngularJS version 1.0 was released in 2012.
Miško Hevery, a Google employee, started to work with AngularJS in 2009.
The idea turned out very well, and the project is now officially supported by Google.

AngularJS is a JavaScript framework. It can be added to an HTML page with a "script" tag.
AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.

##What You Should Already Know

Before you study AngularJS, you should have a basic understanding of:

- HTML
- CSS
- JavaScript

```html
// AngularJS is a JavaScript framework. It is a library written in JavaScript.
// AngularJS is distributed as a JavaScript file, and can be added to a web page with a script tag:
// <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

///////////////////////////////////
// AngularJS Extends HTML

//AngularJS extends HTML with ng-directives.
//The ng-app directive defines an AngularJS application.
//The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
//The ng-bind directive binds application data to the HTML view.
<!DOCTYPE html>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div ng-app="">
      <p>Name: <input type="text" ng-model="name"></p>
      <p ng-bind="name"></p>
    </div>
  </body>
</html>

/*
  * Example explained:
  * AngularJS starts automatically when the web page has loaded.
  * The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application.
  * The ng-model directive binds the value of the input field to the application variable name.
  * The ng-bind directive binds the innerHTML of the <p> element to the application variable name.
*/
<tag> Here are content to be interpreted </tag>

///////////////////////////////////
// AngularJS Expressions

// AngularJS expressions are written inside double braces: {{ expression }}.
// AngularJS expressions binds data to HTML the same way as the ng-bind directive.
// AngularJS will "output" data exactly where the expression is written.
// AngularJS expressions are much like JavaScript expressions: They can contain literals, operators, and variables.
// Example {{ 5 + 5 }} or {{ firstName + " " + lastName }}
<!DOCTYPE html>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div ng-app="">
      <p>My first expression: {{ 5 + 5 }}</p>
    </div>
  </body>
</html>

//If you remove the ng-app directive, HTML will display the expression as it is, without solving it:
<!DOCTYPE html>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div>
      <p>My first expression: {{ 5 + 5 }}</p>
    </div>
  </body>
</html>

// AngularJS expressions bind AngularJS data to HTML the same way as the ng-bind directive.
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div ng-app="">
      <p>Name: <input type="text" ng-model="name"></p>
      <p>{{name}}</p>
    </div>
  </body>
</html>

// AngularJS numbers are like JavaScript numbers:
<div ng-app="" ng-init="quantity=1;cost=5">
  <p>Total in dollar: {{ quantity * cost }}</p>
</div>

//AngularJS strings are like JavaScript strings:
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
  <p>The name is <span ng-bind="firstName + ' ' + lastName"></span></p>
</div>

//AngularJS objects are like JavaScript objects:
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
  <p>The name is {{ person.lastName }}</p>
</div>

//AngularJS arrays are like JavaScript arrays:
<div ng-app="" ng-init="points=[1,15,19,2,40]">
  <p>The third result is {{ points[2] }}</p>
</div>

// Like JavaScript expressions, AngularJS expressions can contain literals, operators, and variables.
// Unlike JavaScript expressions, AngularJS expressions can be written inside HTML.
// AngularJS expressions do not support conditionals, loops, and exceptions, while JavaScript expressions do.
// AngularJS expressions support filters, while JavaScript expressions do not.

///////////////////////////////////
// AngularJS Directives


//AngularJS directives are extended HTML attributes with the prefix ng-.
//The ng-app directive initializes an AngularJS application.
//The ng-init directive initializes application data.
//The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
<div ng-app="" ng-init="firstName='John'">
  <p>Name: <input type="text" ng-model="firstName"></p>
  <p>You wrote: {{ firstName }}</p>
</div>

//Using ng-init is not very common. You will learn how to initialize data in the chapter about controllers.

//The ng-repeat directive repeats an HTML element:
<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

//The ng-repeat directive used on an array of objects:
<div ng-app="" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
  <ul>
    <li ng-repeat="x  in names">
      {{ x.name + ', ' + x.country }}
    </li>
  </ul>
</div>

// AngularJS is perfect for database CRUD (Create Read Update Delete) applications.
// Just imagine if these objects were records from a database.

// The ng-app directive defines the root element of an AngularJS application.
// The ng-app directive will auto-bootstrap (automatically initialize) the application when a web page is loaded.
// Later you will learn how ng-app can have a value (like ng-app="myModule"), to connect code modules.

// The ng-init directive defines initial values for an AngularJS application.
// Normally, you will not use ng-init. You will use a controller or module instead.
// You will learn more about controllers and modules later.

//The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
//The ng-model directive can also:
//Provide type validation for application data (number, email, required).
//Provide status for application data (invalid, dirty, touched, error).
//Provide CSS classes for HTML elements.
//Bind HTML elements to HTML forms.

//The ng-repeat directive clones HTML elements once for each item in a collection (in an array).

///////////////////////////////////
// AngularJS Controllers

// AngularJS controllers control the data of AngularJS applications.
// AngularJS controllers are regular JavaScript Objects.

// AngularJS applications are controlled by controllers.
// The ng-controller directive defines the application controller.
// A controller is a JavaScript Object, created by a standard JavaScript object constructor.

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

//Application explained:

//The AngularJS application is defined by  ng-app="myApp". The application runs inside the <div>.
//The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller.
//The myCtrl function is a JavaScript function.
//AngularJS will invoke the controller with a $scope object.
//In AngularJS, $scope is the application object (the owner of application variables and functions).
//The controller creates two properties (variables) in the scope (firstName and lastName).
//The ng-model directives bind the input fields to the controller properties (firstName and lastName).

//The example above demonstrated a controller object with two properties: lastName and firstName.
//A controller can also have methods (variables as functions):
<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    $scope.fullName = function() {
        return $scope.firstName + " " + $scope.lastName;
    }
});
</script>

//In larger applications, it is common to store controllers in external files.
//Just copy the code between the <script> </script> tags into an external file named personController.js:

<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

<script src="personController.js"></script>

// For the next example we will create a new controller file:
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        {name:'Jani',country:'Norway'},
        {name:'Hege',country:'Sweden'},
        {name:'Kai',country:'Denmark'}
    ];
});

//Save the file as  namesController.js:
//And then use the controller file in an application:

<div ng-app="myApp" ng-controller="namesCtrl">

<ul>
  <li ng-repeat="x in names">
    {{ x.name + ', ' + x.country }}
  </li>
</ul>

</div>

<script src="namesController.js"></script>

///////////////////////////////////
// AngularJS Filters

// Filters can be added to expressions and directives using a pipe character.
// AngularJS filters can be used to transform data:

- **currency**:  Format a number to a currency format.
- **filter**:  Select a subset of items from an array.
- **lowercase**: Format a string to lower case.
- **orderBy**: Orders an array by an expression.
- **uppercase**: Format a string to upper case.

//A filter can be added to an expression with a pipe character (|) and a filter.
//(For the next two examples we will use the person controller from the previous chapter)
//The uppercase filter format strings to upper case:
<div ng-app="myApp" ng-controller="personCtrl">

<p>The name is {{ lastName | uppercase }}</p>

</div>

//The lowercase filter format strings to lower case:
<div ng-app="myApp" ng-controller="personCtrl">

<p>The name is {{ lastName | lowercase }}</p>

</div>

//The currency filter formats a number as currency:
<div ng-app="myApp" ng-controller="costCtrl">

<input type="number" ng-model="quantity">
<input type="number" ng-model="price">

<p>Total = {{ (quantity * price) | currency }}</p>

</div> 

//A filter can be added to a directive with a pipe character (|) and a filter.
//The orderBy filter orders an array by an expression:
<div ng-app="myApp" ng-controller="namesCtrl">

<ul>
  <li ng-repeat="x in names | orderBy:'country'">
    {{ x.name + ', ' + x.country }}
  </li>
</ul>

<div>

//An input filter can be added to a directive with a pipe character (|) 
//and filter followed by a colon and a model name.
//The filter selects a subset of an array:

<div ng-app="myApp" ng-controller="namesCtrl">

<p><input type="text" ng-model="test"></p>

<ul>
  <li ng-repeat="x in names | filter:test | orderBy:'country'">
    {{ (x.name | uppercase) + ', ' + x.country }}
  </li>
</ul>

</div>

///////////////////////////////////
// AngularJS AJAX - $http

//$http is an AngularJS service for reading data from remote servers.

// The following data can be provided by a web server:
// http://www.w3schools.com/angular/customers.php
// **Check the URL to see the data format**

// AngularJS $http is a core service for reading data from web servers.
// $http.get(url) is the function to use for reading server data.
<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul>
  <li ng-repeat="x in names">
    {{ x.Name + ', ' + x.Country }}
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function(response) {$scope.names = response.records;});
});
</script>

Application explained:

// The AngularJS application is defined by ng-app. The application runs inside a <div>.
// The ng-controller directive names the controller object.
// The customersCtrl function is a standard JavaScript object constructor.
// AngularJS will invoke customersCtrl with a $scope and $http object.
// $scope is the application object (the owner of application variables and functions).
// $http is an XMLHttpRequest object for requesting external data.
// $http.get() reads JSON data from http://www.w3schools.com/angular/customers.php.
// If success, the controller creates a property (names) in the scope, with JSON data from the server.


// Requests for data from a different server (than the requesting page), are called cross-site HTTP requests.
// Cross-site requests are common on the web. Many pages load CSS, images, and scripts from different servers.
// In modern browsers, cross-site HTTP requests from scripts are restricted to same site for security reasons.
// The following line, in our PHP examples, has been added to allow cross-site access.
header("Access-Control-Allow-Origin: *");


///////////////////////////////////
// AngularJS Tables

// Displaying tables with angular is very simple:
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function (response) {$scope.names = response.records;});
});
</script>

// To sort the table, add an orderBy filter: 
<table>
  <tr ng-repeat="x in names | orderBy : 'Country'">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

// To display the table index, add a <td> with $index: 
<table>
  <tr ng-repeat="x in names">
    <td>{{ $index + 1 }}</td>
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

// Using $even and $odd
<table>
  <tr ng-repeat="x in names">
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td>
    <td ng-if="$even">{{ x.Name }}</td>
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td>
    <td ng-if="$even">{{ x.Country }}</td>
  </tr>
</table>

///////////////////////////////////
// AngularJS HTML DOM

//AngularJS has directives for binding application data to the attributes of HTML DOM elements.

// The ng-disabled directive binds AngularJS application data to the disabled attribute of HTML elements.

<div ng-app="" ng-init="mySwitch=true">

<p>
<button ng-disabled="mySwitch">Click Me!</button>
</p>

<p>
<input type="checkbox" ng-model="mySwitch">Button
</p>

</div>

//Application explained:

// The ng-disabled directive binds the application data mySwitch to the HTML button's disabled attribute.
// The ng-model directive binds the value of the HTML checkbox element to the value of mySwitch.
// If the value of mySwitch evaluates to true, the button will be disabled: 
<p>
<button disabled>Click Me!</button>
</p>

// If the value of mySwitch evaluates to false, the button will not be disabled: 
<p>
  <button>Click Me!</button>
</p>

// The ng-show directive shows or hides an HTML element.

<div ng-app="">

<p ng-show="true">I am visible.</p>

<p ng-show="false">I am not visible.</p>

</div>

// The ng-show directive shows (or hides) an HTML element based on the value of ng-show.
// You can use any expression that evaluates to true or false:
<div ng-app="">
<p ng-show="hour > 12">I am visible.</p>
</div>

///////////////////////////////////
// AngularJS Events

// AngularJS has its own HTML events directives.

// The ng-click directive defines an AngularJS click event.
<div ng-app="myApp" ng-controller="myCtrl">

<button ng-click="count = count + 1">Click me!</button>

<p>{{ count }}</p>

</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.count = 0;
});
</script>

// The ng-hide directive can be used to set the visibility of a part of an application.
// The value ng-hide="true" makes an HTML element invisible.
// The value ng-hide="false" makes the element visible.
<div ng-app="myApp" ng-controller="personCtrl">

<button ng-click="toggle()">Toggle</button>

<p ng-hide="myVar">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John",
    $scope.lastName = "Doe"
    $scope.myVar = false;
    $scope.toggle = function() {
        $scope.myVar = !$scope.myVar;
    };
});
</script>

//Application explained:

// The first part of the personController is the same as in the chapter about controllers.
// The application has a default property (a variable): $scope.myVar = false;
// The ng-hide directive sets the visibility, of a <p> element with two input fields, 
// according to the value (true or false) of myVar.
// The function toggle() toggles myVar between true and false.
// The value ng-hide="true" makes the element invisible.


// The ng-show directive can also be used to set the visibility of a part of an application.
// The value ng-show="false" makes an HTML element invisible.
// The value ng-show="true" makes the element visible.
// Here is the same example as above, using ng-show instead of ng-hide:
<div ng-app="myApp" ng-controller="personCtrl">

<button ng-click="toggle()">Toggle</button>

<p ng-show="myVar">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John",
    $scope.lastName = "Doe"
    $scope.myVar = true;
    $scope.toggle = function() {
        $scope.myVar = !$scope.myVar;
    }
});
</script>

///////////////////////////////////
// AngularJS Modules

// An AngularJS module defines an application.
// The module is a container for the different parts of an application.
// The module is a container for the application controllers.
// Controllers always belong to a module.

// This application ("myApp") has one controller ("myCtrl"):

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

</body>
</html>

// It is common in AngularJS applications to put the module and the controllers in JavaScript files.
// In this example, "myApp.js" contains an application module definition, while "myCtrl.js" contains the controller:

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script src="myApp.js"></script>
<script src="myCtrl.js"></script>

</body>
</html>

//myApp.js
var app = angular.module("myApp", []); 

// The [] parameter in the module definition can be used to define dependent modules.

// myCtrl.js
app.controller("myCtrl", function($scope) {
    $scope.firstName  = "John";
    $scope.lastName= "Doe";
});

// Global functions should be avoided in JavaScript. They can easily be overwritten 
// or destroyed by other scripts.

// AngularJS modules reduces this problem, by keeping all functions local to the module.

// While it is common in HTML applications to place scripts at the end of the 
// <body> element, it is recommended that you load the AngularJS library either
// in the <head> or at the start of the <body>.

// This is because calls to angular.module can only be compiled after the library has been loaded.

<!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

</body>
</html>


///////////////////////////////////
// AngularJS Applications

// AngularJS modules define AngularJS applications.
// AngularJS controllers control AngularJS applications.
// The ng-app directive defines the application, the ng-controller directive defines the controller.
<div ng-app="myApp" ng-controller="myCtrl">
  First Name: <input type="text" ng-model="firstName"><br>
  Last Name: <input type="text" ng-model="lastName"><br>
  <br>
  Full Name: {{firstName + " " + lastName}}
</div>
<script>
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope) {
      $scope.firstName= "John";
      $scope.lastName= "Doe";
  });
</script>

// AngularJS modules define applications:
var app = angular.module('myApp', []);

// AngularJS controllers control applications:
app.controller('myCtrl', function($scope) {
    $scope.firstName= "John";
    $scope.lastName= "Doe";
});
```

## Source & References

**Examples**

- [http://www.w3schools.com/angular/angular_examples.asp](http://www.w3schools.com/angular/angular_examples.asp)

**References**

- [http://www.w3schools.com/angular/angular_ref_directives.asp](http://www.w3schools.com/angular/angular_ref_directives.asp)
- [http://www.w3schools.com/angular/default.asp](http://www.w3schools.com/angular/default.asp)
- [https://teamtreehouse.com/library/angular-basics/](https://teamtreehouse.com/library/angular-basics/)
---
language: html
lang: ar-ar
filename: learnhtml-tf.html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
translators:
    - ["Ader", "https://github.com/y1n0"]
---

HTML اختصار ل HyperText Markup Language، أي "لغة ترميز النص التشعبي". 
هي لغة تمكننا من كتابة صفحات موجهة لشبكة الويب العالمي. 
هي لغة توصيف للنص، تسمح بكتابة صفحات ويب عن طريق تحديد كيفية عرض النصوص والمعلومات. 
في الحقيقة، ملفات html هي ملفات تحتوي على نصوص بسيطة. 
ما هو توصيف النص هذا؟ هو طريقة لتنظيم معلومات النص عن طريق إحاطته بوُسوم فتح ووسوم غلق. 
هذه الوسوم تعطي معاني محددة للنص الذي تحيطه. 
كباقي لغات الحاسوب، هناك الكثير من إصدارات HTML. سنتحدث هنا عن HTLM5. 

**ملاحظة:** يمكنك تجريب مختلف الوسوم والعناصر بينما تقرأ الدرس عبر موقع كـ [codepen](http://codepen.io/pen/) حتى ترى تأثيرها وتعرف كيف تعمل وتتعود على استعمالها.
هذه المادة تُعنى أساسا بتركيب HTML .وبعض النصائح المفيدة


```html
<!-- التعاليق تحاط بوسوم كما في هذا السطر -->

<!-- #################### الوسوم #################### -->
   
<!-- هنا مثال لملف html الذي سنقوم بالعمل عليه. -->

<!doctype html>
	<html>
		<head>
			<title>موقعي</title>
		</head>
		<body>
			<h1>مرحبا بالعالم!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">الق نظرة كيف يبدو هذا من هنا</a>
			<p>هذه فقرة.</p>
			<p>هذه فقرة أخرى.</p>
			<ul>
				<li>هذا عنصر من لائحة غير مرقمة. (لائحة بالعرائض)</li>
				<li>هذا عنصر آخر</li>
				<li>وهذا آخر عنصر في اللائحة</li>
			</ul>
		</body>
	</html>

<!-- ملف HTML يُبتدأ دائما بتبيين أنه ملف HTML للمتصفح -->
<!doctype html>

<!-- بعد هذا، يبدأ بفتح الوسم <html> -->
<html>

<!-- الذي سيغلق في نهاية الملف بـ </html>. -->
</html>

<!-- لا يجب كتابة أي شيء بعد وسم النهاية ذاك. -->

<!-- داخل هذين الوسمين (<html></html>) نجد: -->

<!-- "ترئيس" محدد ب <head> (يجب أن يغلق بـ </head>) -->
<!-- الترأيس يحتوي على أوصاف وبعض المعلومات الإضافية التي لا تظهر في المتصفح, تدعي metadata (المعلومات الوصفية) -->

<head>
	<title>موقعي</title><!-- الوسم <title> يحدد للمتصفح العنوان الذي يظهر في المكان المخصص للعنوان في نافذة المتصفح. -->
</head>

<!-- بعد الجزء الخاص بـ <head>، نجد الوسم <body> -->
<!-- حتى هنا، لا شيء مما كُتب سيظهر في النافذة الرئيسية للمتصفح. -->
<!-- يجب ان نملأ "جسد" الصفحة بالمحتوى الذي نريد أن نُظهر -->

<body>
	<h1>مرحبا بالعالم!</h1> <!-- الوسم <h1> خاص بالعناوين الكبيرة. -->
	<!-- هناك أيضا وسوم خاصة بالعناوين الفرعية من h1، الأكثر أهمية h2 والذي يليه حتى h6 الذي هو آخر عنوان داخلي. -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">ألق نظرة كيف يبدو هذا من هنا</a> <!-- يظهر رابطا للصفحة التي داخل السمة href="" -->
	<p>هذه فقرة.</p> <!-- يمكن من اضافة نصوص للصفحة. يميز الفقرات -->
	<p>هذه فقرة أخرى.</p>
	<ul> <!-- الوسم <ul> يخلق لائحة بالعرائض -->
	<!-- إذا أردت لائحة مرقمة، هناك الوسم <ol>. ويكون الترتيب فيه حسب تموضع العناصر داخله، الأول فالأول. -->
		<li>هذا عنصر من لائحة غير مرقمة. (لائحة بالعرائض)</li>
		<li>هذا عنصر آخر</li>
		<li>وهذا آخر عنصر في اللائحة</li>
	</ul>
</body>

<!-- وهكذا، كتابة ملفات HTML جد بسيطة -->

<!-- يمكنك كذلك إضافة أنواع أخرى من الوسوم -->

<!-- لادخال صورة: -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- مصدر الصورة يحدد داخل السمة: src="" -->
<!-- مصدرها يمكن أن يكون رابطا أو مسارا لصورة في حاسوبك -->

<!-- يمكنك كذلك تشكيل جداول. -->

<table> <!-- نفتح الجدول بالوسم <table> -->
	<tr> <!-- <tr> تسمح بتشكيل صف. -->
		<th>العنوان الأول</th> <!-- <th> تسمح لنا بإعطاء عنوان لهذا العمود. -->
		<th>العنوان الثاني</th>
	</tr>
	<tr>
		<td>الصف الأول، العمود الأول</td> <!-- <td> تسمح بتشكيل الأعمدة، أو خانات داخل كل صف. -->
		<td>الصف الأول، العمود الثاني</td>
	</tr>
	<tr>
		<td>الصف الثاني، العمود الأول</td>
		<td>الصف الثاني، العمود الأول</td>
	</tr>
</table>

```

## الاستعمال

HTML يُكتب في ملفات تنتهي بـ `.html`.

## لمعرفة المزيد 

* [wikipedia](https://en.wikipedia.org/wiki/HTML)
* [HTML tutorial](https://developer.mozilla.org/en-US/docs/Web/HTML)
* [W3School](http://www.w3schools.com/html/html_intro.asp)
---
language: asciidoc
contributors:
    - ["Ryan Mavilia", "http://unoriginality.rocks/"]
filename: asciidoc.md
---

AsciiDoc is a markup language similar to Markdown and it can be used for anything from books to blogs. Created in 2002 by Stuart Rackham the language is simple but it allows for a great amount of customization.

Document Header

Headers are optional and can't contain blank lines. It must be offset from content by at least one blank line.

Title Only

```
= Document Title

First sentence of document.
```

Title and Author

```
= Document Title
First Last <first.last@learnxinyminutes.com>

Start of this document.
```

Multiple Authors

```
= Document Title
John Doe <john@go.com>; Jane Doe<jane@yo.com>; Black Beard <beardy@pirate.com>

Start of a doc with multiple authors.
```

Revision Line (requires an author line)

```
= Doc Title V1
Potato Man <chip@crunchy.com>
v1.0, 2016-01-13

This article about chips is going to be fun.
```

Paragraphs

```
You don't need anything special for paragraphs.

Add a blank line between paragraphs to separate them.

To create a line blank add a +
and you will receive a line break!
```

Formatting Text

```
_underscore creates italics_
*asterisks for bold*
*_combine for extra fun_*
`use ticks to signify monospace`
`*bolded monospace*`
```

Section Titles

```
= Level 0 (may only be used in document's header)

== Level 1 <h2>

=== Level 2 <h3>

==== Level 3 <h4>

===== Level 4 <h5>

====== Level 5 <h6>

======= Level 6  <h7>

```

Lists

To create a bulleted list use asterisks.

```
* foo
* bar
* baz
```

To create a numbered list use periods.

```
. item 1
. item 2
. item 3
```

You can nest lists by adding extra asterisks or periods up to five times.

```
* foo 1
** foo 2
*** foo 3
**** foo 4
***** foo 5

. foo 1
.. foo 2
... foo 3
.... foo 4
..... foo 5
```
---
category: Algorithms & Data Structures
name: Asymptotic Notation
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Divay Prakash", "http://github.com/divayprakash"]
---

# Asymptotic Notations

## What are they?

Asymptotic Notations are languages that allow us to analyze an algorithm's 
running time by identifying its behavior as the input size for the algorithm 
increases. This is also known as an algorithm's growth rate. Does the 
algorithm suddenly become incredibly slow when the input size grows? Does it 
mostly maintain its quick run time as the input size increases? Asymptotic 
Notation gives us the ability to answer these questions.

## Are there alternatives to answering these questions?

One way would be to count the number of primitive operations at different 
input sizes. Though this is a valid solution, the amount of work this takes 
for even simple algorithms does not justify its use.

Another way is to physically measure the amount of time an algorithm takes to 
complete given different input sizes. However, the accuracy and relativity 
(times obtained would only be relative to the machine they were computed on) 
of this method is bound to environmental variables such as computer hardware 
specifications, processing power, etc.

## Types of Asymptotic Notation

In the first section of this doc we described how an Asymptotic Notation 
identifies the behavior of an algorithm as the input size changes. Let us 
imagine an algorithm as a function f, n as the input size, and f(n) being 
the running time. So for a given algorithm f, with input size n you get 
some resultant run time f(n). This results in a graph where the Y axis is the 
runtime, X axis is the input size, and plot points are the resultants of the 
amount of time for a given input size.

You can label a function, or algorithm, with an Asymptotic Notation in many 
different ways. Some examples are, you can describe an algorithm by its best 
case, worse case, or equivalent case. The most common is to analyze an 
algorithm by its worst case. You typically don't evaluate by best case because 
those conditions aren't what you're planning for. A very good example of this 
is sorting algorithms; specifically, adding elements to a tree structure. Best 
case for most algorithms could be as low as a single operation. However, in 
most cases, the element you're adding will need to be sorted appropriately 
through the tree, which could mean examining an entire branch. This is the 
worst case, and this is what we plan for.

### Types of functions, limits, and simplification

```
Logarithmic Function - log n
Linear Function - an + b
Quadratic Function - an^2 + bn + c
Polynomial Function - an^z + . . . + an^2 + a*n^1 + a*n^0, where z is some 
constant
Exponential Function - a^n, where a is some constant
```

These are some basic function growth classifications used in various 
notations. The list starts at the slowest growing function (logarithmic, 
fastest execution time) and goes on to the fastest growing (exponential, 
slowest execution time). Notice that as 'n', or the input, increases in each 
of those functions, the result clearly increases much quicker in quadratic, 
polynomial, and exponential, compared to logarithmic and linear.

One extremely important note is that for the notations about to be discussed 
you should do your best to use simplest terms. This means to disregard 
constants, and lower order terms, because as the input size (or n in our f(n) 
example) increases to infinity (mathematical limits), the lower order terms 
and constants are of little to no importance. That being said, if you have 
constants that are 2^9001, or some other ridiculous, unimaginable amount, 
realize that simplifying will skew your notation accuracy.

Since we want simplest form, lets modify our table a bit...

```
Logarithmic - log n
Linear - n
Quadratic - n^2
Polynomial - n^z, where z is some constant
Exponential - a^n, where a is some constant
```

### Big-O
Big-O, commonly written as **O**, is an Asymptotic Notation for the worst 
case, or ceiling of growth for a given function. It provides us with an 
_**asymptotic upper bound**_ for the growth rate of runtime of an algorithm.
Say `f(n)` is your algorithm runtime, and `g(n)` is an arbitrary time 
complexity you are trying to relate to your algorithm. `f(n)` is O(g(n)), if 
for some real constants c (c > 0) and n<sub>0</sub>, `f(n)` <= `c g(n)` for every input size 
n (n > n<sub>0</sub>).

*Example 1*

```
f(n) = 3log n + 100
g(n) = log n
```

Is `f(n)` O(g(n))?
Is `3 log n + 100` O(log n)?
Let's look to the definition of Big-O.

```
3log n + 100 <= c * log n
```

Is there some pair of constants c, n<sub>0</sub> that satisfies this for all n > <sub>0</sub>?

```
3log n + 100 <= 150 * log n, n > 2 (undefined at n = 1)
```

Yes! The definition of Big-O has been met therefore `f(n)` is O(g(n)).

*Example 2*

```
f(n) = 3*n^2
g(n) = n
```

Is `f(n)` O(g(n))?
Is `3 * n^2` O(n)?
Let's look at the definition of Big-O.

```
3 * n^2 <= c * n
```

Is there some pair of constants c, n<sub>0</sub> that satisfies this for all n > <sub>0</sub>?
No, there isn't. `f(n)` is NOT O(g(n)).

### Big-Omega
Big-Omega, commonly written as **Ω**, is an Asymptotic Notation for the best 
case, or a floor growth rate for a given function. It provides us with an 
_**asymptotic lower bound**_ for the growth rate of runtime of an algorithm.

`f(n)` is Ω(g(n)), if for some real constants c (c > 0) and n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` is >= `c g(n)` 
for every input size n (n > n<sub>0</sub>).

### Note

The asymptotic growth rates provided by big-O and big-omega notation may or 
may not be asymptotically tight. Thus we use small-o and small-omega notation 
to denote bounds that are not asymptotically tight. 

### Small-o
Small-o, commonly written as **o**, is an Asymptotic Notation to denote the 
upper bound (that is not asymptotically tight) on the growth rate of runtime 
of an algorithm.

`f(n)` is o(g(n)), if for some real constants c (c > 0) and n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` is < `c g(n)` 
for every input size n (n > n<sub>0</sub>).

The definitions of O-notation and o-notation are similar. The main difference 
is that in f(n) = O(g(n)), the bound f(n) <= g(n) holds for _**some**_ 
constant c > 0, but in f(n) = o(g(n)), the bound f(n) < c g(n) holds for 
_**all**_ constants c > 0.

### Small-omega
Small-omega, commonly written as **ω**, is an Asymptotic Notation to denote 
the lower bound (that is not asymptotically tight) on the growth rate of 
runtime of an algorithm.

`f(n)` is ω(g(n)), if for some real constants c (c > 0) and n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` is > `c g(n)` 
for every input size n (n > n<sub>0</sub>).

The definitions of Ω-notation and ω-notation are similar. The main difference 
is that in f(n) = Ω(g(n)), the bound f(n) >= g(n) holds for _**some**_ 
constant c > 0, but in f(n) = ω(g(n)), the bound f(n) > c g(n) holds for 
_**all**_ constants c > 0.

### Theta
Theta, commonly written as **Θ**, is an Asymptotic Notation to denote the 
_**asymptotically tight bound**_ on the growth rate of runtime of an algorithm. 

`f(n)` is Θ(g(n)), if for some real constants c1, c2 and n<sub>0</sub> (c1 > 0, c2 > 0, n<sub>0</sub> > 0), 
`c1 g(n)` is < `f(n)` is < `c2 g(n)` for every input size n (n > n<sub>0</sub>).

∴ `f(n)` is Θ(g(n)) implies `f(n)` is O(g(n)) as well as `f(n)` is Ω(g(n)).

Feel free to head over to additional resources for examples on this. Big-O 
is the primary notation use for general algorithm time complexity.

### Ending Notes
It's hard to keep this kind of topic short, and you should definitely go 
through the books and online resources listed. They go into much greater depth 
with definitions and examples. More where x='Algorithms & Data Structures' is 
on its way; we'll have a doc up on analyzing actual code examples soon.

## Books

* [Algorithms](http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X)
* [Algorithm Design](http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651)

## Online Resources

* [MIT](http://web.mit.edu/16.070/www/lecture/big_o.pdf)
* [KhanAcademy](https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation)
* [Big-O Cheatsheet](http://bigocheatsheet.com/) - common structures, operations, and algorithms, ranked by complexity.
---
language: awk
filename: learnawk.awk
contributors:
     - ["Marshall Mason", "http://github.com/marshallmason"]

---

AWK is a standard tool on every POSIX-compliant UNIX system. It's like a
stripped-down Perl, perfect for text-processing tasks and other scripting
needs. It has a C-like syntax, but without semicolons, manual memory
management, or static typing. It excels at text processing. You can call to it
from a shell script, or you can use it as a stand-alone scripting language.

Why use AWK instead of Perl? Mostly because AWK is part of UNIX. You can always
count on it, whereas Perl's future is in question. AWK is also easier to read
than Perl. For simple text-processing scripts, particularly ones that read
files line by line and split on delimiters, AWK is probably the right tool for
the job.

```awk
#!/usr/bin/awk -f

# Comments are like this

# AWK programs consist of a collection of patterns and actions. The most
# important pattern is called BEGIN. Actions go into brace blocks.
BEGIN {

    # BEGIN will run at the beginning of the program. It's where you put all
    # the preliminary set-up code, before you process any text files. If you
    # have no text files, then think of BEGIN as the main entry point.

    # Variables are global. Just set them or use them, no need to declare..
    count = 0

    # Operators just like in C and friends
    a = count + 1
    b = count - 1
    c = count * 1
    d = count / 1
    e = count % 1 # modulus
    f = count ^ 1 # exponentiation

    a += 1
    b -= 1
    c *= 1
    d /= 1
    e %= 1
    f ^= 1

    # Incrementing and decrementing by one
    a++
    b--

    # As a prefix operator, it returns the incremented value
    ++a
    --b

    # Notice, also, no punctuation such as semicolons to terminate statements

    # Control statements
    if (count == 0)
        print "Starting with count of 0"
    else
        print "Huh?"

    # Or you could use the ternary operator
    print (count == 0) ? "Starting with count of 0" : "Huh?"

    # Blocks consisting of multiple lines use braces
    while (a < 10) {
        print "String concatenation is done" " with a series" " of"
            " space-separated strings"
        print a

        a++
    }

    for (i = 0; i < 10; i++)
        print "Good ol' for loop"

    # As for comparisons, they're the standards:
    a < b   # Less than
    a <= b  # Less than or equal
    a != b  # Not equal
    a == b  # Equal
    a > b   # Greater than
    a >= b  # Greater than or equal

    # Logical operators as well
    a && b  # AND
    a || b  # OR

    # In addition, there's the super useful regular expression match
    if ("foo" ~ "^fo+$")
        print "Fooey!"
    if ("boo" !~ "^fo+$")
        print "Boo!"

    # Arrays
    arr[0] = "foo"
    arr[1] = "bar"
    # Unfortunately, there is no other way to initialize an array. Ya just
    # gotta chug through every value line by line like that.

    # You also have associative arrays
    assoc["foo"] = "bar"
    assoc["bar"] = "baz"

    # And multi-dimensional arrays, with some limitations I won't mention here
    multidim[0,0] = "foo"
    multidim[0,1] = "bar"
    multidim[1,0] = "baz"
    multidim[1,1] = "boo"

    # You can test for array membership
    if ("foo" in assoc)
        print "Fooey!"

    # You can also use the 'in' operator to traverse the keys of an array
    for (key in assoc)
        print assoc[key]

    # The command line is in a special array called ARGV
    for (argnum in ARGV)
        print ARGV[argnum]

    # You can remove elements of an array
    # This is particularly useful to prevent AWK from assuming the arguments
    # are files for it to process
    delete ARGV[1]

    # The number of command line arguments is in a variable called ARGC
    print ARGC

    # AWK has several built-in functions. They fall into three categories. I'll
    # demonstrate each of them in their own functions, defined later.

    return_value = arithmetic_functions(a, b, c)
    string_functions()
    io_functions()
}

# Here's how you define a function
function arithmetic_functions(a, b, c,     localvar) {

    # Probably the most annoying part of AWK is that there are no local
    # variables. Everything is global. For short scripts, this is fine, even
    # useful, but for longer scripts, this can be a problem.

    # There is a work-around (ahem, hack). Function arguments are local to the
    # function, and AWK allows you to define more function arguments than it
    # needs. So just stick local variable in the function declaration, like I
    # did above. As a convention, stick in some extra whitespace to distinguish
    # between actual function parameters and local variables. In this example,
    # a, b, and c are actual parameters, while d is merely a local variable.

    # Now, to demonstrate the arithmetic functions

    # Most AWK implementations have some standard trig functions
    localvar = sin(a)
    localvar = cos(a)
    localvar = atan2(a, b) # arc tangent of b / a

    # And logarithmic stuff
    localvar = exp(a)
    localvar = log(a)

    # Square root
    localvar = sqrt(a)

    # Truncate floating point to integer
    localvar = int(5.34) # localvar => 5

    # Random numbers
    srand() # Supply a seed as an argument. By default, it uses the time of day
    localvar = rand() # Random number between 0 and 1.

    # Here's how to return a value
    return localvar
}

function string_functions(    localvar, arr) {

    # AWK, being a string-processing language, has several string-related
    # functions, many of which rely heavily on regular expressions.

    # Search and replace, first instance (sub) or all instances (gsub)
    # Both return number of matches replaced
    localvar = "fooooobar"
    sub("fo+", "Meet me at the ", localvar) # localvar => "Meet me at the bar"
    gsub("e+", ".", localvar) # localvar => "m..t m. at th. bar"

    # Search for a string that matches a regular expression
    # index() does the same thing, but doesn't allow a regular expression
    match(localvar, "t") # => 4, since the 't' is the fourth character

    # Split on a delimiter
    split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"]

    # Other useful stuff
    sprintf("%s %d %d %d", "Testing", 1, 2, 3) # => "Testing 1 2 3"
    substr("foobar", 2, 3) # => "oob"
    substr("foobar", 4) # => "bar"
    length("foo") # => 3
    tolower("FOO") # => "foo"
    toupper("foo") # => "FOO"
}

function io_functions(    localvar) {

    # You've already seen print
    print "Hello world"

    # There's also printf
    printf("%s %d %d %d\n", "Testing", 1, 2, 3)

    # AWK doesn't have file handles, per se. It will automatically open a file
    # handle for you when you use something that needs one. The string you used
    # for this can be treated as a file handle, for purposes of I/O. This makes
    # it feel sort of like shell scripting:

    print "foobar" >"/tmp/foobar.txt"

    # Now the string "/tmp/foobar.txt" is a file handle. You can close it:
    close("/tmp/foobar.txt")

    # Here's how you run something in the shell
    system("echo foobar") # => prints foobar

    # Reads a line from standard input and stores in localvar
    getline localvar

    # Reads a line from a pipe
    "echo foobar" | getline localvar # localvar => "foobar"
    close("echo foobar")

    # Reads a line from a file and stores in localvar
    getline localvar <"/tmp/foobar.txt"
    close("/tmp/foobar.txt")
}

# As I said at the beginning, AWK programs consist of a collection of patterns
# and actions. You've already seen the all-important BEGIN pattern. Other
# patterns are used only if you're processing lines from files or standard
# input.
#
# When you pass arguments to AWK, they are treated as file names to process.
# It will process them all, in order. Think of it like an implicit for loop,
# iterating over the lines in these files. these patterns and actions are like
# switch statements inside the loop. 

/^fo+bar$/ {
    
    # This action will execute for every line that matches the regular
    # expression, /^fo+bar$/, and will be skipped for any line that fails to
    # match it. Let's just print the line:

    print

    # Whoa, no argument! That's because print has a default argument: $0.
    # $0 is the name of the current line being processed. It is created
    # automatically for you.

    # You can probably guess there are other $ variables. Every line is
    # implicitly split before every action is called, much like the shell
    # does. And, like the shell, each field can be access with a dollar sign

    # This will print the second and fourth fields in the line
    print $2, $4

    # AWK automatically defines many other variables to help you inspect and
    # process each line. The most important one is NF

    # Prints the number of fields on this line
    print NF

    # Print the last field on this line
    print $NF
}

# Every pattern is actually a true/false test. The regular expression in the
# last pattern is also a true/false test, but part of it was hidden. If you
# don't give it a string to test, it will assume $0, the line that it's
# currently processing. Thus, the complete version of it is this:

$0 ~ /^fo+bar$/ {
    print "Equivalent to the last pattern"
}

a > 0 {
    # This will execute once for each line, as long as a is positive
}

# You get the idea. Processing text files, reading in a line at a time, and
# doing something with it, particularly splitting on a delimiter, is so common
# in UNIX that AWK is a scripting language that does all of it for you, without
# you needing to ask. All you have to do is write the patterns and actions
# based on what you expect of the input, and what you want to do with it.

# Here's a quick example of a simple script, the sort of thing AWK is perfect
# for. It will read a name from standard input and then will print the average
# age of everyone with that first name. Let's say you supply as an argument the
# name of a this data file:
#
# Bob Jones 32
# Jane Doe 22
# Steve Stevens 83
# Bob Smith 29
# Bob Barker 72
#
# Here's the script:

BEGIN {

    # First, ask the user for the name
    print "What name would you like the average age for?"

    # Get a line from standard input, not from files on the command line
    getline name <"/dev/stdin"
}

# Now, match every line whose first field is the given name
$1 == name {

    # Inside here, we have access to a number of useful variables, already
    # pre-loaded for us:
    # $0 is the entire line
    # $3 is the third field, the age, which is what we're interested in here
    # NF is the number of fields, which should be 3
    # NR is the number of records (lines) seen so far
    # FILENAME is the name of the file being processed
    # FS is the field separator being used, which is " " here
    # ...etc. There are plenty more, documented in the man page.

    # Keep track of a running total and how many lines matched
    sum += $3
    nlines++
}

# Another special pattern is called END. It will run after processing all the
# text files. Unlike BEGIN, it will only run if you've given it input to
# process. It will run after all the files have been read and processed
# according to the rules and actions you've provided. The purpose of it is
# usually to output some kind of final report, or do something with the
# aggregate of the data you've accumulated over the course of the script.

END {
    if (nlines)
        print "The average age for " name " is " sum / nlines
}

```
Further Reading:

* [Awk tutorial](http://www.grymoire.com/Unix/Awk.html)
* [Awk man page](https://linux.die.net/man/1/awk)
* [The GNU Awk User's Guide](https://www.gnu.org/software/gawk/manual/gawk.html) GNU Awk is found on most Linux systems.
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
    - ["Jonathan Wang", "https://github.com/Jonathansw"]   
    - ["Leo Rudberg", "https://github.com/LOZORD"]
    - ["Betsy Lorton", "https://github.com/schbetsy"]
    - ["John Detter", "https://github.com/jdetter"]
filename: LearnBash.sh
---

Bash is a name of the unix shell, which was also distributed as the shell for the GNU operating system and as default shell on Linux and Mac OS X.
Nearly all examples below can be a part of a shell script or executed directly in the shell.

[Read more here.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# First line of the script is shebang which tells the system how to execute
# the script: http://en.wikipedia.org/wiki/Shebang_(Unix)
# As you already figured, comments start with #. Shebang is also a comment.

# Simple hello world example:
echo Hello world!

# Each command starts on a new line, or after semicolon:
echo 'This is the first line'; echo 'This is the second line'

# Declaring a variable looks like this:
Variable="Some string"

# But not like this:
Variable = "Some string"
# Bash will decide that Variable is a command it must execute and give an error
# because it can't be found.

# Or like this:
Variable= 'Some string'
# Bash will decide that 'Some string' is a command it must execute and give an
# error because it can't be found. (In this case the 'Variable=' part is seen
# as a variable assignment valid only for the scope of the 'Some string'
# command.)

# Using the variable:
echo $Variable
echo "$Variable"
echo '$Variable'
# When you use the variable itself — assign it, export it, or else — you write
# its name without $. If you want to use the variable's value, you should use $.
# Note that ' (single quote) won't expand the variables!

# Parameter expansion ${ }:
echo ${Variable}
# This is a simple usage of parameter expansion
# Parameter Expansion gets a value from a variable.  It "expands" or prints the value
# During the expansion time the value or parameter are able to be modified
# Below are other modifications that add onto this expansion

# String substitution in variables
echo ${Variable/Some/A}
# This will substitute the first occurrence of "Some" with "A"

# Substring from a variable
Length=7
echo ${Variable:0:Length}
# This will return only the first 7 characters of the value

# Default value for variable
echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
# This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0.
# Note that it only returns default value and doesn't change variable value.

# Brace Expansion { }
# Used to generate arbitrary strings
echo {1..10}
echo {a..z}
# This will output the range from the start value to the end value

# Builtin variables:
# There are some useful builtin variables, like
echo "Last program's return value: $?"
echo "Script's PID: $$"
echo "Number of arguments passed to script: $#"
echo "All arguments passed to script: $@"
echo "Script's arguments separated into different variables: $1 $2..."

# Now that we know how to echo and use variables,
# let's learn some of the other basics of bash!

# Our current directory is available through the command `pwd`.
# `pwd` stands for "print working directory".
# We can also use the builtin variable `$PWD`.
# Observe that the following are equivalent:
echo "I'm in $(pwd)" # execs `pwd` and interpolates output
echo "I'm in $PWD" # interpolates the variable

# If you get too much output in your terminal, or from a script, the command
# `clear` clears your screen
clear
# Ctrl-L also works for clearing output

# Reading a value from input:
echo "What's your name?"
read Name # Note that we didn't need to declare a new variable
echo Hello, $Name!

# We have the usual if structure:
# use 'man test' for more info about conditionals
if [ $Name != $USER ]
then
    echo "Your name isn't your username"
else
    echo "Your name is your username"
fi

# NOTE: if $Name is empty, bash sees the above condition as:
if [ != $USER ]
# which is invalid syntax
# so the "safe" way to use potentially empty variables in bash is:
if [ "$Name" != $USER ] ...
# which, when $Name is empty, is seen by bash as:
if [ "" != $USER ] ...
# which works as expected

# There is also conditional execution
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# To use && and || with if statements, you need multiple pairs of square brackets:
if [ "$Name" == "Steve" ] && [ "$Age" -eq 15 ]
then
    echo "This will run if $Name is Steve AND $Age is 15."
fi

if [ "$Name" == "Daniya" ] || [ "$Name" == "Zach" ]
then
    echo "This will run if $Name is Daniya OR Zach."
fi

# Expressions are denoted with the following format:
echo $(( 10 + 5 ))

# Unlike other programming languages, bash is a shell so it works in the context
# of a current directory. You can list files and directories in the current
# directory with the ls command:
ls

# These commands have options that control their execution:
ls -l # Lists every file and directory on a separate line
ls -t # Sorts the directory contents by last-modified date (descending)
ls -R # Recursively `ls` this directory and all of its subdirectories

# Results of the previous command can be passed to the next command as input.
# grep command filters the input with provided patterns. That's how we can list
# .txt files in the current directory:
ls -l | grep "\.txt"

# Use `cat` to print files to stdout:
cat file.txt

# We can also read the file using `cat`:
Contents=$(cat file.txt)
echo "START OF FILE\n$Contents\nEND OF FILE"

# Use `cp` to copy files or directories from one place to another.
# `cp` creates NEW versions of the sources,
# so editing the copy won't affect the original (and vice versa).
# Note that it will overwrite the destination if it already exists.
cp srcFile.txt clone.txt
cp -r srcDirectory/ dst/ # recursively copy

# Look into `scp` or `sftp` if you plan on exchanging files between computers.
# `scp` behaves very similarly to `cp`.
# `sftp` is more interactive.

# Use `mv` to move files or directories from one place to another.
# `mv` is similar to `cp`, but it deletes the source.
# `mv` is also useful for renaming files!
mv s0urc3.txt dst.txt # sorry, l33t hackers...

# Since bash works in the context of a current directory, you might want to 
# run your command in some other directory. We have cd for changing location:
cd ~    # change to home directory
cd ..   # go up one directory
        # (^^say, from /home/username/Downloads to /home/username)
cd /home/username/Documents   # change to specified directory
cd ~/Documents/..    # still in home directory..isn't it??

# Use subshells to work across directories
(echo "First, I'm here: $PWD") && (cd someDir; echo "Then, I'm here: $PWD")
pwd # still in first directory

# Use `mkdir` to create new directories.
mkdir myNewDir
# The `-p` flag causes new intermediate directories to be created as necessary.
mkdir -p myNewDir/with/intermediate/directories

# You can redirect command input and output (stdin, stdout, and stderr).
# Read from stdin until ^EOF$ and overwrite hello.py with the lines
# between "EOF":
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Run hello.py with various stdin, stdout, and stderr redirections:
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# The output error will overwrite the file if it exists,
# if you want to append instead, use ">>":
python hello.py >> "output.out" 2>> "error.err"

# Overwrite output.out, append to error.err, and count lines:
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# Run a command and print its file descriptor (e.g. /dev/fd/123)
# see: man fd
echo <(echo "#helloworld")

# Overwrite output.out with "#helloworld":
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# Cleanup temporary files verbosely (add '-i' for interactive)
# WARNING: `rm` commands cannot be undone
rm -v output.out error.err output-and-error.log
rm -r tempDir/ # recursively delete

# Commands can be substituted within other commands using $( ):
# The following command displays the number of files and directories in the
# current directory.
echo "There are $(ls | wc -l) items here."

# The same can be done using backticks `` but they can't be nested - the preferred way
# is to use $( ).
echo "There are `ls | wc -l` items here."

# Bash uses a case statement that works similarly to switch in Java and C++:
case "$Variable" in
    #List patterns for the conditions you want to meet
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# for loops iterate for as many arguments given:
# The contents of $Variable is printed three times.
for Variable in {1..3}
do
    echo "$Variable"
done

# Or write it the "traditional for loop" way:
for ((a=1; a <= 3; a++))
do
    echo $a
done

# They can also be used to act on files..
# This will run the command 'cat' on file1 and file2
for Variable in file1 file2
do
    cat "$Variable"
done

# ..or the output from a command
# This will cat the output from ls.
for Output in $(ls)
do
    cat "$Output"
done

# while loop:
while [ true ]
do
    echo "loop body here..."
    break
done

# You can also define functions
# Definition:
function foo ()
{
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
}

# or simply
bar ()
{
    echo "Another way to declare functions!"
    return 0
}

# Calling your function
foo "My name is" $Name

# There are a lot of useful commands you should learn:
# prints last 10 lines of file.txt
tail -n 10 file.txt
# prints first 10 lines of file.txt
head -n 10 file.txt
# sort file.txt's lines
sort file.txt
# report or omit repeated lines, with -d it reports them
uniq -d file.txt
# prints only the first column before the ',' character
cut -d ',' -f 1 file.txt
# replaces every occurrence of 'okay' with 'great' in file.txt, (regex compatible)
sed -i 's/okay/great/g' file.txt
# print to stdout all lines of file.txt which match some regex
# The example prints lines which begin with "foo" and end in "bar"
grep "^foo.*bar$" file.txt
# pass the option "-c" to instead print the number of lines matching the regex
grep -c "^foo.*bar$" file.txt
# Other useful options are:
grep -r "^foo.*bar$" someDir/ # recursively `grep`
grep -n "^foo.*bar$" file.txt # give line numbers
grep -rI "^foo.*bar$" someDir/ # recursively `grep`, but ignore binary files
# perform the same initial search, but filter out the lines containing "baz"
grep "^foo.*bar$" file.txt | grep -v "baz"

# if you literally want to search for the string,
# and not the regex, use fgrep (or grep -F)
fgrep "foobar" file.txt

# trap command allows you to execute a command when a signal is received by your script.
# Here trap command will execute rm if any one of the three listed signals is received.
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM

# `sudo` is used to perform commands as the superuser
NAME1=$(whoami)
NAME2=$(sudo whoami)
echo "Was $NAME1, then became more powerful $NAME2"

# Read Bash shell builtins documentation with the bash 'help' builtin:
help
help help
help for
help return
help source
help .

# Read Bash manpage documentation with man
apropos bash
man 1 bash
man bash

# Read info documentation with info (? for help)
apropos info | grep '^info.*('
man info
info info
info 5 info

# Read bash info documentation:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: "Brainfuck"
filename: brainfuck.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
---

Brainfuck (not capitalized except at the start of a sentence) is an extremely
minimal Turing-complete programming language with just 8 commands.

You can try brainfuck on your browser with [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/).

```bf
Any character not "><+-.,[]" (excluding quotation marks) is ignored.

Brainfuck is represented by an array with 30,000 cells initialized to zero
and a data pointer pointing at the current cell.

There are eight commands:
+ : Increments the value at the current cell by one.
- : Decrements the value at the current cell by one.
> : Moves the data pointer to the next cell (cell on the right).
< : Moves the data pointer to the previous cell (cell on the left).
. : Prints the ASCII value at the current cell (i.e. 65 = 'A').
, : Reads a single input character into the current cell.
[ : If the value at the current cell is zero, skips to the corresponding ] .
    Otherwise, move to the next instruction.
] : If the value at the current cell is zero, move to the next instruction.
    Otherwise, move backwards in the instructions to the corresponding [ .

[ and ] form a while loop. Obviously, they must be balanced.

Let's look at some basic brainfuck programs.

++++++ [ > ++++++++++ < - ] > +++++ .

This program prints out the letter 'A'. First, it increments cell #1 to 6.
Cell #1 will be used for looping. Then, it enters the loop ([) and moves
to cell #2. It increments cell #2 10 times, moves back to cell #1, and
decrements cell #1. This loop happens 6 times (it takes 6 decrements for
cell #1 to reach 0, at which point it skips to the corresponding ] and
continues on).

At this point, we're on cell #1, which has a value of 0, while cell #2 has a
value of 60. We move on cell #2, increment 5 times, for a value of 65, and then
print cell #2's value. 65 is 'A' in ASCII, so 'A' is printed to the terminal.


, [ > + < - ] > .

This program reads a character from the user input and copies the character into
cell #1. Then we start a loop. Move to cell #2, increment the value at cell #2,
move back to cell #1, and decrement the value at cell #1. This continues on
until cell #1 is 0, and cell #2 holds cell #1's old value. Because we're on
cell #1 at the end of the loop, move to cell #2, and then print out the value
in ASCII.

Also keep in mind that the spaces are purely for readability purposes. You
could just as easily write it as:

,[>+<-]>.

Try and figure out what this program does:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

This program takes two numbers for input, and multiplies them.

The gist is it first reads in two inputs. Then it starts the outer loop,
conditioned on cell #1. Then it moves to cell #2, and starts the inner
loop conditioned on cell #2, incrementing cell #3. However, there comes a
problem: At the end of the inner loop, cell #2 is zero. In that case,
inner loop won't work anymore since next time. To solve this problem,
we also increment cell #4, and then recopy cell #4 into cell #2.
Then cell #3 is the result.
```

And that's brainfuck. Not that hard, eh? For fun, you can write your own
brainfuck programs, or you can write a brainfuck interpreter in another
language. The interpreter is fairly simple to implement, but if you're a
masochist, try writing a brainfuck interpreter… in brainfuck.
---
name: perl
category: language
language: perl
filename: learnperl-bg.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
    - ["Dan Book", "http://github.com/Grinnz"]
translators:
    - ["Красимир Беров", "https://github.com/kberov"]
lang: bg-bg
---

Perl 5 е изключително мощен език за програмиране с широка област на приложение
и над 25 годишна история.

Perl 5 работи на повече от 100 операционни системи от мини до супер-компютри и е
подходящ както за бърза разработка на скриптове така и за огромни приложения.

```perl
# Едноредовите коментари започват със знака диез.

#### Стриктен режим и предупреждения

use strict;
use warnings;

# Силно препоръчително е всички скриптове и модули да включват тези редове.
# strict спира компилацията в случай на необявени предварително променливи.
# warnings показва предупредителни съобщения в случай на често допускани грешки,
# например използване на променливи без стойност в низове.

#### Типове променливи в Perl

# Променливите започват със съответен знак (sigil - от латински sigillum ),
# който представлява символ, указващ типа на променливата. Името на самата
# променлива започва с буква или знак за подчертаване (_), следван от какъвто и
# да е брой букви, цифри или знаци за подчертаване. Забележете, че ако напишете
# 'use utf8;' (без кавичките), можете да използвате всякакви букви за имена на
# променливите, включително и български.

### Perl има три главни типа променливи: $scalar (скалар), @array (масив), and %hash (хеш).

## Скалари
#  Скаларът представлява единична стойност:
my $animal = "camel";
my $answer = 42;
use utf8;
my $животно = 'камила';

# Стойностите на скаларите могат да бъдат низове, цели числа или числа с
# плаваща запетая (десетични дроби). Perl автоматично ги ползва и превръща от
# един тип стойност в друга, според както е необходимо.

## Масиви
#  Масивът представлява списък от стойности:
my @animals = ("камила", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed   = ("camel", 42, 1.23);

# Елементите на масива се достъпват като се използват квадратни скоби и $,
# който указва каква стойност ще бъде върната (скалар).
my $second = $animals[1];

## Хешове
#  Хешът представлява набор от двойки ключ/стойност:

my %fruit_color = ("ябълка", "червена", "banana", "yellow");

# Може да използвате празно пространство и оператора "=>" (тлъста запетая),
# за да ги изложите по-прегледно:

%fruit_color = (
  ябълка  => "червена",
  banana => "yellow",
);

# Елементите (стойностите) от хеша се достъпват чрез използване на ключовете.
# Ключовете се ограждат с фигурни скоби и се поставя $ пред името на хеша.
my $color = $fruit_color{ябълка};

# Скаларите, масивите и хешовете са документирани по-пълно в perldata.
# На командния ред напишете (без кавичките) 'perldoc perldata'.

#### Указатели (Референции)

# По-сложни типове данни могат да бъдат създавани чрез използване на указатели,
# които ви позволяват да изграждате масиви и хешове в други масиви и хешове.

my $array_ref = \@array;
my $hash_ref = \%hash;
my @array_of_arrays = (\@array1, \@array2, \@array3);

# Също така можете да създавате безименни масиви и хешове, към които сочат само
# указатели.

my $fruits = ["apple", "banana"];
my $colors = {apple => "red", banana => "yellow"};

# Можете да достигате до безименните структури като поставяте отпред съответния
# знак на структурата, която искате да достъпите (дереферирате).

my @fruits_array = @$fruits;
my %colors_hash = %$colors;

# Можете да използвате оператора стрелка (->), за да достигнете до отделна
# скаларна стойност.

my $first = $array_ref->[0];
my $value = $hash_ref->{banana};

# Вижте perlreftut и perlref, където ще намерите по-задълбочена документация за
# указателите (референциите).

#### Условни изрази и цикли

# В Perl ще срещнете повечето от обичайните изрази за условия и обхождане (цикли).

if ($var) {
  ...
} elsif ($var eq 'bar') {
  ...
} else {
  ...
}

unless (условие) {
  ...
}
# Това е друг, по-четим вариант на "if (!условие)"

# Perl-овския начин след-условие
print "Yow!" if $zippy;
print "Нямаме банани" unless $bananas;

#  докато
while (условие) {
  ...
}


# цикли for и повторение
for (my $i = 0; $i < $max; $i++) {
  print "index is $i";
}

for (my $i = 0; $i < @elements; $i++) {
  print "Current element is " . $elements[$i];
}

for my $element (@elements) {
  print $element;
}

# мълчаливо - използва се подразбиращата се променлива $_.
for (@elements) {
  print;
}

# Отново Perl-овския начин след-
print for @elements;

# отпечатване на стойностите чрез обхождане ключовете на указател към хеш
print $hash_ref->{$_} for keys %$hash_ref;

#### Регулярни (обикновени) изрази

# Поддръжката на регулярни изрази  е залеганала дълбоко в Perl. Задълбочена
# документация ще намерите в perlrequick, perlretut и на други места.
# Но ето накратко:

# Просто съвпадение
if (/foo/)       { ... }  # истина ако $_ съдържа "foo"
if ($x =~ /foo/) { ... }  # истина ако $x съдържа "foo"

# Просто заместване

$x =~ s/foo/bar/;         # замества foo с bar в $x
$x =~ s/foo/bar/g;        # Замества ВСИЧКИ ПОЯВИ на foo с bar в $x


#### Файлове и Вход/Изход (I/O)

# Можете да отворите файл за въвеждане на данни в него или за извеждане на
# данни от него като използвате функцията "open()".

open(my $in,  "<",  "input.txt")  or die "Не мога да отворя input.txt: $!";
open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";

# Можете да четете от отворен файлов манипулатор като използвате оператора
# "<>". В скаларен контекст той чете по един ред от файла наведнъж, а в списъчен
# контекст изчита всички редове от файла наведнъж като присвоява всеки ред на
# масива:

my $line  = <$in>;
my @lines = <$in>;

#### Подпрограми (функции)

# Да се пишат подпрограми е лесно:

sub logger {
  my $logmessage = shift;

  open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";

  print $logfile $logmessage;
}

# Сега можем да ползваме подпрограмата като всяка друга вградена функция:

logger("Имаме подпрограма, която пише във файл-отчет!");

#### Модули

# Модулът е набор от програмен код на Perl, обикновено подпрограми, който може
# да бъде използван в друг програмен код на Perl. Обикновено се съхранява във
# файл с разширение .pm, така че perl (програмата) да може лесно да го разпознае.

# В MyModule.pm
package MyModule;
use strict;
use warnings;

sub trim {
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
}

1;

# От другаде:

use MyModule;
MyModule::trim($string);

# Чрез модула Exporter може да направите функциите си износни, така че други
# програми да могат да ги внасят (импортират).
# Такива функции се използват така:

use MyModule 'trim';
trim($string);

# Много Perl-модули могат да се свалят от CPAN (http://www.cpan.org/). Те
# притежават редица полезни свойства, които ще ви помогнат да си свършите работа
# без да откривате колелото. Голям брой известни модули като Exporter са включени
# в дистрибуцията на самия Perl. Вижте perlmod  за повече подробности, свързани с
# модулите в Perl.

#### Обекти

# Обектите в Perl са просто референции, които знаят на кой клас (пакет)
# принадлежат. По този начин методите (подпрограми), които се извикват срещу
# тях могат да бъдат намерени в съответния клас. За да се случи това, в
# конструкторите (обикновено new) се използва вградената функция
# bless. Ако използвате обаче модули като Moose или Moo, няма да ви се налага
# сами да извиквате bless (ще видите малко по-долу).

package MyCounter;
use strict;
use warnings;

sub new {
  my $class = shift;
  my $self = {count => 0};
  return bless $self, $class;
}

sub count {
  my $self = shift;
  return $self->{count};
}

sub increment {
  my $self = shift;
  $self->{count}++;
}

1;

# Методите могат да се извикват на клас или на обект като се използва оператора 
# стрелка (->).

use MyCounter;
my $counter = MyCounter->new;
print $counter->count, "\n"; # 0
$counter->increment;
print $counter->count, "\n"; # 1

# Модулите Moose и Moo от CPAN  ви помагат леснот да създавате класове. Те
# предоставят готов конструктор (new) и прост синтаксис за деклариране на
# свойства на обектите (attributes). Този клас може да се използва по същия начин
# като предишния по-горе.

package MyCounter;
use Moo; # внася strict и warnings

has 'count' => (is => 'rwp', default => 0, init_arg => undef);

sub increment {
  my $self = shift;
  $self->_set_count($self->count + 1);
}

1;

# Обектно-ориентираното програмиране е разгледано по-задълбочено в perlootut,
# а изпълнението му на ниско ниво в Perl е обяснено в perlobj.
```

#### Често задавани въпроси (FAQ)

# perlfaq съдържа въпроси и отговори, отнасящи се до много общи задачи и предлага
# за ползване добри модлули от CPAN, подходящи за решаване на различни проблеми.

#### Повече за четене
 - [Въведение в Perl](http://www.slideshare.net/kberov/01-intro-bg)
 - [PERL - Курс на МГУ "Св.Иван Рилски" (13 ЧАСТИ)](http://www.mgu.bg/drugi/ebooks/belchevski/perl.html)
 - [perl-tutorial](http://perl-tutorial.org/)
 - [Learn at www.perl.com](http://www.perl.org/learn.html)
 - [perldoc](http://perldoc.perl.org/)
 - и идващото с perl: `perldoc perlintro`

---
language: c++
filename: learncpp.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Matt Kline", "https://github.com/mrkline"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Connor Waters", "http://github.com/connorwaters"]
    - ["Ankush Goyal", "http://github.com/ankushg07"]
    - ["Jatin Dhankhar", "https://github.com/jatindhankhar"]
---

C++ is a systems programming language that,
[according to its inventor Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote),
was designed to

- be a "better C"
- support data abstraction
- support object-oriented programming
- support generic programming

Though its syntax can be more difficult or complex than newer languages,
it is widely used because it compiles to native instructions that can be
directly run by the processor and offers tight control over hardware (like C)
while offering high-level features such as generics, exceptions, and classes.
This combination of speed and functionality makes C++
one of the most widely-used programming languages.

```c++
//////////////////
// Comparison to C
//////////////////

// C++ is _almost_ a superset of C and shares its basic syntax for
// variable declarations, primitive types, and functions.

// Just like in C, your program's entry point is a function called
// main with an integer return type.
// This value serves as the program's exit status.
// See http://en.wikipedia.org/wiki/Exit_status for more information.
int main(int argc, char** argv)
{
    // Command line arguments are passed in by argc and argv in the same way
    // they are in C.
    // argc indicates the number of arguments,
    // and argv is an array of C-style strings (char*)
    // representing the arguments.
    // The first argument is the name by which the program was called.
    // argc and argv can be omitted if you do not care about arguments,
    // giving the function signature of int main()

    // An exit status of 0 indicates success.
    return 0;
}

// However, C++ varies in some of the following ways:

// In C++, character literals are chars
sizeof('c') == sizeof(char) == 1

// In C, character literals are ints
sizeof('c') == sizeof(int)


// C++ has strict prototyping
void func(); // function which accepts no arguments

// In C
void func(); // function which may accept any number of arguments

// Use nullptr instead of NULL in C++
int* ip = nullptr;

// C standard headers are available in C++,
// but are prefixed with "c" and have no .h suffix.
#include <cstdio>

int main()
{
    printf("Hello, world!\n");
    return 0;
}

///////////////////////
// Function overloading
///////////////////////

// C++ supports function overloading
// provided each function takes different parameters.

void print(char const* myString)
{
    printf("String %s\n", myString);
}

void print(int myInt)
{
    printf("My int is %d", myInt);
}

int main()
{
    print("Hello"); // Resolves to void print(const char*)
    print(15); // Resolves to void print(int)
}

/////////////////////////////
// Default function arguments
/////////////////////////////

// You can provide default arguments for a function
// if they are not provided by the caller.

void doSomethingWithInts(int a = 1, int b = 4)
{
    // Do something with the ints here
}

int main()
{
    doSomethingWithInts();      // a = 1,  b = 4
    doSomethingWithInts(20);    // a = 20, b = 4
    doSomethingWithInts(20, 5); // a = 20, b = 5
}

// Default arguments must be at the end of the arguments list.

void invalidDeclaration(int a = 1, int b) // Error!
{
}


/////////////
// Namespaces
/////////////

// Namespaces provide separate scopes for variable, function,
// and other declarations.
// Namespaces can be nested.

namespace First {
    namespace Nested {
        void foo()
        {
            printf("This is First::Nested::foo\n");
        }
    } // end namespace Nested
} // end namespace First

namespace Second {
    void foo()
    {
        printf("This is Second::foo\n");
    }
}

void foo()
{
    printf("This is global foo\n");
}

int main()
{
    // Includes all symbols from namespace Second into the current scope. Note
    // that simply foo() no longer works, since it is now ambiguous whether
    // we're calling the foo in namespace Second or the top level.
    using namespace Second;

    Second::foo(); // prints "This is Second::foo"
    First::Nested::foo(); // prints "This is First::Nested::foo"
    ::foo(); // prints "This is global foo"
}

///////////////
// Input/Output
///////////////

// C++ input and output uses streams
// cin, cout, and cerr represent stdin, stdout, and stderr.
// << is the insertion operator and >> is the extraction operator.

#include <iostream> // Include for I/O streams

using namespace std; // Streams are in the std namespace (standard library)

int main()
{
   int myInt;

   // Prints to stdout (or terminal/screen)
   cout << "Enter your favorite number:\n";
   // Takes in input
   cin >> myInt;

   // cout can also be formatted
   cout << "Your favorite number is " << myInt << "\n";
   // prints "Your favorite number is <myInt>"

    cerr << "Used for error messages";
}

//////////
// Strings
//////////

// Strings in C++ are objects and have many member functions
#include <string>

using namespace std; // Strings are also in the namespace std (standard library)

string myString = "Hello";
string myOtherString = " World";

// + is used for concatenation.
cout << myString + myOtherString; // "Hello World"

cout << myString + " You"; // "Hello You"

// C++ strings are mutable.
myString.append(" Dog");
cout << myString; // "Hello Dog"


/////////////
// References
/////////////

// In addition to pointers like the ones in C,
// C++ has _references_.
// These are pointer types that cannot be reassigned once set
// and cannot be null.
// They also have the same syntax as the variable itself:
// No * is needed for dereferencing and
// & (address of) is not used for assignment.

using namespace std;

string foo = "I am foo";
string bar = "I am bar";


string& fooRef = foo; // This creates a reference to foo.
fooRef += ". Hi!"; // Modifies foo through the reference
cout << fooRef; // Prints "I am foo. Hi!"

// Doesn't reassign "fooRef". This is the same as "foo = bar", and
//   foo == "I am bar"
// after this line.
cout << &fooRef << endl; //Prints the address of foo
fooRef = bar;
cout << &fooRef << endl; //Still prints the address of foo
cout << fooRef;  // Prints "I am bar"

//The address of fooRef remains the same, i.e. it is still referring to foo.


const string& barRef = bar; // Create a const reference to bar.
// Like C, const values (and pointers and references) cannot be modified.
barRef += ". Hi!"; // Error, const references cannot be modified.

// Sidetrack: Before we talk more about references, we must introduce a concept
// called a temporary object. Suppose we have the following code:
string tempObjectFun() { ... }
string retVal = tempObjectFun();

// What happens in the second line is actually:
//   - a string object is returned from tempObjectFun
//   - a new string is constructed with the returned object as argument to the
//     constructor
//   - the returned object is destroyed
// The returned object is called a temporary object. Temporary objects are
// created whenever a function returns an object, and they are destroyed at the
// end of the evaluation of the enclosing expression (Well, this is what the
// standard says, but compilers are allowed to change this behavior. Look up
// "return value optimization" if you're into this kind of details). So in this
// code:
foo(bar(tempObjectFun()))

// assuming foo and bar exist, the object returned from tempObjectFun is
// passed to bar, and it is destroyed before foo is called.

// Now back to references. The exception to the "at the end of the enclosing
// expression" rule is if a temporary object is bound to a const reference, in
// which case its life gets extended to the current scope:

void constReferenceTempObjectFun() {
  // constRef gets the temporary object, and it is valid until the end of this
  // function.
  const string& constRef = tempObjectFun();
  ...
}

// Another kind of reference introduced in C++11 is specifically for temporary
// objects. You cannot have a variable of its type, but it takes precedence in
// overload resolution:

void someFun(string& s) { ... }  // Regular reference
void someFun(string&& s) { ... }  // Reference to temporary object

string foo;
someFun(foo);  // Calls the version with regular reference
someFun(tempObjectFun());  // Calls the version with temporary reference

// For example, you will see these two versions of constructors for
// std::basic_string:
basic_string(const basic_string& other);
basic_string(basic_string&& other);

// Idea being if we are constructing a new string from a temporary object (which
// is going to be destroyed soon anyway), we can have a more efficient
// constructor that "salvages" parts of that temporary string. You will see this
// concept referred to as "move semantics".

/////////////////////
// Enums
/////////////////////

// Enums are a way to assign a value to a constant most commonly used for
// easier visualization and reading of code
enum ECarTypes
{
  Sedan,
  Hatchback,
  SUV,
  Wagon
};

ECarTypes GetPreferredCarType()
{
	return ECarTypes::Hatchback;
}

// As of C++11 there is an easy way to assign a type to the enum which can be
// useful in serialization of data and converting enums back-and-forth between
// the desired type and their respective constants
enum ECarTypes : uint8_t
{
  Sedan, // 0
  Hatchback, // 1
  SUV = 254, // 254
  Hybrid // 255
};

void WriteByteToFile(uint8_t InputValue)
{
	// Serialize the InputValue to a file
}

void WritePreferredCarTypeToFile(ECarTypes InputCarType)
{
	// The enum is implicitly converted to a uint8_t due to its declared enum type
	WriteByteToFile(InputCarType);
}

// On the other hand you may not want enums to be accidentally cast to an integer
// type or to other enums so it is instead possible to create an enum class which
// won't be implicitly converted
enum class ECarTypes : uint8_t
{
  Sedan, // 0
  Hatchback, // 1
  SUV = 254, // 254
  Hybrid // 255
};

void WriteByteToFile(uint8_t InputValue)
{
	// Serialize the InputValue to a file
}

void WritePreferredCarTypeToFile(ECarTypes InputCarType)
{
	// Won't compile even though ECarTypes is a uint8_t due to the enum
	// being declared as an "enum class"!
	WriteByteToFile(InputCarType);
}

//////////////////////////////////////////
// Classes and object-oriented programming
//////////////////////////////////////////

// First example of classes
#include <iostream>

// Declare a class.
// Classes are usually declared in header (.h or .hpp) files.
class Dog {
    // Member variables and functions are private by default.
    std::string name;
    int weight;

// All members following this are public
// until "private:" or "protected:" is found.
public:

    // Default constructor
    Dog();

    // Member function declarations (implementations to follow)
    // Note that we use std::string here instead of placing
    // using namespace std;
    // above.
    // Never put a "using namespace" statement in a header.
    void setName(const std::string& dogsName);

    void setWeight(int dogsWeight);

    // Functions that do not modify the state of the object
    // should be marked as const.
    // This allows you to call them if given a const reference to the object.
    // Also note the functions must be explicitly declared as _virtual_
    // in order to be overridden in derived classes.
    // Functions are not virtual by default for performance reasons.
    virtual void print() const;

    // Functions can also be defined inside the class body.
    // Functions defined as such are automatically inlined.
    void bark() const { std::cout << name << " barks!\n"; }

    // Along with constructors, C++ provides destructors.
    // These are called when an object is deleted or falls out of scope.
    // This enables powerful paradigms such as RAII
    // (see below)
    // The destructor should be virtual if a class is to be derived from;
    // if it is not virtual, then the derived class' destructor will
    // not be called if the object is destroyed through a base-class reference
    // or pointer.
    virtual ~Dog();

}; // A semicolon must follow the class definition.

// Class member functions are usually implemented in .cpp files.
Dog::Dog()
{
    std::cout << "A dog has been constructed\n";
}

// Objects (such as strings) should be passed by reference
// if you are modifying them or const reference if you are not.
void Dog::setName(const std::string& dogsName)
{
    name = dogsName;
}

void Dog::setWeight(int dogsWeight)
{
    weight = dogsWeight;
}

// Notice that "virtual" is only needed in the declaration, not the definition.
void Dog::print() const
{
    std::cout << "Dog is " << name << " and weighs " << weight << "kg\n";
}

Dog::~Dog()
{
    std::cout << "Goodbye " << name << "\n";
}

int main() {
    Dog myDog; // prints "A dog has been constructed"
    myDog.setName("Barkley");
    myDog.setWeight(10);
    myDog.print(); // prints "Dog is Barkley and weighs 10 kg"
    return 0;
} // prints "Goodbye Barkley"

// Inheritance:

// This class inherits everything public and protected from the Dog class
// as well as private but may not directly access private members/methods
// without a public or protected method for doing so
class OwnedDog : public Dog {

public:
    void setOwner(const std::string& dogsOwner);

    // Override the behavior of the print function for all OwnedDogs. See
    // http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping
    // for a more general introduction if you are unfamiliar with
    // subtype polymorphism.
    // The override keyword is optional but makes sure you are actually
    // overriding the method in a base class.
    void print() const override;

private:
    std::string owner;
};

// Meanwhile, in the corresponding .cpp file:

void OwnedDog::setOwner(const std::string& dogsOwner)
{
    owner = dogsOwner;
}

void OwnedDog::print() const
{
    Dog::print(); // Call the print function in the base Dog class
    std::cout << "Dog is owned by " << owner << "\n";
    // Prints "Dog is <name> and weights <weight>"
    //        "Dog is owned by <owner>"
}

//////////////////////////////////////////
// Initialization and Operator Overloading
//////////////////////////////////////////

// In C++ you can overload the behavior of operators such as +, -, *, /, etc.
// This is done by defining a function which is called
// whenever the operator is used.

#include <iostream>
using namespace std;

class Point {
public:
    // Member variables can be given default values in this manner.
    double x = 0;
    double y = 0;

    // Define a default constructor which does nothing
    // but initialize the Point to the default value (0, 0)
    Point() { };

    // The following syntax is known as an initialization list
    // and is the proper way to initialize class member values
    Point (double a, double b) :
        x(a),
        y(b)
    { /* Do nothing except initialize the values */ }

    // Overload the + operator.
    Point operator+(const Point& rhs) const;

    // Overload the += operator
    Point& operator+=(const Point& rhs);

    // It would also make sense to add the - and -= operators,
    // but we will skip those for brevity.
};

Point Point::operator+(const Point& rhs) const
{
    // Create a new point that is the sum of this one and rhs.
    return Point(x + rhs.x, y + rhs.y);
}

Point& Point::operator+=(const Point& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

int main () {
    Point up (0,1);
    Point right (1,0);
    // This calls the Point + operator
    // Point up calls the + (function) with right as its parameter
    Point result = up + right;
    // Prints "Result is upright (1,1)"
    cout << "Result is upright (" << result.x << ',' << result.y << ")\n";
    return 0;
}

/////////////////////
// Templates
/////////////////////

// Templates in C++ are mostly used for generic programming, though they are
// much more powerful than generic constructs in other languages. They also
// support explicit and partial specialization and functional-style type
// classes; in fact, they are a Turing-complete functional language embedded
// in C++!

// We start with the kind of generic programming you might be familiar with. To
// define a class or function that takes a type parameter:
template<class T>
class Box {
public:
    // In this class, T can be used as any other type.
    void insert(const T&) { ... }
};

// During compilation, the compiler actually generates copies of each template
// with parameters substituted, so the full definition of the class must be
// present at each invocation. This is why you will see template classes defined
// entirely in header files.

// To instantiate a template class on the stack:
Box<int> intBox;

// and you can use it as you would expect:
intBox.insert(123);

// You can, of course, nest templates:
Box<Box<int> > boxOfBox;
boxOfBox.insert(intBox);

// Until C++11, you had to place a space between the two '>'s, otherwise '>>'
// would be parsed as the right shift operator.

// You will sometimes see
//   template<typename T>
// instead. The 'class' keyword and 'typename' keywords are _mostly_
// interchangeable in this case. For the full explanation, see
//   http://en.wikipedia.org/wiki/Typename
// (yes, that keyword has its own Wikipedia page).

// Similarly, a template function:
template<class T>
void barkThreeTimes(const T& input)
{
    input.bark();
    input.bark();
    input.bark();
}

// Notice that nothing is specified about the type parameters here. The compiler
// will generate and then type-check every invocation of the template, so the
// above function works with any type 'T' that has a const 'bark' method!

Dog fluffy;
fluffy.setName("Fluffy")
barkThreeTimes(fluffy); // Prints "Fluffy barks" three times.

// Template parameters don't have to be classes:
template<int Y>
void printMessage() {
  cout << "Learn C++ in " << Y << " minutes!" << endl;
}

// And you can explicitly specialize templates for more efficient code. Of
// course, most real-world uses of specialization are not as trivial as this.
// Note that you still need to declare the function (or class) as a template
// even if you explicitly specified all parameters.
template<>
void printMessage<10>() {
  cout << "Learn C++ faster in only 10 minutes!" << endl;
}

printMessage<20>();  // Prints "Learn C++ in 20 minutes!"
printMessage<10>();  // Prints "Learn C++ faster in only 10 minutes!"


/////////////////////
// Exception Handling
/////////////////////

// The standard library provides a few exception types
// (see http://en.cppreference.com/w/cpp/error/exception)
// but any type can be thrown an as exception
#include <exception>
#include <stdexcept>

// All exceptions thrown inside the _try_ block can be caught by subsequent
// _catch_ handlers.
try {
    // Do not allocate exceptions on the heap using _new_.
    throw std::runtime_error("A problem occurred");
}

// Catch exceptions by const reference if they are objects
catch (const std::exception& ex)
{
    std::cout << ex.what();
}

// Catches any exception not caught by previous _catch_ blocks
catch (...)
{
    std::cout << "Unknown exception caught";
    throw; // Re-throws the exception
}

///////
// RAII
///////

// RAII stands for "Resource Acquisition Is Initialization".
// It is often considered the most powerful paradigm in C++
// and is the simple concept that a constructor for an object
// acquires that object's resources and the destructor releases them.

// To understand how this is useful,
// consider a function that uses a C file handle:
void doSomethingWithAFile(const char* filename)
{
    // To begin with, assume nothing can fail.

    FILE* fh = fopen(filename, "r"); // Open the file in read mode.

    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

    fclose(fh); // Close the file handle.
}

// Unfortunately, things are quickly complicated by error handling.
// Suppose fopen can fail, and that doSomethingWithTheFile and
// doSomethingElseWithIt return error codes if they fail.
//  (Exceptions are the preferred way of handling failure,
//   but some programmers, especially those with a C background,
//   disagree on the utility of exceptions).
// We now have to check each call for failure and close the file handle
// if a problem occurred.
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Open the file in read mode
    if (fh == nullptr) // The returned pointer is null on failure.
        return false; // Report that failure to the caller.

    // Assume each function returns false if it failed
    if (!doSomethingWithTheFile(fh)) {
        fclose(fh); // Close the file handle so it doesn't leak.
        return false; // Propagate the error.
    }
    if (!doSomethingElseWithIt(fh)) {
        fclose(fh); // Close the file handle so it doesn't leak.
        return false; // Propagate the error.
    }

    fclose(fh); // Close the file handle so it doesn't leak.
    return true; // Indicate success
}

// C programmers often clean this up a little bit using goto:
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r");
    if (fh == nullptr)
        return false;

    if (!doSomethingWithTheFile(fh))
        goto failure;

    if (!doSomethingElseWithIt(fh))
        goto failure;

    fclose(fh); // Close the file
    return true; // Indicate success

failure:
    fclose(fh);
    return false; // Propagate the error
}

// If the functions indicate errors using exceptions,
// things are a little cleaner, but still sub-optimal.
void doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Open the file in read mode
    if (fh == nullptr)
        throw std::runtime_error("Could not open the file.");

    try {
        doSomethingWithTheFile(fh);
        doSomethingElseWithIt(fh);
    }
    catch (...) {
        fclose(fh); // Be sure to close the file if an error occurs.
        throw; // Then re-throw the exception.
    }

    fclose(fh); // Close the file
    // Everything succeeded
}

// Compare this to the use of C++'s file stream class (fstream)
// fstream uses its destructor to close the file.
// Recall from above that destructors are automatically called
// whenever an object falls out of scope.
void doSomethingWithAFile(const std::string& filename)
{
    // ifstream is short for input file stream
    std::ifstream fh(filename); // Open the file

    // Do things with the file
    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

} // The file is automatically closed here by the destructor

// This has _massive_ advantages:
// 1. No matter what happens,
//    the resource (in this case the file handle) will be cleaned up.
//    Once you write the destructor correctly,
//    It is _impossible_ to forget to close the handle and leak the resource.
// 2. Note that the code is much cleaner.
//    The destructor handles closing the file behind the scenes
//    without you having to worry about it.
// 3. The code is exception safe.
//    An exception can be thrown anywhere in the function and cleanup
//    will still occur.

// All idiomatic C++ code uses RAII extensively for all resources.
// Additional examples include
// - Memory using unique_ptr and shared_ptr
// - Containers - the standard library linked list,
//   vector (i.e. self-resizing array), hash maps, and so on
//   all automatically destroy their contents when they fall out of scope.
// - Mutexes using lock_guard and unique_lock

// containers with object keys of non-primitive values (custom classes) require
// compare function in the object itself or as a function pointer. Primitives
// have default comparators, but you can override it.
class Foo {
public:
    int j;
    Foo(int a) : j(a) {}
};
struct compareFunction {
    bool operator()(const Foo& a, const Foo& b) const {
        return a.j < b.j;
    }
};
//this isn't allowed (although it can vary depending on compiler)
//std::map<Foo, int> fooMap;
std::map<Foo, int, compareFunction> fooMap;
fooMap[Foo(1)]  = 1;
fooMap.find(Foo(1)); //true

///////////////////////////////////////
// Lambda Expressions (C++11 and above)
///////////////////////////////////////

// lambdas are a convenient way of defining an anonymous function
// object right at the location where it is invoked or passed as
// an argument to a function.

// For example, consider sorting a vector of pairs using the second
// value of the pair

vector<pair<int, int> > tester;
tester.push_back(make_pair(3, 6));
tester.push_back(make_pair(1, 9));
tester.push_back(make_pair(5, 0));

// Pass a lambda expression as third argument to the sort function
// sort is from the <algorithm> header

sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs) {
        return lhs.second < rhs.second;
    });

// Notice the syntax of the lambda expression,
// [] in the lambda is used to "capture" variables
// The "Capture List" defines what from the outside of the lambda should be available inside the function body and how.
// It can be either:
//     1. a value : [x]
//     2. a reference : [&x]
//     3. any variable currently in scope by reference [&]
//     4. same as 3, but by value [=]
// Example:

vector<int> dog_ids;
// number_of_dogs = 3;
for(int i = 0; i < 3; i++) {
	dog_ids.push_back(i);
}

int weight[3] = {30, 50, 10};

// Say you want to sort dog_ids according to the dogs' weights
// So dog_ids should in the end become: [2, 0, 1]

// Here's where lambda expressions come in handy

sort(dog_ids.begin(), dog_ids.end(), [&weight](const int &lhs, const int &rhs) {
        return weight[lhs] < weight[rhs];
    });
// Note we captured "weight" by reference in the above example.
// More on Lambdas in C++ : http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11

///////////////////////////////
// Range For (C++11 and above)
///////////////////////////////

// You can use a range for loop to iterate over a container
int arr[] = {1, 10, 3};

for(int elem: arr){
	cout << elem << endl;
}

// You can use "auto" and not worry about the type of the elements of the container
// For example:

for(auto elem: arr) {
	// Do something with each element of arr
}

/////////////////////
// Fun stuff
/////////////////////

// Aspects of C++ that may be surprising to newcomers (and even some veterans).
// This section is, unfortunately, wildly incomplete; C++ is one of the easiest
// languages with which to shoot yourself in the foot.

// You can override private methods!
class Foo {
  virtual void bar();
};
class FooSub : public Foo {
  virtual void bar();  // Overrides Foo::bar!
};


// 0 == false == NULL (most of the time)!
bool* pt = new bool;
*pt = 0; // Sets the value points by 'pt' to false.
pt = 0;  // Sets 'pt' to the null pointer. Both lines compile without warnings.

// nullptr is supposed to fix some of that issue:
int* pt2 = new int;
*pt2 = nullptr; // Doesn't compile
pt2 = nullptr;  // Sets pt2 to null.

// There is an exception made for bools.
// This is to allow you to test for null pointers with if(!ptr),
// but as a consequence you can assign nullptr to a bool directly!
*pt = nullptr;  // This still compiles, even though '*pt' is a bool!


// '=' != '=' != '='!
// Calls Foo::Foo(const Foo&) or some variant (see move semantics) copy
// constructor.
Foo f2;
Foo f1 = f2;

// Calls Foo::Foo(const Foo&) or variant, but only copies the 'Foo' part of
// 'fooSub'. Any extra members of 'fooSub' are discarded. This sometimes
// horrifying behavior is called "object slicing."
FooSub fooSub;
Foo f1 = fooSub;

// Calls Foo::operator=(Foo&) or variant.
Foo f1;
f1 = f2;


///////////////////////////////////////
// Tuples (C++11 and above)
///////////////////////////////////////

#include<tuple>

// Conceptually, Tuples are similar to  old data structures (C-like structs) but instead of having named data members,
// its elements are accessed by their order in the tuple.

// We start with constructing a tuple.
// Packing values into tuple
auto first = make_tuple(10, 'A');
const int maxN = 1e9;
const int maxL = 15;
auto second = make_tuple(maxN, maxL);

// Printing elements of 'first' tuple
cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A

// Printing elements of 'second' tuple
cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15

// Unpacking tuple into variables

int first_int;
char first_char;
tie(first_int, first_char) = first;
cout << first_int << " " << first_char << "\n";  // prints : 10 A

// We can also create tuple like this.

tuple<int, char, double> third(11, 'A', 3.14141);
// tuple_size returns number of elements in a tuple (as a constexpr)

cout << tuple_size<decltype(third)>::value << "\n"; // prints: 3

// tuple_cat concatenates the elements of all the tuples in the same order.

auto concatenated_tuple = tuple_cat(first, second, third);
// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A', 3.14141)

cout << get<0>(concatenated_tuple) << "\n"; // prints: 10
cout << get<3>(concatenated_tuple) << "\n"; // prints: 15
cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A'


/////////////////////
// Containers
/////////////////////

// Containers or the Standard Template Library are some predefined templates.
// They manage the storage space for its elements and provide
// member functions to access and manipulate them.

// Few containers are as follows:

// Vector (Dynamic array)
// Allow us to Define the Array or list of objects at run time
#include<vector>
vector<Data_Type> Vector_name; // used to initialize the vector
cin >> val;
Vector_name.push_back(val); // will push the value of variable into array

// To iterate through vector, we have 2 choices:
// Normal looping
for(int i=0; i<Vector_name.size(); i++)
// It will iterate through the vector from index '0' till last index

// Iterator
vector<Data_Type>::iterator it; // initialize the iterator for vector
for(it=vector_name.begin(); it!=vector_name.end();++it)

// For accessing the element of the vector
// Operator []
var = vector_name[index]; // Will assign value at that index to var


// Set
// Sets are containers that store unique elements following a specific order.
// Set is a very useful container to store unique values in sorted order
// without any other functions or code.

#include<set>
set<int> ST;    // Will initialize the set of int data type
ST.insert(30);  // Will insert the value 30 in set ST
ST.insert(10);  // Will insert the value 10 in set ST
ST.insert(20);  // Will insert the value 20 in set ST
ST.insert(30);  // Will insert the value 30 in set ST
// Now elements of sets are as follows
//  10 20 30

// To erase an element
ST.erase(20);  // Will erase element with value 20
// Set ST: 10 30
// To iterate through Set we use iterators
set<int>::iterator it;
for(it=ST.begin();it<ST.end();it++) {
	cout << *it << endl;
}
// Output:
// 10
// 30

// To clear the complete container we use Container_name.clear()
ST.clear();
cout << ST.size();  // will print the size of set ST
// Output: 0

// NOTE: for duplicate elements we can use multiset

// Map
// Maps store elements formed by a combination of a key value
// and a mapped value, following a specific order.

#include<map>
map<char, int> mymap;  // Will initialize the map with key as char and value as int

mymap.insert(pair<char,int>('A',1));
// Will insert value 1 for key A
mymap.insert(pair<char,int>('Z',26));
// Will insert value 26 for key Z

// To iterate
map<char,int>::iterator it;
for (it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << "->" << it->second << '\n';
// Output:
// A->1
// Z->26

// To find the value corresponding to a key
it = mymap.find('Z');
cout << it->second;

// Output: 26


///////////////////////////////////
// Logical and Bitwise operators
//////////////////////////////////

// Most of the operators in C++ are same as in other languages

// Logical operators

// C++ uses Short-circuit evaluation for boolean expressions, i.e, the second argument is executed or
// evaluated only if the first argument does not suffice to determine the value of the expression

true && false // Performs **logical and** to yield false
true || false // Performs **logical or** to yield true
! true        // Performs **logical not** to yield false

// Instead of using symbols equivalent keywords can be used
true and false // Performs **logical and** to yield false
true or false  // Performs **logical or** to yield true
not true       // Performs **logical not** to yield false

// Bitwise operators

// **<<** Left Shift Operator
// << shifts bits to the left
4 << 1 // Shifts bits of 4 to left by 1 to give 8
// x << n can be thought as x * 2^n


// **>>** Right Shift Operator
// >> shifts bits to the right
4 >> 1 // Shifts bits of 4 to right by 1 to give 2
// x >> n can be thought as x / 2^n

~4    // Performs a bitwise not
4 | 3 // Performs bitwise or
4 & 3 // Performs bitwise and
4 ^ 3 // Performs bitwise xor

// Equivalent keywords are
compl 4    // Performs a bitwise not
4 bitor 3  // Performs bitwise or
4 bitand 3 // Performs bitwise and
4 xor 3    // Performs bitwise xor


```
Further Reading:

An up-to-date language reference can be found at
<http://cppreference.com/w/cpp>

Additional resources may be found at <http://cplusplus.com>
---
language: c
filename: learnc.c
contributors:
    - ["Adam Bard", "http://adambard.com/"]
    - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"]
    - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"]
    - ["Marco Scannadinari", "https://marcoms.github.io"]
    - ["Zachary Ferguson", "https://github.io/zfergus2"]
    - ["himanshu", "https://github.com/himanshu81494"]
---

Ah, C. Still **the** language of modern high-performance computing.

C is the lowest-level language most programmers will ever use, but
it more than makes up for it with raw speed. Just be aware of its manual
memory management and C will take you as far as you need to go.

```c
// Single-line comments start with // - only available in C99 and later.

/*
Multi-line comments look like this. They work in C89 as well.
*/

/*
Multi-line comments don't nest /* Be careful */  // comment ends on this line...
*/ // ...not this one!

// Constants: #define <keyword>
// Constants are written in all-caps out of convention, not requirement
#define DAYS_IN_YEAR 365

// Enumeration constants are also ways to declare constants.
// All statements must end with a semicolon
enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
// MON gets 2 automatically, TUE gets 3, etc.

// Import headers with #include
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// (File names between <angle brackets> are headers from the C standard library.)
// For your own headers, use double quotes instead of angle brackets:
//#include "my_header.h"

// Declare function signatures in advance in a .h file, or at the top of
// your .c file.
void function_1();
int function_2(void);

// Must declare a 'function prototype' before main() when functions occur after
// your main() function.
int add_two_ints(int x1, int x2); // function prototype
// although `int add_two_ints(int, int);` is also valid (no need to name the args),
// it is recommended to name arguments in the prototype as well for easier inspection

// Your program's entry point is a function called
// main with an integer return type.
int main(void) {
  // your program
}

// The command line arguments used to run your program are also passed to main
// argc being the number of arguments - your program's name counts as 1
// argv is an array of character arrays - containing the arguments themselves
// argv[0] = name of your program, argv[1] = first argument, etc.
int main (int argc, char** argv)
{
  // print output using printf, for "print formatted"
  // %d is an integer, \n is a newline
  printf("%d\n", 0); // => Prints 0

  ///////////////////////////////////////
  // Types
  ///////////////////////////////////////

  // All variables MUST be declared at the top of the current block scope
  // we declare them dynamically along the code for the sake of the tutorial

  // ints are usually 4 bytes
  int x_int = 0;

  // shorts are usually 2 bytes
  short x_short = 0;

  // chars are guaranteed to be 1 byte
  char x_char = 0;
  char y_char = 'y'; // Char literals are quoted with ''

  // longs are often 4 to 8 bytes; long longs are guaranteed to be at least
  // 64 bits
  long x_long = 0;
  long long x_long_long = 0;

  // floats are usually 32-bit floating point numbers
  float x_float = 0.0f; // 'f' suffix here denotes floating point literal

  // doubles are usually 64-bit floating-point numbers
  double x_double = 0.0; // real numbers without any suffix are doubles

  // integer types may be unsigned (greater than or equal to zero)
  unsigned short ux_short;
  unsigned int ux_int;
  unsigned long long ux_long_long;

  // chars inside single quotes are integers in machine's character set.
  '0'; // => 48 in the ASCII character set.
  'A'; // => 65 in the ASCII character set.

  // sizeof(T) gives you the size of a variable with type T in bytes
  // sizeof(obj) yields the size of the expression (variable, literal, etc.).
  printf("%zu\n", sizeof(int)); // => 4 (on most machines with 4-byte words)

  // If the argument of the `sizeof` operator is an expression, then its argument
  // is not evaluated (except VLAs (see below)).
  // The value it yields in this case is a compile-time constant.
  int a = 1;
  // size_t is an unsigned integer type of at least 2 bytes used to represent
  // the size of an object.
  size_t size = sizeof(a++); // a++ is not evaluated
  printf("sizeof(a++) = %zu where a = %d\n", size, a);
  // prints "sizeof(a++) = 4 where a = 1" (on a 32-bit architecture)

  // Arrays must be initialized with a concrete size.
  char my_char_array[20]; // This array occupies 1 * 20 = 20 bytes
  int my_int_array[20]; // This array occupies 4 * 20 = 80 bytes
  // (assuming 4-byte words)

  // You can initialize an array to 0 thusly:
  char my_array[20] = {0};

  // Indexing an array is like other languages -- or,
  // rather, other languages are like C
  my_array[0]; // => 0

  // Arrays are mutable; it's just memory!
  my_array[1] = 2;
  printf("%d\n", my_array[1]); // => 2

  // In C99 (and as an optional feature in C11), variable-length arrays (VLAs)
  // can be declared as well. The size of such an array need not be a compile
  // time constant:
  printf("Enter the array size: "); // ask the user for an array size
  int array_size;
  fscanf(stdin, "%d", &array_size);
  int var_length_array[array_size]; // declare the VLA
  printf("sizeof array = %zu\n", sizeof var_length_array);

  // Example:
  // > Enter the array size: 10
  // > sizeof array = 40

  // Strings are just arrays of chars terminated by a NULL (0x00) byte,
  // represented in strings as the special character '\0'.
  // (We don't have to include the NULL byte in string literals; the compiler
  //  inserts it at the end of the array for us.)
  char a_string[20] = "This is a string";
  printf("%s\n", a_string); // %s formats a string

  printf("%d\n", a_string[16]); // => 0
  // i.e., byte #17 is 0 (as are 18, 19, and 20)

  // If we have characters between single quotes, that's a character literal.
  // It's of type `int`, and *not* `char` (for historical reasons).
  int cha = 'a'; // fine
  char chb = 'a'; // fine too (implicit conversion from int to char)

  // Multi-dimensional arrays:
  int multi_array[2][5] = {
    {1, 2, 3, 4, 5},
    {6, 7, 8, 9, 0}
  };
  // access elements:
  int array_int = multi_array[0][2]; // => 3

  ///////////////////////////////////////
  // Operators
  ///////////////////////////////////////

  // Shorthands for multiple declarations:
  int i1 = 1, i2 = 2;
  float f1 = 1.0, f2 = 2.0;

  int b, c;
  b = c = 0;

  // Arithmetic is straightforward
  i1 + i2; // => 3
  i2 - i1; // => 1
  i2 * i1; // => 2
  i1 / i2; // => 0 (0.5, but truncated towards 0)

  // You need to cast at least one integer to float to get a floating-point result
  (float)i1 / i2; // => 0.5f
  i1 / (double)i2; // => 0.5 // Same with double
  f1 / f2; // => 0.5, plus or minus epsilon
  // Floating-point numbers and calculations are not exact

  // Modulo is there as well
  11 % 3; // => 2

  // Comparison operators are probably familiar, but
  // there is no Boolean type in C. We use ints instead.
  // (Or _Bool or bool in C99.)
  // 0 is false, anything else is true. (The comparison
  // operators always yield 0 or 1.)
  3 == 2; // => 0 (false)
  3 != 2; // => 1 (true)
  3 > 2; // => 1
  3 < 2; // => 0
  2 <= 2; // => 1
  2 >= 2; // => 1

  // C is not Python - comparisons don't chain.
  // Warning: The line below will compile, but it means `(0 < a) < 2`.
  // This expression is always true, because (0 < a) could be either 1 or 0.
  // In this case it's 1, because (0 < 1).
  int between_0_and_2 = 0 < a < 2;
  // Instead use:
  int between_0_and_2 = 0 < a && a < 2;

  // Logic works on ints
  !3; // => 0 (Logical not)
  !0; // => 1
  1 && 1; // => 1 (Logical and)
  0 && 1; // => 0
  0 || 1; // => 1 (Logical or)
  0 || 0; // => 0

  // Conditional ternary expression ( ? : )
  int e = 5;
  int f = 10;
  int z;
  z = (e > f) ? e : f; // => 10 "if e > f return e, else return f."

  // Increment and decrement operators:
  int j = 0;
  int s = j++; // Return j THEN increase j. (s = 0, j = 1)
  s = ++j; // Increase j THEN return j. (s = 2, j = 2)
  // same with j-- and --j

  // Bitwise operators!
  ~0x0F; // => 0xFFFFFFF0 (bitwise negation, "1's complement", example result for 32-bit int)
  0x0F & 0xF0; // => 0x00 (bitwise AND)
  0x0F | 0xF0; // => 0xFF (bitwise OR)
  0x04 ^ 0x0F; // => 0x0B (bitwise XOR)
  0x01 << 1; // => 0x02 (bitwise left shift (by 1))
  0x02 >> 1; // => 0x01 (bitwise right shift (by 1))

  // Be careful when shifting signed integers - the following are undefined:
  // - shifting into the sign bit of a signed integer (int a = 1 << 31)
  // - left-shifting a negative number (int a = -1 << 2)
  // - shifting by an offset which is >= the width of the type of the LHS:
  //   int a = 1 << 32; // UB if int is 32 bits wide

  ///////////////////////////////////////
  // Control Structures
  ///////////////////////////////////////

  if (0) {
    printf("I am never run\n");
  } else if (0) {
    printf("I am also never run\n");
  } else {
    printf("I print\n");
  }

  // While loops exist
  int ii = 0;
  while (ii < 10) { //ANY value less than ten is true.
    printf("%d, ", ii++); // ii++ increments ii AFTER using its current value.
  } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

  printf("\n");

  int kk = 0;
  do {
    printf("%d, ", kk);
  } while (++kk < 10); // ++kk increments kk BEFORE using its current value.
  // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

  printf("\n");

  // For loops too
  int jj;
  for (jj=0; jj < 10; jj++) {
    printf("%d, ", jj);
  } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

  printf("\n");

  // *****NOTES*****:
  // Loops and Functions MUST have a body. If no body is needed:
  int i;
  for (i = 0; i <= 5; i++) {
    ; // use semicolon to act as the body (null statement)
  }
  // Or
  for (i = 0; i <= 5; i++);

  // branching with multiple choices: switch()
  switch (a) {
  case 0: // labels need to be integral *constant* expressions
    printf("Hey, 'a' equals 0!\n");
    break; // if you don't break, control flow falls over labels
  case 1:
    printf("Huh, 'a' equals 1!\n");
    break;
    // Be careful - without a "break", execution continues until the
    // next "break" is reached.
  case 3:
  case 4:
    printf("Look at that.. 'a' is either 3, or 4\n");
    break;
  default:
    // if `some_integral_expression` didn't match any of the labels
    fputs("Error!\n", stderr);
    exit(-1);
    break;
  }
  /*
  using "goto" in C
  */
  typedef enum { false, true } bool;
  // for C don't have bool as data type :(
  bool disaster = false;
  int i, j;
  for(i=0;i<100;++i)
  for(j=0;j<100;++j)
  {
    if((i + j) >= 150)
        disaster = true;
    if(disaster)
        goto error;
  }
  error :
  printf("Error occurred at i = %d & j = %d.\n", i, j);
  /*
  https://ideone.com/GuPhd6
  this will print out "Error occurred at i = 52 & j = 99."
  */

  ///////////////////////////////////////
  // Typecasting
  ///////////////////////////////////////

  // Every value in C has a type, but you can cast one value into another type
  // if you want (with some constraints).

  int x_hex = 0x01; // You can assign vars with hex literals

  // Casting between types will attempt to preserve their numeric values
  printf("%d\n", x_hex); // => Prints 1
  printf("%d\n", (short) x_hex); // => Prints 1
  printf("%d\n", (char) x_hex); // => Prints 1

  // Types will overflow without warning
  printf("%d\n", (unsigned char) 257); // => 1 (Max char = 255 if char is 8 bits long)

  // For determining the max value of a `char`, a `signed char` and an `unsigned char`,
  // respectively, use the CHAR_MAX, SCHAR_MAX and UCHAR_MAX macros from <limits.h>

  // Integral types can be cast to floating-point types, and vice-versa.
  printf("%f\n", (float)100); // %f formats a float
  printf("%lf\n", (double)100); // %lf formats a double
  printf("%d\n", (char)100.0);

  ///////////////////////////////////////
  // Pointers
  ///////////////////////////////////////

  // A pointer is a variable declared to store a memory address. Its declaration will
  // also tell you the type of data it points to. You can retrieve the memory address
  // of your variables, then mess with them.

  int x = 0;
  printf("%p\n", (void *)&x); // Use & to retrieve the address of a variable
  // (%p formats an object pointer of type void *)
  // => Prints some address in memory;

  // Pointers start with * in their declaration
  int *px, not_a_pointer; // px is a pointer to an int
  px = &x; // Stores the address of x in px
  printf("%p\n", (void *)px); // => Prints some address in memory
  printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer));
  // => Prints "8, 4" on a typical 64-bit system

  // To retrieve the value at the address a pointer is pointing to,
  // put * in front to dereference it.
  // Note: yes, it may be confusing that '*' is used for _both_ declaring a
  // pointer and dereferencing it.
  printf("%d\n", *px); // => Prints 0, the value of x

  // You can also change the value the pointer is pointing to.
  // We'll have to wrap the dereference in parenthesis because
  // ++ has a higher precedence than *.
  (*px)++; // Increment the value px is pointing to by 1
  printf("%d\n", *px); // => Prints 1
  printf("%d\n", x); // => Prints 1

  // Arrays are a good way to allocate a contiguous block of memory
  int x_array[20]; //declares array of size 20 (cannot change size)
  int xx;
  for (xx = 0; xx < 20; xx++) {
    x_array[xx] = 20 - xx;
  } // Initialize x_array to 20, 19, 18,... 2, 1

  // Declare a pointer of type int and initialize it to point to x_array
  int* x_ptr = x_array;
  // x_ptr now points to the first element in the array (the integer 20).
  // This works because arrays often decay into pointers to their first element.
  // For example, when an array is passed to a function or is assigned to a pointer,
  // it decays into (implicitly converted to) a pointer.
  // Exceptions: when the array is the argument of the `&` (address-of) operator:
  int arr[10];
  int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`!
  // It's of type "pointer to array" (of ten `int`s).
  // or when the array is a string literal used for initializing a char array:
  char otherarr[] = "foobarbazquirk";
  // or when it's the argument of the `sizeof` or `alignof` operator:
  int arraythethird[10];
  int *ptr = arraythethird; // equivalent with int *ptr = &arr[0];
  printf("%zu, %zu\n", sizeof arraythethird, sizeof ptr);
  // probably prints "40, 4" or "40, 8"

  // Pointers are incremented and decremented based on their type
  // (this is called pointer arithmetic)
  printf("%d\n", *(x_ptr + 1)); // => Prints 19
  printf("%d\n", x_array[1]); // => Prints 19

  // You can also dynamically allocate contiguous blocks of memory with the
  // standard library function malloc, which takes one argument of type size_t
  // representing the number of bytes to allocate (usually from the heap, although this
  // may not be true on e.g. embedded systems - the C standard says nothing about it).
  int *my_ptr = malloc(sizeof(*my_ptr) * 20);
  for (xx = 0; xx < 20; xx++) {
    *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx
  } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints)

  // Note that there is no standard way to get the length of a
  // dynamically allocated array in C. Because of this, if your arrays are
  // going to be passed around your program a lot, you need another variable
  // to keep track of the number of elements (size) of an array. See the
  // functions section for more info.
  int size = 10;
  int *my_arr = malloc(sizeof(int) * size);
  // Add an element to the array
  size++;
  my_arr = realloc(my_arr, sizeof(int) * size);
  my_arr[10] = 5;

  // Dereferencing memory that you haven't allocated gives
  // "unpredictable results" - the program is said to invoke "undefined behavior"
  printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash.

  // When you're done with a malloc'd block of memory, you need to free it,
  // or else no one else can use it until your program terminates
  // (this is called a "memory leak"):
  free(my_ptr);

  // Strings are arrays of char, but they are usually represented as a
  // pointer-to-char (which is a pointer to the first element of the array).
  // It's good practice to use `const char *' when referring to a string literal,
  // since string literals shall not be modified (i.e. "foo"[0] = 'a' is ILLEGAL.)
  const char *my_str = "This is my very own string literal";
  printf("%c\n", *my_str); // => 'T'

  // This is not the case if the string is an array
  // (potentially initialized with a string literal)
  // that resides in writable memory, as in:
  char foo[] = "foo";
  foo[0] = 'a'; // this is legal, foo now contains "aoo"

  function_1();
} // end main function

///////////////////////////////////////
// Functions
///////////////////////////////////////

// Function declaration syntax:
// <return type> <function name>(<args>)

int add_two_ints(int x1, int x2)
{
  return x1 + x2; // Use return to return a value
}

/*
Functions are call by value. When a function is called, the arguments passed to
the function are copies of the original arguments (except arrays). Anything you
do to the arguments in the function do not change the value of the original
argument where the function was called.

Use pointers if you need to edit the original argument values.

Example: in-place string reversal
*/

// A void function returns no value
void str_reverse(char *str_in)
{
  char tmp;
  int ii = 0;
  size_t len = strlen(str_in); // `strlen()` is part of the c standard library
  for (ii = 0; ii < len / 2; ii++) {
    tmp = str_in[ii];
    str_in[ii] = str_in[len - ii - 1]; // ii-th char from end
    str_in[len - ii - 1] = tmp;
  }
}
//NOTE: string.h header file needs to be included to use strlen()

/*
char c[] = "This is a test.";
str_reverse(c);
printf("%s\n", c); // => ".tset a si sihT"
*/
/*
as we can return only one variable
to change values of more than one variables we use call by reference
*/
void swapTwoNumbers(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
/*
int first = 10;
int second = 20;
printf("first: %d\nsecond: %d\n", first, second);
swapTwoNumbers(&first, &second);
printf("first: %d\nsecond: %d\n", first, second);
// values will be swapped
*/

/*
With regards to arrays, they will always be passed to functions
as pointers. Even if you statically allocate an array like `arr[10]`,
it still gets passed as a pointer to the first element in any function calls.
Again, there is no standard way to get the size of a dynamically allocated
array in C.
*/
// Size must be passed!
// Otherwise, this function has no way of knowing how big the array is.
void printIntArray(int *arr, int size) {
    int i;
    for (i = 0; i < size; i++) {
        printf("arr[%d] is: %d\n", i, arr[i]);
    }
}
/*
int my_arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int size = 10;
printIntArray(my_arr, size);
// will print "arr[0] is: 1" etc
*/

// if referring to external variables outside function, must use extern keyword.
int i = 0;
void testFunc() {
  extern int i; //i here is now using external variable i
}

// make external variables private to source file with static:
static int j = 0; //other files using testFunc2() cannot access variable j
void testFunc2() {
  extern int j;
}
//**You may also declare functions as static to make them private**

///////////////////////////////////////
// User-defined types and structs
///////////////////////////////////////

// Typedefs can be used to create type aliases
typedef int my_type;
my_type my_type_var = 0;

// Structs are just collections of data, the members are allocated sequentially,
// in the order they are written:
struct rectangle {
  int width;
  int height;
};

// It's not generally true that
// sizeof(struct rectangle) == sizeof(int) + sizeof(int)
// due to potential padding between the structure members (this is for alignment
// reasons). [1]

void function_1()
{
  struct rectangle my_rec;

  // Access struct members with .
  my_rec.width = 10;
  my_rec.height = 20;

  // You can declare pointers to structs
  struct rectangle *my_rec_ptr = &my_rec;

  // Use dereferencing to set struct pointer members...
  (*my_rec_ptr).width = 30;

  // ... or even better: prefer the -> shorthand for the sake of readability
  my_rec_ptr->height = 10; // Same as (*my_rec_ptr).height = 10;
}

// You can apply a typedef to a struct for convenience
typedef struct rectangle rect;

int area(rect r)
{
  return r.width * r.height;
}

// if you have large structs, you can pass them "by pointer" to avoid copying
// the whole struct:
int areaptr(const rect *r)
{
  return r->width * r->height;
}

///////////////////////////////////////
// Function pointers
///////////////////////////////////////
/*
At run time, functions are located at known memory addresses. Function pointers are
much like any other pointer (they just store a memory address), but can be used
to invoke functions directly, and to pass handlers (or callback functions) around.
However, definition syntax may be initially confusing.

Example: use str_reverse from a pointer
*/
void str_reverse_through_pointer(char *str_in) {
  // Define a function pointer variable, named f.
  void (*f)(char *); // Signature should exactly match the target function.
  f = &str_reverse; // Assign the address for the actual function (determined at run time)
  // f = str_reverse; would work as well - functions decay into pointers, similar to arrays
  (*f)(str_in); // Just calling the function through the pointer
  // f(str_in); // That's an alternative but equally valid syntax for calling it.
}

/*
As long as function signatures match, you can assign any function to the same pointer.
Function pointers are usually typedef'd for simplicity and readability, as follows:
*/

typedef void (*my_fnp_type)(char *);

// Then used when declaring the actual pointer variable:
// ...
// my_fnp_type f;

//Special characters:
/*
'\a'; // alert (bell) character
'\n'; // newline character
'\t'; // tab character (left justifies text)
'\v'; // vertical tab
'\f'; // new page (form feed)
'\r'; // carriage return
'\b'; // backspace character
'\0'; // NULL character. Usually put at end of strings in C.
//   hello\n\0. \0 used by convention to mark end of string.
'\\'; // backslash
'\?'; // question mark
'\''; // single quote
'\"'; // double quote
'\xhh'; // hexadecimal number. Example: '\xb' = vertical tab character
'\0oo'; // octal number. Example: '\013' = vertical tab character

//print formatting:
"%d";    // integer
"%3d";   // integer with minimum of length 3 digits (right justifies text)
"%s";    // string
"%f";    // float
"%ld";   // long
"%3.2f"; // minimum 3 digits left and 2 digits right decimal float
"%7.4s"; // (can do with strings too)
"%c";    // char
"%p";    // pointer
"%x";    // hexadecimal
"%o";    // octal
"%%";    // prints %
*/

///////////////////////////////////////
// Order of Evaluation
///////////////////////////////////////

//---------------------------------------------------//
//        Operators                  | Associativity //
//---------------------------------------------------//
// () [] -> .                        | left to right //
// ! ~ ++ -- + = *(type)sizeof       | right to left //
// * / %                             | left to right //
// + -                               | left to right //
// << >>                             | left to right //
// < <= > >=                         | left to right //
// == !=                             | left to right //
// &                                 | left to right //
// ^                                 | left to right //
// |                                 | left to right //
// &&                                | left to right //
// ||                                | left to right //
// ?:                                | right to left //
// = += -= *= /= %= &= ^= |= <<= >>= | right to left //
// ,                                 | left to right //
//---------------------------------------------------//

/******************************* Header Files **********************************

Header files are an important part of C as they allow for the connection of C
source files and can simplify code and definitions by separating them into
separate files.

Header files are syntactically similar to C source files but reside in ".h"
files. They can be included in your C source file by using the precompiler
command #include "example.h", given that example.h exists in the same directory
as the C file.
*/

/* A safe guard to prevent the header from being defined too many times. This */
/* happens in the case of circle dependency, the contents of the header is    */
/* already defined.                                                           */
#ifndef EXAMPLE_H /* if EXAMPLE_H is not yet defined. */
#define EXAMPLE_H /* Define the macro EXAMPLE_H. */

/* Other headers can be included in headers and therefore transitively */
/* included into files that include this header.                       */
#include <string.h>

/* Like c source files macros can be defined in headers and used in files */
/* that include this header file.                                         */
#define EXAMPLE_NAME "Dennis Ritchie"
/* Function macros can also be defined. */
#define ADD(a, b) (a + b)

/* Structs and typedefs can be used for consistency between files. */
typedef struct node
{
    int val;
    struct node *next;
} Node;

/* So can enumerations. */
enum traffic_light_state {GREEN, YELLOW, RED};

/* Function prototypes can also be defined here for use in multiple files,  */
/* but it is bad practice to define the function in the header. Definitions */
/* should instead be put in a C file.                                       */
Node createLinkedList(int *vals, int len);

/* Beyond the above elements, other definitions should be left to a C source */
/* file. Excessive includes or definitions should, also not be contained in */
/* a header file but instead put into separate headers or a C file.          */

#endif /* End of the if precompiler directive. */

```
## Further Reading

Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language)
It is *the* book about C, written by Dennis Ritchie, the creator of C, and Brian Kernighan. Be careful, though - it's ancient and it contains some
inaccuracies (well, ideas that are not considered good anymore) or now-changed practices.

Another good resource is [Learn C The Hard Way](http://c.learncodethehardway.org/book/).

If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com).

It's very important to use proper spacing, indentation and to be consistent with your coding style in general.
Readable code is better than clever code and fast code. For a good, sane coding style to adopt, see the
[Linux kernel coding style](https://www.kernel.org/doc/Documentation/process/coding-style.rst).

Other than that, Google is your friend.

[1] [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member)
---
name: Go
category: language
language: Go
lang: ca-es
filename: learngo-ca.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
    - ["Alexej Friesen", "https://github.com/heyalexej"]
    - ["Clayton Walker", "https://github.com/cwalk"]
    - ["Leonid Shevtsov", "https://github.com/leonid-shevtsov"]
translators:
    - ["Xavier Sala", "http://github.com/utrescu"]
---

Go es va crear degut a la necessitat de fer la feina ràpidament. No segueix
la darrera tendència en informàtica, però és la nova forma i la més ràpida de
resoldre problemes reals.

Té conceptes familiars de llenguatges imperatius amb tipat estàtic. És ràpid
compilant i ràpid al executar, afegeix una forma fàcil d'entedre de
concurrència per CPUs de diferents núclis i té característiques que ajuden en
la programació a gran escala.

Go té una gran llibreria de funcions estàndard i una comunitat d'usuaris
entusiasta.

```go
// Comentari d'una sola línia
/* Comentari
multilínia */

// La clausula `package` apareix sempre a sobre de cada fitxer de codi font.
// Quan es desenvolupa un executable en comptes d'una llibreria el nom que
// s'ha de fer servir de `package` ha de ser 'main'.
package main

// `import` es fa servir per indicar quins paquets de llibreries fa servir
// aquest fitxer.
import (
	"fmt"       // Un paquet de la llibreria estàndard de Go.
	"io/ioutil" // Les funcions ioutil de io
	m "math"    // La llibreria de matemàtiques que es referenciarà com a m.
	"net/http"  // Si, un servidor web!
	"os"        // funcions per treballar amb el sistema de fitxers
	"strconv"   // Conversions de cadenes
)

// La definició d'una funció. `main` és especial. És el punt d'entrada per
// l'executable. Tant si t'agrada com si no, Go fa servir corxets.
func main() {
	// Println imprimeix una línia al canal de sortida.
	// Es qualifica amb el nom del paquet, fmt.
	fmt.Println("Hola món!")

	// Crida a una altra funció dins d'aquest paquet.
	mesEnllaDeHola()
}

// Els paràmetres de les funcions es posen dins de parèntesis.
// Els parèntesis fan falta encara que no hi hagi cap paràmetre.
func mesEnllaDeHola() {
	var x int // Declaració d'una variable.
	          // S'han de declarar abans de fer-les servir.
	x = 3     // Assignació d'una variable
	// Hi ha una forma "Curta" amb :=
	// Descobreix el tipus, declara la variable i li assigna valor.
	y := 4
	sum, prod := learnMultiple(x, y)        // La funció retorna dos valors.
	fmt.Println("sum:", sum, "prod:", prod) // Sortida simple.
	aprenTipus()                            // < y minuts, aprèn més!
}

/* <- comentari multilínia
Les funcions poden tenir paràmetres i (multiples!) valors de retorn.
Aquí `x`, `y` són els argumens i `sum` i `prod` són els valors retornats.
Fixa't que `x` i `sum` reben el tipus `int`.
*/
func learnMultiple(x, y int) (sum, prod int) {
	return x + y, x * y // Retorna dos valors.
}

// Alguns tipus incorporats i literals.
func aprenTipus() {
	// Normalment la declaració curta et dóna el que vols.
	str := "Learn Go!" // tipus string

	s2 := `Un tipus cadena "normal" pot tenir
salts de línia.` // El mateix tipus

	// literals Non-ASCII literal. El tipus de Go és UTF-8.
	g := 'Σ' // El tipus rune, és un àlies de int32 conté un caràcter unicode.

	f := 3.14195 // float64, un número de 64 bits amb coma flotant IEEE-754.
	c := 3 + 4i  // complex128, representat internament amb dos float64.

	// Sintaxi amb var i inicialitzadors.
	var u uint = 7 // Sense signe, però depèn de la mida com els int.
	var pi float32 = 22. / 7

	// Conversió de tipus amb declaració curta.
	n := byte('\n') // byte és un àlies de uint8.

	// Les taules tenen mida fixa en temps de compilació.
	var a4 [4]int           // Taula de 4 enters inicialitzats a zero.
	a3 := [...]int{3, 1, 5} // Taula inicialitzada amb tres elements
	// amb els valors 3, 1, i 5.

	// Els "Slices" tenen mida dinàmica. Tant les taules com els "slices"
	// tenen avantatges però és més habitual que es facin servir slices.
	s3 := []int{4, 5, 9}    // Compara amb a3. Aquí no hi ha els tres punts
	s4 := make([]int, 4)    // Crea un slice de 4 enters inicialitzats a zero.
	var d2 [][]float64      // Només es declara però no hi ha valors.
	bs := []byte("a slice") // Sintaxi de conversió de tipus.

	// Com que són dinàmics es poden afegir valors nous als slices.
	// Per afegir-hi elements es fa servir el mètode append().
	// El primer argument és l'slice en el que s'afegeix.
	// Sovint ell mateix com aquí sota.
	s := []int{1, 2, 3}		// Un slice amb tres elements.
	s = append(s, 4, 5, 6)	// Ara s tindrà tres elements més
	fmt.Println(s) // El resultat serà [1 2 3 4 5 6]

	// Per afegir un slice dins d'un altre en comptes de valors atòmics
	// S'hi pot passar una referència a l'altre slice o un literal acabat
	// amb tres punts, que vol dir que s'ha de desempaquetar els elements
	// i afegir-los a "s"
	s = append(s, []int{7, 8, 9}...) // El segon argument és un slice
	fmt.Println(s)	// El resultat ara és [1 2 3 4 5 6 7 8 9]

	p, q := aprenMemoria() // Declara p i q com a punters de int.
	fmt.Println(*p, *q)   // * segueix el punter fins a trobar els valors

	// Els "Mapes" són taules dinàmiques associatives com els hash o els
	// diccionaris d'altres llenguatges.
	m := map[string]int{"tres": 3, "quatre": 4}
	m["un"] = 1

	// En Go les variables que no es fan servir generen un error.
	// El subratllat permet fer servir una variable i descartar-ne el valor.
	_, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs
	// És útil per descartar algun dels valors retornats per una funció
	// Per exemple, es pot ignorar l'error retornat per os.Create amb la idea
	// de que sempre es crearà.
	file, _ := os.Create("output.txt")
	fmt.Fprint(file, "Així es pot escriure en un fitxer")
	file.Close()

	// La sortida compta com a ús d'una variable.
	fmt.Println(s, c, a4, s3, d2, m)

	aprenControlDeFluxe() // Tornem.
}

// A diferència d'altres llenguatges les funcions poden retornar valors amb
// nom. Assignant un nom al valor retornat en la declaració de la funció
// permet retornar valors des de diferents llocs del programa a més de posar
// el return sense valors
func aprenRetornAmbNoms(x, y int) (z int) {
	z = x * y
	return // el retorn de z és implícit perquè ja té valor
}

// Go té un recollidor de basura.
// Té punters però no té aritmetica de punters
// Es poden cometre errors amb un punter a nil però no incrementant-lo.
func aprenMemoria() (p, q *int) {
	// Els valors retornats p i q són punters a un enter.
	p = new(int) // Funció per reservar memòria
	// A la memòria ja hi ha un 0 per defecte, no és nil.
	s := make([]int, 20) // Reserva un bloc de memòria de 20 enters.
	s[3] = 7             // Assigna un valor a un d'ells.
	r := -2              // Declare una altra variable local.
	return &s[3], &r     // & agafa l'adreça d'un objecte.
}

func expensiveComputation() float64 {
	return m.Exp(10)
}

func aprenControlDeFluxe() {
	// Els "If" necessiten corxets però no parèntesis.
	if true {
		fmt.Println("ja ho hem vist")
	}
	// El format del codi està estandaritzat amb la comanda "go fmt."
	if false {
		// Pout.
	} else {
		// Gloat.
	}
	// Si cal encadenar ifs és millor fer servir switch.
	x := 42.0
	switch x {
	case 0:
	case 1:
	case 42:
		// Els case no "passen a través" no cal "break" per separar-los.
		/*
		Per fer-ho hi ha una comanda `fallthrough`, mireu:
		  https://github.com/golang/go/wiki/Switch#fall-through
		*/
	case 43:
		// No hi arriba.
	default:
		// La opció "default" és opcional
	}
	// El 'for' tampoc necessita parèntesis, com el 'if'.
	// Les variables dins d'un bloc if o for són local del bloc.
	for x := 0; x < 3; x++ { // ++ is a statement.
		fmt.Println("iteració", x)
	}
	// x == 42.

	// L'única forma de fer bucles en Go és el 'for' però té moltes variants.
	for { // bucle infinit.
		break    // És una broma!.
		continue // No hi arriba mai.
	}

	// Es fa servir "range" per iterar a una taula, un slice, un mapa
	// o un canal.
	// range torna un valor (channel) o dos (array, slice, string o map).
	for key, value := range map[string]int{"un": 1, "dos": 2, "tres": 3} {
		// Per cada parell del mapa imprimeix la clau i el valor.
		fmt.Printf("clau=%s, valor=%d\n", key, value)
	}
	// Si només cal el valor es pot fer servir _
	for _, name := range []string{"Robert", "Bill", "Josep"} {
		fmt.Printf("Hola, %s\n", name)
	}

	// Es pot usar := per declarar i assignar valors i després
	// comprovar-lo y > x.
	if y := expensiveComputation(); y > x {
		x = y
	}
	// Les funcions literals són closures
	xBig := func() bool {
		return x > 10000 // Referencia a x declarada sobre el switch.
	}
	x = 99999
	fmt.Println("xBig:", xBig()) // cert
	x = 1.3e3                    //  x val 1300
	fmt.Println("xBig:", xBig()) // fals.

	// A més les funcions poden ser definides i cridades com a arguments per
	// una funció sempre que:
	// a) La funció es cridi inmediatament (),
	// b) El tipus del resultat sigui del tipus esperat de l'argument.
	fmt.Println("Suma i duplica dos números: ",
		func(a, b int) int {
			return (a + b) * 2
		}(10, 2)) // Es crida amb els arguments 10 i 2
	// => Suma i duplica dos números: 24

	// Quan el necessitis t'agradarà que hi sigui
	goto love
love:

	aprenFabricaDeFuncions() // func que retorna func és divertit(3)(3)
	aprenDefer()      // Revisió ràpida d'una paraula clau.
	aprendreInterficies() // Bon material properament!
}

func aprenFabricaDeFuncions() {
	// Les dues seguents són equivalents, però la segona és més pràctica
	fmt.Println(sentenceFactory("dia")("Un bonic", "d'estiu!"))

	d := sentenceFactory("dia")
	fmt.Println(d("Un bonic", "d'estiu!"))
	fmt.Println(d("Un tranquil", "de primavera!"))
}

// Els decoradors són habituals en altres llenguatges.
// Es pot fer el mateix en Go amb funcions literals que accepten arguments.
func sentenceFactory(mystring string) func(before, after string) string {
	return func(before, after string) string {
		return fmt.Sprintf("%s %s %s", before, mystring, after) // nou string
	}
}

func aprenDefer() (ok bool) {
	// Les comandes marcades amb defer s'executen després de que la funció
	// hagi acabat.
	defer fmt.Println("Les comandes defer s'executen en ordre invers (LIFO).")
	defer fmt.Println("\nAquesta és la primera línia que s'imprimeix")
	// Defer es fa servir gairebé sempre per tancar un fitxer, en el moment
	// en que acaba el mètode.
	return true
}

// Defineix Stringer com un tipus interfície amb el mètode String().
type Stringer interface {
	String() string
}

// Defineix una estrutura que conté un parell d'enters, x i y.
type parell struct {
	x, y int
}

// Defineix un mètode de l'estructura parell. Ara parell implementa Stringer.
func (p parell) String() string { // p és anomenat el "receptor"
	// Sprintf és una funció del paquet fmt.
	// Fa referència als camps de p.
	return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func aprendreInterficies() {
	// La sintaxi de claus es pot fer servir per inicialitzar un "struct".
	// Gràcies a := defineix i inicialitza p com un struct 'parell'.
	p := parell{3, 4}
	fmt.Println(p.String()) // Es crida al mètode de p.
	var i Stringer          // Declara i de tipus Stringer.
	i = p                   // parell implementa Stringer per tant és vàlid.
	// Es pot cridar el mètode String() igual que abans.
	fmt.Println(i.String())

	// Les funcions de fmt criden a String() per aconseguir una representació
	// imprimible d'un objecte.
	fmt.Println(p) // Treu el mateix d'abans. Println crida el mètode String.
	fmt.Println(i) // Idèntic resultat

	aprendreParamentesVariables("Aquí", "estem", "aprenent!")
}

// Les funcions poden tenir paràmetres variables.
func aprendreParamentesVariables(myStrings ...interface{}) {
	// Itera per cada un dels valors dels paràmetres
	// Ignorant l'índex de la seva posició
	for _, param := range myStrings {
		fmt.Println("paràmetre:", param)
	}

	// Passa el valor de múltipes variables com a paràmetre.
	fmt.Println("parametres:", fmt.Sprintln(myStrings...))

	aprenControlErrors()
}

func aprenControlErrors() {
	// ", ok" Es fa servir per saber si hi és o no.
	m := map[int]string{3: "tres", 4: "quatre"}
	if x, ok := m[1]; !ok { // ok serà fals perquè 1 no està en el mapa.
		fmt.Println("no hi és")
	} else {
		fmt.Print(x) // x seria el valor, si no estés en el mapa.
	}
	// Un valor d'error donarà més informació sobre l'error.
	if _, err := strconv.Atoi("no-int"); err != nil { // _ descarta el valor
		// imprimeix 'strconv.ParseInt: intenta convertir "non-int":
		// syntaxi invalida'
		fmt.Println(err)
	}
	// Es tornarà a les interfícies més tard. Mentrestant,
	aprenConcurrencia()
}

// c és un canal (channel), una forma segura de comunicar objectes.
func inc(i int, c chan int) {
	c <- i + 1 // <- és l'operador "envia" quan un canal està a l'esquerra.
}

// Es pot fer servir inc per incrementar un número de forma concurrent.
func aprenConcurrencia() {
	// La funció make es pot fer servir per crear slices, mapes i canals.
	c := make(chan int)
	// S'inicien tres goroutines.
	// Els números s'incrementaran de forma concurrent, En paral·lel
	// si la màquina on s'executa pot fer-ho i està correctament configurada.
	// Tots tres envien al mateix canal.
	go inc(0, c) // go és la comanda que inicia una nova goroutine.
	go inc(10, c)
	go inc(-805, c)
	// Llegeix tres resultats del canal i els imprimeix.
	// No es pot saber en quin ordre arribaran els resultats!
	fmt.Println(<-c, <-c, <-c) // Canal a la dreta <- és l'operador "rebre"

	cs := make(chan string)       // Un altre canal que processa strings.
	ccs := make(chan chan string) // Un canal de canals string.
	go func() { c <- 84 }()       // Inicia una goroutine i li envia un valor.
	go func() { cs <- "paraula" }() // El mateix però amb cs.
	// Select té una sintaxi semblant a switch però amb canals.
	// Selecciona un cas aleatòriament dels que poden comunicar-se.
	select {
	case i := <-c: // El valor rebit pot ser assignat a una variable,
		fmt.Printf("és un %T", i)
	case <-cs: // O es pot descartar
		fmt.Println("és un string")
	case <-ccs: // Canal buit, no preparat per la comunicació.
		fmt.Println("no ha passat.")
	}
	// Quan arribi aquí s'haurà agafat un valor de c o bé de cs. Una de les
	// goroutines iniciades haurà acabat i l'altra romandrà bloquejada.

	aprenProgramacioWeb() // Go ho fa. Tu vols fer-ho.
}

// Una funció del paquet http inicia un servidor web.
func aprenProgramacioWeb() {

	// El primer paràmetre de ListenAndServe és l'adreça on escoltar
	// i el segon és una interfície http.Handler.
	go func() {
		err := http.ListenAndServe(":8080", pair{})
		fmt.Println(err) // no s'han d'ignorar els errors
	}()

	requestServer()
}

// Es converteix "parell" en un http.Handler només implementant el
// mètode ServeHTTP.
func (p parell) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Serveix dades en el http.ResponseWriter.
	w.Write([]byte("Has après Go en Y minuts!"))
}

func requestServer() {
	resp, err := http.Get("http://localhost:8080")
	fmt.Println(err)
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Printf("\nEl servidor diu: `%s`", string(body))
}
```

## Més informació

L'arrel de tot en Go és la web oficial [official Go web site]
(http://golang.org/). Allà es pot seguir el tutorial, jugar interactivament
i llegir molt més del que hem vist aquí.En el "tour",
[the docs](https://golang.org/doc/) conté informació sobre com escriure codi
net i efectiu en Go, comandes per empaquetar i generar documentació, i
història de les versions.

És altament recomanable llegir La definició del llenguatge. És fàcil de llegir
i sorprenentment curta (com la definició del llenguatge en aquests dies).

Es pot jugar amb codi a [Go playground](https://play.golang.org/p/tnWMjr16Mm).
Prova de fer canvis en el codi i executar-lo des del navegador! Es pot fer
servir [https://play.golang.org](https://play.golang.org) com a [REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) per provar coses i codi
en el navegador sense haver d'instal·lar Go.

En la llista de lectures pels estudiants de Go hi ha
[el codi font de la llibreria estàndard](http://golang.org/src/pkg/).
Ampliament comentada, que demostra el fàcil que és de llegir i entendre els
programes en Go, l'estil de programació, i les formes de treballar-hi. O es
pot clicar en un nom de funció en [la documentació](http://golang.org/pkg/)
i veure'n el codi!

Un altre gran recurs per aprendre Go és
[Go by example](https://gobyexample.com/).

Go Mobile afegeix suport per plataformes mòbils (Android i iOS). Es poden
escriure aplicacions mòbils o escriure llibreries de paquets de Go, que es
poden cridar des de Java (android) i Objective-C (iOS).
Comproveu la [Go Mobile page](https://github.com/golang/go/wiki/Mobile) per
més informació.
---
name: Groovy
category: language
language: Groovy
lang: ca-es
filename: learngroovy-ca.groovy
contributors:
    - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"]
translations:
    - ["Xavier Sala Pujolar", "http://github.com/utrescu"]
---

Groovy - Un llenguatge dinàmic per la plataforma Java [Llegir-ne més.](http://www.groovy-lang.org/)

```groovy

/*
  Posa'l en marxa tu mateix:

  1) Instal.la SDKMAN - http://sdkman.io/
  2) Instal.la Groovy: sdk install groovy
  3) Inicia la consola groovy escrivint: groovyConsole

*/

//  Els comentaris d'una sola línia comencen amb dues barres inverses
/*
Els comentaris multilínia són com aquest.
*/

// Hola món
println "Hola món!"

/*
  Variables:

  Es poden assignar valors a les variables per fer-los servir més tard
*/

def x = 1
println x

x = new java.util.Date()
println x

x = -3.1499392
println x

x = false
println x

x = "Groovy!"
println x

/*
  Col.leccions i mapes
*/

// Crear una llista buida
def technologies = []

/*** Afegir elements a la llista ***/

// Com en Java
technologies.add("Grails")

// El shift a l'esquerra afegeix i retorna la llista
technologies << "Groovy"

// Afegir múltiples elements
technologies.addAll(["Gradle","Griffon"])

/*** Eliminar elements de la llista ***/

// Com en Java
technologies.remove("Griffon")

// La resta també funciona
technologies = technologies - 'Grails'

/*** Iterar per les llistes ***/

// Iterar per tots els elements de la llista
technologies.each { println "Technology: $it"}
technologies.eachWithIndex { it, i -> println "$i: $it"}

/*** Comprovar el contingut de la llista ***/

//Comprovar si la llista conté un o més elements (resultat boolea)
contained = technologies.contains( 'Groovy' )

// O
contained = 'Groovy' in technologies

// Comprovar diversos elements
technologies.containsAll(['Groovy','Grails'])

/*** Ordenar llistes ***/

// Ordenar una llista (canvia la original)
technologies.sort()

// Per ordenar sense canviar la original es pot fer:
sortedTechnologies = technologies.sort( false )

/*** Manipular llistes ***/

//Canvia tots els elements de la llista
Collections.replaceAll(technologies, 'Gradle', 'gradle')

// Desordena la llista
Collections.shuffle(technologies, new Random())

// Buida la llista
technologies.clear()

// Crear un mapa buit
def devMap = [:]

// Afegir valors al mapa
devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
devMap.put('lastName','Perez')

// Iterar per tots els elements del mapa
devMap.each { println "$it.key: $it.value" }
devMap.eachWithIndex { it, i -> println "$i: $it"}

// Comprovar si la clau hi és
assert devMap.containsKey('name')

// Comprova si el mapa conté un valor concret
assert devMap.containsValue('Roberto')

// Obtenir les claus del mapa
println devMap.keySet()

// Obtenir els valors del mapa
println devMap.values()

/*
  Groovy Beans

  Els GroovyBeans són JavaBeans però amb una sintaxi molt més senzilla

  Quan Groovy es compila a bytecode es fan servir les regles següents.

    * Si el nom és declarat amb un modificador (public, private o protected)
      es genera el camp

    * Un nom declarat sense modificadors genera un camp privat amb un getter
      i un setter públics (per exemple una propietat)

    * Si la propietat és declarada final el camp privat es crea i no es
      genera cap setter.

    * Es pot declarar una propietat i també declarar un getter i un setter.

    * Es pot declarar una propietat i un camp amb el mateix nom, la propietat
      farà servir el camp.

    * Si es vol una propietat private o protected s'ha de definir el getter i
      el setter que s'han de declarar private o protected.

    * Si s'accedeix a una propietat de la classe que està definida en temps
      de compilació amb un this implícit o explícit (per exemple this.foo, o
      bé només foo), Groovy accedirà al camp directament en comptes de fer-ho
      a través del getter i el setter.

    * Si s'accedeix a una propietat que no existeix tant implícita com
      explicitament, llavors Groovy accedirà a la propietat a través de la
      meta classe, que pot fer que falli en temps d'execució.

*/

class Foo {
    // Propietat només de lectura
    final String name = "Roberto"

    // Propietat de només lectura amb getter públic i un setter protected
    String language
    protected void setLanguage(String language) { this.language = language }

    // Propietat amb el tipus definit dinàmicament
    def lastName
}

/*
  Bucles i estructres de control
*/

//Groovy té el format tradicional de if -else
def x = 3

if(x==1) {
    println "One"
} else if(x==2) {
    println "Two"
} else {
    println "X greater than Two"
}

// Groovy també té l'operador ternari
def y = 10
def x = (y > 1) ? "worked" : "failed"
assert x == "worked"

//I també té 'l'Operador Elvis'!
//En comptes de fer servir l'operador ternari:
displayName = user.name ? user.name : 'Anonymous'

// Es pot escriure d'aquesta forma:
displayName = user.name ?: 'Anonymous'

//Bucle for
//Itera en un rang
def x = 0
for (i in 0 .. 30) {
    x += i
}

//Itera per una llista
x = 0
for( i in [5,3,2,1] ) {
    x += i
}

//Itera per un array
array = (0..20).toArray()
x = 0
for (i in array) {
    x += i
}

//Itera per un mapa
def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
x = 0
for ( e in map ) {
    x += e.value
}

/*
  Operadors

  Hi ha una llista d'operadors que poden ser sobreescrits en Groovy:
  http://www.groovy-lang.org/operators.html#Operator-Overloading

  Operadors útils de Groovy
*/
//Spread operator:  Invoca una acció en tots els ítems d'un grup d'objectes.
def technologies = ['Groovy','Grails','Gradle']
technologies*.toUpperCase() // = a technologies.collect { it?.toUpperCase() }

//Safe navigation operator: fet servir per evitar el NullPointerException.
def user = User.get(1)
def username = user?.username


/*
  Closures
  Un Closure és com un "bloc de codi" o un punter a un mètode. És un troç de
  codi que està definit i que s podrà executar més tard.

  Més informació a: http://www.groovy-lang.org/closures.html
*/
//Exemple:
def clos = { println "Hola món!" }

println "Executant el Closure:"
clos()

// Passar paràmetres a un Closure
def sum = { a, b -> println a+b }
sum(2,4)

//Els Closures poden fer referència a variables que no formen part de la
// llista dels seus paràmetres.
def x = 5
def multiplyBy = { num -> num * x }
println multiplyBy(10)

// Si es té un Closure que agafa un element com a argument, se'n pot ignorar
// la definició
def clos = { print it }
clos( "hi" )

/*
  Groovy pot recordar els resultats dels Closures [1][2][3]
*/
def cl = {a, b ->
    sleep(3000) // simula un procés llarg
    a + b
}

mem = cl.memoize()

def callClosure(a, b) {
    def start = System.currentTimeMillis()
    mem(a, b)
    println "(a = $a, b = $b) - en ${System.currentTimeMillis() - start} ms"
}

callClosure(1, 2)
callClosure(1, 2)
callClosure(2, 3)
callClosure(2, 3)
callClosure(3, 4)
callClosure(3, 4)
callClosure(1, 2)
callClosure(2, 3)
callClosure(3, 4)

/*
  Expando

  La classe Expando és un bean dinàmic al que se li poden afegir propietats i
  closures com a mètodes d'una instància d'aquesta classe.

http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/
  def user = new Expando(name:"Roberto")
  assert 'Roberto' == user.name

  user.lastName = 'Pérez'
  assert 'Pérez' == user.lastName

  user.showInfo = { out ->
      out << "Name: $name"
      out << ", Last name: $lastName"
  }

  def sw = new StringWriter()
  println user.showInfo(sw)


/*
  Metaprogrammació (MOP)
*/

// Fent servir ExpandoMetaClass per afegir comportament
String.metaClass.testAdd = {
    println "he afegit això"
}

String x = "test"
x?.testAdd()

//Intercepting method calls
class Test implements GroovyInterceptable {
    def sum(Integer x, Integer y) { x + y }

    def invokeMethod(String name, args) {
        System.out.println "Invoca el mètode $name amb arguments: $args"
    }
}

def test = new Test()
test?.sum(2,3)
test?.multiply(2,3)

//Groovy supporta propertyMissing per gestionar la resolució de propietats
class Foo {
   def propertyMissing(String name) { name }
}
def f = new Foo()

assertEquals "boo", f.boo

/*
  TypeChecked i CompileStatic
  Groovy, by nature, és i sempre serà un llenguatge dinàmic però també té
  comprovació de tipus i definicions estàtiques

  More info: http://www.infoq.com/articles/new-groovy-20
*/
//TypeChecked
import groovy.transform.TypeChecked

void testMethod() {}

@TypeChecked
void test() {
    testMeethod()

    def name = "Roberto"

    println naameee

}

//Un altre exemple:
import groovy.transform.TypeChecked

@TypeChecked
Integer test() {
    Integer num = "1"

    Integer[] numbers = [1,2,3,4]

    Date date = numbers[1]

    return "Test"

}

//exemple de CompileStatic
import groovy.transform.CompileStatic

@CompileStatic
int sum(int x, int y) {
    x + y
}

assert sum(2,5) == 7


```

## Per aprendre'n més

[documentació de Groovy](http://www.groovy-lang.org/documentation.html)

[Cònsola de Groovy](http://groovyconsole.appspot.com/)

Uneix-te a un [grup d'usuaris Groovy]
(http://www.groovy-lang.org/usergroups.html)

## Llibres

* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook)

* [Groovy in Action] (http://manning.com/koenig2/)

* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)

[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html
---
language: kotlin
contributors:
    - ["S Webber", "https://github.com/s-webber"]
translators:
    - ["Xavier Sala", "https://github.com/utrescu"] 
lang: ca-es
filename: LearnKotlin-ca.kt
---

Kotlin és un llenguatge estàtic tipat per la JVM, Android i el navegador.
És interoperable al 100% amb Java.
[Llegir-ne més aquí.](https://kotlinlang.org/)

```kotlin
// Els comentaris d'una línia comencen amb //
/*
Els comentaris multilínia són com aquest
*/

// La paraula clau "package" funciona de la mateixa forma que en Java

package com.learnxinyminutes.kotlin

/*
El punt d'entrada dels programes en Kotlin és una funció anomenada "main".
La funció rep un array que té els arguments fets servir al executar-lo.
*/
fun main(args: Array<String>) {
    /*
    La declaració de variables es pot fer tant amb "var" com amb "val".
    A les declarades amb "val" no se'ls hi pot canviar el valor
    en canvi a les declarades amb "var" si.
    */
    val fooVal = 10 // no es podrà canviar el valor de fooVal
    var fooVar = 10
    fooVar = 20 // fooVar si que es pot canviar

    /*
    Gairebé sempre, Kotlin pot determinar el tipus d'una variable,
    de manera que no caldrà definir-lo cada vegada.
    Però es pot definir el tipus d'una variable explícitament d'aquesta forma:
    */
    val foo: Int = 7

    /*
    Els "strings" es poden representar igual que com es fa en Java.
    Es poden escapar caràcters amb la barra inversa.
    */
    val fooString = "Aquí està la meva cadena!"
    val barString = "Imprimir en dues línies?\nCap problema!"
    val bazString = "Es poden posar tabuladors?\tI tant!"
    println(fooString)
    println(barString)
    println(bazString)

    /*
    Es poden definir strings literals envoltant-los amb les triples cometes 
    (""").
    Dins hi poden haver tant salts de línies com d'altres caràcters.
    */
    val fooRawString = """
fun helloWorld(val name : String) {
   println("Hola món!")
}
"""
    println(fooRawString)

    /*
    Els strings poden contenir expressions de plantilla.
    Les expressions de plantilla comencen amb el símbol ($).
    */
    val fooTemplateString = "$fooString té ${fooString.length} caràcters"
    println(fooTemplateString)

    /*
    Perquè una variable pugui contenir null ha de ser declarada específicament
    com a nullable afengint-li ? al seu tipus.
    Es pot accedir a una variable nulable fent servir l'operador ?.
    L'operador ?: permet especificar un valor alternatiu en cas de que la
    variable sigui null.
    */
    var fooNullable: String? = "abc"
    println(fooNullable?.length) // => 3
    println(fooNullable?.length ?: -1) // => 3
    fooNullable = null
    println(fooNullable?.length) // => null
    println(fooNullable?.length ?: -1) // => -1

    /*
    Les funcions es declaren amb la paraula "fun".
    Els arguments s'especifiquen entre corxets després del nom de la funció.
    Els arguments poden tenir un valor per defecte.
    El retorn de les funcions, si cal, es posa després de l'argument.
    */
    fun hello(name: String = "món"): String {
        return "Hola, $name!"
    }
    println(hello("foo")) // => Hola, foo!
    println(hello(name = "bar")) // => Hola, bar!
    println(hello()) // => Hola, món!

    /*
    Un dels paràmetres d'una funció pot ser marcat amb la paraula clau
    "vararg" que permet que una funció accepti un número variable
    d'arguments.
    */
    fun varargExample(vararg names: Int) {
        println("S'han rebut ${names.size} arguments")
    }
    varargExample() // => S'han rebut 0 elements
    varargExample(1) // => S'ha rebut 1 element
    varargExample(1, 2, 3) // => S'han rebut 3 elements

    /*
    Quan una funció consisteix en una sola expressió no calen els corxets
    El cos de la funció es posa rere el símbol =.
    */
    fun odd(x: Int): Boolean = x % 2 == 1
    println(odd(6)) // => false
    println(odd(7)) // => true

    // Si el tipus retornat es pot determinar no cal especificar-lo.
    fun even(x: Int) = x % 2 == 0
    println(even(6)) // => true
    println(even(7)) // => false

    // Les funcions poden tenir altres funcions com arguments i
    // fins i tot retornar-ne.
    fun not(f: (Int) -> Boolean): (Int) -> Boolean {
        return {n -> !f.invoke(n)}
    }
    // Les funcions amb nom es poden especificar quan fan d'arguments amb ::
    val notOdd = not(::odd)
    val notEven = not(::even)
    // Les expressions lambda es poden posar com arguments.
    val notZero = not {n -> n == 0}
    /*
    Si la lambda només té un paràmetre es pot ometre la seva declaració.
    El seu valor serà "it".
    */
    val notPositive = not {it > 0}
    for (i in 0..4) {
        println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}")
    }

    // Les classes es defineixen amb "class".
    class ExampleClass(val x: Int) {
        fun memberFunction(y: Int): Int {
            return x + y
        }

        infix fun infixMemberFunction(y: Int): Int {
            return x * y
        }
    }
    /*
    Per crear una nova instància es crida al constructor.
    Tingueu en compte que Kotlin no té l'operador "new".
    */
    val fooExampleClass = ExampleClass(7)
    // Els mètodes es poden cridar amb la notació .
    println(fooExampleClass.memberFunction(4)) // => 11
    /*
    Si una funció ha estat marcada amb "infix" es pot cridar amb la
    notació infix.
    */
    println(fooExampleClass infixMemberFunction 4) // => 28

    /*
    Les classes "data" són classes que només contenen dades.
    Es creen automàticament els mètodes "hashCode","equals" i "toString"
    */
    data class DataClassExample (val x: Int, val y: Int, val z: Int)
    val fooData = DataClassExample(1, 2, 4)
    println(fooData) // => DataClassExample(x=1, y=2, z=4)

    // Les classes data tenen un mètode "copy".
    val fooCopy = fooData.copy(y = 100)
    println(fooCopy) // => DataClassExample(x=1, y=100, z=4)

    // Els objectes es poden desestructurar amb múltiples variables
    val (a, b, c) = fooCopy
    println("$a $b $c") // => 1 100 4

    // desestructurat en un bucle "for"
    for ((a, b, c) in listOf(fooData)) {
        println("$a $b $c") // => 1 100 4
    }

    val mapData = mapOf("a" to 1, "b" to 2)
    // Els mapes també
    for ((key, value) in mapData) {
        println("$key -> $value")
    }

    // La funció "with" és similar a la de JavaScript.
    data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
    val fooMutableData = MutableDataClassExample(7, 4, 9)
    with (fooMutableData) {
        x -= 2
        y += 2
        z--
    }
    println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8)

    /*
    Es pot crear una llista amb la funció "listOf".
    La llista serà immutable - no s'hi poden afegir o treure elements.
    */
    val fooList = listOf("a", "b", "c")
    println(fooList.size) // => 3
    println(fooList.first()) // => a
    println(fooList.last()) // => c
    // Es pot accedir als elements a partir del seu índex.
    println(fooList[1]) // => b

    // Es poden crear llistes mutables amb la funció "mutableListOf".
    val fooMutableList = mutableListOf("a", "b", "c")
    fooMutableList.add("d")
    println(fooMutableList.last()) // => d
    println(fooMutableList.size) // => 4

    // Es poden crear conjunts amb la funció "setOf".
    val fooSet = setOf("a", "b", "c")
    println(fooSet.contains("a")) // => true
    println(fooSet.contains("z")) // => false

    // Es poden crear mapes amb la funció "mapOf".
    val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9)
    // S'accedeix als valors del mapa a partir del seu índex.
    println(fooMap["a"]) // => 8

    /*
    Les sequències representen col·leccions evaluades quan fan falta.
    Podem crear una seqüencia amb la funció "generateSequence".
    */
    val fooSequence = generateSequence(1, { it + 1 })
    val x = fooSequence.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // Per exemple amb aquesta seqüència es creen els números de Fibonacci:
    fun fibonacciSequence(): Sequence<Long> {
        var a = 0L
        var b = 1L

        fun next(): Long {
            val result = a + b
            a = b
            b = result
            return a
        }

        return generateSequence(::next)
    }
    val y = fibonacciSequence().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

    // Kotlin proporciona funcions de primer ordre per treballar amb
    // col·leccions.
    val z = (1..9).map {it * 3}
                  .filter {it < 20}
                  .groupBy {it % 2 == 0}
                  .mapKeys {if (it.key) "parell" else "senar"}
    println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]}

    // Es pot fer servir el bucle "for" amb qualsevol cosa que proporcioni
    // un iterador.
    for (c in "hello") {
        println(c)
    }

    // els bucles "while" funcionen com en altres llenguatges.
    var ctr = 0
    while (ctr < 5) {
        println(ctr)
        ctr++
    }
    do {
        println(ctr)
        ctr++
    } while (ctr < 10)

    /*
    "if" es pot fer servir com una expressió que retorna un valor.
    Per això no cal l'operador ternari ?: en Kotlin.
    */
    val num = 5
    val message = if (num % 2 == 0) "parell" else "senar"
    println("$num is $message") // => 5 is odd

    // "when" es pot fer servir com alternativa a les cadenes "if-else if".
    val i = 10
    when {
        i < 7 -> println("primer bloc")
        fooString.startsWith("hola") -> println("segon bloc")
        else -> println("bloc else")
    }

    // "when" es pot fer servir amb un argument.
    when (i) {
        0, 21 -> println("0 o 21")
        in 1..20 -> println("en el rang 1 a 20")
        else -> println("cap dels anteriors")
    }

    // "when" es pot fer servir com una funció que retorna valors.
    var result = when (i) {
        0, 21 -> "0 o 21"
        in 1..20 -> "en el rang 1 a 20"
        else -> "cap dels anteriors"
    }
    println(result)

    /*
    Es pot comprovar el tipus d'un objecte fent servir l'operador "is".
    Si un objecte passa una comprovació es pot fer servir sense posar-hi
    cap casting.
    */
    fun smartCastExample(x: Any) : Boolean {
        if (x is Boolean) {
            // x es converteix automàticament a Booleà
            return x
        } else if (x is Int) {
            // x es converteix automàticament a int
            return x > 0
        } else if (x is String) {
            // x es converteix a string automàticament
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(smartCastExample("Hola món!")) // => true
    println(smartCastExample("")) // => false
    println(smartCastExample(5)) // => true
    println(smartCastExample(0)) // => false
    println(smartCastExample(true)) // => true

    // També es pot cridar smarcast en un bloc when
    fun smartCastWhenExample(x: Any) = when (x) {
        is Boolean -> x
        is Int -> x > 0
        is String -> x.isNotEmpty()
        else -> false
    }

    /*
    Les extensions són una forma d'afegir noves funcionalitats a una classe.
    És semblant a les extensions de C#.
    */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("Hola món!".remove('l')) // => Hoa, món!

    println(EnumExample.A) // => A
    println(ObjectExample.hello()) // => hola
}

// Les classes enumerades són semblants a les de Java
enum class EnumExample {
    A, B, C
}

/*
El paràmetre "object" es pot fer servir per crear objectes singleton.
No es poden instanciar però es pot fer referència a la seva única instància
amb el seu nom.
Són similars als singletons d'Scala.
*/
object ObjectExample {
    fun hello(): String {
        return "hola"
    }
}

fun useObject() {
    ObjectExample.hello()
    val someRef: Any = ObjectExample // podem fer servir el nom de l'objecte
}

```

### Per llegir més

* [tutorials de Kotlin](https://kotlinlang.org/docs/tutorials/)
* [Provar Kotlin en el navegador](http://try.kotlinlang.org/)
* [Llista de recursos de Kotlin](http://kotlin.link/)
---
language: chapel
filename: learnchapel.chpl
contributors:
    - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"]
    - ["Ben Harshbarger", "http://github.com/benharsh/"]
---

You can read all about Chapel at [Cray's official Chapel website](http://chapel.cray.com).
In short, Chapel is an open-source, high-productivity, parallel-programming
language in development at Cray Inc., and is designed to run on multi-core PCs
as well as multi-kilocore supercomputers.

More information and support can be found at the bottom of this document.

```chapel
// Comments are C-family style

// one line comment
/*
 multi-line comment
*/

// Basic printing

write("Hello, ");
writeln("World!");

// write and writeln can take a list of things to print.
// Each thing is printed right next to the others, so include your spacing!
writeln("There are ", 3, " commas (\",\") in this line of code");

// Different output channels:
stdout.writeln("This goes to standard output, just like plain writeln() does");
stderr.writeln("This goes to standard error");


// Variables don't have to be explicitly typed as long as
// the compiler can figure out the type that it will hold.
// 10 is an int, so myVar is implicitly an int
var myVar = 10;
myVar = -10;
var mySecondVar = myVar;
// var anError; would be a compile-time error.

// We can (and should) explicitly type things.
var myThirdVar: real;
var myFourthVar: real = -1.234;
myThirdVar = myFourthVar;

// Types

// There are a number of basic types.
var myInt: int = -1000; // Signed ints
var myUint: uint = 1234; // Unsigned ints
var myReal: real = 9.876; // Floating point numbers
var myImag: imag = 5.0i; // Imaginary numbers
var myCplx: complex = 10 + 9i; // Complex numbers
myCplx = myInt + myImag; // Another way to form complex numbers
var myBool: bool = false; // Booleans
var myStr: string = "Some string..."; // Strings
var singleQuoteStr = 'Another string...'; // String literal with single quotes

// Some types can have sizes.
var my8Int: int(8) = 10; // 8 bit (one byte) sized int;
var my64Real: real(64) = 1.516; // 64 bit (8 bytes) sized real

// Typecasting.
var intFromReal = myReal : int;
var intFromReal2: int = myReal : int;

// Type aliasing.
type chroma = int;        // Type of a single hue
type RGBColor = 3*chroma; // Type representing a full color
var black: RGBColor = (0,0,0);
var white: RGBColor = (255, 255, 255);

// Constants and Parameters

// A const is a constant, and cannot be changed after set in runtime.
const almostPi: real = 22.0/7.0;

// A param is a constant whose value must be known statically at
// compile-time.
param compileTimeConst: int = 16;

// The config modifier allows values to be set at the command line.
// Set with --varCmdLineArg=Value or --varCmdLineArg Value at runtime.
config var varCmdLineArg: int = -123;
config const constCmdLineArg: int = 777;

// config param can be set at compile-time.
// Set with --set paramCmdLineArg=value at compile-time.
config param paramCmdLineArg: bool = false;
writeln(varCmdLineArg, ", ", constCmdLineArg, ", ", paramCmdLineArg);

// References

// ref operates much like a reference in C++. In Chapel, a ref cannot
// be made to alias a variable other than the variable it is initialized with.
// Here, refToActual refers to actual.
var actual = 10;
ref refToActual = actual; 
writeln(actual, " == ", refToActual); // prints the same value
actual = -123; // modify actual (which refToActual refers to)
writeln(actual, " == ", refToActual); // prints the same value
refToActual = 99999999; // modify what refToActual refers to (which is actual)
writeln(actual, " == ", refToActual); // prints the same value

// Operators

// Math operators:
var a: int, thisInt = 1234, thatInt = 5678;
a = thisInt + thatInt;  // Addition
a = thisInt * thatInt;  // Multiplication
a = thisInt - thatInt;  // Subtraction
a = thisInt / thatInt;  // Division
a = thisInt ** thatInt; // Exponentiation
a = thisInt % thatInt;  // Remainder (modulo)

// Logical operators:
var b: bool, thisBool = false, thatBool = true;
b = thisBool && thatBool; // Logical and
b = thisBool || thatBool; // Logical or
b = !thisBool;            // Logical negation

// Relational operators:
b = thisInt > thatInt;           // Greater-than
b = thisInt >= thatInt;          // Greater-than-or-equal-to
b = thisInt < a && a <= thatInt; // Less-than, and, less-than-or-equal-to
b = thisInt != thatInt;          // Not-equal-to
b = thisInt == thatInt;          // Equal-to

// Bitwise operators:
a = thisInt << 10;     // Left-bit-shift by 10 bits;
a = thatInt >> 5;      // Right-bit-shift by 5 bits;
a = ~thisInt;          // Bitwise-negation
a = thisInt ^ thatInt; // Bitwise exclusive-or

// Compound assignment operators:
a += thisInt;          // Addition-equals (a = a + thisInt;)
a *= thatInt;          // Times-equals (a = a * thatInt;)
b &&= thatBool;        // Logical-and-equals (b = b && thatBool;)
a <<= 3;               // Left-bit-shift-equals (a = a << 10;)

// Unlike other C family languages, there are no
// pre/post-increment/decrement operators, such as:
//
// ++j, --j, j++, j--

// Swap operator:
var old_this = thisInt;
var old_that = thatInt;
thisInt <=> thatInt; // Swap the values of thisInt and thatInt
writeln((old_this == thatInt) && (old_that == thisInt));

// Operator overloads can also be defined, as we'll see with procedures.

// Tuples

// Tuples can be of the same type or different types.
var sameTup: 2*int = (10, -1);
var sameTup2 = (11, -6);
var diffTup: (int,real,complex) = (5, 1.928, myCplx);
var diffTupe2 = (7, 5.64, 6.0+1.5i);

// Tuples can be accessed using square brackets or parentheses, and are
// 1-indexed.
writeln("(", sameTup[1], ",", sameTup(2), ")");
writeln(diffTup);

// Tuples can also be written into.
diffTup(1) = -1;

// Tuple values can be expanded into their own variables.
var (tupInt, tupReal, tupCplx) = diffTup;
writeln(diffTup == (tupInt, tupReal, tupCplx));

// They are also useful for writing a list of variables, as is common in debugging.
writeln((a,b,thisInt,thatInt,thisBool,thatBool));

// Control Flow

// if - then - else works just like any other C-family language.
if 10 < 100 then
  writeln("All is well");

if -1 < 1 then
  writeln("Continuing to believe reality");
else
  writeln("Send mathematician, something's wrong");

// You can use parentheses if you prefer.
if (10 > 100) {
  writeln("Universe broken. Please reboot universe.");
}

if a % 2 == 0 {
  writeln(a, " is even.");
} else {
  writeln(a, " is odd.");
}

if a % 3 == 0 {
  writeln(a, " is even divisible by 3.");
} else if a % 3 == 1 {
  writeln(a, " is divided by 3 with a remainder of 1.");
} else {
  writeln(b, " is divided by 3 with a remainder of 2.");
}

// Ternary: if - then - else in a statement.
var maximum = if thisInt < thatInt then thatInt else thisInt;

// select statements are much like switch statements in other languages.
// However, select statements don't cascade like in C or Java.
var inputOption = "anOption";
select inputOption {
  when "anOption" do writeln("Chose 'anOption'");
  when "otherOption" {
    writeln("Chose 'otherOption'");
    writeln("Which has a body");
  }
  otherwise {
    writeln("Any other Input");
    writeln("the otherwise case doesn't need a do if the body is one line");
  }
}

// while and do-while loops also behave like their C counterparts.
var j: int = 1;
var jSum: int = 0;
while (j <= 1000) {
  jSum += j;
  j += 1;
}
writeln(jSum);

do {
  jSum += j;
  j += 1;
} while (j <= 10000);
writeln(jSum);

// for loops are much like those in Python in that they iterate over a
// range. Ranges (like the 1..10 expression below) are a first-class object
// in Chapel, and as such can be stored in variables.
for i in 1..10 do write(i, ", ");
writeln();

var iSum: int = 0;
for i in 1..1000 {
  iSum += i;
}
writeln(iSum);

for x in 1..10 {
  for y in 1..10 {
    write((x,y), "\t");
  }
  writeln();
}

// Ranges and Domains

// For-loops and arrays both use ranges and domains to define an index set that
// can be iterated over. Ranges are single dimensional integer indices, while
// domains can be multi-dimensional and represent indices of different types.

// They are first-class citizen types, and can be assigned into variables.
var range1to10: range = 1..10;  // 1, 2, 3, ..., 10
var range2to11 = 2..11; // 2, 3, 4, ..., 11
var rangeThisToThat: range = thisInt..thatInt; // using variables
var rangeEmpty: range = 100..-100; // this is valid but contains no indices

// Ranges can be unbounded.
var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; // 1, 2, 3, 4, 5, ...
var rangeNegInfTo1 = ..1; // ..., -4, -3, -2, -1, 0, 1

// Ranges can be strided (and reversed) using the by operator.
var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10
var reverse2to10by2 = 2..10 by -2; // 10, 8, 6, 4, 2

var trapRange = 10..1 by -1; // Do not be fooled, this is still an empty range
writeln("Size of range '", trapRange, "' = ", trapRange.length);

// Note: range(boundedType= ...) and range(stridable= ...) are only
// necessary if we explicitly type the variable.

// The end point of a range can be determined using the count (#) operator.
var rangeCount: range = -5..#12; // range from -5 to 6

// Operators can be mixed.
var rangeCountBy: range(stridable=true) = -5..#12 by 2; // -5, -3, -1, 1, 3, 5
writeln(rangeCountBy);

// Properties of the range can be queried.
// In this example, printing the first index, last index, number of indices,
// stride, and if 2 is include in the range.
writeln((rangeCountBy.first, rangeCountBy.last, rangeCountBy.length,
           rangeCountBy.stride, rangeCountBy.member(2)));

for i in rangeCountBy {
  write(i, if i == rangeCountBy.last then "\n" else ", ");
}

// Rectangular domains are defined using the same range syntax,
// but they are required to be bounded (unlike ranges).
var domain1to10: domain(1) = {1..10};        // 1D domain from 1..10;
var twoDimensions: domain(2) = {-2..2,0..2}; // 2D domain over product of ranges
var thirdDim: range = 1..16;
var threeDims: domain(3) = {thirdDim, 1..10, 5..10}; // using a range variable

// Domains can also be resized
var resizedDom = {1..10};
writeln("before, resizedDom = ", resizedDom);
resizedDom = {-10..#10};
writeln("after, resizedDom = ", resizedDom);

// Indices can be iterated over as tuples.
for idx in twoDimensions do
  write(idx, ", ");
writeln();

// These tuples can also be deconstructed.
for (x,y) in twoDimensions {
  write("(", x, ", ", y, ")", ", ");
}
writeln();

// Associative domains act like sets.
var stringSet: domain(string); // empty set of strings
stringSet += "a";
stringSet += "b";
stringSet += "c";
stringSet += "a"; // Redundant add "a"
stringSet -= "c"; // Remove "c"
writeln(stringSet.sorted());

// Associative domains can also have a literal syntax
var intSet = {1, 2, 4, 5, 100};

// Both ranges and domains can be sliced to produce a range or domain with the
// intersection of indices.
var rangeA = 1.. ; // range from 1 to infinity
var rangeB =  ..5; // range from negative infinity to 5
var rangeC = rangeA[rangeB]; // resulting range is 1..5
writeln((rangeA, rangeB, rangeC));

var domainA = {1..10, 5..20};
var domainB = {-5..5, 1..10};
var domainC = domainA[domainB];
writeln((domainA, domainB, domainC));

// Arrays

// Arrays are similar to those of other languages.
// Their sizes are defined using domains that represent their indices.
var intArray: [1..10] int;
var intArray2: [{1..10}] int; // equivalent

// They can be accessed using either brackets or parentheses
for i in 1..10 do
  intArray[i] = -i;
writeln(intArray);

// We cannot access intArray[0] because it exists outside
// of the index set, {1..10}, we defined it to have.
// intArray[11] is illegal for the same reason.
var realDomain: domain(2) = {1..5,1..7};
var realArray: [realDomain] real;
var realArray2: [1..5,1..7] real;   // equivalent
var realArray3: [{1..5,1..7}] real; // equivalent

for i in 1..5 {
  for j in realDomain.dim(2) {   // Only use the 2nd dimension of the domain
    realArray[i,j] = -1.61803 * i + 0.5 * j;  // Access using index list
    var idx: 2*int = (i,j);                   // Note: 'index' is a keyword
    realArray[idx] = - realArray[(i,j)];      // Index using tuples
  }
}

// Arrays have domains as members, and can be iterated over as normal.
for idx in realArray.domain {  // Again, idx is a 2*int tuple
  realArray[idx] = 1 / realArray[idx[1], idx[2]]; // Access by tuple and list
}

writeln(realArray);

// The values of an array can also be iterated directly.
var rSum: real = 0;
for value in realArray {
  rSum += value; // Read a value
  value = rSum;  // Write a value
}
writeln(rSum, "\n", realArray);

// Associative arrays (dictionaries) can be created using associative domains.
var dictDomain: domain(string) = { "one", "two" };
var dict: [dictDomain] int = ["one" => 1, "two" => 2];
dict["three"] = 3; // Adds 'three' to 'dictDomain' implicitly
for key in dictDomain.sorted() do
  writeln(dict[key]);

// Arrays can be assigned to each other in a few different ways.
// These arrays will be used in the example.
var thisArray : [0..5] int = [0,1,2,3,4,5];
var thatArray : [0..5] int;

// First, simply assign one to the other. This copies thisArray into
// thatArray, instead of just creating a reference. Therefore, modifying
// thisArray does not also modify thatArray.

thatArray = thisArray;
thatArray[1] = -1;
writeln((thisArray, thatArray));

// Assign a slice from one array to a slice (of the same size) in the other.
thatArray[4..5] = thisArray[1..2];
writeln((thisArray, thatArray));

// Operations can also be promoted to work on arrays. 'thisPlusThat' is also
// an array.
var thisPlusThat = thisArray + thatArray;
writeln(thisPlusThat);

// Moving on, arrays and loops can also be expressions, where the loop
// body's expression is the result of each iteration.
var arrayFromLoop = for i in 1..10 do i;
writeln(arrayFromLoop);

// An expression can result in nothing, such as when filtering with an if-expression.
var evensOrFives = for i in 1..10 do if (i % 2 == 0 || i % 5 == 0) then i;

writeln(arrayFromLoop);

// Array expressions can also be written with a bracket notation.
// Note: this syntax uses the forall parallel concept discussed later.
var evensOrFivesAgain = [i in 1..10] if (i % 2 == 0 || i % 5 == 0) then i;

// They can also be written over the values of the array.
arrayFromLoop = [value in arrayFromLoop] value + 1;


// Procedures

// Chapel procedures have similar syntax functions in other languages. 
proc fibonacci(n : int) : int {
  if n <= 1 then return n;
  return fibonacci(n-1) + fibonacci(n-2);
}

// Input parameters can be untyped to create a generic procedure.
proc doublePrint(thing): void {
  write(thing, " ", thing, "\n");
}

// The return type can be inferred, as long as the compiler can figure it out.
proc addThree(n) {
  return n + 3;
}

doublePrint(addThree(fibonacci(20)));

// It is also possible to take a variable number of parameters.
proc maxOf(x ...?k) {
  // x refers to a tuple of one type, with k elements
  var maximum = x[1];
  for i in 2..k do maximum = if maximum < x[i] then x[i] else maximum;
  return maximum;
}
writeln(maxOf(1, -10, 189, -9071982, 5, 17, 20001, 42));

// Procedures can have default parameter values, and
// the parameters can be named in the call, even out of order.
proc defaultsProc(x: int, y: real = 1.2634): (int,real) {
  return (x,y);
}

writeln(defaultsProc(10));
writeln(defaultsProc(x=11));
writeln(defaultsProc(x=12, y=5.432));
writeln(defaultsProc(y=9.876, x=13));

// The ? operator is called the query operator, and is used to take
// undetermined values like tuple or array sizes and generic types.
// For example, taking arrays as parameters. The query operator is used to
// determine the domain of A. This is uesful for defining the return type,
// though it's not required.
proc invertArray(A: [?D] int): [D] int{
  for a in A do a = -a;
  return A;
}

writeln(invertArray(intArray));

// We can query the type of arguments to generic procedures.
// Here we define a procedure that takes two arguments of
// the same type, yet we don't define what that type is.
proc genericProc(arg1 : ?valueType, arg2 : valueType): void {
  select(valueType) {
    when int do writeln(arg1, " and ", arg2, " are ints");
    when real do writeln(arg1, " and ", arg2, " are reals");
    otherwise writeln(arg1, " and ", arg2, " are somethings!");
  }
}

genericProc(1, 2);
genericProc(1.2, 2.3);
genericProc(1.0+2.0i, 3.0+4.0i);

// We can also enforce a form of polymorphism with the where clause
// This allows the compiler to decide which function to use.
// Note: That means that all information needs to be known at compile-time.
// The param modifier on the arg is used to enforce this constraint.
proc whereProc(param N : int): void
 where (N > 0) {
  writeln("N is greater than 0");
}

proc whereProc(param N : int): void
 where (N < 0) {
  writeln("N is less than 0");
}

whereProc(10);
whereProc(-1);

// whereProc(0) would result in a compiler error because there
// are no functions that satisfy the where clause's condition.
// We could have defined a whereProc without a where clause
// that would then have served as a catch all for all the other cases
// (of which there is only one).

// where clauses can also be used to constrain based on argument type.
proc whereType(x: ?t) where t == int {
  writeln("Inside 'int' version of 'whereType': ", x);
}

proc whereType(x: ?t) {
  writeln("Inside general version of 'whereType': ", x);
}

whereType(42);
whereType("hello");

// Intents

/* Intent modifiers on the arguments convey how those arguments are passed to the procedure.

     * in: copy arg in, but not out
     * out: copy arg out, but not in
     * inout: copy arg in, copy arg out
     * ref: pass arg by reference
*/
proc intentsProc(in inarg, out outarg, inout inoutarg, ref refarg) {
  writeln("Inside Before: ", (inarg, outarg, inoutarg, refarg));
  inarg = inarg + 100;
  outarg = outarg + 100;
  inoutarg = inoutarg + 100;
  refarg = refarg + 100;
  writeln("Inside After: ", (inarg, outarg, inoutarg, refarg));
}

var inVar: int = 1;
var outVar: int = 2;
var inoutVar: int = 3;
var refVar: int = 4;
writeln("Outside Before: ", (inVar, outVar, inoutVar, refVar));
intentsProc(inVar, outVar, inoutVar, refVar);
writeln("Outside After: ", (inVar, outVar, inoutVar, refVar));

// Similarly, we can define intents on the return type.
// refElement returns a reference to an element of array.
// This makes more practical sense for class methods where references to
// elements in a data-structure are returned via a method or iterator.
proc refElement(array : [?D] ?T, idx) ref : T {
  return array[idx];
}

var myChangingArray : [1..5] int = [1,2,3,4,5];
writeln(myChangingArray);
ref refToElem = refElement(myChangingArray, 5); // store reference to element in ref variable
writeln(refToElem);
refToElem = -2; // modify reference which modifies actual value in array
writeln(refToElem);
writeln(myChangingArray);

// Operator Definitions

// Chapel allows for operators to be overloaded.
// We can define the unary operators:
// + - ! ~
// and the binary operators:
// + - * / % ** == <= >= < > << >> & | ˆ by
// += -= *= /= %= **= &= |= ˆ= <<= >>= <=>

// Boolean exclusive or operator.
proc ^(left : bool, right : bool): bool {
  return (left || right) && !(left && right);
}

writeln(true  ^ true);
writeln(false ^ true);
writeln(true  ^ false);
writeln(false ^ false);

// Define a * operator on any two types that returns a tuple of those types.
proc *(left : ?ltype, right : ?rtype): (ltype, rtype) {
  writeln("\tIn our '*' overload!");
  return (left, right);
}

writeln(1 * "a"); // Uses our * operator.
writeln(1 * 2);   // Uses the default * operator.

//  Note: You could break everything if you get careless with your overloads.
//  This here will break everything. Don't do it.

/*
    proc +(left: int, right: int): int {
      return left - right;
    }
*/

// Iterators

// Iterators are sisters to the procedure, and almost everything about
// procedures also applies to iterators. However, instead of returning a single
// value, iterators may yield multiple values to a loop.
//
// This is useful when a complicated set or order of iterations is needed, as
// it allows the code defining the iterations to be separate from the loop
// body.
iter oddsThenEvens(N: int): int {
  for i in 1..N by 2 do
    yield i; // yield values instead of returning.
  for i in 2..N by 2 do
    yield i;
}

for i in oddsThenEvens(10) do write(i, ", ");
writeln();

// Iterators can also yield conditionally, the result of which can be nothing
iter absolutelyNothing(N): int {
  for i in 1..N {
    if N < i { // Always false
      yield i;     // Yield statement never happens
    }
  }
}

for i in absolutelyNothing(10) {
  writeln("Woa there! absolutelyNothing yielded ", i);
}

// We can zipper together two or more iterators (who have the same number
// of iterations) using zip() to create a single zipped iterator, where each
// iteration of the zipped iterator yields a tuple of one value yielded
// from each iterator.
for (positive, negative) in zip(1..5, -5..-1) do
  writeln((positive, negative));

// Zipper iteration is quite important in the assignment of arrays,
// slices of arrays, and array/loop expressions.
var fromThatArray : [1..#5] int = [1,2,3,4,5];
var toThisArray : [100..#5] int;

// Some zipper operations implement other operations.
// The first statement and the loop are equivalent.
toThisArray = fromThatArray;
for (i,j) in zip(toThisArray.domain, fromThatArray.domain) {
  toThisArray[i] = fromThatArray[j];
}

// These two chunks are also equivalent.
toThisArray = [j in -100..#5] j;
writeln(toThisArray);

for (i, j) in zip(toThisArray.domain, -100..#5) {
  toThisArray[i] = j;
}
writeln(toThisArray);

// This is very important in understanding why this statement exhibits a
// runtime error.

/*
  var iterArray : [1..10] int = [i in 1..10] if (i % 2 == 1) then i;
*/

// Even though the domain of the array and the loop-expression are
// the same size, the body of the expression can be thought of as an iterator.
// Because iterators can yield nothing, that iterator yields a different number
// of things than the domain of the array or loop, which is not allowed.

// Classes

// Classes are similar to those in C++ and Java, allocated on the heap.
class MyClass {

// Member variables
  var memberInt : int;
  var memberBool : bool = true;

// Explicitly defined initializer.
// We also get the compiler-generated initializer, with one argument per field.
// Note that soon there will be no compiler-generated initializer when we
// define any initializer(s) explicitly.
  proc MyClass(val : real) {
    this.memberInt = ceil(val): int;
  }

// Explicitly defined deinitializer.
// If we did not write one, we would get the compiler-generated deinitializer,
// which has an empty body.
  proc deinit() {
    writeln("MyClass deinitializer called ", (this.memberInt, this.memberBool));
  }

// Class methods.
  proc setMemberInt(val: int) {
    this.memberInt = val;
  }

  proc setMemberBool(val: bool) {
    this.memberBool = val;
  }

  proc getMemberInt(): int{
    return this.memberInt;
  }

  proc getMemberBool(): bool {
    return this.memberBool;
  }
} // end MyClass

// Call compiler-generated initializer, using default value for memberBool.
var myObject = new MyClass(10);
    myObject = new MyClass(memberInt = 10); // Equivalent
writeln(myObject.getMemberInt());

// Same, but provide a memberBool value explicitly.
var myDiffObject = new MyClass(-1, true);
    myDiffObject = new MyClass(memberInt = -1,
                                memberBool = true); // Equivalent
writeln(myDiffObject);

// Call the initializer we wrote.
var myOtherObject = new MyClass(1.95);
    myOtherObject = new MyClass(val = 1.95); // Equivalent
writeln(myOtherObject.getMemberInt());

// We can define an operator on our class as well, but
// the definition has to be outside the class definition.
proc +(A : MyClass, B : MyClass) : MyClass {
  return new MyClass(memberInt = A.getMemberInt() + B.getMemberInt(),
                      memberBool = A.getMemberBool() || B.getMemberBool());
}

var plusObject = myObject + myDiffObject;
writeln(plusObject);

// Destruction.
delete myObject;
delete myDiffObject;
delete myOtherObject;
delete plusObject;

// Classes can inherit from one or more parent classes
class MyChildClass : MyClass {
  var memberComplex: complex;
}

// Here's an example of generic classes.
class GenericClass {
  type classType;
  var classDomain: domain(1);
  var classArray: [classDomain] classType;

// Explicit constructor.
  proc GenericClass(type classType, elements : int) {
    this.classDomain = {1..#elements};
  }

// Copy constructor.
// Note: We still have to put the type as an argument, but we can
// default to the type of the other object using the query (?) operator.
// Further, we can take advantage of this to allow our copy constructor
// to copy classes of different types and cast on the fly.
  proc GenericClass(other : GenericClass(?otherType),
                     type classType = otherType) {
    this.classDomain = other.classDomain;
    // Copy and cast
    for idx in this.classDomain do this[idx] = other[idx] : classType;
  }

// Define bracket notation on a GenericClass
// object so it can behave like a normal array
// i.e. objVar[i] or objVar(i)
  proc this(i : int) ref : classType {
    return this.classArray[i];
  }

// Define an implicit iterator for the class
// to yield values from the array to a loop
// i.e. for i in objVar do ...
  iter these() ref : classType {
    for i in this.classDomain do
      yield this[i];
  }
} // end GenericClass

// We can assign to the member array of the object using the bracket
// notation that we defined.
var realList = new GenericClass(real, 10);
for i in realList.classDomain do realList[i] = i + 1.0;

// We can iterate over the values in our list with the iterator
// we defined.
for value in realList do write(value, ", ");
writeln();

// Make a copy of realList using the copy constructor.
var copyList = new GenericClass(realList);
for value in copyList do write(value, ", ");
writeln();

// Make a copy of realList and change the type, also using the copy constructor.
var copyNewTypeList = new GenericClass(realList, int);
for value in copyNewTypeList do write(value, ", ");
writeln();


// Modules

// Modules are Chapel's way of managing name spaces.
// The files containing these modules do not need to be named after the modules
// (as in Java), but files implicitly name modules.
// For example, this file implicitly names the learnChapelInYMinutes module

module OurModule {

// We can use modules inside of other modules.
// Time is one of the standard modules.
  use Time;

// We'll use this procedure in the parallelism section.
  proc countdown(seconds: int) {
    for i in 1..seconds by -1 {
      writeln(i);
      sleep(1);
    }
  }

// It is possible to create arbitrarily deep module nests.
// i.e. submodules of OurModule
  module ChildModule {
    proc foo() {
      writeln("ChildModule.foo()");
    }
  }

  module SiblingModule {
    proc foo() {
      writeln("SiblingModule.foo()");
    }
  }
} // end OurModule

// Using OurModule also uses all the modules it uses.
// Since OurModule uses Time, we also use Time.
use OurModule;

// At this point we have not used ChildModule or SiblingModule so
// their symbols (i.e. foo) are not available to us. However, the module
// names are available, and we can explicitly call foo() through them.
SiblingModule.foo();
OurModule.ChildModule.foo();

// Now we use ChildModule, enabling unqualified calls.
use ChildModule;
foo();

// Parallelism

// In other languages, parallelism is typically done with
// complicated libraries and strange class structure hierarchies.
// Chapel has it baked right into the language.

// We can declare a main procedure, but all the code above main still gets
// executed.
proc main() {
  writeln("PARALLELISM START");

// A begin statement will spin the body of that statement off
// into one new task.
// A sync statement will ensure that the progress of the main
// task will not progress until the children have synced back up.

  sync {
    begin { // Start of new task's body
      var a = 0;
      for i in 1..1000 do a += 1;
      writeln("Done: ", a);
    } // End of new tasks body
    writeln("spun off a task!");
  }
  writeln("Back together");

  proc printFibb(n: int) {
    writeln("fibonacci(",n,") = ", fibonacci(n));
  }

// A cobegin statement will spin each statement of the body into one new
// task. Notice here that the prints from each statement may happen in any
// order.
  cobegin {
    printFibb(20); // new task
    printFibb(10); // new task
    printFibb(5);  // new task
    {
      // This is a nested statement body and thus is a single statement
      // to the parent statement, executed by a single task.
      writeln("this gets");
      writeln("executed as");
      writeln("a whole");
    }
  }

// A coforall loop will create a new task for EACH iteration.
// Again we see that prints happen in any order.
// NOTE: coforall should be used only for creating tasks!
// Using it to iterating over a structure is very a bad idea!
  var num_tasks = 10; // Number of tasks we want
  coforall taskID in 1..#num_tasks {
    writeln("Hello from task# ", taskID);
  }

// forall loops are another parallel loop, but only create a smaller number
// of tasks, specifically --dataParTasksPerLocale= number of tasks.
  forall i in 1..100 {
    write(i, ", ");
  }
  writeln();

// Here we see that there are sections that are in order, followed by
// a section that would not follow (e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6,).
// This is because each task is taking on a chunk of the range 1..10
// (1..3, 4..6, or 7..9) doing that chunk serially, but each task happens
// in parallel. Your results may depend on your machine and configuration

// For both the forall and coforall loops, the execution of the
// parent task will not continue until all the children sync up.

// forall loops are particularly useful for parallel iteration over arrays.
// Lets run an experiment to see how much faster a parallel loop is
  use Time; // Import the Time module to use Timer objects
  var timer: Timer;
  var myBigArray: [{1..4000,1..4000}] real; // Large array we will write into

// Serial Experiment:
  timer.start(); // Start timer
  for (x,y) in myBigArray.domain { // Serial iteration
    myBigArray[x,y] = (x:real) / (y:real);
  }
  timer.stop(); // Stop timer
  writeln("Serial: ", timer.elapsed()); // Print elapsed time
  timer.clear(); // Clear timer for parallel loop

// Parallel Experiment:
  timer.start(); // start timer
  forall (x,y) in myBigArray.domain { // Parallel iteration
    myBigArray[x,y] = (x:real) / (y:real);
  }
  timer.stop(); // Stop timer
  writeln("Parallel: ", timer.elapsed()); // Print elapsed time
  timer.clear();

// You may have noticed that (depending on how many cores you have)
// the parallel loop went faster than the serial loop.

// The bracket style loop-expression described
// much earlier implicitly uses a forall loop.
  [val in myBigArray] val = 1 / val; // Parallel operation

// Atomic variables, common to many languages, are ones whose operations
// occur uninterrupted. Multiple threads can therefore modify atomic
// variables and can know that their values are safe.
// Chapel atomic variables can be of type bool, int,
// uint, and real.
  var uranium: atomic int;
  uranium.write(238);      // atomically write a variable
  writeln(uranium.read()); // atomically read a variable

// Atomic operations are described as functions, so you can define your own.
  uranium.sub(3); // atomically subtract a variable
  writeln(uranium.read());

  var replaceWith = 239;
  var was = uranium.exchange(replaceWith);
  writeln("uranium was ", was, " but is now ", replaceWith);

  var isEqualTo = 235;
  if uranium.compareExchange(isEqualTo, replaceWith) {
    writeln("uranium was equal to ", isEqualTo,
             " so replaced value with ", replaceWith);
  } else {
    writeln("uranium was not equal to ", isEqualTo,
             " so value stays the same...  whatever it was");
  }

  sync {
    begin { // Reader task
      writeln("Reader: waiting for uranium to be ", isEqualTo);
      uranium.waitFor(isEqualTo);
      writeln("Reader: uranium was set (by someone) to ", isEqualTo);
    }

    begin { // Writer task
      writeln("Writer: will set uranium to the value ", isEqualTo, " in...");
      countdown(3);
      uranium.write(isEqualTo);
    }
  }

// sync variables have two states: empty and full.
// If you read an empty variable or write a full variable, you are waited
// until the variable is full or empty again.
  var someSyncVar$: sync int; // varName$ is a convention not a law.
  sync {
    begin { // Reader task
      writeln("Reader: waiting to read.");
      var read_sync = someSyncVar$;
      writeln("Reader: value is ", read_sync);
    }

    begin { // Writer task
      writeln("Writer: will write in...");
      countdown(3);
      someSyncVar$ = 123;
    }
  }

// single vars can only be written once. A read on an unwritten single
// results in a wait, but when the variable has a value it can be read
// indefinitely.
  var someSingleVar$: single int; // varName$ is a convention not a law.
  sync {
    begin { // Reader task
      writeln("Reader: waiting to read.");
      for i in 1..5 {
        var read_single = someSingleVar$;
        writeln("Reader: iteration ", i,", and the value is ", read_single);
      }
    }

    begin { // Writer task
      writeln("Writer: will write in...");
      countdown(3);
      someSingleVar$ = 5; // first and only write ever.
    }
  }

// Here's an example using atomics and a sync variable to create a
// count-down mutex (also known as a multiplexer).
  var count: atomic int; // our counter
  var lock$: sync bool;   // the mutex lock

  count.write(2);       // Only let two tasks in at a time.
  lock$.writeXF(true);  // Set lock$ to full (unlocked)
  // Note: The value doesn't actually matter, just the state
  // (full:unlocked / empty:locked)
  // Also, writeXF() fills (F) the sync var regardless of its state (X)

  coforall task in 1..#5 { // Generate tasks
    // Create a barrier
    do {
      lock$;                 // Read lock$ (wait)
    } while (count.read() < 1); // Keep waiting until a spot opens up

    count.sub(1);          // decrement the counter
    lock$.writeXF(true); // Set lock$ to full (signal)

    // Actual 'work'
    writeln("Task #", task, " doing work.");
    sleep(2);

    count.add(1);        // Increment the counter
    lock$.writeXF(true); // Set lock$ to full (signal)
  }

// We can define the operations + * & | ^ && || min max minloc maxloc
// over an entire array using scans and reductions.
// Reductions apply the operation over the entire array and
// result in a scalar value.
  var listOfValues: [1..10] int = [15,57,354,36,45,15,456,8,678,2];
  var sumOfValues = + reduce listOfValues;
  var maxValue = max reduce listOfValues; // 'max' give just max value

// maxloc gives max value and index of the max value.
// Note: We have to zip the array and domain together with the zip iterator.
  var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues,
                                                  listOfValues.domain);

  writeln((sumOfValues, maxValue, idxOfMax, listOfValues[idxOfMax]));

// Scans apply the operation incrementally and return an array with the
// values of the operation at that index as it progressed through the
// array from array.domain.low to array.domain.high.
  var runningSumOfValues = + scan listOfValues;
  var maxScan = max scan listOfValues;
  writeln(runningSumOfValues);
  writeln(maxScan);
} // end main()
```

Who is this tutorial for?
-------------------------

This tutorial is for people who want to learn the ropes of chapel without
having to hear about what fiber mixture the ropes are, or how they were
braided, or how the braid configurations differ between one another. It won't
teach you how to develop amazingly performant code, and it's not exhaustive.
Refer to the [language specification](http://chapel.cray.com/language.html) and
the [module documentation](http://chapel.cray.com/docs/latest/) for more
details.

Occasionally check back here and on the [Chapel site](http://chapel.cray.com)
to see if more topics have been added or more tutorials created.

### What this tutorial is lacking:

 * Exposition of the [standard modules](http://chapel.cray.com/docs/latest/modules/modules.html)
 * Multiple Locales (distributed memory system)
 * Records
 * Parallel iterators

Your input, questions, and discoveries are important to the developers!
-----------------------------------------------------------------------

The Chapel language is still in-development (version 1.15.0), so there are
occasional hiccups with performance and language features. The more information
you give the Chapel development team about issues you encounter or features you
would like to see, the better the language becomes. Feel free to email the team
and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman).

If you're really interested in the development of the compiler or contributing
to the project, [check out the master GitHub repository](https://github.com/chapel-lang/chapel).
It is under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0).

Installing the Compiler
-----------------------

Chapel can be built and installed on your average 'nix machine (and cygwin).
[Download the latest release version](https://github.com/chapel-lang/chapel/releases/)
and it's as easy as

 1. `tar -xvf chapel-1.15.0.tar.gz`
 2. `cd chapel-1.15.0`
 3. `source util/setchplenv.bash # or .sh or .csh or .fish`
 4. `make`
 5. `make check # optional`

You will need to `source util/setchplenv.EXT` from within the Chapel directory
(`$CHPL_HOME`) every time your terminal starts so it's suggested that you drop
that command in a script that will get executed on startup (like .bashrc).

Chapel is easily installed with Brew for OS X

 1. `brew update`
 2. `brew install chapel`

Compiling Code
--------------

Builds like other compilers:

`chpl myFile.chpl -o myExe`

Notable arguments:

 * `--fast`: enables a number of optimizations and disables array bounds
   checks. Should only enable when application is stable.
 * `--set <Symbol Name>=<Value>`: set config param `<Symbol Name>` to `<Value>`
   at compile-time.
 * `--main-module <Module Name>`: use the main() procedure found in the module
   `<Module Name>` as the executable's main.
 * `--module-dir <Directory>`: includes `<Directory>` in the module search path.
---
language: "CHICKEN"
filename: CHICKEN.scm
contributors:
  - ["Diwakar Wagle", "https://github.com/deewakar"]
---


CHICKEN is an implementation of Scheme programming language that can
compile Scheme programs to C code as well as interpret them. CHICKEN
supports RSR5 and RSR7 (work in progress) standards and many extensions.


```scheme
;; #!/usr/bin/env csi -s

;; Run the CHICKEN REPL in the commandline as follows :
;; $ csi

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 0. Syntax
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Single line comments start with a semicolon

#| Block comments
   can span multiple lines and...
   #| can be nested
   |#
|#

;; S-expression comments are used to comment out expressions
#; (display "nothing")    ; discard this expression 

;; CHICKEN has two fundamental pieces of syntax: Atoms and S-expressions
;; an atom is something that evaluates to itself
;; all builtin data types viz. numbers, chars, booleans, strings etc. are atoms
;; Furthermore an atom can be a symbol, an identifier, a keyword, a procedure
;; or the empty list (also called null)
'athing              ;; => athing 
'+                   ;; => + 
+                    ;; => <procedure C_plus>

;; S-expressions (short for symbolic expressions) consists of one or more atoms
(quote +)            ;; => + ; another way of writing '+
(+ 1 2 3)            ;; => 6 ; this S-expression evaluates to a function call
'(+ 1 2 3)           ;; => (+ 1 2 3) ; evaluates to a list 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 1. Primitive Datatypes and Operators 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Numbers
99999999999999999999 ;; integers
#b1010               ;; binary ; => 10
#o10                 ;; octal  ; => 8
#x8ded               ;; hexadecimal ; => 36333
3.14                 ;; real
6.02e+23
3/4                  ;; rational

;;Characters and Strings
#\A                  ;; A char
"Hello, World!"      ;; strings are fixed-length arrays of characters

;; Booleans
#t                  ;; true
#f                  ;; false

;; Function call is written as (f x y z ...)
;; where f is a function and x,y,z, ... are arguments
(print "Hello, World!")    ;; => Hello, World!
;; formatted output
(printf "Hello, ~a.\n" "World")  ;; => Hello, World.

;; print commandline arguments
(map print (command-line-arguments)) 

(list 'foo 'bar 'baz)          ;; => (foo bar baz)
(string-append "pine" "apple") ;; => "pineapple"
(string-ref "tapioca" 3)       ;; => #\i;; character 'i' is at index 3
(string->list "CHICKEN")       ;; => (#\C #\H #\I #\C #\K #\E #\N)
(string->intersperse '("1" "2") ":") ;; => "1:2"
(string-split "1:2:3" ":")     ;; => ("1" "2" "3")


;; Predicates are special functions that return boolean values
(atom? #t)                ;; => #t

(symbol? #t)              ;; => #f

(symbol? '+)              ;; => #t

(procedure? +)            ;; => #t

(pair? '(1 2))            ;; => #t

(pair? '(1 2 . 3))        ;; => #t

(pair? '())               ;; => #f

(list? '())               ;; => #t


;; Some arithmetic operations

(+ 1 1)                   ;; => 2
(- 8 1)                   ;; => 7
(* 10 2)                  ;; => 20
(expt 2 3)                ;; => 8
(remainder 5 2)           ;; => 1
(/ 35 5)                  ;; => 7
(/ 1 3)                   ;; => 0.333333333333333

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 2. Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; You can create variables with define
;; A variable name can use any character except: ()[]{}",'`;#\
(define myvar 5)
myvar        ;; => 5

;; Alias to a procedure
(define ** expt)
(** 2 3)     ;; => 8

;; Accessing an undefined variable raises an exception
s            ;; => Error: unbound variable: s

;; Local binding
(let ((me "Bob"))
  (print me))     ;; => Bob

(print me)        ;; => Error: unbound variable: me

;; Assign a new value to previously defined variable
(set! myvar 10) 
myvar             ;; => 10


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 3. Collections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Pairs
;; 'cons' constructs pairs, 
;; 'car' extracts the first element, 'cdr' extracts the rest of the elements
(cons 'subject 'verb)       ;; => '(subject . verb)
(car (cons 'subject 'verb)) ;; => subject
(cdr (cons 'subject 'verb)) ;; => verb

;; Lists
;; cons creates a new list if the second item is a list
(cons 0 '())         ;; => (0)
(cons 1 (cons 2  (cons 3 '())))    ;; => (1 2 3)
;; 'list' is a convenience variadic constructor for lists
(list 1 2 3)    ;; => (1 2 3)


;; Use 'append' to append lists together
(append '(1 2) '(3 4)) ;; => (1 2 3 4)

;; Some basic operations on lists
(map add1 '(1 2 3))    ;; => (2 3 4)
(reverse '(1 3 4 7))   ;; => (7 4 3 1)
(sort '(11 22 33 44) >)   ;; => (44 33 22 11)

(define days '(SUN MON FRI))
(list-ref days 1)      ;; => MON
(set! (list-ref days 1) 'TUE)
days                   ;; => (SUN TUE FRI)

;; Vectors
;; Vectors are heterogeneous structures whose elements are indexed by integers
;; A Vector typically occupies less space than a list of the same length
;; Random access of an element in a vector is faster than in a list
#(1 2 3)                     ;; => #(1 2 3) ;; literal syntax
(vector 'a 'b 'c)            ;; => #(a b c) 
(vector? #(1 2 3))           ;; => #t
(vector-length #(1 (2) "a")) ;; => 3
(vector-ref #(1 (2) (3 3)) 2);; => (3 3)

(define vec #(1 2 3))
(vector-set! vec 2 4)
vec                         ;; => #(1 2 4)

;; Vectors can be created from lists and vice-verca
(vector->list #(1 2 4))     ;; => '(1 2 4)
(list->vector '(a b c))     ;; => #(a b c)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 4. Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use 'lambda' to create functions.
;; A function always returns the value of its last expression
(lambda () "Hello World")   ;; => #<procedure (?)> 

;; Use extra parens around function definition to execute 
((lambda () "Hello World")) ;; => Hello World ;; argument list is empty

;; A function with an argument
((lambda (x) (* x x)) 3)           ;; => 9
;; A function with two arguments
((lambda (x y) (* x y)) 2 3)       ;; => 6

;; assign a function to a variable
(define sqr (lambda (x) (* x x)))
sqr                        ;; => #<procedure (sqr x)>
(sqr 3)                    ;; => 9

;; We can shorten this using the function definition syntactic sugar
(define (sqr x) (* x x))
(sqr 3)                    ;; => 9

;; We can redefine existing procedures
(foldl cons '() '(1 2 3 4 5)) ;; => (((((() . 1) . 2) . 3) . 4) . 5)
(define (foldl func accu alist)
  (if (null? alist)
    accu
    (foldl func (func (car alist) accu) (cdr alist))))

(foldl cons '() '(1 2 3 4 5))   ;; => (5 4 3 2 1)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 5. Equality
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; For numbers use '='
(= 3 3.0)                  ;; => #t
(= 2 1)                    ;; => #f

;; 'eq?' returns #t if two arguments refer to the same object in memory
;; In other words, it's a simple pointer comparison.
(eq? '() '())              ;; => #t ;; there's only one empty list in memory
(eq? (list 3) (list 3))    ;; => #f ;; not the same object
(eq? 'yes 'yes)            ;; => #t
(eq? 3 3)                  ;; => #t ;; don't do this even if it works in this case
(eq? 3 3.0)                ;; => #f ;; it's better to use '=' for number comparisons
(eq? "Hello" "Hello")      ;; => #f

;; 'eqv?' is same as 'eq?' all datatypes except numbers and characters
(eqv? 3 3.0)               ;; => #f
(eqv? (expt 2 3) (expt 2 3)) ;; => #t
(eqv? 'yes 'yes)           ;; => #t

;; 'equal?' recursively compares the contents of pairs, vectors, and strings,
;; applying eqv? on other objects such as numbers and symbols. 
;; A rule of thumb is that objects are generally equal? if they print the same.

(equal? '(1 2 3) '(1 2 3)) ;; => #t
(equal? #(a b c) #(a b c)) ;; => #t
(equal? 'a 'a)             ;; => #t
(equal? "abc" "abc")       ;; => #t

;; In Summary:
;; eq? tests if objects are identical
;; eqv? tests if objects are operationally equivalent
;; equal? tests if objects have same structure and contents

;; Comparing strings for equality
(string=? "Hello" "Hello") ;; => #t


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 6. Control Flow
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Conditionals
(if #t                     ;; test expression
  "True"                   ;; then expression
  "False")                 ;; else expression
                           ;; => "True"

(if (> 3 2)
  "yes"
  "no")                    ;; => "yes"

;; In conditionals, all values that are not '#f' are treated as true.
;; 0, '(), #() "" , are all true values
(if 0
  "0 is not false"
  "0 is false")            ;; => "0 is not false"

;; 'cond' chains a series of tests and returns as soon as it encounters a true condition
;; 'cond' can be used to simulate 'if/elseif/else' statements
(cond ((> 2 2) "not true so don't return this")
      ((< 2 5) "true, so return this")
      (else "returning default"))    ;; => "true, so return this"


;; A case expression is evaluated as follows:
;; The key is evaluated and compared with each datum in sense of 'eqv?',
;; The corresponding clause in the matching datum is evaluated and returned as result
(case (* 2 3)              ;; the key is 6
  ((2 3 5 7) 'prime)       ;; datum 1
  ((1 4 6 8) 'composite))  ;; datum 2; matched!
                           ;; => composite

;; case with else clause
(case (car '(c d))
  ((a e i o u) 'vowel)
  ((w y) 'semivowel)
  (else 'consonant))       ;; =>  consonant

;; Boolean expressions
;; 'and' returns the first expression that evaluates to #f
;; otherwise, it returns the result of the last expression
(and #t #f (= 2 2.0))                ;; => #f
(and (< 2 5) (> 2 0) "0 < 2 < 5")    ;; => "0 < 2 < 5"

;; 'or' returns the first expression that evaluates to #t 
;; otherwise the result of the last expression is returned
(or #f #t #f)                        ;; => #t
(or #f #f #f)                        ;; => #f

;; 'when' is like 'if' without the else expression
(when (positive? 5) "I'm positive")  ;; => "I'm positive"

;; 'unless' is equivalent to (when (not <test>) <expr>)
(unless (null? '(1 2 3)) "not null") ;; => "not null"


;; Loops
;; loops can be created with the help of tail-recursions
(define (loop count)
  (unless (= count 0)
    (print "hello") 
    (loop (sub1 count))))
(loop 4)                             ;; => hello, hello ...

;; Or with a named let
(let loop ((i 0) (limit 5))
  (when (< i limit)
    (printf "i = ~a\n" i)
    (loop (add1 i) limit)))          ;; => i = 0, i = 1....

;; 'do' is another iteration construct
;; It initializes a set of variables and updates them in each iteration
;; A final expression is evaluated after the exit condition is met
(do ((x 0 (add1 x )))            ;; initialize x = 0 and add 1 in each iteration
  ((= x 10) (print "done"))      ;; exit condition and final expression
  (print x))                     ;; command to execute in each step
                                 ;; => 0,1,2,3....9,done

;; Iteration over lists 
(for-each (lambda (a) (print (* a a)))
          '(3 5 7))                  ;; => 9, 25, 49

;; 'map' is like for-each but returns a list
(map add1 '(11 22 33))               ;; => (12 23 34)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 7. Extensions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; The CHICKEN core is very minimal, but additional features are provided by library extensions known as Eggs.
;; You can install Eggs with 'chicken-install <eggname>' command.

;; 'numbers' egg provides support for full numeric tower.
(require-extension numbers)
;; complex numbers
3+4i                               ;; => 3+2i
;; Supports fractions without falling back to inexact flonums
1/3                                ;; => 1/3
;; provides support for large integers through bignums
(expt 9 20)                        ;; => 12157665459056928801 
;; And other 'extended' functions
(log 10 (exp 1))                   ;; => 2.30258509299405
(numerator 2/3)                    ;; => 2

;; 'utf8' provides unicode support
(require-extension utf8)
"\u03BBx:(\u03BC\u0251.\u0251\u2192\u0251).xx" ;; => "λx:(μɑ.ɑ→ɑ).xx"

;; 'posix' provides file I/O and lots of other services for unix-like operating systems
;; Some of the functions are not available in Windows system,
;; See http://wiki.call-cc.org/man/4/Unit%20posix for more details

;; Open a file to append, open "write only" and create file if it does not exist
(define outfn (file-open "chicken-hen.txt" (+ open/append open/wronly open/creat)))
;; write some text to the file
(file-write outfn "Did chicken came before hen?") 
;; close the file
(file-close outfn)
;; Open the file "read only"
(define infn (file-open "chicken-hen.txt" open/rdonly))
;; read some text from the file
(file-read infn 30)         ;; => ("Did chicken came before hen?  ", 28)
(file-close infn)

;; CHICKEN also supports SRFI (Scheme Requests For Implementation) extensions
;; See 'http://srfi.schemers.org/srfi-implementers.html" to see srfi's supported by CHICKEN
(require-extension srfi-1)         ;; list library
(filter odd? '(1 2 3 4 5 6 7))     ;; => (1 3 5 7)
(count even? '(1 2 3 4 5))         ;; => 2
(take '(12 24 36 48 60) 3)         ;; => (12 24 36)
(drop '(12 24 36 48 60) 2)         ;; => (36 48 60)
(circular-list 'z 'q)              ;; => z q z q ...

(require-extension srfi-13)        ;; string library
(string-reverse "pan")             ;; => "nap"
(string-index "Turkey" #\k)        ;; => 3
(string-every char-upper-case? "CHICKEN") ;; => #t
(string-join '("foo" "bar" "baz") ":")    ;; => "foo:bar:baz"


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 8. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; A 'for .. in ..' iteration like python, for lists
(define-syntax for
  (syntax-rules (in)
                ((for elem in alist body ...)
                 (for-each (lambda (elem) body ...) alist))))

(for x in '(2 4 8 16)
     (print x))          ;; => 2, 4, 8, 16

(for chr in (string->list "PENCHANT")
     (print chr))        ;; => P, E, N, C, H, A, N, T

;; While loop
(define-syntax while
  (syntax-rules ()
                ((while cond body ...)
                 (let loop ()
                   (when cond
                     body ...
                     (loop))))))

(let ((str "PENCHANT") (i 0))
  (while (< i (string-length str))     ;; while (condition)
         (print (string-ref str i))    ;; body 
         (set! i (add1 i))))           
                                       ;; => P, E, N, C, H, A, N, T

;; Advanced Syntax-Rules Primer -> http://petrofsky.org/src/primer.txt
;; Macro system in chicken -> http://lists.gnu.org/archive/html/chicken-users/2008-04/msg00013.html

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 9. Modules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Also See http://wiki.call-cc.org/man/4/Modules

;; The 'test' module exports a value named 'hello' and a macro named 'greet'
(module test (hello greet)
  (import scheme)

  (define-syntax greet
    (syntax-rules ()
      ((_ whom) 
       (begin
         (display "Hello, ")
         (display whom)
         (display " !\n") ) ) ) )

  (define (hello)
    (greet "world") )  )

;; we can define our modules in a separate file (say test.scm) and load them to the interpreter with
;;         (load "test.scm")

;; import the module
(import test)
(hello)                ;; => Hello, world !
(greet "schemers")     ;; => Hello, schemers !

;; We can compile the module files in to shared libraries by using following command,
;;         csc -s test.scm
;;         (load "test.so")

;; Functors
;; Functors are high level modules that can be parameterized by other modules
;; Following functor requires another module named 'M' that provides a function called 'multiply'
;; The functor itself exports a generic function 'square'
(functor (squaring-functor (M (multiply))) (square)
         (import scheme M) 
         (define (square x) (multiply x x)))

;; Module 'nums' can be passed as a parameter to 'squaring-functor'
(module nums (multiply) 
        (import scheme)     ;; predefined modules
        (define (multiply x y) (* x y))) 
;; the final module can be imported and used in our program
(module number-squarer = (squaring-functor nums)) 

(import number-squarer)
(square 3)              ;; => 9

;; We can instantiate the functor for other inputs
;; Here's another example module that can be passed to squaring-functor
(module stars (multiply)
        (import chicken scheme)  ;; chicken module for the 'use' keyword
        (use srfi-1)             ;; we can use external libraries in our module
        (define (multiply x y)
          (list-tabulate x (lambda _ (list-tabulate y (lambda _ '*))))))
(module star-squarer = (squaring-functor stars))

(import star-squarer)
(square 3)              ;; => ((* * *)(* * *)(* * *))

```
## Further Reading
* [CHICKEN User's Manual](http://wiki.call-cc.org/man/4/The%20User%27s%20Manual).
* [RSR5 standards](http://www.schemers.org/Documents/Standards/R5RS)


## Extra Info

* [For programmers of other languages](http://wiki.call-cc.org/chicken-for-programmers-of-other-languages)
* [Compare CHICKEN syntax with other languages](http://plr.sourceforge.net/cgi-bin/plr/launch.py)
---
language: "clojure macros"
filename: learnclojuremacros.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
---

As with all Lisps, Clojure's inherent [homoiconicity](https://en.wikipedia.org/wiki/Homoiconic)
gives you access to the full extent of the language to write code-generation routines
called "macros". Macros provide a powerful way to tailor the language to your needs.

Be careful though. It's considered bad form to write a macro when a function will do.
Use a macro only when you need control over when or if the arguments to a form will
be evaluated.

You'll want to be familiar with Clojure. Make sure you understand everything in
[Clojure in Y Minutes](/docs/clojure/).

```clojure
;; Define a macro using defmacro. Your macro should output a list that can
;; be evaluated as clojure code.
;;
;; This macro is the same as if you wrote (reverse "Hello World")
(defmacro my-first-macro []
  (list reverse "Hello World"))

;; Inspect the result of a macro using macroexpand or macroexpand-1.
;;
;; Note that the call must be quoted.
(macroexpand '(my-first-macro))
;; -> (#<core$reverse clojure.core$reverse@xxxxxxxx> "Hello World")

;; You can eval the result of macroexpand directly:
(eval (macroexpand '(my-first-macro)))
; -> (\d \l \o \r \W \space \o \l \l \e \H)

;; But you should use this more succinct, function-like syntax:
(my-first-macro)  ; -> (\d \l \o \r \W \space \o \l \l \e \H)

;; You can make things easier on yourself by using the more succinct quote syntax
;; to create lists in your macros:
(defmacro my-first-quoted-macro []
  '(reverse "Hello World"))

(macroexpand '(my-first-quoted-macro))
;; -> (reverse "Hello World")
;; Notice that reverse is no longer function object, but a symbol.

;; Macros can take arguments.
(defmacro inc2 [arg]
  (list + 2 arg))

(inc2 2) ; -> 4

;; But, if you try to do this with a quoted list, you'll get an error, because
;; the argument will be quoted too. To get around this, clojure provides a
;; way of quoting macros: `. Inside `, you can use ~ to get at the outer scope
(defmacro inc2-quoted [arg]
  `(+ 2 ~arg))

(inc2-quoted 2)

;; You can use the usual destructuring args. Expand list variables using ~@
(defmacro unless [arg & body]
  `(if (not ~arg)
     (do ~@body))) ; Remember the do!

(macroexpand '(unless true (reverse "Hello World")))
;; ->
;; (if (clojure.core/not true) (do (reverse "Hello World")))

;; (unless) evaluates and returns its body if the first argument is false.
;; Otherwise, it returns nil

(unless true "Hello") ; -> nil
(unless false "Hello") ; -> "Hello"

;; Used without care, macros can do great evil by clobbering your vars
(defmacro define-x []
  '(do
     (def x 2)
     (list x)))

(def x 4)
(define-x) ; -> (2)
(list x) ; -> (2)

;; To avoid this, use gensym to get a unique identifier
(gensym 'x) ; -> x1281 (or some such thing)

(defmacro define-x-safely []
  (let [sym (gensym 'x)]
    `(do
       (def ~sym 2)
       (list ~sym))))

(def x 4)
(define-x-safely) ; -> (2)
(list x) ; -> (4)

;; You can use # within ` to produce a gensym for each symbol automatically
(defmacro define-x-hygienically []
  `(do
     (def x# 2)
     (list x#)))

(def x 4)
(define-x-hygienically) ; -> (2)
(list x) ; -> (4)

;; It's typical to use helper functions with macros. Let's create a few to
;; help us support a (dumb) inline arithmetic syntax
(declare inline-2-helper)
(defn clean-arg [arg]
  (if (seq? arg)
    (inline-2-helper arg)
    arg))

(defn apply-arg
  "Given args [x (+ y)], return (+ x y)"
  [val [op arg]]
  (list op val (clean-arg arg)))

(defn inline-2-helper
  [[arg1 & ops-and-args]]
  (let [ops (partition 2 ops-and-args)]
    (reduce apply-arg (clean-arg arg1) ops)))

;; We can test it immediately, without creating a macro
(inline-2-helper '(a + (b - 2) - (c * 5))) ; -> (- (+ a (- b 2)) (* c 5))

; However, we'll need to make it a macro if we want it to be run at compile time
(defmacro inline-2 [form]
  (inline-2-helper form)))

(macroexpand '(inline-2 (1 + (3 / 2) - (1 / 2) + 1)))
; -> (+ (- (+ 1 (/ 3 2)) (/ 1 2)) 1)

(inline-2 (1 + (3 / 2) - (1 / 2) + 1))
; -> 3 (actually, 3N, since the number got cast to a rational fraction with /)
```

### Further Reading

Writing Macros from [Clojure for the Brave and True](http://www.braveclojure.com/)
[http://www.braveclojure.com/writing-macros/](http://www.braveclojure.com/writing-macros/)

Official docs
[http://clojure.org/macros](http://clojure.org/macros)

When to use macros?
[http://dunsmor.com/lisp/onlisp/onlisp_12.html](http://dunsmor.com/lisp/onlisp/onlisp_12.html)
---
language: clojure
filename: learnclojure.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
---

Clojure is a Lisp family language developed for the Java Virtual Machine. It has
a much stronger emphasis on pure [functional programming](https://en.wikipedia.org/wiki/Functional_programming) than
Common Lisp, but includes several [STM](https://en.wikipedia.org/wiki/Software_transactional_memory) utilities to handle
state as it comes up.

This combination allows it to handle concurrent processing very simply,
and often automatically.

(You need a version of Clojure 1.2 or newer)


```clojure
; Comments start with semicolons.

; Clojure is written in "forms", which are just
; lists of things inside parentheses, separated by whitespace.
;
; The clojure reader assumes that the first thing is a
; function or macro to call, and the rest are arguments.

; The first call in a file should be ns, to set the namespace
(ns learnclojure)

; More basic examples:

; str will create a string out of all its arguments
(str "Hello" " " "World") ; => "Hello World"

; Math is straightforward
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; Equality is =
(= 1 1) ; => true
(= 2 1) ; => false

; You need not for logic, too
(not true) ; => false

; Nesting forms works as you expect
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; Types
;;;;;;;;;;;;;

; Clojure uses Java's object types for booleans, strings and numbers.
; Use `class` to inspect them.
(class 1) ; Integer literals are java.lang.Long by default
(class 1.); Float literals are java.lang.Double
(class ""); Strings always double-quoted, and are java.lang.String
(class false) ; Booleans are java.lang.Boolean
(class nil); The "null" value is called nil

; If you want to create a literal list of data, use ' to stop it from
; being evaluated
'(+ 1 2) ; => (+ 1 2)
; (shorthand for (quote (+ 1 2)))

; You can eval a quoted list
(eval '(+ 1 2)) ; => 3

; Collections & Sequences
;;;;;;;;;;;;;;;;;;;

; Lists are linked-list data structures, while Vectors are array-backed.
; Vectors and Lists are java classes too!
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; A list would be written as just (1 2 3), but we have to quote
; it to stop the reader thinking it's a function.
; Also, (list 1 2 3) is the same as '(1 2 3)

; "Collections" are just groups of data
; Both lists and vectors are collections:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; "Sequences" (seqs) are abstract descriptions of lists of data.
; Only lists are seqs.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; A seq need only provide an entry when it is accessed.
; So, seqs which can be lazy -- they can define infinite series:
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (an infinite series)
(take 4 (range)) ;  (0 1 2 3)

; Use cons to add an item to the beginning of a list or vector
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; Conj will add an item to a collection in the most efficient way.
; For lists, they insert at the beginning. For vectors, they insert at the end.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; Use concat to add lists or vectors together
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; Use filter, map to interact with collections
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; Use reduce to reduce them
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; Reduce can take an initial-value argument too
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; Functions
;;;;;;;;;;;;;;;;;;;;;

; Use fn to create new functions. A function always returns
; its last statement.
(fn [] "Hello World") ; => fn

; (You need extra parens to call it)
((fn [] "Hello World")) ; => "Hello World"

; You can create a var using def
(def x 1)
x ; => 1

; Assign a function to a var
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; You can shorten this process by using defn
(defn hello-world [] "Hello World")

; The [] is the list of arguments for the function.
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; You can also use this shorthand to create functions:
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; You can have multi-variadic functions, too
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; Functions can pack extra arguments up in a seq for you
(defn count-args [& args]
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"

; You can mix regular and packed arguments
(defn hello-count [name & args]
  (str "Hello " name ", you passed " (count args) " extra args"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"


; Maps
;;;;;;;;;;

; Hash maps and array maps share an interface. Hash maps have faster lookups
; but don't retain key order.
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Arraymaps will automatically become hashmaps through most operations
; if they get big enough, so you don't need to worry.

; Maps can use any hashable type as a key, but usually keywords are best
; Keywords are like strings with some efficiency bonuses
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; By the way, commas are always treated as whitespace and do nothing.

; Retrieve a value from a map by calling it as a function
(stringmap "a") ; => 1
(keymap :a) ; => 1

; Keywords can be used to retrieve their value from a map, too!
(:b keymap) ; => 2

; Don't try this with strings.
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; Retrieving a non-present key returns nil
(stringmap "d") ; => nil

; Use assoc to add new keys to hash-maps
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; But remember, clojure types are immutable!
keymap ; => {:a 1, :b 2, :c 3}

; Use dissoc to remove keys
(dissoc keymap :a :b) ; => {:c 3}

; Sets
;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; Add a member with conj
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; Remove one with disj
(disj #{1 2 3} 1) ; => #{2 3}

; Test for existence by using the set as a function:
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; There are more functions in the clojure.sets namespace.

; Useful forms
;;;;;;;;;;;;;;;;;

; Logic constructs in clojure are just macros, and look like
; everything else
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; Use let to create temporary bindings
(let [a 1 b 2]
  (> a b)) ; => false

; Group statements together with do
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; Functions have an implicit do
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; So does let
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")


; Use the threading macros (-> and ->>) to express transformations of
; data more clearly.

; The "Thread-first" macro (->) inserts into each form the result of
; the previous, as the first argument (second item)
(->  
   {:a 1 :b 2} 
   (assoc :c 3) ;=> (assoc {:a 1 :b 2} :c 3)
   (dissoc :b)) ;=> (dissoc (assoc {:a 1 :b 2} :c 3) :b)

; This expression could be written as:
; (dissoc (assoc {:a 1 :b 2} :c 3) :b)
; and evaluates to {:a 1 :c 3}

; The double arrow does the same thing, but inserts the result of
; each line at the *end* of the form. This is useful for collection
; operations in particular:
(->>
   (range 10)
   (map inc)     ;=> (map inc (range 10)
   (filter odd?) ;=> (filter odd? (map inc (range 10))
   (into []))    ;=> (into [] (filter odd? (map inc (range 10)))
                 ; Result: [1 3 5 7 9]

; When you are in a situation where you want more freedom as where to
; put the result of previous data transformations in an 
; expression, you can use the as-> macro. With it, you can assign a
; specific name to transformations' output and use it as a
; placeholder in your chained expressions:

(as-> [1 2 3] input
  (map inc input);=> You can use last transform's output at the last position
  (nth input 2) ;=>  and at the second position, in the same expression
  (conj [4 5 6] input [8 9 10])) ;=> or in the middle !



; Modules
;;;;;;;;;;;;;;;

; Use "use" to get all functions from the module
(use 'clojure.set)

; Now we can use set operations
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; You can choose a subset of functions to import, too
(use '[clojure.set :only [intersection]])

; Use require to import a module
(require 'clojure.string)

; Use / to call functions from a module
; Here, the module is clojure.string and the function is blank?
(clojure.string/blank? "") ; => true

; You can give a module a shorter name on import
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (#"" denotes a regular expression literal)

; You can use require (and use, but don't) from a namespace using :require.
; You don't need to quote your modules if you do it this way.
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java has a huge and useful standard library, so
; you'll want to learn how to get at it.

; Use import to load a java module
(import java.util.Date)

; You can import from an ns too.
(ns test
  (:import java.util.Date
           java.util.Calendar))

; Use the class name with a "." at the end to make a new instance
(Date.) ; <a date object>

; Use . to call methods. Or, use the ".method" shortcut
(. (Date.) getTime) ; <a timestamp>
(.getTime (Date.)) ; exactly the same thing.

; Use / to call static methods
(System/currentTimeMillis) ; <a timestamp> (system is always present)

; Use doto to make dealing with (mutable) classes more tolerable
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => A Date. set to 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; Software Transactional Memory is the mechanism clojure uses to handle
; persistent state. There are a few constructs in clojure that use this.

; An atom is the simplest. Pass it an initial value
(def my-atom (atom {}))

; Update an atom with swap!.
; swap! takes a function and calls it with the current value of the atom
; as the first argument, and any trailing arguments as the second
(swap! my-atom assoc :a 1) ; Sets my-atom to the result of (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Sets my-atom to the result of (assoc {:a 1} :b 2)

; Use '@' to dereference the atom and get the value
my-atom  ;=> Atom<#...> (Returns the Atom object)
@my-atom ; => {:a 1 :b 2}

; Here's a simple counter using an atom
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; Other STM constructs are refs and agents.
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
```

### Further Reading

This is far from exhaustive, but hopefully it's enough to get you on your feet.

Clojure.org has lots of articles:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org has documentation with examples for most core functions:
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure is a great way to build your clojure/FP skills:
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org (yes, really) has a number of getting started articles:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: cmake
contributors:
    - ["Bruno Alano", "https://github.com/brunoalano"]
filename: CMake
---

CMake is a cross-platform, open-source build system. This tool will allow you
to test, compile and create packages of your source code.

The problem that CMake tries to solve is the problem of Makefiles and
Autoconfigure on cross-platforms (different make interpreters have different
command) and the ease-of-use on linking 3rd party libraries.

CMake is an extensible, open-source system that manages the build process in
an operating system and compiler-independent manner. Unlike many
cross-platform systems, CMake is designed to be used in conjunction with the
native build environment. Simple configuration files placed in each source 
directory (called CMakeLists.txt files) are used to generate standard build
files (e.g., makefiles on Unix and projects/workspaces in Windows MSVC) which
are used in the usual way.

```cmake
# In CMake, this is a comment

# To run our code, we will use these steps:
#  - mkdir build && cd build
#  - cmake ..
#  - make
# 
# With those steps, we will follow the best practice to compile into a subdir
# and the second line will request to CMake to generate a new OS-dependent
# Makefile. Finally, run the native Make command.

#------------------------------------------------------------------------------
# Basic
#------------------------------------------------------------------------------
#
# The CMake file MUST be named as "CMakeLists.txt".

# Setup the minimum version required of CMake to generate the Makefile
cmake_minimum_required (VERSION 2.8)

# Raises a FATAL_ERROR if version < 2.8
cmake_minimum_required (VERSION 2.8 FATAL_ERROR)

# We setup the name for our project. After we do that, this will change some
# directories naming convention generated by CMake. We can send the LANG of
# code as second param
project (learncmake C)

# Set the project source dir (just convention)
set( LEARN_CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
set( LEARN_CMAKE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )

# It's useful to setup the current version of our code in the build system
# using a `semver` style
set (LEARN_CMAKE_VERSION_MAJOR 1)
set (LEARN_CMAKE_VERSION_MINOR 0)
set (LEARN_CMAKE_VERSION_PATCH 0)

# Send the variables (version number) to source code header
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
)

# Include Directories
# In GCC, this will invoke the "-I" command
include_directories( include )

# Where are the additional libraries installed? Note: provide includes
# path here, subsequent checks will resolve everything else
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/modules/" )

# Conditions
if ( CONDITION )
  # Output!

  # Incidental information
  message(STATUS "My message")

  # CMake Warning, continue processing
  message(WARNING "My message")

  # CMake Warning (dev), continue processing
  message(AUTHOR_WARNING "My message")

  # CMake Error, continue processing, but skip generation
  message(SEND_ERROR "My message")

  # CMake Error, stop processing and generation
  message(FATAL_ERROR "My message")
endif()

if( CONDITION )

elseif( CONDITION )

else( CONDITION )

endif( CONDITION )

# Loops
foreach(loop_var arg1 arg2 ...)
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
endforeach(loop_var)

foreach(loop_var RANGE total)
foreach(loop_var RANGE start stop [step])

foreach(loop_var IN [LISTS [list1 [...]]]
                    [ITEMS [item1 [...]]])

while(condition)
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
endwhile(condition)


# Logic Operations
if(FALSE AND (FALSE OR TRUE))
  message("Don't display!")
endif()

# Set a normal, cache, or environment variable to a given value.
# If the PARENT_SCOPE option is given the variable will be set in the scope
# above the current scope.
# `set(<variable> <value>... [PARENT_SCOPE])`

# How to reference variables inside quoted and unquoted arguments
# A variable reference is replaced by the value of the variable, or by the
# empty string if the variable is not set
${variable_name}

# Lists
# Setup the list of source files
set( LEARN_CMAKE_SOURCES 
  src/main.c
  src/imagem.c
  src/pather.c
)

# Calls the compiler
#
# ${PROJECT_NAME} refers to Learn_CMake 
add_executable( ${PROJECT_NAME} ${LEARN_CMAKE_SOURCES} )

# Link the libraries
target_link_libraries( ${PROJECT_NAME} ${LIBS} m )

# Where are the additional libraries installed? Note: provide includes
# path here, subsequent checks will resolve everything else
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/modules/" )

# Compiler Condition (gcc ; g++)
if ( "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" )
  message( STATUS "Setting the flags for ${CMAKE_C_COMPILER_ID} compiler" )
  add_definitions( --std=c99 )
endif()

# Check for OS
if( UNIX )
    set( LEARN_CMAKE_DEFINITIONS
        "${LEARN_CMAKE_DEFINITIONS} -Wall -Wextra -Werror -Wno-deprecated-declarations -Wno-unused-parameter -Wno-comment" )
endif()
```

### More Resources

+ [cmake tutorial](https://cmake.org/cmake-tutorial/)
+ [cmake documentation](https://cmake.org/documentation/)
+ [mastering cmake](http://amzn.com/1930934319/)
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
filename: coffeescript.coffee
---

CoffeeScript is a little language that compiles one-to-one into the equivalent
JavaScript, and there is no interpretation at runtime. As one of the successors
to JavaScript, CoffeeScript tries its best to output readable, pretty-printed
and smooth-running JavaScript code, which works well in every JavaScript runtime.
It also attempts to try and make JavaScript more in line with the trends of many
modern languages.

See also [the CoffeeScript website](http://coffeescript.org/), which has a complete tutorial on CoffeeScript.

```coffeescript
# Comments are similar to Ruby and Python, using the hash symbol `#`

###
Block comments are like these, and they translate directly to '/ *'s and '* /'s
for the resulting JavaScript code.

You should understand most of JavaScript semantics
before continuing.
###

# Assignment:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# Conditions:
number = -42 if opposite #=> if(opposite) { number = -42; }

# Functions:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

fill = (container, liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."
#=>var fill;
#
#fill = function(container, liquid) {
#  if (liquid == null) {
#    liquid = "coffee";
#  }
#  return "Filling the " + container + " with " + liquid + "...";
#};

# Ranges:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# Objects:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#    "root": Math.sqrt,
#    "square": square,
#    "cube": function(x) { return x * square(x); }
#   };

# Splats:
race = (winner, runners...) ->
  print winner, runners
#=>race = function() {
#    var runners, winner;
#    winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#    return print(winner, runners);
#  };

# Existence:
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# Array comprehensions:
cubes = (math.cube num for num in list)
#=>cubes = (function() {
#	  var _i, _len, _results;
#	  _results = [];
# 	for (_i = 0, _len = list.length; _i < _len; _i++) {
#		  num = list[_i];
#		  _results.push(math.cube(num));
#	  }
#	  return _results;
# })();

foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
#=>foods = ['broccoli', 'spinach', 'chocolate'];
#
#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
#  food = foods[_k];
#  if (food !== 'chocolate') {
#    eat(food);
#  }
#}
```

## Additional resources

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: coldfusion
filename: learncoldfusion.cfm
contributors:
    - ["Wayne Boka", "http://wboka.github.io"]
    - ["Kevin Morris", "https://twitter.com/kevinmorris"]
---

ColdFusion is a scripting language for web development.
[Read more here.](http://www.adobe.com/products/coldfusion-family.html)

### CFML
_**C**old**F**usion **M**arkup **L**anguage_  
ColdFusion started as a tag-based language. Almost all functionality is available using tags.

```cfm
<em>HTML tags have been provided for output readability</em>

<!--- Comments start with "<!---" and end with "--->" --->
<!---
    Comments can
    also
    span
    multiple lines
--->

<!--- CFML tags have a similar format to HTML tags. --->
<h1>Simple Variables</h1>
<!--- Variable Declaration: Variables are loosely typed, similar to javascript --->
<p>Set <b>myVariable</b> to "myValue"</p>
<cfset myVariable = "myValue" />
<p>Set <b>myNumber</b> to 3.14</p>
<cfset myNumber = 3.14 />

<!--- Displaying simple data --->
<!--- Use <cfoutput> for simple values such as strings, numbers, and expressions --->
<p>Display <b>myVariable</b>: <cfoutput>#myVariable#</cfoutput></p><!--- myValue --->
<p>Display <b>myNumber</b>: <cfoutput>#myNumber#</cfoutput></p><!--- 3.14 --->

<hr />

<h1>Complex Variables</h1>
<!--- Declaring complex variables --->
<!--- Declaring an array of 1 dimension: literal or bracket notation --->
<p>Set <b>myArray1</b> to an array of 1 dimension using literal or bracket notation</p>
<cfset myArray1 = [] />
<!--- Declaring an array of 1 dimension: function notation --->
<p>Set <b>myArray2</b> to an array of 1 dimension using function notation</p>
<cfset myArray2 = ArrayNew(1) />

<!--- Outputting complex variables --->
<p>Contents of <b>myArray1</b></p>
<cfdump var="#myArray1#" /> <!--- An empty array object --->
<p>Contents of <b>myArray2</b></p>
<cfdump var="#myArray2#" /> <!--- An empty array object --->

<!--- Operators --->
<!--- Arithmetic --->
<h1>Operators</h1>
<h2>Arithmetic</h2>
<p>1 + 1 = <cfoutput>#1 + 1#</cfoutput></p>
<p>10 - 7 = <cfoutput>#10 - 7#<br /></cfoutput></p>
<p>15 * 10 = <cfoutput>#15 * 10#<br /></cfoutput></p>
<p>100 / 5 = <cfoutput>#100 / 5#<br /></cfoutput></p>
<p>120 % 5 = <cfoutput>#120 % 5#<br /></cfoutput></p>
<p>120 mod 5 = <cfoutput>#120 mod 5#<br /></cfoutput></p>

<hr />

<!--- Comparison --->
<h2>Comparison</h2>
<h3>Standard Notation</h3>
<p>Is 1 eq 1? <cfoutput>#1 eq 1#</cfoutput></p>
<p>Is 15 neq 1? <cfoutput>#15 neq 1#</cfoutput></p>
<p>Is 10 gt 8? <cfoutput>#10 gt 8#</cfoutput></p>
<p>Is 1 lt 2? <cfoutput>#1 lt 2#</cfoutput></p>
<p>Is 10 gte 5? <cfoutput>#10 gte 5#</cfoutput></p>
<p>Is 1 lte 5? <cfoutput>#1 lte 5#</cfoutput></p>

<h3>Alternative Notation</h3>
<p>Is 1 == 1? <cfoutput>#1 eq 1#</cfoutput></p>
<p>Is 15 != 1? <cfoutput>#15 neq 1#</cfoutput></p>
<p>Is 10 > 8? <cfoutput>#10 gt 8#</cfoutput></p>
<p>Is 1 < 2? <cfoutput>#1 lt 2#</cfoutput></p>
<p>Is 10 >= 5? <cfoutput>#10 gte 5#</cfoutput></p>
<p>Is 1 <= 5? <cfoutput>#1 lte 5#</cfoutput></p>

<hr />

<!--- Control Structures --->
<h1>Control Structures</h1>

<cfset myCondition = "Test" />

<p>Condition to test for: "<cfoutput>#myCondition#</cfoutput>"</p>

<cfif myCondition eq "Test">
    <cfoutput>#myCondition#. We're testing.</cfoutput>
<cfelseif myCondition eq "Production">
    <cfoutput>#myCondition#. Proceed Carefully!!!</cfoutput>
<cfelse>
    myCondition is unknown
</cfif>

<hr />

<!--- Loops --->
<h1>Loops</h1>
<h2>For Loop</h2>
<cfloop from="0" to="10" index="i">
	<p>Index equals <cfoutput>#i#</cfoutput></p>
</cfloop>

<h2>For Each Loop (Complex Variables)</h2>

<p>Set <b>myArray3</b> to [5, 15, 99, 45, 100]</p>

<cfset myArray3 = [5, 15, 99, 45, 100] />

<cfloop array="#myArray3#" index="i">
	<p>Index equals <cfoutput>#i#</cfoutput></p>
</cfloop>

<p>Set <b>myArray4</b> to ["Alpha", "Bravo", "Charlie", "Delta", "Echo"]</p>

<cfset myArray4 = ["Alpha", "Bravo", "Charlie", "Delta", "Echo"] />

<cfloop array="#myArray4#" index="s">
	<p>Index equals <cfoutput>#s#</cfoutput></p>
</cfloop>

<h2>Switch Statement</h2>

<p>Set <b>myArray5</b> to [5, 15, 99, 45, 100]</p>

<cfset myArray5 = [5, 15, 99, 45, 100] />

<cfloop array="#myArray5#" index="i">
	<cfswitch expression="#i#">
		<cfcase value="5,15,45" delimiters=",">
			<p><cfoutput>#i#</cfoutput> is a multiple of 5.</p>
		</cfcase>
		<cfcase value="99">
			<p><cfoutput>#i#</cfoutput> is ninety-nine.</p>
		</cfcase>
		<cfdefaultcase>
			<p><cfoutput>#i#</cfoutput> is not 5, 15, 45, or 99.</p>
		</cfdefaultcase> 
	</cfswitch> 
</cfloop>

<hr />

<h1>Converting types</h1>

<style>
	table.table th, table.table td {
		border: 1px solid #000000;
		padding: 2px;
	}
	
	table.table th {
		background-color: #CCCCCC;
	}
</style>

<table class="table" cellspacing="0">
	<thead>
		<tr>
			<th>Value</th>
			<th>As Boolean</th>
			<th>As number</th>
			<th>As date-time</th>
			<th>As string</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<th>"Yes"</th>
			<td>TRUE</td>
			<td>1</td>
			<td>Error</td>
			<td>"Yes"</td>
		</tr>
		<tr>
			<th>"No"</th>
			<td>FALSE</td>
			<td>0</td>
			<td>Error</td>
			<td>"No"</td>
		</tr>
		<tr>
			<th>TRUE</th>
			<td>TRUE</td>
			<td>1</td>
			<td>Error</td>
			<td>"Yes"</td>
		</tr>
		<tr>
			<th>FALSE</th>
			<td>FALSE</td>
			<td>0</td>
			<td>Error</td>
			<td>"No"</td>
		</tr>
		<tr>
			<th>Number</th>
			<td>True if Number is not 0; False otherwise.</td>
			<td>Number</td>
			<td>See &#34;Date-time values&#34; earlier in this chapter.</td>
			<td>String representation of the number (for example, &#34;8&#34;).</td>
		</tr>
		<tr>
			<th>String</th>
			<td>If "Yes", True <br>If "No", False <br>If it can be converted to 0, False <br>If it can be converted to any other number, True</td>
			<td>If it represents a number (for example, &#34;1,000&#34; or &#34;12.36E-12&#34;), it is converted to the corresponding number.</td>
			<td>If it represents a date-time (see next column), it is converted to the numeric value of the corresponding date-time object. <br>If it is an ODBC date, time, or timestamp (for example &#34;{ts &#39;2001-06-14 11:30:13&#39;}&#34;, or if it is expressed in a standard U.S. date or time format, including the use of full or abbreviated month names, it is converted to the corresponding date-time value. <br>Days of the week or unusual punctuation result in an error. <br>Dashes, forward-slashes, and spaces are generally allowed.</td>
			<td>String</td>
		</tr>
		<tr>
			<th>Date</th>
			<td>Error</td>
			<td>The numeric value of the date-time object.</td>
			<td>Date</td>
			<td>An ODBC timestamp.</td>
		</tr>
	</tbody>
</table>

<hr />

<h1>Components</h1>

<em>Code for reference (Functions must return something to support IE)</em>
```
```cfs
<cfcomponent>
	<cfset this.hello = "Hello" />
	<cfset this.world = "world" />

	<cffunction name="sayHello">
		<cfreturn this.hello & ", " & this.world & "!" />
	</cffunction>
	
	<cffunction name="setHello">
		<cfargument name="newHello" type="string" required="true" />
		
		<cfset this.hello = arguments.newHello />
		 
		<cfreturn true />
	</cffunction>
	
	<cffunction name="setWorld">
		<cfargument name="newWorld" type="string" required="true" />
		
		<cfset this.world = arguments.newWorld />
		 
		<cfreturn true />
	</cffunction>
	
	<cffunction name="getHello">
		<cfreturn this.hello />
	</cffunction>
	
	<cffunction name="getWorld">
		<cfreturn this.world />
	</cffunction>
</cfcomponent>

<cfset this.hello = "Hello" />
<cfset this.world = "world" />

<cffunction name="sayHello">
	<cfreturn this.hello & ", " & this.world & "!" />
</cffunction>

<cffunction name="setHello">
	<cfargument name="newHello" type="string" required="true" />
	
	<cfset this.hello = arguments.newHello />
	 
	<cfreturn true />
</cffunction>

<cffunction name="setWorld">
	<cfargument name="newWorld" type="string" required="true" />
	
	<cfset this.world = arguments.newWorld />
	 
	<cfreturn true />
</cffunction>

<cffunction name="getHello">
	<cfreturn this.hello />
</cffunction>

<cffunction name="getWorld">
	<cfreturn this.world />
</cffunction>


<b>sayHello()</b>
<cfoutput><p>#sayHello()#</p></cfoutput>
<b>getHello()</b>
<cfoutput><p>#getHello()#</p></cfoutput>
<b>getWorld()</b>
<cfoutput><p>#getWorld()#</p></cfoutput>
<b>setHello("Hola")</b>
<cfoutput><p>#setHello("Hola")#</p></cfoutput>
<b>setWorld("mundo")</b>
<cfoutput><p>#setWorld("mundo")#</p></cfoutput>
<b>sayHello()</b>
<cfoutput><p>#sayHello()#</p></cfoutput>
<b>getHello()</b>
<cfoutput><p>#getHello()#</p></cfoutput>
<b>getWorld()</b>
<cfoutput><p>#getWorld()#</p></cfoutput>
```

### CFScript
_**C**old**F**usion **S**cript_  
In recent years, the ColdFusion language has added script syntax to mirror tag functionality. When using an up-to-date CF server, almost all functionality is available using scrypt syntax.

## Further Reading

The links provided here below are just to get an understanding of the topic, feel free to Google and find specific examples.

1. [Coldfusion Reference From Adobe](https://helpx.adobe.com/coldfusion/cfml-reference/topics.html)
2. [Open Source Documentation](http://cfdocs.org/)
---

language: "Common Lisp"
filename: commonlisp.lisp
contributors:
  - ["Paul Nathan", "https://github.com/pnathan"]
---

ANSI Common Lisp is a general purpose, multi-paradigm programming
language suited for a wide variety of industry applications. It is
frequently referred to as a programmable programming language.

The classic starting point is [Practical Common Lisp and freely available.](http://www.gigamonkeys.com/book/)

Another popular and recent book is
[Land of Lisp](http://landoflisp.com/).



```common_lisp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 0. Syntax
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; General form.

;; Lisp has two fundamental pieces of syntax: the ATOM and the
;; S-expression. Typically, grouped S-expressions are called `forms`.

10  ; an atom; it evaluates to itself

:THING ;Another atom; evaluating to the symbol :thing.

t  ; another atom, denoting true.

(+ 1 2 3 4) ; an s-expression

'(4 :foo  t)  ;another one


;;; Comments

;; Single line comments start with a semicolon; use two for normal
;; comments, three for section comments, and four for file-level
;; comments.

#| Block comments
   can span multiple lines and...
    #|
       they can be nested!
    |#
|#

;;; Environment.

;; A variety of implementations exist; most are
;; standard-conformant. CLISP is a good starting one.

;; Libraries are managed through Quicklisp.org's Quicklisp system.

;; Common Lisp is usually developed with a text editor and a REPL
;; (Read Evaluate Print Loop) running at the same time. The REPL
;; allows for interactive exploration of the program as it is "live"
;; in the system.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 1. Primitive Datatypes and Operators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Symbols

'foo ; => FOO  Notice that the symbol is upper-cased automatically.

;; Intern manually creates a symbol from a string.

(intern "AAAA") ; => AAAA

(intern "aaa") ; => |aaa|

;;; Numbers
9999999999999999999999 ; integers
#b111                  ; binary => 7
#o111                  ; octal => 73
#x111                  ; hexadecimal => 273
3.14159s0              ; single
3.14159d0              ; double
1/2                    ; ratios
#C(1 2)                ; complex numbers


;; Function application is written (f x y z ...)
;; where f is a function and x, y, z, ... are operands
;; If you want to create a literal list of data, use ' to stop it from
;; being evaluated - literally, "quote" the data.
'(+ 1 2) ; => (+ 1 2)
;; You can also call a function manually:
(funcall #'+ 1 2 3) ; => 6
;; Some arithmetic operations
(+ 1 1)              ; => 2
(- 8 1)              ; => 7
(* 10 2)             ; => 20
(expt 2 3)           ; => 8
(mod 5 2)            ; => 1
(/ 35 5)             ; => 7
(/ 1 3)              ; => 1/3
(+ #C(1 2) #C(6 -4)) ; => #C(7 -2)

                     ;;; Booleans
t                    ; for true (any not-nil value is true)
nil                  ; for false - and the empty list
(not nil)            ; => t
(and 0 t)            ; => t
(or 0 nil)           ; => 0

                     ;;; Characters
#\A                  ; => #\A
#\λ                  ; => #\GREEK_SMALL_LETTER_LAMDA
#\u03BB              ; => #\GREEK_SMALL_LETTER_LAMDA

;;; Strings are fixed-length arrays of characters.
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; backslash is an escaping character

;; Strings can be concatenated too!
(concatenate 'string "Hello " "world!") ; => "Hello world!"

;; A string can be treated like a sequence of characters
(elt "Apple" 0) ; => #\A

;; format can be used to format strings:
(format nil "~a can be ~a" "strings" "formatted")

;; Printing is pretty easy; ~% is the format specifier for newline.
(format t "Common Lisp is groovy. Dude.~%")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; You can create a global (dynamically scoped) using defparameter
;; a variable name can use any character except: ()",'`;#|\

;; Dynamically scoped variables should have earmuffs in their name!

(defparameter *some-var* 5)
*some-var* ; => 5

;; You can also use unicode characters.
(defparameter *AΛB* nil)


;; Accessing a previously unbound variable is an
;; undefined behavior (but possible). Don't do it.


;; Local binding: `me` is bound to "dance with you" only within the
;; (let ...). Let always returns the value of the last `form` in the
;; let form.

(let ((me "dance with you"))
  me)
;; => "dance with you"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Structs and Collections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Structs
(defstruct dog name breed age)
(defparameter *rover*
    (make-dog :name "rover"
              :breed "collie"
              :age 5))
*rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5)

(dog-p *rover*) ; => true  #| -p signifies "predicate". It's used to
                              check if *rover* is an instance of dog. |#
(dog-name *rover*) ; => "rover"

;; Dog-p, make-dog, and dog-name are all created by defstruct!

;;; Pairs
;; `cons' constructs pairs, `car' and `cdr' extract the first
;; and second elements
(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB)
(car (cons 'SUBJECT 'VERB)) ; => SUBJECT
(cdr (cons 'SUBJECT 'VERB)) ; => VERB

;;; Lists

;; Lists are linked-list data structures, made of `cons' pairs and end
;; with a `nil' (or '()) to mark the end of the list
(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3)
;; `list' is a convenience variadic constructor for lists
(list 1 2 3) ; => '(1 2 3)
;; and a quote can also be used for a literal list value
'(1 2 3) ; => '(1 2 3)

;; Can still use `cons' to add an item to the beginning of a list
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; Use `append' to - surprisingly - append lists together
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; Or use concatenate -

(concatenate 'list '(1 2) '(3 4))

;; Lists are a very central type, so there is a wide variety of functionality for
;; them, a few examples:
(mapcar #'1+ '(1 2 3))             ; => '(2 3 4)
(mapcar #'+ '(1 2 3) '(10 20 30))  ; => '(11 22 33)
(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4)
(every #'evenp '(1 2 3 4))         ; => nil
(some #'oddp '(1 2 3 4))           ; => T
(butlast '(subject verb object))   ; => (SUBJECT VERB)


;;; Vectors

;; Vector's literals are fixed-length arrays
#(1 2 3) ; => #(1 2 3)

;; Use concatenate to add vectors together
(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; Arrays

;; Both vectors and strings are special-cases of arrays.

;; 2D arrays

(make-array (list 2 2))

;; (make-array '(2 2)) works as well.

; => #2A((0 0) (0 0))

(make-array (list 2 2 2))

; => #3A(((0 0) (0 0)) ((0 0) (0 0)))

;; Caution- the default initial values are
;; implementation-defined. Here's how to define them:

(make-array '(2) :initial-element 'unset)

; => #(UNSET UNSET)

;; And, to access the element at 1,1,1 -
(aref (make-array (list 2 2 2)) 1 1 1)

; => 0

;;; Adjustable vectors

;; Adjustable vectors have the same printed representation
;; as fixed-length vector's literals.

(defparameter *adjvec* (make-array '(3) :initial-contents '(1 2 3)
      :adjustable t :fill-pointer t))

*adjvec* ; => #(1 2 3)

;; Adding new element:
(vector-push-extend 4 *adjvec*) ; => 3

*adjvec* ; => #(1 2 3 4)



;;; Naively, sets are just lists:

(set-difference '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1)
(intersection '(1 2 3 4) '(4 5 6 7)) ; => 4
(union '(1 2 3 4) '(4 5 6 7))        ; => (3 2 1 4 5 6 7)
(adjoin 4 '(1 2 3 4))     ; => (1 2 3 4)

;; But you'll want to use a better data structure than a linked list
;; for performant work!

;;; Dictionaries are implemented as hash tables.

;; Create a hash table
(defparameter *m* (make-hash-table))

;; set a value
(setf (gethash 'a *m*) 1)

;; Retrieve a value
(gethash 'a *m*) ; => 1, t

;; Detail - Common Lisp has multiple return values possible. gethash
;; returns t in the second value if anything was found, and nil if
;; not.

;; Retrieving a non-present value returns nil
 (gethash 'd *m*) ;=> nil, nil

;; You can provide a default value for missing keys
(gethash 'd *m* :not-found) ; => :NOT-FOUND

;; Let's handle the multiple return values here in code.

(multiple-value-bind
      (a b)
    (gethash 'd *m*)
  (list a b))
; => (NIL NIL)

(multiple-value-bind
      (a b)
    (gethash 'a *m*)
  (list a b))
; => (1 T)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use `lambda' to create anonymous functions.
;; A function always returns the value of its last expression.
;; The exact printable representation of a function will vary...

(lambda () "Hello World") ; => #<FUNCTION (LAMBDA ()) {1004E7818B}>

;; Use funcall to call lambda functions
(funcall (lambda () "Hello World")) ; => "Hello World"

;; Or Apply
(apply (lambda () "Hello World") nil) ; => "Hello World"

;; De-anonymize the function
(defun hello-world ()
   "Hello World")
(hello-world) ; => "Hello World"

;; The () in the above is the list of arguments for the function
(defun hello (name)
   (format nil "Hello, ~a" name))

(hello "Steve") ; => "Hello, Steve"

;; Functions can have optional arguments; they default to nil

(defun hello (name &optional from)
    (if from
        (format t "Hello, ~a, from ~a" name from)
        (format t "Hello, ~a" name)))

 (hello "Jim" "Alpacas") ;; => Hello, Jim, from Alpacas

;; And the defaults can be set...
(defun hello (name &optional (from "The world"))
   (format t "Hello, ~a, from ~a" name from))

(hello "Steve")
; => Hello, Steve, from The world

(hello "Steve" "the alpacas")
; => Hello, Steve, from the alpacas


;; And of course, keywords are allowed as well... usually more
;;   flexible than &optional.

(defun generalized-greeter (name &key (from "the world") (honorific "Mx"))
    (format t "Hello, ~a ~a, from ~a" honorific name from))

(generalized-greeter "Jim")   ; => Hello, Mx Jim, from the world

(generalized-greeter "Jim" :from "the alpacas you met last summer" :honorific "Mr")
; => Hello, Mr Jim, from the alpacas you met last summer

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Equality
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Common Lisp has a sophisticated equality system. A couple are covered here.

;; for numbers use `='
(= 3 3.0) ; => t
(= 2 1) ; => nil

;; for object identity (approximately) use `eql`
(eql 3 3) ; => t
(eql 3 3.0) ; => nil
(eql (list 3) (list 3)) ; => nil

;; for lists, strings, and bit-vectors use `equal'
(equal (list 'a 'b) (list 'a 'b)) ; => t
(equal (list 'a 'b) (list 'b 'a)) ; => nil

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Control Flow
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Conditionals

(if t                ; test expression
    "this is true"   ; then expression
    "this is false") ; else expression
; => "this is true"

;; In conditionals, all non-nil values are treated as true
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'YEP

;; `cond' chains a series of tests to select a result
(cond ((> 2 2) (error "wrong!"))
      ((< 2 2) (error "wrong again!"))
      (t 'ok)) ; => 'OK

;; Typecase switches on the type of the value
(typecase 1
  (string :string)
  (integer :int))

; => :int

;;; Iteration

;; Of course recursion is supported:

(defun walker (n)
  (if (zerop n)
      :walked
      (walker (- n 1))))

(walker 5) ; => :walked

;; Most of the time, we use DOLIST or LOOP


(dolist (i '(1 2 3 4))
  (format t "~a" i))

; => 1234

(loop for i from 0 below 10
      collect i)

; => (0 1 2 3 4 5 6 7 8 9)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Mutation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use `setf' to assign a new value to an existing variable. This was
;; demonstrated earlier in the hash table example.

(let ((variable 10))
    (setf variable 2))
 ; => 2


;; Good Lisp style is to minimize destructive functions and to avoid
;; mutation when reasonable.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Classes and Objects
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; No more Animal classes, let's have Human-Powered Mechanical
;; Conveyances.

(defclass human-powered-conveyance ()
  ((velocity
    :accessor velocity
    :initarg :velocity)
   (average-efficiency
    :accessor average-efficiency
   :initarg :average-efficiency))
  (:documentation "A human powered conveyance"))

;; defclass, followed by name, followed by the superclass list,
;; followed by slot list, followed by optional qualities such as
;; :documentation.

;; When no superclass list is set, the empty list defaults to the
;; standard-object class. This *can* be changed, but not until you
;; know what you're doing. Look up the Art of the Metaobject Protocol
;; for more information.

(defclass bicycle (human-powered-conveyance)
  ((wheel-size
    :accessor wheel-size
    :initarg :wheel-size
    :documentation "Diameter of the wheel.")
   (height
    :accessor height
    :initarg :height)))

(defclass recumbent (bicycle)
  ((chain-type
    :accessor chain-type
    :initarg  :chain-type)))

(defclass unicycle (human-powered-conveyance) nil)

(defclass canoe (human-powered-conveyance)
  ((number-of-rowers
    :accessor number-of-rowers
    :initarg :number-of-rowers)))


;; Calling DESCRIBE on the human-powered-conveyance class in the REPL gives:

(describe 'human-powered-conveyance)

; COMMON-LISP-USER::HUMAN-POWERED-CONVEYANCE
;  [symbol]
;
; HUMAN-POWERED-CONVEYANCE names the standard-class #<STANDARD-CLASS
;                                                    HUMAN-POWERED-CONVEYANCE>:
;  Documentation:
;    A human powered conveyance
;  Direct superclasses: STANDARD-OBJECT
;  Direct subclasses: UNICYCLE, BICYCLE, CANOE
;  Not yet finalized.
;  Direct slots:
;    VELOCITY
;      Readers: VELOCITY
;      Writers: (SETF VELOCITY)
;    AVERAGE-EFFICIENCY
;      Readers: AVERAGE-EFFICIENCY
;      Writers: (SETF AVERAGE-EFFICIENCY)

;; Note the reflective behavior available to you! Common Lisp is
;; designed to be an interactive system

;; To define a method, let's find out what our circumference of the
;; bike wheel turns out to be using the equation: C = d * pi

(defmethod circumference ((object bicycle))
  (* pi (wheel-size object)))

;; pi is defined in Lisp already for us!

;; Let's suppose we find out that the efficiency value of the number
;; of rowers in a canoe is roughly logarithmic. This should probably be set
;; in the constructor/initializer.

;; Here's how to initialize your instance after Common Lisp gets done
;; constructing it:

(defmethod initialize-instance :after ((object canoe) &rest args)
  (setf (average-efficiency object)  (log (1+ (number-of-rowers object)))))

;; Then to construct an instance and check the average efficiency...

(average-efficiency (make-instance 'canoe :number-of-rowers 15))
; => 2.7725887




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Macros let you extend the syntax of the language

;; Common Lisp doesn't come with a WHILE loop- let's add one.
;; If we obey our assembler instincts, we wind up with:

(defmacro while (condition &body body)
    "While `condition` is true, `body` is executed.

`condition` is tested prior to each execution of `body`"
    (let ((block-name (gensym)) (done (gensym)))
        `(tagbody
           ,block-name
           (unless ,condition
               (go ,done))
           (progn
           ,@body)
           (go ,block-name)
           ,done)))

;; Let's look at the high-level version of this:


(defmacro while (condition &body body)
    "While `condition` is true, `body` is executed.

`condition` is tested prior to each execution of `body`"
  `(loop while ,condition
         do
         (progn
            ,@body)))

;; However, with a modern compiler, this is not required; the LOOP
;; form compiles equally well and is easier to read.

;; Note that ``` is used, as well as `,` and `@`. ``` is a quote-type operator
;; known as quasiquote; it allows the use of `,` . `,` allows "unquoting"
;; variables. @ interpolates lists.

;; Gensym creates a unique symbol guaranteed to not exist elsewhere in
;; the system. This is because macros are expanded at compile time and
;; variables declared in the macro can collide with variables used in
;; regular code.

;; See Practical Common Lisp for more information on macros.
```


## Further Reading

*   [Keep moving on to the Practical Common Lisp book.](http://www.gigamonkeys.com/book/)
*   [A Gentle Introduction to...](https://www.cs.cmu.edu/~dst/LispBook/book.pdf)


## Extra Info

*   [CLiki](http://www.cliki.net/)
*   [common-lisp.net](https://common-lisp.net/)
*   [Awesome Common Lisp](https://github.com/CodyReichert/awesome-cl)

## Credits.

Lots of thanks to the Scheme people for rolling up a great starting
point which could be easily moved to Common Lisp.

- [Paul Khuong](https://github.com/pkhuong) for some great reviewing.
---
category: tool
tool: compojure
contributors:
    - ["Adam Bard", "http://adambard.com/"]
filename: learncompojure.clj
---

## Getting Started with Compojure

Compojure is a DSL for *quickly* creating *performant* web applications
in Clojure with minimal effort:

```clojure
(ns myapp.core
  (:require [compojure.core :refer :all]
            [org.httpkit.server :refer [run-server]])) ; httpkit is a server

(defroutes myapp
  (GET "/" [] "Hello World"))

(defn -main []
  (run-server myapp {:port 5000}))
```

**Step 1:** Create a project with [Leiningen](http://leiningen.org/):

```
lein new myapp
```

**Step 2:** Put the above code in `src/myapp/core.clj`

**Step 3:** Add some dependencies to `project.clj`:

```
[compojure "1.1.8"]
[http-kit "2.1.16"]
```

**Step 4:** Run:

```
lein run -m myapp.core
```

View at: <http://localhost:5000/>

Compojure apps will run on any ring-compatible server, but we recommend
[http-kit](http://http-kit.org/) for its performance and
[massive concurrency](http://http-kit.org/600k-concurrent-connection-http-kit.html).

### Routes

In compojure, each route is an HTTP method paired with a URL-matching pattern,
an argument list, and a body.

```clojure
(defroutes myapp
  (GET "/" [] "Show something")
  (POST "/" [] "Create something")
  (PUT "/" [] "Replace something")
  (PATCH "/" [] "Modify Something")
  (DELETE "/" [] "Annihilate something")
  (OPTIONS "/" [] "Appease something")
  (HEAD "/" [] "Preview something"))
```

Compojure route definitions are just functions which
[accept request maps and return response maps](https://github.com/mmcgrana/ring/blob/master/SPEC):

```clojure
(myapp {:uri "/" :request-method :post})
; => {:status 200
;     :headers {"Content-Type" "text/html; charset=utf-8}
;     :body "Create Something"}
```

The body may be a function, which must accept the request as a parameter:

```clojure
(defroutes myapp
  (GET "/" [] (fn [req] "Do something with req")))
```

Or, you can just use the request directly:

```clojure
(defroutes myapp
  (GET "/" req "Do something with req"))
```

Route patterns may include named parameters:

```clojure
(defroutes myapp
  (GET "/hello/:name" [name] (str "Hello " name)))
```

You can adjust what each parameter matches by supplying a regex:

```clojure
(defroutes myapp
  (GET ["/file/:name.:ext" :name #".*", :ext #".*"] [name ext]
    (str "File: " name ext)))
```

### Middleware

Clojure uses [Ring](https://github.com/ring-clojure/ring) for routing.
Handlers are just functions that accept a request map and return a
response map (Compojure will turn strings into 200 responses for you).

You can easily write middleware that wraps all or part of your
application to modify requests or responses:

```clojure
(defroutes myapp
  (GET "/" req (str "Hello World v" (:app-version req))))

(defn wrap-version [handler]
  (fn [request]
    (handler (assoc request :app-version "1.0.1"))))

(defn -main []
  (run-server (wrap-version myapp) {:port 5000}))
```

[Ring-Defaults](https://github.com/ring-clojure/ring-defaults) provides some handy
middlewares for sites and apis, so add it to your dependencies:

```
[ring/ring-defaults "0.1.1"]
```

Then, you can import it in your ns:

```
(ns myapp.core
  (:require [compojure.core :refer :all]
            [ring.middleware.defaults :refer :all]
            [org.httpkit.server :refer [run-server]]))
```

And use `wrap-defaults` to add the `site-defaults` middleware to your
app:

```
(defn -main []
  (run-server (wrap-defaults myapp site-defaults) {:port 5000}))
```

Now, your handlers may utilize query parameters:

```clojure
(defroutes myapp
  (GET "/posts" req
    (let [title (get (:params req) :title)
          author (get (:params req) :author)]
      (str "Title: " title ", Author: " author))))
```

Or, for POST and PUT requests, form parameters as well

```clojure
(defroutes myapp
  (POST "/posts" req
    (let [title (get (:params req) :title)
          author (get (:params req) :author)]
      (str "Title: " title ", Author: " author))))
```


### Return values

The return value of a route block determines the response body
passed on to the HTTP client, or at least the next middleware in the
ring stack. Most commonly, this is a string, as in the above examples.
But, you may also return a [response map](https://github.com/mmcgrana/ring/blob/master/SPEC):

```clojure
(defroutes myapp
  (GET "/" []
    {:status 200 :body "Hello World"})
  (GET "/is-403" []
    {:status 403 :body ""})
  (GET "/is-json" []
    {:status 200 :headers {"Content-Type" "application/json"} :body "{}"}))
```

### Static Files

To serve up static files, use `compojure.route.resources`.
Resources will be served from your project's `resources/` folder.

```clojure
(require '[compojure.route :as route])

(defroutes myapp
  (GET "/")
  (route/resources "/")) ; Serve static resources at the root path

(myapp {:uri "/js/script.js" :request-method :get})
; => Contents of resources/public/js/script.js
```

### Views / Templates

To use templating with Compojure, you'll need a template library. Here are a few:

#### [Stencil](https://github.com/davidsantiago/stencil)

[Stencil](https://github.com/davidsantiago/stencil) is a [Mustache](http://mustache.github.com/) template library:

```clojure
(require '[stencil.core :refer [render-string]])

(defroutes myapp
  (GET "/hello/:name" [name]
    (render-string "Hello {{name}}" {:name name})))
```

You can easily read in templates from your resources directory. Here's a helper function

```clojure
(require 'clojure.java.io)

(defn read-template [filename]
  (slurp (clojure.java.io/resource filename)))

(defroutes myapp
  (GET "/hello/:name" [name]
    (render-string (read-template "templates/hello.html") {:name name})))
```

#### [Selmer](https://github.com/yogthos/Selmer)

[Selmer](https://github.com/yogthos/Selmer) is a Django and Jinja2-inspired templating language:

```clojure
(require '[selmer.parser :refer [render-file]])

(defroutes myapp
  (GET "/hello/:name" [name]
    (render-file "templates/hello.html" {:name name})))
```

#### [Hiccup](https://github.com/weavejester/hiccup)

[Hiccup](https://github.com/weavejester/hiccup) is a library for representing HTML as Clojure code

```clojure
(require '[hiccup.core :as hiccup])

(defroutes myapp
  (GET "/hello/:name" [name]
    (hiccup/html
      [:html
        [:body
          [:h1 {:class "title"}
            (str "Hello " name)]]])))
```

#### [Markdown](https://github.com/yogthos/markdown-clj)

[Markdown-clj](https://github.com/yogthos/markdown-clj) is a Markdown implementation.

```clojure
(require '[markdown.core :refer [md-to-html-string]])

(defroutes myapp
  (GET "/hello/:name" [name]
    (md-to-html-string "## Hello, world")))
```

Further reading:

* [Official Compojure Documentation](https://github.com/weavejester/compojure/wiki)

* [Clojure for the Brave and True](http://www.braveclojure.com/)
# Contributing

All contributions are welcome, from the tiniest typo to a brand new article.
Translations in all languages are welcome (or, for that matter, original
articles in any language). Send a pull request or open an issue any time of day
or night.

**Please prepend the tag `[language/lang-code]` to your issues and pull
requests.** For example, `[python/en]` for English Python. This will help
everyone pick out things they care about.

We're happy for any contribution in any form, but if you're making more than one
major change (i.e. translations for two different languages) it would be super
cool of you to make a separate pull request for each one so that someone can
review them more effectively and/or individually.

## Style Guidelines

- **Keep lines of under 80 chars**
  + Try to keep **line length in code blocks to 80 characters or fewer**.
  + Otherwise, the text will overflow and look odd.
- **Prefer example to exposition**
  + Try to use as few words as possible.
  + Code examples are preferred over exposition in all cases.
- **Eschew surplusage**
  + We welcome newcomers, but the target audience for this site is programmers
    with some experience.
  + Try to avoid explaining basic concepts except for those specific to the
    language in question.
  + Keep articles succinct and scannable. We all know how to use Google here.
- **Use UTF-8**
  + For translations (or EN articles with non-ASCII characters) please make sure
    your file is UTF-8 encoded.
  + Try to leave out the byte-order-mark at the start of the file. (`:set nobomb`
    in Vim)
  + You can check if the file contains a BOM on Linux/Unix systems by running
    `file language.html.markdown`  You will see this if it uses a BOM:
	`UTF-8 Unicode (with BOM) text`.


### Header configuration

The actual site uses Middleman to generate HTML files from these Markdown ones.
Middleman, or at least the custom scripts underpinning the site, requires that
some key information be defined in the header.

The following fields are necessary for English articles about programming
languages:

- **language** The *programming language* in question
- **contributors** A list of [author, URL] lists to credit

Other fields:

- **filename**: The filename for this article's code. It will be fetched, mashed
  together, and made downloadable.
    + For non-English articles, *filename* should   have a language-specific 
      suffix.
- **lang**: For translations, the human language this article is in. For
  categorization, mostly.

Here's an example header for an Esperanto translation of Ruby:

```yaml
---
language: ruby
filename: learnruby-epo.ruby
contributors:
    - ["Doktor Esperanto", "http://example.com/"]
    - ["Someone else", "http://someoneelseswebsite.com/"]
lang: ep-ep
---
```
---
language: crystal
filename: learncrystal.cr
contributors:
    - ["Vitalii Elenhaupt", "http://veelenga.com"]
    - ["Arnaud Fernandés", "https://github.com/TechMagister/"]

---

```crystal

# This is a comment

# Everything is an object
nil.class  #=> Nil
100.class  #=> Int32
true.class #=> Bool

# Falsey values are: nil, false and null pointers
!nil   #=> true  : Bool
!false #=> true  : Bool
!0     #=> false : Bool

# Integers

1.class #=> Int32

# Four signed integer types
1_i8.class  #=> Int8
1_i16.class #=> Int16
1_i32.class #=> Int32
1_i64.class #=> Int64

# Four unsigned integer types
1_u8.class  #=> UInt8
1_u16.class #=> UInt16
1_u32.class #=> UInt32
1_u64.class #=> UInt64

2147483648.class          #=> Int64
9223372036854775808.class #=> UInt64

# Binary numbers
0b1101 #=> 13 : Int32

# Octal numbers
0o123 #=> 83 : Int32

# Hexadecimal numbers
0xFE012D #=> 16646445 : Int32
0xfe012d #=> 16646445 : Int32

# Floats

1.0.class #=> Float64

# There are two floating point types
1.0_f32.class #=> Float32
1_f32.class   #=> Float32

1e10.class    #=> Float64
1.5e10.class  #=> Float64
1.5e-7.class  #=> Float64

# Chars

'a'.class #=> Char

# Octal codepoint
'\101' #=> 'A' : Char

# Unicode codepoint
'\u0041' #=> 'A' : Char

# Strings

"s".class #=> String

# Strings are immutable
s = "hello, "  #=> "hello, "        : String
s.object_id    #=> 134667712        : UInt64
s += "Crystal" #=> "hello, Crystal" : String
s.object_id    #=> 142528472        : UInt64

# Supports interpolation
"sum = #{1 + 2}" #=> "sum = 3" : String

# Multiline string
"This is
   multiline string"

# String with double quotes
%(hello "world") #=> "hello \"world\""

# Symbols
# Immutable, reusable constants represented internally as Int32 integer value.
# They're often used instead of strings to efficiently convey specific,
# meaningful values

:symbol.class #=> Symbol

sentence = :question?     # :"question?" : Symbol

sentence == :question?    #=> true  : Bool
sentence == :exclamation! #=> false : Bool
sentence == "question?"   #=> false : Bool

# Arrays

[1, 2, 3].class         #=> Array(Int32)
[1, "hello", 'x'].class #=> Array(Int32 | String | Char)

# Empty arrays should specify a type
[]               # Syntax error: for empty arrays use '[] of ElementType'
[] of Int32      #=> [] : Array(Int32)
Array(Int32).new #=> [] : Array(Int32)

# Arrays can be indexed
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] : Array(Int32)
array[0]                #=> 1               : Int32
array[10]               # raises IndexError
array[-6]               # raises IndexError
array[10]?              #=> nil             : (Int32 | Nil)
array[-6]?              #=> nil             : (Int32 | Nil)

# From the end
array[-1] #=> 5

# With a start index and size
array[2, 3] #=> [3, 4, 5]

# Or with range
array[1..3] #=> [2, 3, 4]

# Add to an array
array << 6  #=> [1, 2, 3, 4, 5, 6]

# Remove from the end of the array
array.pop #=> 6
array     #=> [1, 2, 3, 4, 5]

# Remove from the beginning of the array
array.shift #=> 1
array       #=> [2, 3, 4, 5]

# Check if an item exists in an array
array.includes? 3 #=> true

# Special syntax for an array of string and an array of symbols
%w(one two three) #=> ["one", "two", "three"] : Array(String)
%i(one two three) #=> [:one, :two, :three]    : Array(Symbol)

# There is a special array syntax with other types too, as long as
# they define a .new and a #<< method
set = Set{1, 2, 3} #=> [1, 2, 3]
set.class          #=> Set(Int32)

# The above is equivalent to
set = Set(typeof(1, 2, 3)).new
set << 1
set << 2
set << 3

# Hashes

{1 => 2, 3 => 4}.class   #=> Hash(Int32, Int32)
{1 => 2, 'a' => 3}.class #=> Hash(Int32 | Char, Int32)

# Empty hashes should specify a type
{}                     # Syntax error
{} of Int32 => Int32   # {}
Hash(Int32, Int32).new # {}

# Hashes can be quickly looked up by key
hash = {"color" => "green", "number" => 5}
hash["color"]        #=> "green"
hash["no_such_key"]  #=> Missing hash key: "no_such_key" (KeyError)
hash["no_such_key"]? #=> nil

# Check existence of keys hash
hash.has_key? "color" #=> true

# Special notation for symbol and string keys
{key1: 'a', key2: 'b'}     # {:key1 => 'a', :key2 => 'b'}
{"key1": 'a', "key2": 'b'} # {"key1" => 'a', "key2" => 'b'}

# Special hash literal syntax with other types too, as long as
# they define a .new and a #[]= methods
class MyType
  def []=(key, value)
    puts "do stuff"
  end
end

MyType{"foo" => "bar"}

# The above is equivalent to
tmp = MyType.new
tmp["foo"] = "bar"
tmp

# Ranges

1..10                  #=> Range(Int32, Int32)
Range.new(1, 10).class #=> Range(Int32, Int32)

# Can be inclusive or exclusive
(3..5).to_a  #=> [3, 4, 5]
(3...5).to_a #=> [3, 4]

# Check whether range includes the given value or not
(1..8).includes? 2 #=> true

# Tuples are a fixed-size, immutable, stack-allocated sequence of values of
# possibly different types.
{1, "hello", 'x'}.class #=> Tuple(Int32, String, Char)

# Access tuple's value by its index
tuple = {:key1, :key2}
tuple[1] #=> :key2
tuple[2] #=> syntax error : Index out of bound

# Can be expanded into multiple variables
a, b, c = {:a, 'b', "c"}
a #=> :a
b #=> 'b'
c #=> "c"

# Procs represent a function pointer with an optional context (the closure data)
# It is typically created with a proc litteral
proc = ->(x : Int32) { x.to_s }
proc.class # Proc(Int32, String)
# Or using the new method
Proc(Int32, String).new { |x| x.to_s }

# Invoke proc with call method
proc.call 10 #=> "10"

# Control statements

if true
  "if statement"
elsif false
  "else-if, optional"
else
  "else, also optional"
end

puts "if as a suffix" if true

# If as an expression
a = if 2 > 1
      3
    else
      4
    end

a #=> 3

# Ternary if
a = 1 > 2 ? 3 : 4 #=> 4

# Case statement
cmd = "move"

action = case cmd
  when "create"
    "Creating..."
  when "copy"
    "Copying..."
  when "move"
    "Moving..."
  when "delete"
    "Deleting..."
end

action #=> "Moving..."

# Loops
index = 0
while index <= 3
  puts "Index: #{index}"
  index += 1
end
# Index: 0
# Index: 1
# Index: 2
# Index: 3

index = 0
until index > 3
  puts "Index: #{index}"
  index += 1
end
# Index: 0
# Index: 1
# Index: 2
# Index: 3

# But the preferable way is to use each
(1..3).each do |index|
  puts "Index: #{index}"
end
# Index: 0
# Index: 1
# Index: 2
# Index: 3

# Variable's type depends on the type of the expression
# in control statements
if a < 3
  a = "hello"
else
  a = true
end
typeof a #=> (Bool | String)

if a && b
  # here both a and b are guaranteed not to be Nil
end

if a.is_a? String
  a.class #=> String
end

# Functions

def double(x)
  x * 2
end

# Functions (and all blocks) implicitly return the value of the last statement
double(2) #=> 4

# Parentheses are optional where the call is unambiguous
double 3 #=> 6

double double 3 #=> 12

def sum(x, y)
  x + y
end

# Method arguments are separated by a comma
sum 3, 4 #=> 7

sum sum(3, 4), 5 #=> 12

# yield
# All methods have an implicit, optional block parameter
# it can be called with the 'yield' keyword

def surround
  puts '{'
  yield
  puts '}'
end

surround { puts "hello world" }

# {
# hello world
# }


# You can pass a block to a function
# "&" marks a reference to a passed block
def guests(&block)
  block.call "some_argument"
end

# You can pass a list of arguments, which will be converted into an array
# That's what splat operator ("*") is for
def guests(*array)
  array.each { |guest| puts guest }
end

# If a method returns an array, you can use destructuring assignment
def foods
    ["pancake", "sandwich", "quesadilla"]
end
breakfast, lunch, dinner = foods
breakfast #=> "pancake"
dinner    #=> "quesadilla"

# By convention, all methods that return booleans end with a question mark
5.even? # false
5.odd?  # true

# And if a method ends with an exclamation mark, it does something destructive
# like mutate the receiver. Some methods have a ! version to make a change, and
# a non-! version to just return a new changed version
company_name = "Dunder Mifflin"
company_name.gsub "Dunder", "Donald"  #=> "Donald Mifflin"
company_name  #=> "Dunder Mifflin"
company_name.gsub! "Dunder", "Donald"
company_name  #=> "Donald Mifflin"


# Define a class with the class keyword
class Human

  # A class variable. It is shared by all instances of this class.
  @@species = "H. sapiens"

  # type of name is String
  @name : String

  # Basic initializer
  # Assign the argument to the "name" instance variable for the instance
  # If no age given, we will fall back to the default in the arguments list.
  def initialize(@name, @age = 0)
  end

  # Basic setter method
  def name=(name)
    @name = name
  end

  # Basic getter method
  def name
    @name
  end

  # The above functionality can be encapsulated using the attr_accessor method as follows
  property :name

  # Getter/setter methods can also be created individually like this
  getter :name
  setter :name

  # A class method uses self to distinguish from instance methods.
  # It can only be called on the class, not an instance.
  def self.say(msg)
    puts msg
  end

  def species
    @@species
  end
end


# Instantiate a class
jim = Human.new("Jim Halpert")

dwight = Human.new("Dwight K. Schrute")

# Let's call a couple of methods
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# Call the class method
Human.say("Hi") #=> print Hi and returns nil

# Variables that start with @ have instance scope
class TestClass
  @var = "I'm an instance var"
end

# Variables that start with @@ have class scope
class TestClass
  @@var = "I'm a class var"
end
# Variables that start with a capital letter are constants
Var = "I'm a constant"
Var = "can't be updated" # Already initialized constant Var

# Class is also an object in crystal. So class can have instance variables.
# Class variable is shared among the class and all of its descendants.

# base class
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

# derived class
class Worker < Human
end

Human.foo   #=> 0
Worker.foo  #=> 0

Human.foo = 2 #=> 2
Worker.foo    #=> 0

Worker.foo = 3 #=> 3
Human.foo   #=> 2
Worker.foo  #=> 3

module ModuleExample
  def foo
    "foo"
  end
end

# Including modules binds their methods to the class instances
# Extending modules binds their methods to the class itself

class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => undefined method 'foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => undefined method 'foo' for Book


# Exception handling

# Define new exception
class MyException < Exception
end

# Define another exception
class MyAnotherException < Exception; end

ex = begin
   raise MyException.new
rescue ex1 : IndexError
  "ex1"
rescue ex2 : MyException | MyAnotherException
  "ex2"
rescue ex3 : Exception
  "ex3"
rescue ex4 # catch any kind of exception
  "ex4"
end

ex #=> "ex2"

```

## Additional resources

- [Official Documentation](http://crystal-lang.org/)
---
language: brainfuck
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Vojta Svoboda", "https://github.com/vojtasvoboda/"]
filename: learnbrainfuck-cz.bf
lang: cs-cz
---

Brainfuck (psaný bez kapitálek s vyjímkou začátku věty) je extrémně minimální 
Turingovsky kompletní (ekvivalentní) programovací jazyk a má pouze 8 příkazů.

Můžete si ho vyzkoušet přímo v prohlížeči s [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/).

```
Jakýkoliv znak mimo "><+-.,[]" (bez uvozovek) je ignorován.

Brainfuck je reprezentován jako pole, které má 30.000 buněk s počátkem v nule 
a datovým ukazatelem na aktuální buňce.

Můžeme využít těchto osm příkazů:
+ : Přičte k aktuální buňce jedničku.
- : Odečte od aktuální buňky jedničku.
> : Posune datový ukazatel na další buňku, která je napravo.
< : Posune datový ukazatel na předchozí buňku, která je nalevo.
. : Vytiskne ASCII hodnotu aktuální buňky (například 65 = 'A').
, : Načte jeden znak do aktuální buňky.
[ : Pokud je hodnota aktuální buňky nulová, přeskočí na buňku odpovídající ] .
    Jinak skočí na další instrukci.
] : Pokud je hodnota aktuální buňky nulova, přeskočí na další instrukci.
    Jinak skočí zpět na instrukci odpovídající [ .

[ a ] tak tvoří 'while' smyčku a tyto symboly musí tak být v páru.

Pojďme se mrknout na některé brainfuck programy.

++++++ [ > ++++++++++ < - ] > +++++ .

Tento program vypíše písmeno 'A' (v ASCII je to číslo 65). Nejdříve navýší 
buňku #1 na hodnotu 6. Buňka #1 bude použita pro smyčku. Potom program vstoupí 
do smyčky ([) a sníží hodnotu buňky #1 o jedničku. Ve smyčce zvýší hodnotu 
buňky #2 desetkrát, vrátí ze zpět na buňku #1 a sníží její hodnotu o jedničku.
Toto se stane šestkrát (je potřeba šestkrát snížit hodnotu buňky #1, aby byla 
nulová a program přeskočil na konec cyklu označený znakem ].

Na konci smyčky, kdy jsme na buňce #1 (která má hodnotu 0), tak má buňka #2 
hodnotu 60. Přesuneme se na buňku #2 a pětkrát zvýšíme její hodnotu o jedničku 
na hodnotu 65. Na konci vypíšeme hodnotu buňky #2 - 65, což je v ASCII znak 'A' 
na terminálu.


, [ > + < - ] > .

Tento program přečte znak z uživatelského vstupu a zkopíruje ho do buňky #1.
Poté začne smyčka - přesun na buňku #2, zvýšení hodnoty buňky #2 o jedničku,
přesun zpět na buňku #1 a snížení její hodnoty o jedničku. Takto smyčka pokračuje
do té doby, než je buňka #1 nulová a buňka #2 nabyde původní hodnotu buňky #1. 
Protože jsme na buňce #1, přesuneme se na buňku #2 a vytiskneme její hodnotu 
v ASCII.

Je dobré vědět, že mezery jsou v programu uvedené pouze z důvodu čitelnosti. 
Program je možné klidně zapsat i takto:

,[>+<-]>.


Nyní se podívejte na tento program a zkuste zjistit co dělá: 

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Tento program vezme dvě čísla ze vstupu a vynásobí je.

Program nejdříve načte dvě vstupní hodnoty. Poté začíná smyčka řízená hodnotou
v buňce #1 - přesun na buňku #2 a start druhé vnořené smyčky, která je řízená 
hodnotou v buňce #2 a zvyšuje hodnotu v buňce #3. Nicméně je zde problém 
kdy na konci vnitřní smyčky je v buňce #2 nula a smyčka by tak znovu 
napokračovala. Vyřešíme to tak, že zvyšujeme o jedničku i buňku #4 a její 
hodnotu poté překopírujeme do buňky #2. Na konci programu je v buňce #3 
výsledek.
```

A to je brainbuck. Zase tak složitý není, co? Zkuste si nyní napsat nějaký
vlastní brainfuck program a nebo interpretr v jiném jazyce, což není zase
tak složité, ale pokud jste opravdový masochista, zkuste si naprogramovat
interpretr jazyka brainfuck v jazyce... brainfuck :)
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
    - ["Geoffrey Liu", "https://github.com/g-liu"]
    - ["Connor Shea", "https://github.com/connorshea"]
    - ["Deepanshu Utkarsh", "https://github.com/duci9y"]
translators:
    - ["Michal Martinek", "https://github.com/MichalMartinek"]
lang: cs-cz
filename: learncss-cz.css
---

V ranných dobách webu se nevyskytovaly žádné vizuální elementy, pouze čistý text, ale s vývojem webových browserů se staly stránky plné grafických prvků běžné.

A právě proto vzniklo CSS, aby oddělilo obsah (HTML) od vzhledu webové stránky.

Pomocí CSS můžete označit různé elementy na HTML stránce a přiřadit jim různé vzhledové vlastnosti.

Tento návod byl napsán pro CSS 2, avšak CSS 3 se stalo velmi oblíbené a v dnešní době už běžné.

**POZNÁMKA** Protože CSS produkuje vizuální výsledky, je nutné k jeho naučení všechno zkoušet třeba na [dabbletu](http://dabblet.com/).
Tento článek se zaměřuje hlavně na syntaxi a poskytue také pár obecných tipů.

```css
/* komentáře jsou ohraničeny lomítkem s hvězdičkou, přesně jako tyto dva
   řádky, v CSS není nic jako jednořádkový komentář, pouze tenhle zápis   */

/* ################
   ## SELEKTORY
   ################ */

/* Selektor se používá pro vybrání elementu na stránce:
selektor { vlastnost: hodnota; /* více vlastností...  }*/

/*
Toto je náš element:
<div trida='trida1 trida2' id='nejakeID' attr='hodnota' otherAttr='cs-cz co neco' />
*/

/* Můžeme vybrat tento element třeba podle jeho třídy */
.trida1 { }

/* nebo obou tříd! */
.trida1.trida2 { }

/* nebo jeho jména */
div { }

/* nebo jeho id */
#nejakeID { }

/* nebo podle toho, že má atribut! */
[attr] { font-size:smaller; }

/* nebo že argument nabývá specifické hodnoty*/
[attr='hodnota'] { font-size:smaller; }

/* začíná nějakou hodnotou (CSS 3) */
[attr^='ho'] { font-size:smaller; }

/* nebo končí něčím (CSS 3) */
[attr$='ta'] { font-size:smaller; }

/* nebo obsahuje nějakou hodnotu, která je v atributu oddělená mezerami */
[otherAttr~='co'] { }
[otherAttr~='neco'] { }

/* nebo obsahuje hodnotu oddělenou pomlčkou - "-" (U+002D) */
[otherAttr|='cs'] { font-size:smaller; }


/* Můžeme spojit různé selektory, abychom získali specifičtější selektor.
   Pozor, nedávejte mezi ně mezery! */
div.nejaka-trida[attr$='ta'] { }

/* Můžeme vybrat element, který je potomek jineho */
div.vnejsi-element > .jmeno-tridy { }

/* nebo zanořen ještě hlouběji. Potomci jsou přímo pod vnější třídou, pouze 1
   úroveň pod rodičem. Tento selektor bude fungovat na jakékoliv úrovni pod
   rodičem */
div.rodic .jmeno-tridy { }

/* Varování: stejný selektor bez mezery má úplně jiný význam
   Vzpomínáte si jaký? */
div.rodic.jmeno-tridy { }

/* Možná budete chtít vybrat element, který leží přímo vedle */
.jsem-primo-pred + .timto-elementem { }

/* nebo kdekoliv na stejné úrovni stromu */
.jsem-kdekoliv-pred ~ .timto-elementem { }

/* Existují selektory nazvané pseudo třídy, kterými můžeme vybrat elementy,
   když jsou v určitém stavu */

/* na příklad, když kurzor najede na element */
selektor:hover { }

/* nebo již navštívený odkaz */
selektor:visited { }

/* nebo nebyl navštíven */
selektor:link { }

/* nebo když je vybrán, např kliknutím do inputu*/
selektor:focus { }

/* element, ktery je prvni potomek rodiče */
selektor:first-child {}

/* element, který je poslední potomek rodiče */
selektor:last-child {}

/* Stejně jako pseudo třídy, umožňují pseudo elementy stylizovat určité
   části dokumentu */

/* odpovídá virtuálnímu prvnímu potomku */
selektor::before {}

/* odpovídá virtuálnímu poslednímu potomku */
selektor::after {}

/* Na vhodném místě, může být použitá hvězdička jako žolík, který vybere každý element */
* { } /* všechny elementy */
.rodic * { } /* všechny vnořené elementy */
.rodic > * { } /* všichni potomci */

/* ####################
   ## VLASTNOSTI
   #################### */

selektor {

    /* Jednotky délky můžou být relativní nebo absolutní */

    /* Relativní jednotky */
    width: 50%;       /* počet procent šířky rodičovského elementu */
    font-size: 2em;   /* násobek puvodní velikosti fontu elementu */
    font-size: 2rem;  /* nebo kořenového elementu */
    font-size: 2vw;   /* násobek 1% šířky zařízení (viewport) (CSS 3) */
    font-size: 2vh;   /* nebo jeho výšky */
    font-size: 2vmin; /* násobek 1% výšky nebo šířky, dle toho, co je menší */
    font-size: 2vmax; /* nebo větší */

    /* Absolutní jednotky */
    width: 200px;     /* pixely */
    font-size: 20pt;  /* body */
    width: 5cm;       /* centimetry */
    min-width: 50mm;  /* milimetry */
    max-width: 5in;   /* palce */

    /* Barvy */
    color: #F6E;                 /* krátký hexadecimální formát */
    color: #FF66EE;              /* dlouhý hexadecimální formát */
    color: tomato;               /* pojmenovaná barva */
    color: rgb(255, 255, 255);   /* hodnoty rgb */
    color: rgb(10%, 20%, 50%);   /* procenta rgb */
    color: rgba(255, 0, 0, 0.3); /* hodnoty rgba (CSS 3) Poznámka: 0 < a < 1 */
    color: transparent;          /* ekvivalentní jako nastavení alfy 0 */
    color: hsl(0, 100%, 50%);    /* procenta hsl (CSS 3) */
    color: hsla(0, 100%, 50%, 0.3); /* procenta hsl s alfou */

    /* Obrázky jako pozadí elementu */
    background-image: url(/cesta/k/obrazku.jpg); /* uvozovky jsou dobrovolné */

    /* Fonty */
    font-family: Arial;
    /* když název fontu obsahuje mezeru, tak musí být v uvozovkách */
    font-family: "Courier New";
    /* když se první nenaleze, použije se další atd. */
    font-family: "Courier New", Trebuchet, Arial, sans-serif;
}
```

## Použití

Uložte CSS soubor s příponou `.css`.

```xml
<!-- Musíte vložit css soubor do hlavičky vaší stránky. Toto je
     doporučená metoda. Viz http://stackoverflow.com/questions/8284365 -->
<link rel='stylesheet' type='text/css' href='cesta/k/stylu.css' />

<!-- Také lze vložit CSS přímo do HTML. -->
<style>
   a { color: purple; }
</style>

<!-- Nebo přímo nastavit vlasnost elementu -->
<div style="border: 1px solid red;">
</div>
```

## Priorita nebo kaskáda

Element může být vybrán více selektory a jeho vlastnosti můžou být nastaveny více než jednou. V těchto případech, má jedno zadání vlastnosti prioritu před druhým. Obecně platí, že více specifické selektory mají přednost před těmi méně specifickými.

Tento proces se nazývá kaskáda, proto i název kaskádové styly(Cascading Style Sheets).

Máme následující CSS

```css
/* A */
p.trida1[attr='hodnota']

/* B */
p.trida1 { }

/* C */
p.trida2 { }

/* D */
p { }

/* E */
p { vlastnost: hodnota !important; }
```

a tento element
```xml
<p style='/*F*/ vlastnost:hodnota;' trida='trida1 trida2' attr='hodnota' />
```
Priorita stylu je následující. Pamatujte, priorita pro každou **vlastnost**, ne pro celý blok.

* `E` má nejvyšší prioritu kvůli slůvku `!important`. Je doporučováno se úplně vyhnout jeho použití.
* `F` je další, kvůli stylu zadanému přimo do elementu
* `A` je další, protože je více specifické, než cokoliv dalšího. Má 3 selektory: jméno elementu `p`, jeho třídu `trida1`, atribut `attr='hodnota'`.
* `C` je další, i když je stejně specifický jako `B`, protože je uveden až po něm.
* `B` je další
* `D` je poslední

## Kompatibilita

Většina z možností v CSS 2 (a spousta v CSS 3) je dostupná napříč všemi browsery a zařízeními. Ale pořád je dobrá praxe, zkontrolovat dostupnost, před užitím nové vlastnosti/fičury.

## Zdroje

* Přehled dostupnosti [CanIUse](http://caniuse.com).
* CSS hřiště [Dabblet](http://dabblet.com/).
* [Mozilla Developer Network - CSS dokumentace](https://developer.mozilla.org/en-US/docs/Web/CSS)
* [Codrops](http://tympanus.net/codrops/css_reference/)

## Další čtení

* [Pochopení priority v CSS: specifičnost, děditelnost a kaskáda](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [Vybírání elementů pomocí atributů](https://css-tricks.com/almanac/selectors/a/attribute/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - překrývání obsahu](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
* [SASS](http://sass-lang.com/) a [LESS](http://lesscss.org/) pro CSS pre-processing
* [CSS-Triky](https://css-tricks.com)
---
language: Elm
contributors:
    - ["Max Goldstein", "http://maxgoldste.in/"]
translators:
    - ["Robin Pokorný", "http://robinpokorny.com/"]
filename: learnelm-cz.elm
lang: cs-cz
---

Elm je funkcionální reaktivní jazyk, který se kompiluje do (klientského) JavaScriptu.
Elm je silně typovaný, díky tomu je překladač schopen zachytit většinu chyb okamžitě a
vypsat snadno srozumitelná chybová hlášení.
Elm se hodí k tvorbě webových uživatelských rozhraní a her.


```haskell
-- Jednořádkové komentáře začínají dvěma pomlčkami.
{- Víceřádkové komentáře mohou být takto uzavřeny do bloku.
{- Mohou být i zanořeny. -}
-}

{-- Základy --}

-- Aritmetika
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20

-- Každé číslo bez desetinné tečky je typu Int nebo Float.
33 / 2 -- 16.5 s reálným dělením
33 // 2 -- 16 s celočíselným dělením

-- Umocňování
5 ^ 2 -- 25

-- Pravdivostní proměnné
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- Řetězce a znaky
"Toto je textový řetězec, protože používá dvojité uvozovky."
'a' -- znak v jednoduchých uvozovkách

-- Řetězce lze spojovat.
"Ahoj " ++ "světe!" -- "Ahoj světe!"

{-- Seznamy (List), n-tice (Tuple) a Záznamy (Record) --}

-- Každá položka seznamu musí být stejného typu.
["příliš", "žluťoučký", "kůň", "úpěl"]
[1, 2, 3, 4, 5]
-- Druhý příklad lze zapsat také pomocí dvou teček.
List.range 1 5

-- Spojovat seznamy lze stejně jako řetězce.
List.range 1 5 ++ List.range 6 10 == List.range 1 10 -- True

-- K přidání položky do seznamu použijte funkci "cons".
0 :: List.range 1 5 -- [0, 1, 2, 3, 4, 5]

-- Funkce "head" pro získání první položky seznamu i funkce "tail" pro získání následujích položek
-- vrací typ Maybe. Místo zjišťování, jestli nějaká položka není null,
-- se s chybějcími hodnotami vypořádáme explicitně.
List.head (List.range 1 5) -- Just 1
List.tail (List.range 1 5) -- Just [2, 3, 4, 5]
List.head [] -- Nothing
-- List.nazevFunkce odkazuje na funkci, která žije v modulu List.

-- Každý prvek v n-tici může být jiného typu, ale n-tice má pevný počet prvků.
("elm", 42)

-- K získání hodnot z dvojice použijte funkce first a second.
-- (Toto je pouze zkratka. Brzy si ukážeme, jak na to "správně".)
fst ("elm", 42) -- "elm"
snd ("elm", 42) -- 42

-- Prázná n-tice, neboli "unit", se občas používá jako zástupný symbol.
-- Je to jediná hodnota svého typu, který se také nazývá "Unit".
()

-- Záznamy jsou podobné n-ticím, ale prvky jsou pojmenovány. Na pořadí nezáleží.
-- Povšimněte si, že hodnoty vlastností se přiřazují rovnítky, ne dvojtečkami.
{ x = 3, y = 7 }

-- K hodnotám se přistupuje pomocí tečky a názvu vlastnosti.
{ x = 3, y = 7 }.x -- 3

-- Nebo využitím přístupové funkce, což je jen tečka a název vlastnosti.
.y { x = 3, y = 7 } -- 7

-- Změna hodnoty vlastnosti v záznamu. (Záznam tuto vlastnost už musí mít.)
{ osoba |
  jmeno = "Jiří" }

-- Změna více vlastností s využitím aktuálních hodnot.
{ hmotnyBod |
  poloha = hmotnyBod.poloha + hmotnyBod.rychlost,
  rychlost = hmotnyBod.rychlost + hmotnyBod.zrychleni }

{-- Řídicí struktury --}

-- Podmínky vždy musí mít větev "else" a obě větve musí být stejného typu.
if powerLevel > 9000 then
  "PÁNI!"
else
  "hmm"

-- Podmínky lze skládat za sebe.
if n < 0 then
  "n je záporné"
else if n > 0 then
  "n je kladné"
else
  "n je nula"

-- Použíjte příkaz "case" k nalezení shody vzoru a různých možností.
case seznam of
  [] -> "odpovídá práznému seznamu"
  [x]-> "odpovídá seznamu o právě jedné položce, " ++ toString x
  x::xs -> "odpovídá seznamu o alespoň jedné položce, jehož prvním prvkem je " ++ toString x
-- Shody se vyhodnocují v zapsaném pořadí. Kdybychom umístili [x] poslední, nikdy by nenastala shoda,
-- protože x::xs také odpovídá  (xs by byl prázdný seznam). Shody "nepropadají".
-- Překladač vždy upozorní na chybějící nebo přebývající větve.

-- Větvení typu Maybe.
case List.head seznam of
  Just x -> "První položka je " ++ toString x
  Nothing -> "Seznam byl prázdný."

{-- Funkce --}

-- Syntaxe funkcí je v Elmu velmi úsporná, založená spíše na mezerách
-- než na závorkách. Neexistuje tu klíčové slovo "return".

-- Funkci definujeme jejím jménem, parametry, rovnítkem a tělem.
vynasob a b =
  a * b

-- Funkci voláme předáním parametrů (bez oddělujících čárek).
vynasob 7 6 -- 42

-- Částečně aplikované funkci předáme pouze některé parametry.
-- Poté zvolíme nové jméno.
zdvoj =
  vynasob 2

-- Konstanty jsou podobné, ale nepřijímají žádné parametry.
odpoved =
  42

-- Předejte funkci jako parametr jiným funkcím.
List.map zdvoj (List.range 1 4) -- [2, 4, 6, 8]

-- Nebo použijte anonymní funkci.
List.map (\a -> a * 2) (List.range 1 4) -- [2, 4, 6, 8]

-- V definici funkce lze zapsat vzor, může-li nastat pouze jeden případ.
-- Tato funkce přijímá jednu dvojici místo dvou parametrů.
obsah (sirka, delka) =
  sirka * delka

obsah (6, 7) -- 42

-- Složenými závorkami vytvořte vzor pro názvy vlastností v záznamu.
-- Použijte "let" k definici lokálních proměnných.
objem {sirka, delka, hloubka} =
  let
    obsah = sirka * delka
  in
    obsah * hloubka

objem { sirka = 3, delka = 2, hloubka = 7 } -- 42

-- Funkce mohou být rekurzivní.
fib n =
  if n < 2 then
    1
  else
    fib (n - 1) + fib (n - 2)

List.map fib (List.range 0 8) -- [1, 1, 2, 3, 5, 8, 13, 21, 34]

-- Jiná rekurzivní funkce (v praxi použijte List.length).
delkaSeznamu seznam =
  case seznam of
    [] -> 0
    x::xs -> 1 + delkaSeznamu xs

-- Funkce se volají před jakýmkoli infixovým operátorem. Závorky určují prioritu.
cos (degrees 30) ^ 2 + sin (degrees 30) ^ 2 -- 1
-- Nejprve se aplikuje "degrees" na číslo 30, výsledek je pak předán trigonometrickým
-- funkcím, které jsou následně umocněny na druhou, na závěr proběhne sčítání.

{-- Typy a typové anotace --}

-- Překladač odvodí typ každé hodnoty ve vašem programu.
-- Typy vždy začínají velkým písmenem. Čtete x : T jako "x je typu T".
-- Některé běžné typy, které můžete videt v Elmovém REPLu.
5 : Int
6.7 : Float
"ahoj" : String
True : Bool

-- Funkce mají také typy. Čtěte "->" jako "vrací".
-- O typu na konci uvažujte jako návratovém typu, o ostatních jako typech argumentů.
not : Bool -> Bool
round : Float -> Int

-- Když definujete hodnotu, je dobrým zvykem zapsat nad ni její typ.
-- Anotace je formou dokumentace, která je ověřována překladačem.
zdvoj : Int -> Int
zdvoj x = x * 2

-- Funkce jako parametr je uzavřena v závorkách.
-- Typy s malým počátečním písmenem jsou typové proměnné:
-- mohou být libovolného typu, ale v každém volání musí být stejné.
List.map : (a -> b) -> List a -> List b
-- "List tečka map je typu a-vrací-b, vrací seznam-položek-typu-a, vrací seznam-položek-typu-b."

-- Existují tři speciální typové proměnné:
-- číslo (number), porovnatelné (comparable), and spojitelné (appendable).
-- Čísla dovolují použít aritmetiku na Int a Float.
-- Porovnatelné dovolují uspořádat čísla a řetězce, např. a < b.
-- Spojitelné lze zřetězit pomocí a ++ b.

{-- Typové aliasy a výčtové typy --}

-- Pro záznamy a n-tice již typy automaticky existují.
-- (Povšimněte si, že typ vlatnosti záznamu přiřazujeme dvojtečkou a hodnotu rovnítkem.)
pocatek : { x : Float, y : Float, z : Float }
pocatek =
  { x = 0, y = 0, z = 0 }

-- Stávajícím typům lze dávat jména využitím aliasů.
type alias Bod3D =
  { x : Float, y : Float, z : Float }

-- Alias pro záznam funguje také jako jeho konstruktor.
jinyPocatek : Bod3D
jinyPocatek =
  Bod3D 0 0 0

-- Jedná se stále o stejný typ, lze je tedy porovnat.
pocatek == jinyPocatek -- True

-- Oproti tomu výčtový (union) typ definuje zcela nový typ.
-- Výčtový typ se takto jmenuje, protože může být jedním z několika vybraných možností.
-- Každá možnost je reprezentována jako "tag".
type Smer =
  Sever | Jih | Vychod | Zapad

-- Tagy mohou obsahovat další hodnoty známých typů. Lze využít i rekurze.
type IntStrom =
  Vrchol | Uzel Int IntStrom IntStrom
-- "Vrchol" i "Uzel" jsou tagy. Vše, co následuje za tagem, je typ.

-- Tagy lze použít jako hodnoty funkcí.
koren : IntStrom
koren =
  Vrchol 7 List List

-- Výčtové typy (a typové aliasy) mohou obsahovat typové proměnné.
type Strom a =
  Vrchol | Uzel a (Strom a) (Strom a)
-- "Typ strom-prvků-a je vrchol, nebo uzel obsahující a, strom-prvků-a a strom-prvků-a."

-- Vzory se shodují s tagy. Tagy s velkým počátečním písmenem odpovídají přesně.
-- Proměnné malým písmem odpovídají čemukoli. Podtržítko také odpovídá čemukoli,
-- ale určuje, že tuto hodnotu dále nechceme používat.
nejviceVlevo : Strom a -> Maybe a
nejviceVlevo strom =
  case strom of
    Vrchol -> Nothing
    Uzel x Vrchol _ -> Just x
    Uzel _ podstrom _ -> nejviceVlevo podstrom

-- To je víceméně vše o jazyku samotném.
-- Podívejme se nyní, jak organizovat a spouštět náš kód.

{-- Moduly a importování --}

-- Standardní knihovny jsou organizovány do modulů, stejně jako knihovny třetích stran,
-- které můžete využívat. Ve větších projektech můžete definovat vlastní moduly.

-- Vložte toto na začátek souboru. Pokud nic neuvedete, předpokládá se "Main".
module Jmeno where

-- Výchozím chováním je, že se exportuje vše.
-- Případně můžete definovat exportované vlastnosti explicitně.
module Jmeno (MujTyp, mojeHodnota) where

-- Běžný návrhový vzor je expotovat pouze výčtový typ bez jeho tagů.
-- Tento vzor je znám jako krycí typ a často se využívá v knihovnách.

-- Z jiných modulů lze importovat kód a použít jej v aktuálním modulu.
-- Nasledující umístí Dict do aktuálního scope, takže lze volat Dict.insert.
import Dict

-- Importuje modul Dict a typ Dict, takže v anotacích není nutné psát Dict.Dict.
-- Stále lze volat Dict.insert.
import Dict exposing (Dict)

-- Přejmenování importu.
import Graphics.Collage as C

{-- Porty --}

-- Port oznamuje, že budete komunikovat s vnějším světem.
-- Porty jsou dovoleny pouze v modulu Main.

-- Příchozí port je jen typová anotace.
port idKlienta : Int

-- Odchozí port má definici.
port objednavkaKlienta : List String
port objednavkaKlienta = ["Knihy", "Potraviny", "Nábytek"]

-- Nebudeme zacházet do detailů, ale v JavaScriptu se dají nastavit
-- callbacky pro zasílání na příchozí porty a čtení z odchozích portů.

{-- Nástroje pro příkazovou řádku --}

-- Kompilace souboru.
$ elm make MujSoubor.elm

-- Při prvním spuštění nainstaluje Elm standardní knihovny a vytvoří soubor
-- elm-package.json, kde jsou uloženy informace o vašem projektu.

-- Elm reactor je server, který překládá a spouští vaše soubory.
-- Kliknutím na klíč vedle názvu souboru spustíte debugger s cestovám v čase!
$ elm reactor

-- Zkoušejte si jednoduché příkazy v Read-Eval-Print Loop.
$ elm repl

-- Balíčky jsou určeny uživatelským jménem na GitHubu a názvem repozitáře.
-- Nainstalujte nový balíček a uložte jej v souboru elm-package.json.
$ elm package install evancz/elm-lang/html

-- Porovnejte změny mezi verzemi jednoho balíčku.
$ elm package diff elm-lang/html 1.1.0 2.0.0
-- Správce balíčků v Elmu vyžaduje sémantické verzování,
-- takže minor verze nikdy nerozbije váš build.
```

Jazyk Elm je překvapivě malý. Nyní se můžete podívat do skoro jakéhokoli zdrojového kódu
v Elmu a budete mít zběžnou představu o jeho fungování.
Ovšem možnosti, jak psát kód, který je odolný vůči chybám a snadno se refaktoruje, jsou neomezené!

Zde jsou některé užitečné zdroje (v angličtině).

* [Hlavní stránka Elmu](http://elm-lang.org/). Obsahuje:
  * Odkazy na [instalátory](http://elm-lang.org/install)
  * [Documentaci](http://elm-lang.org/docs), včetně [popisu syntaxe](http://elm-lang.org/docs/syntax)
  * Spoustu nápomocných [příkladů](http://elm-lang.org/examples)

* Documentace pro [standardní knihovny Elmu](http://package.elm-lang.org/packages/elm-lang/core/latest/). Povšimněte si:
  * [Základy](http://package.elm-lang.org/packages/elm-lang/core/latest/Basics), které jsou automaticky importovány
  * Typ [Maybe](http://package.elm-lang.org/packages/elm-lang/core/latest/Maybe) a jeho bratranec typ [Result](http://package.elm-lang.org/packages/elm-lang/core/latest/Result), které se běžně používají pro chybějící hodnoty a ošetření chyb.
  * Datové struktury jako [List](http://package.elm-lang.org/packages/elm-lang/core/latest/List), [Array](http://package.elm-lang.org/packages/elm-lang/core/latest/Array), [Dict](http://package.elm-lang.org/packages/elm-lang/core/latest/Dict) a [Set](http://package.elm-lang.org/packages/elm-lang/core/latest/Set)
  * JSON [enkódování](http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Encode) a [dekódování](http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Decode)

* [Architektura Elmu](https://github.com/evancz/elm-architecture-tutorial#the-elm-architecture). Esej od tvůrce Elmu s příklady, jak organizovat kód do komponent.

* [Elm mailing list](https://groups.google.com/forum/#!forum/elm-discuss). Všichni jsou přátelští a nápomocní.

* [Scope v Elmu](https://github.com/elm-guides/elm-for-js/blob/master/Scope.md#scope-in-elm) a [Jak číst typové anotace](https://github.com/elm-guides/elm-for-js/blob/master/How%20to%20Read%20a%20Type%20Annotation.md#how-to-read-a-type-annotation). Další komunitní návody o základech Elmu, psáno pro JavaScriptové vývojáře.

Běžte si zkusit něco napsat v Elmu!
---
name: Go
category: language
language: Go
filename: learngo-cs.go
lang: cs-cz
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
    - ["Alexej Friesen", "https://github.com/heyalexej"]
    - ["Clayton Walker", "https://github.com/cwalk"]
translators:
    - ["Ondra Linek", "https://github.com/defectus/"]
---

Jazyk Go byl vytvořen, jelikož bylo potřeba dokončit práci. Není to poslední 
trend ve světě počítačové vědy, ale je to nejrychlejší a nejnovější způsob,
jak řešit realné problémy.

Go používá známé koncepty imperativních jazyků se statickým typováním.
Rychle se kompiluje a také rychle běží. Přidává snadno pochopitelnou
podporu konkurenčnosti, což umožňuje využít výhody multi-core procesorů a
jazyk také obsahuje utility, které pomáhají se škálovatelným programováním.

Go má již v základu vynikající knihovnu a je s ním spojená nadšená komunita.

```go
// Jednořádkový komentář
/* Několika
 řádkový komentář */

// Každý zdroják začíná deklarací balíčku (package)
// Main je vyhrazené jméno, které označuje spustitelný soubor,
// narozdíl od knihovny
package main

// Importní deklarace říkají, které knihovny budou použity v tomto souboru.
import (
	"fmt"       // Obsahuje formátovací funkce a tisk na konzolu
	"io/ioutil" // Vstupně/výstupní funkce
	m "math"    // Odkaz na knihovnu math (matematické funkce) pod zkratkou m
	"net/http"  // Podpora http protokolu, klient i server.
	"strconv"   // Konverze řetězců, např. na čísla a zpět.
)

// Definice funkce. Funkce main je zvláštní, je to vstupní bod do programu.
// Ať se vám to líbí, nebo ne, Go používá složené závorky
func main() {
	// Println vypisuje na stdout.
	// Musí být kvalifikováno jménem svého balíčko, ftm.
	fmt.Println("Hello world!")

	// Zavoláme další funkci
	svetPoHello()
}

// Funkce mají své parametry v závorkách
// Pokud funkce nemá parametry, tak musíme stejně závorky uvést.
func svetPoHello() {
	var x int // Deklarace proměnné. Proměnné musí být před použitím deklarované
	x = 3     // Přiřazení hodnoty do proměnné
	// Existuje "krátká" deklarace := kde se typ proměnné odvodí, 
	// proměnná vytvoří a přiřadí se jí hodnota
	y := 4
	sum, prod := naucSeNasobit(x, y)        // Funkce mohou vracet více hodnot
	fmt.Println("sum:", sum, "prod:", prod) // Jednoduchý výstup
	naucSeTypy()                            // < y minut je za námi, je čas učit se víc!
}

/* <- začátek mnohořádkového komentáře
Funkce mohou mít parametry a (několik) návratových hodnot.
V tomto případě jsou `x`, `y` parametry a `sum`, `prod` jsou návratové hodnoty.
Všiměte si, že `x` a `sum` jsou typu `int`.
*/
func naucSeNasobit(x, y int) (sum, prod int) {
	return x + y, x * y // Vracíme dvě hodnoty
}

// zabudované typy a literáty.
func naucSeTypy() {
	// Krátká deklarace většinou funguje
	str := "Learn Go!" // typ řetězec.

	s2 := `"surový" literát řetězce
může obsahovat nové řádky` // Opět typ řetězec.

	// Můžeme použít ne ASCII znaky, Go používá UTF-8.
	g := 'Σ' // type runa, což je alias na int32 a ukládá se do něj znak UTF-8

	f := 3.14195 // float64, je IEEE-754 64-bit číslem s plovoucí čárkou.
	c := 3 + 4i  // complex128, interně uložené jako dva float64.

	// takhle vypadá var s inicializací
	var u uint = 7 // Číslo bez znaménka, jehož velikost záleží na implementaci,
	               // stejně jako int
	var pi float32 = 22. / 7

	// takto se převádí typy za pomoci krátké syntaxe
	n := byte('\n') // byte je jiné jméno pro uint8.

	// Pole mají fixní délku, které se určuje v době kompilace.
	var a4 [4]int           // Pole 4 intů, všechny nastaveny na 0.
	a3 := [...]int{3, 1, 5} // Pole nastaveno na tři hodnoty
	// elementy mají hodntu 3, 1 a 5

	// Slicy mají dynamickou velikost. Pole i slacy mají své výhody,
	// ale většinou se používají slicy.
	s3 := []int{4, 5, 9}    // Podobně jako a3, ale není tu výpustka.
	s4 := make([]int, 4)    // Alokuj slice 4 intů, všechny nastaveny na 0.
	var d2 [][]float64      // Deklarace slicu, nic se nealokuje.
	bs := []byte("a slice") // Přetypování na slice

	// Protože jsou dynamické, můžeme ke slicům přidávat za běhu
	// Přidat ke slicu můžeme pomocí zabudované funkce append().
	// Prvním parametrem je slice, návratová hodnota je aktualizovaný slice.
	s := []int{1, 2, 3}		// Výsledkem je slice se 3 elementy.
	s = append(s, 4, 5, 6)	// Přidány další 3 elementy. Slice má teď velikost 6.
	fmt.Println(s) // Slice má hodnoty [1 2 3 4 5 6]

	// Pokud chceme k poli přičíst jiné pole, můžeme předat referenci na slice,
	// nebo jeho literát a přidat výpustku, čímž se slicu "rozbalí" a přidá se k
	// původnímu slicu.
	s = append(s, []int{7, 8, 9}...) // druhým parametrem je literát slicu.
	fmt.Println(s)	// slice má teď hodnoty [1 2 3 4 5 6 7 8 9]

	p, q := naucSePraciSPameti() // Deklarujeme p a q jako typ pointer na int.
	fmt.Println(*p, *q)   // * dereferencuje pointer. Tím se vypíší dva inty.

	// Mapy jsou dynamické rostoucí asociativní pole, jako hashmapa, nebo slovník
	// (dictionary) v jiných jazycích
	m := map[string]int{"tri": 3, "ctyri": 4}
	m["jedna"] = 1

	// Napoužité proměnné jsou v Go chybou.
	// Použijte podtržítko, abychom proměnno "použili".
	_, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs
	// Výpis promenné se počítá jako použití.
	fmt.Println(s, c, a4, s3, d2, m)

	naucSeVetveníProgramu() // Zpátky do běhu.
}

// narozdíl od jiných jazyků, v Go je možné mít pojmenované návratové hodnoty.
// Tak můžeme vracet hodnoty z mnoha míst funkce, aniž bychom uváděli hodnoty v
// return.
func naucSePojmenovaneNavraty(x, y int) (z int) {
	z = x * y
	return // z je zde implicitní, jelikož bylo pojmenováno.
}

// Go má garbage collector. Používá pointery, ale neumožňuje jejich aritmetiku.
// Můžete tedy udělat chybu použitím nil odkazu, ale ne jeho posunutím.
func naucSePraciSPameti() (p, q *int) {
	// Pojmenované parametry p a q mají typ odkaz na int.
	p = new(int) // Zabudované funkce new alokuje paměť.
	// Alokované místo pro int má hodnotu 0 a p už není nil.
	s := make([]int, 20) // Alokujeme paměť pro 20 intů.
	s[3] = 7             // Jednu z nich nastavíme.
	r := -2              // Deklarujeme další lokální proměnnou.
	return &s[3], &r     // a vezmeme si jejich odkaz pomocí &.
}

func narocnyVypocet() float64 {
	return m.Exp(10)
}

func naucSeVetveníProgramu() {
	// Výraz if vyžaduje složené závorky, ale podmínka nemusí být v závorkách.
	if true {
		fmt.Println("říkal jsme ti to")
	}
	// Formátování je standardizované pomocí utility "go fmt".
	if false {
		// posměšek.
	} else {
		// úšklebek.
	}
	// Použij switch, když chceš zřetězit if.
	x := 42.0
	switch x {
	case 0:
	case 1:
	case 42:
		// jednotlivé case nepropadávají. není potřeba "break"
	case 43:
		// nedosažitelné, jelikož už bylo ošetřeno.
	default:
		// implicitní větev je nepovinná.
	}
	// Stejně jako if, for (smyčka) nepoužívá závorky.
	// Proměnné definované ve for jsou lokální vůči smyčce.
	for x := 0; x < 3; x++ { // ++ je výrazem.
		fmt.Println("iterace", x)
	}
	// zde je x == 42.

	// For je jediná smyčka v Go, ale má několik tvarů.
	for { // Nekonečná smyčka
		break    // Dělám si legraci
		continue // Sem se nedostaneme
	}

	// Můžete použít klíčové slovo range pro iteraci nad mapami, poli, slicy,
	// řetězci a kanály.
	// range vrací jednu (kanál) nebo dvě hodnoty (pole, slice, řetězec a mapa).
	for key, value := range map[string]int{"jedna": 1, "dva": 2, "tri": 3} {
		// pro každý pár (klíč a hodnota) je vypiš
		fmt.Printf("klíč=%s, hodnota=%d\n", key, value)
	}

	// stejně jako for, := v podmínce if přiřazuje hodnotu
	// nejříve nastavíme y a pak otestujeme, jestli je y větší než x.
	if y := narocnyVypocet(); y > x {
		x = y
	}
	// Funkční literáty jsou tzv. uzávěry (closure)
	xBig := func() bool {
		return x > 10000 // odkazuje na x deklarované ve příkladu použití switch
	}
	x = 99999
	fmt.Println("xBig:", xBig()) // true
	x = 1.3e3                    // To udělá z x == 1300
	fmt.Println("xBig:", xBig()) // teď už false.

	// Dále je možné funkční literáty definovat a volat na místě jako parametr
	// funkce, dokavaď:
	// a) funkční literát je okamžitě volán pomocí (),
	// b) výsledek se shoduje s očekávaným typem.
	fmt.Println("Sečte + vynásobí dvě čísla: ",
		func(a, b int) int {
			return (a + b) * 2
		}(10, 2)) // Voláno s parametry 10 a 2
	// => Sečti a vynásob dvě čísla. 24

	// Když to potřebujete, tak to milujete
	goto miluji
miluji:

	naučteSeFunkčníFactory() // funkce vracející funkce je zábava(3)(3)
	naučteSeDefer()      // malá zajížďka k důležitému klíčovému slovu.
	naučteSeInterfacy() // Přichází dobré věci!
}

func naučteSeFunkčníFactory() {
	// Následující dvě varianty jsou stejné, ale ta druhá je praktičtější
	fmt.Println(větaFactory("létní")("Hezký", "den!"))

	d := větaFactory("letní")
	fmt.Println(d("Hezký", "den!"))
	fmt.Println(d("Líný", "odpoledne!"))
}

// Dekorátory jsou běžné v jiných jazycích. To samé můžete udělat v Go
// pomocí parameterizovatelných funkčních literátů.
func větaFactory(můjŘetězec string) func(před, po string) string {
	return func(před, po string) string {
		return fmt.Sprintf("%s %s %s", před, můjŘetězec, po) // nový řetězec
	}
}

func naučteSeDefer() (ok bool) {
	// Odloží (defer) příkazy na okamžik těsně před opuštěním funkce.
	// tedy poslední se provede první
	defer fmt.Println("odložené příkazy jsou zpravovaná v LIFO pořadí.")
	defer fmt.Println("\nProto je tato řádka vytištěna první")
	// Defer se běžně používá k zavírání souborů a tím se zajistí, že soubor
	// bude po ukončení funkce zavřen.
	return true
}

// definuje typ interfacu s jednou metodou String()
type Stringer interface {
	String() string
}

// Definuje pár jako strukturu se dvěma poli typu int x a y.
type pár struct {
	x, y int
}

// Definuje method pár. Pár tedy implementuje interface Stringer.
func (p pár) String() string { // p je tu nazýváno "Receiver" - přijímač
	// Sprintf je další veřejná funkce z balíčku fmt.
	// Pomocí tečky přistupujeme k polím proměnné p
	return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func naučteSeInterfacy() {
	// Složené závorky jsou "strukturální literáty. Vyhodnotí a inicializuje
	// strukturu. Syntaxe := deklaruje a inicializuje strukturu.
	p := pár{3, 4}
	fmt.Println(p.String()) // Volá metodu String na p typu pár.
	var i Stringer          // Deklaruje i jako proměnné typu Stringer.
	i = p                   // Toto je možné, jelikož oba implementují Stringer
	// zavolá metodu String(( typu Stringer a vytiskne to samé jako předchozí.
	fmt.Println(i.String())

	// Funkce ve balíčku fmt volají metodu String, když zjišťují, jak se má typ
	// vytisknout.
	fmt.Println(p) // Vytiskne to samé, jelikož Println volá String().
	fmt.Println(i) // Ten samý výstup.

	naučSeVariabilníParametry("super", "učit se", "tady!")
}

// Funcke mohou mít proměnlivé množství parametrů.
func naučSeVariabilníParametry(mojeŘetězce ...interface{}) {
	// Iterujeme přes všechny parametry
	// Potržítku tu slouží k ignorování indexu v poli.
	for _, param := range mojeŘetězce {
		fmt.Println("parameter:", param)
	}

	// Použít variadický parametr jako variadický parametr, nikoliv pole.
	fmt.Println("parametery:", fmt.Sprintln(mojeŘetězce...))

	naučSeOšetřovatChyby()
}

func naučSeOšetřovatChyby() {
	// ", ok" je metodou na zjištění, jestli něco fungovalo, nebo ne.
	m := map[int]string{3: "tri", 4: "ctyri"}
	if x, ok := m[1]; !ok { // ok bude false, jelikož 1 není v mapě.
		fmt.Println("není tu jedna")
	} else {
		fmt.Print(x) // x by bylo tou hodnotou, pokud by bylo v mapě.
	}
	// hodnota error není jen znamením OK, ale může říct více o chybě.
	if _, err := strconv.Atoi("ne-int"); err != nil { // _ hodnotu zahodíme
		// vytiskne 'strconv.ParseInt: parsing "non-int": invalid syntax'
		fmt.Println(err)
	}
	// Znovu si povíme o interfacech, zatím se podíváme na
	naučSeKonkurenčnost()
}

// c je kanál, způsob, jak bezpečně komunikovat v konkurenčním prostředí.
func zvyš(i int, c chan int) {
	c <- i + 1 // <- znamená "pošli" a posílá data do kanálu na levé straně.
}

// Použijeme funkci zvyš a konkurečně budeme zvyšovat čísla.
func naučSeKonkurenčnost() {
	// funkci make jsme již použili na slicy. make alokuje a inicializuje slidy,
	// mapy a kanály.
	c := make(chan int)
	// nastartuj tři konkurenční go-rutiny. Čísla se budou zvyšovat
	// pravděpodobně paralelně pokud je počítač takto nakonfigurován.
	// Všechny tři zapisují do toho samého kanálu.
	go zvyš(0, c) // go je výraz pro start nové go-rutiny.
	go zvyš(10, c)
	go zvyš(-805, c)
	// Přečteme si tři výsledky a vytiskeneme je..
	// Nemůžeme říct, v jakém pořadí výsledky přijdou!
	fmt.Println(<-c, <-c, <-c) // pokud je kanál na pravo, jedná se o "přijmi".

	cs := make(chan string)       // Další kanál, tentokrát pro řetězce.
	ccs := make(chan chan string) // Kanál kanálu řetězců.
	go func() { c <- 84 }()       // Start nové go-rutiny na posílání hodnot.
	go func() { cs <- "wordy" }() // To samé s cs.
	// Select má syntaxi jako switch, ale vztahuje se k operacím nad kanály.
	// Náhodně vybere jeden case, který je připraven na komunikaci.
	select {
        case i := <-c: // Přijatá hodnota může být přiřazena proměnné.
            fmt.Printf("je to typ %T", i)
        case <-cs: // nebo může být zahozena
            fmt.Println("je to řetězec")
        case <-ccs: // prázdný kanál, nepřipraven ke komunikaci.
      		fmt.Println("to se nestane.")
	}
	// V tomto okamžiku máme hodnotu buď z kanálu c nabo cs. Jedna nebo druhá
	// nastartovaná go-rutina skončila a další zůstane blokovaná.

	naučSeProgramovatWeb() // Go to umí. A vy to chcete taky.
}

// jen jedna funkce z balíčku http spustí web server.
func naučSeProgramovatWeb() {

	// První parametr ListenAndServe je TCP adresa, kde poslouchat.
	// Druhý parametr je handler, implementující interace http.Handler.
	go func() {
		err := http.ListenAndServe(":8080", pár{})
		fmt.Println(err) // neignoruj chyby
	}()

	requestServer()
}

// Umožní typ pár stát se http tím, že implementuje její jedinou metodu
// ServeHTTP.
func (p pár) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Servíruj data metodou http.ResponseWriter
	w.Write([]byte("Naučil ses Go za y minut!"))
}

func requestServer() {
	resp, err := http.Get("http://localhost:8080")
	fmt.Println(err)
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Printf("\nWebserver řekl: `%s`", string(body))
}
```

## Kam dále

Vše hlavní o Go se nachází na [oficiálních stránkách go](http://golang.org/).
Tam najdete tutoriály, interaktivní konzolu a mnoho materiálu ke čtení.
Kromě úvodu, [dokumenty](https://golang.org/doc/) tam obsahují jak psát čistý kód v Go
popis balíčků (package), dokumentaci příkazové řádky a historii releasů.

Také doporučujeme přečíst si definici jazyka. Je čtivá a překvapivě krátká. Tedy alespoň proti
jiným současným jazyků.

Pokud si chcete pohrát s Go, tak navštivte [hřiště Go](https://play.golang.org/p/r46YvCu-XX).
Můžete tam spouštět programy s prohlížeče. Také můžete [https://play.golang.org](https://play.golang.org) použít jako
[REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop), kde si v rychlosti vyzkoušíte věci, bez instalace Go.

Na vašem knižním seznamu, by neměly chybět [zdrojáky stadardní knihovny](http://golang.org/src/pkg/).
Důkladně popisuje a dokumentuje Go, styl zápisu Go a Go idiomy. Pokud kliknete na [dokumentaci](http://golang.org/pkg/)
tak se podíváte na dokumentaci.

Dalším dobrým zdrojem informací je [Go v ukázkách](https://gobyexample.com/).

Go mobile přidává podporu pro Android a iOS. Můžete s ním psát nativní mobilní aplikace nebo knihovny, které půjdou
spustit přes Javu (pro Android), nebo Objective-C (pro iOS). Navštivte [web Go Mobile](https://github.com/golang/go/wiki/Mobile)
pro více informací.
---
language: Hack
filename: learnhack-cs.hh
contributors:
    - ["Stephen Holdaway", "https://github.com/stecman"]
translators:
    - ["Vojta Svoboda", "https://github.com/vojtasvoboda/"]
lang: cs-cz
---

Hack je nadmnožinou PHP a běží v rámci virtuálního stroje zvaného HHVM. Hack 
dokáže skoro plně spolupracovat s existujícím PHP a přidává několik vylepšení, 
které známe ze staticky typovaných jazyků.

Níže jsou popsané pouze vlastnosti jazyka Hack. Detaily ohledně jazyka PHP a jeho
syntaxe pak najdete na těchto stránkách v samostatném 
[článku o PHP](http://learnxinyminutes.com/docs/php/).

```php
<?hh

// Hack je aktivní pouze pro soubory, které začínají <?hh.
// TODO <?hh soubory nemohou být jendoduše přeloženy v HTML tak jako <?php.
// Použitím značky <?hh //strict zapnete striktní mód typové kontroly.


// Typování skalární parametrů
function repeat(string $word, int $count)
{
    $word = trim($word);
    return str_repeat($word . ' ', $count);
}

// Typování návratových hodnot
function add(...$numbers) : int
{
    return array_sum($numbers);
}

// Funkce které nic nevrací jsou typované jako "void"
function truncate(resource $handle) : void
{
    // ...
}

// U typování musíme explicitně povolit prázdné (null) hodnoty
function identity(?string $stringOrNull) : ?string
{
    return $stringOrNull;
}

// Typování může být použito i na proměnné třídy
class TypeHintedProperties
{
    public ?string $name;
    
    protected int $id;

    private float $score = 100.0;

    // Typ proměnné si můžeme zadat přímo u definice proměnné v rámci třídy,
    // ale pak ho snadně přetížit v konstruktoru metody.
    public function __construct(int $id)
    {
        $this->id = $id;
    }
}


// Stručné anonymní funkce (lambda funkce)
$multiplier = 5;
array_map($y ==> $y * $multiplier, [1, 2, 3]);


// Generika (generické funkce)
class Box<T>
{
    protected T $data;

    public function __construct(T $data) {
        $this->data = $data;
    }

    public function getData(): T {
        return $this->data;
    }
}

function openBox(Box<int> $box) : int
{
    return $box->getData();
}


// Tvary
// 
// Hack zavádí koncept tvaru pro definování strukturovaných polí s garantovanou
// typovou kontrolou pro klíče.
type Point2D = shape('x' => int, 'y' => int);

function distance(Point2D $a, Point2D $b) : float
{
    return sqrt(pow($b['x'] - $a['x'], 2) + pow($b['y'] - $a['y'], 2));
}

distance(
    shape('x' => -1, 'y' => 5),
    shape('x' => 2, 'y' => 50)
);


// Type aliasing
// 
// Hack přidává několik vylepšení pro lepší čitelnost komplexních typů
newtype VectorArray = array<int, Vector<int>>;

// Množina obsahující čísla
newtype Point = (int, int);

function addPoints(Point $p1, Point $p2) : Point
{
    return tuple($p1[0] + $p2[0], $p1[1] + $p2[1]);
}

addPoints(
    tuple(1, 2),
    tuple(5, 6)
);


// Výčtový typ
enum RoadType : int
{
    Road = 0;
    Street = 1;
    Avenue = 2;
    Boulevard = 3;
}

function getRoadType() : RoadType
{
    return RoadType::Avenue;
}


// Automatické nastavení proměnných třídy
// 
// Aby se nemuseli definovat proměnné třídy a její konstruktor,
// který pouze nastavuje třídní proměnné, můžeme v Hacku vše 
// definovat najednou.
class ArgumentPromotion
{
    public function __construct(public string $name,
                                protected int $age,
                                private bool $isAwesome) {}
}

// Takto by to vypadalo bez automatického nastavení proměnných
class WithoutArugmentPromotion
{
    public string $name;

    protected int $age;

    private bool $isAwesome;

    public function __construct(string $name, int $age, bool $isAwesome)
    {
        $this->name = $name;
        $this->age = $age;
        $this->isAwesome = $isAwesome;
    }
}


// Ko-operativní multi-tasking
// 
// Nová klíčová slova "async" and "await" mohou být použité pro spuštění mutli-taskingu
// Tato vlastnost ovšem zahrnuje vícevláknové zpracování, pouze povolí řízení přenosu
async function cooperativePrint(int $start, int $end) : Awaitable<void>
{
    for ($i = $start; $i <= $end; $i++) { 
        echo "$i ";

        // Dává ostatním úlohám šanci něco udělat
        await RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0);
    }
}

// Toto vypíše "1 4 7 2 5 8 3 6 9"
AwaitAllWaitHandle::fromArray([
    cooperativePrint(1, 3),
    cooperativePrint(4, 6),
    cooperativePrint(7, 9)
])->getWaitHandle()->join();


// Atributy
// 
// Atributy jsou určitou formou metadat pro funkce. Hack přidává některé vestavěné 
// atributy které aktivnují uživatečné chování funkcí.

// Speciální atribut __Memoize způsobí, že výsledek funkce je uložen do cache
<<__Memoize>>
function doExpensiveTask() : ?string
{
    return file_get_contents('http://example.com');
}

// Tělo funkce je v tomto případě vykonáno pouze jednou:
doExpensiveTask();
doExpensiveTask();


// Speciální atribut __ConsistentConstruct signalizuje typové kontrole Hacku, že
// zápis __construct bude stejný pro všechny podtřídy.
<<__ConsistentConstruct>>
class ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
        // ...
    }

    public function someMethod()
    {
        // ...
    }
}

class ConsistentBar extends ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
        // Typová kontrola Hacku zajistí volání konstruktoru rodičovské třídy
        parent::__construct($x, $y);

        // ...
    }

    // Anotace __Override je volitelný signál pro typovou kontrolu Hacku, že
    // tato metoda přetěžuje metodu rodičovské třídy, nebo traitu. Bez uvedení
    // této anotace vyhodí typová kontrola chybu.
    <<__Override>>
    public function someMethod()
    {
        // ...
    }
}

class InvalidFooSubclass extends ConsistentFoo
{
    // Nedodržení zápisu dle rodičovského konstruktoru způsobí syntaktickou chybu:
    //  
    //  "Tento objekt je typu ConsistentBaz a není kompatibilní v tímto objektem,
    //   který je typu ConsistentFoo protože některé jeho metody nejsou kompatibilní."
    //
    public function __construct(float $x)
    {
        // ...
    }

    // Použitím anotace __Override na nepřetíženou metodu způsobí chybu typové kontroly:
    //  
    //  "InvalidFooSubclass::otherMethod() je označená jako přetížená, ale nebyla nalezena
    //   taková rodičovská metoda, nebo rodič kterého přetěžujete není zapsán v <?hh kódu"
    //
    <<__Override>>
    public function otherMethod()
    {
        // ...
    }
}


// Traity mohou implementovat rozhraní, což standardní PHP neumí
interface KittenInterface
{
    public function play() : void;
}

trait CatTrait implements KittenInterface
{
    public function play() : void
    {
        // ...
    }
}

class Samuel
{
    use CatTrait;
}


$cat = new Samuel();
$cat instanceof KittenInterface === true; // True

```

## Více informací

Pro více informací navštivte [referenční příručku jazyka Hack](http://docs.hhvm.com/manual/en/hacklangref.php), 
kde se dozvíte více detailu a vylepšení, které jazyk Hack přidává do PHP, a nebo navštivte [oficiální stránky jazyka Hack](http://hacklang.org/)
pro obecné informace.

Pro instrukce k instalaci jazyka Hack navštivte [oficiální HHVM stránky](http://hhvm.com/).

Pro více informací ohledně zpětné kompatibility s PHP navštivte článek o [nepodporovaných PHP vlastnostech Hacku](http://docs.hhvm.com/manual/en/hack.unsupported.php).
---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
translators:
    - ["Michal Martinek", "https://github.com/MichalMartinek"]
lang: cs-cz
filename: javascript-cz.js
---

JavaScript byl vytvořen Brendan Eichem v roce 1995 pro Netscape. Byl původně
zamýšlen jako jednoduchý skriptovací jazyk pro webové stránky, jako doplněk Javy,
která byla zamýšlena pro více komplexní webové aplikace, ale jeho úzké propojení
s webovými stránkami a vestavěná podpora v prohlížečích způsobila, že se stala
více běžná ve webovém frontendu než Java.


JavaScript není omezen pouze na webové prohlížeče, např. projekt Node.js,
který zprostředkovává samostatně běžící prostředí V8 JavaScriptového enginu z
Google Chrome se stává více a více oblíbený pro serverovou část webových aplikací.

Zpětná vazba je velmi ceněná. Autora článku můžete kontaktovat (anglicky) na
[@adambrenecki](https://twitter.com/adambrenecki), nebo
[adam@brenecki.id.au](mailto:adam@brenecki.id.au), nebo mě, jakožto překladatele,
na [martinek@ludis.me](mailto:martinek@ludis.me).

```js
// Komentáře jsou jako v zayku C. Jednořádkové komentáře začínájí dvojitým lomítkem,
/* a víceřádkové komentáře začínají lomítkem s hvězdičkou
   a končí hvězdičkou s lomítkem */

// Vyrazu můžou být spuštěny pomocí ;
delejNeco();

// ... ale nemusí, středníky jsou automaticky vloženy kdekoliv,
// kde končí řádka, kromě pár speciálních případů
delejNeco()

// Protože tyto případy můžou způsobit neočekávané výsledky, budeme
// středníky v našem návodu používat.

/////////////////////////////////
// 1. Čísla, řetězce a operátory

// JavaScript má jeden číselný typ (čímž je 64-bitový IEEE 754 double).
// Double má 52-bit přesnost, což je dostatečně přesné pro ukládání celých čísel
// do 9✕10¹⁵.
3; // = 3
1.5; // = 1.5

// Základní matematické operace fungují, jak byste očekávali
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// Včetně dělení
5 / 2; // = 2.5

// A také dělení modulo
10 % 2; // = 0
30 % 4; // = 2
18.5 % 7; // = 4.5

// Bitové operace také fungují; když provádíte bitové operace, desetinné číslo  
// (float) se převede na celé číslo (int) se znaménkem *do* 32 bitů
1 << 2; // = 4

// Přednost se vynucuje závorkami.
(1 + 3) * 2; // = 8

// Existují 3 hodnoty mimo obor reálných čísel
Infinity; // + nekonečno; výsledek např. 1/0
-Infinity; // - nekonečno; výsledek např. -1/0
NaN; // výsledek např. 0/0, znamená, že výsledek není číslo ('Not a Number')

// Také existují hodnoty typu bool
true; // pravda
false; // nepravda

// Řetězce znaků jsou obaleny  ' nebo ".
'abc';
"Ahoj světe!";

// Negace se tvoří pomocí !
!true; // = false
!false; // = true

// Rovnost se porovnává ===
1 === 1; // = true
2 === 1; // = false

// Nerovnost zase pomocí !==
1 !== 1; // = false
2 !== 1; // = true

// Další srovnávání
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Řetězce znaků se spojují pomocí +
"Ahoj " + "světe!"; // = "Ahoj světe!"

// ... což funguje nejenom s řetězci
"1, 2, " + 3; // = "1, 2, 3"
"Ahoj " + ["světe", "!"] // = "Ahoj světe,!"

// a porovnávají se pomocí < nebo >
"a" < "b"; // = true

// Rovnost s převodem typů se dělá pomocí == ...
"5" == 5; // = true
null == undefined; // = true

// ...dokud nepoužijete ===
"5" === 5; // = false
null === undefined; // = false

// ...což může občas způsobit divné chování...
13 + !0; // 14
"13" + !0; // '13true'

// Můžeme přistupovat k jednotlivým znakům v řetězci pomocí charAt`
"Toto je řetězec".charAt(0);  // = 'T'

// ...nebo použít `substring` k získání podřetězce
"Ahoj světe".substring(0, 4); // = "Ahoj"

// `length` znamená délka a je to vlastnost, takže nepoužívejte ()
"Ahoj".length; // = 4

// Existují také typy `null` a `undefined`.
null;      // značí, že žádnou hodnotu
undefined; // značí, že hodnota nebyla definovaná (ikdyž
           // `undefined` je hodnota sama o sobě)

// false, null, undefined, NaN, 0 and "" vrací nepravdu (false). Všechno ostatní
// vrací pravdu (true)..
// Všimněte si, že 0 vrací nepravdu, ale "0" vrací pravdu, ikdyž 0 == "0"
// vrací pravdu

///////////////////////////////////
// 2. Proměnné, pole a objekty

// Proměnné jsou deklarovány pomocí slůvka `var`. JavaScript je dynamicky
// typovaný, takže nemusíme specifikovat typ. K přiřazení hodnoty se používá
// znak `=`.
var promenna = 5;

// když vynecháte slůvko 'var' nedostanete chybovou hlášku...
jinaPromenna = 10;

// ...ale vaše proměnná bude vytvořena globálně, bude vytvořena  v globálním
// oblasti působnosti, ne jenom v lokálním tam, kde jste ji vytvořili

// Proměnné vytvořené bez přiřazení obsahují hodnotu undefined.
var dalsiPromenna; // = undefined

// Pokud chcete vytvořit několik proměnných najednou, můžete je oddělit čárkou
var someFourthVar = 2, someFifthVar = 4;

// Existuje kratší forma pro matematické operace na proměnné
promenna += 5; // se provede stejně jako promenna = promenna + 5;
// promenna je ted 10
promenna *= 10; // teď je promenna rovna 100

// a tohle je způsob, jak přičítat a odečítat 1
promenna++; // teď je promenna 101
promenna--; // zpět na 100

// Pole jsou uspořádané seznamy hodnot jakéhokoliv typu
var mojePole = ["Ahoj", 45, true];

// Jednotlivé hodnoty jsou přístupné přes hranaté závorky.
// Členové pole se začínají počítat na nule.
myArray[1]; // = 45

// Pole je proměnlivé délky a členové se můžou měnit
myArray.push("Světe");
myArray.length; // = 4

// Přidání/změna na specifickém indexu
myArray[3] = "Hello";

// JavaScriptové objekty jsou stejné jako asociativní pole v jinných programovacích
// jazycích: je to neuspořádaná množina páru hodnot - klíč:hodnota.
var mujObjekt = {klic1: "Ahoj", klic2: "světe"};

// Klíče jsou řetězce, ale nejsou povinné uvozovky, pokud jsou validní
// JavaScriptové identifikátory. Hodnoty můžou být jakéhokoliv typu-
var mujObjekt = {klic: "mojeHodnota", "muj jiny klic": 4};

// K hodnotám můžeme přistupovat opět pomocí hranatých závorek
myObj["muj jiny klic"]; // = 4

// ... nebo pokud je klíč platným identifikátorem, můžeme přistupovat k
// hodnotám i přes tečku
mujObjekt.klic; // = "mojeHodnota"

// Objekty jsou měnitelné, můžeme upravit hodnoty, nebo přidat nové klíče.
myObj.mujDalsiKlic = true;

// Pokud se snažíte přistoupit ke klíči, který není nastaven, dostanete undefined
myObj.dalsiKlic; // = undefined

///////////////////////////////////
// 3. Řízení toku programu

// Syntaxe pro tuto sekci je prakticky stejná jako pro Javu

// `if` (když) funguje, jak byste čekali.
var pocet = 1;
if (pocet == 3){
    // provede, když se pocet rovná 3
} else if (pocet == 4){
    // provede, když se pocet rovná 4
} else {
    // provede, když je pocet cokoliv jinného
}

// Stejně tak cyklus while
while (true){
    // nekonečný cyklus
}

// Do-while cyklus je stejný jako while, akorát se vždy provede aspoň jednou
var vstup;
do {
    vstup = nactiVstup();
} while (!jeValidni(vstup))

// Cyklus for je stejný jako v Javě nebo jazyku C
// inicializace; podmínka pro pokračování; iterace.
for (var i = 0; i < 3; i++){
    // provede třikrát
}

// Cyklus For-in iteruje přes každo vlastnost prototypu
var popis = "";
var osoba = {prijmeni:"Paul", jmeno:"Ken", vek:18};
for (var x in osoba){
    popis += osoba[x] + " ";
}

//Když chcete iterovat přes vlastnosti, které jsou přímo na objektu a nejsou
//zděněné z prototypů, kontrolujte vlastnosti přes hasOwnProperty()
var popis = "";
var osoba = {prijmeni:"Jan", jmeno:"Novák", vek:18};
for (var x in osoba){
    if (osoba.hasOwnProperty(x)){
        popis += osoba[x] + " ";
    }
}

// for-in by neměl být použit pro pole, pokud záleží na pořadí indexů.
// Neexistuje jistota, že for-in je vrátí ve správném pořadí.

// && je logické a, || je logické nebo
if (dum.velikost == "velký" && dum.barva == "modrá"){
    dum.obsahuje = "medvěd";
}
if (barva == "červená" || barva == "modrá"){
    // barva je červená nebo modtrá
}

// && a || jsou praktické i pro nastavení základních hodnot
var jmeno = nejakeJmeno || "default";


// `switch` zkoumá přesnou rovnost (===)
// Používejte 'break;' po každé možnosti, jinak se provede i možnost za ní.
znamka = 'B';
switch (znamka) {
  case 'A':
    console.log("Výborná práce");
    break;
  case 'B':
    console.log("Dobrá práce");
    break;
  case 'C':
    console.log("Dokážeš to i lépe");
    break;
  default:
    console.log("Ale ne");
    break;
}

////////////////////////////////////////////////////////
// 4. Funckce, Oblast platnosti (scope) a Vnitřní funkce

// JavaScriptové funkce jsou definovány slůvkem `function`.
function funkce(text){
    return text.toUpperCase();
}
funkce("něco"); // = "NĚCO"

// Dávejte si pozor na to, že hodnota k vrácení musí začínat na stejné řádce
// jako slůvko return, jinak se vrátí 'undefined', kvůli automatickému vkládání
// středníků. Platí to zejména pro Allmanův styl zápisu.

function funkce()
{
    return // <- zde je automaticky vložen středník
    {
        tohleJe: "vlastnost objektu"
    }
}
funkce(); // = undefined

// JavaScriptové funkce jsou objekty, takže můžou být přiřazeny různým proměnným
// a předány dalším funkcím jako argumenty, na příklad:
function funkce(){
    // tento kód bude zavolán za 5 vteřin
}
setTimeout(funkce, 5000);
// Poznámka: setTimeout není část JS jazyka, ale funkce poskytována
// prohlížeči a NodeJS

// Další funkce poskytovaná prohlížeči je je setInterval
function myFunction(){
    // tento kód bude volán každých 5 vteřin
}
setInterval(myFunction, 5000);

// Objekty funkcí nemusíme ani deklarovat pomocí jména, můžeme je napsat jako
// ananymní funkci přímo vloženou jako argument
setTimeout(function(){
  // tento kód bude zavolán za 5 vteřin    
}, 5000);

// JavaScript má oblast platnosti funkce, funkce ho mají, ale jiné bloky ne
if (true){
    var i = 5;
}
i; // = 5 - ne undefined, jak byste očekávali v jazyku, kde mají bloky svůj
// rámec působnosti

// Toto je běžný model,který chrání před únikem dočasných proměnných do
//globální oblasti
(function(){
    var docasna = 5;
    // Můžeme přistupovat k globálního oblasti přes přiřazování globalním
    // objektům. Ve webovém prohlížeči je to vždy 'window`. Globální objekt
    // může mít v jiných prostředích jako Node.js jinné jméno.
    window.trvala = 10;
})();
docasna; // způsobí ReferenceError
trvala; // = 10

// Jedna z nejvice mocných vlastnosti JavaScriptu je vnitřní funkce. Je to funkce
// definovaná v jinné funkci, vnitřní funkce má přístup ke všem proměnným ve
// vnější funkci, dokonce i poté, co funkce skončí
function ahojPoPetiVterinach(jmeno){
    var prompt = "Ahoj, " + jmeno + "!";
    // Vnitřní funkce je dána do lokální oblasti platnosti, jako kdyby byla
    // deklarovaná slůvkem 'var'
    function vnitrni(){
        alert(prompt);
    }
    setTimeout(vnitrni, 5000);
    // setTimeout je asynchronní, takže funkce ahojPoPetiVterinach se ukončí
    // okamžitě, ale setTimeout zavolá funkci vnitrni až poté. Avšak protože
    // vnitrni je definována přes ahojPoPetiVterinach, má pořád přístup k
    // proměnné prompt, když je konečně zavolána.
}
ahojPoPetiVterinach("Adam"); // otevře popup s  "Ahoj, Adam!" za 5s

///////////////////////////////////////////////////
// 5. Více o objektech, konstuktorech a prototypech

// Objekty můžou obsahovat funkce
var mujObjekt = {
    mojeFunkce: function(){
        return "Ahoj světe!";
    }
};
mujObjekt.mojeFunkce(); // = "Ahoj světe!"

// Když jsou funkce z objektu zavolány, můžou přistupovat k objektu přes slůvko
// 'this''
var mujObjekt = {
    text: "Ahoj světe!",
    mojeFunkce: function(){
        return this.text;
    }
};
mujObjekt.mojeFunkce(); // = "Ahoj světe!"

// Slůvko this je nastaveno k tomu, kde je voláno, ne k tomu, kde je definováno
// Takže naše funkce nebude fungovat, když nebude v kontextu objektu.
var mojeFunkce = mujObjekt.mojeFunkce;
mojeFunkce(); // = undefined

// Opačně, funkce může být přiřazena objektu a může přistupovat k objektu přes
// this, i když nebyla přímo v definici-
var mojeDalsiFunkce = function(){
    return this.text.toUpperCase();
}
mujObjekt.mojeDalsiFunkce = mojeDalsiFunkce;
mujObjekt.mojeDalsiFunkce(); // = "AHOJ SVĚTE!"

// Můžeme také specifikovat, v jakém kontextu má být funkce volána pomocí
// `call` nebo `apply`.

var dalsiFunkce = function(s){
    return this.text + s;
}
dalsiFunkce.call(mujObjekt, " A ahoj měsíci!"); // = "Ahoj světe! A ahoj měsíci!"

// Funkce `apply`je velmi podobná, akorát bere jako druhý argument pole argumentů
dalsiFunkce.apply(mujObjekt, [" A ahoj slunce!"]); // = "Ahoj světe! A ahoj slunce!"

// To je praktické, když pracujete s funkcí, která bere sekvenci argumentů a
// chcete předat pole.

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN
Math.min.apply(Math, [42, 6, 27]); // = 6

// Ale `call` a `apply` jsou pouze dočasné. Pokud je chcete připojit trvale
// použijte `bind`.

var pripojenaFunkce = dalsiFunkce.bind(mujObjekt);
pripojenaFunkce(" A ahoj Saturne!"); // = "Ahoj světe! A ahoj Saturne!"

// `bind` může být použito čatečně částečně i k používání

var nasobeni = function(a, b){ return a * b; }
var zdvojeni = nasobeni.bind(this, 2);
zdvojeni(8); // = 16

// Když zavoláte funkci se slůvkem 'new', vytvoří se nový objekt a
// a udělá se dostupný funkcím skrz slůvko 'this'. Funkcím volaným takto se říká
// konstruktory

var MujKonstruktor = function(){
    this.mojeCislo = 5;
}
mujObjekt = new MujKonstruktor(); // = {mojeCislo: 5}
mujObjekt.mojeCislo; // = 5

// Každý JsavaScriptový objekt má prototyp. Když budete přistupovat k vlasnosti
// objektu, který neexistuje na objektu, tak se JS koukne do prototypu.

// Některé JS implementace vám umožní přistupovat k prototypu přes magickou
// vlastnost '__proto__'. I když je toto užitečné k vysvětlování prototypů, není
// to součást standardu, ke standartní způsobu k používání prototypu se dostaneme
// později.
var mujObjekt = {
    mujText: "Ahoj svete!"
};
var mujPrototyp = {
    smyslZivota: 42,
    mojeFunkce: function(){
        return this.mujText.toLowerCase()
    }
};

mujObjekt.__proto__ = mujPrototyp;
mujObjekt.smyslZivota; // = 42

// Toto funguje i pro funkce
mujObjekt.mojeFunkce(); // = "Ahoj světe!"

// Samozřejmě, pokud není vlastnost na vašem prototypu, tak se hledá na
// prototypu od prototypu atd.
mujPrototyp.__proto__ = {
    mujBoolean: true
};
mujObjekt.mujBoolean; // = true


// Zde neni žádné kopírování; každý objekt ukládá referenci na svůj prototyp
// Toto znamená, že můžeme měnit prototyp a změny se projeví všude
mujPrototyp.smyslZivota = 43;
mujObjekt.smyslZivota // = 43

// Zmínili jsme již předtím, že '__proto__' není ve standardu a není cesta, jak
// měnit prototyp existujícího objektu. Avšak existují možnosti, jak vytvořit
// nový objekt s daným prototypem

// První je Object.create, což je nedávný přídavek do JS a není dostupný zatím
// ve všech implementacích.
var mujObjekt = Object.create(mujPrototyp);
mujObjekt.smyslZivota // = 43

// Druhý způsob, který funguje všude je pomocí konstuktoru. Konstruktor má
// vlastnost jménem prototype. Toto *není* prototyp samotného konstruktoru, ale
// prototyp nového objektu.
MujKonstruktor.prototype = {
    mojeCislo: 5,
    ziskejMojeCislo: function(){
        return this.mojeCislo;
    }
};
var mujObjekt2 = new MujKonstruktor();
mujObjekt2.ziskejMojeCislo(); // = 5
mujObjekt2.mojeCislo = 6
mujObjekt2.ziskejMojeCislo(); // = 6

// Vestavěnné typy jako čísla nebo řetězce mají také konstruktory, které vytváří
// ekvivalentní obalovací objekty (wrappery).
var mojeCislo = 12;
var mojeCisloObj = new Number(12);
mojeCislo == mojeCisloObj; // = true

// Avšak nejsou úplně přesně stejné
typeof mojeCislo; // = 'number'
typeof mojeCisloObj; // = 'object'
mojeCislo === mojeCisloObj; // = false
if (0){
    // Tento kód se nespustí, protože 0 je nepravdivá (false)
}

if (new Number(0)){
   // Tento kód se spustí, protože obalená čísla jsou objekty,
   // a objekty jsou vždy pravdivé
}

// Avšak, obalovací objekty a normální vestavěnné typy sdílejí prototyp, takže
// můžete přidat funkcionalitu k řetězci
String.prototype.prvniZnak = function(){
    return this.charAt(0);
}
"abc".prvniZnak(); // = "a"

// Tento fakt je často používán v polyfillech, což je implementace novějších
// vlastností JavaScriptu do starších variant, takže je můžete používat třeba
// ve starých prohlížečích

// Pro příkklad, zmínili jsme, že Object.create není dostupný ve všech
// implementacích, můžeme si avšak přidat pomocí polyfillu
if (Object.create === undefined){ // nebudeme ho přepisovat, když existuje
    Object.create = function(proto){
        // vytvoříme dočasný konstruktor
        var Constructor = function(){};
        Constructor.prototype = proto;
        // ten použijeme k vytvoření nového s prototypem
        return new Constructor();
    }
}
```

## Kam dál

[Mozilla Developer
Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript) obsahuje
perfektní dokumentaci pro JavaScript, který je používaný v prohlížečích. Navíc
je to i wiki, takže jakmile se naučíte více, můžete pomoci ostatním, tím, že
přispějete svými znalostmi.

MDN's [A re-introduction to
JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
pojednává o konceptech vysvětlených zde v mnohem větší hloubce. Tento návod
pokrývá hlavně JavaScript sám o sobě. Pokud se chcete naučit více, jak se používá
na webových stránkách, začněte tím, že se kouknete na [DOM](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)

[Learn Javascript by Example and with Challenges](http://www.learneroo.com/modules/64/nodes/350) je varianta tohoto
návodu i s úkoly-

[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) je sbírka
příkladů těch nejvíce nepředvídatelných částí tohoto jazyka.

[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/)
je klasická výuková kniha.

Jako dodatek k přímým autorům tohoto článku, některý obsah byl přizpůsoben z
Pythoního tutoriálu od Louie Dinh na této stráce, a z [JS
Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
z Mozilla Developer Network.
---
language: json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
    - ["Vojta Svoboda", "https://github.com/vojtasvoboda/"]
filename: learnjson-cz.json
lang: cs-cz
---

JSON je exterémně jednoduchý datově nezávislý formát a bude asi jeden z 
nejjednodušších 'Learn X in Y Minutes' ze všech.

JSON nemá ve své nejzákladnější podobě žádné komentáře, ale většina parserů 
umí pracovat s komentáři ve stylu jazyka C (`//`, `/* */`). Pro tyto účely 
však budeme používat 100% validní JSON bez komentářů. Pojďme se podívat na 
syntaxi formátu JSON:

```json
{
  "klic": "value",
  
  "hodnoty": "Musí být vždy uvozený v dvojitých uvozovkách",
  "cisla": 0,
  "retezce": "Hellø, wørld. Všechny unicode znaky jsou povolené, společně s \"escapováním\".",
  "pravdivostni_hodnota": true,
  "prazdna_hodnota": null,

  "velke_cislo": 1.2e+100,

  "objekt": {
    "komentar": "Most of your structure will come from objects.",

    "pole": [0, 1, 2, 3, "Pole nemusí být pouze homogenní.", 5],

    "jiny_objekt": {
      "comment": "Je povolené jakkoli hluboké zanoření."
    }
  },

  "cokoli": [
    {
      "zdroje_drasliku": ["banány"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "alternativni_styl_zapisu": {
    "komentar": "Mrkni se na toto!"
  , "pozice_carky": "Na pozici čárky nezáleží - pokud je před hodnotou, ať už je kdekoli, tak je validní."
  , "dalsi_komentar": "To je skvělé."
  },

  "to_bylo_rychle": "A tím jsme hotový. Nyní již víte vše, co může formát JSON nabídnout!"
}
```
---
language: markdown
lang: cs-cz
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Michal Martinek", "https://github.com/MichalMartinek"]
filename: markdown-cz.md
lang: cs-cz
---

Markdown byl vytvořen Johnem Gruberem v roce 2004. Je zamýšlen jako lehce čitelná
a psatelná syntaxe, která je jednoduše převeditelná do HTML (a dnes i do mnoha
dalších formátů)

```markdown
<!-- Markdown je nadstavba nad HTML, takže jakýkoliv kód HTML je validní
Markdown, to znamená, že můžeme používat HTML elementy, třeba jako komentář, a
nebudou ovlivněny parserem Markdownu. Avšak, pokud vytvoříte HTML element v
Markdownu, tak nemůžete používat syntaxi Markdownu uvnitř tohoto elementu. -->

<!-- Markdown se také mírně liší v jednotlivých interpretacích parseru. Tento
návod vás bude upozorňovat, které vlastnosti jsou obecné a které specifické pro
konkrétní parser. -->

<!-- Nadpisy -->
<!-- Můžete vytvořit HTML elementy <h1> až <h6> jednoduše tak, že text předsadíte
počtem křížků (#), podle toho jaké úrovně to má být nadpis -->
# Toto je <h1>
## Toto je <h2>
### Toto je <h3>
#### Toto je <h4>
##### Toto je <h5>
###### Toto je <h6>

<!-- Markdown obsahuje taky dvě další cesty, jak udělat h1 a h2 -->
Toto je h1
==========

Toto je h2
----------

<!-- Jednoduché stylování textu -->
<!-- Pomocí markdownu můžete text jednoduše označit jako kurzívu či tučný -->

*Tento text je kurzívou;*
_Stejně jako tento._

**Tento text je tučně**
__Stejně jako tento.__

***Tento text je obojí***
**_Jako tento!_**
*__A tento!__*

<!-- Ve verzi Markdownu od GitHubu, máme k dispozici taky prošktrnutí: -->

~~Tento text je prošktrnutý.~~

<!-- Odstavce jsou jedna nebo více řádek textu, oddělených jednou nebo více prázdnými řádky. -->

Toto je odstavec. Píši odstavec, není to zábava?

Teď jsem v odstavci 2.
Jsem pořád v odstavci 2!


Toto je odstavec 3.

<!-- Chtěli jste někdy vložit znak <br /> tag? Můžete napsat na konec odstavce
dvě nebo více mezer a potom začít nový odstavec. -->

Tento odstavec končí dvěma mezerami.  

Nad tímto odstavcem je  <br />!

<!-- Blokové citace se dělají jednoduše pomocí znaku >. -->

> Toto je bloková citace. Můžete dokonce
> manuálně rozdělit řádky, a před každý vložit >, nebo nechat vaše řádky jakkoliv dlouhé, ať se zarovnají sami.
> Nedělá to rozdíl, dokud začínáte vždy znakem >.

> Můžu použít více než jednu
>> odsazení?
> Jak je to úhledné, že?

<!-- Seznamy -->
<!-- Nečíslovaný seznam můžete jednoduše udělat pomocí hvězdiček, plusů, nebo
 pomlček -->

* Položka
* Položka
* Jinná položka

nebo

+ Položka
+ Položka
+ Další položka

nebo

- Položka
- Položka
- Další položka

<!-- Číslovaný seznam se dělají pomocí čísla a . -->

1. Položka jedna
2. Položka dvě
3. Položka tři

<!-- Nemusíte dokonce psát čísla správně a markdown je zobrazi správně,
     ale nemusí to být vždy dobrý nápad -->

1. Položka jedna
1. Položka dvě
1. Položka tři
<!-- (Toto zobrazí to samě, jako příklad nadtím.) -->

<!-- Můžete také tvořit podseznamy -->

1. Položka jedna
2. Položka dvě
3. Položka tři
    * Podpoložka
    * Podpoložka
4. Položka čtyři

<!-- Existují i zašktávací seznamy. Toto vytvoří HTML checkboxy. -->

Boxy níže bez 'x' jsou nezašktrnuté checkboxy.
- [ ] První úkol
- [ ] Druhý úkol
Tento box bude zašktrnutý
- [x] Tento úkol byl dokončen

<!-- Bloky ködu -->
<!-- Můžete označit kód bloku (který používá <code> element) odsazením pomocí 4
     mezer, nebo tabu -->

    Toto je kód
    Stejně jako toto

<!-- Můžete dokonce přidat další 4 mezery nebo tab pro další odsazení -->

    moje_pole.each do |i|
        puts i
    end

<!-- Kód na řádku může být označen pomocí zpětných apostrofů ` -->

Jan nevědel, jak se dělá `go_to()` funkce!

<!-- V Markdownu od GitHubu , můžete použít speciální syntaxi pro kód -->

\`\`\`ruby <!-- vyjma zpětných lomítek, jenom ```ruby ! -->
def neco
    puts "Ahoj světe!"
end
\`\`\` <!-- zde taky, žádné zpětná lomítka, pouze ``` -->

<!-- Text výše nepotřebuje odsazení a navíc GitHub použije zvýraznění označeného
 jazyka. -->

<!-- Horizontální čára (<hr />) -->
<!-- Horizontální čára se jednoduše přidá pomocí 3 nebo více hvězdiček nebo pomlček
s nebo bez mezer. -->

***
---
- - -
****************

<!-- Odkazy -->
<!-- Jedna z nejlepších věcí na Markdownu je, jak jednoduše se dělají odkazy.
Dejte text, který chcete zobrazit, do [] následovaný url v závorkách () a je to. -->

[Klikni na mě!](http://test.com/)

<!-- Můžete také přidat jméno linku pomocí uvozovek -->

[Klikni na mě!](http://test.com/ "Odkaz na Test.com")

<!-- Relativní cesty fungují taky -->

[Jdi na hudbu](/hudba/).

<!-- Markdown taktéž podporuje reference odkazů. -->

[Klikni na tento odkaz][link1] pro více informací!
[Taky zkontrolujte tento odkaz][neco], když chcete.

[link1]: http://test.com/ "Cool!"
[neco]: http://neco.czz/ "Dobře!"

<!-- Titulek může být v apostrofech nebo závorkách, nebo vyjmutý úplně. Reference
 může být kdekoliv ve vašem dokumentu a identifikátor může být jakýkoliv, dokud
 je unikátní.-->

<!-- Také existuje "implicitní pojmenování", které použije text jako id -->

[Toto][] je odkaz..

[toto]: http://totojelink.cz/

<!-- Ale toto není zrovna běžné užívané. -->

<!-- Obrázky -->
<!-- Obrázky se dělají stejně jako odkazy, ale s vykřičníkem na začátku -->

![Toto je atribut alt pro obrázek](http://imgur.com/myimage.jpg "Nepovinný titulek")

<!-- Reference fungují, jak bychom čekali-->

![Toto je atribut alt][mujobrazek]

[mujobrazek]: relativni/cesta/obrazek.jpg "a toto by byl titulek"

<!-- Ostatní -->
<!-- Automatické odkazy -->

<http://stranka.cz/> je stejná jako
[http://stranka.cz/](http://stranka.cz/)

<!-- Automatické odkazy pro emaily-->

<jmeno@prijmeni.cz>

<!-- Escapování znaků -->

Chci napsat *tento text obklopený hvězdičkami*, ale nechci aby to bylo kurzívou, tak udělám: \*tento text obklopený hvězdičkami\*.

<!-- Klávesové zkratky -->
<!-- V Markdownu od GitHubu, můžete použít tag <kbd> k reprezentování klaves na počítači -->

Váš počítač přestal pracovat? Zkuste
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>

<!-- Tabulky -->
<!-- Tabulky jsou povolené pouze v Markdownu od GitHubu a jsou trochu podivně,
     ale když je opravdu chcete: -->

| Sloupec1     | Sloupec2 | Sloupec3      |
| :----------- | :------: | ------------: |
| Vlevo zarovn.| Na střed | Vpravo zarovn.|
| blah         | blah     | blah          |

<!-- nebo, to jde i taky: -->

Sloupec 1 | Sloupec2 | Sloupec3
:-- | :-: | --:
Ohh toto je tak ošklivé | radši to | nedělejte

<!-- Konec -->

```

Pro více informací, prozkoumejte oficiální článek o syntaxi od Johna Grubera
 [zde](http://daringfireball.net/projects/markdown/syntax) a skvělý tahák od Adama Pritcharda [zde](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["Tomáš Bedřich", "http://tbedrich.cz"]
translators:
    - ["Tomáš Bedřich", "http://tbedrich.cz"]
filename: learnpython3-cz.py
lang: cs-cz
---

Python byl vytvořen Guidem Van Rossum v raných 90. letech. Nyní je jedním z nejpopulárnějších jazyků.
Zamiloval jsem si Python pro jeho syntaktickou čistotu - je to vlastně spustitelný pseudokód.

Vaše zpětná vazba je vítána! Můžete mě zastihnout na [@louiedinh](http://twitter.com/louiedinh) nebo louiedinh [at] [email od googlu] anglicky,
autora českého překladu pak na [@tbedrich](http://twitter.com/tbedrich) nebo ja [at] tbedrich.cz

Poznámka: Tento článek je zaměřen na Python 3. Zde se můžete [naučit starší Python 2.7](http://learnxinyminutes.com/docs/python/).

```python

# Jednořádkový komentář začíná křížkem

""" Víceřádkové komentáře používají tři uvozovky nebo apostrofy
    a jsou často využívány jako dokumentační komentáře k metodám
"""

####################################################
## 1. Primitivní datové typy a operátory
####################################################

# Čísla
3  # => 3

# Aritmetické operace se chovají běžným způsobem
1 + 1   # =>  2
8 - 1   # =>  7
10 * 2  # => 20

# Až na dělení, které vrací desetinné číslo
35 / 5  # => 7.0

# Při celočíselném dělení je desetinná část oříznuta (pro kladná i záporná čísla)
5 // 3       # => 1
5.0 // 3.0   # => 1.0  #  celočíselně dělit lze i desetinným číslem
-5 // 3      # => -2
-5.0 // 3.0  # => -2.0

# Pokud použijete desetinné číslo, výsledek je jím také
3 * 2.0  # => 6.0

# Modulo
7 % 3  # => 1

# Mocnění (x na y-tou)
2**4  # => 16

# Pro vynucení priority použijte závorky
(1 + 3) * 2  # => 8

# Logické hodnoty
True
False

# Negace se provádí pomocí not
not True   # => False
not False  # => True

# Logické operátory
# U operátorů záleží na velikosti písmen
True and False  # => False
False or True   # => True

# Používání logických operátorů s čísly
0 and 2     # => 0
-5 or 0     # => -5
0 == False  # => True
2 == True   # => False
1 == True   # => True

# Rovnost je ==
1 == 1  # => True
2 == 1  # => False

# Nerovnost je !=
1 != 1  # => False
2 != 1  # => True

# Další porovnání
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# Porovnání se dají řetězit!
1 < 2 < 3  # => True
2 < 3 < 2  # => False


# Řetězce používají " nebo ' a mohou obsahovat UTF8 znaky
"Toto je řetězec."
'Toto je také řetězec.'

# Řetězce se také dají sčítat, ale nepoužívejte to
"Hello " + "world!"  # => "Hello world!"
# Dají se spojovat i bez '+'
"Hello " "world!"  # => "Hello world!"

# Řetězec lze považovat za seznam znaků
"Toto je řetězec"[0]  # => 'T'

# .format lze použít ke skládání řetězců
"{} mohou být {}".format("řetězce", "skládány")

# Formátovací argumenty můžete opakovat
"{0} {1} stříkaček stříkalo přes {0} {1} střech".format("tři sta třicet tři", "stříbrných")
# => "tři sta třicet tři stříbrných stříkaček stříkalo přes tři sta třicet tři stříbrných střech"

# Pokud nechcete počítat, můžete použít pojmenované argumenty
"{jmeno} si dal {jidlo}".format(jmeno="Franta", jidlo="guláš")  # => "Franta si dal guláš"

# Pokud zároveň potřebujete podporovat Python 2.5 a nižší, můžete použít starší způsob formátování
"%s se dají %s jako v %s" % ("řetězce", "skládat", "jazyce C")


# None je objekt (jinde NULL, nil, ...)
None  # => None

# Pokud porovnáváte něco s None, nepoužívejte operátor rovnosti "==",
# použijte raději operátor "is", který testuje identitu.
"něco" is None  # => False
None is None    # => True

# None, 0, a prázdný řetězec/seznam/slovník se vyhodnotí jako False
# Vše ostatní se vyhodnotí jako True
bool(0)   # => False
bool("")  # => False
bool([])  # => False
bool({})  # => False


####################################################
## 2. Proměnné a kolekce
####################################################

# Python má funkci print
print("Jsem 3. Python 3.")

# Proměnné není třeba deklarovat před přiřazením
# Konvence je používat male_pismo_s_podtrzitky
nazev_promenne = 5
nazev_promenne  # => 5
# Názvy proměnných mohou obsahovat i UTF8 znaky
název_proměnné = 5

# Přístup k předtím nepoužité proměnné vyvolá výjimku
# Odchytávání vyjímek - viz další kapitola
neznama_promenna  # Vyhodí NameError

# Seznam se používá pro ukládání sekvencí
sez = []
# Lze ho rovnou naplnit
jiny_seznam = [4, 5, 6]

# Na konec seznamu se přidává pomocí append
sez.append(1)    # sez je nyní [1]
sez.append(2)    # sez je nyní [1, 2]
sez.append(4)    # sez je nyní [1, 2, 4]
sez.append(3)    # sez je nyní [1, 2, 4, 3]
# Z konce se odebírá se pomocí pop
sez.pop()        # => 3 a sez je nyní [1, 2, 4]
# Vložme trojku zpátky
sez.append(3)    # sez je nyní znovu [1, 2, 4, 3]

# Přístup k prvkům funguje jako v poli
sez[0]  # => 1
# Mínus počítá odzadu (-1 je poslední prvek)
sez[-1]  # => 3

# Přístup mimo seznam vyhodí IndexError
sez[4]  # Vyhodí IndexError

# Pomocí řezů lze ze seznamu vybírat různé intervaly
# (pro matematiky: jedná se o uzavřený/otevřený interval)
sez[1:3]  # => [2, 4]
# Odříznutí začátku
sez[2:]  # => [4, 3]
# Odříznutí konce
sez[:3]  # => [1, 2, 4]
# Vybrání každého druhého prvku
sez[::2]  # =>[1, 4]
# Vrácení seznamu v opačném pořadí
sez[::-1]  # => [3, 4, 2, 1]
# Lze použít jakoukoliv kombinaci parametrů pro vytvoření složitějšího řezu
# sez[zacatek:konec:krok]

# Odebírat prvky ze seznamu lze pomocí del
del sez[2]   # sez je nyní [1, 2, 3]

# Seznamy můžete sčítat
# Hodnoty sez a jiny_seznam přitom nejsou změněny
sez + jiny_seznam   # => [1, 2, 3, 4, 5, 6]

# Spojit seznamy lze pomocí extend
sez.extend(jiny_seznam)   # sez je nyní [1, 2, 3, 4, 5, 6]

# Kontrola, jestli prvek v seznamu existuje, se provádí pomocí in
1 in sez  # => True

# Délku seznamu lze zjistit pomocí len
len(sez)  # => 6


# N-tice je jako seznam, ale je neměnná
ntice = (1, 2, 3)
ntice[0]      # => 1
ntice[0] = 3  # Vyhodí TypeError

# S n-ticemi lze dělat většinu operací, jako se seznamy
len(ntice)         # => 3
ntice + (4, 5, 6)  # => (1, 2, 3, 4, 5, 6)
ntice[:2]          # => (1, 2)
2 in ntice         # => True

# N-tice (nebo seznamy) lze rozbalit do proměnných jedním přiřazením
a, b, c = (1, 2, 3)  # a je nyní 1, b je nyní 2 a c je nyní 3
# N-tice jsou vytvářeny automaticky, když vynecháte závorky
d, e, f = 4, 5, 6
# Prohození proměnných je tak velmi snadné
e, d = d, e  # d je nyní 5, e je nyní 4


# Slovníky ukládají klíče a hodnoty
prazdny_slovnik = {}
# Lze je také rovnou naplnit
slovnik = {"jedna": 1, "dva": 2, "tři": 3}

# Přistupovat k hodnotám lze pomocí []
slovnik["jedna"]  # => 1

# Všechny klíče dostaneme pomocí keys() jako iterovatelný objekt. Nyní ještě
# potřebujeme obalit volání v list(), abychom dostali seznam. To rozebereme
# později. Pozor, že jakékoliv pořadí klíčů není garantováno - může být různé.
list(slovnik.keys())  # => ["dva", "jedna", "tři"]

# Všechny hodnoty opět jako iterovatelný objekt získáme pomocí values(). Opět
# tedy potřebujeme použít list(), abychom dostali seznam. Stejně jako
# v předchozím případě, pořadí není garantováno a může být různé
list(slovnik.values())  # => [3, 2, 1]

# Operátorem in se lze dotázat na přítomnost klíče
"jedna" in slovnik  # => True
1 in slovnik        # => False

# Přístup k neexistujícímu klíči vyhodí KeyError
slovnik["čtyři"]  # Vyhodí KeyError

# Metoda get() funguje podobně jako [], ale vrátí None místo vyhození KeyError
slovnik.get("jedna")   # => 1
slovnik.get("čtyři")   # => None
# Metodě get() lze předat i výchozí hodnotu místo None
slovnik.get("jedna", 4)   # => 1
slovnik.get("čtyři", 4)   # => 4

# metoda setdefault() vloží prvek do slovníku pouze pokud tam takový klíč není
slovnik.setdefault("pět", 5)  # slovnik["pět"] je nastaven na 5
slovnik.setdefault("pět", 6)  # slovnik["pět"] je pořád 5

# Přidání nové hodnoty do slovníku
slovnik["čtyři"] = 4
# Hromadně aktualizovat nebo přidat data lze pomocí update(), parametrem je opět slovník
slovnik.update({"čtyři": 4})  # slovnik je nyní {"jedna": 1, "dva": 2, "tři": 3, "čtyři": 4, "pět": 5}

# Odebírat ze slovníku dle klíče lze pomocí del
del slovnik["jedna"]  # odebere klíč "jedna" ze slovnik


# Množiny ukládají ... překvapivě množiny
prazdna_mnozina = set()
# Také je lze rovnou naplnit. A ano, budou se vám plést se slovníky. Bohužel.
mnozina = {1, 1, 2, 2, 3, 4}  # mnozina je nyní {1, 2, 3, 4}

# Přidání položky do množiny
mnozina.add(5)  # mnozina je nyní {1, 2, 3, 4, 5}

# Průnik lze udělat pomocí operátoru &
jina_mnozina = {3, 4, 5, 6}
mnozina & jina_mnozina  # => {3, 4, 5}

# Sjednocení pomocí operátoru |
mnozina | jina_mnozina  # => {1, 2, 3, 4, 5, 6}

# Rozdíl pomocí operátoru -
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# Operátorem in se lze dotázat na přítomnost prvku v množině
2 in mnozina  # => True
9 in mnozina  # => False


####################################################
## 3. Řízení toku programu, cykly
####################################################

# Vytvořme si proměnnou
promenna = 5

# Takto vypadá podmínka. Na odsazení v Pythonu záleží!
# Vypíše "proměnná je menší než 10".
if promenna > 10:
    print("proměnná je velká jak Rusko")
elif promenna < 10:  # Část elif je nepovinná
    print("proměnná je menší než 10")
else:                # Část else je také nepovinná
    print("proměnná je právě 10")


"""
Smyčka for umí iterovat (nejen) přes seznamy
vypíše:
    pes je savec
    kočka je savec
    myš je savec
"""
for zvire in ["pes", "kočka", "myš"]:
    # Můžete použít formát pro složení řetězce
    print("{} je savec".format(zvire))

"""
range(cislo) vrací iterovatelný objekt čísel od 0 do cislo
vypíše:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
range(spodni_limit, horni_limit) vrací iterovatelný objekt čísel mezi limity
vypíše:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print(i)

"""
Smyčka while se opakuje, dokud je podmínka splněna.
vypíše:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Zkrácený zápis x = x + 1. Pozor, žádné x++ neexisuje.


# Výjimky lze ošetřit pomocí bloku try/except(/else/finally)
try:
    # Pro vyhození výjimky použijte raise
    raise IndexError("Přistoupil jste k neexistujícímu prvku v seznamu.")
except IndexError as e:
    print("Nastala chyba: {}".format(e))
    # Vypíše: Nastala chyba: Přistoupil jste k neexistujícímu prvku v seznamu.
except (TypeError, NameError):  # Více výjimek lze zachytit najednou
    pass  # Pass znamená nedělej nic - nepříliš vhodný způsob ošetření chyb
else:  # Volitelný blok else musí být až za bloky except
    print("OK!")  # Vypíše OK! v případě, že nenastala žádná výjimka
finally:  # Blok finally se spustí nakonec za všech okolností
    print("Uvolníme zdroje, uzavřeme soubory...")

# Místo try/finally lze použít with pro automatické uvolnění zdrojů
with open("soubor.txt") as soubor:
    for radka in soubor:
        print(radka)

# Python běžně používá iterovatelné objekty, což je prakticky cokoliv,
# co lze považovat za sekvenci. Například to, co vrací metoda range(),
# nebo otevřený soubor, jsou iterovatelné objekty.

slovnik = {"jedna": 1, "dva": 2, "tři": 3}
iterovatelny_objekt = slovnik.keys()
print(iterovatelny_objekt)  # => dict_keys(["jedna", "dva", "tři"]). Toto je iterovatelný objekt.

# Můžeme použít cyklus for na jeho projití
for klic in iterovatelny_objekt:
    print(klic)    # vypíše postupně: jedna, dva, tři

# Ale nelze přistupovat k prvkům pod jejich indexem
iterovatelny_objekt[1]  # Vyhodí TypeError

# Všechny položky iterovatelného objektu lze získat jako seznam pomocí list()
list(slovnik.keys())  # => ["jedna", "dva", "tři"]

# Z iterovatelného objektu lze vytvořit iterátor
iterator = iter(iterovatelny_objekt)

# Iterátor je objekt, který si pamatuje stav v rámci svého iterovatelného objektu
# Další hodnotu dostaneme voláním next()
next(iterator)  # => "jedna"

# Iterátor si udržuje svůj stav v mezi jednotlivými voláními next()
next(iterator)  # => "dva"
next(iterator)  # => "tři"

# Jakmile interátor vrátí všechna svá data, vyhodí výjimku StopIteration
next(iterator)  # Vyhodí StopIteration


####################################################
## 4. Funkce
####################################################

# Pro vytvoření nové funkce použijte klíčové slovo def
def secist(x, y):
    print("x je {} a y je {}".format(x, y))
    return x + y  # Hodnoty se vrací pomocí return

# Volání funkce s parametry
secist(5, 6)  # => Vypíše "x je 5 a y je 6" a vrátí 11

# Jiný způsob, jak volat funkci, je použít pojmenované argumenty
secist(y=6, x=5)  # Pojmenované argumenty můžete předat v libovolném pořadí

# Lze definovat funkce s proměnným počtem (pozičních) argumentů
def vrat_argumenty(*argumenty):
    return argumenty

vrat_argumenty(1, 2, 3)  # => (1, 2, 3)

# Lze definovat také funkce s proměnným počtem pojmenovaných argumentů
def vrat_pojmenovane_argumenty(**pojmenovane_argumenty):
    return pojmenovane_argumenty

vrat_pojmenovane_argumenty(kdo="se bojí", nesmi="do lesa")
# => {"kdo": "se bojí", "nesmi": "do lesa"}


# Pokud chcete, lze použít obojí najednou
# Konvence je používat pro tyto účely názvy *args a **kwargs
def vypis_vse(*args, **kwargs):
    print(args, kwargs)  # print() vypíše všechny své parametry oddělené mezerou

vypis_vse(1, 2, a=3, b=4)  # Vypíše: (1, 2) {"a": 3, "b": 4}

# * nebo ** lze použít k rozbalení N-tic nebo slovníků!
ntice = (1, 2, 3, 4)
slovnik = {"a": 3, "b": 4}
vypis_vse(ntice)  # Vyhodnotí se jako vypis_vse((1, 2, 3, 4)) – jeden parametr, N-tice
vypis_vse(*ntice)  # Vyhodnotí se jako vypis_vse(1, 2, 3, 4)
vypis_vse(**slovnik)  # Vyhodnotí se jako vypis_vse(a=3, b=4)
vypis_vse(*ntice, **slovnik)  # Vyhodnotí se jako vypis_vse(1, 2, 3, 4, a=3, b=4)


# Viditelnost proměnných - vytvořme si globální proměnnou x
x = 5

def nastavX(cislo):
    # Lokální proměnná x překryje globální x
    x = cislo  # => 43
    print(x)  # => 43

def nastavGlobalniX(cislo):
    global x
    print(x)  # => 5
    x = cislo  # Nastaví globální proměnnou x na 6
    print(x)  # => 6

nastavX(43)
nastavGlobalniX(6)


# Funkce jsou first-class objekty
def vyrobit_scitacku(pricitane_cislo):
    def scitacka(x):
        return x + pricitane_cislo
    return scitacka

pricist_10 = vyrobit_scitacku(10)
pricist_10(3)  # => 13

# Klíčové slovo lambda vytvoří anonymní funkci
(lambda parametr: parametr > 2)(3)  # => True

# Lze použít funkce map() a filter() z funkcionálního programování
map(pricist_10, [1, 2, 3])
# => <map object at 0x0123467> - iterovatelný objekt s obsahem: [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7])
# => <filter object at 0x0123467> - iterovatelný objekt s obsahem: [6, 7]

# S generátorovou notací lze dosáhnout podobných výsledků, ale vrací seznam
[pricist_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]   # => [6, 7]
# Generátorová notace funguje i pro slovníky
{x: x**2 for x in range(1, 5)}  # => {1: 1, 2: 4, 3: 9, 4: 16}
# A také pro množiny
{pismeno for pismeno in "abeceda"}  # => {"d", "a", "c", "e", "b"}


####################################################
## 5. Třídy
####################################################

# Třída Clovek je potomkem (dědí od) třídy object
class Clovek(object):

    # Atribut třídy - je sdílený všemi instancemi
    druh = "H. sapiens"

    # Toto je kostruktor. Je volán, když vytváříme instanci třídy. Dvě
    # podtržítka na začátku a na konci značí, že se jedná o atribut nebo
    # objekt využívaný Pythonem ke speciálním účelům, ale můžete sami
    # definovat jeho chování. Metody jako __init__, __str__, __repr__
    # a další se nazývají "magické metody". Nikdy nepoužívejte toto
    # speciální pojmenování pro běžné metody.
    def __init__(self, jmeno):
        # Přiřazení parametru do atributu instance jmeno
        self.jmeno = jmeno

    # Metoda instance - všechny metody instance mají "self" jako první parametr
    def rekni(self, hlaska):
        return "{jmeno}: {hlaska}".format(jmeno=self.jmeno, hlaska=hlaska)

    # Metoda třídy - sdílená všemi instancemi
    # Dostává jako první parametr třídu, na které je volána
    @classmethod
    def vrat_druh(cls):
        return cls.druh

    # Statická metoda je volána bez reference na třídu nebo instanci
    @staticmethod
    def odkaslej_si():
        return "*ehm*"


# Vytvoření instance
d = Clovek(jmeno="David")
a = Clovek("Adéla")
print(d.rekni("ahoj"))    # Vypíše: "David: ahoj"
print(a.rekni("nazdar"))  # Vypíše: "Adéla: nazdar"

# Volání třídní metody
d.vrat_druh()  # => "H. sapiens"

# Změna atributu třídy
Clovek.druh = "H. neanderthalensis"
d.vrat_druh()  # => "H. neanderthalensis"
a.vrat_druh()  # => "H. neanderthalensis"

# Volání statické metody
Clovek.odkaslej_si()  # => "*ehm*"


####################################################
## 6. Moduly
####################################################

# Lze importovat moduly
import math
print(math.sqrt(16.0))  # => 4

# Lze také importovat pouze vybrané funkce z modulu
from math import ceil, floor
print(ceil(3.7))   # => 4.0
print(floor(3.7))  # => 3.0

# Můžete také importovat všechny funkce z modulu, ale radši to nedělejte
from math import *

# Můžete si přejmenovat modul při jeho importu
import math as m
math.sqrt(16) == m.sqrt(16)  # => True

# Modul v Pythonu není nic jiného, než obyčejný soubor .py
# Můžete si napsat vlastní a prostě ho importovat podle jména
from muj_modul import moje_funkce  # Nyní vyhodí ImportError - muj_modul neexistuje

# Funkcí dir() lze zjistit, co modul obsahuje
import math
dir(math)


####################################################
## 7. Pokročilé
####################################################

# Generátory jsou funkce, které místo return obsahují yield
def nasobicka_2(sekvence):
    for i in sekvence:
        yield 2 * i

# Generátor generuje hodnoty postupně, jak jsou potřeba. Místo toho, aby vrátil
# celou sekvenci s prvky vynásobenými dvěma, provádí jeden výpočet v každé iteraci.
# To znamená, že čísla větší než 15 se v metodě nasobicka_2 vůbec nezpracují.

# Funkce range() je také generátor - vytváření seznamu 900000000 prvků by zabralo
# hodně času i paměti, proto se místo toho čísla generují postupně.

for i in nasobicka_2(range(900000000)):
    print(i)  # Vypíše čísla 0, 2, 4, 6, 8, ... 30
    if i >= 30:
        break


# Dekorátory jsou funkce, které se používají pro obalení jiné funkce, čímž mohou
# přidávat nebo měnit její stávající chování. Funkci dostávají jako parametr
# a typicky místo ní vrací jinou, která uvnitř volá tu původní.

def nekolikrat(puvodni_funkce):
    def opakovaci_funkce(*args, **kwargs):
        for i in range(3):
            puvodni_funkce(*args, **kwargs)

    return opakovaci_funkce


@nekolikrat
def pozdrav(jmeno):
    print("Měj se {}!".format(jmeno))

pozdrav("Pepo")  # Vypíše 3x: Měj se Pepo!
```

## Co dál?

Spoustu odkazů na české i anglické materiály najdete na [webu české Python komunity]
(http://python.cz/). Můžete také přijít na Pyvo, kde to společně probereme.
---
language: sass
filename: learnsass-cz.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
  - ["Sean Corrales", "https://github.com/droidenator"]
translators:
  - ["Michal Martinek", "https://github.com/MichalMartinek"]
lang: cs-cz
---

Sass je rozšíření jazyka CSS, který přidává nové vlastnosti jako proměnné, zanořování, mixiny a další.
Sass (a další preprocesory, jako  [Less](http://lesscss.org/)) pomáhají vývojářům psát udržovatelný a neopakující (DRY) kód.

Sass nabízí dvě možnosti syntaxe. SCSS, které je stejná jako CSS, akorát obsahuje nové vlastnosti Sassu. Nebo Sass, který používá odsazení místo složených závorek a středníků.
Tento tutoriál bude používat syntaxi CSS.


Pokud jste již obeznámeni s CSS3, budete schopni používat Sass relativně rychle. Nezprostředkovává nějaké úplně nové stylové možnosti, spíše nátroje, jak psát Vás CSS kód více efektivně, udržitelně a jednoduše.

```scss


//Jednořádkové komentáře jsou ze Sassu při kompilaci vymazány

/*Víceřádkové komentáře jsou naopak zachovány */



/*Proměnné
==============================*/



/* Můžete uložit CSS hodnotu (jako třeba barvu) do proměnné.
Použijte symbol '$' k jejímu vytvoření. */

$hlavni-barva: #A3A4FF;
$sekundarni-barva: #51527F;
$body-font: 'Roboto', sans-serif;

/* Můžete používat proměnné napříč vaším souborem.
Teď, když chcete změnit barvu, stačí ji změnit pouze jednou.*/

body {
	background-color: $hlavni-barva;
	color: $sekundarni-barva;
	font-family: $body-font;
}

/* Toto se zkompiluje do: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/* Toto je o hodně více praktické, než měnit každý výskyt barvy.  */



/*Mixiny
==============================*/



/* Pokud zjistíte, že píšete kód pro více než jeden element, můžete jej uložit do mixinu.

Použijte '@mixin' direktivu, plus jméno vašeho mixinu.*/

@mixin na-stred {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* Mixin vložíte pomocí '@include' a jména mixinu */

div {
	@include na-stred;
	background-color: $hlavni-barva;
}

/*Což se zkompiluje do: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}


/* Můžete využít mixiny i třeba pro takovéto ušetření práce: */

@mixin velikost($sirka, $vyska) {
	width: $sirka;
	height: $vyska;
}

/*Stačí vložit argumenty: */

.obdelnik {
	@include velikost(100px, 60px);
}

.ctverec {
	@include velikost(40px, 40px);
}

/* Toto se zkompiluje do: */
.obdelnik {
  width: 100px;
  height: 60px;
}

.ctverec {
  width: 40px;
  height: 40px;
}



/*Funkce
==============================*/   



/* Sass obsahuje funkce, které vám pomůžou splnit různé úkoly. */

/* Funkce se spouštějí pomocí jejich jména, které následuje seznam argumentů uzavřený v kulatých závorkách. */
body {
  width: round(10.25px);    
}

.footer {
  background-color: fade_out(#000000, 0.25)
}

/* Se zkompiluje do: */

body {
  width: 10px;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}   

/* Můžete také definovat vlastní funkce. Funkce jsou velmi podobné mixinům.
   Když se snažíte vybrat mezi funkcí a mixinem, mějte na paměti, že mixiny
   jsou lepší pro generování CSS kódu, zatímco funkce jsou lepší pro logiku.
   Příklady ze sekce Matematické operátory jsou skvělí kandidáti na
   znovupoužitelné funkce. */

/* Tato funkce vrací poměr k velikosti rodiče v procentech.
@function vypocitat-pomer($velikost, $velikost-rodice) {
  @return $velikost / $velikost-rodice * 100%;
}

$hlavni obsah: vypocitat-pomer(600px, 960px);

.hlavni-obsah {
  width: $hlavni-obsah;
}

.sloupec {
  width: vypocitat-pomer(300px, 960px);
}

/* Zkompiluje do: */

.hlavni-obsah {
  width: 62.5%;
}

.sloupec {
  width: 31.25%;
}



/*Dědění
==============================*/



/*Dědění je způsob jak používat vlastnosti pro jeden selektor ve druhém. */

.oznameni {
	@include velikost(5em, 5em);
	border: 5px solid $sekundarni-barva;
}

.oznameni-uspech {
	@extend .oznameni;
	border-color: #22df56;
}

/* Zkompiluje do: */
.oznameni, .oznameni-uspech {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F;
}

.oznameni-uspech {
  border-color: #22df56;
}


/* Dědění CSS výrazů je preferováno před vytvořením mixinu kvůli způsobu,
   jakým způsobem Sass dává dohromady třídy, které sdílejí stejný kód.
   Kdyby to bylo udělané pomocí mixinu, tak výška, šířka, rámeček by byl v
   každém výrazu, který by volal mixin. I když tohle neovlivní vaše workflow,
   přidá to kód navíc do souborů. */


/*Zanořování
==============================*/



/*Sass vám umožňuje zanořovat selektory do selektorů */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #FF0000;
	}
}

/* '&' nahradí rodičovský element. */
/* Můžete také zanořovat pseudo třídy. */
/* Pamatujte, že moc velké zanoření do hloubky snižuje čitelnost.
   Doporučuje se používat maximálně trojité zanoření.
   Na příklad: */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Zkompiluje do: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/*Částečné soubory a importy
==============================*/   



/* Sass umožňuje vytvářet částečné soubory. Tyto soubory pomahájí udržovat váš
   kód modulární. Tyto soubory by měli začínat vždy '_', např. _reset.css.
   Částečné soubory se nepřevádí do CSS. */

/* Toto je kód, který si uložíme do souboru _reset.css */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Sass obsahuje @import, které může být použit pro import částečných souborů.
   Toto se liší od klasického CSS @import, který dělá HTTP požadavek na stáhnutí
   souboru. Sass vezme importovaný soubor a vloží ho do kompilovaného kódu. */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* Zkompiluje do: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}   

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/*Zástupné selektory
==============================*/  



/* Zástupné selektory jsou užitečné, když vytváříte CSS výraz, ze kterého
   chcete později dědit. Když chcete vytvořit výraz, ze kterého je možné pouze
   dědit pomocí @extend, vytvořte zástupný selektor s CSS výrazem. Ten začíná
   symbolem '%' místo '.' nebo '#'. Tyto výrazy se neobjeví ve výsledném CSS */

%okno-obsahu {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.okno-zpravy {
  @extend %okno-obsahu;
  background-color: #0000ff;
}

/* Zkompiluje do: */

.okno-zpravy {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.okno-zpravy {
  background-color: #0000ff;
}



/*Matematické operace
==============================*/   



/* Sass obsahuje následující operátory: +, -, *, /, and %. Tyto operátory
   můžou být velmi užitečné pro počítání hodnot přímo ve vašem souboru Sass.
   Níže je příklad, jak udělat jednoduchý dvousloupcový layout. */

$oblast-obsahu: 960px;
$hlavni-obsah: 600px;
$vedlejsi-sloupec: 300px;

$obsah-velikost: $hlavni-obsah / $oblast-obsahu * 100%;
$vedlejsi-sloupec-velikost: $vedlejsi-sloupec / $oblast-obsahu * 100%;
$zbytek-velikost: 100% - ($main-size + $vedlejsi-sloupec-size);

body {
  width: 100%;
}

.hlavni-obsah {
  width: $obsah-velikost;
}

.vedlejsi-sloupec {
  width: $vedlejsi-sloupec-velikost;
}

.zbytek {
  width: $zbytek-velikost;
}

/* Zkompiluje do: */

body {
  width: 100%;
}

.hlavni-obsah {
  width: 62.5%;
}

.vedlejsi-sloupec {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}


```



## SASS nebo Sass?
Divili jste se někdy, jestli je Sass zkratka nebo ne? Pravděpodobně ne, ale řeknu vám to stejně. Jméno tohoto jazyka je slovo, "Sass", a ne zkratka.
Protože to lidé konstatně píší jako "SASS", nazval ho autor jazyka jako "Syntactically Awesome StyleSheets" (Syntaktický úžasně styly).


## Procvičování Sassu
Pokud si chcete hrát se Sassem ve vašem prohlížeči, navštivte [SassMeister](http://sassmeister.com/).
Můžete používát oba dva způsoby zápisu, stačí si vybrat v nastavení SCSS nebo SASS.


## Kompatibilita

Sass může být použit v jakémkoliv projektu, jakmile máte program, pomocí kterého ho zkompilujete do CSS. Pokud si chcete ověřit, že CSS, které Sass produkuje je kompatibilní s prohlížeči:

[QuirksMode CSS](http://www.quirksmode.org/css/) a [CanIUse](http://caniuse.com) jsou skvělé stránky pro kontrolu kompatibility.


## Kam dál?
* [Oficiální dokumentace](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way](http://thesassway.com/) obsahuje tutoriál a řadu skvělých článků
---
language: c#
contributors:
    - ["Irfan Charania", "https://github.com/irfancharania"]
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Melvyn Laïly", "http://x2a.yt"]
    - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
    - ["Wouter Van Schandevijl", "http://github.com/laoujin"]
    - ["Jo Pearce", "http://github.com/jdpearce"]
    - ["Chris Zimmerman", "https://github.com/chriszimmerman"]
    - ["Shawn McGuire", "https://github.com/bigbash"]
filename: LearnCSharp.cs
---

C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework.

[Read more here.](http://msdn.microsoft.com/en-us/library/vstudio/z1zx9t92.aspx)

```c#
// Single-line comments start with //
/*
Multi-line comments look like this
*/
/// <summary>
/// This is an XML documentation comment which can be used to generate external
/// documentation or provide context help within an IDE
/// </summary>
/// <param name="firstParam">This is some parameter documentation for firstParam</param>
/// <returns>Information on the returned value of a function</returns>
//public void MethodOrClassOrOtherWithParsableHelp(string firstParam) {}

// Specify the namespaces this source code will be using
// The namespaces below are all part of the standard .NET Framework Class Library
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.IO;

// But this one is not:
using System.Data.Entity;
// In order to be able to use it, you need to add a dll reference
// This can be done with the NuGet package manager: `Install-Package EntityFramework`

// Namespaces define scope to organize code into "packages" or "modules"
// Using this code from another source file: using Learning.CSharp;
namespace Learning.CSharp
{
    // Each .cs file should at least contain a class with the same name as the file.
    // You're allowed to do otherwise, but shouldn't for sanity.
    public class LearnCSharp
    {
        // BASIC SYNTAX - skip to INTERESTING FEATURES if you have used Java or C++ before
        public static void Syntax()
        {
            // Use Console.WriteLine to print lines
            Console.WriteLine("Hello World");
            Console.WriteLine(
                "Integer: " + 10 +
                " Double: " + 3.14 +
                " Boolean: " + true);

            // To print without a new line, use Console.Write
            Console.Write("Hello ");
            Console.Write("World");

            ///////////////////////////////////////////////////
            // Types & Variables
            //
            // Declare a variable using <type> <name>
            ///////////////////////////////////////////////////

            // Sbyte - Signed 8-bit integer
            // (-128 <= sbyte <= 127)
            sbyte fooSbyte = 100;

            // Byte - Unsigned 8-bit integer
            // (0 <= byte <= 255)
            byte fooByte = 100;

            // Short - 16-bit integer
            // Signed - (-32,768 <= short <= 32,767)
            // Unsigned - (0 <= ushort <= 65,535)
            short fooShort = 10000;
            ushort fooUshort = 10000;

            // Integer - 32-bit integer
            int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647)
            uint fooUint = 1; // (0 <= uint <= 4,294,967,295)

            // Long - 64-bit integer
            long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
            ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
            // Numbers default to being int or uint depending on size.
            // L is used to denote that this variable value is of type long or ulong

            // Double - Double-precision 64-bit IEEE 754 Floating Point
            double fooDouble = 123.4; // Precision: 15-16 digits

            // Float - Single-precision 32-bit IEEE 754 Floating Point
            float fooFloat = 234.5f; // Precision: 7 digits
            // f is used to denote that this variable value is of type float

            // Decimal - a 128-bits data type, with more precision than other floating-point types,
            // suited for financial and monetary calculations
            decimal fooDecimal = 150.3m;

            // Boolean - true & false
            bool fooBoolean = true; // or false

            // Char - A single 16-bit Unicode character
            char fooChar = 'A';

            // Strings -- unlike the previous base types which are all value types,
            // a string is a reference type. That is, you can set it to null
            string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs)";
            Console.WriteLine(fooString);

            // You can access each character of the string with an indexer:
            char charFromString = fooString[1]; // => 'e'
            // Strings are immutable: you can't do fooString[1] = 'X';

            // Compare strings with current culture, ignoring case
            string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);

            // Formatting, based on sprintf
            string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);

            // Dates & Formatting
            DateTime fooDate = DateTime.Now;
            Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));

            // You can split a string over two lines with the @ symbol. To escape " use ""
            string bazString = @"Here's some stuff
on a new line! ""Wow!"", the masses cried";

            // Use const or read-only to make a variable immutable
            // const values are calculated at compile time
            const int HoursWorkPerWeek = 9001;

            ///////////////////////////////////////////////////
            // Data Structures
            ///////////////////////////////////////////////////

            // Arrays - zero indexed
            // The array size must be decided upon declaration
            // The format for declaring an array is follows:
            // <datatype>[] <var name> = new <datatype>[<array size>];
            int[] intArray = new int[10];

            // Another way to declare & initialize an array
            int[] y = { 9000, 1000, 1337 };

            // Indexing an array - Accessing an element
            Console.WriteLine("intArray @ 0: " + intArray[0]);
            // Arrays are mutable.
            intArray[1] = 1;

            // Lists
            // Lists are used more frequently than arrays as they are more flexible
            // The format for declaring a list is follows:
            // List<datatype> <var name> = new List<datatype>();
            List<int> intList = new List<int>();
            List<string> stringList = new List<string>();
            List<int> z = new List<int> { 9000, 1000, 1337 }; // initialize
            // The <> are for generics - Check out the cool stuff section

            // Lists don't default to a value;
            // A value must be added before accessing the index
            intList.Add(1);
            Console.WriteLine("intList @ 0: " + intList[0]);

            // Others data structures to check out:
            // Stack/Queue
            // Dictionary (an implementation of a hash map)
            // HashSet
            // Read-only Collections
            // Tuple (.Net 4+)

            ///////////////////////////////////////
            // Operators
            ///////////////////////////////////////
            Console.WriteLine("\n->Operators");

            int i1 = 1, i2 = 2; // Shorthand for multiple declarations

            // Arithmetic is straightforward
            Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3

            // Modulo
            Console.WriteLine("11%3 = " + (11 % 3)); // => 2

            // Comparison operators
            Console.WriteLine("3 == 2? " + (3 == 2)); // => false
            Console.WriteLine("3 != 2? " + (3 != 2)); // => true
            Console.WriteLine("3 > 2? " + (3 > 2)); // => true
            Console.WriteLine("3 < 2? " + (3 < 2)); // => false
            Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true
            Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true

            // Bitwise operators!
            /*
            ~       Unary bitwise complement
            <<      Signed left shift
            >>      Signed right shift
            &       Bitwise AND
            ^       Bitwise exclusive OR
            |       Bitwise inclusive OR
            */

            // Incrementations
            int i = 0;
            Console.WriteLine("\n->Inc/Dec-rementation");
            Console.WriteLine(i++); //Prints "0", i = 1. Post-Incrementation
            Console.WriteLine(++i); //Prints "2", i = 2. Pre-Incrementation
            Console.WriteLine(i--); //Prints "2", i = 1. Post-Decrementation
            Console.WriteLine(--i); //Prints "0", i = 0. Pre-Decrementation

            ///////////////////////////////////////
            // Control Structures
            ///////////////////////////////////////
            Console.WriteLine("\n->Control Structures");

            // If statements are c-like
            int j = 10;
            if (j == 10)
            {
                Console.WriteLine("I get printed");
            }
            else if (j > 10)
            {
                Console.WriteLine("I don't");
            }
            else
            {
                Console.WriteLine("I also don't");
            }

            // Ternary operators
            // A simple if/else can be written as follows
            // <condition> ? <true> : <false>
            int toCompare = 17;
            string isTrue = toCompare == 17 ? "True" : "False";

            // While loop
            int fooWhile = 0;
            while (fooWhile < 100)
            {
                //Iterated 100 times, fooWhile 0->99
                fooWhile++;
            }

            // Do While Loop
            int fooDoWhile = 0;
            do
            {
                // Start iteration 100 times, fooDoWhile 0->99
                if (false)
                    continue; // skip the current iteration

                fooDoWhile++;

                if (fooDoWhile == 50)
                    break; // breaks from the loop completely

            } while (fooDoWhile < 100);

            //for loop structure => for(<start_statement>; <conditional>; <step>)
            for (int fooFor = 0; fooFor < 10; fooFor++)
            {
                //Iterated 10 times, fooFor 0->9
            }

            // For Each Loop
            // foreach loop structure => foreach(<iteratorType> <iteratorName> in <enumerable>)
            // The foreach loop loops over any object implementing IEnumerable or IEnumerable<T>
            // All the collection types (Array, List, Dictionary...) in the .Net framework
            // implement one or both of these interfaces.
            // (The ToCharArray() could be removed, because a string also implements IEnumerable)
            foreach (char character in "Hello World".ToCharArray())
            {
                //Iterated over all the characters in the string
            }

            // Switch Case
            // A switch works with the byte, short, char, and int data types.
            // It also works with enumerated types (discussed in Enum Types),
            // the String class, and a few special classes that wrap
            // primitive types: Character, Byte, Short, and Integer.
            int month = 3;
            string monthString;
            switch (month)
            {
                case 1:
                    monthString = "January";
                    break;
                case 2:
                    monthString = "February";
                    break;
                case 3:
                    monthString = "March";
                    break;
                // You can assign more than one case to an action
                // But you can't add an action without a break before another case
                // (if you want to do this, you would have to explicitly add a goto case x
                case 6:
                case 7:
                case 8:
                    monthString = "Summer time!!";
                    break;
                default:
                    monthString = "Some other month";
                    break;
            }

            ///////////////////////////////////////
            // Converting Data Types And Typecasting
            ///////////////////////////////////////

            // Converting data

            // Convert String To Integer
            // this will throw a FormatException on failure
            int.Parse("123");//returns an integer version of "123"

            // try parse will default to type default on failure
            // in this case: 0
            int tryInt;
            if (int.TryParse("123", out tryInt)) // Function is boolean
                Console.WriteLine(tryInt);       // 123

            // Convert Integer To String
            // Convert class has a number of methods to facilitate conversions
            Convert.ToString(123);
            // or
            tryInt.ToString();

            // Casting
            // Cast decimal 15 to a int
            // and then implicitly cast to long
            long x = (int) 15M;
        }

        ///////////////////////////////////////
        // CLASSES - see definitions at end of file
        ///////////////////////////////////////
        public static void Classes()
        {
            // See Declaration of objects at end of file

            // Use new to instantiate a class
            Bicycle trek = new Bicycle();

            // Call object methods
            trek.SpeedUp(3); // You should always use setter and getter methods
            trek.Cadence = 100;

            // ToString is a convention to display the value of this Object.
            Console.WriteLine("trek info: " + trek.Info());

            // Instantiate a new Penny Farthing
            PennyFarthing funbike = new PennyFarthing(1, 10);
            Console.WriteLine("funbike info: " + funbike.Info());

            Console.Read();
        } // End main method

        // CONSOLE ENTRY A console application must have a main method as an entry point
        public static void Main(string[] args)
        {
            OtherInterestingFeatures();
        }

        //
        // INTERESTING FEATURES
        //

        // DEFAULT METHOD SIGNATURES

        public // Visibility
        static // Allows for direct call on class without object
        int // Return Type,
        MethodSignatures(
            int maxCount, // First variable, expects an int
            int count = 0, // will default the value to 0 if not passed in
            int another = 3,
            params string[] otherParams // captures all other parameters passed to method
        )
        {
            return -1;
        }

        // Methods can have the same name, as long as the signature is unique
        // A method that differs only in return type is not unique
        public static void MethodSignatures(
            ref int maxCount, // Pass by reference
            out int count)
        {
            //the argument passed in as 'count' will hold the value of 15 outside of this function
            count = 15; // out param must be assigned before control leaves the method
        }

        // GENERICS
        // The classes for TKey and TValue is specified by the user calling this function.
        // This method emulates the SetDefault of Python
        public static TValue SetDefault<TKey, TValue>(
            IDictionary<TKey, TValue> dictionary,
            TKey key,
            TValue defaultItem)
        {
            TValue result;
            if (!dictionary.TryGetValue(key, out result))
                return dictionary[key] = defaultItem;
            return result;
        }

        // You can narrow down the objects that are passed in
        public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
        {
            // We can iterate, since T is a IEnumerable
            foreach (var item in toPrint)
                // Item is an int
                Console.WriteLine(item.ToString());
        }

        // YIELD
        // Usage of the "yield" keyword indicates that the method it appears in is an Iterator
        // (this means you can use it in a foreach loop)
        public static IEnumerable<int> YieldCounter(int limit = 10)
        {
            for (var i = 0; i < limit; i++)
                yield return i;
        }

        // which you would call like this :
        public static void PrintYieldCounterToConsole()
        {
            foreach (var counter in YieldCounter())
                Console.WriteLine(counter);
        }

        // you can use more than one "yield return" in a method
        public static IEnumerable<int> ManyYieldCounter()
        {
            yield return 0;
            yield return 1;
            yield return 2;
            yield return 3;
        }

        // you can also use "yield break" to stop the Iterator
        // this method would only return half of the values from 0 to limit.
        public static IEnumerable<int> YieldCounterWithBreak(int limit = 10)
        {
            for (var i = 0; i < limit; i++)
            {
                if (i > limit/2) yield break;
                yield return i;
            }
        }

        public static void OtherInterestingFeatures()
        {
            // OPTIONAL PARAMETERS
            MethodSignatures(3, 1, 3, "Some", "Extra", "Strings");
            MethodSignatures(3, another: 3); // explicitly set a parameter, skipping optional ones

            // BY REF AND OUT PARAMETERS
            int maxCount = 0, count; // ref params must have value
            MethodSignatures(ref maxCount, out count);

            // EXTENSION METHODS
            int i = 3;
            i.Print(); // Defined below

            // NULLABLE TYPES - great for database interaction / return values
            // any value type (i.e. not a class) can be made nullable by suffixing a ?
            // <type>? <var name> = <value>
            int? nullable = null; // short hand for Nullable<int>
            Console.WriteLine("Nullable variable: " + nullable);
            bool hasValue = nullable.HasValue; // true if not null

            // ?? is syntactic sugar for specifying default value (coalesce)
            // in case variable is null
            int notNullable = nullable ?? 0; // 0

            // ?. is an operator for null-propagation - a shorthand way of checking for null
            nullable?.Print(); // Use the Print() extension method if nullable isn't null

            // IMPLICITLY TYPED VARIABLES - you can let the compiler work out what the type is:
            var magic = "magic is a string, at compile time, so you still get type safety";
            // magic = 9; will not work as magic is a string, not an int

            // GENERICS
            //
            var phonebook = new Dictionary<string, string>() {
                {"Sarah", "212 555 5555"} // Add some entries to the phone book
            };

            // Calling SETDEFAULT defined as a generic above
            Console.WriteLine(SetDefault<string,string>(phonebook, "Shaun", "No Phone")); // No Phone
            // nb, you don't need to specify the TKey and TValue since they can be
            // derived implicitly
            Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555

            // LAMBDA EXPRESSIONS - allow you to write code in line
            Func<int, int> square = (x) => x * x; // Last T item is the return value
            Console.WriteLine(square(3)); // 9

            // ERROR HANDLING - coping with an uncertain world
            try
            {
                var funBike = PennyFarthing.CreateWithGears(6);

                // will no longer execute because CreateWithGears throws an exception
                string some = "";
                if (true) some = null;
                some.ToLower(); // throws a NullReferenceException
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("Not so much fun now!");
            }
            catch (Exception ex) // catch all other exceptions
            {
                throw new ApplicationException("It hit the fan", ex);
                // throw; // A rethrow that preserves the callstack
            }
            // catch { } // catch-all without capturing the Exception
            finally
            {
                // executes after try or catch
            }

            // DISPOSABLE RESOURCES MANAGEMENT - let you handle unmanaged resources easily.
            // Most of objects that access unmanaged resources (file handle, device contexts, etc.)
            // implement the IDisposable interface. The using statement takes care of
            // cleaning those IDisposable objects for you.
            using (StreamWriter writer = new StreamWriter("log.txt"))
            {
                writer.WriteLine("Nothing suspicious here");
                // At the end of scope, resources will be released.
                // Even if an exception is thrown.
            }

            // PARALLEL FRAMEWORK
            // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx

            var words = new List<string> {"dog", "cat", "horse", "pony"};

            Parallel.ForEach(words,
                new ParallelOptions() { MaxDegreeOfParallelism = 4 },
                word =>
                {
                    Console.WriteLine(word);
                }
            );

            //Running this will produce different outputs
            //since each thread finishes at different times.
            //Some example outputs are:
            //cat dog horse pony
            //dog horse pony cat

            // DYNAMIC OBJECTS (great for working with other languages)
            dynamic student = new ExpandoObject();
            student.FirstName = "First Name"; // No need to define class first!

            // You can even add methods (returns a string, and takes in a string)
            student.Introduce = new Func<string, string>(
                (introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo));
            Console.WriteLine(student.Introduce("Beth"));

            // IQUERYABLE<T> - almost all collections implement this, which gives you a lot of
            // very useful Map / Filter / Reduce style methods
            var bikes = new List<Bicycle>();
            bikes.Sort(); // Sorts the array
            bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels)); // Sorts based on wheels
            var result = bikes
                .Where(b => b.Wheels > 3) // Filters - chainable (returns IQueryable of previous type)
                .Where(b => b.IsBroken && b.HasTassles)
                .Select(b => b.ToString()); // Map - we only this selects, so result is a IQueryable<string>

            var sum = bikes.Sum(b => b.Wheels); // Reduce - sums all the wheels in the collection

            // Create a list of IMPLICIT objects based on some parameters of the bike
            var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
            // Hard to show here, but you get type ahead completion since the compiler can implicitly work
            // out the types above!
            foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome))
                Console.WriteLine(bikeSummary.Name);

            // ASPARALLEL
            // And this is where things get wicked - combine linq and parallel operations
            var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
            // this will happen in parallel! Threads will automagically be spun up and the
            // results divvied amongst them! Amazing for large datasets when you have lots of
            // cores

            // LINQ - maps a store to IQueryable<T> objects, with delayed execution
            // e.g. LinqToSql - maps to a database, LinqToXml maps to an xml document
            var db = new BikeRepository();

            // execution is delayed, which is great when querying a database
            var filter = db.Bikes.Where(b => b.HasTassles); // no query run
            if (42 > 6) // You can keep adding filters, even conditionally - great for "advanced search" functionality
                filter = filter.Where(b => b.IsBroken); // no query run

            var query = filter
                .OrderBy(b => b.Wheels)
                .ThenBy(b => b.Name)
                .Select(b => b.Name); // still no query run

            // Now the query runs, but opens a reader, so only populates as you iterate through
            foreach (string bike in query)
                Console.WriteLine(result);



        }

    } // End LearnCSharp class

    // You can include other classes in a .cs file

    public static class Extensions
    {
        // EXTENSION METHODS
        public static void Print(this object obj)
        {
            Console.WriteLine(obj.ToString());
        }
    }

    // Class Declaration Syntax:
    // <public/private/protected/internal> class <class name>{
    //    //data fields, constructors, functions all inside.
    //    //functions are called as methods in Java.
    // }

    public class Bicycle
    {
        // Bicycle's Fields/Variables
        public int Cadence // Public: Can be accessed from anywhere
        {
            get // get - define a method to retrieve the property
            {
                return _cadence;
            }
            set // set - define a method to set a property
            {
                _cadence = value; // Value is the value passed in to the setter
            }
        }
        private int _cadence;

        protected virtual int Gear // Protected: Accessible from the class and subclasses
        {
            get; // creates an auto property so you don't need a member field
            set;
        }

        internal int Wheels // Internal: Accessible from within the assembly
        {
            get;
            private set; // You can set modifiers on the get/set methods
        }

        int _speed; // Everything is private by default: Only accessible from within this class.
                    // can also use keyword private
        public string Name { get; set; }
        
        // Properties also have a special syntax for when you want a readonly property
        // that simply returns the result of an expression
        public string LongName => Name + " " + _speed + " speed"; 

        // Enum is a value type that consists of a set of named constants
        // It is really just mapping a name to a value (an int, unless specified otherwise).
        // The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
        // An enum can't contain the same value twice.
        public enum BikeBrand
        {
            AIST,
            BMC,
            Electra = 42, //you can explicitly set a value to a name
            Gitane // 43
        }
        // We defined this type inside a Bicycle class, so it is a nested type
        // Code outside of this class should reference this type as Bicycle.Brand

        public BikeBrand Brand; // After declaring an enum type, we can declare the field of this type

        // Decorate an enum with the FlagsAttribute to indicate that multiple values can be switched on
        // Any class derived from Attribute can be used to decorate types, methods, parameters etc
        // Bitwise operators & and | can be used to perform and/or operations

        [Flags]
        public enum BikeAccessories
        {
            None = 0,
            Bell = 1,
            MudGuards = 2, // need to set the values manually!
            Racks = 4,
            Lights = 8,
            FullPackage = Bell | MudGuards | Racks | Lights
        }

        // Usage: aBike.Accessories.HasFlag(Bicycle.BikeAccessories.Bell)
        // Before .NET 4: (aBike.Accessories & Bicycle.BikeAccessories.Bell) == Bicycle.BikeAccessories.Bell
        public BikeAccessories Accessories { get; set; }

        // Static members belong to the type itself rather than specific object.
        // You can access them without a reference to any object:
        // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated);
        public static int BicyclesCreated { get; set; }

        // readonly values are set at run time
        // they can only be assigned upon declaration or in a constructor
        readonly bool _hasCardsInSpokes = false; // read-only private

        // Constructors are a way of creating classes
        // This is a default constructor
        public Bicycle()
        {
            this.Gear = 1; // you can access members of the object with the keyword this
            Cadence = 50;  // but you don't always need it
            _speed = 5;
            Name = "Bontrager";
            Brand = BikeBrand.AIST;
            BicyclesCreated++;
        }

        // This is a specified constructor (it contains arguments)
        public Bicycle(int startCadence, int startSpeed, int startGear,
                       string name, bool hasCardsInSpokes, BikeBrand brand)
            : base() // calls base first
        {
            Gear = startGear;
            Cadence = startCadence;
            _speed = startSpeed;
            Name = name;
            _hasCardsInSpokes = hasCardsInSpokes;
            Brand = brand;
        }

        // Constructors can be chained
        public Bicycle(int startCadence, int startSpeed, BikeBrand brand) :
            this(startCadence, startSpeed, 0, "big wheels", true, brand)
        {
        }

        // Function Syntax:
        // <public/private/protected> <return type> <function name>(<args>)

        // classes can implement getters and setters for their fields
        // or they can implement properties (this is the preferred way in C#)

        // Method parameters can have default values.
        // In this case, methods can be called with these parameters omitted
        public void SpeedUp(int increment = 1)
        {
            _speed += increment;
        }

        public void SlowDown(int decrement = 1)
        {
            _speed -= decrement;
        }

        // properties get/set values
        // when only data needs to be accessed, consider using properties.
        // properties may have either get or set, or both
        private bool _hasTassles; // private variable
        public bool HasTassles // public accessor
        {
            get { return _hasTassles; }
            set { _hasTassles = value; }
        }

        // You can also define an automatic property in one line
        // this syntax will create a backing field automatically.
        // You can set an access modifier on either the getter or the setter (or both)
        // to restrict its access:
        public bool IsBroken { get; private set; }

        // Properties can be auto-implemented
        public int FrameSize
        {
            get;
            // you are able to specify access modifiers for either get or set
            // this means only Bicycle class can call set on Framesize
            private set;
        }

        // It's also possible to define custom Indexers on objects.
        // All though this is not entirely useful in this example, you
        // could do bicycle[0] which returns "chris" to get the first passenger or
        // bicycle[1] = "lisa" to set the passenger. (of this apparent quattrocycle)
        private string[] passengers = { "chris", "phil", "darren", "regina" };

        public string this[int i]
        {
            get {
                return passengers[i];
            }

            set {
                passengers[i] = value;
            }
        }

        //Method to display the attribute values of this Object.
        public virtual string Info()
        {
            return "Gear: " + Gear +
                    " Cadence: " + Cadence +
                    " Speed: " + _speed +
                    " Name: " + Name +
                    " Cards in Spokes: " + (_hasCardsInSpokes ? "yes" : "no") +
                    "\n------------------------------\n"
                    ;
        }

        // Methods can also be static. It can be useful for helper methods
        public static bool DidWeCreateEnoughBicycles()
        {
            // Within a static method, we only can reference static class members
            return BicyclesCreated > 9000;
        } // If your class only needs static members, consider marking the class itself as static.


    } // end class Bicycle

    // PennyFarthing is a subclass of Bicycle
    class PennyFarthing : Bicycle
    {
        // (Penny Farthings are those bicycles with the big front wheel.
        // They have no gears.)

        // calling parent constructor
        public PennyFarthing(int startCadence, int startSpeed) :
            base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra)
        {
        }

        protected override int Gear
        {
            get
            {
                return 0;
            }
            set
            {
                throw new InvalidOperationException("You can't change gears on a PennyFarthing");
            }
        }

        public static PennyFarthing CreateWithGears(int gears)
        {
            var penny = new PennyFarthing(1, 1);
            penny.Gear = gears; // Oops, can't do this!
            return penny;
        }

        public override string Info()
        {
            string result = "PennyFarthing bicycle ";
            result += base.ToString(); // Calling the base version of the method
            return result;
        }
    }

    // Interfaces only contain signatures of the members, without the implementation.
    interface IJumpable
    {
        void Jump(int meters); // all interface members are implicitly public
    }

    interface IBreakable
    {
        bool Broken { get; } // interfaces can contain properties as well as methods & events
    }

    // Classes can inherit only one other class, but can implement any amount of interfaces,
    // however the base class name must be the first in the list and all interfaces follow
    class MountainBike : Bicycle, IJumpable, IBreakable
    {
        int damage = 0;

        public void Jump(int meters)
        {
            damage += meters;
        }

        public bool Broken
        {
            get
            {
                return damage > 100;
            }
        }
    }

    /// <summary>
    /// Used to connect to DB for LinqToSql example.
    /// EntityFramework Code First is awesome (similar to Ruby's ActiveRecord, but bidirectional)
    /// http://msdn.microsoft.com/en-us/data/jj193542.aspx
    /// </summary>
    public class BikeRepository : DbContext
    {
        public BikeRepository()
            : base()
        {
        }

        public DbSet<Bicycle> Bikes { get; set; }
    }

    // Classes can be split across multiple .cs files
    // A1.cs
    public partial class A
    {
        public static void A1()
        {
            Console.WriteLine("Method A1 in class A");
        }
    }

    // A2.cs
    public partial class A
    {
        public static void A2()
        {
            Console.WriteLine("Method A2 in class A");
        }
    }

    // Program using the partial class "A"
    public class Program
    {
        static void Main()
        {
            A.A1();
            A.A2();
        }
    }

    // String interpolation by prefixing the string with $
    // and wrapping the expression you want to interpolate with { braces }
    public class Rectangle
    {
        public int Length { get; set; }
        public int Width { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rect = new Rectangle { Length = 5, Width = 3 };
            Console.WriteLine($"The length is {rect.Length} and the width is {rect.Width}");
        }
    }

    // New C# 6 features
    class GlassBall : IJumpable, IBreakable
    {
        // Autoproperty initializers
        public int Damage { get; private set; } = 0;

        // Autoproperty initializers on getter-only properties
        public string Name { get; } = "Glass ball";

        // Getter-only autoproperty that is initialized in constructor
        public string GenieName { get; }

        public GlassBall(string genieName = null)
        {
            GenieName = genieName;
        }

        public void Jump(int meters)
        {
            if (meters < 0)
                // New nameof() expression; compiler will check that the identifier exists
                // nameof(x) == "x"
                // Prevents e.g. parameter names changing but not updated in error messages
                throw new ArgumentException("Cannot jump negative amount!", nameof(meters));

            Damage += meters;
        }

        // Expression-bodied properties ...
        public bool Broken
            => Damage > 100;

        // ... and methods
        public override string ToString()
            // Interpolated string
            => $"{Name}. Damage taken: {Damage}";

        public string SummonGenie()
            // Null-conditional operators
            // x?.y will return null immediately if x is null; y is not evaluated
            => GenieName?.ToUpper();
    }

    static class MagicService
    {
        private static bool LogException(Exception ex)
        {
            /* log exception somewhere */
            return false;
        }

        public static bool CastSpell(string spell)
        {
            try
            {
                // Pretend we call API here
                throw new MagicServiceException("Spell failed", 42);

                // Spell succeeded
                return true;
            }
            // Only catch if Code is 42 i.e. spell failed
            catch(MagicServiceException ex) when (ex.Code == 42)
            {
                // Spell failed
                return false;
            }
            // Other exceptions, or MagicServiceException where Code is not 42 
            catch(Exception ex) when (LogException(ex))
            {
                // Execution never reaches this block
                // The stack is not unwound
            }
            return false;
            // Note that catching a MagicServiceException and rethrowing if Code
            // is not 42 or 117 is different, as then the final catch-all block
            // will not catch the rethrown exception
        }
    }

    public class MagicServiceException : Exception
    {
        public int Code { get; }

        public MagicServiceException(string message, int code) : base(message)
        {
            Code = code;
        }
    }

    public static class PragmaWarning {
        // Obsolete attribute
        [Obsolete("Use NewMethod instead", false)]
        public static void ObsoleteMethod()
        {
            /* obsolete code */
        }

        public static void NewMethod()
        {
            /* new code */
        }

        public static void Main()
        {
            ObsoleteMethod(); // CS0618: 'ObsoleteMethod is obsolete: Use NewMethod instead'
#pragma warning disable CS0618
            ObsoleteMethod(); // no warning
#pragma warning restore CS0618
            ObsoleteMethod(); // CS0618: 'ObsoleteMethod is obsolete: Use NewMethod instead'
        }
    }
} // End Namespace

using System;
// C# 6, static using
using static System.Math;

namespace Learning.More.CSharp
{
    class StaticUsing
    {
        static void Main()
        {
            // Without a static using statement..
            Console.WriteLine("The square root of 4 is {}.", Math.Sqrt(4));
            // With one
            Console.WriteLine("The square root of 4 is {}.", Sqrt(4));
        }
    }
}

using System;
namespace Csharp7
{
	//New C# 7 Feature
	//Install Microsoft.Net.Compilers Latest from Nuget
	//Install System.ValueTuple Latest from Nuget
	class Program
	{
		static void Main(string[] args)
		{
			//Type 1 Declaration
			(string FirstName, string LastName) names1 = ("Peter", "Parker");
			Console.WriteLine(names1.FirstName);

			//Type 2 Declaration
			var names2 = (First:"Peter", Last:"Parker");
			Console.WriteLine(names2.Last);
		}
	}
}

```

## Topics Not Covered

 * Attributes
 * async/await
 * Web Development
 	* ASP.NET MVC & WebApi (new)
 	* ASP.NET Web Forms (old)
 	* WebMatrix (tool)
 * Desktop Development
 	* Windows Presentation Foundation (WPF) (new)
 	* Winforms (old)

## Further Reading

 * [DotNetPerls](http://www.dotnetperls.com)
 * [C# in Depth](http://manning.com/skeet2)
 * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
 * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
 * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx)
 * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
 * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials)
 * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
 * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)
 * [C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx)
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
    - ["Geoffrey Liu", "https://github.com/g-liu"]
    - ["Connor Shea", "https://github.com/connorshea"]
    - ["Deepanshu Utkarsh", "https://github.com/duci9y"]
    - ["Brett Taylor", "https://github.com/glutnix"]
    - ["Tyler Mumford", "https://tylermumford.com"]
filename: learncss.css
---

Web pages are built with HTML, which specifies the content of a page.
CSS (Cascading Style Sheets) is a separate language which specifies
a page's **appearance**.

CSS code is made of static *rules*. Each rule takes one or more *selectors* and
gives specific *values* to a number of visual *properties*. Those properties are
then applied to the page elements indicated by the selectors.

This guide has been written with CSS 2 in mind, which is extended by the new
features of CSS 3.

**NOTE:** Because CSS produces visual results, in order to learn it, you need to
try everything in a CSS playground like [dabblet](http://dabblet.com/).
The main focus of this article is on the syntax and some general tips.

## Syntax

```css
/* comments appear inside slash-asterisk, just like this line!
   there are no "one-line comments"; this is the only comment style */

/* ####################
   ## SELECTORS
   #################### */

/* the selector is used to target an element on a page. */
selector { property: value; /* more properties...*/ }

/*
Here is an example element:

<div class='class1 class2' id='anID' attr='value' otherAttr='en-us foo bar' />
*/

/* You can target it using one of its CSS classes */
.class1 { }

/* or both classes! */
.class1.class2 { }

/* or its name */
div { }

/* or its id */
#anID { }

/* or using the fact that it has an attribute! */
[attr] { font-size:smaller; }

/* or that the attribute has a specific value */
[attr='value'] { font-size:smaller; }

/* starts with a value (CSS 3) */
[attr^='val'] { font-size:smaller; }

/* or ends with a value (CSS 3) */
[attr$='ue'] { font-size:smaller; }

/* or contains a value in a space-separated list */
[otherAttr~='foo'] { }
[otherAttr~='bar'] { }

/* or contains a value in a dash-separated list, e.g., "-" (U+002D) */
[otherAttr|='en'] { font-size:smaller; }


/* You can combine different selectors to create a more focused selector. Don't
   put spaces between them. */
div.some-class[attr$='ue'] { }

/* You can select an element which is a child of another element */
div.some-parent > .class-name { }

/* or a descendant of another element. Children are the direct descendants of
   their parent element, only one level down the tree. Descendants can be any
   level down the tree. */
div.some-parent .class-name { }

/* Warning: the same selector without a space has another meaning.
   Can you guess what? */
div.some-parent.class-name { }

/* You may also select an element based on its adjacent sibling */
.i-am-just-before + .this-element { }

/* or any sibling preceding it */
.i-am-any-element-before ~ .this-element { }

/* There are some selectors called pseudo classes that can be used to select an
   element only when it is in a particular state */

/* for example, when the cursor hovers over an element */
selector:hover { }

/* or a link has been visited */
selector:visited { }

/* or hasn't been visited */
selected:link { }

/* or an element is in focus */
selected:focus { }

/* any element that is the first child of its parent */
selector:first-child {}

/* any element that is the last child of its parent */
selector:last-child {}

/* Just like pseudo classes, pseudo elements allow you to style certain parts of
    a document  */

/* matches a virtual first child of the selected element */
selector::before {}

/* matches a virtual last child of the selected element */
selector::after {}

/* At appropriate places, an asterisk may be used as a wildcard to select every
   element */
* { } /* all elements */
.parent * { } /* all descendants */
.parent > * { } /* all children */

/* ####################
   ## PROPERTIES
   #################### */

selector {

    /* Units of length can be absolute or relative. */

    /* Relative units */
    width: 50%;       /* percentage of parent element width */
    font-size: 2em;   /* multiples of element's original font-size */
    font-size: 2rem;  /* or the root element's font-size */
    font-size: 2vw;   /* multiples of 1% of the viewport's width (CSS 3) */
    font-size: 2vh;   /* or its height */
    font-size: 2vmin; /* whichever of a vh or a vw is smaller */
    font-size: 2vmax; /* or greater */

    /* Absolute units */
    width: 200px;     /* pixels */
    font-size: 20pt;  /* points */
    width: 5cm;       /* centimeters */
    min-width: 50mm;  /* millimeters */
    max-width: 5in;   /* inches */

    /* Colors */
    color: #F6E;                 /* short hex format */
    color: #FF66EE;              /* long hex format */
    color: tomato;               /* a named color */
    color: rgb(255, 255, 255);   /* as rgb values */
    color: rgb(10%, 20%, 50%);   /* as rgb percentages */
    color: rgba(255, 0, 0, 0.3); /* as rgba values (CSS 3) Note: 0 <= a <= 1 */
    color: transparent;          /* equivalent to setting the alpha to 0 */
    color: hsl(0, 100%, 50%);    /* as hsl percentages (CSS 3) */
    color: hsla(0, 100%, 50%, 0.3); /* as hsl percentages with alpha */

    /* Borders */
    border-width:5px;
    border-style:solid;
    border-color:red;      /* similar to how background-color is set */
    border: 5px solid red; /* this is a short hand approach for the same */
    border-radius:20px;    /* this is a CSS3 property */    

    /* Images as backgrounds of elements */
    background-image: url(/img-path/img.jpg); /* quotes inside url() optional */

    /* Fonts */
    font-family: Arial;
    /* if the font family name has a space, it must be quoted */
    font-family: "Courier New";
    /* if the first one is not found, the browser uses the next, and so on */
    font-family: "Courier New", Trebuchet, Arial, sans-serif;
}
```

## Usage

Save a CSS stylesheet with the extension `.css`.

```html
<!-- You need to include the css file in your page's <head>. This is the
     recommended method. Refer to http://stackoverflow.com/questions/8284365 -->
<link rel='stylesheet' type='text/css' href='path/to/style.css'>

<!-- You can also include some CSS inline in your markup. -->
<style>
   a { color: purple; }
</style>

<!-- Or directly set CSS properties on the element. -->
<div style="border: 1px solid red;">
</div>
```

## Precedence or Cascade

An element may be targeted by multiple selectors and may have a property set on
it in more than once. In these cases, one of the rules takes precedence over
others. Rules with a more specific selector take precedence over a less specific
one, and a rule occurring later in the stylesheet overwrites a previous one
(which also means that if two different linked stylesheets contain rules for an
element and if the rules are of the same specificity, then order of linking
would take precedence and the sheet linked latest would govern styling) .

This process is called cascading, hence the name Cascading Style Sheets.

Given the following CSS:

```css
/* A */
p.class1[attr='value']

/* B */
p.class1 { }

/* C */
p.class2 { }

/* D */
p { }

/* E */
p { property: value !important; }
```

and the following markup:

```html
<p style='/*F*/ property:value;' class='class1 class2' attr='value'>
```

The precedence of style is as follows. Remember, the precedence is for each
**property**, not for the entire block.

* `E` has the highest precedence because of the keyword `!important`. It is
recommended that you avoid its usage.
* `F` is next, because it is an inline style.
* `A` is next, because it is more "specific" than anything else. It has 3
    specifiers: The name of the element `p`, its class `class1`, an attribute
    `attr='value'`.
* `C` is next, even though it has the same specificity as `B`.
    This is because it appears after `B`.
* `B` is next.
* `D` is the last one.

## Media Queries

CSS Media Queries are a feature in CSS 3 which allows you to specify when certain CSS rules should be applied, such as when printed, or when on a screen with certain dimensions or pixel density. They do not add to the selector's specificity.

```css
/* A rule that will be used on all devices */
h1 {
  font-size: 2em;
  color: white;
  background-color: black;
}

/* change the h1 to use less ink on a printer */
@media print {
  h1 {
    color: black;
    background-color: white;
  }
}

/* make the font bigger when shown on a screen at least 480px wide */
@media screen and (min-width: 480px) {
  h1 {
    font-size: 3em;
    font-weight: normal;
  }
}
```

Media queries can include these features:
`width`, `height`, `device-width`, `device-height`, `orientation`, `aspect-ratio`, `device-aspect-ratio`, `color`, `color-index`, `monochrome`, `resolution`, `scan`, `grid`. Most of these features can be prefixed with `min-` or `max-`.

The `resolution` feature is not supported by older devices, instead use `device-pixel-ratio`.

Many smartphones and tablets will attempt to render the page as if it were on a desktop unless you provide a `viewport` meta-tag.

```html
<head>
  <meta name="viewport" content="width=device-width; initial-scale=1.0">
</head>
```

## Compatibility

Most of the features in CSS 2 (and many in CSS 3) are available across all
browsers and devices. But it's always good practice to check before using
a new feature.

## Resources

* [CanIUse](http://caniuse.com) (Detailed compatibility info)
* [Dabblet](http://dabblet.com/) (CSS playground)
* [Mozilla Developer Network's CSS documentation](https://developer.mozilla.org/en-US/docs/Web/CSS) (Tutorials and reference)
* [Codrops' CSS Reference](http://tympanus.net/codrops/css_reference/) (Reference)

## Further Reading

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [Selecting elements using attributes](https://css-tricks.com/almanac/selectors/a/attribute/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
* [SASS](http://sass-lang.com/) and [LESS](http://lesscss.org/) for CSS pre-processing
* [CSS-Tricks](https://css-tricks.com)
---
language: cypher
filename: LearnCypher.cql
contributors:
    - ["Théo Gauchoux", "https://github.com/TheoGauchoux"]
---

Cypher is the Neo4j’s query language to manipulate graphs easily. It reuses syntax from SQL and mixes it with kind of ascii-art to represent graphs.
This tutorial assumes that you already know graph concepts like nodes and relationships.

[Read more here.](https://neo4j.com/developer/cypher-query-language/)


Nodes
---

**Represents a record in a graph.**

```()```
It's an empty *node*, to indicate that there is a *node*, but it's not relevant for the query.

```(n)```
It's a *node* referred by the variable **n**, reusable in the query. It begins with lowercase and uses camelCase.

```(p:Person)```
You can add a *label* to your node, here **Person**. It's like a type / a class / a category. It begins with uppercase and uses camelCase.

```(p:Person:Manager)```
A node can have many *labels*.

```(p:Person {name : 'Théo Gauchoux', age : 22})```
A node can have some *properties*, here **name** and **age**. It begins with lowercase and uses camelCase.

The types allowed in properties :

 - Numeric
 - Boolean
 - String
 - List of previous primitive types

*Warning : there isn't datetime property in Cypher ! You can use String with a specific pattern or a Numeric from a specific date.*

```p.name```
You can access to a property with the dot style.


Relationships (or Edges)
---

**Connects two nodes**

```[:KNOWS]```
It's a *relationship* with the *label* **KNOWS**. It's a *label* as the node's label. It begins with uppercase and use UPPER_SNAKE_CASE.

```[k:KNOWS]```
The same *relationship*, referred by the variable **k**, reusable in the query, but it's not necessary.

```[k:KNOWS {since:2017}]```
The same *relationship*, with *properties* (like *node*), here **since**.

```[k:KNOWS*..4]```
It's a structural information to use in a *path* (seen later). Here, **\*..4** says "Match the pattern, with the relationship **k** which be repeated between 1 and 4 times.


Paths
---

**The way to mix nodes and relationships.**

```(a:Person)-[:KNOWS]-(b:Person)```
A path describing that **a** and **b** know each other.

```(a:Person)-[:MANAGES]->(b:Person)```
A path can be directed. This path describes that **a** is the manager of **b**.

```(a:Person)-[:KNOWS]-(b:Person)-[:KNOWS]-(c:Person)```
You can chain multiple relationships. This path describes the friend of a friend.

```(a:Person)-[:MANAGES]->(b:Person)-[:MANAGES]->(c:Person)```
A chain can also be directed. This path describes that **a** is the boss of **b** and the big boss of **c**.

Patterns often used (from Neo4j doc) :

```
// Friend-of-a-friend 
(user)-[:KNOWS]-(friend)-[:KNOWS]-(foaf)

// Shortest path
path = shortestPath( (user)-[:KNOWS*..5]-(other) )

// Collaborative filtering
(user)-[:PURCHASED]->(product)<-[:PURCHASED]-()-[:PURCHASED]->(otherProduct)

// Tree navigation 
(root)<-[:PARENT*]-(leaf:Category)-[:ITEM]->(data:Product)

```


Create queries
---

Create a new node
```
CREATE (a:Person {name:"Théo Gauchoux"})
RETURN a
```
*`RETURN` allows to have a result after the query. It can be multiple, as `RETURN a, b`.*

Create a new relationship (with 2 new nodes)
```
CREATE (a:Person)-[k:KNOWS]-(b:Person)
RETURN a,k,b
```

Match queries
---

Match all nodes
```
MATCH (n)
RETURN n
```

Match nodes by label
```
MATCH (a:Person)
RETURN a
```

Match nodes by label and property
```
MATCH (a:Person {name:"Théo Gauchoux"})
RETURN a
```

Match nodes according to relationships (undirected)
```
MATCH (a)-[:KNOWS]-(b)
RETURN a,b
```

Match nodes according to relationships (directed)
```
MATCH (a)-[:MANAGES]->(b)
RETURN a,b
```

Match nodes with a `WHERE` clause
```
MATCH (p:Person {name:"Théo Gauchoux"})-[s:LIVES_IN]->(city:City)
WHERE s.since = 2015
RETURN p,state
```

You can use `MATCH WHERE` clause with `CREATE` clause
```
MATCH (a), (b)
WHERE a.name = "Jacquie" AND b.name = "Michel"
CREATE (a)-[:KNOWS]-(b)
```


Update queries
---

Update a specific property of a node
```
MATCH (p:Person)
WHERE p.name = "Théo Gauchoux"
SET p.age = 23
```

Replace all properties of a node
```
MATCH (p:Person)
WHERE p.name = "Théo Gauchoux"
SET p = {name: "Michel", age: 23}
```

Add new property to a node
```
MATCH (p:Person)
WHERE p.name = "Théo Gauchoux"
SET p + = {studies: "IT Engineering"}
```

Add a label to a node
```
MATCH (p:Person)
WHERE p.name = "Théo Gauchoux"
SET p:Internship
```


Delete queries
---

Delete a specific node (linked relationships must be deleted before)
```
MATCH (p:Person)-[relationship]-()
WHERE p.name = "Théo Gauchoux"
DELETE relationship, p
```

Remove a property in a specific node
```
MATCH (p:Person)
WHERE p.name = "Théo Gauchoux"
REMOVE p.age
```
*Pay attention to the `REMOVE`keyword, it's not `DELETE` !*

Remove a label from a specific node
```
MATCH (p:Person)
WHERE p.name = "Théo Gauchoux"
DELETE p:Person
```

Delete entire database
```
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n, r
```
*Seriously, it's the `rm -rf /` of Cypher !*


Other useful clauses
---

```PROFILE```
Before a query, show the execution plan of it.

```COUNT(e)```
Count entities (nodes or relationships) matching **e**.

```LIMIT x```
Limit the result to the x first results.


Special hints
---

- There is just single-line comments in Cypher, with double-slash : // Comments
- You can execute a Cypher script stored in a **.cql** file directly in Neo4j (it's an import). However, you can't have multiple statements in this file (separated by **;**).
- Use the Neo4j shell to write Cypher, it's really awesome.
- The Cypher will be the standard query language for all graph databases (known as **OpenCypher**).
---
language: D
filename: learnd.d
contributors:
    - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]

---

```d
// You know what's coming...
module hello;

import std.stdio;

// args is optional
void main(string[] args) {
    writeln("Hello, World!");
}
```

If you're like me and spend way too much time on the internet, odds are you've heard
about [D](http://dlang.org/). The D programming language is a modern, general-purpose,
multi-paradigm language with support for everything from low-level features to
expressive high-level abstractions.

D is actively developed by a large group of super-smart people and is spearheaded by
[Walter Bright](https://en.wikipedia.org/wiki/Walter_Bright) and
[Andrei Alexandrescu](https://en.wikipedia.org/wiki/Andrei_Alexandrescu).
With all that out of the way, let's look at some examples!

```d
import std.stdio;

void main() {

    // Conditionals and loops work as expected.
    for(int i = 0; i < 10000; i++) {
        writeln(i);
    }

    // 'auto' can be used for inferring types.
    auto n = 1;

    // Numeric literals can use '_' as a digit separator for clarity.
    while(n < 10_000) {
        n += n;
    }

    do {
        n -= (n / 2);
    } while(n > 0);

    // For and while are nice, but in D-land we prefer 'foreach' loops.
    // The '..' creates a continuous range, including the first value
    // but excluding the last.
    foreach(n; 1..1_000_000) {
        if(n % 2 == 0)
            writeln(n);
    }

    // There's also 'foreach_reverse' when you want to loop backwards.
    foreach_reverse(n; 1..int.max) {
        if(n % 2 == 1) {
            writeln(n);
        } else {
            writeln("No!");
        }
    }
}
```

We can define new types with `struct`, `class`, `union`, and `enum`. Structs and unions
are passed to functions by value (i.e. copied) and classes are passed by reference. Furthermore,
we can use templates to parameterize all of these on both types and values!

```d
// Here, 'T' is a type parameter. Think '<T>' from C++/C#/Java.
struct LinkedList(T) {
    T data = null;

    // Use '!' to instantiate a parameterized type. Again, think '<T>'.
    LinkedList!(T)* next;
}

class BinTree(T) {
    T data = null;

    // If there is only one template parameter, we can omit the parentheses.
    BinTree!T left;
    BinTree!T right;
}

enum Day {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

// Use alias to create abbreviations for types.
alias IntList = LinkedList!int;
alias NumTree = BinTree!double;

// We can create function templates as well!
T max(T)(T a, T b) {
    if(a < b)
        return b;

    return a;
}

// Use the ref keyword to ensure pass by reference. That is, even if 'a' and 'b'
// are value types, they will always be passed by reference to 'swap()'.
void swap(T)(ref T a, ref T b) {
    auto temp = a;

    a = b;
    b = temp;
}

// With templates, we can also parameterize on values, not just types.
class Matrix(uint m, uint n, T = int) {
    T[m] rows;
    T[n] columns;
}

auto mat = new Matrix!(3, 3); // We've defaulted type 'T' to 'int'.

```

Speaking of classes, let's talk about properties for a second. A property
is roughly a function that may act like an lvalue, so we can
have the syntax of POD structures (`structure.x = 7`) with the semantics of
getter and setter methods (`object.setX(7)`)!

```d
// Consider a class parameterized on types 'T' & 'U'.
class MyClass(T, U) {
    T _data;
    U _other;
}

// And "getter" and "setter" methods like so:
class MyClass(T, U) {
    T _data;
    U _other;

    // Constructors are always named 'this'.
    this(T t, U u) {
        // This will call the setter methods below.
        data = t;
        other = u;
    }

    // getters
    @property T data() {
        return _data;
    }

    @property U other() {
        return _other;
    }

    // setters
    @property void data(T t) {
        _data = t;
    }

    @property void other(U u) {
        _other = u;
    }
}

// And we use them in this manner:
void main() {
    auto mc = new MyClass!(int, string)(7, "seven");

    // Import the 'stdio' module from the standard library for writing to
    // console (imports can be local to a scope).
    import std.stdio;

    // Call the getters to fetch the values.
    writefln("Earlier: data = %d, str = %s", mc.data, mc.other);

    // Call the setters to assign new values.
    mc.data = 8;
    mc.other = "eight";

    // Call the getters again to fetch the new values.
    writefln("Later: data = %d, str = %s", mc.data, mc.other);
}
```

With properties, we can add any amount of logic to
our getter and setter methods, and keep the clean syntax of
accessing members directly!

Other object-oriented goodies at our disposal
include interfaces, abstract classes,
and overriding methods. D does inheritance just like Java:
Extend one class, implement as many interfaces as you please.

We've seen D's OOP facilities, but let's switch gears. D offers
functional programming with first-class functions, `pure`
functions, and immutable data. In addition, all of your favorite
functional algorithms (map, filter, reduce and friends) can be
found in the wonderful `std.algorithm` module!

```d
import std.algorithm : map, filter, reduce;
import std.range : iota; // builds an end-exclusive range

void main() {
    // We want to print the sum of a list of squares of even ints
    // from 1 to 100. Easy!

    // Just pass lambda expressions as template parameters!
    // You can pass any function you like, but lambdas are convenient here.
    auto num = iota(1, 101).filter!(x => x % 2 == 0)
                           .map!(y => y ^^ 2)
                           .reduce!((a, b) => a + b);

    writeln(num);
}
```

Notice how we got to build a nice Haskellian pipeline to compute num?
That's thanks to a D innovation know as Uniform Function Call Syntax (UFCS).
With UFCS, we can choose whether to write a function call as a method
or free function call! Walter wrote a nice article on this
[here.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394)
In short, you can call functions whose first parameter
is of some type A on any expression of type A as a method.

I like parallelism. Anyone else like parallelism? Sure you do. Let's do some!

```d
// Let's say we want to populate a large array with the square root of all
// consecutive integers starting from 1 (up until the size of the array), and we
// want to do this concurrently taking advantage of as many cores as we have
// available.

import std.stdio;
import std.parallelism : parallel;
import std.math : sqrt;

void main() {
    // Create your large array
    auto arr = new double[1_000_000];

    // Use an index, access every array element by reference (because we're
    // going to change each element) and just call parallel on the array!
    foreach(i, ref elem; parallel(arr)) {
        elem = sqrt(i + 1.0);
    }
}
```
---
language: dart
filename: learndart.dart
contributors:
    - ["Joao Pedrosa", "https://github.com/jpedrosa/"]
---

Dart is a newcomer into the realm of programming languages.
It borrows a lot from other mainstream languages, having as a goal not to deviate too much from
its JavaScript sibling. Like JavaScript, Dart aims for great browser integration.

Dart's most controversial feature must be its Optional Typing.

```dart
import "dart:collection";
import "dart:math" as DM;

// Welcome to Learn Dart in 15 minutes. http://www.dartlang.org/
// This is an executable tutorial. You can run it with Dart or on
// the Try Dart! site if you copy/paste it there. http://try.dartlang.org/

// Function declaration and method declaration look the same. Function
// declarations can be nested. The declaration takes the form of
// name() {} or name() => singleLineExpression;
// The fat arrow function declaration has an implicit return for the result of
// the expression.
example1() {
  example1nested1() {
    example1nested2() => print("Example1 nested 1 nested 2");
    example1nested2();
  }
  example1nested1();
}

// Anonymous functions don't include a name.
example2() {
  example2nested1(fn) {
    fn();
  }
  example2nested1(() => print("Example2 nested 1"));
}

// When a function parameter is declared, the declaration can include the
// number of parameters the function takes by specifying the names of the
// parameters it takes.
example3() {
  example3nested1(fn(informSomething)) {
    fn("Example3 nested 1");
  }
  example3planB(fn) { // Or don't declare number of parameters.
    fn("Example3 plan B");
  }
  example3nested1((s) => print(s));
  example3planB((s) => print(s));
}

// Functions have closure access to outer variables.
var example4Something = "Example4 nested 1";
example4() {
  example4nested1(fn(informSomething)) {
    fn(example4Something);
  }
  example4nested1((s) => print(s));
}

// Class declaration with a sayIt method, which also has closure access
// to the outer variable as though it were a function as seen before.
var example5method = "Example5 sayIt";
class Example5Class {
  sayIt() {
    print(example5method);
  }
}
example5() {
  // Create an anonymous instance of the Example5Class and call the sayIt
  // method on it.
  new Example5Class().sayIt();
}

// Class declaration takes the form of class name { [classBody] }.
// Where classBody can include instance methods and variables, but also
// class methods and variables.
class Example6Class {
  var example6InstanceVariable = "Example6 instance variable";
  sayIt() {
    print(example6InstanceVariable);
  }
}
example6() {
  new Example6Class().sayIt();
}

// Class methods and variables are declared with "static" terms.
class Example7Class {
  static var example7ClassVariable = "Example7 class variable";
  static sayItFromClass() {
    print(example7ClassVariable);
  }
  sayItFromInstance() {
    print(example7ClassVariable);
  }
}
example7() {
  Example7Class.sayItFromClass();
  new Example7Class().sayItFromInstance();
}

// Literals are great, but there's a restriction for what literals can be
// outside of function/method bodies. Literals on the outer scope of class
// or outside of class have to be constant. Strings and numbers are constant
// by default. But arrays and maps are not. They can be made constant by
// declaring them "const".
var example8A = const ["Example8 const array"],
  example8M = const {"someKey": "Example8 const map"};
example8() {
  print(example8A[0]);
  print(example8M["someKey"]);
}

// Loops in Dart take the form of standard for () {} or while () {} loops,
// slightly more modern for (.. in ..) {}, or functional callbacks with many
// supported features, starting with forEach.
var example9A = const ["a", "b"];
example9() {
  for (var i = 0; i < example9A.length; i++) {
    print("Example9 for loop '${example9A[i]}'");
  }
  var i = 0;
  while (i < example9A.length) {
    print("Example9 while loop '${example9A[i]}'");
    i++;
  }
  for (var e in example9A) {
    print("Example9 for-in loop '${e}'");
  }
  example9A.forEach((e) => print("Example9 forEach loop '${e}'"));
}

// To loop over the characters of a string or to extract a substring.
var example10S = "ab";
example10() {
  for (var i = 0; i < example10S.length; i++) {
    print("Example10 String character loop '${example10S[i]}'");
  }
  for (var i = 0; i < example10S.length; i++) {
    print("Example10 substring loop '${example10S.substring(i, i + 1)}'");
  }
}

// Int and double are the two supported number formats.
example11() {
  var i = 1 + 320, d = 3.2 + 0.01;
  print("Example11 int ${i}");
  print("Example11 double ${d}");
}

// DateTime provides date/time arithmetic.
example12() {
  var now = new DateTime.now();
  print("Example12 now '${now}'");
  now = now.add(new Duration(days: 1));
  print("Example12 tomorrow '${now}'");
}

// Regular expressions are supported.
example13() {
  var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$");
  match(s) {
    if (re.hasMatch(s)) {
      print("Example13 regexp matches '${s}'");
    } else {
      print("Example13 regexp doesn't match '${s}'");
    }
  }
  match(s1);
  match(s2);
}

// Boolean expressions need to resolve to either true or false, as no
// implicit conversions are supported.
example14() {
  var v = true;
  if (v) {
    print("Example14 value is true");
  }
  v = null;
  try {
    if (v) {
      // Never runs
    } else {
      // Never runs
    }
  } catch (e) {
    print("Example14 null value causes an exception: '${e}'");
  }
}

// try/catch/finally and throw are used for exception handling.
// throw takes any object as parameter;
example15() {
  try {
    try {
      throw "Some unexpected error.";
    } catch (e) {
      print("Example15 an exception: '${e}'");
      throw e; // Re-throw
    }
  } catch (e) {
    print("Example15 catch exception being re-thrown: '${e}'");
  } finally {
    print("Example15 Still run finally");
  }
}

// To be efficient when creating a long string dynamically, use
// StringBuffer. Or you could join a string array.
example16() {
  var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e;
  for (e in a) { sb.write(e); }
  print("Example16 dynamic string created with "
    "StringBuffer '${sb.toString()}'");
  print("Example16 join string array '${a.join()}'");
}

// Strings can be concatenated by just having string literals next to
// one another with no further operator needed.
example17() {
  print("Example17 "
      "concatenate "
      "strings "
      "just like that");
}

// Strings have single-quote or double-quote for delimiters with no
// actual difference between the two. The given flexibility can be good
// to avoid the need to escape content that matches the delimiter being
// used. For example, double-quotes of HTML attributes if the string
// contains HTML content.
example18() {
  print('Example18 <a href="etc">'
      "Don't can't I'm Etc"
      '</a>');
}

// Strings with triple single-quotes or triple double-quotes span
// multiple lines and include line delimiters.
example19() {
  print('''Example19 <a href="etc">
Example19 Don't can't I'm Etc
Example19 </a>''');
}

// Strings have the nice interpolation feature with the $ character.
// With $ { [expression] }, the return of the expression is interpolated.
// $ followed by a variable name interpolates the content of that variable.
// $ can be escaped like so \$ to just add it to the string instead.
example20() {
  var s1 = "'\${s}'", s2 = "'\$s'";
  print("Example20 \$ interpolation ${s1} or $s2 works.");
}

// Optional types allow for the annotation of APIs and come to the aid of
// IDEs so the IDEs can better refactor, auto-complete and check for
// errors. So far we haven't declared any types and the programs have
// worked just fine. In fact, types are disregarded during runtime.
// Types can even be wrong and the program will still be given the
// benefit of the doubt and be run as though the types didn't matter.
// There's a runtime parameter that checks for type errors which is
// the checked mode, which is said to be useful during development time,
// but which is also slower because of the extra checking and is thus
// avoided during deployment runtime.
class Example21 {
  List<String> _names;
  Example21() {
    _names = ["a", "b"];
  }
  List<String> get names => _names;
  set names(List<String> list) {
    _names = list;
  }
  int get length => _names.length;
  void add(String name) {
    _names.add(name);
  }
}
void example21() {
  Example21 o = new Example21();
  o.add("c");
  print("Example21 names '${o.names}' and length '${o.length}'");
  o.names = ["d", "e"];
  print("Example21 names '${o.names}' and length '${o.length}'");
}

// Class inheritance takes the form of class name extends AnotherClassName {}.
class Example22A {
  var _name = "Some Name!";
  get name => _name;
}
class Example22B extends Example22A {}
example22() {
  var o = new Example22B();
  print("Example22 class inheritance '${o.name}'");
}

// Class mixin is also available, and takes the form of
// class name extends SomeClass with AnotherClassName {}.
// It's necessary to extend some class to be able to mixin another one.
// The template class of mixin cannot at the moment have a constructor.
// Mixin is mostly used to share methods with distant classes, so the
// single inheritance doesn't get in the way of reusable code.
// Mixins follow the "with" statement during the class declaration.
class Example23A {}
class Example23Utils {
  addTwo(n1, n2) {
    return n1 + n2;
  }
}
class Example23B extends Example23A with Example23Utils {
  addThree(n1, n2, n3) {
    return addTwo(n1, n2) + n3;
  }
}
example23() {
  var o = new Example23B(), r1 = o.addThree(1, 2, 3),
    r2 = o.addTwo(1, 2);
  print("Example23 addThree(1, 2, 3) results in '${r1}'");
  print("Example23 addTwo(1, 2) results in '${r2}'");
}

// The Class constructor method uses the same name of the class and
// takes the form of SomeClass() : super() {}, where the ": super()"
// part is optional and it's used to delegate constant parameters to the
// super-parent's constructor.
class Example24A {
  var _value;
  Example24A({value: "someValue"}) {
    _value = value;
  }
  get value => _value;
}
class Example24B extends Example24A {
  Example24B({value: "someOtherValue"}) : super(value: value);
}
example24() {
  var o1 = new Example24B(),
    o2 = new Example24B(value: "evenMore");
  print("Example24 calling super during constructor '${o1.value}'");
  print("Example24 calling super during constructor '${o2.value}'");
}

// There's a shortcut to set constructor parameters in case of simpler classes.
// Just use the this.parameterName prefix and it will set the parameter on
// an instance variable of same name.
class Example25 {
  var value, anotherValue;
  Example25({this.value, this.anotherValue});
}
example25() {
  var o = new Example25(value: "a", anotherValue: "b");
  print("Example25 shortcut for constructor '${o.value}' and "
    "'${o.anotherValue}'");
}

// Named parameters are available when declared between {}.
// Parameter order can be optional when declared between {}.
// Parameters can be made optional when declared between [].
example26() {
  var _name, _surname, _email;
  setConfig1({name, surname}) {
    _name = name;
    _surname = surname;
  }
  setConfig2(name, [surname, email]) {
    _name = name;
    _surname = surname;
    _email = email;
  }
  setConfig1(surname: "Doe", name: "John");
  print("Example26 name '${_name}', surname '${_surname}', "
    "email '${_email}'");
  setConfig2("Mary", "Jane");
  print("Example26 name '${_name}', surname '${_surname}', "
  "email '${_email}'");
}

// Variables declared with final can only be set once.
// In case of classes, final instance variables can be set via constant
// constructor parameter.
class Example27 {
  final color1, color2;
  // A little flexibility to set final instance variables with syntax
  // that follows the :
  Example27({this.color1, color2}) : color2 = color2;
}
example27() {
  final color = "orange", o = new Example27(color1: "lilac", color2: "white");
  print("Example27 color is '${color}'");
  print("Example27 color is '${o.color1}' and '${o.color2}'");
}

// To import a library, use import "libraryPath" or if it's a core library,
// import "dart:libraryName". There's also the "pub" package management with
// its own convention of import "package:packageName".
// See import "dart:collection"; at the top. Imports must come before
// other code declarations. IterableBase comes from dart:collection.
class Example28 extends IterableBase {
  var names;
  Example28() {
    names = ["a", "b"];
  }
  get iterator => names.iterator;
}
example28() {
  var o = new Example28();
  o.forEach((name) => print("Example28 '${name}'"));
}

// For control flow we have:
// * standard switch with must break statements
// * if-else if-else and ternary ..?..:.. operator
// * closures and anonymous functions
// * break, continue and return statements
example29() {
  var v = true ? 30 : 60;
  switch (v) {
    case 30:
      print("Example29 switch statement");
      break;
  }
  if (v < 30) {
  } else if (v > 30) {
  } else {
    print("Example29 if-else statement");
  }
  callItForMe(fn()) {
    return fn();
  }
  rand() {
    v = new DM.Random().nextInt(50);
    return v;
  }
  while (true) {
    print("Example29 callItForMe(rand) '${callItForMe(rand)}'");
    if (v != 30) {
      break;
    } else {
      continue;
    }
    // Never gets here.
  }
}

// Parse int, convert double to int, or just keep int when dividing numbers
// by using the ~/ operation. Let's play a guess game too.
example30() {
  var gn, tooHigh = false,
    n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0;
  top = top ~/ 6;
  gn = new DM.Random().nextInt(top + 1); // +1 because nextInt top is exclusive
  print("Example30 Guess a number between 0 and ${top}");
  guessNumber(i) {
    if (n == gn) {
      print("Example30 Guessed right! The number is ${gn}");
    } else {
      tooHigh = n > gn;
      print("Example30 Number ${n} is too "
        "${tooHigh ? 'high' : 'low'}. Try again");
    }
    return n == gn;
  }
  n = (top - bottom) ~/ 2;
  while (!guessNumber(n)) {
    if (tooHigh) {
      top = n - 1;
    } else {
      bottom = n + 1;
    }
    n = bottom + ((top - bottom) ~/ 2);
  }
}

// Programs have only one entry point in the main function.
// Nothing is expected to be executed on the outer scope before a program
// starts running with what's in its main function.
// This helps with faster loading and even lazily loading of just what
// the program needs to startup with.
main() {
  print("Learn Dart in 15 minutes!");
  [example1, example2, example3, example4, example5, example6, example7,
    example8, example9, example10, example11, example12, example13, example14,
    example15, example16, example17, example18, example19, example20,
    example21, example22, example23, example24, example25, example26,
    example27, example28, example29, example30
    ].forEach((ef) => ef());
}

```

## Further Reading

Dart has a comprehensive web-site. It covers API reference, tutorials, articles and more, including a
useful Try Dart online.
http://www.dartlang.org/
http://try.dartlang.org/



---
language: asciidoc
contributors:
    - ["Ryan Mavilia", "http://unoriginality.rocks/"]
translators:
  - ["Dennis Keller", "https://github.com/denniskeller"]
filename: asciidoc-de.md
lang: de-de
---

AsciiDoc ist eine Auszeichnungssprache ähnlich zu Markdown. Sie kann für alles verwendet werden von Büchern zu Blogs. Erfunden wurde sie 2002 von Stuart Rackham. Die Sprache ist simpel aber sie ermöglicht eine große Anzahl an Anpassungen.

Kopfzeile des Dokuments

Kopfzeilen sind optional und dürfen keine Leerzeilen besitzen. Sie müssen mindestens eine Leerzeile vom Inhalt versetzt sein.

Nur Titel

```
= Dokumententitel

Erster Satz des Dokuments.
```

Titel und Autor

```
= Dokumententitel
Vorname Nachname <Vorname.Nachname@learnxinyminutes.com>

Start des Dokuments.
```

Mehrere Autoren

```
= Dokumententitel
John Doe <john@go.com>; Jane Doe<jane@yo.com>; Black Beard <beardy@pirate.com>

Starte ein Dokument mit mehreren Autoren.
```

Revisionszeile (benötigt eine Autorzeile)

```
= Dokumententitel V1
Potato Man <chip@crunchy.com>
v1.0, 2016-01-13

Dieser Artikel über Chips wird Spaß machen.
```

Absätze

```
Du musst nichts besonderes machen für Absätze.

Füge eine Leerzeile zwischen zwei Absätze, um sie zu trennen.

Um eine Leerzeile zu erhalten musst du ein +
ergänzen und du erhälst einen Umbruch!
```

Textformatierung

```
_Unterstriche erstellt Kursivschrift_
*Sternchen für Fett gedruckt*
*_Kombinieren für extra Spaß_*
`Benutze Ticks um Monospace zu signalisieren`
`*Fett gedruckter Monospace*`
```

Abteilungstitel

```
= Level 0 (sollte nur in der Kopfzeile verwendet werden)

== Level 1 <h2>

=== Level 2 <h3>

==== Level 3 <h4>

===== Level 4 <h5>

====== Level 5 <h6>

======= Level 6  <h7>

```

Listen

Um eine Aufzählung zu erstellen verwendest du Sternchen.

```
* foo
* bar
* baz
```

Um eine nummerierte Liste zu erstellen verwendest du Punkte.

```
. item 1
. item 2
. item 3
```

Um Listen zu verschachteln musst du zusätzliche Sternchen und Punkte hinzufügen. Dies ist bis zu fünf Mal möglich.

```
* foo 1
** foo 2
*** foo 3
**** foo 4
***** foo 5

. foo 1
.. foo 2
... foo 3
.... foo 4
..... foo 5
```
---
category: tool
tool: bash
lang: de-de
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
translators:
    - ["kultprok", "http://www.kulturproktologie.de"]
filename: LearnBash-de.sh
---

Bash ist der Name der Unix-Shell, die als Shell des GNU-Betriebssystems und auch als Standard-Shell von Linux und Mac OS X ausgeliefert wurde.
Beinahe alle der folgenden Beispiele können als Teile eines Shell-Skripts oder direkt in der Shell ausgeführt werden.

[Weitere Informationen \(Englisch\)](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# Die erste Zeile des Scripts nennt sich Shebang, dies gibt dem System an, 
# wie das Script ausgeführt werden soll: http://de.wikipedia.org/wiki/Shebang
# Du hast es bestimmt schon mitgekriegt, Kommentare fangen mit # an. Das Shebang ist auch ein Kommentar

# Ein einfaches Beispiel mit hello world:
echo Hello, world!

# Jeder Befehl fängt auf einer neuen Zeile oder nach einem Semikolon an:
echo 'Dies ist die erste Zeile'; echo 'Dies die zweite Zeile'

# Variablen deklariert man so:
Variable="irgendein String"

# Aber nicht so:
Variable = "irgendein String"
# Bash wird 'Variable' für einen Befehl halten, den es ausführen soll. Es wird einen Fehler ausgeben, 
# weil es den Befehl nicht findet. 

# Und so auch nicht:
Variable= 'Some string'
# Bash wird 'Variable' wieder für einen Befehl halten, den es ausführen soll. Es wird einen Fehler ausgeben, 
# Hier wird der Teil 'Variable=' als nur für diesen einen Befehl gültige Zuweisung an die Variable gesehen.

# Eine Variable wird so benutzt:
echo $Variable
echo "$Variable"
echo ${Variable}
# aber
echo '$Variable'
# Wenn du eine Variable selbst benutzt – ihr Werte zuweist, sie exportierst oder irgendetwas anderes –, 
# dann über ihren Namen ohne $. Aber wenn du ihren zugewiesenen Wert willst, dann musst du $ voranstellen.
# Beachte: ' (Hochkomma) verhindert das Interpretieren der Variablen

# Ersetzen von Zeichenketten in Variablen
echo ${Variable/irgendein/neuer}
# Ersetzt das erste Vorkommen von "irgendein" durch "neuer"

# Teil einer Zeichenkette
Laenge=7
echo ${Variable:0:Laenge}
# Gibt nur die ersten 7 Zeichen zurück

# Standardwert verwenden
echo ${Foo:-"ErsatzWennLeerOderUngesetzt"}
# Das funktioniert mit nicht gesetzten Variablen (Foo=) und leeren Zeichenketten (Foo="")
# Die Zahl 0 (Foo=0) liefert 0.
# Beachte: der wert der Variablen wird nicht geändert

# Eingebaute Variable (BUILTINS):
# Einige nützliche Beispiele
echo "Rückgabewert des letzten Befehls: $?"
echo "Die PID des skripts: $$"
echo "Anzahl der Argumente beim Aufruf: $#"
echo "Alle Argumente beim Aufruf: $@"
echo "Die Argumente in einzelnen Variablen: $1 $2..."

# Einen Wert aus der Eingabe lesen:
echo "Wie heisst du?"
read NAME # Wir mussten nicht mal eine neue Variable deklarieren
echo Hello, $NAME!

# Wir haben die übliche if-Struktur:
# 'man test' liefert weitere Informationen zu Bedingungen
if [ "$NAME" -ne $USER ]
then
    echo "Dein Name ist nicht dein Login-Name"
else
    echo "Dein Name ist dein Login-Name"
fi

# Es gibt auch bedingte Ausführung
echo "immer ausgeführt" || echo "Nur ausgeführt wenn der erste Befehl fehlschlägt"
echo "immer ausgeführt" && echo "Nur ausgeführt wenn der erste Befehl Erfolg hat"

# Um && und || mit if statements zu verwenden, braucht man mehrfache Paare eckiger Klammern:
if [ "$NAME" == "Steve" ] && [ "$Alter" -eq 15 ]
then
    echo "Wird ausgeführt wenn $NAME gleich 'Steve' UND $Alter gleich 15."
fi

if [ "$Name" == "Daniya" ] || [ "$Name" == "Zach" ]
then
    echo "Wird ausgeführt wenn $NAME gleich 'Daniya' ODER $NAME gleich 'Zach'."
fi

# Ausdrücke haben folgendes Format:
echo $(( 10 + 5 ))

# Anders als andere Programmiersprachen ist Bash eine Shell – es arbeitet also im Kontext von Verzeichnissen.
# Du kannst alle Dateien und Verzeichnisse im aktiven Verzeichnis mit ls auflisten:
ls

# Diese Befehle haben Optionen, die ihre Ausführung beeinflussen:
ls -l # Liste alle Dateien und Unterverzeichnisse auf einer eigenen Zeile auf

# Ergebnisse eines vorangegangenen Befehls können an den nächsten Befehl als Input übergeben werden.
# Der grep-Befehl filtert den Input nach dem vorgegebenen Muster. So können wir alle
# txt-Dateien im aktuellen Verzeichnis auflisten:
ls -l | grep "\.txt"

# Ein- und Ausgabe können umgeleitet werden (stdin, stdout, and stderr).
# Von stdin lesen bis "EOF" allein in einer Zeile auftaucht
# und die Datei hello.py mit den Zeilen zwischen den beiden "EOF"
# überschreiben:
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Führe hello.py mit verschiedenen Umleitungen von
# stdin, stdout und stderr aus:
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# Die Fehlerausgabe würde die Datei "error.err" überschreiben (falls sie existiert) 
# verwende ">>" um stattdessen anzuhängen:
python hello.py >> "output.out" 2>> "error.err"

# Überschreibe output.out, hänge an error.err an und zähle die Zeilen beider Dateien:
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# Führe einen Befehl aus und gib dessen "file descriptor" (zB /dev/fd/123) aus
# siehe: man fd
echo <(echo "#helloworld")

# Mehrere Arten, um output.out mit "#helloworld" zu überschreiben:
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# Löschen der Hilfsdateien von oberhalb, mit Anzeige der Dateinamen
# (mit '-i' für "interactive" erfolgt für jede Date eine Rückfrage)
rm -v output.out error.err output-and-error.log

# Die Ausgabe von Befehlen kann mit Hilfe von $( ) in anderen Befehlen verwendet weden:
# Der folgende Befehl zeigt die Anzahl aller Dateien und Unterverzeichnisse
# im aktuellen Verzeichnis an.
echo "Dieser Ordner beinhaltet $(ls | wc -l) Dateien und Verzeichnisse."

# Dasselbe kann man mit "backticks" `` erreichen, aber diese können
# nicht verschachtelt werden. $() ist die empfohlene Methode.
echo "Dieser Ordner beinhaltet `ls | wc -l` Dateien und Verzeichnisse."

# Bash nutzt einen case-Ausdruck, der sich ähnlich wie switch in Java oder C++ verhält.
case "$Variable"
in
    # Liste der Fälle, die unterschieden werden sollen
    0) echo "Hier ist eine Null."
    1) echo "Hier ist eine Eins."
    *) echo "Das ist etwas anderes."
esac

# 'for' Schleifen iterieren über die angegebene Zahl von Argumenten:
# Der Inhalt von $Variable wird dreimal ausgedruckt.
for $Variable in {1..3}
do
    echo "$Variable"
done

# Oder verwende die "traditionelle 'for'-Schleife":
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Schleifen können auch mit Dateien arbeiten:
# 'cat' zeigt zuerst file1 an und dann file2
for Variable in file1 file2
do
    cat "$Variable"
done

# .. oder mit der Ausgabe eines Befehls:
# Ausgabe des Inhalts jeder Datei, die von 'ls' aufgezählt wird
for Output in $(ls)
do
    cat "$Output"
done

# while Schleife:
while [ true ]
do
    echo "Schleifenkörper..."
    break
done

# Funktionen definieren
# Definition:
function foo ()
{
    echo "Argumente funktionieren wie bei skripts: $@"
    echo Und: $1 $2..."
    echo "Dies ist eine Funktion"
    return 0
}

# oder einfacher
bar ()
{
    echo "Auch so kann man Funktionen deklarieren!"
    return 0
}

# Aufruf der Funktion:
foo "My name is" $Name

# Was du noch lernen könntest:
# Ausgabe der letzten 10 Zeilen von file.txt
tail -n 10 file.txt
# Ausgabe der ersten 10 Zeilen von file.txt
head -n 10 file.txt
# sortierte Ausgabe von file.txt
sort file.txt
# Mehrfachzeilen in sortierten Dateien unterdrücken
# oder (mit -d) nur diese ausgeben
uniq -d file.txt
# Ausgabe nur der ersten Spalte (vor dem ersten ',')
cut -d ',' -f 1 file.txt
# ersetze in file.txt jedes vorkommende 'gut' durch 'super' (versteht regex)
sed -i 's/gut/super/g' file.txt
# Ausgabe nach stdout aller Zeilen von file.txt, die auf eine regex passen
# Im Beispiel: Zeilen, die mit "foo" beginnen und mit "bar" enden
grep "^foo.*bar$" file.txt
# Mit der Option "-c" wird stattdessen die Anzahl der gefundenen Zeilen ausgegeben
grep -c "^foo.*bar$" file.txt
# verwende 'fgrep' oder 'grep -F' wenn du buchstäblich nach den Zeichen
# suchen willst, ohne sie als regex zu interpretieren
fgrep "^foo.*bar$" file.txt

# Dokumentation über die in bash eingebauten Befehle
# bekommst du mit dem eingebauten Befehl 'help'
help
help help
help for
help return
help source
help .

# Das bash-Handbuch liest du mit 'man'
apropos bash
man 1 bash
man bash

# Dann gibt es noch das 'info' System (drücke ? um Hilfe angezeigt zu bekommen)
apropos info | grep '^info.*('
man info
info info
info 5 info

# info Dokumentation über bash:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: brainfuck
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["urfuchs", "https://github.com/urfuchs"]
filename: brainfuck-de
lang: de-de

---

Brainfuck ist eine extrem minimalistische Turing-vollständige Programmiersprache
mit lediglich 8 Befehlen.

Mit dem [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/) kann
Brainfuck im Browser ausprobiert werden.

```
Alle Zeichen außer "><+-.,[]" (ohne die Klammern) werden ignoriert.

Brainfuck besteht aus einem Array mit unendlich vielen Elementen, die alle mit Null initalisiert
sind und einem Datenzeiger auf das aktuelle Element.

Es gibt acht Befehle:
+ : Erhöht den Wert an der aktuellen Stelle um Eins.
- : Verringert den Wert an der aktuellen Stelle um Eins.
> : Bewegt den Zeiger um eine Stelle weiter.
< : Bewegt den Zeiger um eine Stelle zurück.
. : Gibt den Wert der aktuellen Zelle als ASCII Wert aus (z.B. 65 = 'A').
, : Liest ein einzelnes Zeichen von der Standardeingabe und speichert dessen ASCII Wert in der aktuellen Zelle.
[ : Wenn der Wert des aktuellen Elements Null ist, bewege des Zeiger hinter den
    zugehörigen ]-Befehl.
    Ansonsten, bewege den Zeiger ein Element weiter.
] : Wenn der Wert des aktuellen Elements Null ist, bewege des Zeiger um eine Stelle
    weiter.
    Ansonsten, bewege den Zeiger hinter den zugehörigen [-Befehl.

[ und ] bilden eine while-Schleife. Offensichtlich müssen sie paarweise vorkommen.

Schauen wir uns einige grundlegende Programme an.

++++++ [ > ++++++++++ < - ] > +++++ .

Dieses Programm gibt den Buchstaben 'A' aus. Zunächst erhöht es den Wert der 1. Zelle auf 6.
Diese erste Zelle wird für die Schleife verwendet. Danach beginnt das Programm
die Schleife ([) und geht vor zu Zelle #2. Es erhöht den Zellwert inkrementell 10 Mal, geht dann zurück
zu Zelle #1, und verringert Zelle #1. Diese Schleife wird 6 Mal durchlaufen (nach 6
Durchläufen ist der Wert der Zelle #1 auf 0 reduziert, dadurch wird die Schleife abgebrochen
und das Programm hinter dem korrespondierenden ] fortgesetzt).

An dieser Stelle befinden wir uns an Zelle #1, die jetzt den Wert 0 hat, während Zelle #2
den Wert 60 hat. Wir gehen vor zu Zelle #2, inkrementieren 5 Mal, bis zum Wert 65,
und geben dann den Wert der Zelle #2 aus. 65 ist ein 'A' im ASCII Zeichensatz,
daher wird 'A' am Terminal ausgegeben..


, [ > + < - ] > .

Dieses Programm liest ein Zeichen von der Benutzereingabe und schreibt dessen Wert
in Zelle #1. Danach beginnt eine Schleife. Rücke vor auf Zelle #2, erhöhe den Wert der Zelle #2,
gehe zurück auf Zelle #1, verringere den Wert der Zelle #1. Dies geht solange bis
Zelle #1 den Wert 0 und Zelle #2 den alten Wert aus #1 hat. Da wir am Ende der Schleife
bie Zelle #1 sind, gehe vor zu Zelle #2 und gibt denb Wert als ASCII Zeichen aus.

Beachte biite, dass die Leerzeichen nur aus Gründen der Lesbarkeit geschrieben werden.
Man könnte genauso schreiben:

,[>+<-]>.

Versuche herauszufinden, was dieses Programm macht:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Dieses Programm nimmt zwei Zahlen als Eingabe und multipliziert sie.

Im Wesentlichen liest es zunächst zwei Werte ein. Dann beginnt die äußere Schleife
mit Zelle #1 als Zähler. Danach geht das Programm zu Zelle #2 vor und startet die innere Schleife
mit Zelle #2 als Zähler. Diese zählt Zelle #3 hoch. Es gibt jedoch ein Problem:
Am Ende der inneren Schleife hat Zelle #2 den Wert Null. Daher würde die innere
Schleife beim nächsten Durchgang nicht mehr funktionieren. Daher wird auch Zelle #4
erhöht und anschließend in Zelle #2 zurückkopiert.
Am Ende steht in Zelle #3 das Ergebnis.
```

Das ist Brainfuck. Nicht so schwierig, oder? Zum Spaß kannst du dein eigenes Brainfuck
Programm schreiben oder du schreibst einen Brainfuck Interpreter in einer anderen
Programmiersprache. Der Interpreter lässt sich ziemlich einfach implementieren.
Falls du Masochist bist, kannst du auch versuchen, einen Brainfuck Interpreter in Brainfuck zu implementieren.
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
  - ["Frederik Ring", "https://github.com/m90"]
  - ["Philipp Fischbeck", "https://github.com/PFischbeck"]
filename: coffeescript-de.coffee
lang: de-de
---

CoffeeScript ist eine kleine Sprache, die eins zu eins nach JavaScript übersetzt wird - es findet keine Interpretation zur Laufzeit statt.
Als Nachfolger von JavaScript konzipiert, gibt CoffeeScript sein Bestes, lesbaren, gut formatierten und sauber laufenden JavaScript-Code zu erzeugen, der in jeder JavaScript-Laufzeit einwandfrei funktioniert.

Auf [der CoffeeScript Website](http://coffeescript.org/) gibt es ein ausführliches Tutorial.

``` coffeescript
# CoffeeScript ist eine dieser Sprachen für "Hipster"
# und folgt daher vielen Trends und Einflüssen aus modernen Sprachen.
# Kommentare werden daher wie in Ruby und Python mit Hashes gekennzeichnet

###
Kommentarblöcke sehen aus wie diese und werden direkt nach '/ *'s und '* /'s
im erzeugten JavaScript umgewandelt.

Vorweg: bevor du mit CoffeeScript anfängst, solltest du bereits einen guten
Überblick über die Sprache JavaScript haben.
###

# Zuweisung:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# Bedingungen:
number = -42 if opposite #=> if(opposite) { number = -42; }

# Funktionen:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

fill = (container, liquid = "Kaffee") ->
  "#{container} wird mit #{liquid} gefüllt..."
#=>var fill;
#
#fill = function(container, liquid) {
#  if (liquid == null) {
#    liquid = "Kaffee";
#  }
#  return container + " wird mit " + liquid + " gefüllt...";
#};

# "Ranges":
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# Objekte:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#  "root": Math.sqrt,
#  "square": square,
#  "cube": function(x) { return x * square(x); }
#}

# "Splats":
race = (winner, runners...) ->
  print winner, runners
#=>race = function() {
#  var runners, winner;
#  winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#  return print(winner, runners);
#};

# Existenz-Operator:
alert "Hab ich's nicht gesagt?" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("Hab ich's nicht gesagt?"); }

# Listen-Abstraktion:
cubes = (math.cube num for num in list)
#=>cubes = (function() {
#	var _i, _len, _results;
#	_results = [];
# 	for (_i = 0, _len = list.length; _i < _len; _i++) {
#		num = list[_i];
#		_results.push(math.cube(num));
#	}
#	return _results;
#  })();

foods = ['Brokkoli', 'Spinat', 'Schokolade']
eat food for food in foods when food isnt 'Schokolade'
#=>foods = ['Brokkoli', 'Spinat', 'Schokolade'];
#
#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
#  food = foods[_k];
#  if (food !== 'Schokolade') {
#    eat(food);
#  }
#}
```

## Weiterführende Links

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: c#
contributors:
    - ["Irfan Charania", "https://github.com/irfancharania"]
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Melvyn Laïly", "http://x2a.yt"]
    - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
translators:
    - ["Frederik Ring", "https://github.com/m90"]
filename: LearnCSharp-de.cs
lang: de-de
---
C# ist eine elegante, typsichere und objektorientierte Sprache, mit der Entwickler eine Vielzahl sicherer und robuster Anwendungen erstellen können, die im .NET Framework ausgeführt werden.

[Mehr über C# erfährst du hier.](http://msdn.microsoft.com/de-de/library/vstudio/z1zx9t92.aspx)

```c#
// Einzeilige Kommentare starten mit zwei Schrägstrichen: //
/*
Mehrzeile Kommentare wie in C Schrägstrich / Stern
*/
/// <summary>
/// XML-Kommentare können zur automatisierten Dokumentation verwendet werden
/// </summary>

// Zu Beginn werden die in der Datei verwendeten Namespaces aufgeführt
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;
using System.IO;

// definiert einen Namespace um Code in "packages" zu organisieren
namespace Learning
{
    // Jede .cs-Datei sollte zumindest eine Klasse mit dem Namen der Datei
    // enthalten. Das ist zwar nicht zwingend erforderlich, es anders zu
    // handhaben führt aber unweigerlich ins Chaos (wirklich)!
    public class LearnCSharp
    {
        // Zuerst erklärt dieses Tutorial die Syntax-Grundlagen,
        // wenn du bereits Java oder C++ programmieren kannst:
        // lies bei "Interessante Features" weiter!
        public static void Syntax()
        {
            // Mit Console.WriteLine kannst du einfachen Text ausgeben:
            Console.WriteLine("Hallo Welt");
            Console.WriteLine(
                "Integer: " + 10 +
                " Double: " + 3.14 +
                " Boolean: " + true);

            // Console.Write erzeugt keinen Zeilenumbruch
            Console.Write("Hallo ");
            Console.Write("Welt");

            ///////////////////////////////////////////////////
            // Typen & Variablen
            ///////////////////////////////////////////////////

            // Deklariere eine Variable mit <Typ> <Name>

            // Sbyte - Vorzeichenbehaftete 8-Bit Ganzzahl
            // (-128 <= sbyte <= 127)
            sbyte fooSbyte = 100;

            // Byte - Vorzeichenlose 8-Bit Ganzzahl
            // (0 <= byte <= 255)
            byte fooByte = 100;

            // Short - 16-Bit Ganzzahl
            // Vorzeichenbehaftet - (-32,768 <= short <= 32,767)
            // Vorzeichenlos - (0 <= ushort <= 65,535)
            short fooShort = 10000;
            ushort fooUshort = 10000;

            // Integer - 32-bit Ganzzahl
            int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647)
            uint fooUint = 1; // (0 <= uint <= 4,294,967,295)

            // Long - 64-bit Ganzzahl
            long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
            ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
            // Ganze Zahlen werden standardmäßig - je nach Größe - als int oder
            // uint behandelt. Ein nachgestelltes L markiert den Wert als long
            // oder ulong.

            // Double - Double-precision 64-bit IEEE 754 Fließkommazahl
            double fooDouble = 123.4; // Genauigkeit: 15-16 Stellen

            // Float - Single-precision 32-bit IEEE 754 Fließkommazahl
            float fooFloat = 234.5f; // Genauigkeit: 7 Stellen
            // Das nachgestellte f zeigt an dass es sich um einen Wert vom Typ
            // float handelt

            // Decimal - ein 128-Bit-Datentyp mit größerer Genauigkeit als
            // andere Fließkommatypen, und somit bestens geeignet für
            // die Berechnung von Geld- und Finanzwerten
            decimal fooDecimal = 150.3m;

            // Boolean - true & false
            bool fooBoolean = true; // oder false

            // Char - Ein einzelnes 16-Bit Unicode Zeichen
            char fooChar = 'A';

            // Strings - im Gegensatz zu allen vorhergehenden Basistypen, die
            // alle Werttypen sind, ist String ein Referenztyp. Strings sind
            // somit nullable, Werttypen sind dies nicht.
            string fooString = "\"maskiere\" Anführungszeichen, und füge \n (Umbrüche) und \t (Tabs) hinzu";
            Console.WriteLine(fooString);

            // Jeder Buchstabe eines Strings kann über seinen Index
            // referenziert werden:
            char charFromString = fooString[1]; // => 'e'
            // Strings sind unveränderlich:
            // `fooString[1] = 'X';` funktioniert nicht

            // Ein Vergleich zweier Strings, unter Berücksichtigung der
            // aktuellen, sprachspezifischen Gegebenheiten (also z.B. a,ä,b,c
            // in deutschsprachigen Umgebungen), und ohne Beachtung von
            // Groß- und Kleinschreibung:
            string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);

            // Formatierung, genau wie "sprintf"
            string fooFs = string.Format("Mikrofon Check, {0} {1}, {0} {1:0.0}", 1, 2);

            // Datumsangaben und Formatierung
            DateTime fooDate = DateTime.Now;
            Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));

            // Durch ein vorangestelltes @ lässt sich ein mehrzeiliger String
            // schreiben. Um " zu maskieren benutzt man ""
            string bazString = @"Hier geht es
zur nächsten Zeile, ""Wahnsinn!"", die Massen waren kaum zu bändigen";

            // Die Keywords const oder readonly kennzeichnen eine
            // unveränderliche Variable/Konstante. Die Werte von Konstanten
            // werden übrigens bereits zur Compile-Zeit berechnet.
            const int HOURS_I_WORK_PER_WEEK = 9001;

            ///////////////////////////////////////////////////
            // Datenstrukturen
            ///////////////////////////////////////////////////

            // Arrays - Index beginnt bei Null
            // Die Größe des Arrays wird bei der Deklaration festgelegt.
            // Die syntaktische Struktur um ein neues Array zu erzeugen sieht
            // folgendermaßen aus:
            // <datatype>[] <varname> = new <datatype>[<array size>];
            int[] intArray = new int[10];

            // Arrays können auch über ein Array-Literal deklariert werden:
            int[] y = { 9000, 1000, 1337 };

            // Indizierung eines Arrays - Zugriff auf ein bestimmtes Element
            Console.WriteLine("intArray @ 0: " + intArray[0]);
            // Arrays sind veränderbar
            intArray[1] = 1;

            // Listen
            // Durch ihre größere Flexibilität kommen Listen in C# weit
            // häufiger zum Einsatz als Arrays. Eine Liste wird so deklariert:
            // List<datatype> <varname> = new List<datatype>();
            List<int> intList = new List<int>();
            List<string> stringList = new List<string>();
            List<int> z = new List<int> { 9000, 1000, 1337 };
            // Die <> kennzeichnen "Generics", mehr dazu unter "Coole Sachen"

            // Listen haben keinen Default-Wert.
            // Bevor auf einen Index zugegriffen werden kann, muss dieser
            // auch gesetzt worden sein:
            intList.Add(1);
            Console.WriteLine("intList @ 0: " + intList[0]);

            // Andere interessante Datenstrukturen sind:
            // Stack/Queue
            // Dictionary (entspricht einer Hash Map)
            // HashSet
            // Read-only Collections
            // Tuple (.Net 4+)

            ///////////////////////////////////////
            // Operatoren
            ///////////////////////////////////////
            Console.WriteLine("\n->Operatoren");

            // kurze Schreibweise um mehrere Deklarationen zusammenzufassen:
            // (Benutzung vom C# Styleguide aber ausdrücklich abgeraten!)
            int i1 = 1, i2 = 2;

            // Arithmetik funktioniert wie erwartet:
            Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3

            // Modulo
            Console.WriteLine("11%3 = " + (11 % 3)); // => 2

            // Vergleiche
            Console.WriteLine("3 == 2? " + (3 == 2)); // => false
            Console.WriteLine("3 != 2? " + (3 != 2)); // => true
            Console.WriteLine("3 > 2? " + (3 > 2)); // => true
            Console.WriteLine("3 < 2? " + (3 < 2)); // => false
            Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true
            Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true

            // Bitweise Operatoren
            /*
            ~       Unäres bitweises NICHT
            <<      Verschieben nach links
            >>      Verschieben nach rechts
            &       Bitweises UND
            ^       Bitweises exklusives ODER
            |       Bitweises inklusives ODER
            */

            // Inkremente
            int i = 0;
            Console.WriteLine("\n->Inkrement / Dekrement");
            Console.WriteLine(i++); //i = 1. Post-Inkrement
            Console.WriteLine(++i); //i = 2. Pre-Inkrement
            Console.WriteLine(i--); //i = 1. Post-Dekrement
            Console.WriteLine(--i); //i = 0. Pre-Dekrement

            ///////////////////////////////////////
            // Kontrollstrukturen
            ///////////////////////////////////////
            Console.WriteLine("\n->Kontrollstrukturen");

            // If-Statements funktionieren wie in C
            int j = 10;
            if (j == 10)
            {
                Console.WriteLine("Ich werde ausgegeben");
            }
            else if (j > 10)
            {
                Console.WriteLine("Ich nicht");
            }
            else
            {
                Console.WriteLine("Ich leider auch nicht");
            }

            // Ternärer Operator
            // Anstatt eines einfachen if/else lässt sich auch folgendes schreiben:
            // <condition> ? <true> : <false>
            int zumVergleich = 17;
            string isTrue = zumVergleich == 17 ? "Ja" : "Nein";

            // while-Schleife
            int fooWhile = 0;
            while (fooWhile < 100)
            {
                // Wird 100mal wiederholt, fooWhile 0->99
                fooWhile++;
            }

            // do-while-Schleife
            int fooDoWhile = 0;
            do
            {
                // Wird 100mal wiederholt, fooDoWhile 0->99
                fooDoWhile++;
            } while (fooDoWhile < 100);

            //for-Schleifen => for(<start_statement>; <conditional>; <step>)
            for (int fooFor = 0; fooFor < 10; fooFor++)
            {
                // Wird 10mal wiederholt, fooFor 0->9
            }

            // foreach-Schleife
            // Die normale Syntax für eine foreach-Schleife lautet:
            // foreach(<iteratorType> <iteratorName> in <enumerable>)
            // foreach kann mit jedem Objekt verwendet werden das IEnumerable
            // oder IEnumerable<T> implementiert. Alle Auflistungs-Typen
            // (Array, List, Dictionary...) im .NET Framework implementieren
            // eines dieser beiden Interfaces.

            foreach (char character in "Hallo Welt".ToCharArray())
            {
                // Ein Durchgang für jedes Zeichen im String
            }
            // (ToCharArray() könnte man hier übrigens auch weglassen,
            // da String IEnumerable bereits implementiert)

            // Switch Struktur
            // Ein Switch funktioniert mit byte, short, char und int Datentypen.
            // Auch Aufzählungstypen können verwendet werden, genau wie
            // die Klasse String, und ein paar Sonderklassen, die Wrapper für
            // Primitives sind: Character, Byte, Short und Integer
            int month = 3;
            string monthString;
            switch (month)
            {
                case 1:
                    monthString = "Januar";
                    break;
                case 2:
                    monthString = "Februar";
                    break;
                case 3:
                    monthString = "März";
                    break;
                // Man kann für mehrere Fälle auch das selbe Verhalten
                // definieren. Jeder Block muss aber mit einem break-Statement
                // abgeschlossen werden. Einzelne Fälle können über
                // `goto case x` erreicht werden
                case 6:
                case 7:
                case 8:
                    monthString = "Sommer!!";
                    break;
                default:
                    monthString = "Irgendein anderer Monat";
                    break;
            }

            ///////////////////////////////////////
            // Umwandlung von Datentypen und Typecasting
            ///////////////////////////////////////

            // Umwandlung

            // von String nach Integer
            // bei einem Fehler wirft diese Code eine Exception
            int.Parse("123"); //gibt die Ganzzahl 123 zurück

            // TryParse gibt bei einem Fehler den Default-Wert zurück
            // (im Fall von int: 0)
            int tryInt;
            if (int.TryParse("123", out tryInt)) // gibt true oder false zurück
            {
                Console.WriteLine(tryInt);       // 123
            }

            // von Integer nach String
            // Die Klasse Convert stellt Methoden zur Konvertierung von
            // unterschiedlichsten Daten zur Verfügung:
            Convert.ToString(123); // "123"
            // oder
            tryInt.ToString(); // "123"
        }

        ///////////////////////////////////////
        // Klassen
        ///////////////////////////////////////
        public static void Classes()
        {

            // Benutze das new-Keyword um eine Instanz einer Klasse zu erzeugen
            Bicycle trek = new Bicycle();

            // So werden Methoden der Instanz aufgerufen
            trek.SpeedUp(3); // Es empfiehlt sich immer Getter und Setter zu benutzen
            trek.Cadence = 100;

            // ToString ist eine Konvention über die man üblicherweiser
            // Informationen über eine Instanz erhält
            Console.WriteLine("Infos zu trek: " + trek.ToString());

            // Wir instantiieren ein neues Hochrad
            PennyFarthing funbike = new PennyFarthing(1, 10);
            Console.WriteLine("Infos zu funbike: " + funbike.ToString());

            Console.Read();
        } // Ende der Methode main

        // Main als Konsolenstartpunkt
        // Eine Konsolenanwendung muss eine Methode Main als Startpunkt besitzen
        public static void Main(string[] args)
        {
            OtherInterestingFeatures();
        }

        ///////////////////////////////////////
        // Interessante Features
        ///////////////////////////////////////

        // Methodensignaturen

        public // Sichtbarkeit
        static // Erlaubt einen Zugriff auf der Klasse (nicht auf einer Instanz)
        int // Typ des Rückgabewerts,
        MethodSignatures(
            // Erstes Argument, erwartet int
            int maxCount,
            // setzt sich selbst auf 0 wenn kein anderer Wert übergeben wird
            int count = 0,
            int another = 3,
            // enthält alle weiteren der Methode übergebenen Parameter (quasi Splats)
            params string[] otherParams
        )
        {
            return -1;
        }

        // Methoden können überladen werden, solange sie eindeutige
        // Signaturen haben
        public static void MethodSignatures(string maxCount)
        {
        }

        // Generische Typen
        // Die Typen für TKey und TValue werden erst beim Aufruf der Methode
        // festgelegt. Diese Methode emuliert z.B. SetDefault aus Python:
        public static TValue SetDefault<TKey, TValue>(
            IDictionary<TKey, TValue> dictionary,
            TKey key,
            TValue defaultItem)
        {
            TValue result;
            if (!dictionary.TryGetValue(key, out result))
            {
                return dictionary[key] = defaultItem;
            }
            return result;
        }

        // Möglichen Typen lassen sich auch über ihr Interface beschränken:
        public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
        {
            // Da T ein IEnumerable ist können wir foreach benutzen
            foreach (var item in toPrint)
            {
                // Item ist ein int
                Console.WriteLine(item.ToString());
            }
        }

        public static void OtherInterestingFeatures()
        {
            // Optionale Parameter
            MethodSignatures(3, 1, 3, "Ein paar", "extra", "Strings");
            // setzt explizit einen bestimmten Parameter, andere werden übersprungen
            MethodSignatures(3, another: 3);

            // Erweiterungsmethoden
            int i = 3;
            i.Print(); // Weiter unten definiert

            // Nullables - perfekt für die Interaktion mit
            // Datenbanken / Rückgabewerten
            // Jeder Wert (d.h. keine Klassen) kann durch das Nachstellen eines ?
            // nullable gemacht werden: <type>? <varname> = <value>
            int? nullable = null; // Die explizite Langform wäre Nullable<int>
            Console.WriteLine("Mein Nullable: " + nullable);
            bool hasValue = nullable.HasValue; // true wenn nicht null

            // ?? ist "syntaktischer Zucker" um einen Defaultwert für den Fall
            // dass die Variable null ist festzulegen.
            int notNullable = nullable ?? 0; // 0

            // Implizit typisierte Variablen
            // Man kann auch den Typ einer Variable auch vom Compiler
            // bestimmen lassen:
            var magic = "magic ist zur Compile-Zeit ein String, folglich geht keine Typsicherheit verloren";
            magic = 9; // funktioniert nicht da magic vom Typ String ist

            // Generics
            var phonebook = new Dictionary<string, string>() {
                {"Resi", "08822 / 43 67"} // Fügt einen Eintrag zum Telefonbuch hinzu
            };

            // Hier könnte man auch unser generisches SetDefault von
            // weiter oben benutzen:
            Console.WriteLine(SetDefault<string,string>(phonebook, "Xaver", "kein Telefon")); // kein Telefon
            // TKey und TValue müssen nicht zwingend angegeben werden, da sie
            // auch implizit vom Compiler ermittelt werden können
            Console.WriteLine(SetDefault(phonebook, "Resi", "kein Telefon")); // 08822 / 43 67

            // Lambdas - konzise Syntax für Inline-Funktionen
            Func<int, int> square = (x) => x * x; // Das letzte Element vom Typ T ist der Rückgabewert
            Console.WriteLine(square(3)); // 9

            // Disposables - einfaches Management von nicht verwalteten Ressourcen
            // So gut wie alle Objekte die auf nicht verwaltete Ressourcen
            // (Dateien, Geräte, ...) zugreifen, implementieren das Interface
            // IDisposable. Das using Statement stellt sicher dass die vom
            // IDisposable benutzten Ressourcen nach der Benutzung wieder
            // freigegeben werden:
            using (StreamWriter writer = new StreamWriter("log.txt"))
            {
                writer.WriteLine("Alles bestens!");
                // Am Ende des Codeblocks werden die Ressourcen wieder
                // freigegeben - auch im Falle einer Exception
            }

            // Parallel Klasse
            // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
            var websites = new string[] {
                "http://www.google.com", "http://www.reddit.com",
                "http://www.shaunmccarthy.com"
            };
            var responses = new Dictionary<string, string>();

            // Für jeden Request wird ein neuer Thread erzeugt, der nächste
            // Schritt wird erst nach Beendigung aller Tasks ausgeführt
            Parallel.ForEach(websites,
                // maximal 3 Threads gleichzeitig
                new ParallelOptions() {MaxDegreeOfParallelism = 3},
                website =>
            {
                // Hier folgt eine langwierige, asynchrone Operation
                using (var r = WebRequest.Create(new Uri(website)).GetResponse())
                {
                    responses[website] = r.ContentType;
                }
            });

            // Dieser Code wird erst nach Beendigung aller Requests ausgeführt
            foreach (var key in responses.Keys)
            {
                Console.WriteLine("{0}:{1}", key, responses[key]);
            }

            // Dynamische Objekte (gut um mit anderen Sprachen zu arbeiten)
            dynamic student = new ExpandoObject();
            // hier muss keine Typ angegeben werden
            student.FirstName = "Christian";

            // Einem solchen Objekt kann man sogar Methoden zuordnen.
            // Das Beispiel gibt einen String zurück und erwartet einen String
            student.Introduce = new Func<string, string>(
                (introduceTo) => string.Format("Hallo {0}, das ist {1}", student.FirstName, introduceTo));
            Console.WriteLine(student.Introduce("Bettina"));

            // IQueryable<T> - So gut wie alle Aufzählungstypen implementieren
            // dieses Interface, welches eine Vielzahl von funktionalen Methoden
            // wie Map / Filter / Reduce zur Verfügung stellt:
            var bikes = new List<Bicycle>();
            // sortiert die Liste
            bikes.Sort();
            // sortiert nach Anzahl Räder
            bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels));
            var result = bikes
                // diese Filter können auch aneinandergehängt werden
                .Where(b => b.Wheels > 3) // (gibt ein IQueryable des vorherigen Typs zurück)
                .Where(b => b.IsBroken && b.HasTassles)
                // diese Zuordnung gibt ein IQueryable<String> zurück
                .Select(b => b.ToString());

            // "Reduce" - addiert alle Räder der Aufzählung zu einem Wert
            var sum = bikes.Sum(b => b.Wheels);

            // So erzeugt man ein implizit typisiertes Objekt, basierend auf
            // den Parametern der Elemente:
            var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
            // Auch wenn wir es hier nicht demonstrieren können:
            // In einer IDE wie VisualStudio kriegen wir hier sogar TypeAhead,
            // da der Compiler in der Lage ist, die passenden Typen zu erkennen.
            foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome))
            {
                Console.WriteLine(bikeSummary.Name);
            }

            // AsParallel-Methode
            // Jetzt kommen die Schmankerl! Die AsParallel-Methode kombiniert
            // LINQ und parallele Operationen:
            var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
            // Diese Berechnung passiert parallel! Benötigte Threads werden
            // automatisch erzeugt, und die Rechenlast unter ihnen aufgeteilt.
            // Ein Traum für die Verarbeitung von großen Datenmengen
            // auf mehreren Cores!

            // LINQ - bildet einen Datenspeicher auf IQueryable<T> Objekte ab
            // LinqToSql beispielsweise speichert und liest aus einer
            // SQL-Datenbank, LinqToXml aus einem XML-Dokument.
            // LINQ-Operationen werden "lazy" ausgeführt.
            var db = new BikeRepository();

            // Die verzögerte Ausführung ist optimal für Datenbankabfragen
            var filter = db.Bikes.Where(b => b.HasTassles); // noch keine Abfrage
            // Es können noch mehr Filter hinzugefügt werden (auch mit
            // Bedingungen) - ideal für z.B. "erweiterte Suchen"
            if (42 > 6)
            {
                filter = filter.Where(b => b.IsBroken); // immer noch keine Abfrage
            }

            var query = filter
                .OrderBy(b => b.Wheels)
                .ThenBy(b => b.Name)
                .Select(b => b.Name); // auch hier: immer noch keine Abfrage

            // Erst hier wird die Datenbankabfrage wirklich ausgeführt,
            // limitiert auf die Elemente die der foreach-Loop verwendet
            foreach (string bike in query)
            {
                Console.WriteLine(result);
            }

        }

    } // Ende der Klasse LearnCSharp

    // Eine .cs-Datei kann auch mehrere Klassen enthalten

    public static class Extensions
    {
        // Erweiterungsmethoden
        public static void Print(this object obj)
        {
            Console.WriteLine(obj.ToString());
        }
    }

    // Syntax zur Deklaration einer Klasse:
    // <public/private/protected/internal> class <class name>{
    //    // Datenfelder, Konstruktoren und Methoden leben alle
    //    // innerhalb dieser Deklaration
    // }

    public class Bicycle
    {
        // Felder/Variablen der Klasse "Bicycle"
        // Das Keyword public macht das Member von überall zugänglich
        public int Cadence
        {
            get // get definiert eine Methode um die Eigenschaft abzurufen
            {
                return _cadence;
            }
            set // set definiert eine Methode um die Eigenschaft zu setzen
            {
                _cadence = value; // value ist der dem Setter übergebene Wert
            }
        }
        private int _cadence;

        // Das Keyword protected macht das Member nur für die Klasse selbst
        // und ihre Subklassen zugänglich
        protected virtual int Gear
        {
            get; // erzeugt eine Eigenschaft für die kein "Zwischenwert" benötigt wird
            set;
        }

        // Das Keyword internal macht das Member innerhalb der Assembly zugänglich
        internal int Wheels
        {
            get;
            private set; // get/set kann auch über Keywords modifiziert werden
        }

        int _speed; // Member ohne vorangestellte Keywords sind standardmäßig
                    // private, sie sind nur innerhalb der Klasse zugänglich.
                    // Man kann aber natürlich auch das Keyword private benutzen.
        private string Name { get; set; }

        // Ein Enum ist ein klar definierter Satz an benannten Konstanten.
        // Eigentlich ordnet es diese Konstanten nur bestimmten Werten zu
        // (einer int-Zahl, solange nicht anders angegeben). Mögliche Typen für
        // die Werte eines Enums sind byte, sbyte, short, ushort, int, uint,
        // long, oder ulong. Alle Werte in einem Enum sind eindeutig.
        public enum BikeBrand
        {
            Colnago,
            EddyMerckx,
            Bianchi = 42, // so kann man den Wert explizit setzen
            Kynast // 43
        }
        // Nachdem dieser Typ in der Klasse "Bicycle" definiert ist,
        // sollte Code ausserhalb der Klasse den Typen als Bicycle.Brand referenzieren

        // Nachdem das Enum deklariert ist, können wir den Typen verwenden:
        public BikeBrand Brand;

        // Als static gekennzeichnete Member gehören dem Typ selbst,
        // nicht seinen Instanzen. Man kann sie also ohne Referenz zu einem
        // Objekt benutzen
        // Console.WriteLine("Schon " + Bicycle.BicyclesCreated + " Fahrräder, nur für dieses Tutorial!");
        static public int BicyclesCreated = 0;

        // readonly-Werte werden zur Laufzeit gesetzt
        // Ihr Wert kann nur bei ihrer Deklaration, oder in einem Konstruktor
        // festgelegt werden
        readonly bool _hasCardsInSpokes = false; // readonly und private

        // Konstruktoren bestimmen was bei einer Instantiierung passiert.
        // Das ist ein Default-Konstruktor:
        public Bicycle()
        {
            // Member der Klasse können über das Keyword this erreicht werden
            this.Gear = 1;
            // oft ist das aber gar nicht nötig
            Cadence = 50;
            _speed = 5;
            Name = "Bonanzarad";
            Brand = BikeBrand.Kynast;
            BicyclesCreated++;
        }

        // Das ist ein spezifischer Konstruktor (d.h. er erwartet Argumente):
        public Bicycle(int startCadence, int startSpeed, int startGear,
                       string name, bool hasCardsInSpokes, BikeBrand brand)
            : base() // ruft zuerst den "base"-Konstruktor auf
        {
            Gear = startGear;
            Cadence = startCadence;
            _speed = startSpeed;
            Name = name;
            _hasCardsInSpokes = hasCardsInSpokes;
            Brand = brand;
        }

        // Konstruktoren können aneinandergehängt werden:
        public Bicycle(int startCadence, int startSpeed, BikeBrand brand) :
            this(startCadence, startSpeed, 0, "richtig große Räder", true, brand)
        {
        }

        // Syntax für Methoden:
        // <public/private/protected> <return type> <function name>(<args>)

        // Klassen können Getter und Setter für Werte definieren,
        // oder diese Werte direkt als Eigenschaft implementieren
        // (in C# der bevorzugte Weg)

        // Parameter von Methoden können Default-Werte haben.
        // "SpeedUp" kann man also auch ohne Parameter aufrufen:
        public void SpeedUp(int increment = 1)
        {
            _speed += increment;
        }

        public void SlowDown(int decrement = 1)
        {
            _speed -= decrement;
        }

        // Eigenschaften mit get/set
        // wenn es nur um den Zugriff auf Daten geht, ist eine Eigenschaft zu
        // empfehlen. Diese können Getter und Setter haben, oder auch nur
        // einen Getter bzw. einen Setter
        private bool _hasTassles; // private Variable
        public bool HasTassles // öffentliches Interface
        {
            get { return _hasTassles; }
            set { _hasTassles = value; }
        }

        // Das kann man auch kürzer schreiben:
        // Dieser Syntax erzeugt automatisch einen hinterlegten Wert,
        // (entsprechend `private bool _isBroken`) der gesetzt
        // bzw. zurückgegeben wird:
        public bool IsBroken { get; private set; }
        public int FrameSize
        {
            get;
            // für Getter und Setter kann der Zugriff auch einzeln
            // beschränkt werden, FrameSize kann also nur von innerhalb
            // der Klasse "Bicycle" gesetzt werden
            private set;
        }

        // Diese Methode gibt eine Reihe an Informationen über das Objekt aus:
        public virtual string ToString()
        {
            return "Gang: " + Gear +
                    " Kadenz: " + Cadence +
                    " Geschwindigkeit: " + _speed +
                    " Name: " + Name +
                    " Hipster-Karten zwischen den Speichen: " + (_hasCardsInSpokes ? "Na klar!" : "Bloß nicht!") +
                    "\n------------------------------\n"
                    ;
        }

        // Auch Methoden können als static gekennzeichnet werden, nützlich
        // beispielsweise für Helper-Methoden
        public static bool DidWeCreateEnoughBicyclesYet()
        {
            // In einer statischen Methode können wir natürlich auch nur
            // statische Member der Klasse referenzieren
            return BicyclesCreated > 9000;
        }
        // Wenn eine Klasse nur statische Member enthält, kann es eine gute Idee
        // sein die Klasse selbst als static zu kennzeichnen

    } // Ende der Klasse "Bicycle"

    // "PennyFarthing" ist eine Unterklasse von "Bicycle"
    class PennyFarthing : Bicycle
    {
        // (Hochräder - englisch Penny Farthing - sind diese antiken Fahrräder
        // mit riesigem Vorderrad. Sie haben keine Gangschaltung.)

        // hier wird einfach der Elternkonstruktor aufgerufen
        public PennyFarthing(int startCadence, int startSpeed) :
            base(startCadence, startSpeed, 0, "Hochrad", true, BikeBrand.EddyMerckx)
        {
        }

        protected override int Gear
        {
            get
            {
                return 0;
            }
            set
            {
                throw new ArgumentException("Ein Hochrad hat keine Gangschaltung, doh!");
            }
        }

        public override string ToString()
        {
            string result = "Hochrad ";
            result += base.ToString(); // ruft die "base"-Version der Methode auf
            return result;
        }
    }

    // Interfaces (auch Schnittstellen genant) definieren nur die Signaturen
    // ihrer Member, enthalten aber auf keinen Fall ihre Implementierung:
    interface IJumpable
    {
        // Alle Member eines Interfaces sind implizit public
        void Jump(int meters);
    }

    interface IBreakable
    {
        // Interfaces können Eigenschaften, Methoden und Events definieren
        bool Broken { get; }
    }

    // Eine Klasse kann nur von einer Klasse erben, kann aber eine beliebige
    // Anzahl von Interfaces implementieren
    class MountainBike : Bicycle, IJumpable, IBreakable
    {
        int damage = 0;

        public void Jump(int meters)
        {
            damage += meters;
        }

        public bool Broken
        {
            get
            {
                return damage > 100;
            }
        }
    }

    // Das hier stellt eine Datenbankverbindung für das LinqToSql-Beispiel her.
    // EntityFramework Code First ist großartig
    // (ähnlich zu Ruby's ActiveRecord, aber bidirektional)
    // http://msdn.microsoft.com/de-de/data/jj193542.aspx
    public class BikeRepository : DbSet
    {
        public BikeRepository()
            : base()
        {
        }

        public DbSet<Bicycle> Bikes { get; set; }
    }
} // Ende des Namespaces
```

## In dieser Übersicht nicht enthalten sind die Themen:

 * Flags
 * Attributes
 * Statische Eigenschaften
 * Exceptions, Abstraction
 * ASP.NET (Web Forms/MVC/WebMatrix)
 * Winforms
 * Windows Presentation Foundation (WPF)

## Zum Weiterlesen gibt es viele gute Anlaufpunkte:

 * [DotNetPerls](http://www.dotnetperls.com)
 * [C# in Depth](http://manning.com/skeet2)
 * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
 * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
 * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx)
 * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
 * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/overview/exploring-webmatrix)
 * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
 * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)

[C# Coding Conventions](http://msdn.microsoft.com/de-de/library/vstudio/ff926074.aspx)
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
translators:
    - ["Kyr", "http://github.com/kyrami"]
lang: de-de
filename: learncss-de.css
---

In den frühen Tagen des Internets gab es keine visuellen Elemente, alles war nur reiner Text. Aber mit der Weiterentwicklung von Browsern wurden auch vollständig visuelle Webseiten zu einem Standard.
Durch Verwendung von CSS lässt sich eine strikte Trennung zwischen HTML-Code und Designelementen erreichen.

Kurzgefasst, CSS ermöglicht es, verschiedene HTML-Elemente innerhalb eines Dokuments auszuwählen und ihnen visuelle Eigenschaften zu geben.

CSS hat wie jede andere Sprache viele Versionen. Hier fokussieren wir uns auf CSS2.0, welche nicht die neueste, aber die am weitesten verbreitete und unterstützte Version ist.

**HINWEIS:** Weil die Ausgabe von CSS visuelle Eigenschaften sind, wirst du wahrscheinlich eine CSS-Sandbox wie [dabblet](http://dabblet.com/) benutzen müssen, um die Sprache richtig zu lernen.
In diesem Artikel wird am meisten auf generelle Hinweise und die Syntax geachtet.


```css
/* Kommentare werden in Sternchen-Schrägstrichkombinationen gepackt (genauso wie hier!) */

/* ####################
   ## SELEKTOREN
   ####################*/

/* Eigentlich ist das grundlegende CSS-Statement sehr simpel */
selektor { eigenschaft: wert; /* mehr eigenschaften...*/ }

/* Der Selektor wird dazu benutzt, ein Element auf der Seite auszuwählen.

Man kann aber auch alle Elemente auf einer Seite auswählen! */
* { color:red; } /* farbe:rot */

/*
Angenommen wir haben folgendes Element auf einer Seite:

<div class='eine-klasse klasse2' id='eineId' attr='wert' />
*/

/* kann man es so über seine Klasse auswählen */
.eine-klasse { }

/* oder über beide Klassen! */
.eine-klasse.klasse2 { }

/* oder über den Namen des Tags */
div { }

/* oder über seine Id */
#eineId { }

/* oder darüber, dass es ein Attribut hat! */
[attr] { font-size:smaller; }

/* oder auch darüber, dass das Attribut einen bestimmten Wert hat */
[attr='wert'] { font-size:smaller; }

/* beginnt mit dem übergebenen Wert */
[attr^='we'] { font-size:smaller; }

/* endet damit */
[attr$='rt'] { font-size:smaller; }

/* oder beinhaltet einen Teil davon */
[attr~='er'] { font-size:smaller; }


/* Noch wichtiger ist aber die Möglichkeit, all das miteinander kombinieren
zu können - man sollte hierbei nur mit der Leerzeichensetzung vorsichtig sein,
ein Leerzeichen macht es zu zwei verschiedenen Selektoren */

div.eine-klasse[attr$='rt'] { } /* so ist es richtig */

/* Man kann auch ein Element über seine Elternelemente auswählen */

/* > wählt ein direktes Kind aus */
div.ein-elternteil > .klassen-name {}

/* Mit einem Leerzeichen getrennt kann man alle Elternelemente ansprechen */
/* Das folgende heißt also, dass jedes Element mit der Klasse 'klassen-name'
und dem Elternteil IN JEDER TIEFE ausgewählt wird */
div.ein-elternteil .klassen-name {}

/* Achtung: das selbe ohne das Leerzeichen hat eine andere Bedeutung,
kannst du mir sagen, was? */
div.ein-elternteil.klassen-name {}

/* Man kann ein Element auch nach seinem direkten Nachbarelement
auswählen */
.ich-bin-vorher + .dieses-element { }

/* Oder über jedes Geschwisterelement davor */
.ich-kann-jeder-davor-sein ~ .dieses-element {}

/* Mit Pseudoklassen lassen sich Elemente anhand ihres momentanen Zustands
auf der Seite auswählen (anstatt über die Seitenstruktur) */

/* Zum Beispiel, wenn über ein Element mit dem Mauszeiger gefahren wird */
:hover {}

/* Oder einen bereits besuchten Link*/
:visited {}

/* Oder einen noch nicht besuchten Link*/
:link {}

/* Oder ein Eingabeelement, das zurzeit im Fokus steht */
:focus {}


/* ####################
   ## EIGENSCHAFTEN
   ####################*/

selector {

    /* Einheiten */
    width: 50%; /* in Prozent */
    font-size: 2em; /* mal der derzeitigen Schriftgröße */
    width: 200px; /* in Pixeln */
    font-size: 20pt; /* in Punkten */
    width: 5cm; /* in Zentimetern */
    width: 50mm; /* in Millimetern */
    width: 5in; /* in Zoll */

    /* Farben */
    background-color: #F6E  /* in kurzem Hex */
    background-color: #F262E2 /* in langem Hex */
    background-color: tomato /* kann auch eine benannte Farbe sein */
    background-color: rgb(255, 255, 255) /* in RGB */
    background-color: rgb(10%, 20%, 50%) /* in RGB Prozent */
    background-color: rgba(255, 0, 0, 0.3); /* in semi-transparentem RGB */

    /* Bilder */
    background-image: url(/pfad-zum-bild/image.jpg);

    /* Schriften */
    font-family: Arial;
    font-family: "Courier New"; /* wenn der Name ein Leerzeichen beinhält, kommt er in
    Anführungszeichen */
    font-family: "Courier New", Trebuchet, Arial; /* wird die erste Schriftart 
    nicht gefunden, wird die zweite benutzt, usw. */
}

```

## Benutzung

Speichere das CSS, das du benutzen willst, mit der Endung '.css'.

```xml
<!-- du musst die CSS-Datei im <head>-bereich der Seite einbinden -->
<link rel='stylesheet' type='text/css' href='filepath/filename.css' />

<!-- Einbindung funktioniert auch inline, wobei diese Methode nicht
empfohlen ist -->
<style>
   selector { property:value; }
</style>

<!-- Oder direkt auf einem Element (sollte aber vermieden werden) -->
<div style='property:value;'>
</div>

```

## Spezifität

Ein Element kann natürlich auch von mehr als einer Regel in einem Stylesheet
angesprochen werdenm und kann eine Eigenschaft auch öfters als einmal zugewiesen
bekommen. In diesen Fällen gibt es Regeln, die die Spezifität von Selektoren regeln.

Wir haben dieses CSS:

```css
/*A*/
p.klasse1[attr='wert']

/*B*/
p.klasse1 {}

/*C*/
p.klasse2 {}

/*D*/
p {}

/*E*/
p { property: wert !important; }

```

und das folgende Markup:

```xml
<p style='/*F*/ property:value;' class='class1 class2' attr='value'>
</p>
```

Die Spezifität der Stile ist wie folgt:
(die Spezifität gilt nur für **einzelne Eigenschaften**, nicht für ganze Blöcke)

* `E` hat die größte Spezifität wegen des Schlüsselworts `!important`.
	man sollte diese Form aber vermeiden.
* `F` ist als nächstes dran, da es direkt an dem Element definiert ist.
* Dann folgt `A`, da es "spezifischer" als alle anderen ist.
	spezifischer = mehr Zuweisungen: 1 Tagname `p` +
	Klassenname `klasse1` + 1 Attribut `attr='value'`
* `C` kommt als nächstes, obwohl es genau so ist wie `B`,
	es erscheint aber später im Stylesheet.
* dann kommt `B`
* und als letztes `D`.

## Kompatibilität

Die meisten Features von CSS sind in allen Browsern verfügbar. Man sollte
jedoch immer darauf achten die benutzten Features auf Verfügbarkeit in den
vom Projekt unterstützten Browser zu überprüfen.

[QuirksMode CSS](http://www.quirksmode.org/css/) oder [Can I Use](http://caniuse.com/) sind zwei der besten Quellen dafür.

## Weiterlesen

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)

---
language: D 
filename: learnd-de.d 
contributors:
    - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]
translators:
    - ["Dominik Süß", "www.thesuess.me"]
lang: de-de
---

```c
// Es war klar dass das kommt...
module hello;

import std.stdio;

// argumente sind optional
void main(string[] args) {
    writeln("Hello, World!");
}
```

Wenn du so wie ich bist und viel zeit im Internet verbringst stehen die Chancen gut
das du schonmal über [D](http://dlang.org/) gehört hast.
Die D-Sprache ist eine moderne, überall einsetzbare programmiersprache die von Low bis
High Level verwendet werden kann und dabei viele Stile anbietet.

D wird aktiv von Walter Bright und Andrei Alexandrescu entwickelt, zwei super schlaue,
richtig coole leute. Da das jetzt alles aus dem weg ist - auf zu den Beispielen!

```c
import std.stdio;

void main() {

    // Logische Ausdrücke und Schleifen funktionieren wie erwartet
    for(int i = 0; i < 10000; i++) {
        writeln(i);
    }

    auto n = 1; // auto um den typ vom Compiler bestimmen zu lassen
    
    // Zahlenliterale können _ verwenden für lesbarkeit
    while(n < 10_000) {
        n += n;
    }

    do {
        n -= (n / 2);
    } while(n > 0);

    // For und while sind ja schön und gut aber D bevorzugt foreach
    // Die '..' erstellen eine Spanne von Zahlen, inklusive dem ersten Wert
    // jedoch ohne dem letzten
    foreach(i; 1..1_000_000) {
        if(n % 2 == 0)
            writeln(i);
    }

    // Es gibt auch ein 'foreach_reverse' wenn du rückwerts gehen willst.
    foreach_reverse(i; 1..int.max) {
        if(n % 2 == 1) {
            writeln(i);
        } else {
            writeln("No!");
        }
    }
}
```

Neue Typen können mit `struct`, `class`, `union`, und `enum` definiert werden. Structs und unions
werden as-value (koppiert) an methoden übergeben wogegen Klassen als Referenz übergeben werden.
Templates können verwendet werden um alle typen zu parameterisieren.

```c
// Hier, T ist ein Type-Parameter, Er funktioniert wie Generics in C#/Java/C++
struct LinkedList(T) {
    T data = null;
    LinkedList!(T)* next; // Das ! wird verwendet um T zu übergeben. (<T> in C#/Java/C++)
}

class BinTree(T) {
    T data = null;
    
    // Wenn es nur einen T parameter gibt können die Klammern um ihn weggelassen werden
    BinTree!T left;
    BinTree!T right;
}

enum Day {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

// Aliase können verwendet werden um die Entwicklung zu erleichtern

alias IntList = LinkedList!int;
alias NumTree = BinTree!double;

// Funktionen können genau so Templates beinhalten

T max(T)(T a, T b) {
    if(a < b)
        return b;

    return a;
}

// Steht ref vor einem Parameter wird sichergestellt das er als Referenz übergeben wird.
// Selbst bei werten wird es immer eine Referenz sein.
void swap(T)(ref T a, ref T b) {
    auto temp = a;

    a = b;
    b = temp;
}

// Templates können ebenso werte parameterisieren.
class Matrix(uint m, uint n, T = int) {
    T[m] rows;
    T[n] columns;
}

auto mat = new Matrix!(3, 3); // Standardmäßig ist T vom typ Integer

```

Wo wir schon bei Klassen sind - Wie wäre es mit Properties! Eine Property
ist eine Funktion die wie ein Wert agiert. Das gibt uns viel klarere Syntax
im Stil von `structure.x = 7` was gleichgültig wäre zu `structure.setX(7)`

```c
// Diese Klasse ist parameterisiert mit T, U

class MyClass(T, U) {
    T _data;
    U _other;

}

// Ihre Getter und Setter Methoden sehen so aus
class MyClass(T, U) {
    T _data;
    U _other;
    
    // Konstruktoren heißen immer `this`
    this(T t, U u) {
        data = t;
        other = u;
    }
    
    // getters
    @property T data() {
        return _data;
    }

    @property U other() {
        return _other;
    }

    // setters    
	// @property kann genauso gut am ende der Methodensignatur stehen
    void data(T t) @property {
        _data = t;
    }

    void other(U u) @property {
        _other = u;
    }
}
// Und so kann man sie dann verwenden

void main() {
    auto mc = MyClass!(int, string);

    mc.data = 7;
    mc.other = "seven";
    
    writeln(mc.data);
    writeln(mc.other);
}
```

Mit properties können wir sehr viel logik hinter unseren gettern
und settern hinter einer schönen syntax verstecken

Other object-oriented goodies at our disposal
Andere Objektorientierte features sind beispielsweise
`interface`s, `abstract class` und `override`.
Vererbung funktioniert in D wie in Java:
Erben von einer Klasse, so viele interfaces wie man will.

Jetzt haben wir Objektorientierung in D gesehen aber schauen
wir uns noch was anderes an.
D bietet funktionale programmierung mit _first-class functions_
puren funktionen und unveränderbare daten.
Zusätzlich können viele funktionale Algorithmen wie z.B
map, filter, reduce und friends im `std.algorithm` Modul gefunden werden!

```c
import std.algorithm : map, filter, reduce;
import std.range : iota; // builds an end-exclusive range

void main() {
    // Wir wollen die summe aller quadratzahlen zwischen
    // 1 und 100 ausgeben. Nichts leichter als das!
 
    // Einfach eine lambda funktion als template parameter übergeben
    // Es ist genau so gut möglich eine normale funktion hier zu übergeben
	// Lambdas bieten sich hier aber an.
    auto num = iota(1, 101).filter!(x => x % 2 == 0)
                           .map!(y => y ^^ 2)
                           .reduce!((a, b) => a + b);

    writeln(num);
}
```

Ist dir aufgefallen wie wir eine Haskell-Style pipeline gebaut haben
um num zu berechnen?
Das war möglich durch die Uniform Function Call Syntax.
Mit UFCS können wir auswählen ob wir eine Funktion als Methode oder
als freie Funktion aufrufen. Walters artikel dazu findet ihr
[hier.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394) 
Kurzgesagt kann man Funktionen deren erster parameter vom typ A ist, als
Methode auf A anwenden.

Parrallel Computing ist eine Tolle sache, findest du nicht auch?

```c
import std.stdio;
import std.parallelism : parallel;
import std.math : sqrt;

void main() {
    // Wir wollen die Wurzel von jeder Zahl in unserem Array berechnen
    // und dabei alle Kerne verwenden die wir zur verfügung haben
    auto arr = new double[1_000_000];

    // Wir verwenden den index und das element als referenz
    // und rufen einfach parallel auf!
    foreach(i, ref elem; parallel(arr)) {
        ref = sqrt(i + 1.0);
    }
}

```
---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
translators:
    - ["Gregor Große-Bölting", "http://www.ideen-und-soehne.de"]
filename: learnelixir-de.ex
lang: de-de
---

Elixir ist eine moderne, funktionale Sprache für die Erlang VM. Sie ist voll 
kompatibel mit Erlang, verfügt aber über eine freundlichere Syntax und bringt 
viele Features mit. 

```ruby

# Einzeilige Kommentare werden mit der Raute gesetzt.

# Es gibt keine mehrzeiligen Kommentare;
# es ist aber problemlos möglich mehrere einzeilige Kommentare hintereinander 
# zu setzen (so wie hier).

# Mit 'iex' ruft man die Elixir-Shell auf.
# Zum kompilieren von Modulen dient der Befehl 'elixirc'.

# Beide Befehle sollten als Umgebungsvariable gesetzt sein, wenn Elixir korrekt
# installiert wurde.

## ---------------------------
## -- Basistypen
## ---------------------------

# Es gibt Nummern:
3    # Integer
0x1F # Integer
3.0  # Float

# Atome, das sind Literale, sind Konstanten mit Namen. Sie starten mit einem 
# ':'.
:hello # Atom

# Außerdem gibt es Tupel, deren Werte im Arbeitsspeicher vorgehalten werden.
{1,2,3} # Tupel

# Die Werte innerhalb eines Tupels können mit der 'elem'-Funktion ausgelesen
# werden:
elem({1, 2, 3}, 0) # => 1

# Listen sind als verkettete Listen implementiert.
[1, 2, 3] # list

# Auf Kopf und Rest einer Liste kann wie folgt zugegriffen werden:
[ kopf | rest ] = [1,2,3]
kopf # => 1
rest # => [2, 3]

# In Elixir, wie auch in Erlang, kennzeichnet '=' ein 'pattern matching'
# (Musterabgleich) und keine Zuweisung.
# Das heißt, dass die linke Seite auf die rechte Seite 'abgeglichen' wird.
# Auf diese Weise kann im Beispiel oben auf Kopf und Rest der Liste zugegriffen
# werden.

# Ein Musterabgleich wird einen Fehler werfen, wenn die beiden Seiten nicht 
# zusammenpassen. 
# Im folgenden Beispiel haben die Tupel eine unterschiedliche Anzahl an
# Elementen:
{a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# Es gibt außerdem 'binaries', 
<<1,2,3>> # binary.

# Strings und 'char lists'
"hello" # String
'hello' # Char-Liste

# ... und mehrzeilige Strings
"""
Ich bin ein 
mehrzeiliger String.
"""
#=> "Ich bin ein\nmehrzeiliger String.\n"

# Alles Strings werden in UTF-8 enkodiert:
"héllò" #=> "héllò"

# Eigentlich sind Strings in Wahrheit nur binaries und 'char lists' einfach
# Listen.
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# In Elixir gibt `?a` den ASCII-Integer für den Buchstaben zurück.
?a #=> 97

# Um Listen zu verbinden gibt es den Operator '++', für binaries nutzt man '<>'
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'hello ' ++ 'world'  #=> 'hello world'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"hello " <> "world"  #=> "hello world"

## ---------------------------
## -- Operatoren
## ---------------------------

# Einfache Arithmetik
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# In Elixir gibt der Operator '/' immer einen Float-Wert zurück.

# Für Division mit ganzzahligen Ergebnis gibt es 'div'
div(10, 2) #=> 5

# Um den Rest der ganzzahligen Division zu erhalten gibt es 'rem'
rem(10, 3) #=> 1

# Natürlich gibt es auch Operatoren für Booleans: 'or', 'and' und 'not'. Diese
# Operatoren erwarten einen Boolean als erstes Argument. 
true and true #=> true
false or true #=> true
# 1 and true    #=> ** (ArgumentError) argument error

# Elixir bietet auch '||', '&&' und '!', die Argumente jedweden Typs 
# akzeptieren. Alle Werte außer 'false' und 'nil' werden zu wahr evaluiert.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil

!true #=> false

# Für Vergleiche gibt es die Operatoren `==`, `!=`, `===`, `!==`, `<=`, `>=`,
# `<` und `>` 
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# '===' und '!==' sind strikter beim Vergleich von Integern und Floats:
1 == 1.0  #=> true
1 === 1.0 #=> false

# Es ist außerdem möglich zwei verschiedene Datentypen zu vergleichen:
1 < :hello #=> true

# Die gesamte Ordnung über die Datentypen ist wie folgt definiert:
# number < atom < reference < functions < port < pid < tuple < list < bitstring

# Um Joe Armstrong zu zitieren: "The actual order is not important, but that a
# total ordering is well defined is important." 

## ---------------------------
## -- Kontrollstrukturen
## ---------------------------

# Es gibt die `if`-Verzweigung
if false do
  "Dies wird nie jemand sehen..."
else
  "...aber dies!"
end

# ...und ebenso `unless`
unless true do
  "Dies wird nie jemand sehen..."
else
  "...aber dies!"
end

# Du erinnerst dich an 'pattern matching'? Viele Kontrollstrukturen in Elixir
# arbeiten damit.

# 'case' erlaubt es uns Werte mit vielerlei Mustern zu vergleichen.
case {:one, :two} do
  {:four, :five} ->
    "Das wird nicht passen"
  {:one, x} ->
    "Das schon und außerdem wird es ':two' dem Wert 'x' zuweisen."
  _ ->
    "Dieser Fall greift immer."
end

# Es ist eine übliche Praxis '_' einen Wert zuzuweisen, sofern dieser Wert
# nicht weiter verwendet wird.
# Wenn wir uns zum Beispiel nur für den Kopf einer Liste interessieren:
[kopf | _] = [1,2,3]
kopf #=> 1

# Für bessere Lesbarkeit können wir auch das Folgende machen:
[kopf | _rest] = [:a, :b, :c]
kopf #=> :a

# Mit 'cond' können diverse Bedingungen zur selben Zeit überprüft werden. Man
# benutzt 'cond' statt viele if-Verzweigungen zu verschachteln.
cond do
  1 + 1 == 3 ->
    "Ich werde nie aufgerufen."
  2 * 5 == 12 ->
    "Ich auch nicht."
  1 + 2 == 3 ->
    "Aber ich!"
end

# Es ist üblich eine letzte Bedingung einzufügen, die immer zu wahr evaluiert. 
cond do
  1 + 1 == 3 ->
    "Ich werde nie aufgerufen."
  2 * 5 == 12 ->
    "Ich auch nicht."
  true ->
    "Aber ich! (dies ist im Grunde ein 'else')"
end

# 'try/catch' wird verwendet um Werte zu fangen, die zuvor 'geworfen' wurden.
# Das Konstrukt unterstützt außerdem eine 'after'-Klausel die aufgerufen wird,
# egal ob zuvor ein Wert gefangen wurde.
try do
  throw(:hello)
catch
  nachricht -> "#{nachricht} gefangen."
after
  IO.puts("Ich bin die 'after'-Klausel.")
end
#=> Ich bin die 'after'-Klausel.
# ":hello gefangen"

## ---------------------------
## -- Module und Funktionen
## ---------------------------

# Anonyme Funktionen (man beachte den Punkt)
square = fn(x) -> x * x end
square.(5) #=> 25

# Anonyme Funktionen unterstützen auch 'pattern' und 'guards'. Guards erlauben
# es die Mustererkennung zu justieren und werden mit dem Schlüsselwort 'when'
# eingeführt:
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir bietet zahlreiche eingebaute Funktionen. Diese sind im gleichen
# Geltungsbereich ('scope') verfügbar.
is_number(10)    #=> true
is_list("hello") #=> false
elem({1,2,3}, 0) #=> 1

# Mehrere Funktionen können in einem Modul gruppiert werden. Innerhalb eines
# Moduls ist es möglich mit dem Schlüsselwort 'def' eine Funktion zu
# definieren.
defmodule Math do
  def sum(a, b) do
    a + b
  end

  def square(x) do
    x * x
  end
end

Math.sum(1, 2)  #=> 3
Math.square(3) #=> 9

# Um unser einfaches Mathe-Modul zu kompilieren muss es unter 'math.ex'
# gesichert werden. Anschließend kann es mit 'elixirc' im Terminal aufgerufen
# werden: elixirc math.ex

# Innerhalb eines Moduls definieren wir private Funktionen mit 'defp'. Eine
# Funktion, die mit 'def' erstellt wurde, kann von anderen Modulen aufgerufen
# werden; eine private Funktion kann nur lokal angesprochen werden.
defmodule PrivateMath do
  def sum(a, b) do
    do_sum(a, b)
  end

  defp do_sum(a, b) do
    a + b
  end
end

PrivateMath.sum(1, 2)    #=> 3
# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)

# Auch Funktionsdeklarationen unterstützen 'guards' und Mustererkennung:
defmodule Geometry do
  def area({:rectangle, w, h}) do
    w * h
  end

  def area({:circle, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometry.area({:rectangle, 2, 3}) #=> 6
Geometry.area({:circle, 3})       #=> 28.25999999999999801048
# Geometry.area({:circle, "not_a_number"})
#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1

# Wegen der Unveränderlichkeit von Variablen ist Rekursion ein wichtiger
# Bestandteil von Elixir.
defmodule Recursion do
  def sum_list([head | tail], acc) do
    sum_list(tail, acc + head)
  end

  def sum_list([], acc) do
    acc
  end
end

Recursion.sum_list([1,2,3], 0) #=> 6

# Elixir-Module unterstützen Attribute. Es gibt eingebaute Attribute, ebenso
# ist es möglich eigene Attribute hinzuzufügen.
defmodule MyMod do
  @moduledoc """
  Dies ist ein eingebautes Attribut in einem Beispiel-Modul
  """

  @my_data 100 # Dies ist ein selbst-definiertes Attribut.
  IO.inspect(@my_data) #=> 100
end

## ---------------------------
## -- 'Records' und Ausnahmebehandlung
## ---------------------------

# 'Records' sind im Grunde Strukturen, die es erlauben einem Wert einen eigenen
# Namen zuzuweisen.
defrecord Person, name: nil, age: 0, height: 0

joe_info = Person.new(name: "Joe", age: 30, height: 180)
#=> Person[name: "Joe", age: 30, height: 180]

# Zugriff auf den Wert von 'name'
joe_info.name #=> "Joe"

# Den Wert von 'age' überschreiben
joe_info = joe_info.age(31) #=> Person[name: "Joe", age: 31, height: 180]

# Der 'try'-Block wird zusammen mit dem 'rescue'-Schlüsselwort dazu verwendet,
# um Ausnahmen beziehungsweise Fehler zu behandeln. 
try do
  raise "Irgendein Fehler."
rescue
  RuntimeError -> "Laufzeit-Fehler gefangen."
  _error -> "Und dies fängt jeden Fehler."
end

# Alle Ausnahmen haben das Attribut 'message'
try do
  raise "ein Fehler"
rescue
  x in [RuntimeError] ->
    x.message
end

## ---------------------------
## -- Nebenläufigkeit
## ---------------------------

# Elixir beruht auf dem Aktoren-Model zur Behandlung der Nebenläufigkeit. Alles
# was man braucht um in Elixir nebenläufige Programme zu schreiben sind drei
# Primitive: Prozesse erzeugen, Nachrichten senden und Nachrichten empfangen. 

# Um einen neuen Prozess zu erzeugen nutzen wir die 'spawn'-Funktion, die
# wiederum eine Funktion als Argument entgegen nimmt.
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>

# 'spawn' gibt eine pid (einen Identifikator des Prozesses) zurück. Diese kann
# nun verwendet werden, um Nachrichten an den Prozess zu senden. Um 
# zu senden nutzen wir den '<-' Operator. Damit das alles Sinn macht müssen wir
# in der Lage sein Nachrichten zu empfangen. Dies wird mit dem 
# 'receive'-Mechanismus sichergestellt:
defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Area = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        area_loop()
    end
  end
end

# Kompiliere das Modul, starte einen Prozess und gib die 'area_loop' Funktion
# in der Shell mit, etwa so:
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>

# Sende eine Nachricht an die 'pid', die ein Muster im 'receive'-Ausdruck
# erfüllt:
pid <- {:rectangle, 2, 3}
#=> Area = 6
#   {:rectangle,2,3}

pid <- {:circle, 2}
#=> Area = 12.56000000000000049738
#   {:circle,2}

# Die Shell selbst ist ein Prozess und mit dem Schlüsselwort 'self' kann man
# die aktuelle pid herausfinden.
self() #=> #PID<0.27.0>

```

## Referenzen und weitere Lektüre

* [Getting started guide](http://elixir-lang.org/getting_started/1.html) auf der [elixir Website](http://elixir-lang.org)
* [Elixir Documentation](http://elixir-lang.org/docs/master/)
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) von Fred Hebert
* "Programming Erlang: Software for a Concurrent World" von Joe Armstrong
---
category: tool
tool: git
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
    - ["kultprok", "http://www.kulturproktologie.de"]
lang: de-de
---

Git ist eine verteilte Versions- und Quellcodeverwaltung. 

Es nimmt Schnappschüsse der Projekte, um mit diesen Schnappschüssen verschiedene Versionen unterscheiden und den Quellcode verwalten zu können.

Anmerkung des Übersetzers: Einige englische Begriffe wie *Repository*, *Commit* oder *Head* sind idiomatische Bestandteile im Umgang mit Git. Sie wurden nicht übersetzt.

## Konzepte der Versionsverwaltung

### Was ist Versionsverwaltung?

Eine Versionsverwaltung erfasst die Änderungen einer Datei oder eines Verzeichnisses im Verlauf der Zeit.

### Zentrale im Vergleich mit verteilter Versionverwaltung

* Zentrale Versionsverwaltung konzentriert sich auf das Synchronisieren, Verfolgen und Sichern von Dateien.
* Verteilte Versionsverwaltung konzentriert sich auf das Teilen der Änderungen. Jede Änderung hat eine eindeutige ID.
* Verteilte Systeme haben keine vorbestimmte Struktur. Ein SVN-ähnliches, zentrales System wäre mit Git ebenso umsetzbar.

[Weiterführende Informationen](http://git-scm.com/book/en/Getting-Started-About-Version-Control)

### Warum Git?

* Ist offline einsetzbar.
* Einfache Kollaboration!
* Branching ist einfach!
* Branching ist schnell!
* Merging ist einfach!
* Git ist schnell.
* Git ist flexibel.

## Die Architektur von Git


### Repository (Repo)

Ein Satz von Dateien, Verzeichnisen, Historieneinträgen, Commits und Heads. Stell es dir wie eine Quellcode-Datenstruktur vor, unter anderem mit der Eigenschaft, dass alle *Elemente* dir Zugriff auf die Revisionshistorie geben.

Ein Repository besteht in Git aus dem .git-Verzeichnis und dem Arbeitsverzeichnis.

### .git-Verzeichnis (Teil des Repositorys)

Das .git-Verzeichnis enthält alle Einstellung, Logs, Branches, den HEAD und mehr.
[Ausführliche Übersicht](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### Arbeitsverzeichnis (Teil des Repositorys)

Dies sind die Verzeichnisse und Dateien in deinem Repository, also z.B. dein Programmcode.

### Index (Teil des .git-Verzeichnisses)

Der Index ist die Staging-Area von Git. Es ist im Grunde eine Ebene, die Arbeitsverzeichnis vom Repository trennt. Sie gibt Entwicklern mehr Einfluss darüber, was ins Git-Repository eingeht.

### Commit

Ein Commit ist ein Schnappschuss von Änderungen in deinem Arbeitsverzeichnis. Wenn du zum Beispiel 5 Dateien hinzugefügt und 2 andere entfernt hast, werden diese Änderungen im Commit (Schnappschuss) enthalten sein. Dieser Commit kann dann in andere Repositories gepusht werden. Oder nicht!

### Branch

Ein Branch, ein Ast oder Zweig, ist im Kern ein Pointer auf den letzten Commit, den du gemacht hast. Während des Commits wird der Pointer automatisch auf Stand gebracht und zeigt dann auf den neuen letzten Commit.

### HEAD und head (Teil des .git-Verzeichnisses)

HEAD ist ein Pointer auf den aktuellen Branch. Ein Repository hat nur einen *aktiven* HEAD. 

Ein *head* ist ein Pointer, der auf einen beliebigen Commit zeigt.  Ein Repository kann eine beliebige Zahl von *heads* enthalten.

### Konzeptionelle Hintergründe

* [Git For Computer Scientists](http://eagain.net/articles/git-for-computer-scientists/)
* [Git For Designers](http://hoth.entp.com/output/git_for_designers.html)


## Befehle


### init

Erstelle ein leeres Git-Repository im aktuellen Verzeichnis. Die Einstellungen, gespeicherte Informationen und mehr zu diesem Git-Repository werden in einem Verzeichnis namens *.git* angelegt.

```bash
$ git init
```

### config

Hiermit werden Einstellungen vorgenommen. Dies kann das Repository, das System selbst oder globale Einstellungen betreffen.

```bash
# Grundlegende Config-Variablen ausgeben und setzen
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "MyEmail@Zoho.com"
$ git config --global user.name "My Name"
```

[Mehr über git config](http://git-scm.com/docs/git-config)

### help

Schnellzugriff auf extrem detaillierte Anleitungen zu allen Befehlen. Oder als Erinnerung zu semantischen Eigenheiten.

```bash
# Übersicht gängiger Befehle
$ git help

# Übersicht aller verfügbaren Befehle
$ git help -a

# Befehlspezifische Hilfe - Bedienungsanleitung
# git help <gesuchter_befehl>
$ git help add
$ git help commit
$ git help init
```

### status

Zeigt die Unterschiede zwischen Index (im Grunde dein Arbeitsverzeichnis/-repository) und dem aktuellen HEAD an.


```bash
# Zeigt den Branch, nicht-verfolgte Dateien, Änderungen und andere Unterschiede an
$ git status

# Anderes Wissenswertes über git status anzeigen
$ git help status
```

### add

Hinzufügen von Dateien zum Arbeitsverzeichnis/-repository. Wenn du neue Dateien nicht mit *git add* zum Arbeitsverzeichnis hinzufügst, werden sie nicht in den Commit aufgenommen!

```bash
# Fügt eine Datei deinem aktuellen Arbeitsverzeichnis hinzu
$ git add HelloWorld.java

# Fügt eine Datei aus einem verschachtelten Verzeichnis hinzu
$ git add /path/to/file/HelloWorld.c

# Reguläre Ausdrücke werden unterstützt!
$ git add ./*.java
```

### branch

Verwalte alle Branches. Du kannst sie mit diesem Befehl ansehen, bearbeiten, neue erzeugen oder löschen.

```bash
# Liste alle bestehenden Branches und Remotes auf
$ git branch -a

# Erstelle einen neuen Branch
$ git branch myNewBranch

# Lösche einen Branch
$ git branch -d myBranch

# Benenne einen Branch um
# git branch -m <oldname> <newname>
$ git branch -m myBranchName myNewBranchName

# Ändere die Beschreibung eines Branchs
$ git branch myBranchName --edit-description
```

### checkout

Bringt alle Dateien im Arbeitsverzeichnis auf den Stand des Index oder des angegebenen Branches.

```bash
# Ein Repo auschecken - wenn nicht anders angegeben ist das der master
$ git checkout
# Eine Datei auschecken - sie befindet sich dann auf dem aktuellen Stand im Repository
$ git checkout /path/to/file
# Einen bestimmten Branch auschecken
$ git checkout branchName
# Erstelle einen neuen Branch und wechsle zu ihm. Wie: "git branch <name>; git checkout <name>"
$ git checkout -b newBranch
```

### clone

Ein bestehendes Repository in ein neues Verzeichnis klonen oder kopieren. Es fügt außerdem für jedes geklonte Repository remote-tracking Branches hinzu. Du kannst auf diese Remote-Branches pushen.

```bash
# Klone learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git
```

### commit

Speichert die aktuellen Inhalte des Index in einen neuen *Commit*. Dieser Commit enthält alle Änderungen und eine vom Benutzer erstellte Beschreibung der Änderungen.

```bash
# Commit mit Beschreibung erstellen.
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"
```

### diff

Zeigt die Unterschiede zwischen Dateien von Arbeitsverzeichnisse, dem Index und Commits an.

```bash
# Unterschiede zwischen deinem Arbeitsverzeichnis und dem Index anzeigen
$ git diff

# Unterschiede zwischen dem Index und dem aktuellsten Commit anzeigen
$ git diff --cached

# Unterschiede zwischen deinem Arbeitsverzeichnis und dem aktuellsten Commit anzeigen
$ git diff HEAD

# Unterschiede zwischen dem Index und dem aktuellsten Commit (betrifft nur Dateien im Index)
$ git diff --staged
```

### grep

Schnell ein Repository durchsuchen.

Optionale Einstellungen:

```bash
# Vielen Dank an Travis Jeffery für die Hinweise.
# Zeilennummerierung in grep-Suchergebnissen
$ git config --global grep.lineNumber true

# Suchergebnisse lesbarer gestalten, auch Gruppierungen sind möglich
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Suche nach "variableName" in allen java-Dateien
$ git grep 'variableName' -- '*.java'

# Suche nach eine Zeile, die "arrayListName" und  "add" oder "remove" enthält
$ git grep -e 'arrayListName' --and \( -e add -e remove \) 
```

Google ist dein Freund; für mehr Beispiele:
[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Zeige Commits für das Repository an.

```bash
# Zeige alle Commits
$ git log

# Zeige die Anzahl n an Commits
$ git log -n 10

# Zeige nur Merges an
$ git log --merges
```

### merge

*Merge*, also verschmelze, alle Änderungen von externen Commits in den aktuellen Branch.

```bash
# Merge den angegebenen Branch in den aktuellen.
$ git merge branchName

# Erstelle immer einen Merge-Commit.
$ git merge --no-ff branchName
```

### mv

Eine Datei umbenennen oder verschieben.	

```bash
# Umbenennen
$ git mv HelloWorld.c HelloNewWorld.c

# Verschieben
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# Umbenennung oder Verschieben erzwingen
# "existingFile" besteht schon im Verzeichnis, wird überschrieben mit "myFile"
$ git mv -f myFile existingFile
```

### pull

Führe einen Pull (zieht alle Daten eines Repositories) aus und führt einen Merge mit einem anderen Branch durch.

```bash
# Update deines lokalen Repos, indem ein Merge der neuen Änderungen
# von den remote-liegenden "origin"- und "master"-Branches durchgeführt wird.
# git pull <remote> <branch>
# git pull => impliziter Verweis auf origin und master
$ git pull origin master

# Führt einen Merge von Änderungen eines remote-Branch und ein Rebase
# des Branch-Commits im lokalen Repo durch. Wie: pull <remote> <branch>, git rebase <branch>"
$ git pull origin master --rebase
```

### push

Führe einen Push, ein Hochladen, und einen Merge von Änderungen eines remote-Branch mit einem Branch aus.

```bash
# Führe Push und Merge von Änderungen des lokalen Repo zu einem
# remote-Branch namens "origin" und dem "master"-Branch aus.
# git push <remote> <branch>
# git push => impliziter Verweis auf => git push origin master
$ git push origin master
```

### rebase (mit Vorsicht einsetzen) 

Nimm alle Änderungen, die in einem Branch durch Commits vorgenommen wurden, und übertrage sie auf einen anderen Branch. Achtung: Führe keinen Rebase von Commits durch, die auf ein öffentliches Repo gepusht wurden.

```bash
# Rebase "experimentBranch" in den "master"-Branch
# git rebase <basisbranch> <themenbranch>
$ git rebase master experimentBranch
```

[Weiterführende Informationen](http://git-scm.com/book/en/Git-Branching-Rebasing)

### reset (mit Vorsicht einsetzen)

Setze den aktuellen HEAD auf den angegebenen Zustand zurück. So können Merges, Pulls, Commits, Hinzufügungen und andere Änderungen rückgängig gemacht werden. Es ist ein hervorragender Befehl, aber auch sehr gefährlich, wenn du nicht weißt, was du tust.

```bash
# Setze die Staging-Area zurück, um dem letzten Commit zu entsprechen (das Verzeichnis bleibt unberührt)
$ git reset

# Setze die Staging-Area zurück, um dem letzten Commit zu entsprechen und überschreibe das Arbeitsverzeichnis
$ git reset --hard

# Bewegt die Spitze des Branches zu dem angegebenen Commit (das Verzeichnis bleibt unberührt)
# Alle Änderungen bleiben im Verzeichnis erhalten
$ git reset 31f2bb1

# Bewegt die Spitze des Branches zurück zu dem angegebenen Commit
# und gleicht die Arbeitsverzeichnisse ab (löscht nicht vom Commit erfasste Änderungen und alle Commits,
# die dem angegebenen Commit folgen).
$ git reset --hard 31f2bb1
```

### rm

Das Gegenteil von *git add*. *git rm* löscht Dateien vom Arbeitsverzeichnis.

```bash
# Entferne HelloWorld.c
$ git rm HelloWorld.c

# Entferne eine Datei aus einem verschachtelten Verzeichnis
$ git rm /pather/to/the/file/HelloWorld.c
```

## Weiterführende Informationen

* [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1)

* [git-scm - Video Tutorials](http://git-scm.com/videos)

* [git-scm - Documentation](http://git-scm.com/docs)

* [Atlassian Git - Tutorials & Workflows](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)

* [gitflow - Ein Modell um mit Branches zu arbeiten](http://nvie.com/posts/a-successful-git-branching-model/)
---
language: Go
filename: learngo-de.go
contributors:
    - ["Joseph Adams", "https://github.com/jcla1"]
    - ["Dennis Keller", "https://github.com/denniskeller"]
translators:
    - ["Jerome Meinke", "https://github.com/jmeinke"]
lang: de-de
---
Die Sprache Go (auch golang) wurde von Google entwickelt und wird seit 2007
benutzt. Go ähnelt in der Syntax der Sprache C, bietet darüber hinaus aber viele
Vorteile. Einerseits verzichtet Go auf Speicherarithmetik und
benutzt einen Garbage Collector. Andererseits enthält Go native Sprachelemente
für die Unterstützung von Nebenläufigkeit. Durch den Fokus auf einen schnellen
Kompilierprozess wird außerdem die Softwareentwicklung in Großprojekten
erleichtert.

Außerdem beinhaltet Go eine gut ausgestattete Standardbibliothek und hat eine
aktive Community.

```go
// Einzeiliger Kommentar
/* Mehr-
   zeiliger Kommentar */

// Wie bei Java gehört jede Quelldatei einem Paket an (Modularisierung).
// "main" ist ein besonderer Paketname, da er ein ausführbares Programm
// einleitet, im Gegensatz zu jedem anderen Namen, der eine Bibliothek
// deklariert.
package main

// Ein "import" wird verwendet, um Pakete zu deklarieren, die in dieser
// Quelldatei Anwendung finden.
import (
    "fmt"      // Ein Paket in der Go Standardbibliothek
    "net/http" // Ja, ein Webserver.
    "strconv"  // Zeichenkettenmanipulation
)

// Es folgt die Definition einer Funktion, in diesem Fall von "main". Auch hier
// ist der Name wieder besonders. "main" markiert den Eintrittspunkt des
// Programms.
func main() {
    // Println gibt eine Zeile zu stdout aus.
    // Der Prefix "fmt" bestimmt das Paket aus welchem die Funktion stammt.
    fmt.Println("Hello world!")

    // Aufruf einer weiteren Funktion definiert innerhalb dieses Pakets.
    beyondHello()
}

// Funktionen können Parameter akzeptieren. Diese werden in Klammern deklariert,
// die aber auch ohne Parameter erforderlich sind.
func beyondHello() {
    var x int // Deklaration einer Variable, muss vor Gebrauch geschehen.
    x = 3     // Zuweisung eines Werts.
    // Kurze Deklaration: Benutzen Sie ":=", um die Typisierung automatisch zu
    // folgern, die Variable zu deklarieren und ihr einen Wert zuzuweisen.
    y := 4

    // Eine Funktion mit mehreren Rückgabewerten.
    sum, prod := learnMultiple(x, y)

    fmt.Println("sum:", sum, "prod:", prod) // Simple Ausgabe
    learnTypes()                            // In < y Minuten lernen Sie mehr!
}

// Funktionen können mehrere Parameter und (mehrere!) Rückgabewerte haben.
func learnMultiple(x, y int) (sum, prod int) {
    return x + y, x * y // Wiedergabe zweier Werte
}

// Überblick über einige eingebaute Typen und Literale.
func learnTypes() {
    // Kurze Deklarationen sind die Norm.
    s := "Lernen Sie Go!" // Zeichenketten-Typ

    s2 := `Eine "raw" Zeichenkette kann
Zeilenumbrüche beinhalten.` // Selber Zeichenketten-Typ

    // nicht-ASCII Literal.  Go Quelltext ist UTF-8 kompatibel.
    g := 'Σ' // Ein Runen-Typ, alias int32, gebraucht für unicode code points.

    f := 3.14195 // float64, eine IEEE-754 64-bit Dezimalzahl
    c := 3 + 4i  // complex128, besteht intern aus zwei float64-er

    // "var"-Syntax mit Initalwert
    var u uint = 7 // Vorzeichenlos, aber die Größe ist implementationsabhängig
    var pi float32 = 22. / 7

    // Umwandlungs-Syntax mit kurzer Deklaration
    n := byte('\n') // byte ist ein Alias für uint8

    // Arrays haben bei Kompile-Zeit festgelegte Größen
    var a4 [4]int           // Ein Array mit 4 ints, alle mit Initialwert 0
    a3 := [...]int{3, 1, 5} // Ein Array mit 4 ints, Initialwerte wie angezeigt

    // "slices" haben eine dynamische Größe. Arrays und Slices haben beide ihre
    // Vorzüge, aber slices werden viel häufiger verwendet
    s3 := []int{4, 5, 9}       // Vergleichen Sie mit a3, hier: keine Ellipse
    s4 := make([]int, 4)       // Weist Speicher für 4 ints zu, alle mit Wert 0
    var d2 [][]float64         // Nur eine Deklaration, keine Speicherzuweisung
    bs := []byte("eine slice") // Umwandlungs-Syntax

    p, q := learnMemory() // Deklariert p & q als Zeiger zu einer int.
    fmt.Println(*p, *q)   // Die gibt die zwei Werte aus. "*" für den Zugriff

    // "Maps" sind dynamische Datenstrukturen mit variabler Größe. Sie sind wie
    // "hashs" oder "dictionaries" aus anderen Sprachen.
    m := map[string]int{"drei": 3, "vier": 4}
    m["eins"] = 1

    // Ungebrauchte Variablen sind Fehler in Go
    // Der Unterstrich wird verwendet, um einen Wert zu verwerfen.
    _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
    // Die Ausgabe zählt natürlich auch als Gebrauch
    fmt.Println(s, c, a4, s3, d2, m)

    learnFlowControl() // Auf zum Kontrollfluss!
}

// Go ist komplett "garbage collected". Sie unterstützt Zeiger (pointers) aber
// keine Zeiger-Rechnungen. Fehler können sich durch "nil" einschleichen, jedoch
// nicht durch erhöhen eines Zeigers.
func learnMemory() (p, q *int) {
    // Die bennanten Rückgabewerte p & q sind vom Typ *int
    p = new(int) // Eingebaute Funktion "new" weist neuen Speicherplatz zu
    // Der zugewiesene Speicher ist mit 0 initialisiert, p ist nicht länger nil
    s := make([]int, 20) // So weist man 20 ints nebeneinander (im Speicher) zu
    s[3] = 7             // Einer von ihnen wird ein Wert zugewiesen
    r := -2              // Deklaration einer weiteren lokalen Variable
    return &s[3], &r     // & gibt die Addresse einer Variable
}

func expensiveComputation() int {
    return 1e6
}

func learnFlowControl() {
    // Bedingte Anweisungen verlangen nach geschweiften Klammern, normale
    // Klammern um die Bedingung werden aber nicht gebraucht.
    if true {
        fmt.Println("hab's dir ja gesagt!")
    }
    // Die Formatierung ist durch den Befehl "go fmt" standardisiert
    if false {
        // nicht hier
    } else {
        // sondern hier! spielt die Musik
    }

    // Benutzen Sie ein "switch" Statement anstatt eine Anreihung von if-s
    x := 1
    switch x {
    case 0:
    case 1:
        // Einzelne Fälle fallen nicht zum nächsten durch!
    case 2:
        // wird nicht ausgeführt
    }
    // Wie bei "if", braucht "for" auch keine Klammern um die Bedingung
    for x := 0; x < 3; x++ { // ++ ist ein Statement
        fmt.Println(x, "-te Iteration")
    }
    // Ab hier gilt wieder: x == 1

    // For ist die einzige Schleifenform in Go, sie hat aber mehrere Formen:
    for { // Endlosschleife
        break    // nur ein Spaß
        continue // wird nie ausgeführt
    }

    // Wie bei for, bedeutet := in einer bedingten Anweisung zunächst die
    // Zuweisung und erst dann die Überprüfung der Bedingung.
    if y := expensiveComputation(); y > x {
        x = y
    }
    // Funktionsliterale sind "closures"
    xBig := func() bool {
        return x > 100 // Verweist auf x, deklariert vor dem switch
    }
    fmt.Println("xBig:", xBig()) // true (im moment gilt: x == 1e6)
    x /= 1e5                     // dies macht x == 10
    fmt.Println("xBig:", xBig()) // jetzt: false

    // Wenn Sie's brauchen, werden Sie's lieben!
    goto love
love:

    learnInterfaces() // Jetzt zum interessanten Teil!
}

// Definiere "Stringer" als ein Interface mit einer Methode: String
type Stringer interface {
    String() string
}

// Definiere ein Paar als struct mit zwei Feldern, Integers mit Namen x & y.
type pair struct {
    x, y int
}

// Definiere eine Methode von "pair".
// Dieser Typ erfüllt jetzt das Stringer interface.
func (p pair) String() string { // p ist der Empfänger
    // Sprintf ist eine weitere öffentliche Funktion von fmt.
    // Der Syntax mit Punkt greift auf die Felder zu.
    return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
    // Der Klammer-Syntax ist ein "struct literal". Es ist ein vollkommen
    // initialisiertes struct. Der := Syntax deklariert und initialisiert p.
    p := pair{3, 4}
    fmt.Println(p.String()) // Aufruf der String() Methode von p.
    var i Stringer          // Deklariere i vom Typ: Stringer
    i = p                   // Ok, weil p auch vom Typ Stringer ist.
    // Aufruf der String Methode von i, gleiche Ausgabe wie zuvor.
    fmt.Println(i.String())

    // Funktionen des fmt-Pakets rufen die String() Methode auf um eine
    // druckbare Variante des Empfängers zu erhalten.
    fmt.Println(p) // gleiche Ausgabe wie zuvor
    fmt.Println(i) // und wieder die gleiche Ausgabe wie zuvor

    learnErrorHandling()
}

func learnErrorHandling() {
    // Das ", ok" Idiom wird häufig verwendet um zu überprüfen ob etwas schief
    // gegangen ist.
    m := map[int]string{3: "drei", 4: "vier"}
    if x, ok := m[1]; !ok { // ok wird false sein, da 1 nicht in der map ist.
        fmt.Println("keine eins gefunden")
    } else {
        fmt.Print(x) // x wäre der Wert, wenn er in der map wäre.
    }
    // Ein Fehler-Wert (error value) gibt mehr Informationen über den Grund für
    // das Problem an.
    if _, err := strconv.Atoi("nicht-int"); err != nil { // _ verwirft den Wert
        // Gibt: "strconv.ParseInt: parsing "nicht-int": invalid syntax" aus
        fmt.Println(err)
    }
    // Wir kommen bald nochmal auf Interfaces zurück. Aber inzwischen:
    learnConcurrency()
}

// c ist ein Kanal, ein sicheres Kommunikationsmedium.
func inc(i int, c chan int) {
    c <- i + 1 // <- ist der "send" Operator, wenn ein Kanal auf der Linken ist
}

// Wir verwenden "inc" um Zahlen parallel zu erhöhen.
func learnConcurrency() {
    // Die selbe "make"-Funktion wie vorhin. Sie initialisiert Speicher für
    // maps, slices und Kanäle.
    c := make(chan int)
    // Starte drei parallele "Goroutines".
    // Die Zahlen werden parallel (concurrently) erhöht.
    // Alle drei senden ihr Ergebnis in den gleichen Kanal.
    go inc(0, c) // "go" ist das Statement zum Start einer neuen Goroutine
    go inc(10, c)
    go inc(-805, c)
    // Auslesen und dann Ausgeben der drei berechneten Werte.
    // Man kann nicht im voraus feststellen in welcher Reihenfolge die Werte
    // ankommen.
    fmt.Println(<-c, <-c, <-c) // mit dem Kanal rechts ist <- der Empfangs-Operator

    cs := make(chan string)       // ein weiterer Kanal, diesmal für strings
    cc := make(chan chan string)  // ein Kanal für string Kanäle

    // Start einer neuen Goroutine, nur um einen Wert zu senden
    go func() { c <- 84 }()
    go func() { cs <- "wortreich" }() // schon wieder, diesmal für
    // "select" hat eine Syntax wie ein switch Statement, aber jeder Fall ist
    // eine Kanaloperation. Es wählt einen Fall zufällig aus allen, die
    // kommunikationsbereit sind, aus.
    select {
    case i := <-c: // der empfangene Wert kann einer Variable zugewiesen werden
        fmt.Printf("es ist ein: %T", i)
    case <-cs: // oder der Wert kann verworfen werden
        fmt.Println("es ist eine Zeichenkette!")
    case <-cc: // leerer Kanal, nicht bereit für den Empfang
        fmt.Println("wird nicht passieren.")
    }
    // Hier wird eine der beiden Goroutines fertig sein, die andere nicht.
    // Sie wird warten bis der Wert den sie sendet von dem Kanal gelesen wird.

    learnWebProgramming() // Go kann es und Sie hoffentlich auch bald.
}

// Eine einzige Funktion aus dem http-Paket kann einen Webserver starten.
func learnWebProgramming() {
    // Der erste Parameter von "ListenAndServe" ist eine TCP Addresse, an die
    // sich angeschlossen werden soll.
    // Der zweite Parameter ist ein Interface, speziell: ein http.Handler
    err := http.ListenAndServe(":8080", pair{})
    fmt.Println(err) // Fehler sollte man nicht ignorieren!
}

// Wir lassen "pair" das http.Handler Interface erfüllen, indem wir seine einzige
// Methode implementieren: ServeHTTP
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // Senden von Daten mit einer Methode des http.ResponseWriter
    w.Write([]byte("Sie haben Go in Y Minuten gelernt!"))
}
```

## Weitere Resourcen
Informationen zu Go findet man auf der [offiziellen Go Webseite](http://golang.org/).
Dort gibt es unter anderem ein Tutorial und interaktive Quelltext-Beispiele, vor
allem aber Dokumentation zur Sprache und den Paketen.

Auch zu empfehlen ist die Spezifikation von Go, die nach heutigen Standards sehr
kurz und gut verständlich formuliert ist. Auf der Leseliste von Go-Neulingen
ist außerdem der Quelltext der [Go standard Bibliothek](http://golang.org/src/pkg/)
einzusehen. Dieser kann als Referenz für leicht zu verstehendes und im idiomatischen Stil
verfasstes Go dienen. Erreichbar ist der Quelltext auch durch das Klicken der Funktionsnamen
in der [offiziellen Dokumentation von Go](http://golang.org/pkg/).
---
language: Hack
lang: de-de
contributors:
    - ["Stephen Holdaway", "https://github.com/stecman"]
    - ["David Lima", "https://github.com/davelima"]
translators:
    - ["Jerome Meinke", "https://github.com/jmeinke"]
filename: learnhack-de.hh
---

Hack ist eine von Facebook neu entwickelte Programmiersprache auf Basis von PHP.
Sie wird von der HipHop Virtual Machine (HHVM) ausgeführt. Die HHVM kann
aufgrund der Ähnlichkeit der Programmiersprachen nicht nur Hack, sondern auch
PHP-Code ausführen. Der wesentliche Unterschied zu PHP besteht in der statischen
Typisierung der Sprache, die eine wesentlich höhere Performance erlaubt.


Hier werden nur Hack-spezifische Eigenschaften beschrieben. Details über PHP's
Syntax findet man im [PHP Artikel](http://learnxinyminutes.com/docs/php/) dieser
Seite.

```php
<?hh

// Hack-Syntax ist nur für Dateien aktiv, die mit dem <?hh Prefix starten.
// Der <?hh Prefix kann nicht wie <?php mit HTML gemischt werden.
// Benutzung von "<?hh //strict" aktiviert den Strikt-Modus des Type-Checkers.


// Typisierung für Funktions-Argumente
function repeat(string $word, int $count)
{
    $word = trim($word);
    return str_repeat($word . ' ', $count);
}

// Typisierung für Rückgabewerte
function add(...$numbers) : int
{
    return array_sum($numbers);
}

// Funktionen ohne Rückgabewert, werden mit "void" typisiert
function truncate(resource $handle) : void
{
    // ...
}

// Typisierung unterstützt die explizit optionale Ein- / Ausgabe von "null"
function identity(?string $stringOrNull) : ?string
{
    return $stringOrNull;
}

// Typisierung von Klassen-Eigenschaften
class TypeHintedProperties
{
    public ?string $name;

    protected int $id;

    private float $score = 100.0;

    // Hack erfordert es, dass typisierte Eigenschaften (also "non-null")
    // einen Default-Wert haben oder im Konstruktor initialisiert werden.
    public function __construct(int $id)
    {
        $this->id = $id;
    }
}


// Kurzgefasste anonyme Funktionen (lambdas)
$multiplier = 5;
array_map($y ==> $y * $multiplier, [1, 2, 3]);


// Weitere, spezielle Felder (Generics)
// Diese kann man sich als ein zugreifbares Interface vorstellen
class Box<T>
{
    protected T $data;

    public function __construct(T $data) {
        $this->data = $data;
    }

    public function getData(): T {
        return $this->data;
    }
}

function openBox(Box<int> $box) : int
{
    return $box->getData();
}


// Formen
//
// Hack fügt das Konzept von Formen hinzu, wie struct-ähnliche arrays
// mit einer typ-geprüften Menge von Schlüsseln
type Point2D = shape('x' => int, 'y' => int);

function distance(Point2D $a, Point2D $b) : float
{
    return sqrt(pow($b['x'] - $a['x'], 2) + pow($b['y'] - $a['y'], 2));
}

distance(
    shape('x' => -1, 'y' => 5),
    shape('x' => 2, 'y' => 50)
);


// Typen-Definition bzw. Aliasing
//
// Hack erlaubt es Typen zu definieren und sorgt somit für bessere Lesbarkeit
newtype VectorArray = array<int, Vector<int>>;

// Ein Tupel mit zwei Integern
newtype Point = (int, int);

function addPoints(Point $p1, Point $p2) : Point
{
    return tuple($p1[0] + $p2[0], $p1[1] + $p2[1]);
}

addPoints(
    tuple(1, 2),
    tuple(5, 6)
);


// Erstklassige Aufzählungen (enums)
enum RoadType : int
{
    Road = 0;
    Street = 1;
    Avenue = 2;
    Boulevard = 3;
}

function getRoadType() : RoadType
{
    return RoadType::Avenue;
}


// Automatische Erstellung von Klassen-Eigenschaften durch Konstruktor-Argumente
//
// Wiederkehrende Definitionen von Klassen-Eigenschaften können durch die Hack-
// Syntax vermieden werden. Hack erlaubt es die Klassen-Eigenschaften über
// Argumente des Konstruktors zu definieren.
class ArgumentPromotion
{
    public function __construct(public string $name,
                                protected int $age,
                                private bool $isAwesome) {}
}

class WithoutArgumentPromotion
{
    public string $name;

    protected int $age;

    private bool $isAwesome;

    public function __construct(string $name, int $age, bool $isAwesome)
    {
        $this->name = $name;
        $this->age = $age;
        $this->isAwesome = $isAwesome;
    }
}


// Kooperatives Multitasking
//
// Die Schlüsselworte "async" and "await" führen Multitasking ein.
// Achtung, hier werden keine Threads benutzt, sondern nur Aktivität getauscht.
async function cooperativePrint(int $start, int $end) : Awaitable<void>
{
    for ($i = $start; $i <= $end; $i++) {
        echo "$i ";

        // Geben anderen Tasks die Möglichkeit aktiv zu werden
        await RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0);
    }
}

// Die Ausgabe von folgendem Code ist "1 4 7 2 5 8 3 6 9"
AwaitAllWaitHandle::fromArray([
    cooperativePrint(1, 3),
    cooperativePrint(4, 6),
    cooperativePrint(7, 9)
])->getWaitHandle()->join();


// Attribute
//
// Attribute repräsentieren eine Form von Metadaten für Funktionen.
// Hack bietet Spezial-Attribute, die nützliche Eigenschaften mit sich bringen.

// Das __Memoize Attribut erlaubt es die Ausgabe einer Funktion zu cachen.
<<__Memoize>>
function doExpensiveTask() : ?string
{
    return file_get_contents('http://example.com');
}

// Der Funktionsrumpf wird im Folgenden nur ein einziges mal ausgeführt:
doExpensiveTask();
doExpensiveTask();


// Das __ConsistentConstruct Attribut signalisiert dem type-checker, dass
// die Funktionsdeklaration von __construct für alle Unterklassen dieselbe ist.
<<__ConsistentConstruct>>
class ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
        // ...
    }

    public function someMethod()
    {
        // ...
    }
}

class ConsistentBar extends ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
        // Der Type-checker erzwingt den Aufruf des Eltern-Klassen-Konstruktors
        parent::__construct($x, $y);

        // ...
    }

    // Das __Override Attribut ist ein optionales Signal an den Type-Checker,
    // das erzwingt, dass die annotierte Methode die Methode der Eltern-Klasse
    // oder des Traits verändert.
    <<__Override>>
    public function someMethod()
    {
        // ...
    }
}

class InvalidFooSubclass extends ConsistentFoo
{
    // Wenn der Konstruktor der Eltern-Klasse nicht übernommen wird,
    // wird der Type-Checker einen Fehler ausgeben:
    //
    //  "This object is of type ConsistentBaz. It is incompatible with this object
    //   of type ConsistentFoo because some of their methods are incompatible"
    //
    public function __construct(float $x)
    {
        // ...
    }

    // Auch bei der Benutzung des __Override Attributs für eine nicht veränderte
    // Methode wird vom Type-Checker eine Fehler ausgegeben:
    //
    //  "InvalidFooSubclass::otherMethod() is marked as override; no non-private
    //   parent definition found or overridden parent is defined in non-<?hh code"
    //
    <<__Override>>
    public function otherMethod()
    {
        // ...
    }
}

// Ein Trait ist ein Begriff aus der objektorientierten Programmierung und
// beschreibt eine wiederverwendbare Sammlung von Methoden und Attributen,
// ähnlich einer Klasse.

// Anders als in PHP können Traits auch als Schnittstellen (Interfaces)
// implementiert werden und selbst Schnittstellen implementieren.
interface KittenInterface
{
    public function play() : void;
}

trait CatTrait implements KittenInterface
{
    public function play() : void
    {
        // ...
    }
}

class Samuel
{
    use CatTrait;
}


$cat = new Samuel();
$cat instanceof KittenInterface === true; // True

```

## Weitere Informationen

Die Hack [Programmiersprachen-Referenz](http://docs.hhvm.com/manual/de/hacklangref.php)
erklärt die neuen Eigenschaften der Sprache detailliert auf Englisch. Für
allgemeine Informationen kann man auch die offizielle Webseite [hacklang.org](http://hacklang.org/)
besuchen.

Die offizielle Webseite [hhvm.com](http://hhvm.com/) bietet Infos zum Download
und zur Installation der HHVM.

Hack's [nicht-untersützte PHP Syntax-Elemente](http://docs.hhvm.com/manual/en/hack.unsupported.php)
werden im offiziellen Handbuch beschrieben.
---
language: haml
filename: learnhaml-de.haml
contributors:
  - ["Simon Neveu", "https://github.com/sneveu"]
  - ["Sol Bekic", "https://github.com/S0lll0s"]
lang: de-de
---

Haml ist eine Markup- und Templatingsprache, aufgesetzt auf Ruby, mit der HTML Dokumente einfach beschrieben werden können.

Haml vermindert Wiederholung und Fehleranfälligkeit, indem es Tags basierend auf der Markup-Struktur schließt und schachtelt.
Dadurch ergibt sich kurzes, präzises und logisches Markup.

Haml kann außerhalb eines Ruby-projekts verwendet werden. Mit dem installierten Haml gem kann man das Terminal benutzen um Haml zu HTML umzuwandeln:

$ haml input_file.haml output_file.html


```haml
/ -------------------------------------------
/ Einrückung
/ -------------------------------------------

/
  Einrückung ist ein wichtiges Element des Haml Syntax, deswegen ist es
  wichtig ein konsequentes Schema zu verwenden. Meistens werden zwei spaces
  verwendet, solange die Einrückungen das gleiche Schema verfolgen können
  aber auch andere Breiten und Tabs verwendet werden


/ -------------------------------------------
/ Kommentare
/ -------------------------------------------

/ Kommentare beginnen mit einem Slash

/
  Mehrzeilige Kommentare werden eingerückt und mit einem Slash
  eingeführt

-# Diese Zeile ist ein "stummes" Kommentar, es wird nicht mitgerendert


/ -------------------------------------------
/ HTML Elemente
/ -------------------------------------------

/ Tags werden durch ein Prozentzeichen und den Tagnamen erzeugt
%body
  %header
    %nav

/ Die Zeilen oben würden folgendes ergeben:
  <body>
    <header>
      <nav></nav>
    </header>
  </body>

/ Text kann direkt nach dem Tagnamen eingefügt werden:
%h1 Headline copy

/ Mehrzeilige Inhalte müssen stattdessen eingerückt werden:
%p 
  This is a lot of content that we could probably split onto two
  separate lines.

/ 
  HTML kann mit &= escaped werden. So werden HTML-sensitive Zeichen
  enkodiert. Zum Beispiel:

%p
  &= "Ja & Nein"

/ würde 'Ja &amp; Nein' ergeben

/ HTML kann mit != dekodiert werden:
%p
  != "so schreibt man ein Paragraph-Tag: <p></p>"

/ ...was 'This is how you write a paragraph tag <p></p>' ergeben würde

/ CSS Klassen können mit '.classname' an Tags angehängt werden:
%div.foo.bar

/ oder über einen Ruby Hash:
%div{:class => 'foo bar'}

/ Das div Tag wird standardmäßig verwendet, divs können also verkürzt werden:
.foo

/ andere Attribute können über den Hash angegeben werden:
%a{:href => '#', :class => 'bar', :title => 'Bar'}

/ Booleesche Attribute können mit 'true' gesetzt werden:
%input{:selected => true}

/ data-Attribute können in einem eigenen Hash im :data key angegeben werden:
%div{:data => {:attribute => 'foo'}}


/ -------------------------------------------
/ Verwendung von Ruby
/ -------------------------------------------

/ Mit dem = Zeichen können Ruby-werte evaluiert und als Tag-text verwendet werden:

%h1= book.name

%p
  = book.author
  = book.publisher


/ Code nach einem Bindestrich wird ausgeführt aber nicht gerendert:
- books = ['book 1', 'book 2', 'book 3']

/ So können zum Beispiel auch Blöcke verwendet werden:
- books.shuffle.each_with_index do |book, index|
  %h1= book

  if book do
    %p This is a book

/
  Auch hier werden wieder keine End-Tags benötigt!
  Diese ergeben sich aus der Einrückung.


/ -------------------------------------------
/ Inline Ruby / Ruby Interpolation
/ -------------------------------------------

/ Ruby variablen können mit #{} in Text interpoliert werden:
%p dein bestes Spiel ist #{best_game}


/ -------------------------------------------
/ Filter
/ -------------------------------------------

/
  Mit dem Doppelpinkt können Haml Filter benutzt werden.
  Zum Beispiel gibt es den :javascript Filter, mit dem inline JS
  geschrieben werden kann:

:javascript
  console.log('Dies ist ein <script>');

```

## Weitere Resourcen

- [What is HAML?](http://haml.info/) - Eine gute Einleitung auf der Haml homepage (englisch)
- [Official Docs](http://haml.info/docs/yardoc/file.REFERENCE.html) - Die offizielle Haml Referenz (englisch)
---
language: Haskell
lang: de-de
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["Henrik Jürges", "https://github.com/santifa"]
    - ["Nikolai Weh", "http://weh.hamburg"]
filename: haskell-de.hs

---

Haskell wurde als praktische und funktionale Sprache entworfen.
Es ist berühmt für das Schema der Monaden und des Typsystems, aber
es sticht vor allem die Einfachheit und Eleganz hervor.

```haskell
-- Einfache Kommentare beginnen mit 2 Bindestriche.
{- So wird ein Kommentar
über mehrere Zeilen angelegt.
-}

----------------------------------------------------
-- 1. Primitive Datentypen und Operatoren
----------------------------------------------------

-- Normale Zahlen.
3 -- 3

-- Einfache Rechenoperationen.
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- Die Division ist per se auf Fließkommazahlen.
35 / 4 -- 8.75

-- Ganzzahlige Division
35 `div` 4 -- 8

-- Boolesche Werte sind Primitiven.
True
False

-- Logik Operationen
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- `not` ist eine Funktion die ein Argument entgegenimmt.
-- Haskell benötigt keine Klammern um Argumente.
-- Sie werden einfach aufgelistet: func arg1 arg2 arg3...
-- Wie man Funktionen definiert kommt weiter unten.


-- Strings und Zeichen
"Das ist ein String."
'a' -- Zeichen
'Einfache Anführungszeichen gehen nicht.' -- error!

-- Strings können konkateniert werden.
"Hello " ++ "world!" -- "Hello world!"

-- Ein String ist eine Liste von Zeichen.
['H', 'a', 'l', 'l', 'o', '!'] -- "Hallo!"
"Das ist eine String" !! 0 -- 'D'


----------------------------------------------------
-- Listen und Tupel
----------------------------------------------------

-- Jedes Element einer Liste muss vom gleichen Typ sein.
-- Zwei gleiche Listen
[1, 2, 3, 4, 5]
[1..5]

-- Die zweite Variante nennt sich die "range"-Syntax.
-- Ranges sind recht flexibel:
['A'..'F'] -- "ABCDEF"

-- Es ist möglich eine Schrittweite anzugeben:
[0,2..10] -- [0,2,4,6,8,10]
[5..1] -- [], da Haskell standardmässig inkrementiert.
[5,4..1] -- [5,4,3,2,1]

-- Der "!!"-Operator extrahiert das Element an einem bestimmten Index:
[1..10] !! 3 -- 4

-- Haskell unterstützt unendliche Listen!
[1..] -- Die Liste aller natürlichen Zahlen

-- Unendliche Listen funktionieren in Haskell, da es "lazy evaluation"
-- unterstützt. Haskell evaluiert erst etwas, wenn es benötigt wird.
-- Somit kannst du nach dem 1000. Element fragen und Haskell gibt es dir:

[1..] !! 999 -- 1000

-- Haskell evaluiert nun die ersten 1 - 1000 Elemente, aber der Rest der Liste
-- bleibt unangetastet. Haskell wird sie solange nicht weiterevaluieren
-- bis es muss.

-- Zwei Listen konkatenieren
[1..5] ++ [6..10]

-- Ein Element als Head hinzufügen
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- Weitere Listenoperationen
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- Listen erschaffen ("list comprehensions")
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- Mit Bedingungen
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- Tupel haben eine feste Länge, jedes Element darf aber ein anderen Typ haben.
-- Ein Tupel:
("haskell", 1)

-- Ein Paar (Pair) ist ein Tupel mit 2 Elementen, auf die man wie folgt
-- zugreifen kann:
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. Funktionen
----------------------------------------------------
-- Eine einfache Funktion die zwei Argumente hat.
add a b = a + b

-- Wenn man ghci (den Haskell Interpreter) benutzt, muss ein `let` davor.
-- let add a b = a + b

-- Eine Funktion aufrufen
add 1 2 -- 3

-- Man kann eine Funktion auch Infix verwenden,
-- wenn man sie mit backticks umgibt
1 `add` 2 -- 3

-- So sieht die Definition eines eigenen Operators aus.
-- Also einer Funktion deren Name aus Symbolen besteht.
-- Die Integer Division:
(//) a b = a `div` b
35 // 4 -- 8

-- Guards sind eine einfache Möglichkeit für Fallunterscheidungen.
fib x
  | x < 2 = 1
  | otherwise = fib (x - 1) + fib (x - 2)

-- Pattern Matching funktioniert ähnlich.
-- Hier sind drei Definitionen von fib. Haskell wird automatisch
-- die erste Funktionen nehmen die dem Pattern der Eingabe entspricht.
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- Pattern matching auf Tupeln:
foo (x, y) = (x + 1, y + 2)

-- Pattern matching auf Listen.
-- `x` ist das erste Element der Liste und `xs` der Rest der Liste.
-- Damit können wir unsere eigene map Funktion bauen:
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- Anonyme Funktionen (Lambda-Funktionen) werden mit einem
-- Backslash eingeleitet, gefolgt von allen Argumenten.
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- Fold (`inject` in einigen Sprachen)
-- Foldl1 bedeutet: fold von links nach rechts und nehme den ersten
-- Wert der Liste als Basiswert für den Akkumulator.
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. Mehr Funktionen
----------------------------------------------------

-- currying: Wenn man nicht alle Argumente an eine Funktion übergibt,
-- so wird sie eine neue Funktion gebildet ("curried").
-- Es findet eine partielle Applikation statt und die neue Funktion
-- nimmt die fehlenden Argumente auf.

add a b = a + b
foo = add 10 -- foo ist nun Funktion die ein Argument nimmt und 10 addiert
foo 5 -- 15

-- Ein alternativer Weg
foo = (+10)
foo 5 -- 15

-- Funktionskomposition
-- Die (.) Funktion verkettet Funktionen.
-- Zum Beispiel, die Funktion Foo nimmt ein Argument, addiert 10 dazu und
-- multipliziert dieses Ergebnis mit 4.
foo = (*4) . (+10)

-- (5 + 10) * 4 = 60
foo 5 -- 60


-- Haskell hat einen Operator `$`, welcher Funktionsapplikation durchführt.
-- Im Gegenzug zu der Standard-Funktionsapplikation, welche linksassoziativ ist
-- und die höchstmögliche Priorität von "10" hat, ist der `$`-Operator
-- rechtsassoziativ und hat die Priorität 0. Dieses hat (i.d.R.) den Effekt,
-- dass der `komplette` Ausdruck auf der rechten Seite als Parameter für die
-- Funktion auf der linken Seite verwendet wird.
-- Mit `.` und `$` kann man sich so viele Klammern ersparen.

(even (fib 7)) -- false

-- Äquivalent:
even $ fib 7 -- false

-- Funktionskomposition:
even . fib $ 7 -- false

----------------------------------------------------
-- 5. Typensystem
----------------------------------------------------

-- Haskell hat ein sehr starkes Typsystem.
-- Alles hat einen Typ und eine Typsignatur.

-- Einige grundlegende Typen:
5 :: Integer
"hello" :: String
True :: Bool

-- Funktionen haben genauso Typen.
-- `not` ist Funktion die ein Bool annimmt und ein Bool zurückgibt:
-- not :: Bool -> Bool

-- Eine Funktion die zwei Integer Argumente annimmt:
-- add :: Integer -> Integer -> Integer

-- Es ist guter Stil zu jeder Funktionsdefinition eine
-- Typdefinition darüber zu schreiben:
double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. If-Ausdrücke und Kontrollstrukturen
----------------------------------------------------

-- If-Ausdruck:
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- If-Ausdrücke können auch über mehrere Zeilen verteilt sein.
-- Die Einrückung ist dabei wichtig.
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- Case-Ausdruck: Am Beispiel vom Parsen von "commandline"-Argumenten.
case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"

-- Haskell nutzt Rekursion anstatt Schleifen.
-- map wendet eine Funktion auf jedes Element einer Liste an.

map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- So kann man auch eine for-Funktion kreieren.
for array func = map func array

-- und so benutzt man sie:
for [0..5] $ \i -> show i

-- wir hätten sie auch so benutzen können:
for [0..5] show

-- foldl oder foldr reduziren Listen auf einen Wert.
-- foldl <Funktion> <initialer Wert> <Liste>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- die Abarbeitung sieht so aus:
(2 * (2 * (2 * 4 + 1) + 2) + 3)

-- foldl ist linksseitig und foldr rechtsseitig.
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- die Abarbeitung sieht so aus:
(2 * 1 + (2 * 2 + (2 * 3 + 4)))

----------------------------------------------------
-- 7. Datentypen
----------------------------------------------------

-- So kann man seine eigenen Datentypen in Haskell anlegen:

data Color = Red | Blue | Green

-- Nun können wir sie in einer Funktion benutzen.

say :: Color -> String
say Red = "You are Red!"
say Blue = "You are Blue!"
say Green =  "You are Green!"

-- Datentypen können auch Parameter aufnehmen:

data Maybe a = Nothing | Just a

-- Diese sind alle vom Typ Maybe:
Just "hello"    -- vom Typ `Maybe String`
Just 1          -- vom Typ `Maybe Int`
Nothing         -- vom Typ `Maybe a` für jedes `a`

----------------------------------------------------
-- 8. Haskell IO
----------------------------------------------------

-- IO kann nicht völlig erklärt werden ohne Monaden zu erklären,
-- aber man kann die grundlegenden Dinge erklären.

-- Wenn eine Haskell Programm ausgeführt wird, so wird `main` aufgerufen.
-- Diese muss etwas vom Typ `IO ()` zurückgeben. Zum Beispiel:

main :: IO ()
main = putStrLn $ "Hello, sky! " ++ (say Blue)
-- putStrLn hat den Typ String -> IO ()

-- Es ist am einfachsten, wenn man sein Programm als Funktion von
-- String nach String implementiert.
-- Zum Beispiel die Funktion interact :: (String -> String) -> IO ()
-- nimmt einen Text, tut etwas damit und gibt diesen wieder aus.

countLines :: String -> String
countLines = show . length . lines

main' = interact countLines

-- Man kann den Typ `IO ()` als Repräsentation einer Sequenz von
-- Aktionen sehen, die der Computer abarbeiten muss.
-- Wie bei einem Programm das in einer Imperativen Sprache geschreiben wurde.
-- Mit der `do` Notation können Aktionen verbunden werden.

sayHello :: IO ()
sayHello = do
   putStrLn "What is your name?"
   name <- getLine -- eine Zeile wird geholt und
                   -- an die Variable "name" gebunden
   putStrLn $ "Hello, " ++ name

-- Übung: Schreibe deine eigene Version von `interact`,
-- die nur eine Zeile einliest.

-- `sayHello` wird niemals ausgeführt, nur `main` wird ausgeführt.
-- Um `sayHello` laufen zulassen kommentiere die Definition von `main`
-- aus und ersetze sie mit:
--     main = sayHello

-- Lass uns untersuchen wie `getLine` arbeitet.
-- Der Typ ist: getLine :: IO String
-- Man kann sich vorstellen das der Wert vom Typ `IO a` ein
-- Programm repräsentiert das etwas vom Typ `a` generiert.
-- Der Wert wird mit `<-` gespeichert und kann wieder benutzt werden.
-- Wir könne auch eigene Funktionen vom Typ `IO String` definieren:

action :: IO String
action = do
   putStrLn "This is a line. Duh"
   input1 <- getLine
   input2 <- getLine
   -- Der Typ von `do` ergibt sich aus der letzten Zeile.
   -- `return` ist eine Funktion und keine Schlüsselwort
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- Nun können wir `action` wie `getLine` benutzen:

main'' = do
    putStrLn "I will echo two lines!"
    result <- action
    putStrLn result
    putStrLn "This was all, folks!"

-- Der Typ `IO` ist ein Beispiel für eine Monade.
-- Haskell benutzt Monaden Seiteneffekte zu kapseln und somit
-- eine rein funktional Sprache zu sein.
-- Jede Funktion die mit der Außenwelt interagiert (z.B. IO)
-- hat den Typ `IO` in seiner Signatur.
-- Damit kann man zwischen "reinen" Funktionen (interagieren nicht
-- mit der Außenwelt oder ändern ihren Zustand) und Anderen unterscheiden.

-- Nebenläufigkeit ist in Haskell sehr einfach, da reine Funktionen
-- leicht nebenläufig arbeiten können.

----------------------------------------------------
-- 9. Die Haskell REPL
----------------------------------------------------

-- Starte die REPL mit dem Befehl `ghci`
-- Nun kann man Haskell Code eingeben.
-- Alle neuen Werte müssen mit `let` gebunden werden:

let foo = 5

-- `:t` zeigt den Typen von jedem Wert an:

>:t foo
foo :: Integer

-- Auch jede `IO ()` Funktion kann ausgeführt werden.

> sayHello
What is your name?
Friend!
Hello, Friend!

```

Es gibt noch viel mehr in Haskell, wie zum Beispiel Typklassen und Monaden.
Dies sind die Ideen durch die Haskell Programmierung zum Spaß wird.
Mit dem folgenden kleinen Beispiel werde ich euch verlassen:
Quicksort in Haskell:

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

Haskell ist sehr einfach zu installieren.
Hol es dir von [hier](http://www.haskell.org/platform/).

Eine sehr viele langsamere Einführung findest du unter:
[Learn you a Haskell](http://learnyouahaskell.com/) oder
[Real World Haskell](http://book.realworldhaskell.org/).
---
language: html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
translators:
    - ["Dennis Keller", "https://github.com/denniskeller"]
filename: learnhtml-de.html
lang: de-de
---

HTML steht für HyperText Markup Language (Hypertext-Auszeichnungssprache).
Sie ist eine Sprache, um Seiten für das World Wide Web zu schreiben..
Es ist eine Auszeichnugssprache, die es uns ermöglicht Webseiten mithilfe des Codes zu schreiben, der kennzeichnet  wie Text und Daten angezeigt werden sollen. Eigentlich sind HTML Dateien nur einfache Textdateien.
Was sind das für Auszeichnungen? Es ist eine Methode, um die Daten der Website zu organisieren mithilfe von Start- und Endtags.
Diese Auszeichnung dient dazu dem Text Bedeutung zu geben, welchen sie umschließt.
Wie viele andere Computersprachen auch, besitzt HTML viele Versionen. Wir werden hier über HTML5 reden.

**NOTE :**  Du kannst die unterschiedlichen Tags und Elemente, während des Tutorials auf Seiten, wie [codepen](http://codepen.io/pen/) testen, um deren Effekte zu sehen und wie diese funktionieren. Auch kannst du dich damit besser mit der Sprache vertraut machen.
Dieser Artikel ist bedacht darauf, nur HTML Syntax und nützliche Tipps zu geben.


```html
<!-- Kommentare werden wie in dieser Zeile geschrieben -->

<!-- #################### Die Tags #################### -->

<!-- Hier ist eine Beispiel HTML Datei, die wir analysieren werden -->

<!doctype html>
	<html>
		<head>
			<title>Meine Website</title>
		</head>
		<body>
			<h1>Hallo Welt!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">Komm schau was das hier anzeigt</a>
			<p>Das ist ein Paragraf.</p>
			<p>Das ist ein weiterer Paragraf.</p>
			<ul>
				<li>Das ist eine Item mit einer nicht-nummerierten Liste (Aufzählungsliste)</li>
				<li>Das ist ein weiteres Item</li>
				<li>Und das ist das letzte Item in der Liste</li>
			</ul>
		</body>
	</html>

<!-- Jede HTML Datei startet damit dem Browser zu sagen, dass die Seite aus HTML besteht. -->
<!doctype html>

<!-- Danach startet sie mit einem Öffnungtag <html>. -->
<html>

<!-- Dieser wird am Ende der Datei mit</html> geschlossen. -->
</html>

<!-- Nichts sollte nach diesen finalen Tag erscheinen. -->

<!-- Dazwischen (Zwischen dem Öffnungs- und Schließungstag <html></html>) finden wir: -->

<!-- Ein Kopf wird definiert mit <head> (er muss mit </head> geschlossen werden). -->
<!-- Der Header beinhaltet Beschreibungen und zusätzliche Informationen, welche nicht dargestellt werden. Das sind Metadaten. -->

<head>
	<title>Meine Seite</title><!-- Der <title> kennzeichnet dem Browser den Titel im Browserfenster und im Tabnamen anzuzeigen. -->
</head>

<!-- Nach dem <head> Bereich findet sich der <body> Tag -->
<!-- Bis zu diesen Punkt wird nichts im Browerfenster angezeigt. -->
<!-- Wir müssen den Body mit dem Inhalt füllen der angezeigt werden soll. -->

<body>
	<h1>Hallo, Welt!</h1> <!-- Der h1 Tag erstellt einen Titel. -->
	<!-- Es gibt auch Untertitel für <h1> von den wichtigsten <h2> zu den Unwichtigsten (h6). -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">Komm, schaue was das zeigt</a> <!-- Eine URL wird zum Hyperlink, wenn es das Attribut href=""  -->
	<p>Das ist ein Absatz.</p> <!-- Der Tag <p> lässt uns Text auf die HTML Seite hinzufügen. -->
	<p>Das ist ein anderer Absatz.</p>
	<ul> <!-- Der <ul> Tag erstellt eine Aufzählungsliste. -->
	<!-- Für eine nummerierte Liste sollten wir stattdessen <ol> verwenden. Das erste Element bekommt 1., das zweite 2. usw. -->
		<li>Das ist ein Element in einer nicht Aufzählungsliste</li>
		<li>Das ist ein anderes Item</li>
		<li>Und das ist das letzte Element in der List</li>
	</ul>
</body>

<!-- Und das war es. Eine HTML Datei kann so simpel sein. -->

<!-- Aber es ist möglich viele weitere zusätzliche HTML tags hinzuzufügen. -->

<!-- Um ein Bild hinzuzufügen. -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- Die Quelle des Bildes wird gezeigt durch das Attribut src="" -->
<!-- Die Quelle kann eine URL sein oder ein Pfad zu deinem Computer. -->

<!-- Es ist ebenso möglich eine Tabelle zu erstellen. -->

<table> <!-- Wir öffnen ein <table> Element. -->
	<tr> <!-- <tr> erlaubt es uns Reihe zu erstellen. -->
		<th>Erster Tabellenkopf</th> <!-- <th> erlaubt es uns der Tabelle einen Titel zu geben. -->
		<th>Zweiter Tabllenkopf</th>
	</tr>
	<tr>
		<td>Erste Zeile, erste Spalte</td> <!-- <td> erlaubt es eine Tabellenzelle zu erstellen. -->
		<td>Erste Zeile, zweite Spalte</td>
	</tr>
	<tr>
		<td>Zweite Zeile, erste Spalte</td>
		<td>Zweite Zeile, zweite Spalte</td>
	</tr>
</table>

```

## Verwendung

HTML Dateien enden mit `.html`.

## Um mehr zu lernen

* [wikipedia (EN)](https://en.wikipedia.org/wiki/HTML)
* [HTML tutorial (EN)](https://developer.mozilla.org/en-US/docs/Web/HTML)
* [W3School (EN)](http://www.w3schools.com/html/html_intro.asp)
---
language: java
filename: LearnJavaDe-de.java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Jakukyo Friel", "http://weakish.github.io"]
    - ["Madison Dickson", "http://github.com/mix3d"]
    - ["Simon Morgan", "http://sjm.io/"]
translators:
    - ["Michael Dähnert", "http://github.com/JaXt0r"]
lang: de-de
---

Java ist eine Programmiersprache für vielfältige Aufgaben. Sie ist imperative und objektorientiert.
Oftmals wird sie für Desktop- Webapplikationen sowie als Programmiersprache im Betriebssystem Android verwendet.
[Weitere Informationen \(Englisch\)](http://docs.oracle.com/javase/tutorial/java/)

```java
// Einzeilige Kommentare starten mit //
/*
Mehrzeilige Kommentare sehen so aus.
*/
/**
JavaDoc Kommentare haben dieses Format. Sie werden verwendet um Klassen, Attribute sowie Methoden zu beschreiben.
*/

// Importieren der Klasse ArrayList aus dem Paket java.util
import java.util.ArrayList;
// Importieren aller Klassen innerhalb des Paketes java.security
import java.security.*;

// Jede .java Datei besteht aus einer äußeren öffentlichen (public) Klasse.
// Der Name der Klasse muss identisch des Dateinamens sein.
public class LearnJavaDe {

    // Ein Programm muss eine main Methode als Eintrittspunkt besitzen.
    public static void main (String[] args) {

        // System.out.println() wird zum Schreiben von zeilenweisen Ausgaben verwendet.
        System.out.println("Hello World!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // Zum Schreiben von Ausgaben ohne Zeilenumbruch wird System.out.print() verwendet.
        System.out.print("Hello ");
        System.out.print("World");


        ///////////////////////////////////////
        // Typen & Variablen
        ///////////////////////////////////////

        // Zum Deklarieren einer Variable nutze <type> <name>
        // Byte - 8-bit vorzeichenbehaftete (signed), binäre Ganzzahl
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short - 16-bit vorzeichenbehaftete (signed), binäre Ganzzahl
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - 32-bit vorzeichenbehaftete (signed), binäre Ganzzahl
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int fooInt = 1;

        // Long - 64-bit vorzeichenbehaftete (signed), binäre Ganzzahl
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L wird verwendet um zu kennzeichnen, dass ein Variablenwert vom Typ long ist.
        // Ohne diesen Buchstaben wird die Zahl automatisch als Integer behandelt.

        // Hinweis: Java besitzt keine vorzeichenlosen (unsigned) Typen.

        // Float - Typ mit einfacher Genauigkeit (Single-precision), 32-bit IEEE 754 Fließkommazahl
        float fooFloat = 234.5f;
        // f wird verwendet um zu kennzeichnen, dass ein Variablenwert vom Typ float ist;
        // Ohne diesen Buchstaben wird die Zahl automatisch als Integer behandelt.

        // Double - Typ mit doppelter Genauigkeit (Double-precision), 64-bit IEEE 754 Fließkommazahl
        double fooDouble = 123.4;

        // Boolean - Wahr & Falsch (true & false)
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - Ein einfacher 16-bit Unicode Buchstabe
        char fooChar = 'A';

        // final Variablen können von einem anderen Objekt nicht erneut zugeordnet werden.
        final int HOURS_I_WORK_PER_WEEK = 9001;

        // Zeichenketten (Strings)
        String fooString = "My String Is Here!";

        // \n ist ein Escape Zeichen welcher eine neue Zeile startet.
        String barString = "Schreiben auf einer neuen Zeile?\nKein Problem!";
        // \t ist ein Escape Zeichen welcher einen Tab-Zeichen anhängt.
        String bazString = "Möchtest du einen Tabulator anhängen?\tKein Problem!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // Arrays
        // Die Arraygröße muss bei Instanziierung entschieden werden.
        // Das folgende Format funktioniert bei Deklaration eines Arrays
        // <datentyp>[] <variablenname> = new <datentyp>[<arraygröße>];
        // <datentyp> <variablenname>[] = new <datentyp>[<arraygröße>];
        int[] intArray = new int[10];
        String[] stringArray = new String[1];
        boolean boolArray[] = new boolean[100];

        // Eine weitere Möglichkeit ein Array zu deklarieren & initialisieren.
        int[] y = {9000, 1000, 1337};
        String names[] = {"Bob", "John", "Fred", "Juan Pedro"};
        boolean bools[] = new boolean[] {true, false, false};

        // Indexierung eines Arrays - Zugriff auf ein Element
        System.out.println("intArray @ 0: " + intArray[0]);

        // Arrays sind 0-indexiert und veränderbar.
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // Weitere nennenswerte Typen
        // ArrayLists - Ähnlich Arrays, allerdings werden mehr Funktionen geboten,
        //            ebenso ist die Arraygröße verwänderbar
        // LinkedLists - Implementierung einer doppelt verlinkten Liste.
        //            Alle Operationen funktioneren so, wie es von einer doppelt verlinkten Liste erwartet wird.
        //            Weitere Informationen: https://de.wikipedia.org/wiki/Liste_(Datenstruktur)#Doppelt_.28mehrfach.29_verkettete_Liste
        // Maps - Eine Sammlung von Objekten, welche eine Verknüpfung von Schlüsseln zu Werten (key => value) vornimmt.
        //            Eine Map kann keine Duplikate enthalten; Jeder Schlüssel kann genau einen Wert beinhalten.
        // HashMaps - Diese Klasse nutzt eine Hashtabelle zur Implementierung eines Map Interfaces.
        //            Dies erlaubt es zur Laufzeit Standardoperationen wie gib (get) und einfügen (insert)
        //            selbst für große Mengen in einer konstanten Zeit auszuführen (Laufzeitverhalten O(n)).

        ///////////////////////////////////////
        // Operatoren
        ///////////////////////////////////////
        System.out.println("\n->Operatoren");

        int i1 = 1, i2 = 2; // Kurform zur Deklaration mehrerer Variablen.

        // Arithmetische Operationen sind einfach nutzbar.
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (0.5 Nachkommazahl abgeschnitten)

        // Modulo
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Vergleichsoperationen
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Bitwise Operatoren!
        /*
        ~      Unäres (unary) bitweise Komplement
        <<     Vorzeichenbehaftete (signed) linke Verschiebung
        >>     Vorzeichenbehaftete (signed) rechte Verschiebung
        >>>    Vorzeichenlose (unsigned) linke Verschiebung
        &      Bitweise UND (AND)
        ^      Bitweise exklusive ODER (OR)
        |      Bitweise inklusive ODER (OR)
        */

        // Inkrementierungen
        int i = 0;
        System.out.println("\n->Inc/Dec-rementierung");
        // Die ++ und -- operatoren inkrementieren und dekrementieren jeweils um 1.
        // Werden sie vor die Variable gesetzt, ink-/dekrementieren sie und geben anschließend ihren Wert zurück.
        // Hinter der Variable geben sie ihren Wert zurück und ändern ihn anschließend.
        System.out.println(i++); // i = 1, schreibt 0 (post-increment)
        System.out.println(++i); // i = 2, schreibt 2 (pre-increment)
        System.out.println(i--); // i = 1, schreibt 2 (post-decrement)
        System.out.println(--i); // i = 0, schreibt 0 (pre-decrement)

        ///////////////////////////////////////
        // Kontrollstrukturen
        ///////////////////////////////////////
        System.out.println("\n->Kontrollstrukturen");

        // If Bedingungen sind wie in den C-Sprachen aufgebaut
        int j = 10;
        if (j == 10){
            System.out.println("Ich wurde geprinted");
        } else if (j > 10) {
            System.out.println("Ich nicht");
        } else {
            System.out.println("Ich auch nicht");
        }

        // While Schleife
        int fooWhile = 0;
        while(fooWhile < 100) {
            System.out.println(fooWhile);
            // Den Zähler inkrementieren
            // 100x iterieren, fooWhile 0,1,2...99
            fooWhile++;
        }
        System.out.println("fooWhile Wert: " + fooWhile);

        // Do While Schleife
        int fooDoWhile = 0;
        do {
            System.out.println(fooDoWhile);
            // Den Zähler inkrementieren
            // 99x iterieren, fooDoWhile 0->99
            fooDoWhile++;
        } while(fooDoWhile < 100);
        System.out.println("fooDoWhile Wert: " + fooDoWhile);

        // For Schleife
        int fooFor;
        // for Schleifenstruktur => for(<start_statement>; <Bedingung>; <Schritt>)
        for (fooFor = 0; fooFor < 10; fooFor++) {
            System.out.println(fooFor);
            // 10x iterieren, fooFor 0->9
        }
        System.out.println("fooFor Wert: " + fooFor);

        // For Each Schleife
        // The for Schleife kann verwendet werden um über Arrays ebenso wie Objekte,
        // welche das Interface Iterable implementieren zu iterieren.
        int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        // for each Schleifenstruktur => for (<Objekt> : <iterable>)
        // Wird gelesen als: Iteriere für jedes Element im Iterable
        // Hinweis: Der Objekttyp muss dem Elementtyp des Iterable entsprechen.

        for (int bar : fooList) {
            System.out.println(bar);
            //9x iterieren und die Werte 1-9 auf jeweils einer neuen Zeile schreiben
        }

        // Switch Case
        // A Schalter (switch) funktioniert mit den Datentypen byte, short, char und int.
        // Ebenso kann er für Aufzählungen (Enums) verwendet werden (Enum Typen folgen weiter unten)
        // der String Klasse (ab Java SE7) und ein paar spezielle Klassen, welche die primitiven Typen ummanteln (wrap):
        // Character, Byte, Short, and Integer.
        int monat = 3;
        String monatsString;
        switch (monat) {
            case 1: monatsString = "Januar";
                    break;
            case 2: monatsString = "Februar";
                    break;
            case 3: monatsString = "März";
                    break;
            default: monatsString = "Ein anderer Monat";
                     break;
        }
        System.out.println("Switch Case Ergebnis: " + monatsString);

        // Bedingungsoperator (Conditional Shorthand)
        // Der Operator '?' kann für schnelle Zuweisungen oder logische Verzweigungen genutzt werden.
        // Er ist wie folgt zu lesen: Wenn die Bedingung wahr ist, nutze <erster Wert>
        // ansonsten nutze <zweiter Wert>
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println(bar); // Schreibt A, denn die Bedingung ist wahr.


        ////////////////////////////////////////
        // Typkonvertierung und Type-Casting
        ////////////////////////////////////////

        // Konvertierung von Daten

        // Konvertiere String nach Integer
        Integer.parseInt("123");// Gibt die Integer Repräsentation von "123" zurück

        // Konvertiere String nach Integer
        Integer.toString(123);// Gibt die String Repräsentation von 123 zurück

        // Für andere Konvertierungen sind die folgenden Klassen zu betrachten:
        // Double
        // Long
        // String

        // Tpe-Casting
        // Java Objekte können benfalls konvertiert werden, hierbei gibt es vielfältige Konzepte.
        // Weitere Informationen finden sich hier (englisch):
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Klassen und Funktionen
        ///////////////////////////////////////

        System.out.println("\n->Klassen & Funktionen");

        // (Die Definition der Klasse Fahrrad folgt)

        // Verwendung einer neuen Klasseninstanz
        Fahrrad trek = new Fahrrad();

        // Aufruf von Methoden des Objektes
        trek.erhöheGeschwindigkeit(3); // Es sollten immer getter- und setter- Methoden verwendet werden
        trek.setTrittfrequenz(100);

        // toString gibt die StringRepräsentation des Objektes zurück.
        System.out.println("trek info: " + trek.toString());

    } // Ende der Main Methode
} // Ende der LearnJavaDe Klasse


// In einer .java-Datei können zusätzliche nicht öffentliche (non-public) äüßere Klassen vorhanden sein.


// Syntax der Klassendeklaration:
// <public/private/protected> class <Klassenname> {
//    // Es folgen Datenfelder, Konstruktoren, Funktionen.
//    // Funktionen werden in Java Methoden genannt.
// }

class Fahrrad {

    // Felder/Variablen der Klasse Fahrrad
    public int trittfrequenz; // Public: Kann von überall her angesprochen werden
    private int geschwindigkeit;  // Private: Nur innerhalb der Klasse sichtbar
    protected int gang; // Protected: Erreichbar innerhalb der Klasse oder Subklassen (sub classes)
    String name; // default: Nur innerhalb des Paketes verwendbar

    // Eine Klasse kann mittelst Konstruktoren erstellt werden.
    // Das ist ein Konstruktor
    public Fahrrad() {
        gang = 1;
        trittfrequenz = 50;
        geschwindigkeit = 5;
        name = "Bontrager";
    }

    // Das ist ein Konstruktor mit Argumenten
    public Bicycle(int initialTrittfrequenz, int initialGeschwindigkeit, int initialGang,
        String name) {
        this.gang = initialGang;
        this.trittfrequenz = initialTrittfrequenz;
        this.geschwindigkeit = initialGeschwindigkeit;
        this.name = name;
    }

    // Syntax von Methoden (Funktionen):
    // <public/private/protected> <Rückgabetyp> <Funktionsname>(<Argumente>)

    // Java Klassen implementieren oftmals getter- und setter-Methoden ihrer Felder

    // Syntax von Methodendeklarationen:
    // <Sichtbarkeit> <Rückgabetyp> <Methodenname>(<Argumente>)
    public int getTrittfrequenz() {
        return tri;
    }

    // void Methoden benötigen kein return Statement.
    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void erhöheGeschwindigkeit(int increment) {
        speed += increment;
    }

    public void verringereGeschwindigkeit(int decrement) {
        speed -= decrement;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    //Methode zur Darstellung der Attributwerte des Objektes.
    @Override
    public String toString() {
        return "Gang: " + gang + " Trittfrequenz: " + trittfrequenz + " Geschwindigkeit: " + geschwindigkeit +
            " name: " + name;
    }
} // Ende der Klasse Fahrrad

// Hochrad ist eine Subklasse von Fahrrad
class Hochrad extends Fahrrad {
    // (Hochräder sind Fahrräder mit einem extrem großen Vorderrad.
    // Sie haben keine Gänge.)

    public Hochrad(int initialTrittfrequenz, int initialGeschwindigkeit){
        // Aufruf des Vater-Konstruktors (parent constructor) mit dem Wort super.
        super(initialTrittfrequenz, initialGeschwindigkeit, 0, "Hochrad");
    }

    // Überschriebene Methoden sollten die Annotation @Override besitzen.
    // Mehr zu Annotationen und deren Verwendungszwecken kann hier nachgelesen werden:
    // (englisch) http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGang(int gang) {
        gang = 0;
    }
}

// Schnittstellen (Interfaces)
// Interface Deklaration
// <Zugriffsrecht> interface <Name> extends <super-Interface> {
//     // Konstanten
//     // Methodendeklarationen
// }

// Beispiel - Nahrung:
public interface Essbar {
	public void essen(); // Jede Klasse, die dieses Interface implementiert
                       // muss auch diese Methode implementieren.
}                       


public interface Verdaulich {
	public void verdauen();
}


// Nun können wir eine Klasse erstellen, die beide Interfaces implementiert.
public class Frucht implements Essbar, Verdaulich {
    @Override
	public void essen() {
		// ...
	}

    @Override
	public void verdauen() {
		// ...
	}
}

// Mit Java kann man nur eine Klasse erweitern (extends) jedoch mehrere Interfaces implementieren.
// z.B.:
public class BeispielKlasse extends ParentBeispielKlasse implements InterfaceEins,
    InterfaceZwei {
    @Override
	public void methodeInterfaceEins() {
	}

    @Override
	public void methodeInterfaceZwei() {
	}
}
```

## Weitere Informationen (in englisch)

Die folgenden Links dienen lediglich dazu Verständnis für die Kapitel aufzubauen.
Für tiefergreifende Fragen ist Google der beste Startpunkt.

**Offizielle Oracle Guides**:

* [Java Tutorial Trail from Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Java Access level modifiers](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Object-Oriented Programming Concepts](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Inheritance](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Abstraction](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Interfaces](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html)

**Online Tutorials**

* [Learneroo.com - Learn Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)


**Bücher**:

* [Head First Java](http://www.headfirstlabs.com/books/hfjava/)

* [Thinking in Java](http://www.mindview.net/Books/TIJ/)

* [Objects First with Java](http://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660)

* [Java The Complete Reference](http://www.amazon.com/gp/product/0071606300)
---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
translators:
    - ["ggb", "http://www.ideen-und-soehne.de"]
filename: learnjavascript-de.js
lang: de-de
---

(Anmerkungen des Original-Autors:)
JavaScript wurde im Jahr 1995 von Brendan Eich bei Netscape entwickelt. Ursprünglich war es als einfachere Skriptsprache für Websites gedacht, ergänzend zu Java, das für komplexere Webanwendungen verwendet wird. Die enge Integration in Websites und der in Browser eingebaute Support der Sprache haben dafür gesorgt, dass JavaScript weit häufiger für Web-Frontends verwendet wird als Java.

Dabei ist JavaScript inzwischen nicht mehr auf Browser beschränkt: Node.js, ein Projekt, das eine eigene Laufzeitumgebung auf Grundlage von Google Chromes V8 mitbringt, wird derzeit immer populärer.

Feedback ist herzlich Willkommen! Der ursprüngliche Autor ist unter [@adambrenecki](https://twitter.com/adambrenecki) oder [adam@brenecki.id.au](mailto:adam@brenecki.id.au) zu erreichen. Der Übersetzer unter [gregorbg@web.de](mailto:gregorbg@web.de).

```js
// Kommentare werden wie in C gesetzt: Einzeilige Kommentare starten mit zwei 
// Slashes
/* während mehrzeilige Kommentare mit einem 
Slash und einem Stern anfangen und enden */

// Statements können mit einem Semikolon beendet werden
machWas();

// ...müssen sie aber nicht, weil Semikola automatisch eingefügt werden, wenn 
// eine neue Zeile beginnt, abgesehen von einigen Ausnahmen.
machWas()

// Obwohl wir uns für den Anfang nicht um diese Ausnahmen kümmern müssen ist 
// es besser die Semikola immer zu setzen.

///////////////////////////////////
// 1. Nummern, Strings und Operationen

// JavaScript hat einen Nummern-Typ (64-bit IEEE 754 double).
3; // = 3
1.5; // = 1.5

// Beinahe alle grundlegenden arithmetischen Operationen arbeiten wie erwartet.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
10 * 2; // = 20
35 / 5; // = 7

// Division funktioniert auch mit einem Ergebnis nach dem Komma.
5 / 2; // = 2.5

// Bit-weise Operationen sind auch möglich; wenn eine Bit-weise Operation 
// ausgeführt wird, wird die Fließkomma-Zahl in einen 32-bit Integer (mit 
// Vorzeichen) umgewandelt.
1 << 2; // = 4

// Die Rangfolge der Operationen kann mit Klammern erzwungen werden.
(1 + 3) * 2; // = 8

// Es gibt drei spezielle, nicht-reale Nummern-Werte:
Infinity; // Ergebnis von z. B. 1 / 0
-Infinity; // Ergebnis von z. B. -1 / 0
NaN; // Ergebnis von z. B. 0 / 0

// Es gibt auch einen Boolean-Typ (für Wahrheitswerte).
true;
false;

// Strings werden mit ' oder " erzeugt.
'abc';
"Hello, world";

// Für die Negation wird das ! benutzt.
!true; // = false
!false; // = true

// Gleichheit wird mit === geprüft.
1 === 1; // = true
2 === 1; // = false

// Ungleichheit wird mit !== überprüft.
1 !== 1; // = false
2 !== 1; // = true

// Andere Vergleichsoperatoren sind
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Strings können mit + verbunden 
"Hello " + "world!"; // = "Hello world!"

// und mit < und > verglichen werden.
"a" < "b"; // = true

// Für den Vergleich von Werten mit "==" wird eine Typumwandlung erzwungen...
"5" == 5; // = true

// ...solange man nicht === verwendet.
"5" === 5; // = false

// Auf einzelne Buchstaben innerhalb eines Strings kann mit der Methode 
// 'charAt' zugegriffen werden
"This is a string".charAt(0);  // = "T"

// Die Methode 'substring' gibt Teilbereiche eines Strings zurück
"Hello world".substring(0, 5); // = "Hello"

// 'length' ist eine Eigenschaft und wird folglich ohne '()' benutzt
"Hello".length; // = 5

// Es gibt außerdem die Werte 'null' und 'undefined'
null; // wird verwendet um einen vorsätzlich gewählten 'Nicht'-Wert anzuzeigen
undefined; // wird verwendet um anzuzeigen, dass der Wert (aktuell) nicht 
           // verfügbar ist (obwohl genau genommen undefined selbst einen Wert 
           // darstellt)

// false, null, undefined, NaN, 0 und "" sind 'falsy', d. h. alles andere ist 
// wahr. Man beachte, dass 0 falsch und "0" wahr ist, obwohl 0 == "0".

///////////////////////////////////
// 2. Variablen, Arrays und Objekte

// Variablen werden mit dem Schlüsselwort 'var' und einem frei wählbaren 
// Bezeichner deklariert. JavaScript ist dynamisch typisiert, so dass man einer
// Variable keinen Typ zuweisen muss. Die Zuweisung verwendet ein einfaches =.
var einWert = 5;

 // Wenn man das Schlüsselwort 'var' weglässt, bekommt man keinen Fehler
einAndererWert = 10;

// ...aber die Variable wird im globalen Kontext erzeugt, nicht in dem Kontext,
// in dem sie erzeugt wurde.

// Variablen die erzeugt wurden ohne ihnen einen Wert zuzuweisen, erhalten den
// Wert 'undefined'.
var einDritterWert; // = undefined

// Es existiert eine Kurzform, um mathematische Operationen mit Variablen 
// auszuführen:
einWert += 5; // äquivalent zu einWert = einWert + 5; einWert ist nun also 10
einWert *= 10; // einWert ist nach dieser Operation 100

// Und es existiert eine weitere, sogar noch kürzere Form, um 1 zu addieren 
// oder zu subtrahieren
einWert++; // nun ist einWert 101
einWert--; // wieder 100

// Arrays sind geordnete Listen von Werten irgendeines Typs
var myArray = ["Hello", 45, true];

// Auf einzelne Elemente eines Arrays kann zugegriffen werden, in dem der Index
// in eckigen Klammern hinter das Array geschrieben werden. Die Indexierung 
// beginnt bei 0.
myArray[1]; // = 45

// Arrays haben keine feste Länge
myArray.push("World");
myArray.length; // = 4

// und sind veränderlich
myArray[3] = "Hello";

// Die Objekte in JavaScript entsprechen 'dictionaries' oder 'maps' in anderen 
// Sprachen: es handelt sich um ungeordnete Schlüssel-Wert-Paare.
var myObj = { key1: "Hello", key2: "World" };

// Schlüssel sind Strings, aber es werden keine Anführungszeichen benötigt, 
// sofern es sich um reguläre JavaScript-Bezeichner handelt. Werte können von
// jedem Typ sein.
var myObj = { myKey: "myValue", "my other key": 4 };

// Auf Attribute von Objekten kann ebenfalls mit eckigen Klammern zugegriffen
// werden,
myObj["my other key"]; // = 4

// ... oder in dem man die Punkt-Notation verwendet, vorausgesetzt es handelt 
// sich bei dem Schlüssel um einen validen Bezeichner.
myObj.myKey; // = "myValue"

// Objekte sind veränderlich, Werte können verändert und neue Schlüssel 
// hinzugefügt werden.
myObj.myThirdKey = true;

// Der Zugriff auf einen noch nicht definierten Schlüssel, liefert ein 
// undefined.
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. Logik und Kontrollstrukturen

// Die if-Struktur arbeitet, wie man es erwartet.
var count = 1;
if (count == 3){
    // wird evaluiert, wenn count gleich 3 ist
} else if (count == 4) {
    // wird evaluiert, wenn count gleich 4 ist
} else {
    // wird evaluiert, wenn es weder 3 noch 4 ist
}

// Genauso 'while'.
while (true) {
    // Eine unendliche Schleife!
}

// Do-while-Scheifen arbeiten wie while-Schleifen, abgesehen davon, dass sie 
// immer mindestens einmal ausgeführt werden.
var input;
do {
    input = getInput();
} while ( !isValid( input ) )

// Die for-Schleife arbeitet genau wie in C und Java:
// Initialisierung; Bedingung, unter der die Ausführung fortgesetzt wird; 
// Iteration.
for ( var i = 0; i < 5; i++ ) {
    // wird 5-mal ausgeführt
}

// '&&' ist das logische und, '||' ist das logische oder
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear";
    // Die Größe des Hauses ist groß und die Farbe blau.
}
if (colour == "red" || colour == "blue"){
    // Die Farbe ist entweder rot oder blau.
}

// Die Auswertung von '&&' und '||' erfolgt so, dass abgebrochen wird, wenn die
// Bedingung erfüllt ist (bei oder) oder nicht-erfüllt ist (bei und). Das ist 
// nützlich, um einen Default-Wert zu setzen.
var name = otherName || "default";

// Ein 'switch' Statement prüft Gleichheit mit ===
// ohne ein 'break' nach jedem Fall
// werden auch die Fälle nach dem korrekten aufgerufen
grade = 'B';
switch (grade) {
  case 'A':
    console.log("Great job");
    break;
  case 'B':
    console.log("OK job");
    break;
  case 'C':
    console.log("You can do better");
    break;
  default:
    console.log("Oy vey");
    break;
}

///////////////////////////////////
// 4. Funktionen, Geltungsbereich und Closures

// In JavaScript werden Funktionen mit dem Schlüsselwort 'function' deklariert.
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"

// Vorsicht: der Ausdruck der den Rückgabewert einer Funktion bildet muss
// auf der selben Zeile beginnen auf der auch das 'return' Keyword steht
// Sonst wird hier ein automatisches Semikolon eingefügt und die Funktion
// gibt 'undefined' zurück
function myFunction()
{
    return // <- Hier wird automatisch ein Semikolon eingefügt
    {
        thisIsAn: 'object literal'
    }
}
myFunction(); // = undefined

// In JavaScript sind Funktionen 'Bürger erster Klasse', also können sie wie 
// Variablen verwendet und als Parameter anderen Funktionen übergeben werden 
// - zum Beispiel, um einen 'event handler' zu 'beliefern'.
function myFunction() {
    // wird ausgeführt, nachdem 5 Sekunden vergangen sind
}
setTimeout(myFunction, 5000);

// Funktionen können auch deklariert werden, ohne ihnen einen Namen zuzuweisen.
// Es ist möglich diese anonymen Funktionen direkt als (oder im) Argument 
// einer anderen Funktion zu definieren.
setTimeout(function(){
    // wird ausgeführt, nachdem 5 Sekunden vergangen sind
}, 5000);

// JavaScript hat einen Geltungsbereich, der sich auf Funktionen erstreckt: 
// Funktionen haben ihren eigenen Geltungsbereich, andere Blöcke nicht.
if(true) {
    var i = 5;
}
i; // = 5 - nicht undefined, wie man es von einer Sprache erwarten würde, die 
   // ihren Geltungsbereich nach Blöcken richtet

// Daraus ergibt sich ein bestimmtes Muster für sofort-ausführbare, anonyme 
// Funktionen, die es vermeiden, dass der globale Geltungsbereich von Variablen
// 'verschmutzt' wird.
(function(){
    var temporary = 5;
    // Auf eine Variable im globalen Geltungsbereich kann zugegriffen werden, 
    // sofern sie im globalen Objekt definiert ist (in einem Webbrowser ist 
    // dies immer das 'window'-Objekt, in anderen Umgebungen, bspw. Node.js, 
    // kann das anders aussehen). 
    window.permanent = 10;
})();
temporary; // wirft einen ReferenceError
permanent; // = 10

// Eines der mächtigsten Charakteristika von JavaScript sind Closures. Wird 
// eine Funktion innerhalb einer anderen Funktion definiert, dann hat die 
// innere Funktion Zugriff auf alle Variablen der äußeren Funktion, sogar dann,
// wenn die äußere Funktion beendet wurde.
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!";
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout wird asynchron ausgeführt. Also wird sayHelloInFiveSeconds 
    // sofort verlassen und setTimeout wird die innere Funktion 'im nachhinein' 
    // aufrufen. Dennoch: Weil sayHelloInFiveSeconds eine Hülle um die innere 
    // Funktion bildet, hat die innere Funktion immer noch Zugriff auf die 
    // Variable prompt.
}
sayHelloInFiveSeconds("Adam");  // wird nach 5 Sekunden ein Popup mit der 
                                // Nachricht "Hello, Adam!" öffnen.

///////////////////////////////////
// 5. Mehr über Objekte, Konstruktoren und Prototypen

// Objekte können Funktionen enthalten.
var myObj = {
    myFunc: function(){
        return "Hello world!";
    }
};
myObj.myFunc(); // = "Hello world!"

// Wenn Funktionen aufgerufen werden, die zu einem Objekt gehören, können sie 
// auf das eigene Objekt mit dem Schlüsselwort 'this' zugreifen.
myObj = {
    myString: "Hello world!",
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = "Hello world!"

// Worauf 'this' gesetzt wird, ist davon abhängig, wie die Funktion aufgerufen 
// wird, nicht wo sie definiert wurde. Unsere Funktion wird daher nicht 
// funktionieren, sofern sie außerhalb des Kontextes des Objekts aufgerufen 
// wird.
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// Umgekehrt ist es möglich eine Funktion einem Objekt zuzuweisen und dadurch 
// Zugriff auf den this-Kontext zu erhalten, sogar dann, wenn die Funktion dem
// Objekt nach dessen Definition zugewiesen wird.
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"

// Mit den Methoden 'call' und 'apply' kann der Kontext eines Funktionsaufrufs
// verändert werden

var anotherFunc = function(s){
    return this.myString + s;
}
anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"

// 'apply' funktioniert beiahe identisch, erwartet die übergebenen Argumente
// aber in einem Array

anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!"

// Das ist hilfreich wenn man einer Funktion eine beliebige Zahl Argumente
// übergeben kann

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// 'call' und 'apply' beeinflussen aber nur den spezifischen Aufruf.
// Um den Kontext einer Funktion dauerhaft zu ändern wird 'bind' benutzt.

var boundFunc = anotherFunc.bind(myObj);
boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"

// Mit 'bind' lassen sich Funktionen auch teilweise anwenden / "curryen".
var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// Wenn eine Funktion mit dem Schlüsselwort 'new' aufgerufen wird, dann wird 
// ein neues Objekt erzeugt. Funktionen, die darauf ausgelegt sind in dieser 
// Art aufgerufen zu werden, werden Konstruktoren genannt.
var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// Jedes JavaScript-Objekt hat einen Prototyp. Wenn man versucht auf eine 
// Eigenschaft des Objekts zuzugreifen, das nicht im Objekt selbst existiert,
// schaut der Interpreter in dessen Prototyp nach.

// Einige JavaScript-Implementierungen erlauben den direkten Zugriff auf den 
// Prototyp eines Objekts durch die magische Eigenschaft __proto__. Obwohl das
// nützlich ist, um  Prototypen im Allgemeinen zu erklären, ist das nicht Teil
// des Standards; zum Standard-Weg der Nutzung von Prototypen kommen wir 
// später.
var myObj = {
    myString: "Hello world!",
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};
myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// Das funktioniert auch bei Funktionen.
myObj.myFunc(); // = "hello world!"

// Sollte die Eigenschaft nicht im Prototypen des Objekts enthalten sein, dann
// wird im Prototypen des Prototypen nachgesehen und so weiter.
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

// Dafür wird nichts hin und her kopiert; jedes Objekt speichert eine Referenz
// auf seinen Prototypen. Das heißt wenn der Prototyp geändert wird, dann 
// werden die Änderungen überall sichtbar.
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

// Es wurde bereits erwähnt, dass __proto__ nicht zum Standard gehört und es 
// gibt ebenso keinen Standard-Weg, um den Prototyp eines existierenden Objekts
// zu ändern. Es gibt dennoch zwei Wege, wie man ein neues Objekt mit einem 
// gegebenen Prototypen erzeugt.

// Der erste Weg ist die Methode Object.create, die eine jüngere Ergänzung des
// JavaScript-Standards ist und daher noch nicht in allen Implementierungen 
// verfügbar.
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// Der zweite Weg, der immer funktioniert, hat mit den Konstruktoren zu tun. 
// Konstruktoren haben eine Eigenschaft, die Prototyp heißt.  Dabei handelt es
// sich *nicht* um den Prototypen der Konstruktor-Funktion; stattdessen handelt
// es sich um den Prototypen, der einem neuen Objekt mitgegeben wird, wenn es 
// mit dem Konstruktor und dem Schlüsselwort 'new' erzeugt wird.
MyConstructor.prototype = {
    getMyNumber: function(){
        return this.myNumber
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5

// Alle primitiven Typen, also strings und numbers, haben auch Konstruktoren, 
// die zu dem Typ äquivalente Wrapper-Objekte erzeugen. 
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// Genau genommen: Sie sind nicht exakt äquivalent.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // Dieser Teil wird nicht ausgeführt, weil 0 'falsy' ist.
}

// Das Wrapper-Objekt und die regulären, eingebauten Typen, teilen sich einen 
// Prototyp; so ist es möglich zum Beispiel einem String weitere Funktionen 
// hinzuzufügen.
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// Diese Tatsache wird häufig bei einer Methode mit dem Namen 'polyfilling'
// verwendet: Dabei wird ein neues Feature von JavaScript in einer älteren 
// Untermenge der Sprache integriert, so dass bestimmte Funktionen auch in 
// älteren Umgebungen und Browsern verwendet werden können.

// Ein Beispiel: Es wurde erwähnt, dass die Methode Object.create nicht in 
// allen Umgebungen verfügbar ist - wir können sie dennoch verwenden, mit einem
// 'polyfill':
if (Object.create === undefined){ // überschreib nichts, was eventuell bereits
                                  // existiert
    Object.create = function(proto){
        // erstelle einen vorübergehenden Konstruktor mit dem richtigen
        // Prototypen
        var Constructor = function(){};
        Constructor.prototype = proto;
        // verwende es dann, um ein neues Objekt mit einem passenden 
        // Prototypen zurückzugeben
        return new Constructor();
    }
}
```

## Zur weiteren Lektüre (englisch)

Das [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript) bietet eine ausgezeichnete Dokumentation für die Verwendung von JavaScript im Browser. Es ist außerdem ein Wiki und ermöglicht es damit anderen zu helfen, wenn man selbst ein wenig Wissen angesammelt hat.

MDN's [A re-introduction to JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) führt sehr viele der hier vorgestellten Konzepte im Detail aus. 

Dieses Tutorial hat nur die Sprache JavaScript vorgestellt; um mehr über den  Einsatz in Websites zu lernen, ist es ein guter Start etwas über das [Document Object Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core) zu lernen.

[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) ist eine tiefgehende Einführung in die kontra-intuitiven Parts der Sprache.

[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) ist ein Klassiker unter den Referenzen.

Zusätzlich zu direkten Beiträgen zu diesem Artikel ist der Inhalt in Anlehnung an Louie Dinh's Python-Tutorial auf dieser Seite und das [JS Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) des Mozilla Developer Network entstanden.
---
language: json
filename: learnjson-de.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
  - ["Timm Albers", "https://github.com/nunull"]
lang: de-de
---

Da JSON ein äußerst einfaches Format für den Austausch von Daten ist, wird dieses
Dokument das vermutlich einfachste "Learn X in Y Minutes" werden.

In seiner grundlegenden Form hat JSON keine eigentlichen Kommentare. Dennoch
akzeptieren die meisten Parser Kommentare in C-Syntax (`//`, `/* */`). Dennoch
soll für dieses Dokument nur 100% gültiges JSON verwendet werden, weshalbt keine
Kommentare verwendet werden. Glücklicherweise ist das nachfolgende Dokument
selbsterklärend.

```json
{
  "schlüssel": "wert",
  
  "alle schlüssel": "müssen durch doppelte Anführungszeichen begrenzt werden",
  "zahlen": 0,
  "zeichenketten": "Alle Unicode-Zeichen (inklusive \"escaping\") sind erlaubt.",
  "boolesche werte": true,
  "nullwert": null,

  "große zahlen": 1.2e+100,

  "objekte": {
    "kommentar": "Die meisten Datenstrukturen in JSON kommen aus Objekten.",

    "array": [0, 1, 2, "Arrays können Werte jeglichen Datentyps aufnehmen.", 4],

    "weiteres objekt": {
      "kommentar": "Objekte können verschachtelt werden."
    }
  },

  "quatsch": [
    {
      "quellen von kalium": ["Bananen"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "Neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "alternative formatierung": {
    "kommentar": "..."
  , "die position": "des Kommas ist nicht relevant - so lange es vor dem Wert steht."
  , "weiterer kommentar": "wie schön"
  , "übrigens": "Auch die Einrückung ist nicht relevant." 
  , "jede": "beliebige Anzahl von Leerzeichen / Tabs ist erlaubt.", "wirklich?":true
  },

  "das war kurz": "Und, du bist fertig. Du weißt nun (fast) alles über JSON."
}
```
---
language: latex
contributors:
    - ["Chaitanya Krishna Ande", "http://icymist.github.io"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
    - ["Sricharan Chiruvolu", "http://sricharan.xyz"]
translators:
  - ["Moritz Kammerer", "https://github.com/phxql"]
  - ["Jerome Meinke", "https://github.com/jmeinke"]
lang: de-de
filename: latex-de.tex
---
```
% Alle Kommentare starten mit einem Prozentzeichen %

% LaTeX ist keine "What You See Is What You Get" Textverarbeitungssoftware wie z.B.
% MS Word oder OpenOffice Writer

% Jedes LaTeX-Kommando startet mit einem Backslash (\)

% LaTeX-Dokumente starten immer mit der Definition des Dokuments, die sie darstellen
% Weitere Dokumententypen sind z.B. book, report, presentations, etc.
% Optionen des Dokuments stehen zwischen den eckigen Klammern []. In diesem Fall
% wollen wir einen 12 Punkte-Font verwenden.
\documentclass[12pt]{article}

% Als nächstes definieren wir die Pakete, die wir verwenden wollen.
% Wenn du z.B. Grafiken, farbigen Text oder Quelltext in dein Dokument einbetten möchtest,
% musst du die Fähigkeiten von LaTeX durch Hinzufügen von Paketen erweitern.
% Wir verwenden die Pakete float und caption für Bilder.
\usepackage{caption}
\usepackage{float}

% Mit diesem Paket können leichter Umlaute getippt werden
\usepackage[utf8]{inputenc}

% Es gibt eigentlich keine Kommentare über mehrere Zeilen, solche kann man
% aber selbst durch die Angabe eigener Kommandos definieren.
% Dieses Kommando kann man später benutzen.
\newcommand{\comment}[1]{}

% Es können durchaus noch weitere Optione für das Dokument gesetzt werden!
\author{Chaitanya Krishna Ande, Colton Kohnke \& Sricharan Chiruvolu}
\date{\today}
\title{Learn \LaTeX\ in Y Minutes!}

% Nun kann's losgehen mit unserem Dokument.
% Alles vor dieser Zeile wird die Preamble genannt.
\begin{document}

\comment{
  Dies ist unser selbst-definierter Befehl
  für mehrzeilige Kommentare.
}

% Wenn wir den Autor, das Datum und den Titel gesetzt haben, kann
% LaTeX für uns eine Titelseite generieren
\maketitle

% Die meisten Paper haben ein Abstract. LaTeX bietet dafür einen vorgefertigen Befehl an.
% Das Abstract sollte in der logischen Reihenfolge, also nach dem Titel, aber vor dem
% Inhalt erscheinen.
% Dieser Befehl ist in den Dokumentenklassen article und report verfügbar.
\begin{abstract}
 \LaTeX -Documentation geschrieben in \LaTeX ! Wie ungewöhnlich und garantiert nicht meine Idee!
\end{abstract}

% Section Befehle sind intuitiv.
% Alle Titel der sections werden automatisch in das Inhaltsverzeichnis übernommen.
\section{Einleitung}
Hi, mein Name ist Moritz und zusammen werden wir \LaTeX\ erforschen!

\section{Noch eine section}
Das hier ist der Text für noch eine section. Ich glaube, wir brauchen eine subsection.

\subsection{Das ist eine subsection} % Subsections sind auch ziemlich intuitiv.
Ich glaube, wir brauchen noch eine.

\subsubsection{Pythagoras}
So ist's schon viel besser.
\label{subsec:pythagoras}

% Wenn wir den Stern nach section schreiben, dann unterdrückt LateX die Nummerierung.
% Das funktioniert auch bei anderen Befehlen.
\section*{Das ist eine unnummerierte section}
Es müssen nicht alle Sections nummeriert sein!

\section{Ein paar Notizen}
\LaTeX\ ist ziemlich gut darin, Text so zu platzieren, dass es gut aussieht.
Falls eine Zeile \\ mal \\ woanders \\ umgebrochen \\ werden \\ soll, füge
\textbackslash\textbackslash in den Code ein.\\

\section{Listen}
Listen sind eine der einfachsten Dinge in \LaTeX. Ich muss morgen einkaufen gehen,
also lass uns eine Einkaufsliste schreiben:
\begin{enumerate} % Dieser Befehl erstellt eine "enumerate" Umgebung.
  % \item bringt enumerate dazu, eins weiterzuzählen.
  \item Salat.
  \item 27 Wassermelonen.
  \item einen Hasen.
  % Wir können die Nummer des Eintrags durch [] überschreiben
  \item[Wie viele?] Mittelgroße Wasserpistolen.

  Kein Listeneintrag, aber immer noch Teil von enumerate.

\end{enumerate} % Alle Umgebungen müssen ein end haben.

\section{Mathe}

Einer der Haupteinsatzzwecke von \LaTeX\ ist das Schreiben von akademischen
Artikeln oder Papern. Meistens stammen diese aus dem Bereich der Mathe oder
anderen Wissenschaften. Und deswegen müssen wir in der Lage sein, spezielle
Symbole zu unserem Paper hinzuzufügen! \\

Mathe kennt sehr viele Symbole, viel mehr als auf einer Tastatur zu finden sind;
Symbole für Mengen und relationen, Pfeile, Operatoren und Griechische Buchstaben,
um nur ein paar zu nennen.\\

Mengen und Relationen spielen eine sehr wichtige Rolle in vielen mathematischen
Papern. So schreibt man in \LaTeX, dass alle y zu X gehören: $\forall$ y $\in$ X. \\

% Achte auf die $ Zeichen vor und nach den Symbolen. Wenn wir in LaTeX schreiben,
% geschieht dies standardmäßig im Textmodus. Die Mathe-Symbole existieren allerdings
% nur im Mathe-Modus. Wir können den Mathe-Modus durch das $ Zeichen aktivieren und
% ihn mit $ wieder verlassen. Variablen können auch im Mathe-Modus angezeigt werden.

Mein Lieblingsbuchstabe im Griechischen ist $\xi$. Ich mag auch $\beta$, $\gamma$ und $\sigma$.
Bis jetzt habe ich noch keinen griechischen Buchstaben gefunden, den \LaTeX nicht kennt!

Operatoren sind ebenfalls wichtige Bestandteile von mathematischen Dokumenten:
Trigonometrische Funktionen ($\sin$, $\cos$, $\tan$),
Logarithmus und Exponenten ($\log$, $\exp$),
Grenzwerte ($\lim$), etc. haben vordefinierte Befehle.
Lass uns eine Gleichung schreiben: \\

$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$\\

Brüche (Zähler / Nenner) können so geschrieben werden:

% 10 / 7
$^{10}/_{7}$

% Komplexere Brüche können so geschrieben werden:
% \frac{Zähler}{Nenner}
$\frac{n!}{k!(n - k)!}$ \\

Wir können Gleichungen auch in einer equation Umgebung verwenden.

% Dies zeigt Mathe in einer equation Umgebung an
\begin{equation} % Aktiviert automatisch den Mathe-Modus.
    c^2 = a^2 + b^2.
    \label{eq:pythagoras} % Pythagoras referenzieren
\end{equation} % Alle \begin Befehle müssen einen \end Befehl besitzen

Wir können nun unsere Gleichung referenzieren!
Gleichung ~\ref{eq:pythagoras} ist auch als das Theorem des Pythagoras bekannt. Dieses wird in
Abschnitt ~\ref{subsec:pythagoras} behandelt. Es können sehr viele Sachen mit Labels versehen werden:
Grafiken, Gleichungen, Sections, etc.

Summen und Integrale können mit den sum und int Befehlen dargestellt werden:

% Manche LaTeX-Compiler beschweren sich, wenn Leerzeilen in Gleichungen auftauchen
\begin{equation}
  \sum_{i=0}^{5} f_{i}
\end{equation}
\begin{equation}
  \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x
\end{equation}

\section{Grafiken}

Lass uns eine Grafik einfügen. Das Platzieren von Grafiken kann etwas trickreich sein.
Aber keine Sorge, ich muss auch jedes mal nachschauen, welche Option wie wirkt.

\begin{figure}[H] % H ist die Platzierungsoption
    \centering % Zentriert die Grafik auf der Seite
    % Fügt eine Grafik ein, die auf 80% der Seitenbreite einnimmt.
    %\includegraphics[width=0.8\linewidth]{right-triangle.png}
    % Auskommentiert, damit es nicht im Dokument auftaucht.
    \caption{Dreieck mit den Seiten $a$, $b$, $c$}
    \label{fig:right-triangle}
\end{figure}

\subsection{Tabellen}
Wir können Tabellen genauso wie Grafiken einfügen.

\begin{table}[H]
  \caption{Überschrift der Tabelle.}
  % Die {} Argumente geben an, wie eine Zeile der Tabelle dargestellt werden soll.
  % Auch hier muss ich jedes Mal nachschauen. Jedes. einzelne. Mal.  
  \begin{tabular}{c|cc}
    Nummer &  Nachname & Vorname \\ % Spalten werden durch & getrennt
    \hline % Eine horizontale Linie
    1 & Biggus & Dickus \\
    2 & Monty & Python
  \end{tabular}
\end{table}

% \section{Links} % Kommen bald!

\section{Verhindern, dass \LaTeX\ etwas kompiliert (z.B. Quelltext)}
Angenommen, wir wollen Quelltext in unserem \LaTeX-Dokument. \LaTeX\ soll
in diesem Fall nicht den Quelltext als \LaTeX-Kommandos interpretieren,
sondern es einfach ins Dokument schreiben. Um das hinzubekommen, verwenden
wir eine verbatim Umgebung.

% Es gibt noch weitere Pakete für Quelltexte (z.B. minty, lstlisting, etc.)
% aber verbatim ist das simpelste.
\begin{verbatim}
  print("Hello World!")
  a%b; % Schau dir das an! Wir können % im verbatim verwenden!
  random = 4; #decided by fair random dice roll
\end{verbatim}

\section{Kompilieren}

Ich vermute, du wunderst dich, wie du dieses tolle Dokument in ein PDF
verwandeln kannst. (Ja, dieses Dokument kompiliert wirklich!) \\

Dafür musst du folgende Schritte durchführen:
  \begin{enumerate}
    \item Schreibe das Dokument. (den \LaTeX -Quelltext).
    \item Kompiliere den Quelltext in ein PDF.
     Das Kompilieren sieht so ähnlich wie das hier aus (Linux): \\
     \begin{verbatim}
        $pdflatex learn-latex.tex learn-latex.pdf
     \end{verbatim}
  \end{enumerate}

Manche \LaTeX-Editoren kombinieren Schritt 1 und 2. Du siehst also nur Schritt 1 und Schritt
2 wird unsichtbar im Hintergrund ausgeführt.

Alle Formatierungsoptionen werden in Schritt 1 in den Quelltext geschrieben. Schritt 2 verwendet
dann diese Informationen und kümmert sich drum, dass das Dokument korrekt erstellt wird.

\section{Ende}

Das war's erst mal!

% Dokument beenden
\end{document}
```
## Mehr Informationen über LateX

* Das tolle LaTeX wikibook: [https://de.wikibooks.org/wiki/LaTeX-Kompendium](https://de.wikibooks.org/wiki/LaTeX-Kompendium)
* Ein Tutorial (englisch): [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/)
---
language: Lua
contributors:
    - ["Tyler Neylon", "http://tylerneylon.com/"]
translators:
    - ["Martin Schimandl", "https://github.com/Git-Jiro"]
filename: learnlua-de.lua
lang: de-de
---

```lua
-- Zwei Gedankenstriche starten ein einzeiliges Kommentar.

--[[
     Fügt man zwei '[' und ']' hinzu,
     erzeugt man einen mehrzeiligen Kommentar.
--]]
--------------------------------------------------------------------------------
-- 1. Variablen und Fluß-Kontrolle.
--------------------------------------------------------------------------------

num = 42  -- Alle Nummern sind vom Typ: Double.
-- Werd nicht nervös, 64-Bit Double haben 52 Bits zum Speichern von exakten
-- Ganzzahlen; Maschinen-Genauigkeit ist kein Problem für Ganzzahlen kleiner als 
-- 52 Bit.

s = 'walternate'  -- Zeichenketten sind unveränderlich, wie bei Python.
t = "Doppelte Anführungszeichen sind auch OK"
u = [[ Doppelte eckige Klammern
       beginnen und beenden
       mehrzeilige Zeichenketten.]]
t = nil  -- Undefineren von t; Lua hat einen Garbage Collection.

-- Blöcke werden durch Schlüsselwörter wie do/end markiert:
while num < 50 do
  num = num + 1  -- Es gibt Keine Operatoren wie ++ oder +=
end

-- If Bedingungen:
if num > 40 then
  print('over 40')
elseif s ~= 'walternate' then  -- ~= bedeutet ungleich
  -- Gleichheits-Check == wie bei Python; OK für Zeichenketten.
  io.write('not over 40\n')  -- Standard ist stdout.
else
  -- Variablen sind standardmäßig global.
  thisIsGlobal = 5  -- Camel case ist üblich.

  -- So macht man eine Variable lokal:
  local line = io.read()  -- Lies die nächste Zeile von stdin.

  -- Zeichenketten zusammenführen mit dem .. Operator:
  print('Winter is coming, ' .. line)
end

-- Undefinierte Variablen geben nil zurück.
-- Das ist kein Fehler:
foo = anUnknownVariable  -- Nun ist foo = nil.

aBoolValue = false

-- Nur nil und false sind unwahr; 0 and '' sind wahr!
if not aBoolValue then print('was false') end

-- 'or' und 'and' sind "kurz-geschlossen". Das ist so ähnlich wie der a?b:c
-- operator in C/js:
-- in C/js:
ans = aBoolValue and 'yes' or 'no'  --> 'no'

karlSum = 0
for i = 1, 100 do  -- Ein Bereich inkludiert beide Enden.
  karlSum = karlSum + i
end

-- Verwende "100, 1, -1" als Breich für Countdowns:
fredSum = 0
for j = 100, 1, -1 do fredSum = fredSum + j end

-- Im Allgemeinen besteht ein Bereich aus: Anfang, Ende, [, Schrittweite].

-- Ein anderes Schleifen-Konstrukt:
repeat
  print('Der Weg der Zukunft')
  num = num - 1
until num == 0

--------------------------------------------------------------------------------
-- 2. Funktionen.
--------------------------------------------------------------------------------

function fib(n)
  if n < 2 then return n end
  return fib(n - 2) + fib(n - 1)
end

-- Closures und anonyme Funktionen sind ok:
function adder(x)
  -- Die zurückgegebene Funktion wird erzeugt wenn addr aufgerufen wird und merkt
  -- sich den Wert von x:
  return function (y) return x + y end
end
a1 = adder(9)
a2 = adder(36)
print(a1(16))  --> 25
print(a2(64))  --> 100

-- Rückgabewerte, Funktions-Aufrufe und Zuweisungen funktionieren alle mit
-- Listen die nicht immer gleich lang sein müssen. Überzählige Empfänger
-- bekommen nil; überzählige Sender werden ignoriert.

x, y, z = 1, 2, 3, 4
-- Nun ist x = 1, y = 2, z = 3, und 4 wird ignoriert.

function bar(a, b, c)
  print(a, b, c)
  return 4, 8, 15, 16, 23, 42
end

x, y = bar('zaphod')  --> prints "zaphod  nil nil"
-- Nun ist x = 4, y = 8, die Werte 15..42 werden ignoriert.

-- Funktionen sind erste Klasse, und können lokal oder global sein.
-- Das ist alles das Gleiche:
function f(x) return x * x end
f = function (x) return x * x end

-- Das auch:
local function g(x) return math.sin(x) end
local g = function(x) return math.sin(x) end
-- Äquivalent zu local function g(x)..., außer das Referenzen auf g im
-- Funktions-Körper nicht wie erwartet funktionieren.
local g; g  = function (x) return math.sin(x) end
-- Die Deklaration 'local g' macht Selbst-Referenzen auf g OK.

-- Nebenbei gesagt, Trigonometrie-Funktionen verwenden Radianten.

-- Funktionsaufrufe mit nur einem Zeichenketten-Parameter brauch keine runden
-- Klammern.
print 'hello'  -- Funktioniert wunderbar.

-- Funktionsaufrufe mit einem Tabellen-Parameter brauchen auch keine runden
-- Klammern. Mehr zu Tabellen kommt später.
print {} -- Funktioniert auch wunderbar.

--------------------------------------------------------------------------------
-- 3. Tabellen.
--------------------------------------------------------------------------------

-- Tabellen sind die einzige zusammengesetzte Struktur in Lua. Sie sind
-- assoziative Arrays. Sie sind so ähnlich wie PHP arrays oder JavaScript
-- Objekte. Sie sind Hash-Lookup-Dictionaries die auch als Listen verwendet
-- werden können.

-- Verwenden von Tabellen als Dictionaries oder Maps:

-- Dict-Literale haben standardmäßig Zeichenketten als Schlüssel:
t = {key1 = 'value1', key2 = false}

-- Zeichenketten-Schlüssel verwenden eine JavaScript ähnliche Punkt-Notation.
print(t.key1)  -- Ausgabe 'value1'.
t.newKey = {}  -- Neues Schlüssel/Wert-Paar hinzufügen.
t.key2 = nil   -- key2 aus der Tabelle entfernen.

-- Literale notation für jeden (nicht-nil) Wert als Schlüssel:
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28])  -- Ausgabe "tau"

-- Schlüssel-Vergleiche funktionieren per Wert für Nummern und Zeichenketten,
-- aber über die Identität bei Tabellen.
a = u['@!#']  -- Nun ist a = 'qbert'.
b = u[{}]     -- Wir würden 1729 erwarten, aber es ist nil:
-- b = nil weil der Lookup fehlschlägt. Er schlägt Fehl, weil der Schlüssel
-- den wir verwendet haben nicht das gleiche Objekt ist das wir verwendet
-- haben um den original Wert zu speichern. Zahlen und Zeichnkette sind daher
-- die praktischeren Schlüssel.

-- Eine Funktion mit nur einem Tabellen-Parameter benötigt keine Klammern.
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'}  -- Ausgabe 'Sonmi~451'.

for key, val in pairs(u) do  -- Tabellen-Iteration.
  print(key, val)
end

-- _G ist eine spezielle Tabelle die alles Globale enthält.
print(_G['_G'] == _G)  -- Ausgabe 'true'.

-- Verwenden von Tabellen als Listen/Arrays:

-- Listen-Literale verwenden implizit Ganzzahlen als Schlüssel:
v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do  -- #v ist die Größe von v für Listen.
  print(v[i])  -- Indices beginnen mit 1 !! SO VERRÜCKT!
end
-- Eine 'Liste' ist kein echter Typ. v ist nur eine Tabelle mit fortlaufenden
-- Ganzzahlen als Schlüssel, die behandelt wird wie eine Liste.

--------------------------------------------------------------------------------
-- 3.1 Metatabellen und Metamethoden
--------------------------------------------------------------------------------

-- Eine Tabelle kann eine Metatabelle haben. Diese verleiht ihr so etwas wie
-- Tabellen-Operator-Überladungs-Verhalten. Später sehen wir wie
-- Metatabellen js-prototypen artiges Verhalten unterstützen.

f1 = {a = 1, b = 2}  -- Repräsentiert den Bruch a/b.
f2 = {a = 2, b = 3}

-- Dies würde Fehlschlagen:
-- s = f1 + f2

metafraction = {}
function metafraction.__add(f1, f2)
  local sum = {}
  sum.b = f1.b * f2.b
  sum.a = f1.a * f2.b + f2.a * f1.b
  return sum
end

setmetatable(f1, metafraction)
setmetatable(f2, metafraction)

s = f1 + f2  -- Rufe __add(f1, f2) vom der Metatabelle von f1 auf.

-- f1 und f2 haben keine Schlüssel für ihre Metatabellen, anders als bei js
-- Prototypen. Daher muss mithilfe von getmetatable(f1) darauf zugegriffen
-- werden. Eine Metatabelle ist wie eine normale Tabelle mit Schlüsseln die
-- Lua bekannt sind, so wie __add.


-- Die nächste Zeile schlägt fehl weil s keine Metatabelle hat:
-- t = s + s
-- Mihilfe von Klassen ähnlichen Mustern kann das gelöst werden.
-- Siehe weiter unten.

-- Ein __index einer Metatabelle überlädt Punkt-Lookups:
defaultFavs = {animal = 'gru', food = 'donuts'}
myFavs = {food = 'pizza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal  -- Funktioniert dank Metatabelle!

--------------------------------------------------------------------------------
-- Direkte Tabellen-Lookups die fehlschlagen werden mithilfe von __index der
-- Metatabelle wiederholt. Das geschieht rekursiv.

-- __index kann auch eine Funktion mit der Form function(tbl, key) sein.
-- Damit kann man Lookups weiter anpassen.

-- Werte wie __index,add, .. werden Metamethoden genannt.
-- HIer eine vollständige Liste aller Metamethoden.

-- __add(a, b)                     für a + b
-- __sub(a, b)                     für a - b
-- __mul(a, b)                     für a * b
-- __div(a, b)                     für a / b
-- __mod(a, b)                     für a % b
-- __pow(a, b)                     für a ^ b
-- __unm(a)                        für -a
-- __concat(a, b)                  für a .. b
-- __len(a)                        für #a
-- __eq(a, b)                      für a == b
-- __lt(a, b)                      für a < b
-- __le(a, b)                      für a <= b
-- __index(a, b)  <fn or a table>  für a.b
-- __newindex(a, b, c)             für a.b = c
-- __call(a, ...)                  für a(...)

--------------------------------------------------------------------------------
-- 3.2 Klassen-Artige Tabellen und Vererbung.
--------------------------------------------------------------------------------

-- Klassen sind in Lua nicht eingebaut. Es gibt verschieden Wege sie mithilfe
-- von Tabellen und Metatabellen zu erzeugen.

-- Die Erklärund des Beispiels erfolgt unterhalb.

Dog = {}                                   -- 1.

function Dog:new()                         -- 2.
  local newObj = {sound = 'woof'}          -- 3.
  self.__index = self                      -- 4.
  return setmetatable(newObj, self)        -- 5.
end

function Dog:makeSound()                   -- 6.
  print('I say ' .. self.sound)
end

mrDog = Dog:new()                          -- 7.
mrDog:makeSound()  -- 'I say woof'         -- 8.

-- 1. Dog verhält sich wie eine Klasse; Ist aber eine Tabelle.
-- 2. "function tablename:fn(...)" ist das gleiche wie
--    "function tablename.fn(self, ...)", Der : fügt nur ein Argument namens
--    self hinzu. Siehe 7 & 8 um zu sehen wie self seinen Wert bekommt.
-- 3. newObj wird eine Instanz von Dog.
-- 4. "self" ist die zu Instanzierende Klasse. Meistern ist self = Dog, aber
--    dies kann durch Vererbung geändert werden. newObj bekommt die Funktionen
--    von self wenn wir die Metatabelle von newObj und __index von self auf
--    self setzen.
-- 5. Zur Erinnerung: setmetatable gibt sein erstes Argument zurück.
-- 6. Der Doppelpunkt funktioniert wie bei 2, aber dieses Mal erwarten wir das
--    self eine Instanz ist und keine Klasse.
-- 7. Das Selbe wie Dog.new(Dog), also self = Dog in new().
-- 8. Das Selbe wie mrDog.makeSound(mrDog); self = mrDog.

--------------------------------------------------------------------------------

-- Vererbungs-Beispiel:

LoudDog = Dog:new()                           -- 1.

function LoudDog:makeSound()
  local s = self.sound .. ' '                 -- 2.
  print(s .. s .. s)
end

seymour = LoudDog:new()                       -- 3.
seymour:makeSound()  -- 'woof woof woof'      -- 4.

--------------------------------------------------------------------------------
-- 1. LoudDog bekommt die Methoden und Variablen von Dog.
-- 2. self hat einen 'sound' Schlüssel von new(), siehe 3.
-- 3. Das Gleiche wie "LoudDog.new(LoudDog)", und umgewandelt zu "Dog.new(LoudDog)"
--    denn LoudDog hat keinen 'new' Schlüssel, aber "__index = Dog" steht in der
--    Metatabelle.
--    Ergebnis: Die Metatabelle von seymour ist LoudDog und "LoudDog.__index = Dog".
--    Daher ist seymour.key gleich seymour.key, LoudDog.key, Dog.key, je nachdem
--    welche Tabelle als erstes einen passenden Schlüssel hat.
-- 4. Der 'makeSound' Schlüssel wird in LoudDog gefunden: Das ist das Gleiche
--    wie "LoudDog.makeSound(seymour)".

-- Wenn nötig, sieht new() einer Sub-Klasse genau so aus wie new() der
-- Basis-Klasse:
function LoudDog:new()
  local newObj = {}
  -- set up newObj
  self.__index = self
  return setmetatable(newObj, self)
end

--------------------------------------------------------------------------------
-- 4. Module.
--------------------------------------------------------------------------------


--[[ Dieser Abschnitt ist auskommentiert damit der Rest des Skripts lauffähig
--   bleibt.
```

```lua
-- Angenommen mod.lua sieht so aus:
local M = {}

local function sayMyName()
  print('Hrunkner')
end

function M.sayHello()
  print('Why hello there')
  sayMyName()
end

return M

-- Eine andere Datei könnte die Funktionen in mod.lua so verwenden:
local mod = require('mod')  -- Führe mod.lua aus.

-- require ist der Standard-Weg um Module zu inkludieren.
-- require verhält sich wie: (Wenn nicht gecached wird; siehe später)
local mod = (function ()
  <Inhalt von mod.lua>
end)()
-- Es ist als ob mod.lua eine Funktion wäre, sodass lokale Variablen in
-- mod.lua ausserhalb unsichtbar sind.

-- Das funktioniert weil mod hier das Gleiche wie M in mod.lua ist:
mod.sayHello()  -- Says hello to Hrunkner.

-- Das ist Falsch: sayMyName existiert nur in mod.lua:
mod.sayMyName()  -- Fehler

-- Der Rückgabe-Wert von require wird zwischengespeichert. Sodass Module nur
-- einmal abgearbeitet werden, auch wenn sie mit require öfters eingebunden
-- werden.

-- Nehmen wir an mod2.lua enthält "print('Hi!')".
local a = require('mod2')  -- Ausgabe Hi!
local b = require('mod2')  -- Keine Ausgabe; a=b.

-- dofile ist wie require aber ohne Zwischenspeichern.
dofile('mod2')  --> Hi!
dofile('mod2')  --> Hi! (läuft nochmal, nicht wie require)

-- loadfile ladet eine lua Datei aber die Datei wird noch nicht abgearbeitet.
f = loadfile('mod2')  -- Sobald f() aufgerufen wird läuft mod2.lua.

-- loadstring ist loadfile für Zeichenketten
g = loadstring('print(343)')  -- Gibt eine Funktion zurück..
g()  -- Ausgabe 343; Vorher kam keine Ausgabe.

--]]

```
## Referenzen

Ich war so begeistert Lua zu lernen, damit ich Spiele mit <a href="http://love2d.org/">Love 2D game engine</a> programmieren konnte.

Ich habe angefangen mit <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">BlackBulletIV's Lua for programmers</a>.
Danach habe ich das offizielle Lua Buch gelesen: <a href="http://www.lua.org/pil/contents.html">Programming in Lua</a>

Es kann auch hilfreich sein hier vorbeizuschauen: <a href="http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">Lua short
reference</a>

Wichtige Themen die hier nicht angesprochen wurden; die Standard-Bibliotheken:

* <a href="http://lua-users.org/wiki/StringLibraryTutorial">string library</a>
* <a href="http://lua-users.org/wiki/TableLibraryTutorial">table library</a>
* <a href="http://lua-users.org/wiki/MathLibraryTutorial">math library</a>
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a>
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a>

Übrigends, die gesamte Datei ist gültiges Lua. Speichere sie als learn.lua und
starte sie als "lua learn.lua" !

Die Erstfassung ist von tylerneylon.com, und ist auch hier verfügbar: <a href="https://gist.github.com/tylerneylon/5853042">github gist</a>. Viel Spaß mit Lua!
---
language: make
contributors:
    - ["Robert Steed", "https://github.com/robochat"]
translators:
  - ["Martin Schimandl", "https://github.com/Git-Jiro"]
filename: Makefile-de
lang: de-de
---

Eine Makefile definiert einen Graphen von Regeln um ein Ziel (oder Ziele)
zu erzeugen. Es dient dazu die geringste Menge an Arbeit zu verrichten um
ein Ziel in einklang mit dem Quellcode zu bringen. Make wurde berühmterweise
von Stuart Feldman 1976 übers Wochenende geschrieben. Make ist noch immer
sehr verbreitet (vorallem im Unix umfeld) obwohl es bereits sehr viel
Konkurrenz und Kritik zu Make gibt.

Es gibt eine vielzahl an Varianten von Make, dieser Artikel beschäftig sich
mit der Version GNU Make. Diese Version ist standard auf Linux.

```make

# Kommentare können so geschrieben werden.

# Dateien sollten Makefile heißen, denn dann können sie als `make <ziel>`
# aufgerufen werden. Ansonsten muss `make -f "dateiname" <ziel>` verwendet
# werden.

# Warnung - Es sollten nur TABULATOREN zur Einrückung im Makefile verwendet
# werden. Niemals Leerzeichen!

#-----------------------------------------------------------------------
# Grundlagen
#-----------------------------------------------------------------------

# Eine Regel - Diese Regel wird nur abgearbeitet wenn die Datei file0.txt
# nicht existiert.
file0.txt:
	echo "foo" > file0.txt
	# Selbst Kommentare in der 'Rezept' Sektion werden an die Shell
	# weitergegeben. Versuche `make file0.txt` oder einfach `make`
	# die erste Regel ist die Standard-Regel.


# Diese Regel wird nur abgearbeitet wenn file0.txt aktueller als file1.txt ist.
file1.txt: file0.txt
	cat file0.txt > file1.txt
	# Verwende die selben Quoting-Regeln wie die Shell
	@cat file0.txt >> file1.txt
	# @ unterdrückt die Ausgabe des Befehls an stdout.
	-@echo 'hello'
	# - bedeutet das Make die Abarbeitung fortsetzt auch wenn Fehler passieren.
	# Versuche `make file1.txt` auf der Kommandozeile.

# Eine Regel kann mehrere Ziele und mehrere Voraussetzungen haben.
file2.txt file3.txt: file0.txt file1.txt
	touch file2.txt
	touch file3.txt

# Make wird sich beschweren wenn es mehrere Rezepte für die gleiche Regel gibt.
# Leere Rezepte zählen nicht und können dazu verwendet werden weitere 
# Voraussetzungen hinzuzufügen.

#-----------------------------------------------------------------------
# Phony-Ziele
#-----------------------------------------------------------------------

# Ein Phony-Ziel ist ein Ziel das keine Datei ist.
# Es wird nie aktuell sein, daher wird Make immer versuchen es abzuarbeiten
all: maker process

# Es ist erlaubt Dinge ausserhalb der Reihenfolge zu deklarieren.
maker:
	touch ex0.txt ex1.txt

# Um das Fehlschlagen von Phony-Regeln zu vermeiden wenn eine echte Datei den
# selben namen wie ein Phony-Ziel hat:
.PHONY: all maker process
# Das ist ein spezielles Ziel. Es gibt noch ein paar mehr davon.

# Eine Regel mit einem Phony-Ziel als Voraussetzung wird immer abgearbeitet
ex0.txt ex1.txt: maker

# Häufige Phony-Ziele sind: all make clean install ...

#-----------------------------------------------------------------------
# Automatische Variablen & Wildcards
#-----------------------------------------------------------------------

process: file*.txt	# Eine Wildcard um Dateinamen zu Vergleichen
	@echo $^	# $^ ist eine Variable die eine Liste aller
			# Voraussetzungen enthält.
	@echo $@	# Namen des Ziels ausgeben.
	#(Bei mehreren Ziel-Regeln enthält $@ den Verursacher der Abarbeitung
	#der Regel.)
	@echo $<	# Die erste Voraussetzung aus der Liste
	@echo $?	# Nur die Voraussetzungen die nicht aktuell sind.
	@echo $+	# Alle Voraussetzungen inklusive Duplikate (nicht wie Üblich)
	#@echo $|	# Alle 'order only' Voraussetzungen

# Selbst wenn wir die Voraussetzungen der Regel aufteilen, $^ wird sie finden.
process: ex1.txt file0.txt
# ex1.txt wird gefunden werden, aber file0.txt wird dedupliziert.

#-----------------------------------------------------------------------
# Muster
#-----------------------------------------------------------------------

# Mit Mustern kann man make beibringen wie Dateien in andere Dateien
# umgewandelt werden.

%.png: %.svg
	inkscape --export-png $^

# Muster-Vergleichs-Regeln werden nur abgearbeitet wenn make entscheidet das Ziel zu
# erzeugen

# Verzeichnis-Pfade werden normalerweise bei Muster-Vergleichs-Regeln ignoriert.
# Aber make wird versuchen die am besten passende Regel zu verwenden.
small/%.png: %.svg
	inkscape --export-png --export-dpi 30 $^

# Make wird die letzte Version einer Muster-Vergleichs-Regel verwenden die es
# findet.
%.png: %.svg
	@echo this rule is chosen

# Allerdings wird make die erste Muster-Vergleicher-Regel verwenden die das
# Ziel erzeugen kann.
%.png: %.ps
	@echo this rule is not chosen if *.svg and *.ps are both present

# Make hat bereits ein paar eingebaute Muster-Vergleichs-Regelen. Zum Beispiel
# weiß Make wie man aus *.c Dateien *.o Dateien erzeugt.

# Ältere Versionen von Make verwenden möglicherweise Suffix-Regeln anstatt
# Muster-Vergleichs-Regeln.
.png.ps:
	@echo this rule is similar to a pattern rule.

# Aktivieren der Suffix-Regel
.SUFFIXES: .png

#-----------------------------------------------------------------------
# Variablen
#-----------------------------------------------------------------------
# auch Makros genannt.

# Variablen sind im Grunde genommen Zeichenketten-Typen.

name = Ted
name2="Sarah"

echo:
	@echo $(name)
	@echo ${name2}
	@echo $name    # Das funktioniert nicht, wird als $(n)ame behandelt.
	@echo $(name3) # Unbekannte Variablen werden als leere Zeichenketten behandelt.

# Es git 4 Stellen um Variablen zu setzen.
# In Reihenfolge der Priorität von höchster zu niedrigster:
# 1: Befehls-Zeilen Argumente
# 2: Makefile
# 3: Shell Umbebungs-Variablen - Make importiert diese automatisch.
# 3: MAke hat einige vordefinierte Variablen.

name4 ?= Jean
# Setze die Variable nur wenn es eine gleichnamige Umgebungs-Variable noch
# nicht gibt.

override name5 = David
# Verhindert das Kommando-Zeilen Argumente diese Variable ändern können.

name4 +=grey
# Werte an eine Variable anhängen (inkludiert Leerzeichen).

# Muster-Spezifische Variablen Werte (GNU Erweiterung).
echo: name2 = Sara # Wahr innerhalb der passenden Regel und auch innerhalb
	# rekursiver Voraussetzungen (ausser wenn es den Graphen zerstören
	# kann wenn es zu kompilizert wird!)

# Ein paar Variablen die von Make automatisch definiert werden.
echo_inbuilt:
	echo $(CC)
	echo ${CXX)}
	echo $(FC)
	echo ${CFLAGS)}
	echo $(CPPFLAGS)
	echo ${CXXFLAGS}
	echo $(LDFLAGS)
	echo ${LDLIBS}

#-----------------------------------------------------------------------
# Variablen 2
#-----------------------------------------------------------------------

# Der erste Typ von Variablen wird bei jeder verwendung ausgewertet.
# Das kann aufwendig sein, daher exisitert ein zweiter Typ von Variablen.
# Diese werden nur einmal ausgewertet. (Das ist eine GNU make Erweiterung)

var := hello
var2 ::=  $(var) hello
#:= und ::= sind äquivalent.

# Diese Variablen werden prozedural ausgwertet (in der Reihenfolge in der sie
# auftauchen), die stehen daher im wiederspruch zum Rest der Sprache!

# Das funktioniert nicht
var3 ::= $(var4) and good luck
var4 ::= good night

#-----------------------------------------------------------------------
# Funktionen
#-----------------------------------------------------------------------

# Make verfügt über eine vielzahl von Funktionen.

sourcefiles = $(wildcard *.c */*.c)
objectfiles = $(patsubst %.c,%.o,$(sourcefiles))

# Das Format ist $(func arg0,arg1,arg2...)

# Ein paar Beispiele
ls:	* src/*
	@echo $(filter %.txt, $^)
	@echo $(notdir $^)
	@echo $(join $(dir $^),$(notdir $^))

#-----------------------------------------------------------------------
# Direktiven
#-----------------------------------------------------------------------

# Inkludiere andere Makefile, sehr praktisch für platformspezifischen Code
include foo.mk

sport = tennis
# Konditionale kompiliereung
report:
ifeq ($(sport),tennis)
	@echo 'game, set, match'
else
	@echo "They think it's all over; it is now"
endif

# Es gibt auch ifneq, ifdef, ifndef

foo = true

ifdef $(foo)
bar = 'hello'
endif
```


### Mehr Resourcen

+ [gnu make documentation](https://www.gnu.org/software/make/manual/)
+ [software carpentry tutorial](http://swcarpentry.github.io/make-novice/)
+ learn C the hard way [ex2](http://c.learncodethehardway.org/book/ex2.html) [ex28](http://c.learncodethehardway.org/book/ex28.html)

---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators :
    - ["Frederik Ring", "https://github.com/m90"]
    - ["Philipp Fischbeck", "https://github.com/PFischbeck"]
filename: markdown-de.md
lang: de-de
---

Markdown wurde im Jahr 2004 von John Gruber kreiert. Ziel ist und war eine
Syntax, in der sich Dokumente leicht schreiben *und* lesen lassen. Außerdem
sollte Markdown sich leicht nach HTML (und in andere Formate) konvertieren
lassen.

```markdown
<!-- Markdown ist eine Obermenge von HTML - jede valide HTML-Datei ist also
automatisch valides Markdown - was heisst dass wir jedes HTML-Element (also auch
Kommentare) in Markdown benutzen können, ohne dass der Parser sie verändert.
Jedoch kann man innerhalb eines solchen HTML-Elements dann kein Markdown
mehr verwenden. -->

<!-- Es existieren unterschiedliche Markdown-Parser und -Dialekte, die sich in
manchen Punkten unterscheiden. Diese Einführung wird versuchen, zu erläutern,
welche Features überall verfügbar sind, und welche davon parser-spezifisch sind -->

<!-- Überschriften -->
<!-- HTML-Überschriften <h1> bis <h6> lassen sich einfach durch ein Voranstellen
der entsprechenden Anzahl an Hashes (#) auszeichnen -->
# Das ist eine <h1>
## Das ist eine <h2>
### Das ist eine <h3>
#### Das ist eine <h4>
##### Das ist eine <h5>
###### Das ist eine <h6>

<!-- Für die Elemente <h1> und <h2> gibt es in Markdown noch Sonderformen -->
Das ist eine h1
=============

Das ist eine h2
-------------

<!-- Einfaches Textstyling -->
<!-- Jeglicher Text lässt sich mit Markdown leicht als kursiv oder
auch als fett auszeichnen -->

*Dieser Text ist kursiv.*
_Genau wie dieser._

**Dieser Text ist fett.**
__Genau wie dieser.__

***Dieser Text ist beides***
**_Dieser auch!_**
*__Und dieser genau so!__*

<!-- In "GitHub Flavored Markdown", dem von GitHub verwendeten Dialekt / Parser,
gibt es auch noch durchgestrichenen Text: -->

~~Dieser Text wird durchgestrichen dargestellt.~~

<!-- Absätze sind eine oder mehrere zusammenhängende Zeilen Text, und werden
durch eine oder mehrere Leerzeilen voneinander abgesetzt. -->

Das ist ein Absatz. Ich kann immer noch nicht glauben, wie viel Spaß das macht !?!

Jetzt bin ich schon bei Absatz 2.
Hier ist dann immer noch Absatz 2!


Jetzt ist das dann Nummer drei!

<!-- Sollte man jemals ein <br />-Tag einfügen wollen, kann man einen Absatz
mit zwei oder mehr Leerzeichen beenden, und danach einen neuen Absatz beginnen. -->

Ich höre mit zwei Leerzeichen auf (markiere mich, und du siehst es).  

Über mir ist wohl ein <br />!

<!-- Zitate werden ganz einfach mit einem  > ausgezeichnet. -->

> Das ist ein Zitat. Du kannst Zeilenumbrüche
> entweder manuell hinzufügen und ein `>` vor jeder Zeile einfügen, oder du kannst deine Zeilen einfach immer länger und länger werden lassen, die Umbrüche werden dann automatisch erzeugt.
> Solange sie mit einem `>` beginnen, macht das keinen Unterschied.

> Auch möglich ist es, den Text
>> mehrstufig einzurücken.
> Nicht schlecht, oder?

<!-- Listen -->
<!-- <ul>s können mit Sternen, Pluszeichen oder Minuszeichen erzeugt werden -->

* Punkt auf der Liste
* Punkt auf der Liste
* Anderer Punkt auf der Liste

oder

+ Punkt auf der Liste
+ Punkt auf der Liste
+ Noch ein Punkt auf der Liste

oder

- Punkt auf der Liste
- Punkt auf der Liste
- Ein letzter Punkt auf der Liste

<!-- <ol>s werden mit einer Zahl gefolgt von einem Punkt erzeugt -->

1. Punkt eins
2. Punkt zwei
3. Punkt drei

<!-- Auch wenn es keine gute Idee sein mag: du müsstest die einzelnen Punkte
nicht mal korrekt numerieren -->

1. Punkt eins
1. Punkt zwei
1. Punkt drei
<!-- (Das sieht genau so aus wie das Beispiel eins weiter oben) -->

<!-- Man kann Listen auch verschachteln -->

1. Punkt eins
2. Punkt zwei
3. Punkt drei
    * Unterpunkt
    * Unterpunkt
4. Punkt vier

<!-- Code-Blöcke -->
<!-- Blöcke von Programmcode (also ein <code>-Element) kannst du auszeichnen,
indem du eine Zeile mit vier Leerzeichen oder einem Tabulator einrückst -->

    Das ist Quellcode
    Das hier auch

<!-- Der Code kann natürlich auch wiederum eingerückt sein -->

    my_array.each do |item|
        puts item
    end

<!-- Innerhalb normalen Texts kannst du Code mit Backticks ` auszeichnen -->

Hermann hatte nicht die leiseste Ahnung, was dieses `go_to()` bedeuten könnte!

<!-- In "GitHub Flavored Markdown" gibt es für Code nocheinmal eine
besondere Syntax -->

\`\`\`ruby <!-- in "echt" musst du die Backslashes entfernen: ```ruby ! -->
def foobar
    puts "Hallo Welt!"
end
\`\`\` <!-- hier auch keine Backslashes, nur ``` -->

<-- der obige Block muss nicht extra eingerückt werden, außerdem fügt GitHub
Syntax-Highlighting für die nach dem ``` angegebene Sprache hinzu -->

<!-- Horizontale Linie (<hr />) -->
<!-- Trenner lassen sich einfach mit drei (oder mehr) Sternen oder Bindestrichen
erzeugen (egal ob mit oder ohne Leerzeichen dazwischen)-->

***
---
- - -
****************

<!-- Hyperlinks -->
<!-- Eines der besten Features von Markdown ist das kinderleichte Erzeugen von
Hyperlinks: Einfach den Linktext in eckige Klammern [] setzen, gefolgt von
einer mit runden Klammern () umschlossenen URL. -->

[Klick mich!](http://test.de/)

<!-- Man kann dem Link auch noch ein title-Attribut geben -->

[Klick mich!](http://test.at/ "Link zu Test.at")

<!-- Relative Pfade funktionieren natürlich auch -->

[Zu meiner Musiksammlung](/music/).

<!-- URLs lassen sich auch über Referenzen festlegen -->

[Klick mich][link1], um mehr über mich herauszufinden!
[Hier kannst du auch mal draufklicken][foobar], wenn es dich interessiert.

[link1]: http://test.de/ "Wahnsinn!"
[foobar]: http://foobar.ch/ "Erstaunlich!"

<!-- Das title-Attribut wird entweder mit Anführungszeichen oder Klammern
umschlossen (oder gleich ganz weggelassen). Die Referenzen können an jeder
Stelle im gesamtem Dokument vorkommen, als ID kann alles verwendet werden, solange
es dokumentweit eindeutig ist. -->

<!-- Man kann den Linktext auch als implizite Referenz benutzen -->

[Das][] ist ein Link.

[das]: http://dasisteinlink.at/

<!-- Das ist aber eher unüblich. -->

<!-- Bilder -->
<!-- Bilder funktionieren genau wie Links, nur dass man noch ein Ausrufezeichen
voranstellt! -->

![Das ist das alt-Attribut für mein Bild](http://imgur.com/myimage.jpg "Hier noch ein title-Attribut")

<!-- Referenzen funktionieren auch hier genau wie erwartet -->

![Das ist das alt-Attribut][meinbild]

[meinbild]: relative/urls/gehen/auch.jpg "hier wäre noch Platz für einen title"

<!-- Bonusfeatures -->
<!-- Auto-Links -->

<http://testwebseite.de/> ist das selbe wie
[http://testwebseite.de/](http://testwebseite.de/)

<!-- Automatische Links für E-Mail-Addressen -->

<foo@bar.com>

<!-- Maskieren -->

Ich würde *diesen Teil gerne mit Sternen umschließen*, doch ohne dass er kursiv
wird. Also mache ich folgendes: \*Ich umschließe diesen Text mit Sternen\*!

<!-- Tabellen -->
<!-- Tabellen gibt es bis jetzt nur in "GitHub Flavored Markdown".
Zudem sind sie ziemlich mühselig, aber wenn du es wirklich wissen willst: -->

| Spalte1      | Spalte2  | Spalte3       |
| :----------- | :------: | ------------: |
| linksbündig  | mittig   | rechtsbündig  |
| blah         | blah     | blah          |

<!-- oder das selbe in grün: -->

Spalte1 | Spalte2 | Spalte3
:-- | :-: | --:
Ganz schön hässlich | vielleicht doch lieber | wieder aufhören

<!-- Das war's! -->

```

Mehr Informationen gibt es in [John Gruber's offiziellem Blog-Post](http://daringfireball.net/projects/markdown/syntax)
und bei Adam Pritchards [grandiosem Cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
Infos zu GitHub Flavored Markdown [gibt es hier](https://help.github.com/articles/github-flavored-markdown).---
language: perl
filename: learnperl-de.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
translators:
    - ["Martin Schimandl", "http://github.com/Git-Jiro"]
lang: de-de
---

Perl 5 ist eine sehr mächtige, funktionsreiche Programmiersprache mit über 25 Jahren Entwicklungsgeschichte.

Perl 5 läuft auf über 100 Platformen von portablen Geräten bis hin zu Mainframes. Perl 5 ist geeignet für Rapid-Prototyping und auch groß angelegte Entwicklungs-Projekte.

```perl
# Einzeilige Kommentare beginnen mit dem # Symbol.


#### Perl Variablen Typen

#  Variablen beginnen mit einem Sigil, das ist ein Symbol das den Typ anzeigt.
#  Ein erlaubter Variablen-Name beginnt mit einem Buchstaben oder einem
#  Unterstrich, gefolgt von beliebig vielen weiteren Buchstaben, Zahlen und
#  Unterstrichen.

### Perl hat drei Haupt-Typen von Variablen: $scalar, @array, und %hash.

## Scalare
#  Ein Scalar repräsentiert einen einzelnen Wert:
my $animal = "camel";
my $answer = 42;

# Scalare Werte könnne Zeichenketten, Ganzzahlen und Gleitkomma-Zahlen sein.
# Perl convertiert automatisch zwischen diesen Werten wenn nötig.

## Arrays
#  Ein Array repräsentiert eine Liste von Werten:
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed   = ("camel", 42, 1.23);



## Hashes
#   Ein Hash representiert ein Set von Schlüssel/Wert Paaren:

my %fruit_color = ("apple", "red", "banana", "yellow");

#  Man kann Leerzeichen und den "=>" Operator verwenden um sie schön darzustellen:

my %fruit_color = (
  apple  => "red",
  banana => "yellow",
);
# Scalare, Arrays und Hashes sind in perldata sehr genau dokumentiert.
# (perldoc perldata)

# Komplexere Daten-Typen können mit hilfe von Referenzen konstruiert werden.
# Dies erlaubt das erstellen von Listen und Hashes in Listen und Hashes.

#### Bedingte Ausführungs- und Schleifen-Konstrukte.

# Perl besitzt die üblichen Bedingte Ausführung- und Schleifen-Konstrukte

if ($var) {
  ...
} elsif ($var eq 'bar') {
  ...
} else {
  ...
}

unless (condition) {
  ...
}
# Dies ist die etwas leserliche Version von "if (!Bedingung)"

# Die Perl-Eigene Post-Bedingungs-Schreibweise
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;

#  while
while (condition) {
  ...
}


# Für Schleifen und Iterationen
for (my $i = 0; $i < $max; $i++) {
  print "index is $i";
}

for (my $i = 0; $i < @elements; $i++) {
  print "Current element is " . $elements[$i];
}

for my $element (@elements) {
  print $element;
}

# Implizite Iteration
for (@elements) {
  print;
}

# Die Perl-Eigene Post-Bedingungs-Schreibweise nochmals
print for @elements;

#### Reguläre Ausdrücke

# Die Unterstützung von Perl für reguläre Ausdrücke ist weit und tiefgreifend.
# Sie ist ausführlich in perlrequick, perlretut und sonstwo dokumentiert.
# Die Kurzfassung:

# Einfaches Vergleichen
if (/foo/)       { ... }  # Wahr wenn "foo" in $_ enthalten ist
if ($a =~ /foo/) { ... }  # Wahr wenn "foo" in $a enthalten ist

# Einfache Substitution

$a =~ s/foo/bar/;         # Ersetzt foo mit bar in $a
$a =~ s/foo/bar/g;        # Ersetzt ALLE VORKOMMNISSE von foo mit bar in $a


#### Dateien und Ein-/Ausgabe

# Dateien werden mit der "open()" Funktion zur Ein- oder Ausgabe geöffnet.

open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";
open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";

# Von einem geöffneten Datei-Handle kann mit dem "<>" Operator gelesen werden.
# In einem Scalaren-Kontext liest man damit eine einzelnen Zeile vom Datei-Handle.
# In einem Listen-Kontext wird damit die komplette Datei eingelesen. Dabei
# entspricht jede Zeile einem Element der Liste:

my $line  = <$in>;
my @lines = <$in>;

#### Schreiben von Subroutinen

# Subroutinen schreiben ist einfach:

sub logger {
  my $logmessage = shift;

  open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";

  print $logfile $logmessage;
}

# Nun können wir die Subroutine genau wie eine eingebaute Funktion verwenden:

logger("We have a logger subroutine!");
```

#### Verwenden von Perl Modulen

Perl Module liefern eine Menge an Funktionen die dabei Helfen das Rad nicht neu erfinden zu müssen. Perl Module können von CPAN (http://www.cpan.org/) heruntergeladen werden. Einige populäre Module sind in der Perl Distribution selbst bereits enthalten.

Perlfaq enthält Fragen und Antworten zu häufig vorkommenden Aufgaben. Sehr oft sind auch Vorschläge enthalten welches CPAN module am besten geeignet ist.

#### Weiterführende Literatur

 - [perl-tutorial](http://perl-tutorial.org/)
 - [Learn at www.perl.com](http://www.perl.org/learn.html)
 - [perldoc](http://perldoc.perl.org/)
 - in Perl eingebaut : `perldoc perlintro`
---
language: python
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["kultprok", "http:/www.kulturproktologie.de"]
filename: learnpython-de.py
lang: de-de
---

Anmerkungen des ursprünglichen Autors:
Python wurde in den frühen Neunzigern von Guido van Rossum entworfen. Es ist heute eine der beliebtesten Sprachen. Ich habe mich in Python wegen seiner syntaktischen Übersichtlichkeit verliebt. Eigentlich ist es ausführbarer Pseudocode.

Feedback ist herzlich willkommen! Ihr erreicht mich unter [@louiedinh](http://twitter.com/louiedinh) oder louiedinh [at] [google's email service]

Hinweis: Dieser Beitrag bezieht sich besonders auf Python 2.7, er sollte aber auf Python 2.x anwendbar sein. Haltet Ausschau nach einem Rundgang durch Python 3, der bald erscheinen soll.

```python
# Einzeilige Kommentare beginnen mit einer Raute (Doppelkreuz)
""" Mehrzeilige Strings werden mit 
    drei '-Zeichen geschrieben und werden
    oft als Kommentare genutzt.
"""

####################################################
## 1. Primitive Datentypen und Operatoren
####################################################

# Die Zahlen
3 #=> 3

# Mathematik funktioniert so, wie man das erwartet
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# Division ist ein wenig kniffliger. Ganze Zahlen werden ohne Rest dividiert
# und das Ergebnis wird automatisch abgerundet.
5 / 2 #=> 2

# Um das zu ändern, müssen wir Gleitkommazahlen einführen und benutzen
2.0     # Das ist eine Gleitkommazahl
11.0 / 4.0 #=> 2.75 Ahhh...schon besser

# Rangfolge wird mit Klammern erzwungen
(1 + 3) * 2 #=> 8

# Boolesche Ausdrücke sind primitive Datentypen
True
False

# Mit not wird negiert
not True #=> False
not False #=> True

# Gleichheit ist ==
1 == 1 #=> True
2 == 1 #=> False

# Ungleichheit ist !=
1 != 1 #=> False
2 != 1 #=> True

# Ein paar weitere Vergleiche
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# Vergleiche können verknüpft werden!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Strings werden mit " oder ' gebildet
"Das ist ein String."
'Das ist auch ein String.'

# Strings können addiert werden!
"Hello " + "world!" #=> "Hello world!"

# Ein String kann wie eine Liste von Zeichen verwendet werden
"Das ist ein String"[0] #=> 'D'

# Mit % können Strings formatiert werden, etwa so:
"%s können %s werden" % ("Strings", "interpoliert")

# Ein modernerer Weg, um Strings zu formatieren, ist die format-Methode.
# Diese Methode wird bevorzugt
"{0} können {1} werden".format("Strings", "formatiert")
# Wir können Schlüsselwörter verwenden, wenn wir nicht abzählen wollen.
"{name} will {food} essen".format(name="Bob", food="Lasagne")

# None ist ein Objekt
None #=> None

# Verwendet nicht das Symbol für Gleichheit `==`, um Objekte mit None zu vergleichen
# Benutzt stattdessen `is`
"etc" is None #=> False
None is None  #=> True

# Der 'is'-Operator testet Objektidentität. Das ist nicht
# sehr nützlich, wenn wir mit primitiven Datentypen arbeiten, aber
# sehr nützlich bei Objekten.

# None, 0, und leere Strings/Listen werden alle als False bewertet.
# Alle anderen Werte sind True
0 == False  #=> True
"" == False #=> True


####################################################
## 2. Variablen und Collections
####################################################

# Textausgabe ist sehr einfach
print "Ich bin Python. Schön, dich kennenzulernen!"


# Es gibt keinen Grund, Variablen vor der Zuweisung zu deklarieren.
some_var = 5    # kleinschreibung_mit_unterstrichen entspricht der Norm
some_var #=> 5

# Das Ansprechen einer noch nicht deklarierte Variable löst eine Exception aus.
# Unter "Kontrollstruktur" kann noch mehr über
# Ausnahmebehandlung erfahren werden.
some_other_var  # Löst einen NameError aus

# if kann als Ausdruck verwendet werden
"yahoo!" if 3 > 2 else 2 #=> "yahoo!"

# Listen speichern Sequenzen
li = []
# Wir können mit einer bereits gefüllten Liste anfangen
other_li = [4, 5, 6]

# append fügt Daten am Ende der Liste ein
li.append(1)    #li ist jetzt [1]
li.append(2)    #li ist jetzt [1, 2]
li.append(4)    #li ist jetzt [1, 2, 4]
li.append(3)    #li ist jetzt [1, 2, 4, 3]
# Vom Ende der Liste mit pop entfernen
li.pop()        #=> 3 und li ist jetzt [1, 2, 4]
# und dann wieder hinzufügen
li.append(3)    # li ist jetzt wieder [1, 2, 4, 3].

# Greife auf Listen wie auf Arrays zu
li[0] #=> 1
# Das letzte Element ansehen
li[-1] #=> 3

# Bei Zugriffen außerhalb der Liste kommt es jedoch zu einem IndexError
li[4] # Raises an IndexError

# Wir können uns Ranges mit Slice-Syntax ansehen
li[1:3] #=> [2, 4]
# Den Anfang auslassen
li[2:] #=> [4, 3]
# Das Ende auslassen
li[:3] #=> [1, 2, 4]

# Ein bestimmtes Element mit del aus der Liste entfernen
del li[2] # li ist jetzt [1, 2, 3]

# Listen können addiert werden
li + other_li #=> [1, 2, 3, 4, 5, 6] - Hinweis: li und other_li werden in Ruhe gelassen

# Listen mit extend verknüpfen
li.extend(other_li) # Jetzt ist li [1, 2, 3, 4, 5, 6]

# Mit in auf Existenz eines Elements prüfen
1 in li #=> True

# Die Länge der Liste mit len ermitteln
len(li) #=> 6


# Tupel sind wie Listen, nur unveränderlich.
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # Löst einen TypeError aus

# Wir können all diese Listen-Dinge auch mit Tupeln anstellen
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Wir können Tupel (oder Listen) in Variablen entpacken
a, b, c = (1, 2, 3)     # a ist jetzt 1, b ist jetzt 2 und c ist jetzt 3
# Tupel werden standardmäßig erstellt, wenn wir uns die Klammern sparen
d, e, f = 4, 5, 6
# Es ist kinderleicht zwei Werte zu tauschen
e, d = d, e     # d is now 5 and e is now 4


# Dictionarys (Wörterbucher) speichern Key-Value-Paare
empty_dict = {}
# Hier ein gefülltes Wörterbuch
filled_dict = {"one": 1, "two": 2, "three": 3}

# Wir können Einträge mit [] nachschlagen
filled_dict["one"] #=> 1

# So holen wir alle Keys (Schlüssel) als Liste
filled_dict.keys() #=> ["three", "two", "one"]
# Hinweis - Die Reihenfolge von Schlüsseln in der Liste ist nicht garantiert.
# Einzelne Resultate können anders angeordnet sein.

# Alle Values (Werte) als Liste
filled_dict.values() #=> [3, 2, 1]
# Hinweis - Hier gelten dieselben Einschränkungen für die Reihenfolge wie bei Schlüsseln.

# Das Vorhandensein eines Schlüssels im Wörterbuch mit in prüfen
"one" in filled_dict #=> True
1 in filled_dict #=> False

# Einen nicht vorhandenenen Schlüssel zu suchen, löst einen KeyError aus
filled_dict["four"] # KeyError

# Mit der get-Methode verhindern wir das
filled_dict.get("one") #=> 1
filled_dict.get("four") #=> None
# Die get-Methode unterstützt auch ein Standardargument, falls der Wert fehlt
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4

# Die setdefault-Methode ist ein sicherer Weg, ein neues Schlüssel-Wert-Paar anzulegen
filled_dict.setdefault("five", 5) #filled_dict["five"] wird auf 5 gesetzt
filled_dict.setdefault("five", 6) #filled_dict["five"] ist noch immer 5


# Sets speichern Mengen
empty_set = set()
# Initialisieren wir ein Set mit ein paar Werten
some_set = set([1,2,2,3,4]) # some_set ist jetzt set([1, 2, 3, 4])

# Seit Python 2.7 kann {} benutzt werden, um ein Set zu erstellen
filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}

# Mehr Elemente hinzufügen
filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}

# Schnittmengen werden mit & gebildet
other_set = {3, 4, 5, 6}
filled_set & other_set #=> {3, 4, 5}

# Mengen werden mit | vereinigt
filled_set | other_set #=> {1, 2, 3, 4, 5, 6}

# Die Differenz einer Menge mit - bilden
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Auf Vorhandensein von Elementen mit in prüfen
2 in filled_set #=> True
10 in filled_set #=> False


####################################################
## 3. Kontrollstruktur
####################################################

# Erstellen wir mal eine Variable
some_var = 5

# Hier eine if-Anweisung. Die Einrückung ist in Python wichtig!
# gibt "some_var ist kleiner als 10" aus
if some_var > 10:
    print "some_var ist viel größer als 10."
elif some_var < 10:    # Dieser elif-Absatz ist optional.
    print "some_var ist kleiner als 10."
else:           # Das hier ist auch optional.
    print "some_var ist tatsächlich 10."


"""
For-Schleifen iterieren über Listen
Ausgabe:
    hund ist ein Säugetier
    katze ist ein Säugetier
    maus ist ein Säugetier
"""
for animal in ["hund", "katze", "maus"]:
    # Wir können Strings mit % formatieren
    print "%s  ist ein Säugetier" % animal
    
"""
`range(Zahl)` gibt eine null-basierte Liste bis zur angegebenen Zahl wieder
Ausgabe:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
While-Schleifen laufen, bis eine Bedingung erfüllt ist.
Ausgabe:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Kurzform für x = x + 1

# Ausnahmebehandlung mit einem try/except-Block

# Funktioniert in Python 2.6 und höher:
try:
    # Mit raise wird ein Fehler ausgegeben
    raise IndexError("Das hier ist ein Index-Fehler")
except IndexError as e:
    pass    # Pass ist nur eine no-op. Normalerweise würden wir hier den Fehler klären.


####################################################
## 4. Funktionen
####################################################

# Mit def neue Funktionen erstellen
def add(x, y):
    print "x ist %s und y ist %s" % (x, y)
    return x + y    # Werte werden mit return zurückgegeben

# Funktionen mit Parametern aufrufen
add(5, 6) #=> Ausgabe ist "x ist 5 und y ist 6" und gibt 11 zurück

# Ein anderer Weg des Funktionsaufrufs sind Schlüsselwort-Argumente
add(y=6, x=5)   # Schlüsselwörter können in beliebiger Reihenfolge übergeben werden.

# Wir können Funktionen mit beliebiger Anzahl von # Positionsargumenten definieren
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# Wir können auch Funktionen mit beliebiger Anzahl
# Schlüsselwort-Argumenten definieren
def keyword_args(**kwargs):
    return kwargs

# Rufen wir es mal auf, um zu sehen, was passiert
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# Wir können beides gleichzeitig machem, wenn wir wollen
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) Ausgabe:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Beim Aufruf von Funktionen können wir das Gegenteil von varargs/kwargs machen!
# Wir benutzen dann *, um Tupel auszuweiten, und ** für kwargs.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # äquivalent zu foo(1, 2, 3, 4)
all_the_args(**kwargs) # äquivalent zu foo(a=3, b=4)
all_the_args(*args, **kwargs) # äquivalent zu  foo(1, 2, 3, 4, a=3, b=4)

# Python hat First-Class-Funktionen
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# Es gibt auch anonyme Funktionen
(lambda x: x > 2)(3) #=> True

# Es gibt auch Funktionen höherer Ordnung als Built-Ins
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Wir können bei map- und filter-Funktionen auch List Comprehensions einsetzen
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Klassen
####################################################

# Wir bilden die Unterklasse eines Objekts, um Klassen zu erhalten.
class Human(object):

    # Ein Klassenattribut. Es wird von allen Instanzen einer Klasse geteilt
    species = "H. sapiens"

    # Ein simpler Konstruktor
    def __init__(self, name):
        # Wir weisen das Argument name dem name-Attribut der Instanz zu
        self.name = name

    # Eine Instanzmethode. Alle Methoden erhalten self als erstes Argument.
    def say(self, msg):
       return "%s: %s" % (self.name, msg)

    # Eine Klassenmethode wird von allen Instanzen geteilt.
    # Sie werden mit der aufrufenden Klasse als erstem Argument aufgerufen
    @classmethod
    def get_species(cls):
        return cls.species

    # Eine statische Methode wird ohne Klasse oder Instanz aufgerufen
    @staticmethod
    def grunt():
        return "*grunt*"


# Eine Instanz einer Klasse erstellen
i = Human(name="Ian")
print i.say("hi")     # gibt "Ian: hi" aus

j = Human("Joel")
print j.say("hello")  #gibt "Joel: hello" aus

# Rufen wir mal unsere Klassenmethode auf
i.get_species() #=> "H. sapiens"

# Ändern wir mal das gemeinsame Attribut
Human.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# Aufruf der statischen Methode
Human.grunt() #=> "*grunt*"


####################################################
## 6. Module
####################################################

# Wir können Module importieren
import math
print math.sqrt(16) #=> 4

# Wir können auch nur spezielle Funktionen eines Moduls importieren
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# Wir können auch alle Funktionen eines Moduls importieren
# Warnung: Dies wird nicht empfohlen
from math import *

# Wir können Modulnamen abkürzen
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Module sind in Python nur gewöhnliche Dateien. Wir
# können unsere eigenen schreiben und importieren. Der Name des 
# Moduls ist der Dateiname.

# Wir können auch die Funktionen und Attribute eines
# Moduls herausfinden.
import math
dir(math)


```

## Lust auf mehr?

### Kostenlos online (Englisch)

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)

### Totholz (Englisch)

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: python3
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["kultprok", "http:/www.kulturproktologie.de"]
    - ["matthiaskern", "https://github.com/matthiaskern"]
filename: learnpython3-de.py
lang: de-de
---

Anmerkungen des ursprünglichen Autors:
Python wurde in den frühen Neunzigern von Guido van Rossum entworfen. Es ist heute eine der beliebtesten Sprachen. Ich habe mich in Python wegen seiner syntaktischen Übersichtlichkeit verliebt. Eigentlich ist es ausführbarer Pseudocode.

Feedback ist herzlich willkommen! Ihr erreicht mich unter [@louiedinh](http://twitter.com/louiedinh) oder louiedinh [at] [google's email service].

Hinweis: Dieser Beitrag bezieht sich insplizit auf Python 3. Falls du lieber Python 2.7 lernen möchtest, schau [hier](http://learnxinyminutes.com/docs/python/) weiter.

```python

# Einzeilige Kommentare beginnen mit einer Raute (Doppelkreuz)

""" Mehrzeilige Strings werden mit
    drei '-Zeichen geschrieben und werden
    oft als Kommentare genutzt.
"""

####################################################
## 1. Primitive Datentypen und Operatoren
####################################################

# Die Zahlen
3 #=> 3

# Mathematik funktioniert so, wie man das erwartet
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20

# Außer Division, welche automatisch Gleitkommazahlen zurückgibt
35 / 5  # => 7.0

# Eine Division kann mit "//" für positive sowie negative Werte abgerundet werden.
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # works on floats too
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# Benutzt man eine Gleitkommazahl, ist auch das Ergebnis eine solche
3 * 2.0 # => 6.0

# Der Rest einer Division
7 % 3 # => 1

# Potenz
2**4 # => 16

# Rangfolge wird mit Klammern erzwungen
(1 + 3) * 2 #=> 8

# Boolesche Ausdrücke sind primitive Datentypen
True
False

# Mit not wird negiert
not True #=> False
not False #=> True

# Boolesche Operatoren
# Hinweis: "and" und "or" müssen klein geschrieben werden
True and False #=> False
False or True #=> True

# Für die Benutzung von Booleschen Operatoren und ganzen Zahlen
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# Gleichheit ist ==
1 == 1 #=> True
2 == 1 #=> False

# Ungleichheit ist !=
1 != 1 #=> False
2 != 1 #=> True

# Ein paar weitere Vergleiche
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# Vergleiche können verknüpft werden!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Strings werden mit " oder ' gebildet
"Das ist ein String."
'Das ist auch ein String.'

# Strings können auch addiert werden! Vermeide dies aber lieber.
"Hallo " + "Welt!" #=> "Hallo Welt!"
# Strings können ohne "+" addiert werden
"Hallo " "welt!"  # => "Hallo Welt!"

# Ein String kann wie eine Liste von Zeichen verwendet werden
"Das ist ein String"[0] #=> 'D'

# .format kann Strings formatieren
"{} können {} werden".format("Strings", "formatiert")

# Schneller geht das mit Wiederholungen
"{0} mag Spagetthi, {0} liebt es zu Schwimmen und ganz besonders mag {0} {1}".format("Hans", "Blattsalat")
#=> "Hans mag Spagetthi, Hans liebt es zu Schwimmen und ganz besonders mag Hans Blattsalat"

# Wir können Schlüsselwörter verwenden, wenn wir nicht abzählen wollen.
"{name} will {food} essen".format(name="Bob", food="Lasagne")
#=> "Bob will Lasagne kochen"

#Falls dein Python 3 Code auch unter Python 2.5 oder darunter laufen soll, kann das alte Format benutzt werden:
"%s können %s werden" % ("Strings", "interpoliert")


# None ist ein Objekt
None #=> None

# Verwendet nicht das Symbol für Gleichheit `==`, um Objekte mit None zu vergleichen
# Benutzt stattdessen `is`. Dieser Operator testet Objektidentität
"etc" is None #=> False
None is None  #=> True



# None, 0, und leere Strings/Listen werden alle als False bewertet.
# Alle anderen Werte sind True
bool(0)  # => False
bool("")  # => False
bool([]) #=> False
bool({}) #=> False


####################################################
## 2. Variablen und Collections
####################################################

# Textausgabe ist sehr einfach
print "Ich bin Python. Schön, dich kennenzulernen!"

# Es gibt keinen Grund, Variablen vor der Zuweisung zu deklarieren.
some_var = 5    # kleinschreibung_mit_unterstrichen entspricht der Norm
some_var #=> 5

# Das Ansprechen einer noch nicht deklarierte Variable löst eine Exception aus.
# Unter "Kontrollstruktur" kann noch mehr über
# Ausnahmebehandlung erfahren werden.
some_unknown_var  # Löst einen NameError aus

# Listen speichern Sequenzen
li = []
# Wir können mit einer bereits gefüllten Liste anfangen
other_li = [4, 5, 6]

# append fügt Daten am Ende der Liste ein
li.append(1)    #li ist jetzt [1]
li.append(2)    #li ist jetzt [1, 2]
li.append(4)    #li ist jetzt [1, 2, 4]
li.append(3)    #li ist jetzt [1, 2, 4, 3]
# Vom Ende der Liste mit pop entfernen
li.pop()        #=> 3 und li ist jetzt [1, 2, 4]
# und dann wieder hinzufügen
li.append(3)    # li ist jetzt wieder [1, 2, 4, 3].

# Greife auf Listen wie auf Arrays zu
li[0] #=> 1
# Das letzte Element ansehen
li[-1] #=> 3

# Bei Zugriffen außerhalb der Liste kommt es jedoch zu einem IndexError
li[4] # Verursacht einen IndexError

# Wir können uns Ranges mit Slice-Syntax ansehen
li[1:3] #=> [2, 4]
# Den Anfang auslassen
li[2:] #=> [4, 3]
# Das Ende auslassen
li[:3] #=> [1, 2, 4]
# Jeden Zweiten Eintrag auswählen
li[::2]   # =>[1, 4]
# Eine umgekehrte Kopie zurückgeben
li[::-1]   # => [3, 4, 2, 1]
# Jegliche Kombination dieser Syntax machen fortgeschrittene Slices möglich
# li[Start:Ende:Schritt]

# Ein bestimmtes Element mit del aus der Liste entfernen
del li[2] # li ist jetzt [1, 2, 3]

# Listen können addiert werden
li + other_li #=> [1, 2, 3, 4, 5, 6] - Hinweis: li und other_li werden in Ruhe gelassen

# Listen mit extend verknüpfen
li.extend(other_li) # Jetzt ist li [1, 2, 3, 4, 5, 6]

# Mit in auf Existenz eines Elements prüfen
1 in li #=> True

# Die Länge der Liste mit len ermitteln
len(li) #=> 6


# Tupel sind wie Listen, nur unveränderlich.
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # Löst einen TypeError aus

# Wir können all diese Listen-Dinge auch mit Tupeln anstellen
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Wir können Tupel (oder Listen) in Variablen entpacken
a, b, c = (1, 2, 3)     # a ist jetzt 1, b ist jetzt 2 und c ist jetzt 3
# Tupel werden standardmäßig erstellt, wenn wir uns die Klammern sparen
d, e, f = 4, 5, 6
# Es ist kinderleicht zwei Werte zu tauschen
e, d = d, e     # d is now 5 and e is now 4


# Dictionarys (Wörterbucher) speichern Schlüssel-Werte-Paare
empty_dict = {}
# Hier ein gefülltes Wörterbuch
filled_dict = {"one": 1, "two": 2, "three": 3}

# Wir können Einträge mit [] nachschlagen
filled_dict["one"] #=> 1

# So holen wir alle Keys (Schlüssel) als Liste
list(filled_dict.keys()) #=> ["three", "two", "one"]
# Hinweis - Die Reihenfolge von Schlüsseln in der Liste ist nicht garantiert.
# Einzelne Resultate können anders angeordnet sein.

# Alle Values (Werte) als Liste
list(filled_dict.values()) #=> [3, 2, 1]
# Hinweis - Hier gelten dieselben Einschränkungen für die Reihenfolge wie bei Schlüsseln.

# Das Vorhandensein eines Schlüssels im Wörterbuch mit "in" prüfen
"one" in filled_dict #=> True
1 in filled_dict #=> False

# Einen nicht vorhandenenen Schlüssel zu suchen, löst einen KeyError aus
filled_dict["four"] # KeyError

# Mit der get-Methode verhindern wir das
filled_dict.get("one") #=> 1
filled_dict.get("four") #=> None
# Die get-Methode unterstützt auch ein Standardargument, falls der Wert fehlt
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4

# Die setdefault-Methode ist ein sicherer Weg, ein neues Schlüssel-Wert-Paar anzulegen
filled_dict.setdefault("five", 5) #filled_dict["five"] wird auf 5 gesetzt
filled_dict.setdefault("five", 6) #filled_dict["five"] ist noch immer 5

# Einträge zu einem Wörterbuch hinzufügen
filled_dict.update({"four":4}) #=> {"one": 1, "two": 2, "three": 3, "four": 4}
#filled_dict["four"] = 4  # noch ein Weg, Werte hinzuzufügen

# Schlüssel von einem Wörterbuch entfernen
del filled_dict["one"]  # Entfert den Schlüssel "one"


# Sets speichern Mengen
empty_set = set()
# Initialisieren wir ein Set mit ein paar Werten
some_set = {1, 1, 2, 2, 3, 4} # some_set ist jetzt {1, 2, 3, 4}

# Neue Variablen können einer Menge gleichgesetzt werden
filled_set = some_set

# Mehr Elemente hinzufügen
filled_set.add(5) # filled_set ist jetzt {1, 2, 3, 4, 5}

# Schnittmengen werden mit & gebildet
other_set = {3, 4, 5, 6}
filled_set & other_set #=> {3, 4, 5}

# Mengen werden mit | vereinigt
filled_set | other_set #=> {1, 2, 3, 4, 5, 6}

# Die Differenz einer Menge mit - bilden
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Auf Vorhandensein von Elementen mit in prüfen
2 in filled_set #=> True
10 in filled_set #=> False


####################################################
## 3. Kontrollstruktur und Iteratoren
####################################################

# Erstellen wir mal eine Variable
some_var = 5

# Hier eine if-Anweisung. Die Einrückung ist in Python wichtig!
# gibt "some_var ist kleiner als 10" aus
if some_var > 10:
    print "some_var ist viel größer als 10."
elif some_var < 10:    # Dieser elif-Absatz ist optional.
    print "some_var ist kleiner als 10."
else:           # Das hier ist auch optional.
    print "some_var ist tatsächlich 10."


"""
For-Schleifen iterieren über Listen
Ausgabe:
    hund ist ein Säugetier
    katze ist ein Säugetier
    maus ist ein Säugetier
"""
for animal in ["hund", "katze", "maus"]:
    # Wir können Strings mit format() formatieren
    print("{}  ist ein Säugetier".format(animal))

"""
`range(Zahl)` gibt eine null-basierte Liste bis zur angegebenen Zahl wieder
Ausgabe:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
"range(unten, oben)" gibt eine Liste von der unteren Zahl bis zur oberen Zahl aus
Ausgabe:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print(i)

"""
While-Schleifen laufen, bis eine Bedingung erfüllt ist.
Ausgabe:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Kurzform für x = x + 1

# Ausnahmebehandlung mit einem try/except-Block
try:
    # Mit raise wird ein Fehler ausgegeben
    raise IndexError("Das hier ist ein Index-Fehler")
except IndexError as e:
    pass    # Pass ist nur eine no-op. Normalerweise würden wir hier den Fehler klären.
except (TypeError, NameError):
    pass    # Mehrere Fehler können zusammen geklärt werden, falls erforderlich.
else:   # Optional, hinter allen except-Blöcken
    print("Keine Probleme!")   # Wird nur ausgeführt, wenn keine Ausnahmen aufgetreten sind
finally: #  Wird immer ausgeführt
    print("Hier können wir Ressourcen aufräumen")

# alternativ zu einem try/finally Block um Aufzuräumen:
with open("meineDatei.txt") as f:
    for line in f:
        print(line)

# Python bietet ein fundamentales Konzept der Iteration.
# Das Objekt, auf das die Interation, also die Wiederholung einer Methode angewandt wird heißt auf Englisch "iterable".
# Die range Method gibt ein solches Objekt aus.

filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable) #=> range(1,10). Dies ist ein "iterable" Objekt.

# Über dieses können  wir auch iterieren
for i in our_iterable:
    print(i)    # Gibt one, two, three aus

# Allerdings können wir die einzelnen Elemente nicht mit ihrem index ausgeben
our_iterable[1]  # TypeError

# Ein iterable ist ein Objekt, das weiß wie es einen Iteratoren erschafft.
our_iterator = iter(our_iterable)

# Unser Iterator ist ein Objekt, das sich merkt, welchen Status es geraden hat während wir durch es gehen.
# Das jeweeils nächste Objekt bekommen wir mit "next()"
next(our_iterator)  #=> "one"

# Es hält den vorherigen Status
next(our_iterator)  #=> "two"
next(our_iterator)  #=> "three"

# Nachdem alle Daten ausgegeben worden sind, kommt eine StopIterator Ausnahme zurück
next(our_iterator) # Gibt StopIteration aus

# Alle Elemente können mit "list()" ausgegeben werden
list(filled_dict.keys())  #=> ["one", "two", "three"]



####################################################
## 4. Funktionen
####################################################

# Mit def neue Funktionen erstellen
def add(x, y):
    print "x ist %s und y ist %s" % (x, y)
    return x + y    # Werte werden mit return zurückgegeben

# Funktionen mit Parametern aufrufen
add(5, 6) #=> Ausgabe ist "x ist 5 und y ist 6" und gibt 11 zurück

# Ein anderer Weg des Funktionsaufrufs sind Schlüsselwort-Argumente
add(y=6, x=5)   # Schlüsselwörter können in beliebiger Reihenfolge übergeben werden.

# Wir können Funktionen mit beliebiger Anzahl von # Positionsargumenten definieren
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# Wir können auch Funktionen mit beliebiger Anzahl
# Schlüsselwort-Argumenten definieren
def keyword_args(**kwargs):
    return kwargs

# Rufen wir es mal auf, um zu sehen, was passiert
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# Wir können beides gleichzeitig machem, wenn wir wollen
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) Ausgabe:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Beim Aufruf von Funktionen können wir das Gegenteil von varargs/kwargs machen!
# Wir benutzen dann *, um Tupel auszuweiten, und ** für kwargs.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # äquivalent zu foo(1, 2, 3, 4)
all_the_args(**kwargs) # äquivalent zu foo(a=3, b=4)
all_the_args(*args, **kwargs) # äquivalent zu  foo(1, 2, 3, 4, a=3, b=4)


# Anwendungsbereich von Funktionen
x = 5

def setX(num):
    # lokale Variable x ist nicht die globale Variable x
    x = num # => 43
    print (x) # => 43

def setGlobalX(num):
    global x
    print (x) # => 5
    x = num # globale Variable x ist jetzt 6
    print (x) # => 6

setX(43)
setGlobalX(6)


# Python hat First-Class-Funktionen
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# Es gibt auch anonyme Funktionen
(lambda x: x > 2)(3) #=> True

# Es gibt auch Funktionen höherer Ordnung als Built-Ins
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Wir können bei map- und filter-Funktionen auch List Comprehensions einsetzen
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Klassen
####################################################

# Wir bilden die Unterklasse eines Objekts, um Klassen zu erhalten.
class Human(object):

    # Ein Klassenattribut. Es wird von allen Instanzen einer Klasse geteilt
    species = "H. sapiens"

    # Ein simpler Konstruktor
    def __init__(self, name):
        # Wir weisen das Argument name dem name-Attribut der Instanz zu
        self.name = name

    # Eine Instanzmethode. Alle Methoden erhalten self als erstes Argument.
    def say(self, msg):
        return "{name}: {message}".format(name=self.name, message=msg)

    # Eine Klassenmethode wird von allen Instanzen geteilt.
    # Sie werden mit der aufrufenden Klasse als erstem Argument aufgerufen
    @classmethod
    def get_species(cls):
        return cls.species

    # Eine statische Methode wird ohne Klasse oder Instanz aufgerufen
    @staticmethod
    def grunt():
        return "*grunt*"


# Eine Instanz einer Klasse erstellen
i = Human(name="Ian")
print i.say("hi")     # gibt "Ian: hi" aus

j = Human("Joel")
print j.say("hello")  #gibt "Joel: hello" aus

# Rufen wir mal unsere Klassenmethode auf
i.get_species() #=> "H. sapiens"

# Ändern wir mal das gemeinsame Attribut
Human.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# Aufruf der statischen Methode
Human.grunt() #=> "*grunt*"


####################################################
## 6. Module
####################################################

# Wir können Module importieren
import math
print math.sqrt(16) #=> 4

# Wir können auch nur spezielle Funktionen eines Moduls importieren
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# Wir können auch alle Funktionen eines Moduls importieren
# Warnung: Dies wird nicht empfohlen
from math import *

# Wir können Modulnamen abkürzen
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Module sind in Python nur gewöhnliche Dateien. Wir
# können unsere eigenen schreiben und importieren. Der Name des
# Moduls ist der Dateiname.

# Wir können auch die Funktionen und Attribute eines
# Moduls herausfinden.
import math
dir(math)


####################################################
## 7. Fortgeschritten
####################################################

# Generatoren helfen um Code schnell und einfach zu schreiben
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# Ein Generator erschafft Werte spontan
# Statt alle Werte auf einmal, wird bei jeder Iteration einer erschaffen.
# iteration.  Das heißt, Werte größer als 15 werden nicht behandelt.
# Die range-Methode ist auch ein Generator. Im Fall einer Liste von 1-900000000
# würde das sehr viel Zeit in Anspruch nehmen.
# Wenn wir eine variable mit einem Namen erschaffen wollen, das
# normalerweise mit einem Python - Schlüsselwort kollidieren würde,
# benutzen wir einen Unterstrich nach dem Wort.
range_ = range(1, 900000000)
# Alle Nummern bis zu einem Ergebnis von >=30 werden verdoppelt
for i in double_numbers(range_):
    print(i)
    if i >= 30:
        break


# Dekoratoren
# In diesem Beispiel die Methode beg umwickelt say
# Beim Aufruf von beg, say wird aufgerufen
# Falls say_please true ist, ändert sich die ausgegebene Nachricht
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Please! I am poor :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Can you buy me a beer?"
    return msg, say_please


print(say())  # Can you buy me a beer?
print(say(say_please=True))  # Can you buy me a beer? Please! I am poor :(

```

## Lust auf mehr?

### Kostenlos online (Englisch)

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)
* [Python Course](http://www.python-course.eu/index.php)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)

### Totholz (Englisch)

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
---
language: ruby
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
translators:
  - ["Christian Albrecht", "https://github.com/coastalchief"]
  - ["Dennis Keller", "https://github.com/denniskeller"]
filename: ruby-de.rb
lang: de-de
---

# Dies ist ein Kommentar

=begin
Dies sind multi-line
Kommentare. Niemand benutzt
die wirklich.
=end

# Objekte - Alles ist ein Objekt

## Zahlen sind Objekte
```
3.class #=> Fixnum
3.to_s #=> "3"
```

### Simple Arithmetik
```
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
```

// Arithmetik ist aber eigentlich nur syntaktischer Zucker
// um eine Methode eines Objekt aufzurufen
```
1.+(3) #=> 4
10.* 5 #=> 50
```

## Special values sind Objekte
```
nil # Nothing to see here
true # truth
false # falsehood

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass
```

## Objektvergleiche
### Gleicheit
```
1 == 1 #=> true
2 == 1 #=> false
```
### Ungleichheit
```
1 != 1 #=> false
2 != 1 #=> true
```
### Neben false selbst, nil ist ein anderer 'falsey' Wert
```
!nil   #=> true
!false #=> true
!0     #=> false
```
### Weitere Vergleiche
```
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true
```
### Logische Operatoren
```
true && false #=> false
true || false #=> true
!true #=> false
```

Es gibt alternative Versionen der logischen Operatoren mit niedrigerer
Wertigkeit. Diese werden meistens bei Flow-Control eingesetzt, um
verschiedenen Ausdrücke zu verketten bis einer true oder false zurück
liefert.

#### and
##### `do_something_else` wird nur ausgewertet wenn `do_something` true ist.
do_something() and do_something_else()

#### or
#####`log_error` wird nur ausgewertet wenn `do_something` false ist.
do_something() or log_error()

## Strings sind Objekte
```
'I am a string'.class #=> String
"I am a string too".class #=> String


platzhalter = 'Ruby'
"Ich kann in #{placeholder} Platzhalter mit doppelten Anführungsstrichen füllen."
```
Einfache Anführungszeichen sollten bevorzugt werden.
Doppelte Anführungszeichen führen interne Berechnungen durch.

### Strings können verbunden werden, aber nicht mit Zahlen
```
'hello ' + 'world'  #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
```
#### Zahl muss in String konvertiert werden
```
'hello ' + 3.to_s #=> "hello 3"
```
### Text ausgeben
```
puts "I'm printing!"
```
# Variablen
## Zuweisungen
### Diese Zuweisung gibt den zugeordneten Wert zurück
```
x = 25 #=> 25
x #=> 25
```
### Damit funktionieren auch mehrfache Zuweisungen
```
x = y = 10 #=> 10
x #=> 10
y #=> 10
```
## Benennung
### Konvention ist snake_case
```
snake_case = true
```
### Benutze verständliche Variablennamen
```
path_to_project_root = '/good/name/'
path = '/bad/name/'
```
# Symbols (sind auch Objekte)
Symbols sind unveränderliche, wiederverwendbare Konstanten, welche intern
als integer repräsentiert werden. Sie werden häufig anstelle von Strings
verwendet, um sinnvoll Werte zu übermitteln.
Symbols werden mit dem Doppelpunkt gekennzeichnet.

```
:pending.class #=> Symbol
status = :pending
status == :pending #=> true
status == 'pending' #=> false
status == :approved #=> false
```
# Arrays

## Ein Array anlegen
```
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
```

## Array können verschiedene Typen beinhalten
```
[1, 'hello', false] #=> [1, "hello", false]
```

## Wie bei arithmetischen Ausdrücken auch wird beim Zugriff auf
## [0] eigentlich die Methode [] des Array Objekts aufgerufen.
```
array.[] 0 #=> 1
array.[] 12 #=> nil
```

## Arrays können von vorne indiziert werden
```
array[0] #=> 1
array[12] #=> nil
```

## Arrays können von hinten indiziert werden
```
array[-1] #=> 5
```

## Arrays können mit Start Index und Länge indiziert werden
```
array[2, 3] #=> [3, 4, 5]
```

## Arrays können mit einer Range indiziert werden
```
array[1..3] #=> [2, 3, 4]
```

## Einen Wert hinzufügen
```
array << 6 #=> [1, 2, 3, 4, 5, 6]
array.push(6) #=> [1, 2, 3, 4, 5, 6]
```

## Testen, ob ein Element schon vorhanden ist
```
array.include?(1) #=> true
```

# Hashes
Hashes sind das Hauptfeature um Key/Values zu speichern

## Ein Hash anlegen
```
hash = { 'color' => 'green', 'number' => 5 }
hash.keys #=> ['color', 'number']
```

## Wert per key herausfinden
```
hash['color'] #=> 'green'
hash['number'] #=> 5
hash['nothing here'] #=> nil
// Fragen an einen Hash nach einem Schlüssel, der nicht existiert, ruft nil hervor:
```

##  Symbols können auch keys sein
```
new_hash = { defcon: 3, action: true }
new_hash.keys #=> [:defcon, :action]
```

## Testen ob ein Key oder ein Value existiert
```
new_hash.has_key?(:defcon) #=> true
new_hash.has_value?(3) #=> true
```

### Tipp:  Arrays und Hashes sind Enumerable
### Und haben gemeinsame, hilfreiche Methoden wie:
### each, map, count, and more

# Kontrolstrukturen
## if
```
if true
  'if statement'
elsif false
  'else if, optional'
else
  'else, also optional'
end
```
## for - Allerdings werden for Schleifen nicht oft vewendet.
```
for counter in 1..5
  puts "iteration #{counter}"
end
```
## Stattdessen: "each" Methode und einen Bloch übergeben
Ein Block ist ein Codeteil, den man einer Methode übergeben kann
Ähnelt stark lambdas, anonymen Funktionen oder Closures in anderen
Programmiersprachen.

```
(1..5).each do |counter|
  puts "iteration #{counter}"
end
```

Die each Methode einer Range führt den Block für jedes Element der Range aus.

Dem Block wird ein "counter" parameter übergeben.

### Den Block kann man auch in geschweiften Klammern schreiben
```
(1..5).each { |counter| puts "iteration #{counter}" }
```

### Each kann auch über den Inhalt von Datenstrukturen iterieren
```
array.each do |element|
  puts "#{element} is part of the array"
end
hash.each do |key, value|
  puts "#{key} is #{value}"
end

counter = 1
while counter <= 5 do
  puts "iteration #{counter}"
  counter += 1
end
```

## case
```
grade = 'B'

case grade
when 'A'
  puts 'Way to go kiddo'
when 'B'
  puts 'Better luck next time'
when 'C'
  puts 'You can do better'
when 'D'
  puts 'Scraping through'
when 'F'
  puts 'You failed!'
else
  puts 'Alternative grading system, eh?'
end
=> "Better luck next time"
```

### Case können auch ranges
```
grade = 82
case grade
when 90..100
  puts 'Hooray!'
when 80...90
  puts 'OK job'
else
  puts 'You failed!'
end
=> "OK job"
```

# Exception handling:
```
begin
  # code here that might raise an exception
  raise NoMemoryError, 'You ran out of memory.'
rescue NoMemoryError => exception_variable
  puts 'NoMemoryError was raised', exception_variable
rescue RuntimeError => other_exception_variable
  puts 'RuntimeError was raised now'
else
  puts 'This runs if no exceptions were thrown at all'
ensure
  puts 'This code always runs no matter what'
end
```
# Funktionen
```
def double(x)
  x * 2
end
```
## Funktionen (und Blocks)
## geben implizit den Wert des letzten Statements zurück
```
double(2) #=> 4
```

### Klammern sind optional wenn das Ergebnis nicht mehrdeutig ist
```
double 3 #=> 6
double double 3 #=> 12
def sum(x, y)
  x + y
end
```

### Methoden Parameter werden per Komma getrennt
```
sum 3, 4 #=> 7
sum sum(3, 4), 5 #=> 12
```

## yield
### Alle Methoden haben einen impliziten, optionalen block Parameter
### Dieser wird mit dem Schlüsselword "yield" aufgerufen
```
def surround
  puts '{'
  yield
  puts '}'
end
surround { puts 'hello world' }
```

## Einen Block kann man auch einer Methoden übergeben
### "&" kennzeichnet die Referenz zum übergebenen Block
```
def guests(&block)
  block.call 'some_argument'
end
```

### Eine Liste von Parametern kann man auch übergeben,
### Diese wird in ein Array konvertiert
### "*" kennzeichnet dies.
```
def guests(*array)
  array.each { |guest| puts guest }
end
```
# Klassen
## Werden mit dem class Schlüsselwort definiert
```
class Human
```

### Konstruktor bzw. Initializer
```
  def initialize(name, age = 0)
    # Assign the argument to the "name" instance variable for the instance
    @name = name
    # If no age given, we will fall back to the default in the arguments list.
    @age = age
  end
```

### setter Methode
```
  def name=(name)
    @name = name
  end
```
### getter Methode
```
  def name
    @name
  end
```

#### getter können mit der attr_accessor Methode vereinfacht definiert werden
```
  attr_accessor :name
  # Getter/setter methods can also be created individually like this
  attr_reader :name
  attr_writer :name
  # A class method uses self to distinguish from instance methods.
  # It can only be called on the class, not an instance.
  def self.say(msg)
    puts msg
  end
  def species
    @@species
  end
end
```

## Eine Klasse instanziieren
```
jim = Human.new('Jim Halpert')
dwight = Human.new('Dwight K. Schrute')
```

## Methodenaufrufe
```
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"
```

## Eine Klassenmethode aufrufen
```
Human.say('Hi') #=> "Hi"
```

## Variable Gültigkeit
### Variablen die mit "$" starten, gelten global
```
$var = "I'm a global var"
defined? $var #=> "global-variable"
```

### Variablen die mit "@" starten, gelten für die Instanz
```
@var = "I'm an instance var"
defined? @var #=> "instance-variable"
```

### Variablen die mit "@@" starten, gelten für die Klasse
```
@@var = "I'm a class var"
defined? @@var #=> "class variable"
```

### Variablen die mit einem Großbuchstaben anfangen, sind Konstanten
```
Var = "I'm a constant"
defined? Var #=> "constant"
```

## Class ist auch ein Objekt
### Hat also auch Instanzvariablen
### Eine Klassenvariable wird innerhalb der Klasse und Ableitungen geteilt.

### Basis Klasse
```
class Human
  @@foo = 0
  def self.foo
    @@foo
  end
  def self.foo=(value)
    @@foo = value
  end
end
```

### Abgeleitete Klasse
```
class Worker < Human
end
Human.foo # 0
Worker.foo # 0
Human.foo = 2 # 2
Worker.foo # 2
```

### Eine Klasseninstanzvariable wird nicht geteilt
```
class Human
  @bar = 0
  def self.bar
    @bar
  end
  def self.bar=(value)
    @bar = value
  end
end
```
```
class Doctor < Human
end
```
```
Human.bar # 0
Doctor.bar # nil
```
```
module ModuleExample
  def foo
    'foo'
  end
end
```
### Module einbinden, heisst ihre Methoden an die Instanzen der Klasse zu binden
### Module erweitern, heisst ihre Mothden an die Klasse selbst zu binden
```
class Person
  include ModuleExample
end
```
```
class Book
  extend ModuleExample
end
```
```
Person.foo     # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => NoMethodError: undefined method `foo'
```
### Callbacks werden ausgeführt, wenn ein Modul eingebunden oder erweitert wird
```
  module ConcernExample
    def self.included(base)
      base.extend(ClassMethods)
      base.send(:include, InstanceMethods)
    end
    module ClassMethods
      def bar
        'bar'
      end
    end
    module InstanceMethods
      def qux
        'qux'
      end
    end
  end
  class Something
    include ConcernExample
  end
```
```
Something.bar     # => 'bar'
Something.qux     # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
```

## Weiterführende Hinweise

//EN

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges.
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.
---
category: tool
tool: ruby ecosystem
contributors:
    - ["Jon Smock", "http://github.com/jonsmock"]
    - ["Rafal Chmiel", "http://github.com/rafalchmiel"]
translators:
  - ["Christian Albrecht", "https://github.com/coastalchief"]
filename: ruby-ecosystem-de.txt
lang: de-de
---

Hier gibt es einen Überblick über die gängigsten Tools zur Verwaltung  
von verschiedenen Ruby Versionen, Gems und Dependencies.  

## Ruby Managers

Einige Betriebssysteme haben bereits eine Ruby Version vorinstalliert  
oder bieten sie als Package zum Download an. Die meisten Rubyisten  
benutzen diese aber eher nicht und wenn, dann um damit einen Ruby  
Manager zu installieren. Damit kann man komfortabel zwischen den  
verschiedenen Versionen hin und herspringen.  

Dies sind die beliebtesten:

* [RVM](https://rvm.io/) - Installiert und wechselt zwischen rubies
  RVM kennt verschiedene Ruby Versionen und hat das Konzept der gemsets,  
  um gem Abhängigkeiten pro Projekt zu managen.  
* [ruby-build](https://github.com/sstephenson/ruby-build)  
  Installiert nur rubies, kann diese aber sehr gut verwalten
* [rbenv](https://github.com/sstephenson/rbenv) - Wechselt Ruby Versionen.
  Wird zusammen mit ruby-build benutzt. Hiermit kann man kontrollieren,  
  wie rubies laden.
* [chruby](https://github.com/postmodern/chruby) - Wechselt Ruby Versionen.
  Ähnlich rbenv.

## Ruby Versionen

Ruby wurde von Yukihiro "Matz" Matsumoto vor gut 20 Jahren veröffentlicht.  
Matz ist nach wie vor in die Entwicklung involviert. Daher kommt auch der  
Name der Referenzimplementierung: MRI (Matz' Reference Implementation).  

Die aktuellste Version ist **2.2.3** und wurde im August 2015 veröffentlicht!  

Hier eine kurze Versionshistorie:

* 2.0.0 - Release im Februar 2013  -- Release zum 20. Geburtstag der Sprache
  [Rubies are forever](http://www.heise.de/developer/artikel/Ruby-2-0-als-Geschenk-zum-20-Geburtstag-1808109.html)
* 1.9.3 - Release im Oktober 2011  
  [End of Life](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/)
* 1.8.7 - Release im Juni 2006  
  [End of Life](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/).

Die Veränderung zwischen 1.8.7 und 1.9.x war sehr groß und eine Migration  
nicht so einfach möglich. Der Versionssprung auf 2.0.0 war verglichen dazu  
weit weniger dramatisch.  
Beispielsweise hat 1.9. Encodings und eine Bytecode VM eingeführt.  
Es gibt immer noch Projekte die auf der stabilen Version 1.8.7 laufen,  
aber diese sind mittlerweile in der Minderheit. Die meisten Projekte  
laufen auf 1.9.x oder auf 2.x.

## Ruby Implementierungen

Das Ruby Ecosystem beinhaltet viele verschiedene Implementierungen von Ruby,  
jedes mit seinen eigenen Vorteilen und verschiedenen Graden von  
Kompatibilität. Auch wenn alle diese Implementierungen in verschiedenen  
Sprachen geschrieben sind, sind sie doch **alle Ruby**.  
Jede Implementierung bietet neben ihren speziellen Features immer auch  
die Möglichkeit normale ruby Dateien auszuführen.

Am ausgereiftesten und stabilsten:

* [MRI](https://github.com/ruby/ruby) - Geschrieben in C, das ist die Referenz Implementierung.
  Sie ist 100% kompatibel (mit sich selbst ;-). Alle anderen rubies 
  bleiben kompatibel mit MRI (siehe [RubySpec](#rubyspec) weiter unten).
* [JRuby](http://jruby.org/) - Geschrieben in Java and Ruby, Robust und ziemlich schnell.
  Der größte Vorteil von JRuby ist die Interoperabilität mit JVM/Java und damit die  
  Benutzung von Ruby im Java Ecosystem.
* [Rubinius](http://rubini.us/) - Geschrieben in Ruby mit C++ bytecode VM. 
  Auch sehr ausgereift und schnell. 

Mittel ausgereift / kompatibel:

* [Maglev](http://maglev.github.io/) - Baut auf Gemstone, ein Smalltalk VM.
  Dieses Projekt versucht das großartige Smalltalk Tooling in die Ruby Welt  
  zu bringen.
* [RubyMotion](http://www.rubymotion.com/) - Ruby in der iOS Entwicklung.

Weniger ausgereift/kompatibel:

* [Topaz](http://topazruby.com/) - Geschrieben in RPython (via PyPy)
  Topaz ist noch ziemlich jung und versucht die schnellste Implementierung  
  zu werden.
* [IronRuby](http://ironruby.net/) - Geschrieben in C# für die .NET Plaftform  
  Das letzte Release von IronRuby ist mittlerweile 5 Jahre her. 

Die Ruby Implementierungen haben ihre eigenen Versionsnummern, sind aber  
trotzdem immer zu einer MRI Version kompatibel.  
Viele können sogar zwischen verschiedenen Modi wechseln (1.8 mode -> 1.9 mode)

## RubySpec

Die meisten Ruby Implementierungen vertrauen der [RubySpec](http://rubyspec.org/).  
sehr stark. Da Ruby keine offizielle Spezifikation hat, hat die  
Community ausführbare Specs (in Ruby) geschrieben, um so die Kompatibilität  
zur MRI testen zu können.

## RubyGems

[RubyGems](http://rubygems.org/) ist der Community Paket Manager von Ruby.  
RubyGems kommt mit Ruby zusammen, so dass kein extra Tool nötig ist.  
  
Ruby Pakete werden "gems" genannt und könnten auf RubyGems.org  
veröffentlicht werden. Jedes Gem enthält den Source Code und Meta Daten,  
wie die Versionsnummer, weitere Abhängigkeiten, Autoren und Lizenzen.

## Bundler

[Bundler](http://bundler.io/) ist ein Tool um Abhängigkeiten zwischen  
Gems aufzulösen und zu managen. Dazu werden diese in einem gemfile  
zusammengefasst und Bundler kümmert sich darum die Abhängigkeiten  
untereinander rekursiv aufzulösen. Entweder es klappt und alle gems  
konnten runtergeladen werden, oder es wird abgebrochen und  
der Konflikt gemeldet.  
Zum Beispiel:  
Wenn Gem A die Version 3 oder höher von Gem Z braucht, aber Gem B  
von Gem Z die Version 2, dann ist das ein Konflikt.  

# Testing

Test-Driven Development ist ein essentieller Teil der Ruby Kultur.  
Ruby bringt sein eigenes Unit-Test framework mit, minitest. Darüberhinaus  
gibt es noch viele weitere Testframeworks mit unterschiedlichsten Zielen:

* [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html) - Eingebaut in Ruby 1.8
  "Unit-style" Testframework
* [minitest](http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html) - Eingebaut in Ruby 1.9/2.0
  "Unit-style" Testframework
* [RSpec](http://rspec.info/) - Ein Testframework welches auf verständliche Testdefinition setzt
* [Cucumber](http://cukes.info/) - Ein BDD Testframework welches Gherkin tests parsen kann

## Be Nice
Die Ruby Community ist stolz darauf eine offene, vielfältige und einladende  
Community zu sein. Es gibt viele aktive Ruby User Gruppen und diverse  
Ruby Konferenzen. Matz selbst ist so oft es geht dabei.

* [Euruko](http://www.euruko2015.org)
* [User Groups](https://www.ruby-lang.org/de/community/user-groups/)

---
language: rust
contributors:
    - ["P1start", "http://p1start.github.io/"]
translators:
    - ["Christian Albrecht", "https://github.com/coastalchief"]
lang: de-de
filename: lernerust-de.rs
---

Rust ist eine Programmiersprache von Mozilla Research.  
Rust vereint Sicherheit, Nebenläufigkeit und eine hohe Praxistauglichkeit.  
  
Sicherheit bedeuted, dass Programmierfehler ausgeschlossen werden, die zu  
Speicherzugriffsfehlern führen könnten. Das funktioniert u.a. dadurch, dass  
es keinen Garbage Collector gibt, sondern ein besonderes Typsystem.  
  
Das erste Release von Rust, 0.1, wurde im Januar 2012 veröffentlicht.  
In den nächsten drei Jahren wurde die Sprache so schnell und aktiv weiter-  
entwickelt, dass es einfach keine stabile gab und geraten wurde den  
nightly build zu nutzen.  
  
Am 15. Mai 2015 wurde Rust 1.0 freigegeben, und zwar mit der Garantie einer  
Abwärtskompatabilität. Verbesserungen der Kompilierzeit und andere Compiler  
verbesserungen finden im Moment im nightly build statt. Von Rust gibt es im  
Moment ungefähr alle sechs Wochen ein Release. Rust 1.1 beta wurde zusammen  
mit dem 1.0 Release zur Verfügung gestellt.  
  
Obwohl Rust eine ziemlich low-level Sprache ist, vereint sie Ansätze aus  
der Welt der funktionalen, der objektorientierten und der nebenläufigen  
Programmierung. Dadurch kann in Rust nicht nur schnell, sondern auch sehr  
effizient entwickelt werden.  
  
  
```rust
// Dies ist ein Kommentar. Ein einzeiliger...
/* ...und multi-zeilen Kommentare sehe so aus */

/////////////////////
// 0. Installation //
/////////////////////
// Stabile binaries gibt es unter https://www.rust-lang.org/downloads.html

// Programme werden in .rs Dateien geschrieben also zum Beispiel  
// "main.rs" und dann kompiliert "rustc main.rs"  
// Herauskommt eine ausführbare Datei "main"  
// Für dieses Tutorial reicht das vollkommen aus. Für größere Projekte  
// sollte das unten beschriebene Cargo angeschaut werden.  

// Cargo  
// Ein gängiges Tool um Rust Projekte zu verwalten ist Cargo. Es macht im  
// wesentlichen drei Dinge: Code bauen, Dependencies laden und  
// Dependencies bauen.  
// Um ein vorhandenes Projekt zu cargo-ifyen müssen drei Dinge gemacht werden  
// * Erstelle eine Cargo.toml Konfigurationsdatei  
// * Verschiebe Source Code in ein src Verzeichnis  
// * Lösche das alte Executable  
//
// 'cargo build' baut den Code  
// 'cargo run' baut und führt das Programm aus  

///////////////
// 1. Basics //
///////////////

// Funktionen
// `i32` ist der Typ für einen 32-bit signed Integer
fn add2(x: i32, y: i32) -> i32 {
    // Impliziter return (kein Semikolon)
    x + y
}

// Main Funktion
fn main() {
    // Zahlen //

    // Unveränderliche Variable
    let x: i32 = 1;

    // Integer/float Suffixe
    let y: i32 = 13i32;
    let f: f64 = 1.3f64;

    // Type inference
    Meistens kann der Rust Compiler selbst schlussfolgern, von welchem
    Typ eine Variable ist, so dass man den Typ nicht explizit angeben muss.
    In diesem Tutorial werden Typen explizit angegeben, um zu demonstrieren,
    welche Möglichkeiten es gibt. Wenn man damit vertraut ist, kann man die
    Typen auch weglassen und die Type Inference hilft dann im Hintergrund.

    let implicit_x = 1;
    let implicit_f = 1.3;

    // Arithmetik
    let sum = x + y + 13;

    // Veränderliche Variable
    let mut mutable = 1;
    mutable = 4;
    mutable += 2;

    // Strings //
    // Strings gibt es in zwei Typen: &str und String

    // Zunächst &str
    let x: &str = "hello world!";

    // Ausgabe
    println!("{} {}", f, x); // 1.3 hello world

    // Ein `String` – heap-allokierter String
    let s: String = "hello world".to_string();

    // Ein string slice – ist eigentlich ein unveränderlicher Pointer
    // auf einen String – er enthält nicht den Inhalt den String, sondern
    // eben nur den Pointer auf etwas, dass den Inhalt kennt:
    // (In diesem Fall, `s`)
    let s_slice: &str = &s;

    // Ausgabe
    println!("{} {}", s, s_slice); // hello world hello world

    // Vektoren/Arrays //

    // Ein Array mit fester Größe
    let vier_ints: [i32; 4] = [1, 2, 3, 4];

    // Ein dynamisches Array (Vektorentor)
    let mut vector: Vec<i32> = vec![1, 2, 3, 4];
    vector.push(5);

    // Ein slice – eine unveränderliche Ansicht, oder Pointer auf einen
    // Vektor oder ein Array. Wie bei Strings, nur eben bei Vektoren
    let slice: &[i32] = &vector;

    // Benutze `{:?}` um eine debug Ausgabe zu erzeugen
    println!("{:?} {:?}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    // Tuples //

    // Ein Tuple ist eine Liste mit fester Größe und kann Werte
    // von unterschiedlichen Typen enthalten
    let x: (i32, &str, f64) = (1, "hello", 3.4);

    // Werte aus Vektor mit `let` destrukturieren
    let (a, b, c) = x;
    println!("{} {} {}", a, b, c); // 1 hello 3.4

    // Vektor Indizes
    println!("{}", x.1); // hello

    //////////////
    // 2. Typen //
    //////////////

    // Struct
    struct Punkt {
        x: i32,
        y: i32,
    }

    let anfang: Punkt = Punkt { x: 0, y: 0 };

    // Ein struct mit unbenannten Felder heisst ‘tuple struct’
    struct Punkt2(i32, i32);

    let anfang2 = Punkt2(0, 0);

    // Einfache enum, so ähnlich wie in C
    enum Richtung {
        Links,
        Rechts,
        Hoch,
        Runter,
    }

    let hoch = Richtung::Hoch;

    // Enum mit Feldern
    enum OptionalI32 {
        EinI32(i32),
        Nix,
    }

    let zwei: OptionalI32 = OptionalI32::EinI32(2);
    let nix = OptionalI32::Nix;

    // Generics //

    struct Foo<T> { bar: T }

    // In der Standard Bibliothek heisst das hier `Option`
    enum Optional<T> {
        EinWert(T),
        KeinWert,
    }

    // Methoden //

    impl<T> Foo<T> {
        // Methoden erwarten einen `self` Parameter
        fn get_bar(self) -> T {
            self.bar
        }
    }

    let a_foo = Foo { bar: 1 };
    println!("{}", a_foo.get_bar()); // 1

    // Traits (vergleichbar mit Interfaces oder Typklassen in anderen Sprachen)
    // In Traits werden nur Method Signaturen erstellt.
    // Die Implementierung findet im impl statt.

    trait MacheIrgendwas<T> {
        fn macheIrgendwas(self) -> Option<T>;
    }

    impl<T> MacheIrgendwas<T> for Foo<T> {
        fn macheIrgendwas(self) -> Option<T> {
            mache(self.bar)
        }
    }

    let anderes_foo = Foo { bar: 1 };
    println!("{:?}", anderes_foo.macheIrgendwas()); // mache(1)

    /////////////////////////
    // 3. Pattern matching //
    /////////////////////////

    let foo = OptionalI32::AnI32(1);
    match foo {
        OptionalI32::EinI32(n) => println!("hier ist ein i32: {}", n),
        OptionalI32::Nix  => println!("hier ist nix!"),
    }

    // Advanced pattern matching
    struct FooBar { x: i32, y: OptionalI32 }
    let bar = FooBar { x: 15, y: OptionalI32::EinI32(32) };

    match bar {
        FooBar { x: 0, y: OptionalI32::EinI32(0) } =>
            println!("Beide Zahlen sind 0!"),
        FooBar { x: n, y: OptionalI32::EinI32(m) } if n == m =>
            println!("Beide Zahlen sind gleich"),
        FooBar { x: n, y: OptionalI32::EinI32(m) } =>
            println!("Zahlen sind unterschiedlich: {} {}", n, m),
        FooBar { x: _, y: OptionalI32::Nix } =>
            println!("Die zweite Zahl ist leer!"),
    }

    /////////////////////
    // 4. Control      //
    /////////////////////

    // `for` Schleife/Iterationen
    let array = [1, 2, 3];
    for i in array.iter() {
        println!("{}", i);
    }

    // Ranges
    for i in 0u32..10 {
        print!("{} ", i);
    }
    println!("");
    // gibt aus: `0 1 2 3 4 5 6 7 8 9 `

    // `if`
    if 1 == 1 {
        println!("Mathe ist klappt!");
    } else {
        println!("Oh nein...");
    }

    // `if` als Ausdruck
    let wert = if true {
        "gut"
    } else {
        "schlecht"
    };

    // `while` Schleife
    while 1 == 1 {
        println!("Läuft...");
    }

    // Unendliche Schleifen
    loop {
        println!("Hello!");
    }

    /////////////////////////////////////
    // 5. Speichersicherheit & Pointer //
    /////////////////////////////////////

    // Owned pointer – nur eine Sache kann einen Pointer 'besitzen'.
    // Das heisst, wenn das Objekt `Box` seinen scope verlässt oder verliert,
    // wird es automatisch im Speicher de-allokiert.
    let mut mine: Box<i32> = Box::new(3);
    // Jetzt wird die Box dereferenziert
    *mine = 5; 
    // Jetzt geht `mine` in den Besitz von `now_its_mine` über. 
    // `mine` wird verschoben.
    let mut now_its_mine = mine;
    *now_its_mine += 2;

    println!("{}", now_its_mine); // ergibt 7

    // Das würde nicht kompilieren, da `now_its_mine` jetzt den Pointer besitzt
    // println!("{}", mine); 

    // Reference – ein unveränderlicher Pointer der fremde Daten referenziert
    // Wenn eine Referenz auf einen Wert gesetzt wird, heisst das, dass man den
    // Wert ausleiht (‘borrowed’).
    // Ein ausgeliehener Wert ist unveränderlich und lebt solange wie der
    // Scope existiert, in dem er erstellt wurde.
    let mut var = 4;
    var = 3;
    let ref_var: &i32 = &var;

    println!("{}", var); // Anders als `mine`, `var` kann hier weiter verwendet werden
    println!("{}", *ref_var);
    // var = 5; // das kompiliert nicht, da `var` ausgeliehen ist
    // *ref_var = 6; // das kompiliert auch nicht, da `ref_var` eine unveränderliche Referenz ist

    // Veränderliche Referenzen
    // Solange ein Wert veränderlich geliehen wurde, kann man nicht darauf zugreifen
    let mut var2 = 4;
    let ref_var2: &mut i32 = &mut var2;
    *ref_var2 += 2;         // '*' wird benutzt um auf den veränderlich geliehenen Wert var2 zu zeigen

    println!("{}", *ref_var2); // 6 , //var2 würde nicht kompilieren. //ref_var2 ist vom Typ &mut i32, also                                                      //stores a reference to an i32 not the value.
    // var2 = 2; // würde das nicht kompilieren, da `var2` geliehen wurde.
}
```

## Weitere Informationen

Es gibt eine ganze Reihe mehr über Rust zu sagen. Dieser Text gibt nur einen  
Einblick in die wichtigsten Sprachmerkmale.  
Um mehr über Rust zu erfahren, sollte man mit den folgenden Stellen starten:

1. Englisch:  
  * [Die offizielle Rust Webseite](http://rust-lang.org)
  * [The Rust Programming Language](https://doc.rust-lang.org/stable/book/README.html)
  * [/r/rust](http://reddit.com/r/rust)
  * the #rust channel on irc.mozilla.org 
  
2. Deutsch
  * [Rust Wikipedia](https://de.wikipedia.org/wiki/Rust_(Programmiersprache))
  * [Artikel im LinuxMagazin](http://www.linux-magazin.de/Ausgaben/2015/08/Rust)
---
language: sass
filename: learnsass-de.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
  - ["Sean Corrales", "https://github.com/droidenator"]
  - ["Kyle Mendes", "https://github.com/pink401k"]
translators:
    - ["Philipp Bochmann", "https://github.com/phbo85"]  
lang: de-de
---
Sass ist eine CSS-erweiternde Sprache, welche Features wie Variablen, Verschachtelung, Mixins und mehr hinzufügt.
Sass (und andere Präprozessoren wie [Less](http://lesscss.org/)) helfen Entwicklern dabei ihren Code wartbar und DRY (Don't Repeat Yourself - wiederhole dich nicht) zu schreiben.

Sass hat zwei verschiedene Syntax-Optionen. SCSS, mit der gleichen Syntax wie CSS aber mit den zusätzlichen Features von Sass. Oder Sass (die originale Syntax), welche Einrückung statt geschweiften Klammern und Semikolons benutzt.
Dieses Tutorial wurde mit SCSS geschrieben.

Wenn du bereits mit CSS3 vertraut bist, wirst du dir Sass relativ schnell aneignen. Es bietet keine neuen Styling-Eigenschaft, sondern Werkzeuge mit denen du dein CSS effizienter schreiben kannst und die Wartung viel einfacher machst.


```scss


//Einzeilige Kommentare werden entfernt, wenn Sass zu CSS kompiliert wird.

/* Mehrzeilige Kommentare bleiben bestehen. */



/* Variablen
============================== */



/* Du kannst einen CSS-Wert (wie eine Farbe) in einer Variable speichern.
Benutze das '$'-Zeichen um eine Variable zu erstellen. */

$primary-color: #A3A4FF;
$secondary-color: #51527F;
$body-font: 'Roboto', sans-serif;

/* Du kannst die Variablen überall in deinem Stylesheet verwenden.
Wenn du nun eine Farbe ändern willst, musst du das nur einmal tun. */

body {
	background-color: $primary-color;
	color: $secondary-color;
	font-family: $body-font;
}

/* Das wird kompiliert zu: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/* Dies ist viel besser wartbar als die Farbe
an jeder Stelle im Stylesheet einzeln ändern zu müssen. */



/* Mixins
============================== */



/* Wenn du merkst, dass du den gleichen Code für mehr als ein
Element schreiben musst, kannst du ihn in einem mixin speichern.

Dazu benutzt du '@mixin' plus einem Namen für dein mixin. */

@mixin center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* Du kannst das mixin mit '@include' und dem Namen des mixin benutzen. */

div {
	@include center;
	background-color: $primary-color;
}

/* Das kompiliert zu: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}


/* Du kannst Mixins benutzen, um shorthand Eigenschaften zu erstellen. */

@mixin size($width, $height) {
	width: $width;
	height: $height;
}

/* Diese kannst du aufrufen und width und height als Parameter übergeben. */

.rectangle {
	@include size(100px, 60px);
}

.square {
	@include size(40px, 40px);
}

/* Compiles to: */
.rectangle {
  width: 100px;
  height: 60px;
}

.square {
  width: 40px;
  height: 40px;
}



/* Funktionen
============================== */



/* Sass bietet Funktionen, welche benutzt werden können um eine Reihe
   von Aufgaben zu bewältigen. Berücksichtige das Folgende: */

/* Funktionen können aufgerufen werden indem du ihren Namen benutzt
   und die benötigten Parameter übergibst. */
body {
  width: round(10.25px);
}

.footer {
  background-color: fade_out(#000000, 0.25)
}

/* Kompiliert: */

body {
  width: 10px;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* Du kannst auch deine eigenen Funktionen definieren. Funktionen ähneln
   Mixins. Wenn du zwischen Funktionen und Mixins auswählen musst, denke
   daran, dass Mixins am besten zur Generierung von CSS eignen, während
   Funktionen besser für Logik in deinem Sass Code genutzt werden. Die
   Beispiele mit in der Sektion "Mathematische Operatoren" sind ideale
   Kandidaten für wiederverwendbare Funktionen. */

/* Diese Funktion errechnet den Prozentwert aus target-size und parent-size
   und gibt diesen zurück. */

@function calculate-percentage($target-size, $parent-size) {
  @return $target-size / $parent-size * 100%;
}

$main-content: calculate-percentage(600px, 960px);

.main-content {
  width: $main-content;
}

.sidebar {
  width: calculate-percentage(300px, 960px);
}

/* Kompiliert: */

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}



/* Extend (Vererbung)
============================== */



/* Extend ist ein Weg um Eigenschaften eines Selektoren mit einem anderem
   zu teilen. */

.display {
	@include size(5em, 5em);
	border: 5px solid $secondary-color;
}

.display-success {
	@extend .display;
	border-color: #22df56;
}

/* Kompiliert: */
.display, .display-success {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F;
}

.display-success {
  border-color: #22df56;
}

/* Aufgrund der Art wie Sass die Klassen zusammen gruppiert, welche
   alle das gleiche Grund-Styling haben, ist Extend der Erstellung
   eines Mixins vorzuziehen. Wenn dies mit einem Mixin gemacht worden
   wäre, würden width, height und border für jedes Element dupliziert
   werden, welches das Mixin aufruft. Dies beeinflusst zwar nicht
   deinen Workflow, bläht aber die vom Sass-Compiler erzeugten Dateien
   unnötige auf. */



/* Nesting (Verschachtelung)
============================== */



/* Sass erlaubt es Selektoren in Selektoren zu verschachteln. */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #FF0000;
	}
}

/* '&' wird durch den übergeordneten Selektor ersetzt. */
/* Du kannst auch Pseudo-Klassen verschachteln. */
/* Denk daran, dass zu viel Verschachtelung deinen Code schlechter
   wartbar macht.
   Die Best Practices empfehlen nicht mehr als 3 Ebenen zu verschachteln.
   Zum Beispiel: */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Kompiliert: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/* Partials und Imports
============================== */



/* Sass erlaubt dir das Erstellen partieller Dateien (partials).
   Das hilft dir modularisierten Sass Code zu schreiben.
   Partielle Dateien fangen mit einem '_' an, z.B. _reset.css.
   Partielle Dateien werden nicht zu CSS generiert. */

/* Schau dir folgendes CSS an, was wir in einer Datei namens _reset.css haben */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Mit @import kannst du in Sass partielle Dateien importieren.
   Dies unterscheidet sich vom traditionellen CSS @import Statement
   welches einen neuen HTTP Request macht, um die zu importierende Datei
   zu holen. Sass nimmt die importierte Datei und kombiniert sie mit
   dem kompilierten Code. */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* Kompiliert: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/* Platzhalter Selektoren
============================== */



/* Platzhalter sind nützlich, um ein CSS Statement zum Erweitern zu
   erstellen. Wenn du ein CSS Statement erstellst, welches du ausschließlich
   zur Verwendung mit @extend nutzen willst, kannst du das mit einem
   Platzhalter tun. Platzhalter fangen mit einem '%' statt einem '.'
   oder '#' an und erscheinen nicht im kompilierten CSS. */

%content-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  @extend %content-window;
  background-color: #0000ff;
}

/* Kompiliert: */

.message-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  background-color: #0000ff;
}



/* Mathematische Operationen
============================== */



/* Sass bietet die folgenden Operatoren: +, -, *, /, und %. Diese können
   nützlich sein, wenn du Werte direkt in Sass berechnen willst, anstatt
   vorher manuell errechnete Werte zu verwenden. Unten folgt ein Beispiel
   für ein einfaches zweispaltiges Design. */

$content-area: 960px;
$main-content: 600px;
$sidebar-content: 300px;

$main-size: $main-content / $content-area * 100%;
$sidebar-size: $sidebar-content / $content-area * 100%;
$gutter: 100% - ($main-size + $sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: $main-size;
}

.sidebar {
  width: $sidebar-size;
}

.gutter {
  width: $gutter;
}

/* Compiles to: */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}

```

## SASS oder Sass?
Hast du dich jemals gefragt, ob Sass ein Akronym ist oder nicht? Hast du wahrscheinlich nicht, aber ich sage es dir trotzdem. Der Name der Sprache ist ein Wort, "Sass", und kein Akronym.
Da die Leute durchgehend "SASS" geschrieben haben, hat der Ersteller der Sprache es scherzhaft "Syntactically Awesome StyleSheets" genannt.

## Sass üben
Wenn du mit Sass in deinem Browser spielen willst, schau dir [SassMeister](http://sassmeister.com/) an.
Du kannst beide Syntax-Optionen benutzen, gehe einfach in die Einstellungen und wähle entweder Sass oder SCSS.

## Kompatibilität
Sass kann in jedem Projekt verwendet werden, solange du ein Programm hast, um es in CSS zu kompilieren.
Du solltest verifizieren, dass das CSS, was du verwendest, mit deinen Ziel-Browsern kompatibel ist.

[QuirksMode CSS](http://www.quirksmode.org/css/) und [CanIUse](http://caniuse.com) sind gute Resourcen um die Kompatibilät zu überpüfen.


## Literaturhinweise
* [Offizielle Dokumentation](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way](http://thesassway.com/) bietet Tutorials (Anfänger bis Fortgeschritten) und Artikel.
---
language: Scala
contributors:
    - ["George Petrov", "http://github.com/petrovg"]
    - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Ha-Duong Nguyen", "http://reference-error.org"]
    - ["Dennis Keller", "github.com/denniskeller"]
translators:
  - ["Christian Albrecht", "https://github.com/coastalchief"]
filename: learnscala-de.scala
lang: de-de
---

Scala ist eine funktionale und objektorientierte Programmiersprache  
für die Java Virtual Machine (JVM), um allgemeine Programmieraufgaben  
zu erledigen. Scala hat einen akademischen Hintergrund und wurde an  
der EPFL (Lausanne / Schweiz) unter der Leitung von Martin Odersky entwickelt.

```scala 
/*
Scala Umgebung einrichten:

1. Scala binaries herunterladen- http://www.scala-lang.org/downloads
2. Unzip/untar in ein Verzeichnis
3. das bin Unterverzeichnis der `PATH` Umgebungsvariable hinzufügen
4. Mit dem Kommando `scala` wird die REPL gestartet und zeigt als Prompt:

scala>

Die REPL (Read-Eval-Print Loop) ist der interaktive Scala Interpreter.  
Hier kann man jeden Scala Ausdruck verwenden und das Ergebnis wird direkt  
ausgegeben.  
Als nächstes beschäftigen wir uns mit ein paar Scala Basics.
*/


/////////////////////////////////////////////////
// 1. Basics
/////////////////////////////////////////////////

// Einzeilige Kommentare beginnen mit zwei Slashes

/*
  Mehrzeilige Kommentare, starten 
  mit einem Slash-Stern und enden mit einem Stern-Slash
*/

// Einen Wert, und eine zusätzliche neue Zeile ausgeben  

println("Hello world!")
println(10)


// Einen Wert, ohne eine zusätzliche neue Zeile ausgeben  

print("Hello world")

/*
  Variablen werden entweder mit var oder val deklariert.  
  Deklarationen mit val sind immutable, also unveränderlich  
  Deklarationen mit var sind mutable, also veränderlich  
  Immutability ist gut.  
*/
val x = 10 // x ist 10
x = 20     // error: reassignment to val
var y = 10
y = 20     // y ist jetzt 20

/*
Scala ist eine statisch getypte Sprache, auch wenn wir in dem o.g. Beispiel  
keine Typen an x und y geschrieben haben.  
In Scala ist etwas eingebaut, was sich Type Inference nennt. Das heißt das der  
Scala Compiler in den meisten Fällen erraten kann, von welchen Typ eine Variable ist,  
so dass der Typ nicht jedes mal angegeben werden muss.  
Einen Typ gibt man bei einer Variablendeklaration wie folgt an:  
*/
val z: Int = 10
val a: Double = 1.0


// Bei automatischer Umwandlung von Int auf Double wird aus 10 eine 10.0  

val b: Double = 10


// Boolean Werte  

true
false


// Boolean Operationen  

!true         // false
!false        // true
true == false // false
10 > 5        // true


// Mathematische Operationen sind wie gewohnt  

1 + 1   // 2
2 - 1   // 1
5 * 3   // 15
6 / 2   // 3
6 / 4   // 1
6.0 / 4 // 1.5


// Die Auswertung eines Ausdrucks in der REPL gibt den Typ  
// und das Ergebnis zurück.  

  scala> 1 + 7
  res29: Int = 8

/*
Das bedeutet, dass das Resultat der Auswertung von 1 + 7 ein Objekt  
von Typ Int ist und einen Wert 0 hat.  
"res29" ist ein sequentiell generierter name, um das Ergebnis des  
Ausdrucks zu speichern. Dieser Wert kann bei Dir anders sein...  
*/

"Scala strings werden in doppelten Anführungszeichen eingeschlossen"  
'a' // A Scala Char  
// 'Einzeln ge-quotete strings gibt es nicht!' <= This causes an error  
  
// Für Strings gibt es die üblichen Java Methoden  

"hello world".length
"hello world".substring(2, 6)
"hello world".replace("C", "3")


// Zusätzlich gibt es noch extra Scala Methoden  
// siehe: scala.collection.immutable.StringOps  

"hello world".take(5)
"hello world".drop(5)


// String interpolation: prefix "s"  

val n = 45
s"We have $n apples" // => "We have 45 apples"


// Ausdrücke im Innern von interpolierten Strings gibt es auch  

val a = Array(11, 9, 6)
val n = 100
s"My second daughter is ${a(0) - a(2)} years old."    // => "My second daughter is 5 years old."
s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples."
s"Power of 2: ${math.pow(2, 2)}"                      // => "Power of 2: 4"


// Formatierung der interpolierten Strings mit dem prefix "f"  

f"Power of 5: ${math.pow(5, 2)}%1.0f"         // "Power of 5: 25"
f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122: 11.0454"


// Raw Strings, ignorieren Sonderzeichen.  

raw"New line feed: \n. Carriage return: \r." // => "New line feed: \n. Carriage return: \r."


// Manche Zeichen müssen "escaped" werden, z.B.   
// ein doppeltes Anführungszeichen in innern eines Strings.  

"They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown""


// Dreifache Anführungszeichen erlauben es, dass ein String über mehrere Zeilen geht  
// und Anführungszeichen enthalten kann.  

val html = """<form id="daform">
                <p>Press belo', Joe</p>
                <input type="submit">
              </form>"""

  
/////////////////////////////////////////////////
// 2. Funktionen
/////////////////////////////////////////////////

// Funktionen werden so definiert  
//  
//   def functionName(args...): ReturnType = { body... }  
//  
// Beachte: Es gibt kein return Schlüsselwort. In Scala ist der letzte Ausdruck  
// in einer Funktion der Rückgabewert.  

def sumOfSquares(x: Int, y: Int): Int = {
  val x2 = x * x
  val y2 = y * y
  x2 + y2
}


// Die geschweiften Klammern können weggelassen werden, wenn  
// die Funktion nur aus einem einzigen Ausdruck besteht:  

def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y


// Syntax für Funktionsaufrufe:  

sumOfSquares(3, 4)  // => 25


// In den meisten Fällen (mit Ausnahme von rekursiven Funktionen), können  
// Rückgabetypen auch weggelassen werden, da dieselbe Typ Inference, wie bei  
// Variablen, auch bei Funktionen greift:  

def sq(x: Int) = x * x  // Compiler errät, dass der return type Int ist


// Funktionen können default parameter haben:  

def addWithDefault(x: Int, y: Int = 5) = x + y
addWithDefault(1, 2) // => 3
addWithDefault(1)    // => 6


// Anonyme Funktionen sehen so aus:  

(x: Int) => x * x


// Im Gegensatz zu def bei normalen Funktionen, kann bei anonymen Funktionen   
// sogar der Eingabetyp weggelassen werden, wenn der Kontext klar ist.  
// Beachte den Typ "Int => Int", dies beschreibt eine Funktion,  
// welche Int als Parameter erwartet und Int zurückgibt.  

val sq: Int => Int = x => x * x


// Anonyme Funktionen benutzt man ganz normal:  

sq(10)   // => 100


// Wenn ein Parameter einer anonymen Funktion nur einmal verwendet wird,  
// bietet Scala einen sehr kurzen Weg diesen Parameter zu benutzen,  
// indem die Parameter als Unterstrich "_" in der Parameterreihenfolge  
// verwendet werden. Diese anonymen Funktionen werden sehr häufig   
// verwendet.  

val addOne: Int => Int = _ + 1  
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)  
addOne(5)      // => 6  
weirdSum(2, 4) // => 16  


// Es gibt einen keyword return in Scala. Allerdings ist seine Verwendung  
// nicht immer ratsam und kann fehlerbehaftet sein. "return" gibt nur aus  
// dem innersten def, welches den return Ausdruck umgibt, zurück.  
// "return" hat keinen Effekt in anonymen Funktionen:  

def foo(x: Int): Int = {  
  val anonFunc: Int => Int = { z =>  
    if (z > 5)  
      return z // Zeile macht z zum return Wert von foo  
    else  
      z + 2    // Zeile ist der return Wert von anonFunc  
  }  
  anonFunc(x)  // Zeile ist der return Wert von foo  
}  


/////////////////////////////////////////////////
// 3. Flow Control
/////////////////////////////////////////////////

// Wertebereiche und Schleifen

1 to 5
val r = 1 to 5
r.foreach(println)
r foreach println
(5 to 1 by -1) foreach (println)
  
// Scala ist syntaktisch sehr großzügig, Semikolons am Zeilenende  
// sind optional, beim Aufruf von Methoden können die Punkte  
// und Klammern entfallen und Operatoren sind im Grunde austauschbare Methoden  

// while Schleife  

var i = 0
while (i < 10) { println("i " + i); i += 1 }
i    // i ausgeben, res3: Int = 10


// Beachte: while ist eine Schleife im klassischen Sinne -  
// Sie läuft sequentiell ab und verändert die loop-Variable.  
// While in Scala läuft schneller ab als in Java und die o.g.  
// Kombinatoren und Zusammenlegungen sind einfacher zu verstehen  
// und zu parellelisieren.

// Ein do while Schleife  

do {
  println("x ist immer noch weniger wie 10")
  x += 1
} while (x < 10)


// Endrekursionen sind ideomatisch um sich wiederholende  
// Dinge in Scala zu lösen. Rekursive Funtionen benötigen explizit einen  
// return Typ, der Compiler kann ihn nicht erraten.  
// Unit, in diesem Beispiel.  

def showNumbersInRange(a: Int, b: Int): Unit = {
  print(a)
  if (a < b)
    showNumbersInRange(a + 1, b)
}
showNumbersInRange(1, 14)


// Conditionals

val x = 10
if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println ("yeah") else println("nay")
println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"


/////////////////////////////////////////////////
// 4. Daten Strukturen (Array, Map, Set, Tuples)
/////////////////////////////////////////////////

// Array

val a = Array(1, 2, 3, 5, 8, 13)
a(0)
a(3)
a(21)    // Exception


// Map - Speichert Key-Value-Paare

val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
m("fork")
m("spoon")
m("bottle")       // Exception
val safeM = m.withDefaultValue("no lo se")
safeM("bottle")

// Set - Speichert Unikate, unsortiert (sortiert -> SortedSet)

val s = Set(1, 3, 7)
s(0) //false
s(1) //true
val s = Set(1,1,3,3,7)
s: scala.collection.immutable.Set[Int] = Set(1, 3, 7)

// Tuple - Speichert beliebige Daten und "verbindet" sie miteinander
// Ein Tuple ist keine Collection.  

(1, 2)
(4, 3, 2)
(1, 2, "three")
(a, 2, "three")


// Hier ist der Rückgabewert der Funktion ein Tuple  
// Die Funktion gibt das Ergebnis, so wie den Rest zurück.  

val divideInts = (x: Int, y: Int) => (x / y, x % y)
divideInts(10, 3)


// Um die Elemente eines Tuples anzusprechen, benutzt man diese  
// Notation: _._n wobei n der index des Elements ist (Index startet bei 1)  

val d = divideInts(10, 3)
d._1
d._2


/////////////////////////////////////////////////
// 5. Objektorientierte Programmierung
/////////////////////////////////////////////////

/*
  Bislang waren alle gezeigten Sprachelemente einfache Ausdrücke, welche zwar  
  zum Ausprobieren und Lernen in der REPL gut geeignet sind, jedoch in  
  einem Scala file selten alleine zu finden sind.  
  Die einzigen Top-Level Konstrukte in Scala sind nämlich:  

  - Klassen (classes)
  - Objekte (objects)
  - case classes
  - traits

  Diesen Sprachelemente wenden wir uns jetzt zu.  
*/

// Klassen

// Zum Erstellen von Objekten benötigt man eine Klasse, wie in vielen  
// anderen Sprachen auch. 

// erzeugt Klasse mit default Konstruktor  

class Hund
scala> val t = new Hund
t: Hund = Hund@7103745


// Der Konstruktor wird direkt hinter dem Klassennamen deklariert.  

class Hund(sorte: String)
scala> val t = new Hund("Dackel")
t: Hund = Hund@14be750c
scala> t.sorte //error: value sorte is not a member of Hund


// Per val wird aus dem Attribut ein unveränderliches Feld der Klasse  
// Per var wird aus dem Attribut ein veränderliches Feld der Klasse  

class Hund(val sorte: String)
scala> val t = new Hund("Dackel")
t: Hund = Hund@74a85515
scala> t.sorte
res18: String = Dackel


// Methoden werden mit def geschrieben  

def bark = "Woof, woof!"


// Felder und Methoden können public, protected und private sein  
// default ist public  
// private ist nur innerhalb des deklarierten Bereichs sichtbar  

class Hund {
  private def x = ...
  def y = ...
}


// protected ist nur innerhalb des deklarierten und aller  
// erbenden Bereiche sichtbar  

class Hund {
  protected def x = ...
}
class Dackel extends Hund {
  // x ist sichtbar
}

// Object
// Wird ein Objekt ohne das Schlüsselwort "new" instanziert, wird das sog.  
// "companion object" aufgerufen. Mit dem "object" Schlüsselwort wird so  
// ein Objekt (Typ UND Singleton) erstellt. Damit kann man dann eine Klasse  
// benutzen ohne ein Objekt instanziieren zu müssen.  
// Ein gültiges companion Objekt einer Klasse ist es aber erst dann, wenn  
// es genauso heisst und in derselben Datei wie die Klasse definiert wurde.  

object Hund {
  def alleSorten = List("Pitbull", "Dackel", "Retriever")
  def createHund(sorte: String) = new Hund(sorte)
}

// Case classes
// Fallklassen bzw. Case classes sind Klassen die normale Klassen um extra  
// Funktionalität erweitern. Mit Case Klassen bekommt man ein paar  
// Dinge einfach dazu, ohne sich darum kümmern zu müssen. Z.B.  
// ein companion object mit den entsprechenden Methoden,  
// Hilfsmethoden wie toString(), equals() und hashCode() und auch noch  
// Getter für unsere Attribute (das Angeben von val entfällt dadurch)  

class Person(val name: String)
class Hund(val sorte: String, val farbe: String, val halter: Person)


// Es genügt das Schlüsselwort case vor die Klasse zu schreiben.  

case class Person(name: String)
case class Hund(sorte: String, farbe: String, halter: Person)


// Für neue Instanzen brauch man kein "new"  

val dackel = Hund("dackel", "grau", Person("peter"))
val dogge = Hund("dogge", "grau", Person("peter"))
  

// getter  

dackel.halter  // => Person = Person(peter)

  
// equals  

dogge == dackel  // => false

  
// copy  
// otherGeorge == Person("george", "9876")  

val otherGeorge = george.copy(phoneNumber = "9876")

// Traits
// Ähnlich wie Java interfaces, definiert man mit traits einen Objekttyp  
// und Methodensignaturen. Scala erlaubt allerdings das teilweise  
// implementieren dieser Methoden. Konstruktorparameter sind nicht erlaubt.  
// Traits können von anderen Traits oder Klassen erben, aber nur von  
// parameterlosen.  

trait Hund {
	def sorte: String
	def farbe: String
	def bellen: Boolean = true
	def beissen: Boolean
}
class Bernhardiner extends Hund{
	val sorte = "Bernhardiner"
	val farbe = "braun"
	def beissen = false
}

  

scala> b  
res0: Bernhardiner = Bernhardiner@3e57cd70  
scala> b.sorte  
res1: String = Bernhardiner  
scala> b.bellen  
res2: Boolean = true  
scala> b.beissen  
res3: Boolean = false  

// Ein Trait kann auch als Mixin eingebunden werden. Die Klasse erbt vom
// ersten Trait mit dem Schlüsselwort "extends", während weitere Traits
// mit "with" verwendet werden können.

trait Bellen {
	def bellen: String = "Woof"
}
trait Hund {
	def sorte: String
	def farbe: String
}
class Bernhardiner extends Hund with Bellen{
	val sorte = "Bernhardiner"
	val farbe = "braun"
}
scala> val b = new Bernhardiner
b: Bernhardiner = Bernhardiner@7b69c6ba
scala> b.bellen
res0: String = Woof

/////////////////////////////////////////////////
// 6. Pattern Matching
/////////////////////////////////////////////////

// Pattern matching in Scala ist ein sehr nützliches und wesentlich  
// mächtigeres Feature als Vergleichsfunktionen in Java. In Scala  
// benötigt ein case Statement kein "break", ein fall-through gibt es nicht.  
// Mehrere Überprüfungen können mit einem Statement gemacht werden.  
// Pattern matching wird mit dem Schlüsselwort "match" gemacht.  

val x = ...
x match {
  case 2 =>
  case 3 =>
  case _ =>
}


// Pattern Matching kann auf beliebige Typen prüfen  

val any: Any = ...
val gleicht = any match {
  case 2 | 3 | 5 => "Zahl"
  case "woof" => "String"
  case true | false => "Boolean"
  case 45.35 => "Double"
  case _ => "Unbekannt"
}


// und auf Objektgleichheit  

def matchPerson(person: Person): String = person match {
  case Person("George", nummer) => "George! Die Nummer ist " + number
  case Person("Kate", nummer)   => "Kate! Die Nummer ist " + nummer
  case Person(name, nummer)     => "Irgendjemand: " + name + ", Telefon: " + nummer
}


// Und viele mehr...  

val email = "(.*)@(.*)".r  // regex
def matchEverything(obj: Any): String = obj match {
  // Werte:
  case "Hello world" => "Got the string Hello world"
  // Typen:
  case x: Double => "Got a Double: " + x
  // Conditions:
  case x: Int if x > 10000 => "Got a pretty big number!"
  // Case Classes:
  case Person(name, number) => s"Got contact info for $name!"
  // RegEx:
  case email(name, domain) => s"Got email address $name@$domain"
  // Tuples:
  case (a: Int, b: Double, c: String) => s"Got a tuple: $a, $b, $c"
  // Strukturen:
  case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c"
  // Patterns kann man ineinander schachteln:
  case List(List((1, 2, "YAY"))) => "Got a list of list of tuple"
}


// Jedes Objekt mit einer "unapply" Methode kann per Pattern geprüft werden  
// Ganze Funktionen können Patterns sein  

val patternFunc: Person => String = {
  case Person("George", number) => s"George's number: $number"
  case Person(name, number) => s"Random person's number: $number"
}


/////////////////////////////////////////////////
// 37. Higher-order functions
/////////////////////////////////////////////////

Scala erlaubt, das Methoden und Funktion wiederum Funtionen und Methoden  
als Aufrufparameter oder Return Wert verwenden. Diese Methoden heissen  
higher-order functions  
Es gibt zahlreiche higher-order functions nicht nur für Listen, auch für  
die meisten anderen Collection Typen, sowie andere Klassen in Scala  
Nennenswerte sind:  
"filter", "map", "reduce", "foldLeft"/"foldRight", "exists", "forall"  

## List

def isGleichVier(a:Int) = a == 4
val list = List(1, 2, 3, 4)
val resultExists4 = list.exists(isEqualToFour)


## map
// map nimmt eine Funktion und führt sie auf jedem Element aus und erzeugt  
// eine neue Liste  
  
// Funktion erwartet ein Int und returned ein Int  

val add10: Int => Int = _ + 10 


// add10 wird auf jedes Element angewendet  

List(1, 2, 3) map add10 // => List(11, 12, 13)


// Anonyme Funktionen können anstatt definierter Funktionen verwendet werden  

List(1, 2, 3) map (x => x + 10)


// Der Unterstrich wird anstelle eines Parameters einer anonymen Funktion  
// verwendet. Er wird an die Variable gebunden.  

List(1, 2, 3) map (_ + 10)


// Wenn der anonyme Block und die Funtion beide EIN Argument erwarten,  
// kann sogar der Unterstrich weggelassen werden.  

List("Dom", "Bob", "Natalia") foreach println


// filter
// filter nimmt ein Prädikat (eine Funktion von A -> Boolean) und findet  
// alle Elemente die auf das Prädikat passen  

List(1, 2, 3) filter (_ > 2) // => List(3)
case class Person(name: String, age: Int)
List(
  Person(name = "Dom", age = 23),
  Person(name = "Bob", age = 30)
).filter(_.age > 25) // List(Person("Bob", 30))


// reduce
// reduce nimmt zwei Elemente und kombiniert sie zu einem Element,  
// und zwar solange bis nur noch ein Element da ist.  

// foreach
// foreach gibt es für einige Collections  

val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println

// For comprehensions
// Eine for-comprehension definiert eine Beziehung zwischen zwei Datensets.  
// Dies ist keine for-Schleife.  

for { n <- s } yield sq(n)
val nSquared2 = for { n <- s } yield sq(n)
for { n <- nSquared2 if n < 10 } yield n
for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared


/////////////////////////////////////////////////
// 8. Implicits
/////////////////////////////////////////////////

// **ACHTUNG:**  
// Implicits sind ein sehr mächtiges Sprachfeature von Scala.
// Es sehr einfach  
// sie falsch zu benutzen und Anfänger sollten sie mit Vorsicht oder am  
// besten erst dann benutzen, wenn man versteht wie sie funktionieren.  
// Dieses Tutorial enthält Implicits, da sie in Scala an jeder Stelle  
// vorkommen und man auch mit einer Lib die Implicits benutzt nichts sinnvolles  
// machen kann.  
// Hier soll ein Grundverständnis geschaffen werden, wie sie funktionieren.  

// Mit dem Schlüsselwort implicit können Methoden, Werte, Funktion, Objekte  
// zu "implicit Methods" werden.  

implicit val myImplicitInt = 100
implicit def myImplicitFunction(sorte: String) = new Hund("Golden " + sorte)


// implicit ändert nicht das Verhalten eines Wertes oder einer Funktion  

myImplicitInt + 2                   // => 102
myImplicitFunction("Pitbull").sorte // => "Golden Pitbull"


// Der Unterschied ist, dass diese Werte ausgewählt werden können, wenn ein  
// anderer Codeteil einen implicit Wert benötigt, zum Beispiel innerhalb von  
// implicit Funktionsparametern  
  
// Diese Funktion hat zwei Parameter: einen normalen und einen implicit  

def sendGreetings(toWhom: String)(implicit howMany: Int) =
  s"Hello $toWhom, $howMany blessings to you and yours!"


// Werden beide Parameter gefüllt, verhält sich die Funktion wie erwartet  

sendGreetings("John")(1000)  // => "Hello John, 1000 blessings to you and yours!"


// Wird der implicit Parameter jedoch weggelassen, wird ein anderer  
// implicit Wert vom gleichen Typ genommen. Der Compiler sucht im  
// lexikalischen Scope und im companion object nach einem implicit Wert,  
// der vom Typ passt, oder  nach einer implicit Methode mit der er in den  
// geforderten Typ konvertieren kann.  
  
// Hier also: "myImplicitInt", da ein Int gesucht wird  

sendGreetings("Jane")  // => "Hello Jane, 100 blessings to you and yours!"


// bzw. "myImplicitFunction"  
// Der String wird erst mit Hilfe der Funktion in Hund konvertiert, und  
// dann wird die Methode aufgerufen  

"Retriever".sorte // => "Golden Retriever"


/////////////////////////////////////////////////
// 19. Misc
/////////////////////////////////////////////////
// Importe

import scala.collection.immutable.List


// Importiere alle Unterpackages  

import scala.collection.immutable._


// Importiere verschiedene Klassen mit einem Statement  

import scala.collection.immutable.{List, Map}


// Einen Import kann man mit '=>' umbenennen  

import scala.collection.immutable.{List => ImmutableList}


// Importiere alle Klasses, mit Ausnahem von....  
// Hier ohne: Map and Set:  

import scala.collection.immutable.{Map => _, Set => _, _}

// Main 

object Application {
  def main(args: Array[String]): Unit = {
    // Sachen kommen hierhin
  }
}


// I/O
// Eine Datei Zeile für Zeile lesen  

import scala.io.Source
for(line <- Source.fromFile("myfile.txt").getLines())
  println(line)


// Eine Datei schreiben  

val writer = new PrintWriter("myfile.txt")
writer.write("Schreibe Zeile" + util.Properties.lineSeparator)
writer.write("Und noch eine Zeile" + util.Properties.lineSeparator)
writer.close()

```

## Weiterführende Hinweise 

// DE
* [Scala Tutorial](https://scalatutorial.wordpress.com)
* [Scala Tutorial](http://scalatutorial.de)

// EN
* [Scala for the impatient](http://horstmann.com/scala/)
* [Twitter Scala school](http://twitter.github.io/scala_school/)
* [The scala documentation](http://docs.scala-lang.org/)
* [Try Scala in your browser](http://scalatutorials.com/tour/)
* [Neophytes Guide to Scala](http://danielwestheide.com/scala/neophytes.html)
* Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user)
---
language: swift
contributors:
  - ["Grant Timmerman", "http://github.com/grant"]
  - ["Christopher Bess", "http://github.com/cbess"]
  - ["Joey Huang", "http://github.com/kamidox"]  
  - ["Anthony Nguyen", "http://github.com/anthonyn60"]
translators:
    - ["Jonas Wippermann", "http://vfuc.co"]
filename: learnswift-de.swift
lang: de-de
---

Swift ist eine Programmiersprache von Apple für die Entwicklung von iOS und OS X Applikationen. Swift wurde 2014 zu Apples WWDC Entwicklerkonferenz vorgestellt und wurde mit dem Ziel entwickelt, fehlerträchtigen Code zu vermeiden sowie mit Objective-C zu koexistieren. Es wird mit dem LLVM Compiler gebaut und ist ab Xcode 6+ verfügbar.

Das offizielle [Swift Programming Language](https://itunes.apple.com/us/book/swift-programming-language/id881256329) Buch von Apple ist kostenlos via iBooks verfügbar.

Außerdem hilfreich ist Apples [Getting Started Guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/RoadMapiOS/index.html), ein guter Einstiegspunkt mit komplettem Swift-Tutorial.

```swift
// importiere ein Modul
import UIKit

//
// MARK: Grundlagen
//

// Xcode unterstützt "Landmarks" um Code zu gliedern, sie werden in der Jump Bar aufgelistet
// MARK: Abschnitts-Markierung
// TODO: Zu erledigen
// FIXME: Zu beheben

// In Swift 2 wurden println und print zusammengefasst in eine print-Methode. Es wird automatisch ein Zeilenumbruch angehängt.
print("Hello, world!") // println ist jetzt print
print("Hello, world!", appendNewLine: false) // printen ohne Zeilenumbruch am Ende

// Variablen (var) können nach der Initialisierung verändert werden 
// Konstanten (let) können nach der Initialisierung NICHT verändert werden 

var myVariable = 42
let øπΩ = "value" // Unicode-Variablennamen
let π = 3.1415926
let convenience = "keyword" // Kontext-abhängiger Variablenname
let weak = "keyword"; let override = "another keyword" // Instruktionen können durch ein Semikolon aufgeteilt werden
let `class` = "keyword" // Nutze "Backticks" um Schlüsselwörter als Variablennamen zu verwenden
let explicitDouble: Double = 70 // Typ explizit festgelegt
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "some text " + String(myVariable) // Casting
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // String Interpolation

// Build-spezifische Werte
// benutzt -D build configuration
#if false
    print("not printed")
    let buildValue = 3
#else
    let buildValue = 7
#endif
print("Build value: \(buildValue)") // Build value: 7

/*
    Optionals ist ein Swift-Feature, welches ermöglicht, dass eine Variable entweder einen (`Some`) oder keinen (`None`) Wert hat

    Da Swift von jeder property einen Wert erwartet, muss sogar nil explizit als Optional festgelegt werden.

    Optional<T> ist ein Enum.
*/
var someOptionalString: String? = "optional" // Kann nil sein
// Genau wie oben, aber ? ist ein postfix operator (Syntax Candy)
var someOptionalString2: Optional<String> = "optional"

if someOptionalString != nil {
    // Ich bin nicht nil
    if someOptionalString!.hasPrefix("opt") {
        print("has the prefix")
    }
    
    let empty = someOptionalString?.isEmpty
}
someOptionalString = nil

// Implizit entpackter Optionalwert
var unwrappedString: String! = "Value is expected."
// Genau wie oben, aber ! ist ein postfix operator (noch mehr Syntax Candy)
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Value is expected."

if let someOptionalStringConstant = someOptionalString {
    // hat einen (`Some`) Wert, nicht nil
    if !someOptionalStringConstant.hasPrefix("ok") {
        // hat keinen "ok"-Prefix
    }
}

// Swift unterstützt das festlegen von Werten eines beliebigen Typens
// AnyObject == id
// Im Gegensatz zum Objective-C `id`, funktioniert AnyObject mit jeglichen Werten (Class, Int, struct, etc)
var anyObjectVar: AnyObject = 7
anyObjectVar = "Changed value to a string, not good practice, but possible."

/*
    Ein Kommentar
    
    /*
        Verschachtelte Kommentare sind ebenfalls unterstützt
    */
*/

//
// MARK: Collections
//

/*
    Array und Dictionary-Typen sind structs. 
    Deswegen implizieren `let` und `var` bei der Initialisierung auch ob sie änderbar (var) oder unveränderlich (let) sind.
*/

// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
let emptyArray = [String]() // let == unveränderlich
let emptyArray2 = Array<String>() // genau wie oben
var emptyMutableArray = [String]() // var == änderbar


// Dictionary
var occupations = [
    "Malcolm": "Captain",
    "kaylee": "Mechanic"
]
occupations["Jayne"] = "Public Relations"
let emptyDictionary = [String: Float]() // let == unveränderlich
let emptyDictionary2 = Dictionary<String, Float>() // genau wie oben
var emptyMutableDictionary = [String: Float]() // var == änderbar


//
// MARK: Kontrollstruktur
//

// for-Schleife (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
    if value == 1 {
        print("One!")
    } else {
        print("Not one!")
    }
}

// for-Schleife mit Indizes (array)
for index in myArray.indices {
    print("Value with index \(index) is \(myArray[index])")
}

// for-Schleife (dictionary)
var dict = ["one": 1, "two": 2]
for (key, value) in dict {
    print("\(key): \(value)")
}

// for-Schleife (range)
for i in -1...shoppingList.count {
    print(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// ..< schließt letzte Nummer aus

// while-Schleife
var i = 1
while i < 1000 {
    i *= 2
}

// do-while-Schleife
do {
    print("hello")
} while 1 == 2

// Switch
// Sehr mächtig, wie `if` statement mit Syntax Candy
// Unterstützt Strings, Objekt-Instanzen und primitive Typen (Int, Double, etc)
let vegetable = "red pepper"
switch vegetable {
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let localScopeValue where localScopeValue.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(localScopeValue)?"
default: // notwendig (um alle möglichen Eingaben zu verarbeiten)
    let vegetableComment = "Everything tastes good in soup."
}


//
// MARK: Funktionen
//

// Funktionen sind ein sogenannter "first-class" Typ, was bedeutet, dass sie
// in Funktionen geschachtelt werden und "herumgereicht" werden können

// Funktion mit Swift header Dokumentation

/**
    Eine Grüß-Funktion

    - Ein Aufzählungspunkt
    - Ein weiterer Aufzählungspunkt in der Dokumentation

    :param: name Ein Name
    :param: day Ein Tag
    :returns: Ein String, der Name und Tag beinhält.
*/
func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")

// Ähnlich wie oben, bloß anderes Funktions-Parameter-Verhalten
func greet2(#requiredName: String, externalParamName localParamName: String) -> String {
    return "Hello \(requiredName), the day is \(localParamName)"
}
greet2(requiredName:"John", externalParamName: "Sunday")


// Funktion, welche mehrere Werte in einem Tupel zurückgibt
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// Ignoriere Tupel-(oder andere)Werte mit _ (Unterstrich)
let (_, price1, _) = pricesTuple // price1 == 3.69
print(price1 == pricesTuple.1) // true
print("Gas price: \(price)")

// Variierende Argumente..
func setup(numbers: Int...) {
    // .. liegen als Array vor
    let number = numbers[0]
    let argCount = numbers.count
}

// Funktionen übergeben und zurückgeben
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

// Übergabe via Referenz ("Pass by reference")
func swapTwoInts(inout a: Int, inout b: Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(&someIntA, &someIntB)
print(someIntB) // 7


//
// MARK: Closures
//
var numbers = [1, 2, 6]

// Funktionen sind besondere Closures ({})

// Closure Beispiel
// `->` teilt Parameter und Rückgabe-Typ
// `in` teilt den Closure Header vom Body
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})


// Wenn der Typ bekannt ist, wie oben, kann folgendes getan werden
numbers = numbers.map({ number in 3 * number })
// oder sogar dies
//numbers = numbers.map({ $0 * 3 })

print(numbers) // [3, 6, 18]

// "Schleppende Closure" (Trailing Closure)
numbers = sorted(numbers) { $0 > $1 }

print(numbers) // [18, 6, 3]

// Sehr verkürzt, da sich der Typ durch den < Operator ableiten lässt

numbers = sorted(numbers, < )

print(numbers) // [3, 6, 18]

//
// MARK: Strukturen
// (häufig einfach structs)
//

// Structures und Klassen haben sehr ähnliche Fähigkeiten
struct NamesTable {
    let names = [String]()
    
    // Eigendefiniertes subscript
    subscript(index: Int) -> String {
        return names[index]
    }
}


// Strukturen haben eine automatisch generierte, designierte Initialisierungsfunktion
let namesTable = NamesTable(names: ["Me", "Them"])
let name = namesTable[1]
print("Name is \(name)") // Name is Them

//
// MARK: Klassen
//

// Klassen, Strukturen und deren Member haben drei Ebenen der Zugriffskontrolle
// Es gibt: internal (default), public, private

public class Shape {
    public func getArea() -> Int {
        return 0;
    }
}

// Alle Methoden und Properties einer Klasse sind public
// Wenn das einfache Ziel ist, Daten in einem strukturierten Objekt zu halten,
// sollte ein `struct` verwendet werden

internal class Rect: Shape {
    var sideLength: Int = 1
    
    // Eigendefinierte Getter und Setter für die Property
    private var perimeter: Int {
        get {
            return 4 * sideLength
        }
        set {
            // `newValue` ist eine implizite Variable, welche in Settern verfügbar ist
            sideLength = newValue / 4
        }
    }
    
    // "Lazy" (faules) Laden einer Property, sie bleibt uninitialisiert (nil),
    // bis sie aufgerufen wird
    lazy var subShape = Rect(sideLength: 4)
    
    // Wenn kein eigendefinierter Getter/Setter notwendig ist,
    // aber trotzdem Code vor und nach dem Setzen eines Variablenwertes laufen soll,
    // kann "willSet" und "didSet" benutzt werden
    var identifier: String = "defaultID" {
        // der `willSet` Parameter wird der Variablenname für den neuen Wert sein 
        willSet(someIdentifier) {
            print(someIdentifier)
        }
    }
    
    init(sideLength: Int) {
        self.sideLength = sideLength
        // super.init muss immer aufgerufen werden, wenn eigene Properties initialisiert werden
        super.init()
    }
    
    func shrink() {
        if sideLength > 0 {
            sideLength -= 1
        }
    }
    
    override func getArea() -> Int {
        return sideLength * sideLength
    }
}

// Eine simple `Square`-Klasse erbt von/erweitert `Rect`
class Square: Rect {
    convenience init() {
        self.init(sideLength: 5)
    }
}

var mySquare = Square()
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4

// Casten der Instanz
let aShape = mySquare as Shape

// Vergleiche Instanzen, nicht äquivalent zum == , welches Objekte vergleicht ("equal to") 
if mySquare === mySquare {
    print("Yep, it's mySquare")
}

// Optionale Initialisierung
class Circle: Shape {
    var radius: Int
    override func getArea() -> Int {
        return 3 * radius * radius
    }
    
    // Ein Fragezeichen nach `init` ist eine optionale Initialisierung,
    // welche nil zurückgeben kann
    init?(radius: Int) {
        self.radius = radius
        super.init()
        
        if radius <= 0 {
            return nil
        }
    }
}

var myCircle = Circle(radius: 1)
print(myCircle?.getArea())    // Optional(3)
print(myCircle!.getArea())    // 3
var myEmptyCircle = Circle(radius: -1)
print(myEmptyCircle?.getArea())    // "nil"
if let circle = myEmptyCircle {
    // wird nicht ausgeführt, da myEmptyCircle nil ist
    print("circle is not nil")
}


//
// MARK: Enums
//

// Enums können optional einen eigenen Typen haben
// Wie Klassen auch können sie Methoden haben

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func getIcon() -> String {
        switch self {
        case .Spades: return "♤"
        case .Hearts: return "♡"
        case .Diamonds: return "♢"
        case .Clubs: return "♧"
        }
    }
}


// Enum-Werte können vereinfacht geschrieben werden, es muss nicht der Enum-Typ
// genannt werden, wenn die Variable explizit deklariert wurde

var suitValue: Suit = .Hearts

// Nicht-Integer-Enums brauchen direkt zugewiesene "Rohwerte"
enum BookName: String {
    case John = "John"
    case Luke = "Luke"
}
print("Name: \(BookName.John.rawValue)")

// Enum mit assoziierten Werten
enum Furniture {
    // mit Int assoziiert
    case Desk(height: Int)
    // mit String und Int assoziiert
    case Chair(String, Int)
    
    func description() -> String {
        switch self {
        case .Desk(let height):
            return "Desk with \(height) cm"
        case .Chair(let brand, let height):
            return "Chair of \(brand) with \(height) cm"
        }
    }
}

var desk: Furniture = .Desk(height: 80)
print(desk.description())     // "Desk with 80 cm"
var chair = Furniture.Chair("Foo", 40)
print(chair.description())    // "Chair of Foo with 40 cm"


//
// MARK: Protokolle
//

// Protokolle (`protocol`s) können verlangen, dass entsprechende
// Typen spezifische Instanz-Properties, Instanz/Klassen-Methoden,
// Operatoren oder Subscripts implementieren/haben

protocol ShapeGenerator {
    var enabled: Bool { get set }
    func buildShape() -> Shape
}

// Protocols mit @objc deklariert ermöglichen optionale Funktionen,
// welche es ermöglichen, abzufragen ob ein Typ einem Protokoll entspricht
@objc protocol TransformShape {
    optional func reshaped()
    optional func canReshape() -> Bool
}

class MyShape: Rect {
    var delegate: TransformShape?
    
    func grow() {
        sideLength += 2

        // Ein Fragezeichen nach einer optionalen Property, Methode oder Subscript
        // ignoriert elegant Nil-Werte und geben nil zurück, anstatt einen Laufzeitfehler zu werfen
        // Dies wird "optional Chaining" (optionale Verkettung) genannt
        if let allow = self.delegate?.canReshape?() {
            // frage erst nach delegate, dann nach Methode
            self.delegate?.reshaped?()
        }
    }
}


//
// MARK: Sonstiges
//

// `extension`s: (Erweiterungen), erweitere Typen um zusätzliche Funktionalität

// Square entspricht jetzt dem `Printable` Protokoll
extension Square: Printable {
    var description: String {
        return "Area: \(self.getArea()) - ID: \(self.identifier)"
    }
}

print("Square: \(mySquare)")

// Standardtypen können ebenfalls erweitert werden
extension Int {
    var customProperty: String {
        return "This is \(self)"
    }
    
    func multiplyBy(num: Int) -> Int {
        return num * self
    }
}

print(7.customProperty) // "This is 7"
print(14.multiplyBy(3)) // 42


//Generics: Ähnlich zu Java und C#. Nutze das `where` keyword um die Bedingung
// des Generics festzulegen

func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
    for (index, value) in enumerate(array) {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let foundAtIndex = findIndex([1, 2, 3, 4], 3)
print(foundAtIndex == 2) // true

// Operatoren:
// Eigendefinierte Operatoren können mit diesen Zeichen beginnen:
//      / = - + * % < > ! & | ^ . ~
// oder
// Unicode Mathematik, Symbole, Pfeile, Dingbat, und Linien/Box - Zeichen
prefix operator !!! {}


// Ein Prefix-Operator, welcher die Seitenlänge verdreifacht
prefix func !!! (inout shape: Square) -> Square {
    shape.sideLength *= 3
    return shape
}

// Aktueller Wert
print(mySquare.sideLength) // 4

// Wert nach verwendung des eigenen Operators
!!!mySquare
print(mySquare.sideLength) // 12
```
---
language: Tcl
contributors:
    - ["Poor Yorick", "http://pooryorick.com/"]
translators:
    - ["Martin Schimandl", "https://github.com/Git-Jiro"]
filename: learntcl-de.tcl
lang: de-de
---

Tcl wurde kreiert von [John Ousterhout](http://wiki.tcl.tk/John Ousterout) als
eine wiederverwendbare Script-Sprache für Chip-Design Werkzeuge die er kreiert
hat. Im Jahre 1997 wurde er mit dem [ACM Software System
Award](http://en.wikipedia.org/wiki/ACM_Software_System_Award) für Tcl
ausgezeichnet. Tcl kann sowohl als eingebettete Scipt-Sprache als auch als
allgemeine Programmier-Sprache verwendet werden. Tcl kann auch als portable
C-Bibliothek verwendet werden. Sogar in Fällen in denen die Script-Fähigkeiten
nicht nötig sind. Denn Tcl stellt Daten-Strukturen wie dynamische Zeichenketten,
Listen und Hash-Tabellen bereit. Die C-Bilbiothek stellt auch portable
Funktionen zur Verfügung: Laden von dynamischen Bibliotheken, Zeichenketten
formatierung und Code Konversion, Dateisystem Operationen, Netzwerk Operationen
und mehr.


Verschiedenste herausragende Fähigkeiten von Tcl:

* Praktische Cross-Platform Netzwerk-API

* Vollständig virtualisiertes Dateisystem

* Stapelbare I/O Kanäle

* Asynchron bis zum Kern

* Vollständige Ko-Routinen

* Robustes und einfach zu verwendendes Thread-Modell


Wenn Lisp ein Listen-Prozessor ist, dann ist TCl ein Zeichenketten-Prozessor.
Alle Werte sind Zeichenketten. Eine Liste ist ein Zeichenketten-Format. Eine
Prozedur-Definition ist ein Zeichenketten-Format. Um leistungsfähig zu sein,
werden Tcl-intern diese Zeichenketten in Strukutierter-Form gepuffert. Ein
Beispiel: Der "list" Befehl arbeitet mit diesen internen gepufferten 
Repräsentationen. Tcl kümmert sich selbständig darum die String-Repräsentationen
zu aktualisieren, falls dies im Skript benötigt werden sollten. Das Kopieren-
beim-Schreiben-Design von Tcl erlaubt es Skript-Authoren mit großen Daten-
Strukturen zu arbeiten ohne zuätzlichen Speicher-Overhead. Prozeduren werden
automatisch byte-kompiliert außer sie verwenden dynamsiche Befehle wie zum
Beispiel "uplevel", "upvar und "trace".

Es ist eine freude in Tcl zu programmieren. Hacker-Typen werden gefallen daran
finden, wenn sie Lisp, Forth oder Smalltalk interessant finden. Tcl wird auch
Ingenieuren und Wissenshaftlern gefallen die nur den Job erledigen wollen,
und zwar mit Werkzeugen die sich ihrem Willen anpassen. Bei Tcl ist jegliche
funktionalität in Befehlen ausgeführt, selbst Dinge wie Schleifen und
Mathematische-Funktionen die bei anderen Sprachen normalerweise Teil der Syntax
sind. Das erlaubt Tcl in den Hintergrund von Domänen spezischen Sprachen zu
treten die das jeweilige Projekt gerade benötigt. Die Tcl-Syntax ist sehr
leichtgewichtig. Sie ist selbst leichtgewichtiger als die Syntax von Lisp.
Tcl steht dir einfach nicht im Weg.


```tcl
#! /bin/env tclsh

################################################################################
## 1. Richtlinien 
################################################################################

# Tcl ist nicht Bash oder C! Das muss gesagt werden, denn standard Shell-Quoting
# funktioniert fast mit Tcl. Daher glauben viele sie können diese Syntax für
# Tcl übernehmen. Am Beginn funktioniert das meist, führt aber schnell zu 
# Frustrationen wenn die Skripte komplexer werden.

# Eckige-Klammern sind nur Quoting-Mechanismen, keine Code-Block-Konstruktoren
# und auch keine Listen-Konstruktoren. In Tcl gibt es diese beiden Dinge nicht.
# Eckige-Klammern werden verwendet um Spezial-Zeichen in Prozeduren zu escapen
# und in Zeichenketten die als Listen formattiert sind.

################################################################################
## 2. Syntax 
################################################################################

# Jede Zeile ist ein Befehl. Das erste Wort ist der Name des Befehls, jedes
# weitere Wort ist ein Argument des Befehls. Wörter sind begrenzt durch
# Leerzeichen. Da jedes Wort auch ein String ist, sind keine speziellen
# auszeichnungen wie Anführungs-Zeichen, Klammern oder Backslashes nötig.
# Selbst wenn Anführungs-Zeichen verwendet werden, denn sie sind ja keine
# String-Konstruktoren, sondern nur Escape-Zeichen.

set greeting1 Sal 
set greeting2 ut
set greeting3 ations


# Strichpunkte begrenzen auch Befehle
set greeting1 Sal; set greeting2 ut; set greeting3 ations 


# Das Dollar-Zeichen zeigt eine Variablen-Substitution an.
set greeting $greeting1$greeting2$greeting3


# Eckige-Klammern zeigen Befehls-Substitionen an. Das Ergebnis des Befehls wird an
# Stelle des Klammern-Ausdrucks eingefügt. Wenn man dem "set" Befehl nur den
# Namen einer Variablen übergibt, gibt er den Wert der Variablen zurück.
set greeting $greeting1$greeting2[set greeting3]


# Befehls-Substitution sollte eigentlich Script-Substitution heißen, denn ein
# komplettes Script, und nicht nur ein Befehl, kann zwischen die Eckigen-Klammern
# geschrieben werden. Der "incr" Befehl erhöht den Wert einer Variable um 1
# und gibt den neuen Wert der Variable zurück.
set greeting $greeting[
	incr i
	incr i
	incr i
]


# Der Backslash unterdrück die Bedeutung von Sonderzeichen
set amount \$16.42


# Der Backslash macht bestimmte Zeichen zu Sonderzeichen
puts lots\nof\n\n\n\n\n\nnewlines

# Ein Wort das in geschweiften Klammern eingeschlossen wurde ist von jeglichen
# speziellen Interpretationen ausgeschlossen. Eine Ausnahme bilden Backslashes
# vor geschweiften Klammern, hiermit wird die geschweifte Klammer von der Suche
# nach der schließenden geschweiften Klammer ausgeschlossen.
set somevar {
    Das ist ein literales $ Zeichen, diese geschweifte Klammer \} wird nicht
    als Ende interpretiert.
} 


# Bei einem Wort das in doppelten Anführungszeichen steht verlieren Leerzeichen
# ihre spezielle Bedeutung.
set name Neo
set greeting "Hallo, $name"


#Variablen-Namen können irgend eine Zeichenkette sein.
set {first name} New


# Die Geschweifte-Klammern-Form der Variablen-Substitution kann sehr komplexe
# Variblen-Namen handhaben.
set greeting "Hello, ${first name}"


# Der "set" Befehl kann immer anstatt einer Variablen-Substition verwendet
# werden.
set greeting "Hello, [set {first name}]"


# Mit dem Expansions-Operator "{*}" werden Wörter innerhalb eines Wortes wieder
# individuell als Teile des aktuellen Befehls behandelt.
set {*}{name Neo}

# Ist Äquivalent zu
set name Neo


# Ein Array ist eine spezielle Varible die also Kontainer für andere Variablen
# dient.
set person(name) Neo
set person(gender) male
set greeting "Hello, $person(name)"


# Ein Namensraum enthält Befehle und Variablen
namespace eval people {
    namespace eval person1 {
        variable name Neo
    }
}


#Der volle Name einer Variablen beihaltet den/die umschließenden
# Namensraum/Namensräume begrenzt durch zwei Doppelpunkte.
set greeting "Hello $people::person1::name"
```

```tcl
################################################################################
## 3. Einige Notizen 
################################################################################

# Jede weitere Funktion ist über Befehle implementiert. Von nun an kommt keine
# neue Syntax hinzu. Alles weitere das es über Tcl zu lernen gibt ist das
# Verhalten individueller Befehle und die bedeutung ihrer Argumente.


# Um einen Interpreter zu bekommen mit dem man nichts mehr machen kann, lösche
# einfach den globalen Namensraum. Das ist nicht sehr sinnvoll, zeigt aber die
# Natur von Tcl.
namespace delete ::


# Wegen des Verhaltens der Namens-Auflösung ist es sicherer den "variable"
# Befehl zu verwenden um in einem Namensraum einen Wert zu deklarieren oder
# zuzuweisen. Wenn eine Variable mit dem namen "name" bereits im globalen
# Namensraum existiert, bewirkt der "set" Befehl das der globalen Variable ein
# Wert zugewiesen wird, anstatt eine Variable im lokalen Namensraum zu erzeugen
namespace eval people {
    namespace eval person1 {
        variable name Neo
    }
}


# Es kann immer der vollständige Name einer Variable verwendet werden, falls
# gewünscht.
set people::person1::name Neo



################################################################################
## 4. Befehle 
################################################################################

# Berechnungen werde mit dem "expr" Befehl durchgeführt.
set a 3
set b 4
set c [expr {$a + $b}]

# Since "expr" performs variable substitution on its own, brace the expression
# to prevent Tcl from performing variable substitution first.  See

# Da der "expr" Befehl eigene Variablen-Substitutionen durchführt, setze den
# zu berechnenden Ausdruck in Eckige-Klammern. Das hindert Tcl daran Variablen-
# Substitutionen durchzuführen. Für Details siehe:
# "http://wiki.tcl.tk/Brace%20your%20#%20expr-essions"


# Der "expr" Befehl versteht Variablen- und Befehls-Substitutionen
set c [expr {$a + [set b]}]


# Der "expr" Befehl stellt Mathematische-Funktionen zur Verfügung.
set c [expr {pow($a,$b)}]


# Mathematische Operatoren sind als Befehle auch im Namensraum 
# ::tcl::mathop verfügbar.
::tcl::mathop::+ 5 3

# Befehle können aus anderen Namensräumen importiert werden.
namespace import ::tcl::mathop::+
set result [+ 5 3]


# Neu Befehle werden mit dem "proc" Befehl gebildet.
proc greet name {
    return "Hello, $name!"
}

#Es können mehrere Parameter spezifiziert werden.
proc greet {greeting name} {
    return "$greeting, $name!"
}


# Wie bereits erwähnt, geschwungene Klammern erzeugen keinen Code-Block.
# Jeder Wert, sogar das dritte Argument für den "proc" Befehl ist eine
# Zeichenkette. Der vorherige Befehl kann daher auch ohne
# geschwungene Klammern geschrieben werden:
proc greet greeting\ name return\ \"Hello,\ \$name!



# Wenn der letzte Parameter der literale Wert "args" ist, sammelt dieser Wert
# alle übrigen Argumente des Befehls ein wenn dieser aufgerufen wird.
proc fold {cmd args} {
    set res 0
    foreach arg $args {
        set res [$cmd $res $arg]
    }
}
fold ::tcl::mathop::* 5 3 3 ;# ->  45


# Bedingte Ausführung ist auch als Befehl implementiert
if {3 > 4} {
    puts {This will never happen}
} elseif {4 > 4} {
    puts {This will also never happen}
} else {
    puts {This will always happen}
}


# Auch Schleifen sind Befehle. Das erste, zweite und dritte Argument des "for"
# Befehls wird als mathematischer Ausdruck behandelt.
for {set i 0} {$i < 10} {incr i} {
    set res [expr {$res + $i}]
}


# Das erste Argument des "while" Befehls wird auch als mathematischer Ausdruck
# behandelt.
set i 0
while {$i < 10} {
    incr i 2
}


# Eine Liste ist eine speziell formatierte Zeichenkette. Im einfachsten Fall
# genügen Leerzeichen als Trennzeichen zwischen den einzelnen Werten.
set amounts 10\ 33\ 18 
set amount [lindex $amounts 1]


# Geschwungene Klammern und Backslashes können verwendet werden um komplexe
# Werte in einer Liste zu formatieren. Eine Liste sieht aus wie ein Skript,
# allerdings verlieren verlieren Zeilenumbrüche und Doppelüunkte ihre 
# besondere Bedeutung. Diese Funktionalität macht Tcl homoikonisch. Die
# folgende Liste enhtält drei Elemente.
set values {

    one\ two

    {three four}

    five\{six

}


# Da Listen auch Zeichenketten sind, kann man Zeichenketten-Operationen auf
# ihnen anwenden. Allerdings mit dem Risiko die Formatierung der Liste zu
# beschädigen.
set values {one two three four}
set values [string map {two \{} $values] ;# $values is no-longer a \
    properly-formatted listwell-formed list


# Der sicherste Weg korrekt formatierte Liste zu erzeugen, ist den "list"
# Befehl zu verwenden.
set values [list one \{ three four]
lappend values { } ;# Ein Leerzeichen als Element der Liste hinzufügen


# Mit "eval" können Werte als Skripts evaluiert weden.
eval {
    set name Neo
    set greeting "Hello, $name"
}


# Eine Liste kann immer an "eval" übergeben werden, solange die Liste einen
# einzigen Befehl entält.
eval {set name Neo}
eval [list set greeting "Hello, $name"]


# Daher: Wenn "eval" verwendet wird, verwende [list] um den gewünschten Befehl
# aufzubauen.
set command {set name}
lappend command {Archibald Sorbisol}
eval $command


# Es ist ein häufiger Fehler die Listen funktionen beim Aufbauen von Listen
# nicht zu verwenden.
set command {set name}
append command { Archibald Sorbisol}
eval $command ;# Hier passiert eine Fehler, denn der "set" Befehl hat nun zu \
    viele Argumente {set name Archibald Sorbisol}


# Dieser Fehler kann auch leicht beim "subst" Befehl passieren.
set replacement {Archibald Sorbisol}
set command {set name $replacement}
set command [subst $command] 
eval $command ;# The same error as before: too many arguments to "set" in \
    {set name Archibald Sorbisol}


# Die korrekte Vorgangsweise ist es den substituierten Wert mit dem "list"
# Befehl zu formatieren.
set replacement [list {Archibald Sorbisol}]
set command {set name $replacement}
set command [subst $command] 
eval $command


# Der "list" Befehl wird sehr häufig verwendet um Werte zu formatieren die
# in Tcl Skript Vorlagen substituiert werden. Es gibt dazu viele Beispiele,
# siehe unterhalb.


# Der "apply" Befehl evaluiert eine Zeichenkette als Befehl.
set cmd {{greeting name} {
    return "$greeting, $name!"
}}
apply $cmd Whaddup Neo


# Der "uplevel" Befehl evaluiert ein Skript in einem höher liegenden
Gültigkeitsbereich.
proc greet {} {
	uplevel {puts "$greeting, $name"}
}

proc set_double {varname value} {
	if {[string is double $value]} {
		uplevel [list variable $varname $value]
	} else {
		error [list {not a double} $value]
	}
}


# Der "upvar" Befehl verknüpft eine Variable im aktuellen Gültigkeitsbereich
# mit einer Variable in einem höher liegenden Gültigkeitsbereich.
proc set_double {varname value} {
	if {[string is double $value]} {
		upvar 1 $varname var
		set var $value
	} else {
		error [list {not a double} $value]
	}
}


# Werde den eingebauten "while" Befehl los.
rename ::while {}


# Definieren einen neuen "while" Befehl mit hilfe des "proc" Befehls.
# Ausführlichere Fehler-Behandlung wird dem Leser als Übung überlassen.
proc while {condition script} {
    if {[uplevel 1 [list expr $condition]]} {
        uplevel 1 $script
        tailcall [namespace which while] $condition $script
    }
}


# Der "coroutine" Befehl erzeugt einen separaten Call-Stack, zusammen mit einem
# Befehl um diesem Call-Stack zu verwenden. Der "yield" Befehl unterbricht
# die Ausführung des aktuellen Call-Stacks.
proc countdown {} {
	#send something back to the initial "coroutine" command
	yield

	set count 3 
	while {$count > 1} {
		yield [incr count -1]
	}
	return 0
}
coroutine countdown1 countdown
coroutine countdown2 countdown
puts [countdown 1] ;# -> 2 
puts [countdown 2] ;# -> 2 
puts [countdown 1] ;# -> 1 
puts [countdown 1] ;# -> 0 
puts [coundown 1] ;# -> invalid command name "countdown1"
puts [countdown 2] ;# -> 1 


```

## Referenzen

[Official Tcl Documentation](http://www.tcl.tk/man/tcl/)

[Tcl Wiki](http://wiki.tcl.tk)

[Tcl Subreddit](http://www.reddit.com/r/Tcl)
---
language: yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
translators:
  - ["Ruben M.", "https://github.com/switchhax"]
filename: learnyaml-de.yaml
lang: de-de
---

YAML ist eine Sprache zur Datenserialisierung, die sofort von Menschenhand geschrieben und gelesen werden kann.

YAML ist ein Erweiterung von von JSON mit der Erweiterung um syntaktisch wichtige Zeilenumbrüche und Einrückungen, ähnlich wie auch in Python. Anders als in Python allerdings erlaubt YAML keine Tabulator-Zeichen.

```yaml
# Kommentare in YAML schauen so aus.

#################
# SKALARE TYPEN #
#################

# Unser Kernobjekt (für das ganze Dokument) wird das Assoziative Datenfeld (Map) sein,
# welches equivalent zu einem Hash oder einem Objekt einer anderen Sprache ist.
Schlüssel: Wert
nochn_Schlüssel: Hier kommt noch ein Wert hin.
eine_Zahl: 100
wissenschaftliche_Notation: 1e+12
boolean: true
null_Wert: null
Schlüssel mit Leerzeichen: value
# Strings müssen nicht immer mit Anführungszeichen umgeben sein, können aber:
jedoch: "Ein String in Anführungzeichen"
"Ein Schlüssel in Anführungszeichen": "Nützlich, wenn du einen Doppelpunkt im Schlüssel haben willst."

# Mehrzeilige Strings schreibst du am besten als 'literal block' (| gefolgt vom Text)
# oder ein 'folded block' (> gefolgt vom text).
literal_block: |
    Dieser ganze Block an Text ist der Wert vom Schlüssel literal_block,
    mit Erhaltung der Zeilenumbrüche.

    Das Literal fährt solange fort bis dieses unverbeult ist und die vorherschende Einrückung wird 
    gekürzt.

        Zeilen, die weiter eingerückt sind, behalten den Rest ihrer Einrückung -
        diese Zeilen sind mit 4 Leerzeichen eingerückt.
folded_style: >
    Dieser ganze Block an Text ist der Wert vom Schlüssel folded_style, aber diesmal
    werden alle Zeilenumbrüche durch ein Leerzeichen ersetzt.

    Freie Zeilen, wie obendrüber, werden in einen Zeilenumbruch verwandelt.

        Weiter eingerückte Zeilen behalten ihre Zeilenumbrüche -
        diese Textpassage wird auf zwei Zeilen sichtbar sein.

####################
# COLLECTION TYPEN #
####################

# Verschachtelung wird duch Einrückung erzielt.
eine_verschachtelte_map:
    schlüssel: wert
    nochn_Schlüssel: Noch ein Wert.
    noch_eine_verschachtelte_map:
        hallo: hallo

# Schlüssel müssen nicht immer String sein.
0.25: ein Float-Wert als Schlüssel

# Schlüssel können auch mehrzeilig sein, ? symbolisiert den Anfang des Schlüssels
? |
    Dies ist ein Schlüssel,
    der mehrzeilig ist.
: und dies ist sein Wert

# YAML erlaubt auch Collections als Schlüssel, doch viele Programmiersprachen
# werden sich beklagen.

# Folgen (equivalent zu Listen oder Arrays) schauen so aus:
eine_Folge:
    - Artikel 1
    - Artikel 2
    - 0.5 # Folgen können verschiedene Typen enthalten.
    - Artikel 4
    - schlüssel: wert
      nochn_schlüssel: nochn_wert
    -
        - Dies ist eine Folge
        - innerhalb einer Folge

# Weil YAML eine Erweiterung von JSON ist, können JSON-ähnliche Maps und Folgen
# geschrieben werden:
json_map: {"schlüssel": "wert"}
json_seq: [3, 2, 1, "Start"]

############################
# EXTRA YAML EIGENSCHAFTEN #
############################

# YAML stellt zusätzlich Verankerung zu Verfügung, welche es einfach machen 
# Inhalte im Dokument zu vervielfältigen. Beide Schlüssel werden den selben Wert haben.
verankerter_inhalt: &anker_name Dieser String wird als Wert beider Schlüssel erscheinen.
anderer_anker: *anker_name

# YAML hat auch Tags, mit denen man explizit Typangaben angibt.
explicit_string: !!str 0.5
# Manche Parser implementieren sprachspezifische Tags wie dieser hier für Pythons
# komplexe Zahlen.
python_komplexe_Zahlen: !!python/komplex 1+2j

####################
# EXTRA YAML TYPEN #
####################

# Strings and Zahlen sind nicht die einzigen Skalare, welche YAML versteht.
# ISO-formatierte Datumsangaben and Zeiangaben können ebenso geparsed werden.
DatumZeit: 2001-12-15T02:59:43.1Z
DatumZeit_mit_Leerzeichen: 2001-12-14 21:59:43.10 -5
Datum: 2002-12-14

# Der !!binary Tag zeigt das ein String base64 verschlüsselt ist.
# Representation des Binären Haufens
gif_datei: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML bietet auch Mengen (Sets), welche so ausschauen
menge:
    ? artikel1
    ? artikel2
    ? artikel3

# Wie in Python sind Mengen nicht anderes als Maps nur mit null als Wert; das Beispiel oben drüber ist equivalent zu:
menge:
    artikel1: null
    artikel2: null
    artikel3: null
```
---
category: Algorithms & Data Structures
name: Dynamic Programming
contributors:
    - ["Akashdeep Goel", "http://github.com/akashdeepgoel"]
---

# Dynamic Programming

## Introduction

Dynamic Programming is a powerful technique used for solving a particular class of problems as we will see. The idea is very simple, If you have solved a problem with the given input, then save the result for future reference, so as to avoid solving the same problem again.

Always remember!
"Those who can't remember the past are condemned to repeat it"

## Ways of solving such Problems

1. *Top-Down* : Start solving the given problem by breaking it down. If you see that the problem has been solved already, then just return the saved answer. If it has not been solved, solve it and save the answer. This is usually easy to think of and very intuitive. This is referred to as Memoization.

2. *Bottom-Up* : Analyze the problem and see the order in which the sub-problems are solved and start solving from the trivial subproblem, up towards the given problem. In this process, it is guaranteed that the subproblems are solved before solving the problem. This is referred to as Dynamic Programming.

## Example of Dynamic Programming

The Longest Increasing Subsequence problem is to find the longest increasing subsequence of a given sequence. Given a sequence `S= {a1 , a2 , a3, a4, ............., an-1, an }` we have to find a longest subset such that for all `j` and `i`,  `j<i` in the subset `aj<ai`.
First of all we have to find the value of the longest subsequences(LSi) at every index i with last element of sequence being ai. Then largest LSi would be the longest subsequence in the given sequence. To begin LSi is assigned to be one since ai is element of the sequence(Last element). Then for all `j` such that `j<i` and `aj<ai`, we find Largest LSj and add it to LSi. Then algorithm take *O(n2)* time.

Pseudo-code for finding the length of the longest increasing subsequence:
This algorithms complexity could be reduced by using better data structure rather than array. Storing predecessor array and variable like largest_sequences_so_far and its index would save a lot time.

Similar concept could be applied in finding longest path in Directed acyclic graph.

```python
for i=0 to n-1
    LS[i]=1
    for j=0 to i-1
        if (a[i] >  a[j] and LS[i]<LS[j])
            LS[i] = LS[j]+1
for i=0 to n-1
    if (largest < LS[i])
```

### Some Famous DP Problems

- Floyd Warshall Algorithm - Tutorial and C Program source code:http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code 
- Integer Knapsack Problem - Tutorial and C Program source code: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem 
- Longest Common Subsequence - Tutorial and C Program source code : http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence 

## Online Resources

* [codechef](https://www.codechef.com/wiki/tutorial-dynamic-programming)
---
language: edn
filename: learnedn.edn
contributors:
  - ["Jason Yeo", "https://github.com/jsyeo"]
  - ["Jonathan D Johnston", "https://github.com/jdjohnston"]
---

Extensible Data Notation (EDN) is a format for serializing data.

EDN is a subset of the syntax used by Clojure. Reading data defined by EDN is
safer than that defined by the full Clojure syntax, especially from untrusted
sources. EDN is restricted to data, no code. It is similar in intent to JSON.
Though it is more commonly used in Clojure, there are implementations of EDN
for many other languages.

The main benefit of EDN over JSON and YAML is that it is extensible. We
will see how it is extended later on.

```clojure
; Comments start with a semicolon.
; Anything after the semicolon is ignored.

;;;;;;;;;;;;;;;;;;;
;;; Basic Types ;;;
;;;;;;;;;;;;;;;;;;;

nil         ; also known in other languages as null

; Booleans
true
false

; Strings are enclosed in double quotes
"hungarian breakfast"
"farmer's cheesy omelette"

; Characters are preceded by backslashes
\g \r \a \c \e

; Keywords start with a colon. They behave like enums. Kind of
; like symbols in Ruby.
:eggs
:cheese
:olives

; Symbols are used to represent identifiers. They start with #.
; You can namespace symbols by using /. Whatever precedes / is
; the namespace of the name.
#spoon
#kitchen/spoon ; not the same as #spoon
#kitchen/fork
#github/fork   ; you can't eat with this

; Integers and floats
42
3.14159

; Lists are sequences of values
(:bun :beef-patty 9 "yum!")

; Vectors allow random access
[:gelato 1 2 -2]

; Maps are associative data structures that associate the key with its value
{:eggs        2
 :lemon-juice 3.5
 :butter      1}

; You're not restricted to using keywords as keys
{[1 2 3 4] "tell the people what she wore",
 [5 6 7 8] "the more you see the more you hate"}

; You may use commas for readability. They are treated as whitespace.

; Sets are collections that contain unique elements.
#{:a :b 88 "huat"}

;;;;;;;;;;;;;;;;;;;;;;;
;;; Tagged Elements ;;;
;;;;;;;;;;;;;;;;;;;;;;;

; EDN can be extended by tagging elements with # symbols.

#MyYelpClone/MenuItem {:name "eggs-benedict" :rating 10}

; Let me explain this with a clojure example. Suppose I want to transform that
; piece of EDN into a MenuItem record.

(defrecord MenuItem [name rating])

; To transform EDN to clojure values, I will need to use the built in EDN
; reader, edn/read-string

(edn/read-string "{:eggs 2 :butter 1 :flour 5}")
; -> {:eggs 2 :butter 1 :flour 5}

; To transform tagged elements, define the reader function and pass a map
; that maps tags to reader functions to edn/read-string like so

(edn/read-string {:readers {'MyYelpClone/MenuItem map->menu-item}}
                 "#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}")
; -> #user.MenuItem{:name "eggs-benedict", :rating 10}

```

# References

- [EDN spec](https://github.com/edn-format/edn)
- [Implementations](https://github.com/edn-format/edn/wiki/Implementations)
- [Tagged Elements](http://www.compoundtheory.com/clojure-edn-walkthrough/)
---
language: css
contributors:
    - ["Kostas Bariotis", "http://kostasbariotis.com"]
filename: css-gr.html.markdown
lang: el-gr
---

Η αρχική μορφή του Παγκόσμιου Ιστού αποτελείτο απο καθαρό κείμενο, χωρίς οπτικά αντικείμενα. Με το πέρας 
του χρόνου και την εξέλιξη των Φυλλομετρητών, οι πλούσιες σελίδες, σε οπτικά και πολυμεσικά αντικείμενα, 
έγιναν καθημερινότητα.

Η CSS μας βοηθάει να διαχωρήσουμε το περιεχόμενο της σελίδας μας (HTML) απο την οπτική της περιγραφή.

Με την CSS ορίζουμε οπτικές ιδιότητες (χρώμα, μέγεθος, κλπ) σε HTML αντικείμενα (H1, div, κλπ).

```css
/* Τα σχόλια εμφανίζονται εντός καθέτου-αστερίσκου, όπως εδώ. 
   Δεν υπάρχουν σχόλια μια γραμμής και πολλών. */

/* ####################
   ## ΚΑΝΟΝΕΣ
   #################### */

/* ένας κανόνας χρησιμοποιείται για να στοχεύσουμε ένα αντικείμενο (selector). */
selector { property: value; /* περισσότερες ιδιότητες...*/ }

/*
Αυτό είναι ενα παράδειγμα αντικειμένου
<div class='class1 class2' id='anID' attr='value' otherAttr='en-us foo bar' />
*/

/* Μπορούμε να το στοχεύσουμε με την χρήση CSS κλάσεων */
.class1 { }

/* Ή και με τις δύο κλάσεις! */
.class1.class2 { }

/* Και με το όνομα του */
div { }

/* Ή με το id του */
#anID { }

/* Ή με το γεγονός ότι περιέχει ενα attribute */
[attr] { font-size:smaller; }

/* Ή οτι το attribute αυτό έχει μια συγκεκριμένη τιμή */
[attr='value'] { font-size:smaller; }

/* Ξεκινάει απο το λεκτικό (CSS 3) */
[attr^='val'] { font-size:smaller; }

/* Καταλήγει σε αυτο το λεκτικό (CSS 3) */
[attr$='ue'] { font-size:smaller; }

/* Περιέχει κάποιο λεκτικό */
[otherAttr~='foo'] { }
[otherAttr~='bar'] { }

/* περιέχει το λεκτικό σε λίστα χωρισμένη με παύλες, δηλαδή: "-" (U+002D) */
[otherAttr|='en'] { font-size:smaller; }


/* Μπορούμε να προσθέσουμε μεταξύ τους selectors για να δημιουργήσουμε πιο αυστηρούς. 
  Δεν βάζουμε κενά ανάμεσα. */
div.some-class[attr$='ue'] { }

/* Μπορούμε να επιλέξουμε αντικείμενα που βρίσκονται μέσα σε άλλα. */
div.some-parent > .class-name { }

/* Ή κάποιο αντικείμενο απόγονο ανεξαρτήτου του βάθους της σχέσης τους. */
div.some-parent .class-name { }

/* ΠΡΟΣΟΧΗ: ο ίδιος selector χωρίς κενά έχει άλλο νόημα. (Άσκηση προς τον αναγνώστη) */
div.some-parent.class-name { }

/* Μπορούμε να επιλέξουμε αντικείμενα με βάση το αμέσως επόμενο αντικείμενο στο ίδιο επίπεδο. */
.i-am-just-before + .this-element { }

/* Ή οποιοδήποτε αντικείμενο που προηγείται */
.i-am-any-element-before ~ .this-element { }

/* Με την βοήθεια των ψευδο-κλάσεων μπορούμε να επιλέξουμε αντικείμενα που βρίσκονται σε μια 
  ορισμένη κατάασταση. */

/* π.χ. όταν ο κέρσορας είναι πάνω απο ένα αντικείμενο */
selector:hover { }

/* ή ένας υπερσύνδεσμος που πατήθηκε */
selector:visited { }

/* ή που δεν πατήθηκε */
selected:link { }

/* ή ένα αντικείμενο που επιλέχθηκε */
selected:focus { }

/* οποιοδήποτε αντικείμενο είναι το πρώτο παιδί των γονέων του */
selector:first-child {}

/* οποιοδήποτε αντικείμενο είναι το πρώτοτελευταίο παιδί των γονέων του */
selector:last-child {}

/* Όπως και με τις ψευδο-κλάσεις, τα ψευδο-αντικείμενα μας επιτρέπουν τα τροποοιήσουμε συγκεκριμένα 
  κομμάτια της σελίδας */

/* επιλέγει το ψευδο-αντικείμενο ακριβώς πριν απο το αντικείμενο */
selector::before {}

/* επιλέγει το ψευδο-αντικείμενο ακριβώς μετά απο τον αντικείμενο */
selector::after {}

/* Σε σωστά σημεία (όχι πολύ ψηλά στην ιεραρχία) ο αστερίσκος μπορείς να χρησιμοποιηθεί για να 
  επιλέξουμε όλα τα αντικείμενα */
* { } /* όλα τα αντικείμενα της σελίδας */
.parent * { } /* όλους τους απόγονους */
.parent > * { } /* όλους τους απόγονους πρώτου επιπέδου */

/* ####################
   ## Ιδιότητες
   #################### */

selector {
    
    /* Οι μονάδες μπορούν να είναι είτε απόλυτες είτε σχετικές */
    
    /* Σχετικές μονάδες */
    width: 50%;       /* ποσοστό επί του πλάτους του γονέα */
    font-size: 2em;   /* πολλαπλασιαστής της αρχικής τιμής του αντικειμένου */
    font-size: 2rem;  /* ή της τιμής του πρώτου αντικειμένου στην ιεραρχία */
    font-size: 2vw;   /* πολλαπλαστιαστής του 1% του οπτικού πλάτους */
    font-size: 2vh;   /* ή τους ύψους */
    font-size: 2vmin; /* οποιοδήποτε απο αυτα τα δύο είναι το μικρότερο */
    font-size: 2vmax; /* ή το μεγαλύτερο */
    
    /* Απόλυτες μονάδες */
    width: 200px;     /* pixels */
    font-size: 20pt;  /* στιγμες */
    width: 5cm;       /* εκατοστά */
    min-width: 50mm;  /* χιλιοστά */
    max-width: 5in;   /* ίντσες */
    
    /* Χρώματα */
    color: #F6E;                 /* σύντομη δεκαεξαδική μορφή */
    color: #FF66EE;              /* δεκαεξαδική μορφή */
    color: tomato;               /* χρώμα με το όνομα του (συγκεκριμένα χρώματα) */
    color: rgb(255, 255, 255);   /* τιμή RGB */
    color: rgb(10%, 20%, 50%);   /* τιμή RGB με ποσοστά */
    color: rgba(255, 0, 0, 0.3); /* τιμή RGBA (CSS3) σσ. 0 < a < 1 */
    color: transparent;          /* όπως και το παραπάνω με a = 0 */
    color: hsl(0, 100%, 50%);    /* τιμή hsl με ποσοστά (CSS 3) */
    color: hsla(0, 100%, 50%, 0.3); /* τιμή hsla με ποσοστά και a */
    
    /* Εικόνες μπορούν να τοποθετηθούν στον φόντο ενός αντικειμένου */
    background-image: url(/img-path/img.jpg);
    
    /* Γραμματοσειρές */
    font-family: Arial;
    /* εάν η γραμματοσειρα περιέχει κενά */
    font-family: "Courier New";
    /* εάν η πρώτη γραμματοσειρα δε βρεθεί εγκατεστημένη στο Λειτουργικό Σύστυμα, αυτόματα 
      επιλέγετε η δεύτερη, κ.κ.ε. */
    font-family: "Courier New", Trebuchet, Arial, sans-serif;
}
```

## Χρήση

Αποθηκεύουμε ένα αρχείο CSS με την επέκταση `.css`.

```xml
<!-- Πρέπει να συμπεριλάβουμε το αρχείο στην επικεφαλίδα(head) ενος HTML αρχείου.
  σσ. http://stackoverflow.com/questions/8284365 -->
<link rel='stylesheet' type='text/css' href='path/to/style.css' />

<!-- Μπορούμε να το ενσωματώσουμε -->
<style>
   a { color: purple; }
</style>

<!-- Ή απευθείας σε κάποιο αντικείμενο (inline) -->
<div style="border: 1px solid red;">
</div>
```

## Ειδικότητα των κανόνων (Cascading απο το αγγλικό τίτλο Cascading Style Sheets)

Ένα αντικείμενο μπορεί να στοχευθεί απο πολλούς κανόνες και μπορεί η ίδια ιδιότητα να 
περιλαμβάνετε σε πολλούς κανόνες. Σε αυτές της περιπτώσεις υπερισχύει πάντα ο πιο ειδικός 
κανόνας και απο αυτούς, αυτός που εμφανίζεται τελευταίος.

```css
/* A */
p.class1[attr='value']

/* B */
p.class1 { }

/* C */
p.class2 { }

/* D */
p { }

/* E */
p { property: value !important; }
```

```xml
<p style='/*F*/ property:value;' class='class1 class2' attr='value' />
```

Η σειρά θα είναι:

* `E` έχει μεγαλύτερο βάρος λόγω του `!important`. Κάλες πρακτικές λένε να το αποφεύγουμε.
* `F` επόμενο λόγω του inline κανόνα.
* `A` επόμενο λόγω του το οτι είναι πιο ειδικό. Περιέχει τρεις selectors.
* `C` επόμενο, λόγω του οτι εμφανίζεται μετα το Β και ας έχει την ίδια ειδικότητα.
* `B` επόμενο.
* `D` τελευταίο.

## Συμβατότητα

Τα περισσότερα απο τα παραπάνω ήδη υποστηρίζονται απο τους γνωστούς φυλλομετρητές. Άλλα θα πρέπει 
πάντα να ελέγχουμε πρωτου τους χρησιμοποιήσουμε.

## Περισσότερα

* Έλεγχος συμβατότητας, [CanIUse](http://caniuse.com).
* CSS Playground [Dabblet](http://dabblet.com/).
* [Mozilla Developer Network's CSS documentation](https://developer.mozilla.org/en-US/docs/Web/CSS)
* [Codrops' CSS Reference](http://tympanus.net/codrops/css_reference/)

## Μελέτη

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [Selecting elements using attributes](https://css-tricks.com/almanac/selectors/a/attribute/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
* [SASS](http://sass-lang.com/) and [LESS](http://lesscss.org/) for CSS pre-processing
* [CSS-Tricks](https://css-tricks.com)
---
language: java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Jakukyo Friel", "http://weakish.github.io"]
    - ["Madison Dickson", "http://github.com/mix3d"]
    - ["Simon Morgan", "http://sjm.io/"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
    - ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
    - ["Rachel Stiyer", "https://github.com/rstiyer"]
filename: LearnJava-gr.java
translators:
  - ["Andreas Loizou" , "https://github.com/lack3r/"]
lang: el-gr
---

H Java είναι μία γενικού-σκοπού, συντρέχων (concurrent), βασισμένη σε κλάσεις, 
αντικειμενοστρεφής (object oriented) γλώσσα προγραμματισμού.
[Διαβάστε περισσότερα εδώ.](http://docs.oracle.com/javase/tutorial/java/)

```java
// Τα σχόλια μονής γραμμής ξεκινούν με //
/*
Τα σχόλια πολλών γραμμών μοιάζουν κάπως έτσι.
*/
/**
Τα σχόλια JavaDoc μοιάζουν κάπως έτσι. Χρησιμοποιούνται για να περιγράψουν την 
Κλάση ή διάφορα χαρακτηριστικά της Κλάσης.
*/

// Εισαγωγή της κλάσης ArrayList η οποία βρίσκεται εντός του πακέτου java.util
import java.util.ArrayList;
// Εισαγωγή όλων των κλάσεων που βρίσκονται εντός του πακέτου java.security
import java.security.*;

// Κάθε αρχείο .java περιέχει μία δημόσια(public) κλάση εξωτερικού-επιπέδου 
// (outer-level), η οποία έχει το ίδιο ονομα με το αρχείο.
public class LearnJava {

    // Για να τρέξει ένα πρόγραμμα java, πρέπει να έχει μία κύρια μέθοδο (main 
    // method) ως αρχικό σημείο.
    public static void main (String[] args) {

        // Χρησιμοποιούμε τη μέθοδο System.out.println() για να τυπώσουμε 
        // γραμμές.
        System.out.println("Hello World!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // Για να τυπώσουμε χωρίς να τυπωθεί αλλαγή γραμμής (newline), 
        // χρησιμοποιούμε System.out.print().
        System.out.print("Hello ");
        System.out.print("World");

        // Χρησιμοποιούμε τη μέθοδο System.out.printf() για έυκολη μορφοποίηση 
        // της εκτύπωσης.
        System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159

        ///////////////////////////////////////
        // Μεταβλητές(Variables)
        ///////////////////////////////////////

        /*
        *  Δήλωση Μεταβλητών
        */
        // Δηλώνουμε μία μεταβλητή χρησιμοποιώντας τη μορφή 
        // <Τύπος Μεταβλητής> <Όνομα Μεταβλητής>
        int fooInt;
        // Δηλώνουμε πολλαπλές μεταβλητές ίδιου τύπου χρησιμοποιώντας τη μορφή 
        // <Τύπος> <Όνομα1>, <Όνομα2>, <Όνομα3>
        int fooInt1, fooInt2, fooInt3;

        /*
        *  Αρχικοποίηση Μεταβλητών
        */

        // Αρχικοποιούμε μια μεταβλητή χρησιμοποιώντας τη μορφή 
        // <τύπος> <όνομα> = <τιμή>
        int fooInt = 1;
        // Αρχικοποιούμε πολλαπλές μεταβλητές ιδίου τύπου με την ίδια τιμή 
        // χρησιμοποιώντας <τύπος> <Όνομα1>, <Όνομα2>, <Όνομα3> = <τιμή>
        int fooInt1, fooInt2, fooInt3;
        fooInt1 = fooInt2 = fooInt3 = 1;

        /*
        *  Τύποι μεταβλητών
        */
        // Byte - 8-bit signed two's complement integer
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short - 16-bit signed two's complement integer
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - 32-bit signed two's complement integer
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int fooInt = 1;

        // Long - 64-bit signed two's complement integer
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // Το L χρησιμοποιείται για να δηλώσει ότι η συγκεκριμένη τιμή της 
        // μεταβλητής είναι τύπου Long;
        // Ό,τιδήποτε χρησιμοποιείται χωρίς αυτό τυχαίνει μεταχείρισης όπως 
        // μία τιμή μεταβλητής integer by default.

        // Σημείωση: Η Java δεν έχει unsigned τύπους.

        // Float - Single-precision 32-bit IEEE 754 Floating Point
        // 2^-149 <= float <= (2-2^-23) * 2^127 
        float fooFloat = 234.5f;
        // f or F χρησιμοποιείται για να δηλώσει ότι η συγκεκριμένη τιμή 
        // μεταβλητής είναι τύπου float;
        // αλλιώς τυγχαίνει μεταχείρισης όπως μία τιμή μεταβλητής double.

        // Double - Double-precision 64-bit IEEE 754 Floating Point
        // 2^-1074 <= x <= (2-2^-52) * 2^1023
        double fooDouble = 123.4;

        // Boolean - Αληθής και Ψευδής (true & false)
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - Ένας μόνο χαρακτήρας 16-bit Unicode
        char fooChar = 'A';

        // Οι μεταβλητές final δεν μπορούν να πάρουν άλλη τιμή 
        // μετά την αρχικοποίηση τους,
        final int HOURS_I_WORK_PER_WEEK = 9001;
        // αλλά μπορούν να αρχικοποιηθούν αργότερα.
        final double E;
        E = 2.71828;


        // BigInteger - Immutable αυθαίρετης-ακρίβειας ακέραιος
        //
        // Ο BigInteger είναι ένας τύπος δεδομένων ο οποίος επιτρέπει στους 
        // προγραμματιστές να χειρίζονται ακέραιους μεγαλύτερους από 64-bits. 
        // Οι ακέραιοι αποθηκεύονται ως πίνακας από bytes και τυχαίνουν 
        // επεξεργασίας χρησιμοποιώντας συναρτήσεις εσωματωμένες στην κλάση 
        // BigInteger
        // Ένας BigInteger μπορεί να αρχικοποιηθεί χρησιμοποιώντας ένα πίνακα 
        // από bytes ή γραμματοσειρά (string).
        
        BigInteger fooBigInteger = new BigInteger(fooByteArray);


        // BigDecimal - Immutable, αυθαίρετης-ακρίβειας, εμπρόσημος (signed)
        // δεκαδικός αριθμός
        //
        // Ένας BigDecimal παίρνει δύο μέρη: Μία αυθαίρετης ακρίβειας, 
        // ακέραια, unscaled τιμή και μία κλιμάκωση(scale) ως ένα 32-bit 
        // ακέραιο (integer).
        //
        // Ο BigDecimal επιτρέπει στον προγραμματιστή να έχει πλήρη έλεγχο 
        // όσον αφορά τη δεκαδική στρογγυλοποίηση (rounding). Προτείνεται η 
        // χρήση του BigDecimal με τιμές νομισμάτων και όπου απαιτείται η 
        // ακριβής δεκαδική ακρίβεια.
        //
        // Ο BigDecimal μπορεί να αρχικοποιηθεί με int, long, double ή String
        // ή με την αρχικοποίηση της unscaled τιμής (BigInteger) και της 
        // κλίμακας (scale) (int).

        BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt);
        
        // Χρειάζεται να είμαστε προσεκτικοί με τον κατασκευαστή (constructor) 
        // ο οποίος παίρνει float ή double καθώς η ανακρίβεια του float/double 
        // θα αντιγραφεί στον BigDecimal.
        // Είναι προτιμότερο να χρησιμοποιείται ο κατασκευαστής String (String 
        // constructor) όταν χρειάζεται ακριβής τιμή.
        
        BigDecimal tenCents = new BigDecimal("0.1");

        // Strings - Γραμματοσειρές
        String fooString = "My String Is Here!";

        // Ο χαρακτήρας \n είναι ένας χαρακτήρας διαφυγής (escaped character) 
        // ο οποίος ξεκινά μία νέα γραμμή
        String barString = "Printing on a new line?\nNo Problem!";
        // Ο χαρακτήρας \t είναι ένας χαρακτήρας διαφυγής (escaped character)
        // ο οποίος προσθέτει ένα χαρακτήρα tab
        String bazString = "Do you want to add a tab?\tNo Problem!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // Πίνακες (Arrays)
        // Το μέγεθος του πίνακα πρέπει να αποφασιστεί με την αρχικοποίηση του
        // πίνακα
        // Οι ακόλουθες μορφές μπορούν να χρησιμοποιηθούν για την δήλωση ενός 
        // πίνακα 
        // <Τυπος δεδομένων>[] <Όνομα Μεταβλητής> = new <Τύπος Δεδομένων>[<μέγεθος πίνακα>];
        // <Τυπος δεδομένων> <Όνομα Μεταβλητής>[] = new <Τυπος δεδομένων>[<μέγεθος πίνακα>];
        int[] intArray = new int[10];
        String[] stringArray = new String[1];
        boolean boolArray[] = new boolean[100];

        // Ακόμη ένας τρόπος για να δηλώσεις (to declare) και να 
        // αρχικοποιήσεις ένα πίνακα
        int[] y = {9000, 1000, 1337};
        String names[] = {"Bob", "John", "Fred", "Juan Pedro"};
        boolean bools[] = new boolean[] {true, false, false};

        // Ευρετηρίαση (indexing) ενός πίνακα - Πρόσβαση (accessing) ενός 
        // στοιχείου
        System.out.println("intArray @ 0: " + intArray[0]);

        // Οι πίνακες ξεκινούν από το μηδέν (zero-indexed) και είναι ευμετάβλητοι (mutable).
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // Παρόμοια
        // ArrayLists - Παρόμοιοι με τους πίνακες με τη διαφορά ότι προσφέρουν 
        // περισσότερη λειτουργικότητα και το μέγεθος είναι ευμετάβλητο 
        // (mutable).
        // LinkedLists - Υλοποίηση διπλά-συνδεδεμένης λίστας(doubly-linked 
        // list). Όλες οι λειτουργίες εκτελώνται όπως αναμένεται σε μία διπλά 
        // συνδεδεμένη (doubly-linked) λίστα.
        // Maps - Ένα σύνολο αντικειμένων τα οποία συνδέου (map) κλειδιά (keys)
        // σε τιμές (values). Ο Map είναι διεπαφή (interface) και συνεπώς δεν 
        // μπορεί να συγκεκριμενοποίηθεί (instantiated).
        // Ο τύπος των κλειδιών και των τιμών τα οποία συμπεριλαμβάνονται σε 
        // ένα Map πρέπει να καθοριστεί κατά τη διάρκεια της 
        // συγκεκριμενοποίησης (instantiation) της κλάσης που υλοποιεί τη 
        // διεπαφή Map. Κάθε κλειδί (key) μπορεί να συνδεθεί (map) σε μόνο μία 
        // αντίστοιχη τιμή και κάθε κλειδί μπορεί να εμφανιστεί μόνο μία φορά 
        // (no duplicates).
        // HashMaps - Η κλάση αυτή χρησιμοποιεί ένα πίνακα-κατακερματισμού 
        // (hashtable) για να υλοποιήσει τη διεπαφή Map. Αυτό επιτρέπει το 
        // χρόνο εκτέλεσης βασικών λειτουργιών, όπως της get και insert 
        // στοιχείου να παραμείνει σταθερός (constant) ακόμη και για μεγάλα 
        // σύνολα (sets.)


        ///////////////////////////////////////
        // Τελεστές (Operators)
        ///////////////////////////////////////
        System.out.println("\n->Operators");

        int i1 = 1, i2 = 2; // Συντομογραφία για πολλαπλές δηλώσεις

        // Οι αριθμητικοί τελεστές είναι απλοί
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (int/int returns int)
        System.out.println("1/2 = " + (i1 / (double)i2)); // => 0.5

        // Υπόλοιπο (Modulo)
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Τελεστές σύγκρισης
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Λογικοί Τελεστές (Boolean)
        System.out.println("3 > 2 && 2 > 3? " + ((3 > 2) && (2 > 3))); // => false
        System.out.println("3 > 2 || 2 > 3? " + ((3 > 2) || (2 > 3))); // => true
        System.out.println("!(3 == 2)? " + (!(3 == 2))); // => true

        // Τελεστές πράξεων με bits (Bitwise)!
        /*
        ~      bitwise τελεστής μοναδιαίου συμπληρώματος (Unary bitwise complement)
        <<     Προσημασμένη ολίσθηση αριστερά (Signed left shift)
        >>     Προσημασμένη/Αριθμητική ολίσθηση Δεξιά (Signed/Arithmetic right shift)
        >>>    Μη προσημασμένη/Λογική ολίσθηση δεξιά (Unsigned/Logical right shift)
        &      Διαδικός τελεστής AND (Bitwise AND)
        ^      Διαδικός τελεστής XOR (Bitwise exclusive OR)
        |      Διαδικός τελεστής OR (Bitwise inclusive OR)
        */

        // Αυξητικοί τελεστές
        int i = 0;
        System.out.println("\n->Inc/Dec-rementation");
        // Οι τελεστές ++ και -- μειώνουν και αυξάνουν κατά 1 αντίστοιχα.
        // Εάν τοποθετητούν πριν τη μεταβλητή, αυξάνουν και μετά επιστρέφουν.
        // Μετά τη μεταβλητή επιστρέφουν και μετά αυξάνουν.
        System.out.println(i++); // i = 1, τυπώνει 0 (post-increment)
        System.out.println(++i); // i = 2, τυπώνει 2 (pre-increment)
        System.out.println(i--); // i = 1, τυπώνει 2 (post-decrement)
        System.out.println(--i); // i = 0, τυπώνει 0 (pre-decrement)

        ///////////////////////////////////////
        // Δομές ελέγχου (Control Structures)
        ///////////////////////////////////////
        System.out.println("\n->Control Structures");

        // Οι δηλώσεις If είναι c-like
        int j = 10;
        if (j == 10) {
            System.out.println("I get printed");
        } else if (j > 10) {
            System.out.println("I don't");
        } else {
            System.out.println("I also don't");
        }

        // Επανάληψη While (While loop)
        int fooWhile = 0;
        while(fooWhile < 100) {
            System.out.println(fooWhile);
            // Άυξησε τον μετρητή
            // Επανάλαβε 100 φορές, fooWhile 0,1,2...99
            fooWhile++;
        }
        System.out.println("fooWhile Value: " + fooWhile);

        // Επανάληψη Do While (Do While Loop)
        int fooDoWhile = 0;
        do {
            System.out.println(fooDoWhile);
            // Άυξησε το μετρητή(counter)
            // Επανάλαβε 99 times, fooDoWhile 0->99
            fooDoWhile++;
        } while(fooDoWhile < 100);
        System.out.println("fooDoWhile Value: " + fooDoWhile);

        // Επανάληψη For (For Loop)
        // Δομή επανάληψης for => 
        // for(<Αρχική Δήλωση>; <προυπόθεση (conditional)>; <βήμα (step)>)
        for (int fooFor = 0; fooFor < 10; fooFor++) {
            System.out.println(fooFor);
            // Iterated 10 times, fooFor 0->9
        }
        System.out.println("fooFor Value: " + fooFor);
        
        // Έξοδος από εμφωλευμένη (nested) επανάληψη For με ετικέττα (Label)
        outer:
        for (int i = 0; i < 10; i++) {
          for (int j = 0; j < 10; j++) {
            if (i == 5 && j ==5) {
              break outer;
              // δραπετεύει εκτός της εξωτερικής(outer) επανάληψης αντί μόνο της εσωτερικής
            }
          }
        }
        
        // Επανάληψη For Each
        // Η επανάληψη for είναι επίσης ικανή να επαναλαμβάνεται τόσο σε 
        // πίνακες όσο και σε αντικείμενα τα οποία υλοποιούν τη διεπαφή 
        // Iterable.
        int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        // Σύνταξη της επανάληψης for each => for (<αντικείμενο> : <iterable>)
        // Διαβάζεται ως: Για κάθε αντικείμενο στο iterable
        // Σημείωση: ο τύπος του αντικειμένου πρέπει να τεριάζει με τον τύπο του στοιχείου του iterable.

        for (int bar : fooList) {
            System.out.println(bar);
            //Επαναλαμβάνεται 9 φορές και τυπώνει 1-9 σε καινούριες γραμμές
        }

        // Switch Case
        // Ένα switch δουλέυει με byte, short, char, και int τύπους δεδομένων.
        // Δουλέυει επίσης με τύπους enumerated (Συζήτηση στους τύπους Enum), 
        // τη κλάση String, και μερικές ειδικές περιπτώσεις οι οποίες 
        // περιλαμβάνουν primitive τύπους: Character, Byte, Short, and Integer.
        int month = 3;
        String monthString;
        switch (month) {
            case 1: monthString = "January";
                    break;
            case 2: monthString = "February";
                    break;
            case 3: monthString = "March";
                    break;
            default: monthString = "Some other month";
                     break;
        }
        System.out.println("Switch Case Result: " + monthString);
        
        // Αρχίζοντας από τη Java 7, switching για Strings δουλεύει έτσι:
        String myAnswer = "maybe";
        switch(myAnswer) {
            case "yes":
                System.out.println("You answered yes.");
                break;
            case "no":
                System.out.println("You answered no.");
                break;
            case "maybe":
                System.out.println("You answered maybe.");
                break;
            default:
                System.out.println("You answered " + myAnswer);
                break;
        }

        // Συντομογραφία του Conditional
        // Μπορείς να χρησιμοποιήσεις τον τελεστή '?' για γρήγορες αναθέσεις ή 
        // logic forks. Διαβάζεται ως "Αν η (πρόταση) είναι αληθής, 
        // χρησιμοποίησε <την πρώτη τιμή>, αλλιώς, χρησιμοποία <την δεύτερη 
        // τιμή>"
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println(bar); // Prints A, because the statement is true


        ////////////////////////////////////////
        // Μετατροπή Τύπων Δεδομένων και Typecasting
        ////////////////////////////////////////

        // Μετατροπή δεδομένων

        // Μετατροπή από String σε Integer
        Integer.parseInt("123");//returns an integer version of "123"

        // Μετατροπή από Integer σε String
        Integer.toString(123);//returns a string version of 123

        // Για άλλες μετατροπές δες τις ακόλουθες κλάσεις:
        // Double
        // Long
        // String

        // Typecasting
        // Μπορείς επίσης να κάνεις cast αντικείμενα Java. Υπάρχουν πολλές 
        // λεπτομέρειες και μερικές πραγματεύονται κάποιες πιο προχωρημένες 
        // ένοιες. Για δες εδώ:
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Κλάσεις και Συναρτήσεις
        ///////////////////////////////////////

        System.out.println("\n->Classes & Functions");

        // (Ο ορισμός της κλάσης Bicycle ακολουθεί)

        // Χρησιμοποία το new για να δημιουργήσεις ένα αντικείμενο μίας κλάσης
        Bicycle trek = new Bicycle();

        // Κλήση μεθόδων του αντικειμένου
        trek.speedUp(3); // Πάντοτε πρέπει να χρησιμοποιείς μεθόδους setter 
        // και getter
        trek.setCadence(100);

        // Το toString επιστρέφει την αναπαράσταση σε String μορφή του 
        // αντικειμένου αυτού.
        System.out.println("trek info: " + trek.toString());

        // Double Brace Initialization
        // Η Γλώσσα Java δεν έχει σύνταξη για το πως να δημιουργήσεις static 
        // Collections με κάποιο εύκολο τρόπο. Συνήθως θα το κάνεις αυτό με 
        // τον παρακάτω τρόπο:

        private static final Set<String> COUNTRIES = new HashSet<String>();
        static {
           validCodes.add("DENMARK");
           validCodes.add("SWEDEN");
           validCodes.add("FINLAND");
        }

        // Αλλά υπάρχει ένας κομψός τρόπος να επιτύχεις το ίδιο πράγμα
        // ευκολότερα, χρησιμοποιώντας κάτι το οποίο λέγεται Double Brace
        // Initialization.

        private static final Set<String> COUNTRIES = new HashSet<String>() {{
            add("DENMARK");
            add("SWEDEN");
            add("FINLAND");
        }}

        // Η πρώτη αγκύλη δημιουργεί μία νέα AnonymousInnerClass και η
        // δεύτερη δηλώνει ένα instance initializer block. Το block
        // καλείται όταν η ανώνυμη εσωτερική κλάση δημιουργηθεί.
        // Η μέθοδος αύτή δεν δουλεύει μόνο για τις Collections, αλλά για όλες 
        // τις non-final κλάσεις.

    } // Τέλος μεθόδου main
} // Τέλος κλάσης LearnJava


// Μπορείς να κάνεις include άλλες, όχι-δημόσιες (non-public) 
// εξωτερικού-επιπέδου (outer-level) κλάσεις σε ένα αρχείο .java, αλλά δεν 
// είναι καλή πρακτική. Αντί αυτού, διαχώρησε τις κλάσεις σε ξεχωριστά αρχεία.

// Σύνταξη Δήλωσης Κλάσης (Class Declaration Syntax):
// <public/private/protected> class <class name> {
//    // Συμπεριλαμβάνονται πεδία δεδομένων (data fields), κατασκευαστές (constructors), συναρτήσεις (functions) .
//    // Οι συναρτήσεις ονομάζονται "μεθόδοι" στη Java.
// }

class Bicycle {

    // Πεδία/μεταβλητές της Κλάσης Bicycle
    // Public(Δημόσιες): Μπορούν να γίνουν προσβάσιμες από παντού
    public int cadence; 
    // Private(Ιδιωτικές): Προσβάσιμες μόνο εντός της κλάσης
    private int speed;  
    // Protected(Προστατευμένες): Προσβάσιμες από την κλάση και τις υποκλάσεις (subclasses) της
    protected int gear; 
    String name; // Προκαθορισμένο: Προσβάσιμη μόνο εντός του πακέτου

    static String className; // Static μεταβλητή κλάσης

    // Static block 
    // H Java δεν υποστηρίζει υλοποίησεις στατικών κατασκευαστών (static 
    // constructors), αλλά έχει ένα static block το οποίο μπορεί να 
    // χρησιμοποιηθεί για να αρχικοποιήσει στατικές μεταβλητές (static 
    // variables). Το block αυτό θα καλεσθεί όταν η κλάση φορτωθεί.
    static {
        className = "Bicycle";
    }

    // Οι κατασκευαστές (constructors) είναι ένας τρόπος για δημιουργία κλάσεων
    // Αυτός είναι ένας κατασκευαστής (constructor)
    public Bicycle() {
        // Μπορείς επίσης να καλέσεις άλλο κατασκευαστή:
        // this(1, 50, 5, "Bontrager");
        gear = 1;
        cadence = 50;
        speed = 5;
        name = "Bontrager";
    }

    // Αυτός είναι ένας κατασκευαστής ο οποίος δέχεται arguments
    public Bicycle(int startCadence, int startSpeed, int startGear,
        String name) {
        this.gear = startGear;
        this.cadence = startCadence;
        this.speed = startSpeed;
        this.name = name;
    }

    // Οι μεθόδοι (Methods) συντάσσονται ως ακολούθως:
    // <public/private/protected> <return type> <όνομα μεθόδου>(<args>)

    // Οι κλάσεις Java συχνά υλοποιούν getters and setters for their fields

    // Σύνταξη δήλωσης μεθόδου:
    // <Προσδιοριστές πρόσβασης> <τύπος επιστροφής> <όνομα μεθόδου>(<args>)
    public int getCadence() {
        return cadence;
    }

    // Οι μεθόδοι void δεν απαιτούν return statement
    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void speedUp(int increment) {
        speed += increment;
    }

    public void slowDown(int decrement) {
        speed -= decrement;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    //Μέθοδος η οποία επιστρέφει ως String τις τιμές των χαρακτηριστικών του 
    // αντικειμένου.
    @Override // Χρησιμοποιείται, καθώς η συγκεκριμένη μέθοδος κληρονομήθηκε από τη κλάση Object.
    public String toString() {
        return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
            " name: " + name;
    }
} // Τέλος κλάσης Bicycle

// Η PennyFarthing είναι υποκλάση της Bicycle
class PennyFarthing extends Bicycle {
    // (Tα Penny Farthings είναι τα ποδήλατα με τον μεγάλο μπροστινό τροχό.
    // Δεν έχουν ταχύτητες.)

    public PennyFarthing(int startCadence, int startSpeed) {
        // Κάλεσε τον parent constructor χρησιμοποιώντας το super
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // Χρειάζεται να μαρκάρεις τη μέθοδο την οποία κάνεις overriding 
    // χρησιμοποιώντας ένα @annotation.
    // Για να μάθεις περισσότερα σχετικά με το τι είναι οι επισημάνσεις 
    // (annotations) και τον σκοπό τους δες αυτό: 
    // http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGear(int gear) {
        gear = 0;
    }
}

// Διεπαφές (Interfaces)
// Σύνταξη δήλωσης διεπαφής
// <access-level> interface <interface-name> extends <super-interfaces> {
//     // Σταθερές (Constants)
//     // Δηλώσεις Μεθόδων (Method declarations)
// }

// Παράδειγμα - Food:
public interface Edible {
    public void eat(); // Κάθε κλάση η οποία υλοποιεί τη διεπαφή αυτή πρέπει 
    // να υλοποιήσει τη συγκεκριμένη μέθοδο.
}

public interface Digestible {
    public void digest();
}


// Μπορούμε να δημιουργήσουμε μία κλάση η οποία υλοποιεί και τις δύο αυτές διεπαφές.
public class Fruit implements Edible, Digestible {
  
    @Override
    public void eat() {
        // ...
    }

    @Override
    public void digest() {
        // ...
    }
}

// Στην Java, μπορείς να κληρονομήσεις (extend) από μόνο μία κλάση,
// αλλά μπορείς να υλοποιήσεις πολλές διεπαφές. Για παράδειγμα:
public class ExampleClass extends ExampleClassParent implements InterfaceOne,
    InterfaceTwo {

    @Override
    public void InterfaceOneMethod() {
    }

    @Override
    public void InterfaceTwoMethod() {
    }

}

// Abstract (Αφηρημένες) Κλάσεις

// Σύνταξη Δήλωσης Abstract Κλάσης
// <access-level> abstract <abstract-class-name> extends <super-abstract-classes> {
//     // Σταθερές και μεταβλητές
//     // Δηλώσεις μεθόδων
// }

// Μαρκάροντας μία κλάση ως abstract σημαίνει ότι περιέχει abstract μεθόδους
// οι οποίες πρέπει να οριστούν σε μία κλάση παιδί (child class).
// Παρόμοια με τις διεπαφές (interfaces), οι abstract κλάσεις δεν μπορούν να
// γίνουν instantiated, αλλά αντί αυτού πρέπει να γίνει extend και οι abstract
// μεθόδοι πρέπει να οριστούν. Διαφορετικά από τις Διεπαφές, οι abstract 
// κλάσεις μπορούν να περιέχουν τόσο υλοποιημένες όσο και abstract μεθόδους.
// Οι μεθόδοι σε μια Διεπαφή δεν μπορούν να έχουν σώμα (δεν είναι υλοποιημένες 
// δηλαδή εκτός εάν η μέθοδος είναι στατική και οι μεταβλητές είναι final by 
// default αντίθετα απο μία abstract κλάση. Επίσης, οι abstract κλάσεις 
// ΜΠΟΡΟΥΝ να έχουν την μέθοδο "main".

public abstract class Animal
{
    public abstract void makeSound();

    // Οι μεθόδοι μπορούν να έχουν σώμα (body)
    public void eat()
    {
        System.out.println("I am an animal and I am Eating.");  
        // Σημείωση: Μπορούμε να έχουμε πρόσβαση σε ιδιωτικές (private) μεταβλητές εδώ.
        age = 30;
    }

    // Δεν χρειάζεται να αρχικοποιηθεί, εντούτοις σε ένα interface μία 
    // μεταβλητή είναι implicitly final και έτσι χρειάζεται να αρχικοποιηθεί
    protected int age;

    public void printAge()
    {
        System.out.println(age);  
    }

    // Οι Abstract κλάσεις μπορούν να έχουν συνάρτηση main.
    public static void main(String[] args)
    {
        System.out.println("I am abstract");
    }
}

class Dog extends Animal
{
    // Σημείωση ότι χρειάζεται να κάνουμε override τις abstract μεθόδους στην
    // abstract κλάση.
    @Override
    public void makeSound()
    {
        System.out.println("Bark");
        // age = 30;	==> ERROR!	Το πεδίο age είναι private στο Animal
    }

    // ΣΗΜΕΙΩΣΗ: Θα πάρεις error εάν χρησιμοποίησεις το
    // @Override annotation εδώ, καθώς η java δεν επιτρέπει
    // να γίνονται override οι static μεθόδοι.
    // Αυτό που γίνεται εδώ ονομάζεται METHOD HIDING.
    // Για δες αυτό το εξαιρετικό ποστ στο SO (Stack Overflow): http://stackoverflow.com/questions/16313649/
    public static void main(String[] args)
    {
        Dog pluto = new Dog();
        pluto.makeSound();
        pluto.eat();
        pluto.printAge();
    }
}

// Κλάσεις Final

// Σύνταξη δήλωσης μίας Final κλάσης
// <access-level> final <final-class-name> {
//     // Σταθερές και μεταβλητές
//     // Δήλωση μεθόδων
// }

// Οι κλάσεις Final είναι κλάσεις οι οποίες δεν μπορούν να κληρονομηθούν και 
// συνεπώς είναι final child. In a way, final classes are the opposite of 
// abstract classes because abstract classes must be extended, but final 
// classes cannot be extended.
public final class SaberToothedCat extends Animal
{
    // Σημείωση ότι χρειάζεται και πάλι να κάνουμε override τις abstract 
    // μεθόδους στην abstract κλάση.
    @Override
    public void makeSound()
    {
        System.out.println("Roar");
    }
}

// Τελικές (Final) μεθόδοι
public abstract class Mammal()
{
    // Σύνταξη μίας Final μεθόδου:
    // <Προσδιοριστής πρόσβασης (access modifier)> final <τύπος επιστροφής> <Όνομα μεθόδου>(<args>)

    // Οι Final μεθόδοι, όπως και οι final κλάσεις δεν μπορούν να γίνουν 
    // overridden από κλάση παιδί,
    // και είναι συνεπώς η τελική υλοποίηση της μεθόδου.
    public final boolean isWarmBlooded()
    {
        return true;
    }
}


// Τύποι Enum
//
// Ένας τύπος enum είναι ένας ειδικός τύπος δεδομένων, ο οποίος επιτρέπει σε 
// μια μεταβλητή να είναι ένα σύνολο από προκαθορισμένες σταθερές. Η μεταβλητή 
// πρέπει να είναι ίση με μία από τις τιμές αυτές που έχουν προκαθοριστεί. 
// Επειδή είναι σταθερές, τα ονόματα ενός enum πεδίου γράφονται με κεφαλαίους 
// χαρακτήρες. Στην γλώσσα προγραμματισμού Java, ορίζεις ένα τύπο enum 
// χρησιμοποιώντας τη δεσμευμένη λέξη enum. Για παράδειγμα, θα μπορούσες να 
// καθορίσεις ένα τύπο enum με όνομα days-of-the-week ως:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

// Μπορούμε να χρησιμοποιήσουμε τον enum Day όπως παρακάτω:

public class EnumTest {
    
    // Μεταβλητή Enum
    Day day;
    
    public EnumTest(Day day) {
        this.day = day;
    }
    
    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;
                    
            case FRIDAY:
                System.out.println("Fridays are better.");
                break;
                         
            case SATURDAY: 
            case SUNDAY:
                System.out.println("Weekends are best.");
                break;
                        
            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }
    
    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs(); // => Mondays are bad.
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs(); // => Midweek days are so-so.
    }
}

// Οι τύποι Enum είναι πολύ πιο δυνατοί από όσο έχουμε δείξει πιο πάνω. 
// Το σώμα του enum (enum body) μπορεί να περιέχει μεθόδους και άλλα πεδία.
// Μπορείς να δεις περισσότερα στο 
// https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

```

## Επιπλέων διάβασμα

Οι σύνδεσμοι που παρέχονται εδώ είναι απλά για να κατανοήσεις περισσότερο το θέμα.
Σε προτρύνουμε να ψάξεις στο Google και να βρεις συγκεκριμένα παραδείγματα.

**Eπίσημοι Οδηγοί της Oracle**:

* [Φροντιστήριο εκμάθησης Java από τη Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Τροποποιητές επιπέδου πρόσβασης(Access level modifiers) Java](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Έννοιες αντικειμενοστραφούς (Object-Oriented) προγραμματισμού](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Κληρονομικότητα (Inheritance)](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Πολυμορφισμός (Polymorphism)](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Αφαιρετικότητα (Abstraction)](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Εξαιρέσεις (Exceptions)](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Διεπαφές (Interfaces)](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Συμβάσεις κώδικα Java (Code Conventions)](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html)

**Πρακτικές και Φροντιστήρια Online**

* [Learneroo.com - Μάθε Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)


**Βιβλία**:

* [Head First Java](http://www.headfirstlabs.com/books/hfjava/)

* [Thinking in Java](http://www.mindview.net/Books/TIJ/)

* [Objects First with Java](http://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660)

* [Java The Complete Reference](http://www.amazon.com/gp/product/0071606300)
---
language: racket
filename: learnracket-gr.rkt
contributors:
  - ["th3rac25", "https://github.com/voila"]
  - ["Eli Barzilay", "https://github.com/elibarzilay"]
  - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
  - ["Duong H. Nguyen", "https://github.com/cmpitg"]
  - ["Keyan Zhang", "https://github.com/keyanzhang"]
translators:
  - ["Vasilis Panagiotopoulos" , "https://github.com/billpcs/"]
lang: el-gr
---

H Racket είναι μια γενικού σκοπού, πολυ-υποδειγματική γλώσσα προγραμματισμού που ανήκει 
στην οικογένεια της Lisp/Scheme

```racket
#lang racket ; ορίζει την γλώσσα που χρησιμοποιόυμε

;;; Σχόλια

;; Τα σχόλια μιας γραμμής ξεκινούν με ερωτηματικό

#| Τα σχόλια ολόκληρου μπλόκ
   μπορούν να εκτείνονται σε πολλές γραμμές και...
    #|
       μπορούν να είναι εμφωλευμένα!
    |#
|#

;; Τα σχόλια S-expression (εκφράσεις S) comments απορρίπτουν την
;; έκφραση που ακολουθεί, δυνατότητα που είναι χρήσιμη για να
;; κάνουμε σχόλια κάποιες εκφράσεις κατά τη διάρκεια του debugging

#; (αυτή η έκφραση δεν θα εκτελεστεί)

;; (Αν δεν καταλαβαίνεται τι είναι οι εκφράσεις , περιμένετε... Θα το μάθουμε
;; πολύ σύντομα!)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Πρωτογενείς τύποι μεταβλητών και τελεστές
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Αριθμοί
9999999999999999999999 ; ακέραιοι
#b111                  ; δυαδικοί => 7
#o111                  ; οκταδικοί => 73
#x111                  ; δεκαεξαδικοί => 273
3.14                   ; πραγματικοί
6.02e+23
1/2                    ; ρητοί
1+2i                   ; μιγαδικοί

;; Οι μορφή των συναρτήσεων είναι (f x y z)
;; όπου το f είναι η συνάρτηση και τα x y z
;; είναι οι όροι που η συνάρτηση δέχεται
;; ως ορίσματα. Αν θέλουμε να δημιουργήσουμε
;; μια λίστα στην κυριολεξία από δίαφορα δεδομένα,
;; χρησιμοποιούμε το ' για να το εμποδίσουμε από το να
;; αξιολογηθεί σαν έκφραση. Για παράδειγμα:
'(+ 1 2) ; => Παραμένει (+ 1 2) και δεν γίνεται η πράξη
;; Τώρα , ας κάνουμε μερικές πράξεις
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(expt 2 3) ; => 8
(quotient 5 2) ; => 2
(remainder 5 2) ; => 1
(/ 35 5) ; => 7
(/ 1 3) ; => 1/3
(exact->inexact 1/3) ; => 0.3333333333333333
(+ 1+2i  2-3i) ; => 3-1i

;;; Λογικές μεταβλητές
#t ; για το true (αληθής)
#f ; για το false (ψευδής)
(not #t) ; => #f
(and 0 #f (error "doesn't get here")) ; => #f
(or #f 0 (error "doesn't get here"))  ; => 0

;;; Χαρακτήρες
#\A ; => #\A
#\λ ; => #\λ
#\u03BB ; => #\λ

;;; Τα αλφαριθμητικά είναι πίνακες χαρακτήρων συγκεκριμένου μήκους
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; Το backslash είναι χαρακτήρας διαφυγής
"Foo\tbar\41\x21\u0021\a\r\n" ; Συμπεριλαμβάνονται οι χαρακτήρες διαφυγής της C,
							  ; σε Unicode
"λx:(μα.α→α).xx"              ; Μπορούν να υπάρχουν και Unicode χαρακτήρες

;; Μπορούμε να ενώσουμε αλφαριθμητικά!
(string-append "Hello " "world!") ; => "Hello world!"

;; Ένα αλφαριθμητικό μπορούμε να το χρησιμοποιήσουμε
;; όπως και μια λίστα από χαρακτήρες
(string-ref "Apple" 0) ; => #\A ;; Παίρνουμε το πρώτο στοιχείο

;; Η συνάρτηση format μπορεί να χρησιμοποιηθεί για
;; να μορφοποιήσουμε αλφαριθμητικά
(format "~a can be ~a" "strings" "formatted") ;; => "strings can be formatted"

;; Η εκτύπωση είναι εύκολη.
(printf "I'm Racket. Nice to meet you!\n")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Μεταβλητές
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Μπορούμε να δημιουργήσουμε μεταβλητές
;; χρησιμοποιώντας το define.
;; Ένα όνομα μεταβλητής μπορεί να χρησιμοποιεί οποιονδήποτε 
;; χαρακτήρα, εκτός από τους: ()[]{}",'`;#|\
(define some-var 5)
some-var ; => 5

;; Μπορούμε επίσης να χρησιμοποιήσουμε unicode χαρακτήρες.
(define ⊆ subset?) ;; Εδώ ουσιαστικά δίνουμε στη ήδη υπάρχουσα συνάρτηση subset?
				   ;; ένα νέο όνομα ⊆ , και παρακάτω την καλούμε με το νέο της όνομα.
(⊆ (set 3 2) (set 1 2 3)) ; => #t

;; Αν ζητήσουμε μια μεταβλητή που δεν έχει οριστεί πριν π.χ.
(printf name)
;; θα πάρουμε το παρακάτω μήνυμα
;name: undefined;
;  cannot reference undefined identifier
;   context...:

;; Η τοπική δέσμευση : `me' δεσμεύεται με το "Bob" μόνο μέσα στο (let ...)
(let ([me "Bob"])
  "Alice"
  me) ; => "Bob"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Δομές και συλλογές
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Δομές
(struct dog (name breed age))
(define my-pet
  (dog "lassie" "collie" 5))
my-pet ; => #<dog>
(dog? my-pet) ; => #t
(dog-name my-pet) ; => "lassie"

;;; Ζεύγη (αμετάβλητα)
;; Η δεσμευμένη λέξη `cons' δημιουργεί ζεύγη,
;; και το `car' και το `cdr' εξάγουν το πρώτο και
;; το δεύτερο στοιχείο αντίστοιχα.
(cons 1 2) ; => '(1 . 2)
(car (cons 1 2)) ; => 1
(cdr (cons 1 2)) ; => 2

;;; Λίστες

;; Οι λίστες είναι linked-list δομές δεδομένων,
;; που έχουν δημιουργηθεί από ζευγάρια 'cons'
;; και τελειώνουν με 'null' (ή αλλιώς '()) για να
;; δηλώσουν ότι αυτό είναι το τέλος της λίστας
(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3)
;; Η δεσμευμένη λέξη  'list' είναι ένας εναλλακτικός
;; (και σαφώς πιο βολικός) τρόπος για να δημιουργούμε
;; λίστες
(list 1 2 3) ; => '(1 2 3)
;; αλλά και χρησιμοποιώντας ένα μονό εισαγωγικό το
;; το αποτέλεσμα είναι και πάλι το ίδιο
'(1 2 3) ; => '(1 2 3)

;; Μπορούμε και πάλι όμως να χρησιμοποιούμε το 'cons' για να
;; προσθέσουμε ένα στοιχείο στην αρχή της λίστας
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; Μπορούμε να χρησιμοποιούμε το 'append' για να προσθέτουμε
;; στοιχεία στο τέλος μιας λίστας. Το στοιχείο αυτό μπορεί
;; και να είναι ολόκληρη λίστα!
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; Οι λίστες στην Racket είναι πολύ βασικές , οπότε υπάρχουν πολλές
;; δυνατές λειτουργίες για αυτές. Παρακάτω είναι μερικά παραδείγματα:
(map add1 '(1 2 3))          ; => '(2 3 4)
(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33)
(filter even? '(1 2 3 4))    ; => '(2 4)
(count even? '(1 2 3 4))     ; => 2
(take '(1 2 3 4) 2)          ; => '(1 2)
(drop '(1 2 3 4) 2)          ; => '(3 4)

;;; Διανύσματα

;; Τα διανύσματα είναι πίνακες σταθερού μήκους
#(1 2 3) ; => '#(1 2 3)

;; Χρησιμοποιούμε το `vector-append' για να προσθέσουμε διανύσματα
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; Σύνολα

;; Δημιουργούμε ένα σύνολο από μία λίστα
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)

;; Προσθέτουμε έναν αριθμό στο σύνολο χρησιμοποιώντας το `set-add'
(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)

;; Αφαιρούμε με το `set-remove'
(set-remove (set 1 2 3) 1) ; => (set 2 3)

;; Βλέπουμε αν υπάρχει ένας αριθμός στο σύνολο με το `set-member?'
(set-member? (set 1 2 3) 1) ; => #t
(set-member? (set 1 2 3) 4) ; => #f

;;; Πίνακες κατακερματισμού (Hashes)

;; Δημιουργήστε ένα αμετάβλητο πίνακα κατακερματισμού
(define m (hash 'a 1 'b 2 'c 3))

;; Παίρνουμε μια τιμή από τον πίνακα
(hash-ref m 'a) ; => 1

;; Αν ζητήσουμε μια τιμή που δεν υπάρχει παίρνουμε μία εξαίρεση
; (hash-ref m 'd) => no value found for key

;; Μπορούμε να δώσουμε μια default τιμή για τα κλειδιά που λείπουν
(hash-ref m 'd 0) ; => 0


;; Χρησιμοποιούμε το 'hash-set' για να επεκτείνουμε
;; ένα πίνακα κατακερματισμού
(define m2 (hash-set m 'd 4))
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))

;; Θυμηθείτε ! Αυτοί οι πίνακες κατακερματισμού
;; είναι αμετάβλητοι!
m ; => '#hash((b . 2) (a . 1) (c . 3))  <-- δεν υπάρχει `d'

;; Χρησιμοποιούμε το `hash-remove' για να αφαιρέσουμε
;; κλειδιά
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Συναρτήσεις
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Χρησιμοποιούμε το `lambda' για να δημιουργήσουμε συναρτήσεις.
;; Μια συνάρτηση πάντα επιστρέφει την τιμή της τελευταίας της έκφρασης
(lambda () "Hello World") ; => #<procedure>
;; Μπορούμε επίσης να χρησιμοποιήσουμε το `λ'
(λ () "Hello World")     ; => Ίδια συνάρτηση

;; Χρησιμοποιούμε τις παρενθέσεις για να καλέσουμε όλες τις συναρτήσεις
;; συμπεριλαμβανομένων και των εκφράσεων 'λάμδα'
((lambda () "Hello World")) ; => "Hello World"
((λ () "Hello World"))      ; => "Hello World"

;; Εκχωρούμε σε μια μεταβλητή την συνάρτηση
(define hello-world (lambda () "Hello World"))
(hello-world) ; => "Hello World"

;; Μπορούμε αυτό να το κάνουμε συντομότερο χρησιμοποιώντας
;; το λεγόμενο syntactic sugar :
(define (hello-world2) "Hello World")

;; Το () στο παραπάνω είναι η λίστα από τα ορίσματα για την συνάρτηση

(define hello
  (lambda (name)
    (string-append "Hello " name)))
(hello "Steve") ; => "Hello Steve"
;; ... ή ισοδύναμα, χρησιμοποιώντας sugared ορισμό:
(define (hello2 name)
  (string-append "Hello " name))

;; Μπορούμε να έχουμε συναρτήσεις με πολλές μεταβλητές χρησιμοποιώντας
;; το `case-lambda'
(define hello3
  (case-lambda
    [() "Hello World"]
    [(name) (string-append "Hello " name)]))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"
;; ... ή να ορίσουμε προαιρετικά ορίσματα με μια έκφραση προκαθορισμένης τιμής
(define (hello4 [name "World"])
  (string-append "Hello " name))

;; Οι συναρτήσεις μπορούν να πακετάρουν επιπλέον
;; ορίσματα μέσα σε μια λίστα
(define (count-args . args)
  (format "You passed ~a args: ~a" (length args) args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
;; ... ή με unsugared μορφή `lambda':
(define count-args2
  (lambda args
    (format "You passed ~a args: ~a" (length args) args)))

;; Μπορούμε να εμπλέξουμε κανονικά και πακεταρισμένα ορίσματα
(define (hello-count name . args)
  (format "Hello ~a, you passed ~a extra args" name (length args)))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"
;; ... και unsugared:
(define hello-count2
  (lambda (name . args)
    (format "Hello ~a, you passed ~a extra args" name (length args))))

;; Και με λέξεις κλειδιά
(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
  (format "~a ~a, ~a extra args" g name (length args)))
(hello-k)                 ; => "Hello World, 0 extra args"
(hello-k 1 2 3)           ; => "Hello World, 3 extra args"
(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
                                         ; => "Hi Finn, 6 extra args"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Ισότητα
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; για αριθμούς χρησιμοποιούμε το `='
(= 3 3.0) ; => #t
(= 2 1)   ; => #f

;; Το `eq?' επιστρέφει #t αν δύο 2 ορίσματα αναφέρονται στο
;; ίδιο αντικείμενο (στη μνήμη),αλλιώς επιστρέφει #f.
;; Με άλλα λόγια, είναι απλή σύγκριση δεικτών.
(eq? '() '()) ; => #t, αφού υπάρχει μόνο μια άδεια λίστα στη μνήμη
(let ([x '()] [y '()])
  (eq? x y))  ; => #t, το ίδιο με πάνω

(eq? (list 3) (list 3)) ; => #f
(let ([x (list 3)] [y (list 3)])
  (eq? x y))            ; => #f — δεν είναι η ίδια λίστα στην μνήμη!

(let* ([x (list 3)] [y x])
  (eq? x y)) ; => #t, Αφού το x και το y τώρα δείχνουν στην ίδια θέση

(eq? 'yes 'yes) ; => #t
(eq? 'yes 'no)  ; => #f

(eq? 3 3)   ; => #t — να είστε προσεκτικοί εδώ
            ; Είναι προτιμότερο να χρησιμοποιείτε `=' για την
            ; σύγκριση αριθμών.
(eq? 3 3.0) ; => #f

(eq? (expt 2 100) (expt 2 100))               ; => #f
(eq? (integer->char 955) (integer->char 955)) ; => #f

(eq? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f

;; Το `eqv?' υποστηρίζει την σύγκριση αριθμών αλλά και χαρακτήρων
;; Για άλλα ήδη μεταβλητών το `eqv?' και το `eq?' επιστρέφουν το ίδιο.
(eqv? 3 3.0)                                   ; => #f
(eqv? (expt 2 100) (expt 2 100))               ; => #t
(eqv? (integer->char 955) (integer->char 955)) ; => #t

(eqv? (string-append "foo" "bar") (string-append "foo" "bar"))   ; => #f

;; Το `equal?' υποστηρίζει την σύγκριση των παρακάτω τύπων μεταβλητών:
;; αλφαριθμητικά, αλφαριθμητικά από bytes, μεταβλητά ζεύγη , διανύσματα,
;; πίνακες κατακερματισμού και δομές.
;; Για άλλα ήδη τύπων μεταβλητών το `equal?' και το `eqv?' επιστρέφουν το
;; ίδιο αποτέλεσμα.
(equal? 3 3.0)                                                   ; => #f
(equal? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #t
(equal? (list 3) (list 3))                                       ; => #t

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Έλεγχος Ροής
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Συνθήκες (conditionals)

(if #t               ; έκφραση ελέγχου
    "this is true"   ; έκφραση then
    "this is false") ; έκφραση else
; => "this is true"


;; Στα conditionals, όλες οι μη #f τιμές θεωρούνται ως #t
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'yep

;; Οι αλυσίδες `cond' είναι σειρές από ελέγχους για να
;; επιλεγεί ένα αποτέλεσμα
(cond [(> 2 2) (error "wrong!")]
      [(< 2 2) (error "wrong again!")]
      [else 'ok]) ; => 'ok

;;; Αντιστοίχιση μοτίβων

(define (fizzbuzz? n)
  (match (list (remainder n 3) (remainder n 5))
    [(list 0 0) 'fizzbuzz]
    [(list 0 _) 'fizz]
    [(list _ 0) 'buzz]
    [_          #f]))

(fizzbuzz? 15) ; => 'fizzbuzz
(fizzbuzz? 37) ; => #f

;;; Βρόχοι

;; Οι επαναλήψεις μπορούν να γίνουν μέσω αναδρομής
(define (loop i)
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i))))
(loop 5) ; => i=5, i=6, ...

;; Παρομοίως με τη χρήση 'let'
(let loop ((i 0))
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i)))) ; => i=0, i=1, ...


;; Θα δείτε παρακάτω πως να προσθέσουμε μια νέα μορφή επανάληψης
;; αλλά η Racket έχει ήδη πολύ ευέλικτη μορφή για τους βρόχους
(for ([i 10])
  (printf "i=~a\n" i)) ; => i=0, i=1, ...
(for ([i (in-range 5 10)])
  (printf "i=~a\n" i)) ; => i=5, i=6, ...

;;;
;;; Επανάληψη μέσα σε ακολουθίες:
;; Το `for' επιτρέπει την επανάληψη μέσα σε πολλά
;; άλλα ήδη από ακολουθίες: Λίστες, διανύσματα,
;; αλφαριθμητικά, σύνολα κτλ..

(for ([i (in-list '(l i s t))])
  (displayln i))

(for ([i (in-vector #(v e c t o r))])
  (displayln i))

(for ([i (in-string "string")])
  (displayln i))

(for ([i (in-set (set 'x 'y 'z))])
  (displayln i))

(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
  (printf "key:~a value:~a\n" k v))

;;; Πιο περίπλοκες επαναλήψεις

;; Παράλληλη σάρωση σε πολλαπλές ακολουθίες
;; (σταματά στην πιο σύντομη)
(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x 1:y 2:z

;; Εμφολευμένοι βρόχοι
(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z

;; Συνθήκες
(for ([i 1000]
      #:when (> i 5)
      #:unless (odd? i)
      #:break (> i 10))
  (printf "i=~a\n" i))
; => i=6, i=8, i=10

;;; Σάρωση σε λίστες
;; Παρόμοιο με τους βρόχους 'for', απλά συλλέγουμε τα αποτελέσματα

(for/list ([i '(1 2 3)])
  (add1 i)) ; => '(2 3 4)

(for/list ([i '(1 2 3)] #:when (even? i))
  i) ; => '(2)

(for/list ([i 10] [j '(x y z)])
  (list i j)) ; => '((0 x) (1 y) (2 z))

(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10))
  i) ; => '(6 8 10)

(for/hash ([i '(1 2 3)])
  (values i (number->string i)))
; => '#hash((1 . "1") (2 . "2") (3 . "3"))

;; Υπάρχουν πολλά είδη από προϋπάρχοντες τρόπους για να συλλέγουμε
;; τιμές από τους βρόχους

(for/sum ([i 10]) (* i i)) ; => 285
(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000
(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t
(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t

;; Και για να χρησιμοποιήσουμε ένα αυθαίρετο συνδυασμό χρησιμοποιούμε
;; το 'for/fold'
(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10

;; Αυτό συχνά μπορεί να αντικαταστήσει τους κοινούς
;; προστακτικούς βρόχους (imperative loops)

;;; Εξαιρέσεις

;; Για να πιάσουμε τις εξαιρέσεις χρησιμοποιούμε το
;; `with-handlers'
(with-handlers ([exn:fail? (lambda (exn) 999)])
  (+ 1 "2")) ; => 999
(with-handlers ([exn:break? (lambda (exn) "no time")])
  (sleep 3)
  "phew") ; => "phew", αλλά αν γίνει το break => "no time"

;; Χρησιμοποιούμε το 'raise' για να άρουμε μια εξαίρεση
;; ή οποιαδήποτε άλλη τιμή
(with-handlers ([number?    ; πιάνουμε αριθμητικές τιμές
                 identity]) ; και τις επιστρέφουμε σαν απλές τιμές
  (+ 1 (raise 2))) ; => 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Αλλαγή τιμών
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Χρησιμοποιούμε το 'set!' για να θέσουμε μια νέα τιμή
;; σε μια ήδη υπάρχουσα μεταβλητή
(define n 5)
(set! n (add1 n))
n ; => 6

;; Χρησιμοποιούμε τα boxes για να δηλώσουμε ρητά ότι μια μεταβλητή
;; θα είναι  mutable (θα μπορεί να αλλάξει η τιμή της)
;; Αυτό είναι παρόμοιο με τους pointers σε άλλες γλώσσες
(define n* (box 5))
(set-box! n* (add1 (unbox n*)))
(unbox n*) ; => 6


;; Πολλοί τύποι μεταβλητών στη Racket είναι αμετάβλητοι π.χ. τα ζεύγη, οι
;; λίστες κτλ. Άλλοι υπάρχουν και σε μεταβλητή και σε αμετάβλητη μορφή
;; π.χ. αλφαριθμητικά, διανύσματα κτλ.
(define vec (vector 2 2 3 4))
(define wall (make-vector 100 'bottle-of-beer))
;; Χρησιμοποιούμε το 'vector-set!' για να ανεώσουμε κάποια
;; συγκεκριμένη θέση
(vector-set! vec 0 1)
(vector-set! wall 99 'down)
vec ; => #(1 2 3 4)


;; Έτσι δημιουργούμε ένα άδειο μεταβλητό πίνακα κατακερματισμού
;; και τον χειριζόμαστε κατάλληλα
(define m3 (make-hash))
(hash-set! m3 'a 1)
(hash-set! m3 'b 2)
(hash-set! m3 'c 3)
(hash-ref m3 'a)     ; => 1
(hash-ref m3 'd 0)   ; => 0
(hash-remove! m3 'a)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Ενότητες (modules)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; Οι ενότητες μας επιτρέπουν να οργανώνουμε τον κώδικα σε πολλαπλά
;; αρχεία και επαναχρησιμοποιούμενες βιβλιοθήκες
;; Εδώ χρησιμοποιούμε υπο-ενότητες, εμφωλευμένες μέσα σε μια
;; άλλη ενότητα που δημιουργεί αυτό το κείμενο (ξεκινώντας από
;; την γραμμή '#lang' )
(module cake racket/base ; ορίζουμε μια ενότητα 'cake' βασισμένο στο
                         ; racket/base

  (provide print-cake) ; συνάρτηση που εξάγεται από την ενότητα

  (define (print-cake n)
    (show "   ~a   " n #\.)
    (show " .-~a-. " n #\|)
    (show " | ~a | " n #\space)
    (show "---~a---" n #\-))

  (define (show fmt n ch) ; εσωτερική συνάρτηση
    (printf fmt (make-string n ch))
    (newline)))

;; Χρησιμοποιουμε το 'require' για να πάρουμε όλα τα
;; παρεχόμενα ονόματα από μία ενότητα
(require 'cake) ; το ' είναι για τοπική υποενότητα
(print-cake 3)
; (show "~a" 1 #\A) ; => error, το `show' δεν έχει εξαχθεί

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. Κλάσεις και αντικείμενα
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Δημιουργούμε μια κλάση fish% (- συνήθως χρησιμοποιούμε
;; το % στο όνομα μιας κλάσης )
(define fish%
  (class object%
    (init size) ; initialization argument
    (super-new) ; superclass initialization
    ;; Field
    (define current-size size)
    ;; Public methods
    (define/public (get-size)
      current-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

;; Δημιουργούμε ένα instance του fish%
(define charlie
  (new fish% [size 10]))

;; Χρησιμοποιούμε το 'send' για να καλέσουμε
;; τις μεθόδους ενός αντικειμένου
(send charlie get-size) ; => 10
(send charlie grow 6)
(send charlie get-size) ; => 16

;; Το `fish%' είναι μία τιμή "πρώτης κλάσης"
;; με το οποίο μπορούμε να κάνουμε προσμείξεις
(define (add-color c%)
  (class c%
    (init color)
    (super-new)
    (define my-color color)
    (define/public (get-color) my-color)))
(define colored-fish% (add-color fish%))
(define charlie2 (new colored-fish% [size 10] [color 'red]))
(send charlie2 get-color)
;; ή χωρίς καθόλου ονόματα :
(send (new (add-color fish%) [size 10] [color 'red]) get-color)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 9. Μακροεντολές
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Οι μακροεντολές μας επιτρέπουν να επεκτείνουμε
;; το συντακτικό μιας γλώσσας.

;; Ας προσθέσουμε έναν βρόχο while
(define-syntax-rule (while condition body ...)
  (let loop ()
    (when condition
      body ...
      (loop))))

(let ([i 0])
  (while (< i  10)
    (displayln i)
    (set! i (add1 i))))

;; Macros are hygienic, you cannot clobber existing variables!
(define-syntax-rule (swap! x y) ; -! is idiomatic for mutation
  (let ([tmp x])
    (set! x y)
    (set! y tmp)))

(define tmp 2)
(define other 3)
(swap! tmp other)
(printf "tmp = ~a; other = ~a\n" tmp other)
;; Η μεταβλητή 'tmp' μετονομάζεται σε 'tmp_1'
;; για να αποφευχθεί η σύγκρουση με τα ονόματα
;; (let ([tmp_1 tmp])
;;   (set! tmp other)
;;   (set! other tmp_1))

;; Αλλά ακόμα υπάρχουν ακόμη μετασχηματισμοί του κώδικα, π.χ.:
(define-syntax-rule (bad-while condition body ...)
  (when condition
    body ...
    (bad-while condition body ...)))
;; αυτή η μακροεντολή είναι χαλασμένη: δημιουργεί ατέρμονα βρόχο
;; και αν προσπαθήσουμε να το χρησιμοποιήσουμε, ο μεταγλωττιστής
;; θα μπει στον ατέρμονα βρόχο.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 10. Συμβόλαια (Contracts)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Τα συμβόλαια βάζουν περιορισμούς σε τιμές που προέρχονται
;; από ενότητες (modules)
(module bank-account racket
  (provide (contract-out
            [deposit (-> positive? any)] ; οι ποσότητες είναι πάντα θετικές
            [balance (-> positive?)]))

  (define amount 0)
  (define (deposit a) (set! amount (+ amount a)))
  (define (balance) amount)
  )

(require 'bank-account)
(deposit 5)

(balance) ; => 5

;; Πελάτες που προσπαθούν να καταθέσουν ένα μη θετικό ποσό παίρνουν
;; το μήνυμα (deposit -5) ; => deposit: contract violation
;;                              expected: positive?
;;                              given: -5
;;                              more details....

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 11. Είσοδος και έξοδος
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Η Racket έχει την έννοια του "port", που είναι παρόμοιο με τα
;; file descriptors σε άλλες γλώσσες.

;; Ανοίγουμε το "/tmp/tmp.txt" και γράφουμε μέσα "Hello World"
;; Αυτό θα προκαλούσε σφάλμα αν το αρχείο υπήρχε ήδη
(define out-port (open-output-file "/tmp/tmp.txt"))
(displayln "Hello World" out-port)
(close-output-port out-port)

;; Προσθέτουμε στο τέλος του "/tmp/tmp.txt"
(define out-port (open-output-file "/tmp/tmp.txt"
                                   #:exists 'append))
(displayln "Hola mundo" out-port)
(close-output-port out-port)

;; Διαβάζουμε από αρχείο ξανά
(define in-port (open-input-file "/tmp/tmp.txt"))
(displayln (read-line in-port))
; => "Hello World"
(displayln (read-line in-port))
; => "Hola mundo"
(close-input-port in-port)

;; Εναλλακτικά, με το call-with-output-file δεν χρειάζεται να κλείσουμε
;; ρητά το αρχείο
(call-with-output-file "/tmp/tmp.txt"
  #:exists 'update ; Rewrite the content
  (λ (out-port)
    (displayln "World Hello!" out-port)))

;; Και το call-with-input-file κάνει το ίδιο πράγμα για την είσοδο
(call-with-input-file "/tmp/tmp.txt"
  (λ (in-port)
    (displayln (read-line in-port))))
```

## Επιπλέον πηγές

Ψάχνεις για περισσότερα ; [Getting Started with Racket](http://docs.racket-lang.org/getting-started/)
---
language: Scala
contributors:
    - ["George Petrov", "http://github.com/petrovg"]
    - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
    - ["Geoff Liu", "http://geoffliu.me"]
translators:
    - ["Vasilis Panagiotopoulos" , "https://github.com/billpcs/"]
filename: learnscala-gr.scala
lang: el-gr
---

Scala - Η επεκτάσιμη γλώσσα

```scala

/*
  Προετοιμαστείτε:

  1) Κατεβάστε την Scala - http://www.scala-lang.org/downloads
  2) Κάνετε εξαγωγή στην επιθυμητή σας τοποθεσία και βάλτε τον υποφάκελο bin
      στο path του συστήματος
  3) Ξεκινήστε ένα scala REPL γράφοντας scala. Θα πρέπει να βλέπετε το prompt:

  scala>

  Αυτό είναι το αποκαλούμενο REPL (Read-Eval-Print Loop) *.
  Μπορείτε να πληκτρολογήσετε οποιαδήποτε έγκυρη έκφραση σε Scala μέσα του ,
  και το αποτέλεσμα θα τυπωθεί. Θα εξηγήσουμε πως μοιάζουν τα αρχεία της Scala
  αργότερα μέσα στο tutorial , αλλά για τώρα ας αρχίσουμε με κάποια βασικά.
  *[Βρόχος του Διάβασε - Αξιολόγησε - Τύπωσε]
*/


/////////////////////////////////////////////////
// 1. Βασικές έννοιες
/////////////////////////////////////////////////

// Τα σχόλια μίας γραμμής ξεκινούν με δύο "/" (:forward slashes) .

/*
  Τα σχόλια που επεκτείνονται σε πολλές γραμμές , όπως μπορείτε
  να δείτε , φαίνονται κάπως έτσι.
*/

// Εκτύπωση με νέα γραμμή στην επόμενη εκτύπωση
println("Hello world!")
println(10)

// Εκτύπωση χωρίς νέα γραμμή στην επόμενη εκτύπωση
print("Hello world")

// Η δήλωση μεταβλητών γίνεται χρησιμοποιώντας var ή val.
// Οι δηλώσεις val είναι αμετάβλητες, ενώ οι var είναι μεταβλητές.
// Η αμεταβλητότητα είναι συμφέρουσα και προσπαθούμε να την χρησιμοποιούμε.
val x = 10 // το x είναι τώρα 10
x = 20 // σφάλμα: αλλαγή σε val
var y = 10
y = 20  // το y είναι τώρα 20

/*
  Η Scala είναι στατικού τύπου γλώσσα, εν τούτοις προσέξτε ότι στις παραπάνω
  δηλώσεις , δεν προσδιορίσαμε κάποιον τύπο. Αυτό συμβαίνει λόγω ενός
  χαρακτηριστικού της Scala που λέγεται συμπερασματολογία τύπων. Στις 
  περισσότερες των περιπτώσεων, ο μεταγλωττιστής της Scala μπορεί να
  μαντέψει ποιος είναι ο τύπος μιας μεταβλητής. Μπορούμε να δηλώσουμε
  αναλυτικά τον τύπο μιας μεταβλητής ως εξής:
*/
val z: Int = 10
val a: Double = 1.0

/*
  Προσέξτε ότι υπάρχει αυτόματη μετατροπή από ακέραιο (Int) σε διπλής 
  ακρίβειας (Double), και συνεπώς το αποτέλεσμα είναι 10.0 και όχι 10. 
*/ 
val b: Double = 10

// Λογικές τιμές
true
false

// Λογικές Πράξεις
!true // false
!false // true
true == false // false
10 > 5 // true

// Η αριθμητική είναι όπως τα συνηθισμένα
1 + 1 // 2
2 - 1 // 1
5 * 3 // 15
6 / 2 // 3
6 / 4 // 1
6.0 / 4 // 1.5


/*
  Αξιολογώντας μια έκφραση στο REPL, σας δίνεται ο τύπος και 
  η τιμή του αποτελέσματος 
*/

1 + 7

/* Η παραπάνω γραμμή έχει το εξής αποτέλεσμα:

  scala> 1 + 7
  res29: Int = 8

  Αυτό σημαίνει ότι το αποτέλεσμα της αξιολόγησης του 1 + 7 είναι ένα αντικείμενο
  τύπου Int με τιμή 8

  Σημειώστε ότι το "res29" είναι ένα σειριακά δημιουργούμενο όνομα μεταβλητής
  για να αποθηκεύονται τα αποτελέσματα των εκφράσεων που έχετε πληκτρολογήσει 
  και συνεπώς η έξοδός σας μπορεί να διαφέρει.
*/

"Τα αλφαριθμητικά στην Scala περικλείονται από διπλά εισαγωγικά"
'a' // Ένας χαρακτήρας στην Scala
// res30: Char = a
// Αλφαριθημτικά με μονά εισαγωγικά δεν υφίστανται <= Αυτό θα προκαλέσει σφάλμα.

// Τα αλφαριθμητικά έχουν τις συνηθισμένες μεθόδους της Java ορισμένες πάνω τους.
"hello world".length
"hello world".substring(2, 6)
"hello world".replace("C", "3")

// Έχουν επίσης μερικές επιπλέον μεθόδους Scala. 
// Δείτε επίσης : scala.collection.immutable.StringOps
"hello world".take(5)
"hello world".drop(5)

// Παρεμβολή αλφαριθμητικών : παρατηρήστε το πρόθεμα "s"
val n = 45
s"We have $n apples" // => "We have 45 apples"

// Παρατηρήστε την χρήση των '{', '}'
val a = Array(11, 9, 6)
s"My second daughter is ${a(0) - a(2)} years old." // => "My second daughter is 5 years old."
s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples."
s"Power of 2: ${math.pow(2, 2)}" // => "Power of 2: 4"

// Μορφοποίηση με παρεμβεβλημένα αλφαριθμητικά με το πρόθεμα "f"
f"Power of 5: ${math.pow(5, 2)}%1.0f" // "Power of 5: 25"
f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122: 11.0454"

// Raw αλφαριθμητικά, που αγνοούν τους ειδικούς χαρακτήρες.
raw"New line feed: \n. Carriage return: \r." // => "New line feed: \n. Carriage return: \r."

// Μερικούς χαρακτήρες πρέπει να τους κάνουμε "escape",
// λ.χ ένα διπλό εισαγωγικό μέσα σε ένα αλφαριθμητικό :
"They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown""

/* 
  Τα τριπλά διπλά-εισαγωγικά επιτρέπουν στα αλφαριθμητικά να εκτείνονται σε
  πολλαπλές γραμμές και να περιέχουν διπλά εισαγωγικά
*/
val html = """<form id="daform">
                <p>Press belo', Joe</p>
                <input type="submit">
              </form>"""


/////////////////////////////////////////////////
// 2. Συναρτήσεις
/////////////////////////////////////////////////

// Οι συναρτήσεις ορίζονται ως εξής:
//
//   def functionName(args...): ReturnType = { body... }
//
// Αν προέρχεστε απο πιο παραδοσιακές γλώσσες (C/C++ , Java) παρατηρήστε
// την παράλειψη του return. Στην Scala , η τελευταία έκφραση στο μπλόκ
// της συνάρτησης είναι η τιμή που επιστρέφει η συνάρτηση.
def sumOfSquares(x: Int, y: Int): Int = {
  val x2 = x * x
  val y2 = y * y
  x2 + y2
}

// Τα { } μπορούν να παραλειφθούν αν η συνάρτηση αποτελείται απο μια απλή έκφραση:
def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y

// Η σύνταξη για την κλήση συναρτήσεων είναι γνώριμη:
sumOfSquares(3, 4)  // => 25

// Στις περισσότερες των περιπτώσεων (με τις αναδρομικές συναρτήσεις να αποτελούν
// την πιο αξιοπρόσεκτη εξαίρεση) , ο τύπος επιστροφής της συνάρτησης μπορεί να
// παραλειφθεί, και η ίδια συμπερασματολογία τύπων που είδαμε με τις μεταβλητές
// θα δουλεύει και με τους τύπους επιστροφής της συνάρτησης:
def sq(x: Int) = x * x  // Ο μεταγλωττιστής μπορεί να μαντέψει ότι
                        // ο τύπος επιστροφής της συνάρτησης είναι Int

// Οι συναρτήσεις μπορούν να έχουν προκαθορισμένες τιμές:
def addWithDefault(x: Int, y: Int = 5) = x + y
addWithDefault(1, 2)  // => 3
addWithDefault(1)  // => 6


// Οι ανώνυμες συναρτήσεις είναι ως εξής:
(x:Int) => x * x

// Σε αντίθεση με τα defs , ακόμα και ο τύπος εισόδου απο τις ανώνυμες
// συναρτήσεις μπορεί να παραληφθεί αν τα συμφραζόμενα το κάνουν ξεκάθαρο.
// Προσέξτε τον τύπο "Int => Int" που σημαίνει ότι μια συνάρτηση παίρνει
// ένα Int και επιστρέφει ένα Int.
val sq: Int => Int = x => x * x

// Οι ανώνυμες συναρτήσεις μπορούν να κληθούν όπως συνήθως:
sq(10)   // => 100

// Αν κάθε όρισμα στην ανώνυμη συνάρτηση χρησιμοποιείται μόνο μία φορά,
// η Scala επιτρέπει έναν ακόμα πιο σύντομο τρόπο να οριστεί. Αυτές
// οι ανώνυμες συναρτήσεις αποδεικνύεται ότι είναι πολύ κοινές ,
// όπως θα γίνει προφανές στο μέρος των δομών δεδομένων.
val addOne: Int => Int = _ + 1
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)

addOne(5)  // => 6
weirdSum(2, 4)  // => 16

// Η δεσμευμένη λέξη return υπάρχει στην Scala , αλλά επιστρέφει μόνο 
// από το πιο εσωτερικό def που την περικλείει.
// ΠΡΟΣΟΧΗ: Η χρήση του return στην Scala είναι επιρρεπής σε λάθη
// και θα πρέπει να αποφεύγεται.
// Δεν έχει καμία επίδραση στις ανώνυμες συναρτήσεις. Για παράδειγμα: 
def foo(x: Int): Int = {
  val anonFunc: Int => Int = { z =>
    if (z > 5)
      return z  // Αυτή η σειρά κάνει το z την τιμή που επιστρέφει η foo!
    else
      z + 2  // Αυτή η γραμμή είναι η τιμή που επιστρέφει η anonFunc
  }
  anonFunc(x)  // Αυτή η γραμμή είναι η τιμή που επιστρέφει η foo
}


/////////////////////////////////////////////////
// 3. Έλεγχος ροής
/////////////////////////////////////////////////

1 to 5
val r = 1 to 5
r.foreach( println )

r foreach println
// ΠΡΟΣΟΧΗ: Η Scala είναι σχετικά επιεικής ως αναφορά τις τελείες και 
// τις παρενθέσεις. Διαβάστε τους κανόνες ξεχωριστά. 
// Αυτό βοηθάει στο να γράφεις DSLs και APIs που διαβάζονται σαν τα Αγγλικά.

(5 to 1 by -1) foreach ( println )

// Ένας βρόχος while :
var i = 0
while (i < 10) {  println("i " + i); i+=1  }

while (i < 10) {  println("i " + i); i+=1  }   // Ναι ξανά! Τι συνέβη; Γιατί;

i    // Εμφάνισε την τιμή του i. Σημειώστε ότι ένας βρόχος while είναι βρόχος
     // με την κλασική έννοια - εκτελείται σειριακά καθώς αλλάζει η μεταβλητή
     // του βρόχου. Το while είναι πολύ γρήγορο , γρηγορότερο απο τους βρόχους
     // της Java , αλλά η χρήση combinators και comprehensions όπως πιο πάνω ,
     // είναι πιο εύκολη στην κατανόηση και στην παραλληλοποίηση.

// Ένας βρόχος do while :
do {
  println("x is still less than 10");
  x += 1
} while (x < 10)

// Η αναδρομή ουράς είναι ένας ιδιωματικός τρόπος να κάνεις επαναλαμβανόμενα
// πράγματα στην Scala. Οι αναδρομικές συναρτήσεις απαιτούν να γραφτεί 
// ρητά ο τύπος που θα επιστρέψουν, αλλιώς ο μεταγλωττιστής δεν μπορεί 
// αλλιώς να τον συνάγει. Παρακάτω είναι μια συνάρτηση που επιστρέφει Unit.
def showNumbersInRange(a:Int, b:Int):Unit = {
  print(a)
  if (a < b)
    showNumbersInRange(a + 1, b)
}
showNumbersInRange(1,14)


// Έλεγχος Ροής

val x = 10

if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println ("yeah") else println("nay")

println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"


/////////////////////////////////////////////////
// 4. Δομές Δεδομένων
/////////////////////////////////////////////////

val a = Array(1, 2, 3, 5, 8, 13)
a(0)
a(3)
a(21)    // "Πετάει" exception

val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
m("fork")
m("spoon")
m("bottle")       // "Πετάει" exception

val safeM = m.withDefaultValue("no lo se")
safeM("bottle")

val s = Set(1, 3, 7)
s(0)
s(1)

/* Δείτε το documentation του map εδώ -
 * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
 */


// Πλειάδες

(1, 2)

(4, 3, 2)

(1, 2, "three")

(a, 2, "three")

// Γιατί να το έχουμε αυτό;
val divideInts = (x:Int, y:Int) => (x / y, x % y)

divideInts(10,3) // Η συνάρτηση divideInts επιστρέφει το αποτέλεσμα 
                 // της ακέραιας διαίρεσης και το υπόλοιπο.

// Για να έχουμε πρόσβαση στα στοιχεία μιας πλειάδας, χρησιμοποιούμε το _._n
// όπου το n είναι ο δείκτης με βάση το 1 του στοιχείου.
val d = divideInts(10,3)

d._1

d._2


/////////////////////////////////////////////////
// 5. Αντικειμενοστραφής Προγραμματισμός
/////////////////////////////////////////////////

/*
  Ότι έχουμε κάνει ως τώρα σε αυτό το tutorial ήταν απλές εκφράσεις
  (τιμές, συναρτήσεις, κτλ.). Αυτές οι εκφράσεις βολεύουν όταν τις
  γράφουμε στο REPL για γρήγορες δοκιμές, αλλά δεν μπορούν να υπάρχουν
  από μόνες τους σε ένα αρχείο Scala. Για παράδειγμα , δεν μπορούμε να
  έχουμε μόνο ένα "val x = 5" στο αρχείο Scala. Αντί αυτού , τα μόνα
  στοιχεία του πάνω επιπέδου που επιτρέπονται στην Scala είναι:

  - αντικείμενα (objects)
  - κλάσεις (classes)
  - κλάσεις περίπτωσης (case classes στην Scala)
  - Χαρακτηριστικά (traits , όπως ονομάζονται στην Scala)

  Και τώρα θα εξηγήσουμε τι είναι αυτά.
*/
// Οι κλάσεις είναι παρόμοιες με τις κλάσεις σε άλλες γλώσσες. Τα ορίσματα του
// "κατασκευαστή" (constructor) δηλώνονται μετά από το όνομα της κλάσης , 
// και η αρχικοποιήση γίνεται μέσα στο σώμα της κλάσης.
class Dog(br: String) {
  // Κώδικας για τον "κατασκευαστή"
  var breed: String = br

  // Ορίζεται μια μέθοδος bark , που επιστρέφει ένα αλφαριθμητικό
  def bark = "Woof, woof!"

  // Οι τιμές και οι μέθοδοι είναι public εκτός αν χρησιμοποιήσουμε κάποια
  // απο τις λέξεις κλειδιά "protected" και "private" . 
  private def sleep(hours: Int) =
    println(s"I'm sleeping for $hours hours")

  // Οι abstract μέθοδοι είναι απλά μέθοδοι χωρίς σώμα. Αν βγάζαμε
  // το σχόλιο απο την επόμενη γραμμή η κλάση Dog θα έπρεπε να 
  // δηλωθεί ως abstract class Dog(...) { ... } :
  // def chaseAfter(what: String): String
}

val mydog = new Dog("greyhound")
println(mydog.breed) // => "greyhound"
println(mydog.bark) // => "Woof, woof!"


// Η λέξη "object" δημιουργεί ένα type ΚΑΙ ένα singleton instance αυτού.
// Είναι κοινό για τις κλάσεις στην Scala να έχουν ένα "συντροφικό object",
// όπου η συμπεριφορά για κάθε instance αιχμαλωτίζεται μέσα στις κλάσεις
// αυτές καθ' αυτές, αλλά η συμπρεριφορά που σχετίζεται με όλα τα instances 
// της κλάσης πάνε μέσα στο object. Η διαφορά είναι παρόμοια με τις
// μεθόδους κλάσεων σε σχέση με στατικές μεθόδους σε άλλες γλώσσες.
// Προσέξτε ότι τα objects και οι κλάσεις μπορούν να έχουν το ίδιο όνομα.
object Dog {
  def allKnownBreeds = List("pitbull", "shepherd", "retriever")
  def createDog(breed: String) = new Dog(breed)
}

// Οι κλάσεις περίπτωσης (case classes) είναι που έχουν την επιπλέον 
// λειτουργικότητα ενσωματωμένη. Μιά συνήθης ερώτηση για αρχάριους στην
// Scala είναι πότε να χρησιμοποιούνται κλάσεις και πότε case κλάσεις.
// Γενικά οι κλάσεις τείνουν να εστιάζουν στην ενθυλάκωση, τον
// πολυμορφισμό και τη συμπεριφορά. Οι τιμές μέσα σε αυτές τις κλάσεις 
// τείνουν να είναι private , και μόνο οι μέθοδοι είναι εκτεθειμένες.
// Ο κύριος σκοπός των case classes είναι να κρατούν δεδομένα που είναι
// σταθερές(immutable). Συνήθως έχουν λίγες μεθόδους και οι μέθοδοι σπάνια
// έχουν παρενέργειες.
case class Person(name: String, phoneNumber: String)

// Δημιουργία ενός instance. Παρατηρήστε ότι τα case classes 
// δεν χρειάζονται την λέξη "new" .
val george = Person("George", "1234")
val kate = Person("Kate", "4567")

// Με τα case classes, παίρνεις μερικά προνόμια δωρεάν , όπως:
george.phoneNumber  // => "1234"

// Ελέγχεται η ισότητα για κάθε πεδίο (δεν χρειάζεται να
// κάνουμε override στο .equals)
Person("George", "1234") == Person("Kate", "1236")  // => false

// Έυκολος τρόπος να κάνουμε αντιγραφή. Δημιουργούμε έναν νέο geroge:
// otherGeorge == Person("george", "9876")
val otherGeorge = george.copy(phoneNumber = "9876")

// Και πολλά άλλα. Τα case classes έχουν και αντιστοίχιση προτύπων 
// (pattern matching) δωρεάν, δείτε παρακάτω.

// Τα χαρακτηριστικά (traits) έρχονται σε λίγο καιρό !

/////////////////////////////////////////////////
// 6. Αντιστοίχιση Προτύπων 
/////////////////////////////////////////////////

// Η αντιστοίχιση προτύπων (pattern matching) είναι ένα πολύ δυνατό και
// ευρέως χρησιμοποιούμενο χαρακτηριστικό στην Scala. Παρακάτω βλέπουμε
// πως γίνεται το pattern matching σε ένα case class. Σημείωση: Σε 
// αντίθεση με άλλες γλώσσες η Scala δεν χρειάζεται breaks, γιατί γίνεται 
// αυτόματα όταν γίνει κάποιο match.

def matchPerson(person: Person): String = person match {
  // Μετά προσδιορίζουμε το πρότυπο (pattern):
  case Person("George", number) => "We found George! His number is " + number
  case Person("Kate", number) => "We found Kate! Her number is " + number
  case Person(name, number) => "We matched someone : " + name + ", phone : " + number
}

val email = "(.*)@(.*)".r  // Ορίζουμε ένα regex για το επόμενο παράδειγμα.
                           // (regex <- REGular EXpression)  

// Το pattern matching μπορεί να μοιάζει γνώριμο απο τα switch statements σε
// γλώσσες που ανήκουν στην οικογένεια της C αλλά είναι πολύ πιο ισχυρό.
// Στην Scala , μπορούμε να κάνουμε match πολύ περισσότερα:
def matchEverything(obj: Any): String = obj match {
  // Μπορούμε να ταιριάξουμε τιμές:
  case "Hello world" => "Got the string Hello world"

  // Μπορούμε να ταιριάξουμε τύπους:
  case x: Double => "Got a Double: " + x

  // Μπορούμε να βάλουμε συνθήκες:
  case x: Int if x > 10000 => "Got a pretty big number!"

  // Μπορούμε να ταιριάξουμε case classes όπως πρίν:
  case Person(name, number) => s"Got contact info for $name!"

  // Μπορούμε να ταιριάξουμε regex:
  case email(name, domain) => s"Got email address $name@$domain"

  // Μπορούμε να ταιριάξουμε πλειάδες:
  case (a: Int, b: Double, c: String) => s"Got a tuple: $a, $b, $c"

  // Μπορούμε να ταιριάξουμε δομές δεδομένων:
  case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c"

  // Μπορούμε να ταιριάξουμε πρότυπα που το ένα είναι μέσα στο άλλο:
  case List(List((1, 2,"YAY"))) => "Got a list of list of tuple"
}

// Στην πραγματικότητα , μπορούμε να κάνουμε pattern matching σε όποιο αντικείμενο
// έχει την μέθοδο "unapply". Αυτό το χαρακτηριστικό είναι τόσο ισχυρό ώστε
// η Scala επιτρέπει να ορίστούν ολόκληρες συναρτήσεις σαν patterns.
val patternFunc: Person => String = {
  case Person("George", number) => s"George's number: $number"
  case Person(name, number) => s"Random person's number: $number"
}


/////////////////////////////////////////////////
// 7. Συναρτησιακός Προγραμματισμός
/////////////////////////////////////////////////

// Η Scala επιτρέπει στις μεθόδους και τις συναρτήσεις να επιστρέφουν ή να
// δέχονται ως παραμέτρους άλλες μεθόδους ή συναρτήσεις.

val add10: Int => Int = _ + 10 // Μια συνάρτηση που δέχεται Int και επιστρέφει Int
List(1, 2, 3) map add10 // List(11, 12, 13) - το add10 εφαρμόζεται σε κάθε στοιχείο
                        // μέσω του map

// Οι ανώνυμες συναρτήσεις μπορούν να χρησιμοποιηθούν αντί 
// ονοματισμένων (όπως απο πάνω) :
List(1, 2, 3) map (x => x + 10)

// Και το σύμβολο της κάτω παύλας , μπορεί να χρησιμοποιηθεί αν υπάρχει μόνο
// ένα όρισμα στην ανώνυμη συνάρτηση. Έτσι δεσμεύεται ως η μεταβλητή.
List(1, 2, 3) map (_ + 10)

// Αν το μπλοκ της ανώνυμης  συνάρτησης ΚΑΙ η συνάρτηση που εφαρμόζεται
// (στην περίπτωσή μας το foreach και το println) παίρνουν ένα όρισμα
// μπορείτε να παραλείψετε την κάτω παύλα.
List("Dom", "Bob", "Natalia") foreach println


// Συνδυαστές

s.map(sq)

val sSquared = s. map(sq)

sSquared.filter(_ < 10)

sSquared.reduce (_+_)

// Η συνάρτηση filter παίρνει ένα κατηγορούμενο (predicate)
// που είναι μια συνάρτηση απο το A -> Boolean και διαλέγει 
// όλα τα στοιχεία που ικανοποιούν αυτό το κατηγορούμενο.
List(1, 2, 3) filter (_ > 2) // List(3)
case class Person(name:String, age:Int)
List(
  Person(name = "Dom", age = 23),
  Person(name = "Bob", age = 30)
).filter(_.age > 25) // List(Person("Bob", 30))


// Το foreach είναι μια μέθοδος της Scala , που ορίζεται για ορισμένες
// συλλογές (collections). Παίρνει έναν τύπο και επιστρέφει Unit
// (μια μέθοδο void)
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println

// For comprehensions

for { n <- s } yield sq(n)

val nSquared2 = for { n <- s } yield sq(n)

for { n <- nSquared2 if n < 10 } yield n

for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared

/*
  Προσοχή : Αυτά δεν ήταν βρόχοι for. Η σημασιολογία ενός βρόχου for είναι
  η επανάληψη, ενώ ένα for-comprehension ορίζει μια σχέση μεταξύ δύο
  συνόλων δεδομένων.
*/

/////////////////////////////////////////////////
// 8. Implicits
/////////////////////////////////////////////////
/*
  ΠΡΟΣΟΧΗ! Τα implicits είναι ένα σύνολο απο ισχυρά χαρακτηριστικά της Scala
  και επομένως είναι εύκολο να γίνει κατάχρηση. Οι αρχάριοι στην Scala θα 
  πρέπει να αντισταθούν στον πειρασμό να τα χρησιμοποιήσουν έως ότου, όχι 
  μόνο καταλάβουν πως λειτουργούν, αλλά ακόμα εξασκηθούν πάνω τους.
  Ο μόνος λόγος που συμπεριλάβαμε αυτό το κομμάτι στο tutorial είναι
  γιατί είναι τόσο κοινό στις βιβλιοθήκες της Scala , που αδύνατο να κάνεις 
  οτιδήποτε σημαντικό χωρίς να χρησιμοποιήσεις μια που να έχει implicits.

*/

// Κάθε τιμή (vals , συναρτήσεις , αντικείμενα , κτλ) μπορεί να δηλωθεί ως
// implicit χρησιμοποιώντας , ναι το μαντέψατε , την λέξη "implicit".
// Σημειώστε ότι χρησιμοποιούμε την κλάση Dog που δημιουργήσαμε στο
// 5ο μέρος των παραδειγμάτων.
implicit val myImplicitInt = 100
implicit def myImplicitFunction(breed: String) = new Dog("Golden " + breed)


// Απο μόνη της, η λέξη implicit, δεν αλλάζει την συμπεριφορά μιάς τιμής
// οπότε οι παραπάνω μπορούν να χρησιμοποιοηθούν όπως συνήθως.
myImplicitInt + 2  // => 102
myImplicitFunction("Pitbull").breed  // => "Golden Pitbull"

// Η διαφορά είναι ότι τώρα αυτές οι τιμές έχουν την δυνατότητα να 
// χρησιμοποιηθούν όταν ένα άλλο κομμάτι κώδικα "χρειάζεται" μια 
// implicit τιμή. Μια τέτοια περίπτωση είναι τα ορίσματα μιας implicit 
// συνάρτησης:
def sendGreetings(toWhom: String)(implicit howMany: Int) =
  s"Hello $toWhom, $howMany blessings to you and yours!"

// Άν τροφοδοτήσουμε μια τιμή για το "homMany", η συνάρτηση συμπεριφέρεται
// ως συνήθως 
sendGreetings("John")(1000)  // => "Hello John, 1000 blessings to you and yours!"

// Αλλά αν παραλείψουμε την παράμετρο implicit , μια implicit τιμή του ιδίου τύπου
// χρησιμοποιείται, στην περίπτωσή μας, το "myImplicitInt"
sendGreetings("Jane")  // => "Hello Jane, 100 blessings to you and yours!"

// Οι παράμετροι implicit συναρτήσεων μας επιτρέπουν να προσομοιάζουμε
// κλάσεις τύπων (type classes) σε άλλες συναρτησιακές γλώσσες.
// Χρησιμοποιείται τόσο συχνά που έχει την δικιά του συντομογραφία.
// Οι επόμενες δύο γραμμές κώδικα σημαίνουν το ίδιο πράγμα.
def foo[T](implicit c: C[T]) = ...
def foo[T : C] = ...



// Μια άλλη περίπτωση στην οποία ο μεταγλωττιστής αναζητά μια implicit τιμή 
// είναι αν έχετε obj.method (...)
// αλλά το "obj" δεν έχει την "method" ως μέθοδο. Σε αυτή την περίπτωση, 
// αν υπάρχει μια implicit μετατροπή του τύπου Α => Β, όπου Α είναι ο τύπος 
// του obj, ενώ το Β έχει μία μέθοδο που ονομάζεται «method», εφαρμόζεται η 
// εν λόγω μετατροπή. Έτσι, έχοντας την MyImplicitFunction μέσα στο πεδίο 
// εφαρμογής(scope), μπορούμε να πούμε:
"Retriever".breed  // => "Golden Retriever"
"Sheperd".bark  // => "Woof, woof!"

// Εδώ το String αρχικά μετατρέπεται σε Dog χρησιμοποιώντας την συνάρτησή μας
// παραπάνω, και μετά καλείται η κατάλληλη μέθοδος. Αυτό είναι ένα εξερετικά
// ισχυρό χαρακτηριστικό, αλλά δεν πρέπει να χρησιμοποιείται με ελαφριά την 
// καρδιά. Μάλιστα, όταν ορίσατε την συνάρτηση implicit παραπάνω, ο μεταγλωττιστής
// θα πρέπει να σας έδωσε μια προειδοποιήση, ότι δεν πρέπει να το κάνετε αυτό 
// εκτός αν πραγματικά γνωρίζετε τι κάνετε.


/////////////////////////////////////////////////
// 9. Διάφορα
/////////////////////////////////////////////////

// Εισαγωγή βιβλιοθηκών κτλ
import scala.collection.immutable.List

// Εισαγωγή των πάντων απο το scala.collection.immutable
import scala.collection.immutable._

// Εισαγωγή πολλών κλάσεων σε μία έκφραση
import scala.collection.immutable.{List, Map}

// Δώστε ένα νέο όνομα στην εισαγωγή σας χρησιμοποιώντας το '=>'
import scala.collection.immutable.{ List => ImmutableList }

// Εισαγωγή όλων των κλάσεων εκτός απο μερικές.
// Το επόμενο δεν εισάγει το Map και το Set:
import scala.collection.immutable.{Map => _, Set => _, _}

// Το σημείο εισαγωγής του προγράμματος σας ορίζεται σε ένα αρχείο scala ,
// χρησιμοποιώντας ένα αντικείμενο (object), με μία μέθοδο , την main.
object Application {
  def main(args: Array[String]): Unit = {
    // Εδω γράφουμε ...
  }
}

// Files can contain multiple classes and objects. Compile with scalac
// Τα files μπορούν να περιέχουν περισσότερες απο μία κλάσεις και 
// αντικείμενα. Το compile γίνεται με την εντολή scalac

// Εισαγωγή και εξαγωγή.

// Για να διβάσετε ένα αρχείο γραμμή προς γραμμή
import scala.io.Source
for(line <- Source.fromFile("myfile.txt").getLines())
  println(line)

// Για να γράψετε σε ένα αρχείο 
val writer = new PrintWriter("myfile.txt")
writer.write("Writing line for line" + util.Properties.lineSeparator)
writer.write("Another line here" + util.Properties.lineSeparator)
writer.close()

```

## Further resources

[Scala for the impatient](http://horstmann.com/scala/)

[Twitter Scala school](http://twitter.github.io/scala_school/)

[The scala documentation](http://docs.scala-lang.org/)

[Try Scala in your browser](http://scalatutorials.com/tour/)

Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user)

---
language: elisp
contributors:
    - ["Bastien Guerry", "https://bzg.fr"]
    - ["Saurabh Sandav", "http://github.com/SaurabhSandav"]
filename: learn-emacs-lisp.el
---

```scheme
;; This gives an introduction to Emacs Lisp in 15 minutes (v0.2d)
;;
;; First make sure you read this text by Peter Norvig:
;; http://norvig.com/21-days.html
;;
;; Then install GNU Emacs 24.3:
;;
;; Debian: apt-get install emacs (or see your distro instructions)
;; OSX: http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg
;; Windows: http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip
;;
;; More general information can be found at:
;; http://www.gnu.org/software/emacs/#Obtaining

;; Important warning:
;;
;; Going through this tutorial won't damage your computer unless
;; you get so angry that you throw it on the floor.  In that case,
;; I hereby decline any responsibility.  Have fun!

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Fire up Emacs.
;;
;; Hit the `q' key to dismiss the welcome message.
;;
;; Now look at the gray line at the bottom of the window:
;;
;; "*scratch*" is the name of the editing space you are now in.
;; This editing space is called a "buffer".
;;
;; The scratch buffer is the default buffer when opening Emacs.
;; You are never editing files: you are editing buffers that you
;; can save to a file.
;;
;; "Lisp interaction" refers to a set of commands available here.
;;
;; Emacs has a built-in set of commands available in every buffer,
;; and several subsets of commands available when you activate a
;; specific mode.  Here we use the `lisp-interaction-mode', which
;; comes with commands to evaluate and navigate within Elisp code.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Semi-colons start comments anywhere on a line.
;;
;; Elisp programs are made of symbolic expressions ("sexps"):
(+ 2 2)

;; This symbolic expression reads as "Add 2 to 2".

;; Sexps are enclosed into parentheses, possibly nested:
(+ 2 (+ 1 1))

;; A symbolic expression contains atoms or other symbolic
;; expressions.  In the above examples, 1 and 2 are atoms,
;; (+ 2 (+ 1 1)) and (+ 1 1) are symbolic expressions.

;; From `lisp-interaction-mode' you can evaluate sexps.
;; Put the cursor right after the closing parenthesis then
;; hold down the control and hit the j keys ("C-j" for short).

(+ 3 (+ 1 2))
;;           ^ cursor here
;; `C-j' => 6

;; `C-j' inserts the result of the evaluation in the buffer.

;; `C-xC-e' displays the same result in Emacs bottom line,
;; called the "minibuffer".  We will generally use `C-xC-e',
;; as we don't want to clutter the buffer with useless text.

;; `setq' stores a value into a variable:
(setq my-name "Bastien")
;; `C-xC-e' => "Bastien" (displayed in the mini-buffer)

;; `insert' will insert "Hello!" where the cursor is:
(insert "Hello!")
;; `C-xC-e' => "Hello!"

;; We used `insert' with only one argument "Hello!", but
;; we can pass more arguments -- here we use two:

(insert "Hello" " world!")
;; `C-xC-e' => "Hello world!"

;; You can use variables instead of strings:
(insert "Hello, I am " my-name)
;; `C-xC-e' => "Hello, I am Bastien"

;; You can combine sexps into functions:
(defun hello () (insert "Hello, I am " my-name))
;; `C-xC-e' => hello

;; You can evaluate functions:
(hello)
;; `C-xC-e' => Hello, I am Bastien

;; The empty parentheses in the function's definition means that
;; it does not accept arguments.  But always using `my-name' is
;; boring, let's tell the function to accept one argument (here
;; the argument is called "name"):

(defun hello (name) (insert "Hello " name))
;; `C-xC-e' => hello

;; Now let's call the function with the string "you" as the value
;; for its unique argument:
(hello "you")
;; `C-xC-e' => "Hello you"

;; Yeah!

;; Take a breath.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Now switch to a new buffer named "*test*" in another window:

(switch-to-buffer-other-window "*test*")
;; `C-xC-e'
;; => [screen has two windows and cursor is in the *test* buffer]

;; Mouse over the top window and left-click to go back.  Or you can
;; use `C-xo' (i.e. hold down control-x and hit o) to go to the other
;; window interactively.

;; You can combine several sexps with `progn':
(progn
  (switch-to-buffer-other-window "*test*")
  (hello "you"))
;; `C-xC-e'
;; => [The screen has two windows and cursor is in the *test* buffer]

;; Now if you don't mind, I'll stop asking you to hit `C-xC-e': do it
;; for every sexp that follows.

;; Always go back to the *scratch* buffer with the mouse or `C-xo'.

;; It's often useful to erase the buffer:
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "there"))

;; Or to go back to the other window:
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "you")
  (other-window 1))

;; You can bind a value to a local variable with `let':
(let ((local-name "you"))
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello local-name)
  (other-window 1))

;; No need to use `progn' in that case, since `let' also combines
;; several sexps.

;; Let's format a string:
(format "Hello %s!\n" "visitor")

;; %s is a place-holder for a string, replaced by "visitor".
;; \n is the newline character.

;; Let's refine our function by using format:
(defun hello (name)
  (insert (format "Hello %s!\n" name)))

(hello "you")

;; Let's create another function which uses `let':
(defun greeting (name)
  (let ((your-name "Bastien"))
    (insert (format "Hello %s!\n\nI am %s."
                    name       ; the argument of the function
                    your-name  ; the let-bound variable "Bastien"
                    ))))

;; And evaluate it:
(greeting "you")

;; Some functions are interactive:
(read-from-minibuffer "Enter your name: ")

;; Evaluating this function returns what you entered at the prompt.

;; Let's make our `greeting' function prompt for your name:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (insert (format "Hello!\n\nI am %s and you are %s."
                    from-name ; the argument of the function
                    your-name ; the let-bound var, entered at prompt
                    ))))

(greeting "Bastien")

;; Let's complete it by displaying the results in the other window:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (insert (format "Hello %s!\n\nI am %s." your-name from-name))
    (other-window 1)))

;; Now test it:
(greeting "Bastien")

;; Take a breath.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Let's store a list of names:
;; If you want to create a literal list of data, use ' to stop it from
;; being evaluated - literally, "quote" the data.
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))

;; Get the first element of this list with `car':
(car list-of-names)

;; Get a list of all but the first element with `cdr':
(cdr list-of-names)

;; Add an element to the beginning of a list with `push':
(push "Stephanie" list-of-names)

;; NOTE: `car' and `cdr' don't modify the list, but `push' does.
;; This is an important difference: some functions don't have any
;; side-effects (like `car') while others have (like `push').

;; Let's call `hello' for each element in `list-of-names':
(mapcar 'hello list-of-names)

;; Refine `greeting' to say hello to everyone in `list-of-names':
(defun greeting ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (mapcar 'hello list-of-names)
    (other-window 1))

(greeting)

;; Remember the `hello' function we defined above?  It takes one
;; argument, a name.  `mapcar' calls `hello', successively using each
;; element of `list-of-names' as the argument for `hello'.

;; Now let's arrange a bit what we have in the displayed buffer:

(defun replace-hello-by-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (search-forward "Hello")
      (replace-match "Bonjour"))
    (other-window 1))

;; (goto-char (point-min)) goes to the beginning of the buffer.
;; (search-forward "Hello") searches for the string "Hello".
;; (while x y) evaluates the y sexp(s) while x returns something.
;; If x returns `nil' (nothing), we exit the while loop.

(replace-hello-by-bonjour)

;; You should see all occurrences of "Hello" in the *test* buffer
;; replaced by "Bonjour".

;; You should also get an error: "Search failed: Hello".
;;
;; To avoid this error, you need to tell `search-forward' whether it
;; should stop searching at some point in the buffer, and whether it
;; should silently fail when nothing is found:

;; (search-forward "Hello" nil 't) does the trick:

;; The `nil' argument says: the search is not bound to a position.
;; The `'t' argument says: silently fail when nothing is found.

;; We use this sexp in the function below, which doesn't throw an error:

(defun hello-to-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    ;; Say hello to names in `list-of-names'
    (mapcar 'hello list-of-names)
    (goto-char (point-min))
    ;; Replace "Hello" by "Bonjour"
    (while (search-forward "Hello" nil 't)
      (replace-match "Bonjour"))
    (other-window 1))

(hello-to-bonjour)

;; Let's boldify the names:

(defun boldify-names ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (re-search-forward "Bonjour \\(.+\\)!" nil 't)
      (add-text-properties (match-beginning 1)
                           (match-end 1)
                           (list 'face 'bold)))
    (other-window 1))

;; This functions introduces `re-search-forward': instead of
;; searching for the string "Bonjour", you search for a pattern,
;; using a "regular expression" (abbreviated in the prefix "re-").

;; The regular expression is "Bonjour \\(.+\\)!" and it reads:
;; the string "Bonjour ", and
;; a group of            | this is the \\( ... \\) construct
;;   any character       | this is the .
;;   possibly repeated   | this is the +
;; and the "!" string.

;; Ready?  Test it!

(boldify-names)

;; `add-text-properties' adds... text properties, like a face.

;; OK, we are done.  Happy hacking!

;; If you want to know more about a variable or a function:
;;
;; C-h v a-variable RET
;; C-h f a-function RET
;;
;; To read the Emacs Lisp manual with Emacs:
;;
;; C-h i m elisp RET
;;
;; To read an online introduction to Emacs Lisp:
;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html
```
---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
    - ["Dzianis Dashkevich", "https://github.com/dskecse"]
    - ["Ryan Plant", "https://github.com/ryanplant-au"]
    - ["Ev Bogdanov", "https://github.com/evbogdanov"]
filename: learnelixir.ex
---

Elixir is a modern functional language built on top of the Erlang VM.
It's fully compatible with Erlang, but features a more standard syntax
and many more features.

```elixir

# Single line comments start with a number symbol.

# There's no multi-line comment,
# but you can stack multiple comments.

# To use the elixir shell use the `iex` command.
# Compile your modules with the `elixirc` command.

# Both should be in your path if you installed elixir correctly.

## ---------------------------
## -- Basic types
## ---------------------------

# There are numbers
3    # integer
0x1F # integer
3.0  # float

# Atoms, that are literals, a constant with name. They start with `:`.
:hello # atom

# Tuples that are stored contiguously in memory.
{1,2,3} # tuple

# We can access a tuple element with the `elem` function:
elem({1, 2, 3}, 0) #=> 1

# Lists that are implemented as linked lists.
[1,2,3] # list

# We can access the head and tail of a list as follows:
[head | tail] = [1,2,3]
head #=> 1
tail #=> [2,3]

# In elixir, just like in Erlang, the `=` denotes pattern matching and
# not an assignment.
#
# This means that the left-hand side (pattern) is matched against a
# right-hand side.
#
# This is how the above example of accessing the head and tail of a list works.

# A pattern match will error when the sides don't match, in this example
# the tuples have different sizes.
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# There are also binaries
<<1,2,3>> # binary

# Strings and char lists
"hello" # string
'hello' # char list

# Multi-line strings
"""
I'm a multi-line
string.
"""
#=> "I'm a multi-line\nstring.\n"

# Strings are all encoded in UTF-8:
"héllò" #=> "héllò"

# Strings are really just binaries, and char lists are just lists.
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# `?a` in elixir returns the ASCII integer for the letter `a`
?a #=> 97

# To concatenate lists use `++`, for binaries use `<>`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'hello ' ++ 'world'  #=> 'hello world'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"hello " <> "world"  #=> "hello world"

# Ranges are represented as `start..end` (both inclusive)
1..10 #=> 1..10
lower..upper = 1..10 # Can use pattern matching on ranges as well
[lower, upper] #=> [1, 10]

# Maps are key-value pairs
genders = %{"david" => "male", "gillian" => "female"}
genders["david"] #=> "male"

# Maps with atom keys can be used like this
genders = %{david: "male", gillian: "female"}
genders.gillian #=> "female"

## ---------------------------
## -- Operators
## ---------------------------

# Some math
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# In elixir the operator `/` always returns a float.

# To do integer division use `div`
div(10, 2) #=> 5

# To get the division remainder use `rem`
rem(10, 3) #=> 1

# There are also boolean operators: `or`, `and` and `not`.
# These operators expect a boolean as their first argument.
true and true #=> true
false or true #=> true
# 1 and true
#=> ** (BadBooleanError) expected a boolean on left-side of "and", got: 1

# Elixir also provides `||`, `&&` and `!` which accept arguments of any type.
# All values except `false` and `nil` will evaluate to true.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil
!true #=> false

# For comparisons we have: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` and `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# `===` and `!==` are more strict when comparing integers and floats:
1 == 1.0  #=> true
1 === 1.0 #=> false

# We can also compare two different data types:
1 < :hello #=> true

# The overall sorting order is defined below:
# number < atom < reference < functions < port < pid < tuple < list < bit string

# To quote Joe Armstrong on this: "The actual order is not important,
# but that a total ordering is well defined is important."

## ---------------------------
## -- Control Flow
## ---------------------------

# `if` expression
if false do
  "This will never be seen"
else
  "This will"
end

# There's also `unless`
unless true do
  "This will never be seen"
else
  "This will"
end

# Remember pattern matching? Many control-flow structures in elixir rely on it.

# `case` allows us to compare a value against many patterns:
case {:one, :two} do
  {:four, :five} ->
    "This won't match"
  {:one, x} ->
    "This will match and bind `x` to `:two` in this clause"
  _ ->
    "This will match any value"
end

# It's common to bind the value to `_` if we don't need it.
# For example, if only the head of a list matters to us:
[head | _] = [1,2,3]
head #=> 1

# For better readability we can do the following:
[head | _tail] = [:a, :b, :c]
head #=> :a

# `cond` lets us check for many conditions at the same time.
# Use `cond` instead of nesting many `if` expressions.
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  1 + 2 == 3 ->
    "But I will"
end

# It is common to set the last condition equal to `true`, which will always match.
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  true ->
    "But I will (this is essentially an else)"
end

# `try/catch` is used to catch values that are thrown, it also supports an
# `after` clause that is invoked whether or not a value is caught.
try do
  throw(:hello)
catch
  message -> "Got #{message}."
after
  IO.puts("I'm the after clause.")
end
#=> I'm the after clause
# "Got :hello"

## ---------------------------
## -- Modules and Functions
## ---------------------------

# Anonymous functions (notice the dot)
square = fn(x) -> x * x end
square.(5) #=> 25

# They also accept many clauses and guards.
# Guards let you fine tune pattern matching,
# they are indicated by the `when` keyword:
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir also provides many built-in functions.
# These are available in the current scope.
is_number(10)    #=> true
is_list("hello") #=> false
elem({1,2,3}, 0) #=> 1

# You can group several functions into a module. Inside a module use `def`
# to define your functions.
defmodule Math do
  def sum(a, b) do
    a + b
  end

  def square(x) do
    x * x
  end
end

Math.sum(1, 2)  #=> 3
Math.square(3) #=> 9

# To compile our simple Math module save it as `math.ex` and use `elixirc`
# in your terminal: elixirc math.ex

# Inside a module we can define functions with `def` and private functions with `defp`.
# A function defined with `def` is available to be invoked from other modules,
# a private function can only be invoked locally.
defmodule PrivateMath do
  def sum(a, b) do
    do_sum(a, b)
  end

  defp do_sum(a, b) do
    a + b
  end
end

PrivateMath.sum(1, 2)    #=> 3
# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)

# Function declarations also support guards and multiple clauses:
defmodule Geometry do
  def area({:rectangle, w, h}) do
    w * h
  end

  def area({:circle, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometry.area({:rectangle, 2, 3}) #=> 6
Geometry.area({:circle, 3})       #=> 28.25999999999999801048
# Geometry.area({:circle, "not_a_number"})
#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1

# Due to immutability, recursion is a big part of elixir
defmodule Recursion do
  def sum_list([head | tail], acc) do
    sum_list(tail, acc + head)
  end

  def sum_list([], acc) do
    acc
  end
end

Recursion.sum_list([1,2,3], 0) #=> 6

# Elixir modules support attributes, there are built-in attributes and you
# may also add custom ones.
defmodule MyMod do
  @moduledoc """
  This is a built-in attribute on a example module.
  """

  @my_data 100 # This is a custom attribute.
  IO.inspect(@my_data) #=> 100
end

# The pipe operator |> allows you to pass the output of an expression
# as the first parameter into a function. 

Range.new(1,10)
|> Enum.map(fn x -> x * x end)
|> Enum.filter(fn x -> rem(x, 2) == 0 end)
#=> [4, 16, 36, 64, 100]

## ---------------------------
## -- Structs and Exceptions
## ---------------------------

# Structs are extensions on top of maps that bring default values,
# compile-time guarantees and polymorphism into Elixir.
defmodule Person do
  defstruct name: nil, age: 0, height: 0
end

joe_info = %Person{ name: "Joe", age: 30, height: 180 }
#=> %Person{age: 30, height: 180, name: "Joe"}

# Access the value of name
joe_info.name #=> "Joe"

# Update the value of age
older_joe_info = %{ joe_info | age: 31 }
#=> %Person{age: 31, height: 180, name: "Joe"}

# The `try` block with the `rescue` keyword is used to handle exceptions
try do
  raise "some error"
rescue
  RuntimeError -> "rescued a runtime error"
  _error -> "this will rescue any error"
end
#=> "rescued a runtime error"

# All exceptions have a message
try do
  raise "some error"
rescue
  x in [RuntimeError] ->
    x.message
end
#=> "some error"

## ---------------------------
## -- Concurrency
## ---------------------------

# Elixir relies on the actor model for concurrency. All we need to write
# concurrent programs in elixir are three primitives: spawning processes,
# sending messages and receiving messages.

# To start a new process we use the `spawn` function, which takes a function
# as argument.
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>

# `spawn` returns a pid (process identifier), you can use this pid to send
# messages to the process. To do message passing we use the `send` operator.
# For all of this to be useful we need to be able to receive messages. This is
# achieved with the `receive` mechanism:

# The `receive do` block is used to listen for messages and process
# them when they are received. A `receive do` block will only
# process one received message. In order to process multiple
# messages, a function with a `receive do` block must recursively
# call itself to get into the `receive do` block again.

defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Area = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        area_loop()
    end
  end
end

# Compile the module and create a process that evaluates `area_loop` in the shell
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>
# Alternatively
pid = spawn(Geometry, :area_loop, [])

# Send a message to `pid` that will match a pattern in the receive statement
send pid, {:rectangle, 2, 3}
#=> Area = 6
#   {:rectangle,2,3}

send pid, {:circle, 2}
#=> Area = 12.56000000000000049738
#   {:circle,2}

# The shell is also a process, you can use `self` to get the current pid
self() #=> #PID<0.27.0>

## ---------------------------
## -- Agents
## ---------------------------

# An agent is a process that keeps track of some changing value

# Create an agent with `Agent.start_link`, passing in a function
# The initial state of the agent will be whatever that function returns
{ok, my_agent} = Agent.start_link(fn -> ["red", "green"] end)

# `Agent.get` takes an agent name and a `fn` that gets passed the current state
# Whatever that `fn` returns is what you'll get back
Agent.get(my_agent, fn colors -> colors end) #=> ["red", "green"]

# Update the agent's state the same way
Agent.update(my_agent, fn colors -> ["blue" | colors] end)
```

## References

* [Getting started guide](http://elixir-lang.org/getting-started/introduction.html) from the [Elixir website](http://elixir-lang.org)
* [Elixir Documentation](http://elixir-lang.org/docs/master/)
* ["Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) by Dave Thomas
* [Elixir Cheat Sheet](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf)
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) by Fred Hebert
* ["Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) by Joe Armstrong
---
language: Elm
contributors:
    - ["Max Goldstein", "http://maxgoldste.in/"]
filename: learnelm.elm
---

Elm is a functional reactive programming language that compiles to (client-side)
JavaScript. Elm is statically typed, meaning that the compiler catches most
errors immediately and provides a clear and understandable error message. Elm is
great for designing user interfaces and games for the web.


```haskell
-- Single line comments start with two dashes.
{- Multiline comments can be enclosed in a block like this.
{- They can be nested. -}
-}

{-- The Basics --}

-- Arithmetic
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20

-- Every number literal without a decimal point can be either an Int or a Float.
33 / 2 -- 16.5 with floating point division
33 // 2 -- 16 with integer division

-- Exponents
5 ^ 2 -- 25

-- Booleans
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- Strings and characters
"This is a string because it uses double quotes."
'a' -- characters in single quotes

-- Strings can be appended.
"Hello " ++ "world!" -- "Hello world!"

{-- Lists, Tuples, and Records --}

-- Every element in a list must have the same type.
["the", "quick", "brown", "fox"]
[1, 2, 3, 4, 5]
-- The second example can also be written with two dots.
List.range 1 5

-- Append lists just like strings.
List.range 1 5 ++ List.range 6 10 == List.range 1 10 -- True

-- To add one item, use "cons".
0 :: List.range 1 5 -- [0, 1, 2, 3, 4, 5]

-- The head and tail of a list are returned as a Maybe. Instead of checking
-- every value to see if it's null, you deal with missing values explicitly.
List.head (List.range 1 5) -- Just 1
List.tail (List.range 1 5) -- Just [2, 3, 4, 5]
List.head [] -- Nothing
-- List.functionName means the function lives in the List module.

-- Every element in a tuple can be a different type, but a tuple has a
-- fixed length.
("elm", 42)

-- Access the elements of a pair with the first and second functions.
-- (This is a shortcut; we'll come to the "real way" in a bit.)
fst ("elm", 42) -- "elm"
snd ("elm", 42) -- 42

-- The empty tuple, or "unit", is sometimes used as a placeholder.
-- It is the only value of its type, also called "Unit".
()

-- Records are like tuples but the fields have names. The order of fields
-- doesn't matter. Notice that record values use equals signs, not colons.
{ x = 3, y = 7 }

-- Access a field with a dot and the field name.
{ x = 3, y = 7 }.x -- 3

-- Or with an accessor function, which is a dot and the field name on its own.
.y { x = 3, y = 7 } -- 7

-- Update the fields of a record. (It must have the fields already.)
{ person |
  name = "George" }

-- Update multiple fields at once, using the current values.
{ particle |
  position = particle.position + particle.velocity,
  velocity = particle.velocity + particle.acceleration }

{-- Control Flow --}

-- If statements always have an else, and the branches must be the same type.
if powerLevel > 9000 then
  "WHOA!"
else
  "meh"

-- If statements can be chained.
if n < 0 then
  "n is negative"
else if n > 0 then
  "n is positive"
else
  "n is zero"

-- Use case statements to pattern match on different possibilities.
case aList of
  [] -> "matches the empty list"
  [x]-> "matches a list of exactly one item, " ++ toString x
  x::xs -> "matches a list of at least one item whose head is " ++ toString x
-- Pattern matches go in order. If we put [x] last, it would never match because
-- x::xs also matches (xs would be the empty list). Matches do not "fall through".
-- The compiler will alert you to missing or extra cases.

-- Pattern match on a Maybe.
case List.head aList of
  Just x -> "The head is " ++ toString x
  Nothing -> "The list was empty."

{-- Functions --}

-- Elm's syntax for functions is very minimal, relying mostly on whitespace
-- rather than parentheses and curly brackets. There is no "return" keyword.

-- Define a function with its name, arguments, an equals sign, and the body.
multiply a b =
  a * b

-- Apply (call) a function by passing it arguments (no commas necessary).
multiply 7 6 -- 42

-- Partially apply a function by passing only some of its arguments.
-- Then give that function a new name.
double =
  multiply 2

-- Constants are similar, except there are no arguments.
answer =
  42

-- Pass functions as arguments to other functions.
List.map double (List.range 1 4) -- [2, 4, 6, 8]

-- Or write an anonymous function.
List.map (\a -> a * 2) (List.range 1 4) -- [2, 4, 6, 8]

-- You can pattern match in function definitions when there's only one case.
-- This function takes one tuple rather than two arguments.
-- This is the way you'll usually unpack/extract values from tuples.
area (width, height) =
  width * height

area (6, 7) -- 42

-- Use curly brackets to pattern match record field names.
-- Use let to define intermediate values.
volume {width, height, depth} =
  let
    area = width * height
  in
    area * depth

volume { width = 3, height = 2, depth = 7 } -- 42

-- Functions can be recursive.
fib n =
  if n < 2 then
    1
  else
    fib (n - 1) + fib (n - 2)

List.map fib (List.range 0 8) -- [1, 1, 2, 3, 5, 8, 13, 21, 34]

-- Another recursive function (use List.length in real code).
listLength aList =
  case aList of
    [] -> 0
    x::xs -> 1 + listLength xs

-- Function calls happen before any infix operator. Parens indicate precedence.
cos (degrees 30) ^ 2 + sin (degrees 30) ^ 2 -- 1
-- First degrees is applied to 30, then the result is passed to the trig
-- functions, which is then squared, and the addition happens last.

{-- Types and Type Annotations --}

-- The compiler will infer the type of every value in your program.
-- Types are always uppercase. Read x : T as "x has type T".
-- Some common types, which you might see in Elm's REPL.
5 : Int
6.7 : Float
"hello" : String
True : Bool

-- Functions have types too. Read -> as "goes to". Think of the rightmost type
-- as the type of the return value, and the others as arguments.
not : Bool -> Bool
round : Float -> Int

-- When you define a value, it's good practice to write its type above it.
-- The annotation is a form of documentation, which is verified by the compiler.
double : Int -> Int
double x = x * 2

-- Function arguments are passed in parentheses.
-- Lowercase types are type variables: they can be any type, as long as each
-- call is consistent.
List.map : (a -> b) -> List a -> List b
-- "List dot map has type a-goes-to-b, goes to list of a, goes to list of b."

-- There are three special lowercase types: number, comparable, and appendable.
-- Numbers allow you to use arithmetic on Ints and Floats.
-- Comparable allows you to order numbers and strings, like a < b.
-- Appendable things can be combined with a ++ b.

{-- Type Aliases and Union Types --}

-- When you write a record or tuple, its type already exists.
-- (Notice that record types use colon and record values use equals.)
origin : { x : Float, y : Float, z : Float }
origin =
  { x = 0, y = 0, z = 0 }

-- You can give existing types a nice name with a type alias.
type alias Point3D =
  { x : Float, y : Float, z : Float }

-- If you alias a record, you can use the name as a constructor function.
otherOrigin : Point3D
otherOrigin =
  Point3D 0 0 0

-- But it's still the same type, so you can equate them.
origin == otherOrigin -- True

-- By contrast, defining a union type creates a type that didn't exist before.
-- A union type is so called because it can be one of many possibilities.
-- Each of the possibilities is represented as a "tag".
type Direction =
  North | South | East | West

-- Tags can carry other values of known type. This can work recursively.
type IntTree =
  Leaf | Node Int IntTree IntTree
-- "Leaf" and "Node" are the tags. Everything following a tag is a type.

-- Tags can be used as values or functions.
root : IntTree
root =
  Node 7 Leaf Leaf

-- Union types (and type aliases) can use type variables.
type Tree a =
  Leaf | Node a (Tree a) (Tree a)
-- "The type tree-of-a is a leaf, or a node of a, tree-of-a, and tree-of-a."

-- Pattern match union tags. The uppercase tags will be matched exactly. The
-- lowercase variables will match anything. Underscore also matches anything,
-- but signifies that you aren't using it.
leftmostElement : Tree a -> Maybe a
leftmostElement tree =
  case tree of
    Leaf -> Nothing
    Node x Leaf _ -> Just x
    Node _ subtree _ -> leftmostElement subtree

-- That's pretty much it for the language itself. Now let's see how to organize
-- and run your code.

{-- Modules and Imports --}

-- The core libraries are organized into modules, as are any third-party
-- libraries you may use. For large projects, you can define your own modules.

-- Put this at the top of the file. If omitted, you're in Main.
module Name where

-- By default, everything is exported. You can specify exports explicitly.
module Name (MyType, myValue) where

-- One common pattern is to export a union type but not its tags. This is known
-- as an "opaque type", and is frequently used in libraries.

-- Import code from other modules to use it in this one.
-- Places Dict in scope, so you can call Dict.insert.
import Dict

-- Imports the Dict module and the Dict type, so your annotations don't have to
-- say Dict.Dict. You can still use Dict.insert.
import Dict exposing (Dict)

-- Rename an import.
import Graphics.Collage as C

{-- Ports --}

-- A port indicates that you will be communicating with the outside world.
-- Ports are only allowed in the Main module.

-- An incoming port is just a type signature.
port clientID : Int

-- An outgoing port has a definition.
port clientOrders : List String
port clientOrders = ["Books", "Groceries", "Furniture"]

-- We won't go into the details, but you set up callbacks in JavaScript to send
-- on incoming ports and receive on outgoing ports.

{-- Command Line Tools --}

-- Compile a file.
$ elm make MyFile.elm

-- The first time you do this, Elm will install the core libraries and create
-- elm-package.json, where information about your project is kept.

-- The reactor is a server that compiles and runs your files.
-- Click the wrench next to file names to enter the time-travelling debugger!
$ elm reactor

-- Experiment with simple expressions in a Read-Eval-Print Loop.
$ elm repl

-- Packages are identified by GitHub username and repo name.
-- Install a new package, and record it in elm-package.json.
$ elm package install elm-lang/html

-- See what changed between versions of a package.
$ elm package diff elm-lang/html 1.1.0 2.0.0
-- Elm's package manager enforces semantic versioning, so minor version bumps
-- will never break your build!
```

The Elm language is surprisingly small. You can now look through almost any Elm
source code and have a rough idea of what is going on. However, the possibilities
for error-resistant and easy-to-refactor code are endless!

Here are some useful resources.

* The [Elm website](http://elm-lang.org/). Includes:
  * Links to the [installers](http://elm-lang.org/install)
  * [Documentation guides](http://elm-lang.org/docs), including the [syntax reference](http://elm-lang.org/docs/syntax)
  * Lots of helpful [examples](http://elm-lang.org/examples)

* Documentation for [Elm's core libraries](http://package.elm-lang.org/packages/elm-lang/core/latest/). Take note of:
  * [Basics](http://package.elm-lang.org/packages/elm-lang/core/latest/Basics), which is imported by default
  * [Maybe](http://package.elm-lang.org/packages/elm-lang/core/latest/Maybe) and its cousin [Result](http://package.elm-lang.org/packages/elm-lang/core/latest/Result), commonly used for missing values or error handling
  * Data structures like [List](http://package.elm-lang.org/packages/elm-lang/core/latest/List), [Array](http://package.elm-lang.org/packages/elm-lang/core/latest/Array), [Dict](http://package.elm-lang.org/packages/elm-lang/core/latest/Dict), and [Set](http://package.elm-lang.org/packages/elm-lang/core/latest/Set)
  * JSON [encoding](http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Encode) and [decoding](http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Decode)

* [The Elm Architecture](https://github.com/evancz/elm-architecture-tutorial#the-elm-architecture). An essay by Elm's creator with examples on how to organize code into components.

* The [Elm mailing list](https://groups.google.com/forum/#!forum/elm-discuss). Everyone is friendly and helpful.

* [Scope in Elm](https://github.com/elm-guides/elm-for-js/blob/master/Scope.md#scope-in-elm) and [How to Read a Type Annotation](https://github.com/elm-guides/elm-for-js/blob/master/How%20to%20Read%20a%20Type%20Annotation.md#how-to-read-a-type-annotation). More community guides on the basics of Elm, written for JavaScript developers.

Go out and write some Elm!
---
language: erlang
contributors:
    - ["Giovanni Cappellotto", "http://www.focustheweb.com/"]
filename: learnerlang.erl
---

```erlang
% Percent sign starts a one-line comment.

%% Two percent characters shall be used to comment functions.

%%% Three percent characters shall be used to comment modules.

% We use three types of punctuation in Erlang.
% Commas (`,`) separate arguments in function calls, data constructors, and
% patterns.
% Periods (`.`) (followed by whitespace) separate entire functions and
% expressions in the shell.
% Semicolons (`;`) separate clauses. We find clauses in several contexts:
% function definitions and in `case`, `if`, `try..catch`, and `receive`
% expressions.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. Variables and pattern matching.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% In Erlang new variables are bound with an `=` statement.
Num = 42.  % All variable names must start with an uppercase letter.

% Erlang has single-assignment variables; if you try to assign a different
% value to the variable `Num`, you’ll get an error.
Num = 43. % ** exception error: no match of right hand side value 43

% In most languages, `=` denotes an assignment statement. In Erlang, however,
% `=` denotes a pattern-matching operation. When an empty variable is used on the
% left hand side of the `=` operator to is bound (assigned), but when a bound
% variable is used on the left hand side the following behaviour is observed.
% `Lhs = Rhs` really means this: evaluate the right side (`Rhs`), and then
% match the result against the pattern on the left side (`Lhs`).
Num = 7 * 6.

% Floating-point number.
Pi = 3.14159.

% Atoms are used to represent different non-numerical constant values. Atoms
% start with lowercase letters, followed by a sequence of alphanumeric
% characters or the underscore (`_`) or at (`@`) sign.
Hello = hello.
OtherNode = example@node.

% Atoms with non alphanumeric values can be written by enclosing the atoms
% with apostrophes.
AtomWithSpace = 'some atom with space'.

% Tuples are similar to structs in C.
Point = {point, 10, 45}.

% If we want to extract some values from a tuple, we use the pattern-matching
% operator `=`.
{point, X, Y} = Point.  % X = 10, Y = 45

% We can use `_` as a placeholder for variables that we’re not interested in.
% The symbol `_` is called an anonymous variable. Unlike regular variables,
% several occurrences of `_` in the same pattern don’t have to bind to the
% same value.
Person = {person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}.
{_, {_, {_, Who}, _}, _} = Person.  % Who = joe

% We create a list by enclosing the list elements in square brackets and
% separating them with commas.
% The individual elements of a list can be of any type.
% The first element of a list is the head of the list. If you imagine removing
% the head from the list, what’s left is called the tail of the list.
ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].

% If `T` is a list, then `[H|T]` is also a list, with head `H` and tail `T`.
% The vertical bar (`|`) separates the head of a list from its tail.
% `[]` is the empty list.
% We can extract elements from a list with a pattern-matching operation. If we
% have a nonempty list `L`, then the expression `[X|Y] = L`, where `X` and `Y`
% are unbound variables, will extract the head of the list into `X` and the tail
% of the list into `Y`.
[FirstThing|OtherThingsToBuy] = ThingsToBuy.
% FirstThing = {apples, 10}
% OtherThingsToBuy = [{pears, 6}, {milk, 3}]

% There are no strings in Erlang. Strings are really just lists of integers.
% Strings are enclosed in double quotation marks (`"`).
Name = "Hello".
[72, 101, 108, 108, 111] = "Hello".


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. Sequential programming.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Modules are the basic unit of code in Erlang. All the functions we write are
% stored in modules. Modules are stored in files with `.erl` extensions.
% Modules must be compiled before the code can be run. A compiled module has the
% extension `.beam`.
-module(geometry).
-export([area/1]). % the list of functions exported from the module.

% The function `area` consists of two clauses. The clauses are separated by a
% semicolon, and the final clause is terminated by dot-whitespace.
% Each clause has a head and a body; the head consists of a function name
% followed by a pattern (in parentheses), and the body consists of a sequence of
% expressions, which are evaluated if the pattern in the head is successfully
% matched against the calling arguments. The patterns are matched in the order
% they appear in the function definition.
area({rectangle, Width, Ht}) -> Width * Ht;
area({circle, R})            -> 3.14159 * R * R.

% Compile the code in the file geometry.erl.
c(geometry).  % {ok,geometry}

% We need to include the module name together with the function name in order to
% identify exactly which function we want to call.
geometry:area({rectangle, 10, 5}).  % 50
geometry:area({circle, 1.4}).  % 6.15752

% In Erlang, two functions with the same name and different arity (number of
% arguments) in the same module represent entirely different functions.
-module(lib_misc).
-export([sum/1]). % export function `sum` of arity 1
                  % accepting one argument: list of integers.
sum(L) -> sum(L, 0).
sum([], N)    -> N;
sum([H|T], N) -> sum(T, H+N).

% Funs are "anonymous" functions. They are called this way because they have
% no name. However, they can be assigned to variables.
Double = fun(X) -> 2 * X end. % `Double` points to an anonymous function
                              % with handle: #Fun<erl_eval.6.17052888>
Double(2).  % 4

% Functions accept funs as their arguments and can return funs.
Mult = fun(Times) -> ( fun(X) -> X * Times end ) end.
Triple = Mult(3).
Triple(5).  % 15

% List comprehensions are expressions that create lists without having to use
% funs, maps, or filters.
% The notation `[F(X) || X <- L]` means "the list of `F(X)` where `X` is taken
% from the list `L`."
L = [1,2,3,4,5].
[2 * X || X <- L].  % [2,4,6,8,10]
% A list comprehension can have generators and filters, which select subset of
% the generated values.
EvenNumbers = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4]

% Guards are constructs that we can use to increase the power of pattern
% matching. Using guards, we can perform simple tests and comparisons on the
% variables in a pattern.
% You can use guards in the heads of function definitions where they are
% introduced by the `when` keyword, or you can use them at any place in the
% language where an expression is allowed.
max(X, Y) when X > Y -> X;
max(X, Y) -> Y.

% A guard is a series of guard expressions, separated by commas (`,`).
% The guard `GuardExpr1, GuardExpr2, ..., GuardExprN` is true if all the guard
% expressions `GuardExpr1`, `GuardExpr2`, ..., `GuardExprN` evaluate to `true`.
is_cat(A) when is_atom(A), A =:= cat -> true;
is_cat(A) -> false.
is_dog(A) when is_atom(A), A =:= dog -> true;
is_dog(A) -> false.

% We won't dwell on the `=:=` operator here; just be aware that it is used to
% check whether two Erlang expressions have the same value *and* the same type.
% Contrast this behaviour to that of the `==` operator:
1 + 2 =:= 3.   % true
1 + 2 =:= 3.0. % false
1 + 2 ==  3.0. % true

% A guard sequence is either a single guard or a series of guards, separated
% by semicolons (`;`). The guard sequence `G1; G2; ...; Gn` is true if at
% least one of the guards `G1`, `G2`, ..., `Gn` evaluates to `true`.
is_pet(A) when is_atom(A), (A =:= dog);(A =:= cat) -> true;
is_pet(A)                                             -> false.

% Warning: not all valid Erlang expressions can be used as guard expressions;
% in particular, our `is_cat` and `is_dog` functions cannot be used within the
% guard sequence in `is_pet`'s definition. For a description of the
% expressions allowed in guard sequences, refer to the specific section
% in the Erlang reference manual:
% http://erlang.org/doc/reference_manual/expressions.html#guards


% Records provide a method for associating a name with a particular element in a
% tuple.
% Record definitions can be included in Erlang source code files or put in files
% with the extension `.hrl`, which are then included by Erlang source code
% files.
-record(todo, {
  status = reminder,  % Default value
  who = joe,
  text
}).

% We have to read the record definitions into the shell before we can define a
% record. We use the shell function `rr` (short for read records) to do this.
rr("records.hrl").  % [todo]

% Creating and updating records:
X = #todo{}.
% #todo{status = reminder, who = joe, text = undefined}
X1 = #todo{status = urgent, text = "Fix errata in book"}.
% #todo{status = urgent, who = joe, text = "Fix errata in book"}
X2 = X1#todo{status = done}.
% #todo{status = done, who = joe, text = "Fix errata in book"}

% `case` expressions.
% `filter` returns a list of all elements `X` in a list `L` for which `P(X)` is
% true.
filter(P, [H|T]) ->
  case P(H) of
    true -> [H|filter(P, T)];
    false -> filter(P, T)
  end;
filter(P, []) -> [].
filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]). % [2, 4]

% `if` expressions.
max(X, Y) ->
  if
    X > Y -> X;
    X < Y -> Y;
    true -> nil
  end.

% Warning: at least one of the guards in the `if` expression must evaluate to
% `true`; otherwise, an exception will be raised.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3. Exceptions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Exceptions are raised by the system when internal errors are encountered or
% explicitly in code by calling `throw(Exception)`, `exit(Exception)`, or
% `erlang:error(Exception)`.
generate_exception(1) -> a;
generate_exception(2) -> throw(a);
generate_exception(3) -> exit(a);
generate_exception(4) -> {'EXIT', a};
generate_exception(5) -> erlang:error(a).

% Erlang has two methods of catching an exception. One is to enclose the call to
% the function that raises the exception within a `try...catch` expression.
catcher(N) ->
  try generate_exception(N) of
    Val -> {N, normal, Val}
  catch
    throw:X -> {N, caught, thrown, X};
    exit:X -> {N, caught, exited, X};
    error:X -> {N, caught, error, X}
  end.

% The other is to enclose the call in a `catch` expression. When you catch an
% exception, it is converted into a tuple that describes the error.
catcher(N) -> catch generate_exception(N).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 4. Concurrency
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Erlang relies on the actor model for concurrency. All we need to write
% concurrent programs in Erlang are three primitives: spawning processes,
% sending messages and receiving messages.

% To start a new process, we use the `spawn` function, which takes a function
% as argument.

F = fun() -> 2 + 2 end. % #Fun<erl_eval.20.67289768>
spawn(F). % <0.44.0>

% `spawn` returns a pid (process identifier); you can use this pid to send
% messages to the process. To do message passing, we use the `!` operator.
% For all of this to be useful, we need to be able to receive messages. This is
% achieved with the `receive` mechanism:

-module(calculateGeometry).
-compile(export_all).
calculateArea() ->
    receive
      {rectangle, W, H} ->
        W * H;
      {circle, R} ->
        3.14 * R * R;
      _ ->
        io:format("We can only calculate area of rectangles or circles.")
    end.

% Compile the module and create a process that evaluates `calculateArea` in the
% shell.
c(calculateGeometry).
CalculateArea = spawn(calculateGeometry, calculateArea, []).
CalculateArea ! {circle, 2}. % 12.56000000000000049738

% The shell is also a process; you can use `self` to get the current pid.
self(). % <0.41.0>

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 5. Testing with EUnit
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Unit tests can be written using EUnits's test generators and assert macros
-module(fib).
-export([fib/1]).
-include_lib("eunit/include/eunit.hrl").

fib(0) -> 1;
fib(1) -> 1;
fib(N) when N > 1 -> fib(N-1) + fib(N-2).

fib_test_() ->
    [?_assert(fib(0) =:= 1),
     ?_assert(fib(1) =:= 1),
     ?_assert(fib(2) =:= 2),
     ?_assert(fib(3) =:= 3),
     ?_assert(fib(4) =:= 5),
     ?_assert(fib(5) =:= 8),
     ?_assertException(error, function_clause, fib(-1)),
     ?_assert(fib(31) =:= 2178309)
    ].

% EUnit will automatically export to a test() function to allow running the tests
% in the erlang shell
fib:test()

% The popular erlang build tool Rebar is also compatible with EUnit
% ```
% rebar eunit
% ```

```

## References

* ["Learn You Some Erlang for great good!"](http://learnyousomeerlang.com/)
* ["Programming Erlang: Software for a Concurrent World" by Joe Armstrong](http://pragprog.com/book/jaerlang/programming-erlang)
* [Erlang/OTP Reference Documentation](http://www.erlang.org/doc/)
* [Erlang - Programming Rules and Conventions](http://www.erlang.se/doc/programming_rules.shtml)
---

category: tool
tool: amd
contributors:
    - ["Frederik Ring", "https://github.com/m90"]

translators:
    - ["Damaso Sanoja", "https://github.com/damasosanoja"]
filename: learnamd-es.js
lang: es-es
---

## Iniciando con AMD

El API del **Módulo de Definición Asíncrono** especifica un mecanismo para definir módulos JavaScript de manera tal que tanto el módulo como sus dependencias puedan ser cargadas de manera asíncrona. Esto es particularmente adecuado para el entorno del navegador donde la carga sincronizada de los módulos genera problemas de rendimiento, usabilidad, depuración y acceso de multi-dominios.

### Conceptos básicos
```javascript
// El API básico de AMD consiste en tan solo dos métodos: `define` y `require`
// y se basa en la definición y consumo de los módulos:
// `define(id?, dependencias?, fábrica)` define un módulo
// `require(dependencias, callback)` importa un conjunto de dependencias y
// las consume al invocar el callback

// Comencemos usando define para definir un nuevo módulo
// que no posee dependencias. Lo haremos enviando un nombre
// y una función fábrica para definirla:
define('awesomeAMD', function(){
  var isAMDAwesome = function(){
    return true;
  };
  // El valor que regresa la función fábrica del módulo será
  // lo que los otros módulos o llamados require recibirán cuando
  // soliciten nuestro módulo `awesomeAMD`.
  // El valor exportado puede ser cualquier cosa, funciones (constructores),
  // objetos, primitivos, incluso indefinidos (aunque eso no ayuda mucho).
  return isAMDAwesome;
});

// Ahora definamos otro módulo que dependa de nuestro módulo `awesomeAMD`.
// Observe que ahora hay un argumento adicional que define 
// las dependencias de nuestro módulo:
define('loudmouth', ['awesomeAMD'], function(awesomeAMD){
  // las dependencias serán enviadas a los argumentos de la fábrica
  // en el orden que sean especificadas
  var tellEveryone = function(){
    if (awesomeAMD()){
      alert('This is sOoOo rad!');
    } else {
      alert('Pretty dull, isn\'t it?');
    }
  };
  return tellEveryone;
});

// Como ya sabemos utilizar define usemos ahora `require` para poner en marcha
//  nuestro programa. La firma de `require` es `(arrayOfDependencies, callback)`.
require(['loudmouth'], function(loudmouth){
  loudmouth();
});

// Para hacer que este tutorial corra código, vamos a implementar una
// versión muy básica (no-asíncrona) de AMD justo aquí:
function define(name, deps, factory){
  // observa como son manejados los módulos sin dependencias
  define[name] = require(factory ? deps : [], factory || deps);
}

function require(deps, callback){
  var args = [];
  // primero recuperemos todas las dependencias que necesita
  // el llamado require
  for (var i = 0; i < deps.length; i++){
    args[i] = define[deps[i]];
  }
  // satisfacer todas las dependencias del callback
  return callback.apply(null, args);
}
// puedes ver este código en acción aquí: http://jsfiddle.net/qap949pd/
```

### Uso en el mundo real con require.js

En contraste con el ejemplo introductorio, `require.js` (la librería AMD más popular) implementa la **A** de **AMD**, permitiéndote cargar los módulos y sus dependencias asincrónicamente via XHR:

```javascript
/* file: app/main.js */
require(['modules/someClass'], function(SomeClass){
  // el callback es diferido hasta que la dependencia sea cargada
  var thing = new SomeClass();
});
console.log('So here we are, waiting!'); // esto correrá primero
```

Por convención, usualmente guardas un módulo en un fichero. `require.js` puede resolver los nombres de los módulos basados en rutas de archivo, de forma que no tienes que nombrar tus módulos, simplemente referenciarlos usando su ubicación. En el ejemplo `someClass` asumimos que se ubica en la carpeta `modules`, relativa a tu `baseUrl` configurada:

* app/
  * main.js
  * modules/
    * someClass.js
    * someHelpers.js
    * ...
  * daos/
    * things.js
    * ...

Esto significa que podemos definir `someClass` sin especificar su id de módulo:

```javascript
/* file: app/modules/someClass.js */
define(['daos/things', 'modules/someHelpers'], function(thingsDao, helpers){
  // definición de módulo, por supuesto, ocurrirá también asincrónicamente
  function SomeClass(){
    this.method = function(){/**/};
    // ...
  }
  return SomeClass;
});
```

Para alterar el comportamiento del mapeo de ruta usa `requirejs.config(configObj)` en tu `main.js`:

```javascript
/* file: main.js */
requirejs.config({
  baseUrl : 'app',
  paths : {
    // también puedes cargar módulos desde otras ubicaciones
    jquery : '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
});
require(['jquery', 'coolLibFromBower', 'modules/someHelpers'], function($, coolLib, helpers){
  // un fichero `main` necesita llamar a require al menos una vez,
  // de otra forma jamás correrá el código
  coolLib.doFancyStuffWith(helpers.transform($('#foo')));
});
```
Las aplicaciones basadas en `require.js` usualmente tendrán un solo punto de entrada (`main.js`) que se pasa a la etiqueta del script `require.js` como un atributo de datos. Será cargado y ejecutado automáticamente al cargar la página:

```html
<!DOCTYPE html>
<html>
<head>
  <title>Cien etiquetas de script? Nunca más!</title>
</head>
<body>
  <script src="require.js" data-main="app/main"></script>
</body>
</html>
```

### Optimizar todo un proyecto usando r.js

Muchas personas prefieren usar AMD para la organización del código durante el desarrollo, pero quieren enviar para producción un solo fichero en vez de ejecutar cientos de XHRs en las cargas de página.

`require.js` incluye un script llamado `r.js` (el que probablemente correrás en node.js, aunque Rhino también es soportado) que puede analizar el gráfico de dependencias de tu proyecto, y armar un solo fichero que contenga todos tus módulos (adecuadamente nombrados), minificado y listo para consumo.

Instálalo usando `npm`:
```shell
$ npm install requirejs -g
```

Ahora puedes alimentarlo con un fichero de configuración:
```shell
$ r.js -o app.build.js
```

Para nuestro ejemplo anterior el archivo de configuración luciría así:
```javascript
/* file : app.build.js */
({
  name : 'main', // nombre del punto de entrada
  out : 'main-built.js', // nombre del fichero donde se escribirá la salida
  baseUrl : 'app',
  paths : {
    // `empty:` le dice a r.js que esto aún debe ser cargado desde el CDN, usando
    // la ubicación especificada en `main.js`
    jquery : 'empty:',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
})
```

Para usar el fichero creado en producción, simplemente intercambia `data-main`:
```html
<script src="require.js" data-main="app/main-built"></script>
```

Un increíblemente detallado [resumen de opciones de generación](https://github.com/jrburke/r.js/blob/master/build/example.build.js) está disponible en el repositorio de GitHub.

### Temas no cubiertos en este tutorial
* [Cargador de plugins / transformaciones](http://requirejs.org/docs/plugins.html)
* [Cargando y exportando estilos CommonJS](http://requirejs.org/docs/commonjs.html)
* [Configuración avanzada](http://requirejs.org/docs/api.html#config)
* [Configuración de Shim (cargando módulos no-AMD)](http://requirejs.org/docs/api.html#config-shim)
* [Cargando y optimizando CSS con require.js](http://requirejs.org/docs/optimization.html#onecss)
* [Usando almond.js para construcciones](https://github.com/jrburke/almond)

### Otras lecturas:

* [Especificaciones oficiales](https://github.com/amdjs/amdjs-api/wiki/AMD)
* [¿Por qué AMD?](http://requirejs.org/docs/whyamd.html)
* [Definición Universal de Módulos](https://github.com/umdjs/umd)

### Implementaciones:

* [require.js](http://requirejs.org)
* [dojo toolkit](http://dojotoolkit.org/documentation/tutorials/1.9/modules/)
* [cujo.js](http://cujojs.com/)
* [curl.js](https://github.com/cujojs/curl)
* [lsjs](https://github.com/zazl/lsjs)
* [mmd](https://github.com/alexlawrence/mmd)
---
category: Algorithms & Data Structures
name: Asymptotic Notation
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
    - ["Gerson Lázaro", "https://gersonlazaro.com"]
lang: es-es
---

# Notaciones asintóticas

## ¿Qué son?

Las notaciones asintóticas son lenguajes que nos permitan analizar el tiempo de 
ejecución de un algoritmo identificando su comportamiento si el tamaño de 
entrada para el algoritmo aumenta. Esto también se conoce como la tasa de 
crecimiento de un algoritmo. ¿El algoritmo de repente se vuelve increíblemente 
lento cuando el tamaño de entrada crece? ¿Tiende a mantener un rápido tiempo de 
ejecución a medida que el tamaño de entrada aumenta? La notación asintótica nos 
da la capacidad para responder a estas preguntas.

## ¿Hay alternativas que respondan a estas preguntas?

Una manera sería contar el número de operaciones primitivas en diferentes 
tamaños de entrada. Aunque esta es una solución válida, la cantidad de trabajo 
que esto conlleva, incluso para los algoritmos simples, no justifica su uso.

Otra manera es medir físicamente la cantidad de tiempo que un algoritmo toma 
para completar su ejecución dados diferentes tamaños de entrada. Sin embargo, 
la exactitud y la relatividad (los tiempos obtenidos sólo serían relativos a la 
máquina sobre la cual se calcularon) de este método está ligado a variables 
ambientales tales como especificaciones de hardware, capacidad de procesamiento,
etc.

## Tipos de Notación Asintótica

En la primera sección de este documento hemos descrito cómo una notación 
asintótica identifica el comportamiento de un algoritmo ante los cambios en el 
tamaño de la entrada. Imaginemos un algoritmo como una función f, con tamaño de 
entrada n, y f(n) siendo el tiempo de ejecución. Así que para un algoritmo f 
dado, con el tamaño de entrada n obtenemos algún tiempo de ejecución resultante 
f(n). Esto resulta en un gráfico donde el eje Y es el tiempo de ejecución, el 
eje X es el tamaño de la entrada y los puntos en el gráfico son los resultantes 
de la cantidad de tiempo para un tamaño de entrada dado.

Puedes etiquetar una función, o un algoritmo, con una notación asintótica de 
muchas maneras diferentes. Algunos ejemplos son describir un algoritmo por su 
mejor caso, su peor caso, o el caso promedio. Lo más común es analizar un 
algoritmo por su peor caso. Por lo general, no se evalúa el mejor caso, porque 
no planeas el algoritmo para estas condiciones. Un muy buen ejemplo de esto son
los algoritmos de ordenamiento; específicamente, añadir elementos a un árbol. 
El mejor caso para la mayoría de los algoritmos podría ser tan bajo como una 
sola operación. Sin embargo, en la mayoría de los casos, el elemento que está 
añadiendo tendrá que ser ordenado adecuadamente a través del árbol, lo que 
podría significar examinar toda una rama. Este es el peor de los casos, y 
para estos casos es que planeamos el algoritmo.


### Tipos de funciones, límites, y simplificación

```
Función logarítmica - log n
Función lineal - an + b
Función cuadrática - an^2 + bn + c
Función polinomicas - an^z + . . . + an^2 + a*n^1 + a*n^0, donde z es constante
Función exponencial - a^n, donde a es constante
```

Estas son algunas clasificaciones de funciones de crecimiento básicos utilizados
en varias notaciones. La lista comienza en la función de crecimiento menor
(logarítmica, el tiempo de ejecución mas rápido) y pasa a la de mayor 
crecimiento  (exponencial, el tiempo de ejecución mas lento). Observe como al 
crecer 'n', o la entrada, en cada una de estas funciones, el resultado aumenta 
claramente mucho más rápido en las cuadráticas, polinómicas y exponenciales, 
en comparación con las logarítmicas y lineales.

Una anotación muy importante es que en las notaciones que se discutirán debes 
hacer tu mejor esfuerzo por utilizar los términos más simples. Esto significa 
hacer caso omiso de las constantes y terminos de orden inferior, porque a medida
que el tamaño de entrada (o n en f(n)) aumenta hacia el infinito (límites 
matemáticos), los términos y constantes de orden inferior se vuelven de poca o 
ninguna importancia. Dicho esto, si tienes constantes que son 2^9001, 
o alguna otra cantidad ridícula, inimaginable, te daras cuenta de que la 
simplificación sesgará la exactitud de la notación.

Como queremos algo simplificado, vamos a modificarlo un poco...

```
Logarítmico - log n
Lineal - n
Cuandrático - n^2
Polinómico - n^z, donde z es constante
Exponencial - a^n, donde a es constante
```

### O-grande (Big-O)
O-grande (Big-O), comúnmente escrito como O, es una notación asintótica para el 
peor caso, o el techo de crecimiento para una función determinada. Si `f (n)` 
es el tiempo de ejecución del algoritmo, y `g (n)` es un tiempo de complejidad 
arbitraria que relacionas con el algoritmo, entonces `f (n)` es O(g(n)), si por 
cualquier constante real c (c > 0), `f (n)` <= `c g(n)` para cada tamaño de 
entrada n (n > 0 ).


*Ejemplo 1*  

```
f(n) = 3log n + 100  
g(n) = log n
```

`f(n)` es O(g(n))?  
`3 log n + 100` es O(log n)?  
Echemos un vistazo a la definición de O-grande.

```
3log n + 100 <= c * log n  
```
¿Hay alguna constante c que satisface esto para todo n? 

```
3log n + 100 <= 150 * log n, n > 2 (indefinido en n = 1)  
```

¡Sí! La definición de O-grande se cumple, por lo tanto `f (n)` es O(g(n)).

*Ejemplo 2*  

```
f(n) = 3*n^2  
g(n) = n
```

`f(n)` es O(g(n))?  
`3 * n^2` es O(n)?  
Echemos un vistazo a la definición de O-grande.

```
3 * n^2 <= c * n  
```

¿Hay alguna constante c que satisface esto para todo n? 
No, no la hay. `f(n)` no es O(g(n)).

### Big-Omega
Big-Omega, comunmente escrito como Ω, es una notación asintótica para el mejor
caso, o el piso en el crecimiento para una función dada.

`f(n)` es Ω(g(n)), si para cualquier constante real c (c > 0), 
`f(n)` es >= `c g(n)` para cualquier tamaño de entrada n (n > 0).

No dudes en dirigirte a los recursos adicionales para ejemplos sobre esto. 
O-grande es la notación principal utilizada para la complejidad general de
tiempo algoritmico.

### Notas finales
Es difícil mantener este tipo de tema corto, y sin duda deberias revisar los 
libros y recursos en línea en la lista. Entran en mucha mayor profundidad con 
definiciones y ejemplos. 

## Libros

* [Algoritmos (Algorithms)](http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X)
* [Diseño de algoritmos (Algorithm Design)](http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651)

## Recursos Online

* [MIT](http://web.mit.edu/16.070/www/lecture/big_o.pdf)
* [KhanAcademy](https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation)
* [Apuntes Facultad de Ingeniería](https://www.scribd.com/document/317979564/Apuntes-Sobre-Analisis-de-Algoritmos)
---
language: awk
filename: learnawk-es.awk
contributors:
    - ["Marshall Mason", "http://github.com/marshallmason"]
translators:
    - ["Hugo Guillén-Ramírez", "http://github.com/HugoGuillen"]
lang: es-es
---

AWK es una herramienta estándar en cada sistema UNIX compatible con POSIX.
Es como un Perl restringido, perfecto para tareas de procesamiento de texto y
otras necesidades de scripting. Tiene una sintaxis similar a C, pero sin 
puntos y comas, manejo manual de memoria y tipado estático. Puedes llamarlo 
desde un script de shell o usarlo como un lenguaje stand-alone para scripting.

¿Por qué elegir AWK sobre Perl? Principalmente, porque AWK es parte de UNIX.
Siempre puedes contar con él, mientras que el futuro de Perl está en duda. AWK
es más fácil de leer que Perl. Para scripts sencillos de procesamiento de texto,
particularmente si es para leer archivos línea a línea y dividir por
delimitadores, probablemente AWK es la herramienta correcta para el trabajo.

```awk
#!/usr/bin/awk -f

# Los comentarios tienen este aspecto.

# Los programas AWK son una colección de patrones y acciones. El patrón más
# importante es BEGIN. Las acciones van en bloques delimitados por llaves.

BEGIN {

    # BEGIN correrá al inicio del programa. Es donde pones todo el código
    # preliminar antes de procesar los archivos de texto. Si no tienes archivos
    # de texto, piensa en BEGIN como el punto de entrada principal del script.

    # Las variables son globales. Asígnalas o úsalas sin declararlas.
    count = 0

    # Los operadores son justo como en C (y amigos).
    a = count + 1
    b = count - 1
    c = count * 1
    d = count / 1
    e = count % 1 # módulo
    f = count ^ 1 # exponenciación

    a += 1
    b -= 1
    c *= 1
    d /= 1
    e %= 1
    f ^= 1

    # Incremento y decremento en uno
    a++
    b--

    # Como un operador prefijo, regresa el valor modificado
    ++a
    --b

    # Nota que no hay puntación para terminar las instrucciones

    # Instrucciones de control
    if (count == 0)
        print "Iniciando count en 0"
    else
        print "Eh?"

    # O puedes usar el operador ternario
    print (count == 0) ? "Iniciando count en 0" : "Eh?"

    # Bloques formados por múltiples líneas usan llaves
    while (a < 10) {
        print "La concatenación de strings se hace " " con series " 
	print " de" " strings separados por espacios"
        print a

        a++
    }

    for (i = 0; i < 10; i++)
        print "El viejo confiable ciclo for"

    # Los operaciones de comparación son estándar...
    a < b   # Menor que
    a <= b  # Menor o igual que
    a != b  # No igual
    a == b  # Igual
    a > b   # Mayor que
    a >= b  # Mayor o igual que

    # ...así como los operadores lógicos
    a && b  # AND
    a || b  # OR

    # Además están las expresiones regulares
    if ("foo" ~ "^fo+$")
        print "Fooey!"
    if ("boo" !~ "^fo+$")
        print "Boo!"

    # Arrays
    arr[0] = "foo"
    arr[1] = "bar"
    # Desafortunadamente no hay otra manera de inicializar un array.
    # Tienes que inicializar cada posición del array.

    # También hay arrays asociativos
    assoc["foo"] = "bar"
    assoc["bar"] = "baz"

    # Y arrays multidimensionales con limitaciones que no mencionaré aquí
    multidim[0,0] = "foo"
    multidim[0,1] = "bar"
    multidim[1,0] = "baz"
    multidim[1,1] = "boo"

    # Puedes probar pertenencia a un array
    if ("foo" in assoc)
        print "Fooey!"

    # También puedes usar el operador 'in' para iterar las claves de un array
    for (key in assoc)
        print assoc[key]

    # La terminal es un array especial llamado ARGV
    for (argnum in ARGV)
        print ARGV[argnum]

    # Puedes eliminar elementos de un array.
    # Esto es útil para prevenir que AWK suponga que algunos argumentos
    # son archivos por procesar.
    delete ARGV[1]

    # El número de argumentos de la terminal está en la variable ARGC
    print ARGC

    # AWK tiene tres categorías de funciones incluidas.
    # Demostraré esas funciones posteriormente.

    return_value = arithmetic_functions(a, b, c)
    string_functions()
    io_functions()
}

# Así se define una función
function arithmetic_functions(a, b, c,     localvar) {

    # Probablemente la parte más molesta de AWK es que no hay variables locales
    # Todo es global. No es problema en scripts pequeños, pero sí para
    # scripts más grandes.

    # Hay un work-around (mmm... hack). Los argumentos de las funciones son 
    # locales para la función, y AWK permite definir más argumentos de función
    # de los que necesita, por lo que define las variables locales en la 
    # declaración como en la función de arriba. Como convención, agrega
    # espacios en blanco para distinguir los parámetros de la función de las 
    # variables locales. En este ejemplo, a, b y c son parámetros y localvar es una 
    # variable local.

    # Ahora, a demostrar las funciones aritméticas

    # La mayoría de las implementaciones de AWK tienen funciones
    # trigonométricas estándar
    localvar = sin(a)
    localvar = cos(a)
    localvar = atan2(a, b) # arcotangente de b / a

    # Y cosas logarítmicas
    localvar = exp(a)
    localvar = log(a)

    # Raíz cuadrada
    localvar = sqrt(a)

    # Trucar un flotante a entero
    localvar = int(5.34) # localvar => 5

    # Números aleatorios
    srand() # La semilla es el argumento. Por defecto usa el tiempo del sistema
    localvar = rand() # Número aleatorio entre 0 y 1.

    # Y aquí se regresa el valor
    return localvar
}

function string_functions(    localvar, arr) {

    # AWK tiene algunas funciones para procesamiento de strings,
    # y muchas dependen fuertemente en expresiones regulares.

    # Buscar y remplazar, primer instancia (sub) o todas las instancias (gsub)
    # Ambas regresan el número de matches remplazados.
    localvar = "fooooobar"
    sub("fo+", "Meet me at the ", localvar) # localvar => "Meet me at the bar"
    gsub("e+", ".", localvar) # localvar => "m..t m. at th. bar"

    # Buscar una cadena que haga match con una expresión regular
    # index() hace lo mismo, pero no permite expresiones regulares
    match(localvar, "t") # => 4, dado que 't' es el cuarto caracter

    # Separar con base en un delimitador
    split("foo-bar-baz", arr, "-") # a => ["foo", "bar", "baz"]

    # Otras funciones útiles
    sprintf("%s %d %d %d", "Testing", 1, 2, 3) # => "Testing 1 2 3"
    substr("foobar", 2, 3) # => "oob"
    substr("foobar", 4) # => "bar"
    length("foo") # => 3
    tolower("FOO") # => "foo"
    toupper("foo") # => "FOO"
}

function io_functions(    localvar) {

    # Ya has visto print
    print "Hello world"

    # También hay printf
    printf("%s %d %d %d\n", "Testing", 1, 2, 3)

    # AWK no tiene handles de archivos en sí mismo. Automáticamente abrirá un 
    # handle de archivo cuando use algo que necesite uno. El string que usaste 
    # para esto puede ser tratada como un handle de archivo para propósitos de I/O.
    # Esto lo hace similar al scripting de shell:

    print "foobar" >"/tmp/foobar.txt"

    # Ahora el string "/tmp/foobar.txt" es un handle. Puedes cerrarlo:
    close("/tmp/foobar.txt")

    # Aquí está como correr algo en el shell
    system("echo foobar") # => muestra foobar

    # Lee una línea de la entrada estándar (stdin) y lo guarda en localvar
    getline localvar

    # Lee una línea desde un pipe
    "echo foobar" | getline localvar # localvar => "foobar"
    close("echo foobar")

    # Lee una línea desde un archivo y la guarda en localvar
    getline localvar <"/tmp/foobar.txt"
    close("/tmp/foobar.txt")
}

# Como dije al inicio, los programas en AWK son una colección de patrones y 
# acciones. Ya conociste el patrón BEGIN. otros patrones sólo se usan si estás
# procesando líneas desde archivos o stdin.

# Cuando pasas argumentos a AWK, son tratados como nombres de archivos a 
# procesar. Los va a procesar todos, en orden. Imagínalos como un ciclo for 
# implícito, iterando sobre las líneas de estos archivos. Estos patrones y
# acciones son como instrucciones switch dentro del ciclo.

/^fo+bar$/ {
    
    # Esta acción se ejecutará por cada línea que haga match con la expresión
    # regular /^fo+bar$/, y será saltada por cualquier línea que no haga match.
    # Vamos a sólo mostrar la línea:

    print

    # ¡Wow, sin argumento! Eso es porque print tiene uno por defecto: $0.
    # $0 es el nombre de la línea actual que se está procesando.
    # Se crea automáticamente para ti.

    # Probablemente puedas adivinar que hay otras variables $. Cada línea es 
    # separada implícitamente antes de que se llame cada acción, justo como lo
    # hace shell. Y, como shell, cada campo puede ser accesado con $.

    # Esto mostrará el segundo y cuarto campos de la línea
    print $2, $4

    # AWK automáticamente define muchas otras variables que te ayudan a
    # inspeccionar y procesar cada línea. La más importante es NF

    # Imprime el número de campos de esta línea
    print NF

    # Imprime el último campo de esta línea
    print $NF
}

# Cada patrón es realmente un prueba de verdadero/falso. La expresión regular
# en el último patrón también es una prueba verdadero/falso, pero parte de eso
# estaba oculto. Si no le das un string a la prueba, supondrá $0, la línea que
# se está procesando. La versión completa de esto es:

$0 ~ /^fo+bar$/ {
    print "Equivalente al último patrón"
}

a > 0 {
    # Esto se ejecutará una vez por línea, mientras a sea positivo
}

# Y ya te das una idea. Procesar archivos de texto, leyendo una línea a la vez,
# y haciendo algo con ella, particularmente separando en un deliminator, es tan
# común en UNIX que AWK es un lenguaje de scripting que hace todo eso por ti
# sin que tengas que pedirlo. Basta con escribir los patrones y acciones 
# basados en lo que esperas de la entrada y lo quieras quieras hacer con ella.

# Aquí está un ejemplo de un script simple, para lo que AWK es perfecto.
# El script lee un nombre de stdin y muestra el promedio de edad para todos los
# que tengan ese nombre. Digamos que como argumento pasamos el nombre de un
# archivo con este contenido:
#
# Bob Jones 32
# Jane Doe 22
# Steve Stevens 83
# Bob Smith 29
# Bob Barker 72
#
# Éste es el script:

BEGIN {

    # Primero, pedir al usuario el nombre
    print "¿Para qué nombre quieres el promedio de edad?"

    # Recuperar una línea de stdin, no de archivos en la línea de comandos
    getline name <"/dev/stdin"
}

# Ahora, hacer match con cada línea cuyo primer campo es el nombre dado
$1 == name {

    # Aquí dentro tenemos acceso a variables útiles precargadas:
    # $0 es toda la línea
    # $3 es el tercer campo, la edad, que es lo que nos interesa
    # NF es el número de campos, que debe ser 3
    # NR es el número de registros (líneas) vistos hasta ahora
    # FILENAME es el nombre del archivo que está siendo procesado
    # FS es el campo separador, " " en este caso
    # Y muchas más que puedes conocer ejecutando 'man awk' en la terminal.

    # Llevar el registro de la suma y cuantas líneas han hecho match.
    sum += $3
    nlines++
}

# Otro patrón especial es END. Va a ejecutarse después de procesar todos los 
# archivos de texto. A diferencia de BEGIN, sólo se ejecuta si le das dado una
# entrada a procesar. Se ejecutará después de que todos los archivos hayan sido
# leídos y procesados según las reglas y acciones que programaste. El propósito
# es usualmente para mostrar un reporte final, o hacer algo con el agregado de
# los datos que has acumulado durante la ejecución del script.

END {
    if (nlines)
        print "La edad promedio para " name " es " sum / nlines
}

```
Más información:

* [Tutorial de AWK](http://www.grymoire.com/Unix/Awk.html)
* [Página man de AWK](https://linux.die.net/man/1/awk)
* [La guía del usuario de GNU Awk](https://www.gnu.org/software/gawk/manual/gawk.html): GNU Awk se encuentra en la mayoría de los sistemas Linux.
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
translators:
    - ["Daniel Zendejas", "https://github.com/danielzendejas"]
filename: LearnBash-es.sh
lang: es-es
---

Tutorial de Shell en español.

Bash es el nombre del shell de unix, el cual también es distribuido como 
el shell del sistema operativo GNU. También es el shell
por defecto de Linux y Mac OS X. Casi todos los ejemplos abajo pueden 
ser parte de un script shell o ser ejecutados directamente en la terminal.

[Leer más aquí.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash

# La primera línea del script es el [shebang](http://en.wikipedia.org/wiki/Shebang_(Unix)) que le indica al sistema 
# cómo ejecutar el script.
# Como te habrás dado cuenta, los comentarios en shell empiezan con #.
# El shebang también es un comentario.

# Ejemplo sencillo de hola mundo:
echo ¡Hola mundo!

# Cada comando empieza con una nueva línea, o después de un punto y coma:
echo 'Esta es la primera línea'; echo 'Esta es la segunda línea'

# Para declarar una variable se hace lo siguiente:
VARIABLE="Mi string"

# Pero no así:
VARIABLE = "Mi string"

# Bash decidirá que VARIABLE es un comando a ejecutar, dando un error.

# Usando la variable:
echo $VARIABLE
echo "$VARIABLE"
echo '$VARIABLE'

# Cuando la variable es usada - o asignada, exportada, etcétera - se
# escribe su nombre sin $. Si se quiere saber el valor de la variables,
# entonces sí se usa $. Note que ' (comilla simple) no expandirá las 
# variables.

# Sustitución de strings en variables.
echo ${VARIABLE/Mi/Una}
# Esto sustituirá la primera cadena "Mi" con "Una".

# Substring de una variable.
echo ${VARIABLE:0:7}
# Esto va a regresar sólo los primeros 7 caracteres del valor.

# Valor por defecto de una variable
echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"}
# Esto trabaja para null (VARIABLE=), string vacío (VARIABLE=""), }
# cero (VARIABLE=0) regresa 0

# Variables del sistema:
# Aquí hay algunas variables incluídas en el sistema:
echo "El valor de regreso del último programa: $?"
echo "PID del sistema: $$"
echo "Número de argumentos: $#"
echo "Argumentos del script: $@"
echo "Argumentos del script separados en variables: $1 $2..."

# Para leer un valor del input:
echo "¿Cuál es tu nombre?"
read NOMBRE # Note que no necesitamos declarar una variable
echo ¡Hola, $NOMBRE!

# Tenemos la estructura 'if' usual:
# use 'man test' para más información sobre condicionales
if [ $NOMBRE -ne $USER ]
then
    echo "Tu nombre es tu usuario."
else
    echo "Tu nombre no es tu usuario."
fi

# También hay ejecuciones condicionadas.
echo "Siempre ejecutado" || echo "Sólo ejecutado si el primer comando falla"
echo "Siempre ejecutado" && echo "Sólo ejecutado si el primer comando NO falla"

# Para usar && y || con condicionales, se necesitan 
# múltiples pares de corchetes:
if [ $NOMBRE == "Steve" ] && [ $EDAD -eq 15 ]
then
    echo "Esto correrá si $NOMBRE es Steve Y $EDAD es 15."
fi

if [ $NOMBRE == "Daniya" ] || [ $NOMBRE == "Zach" ]
then
    echo "Esto correrá si $NOMBRE es Daniya O Zach."
fi

# Las expresiones se denotan con el siguiente formato:
echo $(( 10 + 5 ))

# A diferencia de otros lenguajes de programación, bash es shell , así que
# funciona en un contexto de directorio actual. Puedes listar archivos y
# directorios en un directorio actual con el comando 'ls':
ls

# Estos comandos tienen opciones que controlan su ejecución:
ls -l # Lista todos los archivos y directorios en líneas distintas.

# Los resultados del comando anterior pueden ser pasados al siguiente 
# como input. El comando 'grep' filtra el input con los comandos provistos.
# Así es como podemos listar archivos .txt en el directorio actual:
ls -l | grep "\.txt"

# Puedes también redireccionar el input y el error lanzado de algún comando.
python2 hello.py < "input.in"
python2 hello.py > "output.out"
python2 hello.py 2> "error.err"

# El error lanzado eliminará el contenido del archivo si es que existe,
# para después escribir el error. Para que se concatene (en lugar de eliminar)
# use el comando ">>".

# Los comandos pueden ser sustituidos dentro de otros comandos usando $():
# El siguiente ejemplo despliega el número de archivos y directorios en el
# directorio actual.
echo "Hay $(ls | wc -l) elementos aquí."

# Lo mismo puede ser hecho usando comillas invertidas `` pero no pueden ser
# anidadas. El método preferido es $().
echo "Hay `ls | wc -l` elementos aquí."

# Bash usa una estructura de casos similar al switch de Java o C++:
case "$VARIABLE" in 
    # Lista de patrones que las condiciones deben cumplir: 
    0) echo "Hay un cero.";;
    1) echo "Hay un uno.";;
    *) echo "No es null.";;
esac

# Para los ciclos, se usa la estructura 'for'. Cicla para cada argumento dado:
# El contenido de $VARIABLE se imprime tres veces.
for VARIABLE in {1..3}
do
    echo "$VARIABLE"
done

# ciclos while:
while [true]
do
    echo "cuerpo del ciclo..."
    break
done

# También se pueden definir sub-rutinas (funciones)
# Definición:
function miFuncion ()
{
    echo "Los argumentos trabajan igual que argumentos de script: $@"
    echo "Y: $1 $2..."
    echo "Esto es una función"
    return 0
}

# O simplemente:
miOtraFuncion ()
{
    echo "¡Otra forma de declarar funciones!"
    return 0
}

# Para llamar a tu función
foo "Mi nombre es:" $NOMBRE

# Hay muchos comandos útiles que puedes aprender:
# imprime las últimas 10 líneas del archivo file.txt
tail -n 10 file.txt
# imprime las primeras 10 líneas del archivo file.txt
head -n 10 file.txt
# ordena las líneas del archivo file.txt
sort file.txt
# identifica u omite las líneas repetidas, con -d las reporta
uniq -d file.txt
# imprime sólo la primera columna antes de cada ',' en el archivo|
cut -d ',' -f 1 file.txt
```
---
language: Brainfuck
filename: brainfuck-es.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Daniel Zendejas", "https://github.com/DanielZendejas"]
lang: es-es
---

Brainfuck (con mayúscula sólo al inicio de una oración) es un
lenguaje de programación extremadamente pequeño, Turing completo con sólo 8 comandos.

Puedes probar brainfuck en tu navegador con [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/).


```

Cualquier caracter que no sea "><+-.,[]" (sin incluir las comillas)
será ignorado.

Brainfuck es representado por un arreglo de 30,000 celdas inicializadas
en cero y un puntero apuntando la celda actual.

Existen ocho comandos:

+ : Incrementa 1 al valor de la celda actual.
- : Decrementa 1 al valor de la celda actual.
> : Mueve el apuntador a la siguiente celda. (a la derecha)
< : Mueve el apuntador a la celda anterior. (a la izquierda)
. : Imprime el valor en ASCII de la celda actual (p.e. 65 = 'A')
, : Lee un caracter como input y lo escribe en la celda actual.
[ : Si el valor en la celda actual es cero mueve el apuntador
	hasta el primer ']' que encuentre. Si no es cero sigue a la
	siguiente instrucción.
] : Si el valor en la celda actual es cero, entonces sigue con 
	la siguiente instrucción. Si no entonces mueve el apuntador
	hacia atrás	hasta encontrar el primer '['.

[ y ] forman un while. Obviamente, deben estar balanceados.

Estos son algunos ejemplos de programas escritos con brainfuck.

++++++ [ > ++++++++++ < - ] > +++++ .

Este programa imprime la letra 'A'. Primero, incrementa la celda #1 a 
6. La celda #1 será usada para hacer los ciclos. Después entra al ciclo
([) y se mueve a la celda #2 (>). Después incrementa la celda #2 10 veces,
y se regresa a la celda #1 (<), para después decrementarla en 1 (-).
Este ciclo ocurre 6 veces (le toma 6 decrementos a la celda #1 volverse 0),
cuando esto pasa se salta a (]) y continúa.

En este punto estamos en la celda #1, que tiene un valor de 0, mientras
que la celda #2 tiene un valor de 60. Nos movemos a la celda #2 (>),
la incrementamos 5 veces para tener un valor de 65 y luego imprimimos
el valor de la celda #2 (.). 65 es 'A' en ASCII así que la letra 'A'
se imprime.

, [ > + < - ] > .

Este programa lee un caracter del input y lo copia en la celda #2 (,). 
Después empieza un ciclo. Nos movemos a la celda #2 (>) e incrementamos su
valor (+). Regresamos a la celda #1 y decrementamos su valor en 1 (-).
Esto continúa hasta que la celda #1 contenga un cero. Cuando #1 contenga un 
cero la celda #2 tendrá el valor inicial de #1. Como este ciclo siempre
terminara en la celda #1 nos movemos a la celda #2 e imprimimos (.).

Ten en cuenta que los espacios son sólo para fines de legibilidad.
Es lo mismo escribir el ejemplo de arriba que esto:
,[>+<-]>.

Intenta descrifrar lo que hace este programa:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Este programa toma dos números como input y los multiplica.

Primero recibe dos números del usuario. Luego empieza el ciclo externo,
condicionado en la celda #1. Luego se mueve a la celda #2, comenzando
el ciclo interno condicionado en la celda #2 incrementando la celda #3.
Sin embargo viene un problema: El ciclo interior no funcionará nuevamente
hasta la próxima vez. Para resolver este problema también incrementamos la
celda #4 y luego copiamos la celda #4 a la celda #2. La celda #3 contiene
el resultado.
```
Y eso es brainfuck. No es tan difícil, ¿verdad? Como diversión, puedes escribir
tu propio intérprete de brainfuck o tu propio programa en brainfuck. El
intérprete es relativamente sencillo de hacer, pero si eres masoquista,
puedes intentar construir tu propio intérprete de brainfuck... en brainfuck.
---
category: Algorithms & Data Structures
name: Binary Search
contributors:
    - ["Abhishek Jaisingh", "http://github.com/abhishekjiitr"]
translators:
    - ["Gino Amaury", "https://github.com/ginoamaury"]
lang: es-es    
---

# Búsqueda Binaria

## Por qué Búsqueda Binaria?

La búsqueda es uno de los problemas principales en el dominio de la ciencia de la computación. Hoy en dia hay mas de 1 billon de búsquedas por año, y necesitamos tener algoritmos que puedan hacer esto muy rápido. La búsqueda binaria es uno de los algoritmos fundamentales en la ciencia de la computación. Con el fin de explorarlo, vamos a construir por primera vez un esqueleto teórico y lo utilizaremos para implementar el algoritmo apropiadamente.

## Introducción

Un método sencillo para poner en práctica la búsqueda es hacer una búsqueda lineal, pero este método requiere mucho tiempo y este crece linealmente con la cantidad o el número de datos. es decir, empezar desde el elemento a la izquierda de la matriz [] y uno por uno compara x con cada elemento de la matriz [], si x coincide con un elemento, devuelve el índice. Si x no coincide con ninguno de los elementos, devuelve -1.

```
Búsqueda Lineal: O (n)               Tiempo lineal

Búsqueda Binaria: O ( log(n) )		   Tiempo logarítmico

```
```
def search(arr, x):
 
    for i in range(len(arr)):
 
        if arr[i] == x:
            return i
 
    return -1

```
## Algoritmo de Búsqueda Binaria 

El requisito básico para que la búsqueda binaria funcione es que los datos a buscar deben estar ordenados (en cualquier orden).


### Algo

```
La idea de la búsqueda binaria es usar la información de que la matriz está ordenada y así reducir la complejidad del tiempo a O(Logn). Básicamente ignoramos la mitad de los elementos después de la primera comparación.
1) Compare x con el elemento del medio.
2) si x coincide con el elemento del medio , retornamos el índice del elemento del medio.
3) Si no coincide, si x es mayor que el elemento del medio, entonces x solo puede estar en la mitad derecha justo después del elemento del medio. Así que recurrimos a la mitad derecha. 
4) Si no (x es más pequeño) recurrimos a la mitad izquierda.
Siguiendo la implementación recursiva de búsqueda binaria.

```

### Notas finales 

Hay otra forma de búsqueda binaria que es muy útil.

## Libros

* [CLRS EN](https://mitpress.mit.edu/books/introduction-algorithms)
* [Algoritmos EN](http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X)
* [Diseño de Algoritmos EN](http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651)

## Recursos en línea 

* [GeeksforGeeks EN](http://www.geeksforgeeks.org/the-ubiquitous-binary-search-set-1/)
* [Topcoder Tutorial EN](https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/)
---
language: c++
filename: learncpp-es.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Matt Kline", "https://github.com/mrkline"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Connor Waters", "http://github.com/connorwaters"]
translators:
    - ["Gerson Lázaro", "https://gersonlazaro.com"]
lang: es-es
---

C++ es un lenguaje de programación de sistemas que,
[de acuerdo a su inventor Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote),
fue diseñado para

- ser un "mejor C"
- soportar abstracción de datos
- soportar programación orientada a objetos
- soportar programación genérica

Aunque su sintaxis puede ser más difícil o compleja que los nuevos lenguajes,
es ampliamente utilizado, ya que compila instrucciones nativas que pueden ser
directamente ejecutadas por el procesador y ofrece un estricto control sobre
el hardware (como C), mientras ofrece características de alto nivel como
genericidad, excepciones, y clases. Esta combinación de velocidad y
funcionalidad hace de C ++ uno de los lenguajes de programación más utilizados.

```c++
////////////////////
// Comparación con C
////////////////////

// C ++ es _casi_ un superconjunto de C y comparte su sintaxis básica para las
// declaraciones de variables, tipos primitivos y funciones.

// Al igual que en C, el punto de entrada de tu programa es una función llamada
// main con un retorno de tipo entero.
// Este valor sirve como código de salida del programa.
// Mira http://en.wikipedia.org/wiki/Exit_status para mayor información.
int main(int argc, char** argv)
{
	// Los argumentos de la línea de comandos se pasan por argc y argv de la
	// misma manera que en C.
	// argc indica el número de argumentos,
	// y argv es un arreglo de strings de estilo C (char*)
	// representando los argumentos.
	// El primer argumento es el nombre con el que el programa es llamado.
	// argc y argv pueden omitirse si no te preocupan los argumentos,
	// dejando la definición de la función como int main ()

	// Un estado de salida 0 indica éxito.
    return 0;
}

// Sin embargo, C ++ varía en algunas de las siguientes maneras:

// En C++, los caracteres literales son caracteres
sizeof('c') == sizeof(char) == 1

// En C, los caracteres literales son enteros
sizeof('c') == sizeof(int)


// C++ tiene prototipado estricto
void func(); // función que no acepta argumentos

// En C
void func(); // función que puede aceptar cualquier número de argumentos

// Use nullptr en lugar de NULL en C++
int* ip = nullptr;

// Las cabeceras (headers) estándar de C están disponibles en C ++,
// pero tienen el prefijo "c" y no tienen sufijo .h.
#include <cstdio>

int main()
{
    printf("Hola mundo!\n");
    return 0;
}

//////////////////////////
// Sobrecarga de funciones
//////////////////////////

// C++ soporta sobrecarga de funciones
// siempre que cada función tenga diferentes parámetros.

void print(char const* myString)
{
    printf("String %s\n", myString);
}

void print(int myInt)
{
    printf("Mi entero es %d", myInt);
}

int main()
{
    print("Hello"); // Resolves to void print(const char*)
    print(15); // Resolves to void print(int)
}

////////////////////////////////////
// Argumentos de función por defecto
////////////////////////////////////

// Puedes proporcionar argumentos por defecto para una función si no son
// proporcionados por quien la llama.

void doSomethingWithInts(int a = 1, int b = 4)
{
    // Hacer algo con los enteros aqui
}

int main()
{
    doSomethingWithInts();      // a = 1,  b = 4
    doSomethingWithInts(20);    // a = 20, b = 4
    doSomethingWithInts(20, 5); // a = 20, b = 5
}

// Los argumentos predeterminados deben estar al final de la lista de argumentos.

void invalidDeclaration(int a = 1, int b) // Error!
{
}

/////////////////////
// Espacios de nombre
/////////////////////

// Espacios de nombres proporcionan ámbitos separados para variable, función y
// otras declaraciones.
// Los espacios de nombres se pueden anidar.

namespace First {
    namespace Nested {
        void foo()
        {
            printf("Esto es First::Nested::foo\n");
        }
    } // fin del nombre de espacio Nested
} // fin del nombre de espacio First

namespace Second {
    void foo()
    {
        printf("Esto es Second::foo\n")
    }
}

void foo()
{
    printf("Este es global: foo\n");
}

int main()
{

	// Incluye todos los símbolos del espacio de nombre Second en el ámbito
	// actual. Tenga en cuenta que simplemente foo() no funciona, ya que ahora
	// es ambigua si estamos llamando a foo en espacio de nombres Second o en
	// el nivel superior.
    using namespace Second;

    Second::foo(); // imprime "Esto es Second::foo"
    First::Nested::foo(); // imprime "Esto es First::Nested::foo"
    ::foo(); // imprime "Este es global: foo"
}

/////////////////
// Entrada/Salida
/////////////////

// La entrada y salida de C++ utiliza flujos (streams)
// cin, cout, y cerr representan a stdin, stdout, y stderr.
// << es el operador de inserción >> es el operador de extracción.


#include <iostream> // Incluir para el flujo de entrada/salida

using namespace std; // Los streams estan en std namespace (libreria estandar)

int main()
{
   int myInt;

   // Imprime a la stdout (o terminal/pantalla)
   cout << "Ingresa tu número favorito:\n";
   // Toma una entrada
   cin >> myInt;

   // cout puede también ser formateado
   cout << "Tu número favorito es " << myInt << "\n";
   // imprime "Tu número favorito es <myInt>"

    cerr << "Usado para mensajes de error";
}
////////////////////
// Cadenas (Strings)
////////////////////

// Las cadenas en C++ son objetos y tienen muchas funciones
#include <string>

using namespace std; // Strings también estan en namespace std

string myString = "Hola";
string myOtherString = " Mundo";

// + es usado para concatenar.
cout << myString + myOtherString; // "Hola Mundo"

cout << myString + " Tu"; // "Hola Tu"

// Las cadenas en C++ son mutables y tienen valor semántico.
myString.append(" Perro");
cout << myString; // "Hola Perro"


//////////////
// Referencias
//////////////

// Además de punteros como los de C,
// C++ tiene _references_.
// Estos tipos de puntero no pueden ser reasignados una vez establecidos
// Y no pueden ser nulos.
// También tienen la misma sintaxis que la propia variable:
// No es necesaria * para eliminar la referencia y
// & (dirección) no se utiliza para la asignación.

using namespace std;

string foo = "Yo soy foo";
string bar = "Yo soy bar";

string& fooRef = foo; // Crea una referencia a foo.
fooRef += ". Hola!"; // Modifica foo través de la referencia
cout << fooRef; // Imprime "Yo soy foo. Hola!"

// No trate de reasignar "fooRef". Esto es lo mismo que "foo = bar", y
//   foo == "Yo soy bar"
// después de esta linea.
fooRef = bar;

const string& barRef = bar; // Crea una referencia constante a bar.
// Como en C, los valores constantes (y punteros y referencias) no pueden ser
// modificados.
barRef += ". Hola!"; // Error, referencia constante no puede ser modificada.

// Sidetrack: Antes de hablar más sobre referencias, hay que introducir un
// concepto llamado objeto temporal. Supongamos que tenemos el siguiente código:
string tempObjectFun() { ... }
string retVal = tempObjectFun();

// Lo que pasa en la segunda línea es en realidad:
// - Un objeto de cadena es retornado desde tempObjectFun
// - Una nueva cadena se construye con el objeto devuelto como argumento al
// constructor
// - El objeto devuelto es destruido
// El objeto devuelto se llama objeto temporal. Objetos temporales son
// creados cada vez que una función devuelve un objeto, y es destruido en el
// fin de la evaluación de la expresión que encierra (Bueno, esto es lo que la
// norma dice, pero los compiladores están autorizados a cambiar este
// comportamiento. Busca "return value optimization" para ver mas detalles).
// Así que en este código:
foo(bar(tempObjectFun()))

// Suponiendo que foo y bar existen, el objeto retornado de tempObjectFun es
// pasado al bar, y se destruye antes de llamar foo.

// Ahora, de vuelta a las referencias. La excepción a la regla "en el extremo
// de la expresión encerrada" es si un objeto temporal se une a una
// referencia constante, en cuyo caso su vida se extiende al ámbito actual:

void constReferenceTempObjectFun() {
  // ConstRef obtiene el objeto temporal, y es válido hasta el final de esta
  // función.
  const string& constRef = tempObjectFun();
  ...
}

// Otro tipo de referencia introducida en C ++ 11 es específicamente para
// objetos temporales. No se puede tener una variable de este tipo, pero tiene
// prioridad en resolución de sobrecarga:

void someFun(string& s) { ... }  // Referencia regular
void someFun(string&& s) { ... }  // Referencia a objeto temporal

string foo;
someFun(foo);  // Llama la función con referencia regular
someFun(tempObjectFun());  // Llama la versión con referencia temporal

// Por ejemplo, puedes ver estas dos versiones de constructores para
// std::basic_string:
basic_string(const basic_string& other);
basic_string(basic_string&& other);

// La idea es que si estamos construyendo una nueva cadena de un objeto temporal
// (que va a ser destruido pronto de todos modos), podemos tener un constructor
// mas eficiente que "rescata" partes de esa cadena temporal. Usted verá este
// Concepto denominado "movimiento semántico".

////////////////////////////////////////////
// Clases y programación orientada a objetos
////////////////////////////////////////////

// Primer ejemplo de clases
#include <iostream>

// Declara una clase.
// Las clases son usualmente declaradas en archivos de cabeceras (.h o .hpp)
class Dog {
    // Variables y funciones de la clase son privados por defecto.
    std::string name;
    int weight;

// Todos los miembros siguientes de este son públicos
// Hasta que se encuentre "private" o "protected".
// All members following this are public
// until "private:" or "protected:" is found.
public:

    // Constructor por defecto
    Dog();

	// Declaraciones de funciones de la clase (implementaciones a seguir)
    // Nota que usamos std::string aquí en lugar de colocar
    // using namespace std;
    // arriba.
    // Nunca ponga una declaración "using namespace" en un encabezado.
    void setName(const std::string& dogsName);

    void setWeight(int dogsWeight);
	// Funciones que no modifican el estado del objeto
	// Deben marcarse como const.
	// Esto le permite llamarlas si se envia una referencia constante al objeto.
	// También tenga en cuenta que las funciones deben ser declaradas
	// explícitamente como _virtual_ para que sea reemplazada en las clases
	// derivadas.
	// Las funciones no son virtuales por defecto por razones de rendimiento.
    virtual void print() const;

    // Las funciones también se pueden definir en el interior
    // del cuerpo de la clase.
	// Funciones definidas como tales están entre líneas automáticamente.
    void bark() const { std::cout << name << " barks!\n"; }

    // Junto a los constructores, C++ proporciona destructores.
	// Estos son llamados cuando un objeto se elimina o está fuera del ámbito.
	// Esto permite paradigmas potentes como RAII
	// (mira abajo)
	// El destructor debe ser virtual si una clase es dervada desde el;
	// Si no es virtual, entonces la clase derivada destructor
	// No será llamada si el objeto se destruye a través de una referencia de
	// la clase base o puntero.
    virtual ~Dog();



}; // Un punto y coma debe seguir la definición de clase.

// Las funciones de una clase son normalmente implementados en archivos .cpp.
Dog::Dog()
{
    std::cout << "Un perro ha sido construido\n";
}

// Objetos (tales como cadenas) deben ser pasados por referencia
// Si los estas modificando o referencia constante en caso contrario.
void Dog::setName(const std::string& dogsName)
{
    name = dogsName;
}

void Dog::setWeight(int dogsWeight)
{
    weight = dogsWeight;
}

// Nota que "virtual" sólo se necesita en la declaración, no en la definición.
void Dog::print() const
{
    std::cout << "El perro es " << name << " y pesa " << weight << "kg\n";
}

Dog::~Dog()
{
    std::cout << "Adiós " << name << "\n";
}

int main() {
    Dog myDog; // imprime "Un perro ha sido construido"
    myDog.setName("Barkley");
    myDog.setWeight(10);
    myDog.print(); // imprime "El perro es Barkley y pesa 10 kg"
    return 0;
} // imprime "Adiós Barkley"

// Herencia:

// Esta clase hereda todo lo público y protegido de la clase Dog
class OwnedDog : public Dog {

    void setOwner(const std::string& dogsOwner);

	// Reemplaza el comportamiento de la función de impresión
	// de todos los OwnedDogs. Mira
	// http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping
	// Para una introducción más general si no está familiarizado con el
	// polimorfismo de subtipo.
	// La palabra clave override es opcional, pero asegura que estás
	// reemplazando el método de una clase base.
    void print() const override;

private:
    std::string owner;
};

// Mientras tanto, en el archivo .cpp correspondiente:

void OwnedDog::setOwner(const std::string& dogsOwner)
{
    owner = dogsOwner;
}

void OwnedDog::print() const
{
    Dog::print(); // Llama a la función de impresión en la clase base Dog
    std::cout << "El perro es de " << owner << "\n";
    // Imprime "El perro es <name> y pesa <weight>"
    //         "El perro es de <owner>"
}

////////////////////////////////////////////
// Inicialización y sobrecarga de operadores
////////////////////////////////////////////

// En C ++ se puede sobrecargar el comportamiento
// de los operadores como +, -, *, /, etc.
// Esto se hace mediante la definición de una función que es llamada
// cada vez que se utiliza el operador.

#include <iostream>
using namespace std;

class Point {
public:
    // Las variables de la clase pueden dar valores por defecto de esta manera.
    double x = 0;
    double y = 0;

	// Define un constructor por defecto que no hace nada
    // pero inicializa el punto al valor por defecto (0, 0)
    Point() { };

    // The following syntax is known as an initialization list
    // and is the proper way to initialize class member values
    Point (double a, double b) :
        x(a),
        y(b)
    { /* No hace nada excepto inicializar los valores */ }

    // Sobrecarga el operador +
    Point operator+(const Point& rhs) const;

    // Sobrecarga el operador +=
    Point& operator+=(const Point& rhs);

    // También tendría sentido añadir los operadores - y -=,
   	// Pero vamos a omitirlos por razones de brevedad.
};

Point Point::operator+(const Point& rhs) const
{
    // Crea un nuevo punto que es la suma de este y rhs.
    return Point(x + rhs.x, y + rhs.y);
}

Point& Point::operator+=(const Point& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

int main () {
    Point up (0,1);
    Point right (1,0);
    // Llama al operador + de Point
    // Point llama la función + con right como parámetro
    Point result = up + right;
    // Prints "Result is upright (1,1)"
    cout << "Result is upright (" << result.x << ',' << result.y << ")\n";
    return 0;
}

/////////////////////////
// Plantillas (Templates)
/////////////////////////

// Las plantillas en C++ se utilizan sobre todo en la programación genérica,
// a pesar de que son mucho más poderoso que los constructores genéricos
// en otros lenguajes. Ellos también soportan especialización explícita y
// parcial y clases de tipo estilo funcional; de hecho, son un lenguaje
// funcional Turing-completo incrustado en C ++!

// Empezamos con el tipo de programación genérica que podría estar
// familiarizado.
// Para definir una clase o función que toma un parámetro de tipo:
template<class T>
class Box {
public:
    // En este caso, T puede ser usado como cualquier otro tipo.
    void insert(const T&) { ... }
};

// Durante la compilación, el compilador realmente genera copias de cada
// plantilla con parámetros sustituidos, por lo que la definición completa
// de la clase debe estar presente en cada invocación.
// Es por esto que usted verá clases de plantilla definidas
// Enteramente en archivos de cabecera.

//Para crear una instancia de una clase de plantilla en la pila:
Box<int> intBox;

y puedes utilizarlo como era de esperar:
intBox.insert(123);

// Puedes, por supuesto, anidar plantillas:
Box<Box<int> > boxOfBox;
boxOfBox.insert(intBox);

// Hasta C++11, había que colocar un espacio entre los dos '>'s,
// de lo contrario '>>' serían analizados como el operador de desplazamiento
// a la derecha.


// A veces verás
//   template<typename T>
// en su lugar. La palabra clave "class" y las palabras clave "typename" son
// mayormente intercambiables en este caso. Para la explicación completa, mira
//   http://en.wikipedia.org/wiki/Typename
// (sí, esa palabra clave tiene su propia página de Wikipedia).

// Del mismo modo, una plantilla de función:
template<class T>
void barkThreeTimes(const T& input)
{
    input.bark();
    input.bark();
    input.bark();
}

// Observe que no se especifica nada acerca de los tipos de parámetros aquí.
// El compilador generará y comprobará cada invocación de la plantilla,
// por lo que la función anterior funciona con cualquier tipo "T"
// que tenga un método 'bark' constante!


Dog fluffy;
fluffy.setName("Fluffy")
barkThreeTimes(fluffy); // Imprime "Fluffy barks" 3 veces.

Los parámetros de la plantilla no tienen que ser las clases:
template<int Y>
void printMessage() {
  cout << "Aprende C++ en " << Y << " minutos!" << endl;
}

// Y usted puede especializar explícitamente plantillas
// para código más eficiente.
// Por supuesto, la mayor parte del mundo real que utiliza una especialización
// no son tan triviales como esta.
// Tenga en cuenta que usted todavía tiene que declarar la función (o clase)
// como plantilla incluso si ha especificado de forma explícita todos
// los parámetros.

template<>
void printMessage<10>() {
  cout << "Aprende C++ rapido en solo 10 minutos!" << endl;
}

printMessage<20>();  // Prints "Aprende C++ en 20 minutos!"
printMessage<10>();  // Prints "Aprende C++ rapido en solo 10 minutos!"


/////////////////////
// Manejador de excepciones
/////////////////////

// La biblioteca estándar proporciona algunos tipos de excepción
// (mira http://en.cppreference.com/w/cpp/error/exception)
// pero cualquier tipo puede ser lanzado como una excepción
#include <exception>
#include <stdexcept>

//Todas las excepciones lanzadas dentro del bloque _try_ pueden ser
// capturados por los siguientes manejadores _catch_.
try {
    // No asignar excepciones en el heap usando _new_.
    throw std::runtime_error("Ocurrió un problema");
}

// Captura excepciones por referencia const si son objetos
catch (const std::exception& ex)
{
    std::cout << ex.what();
}
********************************************************************************
// Captura cualquier excepción no capturada por bloques _catch_ anteriores
catch (...)
{
    std::cout << "Excepción desconocida capturada";
    throw; // Re-lanza la excepción
}

///////
// RAII
///////

// RAII significa "Resource Acquisition Is Initialization"
// (Adquisición de recursos es inicialización).
// A menudo se considera el paradigma más poderoso en C++
// Y el concepto es simple: un constructor de un objeto
// Adquiere recursos de ese objeto y el destructor les libera.

// Para entender cómo esto es útil,
// Considere una función que utiliza un identificador de archivo C:
void doSomethingWithAFile(const char* filename)
{
    // Para empezar, asuma que nada puede fallar.

    FILE* fh = fopen(filename, "r"); // Abre el archivo en modo lectura

    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

    fclose(fh); // Cierra el manejador de archivos
}

// Por desgracia, las cosas se complican rápidamente por el control de errores.
// Supongamos que fopen puede fallar, y que doSomethingWithTheFile y
// DoSomethingElseWithIt retornan códigos de error si fallan.
// 	(Excepciones son la mejor forma de manejar los fallos,
// 	 pero algunos programadores, especialmente los que tienen un fondo C,
// 	 estan en desacuerdo sobre la utilidad de las excepciones).
// Ahora tenemos que comprobar cada llamado por fallos y cerrar el manejador
// del archivo si se ha producido un problema.
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Abre el archivo en modo lectura
    if (fh == nullptr) // El puntero retornado es nulo o falla.
        return false; // Reporta el fallo a quien hizo el llamado.

    // Asume que cada función retorna falso si falla
    if (!doSomethingWithTheFile(fh)) {
        fclose(fh); // Cierre el manejador de archivo para que no se filtre.
        return false; // Propaga el error.
    }
    if (!doSomethingElseWithIt(fh)) {
        fclose(fh); // Cierre el manejador de archivo para que no se filtre.
        return false; // Propaga el error.
    }

    fclose(fh); // Cierre el archivo.
    return true; // Indica que todo funcionó correctamente.
}

// Programadores C suelen limpiar esto un poco usando goto:
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r");
    if (fh == nullptr)
        return false;

    if (!doSomethingWithTheFile(fh))
        goto failure;

    if (!doSomethingElseWithIt(fh))
        goto failure;

    fclose(fh); // Cierre el archivo.
    return true; // Indica que todo funcionó correctamente.

failure:
    fclose(fh);
    return false; // Propagate el error
}

// Si las funciones indican errores mediante excepciones,
// Las cosas son un poco más claras, pero pueden optimizarse mas.
void doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Abrir el archivo en modo lectura
    if (fh == nullptr)
        throw std::runtime_error("No puede abrirse el archivo.");

    try {
        doSomethingWithTheFile(fh);
        doSomethingElseWithIt(fh);
    }
    catch (...) {
        fclose(fh); // Asegúrese de cerrar el archivo si se produce un error.
        throw; // Luego vuelve a lanzar la excepción.
    }

    fclose(fh); // Cierra el archivo
}

// Compare esto con el uso de la clase de flujo de archivos de C++ (fstream)
// fstream utiliza su destructor para cerrar el archivo.
// Los destructores son llamados automáticamente
// cuando un objeto queda fuera del ámbito.
void doSomethingWithAFile(const std::string& filename)
{
    // ifstream es la abreviatura de el input file stream
    std::ifstream fh(filename); // Abre el archivo

    // Hacer algo con el archivo
    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

} // El archivo se cierra automáticamente aquí por el destructor


// Esto tiene ventajas _enormes_:
// 1. No importa lo que pase,
//    El recurso (en este caso el manejador de archivo) será limpiado.
//    Una vez que escribes el destructor correctamente,
//    Es _imposible_ olvidar cerrar el identificador y permitir
//    fugas del recurso.
// 2. Tenga en cuenta que el código es mucho más limpio.
//    El destructor se encarga de cerrar el archivo detrás de cámaras
//    Sin que tenga que preocuparse por ello.
// 3. El código es seguro.
//    Una excepción puede ser lanzado en cualquier lugar de la función
//    y la limpieza ocurrirá.

// Todo el código idiomático C++ utiliza RAII ampliamente para todos los
// recursos.
// Otros ejemplos incluyen
// - Memoria usando unique_ptr y shared_ptr
// - Contenedores (Containers) - la biblioteca estándar linked list,
//   vector (es decir, array con auto-cambio de tamaño), hash maps, etc.
//   Destruimos todos sus contenidos de forma automática
//   cuando quedan fuera del ámbito.
// - Mutex utilizando lock_guard y unique_lock


/////////////////////
// Cosas divertidas
/////////////////////

// Aspectos de C ++ que pueden sorprender a los recién llegados
// (e incluso algunos veteranos).
// Esta sección es, por desgracia, salvajemente incompleta;
// C++ es uno de los lenguajes con los que mas facil te disparas en el pie.

// Tu puedes sobreescribir métodos privados!
class Foo {
  virtual void bar();
};
class FooSub : public Foo {
  virtual void bar();  // Sobreescribe Foo::bar!
};


// 0 == false == NULL (La mayoria de las veces)!
bool* pt = new bool;
*pt = 0; // Establece los puntos de valor de 'pt' en falso.
pt = 0;  // Establece 'pt' al apuntador nulo. Ambas lineas compilan sin error.

// nullptr se supone que arregla un poco de ese tema:
int* pt2 = new int;
*pt2 = nullptr; // No compila
pt2 = nullptr;  // Establece pt2 como null.

// Hay una excepción para los valores bool.
// Esto es para permitir poner a prueba punteros nulos con if (!ptr),
// pero como consecuencia se puede asignar nullptr a un bool directamente!
*pt = nullptr;  // Esto todavía compila, a pesar de que '*pt' es un bool!

// '=' != '=' != '='!
// Llama Foo::Foo(const Foo&) o alguna variante (mira movimientos semanticos)
// copia del constructor.
Foo f2;
Foo f1 = f2;

// Llama Foo::Foo(const Foo&) o variante, pero solo copia el 'Foo' parte de
// 'fooSub'. Cualquier miembro extra de 'fooSub' se descarta. Este
// comportamiento horrible se llama "Corte de objetos."
FooSub fooSub;
Foo f1 = fooSub;

// Llama a Foo::operator=(Foo&) o variantes.
Foo f1;
f1 = f2;


// Cómo borrar realmente un contenedor:
class Foo { ... };
vector<Foo> v;
for (int i = 0; i < 10; ++i)
  v.push_back(Foo());
// La siguiente línea establece el tamaño de v en 0,
// pero los destructores no son llamados y los recursos no se liberan!

v.empty();
v.push_back(Foo());  // Nuevo valor se copia en el primer Foo que insertamos

// En verdad destruye todos los valores en v.
// Consulta la sección acerca de los objetos temporales para la
// explicación de por qué esto funciona.
v.swap(vector<Foo>());

```
Otras lecturas:

Una referencia del lenguaje hasta a la fecha se puede encontrar en
<http://cppreference.com/w/cpp>

Recursos adicionales se pueden encontrar en <http://cplusplus.com>
---
language: c
filename: learnc-es.c
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Francisco García", "http://flaskbreaker.tumblr.com/"]
lang: es-es
---

¡Ah!, C. Aun hoy en día sigue siendo el lenguaje por excelencia de la 
computación moderna de alto rendimiento.

C es el lenguaje de más bajo nivel que la mayoría de los programadores
llegarán a usar, pero lo compensa de sobra con pura velocidad. Solo
ten en cuenta el manejo manual de memoria y te llevará tan lejos como
necesites.

```c
// Los comentarios de una sola línea comienzan con //

/*
Los comentarios multilínea tienen este aspecto.
*/

// Importa cabeceras con #include
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Declara por adelantado las armaduras de las funciones en un archivo .h,
// o al principio de tu archivo .c .
void function_1();
void function_2();

// El punto de entrada de tu programa es una función llamada main con
// retorno de tipo entero (integer).
int main() {

// Muestra la salida usando printf, para el "formato print"
// %d es un entero, \n es una nueva línea
printf("%d\n", 0); // => Muestra 0
// Todas las sentencias deben terminar con un punto y coma.

///////////////////////////////////////
// Tipos
///////////////////////////////////////

// Tienes que declarar una variable antes de usarla. La declaración de una
// variable necesites que especifiques su tipo; el tipo de una variable
// determina su tamaño en bytes.

// 'ints' (enteros) son normalmente de 4 bytes
int x_int = 0;

// 'shorts' son normalmente de 2 bytes
short x_short = 0;

// 'chars' son fijo de 1 byte
char x_char = 0;
char y_char = 'y'; // Los caracteres literales se entrecomillan con ''

// 'longs' son a menudo de 4 a 8 bytes; 'long longs' son fijo de por lo
// menos 64 bits
long x_long = 0;
long long x_long_long = 0; 

// 'floats' son normalmente números de coma flotante de 32 bits
float x_float = 0.0;

// 'doubles' son normalmente números de coma flotante de 64 bits
double x_double = 0.0;

// Todos los tipos enteros pueden ser 'unsigned'. Esto significa que no
// pueden ser negativos, pero el valor máximo de una variable 'unsigned'
// es mayor que el de una no 'unsigned' del mismo tamaño.
unsigned char ux_char;
unsigned short ux_short;
unsigned int ux_int;
unsigned long long ux_long_long;

// Todos menos 'char', que es siempre de 1 byte, varían el tamaño 
// dependiendo de tu máquina. sizeof(T) te dice el tamaño de una variable
// de tipo T en bytes por lo que podemos expresar el tamaño de estos tipos
// portatilmente.
// Por ejemplo,
printf("%lu\n", sizeof(int)); // => 4 (en máquinas con 'words' de 4 bytes)

// Los arrays deben ser inicializados con un tamaño concreto.
char my_char_array[20]; // Este array ocupa 1 * 20 = 20 bytes
int my_int_array[20]; // Este array ocupa 4 * 20 = 80 bytes
                      // (suponiendo que tenemos 'words' de 4-byte)


// Puedes inicializar un array a 0 así:
char my_array[20] = {0};

// Indexar un array es como en otros lenguajes -o, más bien, otros
// lenguajes son como C-
my_array[0]; // => 0

// Los arrays varían; ¡son sólo memoria!
my_array[1] = 2;
printf("%d\n", my_array[1]); // => 2

// Las cadenas (strings) son sólo arrays de 'chars' (caracteres)
// terminados en un byte NUL (0x00), representado en las cadenas como el
// carácter especial '\0'.
// (No tenemos porqué añadir el byte nulo en cadenas literales; el
// compilador lo añade al final por nosotros.)
char a_string[20] = "Esto es una cadena";
printf("%s\n", a_string); // %s se sutituye por una cadena.

/*
Te habrás dado cuenta de que a_string es solo de 18 caracteres.
El 'char' #19 es el byte nulo. 
El 'char' #20 es de valor indefinido.
*/

printf("%d\n", a_string[18]); // => 0

///////////////////////////////////////
// Operadores
///////////////////////////////////////

int i1 = 1, i2 = 2; // Forma corta de declaración múltiple
float f1 = 1.0, f2 = 2.0;

// La aritmética es sencilla
i1 + i2; // => 3
i2 - i1; // => 1
i2 * i1; // => 2
i1 / i2; // => 0 (0.5, pero es truncado tras el 0)

f1 / f2; // => 0.5, más o menos épsilon
// Módulo está también
11 % 3; // => 2

// Los operadores de comparación te resultaran familiares, pero no hay
// booleanos en C. Usamos enteros (ints) en su lugar. 0 es falso,
// cualquier otra cosa es verdadero. (Los operadores de comparación 
// siempre devuelven 0 o 1)
3 == 2; // => 0 (Falso)
3 != 2; // => 1 (Verdadero)
3 > 2; // => 1
3 < 2; // => 0
2 <= 2; // => 1
2 >= 2; // => 1

// La lógica funiona en enteros
!3; // => 0 (not lógico)
!0; // => 1
1 && 1; // => 1 (and lógico)
0 && 1; // => 0
0 || 1; // => 1 (or lógico)
0 || 0; // => 0

// ¡Operadores de bits!
~0x0F; // => 0xF0 (Negación)
0x0F & 0xF0; // => 0x00 (AND)
0x0F | 0xF0; // => 0xFF (OR)
0x04 ^ 0x0F; // => 0x0B (XOR)
0x01 << 1; // => 0x02 (desplazar hacia la izquierda (por 1))
0x02 >> 1; // => 0x01 (desplazar hacia la derecha (por 1))

///////////////////////////////////////
// Estructuras de Control
///////////////////////////////////////

if (0) {
  printf("Yo nunca ocurro\n");
} else if (0) {
  printf("Yo tampoco ocurro nunca\n");
} else {
  printf("Yo me muestro\n");
}

// Mientras el bucle exista
int ii = 0;
while (ii < 10) {
    printf("%d, ", ii++); // ii++ incrementa ii en uno, después de usar su valor.
} // => muestra "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

printf("\n");

int kk = 0;
do {
    printf("%d, ", kk);
} while (++kk < 10); // ++kk incrementa kk en uno, antes de usar su valor.
// => muestra "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

printf("\n");

// Bucles 'for' también
int jj;
for (jj=0; jj < 10; jj++) {
    printf("%d, ", jj);
} // => muestra "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

printf("\n");

///////////////////////////////////////
// Cambios de Tipo
///////////////////////////////////////

// Cada valor en C tiene un tipo,  pero tu puedes ingresar un valor en
// otro tipo si quieres.

int x_hex = 0x01; // Puedes asignar hexadecimales a variables

// El cambio de tipos intentará mantener sus valores numéricos
printf("%d\n", x_hex); // => Muestra 1
printf("%d\n", (short) x_hex); // => Muestra 1
printf("%d\n", (char) x_hex); // => Muestra 1

// Los tipos se desbordan sin aviso
printf("%d\n", (char) 257); // => 1 (El valor máximo de un 'char' es 255)

// Los tipos enteros puden cambiarse a tipos de coma flotante, y viceversa
printf("%f\n", (float)100); // %f se sustituye por un 'float'
printf("%lf\n", (double)100); // %lf se sustituye por un 'double'
printf("%d\n", (char)100.0);

///////////////////////////////////////
// Punteros
///////////////////////////////////////

// Un puntero es una variable declarada para almacenar una dirección de 
// memoria. Su declaración además nos dirá el tipo de dato al que apunta. 
// Puedes obtener la dirección de memoria de tus variables, y después
// enlazarlas con ellos.

int x = 0;
printf("%p\n", &x); // Usa & para obtener la dirección de una variable.
// (%p se sustituye por un puntero)
// => Muestra alguna dirección de memoria;

// Los tipos de puntero terminan con * en su declaración
int* px; // px es un puntero a un 'int'
px = &x; // Almacena la dirección de x en px
printf("%p\n", px); // => Muestra alguna dirección de memoria

// Para obtener el valor de la dirección a la que apunta un puntero, pon
// * delante para desreferenciarle. 
printf("%d\n", *px); // => Muestra 0, el valor de x y de la dirección a la
                     //    que apunta px

// También puedes cambiar el valor al que está apuntando el puntero.
// Tenemos que meter la desreferencia entre paréntesis porque ++ tiene
// prioridad frente a *.
(*px)++; // Incrementa el valor al que apunta px en 1
printf("%d\n", *px); // => Muestra 1
printf("%d\n", x); // => Muestra 1

int x_array[20]; // Los arrays son una buena manera de distribuir bloques
int xx;          // continuos de memoria.
for (xx=0; xx<20; xx++) {
    x_array[xx] = 20 - xx;
} // Inicializa x_array a 20, 19, 18,... 2, 1

// Declara un puntero de tipo 'int' y lo inicializa para apuntar a x_array
int* x_ptr = x_array;
// x_ptr ahira apunta al primer elemento del 'array' (el entero 20).
// Esto funciona porque las 'arrays' actualmente son solo punteros a su
// primer elemento.

// Los 'arrays' son punteros a su primer elemento.
printf("%d\n", *(x_ptr)); // => Muestra 20
printf("%d\n", x_array[0]); // => Muestra 20

// Los punteros aumentan y disminuyen en función de su tipo.
printf("%d\n", *(x_ptr + 1)); // => Muestra 19
printf("%d\n", x_array[1]); // => Muestra 19

// Puedes también asigner dinamicamente bloques contiguos de memoria con
// la función malloc de la librería estándard, que toma un entero como
// argumento representando el número de bytes a asignar de la pila.
int* my_ptr = (int*) malloc(sizeof(int) * 20);
for (xx=0; xx<20; xx++) {
    *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx funcionaría también aquí
} // Inicializa la memoria a 20, 19, 18, 17... 2, 1 (como 'ints')

// Desreferenciando la memoria que no has asignado te dará resultados
// impredecibles
printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what?

// Cuando hayas acabado con el bloque de memoría malloc, necesitas 
// liberarlo o sino nadie más podrá usarlo hasta que tu programa se cierre
free(my_ptr);

// Las cadenas pueden ser 'arrays' de chars, pero normalmente se
// representan con punteros 'char':
char* my_str = "This is my very own string";

printf("%c\n", *my_str); // => 'T'

function_1();
} // fin de la función main

///////////////////////////////////////
// Funciones
///////////////////////////////////////

// Sintexis de la declaración de funciones:
// <tipo de retorno> <nombre>(<argumentos>)

int add_two_ints(int x1, int x2){
    return x1 + x2; // Usa 'return' para dar una salida
}

/*
Las funciones son de paso por valor, pero puedes hacer tus propias 
referencias con punteros de manera que las funciones puedan cambiar sus 
valores.

Ejemplo: invertidor de cadenas in-situ
*/

// Una función 'void' no retorna valor
void str_reverse(char* str_in){
    char tmp;
    int ii=0, len = strlen(str_in); // Strlen es parte de la librería 
    for(ii=0; ii<len/2; ii++){      // estándard
        tmp = str_in[ii];
        str_in[ii] = str_in[len - ii - 1]; // ii-th último 'char'
        str_in[len - ii - 1] = tmp;
    }
}

/*
char c[] = "Esto es una prueba.";
str_reverse(c);
printf("%s\n", c); // => ".abeurp anu se otsE"
*/

///////////////////////////////////////
// Definición de tipos y estructuras
///////////////////////////////////////

// Los 'Typedefs' pueden ser utilizados para crear alias de tipos.
typedef int my_type;
my_type my_type_var = 0;

// Las estructuras son sólo grupos de datos.
struct rectangle {
    int width;
    int height;
};


void function_1(){

    struct rectangle my_rec;

    // Utiliza los miembros de una estructura con .
    my_rec.width = 10;
    my_rec.height = 20;

    // Puedes declarar punteros a estructuras
    struct rectangle* my_rec_ptr = &my_rec;

    // Usa la desreferencia para modificar sus miembros...
    (*my_rec_ptr).width = 30;

    // ... o usa la abreviatura ->
    my_rec_ptr->height = 10; // Lo mismo que (*my_rec_ptr).height = 10;
}

// Puedes aplicar un 'typedef' a una estructura por conveniencía.
typedef struct rectangle rect;

int area(rect r){
    return r.width * r.height;
}

///////////////////////////////////////
// Punteros a Funciones
///////////////////////////////////////
/*
En tiempo de ejecución,  las funciones se localizan en unas direcciones de
memoria concretas. Los punteros a funciones son como cualquier otro 
puntero (almacenan una dirección de memoria), pero pueden ser usados para 
utilizar funciones directamente, o para pasar 'handlers' (o funciones 
'callback') por todos lados.
Sin embargo, la sintaxis de definición parecera confusa al principio.

Ejemplo: usar str_reverse desde un puntero
*/
void str_reverse_through_pointer(char * str_in) {
    // Define un puntero a una función, llamado f.
    void (*f)(char *);
    // La armadura debe coincidir exactamente con al función objetivo.

    // Assigna la dirección de la función (determinado en tiempo de ejecuión)
    f = &str_reverse;

    // Llamando la función desde el puntero
    (*f)(str_in);

    // Esta es una alternativa para llamarla pero con una sintaxis igual de válida.
    // f(str_in);
}

/*
Tanto tiempo como las armaduras de las funciones coincidan, podrás asignar
cualquier función al mismo puntero.
Los punteros a funciones  son normalmente envueltos en 'typedef' para
simplificar su legibilidad, como sigue:
*/

typedef void (*my_fnp_type)(char *);

// Es usado para declarar la variable puntero actual:
// ...
// my_fnp_type f; 

```

## Otras lecturas

Lo mejor que puedes encontrar es una copia de [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language). Es *el*
libro de C, escrito por Dennis Ritchie, creador de C y Brian Kernighan. Aún así,
se cuidadoso, es antiguo, contiene algunas inexactitudes, y algunas prácticas 
han cambiado.

Otro buen recurso es [Learn C the hard way](http://c.learncodethehardway.org/book/).

Si tienes una pregunta, lee [compl.lang.c Frequently Asked Questions](http://c-faq.com).

Es muy importante utilizar el espaciado y la sangría apropiados y ser coherente 
con su estilo de codificación en general. El código legible es mejor que el 
código rápido. Para adoptar un buen estilo de codificación, vea el
[Estilo de codificación del kernel Linux] (https://www.kernel.org/doc/Documentation/CodingStyle).

Aparte de eso, Google es tu amigo.
---
language: clojure
filename: learnclojure-es.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Antonio Hernández Blas", "https://twitter.com/nihilipster"]
    - ["Guillermo Vayá Pérez", "http://willyfrog.es"]
lang: es-es
---

Clojure es un lenguaje de la familia Lisp desarrollado sobre la Máquina Virtual
de Java. Tiene un énfasis mayor en la [programación funcional](https://es.wikipedia.org/wiki/Programación_funcional) pura
que Common Lisp, pero incluyendo la posibilidad de usar [SMT](https://es.wikipedia.org/wiki/Memoria_transacional) para manipular
el estado según se presente.

Esta combinación le permite gestionar la concurrencia de manera muy sencilla
y a menudo automáticamente.

(Necesitas la versión de Clojure 1.2 o posterior)


```clojure
; Los comentatios comienzan con punto y coma.

; Clojure se escribe mediante "forms" (patrones), los cuales son
; listas de objectos entre paréntesis, separados por espacios en blanco.

; El "reader" (lector) de Clojure asume que el primer objeto es una
; función o una macro que se va a llamar, y que el resto son argumentos.

; El primer form en un archivo debe ser ns, para establecer el namespace (espacio de
; nombres)
(ns learnclojure)

; Algunos ejemplos básicos:

; str crea una cadena de caracteres a partir de sus argumentos
(str "Hello" " " "World") ; => "Hello World"

; Las operaciones matemáticas son sencillas
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; La igualdad es =
(= 1 1) ; => true
(= 2 1) ; => false

; También es necesaria la negación para las operaciones lógicas
(not true) ; => false

; Cuando se anidan Los patrones, estos funcionan de la manera esperada
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; Tipos
;;;;;;;;;;;;;

; Clojure usa los tipos de objetos de Java para booleanos, strings (cadenas de
; caracteres) y números.
; Usa class para saber de qué tipo es.
(class 1); Los enteros son java.lang.Long por defecto
(class 1.); Los numeros en coma flotante son java.lang.Double
(class ""); Los strings van entre comillas dobles, y son
; son java.lang.String
(class false); Los Booleanos son java.lang.Boolean
(class nil); El valor "null" se escribe nil

; Si quieres crear una lista de datos, precedela con una comilla
; simple para evitar su evaluación
'(+ 1 2) ; => (+ 1 2)
; (que es una abreviatura de (quote (+ 1 2)) )

; Puedes evaluar una lista precedida por comilla  con eval
(eval '(+ 1 2)) ; => 3

; Colecciones & Secuencias
;;;;;;;;;;;;;;;;;;;

; Las Listas están basadas en las listas enlazadas, mientras que los Vectores en
; arrays.
; ¡Los Vectores y las Listas también son clases de Java!
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; Una lista podría ser escrita como (1 2 3), pero debemos ponerle una
; comilla simple delante para evitar que el reader piense que es una función.
; Además, (list 1 2 3) es lo mismo que '(1 2 3)

; Las "Colecciones" son solo grupos de datos
; Tanto las listas como los vectores son colecciones:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; Las "Secuencias" (seqs) son descripciones abstractas de listas de datos.
; Solo las listas son seqs.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; Una seq solo necesita proporcionar una entrada cuando es accedida.
; Así que, las seqs pueden ser perezosas -- pueden establecer series infinitas:
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (una serie infinita)
(take 4 (range)) ;  (0 1 2 3)

; Usa cons para agregar un elemento al inicio de una lista o vector
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; conj agregará un elemento a una colección en la forma más eficiente.
; Para listas, se añade al inicio. Para vectores, al final.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; Usa concat para concatenar listas o vectores
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; Usa filter y map para actuar sobre colecciones
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; Usa reduce para combinar sus elementos
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; reduce puede tener un argumento indicando su valor inicial.
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; Funciones
;;;;;;;;;;;;;;;;;;;;;

; Usa fn para crear nuevas funciones. Una función siempre devuelve
; su última expresión
(fn [] "Hello World") ; => fn

; (Necesitas rodearlo con paréntesis para invocarla)
((fn [] "Hello World")) ; => "Hello World"

; Puedes crear una var (variable) mediante def
(def x 1)
x ; => 1

; Asigna una función a una var
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; Puedes defn como atajo para lo anterior
(defn hello-world [] "Hello World")

; El [] es el vector de argumentos de la función.
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; Otra abreviatura para crear funciones es:
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; Puedes tener funciones multi-variadic: funciones con un numero variable de
; argumentos
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; Las funciones pueden usar argumentos extras dentro de un seq utilizable en la función
(defn count-args [& args]
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"

; Y puedes mezclarlos con el resto de argumentos declarados de la función.
(defn hello-count [name & args]
  (str "Hello " name ", you passed " (count args) " extra args"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"


; Mapas
;;;;;;;;;;

; Mapas de Hash y mapas de arrays comparten una misma interfaz. Los mapas de Hash
; tienen búsquedas más rápidas pero no mantienen el orden de las claves.
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Los mapas de arrays se convertidos en mapas de Hash en la mayoría de
; operaciones si crecen mucho, por lo que no debes preocuparte.

; Los mapas pueden usar cualquier tipo para sus claves, pero generalmente las
; keywords (palabras clave) son lo habitual.
; Las keywords son parecidas a cadenas de caracteres con algunas ventajas de eficiencia
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; Por cierto, las comas son equivalentes a espacios en blanco y no hacen
; nada.

; Recupera un valor de un mapa tratandolo como una función
(stringmap "a") ; => 1
(keymap :a) ; => 1

; ¡Las keywords pueden ser usadas para recuperar su valor del mapa, también!
(:b keymap) ; => 2

; No lo intentes con strings.
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; Si preguntamos por una clave que no existe nos devuelve nil
(stringmap "d") ; => nil

; Usa assoc para añadir nuevas claves a los mapas de Hash
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; Pero recuerda, ¡los tipos de Clojure son inmutables!
keymap ; => {:a 1, :b 2, :c 3}

; Usa dissoc para eliminar llaves
(dissoc keymap :a :b) ; => {:c 3}

; Conjuntos
;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; Añade un elemento con conj
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; Elimina elementos con disj
(disj #{1 2 3} 1) ; => #{2 3}

; Comprueba su existencia usando el conjunto como una función:
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; Hay más funciones en el namespace clojure.sets

; Patrones útiles
;;;;;;;;;;;;;;;;;

; Las construcciones lógicas en clojure son macros, y presentan el mismo aspecto
; que el resto de forms.
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; Usa let para crear un binding (asociación) temporal
(let [a 1 b 2]
  (> a b)) ; => false

; Agrupa expresiones mediante do
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; Las funciones tienen implicita la llamada a do
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; Y el let también
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")

; Módulos
;;;;;;;;;;;;;;;

; Usa use para obtener todas las funciones del módulo
(use 'clojure.set)

; Ahora podemos usar más operaciones de conjuntos
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; Puedes escoger un subgrupo de funciones a importar, también
(use '[clojure.set :only [intersection]])

; Usa require para importar un módulo
(require 'clojure.string)

; Usa / para llamar a las funciones de un módulo
; Aquí, el módulo es clojure.string y la función es blank?
(clojure.string/blank? "") ; => true

; Puedes asignarle una abreviatura a un modulo al importarlo
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (#"" es una expresión regular)

; Puedes usar require (y use, pero no lo hagas) desde un espacio de nombre
; usando :require,
; No necesitas preceder con comilla simple tus módulos si lo haces de esta
; forma.
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java tiene una enorme librería estándar, por lo que resulta util
; aprender como interactuar con ella.

; Usa import para cargar un módulo de java
(import java.util.Date)

; Puedes importar desde un ns también.
(ns test
  (:import java.util.Date
           java.util.Calendar))

; Usa el nombre de la clase con un "." al final para crear una nueva instancia
(Date.) ; <un objeto Date>

; Usa "." para llamar a métodos o usa el atajo ".método"
(. (Date.) getTime) ; <un timestamp>
(.getTime (Date.)) ; exactamente la misma cosa

; Usa / para llamar métodos estáticos.
(System/currentTimeMillis) ; <un timestamp> (System siempre está presente)

; Usa doto para hacer frente al uso de clases (mutables) más tolerable
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => A Date. set to 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; Software Transactional Memory es un mecanismo que usa clojure para gestionar
; el estado persistente. Hay unas cuantas construcciones en clojure que
; hacen uso de este mecanismo.

; Un atom es el más sencillo. Se le da un valor inicial
(def my-atom (atom {}))

; Actualiza un atom con swap!
; swap! toma una función y la llama con el valor actual del atom
; como su primer argumento, y cualquier argumento restante como el segundo
(swap! my-atom assoc :a 1) ; Establece my-atom al resultado de (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Establece my-atom al resultado de (assoc {:a 1} :b 2)

; Usa '@' para no referenciar al atom sino para obtener su valor
my-atom  ;=> Atom<#...> (Regresa el objeto Atom)
@my-atom ; => {:a 1 :b 2}

; Un sencillo contador usando un atom sería
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; Otros forms que utilizan STM son refs y agents.
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
### Lectura adicional

Ésto queda lejos de ser exhaustivo, pero espero que sea suficiente para que puedas empezar tu camino.

Clojure.org tiene muchos artículos:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org contiene documentación con ejemplos para la mayoría de
funciones principales (pertenecientes al core):
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure es una genial forma de mejorar tus habilidades con clojure/FP:
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org (sí, de verdad) tiene un buen número de artículos con los que iniciarse en Clojure:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: coffeescript
lang: es-es
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
translators:
  - ["Pablo Elices", "http://github.com/pabloelices"]
filename: coffeescript-es.coffee
---

``` coffeescript
# CoffeeScript es un lenguaje hipster.
# Tiene convenciones de muchos lenguajes modernos.
# Los comentarios son como en Ruby y Python, usan almohadillas.

###
Los comentarios en bloque son como estos, y se traducen directamente a '/*' y '*/'
para el código JavaScript resultante.

Deberías entender la mayor parte de la semántica de JavaScript antes de continuar.
###

# Asignación:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# Condiciones:
number = -42 if opposite #=> if(opposite) { number = -42; }

# Funciones:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

# Rangos:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# Objetos:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#  "root": Math.sqrt,
#  "square": square,
#  "cube": function(x) { return x * square(x); }
#}

# Número de argumentos variable:
race = (winner, runners...) ->
  print winner, runners

# Existencia:
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# Listas:
cubes = (math.cube num for num in list) #=> ...
```
---
language: C#(C Sharp)
filename: LearnCSharp-es.cs
contributors:
    - ["Irfan Charania", "https://github.com/irfancharania"]
    - ["Max Yankov", "https://github.com/golergka"]
translators:
    - ["Olfran Jiménez", "https://twitter.com/neslux"]	
lang: es-es

---

C# es un lenguaje orientado a objetos elegante y de tipado seguro que
permite a los desarrolladores construir una variedad de aplicaciones
seguras y robustas que se ejecutan en el Framework .NET.

[Lee más aquí.](http://msdn.microsoft.com/es-es/library/vstudio/z1zx9t92.aspx)

```c#
// Los comentarios de una sola línea comienzan con //
/*
Los comentarios de múltiples líneas son de esta manera
*/
/// <summary>
/// Este es un comentario de documentación XML
/// </summary>

// Especifica el espacio de nombres que estará usando la aplicación
using System;
using System.Collections.Generic;


// Define un ambito para organizar el código en "paquetes"
namespace Learning
{
	// Cada archivo .cs debe contener al menos una clase con el mismo nombre que el archivo
    // Se permite colocar cualquier nombre, pero no deberías por cuestiones de consistencia.
    public class LearnCSharp
    {
		// Una aplicación de consola debe tener un método main como punto de entrada
        public static void Main(string[] args)
        {
			// Usa Console.WriteLine para imprimir líneas
            Console.WriteLine("Hello World");
            Console.WriteLine(
                "Integer: " + 10 +
                " Double: " + 3.14 +
                " Boolean: " + true);

			// Para imprimir sin una nueva línea, usa Console.Write
            Console.Write("Hello ");
            Console.Write("World");


            ///////////////////////////////////////////////////
            // Variables y Tipos
            //
            // Declara una variable usando <tipo> <nombre>
            ///////////////////////////////////////////////////

            // Sbyte - Entero de 8 bits con signo
            // (-128 <= sbyte <= 127)
            sbyte fooSbyte = 100;

            // Byte - Entero de 8 bits sin signo
            // (0 <= byte <= 255)
            byte fooByte = 100;

            // Short - Entero de 16 bits con signo
            // (-32,768 <= short <= 32,767)
            short fooShort = 10000;

            // Ushort - Entero de 16 bits sin signo
            // (0 <= ushort <= 65,535)
            ushort fooUshort = 10000;

            // Integer - Entero de 32 bits con signo
            // (-2,147,483,648 <= int <= 2,147,483,647)
            int fooInt = 1;

            // Uinteger - Entero de 32 bits sin signo
            // (0 <= uint <= 4,294,967,295)
            uint fooUint = 1;

            // Long - Entero de 64 bits con signo
            // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
            long fooLong = 100000L;
            // L es usado para indicar que esta variable es de tipo long o ulong
            // un valor sin este sufijo es tratado como int o uint dependiendo del tamaño.

            // Ulong - Entero de 64 bits sin signo
            // (0 <= ulong <= 18,446,744,073,709,551,615)
            ulong fooUlong = 100000L;

            // Float - Precisión simple de 32 bits. IEEE 754 Coma flotante
            // Precisión: 7 dígitos
            float fooFloat = 234.5f;
			// f es usado para indicar que el valor de esta variable es de tipo float
            // de otra manera sería tratado como si fuera de tipo double.

            // Double - Doble precisión de 32 bits. IEEE 754 Coma flotante
            // Precisión: 15-16 dígitos
            double fooDouble = 123.4;

            // Bool - true & false (verdadero y falso)
            bool fooBoolean = true;
            bool barBoolean = false;

			// Char - Un solo caracter Unicode de 16 bits
            char fooChar = 'A';

            // Strings
            string fooString = "My string is here!";
            Console.WriteLine(fooString);

            // Formato de cadenas
            string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);
            Console.WriteLine(fooFormattedString);

            // Formato de fechas
            DateTime fooDate = DateTime.Now;
            Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));

			// \n es un caracter de escape que comienza una nueva línea
            string barString = "Printing on a new line?\nNo Problem!";
            Console.WriteLine(barString);

            // Puede ser escrito mejor usando el símbolo @
            string bazString = @"Here's some stuff
    on a new line!";
            Console.WriteLine(bazString);

            // Las comillas deben ser escapadas
            // usa \" para escaparlas
            string quotedString = "some \"quoted\" stuff";
            Console.WriteLine(quotedString);

			// usa "" cuando las cadenas comiencen con @
            string quotedString2 = @"some MORE ""quoted"" stuff";
            Console.WriteLine(quotedString2);

			// Usa const o readonly para hacer las variables inmutables
			// los valores const son calculados en tiempo de compilación
            const int HOURS_I_WORK_PER_WEEK = 9001;

            // Tipos que aceptan valores NULL (Nullable)
			// cualquier tipo de dato puede ser un tipo nulo añadiendole el sufijo ?
            // <tipo>? <variable> = <valor>
            int? nullable = null;
            Console.WriteLine("Nullable variable: " + nullable);

			// Para usar valores nulos, tienes que usar la propiedad Value
			// o usar conversión explícita
            string? nullableString = "not null";
            Console.WriteLine("Nullable value is: " + nullableString.Value + " or: " + (string) nullableString );

			// ?? is una manera corta de especificar valores por defecto
            // en caso de que la variable sea null
            int notNullable = nullable ?? 0;
            Console.WriteLine("Not nullable variable: " + notNullable);

			// var - el compilador escogerá el tipo de dato más apropiado basado en el valor
            var fooImplicit = true;

            ///////////////////////////////////////////////////
            // Estructura de datos
            ///////////////////////////////////////////////////
            Console.WriteLine("\n->Data Structures");

            // Arreglos
			// El tamaño del arreglo debe decidirse al momento de la declaración
            // El formato para declarar un arreglo es el siguiente:
            // <tipo_de_dato>[] <nombre_variable> = new <tipo_de_dato>[<tamaño>];
            int[] intArray = new int[10];
            string[] stringArray = new string[1];
            bool[] boolArray = new bool[100];

			// Otra forma de declarar e inicializar un arreglo
            int[] y = { 9000, 1000, 1337 };

			// Indexar arreglos - Acceder a un elemento
            Console.WriteLine("intArray @ 0: " + intArray[0]);

			// Los arreglos son de índice cero y son mutables.
            intArray[1] = 1;
            Console.WriteLine("intArray @ 1: " + intArray[1]); // => 1

            // Listas
			// Las listas son usadas más frecuentemente que los arreglos ya que son más flexibles
			// El formato para declarar una lista es el siguiente:
            // List<tipo_de_dato> <nombre_variable> = new List<tipo_de_dato>();
            List<int> intList = new List<int>();
            List<string> stringList = new List<string>();

            // Otra forma de declarar e inicializar una lista
            List<int> z = new List<int> { 9000, 1000, 1337 };

            // Indexar una lista - Acceder a un elemento
            // Las listas son de índice cero y son mutables.
            Console.WriteLine("z @ 0: " + z[2]);

            // Las listas no tienen valores por defecto;
            // Un valor debe ser añadido antes de acceder al índice
            intList.Add(1);
            Console.WriteLine("intList @ 0: " + intList[0]);


            // Otras estructuras de datos a chequear:
            //
            // Pilas/Colas
            // Diccionarios
            // Colecciones de sólo lectura
            // Tuplas (.Net 4+)


            ///////////////////////////////////////
            // Operadores
            ///////////////////////////////////////
            Console.WriteLine("\n->Operators");

            int i1 = 1, i2 = 2; // Modo corto para múltiples declaraciones

            // La aritmética es sencilla
            Console.WriteLine("1+2 = " + (i1 + i2)); // => 3
            Console.WriteLine("2-1 = " + (i2 - i1)); // => 1
            Console.WriteLine("2*1 = " + (i2 * i1)); // => 2
            Console.WriteLine("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down)

            // Módulo
            Console.WriteLine("11%3 = " + (11 % 3)); // => 2

            // Operadores de comparación
            Console.WriteLine("3 == 2? " + (3 == 2)); // => false
            Console.WriteLine("3 != 2? " + (3 != 2)); // => true
            Console.WriteLine("3 > 2? " + (3 > 2)); // => true
            Console.WriteLine("3 < 2? " + (3 < 2)); // => false
            Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true
            Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true

            // Operadores a nivel de bits
            /*
            ~       Complemento a nivel de bits
            <<      Desplazamiento a la izquierda con signo
            >>      Desplazamiento a la derecha con signo
            >>>     Desplazamiento a la derecha sin signo
            &       AND a nivel de bits
            ^       XOR a nivel de bits
            |       OR a nivel de bits
            */

            // Incremento
            int i = 0;
            Console.WriteLine("\n->Inc/Dec-remento");
            Console.WriteLine(i++); //i = 1. Posincrementación
            Console.WriteLine(++i); //i = 2. Preincremento
            Console.WriteLine(i--); //i = 1. Posdecremento
            Console.WriteLine(--i); //i = 0. Predecremento


            ///////////////////////////////////////
            // Estructuras de control
            ///////////////////////////////////////
            Console.WriteLine("\n->Control Structures");

            // Las condiciones if son como en lenguaje c
            int j = 10;
            if (j == 10)
            {
                Console.WriteLine("I get printed");
            }
            else if (j > 10)
            {
                Console.WriteLine("I don't");
            }
            else
            {
                Console.WriteLine("I also don't");
            }

            // Operador ternario
			// Un simple if/else puede ser escrito de la siguiente manera;
            // <condición> ? <true> : <false>
            string isTrue = (true) ? "True" : "False";
            Console.WriteLine("Ternary demo: " + isTrue);


            // Bucle while
            int fooWhile = 0;
            while (fooWhile < 100)
            {
                //Console.WriteLine(fooWhile);
                //Incrementar el contador
                //Iterar 99 veces, fooWhile 0->99
                fooWhile++;
            }
            Console.WriteLine("fooWhile Value: " + fooWhile);

            // Bucle Do While
            int fooDoWhile = 0;
            do
            {
                //Console.WriteLine(fooDoWhile);
                //Incrementar el contador
                //Iterar 99 veces, fooDoWhile 0->99
                fooDoWhile++;
            } while (fooDoWhile < 100);
            Console.WriteLine("fooDoWhile Value: " + fooDoWhile);

            // Bucle For
            int fooFor;
            //Estructura del bucle for => for(<declaración_inicial>; <condición>; <incremento>)
            for (fooFor = 0; fooFor < 10; fooFor++)
            {
                //Console.WriteLine(fooFor);
                //Iterated 10 times, fooFor 0->9
            }
            Console.WriteLine("fooFor Value: " + fooFor);

            // Switch Case
			// El switch funciona con los tipos de datos byte, short, char e int
            // También funciona con las enumeraciones (discutidos en in Tipos Enum),
            // la clase string y algunas clases especiales que encapsulan
            // tipos primitivos: Character, Byte, Short, Integer.
            int month = 3;
            string monthString;
            switch (month)
            {
                case 1:
                    monthString = "January";
                    break;
                case 2:
                    monthString = "February";
                    break;
                case 3:
                    monthString = "March";
                    break;
                default:
                    monthString = "Some other month";
                    break;
            }
            Console.WriteLine("Switch Case Result: " + monthString);


            ////////////////////////////////
            // Conversión de tipos de datos
            ////////////////////////////////

            // Convertir datos

            // Convertir String a Integer
            // esto generará una excepción al fallar la conversión
            int.Parse("123");//retorna una versión entera de "123"

            // TryParse establece la variable a un tipo por defecto
            // en este caso: 0
            int tryInt;
            int.TryParse("123", out tryInt);

            // Convertir Integer a String
            // La clase Convert tiene algunos métodos para facilitar las conversiones
            Convert.ToString(123);

            ///////////////////////////////////////
            // Clases y Funciones
            ///////////////////////////////////////

            Console.WriteLine("\n->Classes & Functions");

            // (Definición de la clase Bicycle (Bicicleta))

            // Usar new para instanciar una clase
            Bicycle trek = new Bicycle();

            // Llamar a los métodos del objeto
            trek.speedUp(3); // Siempre deberías usar métodos setter y métodos getter
            trek.setCadence(100);

            // ToString es una convención para mostrar el valor del objeto.
            Console.WriteLine("trek info: " + trek.ToString());

            // Instanciar otra nueva bicicleta
            Bicycle octo = new Bicycle(5, 10);
            Console.WriteLine("octo info: " + octo.ToString());

            // Instanciar un Penny Farthing (Biciclo)
            PennyFarthing funbike = new PennyFarthing(1, 10);
            Console.WriteLine("funbike info: " + funbike.ToString());

            Console.Read();
        } // Fin del método main


    } // Fin de la clase LearnCSharp

    // Puedes incluir otras clases en un archivo .cs


    // Sintaxis para la declaración de clases:
    // <public/private/protected> class <nombre_de_clase>{
    //    //campos, constructores, funciones todo adentro de la clase.
    //    //las funciones son llamadas métodos como en java.
    // }

    public class Bicycle
    {
        // Campos/Variables de la clase Bicycle
        public int cadence; // Public: Accesible desde cualquier lado
        private int _speed;  // Private: Sólo es accesible desde dentro de la clase
        protected int gear; // Protected: Accesible desde clases y subclases
        internal int wheels; // Internal: Accesible en el ensamblado
        string name; // Todo es privado por defecto: Sólo es accesible desde dentro de esta clase

        // Enum es un tipo valor que consiste un una serie de constantes con nombres
        public enum Brand
        {
            AIST,
            BMC,
            Electra,
            Gitane
        }
        // Definimos este tipo dentro de la clase Bicycle, por lo tanto es un tipo anidado
        // El código afuera de esta clase debería referenciar este tipo como Bicycle.Brand

        public Brand brand; // Declaramos un tipo enum, podemos declarar un campo de este tipo

        // Los miembros estáticos pertenecen al tipo mismo, no a un objeto en específico.
        static public int bicyclesCreated = 0;
        // Puedes acceder a ellos sin referenciar ningún objeto:
        // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated);

        // Los valores readonly (Sólo lectura) son establecidos en tiempo de ejecución
        // sólo pueden ser asignados al momento de la declaración o dentro de un constructor
        readonly bool hasCardsInSpokes = false; // privado de sólo lectura

        // Los constructores son una forma de crear clases
        // Este es un constructor por defecto
        private Bicycle()
        {
            gear = 1;
            cadence = 50;
            _speed = 5;
            name = "Bontrager";
            brand = Brand.AIST;
            bicyclesCreated++;
        }

        // Este es un constructor específico (contiene argumentos)
        public Bicycle(int startCadence, int startSpeed, int startGear,
                       string name, bool hasCardsInSpokes, Brand brand)
        {
            this.gear = startGear; // La palabra reservada "this" señala el objeto actual
            this.cadence = startCadence;
            this._speed = startSpeed;
            this.name = name; // Puede ser útil cuando hay un conflicto de nombres
            this.hasCardsInSpokes = hasCardsInSpokes;
            this.brand = brand;
        }

        // Los constructores pueden ser encadenados
        public Bicycle(int startCadence, int startSpeed, Brand brand) :
            this(startCadence, startSpeed, 0, "big wheels", true)
        {
        }

        // Sintaxis para Funciones:
        // <public/private/protected> <tipo_retorno> <nombre_funcion>(<args>)

        // Las clases pueden implementar getters y setters para sus campos
        // o pueden implementar propiedades

        // Sintaxis para la declaración de métodos:
        // <ámbito> <tipo_retorno> <nombre_método>(<argumentos>)
        public int GetCadence()
        {
            return cadence;
        }

        // Los métodos void no requieren usar return
        public void SetCadence(int newValue)
        {
            cadence = newValue;
        }

        // La palabra reservada virtual indica que este método puede ser sobrescrito
        public virtual void SetGear(int newValue)
        {
            gear = newValue;
        }

        // Los parámetros de un método pueden tener valores por defecto.
		// En este caso, los métodos pueden ser llamados omitiendo esos parámetros
        public void SpeedUp(int increment = 1)
        {
            _speed += increment;
        }

        public void SlowDown(int decrement = 1)
        {
            _speed -= decrement;
        }

        // Propiedades y valores get/set
        // Cuando los datos sólo necesitan ser accedidos, considera usar propiedades.
        // Las propiedades pueden tener get, set o ambos
        private bool _hasTassles; // variable privada
        public bool HasTassles // acceso público
        {
            get { return _hasTassles; }
            set { _hasTassles = value; }
        }

        // Las propiedades pueden ser auto implementadas
        public int FrameSize
        {
            get;
            // Puedes especificar modificadores de acceso tanto para get como para set
            // esto significa que sólo dentro de la clase Bicycle se puede modificar Framesize
            private set;
        }

        //Método para mostrar los valores de atributos de este objeto.
        public override string ToString()
        {
            return "gear: " + gear +
                    " cadence: " + cadence +
                    " speed: " + _speed +
                    " name: " + name +
                    " cards in spokes: " + (hasCardsInSpokes ? "yes" : "no") +
                    "\n------------------------------\n"
                    ;
        }

        // Los métodos también pueden ser estáticos. Puede ser útil para métodos de ayuda
        public static bool DidWeCreateEnoughBycles()
        {
            // Dentro de un método esático,
			// Sólo podemos hacer referencia a miembros estáticos de clases
            return bicyclesCreated > 9000;
        }   // Si tu clase sólo necesita miembros estáticos,
		    // considera establecer la clase como static.

    } // fin de la clase Bicycle

    // PennyFarthing es una subclase de Bicycle
    class PennyFarthing : Bicycle
    {
        // (Penny Farthings son las bicicletas con una rueda delantera enorme.
        // No tienen engranajes.)

        // llamar al constructor de la clase padre
        public PennyFarthing(int startCadence, int startSpeed) :
            base(startCadence, startSpeed, 0, "PennyFarthing", true)
        {
        }

        public override void SetGear(int gear)
        {
            gear = 0;
        }

        public override string ToString()
        {
            string result = "PennyFarthing bicycle ";
            result += base.ToString(); // Llamar a la versión base del método
            return reuslt;
        }
    }

    // Las interfaces sólo contienen las declaraciones
	// de los miembros, sin la implementación.
    interface IJumpable
    {
        void Jump(int meters); // todos los miembros de interfaces son implícitamente públicos
    }

    interface IBreakable
    {
		// Las interfaces pueden contener tanto propiedades como métodos, campos y eventos
        bool Broken { get; }
    }

	// Las clases sólo heredan de alguna otra clase, pero pueden implementar
	// cualquier cantidad de interfaces
    class MountainBike : Bicycle, IJumpable, IBreakable
    {
        int damage = 0;

        public void Jump(int meters)
        {
            damage += meters;
        }

        public void Broken
        {
            get
            {
                return damage > 100;
            }
        }
    }
} // Fin del espacio de nombres

```

## Temas no cubiertos

 * Flags
 * Attributes
 * Generics (T), Delegates, Func, Actions, lambda expressions
 * Static properties
 * Exceptions, Abstraction
 * LINQ
 * ASP.NET (Web Forms/MVC/WebMatrix)
 * Winforms
 * Windows Presentation Foundation (WPF)



## Lecturas recomendadas

 * [DotNetPerls](http://www.dotnetperls.com)
 * [C# in Depth](http://manning.com/skeet2)
 * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
 * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
 * [MSDN Library](http://msdn.microsoft.com/es-es/library/618ayhy6.aspx)
 * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
 * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials)
 * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
 * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)



[Convenciones de código de C#](http://msdn.microsoft.com/es-es/library/vstudio/ff926074.aspx)
---
language: css
filename: learncss-es.css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
    - ["Daniel Zendejas","https://github.com/DanielZendejas"]
lang: es-es
---

Tutorial de CSS en español

En los primeros días de la web no había elementos visuales, todo
era texto plano. Pero después, con el desarrollo de los navegadores,
las páginas con contenido visual empezaron a ser más comunes.
CSS es el lenguaje estándar que existe para separar el contenido
(HTML) y el aspecto visual de las páginas web.

Lo que CSS hace es proveer con una sintaxis que te permite apuntar a distintos 
elementos HTML y asignarles diferentes propiedades visuales.

CSS, como cualquier otro lenguaje, tiene múltiples versiones. Aquí nos enfocamos
en CSS 2.0. No es la versión más reciente pero sí la más soportada y compatible.

**NOTA:** Como los resultados de CSS son efectos visuales, para aprenderlo, 
necesitarás probar todo tipo de cosas en ambientes como 
[dabblet](http://dabblet.com/). Este artículo se enfoca, principalmente, en
la sintaxis y consejos generales.

```css
/* ¡Los comentarios aparecen dentro de diagonal-asterisco, justo como esta línea! */

/* ####################
   ## SELECTORES
   ####################*/

/* Generalmente, la sentencia principal en CSS es muy simple. */
selector { propiedad: valor; /* más propiedades separados por punto y coma...*/ }

/* El selector es usado para apuntar a (seleccionar) un elemento en la página.

¡Puedes apuntar a todos los elementos en la página con el asterisco! */
* { color:red; }

/*
Dado un elemento como este en la página:

<div class='una-clase clase2' id='unaId' attr='valor' />
*/

/* puedes seleccionar el <div> por el nombre de su clase */
.una-clase { }

/*¡O por sus dos clases! */
.una-clase.clase2 { }

/* O por el nombre de su elemento */
div { }

/* O por su Id */
#unaId { }

/* ¡O por el hecho de que tiene un atributo! */
[attr] { font-size:smaller; }

/* O por el hecho de que el atributo tiene un valor determinado */
[attr='valor'] { font-size:smaller; }

/* Empieza con un valor ('val' en este caso)*/
[attr^='val'] { font-size:smaller; }

/* O termina con un valor ('or' en este caso) */
[attr$='or'] { font-size:smaller; }

/* O incluso contiene un valor ('lo' en este caso) */
[attr~='lo'] { font-size:smaller; }

/*Más importante, puedes combinar estos criterios de búsqueda entre sí.
No debe existir ningún espacio entre estas partes porque hace que el 
significado cambie.*/
div.una-clase[attr$='or'] { }

/* También puedes seleccionar un elemento HTML basándote en sus padres*/

/* Un elemento que es hijo directo de otro elemento (Seleccionado de la forma que
vimos anteriormente) */

div.un-padre > .nombre-clase {}

/* O cualquiera de sus ancestros en la jerarquía*/
/* La siguiente sentencia selecciona a cualquier elemento que tenga una clase
"nombre-clase" y sea hijo de un div con clase "un-padre" EN CUALQUIER PROFUNDIDAD*/
div.un-padre .nombre-clase {}

/* advertencia: el mismo selector sin espacio tiene otro significado. ¿Puedes
identificar la diferencia?*/

/* También puedes seleccionar un elemento basado en su hermano inmediato previo*/
.yo-estoy-antes + .este-elemento { }

/*o cualquier hermano previo */
.yo-soy-cualquiera-antes ~ .estes-elemento {}

/* Existen algunas pseudo-clases que permiten seleccionar un elemento
basado en el comportamiendo de la página (a diferencia de la estructura de
la página) */

/* Por ejemplo, para cuando pasas el mouse por encima de un elemento */
:hover {}

/* o una liga visitada*/
:visited {}

/* o una liga no visitada aún*/
:link {}

/* o un elemento de un formulario que esté seleccionado */
:focus {}


/* ####################
   ## PROPIEDADES
   ####################*/

selector {
    
    /* Unidades */
    width: 50%; /* en porcentaje */
    font-size: 2em; /* dos veces el tamaño de la fuente actual */
    width: 200px; /* en pixeles */
    font-size: 20pt; /* en puntos */
    width: 5cm; /* en centimetros */
    width: 50mm; /* en milimetros */
    width: 5in; /* en pulgadas */
    
    /* Colores */
    background-color: #F6E;  /* en hexadecimal corto */
    background-color: #F262E2; /* en hexadecimal largo */
    background-color: tomato; /* puede ser un color con nombre */
    background-color: rgb(255, 255, 255); /* en rgb */
    background-color: rgb(10%, 20%, 50%); /* en rgb percent */
    background-color: rgba(255, 0, 0, 0.3); /* en rgb semi-transparente (con valor alfa)*/
    
    /* Imagenes */
    background-image: url(/ruta-a-la-imagen/imagen.jpg);
    
    /* Fuentes */
    font-family: Arial;
    font-family: "Courier New"; /* si el nombre contiene espacios, debe ir entre comillas */
    font-family: "Courier New", Trebuchet, Arial; /* si la primera fuente no se encontró 
    entonces se busca la seguna, o la tercera, así recursivamente*/
}

```

## Uso

Guarda cualquier CSS que quieras en un archivo con extensión `.css`.

```xml
<!-- Necesitas incluir tu archivo CSS en el elemento <head> de tu HTML: -->
<link rel='stylesheet' type='text/css' href='ruta/archivoDeEstilos.css' />

<!--
también puedes incluir CSS dentro del archivo HTML. Esta no es una buena práctica
y debe ser evitada.
-->
<style>
   selector { propiedad:valor; }
</style>

<!--
También se pueden aplicar propiedades al elemento directamente.
Esta práctica también debe ser evitada a toda costa
-->
<div style='propiedad:valor;'>
</div>

```

## Preferencia y orden

Como te habrás dado cuenta un elemento puede ser seleccionado por más
de un selector. En este caso alguna de las reglas cobra preferencia
sobre las otras:

Dado el siguiente CSS:

```css
/*A*/
p.clase1[attr='valor']

/*B*/
p.clase1 {}

/*C*/
p.clase2 {}

/*D*/
p {}

/*E*/
p { propiedad: valor !important; }

```

Y el siguiente HTML:

```xml
<p style='/*F*/ propiedad:valor;' class='clase1 clase2' attr='valor'>
</p>
```

El orden respetado es el siguiente:  
Recuerda, la preferencia es por cada **property**, no para el bloque completo.

* `E` tiene la preferencia más elevada gracias a la palabra `!important`.  
	Es recomendado evitar esto a menos que sea estrictamente necesario incluirlo.
* `F` le sigue, porque es estilo incrustado directamente en el HTML.
* `A` le sigue, porque es más específico que cualquier otra opción.  
	más específico = más especificadores. Aquí hay tres especificadores: elemento `p` +   
	nombre de la clase `clase1` + un atributo `attr='valor'`
* `C` le sigue. Aunque tiene el mismo número de especificadores como `B`  
	pero aparece después.
* Luego va `B`
* y al final  `D`.

## Compatibilidad

La mayoría de las funcionalidades de CSS2 (y gradualmente de CSS3) son compatibles 
en todos los navegadores y dispositivos. Pero siempre es vital tener en mente la
compatibilidad y disponibilidad del CSS que uses con respecto a los navegadores
y dispositivos para los que desarrolles.

[QuirksMode CSS](http://www.quirksmode.org/css/) es una excelente referencia para esto.

## Recursos

* Para ejecutar un test de compatibilidad, revisa [CanIUse](http://caniuse.com).
* CSS Playground [Dabblet](http://dabblet.com/).
* [Mozilla Developer Network's CSS documentation](https://developer.mozilla.org/en-US/docs/Web/CSS).
* [Codrops' CSS Reference](http://tympanus.net/codrops/css_reference/).

## Otras lecturas

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/).
* [Selecting elements using attributes](https://css-tricks.com/almanac/selectors/a/attribute/).
* [QuirksMode CSS](http://www.quirksmode.org/css/).
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
* [SASS](http://sass-lang.com/) y [LESS](http://lesscss.org/) para preprocesamiento CSS.
* [CSS-Tricks](https://css-tricks.com).

---
category: Algorithms & Data Structures
name: Dynamic Programming
contributors:
    - ["Akashdeep Goel", "http://github.com/akashdeepgoel"]
translators:
    - ["Gino Amaury", "https://github.com/ginoamaury"]
lang: es-es 
---

# Programación Dinámica

## Introducción

La programación dinámica es una técnica poderosa usada para resolver una clase particular de problemas como veremos más adelante. 
La idea es muy simple: si has solucionado un problema con la entrada dada, entonces, guardaremos el resultado para una futura referencia, con el fin de evitar la solución del mismo problema de nuevo.

Recuerda siempre:
"Aquellos que no pueden recordar el pasado están condenados a repetirlo"

## Formas de resolver este tipo de problemas

1. *De arriba hacia abajo (Top-Down)* : Empezamos resolviendo el problema dado descomponiendolo. Si ves que el problema fue resuelto, entonces retorna la respuesta guardada. Si no se ha resuelto, resuélvelo y guarda la respuesta. Esto suele ser fácil de pensar y es muy intuitivo. A esto se le conoce como memoización.

2. *De abajo hacia arriba (Bottom-Up)* : Analiza el problema y ve el orden en que los subproblemas deben ser resueltos y empieza resolviendo el subproblema más trivial, hacia el problema dado. En este proceso, se garantiza que los subproblemas se resuelven antes de resolver el problema. Esto se conoce como Programación Dinámica.

## Ejemplo de Programación Dinámica

El problema de la subsecuencia creciente máxima consiste en encontrar la subsecuencia creciente máxima en una secuencia dada. Dada la secuencia `S= {a1 , a2 , a3, a4, ............., an-1, an }`, tenemos que encontrar un subconjunto más largo tal que para todo `j` y `i`, `j<i` en el subconjunto `aj<ai`.
En primer lugar tenemos que encontrar el valor de las subsecuencias más largas (LSi) en cada índice `i` con el último elemento de la secuencia que es `ai`. El mayor LSi sería la subsecuencia más larga de la secuencia dada. Para empezar, LSi=1 ya que `ai` es un elemento de la secuencia (el último elemento). Entonces, para todo `j` tal que `j<i` y `aj<ai`, encontramos el LSj más grande y lo agregamos al LSi, por lo que el algoritmo toma un tiempo de *O(n2)*.
Pseudocódigo para encontrar la longitud de la subsecuencia creciente máxima:
La complejidad de este algoritmo podría reducirse mediante el uso de una mejor estructura de datos que los arreglos. Guardar un arreglo de predecesores y una variable como `secuencia_mas_grande_hasta_ahora` y su índice podría ahorrar mucho tiempo.

Un concepto similar se podría aplicar para encontrar la trayectoria más larga en un grafo acíclico dirigido (DAG).

```python
for i=0 to n-1
    LS[i]=1
    for j=0 to i-1
        if (a[i] >  a[j] and LS[i]<LS[j])
            LS[i] = LS[j]+1
for i=0 to n-1
    if (largest < LS[i])
```

### Algunos problemas famosos de Programación Dinámica (DP).

- Algoritmo Floyd Warshall(EN) - [Tutorial y código fuente del programa en C](http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code)
- Problema de la Mochila(EN) - [Tutorial y código fuente del programa en C](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem)
- Problema de Subsecuencia Común mas Larga(EN) - [Tutorial y código fuente del programa en C](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence)

## Recursos en línea

* [codechef EN](https://www.codechef.com/wiki/tutorial-dynamic-programming)
---
language: edn
filename: learnedn-es.edn
contributors:
  - ["Jason Yeo", "https://github.com/jsyeo"]
translators:
    - ["Gino Amaury", "https://github.com/ginoamaury"]
lang: es-es 
---

La notación de datos extensible (Extensible Data Notation (EDN)) es un formato para serializar los datos.

La notación se utiliza internamente por Clojure para representar programas. También es
utilizado como un formato de transferencia de datos como JSON. A pesar de que se utiliza más comúnmente en
Clojure, existen implementaciones de EDN para muchos otros lenguajes.

El principal beneficio de EDN sobre JSON y YAML es que es extensible. 
Vamos a ver cómo se extiende más adelante.

```clojure
; Los comentarios comienzan con un punto y coma.
; Cualquier cosa después del punto y coma es ignorado.

;;;;;;;;;;;;;;;;;;;
;;;Tipos Básicos;;;
;;;;;;;;;;;;;;;;;;;

nil         ; También conocido en otros lenguajes como nulo (null).

; Booleanos
true
false

; Las cadenas se encierran entre comillas dobles
"desayuno húngaro"
"tortilla de queso del granjero"

; Los caracteres están precedidos por barras invertidas
\g \r \a \c \e

; Las palabras claves comienzan con dos puntos.Se comportan como las enumeraciones. Más o menos
; Como símbolos en Ruby
:huevos
:queso
:aceitunas

; Los símbolos se utilizan para representar los identificadores.Estos empiezan con #.
; puedes tener espacios usando el símbolo /. cualquier cosa precedida / es
; un espacio en el nombre.
#cuchara
#cocina/cuchara ; no es lo mismo que #spoon
#cocina/tenedor
#github/tenedor   ; no se puede comer con este.

; Números enteros y flotantes
42
3.14159

; Las listas son secuencias de valores.
(:bollo :empanada-de-res 9 "yum!")

; Vectores permiten acceso aleatorio
[:helado 1 2 -2]

; Los mapas son estructuras de datos asociativos que se asocian con la clave de su valor.
{:huevos        2
 :jugo-de-limon 3.5
 :mantequilla     1}

; Usted no está restringido a usar palabras clave como claves.
{[1 2 3 4] "decirle a la gente lo que llevaba",
 [5 6 7 8] "Entre mas tu ves, mas lo odias"}

; Puede usar comas para facilitar la lectura. Se tratan como espacios en blanco.

; Los conjuntos son colecciones que contienen elementos únicos.
#{:a :b 88 "huat"}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;Elementos de etiqueta ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; EDN puede ser extendido por elementos de etiqueta con el símbolo #.

#MyYelpClone/MenuItem {:nombre "huevos-Benedict" :clasificacion 10}

; Permíteme explicar esto con un ejemplo en colujre. Supongamos que quiero
; transformar ese pedazo de EDN en un registro del Menú.

(defrecord MenuItem [nombre clasificacion])

; Para transformar EDN en valores clojure, necesitaremos usar el constructor en EDN
; lectura, edn/read-string

(edn/read-string "{:huevos 2 :mantequilla 1 :harina 5}")
; -> {:huevos 2 :mantequilla 1 :harina 5}

; Para transformar los elementos de etiqueta, definir la función de lectura y pasar un mapa
; que asigna etiquetas a funciones del lector de edn/read-string al igual que.

(edn/read-string {:lectores {'MyYelpClone/MenuItem map->menu-item}}
                 "#MyYelpClone/MenuItem {:nombre \"huevos-benedict\" :clasificacion 10}")
; -> #user.MenuItem{:nombre "huevos-benedict", :clasificacion 10}

```

# Referencias

- [EDN spec (EN)](https://github.com/edn-format/edn)
- [Implementations (EN)](https://github.com/edn-format/edn/wiki/Implementations)
- [Tagged Elements (EN)](http://www.compoundtheory.com/clojure-edn-walkthrough/)
---
language: elisp
contributors:
    - ["Bastien Guerry", "http://bzg.fr"]
translators:
    - ["Guillermo Vayá", "http://willyfrog.es"]
lang: es-es
filename: learn-emacs-lisp-es.el
---

```scheme
;; Introduccion a Emacs Lisp en 15 minutos (v0.2d)
;;
;; Autor: Bastien / @bzg2 / http://bzg.fr
;; Traducción: Guillermo Vayá / @Driadan / http://willyfrog.es
;;
;; Antes de nada, lee este texto de Peter Norvig:
;; Traducido: http://loro.sourceforge.net/notes/21-dias.html
;; Original: http://norvig.com/21-days.html
;;
;; Ahora instala GNU Emacs 24.3:
;;
;; Debian: apt-get install emacs 
;; (o sigue las instrucciones de tu distribución preferida)
;; OSX: http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg
;; Windows: http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip
;;
;; Puedes encontrar información general sobre Emacs en:
;; http://www.gnu.org/software/emacs/#Obtaining

;; Aviso importante:
;;
;; Seguir este tutorial no provocará daños en tu ordenador a menos que
;; te enfades tanto que que acabes tirándolo al suelo. En tal caso 
;; declino cualquier responsabilidad. ¡A divertirse!


;; "N. del. T.": Algunos términos comunes de la informática se han dejado 
;; sin traducir ya que es mucho más probable que el lector los conozca en 
;; su forma en inglés, siendo la versión en español de muy raro uso.
;; Además "sexps" se ha decidido traducir por sexpresión.
;; Por último, añadir que no se han traducido los ejemplos de código ya que no 
;; es necesario entender qué dice el string para comprender el funcionamiento
;; y podría llevar a error.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; Inicia Emacs.
;;
;; Pulsa la tecla `q' para pasar el mensaje de bienvenida.
;;
;; Mira a la línea gris en la parte inferior de la ventana:
;;
;; "*scratch*" es el nombre del espacio editable donde estás.
;; A este espacio editable se le llama "buffer".
;;
;; Scratch es el buffer por defecto cuando abres Emacs.
;; En Emacs nunca editas ficheros, sino que editas buffers que 
;; posteriormente pueden grabarse a un fichero.
;; can save to a file.
;; 
;; "Lisp interaction" indica el conjunto de ordenes disponibles.
;; 
;; Emacs dispone de un set de comandos disponibles en cualquier buffer
;; ("built-ins") y aparte varios conjuntos de ordenes disponibles 
;; según el modo específico que esté activo. En nuestro caso 
;; estamos usando `lisp-interaction-mode', el cual incluye las
;; ordenes necesarias para evaluar y navegar código Elisp.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Un punto y coma comienza un comentario. Pueden ponerse en cualquier 
;; posicion de la linea.
;;
;; Los programas en Elisp se componen de expresiones simbólicas
;; tambien llamadas "sexps":
(+ 2 2)

;; Esta expresión simbólica se lee tal que "Suma 2 y 2"

;; Las sexpresiones se rodean por paréntesis, y pueden anidarse:
(+ 2 (+ 1 1))

;; Una expresion simbólica está formada bien por átomos o bien por otras 
;; expresiones simbólicas. En el ejemplo de arriba, 1 y 2 son átomos, 
;; mientras que (+ 2 (+ 1 1)) y (+ 1 1) son expresiones simbólicas.

;; Gracias a `lisp-interaction-mode' puedes evaluar las sexpresiones.
;; Coloca el cursor justo despues del paréntesis de cierre y 
;; mantén pulsada la tecla Control y la j (para abreviar usaremos "C-j").

(+ 3 (+ 1 2))
;;           ^ pon aquí el cursor
;; `C-j' => 6

;; `C-j' añade el resultado de la evaluación al buffer.

;; `C-xC-e' muestra el mismo resultado pero en la linea inferior
;; la cual se llama "minibuffer".  Este será el metodo que usaremos 
;; normalmente para no llenar el buffer con texto inútil.

;; `setq' guarda un valor en una variable:
(setq my-name "Bastien")
;; `C-xC-e' => "Bastien" (aparece en el mini-buffer)

;; `insert' añade "Hello!" en el punto donde esté tu cursor:
(insert "Hello!")
;; `C-xC-e' => "Hello!"

;; Aunque hemos usado `insert' con solo un parámetro "Hello!", se
;; pueden pasar más. Por ejemplo, en esta otra sexpresión usamos dos:

(insert "Hello" " world!")
;; `C-xC-e' => "Hello world!"

;; Se pueden usar variables en lugar de strings:
(insert "Hello, I am " my-name)
;; `C-xC-e' => "Hello, I am Bastien"

;; Puedes combinar sexpresiones en funciones:
(defun hello () (insert "Hello, I am " my-name))
;; `C-xC-e' => hello

;; Evaluemos la funcion:
(hello)
;; `C-xC-e' => Hello, I am Bastien

;; Los parentesis vacios en la definicion de una funcion indican
;; que no acepta parámetros. En cualquier caso, usar `my-name' siempre
;; es aburrido, asi que vamos a hacer que la función accepte un parámetro
;; (en este caso el parametro se llama "name"):
(defun hello (name) (insert "Hello " name))
;; `C-xC-e' => hello

;; Ahora vamos a llamar a la funcion con el string "you" como valor para 
;; el único parámetro que posee.
(hello "you")
;; `C-xC-e' => "Hello you"

;; ¡Genial!

;; Descansa un poco y respira.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Ahora cambiaremos al nuevo buffer, llamado "*test*", en una nueva ventana.

(switch-to-buffer-other-window "*test*")
;; `C-xC-e'
;; => [La pantalla ahora tiene dos ventanas y el cursor está en el buffer *test*]

;; Mueve el ratón sobre la ventana superior y pulsa el boton izdo. para volver. 
;; Otra forma es usando `C-xo' (pulsa simultaneamente control y x y luego la o)
;; para ir a la otra ventana.

;; Se pueden combinar varias sexpresiones mediante `progn':
(progn
  (switch-to-buffer-other-window "*test*")
  (hello "you"))
;; `C-xC-e'
;; => [De las dos ventanas de la pantalla, el cursor está en la marcada como *test*]

;; A partir de ahora, si no te importa, dejaremos de decir que pulses `C-xC-e': 
;; tendrás que hacerlo para ejecutar cada sexpresión que siga.

;; También tendrás que volver al buffer *scratch* bien con el ratón o con `C-xo'.

;; En ocasiones será util limpiar el buffer:
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "there"))

;; O volver a la ventana anterior:
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "you")
  (other-window 1))

;; Puedes enlazar un valor a una variable local con `let':
(let ((local-name "you"))
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello local-name)
  (other-window 1))

;; En este caso, no hace falta añadir `progn' ya que `let' permite combinar 
;; varias sexpresiones.

;; Vamos a darle formato a un string:
(format "Hello %s!\n" "visitor")

;; Cada %s indica la posicion donde irá un string, el cual será reemplazado 
;; por "visitor". "\n" es el caracter de nueva línea.

;; Mejoremos nuestra funcion usando `format':
(defun hello (name)
  (insert (format "Hello %s!\n" name)))

(hello "you")

;; Creemos una nueva funcion que utililce `let':
(defun greeting (name)
  (let ((your-name "Bastien"))
    (insert (format "Hello %s!\n\nI am %s."
                    name       ; the argument of the function
                    your-name  ; the let-bound variable "Bastien"
                    ))))

;; Y ahora la evaluamos:
(greeting "you")

;; Algunas funciones son interactivas:
(read-from-minibuffer "Enter your name: ")

;; Al evaluar esta función, ésta devuelve lo que hayas introducido.

;; Ahora hagamos nuestra función `greeting' preguntar por tu nombre:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (insert (format "Hello!\n\nI am %s and you are %s."
                    from-name ; the argument of the function
                    your-name ; the let-bound var, entered at prompt
                    ))))

(greeting "Bastien")

;; Y ahora la completamos mostrando el resultado en la otra ventana:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (insert (format "Hello %s!\n\nI am %s." your-name from-name))
    (other-window 1)))

;; Probémosla:
(greeting "Bastien")

;; Descansa un poco y respira.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Creemos una lista de nombres:
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))

;; Para coger el primer elemento de la lista usaremos `car':
(car list-of-names)

;; Para coger todos menos el primer elemento de la lista 
;; usaremos `cdr':
(cdr list-of-names)

;; Para añadir un elemento al comienzo de la lista utilizamos `push':
(push "Stephanie" list-of-names)

;; OJO: `car' y `cdr' no modifican la lista, mientras que `push' sí.
;; ¡Es una diferencia importante! Algunas funciones no tienen efectos 
;; colaterales (como `car') mientras que otras sí (como `push').
;; "N. del T.": estos efectos colaterales se les llama `side-effects' en 
;; las distintas variantes de lisp.

;; Llamemos a `hello' con cada elemento de `list-of-names':
(mapcar 'hello list-of-names)

;; Retocamos `greeting' para que salude a todos los que estén en `list-of-names':
(defun greeting ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (mapcar 'hello list-of-names)
    (other-window 1))

(greeting)

;; ¿Te acuerdas de la función `hello' definida un poco más arriba?
;; Recibía un parámetro: `name'. Así que `mapcar' llama a `hello' con cada 
;; elemento de `list-of-names' como parámetro de `hello'.

;; Ahora ordenaremos un poco lo que tenemos en el buffer:

(defun replace-hello-by-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (search-forward "Hello")
      (replace-match "Bonjour"))
    (other-window 1))

;; (goto-char (point-min)) mueve el cursor al principio del buffer.
;; (search-forward "Hello") busca un string "Hello".
;; (while x y) evalua la/s sexpresion/es y mientras que x devuelva
;; alguna cosa.
;; En el momento que x devuelva `nil' (es decir nada), sale del 
;; bucle `while'.

(replace-hello-by-bonjour)

;; Observamos que todas las veces que teníamos la palabra "Hello" en el buffer *test*
;; han sido reemplazadas por "Bonjour".

;; Y además, hemos obtenido un error: "Search failed: Hello".
;;
;; Para evitar este error, hay que decirle a `search-forward' si debería dejar de 
;; buscar en el buffer en algún momento y si debería fallar sin quejarse cuando 
;; no encuentra nada.

;; (search-forward "Hello" nil t) justo hace eso:

;; El argumento `nil' significa que la busqueda no está ligada a ninguna posición.
;; Y el argumento `t' le pide que no diga nada si no encuentra el string.

;; Usaremos esta sexpresión en la función siguiente, la cual ya 
;; no muestra ningún error:

(defun hello-to-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    ;; Say hello to names in `list-of-names'
    (mapcar 'hello list-of-names)
    (goto-char (point-min))
    ;; Replace "Hello" by "Bonjour"
    (while (search-forward "Hello" nil t)
      (replace-match "Bonjour"))
    (other-window 1))

(hello-to-bonjour)

;; Añadamos algo de color a los nombres:

(defun boldify-names ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (re-search-forward "Bonjour \\(.+\\)!" nil t)
      (add-text-properties (match-beginning 1)
                           (match-end 1)
                           (list 'face 'bold)))
    (other-window 1))

;; Esta función nos presenta `re-search-forward': en vez de 
;; buscar el string "Bonjour" exacto, se busca por un patrón
;; usando una "expresión regular" (lo cual se muestra abreviado 
;; en el prefijo "re-" del inglés "Regular Expression").

;; La expresión regular a utilizar es "Bonjour \\(.+\\)!" y se traduce como:
;; el string "Bonjour ", seguido de 
;; un grupo de           | representado por \\( ... \\)
;;   cualquier caracter  | representado por .
;;   al menos una vez    | representado por +
;; y el string "!".

;; ¿Preparado?  ¡Probemoslo!

(boldify-names)

;; `add-text-properties' añade propiedades al texto, como una fuente.

;; ¡Hale! ¡Ya lo tenemos! ¡Feliz hacking!

;; Si quieres saber más sobre una función o una variable:
;;
;; C-h v la-variable RET
;; C-h f la-funcion RET
;;
;; Si quieres leer el manual de Emacs Lisp desde dentro de Emacs:
;;
;; C-h i m elisp RET
;;
;; Para leer una introducción en linea de Emacs Lisp:
;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html

;; Me gustaría agradecer a las siguientes personas su feedback y sugerencias:
;; - Wes Hardaker
;; - notbob
;; - Kevin Montuori
;; - Arne Babenhauserheide
;; - Alan Schmitt
;; - LinXitoW
;; - Aaron Meurer
```
---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
    - ["Dzianis Dashkevich", "https://github.com/dskecse"]
    - ["Ryan Plant", "https://github.com/ryanplant-au"]
translator:
    - ["Adrian Carrascal", "https://github.com/acarrascalgarcia"]
filename: learnelixir-es.ex
lang: es-es

---

Elixir es un lenguaje funcional moderno construido sobre la máquina virtual de Erlang.
Es completamente compatibe con Erlang, sin embargo, ofrece una sintaxis más estandar
y otras características más.

```elixir

# Los comentarios de única línea
# comienzan con un símbolo numérico.

# No hay comentarios multilinea,
# pero se pueden apilar varios comentarios.

# Para usar el shell de elixir se usa el comando `iex`.
# Los módulos se compilan con el comando `elixirc`.

# Ambos deberían estar en la ruta si elixir se instaló correctamente.

## ---------------------------
## -- Tipos básicos
## ---------------------------

# Hay números
3    # integer
0x1F # integer
3.0  # float

# Átomos, que son literales, una constante con nombre. Comienzan con `:`.
:hello # atom

# Tuples that are stored contiguously in memory.
# Tuplas que se almacenan contiguamente en memoria.
{1,2,3} # tuple

# Se puede acceder a un elemento de una tupla con la función `elem`:
elem({1, 2, 3}, 0) #=> 1

# Listas que se implementan como listas enlazadas.
[1,2,3] # list

# Se puede acceder al primer y último elemento de la lista como:
[head | tail] = [1,2,3]
head #=> 1
tail #=> [2,3]

# En elixir, solo como Erlang, el `=` denota la coincidencia de patrones y
# no una asignación.
#
# This is how the above example of accessing the head and tail of a list works.
# Así es como el ejemplo anterior de acceder al
# primer y último elemento de una lista trabaja.

# Una coincidencia de patrón errará cuando los lados no coincidan, en este ejemplo
# las tuplas tienen diferentes tamaños.
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# También hay binarios
<<1,2,3>> # binary

# Cadenas y listas de caracteres
"hello" # string
'hello' # char list

# Cadenas de varias lineas
"""
I'm a multi-line
string.
"""
#=> "I'm a multi-line\nstring.\n"

# Todas las cadenas se codifican en UTF-8:
"héllò" #=> "héllò"

# Las cadenas son solo binarios realmente, y la lista de caracteres solo listas.
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# `?a` en elixir devuelve el valor ASCII para el caracter `a`
?a #=> 97

# Para concatenar listas se usa `++`, para binarios `<>`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'hello ' ++ 'world'  #=> 'hello world'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"hello " <> "world"  #=> "hello world"

# Los rangos se representan como `start..end` (Es inclusivo)
1..10 #=> 1..10
lower..upper = 1..10 # Se puede usar la coincidencia de patrones en los rangos también
[lower, upper] #=> [1, 10]

# Los mapas son pares de llave-valor
genders = %{"david" => "male", "gillian" => "female"}
genders["david"] #=> "male"

# Los mapas con llaves de tipo átomo se pueden usar como esto
genders = %{david: "male", gillian: "female"}
genders.gillian #=> "female"

## ---------------------------
## -- Opetadores
## ---------------------------

# Aritméticos
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# En elixir el operador `/` siempre devuelve un número flotante

# Para hacer la división de número entero se debe usar `div`
div(10, 2) #=> 5

# Para obtener el residuo de la división se debe usar `rem`
rem(10, 3) #=> 1

# También hay operadores lógicos: `or`, `and` y `not`.
# Estos operadores esperan un boolean como su primer argumento.
true and true #=> true
false or true #=> true
# 1 and true    #=> ** (ArgumentError) argument error

# Elixir también provee `||`, `&&` y `!` donde acepta argumentos de cualquier tipo.
# Todos los valores excepto `false` y `nil` se evaluarán como verdadero.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil
!true #=> false

# Para comparaciones se tiene: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` y `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# `===` y `!==` son más estrictos cuando comparan números:
1 == 1.0  #=> true
1 === 1.0 #=> false

# También se puede comparar dos tipos de datos diferentes:
1 < :hello #=> true

# No se necesita memorizar el orden pero es importante tenerlo en cuenta:
# number < atom < reference < functions < port < pid < tuple < list < bit string

## ---------------------------
## -- Control de flujo
## ---------------------------

# Expresión `if`
if false do
  "This will never be seen"
else
  "This will"
end

# También está la expresión `unless`
unless true do
  "This will never be seen"
else
  "This will"
end

# Se acuerda de la coincidencia de patrones?
# Muchas estructuras de control de flujo en elixir confían en ella.

# `case` permite comparar un valor con muchos patrones:
case {:one, :two} do
  {:four, :five} ->
    "This won't match"
  {:one, x} ->
    "This will match and bind `x` to `:two` in this clause"
  _ ->
    "This will match any value"
end

# Es común vincular el valor a `_` si no se necesita.
# Por ejemplo, si unicamente el primer elemento de la lista es importante:
[head | _] = [1,2,3]
head #=> 1

# Para una mejor lectura se puede hace lo siguiente:
[head | _tail] = [:a, :b, :c]
head #=> :a

# `cond` permite comprobar muchas condiciones al mismo tiempo.
# Usar `cond` en vez de muchas expresiones `if` anidadas.
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  1 + 2 == 3 ->
    "But I will"
end

# Es común estabecer la última condición como `true`, donde siempre va a coincidir.
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  true ->
    "But I will (this is essentially an else)"
end

# `try/catch` se usa para atrapar valores que se lanzan, también soporta una
# clausula `after` que se invoca sin importar si un valor se atrapó o no.
try do
  throw(:hello)
catch
  message -> "Got #{message}."
after
  IO.puts("I'm the after clause.")
end
#=> I'm the after clause
# "Got :hello"

## ---------------------------
## -- Módulos y Funciones
## ---------------------------

# Anonymous functions (notice the dot)
# Funciones anónimas (Ver el punto `.`)
square = fn(x) -> x * x end
square.(5) #=> 25

# También aceptan muchas cláusulas y guards.
# Los guards permiten afinar las coincidencias de patrones,
# se indican por la palabra reservada `when`:
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir también provee muchas funciones incorporadas.
# Esas están disponibles en el ámbito actual.
is_number(10)    #=> true
is_list("hello") #=> false
elem({1,2,3}, 0) #=> 1

# Se pueden agrupar varias funciones en un módulo. Dentro de un módulo
# se usa `def` para definir las funciones.
defmodule Math do
  def sum(a, b) do
    a + b
  end

  def square(x) do
    x * x
  end
end

Math.sum(1, 2)  #=> 3
Math.square(3) #=> 9

# Para compilar el módulo simple de Math se guarda como `math.ex` y se usa `elixirc`
# en la terminal: elixirc math.ex

# Dentro de un módulo se puede definir funciones con `def` y funciones privadas con `defp`.
# Una función definida con `def` está disponible para ser invocada desde otros módulos,
# una función privada se puede solo invocar localmente.
defmodule PrivateMath do
  def sum(a, b) do
    do_sum(a, b)
  end

  defp do_sum(a, b) do
    a + b
  end
end

PrivateMath.sum(1, 2)    #=> 3
# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)

# La declaración de funciones también soportan guards y múltiples cláusulas:
defmodule Geometry do
  def area({:rectangle, w, h}) do
    w * h
  end

  def area({:circle, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometry.area({:rectangle, 2, 3}) #=> 6
Geometry.area({:circle, 3})       #=> 28.25999999999999801048
# Geometry.area({:circle, "not_a_number"})
#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1

# Debido a la inmutabilidad, la recursión es una gran parte de elixir
defmodule Recursion do
  def sum_list([head | tail], acc) do
    sum_list(tail, acc + head)
  end

  def sum_list([], acc) do
    acc
  end
end

Recursion.sum_list([1,2,3], 0) #=> 6

# Los módulos de Elixir soportan atributos, hay atributos incorporados y
# se pueden agregar otros personalizados.
defmodule MyMod do
  @moduledoc """
  This is a built-in attribute on a example module.
  """

  @my_data 100 # This is a custom attribute.
  IO.inspect(@my_data) #=> 100
end

# El operador pipe |> permite que se pase la salida de una expresión
# como el primer parámetro en una función. 

Range.new(1,10)
|> Enum.map(fn x -> x * x end)
|> Enum.filter(fn x -> rem(x, 2) == 0 end)
#=> [4, 16, 36, 64, 100]

## ---------------------------
## -- Structs and Excepciones
## ---------------------------

# Los Structs son extensiones de los mapas que traen valores por defecto,
# garantes en tiempo de compilación y polimorfismo en Elixir.
defmodule Person do
  defstruct name: nil, age: 0, height: 0
end

joe_info = %Person{ name: "Joe", age: 30, height: 180 }
#=> %Person{age: 30, height: 180, name: "Joe"}

# Acceder al valor de name
joe_info.name #=> "Joe"

# Actualizar el valor de age
older_joe_info = %{ joe_info | age: 31 }
#=> %Person{age: 31, height: 180, name: "Joe"}

# El bloque `try` con la palabra reservada `rescue` se usa para manejar excepciones
try do
  raise "some error"
rescue
  RuntimeError -> "rescued a runtime error"
  _error -> "this will rescue any error"
end
#=> "rescued a runtime error"

# Todas las excepciones tienen un mensaje
try do
  raise "some error"
rescue
  x in [RuntimeError] ->
    x.message
end
#=> "some error"

## ---------------------------
## -- Concurrencia
## ---------------------------

# Elixir confía en el modelo actor para la concurrencia. Todo lo que se necesita para escribir
# programas concurrentes en elixir son tres primitivas: procesos de desove,
# envío de mensajes y recepción de mensajes.

# Para empezar un nuevo proceso se usa la función `spawn`,
# donde toma una función como argumento.
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>

# `spawn` devuelve un pid (identificador de proceso), se puede usar este pid para enviar
# mensajes para el proceso. Para hacer que un mensaje pase se usa el operador `send`.
# Para que todo esto se útil se necesita estar disponibles para recibir mensajes. Esto se
# alcanza con el mecanismo `receive`:

# El bloque `receive do` se usa para escuchar los mensajes y procesarlos
# cuando se reciben. Un bloque `receive do` solo procesará
# un mensaje recibido. Para procesar múltiples mensajes,
# una función con un bloque `receive do` tiene que llamarse recursivamente
# para entrar en el bloque `receive do` otra vez.

defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Area = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        area_loop()
    end
  end
end

# Compilar el módulo y crear un proceso que evalue `area_loop` en el shell
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>
# Como alternativa
pid = spawn(Geometry, :area_loop, [])

# Enviar un mensaje al `pid` que coincidirá con un patrón en el que recibe una sentencia
send pid, {:rectangle, 2, 3}
#=> Area = 6
#   {:rectangle,2,3}

send pid, {:circle, 2}
#=> Area = 12.56000000000000049738
#   {:circle,2}

# El shell también es un proceso, se puede usar `self` para obtener el pid actual
self() #=> #PID<0.27.0>

## ---------------------------
## -- Agentes
## ---------------------------

# Un agente es un proceso que mantiene el seguimiento de algún valor cambiante

# Un agente se crea con `Agent.start_link`, introducuendole una función
# El estado inicial del agente será lo que sea que la función devuelva
{ok, my_agent} = Agent.start_link(fn -> ["red, green"] end)

# `Agent.get` toma un nombre de agente y un `fn` que se pasa como el estado actual
# Lo que sea que este `fn` devuelva es lo que se obtendrá de vuelta
Agent.get(my_agent, fn colors -> colors end) #=> ["red, "green"]

# El estado del agente se actualiza de la misma manera
Agent.update(my_agent, fn colors -> ["blue" | colors] end)
```

## Referencias

* [Getting started guide](http://elixir-lang.org/getting-started/introduction.html) from the [Elixir website](http://elixir-lang.org)
* [Elixir Documentation](http://elixir-lang.org/docs/master/)
* ["Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) by Dave Thomas
* [Elixir Cheat Sheet](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf)
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) by Fred Hebert
* ["Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) by Joe Armstrong
---
language: forth
contributors:
    - ["Horse M.D.", "http://github.com/HorseMD/"]
translators:
    - ["Zach Larsen", "http://zachariahlarsen.com/"]
lang: es-es
filename: learnforth-es.fs
---

Forth fue criado por Charles H. Moore en los 70s. Forth es un lenguaje imperativo, basado en pila y entorno de programación, siendo usado en proyectos como Open Firmware. También esta usado por NASA.

Nota: Este articulo enfoca predominantemente en la Gforth implementación de Forth, pero casi todo 
de lo que esta escrito aquí debe funcionar en otro sitio.

```
\ Este es un comentario
( Este es un comentario también pero solo esta usado cuando definiendo palabras. )

\ --------------------------------- Precursor ----------------------------------

\ Todo programación en Forth se hace manipulando el parámetro pila (mas
\ común se refiere como "el pila").
5 2 3 56 76 23 65    \ ok

\ estos números se añadieron al pila desde izquierda a derecho.
.s    \ <7> 5 2 3 56 76 23 65 ok

\ En Forth, todo es o una palabra o un numero.

\ ------------------------------ Básico Aritmética ------------------------------

\ Aritmética (de hecho casi todas palabras que requieren datos) funciona manipulando datos
\ en el pila.
5 4 +    \ ok

\ `.` saca lo alto resulto desde el pila:
.    \ 9 ok

\ Mas ejemplos de aritmética:
6 7 * .        \ 42 ok
1360 23 - .    \ 1337 ok
12 12 / .      \ 1 ok
13 2 mod .     \ 1 ok

99 negate .    \ -99 ok
-99 abs .      \ 99 ok
52 23 max .    \ 52 ok
52 23 min .    \ 23 ok

\ ----------------------------- Pila Manipulación -----------------------------

\ Naturalmente, cuando trabajaremos con el pila, querremos algunos metidos útiles:

3 dup -          \ duplicar el primero articulo (1ra ahora igual a 2da): 3 - 3
2 5 swap /       \ intercambiar la primera con la segunda elemento:        5 / 2
6 4 5 rot .s     \ rotar los tres primero elementos:                   4 5 6
4 0 drop 2 /     \ sacar el primero articulo (no imprima a la pantalla):  4 / 2
1 2 3 nip .s     \ sacar el segundo articulo (similar a drop):    1 3

\ ---------------------- Mas Avanzado Pila Manipulación ----------------------

1 2 3 4 tuck   \ duplicar el primero articulo en el segundo hueco:      1 2 4 3 4 ok
1 2 3 4 over   \ duplicar el segundo articulo a la primera del pila:      1 2 3 4 3 ok
1 2 3 4 2 roll \ *mover* el articulo en este posición a la primera del pila:      1 3 4 2 ok
1 2 3 4 2 pick \ *duplicar* el articulo en este posición a la primera del pila: 1 2 3 4 2 ok

\ Cuando refiere a pila indices, ellos son basado en cero.

\ ------------------------------ Creando Palabras --------------------------------

\ La `:` palabra hace que Forth entra modo de compilar hasta que se ve la `;` palabra.
: cuadrado ( n -- n ) dup * ;    \ ok
5 cuadrado .                     \ 25 ok

\ Podemos ver lo que hace una palabra también.:
see cuadrado     \ : cuadrado dup * ; ok

\ -------------------------------- Condicionales --------------------------------

\ -1 == cierto, 0 == falso. No obstante, valores que no son cero es usualmente tratado como
\ siendo cierto:
42 42 =    \ -1 ok
12 53 =    \ 0 ok

\ `if` es una palabra que solamente compila. `if` <cosas para hacer> `then` <los de mas del programa>.
: ?>64 ( n -- n ) dup 64 > if ." Mas que 64!" then ; \ ok
100 ?>64                                                  \ Mas que 64! ok

\ Else:
: ?>64 ( n -- n ) dup 64 > if ." Mas que 64!" else ." Menos que 64!" then ;
100 ?>64    \ Mas que 64! ok
20 ?>64     \ Menos que 64! ok 

\ ------------------------------------ Loops -----------------------------------

\ `do` también es una palabra que solamente compila.
: miloop ( -- ) 5 0 do cr ." Hola!" loop ; \ ok
miloop
\ Hola!
\ Hola!
\ Hola!
\ Hola!
\ Hola! ok

\ `do` espera dos números en el pila: el último numero y el primero numero.

\ Podemos recibir el valor del indice mientras damos vuelta con `i`:
: uno-a-12 ( -- ) 12 0 do i . loop ;     \ ok
uno-a-12                                 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 ok

\ `?do` funciona similarmente, pero salta el loop si el último y primero
\ números son iguales.
: cuadrados ( n -- ) 0 ?do i cuadrado . loop ;   \ ok
10 cuadrado                              \ 0 1 4 9 16 25 36 49 64 81 ok

\ cambiar el "paso" con `+loop`:
: treces ( n n -- ) ?do i . 3 +loop ;    \ ok
15 0 treces                             \ 0 3 6 9 12 ok

\ Indefinido loops empiezan `begin` <cosas para hacer> <bandera> `until`:
: death ( -- ) begin ." Ya hemos llegado?" 0 until ;    \ ok

\ ---------------------------- Variables y Memoria ----------------------------

\ Use `variable` declarar `edad` ser un variable.
variable edad    \ ok

\ Ahora escribimos 21 a edad con la palabra `!`.
21 edad !    \ ok

\ Por fin podemos imprimir nuestro variable usando la "leer" palabra `@`, que agregue el
\ valor a la pila, or usa `?` que lee y imprime todo juntos.
edad @ .    \ 21 ok
edad ?      \ 21 ok

\ Constantes son muy similar, pero no nos importa los direcciones de memoria:
100 constant PUNTA-QUE-AQUA-HIERVA   \ ok
PUNTA-QUE-AQUA-HIERVA .               \ 100 ok

\ ----------------------------------- Arrays -----------------------------------

\ Creando arrays es similar a variables, pero necesitamos alocar mas
\ memoria a ellos.

\ Puede usar `2 cells allot` para crear un array que es sea 3 cédulas de tamaño:
variable minumeros 2 cells allot    \ ok

\ Inicializar todos los valores a 0
minumeros 3 cells erase    \ ok

\ Alternativamente podemos usar `fill`:
minumeros 3 cells 0 fill

\ o podemos saltar todo arriba y inicializar con valores específicos:
create minumeros 64 , 9001 , 1337 , \ ok (el último `,` es importante!)

\ ...que es equivalente a:

\ Manualmente escribiendo valores a cada indice:
64 minumeros 0 cells + !      \ ok
9001 minumeros 1 cells + !    \ ok
1337 minumeros 2 cells + !    \ ok

\ Leyendo valores en particular array indices:
0 cells minumeros + ?    \ 64 ok
1 cells minumeros + ?    \ 9001 ok

\ Podemos simplificar un poco cuando hacemos una palabra que ayuda cuando manipulando arrays:
: de-arr ( n n -- n ) cells + ;    \ ok
minumeros 2 de-arr ?               \ 1337 ok

\ Que podemos usar cuando escribimos también:
20 minumeros 1 de-arr !    \ ok
minumeros 1 de-arr ?       \ 20 ok

\ ------------------------------ El Pila de Regreso ------------------------------

\ El pila de regreso se usa para retener punteros a cosas cuando palabras están
\ ejecutando otras palabras como loops.

\ Ya hemos visto un uso de esto: `i`, que duplica el primero del pila
\ de regreso. `i` es equivalente a `r@`.
: miloop ( -- ) 5 0 do r@ . loop ;    \ ok

\ También como leyendo, podemos agregar al pila de regreso y sacarlo:
5 6 4 >r swap r> .s    \ 6 5 4 ok

\ NOTA: Porque Forth usa el pila de regreso por punteros de palabras,  `>r` debe
\ siempre ser seguido por un `r>`.

\ ------------------------- Flotante Punto Operaciones --------------------------

\ La mayoría Forths evitan el uso de flotante punto operaciones.
8.3e 0.8e f+ f.    \ 9.1 ok

\ Usualmente agregamos al frente palabras con 'f' cuando usando flotantes:
variable miflotantevar    \ ok
4.4e miflotantevar f!     \ ok
miflotantevar f@ f.       \ 4.4 ok

\ --------------------------------- Notas al Final --------------------------------

\ Usando una palabra que no existe vaciara el pila. No obstante, también hay una palabra
\ específicamente por esto:
clearstack

\ vaciar la pantalla:
page

\ Cargando Forth archivos:
\ s" archivodeforth.fs" included

\ Puede listar cada palabra en el diccionario de Forth (pero es una lista gigante!):
\ words

\ Terminando Gforth:
\ bye

```

##Listo Para Mas?

* [Starting Forth](http://www.forth.com/starting-forth/)
* [Simple Forth](http://www.murphywong.net/hello/simple.htm)
* [Thinking Forth](http://thinking-forth.sourceforge.net/)
---
category: tool
tool: git
filename: LearnGit-es.txt
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translator:
    - ["Raúl Ascencio", "http://rscnt.github.io"]
lang: es-es

---

Git es un sistema de control de versiones distribuido diseñado para manejar
cualquier tipo de proyecto, ya sea grande o pequeño, con velocidad y eficiencia.

Git realiza esto haciendo "snapshots" del proyecto, con ello permite
versionar y administrar nuestro código fuente.

## Versionamiento, conceptos.

### ¿Qué es el control de versiones?
El control de versiones es un sistema que guarda todos los cambios realizados en
uno o varios archivos, a lo largo del tiempo.

### Versionamiento centralizado vs versionamiento distribuido.

+ El versionamiento centralizado se enfoca en sincronizar, rastrear, y respaldar
  archivos.
+ El versionamiento distribuido se enfoca en compartir los cambios realizados.
  Cada cambio tiene un único identificador.
+ El versionamiento distribuido no tiene una estructura definida, incluso se
  puede mantener el estilo de los repositorios SVN con git.

[Información adicional](http://git-scm.com/book/es/Empezando-Acerca-del-control-de-versiones)

### ¿Por qué usar Git?

* Se puede trabajar sin conexión.
* ¡Colaborar con otros es sencillo!.
* Derivar, crear ramas del proyecto (aka: Branching) es fácil.
* Combinar (aka: Merging)
* Git es rápido.
* Git es flexible.

## Arquitectura de Git.

### Repositorio

Un repositorio es un conjunto de archivos, directorios, registros, cambios (aka:
commits), y encabezados (aka: heads). Imagina que un repositorio es una clase,
y que sus atributos otorgan acceso al historial del elemento, además de otras
cosas.

Un repositorio esta compuesto por la carpeta .git y un "árbol de trabajo".

### Directorio .git (componentes del repositorio)

El directorio .git contiene todas las configuraciones, registros, branches, HEAD
y mas.

[Lista detallada.](http://es.gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### Directorio de trabajo (componentes del repositorio)

Es básicamente los directorios y archivos dentro del repositorio. La mayoría de
las veces se le llama "directorio de trabajo".

### Índice (componentes del directorio .git)

El índice es el área de inicio en git. Es básicamente la capa que separa el
directorio de trabajo del repositorio en git. Esto otorga a los desarrolladores
más poder sobre lo que se envía y se recibe del repositorio.

### Commit (aka: cambios)

Un commit es una captura de un conjunto de cambios, o modificaciones hechas en
el directorio de trabajo. Por ejemplo, si se añaden 5 archivos, se eliminan 2,
estos cambios se almacenarán en un commit (aka: captura). Este commit puede ser o
no ser enviado (aka: "pusheado") hacia un repositorio.

### Branch (rama)

Un "branch", es escencialmente un apuntador hacia el último commit (cambio
registrado) que se ha realizado. A medida que se realizan más commits, este
apuntador se actualizará automaticamente hacia el ultimo commit.

### "HEAD" y "head" (componentes del directorio .git)

"HEAD" es un apuntador hacia la rama (branch) que se esta utilizando. Un
repositorio solo puede tener un HEAD activo. En cambio "head", es un apuntador a
cualquier commit realizado, un repositorio puede tener cualquier número de
"heads".

### conceptos - recursos.

* [Git para informáticos](http://eagain.net/articles/git-for-computer-scientists/)
* [Git para diseñadores](http://hoth.entp.com/output/git_for_designers.html)


## Comandos.


### init

Crear un repositorio de git vacio. Las configuraciones, información almacenada y
demás son almacenadas en el directorio ".git".

```bash
$ git init
```

### config

Se utiliza para configurar las opciones ya sea globalmente, o solamente en el
repositorio.

```bash
# Imprime y guarda algunas variables de configuracion básicas. (Globalmente)
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "corre@gmail.com"
$ git config --global user.name "nombre"
```

[Más sobre git config.](http://git-scm.com/book/es/Personalizando-Git-Configuración-de-Git)

### help

Otorga un accceso rápido a una guía extremadamente detallada de cada comando en
git. O puede ser usada simplemente como un recordatorio de estos.

```bash
# Una vista rápida de los comandos disponibles.
$ git help

# Chequear todos los comandos disponibles
$ git help -a

# Obtener ayuda especifica de un comando - manual de usuario
# git help <comando>
$ git help add
$ git help commit
$ git help init
```

### status

Muestra las diferencias entre el archivo índice y el commit al cual apunta el
HEAD actualmente.


```bash
# Mostrará el "branch", archivos sin añadir al repo, cambios y otras
# diferencias
$ git status

# Devuelve ayuda sobre el comando status.
$ git help status
```

### add

Para añadir archivos al árbol (directorio, repositorio) de trabajo. Si no se
utiliza `git add`, los nuevos archivos no se añadirán al arbol de trabajo, por
lo que no se incluirán en los commits (cambios).

```bash
# Añade un archivo en el directorio de trabajo actual.
$ git add FooBar.java

# Añade un archivo que se encuentra bajo un directorio.
$ git add /directorio/del/archivo/Foo.c

# Soporte para expresiones regulares!
$ git add ./*.py
```

### branch

Administra las ramas del repositorio ("branches"). Puedes ver, editar, crear y
borrar ramas ("branches"), usando este comando.

```bash
# lista todas las ramas (remotas y locales)
$ git branch -a

# Añadir una nueva rama ("branch").
$ git branch branchNueva

# Eliminar una rama.
$ git branch -d branchFoo

# Renombrar una rama.
# git branch -m <anterior> <nuevo>
$ git branch -m youngling padawan

# Editar la descripcion de la rama.
$ git branch master --edit-description
```

### checkout

Actualiza todos los archivos en el directorio de trabajo para que sean igual que
las versiones almacenadas en el índice, o en un árbol de trabajo especificado.

```bash
# Despachar un repositorio. - Por defecto la master branch. (la rama principal llamada 'master')
$ git checkout
# Despacha una rama especifica.
$ git checkout padawan
# Crea una nueva rama y cambia hacia ella, es igual a utilizar: "git brach jedi; git checkout jedi"
$ git checkout -b jdei
```

### clone

Clona, o copia, un repositorio existente en un nuevo directorio. También añade el
seguimiento hacia las ramas existentes del repositorio que ha sido clonado, lo que
permite subir (push) los archivos hacia una rama remota.

```bash
# Clonar la repo de jquery.
$ git clone https://github.com/jquery/jquery.git
```

### commit

Almacena el contenido actual del índice en un nuevo "commit". Este
commit contiene los cambios hechos más un resumen proporcionado por el desarrollador.

```bash
# realizar un commit y añadirle un mensaje.
$ git commit -m "jedi anakin wil be - jedis.list"
```

### diff

Muestra las diferencias entre un archivo en el directorio de trabajo, el índice
y los commits.

```bash
# Muestra la diferencia entre un directorio de trabajo y el índice.
$ git diff

# Muestra la diferencia entre el índice y los commits más recientes.
$ git diff --cached

# Muestra la diferencia entre el directorio de trabajo y el commit más reciente.
$ git diff HEAD
```

### grep

Permite realizar una busqueda rápida en un repositorio.

Configuraciones opcionales:

```bash
# Gracias a Travis Jeffery por compartir lo siguiente.
# Permite mostrar numeros de lineas en la salida de grep.
$ git config --global grep.lineNumber true

# Realiza una búsqueda mas legible, incluyendo agrupación.
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Busca por "unaVariable" en todos los archivos .java
$ git grep 'unaVariable' -- '*.java'

# Busca por una línea que contenga "nombreArreglo" y "agregar" o "remover"
$ git grep -e 'nombreArreglo' --and \( -e agregar -e remover \)
```

Más ejemplos:

- [Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Muestra los commits (cambios) registrados en el repositorio.

```bash
# Muestra todos los commits.
$ git log

# Muestra un numero x de commits.
$ git log -n 10

# Muestra solo los commits que se han combinado en el historial.
$ git log --merges
```

### merge

Combina los cambios de commits realizados externamente dentro de la rama en la
que se trabaja.

```bash
# Combina la rama especificada en la rama actual.
$ git merge jediMaster

# Siempre genere un solo merge commit cuando se utiliza merge.
$ git merge --no-ff jediMaster
```

### mv

Renombra o mueve un archivo

```bash
# Renombrando un archivo.
$ git mv HolaMundo.c AdiosMundo.c

# Moviendo un archivo.
$ git mv HolaOtraVezMundo.c ./nuevo/directorio/NuevoArchivo.c

# Sustituye un archivo.
$ git mv -f archivoA archivoB
```

### pull

Trae los cambios de un repositorio y los combina en otro en una rama diferente.

```bash
# Actualiza el repositorio local, combinando los nuevos cambios
# de las ramas remotas "origin" y "master".
# git pull <remota> <rama>
$ git pull origin master
```

### push

Envía y combina los cambios de un repositorio local a un repositorio y rama remotos.

```bash
# Envía y combina cambios de un repositorio local hacia un repositorio remoto
# llamados "origin" y "master", respectivamente.
# git push <remota> <rama>
# git push => por defecto es lo mismo que poner =>  git push origin master
$ git push origin master
```

### rebase

Toma todos los cambios que fueron registrados en una rama, y los repite dentro
de otra rama. *No reescribe los commits que se han empujado antes a un repositorio público.*

```bash
# Integrar ramaExperimento dentro de la rama "master"
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch
```

[Información adicional.](http://git-scm.com/book/es/Ramificaciones-en-Git-Procedimientos-básicos-para-ramificar-y-fusionar)

### reset (precaución)

Reinicia el HEAD actual hacia un estado especificado. Esto permite deshacer
combinaciones (merges), pulls, commits, adds y más. Es un comando útil, pero
tambien peligroso si no se sabe lo que se hace.

```bash
# Reinicia el área principal, con el último cambio registrado. (deja los
# directorios sin cambios)
$ git reset

# Reinicia el área principal, con el último cambio registrado, y reescribe el
# directorio de trabajo.
$ git reset --hard

# Mueve la rama actual hacia el commit especificado (no realiza cambios a los
# directorios), todos los cambios aún existen el directorio.
$ git reset 31f2bb1

# Mueve la rama actual devuelta a un commit especificado, así como el
# directorio (borra todos los cambios que no fueron registrados y todos los
# cambios realizados después del commit especificado).
$ git reset --hard 31f2bb1
```

### rm

Lo contrario de git add, git rm elimina los archivos del directorio de trabajo
actual.

```bash
# Elimina FooBar.c
$ git rm FooBar.c

# Elimina un archivo de un directorio.
$ git rm /directorio/del/archivo/FooBar.c
```

## Información Adicional

* [tryGit - Una forma entretenida y rapida de aprender Git.](http://try.github.io/levels/1/challenges/1)

* [Udemy tutorial de Git: Una guía completa](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/)

* [Inmersión Git - Una visita guiada caminando a través de los fundamentos de git](http://gitimmersion.com/)

* [git-scm - Video-tutoriales](http://git-scm.com/videos)

* [git-scm - Documentacion](http://git-scm.com/book/es)

* [Atlassian Git - Tutoriales y Flujos de trabajo](https://www.atlassian.com/git/)

* [SalesForce Chuleta](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)

* [Git - La guía simple](http://rogerdudler.github.io/git-guide/index.html)

* [Pro Git](http://www.git-scm.com/book/en/v2)

* [Una introducción a Git y Github para principiantes (Tutorial)](http://product.hubspot.com/blog/git-and-github-tutorial-for-beginners)
---
name: Go
category: language
language: Go
lang: es-es
filename: learngo-es.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
    - ["Alexej Friesen", "https://github.com/heyalexej"]
translators:
    - ["Adrian Espinosa", "http://www.adrianespinosa.com"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Nacho Pacheco -- Feb/2015", "https://github.com/gitnacho"]
---

Go fue creado por la necesidad de hacer el trabajo rápidamente. No es la
última tendencia en informática, pero es la forma nueva y más rápida de
resolver problemas reales.

Tiene conceptos familiares de lenguajes imperativos con tipado estático.
Es rápido compilando y rápido al ejecutar, añade una concurrencia fácil de
entender para las CPUs de varios núcleos de hoy día, y tiene
características que ayudan con la programación a gran escala.

Go viene con una biblioteca estándar muy buena y una comunidad entusiasta.

```go
// Comentario de una sola línea
/* Comentario
   multilínea */

// La cláusula `package` aparece al comienzo de cada fichero fuente.
// `main` es un nombre especial que declara un ejecutable en vez de una
// biblioteca.
package main

// La instrucción `import` declara los paquetes de bibliotecas referidos
// en este fichero.
import (
	"fmt"      // Un paquete en la biblioteca estándar de Go.
	"io/ioutil" // Implementa algunas útiles funciones de E/S.
	m "math"   // Biblioteca de matemáticas con alias local m.
	"net/http" // Sí, ¡un servidor web!
	"strconv"  // Conversiones de cadenas.
)

// Definición de una función. `main` es especial. Es el punto de entrada
// para el ejecutable. Te guste o no, Go utiliza llaves.
func main() {
	// Println imprime una línea a stdout.
	// Llámalo con el nombre del paquete, fmt.
	fmt.Println("¡Hola mundo!")

	// Llama a otra función de este paquete.
	másAlláDelHola()
}

// Las funciones llevan parámetros entre paréntesis.
// Si no hay parámetros, los paréntesis siguen siendo obligatorios.
func másAlláDelHola() {
	var x int // Declaración de una variable.
 	          // Las variables se deben declarar antes de utilizarlas.
	x = 3     // Asignación de variable.
	// Declaración "corta" con := para inferir el tipo, declarar y asignar.
	y := 4
	suma, producto := aprendeMúltiple(x, y) // La función devuelve dos
											// valores.
	fmt.Println("suma:", suma, "producto:", producto) // Simple salida.
	aprendeTipos()                        // < y minutos, ¡aprende más!
}

// Las funciones pueden tener parámetros y (¡múltiples!) valores de
// retorno.
func aprendeMúltiple(x, y int) (suma, producto int) {
    return x + y, x * y // Devuelve dos valores.
}

// Algunos tipos incorporados y literales.
func aprendeTipos() {
	// La declaración corta suele darte lo que quieres.
	s := "¡Aprende Go!" // tipo cadena.
	s2 := `Un tipo cadena "puro" puede incluir
saltos de línea.` // mismo tipo cadena

	// Literal no ASCII. Los ficheros fuente de Go son UTF-8.
	g := 'Σ' // Tipo rune, un alias de int32, alberga un carácter unicode.
	f := 3.14195 // float64, el estándar IEEE-754 de coma flotante 64-bit.
	c := 3 + 4i  // complex128, representado internamente por dos float64.
	// Sintaxis var con iniciadores.
	var u uint = 7 // Sin signo, pero la implementación depende del tamaño
	               // como en int.
	var pi float32 = 22. / 7

	// Sintaxis de conversión con una declaración corta.
	n := byte('\n') // byte es un alias para uint8.

	// Los Arreglos tienen un tamaño fijo a la hora de compilar.
	var a4 [4]int           // Un arreglo de 4 ints, iniciados a 0.
	a3 := [...]int{3, 1, 5} // Un arreglo iniciado con un tamaño fijo de tres
							// elementos, con valores 3, 1 y 5.
	// Los Sectores tienen tamaño dinámico. Los arreglos y sectores tienen
	// sus ventajas y desventajas pero los casos de uso para los sectores
	// son más comunes.
	s3 := []int{4, 5, 9}     // Comparar con a3. No hay puntos suspensivos.
	s4 := make([]int, 4)     // Asigna sectores de 4 ints, iniciados a 0.
	var d2 [][]float64       // Solo declaración, sin asignación.
	bs := []byte("a sector") // Sintaxis de conversión de tipo.
	// Debido a que son dinámicos, los sectores pueden crecer bajo demanda.
	// Para añadir elementos a un sector, se utiliza la función incorporada
	// append().
	// El primer argumento es el sector al que se está anexando. Comúnmente,
	// la variable del arreglo se actualiza en su lugar, como en el 
	// siguiente ejemplo.
	sec := []int{1, 2 , 3}      // El resultado es un sector de longitud 3.
	sec = append(sec, 4, 5, 6)  // Añade 3 elementos. El sector ahora tiene una
								// longitud de 6.
	fmt.Println(sec) // El sector actualizado ahora es [1 2 3 4 5 6]
	// Para anexar otro sector, en lugar de la lista de elementos atómicos
	// podemos pasar una referencia a un sector o un sector literal como
	// este, con elipsis al final, lo que significa tomar un sector y
	// desempacar sus elementos, añadiéndolos al sector sec.
	sec = append(sec, []int{7, 8, 9} ...) // El segundo argumento es un
										  // sector literal.
	fmt.Println(sec)  // El sector actualizado ahora es [1 2 3 4 5 6 7 8 9]
	p, q := aprendeMemoria() // Declara p, q para ser un tipo puntero a
							 // int.
	fmt.Println(*p, *q)      // * sigue un puntero. Esto imprime dos ints.

	// Los Mapas son arreglos asociativos dinámicos, como los hash o
	// diccionarios de otros lenguajes.
	m := map[string]int{"tres": 3, "cuatro": 4}
	m["uno"] = 1

	// Las variables no utilizadas en Go producen error.
	// El guión bajo permite "utilizar" una variable, pero descartar su
	// valor.
	_, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
	// Esto cuenta como utilización de variables.
	fmt.Println(s, c, a4, s3, d2, m)

	aprendeControlDeFlujo() // Vuelta al flujo.
}

// Es posible, a diferencia de muchos otros lenguajes tener valores de
// retorno con nombre en las funciones.
// Asignar un nombre al tipo que se devuelve en la línea de declaración de
// la función nos permite volver fácilmente desde múltiples puntos en una
// función, así como sólo utilizar la palabra clave `return`, sin nada
// más.
func aprendeRetornosNombrados(x, y int) (z int) {
	z = x * y
	return // aquí z es implícito, porque lo nombramos antes.
}

// Go posee recolector de basura. Tiene punteros pero no aritmética de
// punteros. Puedes cometer errores con un puntero nil, pero no
// incrementando un puntero.
func aprendeMemoria() (p, q *int) {
	// Los valores de retorno nombrados q y p tienen un tipo puntero
	// a int.
	p = new(int) // Función incorporada que reserva memoria.
	// La asignación de int se inicia a 0, p ya no es nil.
	s := make([]int, 20) // Reserva 20 ints en un solo bloque de memoria.
	s[3] = 7             // Asigna uno de ellos.
	r := -2              // Declara otra variable local.
	return &s[3], &r     // & toma la dirección de un objeto.
}

func cálculoCaro() float64 {
	return m.Exp(10)
}

func aprendeControlDeFlujo() {
	// La declaración If requiere llaves, pero no paréntesis.
	if true {
		fmt.Println("ya relatado")
	}
	// El formato está estandarizado por la orden "go fmt."
	if false {
		// Abadejo.
	} else {
		// Relamido.
	}
	// Utiliza switch preferentemente para if encadenados.
	x := 42.0
	switch x {
	case 0:
	case 1:
	case 42:
		// Los cases no se mezclan, no requieren de "break".
	case 43:
		// No llega.
	}
	// Como if, for no utiliza paréntesis tampoco.
	// Variables declaradas en for e if son locales a su ámbito.
	for x := 0; x < 3; x++ { // ++ es una instrucción.
		fmt.Println("iteración", x)
	}
	// aquí x == 42.

	// For es la única instrucción de bucle en Go, pero tiene formas
	// alternativas.
	for { // Bucle infinito.
		break    // ¡Solo bromeaba!
		continue // No llega.
	}

	// Puedes usar `range` para iterar en un arreglo, un sector, una
	// cadena, un mapa o un canal.
	// `range` devuelve o bien, un canal o de uno a dos valores (arreglo,
	// sector, cadena y mapa).
	for clave, valor := range map[string]int{"uno": 1, "dos": 2, "tres": 3} {
		// por cada par en el mapa, imprime la clave y el valor
		fmt.Printf("clave=%s, valor=%d\n", clave, valor)
	}

	// Como en for, := en una instrucción if significa declarar y asignar
	// primero, luego comprobar y > x.
	if y := cálculoCaro(); y > x {
		x = y
    }
	// Las funciones literales son "cierres".
	granX := func() bool {
		return x > 100 // Referencia a x declarada encima de la instrucción
							// switch.
	}
	fmt.Println("granX:", granX()) // cierto (la última vez asignamos
											 // 1e6 a x).
	x /= 1.3e3                   // Esto hace a x == 1300
	fmt.Println("granX:", granX()) // Ahora es falso.

	// Es más las funciones literales se pueden definir y llamar en línea,
	// actuando como un argumento para la función, siempre y cuando:
	// a) la función literal sea llamada inmediatamente (),
	// b) el tipo del resultado sea del tipo esperado del argumento
	fmt.Println("Suma dos números + doble: ",
		func(a, b int) int {
			return (a + b) * 2
		}(10, 2)) // Llamada con argumentos 10 y 2
	// => Suma dos números + doble: 24

	// Cuando lo necesites, te encantará.
	goto encanto
encanto:

	aprendeFunciónFábrica() // func devolviendo func es divertido(3)(3)
	aprendeADiferir()       // Un rápido desvío a una importante palabra clave.
	aprendeInterfaces()     // ¡Buen material dentro de poco!
}

func aprendeFunciónFábrica() {
	// Las dos siguientes son equivalentes, la segunda es más práctica
	fmt.Println(instrucciónFábrica("día")("Un bello", "de verano"))

	d := instrucciónFábrica("atardecer")
	fmt.Println(d("Un hermoso", "de verano"))
	fmt.Println(d("Un maravilloso", "de verano"))
}

// Los decoradores son comunes en otros lenguajes. Lo mismo se puede hacer
// en Go con funciónes literales que aceptan argumentos.
func instrucciónFábrica(micadena string) func(antes, después string) string {
	return func(antes, después string) string {
		return fmt.Sprintf("¡%s %s %s!", antes, micadena, después) // nueva cadena
	}
}

func aprendeADiferir() (ok bool) {
	// las instrucciones diferidas se ejecutan justo antes de que la
	// función regrese.
	defer fmt.Println("las instrucciones diferidas se ejecutan en orden inverso (PEPS).")
	defer fmt.Println("\nEsta línea se imprime primero debido a que")
	// Defer se usa comunmente para cerrar un fichero, por lo que la
	// función que cierra el fichero se mantiene cerca de la función que lo
	// abrió.
	return true
}

// Define Stringer como un tipo interfaz con un método, String.
type Stringer interface {
	String() string
}

// Define par como una estructura con dos campos int, x e y.
type par struct {
	x, y int
}

// Define un método en el tipo par. Par ahora implementa a Stringer.
func (p par) String() string { // p se conoce como el "receptor"
	// Sprintf es otra función pública del paquete fmt.
	// La sintaxis con punto se refiere a los campos de p.
	return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func aprendeInterfaces() {
	// La sintaxis de llaves es una "estructura literal". Evalúa a una
	// estructura iniciada. La sintaxis := declara e inicia p a esta
	// estructura.
	p := par{3, 4}
	fmt.Println(p.String()) // Llama al método String de p, de tipo par.
	var i Stringer          // Declara i como interfaz de tipo Stringer.
	i = p                   // Válido porque par implementa Stringer.
	// Llama al metodo String de i, de tipo Stringer. Misma salida que
	// arriba.
	fmt.Println(i.String())

	// Las funciones en el paquete fmt llaman al método String para
	// consultar un objeto por una representación imprimible de si
	// mismo.
	fmt.Println(p) // Salida igual que arriba. Println llama al método
				   // String.
	fmt.Println(i) // Salida igual que arriba.
	aprendeNúmeroVariableDeParámetros("¡gran", "aprendizaje", "aquí!")
}

// Las funciones pueden tener número variable de argumentos.
func aprendeNúmeroVariableDeParámetros(misCadenas ...interface{}) {
	// Itera en cada valor de los argumentos variables.
	// El espacio en blanco aquí omite el índice del argumento arreglo.
	for _, parámetro := range misCadenas {
		fmt.Println("parámetro:", parámetro)
	}

	// Pasa el valor de múltiples variables como parámetro variadic.
	fmt.Println("parámetros:", fmt.Sprintln(misCadenas...))
	aprendeManejoDeError()
}

func aprendeManejoDeError() {
	// ", ok" forma utilizada para saber si algo funcionó o no.
	m := map[int]string{3: "tres", 4: "cuatro"}
	if x, ok := m[1]; !ok { // ok será falso porque 1 no está en el mapa.
		fmt.Println("nada allí")
	} else {
		fmt.Print(x) // x sería el valor, si estuviera en el mapa.
	}
	// Un valor de error comunica más información sobre el problema aparte
	// de "ok".
	if _, err := strconv.Atoi("no-int"); err != nil { // _ descarta el
																	  // valor
		// Imprime "strconv.ParseInt: parsing "no-int": invalid syntax".
		fmt.Println(err)
	}
	// Revisaremos las interfaces más adelante. Mientras tanto...
	aprendeConcurrencia()
}

// c es un canal, un objeto de comunicación concurrente seguro.
func inc(i int, c chan int) {
	c <- i + 1 // <- es el operador "enviar" cuando aparece un canal a la
				  // izquierda.
}

// Utilizaremos inc para incrementar algunos números concurrentemente.
func aprendeConcurrencia() {
	// Misma función make utilizada antes para crear un sector. Make asigna
	// e inicia sectores, mapas y canales.
	c := make(chan int)
	// Inicia tres rutinasgo concurrentes. Los números serán incrementados
	// concurrentemente, quizás en paralelo si la máquina es capaz y está
	// correctamente configurada. Las tres envían al mismo canal.
	go inc(0, c) // go es una instrucción que inicia una nueva rutinago.
	go inc(10, c)
	go inc(-805, c)
	// Lee los tres resultados del canal y los imprime.
	// ¡No se puede saber en que orden llegarán los resultados!
	fmt.Println(<-c, <-c, <-c) // Canal a la derecha, <- es el operador
										// "recibe".

	cs := make(chan string)       // Otro canal, este gestiona cadenas.
	ccs := make(chan chan string) // Un canal de canales cadena.
	go func() { c <- 84 }()       // Inicia una nueva rutinago solo para
											// enviar un valor.
	go func() { cs <- "verboso" }() // Otra vez, para cs en esta ocasión.
	// Select tiene una sintaxis parecida a la instrucción switch pero cada
	// caso involucra una operación con un canal. Selecciona un caso de
	// forma aleatoria de los casos que están listos para comunicarse.
	select {
	case i := <-c: // El valor recibido se puede asignar a una variable,
		fmt.Printf("es un %T", i)
	case <-cs:     // o el valor se puede descartar.
		fmt.Println("es una cadena")
	case <-ccs:    // Canal vacío, no está listo para la comunicación.
		fmt.Println("no sucedió.")
	}

	// En este punto un valor fue devuelto de c o cs. Una de las dos
	// rutinasgo que se iniciaron se ha completado, la otrá permancerá
	// bloqueada.

	aprendeProgramaciónWeb() // Go lo hace. Tú también quieres hacerlo.
}

// Una simple función del paquete http inicia un servidor web.
func aprendeProgramaciónWeb() {
// El primer parámetro es la direccinón TCP a la que escuchar.
	// El segundo parámetro es una interfaz, concretamente http.Handler.
	go func() {
		err := http.ListenAndServe(":8080", par{})
		fmt.Println(err) // no ignora errores
	}()
	consultaAlServidor()
}

// Hace un http.Handler de par implementando su único método, ServeHTTP.
func (p par) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Sirve datos con un método de http.ResponseWriter.
	w.Write([]byte("¡Aprendiste Go en Y minutos!"))
}

func consultaAlServidor() {
	resp, err := http.Get("http://localhost:8080")
	fmt.Println(err)
	defer resp.Body.Close()
	cuerpo, err := ioutil.ReadAll(resp.Body)
	fmt.Printf("\nEl servidor web dijo: `%s`\n", string(cuerpo))
}
```

## Más información

La raíz de todas las cosas sobre Go es el
[sitio web oficial de Go](http://golang.org/).
Allí puedes seguir el tutorial, jugar interactivamente y leer mucho más.

La definición del lenguaje es altamente recomendada.  Es fácil de leer y
sorprendentemente corta (como la definición del lenguaje Go en estos
días).

Puedes jugar con el código en el
[parque de diversiones Go](https://play.golang.org/p/ncRC2Zevag).  ¡Trata
de cambiarlo y ejecutarlo desde tu navegador!  Ten en cuenta que puedes
utilizar [https://play.golang.org]( https://play.golang.org) como un
[REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) para probar
cosas y el código en el navegador, sin ni siquiera instalar Go.

En la lista de lecturas para estudiantes de Go está el
[código fuente de la biblioteca estándar](http://golang.org/src/pkg/). 
Ampliamente documentado, que demuestra lo mejor del legible y comprensible
Go, con su característico estilo y modismos.  ¡O puedes hacer clic en un
nombre de función en [la documentación](http://golang.org/pkg/) y
aparecerá el código fuente!

Otro gran recurso para aprender Go está en
[Go con ejemplos](http://goconejemplos.com/).
---
language: Groovy
contributors:
    - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"]
translators:
    - ["Jhoon Saravia", "https://github.com/jhoon"]
lang: es-es
filename: groovy-es.html
---

Groovy - Un lenguaje dinámico para la plataforma Java [Leer más aquí.](http://www.groovy-lang.org/)

```groovy

/*
  Hora de configurar:

  1) Instala GVM - http://gvmtool.net/
  2) Instala Groovy: gvm install groovy
  3) Inicia la consola de groovy escribiendo: groovyConsole

*/

//  Los comentarios de una sola línea inician con dos barras inclinadas
/*
Los comentarios multilínea se ven así.
*/

// Hola Mundo
println "Hola mundo!"

/*
  Variables:

  Puedes asignar valores a variables para usarlas después
*/

def x = 1
println x

x = new java.util.Date()
println x

x = -3.1499392
println x

x = false
println x

x = "Groovy!"
println x

/*
  Mapas y Colecciones
*/

// Creando una lista vacía
def technologies = []

/*** Agregando elementos a la lista ***/

// Como si fuera Java
technologies.add("Grails")

// Doble símbolo de menor agrega un elemento y, además, retorna la lista
technologies << "Groovy"

// Agregando múltiples elementos
technologies.addAll(["Gradle","Griffon"])

/*** Quitando elementos de la lista ***/

// Como si fuera Java
technologies.remove("Griffon")

// La resta también funciona
technologies = technologies - 'Grails'

/*** Iterando Listas ***/

// Para iterar sobre los elementos de una Lista
technologies.each { println "Technology: $it"}
technologies.eachWithIndex { it, i -> println "$i: $it"}

/*** Revisando los contenidos de una Lista ***/

// Evaluar si la lista contiene elemento(s) (boolean)
contained = technologies.contains( 'Groovy' )

// O
contained = 'Groovy' in technologies

// Evaluar por múltiples contenidos
technologies.containsAll(['Groovy','Grails'])

/*** Ordenando Listas ***/

// Para ordenar una Lista (modifica la lista original)
technologies.sort()

// Para ordenarla sin modificar la original, se puede hacer:
sortedTechnologies = technologies.sort( false )

/*** Manipulando Listas ***/

// Reemplazar todos los elementos en la lista
Collections.replaceAll(technologies, 'Gradle', 'gradle')

// Mezclar una lista
Collections.shuffle(technologies, new Random())

// Limpiar una lista
technologies.clear()

// Creando un mapa vacío
def devMap = [:]

// Agregando valores
devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
devMap.put('lastName','Perez')

// Iterar sobre los elementos del mapa
devMap.each { println "$it.key: $it.value" }
devMap.eachWithIndex { it, i -> println "$i: $it"}

// Evaluar si el mapa contiene una llave
assert devMap.containsKey('name')

// Evaluar si el mapa contiene un valor
assert devMap.containsValue('Roberto')

// Para obtener las llaves del mapa
println devMap.keySet()

// Para obtener los valores del mapa
println devMap.values()

/*
  Groovy Beans

  GroovyBeans son JavaBeans pero usando una sintaxis mucho más simple

  Cuando Groovy es compilado a código de bytes, las siguientes reglas son usadas:

    * Si el nombre es declarado con un modificador de acceso (public, private o
      protected), entonces se genera un campo.

    * Un nombre declarado sin modificador de acceso genera un campo privado con
      un getter y un setter públicos (ej: una propiedad)

    * Si una propiedad es declarada como final, entonces el campo privado es creado
      como final y no se genera un setter.

    * Puedes declarar una propiedad y también sus propios getter y setter.

    * Puedes declarar una propiedad y un campo del mismo nombre, en ese caso, la
      propiedad usará ese campo.

    * Si quieres una propiedad private o proteceted, tienes que proveer tus propios
      getter y setter, los cuales deben ser declarados private o protected.

    * Si accedes a una propiedad desde dentro de la clase, la propiedad es definida
      en tiempo de compilación con this implícito o explícito (por ejemplo, this.foo
      o simplemente foo), Groovy accederá al campo directamente en vez de usar el 
      getter y setter.

    * Si accedes a una propiedad que no existe usando foo explícito o implícito, entonces
      Groovy accederá a la propiedad a través de la clase meta, que puede fallar en
      tiempo de ejecución.

*/

class Foo {
    // propiedad de solo lectura
    final String name = "Roberto"

    // propiedad de solo lectura, con getter público y setter como protected
    String language
    protected void setLanguage(String language) { this.language = language }

    // propiedad de tipo dinámico
    def lastName
}

/*
  Derivación Lógica e Iteraciones
*/

// Groovy soporta la clásica sintaxis de if - else
def x = 3

if(x==1) {
    println "One"
} else if(x==2) {
    println "Two"
} else {
    println "X greater than Two"
}

// Groovy también soporta el uso del operador ternario:
def y = 10
def x = (y > 1) ? "worked" : "failed"
assert x == "worked"

// ¡Groovy también soporta 'El Operador Elvis'!
// En lugar de usar el operador ternario:

displayName = user.name ? user.name : 'Anonymous'

// Podemos escribirlo así:
displayName = user.name ?: 'Anonymous'

// Iteración con For
// Iterando en un rango numérico
def x = 0
for (i in 0 .. 30) {
    x += i
}

// Iterando sobre una lista
x = 0
for( i in [5,3,2,1] ) {
    x += i
}

// Iterando sobre un arreglo
array = (0..20).toArray()
x = 0
for (i in array) {
    x += i
}

// Iterando sobre un mapa
def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
x = 0
for ( e in map ) {
    x += e.value
}

/*
  Operadores

  Para la lista de los operadores que Groovy soporta, visita:
  http://www.groovy-lang.org/operators.html#Operator-Overloading

  Operadores Groovy útiles
*/
// Operador de propagación:  invocar una acción en todos los elementos de un objeto agregado.
def technologies = ['Groovy','Grails','Gradle']
technologies*.toUpperCase() // equivale a: technologies.collect { it?.toUpperCase() }

// Operador de navegación segura: usado para evitar un NullPointerException.
def user = User.get(1)
def username = user?.username


/*
  Closures
  Un Closure en Groovy es como un "bloque de código" o un puntero a un método. Es una 
  porci´øn de código que es definida y ejecutada en un punto futuro en el tiempo.

  Más información en: http://www.groovy-lang.org/closures.html
*/
// Ejemplo:
def clos = { println "Hello World!" }

println "Executing the Closure:"
clos()

// Pasando parámetros a un closure
def sum = { a, b -> println a+b }
sum(2,4)

// Los Closures pueden referir a variables no listadas en sus listas de parámetros
def x = 5
def multiplyBy = { num -> num * x }
println multiplyBy(10)

// Si tienes un Closure que toma un solo argumento, puedes omitir la
// definición del parámetro en el Closure
def clos = { print it }
clos( "hi" )

/*
  Groovy puede memorizar los resultados de un Closure [1][2][3]
*/
def cl = {a, b ->
    sleep(3000) // simula algún proceso que consume tiempo
    a + b
}

mem = cl.memoize()

def callClosure(a, b) {
    def start = System.currentTimeMillis()
    mem(a, b)
    println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs."
}

callClosure(1, 2)
callClosure(1, 2)
callClosure(2, 3)
callClosure(2, 3)
callClosure(3, 4)
callClosure(3, 4)
callClosure(1, 2)
callClosure(2, 3)
callClosure(3, 4)

/*
  Expando

  La clase Expando es un bean dinámico para que podamos agregar propiedades y closures
  como métodos a una instancia de esta clase

  http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/
  def user = new Expando(name:"Roberto")
  assert 'Roberto' == user.name

  user.lastName = 'Pérez'
  assert 'Pérez' == user.lastName

  user.showInfo = { out ->
      out << "Name: $name"
      out << ", Last name: $lastName"
  }

  def sw = new StringWriter()
  println user.showInfo(sw)


/*
  Metaprogramación (MOP)
*/

// Usando ExpandoMetaClass para agregar comportamiento
String.metaClass.testAdd = {
    println "we added this"
}

String x = "test"
x?.testAdd()

// Interceptando llamadas a métodos
class Test implements GroovyInterceptable {
    def sum(Integer x, Integer y) { x + y }

    def invokeMethod(String name, args) {
        System.out.println "Invoke method $name with args: $args"
    }
}

def test = new Test()
test?.sum(2,3)
test?.multiply(2,3)

// Groovy soporta propertyMissing para lidiar con intentos de resolución de propiedades.
class Foo {
   def propertyMissing(String name) { name }
}
def f = new Foo()

assertEquals "boo", f.boo

/*
  TypeChecked y CompileStatic
  Groovy, por naturaleza, es y siempre será un lenguaje dinámico pero soporta
  typechecked y compilestatic

  Más información: http://www.infoq.com/articles/new-groovy-20
*/
// TypeChecked
import groovy.transform.TypeChecked

void testMethod() {}

@TypeChecked
void test() {
    testMeethod()

    def name = "Roberto"

    println naameee

}

// Otro ejemplo:
import groovy.transform.TypeChecked

@TypeChecked
Integer test() {
    Integer num = "1"

    Integer[] numbers = [1,2,3,4]

    Date date = numbers[1]

    return "Test"

}

// ejemplo de CompileStatic:
import groovy.transform.CompileStatic

@CompileStatic
int sum(int x, int y) {
    x + y
}

assert sum(2,5) == 7


```

## Más recursos

[Documentación de Groovy](http://www.groovy-lang.org/documentation.html)

[Consola Web de Groovy](http://groovyconsole.appspot.com/)

Únete a un [Groovy user group](http://www.groovy-lang.org/usergroups.html)

## Libros

* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook)

* [Groovy in Action] (http://manning.com/koenig2/)

* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)

[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html
---
language: Hack
contributors:
    - ["Stephen Holdaway", "https://github.com/stecman"]
    - ["David Lima", "https://github.com/davelima"]
translators:
    - ["César Suárez", "https://github.com/csuarez"]
lang: es-es
filename: learnhack-es.hh
---

Hack es un superconjunto de PHP que se ejecuta en una máquina virtual llamada HHVM. Hack es casi totalmente compatible con código PHP ya existente y añade varias características típicas de los lenguajes de programación estáticamente tipados.

En este artículo sólo se cubren las características específicas de Hack. Los detalles sobre la sintaxis de PHP están en el [artículo sobre PHP](http://learnxinyminutes.com/docs/php/) de esta misma web.

```php
<?hh

// La sintaxis de Hack sólo se habilita para los ficheros que comienzan con
// un marcador <?hh. Estos marcadores no pueden intercalarse con código HTML,
// tal como se puede hacer con <?php. Al usar el marcador "<?hh //strict" el
// comprobador de tipado en modo estricto se pone en modo estricto.

// Indicando el tipo de parámetros escalares
function repeat(string $word, int $count)
{
    $word = trim($word);
    return str_repeat($word . ' ', $count);
}

// Indicando el tipo que devuelve una función
function add(...$numbers) : int
{
    return array_sum($numbers);
}

// Las funciones que no devuelven nada usan el tipo "void"
function truncate(resource $handle) : void
{
    // ...
}

// Al determinar un tipo, hay que indicar explícitamente si permite el valor 
// NULL
function identity(?string $stringOrNull) : ?string
{
    return $stringOrNull;
}

// Se puede especificar el tipo de las propiedades de una clase
class TypeHintedProperties
{
    public ?string $name;
    
    protected int $id;

    private float $score = 100.0;

	// El comprobador de tipos de Hack fuerza que las propiedades tipadas
	// tengan un valor por defecto o que estén asignadas en el constructor
    public function __construct(int $id)
    {
        $this->id = $id;
    }
}


// Funciones anónimas concisas (lambdas)
$multiplier = 5;
array_map($y ==> $y * $multiplier, [1, 2, 3]);


// Genéricos
class Box<T>
{
    protected T $data;

    public function __construct(T $data) {
        $this->data = $data;
    }

    public function getData(): T {
        return $this->data;
    }
}

function openBox(Box<int> $box) : int
{
    return $box->getData();
}


// Shapes
//
// Hack añade el concepto de shape para definir estructuras similares a
// vectores, pero con un conjunto de claves garantizado y tipado
type Point2D = shape('x' => int, 'y' => int);

function distance(Point2D $a, Point2D $b) : float
{
    return sqrt(pow($b['x'] - $a['x'], 2) + pow($b['y'] - $a['y'], 2));
}

distance(
    shape('x' => -1, 'y' => 5),
    shape('x' => 2, 'y' => 50)
);


// Alias de tipos
//
// Hack permite crear alias para hacer que los tipos complejos sean más legibles
newtype VectorArray = array<int, Vector<int>>;

// Una tupla que contiene dos enteros
newtype Point = (int, int);

function addPoints(Point $p1, Point $p2) : Point
{
    return tuple($p1[0] + $p2[0], $p1[1] + $p2[1]);
}

addPoints(
    tuple(1, 2),
    tuple(5, 6)
);


// Enumerados de primera clase
enum RoadType : int
{
    Road = 0;
    Street = 1;
    Avenue = 2;
    Boulevard = 3;
}

function getRoadType() : RoadType
{
    return RoadType::Avenue;
}


// Promoción de argumentos en constructores
//
// Para evitar repetir una y otra vez la definición de constructores que
// sólo asignan propiedades, Hack añade una sintaxis concisa para definir
// propiedades junto al constructor.
class ArgumentPromotion
{
    public function __construct(public string $name,
                                protected int $age,
                                private bool $isAwesome) {}
}

class WithoutArgumentPromotion
{
    public string $name;

    protected int $age;

    private bool $isAwesome;

    public function __construct(string $name, int $age, bool $isAwesome)
    {
        $this->name = $name;
        $this->age = $age;
        $this->isAwesome = $isAwesome;
    }
}


// Multitarea cooperativa
//
// "async" y "await" son dos palabras claves nuevas para realizar multi-tarea.
// Esto no implica que se usen hilos, sólo permiten transferir el control de la 
// ejecución.
{
    for ($i = $start; $i <= $end; $i++) { 
        echo "$i ";

        // Da a otras tareas la oportunidad de hacer algo
        await RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0);
    }
}

// Esto imprime "1 4 7 2 5 8 3 6 9"
AwaitAllWaitHandle::fromArray([
    cooperativePrint(1, 3),
    cooperativePrint(4, 6),
    cooperativePrint(7, 9)
])->getWaitHandle()->join();


// Atributos
//
// Los atributos son una especie de metadatos para funciones. Hack implementa
// algunos atributos especiales para introducir esta característica.

// El atributo especial __Memoize hace que el resultado de la función se cacheé.
<<__Memoize>>
function doExpensiveTask() : ?string
{
    return file_get_contents('http://example.com');
}

// Esta función se va a ejecutar sólo una vez:
doExpensiveTask();
doExpensiveTask();


// El atributo __ConsistentConstruct indica al comprobador de tipos de Hack que
// asegure que la signatura de __construct sea la misma para todas las
// subclases.
<<__ConsistentConstruct>>
class ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
        // ...
    }

    public function someMethod()
    {
        // ...
    }
}

class ConsistentBar extends ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
		 // El comprobador de tipos de Hack fuerza que los constructores de 
		 // los padres sean llamados.
        parent::__construct($x, $y);

        // ...
    }

    // La anotación __Override es un atributo opcional para que el comprobador
    // de tipos fuerce que ese método esté sobrecargando un método de un padre
    // o de un trait. Sino, fallará.
    <<__Override>>
    public function someMethod()
    {
        // ...
    }
}

class InvalidFooSubclass extends ConsistentFoo
{
	// Este constructor no coincide con el padre y causará el siguiente error:
	//
	//  "This object is of type ConsistentBaz. It is incompatible with this 
	//   object of type ConsistentFoo because some of their methods are 
	//   incompatible"
    public function __construct(float $x)
    {
        // ...
    }

    // Usando la anotación __Override en un método que no sobrecarga nada se
    // producirá el siguiente error:
    //
    //  "InvalidFooSubclass::otherMethod() is marked as override; no non-private
    //   parent definition found or overridden parent is defined in non-<?hh 
    //   code"
    <<__Override>>
    public function otherMethod()
    {
        // ...
    }
}


// Los traits pueden implementar interfaces (PHP no soporta esto).
interface KittenInterface
{
    public function play() : void;
}

trait CatTrait implements KittenInterface
{
    public function play() : void
    {
        // ...
    }
}

class Samuel
{
    use CatTrait;
}


$cat = new Samuel();
$cat instanceof KittenInterface === true; // True

```

## Más información

Para obtener una explicación más detallada de las características que añade Hack a PHP visita la página de [referencia de Hack](http://docs.hhvm.com/manual/en/hacklangref.php) o la [página oficial de Hack](http://hacklang.org/) para información de caracter más general.

Visita la [página oficial de HHVM](http://hhvm.com/) para ver las instrucciones de su instalación.

También puedes visitar la [sección de características de PHP no soportadas por Hack](http://docs.hhvm.com/manual/en/hack.unsupported.php) para más detalles sobre la retrocompatibilidad entre Hack y PHP.
---
language: haml
filename: learnhaml-es.haml
contributors:
  - ["Simon Neveu", "https://github.com/sneveu"]
translators:
    - ["Camilo Garrido", "http://www.twitter.com/hirohope"]
lang: es-es
---

Haml es un lenguage de marcas principalmente usado con Ruby, que de forma simple y limpia describe el HTML de cualquier documento web sin el uso de código en linea. Es una alternativa popular respecto a usar el lenguage de plantilla de Rails (.erb) y te permite embeber código Ruby en tus anotaciones.

Apunta a reducir la repetición en tus anotaciones cerrando los tags por ti, basándose en la estructura de identación de tu código. El resultado es una anotación bien estructurada, que no se repite, lógica y fácil de leer.

También puedes usar Haml en un proyecto independiente de Ruby, instalando la gema Haml en tu máquina y usando la línea de comandos para convertirlo en html.

$ haml archivo_entrada.haml archivo_salida.html


```haml
/ -------------------------------------------
/ Identación
/ -------------------------------------------

/
  Por la importancia que la identación tiene en cómo tu código es traducido,
  la identación debe ser consistente a través de todo el documento. Cualquier
  diferencia en la identación lanzará un error. Es una práctica común usar dos
  espacios, pero realmente depende de tí, mientras sea consistente.


/ -------------------------------------------
/ Comentarios
/ -------------------------------------------

/ Así es como un comentario se ve en Haml.

/
  Para escribir un comentario multilínea, identa tu código a comentar de tal forma
  que sea envuelto por por una barra.


-# Este es un comentario silencioso, significa que no será traducido al código en absoluto


/ -------------------------------------------
/ Elementos Html
/ -------------------------------------------

/ Para escribir tus tags, usa el signo de porcentaje seguido por el nombre del tag
%body
  %header
    %nav

/ Nota que no hay tags de cierre. El código anterior se traduciría como
  <body>
    <header>
      <nav></nav>
    </header>
  </body>

/ El tag div es un elemento por defecto, por lo que pueden ser escritos simplemente así
.foo

/ Para añadir contenido a un tag, añade el texto directamente después de la declaración
%h1 Headline copy

/ Para escribir contenido multilínea, anídalo.
%p
  Esto es mucho contenido que podríamos dividirlo en dos
  líneas separadas.

/
  Puedes escapar html usando el signo ampersand y el signo igual ( &= ).
  Esto convierte carácteres sensibles en html a su equivalente codificado en html.
  Por ejemplo

%p
  &= "Sí & si"

/ se traduciría en 'Sí &amp; si'

/ Puedes desescapar html usando un signo de exclamación e igual ( != )
%p
  != "Así es como se escribe un tag párrafo <p></p>"

/ se traduciría como 'Así es como se escribe un tag párrafo <p></p>'

/ Clases CSS puedes ser añadidas a tus tags, ya sea encadenando .nombres-de-clases al tag
%div.foo.bar

/ o como parte de un hash Ruby
%div{:class => 'foo bar'}

/ Atributos para cualquier tag pueden ser añadidos en el hash
%a{:href => '#', :class => 'bar', :title => 'Bar'}

/ Para atributos booleanos asigna el valor verdadero 'true'
%input{:selected => true}

/ Para escribir atributos de datos, usa la llave :dato  con su valor como otro hash
%div{:data => {:attribute => 'foo'}}


/ -------------------------------------------
/ Insertando Ruby
/ -------------------------------------------

/
  Para producir un valor Ruby como contenido de un tag, usa un signo igual
  seguido por código Ruby

%h1= libro.nombre

%p
  = libro.autor
  = libro.editor


/ Para correr un poco de código Ruby sin traducirlo en html, usa un guión
- libros = ['libro 1', 'libro 2', 'libro 3']

/ Esto te permite hacer todo tipo de cosas asombrosas, como bloques de Ruby
- libros.shuffle.each_with_index do |libro, indice|
  %h1= libro

  if libro do
    %p Esto es un libro

/
  Nuevamente, no hay necesidad de añadir los tags de cerrado en el código, ni siquiera para Ruby
  La identación se encargará de ello por tí.


/ -------------------------------------------
/ Ruby en linea / Interpolación de Ruby
/ -------------------------------------------

/ Incluye una variable Ruby en una línea de texto plano usando #{}
%p Tu juego con puntaje más alto es #{mejor_juego}


/ -------------------------------------------
/ Filtros
/ -------------------------------------------

/
  Usa un signo dos puntos para definir filtros Haml, un ejemplo de filtro que
  puedes usar es :javascript, el cual puede ser usado para escribir javascript en línea.

:javascript
  console.log('Este es un <script> en linea');

```

## Recusros adicionales

- [¿Qué es HAML? (en inglés)](http://haml.info/) - Una buena introducción que hace mejor el trabajo de explicar los beneficios de usar haml.
- [Documentación Oficial (en inglés)](http://haml.info/docs/yardoc/file.REFERENCE.html) - Si deseas ir un poco más profundo.
---
language: Haskell
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["Jorge Antonio Atempa", "http://www.twitter.com/atempa09"]
filename: haskell-es.md
lang: es-es
---

Haskell fue diseñado como lenguaje de programación funcional práctico y puro. Es famoso por sus mónadas y su sistema de tipos, pero siempre regreso a él debido a su elegancia. Haskell hace la codificación una verdadera alegría para mí.

```haskell
-- Para comentar una sola línea utiliza dos guiones.
{- Para comentar múltiples líneas puedes encerrarlas
en un bloque como este.
-}

----------------------------------------------------
-- 1. Tipos de datos primitivos y Operadores
----------------------------------------------------

-- Tienes números a tu disposición
3 -- 3

-- Matématicas, es lo que esperas
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- Por defecto la división no devuelve un entero
35 / 4 -- 8.75

-- Para la división entera utiliza
35 `div` 4 -- 8

-- Valores booleanos
True
False

-- Operaciones booleanas
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- En los ejemplos superiores, `not` es una función que toma un valor.
-- Haskell no necesita paréntisis para las llamadas a funciones...todos los argumentos
-- son enlistados después de la función. Entonces el patrón general es:
-- func arg1 arg2 arg3...
-- Observa la sección de funciones para obtener información de como escribir tu propia función.

-- Cadenas y caracteres
"Esto es una cadena."
'a' -- caracter
'No puedes utilizar comillas simples para cadenas.' -- ¡error!

-- Concatenación de cadenas
"¡Hola " ++ "mundo!" -- "¡Hola mundo!"

-- Una cadena es una lista de caracteres
['H', 'o', 'l', 'a'] -- "Hola"
"Esto es una cadena" !! 0 -- 'E'


----------------------------------------------------
-- 2. Listas y Tuplas
----------------------------------------------------

-- Cada elemento en una lista debe ser del mismo tipo.
-- Estas dos listas son iguales:
[1, 2, 3, 4, 5]
[1..5]

-- Los rangos son versátiles.
['A'..'F'] -- "ABCDEF"

-- Puedes crear un paso en un rango.
[0,2..10] -- [0, 2, 4, 6, 8, 10]
[5..1] -- Esto no funciona debido a que Haskell incrementa por defecto.
[5,4..1] -- [5, 4, 3, 2, 1]

-- indexación en una lista
[0..] !! 5 -- 5

-- También tienes listas infinitas en Haskell!
[1..] -- una lista de todos los números naturales

-- Las listas infinitas funcionan porque Haskell tiene "lazy evaluation". Esto significa
-- que Haskell solo evalúa las cosas cuando lo necesita. Así que puedes pedir
-- el elemento 1000 de tú lista y Haskell te devolverá:

[1..] !! 999 -- 1000

-- Y ahora Haskell ha evaluado elementos 1 - 1000 de esta lista...pero el
-- resto de los elementos de esta lista "infinita" ¡no existen todavía! Haskell no lo hará
-- en realidad los evalúa hasta que los necesita.

-- uniendo dos listas
[1..5] ++ [6..10]

-- añadiendo a la cabeza de la lista
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- más operaciones con listas
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- Listas por comprensión
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- Listas por comprensión utilizando condicionales
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- Cada elemento en una tupla puede ser de diferente tipo, pero una tupla tiene
-- longitud fija.
-- Ejemplo de una tupla:
("haskell", 1)

-- acceder a los elementos (por ejemplo una tupla de longitud 2)
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. Funciones
----------------------------------------------------
-- Una función simple que recibe dos variables
add a b = a + b

-- Nota: Si estas utilizando ghci (el interprete de Haskell)
-- Necesitas utilizar `let`, por ejemplo
-- let add a b = a + b

-- Utilizando la función
add 1 2 -- 3

-- También puedes llamar a la función enmedio de dos argumentos
-- con acentos abiertos:
1 `add` 2 -- 3

-- ¡También puedes definir funciones sin tener que utilizar letras! De este modo
-- ¡Tú defines tus propios operadores! Aquí esta un operador que realiza
-- una división entera
(//) a b = a `div` b
35 // 4 -- 8

-- Guardas: son una manera fácil para ramificar funciones
fib x
  | x < 2 = 1
  | otherwise = fib (x - 1) + fib (x - 2)

-- La coincidencia de patrones es similar. Aquí hemos dado tres diferentes
-- definiciones para fib. Haskell llamará automáticamente la primer
-- función que coincide con el patrón del valor.
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- Coincidencia de patrones en tuplas:
foo (x, y) = (x + 1, y + 2)

-- Coincidencia de patrones en listas. Aquí `x` es el primer elemento
-- en una lista, y `xs` es el resto de la lista. Podemos escribir
-- nuestra propia función map:
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- Funciones anónimas son creadas con una diagonal invertida seguido de
-- todos los argumentos.
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- utilizando pliegues (llamado `inject` en algunos lenguajes) con una función
-- anónima. foldl1 significa pliegue por la izquierda, y usa el primer valor 
-- en la lista como el valor inicial para el acumulador.
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. Más funciones
----------------------------------------------------

-- aplicación parcial: si no quieres pasar todos los argumentos a una función,
-- esta es "parcialmente aplicada". Esto significa que retorna una función que toma
-- el resto de los argumentos.

add a b = a + b
foo = add 10 -- foo es actualmente una función que toma un número y suma 10 a esta
foo 5 -- 15

-- Otra manera de escribir los mismo
foo = (+10)
foo 5 -- 15

-- composición de funciones
-- el (.) encadena funciones.
-- Por ejemplo, aquí foo es una función que toma un valor. Y se le suma 10,
-- posteriormente multiplica el resultado por 5, y devuelve el resultado final.
foo = (*5) . (+10)

-- (5 + 10) * 5 = 75
foo 5 -- 75

-- fijación de precedencia 
-- Haskell tiene otro operador llamado `$`. Este operador aplica a una función 
-- para un parámetro dado. En contraste a la aplicación de función estándar,  
-- la cúal tiene prioridad más alta posible de 10 y es asociativa por la izquierda, 
-- el operador `$` tiene prioridad de 0 y es asociativa por la derecha. Tal que
-- una baja prioridad significa que la expresión a su derecha es aplicada como parámetro a la función a su izquierda.

-- antes
even (fib 7) -- false

-- equivalentemente
even $ fib 7 -- false

-- composición de funciones
even . fib $ 7 -- false


----------------------------------------------------
-- 5. Firma de tipos
----------------------------------------------------

-- Haskell tiene un fuerte sistema de tipado, y cada cosa tiene una firma de tipo.

-- Algunos tipos básicos:
5 :: Integer
"hola" :: String
True :: Bool

-- Las funciones tienen muchos tipos.
-- `not` toma un booleano y devuelve un booleano:
-- not :: Bool -> Bool

-- Aquí, esta función toma dos argumentos:
-- add :: Integer -> Integer -> Integer

-- Cuando defines un valor, es una buena práctica escribir su tipo en una línea superior:
double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. Control de flujo y Expresiones If
----------------------------------------------------

-- expressiones if en una sola línea
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- expressiones if en múltiples líneas, la identación es importante
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- expressiones case: Aquí se muestra como analizar los argumentos 
-- desde línea de comandos
case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"

-- Haskell no tiene ciclos; en lugar de esto utiliza recursión.
-- map aplica una función sobre cada elemento en un arreglo

map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- tú puedes crear una función utilizando map
for array func = map func array

-- y entonces utilizarla
for [0..5] $ \i -> show i

-- también podríamos haberlo escrito de esta manera:
for [0..5] show

-- Puedes utilizar foldl o foldr para reducir una lista
-- foldl <fn> <valor inicial> <lista>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- Esto es lo mismo que
(2 * (2 * (2 * 4 + 1) + 2) + 3)

-- foldl es izquierda, foldr es derecha
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- Esto es los mismo que
(2 * 1 + (2 * 2 + (2 * 3 + 4)))

----------------------------------------------------
-- 7. Tipos de datos
----------------------------------------------------

-- Por ejemplo, para crear tu propio tipo de dato en Haskell

data Color = Rojo | Azul | Verde

-- Ahora puedes utilizarlo en una función:


say :: Color -> String
say Rojo = "¡Es Rojo!"
say Azul = "¡Es Azul!"
say Verde =  "¡Es Verde!"

-- Tus tipos de datos pueden tener parámetros también:

data Maybe a = Nothing | Just a

-- Estos son todos de tipo Maybe
Just "hello"    -- de tipo `Maybe String`
Just 1          -- de tipo `Maybe Int`
Nothing         -- de tipo `Maybe a` para cualquier `a`

----------------------------------------------------
-- 8. Haskell IO
----------------------------------------------------

-- Mientras que IO no puede ser explicado plenamente sin explicar las mónadas,
-- no es difícil explicar lo suficiente para ponerse en marcha.

-- Cuando un programa en Haskell se ejecuta, `main` es
-- llamado. Este debe devolver un valor de tipo `IO ()`. Por ejemplo:

main :: IO ()
main = putStrLn $ "¡Hola, cielo! " ++ (say Blue)
-- putStrLn tiene tipo String -> IO ()

-- Es más fácil de hacer IO si puedes implementar tu programa como
-- una función de String a String. La función
--    interact :: (String -> String) -> IO ()
-- recibe como entrada un texto,  ejecuta una función e imprime
-- una salida.

countLines :: String -> String
countLines = show . length . lines

main' = interact countLines

-- Puedes pensar en el valor de tipo `IO ()` como la representación 
-- de una secuencia de acciones que la computadora hace, al igual que
-- un programa escrito en un lenguaje imperativo. Podemos utilizar
-- la notación `do` para encadenar acciones. Por ejemplo:

sayHello :: IO ()
sayHello = do
   putStrLn "¿Cual es tu nombre?"
   name <- getLine -- obtenemos un valor y lo proporcionamos a "name"
   putStrLn $ "Hola, " ++ name

-- Ejercicio: escribe tu propia version de `interact` que solo lea 
--           una linea como entrada.

-- Nunca se ejecuta el código en `sayHello`, sin embargo. La única
-- acción que siempre se ejecuta es el valor de `main`.
-- Para ejecutar `sayHello` comenta la definición anterior de `main`
-- y sustituyela por:
--   main = sayHello

-- Vamos a entender mejor como funciona la función `getLine` cuando
-- la utilizamos. Su tipo es:
--    getLine :: IO String
-- Puedes pensar en el valor de tipo `IO a` como la representación
-- programa que generará un valor de tipo `a`
-- cuando es ejecutado (además de cualquier otra cosa que haga). Podemos
-- almacenar y reutilizar el valor usando `<-`. También podemos
-- crear nuestra propia acción de tipo `IO String`:

action :: IO String
action = do
   putStrLn "Esta es una linea."
   input1 <- getLine
   input2 <- getLine
   -- El tipo de la sentencia `do` es la de su última línea.
   -- `return` no es una palabra clave, sino simplemente una función
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- Podemos usar esto sólo como usabamos `getLine`:

main'' = do
    putStrLn "¡Volveré a repetir dos líneas!"
    result <- action
    putStrLn result
    putStrLn "Esto es todo, ¡amigos!"

-- El tipo `IO` es un ejemplo de una "mónada". La forma en que Haskell utiliza una monada
-- permite que sea un lenguaje puramente funcional. Cualquier función que
-- interactue con el mundo exterior (por ejemplo usar IO) obtiene una marca `IO`
-- como su firma de tipo. Esto nos permite pensar qué funciones son "puras"
-- (que no interactuan con el mundo exterior o modifican el estado) y que funciones no lo son.

-- Esta es una poderosa característica, porque es una manera fácil de ejecutar funciones puras
-- concurrentemente; entonces, la concurrencia en Haskell es muy fácil.


----------------------------------------------------
-- 9. El interprete de comandos de Haskell
----------------------------------------------------

-- Para comenzar escribe desde la terminal `ghci`.
-- Ahora puede escribir código en Haskell. Para cualquier valor nuevo
-- que necesites crear utiliza `let`:

let foo = 5

-- Puedes inspeccionar el tipo de cualquier valor con `:t`:

>:t foo
foo :: Integer

-- Puedes ejecutar acciones de tipo `IO ()`

> sayHello
¿Cual es tu nombre?
Amigo
Hola, Amigo

```

Existe mucho más de Haskell, incluyendo clases de tipos y mónadas. Estas son
las grandes ideas que hacen a Haskell divertido. Te dejamos un ejemplo final
de Haskell: una implementación del algoritmo QuickSort:

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

Haskell es fácil de instalar. Obtenlo [aquí](http://www.haskell.org/platform/).

Usted puede encontrar más información en:
[Learn you a Haskell](http://learnyouahaskell.com/) o
[Real World Haskell](http://book.realworldhaskell.org/) o
[Aprende Haskell por el bien de todos](http://aprendehaskell.es/)
---
language: html
filename: learnhtml-es.html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
translators:
    - ["Gino Amaury", "https://github.com/ginoamaury"]
lang: es-es
---

HTML significa Lenguaje de marcado de hipertexto (HyperText Markup Language).
Este es un lenguaje usado para escribir páginas en la web (WWW). 
Este es un lenguaje de marcado, es usado para escribir páginas web usando código para indicar cómo se debe mostrar el texto y los datos.
En efecto, los archivos html son simples archivos de texto.
Qué es esto de marcado? es un método para organizar los datos de la página encerrandolos con etiquetas de apertura y cierre.
Este marcado sirve para darle significancia al texto que éste encierra.
Como en otros lenguajes computacionales, HTML tiene varias versiones. Aquí hablaremos acerca de HTML5.

**Nota :**  Puedes probrar las diferentes etiquetas y elementos a medida que progresas en un tutorial en un sitio como  [codepen](http://codepen.io/pen/) con el fin de ver sus efectos, entender como funcionan y familiarizarse con el lenguaje.
Este artículo está centrado principalmente en la sintaxis HTML y algunos tips de importancia.


```html
<!-- los comentarios están encerrados como en esta línea! -->

<!-- #################### Las Etiquetas #################### -->
   
<!-- Este es un ejemplo de un archivo HTML que analizaremos! -->

<!doctype html>
	<html>
		<head>
			<title>Mi Sitio</title>
		</head>
		<body>
			<h1>Hola, Mundo!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">ven mira lo que esto muestra. </a>
			<p>Esto es un párrafo</p>
			<p>Este es otro párrafo</p>
			<ul>
				<li>Este es un elemento de una lista no numerada (lista de viñetas)</li>
				<li>Este es otro ítem</li>
				<li>Y este es el último ítem de la lista</li>
			</ul>
		</body>
	</html>

<!-- En un archivo HTML siempre inicia indicando le al buscador que esta es una página HTML. -->
<!doctype html>

<!-- Después de esto, iniciamos abriendo una etiqueta html <html> -->
<html>

<!-- Cuando termine el archivo cerraremos la etiqueta así </html>. -->
</html>

<!-- Después de la etiqueta final nada aparecerá o podrá aparecer -->

<!-- Dentro (Entre las etiquetas de apertura y cierre <html></html>), encontraremos: -->

<!-- Un encabezado definido por <head> (Este debe ser cerrado por </head>). -->

<!-- El encabezado contiene alguna descripción y información adicional que no se muestra; estos son los metadatos. -->

<head>
	<title>Mi Sitio</title><!-- La etiqueta <title> Indica al buscador el título a mostrar en la ventana del buscador en la barra de título y en el nombre de la pestaña. -->
</head>

<!-- Después de la sección del encabezado <head> , Encontraremos la etiqueta de cuerpo - <body> -->
<!-- Hasta este punto. no hay nada descrito para que se muestre en la ventana del navegador -->
<!-- Debemos llenar el cuerpo con el contenido que se mostrará -->

<body>
	<h1>Hola, Mundo!</h1> <!-- La etiqueta <h1> crea un título. -->
	<!-- También tenemos subtítulos para <h1> desde la más importante <h2> a la más precisa <h6> -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">ven mira lo que esto muestra.</a> <!-- Un hipervínculo a la URL dada por el atributo href="" -->
	<p>Esto es un párrafo.</p> <!-- La etiqueta <p> nos permite incluir texto en nuestra página HTML -->
	<p>Este es otro párrafo.</p>
	<ul> <!-- La etiqueta <ul> crea una lista de viñetas -->
	<!-- Para tener una lista numerada usamos la etiqueta <ol> dando 1. para el primer elemento, 2. para el segundo, etc. -->
		<li>Este es un elemento de una lista no numerada (lista de viñetas)</li>
		<li>Este es otro ítem</li>
		<li>Y este es el último ítem de la lista</li>
	</ul>
</body>

<!-- Y esto es todo, la creación de un archivo HTML puede ser muy simple. -->

<!-- Sin embargo, es posible añadir muchos otros tipos de etiquetas HTML  -->

<!-- Para insertar una imagen -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- La fuente donde se localiza la imagen se indica utilizando el atributo src=""-->
<!-- La fuente puede ser una URL o incluso una ruta a una archivo en tu computador. -->

<!-- También es posible crear una tabla -->

<table> <!-- Abrimos una etiqueta o elemento tabla <table> -->
	<tr> <!-- <tr> Nos permite crear una fila. -->
		<th>Primer encabezado</th> <!-- <th> Nos permite dar un título a una columna de una tabla -->
		<th>Segundo encabezado</th>
	</tr>
	<tr>
		<td>Primera fila, primera columna</td> <!-- <td> nos permite crear una celda  -->
		<td>Primera fila, segunda columna</td>
	</tr>
	<tr>
		<td>Segunda fila, primera columna</td>
		<td>Segunda fila, segunda columna</td>
	</tr>
</table>

```

## Uso

HTML es escrito en archivos que terminan con (extensión) `.html`.

## Para aprender más! 

* [wikipedia](https://es.wikipedia.org/wiki/HTML)
* [HTML tutorial](https://developer.mozilla.org/es/docs/Web/HTML)
* [W3School (EN)](http://www.w3schools.com/html/html_intro.asp)
---
language: java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
    - ["Camilo Garrido", "http://www.twitter.com/hirohope"]
lang: es-es
filename: LearnJava-es.java
---

Java es un lenguage de programación de propósito general, concurrente, basado en clases y
orientado a objetos.
[Lee más aquí.](http://docs.oracle.com/javase/tutorial/java/index.html)

```java
// Comentarios de una sóla línea comienzan con //
/*
Comentarios multilínea lucen así
*/
/**
Comentarios JavaDoc lucen así. Suelen describir la clase o varios atributos
de una clase.
*/

// Importa la clase ArrayList dentro del paquete java.util
import java.util.ArrayList;
// Importa todas las clases dentro del paquete java.security
import java.security.*;

// Cada archivo .java contiene una clase pública, con el mismo nombre del archivo.
public class AprendeJava {

    // Un programa debe tener un método 'main' como punto de entrada
    public static void main (String[] args) {

        // Usa System.out.println para imprimir líneas
        System.out.println("¡Hola mundo!");
        System.out.println(
            "Entero (int): " + 10 +
            " Doble (double): " + 3.14 +
            " Booleano (boolean): " + true);

        // Para imprimir sin el salto de línea, usa System.out.print
        System.out.print("Hola ");
        System.out.print("Mundo");


        ///////////////////////////////////////
        // Tipos & Variables
        ///////////////////////////////////////

        // Declara una variable usando <tipo> <nombre> [
        // Byte - Entero complemento a dos con signo de 8-bit
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short - Entero complemento a dos con signo de 16-bit
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - Entero complemento a dos con signo de 32-bit
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int fooInt = 1;

        // Long - Entero complemento a dos con signo de 64-bit
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L es usado para denotar que el valor de esta variable es del tipo Long;
        // cualquier cosa sin ella es tratado como un entero por defecto.

        // Nota: Java no tiene tipos sin signo

        // Float - Número de coma flotante IEEE 754 de precisión simple de 32-bit
        float fooFloat = 234.5f;
        // f es usado para denotar qeu el valor de esta variable es del tipo float;
        // de otra manera es tratado como un double.

        // Double - Número de coma flotante IEEE 754 de precisión doble de 64-bit
        double fooDouble = 123.4;

        // Boolean - true & false
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - Un simple carácter unicode de 16-bit
        char fooChar = 'A';

        // Usa 'final' para hacer inmutable las variables
        final int HORAS_QUE_TRABAJO_POR_SEMANA = 9001;

        // Strings
        String fooString = "¡Mi String está aquí!";

        // \n es un carácter escapado que inicia una nueva línea
        String barString = "¿Imprimiendo en una nueva linea?\n¡Ningun problema!";
        // \t es un carácter escapado que añade un carácter tab
        String bazString = "¿Quieres añadir un 'tab'?\t¡Ningun problema!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // Arreglos
        //El tamaño del arreglo debe decidirse en la declaración
        //El formato para la declaración de un arreglo es la siguiente:
        //<tipo_de_dato> [] <nombre_variable> = new <tipo_de_dato>[<tamaño_arreglo>];
        int [] arreglo_de_enteros = new int[10];
        String [] arreglo_de_strings = new String[1];
        boolean [] arreglo_de_booleanos = new boolean[100];

        // Otra forma de declarar & inicializar un arreglo
        int [] y = {9000, 1000, 1337};

        // Indexación de un arreglo - Accediendo un elemento
        System.out.println("arreglo_de_enteros @ 0: " + arreglo_de_enteros[0]);

        // Arreglos comienzan su indexación en cero y son mutables
        arreglo_de_enteros[1] = 1;
        System.out.println("arreglo_de_enteros @ 1: " + arreglo_de_enteros[1]); // => 1

        // Otros para echar un vistazo
        // ArrayLists - Son como arreglos excepto que ofrecen más funcionalidades
        //              y el tamaño es mutable
        // LinkedLists
        // Maps
        // HashMaps

        ///////////////////////////////////////
        // Operadores
        ///////////////////////////////////////
        System.out.println("\n->Operadores");

        int i1 = 1, i2 = 2; // Abreviación para múltiples declaraciones

        // La aritmética es directa
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (0.5 truncado)

        // Módulo
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Operadores de comparación
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // ¡Operaciones a nivel de bits!
        /*
        ~       Complemento unario bit a bit
        <<      Deplazamiento hacia la izquierda con signo
        >>      Deplazamiento hacia la derecha con signo
        >>>     Deplazamiento hacia la derecha sin signo
        &       AND lógico
        ^       OR lógico exclusivo
        |       OR lógico inclusivo
        */

        // Incrementos
        int i = 0;
        System.out.println("\n->Incrementos y reducciones");
        System.out.println(i++); //i = 1. Post-incremento
        System.out.println(++i); //i = 2. Pre-incremento
        System.out.println(i--); //i = 1. Post-reducción
        System.out.println(--i); //i = 0. Pre-reducción

        ///////////////////////////////////////
        // Estructuras de Control
        ///////////////////////////////////////
        System.out.println("\n->Estructuras de Control");

        // Condiciones 'if' son como en c
        int j = 10;
        if (j == 10){
            System.out.println("Me imprimieron");
        } else if (j > 10) {
            System.out.println("A mi no");
        } else {
            System.out.println("A mi tampoco");
        }

        // Ciclos 'while'
        int fooWhile = 0;
        while(fooWhile < 100)
        {
            //System.out.println(fooWhile);
            //Incrementar el contador
            //Iteró 99 veces, fooWhile 0->99
            fooWhile++;
        }
        System.out.println("Valor fooWhile: " + fooWhile);

        // Ciclos 'do while'
        int fooDoWhile = 0;
        do
        {
            //System.out.println(fooDoWhile);
            //Incrementar el contador
            //Iteró 99 veces, fooDoWhile 0->99
            fooDoWhile++;
        }while(fooDoWhile < 100);
        System.out.println("Valor fooDoWhile: " + fooDoWhile);

        // Ciclos 'for'
        int fooFor;
        //Estructura del ciclo 'for' => for(<declaración_de_inicio>; <condicional>; <paso>)
        for(fooFor=0; fooFor<10; fooFor++){
            //System.out.println(fooFor);
            //Iteró 10 veces, fooFor 0->9
        }
        System.out.println("Valor fooFor: " + fooFor);

        // Switch Case
        // Un 'switch' funciona con un tipo de dato byte, short, char e int
        // También funciona con tipos enumerados (discutido en tipos Enum),
        // la clase String y unas pocas clases especiales que envuelven
        // tipos primitivos: Character, Byte, Short e Integer.
        int mes = 3;
        String mesString;
        switch (mes){
            case 1:
                    mesString = "Enero";
                    break;
            case 2:
                    mesString = "Febrero";
                    break;
            case 3:
                    mesString = "Marzo";
                    break;
            default:
                    mesString = "Algun otro mes";
                    break;
        }
        System.out.println("Resultado switch Case: " + mesString);


        ///////////////////////////////////////
        // Convirtiendo Tipos de Datos y Conversión de Tipos
        ///////////////////////////////////////

        // Convirtiendo datos

        // Convertir String a Integer
        Integer.parseInt("123");//retorna una versión entera de "123"

        // Convertir Integer a String
        Integer.toString(123);//retorna una versión string de 123

        // Para otras conversiones fíjate en las siguientes clases
        // Double
        // Long
        // String

        // Conversión de tipos
        // También puedes convertir objetos java, hay muchos detalles
        // con unos pocos conceptos intermedios
        // No dudes en verlos acá
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Clases y Funciones
        ///////////////////////////////////////

        System.out.println("\n->Clases & Funciones");

        // (A continuación la definición de una clase Bicicleta)

        // Usa 'new' para instanciar una clase
        Bicicleta excursion = new Bicicleta();

        // Llama métodos del objeto
        excursion.aumentarVelocidad(3); // Siempre deberías usar metodos 'set' (establecer) y 'get' (obtener)
        excursion.setRitmo(100);

        // 'toString' es una convención para mostrar los valores de este objeto.
        System.out.println("informacion de la excursion: " + excursion.toString());

        ///////////////////////////////////////
        // Genéricos
        ///////////////////////////////////////

        // Utilizando genéricos (a partir de Java 1.5) es posible detectar en tiempo de
        // compilación errores de tipado (en versiones anteriores se detectarían como error
        // de ejecución)

        List<String> v = new ArrayList<String>();
        v.add("test");
        String s = v.get(0); // Si intentamos recuperar s como otro tipo diferente a String
                             // (por ejemplo, un Integer) obtendríamos un error de compilación

    } // Fin del método 'main'
} // Fin de la clase AprendeJava


// Puedes incluir otras clases no públicas en un archivo .java


// Sintaxis de declaración de clases:
// <public/private/protected> class <nombre_de_la_clase>{
//    //variables_de_clase, constructores, todas las funciones.
//    //las funciones son llamadas como métodos en Java.
// }

class Bicicleta {

    // Campos/Variables de Bicicleta
    public int ritmo; // Public: Puede ser accedido desde cualquier parte
    private int velocidad;  // Private: Accesible sólo desde esta clase
    protected int engranaje; // Protected: Accesible desde esta clases y sus subclases
    String nombre; // default: Sólo accesible desde este paquete

    // Constructores son la manera de crear clases
    // Este es un constructor por defecto
    public Bicicleta() {
        engranaje = 1;
        ritmo = 50;
        velocidad = 5;
        nombre = "Bontrager";
    }

    // Este es un constructor específico (contiene argumentos)
    public Bicicleta(int ritmoInicial, int velocidadInicial, int engranajeInicial, String nombre) {
        this.engranaje = engranajeInicial;
        this.ritmo = ritmoInicial;
        this.velocidad = velocidadInicial;
        this.nombre = nombre;
    }

    // Sintaxis de función:
    // <public/private/protected> <tipo_de_retorno> <nombre_funcion>(<argumentos>)

    // Las clases de Java usualmente implementan métodos 'get' (obtener) y 'set' (establecer) para sus campos

    // Sintaxis de declaración de métodos
    // <alcance> <tipo_de_retorno> <nombre_metodo>(<argumentos>)
    public int getRitmo() {
        return ritmo;
    }

    // Métodos void no requieren retornar
    public void setRitmo(int nuevoValor) {
        ritmo = nuevoValor;
    }

    public void setEngranaje(int nuevoValor) {
        engranaje = nuevoValor;
    }

    public void aumentarVelocidad(int incremento) {
        velocidad += incremento;
    }

    public void disminuirVelocidad(int reduccion) {
        velocidad -= reduccion;
    }

    public void setNombre(String nuevoNombre) {
        nombre = nuevoNombre;
    }

    public String getNombre() {
        return nombre;
    }

    //Método para mostrar los valores de los atributos de este objeto.
    @Override
    public String toString() {
        return "engranaje: " + engranaje +
                " ritmo: " + ritmo +
                " velocidad: " + velocidad +
                " nombre: " + nombre;
    }
} // fin clase Bicicleta

// PennyFarthing es una subclase de Bicicleta
class PennyFarthing extends Bicicleta {
    // (Penny Farthings son esas bicicletas con una gran rueda forntal.
    // No tienen engranajes.)

    public PennyFarthing(int ritmoInicial, int velocidadInicial){
        // Llama al constructor del padre con super
        super(ritmoInicial, velocidadInicial, 0, "PennyFarthing");
    }

    // Deberías marcar un método que estás sobre escribiendo con una @anotacion
    // Para aprender más sobre que son y el propósito de las anotaciones
    // echa un vistazo acá: http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setEngranaje(int engranaje) {
        engranaje = 0;
    }

}

```

## Más Lectura

Estos links son sólo para tener un entendimiento del tema, no dudes en
usar Google y encontrar ejemplos más específicos

Otros temas a investigar:

* [Java Tutorial Trail from Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Java Access level modifiers](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Object-Oriented Programming Concepts](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Inheritance](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Abstraction](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Interfaces](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html)
---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
translators:
    - ["Daniel Zendejas","https://github.com/DanielZendejas"]
filename: javascript-es.js
lang: es-es
---
Tutorial de JavaScript en español.

JavaScript fue creado por Brendan Eich en 1995 mientras trabajaba en Netscape. 
Su intención original era crear un lenguaje simple para sitios web, complementándolo
con Java para aplicaciones más complejas. Debido a su integracion estrecha con sitios
web y soporte por defecto de los navegadores modernos se ha vuelto mucho más común 
para front-end que Java.

Sin embargo, JavaScript no sólo se limita a los navegadores web: Node.js, un proyecto que proporciona un entorno de ejecución independiente para el motor V8 de Google Chrome, se está volviendo más y más popular.

¡La retroalimentación es bienvenida! Puedes encontrarme en: 
[@adambrenecki](https://twitter.com/adambrenecki), o
[adam@brenecki.id.au](mailto:adam@brenecki.id.au).

```js
// Los comentarios en JavaScript son los mismos como comentarios en C. 

//Los comentarios de una sola línea comienzan con //,
/* y los comentarios multilínea comienzan
   y terminan con */

// Cada sentencia puede ser terminada con punto y coma ;
hazAlgo();

// ... aunque no es necesario, ya que el punto y coma se agrega automáticamente
// cada que se detecta una nueva línea, a excepción de algunos casos.
hazAlgo()

// Dado que esta práctica puede llevar a resultados inesperados, seguiremos agregando
// punto y coma en esta guía.

///////////////////////////////////
// 1. Números, Strings y Operadores

// JavaScript tiene un solo tipo de número (doble de 64-bit IEEE 754).
// Así como con Lua, no te espantes por la falta de enteros: los dobles tienen 52 bits
// de mantisa, lo cual es suficiente para guardar enteros de hasta 9✕10¹⁵.
3; // = 3
1.5; // = 1.5

// Toda la aritmética básica funciona como uno esperaría.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// Incluyendo divisiones con resultados no enteros.
5 / 2; // = 2.5

// Las operaciones con bits también funcionan; cuando ejecutas una operación con bits
// el número flotante se convierte a entero con signo *hasta* 32 bits.
1 << 2; // = 4

// La jerarquía de las operaciones se aplica con paréntesis.
(1 + 3) * 2; // = 8

// Hay tres casos especiales de valores con los números:
Infinity; // por ejemplo: 1/0
-Infinity; // por ejemplo: -1/0
NaN; // por ejemplo: 0/0

// También hay booleanos:
true;
false;

// Los Strings se pueden crear con ' o ".
'abc';
"Hola, mundo";

// La negación se aplica con la expresión ! 
!true; // = false
!false; // = true

// Para comprobar una igualdad se usa ===
1 === 1; // = true
2 === 1; // = false

// Para comprobar una desigualdad se usa !==
1 !== 1; // = false
2 !== 1; // = true

// Más comparaciones
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Los Strings se concatenan con +
"¡Hola " + "mundo!"; // = "¡Hola mundo!"

// y se comparan con < y con >
"a" < "b"; // = true

// Los tipos no importan con el operador ==...
"5" == 5; // = true
null == undefined; // = true

// ...a menos que uses ===
"5" === 5; // = false
null === undefined; // false

// Los Strings funcionan como arreglos de caracteres
// Puedes acceder a cada caracter con la función charAt()
"Este es un String".charAt(0);  // = 'E'

// ...o puedes usar la función substring() para acceder a pedazos más grandes
"Hola Mundo".substring(0, 4); // = "Hola"

// length es una propiedad, así que no uses ()
"Hola".length; // = 4

// También hay null y undefined
null; // usado para indicar una falta de valor deliberada
undefined; // usado para indicar que un valor no está presente actualmente
           // (aunque undefined es un valor en sí mismo)

// false, null, undefined, NaN, 0 y "" es false; todo lo demás es true.
// Note que 0 es false y "0" es true, a pesar de que 0 == "0".
// Aunque 0 === "0" sí es false.

///////////////////////////////////
// 2. Variables, Arrays y Objetos

// Las variables se declaran con la palabra var. JavaScript cuenta con tipado dinámico,
// así que no se necesitan aplicar tipos. La asignación se logra con el operador =.
var miPrimeraVariable = 5;

// si no escribes la palabra var no se marcará ningún error...
miSegundaVariable = 10;

// ...pero tu variable se declarará en el ámbito global, no en el ámbito
// en el que se definió.

// Las variables que no están aún asignadas tienen el valor undefined.
var miTerceraVariable; // = undefined

// Existen atajos para realizar operaciones aritméticas:
miPrimeraVariable += 5; // equivalente a miPrimeraVariable = miPrimeraVariable + 5;
						// miPrimeraVariable ahora es 10
miPrimeraVariable *= 10; // ahora miPrimeraVariable es 100

// Y atajos aún más cortos para sumar y restar 1
miPrimeraVariable++; // ahora miPrimeraVariable es 101
miPrimeraVariable--; // de vuelta a 100

// Los arreglos son listas ordenadas de valores, de cualquier tipo.
var miArreglo = ["Hola", 45, true];

// Los miembros de un arreglo pueden ser accesados con la sintaxis 
// de indices dentro de corchetes [].
// Los índices empiezan en cero.
miArreglo[1]; // = 45

// Los arreglos son mutables y pueden cambiar de longitud.
miArreglo.push("Mundo");
miArreglo.length; // = 4

// Agregar/Modificar en un determinado índice
miArreglo[3] = "Hola";

// Los objetos en JavaScript son equivalentes a los 'diccionarios' o 'mapas' en otros
// lenguajes: una colección de pares llave/valor desordenada.
var miObjeto = {llave1: "Hola", llave2: "Mundo"};

// Las llaves son strings, pero no se necesitan las comillas si son un identificador
// válido de JavaScript. Los valores pueden ser de cualquier tipo.
var miObjeto = {miLlave: "miValor", "mi otra llave": 4};

// Los atributos de los objetos también pueden ser accesadas usando
//  la sintaxis de corchetes,
miObjeto["mi otra llave"]; // = 4

// ... o usando la sintaxis de punto, dado que la llave es un identificador válido.
miObjeto.miLlave; // = "miValor"

// Los objetos son mutables; los valores pueden ser cambiados y se pueden
// agregar nuevas llaves.
miObjeto.miTerceraLlave = true;

// Si intentas acceder con una llave que aún no está asignada tendrás undefined.
miObjeto.miCuartaLlave; // = undefined

///////////////////////////////////
// 3. Lógica y estructura de control

// La sintaxis de esta sección es casi idéntica a la de Java. 

// La estructura if funciona de la misma forma.
var contador = 1;
if (contador == 3){
    // evaluar si contador es igual a 3
} else if (contador == 4){
    // evaluar si contador es igual a 4
} else {
    // evaluar si contador no es igual a 3 ni a 4
}

// De la misma forma la estructura while.
while (true){
    // ¡Loop infinito!
}

// La estructura Do-while es igual al while, excepto que se ejecuta al menos una vez.
var input
do {
    input = conseguirInput();
} while (!esValido(input))

// la esctructura for es la misma que la de C y Java:
// inicialización; condición; iteración.
for (var i = 0; i < 5; i++){
    // correrá cinco veces
}

// && es un "y" lógico, || es un "o" lógico
if (casa.tamano == "grande" && casa.color == "azul"){
    casa.contiene = "oso";
}
if (color == "rojo" || color == "azul"){
    // el color es rojo o azul
}

// && y || "corto circuito", lo cual es útil para establecer valores por defecto.
var nombre = otroNombre || "default";


// la estructura switch usa === para sus comparaciones
// usa 'break' para terminar cada caso 
// o los casos después del caso correcto serán ejecutados también. 
calificacion = 'B';
switch (calificacion) {
  case 'A':
    console.log("Excelente trabajo");
    break;
  case 'B':
    console.log("Buen trabajo");
    break;
  case 'C':
    console.log("Puedes hacerlo mejor");
    break;
  default:
    console.log("Muy mal");
    break;
}


///////////////////////////////////
// 4. Funciones, ámbitos y closures

// Las funciones en JavaScript son declaradas con la palabra clave "function".
function miFuncion(miArgumentoString){
    return miArgumentoString.toUpperCase(); //la funcion toUpperCase() vuelve todo
    // el String a mayúsculas
}
miFuncion("foo"); // = "FOO"

// Note que el valor a ser regresado debe estar en la misma línea que la
// palabra clave 'return', de otra forma la función siempre regresará 'undefined' 
// debido a la inserción automática de punto y coma.
function miFuncion()
{
    return // <- punto y coma insertado aquí automáticamente
    {
        estaEsUna: 'propiedad'
    }
}
miFuncion(); // = undefined al mandar a llamar la función

// Las funciones en JavaScript son de primera clase, así que pueden ser asignadas
// a variables y pasadas a otras funciones como argumentos - por ejemplo:
function miFuncion(){
    // este código será llamado cada cinco segundos
}
setTimeout(miFuncion, 5000);
// Note: setTimeout no es parte de JS, pero lo puedes obtener de los browsers
// y Node.js.

// Es posible declarar funciones sin nombre - se llaman funciones anónimas
// y se definen como argumentos de otras funciones.
setTimeout(function(){
    // este código se ejecuta cada cinco segundos
}, 5000);

// JavaScript tiene ámbitos de funciones; las funciones tienen su propio ámbito pero
// otros bloques no.
if (true){
    var i = 5;
}
i; // = 5 - en un lenguaje que da ámbitos por bloque esto sería undefined, pero no aquí.

// Este conlleva a un patrón de diseño común llamado "ejecutar funciones anónimas 
//inmediatamente", que preveé variables temporales de fugarse al ámbito global
(function(){
    var temporal = 5;
    // Podemos acceder al ámbito global asignando al 'objeto global', el cual
    // en un navegador siempre es 'window'. El objeto global puede tener
    // un nombre diferente en ambientes distintos, por ejemplo Node.js .
    window.permanente = 10;
})();
temporal; // da ReferenceError
permanente; // = 10

// Una de las características más útiles de JavaScript son los closures.
// Si una función es definida dentro de otra función, la función interna tiene acceso
// a todas las variables de la función externa, incluso aunque la función 
// externa ya haya terminado.
function decirHolaCadaCincoSegundos(nombre){
    var texto = "¡Hola, " + nombre + "!";
    // Las funciones internas son puestas en el ámbito local por defecto
    // como si fueran declaradas con 'var'.
    function interna(){
        alert(texto);
    }
    setTimeout(interna, 5000);
    // setTimeout es asíncrono, así que la función decirHolaCadaCincoSegundos 
    // terminará inmediatamente, y setTimeout llamará a interna() a los cinco segundos
    // Como interna está "cerrada dentro de" decirHolaCadaCindoSegundos, interna todavía tiene
    // acceso a la variable 'texto' cuando es llamada.
}
decirHolaCadaCincoSegundos("Adam"); // mostrará una alerta con "¡Hola, Adam!" en 5s

///////////////////////////////////
// 5. Más sobre objetos; constructores y prototipos

// Los objetos pueden contener funciones.
var miObjeto = {
    miFuncion: function(){
        return "¡Hola Mundo!";
    }
};
miObjeto.miFuncion(); // = "¡Hola Mundo!"

// Cuando las funciones de un objeto son llamadas, pueden acceder a las variables 
// del objeto con la palabra clave 'this'.
miObjeto = {
    miString: "¡Hola Mundo!",
    miFuncion: function(){
        return this.miString;
    }
};
miObjeto.miFuncion(); // = "¡Hola Mundo!"

// Las funciones de un objeto deben ser llamadas dentro del contexto de ese objeto.
var miFuncion = myObj.miFuncion;
miFuncion(); // = undefined

// Una función puede ser asignada al objeto y ganar acceso a él gracias a esto,
// incluso si no estaba dentro del objeto cuando este se definió.
var miOtraFuncion = function(){
    return this.miString.toUpperCase();
}
miObjeto.miOtraFuncion = myOtherFunc;
miObjeto.miOtraFuncion(); // = "¡HOLA MUNDO!"

// Podemos especificar el contexto en el que una función será llamada con los comandos
// 'call' o 'apply'.

var otraFuncion = function(otroString){
    return this.miString + otroString;
}
otraFuncion.call(miObjeto, " y hola Luna!"); // = "¡Hola Mundo! y hola Luna!"

// 'apply' es casi idéntico, pero recibe un arreglo como argumento.

otraFuncion.apply(miObjeto, [" y hola Sol!"]); // = "¡Hola Mundo! y hola Sol!"

// Esto es útil cuando estás trabajando con una función que acepta una secuencia de 
// argumentos y quieres pasar un arreglo.

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// Pero 'call' y 'apply' sólo son temporales. Cuando queremos que se quede, usamos bind.

var funcionUnida = otraFuncion.bind(miObjeto);
funcionUnida(" y hola Saturno!"); // = "¡Hola Mundo! y hola Saturno!"

// Bind también puede ser usada para aplicar parcialmente (curry) una función.

var producto = function(a, b){ return a * b; }
var porDos = producto.bind(this, 2);
porDos(8); // = 16

// Cuando llamas a una función con la palabra clave 'new' un nuevo objeto es creado.
// Se hace disponible a la función. Las funciones diseñadas para ser usadas así se
// llaman constructores.

var MiConstructor = function(){
    this.miNumero = 5;
}
miNuevoObjeto = new MiConstructor(); // = {miNumero: 5}
miNuevoObjeto.miNumero; // = 5

// Todos los objetos JavaScript tienen un 'prototipo'. Cuando vas a acceder a una
// propiedad en un objeto que no existe en el objeto el intérprete buscará en
// el prototipo.

// Algunas implementaciones de JavaScript te permiten acceder al prototipo de 
// un objeto con la propiedad __proto__. Mientras que esto es útil para explicar
// prototipos, no es parte del estándar; veremos formas estándar de usar prototipos
// más adelante.

var miObjeto = {
    miString: "¡Hola Mundo!"
};
var miPrototipo = {
    sentidoDeLaVida: 42,
    miFuncion: function(){
        return this.miString.toLowerCase()
    }
};

miObjeto.__proto__ = miPrototipo;
miObjeto.sentidoDeLaVida; // = 42

// Esto funcionan también para funciones.
miObjeto.miFuncion(); // = "hello world!"

// Por supuesto, si la propiedad que buscas no está en el prototipo, 
// se buscará en el prototipo del prototipo.
miPrototipo.__proto__ = {
    miBoolean: true
};
miObjeto.miBoolean; // = true

// Esto no involucra ningún copiado, cada objeto guarda una referencia a su 
// prototipo. Esto significa que podemos alterar el prototipo y nuestros
// cambios serán reflejados en todos lados.
miPrototipo.sentidoDeLaVida = 43;
miObjeto.sentidoDeLaVida; // = 43

// Mencionabamos anteriormente que __proto__ no está estandarizado, y que no 
// existe una forma estándar de acceder al prototipo de un objeto. De todas formas.
// hay dos formas de crear un nuevo objeto con un prototipo dado.

// El primer método es Object.create, el cual es una adición reciente a JavaScript,
// y por lo tanto, no disponible para todas las implementaciones aún.
var miObjeto = Object.create(miPrototipo);
miObjeto.sentidoDeLaVida; // = 43

// El segundo método, el cual trabaja en todos lados, tiene que ver con los 
// constructores. Los constructores tienen una propiedad llamada prototype.
// Este NO ES el prototipo de la función constructor; es el prototipo que
// se le da a los nuevos objetos cuando son creados con la palabra clave
// new.

MiConstructor.prototype = {
    miNumero: 5,
    getMiNumero: function(){
        return this.miNumero;
    }
};
var miNuevoObjeto2 = new MiConstructor();
miNuevoObjeto2.getMiNumero(); // = 5
miNuevoObjeto2.miNumero = 6
miNuevoObjeto2.getMiNumero(); // = 6

// Los tipos que vienen por defecto en JavaScript (como Strings y números)
// también tienen constructores que crean objetos equivalentes.
var miNumero = 12;
var miNumeroObjeto = new Number(12);
miNumero == miNumeroObjeto; // = true

// No son exactamente iguales.
typeof miNumero; // = 'number'
typeof miNumeroObjeto; // = 'object'
miNumero === miNumeroObjeyo; // = false
if (0){
    // Este código no se ejecutará porque 0 es false.
}

// Aún así, los objetos que envuelven y los prototipos por defecto comparten
// un prototipo. así que puedes agregar funcionalidades a un string de la 
// siguiente forma:
String.prototype.primerCaracter = function(){
    return this.charAt(0);
}
"abc".primerCaracter(); // = "a"

// Este hecho se usa normalmente en "polyfilling", lo cual es implementar
// nuevas funciones a JavaScript en un JavaScript más viejo, así que pueda ser
// compatible con ambintes más viejos de JavaScript (por ejemplo, navegadores viejos).

// Por ejemplo, mencionabamos que Object.create no está aún disponible en todas
// las implementaciones, pero podemos hacerlo con polyfill:
if (Object.create === undefined){ // esta validación sirve para no sobreescribir
    Object.create = function(proto){
    	// hace un constructor temporal con el prototipo correcto
        var Constructor = function(){};
        Constructor.prototype = proto;
        // y luego lo usamos para hacer un objeto con el prototipo
        // correcto.
        return new Constructor();
    }
}
```

## Fuentes y Referencias

La [Red para Desarroladores de Mozilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript) 
proveé excelente documentación para JavaScript para navegadores. Además, está en formato de wiki,
por lo que mientras vayas aprendiendo podrás ayudar a los demás con tu experiencia.

MDN [Una re-introducción a JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
cubre muchos de los conceptos que vimos aquí pero a mayor detalle. Esta guía cubre, más que nada, 
el lenguaje JavaScript solo. Si quieres aprender a cómo usarlo en un ambiente web empieza aprendiendo
sobre el [DOM](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)

[Aprende JavaScript con ejemplos y retos](http://www.learneroo.com/modules/64/nodes/350) es una 
variante de esta guía pero con retos.

[Jardín JavaScript](http://bonsaiden.github.io/JavaScript-Garden/) es una guía para todas las
funciones y características contra-intuitivas del lenguaje.

[JavaScript: La guía definitiva](http://www.amazon.com/gp/product/0596805527/) es una guía clásica / libro de referencia. 

Aparte de las contribuciones directas para este artículo, algo del contenido se adaptó
del tutorial de Python por Louie Dinh en este sitio. y el [Tutorial JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) en la Red de Desarrolladores de Mozilla.
---
category: tool
tool: jquery
contributors:
    - ["Sawyer Charles", "https://github.com/xssc"]
translators:
    - ["Ivan Alburquerque", "https://github.com/AlburIvan"]
lang: es-es
filename: jquery-es.js
---

jQuery es una librería de JavaScript que le ayuda a "hacer más y escribir menos". Esto hace que muchas de las tareas comunes de JavaScript sean más fáciles de escribir. jQuery es utilizado por muchas de las grandes empresas y desarrolladores de todo el mundo. Hace que AJAX, la gestión de eventos, la manipulación de documentos, y mucho más, sea más fácil y rápido.

Debido a que jQuery es una librería de JavaScript debes [aprender JavaScript primero](https://learnxinyminutes.com/docs/es-es/javascript-es/)

```js


///////////////////////////////////
// 1. Selectores

// Los selectores en jQuery son usados para seleccionar un elemento
var page = $(window); // Selecciona toda la ventana gráfica

// Los selectores también pueden ser selectores CSS
var paragraph = $('p'); // Selecciona todos los elementos de párrafo
var table1 = $('#table1'); // Selecciona el elemento con id 'tabla1'
var squares = $('.square'); // Selecciona todos los elementos con la clase "square"
var square_p = $('p.square') // Selecciona los párrafos con la clase "square"


///////////////////////////////////
// 2. Eventos y efectos

// Un evento muy común que se utiliza es el evento 'ready' en el documento
// Se puede utilizar el método de 'ready' para esperar hasta que el elemento haya terminado de cargar
$(document).ready(function(){
  // El código no se ejecutará hasta que el documento haya terminado de cargar
});

// jQuery es muy bueno activando eventos
// Y también en el manejo de lo que ocurre cuando se activa un evento
$('#button').click(); // Dispara un evento click en $ ('# botón')
$('#button').click(function(){
  // El código es ejecutado cuando se hace clic en el elemento de botón #
});

function onAction() {
 // Esto se ejecuta cuando se activa el evento
}

// Algunos otros eventos comunes son:
$('#btn').dblclick(onAction); //Doble clic
$('#btn').hover(onAction); // Pasar el cursor por encima
$('#btn').focus(onAction); // Enfocado
$('#btn').blur(onAction); // Pierde enfoque
$('#btn').submit(onAction); // Enviado
$('#btn').select(onAction); // Cuando se selecciona un elemento
$('#btn').keydown(onAction); // Cuando una tecla es empujada hacia abajo
$('#btn').keyup(onAction); // Cuando se suelta una tecla
$('#btn').keypress(onAction); // Cuando se pulsa una tecla
$('#btn').mousemove(onAction); // Cuando se mueve el mouse
$('#btn').mouseenter(onAction); // El mouse entra en el elemento
$('#btn').mouseleave(onAction); // El mouse sale en el elemento

// También se puede utilizar una función anónima
$('#btn').hover(function(){
  // Se ejecuta al pasar por encima
});

// Todos estos pueden también desencadenar el evento en lugar de manejarlo
// Simplemente no pasando ningún parámetro
$('#btn').dblclick(); // Dispara el evento de doble clic sobre el elemento

// Se puede manejar múltiples eventos, usando el selector una vez
$('#btn').on(
  {dblclick: myFunction1} // Activado con doble clic
  {blur: myFunction1} // Activo en la perdida de enfoque
);

// Puede mover y ocultar elementos con algunos métodos de efecto
$('.table').hide(); // Oculta el(los) elemento(s)

// Nota: llamar a una función en estos métodos aún oculta el elemento
$('.table').hide(function(){
    // El elemento se oculta entonces función ejecutada
});

// Puedes almacenar los selectores en las variables
var tables = $('.table');

// Algunos métodos básicos de manipulación de documento son:
tables.hide(); // Oculta elemento(s)
tables.show(); // Muestra elemento(s)
tables.toggle(); // Cambia el estado de ocultar / mostrar
tables.fadeOut(); // Desvanece
tables.fadeIn(); // Fundirse
tables.fadeToggle(); // Desvanece dentro o fuera
tables.fadeTo(0.5); // Desvanece a una opacidad (entre 0 y 1)
tables.slideUp(); // Desliza hacia arriba
tables.slideDown(); // Desliza hacia abajo
tables.slideToggle(); // Desliza hacia arriba o hacia abajo

// Todo lo anterior toma una velocidad (milisegundos) y la función de devolución de llamada
tables.hide(1000, myFunction); // Animación de ocultar elemento a 1 segundo y luego la funcion de devolución

// 'fadeTo' requiere de una opacidad como su segundo parámetro
tables.fadeTo(2000, 0.1, myFunction); // 2 segundos. decolorar a opacidad de 0.1 luego la función

// Puede conseguir un efecto un poco más avanzado con el método 'animate'
tables.animate({margin-top:"+=50", height: "100px"}, 500, myFunction);
// El método 'animate' toma un objeto de CSS y los valores finales,
// Parámetro opcional de opciones para afinar la animación,
// Y por supuesto la función de devolución de llamada

///////////////////////////////////
// 3. Manipulación

// Estos son similares a los efectos, pero pueden hacer más
$('div').addClass('div') // Añade la clase div a todos los divs

// Métodos comunes de manipulación 
$('p').append('Hola mundo'); // Añade al final del elemento
$('p').attr('class'); // Obtiene atributo
$('p').attr('class', 'content'); // Configura atributos
$('p').hasClass('div'); //Devuelve verdadero si tiene la clase
$('p').height(); // Obtiene la altura del elemento o define la altura


// Para muchos métodos de manipulación, obtener información sobre un elemento
// consigue solamente el primer elemento coincidente
$('p').height(); // Obtiene sólo la altura de la primera etiqueta 'p'

// Puedes utilizar 'each' para recorrer todos los elementos
var heights = [];
$('p').each(function() {
  heights.push($(this).height()); // Añade todas las alturas "p" de la etiqueta a la matriz
});


```---
language: json
filename: learnjson-es.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
  - ["Daniel Zendejas","https://github.com/DanielZendejas"]
lang: es-es
---

Siendo JSON un formato de intercambio de infomación tan sencillo, probablemente este será el Learn X in Y más sencillo jamás.

JSON en su forma más pura no tiene comentarios, pero la mayoría de los parseadores aceptarán comentarios de C (//, /\* \*/). De todas formas, para el propóstio de esto todo será JSON 100% válido. Por suerte, habla por sí mismo.

```json

{
  "llave": "valor",
  
  "llaves": "siempre debe estar entre comillas (ya sean dobles o simples)",
  "numeros": 0,
  "strings": "Høla, múndo. Todo el unicode está permitido, así como \"escapar\".",
  "¿soporta booleanos?": true,
  "vacíos": null,

  "numero grande": 1.2e+100,

  "objetos": {
    "comentario": "La mayoría de tu estructura vendrá de objetos.",

    "arreglo": [0, 1, 2, 3, "Los arreglos pueden contener cualquier cosa.", 5],

    "otro objeto": {
      "comentario": "Estas cosas pueden estar anidadas, muy útil."
    }
  },

  "tontería": [
    {
      "fuentes de potasio": ["bananas"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "estilo alternativo": {
    "comentario": "Mira esto!"
  , "posición de la coma": "no importa - mientras este antes del valor, entonces sera válido"
  , "otro comentario": "qué lindo"
  },

  "eso fue rapido": "Y, estás listo. Ahora sabes todo lo que JSON tiene para ofrecer."
}
```
---
language: Julia
contributors:
    - ["Leah Hanson", "http://leahhanson.us"]
translators:
    - ["Guillermo Garza", "http://github.com/ggarza"]
    - ["Ismael Venegas Castelló", "https://github.com/Ismael-VC"]
filename: learnjulia-es.jl
lang: es-es
---

![JuliaLang](http://s13.postimg.org/z89djuwyf/julia_small.png)

[Julia](http://julialanges.github.io) es un [lenguaje de programación](http://es.wikipedia.org/wiki/Lenguaje_de_programaci%C3%B3n) [multiplataforma](http://es.wikipedia.org/wiki/Multiplataforma) y [multiparadigma](http://es.wikipedia.org/wiki/Lenguaje_de_programaci%C3%B3n_multiparadigma) de [tipado dinámico](http://es.wikipedia.org/wiki/Tipado_din%C3%A1mico), [alto nivel](http://es.wikipedia.org/wiki/Lenguaje_de_alto_nivel) y [alto desempeño](http://es.wikipedia.org/wiki/Computaci%C3%B3n_de_alto_rendimiento) para la computación [genérica](http://es.wikipedia.org/wiki/Lenguaje_de_programaci%C3%B3n_de_prop%C3%B3sito_general), [técnica y científica](http://es.wikipedia.org/wiki/Computaci%C3%B3n_cient%C3%ADfica),  con una sintaxis que es familiar para los usuarios de otros entornos de computación técnica y científica. Provee de un [sofisticado compilador JIT](http://es.wikipedia.org/wiki/Compilaci%C3%B3n_en_tiempo_de_ejecuci%C3%B3n), [ejecución distribuida y paralela](http://docs.julialang.org/en/release-0.3/manual/parallel-computing), [precisión numérica](http://julia.readthedocs.org/en/latest/manual/integers-and-floating-point-numbers) y de una [extensa librería con funciones matemáticas](http://docs.julialang.org/en/release-0.3/stdlib). La librería estándar, escrita casi completamente en Julia, también integra las mejores y más maduras librerías de C y Fortran para el [álgebra lineal](http://docs.julialang.org/en/release-0.3/stdlib/linalg), [generación de números aleatorios](http://docs.julialang.org/en/release-0.3/stdlib/numbers/?highlight=random#random-numbers), [procesamiento de señales](http://docs.julialang.org/en/release-0.3/stdlib/math/?highlight=signal#signal-processing), y [procesamiento de cadenas](http://docs.julialang.org/en/release-0.3/stdlib/strings). Adicionalmente, la comunidad de [desarrolladores de Julia](https://github.com/JuliaLang/julia/graphs/contributors) contribuye un número de [paquetes externos](http://pkg.julialang.org) a través del gestor de paquetes integrado de Julia a un paso acelerado. [IJulia](https://github.com/JuliaLang/IJulia.jl), una colaboración entre las comunidades de [IPython](http://ipython.org) y Julia, provee de una poderosa interfaz gráfica basada en el [navegador para Julia](https://juliabox.org).

En Julia los programas están organizados entorno al [despacho múltiple](http://docs.julialang.org/en/release-0.3/manual/methods/#man-methods); definiendo funciones y sobrecargándolas para diferentes combinaciones de tipos de argumentos, los cuales también pueden ser definidos por el usuario.

### ¡Prueba Julia ahora mismo!

* [TryJupyter](https://try.jupyter.org)
* [JuliaBox](https://juliabox.org)
* [SageMathCloud](https://cloud.sagemath.com)

### Resumen de Características:

* [Despacho múltiple](http://en.wikipedia.org/wiki/Multiple_dispatch): permite definir el comportamiento de las funciones a través de múltiples combinaciones de tipos de argumentos (**métodos**).
* Sistema de **tipado dinámico**: tipos para la documentación, la optimización y el despacho.
* [Buen desempeño](http://julialang.org/benchmarks), comparado al de lenguajes **estáticamente compilados** como C.
* [Gestor de paquetes](http://docs.julialang.org/en/release-0.3/stdlib/pkg) integrado.
* [Macros tipo Lisp](http://docs.julialang.org/en/release-0.3/manual/metaprogramming/#macros) y otras comodidades para la [meta programación](http://docs.julialang.org/en/release-0.3/manual/metaprogramming).
* Llamar funciones de otros lenguajes, mediante paquetes como: **Python** ([PyCall](https://github.com/stevengj/PyCall.jl)), [Mathematica](http://github.com/one-more-minute/Mathematica.jl), **Java** ([JavaCall](http://github.com/aviks/JavaCall.jl)), **R** ([Rif](http://github.com/lgautier/Rif.jl) y [RCall](http://github.com/JuliaStats/RCall.jl)) y **Matlab** ([MATLAB](http://github.com/JuliaLang/MATLAB.jl)).
* [Llamar funciones de C y Fortran](http://docs.julialang.org/en/release-0.3/manual/calling-c-and-fortran-code) **directamente**: sin necesidad de usar envoltorios u APIs especiales.
* Poderosas características de **línea de comandos** para [gestionar otros procesos](http://docs.julialang.org/en/release-0.3/manual/running-external-programs).
* Diseñado para la [computación paralela y distribuida](http://docs.julialang.org/en/release-0.3/manual/parallel-computing) **desde el principio**.
* [Corrutinas](http://en.wikipedia.org/wiki/Coroutine): hilos ligeros "**verdes**".
* Los [tipos definidos por el usuario](http://docs.julialang.org/en/release-0.3/manual/types) son tan **rápidos y compactos** como los tipos estándar integrados.
* [Generación automática de código](http://docs.julialang.org/en/release-0.3/stdlib/base/?highlight=%40code#internals) **eficiente y especializado** para diferentes tipos de argumentos.
* [Conversiones y promociones](http://docs.julialang.org/en/release-0.3/manual/conversion-and-promotion) para tipos numéricos y de otros tipos, **elegantes y extensibles**.
* Soporte eficiente para [Unicode](http://es.wikipedia.org/wiki/Unicode), incluyendo [UTF-8](http://es.wikipedia.org/wiki/UTF-8) pero sin limitarse solo a este.
* [Licencia MIT](https://github.com/JuliaLang/julia/blob/master/LICENSE.md): libre y de código abierto.

Esto se basa en la versión `0.3.11`.

```ruby
# Los comentarios de una línea comienzan con una almohadilla (o signo de gato).

#=
  Los comentarios multilínea pueden escribirse
  usando '#=' antes de el texto  y '=#'
  después del texto. También se pueden anidar.
=#


##############################################
# 1. Tipos de datos primitivos y operadores. #
##############################################

# Todo en Julia es una expresión (Expr).

# Hay varios tipos básicos de números.
3          # => 3          # Int64
3.2        # => 3.2        # Float64
2 + 1im    # => 2 + 1im    # Complex{Int64}
2 // 3     # => 2//3       # Rational{Int64}

# Todos los operadores infijos normales están disponibles.
1 + 1        # => 2
8 - 1        # => 7
10 * 2       # => 20
35 / 5       # => 7.0    # dividir un Int por un Int siempre resulta
                         # en un Float
5 / 2        # => 2.5
div(5, 2)    # => 2      # para un resultado truncado, usa la función div
5 \ 35       # => 7.0
2 ^ 2        # => 4      # exponente, no es XOR
12 % 10      # => 2

# Refuerza la precedencia con paréntesis.
(1 + 3) * 2    # => 8

# Operadores a nivel de bit.
~2         # => -3    # bitwise NOT
3 & 5      # => 1     # bitwise AND
2 | 4      # => 6     # bitwise OR
2 $ 4      # => 6     # bitwise XOR
2 >>> 1    # => 1     # desplazamiento lógico hacia la derecha
2 >> 1     # => 1     # desplazamiento aritmético hacia la derecha
2 << 1     # => 4     # desplazamiento lógico/aritmético hacia la izquierda

# Se puede utilizar la función bits para ver la representación
# binaria de un número.
bits(12345)
# => "0000000000000000000000000000000000000000000000000011000000111001"

bits(12345.0)
# => "0100000011001000000111001000000000000000000000000000000000000000"

# Los valores booleanos (Bool) son primitivos.
true     # => true
false    # => false

# Operadores booleanos.
!true     # => false
!false    # => true
1 == 1    # => true
2 == 1    # => false
1 != 1    # => false
2 != 1    # => true
1 < 10    # => true
1 > 10    # => false
2 <= 2    # => true
2 >= 2    # => true

# ¡Las comparaciones pueden ser concatenadas!
1 < 2 < 3    # => true
2 < 3 < 2    # => false

# Los literales de cadenas (String) se crean con la comilla doble: "
"Esto es una cadena."

# Los literales de caracteres (Char) se crean con la comilla simple: '
'a'

# Una cadena puede ser indexada como una arreglo de caracteres.
"Esto es un string."[1]    # => 'E'    # Los índices en Julia comienzan en: 1

# Sin embargo, esto no va a funcionar bien para las cadenas UTF8 (UTF8String),
# Lo que se recomienda es la iteración (map, for, etc).

# $ puede ser utilizado para la interpolación de cadenas, se puede poner
# cualquier expresión de Julia dentro los paréntesis.
"2 + 2 = $(2 + 2)"    # => "2 + 2 = 4"

# Otra forma para formatear cadenas es usando el macro printf.
@printf "%d es menor de %f\n" 4.5 5.3    # 5 es menor de 5.300000

# ¡Imprimir es muy fácil!
println("¡Hola Julia!")    # ¡Hola Julia!


##############################
# 2. Variables y Colecciones #
##############################

# No hay necesidad de declarar las variables antes de asignarlas.
una_variable = 5    # => 5
una_variable        # => 5

# Acceder a una variable no asignada previamente es una excepción.
try
    otra_variable    # ERROR: otra_variable not defined
catch e
    println(e)       # UndefVarError(:otra_variable)
end

# Los nombres de variables comienzan con una letra o guion bajo: _.
# Después de eso, puedes utilizar letras, dígitos, guiones bajos y signos de
# exclamación.
otraVariable_123! = 6    # => 6

# También puedes utilizar caracteres Unicode.
☃ = 8    # => 8

# Estos son especialmente útiles para la notación matemática
# (multiplicación implicita).
2π    # => 6.283185307179586

#=
  Una nota sobre las convenciones de nomenclatura de Julia:

  * Los nombres de las variables aparecen en minúsculas, con separación de
    palabra indicado por un guion bajo:

    otra_variable

  * Los nombres de los tipos comienzan con una letra mayúscula y separación de
    palabras se muestra con CamelCase en vez de guión bajo:

    OtroTipo

  * Los nombres de las funciones y los macros están en minúsculas, sin
    underscore:

    otromacro

  * Funciones que modifican sus entradas tienen nombres que terminan en: !.
    Estas funciones a veces se les llaman funciones transformadoras o
    funciones in situ:

    otra_funcion!
=#

# Los arreglos (Array) almacenan una secuencia de valores indexados de entre 1 hasta n.
a = Int64[]    # => 0-element Array{Int64,1}

# Los literales de arregos unidimensionales se pueden escribir con valores
# separados por comas.
b = [4, 5, 6]
#=
 => 3-element Array{Int64,1}:
     4
     5
     6
=#
b[1]      # => 4
b[end]    # => 6

# Los arreglos bidimensionales usan valores separados por espacios y filas
# separadas por punto y coma.
matrix = [1 2; 3 4]
#=
 => 2x2 Array{Int64,2}:
     1  2
     3  4
=#

# Añadir cosas al final de un arreglo con push! y append!.
push!(a, 1)      # => [1]
push!(a, 2)      # => [1,2]
push!(a, 4)      # => [1,2,4]
push!(a, 3)      # => [1,2,4,3]
append!(a, b)    # => [1,2,4,3,4,5,6]

# Eliminar del final con pop!.
pop!(b)    # => 6 y b ahora es: [4,5]

# Vamos a ponerlo de nuevo.
push!(b, 6)    # b es ahora [4,5,6] de nuevo

a[1]    # => 1    # recuerda, los índices de Julia empiezan desde 1, no desde 0!

# end es una abreviatura para el último índice. Se puede utilizar en cualquier
# expresión  de indexación.
a[end]    # => 6

# También hay shift! y unshift!.
shift!(a)         # => 1 y a es ahora: [2,4,3,4,5,6]
unshift!(a, 7)    # => [7,2,4,3,4,5,6]

# Los nombres de funciones que terminan en exclamaciones indican que modifican
# su o sus argumentos de entrada.
arr = [5, 4, 6]    # => 3-element Array{Int64,1}: [5,4,6]
sort(arr)          # => [4,5,6] y arr es todavía: [5,4,6]
sort!(arr)         # => [4,5,6] y arr es ahora: [4,5,6]

# Buscando fuera de límites es un BoundsError.
try
    a[0]          # ERROR: BoundsError() in getindex at array.jl:270
    a[end+1]      # ERROR: BoundsError() in getindex at array.jl:270
catch e
    println(e)    # BoundsError()
end

# Las excepciones y los errores dan la línea y el archivo de su procedencia,
# aunque provenga de la librería estándar. Si compilas Julia del código fuente,
# puedes buscar en el código para encontrar estos archivos.

# Se puede inicializar un arreglo con un rango (Range).
a = [1:5]    # => 5-element Array{Int64,1}: [1,2,3,4,5]

# Puedes mirar en los rangos con la sintaxis de rebanada.
a[1:3]       # => [1,2,3]
a[2:end]     # => [2,3,4,5]

# Eliminar elementos de un arreglo por índice con splice!
arr = [3, 4, 5]
splice!(arr, 2)    # => 4 y arr es ahora: [3,5]

# Concatenar arreglos con append!
b = [1, 2, 3]
append!(a, b)    # a ahora es: [1,2,3,4,5,1,2,3]

# Comprueba la existencia de un elemento en un arreglo con in.
in(1, a)    # => true

# Examina la longitud con length.
length(a)    # => 8

# Las tuplas (Tuple) son inmutables.
tup = (1, 2, 3)    # => (1,2,3)    # una tupla tipo (Int64,Int64,Int64)
tup[1]             # => 1

try:
    tup[1] = 3     # ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
catch e
    println(e)     # MethodError(setindex!,(:tup,3,1))
end

# Muchas funciones de arreglos también trabajan en con las tuplas.
length(tup)    # => 3
tup[1:2]       # => (1,2)
in(2, tup)     # => true

# Se pueden desempacar las tuplas en variables individuales.
a, b, c = (1, 2, 3)    # => (1,2,3)    # ahora a es 1, b es 2 y c es 3

# Los tuplas se crean, incluso si se omiten los paréntesis.
d, e, f = 4, 5, 6    # => (4,5,6)

# Una tupla de un elemento es distinta del valor que contiene.
(1,) == 1    # => false
(1) == 1     # => true

# Mira que fácil es cambiar dos valores!
e, d = d, e    # => (5,4)    # ahora d es 5 y e es 4

# Los diccionarios (Dict) son arreglos asociativos.
dicc_vacio = Dict()    # => Dict{Any,Any} with 0 entries

# Se puede crear un diccionario usando una literal.
dicc_lleno = ["uno" => 1, "dos" => 2, "tres" => 3]
#=
 => Dict{ASCIIString,Int64} with 3 entries:
      "tres" => 3
      "dos"  => 2
      "uno"  => 1
=#

# Busca valores con: [].
dicc_lleno["uno"]    # => 1

# Obtén todas las claves con.
keys(dicc_lleno)
#=
 => KeyIterator for a Dict{ASCIIString,Int64} with 3 entries. Keys:
      "tres"
      "dos"
      "uno"
=#

# Nota: los elementos del diccionario no están ordenados y no se guarda el orden
# en que se insertan.

# Obtén todos los valores.
values(dicc_lleno)
#=
 => ValueIterator for a Dict{ASCIIString,Int64} with 3 entries. Values:
      3
      2
      1
=#

# Nota: igual que el anterior en cuanto a ordenamiento de los elementos.

# Comprueba si una clave existe en un diccionario con in y haskey.
in(("uno", 1), dicc_lleno)     # => true
in(("tres", 3), dicc_lleno)    # => false

haskey(dicc_lleno, "uno")      # => true
haskey(dicc_lleno, 1)          # => false

# Tratar de obtener un valor con una clave que no existe producirá un error.
try
    # ERROR: key not found: cuatro in getindex at dict.jl:489
    dicc_lleno["cuatro"]
catch e
    println(e)    # KeyError("cuatro")
end

# Utiliza el método get para evitar este error proporcionando un valor
# predeterminado: get(diccionario, clave, valor_predeterminado).
get(dicc_lleno, "uno", 4)       # => 1
get(dicc_lleno, "cuatro", 4)    # => 4

# Usa conjuntos (Set) para representar colecciones de valores únicos, no
# ordenados.
conjunto_vacio = Set()    # => Set{Any}({})

# Iniciar una conjunto de valores.
conjunto_lleno = Set(1, 2, 2, 3, 4)    # => Set{Int64}({4,2,3,1})

# Añadir más valores a un conjunto.
push!(conjunto_lleno, 5)    # => Set{Int64}({4,2,3,5,1})
push!(conjunto_lleno, 5)    # => Set{Int64}({4,2,3,5,1})

# Comprobar si los valores están en el conjunto.
in(2, conjunto_lleno)     # => true
in(10, conjunto_lleno)    # => false

# Hay funciones de intersección, unión y diferencia de conjuntos.
otro_conjunto = Set(3, 4, 5, 6)             # => Set{Int64}({6,4,5,3})
intersect(conjunto_lleno, otro_conjunto)    # => Set{Int64}({3,4,5})
union(conjunto_lleno, otro_conjunto)        # => Set{Int64}({1,2,3,4,5,6})
setdiff(Set(1, 2, 3, 4), Set(2, 3, 5))      # => Set{Int64}({1,4})


#######################
# 3. Control de Flujo #
#######################

# Hagamos una variable.
una_variable = 5

# Aquí está la declaración de un if. La indentación no es significativa en
# Julia.
if una_variable > 10
    println("una_variable es completamente mayor que 10.")
elseif una_variable < 10                     # esta condición elseif es opcional
    println("una_variable es menor que 10.")
else                                         # esto también es opcional
    println("De echo una_variable es 10.")
end
# imprime: una_variable es menor que 10.

# El bucle for itera sobre tipos iterables, ie. Range, Array, Set,
# Dict y String.
for animal in ["perro", "gato", "ratón"]
    # Se puede usar $ para interpolar variables o expresiones en ls cadenas.
    println("$animal es un mamífero.")
end
#=
 imprime:
   perro es un mamífero.
   gato es un mamífero.
   ratón es un mamífero.
=#

for a in ["perro" => "mamífero", "gato" => "mamífero", "ratón" => "mamífero"]
    println("$(a[1]) es un $(a[2]).")
end
#=
 imprime:
   perro es un mamífero.
   gato es un mamífero.
   ratón es un mamífero.
=#

for (k,v) in ["perro"=>"mamífero", "gato"=>"mamífero", "ratón"=>"mamífero"]
    println("$k es un $v.")
end
#=
 imprime:
   perro es un mamífero.
   gato es un mamífero.
   ratón es un mamífero.
=#

# El bucle while itera hasta que una condición se deje de cumplir.
x = 0
while x < 4
    println(x)
    x += 1    # versión corta de: x = x + 1
end
#=
imprime:
  0
  1
  2
  3
=#

# Maneja excepciones con un bloque try/catch.
try    # intentar
   error("Ooops!")
catch e
   println("capturando: $e")    # capturando: ErrorException("Ooops!")
end


################
# 4. Funciones #
################

# Usa function para crear nuevas funciones.

#=
    function nombre(arglist)
        cuerpo...
    end
=#
function suma(x, y)
    println("x es $x e y es $y")

    # las funciones devuelven el valor de su última expresión
    x + y
end
# => suma (generic function with 1 method)

suma(5, 6)    # => 11    # después de imprimir: x es 5 e y es 6

# También puedes usar esta otra sintaxis para definir funciones!
resta(x, y) = x - y    # => resta (generic function with 1 method)

# Puedes definir funciones que toman un número variable de
# argumentos posicionales (el ... se llama un splat).
function varargs(args...)
    # Usa la palabra clave return para regresar desde cualquier
    # lugar de la función.
    return args
end
# => varargs (generic function with 1 method)

varargs(1, 2, 3)      # => (1,2,3)
varargs([1, 2, 3])    # => ([1,2,3],)

# Acabamos de utilizar el splat (...) en la definición de una función. También
# se puede utilizar al llamar a una función, donde se esparce un arreglo, tupla
# o en general una secuencia iterable en la tupla de argumentos.
varargs([1, 2, 3]...)    # => (1,2,3)    # igual que: varargs(1, 2, 3)

x = (1, 2, 3)    # => (1,2,3)
varargs(x)       # => ((1,2,3),)
varargs(x...)    # => (1,2,3)

varargs("abc"...)    # => ('a','b','c')

# Puedes definir funciones con argumentos posicionales opcionales.
function defaults(a, b, x=5, y=6)
    return "$a $b y $x $y"
end
# => defaults (generic function with 3 methods)

defaults('h', 'g')              # => "h g y 5 6"
defaults('h', 'g', 'j')         # => "h g y j 6"
defaults('h', 'g', 'j', 'k')    # => "h g y j k"

try
    defaults('h')    # ERROR: `defaults` has no method matching defaults(::Char)
    defaults()       # ERROR: `defaults` has no method matching defaults()
catch e
    println(e)       # MethodError(defaults,('h',))
end

# Puedes definir funciones que tomen argumentos de palabras clave.
function args_clave(;k1=4, nombre2="hola")    # nota el punto y coma: ;
    return ["k1" => k1, "nombre2" => nombre2]
end
# => args_clave (generic function with 1 method)

args_clave(nombre2="ness")    # => ["nombre2"=>"ness","k1"=>4]
args_clave(k1="mine")         # => ["k1"=>"mine","nombre2"=>"hola"]
args_clave()                  # => ["nombre2"=>"hola","k1"=>4]

# Puedes combinar todo tipo de argumentos en la misma función.
function todos_los_args(arg_posicional, arg_opcional=2; arg_clave="foo")
    println("argumento posicional: $arg_posicional")
    println("  argumento opcional: $arg_opcional")
    println("     argumento clave: $arg_clave")
end
# => todos_los_args (generic function with 2 methods)

# No se necesita punto y coma ; al llamar la función usando un argumento clave,
# esto solo es necesario en la definición de la función.
todos_los_args(1, 3, arg_clave=4)
#=
 imprime:
   argumento posicional: 1
     argumento opcional: 3
        argumento clave: 4
=#

# Julia tiene funciones de primera clase.
function crear_suma(x)
    suma = function (y)    # función anónima
        return x + y
    end
    return suma
end
# => crear_suma (generic function with 1 method)

# Esta es otra sintaxis (estilo cálculo lambda), para crear funciones anónimas.
(x -> x > 2)(3)    # => true

# Esta función es idéntica a la crear_suma implementación anterior.
crear_suma(x) = y -> x + y

# También puedes nombrar la función interna, si quieres.
function crear_suma(x)
    function suma(y)
        x + y
    end
    suma
end
# => crear_suma (generic function with 1 method)

suma_10 = crear_suma(10)    # => suma (generic function with 1 method)
suma_10(3)                  # => 13

# Hay funciones integradas de orden superior.
map(suma_10, [1, 2, 3])                # => [11,12,13]
filter(x -> x > 5, [3, 4, 5, 6, 7])    # => [6,7]

# Se puede pasar un bloque a las funciones cuyo primer argumento posicional
# es otra función, como en map y filter.
map([1, 2, 3]) do arr
    suma_10(arr)
end
#=
 => 3-element Array{Int64,1}:
     11
     12
     13
=#

filter([3, 4, 5, 6, 7]) do arr
    (x -> x > 5)(arr)
end
#=
 => 2-element Array{Int64,1}:
     6
     7
=#

# Podemos usar comprensiones de listas multidimensionales.
[suma_10(i) for i = [1, 2, 3]]     # => [11, 12, 13]    # 1D
[suma_10(i) for i in [1, 2, 3]]    # => [11, 12, 13]

[i*j for i = [1:3], j in [1:3]]    # 2D
#=
 => 3x3 Array{Int64,2}:
     1  2  3
     2  4  6
     3  6  9
=#

[i*j/k for i = [1:3], j = [1:3], k in [1:3]]    # 3D
#=
 => 3x3x3 Array{Float64,3}:
     [:, :, 1] =
      1.0  2.0  3.0
      2.0  4.0  6.0
      3.0  6.0  9.0

     [:, :, 2] =
      0.5  1.0  1.5
      1.0  2.0  3.0
      1.5  3.0  4.5

     [:, :, 3] =
      0.333333  0.666667  1.0
      0.666667  1.33333   2.0
      1.0       2.0       3.0
=#


############
# 5. Tipos #
############

# Cada valor tiene un tipo y las variables no tienen propios tipos.
# Se puede utilizar la función typeof para obtener el tipo de un valor.
typeof(5)    # => Int64    # en un sistema de 64 bits, de lo contrario: Int32

# Los tipos son valores de primera clase, DataType es el tipo que representa a
# los tipos, incluyéndose a sí mismo.
typeof(Int64)       # => DataType
typeof(DataType)    # => DataType

# Los tipos se usan para la documentación, para optimizaciones
# y el despacho múltiple. No están comprobados estáticamente.

# Los usuarios pueden definir sus propios tipos.
# Son como registros o estructuras en otros idiomas.
# Un nuevo tipos se define utilizado la palabra clave type.

# type Nombre
#   atributo::UnTipo    # las anotaciones de tipos son opcionales
#   ...
# end
type Tigre
    longitud_cola::Float64
    color_pelaje            # sin una anotación de tipo, es lo mismo que `::Any`
end

# Los argumentos del constructor por defecto son los atributos
# del tipo, en el orden en que están listados en la definición.
tigre = Tigre(3.5, "anaranjado")    # => Tigre(3.5,"anaranjado")

# El tipo funciona como método constructor para los valores de ese tipo.
sherekhan = typeof(tigre)(5.6, "fuego")    # => Tigre(5.6,"fuego")


# Este estilo de tipos son llamados tipos concretos.
# Se pueden crear instancias de estos, pero no pueden tener subtipos.
# La otra clase de tipos son los tipos abstractos.

# abstract Nombre
abstract Gato    # sólo un nombre y un punto en la jerarquía de tipos

# No se pueden crear instancias de los tipos abstractos, pero pueden tener
# subtipos. Por ejemplo, Number es un tipo abstracto.
subtypes(Number)
#=
 => 2-element Array{Any,1}:
     Complex{T<:Real}
     Real
=#

subtypes(Gato)    # => 0-element Array{Any,1}

# Cada tipo tiene un supertipo, utiliza la función súper para conseguirlo.
typeof(5)               # => Int64
super(Int64)            # => Signed
super(Signed)           # => Integer
super(Integer)          # => Real
super(Real)             # => Number
super(Number)           # => Any
super(super(Signed))    # => Real
super(Any)              # => Any

# Todos estos tipos, a excepción de Int64, son abstractos.

# <: es el operador de subtipos.
type Leon <: Gato    # Leon es un subtipo de Gato
    color_crin
    rugido::String
end

# Se pueden definir más constructores para un tipo.
# Sólo define una función del mismo nombre que el tipo y llama al constructor
# existente para obtener un valor del tipo correcto.

# Este es un constructor externo porque está fuera de la definición del tipo.
Leon(rugido::String) = Leon("verde", rugido)

type Pantera <: Gato    # Pantera también es un a subtipo de Gato
    color_ojos

    # Pantera sólo tendrá este constructor, y ningún constructor predeterminado.
    Pantera() = new("verde")
end

# Utilizar constructores internos, como se hace en Pantera, te da control sobre
# cómo se pueden crear valores de este tipo. Cuando sea posible, debes utilizar
# constructores externos en lugar de internos.


########################
# 6. Despacho Múltiple #
########################

# En Julia, todas las funciones nombradas son funciones genéricas.
# Esto significa que se construyen a partir de muchos métodos más pequeños.
# Cada constructor de Leon es un método de la función genérica Leon.

# Por ejemplo, vamos a hacer métodos para Leon, Pantera, y Tigre de una
# función genérica maullar:

# acceso utilizando notación de puntos
maullar(animal::Leon) = animal.rugido
# => maullar (generic function with 1 method)
maullar(animal::Pantera) = "grrr"
# => maullar (generic function with 2 methods)
maullar(animal::Tigre) = "rawwwr"
# => maullar (generic function with 3 methods)

# Se puede obtener una lista de métodos con la función methods.
methods(maullar)
#=
  # 3 methods for generic function "maullar":
  maullar(animal::Leon) at none:1
  maullar(animal::Pantera) at none:1
  maullar(animal::Tigre) at none:1
=#

# Prueba de la función maullar.
maullar(tigre)                    # => "rawwwr"
maullar(Leon("cafe", "ROAAR"))    # => "ROAAR"
maullar(Pantera())                # => "grrr"

# Revisar la jerarquía de tipos locales.
issubtype(Tigre, Gato)      # => false    # igual que: Tigre <: Gato
issubtype(Leon, Gato)       # => true     # igual que: Leon <: Gato
issubtype(Pantera, Gato)    # => true

# Definición de una función que acepta argumentos de tipo Gato.
mascota(gato::Gato) = println("El gato dice $(maullar(gato))")

mascota(Leon("42"))    # El gato dice 42

try
    mascota(tigre)    # ERROR: `mascota` has no method matching mascota(::Tigre)
catch e
    println(e)        # MethodError(mascota,(Tigre(3.5,"anaranjado"),))
end

# En los lenguajes orientados a objetos, el despacho simple es común. Esto
# significa que la implementación del método a llamar se selecciona en base
# al tipo del primer argumento.

# En Julia, los tipos de todos los argumentos contribuyen a seleccionar método
# más específico.

# Vamos a definir una función con más argumentos, para que podamos ver la
# diferencia
pelear(t::Tigre, c::Gato) = println("¡El tigre $(t.color_pelaje) gana!")
# => pelear (generic function with 1 method)

pelear(tigre, Pantera())       # ¡El tigre anaranjado gana!
pelear(tigre, Leon("ROAR"))    # ¡El tigre anaranjado gana!

# Vamos a cambiar el comportamiento cuando el Gato sea específicamente un Leon.
pelear(t::Tigre, l::Leon) = println("El león con melena $(l.color_crin) gana.")
# => pelear (generic function with 2 methods)

pelear(tigre, Pantera())       # ¡El tigre anaranjado gana!
pelear(tigre, Leon("ROAR"))    # El león con melena verde gana.

# No necesitamos un tigre para poder luchar.
pelear(l::Leon, c::Gato) = println("El gato victorioso dice $(maullar(c)).")
# => pelear (generic function with 3 methods)

methods(pelear)
#=
  # 3 methods for generic function "pelear":
  pelear(t::Tigre,l::Leon) at none:2
  pelear(t::Tigre,c::Gato) at none:1
  pelear(l::Leon,c::Gato) at none:2
=#

pelear(Leon("balooga!"), Pantera())    # El gato victorioso dice grrr.
try
    # ERROR: `pelear` has no method matching pelear(::Pantera, ::Leon)
    pelear(Pantera(),Leon("RAWR"))
catch    # no hacer nada con la excepción atrapada
end

# Un metodo con el tipo Gato primero.
pelear(c::Gato,l::Leon) = println("El gato le gana al León")
#=
  Warning: New definition
      pelear(Gato,Leon) at none:1
  is ambiguous with:
      pelear(Leon,Gato) at none:1.
  To fix, define
      pelear(Leon,Leon)
  before the new definition.
  pelear (generic function with 4 methods)
=#

# Esta advertencia se debe a que no está claro que método de pelear
# será llamado en:
pelear(Leon("RAR"),Leon("cafe","rar"))    # El gato victorioso dice rar.

# El resultado puede ser diferente en otras versiones de Julia
pelear(l::Leon,l2::Leon) = println("Los leones llegan a un empate")

pelear(Leon("GR"),Leon("cafe","rar"))    # Los leones llegan a un empate


################################
# 7. Un vistazo de bajo nivel. #
################################

# Se puede echar un vistazo al código IR de LLVM y al código
# ensamblador generado.
area_cuadrado(l) = l * l    # => area_cuadrado (generic function with 1 method)

area_cuadrado(5)    # => 25

# ¿Qué sucede cuando damos area_cuadrada diferentes tipos de argumentos?
code_native(area_cuadrado, (Int32,))
#=
    .section  __TEXT,__text,regular,pure_instructions
  Filename: none
  Source line: 1      # prólogo
    push  RBP
    mov RBP, RSP
  Source line: 1
    imul  RDI, RDI    # elevar l al cuadrado
    mov RAX, RDI      # almacenar el resultado en RAX
    pop RBP           # restaurar el puntero base anterior
    ret               # el resultado estará en RAX
=#

code_native(area_cuadrado, (Float32,))
#=
    .section  __TEXT,__text,regular,pure_instructions
  Filename: none
  Source line: 1
    push  RBP
    mov RBP, RSP
  Source line: 1
    mulss XMM0, XMM0    # multiplicación escalar de presición simple (AVX)
    pop RBP
    ret
=#

code_native(area_cuadrado, (Float64,))
#=
    .section  __TEXT,__text,regular,pure_instructions
  Filename: none
  Source line: 1
    push  RBP
    mov RBP, RSP
  Source line: 1
    mulsd XMM0, XMM0    # multiplicación escalar de presición doble (AVX)
    pop RBP
    ret
=#

# Ten en cuenta que Julia usará instrucciones de punto flotante si el tipo de
# alguno de los argumentos es flotante.

# Vamos a calcular el área de un círculo.
area_circulo(r) = π * r * r    # area_circulo (generic function with 1 method)
area_circulo(5)                # 78.53981633974483

code_native(area_circulo, (Int32,))
#=
    .section  __TEXT,__text,regular,pure_instructions
  Filename: none
  Source line: 1
    push  RBP
    mov RBP, RSP
  Source line: 1
    cvtsi2sd  XMM1, EDI            # cargar entero r de la memoria
    movabs  RAX, 4477117456        # cargar constante matemática π
    movsd XMM0, QWORD PTR [RAX]
    mulsd XMM0, XMM1               # π * r
    mulsd XMM0, XMM1               # (π * r) * r
    pop RBP
    ret
=#

code_native(area_circulo, (Float64,))
#=
    .section  __TEXT,__text,regular,pure_instructions
  Filename: none
  Source line: 1
    push  RBP
    mov RBP, RSP
    movabs  RAX, 4477120336
    movsd XMM1, QWORD PTR [RAX]
  Source line: 1
    mulsd XMM1, XMM0
    mulsd XMM1, XMM0
    movaps  XMM0, XMM1
    pop RBP
    ret
=#
```

![Julia-tan](http://s27.postimg.org/x37ndhz0j/julia_tan_small.png)

## ¿Listo para más?

Para más detalles, lee el [manual de Julia](http://docs.julialang.org/en/release-0.3).

El mejor lugar para obtener ayuda con Julia, es en su amigable [lista de correos](https://groups.google.com/forum/#!forum/julia-users).
---
language: kotlin
contributors:
- ["S Webber", "https://github.com/s-webber"]
translators:
- ["Ivan Alburquerque", "https://github.com/AlburIvan"]
lang: es-es
filename: LearnKotlin-es.kt
---

Kotlin es un lenguaje estático tipado para la JVM, Android y el navegador. Es
100% interoperable con Java.
[Leer mas aqui.](https://kotlinlang.org/)

```java
// Los comentarios de una sóla línea comienzan con //

/*
 Los comentarios multilínea lucen así
*/

// La palabra clave "package" funciona de la misma manera que Java.

/*
El punto de entrada para un programa de Kotlin es una función llamada "main".
A dicha función se le pasa un arreglo que contiene los argumentos de la linea de comando.
*/
fun main(args: Array<String>) {
    /*
    La declaración de valores se realiza utilizando tanto "var" como "val".
    Las declaraciones "val" no pueden ser reasignadas, mientras que "var" sí.
    */
    val fooVal = 10 // más adelante no podremos reasignar fooVal con un valor distinto.
    var fooVar = 10
    fooVar = 20 // fooVar puede ser reasignado

    /*
    En la mayoría de los casos, Kotlin puede determinar cuál es el tipo de una variable,
    de tal manera que no tenemos que especificarlo explícitamente cada vez.
    Podemos declarar explícitamente el tipo de una variable así:
    */
    val foo: Int = 7

    /*
    Las cadenas pueden ser representadas de la misma manera que Java.
    El escape de caracteres se realiza con una barra invertida.
    */
    val fooString = "Mi Cadena está aquí!";
    val barString = "¿Imprimiendo en una nueva línea?\nNo hay problema!";
    val bazString = "¿Quíeres agregar una tabulación?\tNo hay problema!";
    println(fooString);
    println(barString);
    println(bazString);

    /*
    Una cadena está delimitada por comillas triple (""").
    Estas cadenas pueden contener saltos de línea y otros caracteres.
    */
    val fooRawString = """
    fun helloWorld(val name : String) {
       println("Hola, mundo!")
    }
    """
    println(fooRawString)

    /*
    Las cadenas pueden contener interpolación de cadenas.
    La interpolación de cadenas comienza con un signo de dólar ($).
    */
    val fooTemplateString = "$fooString tiene ${fooString.length} caracteres"
    println(fooTemplateString)

    /*
    Para que una variable pueda aceptar valor nulo se debe especificar
    explícitamente como anulable añadiendole ? a su tipo.
    Podemos acceder a una variable anulable mediante el uso del operador ?.
    Podemos utilizar el operador ?: para especificar un valor alternativo
    a usar si una variable es nula.
    */
    var fooNullable: String? = "abc"
    println(fooNullable?.length) // => 3
    println(fooNullable?.length ?: -1) // => 3
    fooNullable = null
    println(fooNullable?.length) // => null
    println(fooNullable?.length ?: -1) // => -1


    /*
    Las funciones pueden ser declaras usando la palabra clave "fun".
    Los argumentos de las funciones son especificados entre corchetes despues del nombre de la función.
    Los argumentos de las funciones pueden tener opcionalmente un valor por defecto.
    El tipo de retorno de las funciones, de ser requerido, es especificado despues del argumento.
    */
    fun hello(name: String = "mundo") : String {
        return "Hola, $name!"
    }
    println(hello("foo")) // => Hola, foo!
    println(hello(name = "bar")) // => Hola, bar!
    println(hello()) // => Hola, mundo!

    /*
    Un parametro de la función puede ser marcado con la palabra clave "vararg"
    que permite que una función acepte un numero variable de argumentos.
    */
    fun varargExample(vararg names: Int) {
        println("Argument tiene ${names.size} elementos")
    }
    varargExample() // => Argument tiene 0 elementos
    varargExample(1) // => Argument tiene 1 elementos
    varargExample(1, 2, 3) // => Argument tiene 3 elementos

    /*
    Cuando una función consiste de una sola expresión entonces las llaves
    pueden ser omitidas. El cuerpo es especificado despues del símbolo =
    */
    fun odd(x: Int): Boolean = x % 2 == 1
    println(odd(6)) // => false
    println(odd(7)) // => true

    // Si el tipo de retorno puede ser inferido entonces no se necesita
    // especificarlo.
    fun even(x: Int) = x % 2 == 0
    println(even(6)) // => true
    println(even(7)) // => false

    // Las funciones pueden tomar funciones como argumentos y 
    // retornar funciones.
    fun not(f: (Int) -> Boolean) : (Int) -> Boolean {
        return {n -> !f.invoke(n)}
    }

    // Las funciones con nombre pueden ser especificadas como argumentos
    // utilizando el operador ::.
    val notOdd = not(::odd)
    val notEven = not(::even)
    // Las funciones anónimas pueden ser especificadas como argumentos.
    val notZero = not {n -> n == 0}
    /*
    Si una función anónima tiene un solo parametro entonces la declaración
    puede ser omitida (junto con ->). El nombre del único parametro será "it".
    */
    val notPositive = not {it > 0}
    for (i in 0..4) {
        println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}")
    }

    // La palabra clave "class" es usada para declarar clases.
    class ExampleClass(val x: Int) {
        fun memberFunction(y: Int) : Int {
            return x + y
        }

        infix fun infixMemberFunction(y: Int) : Int {
            return x * y
        }
    }
    /*
    Para crear una nueva instancia llamamos al constructor.
    Nótese que Kotlin no usa la palabra clave "new".
    */
    val fooExampleClass = ExampleClass(7)
    // Las funciones miembros pueden ser llamadas usando la notación de punto (.)
    println(fooExampleClass.memberFunction(4)) // => 11
    /*
    Si una función ha sido marcada con la palabra clave "infix" entonces
    esta puede ser invocada usando la notación infija.
    */
    println(fooExampleClass infixMemberFunction 4) // => 28

    /*
    Las clases "data" son una manera concisa de crear clases que solo contengan datos.
    Los metodos "hashCode"/"equals" y "toString" son generados automáticamente.
    */
    data class DataClassExample (val x: Int, val y: Int, val z: Int)
    val fooData = DataClassExample(1, 2, 4)
    println(fooData) // => DataClassExample(x=1, y=2, z=4)

    // las clases de datos tienen una función "copy".
    val fooCopy = fooData.copy(y = 100)
    println(fooCopy) // => DataClassExample(x=1, y=100, z=4)

    // Los objetos pueden ser estructurados en múltiples variables.
    val (a, b, c) = fooCopy
    println("$a $b $c") // => 1 100 4

    // La función "with" es similar a la expresión de JavaScript "with".
    data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
    val fooMutableData = MutableDataClassExample(7, 4, 9)
    with (fooMutableData) {
        x -= 2
        y += 2
        z--
    }
    println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8)

    /*
    Podemos crear una lista utilizando la función "listOf".
    La lista será inmutable - los elementos no pueden ser añadidos o eliminados.
    */
    val fooList = listOf("a", "b", "c")
    println(fooList.size) // => 3
    println(fooList.first()) // => a
    println(fooList.last()) // => c
    // Los elementos de una lista se pueden acceder a través de su índice.
    println(fooList[1]) // => b

    // Una lista mutable puede ser creada usando la función "mutableListOf".
    val fooMutableList = mutableListOf("a", "b", "c")
    fooMutableList.add("d")
    println(fooMutableList.last()) // => d
    println(fooMutableList.size) // => 4

    // Podemos crear un set usando la función "setOf".
    val fooSet = setOf("a", "b", "c")
    println(fooSet.contains("a")) // => true
    println(fooSet.contains("z")) // => false

    // Podemos crear un mapa usando la función "mapOf".
    val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9)
    // Se puede acceder a los valores del mapa por su llave.
    println(fooMap["a"]) // => 8

    /*
    Las secuencias representan colecciones evaluadas diferidamente.
    Podemos crear una secuencia con la función "generateSequence".
    */
    val fooSequence = generateSequence(1, {it + 1})
    val x = fooSequence.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // Un ejemplo usando las secuencias para generar los números de Fibonacci:
    fun fibonacciSequence() : Sequence<Long> {
        var a = 0L
        var b = 1L

        fun next() : Long {
            val result = a + b
            a = b
            b = result
            return a
        }

        return generateSequence(::next)
    }
    val y = fibonacciSequence().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

    // Kotlin provee funciones de Orden-Mayor para trabajar con colecciones.
    val z = (1..9).map {it * 3}
            .filter {it < 20}
            .groupBy {it % 2 == 0}
            .mapKeys {if (it.key) "even" else "odd"}
    println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]}

    // Un bucle "for" puede ser usado con cualquier cosa que provea un iterador.
    for (c in "hello") {
        println(c)
    }

    // El bucle "while" funciona de la misma manera que en los demás lenguajes.
    var ctr = 0
    while (ctr < 5) {
        println(ctr)
        ctr++
    }
    do {
        println(ctr)
        ctr++
    } while (ctr < 10)

    /*
    "if" puede ser usado como una expresión que retorna un valor.
    Por esta razón el operador ternario ?: no es necesario en Kotlin.
    */
    val num = 5
    val message = if (num % 2 == 0) "even" else "odd"
    println("$num is $message") // => 5 is odd

    // "when" puede ser usado como alternativa a cadenas de "if-else if".
    val i = 10
    when {
        i < 7 -> println("primer bloque")
        fooString.startsWith("hello") -> println("segundo bloque")
        else -> println("else bloque")
    }

    // "when" puede ser usado con argumentos.
    when (i) {
        0, 21 -> println("0 or 21")
        in 1..20 -> println("in the range 1 to 20")
        else -> println("none of the above")
    }

    // "when" puede ser usado como una función que retorna un valor.
    var result = when (i) {
        0, 21 -> "0 or 21"
        in 1..20 -> "in the range 1 to 20"
        else -> "none of the above"
    }
    println(result)

    /*
    Podemos analizar si un objeto es de un tipo particular usando el operador "is".
    Si un objeto pasa un chequeo de tipo entonces éste se puede utilizar como
    ese tipo sin convertido de forma explícita.
     */
    fun smartCastExample(x: Any) : Boolean {
        if (x is Boolean) {
            // x es automaticamente convertido a Boolean
            return x
        } else if (x is Int) {
            // x es automaticamente convertido a Int
            return x > 0
        } else if (x is String) {
            // x  es automaticamente convertido a String
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(smartCastExample("Hola, mundo!")) // => true
    println(smartCastExample("")) // => false
    println(smartCastExample(5)) // => true
    println(smartCastExample(0)) // => false
    println(smartCastExample(true)) // => true

    /*
    Las extensiones son una manera de añadir nuevas funcionalidades a una clase.
    Estas son similares a la extensión de métodos en C#.
     */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("Hola, mundo!".remove('l')) // => Hoa, mundo!

    println(EnumExample.A) // => A
    println(ObjectExample.hello()) // => hola
}

// Las clases "enum" son similares a los tipos "enum" de Java.
enum class EnumExample {
    A, B, C
}

/*
La palabra clave "object" se puede utilizar para crear objetos únicos.
No podemos asignarlo a una variable, pero podemos hacer referencia a ella por su nombre.
Esto es similar a los objetos únicos de Scala
*/
object ObjectExample {
    fun hello() : String {
        return "hola"
    }
}
```

### Lectura Adicional

* [Kotlin tutorials (EN)](https://kotlinlang.org/docs/tutorials/)
* [Try Kotlin in your browser (EN)](http://try.kotlinlang.org/)
* [A list of Kotlin resources (EN)](http://kotlin.link/)
---
language: latex
lang: es-es
contributors:
    - ["Chaitanya Krishna Ande", "http://icymist.github.io"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
    - ["Sricharan Chiruvolu", "http://sricharan.xyz"]
translators:
    - ["Mario Pérez", "https://github.com/MarioPerezEsteso"]
filename: learn-latex-es.tex
---

```tex
% Todas las líneas comentadas comienzan con %
% No existen los comentarios multilínea

% LaTeX NO es un software de procesamiento de texto que cumple con
% "Lo que ves es lo que tienes" como MS Word u OpenOffice

% Todos los comandos de LaTeX comienzan con una contrabarra (\)

% Los documentos LaTeX comienzan definiendo el tipo de documento que se va a
% compilar. Algunos tipos de documentos son libros, informes, presentaciones,
% etc. Las opciones para el documento comienzan en los corchetes []. En este
% caso, se especifica que queremos utilizar una fuente de tamaño 12pt.
\documentclass[12pt]{article}

% A continuación, definimos los paquetes que utilizará el documento.
% Si quieres incluir gráficos, texto coloreado o código fuente de otro lenguaje,
% debes extender las funciones de LaTeX. Esto se consigue añadiendo paquetes.
% A continuación se incluirán los paquetes float y caption para figuras.
\usepackage{caption}
\usepackage{float}

% También podemos definir otras propiedades en el documento
\author{Chaitanya Krishna Ande, Colton Kohnke \& Sricharan Chiruvolu}
\date{\today}
\title{Learn LaTeX in Y Minutes!}

% Ahora estamos preparados para comenzar el documento
% Todo lo que se encuentre antes de esta línea se llama "El Preámbulo"
\begin{document}
% Si especificamos el autor, fecha y título, LaTeX creará una página como título
% por nosotros
\maketitle

% La mayoría de los artículos de investigación tienen un abstract. Es posible
% utilizar comandos predefinidos para ello.
% Esto debería aparecer en su orden lógico. Tras el título pero antes de las
% secciones principales del cuerpo.
% Este comando está disponible en los tipos de documentos article y report.
\begin{abstract}
 Documentación de LaTex escrita en LaTex.
\end{abstract}

% Los comandos de sección son intuitivos.
% Todos los títulos de secciones son añadidos automáticamente a la tabla de contenidos.
\section{Introducción}
Hola, mi nombre es Mario Pérez y estoy traduciendo este documento para aprender LaTex.

\section{Otra sección}
Este es el texto de otra sección. Creo que necesitará una subsección.

\subsection{Esto es una subsección} % Las subsecciones también son fáciles.
Creo que necesitamos otra más.

\subsubsection{Pitágoras}
Mejor ahora.
\label{subsec:pitagoras}

% Utilizando el asterisco podemos decirle a LaTeX que no ponga los números de secciones y subsecciones.
% Esto también funciona con otros comandos de LaTeX.
\section*{Esto es una sección no numerada}
¡No todas las secciones tienen que estar numeradas!

\section{Algunas notas}
LaTeX es generalmente bastante bueno situando el texto donde debe ir. Si una lína \\ necesita \\ ser \\ rota \\ puedes poner \textbackslash\textbackslash en el código fuente. \\

\section{Listas}
Las listas son de las cosas más fáciles de crear en LaTeX. Necesito ir a comprar mañana, así que vamos a crear una lista de la compra.
\begin{enumerate} % Esto crea una lista numerada.
  % \item crea un elemento
  \item Ensalada.
  \item 27 sandías.
  \item Pescado.
  % podemos incluso sobreescribir el número del ítem usando []
  \item[cuántos?] Plátanos.

  No es un ítem de la lista, pero sigue siendo parte de la enumeración.

\end{enumerate} % Todos los contextos deben tener un final.

\section{Matemáticas}

Uno de los usos principales de LaTeX es la producción de artículos académicos o técnicos. Normalmente relacionados con la ciencia y las matemáticas. Debido a esto, necesitamos poder añadir símbolos especiales a nuestro artículo.\\

En matemáticas hay muchos símbolos. Más de los que podemos encontrar en un teclado. Flechas o letras por nombrar un par.\\

Algunos símbolos juegan un papel fundamental en muchos artículos de investigación matemática. Así es como se establece que todo Y pertenece a X:  $\forall$ x $\in$ X. \\
He necesitado añadir el signo $ antes de los símbolos. Esto se debe a que cuando escribimos, estamos en modo texto. Sin embargo, los símbolos solo pueden utilizarse en modo matemático, al cual se entra con el signo $.
% Lo opuesto también se cumple. Una variable también puede ser mostrada en modo matemático, al que también se puede entrar con \[\]

\[a^2 + b^2 = c^2 \]

Mi letra griega favorita es $\xi$. También me gustan $\beta$, $\gamma$ y $\sigma$.
Todavía no he encontrado una letra griega que LaTeX no conozca.

Los operadores son también una parte esencial de un documento matemático: 
funciones trigonométricas ($\sin$, $\cos$, $\tan$), logaritmos y exponenciales ($\log$, $\exp$), límites ($\lim$), etc. tienen comandos predefinidos en LaTeX.

Vamos a escribir una ecuación para ver cómo se hace: \\

$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$

Las fracciones (numeradores-denominadores) pueden escribirse de la siguiente forma:

% 10 / 7
$^{10}/_{7}$

% Las fracciones relativamente complejas pueden escribirse como
% \frac{numerador}{denominador}
$\frac{n!}{k!(n - k)!}$ \\

También podemos insertar ecuaciones en un contexto de ecuación.

% Mostrar matemáticas en el contexto de ecuaciones
\begin{equation} % entra en modo matemático
    c^2 = a^2 + b^2.
    \label{eq:pitagoras} % para referencias
\end{equation} % Todos los contextos deben tener un final.

Podemos referenciar nuestra nueva ecuación.
Ecuación ~\ref{eq:pythagoras} también se conoce como el Teorema de Pitágoras, el cual también se encuentra en la sección ~\ref{subsec:pythagoras}. Muchas cosas pueden ser etiquetadas: figures, equations, sections, etc.

Los sumatorios e integrales son escritor son los comandos sum e int:

% Algunos compiladores de LaTeX se quejarán si hay líneas en blanco
% En un contexto de ecuación.
\begin{equation}
  \sum_{i=0}^{5} f_{i}
\end{equation}
\begin{equation}
  \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x
\end{equation}

\section{Figuras}

Vamos a insertar una figura. Situarla puede ser algo complicado.

\begin{figure}[H] % H aquí establece la situación de la figura.
    \centering % centra la figura en la página
    % Inserta una figura escalada por 0.8 el ancho de la página.
    %\includegraphics[width=0.8\linewidth]{right-triangle.png}
    % La línea anterior ha sido comentada para poder compilar este archivo. Por favor, usa tu imaginación.
    \caption{Triángulo con lados $a$, $b$, $c$}
    \label{fig:right-triangle}
\end{figure}

\subsection{Tablas}
También podemos insertar tablas de la misma manera que las figuras.

\begin{table}[H]
  \caption{Título para la tabla.}
  % los argumentos en {} describen cómo cada fila va a ser representada.
  \begin{tabular}{c|cc}
    Número & Nombre & Apellido \\
    \hline % una línea horizontal
    1 & Biggus & Dickus \\
    2 & Monty & Python
  \end{tabular}
\end{table}

% \section{Hyperlinks} % En construcción

\section{Haciendo que LaTeX no compile algo (por ejemplo, código fuente)}
Digamos que queremos incluir código fuente dentro de nuestro documento LaTex. En ese caso, debemos indicarle a LaTeX que no trate de compilarlo y simplemente lo muestre en el documento. Esto lo realizamos en el contexto verbatim.

% Hay otros paquetes para esta misma tarea, pero verbatim es el más básico.
\begin{verbatim}
  print("Hola Mundo!")
  a%b; % Podemos usar los signos % en verbatim.
  aleatorio = 4; # Número aleatorio
\end{verbatim}

\section{Compilación}

Ahora mismo te estarás preguntando cómo compilar este fabuloso documento y obtener un documento PDF.\\
Para obtener el documento final utilizando LaTeX hay que seguir los siguientes pasos:
  \begin{enumerate}
    \item Escribe el documento en texto plano.
    \item Compila el código para producir un PDF.
     Los pasos de compilación serán algo parecido a esto (en Linux): \\
     \begin{verbatim}
        $pdflatex learn-latex.tex learn-latex.pdf
     \end{verbatim}
  \end{enumerate}

Un gran número de editores LaTeX combinan ambos pasos para que sea más sencillo obtener el documento.

Escribe toda la información de formato en el paso 1 y con el paso 2 obtendrás el documento que has definido en el paso anterior.

\section{End}

Esto es todo por ahora.

% fin del documento
\end{document}
```

## Más información sobre LaTeX

* El wikilibro LaTeX: [https://es.wikibooks.org/wiki/Manual_de_LaTeX](https://es.wikibooks.org/wiki/Manual_de_LaTeX)
* Un tutorial real: [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/)
---
language: less
filename: learnless-es.less
lang: es-es
contributors:
  - ["Saravanan Ganesh", "http://srrvnn.me"]
translators:
  - ["César Suárez", "https://github.com/csuarez"]
---

Less es un pre-procesador CSS, que añade características como variables, anidación, mixins y más.
Less (y otros pre-procesadores como [Sass](http://sass-lang.com/) ayudan a los desarrolladores a escribir código mantenible y DRY (Don't Repeat Yourself).

```css


//Los comentarios de una línea son borrados cuando Less es compilado a CSS.

/* Los comentarios multi-línea se mantienen. */



/*Variables
==============================*/


/* Puedes almacenar un valor CSS (como un color) en una variable.
Usa el símbolo '@' para crear una variable. */

@primary-color: #a3a4ff;
@secondary-color: #51527f;
@body-font: 'Roboto', sans-serif;

/* Puedes usar las variables por toda tu hoja de estilos.
Ahora, si quieres cambiar un color, sólo lo tienes que hacer una vez.*/

body {
	background-color: @primary-color;
	color: @secondary-color;
	font-family: @body-font;
}

/* Esto compilará en: */

body {
	background-color: #a3a4ff;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/* Esto es mucho más mantenible que tener que cambiar el color
   cada vez que aparece en tu hoja de estilos. */



/* Mixins
==============================*/


/* Si tienes el mismo código para más de un elemento, puede que
   quieras reutilizarlo fácilmente. */

.center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* Puedes usar un mixin simplemente añadiendo el selector como un estilo. */

div {
	.center;
	background-color: @primary-color;
}

/* Esto compilará en: */

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
}

div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #a3a4ff;
}

/* Puedes omitir que se compile el código del mixin añadiendo un
   paréntesis después del selector. */

.center() {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
}

div {
  .center;
  background-color: @primary-color;
}

/* Esto compilará en: */
div {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
  background-color: #a3a4ff;
}



/* Anidación
==============================*/


/* Less te permite anidar selectores dentro de otros selectores. */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #f00;
	}
}

/* '&' es replazado por el selector padre. */
/* También se pueden anidar seudo clases. */
/* Ten en cuenta que anidar demasiado puede hacer tu código menos
   mantenible. Las buenas prácticas recomiendan no tener más de 3 niveles de
	 anidación. Por ejemplo: */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Compila en: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/* Funciones
==============================*/


/* Less ofrece funciones que pueden ser usadas para cumplir una gran variedad
   de tareas. Considera lo siguiente: */

/* Las funciones pueden ser llamadas usando su nombre y pasándole los argumentos
   requeridos. */

body {
  width: round(10.25px);
}

.header {
	background-color: lighten(#000, 0.5);
}

.footer {
  background-color: fadeout(#000, 0.25)
}

/* Compila en: */

body {
  width: 10px;
}

.header {
  background-color: #010101;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* Puedes definir tus propias funciones. Las funciones son muy similares a
   los mixins. Cuando tengas que elegir entre una función y un mixin, recuerda
   que los mixins son mejores para generar CSS, mientras que las funciones son
   mejores para la lógica que puedas necesitar en tu código Sass. Los ejemplos
   de la sección 'Operadores matemáticos' son candidatos ideales para ser
   usados como una función reusable. */

/* Esta función calcula la media de dos números. */

.average(@x, @y) {
  @average-result: ((@x + @y) / 2);
}

div {
  .average(16px, 50px);        // aquí se llama a la función
  padding: @average-result;    // y aquí se usa el valor "devuelto"
}

/* Compila en: */

div {
  padding: 33px;
}



/* Extender (Herencia)
==============================*/


/* Extend es una manera de compartir propiedades de un selector con otro. */

.display {
  height: 50px;
}

.display-success {
  &:extend(.display);
	border-color: #22df56;
}

/* Compila en: */
.display,
.display-success {
  height: 50px;
}
.display-success {
  border-color: #22df56;
}

/* Extender una declaración CSS es preferible a crear un mixin
   debido a la manera en la que Sass agrupa las clases que comparten
   los mismos estilos base. Si esto fuese hecho con un mixin, el ancho,
   alto y el borden aparecerían duplicados para cada una de las declaraciones
   que usasen el mixin. Esto no afectará a tu workflow, pero infla
   innecesariamente los ficheros generados por el compilador Less. */


/*Partials and Imports
==============================*/



/* Less allows you to create partial files. This can help keep your Less
   code modularized. Partial files conventionally begin with an '_',
   e.g. _reset.less. and are imported into a main less file that gets
   compiled into CSS */

/* Consider the following CSS which we'll put in a file called _reset.less */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

 /* Less ofrece @import para poder importar parciales a un fichero. Esto se
 		diferencia del @import de CSS en que no hace otra petición HTTP para
		importar el fichero, sino que combina el código importado en el código
		compilado. */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* Compila en: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/* Operaciones matemáticas
==============================*/


/* Less ofrece los siguientes operadores: +, -, *, / y %. Estos son útiles
   para calcular valores directamente en tu código Less en vez de usar valores
   calculados a mano. Mira el siguiente ejemplo que prepara un sencillo diseño
   de dos columnas. */

@content-area: 960px;
@main-content: 600px;
@sidebar-content: 300px;

@main-size: @main-content / @content-area * 100%;
@sidebar-size: @sidebar-content / @content-area * 100%;
@gutter: 100% - (@main-size + @sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: @main-size;
}

.sidebar {
  width: @sidebar-size;
}

.gutter {
  width: @gutter;
}

/* Compila en: */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}


```

## Practica Less

Si quieres probar Less en tu navegador, prueba:
* [Codepen](http://codepen.io/)
* [LESS2CSS](http://lesscss.org/less-preview/)

## Compatibilidad

Sass puede ser usado en cualquier proyecto mientras tengas un programa que lo compile en CSS. Quizás quieras comprobar si el CSS que estás usando es compatible con tus navegadores objetivo.

[QuirksMode CSS](http://www.quirksmode.org/css/) y [CanIUse](http://caniuse.com) son buenos recursos para comprobar la compatibilidad de navegadores.

## Más información
* [Documentación oficial (EN)](http://lesscss.org/features/)
* [Less CSS - Guía para principiantes (EN)](http://www.hongkiat.com/blog/less-basic/)
---
language: LiveScript
filename: learnLivescript-es.ls
contributors:
    - ["Christina Whyte", "http://github.com/kurisuwhyte/"]
translators:
    - ["Daniel Zendejas", "http://github.com/DanielZendejas/"]
lang: es-es
---

LiveScript es un lenguaje funcional compilado sobre Javascript. Comparte
la mayoría de la semántica con este mismo lenguaje. Composición de funciones,
comparación de patrones y muchas otras cosas son las adiciones que hace
LiveScript. Está inspirado en lenguajes como Haskell, F# y Scala.

Livescript es un bifurcación de [Coco][], que en sí mismo es una bifurcación
de [CoffeeScript][]. El lenguaje es estable, y una nueva versión está en 
desarrollo activo para traer aún más funciones.

[Coco]: http://satyr.github.io/coco/
[CoffeeScript]: http://coffeescript.org/

La retroalimentación siempre es bienvenida, así que sientete libre de
contactarme en [@kurisuwhyte](https://twitter.com/kurisuwhyte) :)

```coffeescript
# Justo como su primo CoffeeScript, LiveScript usa símbolos de gato para
# comentarios de una sola línea

/*
 Comentarios multi-línea son escritos con estilo de C. Usa este estilo si quieres
 que los comentarios se preserven en el output de Javascript
 */
```
```coffeescript
# En lo que a la sintaxis se refiere, LiveScript usa indentación para delimitar
# bloques en lugar de llaves {} y espacios para aplicar funciones, en lugar de
# paréntesis.

########################################################################
## 1. Valores básicos
########################################################################

# La carencia de valor se define con la palabra `void` en lugar de `undefined`
void            # igual que `undefined` pero más seguro (no puede ser sobre escrito)

# Ningún valor válido se representa con Null.
null

# El valor básico más pequeño es de tipo lógico: 
true
false

# Y tiene múltiples alias que significan lo mismo:
on; off
yes; no

# Luego vienen los números. Estos número con punto flotante tienen la misma 
# precisión que los de JS:
10
0.4     # Note que el `0` al inicio es requerido

# Para fines de fácil lectura, puedes usar guiones bajos y sufijos en un
# número, y estos serán ignorados por el compilador.
12_344km

# Los Strings son secuencias de caracteres inmutables, como en JS:
"Christina"             # ¡Los apóstrofes están bien!
"""Strings
   de muchas
   líneas
   están
   bien
   también."""

# A veces quieres codificar un palabra clave, la diagonal invertida sirve para esto:
\keyword                # => 'keyword'


# Los arreglos son colecciones ordenadas de datos:
frutas =
  * \manzana
  * \naranja
  * \pera

# Una forma más concisa de representarlos son con corchetes: 
frutas = [ \manzana, \naranja, \pera ]

# Esta es una conveniente de crear listas de Strings, usando
# espacio en blanco para delimitar los items:
frutas = <[ manzana naranja pera ]>

# Puedes recuperar un item usando su índice (empezando en 0):
frutas[0]       # => "manzana"

# Los objetos son colecciones de pares llave/valor sin ordenar, entre otras cosas,
# (detallaremos más al respecto en un momento):
persona =
  nombre: "Christina"
  gusta:
    * "gatitos"
    * "otras cosas"

# Otra vez, puedes expresar el objeto con más consistencia con llaves {}:
persona = {nombre: "Christina", gusta: ["gatitos", "otras cosas"]}

# Puedes conseguir un valor por medio de su llave:
persona.nombre    # => "Christina"
persona["nombre"]  # => "Christina"


# Las expresiones regulares tienen la misma sintaxis que en JavaScript:
expresion-regular = /\s$/

# A excepción de que puedes hacer expresiones de múltiples líneas
# (los comentarios y espacios se ignoran):
expresion-regular = //
        function\s+(.+)         # nombre
        \s* \((.*)\) \s*        # argumentos
        { (.*) }                # cuerpo
        //


########################################################################
## 2. Operaciones básicas
########################################################################

# Los operadores aritméticos son los mismos que en JavaScript:
1 + 2   # => 3
2 - 1   # => 1
2 * 3   # => 6
4 / 2   # => 2
3 % 2   # => 1


# Las comparaciones son casi las mismas, excepto `==` que es igual
# a `===` en. El operador `==` de JS en LiveScript es `~=`, y `===`
# permite comparaciones entre objetos y arreglos, y también 
# comparasiones más estrictas:
2 == 2          # => true
2 == "2"        # => false
2 ~= "2"        # => true
2 === "2"       # => false

[1,2,3] == [1,2,3]        # => false
[1,2,3] === [1,2,3]       # => true

+0 == -0     # => true
+0 === -0    # => false

# Otros operadores relacionales incluyen <, <=, > and >=

# Los valores lógicos pueden ser combinados mediante los operadores 
# lógicos `or`, `and` and `not`: 
true and false  # => false
false or true   # => true
not false       # => true

# Las colecciones también tienen algunos operadores adicionales: 
[1, 2] ++ [3, 4]                # => [1, 2, 3, 4]
'a' in <[ a b c ]>              # => true
'nombre' of { nombre: 'Chris' }     # => true


########################################################################
## 3. Funciones
########################################################################        

# Como LiveScript es funcional, uno esperaría que las funciones recibirían
# un buen tratamiento. En LiveScript es más que aparente que las funciones
# son de primera clase: 
suma = (primerElemento, segundoElemento) -> primerElemento + segundoElemento
add 1, 2        # => 3

# Las funciones que no reciben argumentos son llamadas rápidamente
dos = -> 2
dos!

# LiveScript, al igual que JS, aplica ámbitos (alcance) a sus variables y 
# tiene cierres (closures) también. A diferencia de JavaScript, el operador
# `=` sirve como declaración y siempre declarará la variable en lado izquierdo.

# El operador `:=` está disponible para *reusar* un nombre del ámbito del padre.

# Puedes acceder a los argumentos de una función para conseguir
# los datos internos de una estructura de datos rápidamente: 
cola = ([cabeza, ...resto]) -> resto
cola [1, 2, 3]  # => [2, 3]

# También puedes transformar argumentos usando operadores binarios o unarios.
# Argumentos por defecto también son posibles
foo = (a = 1, b = 2) -> a + b
foo!    # => 3

# También puedes usarlo para clonar un argumento en particular para evitar efectos
# secundarios, por ejemplo: 
copiar = (^^objetivo, fuente) ->
  for k,v of fuente => objetivo[k] = v
  objetivo
a = { a: 1 }
copiar a, { b: 2 }        # => { a: 1, b: 2 }
a                       # => { a: 1 }

# Una función puede ser abreviada usando una flecha larga en lugar de una corta:
suma = (primerElemento, segundoElemento) --> primerElemento + segundoElemento
sumaAbreviada = suma 1
sumaAbreviada 2          # => 3

# Las funciones obtienen un argumento `it` implícito, incluso si no declaras
# ningún argument
identidad = -> it
identidad 1      # => 1

# Los operadores no son funciones en LiveScript. ¡Pero se pueden convertir fácilmente
# en una! Presentamos el seccionamiento de operadores: 
dividir-entre-2 = (/ 2)
[2, 4, 8, 16].map(dividir-entre-2) .reduce (+)

# LiveScript vive de otras cosas aparte de las funciones. Como en cualquier lenguaje 
# funcional obtienes medios para componer (juntar) funciones: 
doble-menos-uno = (- 1) . (* 2)

# A parte de la clásica fórmula matemática `f . g`, también cuentas co los operadores
# `>>` y `<<`, que describen el flujo de los valores dentro de las funciones:
double-minus-one = (* 2) >> (- 1)
double-minus-one = (- 1) << (* 2)

# Hablando del flujo de los valores, LiveScript también tiene los operadores `|>` y `<|`
# que aplican un valor a una función:
map = (f, xs) --> xs.map f
[1 2 3] |> map (* 2)            # => [2 4 6]

# También puedes escoger dónde quieres que el valor se posicione, sólo márcalo con un
# guíon bajo:
reducir = (f, xs, initial) --> xs.reducir f, initial
[1 2 3] |> reducir (+), _, 0     # => 6

# El guíon bajo también se usa para apartar lugares para tus argumentos, por ejemplo: 
division = (dividendo,divisor) -> dividendo / divisor
dividir-entre-2 = division _, 2
dividir-entre-2 4      # => 2

# Por último, LiveScript tiene back-calls (útiles mecanismos para hacer
# callbacks.). A pesar de esto deberías intentar formas más funcionales de hacerlo,
# como Promises:
leerArchivo = (nombre, f) -> f name
a <- leerArchivo 'foo'
b <- leerArchivo 'bar'
console.log a + b

# Igual a:
leerArchivo 'foo', (a) -> leerArchivo 'bar', (b) -> console.log a + b


########################################################################
## 4. Patrones, guardias y control de flujo
########################################################################

# Puedes bifurcar cálculos con la expresión `if...else`:
x = if n > 0 then \positive else \negative

# En lugar de `then`, puedes usar `=>`
x = if n > 0 => \positivo
    else        \negativo
    
# A pesar de esto, a las condiciones complejas es mejor expresarlas con el `switch`:
y = {}
x = switch
  | (typeof y) is \number => \numero
  | (typeof y) is \string => \string
  | 'length' of y         => \arreglo
  | otherwise             => \objeto      # `otherwise` y `_` son lo mismo.

# Los cuerpos de las funciones, declaraciones y asignaciones tienen un `switch` por defecto,
# así que no necesitas escribirlo nuevamente: 

take = (n, [x, ...xs]) -->
                        | n == 0 => []
                        | _      => [x] ++ take (n - 1), xs


########################################################################
## 5. Comprehensions (Auxiliares)
########################################################################

# Mientras que los auxiliares funcionales (para lidiar con listas y objetos)
# están en la librería estándar de JavaScript (y complementada con prelude-ls,
# que es una "librería estándar" de LiveScipt) los "comprehensions" (Auxiliares)
# usualemente te permiten hacer lo mismo pero más rápido y con una sintaxis más
# comprehensible (de ahí su nombre en inglés):
unoAVeinte = [1 to 20]
pares       = [x for x in oneToTwenty when x % 2 == 0]

# `when` y `unless` pueden ser usados como filtros en el auxiliar.

# Los auxiliares para objetos funcionan de la misma forma, excepto que regresa un 
# objeto en lugar de un arreglo:
copiar = { [k, v] for k, v of source }


########################################################################
## 4. PROGRAMACIÓN ORIENTADA A OBJETOS
########################################################################

# Mientras que LiveScript es un lenguaje funcional en la mayoría de los 
# aspectos, también brinda ayudas para la programación orientada a objetos.
# Algunas de estas ayudas son la sintaxis para las clases y un poco de "azucar"
# para las clases heredada de CoffeeScript:
class Animal
  (@nombre, tipo) ->
    @tipo = tipo
  action: (accion) -> "*#{@nombre} (un #{@tipo}) #{accion}*"

class Gato extends Animal
  (@nombre) -> super @nombre, 'gato'
  ronronear: -> @action 'ronronea'

gatito = new Gato 'Mei'
gatito.purr!      # => "*Mei (un gato) ronronea*"

# A parte del clásico patrón de herencia simple, también puedes proveer
# cuantas mezclas quieras para una clase. Las mezclas sólo son objetos:
Abrazable =
  abrazar: -> @action 'es abrazado'

class GatoAbrazable extends Gato implements Abrazable

gatito = new GatoAbrazable 'Ronroneo'
gatito.abrazar!     # => "*Mei (un gato) es abrazado*"
```

## Más recursos

Existe mucho más sobre LiveScript, pero esto debe bastar para que empieces.
El [sitio oficial](http://livescript.net/) tiene mucha información sobre el 
lenguaje, y un compilador en linea para que pruebes cosas inmediatamente.

También querras probar un poco de [prelude.ls](http://gkz.github.io/prelude-ls/), 
y probar el canal `#livescript` en la red Freenode.
---
language: markdown
filename: markdown-es.md
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Daniel Zendejas", "https://github.com/DanielZendejas"]
lang: es-es
---

Markdown fue creado por John Gruber en 2004. Su propósito es ser una sintaxis fácil de leer y escribir que se convierta
fácilmente a HTML (y, actualmente, otros formatos también).

¡Denme toda la retroalimentación que quieran! / ¡Sientanse en la libertad de hacer forks o pull requests!


```markdown
<!-- Markdown está basado en HTML, así que cualquier archivo HTML es Markdown
válido, eso significa que podemos usar elementos HTML en Markdown como, por
ejemplo, el comentario y no serán afectados por un parseador Markdown. Aún 
así si creas un elemento HTML en tu archivo Markdown no podrás usar sintaxis
Markdown dentro de él. -->

<!-- La implementación de Markdown cambia de acuerdo al parseador. Esta
guía servirá para clarificar cuales características son universales y
cuales son específicas de cada parseador-->

<!-- Headers -->
<!-- Puedes crear headers HTML fácilmente precediendo al texto con una serie
de símbolos de números (#)-->

# Esto es un <h1>
## Esto es un <h2>
### Esto es un <h3>
#### Esto es un <h4>
##### Esto es un <h5>
###### Esto es un <h6>

<!-- Markdown también nos proveé con dos alternativas para indicar h1 y h2 -->
Esto es un h1
=============

Esto es un h2
-------------

<!-- Estilos para texto plano -->
<!-- El texto puede ser fácilmente estilizado con italicas, negritas o tachado 
usando markdown -->

*Este texto está en itálicas.*
_Al igual que este texto._

**Este texto está en negritas.**
__Al igual que este texto.__

***Este texto tiene ambos estilos.***
**_Al igual que este!_**
*__¡Y este!__*

<!-- En GitHub Flavored Markdown, el cual es usado para mostrar archivos 
Markdown en GitHub, también tenemos: -->

~~Este texto está tachado.~~

<!-- Los párrafos son una o múltiples líneas de texto adyacentes separadas por 
una o múltiples líneas en blanco-->

Este es un párrafo. Estoy escribiendo un párrafo, ¿No es divertido?

Ahora estoy en el párrafo dos.
¡Sigo en el párrafo dos!

¡Estoy en el párrafo tres!

<!-- Si en algún momento quieres insertar un break HTML <br />, puedes terminar
un párrafo con dos o más espacios y luego empieza un párrafo nuevo-->

Termino con dos espacios (selecciona esta línea completa para que los veas).  

¡Hay un <br /> arriba de mí!

<!-- Las citas de bloque son fáciles y se pueden hacer con el caracter >. -->

> Esta es una cita de bloque. Puedes
> envolver tus líneas manualmente y poner un `>` antes de cada línea o puedes dejar que tus líneas sean muy largas y que se envuelvan solas.
> No hay diferencia, siempre y cuando empiecen con `>`.

> ¿También puedes usar más de un nivel
>> de indentación?
> Esto es muy útil ¿No?

<!-- Listas -->
<!-- Las listas desordenadas se hacen usando asteriscos, símbolos de más,
 o guiones -->

* Item
* Item
* Otro item

o

+ Item
+ Item
+ Un item más

o

- Item
- Item
- El último item

<!-- Las listas ordenadas se logran con un número seguido de un punto -->

1. Item uno
2. Item dos
3. Item tres

<!-- Aunque Markdown mostrará los items correctamente en orden, esto no
es una buena idea -->

1. Item uno
1. Item dos
1. Item tres
<!-- (Esto muestra lo mismo que el ejemplo de arriba) -->

<!-- También puedes usar sub-listas -->

1. Item uno
2. Item dos
3. Item tres
    * Sub-item
    * Sub-item
4. Item cuatro

<!-- Bloques de código -->
<!-- Puedes indicar un bloque de código (usan los elementos <code>) indentando 
una línea con cuatro espacios o un tab-->

    Esto es código
    Esto también

<!-- También puedes insertar dos tabs (o cuatro espacios adicionales)
para indentar dentro del código -->

    my_array.each do |item|
        puts item
    end

<!-- Código dentro de la línea puede ser escrito usando la comilla ` -->

¡John no sabía lo que la función `go_to()` hacía!

<!-- Con GitHub Flavored Markdown, puedes usar una sintaxis especial para código -->

\`\`\`ruby <!-- quita esas comillas cuando lo hagas, deja sólo ```ruby ! -->
def foobar
    puts "Hello world!"
end
\`\`\` <!-- aquí también, sin comillas, sólo ``` -->

<!-- El texto de arriba no necesita indentación, aparte GitHub usará
resaltará la sintaxis del lenguaje que especifiques después de ``` -->

<!-- Regla horizontal (<hr />) -->
<!-- Las reglas horizontales se agregan fácilmente con tres o más asteriscos o guiones,
con o sin espacios. -->

***
---
- - - 
****************

<!-- Ligas -->
<!-- Una de las mejores cosas de Markdown es la facilidad para hacer ligas. Pon
el texto a mostrar en corchetes [] seguidos por la URL en paréntesis () -->

[¡Haz click!](http://test.com/)

<!-- También puedes agregar el titulo de la liga usando comillas dentro de los paréntesis -->

[¡Haz click!](http://test.com/ "Liga al test.com")

<!-- También funcionan las rutas relativas. -->

[Ir a la música](/music/).

<!-- Markdown también soporta ligas con estilo de referencia -->

¡[Has click a esta liga][liga1] para más información!
[También mira esta liag][foobar] si quieres.




<!-- El título también puede estar en comillas simples o dentro de paréntesis,
también se pueden omitir completamente. Las referencias pueden estar en cualquier
lugar en tu documento y los IDs de referencia pueden ser lo que sea mientras sean únicos. -->

<!-- También hay "nombramiento implicito" el cual te permite usar el texto de la liga como id -->

[Esta][] es una liga.



<!-- Pero no se usa comúnmente. -->

<!-- Imagenes -->
<!-- Las imagenes se hacen de la misma forma que las ligas pero con un símbolo de exclamaciónal frente! -->

![Esta es una etiqueta (texto alternativo) para mi imagen](http://imgur.com/myimage.jpg "Un titulo opcional")

<!-- Y el estilo de referencia funciona como se espera -->

![Esta es una etiqueta.][myimage]



<!-- Misceláneos -->
<!-- Auto-ligas -->

<http://testwebsite.com/> equivale a
[http://testwebsite.com/](http://testwebsite.com/)

<!-- Auto-ligas para correos electrónicos -->

<foo@bar.com>

<!-- Escapando caracteres -->

Quiero escribir *este texto rodeado por asteriscos* pero no quiero que esté en itálicas,
así que hago esto: \*Este texto está rodeado de asteriscos\*.

<!-- Tablas -->
<!-- Las tablas sólo están disponibles en GitHub Flavored Markdown y son un poco pesadas,
pero si de verdad las quieres: -->

| Col1         | Col2     | Col3          |
| :----------- | :------: | ------------: |
| Izquierda | Centrado | Derecha |
| blah         | blah     | blah          |

<!-- o, para los mismos resultados -->

Col 1 | Col2 | Col3
:-- | :-: | --:
Ugh esto es feo | has que | pare.

<!-- ¡El fin! -->

```

Para más información, mira el post oficial de John Gruber's [aquí](http://daringfireball.net/projects/markdown/syntax) y la gran referencia de Adam Pritchard's [aquí](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: Objective-C
contributors:
    - ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
    - ["Yannick Loriot", "https://github.com/YannickL"]
    - ["Levi Bostian", "https://github.com/levibostian"]
translators:
    - ["David Hsieh", "http://github.com/deivuh"]
lang: es-es
filename: LearnObjectiveC-es.m
---
Objective C es el lenguaje de programación principal utilizado por Apple para los sistemas operativos OS X y iOS y sus respectivos frameworks, Cocoa y Cocoa Touch.
Es un lenguaje de programación para propósito general que le agrega al lenguaje de programación C una mensajería estilo "Smalltalk".


```objective_c
// Los comentarios de una sola línea inician con //

/*
Los comentarios de múltiples líneas se ven así.
*/

// Importa los encabezados de Foundation con #import
// Utiliza <> para importar archivos globales (generalmente frameworks)
// Utiliza "" para importar archivos locales (del proyecto)
#import <Foundation/Foundation.h>
#import "MyClass.h"

// Si habilitas módulos para proyectos de iOS >= 7.0 u OS X >= 10.9 en
// Xcode 5, puedes importarlos de la siguiente manera:
@import Foundation;

// El punto de entrada de tu programa es una función llamada 
// main con un tipo de retorno entero. 
int main (int argc, const char * argv[])
{
    // Crear un autorelease pool para manejar la memoria al programa
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // Si se utiliza el conteo automático de referencias (ARC), 
    // utiliza @autoreleasepool:    
    @autoreleasepool {

    // Utiliza NSLog para imprimir líneas a la consola
    NSLog(@"Hello World!"); // Imprimir el string "Hello World!"
 
    ///////////////////////////////////////
    // Tipos y variables
    ///////////////////////////////////////
    
    // Declaraciones de primitivos
    int myPrimitive1  = 1;
    long myPrimitive2 = 234554664565;
    
    // Declaraciones de objetos
    // Pon el * como prefijo de los nombre de las variables para declaraciones
    // de objetos de tipos fuertes 
    MyClass *myObject1 = nil;  // Tipo fuerte
    id       myObject2 = nil;  // Tipo débil
    // %@ es un objeto
    // 'description' es una convención para mostrar el valor de los objetos
    NSLog(@"%@ and %@", myObject1, [myObject2 description]); 
    // imprime => "(null) and (null)"
    
    // String
    NSString *worldString = @"World";
    NSLog(@"Hello %@!", worldString); // imprime => "Hello World!" 
    // NSMutableString es una versión mutable del objeto NSString
    NSMutableString *mutableString = [NSMutableString stringWithString:@"Hello"];
    [mutableString appendString:@" World!"];
    NSLog(@"%@", mutableString); // imprime => "Hello World!"
    
    // Literales de caracteres
    NSNumber *theLetterZNumber = @'Z';
    char theLetterZ            = [theLetterZNumber charValue]; // o 'Z'
    NSLog(@"%c", theLetterZ);

    // Literales de enteros
    NSNumber *fortyTwoNumber = @42;
    int fortyTwo             = [fortyTwoNumber intValue]; // o 42
    NSLog(@"%i", fortyTwo);
    
    NSNumber *fortyTwoUnsignedNumber = @42U;
    unsigned int fortyTwoUnsigned    = [fortyTwoUnsignedNumber unsignedIntValue]; // o 42
    NSLog(@"%u", fortyTwoUnsigned);
    
    NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42];
    short fortyTwoShort           = [fortyTwoShortNumber shortValue]; // o 42
    NSLog(@"%hi", fortyTwoShort);

    NSNumber *fortyOneShortNumber   = [NSNumber numberWithShort:41];
    unsigned short fortyOneUnsigned = [fortyOneShortNumber unsignedShortValue]; // o 41
    NSLog(@"%u", fortyOneUnsigned);
    
    NSNumber *fortyTwoLongNumber = @42L;
    long fortyTwoLong            = [fortyTwoLongNumber longValue]; // o 42
    NSLog(@"%li", fortyTwoLong);

    NSNumber *fiftyThreeLongNumber   = @53L;
    unsigned long fiftyThreeUnsigned = [fiftyThreeLongNumber unsignedLongValue]; // o 53
    NSLog(@"%lu", fiftyThreeUnsigned);

    // Literales de punto flotante
    NSNumber *piFloatNumber = @3.141592654F;
    float piFloat           = [piFloatNumber floatValue]; // o 3.141592654f
    NSLog(@"%f", piFloat); // imprime => 3.141592654
    NSLog(@"%5.2f", piFloat); // imprime => " 3.14"
    
    NSNumber *piDoubleNumber = @3.1415926535;
    double piDouble          = [piDoubleNumber doubleValue]; // o 3.1415926535
    NSLog(@"%f", piDouble);
    NSLog(@"%4.2f", piDouble); // imprime => "3.14"

    // NSDecimalNumber es una clase de punto-fijo que es más preciso que float o double    
    NSDecimalNumber *oneDecNum = [NSDecimalNumber decimalNumberWithString:@"10.99"];
    NSDecimalNumber *twoDecNum = [NSDecimalNumber decimalNumberWithString:@"5.002"];
    // NSDecimalNumber no tiene la capacidad de utilizar los operadores estándares 
    // +, -, * , /, por lo que cuenta con sus propios operadores:    
    [oneDecNum decimalNumberByAdding:twoDecNum]; 
    [oneDecNum decimalNumberBySubtracting:twoDecNum];
    [oneDecNum decimalNumberByMultiplyingBy:twoDecNum];
    [oneDecNum decimalNumberByDividingBy:twoDecNum];
    NSLog(@"%@", oneDecNum); // imprime => 10.99 como NSDecimalNumber es inmutable

    // Literales BOOL
    NSNumber *yesNumber = @YES;
    NSNumber *noNumber  = @NO;
    // o
    BOOL yesBool = YES;
    BOOL noBool  = NO;
    NSLog(@"%i", yesBool); // prints => 1

    // Objecto arreglo
    // Puede contener diferentes tipos de datos, pero deben de ser un objeto de
    // Objective-C    
    NSArray *anArray      = @[@1, @2, @3, @4];
    NSNumber *thirdNumber = anArray[2];
    NSLog(@"Third number = %@", thirdNumber); // imprime => "Third number = 3"
    // NSMutableArray es una versión mutable de NSArray, permitiendo el cambio
    // de los elementos del arreglo y el agrandado o encojimiento del objeto arreglo.
    // Conveniente, pero no tan eficiente como NSArray en cuanto a rendimiento. 
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:2];
    [mutableArray addObject:@"Hello"];
    [mutableArray addObject:@"World"];
    [mutableArray removeObjectAtIndex:0];
    NSLog(@"%@", [mutableArray objectAtIndex:0]); // imprime => "World"

    // Objecto Diccionario
    NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
    NSObject *valueObject     = aDictionary[@"A Key"];
    NSLog(@"Object = %@", valueObject); // imprime => "Object = (null)"
    // NSMutableDictionary también está disponible como un objeto mutable
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:2];
    [mutableDictionary setObject:@"value1" forKey:@"key1"];
    [mutableDictionary setObject:@"value2" forKey:@"key2"];
    [mutableDictionary removeObjectForKey:@"key1"];

    // Objeto de Set
    NSSet *set = [NSSet setWithObjects:@"Hello", @"Hello", @"World", nil];
    NSLog(@"%@", set); // imprime => {(Hello, World)} (el orden puede variar)
    // NSMutableSet también está disponible como un objeto mutable
    NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:2];
    [mutableSet addObject:@"Hello"];
    [mutableSet addObject:@"Hello"];
    NSLog(@"%@", mutableSet); // prints => {(Hello)}

    ///////////////////////////////////////
    // Operadores
    ///////////////////////////////////////
    
    // Los operadores funcionan como en el lenguaje C
    // Por ejemplo:
    2 + 5; // => 7
    4.2f + 5.1f; // => 9.3f
    3 == 2; // => 0 (NO)
    3 != 2; // => 1 (YES)
    1 && 1; // => 1 (and lógico)
    0 || 1; // => 1 (or lógico)
    ~0x0F; // => 0xF0 (negación bitwise)
    0x0F & 0xF0; // => 0x00 (AND bitwise)
    0x01 << 1; // => 0x02 (acarreamiento a la izquierda bitwise (por 1))

    ///////////////////////////////////////
    // Estructuras de control
    ///////////////////////////////////////

    // Declaraciones If-Else
    if (NO)
    {
        NSLog(@"I am never run");
    } else if (0)
    {
        NSLog(@"I am also never run");
    } else
    {
        NSLog(@"I print");
    }

    // Declaración Switch
    switch (2)
    {
        case 0:
        {
            NSLog(@"I am never run");
        } break;
        case 1:
        {
            NSLog(@"I am also never run");
        } break;
        default:
        {
            NSLog(@"I print");
        } break;
    }
    
    // Declaración de ciclos While
    int ii = 0;
    while (ii < 4)
    {
        NSLog(@"%d,", ii++); // ii++ incrementa ii en la misma línea, luego de 
                             // utilizar su valor
    } // imprime => "0," 
      //           "1,"
      //           "2,"
      //           "3,"

    // Declaración de ciclos For
    int jj;
    for (jj=0; jj < 4; jj++)
    {
        NSLog(@"%d,", jj);
    } // imprime => "0," 
      //           "1,"
      //           "2,"
      //           "3,"
     
    // Declaraciones foreach
    NSArray *values = @[@0, @1, @2, @3];
    for (NSNumber *value in values)
    {
        NSLog(@"%@,", value);
    } // imprime => "0," 
      //           "1,"
      //           "2,"
      //           "3,"

    // Objeto de ciclos For. Puede ser utilizado con cualquier tipo de objecto de 
    // Objective-C
    for (id item in values) { 
        NSLog(@"%@,", item); 
    } // imprime => "0," 
      //           "1,"
      //           "2,"
      //           "3,"

    // Declaraciones Try-Catch-Finally
    @try
    {
        // Tus declaraciones aquí
        @throw [NSException exceptionWithName:@"FileNotFoundException"
                            reason:@"File Not Found on System" userInfo:nil];
    } @catch (NSException * e) // utiliza: @catch (id exceptionName) para atrapar 
                               // todos los objetos
    {
        NSLog(@"Exception: %@", e);
    } @finally
    {
        NSLog(@"Finally. Time to clean up.");
    } // imprime => "Exception: File Not Found on System"
      //           "Finally. Time to clean up."

    // Los objetos NSError son útiles para argumentos de función para los 
    // errores de usuario.    
    NSError *error = [NSError errorWithDomain:@"Invalid email." code:4 userInfo:nil];
 
    ///////////////////////////////////////
    // Objetos
    ///////////////////////////////////////
    
    // Crea una instancia de objeto alocando memoria e inicializándola
    // Un objeto no es completamente funcional hasta que ambos pasos hayan sido
    // completados
    MyClass *myObject = [[MyClass alloc] init];
        
    // El modelo de programación orientada a objetos de Objective-C es basada en
    // el envío de mensajes a instancias de objetos
    // En Objective-C no se llama a un método; se envía un mensaje    
    [myObject instanceMethodWithParameter:@"Steve Jobs"];

    // Limpiar la memoria que se utilizó en el programa    
    [pool drain];

    // Fin de @autoreleasepool
    }
    
    // Fin del programa
    return 0;
}

///////////////////////////////////////
// Clases y funciones
///////////////////////////////////////

// Declara tu clase en archivo de encabezado (MyClass.h)
// Sintaxis de declaración de clase:
// @interface NombreDeClase : NombreDeClasePadre <ProtocolosImplementados>
// {
//    type nombre; <= declaraciones de variables;
// }
// @property tipo nombre; <= declaración de propiedades
// -/+ (tipo) Declaración de método; <= Declaración de método
// @end
@interface MyClass : NSObject <MyProtocol> // NSObject es la clase de objeto 
                                           // base de  Objective-C.
{
    // Declaraciones de variables de instancia (puede existir en el archivo de 
    // interfaz o de implementación)    
    int count; // Acceso protegido por defecto. 
    @private id data; // Acceso privado (Más conveniente de declarar en el 
                      // archivo de implementación)
    NSString *name; 
}
// Notación conveneinte para acceso público de las variables para generar un 
// método setter
// Por defecto, el nombre del método setter 'set' seguido del nombre de 
// variable @property
@property int propInt; // Nombre del método 'setter' = 'setPropInt'
@property (copy) id copyId; // (copy) => Copia el objeto durante la asignación
// (readonly) => No se le puede asignar un valor fuera de @interface
@property (readonly) NSString *roString; // utiliza @synthesize en 
                                         // @implementation para crear un accesor
// Puedes personalizar el nombre del getter y del setter en lugar de utilizar
// el nombre por defecto "set".
@property (getter=lengthGet, setter=lengthSet:) int length;
 
// Métodos
+/- (return type)methodSignature:(Parameter Type *)parameterName;

// + Para métodos de clase:
+ (NSString *)classMethod;
+ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight;

// - Para métodos de instancia:
- (NSString *)instanceMethodWithParameter:(NSString *)string;
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;

// Métodos de constructor con argumentos
- (id)initWithDistance:(int)defaultDistance;
// Los nombres de los métodos de Objective-C son muy descriptivos. 
// Siempre nombra los métodos de acuerdo con sus argumentos

@end // Define el final de la interfaz


// Para acceder a las variables públicas desde el archivo de implementación, 
// @property genera un método setter automáticamente. El nombre del método 
// es 'set' seguido de un nombre de variable @property:
MyClass *myClass = [[MyClass alloc] init]; // Crea una instancia del objeto MyClass 
[myClass setCount:10]; 
NSLog(@"%d", [myClass count]); // imprime => 10
// O utilizando los métodos getter y setter personalizados en @interface:
[myClass lengthSet:32];
NSLog(@"%i", [myClass lengthGet]); // imprime => 32
// Por conveniencia, puedes utilizar la notación de punto para asignar y 
// acceder a las variables de una instancia de objeto.
myClass.count = 45;
NSLog(@"%i", myClass.count); // imprime => 45

// Llama a métodos de clase:
NSString *classMethodString = [MyClass classMethod];
MyClass *classFromName = [MyClass myClassFromName:@"Hello"];

// Llama a métodos de instancia:
MyClass *myClass = [[MyClass alloc] init]; // Crea una instancia de objeto Myclass
NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hello"];

// Selectors
// Una forma dinámica de representar métodos. Utilizados para llamar métodos 
// de una clase, pasar métodos a través de funciones para avisar a otras clases 
// para que lo llamen, y para guardar métodos como una variable.
// SEL es el tipo de dato. @selector() devuelve un selector del nombre de 
// método proveído methodAparameterAsString:andAParameterAsNumber: es un nombre 
// para un método en MyClass
SEL selectorVar = @selector(methodAParameterAsString:andAParameterAsNumber:); 
if ([myClass respondsToSelector:selectorVar]) { // Revisa si la clase contiene el método
    // Debe de poner todos los argumentos de método en un solo objeto para mandar una 
    // función performSelector.    
    NSArray *arguments = [NSArray arrayWithObjects:@"Hello", @4, nil];
    [myClass performSelector:selectorVar withObject:arguments]; // Calls the method
} else {
    // NSStringFromSelector() devuelve un NSString del nombre de método de un selector dado
    NSLog(@"MyClass does not have method: %@", NSStringFromSelector(selectedVar));
}

// Implementa los métodos de un archivo de implementación (MyClass.m):
@implementation MyClass {
    long distance; // Variable de instancia de acceso privado
    NSNumber height;
}

// Para acceder a una variable pública del archivo de interfaz, utiliza '_' seguido del
// nombre de la variable:
_count = 5; // Hace referencia a "int count" de la interfaz de MyClass
// Accede variables definidas en el archivo de implementación:
distance = 18; // Hace referencia a "long distance" de la implementación de MyClass
// Para utilizar una variable @property en el archivo de implementación, utiliza 
// @synthesize para crear una variable de acceso:
@synthesize roString = _roString; // _roString ahora está disponible en @implementation

// Lamado antes de llamar algún método o instanciar cualquier objeto
+ (void)initialize 
{
    if (self == [MyClass class]) {
        distance = 0;
    }
}

// Contraparte para inicializar un método. Llamado cuando el contador de referencias
// del objeto es cero
- (void)dealloc
{
    [height release]; // Si no utilizas ARC, asegúrate de liberar las variables de 
                      // objeto de las clases
    [super dealloc];  // y llama el método dealloc de la clase padre
}

// Los constructores son una manera de crear instancias de una clase
// Este es el constructor por defecto que es llamado cuando el objeto es inicializado.
- (id)init
{
    if ((self = [super init])) // 'super' es utilizado para acceder a los 
                               // métodos de la clase padre.
    {
        self.count = 1; // 'self' es utilizado para que el objeto se llame a sí mismo.
    }
    return self;
}
// Se pueden crear constructores que contiene argumentos
- (id)initWithDistance:(int)defaultDistance 
{
    distance = defaultDistance;
    return self;
}

+ (NSString *)classMethod
{
    return @"Some string";
}

+ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight 
{
    height = defaultHeight;
    return [[self alloc] init];
}

- (NSString *)instanceMethodWithParameter:(NSString *)string
{
    return @"New string";
}

- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number
{
    return @42;
}

// Objective-C no tiene declaraciones de métodos privados, pero pueden ser simulados.
// Para simular un método privado, crea un método en @implementation pero no en @interface.
- (NSNumber *)secretPrivateMethod {
    return @72;
}
[self secretPrivateMethod]; // Calls private method

// Métodos declarados dentro de MyProtocol
- (void)myProtocolMethod
{
    // statements
}

@end // Declara el final de la implementación

///////////////////////////////////////
// Categorías
///////////////////////////////////////
// Una categoría es un grupo de métodos diseñados para extender una clase. 
// Te permiten agregar nuevos métodos a una clase existente por propósitos 
// de organización. Éstos no deben de serconfundidos con subclases. 
// Las subclases existen para CAMBIAR la funcionalidad de un objeto mientras 
// que las categoríasle AGREGAN funcionalidad de un objeto.
// Las categorías te permiten:
// -- Agregar métodos a una clase existente por propósitos de oganización.
// -- Extender clases de objetos de Objective-C (ejemplo: NSString) para 
//    agregar tus propios métodos.
// -- Agregar la habilidad de crear métodos protegidos y privados para las clases.
// NOTA: No sobreescribas los métodos de las clases base en una categoría 
// aunque tengas la habilidad de poder hacerlo. Sobreescribir métodos puede 
// causar errores en la compilación después entre diferentes categorías y 
// puede arruinar el propósito de las categorías de solo AGREGAR funcionalidad. 
// Utiliza subclass para sobreescribir métodos.

// Aquí una clase base simple, Car.
@interface Car : NSObject

@property NSString *make;
@property NSString *color;

- (void)turnOn;
- (void)accelerate;

@end

// Y la implementación de la clase simple, Car
#import "Car.h"

@implementation Car

@synthesize make = _make;
@synthesize color = _color;

- (void)turnOn {
    NSLog(@"Car is on.");
}
- (void)accelerate {
    NSLog(@"Accelerating.");
}

@end

// Ahora, si quisieramos crear un objeto Truck (Camión), crearíamos una 
// subclase de Car (Carro) como si le cambiaramos de funcionalidad de Car 
// para que se comporte como un camión. Pero digamos que únicamente queremos 
// agregar funcionalidad al Car (Carro) existente. Un buen ejemplo sería 
// limpiar el carro. Así que crearíamos una cateog®iea para agregar los 
// métodos de limpieza: 
// Archivo @interface: Car+Clean.h (NombreBaseDeClase+NombreDeCategoria.h)
#import "Car.h" // Asegúrate de improtar la clase que deseas extener.

@interface Car (Clean) // El nombre de la categoría está dentro de (), 
                       // seguido del nombre de la clase base

- (void)washWindows; // Nombres de los nuevos métodos que le agregamos 
                     // a nuestro objeto Car
- (void)wax;

@end

// Archivo @implementation: Car+Clean.m (NombreBaseDeClase+NombreDeCategoria.m)
#import "Car+Clean.h" // Importa el archivo de @interface de la categoría Clean

@implementation Car (Clean)

- (void)washWindows {
    NSLog(@"Windows washed.");
}
- (void)wax {
    NSLog(@"Waxed.");
}

@end 

// Cualquier instancia del objeto Car tiene la habilidad de utilizar una 
// categoría. Todo lo que necesitan es importarlo: 
#import "Car+Clean.h" // Importa todas las diferentes categorías que 
                      // necesites utilizar
#import "Car.h" // También debes de importar la clase base para su 
                // funcionalidad original 

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        Car *mustang = [[Car alloc] init];
        mustang.color = @"Red";
        mustang.make = @"Ford";

        [mustang turnOn]; // Utiliza métodos de la clase base Car.
        [mustang washWindows]; // Utiliza métodos de la categoría Clean de Car.
    }
    return 0; 
}

// Objective-C no tiene declaraciones para métodos protegidos, pero los puedes
// simular. Crea una categoría conteniendo todos los métodos protegidos, 
// luego importa ÚNICAMENTE al archivo @implementation de una clase que 
// pertenece a la clase Car.
@interface Car (Protected) // Nombrando la categoría 'Protected' para 
                           // recordar que los métodos están protegidos
    
- (void)lockCar; // Los métodos enlistados aquí solo puedens ser creados 
                 // por objetos Car

@end
// Para utilizar los métodos protegidos, importa la categoría, 
// luego implementa sus métodos:
#import "Car+Protected.h" // Recuerda, importa únicamente el archivo 
                          // de @implementation

@implementation Car 

- (void)lockCar {
    NSLog(@"Car locked."); // Las instancias de Car no puede utilizar 
                           // lockCar porque no se encuentra en @interface
}

@end

///////////////////////////////////////
// Extensiones
///////////////////////////////////////
// Las Extensions te permiten sobreescribir atributos de propiedades de 
// acceso público y métodos de un @interface
// Archivo @interface: Shape.h
@interface Shape : NSObject 

@property (readonly) NSNumber *numOfSides;

- (int)getNumOfSides;

@end
// Puedes sobreescribir la variable numOfSides o el métodos getNumOfSlides 
// para modificarlos con una extensión:
// Archivo @implementation: Shape.m
#import "Shape.h"
// Las extensiones se encuentran en el mismo archivo que el archivo 
// de @implementation
@interface Shape () // () después del nombre de la clase base declara 
                    // una extensión

@property (copy) NSNumber *numOfSides; // Hacer numOfSlides copy en lugar
                                       // de readonly.
-(NSNumber)getNumOfSides; // Hacer que getNumOfSides devuelva un NSNumber 
                          // en lugar de un int.
-(void)privateMethod; // También puedes crear una nuevos métodos privados 
                      // dentro de las extensiones

@end
// @implementation principal:
@implementation Shape 

@synthesize numOfSides = _numOfSides;

-(NSNumber)getNumOfSides { // Todas las declaraciones dentro de extensions 
                           // deben de ser dentro de @implementation
    return _numOfSides;
}
-(void)privateMethod {
    NSLog(@"Private method created by extension. Shape instances cannot call me.");
}

@end

///////////////////////////////////////
// Protocolos
///////////////////////////////////////
// Un protocolo declara métodos que pueden ser implementados por cualquier otra
// clase. Los protocolos no son clases. Simplementen define una interfaz que 
// otros objetos deben de implementar.
// Archivo @protocol: "CarUtilities.h"
@protocol CarUtilities <NSObject> // <NSObject> => Nombre de otro protocolo 
                                  // que se incluye en éste
    @property BOOL engineOn; // La clase que lo adopta debe de utilizar 
                            // @synthesize para todas las @properties definidas 
    - (void)turnOnEngine; // y todos los métodos definidos
@end
// A continuación una clase ejemplo que implementa el protcolo
#import "CarUtilities.h" // Importar el archivo @protocol.

@interface Car : NSObject <CarUtilities> // El nombre del protocolo dentro de <>
    // No necesitas los nombres de @property o métodos aquí para CarUtilities.
    // Estos solo es requerido por @implementation.
- (void)turnOnEngineWithUtilities:(id <CarUtilities>)car; // También Puedes 
                                                          // utilizar protocolos 
                                                          // como datos.
@end
// El @implementation necesita que se implementen @properties y métodos 
// del protocolo.
@implementation Car : NSObject <CarUtilities>

@synthesize engineOn = _engineOn; // Crear una declaración @synthesize para el 
                                  // @property engineOn.

- (void)turnOnEngine { // Implementa turnOnEngine como quieras. Los 
                       // protocolos no definen
    _engineOn = YES;   // cómo implementas un método, con tal de que lo implementes.
}
// Puedes utilizar un protocolo como data mientras sepas quee métodos y variables 
// tiene implementado.
- (void)turnOnEngineWithCarUtilities:(id <CarUtilities>)objectOfSomeKind { 
    [objectOfSomeKind engineOn]; // Tienes acceso a las variables 
    [objectOfSomeKind turnOnEngine]; // y los métodos del objeto
    [objectOfSomeKind engineOn]; // Puede o no puede ser YES. La clase lo 
                                 // implementa como se quiera.
}

@end
// Las instancias de Car ahora tienen acceso al protocolo.
Car *carInstance = [[Car alloc] init];
[carInstance setEngineOn:NO];
[carInstance turnOnEngine];
if ([carInstance engineOn]) {
    NSLog(@"Car engine is on."); // imprime => "Car engine is on."
}
// Asegúrate de revisar si un objeto de tipo 'id' implementa un protocolo antes
// de llamar a sus métodos:
if ([myClass conformsToProtocol:@protocol(CarUtilities)]) {
    NSLog(@"This does not run as the MyClass class does not implement the CarUtilities protocol.");
} else if ([carInstance conformsToProtocol:@protocol(CarUtilities)]) {
    NSLog(@"This does run as the Car class implements the CarUtilities protocol.");
}
// Las categorías también pueden implementar protcolos: @interface Car 
// (CarCategory) <CarUtilities>
// Puedes implementar varios protocolos: 
// @interface Car : NSObject <CarUtilities, CarCleaning>
// NOTA: Si dos o más protocolos dependen entre sí, asegúrate de declararlos 
// de manera adelantada:
#import "Brother.h"

@protocol Brother; // Declaración adelantada. Sin ésto, el compilador 
                   // tira un error.

@protocol Sister <NSObject>

- (void)beNiceToBrother:(id <Brother>)brother;

@end

// Ver si el problema es que Sister depende de Brother, 
// y Brother dependa de Sister.
#import "Sister.h"

@protocol Sister; // Estas líneas detienen la recursión, resolviendo el problema.

@protocol Brother <NSObject>
 
- (void)beNiceToSister:(id <Sister>)sister;

@end


///////////////////////////////////////
// Bloques
///////////////////////////////////////
// Los bloques son declaraciones de código, tal como una función, pueden 
// ser utilizados como data.
// A continuación un bloque simple con un argumento entero que devuelve 
// un el argumento más 4.
int (^addUp)(int n); // Declarar una variable para almacenar el bloque.
void (^noParameterBlockVar)(void); // Ejemplo de una declaración de variable 
                                   // de bloque sin argumentos.
// Los bloques tienen acceso a variables del mismo ámbito. Pero las variables 
// son solo readonly y el valor pasado al bloque es el valor de la variable 
// cuando el bloque es creado.
int outsideVar = 17; // Si modificamos outsideVar después de declarar addUp,
                     // outsideVar AÚN es 17.
__block long mutableVar = 3; // __block hace que las variables se puedan 
                             // escribir en bloques.
addUp = ^(int n) { // Remueve (int n) para tener un bloque que no recibe 
                   // ningún parámetro
    NSLog(@"You may have as many lines in a block as you would like.");
    NSSet *blockSet; // También puedes declarar variables locales.
    mutableVar = 32; // Asignar un nuevo valor a la variable __block.
    return n + outsideVar; // Declaraciones de retorno son opcionales.
}
int addUp = add(10 + 16); // Llama al bloque de código con argumentos.
// Los bloques son usualmente utilizados como argumentos a funciones que 
// son llamados más adelante o para callbacks.
@implementation BlockExample : NSObject 
 
 - (void)runBlock:(void (^)(NSString))block {
    NSLog(@"Block argument returns nothing and takes in a NSString object.");
    block(@"Argument given to block to execute."); // Calling block.
 }

 @end


///////////////////////////////////////
// Manejo de memoria
///////////////////////////////////////
/* 
Para cada objeto utilizado en una aplicación, la memoria debe de ser alocada 
para ese objeto. Cuando la aplicación termina de utilizar ese objeto, la 
memoria debe de ser desalocada para asegurar la  eficiencia de la aplicación.
Objetive-C no utiliza garbage collection, y en lugar de eso utiliza conteos 
de referencias. Mientras haya al menos una referencia del objeto (también 
conocido como tener un objeto de adueñado), entonces el objeto estará 
disponible para su uso.

Cuando una instancia es dueña un objeto, su contador de referencia incrementa
por uno. Cuando el objeto es liberado, el contador de referencia decrementa uno. 
Cuando el conteo de referencia es cero, el objeto es removido de la memoria.

Con todas las interacciones de los objetos, sigue el patrón de:
(1) Crear e lobjeto, (2) utiliza el objeto, (3) libera el objeto de la memoria.
*/

MyClass *classVar = [MyClass alloc]; // 'alloc' asigna uno al conteo de 
                                     // referencias de classVar. Devuelve un 
                                     // puntero al objeto
[classVar release]; // Decrementa el conteo de referencias de classVar's
// 'retain'
// 'retain' adueña la instancia de objeto existente e incrementa el conteo de 
// referencia por uno. Devuelve un puntero al objeto.
MyClass *newVar = [classVar retain]; // Si classVar es liberado, el objeto 
                                     // aún se queda en memoria porque newVar
                                     // es el dueño.
[classVar autorelease]; // Remueve el adueñamiento de un objeto al final del 
                        // bloque @autoreleasepool. Devuelve un puntero al objeto.

// @property puede utilizar 'retain' y 'assign' también para pequeñas 
// definiciones convenientes
@property (retain) MyClass *instance; // Libera el valor viejo y retiene 
                                      // uno nuevo (referencia fuerte)
@property (assign) NSSet *set; // Apunta a un nuevo valor sin retener/liberar 
                               // una referencia vieja (débil)

// Conteo Automático de Referencias (ARC)
// Debido a que el manejo de memoria puede ser un dolor, en Xcode 4.2 y iOS 4 
// se introdujo el Conteo Automático de Referencias (ARC).
// ARC es una funcionalidad del compilador que agrega retain, release y
// autorealase automáticamente, así que al
// utilizar ARC, no se debe de utilizar retain, release o autorelease.
MyClass *arcMyClass = [[MyClass alloc] init]; 
// ... código utilizando arcMyClass
// Sin ARC, necesitarás llamar: [arcMyClass release] luego de terminar de 
// utilizar arcMyClass. Pero con ARC, no hay necesidad. Insertará 
// automáticamente la declaración de liberación.

// Mientras que para los atributos de @property 'assign' y 'retain', con ARC 
// utilizarás 'weak' y 'strong'
@property (weak) MyClass *weakVar; // 'weak' no adueña el objeto. El conteo de 
                                   // referencias de la instancia original
// es fijado a ceor, weakVar automáticamente recibe el valor de  nil para 
// evitar cualquier 'crashing'.
@property (strong) MyClass *strongVar; // 'strong' se adueña del objeto. 
                                       // Asegura que el objeto se quede en memoria.

// Para variables regulares (no variables de @property), utiliza lo siguiente:
__strong NSString *strongString; // Por defecto. La variables de retenida en 
                                 // memoria hasta que se salga del ámbito.
__weak NSSet *weakSet; // Referencia débil a un objeto existente. Cuando el 
                       // objeto existente es liberado, weakSet le es asginado
                       // un valor nil
__unsafe_unretained NSArray *unsafeArray; // Como __weak, pero unsafeArray no 
                                          // es asginado a nil cuando el objeto
                                          // existente es liberado.

```
## Lecturas sugeridas

[Wikipedia Objective-C](http://es.wikipedia.org/wiki/Objective-C)

[Programming with Objective-C. Libro PDF de Apple](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf)

[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started)
---
name: perl
category: language
language: perl
filename: learnperl-es.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
translators:
    - ["Francisco Gomez", "http://github.com/frncscgmz"]
    - ["Joaquín Ferrero", "http://github.com/joaquinferrero"]
lang: es-es
---

Perl 5 es un lenguaje de programación altamente capaz, rico en características, con más de 25 años de desarrollo.

Perl 5 corre en más de 100 plataformas, desde portátiles hasta ordenadores centrales, y es adecuado para realizar desde prototipos rápidos hasta desarrollar proyectos a gran escala.

```perl
# Comentarios de una sola línea con un carácter hash

#### Tipos de variables en Perl

# Las variables comienzan con el símbolo $
# Un nombre de variable válido empieza con una letra o un guión bajo,
# seguido por cualquier número de letras, números o guiones bajos

### Perl tiene tres tipos principales de variables: escalares, arreglos y hashes

## Escalares
# Un escalar representa un solo valor:
my $animal    = "camello";
my $respuesta = 42;

# Los valores escalares pueden ser cadenas de caracteres, números enteros o 
# de punto flotante; Perl automáticamente los convertirá como sea requerido

## Arreglos
# Un arreglo representa una lista de valores:
my @animales = ("camello","llama","buho"};
my @numeros  = (23, 42, 69);
my @mixto    = ("camello", 42, 1.23);

## Hashes
# Un hash representa un conjunto de pares llave/valor:
my %color_fruta = ("manzana","rojo","banana","amarillo");

# Puede usar un espacio en blanco y el operador "=>" para asignarlos más fácilmente
my %color_fruta = (
   manzana => "rojo",
   banana  => "amarillo",
);

# Los escalares, arreglos y hashes están más documentados en perldata (perldoc perldata)

# Los tipos de datos más complejos se pueden construir utilizando 
# referencias, las cuales le permiten construir listas y hashes dentro
# de listas y hashes

#### Estructuras condicionales y de ciclos

# Perl tiene la mayoría de las estructuras condicionales y de ciclos más comunes
if ( $var ) {
    ...;
} elsif ( $var eq 'bar' ) { 
    ...;
} else {
    ...;
}

unless ( condicion ) {
    ...;
}

# Esto se ofrece como una versión más fácil de leer que "if (!condición)"

# La postcondición al modo Perl:
print "Yow!" if $zippy;
print "No tenemos bananas" unless $bananas;

# while
while ( condicion ) {
    ...;
}

# for y foreach
for ($i = 0; $i <= $max; $i++) {
    ...;
}

for $i (0 .. $max) {
    ...;
}

foreach (@array) {
    print "Este elemento es $_\n";
}


#### Expresiones regulares

# El soporte de expresiones regulares en Perl es muy amplio y profundo, y 
# está sujeto a una extensa documentación en perlrequick, perlretut, entre otros.
# Sin embargo, resumiendo:

# Coincidencia simple
if (/foo/)       { ... }  # verdadero si $_ contiene "foo"
if ($a =~ /foo/) { ... }  # verdadero si $a contiene "foo"

# Substitución simple
$a =~ s/foo/bar/;         # remplaza "foo" con "bar" en $a
$a =~ s/foo/bar/g;        # remplaza TODAS LAS INSTANCIAS de "foo" con "bar" en $a


#### Archivos y E/S

# Puede abrir un archivo para obtener datos o escribirlos utilizando la 
# función "open()"

open(my $entrada, "<"  "entrada.txt") or die "No es posible abrir entrada.txt: $!";
open(my $salida,  ">", "salida.txt")  or die "No es posible abrir salida.txt: $!";
open(my $log,     ">>", "mi.log")     or die "No es posible abrir mi.log: $!";

# Es posible leer desde un gestor de archivo abierto utilizando el operador "<>".
# En contexto escalar, leer una sola línea desde el gestor de archivo, y
# en contexto de lista, leer el archivo completo en donde asigna 
# cada línea a un elemento de la lista

my $linea  = <$entrada>;
my @lineas = <$entrada>;

#### Escribiendo subrutinas

# Escribir subrutinas es fácil:
sub logger {
   my $mensajelog = shift;
   open my $archivolog, ">>", "mi.log" or die "No es posible abrir mi.log: $!";
   print $archivolog $mensajelog;
}

# Ahora podemos utilizar la subrutina al igual que cualquier otra función incorporada:
logger("Tenemos una subrutina logger!");

```

#### Utilizando módulos Perl

Los módulos en Perl proveen de una gama de funciones que le pueden ayudar a evitar reinventar la rueda. Éstas se pueden  descargar desde CPAN ( http://www.cpan.org/ ). Algunos de los módulos más populares ya están incluidos con la misma distribución de Perl.

perlfaq contiene preguntas y respuestas relacionadas con muchas tareas comunes, y algunas veces provee sugerencias sobre buenos módulos de CPAN que puede usar.

#### Material de Lectura

     - [perl-tutorial](http://perl-tutorial.org/)
     - [Learn Perl](http://www.perl.org/learn.html)
     - [perldoc](http://perldoc.perl.org/)
     - y en su propio perl: `perldoc perlintro`
---
category: tool
tool: composer
contributors:
    - ["Brett Taylor", "https://github.com/glutnix"]
translators:
    - ["Ivan Alburquerque", "https://github.com/AlburIvan"]
lang: es-es
filename: LearnComposer-es.sh
---

[Composer](https://getcomposer.org/) es una herramienta para manejar las dependencias en PHP. Nos permite declarar las librerías de las cuales el proyecto depende y las maneja automáticamente (instala/actualiza) por ti.

# Instalando

```sh
# Instala el binario 'composer.phar' en el directorio actual
curl -sS https://getcomposer.org/installer | php
# Si utiliza este método, tendrá que invocar a 'composer' de esta manera:
php composer.phar about

# Instala el binario en ~/bin/composer
# Nota: asegúrese de que ~/bin está en la variable de entorno PATH del shell
curl -sS https://getcomposer.org/installer | php -- --install-dir=~/bin --filename=composer
```

Los usuarios de Windows deben seguir las [instrucciones de instalación de Windows (EN)](https://getcomposer.org/doc/00-intro.md#installation-windows)

## Confirmación de la instalación

```sh
# Comprobar la versión y lista las opciones
composer

# Obtener más ayuda para las opciones
composer help require

# Comprueba si composer es capaz hacer las cosas que necesita y si está actualizado
composer diagnose
composer diag # versión corta

# Actualiza el binario composer a la última versión
composer self-update
composer self # versión corta
```

# Uso

Composer almacena sus dependencias del proyecto en `composer.json`. Usted puede editar este archivo, pero lo mejor es dejar que composer lo gestione por usted.

```sh
# Crear un nuevo proyecto en la carpeta actual
composer init
# Este corre un cuestionario interactivo que le pide detalles sobre su proyecto. 
# Dejándolos en blanco está bien a menos que usted está haciendo otros proyectos que dependen de éste.

# Si un archivo 'composer.json' ya existe, descargar las dependencias
composer install

# Para descargar solo las dependencias de producción, es decir, excluyendo las 
# dependencias de desarrollo
composer install --no-dev

# Añadir una dependencia de producción a este proyecto
composer require guzzlehttp/guzzle
# Composer buscará cuál es la última versión de guzzlehttp/Guzzle, lo descarga,
# y finalmente añade la nueva dependencia al campo requerido en 'composer.json'.

composer require guzzlehttp/guzzle:6.0.*
# Composer descargará la versión más reciente que coincida con el patrón 
# (ej 6.0.2) y añade la dependencia al campo requerido en 'composer.json'.

composer require --dev phpunit/phpunit:~4.5.0
# Se requerirá como una dependencia de desarrollo. Se usará la última 
# versión >= 4.5.0 y < 4.6.0

composer require-dev phpunit/phpunit:^4.5.0
# Se requerirá como una dependencia de desarrollo. Se usará la última versión >= 4.5.0 y <  5.0

# Para obtener más información sobre las coincidencias de versiones de Composer, 
ver [La Documentación de Composer\'s Sobre Versiones (EN)](https://getcomposer.org/doc/articles/versions.md)

# Para ver qué opciones están disponibles para instalar y los paquetes instalados actualmente  
composer show

# Para ver qué paquetes están instalados actualmente
composer show --installed

# Para encontrar un paquete con 'mailgun' en su nombre o descripción
composer search mailgun
```

[Packagist.org (EN)](https://packagist.org/) es el repositorio principal de paquetes de Composer. Busca allí para paquetes existentes de terceros.

## `composer.json` vs `composer.lock`

El archivo `composer.json` almacena las preferencias de versión flotantes de su proyecto para cada dependencia, junto con otra información.

El archivo `composer.lock` almacena exactamente cuál es la versión que ha descargado para cada dependencia. Nunca editar este archivo.

Si se incluye el archivo `composer.lock` en su repositorio git, todos los desarrolladores instalarán la versión utilizada actualmente de la dependencia. Incluso cuando se libera una nueva versión de una dependencia, Composer continuará para descargar la versión grabada en el archivo '.lock'.

```sh
# Si desea actualizar todas las dependencias a su versión más reciente aún que coincidan con sus preferencias versión
composer update

# Si desea la nueva versión de una dependencia particular:
composer update phpunit/phpunit

# Si desea migrar la preferencia de un paquete a una versión más reciente, puede que tenga que quitar primero el paquete de más antiguo y sus dependencias.
composer remove --dev phpunit/phpunit
composer require --dev phpunit/phpunit:^5.0

```

## Autocargador

Composer crea una clase de cargador automático que puede requerir su aplicación. Se puede hacer instancias de clases a través de su espacio de nombres.

```php
require __DIR__ . '/vendor/autoload.php';

$mailgun = new Mailgun\Mailgun("key");
```

### PSR-4 Autocargador

Usted puede añadir sus propios espacios de nombres para el cargador automático.

En `composer.json`, añadir el campo 'autoload':

```json
{
  "autoload": {
    "psr-4": {"Acme\\": "src/"}
  }
}
```
Esto le indicará al cargador automático que busque cualquier cosa en el espacio de nombres `\Acme\` dentro de la carpeta src`.

También puedes usar [usar PSR-0, un mapa de clase o simplemente una lista de archivos para incluir (EN)](https://getcomposer.org/doc/04-schema.md#autoload). También está el campo `autoload-dev` para espacios de nombres de sólo desarrollo.

Al añadir o modificar la clave de carga automática, tendrá que reconstruir el cargador automático:

```sh
composer dump-autoload
composer dump # shorthand

# Optimiza los paquetes PSR0 y PSR4 a ser cargados con classmaps también. Es lento para correr, pero mejora el rendimiento en producción.
composer dump-autoload --optimize --no-dev
```

# El Cache de Composer

```sh
# Composer retendrá los paquetes descargados para su uso en el futuro. Puede removerlos con:
composer clear-cache
```

# Solución de problemas

```sh
composer diagnose
composer self-update
composer clear-cache
```

## Temas (todavía) no cubiertos en este tutorial

* Crear y distribuir tus propios paquetes en Packagist.org o en otra parte
* Pre- y post- script: ejecutar tareas cuando ciertos eventos tienen lugar composer

### Referencias

* [Composer - Dependency Manager for PHP (EN)](https://getcomposer.org/)
* [Packagist.org (EN)](https://packagist.org/)---
language: PHP
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
translators:
    - ["Mario Pérez", "https://github.com/MarioPerezEsteso"]
lang: es-es
filename: learnphp-es.php
---

Este documento explica el funcionamiento de PHP 5+.

```php
<?php // El código PHP debe estar dentro de etiquetas <?php

// Si tu fichero php solo contiene código php, es una buena práctica
// omitir la etiqueta de cierre php para prevenir salidas acidentales.

// Dos barras comienzan un comentario de una línea.

# También lo hará una almohadilla, pero // es más común

/*
     Escribir texto entre una barra-asterisco y asterisco-barra
     crea un comentario multilínea.
*/

// Utiliza "echo" o "print" para imprimir por pantalla
print('Hola '); // Imprime "Hola " sin salto de línea

// () son opcionales para print y echo
echo "Mundo\n"; // Imprime "Mundo" con un salto de línea
// (todas las sentencias deben finalizar con un punto y coma)

// Cualquier cosa fuera de las etiquetas <?php se imprime automáticamente
?>
¡Hola Mundo de nuevo!
<?php


/************************************
 * Tipos y variables
 */

// Las variables comienzan con el símbolo $.
// Una variable válida comienza con una letra o guión bajo,
// seguida de cualquier cantidad de letras, números o guiones bajos.

// Las variables booleanas no distinguen entre mayúsculas o minúsculas
$boolean = true;  // o TRUE o True
$boolean = false; // o FALSE o False

// Enteros
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (un 0 al comienzo declara un número octal)
$int4 = 0x0F; // => 15 (un 0x al comienzo declara un hexadecimal)

// Floats (también conocidos como doubles)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// Eliminar variable
unset($int1);

// Operaciones aritméticas
$suma        = 1 + 1; // 2
$diferencia  = 2 - 1; // 1
$producto    = 2 * 2; // 4
$cociente    = 2 / 1; // 2

// Operaciones aritméticas de escritura rápida
$numero = 0;
$numero += 1;      // Incrementa $numero en 1
echo $numero++;    // Imprime 1 (incremento después la evaluación)
echo ++$numero;    // Imprime 3 (incremento antes de la evaluación)
$numero /= $float; // Divide y asigna el cociente a $numero

// Las cadenas de caracteres deben declararse entre comillas simples
$sgl_quotes = '$String'; // => '$String'

// Evita utilizar comillas dobles excepto para embeber otras variables
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// Los caracteres especiales solo son válidos entre comillas dobles
$escaped   = "Esto contiene \t un caracter tabulador.";
$unescaped = 'Esto solo contiene una barra y una t: \t';

// Rodea una variable entre corchetes si es necesario
$dinero = "Tengo $${numero} en el banco.";

// Desde PHP 5.3, los nowdocs pueden ser utilizados para multilíneas no interpoladas
$nowdoc = <<<'END'
Multi line
string
END;

// Heredocs interpola cadenas de caracteres
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// La concatenación de cadenas de caracteres se realiza con .
echo 'Esta cadena de caracteres ' . 'está concatenada';

// Las cadenas de caracteres pueden ser pasadas como parámetros en un echo
echo 'Multiples', 'Parametros', 'Validos';  // Devuelve 'MultiplesParametrosValidos'


/********************************
 * Constantes
 */

// Una constante se define utilizando define()
// y nunca puede ser cambiada en tiempo de ejecución

// un nombre válido para una constante debe comenzar con una letra o guión bajo,
// seguido por cualquier número de letras, números o guiones bajos.
define("FOO",     "algo");

// el acceso a una constante se puede realizar llamando a la variable elegida sin un símbolo de $
echo FOO; // Devuelve 'algo'
echo 'Esto imprime '.FOO;  // Devuelve 'Esto imprime algo'



/********************************
 * Arrays
 */

// Todos los arrays en PHP son asociativos (hashmaps),

// Los arrays asociativos son conocidos como hashmaps en algunos lenguajes.

// Funciona con todas las versiones de php
$asociativo = array('Uno' => 1, 'Dos' => 2, 'Tres' => 3);

// PHP 5.4 introdujo una nueva sintaxis
$asociativo = ['Uno' => 1, 'Dos' => 2, 'Tres' => 3];

echo $asociativo['Uno']; // imprime 1

// Lista literales implícitamente asignados con claves enteras
$array = ['Uno', 'Dos', 'Tres'];
echo $array[0]; // => "Uno"

// Añadir un elemento al final de un array
$array[] = 'Cuatro';
// o
array_push($array, 'Cinco');

// Eliminar un elemento de un array
unset($array[3]);

/********************************
 * Salidas por pantalla
 */

echo('¡Hola Mundo!');
// Imprime ¡Hola Mundo! en stdout.
// Stdout es la página web si se está ejecutando en un navegador.

print('!Hola Mundo!'); // Es lo mismo que echo

// No es necesario el paréntesis en echo y print
echo '¡Hola Mundo!';
print '¡Hola Mundo!';

$parrafo = 'parrafo';

echo 100;      // Haz echo de escalares directamente
echo $parrafo; // o de variables

// Si las etiquetas cortas estás configuradas y tu versión de PHP es
// la 5.4.0 o superior, puede utilizar la sintaxis abreviada de echo
?>
<p><?= $parrafo?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $x ahora contiene el mismo valor que $y
$z = &$y;
// $z contiene ahora una referencia a $y. Un cambio en el valor de
// $z cambiará también el valor de $y, y viceversa.
// $x sin embargo, tendrá el valor original de $y

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0

// Dump muestra el tipo y valor de una variable en stdout
var_dump($z); // imprime int(0)

// Para mostrar el valor de una variable en un formato legible para humanos
print_r($array); // imprime: Array ( [0] => Uno [1] => Dos [2] => Tres )

/********************************
 * Lógica
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// assert lanza una advertencia si su argumento no es verdadero

// Estas comparaciones siempre serán verdaderas, incluso si los tipos no son los mismos.
assert($a == $b); // igualdad
assert($c != $a); // desigualdad
assert($c <> $a); // desigualdad alternativa
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// Los siguiente solo será verdadero si los valores coinciden y son del mismo tipo.
assert($c === $d);
assert($a !== $d);
assert(1 === '1');
assert(1 !== '1');

// Operador 'Spaceship' (desde PHP 7)
// Devuelve 0 si ambos valores son iguales
// Devuelve 1 si el valor de la izquierda es mayor
// Devuelve -1 si el valor de la derecha es mayor

$a = 100;
$b = 1000;

echo $a <=> $a; //  0 porque son iguales
echo $a <=> $b; // -1 porque $a < $b
echo $b <=> $a; //  1 porque $b > $a

// Las variables pueden ser convertidas entre tipos, dependiendo de su uso.

$entero = 1;
echo $entero + $entero; // => 2

$string = '1';
echo $string + $string; // => 2 (los strings son convertidos a enteros)

$string = 'uno';
echo $string + $string; // => 0
// Muestra un 0 porque el operador + no puede convertir la cadena de caracteres 'uno' a un número

// La conversión de tipos puede ser utilizada para tratar a una variable como otro tipo

$boolean = (boolean) 1; // => true

$cero = 0;
$boolean = (boolean) $cero; // => false

// También hay funciones dedicadas a la conversión de tipos
$entero = 5;
$string = strval($entero);

$var = null; // Valor nulo


/********************************
 * Estructuras de control
 */

if (true) {
    print 'He sido imprimido';
}

if (false) {
    print 'Yo no';
} else {
    print 'He sido imprimido';
}

if (false) {
    print 'No se imprime';
} elseif(true) {
    print 'Sí se imprime';
}

// operador ternario
print (false ? 'No se imprime' : 'Sí se imprime');

// atajo para el operador ternario desde PHP 5.3
// equivalente de "$x ? $x : 'Sí'""
$x = false;
print($x ?: 'Sí');

// operador 'no definido' desde php 7
$a = null;
$b = 'Imprime';
echo $a ?? 'a no está definido'; // imprime 'a no está definido'
echo $b ?? 'b no está definido'; // imprime 'Imprime'


$x = 0;
if ($x === '0') {
    print 'No imprime';
} elseif($x == '1') {
    print 'No imprime';
} else {
    print 'Imprime';
}



// Esta sintaxis alternativa se utiliza para plantillas:
?>

<?php if ($x): ?>
Esto se muestra si la evaluación es verdadera.
<?php else: ?>
En otro caso, se muestra esto.
<?php endif; ?>

<?php

// Utiliza el switch para tener algo más de lógica.
switch ($x) {
    case '0':
        print 'Switch does type coercion';
        break; // Debes incluir un break para no seguir con los casos 'Dos' y 'Tres'
    case 'Dos':
    case 'Tres':
        // Hacer algo si la variables es 'Dos' o 'Tres'
        break;
    default:
        // Hacer algo por defecto
}

// Los bucles While, do...while y for te serán familiares
$i = 0;
while ($i < 5) {
    echo $i++;
}; // Imprime "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // Imprime "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // Imprime "0123456789"

echo "\n";

$ruedas = ['bicicleta' => 2, 'coche' => 4];

// Los bucles foreach pueden iterar por arrays
foreach ($ruedas as $numero_ruedas) {
    echo $numero_ruedas;
} // Imprime "24"

echo "\n";

// También se puede iterar sobre las claves, así como sobre los valores
foreach ($ruedas as $vehiculo => $numero_ruedas) {
    echo "Un $vehiculo tiene $numero_ruedas ruedas";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // Sale fuera del bucle while
    }
    echo $i++;
} // Imprime "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // Se salta esta iteración del bucle
    }
    echo $i;
} // Imprime "0124"


/********************************
 * Funciones
 */

// Define una función con "function":
function mi_funcion () {
    return 'Hola';
}

echo mi_funcion(); // => "Hola"

// Un nombre válido de función comienza con una letra o guión bajo, seguido de cualquier
// número de letras, números o guiones bajos.

function anadir ($x, $y = 1) { // $y es opcional y por defecto es 1
    $resultado = $x + $y;
    return $resultado;
}

echo anadir(4); // => 5
echo anadir(4, 2); // => 6

// $resultado no es accesible fuera de la función
// print $resultado; // Devuelve una advertencia.

// Desde PHP 5.3 se pueden declarar funciones anónimas
$inc = function ($x) {
    return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
    echo "$x - $y - $z";
}

// Las funciones pueden devolver funciones
function bar ($x, $y) {
    // Utiliza 'use' para meter variables de fuera de la función
    return function ($z) use ($x, $y) {
        foo($x, $y, $z);
    };
}

$bar = bar('A', 'B');
$bar('C'); // Imprime "A - B - C"

// Puedes llamar a funciones utilizando cadenas de caracteres
$nombre_funcion = 'add';
echo $nombre_funcion(1, 2); // => 3
// Es útil para determinarl qué función ejecutar.
// O, utiliza call_user_func(callable $callback [, $parameter [, ... ]]);


// Puedes obtener todos los parámetros pasados a una función
function parametros() {
    $numero_argumentos = func_num_args();
    if ($numero_argumentos > 0) {
        echo func_get_arg(0) . ' | ';
    }
    $args_array = func_get_args();
    foreach ($args_array as $key => $arg) {
        echo $key . ' - ' . $arg . ' | ';
    }
}

parametros('Hola', 'Mundo'); // Hola | 0 - Hola | 1 - Mundo |

// Desde PHP 5.6 se puede obtener un número variable de argumentos
function variable($palabra, ...$lista) {
	echo $palabra . " || ";
	foreach ($lista as $item) {
		echo $item . ' | ';
	}
}

variable("Separa", "Hola", "Mundo") // Separa || Hola | Mundo |

/********************************
 * Includes
 */

<?php
// Los ficheros PHP incluidos deben comenzar también con la etiqueta de <?php

include 'mi-fichero.php';
// El código de mi-fichero.php ya está disponible en el entorno actual.
// Si el fichero no puede ser incluido (por ejemplo porque no se ha encontrado),
// se muestra una advertencia.

include_once 'mi-fichero.php';
// Si el código del fichero mi-fichero.php ya ha sido incluido, ya no se
// incluirá de nuevo. Este previene errores por múltiples declaraciones.

require 'mi-fichero.php';
require_once 'mi-fichero.php';
// Es lo mismo que el include(), pero require() causará un error fatal si el archivo
// no ha podido ser incluido.

// Contenido de mi-include.php:
<?php

return 'Cualquier cosa.';
// acabar archivo

// Los include y require también pueden devolver un valor.
$valor = include 'mi-include.php';

// Los archivos son incluidos en función de la ruta data o, si ninguna ruta es
// especificada se utilizará la directiva de configuración de include_path. Si el
// fichero no se encuentra en el include_path, include comprobará la ruta del código
// que lo llama antes de fallar.
/* */

/********************************
 * Clases
 */

// Las clases son definidas con la palabra clave class

class MiClase
{
    const MI_CONSTANTE      = 'valor'; // Una constante

    static $staticVar   = 'static';

    // Las variables estáticas y su visibilidad
    public static $publicStaticVar = 'publicStatic';
    // Accesible solo dentro de su clase
    private static $privateStaticVar = 'privateStatic';
    // Accesible desde la clase y las subclases
    protected static $protectedStaticVar = 'protectedStatic';

    // Las propiedades deben declarar su visibilidad
    public $propiedad    = 'public';
    public $instanceProp;
    protected $prot = 'protected'; // Accesible desde la clase y las subclases
    private $priv   = 'private';   // Accesible solo desde la clase

    // Crear un constructor con __construct
    public function __construct($instanceProp) {
        // Accede a las variables de la instancia con $this
        $this->instanceProp = $instanceProp;
    }

    // Los métodos son declarados como funciones dentro de una clase
    public function miMetodo()
    {
        print 'MiClase';
    }

    // la palabra clave final hará una función no sobreescribible
    final function noMePuedesSobreEscribir()
    {
    }

/*
 * Declarar propiedades de clase o métodos como estáticos los hace accesibles sin
 * necesidad de instanciar la clase. Una propiedad declarada como estática no
 * puede ser accedida mediante una instancia de la clase, pero sí mediante un
 * método estático.
 */

    public static function miMetodoEstatico()
    {
        print 'Soy estático';
    }
}

// Las constantes de una clase siempre pueden ser accedidas estáticamente
echo MiClase::MI_CONSTANTE;    // Muestra 'valor';

echo MiClase::$staticVar;  // Muestra 'static';
MiClase::miMetodoEstatico(); // Muestra 'Soy estático';

// Instancia una clase usando new
$mi_clase = new MiClase('Una instancia');
// Los paréntesis son opcionales si no se pasa ningún argumento.

// Accede a los miembros de una clase utilizando ->
echo $mi_clase->propiedad;     // => "public"
echo $mi_clase->instanceProp; // => "Una instancia"
$mi_clase->miMetodo();        // => "MiClase"


// Extender clases utilizando "extends"
class MiOtraClase extends MiClase
{
    function imprimePropiedadProtegida()
    {
        echo $this->prot;
    }

    // Sobreescribe un método
    function miMetodo()
    {
        parent::miMetodo();
        print ' > MiOtraClase';
    }
}

$mi_otra_clase = new MiOtraClase('Propiedad de instancia');
$mi_otra_clase->imprimePropiedadProtegida(); // => Imprime "protected"
$mi_otra_clase->miMetodo();               // Imprime "MiClase > MiOtraClase"

final class NoMePuedesExtender
{
}

// Puedes utilizar "métodos mágicos" para crear los getters y setters
class MiClaseMapeada
{
    private $propiedad;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MiClaseMapeada();
echo $x->propiedad; // Utilizará el método __get()
$x->propiedad = 'Algo'; // Utilizará el método __set()

// Las clases pueden ser abstractas (utilizando la palabra clave abstract) o
// implementando interfaces (utilizando la palabra clave implements).
// Una interfaz puede ser declarada con la palabra clave interface.

interface InterfazUno
{
    public function hazAlgo();
}

interface InterfazDos
{
    public function hazOtraCosa();
}

// las interfaces pueden ser extendidas
interface InterfazTres extends InterfazDos
{
    public function hazCualquierOtraCosa();
}

abstract class MiClaseAbstracta implements InterfazUno
{
    public $x = 'hazAlgo';
}

class MiOtraClase extends MiClaseAbstracta implements InterfazDos
{
    public function hazAlgo()
    {
        echo $x;
    }

    public function hazOtraCosa()
    {
        echo 'hazOtraCosa';
    }
}


// Las clases pueden implementar más de una interfaz
class CualquierOtraClase implements InterfazUno, InterfazDos
{
    public function hazAlgo()
    {
        echo 'hazAlgo';
    }

    public function hazOtraCosa()
    {
        echo 'hazOtraCosa';
    }
}


/********************************
 * Traits
 */

// Los traits están disponibles desde PHP 5.4.0 y son declarados utilizando "trait"

trait MiTrait
{
    public function miMetodoTrait()
    {
        print 'Tengo trait';
    }
}

class MiClaseTrait
{
    use MiTrait;
}

$cls = new MiClaseTrait();
$cls->miMetodoTrait(); // Imprime "Tengo trait"


/********************************
 * Namespaces
 */

// Esta sección está separada porque una declaración de namespace debe
// ser la primera sentencia en un archivo. Vamos a suponer que no es el caso

<?php

// Por defecto, las clases existen en el namespace global y pueden ser llamadas
// explícitamente con una contrabarra.

$cls = new \MiClase();



// Estableder el namespace para un archivo
namespace Mi\Namespace;

class MiClase
{
}

// (de otro archivo)
$cls = new Mi\Namespace\MiClase;

// O de otro namespace.
namespace Mi\Otro\Namespace;

use Mi\Namespace\MiClase;

$cls = new MiClase();

// O se puede asignar un ales al namespace

namespace Mi\Otro\Namespace;

use Mi\Namespace as OtroNamespace;

$cls = new OtroNamespace\MiClase();


/**********************
* Late Static Binding
*
*/

class ClasePadre {
    public static function quien() {
        echo "Soy una " . __CLASS__ . "\n";
    }
    public static function test() {
        // Auto referencia a la clase en la que el método está definido
        self::quien();
        // Referencia estáticamente a la clase donde el método ha sido llamado
        static::quien();
    }
}

ClasePadre::test();
/*
Soy una ClasePadre
Soy una ClasePadre
*/

class ClaseHija extends ClasePadre {
    public static function quien() {
        echo "Pero soy una " . __CLASS__ . "\n";
    }
}

ClaseHija::test();
/*
Soy una ClasePadre
Pero soy una ClaseHija
*/


/**********************
*  Manejo de errores
*  
*/

// Una simple gestión de errores puede ser realizada con un bloque try catch

try {
    // Haz algo
} catch (Exception $e) {
    // Maneja la excepción
}

// Cuando se utilicen bloques try catch en un entorno con namespaces hay que
// usar lo siguiente

try {
    // Haz algo
} catch (\Exception $e) {
    // Maneja la excepción
}

// Excepciones personalizadas

class MiExcepcion extends Exception {}

try {

    $condicion = true;

    if ($condicion) {
        throw new MiExcepcion('Ha pasado algo');
    }

} catch (MiExcepcion $e) {
    // Manejar la excepción
}

```

## Más información

Visita la [documentación oficial de PHP](http://www.php.net/manual/) para más referencias
y apoyo de la comunidad.

Si estás interesado en buenas prácticas, visita
[PHP The Right Way](http://www.phptherightway.com/).

Si vienes de un lenguaje con una buena gestión de paquetes, visita
[Composer](http://getcomposer.org/).

Para estándares comunes, visita el PHP Framework Interoperability Group
[PSR standards](https://github.com/php-fig/fig-standards).
---
category: tool
tool: powershell
contributors:
    - ["Wouter Van Schandevijl", "https://github.com/laoujin"]
translators:
    - ["Alexander Salamanca", "https://github.com/alexitosrv"]
filename: LearnPowershell-es.ps1
lang: es-es
---

PowerShell es el lenguaje de automatización y gestión de configuraciones de Windows hecho por Microsoft basado en .NET Framework. Desde Windows 7 en adelante, esos sistemas operativos incluyen un intérprete de PowerShell.
Casi todos los ejemplos a continuación pueden ser parte de un script o ejecutados directamente en la consola de PowerShell.

Una diferencia clave con respecto a Bash es que en PowerShell casi todo son manipulaciones de objetos en vez de análisis sobre flujos de texto plano.

[Leer más acá.](https://technet.microsoft.com/en-us/library/bb978526.aspx) (EN)

Si no está seguro sobre el ambiente de ejecución en su sistema:

```powershell
Get-ExecutionPolicy -List
Set-ExecutionPolicy AllSigned
# Otras opciones de políticas de ejecución son:
# - Restricted: Los scripts no correrán.
# - RemoteSigned: Los scripts que se hayan descargado sólo correrán si han sido firmados por un editor de confianza. 
# - AllSigned: Los scripts requieren ser firmados por un editor de confianza.
# - Unrestricted: Ejecuta cualquier script.
help about_Execution_Policies # para obtener más ayuda sobre políticas de ejecución.

# Versión instalada de PowerShell:
$PSVersionTable
```

Para obtener ayuda:

```
# Si necesita encontrar algún comando
Get-Command about_* # tiene por abreviación (o alias): gcm
Get-Command -Verb Add # lista todos los comandos que tienen por verbo 'Add'
Get-Alias ps
Get-Alias -Definition Get-Process

Get-Help ps | less # alias: help
ps | Get-Member # alias: gm

Show-Command Get-EventLog # Muestra un formulario para llenar los parámetros del comando Get-EventLog

Update-Help # Actualiza la ayuda (debe ser ejecutado en una consola elevada como admin)
```

Acá inicia el tutorial:

```
# Como ya lo notó, los comentarios empiezan con #

# Ejemplo de un simple hola mundo:
echo Hola mundo!
# echo es el alias del comando Write-Output (a los comandos también se les dice cmdlets)
# La mayoría de los cmdlets y funciones siguen la convención de llamarse de la forma: Verbo-Sustantivo

# Cada comando inicia en una nueva línea, o después de un punto y coma:
echo 'Esta es la primer línea'; echo 'Esta es la segunda'

# La declaración de una variable se ve así:
$unaCadena ="Algún texto"
# O así:
$unNumero = 5 -as [double] 
$unaLista = 1,2,3,4,5
$unaCadena = $unaLista -join '--' # también existe el parámetro -split 
$unaTablaHash = @{nom1='val1'; nom2='val2'}

# Uso de variables:
echo $unaCadena
echo "Interpolación: $unaCadena"
echo "`$unaCadena tiene longitud de $($unaCadena.Length)"  
echo '$unaCadena'
echo @"
Esta es una Here-String
$otraVariable
"@
# Note que una ' (comilla simple) no expande las variables!
# Las Here-Strings también funcionan con comilla simple

# Variables Automáticas:
# Hay algunas variables previamente definidas en el ambiente que le pueden servir, tales como
echo "Booleanos: $TRUE y $FALSE"
echo "Valor vacío: $NULL"
echo "Valor de retorno del último programa: $?"
echo "Código de salida del último programa en Windows: $LastExitCode"
echo "El último token en la última línea de la sesión activa: $$"
echo "El primer token: $^"
echo "PID del script: $PID"
echo "Ruta completa del directorio dónde está el script actual: $PSScriptRoot"
echo 'Ruta completa de script actual: ' + $MyInvocation.MyCommand.Path
echo "Ruta completa de directorio actual: $Pwd"
echo "Argumentos pasados a la invocación de una función, script o bloque de código: $PSBoundParameters"
echo "Argumentos no predefinidos: $($Args -join ', ')."
# Para saber más sobre variables automáticas: `help about_Automatic_Variables`

# Para enlazar otro archivo (operador punto)
. .\otroNombreDeScript.ps1


### Control de Flujo
# Tenemos la estructura de if como es usual:
if ($Edad -is [string]) {
	echo 'Pero... si $Edad no puede ser una cadena de texto!'
} elseif ($Edad -lt 12 -and $Edad -gt 0) {
	echo 'Niño (Menor de 12. Mayor que 0)'
} else {
	echo 'Adulto'
}

# Sentencias switch de PS son más poderosas comparadas con otros lenguajes
$val = "20"
switch($val) {
  { $_ -eq 42 }           { "La respuesta es 42"; break }
  '20'                    { "Exactamente 20"; break }
  { $_ -like 's*' }       { "No distingue entre mayúsculas/minúsculas"; break }
  { $_ -clike 's*'}       { "clike, ceq, cne para ser diferenciar el caso entre mayúsculas/minúsculas"; break }
  { $_ -notmatch '^.*$'}  { "Emparejamiento de expresiones regulares. cnotmatch, cnotlike, ..."; break }
  { 'x' -contains 'x'}    { "FALSO! -contains es para listas!"; break }
  default                 { "Otros" }
}

# El for clásico
for($i = 1; $i -le 10; $i++) {
  "Número de ciclo $i"
}
# O más corto
1..10 | % { "Número de ciclo $_" }

# PowerShell también incluye
foreach ($var in 'valor1','valor2','valor3') { echo $var }
# while () {}
# do {} while ()
# do {} until ()

# Manejo de excepciones
try {} catch {} finally {}
try {} catch [System.NullReferenceException] {
	echo $_.Exception | Format-List -Force
}


### Proveedores
# Lista de archivos y directorios en la ubicación actual
ls # o el alias `dir`
cd ~ # ir al directorio principal del usuario

Get-Alias ls # -> Get-ChildItem
# ¿¡Eh!? Estos cmdlets tienen nombres genéricos porque a diferencia de otros lenguajes de scripting,
# PowerShell no opera únicamente en el directorio actual.
cd HKCU: # se dirige a la rama HKEY_CURRENT_USER del registro de Windows

# Para hacer un listado de todos los proveedores disponibles
Get-PSProvider


### Tuberías
# Los Cmdlets tienen parámetros que controlan su ejecución:
Get-ChildItem -Filter *.txt -Name # Se obtiene sólo el nombre de todos los archivos txt
# Sólo se necesita escribir caracteres de un parámetro hasta que deja de ser ambiguo
ls -fi *.txt -n # -f no se puede porque también existe -Force 
# Use `Get-Help Get-ChildItem -Full` para un tratado más completo

# Los results del cmdlet anterior se le pueden pasar como entrada al siguiente.
# `$_` representa el objeto actual en el objeto de tubería.
ls | Where-Object { $_.Name -match 'c' } | Export-CSV exportado.txt
ls | ? { $_.Name -match 'c' } | ConvertTo-HTML | Out-File exportado.html

# Si se confunde con la tubería use `Get-Member` para revisar
# los métodos y propiedades de los objetos de la tubería:
ls | Get-Member
Get-Date | gm

# ` es el caracter de continuación de línea. O termine la línea con un |
Get-Process | Sort-Object ID -Descending | Select-Object -First 10 Name,ID,VM `
	| Stop-Process -WhatIf

Get-EventLog Application -After (Get-Date).AddHours(-2) | Format-List

# Use % como una abreviación de ForEach-Object
(a,b,c) | ForEach-Object `
	-Begin { "Iniciando"; $counter = 0 } `
	-Process { "Procesando $_"; $counter++ } `
	-End { "Terminando: $counter" }

# El siguiente comando ps (alias de Get-Process) devuelve una tabla con 3 columnas
# La tercera columan es el valor de memoria virtual en MB y usando 2 dígitos decimales
# Las columnas calculadas pueden escribirse más extensamente como:
# `@{name='lbl';expression={$_}`
ps | Format-Table ID,Name,@{n='VM(MB)';e={'{0:n2}' -f ($_.VM / 1MB)}} -autoSize


### Funciones
# El atributo [string] es opcional.
function foo([string]$nombre) {
	echo "Hey $nombre, aquí tiene una función"
}

# Llamando una función
foo "Diga mi nombre"

# Funciones con parámetros nombrados, atributos de parámetros y documentación analizable
<#
.SYNOPSIS
Establecer un nuevo sitio web
.DESCRIPTION
Crea todo lo que su sitio necesite
.PARAMETER siteName
El nombre para el nuevo sitio web
.EXAMPLE
Crear-SitioWeb -Nombre SitioBonito -Po 5000
Crear-SitioWeb SiteWithDefaultPort
Crear-SitioWeb nombreSitio 2000 # ERROR! No se pudo validar arguemento de puerto
('nombre1','nombre2') | Crear-SitioWeb -Verbose
#>
function Crear-SitioWeb() {
	[CmdletBinding()]
	param (
		[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
		[Alias('nombre')]
		[string]$nombreSitio,
		[ValidateSet(3000,5000,8000)]
		[int]$puerto = 3000
	)
	BEGIN { Write-Verbose 'Creando nuevo(s) sitio(s) web' }
	PROCESS { echo "nombre: $nombreSitio, puerto: $puerto" }
	END { Write-Verbose 'Sitio(s) web creado(s)' }
}


### Todo es .NET
# Una cadena PS es, de hecho, una cadena tipo System.String de .NET 
# Todos los métodos y propiedades de .NET están disponibles
'cadena'.ToUpper().Replace('E', 'eee')
# O más powershellezco
'cadena'.ToUpper() -replace 'E', 'eee'

# ¿No recuerda cómo es que se llama cierto método .NET?
'cadena' | gm

# Sintaxis para ejecutar métodos .NET estáticos
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

# Nótese que cualquier función que proviene de .NET Framework REQUIERE paréntesis para ser invocada
# al contrario de las funciones definidas desde PS, las cuales NO PUEDEN ser invocadas con paréntesis.
# Si se invoca una función/cmdlet de PS usando paréntesis,
# es equivalente a que le estuviera pasando un parámetro de tipo lista
$writer = New-Object System.IO.StreamWriter($ruta, $true)
$writer.Write([Environment]::NewLine)
$writer.Dispose()

### Entrada/Salida
# Leyendo una variable 
$Nombre = Read-Host "¿Cómo se llama?"
echo "¡Hola $Nombre!"
[int]$Edad = Read-Host "¿Cuál es su edad?"

# Test-Path, Split-Path, Join-Path, Resolve-Path
# Get-Content filename # devuelve un string[]
# Set-Content, Add-Content, Clear-Content
Get-Command ConvertTo-*,ConvertFrom-*


### Material útil
# Actualizar la ruta de ejecuciones (PATH)
$env:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + 
	";" + [System.Environment]::GetEnvironmentVariable("Path", "User")

# Encontrar Python en el path
$env:PATH.Split(";") | Where-Object { $_ -like "*python*"}

# Cambiar el directorio de trabajo sin tener que memorizar la ruta anterior
Push-Location c:\temp # se cambia el directorio de trabajo a c:\temp
Pop-Location # revierte el cambio y se devuelve a donde estaba al principio
# Los aliases son : pushd y popd

# Desbloquear un archivo después de descargarlo de Internet
Get-ChildItem -Recurse | Unblock-File

# Abre Windows Explorer en la ruta actual (usando el alias ii de Invoke-Item)
ii .

# Pulse cualquier tecla para salir
$host.UI.RawUI.ReadKey()
return

# Para crear un acceso directo
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($link)
$Shortcut.TargetPath = $file
$Shortcut.WorkingDirectory = Split-Path $file
$Shortcut.Save()
```


Configurando el shell

```
# $Profile es la ruta completa para su `Microsoft.PowerShell_profile.ps1`
# Todo el código alojado allí será ejecutado cuando se ejecuta una nueva sesión de PS 
if (-not (Test-Path $Profile)) {
	New-Item -Type file -Path $Profile -Force
	notepad $Profile
}
# Más información en: `help about_profiles`
# Para un shell más productivo, asegúrese de verifivar el proyecto PSReadLine descrito abajo
```

Proyectos interesantes (EN)

* [Channel9](https://channel9.msdn.com/Search?term=powershell%20pipeline#ch9Search&lang-en=en) Tutoriales de PowerShell
* [PSGet](https://github.com/psget/psget) NuGet para PowerShell
* [PSReadLine](https://github.com/lzybkr/PSReadLine/) Una implementación inspirada en bash para PowerShell (¡Es tan buena que ahora viene con Windows10 por defecto!)
* [Posh-Git](https://github.com/dahlbyk/posh-git/) Un intérprete bonito de Git (¡Recomendado!)
* [PSake](https://github.com/psake/psake) Herramienta de automatización de compilaciones
* [Pester](https://github.com/pester/Pester) Framework de pruebas BDD
* [Jump-Location](https://github.com/tkellogg/Jump-Location) Powershell `cd` que lee su mente


Material no cubierto en esta guía  

* WMI: Windows Management Intrumentation (Get-CimInstance)  
* Multitarea: Start-Job -scriptBlock {...}, 
* Firmas de código
* Remoting (Enter-PSSession/Exit-PSSession; Invoke-Command)
---
language: python
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["Camilo Garrido", "http://www.twitter.com/hirohope"]
    - ["Fabio Souto", "http://fabiosouto.me"]
lang: es-es
filename: learnpython-es.py
---

Python fue creado por Guido Van Rossum en el principio de los 90. Ahora es uno
de los lenguajes más populares que existen. Me enamoré de Python por su claridad sintáctica.
Es básicamente pseudocódigo ejecutable.

¡Comentarios serán muy apreciados! Pueden contactarme en [@louiedinh](http://twitter.com/louiedinh) o louiedinh [at] [servicio de email de google]

Nota: Este artículo aplica a Python 2.7 específicamente, pero debería ser aplicable a Python 2.x. ¡Pronto un recorrido por Python 3!

```python
# Comentarios de una línea comienzan con una almohadilla (o signo gato)
""" Strings multilínea pueden escribirse
    usando tres "'s, y comúnmente son usados
    como comentarios.
"""

####################################################
## 1. Tipos de datos primitivos y operadores.
####################################################

# Tienes números
3 #=> 3

# Evidentemente puedes realizar operaciones matemáticas
1 + 1  #=> 2
8 - 1  #=> 7
10 * 2  #=> 20
35 / 5  #=> 7

# La división es un poco complicada. Es división entera y toma la parte entera
# de los resultados automáticamente.
5 / 2  #=> 2

# Para arreglar la división necesitamos aprender sobre 'floats'
# (números de coma flotante).
2.0     # Esto es un 'float'
11.0 / 4.0  #=> 2.75 ahhh...mucho mejor

# Resultado de la división de enteros truncada para positivos y negativos
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # funciona con números de coma flotante
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# El operador módulo devuelve el resto de una división entre enteros
7 % 3 # => 1

# Exponenciación (x elevado a y)
2**4 # => 16

# Refuerza la precedencia con paréntesis
(1 + 3) * 2  #=> 8

# Operadores booleanos
# Nota: "and" y "or" son sensibles a mayúsculas
True and False #=> False
False or True #=> True

# Podemos usar operadores booleanos con números enteros
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# Niega con 'not'
not True #=> False
not False #=> True

# Igualdad es ==
1 == 1 #=> True
2 == 1 #=> False

# Desigualdad es !=
1 != 1 #=> False
2 != 1 #=> True

# Más comparaciones
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# ¡Las comparaciones pueden ser concatenadas!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Strings se crean con " o '
"Esto es un string."
'Esto también es un string'

# ¡Strings también pueden ser sumados!
"Hola " + "mundo!" #=> "Hola mundo!"

# Un string puede ser tratado como una lista de caracteres
"Esto es un string"[0] #=> 'E'

# % pueden ser usados para formatear strings, como esto:
"%s pueden ser %s" % ("strings", "interpolados")

# Una forma más reciente de formatear strings es el método 'format'.
# Este método es la forma preferida
"{0} pueden ser {1}".format("strings", "formateados")
# Puedes usar palabras clave si no quieres contar.
"{nombre} quiere comer {comida}".format(nombre="Bob", comida="lasaña")

# None es un objeto
None #=> None

# No uses el símbolo de igualdad `==` para comparar objetos con None
# Usa `is` en lugar de
"etc" is None #=> False
None is None  #=> True

# El operador 'is' prueba la identidad del objeto. Esto no es
# muy útil cuando se trata de datos primitivos, pero es
# muy útil cuando se trata de objetos.

# None, 0, y strings/listas vacíos(as) todas se evalúan como False.
# Todos los otros valores son True
bool(0) #=> False
bool("") #=> False


####################################################
## 2. Variables y Colecciones
####################################################

# Imprimir es muy fácil
print "Soy Python. ¡Encantado de conocerte!"


# No hay necesidad de declarar las variables antes de asignarlas.
una_variable = 5    # La convención es usar guiones_bajos_con_minúsculas
una_variable #=> 5

# Acceder a variables no asignadas previamente es una excepción.
# Ve Control de Flujo para aprender más sobre el manejo de excepciones.
otra_variable  # Levanta un error de nombre

# 'if' puede ser usado como una expresión
"yahoo!" if 3 > 2 else 2 #=> "yahoo!"

# Las listas almacenan secuencias
lista = []
# Puedes empezar con una lista prellenada
otra_lista = [4, 5, 6]

# Añadir cosas al final de una lista con 'append'
lista.append(1)    # lista ahora es [1]
lista.append(2)    # lista ahora es [1, 2]
lista.append(4)    # lista ahora es [1, 2, 4]
lista.append(3)    # lista ahora es [1, 2, 4, 3]
# Remueve del final de la lista con 'pop'
lista.pop()        #=> 3 y lista ahora es [1, 2, 4]
# Pongámoslo de vuelta
lista.append(3)    # Nuevamente lista ahora es [1, 2, 4, 3].

# Accede a una lista como lo harías con cualquier arreglo
lista[0] #=> 1
# Mira el último elemento
lista[-1] #=> 3

# Mirar fuera de los límites es un error 'IndexError'
lista[4] # Levanta la excepción IndexError

# Puedes mirar por rango con la sintáxis de trozo.
# (Es un rango cerrado/abierto para ustedes los matemáticos.)
lista[1:3] #=> [2, 4]
# Omite el inicio
lista[2:] #=> [4, 3]
# Omite el final
lista[:3] #=> [1, 2, 4]

# Remueve elementos arbitrarios de una lista con 'del'
del lista[2] # lista ahora es [1, 2, 3]

# Puedes sumar listas
lista + otra_lista #=> [1, 2, 3, 4, 5, 6] - Nota: lista y otra_lista no se tocan

# Concatenar listas con 'extend'
lista.extend(otra_lista) # lista ahora es [1, 2, 3, 4, 5, 6]

# Chequea la existencia en una lista con
1 in lista #=> True

# Examina el tamaño de una lista con 'len'
len(lista) #=> 6


# Las tuplas son como las listas, pero son inmutables.
tupla = (1, 2, 3)
tupla[0] #=> 1
tupla[0] = 3  # Levanta un error TypeError

# También puedes hacer todas esas cosas que haces con listas
len(tupla) #=> 3
tupla + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tupla[:2] #=> (1, 2)
2 in tupla #=> True

# Puedes desempacar tuplas (o listas) en variables
a, b, c = (1, 2, 3)     # a ahora es 1, b ahora es 2 y c ahora es 3
# Tuplas son creadas por defecto si omites los paréntesis
d, e, f = 4, 5, 6
# Ahora mira que fácil es intercambiar dos valores
e, d = d, e     # d ahora es 5 y e ahora es 4


# Diccionarios almacenan mapeos
dicc_vacio = {}
# Aquí está un diccionario prellenado
dicc_lleno = {"uno": 1, "dos": 2, "tres": 3}

# Busca valores con []
dicc_lleno["uno"] #=> 1

# Obtén todas las llaves como una lista
dicc_lleno.keys() #=> ["tres", "dos", "uno"]
# Nota - El orden de las llaves del diccionario no está garantizada.
# Tus resultados podrían no ser los mismos del ejemplo.

# Obtén todos los valores como una lista
dicc_lleno.values() #=> [3, 2, 1]
# Nota - Lo mismo que con las llaves, no se garantiza el orden.

# Chequea la existencia de una llave en el diccionario con 'in'
"uno" in dicc_lleno #=> True
1 in dicc_lleno #=> False

# Buscar una llave inexistente deriva en KeyError
dicc_lleno["cuatro"] # KeyError

# Usa el método 'get' para evitar la excepción KeyError
dicc_lleno.get("uno") #=> 1
dicc_lleno.get("cuatro") #=> None
# El método 'get' soporta un argumento por defecto cuando el valor no existe.
dicc_lleno.get("uno", 4) #=> 1
dicc_lleno.get("cuatro", 4) #=> 4

# El método 'setdefault' es una manera segura de añadir nuevos pares
# llave-valor en un diccionario
dicc_lleno.setdefault("cinco", 5) #dicc_lleno["cinco"] es puesto con valor 5
dicc_lleno.setdefault("cinco", 6) #dicc_lleno["cinco"] todavía es 5


# Sets (conjuntos) almacenan ... bueno, conjuntos
conjunto_vacio = set()
# Inicializar un conjunto con montón de valores
un_conjunto = set([1,2,2,3,4]) # un_conjunto ahora es set([1, 2, 3, 4])

# Desde Python 2.7, {} puede ser usado para declarar un conjunto
conjunto_lleno = {1, 2, 2, 3, 4} # => {1 2 3 4}

# Añade más valores a un conjunto
conjunto_lleno.add(5) # conjunto_lleno ahora es {1, 2, 3, 4, 5}

# Haz intersección de conjuntos con &
otro_conjunto = {3, 4, 5, 6}
conjunto_lleno & otro_conjunto #=> {3, 4, 5}

# Haz unión de conjuntos con |
conjunto_lleno | otro_conjunto #=> {1, 2, 3, 4, 5, 6}

# Haz diferencia de conjuntos con -
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Chequea la existencia en un conjunto con 'in'
2 in conjunto_lleno #=> True
10 in conjunto_lleno #=> False


####################################################
## 3. Control de Flujo
####################################################

# Hagamos sólo una variable
una_variable = 5

# Aquí está una declaración de un 'if'. ¡La indentación es importante en Python!
# imprime "una_variable es menor que 10"
if una_variable > 10:
    print "una_variable es completamente mas grande que 10."
elif una_variable < 10:    # Este condición 'elif' es opcional.
    print "una_variable es mas chica que 10."
else:           # Esto también es opcional.
    print "una_variable es de hecho 10."


"""
For itera sobre listas
imprime:
    perro es un mamifero
    gato es un mamifero
    raton es un mamifero
"""
for animal in ["perro", "gato", "raton"]:
    # Puedes usar % para interpolar strings formateados
    print "%s es un mamifero" % animal

"""
`range(número)` retorna una lista de números
desde cero hasta el número dado
imprime:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
While itera hasta que una condición no se cumple.
imprime:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # versión corta de x = x + 1

# Maneja excepciones con un bloque try/except

# Funciona desde Python 2.6 en adelante:
try:
    # Usa raise para levantar un error
    raise IndexError("Este es un error de indice")
except IndexError as e:
    pass    # Pass no hace nada. Usualmente harias alguna recuperacion aqui.


####################################################
## 4. Funciones
####################################################

# Usa 'def' para crear nuevas funciones
def add(x, y):
    print "x es %s y y es %s" % (x, y)
    return x + y    # Retorna valores con una la declaración return

# Llamando funciones con parámetros
add(5, 6) #=> imprime "x es 5 y y es 6" y retorna 11

# Otra forma de llamar funciones es con argumentos de palabras claves
add(y=6, x=5)   # Argumentos de palabra clave pueden ir en cualquier orden.

# Puedes definir funciones que tomen un número variable de argumentos
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# Puedes definir funciones que toman un número variable de argumentos
# de palabras claves
def keyword_args(**kwargs):
    return kwargs

# Llamémosla para ver que sucede
keyword_args(pie="grande", lago="ness") #=> {"pie": "grande", "lago": "ness"}

# Puedes hacer ambas a la vez si quieres
def todos_los_argumentos(*args, **kwargs):
    print args
    print kwargs
"""
todos_los_argumentos(1, 2, a=3, b=4) imprime:
    (1, 2)
    {"a": 3, "b": 4}
"""

# ¡Cuando llames funciones, puedes hacer lo opuesto a varargs/kwargs!
# Usa * para expandir tuplas y usa ** para expandir argumentos de palabras claves.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
todos_los_argumentos(*args) # es equivalente a foo(1, 2, 3, 4)
todos_los_argumentos(**kwargs) # es equivalente a foo(a=3, b=4)
todos_los_argumentos(*args, **kwargs) # es equivalente a foo(1, 2, 3, 4, a=3, b=4)

# Python tiene funciones de primera clase
def crear_suma(x):
    def suma(y):
        return x + y
    return suma

sumar_10 = crear_suma(10)
sumar_10(3) #=> 13

# También hay funciones anónimas
(lambda x: x > 2)(3) #=> True

# Hay funciones integradas de orden superior
map(sumar_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Podemos usar listas por comprensión para mapeos y filtros agradables
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Clases
####################################################

# Heredamos de object para obtener una clase.
class Humano(object):

    # Un atributo de clase es compartido por todas las instancias de esta clase
    especie = "H. sapiens"

    # Constructor básico, se llama al instanciar la clase.
    def __init__(self, nombre):
        # Asigna el argumento al atributo nombre de la instancia
        self.nombre = nombre

    # Un método de instancia. Todos los metodos toman self como primer argumento
    def decir(self, msg):
       return "%s: %s" % (self.nombre, msg)

    # Un metodo de clase es compartido a través de todas las instancias
    # Son llamados con la clase como primer argumento
    @classmethod
    def get_especie(cls):
        return cls.especie

    # Un metodo estático es llamado sin la clase o instancia como referencia
    @staticmethod
    def roncar():
        return "*roncar*"


# Instancia una clase
i = Humano(nombre="Ian")
print i.decir("hi")     # imprime "Ian: hi"

j = Humano("Joel")
print j.decir("hello")  #imprime "Joel: hello"

# Llama nuestro método de clase
i.get_especie() #=> "H. sapiens"

# Cambia los atributos compartidos
Humano.especie = "H. neanderthalensis"
i.get_especie() #=> "H. neanderthalensis"
j.get_especie() #=> "H. neanderthalensis"

# Llama al método estático
Humano.roncar() #=> "*roncar*"


####################################################
## 6. Módulos
####################################################

# Puedes importar módulos
import math
print math.sqrt(16) #=> 4

# Puedes obtener funciones específicas desde un módulo
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# Puedes importar todas las funciones de un módulo
# Precaución: Esto no es recomendable
from math import *

# Puedes acortar los nombres de los módulos
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Los módulos de Python son sólo archivos ordinarios de Python.
# Puedes escribir tus propios módulos e importarlos. El nombre del módulo
# es el mismo del nombre del archivo.

# Puedes encontrar que funciones y atributos definen un módulo.
import math
dir(math)


####################################################
## 7. Avanzado
####################################################

# Los generadores permiten evaluación perezosa
def duplicar_numeros(iterable):
    for i in iterable:
        yield i + i

# Un generador crea valores sobre la marcha
# En vez de generar y devolver todos los valores de una vez, crea un valor
# en cada iteración. En este ejemplo los valores mayores que 15 no serán 
# procesados en duplicar_numeros.
# Nota: xrange es un generador que hace lo mismo que range.
# Crear una lista de 1 a 900000000 lleva mucho tiempo y ocupa mucho espacio.
# xrange crea un generador, mientras que range crea toda la lista.
# Añadimos un guión bajo a los nombres de variable que coinciden con palabras
# reservadas de python.
xrange_ = xrange(1, 900000000)

# duplica todos los números hasta que encuentra un resultado >= 30
for i in duplicar_numeros(xrange_):
    print i
    if i >= 30:
        break

# Decoradores
# en este ejemplo pedir rodea a hablar
# Si por_favor es True se cambiará el mensaje.
from functools import wraps


def pedir(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, por_favor = target_function(*args, **kwargs)
        if por_favor:
            return "{} {}".format(msg, "¡Por favor! Soy pobre :(")
        return msg

    return wrapper


@pedir
def hablar(por_favor=False):
    msg = "¿Me puedes comprar una cerveza?"
    return msg, por_favor

print hablar()  # ¿Me puedes comprar una cerveza?
print hablar(por_favor=True)  # ¿Me puedes comprar una cerveza? ¡Por favor! Soy pobre :(
```

## ¿Listo para más?

### Gratis y en línea

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)

### Encuadernados

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
translators:
    - ["Camilo Garrido", "http://twitter.com/hirohope"]
lang: es-es
filename: learnpython3-es.py
---

Python fue creado por Guido Van Rossum en el principio de los 90'. Ahora es uno
de los lenguajes más populares en existencia. Me enamoré de Python por su claridad sintáctica.
Es básicamente pseudocódigo ejecutable.

¡Comentarios serán muy apreciados! Pueden contactarme en [@louiedinh](http://twitter.com/louiedinh) o louiedinh [at] [servicio de email de google]

Nota: Este artículo aplica a Python 2.7 específicamente, pero debería ser aplicable a Python 2.x. ¡Pronto un recorrido por Python 3!

```python

# Comentarios de una línea comienzan con una almohadilla (o signo gato)

""" Strings multilinea pueden escribirse
    usando tres "'s, y comunmente son usados
    como comentarios.
"""

####################################################
## 1. Tipos de datos primitivos y operadores.
####################################################

# Tienes números
3 #=> 3

# Matemática es lo que esperarías
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20

# Excepto la división la cual por defecto retorna un número 'float' (número de coma flotante)
35 / 5  # => 7.0

# Cuando usas un float, los resultados son floats
3 * 2.0 # => 6.0

# Refuerza la precedencia con paréntesis
(1 + 3) * 2  # => 8


# Valores 'boolean' (booleanos) son primitivos
True
False

# Niega con 'not'
not True  # => False
not False  # => True


# Igualdad es ==
1 == 1  # => True
2 == 1  # => False

# Desigualdad es !=
1 != 1  # => False
2 != 1  # => True

# Más comparaciones
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# ¡Las comparaciones pueden ser concatenadas!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# Strings se crean con " o '
"Esto es un string."
'Esto también es un string'

# ¡Strings también pueden ser sumados!
"Hola " + "mundo!" #=> "Hola mundo!"

# Un string puede ser tratado como una lista de caracteres
"Esto es un string"[0] #=> 'E'

# .format puede ser usaro para darle formato a los strings, así:
"{} pueden ser {}".format("strings", "interpolados")

# Puedes repetir los argumentos de formateo para ahorrar tipeos.
"{0} sé ligero, {0} sé rápido, {0} brinca sobre la {1}".format("Jack", "vela") #=> "Jack sé ligero, Jack sé rápido, Jack brinca sobre la vela"
# Puedes usar palabras claves si no quieres contar.
"{nombre} quiere comer {comida}".format(nombre="Bob", food="lasaña") #=> "Bob quiere comer lasaña"


# None es un objeto
None  # => None

# No uses el símbolo de igualdad `==` para comparar objetos con None
# Usa `is` en su lugar
"etc" is None #=> False
None is None  #=> True

# None, 0, y strings/listas/diccionarios vacíos(as) todos se evalúan como False.
# Todos los otros valores son True
bool(0)  # => False
bool("")  # => False
bool([]) #=> False
bool({}) #=> False


####################################################
## 2. Variables y Colecciones
####################################################

# Python tiene una función para imprimir
print("Soy Python. Encantado de conocerte")

# No hay necesidad de declarar las variables antes de asignarlas.
una_variable = 5    # La convención es usar guiones_bajos_con_minúsculas
una_variable #=> 5

# Acceder a variables no asignadas previamente es una excepción.
# Ve Control de Flujo para aprender más sobre el manejo de excepciones.
otra_variable  # Levanta un error de nombre

# Listas almacena secuencias
lista = []
# Puedes empezar con una lista prellenada
otra_lista = [4, 5, 6]

# Añadir cosas al final de una lista con 'append'
lista.append(1)    #lista ahora es [1]
lista.append(2)    #lista ahora es [1, 2]
lista.append(4)    #lista ahora es [1, 2, 4]
lista.append(3)    #lista ahora es [1, 2, 4, 3]
# Remueve del final de la lista con 'pop'
lista.pop()        #=> 3 y lista ahora es [1, 2, 4]
# Pongámoslo de vuelta
lista.append(3)    # Nuevamente lista ahora es [1, 2, 4, 3].

# Accede a una lista como lo harías con cualquier arreglo
lista[0] #=> 1
# Mira el último elemento
lista[-1] #=> 3

# Mirar fuera de los límites es un error 'IndexError'
lista[4] # Levanta la excepción IndexError

# Puedes mirar por rango con la sintáxis de trozo.
# (Es un rango cerrado/abierto para ustedes los matemáticos.)
lista[1:3] #=> [2, 4]
# Omite el inicio
lista[2:] #=> [4, 3]
# Omite el final
lista[:3] #=> [1, 2, 4]
# Selecciona cada dos elementos
lista[::2]   # =>[1, 4]
# Invierte la lista
lista[::-1]   # => [3, 4, 2, 1]
# Usa cualquier combinación de estos para crear trozos avanzados
# lista[inicio:final:pasos]

# Remueve elementos arbitrarios de una lista con 'del'
del lista[2] # lista ahora es [1, 2, 3]

# Puedes sumar listas
lista + otra_lista #=> [1, 2, 3, 4, 5, 6] - Nota: lista y otra_lista no se tocan

# Concatenar listas con 'extend'
lista.extend(otra_lista) # lista ahora es [1, 2, 3, 4, 5, 6]

# Chequea la existencia en una lista con 'in'
1 in lista #=> True

# Examina el largo de una lista con 'len'
len(lista) #=> 6


# Tuplas son como listas pero son inmutables.
tupla = (1, 2, 3)
tupla[0] #=> 1
tupla[0] = 3  # Levanta un error TypeError

# También puedes hacer todas esas cosas que haces con listas
len(tupla) #=> 3
tupla + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tupla[:2] #=> (1, 2)
2 in tupla #=> True

# Puedes desempacar tuplas (o listas) en variables
a, b, c = (1, 2, 3)     # a ahora es 1, b ahora es 2 y c ahora es 3
# Tuplas son creadas por defecto si omites los paréntesis
d, e, f = 4, 5, 6
# Ahora mira que fácil es intercambiar dos valores
e, d = d, e     # d ahora es 5 y e ahora es 4


# Diccionarios almacenan mapeos
dicc_vacio = {}
# Aquí está un diccionario prellenado
dicc_lleno = {"uno": 1, "dos": 2, "tres": 3}

# Busca valores con []
dicc_lleno["uno"] #=> 1

# Obtén todas las llaves como una lista con 'keys()'. Necesitamos envolver la llamada en 'list()' porque obtenemos un iterable. Hablaremos de eso luego.
list(dicc_lleno.keys()) #=> ["tres", "dos", "uno"]
# Nota - El orden de las llaves del diccionario no está garantizada.
# Tus resultados podrían no ser los mismos del ejemplo.

# Obtén todos los valores como una lista. Nuevamente necesitamos envolverlas en una lista para sacarlas del iterable.
list(dicc_lleno.values()) #=> [3, 2, 1]
# Nota - Lo mismo que con las llaves, no se garantiza el orden.

# Chequea la existencia de una llave en el diccionario con 'in'
"uno" in dicc_lleno #=> True
1 in dicc_lleno #=> False

# Buscar una llave inexistente deriva en KeyError
dicc_lleno["cuatro"] # KeyError

# Usa el método 'get' para evitar la excepción KeyError
dicc_lleno.get("uno") #=> 1
dicc_lleno.get("cuatro") #=> None
# El método 'get' soporta un argumento por defecto cuando el valor no existe.
dicc_lleno.get("uno", 4) #=> 1
dicc_lleno.get("cuatro", 4) #=> 4

# El método 'setdefault' inserta en un diccionario solo si la llave no está presente
dicc_lleno.setdefault("cinco", 5) #dicc_lleno["cinco"] es puesto con valor 5
dicc_lleno.setdefault("cinco", 6) #dicc_lleno["cinco"] todavía es 5


# Remueve llaves de un diccionario con 'del'
del dicc_lleno['uno'] # Remueve la llave 'uno' de dicc_lleno

# Sets (conjuntos) almacenan ... bueno, conjuntos
conjunto_vacio = set()
# Inicializar un conjunto con montón de valores. Yeah, se ve un poco como un diccionario. Lo siento.
un_conjunto = {1,2,2,3,4} # un_conjunto ahora es {1, 2, 3, 4}

# Añade más valores a un conjunto
conjunto_lleno.add(5) # conjunto_lleno ahora es {1, 2, 3, 4, 5}

# Haz intersección de conjuntos con &
otro_conjunto = {3, 4, 5, 6}
conjunto_lleno & otro_conjunto #=> {3, 4, 5}

# Haz unión de conjuntos con |
conjunto_lleno | otro_conjunto #=> {1, 2, 3, 4, 5, 6}

# Haz diferencia de conjuntos con -
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Chequea la existencia en un conjunto con 'in'
2 in conjunto_lleno #=> True
10 in conjunto_lleno #=> False


####################################################
## 3. Control de Flujo
####################################################

# Let's just make a variable
some_var = 5

# Aquí está una declaración de un 'if'. ¡La indentación es significativa en Python!
# imprime "una_variable es menor que 10"
if una_variable > 10:
    print("una_variable es completamente mas grande que 10.")
elif una_variable < 10:    # Este condición 'elif' es opcional.
    print("una_variable es mas chica que 10.")
else:           # Esto también es opcional.
    print("una_variable es de hecho 10.")

"""
For itera sobre listas
imprime:
    perro es un mamifero
    gato es un mamifero
    raton es un mamifero
"""
for animal in ["perro", "gato", "raton"]:
    # Puedes usar % para interpolar strings formateados
    print("{} es un mamifero".format(animal))

"""
`range(número)` retorna una lista de números
desde cero hasta el número dado
imprime:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
While itera hasta que una condición no se cumple.
imprime:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # versión corta de x = x + 1

# Maneja excepciones con un bloque try/except
try:
    # Usa raise para levantar un error
    raise IndexError("Este es un error de indice")
except IndexError as e:
    pass    # Pass no hace nada. Usualmente harias alguna recuperacion aqui.

# Python oferce una abstracción fundamental llamada Iterable.
# Un iterable es un objeto que puede ser tratado como una sequencia.
# El objeto es retornado por la función 'range' es un iterable.

dicc_lleno = {"uno": 1, "dos": 2, "tres": 3}
nuestro_iterable = dicc_lleno.keys()
print(nuestro_iterable) #=> range(1,10). Este es un objeto que implementa nuestra interfaz Iterable

Podemos recorrerla.
for i in nuestro_iterable:
    print(i)    # Imprime uno, dos, tres

# Aunque no podemos selecionar un elemento por su índice.
nuestro_iterable[1]  # Genera un TypeError

# Un iterable es un objeto que sabe como crear un iterador.
nuestro_iterator = iter(nuestro_iterable)

# Nuestro iterador es un objeto que puede recordar el estado mientras lo recorremos.
# Obtenemos el siguiente objeto llamando la función __next__.
nuestro_iterator.__next__()  #=> "uno"

# Mantiene el estado mientras llamamos __next__.
nuestro_iterator.__next__()  #=> "dos"
nuestro_iterator.__next__()  #=> "tres"

# Después que el iterador ha retornado todos sus datos, da una excepción StopIterator.
nuestro_iterator.__next__() # Genera StopIteration

# Puedes obtener todos los elementos de un iterador llamando a list() en el.
list(dicc_lleno.keys())  #=> Retorna ["uno", "dos", "tres"]



####################################################
## 4. Funciones
####################################################

# Usa 'def' para crear nuevas funciones
def add(x, y):
    print("x es {} y y es {}".format(x, y))
    return x + y    # Retorna valores con una la declaración return

# Llamando funciones con parámetros
add(5, 6) #=> imprime "x es 5 y y es 6" y retorna 11

# Otra forma de llamar funciones es con argumentos de palabras claves
add(y=6, x=5)   # Argumentos de palabra clave pueden ir en cualquier orden.


# Puedes definir funciones que tomen un número variable de argumentos
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# Puedes definir funciones que toman un número variable de argumentos
# de palabras claves
def keyword_args(**kwargs):
    return kwargs

# Llamémosla para ver que sucede
keyword_args(pie="grande", lago="ness") #=> {"pie": "grande", "lago": "ness"}


# Puedes hacer ambas a la vez si quieres
def todos_los_argumentos(*args, **kwargs):
    print args
    print kwargs
"""
todos_los_argumentos(1, 2, a=3, b=4) imprime:
    (1, 2)
    {"a": 3, "b": 4}
"""

# ¡Cuando llames funciones, puedes hacer lo opuesto a varargs/kwargs!
# Usa * para expandir tuplas y usa ** para expandir argumentos de palabras claves.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
todos_los_argumentos(*args) # es equivalente a foo(1, 2, 3, 4)
todos_los_argumentos(**kwargs) # es equivalente a foo(a=3, b=4)
todos_los_argumentos(*args, **kwargs) # es equivalente a foo(1, 2, 3, 4, a=3, b=4)

# Python tiene funciones de primera clase
def crear_suma(x):
    def suma(y):
        return x + y
    return suma

sumar_10 = crear_suma(10)
sumar_10(3) #=> 13

# También hay funciones anónimas
(lambda x: x > 2)(3) #=> True

# Hay funciones integradas de orden superior
map(sumar_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Podemos usar listas por comprensión para mapeos y filtros agradables
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Classes
####################################################


# Heredamos de object para obtener una clase.
class Humano(object):

    # Un atributo de clase es compartido por todas las instancias de esta clase
    especie = "H. sapiens"

    # Constructor basico
    def __init__(self, nombre):
        # Asigna el argumento al atributo nombre de la instancia
        self.nombre = nombre

    # Un metodo de instancia. Todos los metodos toman self como primer argumento
    def decir(self, msg):
       return "%s: %s" % (self.nombre, msg)

    # Un metodo de clase es compartido a través de todas las instancias
    # Son llamados con la clase como primer argumento
    @classmethod
    def get_especie(cls):
        return cls.especie

    # Un metodo estatico es llamado sin la clase o instancia como referencia
    @staticmethod
    def roncar():
        return "*roncar*"


# Instancia una clase
i = Humano(nombre="Ian")
print i.decir("hi")     # imprime "Ian: hi"

j = Humano("Joel")
print j.decir("hello")  #imprime "Joel: hello"

# Llama nuestro método de clase
i.get_especie() #=> "H. sapiens"

# Cambia los atributos compartidos
Humano.especie = "H. neanderthalensis"
i.get_especie() #=> "H. neanderthalensis"
j.get_especie() #=> "H. neanderthalensis"

# Llama al método estático
Humano.roncar() #=> "*roncar*"


####################################################
## 6. Módulos
####################################################

# Puedes importar módulos
import math
print(math.sqrt(16)) #=> 4.0

# Puedes obtener funciones específicas desde un módulo
from math import ceil, floor
print(ceil(3.7)) #=> 4.0
print(floor(3.7))#=> 3.0

# Puedes importar todas las funciones de un módulo
# Precaución: Esto no es recomendable
from math import *

# Puedes acortar los nombres de los módulos
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Los módulos de Python son sólo archivos ordinarios de Python.
# Puedes escribir tus propios módulos e importarlos. El nombre del módulo
# es el mismo del nombre del archivo.

# Puedes encontrar que funciones y atributos definen un módulo.
import math
dir(math)


####################################################
## 7. Avanzado
####################################################

# Los generadores te ayudan a hacer un código perezoso (lazy)
def duplicar_numeros(iterable):
    for i in iterable:
        yield i + i

# Un generador crea valores sobre la marcha.
# En vez de generar y retornar todos los valores de una vez, crea uno en cada iteración.
# Esto significa que valores más grandes que 15 no serán procesados en 'duplicar_numeros'.
# Fíjate que 'range' es un generador. Crear una lista 1-900000000 tomaría mucho tiempo en crearse.
_rango = range(1, 900000000)
# Duplicará todos los números hasta que un resultado >= se encuentre.
for i in duplicar_numeros(_rango):
    print(i)
    if i >= 30:
        break


# Decoradores
# en este ejemplo 'pedir' envuelve a 'decir'
# Pedir llamará a 'decir'. Si decir_por_favor es True entonces cambiará el mensaje a retornar
from functools import wraps


def pedir(_decir):
    @wraps(_decir)
    def wrapper(*args, **kwargs):
        mensaje, decir_por_favor = _decir(*args, **kwargs)
        if decir_por_favor:
            return "{} {}".format(mensaje, "¡Por favor! Soy pobre :(")
        return mensaje

    return wrapper


@pedir
def say(decir_por_favor=False):
    mensaje = "¿Puedes comprarme una cerveza?"
    return mensaje, decir_por_favor


print(decir())  # ¿Puedes comprarme una cerveza?
print(decir(decir_por_favor=True))  # ¿Puedes comprarme una cerveza? ¡Por favor! Soy pobre :()
```

## ¿Listo para más?

### Gratis y en línea

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/3/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)

### Encuadernados

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: Statistical computing with Python
contributors:
    - ["e99n09", "https://github.com/e99n09"]
filename: pythonstatcomp-es.py
translators:
    - ["Damaso Sanoja", "https://github.com/damasosanoja"]
lang: es-es
---

Este es un tutorial de como realizar tareas típicas de programación estadística usando Python. Está destinado a personas con cierta familiaridad con Python y con experiencia en programación estadística en lenguajes como R, Stata, SAS, SPSS, or MATLAB.

```python

# 0. Cómo configurar ====

""" Configurar con IPython y pip install lo siguiente: numpy, scipy, pandas,
    matplotlib, seaborn, requests.
        Asegúrese de realizar este tutorial con el IPython notebook para tener fácil
    acceso a las ayudas en tiempo real y la documentación respectiva.
"""

# 1. Captura de datos ====

""" Muchos prefieren Python sobre R ya que quieren interactuar mucho
    con la web, bien sea haciendo webscraping o solicitando datos mediante
    un API. Esto se puede hacer en R, pero en el contexto de un proyecto
    que ya usa Python, existen beneficios al mantener un solo lenguaje.
"""

import requests # para llamados HTTP (webscraping, APIs)
import os

# webscraping
r = requests.get("https://github.com/adambard/learnxinyminutes-docs")
r.status_code # si es 200, el llamado ha sido exitoso
r.text # código fuente de la página
print(r.text) # formateado y embellecido
# graba el código fuente en un fichero:
os.getcwd() # verifica cual es el directorio de trabajo
f = open("learnxinyminutes.html","wb")
f.write(r.text.encode("UTF-8"))
f.close()

# descargando un csv
fp = "https://raw.githubusercontent.com/adambard/learnxinyminutes-docs/master/"
fn = "pets.csv"
r = requests.get(fp + fn)
print(r.text)
f = open(fn,"wb")
f.write(r.text.encode("UTF-8"))
f.close()

""" para saber más del módulo de peticiones, incluyendo APIs, ver
    http://docs.python-requests.org/en/latest/user/quickstart/
"""

# 2. Leyendo un fichero CSV ====

""" El paquete pandas de Wes McKinney brinda objetos 'DataFrame' en Python. Si
    has usado R, ya estarás familiarizado con la idea de "data.frame".
"""

import pandas as pd, numpy as np, scipy as sp
pets = pd.read_csv(fn)
pets
#       nombre edad  peso especies
# 0    fluffy     3    14      cat
# 1  vesuvius     6    23     fish
# 2       rex     5    34      dog

""" Usuarios de R: notar que Python, al igual que otros lenguajes de programación
    normales, comienza indexando desde 0. R de forma inusual comienza desde 1.
"""

# dos formas distintas de imprimir una columna
pets.age
pets["age"]

pets.head(2) # imprime las primeras dos filas
pets.tail(1) # imprime la última fila

pets.name[1] # 'vesuvius'
pets.species[0] # 'cat'
pets["weight"][2] # 34

# en R, puedes esperar obtener 3 filas haciendo esto, pero aquí obtienes 2:
pets.age[0:2]
# 0    3
# 1    6

sum(pets.age)*2 # 28
max(pets.weight) - min(pets.weight) # 20

""" Si estás procesando grandes cantidades de cálculos de álgebra lineal, podrías
    querer usar matrices, no DataFrames. Los DataFrames son ideales para combinar
    columnas de diferentes tipos.
"""

# 3. Gráficas ====

import matplotlib as mpl, matplotlib.pyplot as plt
%matplotlib inline

# Para hacer virtualización de datos en Python, usa matplotlib

plt.hist(pets.age);

plt.boxplot(pets.weight);

plt.scatter(pets.age, pets.weight); plt.xlabel("age"); plt.ylabel("weight");

# seaborn está por encima de matplotlib y logra mejores gráficos

import seaborn as sns

plt.scatter(pets.age, pets.weight); plt.xlabel("age"); plt.ylabel("weight");

# también hay algunas funciones gráficas específicas de seaborn
# nota como seaborn etiqueta automáticamente el eje x en este gráfico de barras
sns.barplot(pets["age"])

# los veteranos de R pueden seguir usando ggplot
from ggplot import *
ggplot(aes(x="age",y="weight"), data=pets) + geom_point() + labs(title="pets")
# fuente: https://pypi.python.org/pypi/ggplot

# incluso hay un porteo d3.js: https://github.com/mikedewar/d3py

# 4. Limpieza simple de datos y análisis exploratorio ====

""" Tenemos ahora un ejemplo más complicado que demuestra un flujo básico para
    limpieza de datos que lleva a la creación de algunos gráficos exploratorios
    y la ejecución de una regresión lineal.
        El conjunto de datos fue transcrito de Wikipedia a mano. Contiene
    todos los Emperadores Romanos Sagrados y fechas claves en sus vidas
    (nacimiento, muerte, coronación, etc.).
        El objetivo del análisis es explorar si existe alguna relación
    entre el año de nacimiento del Emperador y su tiempo de vida.
    fuente de datos: https://en.wikipedia.org/wiki/Holy_Roman_Emperor
"""

# cargar algunos datos de los Emperadores Romanos Sagrados
url = "https://raw.githubusercontent.com/e99n09/R-notes/master/data/hre.csv"
r = requests.get(url)
fp = "hre.csv"
f = open(fp,"wb")
f.write(r.text.encode("UTF-8"))
f.close()

hre = pd.read_csv(fp)

hre.head()
"""
   Ix      Dynasty        Name        Birth             Death Election 1  
0 NaN  Carolingian   Charles I  2 April 742    28 January 814        NaN   
1 NaN  Carolingian     Louis I          778       20 June 840        NaN   
2 NaN  Carolingian   Lothair I          795  29 September 855        NaN   
3 NaN  Carolingian    Louis II          825     12 August 875        NaN   
4 NaN  Carolingian  Charles II  13 June 823     6 October 877        NaN   

  Election 2      Coronation 1   Coronation 2 Ceased to be Emperor  
0        NaN   25 December 800            NaN       28 January 814   
1        NaN  11 September 813  5 October 816          20 June 840   
2        NaN       5 April 823            NaN     29 September 855   
3        NaN        Easter 850     18 May 872        12 August 875   
4        NaN   29 December 875            NaN        6 October 877   

  Descent from whom 1 Descent how 1 Descent from whom 2 Descent how 2  
0                 NaN           NaN                 NaN           NaN  
1           Charles I           son                 NaN           NaN  
2             Louis I           son                 NaN           NaN  
3           Lothair I           son                 NaN           NaN  
4             Louis I           son                 NaN           NaN  
"""

# limpiar las columnas de Nacimiento y Muerte

import re # módulo para expresiones regulares

rx = re.compile(r'\d+$') # coincidencia de últimos dígitos

""" Esta función aplica una expresión regular a una columna de entrada (Birth,
    Death), nivela la lista resultante, la convierte en un objeto Series, y
    finalmente convierte el tipo del objeto Series de string a entero. Para
    más información sobre que hace cada parte del código, ver:
      - https://docs.python.org/2/howto/regex.html
      - http://stackoverflow.com/questions/11860476/how-to-unlist-a-python-list
      - http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html
"""
def extractYear(v):
    return(pd.Series(reduce(lambda x,y: x+y,map(rx.findall,v),[])).astype(int))

hre["BirthY"] = extractYear(hre.Birth)
hre["DeathY"] = extractYear(hre.Death)

# hacer una columna decir la edad estimada
hre["EstAge"] = hre.DeathY.astype(int) - hre.BirthY.astype(int)

# gráfica de dispersión simple, sin línea de tendencia, el color representa dinastía
sns.lmplot("BirthY", "EstAge", data=hre, hue="Dynasty", fit_reg=False);

# usa scipy para hacer regresiones lineales
from scipy import stats
(slope,intercept,rval,pval,stderr)=stats.linregress(hre.BirthY,hre.EstAge)
# código fuente: http://wiki.scipy.org/Cookbook/LinearRegression

# verifica la pendiente (slope)
slope # 0.0057672618839073328

# verifica el valor R^2 :
rval**2 # 0.020363950027333586

# verifica el valor p
pval # 0.34971812581498452

# usa seaborn para hacer un gráfico de dispersión y dibujar una regresión lineal
# de la tendencia
sns.lmplot("BirthY", "EstAge", data=hre);

""" Para más información sobre seaborn, ver
      - http://web.stanford.edu/~mwaskom/software/seaborn/
      - https://github.com/mwaskom/seaborn
    Para más información sobre SciPy, ver
      - http://wiki.scipy.org/SciPy
      - http://wiki.scipy.org/Cookbook/
    Para ver una versión del análisis de los Emperadores Romanos usando R, ver
      - http://github.com/e99n09/R-notes/blob/master/holy_roman_emperors_dates.R
"""
```

Si quieres aprender más, obtén _Python for Data Analysis_ por Wes McKinney. Es un extraordinario recurso usado como referencia para escribir este tutorial.

También puedes encontrar gran cantidad de tutoriales interactivos de IPython en temas específicos a tus intereses, como Pilon de Cam Davidson <a href="http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/" Title="Probabilistic Programming and Bayesian Methods for Hackers">Probabilistic Programming and Bayesian Methods for Hackers</a>.

Ver más módulos para investigar:
   - análisis de texto y procesamiento natural del lenguaje: nltk, http://www.nltk.org
   - análisis de redes sociales: igraph, http://igraph.org/python/
---
language: R
contributors:
    - ["e99n09", "http://github.com/e99n09"]
    - ["isomorphismes", "http://twitter.com/isomorphisms"]
translators:
    - ["David Hsieh", "http://github.com/deivuh"]
lang: es-es    
filename: learnr-es.r
---

R es un lenguaje de computación estadística. Tiene muchas librerías para cargar
y limpiar sets de datos, ejecutar procedimientos estadísticos y generar
gráficas. También puedes ejecutar comandos `R` dentro de un documento de
LaTeX.

```r

# Los comentariso inician con símbolos numéricos.

# No puedes hacer comentarios de múltiples líneas
# pero puedes agrupar múltiples comentarios de esta manera. 

# En Windows puedes utilizar CTRL-ENTER para ejecutar una línea.
# En Mac utilizas COMMAND-ENTER


#############################################################################
# Cosas que puedes hacer sin entender nada acerca de programación
#############################################################################

# En esta sección, mostramos algunas cosas chileras / cool que puedes hacer en
# R sin entender nada de programación. No te preocupes en entender nada 
# de lo que hace este código. Solo disfruta!

data()	        # Examinar sets de datos pre-cargados
data(rivers)	# Obtiene este: Lengths of Major North American Rivers"
ls()	        # Fijarse que "rivers" ahora aparece en el workspace
head(rivers)	# Echarle un ojo al set de datos
# 735 320 325 392 524 450

length(rivers)	# ¿Cuántos ríos fueron medidos?
# 141
summary(rivers) # ¿Cuáles son algunas estadísticas generales?
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#  135.0   310.0   425.0   591.2   680.0  3710.0

# Generar una gráfica tallo-y-hoja (Visualización de datos tipo histograma)
stem(rivers)

# El punto decimal son 2 dígitos a la derecha de | 
#
#   0 | 4
#   2 | 011223334555566667778888899900001111223333344455555666688888999
#   4 | 111222333445566779001233344567
#   6 | 000112233578012234468
#   8 | 045790018
#  10 | 04507
#  12 | 1471
#  14 | 56
#  16 | 7
#  18 | 9
#  20 |
#  22 | 25
#  24 | 3
#  26 |
#  28 |
#  30 |
#  32 |
#  34 |
#  36 | 1

stem(log(rivers)) # Fijarse que la data no es normal ni log-normal!
# Toma eso, fundamentalistas de la curva de campana!

# El punto decimal está a 1 dígito a la izquierda del |
#
#  48 | 1
#  50 |
#  52 | 15578
#  54 | 44571222466689
#  56 | 023334677000124455789
#  58 | 00122366666999933445777
#  60 | 122445567800133459
#  62 | 112666799035
#  64 | 00011334581257889
#  66 | 003683579
#  68 | 0019156
#  70 | 079357
#  72 | 89
#  74 | 84
#  76 | 56
#  78 | 4
#  80 |
#  82 | 2

# Generar un histograma:
hist(rivers, col="#333333", border="white", breaks=25) # Juega con los estos parámetros
hist(log(rivers), col="#333333", border="white", breaks=25) # Generarás más gráficas después

# Aquí hay otro set de datos pre-cargado. R tiene bastantes de éstos.
data(discoveries)
plot(discoveries, col="#333333", lwd=3, xlab="Year",
     main="Number of important discoveries per year")
plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year",
     main="Number of important discoveries per year")

# En lugar de dejar el orden por defecto (por año),
# podemos ordenar de tal manera que muestre qué es típico:
sort(discoveries)
#  [1]  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2
# [26]  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3
# [51]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  4  4  4  4  4  4  4
# [76]  4  4  4  4  5  5  5  5  5  5  5  6  6  6  6  6  6  7  7  7  7  8  9 10 12

stem(discoveries, scale=2)
#
#  El punto decimal se encuentra en |
#
#   0 | 000000000
#   1 | 000000000000
#   2 | 00000000000000000000000000
#   3 | 00000000000000000000
#   4 | 000000000000
#   5 | 0000000
#   6 | 000000
#   7 | 0000
#   8 | 0
#   9 | 0
#  10 | 0
#  11 |
#  12 | 0

max(discoveries)
# 12
summary(discoveries)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#    0.0     2.0     3.0     3.1     4.0    12.0

# Tirar los dados varias veces
round(runif(7, min=.5, max=6.5))
# 1 4 6 1 4 6 4
# Tus números será diferente de los míos, a menos que tengamos el mismo valor 
# de random.seed(31337)

# Dibuja de un Gaussian 9 veces
rnorm(9)
# [1]  0.07528471  1.03499859  1.34809556 -0.82356087  0.61638975 -1.88757271
# [7] -0.59975593  0.57629164  1.08455362



##################################################
# Tipos de datos y aritmética básica
##################################################

# Ahora para la parte de programación orientada a objetos del tutorial.
# En esta sección conocerás los tipos de datos importantes de R:
# Enteros, numéricos, caracteres, lógicos, y factores.
# Hay otros, pero esos son los que menos necesitas para empezar.

# ENTEROS
# Enteros de almacenamiento largo son escritos con L
5L # 5
class(5L) # "integer"
# (Try ?class para más información en la función class().)
# En R, cada valor único, como 5L, es considerado un vector de logitud 1
length(5L) # 1
# También puedes tener un vector de enteros con longitud > 1:
c(4L, 5L, 8L, 3L) # 4 5 8 3
length(c(4L, 5L, 8L, 3L)) # 4
class(c(4L, 5L, 8L, 3L)) # "integer"

# NUMÉRICOS
# Un "numérico" es un número de punto flotante de doble precisión.
5 # 5
class(5) # "numeric"
# Nuevamente, todo en R es un vector;
# puedes hacer un vector numérico con más de un elemento
c(3,3,3,2,2,1) # 3 3 3 2 2 1
# También puedes utilizar el notación científica
5e4 # 50000
6.02e23 # Número de Avogadro
1.6e-35 # Logintud Planck 
# También puedes tener números infinitamente grandes o pequeños
class(Inf)	# "numeric"
class(-Inf)	# "numeric"
# Puede que uses "Inf", por ejemplo, en integrate(dnorm, 3, Inf);
# esto obvia las tablas de puntos Z.

# ARITMÉTICA BÁSICA
# Puedes hacer aritmética con números
# Haciendo aritmética en un mix de enteros y numéricos, te da otro numérico
10L + 66L # 76      # entero mas entero da entero
53.2 - 4  # 49.2    # entero menos entero da numérico
2.0 * 2L  # 4       # numérico veces entero da numérico
3L / 4    # 0.75    # entero sobre numérico da numérico
3 %% 2	  # 1       # el residuo de dos numéricos es otro numérico
# La aritmética ilegal rinde un "not-a-number"
0 / 0 # NaN
class(NaN) # "numeric"
# Puedes hacer aritmética con dos vectores con longitud mayor a 1,
# siempre que la longitud del vector mayor es un entero múltiplo del menor.
c(1,2,3) + c(1,2,3) # 2 4 6

# CARACTERES
# No hay diferencia entre strings y caracteres en R
"Horatio" # "Horatio"
class("Horatio") # "character"
class('H') # "character"
# Ambos eran vectores de caracteres de longitud 1
# Aquí hay uno más largo:
c('alef', 'bet', 'gimmel', 'dalet', 'he')
# =>
# "alef"   "bet"    "gimmel" "dalet"  "he"
length(c("Call","me","Ishmael")) # 3
# Puedes hacer operaciones regex en vectores de caracteres:
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
# R tiene varios vectores predefinidos de caracteres 
letters
# =>
#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [20] "t" "u" "v" "w" "x" "y" "z"
month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

# LÓGICOS
# En R, un "logical" es un boolean
class(TRUE)	# "logical"
class(FALSE)	# "logical"
# Ese comportamiento es normal
TRUE == TRUE	# TRUE
TRUE == FALSE	# FALSE
FALSE != FALSE	# FALSE
FALSE != TRUE	# TRUE
# El dato faltante (NA) es lógico también
class(NA)	# "logical"
# Utiliza | y & para operaciones lógicas
# OR
TRUE | FALSE	# TRUE
# AND
TRUE & FALSE	# FALSE
# Puedes probar si x es TRUE (verdadero)
isTRUE(TRUE)	# TRUE
# Aquí tenemos un vector lógico con varios elementos:
c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE
c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE

# FACTORES
# La clase factor es para datos de categoría
# Los factores pueden ser ordenados (como las calificaciones de los niños) 
# o sin orden (como el género)
factor(c("female", "female", "male", NA, "female"))
#  female female male   <NA>   female
# Levels: female male
# Los "levels" son los valores que los datos categóricos pueden tener
# Tomar nota que los datos faltantes no entran a los niveles
levels(factor(c("male", "male", "female", NA, "female"))) # "female" "male"
# Si un vector de factores tiene longitud 1, sus niveles también tendrán
# una longitud de 1 también

length(factor("male")) # 1
length(levels(factor("male"))) # 1
# Los factores son comúnmente vistos en marcos de dato, y una estructura de 
# datos que cubriremos después
data(infert) # "Infertility after Spontaneous and Induced Abortion"
levels(infert$education) # "0-5yrs"  "6-11yrs" "12+ yrs"

# NULL
# "NULL" es uno raro; utilízalo para "limpiar" un vector
class(NULL)	# NULL
parakeet = c("beak", "feathers", "wings", "eyes")
parakeet
# =>
# [1] "beak"     "feathers" "wings"    "eyes"
parakeet <- NULL
parakeet
# =>
# NULL

# COERCIÓN DE TIPO
# La coerción de tipos es cuando forzas un valor diferente tipo al que puede tomar.
as.character(c(6, 8)) # "6" "8"
as.logical(c(1,0,1,1)) # TRUE FALSE  TRUE  TRUE
# Si pones elementos de diferentes tipos en un vector, coerciones raras pasan:
c(TRUE, 4) # 1 4
c("dog", TRUE, 4) # "dog"  "TRUE" "4"
as.numeric("Bilbo")
# =>
# [1] NA
# Warning message:
# NAs introduced by coercion

# También tomar nota: Esos solo eran datos de tipos básicos
# Hay mucho más tipos de datos, como las fechas, series de tiempo, etc.


##################################################
# Variables, ciclos, condiciones (if/else)
##################################################

# A variable is like a box you store a value in for later use.
# We call this "assigning" the value to the variable.
# Having variables lets us write loops, functions, and if/else statements

# VARIABLES
# Muchas maneras de asignar valores:
x = 5 # esto es posible
y <- "1" # esto es preferido
TRUE -> z # estos funciona pero es raro

# CICLOS
# Tenemos ciclos 'for'
for (i in 1:4) {
  print(i)
}
# Tenemos ciclos 'while'
a <- 10
while (a > 4) {
	cat(a, "...", sep = "")
	a <- a - 1
}
# Ten en mente que los ciclos 'for' y 'while' son lentos en R
# Operaciones con vectores enteros (i.e. una fila o columna completa)
# o tipos de función apply() (que discutiremos después) son preferidos

# CONDICIONES (IF/ELSE)
# De nuevo, bastante normal
if (4 > 3) {
	print("4 is greater than 3")
} else {
	print("4 is not greater than 3")
}
# =>
# [1] "4 is greater than 3"

# FUNCIONES
# Definidos de la siguiente manera:
jiggle <- function(x) {
	x = x + rnorm(1, sd=.1)	#agregar un poco de ruido (controlado)
	return(x)
}
# Llamados como cualquier otra función de R
jiggle(5)	# 5±ε. luego de set.seed(2716057), jiggle(5)==5.005043



###########################################################################
# Estructura de datos: Vectores, matrices, marcos da datos y arreglos
###########################################################################

# UNIDIMENSIONAL

# Empecemos desde el principio, y con algo que ya conoces: vectores.
vec <- c(8, 9, 10, 11)
vec	#  8  9 10 11
# Preguntamos por elementos específicos poniendo un subconjunto en corchetes
# (Toma nota de que R empieza los conteos desde 1)
vec[1]		# 8
letters[18]	# "r"
LETTERS[13]	# "M"
month.name[9]	# "September"
c(6, 8, 7, 5, 3, 0, 9)[3]	# 7
# También podes buscar por los índices de componentes específicos,
which(vec %% 2 == 0)	# 1 3
# obtener la primera o las últimas entradas de un vector,
head(vec, 1)	# 8
tail(vec, 2)	# 10 11
# o averiguar si cierto valor se encuentra dentro de un vector
any(vec == 10) # TRUE
# Si un índice "se pasa", obtendrás un NA:
vec[6]	# NA
# Puedes encontrar la longitud de un vector con length()
length(vec)	# 4
# Puedes realizar operaciones con vectores enteros o con subconjuntos de vectores
vec * 4	# 16 20 24 28
vec[2:3] * 5	# 25 30
any(vec[2:3] == 8) # FALSE
# y R tiene muchas funciones pre-definidas para resumir vectores
mean(vec)	# 9.5
var(vec)	# 1.666667
sd(vec)		# 1.290994
max(vec)	# 11
min(vec)	# 8
sum(vec)	# 38
# Otras funciones pre-definidas:
5:15	# 5  6  7  8  9 10 11 12 13 14 15
seq(from=0, to=31337, by=1337)
# =>
#  [1]     0  1337  2674  4011  5348  6685  8022  9359 10696 12033 13370 14707
# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751

# BIDIMENCIONAL (TODO EN UNA CLASE)

# Puedes hacer una matriz de las entradas todos de un mismo tipo como:
mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# A diferencia de un vector, una clase matriz es una 'matriz', 
# sin importar qué contiene
class(mat) # => "matrix"
# Consulta la primera fila
mat[1,]	# 1 4
# Realiza una operación en la primera columna
3 * mat[,1]	# 3 6 9
# Consulta por una celda específica
mat[3,2]	# 6

# Transpone una matriz entera
t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6

# Multiplicación de matrices
mat %*% t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]   17   22   27
# [2,]   22   29   36
# [3,]   27   36   45

# cbind() une vectores como columnas para hacer una matriz
mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
mat2
# =>
#      [,1] [,2]
# [1,] "1"  "dog"
# [2,] "2"  "cat"
# [3,] "3"  "bird"
# [4,] "4"  "dog"
class(mat2)	# matrix
# De nuevo, ten en cuenta lo que sucedió
# Debido a que las matrices deben de contener todas las entradas del mismo tipo,
# todo fue convertido a la clase caracter
c(class(mat2[,1]), class(mat2[,2]))

# rbind() une vectores como filas para hacer una matriz
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    4    5
# [2,]    6    7    0    4
# Ah, todo es de la misma clase. No hay coerciones. Mucho mejor.

# BIDIMENSIONAL (DIFERENTES CLASES)

# Para columnas de tipos diferentes, utiliza un data frame
# Esta estructura de datos es muy útil para programación estadística,
# una versión de ésta fue agregada a Python en el paquete "pandas".

students <- data.frame(c("Cedric","Fred","George","Cho","Draco","Ginny"),
                       c(3,2,2,1,0,-1),
                       c("H", "G", "G", "R", "S", "G"))
names(students) <- c("name", "year", "house") # name the columns
class(students)	# "data.frame"
students
# =>
#     name year house
# 1 Cedric    3     H
# 2   Fred    2     G
# 3 George    2     G
# 4    Cho    1     R
# 5  Draco    0     S
# 6  Ginny   -1     G
class(students$year)	# "numeric"
class(students[,3])	# "factor"
# encontrar las dimensiones
nrow(students)	# 6
ncol(students)	# 3
dim(students)	# 6 3
# La función data.frame() convierte vectores de caracteres en vectores 
# de factores por defecto; deshabilita este atributo
# stringsAsFactors = FALSE cuando vayas a crear el data.frame
?data.frame

# Hay otras formas de hacer subconjuntos de data frames
students$year	# 3  2  2  1  0 -1
students[,2]	# 3  2  2  1  0 -1
students[,"year"]	# 3  2  2  1  0 -1

# Una versión aumentada de la estructura data.frame es el data.table
# Si estás trabajando huge o panel data, o necesitas unificar algunos 
# subconjuntos de datos, data.table puede ser una buena elección.
# Aquí un tour:
install.packages("data.table") # Descarga el paquete de CRAN
require(data.table) # Cárgalo
students <- as.data.table(students)
students # Tomar en cuenta la diferencia de la impresión
# =>
#      name year house
# 1: Cedric    3     H
# 2:   Fred    2     G
# 3: George    2     G
# 4:    Cho    1     R
# 5:  Draco    0     S
# 6:  Ginny   -1     G
students[name=="Ginny"] # obtener filas con name == "Ginny"
# =>
#     name year house
# 1: Ginny   -1     G
students[year==2] # obtener filas con year == 2
# =>
#      name year house
# 1:   Fred    2     G
# 2: George    2     G
# data.table hace que la unificación de dos sets de datos sea fácil
# Hagamos otro data.table para unifiar a los estudiantes
founders <- data.table(house=c("G","H","R","S"),
                       founder=c("Godric","Helga","Rowena","Salazar"))
founders
# =>
#    house founder
# 1:     G  Godric
# 2:     H   Helga
# 3:     R  Rowena
# 4:     S Salazar
setkey(students, house)
setkey(founders, house)
students <- founders[students] # Unifica los dos sets de datos comparando "house"
setnames(students, c("house","houseFounderName","studentName","year"))
students[,order(c("name","year","house","houseFounderName")), with=F]
# =>
#    studentName year house houseFounderName
# 1:        Fred    2     G           Godric
# 2:      George    2     G           Godric
# 3:       Ginny   -1     G           Godric
# 4:      Cedric    3     H            Helga
# 5:         Cho    1     R           Rowena
# 6:       Draco    0     S          Salazar

# data.table hace que sea fácil obtener resúmenes de las tablas
students[,sum(year),by=house]
# =>
#    house V1
# 1:     G  3
# 2:     H  3
# 3:     R  1
# 4:     S  0

# Para eliminar una columna de un data.frame o data.table, 
# asignarle el valor NULL.
students$houseFounderName <- NULL
students
# =>
#    studentName year house
# 1:        Fred    2     G
# 2:      George    2     G
# 3:       Ginny   -1     G
# 4:      Cedric    3     H
# 5:         Cho    1     R
# 6:       Draco    0     S

# Elimina una fila poniendo un subconjunto
# Usando data.table:
students[studentName != "Draco"]
# =>
#    house studentName year
# 1:     G        Fred    2
# 2:     G      George    2
# 3:     G       Ginny   -1
# 4:     H      Cedric    3
# 5:     R         Cho    1
# Usando data.frame:
students <- as.data.frame(students)
students[students$house != "G",]
# =>
#   house houseFounderName studentName year
# 4     H            Helga      Cedric    3
# 5     R           Rowena         Cho    1
# 6     S          Salazar       Draco    0

# MULTI-DIMENSIONAL (TODOS LOS ELEMENTOS DE UN TIPO)

# Arreglos crean una tabla de dimensión n
# Todos los elementos deben de ser del mismo tipo
# Puedes hacer una tabla bi-dimensional (como una matriz)
array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4))
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    8    3
# [2,]    2    5    9    6
# Puedes utilizar un arreglo para hacer una matriz tri-dimensional también
array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2))
# =>
# , , 1
#
#      [,1] [,2]
# [1,]    2    8
# [2,]  300    9
# [3,]    4    0
#
# , , 2
#
#      [,1] [,2]
# [1,]    5   66
# [2,]   60    7
# [3,]    0  847

# LISTAS (MULTI-DIMENSIONAL, POSIBLEMENTE DESIGUALES, DE DIFERENTES TIPOS)

# Finalmente, R tiene listas (de vectores)
list1 <- list(time = 1:40)
list1$price = c(rnorm(40,.5*list1$time,4)) # aleatorio
list1
# Puedes obtener elementos de una lista de la siguiente manera
list1$time # Una manera
list1[["time"]] # Otra manera
list1[[1]] # Y otra manera
# =>
#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# [34] 34 35 36 37 38 39 40
# Puedes crear una lista de subconjuntos como cualquier otro vector
list1$price[4]

# Las listas no son la estructura de datos más eficiente para trabajar en R;
# a menos de que tengas una buena razón, deberías de quedarte con data.frames
# Las listas son usualmente devueltas por funciones que realizan regresiones 
# lineales

##################################################
# La familia de funciones apply()
##################################################

# Te recuerdas de mat?
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# Utiliza apply(X, MARGIN, FUN) paraaplicar una función FUN a la matriz X
# sobre las filas (MAR = 1) o las columnas (MAR = 2)
# Eso es, R aplica FUN sobre cada fila (o columna) de X, mucho más rápido que
# lo que haría un ciclo 'for' o 'loop'
apply(mat, MAR = 2, jiggle)
# =>
#      [,1] [,2]
# [1,]    3   15
# [2,]    7   19
# [3,]   11   23
# Otras funciones: ?lapply, ?sapply

# No te sientas muy intimidado; todos están de acuerdo que son confusas

# El paquete plyr busca reemplazar (y mejorar) la familiar *apply()
install.packages("plyr")
require(plyr)
?plyr



#########################
# Carga de datos
#########################

# "pets.csv" es un archivo en internet
# (pero puede ser tan fácil como tener el archivo en tu computadora)
pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv")
pets
head(pets, 2) # primeras dos filas
tail(pets, 1) # última fila

# Para guardar un data frame o una matriz como un archivo .csv
write.csv(pets, "pets2.csv") # para hacer un nuevo archivo .csv
# definir el directorio de trabajo con setwd(), búscalo con getwd()

# Prueba ?read.csv ?write.csv para más información


#########################
# Gráficas
#########################

# FUNCIONES PREDEFINIDAS DE GRAFICACIÓN
# Gráficos de dispersión!
plot(list1$time, list1$price, main = "fake data")
# Regresiones!
linearModel <- lm(price  ~ time, data = list1)
linearModel # Muestra el resultado de la regresión
# Grafica la línea de regresión
abline(linearModel, col = "red")
# Obtiene una veridad de diagnósticos
plot(linearModel)
# Histogramas!
hist(rpois(n = 10000, lambda = 5), col = "thistle")
# Barras!
barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow"))

# GGPLOT2
# Pero éstas no son las gráficas más bonitas de R
# Prueba el paquete ggplot2 para mayor variedad y mejores gráficas
install.packages("ggplot2")
require(ggplot2)
?ggplot2
pp <- ggplot(students, aes(x=house))
pp + geom_histogram()
ll <- as.data.table(list1)
pp <- ggplot(ll, aes(x=time,price))
pp + geom_point()
# ggplot2 tiene una excelente documentación 
# (disponible en http://docs.ggplot2.org/current/)



```

## ¿Cómo obtengo R?

* Obtén R y R GUI de [http://www.r-project.org/](http://www.r-project.org/)
* [RStudio](http://www.rstudio.com/ide/) es otro GUI
---
language: racket
filename: learnracket-es.rkt
contributors:
  - ["th3rac25", "https://github.com/voila"]
  - ["Eli Barzilay", "https://github.com/elibarzilay"]
  - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
  - ["Duong H. Nguyen", "https://github.com/cmpitg"]
  - ["Keyan Zhang", "https://github.com/keyanzhang"]
translators:
    - ["Carlos Roman", "https://github.com/carlochess"]
lang: es-es
---
Racket es un lenguaje de propósito general, multiparadigma que hace parte de la familia Lisp/Scheme.

```racket
#lang racket ; Define el lenguaje que usas

;;; Comentarios

;; Los comentarios de una sola línea inician con un punto y coma

#| Un bloque de comentarios
   puede distribuirse en varias líneas...
    #|
       ¡Incluso puede estar anidado!
    |#
|#

;; Los comentarios descartan la siguiente expresión,
;; pero son útiles para comentar expresiones al momento de depurar el código
#; (Esta expresión es descartada)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Tipos de datos primitivos y operadores
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Numeros
9999999999999999999999 ; Enteros
#b111                  ; binario => 7
#o111                  ; octal => 73
#x111                  ; hexadecimal => 273
3.14                   ; reales
6.02e+23
1/2                    ; racionaless
1+2i                   ; numeros complejos

;; La aplicación de funciones es escrita de la siguiente forma: (f x y z ...)
;; donde f es una función y “x, y, z” son sus operandos 
;; Si quieres crear una lista de literales debes agregar ' al inicio
;; para que no sean evaluados
'(+ 1 2) ; => (+ 1 2)
;; Ahora algunas operaciones aritméticas
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(expt 2 3) ; => 8
(quotient 5 2) ; => 2
(remainder 5 2) ; => 1
(/ 35 5) ; => 7
(/ 1 3) ; => 1/3
(exact->inexact 1/3) ; => 0.3333333333333333
(+ 1+2i  2-3i) ; => 3-1i

;;; Booleanos
#t ; Para verdadero (true)
#f ; Para falso (false) -- cualquier valor distinto de #f es verdadero
(not #t) ; => #f
(and 0 #f (error "No entra aquí")) ; => #f
(or #f 0 (error "No entra aquí"))  ; => 0

;;; Caracteres
#\A ; => #\A
#\λ ; => #\λ
#\u03BB ; => #\λ

;;; Los Strings tienen una longitud fija
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; backslash es un caracter de escape
"Foo\tbar\41\x21\u0021\a\r\n" ; incluye escape para C, Unicode
"λx:(μα.α→α).xx"              ; Puedes incluir caracteres Unicode

;; ¡Los tipos de dato Strings pueden unirse tambien!
(string-append "Hello " "world!") ; => "Hello world!"

;; Un string puede ser tratado como una lista de caracteres
(string-ref "Apple" 0) ; => #\A

;; la función format puede usarse para darle formato a un string:
(format "~a can be ~a" "strings" "formatted")

;; Imprimir en consola es muy simple
(printf "I'm Racket. Nice to meet you!\n")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Puedes crear una variable usando define
;; el nombre de una variable puede contener cualquier nombre excepto: ()[]{}",'`;#|\
(define some-var 5)
some-var ; => 5

;; También puedes usar caracteres unicode
(define ⊆ subset?)
(⊆ (set 3 2) (set 1 2 3)) ; => #t

;; Acceder a una variable no definida con anterioridad resulta en una excepción
; x ; => x: undefined ...

;; Local binding: La variable 'me' esta limitada a tomar el valor "Bob" dentro del ambiente (let ...)
(let ([me "Bob"])
  "Alice"
  me) ; => "Bob"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Estructuras y colecciones
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Estructuras
(struct dog (name breed age))
(define my-pet
  (dog "lassie" "collie" 5))
my-pet ; => #<dog>
(dog? my-pet) ; => #t
(dog-name my-pet) ; => "lassie"

;;; Parejas (Inmutables)
;; 'cons' construye parejas, 'car' y 'cdr' extraen el primer
;; y segundo elemento respectivamente de una pareja
(cons 1 2) ; => '(1 . 2)
(car (cons 1 2)) ; => 1
(cdr (cons 1 2)) ; => 2

;;; Listas

;; Las Listas son estructuras secuenciales no indexadas, hechas con ‘cons’ y
;; con un 'null' (o '()) para denotar el final de la lista
(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3)
;; 'list' es otro constructor apropiado para las listas
(list 1 2 3) ; => '(1 2 3)
;; y el simbolo comilla (') puede ser usado en una lista de valores literales
'(1 2 3) ; => '(1 2 3)

;; Aquí aun se puede usar 'cons' para agregar un elemento al comienzo de la lista
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; El uso de 'append' para unir un par de listas
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; Las listas son un tipo de dato básico, por lo cual proveen numerosas funcionalidades;
;; algunos ejemplos son:
(map add1 '(1 2 3))          ; => '(2 3 4)
(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33)
(filter even? '(1 2 3 4))    ; => '(2 4)
(count even? '(1 2 3 4))     ; => 2
(take '(1 2 3 4) 2)          ; => '(1 2)
(drop '(1 2 3 4) 2)          ; => '(3 4)

;;; Vectores

;; Los Vectores son arreglos de longitud fija
#(1 2 3) ; => '#(1 2 3)

;; Se usa 'vector-append' para unir dos vectores
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; Conjuntos

;; Crear un conjunto a partir de una lista
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)

;; Agregar/Asignar un nuevo elemento 'set-add'
;; (Funcional: retorna un conjunto extendido en vez de una mutar la entrada)
(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)

;; Remueve el elemento agregado anteriormente 'set-remove'
(set-remove (set 1 2 3) 1) ; => (set 2 3)

;; Prueba la existencia de un elemento con la funcion 'set-member?'
(set-member? (set 1 2 3) 1) ; => #t
(set-member? (set 1 2 3) 4) ; => #f

;;; Tablas Hashs

;; Crea una tabla hash inmutable (Abajo presentamos un ejemplo)
(define m (hash 'a 1 'b 2 'c 3))

;; Conseguir un valor
(hash-ref m 'a) ; => 1

;; Conseguir un valor que no está presente es una excepción
; (hash-ref m 'd) => no value found

;; Puedes proveer un valor por defecto si el valor para la llave no se encuentra
(hash-ref m 'd 0) ; => 0

;; Usa 'hash-set' para ampliar un tabla hash “inmutable”
;; (Retorna la tabla hash extendida en vez de una mutarla)
(define m2 (hash-set m 'd 4))
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))

;; ¡Recuerde que estas tablas hash son inmutables!
m ; => '#hash((b . 2) (a . 1) (c . 3))  <-- no 'd'

;; Usa 'hash-remove' para quitar las llaves de la tabla hash (functional tambien)
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Funciones
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Usa 'lambda' para crear funciones.
;; Una función siempre retorna el valor de su última expresión
(lambda () "Hello World") ; => #<procedure>
;; También se puede usar el caracter Unicode 'λ'
(λ () "Hello World")     ; => same function

;; Usa los paréntesis exteriores para llamar la función
((lambda () "Hello World")) ; => "Hello World"
((λ () "Hello World"))      ; => "Hello World"

;; Asigna una función a una variable
(define hello-world (lambda () "Hello World"))
(hello-world) ; => "Hello World"

;; Puede acortar esto usando el azúcar sintáctico para la definición de una función:
(define (hello-world2) "Hello World")

;; El paréntesis () del ejemplo anterior denota la lista de argumentos para la función
(define hello
  (lambda (name)
    (string-append "Hello " name)))
(hello "Steve") ; => "Hello Steve"
;; ... O de forma similar, usando el azúcar sintáctico para una definición:
(define (hello2 name)
  (string-append "Hello " name))

;; Puedes tener una función con parametros variables, using 'case-lambda'
(define hello3
  (case-lambda
    [() "Hello World"]
    [(name) (string-append "Hello " name)]))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"
;; ... o especificar los argumentos opcionales junto con su valor por defecto
(define (hello4 [name "World"])
  (string-append "Hello " name))

;; Las funciones pueden tener argumentos extra empaquetados como una lista
(define (count-args . args)
  (format "You passed ~a args: ~a" (length args) args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
;; ... o sin usar el azúcar sintáctico:
(define count-args2
  (lambda args
    (format "You passed ~a args: ~a" (length args) args)))

;; Puedes combinar argumentos regulares y empaquetados
(define (hello-count name . args)
  (format "Hello ~a, you passed ~a extra args" name (length args)))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"
;; ... Sin usar azúcar sintáctica:
(define hello-count2
  (lambda (name . args)
    (format "Hello ~a, you passed ~a extra args" name (length args))))

;; Y con keywords
(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
  (format "~a ~a, ~a extra args" g name (length args)))
(hello-k)                 ; => "Hello World, 0 extra args"
(hello-k 1 2 3)           ; => "Hello World, 3 extra args"
(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
                                         ; => "Hi Finn, 6 extra args"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Comparando
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Para números usa '='
(= 3 3.0) ; => #t
(= 2 1)   ; => #f

;; 'eq?' retorna #t si 2 argumentos refieren al mismo objeto en memoria 
;; #f de lo contrario.
;; En otras palabras, es una simple comparación de punteros.
(eq? '() '()) ; => #t, Debido a que existe solo una lista vacia en memoria
(let ([x '()] [y '()])
  (eq? x y))  ; => #t, igual que arriba

(eq? (list 3) (list 3)) ; => #f
(let ([x (list 3)] [y (list 3)])
  (eq? x y))            ; => #f — ¡No es la misma lista en memoria!

(let* ([x (list 3)] [y x])
  (eq? x y)) ; => #t, debido a que ‘x’ y ‘y’ ahora apuntan a la misma posición en memoria

(eq? 'yes 'yes) ; => #t
(eq? 'yes 'no)  ; => #f

(eq? 3 3)   ; => #t — Te cuidado aqui
            ; es mejor usar '=' para comparacion de numeros.
(eq? 3 3.0) ; => #f

(eq? (expt 2 100) (expt 2 100))               ; => #f
(eq? (integer->char 955) (integer->char 955)) ; => #f

(eq? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f

;; 'eqv?' permite comparar números y caracteres..
;; for other datatypes, 'eqv?' and 'eq?' return the same result.
(eqv? 3 3.0)                                   ; => #f
(eqv? (expt 2 100) (expt 2 100))               ; => #t
(eqv? (integer->char 955) (integer->char 955)) ; => #t

(eqv? (string-append "foo" "bar") (string-append "foo" "bar"))   ; => #f

;; 'equal?' permite comparar los siguientes tipos de datos:
;; strings, byte strings, pairs, mutable pairs, vectors, boxes, 
;; hash tables, and inspectable estructuras.
;; para otros tipos de datos, 'equal?' y 'eqv?' devuelven el mismo resultado.
(equal? 3 3.0)                                                   ; => #f
(equal? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #t
(equal? (list 3) (list 3))                                       ; => #t

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Control de flujo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Condicionales

(if #t               ; expresión de prueba
    "this is true"   ; expresión si la expresión de prueba es verdadera
    "this is false") ; de lo contrario expression
; => "this is true"

;; En condicionales, todos los valores que no son #f son tratados como verdadero
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'yep

;; Las expresiones 'cond' son una serie de pruebas para seleccionar el resultado
(cond [(> 2 2) (error "wrong!")]
      [(< 2 2) (error "wrong again!")]
      [else 'ok]) ; => 'ok

;;; Coincidencia de patrones (Pattern Matching)

(define (fizzbuzz? n)
  (match (list (remainder n 3) (remainder n 5))
    [(list 0 0) 'fizzbuzz]
    [(list 0 _) 'fizz]
    [(list _ 0) 'buzz]
    [_          #f]))

(fizzbuzz? 15) ; => 'fizzbuzz
(fizzbuzz? 37) ; => #f

;;; Ciclos

;; Los ciclos pueden expresarse a través de recursión (de cola)
(define (loop i)
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i))))
(loop 5) ; => i=5, i=6, ...

;; De igual forma, con un let
(let loop ((i 0))
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i)))) ; => i=0, i=1, ...

;; El siguiente ejemplo muestra cómo expresar un ciclo for, pero Racket tiene
;; otra forma aún más flexible de expresarlos:
(for ([i 10])
  (printf "i=~a\n" i)) ; => i=0, i=1, ...
(for ([i (in-range 5 10)])
  (printf "i=~a\n" i)) ; => i=5, i=6, ...

;;; Iterando sobre otras secuencias
;; 'for' permite iterar sobre varios tipos de secuencias:
;; lists, vectors, strings, sets, hash tables, etc...

(for ([i (in-list '(l i s t))])
  (displayln i))

(for ([i (in-vector #(v e c t o r))])
  (displayln i))

(for ([i (in-string "string")])
  (displayln i))

(for ([i (in-set (set 'x 'y 'z))])
  (displayln i))

(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
  (printf "key:~a value:~a\n" k v))

;;; Iteradores mas sofisticados

;; Escaneo paralelo de múltiples secuencias (se detiene en la más pequeña)
(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x 1:y 2:z

;; Loops anidados
(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z

;; Condicionales
(for ([i 1000]
      #:when (> i 5)
      #:unless (odd? i)
      #:break (> i 10))
  (printf "i=~a\n" i))
; => i=6, i=8, i=10

;;; Secuncias por compresión
;; Muy similar a los ciclos 'for' -- solo recolectando los resultados

(for/list ([i '(1 2 3)])
  (add1 i)) ; => '(2 3 4)

(for/list ([i '(1 2 3)] #:when (even? i))
  i) ; => '(2)

(for/list ([i 10] [j '(x y z)])
  (list i j)) ; => '((0 x) (1 y) (2 z))

(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10))
  i) ; => '(6 8 10)

(for/hash ([i '(1 2 3)])
  (values i (number->string i)))
; => '#hash((1 . "1") (2 . "2") (3 . "3"))

;; Existen otras formas de recolectar los valores usando otras expresiones:
(for/sum ([i 10]) (* i i)) ; => 285
(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000
(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t
(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t
;; Y para usar cualquier combinación arbitraria, use 'for/fold'
(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10
;; (Esto frecuentemente reemplaza los ciclos en los lenguajes imperativos)

;;; Excepciones

;; Para atrapar excepciones, usa las funciones 'with-handlers'
(with-handlers ([exn:fail? (lambda (exn) 999)])
  (+ 1 "2")) ; => 999
(with-handlers ([exn:break? (lambda (exn) "no time")])
  (sleep 3)
  "phew") ; => "phew", pero si usa un break => "no time"

;; Usa 'raise' para lanzar una excepción o cualquier otro valor
(with-handlers ([number?    ; atrapa valores numericos lanzados
                 identity]) ; los retorna como valores
  (+ 1 (raise 2))) ; => 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Mutación
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Usa 'set!' para asignar un nuevo valor a una variable existente
(define n 5)
(set! n (add1 n))
n ; => 6

;; Usa boxes para valores explícitamente mutables (similar a punteros o
;; referencias en otros lenguajes)
(define n* (box 5))
(set-box! n* (add1 (unbox n*)))
(unbox n*) ; => 6

;; Muchos tipos de datos en Racket son inmutables (pairs, lists, etc), algunos poseen
;; ambos sabores mutable e immutable (strings, vectors, hash tables,
;; etc...)

;; Usa 'vector' o 'make-vector' para crear vectores mutables
(define vec (vector 2 2 3 4))
(define wall (make-vector 100 'bottle-of-beer))
;; Usa vector-set! para actualizar una posición
(vector-set! vec 0 1)
(vector-set! wall 99 'down)
vec ; => #(1 2 3 4)

;; Crea una tabla hash vacía  y manipulata
(define m3 (make-hash))
(hash-set! m3 'a 1)
(hash-set! m3 'b 2)
(hash-set! m3 'c 3)
(hash-ref m3 'a)   ; => 1
(hash-ref m3 'd 0) ; => 0
(hash-remove! m3 'a)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Modulos
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Los Modulos permiten organizar el código en multiples archivos para reusarlos
;; en bibliotecas; Aquí usamos sub-modules, anidados en todo el modulo que
;; este texto hace (empezando desde la línea "#lang")

(module cake racket/base ; definimos un modulo llamado 'cake' basado en racket/base

  (provide print-cake) ; function exportada por el modulo

  (define (print-cake n)
    (show "   ~a   " n #\.)
    (show " .-~a-. " n #\|)
    (show " | ~a | " n #\space)
    (show "---~a---" n #\-))

  (define (show fmt n ch) ; función interna 
    (printf fmt (make-string n ch))
    (newline)))

;; Usa 'require' para obtener todos los nombre que provee un modulo
(require 'cake) ; el apostrofe ' indica que es un submódulo local
(print-cake 3)
; (show "~a" 1 #\A) ; => error, la función 'show' no fue exportada

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. Clases y objectos
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Crea una clase llamada fish% (-% Es un una forma de indicar los límites de la clase)
(define fish%
  (class object%
    (init size) ; inicialización del argumento
    (super-new) ; inicialización de la superclase
    ;; Campo
    (define current-size size)
    ;; Metodos públicos
    (define/public (get-size)
      current-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

;; Crea una instancia de la clase fish%
(define charlie
  (new fish% [size 10]))

;; Usa 'send' para llamar un método de un objeto
(send charlie get-size) ; => 10
(send charlie grow 6)
(send charlie get-size) ; => 16

;; 'fish%' is a plain "first class" value, which can get us mixins
(define (add-color c%)
  (class c%
    (init color)
    (super-new)
    (define my-color color)
    (define/public (get-color) my-color)))
(define colored-fish% (add-color fish%))
(define charlie2 (new colored-fish% [size 10] [color 'red]))
(send charlie2 get-color)
;; o, sin nombres:
(send (new (add-color fish%) [size 10] [color 'red]) get-color)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 9. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Las Macros te permite extender la sintaxis del lenguaje

;; Agreguemos un ciclo while
(define-syntax-rule (while condition body ...)
  (let loop ()
    (when condition
      body ...
      (loop))))

(let ([i 0])
  (while (< i  10)
    (displayln i)
    (set! i (add1 i))))

;; Las Macros son higienicas, ¡no puedes aplastar las variables existentes!
(define-syntax-rule (swap! x y) ; -! es un caracter que indica mutación
  (let ([tmp x])
    (set! x y)
    (set! y tmp)))

(define tmp 2)
(define other 3)
(swap! tmp other)
(printf "tmp = ~a; other = ~a\n" tmp other)
;; La variable 'tmp' es renombrada a 'tmp_1'
;; Para evitar el conflicto de nombres
;; (let ([tmp_1 tmp])
;;   (set! tmp other)
;;   (set! other tmp_1))

;; Pero aun hay algunas transfromaciones de código, por ejemplo:
(define-syntax-rule (bad-while condition body ...)
  (when condition
    body ...
    (bad-while condition body ...)))
;; Esta macro es incorrecta: genera código infinitamente, si tratas de usarla
;; el compilador entrará en un ciclo infinito

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 10. Contratos
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Los Contratos imponen restricciones a los valores exportados desde los módulos

(module bank-account racket
  (provide (contract-out
            [deposit (-> positive? any)] ; Los montos siempre son positivos
            [balance (-> positive?)]))

  (define amount 0)
  (define (deposit a) (set! amount (+ amount a)))
  (define (balance) amount)
  )

(require 'bank-account)
(deposit 5)

(balance) ; => 5

;; El cliente intenta depositar un monto negativo por lo cual es rechazado
;; (deposit -5) ; => depósito: violación del contrato
;; expected: positive?
;; given: -5
;; more details....

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 11. Entrada y salida
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Racket tiene el concepto de "port", el cual es muy similar al de descriptores
;; de ficheros en otros lenguajes

;; Abre "/tmp/tmp.txt" y escribe "Hello World"
;; Esto lanzará un error si el archivo existe
(define out-port (open-output-file "/tmp/tmp.txt"))
(displayln "Hello World" out-port)
(close-output-port out-port)

;; Agregar información a "/tmp/tmp.txt" (incluso si el archivo existe)
(define out-port (open-output-file "/tmp/tmp.txt"
                                   #:exists 'append))
(displayln "Hola mundo" out-port)
(close-output-port out-port)

;; Lee del archivo de nuevo
(define in-port (open-input-file "/tmp/tmp.txt"))
(displayln (read-line in-port))
; => "Hello World"
(displayln (read-line in-port))
; => "Hola mundo"
(close-input-port in-port)

;; Alternativamente, haciendo uso de call-with-output-file no necesitas expresamente
;; cerrar el archivo
(call-with-output-file "/tmp/tmp.txt"
  #:exists 'update ; Rewrite the content
  (λ (out-port)
    (displayln "World Hello!" out-port)))

;; Y usar la función call-with-input-file hace lo mismo para la entrada
(call-with-input-file "/tmp/tmp.txt"
  (λ (in-port)
    (displayln (read-line in-port))))
```

## Mas información

¿Quieres saber mas? Prueba en [Empezando con Racket](http://docs.racket-lang.org/getting-started/)




---
category: tool
tool: ruby ecosystem
contributors:
    - ["Jon Smock", "http://github.com/jonsmock"]
    - ["Rafal Chmiel", "http://github.com/rafalchmiel"]
translators:
    - ["Ale Mohamad", "http://twitter.com/alemohamad"]
lang: es-es
---

Las personas que usan Ruby en general tienen una tendencia a instalar diferentes
versiones de Ruby, administrar sus paquetes (o gemas), y gestionar las
dependencias de sus gemas.

## Gestores de Ruby

Algunas plataformas ya tienen Ruby pre-instalado o disponible como un paquete
propio. Muchos rubystas no utilizan estas versiones, o si lo hacen, solo lo
utilizan para preparar otra instalación o implementación de Ruby. En lugar de
eso, los rubystas tienden a instalar un gestor de Ruby para poder instalar
diferentes versiones y poder cambiar dependiendo del entorno de cada proyecto.

Los siguientes son gestores populares de entorno de Ruby:

* [RVM](https://rvm.io/) - Instala y cambia versiones de Ruby. Además RVM tiene
  el concepto de gemsets para aislar complemtante entornos de proyectos.
* [ruby-build](https://github.com/sstephenson/ruby-build) - Solo instala
  versiones de Ruby. Se utiliza para tener un control más fino sobre las
  versiones instaladas de Ruby.
* [rbenv](https://github.com/sstephenson/rbenv) - Solo se utiliza para cambiar
  la versión de Ruby. Se utiliza junto con ruby-build. Se utiliza para tener un
  control más fino sobre cómo se carga Ruby en el sistema.
* [chruby](https://github.com/postmodern/chruby) - Solo se utiliza para cambiar
  la versión de Ruby. En espíritu es similar a rbenv. No le es tan importante
  como son instaladas las versiones de Ruby.

## Versiones de Ruby

Ruby fue creado por Yukihiro "Matz" Matsumoto, quien se mantiene como una
especie de [BDFL](https://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life),
aunque recientemente está cambiando. Como resultado, la implementación de
referencia de Ruby es llamada MRI (Matz' Reference Implementation), y cuando se
habla de una versión de Ruby, se está haciendo referencia a la versión inicial
de MRI.

Las tres versiones mayores en uso de Ruby son:

* 2.0.0 - Lanzada en Febrero de 2013. La mayoría de las librerías importantes y
  frameworks soportan 2.0.0.
* 1.9.3 - Lanzada en Octubre de 2011. Es la versión que actualmente usan más
  rubystas. Además fue
  [retirada](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/)
* 1.8.7 - Ruby 1.8.7 fue
  [retirada](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/).

El cambio de 1.8.7 a 1.9.x es un cambio mucho mayor que de 1.9.3 a 2.0.0. Por
ejemplo, la serie 1.9 presentó codificaciones (encodings) y un bytecode VM.
Todavía hay proyectos que utilizan 1.8.7, pero se están convirtiendo en una
pequeña minoría, debido a que la mayor parte de la comunidad está utilizando
como mínimo 1.9.2 o 1.9.3.

## Implementaciones de Ruby

El ecosistema de Ruby disfruta de muchas diferentes implementaciones de Ruby,
cada una con fortalezas únicas y estados de compatibilidad. Para ser claros, las
diferentes implementaciones están escritas en diferentes lenguajes, pero *todas
son Ruby*. Cada implementación tiene hooks especiales y características extra,
pero todas interpretan archivos Ruby de forma normal. Por ejemplo, JRuby está
escrito en Java, pero no necesitás saber de Java para poder utilizarla.

Muy maduras/compatibles:

* [MRI](https://github.com/ruby/ruby) - Escrita en C, es la implementación de
  referencia de Ruby. Por definición es 100% compatible (consigo misma). Las
  otras implementaciones de Ruby mantienen compatibilidad con MRI (ver
  [RubySpec](#rubyspec) más abajo).
* [JRuby](http://jruby.org/) - Escrita en Java y Ruby, esta implementación es
  robusta y bastante veloz. Más importante, la fortaleza de JRuby reside en la
  interoperabilidad con JVM/Java, pudiendo utilizar herramientas, proyectos y
  lenguajes ya existentes en JVM.
* [Rubinius](http://rubini.us/) - Escrita principalmente en Ruby junto con un
  bytecode VM de C++. Además es bastante madura y veloz. Debido a que está
  implementada de forma directa en Ruby, expone varias funcionalidades de VM en
  rubyland.

Medianamente maduras/compatibles:

* [Maglev](http://maglev.github.io/) - Construida sobre Gemstone, una VM de
  Smalltalk. Smalltalk tiene herramientas que son impresionantes, y este
  proyecto intenta llevar eso dentro del desarrollo con Ruby.
* [RubyMotion](http://www.rubymotion.com/) - Lleva Ruby al desarrollo en iOS.

No tan maduras/compatibles:

* [Topaz](http://topazruby.com/) - Escrito en RPython (usando el intérprete
  PyPy), Topaz es bastante joven y no tan compatible. Se muestra prometedor para
  ser una implementación de Ruby de alta performance.
* [IronRuby](http://ironruby.net/) - Escrita en C#, apuntando a la plataforma
  .NET. El trabajo en IronRuby parece haberse detenido desde que Microsoft
  retiró su soporte.

Las implementaciones de Ruby pueden tener su propio número de versión de
release, pero siempre apuntan a una versión específica de MRI para poder tener
compatibilidad. Muchas implementaciones tienen la habilidad de trabajar en
diferentes modos (por ejemplo, modo 1.8 o 1.9) para especificar a qué versión de
MRI están apuntando.

## RubySpec

Muchas implementaciones de Ruby dependen en gran medida de
[RubySpec](http://rubyspec.org/). Ruby no tiene una especificación oficial, por
lo que la comunidad ha escrito especificaciones ejecutables en Ruby para poder
testear la compatibilidad de sus implementaciones con MRI.

## RubyGems

[RubyGems](http://rubygems.org/) es un manejador de paquetes/comunidad de Ruby.
RubyGems viene incluido con Ruby, por lo que no hay necesidad de instalarlo por
separado.

Los paquetes de Ruby son llamados "gemas" ("gems"), y pueden ser alojados por la
comunidad en RubyGems.org. Cada gema contiene su código fuente y algo de
metadata, incluyendo cosas como la versión, dependencias, autor(es), y
licencia(s).

## Bundler

[Bundler](http://bundler.io/) es una herramienta para resolución de dependencias
de gemas. Utiliza un archivo llamado Gemfile en cada proyecto para poder
organizar sus dependencias, y luego poder agregar dichas dependencias y sus
dependencias de forma recursiva. Hace esta acción hasta que resuelve y descarga
todas las dependencias, o se detiene si es que un conflicto aparece.

Bundler eleva un error si encuentra dependencias conflictivas. Por ejemplo, si
la gema A requiere la versión 3 o mayor de la gema Z, pero la gema B requiere la
versión 2, Bundler va a notificarte sobre dicho conflicto. Esto es
extremadamente útil ya que varias gemas hacen referencia a otras gemas (de las
cuales puede referenciar a otras gemas), lo cual puede formar un gran grafo de
dependencias para resolver.

# Testing

Testing es una parte grande dentro de la cultura de Ruby. Ruby incluye su propio
framework de testing llamado minitest (o TestUnit para la versión 1.8.x de
Ruby). Hay varias librerías de testing con diferentes objetivos.

* [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html) - Framework de testing de Ruby 1.8
* [minitest](http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html) - Framework de testing de Ruby 1.9/2.0
* [RSpec](http://rspec.info/) - Un framework de testing que se focaliza en expresividad
* [Cucumber](http://cukes.info/) - Un framework de testing que utiliza BDD, que parsea tests con formato Gherkin

## Se Amable

La comunidad de Ruby se enorgullece de ser una comunidad abierta, diversa y
acogedora. Matz mismo es extremadamente amigable, y en general la generosidad de
los rubystas es increíble.
---
language: ruby
filename: learnruby-es.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
  - ["Gabriel Halley", "https://github.com/ghalley"]
  - ["Persa Zula", "http://persazula.com"]
translators:
    - ["Camilo Garrido", "http://www.twitter.com/hirohope"]
    - ["Erick Bernal", "http://www.twitter.com/billowkib"]
lang: es-es
---

```ruby
# Esto es un comentario

=begin
Este es un comentario multilínea
Nadie los usa.
Tu tampoco deberías
=end

# En primer lugar: Todo es un objeto

# Los números son objetos

3.class #=> Fixnum

3.to_s #=> "3"


# Un poco de aritmética básica
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
5 % 3 #=> 2

# La aritmética es sólo azúcar sintáctico
# para llamar un método de un objeto
1.+(3) #=> 4
10.* 5 #=> 50

# Los valores especiales son objetos
nil # Nada que ver aqui
true # Verdadero
false # Falso

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Igualdad
1 == 1 #=> true
2 == 1 #=> false

# Desigualdad
1 != 1 #=> false
2 != 1 #=> true

# Además de 'false', 'nil' es otro valor falso

!nil   #=> true
!false #=> true
!0     #=> false

# Más comparaciones
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Operadores lógicos
true && false #=> false
true || false #=> true
!true #=> false

# Existen versiones alternativas de los operadores lógicos con menor prioridad
# Estos son usados como constructores controladores de flujo que encadenan
# sentencias hasta que una de ellas retorne verdadero o falso

# `has_otra_cosa` solo se llama si `has_algo` retorna verdadero.
has_algo() and has_otra_cosa()
# `registra_error` solo se llama si `has_algo` falla
has_algo() or registra_error()


# Los strings son objetos

'Soy un string'.class #=> String
"Soy un string también".class #=> String

referente = "usar interpolación de strings"
"Yo puedo #{referente} usando strings de comillas dobles"
#=> "Yo puedo usar interpolación de strings usando strings de comillas dobles"


# Imprime a la salida estándar
puts "¡Estoy imprimiendo!"

# Variables
x = 25 #=> 25
x #=> 25

# Nota que la asignación retorna el valor asignado
# Esto significa que puedes hacer múltiples asignaciones:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Por convención, usa snake_case para nombres de variables
snake_case = true

# Usa nombres de variables descriptivos
ruta_para_la_raiz_de_un_projecto = '/buen/nombre/'
ruta = '/mal/nombre/'

# Los símbolos (son objetos)
# Los símbolos son inmutables, constantes reusables representadas internamente por un
# valor entero. Son normalmente usados en vez de strings para expresar eficientemente
# valores específicos y significativos

:pendiente.class #=> Symbol

status = :pendiente

status == :pendiente #=> true

status == 'pendiente' #=> false

status == :aprovado #=> false

# Arreglos

# Esto es un arreglo
arreglo = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Arreglos pueden contener elementos de distintos tipos

[1, "hola", false] #=> => [1, "hola", false]

# Arreglos pueden ser indexados
# Desde el frente
arreglo[0] #=> 1
arreglo.first #=> 1
arreglo[12] #=> nil

# Al igual que en aritmética, el acceso como variable[índice]
# es sólo azúcar sintáctica
# para llamar el método [] de un objeto
arreglo.[] 0 #=> 1
arreglo.[] 12 #=> nil

# Desde el final
arreglo[-1] #=> 5
arreglo.last #=> 5

# Con un índice de inicio y longitud
arreglo[2, 3] #=> [3, 4, 5]

# Invertir un arreglo
a = [1, 2, 3]
a.reverse! #=> [3, 2, 1]

# O con rango
arreglo[1..3] #=> [2, 3, 4]

# Añade elementos a un arreglo así
arreglo << 6 #=> [1, 2, 3, 4, 5, 6]
# O así
arreglo.push(6) #=> [1, 2, 3, 4, 5, 6]

#Verifica si un elemento ya existe en ese arreglo
arreglo.include?(1) #=> true

# Hashes son los diccionarios principales de Ruby con pares llave/valor.
# Hashes se denotan con llaves:
hash = {'color' => 'verde', 'numero' => 5}

hash.keys #=> ['color', 'numero']

# Hashes pueden buscar rápidamente una llave:
hash['color'] #=> 'verde'
hash['numero'] #=> 5

# Preguntarle a un hash por una llave que no existe retorna 'nil':
hash['nada aqui'] #=> nil

# Desde Ruby 1.9, hay una sintaxis especial cuando se usa un símbolo como llave:

nuevo_hash = { defcon: 3, accion: true}

nuevo_hash.keys #=> [:defcon, :accion]

# Verifica la existencia de llaves y valores en el hash
new_hash.has_key?(:defcon) #=> true
new_hash.has_value?(3) #=> true

# Tip: Tanto los arreglos como los hashes son Enumerable (enumerables)
# Comparten muchos métodos útiles tales como 'each', 'map', 'count', y más

# Estructuras de Control

if true
  "declaracion 'if'"
elsif false
 "else if, opcional"
else
 "else, tambien opcional"
end

for contador in 1..5
  puts "iteracion #{contador}"
end
#=> iteracion 1
#=> iteracion 2
#=> iteracion 3
#=> iteracion 4
#=> iteracion 5

# SIN EMBARGO, nadie usa ciclos `for`
# En su lugar debes usar el método "each" y pasarle un block (bloque).
# Un bloque es un fragmento código que puedes pasar a métodos como `each`.
# Es símilar a las funciones lambda, funciones anónimas o `closures` en otros
# lenguajes de programación.
#
# El método `each` de un Range (rango) ejecuta el bloque una vez por cada elemento.
# Al bloque se le pasa un contador como parametro.
# Usar el método `each` con un bloque se ve así:

(1..5).each do |contador|
  puts "iteracion #{contador}"
end
#=> iteracion 1
#=> iteracion 2
#=> iteracion 3
#=> iteracion 4
#=> iteracion 5

# También puedes envolver el bloque entre llaves:
(1..5).each { |counter| puts "iteración #{contador}" }

#El contenido de las estructuras de datos en ruby puede ser iterado usando `each`.
arreglo.each do |elemento|
  puts "#{elemento} es parte del arreglo"
end
hash.each do |llave, valor|
  puts "#{llave} es #{valor}"
end

# Si aún necesitas un índice puedes usar "each_with_index" y definir una variable
# índice.
arreglo.each_with_index do |element, index|
  puts "#{element} tiene la posición #{index} en el arreglo"
end

contador = 1
while contador <= 5 do
  puts "iteracion #{contador}"
  contador += 1
end
#=> iteracion 1
#=> iteracion 2
#=> iteracion 3
#=> iteracion 4
#=> iteracion 5

# Hay una gran variedad de otras funciones iterativas útiles en Ruby,
# por ejemplo `map`, `reduce`,  `inject`, entre otras. Map, por ejemplo,
# toma el arreglo sobre el cuál está iterando, le hace cambios
# definidos en el bloque, y retorna un arreglo completamente nuevo.
arreglo = [1,2,3,4,5]
duplicado = array.map do |elemento|
  elemento * 2
end
puts duplicado
#=> [2,4,6,8,10]
puts array
#=> [1,2,3,4,5]

nota = 'B'

case nota
when 'A'
  puts "Muy bien muchacho"
when 'B'
  puts "Mejor suerte para la proxima"
when 'C'
  puts "Puedes hacerlo mejor"
when 'D'
  puts "Sobreviviendo"
when 'F'
  puts "¡Reprobaste!"
else
  puts "Sistema alternativo de notas, ¿eh?"
end
#=> "Mejor suerte para la proxima"

# Los casos también pueden usar rangos
nota = 82

case nota
when 90..100
  puts 'Excelente!'
when 80..100
  puts 'Buen trabajo'
else
  puts '¡Reprobaste!'
end
#=> "Buen trabajo"

# Manejo de excepciones
begin
  # código que podría causar excepción
  raise NoMemoryError, 'Se te acabó la memoria'
rescue NoMemoryError => variable_de_excepcion
  puts 'El error NoMemoryError ocurrió', variable_de_excepcion
rescue RuntimeError => otra_variable_de_excepcion
  puts 'El error RuntimeError ocurrió'
else
  puts 'Esto se ejecuta si ningun error ocurrió'
ensure
  puts 'Este código siempre se ejecuta, sin importar que'
end

# Funciones

def doble(x)
  x * 2
end

# Funciones (y todos los bloques) implícitamente retornan el valor de la última instrucción
doble(2) #=> 4

# Paréntesis son opcionales cuando el resultado no es ambiguo
doble 3 #=> 6

doble doble 3 #=> 12

def suma(x,y)
  x + y
end

# Arguméntos del método son separados por coma
suma 3, 4 #=> 7

suma suma(3,4), 5 #=> 12

# yield
# Todos los métodos tienen un parámetro bloque opcional e implícito
# puede llamarse con la palabra clave 'yield'

def alrededor
  puts "{"
  yield
  puts "}"
end

alrededor { puts 'hola mundo' }

# {
# hola mundo
# }

# Puedes pasar un bloque a una función
# '&' representa una referencia a un bloque
def visitantes(&bloque)
  bloque.call
end

# Puedes pasar una lista de argumentos, que serán convertidos en un arreglo
# Para eso sirve el operador ('*')
def visitantes(*arreglo)
  arreglo.each { |visitante| puts visitante }
end

# Define una clase con la palabra clave 'class'
class Humano

  # Una variable de clase. Es compartida por todas las instancias de la clase.
  @@species = "H. sapiens"

  # Inicializador Básico
  def initialize(nombre, edad=0)
    # Asigna el argumento a la variable de instancia 'nombre'
    @nombre = nombre
    # Si no dan edad, se usará el valor por defecto en la lista de argumentos.
    @edad = edad
  end

  # Método 'setter' (establecer) básico
  def nombre=(nombre)
    @nombre = nombre
  end

  # Método 'getter' (obtener) básico
  def nombre
    @nombre
  end

  # La funcionalidad anterior puede ser encapsulada usando el método attr_accessor
  # de la siguiente manera

  attr_accessor :name

  # Los métodos de tipo getter y setter también se pueden crear de manera individual
  # de la siguiente manera

  attr_reader :name
  attr_writer :name

  # Un método de clase usa 'self' (sí mismo) para distinguirse de métodos de instancia.
  # Sólo puede ser llamado en la clase, no por una instancia.
  def self.decir(mensaje)
    puts mensaje
  end

  def especie
    @@especie
  end
end


# Instancia una clase
jim = Humano.new("Jim Halpert")

dwight = Humano.new("Dwight K. Schrute")

# Llamemos un par de métodos
jim.especie #=> "H. sapiens"
jim.nombre #=> "Jim Halpert"
jim.nombre = "Jim Halpert II" #=> "Jim Halpert II"
jim.nombre #=> "Jim Halpert II"
dwight.especie #=> "H. sapiens"
dwight.nombre #=> "Dwight K. Schrute"

# Llama el método de clase
Humano.decir("Hi") #=> "Hi"

# El alcance de las variables es definido por la manera en que las nombramos.
# Las variables que inician con $ tienen un alcance global
$var = "Soy una variable global"
defined? $var #=> "global-variable"

# Las variables que empiezan con @ tienen un alcance de instancia
@var = "Soy una variable de instancia"
defined? @var #=> "instance-variable"

# Variables que empiezan con @@ tienen un alcance de clase
@@var = "Soy una variable de clase"
defined? @@var #=> "class variable"

# Las variables que empiezan con letra mayuscula son constantes
Var = "Soy una constante"
defined? Var #=> "constant"

# Las clases también son un objeto en ruby. Por lo cual, las clases también pueden tener variables de instancia.
# Variables de clase son compartidas a través de la clase y todos sus descendientes.

# clase base
class Humano
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(valor)
    @@foo = valor
  end
end

# clase derivada
class Trabajador < Humano
end

Humano.foo # 0
Trabajador.foo # 0

Humano.foo = 2 # 2
Trabajador.foo # 2

# Las variables de instancia de la clase no son compartidas por los descendientes de la clase.

class Humano
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(valor)
    @bar = valor
  end
end

class Doctor < Humano
end

Humano.bar # 0
Doctor.bar # nil

module ModuloEjemplo
  def foo
    'foo'
  end
end

# Al incluir un módulo sus métodos se comparten con las instancias de la clase
# Al extender un módulo sus métodos se comparten con la clase misma

class Persona
  include ModuloEjemplo
end

class Libro
  extend ModuloEjemplo
end

Persona.foo     # => NoMethodError: undefined method `foo' for Persona:Class
Persona.new.foo # => 'foo'
Libro.foo       # => 'foo'
Libro.new.foo   # => NoMethodError: undefined method `foo'

# Las llamadas de retorno (callbacks) son ejecutadas cuando se incluye o
# extiende un módulo
module EjemploConcern
  def self.incluido(base)
    base.extend(MetodosClase)
    base.send(:include, MetodosInstancia)
  end

  module MetodosClase
    def bar
      'bar'
    end
  end

  module MetodosInstancia
    def qux
      'qux'
    end
  end
end

class Algo
  include EjemploConcern
end

Algo.bar #=> 'bar'
Algo.qux #=> NoMethodError: undefined method `qux'
Algo.new.bar # => NoMethodError: undefined method `bar'
Algo.new.qux # => 'qux'
```

## Recursos adicionales
- [Aprende Ruby Mediante Ejemplo con Ejercicios](http://www.learneroo.com/modules/61/nodes/338) - Una variante de
esta referencia con ejercicios en navegador.
- [Documentación Oficial](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby desde otros lenguajes](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programando Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - Una
[edición antigua](http://ruby-doc.com/docs/ProgrammingRuby/) gratuita disponible en línea.
- [Guía de estilo de Ruby](https://github.com/bbatsov/ruby-style-guide) - Guía de estilo creada por la comunidad.
---
language: rust
contributors:
    - ["P1start", "http://p1start.github.io/"]
translators:
    - ["Razican", "https://www.razican.com/"]
filename: learnrust-es.rs
lang: es-es
---

Rust es un lenguaje de programación desarrollado por Mozzilla Research. Rust
combina el control del rendimiento a bajo nivel con la comodidad del alto nivel
y garantías de seguridad.

Consigue cumplir estos objetivos sin necesidad de un recolector de basura o
runtime, haciendo posible usar las librerías de Rust como sustituto de C.

La primera versión de Rust, la 0.1, fue lanzada en enero de 2012, y durante 3
años el desarrollo fue tan rápido que hasta hace poco el uso de las versiones
estables no era recomendable, y se aconsejaba usar las compilaciones nocturnas.

El 15 de mayo de 2015 se lanzó Rust 1.0, con una garantía completa de
retrocompatibilidad. A día de hoy los tiempos de compilación han mejorado mucho
desde ese lanzamiento, así como otros aspectos del lenguaje y el compilador.
Rust ha adoptado un modelo de desarrollo por series de publicaciones periódicas,
con lanzamientos cada 6 semanas. Junto con cada lanzamiento, se lanza la beta de
la siguiente versión.

A pesar de que Rust es un lenguaje relativamente de bajo nivel, tiene conceptos
funcionales que generalmente se encuentran en lenguajes de más alto nivel. Esto
hace que Rust sea rápido y al mismo tiempo fácil y eficiente a la hora de
programar.

```rust
// Esto es un comentario. Los comentarios de una sola línea se hacen así...
/* ...y los de múltiples líneas así */

//////////////////////////
// 1. Conceptos básicos //
//////////////////////////

// Funciones
// `i32` es el tipo para enteros de 32 bits con signo
fn suma2(x: i32, y: i32) -> i32 {
    // Retorno implícito (sin punto y coma)
    x + y
}

// Función principal
fn main() {
    // N;umeros //

    // Bindings (variables) inmutables
    let x: i32 = 1;

    // Sufijos para enteros / floats
    let y: i32 = 13i32;
    let f: f64 = 1.3f64;

    // Inferencia de tipos
    // La mayor parte del tiempo, el compilador de Rust puede inferir el tipo de
    // una variable, por lo que no necesitas escribir una anotación de tipo
    // explícita. A lo largo de este tutorial, los tipos están anotados
    // explícitamente en varios sitios, pero solo con propósito demostrativo. La
    // inferencia de tipos puede manejar esto por ti la mayor parte del tiempo.
    let x_implicita = 1;
    let f_implicita = 1.3;

    // Aritmética
    let sum = x + y + 13;

    // Variable mutable
    let mut mutable = 1;
    mutable = 4;
    mutable += 2;

    // Strings (cadenas de caracteres) //

    // Strings literales
    let x: &str = "hola mundo!";

    // Impresión por consola
    println!("{} {}", f, x); // 1.3 hola mundo!

    // Un `String` – una cadena en memoria dinámica (heap)
    let s: String = "hola mundo".to_string();

    // Una porión de cadena (slice) – una vista inmutable a otra cadena
    // Esto es básicamente un puntero inmutable a un string string – en realidad
    // no contiene los caracteres de la cadena, solo un puntero a algo que los
    // tiene (en este caso, `s`)
    let s_slice: &str = &s;

    println!("{} {}", s, s_slice); // hola mundo hola mundo

    // Vectores/arrays //

    // A fixed-size array
    let cuatro_enteros: [i32; 4] = [1, 2, 3, 4];

    // Un array dinámico (vector)
    let mut vector: Vec<i32> = vec![1, 2, 3, 4];
    vector.push(5);

    // Una porción (slice) – una vista inmutable a un vector o array
    // Esto es parecido a un slice de un string, pero para vectores
    let slice: &[i32] = &vector;

    // Usa `{:?}` para imprimir algo en estilo debug
    println!("{:?} {:?}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    // Tuplas //

    // Una tupla es un conjunto de tamaño fijo de valores. Pueden ser de diferente tipo.
    let x: (i32, &str, f64) = (1, "hola", 3.4);

    // Desestructurando `let`
    let (a, b, c) = x;
    println!("{} {} {}", a, b, c); // 1 hola 3.4

    // Indexando
    println!("{}", x.1); // hola

    //////////////
    // 2. Tipos //
    //////////////

    // Estructuras
    struct Punto {
        x: i32,
        y: i32,
    }

    let origen: Punto = Punto { x: 0, y: 0 };

    // Una estructura con campos sin nombre, una ‘estructura de tupla’
    struct Punto2(i32, i32);

    let origen2 = Punto2(0, 0);

    // Enums básicos como en C
    enum Direccion {
        Izquierda,
        Derecha,
        Arriba,
        Abajo,
    }

    let arriba = Direccion::Arriba;

    // Enum con campos
    enum OpcionalI32 {
        UnI32(i32),
        Nada,
    }

    let dos: OpcionalI32 = OpcionalI32::UnI32(2);
    let nada = OpcionalI32::Nada;

    // Genéricos //

    struct Foo<T> { bar: T }

    // Esto está definido en la librería estándar como `Option`
    enum Opcional<T> {
        AlgunVal(T),
        SinVal,
    }

    // Métodos //

    impl<T> Foo<T> {
        // Los métodos reciben un parámetro explícito `self`
        fn get_bar(self) -> T {
            self.bar
        }
    }

    let un_foo = Foo { bar: 1 };
    println!("{}", un_foo.get_bar()); // 1

    // Traits (conocidos como interfaces o typeclasses en otros lenguajes) //

    trait Frobnicate<T> {
        fn frobnicate(self) -> Option<T>;
    }

    impl<T> Frobnicate<T> for Foo<T> {
        fn frobnicate(self) -> Option<T> {
            Some(self.bar)
        }
    }

    let otro_foo = Foo { bar: 1 };
    println!("{:?}", otro_foo.frobnicate()); // Some(1)

    /////////////////////////////////
    // 3. Comparación con patrones //
    /////////////////////////////////

    let foo = OpcionalI32::UnI32(1);
    match foo {
        OpcionalI32::UnI32(n) => println!("es un i32: {}", n),
        OpcionalI32::Nada  => println!("no es nada!"),
    }

    // comparación de patrones avanzada
    struct FooBar { x: i32, y: OpcionalI32 }
    let bar = FooBar { x: 15, y: OpcionalI32::UnI32(32) };

    match bar {
        FooBar { x: 0, y: OpcionalI32::UnI32(0) } =>
            println!("Los números son cero!"),
        FooBar { x: n, y: OpcionalI32::UnI32(m) } if n == m =>
            println!("Los números son iguales"),
        FooBar { x: n, y: OpcionalI32::UnI32(m) } =>
            println!("Números diferentes: {} {}", n, m),
        FooBar { x: _, y: OpcionalI32::Nada } =>
            println!("El segudo número no es nada!"),
    }

    /////////////////////////
    // 4. Flujo de control //
    /////////////////////////

    // bucles `for`
    let array = [1, 2, 3];
    for i in array.iter() {
        println!("{}", i);
    }

    // Rangos
    for i in 0u32..10 {
        print!("{} ", i);
    }
    println!("");
    // imprime `0 1 2 3 4 5 6 7 8 9 `

    // `if`
    if 1 == 1 {
        println!("Las matemáticas funcionan!");
    } else {
        println!("Oh no...");
    }

    // `if` como una expresión
    let valor = if true {
        "bueno"
    } else {
        "malo"
    };

    // bucle `while`
    while 1 == 1 {
        println!("El universo está funcionando correctamente.");
    }

    // Bucle infinito
    loop {
        println!("Hola!");
    }

    ////////////////////////////////////////
    // 5. Seguridad de memoria y punteros //
    ////////////////////////////////////////

    // Posesión de punteros – solo uno puede ‘poseer’ un puntero en cada momento
    // Esto significa que cuando la `Box` queda fuera del ámbito, puede ser
    // liberada automáticamente de manera segura.
    let mut mio: Box<i32> = Box::new(3);
    *mio = 5; // dereferenciar
    // Aquí, `ahora_es_mio`, toma posesión de `mio`. En otras palabras, `mio` se
    // mueve.
    let mut ahora_es_mio = mio;
    *ahora_es_mio += 2;

    println!("{}", ahora_es_mio); // 7
    // println!("{}", mio); // esto no compilaría, porque `now_its_mine` es el
    // que posee el puntero

    // Referencia – un puntero inmutable que referencia a otro dato
    // Cuando se crea una referencia a un valor, decimos que el valor ha sido
    // ‘tomado prestado’.
    // Mientras un valor está prestado como inmutable, no puede ser modificado o
    // movido.
    // Una prestación dura hasta el fin del ámbito en el que se creó.
    let mut var = 4;
    var = 3;
    let ref_var: &i32 = &var;

    println!("{}", var); // A diferencia de `mio`, `var` se puede seguir usando
    println!("{}", *ref_var);
    // var = 5; // esto no compilaría, porque `var` está prestada
    // *ref_var = 6; // esto tampoco, porque `ref_var` es una referencia
    // inmutable

    // Referencia mutable
    // Mientras que un valor está prestado como mutable, no puede ser accedido
    // desde ningún otro sitio.
    let mut var2 = 4;
    let ref_var2: &mut i32 = &mut var2;
    *ref_var2 += 2; // '*' se usa para apuntar al var2 prestado como mutable

    println!("{}", *ref_var2); // 6 , //var2 no compilaría. //ref_var2 es de
                               // tipo &mut i32, por lo que guarda una
                               // referencia a un i32 no el valor.
    // var2 = 2; // esto no compilaría porque `var2` está prestado
}
```

## Lectura adicional

Rust es mucho más que esto. Esto es solo lo más básico para que puedas entender
las cosas más importantes. Para aprender más sobre Rust, lee [The Rust
Programming Language](http://doc.rust-lang.org/book/index.html) y echa un
vistazo al subreddit [/r/rust](http://reddit.com/r/rust). Los compañeros en el
canal #rust en irc.mozilla.org también son muy buenos con los recien llegados.
También puedes acceder a [Rust users](https://users.rust-lang.org/) a pedir
ayuda o a [Rust internals](https://internals.rust-lang.org/) para aprender más
sobre el lenguaje y colaborar en su desarrollo.

También puedes probar Rust con un compilador online en el oficial [Rust
playpen](http://play.rust-lang.org) o en la [web principal de
Rust](http://rust-lang.org).
---
language: sass
filename: learnsass-es.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
  - ["Sean Corrales", "https://github.com/droidenator"]
  - ["Kyle Mendes", "https://github.com/pink401k"]
  - ["Keith Miyake", "https://github.com/kaymmm"]
translators:
  - ["César Suárez", "https://github.com/csuarez"]
lang: es-es
---

Sass es un lenguaje que extiende CSS y que añade características tales como variables, anidación, mixins y más. Sass (y otros preprocesadores tales como [Less](http://lesscess.org/)) ayudan a los desarrolladores a escribir código mantenible y DRY (Don't Repeat Yourself).

Sass tiene dos sintaxis para elegir: SCSS, que usa la misma que CSS pero con las características añadidas de Sass, y Sass (la sintaxis original) que usa identación en vez de llaves y puntos y comas. Este tutorial está escrito en SCSS.

Si ya estás familiarizado con CSS3, vas a entender Sass relativamente rápido. Sass no ofrece nuevas propiedades de estilo, si no que añade herramientas para escribir tus CSS de manera más eficiente, haciendo su mantenimiento mucho más sencillo.

```scss


//Los comentarios en una sola línea son eliminados cuando Sass es compilado a CSS.

/* Los comentarios multi-línea se mantienen. */



/* Variables
============================== */


/* Puedes almacenar valores CSS (como un color) en una variable.
Usa el símbolo '$' para crear una variable */

$primary-color: #A3A4FF;
$secondary-color: #51527F;
$body-font: 'Roboto', sans-serif;

/* Puedes usar las variables a lo largo de tu hoja de estilos.
Ahora, si quieres cambiar el color, sólo lo tienes que hacer una vez. */

body {
	background-color: $primary-color;
	color: $secondary-color;
	font-family: $body-font;
}

/* Este código compilará en: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}

/* El resultado es mucho más mantenible que tener que cambiar el color
cada vez que aparece en la hoja de estilos. */



/* Directivas de control
============================== */


/* Sass permite usar @if, @else, @for, @while, y @each para controlar la
   compilación de tu código en CSS. */

/* Los bloques @if/@else se comportan tal como es de esperar */

$debug: true !default;

@mixin debugmode {
	@if $debug {
		@debug "Modo debug activado";

		display: inline-block;
	}
	@else {
		display: none;
	}
}

.info {
	@include debugmode;
}

/* Si $debug es true, .info es mostrado; si es false entonces
no se muestra.

Nota: @debug mostrará información de depuración en la consola.
Es muy útil para ver el contenido de tus variables cuando estás depurando. */

.info {
	display: inline-block;
}

/* @for es un bucle que itera un conjunto de valores.
Es particularmente útil para dar estilos a una colección de objetos.
Hay dos formas "through" y "to". El primero incluye el último valor
mientras que el segundo para antes del último valor. */

@for $c from 1 to 4 {
	div:nth-of-type(#{$c}) {
		left: ($c - 1) * 900 / 3;
	}
}

@for $c from 1 through 3 {
	.myclass-#{$c} {
		color: rgb($c * 255 / 3, $c * 255 / 3, $c * 255 / 3);
	}
}

/* Esto compila en: */

div:nth-of-type(1) {
	left: 0;
}

div:nth-of-type(2) {
	left: 300;
}

div:nth-of-type(3) {
	left: 600;
}

.myclass-1 {
	color: #555555;
}

.myclass-2 {
	color: #aaaaaa;
}

.myclass-3 {
	color: white;
// SASS convierte automáticamente #FFFFFF a white
}

/* @while es bastante sencillo: */

$columns: 4;
$column-width: 80px;

@while $columns > 0 {
	.col-#{$columns} {
		width: $column-width;
		left: $column-width * ($columns - 1);
	}

	$columns: $columns - 1;
}

/* Esto se convierte en el siguiente CSS: */

.col-4 {
	width: 80px;
	left: 240px;
}

.col-3 {
	width: 80px;
	left: 160px;
}

.col-2 {
	width: 80px;
	left: 80px;
}

.col-1 {
	width: 80px;
	left: 0px;
}

/* @each funciona parecido a @for, pero usa una lista en ver del valor ordinal
Nota: puedes especificar listas como cualquier otra variable usando espacios
como delimitadores. */

$social-links: facebook twitter linkedin reddit;

.social-links {
	@each $sm in $social-links {
		.icon-#{$sm} {
			background-image: url("images/#{$sm}.png");
		}
	}
}

/* Esto resultará en: */

.social-links .icon-facebook {
	background-image: url("images/facebook.png");
}

.social-links .icon-twitter {
	background-image: url("images/twitter.png");
}

.social-links .icon-linkedin {
	background-image: url("images/linkedin.png");
}

.social-links .icon-reddit {
	background-image: url("images/reddit.png");
}



/* Mixins
==============================*/


/* Si te encuentras con que estás escribiendo el mismo código en más de un
elemento, puede que quieras almacenarlo en un mixin.

Usa la directiva '@mixin', más un nombre para tu mixin. */

@mixin center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* Puedes usarlo con '@include' y el nombre del mixin. */

div {
	@include center;
	background-color: $primary-color;
}

/* Esto compilará en: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}

/* Puedes usar mixins para crear una propiedad shorthand. */

@mixin size($width, $height) {
	width: $width;
	height: $height;
}

/* La que puedes invocar pasándole los argumentos width y height. */

.rectangle {
	@include size(100px, 60px);
}

.square {
	@include size(40px, 40px);
}

/* Compila en: */
.rectangle {
  width: 100px;
  height: 60px;
}

.square {
  width: 40px;
  height: 40px;
}



/* Funciones
============================== */


/* Sass ofrece funciones que pueden ser usadas para realizar una gran variedad
   de tareas. Por ejemplo: */

/* Se pueden invocar funciones usando su nombre y pasándole los
   argumentos requeridos. */
body {
  width: round(10.25px);
}

.footer {
  background-color: fade_out(#000000, 0.25);
}

/* Compila en: */

body {
  width: 10px;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* Puedes definir tus propias funciones. Las funciones son muy similares a
   los mixins. Cuando tengas que elegir entre una función y un mixin, recuerda
   que los mixins son mejores para generar CSS, mientras que las funciones son
   mejores para la lógica que puedas necesitar en tu código Sass. Los ejemplos
   de la sección 'Operadores matemáticos' son candidatos ideales para ser
   usados como una función reusable. */

/* Esta función toma un tamaño objetivo y el tamaño de un padre y
  devuelve el porcentaje. */

@function calculate-percentage($target-size, $parent-size) {
  @return $target-size / $parent-size * 100%;
}

$main-content: calculate-percentage(600px, 960px);

.main-content {
  width: $main-content;
}

.sidebar {
  width: calculate-percentage(300px, 960px);
}

/* Compila en: */

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}



/* Extender (Herencia)
============================== */


/* Extend es una manera de compartir propiedades de un selector con otro. */

.display {
	@include size(5em, 5em);
	border: 5px solid $secondary-color;
}

.display-success {
	@extend .display;
	border-color: #22df56;
}

/* Compila en: */
.display, .display-success {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F;
}

.display-success {
  border-color: #22df56;
}

/* Extender una declaración CSS es preferible a crear un mixin
   debido a la manera en la que Sass agrupa las clases que comparten
   los mismos estilos base. Si esto fuese hecho con un mixin, el ancho,
   alto y el borden aparecerían duplicados para cada una de las declaraciones
   que usasen el mixin. Esto no afectará a tu workflow, pero infla
   innecesariamente los ficheros generados por el compilador Sass. */



/* Anidación
============================== */


/* Sass permite anidar selectores dentro de otros selectores. */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #FF0000;
	}
}

/* '&' será reemplazado por el selector del padre. */

/* También puedes anidar seudo clases. */

/* Ten en cuenta que anidar demasiado hará tu código menos mantenible.
Como buena práctica, se recomienda no tener más de 3 niveles de anidación.
Por ejemplo: */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Compila en: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/* Parciales e importaciones
============================== */


/* Sass permite que crees ficheros parciales. Esto te puede ayudar a mantener
   tu código Sass modularizado. Los ficheros parciales deben comenzar por '_',
   p.e. _reset.css.
   Los parciales no son convertidos en CSS. */

/* Mira este al que vamos a añadir un fichero llamado _reset.css */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Con @import puedes importar parciales a un fichero. Este se diferencia del
   @import de CSS en que no hace otra petición HTTP para importar el fichero.
   Sass, sino que combina el código importado en el código compilado. */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* Compila en: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/* Placeholders
============================== */


/* Los placeholders son útiles cuando estás creando una declaración CSS a
   extender. Si quieres crear una declaración que sólo va a ser usada con @extend,
   puedes hacerlo mediante un placeholder. Los placeholders comienzan con '%'
   en vez de '.' o '#'. Esto no aparecen en el código CSS compilado. */

%content-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  @extend %content-window;
  background-color: #0000ff;
}

/* Compila en: */

.message-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  background-color: #0000ff;
}



/* Operaciones matemáticas
============================== */


/* Sass provee los siguientes operadores: +, -, *, / y %. Estos son útiles
   para calcular valores directamente en tu código Sass en vez de usar valores
   calculados a mano. Mira el siguiente ejemplo que prepara un sencillo diseño
   de dos columnas. */

$content-area: 960px;
$main-content: 600px;
$sidebar-content: 300px;

$main-size: $main-content / $content-area * 100%;
$sidebar-size: $sidebar-content / $content-area * 100%;
$gutter: 100% - ($main-size + $sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: $main-size;
}

.sidebar {
  width: $sidebar-size;
}

.gutter {
  width: $gutter;
}

/* Compila en: */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}

```

## ¿SASS o Sass?
¿Alguna vez has pensado si Sass es un acrónimo o no? Seguramente no, pero te lo vamos a explicar de todas maneras. "Sass" es una palabra, no un acrónimo.
Como todo el mundo lo escribe como "SASS", el creador del lenguaje lo ha llamado de broma "Syntactically Awesome StyleSheets" (Hojas de estilo sintácticamente increíbles).


## Practica Sass
Si quieres probar Sass en tu navegador, prueba  [SassMeister](http://sassmeister.com/).
Puedes usar cualquier sintaxis, o elegir en la configuración entre Sass y SCSS.

## Compatibilidad
Sass puede ser usado en cualquier proyecto mientras tengas un programa que lo compile en CSS. Quizás quieras comprobar si el CSS que estás usando es compatible con tus navegadores objetivo.

[QuirksMode CSS](http://www.quirksmode.org/css/) y [CanIUse](http://caniuse.com) son buenos recursos para comprobar la compatibilidad de navegadores.


## Más información
* [Documentación oficial (EN)](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way (EN)](http://thesassway.com/) tiene tutoriales (para principiantes y avanzandos) and artículos.
---
language: self
contributors:
    - ["Russell Allen", "http://github.com/russellallen"]
filename: learnself-es.self
translators:
    - ["Damaso Sanoja", "https://github.com/damasosanoja"]
lang: es-es
---

Self es un lenguaje OO basado en prototipo rápido que corre en su propio vm JIT. La mayoría del desarrollo se hace a través de la interacción con objetos vivos en un entorno de desarrollo visual llamado *morphic* que tiene integrado navegador y depurador.

Todo en Self es un objeto. Todos los cómputos son hechos enviando mensajes a los objetos. En Self se puede entender por Objetos a los conjuntos de pares clave-valor.

# Construyendo objetos

El intérprete incorporado de Self puede construir objetos, incluyendo objetos-métodos.

```
"Esto es un comentario"

"Una cadena de caracteres (string):"
'Esto es un string con \'caracteres\' escapados.\n'

"Un entero de 30 bits"
23

"Un decimal de 30 bits"
3.2

"-20"
-14r16

"Un objeto que solo entiende un mensaje, 'x' que regresa 20"
(|
  x = 20.
|)

"Un objeto que además entiende 'x:' que establece la posición x"
(|
  x <- 20.
|)

"Un objeto que entiende el método 'doubleX' el cual
duplica el valor de x y luego regresa el objeto"
(|
  x <- 20.
  doubleX = (x: x * 2. self)
|)

"Un objeto que entiende todos los mensajes
que 'traits point' entiende". El intérprete
mira a 'traits point' enviando los mensajes
'traits' y luego 'point' a un objeto conocido llamado
el 'lobby'. El mira el objeto 'true' enviando
también el mensaje 'true' al lobby."
(|     parent* = traits point.
       x = 7.
       y <- 5.
       isNice = true.
|)
```

# Enviando mensajes a los objetos

Los mensajes pueden ser unarios, binarios o palabras clave. La precedencia es en ese orden. A diferencia de Smalltalk, la precedencia de los mensajes binarios debe ser especificada, y todas las palabras clave después de la primera deben comenzar con una letra mayúscula. Los mensajes se separan de sus destinos mediante espacios en blanco.

```
"mensaje unario, envía 'printLine' al objeto '23'
que imprime el string '23' en stdout y regresa el objeto recibido (ejem 23)"
23 printLine

"envía el mensaje '+' con '7' para '23', luego el mensaje '*' con '8' para el resultado"
(23 + 7) * 8

"envía 'power:' para '2' con '8' regresa 256"
2 power: 8

"envía 'keyOf:IfAbsent:' para 'hello' con los argumentos 'e' y '-1'.
Regresa 1, el índice de 'e' en 'hello'."
'hello' keyOf: 'e' IfAbsent: -1
```

# Bloques

Self define el control de flujo como Smalltalk y Ruby mediante bloques Los bloques son cómputos demorados de la forma.:

```
[|:x. localVar| x doSomething with: localVar]
```

Ejemplos del uso de bloques:

```
"regresa 'HELLO'"
'hello' copyMutable mapBy: [|:c| c capitalize]

"regresa 'Nah'"
'hello' size > 5 ifTrue: ['Yay'] False: ['Nah']

"regresa 'HaLLO'"
'hello' copyMutable mapBy: [|:c|
   c = 'e' ifTrue: [c capitalize]
            False: ['a']]
```

Las expresiones múltiples son separadas por un punto. ^ retorna inmediatamente.

```
"returns An 'E'! How icky!"
'hello' copyMutable mapBy: [|:c. tmp <- ''|
   tmp: c capitalize.
   tmp = 'E' ifTrue: [^ 'An \'E\'! How icky!'].
   c capitalize
   ]
```

Los bloques son ejecutados al enviales el mensaje 'value' y son inherentes (delegados a) sus contextos:
```
"returns 0"
[|x|
    x: 15.
    "Envía repetidamente 'value' al primer bloque mientras el resultado de enviar 'value' al segundo bloque es el objeto 'true'"
    [x > 0] whileTrue: [x: x - 1].
    x
] value
```

# Métodos

Los métodos son como los bloques pero no están dentro de un contexto sino que son almacenados como valores de ranuras. A diferencia de Smalltalk, los métodos no regresan por defecto 'self' sino su valor final.

```
"Aquí tenemos un objeto con una ranura asignable 'x' y un método 'reduceXTo: y'.
Enviando el mensaje 'reduceXTo: 10' a este objeto pondrá
el objeto '10' en la ranura 'x' y regresará el objeto original"
(|
    x <- 50.
    reduceXTo: y = (
        [x > y] whileTrue: [x: x - 1].
        self)
|)
.
```

# Prototipos

Self no posee clases. La forma de acceder a un objeto es encontrando un prototipo y copiándolo.

```
| d |
d: dictionary copy.
d at: 'hello' Put: 23 + 8.
d at: 'goodbye' Put: 'No!.
"Prints No!"
( d at: 'goodbye' IfAbsent: 'Yes! ) printLine.
"Prints 31"
( d at: 'hello' IfAbsent: -1 ) printLine.
```

# Para mayor información

El [Manual de Self](http://handbook.selflanguage.org) tiene mucha más información, y nada mejor que experiencia de primera mano con Self descargándolo de su [página web](http://www.selflanguage.org).
---
language: swift
contributors:
  - ["Grant Timmerman", "http://github.com/grant"]
  - ["Christopher Bess", "http://github.com/cbess"]
  - ["Joey Huang", "http://github.com/kamidox"]  
  - ["Anthony Nguyen", "http://github.com/anthonyn60"]
translators:
    - ["David Hsieh", "http://github.com/deivuh"]
lang: es-es
filename: learnswift-es.swift
---

Swift es un lenguaje de programación para el desarrollo en iOS y OS X creado
por Apple. Diseñado para coexistir con Objective-C y ser más resistente contra
el código erroneo, Swift fue introducido en el 2014 en el WWDC, la conferencia
de desarrolladores de Apple.

Véase también la guía oficial de Apple, [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/), el cual tiene un completo tutorial de Swift.


```swift
// Importar un módulo
import UIKit

//
// MARK: Básicos
//

// XCode soporta referencias para anotar tu código y agregarlos a lista de la
// barra de saltos.
// MARK: Marca de sección
// TODO: Hacer algo pronto
// FIXME: Arreglar este código

// En Swift 2, println y print fueron combinados en un solo método print.
// Print añade una nueva línea automáticamente.
print("Hola, mundo") // println ahora es print
print("Hola, mundo", appendNewLine: false) // print sin agregar nueva línea

// Valores de variables (var) pueden cambiar después de ser asignados
// Valores de constrantes (let) no pueden cambiarse después de ser asignados

var myVariable = 42
let øπΩ = "value" // nombres de variable unicode
let π = 3.1415926
let convenience = "keyword" // nombre de variable contextual
// Las declaraciones pueden ser separadas por punto y coma (;)
let weak = "keyword"; let override = "another keyword"
// Los acentos abiertos (``) permiten utilizar palabras clave como nombres de
// variable
let `class` = "keyword"
let explicitDouble: Double = 70
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "some text " + String(myVariable) // Conversión (casting)
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // Interpolación de string

// Valores específicos de la compilación (build)
// utiliza la configuración -D
#if false
    print("No impreso")
    let buildValue = 3
#else
    let buildValue = 7
#endif
print("Build value: \(buildValue)") // Build value: 7

/*
    Las opcionales son un aspecto del lenguaje Swift que permite el
    almacenamiento de un valor `Some` (algo) o `None` (nada).

    Debido a que Swift requiere que cada propiedad tenga un valor,
    hasta un valor 'nil' debe de ser explicitamente almacenado como un
    valor opcional.

    Optional<T> es un enum.
*/
var someOptionalString: String? = "opcional" // Puede ser nil
// Al igual que lo anterior, pero ? es un operador postfix (sufijo)
var someOptionalString2: Optional<String> = "opcional"

if someOptionalString != nil {
    // No soy nil
    if someOptionalString!.hasPrefix("opt") {
        print("Tiene el prefijo")
    }

    let empty = someOptionalString?.isEmpty
}
someOptionalString = nil

// Opcional implícitamente desenvuelto
var unwrappedString: String! = "Un valor esperado."
// Al igual que lo anterior, pero ! es un operador postfix (sufijo)
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Un valor esperado."

if let someOptionalStringConstant = someOptionalString {
    // tiene valor `Some` (algo), no nil
    if !someOptionalStringConstant.hasPrefix("ok") {
        // No tiene el prefijo
    }
}

// Swift tiene soporte de almacenamiento para cualquier tipo de valor.
// AnyObject == id
// A diferencia de Objective-C `id`, AnyObject funciona con cualquier
// valor (Class, Int, struct, etc)
var anyObjectVar: AnyObject = 7
anyObjectVar = "Cambiado a un valor string, no es buena práctica, pero posible."

/*
    Comentar aquí

    /*
        Comentarios anidados también son soportados
    */
*/

//
// MARK: Colecciones
//

/*
    Tipos Array (arreglo) y Dictionary (diccionario) son structs (estructuras).
    Así que `let` y `var` también indican si son mudables (var) o
    inmutables (let) durante la declaración de sus tipos.    
*/

// Array (arreglo)
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
let emptyArray = [String]() // let == inmutable
let emptyArray2 = Array<String>() // igual que lo anterior
var emptyMutableArray = [String]() // var == mudable


// Dictionary (diccionario)
var occupations = [
    "Malcolm": "Captain",
    "kaylee": "Mechanic"
]
occupations["Jayne"] = "Public Relations"
let emptyDictionary = [String: Float]() // let == inmutable
let emptyDictionary2 = Dictionary<String, Float>() // igual que lo anterior
var emptyMutableDictionary = [String: Float]() // var == mudable


//
// MARK: Flujo de control
//

// Ciclo for (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
    if value == 1 {
        print("Uno!")
    } else {
        print("No es uno!")
    }
}

// Ciclo for (dictionary)
var dict = ["uno": 1, "dos": 2]
for (key, value) in dict {
    print("\(key): \(value)")
}

// Ciclo for (range)
for i in -1...shoppingList.count {
    print(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// Utilizar ..< para excluir el último valor

// Ciclo while
var i = 1
while i < 1000 {
    i *= 2
}

// Ciclo do-while
do {
    print("Hola")
} while 1 == 2

// Switch
// Muy potente, se puede pensar como declaraciones `if` con _azúcar sintáctico_
// Soportan String, instancias de objetos, y primitivos (Int, Double, etc)
let vegetable = "red pepper"
switch vegetable {
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let localScopeValue where localScopeValue.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(localScopeValue)?"
default: // obligatorio (se debe cumplir con todos los posibles valores de entrada)
    let vegetableComment = "Everything tastes good in soup."
}


//
// MARK: Funciones
//

// Funciones son un tipo de primera-clase, quiere decir que pueden ser anidados
// en funciones y pueden ser pasados como parámetros

// Función en documentación de cabeceras Swift (formato reStructedText)

/**
    Una operación de saludo

    - Una viñeta en la documentación
    - Otra viñeta en la documentación

    :param: name Un nombre
    :param: day Un día
    :returns: Un string que contiene el valor de name y day
*/
func greet(name: String, day: String) -> String {
    return "Hola \(name), hoy es \(day)."
}
greet("Bob", "Martes")

// Similar a lo anterior, a excepción del compartamiento de los parámetros
// de la función
func greet2(requiredName: String, externalParamName localParamName: String) -> String {
    return "Hola \(requiredName), hoy es el día \(localParamName)"
}
greet2(requiredName:"John", externalParamName: "Domingo")

// Función que devuelve múltiples valores en una tupla
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// Ignorar tupla (u otros) valores utilizando _ (guión bajo)
let (_, price1, _) = pricesTuple // price1 == 3.69
print(price1 == pricesTuple.1) // true
print("Gas price: \(price)")

// Cantidad variable de argumentos
func setup(numbers: Int...) {
    // Es un arreglo
    let number = numbers[0]
    let argCount = numbers.count
}

// Pasando y devolviendo funciones
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

// Pasando como referencia
func swapTwoInts(inout a: Int, inout b: Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(&someIntA, &someIntB)
print(someIntB) // 7


//
// MARK: Closures (Clausuras)
//
var numbers = [1, 2, 6]

// Las funciones son un caso especial de closure ({})

// Ejemplo de closure.
// `->` Separa los argumentos del tipo de retorno
// `in` Separa la cabecera del cuerpo del closure
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})

// Cuando se conoce el tipo, como en lo anterior, se puede hacer esto
numbers = numbers.map({ number in 3 * number })
// o esto
//numbers = numbers.map({ $0 * 3 })

print(numbers) // [3, 6, 18]

// Closure restante
numbers = sorted(numbers) { $0 > $1 }

print(numbers) // [18, 6, 3]

// Bastante corto, debido a que el operador < infiere los tipos

numbers = sorted(numbers, < )

print(numbers) // [3, 6, 18]

//
// MARK: Estructuras
//

// Las estructuras y las clases tienen capacidades similares
struct NamesTable {
    let names = [String]()

    // Subscript personalizado
    subscript(index: Int) -> String {
        return names[index]
    }
}

// Las estructuras tienen un inicializador designado autogenerado (implícitamente)
let namesTable = NamesTable(names: ["Me", "Them"])
let name = namesTable[1]
print("Name is \(name)") // Name is Them

//
// MARK: Clases
//

// Las clases, las estructuras y sus miembros tienen tres niveles de control de acceso
// Éstos son: internal (predeterminado), public, private

public class Shape {
    public func getArea() -> Int {
        return 0;
    }
}

// Todos los métodos y las propiedades de una clase son public (públicas)
// Si solo necesitas almacenar datos en un objecto estructurado,
// debes de utilizar `struct`

internal class Rect: Shape {
    var sideLength: Int = 1

    // Getter y setter personalizado
    private var perimeter: Int {
        get {
            return 4 * sideLength
        }
        set {
            // `newValue` es una variable implícita disponible para los setters
            sideLength = newValue / 4
        }
    }

    // Lazily loading (inicialización bajo demanda) a una propiedad
    // subShape queda como nil (sin inicializar) hasta que getter es llamado
    lazy var subShape = Rect(sideLength: 4)

    // Si no necesitas un getter y setter personalizado
    // pero aún quieres ejecutar código antes y después de hacer get o set
    // a una propiedad, puedes utilizar `willSet` y `didSet`    
    var identifier: String = "defaultID" {
        // El argumento `willSet` será el nombre de variable para el nuevo valor
        willSet(someIdentifier) {
            print(someIdentifier)
        }
    }

    init(sideLength: Int) {
        self.sideLength = sideLength
        // Siempre poner super.init de último al momento de inicializar propiedades
        // personalizadas
        super.init()
    }

    func shrink() {
        if sideLength > 0 {
            sideLength -= 1
        }
    }

    override func getArea() -> Int {
        return sideLength * sideLength
    }
}

// Una clase simple `Square` que extiende de `Rect`
class Square: Rect {
    convenience init() {
        self.init(sideLength: 5)
    }
}

var mySquare = Square()
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4

// Conversión de tipo de instancia
let aShape = mySquare as Shape

// Comparar instancias, no es igual a == que compara objetos (equal to)
if mySquare === mySquare {
    print("Yep, it's mySquare")
}

// Inicialización (init) opcional
class Circle: Shape {
    var radius: Int
    override func getArea() -> Int {
        return 3 * radius * radius
    }

    // Un signo de interrogación como sufijo después de `init` es un init opcional
    // que puede devolver nil    
    init?(radius: Int) {
        self.radius = radius
        super.init()

        if radius <= 0 {
            return nil
        }
    }
}

var myCircle = Circle(radius: 1)
print(myCircle?.getArea())    // Optional(3)
print(myCircle!.getArea())    // 3
var myEmptyCircle = Circle(radius: -1)
print(myEmptyCircle?.getArea())    // "nil"
if let circle = myEmptyCircle {
    // no será ejecutado debido a que myEmptyCircle es nil
    print("circle is not nil")
}


//
// MARK: Enums
//


// Los enums pueden ser opcionalmente de un tipo específico o de su propio tipo
// Al igual que las clases, pueden contener métodos

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func getIcon() -> String {
        switch self {
        case .Spades: return "♤"
        case .Hearts: return "♡"
        case .Diamonds: return "♢"
        case .Clubs: return "♧"
        }
    }
}

// Los valores de enum permite la sintaxis corta, sin necesidad de poner
// el tipo del enum cuando la variable es declarada de manera explícita
var suitValue: Suit = .Hearts

// Enums de tipo no-entero requiere asignaciones de valores crudas directas
enum BookName: String {
    case John = "John"
    case Luke = "Luke"
}
print("Name: \(BookName.John.rawValue)")

// Enum con valores asociados
enum Furniture {
    // Asociación con Int
    case Desk(height: Int)
    // Asociación con String e Int
    case Chair(String, Int)

    func description() -> String {
        switch self {
        case .Desk(let height):
            return "Desk with \(height) cm"
        case .Chair(let brand, let height):
            return "Chair of \(brand) with \(height) cm"
        }
    }
}

var desk: Furniture = .Desk(height: 80)
print(desk.description())     // "Desk with 80 cm"
var chair = Furniture.Chair("Foo", 40)
print(chair.description())    // "Chair of Foo with 40 cm"


//
// MARK: Protocolos
//

// `protocol` puede requerir que los tipos tengan propiedades
// de instancia específicas, métodos de instancia, métodos de tipo,
// operadores, y subscripts


protocol ShapeGenerator {
    var enabled: Bool { get set }
    func buildShape() -> Shape
}

// Protocolos declarados con @objc permiten funciones opcionales,
// que te permite evaluar conformidad
@objc protocol TransformShape {
    optional func reshaped()
    optional func canReshape() -> Bool
}

class MyShape: Rect {
    var delegate: TransformShape?

    func grow() {
        sideLength += 2

        // Pon un signo de interrogación después de la propiedad opcional,
        // método, o subscript para ignorar un valor nil y devolver nil
        // en lugar de  tirar un error de tiempo de ejecución
        // ("optional chaining")        
        if let allow = self.delegate?.canReshape?() {
            // test for delegate then for method
            self.delegate?.reshaped?()
        }
    }
}


//
// MARK: Otros
//

// `extension`: Agrega funcionalidades a tipos existentes

// Square ahora se "conforma" al protocolo `Printable`
extension Square: Printable {
    var description: String {
        return "Area: \(self.getArea()) - ID: \(self.identifier)"
    }
}

print("Square: \(mySquare)")

// También puedes hacer extend a tipos prefabricados (built-in)
extension Int {
    var customProperty: String {
        return "This is \(self)"
    }

    func multiplyBy(num: Int) -> Int {
        return num * self
    }
}

print(7.customProperty) // "This is 7"
print(14.multiplyBy(3)) // 42

// Generics: Similar Java y C#. Utiliza la palabra clave `where` para
// especificar los requerimientos de los genéricos.

func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
    for (index, value) in enumerate(array) {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let foundAtIndex = findIndex([1, 2, 3, 4], 3)
print(foundAtIndex == 2) // true

// Operadores:
// Operadores personalizados puede empezar con los siguientes caracteres:
//      / = - + * % < > ! & | ^ . ~
// o
// Caracteres unicode: math, symbol, arrow, dingbat, y line/box.
prefix operator !!! {}

// Un operador prefix que triplica la longitud del lado cuando es utilizado
prefix func !!! (inout shape: Square) -> Square {
    shape.sideLength *= 3
    return shape
}

// Valor actual
print(mySquare.sideLength) // 4

// Cambiar la longitud del lado utilizando el operador !!!,
// incrementa el tamaño por 3
!!!mySquare
print(mySquare.sideLength) // 12
```
---
language: Tcl
contributors:
    - ["Poor Yorick", "https://pooryorick.com/"]
translators:
    - ["Héctor Romojaro", "https://github.com/hromojaro"]
lang: es-es
filename: learntcl-es.tcl
---

Tcl fue creado por [John Ousterhout](https://wiki.tcl.tk/John%20Ousterout) como
un lenguaje reutilizable de scripting para herramientas de diseño de circuitos
de las que él era autor.  En 1997 recibió el 
[ACM Software System Award](https://en.wikipedia.org/wiki/ACM_Software_System_Award) 
por Tcl.   Tcl puede ser utilizado tanto como lenguaje de scripting embebido, 
como lenguaje de programación general.  Puede ser utilizado también como una
biblioteca portable de C, incluso en casos donde no se requieren capacidades
de scripting, ya que provee de estructuras de datos tales como cadenas (*string*)
de caracteres dinámicas, listas y tablas hash.  La biblioteca de C también
provee funcionalidad portable para cargar bibliotecas dinámicas, formato de
cadenas y conversión de código, operaciones sobre el sistema de ficheros,
operaciones de red y más.  Algunas características reseñables de Tcl:

* Conveniente API de red multiplataforma

* Sistema de ficheros totalmente virtualizado

* Canales apilables de E/S

* Asíncrono hasta el núcleo

* Corrutinas completas

* Un modelo de hebras reconocido como robusto y fácil de usar


Tcl tiene mucho en común con Lisp pero, en lugar de listas, Tcl utiliza cadenas
de caracteres como moneda de cambio del lenguaje.  Todos los valores son cadenas.
Una lista es una cadena con un formato definido, y el cuerpo de un procedimiento
(un script) es también una cadena en lugar de un bloque.  Para incrementar el
rendimiento, Tcl cachea internamente representaciones estructuradas de estos
valores.  Las rutinas con listas, por ejemplo, operan en la representación interna
en caché, y Tcl se ocupa de actualizar la representación en cadenas si es realmente
necesario en el script.  El diseño *copy-on-write* de Tcl permite a los autores
de scripts mover grandes volúmenes de datos sin incurrir en el consumo adicional
de memoria.  Los procedimientos son automáticamente compilados (*byte-compiled*)
a no ser que utilicen rutinas dinámicas como "uplevel", "upvar" o "trace".

Programar en Tcl es un placer.  Le resultará atractivo a hackers que encuentren
atractivo Lisp, Forth o Smalltalk, y a ingenieros y científicos que simplemente
quieren ponerse a trabajar con una herramienta que se doblega a su voluntad.  La
disciplina de exponer toda la funcionalidad programática como rutinas, incluyendo
cosas como iteraciones y operaciones matemáticas que normalmente están en la
sintaxis de otros lenguajes, permitiendo fundirse en el fondo de cualquier
funcionalidad específica del dominio que necesita un proyecto.  Su sintaxis,
incluso más simple que la de lisp, simplemente se quita de en medio.



```tcl
#! /bin/env tclsh

###############################################################################
## 1. Directrices
###############################################################################

# ¡Tcl no es ni Sh ni C!  Es necesario decirlo porque el entrecomillado estándar
# de shell casi funciona en Tcl, y es común que la gente empiece con Tcl e
# intente utilizar sintaxis de otros lenguajes.  Funciona al principio, pero
# rápidamente conduce a frustración cuando los scripts se vuelven más complejos.

# Las llaves son un mecanismo de entrecomillado, no de sintaxis para la construcción
# de bloques de código o listas.  Tcl no tiene ninguna de ellas.  Las llaves se
# usan para escapar caracteres especiales, lo que las hace apropiadas para 
# entrecomillar cuerpos de procedimientos y cadenas que deberían ser interpretadas
# como listas.


###############################################################################
## 2. Sintaxis
###############################################################################

# Un script consiste en comandos delimitados por saltos de línea o puntos y coma.
# Cada comando es una llamada a una rutina.  La primera palabra es el nombre de
# la rutina a llamar, y las siguientes palabras son argumentos de la rutina.
# Las palabras están delimitadas por espacios.  Puesto que cada argumento es una
# palabra en el comando, y una cadena de caracteres, puede no ser entrecomillada:
set part1 Sal
set part2 ut; set part3 ations


# el símbolo del dólar introduce la sustitución de variables:
set greeting $part1$part2$part3


# Cuando "set"recibe sólamente el nombre de una variable, devuelve su valor:
set part3 ;# Returns the value of the variable.


# Los corchetes delimitan un script que será evaluado y sustituido por su resultado:
set greeting $part1$part2[set part3]


# Un script incrustado puede estar compuesto de múltiples comandos, el último de
# los cuales devuelve el resultado de la sustitución:
set greeting $greeting[
    incr i
    incr i
    incr i
]
puts $greeting ;# La salida es "Salutations3"

# Cada palabra en un comando es una cadena, incluyendo el nombre de la rutina,
# así que se pueden utilizar sustituciones allí también. Dada esta asignación
# de variable,

set action pu

# los siguientes tres comandos son equivalentes:
puts $greeting
${action}ts $greeting 
[set action]ts $greeting


# La barra invertida suprime el significado especial de los caracteres:
set amount \$16.42


# La barra invertida añade significado especial a ciertos caracteres:
puts lots\nof\n\n\n\n\n\nnewlines


# Una palabra encerrada entre llaves no está sujeta a interpretación especial o
# sustitución, excepto que una barra invertida antes de una llave no cuenta al
# buscar la llave de cierre:
set somevar {
    This is a literal $ sign, and this \} escaped
    brace remains uninterpreted
}


# En una palabra delimitada por comillas dobles, los espacios pierden su significado
# especial:
set name Neo
set greeting "Hello, $name"


# Un nombre de variable puede ser cualquier cadena:
set {first name} New


# La forma de sustitución de variables utilizando llaves permite nombres de
# variable más complejos:
set greeting "Hello, ${first name}"


# "set" puede utilizarse siempre en lugar de la sustitución de variables, y permite
# utilizar cualquier nombre de variable:
set greeting "Hello, [set {first name}]"


# Para desempaquetar una lista en un el comando, se utiliza el operador de expansión,
# "{*}".  Estos dos comandos son equivalentes:
set name Neo
set {*}{name Neo}


# Un array es una variable especial que sirve como contenedor de otras variables.
set person(name) Neo
set person(destiny) {The One}
set greeting "Hello, $person(name)"


# "variable" se puede utilizar para declarar o asignar variables. Al contrario
# que "set", que utiliza el espacio de nombres global y el actual para resolver
# un nombre de variable, "variable" usa solamente el actual:
variable name New


# "namespace eval" crea un nuevo espacio de nombres en caso de no existir.
# Un espacio de nombres puede contener tanto rutinas como variables:
namespace eval people {
    namespace eval person1 {
        variable name Neo
    }
}


# Use dos o más ":" para delimitar componentes del espacio de nombres en nombres
# de variables:
namespace eval people {
    set greeting "Hello $person1::name"
}

# Dos o más ":" también delimitan componentes del espacio de nombres en nombres
# de rutinas:
proc people::person1::speak {} {
    puts {I am The One.}
}

# Nombres completos comienzan con dos ":":
set greeting "Hello $::people::person1::name"



###############################################################################
## 3. No más sintaxis
###############################################################################

# El resto de funcionalidades se implementa mediante rutinas.  Desde este punto,
# no hay nueva sintaxis.  Todo lo que queda para aprender Tcl es acerca del
# comportamiento de rutinas individuales y el significado que asignan a sus
# argumentos.



###############################################################################
## 4. Variables y espacios de nombres
###############################################################################

# Cada variable y cada rutina están asociadas a algún espacio de nombres

# Para terminar con un intérprete inútil, sólo hay que eliminar el espacio de
# nombres global.  No es algo muy útil, pero sirve para ilustrar la naturaleza
# de Tcl.  El nombre del espacio de nombres global es en realidad la cadena
# vacía, pero la única forma de representarlo es como un nombre completo. Para
# probarlo, se puede usar esta rutina.
proc delete_global_namespace {} {
    namespace delete ::
}

# Como "set" siempre mantiene su vista en los espacios de nombres global y actual,
# es más seguro utilizar "variable" para declarar o asignar un valor a una
# variable.  Si una variable llamada "nombre" ya existe en el espacio de nombres
# global, usar "set" asignará un valor a la variable local en lugar de a la
# variable del espacio de nombres actual, mientras que "variable" opera en el
# espacio de nombres actual solamente.
namespace eval people {
    namespace eval person1 {
        variable name Neo
    }
}

# Una vez que una variable es declarada en un espacio de nombres, [set] la vé
# en lugar de una variable de idéntico nombre en el espacio de nombres global:
namespace eval people {
    namespace eval person1 {
        variable name
        set name Neo
    }
}

# En cambio, si "set" tiene que crear una nueva variable, siempre lo hace en el
# espacio de nombres actual:
unset name
namespace eval people {
    namespace eval person1 {
        set name neo
    }

}
set people::person1::name


# Un nombre absoluto siempre comienza con el nombre del espacio de nombres global
# (cadena vacía), seguido de dos ":":
set ::people::person1::name Neo


# En el interior de un procedimiento, la variable enlaza una variable en el espacio
# de nombres actual en el ámbito local:
namespace eval people::person1 {
    proc fly {} {
        variable name
        puts "$name is flying!"
    }
}




###############################################################################
## 4. Rutinas incorporadas
###############################################################################

# Las operaciones matemáticas se pueden hacer con "expr":
set a 3
set b 4
set c [expr {$a + $b}]

# Como "expr" realiza sustituciones de variables por sí mismo, es necesario
# poner la expresión entre llaves para prevenir a Tcl sustituir las variables
# primero. Ver "http://wiki.tcl.tk/Brace%20your%20#%20expr-essions" para más
# detalles.


# "expr" entiende sustitución de variables y scripts:
set c [expr {$a + [set b]}]


# "expr" provee de un conjunto de funciones matemáticas:
set c [expr {pow($a,$b)}]


# Los operadores matemáticos están disponibles como rutinas en el espacio de
# nombres ::tcl::mathop
::tcl::mathop::+ 5 3

# Las rutinas pueden ser importadas desde otros espacios de nombres:
namespace import ::tcl::mathop::+
set result [+ 5 3]


# Los valores no numéricos deben ser entrecomillados, y los operadores como "eq"
# pueden utilizarse para restringir la operación a una comparación de cadenas:
set name Neo
expr {{Bob} eq $name}

# Los operadores generales recurren a la comparación de cadenas si una operación
# numérica no es factible.
expr {{Bob} == $name}


# "proc" crea nuevas rutinas:
proc greet name {
    return "Hello, $name!"
}

# Se pueden especificar múltiples parámetros:
proc greet {greeting name} {
    return "$greeting, $name!"
}


# Como se dijo antes, las llaves no construyen un bloque de código.  Cada valor,
# incluso el tercer argumento de "proc", es una cadena.  El comando anterior
# puede ser reescrito sin usar llaves:
proc greet greeting\ name return\ \"\$greeting,\ \$name!\"



# Cuando el último parámetro es el valor literal "args", todos los argumentos
# extra pasados a la rutina son recogidos en una lista y asignado a "args":
proc fold {cmd first args} {
    foreach arg $args {
        set first [$cmd $first $arg]
    }
    return $first
}
fold ::tcl::mathop::* 5 3 3 ;# ->  45


# La ejecución condicional se implementa como una rutina:
if {3 > 4} {
    puts {This will never happen}
} elseif {4 > 4} {
    puts {This will also never happen}
} else {
    puts {This will always happen}
}


# Los bucles se implementan como rutinas.  Los primer y tercer argumentos de "for"
# son tratados como scripts, mientras que el segundo lo es como una expresión:
set res 0
for {set i 0} {$i < 10} {incr i} {
    set res [expr {$res + $i}]
}
unset res


# El primer argumento de "while" se trata también como una expresión:
set i 0
while {$i < 10} {
    incr i 2
}


# Una lista es una cadena, y los elementos de la lista se delimitan con espacios
# en blanco:
set amounts 10\ 33\ 18
set amount [lindex $amounts 1]

# El espacio en blanco dentro de una lista debe ser entrecomillado:
set inventory {"item 1" item\ 2 {item 3}}


# Generalmente, es mejor idea usar rutinas de listas al modificarlas:
lappend inventory {item 1} {item 2} {item 3}


# Las llaves y barras invertidas pueden utilizarse para formatear valores más
# complejos en una lista.  Una lista parece un script, excepto en que el carácter
# de nueva línea y el ":" pierden su significado especial, y no hay sustitución
# de variable o scripts.  Esta característica hace Tcl homoicónico.  Hay tres
# elementos en la siguiente lista:
set values {

    one\ two

    {three four}

    five\{six

}


# Como, al igual que todos los valores, una lista es una cadena, operaciones de
# cadenas pueden ser realizadas sobre ellas, corriendo el riesgo de corromper
# el formato de la lista:
set values {one two three four}
set values [string map {two \{} $values] ;# $values is no-longer a \
    properly-formatted list


# La forma segura de conseguir una lista debidamente formateada es utilizando
# las rutinas propias de lista:
set values [list one \{ three four]
lappend values { } ;# add a single space as an item in the list


# Se puede utilizar "eval" para evaluar un valor como un script:
eval {
    set name Neo
    set greeting "Hello, $name"
}


# Una lista siempre puede ser pasada a "eval" como un script compuesto de un único
# comando:
eval {set name Neo}
eval [list set greeting "Hello, $name"]


# Por lo tanto, cuando se utiliza "eval", use "list" para construir el comando
# deseado:
set command {set name}
lappend command {Archibald Sorbisol}
eval $command


# Un error común es no usar funciones de listas al construir un comando:
set command {set name}
append command { Archibald Sorbisol}
try {
    eval $command ;# El error es que "set" tiene demasiados argumentos en \
        {set name Archibald Sorbisol}
} on error {result eoptions} {
    puts [list {received an error} $result]
}

# Este error puede ocurrir fácilmente con "subst":

set replacement {Archibald Sorbisol}
set command {set name $replacement}
set command [subst $command] 
try {
    eval $command ;# El mismo error que antes:  demasiados argumentos a "set" en \
        {set name Archibald Sorbisol}
} trap {TCL WRONGARGS} {result options} {
    puts [list {received another error} $result]
}


# "list" formatea correctamente un valor para su sustitución:
set replacement [list {Archibald Sorbisol}]
set command {set name $replacement}
set command [subst $command]
eval $command


# "list" se utiliza normalmente para formatear valores para su sustitución en
# scripts: Hay muchos ejemplos de esto más abajo.


# "apply" evalúa una lista de dos elementos como una rutina:
set cmd {{greeting name} {
    return "$greeting, $name!"
}}
apply $cmd Whaddup Neo

# Un tercer elemento puede ser utilizado para especificar el espacio de nombres
# donde aplicar la rutina:
set cmd [list {greeting name} {
    return "$greeting, $name!"
} [namespace current]]
apply $cmd Whaddup Neo


# "uplevel" evalúa un script en un nivel superior de la pila de llamadas:
proc greet {} {
    uplevel {puts "$greeting, $name"}
}

proc set_double {varname value} {
    if {[string is double $value]} {
        uplevel [list variable $varname $value]
    } else {
        error [list {not a double} $value]
    }
}


# "upvar" enlaza una variable en el nivel actual de la pila de llamadas a una
# variable en un nivel superior:
proc set_double {varname value} {
    if {[string is double $value]} {
        upvar 1 $varname var
        set var $value
    } else {
        error [list {not a double} $value]
    }
}


# Deshacerse de la rutina "while" incorporada, y utilizar "proc" para definir
# una nueva:
rename ::while {}
# la manipulación se deja como ejercicio:
proc while {condition script} {
    if {[uplevel 1 [list expr $condition]]} {
        uplevel 1 $script
        tailcall [namespace which while] $condition $script
    }
}


# "coroutine" crea una nueva pila de llamadas, una nueva rutina en la que
# introducir esa pila de llamadas, y luego llama a dicha rutina. "yield" suspende
# la evaluación en esa pila y devuelve el control a la pila que efectúa la llamada.
proc countdown count {
    # devuelve algo al creador de la corrutina, efectivamente pausando esta
    # pila de llamadas por ahora.
    yield [info coroutine]

    while {$count > 1} {
        yield [incr count -1]
    }
    return 0
}
coroutine countdown1 countdown 3
coroutine countdown2 countdown 5
puts [countdown1] ;# -> 2 
puts [countdown2] ;# -> 4 
puts [countdown1] ;# -> 1 
puts [countdown1] ;# -> 0 
catch {
    puts [coundown1] ;# -> invalid command name "countdown1"
} cres copts 
puts $cres
puts [countdown2] ;# -> 3 


# Pilas de corrutinas pueden cederse el control entre sí:

proc pass {whom args} {
    return [yieldto $whom {*}$args]
}

coroutine a apply {{} {
        yield
        set result [pass b {please pass the salt}]
        puts [list got the $result]
        set result [pass b {please pass the pepper}]
        puts [list got the $result]
}}

coroutine b apply {{} {
    set request [yield]
    while 1 {
        set response [pass c $request]
        puts [list [info coroutine] is now yielding]
        set request [pass a $response]
    }
}}

coroutine c apply {{} {
    set request [yield]
    while 1 {
        if {[string match *salt* $request]} {
            set request [pass b salt]
        } else {
            set request [pass b huh?]
        }
    }
}}

# Pon las cosas en marcha
a


```

## Reference

[Documentación oficial de Tcl](http://www.tcl.tk/man/tcl/)

[Tcl Wiki](http://wiki.tcl.tk)

[Tcl Subreddit](http://www.reddit.com/r/Tcl)
---
category: tool
tool: tmux
contributors:
    - ["mdln", "https://github.com/mdln"]
filename: LearnTmux-es.txt
translators:
    - ["Damaso Sanoja", "https://github.com/damasosanoja"]
lang: es-es
---


[tmux](http://tmux.sourceforge.net)
es un terminal multiplexor: habilita la creación, acceso y control
de múltiples terminales controlados desde una sola pantalla. tmux
puede ser separado de una pantalla y continuar corriendo en el fondo
y luego ser insertado nuevamente.


```

  tmux [command]     # Corre un comando
                     # 'tmux' sin comandos creará una nueva sesión

    new              # Crea una nueva sesión
     -s "Session"    # Crea sesión con nombre
     -n "Window"     # Crea ventana con nombre
     -c "/dir"       # Comienza en el directorio destino 

    attach           # Adjunta sesión última/disponible
     -t "#"          # Adjunta sesión destino
     -d              # Separa la sesión de otras instancias

    ls               # Lista las sesiones abiertas
     -a              # Lista todas las sesiones abiertas

    lsw              # Lista las ventanas
     -a              # Lista todas las ventanas
     -s              # Lista todas las ventanas en la sesión

    lsp              # Lista los páneles
     -a              # Lista todos los páneles
     -s              # Lista todos los páneles de la sesión
     -t              # Lista los páneles de aplicación en el destino

    kill-window      # Cierra la ventana actual
     -t "#"          # Cierra la ventana destino
     -a              # Cierra todas las ventanas
     -a -t "#"       # Cierra todas las ventanas menos el destino

    kill-session     # Cierra la sesión actual
     -t "#"          # Cierra la sesión destino
     -a              # Cierra todas las sesiones
     -a -t "#"       # Cierra todas las sesiones menos el destino

```


### Atajos de Teclado

El método para controlar una sesión adjunta tmux es mediante
combinaciones de teclas llamadas teclas 'Prefijo'.

```
----------------------------------------------------------------------
  (C-b) = Ctrl + b    # combinación 'Prefijo' necesaria para usar atajos

  (M-1) = Meta + 1 -o- Alt + 1
----------------------------------------------------------------------

  ?                  # Lista todos los atajos de teclado
  :                  # Entra en la línea de comandos tmux
  r                  # Fuerza el redibujado del cliente adjuntado
  c                  # Crea una nueva ventana

  !                  # Separa el panel actual fuera de la ventana.
  %                  # Separa el panel actual en dos, izquierdo y derecho
  "                  # Separa el panel actual en dos, superior e inferior

  n                  # Cambia a la siguiente ventana
  p                  # Cambia a la ventana previa
  {                  # Intercambia el panel actual con el anterior
  }                  # Intercambia el panel actual con el próximo

  s                  # Selecciona una nueva sesión para el cliente adjuntado
                     interactivamente
  w                  # Elegir la ventana actual interactivamente
  0 al 9             # Seleccionar ventanas 0 al 9

  d                  # Separa el cliente actual
  D                  # Elige un cliente para separar

  &                  # Cierra la ventana actual
  x                  # Cierra el panel actual

  Up, Down           # Cambia al panel superior, inferior, izquierdo, o derecho
  Left, Right

  M-1 to M-5         # Organizar páneles:
                       # 1) uniformes horizontales
                       # 2) uniformes verticales
                       # 3) principal horizontal
                       # 4) principal vertical
                       # 5) mozaico

  C-Up, C-Down       # Redimensiona el panel actual en pasos de una celda
  C-Left, C-Right

  M-Up, M-Down       # Redimensiona el panel actual en pasos de cinco celdas
  M-Left, M-Right

```


### Configurando ~/.tmux.conf

tmux.conf puede usarse para establecer opciones automáticas al arrancar, parecido a como .vimrc o init.el hacen.

```
# Ejemplo de tmux.conf
# 2014.10


### General
###########################################################################

# Habilita UTF-8
setw -g utf8 on
set-option -g status-utf8 on

# Fuera de pantalla/Historia límite
set -g history-limit 2048

# Comienzo de índice
set -g base-index 1

# Ratón
set-option -g mouse-select-pane on

# Forza recarga de fichero de configuración
unbind r
bind r source-file ~/.tmux.conf


### Atajos de teclado
###########################################################################

# Desvincula C-b como el prefijo por defecto
unbind C-b

# Establece el nuevo prefijo
set-option -g prefix `

# Regresa a la ventana previa cuando el prefijo es accionado dos veces
bind C-a last-window
bind ` last-window

# Permite intercambiar C-a y ` usando F11/F12
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Preferencias de atajos
setw -g mode-keys vi
set-option -g status-keys vi

# Moviéndose entre paneles con movimientos de teclas vim
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Ciclo/Intercambio de Ventana
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# División rápida de paneles
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# Activar sesión mas interna (cuando se anida tmux) para enviar comandos
bind a send-prefix


### Temas
###########################################################################

# Paleta de Colores de la Barra de estado
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# Paleta de Colores del Borde del Panel
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Paleta de Colores de Mensajes
set-option -g message-fg black
set-option -g message-bg green

# Paleta de Colores de la Ventana
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-content-attr default
setw -g window-status-content-fg yellow
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### UI
###########################################################################

# Notificación
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# Establece automáticamente títulos de ventanas
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not)

# Ajustes de barra de estado
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# Muestra indicadores de rendimiento en barra de estado
# Requiere https://github.com/thewtex/tmux-mem-cpu-load/
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```


### Referencias

[Tmux | Inicio](http://tmux.sourceforge.net)

[Tmux Manual](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)

[Mostrar CPU/MEM % en barra de estado](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)
---
category: tool
tool: tmux
contributors:
    - ["mdln", "https://github.com/mdln"]
translators:
    - ["Ferran Pelayo", "https://github.com/ferranpm"]
filename: LearnTmux-es.txt
lang: es-es
---

[tmux](http://tmux.sourceforge.net) permite crear, controlar y acceder a
multiples terminales desde una sola ventana.  Puede desconectarse una sesión de
la ventana, seguir corriendo en segundo plano y volver a conectar otra ventana
más tarde.


```

  tmux [command]     # Correr un comando de tmux
                     # 'tmux' sin comando crea una nueva sesión.

    new              # Crear una nueva sesión
     -s "Session"    # Crear una sesión con nombre
     -n "Window"     # Crear una ventana con nombre
     -c "/dir"       # Empezar en el directorio "/dir"

    attach           # Atar la ventana a la ultima sesión iniciada
     -t "#"          # Atar la ventana a la sesión "#"
     -d              # Desatar la ventana de la sesión

    ls               # Listar las sesiones abiertas
     -a              # Listar todas las sesiones abiertas

    lsw              # Listar ventanas
     -a              # Listar todas las ventanas
     -s              # Listar todas las ventanas de la sesión

    lsp              # Listar paneles
     -a              # Listar todos los paneles
     -s              # Listar todos los paneles de la sesión
     -t              # Listar paneles de la aplicación en el target

    kill-window      # Eliminar la ventana actual
     -t "#"          # Eliminar la ventana "#"
     -a              # Eliminar todas las ventanas
     -a -t "#"       # Eliminar todas las ventanas menos la "#"

    kill-session     # Eliminar la sesión actual
     -t "#"          # Eliminar la sesión "#"
     -a              # Eliminar todas las sessiones
     -a -t "#"       # Eliminar todas las sessiones menos la "#"

```


### Atajos de teclado

Para controlar una sesión atada se usa la combinación llamada 'Prefijo' + atajo.

```
----------------------------------------------------------------------
  (C-b) = Ctrl + b    # 'Prefijo' por defecto requerido para usar los atajos

  (M-1) = Meta + 1 -o- Alt + 1
----------------------------------------------------------------------

  ?                  # Listar todos los atajos de teclado
  :                  # Insertar un comando de tmux
  r                  # Forzar refresco gráfico del cliente
  c                  # Crear una nueva ventana

  !                  # Quitar el panel actual de la ventana
  %                  # Dividir el panel actual en dos (derecha e izquierda)
  "                  # Dividir el panel actual en dos (arriba y abajo)

  n                  # Cambiar a la siguiente ventana
  p                  # Cambiar a la ventana anterior
  {                  # Cambiar el panel por el panel anterior
  }                  # Cambiar el panel por el siguiente panel

  s                  # Seleccionar y atar el cliente a una sesión distinta
                     de forma interactiva
  w                  # Seleccionar una ventana de forma interactiva
  0 to 9             # Seleccionar una ventana (del 0 al 9)

  d                  # Desatar el cliente actual de la sesión
  D                  # Escojer un cliente a desatar

  &                  # Eliminar la ventana actual
  x                  # Eliminar el panel actual

  Up, Down           # Cambiar al panel de arriba, debajo, izquierda o derecha
  Left, Right

  M-1 to M-5         # Ordenar los paneles

  C-Up, C-Down       # Dimensionar el panel actual en pasos de una celda
  C-Left, C-Right

  M-Up, M-Down       # Dimensionar el panel actual en pasos de cinco celdas
  M-Left, M-Right

```


### Configurar ~/.tmux.conf

El fichero tmux.conf se puede configurar para establecer unas opciones por
defecto, igual que .vimrc o init.el para vim o emacs.

```
# Ejemplo tmux.conf
# 2014.10


### General
###########################################################################

# Activar UTF-8
setw -g utf8 on
set-option -g status-utf8 on

# Limite del historico de comandos
set -g history-limit 2048

# Index Start
set -g base-index 1

# Ratón
set-option -g mouse-select-pane on

# Forzar volver a cargar el fichero de configuración
unbind r
bind r source-file ~/.tmux.conf


### Atajos de teclado
###########################################################################

# Quitar C-b como prefijo por defecto
unbind C-b

# Establecer ` como nuevo prefijo
set-option -g prefix `

# Volver a la ventana anterior cuando el prefijo se pulsa dos veces
bind C-a last-window
bind ` last-window

# Intercambiar entre C-a y ` como prefijo pulsando F11/F12
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Preferencias de los atajos
setw -g mode-keys vi
set-option -g status-keys vi

# Mover entre paneles con atajos de vim
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Cambiar/Saltar de ventana
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# Divisiones de paneles
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

### Tema de colores
###########################################################################

# Barra de estado
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# Bordes de paneles
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Color de los mensajes
set-option -g message-fg black
set-option -g message-bg green

# Colores del estado de las ventanas
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-content-attr default
setw -g window-status-content-fg yellow
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### UI
###########################################################################

# Notificaciones
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# Titulos de las ventanas
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not)

# Formato de la barra de estado
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# Mostrar estadisticas de rendimiento en la barra de estado
# Requiere https://github.com/thewtex/tmux-mem-cpu-load/
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```


### Referencias

[Tmux | Home](http://tmux.sourceforge.net)

[Tmux Manual page](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)

[Display CPU/MEM % in statusbar](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)
---
language: TypeScript
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
filename: learntypescript-es.ts
translators:
    - ["Damaso Sanoja", "https://github.com/damasosanoja"]
lang: es-es
---

TypeScript es un lenguaje cuyo objetivo es facilitar el desarrollo de aplicaciones a gran escala escritas en JavaScript.
TypeScript añade conceptos comunes como clases, módulos, interfaces, genéricos y (opcionalmente) tipeo estático a JavaScript.
Es un superset de JavaScript: todo el código JavaScript es código válido en TypeScript de manera que se puede integrar fácilmente a cualquier proyecto . El compilador TypeScript emite JavaScript.

Este artículo se enfocará solo en la sintáxis extra de TypeScript, y no en [JavaScript] (../javascript/).

Para probar el compilador de TypeScript, diríjase al [Área de Pruebas] (http://www.typescriptlang.org/Playground) donde podrá tipear código, y ver como se auto-completa al tiempo que ve el código emitido JavaScript.

```js
// Existen 3 tipos básicos en TypeScript
var isDone: boolean = false;
var lines: number = 42;
var name: string = "Anders";

// Cuando es imposible de saber, tenemos el tipo "Any"
var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okey, definitivamente un boolean

// Para colecciones, hay matrices de tipos y matrices genéricas
var list: number[] = [1, 2, 3];
// Alternativamente, usando la matriz genérica
var list: Array<number> = [1, 2, 3];

// Para enumeradores:
enum Color {Red, Green, Blue};
var c: Color = Color.Green;

// Finalmente, "void" es usado para el caso especial de una función que no retorna nada
function bigHorribleAlert(): void {
  alert("I'm a little annoying box!");
}

// Las funciones son ciudadanos de primera clase, soportan la sintáxis lambda "fat arrow" y
// usan el tipo inferencia

// Lo siguiente es equivalante, la misma firma será inferida por el
// compilador, y el mismo JavaScript será emitido
var f1 = function(i: number): number { return i * i; }
// Retorna tipo inferido
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
// Retorna tipo inferido
var f4 = (i: number) => { return i * i; }
// Retorna tipo inferido, one-liner significa que no es necesario que regresen palabras claves
var f5 = (i: number) =>  i * i;

// Las interfaces son estructurales, todo lo que tenga las propiedades cumple con
// la interfase
interface Person {
  name: string;
  // Propiedades opcionales, marcadas con un "?"
  age?: number;
  // Y por supuesto funciones
  move(): void;
}

// Objeto que implementa la interfase "Persona"
// Puede ser tratada como Persona ya que posee las propiedades name y move
var p: Persona = { name: "Bobby", move: () => {} };
// Objetos que tienen propiedades opcionales:
var validPersona: Persona = { name: "Bobby", age: 42, move: () => {} };
// No es una persona porque su edad no es un número
var invalidPersona: Persona = { name: "Bobby", age: true };

// Las interfases también pueden describir un tipo de función
interface SearchFunc {
  (source: string, subString: string): boolean;
}
// Solo los tipos de parámetros son importantes, los nombres no son importantes.
var mySearch: SearchFunc;
mySearch = function(src: string, sub: string) {
  return src.search(sub) != -1;
}

// Clases - los miembros son públicos por defecto
class Point {
  // Properties
    x: number;

    // Constructor - las palabras clave public/private en este contexto generarán
    // un código boiler plate para la propiedad y la inicialización en el
    // constructor.
    // En este ejemplo, "y" debe ser definida al igual que "x" lo es, pero con menos código
    // También son soportados valores por defecto

    constructor(x: number, public y: number = 0) {
        this.x = x;
    }

    // Funciones
    dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

    // Miembros estáticos
    static origin = new Point(0, 0);
}

var p1 = new Point(10 ,20);
var p2 = new Point(25); //y será 0

// Herencia
class Point3D extends Point {
    constructor(x: number, y: number, public z: number = 0) {
        super(x, y); // Un llamado explícito al constructor de la super clase es indispensable
    }

    // Sobrescribir
    dist() {
        var d = super.dist();
        return Math.sqrt(d * d + this.z * this.z);
    }
}

// Módulos, los "." pueden ser usados como separadores para los submódulos
module Geometry {
  export class Square {
    constructor(public sideLength: number = 0) {
    }
    area() {
      return Math.pow(this.sideLength, 2);
    }
  }
}

var s1 = new Geometry.Square(5);

// Un alias local para referirse a un módulo
import G = Geometry;

var s2 = new G.Square(10);

// Genéricos
// Clases
class Tuple<T1, T2> {
    constructor(public item1: T1, public item2: T2) {
    }
}

// Interfases
interface Pair<T> {
    item1: T;
    item2: T;
}

// Y funciones
var pairToTuple = function<T>(p: Pair<T>) {
    return new Tuple(p.item1, p.item2);
};

var tuple = pairToTuple({ item1:"hello", item2:"world"});

// Incluyendo referencias a un archivo de definición:
/// <reference path="jquery.d.ts" />

```

## Para mayor información
 * [Sitio Oficial de TypeScript] (http://www.typescriptlang.org/)
 * [Especificaciones del lenguaje TypeScript (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg - Introduciendo TypeScript en Canal 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [Código fuente en GitHub] (https://github.com/Microsoft/TypeScript)
 * [Definitely Typed - repositorio para definiciones de tipo] (http://definitelytyped.org/)
---
category: tool
tool: vim
contributors:
    - ["RadhikaG", "https://github.com/RadhikaG"]
translators:
    - ["Ivan Alburquerque", "https://github.com/AlburIvan"]
lang: es-es
filename: LearnVim-es.txt
---


[Vim](http://www.vim.org)
(Vi IMproved) es un clón del popular editor vi para UNIX. Es un editor de texto
diseñado para ser veloz e incrementar la productividad, es ubicuo en la mayoría
de los sistemas basados en UNIX. Cuenta con numerosas combinaciones de teclas 
para la navegación rápida a puntos especificos en un archivo y para rápida edición.



## Fundamentos de la navegación en Vim

```
    vim <archivo>    # Abre <archivo> en vim
    :q               # Salir de vim
    :w               # Guardar archivo actual
    :wq              # Guardar archivo y salir de vim
    :q!              # Salir de vim sin guardar el archivo
                     # ! *forza* :q a ejecutarse, por lo tanto sale de vim sin guardar
    :x               # Guardar el archivo y salir de vim, versión corta de :wq

    u                # Deshacer
    CTRL+R           # Rehacer

    h                # Desplazarse un carácter hacía la izquierda
    j                # Desplazarse una línea hacía abajo
    k                # Desplazarse una línea hacía arriba
    l                # Desplazarse un carácter hacía la derecha

    # Desplazarse dentro de la línea 

    0                # Desplazarse hacia el inicio de la línea
    $                # Desplazarse al final de la línea
    ^                # Desplazarse al primer carácter no blanco en la línea

    # Buscando en el texto

    /word            # Resalta todas las ocurrencias de la palabra después del cursor
    ?word            # Resalta todas las ocurrencias de la palabra antes del cursor
    n                # Desplaza el cursor a la siguiente ocurrencia de la palabra después de una búsqueda
    N                # Desplaza el cursor a la anterior ocurrencia de la palabra

    :%s/foo/bar/g    # Cambia 'foo' a 'bar' en cada línea en el archivo
    :s/foo/bar/g     # Cambia 'foo' a 'bar' en la línea actual

    # Saltando caracteres

    f<carácter>     # Salta adelante y aterriza en <carácter>
    t<carácter>     # Salta adelante y aterriza antes de <carácter>

    # Por ejemplo,
    f<               # Salta adelante y aterriza en <
    t<               # Salta adelante y aterriza justo antes de <
    
    # Desplazarse por palabras

    w                # Desplazarse hacia adelante por una palabra
    b                # Desplazarse hacia atrás por una palabra
    e                # Desplazarse al final de la palabra actual

    # Otros caracteres para desplazarse

    gg               # Ir al principio del archivo
    G                # Ir al final del archivo
    :NUM             # Ir a la línea número NUM (NUM es cualquier número)
    H                # Desplazarse al principio de la pantalla
    M                # Desplazarse a la mitad de la pantalla
    L                # Desplazarse al final de la pantalla
```

## Modos:

Vim se basa en el concepto de **modos**.


Modo Comando    - Vim se pone en marcha en éste modo, se usa para navegar y escribir comandos    
Modo Inserción  - Utilizado para realizar cambios en el archivo    
Modo Visual     - Utilizado para resaltar texto y operar en ellos    
Modo Ex         - Utilizado para ir hacia la parte inferior con ':' para introducir comandos


```
    i                # Pone a Vim en modo de inserción, antes de la posición del cursor
    a                # Pone a Vim en modo de inserción, después de la posición del cursor
    v                # Pone a Vim en modo de visual
    :                # Pone a Vim en modo Ex
    <esc>            # Sale de cualquier modo en que se encuentre, al modo comando

    # Copiando y pegando texto

    y                # Copia lo que se encuentre seleccionado
    yy               # Copia la linea actual
    d                # Elimina lo que se encuentre seleccionado
    dd               # Elimina la linea actual
    p                # Pega el texto copiado después de la posición del cursor
    P                # Pega el texto copiado antes de la posición del cursor
    x                # Elimina el carácter debajo de la posición del cursor
```

## La "Gramática" de vim

Vim puede ser pensado como un conjunto de comandos en un
formato "verbo-sustantivo-modificador ', donde:

Verbo       - La acción a realizar    
Modificador - Como vas hacer la acción    
Sustantivo  - el objeto al que se le va a aplicar la acción

Algunos ejemplos importantes de "Verbos", "Modificadores" y "Sustantivos":

```
    # 'Verbos'
 
    d                # Eliminar
    c                # Cambiar
    y                # Copiar
    v                # Seleccionar visualmente

    # 'Modificadores'

    i                # Dentro
    a                # Alrededor
    NUM              # Número (NUM es cualquier número)
    f                # Busca algo y aterriza sobre el
    t                # Busca algo y se detiene antes de
    /                # Encuentra una cadena desde el cursor en adelante
    ?                # Encuentra una cadena antes del cursor

    # 'Sustantivos'

    w                # Palabra
    s                # Oración
    p                # Párrafo
    b                # Bloque
    
    # "Frases" de ejemplo o comandos

    d2w              # Elimina 2 palabras
    cis              # Cambia dentro de una oración
    yip              # Copia dentro de un párrafo (copia el párrafo donde estás)
    ct<              # Cambia para abrir un paréntesis
                     # Cambie el texto desde donde está a la siguiente paréntesis abierto
    d$               # Eliminar hasta el final de la línea
```

## Algunos accesos directos y trucos

```
    >                # Sangrar la selección por un bloque
    <                # Desangrar la selección por un bloque
    :earlier 15m     # Devuelve el documento de nuevo a como era hace 15 minutos
    :later 15m       # Deshace el comando anterior
    ddp              # Intercambia la posición de las lineas consecutivas, dd después p
    .                # Repite la acción previa
```

## Macros

Las macros son, básicamente, las acciones que se pueden grabar.
Cuando comienzas a grabar un macro, registra **todas** las acciones y comandos
que se utilizan hasta que detenga la grabación. En la invocación de un macro,
se aplica exactamente la misma secuencia de acciones y comandos de nuevo
en la selección de texto.

```
    qa               # Comienza a grabar un macro llamada 'a'
    q                # Detiene la grabación
    @a               # Comienza la reproducción del macro
```

### Configurando ~/.vimrc

El archivo .vimrc puede ser usado para configurar Vim en el arranque.

Aquí está un ejemplo de un archivo ~ / .vimrc:

```
" Ejemplo ~/.vimrc
" 2015.10 

" Se requiere para que vim sea 'mejor'
set nocompatible

" Determina la extensión del archivo por el nombre para permitir el auto-indentado inteligente, etc...
filetype indent plugin on

" Habilita el resaltado de sintaxis
syntax on

" Mejor terminación de línea de comandos
set wildmenu

" Usa búsqueda sensible a mayúsculas excepto cuando se utilizan letras mayúsculas
set ignorecase
set smartcase

" Al abrir una nueva línea, si la sangría especifica del archivo no está habilitada,
" mantén la misma sangría que la línea que estás actualmente
set autoindent

" Despliega el número de línea a la izquierda
set number

" Opciones de sangría, cambialas de acuerdo a tus preferencias personales

" Número de espacios visuales por tabulación
set tabstop=4

" Número de espacios de las tabulaciones al editar
set softtabstop=4

" Número de espacios sangrados cuando las operaciones de resangrado (>> y <<) son usadas
set shiftwidth=4

" Convertir tabulaciones en espacios
set expandtab

" Habilitar la tabulación inteligente y el espaciamiento para el sangrado y la alineación
set smarttab
```

### Referencias

[Vim | Home (EN)](http://www.vim.org/index.php)

`$ vimtutor` Command

[A vim Tutorial and Primer (EN)](https://danielmiessler.com/study/vim/)

[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread) (EN)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about)

[Arch Linux Wiki (EN)](https://wiki.archlinux.org/index.php/Vim)---
language: Visual Basic
contributors:
    - ["Brian Martin", "http://brianmartin.biz"]
translators:
    - ["Adolfo Jayme Barrientos", "https://github.com/fitojb"]
author: Brian Martin
author_url: https://github.com/fitojb
filename: learnvisualbasic-es.vb
lang: es-es
---

```vb
Module Module1

    Sub Main()
        ' Un vistazo rápido a las aplicaciones de consola de Visual Basic antes
        ' de que profundicemos en el tema.
        ' El apóstrofo inicia una línea de comentario.
        ' Para explorar este tutorial dentro del Compilador de Visual Basic,
        ' he creado un sistema de navegación.
        ' Dicho sistema se explicará a medida que avancemos en este
        ' tutorial; gradualmente entenderás lo que significa todo.
        Console.Title = ("Aprende X en Y minutos")
        Console.WriteLine("NAVEGACIÓN") 'Mostrar 
        Console.WriteLine("")
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine("1. Salida «Hola, mundo»")
        Console.WriteLine("2. Entrada «Hola, mundo»")
        Console.WriteLine("3. Calcular números enteros")
        Console.WriteLine("4. Calcular números decimales")
        Console.WriteLine("5. Una calculadora funcional")
        Console.WriteLine("6. Uso de bucles «Do While»")
        Console.WriteLine("7. Uso de bucles «For While»")
        Console.WriteLine("8. Declaraciones condicionales")
        Console.WriteLine("9. Selecciona una bebida")
        Console.WriteLine("50. Acerca de")
        Console.WriteLine("Elige un número de la lista anterior")
        Dim selection As String = Console.ReadLine
        Select Case selection
            Case "1" 'Salida «hola, mundo»
                Console.Clear() 'Limpia la consola y abre la subrutina privada
                SalidaHolaMundo() 'Abre la subrutina privada nombrada
            Case "2" 'Entrada «hola, mundo»
                Console.Clear()
                EntradaHolaMundo()
            Case "3" 'Calcular números enteros 
                Console.Clear()
                CalcularNumerosEnteros()
            Case "4" 'Calcular números decimales
                Console.Clear()
                CalcularNumerosDecimales()
            Case "5" 'Una calculadora funcional
                Console.Clear()
                CalculadoraFuncional()
            Case "6" 'Uso de bucles «Do While»
                Console.Clear()
                UsoBuclesDoWhile()
            Case "7" 'Uso de bucles «For While»
                Console.Clear()
                UsoBuclesFor()
            Case "8" 'Declaraciones condicionales
                Console.Clear()
                DeclaracionCondicional()
            Case "9" 'Declaración «If/Else»
                Console.Clear()
                DeclaracionIfElse() 'Selecciona una bebida
            Case "50" 'Cuadro de mensaje «Acerca de»
                Console.Clear()
                Console.Title = ("Aprende X en Y minutos :: Acerca de")
                MsgBox("Tutorial escrito por Brian Martin (@BrianMartinn")
                Console.Clear()
                Main()
                Console.ReadLine()

        End Select
    End Sub

    'Uno - He usado números para guiarme por el sistema de navegación anterior
    'cuando regrese posteriormente a implementarlo.

    'Usamos subrutinas privadas para separar distintas secciones del programa. 
    Private Sub SalidaHolaMundo()
        'Título de la aplicación de consola
        Console.Title = "Salida «Hola, mundo» | Aprende X en Y minutos"
        'Usa Console.Write("") o Console.WriteLine("") para mostrar salidas.
        'Seguido por Console.Read(), o bien, Console.Readline()
        'Console.ReadLine() muestra la salida en la consola.
        Console.WriteLine("Hola, mundo")
        Console.ReadLine()
    End Sub

    'Dos
    Private Sub EntradaHolaMundo()
        Console.Title = "«Hola, mundo, soy...» | Aprende X en Y minutos"
        ' Variables
        ' Los datos que introduzca un usuario deben almacenarse.
        ' Las variables también empiezan por Dim y terminan por As VariableType.

        ' En este tutorial queremos conocer tu nombre y hacer que el programa
        ' responda a este.
        Dim nombredeusuario As String
        'Usamos «string» porque es una variable basada en texto.
        Console.WriteLine("Hola, ¿cómo te llamas? ") 'Preguntar nombre de usuario.
        nombredeusuario = Console.ReadLine() 'Almacenar nombre del usuario.
        Console.WriteLine("Hola, " + nombredeusuario) 'La salida es Hola, nombre
        Console.ReadLine() 'Muestra lo anterior.
        'El código anterior te hará una pregunta y mostrará la respuesta.
        'Entre otras variables está Integer, la cual usaremos para números enteros.
    End Sub

    'Tres
    Private Sub CalcularNumerosEnteros()
        Console.Title = "Calcular números enteros | Aprende X en Y minutos"
        Console.Write("Primer número: ") 'Escribe un núm. entero, 1, 2, 104, etc
        Dim a As Integer = Console.ReadLine()
        Console.Write("Segundo número: ") 'Escribe otro número entero.
        Dim b As Integer = Console.ReadLine()
        Dim c As Integer = a + b
        Console.WriteLine(c)
        Console.ReadLine()
        'Lo anterior es una calculadora sencilla
    End Sub

    'Cuatro
    Private Sub CalcularNumerosDecimales()
        Console.Title = "Calcular con tipo doble | Aprende X en Y minutos"
        'Por supuesto, nos gustaría sumar decimales.
        'Por ello podríamos cambiar del tipo Integer al Double.

        'Escribe un número fraccionario, 1.2, 2.4, 50.1, 104.9 etc
        Console.Write("Primer número: ")
        Dim a As Double = Console.ReadLine
        Console.Write("Segundo número: ") 'Escribe el segundo número.
        Dim b As Double = Console.ReadLine
        Dim c As Double = a + b
        Console.WriteLine(c)
        Console.ReadLine()
        'Este programa puede sumar 1.1 y 2.2
    End Sub

    'Cinco
    Private Sub CalculadoraFuncional()
        Console.Title = "La calculadora funcional | Aprende X en Y minutos"
        'Pero si quieres que la calculadora reste, divida, multiplique y
        'sume.
        'Copia y pega lo anterior.
        Console.Write("Primer número: ")
        Dim a As Double = Console.ReadLine
        Console.Write("Segundo número: ")
        Dim b As Integer = Console.ReadLine
        Dim c As Integer = a + b
        Dim d As Integer = a * b
        Dim e As Integer = a - b
        Dim f As Integer = a / b

        'Mediante las líneas siguientes podremos restar,
        'multiplicar y dividir los valores a y b
        Console.Write(a.ToString() + " + " + b.ToString())
        'Queremos dar un margen izquierdo de 3 espacios a los resultados.
        Console.WriteLine(" = " + c.ToString.PadLeft(3))
        Console.Write(a.ToString() + " * " + b.ToString())
        Console.WriteLine(" = " + d.ToString.PadLeft(3))
        Console.Write(a.ToString() + " - " + b.ToString())
        Console.WriteLine(" = " + e.ToString.PadLeft(3))
        Console.Write(a.ToString() + " / " + b.ToString())
        Console.WriteLine(" = " + f.ToString.PadLeft(3))
        Console.ReadLine()

    End Sub

    'Seis
    Private Sub UsoBuclesDoWhile()
        'Igual que la subrutina privada anterior
        'Esta vez preguntaremos al usuario si quiere continuar (¿sí o no?)
        'Usamos el bucle Do While porque no sabemos si el usuario quiere
        'usar el programa más de una vez.
        Console.Title = "Uso de bucles «Do While» | Aprende X en Y minutos"
        Dim respuesta As String 'Usamos la variable «String» porque la resp. es texto
        Do 'Comenzamos el programa con
            Console.Write("Primer número: ")
            Dim a As Double = Console.ReadLine
            Console.Write("Segundo número: ")
            Dim b As Integer = Console.ReadLine
            Dim c As Integer = a + b
            Dim d As Integer = a * b
            Dim e As Integer = a - b
            Dim f As Integer = a / b

            Console.Write(a.ToString() + " + " + b.ToString())
            Console.WriteLine(" = " + c.ToString.PadLeft(3))
            Console.Write(a.ToString() + " * " + b.ToString())
            Console.WriteLine(" = " + d.ToString.PadLeft(3))
            Console.Write(a.ToString() + " - " + b.ToString())
            Console.WriteLine(" = " + e.ToString.PadLeft(3))
            Console.Write(a.ToString() + " / " + b.ToString())
            Console.WriteLine(" = " + f.ToString.PadLeft(3))
            Console.ReadLine()
            'Preguntar si el usuario quiere continuar. Desafortunadamente,
            'distingue entre mayúsculas y minúsculas.
            Console.Write("¿Quieres continuar? (s / n)")
            'El programa toma la variable, la muestra y comienza de nuevo.
            respuesta = Console.ReadLine
        'La orden que hará funcionar esta variable es en este caso «s»
        Loop While respuesta = "s"

    End Sub

    'Siete
    Private Sub UsoBuclesFor()
        'A veces el programa debe ejecutarse solo una vez.
        'En este programa contaremos a partir de 10.

        Console.Title = "Uso de bucles «For» | Aprende X en Y minutos"
        'Declarar Variable y desde qué número debe contar en Step -1,
        'Step -2, Step -3, etc.
        For i As Integer = 10 To 0 Step -1 
            Console.WriteLine(i.ToString) 'Muestra el valor del contador
        Next i 'Calcular el valor nuevo
        Console.WriteLine("Iniciar") '¡¡Comencemos el programa, nene!!
        Console.ReadLine() '¡¡ZAS!! - Quizá me he emocionado bastante :)
    End Sub

    'Ocho
    Private Sub DeclaracionCondicional()
        Console.Title = "Declaraciones condicionales | Aprende X en Y minutos"
        Dim nombredeUsuario As String = Console.ReadLine
        Console.WriteLine("Hola, ¿cómo te llamas? ") 'Preguntar nombre de usuario.
        nombredeUsuario = Console.ReadLine() 'Almacena el nombre de usuario.
        If nombredeUsuario = "Adam" Then
            Console.WriteLine("Hola, Adam")
            Console.WriteLine("Gracias por crear este útil sitio web")
            Console.ReadLine()
        Else
            Console.WriteLine("Hola, " + nombredeUsuario)
            Console.WriteLine("¿Has visitado www.learnxinyminutes.com?")
            Console.ReadLine() 'Termina y muestra la declaración anterior.
        End If
    End Sub

    'Nueve
    Private Sub DeclaracionIfElse()
    Console.Title = "Declaración «If / Else» | Aprende X en Y minutos"
        'A veces es importante considerar más de dos alternativas.
        'A veces, algunas de estas son mejores.
        'Cuando esto sucede, necesitaríamos más de una declaración «if».
        'Una declaración «if» es adecuada para máquinas expendedoras.
        'En las que el usuario escribe un código (A1, A2, A3) para elegir.
        'Pueden combinarse todas las elecciones en una sola declaración «if».

        Dim seleccion As String = Console.ReadLine 'Valor de la selección
            Console.WriteLine("A1. para 7Up")
            Console.WriteLine("A2. para Fanta")
            Console.WriteLine("A3. para Dr. Pepper")
            Console.WriteLine("A4. para Coca-Cola")
            Console.ReadLine()
            If selection = "A1" Then
                Console.WriteLine("7up")
                Console.ReadLine()
            ElseIf selection = "A2" Then
                Console.WriteLine("fanta")
                Console.ReadLine()
            ElseIf selection = "A3" Then
                Console.WriteLine("dr. pepper")
                Console.ReadLine()
            ElseIf selection = "A4" Then
                Console.WriteLine("coca-cola")
                Console.ReadLine()
            Else
                Console.WriteLine("Selecciona un producto")
                Console.ReadLine()
            End If

    End Sub

End Module

```

## Referencias

Aprendí Visual Basic en la aplicación de consola. Esta me permitió entender los principios de la programación para, posteriormente, aprender otros lenguajes con facilidad.

He creado un <a href="http://www.vbbootcamp.co.uk/" Title="Tutorial de Visual Basic">tutorial de Visual Basic</a> más exhaustivo para quienes quieran saber más.

Toda la sintaxis es válida. Copia el código y pégalo en el compilador de Visual Basic y ejecuta (F5) el programa. 
---
language: whip
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
translators:
    - ["Daniel Zendejas", "https://github.com/DanielZendejas"]
author: Tenor Biel
author_url: http://github.com/L8D
filename: whip-es.lisp
lang: es-es
---
Tutorial de Whip en español.

Whip es un dialecto de LISP hecho para escribir código y conceptos
simples. Ha tomado prestado bastante de la sintaxis de Haskell
(un lenguaje no relacionado).

Esta documentación fue escrita por el creador del lenguaje

```scheme
; Los comentarios son como en LISP, con punto y coma...

; La mayoría de las sentencias de primer nivel están dentro de
; "formas". Una forma no es más que cosas dentro de paréntesis
no_en_la_forma
(en_la_form)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 1. Números, Strings y Operadores

;Whip tiene un tipo para números (es el estándar 64-bit IEEE 754 double, de JS)
3 ; => 3
1.5 ; => 1.5

; Las funciones son llamadas si son el primer elemento de una forma
(funcion_llamada argumentos)

; La mayoría de los operadores se hacen con funciones
; Toda la aritmética básica es bastante estándar
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2
; incluso el módulo
(% 9 4) ; => 1
; división impar al estilo de JavaScript.
(/ 5 2) ; => 2.5

; Las formas anidadas funcionan como se espera.
(* 2 (+ 1 3)) ; => 8

; Hay un tipo booleano.
true
false

; Los Strings son creados con comillas dobles ".
"Hola mundo"

; Los caracteres solos se declaran con comillas simples '.
'a'

; La negación usa la función 'not'.
(not true) ; => false
(not false) ; => true

; La mayoría de las funcions que no vienen de Haskell tienen
; atajos. La función 'not' también se puede declarar con '!'.
(! (! true)) ; => true

; La igualdad es `equal` o `=`.
(= 1 1) ; => true
(equal 2 1) ; => false

; Por ejemplo, la desigualdad sería combinar la función 'not' con
; la función de igualdad
(! (= 2 1)) ; => true

; Más comparaciones
(< 1 10) ; => true
(> 1 10) ; => false
; y su contraparte textual.
(lesser 1 10) ; => true
(greater 1 10) ; => false

; Los Strings pueden concatenarse con la función +.
(+ "Hola " "mundo!") ; => "Hello world!"

; También puedes usar las comparativas de JavaScript
(< 'a' 'b') ; => true
; ...y la coerción de tipos
(= '5' 5)

; La función 'at' o @ accesa a los caracteres dentro de los strings,
; empezando en 0.
(at 0 'a') ; => 'a'
(@ 3 "foobar") ; => 'b'

; También están las variables  `null` and `undefined`.
null; usado para indicar una falta de valor deliberada.
undefined; usado para indicar un valor que aún no está definido.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 2. Variables, Listas y Diccionarios

; Las variables son declaradas con las funciones `def` o `let`.
; Las variables que aún no son asignadas tendrán el valor `undefined`.
(def mi_variable 5)
; `def` asignará la variable al contexto global.
; `let` asignará la variable al contexto local,  
; y tiene una sintaxis distinta.
(let ((mi_variable 5)) (+ mi_variable 5)) ; => 10
(+ mi_variable 5) ; = undefined + 5 => undefined

; Las listas son arreglos de valores de cualquier tipo.
; Básicamente, son formas sin funciones al inicio.
(1 2 3) ; => [1, 2, 3] (sintaxis JavaScript)

; Los diccionarios son el equivalente en Whip de los 'objetos' de JavaScript,
; los 'dicts' de Python o los 'hashes' de Ruby: una colección desordenada
; de pares llave-valor
{"llave1" "valor1" "llave2" 2 3 3}

; Las llaves son sólo valores, identificadores, números o strings.
(def mi_diccionario {mi_llave "mi_valor" "mi otra llave" 4})
; Pero con Whip, los diccionarios son leidos así:
; "llave" "espacio en blanco" "valor" "espacio en blanco"
{"llave" "valor"
"otra llave"
1234
}

; Las definiciones de los diccionarios pueden accesarse con la función @
; (como los strings y las listas)
(@ "mi otra llave" mi_diccionario) ; => 4

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 3. Logica y secuencias de control

; La funcion `if` es bastante simple, aunque distinta que en otros lenguajes.
(if true "regresa esto si es true" "regresa esto si es false")
; => "regresa esto si es true"

; Y para el operador ternario `?`
(? false true false) ; => false

? `both` es un 'y' lógico, mientras que la función `either` es un 'o'.
(both true true) ; => true
(both true false) ; => false
(either true false) ; => true
(either false false) ; => false
; Y sus atajos son '&' y '^' respectivamente
; & => both
; ^ => either
(& true true) ; => true
(^ false true) ; => true

;;;;;;;;;
; Lambdas

; Las Lambdas en Whip son declaradas con las funciones `lambda` o `->`.
; Las funciones regulares en realidad sólo son lambdas con nombre.
(def mi_funcion (-> (x y) (+ (+ x y) 10)))
;         |       |   |         |
;         |       |   |    valor regresado(estas son las variables argumentos)
;         |       | argumentos
;         | declaración de lambda
;         |
;   nombre de la lambda

(mi_funcion 10 10) ; = (+ (+ 10 10) 10) => 30

; Obviamente, todas las lambdas por definición son anónimas y
; técnicamente siempre usadas anónimamente. Redundancia.
((lambda (x) x) 10) ; => 10

;;;;;;;;;;;;;;;;
; Comprensiones

; `range` o `..` genera una lista de números que comprende
; cada entero dentro de los argumentos.
(range 1 5) ; => (1 2 3 4 5)
(.. 0 2)    ; => (0 1 2)

; `map` aplica su primer argumento (que debe ser una función)
; al siguiente argumento (que es una lista).
(map (-> (x) (+ x 1)) (1 2 3)) ; => (2 3 4)

; Reducir
(reduce + (.. 1 5))
; equivale a
((+ (+ (+ 1 2) 3) 4) 5)

; Nota: map y reduce no tienen atajos.

; `slice` o `\` es idéntico a la función .slice() de JavaScript
; Pero toma la lista del primer argumento, no del último.
(slice (.. 1 5) 2) ; => (3 4 5)
(\ (.. 0 100) -5) ; => (96 97 98 99 100)

; `append` o `<<` se explica solo.
(append 4 (1 2 3)) ; => (1 2 3 4)
(<< "bar" ("foo")) ; => ("foo" "bar")

; Length se explica solo.
(length (1 2 3)) ; => 3
(_ "foobar") ; => 6

;;;;;;;;;;;;;;;
; Elementos de Haskell

; Primer elemento en una lista
(head (1 2 3)) ; => 1

; Lista del segundo elemento al último en una lista
(tail (1 2 3)) ; => (2 3)

; Último elemento en una lista
(last (1 2 3)) ; => 3

; Contrario a `tail`
(init (1 2 3)) ; => (1 2)

; Lista del primer elemento al argumento
(take 1 (1 2 3 4)) ; (1 2)

; Contrario a `take`
(drop 1 (1 2 3 4)) ; (3 4)

; Valor más pequeño de una lista
(min (1 2 3 4)) ; 1

; Valor más grande de una lista
(max (1 2 3 4)) ; 4

; Comprobar que el elemento está en la lista
(elem 1 (1 2 3)) ; true
(elem "foo" {"foo" "bar"}) ; true
(elem "bar" {"foo" "bar"}) ; false

; Invertir el orden de la lista
(reverse (1 2 3 4)) ; => (4 3 2 1)

; Comprobar si un elemento es par o impar
(even 1) ; => false
(odd 1) ; => true

; Separar string en una lista de strings, separados por espacios
(words "foobar nachos cheese") ; => ("foobar" "nachos" "cheese")
; Juntar lista de strings.
(unwords ("foo" "bar")) ; => "foobar"
(pred 21) ; => 20
(succ 20) ; => 21
```

Para más información, revisa el [repositorio](http://github.com/L8D/whip)
---
language: wolfram
lang: es-es
contributors:
    - ["Daniel Caballero", "http://github.com/danielcaballero796/"]
filename: learnwolfram-es.md
---

Wolfram es un lenguaje subyacente originalmente utilizado en Mathematica, pero ahora esta disponible para su uso en múltiples contextos.

El lenguaje de Wolfram tiene varias interfaces:
* La interfaz de línea de comandos kernel de Raspberry Pi (recién llamada _The Wolfram Language_) que se ejecuta interactivamente y no puede producir entrada gráfica.
* _Mathematica_ que es un texto rico / editor de matemáticas con Wolfram interactivo construido: presionando shift + Return en una "celda de código" crea una celda de salida con el resultado, que no es dinámica
* _Wolfram Workbench_ la cual es la interfaz de Eclipse del lenguaje Wolfram

El código de este ejemplo se puede escribir en cualquier interfaz y editarlo con Wolfram Workbench. Cargar directamente en Matematica puede resultar incómodo porque el archivo no contiene información de formato de celda (lo que haría que el archivo sea un desastre enorme para ser leído como texto) - puede ser visto / editado pero tal vez requerira algún ajuste.

```
(* Esto es un comentario *)

(* En Mathematica en lugar de utilizar estos comentarios, puede crear una celda de texto
   Y anotar su código con texto e imágenes bien escritas *)

(* Escribe una expresión devuelve el resultado *)
2*2              (* 4 *)
5+8              (* 13 *)

(* Llamada de función *)
(* Nota, los nombres de funciones (y todo lo demás) distingue entre mayúsculas y minúsculas *)
Sin[Pi/2]        (* 1 *)

(* Sintaxis alternativa para la Llamada de una función con un parámetro *)
Sin@(Pi/2)       (* 1 *)
(Pi/2) // Sin    (* 1 *)

(* Cada sintaxis en WL tiene algún equivalente como una llamada de función *)
Veces[2, 2]      (* 4 *)
Mas[5, 8]       (* 13 *)

(* El uso de una variable por primera vez lo define y lo hace global *)
x = 5             (* 5 *)
x == 5            (* verdadero, Estilo-C asignación y pruebas de igualdad *)
x                 (* 5 *)
x = x + 5         (* 10 *)
x                 (* 10 *)
Modifique[x, 20]  (* No estaba bromeando cuando dije que TODO tiene una función equivalente *)
x                 (* 20 *)

(* Debido a que WL se basa en un sistema de álgebra computacional, *)
(* El uso de variables indefinidas está bien, simplemente obstruyen la evaluación *)
cow + 5          (* 5 + cow, cow es indefinido por lo que no puede evaluar más *)
cow + 5 + 10     (* 15 + cow, Va a evaluar lo que puede *)
%                (* 15 + cow, % Busca el último retorno *)
% - cow          (* 15, cow variable indefinida cancelada *)
moo = cow + 5    (* Cuidado, moo ahora tiene una expresión, no un número! *)

(* Definición de una función *)
Double[x_] := x * 2    (* Nota: = para evitar la evaluación inmediata del RHS
                          y después de x para indicar que no hay restricciones de concordancia de patrones *)
Double[10]             (* 20 *)
Double[Sin[Pi/2]]      (* 2 *)
Double @ Sin @ (Pi/2)  (* 2, @-Sintaxis evita colas de paréntesis *)
(Pi/2) // Sin // Double(* 2, //-Sintaxis lista las funciones en orden de ejecución *)

(* Para un uso de programación de estilo imperativo; Para separar declaraciones *)
(* Descarta cualquier salida de LHS y ejecuta RHS *)
miPrimero[] := (Print@"Hola"; Print@"Mundo")  (* Tenga en cuenta que los padres externos son críticos
                                                ; La precedencia es menor que := *)
miPrimero[]                                    (* Hola Mundo *)

(* Estilo-C para bucle *)
PrintTo[x_] := For[y=0, y<x, y++, (Print[y])]  (* Start, test, incr, body *)
PrintTo[5]                                     (* 0 1 2 3 4 *)

(* bucle *)
x = 0; While[x < 2, (Print@x; x++)]     (* Ciclo con prueba y cuerpo *)

(* Si y condicionales *)
x = 8; If[x==8, Print@"Yes", Print@"No"]   (* Condición, Caso verdadero, Caso distinto*)
Switch[x, 2, Print@"Two", 8, Print@"Yes"]  (* Interruptor de estilo de coincidencia de valor *)
Which[x==2, Print@"No", x==8, Print@"Yes"] (* estilo de caso distinto *)

(* Las variables distintas de los parámetros son globales por defecto, incluso dentro de las funciones *)
y = 10             (* 10, Variable global y *)
PrintTo[5]         (* 0 1 2 3 4 *)
y                  (* 5, Global por contador de bucle dentro de PrintTo *)
x = 20             (* 20, Variable global x *)
PrintTo[5]         (* 0 1 2 3 4 *)
x                  (* 20, x en PrintTo Es un parámetro y automáticamente local *)

(* Las variables locales se declaran utilizando la metafunción del módulo *)
(* Version con variable local*)
BetterPrintTo[x_] := Module[{y}, (For[y=0, y<x, y++, (Print@y)])]
y = 20             (* Variable global y *)
BetterPrintTo[5]   (* 0 1 2 3 4 *)
y                  (* 20, eso es mejor *)

(* El módulo realmente nos permite declarar cualquier ámbito que nos guste *)
Module[{count}, count=0;        (* Declare el alcance de este recuento de variables *)
  (IncCount[] := ++count);      (* Estas funciones están dentro de ese ámbito *)
  (DecCount[] := --count)]
count              (* count - Variable global count no esta definida *)
IncCount[]         (* 1, usando count variable dentro del alcance *)
IncCount[]         (* 2, incCount lo actualiza *)
DecCount[]         (* 1, decCount tambien lo hace *)
count              (* count - Aún ninguna variable global con ese nombre*)

(* listas *)
miLista = {1, 2, 3, 4}     (* {1, 2, 3, 4} *)
miLista[[1]]               (* 1 - Los índices de la lista de notas comienzan en 1, no 0 *)
Map[Double, miLista]       (* {2, 4, 6, 8} - Lista de estilo funcional mapa función *)
Double /@ miLista          (* {2, 4, 6, 8} - Sintaxis abreviada para arriba *)
Scan[Print, miLista]       (* 1 2 3 4 - Lista de bucle sobre estilo imperativo *)
Fold[Plus, 0, miLista]     (* 10 (0+1+2+3+4) *)
FoldList[Plus, 0, miLista] (* {0, 1, 3, 6, 10} - Guardar los resultados intermedios *)
Append[miLista, 5]         (* {1, 2, 3, 4, 5} - Note que miLista no está actualizada *)
Prepend[miLista, 5]        (* {5, 1, 2, 3, 4} - añada "miLista = " Si quieres que lo sea *)
Join[miLista, {3, 4}]      (* {1, 2, 3, 4, 3, 4} *)
miLista[[2]] = 5          (* {1, 5, 3, 4} - Esto actualiza miLista *)

(* Asociaciones, aka Diccionarios /Hashes *)
miHash = <|"Green" -> 2, "Red" -> 1|>   (* crea una asociación *)
miHash[["Green"]]                       (* 2, uselo *)
miHash[["Green"]] := 5                  (* 5, actualizalo *)
miHash[["Puce"]] := 3.5                 (* 3.5, extiendalo *)
KeyDropFrom[miHash, "Verde"]            (* Limpia la llave Verde *)
Claves[miHash]                            (* {Rojo} *)
Valores[miHash]                          (* {1} *)

(* Y no puedes hacer ninguna demo de Wolfram sin mostrar esto *)
Manipular[y^2, {y, 0, 20}] (* Devuelve una interfaz de usuario reactiva que muestra y ^ 2
                               Y permite ajustar y entre 0-20 con un deslizador.
                               Sólo funciona en interfaces gráficas *)
```

##Listo para mas?

* [Centro de Documentación](http://reference.wolfram.com/language/)
---
language: xml
filename: learnxml-es.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Daniel Zendejas", "https://github.com/DanielZendejas"]
lang: es-es
---
XML es un lenguaje diseñado para guardar y transportar datos

A diferencia de HTML, XML no especifica cómo desplegar la información,
sólo la guarda.

* Sintaxis XML

```xml
<!-- Los comentarios en XML son de esta forma -->

<?xml version="1.0" encoding="UTF-8"?>
<tiendaDeLibros>
  <libro categoria="COCINA">
    <titulo lenguaje="en">Everyday Italian</titulo>
    <autor>Giada De Laurentiis</autor>
    <anio>2005</anio>
    <precio>30.00</precio>
  </libro>
  <libro categoria="INFANTES">
    <titulo lenguaje="en">Harry Potter</titulo>
    <autor>J K. Rowling</autor>
    <anio>2005</anio>
    <precio>29.99</precio>
  </libro>
  <libro categoria="WEB">
    <titulo lenguaje="en">Learning XML</titulo>
    <autor>Erik T. Ray</autor>
    <anio>2003</anio>
    <precio>39.95</precio>
  </libro>
</tiendaDeLibros>

<!-- Este es un archivo típico de XML.
  Empieza con una declaración de metadatos (opcional).
  
  XML usa una estructura de árbol. El nodo raíz es 'tiendaDeLibros', el cual
  tiene tres nodos hijos, todos llamados 'libros'. 
  Esos nodos tienen más nodos hijos, y así continúa... 
  
  Los nodos son creados usando tags que abren y cierran, y los hijos 
  son sólo nodos entre estas tags.-->


<!-- XML guarda dos tipos de datos:
  1 - Atributos -> Son los metadatos de un nodo.
	  Usualmente el parseador XML usa esta información para guardar los datos
	  apropiadamente. Aparecen con el formato (nombre="valor") dentro de la
	  tag que abre.
  2 - Elementos -> Ese es el dato puro.
  	  Eso es lo que el parseador recuperará del archivo XML.
  	  Los elementos aparecen entre las tags que abren y cierran.-->
      
  
<!-- Debajo, un elemento con dos atributos. -->
<archivo tipo="gif" id="4293">computer.gif</archivo>


```

* Documentos con buen formato x Validación

Un documento XML está bien formado cuando es sintácticamente correcto.
Aún esto, es posible inyectar más restricciones en el documento,
usando definiciones de documento, así como DTD o XML Schemas.

Un documento XML que sigue a una definición de documento (un esquema) es
válida. 

Con esta herramienta puedes validar datos XML fuera de la aplicación

```xml

<!-- Debajo puedes encontrar una versión simplificada del documento 
  tiendaDeLibros en adición a la definición DTD.-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "tiendaDeLibros.dtd">
<tiendaDeLibros>
  <libro categoriq="COCINA">
    <titulo>Everyday Italian</titulo>
    <precio>30.00</precio>
  </libro>
</tiendaDeLibros>

<!-- El DTD de este documento podría verse algo así:-->

<!DOCTYPE note
[
<!ELEMENT tiendaDeLibros (libro+)>
<!ELEMENT libro (titulo,precio)>
<!ATTLIST libro categoria CDATA "Literatura">
<!ELEMENT titulo (#PCDATA)>
<!ELEMENT precio (#PCDATA)>
]>

<!--El DTD empieza con una declaración.
	Después el nodo raíz es declarado, requiriendo 1 o más nodos 'libro'
	Cada 'libro' debe contener exactamente un 'titulo' y un 'precio' y
	un atributo llamado 'categoria', con "Literatura" como su valor 
	default.
	Los nodos 'titulo' y 'precio' contienen datos de caracteres
	parseados.
	
	El DTD puede ser declarado dentro del XML mismo.-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT tiendaDeLibros (libro+)>
<!ELEMENT libro (titulo,precio)>
<!ATTLIST libro categoria CDATA "Literatura">
<!ELEMENT titulo (#PCDATA)>
<!ELEMENT precio (#PCDATA)>
]>
<tiendaDeLibros>
  <libro categoriq="COCINA">
    <titulo>Everyday Italian</titulo>
    <precio>30.00</precio>
  </libro>
</tiendaDeLibros>
```
---
language: yaml
lang: es-es
filename: learnyaml-es.yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
  - ["Everardo Medina","https://github.com/everblut"]
translators:
  - ["Daniel Zendejas","https://github.com/DanielZendejas"]
---
Tutorial de YAML en español.

YAML es un lenguaje de serialización de datos diseñado para ser
leído y escrito por humanos.

Basa su funcionalidad en JSON, con la adición de líneas nuevas
e indentación inspirada en Python. A diferencia de Python, YAML
no permite tabulaciones literales.

```yaml
# Los comentarios en YAML se ven así.

###################
# TIPOS ESCALARES #
###################

# Nuestro objeto raíz (el cual es el mismo a lo largo de todo el
# documento) será un mapa, equivalente a un diccionario, hash,
# u objeto en otros lenguajes.

llave: valor
otra_llave: Otro valor
un_valor_numerico: 100
notacion_cientifica: 1e+12
booleano: true
valor_nulo: null
llave con espacios: valor
# Nótese que los strings no deben estar entre comillas, aunqué también es válido.
llave: "Un string, entre comillas."
"Las llaves tambien pueden estar entre comillas.": "valor entre comillas"

# Los strings de líneas múltiples pueden ser escritos
# como un 'bloque literal' (usando pipes |)
# o como un 'bloque doblado' (usando >)

bloque_literal: |
	Este bloque completo de texto será preservado como el valor de la llave
	'bloque_literal', incluyendo los saltos de línea.

	Se continúa guardando la literal hasta que se cese la indentación.
		Cualquier línea que tenga más indentación, mantendrá los espacios dados
		(por ejemplo, estas líneas se guardarán con cuatro espacios)

bloque_doblado: >
	De la misma forma que el valor de 'bloque_literal', todas estas
	líneas se guardarán como una sola literal, pero en esta ocasión todos los
	saltos de línea serán reemplazados por espacio.

	Las líneas en blanco, como la anterior, son convertidas a un salto de línea.

        Las líneas con mayor indentación guardan sus saltos de línea.
        Esta literal ocuparán dos líneas.

# La indentación se usa para anidar elementos
un_mapa_indentado:
    llave: valor
    otra_llave: otro valor
    otro_mapa_indentado:
        llave_interna: valor_interno

# Las llaves de los mapas no requieren ser strings necesariamente
0.25: una llave numérica

# Las llaves también pueden ser objetos de multiples líneas,
# usando ? para indicar el inicio de una llave
? |
    Esto es una llave
    que tiene múltiples líneas
: y este es su valor

########################
# TIPOS DE COLECCIONES #
########################

# Las colecciones en YAML usan la indentación para delimitar el alcance
# y cada elemento de la colección inicia en su propia línea.
# YAML tambien permite colecciones como llaves, pero muchos lenguajes de
# programación se quejarán.

# Las secuencias (equivalentes a listas o arreglos) se ven así:
- Amarillo
- Verde
- Azul

# Se puede usar una secuencia como valor para una llave.
secuencia:
    - Elemento 1
    - Elemento 2
    - Elemento 3
    - Elemento 4

# Las secuencias pueden contener secuencias como elementos.
- [Uno, Dos, Tres]
- [Domingo, Lunes, Martes]
- [Luna, Marte, Tierra]

# Las secuencias pueden tener distintos tipos en su contenido.
secuencia_combinada:
    - texto
    - 5
    - 0.6
    - llave: valor # se convierte en un json dentro de la secuencia
    -
        - Esta es una secuencia
        - ...dentro de otra secuencia

# Dado que todo JSON está incluído dentro de YAML, también puedes escribir
# mapas con la sintaxis de JSON y secuencias:
mapa_de_json_1: {"llave": "valor"}
mapa_de_json_2:
  llave: valor

# Las secuencias tambien se pueden escribir como un arreglo al estilo JSON
secuencia_de_json_1: [3, 2, 1, "despegue"]
secuencia_de_json_2:
  - 3
  - 2
  - 1
  - "despegue"

# YAML también soporta conjuntos usando el simbolo ?
# y se ven de la siguiente forma:
set:
    ? item1
    ? item2
    ? item3

# Se puede usar el tag !!set
# Al igual que Python, los conjuntos sólo son mapas con valores nulos.
# El ejemplo de arriba es equivalente a:
set2:
    item1: null
    item2: null
    item3: null

##################################
# CARACTERÍSTICAS EXTRAS DE YAML #
##################################

# YAML usa tres guiones (---) para diferenciar entre directivas
# y contenido del documento.
# Por otra parte, tres puntos (...) se utilizan para indicar
# el final del documento en casos especiales.

# YAML tiene funciones útiles llamadas 'anchors' (anclas), que te permiten
# duplicar fácilmente contenido a lo largo de tu documento.
# El ampersand indica la declaración del ancla,
declara_ancla: &texto texto de la llave
# el asterisco indica el uso de dicha ancla.
usa_ancla: *texto # tendrá el valor "texto de la llave"

################
# TAGS EN YAML #
################

# En YAML, los nodos que no tienen un tag obtienen su tipo
# según la aplicación que los use, al usar un tag
# se pueden declarar tipos explícitamente.
string_explicito: !!str 0.5 # !!str para declarar un string
integer_explicito: !!int 5 # !!int para declarar un integer
float_explicito: !!float 1.2 # !!float para declarar un float
conjunto_explicito: !!set # !!set para declarar un conjunto
  ? Uno
  ? Dos
  ? Tres
mapa_ordenado_explicito: !!omap # !!omap para declarar un mapa ordenado
- Primero: 1
- Segundo: 2
- Tercero: 3
- Cuarto: 4

# Tags para los numeros enteros
llave_canonica: 5222
llave_decimal: +5222
llave_octal: 010
llave_hexadecimal: 0xC

#Tags para los numeros flotantes
llave_canonica: 1.215e+3
llave_exponencial: 12.3555e+02
llave_fija: 12.15
llave_negativa_infinita: -.inf
llave_numero_invalido: .NaN

# Tags para las fechas y horas
llave_canonica: 2001-12-15T02:59:43.1Z
llave_iso8601: 2001-12-14t21:59:43.10-05:00
llave_con_espacios: 2001-12-14 21:59:43.10 -5
llave_fecha: 2002-12-14

# Además existen tags para
null: #valor nulo
booleans: [ true, false ] # Valores booleanos
string: '012345' # Valor en string


# Algunos parseadores implementan tags específicas del lenguaje, como el
# que se muestra a continuación, encargado de manejar números complejos en
# Python:
numero_complejo_python: !!python/complex 1+2j

# El tag !!binary indica que un string es en realidad un blob
# representado en base-64.
archivo_gif: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

```

### Recursos adicionales

+ [Sitio oficial de YAML](http://yaml.org/)
+ [Parser en línea de de YAML](http://yaml-online-parser.appspot.com/)
+ [Validador en línea de YAML](http://codebeautify.org/yaml-validator)
---
language: bf
filename: bf-fa.bf
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
lang: fa-ir
---

<p dir='rtl'>برین فاک زبان برنامه نویسی تورینگ کامل بی نهایت ساده ایست که دارای فقط هشت</p>
<p dir='rtl'>دستور است.</p>

<p dir='rtl'>هر کارکتری به جر کارکتر های زیر در این زبان در نظر گرفته نمیشود.</p>


`>` `<` `+` `-` `.` `,` `[` `]`

<p dir='rtl'>برین فاک به صورت یک آرایه ی سی هزار خانه ای کار میکند که در ابتدا تمامی خانه های آن صفر هستند.</p>
<p dir='rtl'>همچنین یک اشاره گر در این برنامه به خانه ی فعلی اشاره میکند.</p>

<p dir='rtl'>در زیر هشت دستور این زبان شرح داده شده است:</p>

<p dir='rtl'>`+` : یک عدد به خانه ی فعلی اضافه می کند.
<p dir='rtl'>`-` : یک عدد از خانه ی فعلی کم می کند.  </p>
<p dir='rtl'>`>` : اشاره گر به خانه ی بعدی میرود -- به راست</p>
<p dir='rtl'>`<` : اشاره گر به خانه ی قبلی میرود -- به چپ</p>
<p dir='rtl'>`.` : کارکتر اسکی معادل مقدار خانه ی فعلی را چاپ میکند. -- به عنوان مثال 65 برای A</p>
<p dir='rtl'>`,` : یک کارکتر را از ورودی خوانده و مقدار آن را در خانه ی فعلی زخیره میکند.</p>
<p dir='rtl'>`[` : اگر مقدار خانه ی فعلی صفر باشد به محل بسته شدن کروشه جهش میکند. -- و از همه ی دستور های بین آن صرف نظر میشود.</p>
<p dir='rtl'>در غیر این صورت به دستور بعدی میرود.</p>
<p dir='rtl'>`]` : اگر مقدار خانه ی فعلی صفر باشد به خانه ی بعدی و در غیر این صورت به محل باز شدن کروشه جهش می کند. -- به عقب</p>

<p dir='rtl'>دو علامت کروشه امکان ایجاد حلقه را فراهم میکنند.</p>

<p dir='rtl'>در اینجا یک برنامه ی ساره برین فاک را مشاهده میکنید.</p>

```
++++++ [ > ++++++++++ < - ] > +++++ .
```

<p dir='rtl'>این برنامه کارکتر A را بر روی خروجی چاپ میکند.</p>
<p dir='rtl'>در این برنامه خانه ی اول به عنوان متغیر حلقه و خانه ی دوم برای مقدار عددی A</p>
<p dir='rtl'>ابتدا عدد شش در خانه ی اول ایجاد شده. سپس  برنامه  وارد یک حلقه میشود که در هر بار </p>
<p dir='rtl'>تکرار آن اشاره گر به خانه ی دوم رفته و ده بار به خانه ی فعلی اضافه می کند.</p>
<p dir='rtl'>-- و در انتهای حلقه به خانه ی اول برگشته تا حلقه کنترل شود</p>
<p dir='rtl'>بعد از اتمام حلقه به خانه ی دوم میرود و پنج بار به این خانه اضافه کرده و سپس آنرا چاپ میکند.</p>

```
, [ > + < - ] > .
```

<p dir='rtl'>در این برنامه ابتدا یک کارکتر از ورودی خوانده می شود. سپس یک حلقه به تعداد بار مقدار</p>
<p dir='rtl'>عددی کارکتر، یک عدد به خانه ی دوم اضافه می کند. با این کار در واقع برنامه مقدار ورودی را در خانه ی </p>
<p dir='rtl'>دوم کپی می کند. و در نهایت آن را برروی خروجی چاپ می کند.</p>

<p dir='rtl'>توجه داشته باشید که ردر بالا فواصل بین دستور ها فقط برای خوانایی بیشتر گذاشته شده اند.</p>
<p dir='rtl'>در واقع برنامه بالا به شکل زیر صحیح می باشد.</p>

```
,[>+<-]>.
```

<p dir='rtl'>حال سعی کنید ببینید که برنامه ی زیر چه کاری انجام می دهد؟</p>

```
,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>
```

<p dir='rtl'>این برنامه دو عدد را از ورودی خوانده و با هم ضرب می کند.</p>

<p dir='rtl'>ابتدا دو عدد از ورودی خوانده می شوند. سپس حلقه ی بیرونی بر روی خانه شماره یک شروع میشود.</p>
<p dir='rtl'>و درون آن حلقه ی دیگری بر روی خانه ی دوم شروع میشود که خانه ی 3 را زیاد میکند.</p>
<p dir='rtl'>ولی مشکلی که در اینجا به وجود می آید اینست که در پایان حلقه ی دوم مقدار خانه ی 2 صفر شده</p>
<p dir='rtl'>و مقدار اولیه ی آن از دست رفته است. برای حل این مشکل خانه ی شماره چهار هم زیاد میشود</p>
<p dir='rtl'>و در پایان حلقه مقدار آن به خانه 2 کپی میشود.</p>
<p dir='rtl'>در پایان خانه ی شماره 2  حاوی حاصلضرب خواهد بود.</p>

<hr>

<p dir='rtl'>و این همه ی برین فاک بود! خیلی ساده برای یادگیری ولی سنگین برای به کار بردن.</p>
<p dir='rtl'>حال می توانید برای تفریح مشغول نوشتن برنامه ی های مختلف با آن شوید.</p>
<p dir='rtl'>و یا یک اجرا کننده برین فاک را با یک زبان دیگر پیاده سازی کنید.</p>
<p dir='rtl'>و یا اگر خیلی دوست داشتید یک اجرا کننده ی برین فاک با برین فاک بنویسید!!</p>
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
    - ["Geoffrey Liu", "https://github.com/g-liu"]
    - ["Connor Shea", "https://github.com/connorshea"]
    - ["Deepanshu Utkarsh", "https://github.com/duci9y"]
    - ["Tyler Mumford", "https://tylermumford.com"]
translators:
    - ["Arashk", "https://github.com/Arashk-A"]
lang: fa-ir
filename: learncss-fa.css
---

<p dir='rtl'>در روزهای آغازین وب هیچگونه عنصر بصری مشاهده نمیشد و محتوا به صورت متن خالی بود. </p>
<p dir='rtl'>اما با توسعه بیشتر مرورگرها صفحات وب کاملاً تصویری نیز رایج شد</p>
<p dir='rtl'>CSS زبان استانداردی که موجودیت آن برای حفظ جدایی بین محتوا (HTML) و نگاه و احساس از</p>
<p dir='rtl'>صفحات وب است.</p>

<p dir='rtl'>به طور خلاصه, کاری که CSS انجام میدهد ارائه نحوه ایست که شما را قادر به هدف قرار دادن</p>
<p dir='rtl'>عناصر مختلف در یک صفحه HTML کرده و امکان اختصاص خواص متفاوت بصری به آنها را میدهد.</p>


<p dir='rtl'>مانند هر زبانی, CSS نسخه های زیادی دارد که در اینجا توجه ما روی CSS2.0 است. با وجودی که این نسخه جدیدترین نسخه نمیباشد اما بیشترین پشتیبانی و سازگاری را در میان نسخه های مختلف را دارد</p>

<p dir='rtl'><strong>توجه: </strong> برای مشاهده برخی از نتایج جلوه های تصویری CSS به منظور یادگیری بیشتر شما باید چیزهای گوناگونی در محیطی مثل [dabblet](http://dabblet.com/) امتحان کنید. توجه اصلی این مقاله روی دستورات و برخی از نکات عمومی است.</p>


<p dir='rtl'>در CSS همه توضیحات داخل ستاره-بروم نوشته میشوند زیرا CSS دستوری برای توضیحات تک خطی مثل C ندارد</p>

```CSS
/* comments appear inside slash-asterisk, just like this line!
   there are no "one-line comments"; this is the only comment style */
```

<p dir='rtl'>به طور کلی دستورات CSS بسیار ساده هستند که در آن یک انتخابگر (selector) عنصری را در  روی صفحه هدف قرار میدهد.</p>

```CSS
selector { property: value; /* more properties...*/ }
```

<p dir='rtl'>با استفاده از ستاره می توان برای همه عناصر روی صفحه استایل تعریف کرد</p>


```CSS
* { color:red; }
```

<p dir='rtl'>فرض کنید عنصری مثل این بر روی صفحه قرار دارد</p>

```html
<div class='some-class class2' id='someId' attr='value' otherAttr='en-us foo bar' />
```
<p dir='rtl'>شما میتوانید با استفاده از نام کلاس آنرا انتخاب کنید</p>


```CSS
.some-class { }
```

<p dir='rtl'>یا با استفاده از نام دو کلاس</p>

```CSS
.some-class.class2 { }
```

<p dir='rtl'>یا با استفاده از نام id</p>

```CSS
#someId { }
```

<p dir='rtl'>یا با استفاده از نام خود عنصر</p>

```CSS
div { }
```

<p dir='rtl'>یا با استفاده از `attr`</p>

```CSS
[attr] { font-size:smaller; }
```

<p dir='rtl'>یا با استفاده از ارزشی که برای `attr` مشخص شده</p>

```CSS
[attr='value'] { font-size:smaller; }
```

<p dir='rtl'>با استفاده از ارزشی که برای `attr` مشخص شده و آن ارزش با `val` شروع میشود در CSS3</p>

```CSS
[attr^='val'] { font-size:smaller; }
```

<p dir='rtl'>با استفاده از ارزشی که برای `attr` مشخص شده و آن ارزش با `ue` به پایان میرسد در CSS3</p>

```CSS
[attr$='ue'] { font-size:smaller; }
```

<p dir='rtl'>یا با انتخاب بوسیله یکی از ارزشهایی که در لیست `otherAttr` بوسیله فاصله از هم جدا شده اند در CSS3</p>

```CSS
[attr$='ue'] { font-size:smaller; }
```

<p dir='rtl'>یا ارزش(`value`) دقیقاً خود ارزش(`value`) یا بوسیله `-` که یونیکد (U+002D) از حرف بعدی جدا شود</p>

```CSS
[otherAttr|='en'] { font-size:smaller; }
```

<p dir='rtl'>و مهمتر از همه اینکه میتوان آنها را ترکیب کرد. نکته مهمی که در اینجا باید مد نظر داشته باشید این است که هنگام ترکیب نباید هیچگونه فاصله ای بین آنها قرار گیرد زیرا در این حالت معنای دستور تغییر میکند</p>

```CSS
div.some-class[attr$='ue'] { }
```

<p dir='rtl'>CSS این امکان را به شما میدهد که یک عنصر را بوسیله والدین آن انتخاب کنید</p>
<p dir='rtl'>برای مثال دستور زیر همه عناصری را که نام کلاس آنها <span dir="ltr">`.class-name`</span> و دارای پدر و مادری با این مشخصه <span dir="ltr">`div.some-parent`</span> هستند را انتخاب میکند.</p>

```CSS
div.some-parent > .class-name {}
```


<p dir='rtl'>یا دستور زیر که همه عناصری را که نام کلاس آنها <span dir="ltr">`.class-name`</span> و داخل عنصری با مشخصه <span dir="ltr">`div.some-parent`</span> هستند را در هر عمقی که باشند (یعنی فرزندی از فرزندان <span dir="ltr">`div.some-parent`</span><span dir="ltr"> باشند) انتخاب میکند.</p>

```CSS
div.some-parent .class-name {}
```

<p dir='rtl'>نکته ای که در اینجا باید به آن توجه کنید این است که این رستور با فاصله ای بین نام دو کلاس همراه است و با مثال زیر که در بالا هم ذکر شد تفاوت دارد.</p>

```CSS
div.some-parent.class-name {}
```

<p dir='rtl'>دستور زیر همه عناصری را که نام کلاس آنها <span dir="ltr">`.this-element`</span> و بلافاصله بعد از عنصری با مشخصه <span dir="ltr">`.i-am-before`</span> قرار دارد را انتخاب میکند.</p>

```CSS
.i-am-before + .this-element { }
```

<p dir='rtl'>هر خواهر یا برادری که بعد از <span dir="ltr">`.i-am-before`</span> بیاید در اینجا لازم نیست بلافاصله بعد از هم قرار بگیرند ولی باید دارای پدر و مادری یکسان باشند.</p>

```CSS
.i-am-any-before ~ .this-element {}
```
<p dir='rtl'>در زیر چند نمونه از شبه کلاسها را معرفی میکنیم که به شما اجازه میدهد عناصر را بر اساس رفتار آنها در صفحه انتخاب کنید.</p>
<p dir='rtl'>برای مثال زمانی که اشاره گر ماوس روی عنصری بر روی صفحه قرار دارد.</p>

```CSS
selector:hover {}
```

<p dir='rtl'>یا زمانی از یک لینک بازید کردید.</p>

```CSS
selected:visited {}
```

<p dir='rtl'>یا زمانی از لینکی بازید نشده است.</p>

```CSS
selected:link {}
```

<p dir='rtl'>یا زمانی که روی یک عنصر ورودی متمرکز شده.</p>

```CSS
selected:focus {}
```

<h3 dir='rtl'>واحدها</h3>

```CSS
selector {

    /* واحدها اندازه */
    width: 50%; /* در اساس درصد */
    font-size: 2em; /* بر اساس اندازه font-size یعنی دو برابر اندازه فونت فعلی */
    width: 200px; /* بر اساس پیکسل */
    font-size: 20pt; /* بر اساس points (نکات) */
    width: 5cm; /* بر اساس سانتیمتر */
    min-width: 50mm; /* بر اساس میلیمتر */
    max-width: 5in; /* بر اساس اینچ. max-(width|height) */
    height: 0.2vh; /* بر اساس ارتفاع دید `vh = نسبت به 1٪ از ارتفاع دید` (CSS3) */
    width: 0.4vw; /* بر اساس عرض دید `vw = نسبت به 1٪ از عرض دید` (CSS3) */
    min-height: 0.1vmin; /* بر اساس کوچکترین مقدار از ارتفاع یا عرض دید (CSS3) */
    max-width: 0.3vmax; /* مانند مثال بالا برای بیشترین مقدار (CSS3) */

    /* رنگها */
    background-color: #F6E;  /* بر اساس short hex */
    background-color: #F262E2; /* بر اساس long hex format */
    background-color: tomato; /* بر اساس نام رنگ */
    background-color: rgb(255, 255, 255); /* بر اساس rgb */
    background-color: rgb(10%, 20%, 50%); /* بر اساس درصد rgb , (rgb percent) */
    background-color: rgba(255, 0, 0, 0.3); /* بر اساس rgba (نیمه شفاف) , (semi-transparent rgb) (CSS3) */
    background-color: transparent; /* شفاف */
    background-color: hsl(0, 100%, 50%); /* بر اساس hsl format (CSS3). */
    background-color: hsla(0, 100%, 50%, 0.3); /* بر اساس hsla ,مثل RGBAکه میتوان شفافیت را در آخر انتخاب کرد (CSS3) */


    /* عکسها */
    background-image: url(/path-to-image/image.jpg); /* گذاشتن نقل قول داخل url() اختیاری است*/

    /* فونتها */
    font-family: Arial;
    font-family: "Courier New"; /* اگر اسم فونت با فاصله همراه باشد باید داخل نقل قول یک یا دو نوشته شود */
    font-family: "Courier New", Trebuchet, Arial, sans-serif; /* اگر فونت اولی پیدا نشد مرورگر به سراغ نام بعدی میرود */
}
```

<h2 dir='rtl'>نحوه استفاده</h2>

<p dir='rtl'>هر دستور CSS را که می خواهید در فایلی با پسوند <span dir="ltr">.css</span> ذخیره کنید </p>
<p dir='rtl'>حالا با استفاده از کد زیر آنرا در قسمت `head` داخل فایل html خود تعریف کنید </p>

```html
<link rel='stylesheet' type='text/css' href='path/to/style.css' />
```

<p dir='rtl'>یا میتوان با استفاده از تگ `style` درون `head` دستورات CSS را به صورت درون برنامه ای تعریف کرد اما توسیه میشود تا جای ممکن از این کار اجتناب کنید. </p>

```html
<style>
   a { color: purple; }
</style>
```

<p dir='rtl'>همچنین شما میتوانید دستورات CSS را به عنوان یک مشخصه برای عنصر تعریف کنید ولی تا جای ممکن باید از این کار اجتناب کنید.</p>

```html
<div style="border: 1px solid red;">
</div>
```

<h2 dir='rtl'>حق تقدم یا اولویت</h2>

<p dir='rtl'>همانگونه که مشاهده کردید یک مشخصه می تواند به وسیله چندین انتخابگر انتخاب گردد.</p>
<p dir='rtl'>و همچنین یک ویژگی میتواند چندین بار برای یک عنصر تعریف شود.</p>
<p dir='rtl'>در این صورت یک دستور میتواند بر دستورات دیگر حق تقدم یا اولویت پیدا کند.</p>

<p dir='rtl'>به مثال زیر توجه کنید:</p>

```CSS
/*A*/
p.class1[attr='value']

/*B*/
p.class1 {}

/*C*/
p.class2 {}

/*D*/
p {}

/*E*/
p { property: value !important; }

```

<p dir='rtl'>و همچنین به کد زیر:</p>

```html
<p style='/*F*/ property:value;' class='class1 class2' attr='value'>
</p>

```
‍‍
<p dir='rtl'>حق تقدم یا اولویت برای مثال بالا به این صورت است:</p>
<p dir='rtl'>توجه داشته باشید که حق تقدم برای هر کدام از ویژگیها است نه برای کل مجموعه.</p>

<p dir='rtl'>E دارای بیشترین الویت برای اینکه از <span dir="ltr">`!important`</span> استفاده کرده.</p>
<p dir='rtl'>اما توصیه میشود تا جای ممکن از این کار اجتناب کنید مگر اینکه اینکار ضرورت داشته باشد</p>
<p dir='rtl'>اولویت بعدی با F است زیرا که از روش درون برنامه ای استفاده کرده </p>
<p dir='rtl'>اولویت بعدی با A است زیرا که بیشتر از بقیه مشخص تر تعریف شپه </p>
<p dir='rtl'>مشخص تر = مشخص کننده بیشتر. دارای ۳ مشخص کننده: ۱ تگ <span dir="ltr">`p`</span> + ۱ کلاس با نام <span dir="ltr">`class1`</span> + ۱ خاصیت <span dir="ltr">`attr="value"`</span></p>
<p dir='rtl'>اولویت بعدی با C است که مشخصه یکسانی با B دارد ولی بعد از آن تعریف شده است.</p>
<p dir='rtl'>اولویت بعدی با B</p>
<p dir='rtl'>و در آخر D</p>

<h2 dir='rtl'>سازگاری</h2>

<p dir='rtl'>بسیار از ویژگیهای CSS2 (و به تدریج CSS3) بر روی تمام مرورگرها و دستگاه ها سازگارند.اما همیشه حیاتی است که سازگاری CSS مورد استفاده خود را با مرورگر هدف چک کنید.</p>

<p dir='rtl'> یک منبع خوب برای این کار است</p>
[QuirksMode CSS](http://www.quirksmode.org/css/)

<p dir='rtl'>برای یک تست سازگاری سریع, منبع زیر میتواند کمک بزرگی برای این کار باشد.</p>
[CanIUse](http://caniuse.com/)

<h2 dir='rtl'> منابع دیگر </h2>


[Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)

[QuirksMode CSS](http://www.quirksmode.org/css/)

[Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)


---
language: java
lang: fa-ir
contributors:
    - ["Jake Prather", "https://github.com/JakeHP"]
    - ["Jakukyo Friel", "https://weakish.github.io"]
    - ["Madison Dickson", "https://github.com/mix3d"]
    - ["Simon Morgan", "https://sjm.io/"]
    - ["Zachary Ferguson", "https://github.com/zfergus2"]
    - ["Cameron Schermerhorn", "https://github.com/cschermerhorn"]
    - ["Rachel Stiyer", "https://github.com/rstiyer"]
    - ["Michael Dähnert", "https://github.com/JaXt0r"]
    - ["Rob Rose", "https://github.com/RobRoseKnows"]
    - ["Sean Nam", "https://github.com/seannam"]
translators:
    - ["ghaseminya", "https://github.com/ghaseminya"]
filename: LearnJava-fa.java
---

جاوا یک زبان برنامه نویسی کامپیوتری چند منظوره، با قابلیت همزمانی، برپایه کلاس و شی گرایی می باشد.
[مطالعه بیشتر.](https://docs.oracle.com/javase/tutorial/java/)

```java
// Single-line comments start with //

/*
Multi-line comments look like this.
*/

/**
 * JavaDoc comments look like this. Used to describe the Class or various
 * attributes of a Class.
 * Main attributes:
 *
 * @author         @ghaseminya
 * @version     v1.0
*/

// Import ArrayList class inside of the java.util package
import java.util.ArrayList;
// Import all classes inside of java.security package
import java.security.*;

// Each .java file contains one outer-level public class, with the same name
// as the file.
public class LearnJava {

    // In order to run a java program, it must have a main method as an entry 
    // point.
    public static void main (String[] args) {

    ///////////////////////////////////////
    // Input/Output
    ///////////////////////////////////////

        /*
        * Output
        */

        // Use System.out.println() to print lines.
        System.out.println("Hello World!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // To print without a newline, use System.out.print().
        System.out.print("Hello ");
        System.out.print("World");

        // Use System.out.printf() for easy formatted printing.
        System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159

        /*
         * Input
         */

        // use Scanner to read input
        // must import java.util.Scanner;
        Scanner scanner = new Scanner(System.in);

        // read string input
        String name = scanner.next();

        // read byte input
        byte numByte = scanner.nextByte();

        // read int input
        int numInt = scanner.nextInt();

        // read long input
        float numFloat - scanner.nextFloat();

        // read double input
        double numDouble = scanner.nextDouble();

        // read boolean input
        boolean bool = scanner.nextBoolean();

        ///////////////////////////////////////
        // Variables
        ///////////////////////////////////////

        /*
        *  Variable Declaration
        */
        // Declare a variable using <type> <name>
        int fooInt;
        // Declare multiple variables of the same 
        // type <type> <name1>, <name2>, <name3>
        int fooInt1, fooInt2, fooInt3;

        /*
        *  Variable Initialization
        */

        // Initialize a variable using <type> <name> = <val>
        int barInt = 1;
        // Initialize multiple variables of same type with same 
        // value <type> <name1>, <name2>, <name3> = <val>
        int barInt1, barInt2, barInt3;
        barInt1 = barInt2 = barInt3 = 1;

        /*
        *  Variable types
        */
        // Byte - 8-bit signed two's complement integer
        // (-128 <= byte <= 127)
        byte fooByte = 100;
        
        // If you would like to interpret a byte as an unsigned integer
        // then this simple operation can help
        int unsignedIntLessThan256 = 0xff & fooByte;
        // this contrasts a cast which can be negative.
        int signedInt = (int) fooByte;

        // Short - 16-bit signed two's complement integer
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - 32-bit signed two's complement integer
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int bazInt = 1;

        // Long - 64-bit signed two's complement integer
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L is used to denote that this variable value is of type Long;
        // anything without is treated as integer by default.

        // Note: byte, short, int and long are signed. They can have positive and negative values.
        // There are no unsigned variants.
        // char, however, is 16-bit unsigned.

        // Float - Single-precision 32-bit IEEE 754 Floating Point
        // 2^-149 <= float <= (2-2^-23) * 2^127
        float fooFloat = 234.5f;
        // f or F is used to denote that this variable value is of type float;
        // otherwise it is treated as double.

        // Double - Double-precision 64-bit IEEE 754 Floating Point
        // 2^-1074 <= x <= (2-2^-52) * 2^1023
        double fooDouble = 123.4;

        // Boolean - true & false
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - A single 16-bit Unicode character
        char fooChar = 'A';

        // final variables can't be reassigned to another object,
        final int HOURS_I_WORK_PER_WEEK = 9001;
        // but they can be initialized later.
        final double E;
        E = 2.71828;

        // BigInteger - Immutable arbitrary-precision integers
        //
        // BigInteger is a data type that allows programmers to manipulate
        // integers longer than 64-bits. Integers are stored as an array of
        // of bytes and are manipulated using functions built into BigInteger
        //
        // BigInteger can be initialized using an array of bytes or a string.        
        BigInteger fooBigInteger = new BigInteger(fooByteArray);

        // BigDecimal - Immutable, arbitrary-precision signed decimal number
        //
        // A BigDecimal takes two parts: an arbitrary precision integer 
        // unscaled value and a 32-bit integer scale
        //
        // BigDecimal allows the programmer complete control over decimal
        // rounding. It is recommended to use BigDecimal with currency values
        // and where exact decimal precision is required.
        //
        // BigDecimal can be initialized with an int, long, double or String
        // or by initializing the unscaled value (BigInteger) and scale (int).
        BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt);
        
        // Be wary of the constructor that takes a float or double as
        // the inaccuracy of the float/double will be copied in BigDecimal.
        // Prefer the String constructor when you need an exact value.
        BigDecimal tenCents = new BigDecimal("0.1");

        // Strings
        String fooString = "My String Is Here!";

        // \n is an escaped character that starts a new line
        String barString = "Printing on a new line?\nNo Problem!";
        // \t is an escaped character that adds a tab character
        String bazString = "Do you want to add a tab?\tNo Problem!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // String Building
        // #1 - with plus operator
        // That's the basic way to do it (optimized under the hood)
        String plusConcatenated = "Strings can " + "be concatenated " + "via + operator.";
        System.out.println(plusConcatenated);
        // Output: Strings can be concatenated via + operator.

        // #2 - with StringBuilder
        // This way doesn't create any intermediate strings. It just stores the string pieces, and ties them together
        // when toString() is called.
        // Hint: This class is not thread safe. A thread-safe alternative (with some impact on performance) is StringBuffer.
        StringBuilder builderConcatenated = new StringBuilder();
        builderConcatenated.append("You ");
        builderConcatenated.append("can use ");
        builderConcatenated.append("the StringBuilder class.");
        System.out.println(builderConcatenated.toString()); // only now is the string built 
        // Output: You can use the StringBuilder class.
        
        // StringBuilder is efficient when the fully constructed String is not required until the end of some processing.
        StringBuilder stringBuilder = new StringBuilder();
        String inefficientString = "";
        for(int i = 0 ; i < 10; i++){
            stringBuilder.append(i).append(" ");
            inefficientString += i + " ";
        }
        System.out.println(inefficientString);
        System.out.println(stringBuilder.toString());
        // inefficientString requires a lot more work to produce, as it generates a String on every loop iteration.
        // Simple concatenation with + is compiled to a StringBuilder and toString()
        // Avoid string concatenation in loops.
        
        // #3 - with String formatter
        // Another alternative way to create strings. Fast and readable.
        String.format("%s may prefer %s.", "Or you", "String.format()");
        // Output: Or you may prefer String.format().
 
        // Arrays
        // The array size must be decided upon instantiation
        // The following formats work for declaring an array
        // <datatype>[] <var name> = new <datatype>[<array size>];
        // <datatype> <var name>[] = new <datatype>[<array size>];
        int[] intArray = new int[10];
        String[] stringArray = new String[1];
        boolean boolArray[] = new boolean[100];

        // Another way to declare & initialize an array
        int[] y = {9000, 1000, 1337};
        String names[] = {"Bob", "John", "Fred", "Juan Pedro"};
        boolean bools[] = {true, false, false};

        // Indexing an array - Accessing an element
        System.out.println("intArray @ 0: " + intArray[0]);

        // Arrays are zero-indexed and mutable.
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // Other data types worth checking out
        // ArrayLists - Like arrays except more functionality is offered, and
        //              the size is mutable.
        // LinkedLists - Implementation of doubly-linked list. All of the
        //               operations perform as could be expected for a
        //               doubly-linked list.
        // Maps - A set of objects that map keys to values. Map is
        //        an interface and therefore cannot be instantiated.
        //        The type of keys and values contained in a Map must
        //        be specified upon instantiation of the implementing
        //        class. Each key may map to only one corresponding value,
        //        and each key may appear only once (no duplicates).
        // HashMaps - This class uses a hashtable to implement the Map
        //            interface. This allows the execution time of basic
        //            operations, such as get and insert element, to remain
        //            constant even for large sets.
        // TreeMap - This class is a sorted tree structure. It implements a red
        //           black tree and sorts the entries based on the key value or
        //           the comparator provided while creating the object

        ///////////////////////////////////////
        // Operators
        ///////////////////////////////////////
        System.out.println("\n->Operators");

        int i1 = 1, i2 = 2; // Shorthand for multiple declarations

        // Arithmetic is straightforward
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (int/int returns int)
        System.out.println("1/2 = " + (i1 / (double)i2)); // => 0.5

        // Modulo
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Comparison operators
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Boolean operators
        System.out.println("3 > 2 && 2 > 3? " + ((3 > 2) && (2 > 3))); // => false
        System.out.println("3 > 2 || 2 > 3? " + ((3 > 2) || (2 > 3))); // => true
        System.out.println("!(3 == 2)? " + (!(3 == 2))); // => true

        // Bitwise operators!
        /*
        ~      Unary bitwise complement
        <<     Signed left shift
        >>     Signed/Arithmetic right shift
        >>>    Unsigned/Logical right shift
        &      Bitwise AND
        ^      Bitwise exclusive OR
        |      Bitwise inclusive OR
        */

        // Increment operators
        int i = 0;
        System.out.println("\n->Inc/Dec-rementation");
        // The ++ and -- operators increment and decrement by 1 respectively.
        // If they are placed before the variable, they increment then return;
        // after the variable they return then increment.
        System.out.println(i++); // i = 1, prints 0 (post-increment)
        System.out.println(++i); // i = 2, prints 2 (pre-increment)
        System.out.println(i--); // i = 1, prints 2 (post-decrement)
        System.out.println(--i); // i = 0, prints 0 (pre-decrement)

        ///////////////////////////////////////
        // Control Structures
        ///////////////////////////////////////
        System.out.println("\n->Control Structures");

        // If statements are c-like
        int j = 10;
        if (j == 10) {
            System.out.println("I get printed");
        } else if (j > 10) {
            System.out.println("I don't");
        } else {
            System.out.println("I also don't");
        }

        // While loop
        int fooWhile = 0;
        while(fooWhile < 100) {
            System.out.println(fooWhile);
            // Increment the counter
            // Iterated 100 times, fooWhile 0,1,2...99
            fooWhile++;
        }
        System.out.println("fooWhile Value: " + fooWhile);

        // Do While Loop
        int fooDoWhile = 0;
        do {
            System.out.println(fooDoWhile);
            // Increment the counter
            // Iterated 99 times, fooDoWhile 0->99
            fooDoWhile++;
        } while(fooDoWhile < 100);
        System.out.println("fooDoWhile Value: " + fooDoWhile);

        // For Loop
        // for loop structure => for(<start_statement>; <conditional>; <step>)
        for (int fooFor = 0; fooFor < 10; fooFor++) {
            System.out.println(fooFor);
            // Iterated 10 times, fooFor 0->9
        }
        System.out.println("fooFor Value: " + fooFor);
        
        // Nested For Loop Exit with Label
        outer:
        for (int i = 0; i < 10; i++) {
          for (int j = 0; j < 10; j++) {
            if (i == 5 && j ==5) {
              break outer;
              // breaks out of outer loop instead of only the inner one
            }
          }
        }
        
        // For Each Loop
        // The for loop is also able to iterate over arrays as well as objects
        // that implement the Iterable interface.
        int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        // for each loop structure => for (<object> : <iterable>)
        // reads as: for each element in the iterable
        // note: the object type must match the element type of the iterable.
        for (int bar : fooList) {
            System.out.println(bar);
            //Iterates 9 times and prints 1-9 on new lines
        }

        // Switch Case
        // A switch works with the byte, short, char, and int data types.
        // It also works with enumerated types (discussed in Enum Types), the
        // String class, and a few special classes that wrap primitive types:
        // Character, Byte, Short, and Integer.
        int month = 3;
        String monthString;
        switch (month) {
            case 1: monthString = "January";
                    break;
            case 2: monthString = "February";
                    break;
            case 3: monthString = "March";
                    break;
            default: monthString = "Some other month";
                     break;
        }
        System.out.println("Switch Case Result: " + monthString);
        
        // Starting in Java 7 and above, switching Strings works like this:
        String myAnswer = "maybe";
        switch(myAnswer) {
            case "yes":
                System.out.println("You answered yes.");
                break;
            case "no":
                System.out.println("You answered no.");
                break;
            case "maybe":
                System.out.println("You answered maybe.");
                break;
            default:
                System.out.println("You answered " + myAnswer);
                break;
        }
        
        
        // Try-with-resources (Java 7+)
        // Try-catch-finally statements work as expected in Java but in Java 7+
        // the try-with-resources statement is also available. Try-with-resources
        // simplifies try-catch-finally statements be closing resources
        // automatically.
        
        // In order to use a try-with-resources, include a an instance of a class
        // in the try statement. The class must implement java.lang.AutoCloseable.
        try(BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) {
            // You can attempt to do something that could throw an exception.
            System.out.println(br.readLine());
            // In Java 7, the resource will always be closed, even if it throws
            // an Exception. 
        } catch (Exception ex) {
            //The resource will be closed before the catch statement executes.
            System.out.println("readLine() failed.");
        }
        // No need for a finally statement in this case, the BufferedReader is
        // already closed. This can be used to avoid certain edge cases where
        // a finally statement might not be called.
        // To learn more:
        // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
        
        
        // Conditional Shorthand
        // You can use the '?' operator for quick assignments or logic forks.
        // Reads as "If (statement) is true, use <first value>, otherwise, use
        // <second value>"
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println(bar); // Prints A, because the statement is true

        ////////////////////////////////////////
        // Converting Data Types And Typecasting
        ////////////////////////////////////////

        // Converting data

        // Convert String To Integer
        Integer.parseInt("123");//returns an integer version of "123"

        // Convert Integer To String
        Integer.toString(123);//returns a string version of 123

        // For other conversions check out the following classes:
        // Double
        // Long
        // String

        // Typecasting
        // You can also cast Java objects, there's a lot of details and deals
        // with some more intermediate concepts. Feel free to check it out here:
        // https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

        ///////////////////////////////////////
        // Classes And Functions
        ///////////////////////////////////////

        System.out.println("\n->Classes & Functions");

        // (definition of the Bicycle class follows)

        // Use new to instantiate a class
        Bicycle trek = new Bicycle();

        // Call object methods
        trek.speedUp(3); // You should always use setter and getter methods
        trek.setCadence(100);

        // toString returns this Object's string representation.
        System.out.println("trek info: " + trek.toString());

        // Double Brace Initialization
        // The Java Language has no syntax for how to create static Collections
        // in an easy way. Usually you end up in the following way:
        private static final Set<String> COUNTRIES = new HashSet<String>();
        static {
           COUNTRIES.add("DENMARK");
           COUNTRIES.add("SWEDEN");
           COUNTRIES.add("FINLAND");
        }

        // But there's a nifty way to achieve the same thing in an
        // easier way, by using something that is called Double Brace
        // Initialization.
        private static final Set<String> COUNTRIES = new HashSet<String>() {{
            add("DENMARK");
            add("SWEDEN");
            add("FINLAND");
        }}

        // The first brace is creating a new AnonymousInnerClass and the
        // second one declares an instance initializer block. This block
        // is called when the anonymous inner class is created.
        // This does not only work for Collections, it works for all
        // non-final classes.

    } // End main method
} // End LearnJava class

// You can include other, non-public outer-level classes in a .java file,
// but it is not good practice. Instead split classes into separate files.

// Class Declaration Syntax:
// <public/private/protected> class <class name> {
//    // data fields, constructors, functions all inside.
//    // functions are called as methods in Java.
// }

class Bicycle {

    // Bicycle's Fields/Variables
    public int cadence; // Public: Can be accessed from anywhere
    private int speed;  // Private: Only accessible from within the class
    protected int gear; // Protected: Accessible from the class and subclasses
    String name; // default: Only accessible from within this package
    static String className; // Static class variable

    // Static block 
    // Java has no implementation of static constructors, but
    // has a static block that can be used to initialize class variables 
    // (static variables). 
    // This block will be called when the class is loaded.
    static {
        className = "Bicycle";
    }

    // Constructors are a way of creating classes
    // This is a constructor
    public Bicycle() {
        // You can also call another constructor:
        // this(1, 50, 5, "Bontrager");
        gear = 1;
        cadence = 50;
        speed = 5;
        name = "Bontrager";
    }
    // This is a constructor that takes arguments
    public Bicycle(int startCadence, int startSpeed, int startGear,
        String name) {
        this.gear = startGear;
        this.cadence = startCadence;
        this.speed = startSpeed;
        this.name = name;
    }

    // Method Syntax:
    // <public/private/protected> <return type> <function name>(<args>)

    // Java classes often implement getters and setters for their fields

    // Method declaration syntax:
    // <access modifier> <return type> <method name>(<args>)
    public int getCadence() {
        return cadence;
    }

    // void methods require no return statement
    public void setCadence(int newValue) {
        cadence = newValue;
    }
    public void setGear(int newValue) {
        gear = newValue;
    }
    public void speedUp(int increment) {
        speed += increment;
    }
    public void slowDown(int decrement) {
        speed -= decrement;
    }
    public void setName(String newName) {
        name = newName;
    }
    public String getName() {
        return name;
    }

    //Method to display the attribute values of this Object.
    @Override // Inherited from the Object class.
    public String toString() {
        return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
            " name: " + name;
    }
} // end class Bicycle

// PennyFarthing is a subclass of Bicycle
class PennyFarthing extends Bicycle {
    // (Penny Farthings are those bicycles with the big front wheel.
    // They have no gears.)

    public PennyFarthing(int startCadence, int startSpeed) {
        // Call the parent constructor with super
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // You should mark a method you're overriding with an @annotation.
    // To learn more about what annotations are and their purpose check this
    // out: http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGear(int gear) {
        this.gear = 0;
    }
}

// Interfaces
// Interface declaration syntax
// <access-level> interface <interface-name> extends <super-interfaces> {
//     // Constants
//     // Method declarations
// }

// Example - Food:
public interface Edible {
    public void eat(); // Any class that implements this interface, must
                       // implement this method.
}

public interface Digestible {
    public void digest();
    // In Java 8, interfaces can have default method.
    // public void digest() {
    //     System.out.println("digesting ...");
    // }
}

// We can now create a class that implements both of these interfaces.
public class Fruit implements Edible, Digestible {
    @Override
    public void eat() {
        // ...
    }

    @Override
    public void digest() {
        // ...
    }
}

// In Java, you can extend only one class, but you can implement many
// interfaces. For example:
public class ExampleClass extends ExampleClassParent implements InterfaceOne,
    InterfaceTwo {
    @Override
    public void InterfaceOneMethod() {
    }

    @Override
    public void InterfaceTwoMethod() {
    }

}

// Abstract Classes

// Abstract Class declaration syntax
// <access-level> abstract <abstract-class-name> extends <super-abstract-classes> {
//     // Constants and variables
//     // Method declarations
// }

// Marking a class as abstract means that it contains abstract methods that
// must be defined in a child class. Similar to interfaces, abstract classes
// cannot be instantiated, but instead must be extended and the abstract
// methods defined. Different from interfaces, abstract classes can contain a
// concrete and abstract methods. Methods in an interface cannot have a body,
// mixture of unless the method is static, and variables are final by default,
// unlike an abstract class. Also abstract classes CAN have the "main" method.
public abstract class Animal
{
    public abstract void makeSound();

    // Method can have a body
    public void eat()
    {
        System.out.println("I am an animal and I am Eating.");
        // Note: We can access private variable here.
        age = 30;
    }

    // No need to initialize, however in an interface
    // a variable is implicitly final and hence has
    // to be initialized.
    protected int age;

    public void printAge()
    {
        System.out.println(age);  
    }

    // Abstract classes can have main function.
    public static void main(String[] args)
    {
        System.out.println("I am abstract");
    }
}

class Dog extends Animal
{
    // Note still have to override the abstract methods in the
    // abstract class.
    @Override
    public void makeSound()
    {
        System.out.println("Bark");
        // age = 30;    ==> ERROR!    age is private to Animal
    }

    // NOTE: You will get an error if you used the
    // @Override annotation here, since java doesn't allow
    // overriding of static methods.
    // What is happening here is called METHOD HIDING.
    // Check out this SO post: http://stackoverflow.com/questions/16313649/
    public static void main(String[] args)
    {
        Dog pluto = new Dog();
        pluto.makeSound();
        pluto.eat();
        pluto.printAge();
    }
}

// Final Classes

// Final Class declaration syntax
// <access-level> final <final-class-name> {
//     // Constants and variables
//     // Method declarations
// }

// Final classes are classes that cannot be inherited from and are therefore a
// final child. In a way, final classes are the opposite of abstract classes
// because abstract classes must be extended, but final classes cannot be
// extended.
public final class SaberToothedCat extends Animal
{
    // Note still have to override the abstract methods in the
    // abstract class.
    @Override
    public void makeSound()
    {
        System.out.println("Roar");
    }
}

// Final Methods
public abstract class Mammal()
{
    // Final Method Syntax:
    // <access modifier> final <return type> <function name>(<args>)

    // Final methods, like, final classes cannot be overridden by a child
    // class, and are therefore the final implementation of the method.
    public final boolean isWarmBlooded()
    {
        return true;
    }
}

// Enum Type
//
// An enum type is a special data type that enables for a variable to be a set
// of predefined constants. The variable must be equal to one of the values
// that have been predefined for it. Because they are constants, the names of
// an enum type's fields are in uppercase letters. In the Java programming
// language, you define an enum type by using the enum keyword. For example,
// you would specify a days-of-the-week enum type as:
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

// We can use our enum Day like that:
public class EnumTest {
    // Variable Enum
    Day day;
    
    public EnumTest(Day day) {
        this.day = day;
    }
    
    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;
            case FRIDAY:
                System.out.println("Fridays are better.");
                break;   
            case SATURDAY: 
            case SUNDAY:
                System.out.println("Weekends are best.");
                break;     
            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }
    
    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs(); // => Mondays are bad.
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs(); // => Midweek days are so-so.
    }
}

// Enum types are much more powerful than we show above. 
// The enum body can include methods and other fields.
// You can see more at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

```

## مطالب بیشتر

لینکهای زیر را می توانید برای پیگیرهای بیشتر استفاده کنید.
البته همیشه گوگل را در کنار دستتان داشته باشید!

**سایت های راهنمای اصلی**:

* [Java Tutorial Trail from Sun / Oracle](https://docs.oracle.com/javase/tutorial/index.html)

* [Java Access level modifiers](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Object-Oriented Programming Concepts](https://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Polymorphism](https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Abstraction](https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Exceptions](https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Interfaces](https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java Code Conventions](https://www.oracle.com/technetwork/java/codeconvtoc-136057.html)

* New features in Java 8:
    * [Lambda expressions (functional programming)](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
    * [Date and time API (java.time package)](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html)

**راهنما و سایت های آموزشی**

* [Learneroo.com - Learn Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)

**کتاب‌ها**:

* [Head First Java](http://www.headfirstlabs.com/books/hfjava/)

* [Thinking in Java](http://www.mindview.net/Books/TIJ/)

* [Objects First with Java](https://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660)

* [Java The Complete Reference](https://www.amazon.com/gp/product/0071606300)
---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
translators:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
filename: javascript-fa.js
lang: fa-ir
---

<p dir='rtl'>
جاوااسکریپت توسط برندن ایش از شرکت NetScape در سال 1995 ساخته شد. در ابتدا به عنوان یک زبان اسکریپت‌نویسی  در کنار جاوا (که برای موارد پیچیده تر در طراحی وب در نظر گرفته میشد) مورد استفاده بود، ولی در پی نفوذ بسیار گسترده آن در وب و همچنین پشتیبانی پیش-ساخته آن در مرورگر ها، امروزه به مراتب بیشتر از جاوا در برنامه نویسی سمت-کاربر در وب به کار برده میشود.
با این حال جاوااسکریپت فقط محدود به مرورگر های وب نمیشود. Node.js پروژه ایست که یک نسخه ی مستقل از اجراکننده ی موتور جاوااسکریپت V8 از گوگل کروم را در اختیار قرار میده که هر روزه درحال محبوب تر شدن نیز هست.
</p>

<p dir='rtl'>
قدر دان نظرات سازنده شما هستم! شما میتوانید از طریق زیر با من تماس بگیرید:
</p>

[@adambrenecki](https://twitter.com/adambrenecki), or
[adam@brenecki.id.au](mailto:adam@brenecki.id.au).

<p dir='rtl'>
// توضیحات همانند C هستند. توضیحات یک خطی با دو خط مورب شروع میشوند.,
</p>

<p dir='rtl'>
/* و توضیحات چند خطی با خط مورب-ستاره شروع،
   و با ستاره-خط مورب ختم میشوند */
</p>

```js
// Comments are like C. Single-line comments start with two slashes,
/* and multiline comments start with slash-star
   and end with star-slash */
```
<p dir='rtl'>
گزاره ها را میتوانید با نقطه ویرگول پایان دهید ;
</p>
```js
doStuff();
```
<p dir='rtl'>
ولی لزومی به این کار نیست. نقطه ویرگول به صورت خودکار در نظر گرفته میشوند.  
</p>
<p dir='rtl'>
وقتی که خط جدیدی شروع میشود. مگر در موارد خاص.
</p>
```js
doStuff()
```
<p dir='rtl'>برای اینگه درگیر آن موارد خاص نشویم، در اینجا از اون ها  </p>
<p dir='rtl'>صرف نظر میکنیم.</p>

<h2 dir='rtl'>1. اعداد، رشته ها و عملگرها</h2>

<p dir='rtl'>جاوااسکریپت فقط یک نوع عدد دارد و آن عدد اعشاری 64 بیتی IEEE 754 است.</p>
<p dir='rtl'>نترسید! و نگران اعداد صحیح نباشید! این اعداد اعشاری دارای 54 بیت مانتیس هستند که قابلیت ذخیره ی </p>
<p dir='rtl'>دقیق اعداد صحیح تا مقدار تقریبی 9x10¹⁵  را دارند.</p>
```js
3; // = 3
1.5; // = 1.5
```
<p dir='rtl'>
تمامی عملگر های محاسباتی آن طوری که انتظارش را دارید عمل خواهند کرد.
</p>
```js
1 + 1; // = 2
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7
```
<p dir='rtl'>و این حتی شامل تقسیم هم میشود.</p>
```js
5 / 2; // = 2.5
```
<p dir='rtl'>عملگر های بیتی هم به همین شکل. وقتی از یک عملگر بیتی استفاده میکنید، عدد اعشاری شما</p>
<p dir='rtl'>به عدد صحیح علامت دار *تا 32 بیت* تبدیل میشود.</p>
```js
1 << 2; // = 4
```
<p dir='rtl'>عملیات داخل پرانتز تقدم بالاتری دارند.</p>
```js
(1 + 3) * 2; // = 8
```
<p dir='rtl'>سه مقدار خاص وجود دارند که در واقع مقادیر عددی نیستند:</p>
```js
Infinity; // result of e.g. 1/0
-Infinity; // result of e.g. -1/0
NaN; // result of e.g. 0/0
```
<p dir='rtl'>مقادیر بولی هم تعریف شده هستند:</p>
```js
true;
false;
```
<p dir='rtl'>رشته ها با آپستروف و یا گیومه تعریف میشوند.</p>
```js
'abc';
"Hello, world";
```
<p dir='rtl'>و منفی کردن شرط با علامت تعجب</p>
```js
!true; // = false
!false; // = true
```
<p dir='rtl'>تساوی دو مقدار با ==</p>
```js
1 == 1; // = true
2 == 1; // = false
```
<p dir='rtl'>و عدم تساوی با !=</p>
```js
1 != 1; // = false
2 != 1; // = true
```
<p dir='rtl'>و سایر عمیلات های مقایسه</p>
```js
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true
```
<p dir='rtl'>رشته ها با علامت جمع به یکدیگر متصل میشوند</p>
```js
"Hello " + "world!"; // = "Hello world!"
```
<p dir='rtl'>و با علامت برگتر و یا کوچکتر با یکدیگر مقایسه میشوند.</p>
```js
"a" < "b"; // = true
```
<p dir='rtl'>نوع متغیر برای عملیات مقایسه تطبیق داده میشود</p>
```js
"5" == 5; // = true
```
<p dir='rtl'>مگر اینکه از سه مساوی استفاده شود!</p>
```js
"5" === 5; // = false
```
<p dir='rtl'>با استفاده از charAt میتوانید به کارکتر های یک رشته دسترسی پیدا کنید.</p>
```js
"This is a string".charAt(0);
```
<p dir='rtl'>از null برای نشان دادن عمدی مقدار هیج استفاده میشود.</p>
<p dir='rtl'>و از undefined برای نشان دادن اینکه در حال حاظر مقدار موجود نمی باشد، هرچند خود undefined یک مقدار محسوب میشود.</p>
```js
null; // used to indicate a deliberate non-value
undefined; // used to indicate a value is not currently present (although undefined
           // is actually a value itself)
```
<p dir='rtl'>false, null, undefined, NaN, 0 و "" مقدار نادرست و هر چیز دیگر مقدار درست طلقی میشوند.</p>
<p dir='rtl'>توجه داشته باشید که 0 نادرست و "0" درست طلقی میشوند حتی در عبارت 0=="0".</p>

<h2 dir='rtl'> 2. متغیر ها، آرایه ها و شئ ها </h2>

<p dir='rtl'>متغیر ها با کلید واژه var تعریف میشوند. اشیا در جاوااسکریپت دارای نوع پویا هستند، </p>
<p dir='rtl'>بدین شکل که برای تعریف نیازی به مشخص کردن نوع متعیر نیست. </p>
<p dir='rtl'>برای مقدار دهی از علامت مساوی استفاده میشود. </p>
```js
var someVar = 5;
```

<p dir='rtl'>اگر کلید واژه var را قرار ندهید، هیچ خطایی دریافت نخواهید کرد... </p>
```js
someOtherVar = 10;
```

<p dir='rtl'>در عوض  متغیر شما در گستره ی کل برنامه تعریف شده خواهد بود. </p>

<p dir='rtl'>متغیر هایی که تعریف شده ولی مقدار دهی نشوند، دارای مقدار undefined خواهند بود. </p>
```js
var someThirdVar; // = undefined
```

<p dir='rtl'>برای اعمال عملگر های محاسباتی، میانبر هایی وجود دارند: </p>
```js
someVar += 5; // equivalent to someVar = someVar + 5; someVar is 10 now
someVar *= 10; // now someVar is 100
```

<p dir='rtl'>حتی از این هم کوتاهتر برای اضافه یا کم کردن یک عدد با مقدار یک. </p>
```js
someVar++; // now someVar is 101
someVar--; // back to 100
```

<p dir='rtl'>آرایه ها در واقع لیستی مرتب شده از مقادیر مختلف از هر نوعی هستند. </p>
```js
var myArray = ["Hello", 45, true];
```

<p dir='rtl'>به اعضای یک آرایه میتوان از طریق قرار دادن کروشه در جلوی نام آن دسترسی پیدا کرد. </p>
<p dir='rtl'>نمایه ی آرایه از صفر شروع میشود. </p>
```js
myArray[1]; // = 45
```

<p dir='rtl'>آرایه ها ناپایدار و دارای طول قابل تغییر هستند </p>
```js
myArray.push("World");
myArray.length; // = 4
```

<p dir='rtl'>در جاوااسکریپت، اشیاء چیزی شبیه دیکشنری و یا نقشه در زبان های دیگر هستند: </p>
<p dir='rtl'>یک مجموعه ی نامرتب از جفت های کلید-مقدار. </p>
```js
var myObj = {key1: "Hello", key2: "World"};
```

<p dir='rtl'>کلید ها از نوع رشته هستند ولی در صورتی که مقدار معتبری برای اسم گزاری باشند نیازی به آوردن آنها درون گیومه نیست. </p>
```js
var myObj = {myKey: "myValue", "my other key": 4};
```

<p dir='rtl'>اعضای یک شئ را نیز میتوانید با استفاده از کروشه در مقابل نام آنها استخراج کنید. </p>
```js
myObj["my other key"]; // = 4
```

<p dir='rtl'>...و یا از طریق نقطه در صورتی که اسم عضو مورد نظر اسم معتبری برای اسم گزاری باشد.</p>
```js
myObj.myKey; // = "myValue"
```

<p dir='rtl'>اشیاء ناپایدار و قابل اضافه کردن عضو جدید هستند.</p>
```js
myObj.myThirdKey = true;
```

<p dir='rtl'>اگر سعی کنید عضوی را که وجود ندارد استخراج کنید، مقدار undefined را دریافت خواهید کرد. </p>
```js
myObj.myFourthKey; // = undefined
```

<h2 dir='rtl'>3. منطق و ساختار کنترل</h2>

<p dir='rtl'>ساختار if به شکلی که انتظارش را دارید کار میکند.</p>
```js
var count = 1;
if (count == 3){
    // evaluated if count is 3
} else if (count == 4) {
    // evaluated if count is 4
} else {
    // evaluated if it's not either 3 or 4
}
```

<p dir='rtl'>و همینطور حلقه while</p>
```js
while (true) {
    // An infinite loop!
}
```

<p dir='rtl'>حلقه do-while شبیه while است با این تفاوت که حداقل یکبار اجرا میشود.</p>
```js
var input
do {
    input = getInput();
} while (!isValid(input))
```

<p dir='rtl'>حلقه for همانند زبان C و جاوا کار می کند.</p>
<p dir='rtl'>مقدار دهی اولیه; شرط ادامه; چرخش حلقه</p>
```js
for (var i = 0; i < 5; i++){
    // will run 5 times
}
```

<p dir='rtl'>عملگر && و || به ترتیب "و" و "یا" ی منطقی هستند.</p>
```js
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear";
}
if (colour == "red" || colour == "blue"){
    // colour is either red or blue
}
```

<p dir='rtl'>از || همچنین میتوان برای تعیین مقدار پیشفرض استفاده کرد.</p>
```js
var name = otherName || "default";
```

<h2 dir='rtl'>4. توابع و مفاهیم گستره و بستار</h2>

<p dir='rtl'>توابع در جاوااسکریپت با استفاده از کلیدواژه ی function تعریف میشوند.</p>
```js
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"
```

<p dir='rtl'>توابع در جاوااسکریپت نوعی شئ پایه محسوب میشوند، بنابر این می توانید آنها را به اشیاء مختلف</p>
<p dir='rtl'>اضافه کنید و یا به عنوان پارامتر به توابع دیگر ارسال کنید.</p>
<p dir='rtl'>- برای مثال وقتی که با یک رویداد کار میکنید.</p>
```js
function myFunction(){
    // this code will be called in 5 seconds' time
}
setTimeout(myFunction, 5000);
```

<p dir='rtl'>توجه کنید که setTimeout تابعی تعریف شده در جاوااسکریپت نیست، ولی مرورگر ها و node.js از آن پشتیبانی میکنند.</p>


<p dir='rtl'>توابع نیازی به داشتن اسم ندارند. برای  مثال وقتی تابعی را به تابعی دیگر ارسال میکنید</p>
<p dir='rtl'>میتوانید آنرا به صورت بینام تعریف کنید.</p>
```js
setTimeout(function(){
    // this code will be called in 5 seconds' time
}, 5000);
```

<p dir='rtl'>توابع دارای محدوده ی متغیر های خود هستند.</p>
<p dir='rtl'>بر خلاف دیگر ساختار ها - مانند if</p>
```js
if (true){
    var i = 5;
}
i; // = 5 - not undefined as you'd expect in a block-scoped language
```

<p dir='rtl'>به همین دلیل الگوی خاصی به نام "تابعی که بلافاصله صدا زده میشود" پدید آمده </p>
<p dir='rtl'>تا از اضافه شدن متغیر های قسمتی از برنامه به گستره ی کلی برنامه جلوگیری شود.</p>
```js
(function(){
    var temporary = 5;
    // We can access the global scope by assiging to the 'global object', which
    // in a web browser is always 'window'. The global object may have a
    // different name in non-browser environments such as Node.js.
    window.permanent = 10;
})();
temporary; // raises ReferenceError
permanent; // = 10
```

<p dir='rtl'>یکی از برترین ویژگی های جاوااسکریپت مفهومی با نام بستار است</p>
<p dir='rtl'>بدین شکل که اگر تابعی درون تابع دیگری تعریف شود، تابع درونی به تمام متغیر های تابع خارجی دسترسی</p>
<p dir='rtl'>خواهد داشت، حتی بعد از اینکه تابع خارجی به اتمام رسیده باشد.</p>
```js
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!";
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout is asynchronous, so the sayHelloInFiveSeconds function will
    // exit immediately, and setTimeout will call inner afterwards. However,
    // because inner is "closed over" sayHelloInFiveSeconds, inner still has
    // access to the 'prompt' variable when it is finally called.
}
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
```

<h2 dir='rtl'>5. دیگر اشیاء، سازنده ها و پیش‌نمونه ها</h2>

<p dir='rtl'>اشیاء میتوانند تابع داشته باشند.</p>
```js
var myObj = {
    myFunc: function(){
        return "Hello world!";
    }
};
myObj.myFunc(); // = "Hello world!"
```

<p dir='rtl'>وقتی تابع یک شی صدا زده می شود، تابع میتواند به سایر مقادیر درون آن شی </p>
<p dir='rtl'>از طریق کلید واژه ی this دسترسی داشته باشد.</p>
```js
myObj = {
    myString: "Hello world!",
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = "Hello world!"
```


<p dir='rtl'>اینکه مقدار this چه باشد بستگی به این دارد که تابع چگونه صدا زده شود</p>
<p dir='rtl'>نه اینکه تابع کجا تعریف شده است.</p>
<p dir='rtl'>بنابر این تابع بالا اگر بدین شکل صدا زده شود کار نخواهد کرد</p>
```js
var myFunc = myObj.myFunc;
myFunc(); // = undefined
```


<p dir='rtl'>به همین شکل، تابعی که در جای دیگر تعریف شده را میتوانید به یک شی الحاق کنید</p>
<p dir='rtl'>و بدین ترتیب تابع میتواند به مقادیر درون شی از طریق this دسترسی پیدا کند.</p>
```js
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"
```


<p dir='rtl'>اگر تابعی با کلید new صدا زده شوند، شی جدیدی ایجاد شده و تابع در گستره ی آن صدا زده میشود.</p>
<p dir='rtl'>توابعی که بدین شکل صدا زده شوند در واقع نقش سازنده را ایفا می کنند.</p>
```js
var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5
```


<p dir='rtl'>تمامی اشیاء در جاوااسکریپت دارای  یک پیش نمونه هستند</p>
<p dir='rtl'>به شکلی که اگر تابع صدا زده شده بر روی شی مستقیما روی آن تعریف نشده باشد</p>
<p dir='rtl'>اجرا کننده ی برنامه در لیست پیش نمونه به دنبال آن تابع خواهد گشت</p>

<p dir='rtl'>برخی اجرا کننده های جاوااسکریپت به شما اجازه ی دسترسی به پیش نمونه های یک شی را از</p>
<p dir='rtl'>طریق عضو جادویی __proto__ میدهند.</p>
<p dir='rtl'>هرچند این به شناخت پیش نمونه ها کمک میکند ولی در حیطه ی جاوااسکریپت استاندارد قرار نمیگیرد.</p>
<p dir='rtl'>در ادامه شکل استاندارد پیش نمونه ها مورد بررسی قرار میگیرند.</p>
```js
var myObj = {
    myString: "Hello world!",
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};
myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42
```

<p dir='rtl'>این موضوع در مورد توابع نیز صدق میکند.</p>
```js
myObj.myFunc(); // = "hello world!"
```


<p dir='rtl'>اگر عضو مورد نظر در پیش نمونه ی شی یافت نشود، پیش نمونه ی پیش نمونه جستجو شده و الی آخر</p>
```js
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true
```


<p dir='rtl'>توجه داشته باشید که پیش نمونه ها کپی نمی شوند و هر شی جدید به پیش نمونه موجود اشاره میکند</p>
<p dir='rtl'>بدین ترتیب اگر تابعی به پیش نمونه اضافه شود تمامی اشیاء میتوانند به آن دسترسی پیدا کنند.</p>
```js
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43
```

<p dir='rtl'>پیش تر اشاره شد که __proto__ راه استانداردی برای دسترسی به پیش نمونه نیست و هیچ استانداردی نیز برای دسترسی به پیش نمونه ی یک شی موجود پیش بینی نشده است</p>
<p dir='rtl'>ولی دو راه برای ارائه پیش نمونه برای اشیاء جدید وجود دارد.</p>

<p dir='rtl'>اولی وقتیست که از تابع Object.create استفاده میشود - که اخیرا به زبان اضافه شده است و بنابراین بر روی همه ی پیاده سازی های آن وجود ندارد.</p>
```js
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43
```


<p dir='rtl'>راه دوم - که همه جا قابل استفاده است - مربوط به سازنده ها می شود.</p>
<p dir='rtl'>سازنده ها دارای عضوی با نام prototype هستند. این پیش نمونه ی خود سازنده نیست</p>
<p dir='rtl'>بلکه پیش نمونه ایست که به تمامی اشیاء ساخته شده توسط این سازنده الحاق میشود.</p>
```js
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function(){
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6
```


<p dir='rtl'>رشته ها و سایر سازنده های پیش ساخته ی زبان نیز دارای این ویژگی هستند.</p>
```js
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true
```


<p dir='rtl'>به جز این که این سازنده ها دقیقا مانند سازنده های دیگر نیستند.</p>
```js
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // This code won't execute, because 0 is falsy.
}
```


<p dir='rtl'>ولی به هر حال هم اشیاء عادی و هم اشیاء پیش ساخته هر دو در داشتن پیش نمونه مشترک هستند</p>
<p dir='rtl'>بنابر این شما میتوانید ویژگی و تابع جدیدی به رشته ها - به عنوان مثال - اضافه کنید.</p>


<p dir='rtl'>گاها به از این خاصیت با عنوان پلی فیل و برای اضافه کردن ویژگی های جدید به مجموعه ای از اشیاء فعلی زبان استفاده میشود </p>
<p dir='rtl'>که کاربرد فراوانی در پشتیبانی از نسخه های قدیمیتر مرورگر ها دارد.</p>
```js
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"
```


<p dir='rtl'>برای مثال، پیشتر اشاره کردیم که Object.create در نسخه های جدید پشتیبانی نشده است</p>
<p dir='rtl'>ولی میتوان آن را به صورت پلی فیل استفاده کرد.</p>
```js
if (Object.create === undefined){ // don't overwrite it if it exists
    Object.create = function(proto){
        // make a temporary constructor with the right prototype
        var Constructor = function(){};
        Constructor.prototype = proto;
        // then use it to create a new, appropriately-prototyped object
        return new Constructor();
    }
}
```

<h2 dir='rtl'> منابع دیگر </h2>

The [Mozilla Developer
Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript) 
<p dir='rtl'>مرجعی بسیار خوب برای جاوااسکریپت به شکلی که در مرورگر ها مورد استفاده قرار گرفته است.</p>
<p dir='rtl'>از آنجایی که این منبع یک ویکی میباشد همانطور که مطالب بیشتری یاد میگیرید میتوانید به دیگران نیز در یادگیری آن کمک کنید.</p>

MDN's [A re-introduction to
JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
<p dir='rtl'>مشابه مطالبی که اینجا مطرح شده با جزییات بیشتر. در اینجا به شکل عمدی جاوااسکریپت فقط از دیدگاه زبان برنامه نویسی مورد بررسی قرار گرفته</p>
<p dir='rtl'>در حالی که در این منبع میتوانید بیشتر از کاربرد آن در صفحات وب آشنایی پیدا کنید.</p>
[Document Object
Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)

[Javascript Garden](http://bonsaiden.github.io/JavaScript-Garden/) 
<p dir='rtl'>راهنمای دقیقی از قسمت های غیر ملموس زبان.</p>

<p dir='rtl'>اضافه بر این در ویرایش این مقاله، قسمت هایی از سایت زیر مورد استفاده قرار گرفته است.</p>
Louie Dinh's Python tutorial on this site, and the [JS
Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
on the Mozilla Developer Network.
---
language: factor
contributors:
    - ["hyphz", "http://github.com/hyphz/"]
filename: learnfactor.factor
---

Factor is a modern stack-based language, based on Forth, created by Slava Pestov.

Code in this file can be typed into Factor, but not directly imported because the vocabulary and import header would make the beginning thoroughly confusing.

```
! This is a comment

! Like Forth, all programming is done by manipulating the stack.
! Stating a literal value pushes it onto the stack.
5 2 3 56 76 23 65    ! No output, but stack is printed out in interactive mode

! Those numbers get added to the stack, from left to right.
! .s prints out the stack non-destructively.
.s     ! 5 2 3 56 76 23 65

! Arithmetic works by manipulating data on the stack.
5 4 +    ! No output

! `.` pops the top result from the stack and prints it.
.    ! 9

! More examples of arithmetic:
6 7 * .        ! 42
1360 23 - .    ! 1337
12 12 / .      ! 1
13 2 mod .     ! 1

99 neg .       ! -99
-99 abs .      ! 99
52 23 max .    ! 52
52 23 min .    ! 23

! A number of words are provided to manipulate the stack, collectively known as shuffle words.

3 dup -          ! duplicate the top item (1st now equals 2nd): 3 - 3
2 5 swap /       ! swap the top with the second element:        5 / 2
4 0 drop 2 /     ! remove the top item (don't print to screen):  4 / 2
1 2 3 nip .s     ! remove the second item (similar to drop):    1 3
1 2 clear .s     ! wipe out the entire stack
1 2 3 4 over .s  ! duplicate the second item to the top: 1 2 3 4 3
1 2 3 4 2 pick .s ! duplicate the third item to the top: 1 2 3 4 2 3

! Creating Words
! The `:` word sets Factor into compile mode until it sees the `;` word.
: square ( n -- n ) dup * ;    ! No output
5 square .                     ! 25

! We can view what a word does too.
! \ suppresses evaluation of a word and pushes its identifier on the stack instead.
\ square see    ! : square ( n -- n ) dup * ;

! After the name of the word to create, the declaration between brackets gives the stack effect.
! We can use whatever names we like inside the declaration:
: weirdsquare ( camel -- llama ) dup * ;

! Provided their count matches the word's stack effect:
: doubledup ( a -- b ) dup dup ; ! Error: Stack effect declaration is wrong
: doubledup ( a -- a a a ) dup dup ; ! Ok
: weirddoubledup ( i -- am a fish ) dup dup ; ! Also Ok

! Where Factor differs from Forth is in the use of quotations.
! A quotation is a block of code that is pushed on the stack as a value.
! [ starts quotation mode; ] ends it.
[ 2 + ]       ! Quotation that adds 2 is left on the stack
4 swap call . ! 6

! And thus, higher order words. TONS of higher order words.
2 3 [ 2 + ] dip .s      ! Pop top stack value, run quotation, push it back: 4 3
3 4 [ + ] keep .s       ! Copy top stack value, run quotation, push the copy: 7 4
1 [ 2 + ] [ 3 + ] bi .s ! Run each quotation on the top value, push both results: 3 4
4 3 1 [ + ] [ + ] bi .s ! Quotations in a bi can pull values from deeper on the stack: 4 5 ( 1+3 1+4 )
1 2 [ 2 + ] bi@ .s      ! Run the quotation on first and second values
2 [ + ] curry           ! Inject the given value at the start of the quotation: [ 2 + ] is left on the stack

! Conditionals
! Any value is true except the built-in value f.
! A built-in value t does exist, but its use isn't essential.
! Conditionals are higher order words as with the combinators above.

5 [ "Five is true" . ] when                     ! Five is true
0 [ "Zero is true" . ] when                     ! Zero is true
f [ "F is true" . ] when                        ! No output
f [ "F is false" . ] unless                     ! F is false
2 [ "Two is true" . ] [ "Two is false" . ] if   ! Two is true

! By default the conditionals consume the value under test, but starred variants
! leave it alone if it's true:

5 [ . ] when*      ! 5
f [ . ] when*      ! No output, empty stack, f is consumed because it's false


! Loops
! You've guessed it.. these are higher order words too.

5 [ . ] each-integer               ! 0 1 2 3 4
4 3 2 1 0 5 [ + . ] each-integer   ! 0 2 4 6 8
5 [ "Hello" . ] times              ! Hello Hello Hello Hello Hello

! Here's a list:
{ 2 4 6 8 }                        ! Goes on the stack as one item

! Loop through the list:
{ 2 4 6 8 } [ 1 + . ] each          ! Prints 3 5 7 9
{ 2 4 6 8 } [ 1 + ] map             ! Leaves { 3 5 7 9 } on stack

! Loop reducing or building lists:
{ 1 2 3 4 5 } [ 2 mod 0 = ] filter  ! Keeps only list members for which quotation yields true: { 2 4 }  
{ 2 4 6 8 } 0 [ + ] reduce .        ! Like "fold" in functional languages: prints 20 (0+2+4+6+8)
{ 2 4 6 8 } 0 [ + ] accumulate . .  ! Like reduce but keeps the intermediate values in a list: prints { 0 2 6 12 } then 20
1 5 [ 2 * dup ] replicate .         ! Loops the quotation 5 times and collects the results in a list: { 2 4 8 16 32 }
1 [ dup 100 < ] [ 2 * dup ] produce ! Loops the second quotation until the first returns false and collects the results: { 2 4 8 16 32 64 128 }

! If all else fails, a general purpose while loop:
1 [ dup 10 < ] [ "Hello" . 1 + ] while  ! Prints "Hello" 10 times
                                        ! Yes, it's hard to read
                                        ! That's what all those variant loops are for

! Variables
! Usually Factor programs are expected to keep all data on the stack.
! Using named variables makes refactoring harder (and it's called Factor for a reason)
! Global variables, if you must:

SYMBOL: name            ! Creates name as an identifying word
"Bob" name set-global   ! No output
name get-global .       ! "Bob"                     

! Named local variables are considered an extension but are available
! In a quotation..
[| m n                  ! Quotation captures top two stack values into m and n
 | m n + ]              ! Read them

! Or in a word..
:: lword ( -- )           ! Note double colon to invoke lexical variable extension
   2 :> c                 ! Declares immutable variable c to hold 2
   c . ;                  ! Print it out

! In a word declared this way,  the input side of the stack declaration
! becomes meaningful and gives the variable names stack values are captured into
:: double ( a -- result ) a 2 * ;

! Variables are declared mutable by ending their name with a shriek
:: mword2 ( a! -- x y )   ! Capture top of stack in mutable variable a
   a                      ! Push a
   a 2 * a!               ! Multiply a by 2 and store result back in a
   a ;                    ! Push new value of a
5 mword2                  ! Stack: 5 10

! Lists and Sequences
! We saw above how to push a list onto the stack

0 { 1 2 3 4 } nth         ! Access a particular member of a list: 1
10 { 1 2 3 4 } nth        ! Error: sequence index out of bounds
1 { 1 2 3 4 } ?nth        ! Same as nth if index is in bounds: 2
10 { 1 2 3 4 } ?nth       ! No error if out of bounds: f

{ "at" "the" "beginning" } "Append" prefix    ! { "Append" "at" "the" "beginning" }
{ "Append" "at" "the" } "end" suffix          ! { "Append" "at" "the" "end" }
"in" 1 { "Insert" "the" "middle" } insert-nth ! { "Insert" "in" "the" "middle" }
"Concat" "enate" append                       ! "Concatenate" - strings are sequences too
"Concatenate" "Reverse " prepend              ! "Reverse Concatenate"
{ "Concatenate " "seq " "of " "seqs" } concat ! "Concatenate seq of seqs"
{ "Connect" "subseqs" "with" "separators" } " " join  ! "Connect subseqs with separators"

! And if you want to get meta, quotations are sequences and can be dismantled..
0 [ 2 + ] nth                              ! 2
1 [ 2 + ] nth                              ! +
[ 2 + ] \ - suffix                         ! Quotation [ 2 + - ]


```

##Ready For More?

* [Factor Documentation](http://docs.factorcode.org/content/article-help.home.html)
---
name: Go
category: language
language: Go
filename: learngo-fi.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
    - ["Alexej Friesen", "https://github.com/heyalexej"]
    - ["Clayton Walker", "https://github.com/cwalk"]
translators:
    - ["Timo Virkkunen", "https://github.com/ComSecNinja"]
lang: fi-fi
---

Go luotiin työn tekemistä varten. Se ei ole tietojenkäsittelyn uusin trendi,
mutta se on uusin nopein tapa ratkaista oikean maailman ongelmia.

Sillä on staattisesti tyypitetyistä imperatiivisista kielistä tuttuja
konsepteja. Se kääntyy ja suorittuu nopeasti, lisää helposti käsitettävän
samanaikaisten komentojen suorittamisen nykyaikaisten moniytimisten
prosessoreiden hyödyntämiseksi ja antaa käyttäjälle ominaisuuksia suurten
projektien käsittelemiseksi.

Go tuo mukanaan loistavan oletuskirjaston sekä innokkaan yhteisön.  

```go
// Yhden rivin kommentti
/* Useamman
 rivin kommentti */

// Package -lausekkeella aloitetaan jokainen lähdekooditiedosto.
// Main on erityinen nimi joka ilmoittaa
// suoritettavan tiedoston kirjaston sijasta.
package main

// Import -lauseke ilmoittaa tässä tiedostossa käytetyt kirjastot.
import (
	"fmt"       // Paketti Go:n oletuskirjastosta.
	"io/ioutil" // Implementoi hyödyllisiä I/O -funktioita.
	m "math"    // Matematiikkakirjasto jolla on paikallinen nimi m.
	"net/http"  // Kyllä, web-palvelin!
	"strconv"   // Kirjainjonojen muuntajia.
)

// Funktion määrittelijä. Main on erityinen: se on ohjelman suorittamisen
// aloittamisen alkupiste. Rakasta tai vihaa sitä, Go käyttää aaltosulkeita.
func main() {
    // Println tulostaa rivin stdoutiin.
	// Se tulee paketin fmt mukana, joten paketin nimi on mainittava.
	fmt.Println("Hei maailma!")

    // Kutsu toista funktiota tämän paketin sisällä.
	beyondHello()
}

// Funktioilla voi olla parametrejä sulkeissa.
// Vaikkei parametrejä olisikaan, sulkeet ovat silti pakolliset.
func beyondHello() {
	var x int // Muuttujan ilmoittaminen: ne täytyy ilmoittaa ennen käyttöä.
	x = 3     // Arvon antaminen muuttujalle.
    // "Lyhyet" ilmoitukset käyttävät := joka päättelee tyypin, ilmoittaa
    // sekä antaa arvon muuttujalle.
	y := 4
	sum, prod := learnMultiple(x, y)          // Funktio palauttaa kaksi arvoa.
	fmt.Println("summa:", sum, "tulo:", prod) // Yksinkertainen tuloste.
	learnTypes()                              // < y minuuttia, opi lisää!
}

/* <- usean rivin kommentti
Funktioilla voi olla parametrejä ja (useita!) palautusarvoja.
Tässä `x`, `y` ovat argumenttejä ja `sum`, `prod` ovat ne, mitä palautetaan.
Huomaa että `x` ja `sum` saavat tyyin `int`.
*/
func learnMultiple(x, y int) (sum, prod int) {
	return x + y, x * y // Palauta kaksi arvoa.
}

// Sisäänrakennettuja tyyppejä ja todellisarvoja.
func learnTypes() {
    // Lyhyt ilmoitus antaa yleensä haluamasi.
	str := "Opi Go!" // merkkijonotyyppi.

	s2 := `"raaka" todellisarvoinen merrkijono
voi sisältää rivinvaihtoja.` // Sama merkkijonotyyppi.

    // Ei-ASCII todellisarvo. Go-lähdekoodi on UTF-8.
	g := 'Σ' // riimutyyppi, lempinimi int32:lle, sisältää unicode-koodipisteen.

	f := 3.14195 //float64, IEEE-754 64-bittinen liukuluku.
	c := 3 + 4i  // complex128, sisäisesti ilmaistu kahdella float64:lla.

	// var -syntaksi alkuarvoilla.
	var u uint = 7 // Etumerkitön, toteutus riippuvainen koosta kuten int.
	var pi float32 = 22. / 7

    // Muuntosyntaksi lyhyellä ilmoituksella.
	n := byte('\n') // byte on leminimi uint8:lle.

    // Listoilla on kiinteä koko kääntöhetkellä.
	var a4 [4]int           // 4 int:in lista, alkiot ovat alustettu nolliksi.
	a3 := [...]int{3, 1, 5} // Listan alustaja jonka kiinteäksi kooksi tulee 3
	// alkiota, jotka saavat arvot 3, 1, ja 5.

    // Siivuilla on muuttuva koko. Sekä listoilla että siivuilla on puolensa,
    // mutta siivut ovat yleisempiä käyttötapojensa vuoksi.
	s3 := []int{4, 5, 9}    // Vertaa a3: ei sananheittoa (...).
	s4 := make([]int, 4)    // Varaa 4 int:n siivun, alkiot alustettu nolliksi.
	var d2 [][]float64      // Vain ilmoitus, muistia ei varata.
	bs := []byte("a slice") // Tyypinmuuntosyntaksi.

    // Koska siivut ovat dynaamisia, niitä voidaan yhdistellä sellaisinaan.
    // Lisätäksesi alkioita siivuun, käytä sisäänrakennettua append()-funktiota.
	// Ensimmäinen argumentti on siivu, johon alkoita lisätään.
	s := []int{1, 2, 3}		// Tuloksena on kolmen alkion pituinen lista.
	s = append(s, 4, 5, 6)	// Lisätty kolme alkiota. Siivun pituudeksi tulee 6.
	fmt.Println(s) // Päivitetty siivu on nyt [1 2 3 4 5 6]

    // Lisätäksesi siivun toiseen voit antaa append-funktiolle referenssin
    // siivuun tai todellisarvoiseen siivuun lisäämällä sanaheiton argumentin
    // perään. Tämä tapa purkaa siivun alkiot ja lisää ne siivuun s.
	s = append(s, []int{7, 8, 9}...) // 2. argumentti on todellisarvoinen siivu.
	fmt.Println(s)	// Päivitetty siivu on nyt [1 2 3 4 5 6 7 8 9]

	p, q := learnMemory() // Ilmoittaa p ja q olevan tyyppiä osoittaja int:iin.
	fmt.Println(*p, *q)   // * seuraa osoittajaa. Tämä tulostaa kaksi int:ä.

    // Kartat ovat dynaamisesti kasvavia assosiatiivisia listoja, kuten hash tai
    // dictionary toisissa kielissä.
	m := map[string]int{"three": 3, "four": 4}
	m["one"] = 1

    // Käyttämättömät muuttujat ovat virheitä Go:ssa.
    // Alaviiva antaa sinun "käyttää" muuttujan mutta hylätä sen arvon.
	_, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs
	// Tulostaminen tietysti lasketaan muuttujan käyttämiseksi.
	fmt.Println(s, c, a4, s3, d2, m)

	learnFlowControl() // Takaisin flowiin.
}

// Go:ssa on useista muista kielistä poiketen mahdollista käyttää nimettyjä
// palautusarvoja.
// Nimen antaminen palautettavan arvon tyypille funktion ilmoitusrivillä
// mahdollistaa helpon palaamisen useasta eri funktion suorituskohdasta sekä
// pelkän return-lausekkeen käytön ilman muita mainintoja.
func learnNamedReturns(x, y int) (z int) {
	z = x * y
	return // z on epäsuorasti tässä, koska nimesimme sen aiemmin.
}

// Go kerää kaikki roskansa. Siinä on osoittajia mutta ei niiden laskentoa.
// Voit tehdä virheen mitättömällä osoittajalla, mutta et
// kasvattamalla osoittajaa.
func learnMemory() (p, q *int) {
    // Nimetyillä palautusarvoilla p ja q on tyyppi osoittaja int:iin.
	p = new(int) // Sisäänrakennettu funktio new varaa muistia.
    // Varattu int on alustettu nollaksi, p ei ole enää mitätön.
	s := make([]int, 20) // Varaa 20 int:ä yhteen kohtaan muistissa.
	s[3] = 7             // Anna yhdelle niistä arvo.
	r := -2              // Ilmoita toinen paikallinen muuttuja.
	return &s[3], &r     // & ottaa asian osoitteen muistissa.
}

func expensiveComputation() float64 {
	return m.Exp(10)
}

func learnFlowControl() {
    // If -lausekkeet vaativat aaltosulkeet mutta ei tavallisia sulkeita.
	if true {
		fmt.Println("mitä mä sanoin")
	}
	// Muotoilu on standardoitu käyttämällä komentorivin komentoa "go fmt".
	if false {
		// Nyrpistys.
	} else {
		// Nautinto.
	}
	// Käytä switch -lauseketta ketjutettujen if -lausekkeiden sijasta.
	x := 42.0
	switch x {
	case 0:
	case 1:
	case 42:
		// Tapaukset eivät "tipu läpi".
		/*
		Kuitenkin meillä on erikseen `fallthrough` -avainsana. Katso:
		  https://github.com/golang/go/wiki/Switch#fall-through
		*/
	case 43:
		// Saavuttamaton.
	default:
		// Oletustapaus (default) on valinnainen.
	}
    // Kuten if, for -lauseke ei myöskään käytä tavallisia sulkeita.
	// for- ja if- lausekkeissa ilmoitetut muuttujat ovat paikallisia niiden
    // piireissä.
	for x := 0; x < 3; x++ { // ++ on lauseke. Sama kuin "x = x + 1".
		fmt.Println("iteraatio", x)
	}
	// x == 42 tässä.

    // For on kielen ainoa silmukkalauseke mutta sillä on vaihtoehtosia muotoja.
	for { // Päättymätön silmukka.
		break    // Kunhan vitsailin.
		continue // Saavuttamaton.
	}

    // Voit käyttää range -lauseketta iteroidaksesi listojen, siivujen, merkki-
    // jonojen, karttojen tai kanavien läpi. range palauttaa yhden (kanava) tai
    // kaksi arvoa (lista, siivu, merkkijono ja kartta).
	for key, value := range map[string]int{"yksi": 1, "kaksi": 2, "kolme": 3} {
        // jokaista kartan paria kohden, tulosta avain ja arvo
		fmt.Printf("avain=%s, arvo=%d\n", key, value)
	}

    // Kuten for -lausekkeessa := if -lausekkeessa tarkoittaa ilmoittamista ja
    // arvon asettamista.
    // Aseta ensin y, sitten testaa onko y > x.
	if y := expensiveComputation(); y > x {
		x = y
	}
    // Todellisarvoiset funktiot ovat sulkeumia.
	xBig := func() bool {
		return x > 10000 // Viittaa ylempänä ilmoitettuun x:ään.
	}
	fmt.Println("xBig:", xBig()) // tosi (viimeisin arvo on e^10).
	x = 1.3e3                    // Tämä tekee x == 1300
	fmt.Println("xBig:", xBig()) // epätosi nyt.

    // Lisäksi todellisarvoiset funktiot voidaan samalla sekä ilmoittaa että
    // kutsua, jolloin niitä voidaan käyttää funtioiden argumentteina kunhan:
    // a) todellisarvoinen funktio kutsutaan välittömästi (),
    // b) palautettu tyyppi vastaa odotettua argumentin tyyppiä.
	fmt.Println("Lisää ja tuplaa kaksi numeroa: ",
		func(a, b int) int {
			return (a + b) * 2
		}(10, 2)) // Kutsuttu argumenteilla 10 ja 2
	// => Lisää ja tuplaa kaksi numeroa: 24

	// Kun tarvitset sitä, rakastat sitä.
	goto love
love:

	learnFunctionFactory() // Funktioita palauttavat funktiot
	learnDefer()      // Nopea kiertoreitti tärkeään avainsanaan.
	learnInterfaces() // Hyvää kamaa tulossa!
}

func learnFunctionFactory() {
    // Seuraavat kaksi ovat vastaavia, mutta toinen on käytännöllisempi
	fmt.Println(sentenceFactory("kesä")("Kaunis", "päivä!"))

	d := sentenceFactory("kesä")
	fmt.Println(d("Kaunis", "päivä!"))
	fmt.Println(d("Laiska", "iltapäivä!"))
}

// Somisteet ovat yleisiä toisissa kielissä. Sama saavutetaan Go:ssa käyttämällä
// todellisarvoisia funktioita jotka ottavat vastaan argumentteja.
func sentenceFactory(mystring string) func(before, after string) string {
	return func(before, after string) string {
		return fmt.Sprintf("%s %s %s", before, mystring, after) // uusi jono
	}
}

func learnDefer() (ok bool) {
    // Lykätyt lausekkeet suoritetaan juuri ennen funktiosta palaamista.
	defer fmt.Println("lykätyt lausekkeet suorittuvat")
    defer fmt.Println("käänteisessä järjestyksessä (LIFO).")
	defer fmt.Println("\nTämä rivi tulostuu ensin, koska")
    // Defer -lauseketta käytetään yleisesti tiedoston sulkemiseksi, jotta
    // tiedoston sulkeva funktio pysyy lähellä sen avannutta funktiota.
	return true
}

// Määrittele Stringer rajapintatyypiksi jolla on
// yksi jäsenfunktio eli metodi, String.
type Stringer interface {
	String() string
}

// Määrittele pair rakenteeksi jossa on kaksi kenttää, x ja y tyyppiä int.
type pair struct {
	x, y int
}

// Määrittele jäsenfunktio pair:lle. Pair tyydyttää nyt Stringer -rajapinnan.
func (p pair) String() string { // p:tä kutsutaan nimellä "receiver"
    // Sprintf on toinen julkinen funktio paketissa fmt.
	// Pistesyntaksilla viitataan P:n kenttiin.
	return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
    // Aaltosuljesyntaksi on "todellisarvoinen rakenne". Se todentuu alustetuksi
    // rakenteeksi. := -syntaksi ilmoittaa ja alustaa p:n täksi rakenteeksi.
	p := pair{3, 4}
	fmt.Println(p.String()) // Kutsu p:n (tyyppiä pair) jäsenfunktiota String.
	var i Stringer          // Ilmoita i Stringer-rajapintatyypiksi.
	i = p                   // Pätevä koska pair tyydyttää rajapinnan Stringer.
    // Kutsu i:n (Stringer) jäsenfunktiota String. Tuloste on sama kuin yllä.
	fmt.Println(i.String())

    // Funktiot fmt-paketissa kutsuvat argumenttien String-jäsenfunktiota
    // selvittääkseen onko niistä saatavilla tulostettavaa vastinetta.
	fmt.Println(p) // Tuloste on sama kuin yllä. Println kutsuu String-metodia.
	fmt.Println(i) // Tuloste on sama kuin yllä.

	learnVariadicParams("loistavaa", "oppimista", "täällä!")
}

// Funktioilla voi olla muuttuva eli variteettinen
// määrä argumentteja eli parametrejä.
func learnVariadicParams(myStrings ...interface{}) {
    // Iteroi jokaisen argumentin läpi.
    // Tässä alaviivalla sivuutetaan argumenttilistan kunkin kohdan indeksi.
	for _, param := range myStrings {
		fmt.Println("param:", param)
	}

    // Luovuta variteettinen arvo variteettisena parametrinä.
	fmt.Println("params:", fmt.Sprintln(myStrings...))

	learnErrorHandling()
}

func learnErrorHandling() {
	// "; ok" -muotoa käytetään selvittääksemme toimiko jokin vai ei.
	m := map[int]string{3: "kolme", 4: "neljä"}
	if x, ok := m[1]; !ok { // ok on epätosi koska 1 ei ole kartassa.
		fmt.Println("ei ketään täällä")
	} else {
		fmt.Print(x) // x olisi arvo jos se olisi kartassa.
	}
    // Virhearvo voi kertoa muutakin ongelmasta.
	if _, err := strconv.Atoi("ei-luku"); err != nil { // _ sivuuttaa arvon
        // tulostaa strconv.ParseInt: parsing "ei-luku": invalid syntax
		fmt.Println(err)
	}
    // Palaamme rajapintoihin hieman myöhemmin. Sillä välin,
	learnConcurrency()
}

// c on kanava, samanaikaisturvallinen viestintäolio.
func inc(i int, c chan int) {
	c <- i + 1 // <- on "lähetysoperaattori" kun kanava on siitä vasemmalla.
}

// Käytämme inc -funktiota samanaikaiseen lukujen lisäämiseen.
func learnConcurrency() {
    // Sama make -funktio jota käytimme aikaisemmin siivun luomiseksi. Make
    // varaa muistin ja alustaa siivut, kartat ja kanavat.
	c := make(chan int)
    // Aloita kolme samanaikaista gorutiinia (goroutine). Luvut kasvavat
    // samanaikaisesti ja ehkäpä rinnakkain jos laite on kykenevä ja oikein
    // määritelty. Kaikki kolme lähettävät samalle kanavalle.
	go inc(0, c) // go -lauseke aloittaa uuden gorutiinin.
	go inc(10, c)
	go inc(-805, c)
    // Lue kolme palautusarvoa kanavalta ja tulosta ne.
    // Niiden saapumisjärjestystä ei voida taata!
    // <- on "vastaanotto-operaattori" jos kanava on oikealla
	fmt.Println(<-c, <-c, <-c)

	cs := make(chan string)       // Toinen kanava joka käsittelee merkkijonoja.
	ccs := make(chan chan string) // Kanava joka käsittelee merkkijonokanavia.
	go func() { c <- 84 }()       // Aloita uusi gorutiini arvon lähettämiseksi.
	go func() { cs <- "sanaa" }() // Uudestaan, mutta cs -kanava tällä kertaa.
    // Select -lausekkeella on syntaksi kuten switch -lausekkeella mutta
    // jokainen tapaus sisältää kanavaoperaation. Se valitsee satunnaisen
    // tapauksen niistä kanavista, jotka ovat kommunikaatiovalmiita
	select {
	case i := <-c: // Vastaanotettu arvo voidaan antaa muuttujalle
		fmt.Printf("se on %T", i)
	case <-cs: // tai vastaanotettu arvo voidaan sivuuttaa.
		fmt.Println("se on merkkijono")
	case <-ccs: // Tyhjä kanava; ei valmis kommunikaatioon.
		fmt.Println("ei tapahtunut.")
	}
    // Tässä vaiheessa arvo oli otettu joko c:ltä tai cs:ltä. Yksi kahdesta
    // ylempänä aloitetusta gorutiinista on valmistunut, toinen pysyy tukossa.

	learnWebProgramming() // Go tekee sitä. Sinäkin haluat tehdä sitä.
}

// Yksittäinen funktio http -paketista aloittaa web-palvelimen.
func learnWebProgramming() {

    // ListenAndServe:n ensimmäinen parametri on TCP-osoite, jota kuunnellaan.
    // Toinen parametri on rajapinta, http.Handler.
	go func() {
		err := http.ListenAndServe(":8080", pair{})
		fmt.Println(err) // älä sivuuta virheitä.
	}()

	requestServer()
}

// Tee pair:sta http.Handler implementoimalla sen ainoa metodi, ServeHTTP.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // Tarjoa dataa metodilla http.ResponseWriter.
	w.Write([]byte("Opit Go:n Y minuutissa!"))
}

func requestServer() {
	resp, err := http.Get("http://localhost:8080")
	fmt.Println(err)
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Printf("\nWeb-palvelin sanoo: `%s`", string(body))
}
```

## Lisää luettavaa

Go-tietämyksen alku ja juuri on sen [virallinen verkkosivu]()(http://golang.org/).
Siellä voit seurata oppitunteja, askarrella vuorovaikutteisesti sekä lukea paljon.
Kierroksen lisäksi [dokumentaatio](https://golang.org/doc/) pitää sisällään tietoa
siistin Go-koodin kirjoittamisesta, pakettien ja komentojen käytöstä sekä julkaisuhistoriasta.

Kielen määritelmä itsessään on suuresti suositeltavissa. Se on helppolukuinen ja
yllättävän lyhyt (niissä määrin kuin kielimääritelmät nykypäivänä ovat.)

Voit askarrella parissa kanssa [Go playgroundissa](https://play.golang.org/p/tnWMjr16Mm).
Muuttele sitä ja aja se selaimestasi! Huomaa, että voit käyttää [https://play.golang.org](https://play.golang.org)
[REPL:na](https://en.wikipedia.org/wiki/Read-eval-print_loop) testataksesi ja koodataksesi selaimessasi, ilman Go:n asentamista.

Go:n opiskelijoiden lukulistalla on [oletuskirjaston lähdekoodi](http://golang.org/src/pkg/).
Kattavasti dokumentoituna se antaa parhaan kuvan helppolukuisesta ja ymmärrettävästä Go-koodista,
-tyylistä ja -tavoista. Voit klikata funktion nimeä [doukumentaatiossa](http://golang.org/pkg/) ja
lähdekoodi tulee esille!

Toinen loistava paikka oppia on [Go by example](https://gobyexample.com/).

Go Mobile lisää tuen mobiilialustoille (Android ja iOS). Voit kirjoittaa pelkällä Go:lla natiiveja applikaatioita tai tehdä kirjaston joka sisältää sidoksia
Go-paketista, jotka puolestaan voidaan kutsua Javasta (Android) ja Objective-C:stä (iOS). Katso [lisätietoja](https://github.com/golang/go/wiki/Mobile).
---
language: markdown
filename: markdown-fi.md
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Timo Virkkunen", "https://github.com/ComSecNinja"]
lang: fi-fi
---

John Gruber loi Markdownin vuona 2004. Sen tarkoitus on olla helposti luettava ja kirjoitettava syntaksi joka muuntuu helposti HTML:ksi (ja nyt myös moneksi muuksi formaatiksi).

```markdown
<!-- Jokainen HTML-tiedosto on pätevää Markdownia. Tämä tarkoittaa että voimme
käyttää HTML-elementtejä Markdownissa, kuten kommentteja, ilman että markdown
-jäsennin vaikuttaa niihin. Tästä johtuen et voi kuitenkaan käyttää markdownia
HTML-elementtien sisällä jos luot sellaisen markdown-tiedostoon. -->

<!-- Markdownin toteutus vaihtelee jäsentimestä toiseen. Tämä opas yrittää
selventää mitkä ominaisuudet ovat yleisiä ja mitkä ovat eritysesti tiettyjen
jäsentimien ominaisuuksia. -->

<!-- Otsikot -->
<!-- Voit luoda HTML-elementtejä <h1> - <h6> helposti aloittamalla rivin
haluamallasi määrällä ristikkomerkkejä (#).  -->
# Tämä on <h1>
## Tämä on <h2>
### Tämä on <h3>
#### Tämä on <h4>
##### Tämä on <h5>
###### Tämä on <h6>

<!-- Markdownissa on myös vaihtoehtoisia tapoja ilmaista h1 ja h2. -->
Tämä on h1
=============

Tämä on h2
-------------

<!-- Yksinkertaiset tekstimuotoilut -->
<!-- Tekstin voi helposti muotoilla kursiiviksi tai lihavoiduksi. -->

*Tämä teksti on kursivoitua.*
_Kuten on myös tämä teksti._

**Tämä teksti on lihavoitua.**
__Kuten on tämäkin teksti.__

***Tämä teksti on molempia.***
**_Kuten tämäkin!_**
*__Kuten tämäkin!__*

<!-- GitHub-tyylisessä Markdownissa, jota käytetään tiedostojen esittämiseksi
GitHubissa, meillä on käytössämme myös yliviivaus: -->

~~Tämä teksti on yliviivattua.~~

<!-- Kappaleet ovat yhdellä tai useammalla peräkkäisellä tekstirivillä jotka
erotellaan yhdellä tai useammalla tyhjällä rivillä -->

Tämä on kappala. Kirjoittelen kappaleeseen, eikö tämä olekin hauskaa?

Nyt olen kappaleessa 2.
Olen edelleen toisessa kappaleessa!


Olen kolmannessa kappaleessa!

<!-- Jos haluat lisätä <br /> HTML-elementin, päätä kappale kahdella tai
useammalla välilyönnillä ja aloita sitten uusi kappale -->

Päätän tämän kahteen välilyöntiin (maalaa minut nähdäksesi ne).  

There's a <br /> above me!

<!-- Lainaukset ovat helppoja ja ne tehdään >-merkillä -->

> Tämä on lainaus. Voit joko
> manuaalisesti rivittää tekstisi ja laittaa >-merkin jokaisen rivin eteen tai antaa jäsentimen rivittää pitkät tekstirivit.
> Sillä ei ole merkitystä kunhan rivit alkavat >-merkillä.

> Voit myös käyttää useampaa
>> sisennystasoa
> Kuinka hienoa se on?

<!-- Listat -->
<!-- Järjestämättömät listat tehdään asteriskilla, plussalla tai viivalla -->

* Kohta
* Kohta
* Kolmas kohta

tai

+ Kohta
+ Kohta
+ Kolmas kohta

tai

- Kohta
- Kohta
- Kolmas kohta

<!-- Järjestetyt listat tehdään järjestysluvuilla. -->

1. Kohta yksi
2. Kohta kaksi
3. Kohta kolme

<!-- Sinun ei tarvitse edes merkitä kohtia oikein ja silti markdown näyttää
oikean järjestyksen, mutta se ei välttämättä ole hyvä idea. -->

1. Kohta yksi
1. Kohta kaksi
1. Kohta kolme
<!-- (Tämä korjaantuu samanlaiseksi kuin yllä oleva esimerkki) -->

<!-- Voit myös käyttää alalistoja. -->

1. Kohta yksi
2. Kohta kaksi
3. Kohta kolme
    * Alakohta
    * Alakohta
4. Kohta neljä

<!-- Myös tehtävälistoja on olemassa. Tämä tekee HTML-valintaruutuja. -->

Alla olevat ruudut ilman x-merkkiä ovat merkitsemättömiä HTML-valintaruutuja.
- [ ] Ensimmäinen suoritettava tehtävä.
- [ ] Toinen tehtävä joka täytyy tehdä
Tämä alla oleva ruutu on merkitty HTML-valintaruutu.
- [x] Tämä tehtävä on suoritettu

<!-- Koodiosiot -->
<!-- Voit merkitä koodiosion (jaka käyttää <code> -elementtiä) sisentämällä
rivin neljällä välilyönnillä tai tabulaattorilla. -->

    Tämä on koodia
    Kuten tämäkin

<!-- Voit myös sisentää koodia samalla tavalla. -->

    my_array.each do |item|
        puts item
    end

<!-- Muun tekstin seassa oleva koodi merkitään kahden `-merkin väliin -->

John ei tiennyt edes mitä `go_to()` -funktio teki!

<!-- GitHubin Markdownissa voit käyttää erityissyntaksia koodille. -->

\`\`\`ruby <!-- paitsi että poista nuo kenoviivat, vain ```ruby ! -->
def foobar
    puts "Hello world!"
end
\`\`\` <!-- tästä myös, ei kenoviivoja, vain ``` -->

<!-- Yllä oleva teksti ei vaadi sisennystä. Lisäksi GitHub käyttää ``` jälkeen
mainitsemasi kielen syntaksin korostusta  -->

<!-- Vaakaviiva (<hr />) -->
<!-- Vaakaviivojen lisääminen käy näppärästi kolmella tai useammalla
asteriskilla taikka viivalla, välilyönneillä tai ilman -->

***
---
- - -
****************

<!-- Linkit -->
<!-- yksi markdownin parhaita ominaisuuksia on yksinkertaiset hyperlinkit. Laita
näytettävä teksti hakasulkuihin [] ja URL-osoite perään sulkeissa (). -->

[Klikkaa tästä!](http://example.com/)

<!-- Voit myös lisätä linkin otsikon heittomerkeissä osoitteen perään. -->

[Klikkaa tästä!](http://example.com/ "Linkki Example.com:iin")

<!-- Suhteelliset polut toimivat myös. -->

[Musiikkia](/musiikki/).

<!-- Markdown tukee myös viittaustyylisiä linkkejä. -->

[Klikkaa tätä linkkiä][link1] saadaksesi lisätietoja!
[Katso myös tämä linkki][foobar] jos haluat.

[link1]: http://example.com/ "Siistii!"
[foobar]: http://foobar.biz/ "Selkis!"

<!-- Otsikko voi olla myös ykittäisissä heittomerkeissä tai sulkeissa, tai
ohitettu kokonaan. Viittaukset voivat olla missä tahansa kohdassa dokumenttia ja
viittausten ID:t voivat olla mitä tahansa kunhan ne ovat uniikkeja. -->

<!-- Voit myös käyttää linkin tekstiä ID:nä näin: -->

[This][] is a link.

[this]: http://tämäonlinkki.com/

<!-- Mutta tämä tapa ei ole yleinen. -->

<!-- Kuvat -->
<!-- Kuvat tehdään samalla tavalla kuin linkitkin, mutta huutomerkki edessä! -->

![Kuvan alt-attribuutti](http://imgur.com/munkuva.jpg "Vaihtoehtoinen otsikko")

<!-- Ja viittaukset toimivat odotetusti. -->

![Tämä on se alt-attribuutti][munkuva]

[munkuva]: suhteellinen/polku/siitii/kuva.jpg "otsikko tähän tarvittaessa"

<!-- Sekalaista -->
<!-- Automaattiset linkit -->

<http://testwebsite.com/> on sama kuin
[http://testwebsite.com/](http://testwebsite.com/)

<!-- Automaattiset sähköpostilinkit -->

<foo@bar.com>

<!-- Varattujen merkkien käyttö -->

haluan kirjoittaa *tämän tekstin jonka ympärillä on asteriskit* mutta en halua
sen kursivoituvan, joten teen näin: \*tämän tekstin ympärillä on asteriskit\*.

<!-- Näppäimistön näppäimet -->
<!-- GitHubin Markdownissa, voit käyttää <kbd> -tagia esittämään näppäimiä -->

Tietokoneesi kaatui? Kokeile painaa
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>

<!-- Taulukot -->
<!-- Taulukot ovat saatavilla vain GitHubin markdownissa ja ne ovat melko
vaivalloisia käyttää, mutta jos todella haluat: -->

| Kolumni1     | Kolumni2 | Kolumni3      |
| :----------- | :------: | ------------: |
| Vasemmalle   | Keskelle | Oikealle      |
| blaa         | blaa     | blaa          |

<!-- vaihtoehtoisesti, sama tulos -->

Kolumni 1 | Kolumni 2 | Kolumni 3
:-- | :-: | --:
Hyi tämä on ruma | saa se | loppumaan

<!-- Loppu! -->

```

Lisää tietoa löydät John Gruberin [virallisesta julkaisusta](http://daringfireball.net/projects/markdown/syntax)
ja Adam Pritchardin loistavasta [lunttilapusta](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: ruby
filename: learnruby-fi.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
  - ["Gabriel Halley", "https://github.com/ghalley"]
  - ["Persa Zula", "http://persazula.com"]
  - ["Jake Faris", "https://github.com/farisj"]
translators:
  - ["Oliver Vartiainen", "https://github.com/firoxer"]
lang: fi-fi
---

```ruby
# Tässä yhden rivin kommentti

=begin
Tässä usean rivin kommentti
Näitä ei kylläkään käytetä
Joten käytetään vastedes vain yksirivisiä
=end

# Tärkeintä on muistaa, että Rubyssa kaikki pohjautuu olioihin.

# Luvutkin ovat olioita:

3.class #=> Fixnum

3.to_s #=> "3"

# Peruslaskutoimituksia:
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
5 % 3 #=> 2

# Bittioperaatioita:
3 & 5 #=> 1
3 | 5 #=> 7
3 ^ 5 #=> 6

# Laskutoimitukset ovat vain syntaksisokeria lukuolion laskumetodin kutsulle:
1.+(3) #=> 4
10.* 5 #=> 50

# Erityisarvotkin ovat olioita:

nil # vastaa joidenkin kielten "null"-arvoa
true # tosi
false # epätosi

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Samanvertaisuuden testaus:
1 == 1 #=> true
2 == 1 #=> false

# ...ja sama eriarvoisuudelle:
1 != 1 #=> false
2 != 1 #=> true

# "nil" ja "false" ovat ainoat epätodet arvot; kaikki muu ymmärretään todeksi:
!nil   #=> true
!false #=> true
!0     #=> false

# Lisää vertailuoperaatioita:
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Kahdensuuntainen vertailuoperaattori:
1 <=> 10 #=> -1
10 <=> 1 #=> 1
1 <=> 1 #=> 0

# Logiikkaoperaattorit:
true && false #=> false
true || false #=> true
!true #=> false

# Merkkipohjaisten logiikkaoperaattorien vaihtoehtona on sanalliset muodot,
# joilla on hyvin matala presedenssi. Niillä voi muokata ohjelman kulkua
# esimerkiksi väitelausekkeita ketjuttaen.

# Metodia `do_something_else` kutsutaan vain, jos `do_something` onnistuu:
do_something() and do_something_else()
# Metodia `log_error` kutsutaan vain, jos `do_something` epäonnistuu:
do_something() or log_error()

# Merkkijonot ovat olioita:

'Tässä on merkkijono'.class #=> String
"Rajaavat lainausmerkit voivat olla yksin- tai kaksinkertaisia".class #=> String

täyte = 'sisällyttää muita merkkijonoja'
"Kaksinkertaisilla lainausmerkeillä voi #{täyte}"
#=> "Kaksinkertaisilla lainausmerkeillä voi sisällyttää muita merkkijonoja"

# Yksinkertaisia lainausmerkkejä kannattaa silti suosia, sillä kaksinkertaiset
# merkit saattavat aiheuttaa turhia kielensisäisiä tarkistuksia.

# Merkkijonoja voi yhdistellä toisiinsa:
'hello ' + 'world'  #=> "hello world"

# ...mutta luvut vaativat ensin tyyppimuunnoksen:
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"

# Merkkijonoja voi soveltaa laskutoimituksiin... odotettavin seurauksin:
'hello ' * 3 #=> "hello hello hello "

# Merkkijonoa voi jatkaa toisella:
'hello' << ' world' #=> "hello world"

# Tulosteen luonti kera rivinvaihdon:
puts "I'm printing!"
#=> I'm printing!
#=> nil

# ...ja ilman rivinvaihtoa:
print "I'm printing!"
#=> I'm printing! => nil

# Muuttujien määrittely:
x = 25 #=> 25
x #=> 25

# Arvon asettaminen palauttaa arvon itsensä, joten usean muuttujan arvon
# yhtäaikainen määrittely käy vaivatta:
x = y = 10 #=> 10
x #=> 10
y #=> 10

# Muuttujien sanaerottimena käytetään alaviivaa:
snake_case = true

# Lisäksi Rubyssa suositaan ytimekkäitä nimiä:
path_to_project_root = '/good/name/'
path = '/bad/name/'

# Symbolit

# Symbolit ovat muuttumattomia, uudelleenkäytettäviä vakioita.
# Niitä käytetään merkkijonojen sijaan, kun tarkoitus on viitata arvoon,
# jolla on tietty, pysyvä merkitys:

:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# Taulukot

# Tässä taulukko:
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Taulukko saa koostua erityyppisistä arvoista:
[1, 'hello', false] #=> [1, "hello", false]

# Taulukon alkioihin voi viitata järjestysnumerolla nollasta alkaen:
array[0] #=> 1
array.first #=> 1
array[12] #=> nil

# Kuten laskutoimituksissa nähty syntaksisokeri on myös taulukon alkioiden haku
# pohjimmiltaan vain taulukko-olioon kuuluvan "[]"-metodin kutsu:
array.[] 0 #=> 1
array.[] 12 #=> nil

# Haku käy myös lopustapäin:
array[-1] #=> 5
array.last #=> 5

# Alitaulukon haku käy indeksiparilla...
array[2, 3] #=> [3, 4, 5]

# ...tai määrittelemällä väli:
array[1..3] #=> [2, 3, 4]

# Taulukon voi kääntää:
a=[1,2,3]
a.reverse! #=> [3,2,1]

# Ja sitä voi jatkaa näin...
array << 6 #=> [1, 2, 3, 4, 5, 6]

# ...tai näin:
array.push(6) #=> [1, 2, 3, 4, 5, 6]

# Alkion olemassaolon tarkistus:
array.include?(1) #=> true

# Hashit eli assosiaatiotaulut ovat Rubyn tärkein avain-/arvoparirakenne.
# Hash luodaan aaltosulkeilla:
hash = { 'color' => 'green', 'number' => 5 }

hash.keys #=> ['color', 'number']

# Hash toimii erityisen nopeasti, kun haetaan arvoa avaimen perusteella:
hash['color'] #=> 'green'
hash['number'] #=> 5

# Jos hashistä ei löyty avainta vastaavaa arvoa, palautetaan nil-arvo:
hash['nothing here'] #=> nil

# Symbolihashin määrittelylle on oma syntaksinsa (alkaen Rubyn versiosta 1.9):
new_hash = { defcon: 3, action: true }
new_hash.keys #=> [:defcon, :action]

# Hashin avaimen ja arvon olemassaolon tarkistus:
new_hash.key?(:defcon) #=> true
new_hash.value?(3) #=> true

# Vinkki! Sekä taulukot että hashit sisältävät Enumerable-moduulin,
# johon kuuluu useita hyödyllisiä iterointimetodeja kuten .each, .map,
# .reduce ja .count

# Rakenteita

if true
  'if statement'
elsif false
  'else if, optional'
else
  'else, also optional'
end

for counter in 1..5
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# HUOMAA, että for-rakennetta kannattaa välttää, sillä Rubyssa suosittu
# each-metodi ajaa saman asian idiomaattisemmin. Each-metodi ottaa ainoana
# argumenttinaan lohkon. Lohkot toimivat pitkälti samoin kuin muiden kielten
# anonyymit funktiot, lambdat tai sulkeumat.

# Lukuvälit vastaavat each-metodiin, jolloin sille annettu lohko ajetaan
# kerran jokaiselle välin kokonaisluvulle.
# Lukuvälin each-rakenne lohkoineen näyttää tältä:

(1..5).each do |counter|
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# Lohkoa ympäröivät do/end-avainsanat voi korvata myös aaltosulkeilla:
(1..5).each { |counter| puts "iteration #{counter}" }

# Lukuvälien lisäksi myös tietorakenteita voidaan iteroida each-metodilla:
array.each do |element|
  puts "#{element} is part of the array"
end
hash.each do |key, value|
  puts "#{key} is #{value}"
end

# Taulukoita voi iteroida metodilla each_with_index, jolloin lohko saa
# argumenteikseen sekä alkion että indeksin:
array.each_with_index do |element, index|
  puts "#{element} is number #{index} in the array"
end

counter = 1
while counter <= 5 do
  puts "iteration #{counter}"
  counter += 1
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# Each-metodin lisäksi Rubyssa on useita muita iterointimetodeja kuten
# "map" ja "reduce". Näistä "map" kutsuttuna taulukolla ottaa argumentikseen
# lohkon, suorittaa sen kerran jokaiselle rakenteen jäsenelle, ja lopuksi
# palauttaa uuden taulukon, jonka jäsenet ovat lohkon suorituksen tuloksia.

array = [1, 2, 3, 4, 5]
doubled = array.map do |element|
  element * 2
end
puts doubled
#=> [2,4,6,8,10]
puts array
#=> [1,2,3,4,5]

# Case-rakenne siirtää ohjelman kulun yhdelle monista määritellyistä poluista:

grade = 'B'

case grade
when 'A'
  puts 'Way to go kiddo'
when 'B'
  puts 'Better luck next time'
when 'C'
  puts 'You can do better'
when 'D'
  puts 'Scraping through'
when 'F'
  puts 'You failed!'
else
  puts 'Alternative grading system, eh?'
end
#=> "Better luck next time"

# Case-rakenteessa voidaan hyödyntää lukuvälejä:
grade = 82
case grade
when 90..100
  puts 'Hooray!'
when 80...90
  puts 'OK job'
else
  puts 'You failed!'
end
#=> "OK job"

# Virheidenkäsittely:
begin
  # Seuraava koodinpätkä aiheuttaa NoMemoryError-poikkeuksen
  raise NoMemoryError, 'You ran out of memory.'
rescue NoMemoryError => exception_variable
  puts 'NoMemoryError was raised', exception_variable
rescue RuntimeError => other_exception_variable
  puts 'RuntimeError was raised now'
else
  puts 'This runs if no exceptions were thrown at all'
ensure
  puts 'This code always runs no matter what'
end

# Ylimmän näkyvyysalueen metodi näyttää itsenäiseltä funktiolta:
def double(x)
  x * 2
end

# Funktiot (ja lohkot) palauttavat implisiittisesti
# viimeiseksi ajamansa lausekkeen arvon:
double(2) #=> 4

# Metodikutsun argumentteja ympäröivät kaarisulkeet voi jättää pois,
# kunhan koodi ei muutu monitulkintaiseksi:

double 3 #=> 6

double double 3 #=> 12

def sum(x, y)
  x + y
end

# Argumentit erotetaan pilkuilla:

sum 3, 4 #=> 7

sum sum(3, 4), 5 #=> 12

# Kaikilla metodeilla on implisiittinen lohkoparametri,
# joka voidaan suorittaa yield-avainsanalla:

def surround
  puts '{'
  yield
  puts '}'
end

surround { puts 'hello world' }

# {
# hello world
# }

# Metodille annetun lohkon voi nimetä parametrilistassa &-merkin avulla,
# minkä jälkeen se suoritetaan call-metodilla:
def guests(&block)
  block.call 'some_argument'
end

# Metodille voi antaa vaihtelevan määrän muuttujia. Ne siirretään taulukkoon,
# jolle annetaan parametrilistassa nimi \*-merkin avulla
def guests(*array)
  array.each { |guest| puts guest }
end

# Luokan määritys aloitetaan class-avainsanalla:

class Human

  # Tässä luokkamuuttuja, joka on yhteinen kaikille luokan olioille:
  @@species = 'H. sapiens'

  # Alustusmetodin määrittely:
  def initialize(name, age = 0)
	# name-oliomuuttujan arvon asetus metodille annetun name-muuttujan mukaan:
    @name = name

	# Jos tätä metodia kutsuessa jätetään toinen argumentti (age) antamatta,
	# saa se parametriluettelossa määritetyn arvon 0:
    @age = age
  end

  # Tyypillinen oliomuuttujan arvon asettava metodi:
  def name=(name)
    @name = name
  end

  # Tyypillinen oliomuuttujan arvon palauttava metodi:
  def name
    @name
  end

  # Edelliset kaksi metodia voi ilmaista idiomaattisemmin myös näin:
  attr_accessor :name

  # Lisäksi arvon palauttavan ja asettavan metodin voi määritellä erikseen:
  attr_reader :name
  attr_writer :name

  # Luokkametodeissa käytetään avainsanaa self erotuksena oliometodeista.
  # Luokkametodia voi kutsua vain luokalla itsellään, ei olioilla:
  def self.say(msg)
    puts msg
  end

  def species
    @@species
  end
end

# Olion luonti:

jim = Human.new('Jim Halpert')

dwight = Human.new('Dwight K. Schrute')

# Olion metodien kutsuja:
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# Luokkametodin kutsu:
Human.say('Hi') #=> "Hi"

# Muuttujan näkyvyysalueen voi määritellä etuliitteellä.

# $-alkuiset muuttujat ovat globaaleja:
$var = "I'm a global var"
defined? $var #=> "global-variable"

# @-alkuiset muuttujat kuuluvat oliolle,
# jonka näkyvyysalueella määrittely tehdään:
@var = "I'm an instance var"
defined? @var #=> "instance-variable"

# @@-alkuiset muuttujat kuuluvat vastaavasti näkyvyysalueensa luokalle:
@@var = "I'm a class var"
defined? @@var #=> "class variable"

# Isolla alkukirjaimella nimetyt muuttujat ovatkin vakioita:
Var = "I'm a constant"
defined? Var #=> "constant"

# Kuten odottaa saattaa, myös luokat itsessään ovat olioita.
# Siksi niille voi määritellä muuttujia, jotka ovat yhteisiä kaikille
# luokan ilmentymille ja perillisille.

# Tavallisen luokan määrittely:

class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

# Perillisluokan määrittely:

class Worker < Human
end

Human.foo # 0
Worker.foo # 0

Human.foo = 2 # 2
Worker.foo # 2

# Oliomuuttuja on kuitenkin olion oma eikä periydy:

class Human
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doctor < Human
end

Human.bar # 0
Doctor.bar # nil

module ModuleExample
  def foo
    'foo'
  end
end

# Moduulien lisääminen luokkaan "include"-avainsanalla siirtää moduulin metodit
# luokan ilmentymille, kun taas "extend" avainsana siirtää metodit
# luokalle itselleen:

class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => NoMethodError: undefined method `foo'

# Callback-tyyppiset metodit suoritetaan moduulia sisällyttäessä:

module ConcernExample
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class Something
  include ConcernExample
end

Something.bar     # => 'bar'
Something.qux     # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
```

## Lisämateriaalia englanniksi

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - Selaimessa tehtäviä harjoituksia tämän dokumentin hengessä
- [An Interactive Tutorial for Ruby](https://rubymonk.com/)
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/) - Virallinen dokumentaatio
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - Vanhempi, mutta [ilmainen painos](http://ruby-doc.com/docs/ProgrammingRuby/) on luettavissa netissä
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - Yhteisön luoma Ruby-tyyliopas
- [Try Ruby](http://tryruby.org) - Rubyn perusteet interaktiivisesti
---
language: forth
contributors:
    - ["Horse M.D.", "http://github.com/HorseMD/"]
filename: learnforth.fs
---

Forth was created by Charles H. Moore in the 70s. It is an imperative,
stack-based language and programming environment, being used in projects
such as Open Firmware. It's also used by NASA.

Note: This article focuses predominantly on the Gforth implementation of
Forth, but most of what is written here should work elsewhere.

```forth
\ This is a comment
( This is also a comment but it's only used when defining words )

\ --------------------------------- Precursor ----------------------------------

\ All programming in Forth is done by manipulating the parameter stack (more
\ commonly just referred to as "the stack").
5 2 3 56 76 23 65    \ ok

\ Those numbers get added to the stack, from left to right.
.s    \ <7> 5 2 3 56 76 23 65 ok

\ In Forth, everything is either a word or a number.

\ ------------------------------ Basic Arithmetic ------------------------------

\ Arithmetic (in fact most words requiring data) works by manipulating data on
\ the stack.
5 4 +    \ ok

\ `.` pops the top result from the stack:
.    \ 9 ok

\ More examples of arithmetic:
6 7 * .        \ 42 ok
1360 23 - .    \ 1337 ok
12 12 / .      \ 1 ok
13 2 mod .     \ 1 ok

99 negate .    \ -99 ok
-99 abs .      \ 99 ok
52 23 max .    \ 52 ok
52 23 min .    \ 23 ok

\ ----------------------------- Stack Manipulation -----------------------------

\ Naturally, as we work with the stack, we'll want some useful methods:

3 dup -          \ duplicate the top item (1st now equals 2nd): 3 - 3
2 5 swap /       \ swap the top with the second element:        5 / 2
6 4 5 rot .s     \ rotate the top 3 elements:                   4 5 6
4 0 drop 2 /     \ remove the top item (don't print to screen):  4 / 2
1 2 3 nip .s     \ remove the second item (similar to drop):    1 3

\ ---------------------- More Advanced Stack Manipulation ----------------------

1 2 3 4 tuck   \ duplicate the top item into the second slot:      1 2 4 3 4 ok
1 2 3 4 over   \ duplicate the second item to the top:             1 2 3 4 3 ok
1 2 3 4 2 roll \ *move* the item at that position to the top:      1 3 4 2 ok
1 2 3 4 2 pick \ *duplicate* the item at that position to the top: 1 2 3 4 2 ok

\ When referring to stack indexes, they are zero-based.

\ ------------------------------ Creating Words --------------------------------

\ The `:` word sets Forth into compile mode until it sees the `;` word.
: square ( n -- n ) dup * ;    \ ok
5 square .                     \ 25 ok

\ We can view what a word does too:
see square     \ : square dup * ; ok

\ -------------------------------- Conditionals --------------------------------

\ -1 == true, 0 == false. However, any non-zero value is usually treated as
\ being true:
42 42 =    \ -1 ok
12 53 =    \ 0 ok

\ `if` is a compile-only word. `if` <stuff to do> `then` <rest of program>.
: ?>64 ( n -- n ) dup 64 > if ." Greater than 64!" then ; \ ok
100 ?>64                                                  \ Greater than 64! ok

\ Else:
: ?>64 ( n -- n ) dup 64 > if ." Greater than 64!" else ." Less than 64!" then ;
100 ?>64    \ Greater than 64! ok
20 ?>64     \ Less than 64! ok

\ ------------------------------------ Loops -----------------------------------

\ `do` is also a compile-only word.
: myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok
myloop
\ Hello!
\ Hello!
\ Hello!
\ Hello!
\ Hello! ok

\ `do` expects two numbers on the stack: the end number and the start number.

\ We can get the value of the index as we loop with `i`:
: one-to-12 ( -- ) 12 0 do i . loop ;     \ ok
one-to-12                                 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 ok

\ `?do` works similarly, except it will skip the loop if the end and start
\ numbers are equal.
: squares ( n -- ) 0 ?do i square . loop ;   \ ok
10 squares                                   \ 0 1 4 9 16 25 36 49 64 81 ok

\ Change the "step" with `+loop`:
: threes ( n n -- ) ?do i . 3 +loop ;    \ ok
15 0 threes                             \ 0 3 6 9 12 ok

\ Indefinite loops with `begin` <stuff to do> <flag> `until`:
: death ( -- ) begin ." Are we there yet?" 0 until ;    \ ok

\ ---------------------------- Variables and Memory ----------------------------

\ Use `variable` to declare `age` to be a variable.
variable age    \ ok

\ Then we write 21 to age with the word `!`.
21 age !    \ ok

\ Finally we can print our variable using the "read" word `@`, which adds the
\ value to the stack, or use `?` that reads and prints it in one go.
age @ .    \ 21 ok
age ?      \ 21 ok

\ Constants are quite similar, except we don't bother with memory addresses:
100 constant WATER-BOILING-POINT    \ ok
WATER-BOILING-POINT .               \ 100 ok

\ ----------------------------------- Arrays -----------------------------------

\ Creating arrays is similar to variables, except we need to allocate more
\ memory to them.

\ You can use `2 cells allot` to create an array that's 3 cells long:
variable mynumbers 2 cells allot    \ ok

\ Initialize all the values to 0
mynumbers 3 cells erase    \ ok

\ Alternatively we could use `fill`:
mynumbers 3 cells 0 fill

\ or we can just skip all the above and initialize with specific values:
create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!)

\ ...which is equivalent to:

\ Manually writing values to each index:
64 mynumbers 0 cells + !      \ ok
9001 mynumbers 1 cells + !    \ ok
1337 mynumbers 2 cells + !    \ ok

\ Reading values at certain array indexes:
0 cells mynumbers + ?    \ 64 ok
1 cells mynumbers + ?    \ 9001 ok

\ We can simplify it a little by making a helper word for manipulating arrays:
: of-arr ( n n -- n ) cells + ;    \ ok
mynumbers 2 of-arr ?               \ 1337 ok

\ Which we can use for writing too:
20 mynumbers 1 of-arr !    \ ok
mynumbers 1 of-arr ?       \ 20 ok

\ ------------------------------ The Return Stack ------------------------------

\ The return stack is used to the hold pointers to things when words are
\ executing other words, e.g. loops.

\ We've already seen one use of it: `i`, which duplicates the top of the return
\ stack. `i` is equivalent to `r@`.
: myloop ( -- ) 5 0 do r@ . loop ;    \ ok

\ As well as reading, we can add to the return stack and remove from it:
5 6 4 >r swap r> .s    \ 6 5 4 ok

\ NOTE: Because Forth uses the return stack for word pointers,  `>r` should
\ always be followed by `r>`.

\ ------------------------- Floating Point Operations --------------------------

\ Most Forths tend to eschew the use of floating point operations.
8.3e 0.8e f+ f.    \ 9.1 ok

\ Usually we simply prepend words with 'f' when dealing with floats:
variable myfloatingvar    \ ok
4.4e myfloatingvar f!     \ ok
myfloatingvar f@ f.       \ 4.4 ok

\ --------------------------------- Final Notes --------------------------------

\ Typing a non-existent word will empty the stack. However, there's also a word
\ specifically for that:
clearstack

\ Clear the screen:
page

\ Loading Forth files:
\ s" forthfile.fs" included

\ You can list every word that's in Forth's dictionary (but it's a huge list!):
\ words

\ Exiting Gforth:
\ bye

```

##Ready For More?

* [Starting Forth](http://www.forth.com/starting-forth/)
* [Simple Forth](http://www.murphywong.net/hello/simple.htm)
* [Thinking Forth](http://thinking-forth.sourceforge.net/)
---
language: Fortran
contributors:
    - ["Robert Steed", "https://github.com/robochat"]
filename: learnfortran.f95
---

Fortran is one of the oldest computer languages. It was developed in the 1950s 
by IBM for numeric calculations (Fortran is an abbreviation of "Formula 
Translation"). Despite its age, it is still used for high-performance computing
such as weather prediction. However, the language has changed considerably over
the years, although mostly maintaining backwards compatibility; well known
versions are FORTRAN 77, Fortran 90, Fortran 95, Fortran 2003, Fortran 2008 and
Fortran 2015.

This overview will discuss the features of Fortran 95 since it is the most
widely implemented of the more recent specifications and the later versions are
largely similar (by comparison FORTRAN 77 is a very different language).

```fortran

! This is a comment.


program example   !declare a program called example.

    ! Code can only exist inside programs, functions, subroutines or modules.
    ! Using indentation is not required but it is recommended.


    ! Declaring Variables
    ! ===================
    
    ! All declarations must come before statements and expressions.
    
    implicit none    !prevents dynamic declaration of variables (recommended!)
    ! Implicit none must be redeclared in every function/program/module...
    
    ! IMPORTANT - Fortran is case insensitive.
    real z
    REAL Z2

    real :: v,x    ! WARNING: default initial values are compiler dependent!    
    real :: a = 3, b=2E12, c = 0.01
    integer :: i, j, k=1, m
    real, parameter :: PI = 3.1415926535897931    !declare a constant.
    logical :: y = .TRUE. , n = .FALSE.    !boolean type.
    complex :: w = (0,1)    !sqrt(-1)
    character (len=3) :: month    !string of 3 characters.
    
    real :: array(6)     !declare an array of 6 reals.
    real, dimension(4) :: arrayb    !another way to declare an array.
    integer :: arrayc(-10:10)   !an array with a custom index.
    real :: array2d(3,2)    !multidimensional array.
    
    ! The '::' separators are not always necessary but are recommended.

    ! many other variable attributes also exist:
    real, pointer :: p    !declare a pointer.

    integer, parameter :: LP = selected_real_kind(20)
    real (kind = LP) :: d    !long precision variable.

    ! WARNING: initialising variables during declaration causes problems
    ! in functions since this automatically implies the 'save' attribute
    ! whereby values are saved between function calls. In general, separate
    ! declaration and initialisation code except for constants!
    
    
    ! Strings
    ! =======

    character :: a_char = 'i'
    character (len = 6) :: a_str = "qwerty"
    character (len = 30) :: str_b
    character (len = *), parameter :: a_long_str = "This is a long string."
    !can have automatic counting of length using (len=*) but only for constants.
    
    str_b = a_str // " keyboard"    !concatenate strings using // operator.


    ! Assignment & Arithmetic
    ! =======================

    Z = 1    !assign to variable z declared above (case insensitive).
    j = 10 + 2 - 3
    a = 11.54  /  (2.3 * 3.1)
    b = 2**3    !exponentiation


    ! Control Flow Statements & Operators
    ! ===================================

    ! Single-line if statement
    if (z == a) b = 4  !condition always need surrounding parentheses.

    if (z /= a) then !z not equal to a
    ! Other symbolic comparisons are < > <= >= == /=
      b = 4
    else if (z .GT. a) then !z greater than a
    ! Text equivalents to symbol operators are .LT. .GT. .LE. .GE. .EQ. .NE.  
      b = 6
    else if (z < a) then !'then' must be on this line.
      b = 5 !execution block must be on a new line.
    else
      b = 10
    end if !end statement needs the 'if' (or can use 'endif').


    if (.NOT. (x < c .AND. v >= a .OR. z == z)) then   !boolean operators.
      inner: if (.TRUE.) then    !can name if-construct.
        b = 1
      endif inner    !then must name endif statement.
    endif


    i = 20
    select case (i)
      case (0)    !case i == 0
        j=0
      case (1:10)    !cases i is 1 to 10 inclusive.
        j=1
      case (11:)    !all cases where i>=11
        j=2
      case default
        j=3
    end select


    month = 'jan'
    ! Condition can be integer, logical or character type.
    ! Select constructions can also be named.
    monthly: select case (month)
      case ("jan")
         j = 0
      case default
         j = -1
    end select monthly


    do i=2,10,2    !loops from 2 to 10 (inclusive) in increments of 2.
      innerloop: do j=1,3    !loops can be named too.
        exit    !quits the loop.
      end do innerloop
    cycle    !jump to next loop iteration.
    enddo

    
    ! Goto statement exists but it is heavily discouraged though.
    goto 10    
    stop 1    !stops code immediately (returning specified condition code).
10  j = 201    !this line is labeled as line 10
    
    
    ! Arrays
    ! ======
    array = (/1,2,3,4,5,6/)
    array = [1,2,3,4,5,6]    !using Fortran 2003 notation.
    arrayb = [10.2,3e3,0.41,4e-5]
    array2d =  reshape([1.0,2.0,3.0,4.0,5.0,6.0], [3,2])
    
    ! Fortran array indexing starts from 1.
    ! (by default but can be defined differently for specific arrays).
    v = array(1)    !take first element of array.
    v = array2d(2,2)
    
    print *, array(3:5)    !print all elements from 3rd to 5th (inclusive).
    print *, array2d(1,:)    !print first column of 2d array.
    
    array = array*3 + 2    !can apply mathematical expressions to arrays.
    array = array*array    !array operations occur element-wise.
    !array = array*array2d    !these arrays would not be compatible.
    
    ! There are many built-in functions that operate on arrays.
    c = dot_product(array,array)    !this is the dot product.
    ! Use matmul() for matrix maths.
    c = sum(array)
    c = maxval(array)
    print *, minloc(array)
    c = size(array)
    print *, shape(array)
    m = count(array > 0)
    
    ! Loop over an array (could have used Product() function normally).
    v = 1
    do i = 1, size(array)
        v = v*array(i)
    end do
    
    ! Conditionally execute element-wise assignments.
    array = [1,2,3,4,5,6]
    where (array > 3)
        array = array + 1
    elsewhere (array == 2)
        array = 1
    elsewhere
        array = 0
    end where
    
    ! Implied-DO loops are a compact way to create arrays.
    array = [ (i, i = 1,6) ]    !creates an array of [1,2,3,4,5,6]
    array = [ (i, i = 1,12,2) ]    !creates an array of [1,3,5,7,9,11]
    array = [ (i**2, i = 1,6) ]    !creates an array of  [1,4,9,16,25,36]
    array = [ (4,5, i = 1,3) ]    !creates an array of [4,5,4,5,4,5]
    

    ! Input/Output
    ! ============
    
    print *, b    !print the variable 'b' to the command line

    ! We can format our printed output.
    print "(I6)", 320    !prints '   320'
    print "(I6.4)", 3    !prints '  0003' 
    print "(F6.3)", 4.32    !prints ' 4.320'
    
    ! The letter indicates the expected type and the number afterwards gives 
    ! the number of characters to use for printing the value.
    ! Letters can be I (integer), F (real), E (engineering format), 
    ! L (logical), A (characters) ...
    print "(I3)", 3200    !print '***' since the number doesn't fit.
    
    ! we can have multiple format specifications.
    print "(I5,F6.2,E6.2)", 120, 43.41, 43.41
    print "(3I5)", 10, 20, 30    !3 repeats of integers (field width = 5).
    print "(2(I5,F6.2))", 120, 43.42, 340, 65.3   !repeated grouping of formats.

    ! We can also read input from the terminal.
    read *, v
    read "(2F6.2)", v, x    !read two numbers

    ! To read a file.
    open(unit=11, file="records.txt", status="old") 
    ! The file is referred to by a 'unit number', an integer that you pick in
    ! the range 9:99. Status can be one of {'old','replace','new'}.
    read(unit=11, fmt="(3F10.2)") a, b, c
    close(11)

    ! To write a file.
    open(unit=12, file="records.txt", status="replace")
    write(12, "(F10.2,F10.2,F10.2)") c, b, a
    close(12)

    ! There are more features available than discussed here and alternative 
    ! variants due to backwards compatibility with older Fortran versions.
    
    
    ! Built-in Functions
    ! ==================

    ! Fortran has around 200 functions/subroutines intrinsic to the language.
    ! Examples - 
    call cpu_time(v)    !sets 'v' to a time in seconds.
    k = ior(i,j)    !bitwise OR of 2 integers.
    v = log10(x)    !log base 10.
    i = floor(b)    !returns the closest integer less than or equal to x.
    v = aimag(w)    !imaginary part of a complex number.
    

    ! Functions & Subroutines
    ! =======================
    
    ! A subroutine runs some code on some input values and can cause
    ! side-effects or modify the input values.
    
    call routine(a,c,v)    !subroutine call.
    
    ! A function takes a list of input parameters and returns a single value.
    ! However the input parameters may still be modified and side effects 
    ! executed.
    
    m = func(3,2,k)  !function call.
    
    ! Function calls can also be evoked within expressions.
    Print *, func2(3,2,k) 
    
    ! A pure function is a function that doesn't modify its input parameters
    ! or cause any side-effects.
    m = func3(3,2,k)


contains ! Zone for defining sub-programs internal to the program.

    ! Fortran has a couple of slightly different ways to define functions.

    integer function func(a,b,c)    !a function returning an integer value.
        implicit none   !best to use implicit none in function definitions too.
        integer :: a,b,c !type of input parameters defined inside the function.
        if (a >= 2) then
            func = a + b + c !the return variable defaults to the function name.
            return !can return the current value from the function at any time.
        endif
        func = a + c
        ! Don't need a return statement at the end of a function.
    end function func


    function func2(a,b,c) result(f)    !return variable declared to be 'f'.
        implicit none
        integer, intent(in) :: a,b    !can declare and enforce that variables 
                                      !are not modified by the function.
        integer, intent(inout) :: c
        integer :: f     !function return type declared inside the function.
        integer :: cnt = 0    !GOTCHA - initialisation implies variable is
                              !saved between function calls.
        f = a + b - c
        c = 4    !altering the value of an input variable.
        cnt  = cnt + 1    !count number of function calls.
    end function func2


    pure function func3(a,b,c)  !a pure function can have no side-effects.
        implicit none
        integer, intent(in) :: a,b,c
        integer :: func3
        func3 = a*b*c
    end function func3


    subroutine routine(d,e,f)
        implicit none
        real, intent(inout) :: f
        real, intent(in) :: d,e
        f = 2*d + 3*e + f
    end subroutine routine


end program example   ! End of Program Definition -----------------------


! Functions and Subroutines declared externally to the program listing need 
! to be declared to the program using an Interface declaration (even if they
! are in the same source file!) (see below). It is easier to define them within
! the 'contains' section of a module or program.

elemental real function func4(a) result(res)
! An elemental function is a Pure function that takes a scalar input variable
! but can also be used on an array where it will be separately applied to all 
! of the elements of an array and return a new array.
    real, intent(in) :: a
    res = a**2 + 1.0
end function func4


! Modules
! =======

! A module is a useful way to collect related declarations, functions and 
! subroutines together for reusability.

module fruit
    real :: apple
    real :: pear
    real :: orange
end module fruit


module fruity
    ! Declarations must be in the order: modules, interfaces, variables.
    ! (can declare modules and interfaces in programs too).
    
    use fruit, only: apple, pear    ! use apple and pear from fruit module.
    implicit none    !comes after module imports.

    private    !make things private to the module (default is public).
    ! Declare some variables/functions explicitly public.
    public :: apple,mycar,create_mycar
    ! Declare some variables/functions private to the module (redundant here).
    private :: func4
    
    ! Interfaces
    ! ==========
    ! Explicitly declare an external function/procedure within the module
    ! (better in general to put functions/procedures in the 'contains' section).
    interface
        elemental real function func4(a) result(res)
            real, intent(in) :: a
        end function func4
    end interface
    
    ! Overloaded functions can be defined using named interfaces.
    interface myabs
        ! Can use 'module procedure' keyword to include functions already
        ! defined within the module.
        module procedure real_abs, complex_abs
    end interface 
        
    ! Derived Data Types
    ! ==================
    ! Can create custom structured data collections.
    type car
        character (len=100) :: model
        real :: weight    !(kg)
        real :: dimensions(3)    !i.e. length-width-height (metres).
        character :: colour
    end type car
    
    type(car) :: mycar    !declare a variable of your custom type.
    ! See create_mycar() routine for usage.
    
    ! Note: There are no executable statements in modules.
    
contains

    subroutine create_mycar(mycar)
        ! Demonstrates usage of a derived data type.
        implicit none
        type(car),intent(out) :: mycar
        
        ! Access type elements using '%' operator.
        mycar%model = "Ford Prefect"
        mycar%colour = 'r'
        mycar%weight = 1400
        mycar%dimensions(1) = 5.0    !default indexing starts from 1!
        mycar%dimensions(2) = 3.0
        mycar%dimensions(3) = 1.5
        
    end subroutine

    real function real_abs(x)
        real :: x
        if (x<0) then
            real_abs = -x
        else
            real_abs = x
        end if
    end function real_abs
    
    real function complex_abs(z)
        complex :: z
        ! long lines can be continued using the continuation character '&'
        complex_abs = sqrt(real(z)**2 + &
                                         aimag(z)**2)
    end function complex_abs


end module fruity

```

### More Resources

For more information on Fortran:

+ [wikipedia](https://en.wikipedia.org/wiki/Fortran)
+ [Fortran_95_language_features](https://en.wikipedia.org/wiki/Fortran_95_language_features)
+ [fortranwiki.org](http://fortranwiki.org)
+ [www.fortran90.org/](http://www.fortran90.org)
+ [list of Fortran 95 tutorials](http://www.dmoz.org/Computers/Programming/Languages/Fortran/FAQs%2C_Help%2C_and_Tutorials/Fortran_90_and_95/)
+ [Fortran wikibook](https://en.wikibooks.org/wiki/Fortran)
+ [Fortran resources](http://www.fortranplus.co.uk/resources/fortran_resources.pdf)
+ [Mistakes in Fortran 90 Programs That Might Surprise You](http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html)
---
category: Algorithms & Data Structures
name: Asymptotic Notation
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Divay Prakash", "http://github.com/divayprakash"]
translators:
    - ["Agathe Begault", "https://github.com/begault"]
lang: fr-fr    
---

# Notations Asymptotiques

## Qu'est ce que c'est?

Les notations asymptotiques sont des langages qui nous permettent d'analyser l'ordre de grandeur du temps d'exécution d'un algorithme en identifiant son comportement à mesure que les données d'entrée de l'algorithme augmentent. On appelle également cela le taux de croissance d'un algorithme. 

Est ce que l'algorithme devient soudainement extrêmement lent si on augmente sa taille d'entrée ? Est ce qu'il arrive à maintenir un temps d'exécution rapide ? La notation asymptotique nous permet simplement de répondre à ces questions et d'ainsi comparer différents algorithmes. 

## Y a t-il des alternatives pour répondre à ces problématiques ? 

Une première solution serait de compter le nombre d'opérations primitives, en fonction de la taille d'entrée. 
Cette méthode est viable mais nécessite une quantité de travail trop importante par rapport à son utilisation, même sur des algorithmes simples. 

Une autre approche serait de mesurer physiquement le temps qu'un algorithme met pour traiter différentes quantités de données. Cependant, la précision et la relativité de cette méthode (les temps obtenus n'étant relatifs qu'à la machine sur laquelle ils ont été calculés) est liée à des variables environnementales comme les spécifications du matériel informatique utilisé, la puissance de traitement de la machine, etc. 

## Les types de Notations asymptotiques

En première partie de ce document, nous avons décris comment une notation asymptotique identifie le comportement d'un algorithme à mesure que la taille des données d'entrée change. Imaginons un algorithme comme une fonction f, ayant comme taille de donnée d'entrée n, et f(n) étant le temps d'exécution. Ainsi, pour un algorithme donné f, avec une taille d'entrée  n, nous obtenons en résultat un temps d'exécution f(n). Cela nous fournit un graphique où l'axe Y est le temps d'exécution, l'axe X est la taille d'entrée et la courbe tracée est le résultat du temps utilisé pour traiter chaque quantité de données.  

Vous pouvez décrire une fonction ou un algorithme avec une notation asymptotique de plusieurs manières. Par exemple, vous pouvez décrire un algorithme en partant du meilleur des cas, du pire ou d'un cas intermédiaire. Le plus courant est de commencer à analyser un algorithme avec le pire cas. Le meilleur cas n'est pas nécessaire car ce ne sont pas les conditions que vous envisagez. L'algorithme de tri est un très bon exemple, particulièrement en ajoutant des éléments à une structure arborescente. Le meilleur cas pour la plupart des algorithmes ne nécessite qu'une simple opération alors que dans la plupart des cas, l'élément à ajouter aura besoin d'être  trié de manière appropriée à travers l'arbre de données. Cette action pourrait signifier l'examen d'une branche entière de l'arbre. C'est d'ailleurs le pire cas et celui que nous prévoyons. 

### Les types de fonctions, limites et simplifications

```
Fonction logarithmique - log n
Fonction linéaire - an + b
Fonction quadratique - an^2 + bn + c
Fonction polynomiale - an^z + . . . + an^2 + a*n^1 + a*n^0, où z est une constante
Fonction exponentielle - a^n, où a est une constante 
```

Voici une classification de fonctions croissantes de base, utilisées dans de nombreuses notations. La liste commence par la plus lente des fonctions croissantes (logarithmique, le temps d'exécution le plus rapide) et finit avec la plus croissante des fonctions (exponentielle, le temps d'exécution le plus lent). Notez que lorsque 'n' ou les données d'entrée augmentent pour chacune de ces fonctions, le résultat augmente clairement plus rapidement avec les fonctions quadratique, polynomiale et exponentielle qu'avec les fonctions logarithmique et linéaire. 

Il est important de noter que les notations suivantes doivent être utilisées avec les termes les plus simples. Cela signifie d'ignorer les constantes et termes de l'ordre inférieur. En effet, puisque la taille d'entrée (ou n dans notre exemple f(n)) peut augmenter à l'infini (limites mathématiques), les termes et constantes de l'ordre inférieur sont insignifiants. Ceci dit, si vous avez une constante égale à 2^9001, ou toute autre valeur ridicule et inimaginable, dans ce cas la simplification nuira à votre précision de notation. 

Puisque nous voulons la forme la plus simple, modifions un peu notre table... 

```
Logarithmique - log n
Linéaire - n
Quadratique - n^2
Polynomiale - n^z, où z est une constante
Exponentielle - a^n, où a est une constante 
```

### Big-O

Big-O, couramment écris **O**, est une notation asymptotique pour le cas le plus mauvais (ou plafond de croissance) d'une fonction donnée. Il nous fournit une _**limite supérieure asymptotique**_ pour le taux de croissance du temps d'exécution d'un algorithme. 

Prenons 'f(n)' comme temps d'exécution de notre algorithme et 'g(n)' comme complexité de temps arbitraire que nous essayons d'appliquer à notre algorithme. 'f(n)' est O(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub>,  'f(n)' <= 'c g(n)' pour toute taille d'entrée n (n > n<sub>0</sub>).

*Exemple 1*

```
f(n) = 3log n + 100
g(n) = log n
```

Est-ce que `f(n)` O(g(n))?
Est-ce que `3 log n + 100` O(log n)?
Regardons maintenant la définition de Big-O.

```
3log n + 100 <= c * log n
```

Existe t-il une paire de constantes c, n<sub>0</sub> qui satisfait cela pour tout n > <sub>0</sub>?

```
3log n + 100 <= 150 * log n, n > 2 (Indéfini avec n = 1)
```

Oui ! La définition de Big-O a été satisfaite, donc `f(n)` is O(g(n)).

*Exemple 2*

```
f(n) = 3*n^2
g(n) = n
```

Est-ce que `f(n)` O(g(n))?
Est-ce que `3 * n^2` O(n)?
Regardons de nouveau la définition de Big-O.

```
3 * n^2 <= c * n
```

Existe t-il une paire de constantes c, n<sub>0</sub> qui satisfait cela pour tout n > <sub>0</sub>?
Non, il n'en existe pas. `f(n)` n'est pas égal à O(g(n)).

### Big-Omega

Big-Omega, courrament écris **Ω**, est une notation asymptotique pour le meilleur cas (ou limite de croissance basse) d'une fonction donnée. Il nous fournit une _**limite inférieure asymptotique**_ pour le taux de croissance du temps d'exécution d'un algorithme. 

Prenons 'f(n)' comme temps d'exécution de notre algorithme et 'g(n)' comme complexité de temps arbitraire que nous essayons d'appliquer à notre algorithme. 'f(n)' est Ω(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub>, 'f(n)' >= 'c g(n)' pour toute taille d'entrée n (n > n<sub>0</sub>).

### Remarque

Les taux de croissance asymptotiques fournis par les notations big-O et big-omega peuvent ou non être asymptotiquement serrés. Nous utilisons ainsi les notations small-o et small-omega pour désigner des limites qui ne sont pas asymptotiquement serrées. 

### Small-o
Small-o, couramment écris **o**, est une notation asymptotique pour désigner la limite supérieure (ce qui n'est pas asymptotiquement serré) du taux de croissance du temps d'exécution d'un algorithme. 

`f(n)` est o(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` < `c g(n)` 
pour toute taille d'entrée n (n > n<sub>0</sub>).

Les définitions de O-notation et o-notation sont similaires. La principale différence est visible quand f(n) = O(g(n)). Dans ce cas, la limite f(n) <= g(n) est appliquée pour _**quelques**_ constantes c > 0. Lorsque f(n) = o(g(n)), la limite f(n) < c g(n) est appliquée pour _**toute**_ constante c > 0.

### Small-omega
Small-omega, couramment écris **ω**, est une notation asymptotique pour désigner la limite inférieure (ce qui n'est pas asymptotiquement serré) du taux de croissance du temps d'exécution d'un algorithme.

`f(n)` est ω(g(n)), si pour certaines constantes c (c > 0) et n<sub>0</sub> (n<sub>0</sub> > 0), `f(n)` > `c g(n)` 
pour toute taille d'entrée n (n > n<sub>0</sub>).

Les définitions de Ω-notation et ω-notation sont similaires. La principale différence est visible quand f(n) = Ω(g(n)). Dans ce cas, la limite f(n) >= g(n) est appliquée pour _**quelques**_ constantes c > 0. Lorsque f(n) = ω(g(n)), la limite f(n) > c g(n) est appliquée pour _**toute**_ constante c > 0.

### Theta
Theta, couramment écris **Θ**, est une notation asymptotique pour désigner la _**borne asymptotique sous contrainte**_ du taux de croissance du temps d'exécution d'un algorithme. 

`f(n)` est Θ(g(n)), si pour certaines constantes réelles c1, c2 et n<sub>0</sub> (c1 > 0, c2 > 0, n<sub>0</sub> > 0), `c1 g(n)` < `f(n)` < `c2 g(n)` pour toute taille d'entrée n (n > n<sub>0</sub>).

∴ `f(n)` est Θ(g(n)) implique que `f(n)` est égal à O(g(n)) autant que `f(n)` est égal à Ω(g(n)).

N'hésitez pas à trouver de plus amples informations à ce sujet. Big-O est la notation la plus couramment utilisée pour le calcul de complexité du temps d'un algorithme. 

### Notes de fin
Il est difficile de traiter ce type de sujets dans un article court tant les exemples, méthodes et informations sont nombreuses. C'est pourquoi nous vous invitons à jeter un oeil aux livres et liens listés ci-dessous.  
Ces ressources apportent plus de détails avec des exemples et des définitions. 

## Livres

* [Algorithmes](http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X)
* [Conception algorithmique](http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651)

##  Ressources en ligne

* [MIT](http://web.mit.edu/16.070/www/lecture/big_o.pdf)
* [KhanAcademy](https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation)
* [Big-O Cheatsheet](http://bigocheatsheet.com/) - Structures, opérations, et algorithmes communs, classés par complexité.
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
translators:
    - ["Baptiste Fontaine", "http://bfontaine.net"]
filename: LearnBash-fr.sh
lang: fr-fr
---

Bash est le nom du shell UNIX, qui était aussi distribué avec le système
d’exploitation GNU et est le shell par défaut sur Linux et Mac OS X.

Presque tous les exemples ci-dessous peuvent être écrits dans un script shell
ou exécutés directement dans le terminal.

[Plus d’informations ici.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# La première ligne du script s’appelle le « shebang, » qui indique au système
# comment exécuter le script : http://fr.wikipedia.org/wiki/Shebang
# Comme vous pouvez le remarquer, les commentaires commencent par #. Le shebang
# est aussi un commentaire

# Un exemple simple qui affiche « Hello world! » :
echo Hello world!

# Chaque commande commence sur une nouvelle ligne ou après un point-virgule :
echo 'Ceci est la première ligne'; echo 'Ceci est la seconde ligne'

# La déclaration d’une variable ressemble à ça :
VARIABLE="Du texte"

# Mais pas comme ça :
VARIABLE = "Du texte"
# Bash va penser que VARIABLE est une commande qu’il doit exécuter et va
# afficher une erreur parce qu’elle est introuvable.

# Utiliser une variable :
echo $VARIABLE
echo "$VARIABLE"
echo '$VARIABLE'
# Quand vous utilisez la variable en elle-même – en lui assignant une valeur,
# en l’exportant ou autre – vous écrivez son nom sans $. Si vous voulez
# utiliser sa valeur, vous devez utiliser $.
# Notez qu’entourer une variable de deux guillemets simples (') empêche
# l’expansion des variables !

# Substitution de chaîne de caractères dans une variable
echo ${VARIABLE/Some/A}
# Ceci va remplacer la première occurrence de « Some » par « A »

# Sous-chaîne d’une variable
echo ${VARIABLE:0:7}
# Ceci va retourner seulement les 7 premiers caractères de la valeur

# Valeur par défaut d’une variable
echo ${FOO:-"ValeurParDefautSiFOOestVideOuInexistant"}
# Ceci marche pour null (FOO=), la chaîne de caractères vide (FOO=""). Zéro
# (FOO=0) retourne 0

# Variables pré-remplies :
# Il y a quelques variables pré-remplies utiles, comme :
echo "La valeur de retour du dernier programme : $?"
echo "Le PID du script : $$"
echo "Nombre d’arguments : $#"
echo "Arguments du script : $@"
echo "Arguments du script séparés en plusieurs variables : $1 $2..."

# Lire une valeur depuis l’entrée standard :
echo "Quel est votre nom ?"
read NAME # Notez que l’on n’a pas eu à déclarer une nouvelle variable
echo Bonjour, $NAME!

# Nous avons l’habituelle structure « if » :
# Utilisez 'man test' pour plus d’informations à propos des conditions
if [ $NAME -ne $USER ]
then
    echo "Votre nom n’est pas votre pseudo"
else
    echo "Votre nom est votre pseudo"
fi

# Il y a aussi l’exécution conditionnelle
echo "Toujours exécuté" || echo "Exécuté si la première commande ne réussit pas"
echo "Toujours exécuté" && echo "Exécuté si la première commande réussit"

# Pour utiliser && et || avec des commandes « if, » vous devez utiliser
# plusieurs paires de crochets :
if [ $NAME == "Steve" ] && [ $AGE -eq 15 ]
then
    echo "Ceci sera exécuté si $NAME est Steve ET $AGE est 15."
fi

if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ]
then
    echo "Ceci sera exécuté si $NAME est Daniya OU Zach."
fi

# Les expressions sont écrites dans le format suivant :
echo $(( 10 + 5 ))

# Contrairement aux autres langages de programmation, Bash est un shell — il
# est donc exécuté dans le contexte du répertoire courant. Vous pouvez lister
# les fichiers et dossiers dans le répertoire courant avec la commande `ls` :
ls

# Ces commandes ont des options qui contrôlent leur exécution :
ls -l # Liste tous les fichiers et répertoires sur des lignes séparées

# Les résultat de la commande précédente peuvent être passés à la commande
# suivante en entrée.
# La commande grep filtre l’entrée avec les motifs donnés. On peut ainsi lister
# les fichiers .txt dans le répertoire courant :
ls -l | grep "\.txt"

# Vous pouvez aussi rediriger l’entrée et les sorties standards et d’erreur
# d’une commande :
python2 hello.py < "entrée.in"
python2 hello.py > "sortie.out"
python2 hello.py 2> "erreur.err"
# Ceci va écraser le fichier s'il existe; si vous préférez écrire à la fin de
# celui-ci, utilisez >> à la place.

# Les commandes peuvent se substituer à l’intérieur d’autres commandes en
# utilisant $( ) :
# La commande ci-dessous affiche le nombre de fichiers et répertoires dans le
# répertoire courant :
echo "Il y a $(ls | wc -l) choses ici."

# On peut faire la même chose avec les accents graves `` mais on ne peut pas
# les imbriquer — la façon la plus courante est d’utiliser $( ).
echo "There are `ls | wc -l` items here."

# Bash a une commande case qui marche de façon similaire au switch de Java et
# C++ :
case "$VARIABLE" in
    #List patterns for the conditions you want to meet
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# La boucle for itère autant de fois qu’elle a d’arguments :
# Le contenu de $VARIABLE est affiché trois fois.
for VARIABLE in {1..3}
do
    echo "$VARIABLE"
done

# Ou écrivez-la de façon « traditionnelle » :
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Elles peuvent aussi être utilisées pour agir sur des fichiers :
# Cette boucle va exécuter la commande 'cat' sur fichier1 et fichier2
for VARIABLE in fichier1 fichier2
do
    cat "$VARIABLE"
done

# …ou la sortie d’une commande :
# Ceci va afficher la sortie de ls.
for OUTPUT in $(ls)
do
    cat "$OUTPUT"
done

# Boucle while :
while [ true ]
do
    echo "corps de la boucle ..."
    break
done

# Vous pouvez aussi définir des fonctions
# Définition :
function foo ()
{
    echo "Les arguments fonctionnent comme les arguments de script : $@"
    echo "Et : $1 $2..."
    echo "Ceci est une fonction"
    return 0
}

# Ou plus simplement :
bar ()
{
    echo "Une autre façon de définir des fonctions !"
    return 0
}

# Appeler votre fonction
foo "Mon nom est" $NAME

# Il y a plein de commandes utiles que vous devriez apprendre :
# affiche les 10 dernières lignes de fichier.txt
tail -n 10 fichier.txt
# affiche les 10 premières lignes de fichier.txt
head -n 10 fichier.txt
# trie les lignes de fichier.txt
sort fichier.txt
# montre ou omet les lignes répétées, avec -d pour les montrer
uniq -d fichier.txt
# affiche uniquement la première colonne avant le caractère « , »
cut -d ',' -f 1 fichier.txt
# remplace chaque occurrence de 'okay' par 'super' dans fichier.txt
# (compatible avec les expression rationnelles)
sed -i 's/okay/super/g' fichier.txt
# affiche toutes les lignes de fichier.txt qui correspondent à une expression
# rationnelle, dans cet exemple les lignes qui commencent par « foo » et
# finissent par « bar »
grep "^foo.*bar$" fichier.txt
# ajoutez l’option « -c » pour afficher le nombre de lignes concernées
grep -c "^foo.*bar$" fichier.txt
# Si vous voulez vraiment chercher une chaîne de caractères, et non
# l’expression rationnelle, utilisez fgrep (ou grep -F)
fgrep "^foo.*bar$" fichier.txt
```
---
language: bf
filename: learnbrainfuck-fr.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Baptiste Fontaine", "http://bfontaine.net"]
lang: fr-fr
---

Brainfuck (sans majuscule à part au début d’une phrase) est un langage
Turing-complet extrêmement simple avec seulement 8 commandes.

```
Tout caractère en dehors de "><+-.,[]" (en dehors des guillemets) est ignoré.

Brainfuck est représenté par un tableau de 30 000 cellules initialisées à 0 et
un pointeur de données pointant sur la cellule courante.

Il y a huit commandes :
+ : Incrémente la valeur de la cellule courante de un.
- : Décrémente la valeur de la cellule courante de un.
> : Déplace le pointeur de données sur la cellule suivante (à droite).
< : Déplace le pointeur de données sur la cellule précédente (à gauche).
. : Affiche la valeur ASCII de la cellule courante (par ex. 65 = 'A').
, : Lit un caractère et le place dans la cellule courante.
[ : Si la valeur dans la cellule courante vaut 0, saute au ] correspondant.
    Sinon, continue avec la commande suivante.
] : Si la valeur dans la cellule courante vaut 0, continue avec la commande
    suivante. Sinon, retourne au [ correspondant.

[ et ] forment une boucle « tant que » (« while »). Ils doivent évidemment
aller par paires.

Regardons quelques programmes simples en brainfuck.

++++++ [ > ++++++++++ < - ] > +++++ .

Ce programme affiche la lettre 'A'. Il commence par incrémenter la première
cellule à 6. Il entre ensuite dans une boucle et se déplace sur la seconde
cellule. Il l’incrémente 10 fois, retourne sur la première cellule, et la
décrémente. Cette boucle est exécutée 6 fois (ce qui correspond aux 6
décrémentations de la première cellule pour la faire atteindre 0, ce qui fait
sortir de la boucle).

À ce moment-là, nous sommes sur la première cellule, qui a une valeur de 0,
tandis que la seconde cellule a une valeur de 60. Nous nous déplaçons sur
celle-ci, l’incrémentons 5 fois, pour une valeur de 65, et affichons sa valeur.
En ASCII, 65 correspond à 'A' donc le programme affiche 'A' dans le terminal.

, [ > + < - ] > .

Ce programme lit un caractère sur l’entrée standard et le copie dans la
première cellule. Il commence ensuite une boucle : il bouge sur la seconde
cellule, incrémente sa valeur, retourne sur la première et décrémente sa
valeur. Il continue jusqu’à ce que la valeur de la première cellule soit à 0,
et que la seconde cellule contienne l’ancienne valeur de la première. Comme
nous sommes sur la première cellule à la fin de la boucle, il bouge sur la
seconde et affiche sa valeur en ASCII.

Souvenez-vous que les espaces sont uniquement là pour favoriser la lisibilité,
vous pourriez tout aussi aisément écrire le programme comme ceci :

,[>+<-]>.

Essayez et devinez ce que ce programme fait :

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Ce programme prend deux nombres en entrée, et les multiplie.

Il commence par lire deux entrées, puis commence une boucle externe, qui a une
condition sur la première cellule. Il bouge ensuite sur la seconde, et commence
une boucle interne sur celle-ci, en incrémentant la troisième cellule. Il y a
cependant un problème : à la fin de la boucle interne, la valeur de la seconde
cellule est à zéro. Dans ce cas, la boucle interne ne fonctionnera pas une
seconde fois. Pour régler le problème, nous incrémentons aussi la quatrième
cellule, puis recopions sa valeur dans la seconde cellule.
À la fin, la troisième cellule contient le résultat de la multiplication.
```

Et voilà ce qu’est le brainfuck. Pas très dur, hein ? Pour le fun, vous pouvez
écrire vos propres programmes en brainfuck, ou écrire un interpréteur brainfuck
dans un autre langage. L’interpréteur est relativement simple à implémenter,
mais si vous êtes un masochiste, essayez d’écrire un interpréteur brainfuck en…
brainfuck.
---
category: Algorithms & Data Structures
name: Binary Search
contributors:
    - ["Abhishek Jaisingh", "http://github.com/abhishekjiitr"]
translators:
    - ["Hughes Perreault", "https://github.com/hperreault"]
lang: fr-fr
---

# Recherche Binaire

## Pourquoi la Recherche Binaire ?

La recherche est un des principaux problèmes dans le domaine de l'informatique. De nos jours, il y a plus de 1 milliard de recherches par année, et nous avons besoin d'algorithmes pour faire cela rapidement. La recherche binaire est un des algorithmes les plus fondamentaux en informatique. Pour pouvoir l'explorer en détail, nous allons d'abord établir une base théorique, puis nous allons utiliser cette base pour implémenter l'algorithme en soi.

## Introduction

Une façon simple d'implémenter la recherche est de faire une recherche linéaire. Cependant, cette approche prend beaucoup de temps, et ce temps augmente linéairement avec la quantité de données. Par exemple, partons du premier élément d'un tableau t[], et un par un, comparons x avec chaque élément de t[]. Si x est égal à un élément, nous retournons l'index, si x n'égale aucun élément, nous retournons -1.

```
Recherche Linéaire: O (n)               Temps Linéaire

Recherche Binaire:  O ( log(n) )        Temps Logarithmique

```
```
def search(arr, x):

    for i in range(len(arr)):

        if arr[i] == x:
            return i

    return -1

```
## L'Algorithme de Recherche Binaire

Le prérequis fondamental de la recherche binaire est que les éléments soient triés.

### Algo

```
L'idée derrière la recherche binaire est d'utiliser le fait que le tableau est trié afin de réduire la complexité à O(Log(n)). Nous pouvons ignorer la moitié des éléments après la première comparaison.
1) Comparons x avec l'élément du milieu.
2) Si x est égal à cet élément, nous retournons l'index du milieu.
3) Sinon, si x est plus grand que l'élément du milieu, alors x peut seulement être dans la dernière moitié du tableau. Donc, nous recommençons la procédure avec cette dernière moitié.
4) Sinon (x est plus petit), nous recommençons la procédure avec la première moitié du tableau.
Ensuite nous avons une implémentation récursive de la recherche binaire.

```

### Note de la fin

Partie en construction.

## Livre

* [CLRS EN](https://mitpress.mit.edu/books/introduction-algorithms)
* [Algorithmes EN](http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X)
* [Design d'Algorithmes EN](http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651)

## Ressources en ligne

* [GeeksforGeeks EN](http://www.geeksforgeeks.org/the-ubiquitous-binary-search-set-1/)
* [Topcoder Tutorial EN](https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/)
---
language: c++
filename: learncpp-fr.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Matt Kline", "https://github.com/mrkline"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Connor Waters", "http://github.com/connorwaters"]
translators:
    - ["Xuan-thi Nguyen", "http://github.com/mellenguyen"]
lang: fr-fr
---

C++ est un langage de programmation système qui,
[selon son créateur Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote),
fut créé pour

- être un "C amélioré"
- gérer l'abstraction des données
- gérer la programmation orienté objet
- gérer la programmation générique

Bien que sa syntaxe puisse être plus difficile ou complexe que des langages
récents, il est largement utilisé car il compile en instructions natives qui
peuvent être directement exécutées par le processeur et offre un contrôle
rigoureux du matériel (comme le C) tout en fournissant des caractéristiques de
haut niveau telles que la généricité, les exceptions et les classes.
Cette combinaison de vitesse et de fonctionnalités rend le C++ un des langages
de programmation les plus utilisés au monde.

```c++
/////////////////////////////////
// Comparaison avec le C
/////////////////////////////////

// C++ est _presque_ un sur-ensemble du C et partage sa syntaxe basique pour les
// déclarations de variables, les types primitifs et les fonctions.

// Tout comme en C, le point d'entrée de votre programme est une fonction
// appelée main, avec un integer comme type de retour.
// Cette valeur constitue l'état de fin d'exécution du programme.
// Voir http://en.wikipedia.org/wiki/Exit_status pour plus d'informations.
int main(int argc, char** argv)
{
    // Les arguments de ligne de commande sont passés avec argc et argv de la
    // même manière qu'en C.
    // argc indique le nombre d'arguments,
    // et argv est un tableau de chaînes façon C (char*)
    // représentant les arguments.
    // Le premier argument est le nom par lequel le programme est appelé.
    // argc et argv peuvent être omis si vous ne vous souciez pas des
    // arguments, nous donnant comme signature de fonction int main()

    // Un état de fin d'exécution 0 indique le succès.
    return 0;
}

// Cependant, C++ varie du C selon certains éléments:

// En C++, les caractères littéraux sont des chars
sizeof('c') == sizeof(char) == 1

// En C, les caractères littéraux sont des ints
sizeof('c') == sizeof(int)

// C++ a un prototypage strict
void func(); // fonction qui ne prend aucun argument

// En C
void func(); // fonction qui peut prendre n'importe quel nombre d'arguments

// Utilise nullptr au lieu de NULL in C++
int* ip = nullptr;

// Les en-têtes standards du C sont disponibles en C++,
// mais son préfixés avec "c" et n'ont pas de suffixe .h
#include <cstdio>

int main()
{
    printf("Bonjour tout le monde!\n");
    return 0;
}

/////////////////////////////////
// Surchage de fonctions
/////////////////////////////////

// C++ gère la surchage de fonctions
// Chaque fonction fournie prend différents paramètres.

void print(char const* maChaine)
{
    printf("Chaîne %s\n", maChaine);
}

void print(int monEntier)
{
    printf("Mon entier est %d", monEntier);
}

int main()
{
    print("Bonjour"); // Utilise void print(const char*)
    print(15); // Utilise void print(int)
}

/////////////////////////////////////////////
// Arguments par défaut de fonctions
/////////////////////////////////////////////

// Vous pouvez fournir des arguments par défaut pour une fonction s'ils ne sont
// pas fournis par l'appelant.

void faitDesChosesAvecDesEntiers(int a = 1, int b = 4)
{
    // Do something with the ints here
}

int main()
{
    faitDesChosesAvecDesEntiers();      // a = 1,  b = 4
    faitDesChosesAvecDesEntiers(20);    // a = 20, b = 4
    faitDesChosesAvecDesEntiers(20, 5); // a = 20, b = 5
}

// Les arguments par défaut doivent être à la fin de la liste des arguments.

void invalidDeclaration(int a = 1, int b) // Erreur !
{
}


//////////////////////////
// Espaces de nom
//////////////////////////

// Les espaces de nom fournissent une séparation des portées pour les
// variables, fonctions, et autres déclarations.
// Les espaces de nom peuvent être imbriqués.

namespace Premier {
    namespace Imbrique {
        void foo()
        {
            printf("Ceci est le Premier::Imbrique::foo\n");
        }
    } // fin de l'espace de nom Imbrique
} // fin de l'espace de nom Premier

namespace Second {
    void foo()
    {
        printf("Ceci est le Second::foo\n")
    }
}

void foo()
{
    printf("Ceci est un foo global\n");
}

int main()
{
    // Inclut tous les symboles de l'espace de nom Second dans la portée
    // actuelle. Notez que le foo() simple ne marche plus, car l'appel est
    // ambigu entre le foo de l'espace de nom Second et celui de premier
    // niveau.
    using namespace Second;

    Second::foo(); // imprime "Ceci est le Second::foo"
    Premier::Imbrique::foo(); // imprime "Ceci est le Premier::Imbrique::foo"
    ::foo(); // imprime "Ceci est un foo global"
}

/////////////////////////
// Entrée/Sortie
/////////////////////////

// Les entrées et sorties en C++ utilisent des flux (streams)
// cin, cout et cerr représentent stdin, stdout et stderr.
// << est l'opérateur d'insertion et >> est l'opérateur d'extraction.

#include <iostream> // Inclusion pour les flux d'entrée/sortie

// Les flux sont dans l'espace de nom std (librairie standard)
using namespace std;

int main()
{
   int monEntier;

   // Affiche sur stdout (ou le terminal/l'écran)
   cout << "Entrez votre chiffre favori:\n";
   // Prend l'entrée clavier
   cin >> monEntier;

   // cout peut également être formaté
   cout << "Votre chiffre favori est " << monEntier << "\n";
   // imprime "Votre chiffre favori est <monEntier>"

    cerr << "Utilisé pour les messages d'erreurs";
}

/////////////////////////////////
// Chaînes de caractères
/////////////////////////////////

// Les chaînes de caractères en C++ sont des objets et ont plusieurs fonctions
// membres
#include <string>

// Les chaînes de caractères sont aussi dans l'espace de
// nom std (librairie standard)
using namespace std;

string maChaine = "Bonjour";
string monAutreChaine = " tout le monde !";

// + est utilisé pour la concaténation.
cout << maChaine + monAutreChaine; // Bonjour tout le monde !"

cout << maChaine + " toi !"; // "Bonjour toi !"

// Les chaînes de caractères C++ sont mutables.
maChaine.append(" le chien !");
cout << maChaine; // "Bonjour le chien !"


//////////////////////
// Références
//////////////////////

// En plus des pointeurs comme ceux en C,
// C++ possède des _références_.
// Ce sont des types de pointeurs qui ne peuvent pas être réassignés
// une fois initialisés, et ne peuvent pas être nulles.
// Ils partagent la même syntaxe que les variables elles-mêmes:
// les * ne sont pas nécessaires pour les déréférencer et
// & (addresse de) n'est pas utilisé pour l'assignement.

using namespace std;

string foo = "Je suis foo";
string bar = "Je suis bar";


string& fooRef = foo; // Ceci créé une référence à foo
fooRef += ". Salut!"; // Modifie foo à travers la référence
cout << fooRef; // Affiche "Je suis foo. Salut!"

// Ne réassigne pas "fooRef". Ceci revient à faire "foo = bar", et
// foo == "I am bar"
// après cette ligne.
cout << &fooRef << endl; // Affiche l'adresse de foo
fooRef = bar;
cout << &fooRef << endl; // Affiche toujours l'adresse de foo
cout << fooRef;  // Affiche "Je suis bar"

// L'adresse de fooRef reste la même, c.-à-d. référence toujours foo.


const string& barRef = bar; // Créé une référence constante de bar.
// Comme en C, les valeurs constantes (et pointeurs et références) ne peuvent
// être modifiées.

// Erreur, les valeurs constantes ne peuvent être modifiées.
barRef += ". Salut!";

// Parenthèse: avant de développer le sujet des références, nous devons
// introduire un concept appelé un objet temporaire. Supposons que nous ayons
// le code suivant :
string objetTemporaireFun() { ... }
string valeurRetenu = objetTemporaireFun();

// Les différents événements se déroulant à la seconde ligne sont :
//   - un objet chaîne de caractères est retourné de objetTemporaireFun
//   - une nouvelle chaîne de caractères est construite avec la valeur
//     retournée comme argument du constructeur
//   - l'objet retourné est détruit.
// L'objet retourné est appelé un objet temporaire. Les objets temporaires sont
// créés chaque fois qu'une fonction retourne un objet, et sont détruits à la
// fin de l'évaluation de l'expression fermante (c'est ce que le standard
// énonce, mais les compilateurs sont autorisés à changer ce comportement.
// Cherchez "optimisation valeur de retour" si vous êtes intéressé par ce genre
// de détails).
// Dans cette ligne de code :
foo(bar(objetTemporaireFun()))

// en supposant que foo et bar existent, l'objet retourné de objetTemporaireFun
// est passé à bar, et est détruit avant que foo soit appelé.

// Revenons maintenant aux références. L'exception à la règle "objet détruit à
// la fin de l'expression fermante" s'applique dans le cas d'un objet
// temporaire lié à une référence constante, où sa durée de vie se voit
// prolongée à la portée courante :

void referenceConstanteObjetTemporaireFun() {
  // referenceConst prend l'objet temporaire, et est valide jusqu'à la fin de
  // la fonction.
  const string& referenceConst = objetTemporaireFun();
  ...
}

// Un autre type de référence introduit en C++11 est spécifiquement pour les
// objets temporaires. Vous ne pouvez pas avoir de variable de ce type, mais
// il prime dans la résolution de surcharge :

void fonctionFun(string& s) { ... }  // Référence régulière
void fonctionFun(string&& s) { ... }  // Référence un objet temporaire

string foo;
// Appelle la version avec référence régulière
fonctionFun(foo);

// Appelle la version avec référence temporaire
fonctionFun(objetTemporaireFun());

// Par exemple, vous aurez ces deux versions de constructeurs pour
// std::basic_string :
basic_string(const basic_string& other);
basic_string(basic_string&& other);

// L'idéal étant de construire une nouvelle chaîne de caractères avec un objet
// temporaire (qui sera détruit de toute façon), nous pouvons ainsi avoir un
// constructeur qui "sauve" des parties de cette chaîne de caractères
// temporaire. Vous verrez ce concept sous le nom de "sémantique de mouvement".

////////////////////////
// Enumérations
////////////////////////

// Les énumérations sont un moyen d'assigner une valeur à une constante
// fréquemment utilisée pour une meilleure visualisation et lecture du code.
enum ETypesDeVoitures
{
  Berline,
  Hayon,
  4x4,
  Break
};

ETypesDeVoitures ObtenirVoiturePreferee()
{
	return ETypesDeVoitures::Hayon;
}

// En C++11, il existe une manière simple d'assigner un type à une énumération,
// ce qui peut-être utile en sérialisation de données et conversion
// d'énumérations entre le type voulu et ses constantes respectives.
enum ETypesDeVoitures : uint8_t
{
  Berline, // 0
  Hayon, // 1
  4x4 = 254, // 254
  Hybride // 255
};

void EcrireOctetDansLeFichier(uint8_t ValeurEntree)
{
	// Sérialise la valeur d'entrée dans un fichier
}

void EcrireTypeVoiturePrefereDansLeFichier(ETypesDeVoitures TypeVoitureEntree)
{
	// L'énumération est implicitement convertie en uint8_t du à la déclaration
    // de son type d'énumération
	EcrireOctetDansLeFichier(TypeVoitureEntree);
}

// D'autre part, vous pourriez ne pas vouloir que des énumérations soient
// accidentellement converties en entiers ou en d'autres énumérations. Il est
// donc possible de créer une classe d'énumération qui ne sera pas
// implicitement convertie.
enum class ETypesDeVoitures : uint8_t
{
  Berline, // 0
  Hayon, // 1
  4x4 = 254, // 254
  Hybride // 255
};

void EcrireOctetDansLeFichier(uint8_t ValeurEntree)
{
	// Sérialise la valeur d'entrée dans un fichier
}

void EcrireTypeVoiturePrefereDansLeFichier(ETypesDeVoitures TypeVoitureEntree)
{
	// Ne compilera pas même si ETypesDeVoitures est un uint8_t car
	// l'énumération est déclarée en tant que "classe d'énumération" !
	EcrireOctetDansLeFichier(TypeVoitureEntree);
}

///////////////////////////////////////////////////
// Classes et programmation orientée objet
///////////////////////////////////////////////////

#include <iostream>

// Déclare une classe.
// Les classes sont habituellement déclarées dans les fichiers d'en-tête (.h ou .hpp).
class Chien {
    // Les variables et fonctions membres sont privées par défaut.
    std::string nom;
    int poids;

// Tous les membres suivants sont publiques jusqu'à ce que "private:" ou
// "protected:" soit trouvé
public:

    // Constructeur par défaut
    Chien();

    // Déclaractions de fonctions membres (implémentations à suivre)
    // Notez que nous utilisons std::string ici au lieu de placer
    // using namespace std;
    // au-dessus.
    // Ne jamais utiliser une instruction "using namespace" dans l'en-tête.
    void initialiserNom(const std::string& nomDuChien);

    void initialiserPoids(int poidsDuChien);

    // Les fonctions qui ne modifient pas l'état de l'objet devraient être
    // marquées en constantes avec const.
    // Ceci vous permet de les appeler avec une référence constante vers l'objet.
    // Notez aussi que les fonctions devant être surchargées dans des classes
    // dérivées doivent être explicitement déclarées avec _virtual_.
    // Les fonctions ne sont pas virtuelles par défault pour des raisons de
    // performances.
    virtual void imprimer() const;

    // Les fonctions peuvent également être définies à l'intérieur du corps de
    // la classe. Ces fonctions sont automatiquement "inline".
    void aboyer() const { std::cout << nom << " fait ouaf !\n"; }

    // En plus des constructeurs, C++ fournit des destructeurs.
    // Ils sont appelés quand l'objet est supprimé ou dépasse le cadre de sa
    // portée. Ceci permet de puissants paradigmes tels que RAII
    // (voir plus loin)
    // Le destructeur devrait être virtuel si la classe est abstraite;
    // s'il n'est pas virtuel, alors le destructeur de la classe dérivée ne
    // sera pas appelé si l'objet est détruit par le biais d'une référence à la
    // classe de base ou d'un pointeur.
    virtual ~Chien();

}; // Un point virgule doit clôre la définition de la classe.

// Les fonctions membres de la classe sont habituellement implémentées dans des
// fichiers .cpp.
Chien::Chien()
{
    std::cout << "Un chien a été construit\n";
}

// Les objets (comme les chaînes de caractères) devraient être passés par
// référence si vous les modifiez ou par référence constante si vous ne les
// modifiez pas.
void Chien::initialiserNom(const std::string& nomDuChien)
{
    nom = nomDuChien;
}

void Chien::initialiserPoids(int poidsDuChien)
{
    poids = poidsDuChien;
}

// Notez que le mot-clé "virtual" est nécessaire uniquement à la déclaration,
// et non à la définition.
void Chien::imprimer() const
{
    std::cout << "Le chien s'appelle " << nom << " et pèse " << poids << "kg\n";
}

Chien::~Chien()
{
    cout << "Au revoir " << nom << " !\n";
}

int main() {
    Chien monChien; // imprime "Un chien a été construit"
    monChien.initialiserNom("Barkley");
    monChien.initialiserPoids(10);
    monChien.imprime(); // imprime "Le chien s'appelle Barkley et pèse 10 kg"
    return 0;
} // prints "Au revoir Barkley !"

// Héritage :

// Cette classe hérite de toutes les propriétés publiques et protégées de la
// classe Chien ainsi que celles privées, mais n'ont pas accès direct aux
// membres et méthodes privés sans l'aide d'une méthode publique ou protégée
class ChienDomestique : public ChienDomestique {

    void definirProprietaire(const std::string& proprietaireDuChien);

    // Surcharge le comportement de la fonction d'impression pour tous les
    // ChienDomestiques.
    // Voir https://fr.wikipedia.org/wiki/Polymorphisme_(informatique)#Polymorphisme_par_sous-typage
    // pour une introduction plus générale si vous n'êtes pas familier avec le
    // concept de polymorphisme par sous-typage (appelé aussi polymorphisme
    // d'inclusion).
    // Le mot-clé "override" est optionnel mais assure que vous surchargez bien
    // la méthode de la classe de base.
    void imprimer() const override;

private:
    std::string proprietaire;
};

// Pendant ce temps, dans le fichier .cpp correspondant :

void ChienDomestique::definirProprietaire(const std::string& proprietaireDuChien)
{
    proprietaire = proprietaireDuChien;
}

void ChienDomestique::imprimer() const
{
    // Appelle la fonction "imprimer" dans la classe de base Chien
    Chien::imprimer();
    std::cout << "Le chien appartient à " << proprietaire << "\n";
    // Affiche "Le chien est <nom> et pèse <poids>"
    //         "Le chien appartient à <proprietaire>"
}

////////////////////////////////////////////////////
// Initialisation et opérateur de surcharge
////////////////////////////////////////////////////

// En C++, vous pouvez surcharger le comportement d'opérateurs tels
// que +, -, *, /, etc.
// La surcharge se fait en définissant une fonction qui sera appelée à chaque
// fois que l'opérateur sera utilisé.

#include <iostream>
using namespace std;

class Point {
public:
    // Les variables membres peuvent avoir des valeurs par défaut
    double x = 0;
    double y = 0;

    // Définit un constructeur par défaut qui ne fait rien
    // mais initialise le Point à la valeur par défaut (0, 0)
    Point() { };

    // La syntaxe suivante s'appelle une liste d'initialisation et est
    // la façon correcte d'initialiser les valeurs des membres d'une classe.
    Point (double a, double b) :
        x(a),
        y(b)
    { /* Ne fait rien à part initialiser les valeurs */ }

    // Surcharge l'opérateur +
    Point operator+(const Point& rhs) const;

    // Surcharge l'opérateur +=
    Point& operator+=(const Point& rhs);

    // Il serait également logique d'ajouter les opérateurs - et -=,
    // mais nous les éclipsons par soucis de concision.
};

Point Point::operator+(const Point& rhs) const
{
    // Créé un nouveau point qui est la somme de celui-ci de rhs.
    return Point(x + rhs.x, y + rhs.y);
}

Point& Point::operator+=(const Point& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

int main () {
    Point haut (0,1);
    Point droite (1,0);
    // Appelle l'opérateur + du Point
    // Le point "haut" appelle la fonction + avec "droite" comme paramètre
    Point resultat = haut + droite;
    // Affiche "Le résultat est haut-droite (1,1)"
    cout << "Le résultat est haut-droite (" << resultat.x << ','
         << resultat.y << ")\n";
    return 0;
}

////////////////////////////////
// Patrons (templates)
////////////////////////////////

// Les templates (patrons) en C++ sont majoritairement
// utilisés pour la programmation générique, bien qu'ils soient bien plus
// puissants que les constructeurs génériques dans d'autres langages.
// Ils gèrent également la spécialisation explicite et partielle ainsi que
// les classes fonctionnelles; en fait, ils sont un langage fonctionnelles
// Turing-complete embedded in C++ !

// Nous commencons avec le genre de programmation générique auquel vous êtes
// peut-être familier. Pour définir une classe ou fonction qui prend un type de
// paramètre particulier :
template<class T>
class Boite {
public:
    // Dans cette classe, T représente n'importe quel type possible.
    void inserer(const T&) { ... }
};

// Pendant la compilation, le compilateur génère des copies de chaque template
// avec les paramètres substitués; ainsi, la définition complète de chaque
// classe doit être présente à chaque appel. C'est pourquoi vous verrez les
// classes de templates définies entièrement dans les fichiers d'en-tête.

// Pour instancier une classe de template sur la pile ("stack") :
Boite<int> boiteDEntiers;

// et vous pouvez l'utiliser comme prévu :
boiteDEntiers.inserer(123);

// Vous pouvez, bien sûr, imbriquer les templates :
Boite<Boite<int> > boiteDeBoites;
boiteDeBoites.inserer(boiteDEntiers);

// Jusqu'à C++11, il était nécessaire de placer un espace entre les deux '>'s,
// sinon '>>' était parsé en tant qu'opérateur de décalage vers la droite.

// Vous croiserez peut-être cette syntaxe
//   template<typename T>
// à la place. Les mot-clé 'class' et 'typename' sont _généralement_
// interchangeables. Pour plus d'explications, allez à
//   http://en.wikipedia.org/wiki/Typename
// ou
// https://fr.wikibooks.org/wiki/Programmation_C-C%2B%2B/Les_templates/Mot-cl%C3%A9_typename
// (oui, ce mot-clé a sa propre page Wikipedia).

// De manière similaire, un patron de fonction :
template<class T>
void aboyerTroisFois(const T& entree)
{
    entree.aboyer();
    entree.aboyer();
    entree.aboyer();
}

// Remarquez ici que rien n'est spécifié à propos du type du paramètre. Le
// compilateur va générer et vérifier le type à chaque appel du patron, c'est
// pourquoi l'appel de fonction suivant marche pour n'importe quel type 'T' qui
// a une méthode constante 'aboyer' !

Chien docile;
docile.initialiserNom("Docile")
aboyerTroisFois(docile); // Affiche "Docile fait ouaf !" trois fois.

// Les paramètres génériques (ou paramètres template) ne sont pas forcément des
// classes :
template<int Y>
void imprimerMessage() {
  cout << "Apprenez le C++ en " << Y << " minutes !" << endl;
}

// Vous pouvez explicitement spécialiser les templates pour un code plus
// optimisé. Bien sûr, les utilisations effectives de la spécialisation ne sont
// pas aussi triviales que celle-ci.
// Notez que vous avez toujours besoin de déclarer la fonction (ou classe)
// comme template, même si vous spécifiez explicitement tous les paramètres.
template<>
void imprimerMessage<10>() {
  cout << "Apprenez le C++ plus vite en seulement 10 minutes !" << endl;
}

// Affiche "Apprenez le C++ en 20 minutes !"
imprimerMessage<20>();
// Affiche "Apprenez le C++ plus vite en seulement 10 minutes !"
imprimerMessage<10>();

//////////////////////////////////
// Gestion des exceptions
//////////////////////////////////

// La bibliothèque standard fournit quelques types d'exception
// (voir http://en.cppreference.com/w/cpp/error/exception)
// mais n'importe quel type peut être lancé en tant qu'exception.
#include <exception>
#include <stdexcept>

// Toutes les exceptions lancées à l'intérieur d'un block _try_ peuvent être
// attrapées par les blocs de traitement d'erreurs (_catch_ handlers).
try {
    // N'allouez pas des exceptions sur le tas (heap) en utilisant _new_.
    throw std::runtime_error("Un problème s'est produit");
}

// Attrapez les exceptions avec des références constantes si ce sont des objets
catch (const std::exception& ex)
{
    std::cout << ex.what();
}

// Attrape n'importe quelle exception non attrapée par les blocs _catch_
// précédents
catch (...)
{
    std::cout << "Exception inconnue attrapée";
    throw; // Re-lance l'exception
}

////////////////
// RAII
////////////////

// RAII signifie "Resource Acquisition Is Initialization", soit l'Acquisition
// d'une Ressource est une Initialisation en français.
// Il est souvent considéré comme le paradigme le plus puissant en C++ et
// est le concept simple qu'un constructeur d'un objet acquiert les ressources
// d'un objet et que le destructeur les libère.

// Afin de comprendre son utilité, considérons une fonction qui utilise la
// gestion d'un fichier C :
void faireQuelqueChoseAvecUnFichier(const char* nomDuFichier)
{
    // Pour commencer, supposns que rien ne peut échouer.

    FILE* fh = fopen(nomDuFichier, "r"); // Ouvre le fichier en lecture

    faireQuelqueChoseAvecLeFichier(fh);
    faireAutreChoseAvec(fh);

    fclose(fh); // Ferme la gestion du fichier.
}

// Malheureusement, les choses deviennent compliquées avec la gestion
// d'erreurs. Supposons que fopen échoue, et que faireQuelqueChoseAvecLeFichier
// et faireAutreChoseAvec retournent des codes d'erreur si elles échouent.
//  (Les exceptions sont le meilleur moyen de gérer l'échec, mais des
//   programmeurs, surtout avec un passif en C,
//   sont en désaccord avec l'utilité des exceptions).
// Nous devons maintenant vérifier chaque appel en cas d'échec et fermer la
// gestion du fichier si un problème se produit.
bool faireQuelqueChoseAvecUnFichier(const char* nomDuFichier)
{
    FILE* fh = fopen(nomDuFichier, "r"); // Ouvre le fichier en mode lecture.
    if (fh == nullptr) // Le pointeur retourné est null à un échec.
        return false; // Signale cet échec à l'appelant.

    // Suppose que chaque fonction retourne faux si elle échoue
    if (!faireQuelqueChoseAvecLeFichier(fh)) {
        fclose(fh); // Ferme le flux d'entrée du fichier pour empêcher les fuites
        return false; // Propage l'erreur
    }
    if (!faireAutreChoseAvec(fh)) {
        fclose(fh);
        return false;
    }

    fclose(fh);
    return true;
}

// Les programmeurs en C clarifient souvent tout cela en utilisant goto :
bool faireQuelqueChoseAvecUnFichier(const char* nomDuFichier)
{
    FILE* fh = fopen(nomDuFichier, "r");
    if (fh == nullptr)
        return false;

    if (!faireQuelqueChoseAvecLeFichier(fh))
        goto echec;

    if (!faireAutreChoseAvec(fh))
        goto echec;

    fclose(fh); // Ferme la gestion du fichier
    return true; // Indique le succès

echec:
    fclose(fh);
    return false; // Propage l'erreur
}

// Si les fonctions indiquent des erreurs en utilisant des exceptions,
// les choses sont un peu plus claires, mais toujours sous-optimales.
void faireQuelqueChoseAvecUnFichier(const char* nomDuFichier)
{
    FILE* fh = fopen(nomDuFichier, "r"); // Ouvre le fichier en lecture
    if (fh == nullptr)
        throw std::runtime_error("Ouverture du fichier impossible.");

    try {
        faireQuelqueChoseAvecLeFichier(fh);
        faireAutreChoseAvec(fh);
    }
    catch (...) {
        // Assurez-vous de bien fermer le fichier si une erreur arrive
        fclose(fh);
        throw; // Puis re-lancer l'exception
    }

    fclose(fh); // Ferme le fichier
    // Tout s'est déroulé correctement
}

// Comparez ceci à l'utilisation de la classe de flux de fichier
// en C++ (fstream).
// fstream utilise son destructeur pour fermer le fichier.
// Pour rappel, les destructeurs sont automatiquement appelée dès qu'un objet
// sort du cadre de sa portée.
void faireQuelqueChoseAvecUnFichier(const std::string& nomDuFichier)
{
    // ifstream is short for input file stream
    std::ifstream fh(nomDuFichier); // Ouvre le fichier

    // Faire des choses avec le fichier
    faireQuelqueChoseAvecLeFichier(fh);
    faireAutreChoseAvec(fh);

} // Le fichier est automatiquement fermé ici par le destructeur

// Ceci a des avantages _énormes_ :
// 1. Peu importe la situation, la ressource (dans ce cas précis la gestion
//    de fichier) sera libérée. Si le destructeur est écrit correctement,
//    il est _impossible_ d'oublier de fermer la gestion et d'entraîner une
//    une fuite de ressources (si l'objet est sur la pile).
// 2. Remarquez que le code est beaucoup plus clair.
//    Le destructeur gère la fermeture du fichier discrètement sans avoir
//    besoin de s'en préoccuper.
// 3. Le code est fiable par rapport aux exceptions.
//    Une exception peut être lancée n'importe où dans la fonction, le
//    nettoyage se fera toujours.

// Tout code C++ idiomatique utilise considérablement RAII pour toutes les
// ressources.
// Des exemples additionnels inclus :
// - La mémoire utilisant unique_ptr et shared_ptr
// - Des conteneurs (containers) - la liste chaînée de la librairie standard,
//   des vecteurs (c.-à-d. tableaux auto-redimensionnés), tables de hachage, et
//   ainsi de suite. Tous détruisent leur contenu quand ils sortent du cadre
//   de leur portée.
// - Les mutex utilisant lock_guard et unique_lock


//////////////////
// Divers
//////////////////

// Ici sont regroupés des aspects du C++ qui peuvent être surprenants aux
// novices (et même à quelques habitués).
// Cette section est, malheureusement, grandement incomplète; C++ est un des
// langages où il est très facile de se tirer soi-même dans le pied.

// Vous pouvez surcharger des méthodes privées !
class Foo {
  virtual void bar();
};
class FooSub : public Foo {
  virtual void bar();  // Surcharge Foo::bar!
};

// 0 == false == NULL (la plupart du temps) !
bool* pt = new bool;
*pt = 0; // Affecte false à la valeur de la variable pointée par 'pt'.
pt = 0;  // Affecte le pointeur null à 'pt'.
// Les deux lignes compilent sans avertissement.

// nullptr est supposé régler un peu ce problème :
int* pt2 = new int;
*pt2 = nullptr; // Ne compile pas
pt2 = nullptr;  // Affecte null à pt2

// Il y a une exception faite pour les booléens.
// Ceci vous permet de tester les pointeurs null avec if(!ptr),
// mais par conséquent, vous pouvez assigner nullptr à un booléen directement !
*pt = nullptr;  // Ceci compile toujours, même si '*pt' est un booléen !

// '=' != '=' != '='!
// Appelle Foo::Foo(const Foo&) ou une variante du (voir sémantiques de mouvement)
// constructeur par copie.
Foo f2;
Foo f1 = f2;

// Appelle Foo::Foo(const Foo&) ou une variante, mais copie seulement la partie
// 'Foo' de 'fooSub'. Tout membre extra de 'fooSub' est ignoré.
// Ce comportement parfois horrifiant est appelé "object slicing".
FooSub fooSub;
Foo f1 = fooSub;

// Appelle Foo::operator=(Foo&) ou une variante.
Foo f1;
f1 = f2;

// Comment vraiment nettoyer un conteneur :
class Foo { ... };
vector<Foo> v;
for (int i = 0; i < 10; ++i)
  v.push_back(Foo());

// La ligne suivante affecte la taille de v à 0, mais les destructeurs ne sont
// appelés et les ressources ne sont pas libérées !
v.empty();
// La nouvelle valeur est copiée dans le premier Foo que nous avons inséré
v.push_back(Foo());

// Ceci nettoie toutes les valeurs de v. Voir la section à propos des objets
// temporaires pour comprendre pourquoi cela fonctionne.
v.swap(vector<Foo>());

```
Lecture complémentaire :

Une référence à jour du langage est disponible à
<http://cppreference.com/w/cpp>

Des ressources supplémentaires sont disponibles à <http://cplusplus.com>
---
language: clojure
filename: learnclojure-fr.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Bastien Guerry", "https://github.com/bzg"]
lang: fr-fr
---

Clojure est un langage de la famille des Lisp développé pour la machine
virtuelle Java.  Ce langage insiste beaucoup plus sur la [programmation
fonctionnelle](https://fr.wikipedia.org/wiki/Programmation_fonctionnelle) pure
que Common Lisp, mais comprend plusieurs outils de gestion de la mémoire
transactionnelle
[STM](https://en.wikipedia.org/wiki/Software_transactional_memory) pour gérer
les changements d'états si besoin.

Cette combinaison permet de gérer le parallélisme très simplement, et
souvent de façon automatique.

(Vous avez besoin de Clojure 1.2 ou plus récent pour ce tutoriel.)

```clojure
; Les commentaires commencent avec un point-virgule.

; Clojure est composé de « formes », qui sont simplement des listes
; d'expressions entre parenthèses, séparées par une ou des espaces.
;
; L'interpréteur Clojure suppose que le premier élément est une fonction
; ou une macro, et que le reste contient des arguments.

; Le premier appel dans un fichier doit être ns, pour définir
; l'espace de nom
(ns learnclojure)

; D'autres d'exemples basiques:

; str va créer une chaîne de caractères à partir de tous ses arguments
(str "Hello" " " "World") ; => "Hello World"

; Les opérations mathématiques sont simples
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; L'égalité est =
(= 1 1) ; => true
(= 2 1) ; => false

; Vous avez aussi besoin de not pour la négation logique
(not true) ; => false

; Les formes imbriquées fonctionnent comme on s'y attend
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; Types
;;;;;;;;;;;;;

; Clojure utilise les types d'objets Java pour les booléens, les chaînes de
; caractères et les nombres.
; Utilisez `class` pour inspecter les types.
(class 1) ; Les nombres entiers littéraux sont java.lang.Long par défaut
(class 1.); Les flottants littéraux sont java.lang.Double
(class ""); Les chaînes sont toujours entourées de guillemets doubles, et sont java.lang.String
(class false) ; Les booléens sont java.lang.Boolean
(class nil); La valeur "null" est appelée nil

; Si vous voulez créer une liste littérale de données, utilisez ' pour en
; empêcher son évaluation
'(+ 1 2) ; => (+ 1 2)
; (qui est un raccourci pour (quote (+ 1 2)))

; Vous pouvez évaluer une liste "quotée":
(eval '(+ 1 2)) ; => 3

; Collections & séquences
;;;;;;;;;;;;;;;;;;;;;;;;;

; Les listes sont des structures de données en listes chaînées, alors que les
; vecteurs reposent sur des tableaux.
; Les vecteurs et les listes sont des classes Java aussi !
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; Une liste serait écrite comme (1 2 3), mais nous devons la quoter
; pour empêcher l'interpréteur de penser que c'est une fonction.
; Et (list 1 2 3) est la même chose que '(1 2 3)

; Les "Collections" sont juste des groupes de données
; Les listes et les vecteurs sont tous deux des collections:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; Les "séquences" (seqs) sont des abstractions à partir de listes de données.
; Seules les listes sont elles-mêmes des séquences.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; Une séquence n'a besoin de fournir une entrée que lorsqu'on y accède.
; Donc, les séquences peuvent être "lazy" -- et définir une série infinie:
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (une série infinie)
(take 4 (range)) ;  (0 1 2 3)

; Utilisez cons pour ajouter un item au début d'une liste ou d'un vecteur
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; Conj ajoutera un item à une collection de la manière la plus efficace
; Pour les listes, conj ajoute l'item au début; pour les vecteurs, à la fin.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; Utilisez concat pour ajouter des listes ou vecteurs:
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; Utilisez filter, map pour interagir avec des collections
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; Utilisez reduce pour les réduire
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; Reduce peut aussi prendre un argument pour la valeur initiale
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; Fonctions
;;;;;;;;;;;;;;;;;;;;;

; Utilisez fn pour créer de nouvelles fonctions.
; Une fonction renvoie toujours sa dernière expression.
(fn [] "Hello World") ; => fn

; (Vous devez ajouter des parenthèses pour l'appeler)
((fn [] "Hello World")) ; => "Hello World"

; Vous pouvez créer une variable en utilisant def
(def x 1)
x ; => 1

; Assignez une fonction à une variable
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; Vous pouvez raccourcir le procédé en utilisant defn
(defn hello-world [] "Hello World")

; [] contient la liste des arguments de la fonction
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; Vous pouvez aussi utiliser ce raccourci pour créer des fonctions
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; Vous pouvez avoir des fonctions multi-variadiques
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; Les fonctions peuvent inclure des arguments supplémentaires dans une séquence
(defn count-args [& args]
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "Vous avez passé 3 args: (1 2 3)"

; Vous pouvez combiner les arguments normaux et supplémentaires
(defn hello-count [name & args]
  (str "Hello " name ", vous avez passé " (count args) " args supplémentaires"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, vous avez passé 3 args supplémentaires"


; Maps
;;;;;;;;;;;;;;;

; Les hashmaps et les arraymaps partagent une interface. Les hashmaps
; sont interrogés plus rapidement mais ne retiennent pas l'ordre des clefs.
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Les array maps deviennent automatiquement des hashmaps pour la
; plupart des opérations si elles deviennent assez larges, donc vous
; n'avez pas à vous en faire.

; Tous les types "hashables" sont acceptés comme clefs, mais en
; général on utilise des mots-clefs ("keywords")
; Les mots-clefs sont comme les chaînes de caractères mais en plus efficaces
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; Au passage, les virgules sont toujours traitées comme des espaces et
; ne font rien.

; Sélectionnez une valeur dans une map en l'appelant comme fonction
(stringmap "a") ; => 1
(keymap :a) ; => 1

; Les mots-clefs peuvent aussi être utilisés pour sélectionner leur
; valeur dans une map !
(:b keymap) ; => 2

; N'essayez pas ça avec les chaînes de caractères
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; Sélectionner une clef absente renvoie nil
(stringmap "d") ; => nil

; Use assoc to add new keys to hash-maps
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; Mais souvenez-vous, les types en Clojure sont immuables !
keymap ; => {:a 1, :b 2, :c 3}

; Utilisez dissoc pour retirer des clefs
(dissoc keymap :a :b) ; => {:c 3}

; Ensembles
;;;;;;;;;;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; Ajoutez un élément avec conj
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; Retirez-en un avec disj
(disj #{1 2 3} 1) ; => #{2 3}

; Testez la présence en utilisant l'ensemble comme une fonction
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; Il y a encore d'autres fonctions dans l'espace de nom clojure.sets.

; Formes et macros utiles
;;;;;;;;;;;;;;;

; Les constructions logiques en Clojure sont juste des macros, et
ressemblent à toutes les autres formes:
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; Utilisez let pour créer des assignations temporaires
(let [a 1 b 2]
  (> a b)) ; => false

; Groupez les énoncés ensemble avec do
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; Les fonctions ont un do implicit
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; De même pour let
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")

; Utilisez les Threading Macros (-> et ->>) pour exprimer plus
; clairement vos transformations, en y pensant de manière multi-niveaux.

; La "flèche simple" ou "Thread-first", insère, à chaque niveau
; de la transformation, la forme courante en la seconde position
; de la forme suivante, constituant à chaque fois un nouvel étage
; de transformation. Par exemple:
(->  
   {:a 1 :b 2}
   (assoc :c 3) ;=> Génère ici (assoc {:a 1 :b 2} :c 3)
   (dissoc :b)) ;=> Génère ici (dissoc (assoc {:a 1 :b 2} :c 3) :b)

; Cette expression est ré-écrite en:
; (dissoc (assoc {:a 1 :b 2} :c 3) :b)
; et est évaluée en : {:a 1 :c 3}

; La "flèche double" ou "Thread-last" procède de la même manière
; que "->", mais insère le résultat de la réécriture de chaque
; étage en dernière position. Par exemple:
(->>
   (range 10)
   (map inc)     ;=> Génère ici (map inc (range 10)
   (filter odd?) ;=> Génère ici (filter odd? (map inc (range 10))
   (into []))    ;=> Génère ici (into [] (filter odd? (map inc (range 10))), ce qui est évalué au final à;
                 ; [1 3 5 7 9]

; Quand vous êtes dans une situation où vous voulez plus de liberté pour choisir 
; où mettre le résultat des étages précédents, vous pouvez utiliser la
; macro as->. Avec cette macro, donnez un nom spécifique au résultat de la transformation
; précédente pour le placer, à votre guise, où bon vous semble dans l'étage courant:
(as-> [1 2 3] input
  (map inc input);=> Utilisation du résultat en dernière position
  (nth input 4) ;=> et en deuxième position, dans la même expression
  (conj [4 5 6] input [8 9 10])) ;=> ou au milieu !

; Modules
;;;;;;;;;;;;;;;

; Utilisez "use" pour obtenir toutes les fonctions d'un module
(use 'clojure.set)

; Maintenant nous pouvons utiliser les opération de set
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; Vous pouvez aussi choisir un sous-ensemble de fonctions à importer
(use '[clojure.set :only [intersection]])

; Utilisez require pour importer un module
(require 'clojure.string)

; Utilisez / pour appeler les fonctions d'un module
; Ici, le module est clojure.string et la fonction est blank?
(clojure.string/blank? "") ; => true

; Vous pouvez associer un nom plus court au module au moment de l'importer
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (#"" dénote une expression régulière)

; Vous pouvez utiliser require (et use, mais ne le faites pas) en
; appelant :require depuis un espace de noms.
; Dans ce cas-là, vous n'avez pas besoin de "quoter" vos modules:
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java a une librairie standard énorme, donc vous voudrez apprendre à
; vous familiariser avec.

; Utilisez import pour charger un module java
(import java.util.Date)

; Vous pouvez importer depuis un ns aussi.
(ns test
  (:import java.util.Date
           java.util.Calendar))

; Utilisez les noms de classes avec "." à la fin pour créer une instance
(Date.) ; <un objet date>

; Utilisez . pour invoquer des méthodes. Ou utilisez le raccourci ".method"
(. (Date.) getTime) ; <un timestamp>
(.getTime (Date.)) ; exactement la même chose

; Utilisez / pour appeler des méthodes statiques
(System/currentTimeMillis) ; <un timestamp> (system est toujours présent)

; Utilisez doto to rendre plus tolérable l'interaction avec des
; classes (mutables)
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => Une classe Date. définie comme 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; La mémoire logiciel transactionnelle ("Software Transactional Memory")
; est le mécanisme que Clojure utilise pour gérer les états persistents.
; Il y a plusieurs formes en Clojure qui utilisent cela.

; L'atome est la plus simple. Passez-lui une valeur initiale
(def my-atom (atom {}))

; Mettez à jour un atome avec swap!.
; swap! prend une fonction en argument et l'appelle avec la valeur
; actuelle de l'atome comme premier argument, et les autres arguments
; comme second argument.
(swap! my-atom assoc :a 1) ; Définit my-atom comme le résultat de (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Définit my-atom comme le résultat de (assoc {:a 1} :b 2)

; Use '@' to dereference the atom and get the value
my-atom  ;=> Atom<#...> (Renvoie l'objet Atom)
@my-atom ; => {:a 1 :b 2}

; Voici un simple compteur utilisant un atome
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; Les autres formes STM sont les refs et les agents.
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
```

### Lectures complémentaires

C'est loin d'être exhaustif, mais assez pour vous permettre de continuer.

Clojure.org propose de nombreux articles:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org a de la documentation avec des exemples pour la
plupart des fonctions principales :
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure est une super manière d'augmenter vos compétences en Clojure et
en programmation fonctionnelle :
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org a pas mal d'article pour débuter :
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
translators:
  - ["Geoffrey Roguelon", "https://github.com/GRoguelon"]
lang: fr-fr
filename: coffeescript-fr.coffee
---

``` coffeescript
# CoffeeScript est un langage préprocesseur, il permet de générer du Javascript.
# Il suit les tendances de certains langages récents.
# Par exemple, les commentaires se définissent comme en Ruby ou en Python.

###
Ceci est un bloc de commentaires
il est converti directement avec '/ *' et '* /'
pour correspondre aux commentaires Javascript

Vous devez comprendre la syntaxe du langage JavaScript pour continuer.
###

# Affectation :
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# Structures de contrôle :
number = -42 if opposite #=> if(opposite) { number = -42; }

# Fonctions :
square = (x) -> x * x #=> var square = function(x) { return x * x; }

# Intervals :
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# Objets :
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#  "root": Math.sqrt,
#  "square": square,
#  "cube": function(x) { return x * square(x); }
#}

# Liste d'arguments variables :
race = (winner, runners...) ->
  print winner, runners

# Existance :
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# Lecture d'un tableau :
cubes = (math.cube num for num in list) #=> ...
```
---
language: crystal
filename: learncrystal-fr.cr
contributors:
    - ["Vitalii Elenhaupt", "http://veelenga.com"]
    - ["Arnaud Fernandés", "https://github.com/TechMagister/"]
translators:
    - ["Arnaud Fernandés", "http://github.com/TechMagister/"]
lang: fr-fr
---

```crystal

# Ceci est un commentaire

# Tout est objet
nil.class  #=> Nil
100.class  #=> Int32
true.class #=> Bool

# Les valeurs fausses sont : nil, false et les pointeurs null
!nil   #=> true  : Bool
!false #=> true  : Bool
!0     #=> false : Bool

# Entiers

1.class #=> Int32

# Quatre types d'entiers signés
1_i8.class  #=> Int8
1_i16.class #=> Int16
1_i32.class #=> Int32
1_i64.class #=> Int64

# Quatre types d'entiers non signés
1_u8.class  #=> UInt8
1_u16.class #=> UInt16
1_u32.class #=> UInt32
1_u64.class #=> UInt64

2147483648.class          #=> Int64
9223372036854775808.class #=> UInt64

# Nombre en base binaire
0b1101 #=> 13 : Int32

# Nombre en base octale
0o123 #=> 83 : Int32

# Nombres hexadécimaux
0xFE012D #=> 16646445 : Int32
0xfe012d #=> 16646445 : Int32

# Nombres à virgule

1.0.class #=> Float64

# Il y a deux types de nombres à virgule
1.0_f32.class #=> Float32
1_f32.class   #=> Float32

1e10.class    #=> Float64
1.5e10.class  #=> Float64
1.5e-7.class  #=> Float64

# Caractères

'a'.class #=> Char

# Notation octale des caractères
'\101' #=> 'A' : Char

# Notation unicode
'\u0041' #=> 'A' : Char

# Chaînes de caratères

"s".class #=> String

# Les chaînes de caractères sont immuables
s = "hello, "  #=> "hello, "        : String
s.object_id    #=> 134667712        : UInt64
s += "Crystal" #=> "hello, Crystal" : String
s.object_id    #=> 142528472        : UInt64

# Interpolation
"sum = #{1 + 2}" #=> "sum = 3" : String

# Chaînes multilignes
"Ceci est une chaine sur
plusieurs lignes"

# Une autre notation pour les chaînes de caratères
# qui permet d'insérer des guillemets
%(hello "world") #=> "hello \"world\""

# Symboles
# Ils sont immuables et réutilisables, ils sont représentés en interne par
# un Int32. Ils sont souvent utilisés à la place des chaînes de caractères
# quand l'identité est plus importante que le contenu

:symbol.class #=> Symbol

sentence = :question?     # :"question?" : Symbol

sentence == :question?    #=> true  : Bool
sentence == :exclamation! #=> false : Bool
sentence == "question?"   #=> false : Bool

# Tableaux

[1, 2, 3].class         #=> Array(Int32)
[1, "hello", 'x'].class #=> Array(Int32 | String | Char)

# Un type doit être spécifié pour les tableaux vides
[]               # Syntax error: for empty arrays use '[] of ElementType'
[] of Int32      #=> [] : Array(Int32)
Array(Int32).new #=> [] : Array(Int32)

# Les tableaux peuvent être indexés
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5] : Array(Int32)
array[0]                #=> 1               : Int32
array[10]               # lève l'exception IndexError
array[-6]               # lève l'exception IndexError
array[10]?              #=> nil             : (Int32 | Nil)
array[-6]?              #=> nil             : (Int32 | Nil)

# À partir de la fin
array[-1] #=> 5

# Avec un index de début et la taille
array[2, 3] #=> [3, 4, 5]

# Ou avec un intervalle
array[1..3] #=> [2, 3, 4]

# L'ajout à un tableau peut se faire avec l'opérateur <<
array << 6  #=> [1, 2, 3, 4, 5, 6]

# Enlève la dernière entrée
array.pop #=> 6
array     #=> [1, 2, 3, 4, 5]

# Enlève la première entrée
array.shift #=> 1
array       #=> [2, 3, 4, 5]

# Vérifie si un élément est présent dans le tableau
array.includes? 3 #=> true

# Syntaxe spéciale pour un tableau de chaîne de caractères ou de symboles
%w(one two three) #=> ["one", "two", "three"] : Array(String)
%i(one two three) #=> [:one, :two, :three]    : Array(Symbol)

# Il y a une syntaxe spéciale pour les tableaux et autres types
# du moment qu'ils définissent une méthode .new et #<<
set = Set{1, 2, 3} #=> [1, 2, 3]
set.class          #=> Set(Int32)

# Ce qui est ci dessus est équivalent à :
set = Set(typeof(1, 2, 3)).new
set << 1
set << 2
set << 3

# Tableaux associatifs

{1 => 2, 3 => 4}.class   #=> Hash(Int32, Int32)
{1 => 2, 'a' => 3}.class #=> Hash(Int32 | Char, Int32)

# Un type doit être spécifié pour les tableaux associatifs vides
{}                     # Syntax error
{} of Int32 => Int32   # {}
Hash(Int32, Int32).new # {}

# L'accès se fait via une clé
hash = {"color" => "green", "number" => 5}
hash["color"]        #=> "green"
hash["no_such_key"]  #=> Missing hash key: "no_such_key" (KeyError)
hash["no_such_key"]? #=> nil

# Vérifie l'existence d'une clé
hash.has_key? "color" #=> true

# Notation spéciale pour les clés si ce sont des symboles
# ou chaînes de caractères
{key1: 'a', key2: 'b'}     # {:key1 => 'a', :key2 => 'b'}
{"key1": 'a', "key2": 'b'} # {"key1" => 'a', "key2" => 'b'}

# De même que pour les tableaux simples, une syntaxe spéciale
# existe du moment que le type définit une méthode .new et #[]=
class MyType
  def []=(key, value)
    puts "do stuff"
  end
end

MyType{"foo" => "bar"}

# Ce qui est équivalent à :
tmp = MyType.new
tmp["foo"] = "bar"
tmp

# Intervalle

1..10                  #=> Range(Int32, Int32)
Range.new(1, 10).class #=> Range(Int32, Int32)

# Ils peuvent être inclusifs ou exclusifs
(3..5).to_a  #=> [3, 4, 5]
(3...5).to_a #=> [3, 4]

# Vérifie si un intervalle contient une valeur
(1..8).includes? 2 #=> true

# les tuples sont fixés en taille, immuables et alloués sur la pile
{1, "hello", 'x'}.class #=> Tuple(Int32, String, Char)

# L'accès peut se faire en utilisant un index
tuple = {:key1, :key2}
tuple[1] #=> :key2
tuple[2] #=> syntax error : Index out of bound

# Ils peuvent être scindés en plusieurs variables
a, b, c = {:a, 'b', "c"}
a #=> :a
b #=> 'b'
c #=> "c"

# Les procédures ( Proc ) sont des pointeurs de fonction
# avec un contexte optionel. Ils sont généralement créés avec
# cette notation :
proc = ->(x : Int32) { x.to_s }
proc.class # Proc(Int32, String)
# Ou en utilisant la méthode new
Proc(Int32, String).new { |x| x.to_s }

# On les invoque avec la méthode call
proc.call 10 #=> "10"

# Contrôle de flux

if true
  "if statement"
elsif false
  "else-if, optional"
else
  "else, also optional"
end

puts "if as a suffix" if true

# Le si ( if ) peut être utilisé pour une déclaration
a = if 2 > 1
      3
    else
      4
    end

a #=> 3

# Opérateur ternaire
a = 1 > 2 ? 3 : 4 #=> 4

# Aiguillage à l'aide du mot clé "case"
cmd = "move"

action = case cmd
  when "create"
    "Creating..."
  when "copy"
    "Copying..."
  when "move"
    "Moving..."
  when "delete"
    "Deleting..."
end

action #=> "Moving..."

# Boucle
index = 0
while index <= 3
  puts "Index: #{index}"
  index += 1
end
# Index: 0
# Index: 1
# Index: 2
# Index: 3

index = 0
until index > 3
  puts "Index: #{index}"
  index += 1
end
# Index: 0
# Index: 1
# Index: 2
# Index: 3

# Mais il est préférable d'utiliser #each
(1..3).each do |index|
  puts "Index: #{index}"
end
# Index: 0
# Index: 1
# Index: 2
# Index: 3

# Le type d'une variable dépend du type de l'expression
# dans la déclaration du if
if a < 3
  a = "hello"
else
  a = true
end
typeof a #=> (Bool | String)

if a && b
  # ici a et b ne sont pas null
end

if a.is_a? String
  a.class #=> String
end

# Fonctions

def double(x)
  x * 2
end

# Les fonctions et tous les blocs retournent la valeur de la dernière évaluation
double(2) #=> 4

# Les parenthèses sont optionnelle quand l'appel n'est pas ambigü
double 3 #=> 6

double double 3 #=> 12

def sum(x, y)
  x + y
end

# Les arguments sont séparés par une virgule
sum 3, 4 #=> 7

sum sum(3, 4), 5 #=> 12

# yield
# Toutes les méthodes on un paramètre optionel et implicite de type bloc
# il peut être appelé avec le mot clé 'yield'
def surround
  puts '{'
  yield
  puts '}'
end

surround { puts "hello world" }

# {
# hello world
# }

# Un bloc peut être passé à une fonction
# Le "&" marque une référence à un bloc
def guests(&block)
  block.call "some_argument"
end

# Une liste d'arguments peut être donnée, qui sera convertie en tableau
# Pour cela, utilisez l'opérateur "*"
def guests(*array)
  array.each { |guest| puts guest }
end

# Si une méthode retourne un tableau, il peut être scindé
def foods
    ["pancake", "sandwich", "quesadilla"]
end
breakfast, lunch, dinner = foods
breakfast #=> "pancake"
dinner    #=> "quesadilla"

# Par convention, toutes les méthodes qui retournent un booléen
# finissent par un point d'interrogation
5.even? # false
5.odd?  # true

# Si une méthode finit avec un point d'exclamation, c'est qu'elle fait une
# opération destructrice. Quelques méthodes ont une version "!" pour faire
# des changements et une version non-"!" pour retourner une nouvelle version
company_name = "Dunder Mifflin"
company_name.gsub "Dunder", "Donald"  #=> "Donald Mifflin"
company_name  #=> "Dunder Mifflin"
company_name.gsub! "Dunder", "Donald"
company_name  #=> "Donald Mifflin"


# Les classes se définissent avec le mot clé "class"
class Human

  # Une variable de classe, partagée par toutes les instances
  @@species = "H. sapiens"

  # "name" est une chaine de caratère ( String )
  @name : String

  # Constructeur basique, assigne l'argument à la variable "name"
  # si l'age n'est pas donné, sa valeur sera de 0
  def initialize(@name, @age = 0)
  end

  # Mutateur
  def name=(name)
    @name = name
  end

  # Accesseur
  def name
    @name
  end

  # La macro "property" va générer les deux précédentes méthodes
  property :name

  # Les accesseurs/mutateurs peuvent aussi être créés individuellement
  getter :name
  setter :name

  # Une méthode de classe utilise "self" pour se distinguer d'une
  # méthode d'instance. Elle ne peut être appelée qu'à partir de la classe
  def self.say(msg)
    puts msg
  end

  def species
    @@species
  end
end


# Instantie une classe
jim = Human.new("Jim Halpert")

dwight = Human.new("Dwight K. Schrute")

# Appelons quelques méthodes
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# Appel de la méthode de classe
Human.say("Hi") #=> Affiche "Hi" et retourne nil

# Les variables qui commencent par @ ont une portée d'instance
class TestClass
  @var = "Je suis une variable d'instance"
end

# Les variables qui commencent par @@ ont une portée de classe
class TestClass
  @@var = "Je suis une variable de classe"
end
# Les constantes commencent par une lettre majuscule
Var = "Je suis constante"
Var = "impossible" # Already initialized constant Var

# La classe est aussi un objet
# Les variables de classe sont partagées avec les descendants

# Classe de base
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

# Classe dérivée
class Worker < Human
end

Human.foo   #=> 0
Worker.foo  #=> 0

Human.foo = 2 #=> 2
Worker.foo    #=> 0

Worker.foo = 3 #=> 3
Human.foo   #=> 2
Worker.foo  #=> 3

module ModuleExample
  def foo
    "foo"
  end
end

# Inclure (include) des modules ajoute leurs méthodes aux instances
# Étendre (extend) ajoute les méthodes à la classe

class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => undefined method 'foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => undefined method 'foo' for Book


# Gestion des exceptions

# Définit un type d'exeption
class MyException < Exception
end

# Définit une autre exception
class MyAnotherException < Exception; end

ex = begin
   raise MyException.new
rescue ex1 : IndexError
  "ex1"
rescue ex2 : MyException | MyAnotherException
  "ex2"
rescue ex3 : Exception
  "ex3"
rescue ex4 # attrape toutes les autres exceptions
  "ex4"
end

ex #=> "ex2"

```

## Ressources additionnelles

- [Documentation Officielle (EN)](http://crystal-lang.org/)
---
language: c#
contributors:
    - ["Irfan Charania", "https://github.com/irfancharania"]
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Melvyn Laïly", "http://x2a.yt"]
    - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
translators:
    - ["Olivier Hoarau", "https://github.com/Olwaro"]
filename: LearnCSharp-fr.cs
lang: fr-fr
---

C# est un langage de programmation orienté objet à typage fort qui permet aux développeurs de créer une grande variété d'applications fiables et robustes s'appuyant sur le framework .NET.

[Plus d'infos](http://msdn.microsoft.com/fr-fr/library/67ef8sbd.aspx)

```c#
// Les commentaires sur une seule ligne commencent par //
/*
Les
commentaires
multi-lignes
ressemblent
à
ceci
*/
/// <summary>
/// Ceci est un commentaire de documentation XML
/// </summary>

// Importez des namespaces avec l'instruction 'using'
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;

// Définit la portée du code pour une meilleure organisation
namespace Learning
{
    // Chaque fichier .cs devrait au moins contenir une classe avec le même nom 
    // que celui du fichier. Ce n'est pas une obligation mais c'est mieux ! 
    public class LearnCSharp
    {
        // LES BASES - si vous avez déjà de l'expérience en Java ou C++
        // passez directement à la partie FONCTIONNALITÉS INTERÉSSANTES 
        public static void Syntax() 
        {
            // Utilisez Console.WriteLine pour écrire sur la sortie
            Console.WriteLine("Hello World");
            Console.WriteLine(
                "Entier: " + 10 +
                " Double: " + 3.14 +
                " Booleen: " + true);

            // Pour omettre le retour à la ligne : Console.Write
            Console.Write("Hello ");
            Console.Write("World");

            ///////////////////////////////////////////////////
            // Types et Variables
            // Déclarez une variable avec la syntaxe <type> <nom>
            ///////////////////////////////////////////////////

            // Sbyte - Entier signé sur 8 bits
            // (-128 <= sbyte <= 127)
            sbyte fooSbyte = 100;

            // Byte - Entier non-signé sur 8 bits
            // (0 <= byte <= 255)
            byte fooByte = 100;

            // Short - Entier sur 16 bits
            // Signé - (-32,768 <= short <= 32,767)
            // Non-signé - (0 <= ushort <= 65,535)
            short fooShort = 10000;
            ushort fooUshort = 10000;

            // Int - Entier sur 32 bits
            int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647)
            uint fooUint = 1; // (0 <= uint <= 4,294,967,295)

            // Long - Entier sur 64 bits
            long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
            ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
            // Par défaut le type d'un littéral entier est int ou uint
            // on ajoute 'L' pour spécifier la création d'un long

            // Double - Réel sur 64 bits en virgule flottante (norme IEEE 754) 
            double fooDouble = 123.4; // Precision : 15-16 chiffres

            // Float - Réel sur 32 bits en virgule flottante (norme IEEE 754) 
            float fooFloat = 234.5f; // Precision : 7 chiffres
            // Par défaut le type d'un littéral réel est double
            // on ajoute 'f' pour spécifier la création d'un float

            // Decimal - Type de donnée numérique sur 128 bits, fournit une plus 
            // grande précision et une plage de valeurs réduite.
            // Approprié aux calculs financiers et monétaires
            decimal fooDecimal = 150.3m;

            // Booléen - vrai / faux
            bool fooBoolean = true; // ou false

            // Char - Un unique caractère Unicode sur 16 bits
            char fooChar = 'A';

            // String -- contrairement aux types précédents qui sont des types valeurs,
            // string est un type référence. Il peut donc avoir la valeur null
            string fooString = "\"échappement\" de guillemets et ajout de \n (nouvelle ligne) et  de \t (tabulation)";
            Console.WriteLine(fooString);

            // Il est possible d'accéder à chaque caractère d'une chaîne de caractères via son index
            char charFromString = fooString[1]; // 'é'
            // une chaîne de caractères est immuable : impossible de faire fooString[1] = 'X';

            // Comparaison de chaînes de caractères avec la culture courrante en ignorant la casse
            string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);

            // Formatage
            string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);

            // Dates et formatage
            DateTime fooDate = DateTime.Now;
            Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));

            // Il est possible d'étaler une chaîne de caractères sur plusieurs lignes avec le symbole @.
            // Pour échapper " utilisez ""
            string bazString = @"Voici quelques trucs
sur une nouvelle ligne! ""Wow!"", quel style";

            // Utilisez const ou read-only pour rendre une variable immuable.
            // Les valeurs constantes sont calculées au moment de la compilation
            const int HOURS_I_WORK_PER_WEEK = 9001;

            ///////////////////////////////////////////////////
            // Structures de données
            ///////////////////////////////////////////////////

            // Tableaux - indexé à partir de zéro
            // La taille d'un tableau doit être décidée à la déclaration
            // La syntaxe pour déclarer un tableau est la suivante :
            // <type>[] <nom> = new <type>[<taille>]
            int[] intArray = new int[10];

            // Une autre méthode de déclaration et d'initialisation
            int[] y = { 9000, 1000, 1337 };

            // Indexer un tableau - Accéder à un élément
            Console.WriteLine("intArray à 0: " + intArray[0]);
            // Les tableaux sont muables.
            intArray[1] = 1;

            // Listes
            // Elles sont plus souvent utilisées que les tableaux car plus souples
            // La syntaxe pour déclarer une liste est la suivante :
            // List<type> <nom> = new List<type>();
            List<int> intList = new List<int>();
            List<string> stringList = new List<string>();
            List<int> z = new List<int> { 9000, 1000, 1337 }; // intialisation
            // Les <> indiquent un type générique 
            // Pus d'info dans la partie FONCTIONNALITÉS INTERÉSSANTES

            // Les éléments d'une liste ne sont pas null par défaut
            // Il faut ajouter une valeur avant d'y accéder par index
            intList.Add(1);
            Console.WriteLine("intList à 0: " + intList[0]);

            // Autres structures de données à étudier :
            // Stack/Queue (Pile/File)
            // Dictionary (une implémentation de hash map)
            // HashSet (représente un ensemble)
            // Collections en lecture seule
            // Tuple (.Net 4+)

            ///////////////////////////////////////
            // Opérateurs
            ///////////////////////////////////////
            Console.WriteLine("\n->Opérateurs");

            int i1 = 1, i2 = 2; // Raccourci pour des déclarations multiples

            // Arithmétique classique
            Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3

            // Modulo
            Console.WriteLine("11%3 = " + (11 % 3)); // => 2

            // Opérateurs de comparaison
            Console.WriteLine("3 == 2? " + (3 == 2)); // => False
            Console.WriteLine("3 != 2? " + (3 != 2)); // => True
            Console.WriteLine("3 > 2? " + (3 > 2)); // => True
            Console.WriteLine("3 < 2? " + (3 < 2)); // => False
            Console.WriteLine("2 <= 2? " + (2 <= 2)); // => True
            Console.WriteLine("2 >= 2? " + (2 >= 2)); // => True

            // Opérateurs bit à bit !
            /*
            ~       Compément unaire
            <<      Décalage à gauche
            >>      Décalage à droite
            &       ET logique
            ^       OU exclusif
            |       OU inclusif
            */

            // Incrémentations
            int i = 0;
            Console.WriteLine("\n->Inc/Dec-rementation");
            Console.WriteLine(i++); //i = 1. Post-Incrémentation
            Console.WriteLine(++i); //i = 2. Pre-Incrémentation
            Console.WriteLine(i--); //i = 1. Post-Decrémentation
            Console.WriteLine(--i); //i = 0. Pre-Decrémentation

            ///////////////////////////////////////
            // Structures de contrôle
            ///////////////////////////////////////
            Console.WriteLine("\n->Structures de contrôle");

            // Structure conditionnelle
            int j = 10;
            if (j == 10)
            {
                Console.WriteLine("Je serai affiché");
            }
            else if (j > 10)
            {
                Console.WriteLine("Pas moi");
            }
            else
            {
                Console.WriteLine("Moi non plus");
            }

            // Opérateur ternaire
            // Un simple if/else peut s'écrire :
            // <condition> ? <valeur si true> : <valeur si false>
            int toCompare = 17;
            string isTrue = toCompare == 17 ? "True" : "False";

            // Boucle while
            int fooWhile = 0;
            while (fooWhile < 100)
            {
                // 100 passages, de 0 à 99
                fooWhile++;
            }

            // Boucle Do While
            int fooDoWhile = 0;
            do
            {
                // 100 passages, de 0 à 99
                fooDoWhile++;
            } while (fooDoWhile < 100);

            // Boucle for 
            // Structure : for(<etat_initial>; <condition>; <pas>)
            for (int fooFor = 0; fooFor < 10; fooFor++)
            {
                // 10 passages, de 0 à 9
            }

            // La boucle foreach
            // Structure : foreach(<type_iterateur> <nom_iterateur> in <enumerable>)
            // Cette boucle est utilisable sur des objets implémentant IEnumerable ou IEnumerable<T>
            // Toutes les collections du framework .NET (Tableaux, Listes, ...) implémentent ces interfaces.
            // (Notez que dans l'exemple suivant .ToCharArray() peut être omit car
            //  string implémente IEnumerable)
            foreach (char character in "Hello World".ToCharArray())
            {
                //Itération sur chaque caractère
            }

            // La structure Switch Case
            // Un switch fonctionne avec les types : byte, short, char et int.
            // Les enums sont aussi supportés ainsi que les chaînes de caractères et quelques
            // classes spéciales basées sur les types primitifs : Character, Byte, Short et Integer.
            int mois = 3;
            string moisString;
            switch (mois)
            {
                case 1:
                    moisString = "Janvier";
                    break;
                case 2:
                    moisString = "Février";
                    break;
                case 3:
                    moisString = "Mars";
                    break;

                // Vous pouvez assigner plus d'un 'case' à une action
                // Mais vous ne pouvez pas ajouter une action sans 'break' avant un 'case'
                // (pour ce faire, il faudrait ajouter explicitement un 'goto case x')
                case 6:
                case 7:
                case 8:
                    moisString = "C'est l'été!";
                    break;
                default:
                    moisString = "Un autre mois oO";
                    break;
            }

            ///////////////////////////////////////
            // conversion de type de donnée et transtypage
            ///////////////////////////////////////

            // conversion de string vers int
            // lève une exception en cas d'erreur
            int.Parse("123"); //retourne la valeur entière de "123"

            // TryParse affecte la valeur par défaut du type en cas d'erreur
            // dans ce cas : 0
            int tryInt;
            if (int.TryParse("123", out tryInt)) // La fonction retourne un booléen
                Console.WriteLine(tryInt);       // => 123

            // conversion d'un entier vers une chaîne de caractères
            // La classe Convert possède plusieurs méthodes pour faciliter la conversion
            Convert.ToString(123);
            // ou
            tryInt.ToString();
        }

        ///////////////////////////////////////
        // CLASSES - voir les définitions à la fin du fichier
        ///////////////////////////////////////

        public static void Classes()
        {
            // voir les déclarations à la fin du fichier

            // Utilisez 'new' pour instancier une classe
            Bicycle trek = new Bicycle();

            // Appel des méthodes de l'objet
            trek.SpeedUp(3); // Il est toujours bon d'utiliser des accesseurs
            trek.Cadence = 100;

            // Affichage de la valeur de retour d'une méthode.
            Console.WriteLine("trek info: " + trek.Info());

            // Instanciation d'un nouveau PennyFarthing
            PennyFarthing funbike = new PennyFarthing(1, 10);
            Console.WriteLine("funbike info: " + funbike.Info());

            Console.Read();
        }

        // POINT D'ENTRÉE - Une application console doit avoir une méthode main comme point d'entrée
        public static void Main(string[] args)
        {
            OtherInterestingFeatures();
        }

        //
        // FONCTIONNALITÉS INTÉRÉSSANTES
        //
        
        // SIGNATURE DE METHODE
        public // Visibilité
        static // Permet un appel direct par la classe (sans instanciation) 
        int // Type de retour,
        MethodSignatures(
            int maxCount, // Premier paramètre, de type int
            int count = 0, // Valeur par défaut si aucun argument n'est passé
            int another = 3,
            params string[] otherParams // Capture tous les arguments passés à la méthode
        )
        { 
            return -1;
        }

        // Des méthodes peuvent avoir le même nom tant que leur signature est unique
        public static void MethodSignature(string maxCount)
        {
        }

        // TYPE GÉNÉRIQUE
        
        // Les types TKey et TValue sont spécifiés par l'utilisateur lors de l'appel de la fonction
        // Cette méthode émule SetDefaut de Python
        public static TValue SetDefault<TKey, TValue>(
            IDictionary<TKey, TValue> dictionary, 
            TKey key, 
            TValue defaultItem)
        {
            TValue result;
            if (!dictionary.TryGetValue(key, out result))
                return dictionary[key] = defaultItem;
            return result;
        }

        // Vous pouvez limiter les types autorisés
        public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
        {
            // Nous sommes sûrs de pouvoir itérer, car T implémente IEnumerable<int>
            foreach (var item in toPrint)
                // Item sera de type int
                Console.WriteLine(item.ToString());
        }

        public static void OtherInterestingFeatures()
        {
            // PARAMÈTERES OPTIONNELS
            MethodSignatures(3, 1, 3, "Des", "Paramètres", "En plus");
            MethodSignatures(3, another: 3); // affectation explicite, les autres 
                                             // paramètres ont la valeur par défaut

            // MÉTHODE D'EXTENSION
            int i = 3;
            i.Print(); // Définit plus bas

            // TYPES NULLABLE - idéal pour les interactions avec une base de données ou pour les valeurs de retour
            // Tous les types valeurs peuvent être rendus nullable en les suffixant par '?'
            // <type>? <nom> = <value>
            int? nullable = null; // raccourci pour Nullable<int>
            Console.WriteLine("Nullable variable: " + nullable);
            bool hasValue = nullable.HasValue; // retourne vrai si la valeur n'est pas null

            // ?? est un sucre syntaxique pour spécifier une valeur par défaut
            // au cas ou une autre valeur serait nulle
            int notNullable = nullable ?? 0; // 0

            // VARIABLES IMPLICITEMENT TYPÉES - vous pouvez laisser le compilateur deviner le type d'une variable
            var magic = "magic est de type string à la compilation. On a toujours un typage fort !";
            // magic = 9; // ne fonctionnera pas car magic est désormais une chaîne de caractères

            // TYPES GÉNÉRIQUES
            var agenda = new Dictionary<string, string>() { 
                {"Sarah", "212 555 5555"} // Ajout d'une entrée à notre agenda
            };

            // Appel de la fonction SetDefault (définie plus haut)
            Console.WriteLine(SetDefault<string,string>(agenda, "Shaun", "Pas de numéro")); // => Pas de numéro
            // Notez que vous n'avez pas à spécifier TKey et TValue car le compilateur saura les inférer.
            Console.WriteLine(SetDefault(agenda, "Sarah", "No Phone")); // => 212 555 5555

            // EXPRESSION LAMBDA - permet d'écrire une fonction en tant qu'expression
            Func<int, int> square = (x) => x * x; // La dernière expression est la valeur de retour
            Console.WriteLine(square(3)); // => 9

            // GESTION AUTOMATIQUE DES RESSOURCES - vous permet de manipuler facilement des resources non-managées
            // La plus part des objets qui accèdent à des ressources non-managées (handle de fichier, périphérique, etc.)
            // implémentent l'interface IDisposable. L'instruction using prend soin
            // de libérer les objets IDisposable proprement à votre place.
            using (StreamWriter writer = new StreamWriter("log.txt"))
            {
                writer.WriteLine("Rien à signaler");
                // À la fin de cette portée les ressources seront libérées.
                // Même si une exception est levée.
            } 

            // BIBLIOTHÈQUE DE TÂCHES PARALLÈLES (TPL)
            // http://msdn.microsoft.com/fr-fr/library/dd460717.aspx
            var websites = new string[] { 
                "http://www.google.com", "http://www.reddit.com", 
                "http://www.shaunmccarthy.com"
            };
            var responses = new Dictionary<string, string>();
            
            // L'exemple suivant exécutera chaque requête dans un thread séparé,
            // et attendra la fin de chacun d'entre eux avant de continuer
            Parallel.ForEach(websites, 
                new ParallelOptions() {MaxDegreeOfParallelism = 3}, // maximum de 3 threads
                website =>
            {
                // Fait quelque chose de long
                using (var r = WebRequest.Create(new Uri(website)).GetResponse())
                {
                    responses[website] = r.ContentType;
                }
            });

            // Ceci ne s'exécutera pas tant que les threads n'auront pas fini leur travail
            foreach (var key in responses.Keys)
                Console.WriteLine("{0}:{1}", key, responses[key]);

            // TYPE DYNAMIQUE - idéal pour travailler avec d'autres langages
            dynamic student = new ExpandoObject();
            student.FirstName = "Mon prénom"; // Pas besoin de définir l'objet

            // Vous pouvez même ajouter des méthodes (dans cet exemple : la méthode prend une chaîne de caractères et retourne une chaîne de caractères)
            student.Introduce = new Func<string, string>(
                (introduceTo) => string.Format("Hey {0}, c'est {1}", student.FirstName, introduceTo));
            Console.WriteLine(student.Introduce("Beth"));

            // IQUERYABLE<T> - quasiment toutes les collections implémentent cette interface
            // ce qui permet d'utiliser des méthodes de style 'Filter' / 'Map' / 'Reduce' 
            var bikes = new List<Bicycle>();
            bikes.Sort(); // Trie le tableau sur place
            bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels)); // Trie en se basant sur la propriété Wheels
            var result = bikes
                .Where(b => b.Wheels > 3) // 'Filter' - enchaînable (retourne un IQueryable du type précédent)
                .Where(b => b.IsBroken && b.HasTassles)
                .Select(b => b.ToString()); // 'Map' - on retourne le .ToString() de chaque élément filtré,
                                            // le résultat est un IQueryable<string>

            var sum = bikes.Sum(b => b.Wheels); // 'Reduce' - fait la somme de tous les Wheels de la liste

            // Creation d'une liste d'objet anonymes basés sur des paramètres de la classe Bike
            var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
            // Le compilateur peut inférer le type de ces objets anonymes, permettant à certains IDE d'effectuer 
            // des autos-complétion.
            foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome))
                Console.WriteLine(bikeSummary.Name);


            // ASPARALLEL
            // C'est ici que les choses se compliquent - un mélange de LINQ et de TPL
            var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
            // La ligne précédente s'exécute en parallèle ! Des threads seront gérés automatiquement
            // et les données y seront réparties. Idéal sur de grosses données (et si votre 
            // machine dispose de plusieurs coeurs)


            // LINQ - lie une source de données à des objets IQueryable<T>
            // ex : LindToSql => liaison avec une base de données, LinqToXml => liaison avec un document xml
            var db = new BikeRespository();

            // l'exécution est décalée, ce qui est préférable quand on travaille sur une base données
            var filter = db.Bikes.Where(b => b.HasTassles); // pas de requête exécutée
            if (42 > 6) //  Vous pouvez continuer à affiner la recherche
                filter = filter.Where(b => b.IsBroken); // pas de requête exécutée

            var query = filter
                .OrderBy(b => b.Wheels)
                .ThenBy(b => b.Name)
                .Select(b => b.Name); // toujours pas de requête exécutée

            // Maintenant la requête est exécutée, mais retourne des données uniquement au fil de l'itération
            foreach (string bike in query) 
                Console.WriteLine(result);
            
        }

    } // Fin de la classe LearnCSharp

    // Il est possible d'inclure plusieurs classes dans un fichier .cs

    public static class Extensions
    {
        // EXTENSION DE FONCTIONS
        public static void Print(this object obj)
        {
            Console.WriteLine(obj.ToString());
        }
    }

    // Syntaxe de déclaration de classe :
    // <public/private/protected/internal> class <class name>{
    //    // champs, constructeurs, fonctions
    //    // tout est déclaré et implémenté à l'intérieur
    // }

    public class Bicycle
    {
        // Propriétés et variable de la classe
        public int Cadence // Public : peut être accédé de partout
        {
            get // get - définit une méthode pour lire la propriété
            {
                return _cadence;
            }
            set // set - définit une méthode pour affecter une valeur à la propriété
            {
                _cadence = value; // 'value' est la valeur passée en argument au setteur
            }
        }
        private int _cadence;

        protected virtual int Gear // Protected : accessible depuis la classe et ses classes filles
        {
            get; // crée une propriété automatique, pas besoin de créer une variable de stockage
            set;
        }

        internal int Wheels // Internal : accessible depuis l'assembly
        {
            get;
            private set; // Il est possible de choisir la portée d'un accesseur
        }

        int _speed; // Par défaut tout est privé au sein d'une classe : accessible uniquement depuis la classe
                    // on peut ajouter explicitement le mot clé 'private'

        public string Name { get; set; }


        // Enum est un type valeur formé par un ensemble de constantes nommées
        // C'est simplement une manière de mettre un nom sur une valeur (int par défaut).
        // Les types compatibles pour un enum sont : byte, sbyte, short, ushort, int, uint, long et ulong.
        // Un enum ne peut pas contenir deux fois la même valeur
        public enum BikeBrand
        {
            AIST,
            BMC,
            Electra = 42, // il est possible de donner explicitement une valeur
            Gitane // 43
        }
        // Nous avons défini cet enum à l'intérieur de la classe Bicycle, c'est donc un type imbriqué
        // Pour le référencer à l'extérieur, il faudra utiliser Bicycle.BikeBrand

        public BikeBrand Brand; // Après avoir déclaré notre type enum, on peut créer un champ de ce type

        // Les membres statiques appartiennent à une classe plutôt qu'à une instance particulière
        // Il est possible d'y accéder sans passer par un objet :
        // ex : Console.WriteLine("Bicycles créés : " + Bicycle.bicyclesCreated);
        static public int BicyclesCreated = 0;

        // Les valeurs en lecture seule sont affectées lors de l'exécution
        // Elles ne peuvent être assignées que lors de leur déclaration ou dans un constructeur
        readonly bool _hasCardsInSpokes = false; // variable en lecture et privée

        // Les constructeurs sont un moyen de créer des objets
        // Voici un constructeur par défaut (pas d'arguments)
        public Bicycle() 
        {
            this.Gear = 1; // accès aux membres de la classe via le mot clé this
            Cadence = 50;  // qui est souvent implicite
            _speed = 5;
            Name = "Bontrager";
            Brand = BikeBrand.AIST;
            BicyclesCreated++;
        }

        // Voici un constructeur spécifique (qui prend des arguments)
        public Bicycle(int startCadence, int startSpeed, int startGear,
                       string name, bool hasCardsInSpokes, BikeBrand brand) 
            : base() // possibilité d'appeler le constructeur de la classe mère (ici Object)
        {
            Gear = startGear; 
            Cadence = startCadence;
            _speed = startSpeed;
            Name = name; 
            _hasCardsInSpokes = hasCardsInSpokes;
            Brand = brand;
        }

        // Les constructeurs peuvent s'enchaîner
        public Bicycle(int startCadence, int startSpeed, BikeBrand brand) :
            this(startCadence, startSpeed, 0, "big wheels", true, brand)
        {
        }

        // Syntaxe de méthode :
        // <public/private/protected> <type de retour> <nom de methode>(<args>)

        // Les classes peuvent implémenter des accesseurs pour leurs champs
        // ou implémenter des propriétés (c'est la méthode dominante en C#)

        // Les paramètres de méthodes peuvent avoir des valeurs par défaut
        // Dans ce cas, la méthode peut être appelée sans arguments
        public void SpeedUp(int increment = 1)
        {
            _speed += increment;
        }

        public void SlowDown(int decrement = 1)
        {
            _speed -= decrement;
        }

        // Les propriétés se chargent de lire/modifier des valeurs
        // elles peuvent être en lecture(get), en écriture(set) ou les deux
        private bool _hasTassles; // variable privée
        public bool HasTassles // propriété publique
        {
            get { return _hasTassles; }
            set { _hasTassles = value; }
        }

        // Il est possible de définir une propriété automatique sur une ligne
        // cette syntaxe créera une variable de stockage automatiquement.
        // Il est possible de modifier l'accèsibilité des getter/setter pour limiter leur utilisation
        public bool IsBroken { get; private set; }

        // La même chose sur plusieurs lignes
        public int FrameSize
        {
            get;
            // Notez que seule la classe Bicycle peut changer la valeur de FrameSize
            private set;
        }

        // Méthode qui affiche la valeur des champs de cet objet
        public virtual string Info()
        {
            return "Gear: " + Gear +
                    " Cadence: " + Cadence +
                    " Speed: " + _speed +
                    " Name: " + Name +
                    " Cards in Spokes: " + (_hasCardsInSpokes ? "yes" : "no") +
                    "\n------------------------------\n"
                    ;
        }

        // Les méthodes peuvent aussi être statiques. Utile pour les méthodes d'aide.
        public static bool DidWeCreateEnoughBycles()
        {
            // À l'intérieur d'une méthode statique on ne peut que référencer des membres statiques !
            return BicyclesCreated > 9000;
        } // Si votre classe n'a que des membres statiques, marquez la comme statique 

    } // fin de la classe Bicycle

    // PennyFarthing est une classe dérivée de Bicycle
    class PennyFarthing : Bicycle
    {
        // Appel au constructeur de la classe mère
        public PennyFarthing(int startCadence, int startSpeed) :
            base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra)
        {
        }

        protected override int Gear
        {
            get
            {
                return 0;
            }
            set
            {
                // Lève une exception
                throw new ArgumentException("Impossible de modifier Gear sur un PennyFarthing");
            }
        }

        public override string Info()
        {
            string result = "PennyFarthing bicycle ";
            result += base.ToString(); // Appel à la version de base de cette méthode
            return result;
        }
    }

    // Les interfaces contiennent uniquement la signature de leurs membres, sans implémentation.
    interface IJumpable
    {
        void Jump(int meters); // Tous les membres d'interface sont publics par défaut
    }

    interface IBreakable
    {
        bool Broken { get; } // Les interfaces peuvent contenir des propriétés, 
                             // des méthodes et des évènements
    }

    // Une classe ne peut hériter que d'une seule autre classe, mais peut implémenter plusieurs interfaces
    class MountainBike : Bicycle, IJumpable, IBreakable
    {
        int damage = 0;

        public void Jump(int meters)
        {
            damage += meters;
        }

        public bool Broken
        {
            get
            {
                return damage > 100;
            }
        }
    }

    /// <summary>
    /// Utilisé pour illustrer la connexion à une base donnée dans l'exemple LinqToSql
    /// L'approche code first d'EntityFramework est très pratique (un peu comme ActiveRecord de Ruby) 
    /// http://msdn.microsoft.com/fr-fr/data/jj193542.aspx
    /// </summary>
    public class BikeRespository : DbSet
    {
        public BikeRespository()
            : base()
        {
        }

        public DbSet<Bicycle> Bikes { get; set; }
    }
} // Fin du namespace
```

## Sujets non-abordés

 * Flags
 * Attribus
 * Propriétés statiques
 * Exceptions, Abstraction
 * ASP.NET (Web Forms/MVC/WebMatrix)
 * Winforms
 * Windows Presentation Foundation (WPF)

## Lectures Complémentaires

 * [DotNetPerls](http://www.dotnetperls.com)
 * [C# in Depth](http://manning.com/skeet2)
 * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
 * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
 * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx)
 * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
 * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials)
 * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
 * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)

[Convention de codage C#](http://msdn.microsoft.com/library/vstudio/ff926074)
---
language: css
filename: cascading-fr.css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
    - ["@prrrnd", "https://github.com/prrrnd"]
lang: fr-fr
---

Au début du web, il n'y avait pas d'élements visuels, simplement du texte pur. Mais avec le dévelopement des navigateurs,
des pages avec du contenu visuel sont arrivées.
CSS est le langage standard qui existe et permet de garder une séparation entre
le contenu (HTML) et le style d'une page web.

En résumé, CSS fournit une syntaxe qui vous permet de cibler des élements présents
sur une page HTML afin de leur donner des propriétés visuelles différentes.

Comme tous les autres langages, CSS a plusieurs versions. Ici, nous allons parler de CSS2.0
qui n'est pas le plus récent, mais qui reste le plus utilisé et le plus compatible avec les différents navigateurs.

**NOTE :** Vous pouvez tester les effets visuels que vous ajoutez au fur et à mesure du tutoriel sur des sites comme [dabblet](http://dabblet.com/) afin de voir les résultats, comprendre, et vous familiariser avec le langage.
Cet article porte principalement sur la syntaxe et quelques astuces.


```css
/* Les commentaires sont entourés par slash-étoile, comme cette ligne! */

/* ####################
   ## SÉLECTEURS
   ####################*/

/* Généralement, la première déclaration en CSS est très simple */
selecteur { propriete: valeur; /* autres proprietés...*/ }

/* Le sélecteur sert à cibler un élément du HTML

Vous pouvez cibler tous les éléments d'une page! */
* { color:red; }

/*
Voici un élément dans notre HTML :

<div class='une-classe classe2' id='unId' attr='valeur' />
*/

/* Vous pouvez le cibler par une classe */
.une-classe { }

/* ou par deux */
.une-classe.classe2 { }

/* ou par son type */
div { }

/* ou son id */
#unId { }

/* ou par le fait qu'il a un attribut */
[attr] { font-size:smaller; }

/* ou que l'attribut a une valeur spécifique */
[attr='valeur'] { font-size:smaller; }

/* commence avec une valeur */
[attr^='val'] { font-size:smaller; }

/* termine avec une valeur */
[attr$='eur'] { font-size:smaller; }

/* contient une valeur */
[attr~='leu'] { font-size:smaller; }


/* Ce qu'il faut bien comprendre, c'est que vous pouvez combiner ceci -- Il ne doit pas y avoir
d'espaces entre. */
div.une-classe[attr$='eu'] { }

/* Vous pouvez aussi cibler un élément par son parent. */

/* Un élément qui est en enfant direct */
div.un-parent > .enfant {}

/* Cela cible aussi les .enfants plus profonds dans la structure HTML */
div.un-parent .enfants {}

/* Attention : le même sélecteur sans espace a un autre sens. */
div.un-parent.classe {}

/* Vous pouvez cibler un élément basé sur un enfant de même parent */
.je-suis-avant + .cet-element { }

/* ou n'importe quel enfant de même parent avec celui ci */
.je-suis-tout-avant ~ .cet-element {}

/* Il y a des pseudo-classes qui permettent de cibler un élément
basé sur le comportement, en plus de la structure de la page */

/* élément avec le curseur au-dessus */
:hover {}

/* lien visité */
:visited {}

/* lien non visité */
:link {}

/* élément avec le focus */
:focus {}


/* ####################
   ## PROPRIÉTÉS
   ####################*/

selecteur {

    /* Units */
    width: 50%; /* pourcentage */
    font-size: 2em; /* taille de la police multipliée par X */
    width: 200px; /* pixels */
    font-size: 20pt; /* points */
    width: 5cm; /* centimetres */
    width: 50mm; /* millimetres */
    width: 5in; /* pouces */

    /* Couleurs */
    background-color: #F6E;  /* court hex */
    background-color: #F262E2; /* long hex */
    background-color: tomato; /* couleur nommée */
    background-color: rgb(255, 255, 255); /* rouge, vert, bleu */
    background-color: rgb(10%, 20%, 50%); /* rouge, vert, bleu en pourcent */
    background-color: rgba(255, 0, 0, 0.3); /* rouge, vert, bleu avec transparence */

    /* Images */
    background-image: url(/chemin-vers-image/image.jpg);

    /* Polices */
    font-family: Arial;
    font-family: "Courier New"; /* Si espace, entre guillemets */
    font-family: "Courier New", Trebuchet, Arial; /* Si la première n'est pas trouvée, la deuxième est utilisée, etc... */
}

```

## Utilisation

Le CSS s'écrit dans des fichiers `.css`.

```xml
<!-- Vous devez inclure le CSS dans la balise <head> : -->
<link rel='stylesheet' type='text/css' href='chemin/style.css' />

<!-- Vous pouvez inclure du CSS dans le HTML directement, mais ce n'est vraiment pas recommandé. -->
<style>
   selecteur { propriete:valeur; }
</style>

<!-- ou directement sur l'élément HTML.
PS : à ne pas faire. -->
<div style='propriete:valeur;'>
</div>

```

## Priorités

Comme on vient de le voir, un élément peut être ciblé par plus qu'un seul sélecteur
et une même propriété peut être définie plusieurs fois.
Dans ces cas, une des propriétés devient prioritaire.

Voici du code CSS :

```css
/*A*/
p.classe1[attr='valeur']

/*B*/
p.classe1 {}

/*C*/
p.classe2 {}

/*D*/
p {}

/*E*/
p { propriete: valeur !important; }

```

et le code HTML:

```xml
<p style='/*F*/ propriete:valeur;' class='classe1 classe2' attr='valeur'>
</p>
```

Les priorités de style sont :
Attention, les priorités s'appliquent aux **propriétés**, pas aux blocs entiers.

* `E` a la priorité grâce à `!important`.  
* `F` vient ensuite, car le code se trouve directement dans le HTML.
* `A` vient ensuite, car il est le plus spécifique.  
	plus spécifique veut dire, celui qui cible le plus l'élément
* `C` vient ensuite. Il est aussi spécifique que `B`, mais est écrit après.
* Puis `B`
* Et enfin `D`.

## Compatibilité

La plupart des fonctionnalités de CSS2 (et de plus en plus CSS3) sont compatibles
avec tous les navigateurs. Mais il est important de vérifier la compatibilité.

[QuirksMode CSS](http://www.quirksmode.org/css/) est une très bonne source pour cela.

## En savoir plus (en anglais)

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
---
language: D
filename: learnd-fr.d
contributors:
    - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]
translators:
    - ["Quentin Ladeveze", "aceawan.eu"]
lang: fr-fr
---

```c
// Commençons par un classique
module hello;

import std.stdio;

// args n'est pas obligatoire
void main(string[] args) {
    writeln("Bonjour le monde !");
}
```

Si vous êtes comme moi et que vous passez beaucoup trop de temps sur internet, il y a
de grandes chances pour que vous ayez déjà entendu parler du [D](http://dlang.org/).
D est un langage de programmation moderne, généraliste, multi-paradigmes qui contient
des fonctionnalités aussi bien de bas niveau que de haut niveau.

D est activement développé par de nombreuses personnes très intelligents, guidées par
[Walter Bright](https://fr.wikipedia.org/wiki/Walter_Bright))) et
[Andrei Alexandrescu](https://fr.wikipedia.org/wiki/Andrei_Alexandrescu).
Après cette petite introduction, jetons un coup d'oeil à quelques exemples.

```c
import std.stdio;

void main() {
    //Les conditions et les boucles sont classiques.
    for(int i = 0; i < 10000; i++) {
        writeln(i);
    }

    // On peut utiliser auto pour inférer automatiquement le
    // type d'une variable.
    auto n = 1;

    // On peut faciliter la lecture des valeurs numériques
    // en y insérant des `_`.
    while(n < 10_000) {
        n += n;
    }

    do {
        n -= (n / 2);
    } while(n > 0);

    // For et while sont très utiles, mais en D, on préfère foreach.
    // Les deux points : '..', créent un intervalle continu de valeurs
    // incluant la première mais excluant la dernière.
    foreach(i; 1..1_000_000) {
        if(n % 2 == 0)
            writeln(i);
    }

    // On peut également utiliser foreach_reverse pour itérer à l'envers.
    foreach_reverse(i; 1..int.max) {
        if(n % 2 == 1) {
            writeln(i);
        } else {
            writeln("Non !");
        }
    }
}
```
On peut définir de nouveaux types avec les mots-clés `struct`, `class`,
`union` et `enum`. Ces types sont passés à la fonction par valeur (ils sont copiés)
De plus, on peut utiliser les templates pour rendre toutes ces abstractions génériques.

```c
// Ici, 'T' est un paramètre de type. Il est similaire au <T> de C++/C#/Java.
struct LinkedList(T) {
    T data = null;

	// Utilisez '!' pour instancier un type paramétré.
	// Encore une fois semblable à '<T>'
    LinkedList!(T)* next;
}

class BinTree(T) {
    T data = null;

    // S'il n'y a qu'un seul paramètre de template,
    // on peut s'abstenir de mettre des parenthèses.
    BinTree!T left;
    BinTree!T right;
}

enum Day {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

// Utilisez alias pour créer des abreviations pour les types.
alias IntList = LinkedList!int;
alias NumTree = BinTree!double;

// On peut tout aussi bien créer des templates de function !
T max(T)(T a, T b) {
    if(a < b)
        return b;

    return a;
}

// On peut utiliser le mot-clé ref pour s'assurer que quelque chose est passé
// par référence, et ceci, même si a et b sont d'ordinaire passés par valeur.
// Ici ils seront toujours passés par référence à 'swap()'.
void swap(T)(ref T a, ref T b) {
    auto temp = a;

    a = b;
    b = temp;
}

// Avec les templates, on peut également passer des valeurs en paramètres.
class Matrix(uint m, uint n, T = int) {
    T[m] rows;
    T[n] columns;
}

auto mat = new Matrix!(3, 3); // T est 'int' par défaut

```
À propos de classes, parlons des propriétés. Une propriété est, en gros,
une méthode qui peut se comporter comme une lvalue. On peut donc utiliser
la syntaxe des structures classiques (`struct.x = 7`) comme si il
s'agissait de méthodes getter ou setter.

```c
// Considérons une classe paramétrée avec les types 'T' et 'U'
class MyClass(T, U) {
    T _data;
    U _other;
}

// Et des méthodes "getter" et "setter" comme suit:
class MyClass(T, U) {
    T _data;
    U _other;

	// Les constructeurs s'appellent toujours 'this'.
    this(T t, U u) {
		// Ceci va appeller les setters ci-dessous.
        data = t;
        other = u;
    }

    // getters
    @property T data() {
        return _data;
    }

    @property U other() {
        return _other;
    }

    // setters
    @property void data(T t) {
        _data = t;
    }

    @property void other(U u) {
        _other = u;
    }
}

// Et on l'utilise de cette façon:
void main() {
    auto mc = new MyClass!(int, string)(7, "seven");

	// Importer le module 'stdio' de la bibliothèque standard permet
	// d'écrire dans la console (les imports peuvent être locaux à une portée)
    import std.stdio;

	// On appelle les getters pour obtenir les valeurs.
    writefln("Earlier: data = %d, str = %s", mc.data, mc.other);

	// On appelle les setter pour assigner de nouvelles valeurs.
    mc.data = 8;
    mc.other = "eight";

	// On appelle les setter pour obtenir les nouvelles valeurs.
    writefln("Later: data = %d, str = %s", mc.data, mc.other);
}
```
Avec les propriétés, on peut construire nos setters et nos getters
comme on le souhaite, tout en gardant une syntaxe très propre,
comme si on accédait directement à des membres de la classe.

Les autres fonctionnalités orientées objets à notre disposition
incluent les interfaces, les classes abstraites, et la surcharge
de méthodes. D gère l'héritage comme Java: On ne peut hériter que
d'une seule classe et implémenter autant d'interface que voulu.

Nous venons d'explorer les fonctionnalités objet du D, mais changeons
un peu de domaine. D permet la programmation fonctionelle, avec les fonctions
de premier ordre, les fonctions `pures` et les données immuables.
De plus, tout vos algorithmes fonctionels favoris (map, reduce, filter)
sont disponibles dans le module `std.algorithm`.

```c
import std.algorithm : map, filter, reduce;
import std.range : iota; // construit un intervalle excluant la dernière valeur.

void main() {
	// On veut un algorithme qui affiche la somme de la liste des carrés
	// des entiers paires de 1 à 100. Un jeu d'enfant !

	// On se contente de passer des expressions lambda en paramètre à des templates.
	// On peut fournir au template n'importe quelle fonction, mais dans notre
	// cas, les lambdas sont pratiques.
    auto num = iota(1, 101).filter!(x => x % 2 == 0)
                           .map!(y => y ^^ 2)
                           .reduce!((a, b) => a + b);

    writeln(num);
}
```

Vous voyez qu'on a calculé `num` comme on le ferait en haskell par exemple ?
C'est grâce à une innovation de D qu'on appelle "Uniform Function Call Syntax".
Avec l'UFCS, on peut choisir d'écrire un appel à une fonction de manière
classique, ou comme un appel à une méthode. Walter Brighter a écrit un
article en anglais sur l'UFCS [ici.](http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394)
Pour faire court, on peut appeller une fonction dont le premier paramètre
est de type A, comme si c'était une méthode de A.

J'aime le parallélisme. Vous aimez le parallélisme ? Bien sûr que vous aimez ça.
Voyons comment on le fait en D !

```c
import std.stdio;
import std.parallelism : parallel;
import std.math : sqrt;

void main() {
    // On veut calculer la racine carrée de tous les nombres
    // dans notre tableau, et profiter de tous les coeurs
    // à notre disposition.
    auto arr = new double[1_000_000];

    // On utilise un index et une référence à chaque élément du tableau.
    // On appelle juste la fonction parallel sur notre tableau !
    foreach(i, ref elem; parallel(arr)) {
        ref = sqrt(i + 1.0);
    }
}


```
---
category: Algorithms & Data Structures
name: Dynamic Programming
contributors:
    - ["Akashdeep Goel", "http://github.com/akashdeepgoel"]
translators:
    - ["Hughes Perreault", "https://github.com/hperreault"]
lang: fr-fr
---


# Programmation dynamique

## Introduction

La programmation dynamique est une technique très efficace pour résoudre une certaine classe de problèmes, comme nous allons le voir. L'idée est très simple, si nous avons résolu un problème avec une certaine entrée, alors nous sauvons le résultat pour pouvoir y accéder plus tard, pour éviter d'avoir à le calculer à nouveau.

## Moyens de résoudre ces problèmes

1.) *De haut en bas* : Commençons à résoudre le problème en le séparant en morceaux. Si nous voyons que le problème a déjà été résolu, alors nous retournons la réponse précédemment sauvegardée. Si le problème n'a pas été résolu, alors nous le résolvons et sauvegardons la réponse. C'est généralement facile et intuitif de réfléchir de cette façon. Cela s'appelle la Mémorisation.

2.) *De bas en haut* : Il faut analyser le problème et trouver les sous-problèmes, et l'ordre dans lequel il faut les résoudre. Ensuite, nous devons résoudre les sous-problèmes et monter jusqu'au problème que nous voulons résoudre. De cette façon, nous sommes assurés que les sous-problèmes sont résolus avant de résoudre le vrai problème. Cela s'appelle la Programmation Dynamique.

## Exemple de Programmation Dynamique

Le problème de la plus grande sous-chaîne croissante est de trouver la plus grande sous-chaîne croissante dans une chaîne. Soit la chaîne `S = {a1, a2, a3, a4, ............., an-1, an}`, nous avons à trouver la plus grande chaîne telle que pour tout `j` et `i`, `j<i` dans la chaîne `aj<ai`.
Premièrement, nous avons à trouver la valeur de la plus grande sous-chaîne (LSi) à chaque index `i`, avec le dernier élément de la sous-chaîne étant ai. Alors, la plus grande sous-chaîne sera le plus gros LSi. Pour commencer, LSi est égal à 1, car ai est le seul élément de la chaîne (le dernier). Ensuite, pour chaque `j` tel que `j<i` et `aj<ai`, nous trouvons le plus grand LSj et ajoutons le à LSi. L'algorithme fonctionne en temps *O(n2)*.   

Pseudo-code pour trouver la longueur de la plus grande sous-chaîne croissante :
La complexité de cet algorithme peut être réduite en utilisant une meilleure structure de données qu'un tableau. Par exemple, si nous sauvegardions le tableau d'origine, ou une variable comme plus_grande_chaîne_jusqu'à_maintenant et son index, nous pourrions sauver beaucoup de temps. 

Le même concept peut être appliqué pour trouver le chemin le plus long dans un graphe acyclique orienté.  

```python
 for i=0 to n-1
            LS[i]=1
            for j=0 to i-1
                        if (a[i] >  a[j] and LS[i]<LS[j])
                                    LS[i] = LS[j]+1
 for i=0 to n-1
            if (largest < LS[i])
```

### Problèmes classiques de programmation dynamique

L'algorithme de Floyd Warshall(EN)) - Tutorial and C Program source code:http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code

Problème du sac à dos(EN) - Tutorial and C Program source code: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem


Plus longue sous-chaîne commune(EN) - Tutorial and C Program source code : http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence

## Online Resources

* [codechef EN](https://www.codechef.com/wiki/tutorial-dynamic-programming)
---
language: elisp
contributors:
    - ["Bastien Guerry", "https://bzg.fr"]
    - ["Saurabh Sandav", "http://github.com/SaurabhSandav"]
translators:
    - ["Bastien Guerry", "https://bzg.fr"]
filename: learn-emacs-lisp-fr.el
lang: fr-fr
---

```scheme
;; Ceci est une introduction à Emacs Lisp en 15 minutes (v0.2d)
;;
;; Auteur : Bastien / @bzg2 / https://bzg.fr
;;
;; Prenez d'abord le temps de lire ce texte en anglais de Peter Norvig :
;; http://norvig.com/21-days.html
;;
;; Ensuite installez GNU Emacs 24.3 (ou une version ultérieure) :
;;
;; Debian : apt-get install emacs (voir les instructions pour votre distribution)
;; MacOSX : http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg
;; Windows : http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip
;;
;; Vous trouverez plus d'informations sur l'installation :
;; http://www.gnu.org/software/emacs/#Obtaining

;; Avertissement important :
;;
;; Suivre ce tutoriel ne risque pas d'endommager votre ordinateur,
;; sauf si vous vous énervez au point de le jeter par terre. En tout
;; cas, je décline toute responsabilité en cas de problème.
;; Amusez-vous bien !

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Lancez Emacs.
;;
;; Tapez la touche "q" pour enlever le message d'accueil.
;;
;; Maintenant regardez la ligne grise au pied de la fenêtre :
;;
;; "*scratch*" est le nom de l'espace d'édition dans lequel vous vous
;; trouvez. Cet espace d'édition est appelé un "buffer".
;;
;; Le buffer scratch est le buffer par défaut quand on ouvre Emacs.
;; Vous n'éditez jamais de fichier directement : vous éditez des
;; buffers que vous pouvez sauvegarder dans des fichiers.
;;
;; "Lisp interaction" désigne le jeu de commandes disponible ici.
;;
;; Emacs a un jeu de commandes par défaut pour chaque buffer, et
;; plusieurs autres jeux de commandes disponibles quand vous activez
;; un mode particulier.  Ici nous utilisons `lisp-interaction-mode',
;; qui propose des commandes pour évaluer et naviguer dans du code
;; Elisp.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Le point-virgule commence un commentaire partout sur une ligne.
;;
;; Les programmes Elisp sont composés d'expressions symboliques aussi
;; appelées "sexps" :
(+ 2 2)

;; Cette expression symbolique se lit "Ajouter 2 à 2".

;; Les sexps sont placées entre parenthèses, possiblement sur
;; plusieurs niveaux :
(+ 2 (+ 1 1))

;; Une expression symbolique contient des atomes ou d'autres
;; expressions symboliques. Dans les exemples ci-dessus, 1 et 2 sont
;; des atomes et (+ 2 (+ 1 1)) et (+ 1 1) des expressions symboliques.

;; Dans le mode `lisp-interaction-mode' vous pouvez évaluer les sexps.
;; Placez le curseur juste après la parenthèse fermante, tenez la
;; touche "Control" enfoncée et appuyez sur la touche "j" (soit le
;; raccourci "C-j").

(+ 3 (+ 1 2))
;;           ^ curseur ici
;; `C-j' => 6

;; `C-j' insère le résultat de l'évaluation dans le buffer.

;; `C-x C-e' affiche le même résultat dans la ligne tout en bas
;; d'Emacs, appelée le "minibuffer". On utilise en général `C-x C-e',
;; pour ne pas encombrer le buffer avec du texte inutile.

;; `setq' assigne une valeur à une variable :
(setq my-name "Bastien")
;; `C-x C-e' => "Bastien" (affiché dans le minibuffer)

;; `insert' va insérer "Hello!" là où se trouve le curseur :
(insert "Hello!")
;; `C-x C-e' => "Hello!"

;; Nous utilisons `insert' avec un seul argument "Hello!", mais
;; nous pouvons passer plus d'arguments - ici nous en passons deux :

(insert "Hello" " world!")
;; `C-x C-e' => "Hello world!"

;; Vous pouvez utiliser des variables au lieu de chaînes de caractères :
(insert "Hello, I am " my-name)
;; `C-x C-e' => "Hello, I am Bastien"

;; Vous pouvez combiner les sexps en fonctions :
(defun hello () (insert "Hello, I am " my-name))
;; `C-x C-e' => hello

;; Vous pouvez évaluer les fonctions :
(hello)
;; `C-x C-e' => Hello, I am Bastien

;; Les parenthèses vides dans la définition de la fonction signifient
;; qu'elle ne prend pas d'argument. Mais toujours utiliser `my-name'
;; est ennuyant, demandons à la fonction d'accepter un argument (ici
;; l'argument est appelé "name") :

(defun hello (name) (insert "Hello " name))
;; `C-x C-e' => hello

;; Maintenant appelons la fonction avec la chaîne de caractères "you"
;; comme valeur de son unique argument :
(hello "you")
;; `C-x C-e' => "Hello you"

;; Youpi!

;; Faites une pause.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Maintenant ouvrez un nouveau buffer appelé "*test*" dans une
;; nouvelle fenêtre :

(switch-to-buffer-other-window "*test*")
;; `C-x C-e'
;; => [l'écran a deux fenêtres et le curseur est dans le buffer *test*]

;; Placez la souris sur la fenêtre du haut et cliquez-gauche pour
;; retourner dans cette fenêtre. Ou bien utilisez `C-x o' (i.e. tenez
;; control-x appuyé et appuyez sur o) pour aller dans l'autre fenêtre
;; interactivement.

;; Vous pouvez combiner plusieurs sexps avec `progn' :
(progn
  (switch-to-buffer-other-window "*test*")
  (hello "you"))
;; `C-x C-e'
;; => [L'écran a deux fenêtres et le curseur est dans le buffer *test*]

;; Maintenant si ça ne vous dérange pas, je vais arrêter de vous
;; demander de faire `C-x C-e' : faites-le pour chaque sexp qui suit.

;; Retournez toujours dans le buffer *scratch* avec la souris ou `C-x o'.

;; Il est souvent utile d'effacer le contenu du buffer :
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "there"))

;; Ou d'aller à l'autre fenêtre :
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "you")
  (other-window 1))

;; Vous pouvez associer une valeur à une variable locale avec `let' :
(let ((local-name "you"))
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello local-name)
  (other-window 1))

;; Dans ce cas pas besoin d'utiliser `progn' puisque `let' combine
;; aussi plusieurs sexps.

;; Mettons en forme une chaîne de caractères :
(format "Hello %s!\n" "visitor")

;; %s désigne l'emplacement de la chaîne, remplacé par "visitor".
;; \n est le caractère de saut de ligne.

;; Améliorons notre fonction en utilisant "format" :
(defun hello (name)
  (insert (format "Hello %s!\n" name)))

(hello "you")

;; Créons une autre fonction qui utilise `let' :
(defun greeting (name)
  (let ((your-name "Bastien"))
    (insert (format "Hello %s!\n\nI am %s."
                    name       ; l'argument de la fonction
                    your-name  ; la variable "let-bindée" "Bastien"
                    ))))

;; Et évaluons-la :
(greeting "you")

;; Certaines fonctions sont interactives :
(read-from-minibuffer "Enter your name: ")

;; Évaluer cette fonction va renvoyer ce que vous avez saisi dans le
;; minibuffer.

;; Faisons que notre fonction `greeting' vous demande votre nom :
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (insert (format "Hello!\n\nI am %s and you are %s."
                    from-name ; l'argument de la fonction
                    your-name ; la variable "let-bindée", entrée dans le minibuffer
                    ))))

(greeting "Bastien")

;; Complétons la fonction pour qu'elle affiche le résultat dans
;; l'autre fenêtre :
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (insert (format "Hello %s!\n\nI am %s." your-name from-name))
    (other-window 1)))

;; Maintenant testons :
(greeting "Bastien")

;; Faites une pause.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Stockons une liste de noms :
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))

;; Récupérez le premier élément de la liste avec `car' :
(car list-of-names)

;; Récupérez tous les élements sauf le premier avec `cdr' :
(cdr list-of-names)

;; Ajoutez un élément au début avec `push' :
(push "Stephanie" list-of-names)

;; Note : `car' et `cdr' ne modifient pas la liste, mais `push' oui.
;; C'est une différence importante : certaines fonctions n'ont pas
;; d'effets de bord (comme `car') et d'autres oui (comme `push').

;; Évaluons `hello' pour tous les éléments dans `list-of-names' :
(mapcar 'hello list-of-names)

;; Améliorons `greeting' pour dire hello aux noms de `list-of-names' :
(defun greeting ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (mapcar 'hello list-of-names)
    (other-window 1))

(greeting)

;; Vous vous souvenez de la fonction `hello' définie ci-dessus ?  Elle
;; prend seulement un argument, un nom.  `mapcar' appelle `hello' en
;; utilisant successivement chaque élément de `list-of-names' comme
;; argument de `hello'.

;; Maintenant arrangeons un peu ce qui est affiché dans le buffer :

(defun replace-hello-by-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (search-forward "Hello")
      (replace-match "Bonjour"))
    (other-window 1))

;; (goto-char (point-min)) va au début du buffer.
;; (search-forward "Hello") cherche la chaîne "Hello".
;; (while x y) évalue la sexp(s) y tant que x renvoie quelque chose.
;; Si x renvoie `nil' (rien), nous sortons de la boucle.

(replace-hello-by-bonjour)

;; Vous devriez voir toutes les occurrences de "Hello" dans le buffer
;; *test* remplacées par "Bonjour".

;; Vous devriez aussi avoir une erreur : "Search failed: Hello".
;;
;; Pour éviter cette erreur, il faut dire à `search-forward' si la
;; recherche doit s'arrêter à un certain point du buffer, et si elle
;; doit s'arrêter silencieusement si aucune chaîne n'est trouvée.

;; (search-forward "Hello" nil t) fait ça :

;; L'argument `nil' indique que la recherche n'est pas limitée à une
;; position.  L'argument `t' indique de s'arrêter silencieusement si
;; rien n'est trouvé.

;; Nous utilisons cette sexp dans la fonction ci-dessous, qui ne
;; renvoie pas d'erreur :

(defun hello-to-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    ;; Dit hello aux noms de `list-of-names'
    (mapcar 'hello list-of-names)
    (goto-char (point-min))
    ;; Remplace "Hello" par "Bonjour"
    (while (search-forward "Hello" nil t)
      (replace-match "Bonjour"))
    (other-window 1))

(hello-to-bonjour)

;; Mettons les noms en gras :

(defun boldify-names ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (re-search-forward "Bonjour \\(.+\\)!" nil t)
      (add-text-properties (match-beginning 1)
                           (match-end 1)
                           (list 'face 'bold)))
    (other-window 1))

;; Cette fonction introduit `re-search-forward' : au lieu de chercher
;; la chaîne "Bonjour", nous cherchons un "pattern" en utilisant une
;; "expression régulière" (le préfixe "re-" signifie "regular
;; expression").

;; L'expression régulière est "Bonjour \\(.+\\)!" et se lit :
;; la chaîne "Bonjour ", et
;; un groupe de                | c'est la syntaxe \\( ... \\)
;;   n'importe quel caractère  | c'est le .
;;   une ou plusieurs fois     | c'est le +
;; et la chaîne "!".

;; Prêt ?  Testons !

(boldify-names)

;; `add-text-properties' ajoute des propriétés textuelles telle que
;; des "faces" (une "face" définit la fonte, la couleur, la taille et
;; d'autres propriétés du texte.)

;; Et voilà, c'est fini.  Happy hacking!

;; Si vous voulez en savoir plus sur une variable ou une fonction :
;;
;; C-h v une-variable RET
;; C-h f une-fonction RET
;;
;; Pour lire le manuel Emacs Lisp avec Emacs :
;;
;; C-h i m elisp RET
;;
;; Pour lire en ligne une introduction à Emacs Lisp :
;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html

;; Merci à ces personnes pour leurs retours et suggetions :
;; - Wes Hardaker
;; - notbob
;; - Kevin Montuori
;; - Arne Babenhauserheide
;; - Alan Schmitt
;; - LinXitoW
;; - Aaron Meurer
```
---
language: erlang
contributors:
    - ["Giovanni Cappellotto", "http://www.focustheweb.com/"]
translators:
    - ["Julien Cretel", "https://github.com/Jubobs"]
filename: learnerlang-fr.erl
lang: fr-fr
---

```erlang
% Un signe pour cent marque le début d'un commentaire de fin de ligne.

%% Deux signes pour cent sont utilisés pour commenter les fonctions.

%%% Trois signes pour cent sont utilisés pour commenter les modules.

% Trois symboles de ponctuation sont utilisés en Erlang.
% Les virgules (`,`) servent à séparer les paramètres dans les appels de
% fonctions, les contructeurs, et les motifs.
% Les points (`.`) (suivis par des blancs) servent à séparer les fonctions et
% les expressions dans l'interpréteur.
% Les points-virgules (`;`) servent à séparer les clauses. Ces dernières
% apparaissent dans différent cas de figure : définitions de fonctions et
% expressions `case`, `if`, `try..catch`, `receive`.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. Variables et filtrage par motif
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

(L'équivalent anglais de *filtrage par motif* est *pattern patching*.)

Nb = 42.  % Chaque nom de variable doit commencer par une lettre majuscule.

% Les variables Erlang ne peuvent être affectées qu'une seule fois ; si vous
% essayez d'affecter une autre valeur à la variable `Nb`, vous obtiendrez
% une erreur.
Nb = 43. % ** exception error: no match of right hand side value 43

% Dans la plupart des languages, `=` indique une affectation. En Erlang,
% cependant, `=` indique un filtrage par motif. En fait, `Gauche = Droit`
% signifie ce qui suit : évalue le côté droit (`Droit`), et ensuite filtre le
% résultat à l'aide du motif du côté gauche (`Gauche`).
Nb = 7 * 6.

% Nombre en virgule flottante.
Pi = 3.14159.

% Les atomes représentent des valeurs constantes non-numériques. Un atome
% commence par une lettre minuscule, suivie d'une séquence composée de
% caractères alphanumériques, de tirets bas (`_`), ou d'arobases (`@`).
Bonjour = bonjour.
AutreNoeud = exemple@noeud.

% Les atomes de valeur autre qu'alphanumérique peuvent être délimités par
% des guillemets droits simples.
AtomeAvecEspace = 'un atome contenant des espaces'.

% Les tuples sont similaires aux enregistrements du language C.
Point = {point, 10, 45}.

% Pour extraire des valeurs d'un tuple, on filtre par motif avec
% l'opérateur `=`.
{point, X, Y} = Point.  % X = 10, Y = 45

% On peut utiliser `_` comme caractère joker pour les variables qui ne nous
% intéressent pas. Le symbol `_` est appelé variable muette. Contrairement
% aux variables normales, de multiples apparitions de `_` dans un même motif
% ne lient pas nécessairement à la même valeur.
Personne = {personne, {nom, {prenom, joe}, {famille, armstrong}},
                      {pointure, 42}}.
{_, {_, {_, Qui}, _}, _} = Personne.  % Qui = joe

% Pour créer une liste, on écrit les éléments de la liste entre crochets, en
% les séparant par des virgules.
% Les éléments d'une liste peuvent avoir n'importe quel type.
% Le premier élément d'une liste est appelé la tête de la liste. Si on retire
% la tête d'une liste, ce qui reste est appelée la queue de la liste.
Articles = [{pommes, 10}, {poires, 6}, {lait, 3}].

% Si `Q` est une liste, alors `[T|Q]` est aussi une liste dont la tête est `T`
% et dont la queue est `Q`. La barre verticale (`|`) sépare la tête d'une
% liste de sa queue.
% `[]` est la liste vide.
% On peut extraire des éléments d'une liste par filtrage de motif. Si `L` est
% une liste non vide, alors l'expression `[X|Y] = L`, où `X` et `Y` sont des
% variables non affectées, va extraire la tête de la liste dans `X` et la
% queue de la liste dans `Y`.
[PremierArticle|AutresArticles] = Articles.
% PremierArticle = {pommmes, 10}
% AutresArticles = [{poires, 6}, {lait, 3}]

% Il n'y a pas de chaînes de caractères en Erlang. Les chaînes de caractères
% ne sont rien de plus que des listes d'entiers.
% Les chaînes de caractères sont délimitées par des guillemets droits doubles
% (`"`).
Nom = "Bonjour".
[66, 111, 110, 106, 111, 117, 114] = "Bonjour".


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. Programmation séquentielle.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Les modules constituent l'unité de base d'un programme Erlang. Toutes les
% fonctions que l'on écrit sont enregistrées dans des modules. Les modules sont
% enregistrés dans des fichiers avec une extension `.erl`.
% Les modules doivent être compilés afin d'éxecuter le programme.
% Un module compilé a une extension `.beam`.
-module(geometrie).
-export([aire/1]). % la liste des fonctions exportées par le module.

% La fonction `aire` est composée de deux clauses. Les clauses sont séparées
% par un point-virgule, et la dernière clause est suivie d'un point et un
% espace blanc. Chaque clause a une en-tête et un corps ; l'en-tête consiste
% en un nom de fonction suivi d'un motif (entre parenthèses), et le corps
% consiste en une séquence d'expressions, qui sont évaluées si le motif de
% l'en-tête est cohérent par rapport à la valeur des paramètres d'appel.
% L'expression est filtrée séquentiellement par les différents motifs, dans
% l'ordre dans lequel ils apparaissent dans la définition de la fonction.
aire({rectangle, Largeur, Hauteur}) -> Largeur * Hauteur;
aire({cercle, R})                   -> 3.14159 * R * R.

% Compilation du code du fichier geometrie.erl.
c(geometrie).  % {ok,geometrie}

% Le nom du module doit être inclus avec le nom de la fonction afin
% d'identifier précisément quelle fonction on souhaite appeler.
geometrie:aire({rectangle, 10, 5}).  % 50
geometrie:area({cercle, 1.4}).  % 6.15752

% En Erlang, deux fonctions portant le même nom mais ayant des arités
% différentes (c'est à dire ne prenant pas le même nombre de paramètres)
% au sein d'un même module représentent des fonctions complètement
% différentes.
-module(lib_divers).
-export([somme/1]). % exporte la fonction `somme` d'arité 1
                    % acceptant un paramètre : une liste d'entiers.
somme(L) -> somme(L, 0).
somme([], N)    -> N;
somme([T|Q], N) -> somme(Q, T+N).

% Les `fun`s sont des fonctions "anonymes" ; elles sont appelées ainsi parce
% qu'elles n'ont pas de nom. Cependant, elles peuvent être affectées à des
% variables.
Doubler = fun(X) -> 2 * X end. % `Doubler` pointe vers une fonction anonyme
                               % dont le handle est : #Fun<erl_eval.6.17052888>
Doubler(2).  % 4

% Les fonctions peuvent prendre des `fun`s comme paramètres et peuvent renvoyer
% des `fun`s.
Mult = fun(Fois) -> ( fun(X) -> X * Fois end ) end.
Tripler = Mult(3).
Tripler(5).  % 15

% Les listes en compréhension sont des expressions qui créent des listes sans
% requérir ni `fun`s, ni maps, ni filters.
% La notation `[F(X) || X <- L]` signifie "la liste des `F(X)` où `X` est
% extrait de la liste `L`."
L = [1,2,3,4,5].
[2 * X || X <- L].  % [2,4,6,8,10]
% Une liste en compréhension peut être constituée de générateurs, ainsi que de
% gardes, qui sélectionnent un sous-ensemble des valeurs générées.
NombresPairs = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4]

% La garde est un élément syntaxique qui rend le filtrage par motif encore
% plus puissant. Les gardes permettent de d'effectuer de simple tests et
% comparaisons sur les variables d'un motif. Les gardes peuvent être
% utilisées dans les en-têtes de fonctions, au sein desquelles elles sont
% introduites par le mot-clé `when`, ou encore à n'importe quel endroit où
% une expression est autorisée.
max(X, Y) when X > Y -> X;
max(X, Y) -> Y.

% Une garde est une série d'expressions gardes, séparées par des virgules (`,`).
% La garde `ExprGarde1, ExprGarde2, ..., ExprGardeN` est vraie si toutes les
% expressions gardes `ExprGarde1`, `ExprGarde2, ..., `ExprGardeN` ont pour
% valeur `true`.
est_chat(A) when is_atom(A), A =:= chat -> true;
est_chat(A) -> false.
est_chien(A) when is_atom(A), A =:= chien -> true;
est_chien(A) -> false.

% Une séquence de gardes est composée soit d'une seule garde ou bien d'une
% série de gardes, séparées par des points-virgules (`;`). La séquence de
% gardes `G1; G2; ...; Gn` est vraie si au moins l'une des gardes `G1`, `G2`,
% ..., `Gn` a pour valeur `true`.
est_animal(A) when is_atom(A), (A =:= chien) or (A =:= chat) -> true;
est_animal(A)                                                -> false.

% Attention : toutes les expressions Erlang valides ne peuvent pas être
% utilisées comme expressions gardes ; en particulier, nos fonctions
% `est_chat` et `est_chien` ne sont pas autorisées au sein de la séquence de
% gardes dans la définition de `est_animal`. Pour plus de détails sur les
% expressions autorisées ands les séquences de gardes, voir cette
% [section](http://erlang.org/doc/reference_manual/expressions.html#id81912)
% du manuel Erlang.

% Les enregistrements permettent d'associer un nom à un certain élément dans
% un tuple.
% Les enregistrements peuvent être définis dans des fichiers sources Erlang
% ou bien dans des fichiers avec une extension `.hrl`, qui sont ensuite inclus
% dans des fichiers sources Erlang.
-record(afaire, {
  statut = rappel,  % Valeur par défaut
  qui = joe,
  texte
}).

% Les définitions d'enregistrements doivent être lues dans l'interpreteur
% pour qu'on puisse définir un enregistrement. On utilise la fonction `rr`
% (abbréviation de *read records* en anglais, ou *lire enregistrements* en
% français) pour ça.
rr("enregistrements.hrl").  % [afaire]

% Création et mise à jour d'enregistrements :
X = #afaire{}.
% #afaire{statut = rappel, qui = joe, texte = undefined}
X1 = #afaire{statut = urgent, texte = "Corriger erreurs dans livre"}.
% #afaire{statut = urgent, qui = joe, texte = "Corriger erreurs dans livre"}
X2 = X1#afaire{statut = fini}.
% #afaire{statut = fini, qui = joe, texte = "Corriger erreurs dans livre"}

% Expressions `case`.
% `filter` renvoie une liste de tous les éléments `X` d'une liste `L` pour
% lesquels `P(X)` est vrai.
filter(P, [H|T]) ->
  case P(H) of
    true -> [H|filter(P, T)];
    false -> filter(P, T)
  end;
filter(P, []) -> [].
filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]). % [2, 4]

% Expressions `if`.
max(X, Y) ->
  if
    X > Y -> X;
    X < Y -> Y;
    true -> nil
  end.

% Attention : au moins l'une des gardes dans l'expression `if` doit avoir pour
% valeur `true` ; autrement, une exception sera lancée.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3. Exceptions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Des exceptions sont lancées par le système quand des erreurs internes
% surviennent, ou de manière explicite dans le programme en appelant
% `throw(Exception)`, `exit(Exception)`, ou `erlang:error(Exception)`.
generer_exception(1) -> a;
generer_exception(2) -> throw(a);
generer_exception(3) -> exit(a);
generer_exception(4) -> {'EXIT', a};
generer_exception(5) -> erlang:error(a).

% Erlang dispose de deux méthodes pour capturer une exception. La première
% consiste à inclure l'appel de de la fonction qui lance l'exception dans une
% expression `try...catch`.
catcher(N) ->
  try generer_exception(N) of
    Val -> {N, normal, Val}
  catch
    throw:X -> {N, caught, thrown, X};
    exit:X -> {N, caught, exited, X};
    error:X -> {N, caught, error, X}
  end.

% L'autre méthode consiste à inclure l'appel dans une expression `catch`.
% Quand une exception est capturée, elle est convertie en un tuple qui décrit
% l'erreur.
catcher(N) -> catch generer_exception(N).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 4. Concurrence
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Erlang est basé sur le modèle d'acteur pour la concurrence. Seulement trois
% opérations sont requises pour écrire des programmes concurrents en Erlang :
% la création de processus, l'envoi de messages, et la réception de messages.

% Pour démarrer un nouveau processus, on utilise la fonction `spawn`, qui
% prend une fonction comme paramètre.

F = fun() -> 2 + 2 end. % #Fun<erl_eval.20.67289768>
spawn(F). % <0.44.0>

% `spawn` renvoie un pid (*process identifier* en anglais, ou *identifiant de
% processus* en français), qui peut être utilisé pour envoyer des messages au
% processus en question. Pour passer des messages, on utilise l'opérateur `!`.
% Pour que cela soit utile, on doit aussi être en mesure de recevoir des
% messages, ce qui est accompli grâce à une clause `receive` :

-module(calculerGeometrie).
-compile(export_all).
calculerAire() ->
    receive
      {rectangle, W, H} ->
        W * H;
      {cercle, R} ->
        3.14 * R * R;
      _ ->
        io:format("Seule l'aire d'un rectangle / cercle peut etre calculee.")
    end.

% Compilation du module and création d'un processus qui évalue `calculerAire`
% dans l'interpréteur.
c(calculerGeometrie).
CalculerAire = spawn(calculerGeometrie, calculerAire, []).
CalculerAire ! {cercle, 2}. % 12.56000000000000049738

% L'interpréteur est lui-même un processus ; on peut utiliser `self` pour
% obtenir le pid actuel.
self(). % <0.41.0>

```

## Ressources (en anglais)

* ["Learn You Some Erlang for great good!"](http://learnyousomeerlang.com/)
* ["Programming Erlang: Software for a Concurrent World" by Joe Armstrong](http://pragprog.com/book/jaerlang/programming-erlang)
* [Erlang/OTP Reference Documentation](http://www.erlang.org/doc/)
* [Erlang - Programming Rules and Conventions](http://www.erlang.se/doc/programming_rules.shtml)
---
language: F#
lang: fr-fr
contributors:
    - ["Scott Wlaschin", "http://fsharpforfunandprofit.com/"]
translators:
    - ["Alois de Gouvello", "https://github.com/aloisdg"]
filename: learnfsharp-fr.fs
---

F# est un langage de programmation fonctionnel et orienté objet. Il est gratuit et son code source est ouvert. Il tourne sur Linux, Mac, Windows et plus.

Il possède un puissant système de type qui piège de nombreuses erreurs à la compilation, mais il utilise l'inférence de type ce qui lui permet d'être lu comme un langage dynamique.

La syntaxe de F# est différente des langages héritant de C.

* Les accolades ne sont pas utilisées pour délimiter les blocs de code. À la place, l'indentation est utilisée (à la manière de Python).
* Les espaces sont utilisés pour séparer les paramètres à la place des virgules.

Si vous voulez essayer le code ci-dessous, vous pouvez vous rendre sur [tryfsharp.org](http://www.tryfsharp.org/Create) et le coller dans le [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop).

```fsharp

// Les commentaires d'une seule ligne commencent par un double slash
(* Les commentaires multilignes utilise les paires (* . . . *)

-fin du commentaire multilignes- *)

// ================================================
// Syntaxe de base
// ================================================

// ------ "Variables" (mais pas vraiment) ------
// Le mot clé "let" définit une valeur (immutable)
let myInt = 5
let myFloat = 3.14
let myString = "hello"           // Notons qu'aucun type n'est nécessaire

// ------ Listes ------
let twoToFive = [2;3;4;5]        // Les crochets créent une liste avec
                                 // des point-virgules pour délimiteurs.
let oneToFive = 1 :: twoToFive   // :: crée une liste avec un nouvel élément
// Le résultat est [1;2;3;4;5]
let zeroToFive = [0;1] @ twoToFive   // @ concatène deux listes

// IMPORTANT: les virgules ne sont jamais utilisées pour délimiter,
// seulement les point-virgules !

// ------ Fonctions ------
// Le mot clé "let" définit aussi le nom d'une fonction.
let square x = x * x          // Notons qu'aucune parenthèse n'est utilisée.
square 3                      // Maitenant, exécutons la fonction.
                              // Encore une fois, aucune parenthèse.

let add x y = x + y           // N'utilisez pas add (x,y) ! Cela signifie
                              // quelque chose de complètement différent.
add 2 3                       // À présent, exécutons la fonction.

// Pour définir une fonction sur plusieurs lignes, utilisons l'indentation.
// Les point-virgules ne sont pas nécessaires.
let evens list =
   let isEven x = x%2 = 0     // Définit "isEven" comme une fonction imbriquée
   List.filter isEven list    // List.filter est une fonction de la librairie
                              // à deux paramètres: un fonction retournant un
                              // booléen et une liste sur laquelle travailler

evens oneToFive               // À présent, exécutons la fonction.

// Vous pouvez utilisez les parenthèses pour clarifier.
// Dans cet exemple, "map" est exécutée en première, avec deux arguments,
// ensuite "sum" est exécutée sur le résultat.
// Sans les parenthèses, "List.map" serait passé en argument à List.sum.
let sumOfSquaresTo100 =
   List.sum ( List.map square [1..100] )

// Vous pouvez rediriger la sortie d'une fonction vers une autre avec "|>"
// Rediriger des données est très commun en F#, comme avec les pipes UNIX.

// Voici la même fonction sumOfSquares écrite en utilisant des pipes
let sumOfSquaresTo100piped =
   [1..100] |> List.map square |> List.sum  // "square" est déclaré avant

// Vous pouvez définir des lambdas (fonctions anonymes) grâce au mot clé "fun"
let sumOfSquaresTo100withFun =
   [1..100] |> List.map (fun x -> x*x) |> List.sum

// En F#, il n'y a pas de mot clé "return". Une fonction retourne toujours
// la valeur de la dernière expression utilisée.

// ------ Pattern Matching ------
// Match..with.. est une surcharge de la condition case/switch.
let simplePatternMatch =
   let x = "a"
   match x with
    | "a" -> printfn "x is a"
    | "b" -> printfn "x is b"
    | _ -> printfn "x is something else"   // underscore correspond à tout le reste

// F# n'autorise pas la valeur null par défaut -- vous devez utiliser le type Option
// et ensuite faire correspondre le pattern.
// Some(..) et None sont approximativement analogue à des wrappers de Nullable
let validValue = Some(99)
let invalidValue = None

// Dans cet exemple, match..with trouve une correspondance à "Some" et à "None",
// et affiche la valeur du "Some" en même temps.
let optionPatternMatch input =
   match input with
    | Some i -> printfn "input is an int=%d" i
    | None -> printfn "input is missing"

optionPatternMatch validValue
optionPatternMatch invalidValue

// ------ Affichage ------
// Les fonctions printf/printfn sont similaires aux fonctions
// Console.Write/WriteLine de C#.
printfn "Printing an int %i, a float %f, a bool %b" 1 2.0 true
printfn "A string %s, and something generic %A" "hello" [1;2;3;4]

// Il y a aussi les fonctions printf/sprintfn pour formater des données
// en string. C'est similaire au String.Format de C#.

// ================================================
// Plus sur les fonctions
// ================================================

// F# est un véritable langage fonctionel -- les fonctions sont des
// entités de premier ordre et peuvent êtres combinées facilement
// pour créer des constructions puissantes

// Les modules sont utilisés pour grouper des fonctions ensemble.
// L'indentation est nécessaire pour chaque module imbriqué.
module FunctionExamples =

    // définit un simple fonction d'addition
    let add x y = x + y

    // usage basique d'une fonction
    let a = add 1 2
    printfn "1+2 = %i" a

    // partial application to "bake in" parameters (?)
    let add42 = add 42
    let b = add42 1
    printfn "42+1 = %i" b

    // composition pour combiner des fonctions
    let add1 = add 1
    let add2 = add 2
    let add3 = add1 >> add2
    let c = add3 7
    printfn "3+7 = %i" c

    // fonctions de premier ordre
    [1..10] |> List.map add3 |> printfn "new list is %A"

    // listes de fonction et plus
    let add6 = [add1; add2; add3] |> List.reduce (>>)
    let d = add6 7
    printfn "1+2+3+7 = %i" d

// ================================================
// Listes et collections
// ================================================

// Il y a trois types de collection ordonnée :
// * Les listes sont les collections immutables les plus basiques
// * Les tableaux sont mutables et plus efficients
// * Les séquences sont lazy et infinies (e.g. un enumerator)
//
// Des autres collections incluent des maps immutables et des sets
// plus toutes les collections de .NET

module ListExamples =

    // les listes utilisent des crochets
    let list1 = ["a";"b"]
    let list2 = "c" :: list1    // :: pour un ajout au début
    let list3 = list1 @ list2   // @ pour la concatenation

    // Compréhensions des listes (aka générateurs)
    let squares = [for i in 1..10 do yield i*i]

    //  Générateur de nombre premier
    let rec sieve = function
        | (p::xs) -> p :: sieve [ for x in xs do if x % p > 0 then yield x ]
        | []      -> []
    let primes = sieve [2..50]
    printfn "%A" primes

    // le pattern matching pour les listes
    let listMatcher aList =
        match aList with
        | [] -> printfn "the list is empty"
        | [first] -> printfn "the list has one element %A " first
        | [first; second] -> printfn "list is %A and %A" first second
        | _ -> printfn "the list has more than two elements"

    listMatcher [1;2;3;4]
    listMatcher [1;2]
    listMatcher [1]
    listMatcher []

    // Récursion en utilisant les listes
    let rec sum aList =
        match aList with
        | [] -> 0
        | x::xs -> x + sum xs
    sum [1..10]

    // -----------------------------------------
    // Fonctions de la librairie standard
    // -----------------------------------------

    // map
    let add3 x = x + 3
    [1..10] |> List.map add3

    // filtre
    let even x = x % 2 = 0
    [1..10] |> List.filter even

    // beaucoup plus -- se référer à la documentation

module ArrayExamples =

    // les tableaux utilisent les crochets avec des barres
    let array1 = [| "a";"b" |]
    let first = array1.[0]        // accès à l'index en utilisant un point

    // le pattern matching des tableaux est le même que celui des listes
    let arrayMatcher aList =
        match aList with
        | [| |] -> printfn "the array is empty"
        | [| first |] -> printfn "the array has one element %A " first
        | [| first; second |] -> printfn "array is %A and %A" first second
        | _ -> printfn "the array has more than two elements"

    arrayMatcher [| 1;2;3;4 |]

    // Fonctions de la librairie standard comme celles des listes
    [| 1..10 |]
    |> Array.map (fun i -> i+3)
    |> Array.filter (fun i -> i%2 = 0)
    |> Array.iter (printfn "value is %i. ")

module SequenceExamples =

    // Les séquences utilisent des accolades
    let seq1 = seq { yield "a"; yield "b" }

    // Les séquences peuvent utiliser yield et
    // peuvent contenir des sous-sequences
    let strange = seq {
        // "yield" ajoute un élément
        yield 1; yield 2;

        // "yield!" ajoute une sous-sequence complète
        yield! [5..10]
        yield! seq {
            for i in 1..10 do
              if i%2 = 0 then yield i }}
    // test
    strange |> Seq.toList

    // Les séquences peuvent être créées en utilisant "unfold"
    // Voici la suite de fibonacci
    let fib = Seq.unfold (fun (fst,snd) ->
        Some(fst + snd, (snd, fst + snd))) (0,1)

    // test
    let fib10 = fib |> Seq.take 10 |> Seq.toList
    printf "first 10 fibs are %A" fib10

// ================================================
// Types de données
// ================================================

module DataTypeExamples =

    // Toutes les données sont immutables par défaut

    // Les tuples sont de simple et rapide types anonymes
    // -- Utilisons une virgule pour créer un tuple
    let twoTuple = 1,2
    let threeTuple = "a",2,true

    // Pattern match pour déballer
    let x,y = twoTuple  // assigne x=1 y=2

    // ------------------------------------
    // Record types ont des champs nommés
    // ------------------------------------

    // On utilise "type" avec des accolades pour définir un type record
    type Person = {First:string; Last:string}

    // On utilise "let" avec des accolades pour créer un record (enregistrement)
    let person1 = {First="John"; Last="Doe"}

    // Pattern match pour déballer
    let {First=first} = person1    // assigne first="john"

    // ------------------------------------
    // Union types (ou variants) ont un set (ensemble) de choix
    // Un seul cas peut être valide à la fois.
    // ------------------------------------

    // On utilise "type" avec bar/pipe pour definir un union type
    type Temp =
        | DegreesC of float
        | DegreesF of float

    // On utilise un de ces choix pour en créér un
    let temp1 = DegreesF 98.6
    let temp2 = DegreesC 37.0

    // Pattern match on all cases to unpack(?)
    let printTemp = function
       | DegreesC t -> printfn "%f degC" t
       | DegreesF t -> printfn "%f degF" t

    printTemp temp1
    printTemp temp2

    // ------------------------------------
    // Types récursif
    // ------------------------------------

    // Les types peuvent être combinés récursivement de façon complexe
    // sans avoir à créer des sous-classes
    type Employee =
      | Worker of Person
      | Manager of Employee list

    let jdoe = {First="John";Last="Doe"}
    let worker = Worker jdoe

    // ------------------------------------
    // Modelling with types(?)
    // ------------------------------------

    // Les types union sont excellents pour modelling state without using flags(?)
    type EmailAddress =
        | ValidEmailAddress of string
        | InvalidEmailAddress of string

    let trySendEmail email =
        match email with // utilisations du pattern matching
        | ValidEmailAddress address -> ()   // envoyer
        | InvalidEmailAddress address -> () // ne pas envoyer

    // Combiner ensemble, les types union et les types record
    // offrent une excellente fondation pour le domain driven design.
    // Vous pouvez créer des centaines de petit types qui reflèteront fidèlement
    // le domain.

    type CartItem = { ProductCode: string; Qty: int }
    type Payment = Payment of float
    type ActiveCartData = { UnpaidItems: CartItem list }
    type PaidCartData = { PaidItems: CartItem list; Payment: Payment}

    type ShoppingCart =
        | EmptyCart  // aucune donnée
        | ActiveCart of ActiveCartData
        | PaidCart of PaidCartData

    // ------------------------------------
    // Comportement natif des types
    // ------------------------------------

    // Les types natifs ont un comportement "prêt-à-l'emploi" des plus utiles, sans code à ajouter.
    // * Immutabilité
    // * Pretty printing au debug
    // * Egalité et comparaison
    // * Sérialisation

    // Le Pretty printing s'utilise avec %A
    printfn "twoTuple=%A,\nPerson=%A,\nTemp=%A,\nEmployee=%A"
             twoTuple person1 temp1 worker

    // L'égalité et la comparaison sont innés
    // Voici un exemple avec des cartes.
    type Suit = Club | Diamond | Spade | Heart
    type Rank = Two | Three | Four | Five | Six | Seven | Eight
                | Nine | Ten | Jack | Queen | King | Ace

    let hand = [ Club,Ace; Heart,Three; Heart,Ace;
                 Spade,Jack; Diamond,Two; Diamond,Ace ]

    // tri
    List.sort hand |> printfn "sorted hand is (low to high) %A"
    List.max hand |> printfn "high card is %A"
    List.min hand |> printfn "low card is %A"

// ================================================
// Les Active patterns
// ================================================

module ActivePatternExamples =

    // F# a un type particulier de pattern matching nommé "active patterns"
    // où le pattern peut être parsé ou détecté dynamiquement.

    // "banana clips" est la syntaxe pour l'active patterns

    // par exemple, on définit un "active" pattern pour correspondre à des types "character"...
    let (|Digit|Letter|Whitespace|Other|) ch =
       if System.Char.IsDigit(ch) then Digit
       else if System.Char.IsLetter(ch) then Letter
       else if System.Char.IsWhiteSpace(ch) then Whitespace
       else Other

    // ... et ensuite on l'utilise pour rendre la logique de parsing plus claire
    let printChar ch =
      match ch with
      | Digit -> printfn "%c is a Digit" ch
      | Letter -> printfn "%c is a Letter" ch
      | Whitespace -> printfn "%c is a Whitespace" ch
      | _ -> printfn "%c is something else" ch

    // afficher une liste
    ['a';'b';'1';' ';'-';'c'] |> List.iter printChar

    // -----------------------------------------
    // FizzBuzz en utilisant les active patterns
    // -----------------------------------------

    // Vous pouvez créer un partial matching patterns également
    // On utilise just un underscore dans la définition, et on retourne Some si ça correspond.
    let (|MultOf3|_|) i = if i % 3 = 0 then Some MultOf3 else None
    let (|MultOf5|_|) i = if i % 5 = 0 then Some MultOf5 else None

    // la fonction principale
    let fizzBuzz i =
      match i with
      | MultOf3 & MultOf5 -> printf "FizzBuzz, "
      | MultOf3 -> printf "Fizz, "
      | MultOf5 -> printf "Buzz, "
      | _ -> printf "%i, " i

    // test
    [1..20] |> List.iter fizzBuzz

// ================================================
// Concision
// ================================================

module AlgorithmExamples =

    // F# a un haut ratio signal/bruit, permettant au code de se lire
    // presque comme un véritable algorithme

    // ------ Exemple: definir une fonction sumOfSquares ------
    let sumOfSquares n =
       [1..n]              // 1) Prendre tous les nombres de 1 à n
       |> List.map square  // 2) Elever chacun d'entre eux au carré
       |> List.sum         // 3) Effectuer leur somme

    // test
    sumOfSquares 100 |> printfn "Sum of squares = %A"

    // ------ Exemple: definir un fonction de tri ------
    let rec sort list =
       match list with
       // Si la liste est vide
       | [] ->
            []                            // on retourne une liste vide
       // si la list n'est pas vide
       | firstElem::otherElements ->      // on prend le premier élément
            let smallerElements =         // on extrait les éléments plus petits
                otherElements             // on prend les restants
                |> List.filter (fun e -> e < firstElem)
                |> sort                   // et on les trie
            let largerElements =          // on extrait les plus grands
                otherElements             // de ceux qui restent
                |> List.filter (fun e -> e >= firstElem)
                |> sort                   // et on les trie
            // On combine les 3 morceaux dans une nouvelle liste que l'on retourne
            List.concat [smallerElements; [firstElem]; largerElements]

    // test
    sort [1;5;23;18;9;1;3] |> printfn "Sorted = %A"

// ================================================
// Code Asynchrone
// ================================================

module AsyncExample =

    // F# inclus des fonctionnalités pour aider avec le code asynchrone
    // sans rencontrer la "pyramid of doom"
    //
    // L'exemple suivant télécharge une séquence de page web en parallèle.

    open System.Net
    open System
    open System.IO
    open Microsoft.FSharp.Control.CommonExtensions

    // Récupérer le contenu d'une URL de manière asynchrone
    let fetchUrlAsync url =
        async {   // Le mot clé "async" et les accolades
                  // créent un objet "asynchrone"
            let req = WebRequest.Create(Uri(url))
            use! resp = req.AsyncGetResponse()
                // use! est un assignement asynchrone
            use stream = resp.GetResponseStream()
                // "use" déclenche automatiquement close()
                // sur les ressources à la fin du scope
            use reader = new IO.StreamReader(stream)
            let html = reader.ReadToEnd()
            printfn "finished downloading %s" url
            }

    // une liste des sites à rapporter
    let sites = ["http://www.bing.com";
                 "http://www.google.com";
                 "http://www.microsoft.com";
                 "http://www.amazon.com";
                 "http://www.yahoo.com"]

    // C'est parti!
    sites
    |> List.map fetchUrlAsync  // créez une liste de tâche asynchrone
    |> Async.Parallel          // dites aux tâches de tourner en parallèle
    |> Async.RunSynchronously  // démarrez les!

// ================================================
// .NET compatabilité
// ================================================

module NetCompatibilityExamples =

    // F# peut réaliser presque tout ce que C# peut faire, et il s'intègre
    // parfaitement avec les librairies .NET ou Mono.

    // ------- Travaillez avec les fonctions des librairies existantes  -------

    let (i1success,i1) = System.Int32.TryParse("123");
    if i1success then printfn "parsed as %i" i1 else printfn "parse failed"

    // ------- Implémentez des interfaces à la volée! -------

    // Créer un nouvel objet qui implémente IDisposable
    let makeResource name =
       { new System.IDisposable
         with member this.Dispose() = printfn "%s disposed" name }

    let useAndDisposeResources =
        use r1 = makeResource "first resource"
        printfn "using first resource"
        for i in [1..3] do
            let resourceName = sprintf "\tinner resource %d" i
            use temp = makeResource resourceName
            printfn "\tdo something with %s" resourceName
        use r2 = makeResource "second resource"
        printfn "using second resource"
        printfn "done."

    // ------- Code orienté objet -------

    // F# est aussi un véritable language OO.
    // Il supporte les classes, l'héritage, les méthodes virtuelles, etc.

    // interface avec type générique
    type IEnumerator<'a> =
        abstract member Current : 'a
        abstract MoveNext : unit -> bool

    // Classe de base abstraite avec méthodes virtuelles
    [<AbstractClass>]
    type Shape() =
        // propriétés en lecture seule
        abstract member Width : int with get
        abstract member Height : int with get
        // méthode non-virtuelle
        member this.BoundingArea = this.Height * this.Width
        // méthode virtuelle avec implémentation de la classe de base
        abstract member Print : unit -> unit
        default this.Print () = printfn "I'm a shape"

    // classe concrète qui hérite de sa classe de base et surcharge
    type Rectangle(x:int, y:int) =
        inherit Shape()
        override this.Width = x
        override this.Height = y
        override this.Print ()  = printfn "I'm a Rectangle"

    // test
    let r = Rectangle(2,3)
    printfn "The width is %i" r.Width
    printfn "The area is %i" r.BoundingArea
    r.Print()

    // ------- extension de méthode  -------

    // Juste comme en C#, F# peut étendre des classes existantes avec des extensions de méthode.
    type System.String with
       member this.StartsWithA = this.StartsWith "A"

    // test
    let s = "Alice"
    printfn "'%s' starts with an 'A' = %A" s s.StartsWithA

    // ------- événements -------

    type MyButton() =
        let clickEvent = new Event<_>()

        [<CLIEvent>]
        member this.OnClick = clickEvent.Publish

        member this.TestEvent(arg) =
            clickEvent.Trigger(this, arg)

    // test
    let myButton = new MyButton()
    myButton.OnClick.Add(fun (sender, arg) ->
            printfn "Click event with arg=%O" arg)

    myButton.TestEvent("Hello World!")

```

## Plus d'information

Pour plus de démonstration de F#, rendez-vous sur le site [Try F#](http://www.tryfsharp.org/Learn), ou suivez la série [why use F#](http://fsharpforfunandprofit.com/why-use-fsharp/).

Apprenez en davantage à propose de F# sur [fsharp.org](http://fsharp.org/).
---
category: tool
tool: git
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Leo Rudberg" , "http://github.com/LOZORD"]
    - ["Betsy Lorton" , "http://github.com/schbetsy"]
    - ["Bruno Volcov", "http://github.com/volcov"]
translators:
    - ["Xuan-thi Nguyen", "http://github.com/mellenguyen"]
filename: LearnGit-fr.txt
lang: fr-fr
---

Git est un logiciel de contrôle de versions distribué et un système de gestion
du code source.

Il effectue sa tâche via des séries d'instantanés (snapshots) du projet, et
travaille avec ces instantanés afin de fournir les fonctionnalités de gestion
de version et de code source.

## Concepts du versionnage

### Qu'est ce que le contrôle de version ?

Le contrôle de version est un système qui enregistre les changements faits sur
un ou plusieurs fichiers au fil du temps.

### Versionnage centralisé VS Versionnage distribué

* Le contrôle de version centralisé se concentre sur la synchronisation, le
suivi et la sauvegarde des fichiers.
* Le contrôle de version distribué se focalise sur l'échange des changements.
Chaque changement a un identifiant unique.
* Les systèmes distribués n'ont pas de structure définie. Vous pouvez aisément
avoir un système centralisé de type SVN, avec Git.

[Informations additionnelles](http://git-scm.com/book/fr/v1/D%C3%A9marrage-rapide-%C3%80-propos-de-la-gestion-de-version)

### Pourquoi utiliser Git ?

* Fonctionne hors ligne.
* Travailler avec les autres devient facile !
* Ramifier le travail (créer des branches différentes) est facile !
* Fusionner le travail est facile !
* Git est rapide.
* Git est flexible.

## Architecture Git


### Dépôt ("repository")

Un ensemble de fichiers, dossiers, historiques de modifications, commits
(validations de changements) et de heads (état courant, "tête").
Représentez-vous ceci comme une structure de données de code source, avec la
particularité que chaque "élement" vous donne, entre autres, accès à son
historique des révisions.

Un dépôt Git comprend un répertoire .git et "l'arbre de travail" (working tree).

### Répertoire .git (composant du dépôt)

Le répertoire .git contient toutes les configurations, logs (journaux),
branches, HEAD et plus.
[Liste détaillée (EN)](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### Arbre de travail (composant du dépôt)

Il s'agit de l'ensemble des répertoires et fichiers de votre dépôt. Il est
souvent qualifié de répertoire de travail ("working directory").

### Index (composant du répertoire .git)

L'index est la zone de transit ("staging area") dans Git. Il s'agit d'une couche
séparant votre arbre de travail de votre dépôt Git. Ceci donne aux développeurs
plus de pouvoir sur ce qu'ils envoient au dépôt.

### Commit

Un "commit" (validation de changements) est un instantané d'un ensemble de
modifications de votre arbre de travail. Par exemple, si vous avez rajouté 5
fichiers et enlevé 2 autres, ces changements seront contenus dans un commit
(ou "snapshot", instantané). Ce commit peut ensuite être poussé ("pushed") dans
d'autres dépôts, ou non !

### Branches

Une branche consiste essentiellement en un pointeur vers le dernier commit que
vous avez fait. Au fur et à mesure de vos commits, ce pointeur se mettra
automatiquement à jour pour pointer vers le dernier commit.

### Etiquette ("tag")

Une étiquette est une marque sur un point spécifique de l'historique.
Typiquement, on utilise cette fonctionnalité pour marquer les états de
publication (v1.0, et ainsi de suite).

### HEAD and head (composant du répertoire .git)

HEAD est un pointeur pointant vers la branche courante. Un dépôt ne peut avoir
qu'un seul HEAD *actif*.
head est un pointeur pouvant pointer sur n'importe quel commit. Un dépôt peut
avoir un nombre illimité de heads.

### Les états dans Git
* Modifié - Des changements on été faits à un fichier mais ce dernier n'a pas
encore été rajouté à l'ensemble des fichiers Git
* Indexé ("staged") - Indique qu'un fichier modifié ira dans le prochain commit
* Validé ("committed") - Les fichiers ont été validés dans l'ensemble de
fichiers

### Ressources conceptuelles

* [Git pour les informaticiens (EN)](http://eagain.net/articles/git-for-computer-scientists/)
* [Git pour les designers (EN)](http://hoth.entp.com/output/git_for_designers.html)


## Commandes


### init

Créé un dépôt Git vide. Les paramètres du dépôt Git, les informations stockées
et plus sont dans un répertoire (un dossier) nommé ".git".

```bash
$ git init
```

### config

Configuration des paramètres. Que ce soit pour le dépôt, le système lui-même,
ou la configuration globale (le fichier de configuration globale
est `~/.gitconfig`).


```bash
# Lit et assigne quelques variables (globales) de configuration de base
$ git config --global user.email "monEmail@foo.com"
$ git config --global user.name "Mon nom"
```

[Apprenez-en plus à propos de git config.](https://git-scm.com/book/fr/v1/Personnalisation-de-Git-Configuration-de-Git)

### help

Vous donne un accès rapide à un guide extrêmement détaillé de chaque commande.
Ou juste vous donner un rappel rapide de la sémantique.

```bash
# Vérifie rapidement les commandes disponibles
$ git help

# Vérifie toutes les commandes disponibles
$ git help -a

# Aide pour une commande spécifique - manuel utilisateur
# git help <command_here>
$ git help add
$ git help commit
$ git help init
# ou git <command_here> --help
$ git add --help
$ git commit --help
$ git init --help
```

### ignorer des fichiers

Ne plus suivre certains fichiers et dossiers de Git.
Habituellement fait pour les fichiers privés et temporaires qui seraient,
autrement, partagés dans le dépôt.
```bash
$ echo "temp/" >> .gitignore
$ echo "cle_privee" >> .gitignore
```

### status

Montre les différences entre le fichier indexé (typiquement votre copie/dépôt
de travail) et le HEAD actuel.


```bash
# Affiche la branche, les fichiers non suivis, les changements et autres
différences
$ git status

# Pour en apprendre plus sur git status
$ git help status
```

### add

Rajoute des fichiers à la zone d'index. Si vous ne faites pas `git add` sur les
nouveaux fichiers, ils ne seront pas inclus dans les commits !

```bash
# rajoute un fichier dans votre répertoire de travail actuel
$ git add HelloWorld.java

# rajoute un fichier dans un répertoire imbriqué
$ git add /path/to/file/HelloWorld.c

# Gestion des expressions régulières !
$ git add ./*.java
```

On ne fait que rajouter des fichiers dans la zone d'index, on ne valide pas
les changements au répertoire/dépôt de travail.

### branch

Gère vos branches. Vous pouvez voir, éditer, créer et supprimer des branches en
utilisant cette commande.

```bash
# Liste les branches existantes et distantes
$ git branch -a

# Créé une nouvelle branche
$ git branch maNouvelleBranche

# Supprime une branche
$ git branch -d maBranche

# Renomme une branche
# git branch -m <anciennom> <nouveaunom>
$ git branch -m nomDeMaBranche nouveauNomDeMaBranche

# Edite la description d'une branche
$ git branch nomDeMaBranche --edit-description
```

### tag

Gère vos étiquettes

```bash
# Liste les étiquettes
$ git tag

# Créé une étiquette annotée
# L'option -m spécifie un message qui sera stockée dans l'étiquette.
# Si vous ne spécifiez pas de message pour une étiquette annotée,
# Git lance votre éditeur pour que vous puissiez le saisir.
$ git tag -a v2.0 -m 'ma version 2.0'

# Affiche des informations à propos de l'étiquette
# comprenant des informations sur l'auteur, la date du commit correspondant,
# et le message d'annotation avant d'afficher les informations du commit.
$ git show v2.0

# Pousse une seule étiquette dans le dépôt distant
$ git push origin v2.0

# Pousse beaucoup d'étiquettes dans le dépôt distant
$ git push origin --tags
```

### checkout

Met à jour tous les fichiers dans l'arbre de travail afin de correspondre à la
version de la zone d'index ou de l'arbre spécifié.

```bash
# Obtenir une copie de travail du dépôt - par défaut on prend la branche master
$ git checkout

# Bascule vers une branche spéficiée
$ git checkout nomDeLaBranche

# Créé une nouvelle branche et bascule sur celle-ci
# Revient à faire "git branch <name>; git checkout <name>"
$ git checkout -b nouvelleBranche
```

### clone

Clone (ou copie) un dépôt existant dans un nouveau répertoire. Rajoute
également les branches distantes pour chaque branche du dépôt clôné, ce qui
vous permet de pousser vers une branche distante.

```bash
# Clone learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git

# Clone superficiel ("shallow clone") - clone plus rapide qui récupère
seulement le dernier instantané ("snapshot")
$ git clone --depth 1 https://github.com/adambard/learnxinyminutes-docs.git

# Clone seulement une branche spécifique
$ git clone -b master-cn https://github.com/adambard/learnxinyminutes-docs.git --single-branch
```

### commit

Conserve le contenu actuel de la zone d'index dans un nouveau "commit." Ce
commit contient les changements faits, accompagnés d'un message écrit par son
auteur.

```bash
# Commit avec un message
$ git commit -m "Ajout de la fonction multiplierNombres() dans HelloWorld.c"

# Rajoute automatiquement dans l'index les fichiers modifiés ou supprimés,
# à l'exception des nouveaux fichiers, puis commit
$ git commit -a -m "Modification de foo.php et suppression de bar.php"

# Change le dernier commit (ceci supprime le commit précédent avec un
# nouveau commit)
$ git commit --amend -m "Message corrigé"
```

### diff

Montre les différences entre un fichier dans le répertoire de travail, la zone
d'index and les commits.

```bash
# Affiche les différences entre votre répertoire de travail et l'index
$ git diff

# Affiche les différences entre l'index et le plus récent commit.
$ git diff --cached

# Affiche les différences entre votre répertoire de travail et le plus récent
# commit
$ git diff HEAD
```

### grep

Permet de faire une recherche rapide dans le dépôt.

Configurations optionnelles :

```bash
# Merci à Travis Jeffery pour ce qui suit
# Affiche les numéros des lignes dans les résultats de la recherche grep
$ git config --global grep.lineNumber true

# Rend les résultats de recherche plus lisibles, en incluant les groupements
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Recherche de "nomDeVariable" dans tous les fichiers java
$ git grep 'nomDeVariable' -- '*.java'

# Recherche une ligne contenant "nomDeTableau", et "rajouter" ou "enlever"
$ git grep -e 'nomDeTableau' --and \( -e rajouter -e enlever \)
```

Google est votre ami; pour plus d'exemples :
[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Affiche les commits d'un dépôt.

```bash
# Montre tous les commits
$ git log

# Montre seulement les messages de commits et leur référence
$ git log --oneline

# Montre seulement les commits commits des merges (fusions)
$ git log --merges
```

### merge

Fusionne les changements provenant de commits externes dans la branche
courante.

```bash
# Fusionne la branche spécifiée dans la branche courante.
$ git merge nomDeBranche

# Génère toujours un commit quand on fusionne
$ git merge --no-ff branchName
```

### mv

Renomme ou déplace un fichier

```bash
# Renomme un fichier
$ git mv HelloWorld.c HelloNewWorld.c

# Déplace un fichier
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# Force le renommage ou le déplacement
# Si "fichierExistant" existe déjà dans le répertoire, il sera écrasé
$ git mv -f monFichier fichierExistant
```

### pull

Récupère la version d'un dépôt et la fusionne avec une autre branche.

```bash
# Met à jour votre dépôt local en y intégrant les changements
# depuis la branche "master" du dépôt distant "origin".
# git pull <remote> <branch>
$ git pull origin master

# Par défaut, git pull mettra à jour votre branche actuelle
# en y intégrant les nouveaux changements venant de sa branche distante suivie
$ git pull

# Intègre les changements de la branche distante et "rebase"
# les commits de la branche dans votre dépôt local, comme ceci:
#"git pull <remote> <branch>, git rebase <branch>"
$ git pull origin master --rebase
```

### push

Pousse et fusionne les changements d'une dépôt local vers une branche distante.

```bash
# Pousse et fusionne les changements d'un dépôt local vers la branche
# appelée "master" du dépôt distant "master".
# git push <remote> <branch>
$ git push origin master

# Par défaut, git push poussera et fusionnera les changements de la branche
# courante vers sa branche distante suivie.
$ git push

# Pour faire le lien entre la branche locale courante et sa branche distante,
# rajouter l'option -u :
$ git push -u origin master
# Dorénavant, à chaque fois que vous voulez pousser depuis cette même branche
# locale, utilisez ce raccourci :
$ git push
```

### stash

Sauvegarde ("stash") l'état actuel de votre espace de travail et le garde dans
pile de changements non finis que vous pouvez réappliquer n'importe quand.

Supposons que vous avez effectué du travail dans votre dépôt git, mais que vous
voulez récupérer la version de la branche distante. Depuis que vous avez des
changements "malpropres" (non commités) à quelques fichiers, vous ne pouvez pas
faire de `git pull`. A la place, vous pouvez utiliser `git stash` afin de
sauvegarder votre travail dans la pile !

```bash
$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 added the index file"
  HEAD is now at 049d078 added the index file
  (To restore them type "git stash apply")
```

Vous pouvez maintenant pull !

```bash
git pull
```
`...changes apply...`

Vérifiez maintenant que tout est OK

```bash
$ git status
# On branch master
nothing to commit, working directory clean
```

Vous pouvez constater quels "morceaux" vous avez stash jusque là en
utilisant `git stash list`.
Puisque les changements sont gardés dans une pile Last-In-First-Out, notre
changement le plus récent sera en premier.

```bash
$ git stash list
stash@{0}: WIP on master: 049d078 rajout du fichier index
stash@{1}: WIP on master: c264051 annulation de "rajout de la taille_fichier"
stash@{2}: WIP on master: 21d80a5 ajout des chiffres aux logs
```

Appliquons maintenant les changements en les enlevant de notre pile.

```bash
$ git stash pop
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   index.html
#      modified:   lib/simplegit.rb
#
```

`git stash apply` effectue le même travail

Vous êtes maintenant prêt à retourner sur vos tâches de travail !

[Lecture additionelle.](https://git-scm.com/book/fr/v1/Utilitaires-Git-Le-remisage)

### rebase (attention)

Prend tous les changements qui ont été commités sur une branche, et les
ré-applique sur une autre branche.
*Ne rebasez pas les commits que vous avez poussés sur un dépôt publique*.

```bash
# Expérimentation d'un rebase dans la branche "master"
# git rebase <basebranch> <topicbranch>
$ git rebase master brancheExperience
```

[Lecture additionelle.](https://git-scm.com/book/fr/v1/Les-branches-avec-Git-Rebaser)

### reset (attention)

Réinitialise le pointeur HEAD courant à l'état spécifié. Ceci vous permet
d'annuler des fusions, des pulls, commits, ajouts et autres. C'est une commande
puissante mais également dangereuse si vous ne savez pas ce que vous faites.

```bash
# Réinitialise la zone d'index afin de correspondre au dernier commit (laisse
# le répertoire inchangé).
$ git reset

# Réinitialise la zone d'index afin de correspondre au dernier commit et
# réécrit le répertoire de travail.
$ git reset --hard

# Déplace le pointeur de la branche courante au commit spécifié (laisse
# le répertoire inchangé). Tous les changements existents toujours dans
# le répertoire.
$ git reset 31f2bb1

# Déplace le pointeur de la branche courante en arrière, au commit spécifié
# et fait correspondre le répertoire de travail (supprime les changements
# non commités et tous les commits après le commit spécifié).
$ git reset --hard 31f2bb1
```

### rm

Le contraire de git add, git rm supprime les fichiers de l'arbre de travail
courant.

```bash
# Supprime HelloWorld.c
$ git rm HelloWorld.c

# Enlève un fichier d'un répertoire imbriqué.
$ git rm /chemin/vers/le/fichier/HelloWorld.c
```

## Informations complémentaires

* [tryGit - A fun interactive way to learn Git (EN)](http://try.github.io/levels/1/challenges/1)

* [Udemy Git Tutorial: A Comprehensive Guide (EN)](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/)

* [git-scm - Tutoriaux vidéos](http://git-scm.com/videos)

* [git-scm - Documentation](http://git-scm.com/docs)

* [Atlassian Git - Tutoriaux et Workflows](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet (EN)](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys (EN)](http://www.gitguys.com/)

* [Git - the simple guide (EN)](http://rogerdudler.github.io/git-guide/index.html)

* [Livre Pro Git](http://www.git-scm.com/book/fr/v1)

* [Une introduction à Git et GitHub pour les débutants (tutoriel) (EN)](http://product.hubspot.com/blog/git-and-github-tutorial-for-beginners)
---
name: Go
category: language
language: Go
lang: fr-fr
filename: learngo-fr.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
    - ["Alexej Friesen", "https://github.com/heyalexej"]
    - ["Jean-Philippe Monette", "http://blogue.jpmonette.net/"]
---

Go a été créé dans l'optique de développer de façon efficace. Ce n'est pas la
dernière tendance en ce qui est au développement, mais c'est la nouvelle façon
de régler des défis réels de façon rapide.

Le langage possède des concepts familiers à la programmation impérative avec
typage. Il est rapide à compiler et exécuter, ajoute une concurrence facile à
comprendre, pour les processeurs multi coeurs d'aujourd'hui et apporte des
fonctionnalités facilitant le développement à grande échelle.

Développer avec Go, c'est bénéficier d'une riche bibliothèque standard et d'une
communauté active.

```go
// Commentaire ligne simple
/* Commentaire
 multiligne */

// Un paquet débute avec une clause "package"
// "Main" est un nom spécial déclarant un paquet de type exécutable plutôt
// qu'une bibliothèque
package main

// "Import" déclare les paquets référencés dans ce fichier.
import (
  "fmt"       // Un paquet dans la bibliothèque standard.
  "io/ioutil" // Implémente des fonctions utilitaires I/O.
  m "math"    // Bibliothèque mathématique utilisant un alias local "m".
  "net/http"  // Un serveur Web!
  "strconv"   // Bibliothèque pour convertir les chaînes de caractères.
)

// Une définition de fonction. La fonction "main" est spéciale - c'est le point
// d'entrée du binaire.
func main() {
  // Println retournera la valeur à la console.
  // Associez la fonction avec son paquet respectif, fmt.
  fmt.Println("Hello world!")

  // Appelez une fonction différente à partir de ce paquet.
  beyondHello()
}

// Les fonctions ont des paramètres entre parenthèses.
// Les parenthèses sont nécessaires avec ou sans paramètre.
func beyondHello() {
  var x int // Déclaration de variable. Les variables doivent être déclarées
            // avant leur utilisation.
  x = 3     // Assignation de valeur.
  // Les déclarations courtes utilisent := pour inférer le type, déclarer et
  // assigner.
  y := 4
  sum, prod := learnMultiple(x, y)        // La fonction retourne deux valeurs.
  fmt.Println("sum:", sum, "prod:", prod) // Affichage simple.
  learnTypes()                            // < y minutes, en savoir plus!
}

// Les fonctions peuvent avoir des paramètres et plusieurs valeurs retournées.
func learnMultiple(x, y int) (sum, prod int) {
  return x + y, x * y // Deux valeurs retournées.
}

// Quelques types inclus et littéraux.
func learnTypes() {
  // Une déclaration courte infère généralement le type désiré.
  str := "Learn Go!" // Type string.

  s2 := `Une chaîne de caractères peut contenir des
sauts de ligne.` // Chaîne de caractère.

  // Littéral non-ASCII. Les sources Go utilisent le charset UTF-8.
  g := 'Σ' // type rune, un alias pour le type int32, contenant un caractère
           // unicode.

  f := 3.14195 // float64, un nombre flottant IEEE-754 de 64-bit.
  c := 3 + 4i  // complex128, considéré comme deux float64 par le compilateur.

  // Syntaxe "var" avec une valeur d'initialisation.
  var u uint = 7 // Non signé, mais la taille dépend selon l'entier.
  var pi float32 = 22. / 7

  // Conversion avec syntaxe courte.
  n := byte('\n') // byte est un alias du type uint8.

  // Les tableaux ont une taille fixe déclarée à la compilation.
  var a4 [4]int           // Un tableau de 4 ints, tous initialisés à 0.
  a3 := [...]int{3, 1, 5} // Un tableau initialisé avec une taille fixe de 3
  // éléments, contenant les valeurs 3, 1 et 5.

  // Les slices ont des tailles dynamiques. Les tableaux et slices ont chacun
  // des avantages, mais les cas d'utilisation des slices sont plus fréquents.
  s3 := []int{4, 5, 9}    // Comparable à a3.
  s4 := make([]int, 4)    // Alloue un slice de 4 ints, initialisés à 0.
  var d2 [][]float64      // Déclaration seulement, sans allocation de mémoire.
  bs := []byte("a slice") // Conversion d'une chaîne en slice de bytes.

  // Parce qu'elles sont dynamiques, les slices peuvent être jointes sur
  // demande. Pour joindre un élément à une slice, la fonction standard append()
  // est utilisée. Le premier argument est la slice à utiliser. Habituellement,
  // la variable tableau est mise à jour sur place, voir ci-bas.
  s := []int{1, 2, 3}     // Le résultat est une slice de taille 3.
  s = append(s, 4, 5, 6)  // Ajout de 3 valeurs. La taille est de 6.
  fmt.Println(s)          // La valeur est de [1 2 3 4 5 6]

  // Pour ajouter une slice à une autre, au lieu d'utiliser une liste de valeurs
  // atomiques, il est possible de mettre en argument une référence de
  // slice littérale grâce aux points de suspension.
  s = append(s, []int{7, 8, 9}...) // Le deuxième argument est une slice
                                   // littérale.
  fmt.Println(s)  // La slice contient [1 2 3 4 5 6 7 8 9]

  p, q := learnMemory() // Déclare p, q comme étant des pointeurs de type int.
  fmt.Println(*p, *q)   // * suit un pointeur. Ceci retourne deux ints.

  // Les maps sont des tableaux associatifs de taille dynamique, comme les
  // hash ou les types dictionnaires de certains langages.
  m := map[string]int{"trois": 3, "quatre": 4}
  m["un"] = 1

  // Les valeurs inutilisées sont considérées comme des erreurs en Go.
  // Un tiret bas permet d'ignorer une valeur inutilisée, évitant une erreur.
  _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs

  // Cependant, son affichage en console est considéré comme une utilisation,
  // ce qui ne sera pas considéré comme une erreur à la compilation.
  fmt.Println(s, c, a4, s3, d2, m)

  learnFlowControl() // De retour dans le flux.
}

// Il est possible, à l'opposé de plusieurs autres langages, de retourner des
// variables par leur nom à partir de fonctions.
// Assigner un nom à un type retourné par une fonction permet de retrouver sa
// valeur ainsi que d'utiliser le mot-clé "return" uniquement, sans plus.
func learnNamedReturns(x, y int) (z int) {
  z = x * y
  return // z est implicite, car la variable a été définie précédemment.
}

// La récupération de la mémoire est automatique en Go. Le langage possède des
// pointeurs, mais aucune arithmétique des pointeurs (*(a + b) en C). Vous
// pouvez produire une erreur avec un pointeur nil, mais pas en incrémentant un
// pointeur.
func learnMemory() (p, q *int) {
  // Les valeurs retournées p et q auront le type pointeur int.
  p = new(int) // Fonction standard "new" alloue la mémoire.
  // Le int alloué est initialisé à 0, p n'est plus nil.
  s := make([]int, 20) // Alloue 20 ints en un seul bloc de mémoire.
  s[3] = 7             // Assigne l'un des entiers.
  r := -2              // Déclare une autre variable locale.
  return &s[3], &r     // & retourne l'adresse d'un objet.
}

func expensiveComputation() float64 {
  return m.Exp(10)
}

func learnFlowControl() {
  // Bien que les "if" requièrent des accolades, les parenthèses ne sont pas
  // nécessaires pour contenir le test booléen.
  if true {
    fmt.Println("voilà!")
  }
  // Le formatage du code est standardisé par la commande shell "go fmt."
  if false {
    // bing.
  } else {
    // bang.
  }
  // Utilisez "switch" au lieu des "if" en chaîne
  x := 42.0
  switch x {
  case 0:
  case 1:
  case 42:
    // Les "case" n'ont pas besoin de "break;".
  case 43:
    // Non-exécuté.
  }
  // Comme les "if", les "for" n'utilisent pas de parenthèses.
  // Les variables déclarées dans les "for" et les "if" sont locales à leur
  // portée.
  for x := 0; x < 3; x++ { // ++ est une incrémentation.
    fmt.Println("itération ", x)
  }
  // x == 42 ici.

  // "For" est le seul type de boucle en Go, mais possède différentes formes.
  for { // Boucle infinie
    break    // C'est une farce
    continue // Non atteint.
  }

  // Vous pouvez utiliser une "range" pour itérer dans un tableau, une slice, une
  // chaîne, une map ou un canal. Les "range" retournent un canal ou deux
  // valeurs (tableau, slice, chaîne et map).
  for key, value := range map[string]int{"une": 1, "deux": 2, "trois": 3} {
    // pour chaque pair dans une map, affichage de la valeur et clé
    fmt.Printf("clé=%s, valeur=%d\n", key, value)
  }

  // À l'opposé du "for", := dans un "if" signifie la déclaration et
  // l'assignation y en premier, et ensuite y > x
  if y := expensiveComputation(); y > x {
    x = y
  }
  // Les fonctions littérales sont des fermetures.
  xBig := func() bool {
    return x > 10000
  }
  fmt.Println("xBig:", xBig()) // true (la valeur e^10 a été assignée à x).
  x = 1.3e3                    // Ceci fait x == 1300
  fmt.Println("xBig:", xBig()) // Maintenant false.

  // De plus, les fonctions littérales peuvent être définies et appelées
  // sur la même ligne, agissant comme argument à cette fonction, tant que:
  // a) la fonction littérale est appelée suite à (),
  // b) le résultat correspond au type de l'argument.
  fmt.Println("Ajoute + multiplie deux nombres : ",
    func(a, b int) int {
      return (a + b) * 2
    }(10, 2)) // Appelle la fonction avec les arguments 10 et 2
  // => Ajoute + double deux nombres : 24

  // Quand vous en aurez besoin, vous allez l'adorer.
  goto love
love:

  learnFunctionFactory() // func retournant func correspondant à fun(3)(3).
  learnDefer()           // Un survol de cette instruction importante.
  learnInterfaces()      // Incontournable !
}

func learnFunctionFactory() {
  // Les deux syntaxes sont identiques, bien que la seconde soit plus pratique.
  fmt.Println(sentenceFactory("été")("Une matinée d'", "agréable!"))

  d := sentenceFactory("été")
  fmt.Println(d("Une matinée d'", "agréable!"))
  fmt.Println(d("Une soirée d'", "relaxante!"))
}

// Le décorateur est un patron de conception commun dans d'autres langages.
// Il est possible de faire de même en Go avec des fonctions littérales
// acceptant des arguments.
func sentenceFactory(mystring string) func(before, after string) string {
  return func(before, after string) string {
    return fmt.Sprintf("%s %s %s", before, mystring, after) // nouvelle chaîne
  }
}

func learnDefer() (ok bool) {
  // Les déclarations différées sont exécutées avant la sortie d'une fonction.
  defer fmt.Println("les déclarations différées s'exécutent en ordre LIFO.")
  defer fmt.Println("\nCette ligne est affichée en premier parce que")
  // Les déclarations différées sont utilisées fréquemment pour fermer un
  // fichier, afin que la fonction ferme le fichier en fin d'exécution.
  return true
}

// Défini Stringer comme étant une interface avec une méthode, String.
type Stringer interface {
  String() string
}

// Défini pair comme étant une structure contenant deux entiers, x et y.
type pair struct {
  x, y int
}

// Défini une méthode associée au type pair. Pair implémente maintenant Stringer
func (p pair) String() string { // p s'appelle le "destinataire"
  // Sprintf est une autre fonction publique dans le paquet fmt.
  // La syntaxe avec point permet de faire référence aux valeurs de p.
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
  // La syntaxe avec accolade défini une "structure littérale". Celle-ci
  // s'évalue comme étant une structure. La syntaxe := déclare et initialise p
  // comme étant une instance.
  p := pair{3, 4}
  fmt.Println(p.String()) // Appelle la méthode String de p, de type pair.
  var i Stringer          // Déclare i instance de l'interface Stringer.
  i = p                   // Valide, car pair implémente Stringer.
  // Appelle la méthode String de i, de type Stringer. Retourne la même valeur
  // que ci-haut.
  fmt.Println(i.String())

  // Les fonctions dans le paquet fmt appellent la méthode String, demandant
  // aux objets d'afficher une représentation de leur structure.
  fmt.Println(p) // Affiche la même chose que ci-haut. Println appelle la
                 // méthode String.
  fmt.Println(i) // Affiche la même chose que ci-haut.

  learnVariadicParams("apprentissage", "génial", "ici!")
}

// Les fonctions peuvent être définie de façon à accepter un ou plusieurs
// paramètres grâce aux points de suspension, offrant une flexibilité lors de
// son appel.
func learnVariadicParams(myStrings ...interface{}) {
  // Itère chaque paramètre dans la range.
  // Le tiret bas sert à ignorer l'index retourné du tableau.
  for _, param := range myStrings {
    fmt.Println("paramètre:", param)
  }

  // Passe une valeur variadique comme paramètre variadique.
  fmt.Println("paramètres:", fmt.Sprintln(myStrings...))

  learnErrorHandling()
}

func learnErrorHandling() {
  // ", ok" idiome utilisée pour définir si l'opération s'est déroulée avec
  // succès ou non
  m := map[int]string{3: "trois", 4: "quatre"}
  if x, ok := m[1]; !ok { // ok sera faux, car 1 n'est pas dans la map.
    fmt.Println("inexistant")
  } else {
    fmt.Print(x) // x serait la valeur, si elle se trouvait dans la map.
  }
  // Une erreur ne retourne qu'un "ok", mais également plus d'information
  // par rapport à un problème survenu.
  if _, err := strconv.Atoi("non-int"); err != nil { // _ discarte la valeur
    // retourne: 'strconv.ParseInt: parsing "non-int": invalid syntax'
    fmt.Println(err)
  }
  // Nous réviserons les interfaces un peu plus tard. Pour l'instant,
  learnConcurrency()
}

// c est un canal, un objet permettant de communiquer en simultané de façon
// sécurisée.
func inc(i int, c chan int) {
  c <- i + 1 // <- est l'opérateur "envoi" quand un canal apparaît à
             // gauche.
}

// Nous utiliserons inc pour incrémenter des nombres en même temps.
func learnConcurrency() {
  // La fonction "make" utilisée précédemment pour générer un slice. Elle
  // alloue et initialise les slices, maps et les canaux.
  c := make(chan int)
  // Démarrage de trois goroutines simultanées. Les nombres seront incrémentés
  // simultanément, peut-être en paralèle si la machine le permet et configurée
  // correctement. Les trois utilisent le même canal.
  go inc(0, c) // go est une instruction démarrant une nouvelle goroutine.
  go inc(10, c)
  go inc(-805, c)
  // Lis et affiche trois résultats du canal - impossible de savoir dans quel
  // ordre !
  fmt.Println(<-c, <-c, <-c) // Canal à droite, <- est l'opérateur de
                             // "réception".

  cs := make(chan string)       // Un autre canal, celui-ci gère des chaînes.
  ccs := make(chan chan string) // Un canal de canaux de chaînes.
  go func() { c <- 84 }()       // Démarre une nouvelle goroutine, pour
                                // envoyer une valeur.
  go func() { cs <- "wordy" }() // De nouveau, pour cs cette fois-ci.
  // Select possède une syntaxe similaire au switch, mais chaque cas requiert
  // une opération impliquant un canal. Il sélectionne un cas aléatoirement
  // prêt à communiquer.
  select {
  case i := <-c: // La valeur reçue peut être assignée à une variable,
    fmt.Printf("c'est un %T", i)
  case <-cs: // ou la valeur reçue peut être ignorée.
    fmt.Println("c'est une chaîne")
  case <-ccs: // Un canal vide, indisponible à la communication.
    fmt.Println("ne surviendra pas.")
  }
  // À ce point, une valeur a été prise de c ou cs. L'une des deux goroutines
  // démarrée plus haut a complétée, la seconde restera bloquée.

  learnWebProgramming() // Go permet la programmation Web.
}

// Une seule fonction du paquet http démarre un serveur Web.
func learnWebProgramming() {

  // Le premier paramètre de ListenAndServe est une adresse TCP à écouter.
  // Le second est une interface, de type http.Handler.
  go func() {
    err := http.ListenAndServe(":8080", pair{})
    fmt.Println(err) // n'ignorez pas les erreurs !
  }()

  requestServer()
}

// Implémente la méthode ServeHTTP de http.Handler à pair, la rendant compatible
// avec les opérations utilisant l'interface http.Handler.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  // Répondez à une requête à l'aide de la méthode http.ResponseWriter.
  w.Write([]byte("Vous avez appris Go en Y minutes!"))
}

func requestServer() {
  resp, err := http.Get("http://localhost:8080")
  fmt.Println(err)
  defer resp.Body.Close()
  body, err := ioutil.ReadAll(resp.Body)
  fmt.Printf("\nLe serveur Web a dit: `%s`", string(body))
}
```

## En savoir plus

La référence Go se trouve sur [le site officiel de Go](http://golang.org/).
Vous pourrez y suivre le tutoriel interactif et en apprendre beaucoup plus.

Une lecture de la documentation du langage est grandement conseillée. C'est
facile à lire et très court (comparé aux autres langages).

Vous pouvez exécuter et modifier le code sur [Go playground](https://play.golang.org/p/tnWMjr16Mm). Essayez de le modifier et de l'exécuter à partir de votre navigateur! Prennez en note que vous pouvez utiliser [https://play.golang.org](https://play.golang.org) comme un [REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) pour tester et coder dans votre navigateur, sans même avoir à installer Go.

Sur la liste de lecteur des étudiants de Go se trouve le [code source de la
librairie standard](http://golang.org/src/pkg/). Bien documentée, elle démontre
le meilleur de la clarté de Go, le style ainsi que ses expressions. Sinon, vous
pouvez cliquer sur le nom d'une fonction dans [la
documentation](http://golang.org/pkg/) et le code source apparaît!

Une autre excellente ressource pour apprendre est [Go par l'exemple](https://gobyexample.com/).
---
language: haml
filename: learnhaml-fr.haml
contributors:
  - ["Simon Neveu", "https://github.com/sneveu"]
  - ["Thibault", "https://github.com/iTech-"]
lang: fr-fr
---

Haml est un langage de balisage utilisé majoritairement avec Ruby, qui décrit de manière simple et propre le HTML de n'importe quelle page web sans l'utilisation des traditionnelles lignes de code. Le langage est une alternative très populaire au langage de templates Rails (.erb) et permet d'intégrer du code en Ruby dans votre balisage.

Son but est de réduire le nombre de répétitions dans le balisage en fermant des balises pour vous en se basant sur l'indentation de votre code. Finalement, le balisage est bien structuré, ne contient pas de répétition, est logique et facile à lire.

Vous pouvez aussi utiliser Haml sur un projet indépendant de Ruby, en installant les gems de Haml et en le convertissant en html grâce aux commandes.

$ haml fichier_entree.haml fichier_sortie.html


```haml
/ -------------------------------------------
/ Indentation 
/ -------------------------------------------

/
  A cause de l'importance de l'indentation sur la manière dont votre code sera
  converti, l'indentation doit être constante à travers votre document. Un 
  simple changement d'indentation entrainera une erreur. En général, on utilise
  deux espaces, mais ce genre de décision sur l'indentation vous appartient, du
  moment que vous vous y tenez.

/ -------------------------------------------
/ Commentaires
/ -------------------------------------------

/ Ceci est un commentaire en Haml.

/
  Pour écrire un commentaire sur plusieurs lignes, indentez votre code
  commenté en le commençant par un slash

-# Ceci est un commentaire silencieux, qui n'apparaîtra pas dans le fichier


/ -------------------------------------------
/ Eléments HTML
/ -------------------------------------------

/ Pour écrire vos balises, utilisez un pourcentage suivi du nom de votre balise
%body
  %header
    %nav

/ Remarquez qu'il n'y a aucunes balises fermées. Le code produira alors ceci
  <body>
    <header>
      <nav></nav>
    </header>
  </body>

/ La balise div est l'élément par défaut, vous pouvez donc l'écrire comme ceci
.balise

/ Pour ajouter du contenu à votre balise, ajoutez le texte après sa déclaration
%h1 Titre contenu

/ Pour écrire du contenu sur plusieurs lignes, imbriquez le
%p
  Ce paragraphe contient beaucoup de contenu qui pourrait
  probablement tenir sur deux lignes séparées.

/
  Vous pouvez utiliser des caractères html spéciaux en utilisant &=. Cela va
  convertir les caractères comme &, /, : en leur équivalent HTML. Par exemple

%p
  &= "Oui & oui"

/ Produira 'Oui &amp; oui'

/ Vous pouvez écrire du contenu html sans qu'il soit converti en utilisant !=
%p
  != "Voici comment écrire une balise de paragraphe <p></p>"

/ Cela produira 'Voici comment écrire une balise de paragraphe <p></p>'

/ Une classe CSS peut être ajouté à votre balise en chainant le nom de la classe
%div.truc.machin

/ ou en utilisant un hash de Ruby
%div{:class => 'truc machin'}

/ Des attributs pour n'importe quelles balises peuvent être ajoutés au hash
%a{:href => '#', :class => 'machin', :title => 'Titre machin'}

/ Pour affecter une valeur à un booléen, utilisez 'true' 
%input{:selected => true}

/ Pour écrire des data-attributes, utilisez le :data avec la valeur d'un hash
%div{:data => {:attribute => 'machin'}}


/ -------------------------------------------
/ Insérer du Ruby
/ -------------------------------------------

/
  Pour transférer une valeur de Ruby comme contenu d'une balise, utilisez le
  signe égal suivi du code Ruby

%h1= livre.titre

%p
  = livre.auteur
  = livre.editeur


/ Pour lancer du code Ruby sans le convertir en HTML, utilisez un trait d'union
- livres = ['livre 1', 'livre 2', 'livre 3']

/ Ceci vous permet de faire des choses géniales comme des blocs Ruby
- livre.shuffle.each_with_index do |livre, index|
  %h1= livre 

  if livre do
    %p Ceci est un livre

/
  Encore une fois il n'est pas nécessaire d'ajouter une balise fermante, même
  pour Ruby.
  L'indentation le fera pour vous.


/ -------------------------------------------
/ Ruby en-ligne / Interpolation en Ruby
/ -------------------------------------------

/ Inclure une variable Ruby dans une ligne en utilisant #{}
%p Votre meilleur score est #{record}


/ -------------------------------------------
/ Filtres
/ -------------------------------------------

/
  Utilisez les deux points pour définir un filtre Haml, vous pouvez par exemple
  utiliser un filtre :javascript pour écrire du contenu en-ligne js

:javascript
  console.log('Ceci est la balise en-ligne <script>');

```

## Lectures complémentaires

- [Qu'est-ce que HAML ?](http://haml.info/) - Une bonne introduction qui explique très bien les avantages d'utiliser HAML.
- [Documentation officielle](http://haml.info/docs/yardoc/file.REFERENCE.html) - Si vous souhaitez en apprendre plus et aller plus loin.
---
language: Haskell
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["David Baumgartner", "http://davidbaumgartner.ch"]    
lang: fr-fr
filename: learnhaskell-fr.hs
---

Haskell a été conçu pour être un langage fonctionnel pur et maniable. Il est connu pour ses monades et son système de types, mais je n'ai cesse d'y revenir pour son élégance. Pour moi, Haskell fait de la programmation une joie.

```haskell
-- Un commentaire en une ligne commence avec deux tirets.
{- Un commentaire sur plusieurs lignes peut être contenu dans
un bloc de cette façon.
-}

----------------------------------------------------
-- 1. Types de données primitifs et opérateurs
----------------------------------------------------

-- Vous avez les nombres
3 -- 3

-- Les maths sont comme vous vous y attendez
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- La division n'est pas entière par défaut
35 / 4 -- 8.75

-- division entière
35 `div` 4 -- 8

-- Les booléens sont primitifs
True
False

-- Opérations avec les booléens
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- Dans les exemples plus hauts, `not` est une fonction qui prend une valeur.
-- Haskell n'a pas besoin de parenthèses pour appeler une fonction... tous
-- les arguments sont juste listés après la fonction. Le schéma général est
-- donc :
-- func arg1 arg2 arg3...
-- Voyez la section sur les fonctions pour savoir comment écrire les vôtres.

-- Caractères et chaînes de caractère
"Ceci est une chaîne de caractère."
'a' -- caractère
'Vous ne pouvez pas utiliser des apostrophes pour les chaînes de caractère.' -- erreur !

-- Les chaînes peuvent être concaténées
"Hello " ++ "world!" -- "Hello world!"

-- Une chaîne de caractère est *réellement* une liste
"Ceci est une chaîne." !! 0 -- 'C'


----------------------------------------------------
-- 2. Listes et tuples
----------------------------------------------------

-- Tous les éléments d'une liste doit avoir le même type.
-- les deux lignes suivantes sont semblables
[1, 2, 3, 4, 5]
[1..5]

-- Il y a aussi des listes infinies en Haskell !
[1..] -- une liste de tous les nombres naturels

-- Les listes infinies fonctionnent parce que Haskell est « paresseux »:
-- ça veut dire qu'il n'évalue que ce qui a besoin de l'être. Vous pouvez
-- donc vous demander le 1000e élément de votre liste et il vous le donnera :

[1..] !! 999 -- 1000

-- Et là, Haskell a évalué les éléments 1 à 1000 de la liste... mais le reste
-- de cette liste « infinie » n'existe pas encore ! En fait, Haskell ne va jamais 
-- le faire à moins qu'il ne le doive.

-- Adjoindre deux listes 
[1..5] ++ [6..10]

-- ajouter au début de la liste
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- l'indice d'une liste
[0..] !! 5 -- 5

-- d'autres opérations sur les listes
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

--liste en compréhension
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

--avec un conditionnel
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- Chaque élément d'un tuple peut être d'un type différent, mais un
-- tuple a une longueur fixée.
-- Un tuple :
("haskell", 1)

-- accéder aux éléments d'un tuple
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. Functions
----------------------------------------------------
-- Une simple fonction qui prend deux paramètres
add a b = a + b

-- Notez que si vous utilisez ghci (l'interpréteur Haskell)
-- vous devrez utiliser `let`. Par exemple :
-- let add a b = a + b

-- Utiliser une fonction
add 1 2 -- 3

-- Vous pouvez également mettre le nom de la fonction entre les
-- deux arguments avec des accents graves :
1 `add` 2 -- 3

-- Vous pouvez également définir des fonctions qui n'ont pas de
-- lettres ! Ça vous laisse créer vos propres opérateurs ! Voilà 
-- un opérateur qui fait une division entière :
(//) a b = a `div` b
35 // 4 -- 8

-- Gardes : Une façon de gérer la valeur de vos arguments en amont
fib x
  | x < 2 = x
  | otherwise = fib (x - 1) + fib (x - 2)

-- Le filtrage par motif est similaire. Là, on a donné trois 
-- définitions différentes de `fib`. Haskell appellera automatiquement
-- la première fonction qui correspond au motif de la valeur.
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- Filtrage par motif sur un tuple.
foo (x, y) = (x + 1, y + 2)

-- Filtrage par motif sur des listes. Ici, `x` est le premier
-- élément de la liste, et `xs` le reste. On peut écrire notre
-- propre fonction `map` :
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- Les fonctions anonymes sont créées avec des barres obliques 
-- inverses, suivies de tous les arguments.
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- Une utilisation de fold (appelée `inject` dans quelques autres
-- langages) avec comme paramètre une fonction anonyme.
-- `foldl1` veut dire fold left -- soit littéralement pli gauche --
-- et utilise la première valeur de la liste comme accumulateur.
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. Plus de fonctions
----------------------------------------------------

-- curryfication : si vous n'appliquez pas tous les arguments à une
-- fonction, elle devient « curryfiée ». Ça veut dire qu'elle retourne
-- une fonction qui prend le reste des arguments.

add a b = a + b
foo = add 10 -- foo est une fonction qui prend un nombre et y ajoute 10
foo 5 -- 15

-- Une autre façon de l'écrire
foo = (+10)
foo 5 -- 15

-- Composition de fonctions
-- la fonction (.) enchaîne deux fonctions.
-- Par exemple, on a foo qui est une fonction qui prend une valeur, y ajoute
-- 10 et multiplie ce résultat par 5, et ensuite retourne la valeur finale.
foo = (*5) . (+10)

-- (5 + 10) * 5 = 75
foo 5 -- 75

-- fixation de priorité
-- Haskell a une autre fonction appelée `$`. Elle peut changer la priorité
-- de sorte que tout ce qu'il y a à sa gauche est calculé d'abord et ensuite 
-- appliqué à tout ce qu'il y a à droite. Vous pouvez utiliser `.` et `$` 
-- pour vous débarrasser de beaucoup de parenthèses :

-- avant
(even (fib 7)) -- False

-- ensuite
even . fib $ 7 -- False

----------------------------------------------------
-- 5. Signature de type
----------------------------------------------------

-- Haskell a un système de types très strict : par exemple, tout a un type.

-- Quelques types simples :
5 :: Integer
"hello" :: String
True :: Bool

-- Les fonctions ont également des types.
-- `not` prend un booléen et retourne un booléen.
-- not :: Bool -> Bool

-- Voilà une fonction qui prend deux paramètres.
-- add :: Integer -> Integer -> Integer

-- Quand vous définissez une valeur (souvenez-vous, tout est valeur en
-- Haskell), une bonne pratique est d'écrire son type explicitement
double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. Flux de contrôle et structures conditionnelles
----------------------------------------------------

-- structure conditionnelle if
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- les structures if peuvent être écrites sur plusieurs lignes
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- les structures case : voilà comment vous pourriez analyser les arguments de 
-- ligne de commande
case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"


-- Haskell n'a pas de boucles parce qu'il utilise la récursion.
-- `map` applique une fonction sur chaque élément d'une liste

map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- vous pouvez créer une fonction `for` en utilisant `map`
for array func = map func array

-- et l'utiliser
for [0..5] $ \i -> show i

-- nous aurions pu l'écrire également ainsi
for [0..5] show

-- vous pouvez utiliser foldl et foldr pour 
-- réduire une liste
-- foldl <fonction> <valeur initiale> <liste>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- C'est donc la même chose que 
(2 * (2 * (2 * 4 + 1) + 2) + 3)

-- foldl évalue de gauche à droite, foldr
-- de droite à gauche
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- Et c'est équivalent à
(2 * 3 + (2 * 2 + (2 * 1 + 4)))

----------------------------------------------------
-- 7. Types de données
----------------------------------------------------

-- Vous pouvez écrire vos propres types de données en Haskell

data Couleur = Rouge | Bleu | Vert

-- Et maintenant l'utiliser dans une fonction


say :: Couleur -> String
say Rouge = "Vous êtes Rouge !"
say Bleu = "Vous êtes Bleu !"
say Vert =  "Vous êtes Vert !"

-- Vos types peuvent également avoir des paramètres

data Maybe a = Nothing | Just a

-- Tous les exemples ci-dessous sont issus du type Maybe
Just "hello"    -- of type `Maybe String`
Just 1          -- of type `Maybe Int`
Nothing         -- of type `Maybe a` for any `a`

----------------------------------------------------
-- 8. Haskell IO
----------------------------------------------------

-- Tandis que l'IO ne peut pas être totalement expliqué pleinement
-- sans que les monades ne le soient, il n'est pas difficile
-- d'expliquer suffisamment pour commencer.

-- Quand un programme en Haskell est exécuté, la fonction `main`
-- est appelée. Il doit retourner une valeur de type `IO ()`.
-- Par exemple :

main :: IO ()
main = putStrLn $ "Bonjour, le ciel ! " ++ (say Blue) 
-- putStrLn a comme type String -> IO ()

-- La façon la plus simple pour faire de l'IO est de faire un programme 
-- fonction de String vers String. La fonction
--    interact :: (String -> String) -> IO ()
-- prend un texte, applique une fonction et affiche le résultat.

countLines :: String -> String
countLines = show . length . lines

main' = interact countLines

-- Vous pouvez considérer qu'une valeur de type `IO ()` représente
-- une séquence d'actions que l'ordinateur exécute, un peu comme 
-- dans un langage impératif. On peut utiliser la structure `do` 
-- pour enchaîner des actions. Par exemple :

sayHello :: IO ()
sayHello = do 
   putStrLn "Quel est ton nom ?"
   name <- getLine -- prend une ligne et assigne sa valeur à `name`
   putStrLn $ "Salut, " ++ name
   
-- Exercice : écrire votre propre version d'`interact` qui ne fait 
--           que de lire une ligne d'entrée.
   
-- Le code de `sayHello` ne sera jamais exécuté, cependant. La seule
-- action qui sera exécutée est la valeur de `main`.
-- Pour lancer `sayHello`, commentez l'ancienne définition de `main`
-- et remplacez-le par :
--   main = sayHello

-- Essaions de mieux comprendre comment la fonction `getLine` que 
-- nous venons d'utiliser. Son type est :
--    getLine :: IO String
-- vous pouvez considérer le type `IO a` comme un programme que
-- le programme va générer comme une valeur de type `a` quand
-- il sera exécuté. On peut l'enregistrer et la réutiliser en
-- utilisant `<-`. On peut aussi faire nos propres actions
-- de type `IO String` :

action :: IO String
action = do
   putStrLn "C'est une ligne. Heu"
   input1 <- getLine 
   input2 <- getLine
   -- Le type de la structure `do` est celui de sa dernière ligne.
   -- `return` n'est pas un mot clef, mais simplement une fonction.
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- On peut maintenant l'utiliser comme on a utilisé `getLine`
-- tout à l'heure

main'' = do
    putStrLn "Je vais afficher deux lignes !"
    result <- action 
    putStrLn result
    putStrLn "C'était tout !"

-- Le type `IO` est un exemple de « monade ». La façon dont Haskell utilise
-- une monade pour faire de l'IO lui permet d'être purement fonctionnel. N'importe
-- quelle fonction qui interagit avec le « monde extérieur » (c'est à dire fait de l'IO)
-- devient marqué comme `IO` dans la signature de son type. Ça nous montre
-- quelles fonctions sont « pures » (n'interagissent pas avec le monde extérieur
-- ou ne changent pas d'état) et quelles fonctions ne le sont pas.

-- C'est une fonctionnalité très puissante, car il est facile d'exécuter 
-- des fonctions pures simultanément, et donc la concurrence en Haskell
-- est très facile.


----------------------------------------------------
-- 9. Le REPL de Haskell
----------------------------------------------------

-- Lancer le REPL en tapant `ghci`.
-- Vous pouvez maintenant taper du code Haskell.
-- Toutes les nouvelles valeurs peuvent être crées 
-- avec `let` :

let foo = 5

-- Vous pouvez voir le type de n'importe quelle valeur avec `:t` :

>:t foo
foo :: Integer

-- Vous pouvez également lancer des actions de type `IO ()`

> sayHello
Quel est ton nom ?
Ami
Salut, Ami !

```

Et Haskell ne se limite pas à ça, on trouve encore par exemple les classes de types et les monades. Il y a beaucoup de raisons qui font que coder en Haskell est si *fun*. Je vous laisse avec un dernier exemple : une implémentation de quicksort :

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

Haskell facile à installer. Téléchargez-le [ici](http://www.haskell.org/platform/).

Vous pouvez trouver une approche beaucoup plus douce avec les excellents
[Learn you a Haskell](http://lyah.haskell.fr/) ou
[Real World Haskell (en)](http://book.realworldhaskell.org/).
---
language: html
filename: learnhtml-fr.html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
lang: fr-fr
---

HTML signifie HyperText Markup Language. 
C'est un langage (format de fichiers) qui permet d'écrire des pages internet.
C’est un langage de balisage, il nous permet d'écrire des pages HTML au moyen de balises (Markup, en anglais).
Les fichiers HTML sont en réalité de simple fichier texte.
Qu'est-ce que le balisage ? C'est une façon de hiérarchiser ses données en les entourant par une balise ouvrante et une balise fermante. 
Ce balisage sert à donner une signification au texte ainsi entouré.
Comme tous les autres langages, HTML a plusieurs versions. Ici, nous allons parlons de HTML5.

**NOTE :** Vous pouvez tester les différentes balises que nous allons voir au fur et à mesure du tutoriel sur des sites comme [codepen](http://codepen.io/pen/) afin de voir les résultats, comprendre, et vous familiariser avec le langage.
Cet article porte principalement sur la syntaxe et quelques astuces.


```html
<!-- Les commentaires sont entouré comme cette ligne! -->

<!-- #################### Les balises #################### -->
   
<!-- Voici un exemple de fichier HTML que nous allons analyser -->
<!-- Venez voir ce que ça donne  --> 

<!doctype html>
	<html>
		<head>
			<title>Mon Site</title>
		</head>
		<body>
			<h1>Hello, world!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">Venez voir ce que ça donne</a>
			<p>Ceci est un paragraphe</p>
			<p>Ceci est un autre paragraphe</p>
			<ul>
				<li>Ceci est un item d'une liste non ordonnée (liste à puces)</li>
				<li>Ceci est un autre item</li>
				<li>Et ceci est le dernier item de la liste</li>
			</ul>
		</body>
	</html>

<!-- Un fichier HTML débute toujours par indiquer au navigateur que notre page est faite en HTML -->

<!doctype html>

<!-- Après ça on commence par ouvrir une balise <html> -->
<html>
</html>
<!-- Et puis on la referme à la fin du fichier avec </html> -->
<!-- après cette balise de fin, plus rien ne doit apparaître. -->

<!-- À l'intérieur (entre la balise ouvrant et fermante <html></html>), on trouve : -->

<!-- Un entête (<head> en anglais ; il faut le refermer avec </head>) -->
<!-- L'entête contient des descriptions et informations annexes qui ne sont pas affichées : se sont les métadonnées -->

<head>
	<title>Mon Site</title><!-- La balise <title> permet d'indiquer au navigateur le titre à afficher dans la barre de l'onglet de la fenêtre -->
</head>

<!-- Après la balise <head>, on trouve la balise <body> -->
<!-- Pour le moment, rien n'est encore affiché dans la fenêtre du navigateur. -->
<!-- Il faut ensuite remplir le corps (balise <body>) avec du contenu -->

<body>
	<h1>Hello, world!</h1> <!-- La balise h1 permet de structurer le texte, c'est  un titre -->
	<!-- Il exite différents sous-titres à <h1> qui sont hiérarchisés du plus important (h2) au plus précis (h6) -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">Venez voir ce que ça donne</a> <!-- Lien vers la source cible indiqué dans href="" -->
	<p>Ceci est un paragraphe </p> <!-- La balise <p> permet d'inclure du texte à la page html -->
	<p>Ceci est un autre paragraphe</p>
	<ul> <!-- La balise <ul> permet d'introduire une liste à puces -->
	<!-- Si on souhaite une liste ordonnée : <ol> liste numérotée, 1. pour le premier élément, 2. pour le second, etc -->
		<li>Ceci est un item d'une liste non ordonnée (liste à puces)</li>
		<li>Ceci est un autre item</li>
		<li>Et ceci est le dernier item de la liste</li>
	</ul>
</body>

<!-- Voilà comment créer un fichier HTML simple -->

<!-- Mais il est possible d'ajouter encore des balises plus spécifiques -->

<!-- Pour insérer une image -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- On indique la source de l'image dans src="" -->
<!-- La source peut-être un URL ou encore la destination d'un fichier de votre ordinateur -->

<!-- Il est possible de réaliser des tableaux également -->

<table> <!-- On ouvre la balise <table> -->
	<tr> <!-- <tr> permet de créer une ligne -->
		<th>First Header</th> <!-- <th> permet de créer un titre au tableau -->
		<th>Second Header</th>
	</tr>
	<tr>
		<td>Première ligne, première cellule</td> <!-- <td> permet de créer une cellule -->
		<td>Première ligne, deuxième cellule</td>
	</tr>
	<tr>
		<td>Deuxième ligne, première cellule</td>
		<td>Deuxième ligne, deuxième cellule</td>
	</tr>
</table>

```

## Utilisation

Le HTML s'écrit dans des fichiers `.html`.

## En savoir plus 

* [Tutoriel HTML](http://slaout.linux62.org/html_css/html.html)
* [W3School](http://www.w3schools.com/html/html_intro.asp)
---
language: hy
filename: learnhy-fr.hy
contributors:
    - ["Abhishek L", "http://twitter.com/abhishekl"]
translators:
  - ["Hughes Perreault", "https://github.com/hperreault"]
lang: fr-fr
---

Hy est un dialecte du lisp bâti par dessus python. Il fonctionne en
convertissant le code hy en un arbre de syntaxe abstraite de python (ast).
Ceci permet à hy d'appeler du code python et à python d'appeler du code hy.

Ce tutoriel fonctionne pour hy > 0.9.12

```clojure
;; Ceci est une introduction simple à hy, pour un tutoriel rapide aller à
;; http://try-hy.appspot.com
;;
; Les commentaires se font avec des points-virgules, comme les autres LISPS

;; les s-expression de bases
; Les programmes Lisp sont fait d'expressions symboliques ou sexps qui
; ressemblent à
(some-function args)
; maintenant le quintessentiel hello world
(print "hello world")

;; les types de données simples
; Tous les types de données simples sont exactement similaires à leurs
; homologues de python
42 ; => 42
3.14 ; => 3.14
True ; => True
4+10j ; => (4+10j) un nombre complexe

; Commençons par un peu d'arithmétique très simple
(+ 4 1) ;=> 5
; l'opérateur est appliqué à tous les arguments, comme les autres lisps
(+ 4 1 2 3) ;=> 10
(- 2 1) ;=> 1
(* 4 2) ;=> 8
(/ 4 1) ;=> 4
(% 4 2) ;=> 0 l'opérateur modulo
; l'opérateur d'élévation à la puissance est représenté par ** comme en python
(** 3 2) ;=> 9
; les expressions imbriquées vont se comporter comme on s'y attend
(+ 2 (* 4 2)) ;=> 10
; aussi, les opérateurs logiques and or not et equal to etc. vont se comporter
; comme on s'y attend
(= 5 4) ;=> False
(not (= 5 4)) ;=> True

;; variables
; les variables sont déclarées en utilisant setv, les noms de variables
; peuvent utiliser l'UTF-8 à l'exception de ()[]{}",'`;#|
(setv a 42)
(setv π 3.14159)
(def *foo* 42)
;; d'autres types de conteneurs
; les chaînes, les listes, les tuples et dicts
; ce sont exactement les mêmes que les types de conteneurs de python
"hello world" ;=> "hello world"
; les opérations sur les chaînes fonctionnent comme en python
(+ "hello " "world") ;=> "hello world"
; les listes sont créés en utilisant [], l'indexation commence à 0
(setv mylist [1 2 3 4])
; les tuples sont des structures de données immuables
(setv mytuple (, 1 2))
; les dictionnaires sont des paires clé-valeur
(setv dict1 {"key1" 42 "key2" 21})
; :nom peut être utilisé pour définir des mots clés dans hy qui peuvent être
;  utilisées comme clés
(setv dict2 {:key1 41 :key2 20})
; utilisez `get' pour obtenir l'élément à l'index / clé
(get mylist 1) ;=> 2
(get dict1 "key1") ;=> 42
; Alternativement, si des mots clés ont été utilisés, l'élément peut être
; obtenu directement
(:key1 dict2) ;=> 41

;; fonctions et autres constructions de programme
; les fonctions sont définies en utilisant defn, la dernière sexp est renvoyé par défaut
(defn greet [name]
  "A simple greeting" ; une docstring optionnelle
  (print "hello " name))

(greet "bilbo") ;=> "hello bilbo"

; les fonctions peuvent prendre des arguments optionnels ainsi que des
; arguments sous forme de mots clés
(defn foolists [arg1 &optional [arg2 2]]
  [arg1 arg2])

(foolists 3) ;=> [3 2]
(foolists 10 3) ;=> [10 3]

; les fonctions anonymes sont créés en utilisant `fn' ou `lambda'
; qui sont semblable à `defn '
(map (fn [x] (* x x)) [1 2 3 4]) ;=> [1 4 9 16]

;; Opérations sur les séquences
; hy a des utilitaires natifs pour les opérations sur les séquences etc.
; récupérez le premier élément en utilisant  `first' ou `car'
(setv mylist [1 2 3 4])
(setv mydict {"a" 1 "b" 2})
(first mylist) ;=> 1

; découpez les listes en utilisant slice
(slice mylist 1 3) ;=> [2 3]

; obtenez les éléments d'une liste ou dict en utilisant `get'
(get mylist 1) ;=> 2
(get mydict "b") ;=> 2
; l'indexation des listes commence à 0 comme en python
; assoc peut définir les éléments à clés/index
(assoc mylist 2 10) ; makes mylist [1 2 10 4]
(assoc mydict "c" 3) ; makes mydict {"a" 1 "b" 2 "c" 3}
; il ya tout un tas d'autres fonctions de base qui rend le travail avec
; les séquences amusant

;; les importations fonctionnent comme en pyhtonn
(import datetime)
(import [functools [partial reduce]]) ; importe fun1 et fun2 de module1
(import [matplotlib.pyplot :as plt]) ; faire une importation foo comme bar
; toutes les méthodes natives de python sont accessibles à partir de hy
; a.foo(arg) est appelé (.foo a arg)
(.split (.strip "hello world  ")) ;=> ["hello" "world"]

;; Conditionelles
; (if condition (body-if-true) (body-if-false)
(if (= passcode "moria")
  (print "welcome")
  (print "Speak friend, and Enter!"))

; imbriquez plusieurs if else if avec le mot clé cond
(cond
 [(= someval 42)
  (print "Life, universe and everything else!")]
 [(> someval 42)
  (print "val too large")]
 [(< someval 42)
  (print "val too small")])

; groupez les expressions avec do, ceux-ci seront executé séquentiellemnt
; les expressions comme defn ont un do implicite
(do
 (setv someval 10)
 (print "someval is set to " someval)) ;=> 10

; créer une liaison lexicale avec `let', toutes les variables déclarées
; comme cela ont une portée locale
(let [[nemesis {"superman" "lex luther"
                "sherlock" "moriarty"
                "seinfeld" "newman"}]]
  (for [(, h v) (.items nemesis)]
	(print (.format "{0}'s nemesis was {1}" h v))))

;; classes
; les classes sont définies comme ceci
(defclass Wizard [object]
  [[--init-- (fn [self spell]
             (setv self.spell spell) ; init the spell attr
             None)]
   [get-spell (fn [self]
              self.spell)]])

;; allez voir hylang.org
```

### Lectures complémentaires

Ce tutoriel est juste une simple introduction à hy/lisp/python.

La documentation de HY: [http://hy.readthedocs.org](http://hy.readthedocs.org)

Le repo GitHub de HY: [http://github.com/hylang/hy](http://github.com/hylang/hy)

Sur freenode irc #hy, twitter hashtag #hylang
---
language: javascript
contributors:
    - ['Adam Brenecki', 'http://adam.brenecki.id.au']
    - ['Ariel Krakowski', 'http://www.learneroo.com']
filename: javascript-fr.js
translators:
    - ['@nbrugneaux', 'https://nicolasbrugneaux.me']
    - ['Michel Antoine', 'https://github.com/antoin-m']
lang: fr-fr
---

JavaScript a été créé par Brendan Eich, travaillant alors a Netscape, en 1995.
Le langage avait à l'origine pour but d'être un langage de scripting simple
pour les sites web, complétant le Java (à ne pas confondre avec JavaScript)
pour des applications web complexes. Mais son intégration très proche et
simple des pages web, ainsi que le support natif des navigateurs a rendu
le JavaScript incontournable aujourd'hui tant bien dans le front-end que
dans le back-end.

En effet, le JavaScript n'est plus uniquement limité aux navigateurs, grâce à
Node.JS, un projet qui offre un environnement indépendant dans lequel un
interpréteur Javascript, basé sur le célèbre moteur V8 de Google Chrome,
peut être utilisé directement côté serveur pour exécuter des programmes écrits
en JavaScript.

ECMAScript (la norme du langage Javascript) entre en version 6. Cette version introduit de nombreuses mises à jour tout en restant rétrocompatible. L'implémentation de ces nouvelles fonctionnalités est en cours et celles-ci ne sont donc pas forcément compatibles avec tous les navigateurs.

```js
// Les commentaires sont comme en C. Les commentaires mono-ligne commencent par 2 slashs,
/* et les commentaires sur plusieurs lignes commencent avec slash-étoile
   et finissent avec étoile-slash */

// Toutes les expressions peuvent finir par ;
doStuff();

// ... mais n'en n'ont pas forcément besoin, les point-virgules sont ajoutés
// lors de l’interprétation aux sauts de ligne, sauf exceptions
doStuff()

// Parce que ces cas peuvent produire des effets inattendus, nous utiliserons
// des point-virgules dans ce guide.


///////////////////////////////////
// 1. Nombres, Chaines de caractères et Opérateurs

// JavaScript a un seul type de nombre (qui est un 64-bit IEEE 754 double (décimaux))
// Comme avec le Lua, ne paniquez pas à cause du manque d'int (entiers) : les
// doubles ont un mantisse de 52 bits, ce qui est assez pour stocker des int jusqu'à
// 9 x 10¹⁵ exactement.
3; // = 3
1.5; // = 1.5

// L'arithmétique de base fonctionne comme vous vous y attendriez
1 + 1; // = 2
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// Ainsi que les divisions non-entières
5 / 2; // = 2.5

// Les opérations bits à bits fonctionnent aussi, quand vous effectuez une opération
// bits à bits, votre nombre décimal est converti en entier *jusqu'à* 32 bits.
1 << 2; // = 4

// Comme en mathématiques, la priorité est donnée aux parenthèses.
(1 + 3) * 2; // = 8

// Il existe 3 valeurs spéciales pour les nombres:
Infinity; // le résultat de 1/0 par exemple
-Infinity; // le résultat de -1/0 par exemple
NaN; // le résultat de 0/0 par exemple

// Il existe également le type booléen.
true; // vrai
false; // faux

// Les chaines de caractères (strings) sont créees avec " ou ' indifféremment, la seule
// raison de choisir l'un ou l'autre est la cohérence dans votre code.
"abc";
'Hello, world';

// *ES6:* Les chaines de caractères peuvent être crées en utilisant un modèle
// entouré des quotes inverses (`) à la place des quotes classiques (' ou ").
// Les variables sont interprétées avec ${var}
let banta = "Harry", santa = "Hermione";
`${banta}, your santa is ${santa}.` // = "Harry, your santa is Hermione."

// La négation utilise le symbole !
!true; // = false
!false; // = true

// L'égalité est === ou ==
// === compare la valeur exacte 2 === '2' // = false
// == convertit la valeur pour comparer 2 === '2' // = true
// En général, il vaut mieux utiliser === pour ne pas faire d'erreur.
1 === 1; // = true
2 === 1; // = false

// L'inégalité est !== ou !=, basé sur le même principe qu'avant.
1 !== 1; // = false
2 !== 1; // = true

// Plus de comparaisons :
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Les chaines de caractères se concatènent avec +
'Hello ' + 'world!'; // = 'Hello world!'

// et peuvent être comparées alphabétiquement avec < et >
'a' < 'b'; // = true

// Vous pouvez accéder les caractères dans une string avec charAt
'This is a string'.charAt(0);  // = 'T'

// ... ou utiliser substring pour avoir un plus gros morceau
'Hello world'.substring(0, 5); // = 'Hello'

// la longueur, length, est une propriété, donc n'utilisez pas de ()
'Hello'.length; // = 5

// Il y a également null et undefined
null; // utilisé pour une non-valeur
undefined; // utilisé pour une valeur actuellement non présente (cependant,
           // undefined est aussi une valeur valide)

// false, null, undefined, NaN, 0 and '' sont 'presque-faux' (falsy), tout le reste
// est 'presque-vrai' (truthy)
// Notez que 0 est falsy mais '0' est truthy, alors même que 0 == '0' (mais 0 !== '0')

// *ES6:* Introduction d'un nouveau type primitif : Symbol
var symbol_one = Symbol();
var symbol_two = Symbol('This is optional description, for debugging');
typeof symbol_one === 'symbol' // = true

// *ES6:* Un Symbol est immutable et unique
Symbol() === Symbol() // = false
Symbol('learnx') === Symbol('learnx') // = false

///////////////////////////////////
// 2. Variables, Tableaux, Objets, Maps et Sets

// Les variables sont déclarées avec le mot clé var. Le typage en JavaScript est
// dynamique, donc pas besoin de spécifier le type. L'assignement utilise un seul =.
var someVar = 5;

// si vous oubliez le mot clé var, vous n'aurez pas d'erreur (sauf en mode strict)
someOtherVar = 10;

// ... mais la variable aura une portée globale (plus communément trouvé en tant
// que "global scope" en anglais), et non pas une portée limitée à la fonction
// dans laquelle vous l'aviez définie.

// Les variables déclarées et non assignées sont undefined par défaut
var someThirdVar;
var someThirdVar = undefined;

// ... sont deux déclarations identiques.

// Il est possible de déclarer plusieurs variables en séparant leur déclaration
// avec l'opérateur virgule
var someFourthVar = 2, someFifthVar = 4;

// *ES6:* Les variables peuvent maintenant être déclarées avec les mots-clés
// `let` et `const`
let someSixthVar = 6;
const someSeventhVar = 7;

// *ES6:* Le mot-clé `let` attache la variable au block de code et non à la fonction
// à l'inverse de `var`
for (let i = 0; i < 10; i++) {
    x += 10;
}
i; // = raises ReferenceError

// *ES6:* Les variables "const" doivent être assignées lors de l'initialisation
const someEighthVar = 7;
const someNinthVar; // raises SyntaxError

// *ES6:* Modifier une variable constante ne lève par d'erreur mais échoue
// silencieusement
const someNinthVar = 9;
someNinthVar = 10;
someNinthVar; // = 9

// Il y a des raccourcis pour les opérations mathématiques:
someVar += 5; // équivalent pour someVar = someVar + 5;
someVar *= 10; // de même, someVar = someVar * 10;
someVar++;    // = someVar += 1;
someVar--;  // = someVar -= 1;

// Les tableaux (Arrays) sont des listes ordonnées de valeurs, de tous types.
var myArray = ['Hello', 45, true];

// Leurs membres peuvent être accédés en utilisant les crochets
// Les indices commencent à 0.
myArray[1]; // = 45

// Les tableaux sont modifiables, ainsi que leurs longueurs
myArray.push( 'World' );
myArray.length; // = 4

// Ajout/Modification à un index spécifique
myArray[3] = 'Hello';

// *ES6:* Les Arrays peuvent maintenant être déstructurés en utilisant le pattern matching
var [a, b] = [1, 2];
var [a, , b] = [1, -2, 2]

a; // = 1
b; // = 2

// *ES6:* La déstructuration peut échouer silencieusement.
// Il est aussi possible d'utiliser des valeurs par défaut
var [a] = [];
a; // = undefined;
var [a = 1] = [];
a; // = 1;
var [a = 1] = [2];
a; // = 2;

// Les objets JavaScript sont appelés 'dictionnaires' ou 'maps' dans certains autres
// langages : ils sont une liste non-ordonnée de paires clé-valeur.
var myObj = {key1: 'Hello', key2: 'World'};

// Les clés sont des strings, mais les ' ou " sont optionels si elles sont des
// noms valides en JavaScript. Les valeurs peuvent être de n'importe quel type.
var myObj = {myKey: 'myValue', 'my other key': 4};

// Les attributs d'objets peuvent être accédés avec les crochets
myObj['my other key']; // = 4

// .. ou avec un point si la clé est un identifiant valide.
myObj.myKey; // = 'myValue'

// *ES6:* Un Symbol peut être utilisé en tant que clé. Puisque ceux-ci sont uniques,
// le seul moyen d'accéder à la propriété est d'avoir une référence sur ce Symbol.
myObj["key"] = "public value";
myObj[Symbol("key")] = "secret value";
myObj[Symbol("key")]; // = undefined

// Les objets sont eux aussi modifiables.
myObj.myThirdKey = true;

// Si vous essayez d'accéder à une valeur non-définie, vous obtiendrez undefined
myObj.myFourthKey; // = undefined

// *ES6:* Comme les Arrays, les Objects peuvent être déstructurés en utilisant le pattern matching
var {foo} = {foo: "bar"};
foo // = "bar"

// *ES6:* Les Objects déstructurés peuvent utiliser des noms de variables différents
// de ceux d'origine grâce au pattern matching
var {foo, moo: baz} = {foo: "bar", moo: "car"};
foo // = "bar"
baz // = "car"

// *ES6:* Il est possible d'utiliser des valeurs par défaut lor de la déstructuration d'un Object
var {foo="bar"} = {moo: "car"};
foo // = "bar"

// *ES6:* Une erreur lors de la déstructuration restera silencieuse
var {foo} = {};
foo // = undefined

// *ES6:* Les Maps sont des objets itérables de type clé-valeur.
// Il est possible de créer une nouvelle map en utilisant `new Map()`
var myMap = new Map();

// *ES6:* Il est possible d'ajouter un couple clé-valeur avec la méthode `.set()`,
// de récupérer une valeur avec `.get()`,
// de vérifier qu'une clé existe avec `.has()`
// et enfin de supprimer un couple clé-valeur avec `.delete()`

myMap.set("name", "Douglas");
myMap.get("name"); // = "Douglas"
myMap.has("name"); // = true
myMap.delete("name");

// *ES6:* Les Sets sont des ensembles de valeurs uniques.
// Il est possible de créer un set avec `new Set()`.
// Toute valeur non unique est ignorée.
var mySet = new Set([1,2,2]);
console.log([...mySet]); // = [1,2]

///////////////////////////////////
// 3. Logique et structures de contrôle

// Les si (if) fonctionnent comme vous vous y attendez.
var count = 1;
if (count === 3) {
  // seulement quand count est 3
}
else if (count === 4) {
  // uniquement quand count est 4
}
else {
  // le reste du temps, si ni 3, ni 4.
}

// De même pour while.
while (true) {
  // Une boucle infinie !
}

// Les boucles do-while sont pareilles, mais sont exécutées au moins une fois.
var input
do {
  input = getInput();
} while (!isValid(input))

// La boucle for est la même qu'en C ou en Java:
// initialisation; condition pour continuer; itération
for (var i = 0; i < 5; i++){
    // sera exécutée 5 fois
}

// La boucle for...in permet d'itérer sur les noms des propriétés d'un objet
var description = "";
var person = {fname:"Paul", lname:"Ken", age:18};
for (var x in person){
    description += person[x] + " ";
}
description; // = "Paul Ken 18 "

// *ES6:* La boucle for...of permet d'itérer sur les propriétés d'un objet
var description = "";
var person = {fname:"Paul", lname:"Ken", age:18};
for (var x of person){
    description += x + " ";
}
description; // = "Paul Ken 18 "

// && est le "et" logique, || est le "ou" logique
if (house.size === 'big' && house.colour === 'blue'){
    house.contains = 'bear';
}
if (colour === 'red' || colour === 'blue'){
    // colour est soit 'red' soit 'blue'
}

// Les raccourcis && et || sont pratiques pour instancier des valeurs par defaut.
var name = otherName || 'default';

// Ceci est l'équivalent de
var name = otherName;
if (!name){
  name = 'default';
}

// Le switch vérifie les égalités avec ===
// utilisez un "break" à la fin de chaque cas
// ou les cas suivants seront eux aussi exécutés
grade = 'B';
switch (grade) {
  case 'A':
    console.log('Great job');
    break;
  case 'B':
    console.log('OK job');
    break;
  case 'C':
    console.log('You can do better');
    break;
  default:
    console.log('Oy vey');
    break;
}


///////////////////////////////////
// 4. Fonctions, Scope (Environnement) et Closures

// Les fonctions sont déclarées avec le mot clé function
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction('foo'); // = 'FOO'

// Attention, la valeur à retourner doit se trouver sur la même ligne que
// le mot-clé `return` sinon la fonction retournera systématiquement `undefined`
function myFunction(){
    return // <- semicolon automatically inserted here
    {thisIsAn: 'object literal'}
}
myFunction(); // = undefined

// *ES6:* Les paramètres des fonctions peuvent désormais avoir des valeurs par défaut
function default(x, y = 2) {
    return x + y;
}
default(10); // == 12

// Les fonctions JavaScript sont des objets de première classe, donc peuvent
// être réassignées à d'autres variables et passées en tant que paramètres pour
// d'autres fonctions
function myFunction(){
    // ce code s'exécutera dans 5 secondes
}
setTimeout(myFunction, 5000);
// Note: setTimeout ne fait pas parti du langage, mais les navigateurs ainsi
// que Node.js le rendent disponible

// Les fonctions n'ont pas nécessairement besoin d'un nom, elles peuvent être
// anonymes
setTimeout(function(){
    // ce code s'exécutera dans 5 secondes
}, 5000);

// *ES6:* Introduction d'un sucre syntaxique permettant de créer
// une fonction anonyme de la forme : `param => returnValue`.
setTimeout(() => console.log('5 seconds, are up.'), 5000);

// Le Javascript crée uniquement un scope, une portée d'action limitée, pour
// les fonctions, et pas dans les autres blocs.
if (true){
    var i = 5;
}
i; // = 5 - et non undefined comme vous pourriez vous y attendre

// Cela a mené à un style commun de fonctions anonymes immédiatement exécutée;
// ou "immediately-executing anonymous functions"
(function(){
    var temporary = 5;
    // Nous pouvons accéder au scope global en assignant à l'objet global,
    // qui dans les navigateurs est "window". Il est différent dans Node.js,
    // le scope global sera en fait local au module dans lequel vous
    // vous trouvez. http://nodejs.org/api/globals.html
    window.permanent = 10;
})();
// Cela permet de ne pas avoir de fuites de variables qui polluent
// l’environnement global.
temporary; // raises ReferenceError
permanent; // = 10

// Une des fonctionnalités les plus puissantes de Javascript est le système de
// closures. Si une fonction est définie dans une autre fonction, alors la
// fonction interne aura accès aux variables de la fonction parente, même si
// celle-ci a déjà finie son exécution.
function sayHelloInFiveSeconds(name){
    var prompt = 'Hello, ' + name + '!';
    // Fonction interne
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout is asynchrone, donc la fonction sayHelloInFiveSeconds quittera
    // immédiatement, et setTimeout appelera inner après.
}
sayHelloInFiveSeconds('Adam'); // ouvre un popup avec 'Hello, Adam!' dans 5sec

// *ES6:* Les paramètres des fonctions appelées avec un tableau en entré
// préfixé par `...` vont se peupler avec les éléments du tableau
function spread(x, y, z) {
    return x + y + z;
}
spread(...[1,2,3]); // == 6

// *ES6:* Les fonctions peuvent recevoir les paramètres dans un tableau en utilisant l'opérateur `...`
function spread(x, y, z) {
    return x + y + z;
}
spread(...[1,2,3]); // == 6

///////////////////////////////////
// 5. Encore plus à propos des Objets; Constructeurs and Prototypes

// Les objets peuvent contenir des fonctions.
var myObj = {
    myFunc: function(){
        return 'Hello world!';
    }
};
myObj.myFunc(); // = 'Hello world!'

// Lorsqu'une fonction attachée à un objet est appelée, elle peut accéder à
// l'objet avec le mot clé this.
myObj = {
    myString: 'Hello world!',
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = 'Hello world!'

// La valeur de "this" change de par l'endroit où la fonction est appelée, et
// non de l'endroit où elle est définie. Donc elle ne fonctionnera pas si elle
// est appelée hors du contexte l'objet.
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// A l'inverse, une fonction peut être attachée à un objet et y gagner l'accès
// au travers de "this"
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}

myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = 'HELLO WORLD!'

// Le contexte correspond à la valeur de "this".
// Nous pouvons aussi spécifier un contexte, forcer la valeur de "this,
// pour une fonction quand elle est appelée grâce à "call" ou "apply".
var anotherFunc = function(s){
    return this.myString + s;
}
anotherFunc.call(myObj, ' And Hello Moon!'); // = 'Hello World! And Hello Moon!'

// 'apply' est presque identique, mais prend un tableau comme liste d’arguments.

anotherFunc.apply(myObj, [' And Hello Sun!']); // = 'Hello World! And Hello Sun!'

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// Mais, "call" and "apply" fonctionnenent uniquement au moment de l'appel de la
// fonction. Pour lier le contexte de façon permanente, nous pouvons utiliser
// "bind" pour garder une référence à la fonction avec ce "this".
var boundFunc = anotherFunc.bind(myObj);
boundFunc(' And Hello Saturn!'); // = 'Hello World! And Hello Saturn!'

// "bind" peut aussi être utilisé pour créer une application partielle de la
// fonction (curry)
var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// Lorsque vous appelez une fonction avec le mot clé "new", un nouvel objet est
// crée et mis à disposition de la fonction via "this". Ces fonctions sont
// communément appelées constructeurs.
var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// Chaque objet en Javascript a un "prototype". Quand vous essayez d'accéder à
// une propriété que l'objet n'a pas, l'interpréteur va regarder son prototype.

// Quelques implémentations de JS vous laissent accéder au prototype avec la
// propriété "magique" __proto__. Ceci peut être utile, mais n'est pas standard
// et ne fonctionne pas dans certains des navigateurs actuels.
var myObj = {
    myString: 'Hello world!'
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42
myObj.myFunc(); // = 'hello world!'

myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true


// Pour obtenir le prototype il existe également Object.getPrototypeOf
Object.getPrototypeOf( myObj ) // = {meaningOfLife: 42, myFunc: function}

// Il n'y a pas de copie ici. Chacun des objets stocke une référence à son
// prototype. Cela veut dire que l'on peut le modifier et cela se répercutera
// partout.
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

// L'inverse n'est cependant pas vrai. Changer la propriété d'un objet ne change
// pas la chaine prototypale.
myObj.meaningOfLife = 42;
myPrototype.meaningOfLife; // = 43

// Comme précédemment dit, __proto__ n'est pas standard et ne devrait pas être
// utilisé. Il y a deux autres moyen de créer un nouvel objet avec un prototype
// donné.

// Le premier est Object.create, mais c'est assez récent et risque de ne pas
// fonctionner dans tous les navigateurs.
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// Le deuxième moyen, qui marche partout, fonctionne avec les constructeurs.
// Les constructeurs ont un propriété appelée prototype. Ce n'est *pas* le
// prototype du constructeur de la fonction elle-même, c'est le prototype que
// les nouveaux objets crées grâce à ce constructeur avec "new" auront.
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function(){
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// Les types pré-définis tels que les strings ou nombres ont aussi des
// constructeurs
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// ... mais ils ne sont pas exactement équivalent.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // 0 est falsy, le code ne fonctionnera pas.
}

// Cependant, vous pouvez ajouter des fonctionnalités aux types de bases grâce à
// cette particularité.
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
'abc'.firstCharacter(); // = 'a'

// C'est très souvent utilisé pour le "polyfilling", qui implémente des nouvelles
// fonctionnalités de JavaScript dans de plus anciens environnements, tels que
// les vieux navigateurs.

//Par exemple, Object.create est assez récent, mais peut être implémenté grâce à
// ce polyfill
if (Object.create === undefined){ // pour ne pas reécrire si la fonction existe déjà
    Object.create = function(proto){
        // on crée un constructeur temporaire avec le bon prototype
        var Constructor = function(){};
        Constructor.prototype = proto;
        // puis on utilise "new" pour créer un object avec ce même prototype
        return new Constructor();
    }
}

// *ES6:* Les objets peuvent être équipés de proxies qui permettent d'intercepter
// les actions sur leurs propriétés. Voici comment créer un proxy sur un objet :
var proxyObject = new Proxy(object, handler);

// *ES6:* Les méthodes d'un objet handler sont appelées lors de l'interception d'une action.
// La méthode `.get()` est appelée à chaque lecture d'une propriété
// tandis que la méthode `.set()` est appelée à chaque écriture.
var handler = {
  get (target, key) {
    console.info('Get on property' + key);
    return target[key];
  },
  set (target, key, value) {
    console.info('Set on property' + key);
    return true;
  }
}

// *ES6:* Les classes peuvent désormais être définies en utilisant le mot-clé `class`.
// Le constructeur s'appelle `constructor` et les méthodes statiques utilisent le mot-clé `static`
class Foo {
    constructor() {console.log("constructing Foo");}
    bar() {return "bar";}
    static baz() {return "baz";}
}

// *ES6:* Les objets issus des classes sont initialisés avec le mot-clé `new`.
// Il est possible d'hériter d'une classe avec le mot-clé `extends`
var FooObject = new Foo(); // = "constructing Foo"
class Zoo extends Foo {}

// *ES6:* Les méthodes statiques doivent être appelées par la classe, les autres méthodes par l'objet
Foo.baz() // = "baz"
FooObject.bar() // = "bar"

// *ES6:* Il est désormais possible d'exporter des valeurs en tant que module.
// Les exports peuvent être n'importe quel objet, valeur ou fonction.
var api = {
  foo: "bar",
  baz: "ponyfoo"
};
export default api;

// *ES6:* La syntaxe `export default` permet d'exporter l'objet sans en changer le nom.
// Il y a plusieurs façons de l'importer:
import coolapi from "api"; // = importe le module dans la variable `coolapi`
import {foo, baz} from "api"; // = importe les attributs `foo` et `baz` du module
import {foo as moo, baz} from "api"; // = importe les attributs `foo` (en le renommant `moo`) et `baz` du module
import _, {map} from "api"; // = importe les exports par défaut ET `map`
import * as coolapi from "api"; // = importe le namespace global du module

```

## Pour aller plus loin (en anglais)

The [Mozilla Developer
Network](https://developer.mozilla.org/fr-FR/docs/Web/JavaScript) expose une
excellente documentation pour le Javascript dans les navigateurs. Et contient
également un wiki pour s'entraider.

MDN's [A re-introduction to
JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
recouvre les principaux sujets vus ici. Le guide est délibérément uniquement
à propos du JavaScript, et ne parle pas des navigateurs; pour cela, dirigez vous
plutôt ici :
[Document Object
Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)

[Learn Javascript by Example and with Challenges](http://www.learneroo.com/modules/64/nodes/350) quelques challenges.

[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) is an in-depth
un guide pour vous éviter les faux-amis dans le JavaScript.

[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) un classique. A lire.

En addition aux contributeurs de cet article, du contenu provient du
"Python tutorial" de Louie Dinh, et de [JS
Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
sur le réseau Mozilla.
---
category: tool
tool: jquery
contributors:
    - ["Sawyer Charles", "https://github.com/xssc"]
translators:
    - ["Sylvain Vaure", "https://github.com/Gnomino"]
filename: jquery-fr.js
lang: fr-fr
---

jQuery est une bibliothèque JavaScript dont le but est de permettre de "faire plus en écrivant moins" (do more, write less). Elle facilite l'écriture de nombreuses fonctions, notamment au niveau d'AJAX, de la gestion d'événements, ou encore de la manipulation de documents. 
C'est pourquoi aujourd'hui, jQuery est utilisée par de nombreuses grandes entreprises et par des développeurs du monde entier.

Étant donné que jQuery est une bibliothèque JavaScript, vous devriez d'abord [apprendre le JavaScript](https://learnxinyminutes.com/docs/fr-fr/javascript-fr/)
```js


///////////////////////////////////
// 1. Les sélecteurs

// On utilise les sélecteurs de jQuery pour sélectionner des éléments
var page = $(window); // Sélectionne tout le viewport

// On peut aussi utiliser des sélecteurs CSS
var paragraph = $('p'); // Sélectionne tous les éléments paragraphes
var table1 = $('#table1'); // Sélectionne l'élément qui a l'id 'table1'
var squares = $('.square'); // Sélectionne tous les éléments avec la classe 'square'
var square_p = $('p.square') // Sélectionne tous les paragraphes avec la classe 'square'


///////////////////////////////////
// 2. Événements et effets
// jQuery gère très bien ce qui se passe lorsqu'un événement est déclenché
// L'événement 'ready' est très souvent utilisé sur le document
// On utilise la méthode 'ready' pour attendre que l'élément ait fini de se charger
$(document).ready(function(){
  // Ce code ne s'exécutera pas avant que le document soit chargé (prêt)
});
// On peut aussi utiliser des fonctions définies
function onAction() {
  // Ceci est exécuté quand l'événement est déclenché
}
$('#btn').click(onAction); // Appelle onAction à chaque clic

function onAction() {
  // Ceci est exécuté quand un évènement est déclenché
}

// D'autres évènements communs :
$('#btn').dblclick(onAction); // Double clic
$('#btn').hover(onAction); // Survol de la souris
$('#btn').focus(onAction); // Gain du focus
$('#btn').blur(onAction); // Perte du focus
$('#btn').submit(onAction); // Envoi (d'un formulaire)
$('#btn').select(onAction); // Quand un élement est sélectionné
$('#btn').keydown(onAction); // Quand une touche est enfoncée
$('#btn').keyup(onAction); // Quand une touche est relâchée
$('#btn').keypress(onAction); // Quand on appuie sur un touche
$('#btn').mousemove(onAction); // Quand la souris se déplace
$('#btn').mouseenter(onAction); // La souris entre dans l'élément
$('#btn').mouseleave(onAction); // La souris sort de l'élément

// On peut aussi utiliser des fonctions lambdas
$('#btn').hover(function(){
  // exécuté lors d'un survol de la souris
});

// Il est possible de déclencher l'événement sans le gérer
// simplement en ne passant aucun paramètre à la méthode
$('#btn').dblclick(); // Simule un double clic sur l'élément

// On peut gérer plusieurs événements en utilisant le sélecteur une seule fois
$('#btn').on(
  {dblclick: myFunction1} // Déclenché à chaque double clic
  {blur: myFunction1} // Déclenché quand l'élément perd le focus
);

// On peut déplacer et cacher des éléments grâce à des fonctions d'effets
$('.table').hide(); // Cache le(s) élément(s)

// Note: même avec un appel à une fonction dans ces méthodes 
// cache quand même l'élément
$('.table').hide(function(){
    // L'élément est caché, puis la fonction est exécutée
});

// On peut stocker des sélecteurs dans des variables
var tables = $('.table');

// Des méthodes basique de manipulation de document :
tables.hide(); // Cache un(des) élément(s)
tables.show(); // Montre (dé-cache) un(des) élément(s)
tables.toggle(); // Change le statut le statut caché/montré
tables.fadeOut(); // Fait disparaître l'élément
tables.fadeIn(); // Fait apparaître l'élément
tables.fadeToggle(); // Fait apparaître ou disparaître
tables.fadeTo(0.5); // Fondu jusqu'à une certaine opacité (entre 0 et 1)
tables.slideUp(); // Cache l'élément avec un effet de glissement vers le haut
tables.slideDown(); // Fait apparaître l'élément avec un glissement vers le bas
tables.slideToggle(); // Cache/Montre l'élément avec un effet de glissement

// Les méthodes ci-dessus prennent en arguments 
// une vitesse (millisecondes) et une function callback
tables.hide(1000, myFunction); // Animation d'une seconde, puis appel à la fonction

// fadeTo doit avoir une opacité entre 0 et 1 comme deuxième argument
tables.fadeTo(2000, 0.1, myFunction); // 2 sec. fade to 0.1 opacity then function

// La méthode animate permet des animations légèrement plus poussées
tables.animate({margin-top:"+=50", height: "100px"}, 500, myFunction);
// La méthode prend un objet de css et de valeurs finales,
// des paramètres d'options facultatifs pour régler l'animation,
// et bien sûr la fonction callback

///////////////////////////////////
// 3. Manipulation

// Ces méthodes sont similaires aux effets mais permettent d'aller plus loin
$('div').addClass('taming-slim-20'); // Ajoute la classe taming-slim-20 aux div 

// Méthodes ordinaires de manipulation
$('p').append('Hello world'); // Ajoute à la fin de l'élément
$('p').attr('class'); // Renvoie la valeur de l'attribut
$('p').attr('class', 'content'); // Change la valeur de l'attribut
$('p').hasClass('taming-slim-20'); // Renvoie vrai si l'élément est de la classe
$('p').height(); // Renvoie la hauteur de l'élément ou la change


// Pour beaucoup de méthodes de manipulation, récupérer des informations
// d'un élément renverra SEULEMENT ceelles du premier
$('p').height(); // Renvoie SEULEMENT la hauteur du premier élément 'p'

// On peut utiliser 'each' pour parcourir tous les éléments
var heights = [];
$('p').each(function() {
  heights.push($(this).height()); // Ajoute la hauteur de tous les éléments 'p' à la liste
});


``

---
language: json
filename: learnjson-fr.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
  - ["Alois de Gouvello","https://github.com/aloisdg"]
lang: fr-fr
---

Comme JSON est un format d'échange de données extrêmement simple, ce Apprendre X en Y minutes
est susceptible d'être le plus simple jamais réalisé.

JSON dans son état le plus pur n'a aucun commentaire, mais la majorité des parseurs accepterons
les commentaires du langage C (`//`, `/* */`). Pour les besoins de ce document, cependant,
tout sera du JSON 100% valide. Heureusement, il s'explique par lui-même.


```json
{
  "Clé": "valeur",
  
  "Clés": "devront toujours être entourées par des guillemets",
  "nombres": 0,
  "chaînes de caractères": "Hellø, wørld. Tous les caractères Unicode sont autorisés, accompagné d'un \"caractère d'échappement\".",
  "a des booléens ?": true,
  "rien": null,

  "grand nombre": 1.2e+100,

  "objets": {
    "commentaire": "La majorité de votre strucutre sera des objets.",

    "tableau": [0, 1, 2, 3, "Les tableaux peuvent contenir n'importe quoi.", 5],

    "un autre objet": {
      "commentaire": "Ces choses peuvent être imbriquées. C'est très utile."
    }
  },

  "bêtises": [
    {
      "sources de potassium": ["bananes"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "style alternatif": {
    "commentaire": "regarde ça !"
  , "position de la virgule": "n'a pas d'importance - aussi longtemps qu'elle est avant la valeur, alors elle est valide."
  , "un autre commentaire": "comme c'est gentil"
  },

  "C'était court": "Et, vous avez terminé. Maintenant, vous savez tout ce que JSON a à offrir."
}
```
---
language: LiveScript
filename: learnLivescript-fr.ls
contributors:
    - ["Christina Whyte", "http://github.com/kurisuwhyte/"]
translators:
    - ["Morgan Bohn", "https://github.com/dotmobo"]
lang: fr-fr
---

LiveScript est un langage qui compile en JavaScript. Il a un rapport direct 
avec JavaScript, et vous permet d'écrire du JavaScript plus simplement, plus 
efficacement et sans répétitivité. LiveScript ajoute non seulement des 
fonctionnalités pour écrire du code fonctionnel, mais possède aussi nombre 
d'améliorations pour la programmation orientée objet et la programmation 
impérative.

LiveScript est un descendant direct de [Coco][], indirect de [CoffeeScript][],
avec lequel il a beaucoup plus de compatibilité.

[Coco]: http://satyr.github.io/coco/
[CoffeeScript]: http://coffeescript.org/

Vous pouvez contacter l'auteur du guide original en anglais ici :
[@kurisuwhyte](https://twitter.com/kurisuwhyte)


```coffeescript
# Comme son cousin CoffeeScript, LiveScript utilise le symbole dièse pour les 
# commentaires sur une ligne.

/*
 Les commentaires sur plusieurs lignes utilisent la syntaxe du C. Utilisez-les
 si vous voulez préserver les commentaires dans la sortie JavaScript.
 */
```
```coffeescript
# LiveScript utilise l'indentation pour délimiter les blocs de code plutôt que 
# les accolades, et les espaces pour appliquer les fonctions (bien que les 
# parenthèses soient utilisables).


########################################################################
## 1. Valeurs basiques
########################################################################

# Les valeurs non définies sont représentées par le mot clé `void` à la place de
# `undefined`
void            # comme `undefined` mais plus sûr (ne peut pas être redéfini)

# Une valeur non valide est représentée par Null.
null


# Les booléens s'utilisent de la façon suivante:
true
false

# Et il existe divers alias les représentant également:
on; off
yes; no


# Puis viennent les nombres entiers et décimaux.
10
0.4     # Notez que le `0` est requis

# Dans un souci de lisibilité, vous pouvez utiliser les tirets bas et les 
# suffixes sur les nombres. Il seront ignorés à la compilation.
12_344km


# Les chaînes sont des séquences immutables de caractères, comme en JS:
"Christina"             # Les apostrophes fonctionnent également!
"""Multi-line
   strings
   are
   okay
   too."""

# De temps à autre, vous voulez encoder un mot clé; la notation en backslash 
# rend cela facile:
\keyword                # => 'keyword'


# Les tableaux sont des collections ordonnées de valeurs.
fruits =
  * \apple
  * \orange
  * \pear

# Il peuvent être écrits de manière plus consises à l'aide des crochets:
fruits = [ \apple, \orange, \pear ]

# Vous pouvez également utiliser la syntaxe suivante, à l'aide d'espaces, pour 
# créer votre liste de valeurs:
fruits = <[ apple orange pear ]>

# Vous pouvez récupérer une entrée à l'aide de son index:
fruits[0]       # => "apple"

# Les objets sont une collection non ordonnées de paires clé/valeur, et 
# d'autres choses (que nous verrons plus tard).
person =
  name: "Christina"
  likes:
    * "kittens"
    * "and other cute stuff"

# A nouveau, vous pouvez utiliser une expression plus consise à l'aide des 
# accolades:
person = {name: "Christina", likes: ["kittens", "and other cute stuff"]}

# Vous pouvez récupérer une entrée via sa clé:
person.name     # => "Christina"
person["name"]  # => "Christina"


# Les expressions régulières utilisent la même syntaxe que JavaScript:
trailing-space = /\s$/          # les mots-composés deviennent motscomposés

# A l'exception que vous pouvez pouvez utiliser des expressions sur plusieurs
# lignes!
# (les commentaires et les espaces seront ignorés)
funRE = //
        function\s+(.+)         # nom
        \s* \((.*)\) \s*        # arguments
        { (.*) }                # corps
        //


########################################################################
## 2. Les opérations basiques
########################################################################

# Les opérateurs arithmétiques sont les mêmes que pour JavaScript:
1 + 2   # => 3
2 - 1   # => 1
2 * 3   # => 6
4 / 2   # => 2
3 % 2   # => 1


# Les comparaisons sont presque identiques, à l'exception que `==` équivaut au
# `===` de JS, là où le `==` de JS est `~=` en LiveScript, et `===` active la 
# comparaison d'objets et de tableaux, ainsi que les comparaisons strictes 
# (sans conversion de type)
2 == 2          # => true
2 == "2"        # => false
2 ~= "2"        # => true
2 === "2"       # => false

[1,2,3] == [1,2,3]        # => false
[1,2,3] === [1,2,3]       # => true

+0 == -0     # => true
+0 === -0    # => false

# Les opérateurs suivants sont également disponibles: <, <=, > et >=

# Les valeurs logiques peuvent être combinéees grâce aux opérateurs logiques 
# `or`, `and` et `not`
true and false  # => false
false or true   # => true
not false       # => true


# Les collections ont également des opérateurs additionnels
[1, 2] ++ [3, 4]                # => [1, 2, 3, 4]
'a' in <[ a b c ]>              # => true
'name' of { name: 'Chris' }     # => true


########################################################################
## 3. Fonctions
########################################################################        

# Puisque LiveScript est fonctionnel, vous vous attendez à une bonne prise en 
# charge des fonctions. En LiveScript, il est encore plus évident que les 
# fonctions sont de premier ordre:
add = (left, right) -> left + right
add 1, 2        # => 3

# Les fonctions qui ne prennent pas d'arguments peuvent être appelées avec un 
# point d'exclamation!
two = -> 2
two!

# LiveScript utilise l'environnement de la fonction, comme JavaScript.
# A l'inverse de JavaScript, le `=` fonctionne comme un opérateur de 
# déclaration, et il déclarera toujours la variable située à gauche (sauf si
# la variable a été déclarée dans l'environnement parent). 

# L'opérateur `:=` est disponible pour réutiliser un nom provenant de 
# l'environnement parent.


# Vous pouvez extraire les arguments d'une fonction pour récupérer 
# rapidement les valeurs qui vous intéressent dans une structure de données 
# complexe:
tail = ([head, ...rest]) -> rest
tail [1, 2, 3]  # => [2, 3]

# Vous pouvez également transformer les arguments en utilisant les opérateurs
# binaires et unaires. Définir des arguments par défaut est aussi possible.
foo = (a = 1, b = 2) -> a + b
foo!    # => 3

# You pouvez utiliser cela pour cloner un argument en particulier pour éviter 
# les effets secondaires. Par exemple:
copy = (^^target, source) ->
  for k,v of source => target[k] = v
  target
a = { a: 1 }
copy a, { b: 2 }        # => { a: 1, b: 2 }
a                       # => { a: 1 }


# Une fonction peut être curryfiée en utilisant une longue flèche à la place
# d'une courte:
add = (left, right) --> left + right
add1 = add 1
add1 2          # => 3

# Les fonctions ont un argument `it` implicite si vous n'en déclarez pas:
identity = -> it
identity 1      # => 1

# Les opérateurs ne sont pas des fonctions en LiveScript, mais vous pouvez 
# facilement les transformer en fonction:
divide-by-two = (/ 2)
[2, 4, 8, 16].map(divide-by-two).reduce (+)

# Comme dans tout bon langage fonctionnel, vous pouvez créer des fonctions 
# composées d'autres fonctions:
double-minus-one = (- 1) . (* 2)

# En plus de la formule mathématique `f . g`, vous avez les opérateurs `>>`
# et `<<`, qui décrivent l'ordre d'application des fonctions composées. 
double-minus-one = (* 2) >> (- 1)
double-minus-one = (- 1) << (* 2)


# Pour appliquer une valeur à une fonction, vous pouvez utiliser les opérateurs
# `|>` et `<|`:
map = (f, xs) --> xs.map f
[1 2 3] |> map (* 2)            # => [2 4 6]

# La version sans pipe correspond à:
((map (* 2)) [1, 2, 3])

# You pouvez aussi choisir où vous voulez que la valeur soit placée, en 
# marquant la position avec un tiret bas (_):
reduce = (f, xs, initial) --> xs.reduce f, initial
[1 2 3] |> reduce (+), _, 0     # => 6


# Le tiret bas est également utilisé pour l'application partielle,
# que vous pouvez utiliser pour toute fonction:
div = (left, right) -> left / right
div-by-two = div _, 2
div-by-two 4      # => 2


# Pour conclure, LiveScript vous permet d'utiliser les fonctions de rappel.
# (mais vous devriez essayer des approches plus fonctionnelles, comme 
# Promises).
# Un fonction de rappel est une fonction qui est passée en argument à une autre
# fonction:
readFile = (name, f) -> f name
a <- readFile 'foo'
b <- readFile 'bar'
console.log a + b

# Equivalent à:
readFile 'foo', (a) -> readFile 'bar', (b) -> console.log a + b


########################################################################
## 4. Conditionnalités
########################################################################

# Vous pouvez faire de la conditionnalité à l'aide de l'expression `if...else`:
x = if n > 0 then \positive else \negative

# A la place de `then`, vous pouvez utiliser `=>`
x = if n > 0 => \positive
    else        \negative

# Pour les conditions complexes, il vaut mieux utiliser l'expresssion `switch`:
y = {}
x = switch
  | (typeof y) is \number => \number
  | (typeof y) is \string => \string
  | 'length' of y         => \array
  | otherwise             => \object      # `otherwise` et `_` correspondent.

# Le corps des fonctions, les déclarations et les assignements disposent d'un
# `switch` implicite, donc vous n'avez pas besoin de le réécrire: 
take = (n, [x, ...xs]) -->
    | n == 0 => []
    | _      => [x] ++ take (n - 1), xs


########################################################################
## 5. Compréhensions
########################################################################

# Comme en python, vous allez pouvoir utiliser les listes en compréhension,
# ce qui permet de générer rapidement et de manière élégante une liste de 
# valeurs:
oneToTwenty = [1 to 20]
evens       = [x for x in oneToTwenty when x % 2 == 0]

# `when` et `unless` peuvent être utilisés comme des filtres.

# Cette technique fonctionne sur les objets de la même manière. Vous allez
# pouvoir générer l'ensemble de paires clé/valeur via la syntaxe suivante:
copy = { [k, v] for k, v of source }


########################################################################
## 4. Programmation orientée objet
########################################################################

# Bien que LiveScript soit un langage fonctionnel, il dispose d'intéressants
# outils pour la programmation objet. La syntaxe de déclaration d'une classe
# est héritée de CoffeeScript:
class Animal
  (@name, kind) ->
    @kind = kind
  action: (what) -> "*#{@name} (a #{@kind}) #{what}*"

class Cat extends Animal
  (@name) -> super @name, 'cat'
  purr: -> @action 'purrs'

kitten = new Cat 'Mei'
kitten.purr!      # => "*Mei (a cat) purrs*"

# En plus de l'héritage classique, vous pouvez utiliser autant de mixins
# que vous voulez pour votre classe. Les mixins sont juste des objets:
Huggable =
  hug: -> @action 'is hugged'

class SnugglyCat extends Cat implements Huggable

kitten = new SnugglyCat 'Purr'
kitten.hug!     # => "*Mei (a cat) is hugged*"
```

## Lectures complémentaires

Il y a beaucoup plus de choses à dire sur LiveScript, mais ce guide devrait 
suffire pour démarrer l'écriture de petites fonctionnalités.
Le [site officiel](http://livescript.net/) dispose de beaucoup d'information,
ainsi que d'un compilateur en ligne vous permettant de tester le langage!

Jetez également un coup d'oeil à [prelude.ls](http://gkz.github.io/prelude-ls/),
et consultez le channel `#livescript` sur le réseau Freenode.
---
language: Lua
filename: learnlua-fr.lua
contributors:
    - ["Tyler Neylon", "http://tylerneylon.com/"]
translators:
    - ["Roland Yonaba", "http://github.com/Yonaba"]
lang: fr-fr
---

```lua
-- Les commentaires unilignes commencent par un double tiret.

--[[
     Les doubles crochets à la suite du double tiret 
     permettent d'insérer des commentaires multilignes.
--]]

----------------------------------------------------
-- 1. Variables et contrôle d'exécution.
----------------------------------------------------

num = 42  -- Tous les nombres sont de type double.
-- Rassurez vous cependant, les doubles stockés sur 64-bits
-- en réservent 52 pour la valeur exacte des entiers. La
-- précision n'est donc pas un problème pour tout entier qui
-- peut être codé sur moins de 52 bits.

s = 'walternate'  -- Chaines de caractères immuables comme en Python.
t = "une chaine avec des guillemets doubles"
u = [[les double crochets permettent
      d'avoir une chaine de caractères
      sur plusieurs lignes.]]
t = nil  -- Affecte la valeur nulle à t; Lua possède un ramasse-miettes

-- Le do/end définit un bloc de code
while num < 50 do
  num = num + 1  -- Pas d'opérateurs de type ++ ou +=.
end

-- Les structures en if:
if num > 40 then
  print('supérieur à 40')
elseif s ~= 'walternate' then  -- ~= : est différent de.
  -- Le test d'égalité se fait avec == comme en Python.
  io.write('inférieur à 40\n')  -- Écrit par defaut sur la sortie stdout.
else
  -- Les variables sont globales par défaut.
  thisIsGlobal = 5  -- le style camelCase est courant.

  -- Une variable locale est déclarée avec le mot-clé local:
  local line = io.read()  -- Permet de lire la ligne suivante dans stdin.

  -- .. est l'opérateur de concaténation:
  print("L'hiver approche, " .. line)
end

-- Les variables non définies reçoivent par défaut la valeur nil.
foo = anUnknownVariable  -- Maintenant, foo = nil.

aBoolValue = false

-- Seuls nil et false sont des valeurs fausses.
-- Mais 0 et '' sont des valeurs vraies!
if not aBoolValue then print('etait faux') end

-- L'évaluation du 'or' et du 'and' est court-circuité.
-- Comme avec les ternaires du C et du JS: a?b:c
ans = aBoolValue and 'oui' or 'non'  --> 'non'

karlSum = 0
for i = 1, 100 do  -- Les bornes sont incluses dans l'intervalle.
  karlSum = karlSum + i
end

-- Utilisez "100, 1, -1" pour la décrémentation:
fredSum = 0
for j = 100, 1, -1 do fredSum = fredSum + j end

-- En général, l'intervalle est début, fin[, pas].

-- Un autre type de boucle:
repeat
  print('the way of the future')
  num = num - 1
until num == 0


----------------------------------------------------
-- 2. Fonctions.
----------------------------------------------------

function fib(n)
  if n < 2 then return n end
  return fib(n - 2) + fib(n - 1)
end

-- Lua implémente les fermetures et les fonctions anonymes:
function adder(x)
  -- La fonction retournée est créée lorsque adder est appelé
  -- et elle se rappelle de la valeur de x.
  return function (y) return x + y end
end
a1 = adder(9)
a2 = adder(36)
print(a1(16))  --> 25
print(a2(64))  --> 100

-- Les valeurs de retour, les appels de fonction, les assignations
-- supportent tous les listes qui peuvent ne pas correspondre en longueur.
-- Dans ce cas, les variables à assigner en supplément reçoivent nil
-- tandis que les valeurs à attribuer en supplément sont ignorées

x, y = 1, 2 -- x = 1 et y = 2
x, y, z = 1, 2 -- x = 1, y = 2 et z = nil
x, y, z = 1, 2, 3, 4 -- x = 1, y = 2, z = 3, et 4 est ignoré.

function bar(a, b, c)
  print(a, b, c)
  return 4, 8, 15, 16, 23, 42
end

x, y = bar('zaphod')  --> affiche "zaphod nil nil"
-- x = 4, y = 8, les valeurs 15 à 42 sont ignorées.

-- Les fonctions sont des valeurs de première classe 
-- et peuvent être locales/globales.
-- Les déclarations suivantes sont identiques:
function f(x) return x * x end
f = function (x) return x * x end

-- Il en va de même pour les déclarations suivantes:
local function g(x) return math.sin(x) end
local g = function(x) return math.sin(x) end
-- Sauf que pour le dernier cas, même si local g = function(x)
-- est équivalent à local function g(x), il n'est pas possible
-- de faire appel à g à l'intérieur du corps de la fonction (récursion)

-- À moins de déclarer la fonction auparavant:
local g; g  = function (x) return math.sin(x) end

-- À propos, les fonctions trigonométriques interprètent 
-- leurs arguments en radians.
print(math.cos(math.pi)) -- affiche "-1"
print(math.sin(math.pi)) -- affiche "0"

-- Lorsqu'une fonction est appelée avec un seul argument qui est une chaine,
-- les parenthèses peuvent être omises:
print 'hello'  -- équivalent à print('hello').

-- Lorsqu'une fonction est appelée avec un seul argument qui est une table,
-- les parenthèses peuvent aussi être omises.
print {} -- équivalent à print({}).


----------------------------------------------------
-- 3. Tables.
----------------------------------------------------

-- Tables = Seule structure de données en Lua;
--          Ce sont des listes assotiatives.
-- Elles sont similaires aux tables PHP ou aux objets JS :
-- des tables-dictionnaires que l'on peut utiliser en tant que listes.

-- Tables en tant que dictionnaires:

-- Les clés sont des chaines de caractères par défaut:
t = {key1 = 'valeur1', key2 = false}

-- Elles peuvent être indexées avec la notation en point, comme en JS:
print(t.key1)  -- Affiche "valeur1".
t.newKey = {}  -- Ajoute une nouvelle paire clé/valeur.
t.key2 = nil   -- Supprime la clé "key2" de la table.

-- Notation littérale pour toute valeur non nulle en tant que clé:
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28])  -- affiche "tau"

-- La correspondance des clés se fait par valeur pour
-- les nombres et les chaines, mais par référence pour les tables.
a = u['@!#']  -- a = 'qbert'.
b = u[{}]     -- On pourrait s'attendre à 1729, mais l'on obtient nil:
-- b = nil car la clé utilisée n'est pas le même objet que celui
-- utilisé pour stocker la valeur originale 1729.

-- Si une fonction prend en argument une seule table, l'on peut
-- omettre les parenthèses:
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'}  -- Affiche 'Sonmi~451'.

for key, val in pairs(u) do  -- Parcours d'une table.
  print(key, val)
end

-- _G est une table spéciale contenant toutes les variables globales,
-- et donc elle même.
print(_G['_G'] == _G)  -- Affiche 'true'.

-- Tables en tant que listes:

-- De manière implicite, les clés sont des nombres entiers:
v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do  -- #v retourne la taille de la table v si elle est une liste.
  print(v[i])  -- Attention, en Lua, les indices commencent à 1!
end
-- Il n'existe pas vraiment de type 'liste' en Lua, v est juste
-- une table avec des clés qui sont des nombres entiers consécutifs
-- commençant à 1. Lua le traite comme étant une liste.

----------------------------------------------------
-- 3.1 Métatables and métaméthodes.
----------------------------------------------------

-- Une table peut avoir une métatable qui confère à la table
-- un patron/prototype de conception (surcharge d'opération). Nous verrons
-- dans la suite comment les métatables imitent le prototypage du JS.

f1 = {a = 1, b = 2}  -- Représente la fraction a/b.
f2 = {a = 2, b = 3}

-- Ceci créée une erreur:
-- s = f1 + f2

metafraction = {}
function metafraction.__add(f1, f2)
  local sum = {}
  sum.b = f1.b * f2.b
  sum.a = f1.a * f2.b + f2.a * f1.b
  return sum
end

setmetatable(f1, metafraction)
setmetatable(f2, metafraction)

s = f1 + f2  -- appèle __add(f1, f2) de la métatable de f1

-- f1, f2 ne possèdent pas de clé qui pointent vers leur métatable, comme
-- avec les prototypes en JS. Mais l'on peut utiliser getmetatable(f1).
-- La métatable est une table normale avec des clés prédéfinies, comme __add.

-- Mais la ligne suivante génère une erreur puisque s n'a pas de métatable:
-- t = s + s
-- En implémentant de l'orienté objet, comme nous le verrons par la suite,
-- le problème est résolu.

-- Une clé __index dans une métatable mt surcharge l'indexation dans sa table t
-- si la clé est absente de cette table t:
defaultFavs = {animal = 'gru', food = 'donuts'}
myFavs = {food = 'pizza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal  -- Affiche "gru"! merci à la métatable!

-- Ainsi donc, un accès direct à une valeur dans une table via une clé 
-- inexistante (ce qui normalement retourne "nil") conduira à exploiter
-- le champ __index de la métatable. Cela peut être récursif.

-- Le champ __index peut aussi être une fonction (tbl, clé)
-- ce qui permet une gestion plus souple des indexations.

-- Les clés __index, __add,... sont appelées métaméthodes.
-- En voici la liste complète:

-- __add(a, b)                   pour a + b
-- __sub(a, b)                   pour a - b
-- __mul(a, b)                   pour a * b
-- __div(a, b)                   pour a / b
-- __mod(a, b)                   pour a % b
-- __pow(a, b)                   pour a ^ b
-- __unm(a)                      pour -a
-- __concat(a, b)                pour a .. b
-- __len(a)                      pour #a
-- __eq(a, b)                    pour a == b
-- __lt(a, b)                    pour a < b
-- __le(a, b)                    pour a <= b
-- __index(a, b)  <fn ou table>  pour a.b
-- __newindex(a, b, c)           pour a.b = c
-- __call(a, ...)                pour a(...)

----------------------------------------------------
-- 3.2 Pseudo-orienté objet et héritage.
----------------------------------------------------

-- Lua n'implémente pas d'orienté objet par défaut.
-- Mais il reste possible d'imiter de plusieurs manières 
-- le concept de "classe" grâce aux tables et aux métatables.

-- L'explication pour l'exemple qui suit vient juste après.

Dog = {}                                   -- 1.

function Dog:new()                         -- 2.
  local newObj = {sound = 'woof'}          -- 3.
  self.__index = self                      -- 4.
  return setmetatable(newObj, self)        -- 5.
end

function Dog:makeSound()                   -- 6.
  print('Je dis: ' .. self.sound..'!')
end

mrDog = Dog:new()                          -- 7.
mrDog:makeSound()  -- 'Je dis: woof!       -- 8.

-- 1. Dog agit comme une classe; c'est une simple table.
-- 2. L'expression tbl:fn(...) est identique à 
--    tbl.fn(self, ...)
--    La notation : permet de passer par défaut un premier 
--    argument appelé "self" à la fonction tbl.fn
--    Voir 7 & 8 ci-après pour comprendre comment self prend
--    sa valeur.
-- 3. newObj sera une instance de la classe Dog.
-- 4. self = la classe instanciée. Souvent, self = Dog, mais
--    cela peut changer du fait de l'héritage.
--    newObj reçoit les fonctions de self si l'__index des
--    métatables de newObj et de self pointent vers self.
-- 5. Rappel: setmetatable retourne son premier argument.
-- 6. La notation : fonctionne comme au 2, mais cette fois, self
--    est une instance au lieu d'être une classe.
-- 7. Similaire à Dog.new(Dog), donc self = Dog dans new().
-- 8. Similaire à mrDog.makeSound(mrDog); self = mrDog.

----------------------------------------------------

-- Exemple d'héritage:

LoudDog = Dog:new()                           -- 1.

function LoudDog:makeSound()
  local s = self.sound .. ' '                 -- 2.
  print(s .. s .. s..'!')
end

seymour = LoudDog:new()                       -- 3.
seymour:makeSound()  -- 'woof woof woof!'     -- 4.

-- 1. LoudDog reçoit les méthodes et les variables de Dog.
-- 2. self possède une clé 'sound', reçue de new(), voir 3.
-- 3. Similaire à LoudDog.new(LoudDog) et traduit en Dog.new(LoudDog),
--    puisque LoudDog ne possède pas de clé 'new', mais a une métatable
--    qui a la clé __index = Dog.
--    Résulat: la métatable de seymour est LoudDog, et
--    LoudDog.__index = LoudDog. Donc seymour.key deviendra soit égal à
--    seymour.key, LoudDog.key, Dog.key, selon le fait qu'il s'agira
--    de la première table ayant la clé 'key' en question, en remontant
--    dans la hiérarchie.
-- 4. La clé 'makeSound' est trouvée dans LoudDog; cela est similaire
--    à LoudDog.makeSound(seymour).

-- Si besoin est, la méthode new() de la sous-classe est
-- identique à la méthode new() de sa classe mère:
function LoudDog:new()
  local newObj = {}
  -- Prépare self à être la superclasse de newObj:
  self.__index = self
  return setmetatable(newObj, self)
end

----------------------------------------------------
-- 4. Modules.
----------------------------------------------------


--[[ Cette section est mise en commentaire afin que le reste du
--   ce script reste exécutable.
```

```lua
-- Supposons que le fichier mod.lua contienne ceci:
local M = {}

local function sayMyName()
  print('Hrunkner')
end

function M.sayHello()
  print('hello')
  sayMyName()
end

return M

--  Un autre fichier peut exploiter le contenu défini dans mod.lua's:
local mod = require('mod')  -- Exécute le fichier mod.lua.

-- require est le moyen par défaut d'inclure des modules.
-- require agit comme:     (si non trouvé en cache; voir ci-après)
local mod = (function ()
  <contenu de mod.lua>
end)()
-- Comme si le contenu de mod.lua était enveloppé dans le corps d'une fonction,
-- si bien que les variables locales contenues dans mod.lua sont 
-- inaccessibles en dehors de ce module.

-- Le code suivant fonctionne car mod = M (dans mod.lua):
mod.sayHello()  -- Dis bonjour à Hrunkner.

-- Le code suivant génère une erreur car sayMyName est local à mod.lua:
mod.sayMyName()  -- erreur!

-- Les valeurs retournées par require sont mises en cache, ce qui fait
-- qu'un module est toujours chargé une seule fois, même s'il est inclus
-- avec require à plusieurs reprises.

-- Supposons que mod2.lua contienne le code "print('Hi!')".
local a = require('mod2')  -- Affiche "Hi!"
local b = require('mod2')  -- N'affiche rien; et a = b.

-- dofile est identique à require, sauf qu'il ne fait pas de mise en cache:
dofile('mod2')  --> Hi!
dofile('mod2')  --> Hi! (le code de mod2.lua est encore exécuté)

-- loadfile charge le contenu d'un fichier, sans l'exécuter.
f = loadfile('mod2')  -- L'appel f() exécute le contenu de mod2.lua.

-- loadstring est similaire à loadfile, mais pour les chaines de caractères.
g = loadstring('print(343)')  -- Retourne une fonction.
g()  -- Affiche 343; Rien n'est affiché avant cet appel.

--]]

```
## Références

*Les références qui suivent sont en Anglais.*

Les sujets non abordés dans ce tutoriel sont couverts en intégralité par 
les librairies standard:

* La librairie <a href="http://lua-users.org/wiki/StringLibraryTutorial">string</a>
* La librairie <a href="http://lua-users.org/wiki/TableLibraryTutorial">table</a>
* La librairie <a href="http://lua-users.org/wiki/MathLibraryTutorial">math</a>
* La librairie <a href="http://lua-users.org/wiki/IoLibraryTutorial">io</a>
* La librairie <a href="http://lua-users.org/wiki/OsLibraryTutorial">os</a>

Autres références complémentaires:

* <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">Lua pour programmeurs</a>
* <a href="lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">Référence condensée de Lua</a>
* <a href="http://www.lua.org/pil/contents.html">Programmer en Lua</a>
* <a href="http://www.lua.org/manual/">Les manuels de référence Lua</a>

A propos, ce fichier est exécutable. Sauvegardez-le sous le nom *learn.lua* et
exécutez-le avec la commande `lua learn.lua` !

Ce tutoriel a été originalement écrit pour <a href="tylerneylon.com">tylerneylon.com</a> et est aussi 
disponible en tant que <a href="https://gist.github.com/tylerneylon/5853042">gist</a>.
Il a été traduit en français par Roland Yonaba (voir son <a href="http://github.com/Yonaba">github</a>).

Amusez-vous bien avec Lua!
---
language: make
contributors:
    - ["Robert Steed", "https://github.com/robochat"]
translators:
    - ["altaris", "https://github.com/altaris"]
filename: Makefile-fr
lang: fr-fr
---

Un makefile est un fichier qui définit un ensemble de règles liées entre elles
pour créer une ou plusieurs cibles. L'idée est d'effectuer le moins de travail
possible afin de mettre à jour la ou les cibles en fonction des dépendances.

Écrit en un week-end par Stuart Feldman en 1976, le make et les
makefiles sont encore très utilisés (principalement dans les systèmes Unix),
malgré la concurrence et les critiques faites à son égard.

Le programme make a plusieurs variantes. Dans ce tutoriel, nous utiliserons
l'implémentation standard : GNU make.

```make

# Ceci est un commentaire.

# Un makefile devrait être nommé "Makefile" (avec ou sans la
# majuscule). Il peut alors être exécuté par `make <cible>`.
# Ce nommage n'est toutefois pas obligatoire : utiliser
# `make -f "fichier" <cible>`.

# ATTENTION : l'indentation est quant à elle obligatoire, et se fait avec des
# tabulations, pas avec des espaces !

#-----------------------------------------------------------------------
# Les basiques
#-----------------------------------------------------------------------

# Une règle. Elle ne sera exécutée que si fichier0.txt n'existe pas.
fichier0.txt:
	echo "truc" > fichier0.txt
	# Même les commentaires sont transférés dans le terminal.

# Cette règle ne sera exécutée que si fichier0.txt est plus récent que
# fichier1.txt.
fichier1.txt: fichier0.txt
	cat fichier0.txt > fichier1.txt
	# Utiliser la même syntaxe que dans un terminal.
	@cat fichier0.txt >> fichier1.txt
	# @ empêche l'affichage de la sortie texte d'une commande.
	-@echo 'hello'
	# - signifie que la règle devrait continuer à s'exécuter si cette commande
	# échoue.

# Une règle peut avoir plusieurs cibles et plusieurs dépendances.
fichier2.txt fichier3.txt: fichier0.txt fichier1.txt
	touch fichier2.txt
	touch fichier3.txt

# Make affichera un avertissement si le makefile comporte plusieurs règles pour
# une même cible. Cependant les règles vides ne comptent pas, et peuvent être
# utilisées pour ajouter des dépendances plus facilement.

#-----------------------------------------------------------------------
# Fausses règles
#-----------------------------------------------------------------------

# Une fausse règle est une règle qui ne correspond pas à un fichier.
# Par définition, elle ne peut pas être à jour, et donc make l’exécutera à
# chaque demande.
all: maker process

# La déclaration des règles peut être faite dans n'importe quel ordre.
maker:
	touch ex0.txt ex1.txt

# On peut transformer une règle en fausse règle grâce à la cible spéciale
# suivante :
.PHONY: all maker process

# Une règle dépendante d'une fausse règle sera toujours exécutée.
ex0.txt ex1.txt: maker

# Voici quelques exemples fréquents de fausses règles : all, make, clean,
# install...

#-----------------------------------------------------------------------
# Variables automatiques et wildcards
#-----------------------------------------------------------------------

# Utilise un wildcard pour des noms de fichier
process: fichier*.txt
	@echo $^    # $^ est une variable contenant la liste des dépendances de la
				# cible actuelle.
	@echo $@    # $@ est le nom de la cible actuelle. En cas de cibles
				# multiples, $@ est le nom de la cible ayant causé l'exécution
				# de cette règle.
	@echo $<    # $< contient la première dépendance.
	@echo $?    # $? contient la liste des dépendances qui ne sont pas à jour.
	@echo $+    # $+ contient la liste des dépendances avec d'éventuels
				# duplicatas, contrairement à $^.
	@echo $|    # $| contient la liste des cibles ayant préséance sur la cible
				# actuelle.

# Même si la définition de la règle est scindée en plusieurs morceaux, $^
# listera toutes les dépendances indiquées.
process: ex1.txt fichier0.txt
# Ici, fichier0.txt est un duplicata dans $+.

#-----------------------------------------------------------------------
# Pattern matching
#-----------------------------------------------------------------------

# En utilisant le pattern matching, on peut par exemple créer des règles pour
# convertir les fichiers d'un certain format dans un autre.
%.png: %.svg
	inkscape --export-png $^

# Make exécute une règle même si le fichier correspondant est situé dans un sous
# dossier. En cas de conflit, la règle avec la meilleure correspondance est
# choisie.
small/%.png: %.svg
	inkscape --export-png --export-dpi 30 $^

# Dans ce type de conflit (même cible, même dépendances), make exécutera la
# dernière règle déclarée...
%.png: %.svg
	@echo cette règle est choisie

# Dans ce type de conflit (même cible mais pas les mêmes dépendances), make
# exécutera la première règle pouvant être exécutée.
%.png: %.ps
	@echo cette règle n\'est pas choisie si *.svg et *.ps sont présents

# Make a des règles pré établies. Par exemple, il sait comment créer la cible
# *.o à partir de *.c.

# Les makefiles plus vieux utilisent un matching par extension de fichier.
.png.ps:
	@echo cette règle est similaire à une règle par pattern matching

# Utiliser cette règle spéciale pour déclarer une règle comme ayant un
# matching par extension de fichier.
.SUFFIXES: .png

#-----------------------------------------------------------------------
# Variables, ou macros
#-----------------------------------------------------------------------

# Les variables sont des chaînes de caractères.

variable = Ted
variable2="Sarah"

echo:
	@echo $(variable)
	@echo ${variable2}
	@echo $variable    # Cette syntaxe signifie $(n)ame et non pas $(variable) !
	@echo $(variable3) # Les variables non déclarées valent "" (chaîne vide).

# Les variables sont déclarées de 4 manières, de la plus grande priorité à la
# plus faible :
# 1 : dans la ligne de commande qui invoque make,
# 2 : dans le makefile,
# 3 : dans les variables d’environnement du terminal qui invoque make,
# 4 : les variables prédéfinies.

# Assigne la variable si une variable d’environnement du même nom n'existe pas
# déjà.
variable4 ?= Jean

# Empêche cette variable d'être modifiée par la ligne de commande.
override variable5 = David

# Concatène à une variable (avec un espace avant).
variable4 +=gris

# Assignations de variable pour les règles correspondant à un pattern
# (spécifique à GNU make).
*.png: variable2 = Sara # Pour toutes les règles correspondant à *.png, et tous
						# leurs descendants, la variable variable2 vaudra
						# "Sara".
# Si le jeux des dépendances et descendances devient vraiment trop compliqué,
# des incohérences peuvent survenir.

# Certaines variables sont prédéfinies par make :
affiche_predefinies:
	echo $(CC)
	echo ${CXX}
	echo $(FC)
	echo ${CFLAGS}
	echo $(CPPFLAGS)
	echo ${CXXFLAGS}
	echo $(LDFLAGS)
	echo ${LDLIBS}

#-----------------------------------------------------------------------
# Variables : le retour
#-----------------------------------------------------------------------

# Les variables sont évaluées à chaque instance, ce qui peut être coûteux en
# calculs. Pour parer à ce problème, il existe dans GNU make une seconde
# manière d'assigner des variables pour qu'elles ne soient évaluées qu'une seule
# fois seulement.

var := A B C
var2 ::=  $(var) D E F # := et ::= sont équivalents.

# Ces variables sont évaluées procéduralement (i.e. dans leur ordre
# d'apparition), contrairement aux règles par exemple !

# Ceci ne fonctionne pas.
var3 ::= $(var4) et fais de beaux rêves
var4 ::= bonne nuit

#-----------------------------------------------------------------------
# Fonctions
#-----------------------------------------------------------------------

# Make a une multitude de fonctions. La syntaxe générale est
# $(fonction arg0,arg1,arg2...).

# Quelques exemples :

fichiers_source = $(wildcard *.c */*.c)
fichiers_objet  = $(patsubst %.c,%.o,$(fichiers_source))

ls: * src/*
	@echo $(filter %.txt, $^)
	@echo $(notdir $^)
	@echo $(join $(dir $^),$(notdir $^))

#-----------------------------------------------------------------------
# Directives
#-----------------------------------------------------------------------

# Inclut d'autres makefiles.
include meuh.mk

# Branchements conditionnels.
sport = tennis
report:
ifeq ($(sport),tennis)                  # Il y a aussi ifneq.
	@echo 'jeu, set et match'
else
	@echo "C'est pas ici Wimbledon ?"
endif

truc = true
ifdef $(truc)                           # Il y a aussi ifndef.
	machin = 'salut'
endif
```

## Quelques références

### En français

+ [Introduction à Makefile (developpez.com)]
(http://gl.developpez.com/tutoriel/outil/makefile/),
+ [Compilez sous GNU/Linux ! (openclassrooms)]
(https://openclassrooms.com/courses/compilez-sous-gnu-linux).

### En anglais

+ [Documentation de GNU make](https://www.gnu.org/software/make/manual/),
+ [Software carpentry tutorial](http://swcarpentry.github.io/make-novice/),
+ Learn C the hard way [ex2](http://c.learncodethehardway.org/book/ex2.html)
[ex28](http://c.learncodethehardway.org/book/ex28.html).
---
language: markdown
contributors:
- ["Andrei Curelaru", "http://www.infinidad.fr"]
filename: markdown-fr.md
lang: fr-fr
---

Markdown a été créé par John Gruber en 2004. Il se veut être d'une syntaxe
facile à lire et à écrire, aisément convertible en HTML
 (et beaucoup d'autres formats aussi à présent).

Faites moi autant de retours que vous voulez! Sentez vous libre de "forker" 
et envoyer des pull request!


```markdown
<!-- Markdown est une sorte de cousin du HTML, si bien que tout document HTML 
est un document Markdown valide. Autrement dit, vous pouvez utiliser des 
balises HTML dans un fichier Markdown, comme la balise commentaire dans 
laquelle nous sommes à présent, car celle-ci ne sera pas affectée par 
le parser( analyseur syntaxique ) Markdown. -->

<!-- Toutefois, si vous voulez créer un élément HTML dans un fichier Markdown,
 vous ne pourrez pas utiliser du Markdown à l'intérieur de ce dernier. -->

<!--  Le Markdown est implémenté de différentes manières, selon le parser. 
Ce guide va alors tenter de trier les fonctionnalités universelles de celles
spécifiques à un parser.  -->

<!-- Headers ( En-têtes ) -->
<!-- Vous pouvez facilement créer des éléments HTML <h1> à <h6> en précédant
 le texte de votre futur titre par un ou plusieurs dièses ( # ), de un à six,
  selon le niveau de titre souhaité. -->
# Ceci est un <h1>
## Ceci est un <h2>
### Ceci est un <h3>
#### Ceci est un <h4>
##### Ceci est un <h5>
###### Ceci est un <h6>

<!-- 
Markdown fournit également une façon alternative de marquer les h1 et h2 
-->

Ceci est un h1
=============

Ceci est un h2
-------------

<!-- Styles basiques pour du texte -->
<!-- On peut facilement rendre un texte "gras" ou "italique" en Markdown -->

*Ce texte est en italique.*
_Celui-ci aussi._

**Ce texte est en gras.**
__Celui-là aussi.__

***Ce texte a les deux styles.***
**_Pareil ici_**
*__Et là!__*

<!-- Dans le "GitHub Flavored Markdown", utilisé pour interpréter le Markdown 
sur GitHub, on a également le strikethrough ( texte barré ) : -->

~~Ce texte est barré avec strikethrough.~~

<!--  Les Paragraphes sont représentés par une ou plusieurs lignes de texte 
séparées par une ou plusieurs lignes vides. -->

Ceci est un paragraphe. Là, je suis dans un paragraphe, facile non?

Maintenant je suis dans le paragraphe 2.
Je suis toujours dans le paragraphe 2!


Puis là, eh oui, le paragraphe 3!

<!--  
Si jamais vous souhaitez insérer une balise HTML <br />, vous pouvez ajouter 
un ou plusieurs espaces à la fin de votre paragraphe, et en commencer 
un nouveau.
-->

J'ai deux espaces vides à la fin (sélectionnez moi pour les voir). 

Bigre, il y a un <br /> au dessus de moi!

<!-- Les 'Blocs de Citations' sont générés aisément, grâce au caractère > -->

> Ceci est une superbe citation. Vous pouvez même
> revenir à la ligne quand ça vous chante, et placer un `>` 
> devant chaque bout de ligne faisant partie
> de la citation.
> La taille ne compte pas^^ tant que chaque ligne commence par un `>`.

> Vous pouvez aussi utiliser plus d'un niveau
>> d'imbrication!
> Classe et facile, pas vrai?

<!-- les Listes -->
<!-- les Listes non ordonnées sont marquées par des asterisques, 
signes plus ou signes moins. -->

* Item
* Item
* Un autre item

ou

+ Item
+ Item
+ Encore un item

ou

- Item
- Item
- Un dernier item

<!-- les Listes Ordonnées sont générées via un nombre suivi d'un point -->

1. Item un
2. Item deux
3. Item trois

<!-- Vous pouvez même vous passer de tout numéroter, et Markdown générera 
les bons chiffres. Ceci dit, cette variante perd en clarté.-->

1. Item un
1. Item deux
1. Item trois
<!-- ( cette liste sera interprétée de la même façon que celle au dessus ) -->

<!-- Vous pouvez également utiliser des sous-listes -->

1. Item un
2. Item deux
3. Item trois
* Sub-item
* Sub-item
4. Item quatre

<!-- Il y a même des "listes de Taches". Elles génèrent des champs HTML 
de type checkbox. -->

Les [ ] ci dessous, n'ayant pas de [ x ], 
deviendront des cases à cocher HTML non-cochées.

- [ ] Première tache à réaliser.
- [ ] Une autre chose à faire.
La case suivante sera une case à cocher HTML cochée.
- [x] Ça ... c'est fait!

<!-- les Blocs de Code -->
<!-- Pour marquer du texte comme étant du code, il suffit de commencer 
chaque ligne en tapant 4 espaces (ou un Tab) -->

    echo "Ça, c'est du Code!";
    var Ça = "aussi !";

<!-- L'indentation par tab ou série de quatre espaces 
fonctionne aussi à l'intérieur du bloc de code -->

    my_array.each do |item|
       puts item
    end

<!-- Des bouts de code en mode 'inline' s'ajoutent en les entourant de ` -->

La fonction `run()` ne vous oblige pas à aller courir!

<!-- Via GitHub Flavored Markdown, vous pouvez utiliser 
des syntaxes spécifiques -->

\`\`\`ruby 
<!-- mais enlevez les backslashes quand vous faites ça, 
gardez juste ```ruby ( ou nom de la syntaxe correspondant à votre code )-->
def foobar
puts "Hello world!"
end
\`\`\` <!-- pareil, pas de backslashes, juste ``` en guise de fin -->

<-- Pas besoin d'indentation pour le code juste au dessus, de plus, GitHub 
va utiliser une coloration syntaxique pour le langage indiqué après les ``` -->

<!-- Ligne Horizontale (<hr />) -->
<!-- Pour en insérer une, utilisez trois ou plusieurs astérisques ou tirets, 
avec ou sans espaces entre chaque un. -->

***
---
- - -
****************

<!-- Liens -->
<!-- Une des fonctionnalités sympathiques du Markdown est la facilité 
d'ajouter des liens. Le texte du lien entre [ ], l'url entre ( ), 
et voilà l'travail.
-->

[Clic moi!](http://test.com/)

<!-- 
Pour ajouter un attribut Title, collez le entre guillemets, avec le lien. 
-->

[Clic moi!](http://test.com/ "Lien vers Test.com")

<!-- les Liens Relatifs marchent aussi -->

[En avant la musique](/music/).

<!-- Les liens façon "références" sont eux aussi disponibles en Markdown -->

[Cliquez ici][link1] pour plus d'information!
[Regardez aussi par ici][foobar] si vous voulez.

[link1]: http://test.com/ "Cool!"
[foobar]: http://foobar.biz/ "Alright!"

<!--  Le titre peut aussi être entouré de guillemets simples, 
entre parenthèses ou absent. Les références peuvent être placées 
un peu où vous voulez dans le document, et les identifiants 
(link1, foobar, ...) quoi que ce soit tant qu'ils sont uniques -->

<!-- Il y a également le "nommage implicite" qui transforme le texte du lien
 en identifiant -->

[Ceci][] est un lien.

[ceci]: http://ceciestunlien.com/

<!-- mais ce n'est pas beaucoup utilisé. -->

<!-- Images -->
<!-- Pour les images, la syntaxe est identique aux liens, sauf que précédée
 d'un point d'exclamation! -->

![Attribut ALT de l'image](http://imgur.com/monimage.jpg "Titre optionnel")

<!-- Là aussi, on peut utiliser le mode "références" -->

![Ceci est l'attribut ALT de l'image][monimage]

[monimage]: relative/urls/cool/image.jpg "si vous voulez un titre, c'est ici."

<!-- Divers -->
<!-- Liens Automatiques -->

<http://testwebsite.com/> est équivalent à :
[http://testwebsite.com/](http://testwebsite.com/)

<!-- Liens Automatiques pour emails -->

<foo@bar.com>

<!-- Escaping -->
Il suffit de précéder les caractères spécifiques à ignorer par des backslash \

Pour taper *ce texte* entouré d'astérisques mais pas en italique : 
Tapez \*ce texte\*.

<!-- Tableaux -->
<!-- les Tableaux ne sont disponibles que dans le GitHub Flavored Markdown
 et c'est ce n'est pas super agréable d'utilisation. 
 Mais si vous en avez besoin :
 -->

| Col1 | Col2 | Col3 |
| :----------- | :------: | ------------: |
| Alignement Gauche | Centé | Alignement Droite |
| bla | bla | bla |

<!-- ou bien, pour un résultat équivalent : -->

Col 1 | Col2 | Col3
:-- | :-: | --:
Ough que c'est moche | svp | arrêtez

<!-- Fin! -->

```

Pour plus d'information :
 consultez [ici](http://daringfireball.net/projects/markdown/syntax) le post officiel de Jhon Gruber à propos de la syntaxe, 
 et [là](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) la superbe cheatsheet de Adam Pritchard.
---
language: Objective-C
contributors:
    - ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
    - ["Yannick Loriot", "https://github.com/YannickL"]
    - ["Levi Bostian", "https://github.com/levibostian"]
translators:
    - ["Yannick Loriot", "https://github.com/YannickL"]
filename: LearnObjectiveC-fr.m
lang: fr-fr
---

L'Objective-C est un langage de programmation orienté objet réflexif principalement utilisé par Apple pour les systèmes d'exploitations Mac OS X et iOS et leurs frameworks respectifs, Cocoa et Cocoa Touch.

```objective-c
// Les commentaires sur une seule ligne commencent par //

/*
Les
commentaires
multi-lignes
ressemblent
à
ceci
*/

// #import permet d'importer les en-têtes d'autres fichiers
// Utilisez <> pour importer des fichiers globaux (en général des frameworks)
// Utilisez "" pour importer des fichiers locaux (du projet)
#import <Foundation/Foundation.h>
#import "MaClasse.h"

// Si vous activez les modules dans les projets iOS >= 7 ou Mac OS X >= 10.9
// dans Xcode 5, vous pouvez importer les frameworks comme cela :
@import Foundation;

// Le point d'entrée de votre programme est une fonction qui s'appelle main
// et qui retourne un entier comme type
int main (int argc, const char * argv[])
{
    // Créer un groupe de libération automatique de la mémoire pour l'ensemble
    // du programme
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // Si vous utilisez le comptage de référence automatique (ARC), utilisez
    // @autoreleasepool à la place :
    @autoreleasepool {

    // NSLog() permet d'afficher une chaine de caractères dans la console
    // Affiche la chaine de caractères "Bonjour Tous Le Monde !"
    NSLog(@"Bonjour tous le Monde !"); 
 
    ///////////////////////////////////////
    // Les Types & Les Variables
    ///////////////////////////////////////
    
    // La déclaration de primitive
    int maPrimitive1  = 1;
    long maPrimitive2 = 234554664565;
    
    // La déclaration d'objet
    // Il faut mettre un astérisque devant la déclaration d'objet fortement typé
    MaClasse *monObject1 = nil;  // Typage fort
    id       monObject2  = nil;  // Typage faible
    // 'description' est une méthode qui permet de retourner un aperçut de l'objet sous forme textuelle
    // La méthode 'description' est appelée par défaut quand on utilise le paramètre '%@'
    NSLog(@"%@ and %@", monObject1, [monObject2 description]); // Affiche "(null) et (null)"
    
    // Les chaines de caractères
    NSString *chaineMonde = @"Monde";
    NSLog(@"Bonjour tous le %@ !", chaineMonde); // affiche => "Bonjour Tous Le Monde !" 
    // NSMutableString est une chaine mutable
    NSMutableString *chaineMutable = [NSMutableString stringWithString:@"Bonjour tous le"];
    [chaineMutable appendString:@" Monde !"];
    NSLog(@"%@", chaineMutable); // affiche => "Bonjour Tous Le Monde !"
    
    // Les caractères
    NSNumber *laLettreZSousFormeDeNombre = @'Z';
    char laLettreZ                       = [laLettreZSousFormeDeNombre charValue]; // ou 'Z'
    NSLog(@"%c", laLettreZ);

    // Les nombres
    NSNumber *nombreQuaranteDeux = @42;
    int quaranteDeux             = [nombreQuaranteDeux intValue]; // ou 42
    NSLog(@"%i", quaranteDeux);
    
    NSNumber *nombreQuaranteDeuxnonSigne = @42U;
    unsigned int quaranteDeuxnonSigne    = [nombreQuaranteDeuxnonSigne unsignedIntValue];
    NSLog(@"%u", fortyTwoUnsigned);
    
    NSNumber *nombreQuaranteDeuxCourt = [NSNumber numberWithShort:42];
    short quaranteDeuxCourt           = [nombreQuaranteDeuxCourt shortValue]; // ou 42
    NSLog(@"%hi", fortyTwoShort);
    
    NSNumber *nombreQuaranteDeuxLong = @42L;
    long quaranteDeuxLong            = [nombreQuaranteDeuxLong longValue]; // ou 42
    NSLog(@"%li", fortyTwoLong);

    // Les nombres flottants
    NSNumber *nombrePiFlottant = @3.141592654F;
    float piFlottant           = [nombrePiFlottant floatValue]; // ou 3.141592654f
    NSLog(@"%f", piFlottant); // affiche => 3.141592654
    NSLog(@"%5.2f", piFlottant); // affiche => " 3.14"
    
    NSNumber *nombrePiDouble = @3.1415926535;
    double piDouble          = [nombrePiDouble doubleValue]; // ou 3.1415926535
    NSLog(@"%f", piDouble);
    NSLog(@"%4.2f", piDouble); // affiche => "3.14"

    // NSDecimalNumber est une classe pour avoir plus de précision sur les nombres
    // flottants et les doubles
    NSDecimalNumber *decNumUn   = [NSDecimalNumber decimalNumberWithString:@"10.99"];
    NSDecimalNumber *decNumDeux = [NSDecimalNumber decimalNumberWithString:@"5.002"];
    // NSDecimalNumber ne permet pas d'utiliser les opérations standards (+, -, *, /)
    // Il faut utiliser les méthodes suivantes à la place :
    [decNumUn decimalNumberByAdding:decNumDeux]; 
    [decNumUn decimalNumberBySubtracting:decNumDeux];
    [decNumUn decimalNumberByMultiplyingBy:decNumDeux];
    [decNumUn decimalNumberByDividingBy:decNumDeux];
    NSLog(@"%@", decNumUn); // affiche => 10.99 comme NSDecimalNumber est immuable

    // Les booléens
    NSNumber *ouiNumber = @YES;
    NSNumber *nonNumber = @NO;
    // ou
    BOOL ouiBool = YES;
    BOOL nonBool  = NO;
    NSLog(@"%i", ouiBool); // affiche => 1

    // Les listes
    // Une liste peut contenir uniquement des objets
    NSArray *uneListe         = @[@1, @2, @3, @4];
    NSNumber *troisiemeNombre = uneListe[2];
    NSLog(@"Troisième nombre = %@", troisiemeNombre); // affiche "Troisième nombre = 3"
    // NSMutableArray est une version mutable de NSArray
    // Cela permet de modifier la liste en ajoutant de nouveaux éléments et en supprimant ou
    // changeant de place des objets déjà présent
    // C'est très pratique, mais pas aussi performant que l'utilisation de la classe NSArray
    NSMutableArray *listeMutable = [NSMutableArray arrayWithCapacity:2];
    [listeMutable addObject:@"Bonjour tous le"];
    [listeMutable addObject:@"Monde"];
    [listeMutable removeObjectAtIndex:0];
    NSLog(@"%@", [listeMutable objectAtIndex:0]); // affiche => "Monde"

    // Les dictionnaires
    // Un dictionnaire est un ensemble de clés : valeurs
    NSDictionary *unDictionnaire = @{ @"cle1" : @"valeur1", @"cle2" : @"valeur2" };
    NSObject *valeur             = unDictionnaire[@"Une clé"];
    NSLog(@"Objet = %@", valeur); // affiche "Objet = (null)"
    // NSMutableDictionary est un dictionnaire mutable, c-à-d que l'on peut modifier
    NSMutableDictionary *dictionnaireMutable = [NSMutableDictionary dictionaryWithCapacity:2];
    [dictionnaireMutable setObject:@"valeur1" forKey:@"cle1"];
    [dictionnaireMutable setObject:@"valeur2" forKey:@"cle2"];
    [dictionnaireMutable removeObjectForKey:@"cle1"];

    // Les ensembles
    // Un ensemble ne peut contenir de duplicatas contrairement aux NSArray
    NSSet *ensemble = [NSSet setWithObjects:@"Salut", @"Salut", @"Monde", nil];
    NSLog(@"%@", ensemble); // affiche => {(Salut, Monde)} (Pas forcément dans le même ordre)
    // NSMutableSet est un ensemble mutable 
    NSMutableSet *ensembleMutable = [NSMutableSet setWithCapacity:2];
    [ensembleMutable addObject:@"Salut"];
    [ensembleMutable addObject:@"Salut"];
    NSLog(@"%@", ensembleMutable); // affiche => {(Salut)}

    ///////////////////////////////////////
    // Les Operateurs
    ///////////////////////////////////////
    
    // Les opérateurs sont pareil qu'en C
    // Par exemple :
    2 + 5; // => 7
    4.2f + 5.1f; // => 9.3f
    3 == 2; // => 0 (NO)
    3 != 2; // => 1 (YES)
    1 && 1; // => 1 (et logique)
    0 || 1; // => 1 (ou logique)
    ~0x0F; // => 0xF0 (négation bit à bit)
    0x0F & 0xF0; // => 0x00 (et bit à bit)
    0x01 << 1; // => 0x02 (décalage à gauche (par 1))

    ///////////////////////////////////////
    // Les Structures de Contrôle
    ///////////////////////////////////////

    // Structure "Si-Sinon" (If-Else) 
    if (NO)
    {
        NSLog(@"Je ne suis jamais affiché");
    } else if (0)
    {
        NSLog(@"Je ne suis jamais affiché aussi");
    } else
    {
        NSLog(@"Je suis affiché");
    }

    // Structure "Selon" (Switch)
    switch (2)
    {
        case 0:
        {
            NSLog(@"Je ne suis jamais affiché");
        } break;
        case 1:
        {
            NSLog(@"Je ne suis jamais affiché aussi");
        } break;
        default:
        {
            NSLog(@"Je suis affiché");
        } break;
    }
    
    // Structure de boucle "Tant Que" (While)
    int ii = 0;
    while (ii < 4)
    {
        NSLog(@"%d,", ii++); // ii++ incrémente ii après avoir utilisé sa valeur
    } // => affiche "0," 
      //            "1,"
      //            "2,"
      //            "3,"

    // Structure de boucle "Pour" (For)
    int jj;
    for (jj=0; jj < 4; jj++)
    {
        NSLog(@"%d,", jj);
    } // => affiche "0," 
      //            "1,"
      //            "2,"
      //            "3,"
     
    // Structure de boucle "Pour Chaque" (Foreach)
    NSArray *valeurs = @[@0, @1, @2, @3];
    for (NSNumber *valeur in valeurs)
    {
        NSLog(@"%@,", valeur);
    } // => affiche "0," 
      //            "1,"
      //            "2,"
      //            "3,"

    // Structure "Essayer-Attraper-Finalement" (Try-Catch-Finally)
    @try
    {
        @throw [NSException exceptionWithName:@"FileNotFoundException"
                            reason:@"Fichier non trouvé" userInfo:nil];
    } @catch (NSException * e)
    {
        NSLog(@"Une exception est survenue : %@", e);
    } @finally
    {
        NSLog(@"Finalement");
    } // => affiche "Une exception est survenue : Fichier non trouvé"
      //            "Finalement"
 
    ///////////////////////////////////////
    // Les Objets
    ///////////////////////////////////////
    
    // Définis et créé une instance d'objet en allouant un espace mémoire puis en
    // l'initialisant. Un objet n'est pas complétement fonctionnel tant que les deux
    // étapes précédentes ne sont pas terminées
    MaClass *monObjet = [[MaClass alloc] init];
        
    // L'Objet en Objective-C est basé sur le principe d'envoie de message et non sur
    // celui d'appel de méthode comme la plupart des autres langages
    // C'est un détail important car cela veut dire que l'on peut envoyer un message
    // à un objet qui ne possède pas la méthode demandée sans aucune incidence sur le
    // fonctionnement du programme (aucune exception ne sera levée)
    [myObject instanceMethodWithParameter:@"Steve Jobs"];

    // Nettoie la mémoire qui a été utilisée dans le programme
    [pool drain];

    // Fin de l'@autoreleasepool
    }
    
    // Fin du programme
    return 0;
}

///////////////////////////////////////
// Les Classes et Les Fonctions
///////////////////////////////////////

// Déclaration d'une classe dans un en-tête de fichier (MaClasse.h) :
// La déclaration d'une classe en Objective-C commence par la déclaration de son interface :
// @interface NomDeLaClasse : NomDeLaClasseParent <ListeDesProtocoles>
// {
//    type nom; // Déclaration d'une variable;
// }
// @property type nom; // Déclaration d'une propriété
// -/+ (type)nomDeLaMethode; // Déclaration d'une methode
// @end // Termine la déclaration
// NSObject est la classe de base (super classe) en Objective-C
@interface MaClasse : NSObject <MonProtocole>
{
    int nombre; // Accès protégé par défaut (équivalent à '@protected int nombre;')
    @private id donnee; // Accès privé (il est plus pratique de le faire dans l'implémentation)
    NSString *nom; 
}
// Les propriétés permettent de générer les accésseurs/affecteurs publiques à la compilation
// Par défaut, le nom de l'affecteur est la chaine 'set' suivi par le nom de la @property
@property int propInt; // Nom de l'affecteur = 'setPropInt'
@property (copy) id copyId; // (copy) => Copie l'objet pendant l'affectation
// (readonly) => Ne peut pas affecté la variable en dehors de l'@interface
// Il faut utiliser le mot clé '@synthesize' dans l'@implementation pour créer l'accésseur
@property (readonly) NSString *roString;
// Vous pouvez aussi personnaliser les noms des accésseurs ou des affecteurs
@property (getter=longeurGet, setter=longeurSet:) int longeur;
 
// Methodes
+/- (TypeDeRetour)signatureDeLaMethode:(TypeDuParametre *)nomDuParametre;

// '+' signifie que c'est une méthode de classe (statique) :
+ (NSString *)methodeDeClasse;
+ (MaClasse *)maClasseDepuisLaHauteur:(NSNumber *)hauteurParDefaut;

// '-' pour les méthodes d'instances :
- (NSString *)methodeInstanceAvecUnParametre:(NSString *)string;
- (NSNumber *)methodeInstanceAvecUnParametre:(NSString*)string puisUnDeuxieme:(NSNumber *)number;

// Contructeur avec des arguments :
- (id)initAvecDistance:(int)distanceParDefault;
// Les méthodes en Objective-C sont très descriptives

@end // Fin de l'interface


// Voici un exemple d'utilisation de MaClasse
MaClasse *maClasse = [[MaClasse alloc] init]; // créer une instance de MaClasse
[maClasse setNombre:10]; 
NSLog(@"%d", [maClasse nombre]); // affiche => 10
[myClass longeurSet:32];
NSLog(@"%i", [maClasse longeurGet]); // affiche => 32
// Pour des raisons pratiques vous pouvez aussi utiliser la notation en point pour accéder aux 
// variables d'instances :
maClasse.nombre = 45;
NSLog(@"%i", maClasse.nombre); // maClasse => 45

// Pour appeler une méthode de classe :
NSString *s1 = [MaClasse methodeDeClasse];
MaClasse *m2 = [MaClasse maClasseDepuisLaHauteur:38];

// Pour appeler une méthode d'instance :
MaClasse *maClasse = [[MaClasse alloc] init]; // Créer une instance de MaClasse
NSString *stringDepuisUneInstanceDeMethode = [maClasse methodeInstanceAvecUnParametre:@"Salut"];

// Les sélecteurs sont un moyen de décrire les méthodes dynamiquement
// Ils sont utilisés pour appeler des méthodes de classe et avoir des pointeurs de fonctions
// facilement manipulable
// SEL est un type de donnée et @selector retourne un selecteur à partir d'un nom de methode
SEL selecteur = @selector(methodeInstanceAvecUnParametre:puisUnDeuxieme:); 
if ([maClasse respondsToSelector:selecteur]) { // Vérifie si la classe possède la méthode
    // Met les arguments de la méthode dans un seul objet pour l'envoyer via la fonction
    // performSelector:withObject: 
    NSArray *arguments = [NSArray arrayWithObjects:@"Hello", @4, nil];
    [myClass performSelector:selectorVar withObject:arguments]; // Appele la méthode via le sélecteur
}
else {
    // NSStringFromSelector() retourne une chaine de caractères à partir d'un sélecteur
    NSLog(@"MaClasse ne possède pas de méthode : %@", NSStringFromSelector(selecteur));
}

// Le fichier d'implémentation de la classe MaClasse (MaClasse.m) doit commencer comme ceci :
@implementation MaClasse {
    long distance; // Variable d'instance privée (équivalent à @private dans l'interface)
    NSNumber hauteur;
}

// Pour accéder à une variable depuis l'implémentation on peut utiliser le _ (tiret bas) devant le nom
// de la variable :
_nombre = 5;
// Accès d'une variable définie dans le fichier d'implémentation :
distance = 18;
// Pour utiliser la variable définie par l'intermédiaire de @property, il faut utiliser @synthesize 
// qui permet de créer les affecteurs et les accésseurs correspondants :
@synthesize roString = _roString; // _roString est maintenant disponible dans l'implementation

// A l'inverse dela méthode 'init' qui est appelée lors de la création d'un objet, la fonction 
// 'dealloc' est appelée quand l'objet est supprimé
- (void)dealloc
{
    [hauteur release]; // Si vous n'utilisez pas ARC, pensez bien à supprimer l'objet 
    [super dealloc];   // et à appeler la méthode 'dealloc' de la classe parent 
}

// Les constructeurs sont des fonctions qui permettent d'instancier un objet
// 'init' est le constructeur par défaut en Objective-C
- (id)init
{
    if ((self = [super init])) // 'super' est utilisé pour appeler la méthode de la classe parent
    {
        self.count = 1; // 'self' permet d'appeler la méthode de l'instance courante
    }
    return self;
}

// Vous pouvez aussi créer des constructeurs avec des arguments
// Attention : chaque nom de constructeur doit absolument commencer par 'init'
- (id)initAvecUneDistance:(int)distanceParDefault 
{
    if ((self = [super init]))
    {
        distance = distanceParDefault;
        return self;
    }
}

// Implémentation d'une méthode de classe
+ (NSString *)methodDeClasse
{
    return [[self alloc] init];
}

+ (MaClasse *)maClasseDepuisUneHauteur:(NSNumber *)hauteurParDefaut
{
    hauteur = hauteurParDefaut;
    return [[self alloc] init];
}

// Implémentation d'une méthode d'instance
- (NSString *)methodeInstanceAvecUnParametre:(NSString *)string
{
    return @"Ma chaine de caractère";
}

- (NSNumber *)methodeInstanceAvecUnParametre:(NSString*)string puisUnDeuxieme:(NSNumber *)number
{
    return @42;
}

// Pour créer une méthode privée, il faut la définir dans l'implementation et non pas dans 
// l'interface
- (NSNumber *)methodePrivee
{
    return @72;
}

[self methodePrivee]; // Appel de la méthode privée

// Implémentation d'une méthode qui est déclarée dans <MonProtocole>
- (void)methodeDuProtocole
{
    // expressions
}

@end // Fin de l'implémentation

/*
 * Un protocole déclare des méthodes et propriétés que chaque implémentation doit avoir afin de se
 * conformer à celui-ci
 * Un protocole n'est pas une classe, c'est juste une interface
 */
@protocol MonProtocole
    - (void)methodeDuProtocole;
@end


///////////////////////////////////////
// Gestion de la mémoire
///////////////////////////////////////
/* 
À chaque fois qu'un objet est créé dans l'application, un bloc mémoire doit être alloué.
Quand l'application en a fini avec cet objet, la mémoire doit être libérée afin d'éviter les fuites
mémoires
Il n'y a pas de ramasse-miettes en Objective-C contrairement à Java par exemple. La gestion de la
mémoire repose sur le comptage de référence qui, pour chaque objet, assigne un compteur qui permet
de connaitre le nombre de référence qui l'utilise.

Le principe est le suivant :
Lorsque l'objet est créé, le compteur est initialisé à 1. Quand une instance détient un objet, le
compteur est incrémenté de un. Quand l'objet est libéré, le compteur est décrémenté de un.  Au moment
où le compteur arrive à zéro, l'objet est supprimé de la mémoire

Une bonne pratique à suivre quand on travaille avec des objets est la suivante :
(1) créer un objet, (2) utiliser l'objet, (3) supprimer l'objet de la mémoire
*/

MaClasse *classeVar = [MyClass alloc]; // 'alloc' incrémente le compteur de référence
[classeVar release]; // Décrémente le compteur de rérence
// 'retain' incrémente le compteur de référence
// Si 'classeVar' est libéré, l'objet reste en mémoire car le compteur de référence est non nul
MaClasse *nouvelleVar = [classVar retain];
[classeVar autorelease]; // Supprime l'appartenance de l'objet à la fin du bloc

// Les @property peuvent utiliser 'retain' et 'assign' 
@property (retain) MaClasse *instance; // Libère l'ancienne valeur et retient la nouvelle
@property (assign) NSSet *set; // Pointeur vers la valeur sans retenir/libérer l'ancienne valeur

// Automatic Reference Counting (ARC)
// La gestion de la mémoire étant pénible, depuis iOS 4 et Xcode 4.2, Apple a introduit le comptage de
// référence automatique (Automatic Reference Counting en anglais)
// ARC est une fonctionnalité du compilateur qui permet d'ajouter les 'retain', 'release' et 'autorelease'
// automatiquement. Cela veut dire que lorsque vous utilisez ARC vous ne devez plus utiliser ces mot-clés
MaClasse *arcMaClasse = [[MaClasse alloc] init]; 
// ... code utilisant arcMaClasse
// Sans ARC, vous auriez dû appeler [arcMaClasse release] après avoir utilisé l'objet. Mais avec ARC
// activé il n'est plus nécessaire de le faire car le compilateur ajoutera l'expréssion automatiquement
// pour vous

// Les mots clés 'assign' et 'retain', avec ARC sont respectivement remplacé par 'weak' et 'strong'
@property (weak) MaClasse *weakVar; // 'weak' ne retient pas l'objet. Si le compteur de référence
// descend à zero, weakVar sera automatiquement mis à nil
@property (strong) MaClasse *strongVar; // 'strong' prend possession de l'objet comme le ferait avec
// le mot-clé 'retain'

// Pour l'instanciation des variables (en dehors de @property), vous pouvez utiliser les instructions
// suivantes :
__strong NSString *strongString; // Par défaut. La variable est retenue en mémoire jusqu'à la fin
//  de sa portée
__weak NSSet *weakSet; // Maintient une référence vers l'objet sans incrémenter son compteur de référence :
// Lorsque l'objet sera supprimé, weakSet sera mis à nil automatiquement
__unsafe_unretained NSArray *unsafeArray; // Comme __weak, mais la variable n'est pas mis à nil quand
// l'objet est supprimé

```

##  Lectures Complémentaires

[La Page Wikipedia de l'Objective-C](http://fr.wikipedia.org/wiki/Objective-C)

[iOS pour les écoliers : Votre première app iOS](http://www.raywenderlich.com/fr/39272/ios-pour-les-ecoliers-votre-premiere-app-ios-partie-12)

[Programming with Objective-C. Apple PDF book](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf)
---
name: perl
category: language
language: perl
filename: learnperl-fr.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
    - ["Matteo Taroli", "http://www.matteotaroli.be"]
translators:
    - ["Matteo Taroli", "http://www.matteotaroli.be"]
lang: fr-fr
---
Perl 5 est un langage de programmation riche en fonctionnalité, avec plus de 25 ans de développement.

Perl 5 fonctionne sur plus de 100 plateformes, allant des pc portables aux mainframes et
est autant adapté à un prototypage rapide qu'à des projets de grande envergure.

```perl
#  Les commentaires en une ligne commencent par un dièse


#### Types de variables de Perl

# Les variables comment par un symbole précisant le type.
# Un nom de variable valide commence par une lettre ou un underscore,
# suivi d'un nombre quelconque de lettres, chiffres ou underscores.

### Perl a trois types principaux de variables: $scalaire, @tableau and %hash

## Scalaires
#  Un scalaire représente une valeur unique :
my $animal = "chameau";
my $reponse = 42;

# Les valeurs scalaires peuvent être des strings, des entiers ou des nombres à virgule flottante
# et Perl les convertira automatiquement entre elles quand nécessaire.

## Tableaux
#  Un tableau représente une liste de valeurs :
my @animaux = ("chameau", "lama", "chouette");
my @nombres = (23, 42, 69);
my @melange = ("chameau", 42, 1.23);

## Hashes
#  Un hash représente un ensemble de paires de clé/valeur :
my %fruit_couleur = ("pomme", "rouge", "banane", "jaune");

# Vous pouvez utiliser des espaces et l'opérateur "=>" pour les disposer plus joliment :

my %fruit_couleur = (
	pomme  => "rouge",
	banane => "jaune"
);

# Les scalaires, tableaux et hashes sont plus amplement documentés dans le perldata
# (perldoc perldata)

# Des types de données plus complexes peuvent être construits en utilisant des références,
# vous permettant de construire des listes et des hashes à l'intérieur d'autres listes et hashes.

#### Conditions et boucles

# Perl possède la plupart des conditions et boucles habituelles.

if ($var) {
  ...
} elsif ($var eq 'bar') {
  ...
} else {
  ...
}

unless (condition) {
  ...
}
# Ceci est fourni en tant que version plus lisible de "if (!condition)"

# la postcondition à la sauce Perl

print "Yow!" if $zippy;
print "Nous n'avons pas de banane." unless $bananes;

#  while
while (condition) {
  ...
}

# boucle for et iteration
for (my $i = 0; $i < $max; $i++) {
  print "l'index est $i";
}

for (my $i = 0; $i < @elements; $i++) {
  print "L'élément courant est " . $elements[$i];
}

for my $element (@elements) {
  print $element;
}

# implicitement

# La variable de contexte scalaire $_ est utilisée par défaut dans différentes
# situations, comme par exemple dans la boucle foreach ou en argument par défaut
# de la plupart des fonctions pour en simplifier l'écriture.

# Dans l'exemple suivant, $_ prends successivement la valeur de
# chaque élément de la liste.  

for (@elements) {
  print; # affiche le contenu de $_
}


#### Expressions régulières

# Le support des expressions régulières par Perl est aussi large que profond
# et est sujet à une longue documentation sur perlrequick, perlretut et ailleurs.
# Cependant, pour faire court :

# Simple correspondance
if (/foo/)       { ... }  # vrai si $_ contient "foo"
if ($a =~ /foo/) { ... }  # vrai si $a contient "foo"

# Simple substitution

$a =~ s/foo/bar/;         # remplace le premier foo par bar dans $a
$a =~ s/foo/bar/g;        # remplace TOUTES LES INSTANCES de foo par bar dans $a


#### Fichiers et E/S

# Vous pouvez ouvrir un fichier pour y écrire ou pour le lire avec la fonction "open()".

open(my $in,  "<",  "input.txt")  or die "Impossible d'ouvrir input.txt: $!";
open(my $out, ">",  "output.txt") or die "Impossible d'ouvrir output.txt: $!";
open(my $log, ">>", "my.log")     or die "Impossible d'ouvrir my.log: $!";

# Vous pouvez lire depuis un descripteur de fichier grâce à l'opérateur "<>".
# Dans un contexte scalaire, il lit une seule ligne depuis le descripteur de fichier
# et dans un contexte de liste, il lit le fichier complet, assignant chaque ligne à un
# élément de la liste :

my $ligne = <$in>
my $lignes = <$in>

#### Ecrire des fonctions

# Ecrire des fonctions est facile :

sub logger {
  my $logmessage = shift;

  open my $logfile, ">>", "my.log" or die "Impossible d'ouvrir my.log: $!";

  print $logfile $logmessage;
}

# Maintenant, nous pouvons utiliser cette fonction comme n'importe quelle fonction intégrée :

logger("On a une fonction de logging!!");
```

#### Utiliser des modules Perl

Les modules Perl fournissent une palette de fonctionnalités vous évitant de réinventer la roue et peuvent être téléchargés depuis CPAN (http://www.cpan.org/). Un certain nombre de modules populaires sont inclus dans la distribution même de Perl.

Perlfaq contiens des questions et réponses liées aux tâches habituelles et propose souvent des suggestions quant aux bons modules à utiliser.

#### Pour en savoir plus
 - [perl-tutorial](http://perl-tutorial.org/)
 - [Learn at www.perl.com](http://www.perl.org/learn.html)
 - [perldoc](http://perldoc.perl.org/)
 - and perl built-in : `perldoc perlintro`
---
language: PHP
filename: php-fr.php
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
translators:
    - ["Pascal Boutin", "http://pboutin.net/"]
lang: fr-fr
---

This document describes PHP 5+.

```php
 // Le code PHP doit être placé à l'intérieur de balises '<?php'

// Si votre fichier php ne contient que du code PHP, il est
// généralement recommandé de ne pas fermer la balise '?>'

// Deux barres obliques amorcent un commentaire simple.

# Le dièse aussi, bien que les barres obliques soient plus courantes

/*
    Les barres obliques et les astérisques peuvent être utilisés
    pour faire un commentaire multi-lignes.
*/

// Utilisez "echo" ou "print" afficher une sortie
print('Hello '); // Affiche "Hello " sans retour à la ligne

// Les parenthèses sont facultatives pour print et echo
echo "World\n"; // Affiche "World" avec un retour à la ligne

// toutes les instructions doivent se terminer par un point-virgule

// Tout ce qui se trouve en dehors des <?php ?> est automatiquement
// affiché en sortie
Hello World Again!
<?php


/************************************
 * Types & Variables
 */

// Les noms de variables débutent par le symbole $
// Un nom de variable valide commence par une lettre ou un souligné,
// suivi de n'importe quelle lettre, nombre ou de soulignés.

// Les valeurs booléenes ne sont pas sensibles à la casse
$boolean = true;  // ou TRUE ou True
$boolean = false; // ou FALSE ou False

// Entiers (integers)
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (un 0 devant la valeur désigne une valeur octale)
$int4 = 0x0F; // => 15 (un 0x devant la valeur désigne une valeur hexadécimale)

// Réels (floats, doubles)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// Suppression d'une variable
unset($int1);

// Arithmétique
$sum        = 1 + 1; // 2 (addition)
$difference = 2 - 1; // 1 (soustraction)
$product    = 2 * 2; // 4 (produit)
$quotient   = 2 / 1; // 2 (division)

// Arithmétique (raccourcis)
$number = 0;
$number += 2;      // Incrémente $number de 2
echo $number++;    // Affiche 2 (incrémente après l'évaluation)
echo ++$number;    // Affiche 4 (incrémente avant l'évaluation)
$number /= $float; // Divise et assigne le quotient à $number

// Les chaînes de caractères (strings) doivent être à
// l'intérieur d'une paire d'apostrophes
$sgl_quotes = '$String'; // => '$String'

// Évitez les guillemets sauf pour inclure le contenu d'une autre variable
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// Les caractères spéciaux sont seulement échappés avec des guillemets
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// En cas de besoin, placez la variable dans des accolades
$money = "I have $${number} in the bank.";

// Depuis PHP 5.3, Nowdoc peut être utilisé pour faire des chaînes
// multi-lignes non-interprétées
$nowdoc = <<<'END'
Multi line
string
END;

// Heredoc peut être utilisé pour faire des chaînes multi-lignes interprétées
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// La concaténation de chaînes se fait avec un .
echo 'This string ' . 'is concatenated';


/********************************
 * Constantes
 */

// Une constante est déclarée avec define()
// et ne peut jamais être changée durant l'exécution

// un nom valide de constante commence par une lettre ou un souligné,
// suivi de n'importe quelle lettre, nombre ou soulignés.
define("FOO",     "something");

// on peut accéder à une constante en utilisant directement son nom
echo 'This outputs '.FOO;


/********************************
 * Tableaux (array)
 */

// Tous les tableaux en PHP sont associatifs (hashmaps),

// Fonctionne dans toutes les versions de PHP
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);

// PHP 5.4 a introduit une nouvelle syntaxe
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];

echo $associative['One']; // affiche 1

// Dans une liste simple, l'index est automatiquement attribué en tant que clé
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"

// Ajoute un élément à la fin du tableau
$array[] = 'Four';

// Retrait d'un élément du tableau
unset($array[3]);

/********************************
 * Affichage
 */

echo('Hello World!');
// Affiche Hello World! dans stdout.
// Stdout est la page web si on exécute depuis un navigateur.

print('Hello World!'); // Pareil à "écho"

// Pour écho, vous n'avez pas besoin des parenthèses
echo 'Hello World!';
print 'Hello World!'; // Pour print non plus

$paragraph = 'paragraph';

echo 100;        // Affichez un scalaire directement
echo $paragraph; // ou des variables

// Si le raccourci de sortie est configuré, ou si votre version de PHP est
// 5.4.0+, vous pouvez utiliser ceci:
?>
<p><?= $paragraph ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $x contient maintenant la même valeur que $y
$z = &$y;
// $z contient une référence vers $y. Changer la valeur de
// $z changerait également la valeur de $y, et vice-versa.
// $x resterait inchangé comme la valeur initiale de $y

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0

// Affiche le type et la valeur de la variable dans stdout
var_dump($z); // prints int(0)

// Affiche la variable dans stdout dans un format plus convivial
print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three )

/********************************
 * Logique
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// assert affiche un avertissement dans son argument n'est pas vrai

// Ces comparaisons vont toujours être vraies, même si leurs
// types ne sont pas les mêmes.
assert($a == $b); // égalité
assert($c != $a); // inégalité
assert($c <> $a); // inégalité (moins courant)
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// Ces comparaisons vont seulement être vraies si les types concordent.
assert($c === $d);
assert($a !== $d);
assert(1 === '1');
assert(1 !== '1');

// Opérateur 'spaceship' depuis PHP 7
$a = 100;
$b = 1000;

echo $a <=> $a; // 0 car ils sont égaux
echo $a <=> $b; // -1 car $a < $b
echo $b <=> $a; // 1 car $b > $a

// Les variables peuvent être transtypées dépendamment de leur usage.

$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2

$string = 'one';
echo $string + $string; // => 0
// Donne 0 car l'opérateur + ne peut pas transtyper la chaîne 'one' en un nombre

// On peut également transtyper manuellement pour utiliser
// une variable dans un autre type

$boolean = (boolean) 1; // => true

$zero = 0;
$boolean = (boolean) $zero; // => false

// Il y a également des fonctions dédiées pour transtyper
$integer = 5;
$string = strval($integer);

$var = null; // Valeur nulle


/********************************
 * Structures de contrôle
 */

if (true) {
    print 'Je suis affiché';
}

if (false) {
    print 'Je ne le suis pas';
} else {
    print 'Je suis affiché';
}

if (false) {
    print 'Je ne suis pas affiché';
} elseif (true) {
    print 'Je le suis';
}

// Opérateur ternaire
print (false ? 'N\'est pas affiché' : 'L\'est');

// Opérateur ternaire depuis PHP 5.3
// équivalent de $x ? $x : 'Does'
$x = false;
print($x ?: 'Does');

// depuis PHP 7, on peut facilement vérifier si une valeur est nulle
$a = null;
$b = 'Hello World';
echo $a ?? 'a is not set'; // Affiche 'a is not set'
echo $b ?? 'b is not set'; // Affiche 'Hello World'


$x = 0;
if ($x === '0') {
    print 'Pas affiché';
} elseif($x == '1') {
    print 'Pas affiché';
} else {
    print 'Affiché';
}


// Cette syntaxe alternative est particulièrement utile avec du HTML:
?>

<?php if ($x): ?>
<p>Ceci est affiché si $x est vrai</p>
<?php else: ?>
<p>Ceci est affiché si $x est faux</p>
<?php endif; ?>

<?php

// On peut également utiliser une condition multiple (switch case)
switch ($x) {
    case '0':
        print 'Les switch font du transtypage implicite';
        break; // Il est important de déclaré un 'break', sinon les cas
               // 'two' et 'three' seront évalués
    case 'two':
    case 'three':
        // Si $x == 'two' || $x == 'three'
        break;
    default:
        // Si aucun cas n'a été vrai
}

// Structures itératives (for, while, do while)
$i = 0;
while ($i < 5) {
    echo $i++;
}; // Affiche "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // Affiche "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // Affiche "0123456789"

echo "\n";

$wheels = ['bicycle' => 2, 'car' => 4];

// Les boucles 'foreach' sont utiles pour parcourir les tableaux
foreach ($wheels as $wheel_count) {
    echo $wheel_count;
} // Affiche "24"

echo "\n";

// Il est également possible d'accéder aux clés du tableau
foreach ($wheels as $vehicle => $wheel_count) {
    echo "The $vehicle have $wheel_count wheels";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // Permet d'arrêter la boucle
    }
    echo $i++;
} // Affiche "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // Permet de passer immédiatement à l'itération suivante
    }
    echo $i;
} // Affiche "0124"


/********************************
 * Fonctions
 */

// On peut déclarer une fonction avec le mot clé 'function'
function my_function () {
  return 'Hello';
}

echo my_function(); // => "Hello"


// Les noms de fonction débutent par le symbole $
// Un nom de variable valide commence par une lettre ou un souligné,
// suivi de n'importe quelle lettre, nombre ou de soulignés.

function add ($x, $y = 1) { // $y est facultatif et sa valeur par défaut est 1
  $result = $x + $y;
  return $result;
}

echo add(4); // => 5
echo add(4, 2); // => 6

// $result n'est pas accessible en dehors de la fonction
// print $result; // Retourne un avertissement

// Depuis PHP 5.3 on peut déclarer des fonctions anonymes
$inc = function ($x) {
  return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
  echo "$x - $y - $z";
}

// Une fonction peut retourner une fonction
function bar ($x, $y) {
  // On peut utiliser 'use' pour passer des variables externes
  return function ($z) use ($x, $y) {
    foo($x, $y, $z);
  };
}

$bar = bar('A', 'B');
$bar('C'); // Affiche "A - B - C"

// On peut exécuter une fonction par son nom en chaîne de caractères
$function_name = 'add';
echo $function_name(1, 2); // => 3
// Utile pour déterminer par programmation quelle fonction exécuter.

// On peut également utiliser
call_user_func(callable $callback [, $parameter [, ... ]]);

/********************************
 * Insertions
 */

<?php
// Le PHP se trouvant dans un fichier inclus doit
// également commencer par une balise PHP.

include 'my-file.php';
// Le code se trouvant dans my-file.php est maintenant disponible dans
// le contexte courant. Si le fichier ne peut pas être inclus
// (ex. non trouvé), un avertissement sera émit.

include_once 'my-file.php';
// Si le code dans my-file.php a déjà été inclus ailleur, il ne va pas
// être inclus de nouveau.

require 'my-file.php';
require_once 'my-file.php';
// Même comportement que include() mais déclenche une érreur fatale si le fichier ne peux pas être inclus.

// Contenu de my-include.php:
<?php

return 'Anything you like.';
// Fin de my-include.php

// include() et require() peuvent également retourner une valeur
$value = include('my-include.php');

// Les fichiers sont inclus depuis le chemin donné ou, si aucun chemin n'est donné,
// la configuration 'include_path'. Si le fichier n'est pas trouvé dans le 'include_path',
// include va finalement vérifier dans le répertoire courant avant d'échouer.

/********************************
 * Classes
 */

// Les classes sont définies avec le mot clé 'class'

class MyClass
{
    const MY_CONST      = 'value'; // Une constante

    static $staticVar   = 'static';

    // Variables statiques et leur visibilité
    public static $publicStaticVar = 'publicStatic';
    // Accessible à l'intérieur de la classe seulement
    private static $privateStaticVar = 'privateStatic';
    // Accessible à l'intérieur de la classe et des classes enfants
    protected static $protectedStaticVar = 'protectedStatic';

    // Les attributs doivent définir leur visibilité
    public $property = 'public';
    public $instanceProp;
    protected $prot = 'protected';
    private $priv   = 'private';

    // Déclaration d'un constructeur avec __construct
    public function __construct($instanceProp) {
        // Access instance variables with $this
        $this->instanceProp = $instanceProp;
    }

    // Les méthodes sont déclarés par des fonctions au sein de la classe
    public function myMethod()
    {
        print 'MyClass';
    }

    // le mot clé 'final' rend la function impossible à surcharger
    final function youCannotOverrideMe()
    {
    }

/*
 * Les attributs et méthodes statiques peuvent être accédés sans devoir
 * instancier la classe. Les attributs statiques ne sont pas accessibles depuis
 * une instance, même si les méthodes statiques le sont.
 */

    public static function myStaticMethod()
    {
        print 'I am static';
    }
}

// Les constantes d'une classe peuvent toujours être utilisé de façon statique
echo MyClass::MY_CONST;    // Outputs 'value';

echo MyClass::$staticVar;  // Retourne 'static';
MyClass::myStaticMethod(); // Retourne 'I am static';

// On peut instancier une classe en utilisant le mot clé 'new'
$my_class = new MyClass('An instance property');

// On peut accéder aux attributs/méthodes d'une instance avec ->
echo $my_class->property;     // => "public"
echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod();        // => "MyClass"


// On peut hériter d'une classe en utilisant 'extends'
class MyOtherClass extends MyClass
{
    function printProtectedProperty()
    {
        echo $this->prot;
    }

    // Surcharge d'une méthode
    function myMethod()
    {
        parent::myMethod();
        print ' > MyOtherClass';
    }
}

$my_other_class = new MyOtherClass('Instance prop');
$my_other_class->printProtectedProperty(); // => Retourne "protected"
$my_other_class->myMethod();               // Retourne "MyClass > MyOtherClass"

// On peut empêcher qu'une classe soit héritée
final class YouCannotExtendMe
{
}

// On peut utiliser des "méthodes magiques" pour se faire des accesseurs
class MyMapClass
{
    private $property;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MyMapClass();
echo $x->property; // Va utiliser la méthode __get()
$x->property = 'Something'; // Va utiliser la méthode __set()

// Les classes peuvent être abstraites (en utilisant le mot clé 'abstract'), ou
// elle peuvent implémenter une interface (en utilisant le mot clé 'implement').

// Une interface peut être déclarée avec le mot clé 'interface'

interface InterfaceOne
{
    public function doSomething();
}

interface InterfaceTwo
{
    public function doSomethingElse();
}

// Les interfaces peuvent hériter d'autres interfaces
interface InterfaceThree extends InterfaceTwo
{
    public function doAnotherContract();
}

abstract class MyAbstractClass implements InterfaceOne
{
    public $x = 'doSomething';
}

class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
{
    public function doSomething()
    {
        echo $x;
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


// Les classes peuvent implémenter plusieurs interfaces à la fois
class SomeOtherClass implements InterfaceOne, InterfaceTwo
{
    public function doSomething()
    {
        echo 'doSomething';
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}

/********************************
 * Espaces de noms (namespaces)
 */

// Cette section est séparée, car une déclaration d'espace de nom doit être
// la première chose que l'on retrouve dans un fichier PHP,
// imaginons que c'est le cas

<?php

// Par défaut, les classes existent dans l'espace de nom global et peuvent
// être appelé explicitement avec un antislash.

$cls = new \MyClass();



// On peut spécifier l'espace de nom d'un fichier comme cela
namespace My\Namespace;

class MyClass
{
}

// (depuis un autre fichier...)
$cls = new My\Namespace\MyClass;

// Ou depuis un autre espace de nom
namespace My\Other\Namespace;

use My\Namespace\MyClass;

$cls = new MyClass();

// On peut également utiliser un alias sur un espace de nom

namespace My\Other\Namespace;

use My\Namespace as SomeOtherNamespace;

$cls = new SomeOtherNamespace\MyClass();

*/

```

## Pour plus d'informations

Visitez la [documentation officielle](http://www.php.net/manual/fr).

Si vous êtes intéressé par les bonnes pratiques, visitez
[PHP The Right Way](http://www.phptherightway.com/) (anglais seulement).

Si vous êtes habitué à utiliser de bons gestionaires de dépendances, regardez
[Composer](http://getcomposer.org/).

Pour consulter les standards, visitez "the PHP Framework Interoperability Groups"
[PSR standards](https://github.com/php-fig/fig-standards).
---
language: python
filename: learnpython-fr.py
contributors:
  - ["Louie Dinh", "http://ldinh.ca"]
translators:
  - ["Sylvain Zyssman", "https://github.com/sylzys"]
  - ["Nami-Doc", "https://github.com/Nami-Doc"]
lang: fr-fr
---

Python a été créé par Guido Van Rossum au début des années 90. C'est maintenant un des langages de programmation les plus populaires.
Je suis tombé amoureux de Python de par la clarté de sa syntaxe. C'est pratiquement du pseudo-code exécutable.

Vos retours sont grandement appréciés. Vous pouvez me contacter sur Twitter [@louiedinh](http://twitter.com/louiedinh) ou par e-mail: louiedinh [at] [google's email service]

N.B. : Cet article s'applique spécifiquement à Python 2.7, mais devrait s'appliquer pour toute version Python 2.x. Python 2.7 est en fin de vie et ne sera plus maintenu à partir de 2020, il est donc recommandé d'apprendre Python avec Python 3. Pour Python 3.x, il existe un autre [tutoriel pour Python 3](http://learnxinyminutes.com/docs/fr-fr/python3-fr/).

```python
# Une ligne simple de commentaire commence par un dièse
""" Les lignes de commentaires multipes peuvent être écrites
    en utilisant 3 guillemets ("), et sont souvent utilisées
    pour les commentaires
"""

####################################################
## 1. Types Primaires et Opérateurs
####################################################

# Les nombres
3 #=> 3

# Les calculs produisent les résultats mathématiques escomptés
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# La division est un peu spéciale. C'est une division d'entiers, et Python arrondi le résultat par défaut automatiquement.
5 / 2 #=> 2

# Pour corriger ce problème, on utilise les float.
2.0     # Voici un float
11.0 / 4.0 #=> 2.75 ahhh... beaucoup mieux

# Forcer la priorité avec les parenthèses
(1 + 3) * 2 #=> 8

# Les valeurs booléenes sont de type primitif
True
False

# Pour la négation, on utilise "not"
not True #=> False
not False #=> True

# Pour l'égalité, ==
1 == 1 #=> True
2 == 1 #=> False

# L'inégalité est symbolisée par !=
1 != 1 #=> False
2 != 1 #=> True

# D'autres comparateurs
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# On peut enchaîner les comparateurs !
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Les chaînes de caractères sont créées avec " ou '
"C'est une chaîne."
'C\'est aussi une chaîne.'

# On peut aussi les "additioner" !
"Hello " + "world!" #=> "Hello world!"

# Une chaîne peut être traitée comme une liste de caractères
"C'est une chaîne"[0] #=> 'C'

# % peut être utilisé pour formatter des chaîne, comme ceci:
"%s can be %s" % ("strings", "interpolated")

# Une autre manière de formatter les chaînes de caractères est d'utiliser la méthode 'format'
# C'est la méthode à privilégier
"{0} peut être {1}".format("La chaîne", "formattée")
# On peut utiliser des mot-clés au lieu des chiffres.
"{name} veut manger des {food}".format(name="Bob", food="lasagnes")

# None est un objet
None #=> None

# Ne pas utiliser le symbole d'inégalité "==" pour comparer des objet à None
# Il faut utiliser "is"
"etc" is None #=> False
None is None  #=> True

# L'opérateur 'is' teste l'identité de l'objet.
# Ce n'est pas très utilisé avec les types primitifs, mais cela peut être très utile
# lorsque l'on utilise des objets.

# None, 0, et les chaînes de caractères vides valent False.
# Toutes les autres valeurs valent True
0 == False  #=> True
"" == False #=> True


####################################################
## 2. Variables et Collections
####################################################

# Afficher du texte, c'est facile
print "Je suis Python. Enchanté!"


# Il n'y a pas besoin de déclarer les variables avant de les assigner.
some_var = 5    # La convention veut que l'on utilise des minuscules_avec_underscores
some_var #=> 5

# Accéder à une variable non assignée lève une exception
# Voyez les structures de contrôle pour en apprendre plus sur la gestion des exceptions.
some_other_var  # Lève une exception

# 'if' peut être utilisé comme expression
"yahoo!" if 3 > 2 else 2 #=> "yahoo!"

# Listes
li = []
# On peut remplir liste dès l'instanciation
other_li = [4, 5, 6]

# On ajoute des éléments avec 'append'
li.append(1)    #li contient [1]
li.append(2)    #li contient [1, 2]
li.append(4)    #li contient [1, 2, 4]
li.append(3)    #li contient [1, 2, 4, 3]

# Et on les supprime avec 'pop'
li.pop()        #=> 3 et li contient [1, 2, 4]
# Remettons-le dans la liste
li.append(3)    # li contient [1, 2, 4, 3] de nouveau.

# On accède aux éléments d'une liste comme à ceux un tableau.
li[0] #=> 1
# Le dernier élément
li[-1] #=> 3

# Accèder aux indices hors limite lève une exception
li[4] # Lève un 'IndexError'

# On peut accèder à des rangs de valeurs avec la syntaxe "slice"
# (C'est un rang de type 'fermé/ouvert' pour les plus matheux)
li[1:3] #=> [2, 4]
# Sans spécifier de fin de rang, on "saute" le début de la liste
li[2:] #=> [4, 3]
# Sans spécifier de début de rang, on "saute" la fin de la liste
li[:3] #=> [1, 2, 4]

# Retirer un élément spécifique dee la liste avec "del"
del li[2] # li contient [1, 2, 3]

# On peut additionner des listes entre elles
li + other_li #=> [1, 2, 3, 4, 5, 6] - Note: li et other_li existent toujours à part entière

# Concaténer des listes avec "extend()"
li.extend(other_li) # li vaut maintenant [1, 2, 3, 4, 5, 6]

# Vérifier l'existence d'un élément dans une liste avec "in"
1 in li #=> True

# Récupérer la longueur avec "len()"
len(li) #=> 6


# Les "tuples" sont comme des listes, mais sont immuables.
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # Lève un 'TypeError'

# Mais vous pouvez faire tout ceci sur les tuples:
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Vous pouvez "dé-packager" les tuples (ou les listes) dans des variables
a, b, c = (1, 2, 3)     # a vaut maintenant 1, b vaut maintenant 2 and c vaut maintenant 3
# Sans parenthèses, un tuple est créé par défaut
d, e, f = 4, 5, 6
# Voyez maintenant comme il est facile d'inverser 2 valeurs
e, d = d, e     # d is now 5 and e is now 4


# Dictionnaires
empty_dict = {}
# Un dictionnaire pré-rempli
filled_dict = {"one": 1, "two": 2, "three": 3}

# Trouver des valeurs avec []
filled_dict["one"] #=> 1

# Récupérer toutes les clés sous forme de liste avec "keys()"
filled_dict.keys() #=> ["three", "two", "one"]
# Note - l'ordre des clés du dictionnaire n'est pas garanti.
# Vos résultats peuvent différer de ceux ci-dessus.

# Récupérer toutes les valeurs sous forme de liste avec "values()"
filled_dict.values() #=> [3, 2, 1]
# Note - Même remarque qu'au-dessus concernant l'ordre des valeurs.

# Vérifier l'existence d'une clé dans le dictionnaire avec "in"
"one" in filled_dict #=> True
1 in filled_dict #=> False

# Chercher une clé non existante lève une 'KeyError'
filled_dict["four"] # KeyError

# Utiliser la méthode "get()" pour éviter 'KeyError'
filled_dict.get("one") #=> 1
filled_dict.get("four") #=> None
# La méthode get() prend un argument par défaut quand la valeur est inexistante
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4

# La méthode "setdefault()" permet d'ajouter de manière sécuris une paire clé-valeur dans le dictionnnaire
filled_dict.setdefault("five", 5) #filled_dict["five"] vaut 5
filled_dict.setdefault("five", 6) #filled_dict["five"] is toujours 5


# Les sets stockent ... des sets
empty_set = set()
# On initialise un "set()" avec tout un tas de valeurs
some_set = set([1,2,2,3,4]) # some_set vaut maintenant set([1, 2, 3, 4])

# Depuis Python 2.7, {} peut être utilisé pour déclarer un 'set'
filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}

# Ajouter plus d'éléments au set
filled_set.add(5) # filled_set contient maintenant {1, 2, 3, 4, 5}

# Intersection de sets avec &
other_set = {3, 4, 5, 6}
filled_set & other_set #=> {3, 4, 5}

# Union de sets avec |
filled_set | other_set #=> {1, 2, 3, 4, 5, 6}

# Différence de sets avec -
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Vérifier l'existence d'une valeur dans un set avec "in"
2 in filled_set #=> True
10 in filled_set #=> False


####################################################
## 3. Structure de contrôle
####################################################

# Initialisons une variable
some_var = 5

# Voici une condition 'if'. L'indentation est significative en Python !
# Affiche "some_var est inférieur à 10"
if some_var > 10:
    print "some_var est supérieur à 10."
elif some_var < 10:    # La clause elif est optionnelle
    print "some_var iinférieur à 10."
else:           # La clause else également
    print "some_var vaut 10."


"""
Les boucles "for" permettent d'itérer sur les listes
Affiche:
    chien : mammifère
    chat : mammifère
    souris : mammifère
"""
for animal in ["chien", "chat", "souris"]:
    # On peut utiliser % pour l'interpolation des chaînes formattées
    print "%s : mammifère" % animal

"""
"range(number)" retourne une liste de nombres
de 0 au nombre donné
Affiche:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
Les boucles "while" boucle jusqu'à ce que leur condition ne soit plus vraie
Affiche:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Raccourci pour x = x + 1

# Gérer les exceptions avec un bloc try/except

# Fonctionne pour Python 2.6 et ultérieur:
try:
    # Utiliser "raise" pour lever une exception
    raise IndexError("This is an index error")
except IndexError as e:
    pass    # Pass ne prend pas d'arguments. Généralement, on gère l'erreur ici.


####################################################
## 4. Fonctions
####################################################

# Utiliser "def" pour créer une nouvelle fonction
def add(x, y):
    print "x vaut %s et y vaur %s" % (x, y)
    return x + y    # Renvoi de valeur avec 'return'

# Appeller une fonction avec des paramètres
add(5, 6) #=> Affichet "x is 5 et y vaut 6" et renvoie 11

# Une autre manière d'appeller une fonction, avec les arguments
add(y=6, x=5)   # Les arguments peuvent venir dans n'importe quel ordre.

# On peut définir une foncion qui prend un nombre variable de paramètres
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# On peut également définir une fonction qui prend un nombre
# variable d'arguments
def keyword_args(**kwargs):
    return kwargs

# Appelons-là et voyons ce qu'il se passe
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# On peut faire les deux à la fois si on le souhaite
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) affiche:
    (1, 2)
    {"a": 3, "b": 4}
"""

# En appellant les fonctions, on peut faire l'inverse des paramètres / arguments !
# Utiliser * pour développer les paramètres, et ** pour développer les arguments
params = (1, 2, 3, 4)
args = {"a": 3, "b": 4}
all_the_args(*args) # equivaut à foo(1, 2, 3, 4)
all_the_args(**kwargs) # equivaut à foo(a=3, b=4)
all_the_args(*args, **kwargs) # equivaut à foo(1, 2, 3, 4, a=3, b=4)

# Python a des fonctions de première classe
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# Mais également des fonctions anonymes
(lambda x: x > 2)(3) #=> True

# On trouve aussi des fonctions intégrées plus évoluées
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# On peut utiliser la syntaxe des liste pour construire les "maps" et les "filters"
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Classes
####################################################

# Une classe est un objet
class Human(object):

    # Un attribut de classe. Il est partagé par toutes les instances de cette classe.
    species = "H. sapiens"

    # Initialiseur basique
    def __init__(self, name):
        # Assigne le paramètre à l'attribut de l'instance de classe.
        self.name = name

    # Une méthode de l'instance. Toutes les méthodes prennent "self" comme 1er paramètre.
    def say(self, msg):
       return "%s: %s" % (self.name, msg)

    # Une méthode de classe est partagée par toutes les instances.
    # On les appelle avec le nom de la classe en premier paramètre
    @classmethod
    def get_species(cls):
        return cls.species

    # Une méthode statique est appellée sans référence à une classe ou à une instance 
    @staticmethod
    def grunt():
        return "*grunt*"


# Instancier une classe
i = Human(name="Ian")
print i.say("hi")     # Affiche "Ian: hi"

j = Human("Joel")
print j.say("hello")  #Affiche "Joel: hello"

# Appeller notre méthode de classe
i.get_species() #=> "H. sapiens"

# Changer les attributs partagés
Human.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# Appeller la méthode statique
Human.grunt() #=> "*grunt*"


####################################################
## 6. Modules
####################################################

# On peut importer des modules
import math
print math.sqrt(16) #=> 4

# Et récupérer des fonctions spécifiques d'un module
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# Récuperer toutes les fonctions d'un module
# Attention, ce n'est pas recommandé.
from math import *

# On peut raccourcir le nom d'un module
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Les modules Python sont juste des fichiers Python ordinaires.
# On peut écrire ses propres modules et les importer.
# Le nom du module doit être le même que le nom du fichier.

# On peut trouver quelle fonction et attributs déterminent un module
import math
dir(math)


```

## Prêt à aller plus loin?

### En ligne gratuitement

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)

### Format papier

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
translators:
    - ["Gnomino", "https://github.com/Gnomino"]
filename: learnpython3-fr.py
lang: fr-fr
---

Python a été créé par Guido Van Rossum au début des années 90. C'est maintenant un des 
langages les populaires. Je suis tombé amoureux de Python pour la clarté de sa syntaxe.
C'est tout simplement du pseudo-code exécutable.

L'auteur original apprécierait les retours (en anglais): vous pouvez le contacter sur Twitter à [@louiedinh](http://twitter.com/louiedinh) ou par mail à l'adresse louiedinh [at] [google's email service]

Note : Cet article s'applique spécifiquement à Python 3. Jettez un coup d'oeil [ici](http://learnxinyminutes.com/docs/fr-fr/python-fr/) pour apprendre le vieux Python 2.7

```python

# Un commentaire d'une ligne commence par un dièse

""" Les chaînes de caractères peuvent être écrites
    avec 3 guillemets doubles ("), et sont souvent
    utilisées comme des commentaires.
"""

####################################################
## 1. Types de données primaires et opérateurs
####################################################

# On a des nombres
3  # => 3

# Les calculs sont ce à quoi on s'attend
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20

# Sauf pour la division qui retourne un float (nombre à virgule flottante)
35 / 5  # => 7.0

# Résultats de divisions entières tronqués pour les nombres positifs et négatifs
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # works on floats too
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# Quand on utilise un float, le résultat est un float
3 * 2.0 # => 6.0

# Modulo (reste de la division)
7 % 3 # => 1

# Exponentiation (x**y, x élevé à la puissance y)
2**4 # => 16

# Forcer la priorité de calcul avec des parenthèses
(1 + 3) * 2  # => 8

# Les valeurs booléennes sont primitives
True
False

# Négation avec not
not True  # => False
not False  # => True

# Opérateurs booléens
# On note que "and" et "or" sont sensibles à la casse
True and False #=> False
False or True #=> True

# Utilisation des opérations booléennes avec des entiers :
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# On vérifie une égalité avec ==
1 == 1  # => True
2 == 1  # => False

# On vérifie une inégalité avec !=
1 != 1  # => False
2 != 1  # => True

# Autres opérateurs de comparaison
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# On peut enchaîner les comparaisons
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# (is vs. ==) is vérifie si deux variables pointent sur le même objet, mais == vérifie
# si les objets ont la même valeur.
a = [1, 2, 3, 4] # a pointe sur une nouvelle liste, [1, 2, 3, 4]
b = a # b pointe sur a
b is a # => True, a et b pointent sur le même objet
b == a # => True, les objets a et b sont égaux
b = [1, 2, 3, 4] # b pointe sur une nouvelle liste, [1, 2, 3, 4]
b is a # => False, a et b ne pointent pas sur le même objet
b == a # => True, les objets a et b ne pointent pas sur le même objet

# Les chaînes (ou strings) sont créées avec " ou '
"Ceci est une chaine"
'Ceci est une chaine aussi.'

# On peut additionner des chaînes aussi ! Mais essayez d'éviter de le faire.
"Hello " + "world!"  # => "Hello world!"
# On peut aussi le faire sans utiliser '+'
"Hello " "world!"  # => "Hello world!"

# On peut traîter une chaîne comme une liste de caractères
"This is a string"[0]  # => 'T'

# .format peut être utilisé pour formatter des chaînes, comme ceci:
"{} peuvent etre {}".format("Les chaînes", "interpolées")

# On peut aussi réutiliser le même argument pour gagner du temps.
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
#=> "Jack be nimble, Jack be quick, Jack jump over the candle stick"

# On peut aussi utiliser des mots clés pour éviter de devoir compter.
"{name} wants to eat {food}".format(name="Bob", food="lasagna") #=> "Bob wants to eat lasagna"

# Si votre code doit aussi être compatible avec Python 2.5 et moins,
# vous pouvez encore utiliser l'ancienne syntaxe :
"Les %s peuvent être %s avec la %s méthode" % ("chaînes", "interpolées", "vieille")


# None est un objet
None  # => None

# N'utilisez pas "==" pour comparer des objets à None
# Utilisez plutôt "is". Cela permet de vérifier l'égalité de l'identité des objets.
"etc" is None  # => False
None is None  # => True

# None, 0, and les strings/lists/dicts (chaînes/listes/dictionnaires) valent False lorsqu'ils sont convertis en booléens.
# Toutes les autres valeurs valent True
bool(0)  # => False
bool("")  # => False
bool([]) #=> False
bool({}) #=> False


####################################################
## 2. Variables et Collections
####################################################

# Python a une fonction print pour afficher du texte
print("I'm Python. Nice to meet you!")

# Par défaut, la fonction print affiche aussi une nouvelle ligne à la fin.
# Utilisez l'argument optionnel end pour changer ce caractère de fin.
print("Hello, World", end="!") # => Hello, World!

# Pas besoin de déclarer des variables avant de les définir.
# La convention est de nommer ses variables avec des minuscules_et_underscores
some_var = 5
some_var  # => 5

# Tenter d'accéder à une variable non définie lève une exception.
# Voir Structures de contrôle pour en apprendre plus sur le traitement des exceptions.
une_variable_inconnue  # Lève une NameError

# Les listes permettent de stocker des séquences
li = []
# On peut initialiser une liste pré-remplie
other_li = [4, 5, 6]

# On ajoute des objets à la fin d'une liste avec .append
li.append(1)    # li vaut maintenant [1]
li.append(2)    # li vaut maintenant [1, 2]
li.append(4)    # li vaut maintenant [1, 2, 4]
li.append(3)    # li vaut maintenant [1, 2, 4, 3]
# On enlève le dernier élément avec .pop
li.pop()        # => 3 et li vaut maintenant [1, 2, 4]
# Et on le remet
li.append(3)    # li vaut de nouveau [1, 2, 4, 3]

# Accès à un élément d'une liste :
li[0]  # => 1
# Accès au dernier élément :
li[-1]  # => 3

# Accéder à un élément en dehors des limites lève une IndexError
li[4]  # Lève une IndexError

# On peut accéder à une intervalle avec la syntaxe "slice"
# (c'est un rang du type "fermé/ouvert")
li[1:3]  # => [2, 4]
# Omettre les deux premiers éléments
li[2:]  # => [4, 3]
# Prendre les trois premiers
li[:3]  # => [1, 2, 4]
# Sélectionner un élément sur deux
li[::2]   # =>[1, 4]
# Avoir une copie de la liste à l'envers
li[::-1]   # => [3, 4, 2, 1]
# Pour des "slices" plus élaborées :
# li[debut:fin:pas]

# Faire une copie d'une profondeur de un avec les "slices"
li2 = li[:] # => li2 = [1, 2, 4, 3] mais (li2 is li) vaut False.

# Enlever des éléments arbitrairement d'une liste
del li[2]   # li is now [1, 2, 3]

# On peut additionner des listes
# Note: les valeurs de li et other_li ne sont pas modifiées.
li + other_li   # => [1, 2, 3, 4, 5, 6]

# Concaténer des listes avec "extend()"
li.extend(other_li)   # Now li is [1, 2, 3, 4, 5, 6]

# Vérifier la présence d'un objet dans une liste avec "in"
1 in li   # => True

# Examiner la longueur avec "len()"
len(li)   # => 6


# Les tuples sont comme des listes mais sont immuables.
tup = (1, 2, 3)
tup[0]   # => 1
tup[0] = 3  # Lève une TypeError

# Note : un tuple de taille un doit avoir une virgule après le dernier élément,
# mais ce n'est pas le cas des tuples d'autres tailles, même zéro.
type((1))  # => <class 'int'>
type((1,)) # => <class 'tuple'>
type(())   # => <class 'tuple'>

# On peut utiliser la plupart des opérations des listes sur des tuples.
len(tup)   # => 3
tup + (4, 5, 6)   # => (1, 2, 3, 4, 5, 6)
tup[:2]   # => (1, 2)
2 in tup   # => True

# Vous pouvez décomposer des tuples (ou des listes) dans des variables
a, b, c = (1, 2, 3)     # a vaut 1, b vaut 2 et c vaut 3
# Les tuples sont créés par défaut sans parenthèses
d, e, f = 4, 5, 6
# Voyez comme il est facile d'intervertir deux valeurs :
e, d = d, e     # d vaut maintenant 5 et e vaut maintenant 4


# Créer un dictionnaire :
empty_dict = {}
# Un dictionnaire pré-rempli :
filled_dict = {"one": 1, "two": 2, "three": 3}

# Note : les clés des dictionnaires doivent être de types immuables.
# Elles doivent être convertibles en une valeur constante pour une recherche rapide.
# Les types immuables incluent les ints, floats, strings et tuples.
invalid_dict = {[1,2,3]: "123"} # => Lève une TypeError: unhashable type: 'list'
valid_dict = {(1,2,3):[1,2,3]}  # Par contre, les valeurs peuvent être de tout type.

# On trouve une valeur avec []
filled_dict["one"]   # => 1

# On obtient toutes les clés sous forme d'un itérable avec "keys()" Il faut l'entourer
# de list() pour avoir une liste Note: l'ordre n'est pas garanti.
list(filled_dict.keys())   # => ["three", "two", "one"]


# On obtient toutes les valeurs sous forme d'un itérable avec "values()". 
# Là aussi, il faut utiliser list() pour avoir une liste.
# Note : l'ordre n'est toujours pas garanti.
list(filled_dict.values())   # => [3, 2, 1]


# On vérifie la présence d'une clé dans un dictionnaire avec "in"
"one" in filled_dict   # => True
1 in filled_dict   # => False

# L'accès à une clé non-existente lève une KeyError
filled_dict["four"]   # KeyError

# On utilise "get()" pour éviter la KeyError
filled_dict.get("one")   # => 1
filled_dict.get("four")   # => None
# La méthode get accepte une valeur de retour par défaut en cas de valeur non-existante.
filled_dict.get("one", 4)   # => 1
filled_dict.get("four", 4)   # => 4

# "setdefault()" insère une valeur dans un dictionnaire si la clé n'est pas présente.
filled_dict.setdefault("five", 5)  # filled_dict["five"] devient 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] est toujours 5

# Ajouter à un dictionnaire
filled_dict.update({"four":4}) #=> {"one": 1, "two": 2, "three": 3, "four": 4}
#filled_dict["four"] = 4  # une autre méthode

# Enlever des clés d'un dictionnaire avec del
del filled_dict["one"]  # Enlever la clé "one" de filled_dict.


# Les sets stockent des ensembles
empty_set = set()
# Initialiser un set avec des valeurs. Oui, ça ressemble aux dictionnaires, désolé.
some_set = {1, 1, 2, 2, 3, 4}   # some_set est maintenant {1, 2, 3, 4}

# Comme les clés d'un dictionnaire, les éléments d'un set doivent être immuables.
invalid_set = {[1], 1} # => Lève une TypeError: unhashable type: 'list'
valid_set = {(1,), 1}

# On peut changer un set :
filled_set = some_set

# Ajouter un objet au set :
filled_set.add(5)   # filled_set vaut maintenant {1, 2, 3, 4, 5}

# Chercher les intersections de deux sets avec &
other_set = {3, 4, 5, 6}
filled_set & other_set   # => {3, 4, 5}

# On fait l'union de sets avec |
filled_set | other_set   # => {1, 2, 3, 4, 5, 6}

# On fait la différence de deux sets avec -
{1, 2, 3, 4} - {2, 3, 5}   # => {1, 4}

# On vérifie la présence d'un objet dans un set avec in
2 in filled_set   # => True
10 in filled_set   # => False



####################################################
## 3. Structures de contrôle et Itérables
####################################################

# On crée juste une variable
some_var = 5

# Voici une condition "si". L'indentation est significative en Python!
# Affiche: "some_var is smaller than 10"
if some_var > 10:
    print("some_var is totally bigger than 10.")
elif some_var < 10:    # La clause elif ("sinon si") est optionelle
    print("some_var is smaller than 10.")
else:                  # La clause else ("sinon") l'est aussi.
    print("some_var is indeed 10.")


"""
Les boucles "for" itèrent sur une liste
Affiche:
    chien est un mammifère
    chat est un mammifère
    souris est un mammifère
"""
for animal in ["chien", "chat", "souris"]:
    # On peut utiliser format() pour interpoler des chaînes formattées
    print("{} est un mammifère".format(animal))

"""
"range(nombre)" retourne un itérable de nombres
de zéro au nombre donné
Affiche:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
"range(debut, fin)" retourne un itérable de nombre
de debut à fin.
Affiche:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print(i)

"""
"range(debut, fin, pas)" retourne un itérable de nombres
de début à fin en incrémentant de pas.
Si le pas n'est pas indiqué, la valeur par défaut est 1.
Affiche:
    4
    6
    8
"""
for i in range(4, 8, 2):
    print(i)
"""

Les boucles "while" bouclent jusqu'à ce que la condition devienne fausse.
Affiche:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Raccourci pour x = x + 1

# On gère les exceptions avec un bloc try/except
try:
    # On utilise "raise" pour lever une erreur
    raise IndexError("Ceci est une erreur d'index")
except IndexError as e:
    pass    # Pass signifie simplement "ne rien faire". Généralement, on gère l'erreur ici.
except (TypeError, NameError):
    pass    # Si besoin, on peut aussi gérer plusieurs erreurs en même temps.
else:   # Clause optionelle des blocs try/except. Doit être après tous les except.
    print("Tout va bien!")   # Uniquement si aucune exception n'est levée.
finally: #  Éxécuté dans toutes les circonstances.
    print("On nettoie les ressources ici")

# Au lieu de try/finally pour nettoyer les ressources, on peut utiliser with
with open("myfile.txt") as f:
    for line in f:
        print(line)

# Python offre une abstraction fondamentale : l'Iterable.
# Un itérable est un objet pouvant être traîté comme une séquence.
# L'objet retourné par la fonction range() est un itérable.

filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable) #=> range(1,10). C'est un objet qui implémente l'interface Iterable

# On peut boucler dessus
for i in our_iterable:
    print(i)    # Affiche one, two, three

# Cependant, on ne peut pas accéder aux éléments par leur adresse.
our_iterable[1]  # Lève une TypeError

# Un itérable est un objet qui sait créer un itérateur.
our_iterator = iter(our_iterable)

# Notre itérateur est un objet qui se rappelle de notre position quand on le traverse.
# On passe à l'élément suivant avec "next()".
next(our_iterator)  #=> "one"

# Il garde son état quand on itère.
next(our_iterator)  #=> "two"
next(our_iterator)  #=> "three"

# Après que l'itérateur a retourné toutes ses données, il lève une exception StopIterator
next(our_iterator) # Lève une StopIteration

# On peut mettre tous les éléments d'un itérateur dans une liste avec list()
list(filled_dict.keys())  #=> Returns ["one", "two", "three"]


####################################################
## 4. Fonctions
####################################################

# On utilise "def" pour créer des fonctions
def add(x, y):
    print("x est {} et y est {}".format(x, y))
    return x + y    # On retourne une valeur avec return

# Appel d'une fonction avec des paramètres :
add(5, 6)   # => affiche "x est 5 et y est 6" et retourne 11

# Une autre manière d'appeller une fonction : avec des arguments
add(y=6, x=5)   # Les arguments peuvent être dans n'importe quel ordre.

# Définir une fonction qui prend un nombre variable d'arguments
def varargs(*args):
    return args

varargs(1, 2, 3)   # => (1, 2, 3)

# On peut aussi définir une fonction qui prend un nombre variable de paramètres.
def keyword_args(**kwargs):
    return kwargs

# Appelons la pour voir ce qu'il se passe :
keyword_args(big="foot", loch="ness")   # => {"big": "foot", "loch": "ness"}


# On peut aussi faire les deux à la fois :
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) affiche:
    (1, 2)
    {"a": 3, "b": 4}
"""

# En appelant des fonctions, on peut aussi faire l'inverse :
# utiliser * pour étendre un tuple de paramètres 
# et ** pour étendre un dictionnaire d'arguments.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)   # équivalent à foo(1, 2, 3, 4)
all_the_args(**kwargs)   # équivalent à foo(a=3, b=4)
all_the_args(*args, **kwargs)   # équivalent à foo(1, 2, 3, 4, a=3, b=4)

# Retourne plusieurs valeurs (avec un tuple)
def swap(x, y):
    return y, x # Retourne plusieurs valeurs avec un tuple sans parenthèses.
                # (Note: on peut aussi utiliser des parenthèses)

x = 1
y = 2
x, y = swap(x, y) # => x = 2, y = 1
# (x, y) = swap(x,y) # Là aussi, rien ne nous empêche d'ajouter des parenthèses

# Portée des fonctions :
x = 5

def setX(num):
    # La variable locale x n'est pas la même que la variable globale x
    x = num # => 43
    print (x) # => 43

def setGlobalX(num):
    global x
    print (x) # => 5
    x = num # la variable globale x est maintenant 6
    print (x) # => 6

setX(43)
setGlobalX(6)


# Python a des fonctions de première classe
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)   # => 13

# Mais aussi des fonctions anonymes
(lambda x: x > 2)(3)   # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5

# TODO - Fix for iterables
# Il y a aussi des fonctions de base
map(add_10, [1, 2, 3])   # => [11, 12, 13]
map(max, [1, 2, 3], [4, 2, 1])   # => [4, 2, 3]

filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]

# On peut utiliser les compréhensions de listes pour de jolies maps et filtres.
# Une compréhension de liste stocke la sortie comme une liste qui peut elle même être une liste imbriquée.
[add_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]   # => [6, 7]

####################################################
## 5. Classes
####################################################


# On utilise l'opérateur "classe" pour définir une classe
class Human:

    # Un attribut de la classe. Il est partagé par toutes les instances de la classe.
    species = "H. sapiens"

    # L'initialiseur de base. Il est appelé quand la classe est instanciée.
    # Note : les doubles underscores au début et à la fin sont utilisés pour
    # les fonctions et attributs utilisés par Python mais contrôlés par l'utilisateur.
    # Les méthodes (ou objets ou attributs) comme: __init__, __str__,
    # __repr__ etc. sont appelés méthodes magiques.
    # Vous ne devriez pas inventer de noms de ce style.
    def __init__(self, name):
        # Assigner l'argument à l'attribut de l'instance
        self.name = name

    # Une méthode de l'instance. Toutes prennent "self" comme premier argument.
    def say(self, msg):
        return "{name}: {message}".format(name=self.name, message=msg)

    # Une méthode de classe est partagée avec entre les instances
    # Ils sont appelés avec la classe comme premier argument
    @classmethod
    def get_species(cls):
        return cls.species

    # Une méthode statique est appelée sans référence à une instance ni à une classe.
    @staticmethod
    def grunt():
        return "*grunt*"


# Instantier une classe
i = Human(name="Ian")
print(i.say("hi"))     # affiche "Ian: hi"

j = Human("Joel")
print(j.say("hello"))  # affiche "Joel: hello"

# Appeller notre méthode de classe
i.get_species()   # => "H. sapiens"

# Changer les attributs partagés
Human.species = "H. neanderthalensis"
i.get_species()   # => "H. neanderthalensis"
j.get_species()   # => "H. neanderthalensis"

# Appeller la méthode statique
Human.grunt()   # => "*grunt*"


####################################################
## 6. Modules
####################################################

# On peut importer des modules
import math
print(math.sqrt(16))  # => 4.0

# On peut importer des fonctions spécifiques d'un module
from math import ceil, floor
print(ceil(3.7))  # => 4.0
print(floor(3.7))   # => 3.0

# On peut importer toutes les fonctions d'un module
# Attention: ce n'est pas recommandé.
from math import *

# On peut raccourcir un nom de module
import math as m
math.sqrt(16) == m.sqrt(16)   # => True

# Les modules Python sont juste des fichiers Python.
# Vous pouvez écrire les vôtres et les importer. Le nom du module
# est le nom du fichier.

# On peut voir quels fonctions et objets un module définit
import math
dir(math)


####################################################
## 7. Avancé
####################################################

# Les générateurs aident à faire du code paresseux (lazy)
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# Un générateur crée des valeurs à la volée.
# Au lieu de générer et retourner toutes les valeurs en une fois, il en crée une à chaque
# itération.  Cela signifie que les valeurs supérieures à 15 ne seront pas traîtées par
# double_numbers.
# Note : range est un générateur aussi. 
# Créer une liste 1-900000000 prendrait beaucoup de temps
# On met un underscore à la fin d'un nom de variable normalement réservé par Python.
range_ = range(1, 900000000)
# Double tous les nombres jusqu'à ce qu'un nombre >=30 soit trouvé
for i in double_numbers(range_):
    print(i)
    if i >= 30:
        break


# Decorateurs
# Dans cet exemple, beg enveloppe say
# Beg appellera say. Si say_please vaut True le message retourné sera changé
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Please! I am poor :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Can you buy me a beer?"
    return msg, say_please


print(say())  # affiche Can you buy me a beer?
print(say(say_please=True))  # affiche Can you buy me a beer? Please! I am poor :(
```

## Prêt pour encore plus ?

### En ligne et gratuit (en anglais)

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)
* [Python Course](http://www.python-course.eu/index.php)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)

### Livres (en anglais)

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
---
language: R
contributors:
    - ["e99n09", "http://github.com/e99n09"]
    - ["isomorphismes", "http://twitter.com/isomorphisms"]
translators:
    - ["Anne-Catherine Dehier", "https://github.com/spellart"]
filename: learnr-fr.r
lang: fr-fr
---

R est un langage de programmation statistique. Il dispose de nombreuses
bibliothèques pour le téléchargement et le nettoyage d'ensembles de données,
l'exécution de procédures statistiques, et la réalisation de graphiques.
On peut également exécuter des commmandes `R` au sein d'un document LaTeX.


```r

# Les commentaires commencent avec des symboles numériques.

# Il n'est pas possible de faire des commentaires multilignes,
# mais on peut placer plusieurs commentaires les uns en dessous
# des autres comme ceci.

# Sur Mac, taper COMMAND-ENTER pour exécuter une ligne
# et sur Windows taper CTRL-ENTER



########################################################################
# Les choses que vous pouvez faire sans rien comprendre
# à la programmation
########################################################################

# Dans cette section, nous vous montrons quelques trucs cools que vous
# pouvez faire avec R sans rien comprendre à la programmation.
# Ne vous inquiétez pas si vous ne comprenez pas tout ce que le code fait.
# Profitez simplement !

data()          # parcours les ensembles de données préchargées
data(rivers)	# récupère ceci : "Lengths of Major North American Rivers"
ls()            # notez que "rivers" apparaît maintenant dans votre espace de travail
head(rivers)	# donne un aperçu des données
# 735 320 325 392 524 450

length(rivers)	# Combien de rivers ont été mesurées ?
# 141
summary(rivers) # Quelles sont les principales données statistiques ?
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#  135.0   310.0   425.0   591.2   680.0  3710.0

# Fait un diagramme à tiges et à feuilles (visualisation de données de
# types histogramme)
stem(rivers)


#  Le point décimal est de 2 chiffres à droite du |
#
#   0 | 4
#   2 | 011223334555566667778888899900001111223333344455555666688888999
#   4 | 111222333445566779001233344567
#   6 | 000112233578012234468
#   8 | 045790018
#  10 | 04507
#  12 | 1471
#  14 | 56
#  16 | 7
#  18 | 9
#  20 |
#  22 | 25
#  24 | 3
#  26 |
#  28 |
#  30 |
#  32 |
#  34 |
#  36 | 1

stem(log(rivers)) # Notez que les données ne sont ni normales
# ni lognormales !
# Prenez-ça, la courbe en cloche

#  Le point décimal est à 1 chiffre à gauche du |
#
#  48 | 1
#  50 |
#  52 | 15578
#  54 | 44571222466689
#  56 | 023334677000124455789
#  58 | 00122366666999933445777
#  60 | 122445567800133459
#  62 | 112666799035
#  64 | 00011334581257889
#  66 | 003683579
#  68 | 0019156
#  70 | 079357
#  72 | 89
#  74 | 84
#  76 | 56
#  78 | 4
#  80 |
#  82 | 2

# Fait un histogramme :
hist(rivers, col="#333333", border="white", breaks=25) # amusez-vous avec ces paramètres
hist(log(rivers), col="#333333", border="white", breaks=25) # vous ferez plus de tracés plus tard

# Ici d'autres données qui viennent préchargées. R en a des tonnes.
data(discoveries)
plot(discoveries, col="#333333", lwd=3, xlab="Year",
     main="Number of important discoveries per year")
plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year",
     main="Number of important discoveries per year")

# Plutôt que de laisser l'ordre par défaut (par année)
# Nous pourrions aussi trier pour voir ce qu'il y a de typique
sort(discoveries)
#  [1]  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2
# [26]  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3
# [51]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  4  4  4  4  4  4  4
# [76]  4  4  4  4  5  5  5  5  5  5  5  6  6  6  6  6  6  7  7  7  7  8  9 10 12

stem(discoveries, scale=2)
#
#  Le point décimale est à la |
#
#   0 | 000000000
#   1 | 000000000000
#   2 | 00000000000000000000000000
#   3 | 00000000000000000000
#   4 | 000000000000
#   5 | 0000000
#   6 | 000000
#   7 | 0000
#   8 | 0
#   9 | 0
#  10 | 0
#  11 |
#  12 | 0

max(discoveries)
# 12
summary(discoveries)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#    0.0     2.0     3.0     3.1     4.0    12.0

# Lance un dé plusieurs fois
round(runif(7, min=.5, max=6.5))
# 1 4 6 1 4 6 4
# Vos numéros diffèreront des miens à moins que nous mettions
# le même random.seed(31337)

# Dessine à partir d'une normale Gaussienne 9 fois
rnorm(9)
# [1]  0.07528471  1.03499859  1.34809556 -0.82356087  0.61638975 -1.88757271
# [7] -0.59975593  0.57629164  1.08455362



##############################################################
# les types de données et l'arithmétique de base
##############################################################

# Maintenant pour la partie orientée programmation du tutoriel.
# Dans cette section vous rencontrerez les types de données importants de R :
# les entiers, les numériques, les caractères, les logiques, et les facteurs.

# LES ENTIERS
# Les entiers de type long sont écrits avec L
5L # 5
class(5L) # "integer"
# (Essayez ?class pour plus d'informations sur la fonction class().)
# Avec R, chaque valeur seule, comme 5L, est considérée comme
# un vecteur de longueur 1
length(5L) # 1
# On peut avoir un vecteur d'entiers avec une longueur > 1 :
c(4L, 5L, 8L, 3L) # 4 5 8 3
length(c(4L, 5L, 8L, 3L)) # 4
class(c(4L, 5L, 8L, 3L)) # "integer"

# LES NUMÉRIQUES
# Un "numeric" est un nombre à virgule flottante d'une précision double
5 # 5
class(5) # "numeric"
# Encore une fois, tout dans R est un vecteur ;
# Vous pouvez faire un vecteur numérique avec plus d'un élément
c(3,3,3,2,2,1) # 3 3 3 2 2 1
# Vous pouvez aussi utiliser la notation scientifique
5e4 # 50000
6.02e23 # nombre d'Avogadro
1.6e-35 # longueur de Planck
# Vous pouvez également avoir des nombres infiniments grands ou petits
class(Inf)	# "numeric"
class(-Inf)	# "numeric"
# Vous pouvez utiliser "Inf", par exemple, dans integrate(dnorm, 3, Inf);
# Ça permet d'éviter de réaliser une table de la loi normale.

# ARITHMÉTIQUES DE BASE
# Vous pouvez faire de l'arithmétique avec des nombres
# Faire des opérations arithmétiques en mixant des entiers
# et des numériques
# donne un autre numérique
10L + 66L # 76      # un entier plus un entier donne un entier
53.2 - 4  # 49.2    # un numérique moins un numérique donne un numérique
2.0 * 2L  # 4       # un numérique multiplié par un entier donne un numérique
3L / 4    # 0.75    # un entier sur un numérique donne un numérique
3 %% 2    # 1       # le reste de deux numériques est un autre numérique
# Les opérations arithmétiques illégales donnent un "Not A Number" :
0 / 0 # NaN
class(NaN) # "numeric"
# Vous pouvez faire des opérations arithmétiques avec deux vecteurs d'une
# longueur plus grande que 1, à condition que la longueur du plus grand
# vecteur soit un multiple entier du plus petit
c(1,2,3) + c(1,2,3) # 2 4 6

# LES CARACTÈRES
# Il n'y a pas de différences entre les chaînes de caractères et
# les caractères en R
"Horatio" # "Horatio"
class("Horatio") # "character"
class('H') # "character"
# Ceux-ci sont tous les deux des vecteurs de longueur 1
# Ici un plus long :
c('alef', 'bet', 'gimmel', 'dalet', 'he')
# =>
# "alef"   "bet"    "gimmel" "dalet"  "he"
length(c("Call","me","Ishmael")) # 3
# Vous pouvez utiliser des expressions rationnelles sur les vecteurs de caractères :
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
# R possède plusieurs vecteurs de caractères préconstruits :
letters
# =>
#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [20] "t" "u" "v" "w" "x" "y" "z"
month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

# LES TYPES BOOLÉENS
# En R, un "logical" est un booléen
class(TRUE)	# "logical"
class(FALSE)	# "logical"
# Leur comportement est normal
TRUE == TRUE	# TRUE
TRUE == FALSE	# FALSE
FALSE != FALSE	# FALSE
FALSE != TRUE	# TRUE
# Les données manquantes (NA) sont logiques également
class(NA)	# "logical"
# On utilise | et & pour les operations logiques.
# OR
TRUE | FALSE	# TRUE
# AND
TRUE & FALSE	# FALSE
# Vous pouvez tester si x est TRUE
isTRUE(TRUE)	# TRUE
# Ici nous avons un vecteur de type logique avec plusieurs éléments :
c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE
c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE

# LES FACTEURS
# Les facteurs sont généralement utilisés pour y stocker des
# variables qualitatives (catégorielles).
# Les facteurs peuvent être ordonnés (comme le niveau scolaire
# des enfants) ou non ordonnés (comme le sexe)
factor(c("female", "female", "male", NA, "female"))
#  female female male   <NA>   female
# Les niveaux : female male
# Les facteurs possèdent un attribut appelé niveau ("level").
# Les niveaux sont des vecteurs contenant toutes les valeurs
# que peuvent prendre les données catégorielles.
# Notez que les données manquantes n'entrent pas dans le niveau
levels(factor(c("male", "male", "female", NA, "female"))) # "female" "male"
# Si le vecteur de facteurs a une longueur 1, ses niveaux seront
# de longueur 1 également
length(factor("male")) # 1
length(levels(factor("male"))) # 1
# On rencontre communément des facteurs dans des "data frame",
# un type de données que nous couvrirons plus tard
data(infert) # "Infertility after Spontaneous and Induced Abortion"
levels(infert$education) # "0-5yrs"  "6-11yrs" "12+ yrs"

# NULL
# "NULL" est bizarre ; on l'utilise pour effacer un vecteur
class(NULL)	# NULL
parakeet = c("beak", "feathers", "wings", "eyes")
parakeet
# =>
# [1] "beak"     "feathers" "wings"    "eyes"
parakeet <- NULL
parakeet
# =>
# NULL

# LES CONVERSIONS DE TYPES
# Les conversions de types servent à forcer une valeur à prendre
# un type différent
as.character(c(6, 8)) # "6" "8"
as.logical(c(1,0,1,1)) # TRUE FALSE  TRUE  TRUE
# Si vous mettez des éléments de différents types dans un vecteur,
# des coercitions bizarres se produisent :
c(TRUE, 4) # 1 4
c("dog", TRUE, 4) # "dog"  "TRUE" "4"
as.numeric("Bilbo")
# =>
# [1] NA
# Message d'avertissement :
# NAs est introduit par coercition

# Notez également : ce n'étaient que des types de données basiques
# Il y a beaucoup d'autres types de données, comme les dates,
# les séries temporelles, etc ...



#######################################
# Variables, boucles , if/else
#######################################

# Une variable est comme une boîte dans laquelle on garde une valeur
# pour l'utiliser plus tard.
# Nous appellons ça "assigner" une valeur à une variable.
# Avoir des variables nous permet d'écrire des boucles, des fonctions, et
# des instructions conditionnelles (if/else)

# LES VARIABLES
# Beaucoup de façons d'assigner des choses :
x = 5 # c'est correct
y <- "1" # c'est préféré
TRUE -> z # ça marche mais c'est bizarre

# LES BOUCLES
# Il y a les boucles for :
for (i in 1:4) {
  print(i)
}
# Il y a les boucles while :
a <- 10
while (a > 4) {
    cat(a, "...", sep = "")
    a <- a - 1
}
# Gardez à l'esprit que les boucles for et while s'exécutent lentement
# en R.
# Des opérations sur la totalité d'un vecteur (ex une ligne entière,
# une colonne entière),
# ou les fonctions de type apply() (nous en parlerons plus tard),
# sont préférées.

# IF/ELSE
# Encore une fois assez standard
if (4 > 3) {
    print("4 is greater than 3")
} else {
    print("4 is not greater than 3")
}
# =>
# [1] "4 is greater than 3"

# LES FONCTIONS
# se définissent comme ceci :
jiggle <- function(x) {
    x = x + rnorm(1, sd=.1)	# ajoute un peu de bruit (contrôlé)
    return(x)
}
# Appelées comme n'importe quelles autres fonction R :
jiggle(5)	# 5±ε. After set.seed(2716057), jiggle(5)==5.005043



##########################################################################
# Les structures de données : les vecteurs, les matrices,
# les data frames et les tableaux
##########################################################################

# À UNE DIMENSION

# Commençons par le tout début, et avec quelque chose que
# vous connaissez déjà : les vecteurs.
vec <- c(8, 9, 10, 11)
vec	#  8  9 10 11
# Nous demandons des éléments spécifiques en les mettant entre crochets
# (Notez que R commence à compter à partir de 1)
vec[1]		# 8
letters[18]	# "r"
LETTERS[13]	# "M"
month.name[9]	# "September"
c(6, 8, 7, 5, 3, 0, 9)[3]	# 7
# Nous pouvons également rechercher des indices de composants spécifiques,
which(vec %% 2 == 0)	# 1 3
# Récupèrer seulement les premières ou dernières entrées du vecteur,
head(vec, 1)	# 8
tail(vec, 2)	# 10 11
# ou vérifier si un certaine valeur est dans le vecteur
any(vec == 10) # TRUE
# Si un index "dépasse" vous obtiendrez NA :
vec[6]	# NA
# Vous pouvez trouver la longueur de votre vecteur avec length()
length(vec)	# 4
# Vous pouvez réaliser des opérations sur des vecteurs entiers ou des
# sous-ensembles de vecteurs
vec * 4	# 16 20 24 28
vec[2:3] * 5	# 25 30
any(vec[2:3] == 8) # FALSE
# Et R a beaucoup de méthodes statistiques pré-construites pour les vecteurs :
mean(vec)	# 9.5
var(vec)	# 1.666667
sd(vec)		# 1.290994
max(vec)	# 11
min(vec)	# 8
sum(vec)	# 38
# Quelques fonctions préconstruites sympas supplémentaires :
5:15	# 5  6  7  8  9 10 11 12 13 14 15
seq(from=0, to=31337, by=1337)
# =>
#  [1]     0  1337  2674  4011  5348  6685  8022  9359 10696 12033 13370 14707
# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751

# À DEUX DIMENSIONS (TOUT DANS UNE CLASSE)

# Vous pouvez créer une matrice à partir d'entrées du même type comme ceci :
mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# Différemment du vecteur, la classe d'une matrice est "matrix",
# peut importe ce qu'elle contient
class(mat) # => "matrix"
# Récupérer la première ligne
mat[1,]	# 1 4
# Réaliser une opération sur la première colonne
3 * mat[,1]	# 3 6 9
# Demander une cellule spécifique
mat[3,2]	# 6

# Transposer la matrice entière
t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6

# La multiplication de matrices
mat %*% t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]   17   22   27
# [2,]   22   29   36
# [3,]   27   36   45

# cbind() colle des vecteurs ensemble en colonne pour faire une matrice
mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
mat2
# =>
#      [,1] [,2]
# [1,] "1"  "dog"
# [2,] "2"  "cat"
# [3,] "3"  "bird"
# [4,] "4"  "dog"
class(mat2)	# matrix
# Encore une fois regardez ce qui se passe !
# Parce que les matrices peuvent contenir des entrées de toutes sortes de
# classes, tout sera converti en classe caractère
c(class(mat2[,1]), class(mat2[,2]))

# rbind() colle des vecteurs ensemble par lignes pour faire une matrice
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    4    5
# [2,]    6    7    0    4
# Ah, tout de la même classe. Pas de coercitions. Beaucoup mieux.

# À DEUX DIMENSIONS (DE CLASSES DIFFÉRENTES)

# Pour des colonnes de différents types, utiliser une data frame
# Cette structure de données est si utile pour la programmation statistique,
# qu'une version a été ajoutée à Python dans le paquet "pandas".

students <- data.frame(c("Cedric","Fred","George","Cho","Draco","Ginny"),
                       c(3,2,2,1,0,-1),
                       c("H", "G", "G", "R", "S", "G"))
names(students) <- c("name", "year", "house") # name the columns
class(students)	# "data.frame"
students
# =>
#     name year house
# 1 Cedric    3     H
# 2   Fred    2     G
# 3 George    2     G
# 4    Cho    1     R
# 5  Draco    0     S
# 6  Ginny   -1     G
class(students$year)	# "numeric"
class(students[,3])	# "factor"
# Trouver les dimensions
nrow(students)	# 6
ncol(students)	# 3
dim(students)	# 6 3
# La fonction data.frame() convertit les vecteurs caractères en vecteurs de
# facteurs par défaut; désactiver cette fonction en règlant
# stringsAsFactors = FALSE quand vous créer la data.frame
?data.frame

# Il y a plusieurs façons de subdiviser les data frames,
# toutes subtilement différentes
students$year	# 3  2  2  1  0 -1
students[,2]	# 3  2  2  1  0 -1
students[,"year"]	# 3  2  2  1  0 -1

# Une version améliorée de la structure data.frame est data.table.
# Si vous travaillez avec des données volumineuses ou des panels, ou avez
# besoin de fusionner quelques ensembles de données, data.table peut être
# un bon choix. Ici un tour éclair :
install.packages("data.table") # télécharge le paquet depuis CRAN
require(data.table) # le charge
students <- as.data.table(students)
students # regardez la différence à l'impression
# =>
#      name year house
# 1: Cedric    3     H
# 2:   Fred    2     G
# 3: George    2     G
# 4:    Cho    1     R
# 5:  Draco    0     S
# 6:  Ginny   -1     G
students[name=="Ginny"] # obtiens les lignes avec name == "Ginny"
# =>
#     name year house
# 1: Ginny   -1     G
students[year==2] # obtiens les lignes avec year == 2
# =>
#      name year house
# 1:   Fred    2     G
# 2: George    2     G
# data.table facilite la fusion entre deux ensembles de données
# Faisons une autre data.table pour fusionner students
founders <- data.table(house=c("G","H","R","S"),
                       founder=c("Godric","Helga","Rowena","Salazar"))
founders
# =>
#    house founder
# 1:     G  Godric
# 2:     H   Helga
# 3:     R  Rowena
# 4:     S Salazar
setkey(students, house)
setkey(founders, house)
students <- founders[students] # merge les deux ensembles de données qui matchent "house"
setnames(students, c("house","houseFounderName","studentName","year"))
students[,order(c("name","year","house","houseFounderName")), with=F]
# =>
#    studentName year house houseFounderName
# 1:        Fred    2     G           Godric
# 2:      George    2     G           Godric
# 3:       Ginny   -1     G           Godric
# 4:      Cedric    3     H            Helga
# 5:         Cho    1     R           Rowena
# 6:       Draco    0     S          Salazar

# data.table facilite le résumé des tableaux
students[,sum(year),by=house]
# =>
#    house V1
# 1:     G  3
# 2:     H  3
# 3:     R  1
# 4:     S  0

# Pour supprimer une colonne d'une data.frame ou data.table,
# assignez-lui la valeur NULL
students$houseFounderName <- NULL
students
# =>
#    studentName year house
# 1:        Fred    2     G
# 2:      George    2     G
# 3:       Ginny   -1     G
# 4:      Cedric    3     H
# 5:         Cho    1     R
# 6:       Draco    0     S

# Supprimer une ligne en subdivisant
# En utilisant data.table :
students[studentName != "Draco"]
# =>
#    house studentName year
# 1:     G        Fred    2
# 2:     G      George    2
# 3:     G       Ginny   -1
# 4:     H      Cedric    3
# 5:     R         Cho    1
# En utilisant data.frame :
students <- as.data.frame(students)
students[students$house != "G",]
# =>
#   house houseFounderName studentName year
# 4     H            Helga      Cedric    3
# 5     R           Rowena         Cho    1
# 6     S          Salazar       Draco    0

# MULTI-DIMENSIONNELLE (TOUS ÉLÉMENTS D'UN TYPE)

# Les arrays créent des tableaux de n dimensions.
# Tous les éléments doivent être du même type.
# Vous pouvez faire un tableau à 2 dimensions (une sorte de matrice)
array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4))
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    8    3
# [2,]    2    5    9    6
# Vous pouvez aussi utiliser array pour faire des matrices à 3 dimensions :
array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2))
# =>
# , , 1
#
#      [,1] [,2]
# [1,]    2    8
# [2,]  300    9
# [3,]    4    0
#
# , , 2
#
#      [,1] [,2]
# [1,]    5   66
# [2,]   60    7
# [3,]    0  847

# LES LISTES (MULTI-DIMENSIONNELLES, ÉVENTUELLEMMENT DÉCHIRÉES,
# DE DIFFÉRENTS TYPES)

# Enfin, R a des listes (de vecteurs)
list1 <- list(time = 1:40)
list1$price = c(rnorm(40,.5*list1$time,4)) # random
list1
# Vous pouvez obtenir des éléments de la liste comme ceci
list1$time # une façon
list1[["time"]] # une autre façon
list1[[1]] # encore une façon différente
# =>
#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# [34] 34 35 36 37 38 39 40
# Vous pouvez subdiviser les éléments d'une liste comme n'importe quel vecteur
list1$price[4]

# Les listes ne sont pas les structures de données les plus efficaces
# à utiliser avec R ;
# À moins d'avoir une très bonne raison, vous devriez utiliser data.frames
# Les listes sont souvent retournées par des fonctions qui effectuent
# des régressions linéaires.

##########################################
# La famille de fonction apply()
##########################################

# Vous vous rappelez mat ?
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# Utilisez apply(X, MARGIN, FUN) pour appliquer la fonction FUN à la matrice X
# sur les lignes (MAR = 1) ou les colonnes (MAR = 2)
# R exécute FUN à chaque lignes (ou colonnes) de X, beaucoup plus rapidement
# que le ferait une boucle for ou while
apply(mat, MAR = 2, jiggle)
# =>
#      [,1] [,2]
# [1,]    3   15
# [2,]    7   19
# [3,]   11   23
# D'autres fonctions : ?lapply, ?sapply

# Ne soyez pas trop intimidé ; tout le monde reconnaît que c'est un peu déroutant

# Le paquet plyr vise à remplacer (et améliorer !) la famille *apply().
install.packages("plyr")
require(plyr)
?plyr



############################
# Charger des données
############################

# "pets.csv" est un fichier sur internet
# (mais il pourrait être tout aussi facilement sur votre ordinateur)
pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv")
pets
head(pets, 2) # first two rows
tail(pets, 1) # last row

# Pour sauvegarder une data frame ou une matrice en fichier .csv
write.csv(pets, "pets2.csv") # to make a new .csv file
# définir le répertoire de travail avec setwd(), le récupérer avec getwd()

# Essayez ?read.csv et ?write.csv pour plus d'informations



################
# Les tracés
################

# LES FONCTIONS DE TRACÉ PRÉCONSTRUITES
# Les diagrammes de dispersion !
plot(list1$time, list1$price, main = "fake data")
# Les régressions !
linearModel <- lm(price  ~ time, data = list1)
linearModel # sort le résultat de la régression
# Tracer une ligne de regression sur une tracé existant
abline(linearModel, col = "red")
# Obtenir une variété de diagnostiques sympas
plot(linearModel)
# Les histogrammes !
hist(rpois(n = 10000, lambda = 5), col = "thistle")
# Les diagrammes en bâtons !
barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow"))

# GGPLOT2
# Mais ceux-ci ne sont même pas les plus jolis tracés de R
# Essayez le paquet ggplot2 pour d'avantages de graphiques
install.packages("ggplot2")
require(ggplot2)
?ggplot2
pp <- ggplot(students, aes(x=house))
pp + geom_histogram()
ll <- as.data.table(list1)
pp <- ggplot(ll, aes(x=time,price))
pp + geom_point()
# ggplot2 a une documentation excellente
#(disponible sur http://docs.ggplot2.org/current/)



```

## Comment obtenir R ?

* Obtiens R et R GUI depuis [http://www.r-project.org/](http://www.r-project.org/)
* [RStudio](http://www.rstudio.com/ide/) est un autre GUI
---
language: racket
filename: learnracket-fr.rkt
contributors:
  - ["th3rac25", "https://github.com/voila"]
  - ["Eli Barzilay", "https://github.com/elibarzilay"]
  - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
translators:
  - ["Xavier Nayrac", "https://github.com/lkdjiin"]
lang: fr-fr
---

Racket est un langage de programmation généraliste, multi-paradigme,
descendant de Lisp/Scheme.

Les retours et commentaires sont appréciés ! Vous pouvez joindre l'auteur
original ici :
[@th3rac25](http://twitter.com/th3rac25) ou là : th3rac25 [at] [google's email
service]. Vous pouvez joindre le traducteur de ce document ici :
[@lkdjiin](http://twitter.com/lkdjiin).

```racket
#lang racket ; défini le dialecte à utiliser.

;;; Commentaires

;; Une ligne de commentaire commence par un point-virgule.

#| Un bloc de commentaires
   peut tenir sur plusieurs lignes…
    #|
       et on peut les imbriquer !
    |#
|#

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Types de données et opérateurs primitifs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Nombres
9999999999999999999999 ; entier
#b111                  ; binaire => 7
#o111                  ; octal => 73
#x111                  ; hexadécimal => 273
3.14                   ; réel
6.02e+23
1/2                    ; rationnel
1+2i                   ; complexe

;; Un appel de fonction s'écrit (f x y z ...)
;; où f est une fonction et x, y, z, ... sont des arguments.
;; Si vous voulez créer une liste littérales, utilisez ' pour
;; empécher l'évaluation de la liste.
'(+ 1 2) ; => (+ 1 2)
;; Et maintenant, un peu d'arithmétique
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(expt 2 3) ; => 8
(quotient 5 2) ; => 2
(remainder 5 2) ; => 1
(/ 35 5) ; => 7
(/ 1 3) ; => 1/3
(exact->inexact 1/3) ; => 0.3333333333333333
(+ 1+2i  2-3i) ; => 3-1i

;;; Booléens
#t ; pour vrai
#f ; pour faux -- Toute autre valeur que #f est vraie
(not #t) ; => #f
(and 0 #f (error "doesn't get here")) ; => #f
(or #f 0 (error "doesn't get here"))  ; => 0

;;; Caractères
#\A ; => #\A
#\λ ; => #\λ
#\u03BB ; => #\λ

;;; Une chaîne de caractères est un tableau de caractères de longueur
;;; fixe.
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; Le backslash est le caractère d'échappement
"Foo\tbar\41\x21\u0021\a\r\n" ; Sont inclus les échappements de type C
                              ; et unicode
"λx:(μα.α→α).xx"              ; une chaîne peut inclure de l'unicode

;; On peut ajouter une chaîne à une autre
(string-append "Hello " "world!") ; => "Hello world!"

;; Une chaîne peut être traitée comme une liste de caractères
(string-ref "Apple" 0) ; => #\A

;; format est utilisé pour formatter une chaîne
(format "~a can be ~a" "strings" "formatted")

;; L'affichage est tout simple
(printf "I'm Racket. Nice to meet you!\n")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Vous pouvez créer une variable à l'aide de define
;; Une variable peut contenir n'importe quel caractères, à l'exception
;; de : ()[]{}",'`;#|\
(define some-var 5)
some-var ; => 5

;; Vous pouvez aussi utiliser des caractères unicode
(define ⊆ subset?)
(⊆ (set 3 2) (set 1 2 3)) ; => #t

;; Accéder à une variable non-initialisée provoque une exception
; x ; => x: indéfini ...

;; Déclaration locale : `me` est attaché à "Bob" seulement à l'intérieur
;; de (let ...)
(let ([me "Bob"])
  "Alice"
  me) ; => "Bob"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Structures and Collections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Structures
(struct dog (name breed age))
(define my-pet
  (dog "lassie" "collie" 5))
my-pet ; => #<dog>
(dog? my-pet) ; => #t
(dog-name my-pet) ; => "lassie"

;;; Paires (non mutable)
;; `cons` construit une paire, `car` et `cdr` extraient respectivement le
;; premier et le second élément.
(cons 1 2) ; => '(1 . 2)
(car (cons 1 2)) ; => 1
(cdr (cons 1 2)) ; => 2

;;; Listes

;; Les listes en Racket sont des structures de données de type *linked-list*,
;; produites avec des paires assemblées avec `cons` et terminée par `null`
;; (ou '()).
(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3)
;; `list` est un constructeur variadique plus commode à utiliser
(list 1 2 3) ; => '(1 2 3)
;; et un guillemet simple peut aussi être utilisé pour une liste littérale
'(1 2 3) ; => '(1 2 3)

;; On peut toujours utiliser `cons` pour ajouter un élément au début
;; d'une liste
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; Utilisez `append` pour ajouter une liste à une autre
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; Une liste est un type très basique, il y a donc *beaucoup* de
;; fonctionnalités qui leur sont dédiées, quelques exemples :
(map add1 '(1 2 3))          ; => '(2 3 4)
(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33)
(filter even? '(1 2 3 4))    ; => '(2 4)
(count even? '(1 2 3 4))     ; => 2
(take '(1 2 3 4) 2)          ; => '(1 2)
(drop '(1 2 3 4) 2)          ; => '(3 4)

;;; Vecteurs

;; Un vecteur est un tableau de taille fixe
#(1 2 3) ; => '#(1 2 3)

;; Utilisez `vector-append` pour additionner des vecteurs entre eux
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; Sets

;; Créez un set à partir d'une liste
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)

;; Ajoutez un membre avec `set-add`
;; (Fonctionnel: renvoit le set étendu, plutôt que de muter le set en entrée)
(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)

;; Retirez un membre avec `set-remove`
(set-remove (set 1 2 3) 1) ; => (set 2 3)

;; Testez l'existence d'un membre avec `set-member?`
(set-member? (set 1 2 3) 1) ; => #t
(set-member? (set 1 2 3) 4) ; => #f

;;; Tables de hashage

;; Créer une table de hashage non-mutable (un exemple mutable plus loin)
(define m (hash 'a 1 'b 2 'c 3))

;; Retrouver une valeur
(hash-ref m 'a) ; => 1

;; Chercher une valeur inexistante provoque une exceptions
; (hash-ref m 'd) => no value found

;; Vous pouvez fournir une valeur par défaut pour les clés manquantes
(hash-ref m 'd 0) ; => 0

;; Utilisez `hash-set` pour étendre une table de hashage non-mutable
;; (Renvoit la table étendu, plutôt que de la muter)
(define m2 (hash-set m 'd 4))
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))

;; Rappelez-vous, ces tables de hashage sont non-mutables !
m ; => '#hash((b . 2) (a . 1) (c . 3))  <-- no `d'

;; Utilisez `hash-remove` pour supprimer des clés (également fonctionnel)
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Fonctions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Utilisez `lambda` pour créer des fonctions.
;; Une fonction renvoie toujours la valeur de sa dernière expression.
(lambda () "Hello World") ; => #<procedure>
;; On peut aussi utiliser le caractère unicode `λ'
(λ () "Hello World")     ; => même fonction

;; Utilisez des parenthèses pour appeler toutes les fonctions, ce qui
;; inclus aussi les expressions lambda
((lambda () "Hello World")) ; => "Hello World"
((λ () "Hello World"))      ; => "Hello World"

;; Assignez une fonction à une variable
(define hello-world (lambda () "Hello World"))
(hello-world) ; => "Hello World"

;; Vous pouvez raccourcir ceci en utilisant le sucre syntaxique pour la
;; définition de fonction :
(define (hello-world2) "Hello World")

;; Entre les () après lambda, vous déclarez la liste des arguments de la
;; fonction
(define hello
  (lambda (name)
    (string-append "Hello " name)))
(hello "Steve") ; => "Hello Steve"
;; … ou alors, en utilisant le sucre syntaxique, ce qui suit est équivalent
(define (hello2 name)
  (string-append "Hello " name))

;; Vous pouvez obtenir des fonctions variadique en utilisant `case-lambda`
(define hello3
  (case-lambda
    [() "Hello World"]
    [(name) (string-append "Hello " name)]))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"
;; … ou spécifier des arguments optionnels avec une valeur par défaut
(define (hello4 [name "World"])
  (string-append "Hello " name))

;; Les fonctions peuvent rassembler des arguments supplémentaires dans une
;; liste
(define (count-args . args)
  (format "You passed ~a args: ~a" (length args) args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
;; … ou bien avec `lambda`, sans sucre syntaxique
(define count-args2
  (lambda args
    (format "You passed ~a args: ~a" (length args) args)))

;; Vous pouvez mixer arguments réguliers et supplémentaires
(define (hello-count name . args)
  (format "Hello ~a, you passed ~a extra args" name (length args)))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"
;; … sans sucre syntaxique
(define hello-count2
  (lambda (name . args)
    (format "Hello ~a, you passed ~a extra args" name (length args))))

;; Avec des mot-clés cette fois
(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
  (format "~a ~a, ~a extra args" g name (length args)))
(hello-k)                 ; => "Hello World, 0 extra args"
(hello-k 1 2 3)           ; => "Hello World, 3 extra args"
(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
                                         ; => "Hi Finn, 6 extra args"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Égalité
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Pour les nombres, utilisez `=`
(= 3 3.0) ; => #t
(= 2 1) ; => #f

;; Pour tester l'identité des objets, utilisez `eq?`
(eq? 3 3) ; => #t
(eq? 3 3.0) ; => #f
(eq? (list 3) (list 3)) ; => #f

;; Pour les collections, utilisez `equal?`
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
(equal? (list 'a 'b) (list 'b 'a)) ; => #f

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Structures de contrôle
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Conditions

(if #t               ; expression pour le test
    "this is true"   ; expression si vrai
    "this is false") ; expression si faux
; => "this is true"

;; Dans les condition, toutes les valeurs non-fausses sont traitées commentaires
;; étant vraies (c'est à dire toutes sauf #f)
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'yep

;; `cond` permet d'enchaîner une série de tests afin d'obtenir un résultat
(cond [(> 2 2) (error "wrong!")]
      [(< 2 2) (error "wrong again!")]
      [else 'ok]) ; => 'ok

;;; Filtrage par motif (*pattern matching*)

(define (fizzbuzz? n)
  (match (list (remainder n 3) (remainder n 5))
    [(list 0 0) 'fizzbuzz]
    [(list 0 _) 'fizz]
    [(list _ 0) 'buzz]
    [_          #f]))

(fizzbuzz? 15) ; => 'fizzbuzz
(fizzbuzz? 37) ; => #f

;;; Les boucles

;; On peut boucler en utilisant la récursion (terminale)
(define (loop i)
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i))))
(loop 5) ; => i=5, i=6, ...

;; D'une manière similaire, avec un `let` nommé
(let loop ((i 0))
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i)))) ; => i=0, i=1, ...

;; Voir plus loin pour l'ajout d'une nouvelle forme `loop`, mais Racket
;; possède déjà une forme `for` flexible et élaborée pour les itérations
(for ([i 10])
  (printf "i=~a\n" i)) ; => i=0, i=1, ...
(for ([i (in-range 5 10)])
  (printf "i=~a\n" i)) ; => i=5, i=6, ...

;;; Itérer sur autre chose que des nombres
;; `for` permet d'itérer sur plein de type de séquences:
;; listes, vecteurs, chaînes de caractères, sets, tables de hashage, etc

(for ([i (in-list '(l i s t))])
  (displayln i))

(for ([i (in-vector #(v e c t o r))])
  (displayln i))

(for ([i (in-string "string")])
  (displayln i))

(for ([i (in-set (set 'x 'y 'z))])
  (displayln i))

(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
  (printf "key:~a value:~a\n" k v))

;;; Itérations plus complexes

;; Balayage parallèle de plusieurs séquences (on stoppe sur la plus petite)
(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x 1:y 2:z

;; Boucles imbriquées
(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z

;; Conditions dans les boucles
(for ([i 1000]
      #:when (> i 5)
      #:unless (odd? i)
      #:break (> i 10))
  (printf "i=~a\n" i))
; => i=6, i=8, i=10

;;; Compréhensions de liste
;; Très similaires aux boucles `for` -- renvoient en plus une collection

(for/list ([i '(1 2 3)])
  (add1 i)) ; => '(2 3 4)

(for/list ([i '(1 2 3)] #:when (even? i))
  i) ; => '(2)

(for/list ([i 10] [j '(x y z)])
  (list i j)) ; => '((0 x) (1 y) (2 z))

(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10))
  i) ; => '(6 8 10)

(for/hash ([i '(1 2 3)])
  (values i (number->string i)))
; => '#hash((1 . "1") (2 . "2") (3 . "3"))

;; Il y a plein d'autres fonctions natives pour collecter des données à
;; l'aide de boucles
(for/sum ([i 10]) (* i i)) ; => 285
(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000
(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t
(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t
;; Et pour n'importe quell combinaison arbitraire, utilisez `for/fold`
(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10
;; (Ceci peut souvent remplacer des boucles communes de style impératif)

;;; Exceptions

;; Pour capturer une exception, utilisez la forme `with-handlers`
(with-handlers ([exn:fail? (lambda (exn) 999)])
  (+ 1 "2")) ; => 999
(with-handlers ([exn:break? (lambda (exn) "no time")])
  (sleep 3)
  "phew") ; => "phew", but if you break it => "no time"

;; Utilisez `raise` pour soulever une exception, ou encore n'importe quelle
;; autre valeur
(with-handlers ([number?    ; capturer la valeur numérique soulevée
                 identity]) ; la renvoyer en tant que valeur simple
  (+ 1 (raise 2))) ; => 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Mutabilité
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Utilisez `set!` pour réassigner une valeur à une variable existante
(define n 5)
(set! n (add1 n))
n ; => 6

;; Utilisez le mécanisme des boites (*box*) pour les valeurs explicitement
;; mutables (similaire aux pointeurs ou références dans d'autres langages)
(define n* (box 5))
(set-box! n* (add1 (unbox n*)))
(unbox n*) ; => 6

;; Beaucoup de types de données en Racket sont non-mutables (paires, listes,
;; etc), certains ont à la fois une version mutable et une version
;; non-mutable (chaînes, vecteurs, tables de hashage, etc)

;; Utilisez `vector` ou `make-vector` pour créer des vecteurs mutables
(define vec (vector 2 2 3 4))
(define wall (make-vector 100 'bottle-of-beer))
;; Utilisez `vector-set!` pour mettre à jour un emplacement
(vector-set! vec 0 1)
(vector-set! wall 99 'down)
vec ; => #(1 2 3 4)

;; Créer une table de hashage mutable vide et la manipuler
(define m3 (make-hash))
(hash-set! m3 'a 1)
(hash-set! m3 'b 2)
(hash-set! m3 'c 3)
(hash-ref m3 'a)   ; => 1
(hash-ref m3 'd 0) ; => 0
(hash-remove! m3 'a)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Modules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Les modules permettent d'organiser le code en plusieurs fichiers
;; et bibliothèques réutilisables. Ici, nous utiliserons des sous-modules,
;; imbriqués dans le grand module que forme ce texte (qui démarre à la
;; ligne `#lang`).

(module cake racket/base ; défini un module `cake', basé sur racket/base

  (provide print-cake) ; fonction exportée par le module (publique)

  (define (print-cake n)
    (show "   ~a   " n #\.)
    (show " .-~a-. " n #\|)
    (show " | ~a | " n #\space)
    (show "---~a---" n #\-))

  (define (show fmt n ch) ; fonction interne/privée
    (printf fmt (make-string n ch))
    (newline)))

;; Utilisez `require` pour importer les fonctions fournies par un
;; module (provide)
(require 'cake) ; le ' est pour un sous-module local
(print-cake 3)
; (show "~a" 1 #\A) ; => erreur, `show` n'est pas exportée

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. Classes et objets
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Créer une classe fish% (% est idiomatique pour les noms de classes)
(define fish%
  (class object%
    (init size) ; argument pour l'initialisation
    (super-new) ; initialisation de la super-classe
    ;; Les champs/membres/variables de classe
    (define current-size size)
    ;; Méthodes publiques
    (define/public (get-size)
      current-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

;; Créer une instance de fish%
(define charlie
  (new fish% [size 10]))

;; Utilisez `send` pour appeler une méthode d'un objet
(send charlie get-size) ; => 10
(send charlie grow 6)
(send charlie get-size) ; => 16

;; `fish%` est une simple valeur de «première classe», ce qui va permettre
;; la composition (*mixins*)
(define (add-color c%)
  (class c%
    (init color)
    (super-new)
    (define my-color color)
    (define/public (get-color) my-color)))
(define colored-fish% (add-color fish%))
(define charlie2 (new colored-fish% [size 10] [color 'red]))
(send charlie2 get-color)
;; ou, sans les noms:
(send (new (add-color fish%) [size 10] [color 'red]) get-color)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 9. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Les macros permettent d'étendre la syntaxe du langage

;; Ajoutons une boucle `loop`
(define-syntax-rule (while condition body ...)
  (let loop ()
    (when condition
      body ...
      (loop))))

(let ([i 0])
  (while (< i  10)
    (displayln i)
    (set! i (add1 i))))

;; Les macros sont hygiéniques, vous ne pouvez pas *clasher* avec les
;; variables existantes !
(define-syntax-rule (swap! x y) ; ! est idiomatique pour la mutation
  (let ([tmp x])
    (set! x y)
    (set! y tmp)))

(define tmp 2)
(define other 3)
(swap! tmp other)
(printf "tmp = ~a; other = ~a\n" tmp other)
;; La variable `tmp` est renommée en `tmp_1`
;; dans le but d'éviter un conflit de nom
;; (let ([tmp_1 tmp])
;;   (set! tmp other)
;;   (set! other tmp_1))

;; Mais il faut quand même faire bien attention avec les macros, par exemple:
(define-syntax-rule (bad-while condition body ...)
  (when condition
    body ...
    (bad-while condition body ...)))
;; cette macro est cassée : ell génère un code infini, si vous l'essayez
;; le compilateur va entrer dans une boucle infinie.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 10. Contrats
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Les contrats imposent des contraintes aux valeurs exportées depuis
;; les modules

(module bank-account racket
  (provide (contract-out
            [deposit (-> positive? any)] ; un dépot est toujours positif
            [balance (-> positive?)]))

  (define amount 0)
  (define (deposit a) (set! amount (+ amount a)))
  (define (balance) amount)
  )

(require 'bank-account)
(deposit 5)

(balance) ; => 5

;; Les clients qui essaient de déposer un montant non-positif sont blamés
;; (deposit -5) ; => deposit: contract violation
;; expected: positive?
;; given: -5
;; more details....
```

## Pour aller plus loin

Vous en voulez plus ? Essayez
[Getting Started with Racket](http://docs.racket-lang.org/getting-started/)
---
category: tool
tool: ruby ecosystem
contributors:
    - ["Jon Smock", "http://github.com/jonsmock"]
    - ["Rafal Chmiel", "http://github.com/rafalchmiel"]
translators:
    - ["Xuan-thi Nguyen", "http://github.com/mellenguyen"]
    - ["Sylvain Abélard", "http://github.com/abelards"]
lang: fr-fr

---

Les gens utilisant Ruby adoptent généralement un gestionnaire pour installer
différentes versions de Ruby, gérer leurs paquets (ou gems), et gérer les
dépendances des gems.

## Ruby Managers

Quelques plateformes possèdent Ruby pré-installé ou disponible en tant que
paquet. La plupart des rubyistes ne les utilisent pas, ou si c'est le cas, ne
les utilisent que pour faire démarrer un autre installateur ou implémentation de
Ruby. Les rubyistes tendent plutôt à installer un gestionnaire en Ruby pour installer
et changer entre les différentes et nombreuses versions de Ruby et les
environnements de leurs projets Ruby.

Les gestionnaires d'environnement Ruby les plus populaires sont :

* [RVM](https://rvm.io/) - Installe et navigue entre les rubies. RVM possède
  églement le concept des gemsets pour isoler les environnements de projets
  complètement.
* [ruby-build](https://github.com/sstephenson/ruby-build) - Installe seulement
  les rubies. Utilisez-le pour un contrôle plus fin des installations des
  rubies.
* [rbenv](https://github.com/sstephenson/rbenv) - Navigue seulement entre les
  rubies. Utilisé avec ruby-build. Utilisez-le pour un contrôle plus fin des
  chargements des rubies.
* [chruby](https://github.com/postmodern/chruby) - Navigue seulement entre les
  rubies. Similaire à rbenv. Neutre sur comment les rubies sont installés.

## Versions de Ruby

Ruby a été créé par Yukihiro "Matz" Matsumoto, qui reste quelque peu un
[BDFL](https://fr.wikipedia.org/wiki/Benevolent_Dictator_for_Life), bien que
cela soit récemment en changement. Jusqu'à la standardisation du langage en
2011, l'implémentation de référence de Ruby était appelé MRI (Matz' Reference
Implementation).

Les trois versions majeures de Ruby actuellement utilisées sont :

* 2.0.0 - Sortie en février 2013. La plupart des bibliothèques et frameworks
  gèrent la versions 2.0.0.
* 1.9.3 - Sortie en octobre 2011. Il s'agit de la version que la majorité des
  rubyists utilisent actuellement. [Fin de vie](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/)
* 1.8.7 - Sortie en juin 2006. [Fin de vie](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/).

Les changements entre 1.8.7 et 1.9.x sont bien plus grands qu'entre 1.9.3
et 2.0.0. Par exemple, les versions 1.9 ont introduit le support des
encodages et d'une VM bytecode ([YARV](https://fr.wikipedia.org/wiki/YARV)).
Il y a toujours des projets sur 1.8.7, mais ils deviennent minoritaires, étant
donné que la majorité de la communauté a migré vers au moins 1.9.2 ou 1.9.3.

## Implémentations Ruby

L'écosystème Ruby comprend de nombreuses implémentations de Ruby, chacune avec
des points forts uniques et différents degrés de compatibilité. Les différentes
implémentations sont écrites dans différents languages.
Chaque implémentation a des "hooks" et des fonctionnalités spécifiques, elles
exécutent cependant très bien des fichiers Ruby classiques.
Par exemple, JRuby est écrit en Java, mais vous n'avez pas besoin de connaître
le Java pour l'utiliser.

Très mature/compatible:

* [MRI](https://github.com/ruby/ruby) - Ecrite en C, c'est l'implémentation de
  référence de Ruby. Elle est par définition 100% compatible (avec elle-même).
  Tous les autres rubies maintiennent la compatibilité avec MRI
  (voir [RubySpec](#rubyspec) à la suite).
* [JRuby](http://jruby.org/) - Écrite en Java et Ruby, cette robuste
  implémentation est assez rapide.
  La force de JRuby réside surtout sur l'interopérabilité JVM/Java, faisant
  levier sur des outils JVM, des projets et des langages existants.
* [Rubinius](http://rubini.us/) - Ecrite principalement en Ruby avec une VM
  bytecode en C++. Egalement mature et rapide. Etant donné qu'elle est
  implémentée en Ruby, elle couvre beaucoup de fonctionnalités de la
  VM dans Ruby.

Mpyennement mature/compatible:

* [Maglev](http://maglev.github.io/) - Basée sur Gemstone, une VM Smalltalk.
  Smalltalk possède quelques outils impressionnants, et ce projet tente
  de les apporter dans le développement Ruby.
* [RubyMotion](http://www.rubymotion.com/) - Ruby pour développement iOS et Android.
* [Opal](http://opalrb.org/) - Compile le Ruby en Javascript

Les implémentations de Ruby peuvent avoir leurs propres numéros de versions,
mais elles ciblent toujours une versions spéficique de MRI pour la
compatibilité.
Beaucoup d'implémentations ont la capacité d'entrer dans différents modes
(par exemple, la version 1.8 ou 1.9) afin de spécifier quelle version de MRI
cibler.

Une liste non exhaustive d'implémentations peut être trouvée [ici (EN)](https://github.com/cogitator/ruby-implementations/wiki/List-of-Ruby-implementations).

## RubySpec

La plupart des implémentations Ruby s'appuient fortement sur [RubySpec](http://rubyspec.org/).
Ruby n'a pas de spécification officielle, c'est pourquoi la commaunité a écrit
des spécifications exécutables en Ruby pour tester la compatibilité de leur
implémentation avec MRI.

## RubyGems

[RubyGems](http://rubygems.org/) est un gestionnaire de paquets communautaire
pour Ruby.
RubyGems est livré avec Ruby, il n'y a donc pas besoin de le télécharger
séparément.

Les paquets Ruby sont appelés des "gems", et peuvent être hébergés par la
communauté à RubyGems.org. Chaque gem contient son code source et quelques
métadatas, includant des choses comme la version, les dépendances,
l(es) auteur(s) et la/les licence(s).

## Bundler

[Bundler](http://bundler.io/) est un outil de résolution de dépendances des gems. Il
utilise le Gemfile d'un projet pour en trouver les dépendances, et récupère
ensuite les dépendances de ces dépendances récursivement. Il déroule cet
algorithme jusqu'à ce que toutes les dépendances soient résolues et
téléchargées, ou s'arrête si un conflit est trouvé.

Bundler lèvera une erreur s'il trouve des conflits de dépendances. Par exemple,
si la gem A recquiert la version 3 ou plus de gem Z, mais que gem B recquiert
seulement la version 2 de la même gem Z, Bundler vous notifiera ce conflit. Cela devient
extrêmement utile, étant donné que beaucoup de gems font référence à d'autres
gems (qui se réfèrent à d'autres gems et ainsi de suite), ce qui peut former un large graphe de
dépendance à résoudre.

# Les tests

Tester fait partie intégrante de la culture Ruby. Ruby fournit son propre
framework de tests unitaires appelé minitest (ou TestUnit pour Ruby
version 1.8.x). Il existe beaucoup de librairies de tests avec des buts
différents.

* [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html) - Framework de tests intégré de Ruby version 1.8 style "Unit"
* [minitest](http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html) - Framework de tests intégré de Ruby version 1.9/2.0
* [RSpec](http://rspec.info/) - Un framework de tests qui se focalise sur l'expressivité
* [Cucumber](http://cukes.info/) - Un framework de tests BDD ([behaviour-driven development](https://fr.wikipedia.org/wiki/Behavior_driven_development)) qui parse les tests formatés de Gherkin.

## Soyez gentil

La communauté Ruby est fière d'être une communauté ouverte, riche et
accueillante. Matz lui-même est extrêmement sociable, et la générosité des
rubyistes est généralement remarquable.
---
language: ruby
filename: learnruby-fr.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]

translators:
  - ["Geoffrey Roguelon", "https://github.com/GRoguelon"]
  - ["Nami-Doc", "https://github.com/Nami-Doc"]
  - ["Sylvain Abélard", "http://github.com/abelards"]
lang: fr-fr
---

```ruby
# Ceci est un commentaire

=begin
Ceci est un commentaire multiligne
Personne ne les utilise
Vous devriez en faire de même
=end

# Tout d'abord : tout est un objet.

# Les nombres sont des objets

3.class #=> Fixnum # on voit que c'est une classe Ruby et non un "type spécial"

3.to_s #=> "3" # on peut appeler des méthodes sur ces objets, comme `to_s` (transforme en texte)

# Les opérateurs de base
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
22 / 7 #=> 3 # si les deux éléments sont entiers, c'est une division entière
22.0 / 7 #=> 3.142857142857143
22 / 7.0 #=> 3.142857142857143
2**10 #=> 1024 # exposant
1024 % 10 #=> 4 # modulo (reste de la division euclidienne)

# Les opérateurs sont juste des raccourcis
# pour appeler une méthode sur un objet
1.+(3) #=> 4
10.* 5 #=> 50

# Les valeurs spéciales sont des objets
nil # nul, vide ou indéfini
true # vrai
false # faux

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Égalité
1 == 1 #=> true
2 == 1 #=> false

# Inégalité
1 != 1 #=> false
2 != 1 #=> true
!true  #=> false
!false #=> true

# à part false lui-même, nil est la seule autre valeur "considérée comme fausse"
!nil   #=> true
!false #=> true
!0     #=> false

# Plus de comparaisons
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Les chaînes de caractères sont des objets
'Je suis une chaîne de caractères'.class #=> String
"Je suis également une chaîne de caractères".class #=> String

placeholder = "utiliser l'interpolation de chaîne de caractères"
"Je peux #{placeholder} quand j'utilise les guillemets"
#=> "Je peux utiliser l'interpolation de chaîne de caractères quand j'utilise les guillemets"

# Affichez un message
puts "J'affiche à l'écran!"

# Il y a quelques autres raccourcis pour les chaînes de caractères
rb = "Ruby"
interpolation = "Bonjour Ruby" # s'écrit aussi %Q[Bonjour #{rb}] %Q{Bonjour #{rb}} avec l'interpolation
literal = "Bonjour \#{rb}" # avec le backslash, le dièse est un "vrai dièse" (le slash ne s'affiche que pour le debug)
literal == %q[Bonjour #{rb}] # le Q majuscule fait l'interpolation, le q minuscule ne la fait pas
multi = %Q[Cette chaîne
est sur plusieurs
lignes] # => "Cette chaîne\nest sur plusieurs\nlignes" # le caractère \n signifie retour à la ligne

# Variables
x = 25 #=> 25
x #=> 25

# Notez que l'affectation retourne la valeur affectée.
# Cela signifie que vous pouvez affecter plusieurs fois de suite :

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Par convention, utilisez la notation underscore
# pour nommer les variables
snake_case = true

# Utilisez des noms de variable explicites
path_to_project_root = '/nom/correct/'
path = '/mauvais/nom/'

# Symboles (aussi des objets)
# Les symboles sont immuables, constants,
# réutilisables et représentés en interne
# par une valeur entière. Ils sont souvent
# utilisés à la place des chaînes de caractères
# pour transmettre efficacement des valeurs
# spécifiques ou significatives

:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# Tableaux

# Ceci est un tableau
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Les tableaux contiennent différents types d'élément.

[1, "hello", false] #=> [1, "hello", false]

# Les tableaux peuvent être indexés
# Trouver la valeur en donnant la position en partant du début (le premier élément est à l'index 0)
array[0] #=> 1
array[12] #=> nil

# Comme les opérateurs, la syntaxe [var] est juste un raccourci
# pour appeler la méthode [] d'un objet
array.[] 0 #=> 1
array.[] 12 #=> nil

# On peut compter en partant de la fin avec un index négatif (le dernier élément est à l'index -1)
array[-1] #=> 5

# Avec un index de début et un nombre d'éléments [position, nombre]
array[1, 3] #=> [2, 3, 4]

# Ou avec un intervalle [index de début .. index de fin]
array[1..3] #=> [2, 3, 4]

# Ajoutez un élément au tableau comme ceci
array << 6 #=> [1, 2, 3, 4, 5, 6]

# Les Hash sont des dictionnaires Ruby contenant des paires de clé/valeur.
# Les Hash sont délimitées par des accolades :
hash = {'color' => 'green', 'number' => 5}

hash.keys #=> ['color', 'number']

# Les Hash retournent la valeur
# en utilisant la clé associée à la valeur :
hash['color'] #=> 'green'
hash['number'] #=> 5

# Recherchez une clé inexistante dans une Hash retourne nil :
hash['nothing here'] #=> nil

# Depuis Ruby 1.9, Une syntaxe spécifique est apparue
# en utilisant les symboles comme clés :

new_hash = { defcon: 3, action: true}

new_hash.keys #=> [:defcon, :action]

# Astuce : Les tableaux et les Hash sont dénombrables
# Ils partagent un certain nombre de méthodes pratiques
# telle que each, map, count, etc...

# Structures de contrôle

if true
  "si l'instruction est vraie"
elsif false
  "si l'instruction de départ n'était pas vraie, et que cette nouvelle condition est vraie (facultatif)"
else
  "tous les autres cas (il est également facultatif de faire une clause else)"
end

for compteur in 1..5
  puts "itération #{compteur}"
end
#=> itération 1
#=> itération 2
#=> itération 3
#=> itération 4
#=> itération 5

# CEPENDANT, l'usage de la boucle for est très rare.
# À la place, utilisez la méthode "each"
# et passez lui un bloc de code.
# Un bloc de code est un ensemble d'instructions
# que vous pouvez passer à une methode comme "each".
# Les blocs sont similaires aux lambdas, aux fonctions anonymes
# ou encore aux closures dans d'autres langages.
#
# La méthode "each" exécute le bloc de code
# pour chaque élément de l'intervalle d'éléments.
# Le bloc de code passe un paramètre compteur.
# Appelez la méthode "each" avec un bloc de code comme ceci :

(1..5).each do |compteur|
  puts "itération #{compteur}"
end
#=> itération 1
#=> itération 2
#=> itération 3
#=> itération 4
#=> itération 5

# Vous pouvez également mettre un bloc de code entre accolades :
(1..5).each {|compteur| puts "itération #{compteur}"}

# Le contenu des structures de données peut être parcouru
# en utilisant la méthode each.
array.each do |element|
  puts "#{element} est une partie du tableau"
end
hash.each do |cle, valeur|
  puts "#{cle} est #{valeur}"
end

compteur = 1
while compteur <= 5 do
  puts "itération #{compteur}"
  compteur += 1
end
#=> itération 1
#=> itération 2
#=> itération 3
#=> itération 4
#=> itération 5

grade = 'B'

case grade
when 'A'
  puts "Excellent"
when 'B'
  puts "Plus de chance la prochaine fois"
when 'C'
  puts "Vous pouvez faire mieux"
when 'D'
  puts "C'est pas terrible"
when 'F'
  puts "Vous avez échoué!"
else
  puts "Sytème de notation alternatif"
end

# Fonctions

def double(x)
  x * 2
end

# Les fonctions (et tous les blocs de code) retournent
# implicitement la valeur de la dernière instruction évaluée
double(2) #=> 4

# Les parenthèses sont facultatives
# lorsqu'il n'y a pas d'ambiguïté sur le résultat
double 3 #=> 6

double double 3 #=> 12

def sum(x,y)
  x + y
end

# Les paramètres de méthode sont séparés par des virgules
sum 3, 4 #=> 7

sum sum(3,4), 5 #=> 12

# yield
# Toutes les méthodes ont un argument facultatif et implicite
# de type bloc de code
# il peut être appelé avec le mot clé 'yield'

def surround
  puts "{"
  yield
  puts "}"
end

surround { puts 'Bonjour tout le monde' }

# {
# Bonjour tout le monde
# }


# Définissez une classe avec le mot clé 'class'
class Humain

  # Une variable de classe
  # est partagée par toutes les instances de cette classe.
  @@espece = "H. sapiens"

  # Constructeur de classe
  def initialize(nom, age = 0)
    # Affectez l'argument à la variable d'instance 'nom'
    # pour la durée de vie de l'instance de cette classe
    @nom = nom
    # Si le paramètre 'age' est absent,
    # la valeur par défaut définie dans la liste des arguments sera utilisée.
    @age = age
  end

  # Une simple méthode setter
  def nom=(nom)
    @nom = nom
  end

  # Une simple méthode getter
  def nom
    @nom
  end

  # Une méthode de classe utilise le mot clé 'self'
  # pour se distinguer de la méthode d'instance.
  # La méthode sera alors accessible à partir de la classe
  # et non pas de l'instance.
  def self.say(msg)
    puts "#{msg}"
  end

  def espece
    @@espece
  end

end


# Instanciez une classe
jim = Humain.new("Jim Halpert")

dwight = Humain.new("Dwight K. Schrute")

# Appelez quelques méthodes
jim.espece #=> "H. sapiens"
jim.nom #=> "Jim Halpert"
jim.nom = "Jim Halpert II" #=> "Jim Halpert II"
jim.nom #=> "Jim Halpert II"
dwight.espece #=> "H. sapiens"
dwight.nom #=> "Dwight K. Schrute"

# Appelez la méthode de classe
Humain.say("Hi") #=> "Hi"

# Les classes sont également des objets en Ruby.
# Par conséquent, les classes peuvent avoir des variables d'instance.
# Les variables de classe sont partagées parmi
# la classe et ses descendants.

# Classe parente
class Humain
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(valeur)
    @@foo = valeur
  end
end

# Classe fille
class Travailleur < Humain
end

Humain.foo # 0
Travailleur.foo # 0

Humain.foo = 2 # 2
Travailleur.foo # 2

# Les variables d'instance de classe ne sont pas partagées
# avec les classes héritées.

class Humain
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(valeur)
    @bar = valeur
  end
end

class Docteur < Humain
end

Humain.bar # 0
Docteur.bar # nil

```
---
language: rust
contributors:
    - ["P1start", "http://p1start.github.io/"]
translators:
    - ["Ryan Rembert", "http://jrrembert.github.io"]
filename: learnrust-fr.rs
lang: fr-fr
---

Rust est un langage de programmation développé par Mozilla Research. Rust combine le contrôle de bas niveau sur la performance avec la commodité et la sécurité garanties de haut niveau.

Il atteint ces objectifs sans avoir besoin d'un ramasse-miettes ou environnement d'exécution, ce qui rend possible l'utilisation de bibliothèques Rust comme une substitution directe pour C.

La première version de Rust, 0.1, est sortie en janvier 2012 et a tellement évolué rapidement que jusqu'à récemment, l'utilisation de versions stables était déconseillée - à la place ce était conseillé d'utiliser les nightly builds.

Le 15 mai 2015, Rust 1.0 a été libéré avec une garantie complète de compatibilité ascendante. Améliorations aux temps de compilation et d'autres aspects du compilateur sont actuellement disponibles dans la nightly builds. Rust a adopté un modèle de train de livraison avec les versions régulières toutes les six semaines. Rust 1.1 beta a été mis à disposition au moment de la livraison de Rust 1.0.

Bien que Rust soit un langage relativement bas niveau, Rust a quelques concepts fonctionnels qui se trouvent généralement dans les langues de niveau supérieur. Cela rend Rust non seulement rapide, mais aussi efficace et facile à coder.

```rust
// Ceci est un commentaire. Les commentaires de ligne ressemblent à ceci...
// et continuent sur plusieurs lignes comme cela.

/// Les commentaires de documentation ressemblent à ça et supportent la
/// syntaxe Markdown.
/// # Exemples
///
/// ```
/// let cinq = 5
/// ```

///////////////
// 1. Basics //
///////////////

// Les fonctions
// `I32` est le type 32 bits entiers signés
fn add2(x: i32, y: i32) -> i32 {
    // Retour implicite (pas de point virgule)
    x + y
}

// Fonction principale
fn main() {
    // Nombres //

    // Liaison immutable
    let x: i32 = 1;

    // Suffixes entiers et flottants
    let y: I32 = 13i32;
    let f: f64 = 1.3f64;

    // Inférence de type
    // La plupart du temps, le compilateur Rust peut déduire quel est le type 
    // de variable, donc vous n'avez pas à écrire une annotation de type explicite.
    // Tout au long de ce tutoriel, les types sont explicitement annotées dans
    // de nombreux endroits, mais seulement à des fins de démonstration.
    // L'inférence de type peut les générer pour vous la plupart du temps.
    let implicit_x = 1;
    let implicit_f = 1,3;

    // Arithmétique
    let somme = x + y + 13;

    // Variable Mutable
    let mut mutable = 1;
    let mutable = 4;
    let mutable += 2;

    // Chaînes //

    // Chaîne littérales
    let x: &str = "Bonjour tout le monde !";

    // Affichage
    println!("{} {}", f, x); // 1.3 Bonjour tout le monde

    // Une `Chaîne` - une chaîne de tas alloué
    let s: String = "Bonjour tout le monde".to_string();

    // Un morceau de chaîne - une vue immutable sur une autre chaîne.
    // C'est essentiellement un pointeur immutable sur une chaîne - ça ne
    // contient effectivement pas le contenu d'une chaîne, juste un pointeur vers
    // le début et la fin de la chaîne.
    let s_slice: &str = &s;

    println!("{} {}", s, s_slice); // Bonjour tout le monde Bonjour tout le monde

    // Vecteurs/tableau //

    // Un tableau de taille fixe
    let four_ints: [i32; 4] = [1, 2, 3, 4];

    // Un tableau dynamique(vecteur)
    let mut vecteur: Vec<i32> = vec![1, 2, 3, 4];
    vecteur.push(5);

    // Une tranche - une vue immutable sur un vecteur ou un tableau.
    // Ceci est un peu comme un morceau de chaîne, mais pour les vecteurs.
    let tranche: &[i32] = &vecteur;

    // Utiliser `{:?}` pour afficher quelque chose en mode debug
    println!("{:?} {:?}", vecteur, tranche); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    // Tuples //

    // Un tuple est un ensemble de valeurs qui peuvent être de différents types.
    let x:(i32, &str, f64) = (1, "bonjour", 3.4);

    // Déstructurer `let`
    let (a, b, c) = x;
    println!("{} {} {}", a, b, c); // 1 bonjour 3.4

    // indexation
    println!("{}", x.1); // Bonjour

    //////////////
    // 2. Types //
    //////////////

    // Struct
    struct Point {
        x: i32,
        y: i32,
    }

    let origine: Point = Point { x: 0, y: 0 };

    // Un struct avec des champs sans nom, appelé 'tuple struct'.
    struct Point2(i32, i32);

    let origine2 = Point2(0, 0);

    // Basic C-like enum
    enum Direction {
        Àgauche,
        Droite,
        En_Haut,
        Vers_Le_Bas,
    }

    let en_haut = Direction::En_Haut;

    // Enum avec des champs
    enum OptionnelI32 {
        AnI32(I32),
        Rien,
    }

    let deux: OptionnelI32 = OptionnelI32::AnI32(2);
    let rien = OptionnelI32::Rien;

    // Generics //

    struct Foo<T> { bar: T }

    // Ceci est défini dans la bibliothèque standard comme `Option`.
    enum Optionnel<T> {
        SomeVal(T),
        NoVal,
    }

    // Méthodes //

    impl<T> Foo<T> {
        // Méthodes prennent un paramètre explicite `de self`.
        fn get_bar(self) -> T {
            self.bar
        }
    }

    let a_foo = Foo { bar: 1 };
    println!("{}", a_foo.get_bar()); // 1

    // Traits (connu sous le nom des interfaces ou des classes de types dans
    // d'autres langues).

    trait Frobnicate<T> {
        fn frobnicate(self) -> Option<T>;
    }

    impl<T> Frobnicate<T> for Foo<T> {
        fn frobnicate(self) -> Option<T> {
            Some(self.bar)
        }
    }

    let another_foo = Foo { bar: 1 };
    println!("{:?}", another_foo.frobnicate()); // Some(1)

    /////////////////////////
    // 3. Motif correspondant //
    /////////////////////////

    let foo = OptionnelI32::AnI32(1);
    match foo {
        OptionnelI32::AnI32(n) => println!("Il est un i32: {}", n),
        OptionnelI32::Rien     => println!("Il n'y a rien!"),
    }

    // Motif avancé correspondant
    struct FooBar { x: i32, y: OptionnelI32 }
    let bar = FooBar { x: 15, y: OptionnelI32::AnI32(32) };

    match bar {
        FooBar { x: 0, y: OptionnelI32 :: AnI32(0)} =>
            println!("Les chiffres sont nuls!"),
        FooBar { x: n, y: OptionnelI32 :: AnI32(m)} if n == m =>
            println!("Les chiffres sont les mêmes"),
        FooBar { x: n, y: OptionnelI32 :: AnI32(m)} =>
            println!("Différents numéros: {} {}", n, m)!,
        FooBar { x: _, y: OptionnelI32 :: Rien} =>
            println!("Le deuxième numéro est rien!"),
    }

    /////////////////////
    // 4. Flux de contrôle //
    /////////////////////

    // `for` boucles / itération
    let array = [1, 2, 3];
    for i in array.iter() {
        println!("{}", i);
    }

    // Ranges
    for i in 0u32..10 {
        print!("{}", i);
    }
    println!("");
    // imprime `0 1 2 3 4 5 6 7 8 9`

    // `if`
    if 1 == 1 {
        println!("Maths est travaille!");
    } else {
        println!("Oh non ...!");
    }

    // `if` comme expression
    let valeur = if true {
        "bien"
    } else {
        "mal"
    };

    // `while` boucle
    while 1 == 1 {
        println!("L'univers fonctionne normalement.");
    }

    // Boucle infinie
    loop {
        println!("Bonjour!");
    }

    /////////////////////////////////
    // 5. Sécurité & pointeurs mémoire //
    /////////////////////////////////

    // Pointeur occasion - une seule chose peut "posséder" pointeur à un moment.
    // Cela signifie que lorsque le `Box` laisse son champ d'application, il
    // peut être automatiquement libérée en toute sécurité.
    let mut mine: Box<i32> = Box::new(3);
    *mine = 5; // déréférencer
    // Ici, `now_its_mine` prend possession de `mine`. En d'autres termes,
    // `mine` est déplacé.
    let mut now_its_mine = mine;
    *now_its_mine += 2;

    println!("{}", now_its_mine); // 7
    // println!("{}", now_its_mine); // Cela ne compile pas parce
    // que `now_its_mine` possède maintenant le pointeur

    // Référence - un pointeur immutable qui fait référence à d'autres données.
    // Quand une référence est prise à une valeur, nous disons que la valeur
    // a été "emprunté".
    // Même si une valeur est emprunté immutablement, il ne peut pas être
    // muté ou déplacé.
    // Un emprunt dure jusqu'à la fin de la portée, il a été créé.
    let mut var = 4;
    var = 3;
    let ref_var: &i32 = &var;

    println!("{}", var); // Contrairement à `mine`, `var` peut encore être utilisé
    println!("{}", *ref_var);
    // Var = 5; // Cela ne compile pas parce que `var` est emprunté.
    // *ref_var = 6; // Ce ne serait pas correct non plus, parce que `ref_var` est une
    // référence immutable.

    // Référence Mutable
    // Même si une valeur est empruntée de façon mutable, elle ne peut pas être
    // accessible à tous.
    let mut var2 = 4;
    let ref_var2: &mut i32 = &mut var2;
    // '*' est utilisé pour pointer vers le var2 mutably emprunté.
    *ref_var2 += 2;

    println!("{}", * ref_var2); // 6, // var2 ne compilerait pas.
    // ref_var2 est de type &mut i32 donc stocke la référence à i32,
    // pas la valeur.
    // var2 = 2; // Cela ne compile pas parce que `var2` est emprunté.
}
```

## Autres lectures

Il y a beaucoup plus à Rust -- ce est juste l'essentiel de Rust afin que vous puissiez comprendre
les choses les plus importantes. Pour en savoir plus sur Rust, lire [La Programmation Rust
Langue](http://doc.rust-lang.org/book/index.html) et etudier la
[/r/rust](http://reddit.com/r/rust) subreddit. Les gens sur le canal de #rust sur
irc.mozilla.org sont aussi toujours prêts à aider les nouveaux arrivants.

Vous pouvez également essayer caractéristiques de Rust avec un compilateur en ligne sur le fonctionnaire
[Rust parc](http://play.rust-lang.org) ou sur la principale
[Site Rust](http://rust-lang.org).
---
language: Scala
contributors:
    - ["George Petrov", "http://github.com/petrovg"]
    - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
translators:
    - ["Anne-Catherine Dehier", "https://github.com/spellart"]
filename: learnscala-fr.scala
lang: fr-fr
---

### Scala - le langage évolutif

```scala

/*
  Pour vous préparer :

  1) (Téléchargez Scala)[http://www.scala-lang.org/downloads]
  2) Dézippez/décompressez dans votre endroit préféré
  et ajoutez le chemin du sous-répertoire bin au chemin du système
  3) Commencez un REPL de Scala en tapant juste scala. Vous devriez voir le prompteur :

  scala>

  C'est ce qu'on appelle un REPL (Read-Eval-Print-Loop), c'est une interface de programmation interactive.
  Vous pouvez y exécuter des commandes.
  Allons-y :
*/

println(10) // affiche l'integer 10

println("Boo!") // affiche avec retour à la ligne la chaîne de caractère Boo!


// Quelques basiques

// Imprimer et forcer une nouvelle ligne à la prochaine impression
println("Hello world!")
// Imprimer sans forcer une nouvelle ligne à la prochaine impression
print("Hello world")

// Pour déclarer des valeurs on utilise var ou val
// Les déclarations val sont immuables, tandis que les var sont muables.
// L'immuabilité est une bonne chose.

val x = 10 // x vaut maintenant 10
x = 20 // erreur : réaffectation à val
var x = 10
x = 20 // x vaut maintenant 20

// Les commentaires d'une ligne commencent par deux slashs

/*
Les commentaires multilignes ressemblent à ça.
*/

// les valeurs booléennes
true
false

// Les opérateurs booléens
!true // false
!false // true
true == false // false
10 > 5 // true

// Les opérateurs mathématiques sont habituels
1 + 1 // 2
2 - 1 // 1
5 * 3 // 15
6 / 2 // 3


// Le REPL donne le type et la valeur du résultat quand vous évaluez une commande

1 + 7

/* Les lignes ci-dessous donnent les résultats :

  scala> 1 + 7
  res29: Int = 8

  Ça signifie que le résultat de l'évaluation 1 + 7 est un objet de
  type Int avec une valeur de 8

  1+7 donnera le même résultat
*/


// Tout est un objet, même une fonction. Tapez ceci dans le REPL :

7 // donne res30: Int = 7 (res30 est seulement un nom de variable généré pour le résultat)


// La ligne suivante est une fonction qui prend un Int et retourne son carré
(x:Int) => x * x


// On peut assigner cette fonction à un identifieur comme ceci :
val sq = (x:Int) => x * x

/* La ligne suivante nous dit :

   sq: Int => Int = <function1>

   Ce qui signifie que cette fois-ci nous avons donné un nom explicite à la valeur.
   sq est une fonction qui prend un Int et retourne un Int.


   sq peut être exécutée comme ci-dessous :
*/

sq(10) // donne comme résultat : res33: Int = 100.


// les deux-points définissent explicitement le type de la valeur,
// dans ce cas une fonction qui prend un Int et retourne un Int.
val add10: Int => Int = _ + 10

// Scala autorise des méthodes et des fonctions à retourner
// ou prendre comme paramètres des autres fonctions ou méthodes


List(1, 2, 3) map add10 // List(11, 12, 13) - add10 est appliqué à chaque éléments


// Les fonctions anonymes peuvent être utilisées à la place des fonctions nommées :
List(1, 2, 3) map (x => x + 10)




// Le tiret du bas peut être utilisé si la fonction anonyme ne prend qu'un paramètre.
// Il se comporte comme une variable
List(1, 2, 3) map (_ + 10)



// Si le bloc et la fonction anonyme prennent tous les deux un seul argument,
// vous pouvez omettre le tiret du bas
List("Dom", "Bob", "Natalia") foreach println



// Les structures de données

val a = Array(1, 2, 3, 5, 8, 13)
a(0)
a(3)
a(21)    // Lance une exception

val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
m("fork")
m("spoon")
m("bottle")       // Lance une exception

val safeM = m.withDefaultValue("no lo se")
safeM("bottle")

val s = Set(1, 3, 7)
s(0)
s(1)

/* Jetez un oeil sur la documentation de map ici -
 * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
 */


// Tuples

(1, 2)

(4, 3, 2)

(1, 2, "three")

(a, 2, "three")

// Exemple d'utilisation
val divideInts = (x:Int, y:Int) => (x / y, x % y)


divideInts(10,3) // La fonction divideInts donne le résultat et le reste de la division

// Pour accéder à un élément d'un tuple, utilisez _._n
// où n est l'index de base 1 de l'élément
val d = divideInts(10,3)

d._1

d._2



// Des combinaisons

s.map(sq)

val sSquared = s. map(sq)

sSquared.filter(_ < 10)

sSquared.reduce (_+_)



// La fonction filter prend un prédicat (une fonction de type A -> Booléen) et
// sélectionne tous les éléments qui satisfont ce prédicat
List(1, 2, 3) filter (_ > 2) // List(3)
case class Person(name: String, age: Int)
List(
  Person(name = "Dom", age = 23),
  Person(name = "Bob", age = 30)
).filter(_.age > 25) // List(Person("Bob", 30))



// Scala a une méthode foreach définie pour certaines collections
// qui prend en argument une fonction renvoyant Unit (une méthode void)
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println




// Compréhensions de listes

for { n <- s } yield sq(n)

val nSquared2 = for { n <- s } yield sq(n)

for { n <- nSquared2 if n < 10 } yield n

for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared



/* Les exemples précédents ne sont pas des boucles for. La sémantique des boucles for
   est "répète", alors qu'une for-compréhension définit une relation
   entre deux ensembles de données. */



// Boucles et itération

1 to 5
val r = 1 to 5
r.foreach( println )

r foreach println
// NB: Scala est vraiment tolérant par rapport aux points et aux parenthèses en étudiant les roles séparément.
// Ça aide pour écrire des DSL ou des API qui se lisent comme en anglais.


(5 to 1 by -1) foreach ( println )

// Une boucle while
var i = 0
while (i < 10) {  println("i " + i); i+=1  }

while (i < 10) {  println("i " + i); i+=1  }  // Oui, encore. Qu'est-ce qui s'est passé ? Pourquoi ?






i    // Montre la valeur de i. Notez que while est une boucle au sens classique.
     // Il exécute séquentiellement pendant que la variable de boucle change.
     // While est très rapide,
     // mais utiliser des combinateurs et des compréhensions comme ci-dessus est plus
     // facile pour comprendre et pour faire la parallélisation

i = 0
// La boucle do while
do {
  println("x is still less than 10");
  i += 1
} while (i < 10)


// La récursivité est un moyen idiomatique de faire une chose répétitive en Scala.
// Les fonctions récursives ont besoin d'un type de retour explicite,
// le compilateur ne peut pas le déduire.
// Ici c'est Unit.
def showNumbersInRange(a:Int, b:Int):Unit = {
  print(a)
  if (a < b)
    showNumbersInRange(a + 1, b)
}



// Structures de contrôle

val x = 10

if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println ("yeah") else println("nay")

println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"

var i = 0
while (i < 10) { println("i " + i); i+=1  }



// Les caractéristiques "Orienté Objet"

// Création d'une classe Dog
class Dog {
  // Une méthode appelée bark qui retourne une chaîne de caractère
  def bark: String = {
    // le corps de la méthode
    "Woof, woof!"
  }
}


// Les classes peuvent contenir presque n'importe quelle autre construction, incluant d'autres classes,
// des fonctions, des méthodes, des objets, des classes case, des traits, etc ...



// Les classes case

case class Person(name:String, phoneNumber:String)

Person("George", "1234") == Person("Kate", "1236")




// Correspondances de motifs

val me = Person("George", "1234")

me match { case Person(name, number) => {
            "We matched someone : " + name + ", phone : " + number }}

me match { case Person(name, number) => "Match : " + name; case _ => "Hm..." }

me match { case Person("George", number) => "Match"; case _ => "Hm..." }

me match { case Person("Kate", number) => "Match"; case _ => "Hm..." }

me match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" }

val kate = Person("Kate", "1234")

kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" }



// Expressions régulières

val email = "(.*)@(.*)".r  // On fait une Regex en invoquant r sur la chaîne de caractère

val email(user, domain) = "henry@zkpr.com"

"mrbean@pyahoo.com" match {
  case email(name, domain) => "I know your name, " + name
}



// Les chaînes de caractères

"Les chaînes de caractères Scala sont entourées de doubles guillements"
'a' // Un caractère de Scala
// 'Les simples guillemets n'existent pas en Scala' // Erreur
"Les chaînes de caractères possèdent les méthodes usuelles de Java".length
"Il y a aussi quelques méthodes extra de Scala.".reverse

// Voir également : scala.collection.immutable.StringOps

println("ABCDEF".length)
println("ABCDEF".substring(2, 6))
println("ABCDEF".replace("C", "3"))

val n = 45
println(s"We have $n apples")

val a = Array(11, 9, 6)
println(s"My second daughter is ${a(2-1)} years old")

// Certains caractères ont besoin d'être "échappés",
// ex un guillemet à l'intérieur d'une chaîne de caractères :
val a = "They stood outside the \"Rose and Crown\""

// Les triples guillemets permettent d'écrire des chaînes de caractères
// sur plusieurs lignes et peuvent contenir des guillemets

val html = """<form id="daform">
                <p>Press belo', Joe</p>
             |  <input type="submit">
              </form>"""



// Structure et organisation d'une application

// Importer des chaînes de caratères
import scala.collection.immutable.List

// Importer tous les sous-paquets
import scala.collection.immutable._

// Importer plusieurs classes en une seule instruction
import scala.collection.immutable.{List, Map}

// Renommer un import en utilisant '=>'
import scala.collection.immutable.{ List => ImmutableList }

// Importer toutes les classes, à l'exception de certaines.
// La ligne suivante exclut Map et Set :
import scala.collection.immutable.{Map => _, Set => _, _}

// Le point d'entrée du programme est défini dans un fichier scala
// utilisant un objet, avec une simple méthode main :
object Application {
  def main(args: Array[String]): Unit = {
    // Votre code ici.
  }
}

// Les fichiers peuvent contenir plusieurs classes et plusieurs objets.
// On les compile avec scalac




// Entrée et Sortie

// Pour lire un fichier ligne par ligne
import scala.io.Source
for(line <- Source.fromFile("myfile.txt").getLines())
  println(line)

// On utilise le PrintWriter de Java pour écrire un fichier


```

## Autres ressources

[Scala for the impatient](http://horstmann.com/scala/)

[Twitter Scala school](http://twitter.github.io/scala_school/)

[The scala documentation](http://docs.scala-lang.org/)

[Try Scala in your browser](http://scalatutorials.com/tour/)

Rejoindre le [Scala user group](https://groups.google.com/forum/#!forum/scala-user)
---
category: tool
tool: tmux
contributors:
    - ["mdln", "https://github.com/mdln"]
translators:
    - ["Xuan-thi Nguyen", "https://github.com/mellenguyen"]
filename: LearnTmux-fr.txt
lang: fr-fr
---


[Tmux](http://tmux.sourceforge.net) est un multiplexeur de terminal: il permet
de créer plusieurs terminaux, accédés et contrôlés depuis un seul écran. Tmux
peut être détaché de l'écran tout en continuant de fonctionner en tâche de
fond, puis rattaché de nouveau.


```

  tmux [command]     # Exécute une commande
                     # 'tmux' sans commande créé une nouvelle session

    new              # Créé une nouvelle session
     -s "Session"    # Créé une session nommée "Session"
     -n "Window"     # Créé une fenêtre nommée "Window"
     -c "/dir"       # Démarre dans le dossier cible "/dir"

    attach           # S'attache à la dernière session ou la session disponible
     -t "#"          # S'attache à la session cible
     -d              # Détache la session des autres instances

    ls               # Liste les sessions ouvertes
     -a              # Liste toutes les sessions ouvertes

    lsw              # Liste les fenêtres de la session courante
     -a              # Liste toutes les fenêtres
     -s              # Liste toutes les fenêtres en session

    lsp              # Liste les panels
     -a              # Liste tous les panels
     -s              # Liste tous les panels en session
     -t              # Liste tous les panels dans la cible

    kill-window      # Tue la fenêtre courante
     -t "#"          # Tue la fenêtre cible
     -a              # Tue toutes les fenêtres
     -a -t "#"       # Tue toutes les fenêtres sauf la cible

    kill-session     # Tue la session courante
     -t "#"          # Tue la session cible
     -a              # Tue toutes les sessions
     -a -t "#"       # Tue toutes les sessions sauf la cible

```


### Raccourcis clavier

Afin de contrôler une session tmux attachée, on utilise une combinaison de
touches appelées 'Préfixe'. Elle doit être pressée afin d'utiliser les
raccourcis.

```
--------------------------------------------------------------------------------
  (C-b) = Ctrl + b  # Combinaison 'Préfixe' requise pour utiliser les raccourcis

  (M-1) = Meta + 1 -ou- Alt + 1
--------------------------------------------------------------------------------

  ?                  # Liste tous les raccourcis
  :                  # Entre dans l'invite de commande de tmux
  r                  # Force la redéfinition du client attaché
  c                  # Créé une nouvelle fenêtre

  !                  # Sépare le panel courant de sa fenêtre
  %                  # Sépare le panel courant en deux, gauche et droite
  "                  # Sépare le panel courant en deux, haut et bas

  n                  # Changer vers la fenêtre suivante
  p                  # Changer vers la fenêtre précédente
  {                  # Echange le panel courant avec le panel précédent
  }                  # Echange le panel courant avec le panel suivant

  s                  # Sélectionne une nouvelle session pour le client attaché
                     # de manière interactive
  w                  # Choisi la fenêtre courante de manière interactive
  0 to 9             # Sélectionne la fenêtre de 0 à 9

  d                  # Détache le client courant
  D                  # Choisi un client à détacher

  &                  # Tue la fenêtre courante
  x                  # Tue le panel courant

  Up, Down           # Change vers le panel au dessus, en dessous, à gauche
  Left, Right        # ou à droite

  M-1 to M-5         # Arrange les panels:
                       # 1) égaliser sur l'horizontale
                       # 2) égaliser sur la verticale
                       # 3) panel principal en haut et le reste en bas
                       #    de gauche à droite
                       # 4) panel principal à gauche et le reste à droite
                       #    de haut en bas
                       # 5) "tiled" : égalise les panels
                       #    sur la hauteur et la largeur

  C-Up, C-Down       # Redimensionne le panel courant par pas de une cellule
  C-Left, C-Right

  M-Up, M-Down       # Redimensionne le panel courant par pas de cinq cellules
  M-Left, M-Right

```


### Configuration de ~/.tmux.conf

tmux.conf peut être utilisé pour fixer les options automatiquement au
démarrage, comme .vimrc ou init.el.

```
# Exemple de tmux.conf
# 2014.10


### Général
###########################################################################

# Active UTF-8
setw -g utf8 on
set-option -g status-utf8 on

# Limite de l'historique
set -g history-limit 2048

# Indice de début du nombre de panels
set -g base-index 1

# Souris
set-option -g mouse-select-pane on

# Force le rechargement du fichier de configuration
unbind r
bind r source-file ~/.tmux.conf


### Raccourcis clavier
###########################################################################

# Annule C-b en tant que préfixe par défaut
unbind C-b

# Définit un nouveau préfixe par défaut
set-option -g prefix `

# Retourne à la fenêtre précédente quand le préfixe est pressé deux fois
bind C-a last-window
bind ` last-window

# Permet d'échanger C-a et ` en utilisant F11/F12
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Préférences de raccourcis clavier
setw -g mode-keys vi
set-option -g status-keys vi

# Navigue entre les panels avec les raccourcis clavier de vim
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Navigation entre les fenêtres
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# Commandes simples de séparation des panels
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# Active la session la plus imbriquée (en faisant de l'imbrication sous tmux)
# pour envoyer des commandes
bind a send-prefix


### Thème
###########################################################################

# Palette de couleurs pour la barre de statuts
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# Palette de couleurs pour les bordures des panels
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Palette de couleurs pour les messages
set-option -g message-fg black
set-option -g message-bg green

# Palette de couleurs pour les fenêtres
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-content-attr default
setw -g window-status-content-fg yellow
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### UI
###########################################################################

# Notification
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# Définir automatiquement des titres de fenêtres
set-option -g set-titles on
# Numéro de fenêtre, nom du programme, actif (ou non)
set-option -g set-titles-string '#H:#S.#I.#P #W #T'

# Réglages de la barre de statuts
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# Présente des indicateurs de performance dans la barre de statuts
# Recquiert https://github.com/thewtex/tmux-mem-cpu-load/
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```


### Références

[Tmux | Home](http://tmux.sourceforge.net)

[Page du manuel Tmux](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)

[Montrer le pourcentage CPU/MEM dans la barre de statuts](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)

[tmuxinator - Gère des sessions tmux complexes](https://github.com/tmuxinator/tmuxinator)
---
language: TypeScript
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
translators:
    - ["Alois de Gouvello", "https://github.com/aloisdg"]
filename: learntypescript-fr.ts
lang: fr-fr
---

TypeScript est un langage visant à faciliter le développement d'applications larges et scalables, écrites en JavaScript.
TypeScript ajoute des concepts classiques comme les classes, les modules, les interfaces, les génériques et le typage statique (optionnel) à JavaScript.
C'est une surcouche de JavaScript : tout le code JavaScript est valide en TypeScript ce qui permet de l'ajouter de façon transparente à n'importe quel projet. Le code TypeScript est transcompilé en JavaScript par le compilateur.

Cet article se concentrera seulement sur la syntaxe supplémentaire de TypeScript, plutôt que celle de [JavaScript] (../javascript/).

Pour tester le compilateur de TypeScript, rendez-vous au [Playground] (http://www.typescriptlang.org/Playground) où vous pourrez coder, profiter d'une autocomplétion et accéder directement au rendu JavaScript.

```js
// Il y a 3 types basiques en TypeScript
var isDone: boolean = false;
var lines: number = 42;
var name: string = "Anders";

// Si nous ne pouvons pas déterminer le type, on utilise `Any`
var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // ok, définitivement un booléen

// Pour les collections, il y a les tableaux typés et les tableaux génériques
var list: number[] = [1, 2, 3]; // Un tableaux typé
var list: Array<number> = [1, 2, 3]; // un tableau générique

// Pour les énumeration
enum Color { Red, Green, Blue };
var c: Color = Color.Green;

// Enfin, `void` est utilisé dans le cas spécifique
// d'une fonction ne retournant rien
function bigHorribleAlert(): void {
  alert("Je suis une petite boîte ennuyeuse !");
}

// Les fonctions sont des entités de première classe. Le langage supporte
// les expressions lambda et utilise l'inférence de type

// Les fonctions ci-dessous sont équivalentes, une signature identique
// sera inférée par le compilateur, et le même JavaScript sera généré
var f1 = function(i: number): number { return i * i; }
// Retourne un type inféré
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
// Retourne un type inféré
var f4 = (i: number) => { return i * i; }
// Retourne un type inféré, ici le mot clé `return` n'est pas nécessaire
var f5 = (i: number) =>  i * i;

// Les interfaces sont structurées, tout les objets qui ont ces propriétés
// sont compatible avec l'interface
interface Person {
  name: string;
  // Les propriétés optionnelles sont identifiées avec un "?"
  age?: number;
  // Et bien sûr, les fonctions
  move(): void;
}

// Un objet implémentant l'interface "Person" peut être traité comme 
// une Person car il a les propriétés "name" et "move"
var p: Person = { name: "Bobby", move: () => {} };
// Des objets implémentants la propriété optionnelle :
// valide car "age" est un nombre
var validPerson: Person = { name: "Bobby", age: 42, move: () => {} };
// invalide car "age" n'est pas un nombre
var invalidPerson: Person = { name: "Bobby", age: true };

// Les interfaces peuvent aussi décrire un type de fonction
interface SearchFunc {
  (source: string, subString: string): boolean;
}

// Seul les types des paramètres sont importants. Les noms ne le sont pas.
var mySearch: SearchFunc;
mySearch = function(src: string, sub: string) {
  return src.search(sub) != -1;
}

// Les membres des classes sont publiques par défaut.
class Point {
  // Propriétés
  x: number;

  // Constructeur - Les mots clés "public" et "private" dans ce contexte
  //  génèrent le code de la propriété et son initialisation dans le
  // constructeur. Ici, "y" sera défini de la même façon que "x",
  // mais avec moins de code. Les valeurs par défaut sont supportées.
  constructor(x: number, public y: number = 0) {
    this.x = x;
  }

  // Fonctions
  dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

  // Membres statiques
  static origin = new Point(0, 0);
}

var p1 = new Point(10 ,20);
var p2 = new Point(25); // y sera 0

// Héritage
class Point3D extends Point {
  constructor(x: number, y: number, public z: number = 0) {
    // Un appel explicite au constructeur de la super classe
    // est obligatoire.
    super(x, y);
  }

  // Redéfinition
  dist() {
    var d = super.dist();
    return Math.sqrt(d * d + this.z * this.z);
  }
}

// Modules, "." peut être utilisé comme un séparateur de sous modules.
module Geometry {
  export class Square {
    constructor(public sideLength: number = 0) {
    }
    area() {
      return Math.pow(this.sideLength, 2);
    }
  }
}

var s1 = new Geometry.Square(5);

// Alias local pour référencer un module
import G = Geometry;

var s2 = new G.Square(10);

// Génériques
// Classes
class Tuple<T1, T2> {
  constructor(public item1: T1, public item2: T2) {
  }
}

// Interfaces
interface Pair<T> {
  item1: T;
  item2: T;
}

// Et fonctions
var pairToTuple = function<T>(p: Pair<T>) {
  return new Tuple(p.item1, p.item2);
};

var tuple = pairToTuple({ item1:"hello", item2:"world"});

// Inclure des références à un fichier :
/// <reference path="jquery.d.ts" />

```

## Lectures complémentaires
 * [Site officiel de TypeScript] (http://www.typescriptlang.org/)
 * [Spécification du langage TypeScript (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [Code source sur GitHub] (https://github.com/Microsoft/TypeScript)
 * [Definitely Typed - repository for type definitions] (http://definitelytyped.org/)
---
category: tool
tool: vim
filename: LearnVim-fr.txt
contributors:
    - ["RadhikaG", "https://github.com/RadhikaG"]
translators:
    - ["Thibault", "https://github.com/napnac"]
lang: fr-fr
---


[Vim](http://www.vim.org)
(Vi IMproved) est le clone le plus populaire de l'éditeur de texte vi sous Unix.
Vim est un éditeur de texte omniprésent sur les systèmes de type Unix, et a pour
objectif la rapidité ainsi que l'augmentation de la productivité. Il a de 
nombreux raccourcis claviers pour une navigation et une édition plus rapide.

## Navigation basique avec Vim

```
    vim <ficher>     # Ouvre <fichier> avec vim
    :q               # Quitte vim
    :w               # Sauvegarde le fichier actuel
    :wq              # Sauvegarde le fichier actuel et quitte vim
    :q!              # Quitte vim sans sauvegarder
                     # ! *force* l'exécution de :q, ce qui par conséquent 
                     # oblige vim à quitter sans sauvegarder
    :x               # Sauvegarde le fichier et quitte vim (raccourcis de :wq)

    u                # Annuler
    CTRL+R           # Rétablir

    h                # Déplace le curseur vers la gauche
    j                # Déplace le curseur vers le bas
    k                # Déplace le curseur vers le haut
    l                # Déplace le curseur vers la droite

    # Mouvements au sein d'une ligne

    0                # Va au début de la ligne
    $                # Va à la fin de la ligne
    ^                # Va au premier caractère non blanc de la ligne

    # Rechercher dans un texte

    /mot             # Surligne toutes les occurrences du mot après le curseur
    ?mot             # Surligne toutes les occurrences du mot avant le curseur
    n                # Déplace le curseur sur la prochaine occurrence du mot recherché
    N                # Déplace le curseur sur la précédente occurrence du mot recherché

    :%s/abc/def/g    # Transforme les 'abc' en 'def' sur chaque ligne du texte
    :s/abc/def/g     # Transforme les 'abc' en 'def' sur la ligne actuelle

    # Se déplacer vers un caractère

    f<caractère>     # Se déplace en avant jusqu'à <caractère>
    t<caractère>     # Se déplace en avant juste avant <caractère>

    # Par exemple
    f<               # Se déplace en avant jusqu'à <
    t<               # Se déplace en avant juste avant <
    
    # Se déplacer dans un mot

    w                # Avance d'un mot
    b                # Recule d'un mot
    e                # Se déplace jusqu'à la fin du mot actuel

    # D'autres raccourcis pour se déplacer

    gg               # Va au début du fichier
    G                # Va à la fin du fichier
    :NB              # Va à la ligne numéro NB (où NB est un nombre)
    H                # Se déplace jusqu'en haut de l'écran
    M                # Se déplace jusqu'au milieu de l'écran
    L                # Se déplace jusqu'en bas de l'écran
```

## Modes

Vim est basé sur le concept de **modes**.

Mode Commande  - pour se déplacer et exécuter des commandes (vim démarre dans ce mode)
Mode Insertion - pour éditer le fichier
Mode Visuel    - pour sélectionner du texte et réaliser des opérations dessus
Mode Ex        - pour entrer des commandes avec ':'

```
    i                # Mode insertion, avant le curseur
    a                # Mode insertion, après le curseur
    v                # Mode visuel
    :                # Mode ex
    <esc>            # 'Echap' permet de revenir dans le mode commande

    # Copier/Coller du texte

    y                # Copie le texte sélectionné
    yy               # Copie la ligne actuelle
    d                # Supprime ce qui est sélectionné
    dd               # Supprime la ligne actuelle
    p                # Colle après le curseur
    P                # Colle avant le curseur
    x                # Supprime le caractère sous le curseur
```

## La "Grammaire" de Vim

Vim peut être vu comme un ensemble de commande sous la forme
'Verbe-Modificateur-Nom' :

Verbe        - notre action
Modificateur - la manière de faire l'action
Nom          - l'objet désigné par l'action

Quelques exemples importants de 'Verbes', 'Modificateurs', et de 'Noms' :

```
    # 'Verbes'
 
    d                # Supprime
    c                # Transforme
    y                # Copie
    v                # Sélectionne

    # 'Modificateurs'

    i                # A l'intérieur
    a                # Autour
    NB               # Nombre
    f                # Cherche quelque chose et se déplace dessus
    t                # Cherche quelque chose et se déplace juste avant
    /                # Cherche une chaîne de caractères après le curseur
    ?                # Cherche une chaîne de caractères avant le curseur

    # 'Noms'

    w                # Mot
    s                # Phrase
    p                # Paragraphe
    b                # Bloc
    
    # Exemple de 'phrases' ou commandes

    d2w              # Supprime 2 mots
    cis              # Transforme l'intérieur de la phrase
    yip              # Copie l'intérieur du paragraphe
    ct<              # Transforme le texte du curseur jusqu'au caractère avant le <
    d$               # Supprime jusqu'à la fin de la ligne
```

## Quelques raccourcis et astuces

```
    >                # Indente la sélection d'un bloc
    <                # Dé-indente la sélection d'un bloc
    :earlier 15m     # Retrouve le document comme il était il y a 15 minutes
    :later 15m       # Inverse la commande précédente
    ddp              # Echange la position de deux lignes consécutives (dd puis p)
    .                # Répète la dernière action effectuée
```

## Macros

Les macros sont des actions enregistrables.
Quand on commence à enregistrer une macro, Vim enregistre **toutes** les actions
et les commandes que vous utilisez, jusqu'à ce que vous arrêtiez d'enregistrer.
Lorsque vous appelez une macro, elle applique exactement les mêmes actions et 
commandes sur le texte sélectionné.

```
    qa               # Commence l'enregistrement de la macro 'a'
    q                # Arrête l'enregistrement
    @a               # Appelle la macro 'a'
```

### Configuration de ~/.vimrc

Le fichier .vimrc est utilisé pour configurer Vim lors du démarrage.

Voici un exemple de fichier ~/.vimrc :

```
" Exemple de ~/.vimrc
" 2015.10 

" Nécessaire à Vim pour être 'iMproved'
set nocompatible

" Détermine l'extension du fichier à partir du nom pour permettre une indentation
" automatique intelligente, etc.
filetype indent plugin on

" Active la coloration syntaxique
syntax on

" Une meilleure complétion de la ligne de commande
set wildmenu

" Utilise une recherche insensible à la case sauf quand on utilise des majuscules
set ignorecase
set smartcase

" Quand on commence une nouvelle ligne et qu'aucun type d'indentation n'est activé
" on utilise la même indentation que sur la ligne précédente
set autoindent

" Affiche le numéro de la ligne sur la gauche de l'écran
set number

" Options d'indentation, à changer en fonction des préférences personnelles

" Nombre d'espaces visuels par tabulation
set tabstop=4

" Nombre d'espaces par tabulation
set softtabstop=4

" Nombre d'espaces indentés avec les opérations d'indentations (>> et <<)
set shiftwidth=4

" Convertis les tabulations en espaces
set expandtab

" Active des tabulations et des espaces intelligents pour l'indentation et l'alignement
set smarttab
```

### Références

[Vim | Home](http://www.vim.org/index.php)

`$ vimtutor`

[A vim Tutorial and Primer](https://danielmiessler.com/study/vim/)

[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about)

[Arch Linux Wiki](https://wiki.archlinux.org/index.php/Vim)
---
language: wolfram
contributors:
    - ["hyphz", "http://github.com/hyphz/"]
translators:
    - ["altaris", "http://github.com/altaris/"]
filename: learnwolfram-fr.nb
lang: fr-fr
---

Le langage Wolfram est utilisé dans les programmes suivants :
* La ligne de commandes interactive noyau du Raspberry Pi, mais elle ne peut pas
gérer des éléments graphiques.
* _Mathematica_, un éditeur de texte riche spécialisé pour les mathématiques :
appuyer sur `Shift + Entrée` dans une cellule de code crée un nouvelle cellule 
contenant le résultat.
* _Wolfram Wokbench_, une variante d'Eclipse spécialisée pour le langage
Wolfram.

Ce code d'exemple peut être utilisé et modifié dans ces logiciels. Cependant, le
copier-coller directement dans Mathematica peut causer des problèmes de
formatage, car il ne contient aucune information de mise en page.

```
(* Ceci est un commentaire *)

(* Dans Mathematica, au lieu d'utiliser ces commentaires, vous pouvez créer des
   cellules de texte et insérer de jolies images *)

(* Saisissez une opération et appuyez sur Shift + Entrée pour obtenir le
   résultat *)
2*2              (* 4 *)
5+8              (* 13 *)

(* Appels de fonction *)
Sin[Pi/2]        (* 1 *)
(* Syntaxe alternative pour les appels de fonction à 1 paramètre *)
Sin@(Pi/2)       (* 1 *)
(Pi/2) // Sin    (* 1 *)

(* Attention : le langage est sensible à la casse ! *)

(* Toutes les expressions sont en réalité des appels de fonction *)
Times[2, 2]      (* 4 *)
Plus[5, 8]       (* 13 *)

(* Utiliser une variable pour la première fois la déclare globalement *)
x = 5            (* 5 *)
x == 5           (* True, l'assignation et le test d'égalité est écrit comme
                    en C *)
x                (* 5 *)
x = x + 5        (* 10 *)
x                (* 10 *)
Set[x, 20]       (* TOUT est un appel de fonction, TOUUUUUUUUT *)
x                (* 20 *)

(* Le langage Wolfram effectue des manipulations symboliques, donc utiliser des
   variables non déclarées est légal *)
truc + 5         (* 5 + truc, comme truc n'est pas déclarée, l'évaluation
                    s'arrête là *)
truc + 5 + 10    (* 15 + truc, on évalue ce qu'on peut... *)
%                (* 15 + truc, % représente le dernier résultat *)
% - truc         (* 15, les variables non déclarées peuvent quand même
                    s'annuler *)
chose = truc + 5 (* Attention : chose est ici une expression et non un nombre *)

(* Déclaration d'une fonction *)
Double[x_] := x * 2     (* Le symbole := empêche l'évaluation immédiate du terme
                           à droite *)
Double[10]              (* 20 *)
Double[Sin[Pi/2]]       (* 2 *)
Double @ Sin @ (Pi/2)   (* 2, Utiliser @ évite les paquets de crochets
                           fermants si moches *)
(Pi/2) // Sin // Double (* 2, Utiliser // permet d'écrire les fonctions dans
                           l'ordre d'appel *)

(* En programmation impérative, utiliser ; pour séparer les expressions *)
Salut[] := (Print@"Hello"; Print@"World")  (* Les parenthèses sont nécessaires
                                              car ; est prioritaire sur := *)
Salut[]                                    (* Hello World *)

(* Boucles For à la C *)
Compter[x_] := For[y=0, y<x, y++, (Print[y])]  (* L'évaluation des boucles For
                                                  se fait comme en C *)
Compter[5]                                     (* 0 1 2 3 4 *)

(* Boucles While *)
x = 0; While[x < 2, (Print@x; x++)]     (* De nouveau, comme en C *)

(* Expressions conditionnelles et If *)
x = 8; If[x==8, Print@"Huit", Print@"Pas huit"] (* If [condition, si vrai,
                                                   si faux] *)
Switch[x, 2, Print@"Deux", 8, Print@"Huit"]     (* Switch par valeur *)
Which[x==2, Print@"Deux", x==8, Print@"Huit"]   (* Switch du type if, else if,
                                                   else if, ..., else *)

(* Les variables autres que les paramètres de fonctions sont par défaut
   globales, même à l'intérieur des fonctions *)
y = 10             (* 10, y est une variable globale *)
Compter[5]         (* 0 1 2 3 4 *)
y                  (* 5, y a été modifiée par Compter *)
x = 20             (* 20, x est une variable globale *)
Compter[5]         (* 0 1 2 3 4 *)
x                  (* 20, dans Compter, le paramètre x masque la variable
                      globale x *)

(* La fonction Module permet d'utiliser des variables locales *)
MieuxCompter[x_] := Module[{y}, (For[y=0, y<x, y++, (Print@y)])]
y = 20             (* y est une variable globale *)
MieuxCompter[5]    (* 0 1 2 3 4 *)
y                  (* 20, y n'a pas été modifiée car le y du Module masque le
                          y global. C'est bien mieux comme ça ! *)

(* Module permet de faire des déclarations globales aussi *)
Module[{compte}, compte=0;      (* compte est une variable locale *)
  (Incrementer[] := ++compte);  (* Ce module déclare des fonctions, mais elles
                                   ne sont globales. Elles ont cependant accès
                                   aux variables locales au module. *)
  (Decrementer[] := --compte)]
compte             (* compte, car il n'y a pas de variable globale nommée
                      compte *)
Incrementer[]      (* 1, la fonction utilise la variable compte du module *)
Incrementer[]      (* 2, le précédent appel de Incrementer a modifié compte *)
Decrementer[]      (* 1 *)
compte             (* compte, car il n'existe toujours pas de variable globale
                      nommé compte *)

(* Listes *)
liste = {1, 2, 3, 4}     (* {1, 2, 3, 4} *)
liste[[1]]               (* 1, les indexes commencent à 1 et non 0 !!! *)
Map[Double, liste]       (* {2, 4, 6, 8}, appliquer une fonction à une liste de
                             manière fonctionnelle *)
Double /@ liste          (* {2, 4, 6, 8}, syntaxe abrégée de la ligne
                             précédente *)
Scan[Print, liste]       (* 1 2 3 4, boucle impérative sur une liste *)
Fold[Plus, 0, liste]     (* 10 (0+1+2+3+4) *)
FoldList[Plus, 0, liste] (* {0, 1, 3, 6, 10}, variante de la fonction
                             précédente qui donne aussi les résultats
                             intermédiaires *)
Append[liste, 5]         (* {1, 2, 3, 4, 5}, liste n'est pas modifiée... *)
Prepend[liste, 5]        (* {5, 1, 2, 3, 4}, ... mais elle peut l'être en 
                             écrivant "liste = " *)
Join[liste, {3, 4}]      (* {1, 2, 3, 4, 3, 4} *)
liste[[2]] = 5           (* {1, 5, 3, 4}, ceci modifie bien la liste *)

(* Tables associatives, ou dictionnaires *)
table = <|"Vert" -> 2, "Rouge" -> 1|> (* Crée une table associative *)
table[["Vert"]]                       (* 2, l'utilise *)
table[["Vert"]] := 5                  (* 5, la modifie *)
table[["Bleu"]] := 3.5                (* 3.5, l'étend *)
KeyDropFrom[table, "Vert"]            (* Supprime la clé "Vert" *)
Keys[table]                           (* {Rouge, Bleu} *)
Values[table]                         (* {1, 3.5} *)

(* Pour finir, toute bonne démonstration du langage Wolfram contient un
   Manipulate ! *)
Manipulate[y^2, {y, 0, 20}] (* Crée une interface graphique interactive qui
                               affiche y^2, permettant à l'utilisateur de
                               modifier la valeur de y grâce à un contrôle
                               allant de 0 à 20. Ne fonctionne que si le
                               logiciel utilisé gère les éléments graphiques. *)
```

## Envie d'aller plus loin ?

* [Documentation du langage Wolfram (en anglais)]
(http://reference.wolfram.com/language/)
---
language: xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Geoffrey Liu", "https://github.com/g-liu"]
filename: learnxml-fr.xml
lang: fr-fr
---

XML est un langage de balisage conçu pour stocker et transporter les informations.

Contrairement à HTML, XML ne spécifie pas comment afficher ou formater les informations, juste comment les porter.

* La syntaxe XML

```xml
<!-- Les commentaires en XML ressemblent ceci -->

<?xml version="1.0" encoding="UTF-8"?>
<librairie>
  <livre categorie="CUISINE">
    <titre lang="en">Everyday Italian</titre>
    <auteur>Giada De Laurentiis</auteur>
    <an>2005</an>
    <prix>30.00</prix>
  </livre>
  <livre categorie="ENFANTS">
    <titre lang="en">Harry Potter</titre>
    <auteur>J. K. Rowling</auteur>
    <an>2005</an>
    <prix>29.99</prix>
  </livre>
  <livre categorie="WEB">
    <titre lang="en">Learning XML</titre>
    <auteur>Erik T. Ray</auteur>
    <an>2003</an>
    <prix>39.95</prix>
  </livre>
</librairie>

<!-- Ce qui suit est un fichier XML typique.
  Il commence par une déclaration, qui informe certaines métadonnées (en option).

XML utilise une structure arborescente. Ci-dessus, le nœud racine est «librairie», qui a 
   trois nœuds enfants, qui sont appelés «livres». Ces nœuds ont plus de nœuds enfants, et ainsi de suite ...

On crée les nœuds avec des balises d'ouverture / fermeture, et les enfants sont les nœuds juste entre 
   les balises d'ouverture et de fermeture. -->


<!-- XML porte deux types d'informations:
  1 - Les attributs -> les métadonnées sur un nœud.
      Habituellement, l'analyseur XML utilise cette information pour bien stocker les données.
  2 - Les éléments -> les informations pures.
      C'est ce que l'analyseur retrouvera du fichier XML.
      Les éléments apparaissent entre les balises d'ouverture et de fermeture, sans parenthèses. -->
      
  
<!-- Ci-dessous, un élément avec deux attributs -->
<fichier type="gif" id="4293">ordinateur.gif</fichier>


```

* Un document bien formaté & le validation

Un document XML est bien formaté s'il est syntaxiquement correct. 
Cependant, il est possible d'injecter plus de contraintes dans le document, 
en utilisant les définitions de documents, tels que les schémas DTD et XML.

Un document XML qui suit une définition de document est dit valide, 
en ce qui concerne ce document.

Avec cet outil, vous pouvez vérifier les données XML en dehors de la logique de l'application.

```xml

<!-- Ci-dessous, vous pouvez voir une version simplifiée du document de librairie, 
   avec l'addition de définition DTD. -->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Librairie.dtd">
<librairie>
  <livre categorie="CUISINE">
    <titre>Everyday Italian</titre>
    <prix>30.00</prix>
  </livre>
</librairie>

<!-- Cette DTD pourrait être quelque chose comme: -->

<!DOCTYPE note
[
<!ELEMENT librairie (livre+)>
<!ELEMENT livre (titre,prix)>
<!ATTLIST livre categorie CDATA "Littérature">
<!ELEMENT titre (#PCDATA)>
<!ELEMENT prix (#PCDATA)>
]>

<!-- La DTD commence par une déclaration.
   Après, le nœud racine est déclaré, qui exige un ou plusieurs nœuds enfants. 
   Chaque «livre» doit contenir exactement un «titre» et «prix» et un attribut 
   appelé «catégorie», avec «littérature» comme valeur par défaut. 
   Les nœuds de «titre» et «prix» contiennent des informations de caractère analysés
   (Anglais: «parsed character data») -->

<!-- La DTD pourrait être déclarée dans le fichier XML lui-même -->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT librairie (livre+)>
<!ELEMENT livre (titre,prix)>
<!ATTLIST livre categorie CDATA "Littérature">
<!ELEMENT titre (#PCDATA)>
<!ELEMENT prix (#PCDATA)>
]>

<librairie>
  <livre categorie="CUISINE">
    <titre>Everyday Italian</titre>
    <prix>30.00</prix>
  </livre>
</librairie>
```
---
language: yaml
filename: learnyaml-fr.yaml
contributors:
  - ["Andrei Curelaru", "http://www.infinidad.fr"]
lang: fr-fr
---

Proposé à l'origine par Clark Evans en Mai 2001, YAML est un un format de 
représentation de données par sérialisation, conçu pour être aisément 
modifiable et lisible par nous-mêmes, les humains.

YAML est plus concis que le XML auquel il est parfois comparé par ceux qui le
découvre, plus lisible et clair que le CSV, et emprunte beaucoup au JSON dont
il est un parent naturel. Toutefois, YAML emprunte également des idées et
concepts de Python, et s'intègre bien avec bon nombre de langages.
Contrairement à ce dernier, YAML interdit l'utilisation des tabulations.


```yaml
# Les commentaires sont précédés d'un signe "#", comme cette ligne.

#############
# SCALAIRES #
#############

# Les scalaires sont l'ensemble des types YAML qui ne sont pas des collections
# (listes ou tableaux associatifs).

# Notre objet root (racine), sera une map (carte) et englobera
# l'intégralité du document. Cette map est l'équivalent d'un dictionnaire,
# hash ou objet dans d'autres langages.
clé: valeur
autre_clé: une autre valeur
valeur_numérique: 100
notation_scientifique: 1e+12
booléen: true
valeur_null: null
clé avec espaces: valeur
# Bien qu'il ne soit pas nécessaire de mettre les chaînes de caractères
# entre guillemets, cela reste possible, et parfois utile.
toutefois: "Une chaîne, peut être contenue entre guillemets."
"Une clé entre guillemets.": "Utile si l'on veut utiliser ':' dans la clé."

# Les chaînes couvrant plusieurs lignes, peuvent être écrites au choix,
# comme un "bloc littéral" (avec '|') ou bien un "bloc replié" (avec '>').
bloc_littéral: |
    Tout ce bloc de texte sera la valeur de la clé "bloc_littéral", 
    avec préservation des retours à la ligne.

    Le littéral continue jusqu'à ce que l'indentation soit annulée.

        Toutes lignes qui seraient "davantage indentées" conservent leur
        indentation, constituée de 4 espaces.
bloc_replié: >
    Tout ce bloc de texte sera la valeur de la clé "bloc_replié", mais 
    cette fois-ci, toutes les nouvelles lignes deviendront un simple espace.

    Les lignes vides, comme ci-dessus, seront converties en caractère de
    nouvelle ligne.

        Les lignes "plus-indentées" gardent leurs retours à la ligne -
        ce texte apparaîtra sur deux lignes.

###############
# COLLECTIONS #
###############

# L'imbrication est créée par indentation.
une_map_imbriquée:
    clé: valeur
    autre_clé: autre valeur
    autre_map_imbriquée:
        bonjour: bonjour

# Les clés des maps ne sont pas nécessairement des chaînes de caractères.
0.25: une clé de type flottant

# Les clés peuvent également être des objets s'étendant sur plusieurs lignes,
# en utilisant le signe "?" pour indiquer le début de la clé.
? |
    ceci est une clé
    sur de multiples lignes
: et ceci est sa valeur

# YAML autorise aussi l'usage des collections à l'intérieur des clés,
# mais certains langages de programmation ne le tolère pas si bien.

# Les séquences (équivalent des listes ou tableaux) ressemblent à cela :
une_séquence:
    - Objet 1
    - Objet 2
    - 0.5 # les séquences peuvent contenir des types variés.
    - Objet 4
    - clé: valeur
      autre_clé: autre_valeur
    -
        - Ceci est une séquence
        - dans une autre séquence

# YAML étant un proche parent de JSON, vous pouvez écrire directement
# des maps et séquences façon JSON
json_map: {"clé": "valeur"}
json_seq: [1, 2, 3, "soleil"]

################################
# AUTRES FONCTIONNALITÉES YAML #
################################

# YAML possède une fonctionnalité fort utile nommée "ancres". Celle-ci
# vous permet de dupliquer aisément du contenu au sein de votre document.

# Les deux clés suivantes auront la même valeur :
contenu_ancré: &nom_ancre Cette chaîne sera la valeur des deux clés.
autre_ancre: *nom_ancre

# Avec les tags YAML, vous pouvez explicitement déclarer des types de données.
chaine_explicite: !!str 0.5

# Certains analyseurs syntaxiques (parsers) implémentent des tags spécifiques à
# d'autres langages, comme par exemple celui des nombres complexes de Python.
python_complex_number: !!python/complex 1+2j

#####################
# AUTRES TYPES YAML #
#####################

# YAML interprète également les données formatées ISO de type date et datetime,
# pas seulement les chaînes et nombres.
datetime: 2001-12-15T02:59:43.1Z
datetime_avec_espaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14

# Le tag !!binary indique que la chaîne à suivre est la représentation binaire
# d'un blob encodé en base64. En clair ? Une image !
fichier_gif: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML a de même un type "set", semblable à ceci :
set:
    ? item1
    ? item2
    ? item3

# Comme dans Python, les sets ne sont que des maps contenant des valeurs null ;
# le set précédent est l'équivalent du suivant :
set2:
    item1: null
    item2: null
    item3: null

```

Quelques références et outils :

- Documentation officielle [YAML 1.2](http://www.yaml.org/spec/1.2/spec.html) *anglais*,
- Une [Introduction à YAML](http://sweetohm.net/html/introduction-yaml.html) très bien construite et claire,
- Un outil pour tester [en ligne](http://yaml-online-parser.appspot.com/) la syntaxe YAML, avec des exemples.
---
language: F#
contributors:
    - ["Scott Wlaschin", "http://fsharpforfunandprofit.com/"]
filename: learnfsharp.fs
---

F# is a general purpose functional/OO programming language.  It's free and open source, and runs on Linux, Mac, Windows and more.

It has a powerful type system that traps many errors at compile time, but it uses type inference so that it reads more like a dynamic language.

The syntax of F# is different from C-style languages:

* Curly braces are not used to delimit blocks of code. Instead, indentation is used (like Python).
* Whitespace is used to separate parameters rather than commas.

If you want to try out the code below, you can go to [tryfsharp.org](http://www.tryfsharp.org/Create) and paste it into an interactive REPL.

```csharp

// single line comments use a double slash
(* multi line comments use (* . . . *) pair

-end of multi line comment- *)

// ================================================
// Basic Syntax
// ================================================

// ------ "Variables" (but not really) ------
// The "let" keyword defines an (immutable) value
let myInt = 5
let myFloat = 3.14
let myString = "hello"           // note that no types needed

// ------ Lists ------
let twoToFive = [2; 3; 4; 5]        // Square brackets create a list with
                                 // semicolon delimiters.
let oneToFive = 1 :: twoToFive   // :: creates list with new 1st element
// The result is [1; 2; 3; 4; 5]
let zeroToFive = [0; 1] @ twoToFive   // @ concats two lists

// IMPORTANT: commas are never used as delimiters, only semicolons!

// ------ Functions ------
// The "let" keyword also defines a named function.
let square x = x * x          // Note that no parens are used.
square 3                      // Now run the function. Again, no parens.

let add x y = x + y           // don't use add (x,y)! It means something
                              // completely different.
add 2 3                       // Now run the function.

// to define a multiline function, just use indents. No semicolons needed.
let evens list =
   let isEven x = x % 2 = 0     // Define "isEven" as a sub function
   List.filter isEven list    // List.filter is a library function
                              // with two parameters: a boolean function
                              // and a list to work on

evens oneToFive               // Now run the function

// You can use parens to clarify precedence. In this example,
// do "map" first, with two args, then do "sum" on the result.
// Without the parens, "List.map" would be passed as an arg to List.sum
let sumOfSquaresTo100 =
   List.sum ( List.map square [1..100] )

// You can pipe the output of one operation to the next using "|>"
// Piping data around is very common in F#, similar to UNIX pipes.

// Here is the same sumOfSquares function written using pipes
let sumOfSquaresTo100piped =
   [1..100] |> List.map square |> List.sum  // "square" was defined earlier

// you can define lambdas (anonymous functions) using the "fun" keyword
let sumOfSquaresTo100withFun =
   [1..100] |> List.map (fun x -> x * x) |> List.sum

// In F# there is no "return" keyword. A function always
// returns the value of the last expression used.

// ------ Pattern Matching ------
// Match..with.. is a supercharged case/switch statement.
let simplePatternMatch =
   let x = "a"
   match x with
    | "a" -> printfn "x is a"
    | "b" -> printfn "x is b"
    | _ -> printfn "x is something else"   // underscore matches anything

// F# doesn't allow nulls by default -- you must use an Option type
// and then pattern match.
// Some(..) and None are roughly analogous to Nullable wrappers
let validValue = Some(99)
let invalidValue = None

// In this example, match..with matches the "Some" and the "None",
// and also unpacks the value in the "Some" at the same time.
let optionPatternMatch input =
   match input with
    | Some i -> printfn "input is an int=%d" i
    | None -> printfn "input is missing"

optionPatternMatch validValue
optionPatternMatch invalidValue

// ------ Printing ------
// The printf/printfn functions are similar to the
// Console.Write/WriteLine functions in C#.
printfn "Printing an int %i, a float %f, a bool %b" 1 2.0 true
printfn "A string %s, and something generic %A" "hello" [1; 2; 3; 4]

// There are also sprintf/sprintfn functions for formatting data
// into a string, similar to String.Format in C#.

// ================================================
// More on functions
// ================================================

// F# is a true functional language -- functions are first
// class entities and can be combined easily to make powerful
// constructs

// Modules are used to group functions together
// Indentation is needed for each nested module.
module FunctionExamples =

    // define a simple adding function
    let add x y = x + y

    // basic usage of a function
    let a = add 1 2
    printfn "1 + 2 = %i" a

    // partial application to "bake in" parameters
    let add42 = add 42
    let b = add42 1
    printfn "42 + 1 = %i" b

    // composition to combine functions
    let add1 = add 1
    let add2 = add 2
    let add3 = add1 >> add2
    let c = add3 7
    printfn "3 + 7 = %i" c

    // higher order functions
    [1..10] |> List.map add3 |> printfn "new list is %A"

    // lists of functions, and more
    let add6 = [add1; add2; add3] |> List.reduce (>>)
    let d = add6 7
    printfn "1 + 2 + 3 + 7 = %i" d

// ================================================
// Lists and collection
// ================================================

// There are three types of ordered collection:
// * Lists are most basic immutable collection.
// * Arrays are mutable and more efficient when needed.
// * Sequences are lazy and infinite (e.g. an enumerator).
//
// Other collections include immutable maps and sets
// plus all the standard .NET collections

module ListExamples =

    // lists use square brackets
    let list1 = ["a"; "b"]
    let list2 = "c" :: list1    // :: is prepending
    let list3 = list1 @ list2   // @ is concat

    // list comprehensions (aka generators)
    let squares = [for i in 1..10 do yield i * i]

    // A prime number generator
    // - this is using a short notation for the pattern matching syntax
    // - (p::xs) is 'first :: tail' of the list, could also be written as p :: xs
    //   this means this matches 'p' (the first item in the list), and xs is the rest of the list
    //   this is called the 'cons pattern'
    // - uses 'rec' keyword, which is necessary when using recursion
    let rec sieve = function
        | (p::xs) -> p :: sieve [ for x in xs do if x % p > 0 then yield x ]
        | []      -> []
    let primes = sieve [2..50]
    printfn "%A" primes

    // pattern matching for lists
    let listMatcher aList =
        match aList with
        | [] -> printfn "the list is empty"
        | [first] -> printfn "the list has one element %A " first
        | [first; second] -> printfn "list is %A and %A" first second
        | _ -> printfn "the list has more than two elements"

    listMatcher [1; 2; 3; 4]
    listMatcher [1; 2]
    listMatcher [1]
    listMatcher []

    // recursion using lists
    let rec sum aList =
        match aList with
        | [] -> 0
        | x::xs -> x + sum xs
    sum [1..10]

    // -----------------------------------------
    // Standard library functions
    // -----------------------------------------

    // map
    let add3 x = x + 3
    [1..10] |> List.map add3

    // filter
    let even x = x % 2 = 0
    [1..10] |> List.filter even

    // many more -- see documentation

module ArrayExamples =

    // arrays use square brackets with bar
    let array1 = [| "a"; "b" |]
    let first = array1.[0]        // indexed access using dot

    // pattern matching for arrays is same as for lists
    let arrayMatcher aList =
        match aList with
        | [| |] -> printfn "the array is empty"
        | [| first |] -> printfn "the array has one element %A " first
        | [| first; second |] -> printfn "array is %A and %A" first second
        | _ -> printfn "the array has more than two elements"

    arrayMatcher [| 1; 2; 3; 4 |]

    // Standard library functions just as for List

    [| 1..10 |]
    |> Array.map (fun i -> i + 3)
    |> Array.filter (fun i -> i % 2 = 0)
    |> Array.iter (printfn "value is %i. ")


module SequenceExamples =

    // sequences use curly braces
    let seq1 = seq { yield "a"; yield "b" }

    // sequences can use yield and
    // can contain subsequences
    let strange = seq {
        // "yield" adds one element
        yield 1; yield 2;

        // "yield!" adds a whole subsequence
        yield! [5..10]
        yield! seq {
            for i in 1..10 do
              if i % 2 = 0 then yield i }}
    // test
    strange |> Seq.toList


    // Sequences can be created using "unfold"
    // Here's the fibonacci series
    let fib = Seq.unfold (fun (fst,snd) ->
        Some(fst + snd, (snd, fst + snd))) (0,1)

    // test
    let fib10 = fib |> Seq.take 10 |> Seq.toList
    printf "first 10 fibs are %A" fib10


// ================================================
// Data Types
// ================================================

module DataTypeExamples =

    // All data is immutable by default

    // Tuples are quick 'n easy anonymous types
    // -- Use a comma to create a tuple
    let twoTuple = 1, 2
    let threeTuple = "a", 2, true

    // Pattern match to unpack
    let x, y = twoTuple  // sets x = 1, y = 2

    // ------------------------------------
    // Record types have named fields
    // ------------------------------------

    // Use "type" with curly braces to define a record type
    type Person = {First:string; Last:string}

    // Use "let" with curly braces to create a record
    let person1 = {First="John"; Last="Doe"}

    // Pattern match to unpack
    let {First = first} = person1    // sets first="John"

    // ------------------------------------
    // Union types (aka variants) have a set of choices
    // Only case can be valid at a time.
    // ------------------------------------

    // Use "type" with bar/pipe to define a union type
    type Temp =
        | DegreesC of float
        | DegreesF of float

    // Use one of the cases to create one
    let temp1 = DegreesF 98.6
    let temp2 = DegreesC 37.0

    // Pattern match on all cases to unpack
    let printTemp = function
       | DegreesC t -> printfn "%f degC" t
       | DegreesF t -> printfn "%f degF" t

    printTemp temp1
    printTemp temp2

    // ------------------------------------
    // Recursive types
    // ------------------------------------

    // Types can be combined recursively in complex ways
    // without having to create subclasses
    type Employee =
      | Worker of Person
      | Manager of Employee list

    let jdoe = {First="John"; Last="Doe"}
    let worker = Worker jdoe

    // ------------------------------------
    // Modeling with types
    // ------------------------------------

    // Union types are great for modeling state without using flags
    type EmailAddress =
        | ValidEmailAddress of string
        | InvalidEmailAddress of string

    let trySendEmail email =
        match email with // use pattern matching
        | ValidEmailAddress address -> ()   // send
        | InvalidEmailAddress address -> () // don't send

    // The combination of union types and record types together
    // provide a great foundation for domain driven design.
    // You can create hundreds of little types that accurately
    // reflect the domain.

    type CartItem = { ProductCode: string; Qty: int }
    type Payment = Payment of float
    type ActiveCartData = { UnpaidItems: CartItem list }
    type PaidCartData = { PaidItems: CartItem list; Payment: Payment}

    type ShoppingCart =
        | EmptyCart  // no data
        | ActiveCart of ActiveCartData
        | PaidCart of PaidCartData

    // ------------------------------------
    // Built in behavior for types
    // ------------------------------------

    // Core types have useful "out-of-the-box" behavior, no coding needed.
    // * Immutability
    // * Pretty printing when debugging
    // * Equality and comparison
    // * Serialization

    // Pretty printing using %A
    printfn "twoTuple=%A,\nPerson=%A,\nTemp=%A,\nEmployee=%A"
             twoTuple person1 temp1 worker

    // Equality and comparison built in.
    // Here's an example with cards.
    type Suit = Club | Diamond | Spade | Heart
    type Rank = Two | Three | Four | Five | Six | Seven | Eight
                | Nine | Ten | Jack | Queen | King | Ace

    let hand = [ Club, Ace; Heart, Three; Heart, Ace;
                 Spade, Jack; Diamond, Two; Diamond, Ace ]

    // sorting
    List.sort hand |> printfn "sorted hand is (low to high) %A"
    List.max hand |> printfn "high card is %A"
    List.min hand |> printfn "low card is %A"


// ================================================
// Active patterns
// ================================================

module ActivePatternExamples =

    // F# has a special type of pattern matching called "active patterns"
    // where the pattern can be parsed or detected dynamically.

    // "banana clips" are the syntax for active patterns

    // You can use "elif" instead of "else if" in conditional expressions.
    // They are equivalent in F#

    // for example, define an "active" pattern to match character types...
    let (|Digit|Letter|Whitespace|Other|) ch =
       if System.Char.IsDigit(ch) then Digit
       elif System.Char.IsLetter(ch) then Letter
       elif System.Char.IsWhiteSpace(ch) then Whitespace
       else Other

    // ... and then use it to make parsing logic much clearer
    let printChar ch =
      match ch with
      | Digit -> printfn "%c is a Digit" ch
      | Letter -> printfn "%c is a Letter" ch
      | Whitespace -> printfn "%c is a Whitespace" ch
      | _ -> printfn "%c is something else" ch

    // print a list
    ['a'; 'b'; '1'; ' '; '-'; 'c'] |> List.iter printChar

    // -----------------------------------
    // FizzBuzz using active patterns
    // -----------------------------------

    // You can create partial matching patterns as well
    // Just use underscore in the definition, and return Some if matched.
    let (|MultOf3|_|) i = if i % 3 = 0 then Some MultOf3 else None
    let (|MultOf5|_|) i = if i % 5 = 0 then Some MultOf5 else None

    // the main function
    let fizzBuzz i =
      match i with
      | MultOf3 & MultOf5 -> printf "FizzBuzz, "
      | MultOf3 -> printf "Fizz, "
      | MultOf5 -> printf "Buzz, "
      | _ -> printf "%i, " i

    // test
    [1..20] |> List.iter fizzBuzz

// ================================================
// Conciseness
// ================================================

module AlgorithmExamples =

    // F# has a high signal/noise ratio, so code reads
    // almost like the actual algorithm

    // ------ Example: define sumOfSquares function ------
    let sumOfSquares n =
       [1..n]              // 1) take all the numbers from 1 to n
       |> List.map square  // 2) square each one
       |> List.sum         // 3) sum the results

    // test
    sumOfSquares 100 |> printfn "Sum of squares = %A"

    // ------ Example: define a sort function ------
    let rec sort list =
       match list with
       // If the list is empty
       | [] ->
            []                            // return an empty list
       // If the list is not empty
       | firstElem::otherElements ->      // take the first element
            let smallerElements =         // extract the smaller elements
                otherElements             // from the remaining ones
                |> List.filter (fun e -> e < firstElem)
                |> sort                   // and sort them
            let largerElements =          // extract the larger ones
                otherElements             // from the remaining ones
                |> List.filter (fun e -> e >= firstElem)
                |> sort                   // and sort them
            // Combine the 3 parts into a new list and return it
            List.concat [smallerElements; [firstElem]; largerElements]

    // test
    sort [1; 5; 23; 18; 9; 1; 3] |> printfn "Sorted = %A"

// ================================================
// Asynchronous Code
// ================================================

module AsyncExample =

    // F# has built-in features to help with async code
    // without encountering the "pyramid of doom"
    //
    // The following example downloads a set of web pages in parallel.

    open System.Net
    open System
    open System.IO
    open Microsoft.FSharp.Control.CommonExtensions

    // Fetch the contents of a URL asynchronously
    let fetchUrlAsync url =
        async {   // "async" keyword and curly braces
                  // creates an "async" object
            let req = WebRequest.Create(Uri(url))
            use! resp = req.AsyncGetResponse()
                // use! is async assignment
            use stream = resp.GetResponseStream()
                // "use" triggers automatic close()
                // on resource at end of scope
            use reader = new IO.StreamReader(stream)
            let html = reader.ReadToEnd()
            printfn "finished downloading %s" url
            }

    // a list of sites to fetch
    let sites = ["http://www.bing.com";
                 "http://www.google.com";
                 "http://www.microsoft.com";
                 "http://www.amazon.com";
                 "http://www.yahoo.com"]

    // do it
    sites
    |> List.map fetchUrlAsync  // make a list of async tasks
    |> Async.Parallel          // set up the tasks to run in parallel
    |> Async.RunSynchronously  // start them off

// ================================================
// .NET compatibility
// ================================================

module NetCompatibilityExamples =

    // F# can do almost everything C# can do, and it integrates
    // seamlessly with .NET or Mono libraries.

    // ------- work with existing library functions  -------

    let (i1success, i1) = System.Int32.TryParse("123");
    if i1success then printfn "parsed as %i" i1 else printfn "parse failed"

    // ------- Implement interfaces on the fly! -------

    // create a new object that implements IDisposable
    let makeResource name =
       { new System.IDisposable
         with member this.Dispose() = printfn "%s disposed" name }

    let useAndDisposeResources =
        use r1 = makeResource "first resource"
        printfn "using first resource"
        for i in [1..3] do
            let resourceName = sprintf "\tinner resource %d" i
            use temp = makeResource resourceName
            printfn "\tdo something with %s" resourceName
        use r2 = makeResource "second resource"
        printfn "using second resource"
        printfn "done."

    // ------- Object oriented code -------

    // F# is also a fully fledged OO language.
    // It supports classes, inheritance, virtual methods, etc.

    // interface with generic type
    type IEnumerator<'a> =
        abstract member Current : 'a
        abstract MoveNext : unit -> bool

    // abstract base class with virtual methods
    [<AbstractClass>]
    type Shape() =
        // readonly properties
        abstract member Width : int with get
        abstract member Height : int with get
        // non-virtual method
        member this.BoundingArea = this.Height * this.Width
        // virtual method with base implementation
        abstract member Print : unit -> unit
        default this.Print () = printfn "I'm a shape"

    // concrete class that inherits from base class and overrides
    type Rectangle(x:int, y:int) =
        inherit Shape()
        override this.Width = x
        override this.Height = y
        override this.Print ()  = printfn "I'm a Rectangle"

    // test
    let r = Rectangle(2, 3)
    printfn "The width is %i" r.Width
    printfn "The area is %i" r.BoundingArea
    r.Print()

    // ------- extension methods  -------

    // Just as in C#, F# can extend existing classes with extension methods.
    type System.String with
       member this.StartsWithA = this.StartsWith "A"

    // test
    let s = "Alice"
    printfn "'%s' starts with an 'A' = %A" s s.StartsWithA

    // ------- events  -------

    type MyButton() =
        let clickEvent = new Event<_>()

        [<CLIEvent>]
        member this.OnClick = clickEvent.Publish

        member this.TestEvent(arg) =
            clickEvent.Trigger(this, arg)

    // test
    let myButton = new MyButton()
    myButton.OnClick.Add(fun (sender, arg) ->
            printfn "Click event with arg=%O" arg)

    myButton.TestEvent("Hello World!")

```

## More Information

For more demonstrations of F#, go to the [Try F#](http://www.tryfsharp.org/Learn) site, or my [why use F#](http://fsharpforfunandprofit.com/why-use-fsharp/) series.

Read more about F# at [fsharp.org](http://fsharp.org/).
---
category: tool
tool: git
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Leo Rudberg" , "http://github.com/LOZORD"]
    - ["Betsy Lorton" , "http://github.com/schbetsy"]
    - ["Bruno Volcov", "http://github.com/volcov"]
    - ["Andrew Taylor", "http://github.com/andrewjt71"]
    - ["Jason Stathopulos", "http://github.com/SpiritBreaker226"]
    - ["Milo Gilad", "http://github.com/Myl0g"]
filename: LearnGit.txt
---

Git is a distributed version control and source code management system.

It does this through a series of snapshots of your project, and it works
with those snapshots to provide you with functionality to version and
manage your source code.

## Versioning Concepts

### What is version control?

Version control is a system that records changes to a file(s), over time.

### Centralized Versioning vs. Distributed Versioning

* Centralized version control focuses on synchronizing, tracking, and backing 
up files.
* Distributed version control focuses on sharing changes. Every change has a 
unique id.
* Distributed systems have no defined structure. You could easily have a SVN 
style, centralized system, with git.

[Additional Information](http://git-scm.com/book/en/Getting-Started-About-Version-Control)

### Why Use Git?

* Can work offline.
* Collaborating with others is easy!
* Branching is easy!
* Branching is fast!
* Merging is easy!
* Git is fast.
* Git is flexible.

## Git Architecture

### Repository

A set of files, directories, historical records, commits, and heads. Imagine it
as a source code data structure, with the attribute that each source code
"element" gives you access to its revision history, among other things.

A git repository is comprised of the .git directory & working tree.

### .git Directory (component of repository)

The .git directory contains all the configurations, logs, branches, HEAD, and 
more.
[Detailed List.](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### Working Tree (component of repository)

This is basically the directories and files in your repository. It is often
referred to as your working directory.

### Index (component of .git dir)

The Index is the staging area in git. It's basically a layer that separates 
your working tree from the Git repository. This gives developers more power 
over what gets sent to the Git repository.

### Commit

A git commit is a snapshot of a set of changes, or manipulations to your 
Working Tree. For example, if you added 5 files, and removed 2 others, these 
changes will be contained in a commit (or snapshot). This commit can then be 
pushed to other repositories, or not!

### Branch

A branch is essentially a pointer to the last commit you made. As you go on
committing, this pointer will automatically update to point the latest commit.

### Tag

A tag is a mark on specific point in history. Typically people use this
functionality to mark release points (v1.0, and so on)

### HEAD and head (component of .git dir)

HEAD is a pointer that points to the current branch. A repository only has 1 
*active* HEAD.  
head is a pointer that points to any commit. A repository can have any number 
of heads.

### Stages of Git
* Modified - Changes have been made to a file but file has not been committed 
to Git Database yet
* Staged - Marks a modified file to go into your next commit snapshot
* Committed - Files have been committed to the Git Database

### Conceptual Resources

* [Git For Computer Scientists](http://eagain.net/articles/git-for-computer-scientists/)
* [Git For Designers](http://hoth.entp.com/output/git_for_designers.html)

## Commands

### init

Create an empty Git repository. The Git repository's settings, stored 
information, and more is stored in a directory (a folder) named ".git".

```bash
$ git init
```

### config

To configure settings. Whether it be for the repository, the system itself,
or global configurations ( global config file is `~/.gitconfig` ).

```bash
# Print & Set Some Basic Config Variables (Global)
$ git config --global user.email "MyEmail@Zoho.com"
$ git config --global user.name "My Name"
```

[Learn More About git config.](http://git-scm.com/docs/git-config)

### help

To give you quick access to an extremely detailed guide of each command. Or to
just give you a quick reminder of some semantics.

```bash
# Quickly check available commands
$ git help

# Check all available commands
$ git help -a

# Command specific help - user manual
# git help <command_here>
$ git help add
$ git help commit
$ git help init
# or git <command_here> --help
$ git add --help
$ git commit --help
$ git init --help
```

### ignore files

To intentionally untrack file(s) & folder(s) from git. Typically meant for
private & temp files which would otherwise be shared in the repository.

```bash
$ echo "temp/" >> .gitignore
$ echo "private_key" >> .gitignore
```

### status

To show differences between the index file (basically your working copy/repo)
and the current HEAD commit.

```bash
# Will display the branch, untracked files, changes and other differences
$ git status

# To learn other "tid bits" about git status
$ git help status
```

### add

To add files to the staging area/index. If you do not `git add` new files to 
the staging area/index, they will not be included in commits!

```bash
# add a file in your current working directory
$ git add HelloWorld.java

# add a file in a nested dir
$ git add /path/to/file/HelloWorld.c

# Regular Expression support!
$ git add ./*.java

# You can also add everything in your working directory to the staging area.
$ git add -A
```

This only adds a file to the staging area/index, it doesn't commit it to the
working directory/repo.

### branch

Manage your branches. You can view, edit, create, delete branches using this 
command.

```bash
# list existing branches & remotes
$ git branch -a

# create a new branch
$ git branch myNewBranch

# delete a branch
$ git branch -d myBranch

# rename a branch
# git branch -m <oldname> <newname>
$ git branch -m myBranchName myNewBranchName

# edit a branch's description
$ git branch myBranchName --edit-description
```

### tag

Manage your tags

```bash
# List tags
$ git tag

# Create a annotated tag
# The -m specifies a tagging message, which is stored with the tag.
# If you don’t specify a message for an annotated tag,
# Git launches your editor so you can type it in.
$ git tag -a v2.0 -m 'my version 2.0'

# Show info about tag
# That shows the tagger information, the date the commit was tagged,
# and the annotation message before showing the commit information.
$ git show v2.0

# Push a single tag to remote
$ git push origin v2.0

# Push a lot of tags to remote
$ git push origin --tags
```

### checkout

Updates all files in the working tree to match the version in the index, or 
specified tree.

```bash
# Checkout a repo - defaults to master branch
$ git checkout

# Checkout a specified branch
$ git checkout branchName

# Create a new branch & switch to it
# equivalent to "git branch <name>; git checkout <name>"

$ git checkout -b newBranch
```

### clone

Clones, or copies, an existing repository into a new directory. It also adds
remote-tracking branches for each branch in the cloned repo, which allows you 
to push to a remote branch.

```bash
# Clone learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git

# shallow clone - faster cloning that pulls only latest snapshot
$ git clone --depth 1 https://github.com/adambard/learnxinyminutes-docs.git

# clone only a specific branch
$ git clone -b master-cn https://github.com/adambard/learnxinyminutes-docs.git --single-branch
```

### commit

Stores the current contents of the index in a new "commit." This commit 
contains the changes made and a message created by the user.

```bash
# commit with a message
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"

# automatically stage modified or deleted files, except new files, and then commit
$ git commit -a -m "Modified foo.php and removed bar.php"

# change last commit (this deletes previous commit with a fresh commit)
$ git commit --amend -m "Correct message"
```

### diff

Shows differences between a file in the working directory, index and commits.

```bash
# Show difference between your working dir and the index
$ git diff

# Show differences between the index and the most recent commit.
$ git diff --cached

# Show differences between your working dir and the most recent commit
$ git diff HEAD
```

### grep

Allows you to quickly search a repository.

Optional Configurations:

```bash
# Thanks to Travis Jeffery for these
# Set line numbers to be shown in grep search results
$ git config --global grep.lineNumber true

# Make search results more readable, including grouping
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Search for "variableName" in all java files
$ git grep 'variableName' -- '*.java'

# Search for a line that contains "arrayListName" and, "add" or "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
```

Google is your friend; for more examples
[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Display commits to the repository.

```bash
# Show all commits
$ git log

# Show only commit message & ref
$ git log --oneline

# Show merge commits only
$ git log --merges

# Show all commits represented by an ASCII graph
$ git log --graph
```

### merge

"Merge" in changes from external commits into the current branch.

```bash
# Merge the specified branch into the current.
$ git merge branchName

# Always generate a merge commit when merging
$ git merge --no-ff branchName
```

### mv

Rename or move a file

```bash
# Renaming a file
$ git mv HelloWorld.c HelloNewWorld.c

# Moving a file
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# Force rename or move
# "existingFile" already exists in the directory, will be overwritten
$ git mv -f myFile existingFile
```

### pull

Pulls from a repository and merges it with another branch.

```bash
# Update your local repo, by merging in new changes
# from the remote "origin" and "master" branch.
# git pull <remote> <branch>
$ git pull origin master

# By default, git pull will update your current branch
# by merging in new changes from its remote-tracking branch 
$ git pull

# Merge in changes from remote branch and rebase
# branch commits onto your local repo, like: "git fetch <remote> <branch>, git 
# rebase <remote>/<branch>"
$ git pull origin master --rebase
```

### push

Push and merge changes from a branch to a remote & branch.

```bash
# Push and merge changes from a local repo to a
# remote named "origin" and "master" branch.
# git push <remote> <branch>
$ git push origin master

# By default, git push will push and merge changes from
# the current branch to its remote-tracking branch 
$ git push

# To link up current local branch with a remote branch, add -u flag:
$ git push -u origin master
# Now, anytime you want to push from that same local branch, use shortcut:
$ git push
```

### stash

Stashing takes the dirty state of your working directory and saves it on a 
stack of unfinished changes that you can reapply at any time.

Let's say you've been doing some work in your git repo, but you want to pull
from the remote. Since you have dirty (uncommitted) changes to some files, you
are not able to run `git pull`. Instead, you can run `git stash` to save your
changes onto a stack!

```bash
$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 added the index file"
  HEAD is now at 049d078 added the index file
  (To restore them type "git stash apply")
```

Now you can pull!

```bash
git pull
```
`...changes apply...`

Now check that everything is OK

```bash
$ git status
# On branch master
nothing to commit, working directory clean
```

You can see what "hunks" you've stashed so far using `git stash list`.
Since the "hunks" are stored in a Last-In-First-Out stack, our most recent 
change will be at top.

```bash
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
```

Now let's apply our dirty changes back by popping them off the stack.

```bash
$ git stash pop
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   index.html
#      modified:   lib/simplegit.rb
#
```

`git stash apply` does the same thing

Now you're ready to get back to work on your stuff!

[Additional Reading.](http://git-scm.com/book/en/v1/Git-Tools-Stashing)

### rebase (caution)

Take all changes that were committed on one branch, and replay them onto 
another branch.
*Do not rebase commits that you have pushed to a public repo*.

```bash
# Rebase experimentBranch onto master
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch
```

[Additional Reading.](http://git-scm.com/book/en/Git-Branching-Rebasing)

### reset (caution)

Reset the current HEAD to the specified state. This allows you to undo merges,
pulls, commits, adds, and more. It's a great command but also dangerous if you 
don't know what you are doing.

```bash
# Reset the staging area, to match the latest commit (leaves dir unchanged)
$ git reset

# Reset the staging area, to match the latest commit, and overwrite working dir
$ git reset --hard

# Moves the current branch tip to the specified commit (leaves dir unchanged)
# all changes still exist in the directory.
$ git reset 31f2bb1

# Moves the current branch tip backward to the specified commit
# and makes the working dir match (deletes uncommitted changes and all commits
# after the specified commit).
$ git reset --hard 31f2bb1
```

### reflog (caution)

Reflog will list most of the git commands you have done for a given time period,
default 90 days.

This give you the chance to reverse any git commands that have gone wrong 
(for instance, if a rebase has broken your application).

You can do this:

1. `git reflog` to list all of the git commands for the rebase

```
38b323f HEAD@{0}: rebase -i (finish): returning to refs/heads/feature/add_git_reflog
38b323f HEAD@{1}: rebase -i (pick): Clarify inc/dec operators
4fff859 HEAD@{2}: rebase -i (pick): Update java.html.markdown
34ed963 HEAD@{3}: rebase -i (pick): [yaml/en] Add more resources (#1666)
ed8ddf2 HEAD@{4}: rebase -i (pick): pythonstatcomp spanish translation (#1748)
2e6c386 HEAD@{5}: rebase -i (start): checkout 02fb96d
```
2. Select where to reset to, in our case its `2e6c386`, or `HEAD@{5}`
3. 'git reset --hard HEAD@{5}' this will reset your repo to that head
4. You can start the rebase again or leave it alone.

[Additional Reading.](https://git-scm.com/docs/git-reflog)

### revert

Revert can be used to undo a commit. It should not be confused with reset which 
restores the state of a project to a previous point. Revert will add a new 
commit which is the inverse of the specified commit, thus reverting it.

```bash
# Revert a specified commit
$ git revert <commit>
```

### rm

The opposite of git add, git rm removes files from the current working tree.

```bash
# remove HelloWorld.c
$ git rm HelloWorld.c

# Remove a file from a nested dir
$ git rm /pather/to/the/file/HelloWorld.c
```

## Further Information

* [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1)

* [Learn Git Branching - the most visual and interactive way to learn Git on the web](http://learngitbranching.js.org/)

* [Udemy Git Tutorial: A Comprehensive Guide](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/)

* [Git Immersion - A Guided tour that walks through the fundamentals of git](http://gitimmersion.com/)

* [git-scm - Video Tutorials](http://git-scm.com/videos)

* [git-scm - Documentation](http://git-scm.com/docs)

* [Atlassian Git - Tutorials & Workflows](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet](http://res.cloudinary.com/hy4kyit2a/image/upload/SF_git_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)

* [Git - the simple guide](http://rogerdudler.github.io/git-guide/index.html)

* [Pro Git](http://www.git-scm.com/book/en/v2)

* [An introduction to Git and GitHub for Beginners (Tutorial)](http://product.hubspot.com/blog/git-and-github-tutorial-for-beginners)
---
name: Go
category: language
language: Go
filename: learngo.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
    - ["Alexej Friesen", "https://github.com/heyalexej"]
    - ["Clayton Walker", "https://github.com/cwalk"]
    - ["Leonid Shevtsov", "https://github.com/leonid-shevtsov"]
---

Go was created out of the need to get work done. It's not the latest trend
in computer science, but it is the newest fastest way to solve real-world
problems.

It has familiar concepts of imperative languages with static typing.
It's fast to compile and fast to execute, it adds easy-to-understand
concurrency to leverage today's multi-core CPUs, and has features to
help with large-scale programming.

Go comes with a great standard library and an enthusiastic community.

```go
// Single line comment
/* Multi-
 line comment */

// A package clause starts every source file.
// Main is a special name declaring an executable rather than a library.
package main

// Import declaration declares library packages referenced in this file.
import (
	"fmt"       // A package in the Go standard library.
	"io/ioutil" // Implements some I/O utility functions.
	m "math"    // Math library with local alias m.
	"net/http"  // Yes, a web server!
	"os"        // OS functions like working with the file system
	"strconv"   // String conversions.
)

// A function definition. Main is special. It is the entry point for the
// executable program. Love it or hate it, Go uses brace brackets.
func main() {
	// Println outputs a line to stdout.
	// Qualify it with the package name, fmt.
	fmt.Println("Hello world!")

	// Call another function within this package.
	beyondHello()
}

// Functions have parameters in parentheses.
// If there are no parameters, empty parentheses are still required.
func beyondHello() {
	var x int // Variable declaration. Variables must be declared before use.
	x = 3     // Variable assignment.
	// "Short" declarations use := to infer the type, declare, and assign.
	y := 4
	sum, prod := learnMultiple(x, y)        // Function returns two values.
	fmt.Println("sum:", sum, "prod:", prod) // Simple output.
	learnTypes()                            // < y minutes, learn more!
}

/* <- multiline comment
Functions can have parameters and (multiple!) return values.
Here `x`, `y` are the arguments and `sum`, `prod` is the signature (what's returned).
Note that `x` and `sum` receive the type `int`.
*/
func learnMultiple(x, y int) (sum, prod int) {
	return x + y, x * y // Return two values.
}

// Some built-in types and literals.
func learnTypes() {
	// Short declaration usually gives you what you want.
	str := "Learn Go!" // string type.

	s2 := `A "raw" string literal
can include line breaks.` // Same string type.

	// Non-ASCII literal. Go source is UTF-8.
	g := 'Σ' // rune type, an alias for int32, holds a unicode code point.

	f := 3.14195 // float64, an IEEE-754 64-bit floating point number.
	c := 3 + 4i  // complex128, represented internally with two float64's.

	// var syntax with initializers.
	var u uint = 7 // Unsigned, but implementation dependent size as with int.
	var pi float32 = 22. / 7

	// Conversion syntax with a short declaration.
	n := byte('\n') // byte is an alias for uint8.

	// Arrays have size fixed at compile time.
	var a4 [4]int           // An array of 4 ints, initialized to all 0.
	a5 := [...]int{3, 1, 5, 10, 100} // An array initialized with a fixed size of five
	// elements, with values 3, 1, 5, 10, and 100.

	// Slices have dynamic size. Arrays and slices each have advantages
	// but use cases for slices are much more common.
	s3 := []int{4, 5, 9}    // Compare to a5. No ellipsis here.
	s4 := make([]int, 4)    // Allocates slice of 4 ints, initialized to all 0.
	var d2 [][]float64      // Declaration only, nothing allocated here.
	bs := []byte("a slice") // Type conversion syntax.

	// Because they are dynamic, slices can be appended to on-demand.
	// To append elements to a slice, the built-in append() function is used.
	// First argument is a slice to which we are appending. Commonly,
	// the array variable is updated in place, as in example below.
	s := []int{1, 2, 3}		// Result is a slice of length 3.
	s = append(s, 4, 5, 6)	// Added 3 elements. Slice now has length of 6.
	fmt.Println(s) // Updated slice is now [1 2 3 4 5 6]

	// To append another slice, instead of list of atomic elements we can
	// pass a reference to a slice or a slice literal like this, with a
	// trailing ellipsis, meaning take a slice and unpack its elements,
	// appending them to slice s.
	s = append(s, []int{7, 8, 9}...) // Second argument is a slice literal.
	fmt.Println(s)	// Updated slice is now [1 2 3 4 5 6 7 8 9]

	p, q := learnMemory() // Declares p, q to be type pointer to int.
	fmt.Println(*p, *q)   // * follows a pointer. This prints two ints.

	// Maps are a dynamically growable associative array type, like the
	// hash or dictionary types of some other languages.
	m := map[string]int{"three": 3, "four": 4}
	m["one"] = 1

	// Unused variables are an error in Go.
	// The underscore lets you "use" a variable but discard its value.
	_, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a5, s4, bs
	// Usually you use it to ignore one of the return values of a function
	// For example, in a quick and dirty script you might ignore the
	// error value returned from os.Create, and expect that the file
	// will always be created.
	file, _ := os.Create("output.txt")
	fmt.Fprint(file, "This is how you write to a file, by the way")
	file.Close()
	
	// Output of course counts as using a variable.
	fmt.Println(s, c, a4, s3, d2, m)

	learnFlowControl() // Back in the flow.
}

// It is possible, unlike in many other languages for functions in go
// to have named return values.
// Assigning a name to the type being returned in the function declaration line
// allows us to easily return from multiple points in a function as well as to
// only use the return keyword, without anything further.
func learnNamedReturns(x, y int) (z int) {
	z = x * y
	return // z is implicit here, because we named it earlier.
}

// Go is fully garbage collected. It has pointers but no pointer arithmetic.
// You can make a mistake with a nil pointer, but not by incrementing a pointer.
func learnMemory() (p, q *int) {
	// Named return values p and q have type pointer to int.
	p = new(int) // Built-in function new allocates memory.
	// The allocated int is initialized to 0, p is no longer nil.
	s := make([]int, 20) // Allocate 20 ints as a single block of memory.
	s[3] = 7             // Assign one of them.
	r := -2              // Declare another local variable.
	return &s[3], &r     // & takes the address of an object.
}

func expensiveComputation() float64 {
	return m.Exp(10)
}

func learnFlowControl() {
	// If statements require brace brackets, and do not require parentheses.
	if true {
		fmt.Println("told ya")
	}
	// Formatting is standardized by the command line command "go fmt."
	if false {
		// Pout.
	} else {
		// Gloat.
	}
	// Use switch in preference to chained if statements.
	x := 42.0
	switch x {
	case 0:
	case 1:
	case 42:
		// Cases don't "fall through".
		/*
		There is a `fallthrough` keyword however, see:
		  https://github.com/golang/go/wiki/Switch#fall-through
		*/
	case 43:
		// Unreached.
	default:
		// Default case is optional.
	}
	// Like if, for doesn't use parens either.
	// Variables declared in for and if are local to their scope.
	for x := 0; x < 3; x++ { // ++ is a statement.
		fmt.Println("iteration", x)
	}
	// x == 42 here.

	// For is the only loop statement in Go, but it has alternate forms.
	for { // Infinite loop.
		break    // Just kidding.
		continue // Unreached.
	}

	// You can use range to iterate over an array, a slice, a string, a map, or a channel.
	// range returns one (channel) or two values (array, slice, string and map).
	for key, value := range map[string]int{"one": 1, "two": 2, "three": 3} {
		// for each pair in the map, print key and value
		fmt.Printf("key=%s, value=%d\n", key, value)
	}
	// If you only need the value, use the underscore as the key
	for _, name := range []string{"Bob", "Bill", "Joe"} {
		fmt.Printf("Hello, %s\n", name)
	}

	// As with for, := in an if statement means to declare and assign
	// y first, then test y > x.
	if y := expensiveComputation(); y > x {
		x = y
	}
	// Function literals are closures.
	xBig := func() bool {
		return x > 10000 // References x declared above switch statement.
	}
	x = 99999
	fmt.Println("xBig:", xBig()) // true
	x = 1.3e3                    // This makes x == 1300
	fmt.Println("xBig:", xBig()) // false now.

	// What's more is function literals may be defined and called inline,
	// acting as an argument to function, as long as:
	// a) function literal is called immediately (),
	// b) result type matches expected type of argument.
	fmt.Println("Add + double two numbers: ",
		func(a, b int) int {
			return (a + b) * 2
		}(10, 2)) // Called with args 10 and 2
	// => Add + double two numbers: 24

	// When you need it, you'll love it.
	goto love
love:

	learnFunctionFactory() // func returning func is fun(3)(3)
	learnDefer()      // A quick detour to an important keyword.
	learnInterfaces() // Good stuff coming up!
}

func learnFunctionFactory() {
	// Next two are equivalent, with second being more practical
	fmt.Println(sentenceFactory("summer")("A beautiful", "day!"))

	d := sentenceFactory("summer")
	fmt.Println(d("A beautiful", "day!"))
	fmt.Println(d("A lazy", "afternoon!"))
}

// Decorators are common in other languages. Same can be done in Go
// with function literals that accept arguments.
func sentenceFactory(mystring string) func(before, after string) string {
	return func(before, after string) string {
		return fmt.Sprintf("%s %s %s", before, mystring, after) // new string
	}
}

func learnDefer() (ok bool) {
	// Deferred statements are executed just before the function returns.
	defer fmt.Println("deferred statements execute in reverse (LIFO) order.")
	defer fmt.Println("\nThis line is being printed first because")
	// Defer is commonly used to close a file, so the function closing the
	// file stays close to the function opening the file.
	return true
}

// Define Stringer as an interface type with one method, String.
type Stringer interface {
	String() string
}

// Define pair as a struct with two fields, ints named x and y.
type pair struct {
	x, y int
}

// Define a method on type pair. Pair now implements Stringer because Pair has defined all the methods in the interface.
func (p pair) String() string { // p is called the "receiver"
	// Sprintf is another public function in package fmt.
	// Dot syntax references fields of p.
	return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
	// Brace syntax is a "struct literal". It evaluates to an initialized
	// struct. The := syntax declares and initializes p to this struct.
	p := pair{3, 4}
	fmt.Println(p.String()) // Call String method of p, of type pair.
	var i Stringer          // Declare i of interface type Stringer.
	i = p                   // Valid because pair implements Stringer
	// Call String method of i, of type Stringer. Output same as above.
	fmt.Println(i.String())

	// Functions in the fmt package call the String method to ask an object
	// for a printable representation of itself.
	fmt.Println(p) // Output same as above. Println calls String method.
	fmt.Println(i) // Output same as above.

	learnVariadicParams("great", "learning", "here!")
}

// Functions can have variadic parameters.
func learnVariadicParams(myStrings ...interface{}) {
	// Iterate each value of the variadic.
	// The underbar here is ignoring the index argument of the array.
	for _, param := range myStrings {
		fmt.Println("param:", param)
	}

	// Pass variadic value as a variadic parameter.
	fmt.Println("params:", fmt.Sprintln(myStrings...))

	learnErrorHandling()
}

func learnErrorHandling() {
	// ", ok" idiom used to tell if something worked or not.
	m := map[int]string{3: "three", 4: "four"}
	if x, ok := m[1]; !ok { // ok will be false because 1 is not in the map.
		fmt.Println("no one there")
	} else {
		fmt.Print(x) // x would be the value, if it were in the map.
	}
	// An error value communicates not just "ok" but more about the problem.
	if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value
		// prints 'strconv.ParseInt: parsing "non-int": invalid syntax'
		fmt.Println(err)
	}
	// We'll revisit interfaces a little later. Meanwhile,
	learnConcurrency()
}

// c is a channel, a concurrency-safe communication object.
func inc(i int, c chan int) {
	c <- i + 1 // <- is the "send" operator when a channel appears on the left.
}

// We'll use inc to increment some numbers concurrently.
func learnConcurrency() {
	// Same make function used earlier to make a slice. Make allocates and
	// initializes slices, maps, and channels.
	c := make(chan int)
	// Start three concurrent goroutines. Numbers will be incremented
	// concurrently, perhaps in parallel if the machine is capable and
	// properly configured. All three send to the same channel.
	go inc(0, c) // go is a statement that starts a new goroutine.
	go inc(10, c)
	go inc(-805, c)
	// Read three results from the channel and print them out.
	// There is no telling in what order the results will arrive!
	fmt.Println(<-c, <-c, <-c) // channel on right, <- is "receive" operator.

	cs := make(chan string)       // Another channel, this one handles strings.
	ccs := make(chan chan string) // A channel of string channels.
	go func() { c <- 84 }()       // Start a new goroutine just to send a value.
	go func() { cs <- "wordy" }() // Again, for cs this time.
	// Select has syntax like a switch statement but each case involves
	// a channel operation. It selects a case at random out of the cases
	// that are ready to communicate.
	select {
	case i := <-c: // The value received can be assigned to a variable,
		fmt.Printf("it's a %T", i)
	case <-cs: // or the value received can be discarded.
		fmt.Println("it's a string")
	case <-ccs: // Empty channel, not ready for communication.
		fmt.Println("didn't happen.")
	}
	// At this point a value was taken from either c or cs. One of the two
	// goroutines started above has completed, the other will remain blocked.

	learnWebProgramming() // Go does it. You want to do it too.
}

// A single function from package http starts a web server.
func learnWebProgramming() {

	// First parameter of ListenAndServe is TCP address to listen to.
	// Second parameter is an interface, specifically http.Handler.
	go func() {
		err := http.ListenAndServe(":8080", pair{})
		fmt.Println(err) // don't ignore errors
	}()

	requestServer()
}

// Make pair an http.Handler by implementing its only method, ServeHTTP.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Serve data with a method of http.ResponseWriter.
	w.Write([]byte("You learned Go in Y minutes!"))
}

func requestServer() {
	resp, err := http.Get("http://localhost:8080")
	fmt.Println(err)
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Printf("\nWebserver said: `%s`", string(body))
}
```

## Further Reading

The root of all things Go is the [official Go web site](http://golang.org/).
There you can follow the tutorial, play interactively, and read lots.
Aside from a tour, [the docs](https://golang.org/doc/) contain information on
how to write clean and effective Go code, package and command docs, and release history.

The language definition itself is highly recommended. It's easy to read
and amazingly short (as language definitions go these days.)

You can play around with the code on [Go playground](https://play.golang.org/p/tnWMjr16Mm). Try to change it and run it from your browser! Note that you can use [https://play.golang.org](https://play.golang.org) as a [REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) to test things and code in your browser, without even installing Go.

On the reading list for students of Go is the [source code to the standard
library](http://golang.org/src/pkg/). Comprehensively documented, it
demonstrates the best of readable and understandable Go, Go style, and Go
idioms. Or you can click on a function name in [the
documentation](http://golang.org/pkg/) and the source code comes up!

Another great resource to learn Go is [Go by example](https://gobyexample.com/).

Go Mobile adds support for mobile platforms (Android and iOS). You can write all-Go native mobile apps or write a library that contains bindings from a Go package, which can be invoked via Java (Android) and Objective-C (iOS). Check out the [Go Mobile page](https://github.com/golang/go/wiki/Mobile) for more information.
---
language: Groovy
filename: learngroovy.groovy
contributors:
    - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"]
filename: learngroovy.groovy
---

Groovy - A dynamic language for the Java platform [Read more here.](http://www.groovy-lang.org/)

```groovy

/*
  Set yourself up:

  1) Install SDKMAN - http://sdkman.io/
  2) Install Groovy: sdk install groovy
  3) Start the groovy console by typing: groovyConsole

*/

//  Single line comments start with two forward slashes
/*
Multi line comments look like this.
*/

// Hello World
println "Hello world!"

/*
  Variables:

  You can assign values to variables for later use
*/

def x = 1
println x

x = new java.util.Date()
println x

x = -3.1499392
println x

x = false
println x

x = "Groovy!"
println x

/*
  Collections and maps
*/

//Creating an empty list
def technologies = []

/*** Adding a elements to the list ***/

// As with Java
technologies.add("Grails")

// Left shift adds, and returns the list
technologies << "Groovy"

// Add multiple elements
technologies.addAll(["Gradle","Griffon"])

/*** Removing elements from the list ***/

// As with Java
technologies.remove("Griffon")

// Subtraction works also
technologies = technologies - 'Grails'

/*** Iterating Lists ***/

// Iterate over elements of a list
technologies.each { println "Technology: $it"}
technologies.eachWithIndex { it, i -> println "$i: $it"}

/*** Checking List contents ***/

//Evaluate if a list contains element(s) (boolean)
contained = technologies.contains( 'Groovy' )

// Or
contained = 'Groovy' in technologies

// Check for multiple contents
technologies.containsAll(['Groovy','Grails'])

/*** Sorting Lists ***/

// Sort a list (mutates original list)
technologies.sort()

// To sort without mutating original, you can do:
sortedTechnologies = technologies.sort( false )

/*** Manipulating Lists ***/

//Replace all elements in the list
Collections.replaceAll(technologies, 'Gradle', 'gradle')

//Shuffle a list
Collections.shuffle(technologies, new Random())

//Clear a list
technologies.clear()

//Creating an empty map
def devMap = [:]

//Add values
devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
devMap.put('lastName','Perez')

//Iterate over elements of a map
devMap.each { println "$it.key: $it.value" }
devMap.eachWithIndex { it, i -> println "$i: $it"}

//Evaluate if a map contains a key
assert devMap.containsKey('name')

//Evaluate if a map contains a value
assert devMap.containsValue('Roberto')

//Get the keys of a map
println devMap.keySet()

//Get the values of a map
println devMap.values()

/*
  Groovy Beans

  GroovyBeans are JavaBeans but using a much simpler syntax

  When Groovy is compiled to bytecode, the following rules are used.

    * If the name is declared with an access modifier (public, private or
      protected) then a field is generated.

    * A name declared with no access modifier generates a private field with
      public getter and setter (i.e. a property).

    * If a property is declared final the private field is created final and no
      setter is generated.

    * You can declare a property and also declare your own getter or setter.

    * You can declare a property and a field of the same name, the property will
      use that field then.

    * If you want a private or protected property you have to provide your own
      getter and setter which must be declared private or protected.

    * If you access a property from within the class the property is defined in
      at compile time with implicit or explicit this (for example this.foo, or
      simply foo), Groovy will access the field directly instead of going though
      the getter and setter.

    * If you access a property that does not exist using the explicit or
      implicit foo, then Groovy will access the property through the meta class,
      which may fail at runtime.

*/

class Foo {
    // read only property
    final String name = "Roberto"

    // read only property with public getter and protected setter
    String language
    protected void setLanguage(String language) { this.language = language }

    // dynamically typed property
    def lastName
}

/*
  Logical Branching and Looping
*/

//Groovy supports the usual if - else syntax
def x = 3

if(x==1) {
    println "One"
} else if(x==2) {
    println "Two"
} else {
    println "X greater than Two"
}

//Groovy also supports the ternary operator:
def y = 10
def x = (y > 1) ? "worked" : "failed"
assert x == "worked"

//Groovy supports 'The Elvis Operator' too!
//Instead of using the ternary operator:

displayName = user.name ? user.name : 'Anonymous'

//We can write it:
displayName = user.name ?: 'Anonymous'

//For loop
//Iterate over a range
def x = 0
for (i in 0 .. 30) {
    x += i
}

//Iterate over a list
x = 0
for( i in [5,3,2,1] ) {
    x += i
}

//Iterate over an array
array = (0..20).toArray()
x = 0
for (i in array) {
    x += i
}

//Iterate over a map
def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
x = 0
for ( e in map ) {
    x += e.value
}

/*
  Operators

  Operator Overloading for a list of the common operators that Groovy supports:
  http://www.groovy-lang.org/operators.html#Operator-Overloading

  Helpful groovy operators
*/
//Spread operator:  invoke an action on all items of an aggregate object.
def technologies = ['Groovy','Grails','Gradle']
technologies*.toUpperCase() // = to technologies.collect { it?.toUpperCase() }

//Safe navigation operator: used to avoid a NullPointerException.
def user = User.get(1)
def username = user?.username


/*
  Closures
  A Groovy Closure is like a "code block" or a method pointer. It is a piece of
  code that is defined and then executed at a later point.

  More info at: http://www.groovy-lang.org/closures.html
*/
//Example:
def clos = { println "Hello World!" }

println "Executing the Closure:"
clos()

//Passing parameters to a closure
def sum = { a, b -> println a+b }
sum(2,4)

//Closures may refer to variables not listed in their parameter list.
def x = 5
def multiplyBy = { num -> num * x }
println multiplyBy(10)

// If you have a Closure that takes a single argument, you may omit the
// parameter definition of the Closure
def clos = { print it }
clos( "hi" )

/*
  Groovy can memoize closure results [1][2][3]
*/
def cl = {a, b ->
    sleep(3000) // simulate some time consuming processing
    a + b
}

mem = cl.memoize()

def callClosure(a, b) {
    def start = System.currentTimeMillis()
    mem(a, b)
    println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs."
}

callClosure(1, 2)
callClosure(1, 2)
callClosure(2, 3)
callClosure(2, 3)
callClosure(3, 4)
callClosure(3, 4)
callClosure(1, 2)
callClosure(2, 3)
callClosure(3, 4)

/*
  Expando

  The Expando class is a dynamic bean so we can add properties and we can add
  closures as methods to an instance of this class

  http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/
  def user = new Expando(name:"Roberto")
  assert 'Roberto' == user.name

  user.lastName = 'Pérez'
  assert 'Pérez' == user.lastName

  user.showInfo = { out ->
      out << "Name: $name"
      out << ", Last name: $lastName"
  }

  def sw = new StringWriter()
  println user.showInfo(sw)


/*
  Metaprogramming (MOP)
*/

//Using ExpandoMetaClass to add behaviour
String.metaClass.testAdd = {
    println "we added this"
}

String x = "test"
x?.testAdd()

//Intercepting method calls
class Test implements GroovyInterceptable {
    def sum(Integer x, Integer y) { x + y }

    def invokeMethod(String name, args) {
        System.out.println "Invoke method $name with args: $args"
    }
}

def test = new Test()
test?.sum(2,3)
test?.multiply(2,3)

//Groovy supports propertyMissing for dealing with property resolution attempts.
class Foo {
   def propertyMissing(String name) { name }
}
def f = new Foo()

assertEquals "boo", f.boo

/*
  TypeChecked and CompileStatic
  Groovy, by nature, is and will always be a dynamic language but it supports
  typechecked and compilestatic

  More info: http://www.infoq.com/articles/new-groovy-20
*/
//TypeChecked
import groovy.transform.TypeChecked

void testMethod() {}

@TypeChecked
void test() {
    testMeethod()

    def name = "Roberto"

    println naameee

}

//Another example:
import groovy.transform.TypeChecked

@TypeChecked
Integer test() {
    Integer num = "1"

    Integer[] numbers = [1,2,3,4]

    Date date = numbers[1]

    return "Test"

}

//CompileStatic example:
import groovy.transform.CompileStatic

@CompileStatic
int sum(int x, int y) {
    x + y
}

assert sum(2,5) == 7


```

## Further resources

[Groovy documentation](http://www.groovy-lang.org/documentation.html)

[Groovy web console](http://groovyconsole.appspot.com/)

Join a [Groovy user group](http://www.groovy-lang.org/usergroups.html)

## Books

* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook)

* [Groovy in Action] (http://manning.com/koenig2/)

* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)

[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html
---
language: Hack
contributors:
    - ["Stephen Holdaway", "https://github.com/stecman"]
    - ["David Lima", "https://github.com/davelima"]
filename: learnhack.hh
---

Hack is a superset of PHP that runs under a virtual machine called HHVM. Hack
is almost completely interoperable with existing PHP code and adds a bunch of
useful features from statically typed languages.


Only Hack-specific features are covered here. Details about PHP's syntax are
available in the [PHP article](http://learnxinyminutes.com/docs/php/) on this site.

```php
<?hh

// Hack syntax is only enabled for files starting with an <?hh marker
// <?hh markers cannot be interspersed with HTML the way <?php can be.
// Using the marker "<?hh //strict" puts the type checker in strict mode.


// Scalar parameter type hints
function repeat(string $word, int $count)
{
    $word = trim($word);
    return str_repeat($word . ' ', $count);
}

// Type hints for return values
function add(...$numbers) : int
{
    return array_sum($numbers);
}

// Functions that return nothing are hinted as "void"
function truncate(resource $handle) : void
{
    // ...
}

// Type hints must explicitly allow being nullable
function identity(?string $stringOrNull) : ?string
{
    return $stringOrNull;
}

// Type hints can be specified on class properties
class TypeHintedProperties
{
    public ?string $name;

    protected int $id;

    private float $score = 100.0;

    // Hack's type checker enforces that typed properties either have a
    // default value or are set in the constructor.
    public function __construct(int $id)
    {
        $this->id = $id;
    }
}


// Concise anonymous functions (lambdas)
$multiplier = 5;
array_map($y ==> $y * $multiplier, [1, 2, 3]);


// Generics
class Box<T>
{
    protected T $data;

    public function __construct(T $data) {
        $this->data = $data;
    }

    public function getData(): T {
        return $this->data;
    }
}

function openBox(Box<int> $box) : int
{
    return $box->getData();
}


// Shapes
//
// Hack adds the concept of shapes for defining struct-like arrays with a
// guaranteed, type-checked set of keys
type Point2D = shape('x' => int, 'y' => int);

function distance(Point2D $a, Point2D $b) : float
{
    return sqrt(pow($b['x'] - $a['x'], 2) + pow($b['y'] - $a['y'], 2));
}

distance(
    shape('x' => -1, 'y' => 5),
    shape('x' => 2, 'y' => 50)
);


// Type aliasing
//
// Hack adds a bunch of type aliasing features for making complex types readable
newtype VectorArray = array<int, Vector<int>>;

// A tuple containing two integers
newtype Point = (int, int);

function addPoints(Point $p1, Point $p2) : Point
{
    return tuple($p1[0] + $p2[0], $p1[1] + $p2[1]);
}

addPoints(
    tuple(1, 2),
    tuple(5, 6)
);


// First-class enums
enum RoadType : int
{
    Road = 0;
    Street = 1;
    Avenue = 2;
    Boulevard = 3;
}

function getRoadType() : RoadType
{
    return RoadType::Avenue;
}


// Constructor argument promotion
//
// To avoid boilerplate property and constructor definitions that only set
// properties, Hack adds a concise syntax for defining properties and a
// constructor at the same time.
class ArgumentPromotion
{
    public function __construct(public string $name,
                                protected int $age,
                                private bool $isAwesome) {}
}

class WithoutArgumentPromotion
{
    public string $name;

    protected int $age;

    private bool $isAwesome;

    public function __construct(string $name, int $age, bool $isAwesome)
    {
        $this->name = $name;
        $this->age = $age;
        $this->isAwesome = $isAwesome;
    }
}


// Co-operative multi-tasking
//
// Two new keywords "async" and "await" can be used to perform multi-tasking
// Note that this does not involve threads - it just allows transfer of control
async function cooperativePrint(int $start, int $end) : Awaitable<void>
{
    for ($i = $start; $i <= $end; $i++) {
        echo "$i ";

        // Give other tasks a chance to do something
        await RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0);
    }
}

// This prints "1 4 7 2 5 8 3 6 9"
AwaitAllWaitHandle::fromArray([
    cooperativePrint(1, 3),
    cooperativePrint(4, 6),
    cooperativePrint(7, 9)
])->getWaitHandle()->join();


// Attributes
//
// Attributes are a form of metadata for functions. Hack provides some
// special built-in attributes that introduce useful behaviour.

// The __Memoize special attribute causes the result of a function to be cached
<<__Memoize>>
function doExpensiveTask() : ?string
{
    return file_get_contents('http://example.com');
}

// The function's body is only executed once here:
doExpensiveTask();
doExpensiveTask();


// The __ConsistentConstruct special attribute signals the Hack type checker to
// ensure that the signature of __construct is the same for all subclasses.
<<__ConsistentConstruct>>
class ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
        // ...
    }

    public function someMethod()
    {
        // ...
    }
}

class ConsistentBar extends ConsistentFoo
{
    public function __construct(int $x, float $y)
    {
        // Hack's type checker enforces that parent constructors are called
        parent::__construct($x, $y);

        // ...
    }

    // The __Override annotation is an optional signal for the Hack type
    // checker to enforce that this method is overriding a method in a parent
    // or trait. If not, this will error.
    <<__Override>>
    public function someMethod()
    {
        // ...
    }
}

class InvalidFooSubclass extends ConsistentFoo
{
    // Not matching the parent constructor will cause a type checker error:
    //
    //  "This object is of type ConsistentBaz. It is incompatible with this object
    //   of type ConsistentFoo because some of their methods are incompatible"
    //
    public function __construct(float $x)
    {
        // ...
    }

    // Using the __Override annotation on a non-overridden method will cause a
    // type checker error:
    //
    //  "InvalidFooSubclass::otherMethod() is marked as override; no non-private
    //   parent definition found or overridden parent is defined in non-<?hh code"
    //
    <<__Override>>
    public function otherMethod()
    {
        // ...
    }
}


// Traits can implement interfaces (standard PHP does not support this)
interface KittenInterface
{
    public function play() : void;
}

trait CatTrait implements KittenInterface
{
    public function play() : void
    {
        // ...
    }
}

class Samuel
{
    use CatTrait;
}


$cat = new Samuel();
$cat instanceof KittenInterface === true; // True

```

## More Information

Visit the [Hack language reference](http://docs.hhvm.com/manual/en/hacklangref.php)
for detailed explanations of the features Hack adds to PHP, or the [official Hack website](http://hacklang.org/)
for more general information.

Visit the [official HHVM website](http://hhvm.com/) for HHVM installation instructions.

Visit [Hack's unsupported PHP features article](http://docs.hhvm.com/manual/en/hack.unsupported.php)
for details on the backwards incompatibility between Hack and PHP.
---
language: haml
filename: learnhaml.haml
contributors:
  - ["Simon Neveu", "https://github.com/sneveu"]
---

Haml is a markup language predominantly used with Ruby that cleanly and simply describes the HTML of any web document without the use of inline code. It is a popular alternative to using Rails templating language (.erb) and allows you to embed Ruby code into your markup.

It aims to reduce repetition in your markup by closing tags for you based on the structure of the indents in your code. The result is markup that is well-structured, DRY, logical, and easier to read.

You can also use Haml on a project independent of Ruby, by installing the Haml gem on your machine and using the command line to convert it to html.

$ haml input_file.haml output_file.html


```haml
/ -------------------------------------------
/ Indenting
/ -------------------------------------------

/
  Because of the importance indentation has on how your code is rendered, the
  indents should be consistent throughout the document. Any differences in
  indentation will throw an error. It's common-practice to use two spaces,
  but it's really up to you, as long as they're constant.


/ -------------------------------------------
/ Comments
/ -------------------------------------------

/ This is what a comment looks like in Haml.

/
  To write a multi line comment, indent your commented code to be
  wrapped by the forward slash

-# This is a silent comment, which means it won't be rendered into the doc at all


/ -------------------------------------------
/ Html elements
/ -------------------------------------------

/ To write your tags, use the percent sign followed by the name of the tag
%body
  %header
    %nav

/ Notice no closing tags. The above code would output
  <body>
    <header>
      <nav></nav>
    </header>
  </body>

/ The div tag is the default element, so they can be written simply like this
.foo

/ To add content to a tag, add the text directly after the declaration
%h1 Headline copy

/ To write multiline content, nest it instead
%p
  This is a lot of content that we could probably split onto two
  separate lines.

/
  You can escape html by using the ampersand and equals sign ( &= ). This
  converts html-sensitive characters (&, /, :) into their html encoded
  equivalents. For example

%p
  &= "Yes & yes"

/ would output 'Yes &amp; yes'

/ You can unescape html by using the bang and equals sign ( != )
%p
  != "This is how you write a paragraph tag <p></p>"

/ which would output 'This is how you write a paragraph tag <p></p>'

/ CSS classes can be added to your tags either by chaining .classnames to the tag
%div.foo.bar

/ or as part of a Ruby hash
%div{:class => 'foo bar'}

/ Attributes for any tag can be added in the hash
%a{:href => '#', :class => 'bar', :title => 'Bar'}

/ For boolean attributes assign the value 'true'
%input{:selected => true}

/ To write data-attributes, use the :data key with its value as another hash
%div{:data => {:attribute => 'foo'}}


/ -------------------------------------------
/ Inserting Ruby
/ -------------------------------------------

/
  To output a Ruby value as the contents of a tag, use an equals sign followed
  by the Ruby code

%h1= book.name

%p
  = book.author
  = book.publisher


/ To run some Ruby code without rendering it to the html, use a hyphen instead
- books = ['book 1', 'book 2', 'book 3']

/ Allowing you to do all sorts of awesome, like Ruby blocks
- books.shuffle.each_with_index do |book, index|
  %h1= book

  if book do
    %p This is a book
    
/ Adding ordered / unordered list
%ul
  %li
    =item1
    =item2

/
  Again, no need to add the closing tags to the block, even for the Ruby.
  Indentation will take care of that for you.

/ -------------------------------------------
/ Inserting Table with bootstrap classes
/ -------------------------------------------

%table.table.table-hover
  %thead
    %tr
      %th Header 1
      %th Header 2
    
    %tr
      %td Value1
      %td value2
    
  %tfoot
    %tr
      %td
        Foot value


/ -------------------------------------------
/ Inline Ruby / Ruby interpolation
/ -------------------------------------------

/ Include a Ruby variable in a line of plain text using #{}
%p Your highest scoring game is #{best_game}


/ -------------------------------------------
/ Filters
/ -------------------------------------------

/
  Use the colon to define Haml filters, one example of a filter you can
  use is :javascript, which can be used for writing inline js

:javascript
  console.log('This is inline <script>');

```

## Additional resources

- [What is HAML?](http://haml.info/) - A good introduction that does a much better job of explaining the benefits of using HAML.
- [Official Docs](http://haml.info/docs/yardoc/file.REFERENCE.html) - If you'd like to go a little deeper.
---
language: Haskell
filename: learnhaskell.hs
contributors:
    - ["Adit Bhargava", "http://adit.io"]
---

Haskell was designed as a practical, purely functional programming
language. It's famous for its monads and its type system, but I keep coming back
to it because of its elegance. Haskell makes coding a real joy for me.

```haskell
-- Single line comments start with two dashes.
{- Multiline comments can be enclosed
in a block like this.
-}

----------------------------------------------------
-- 1. Primitive Datatypes and Operators
----------------------------------------------------

-- You have numbers
3 -- 3

-- Math is what you would expect
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- Division is not integer division by default
35 / 4 -- 8.75

-- integer division
35 `div` 4 -- 8

-- Boolean values are primitives
True
False

-- Boolean operations
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- In the above examples, `not` is a function that takes one value.
-- Haskell doesn't need parentheses for function calls...all the arguments
-- are just listed after the function. So the general pattern is:
-- func arg1 arg2 arg3...
-- See the section on functions for information on how to write your own.

-- Strings and characters
"This is a string."
'a' -- character
'You cant use single quotes for strings.' -- error!

-- Strings can be concatenated
"Hello " ++ "world!" -- "Hello world!"

-- A string is a list of characters
['H', 'e', 'l', 'l', 'o'] -- "Hello"
"This is a string" !! 0 -- 'T'


----------------------------------------------------
-- 2. Lists and Tuples
----------------------------------------------------

-- Every element in a list must have the same type.
-- These two lists are equal:
[1, 2, 3, 4, 5]
[1..5]

-- Ranges are versatile.
['A'..'F'] -- "ABCDEF"

-- You can create a step in a range.
[0,2..10] -- [0, 2, 4, 6, 8, 10]
[5..1] -- [] (Haskell defaults to incrementing)
[5,4..1] -- [5, 4, 3, 2, 1]

-- indexing into a list
[1..10] !! 3 -- 4 (zero-based indexing)

-- You can also have infinite lists in Haskell!
[1..] -- a list of all the natural numbers

-- Infinite lists work because Haskell has "lazy evaluation". This means
-- that Haskell only evaluates things when it needs to. So you can ask for
-- the 1000th element of your list and Haskell will give it to you:

[1..] !! 999 -- 1000

-- And now Haskell has evaluated elements 1 - 1000 of this list...but the
-- rest of the elements of this "infinite" list don't exist yet! Haskell won't
-- actually evaluate them until it needs to.

-- joining two lists
[1..5] ++ [6..10]

-- adding to the head of a list
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- more list operations
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- list comprehensions
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- with a conditional
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- Every element in a tuple can be a different type, but a tuple has a
-- fixed length.
-- A tuple:
("haskell", 1)

-- accessing elements of a pair (i.e. a tuple of length 2)
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. Functions
----------------------------------------------------
-- A simple function that takes two variables
add a b = a + b

-- Note that if you are using ghci (the Haskell interpreter)
-- You'll need to use `let`, i.e.
-- let add a b = a + b

-- Using the function
add 1 2 -- 3

-- You can also put the function name between the two arguments
-- with backticks:
1 `add` 2 -- 3

-- You can also define functions that have no letters! This lets
-- you define your own operators! Here's an operator that does
-- integer division
(//) a b = a `div` b
35 // 4 -- 8

-- Guards: an easy way to do branching in functions
fib x
  | x < 2 = 1
  | otherwise = fib (x - 1) + fib (x - 2)

-- Pattern matching is similar. Here we have given three different
-- equations that define fib. Haskell will automatically use the first
-- equation whose left hand side pattern matches the value.
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- Pattern matching on tuples:
foo (x, y) = (x + 1, y + 2)

-- Pattern matching on lists. Here `x` is the first element
-- in the list, and `xs` is the rest of the list. We can write
-- our own map function:
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- Anonymous functions are created with a backslash followed by
-- all the arguments.
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- using fold (called `inject` in some languages) with an anonymous
-- function. foldl1 means fold left, and use the first value in the
-- list as the initial value for the accumulator.
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. More functions
----------------------------------------------------

-- partial application: if you don't pass in all the arguments to a function,
-- it gets "partially applied". That means it returns a function that takes the
-- rest of the arguments.

add a b = a + b
foo = add 10 -- foo is now a function that takes a number and adds 10 to it
foo 5 -- 15

-- Another way to write the same thing
foo = (10+)
foo 5 -- 15

-- function composition
-- the operator `.` chains functions together.
-- For example, here foo is a function that takes a value. It adds 10 to it,
-- multiplies the result of that by 4, and then returns the final value.
foo = (4*) . (10+)

-- 4*(10+5) = 60
foo 5 -- 60

-- fixing precedence
-- Haskell has an operator called `$`. This operator applies a function 
-- to a given parameter. In contrast to standard function application, which 
-- has highest possible priority of 10 and is left-associative, the `$` operator 
-- has priority of 0 and is right-associative. Such a low priority means that
-- the expression on its right is applied as the parameter to the function on its left.

-- before
even (fib 7) -- false

-- equivalently
even $ fib 7 -- false

-- composing functions
even . fib $ 7 -- false


----------------------------------------------------
-- 5. Type signatures
----------------------------------------------------

-- Haskell has a very strong type system, and every valid expression has a type. 

-- Some basic types:
5 :: Integer
"hello" :: String
True :: Bool

-- Functions have types too.
-- `not` takes a boolean and returns a boolean:
-- not :: Bool -> Bool

-- Here's a function that takes two arguments:
-- add :: Integer -> Integer -> Integer

-- When you define a value, it's good practice to write its type above it:
double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. Control Flow and If Expressions
----------------------------------------------------

-- if-expressions
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- if-expressions can be on multiple lines too, indentation is important
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- case expressions: Here's how you could parse command line arguments
case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"

-- Haskell doesn't have loops; it uses recursion instead.
-- map applies a function over every element in a list

map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- you can make a for function using map
for array func = map func array

-- and then use it
for [0..5] $ \i -> show i

-- we could've written that like this too:
for [0..5] show

-- You can use foldl or foldr to reduce a list
-- foldl <fn> <initial value> <list>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- This is the same as
(2 * (2 * (2 * 4 + 1) + 2) + 3)

-- foldl is left-handed, foldr is right-handed
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- This is now the same as
(2 * 1 + (2 * 2 + (2 * 3 + 4)))

----------------------------------------------------
-- 7. Data Types
----------------------------------------------------

-- Here's how you make your own data type in Haskell

data Color = Red | Blue | Green

-- Now you can use it in a function:

say :: Color -> String
say Red   = "You are Red!"
say Blue  = "You are Blue!"
say Green = "You are Green!"

-- Your data types can have parameters too:

data Maybe a = Nothing | Just a

-- These are all of type Maybe
Just "hello"    -- of type `Maybe String`
Just 1          -- of type `Maybe Int`
Nothing         -- of type `Maybe a` for any `a`

----------------------------------------------------
-- 8. Haskell IO
----------------------------------------------------

-- While IO can't be explained fully without explaining monads,
-- it is not hard to explain enough to get going.

-- When a Haskell program is executed, `main` is
-- called. It must return a value of type `IO a` for some type `a`. For example:

main :: IO ()
main = putStrLn $ "Hello, sky! " ++ (say Blue)
-- putStrLn has type String -> IO ()

-- It is easiest to do IO if you can implement your program as
-- a function from String to String. The function
--    interact :: (String -> String) -> IO ()
-- inputs some text, runs a function on it, and prints out the
-- output.

countLines :: String -> String
countLines = show . length . lines

main' = interact countLines

-- You can think of a value of type `IO ()` as representing a
-- sequence of actions for the computer to do, much like a
-- computer program written in an imperative language. We can use
-- the `do` notation to chain actions together. For example:

sayHello :: IO ()
sayHello = do
   putStrLn "What is your name?"
   name <- getLine -- this gets a line and gives it the name "name"
   putStrLn $ "Hello, " ++ name

-- Exercise: write your own version of `interact` that only reads
--           one line of input.

-- The code in `sayHello` will never be executed, however. The only
-- action that ever gets executed is the value of `main`.
-- To run `sayHello` comment out the above definition of `main`
-- and replace it with:
--   main = sayHello

-- Let's understand better how the function `getLine` we just
-- used works. Its type is:
--    getLine :: IO String
-- You can think of a value of type `IO a` as representing a
-- computer program that will generate a value of type `a`
-- when executed (in addition to anything else it does). We can
-- name and reuse this value using `<-`. We can also
-- make our own action of type `IO String`:

action :: IO String
action = do
   putStrLn "This is a line. Duh"
   input1 <- getLine
   input2 <- getLine
   -- The type of the `do` statement is that of its last line.
   -- `return` is not a keyword, but merely a function
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- We can use this just like we used `getLine`:

main'' = do
    putStrLn "I will echo two lines!"
    result <- action
    putStrLn result
    putStrLn "This was all, folks!"

-- The type `IO` is an example of a "monad". The way Haskell uses a monad to
-- do IO allows it to be a purely functional language. Any function that
-- interacts with the outside world (i.e. does IO) gets marked as `IO` in its
-- type signature. This lets us reason about which functions are "pure" (don't
-- interact with the outside world or modify state) and which functions aren't.

-- This is a powerful feature, because it's easy to run pure functions
-- concurrently; so, concurrency in Haskell is very easy.


----------------------------------------------------
-- 9. The Haskell REPL
----------------------------------------------------

-- Start the repl by typing `ghci`.
-- Now you can type in Haskell code. Any new values
-- need to be created with `let`:

let foo = 5

-- You can see the type of any value or expression with `:t`:

> :t foo
foo :: Integer

-- Operators, such as `+`, `:` and `$`, are functions.
-- Their type can be inspected by putting the operator in parentheses:

> :t (:)
(:) :: a -> [a] -> [a]

-- You can get additional information on any `name` using `:i`:

> :i (+)
class Num a where
  (+) :: a -> a -> a
  ...
    -- Defined in ‘GHC.Num’
infixl 6 +

-- You can also run any action of type `IO ()`

> sayHello
What is your name?
Friend!
Hello, Friend!

```

There's a lot more to Haskell, including typeclasses and monads. These are the
big ideas that make Haskell such fun to code in. I'll leave you with one final
Haskell example: an implementation of a quicksort variant in Haskell:

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

There are two popular ways to install Haskell: The traditional [Cabal-based installation](http://www.haskell.org/platform/), and the newer [Stack-based process](https://www.stackage.org/install).

You can find a much gentler introduction from the excellent
[Learn you a Haskell](http://learnyouahaskell.com/) or
[Real World Haskell](http://book.realworldhaskell.org/).
---
language: haxe
filename: LearnHaxe3.hx
contributors:
    - ["Justin Donaldson", "https://github.com/jdonaldson/"]
    - ["Dan Korostelev", "https://github.com/nadako/"]
---

Haxe is a web-oriented language that provides platform support for C++, C#,
Swf/ActionScript, Javascript, Java, and Neko byte code (also written by the
Haxe author).  Note that this guide is for Haxe version 3.  Some of the guide
may be applicable to older versions, but it is recommended to use other
references.

```csharp
/*
   Welcome to Learn Haxe 3 in 15 minutes.  http://www.haxe.org
   This is an executable tutorial.  You can compile and run it using the haxe
   compiler, while in the same directory as LearnHaxe.hx:
   $> haxe -main LearnHaxe3 -x out

   Look for the slash-star marks surrounding these paragraphs.  We are inside
   a "Multiline comment".  We can leave some notes here that will get ignored
   by the compiler.

   Multiline comments are also used to generate javadoc-style documentation for
   haxedoc.  They will be used for haxedoc if they immediately precede a class,
   class function, or class variable.

 */

// Double slashes like this will give a single-line comment


/*
   This is your first actual haxe code coming up, it's declaring an empty
   package.  A package isn't necessary, but it's useful if you want to create a
   namespace for your code (e.g. org.yourapp.ClassName).

   Omitting package declaration is the same as declaring an empty package.
 */
package; // empty package, no namespace.

/*
   Packages are directories that contain modules. Each module is a .hx file
   that contains types defined in a package. Package names (e.g. org.yourapp)
   must be lower case while module names are capitalized. A module contain one
   or more types whose names are also capitalized.

   E.g, the class "org.yourapp.Foo" should have the folder structure org/module/Foo.hx,
   as accessible from the compiler's working directory or class path.

   If you import code from other files, it must be declared before the rest of
   the code.  Haxe provides a lot of common default classes to get you started:
 */
import haxe.ds.ArraySort;

// you can import many classes/modules at once with "*"
import haxe.ds.*;

// you can import static fields
import Lambda.array;

// you can also use "*" to import all static fields
import Math.*;

/*
   You can also import classes in a special way, enabling them to extend the
   functionality of other classes like a "mixin".  More on 'using' later.
 */
using StringTools;

/*
   Typedefs are like variables... for types.  They must be declared before any
   code.  More on this later.
 */
typedef FooString = String;

// Typedefs can also reference "structural" types, more on that later as well.
typedef FooObject = { foo: String };

/*
   Here's the class definition.  It's the main class for the file, since it has
   the same name (LearnHaxe3).
 */
class LearnHaxe3{
    /*
       If you want certain code to run automatically, you need to put it in
       a static main function, and specify the class in the compiler arguments.
       In this case, we've specified the "LearnHaxe3" class in the compiler
       arguments above.
     */
    static function main(){

        /*
           Trace is the default method of printing haxe expressions to the
           screen.  Different targets will have different methods of
           accomplishing this.  E.g., java, c++, c#, etc. will print to std
           out.  Javascript will print to console.log, and flash will print to
           an embedded TextField.  All traces come with a default newline.
           Finally, It's possible to prevent traces from showing by using the
           "--no-traces" argument on the compiler.
         */
        trace("Hello World, with trace()!");

        /*
           Trace can handle any type of value or object.  It will try to print
           a representation of the expression as best it can.  You can also
           concatenate strings with the "+" operator:
         */
        trace( " Integer: " + 10 + " Float: " + 3.14 + " Boolean: " + true);

        /*
           In Haxe, it's required to separate expressions in the same block with
           semicolons.  But, you can put two expressions on one line:
         */
        trace('two expressions..'); trace('one line');


        //////////////////////////////////////////////////////////////////
        // Types & Variables
        //////////////////////////////////////////////////////////////////
        trace("***Types & Variables***");

        /*
           You can save values and references to data structures using the
           "var" keyword:
         */
        var an_integer:Int = 1;
        trace(an_integer + " is the value for an_integer");


        /*
           Haxe is statically typed, so "an_integer" is declared to have an
           "Int" type, and the rest of the expression assigns the value "1" to
           it.  It's not necessary to declare the type in many cases.  Here,
           the haxe compiler is inferring that the type of another_integer
           should be "Int".
         */
        var another_integer = 2;
        trace(another_integer + " is the value for another_integer");

        // The $type() method prints the type that the compiler assigns:
        $type(another_integer);

        // You can also represent integers with hexadecimal:
        var hex_integer = 0xffffff;

        /*
           Haxe uses platform precision for Int and Float sizes.  It also
           uses the platform behavior for overflow.
           (Other numeric types and behavior are possible using special
           libraries)
         */

        /*
           In addition to simple values like Integers, Floats, and Booleans,
           Haxe provides standard library implementations for common data
           structures like strings, arrays, lists, and maps:
         */

        var a_string = "some" + 'string';  // strings can have double or single quotes
        trace(a_string + " is the value for a_string");

        /*
           Strings can be "interpolated" by inserting variables into specific
           positions.  The string must be single quoted, and the variable must
           be preceded with "$".  Expressions can be enclosed in ${...}.
         */
        var x = 1;
        var an_interpolated_string = 'the value of x is $x';
        var another_interpolated_string = 'the value of x + 1 is ${x + 1}';

        /*
           Strings are immutable, instance methods will return a copy of
           parts or all of the string.
           (See also the StringBuf class).
         */
        var a_sub_string = a_string.substr(0,4);
        trace(a_sub_string + " is the value for a_sub_string");

        /*
           Regexes are also supported, but there's not enough space to go into
           much detail.
         */
        var re = ~/foobar/;
        trace(re.match('foo') + " is the value for (~/foobar/.match('foo')))");

        /*
           Arrays are zero-indexed, dynamic, and mutable.  Missing values are
           defined as null.
         */
        var a = new Array<String>(); // an array that contains Strings
        a[0] = 'foo';
        trace(a.length + " is the value for a.length");
        a[9] = 'bar';
        trace(a.length + " is the value for a.length (after modification)");
        trace(a[3] + " is the value for a[3]"); //null

        /*
           Arrays are *generic*, so you can indicate which values they contain
           with a type parameter:
         */
        var a2 = new Array<Int>(); // an array of Ints
        var a3 = new Array<Array<String>>(); // an Array of Arrays (of Strings).

        /*
           Maps are simple key/value data structures.  The key and the value
           can be of any type.
         */
        var m = new Map<String, Int>();  // The keys are strings, the values are Ints.
        m.set('foo', 4);
        // You can also use array notation;
        m['bar'] = 5;
        trace(m.exists('bar') + " is the value for m.exists('bar')");
        trace(m.get('bar') + " is the value for m.get('bar')");
        trace(m['bar'] + " is the value for m['bar']");

        var m2 =  ['foo' => 4, 'baz' => 6]; // Alternative map syntax
        trace(m2 + " is the value for m2");

        /*
           Remember, you can use type inference.  The Haxe compiler will
           decide the type of the variable the first time you pass an
           argument that sets a type parameter.
         */
        var m3 = new Map();
        m3.set(6, 'baz'); // m3 is now a Map<Int,String>
        trace(m3 + " is the value for m3");

        /*
           Haxe has some more common datastructures in the haxe.ds module, such as
           List, Stack, and BalancedTree
         */


        //////////////////////////////////////////////////////////////////
        // Operators
        //////////////////////////////////////////////////////////////////
        trace("***OPERATORS***");

        // basic arithmetic
        trace((4 + 3) + " is the value for (4 + 3)");
        trace((5 - 1) + " is the value for (5 - 1)");
        trace((2 * 4) + " is the value for (2 * 4)");
        trace((8 / 3) + " is the value for (8 / 3) (division always produces Floats)");
        trace((12 % 4) + " is the value for (12 % 4)");


        //basic comparison
        trace((3 == 2) + " is the value for 3 == 2");
        trace((3 != 2) + " is the value for 3 != 2");
        trace((3 >  2) + " is the value for 3 > 2");
        trace((3 <  2) + " is the value for 3 < 2");
        trace((3 >= 2) + " is the value for 3 >= 2");
        trace((3 <= 2) + " is the value for 3 <= 2");

        // standard bitwise operators
        /*
        ~       Unary bitwise complement
        <<      Signed left shift
        >>      Signed right shift
        >>>     Unsigned right shift
        &       Bitwise AND
        ^       Bitwise exclusive OR
        |       Bitwise inclusive OR
        */

        //increments
        var i = 0;
        trace("Increments and decrements");
        trace(i++); //i = 1. Post-Incrementation
        trace(++i); //i = 2. Pre-Incrementation
        trace(i--); //i = 1. Post-Decrementation
        trace(--i); //i = 0. Pre-Decrementation

        //////////////////////////////////////////////////////////////////
        // Control Structures
        //////////////////////////////////////////////////////////////////
        trace("***CONTROL STRUCTURES***");

        // if statements
        var j = 10;
        if (j == 10){
            trace("this is printed");
        } else if (j > 10){
            trace("not greater than 10, so not printed");
        } else {
            trace("also not printed.");
        }

        // there is also a "ternary" if:
        (j == 10) ?  trace("equals 10") : trace("not equals 10");

        /*
           Finally, there is another form of control structures that operates
           at compile time:  conditional compilation.
         */
#if neko
        trace('hello from neko');
#elseif js
        trace('hello from js');
#else
        trace('hello from another platform!');
#end
        /*
           The compiled code will change depending on the platform target.
           Since we're compiling for neko (-x or -neko), we only get the neko
           greeting.
         */


        trace("Looping and Iteration");

        // while loop
        var k = 0;
        while(k < 100){
            // trace(counter); // will print out numbers 0-99
            k++;
        }

        // do-while loop
        var  l = 0;
        do{
            trace("do statement always runs at least once");
        } while (l > 0);

        // for loop
        /*
           There is no c-style for loop in Haxe, because they are prone
           to error, and not necessary.  Instead, Haxe has a much simpler
           and safer version that uses Iterators (more on those later).
         */
        var m = [1,2,3];
        for (val in m){
            trace(val + " is the value for val in the m array");
        }

        // Note that you can iterate on an index using a range
        // (more on ranges later as well)
        var n = ['foo', 'bar', 'baz'];
        for (val in 0...n.length){
            trace(val + " is the value for val (an index for n)");
        }


        trace("Array Comprehensions");

        // Array comprehensions give you the ability to iterate over arrays
        // while also creating filters and modifications.
        var filtered_n = [for (val in n) if (val != "foo") val];
        trace(filtered_n + " is the value for filtered_n");

        var modified_n = [for (val in n) val += '!'];
        trace(modified_n + " is the value for modified_n");

        var filtered_and_modified_n = [for (val in n) if (val != "foo") val += "!"];
        trace(filtered_and_modified_n + " is the value for filtered_and_modified_n");

        //////////////////////////////////////////////////////////////////
        // Switch Statements (Value Type)
        //////////////////////////////////////////////////////////////////
        trace("***SWITCH STATEMENTS (VALUE TYPES)***");

        /*
           Switch statements in Haxe are very powerful.  In addition to working
           on basic values like strings and ints, they can also work on the
           generalized algebraic data types in enums (more on enums later).
           Here's some basic value examples for now:
         */
        var my_dog_name = "fido";
        var favorite_thing  = "";
        switch(my_dog_name){
            case "fido" : favorite_thing = "bone";
            case "rex"  : favorite_thing = "shoe";
            case "spot" : favorite_thing = "tennis ball";
            default     : favorite_thing = "some unknown treat";
            // case _   : favorite_thing = "some unknown treat"; // same as default
        }
        // The "_" case above is a "wildcard" value
        // that will match anything.

        trace("My dog's name is " + my_dog_name
                + ", and his favorite thing is a: "
                + favorite_thing);

        //////////////////////////////////////////////////////////////////
        // Expression Statements
        //////////////////////////////////////////////////////////////////
        trace("***EXPRESSION STATEMENTS***");

        /*
           Haxe control statements are very powerful because every statement
           is also an expression, consider:
        */

        // if statements
        var k = if (true) 10 else 20;

        trace("k equals ", k); // outputs 10

        var other_favorite_thing = switch(my_dog_name) {
            case "fido" : "teddy";
            case "rex"  : "stick";
            case "spot" : "football";
            default     : "some unknown treat";
        }

        trace("My dog's name is " + my_dog_name
                + ", and his other favorite thing is a: "
                + other_favorite_thing);

        //////////////////////////////////////////////////////////////////
        // Converting Value Types
        //////////////////////////////////////////////////////////////////
        trace("***CONVERTING VALUE TYPES***");

        // You can convert strings to ints fairly easily.

        // string to integer
        Std.parseInt("0"); // returns 0
        Std.parseFloat("0.4"); // returns 0.4;

        // integer to string
        Std.string(0); // returns "0";
        // concatenation with strings will auto-convert to string.
        0 + "";  // returns "0";
        true + ""; // returns "true";
        // See documentation for parsing in Std for more details.


        //////////////////////////////////////////////////////////////////
        // Dealing with Types
        //////////////////////////////////////////////////////////////////

        /*

           As mentioned before, Haxe is a statically typed language.  All in
           all, static typing is a wonderful thing.  It enables
           precise autocompletions, and can be used to thoroughly check the
           correctness of a program.  Plus, the Haxe compiler is super fast.

           *HOWEVER*, there are times when you just wish the compiler would let
           something slide, and not throw a type error in a given case.

           To do this, Haxe has two separate keywords.  The first is the
           "Dynamic" type:
         */
        var dyn: Dynamic = "any type of variable, such as this string";

        /*
           All that you know for certain with a Dynamic variable is that the
           compiler will no longer worry about what type it is. It is like a
           wildcard variable:  You can pass it instead of any variable type,
           and you can assign any variable type you want.

           The other more extreme option is the "untyped" keyword:
         */

            untyped {
                var x:Int = 'foo'; // this can't be right!
                var y:String = 4; // madness!
            }

        /*
           The untyped keyword operates on entire *blocks* of code, skipping
           any type checks that might be otherwise required. This keyword should
           be used very sparingly, such as in limited conditionally-compiled
           situations where type checking is a hindrance.

           In general, skipping type checks is *not* recommended.  Use the
           enum, inheritance, or structural type models in order to help ensure
           the correctness of your program.  Only when you're certain that none
           of the type models work should you resort to "Dynamic" or "untyped".
         */

        //////////////////////////////////////////////////////////////////
        // Basic Object Oriented Programming
        //////////////////////////////////////////////////////////////////
        trace("***BASIC OBJECT ORIENTED PROGRAMMING***");


        /*
           Create an instance of FooClass.  The classes for this are at the
           end of the file.
         */
        var foo_instance = new FooClass(3);

        // read the public variable normally
        trace(foo_instance.public_any + " is the value for foo_instance.public_any");

        // we can read this variable
        trace(foo_instance.public_read + " is the value for foo_instance.public_read");
        // but not write it
        // foo_instance.public_read = 4; // this will throw an error if uncommented:
        // trace(foo_instance.public_write); // as will this.

        // calls the toString method:
        trace(foo_instance + " is the value for foo_instance");
        // same thing:
        trace(foo_instance.toString() + " is the value for foo_instance.toString()");


        /*
           The foo_instance has the "FooClass" type, while acceptBarInstance
           has the BarClass type.  However, since FooClass extends BarClass, it
           is accepted.
         */
        BarClass.acceptBarInstance(foo_instance);

        /*
           The classes below have some more advanced examples, the "example()"
           method will just run them here.
         */
        SimpleEnumTest.example();
        ComplexEnumTest.example();
        TypedefsAndStructuralTypes.example();
        UsingExample.example();

    }

}

/*
   This is the "child class" of the main LearnHaxe3 Class
 */
class FooClass extends BarClass implements BarInterface{
    public var public_any:Int; // public variables are accessible anywhere
    public var public_read (default, null): Int; // enable only public read
    public var public_write (null, default): Int; // or only public write
    public var property (get, set): Int; // use this style to enable getters/setters

    // private variables are not available outside the class.
    // see @:allow for ways around this.
    var _private:Int; // variables are private if they are not marked public

    // a public constructor
    public function new(arg:Int){
        // call the constructor of the parent object, since we extended BarClass:
        super();

        this.public_any = 0;
        this._private = arg;

    }

    // getter for _private
    function get_property() : Int {
        return _private;
    }

    // setter for _private
    function set_property(val:Int) : Int {
        _private = val;
        return val;
    }

    // special function that is called whenever an instance is cast to a string.
    public function toString(){
        return _private + " with toString() method!";
    }

    // this class needs to have this function defined, since it implements
    // the BarInterface interface.
    public function baseFunction(x: Int) : String{
        // convert the int to string automatically
        return x + " was passed into baseFunction!";
    }
}

/*
    A simple class to extend
*/
class BarClass {
    var base_variable:Int;
    public function new(){
        base_variable = 4;
    }
    public static function acceptBarInstance(b:BarClass){
    }
}

/*
    A simple interface to implement
*/
interface BarInterface{
    public function baseFunction(x:Int):String;
}

//////////////////////////////////////////////////////////////////
// Enums and Switch Statements
//////////////////////////////////////////////////////////////////

/*
   Enums in Haxe are very powerful.  In their simplest form, enums
   are a type with a limited number of states:
 */

enum SimpleEnum {
    Foo;
    Bar;
    Baz;
}

//   Here's a class that uses it:

class SimpleEnumTest{
    public static function example(){
        var e_explicit:SimpleEnum = SimpleEnum.Foo; // you can specify the "full" name
        var e = Foo; // but inference will work as well.
        switch(e){
            case Foo: trace("e was Foo");
            case Bar: trace("e was Bar");
            case Baz: trace("e was Baz"); // comment this line to throw an error.
        }

        /*
           This doesn't seem so different from simple value switches on strings.
           However, if we don't include *all* of the states, the compiler will
           complain.  You can try it by commenting out a line above.

           You can also specify a default for enum switches as well:
         */
        switch(e){
            case Foo: trace("e was Foo again");
            default : trace("default works here too");
        }
    }
}

/*
    Enums go much further than simple states, we can also enumerate
    *constructors*, but we'll need a more complex enum example
 */
enum ComplexEnum{
    IntEnum(i:Int);
    MultiEnum(i:Int, j:String, k:Float);
    SimpleEnumEnum(s:SimpleEnum);
    ComplexEnumEnum(c:ComplexEnum);
}
// Note: The enum above can include *other* enums as well, including itself!
// Note: This is what's called *Algebraic data type* in some other languages.

class ComplexEnumTest{
    public static function example(){
        var e1:ComplexEnum = IntEnum(4); // specifying the enum parameter
        /*
           Now we can switch on the enum, as well as extract any parameters
           it might of had.
         */
        switch(e1){
            case IntEnum(x) : trace('$x was the parameter passed to e1');
            default: trace("Shouldn't be printed");
        }

        // another parameter here that is itself an enum... an enum enum?
        var e2 = SimpleEnumEnum(Foo);
        switch(e2){
            case SimpleEnumEnum(s): trace('$s was the parameter passed to e2');
            default: trace("Shouldn't be printed");
        }

        // enums all the way down
        var e3 = ComplexEnumEnum(ComplexEnumEnum(MultiEnum(4, 'hi', 4.3)));
        switch(e3){
            // You can look for certain nested enums by specifying them explicitly:
            case ComplexEnumEnum(ComplexEnumEnum(MultiEnum(i,j,k))) : {
                trace('$i, $j, and $k were passed into this nested monster');
            }
            default: trace("Shouldn't be printed");
        }
        /*
           Check out "generalized algebraic data types" (GADT) for more details
           on why these are so great.
         */
    }
}

class TypedefsAndStructuralTypes {
    public static function example(){
        /*
           Here we're going to use typedef types, instead of base types.
           At the top we've declared the type "FooString" to mean a "String" type.
         */
        var t1:FooString = "some string";

        /*
           We can use typedefs for "structural types" as well.  These types are
           defined by their field structure, not by class inheritance.  Here's
           an anonymous object with a String field named "foo":
         */

        var anon_obj = { foo: 'hi' };

        /*
           The anon_obj variable doesn't have a type declared, and is an
           anonymous object according to the compiler.  However, remember back at
           the top where we declared the FooObj typedef?  Since anon_obj matches
           that structure, we can use it anywhere that a "FooObject" type is
           expected.
         */

        var f = function(fo:FooObject){
            trace('$fo was passed in to this function');
        }
        f(anon_obj); // call the FooObject signature function with anon_obj.

        /*
           Note that typedefs can have optional fields as well, marked with "?"

           typedef OptionalFooObj = {
                ?optionalString: String,
                requiredInt: Int
           }
         */

        /*
           Typedefs work well with conditional compilation.  For instance,
           we could have included this at the top of the file:

#if( js )
        typedef Surface = js.html.CanvasRenderingContext2D;
#elseif( nme )
        typedef Surface = nme.display.Graphics;
#elseif( !flash9 )
        typedef Surface = flash8.MovieClip;
#elseif( java )
        typedef Surface = java.awt.geom.GeneralPath;
#end

        That would give us a single "Surface" type to work with across
        all of those platforms.
        */
    }
}

class UsingExample {
    public static function example() {

        /*
           The "using" import keyword is a special type of class import that
           alters the behavior of any static methods in the class.

           In this file, we've applied "using" to "StringTools", which contains
           a number of static methods for dealing with String types.
         */
        trace(StringTools.endsWith("foobar", "bar") + " should be true!");

        /*
           With a "using" import, the first argument type is extended with the
           method.  What does that mean?  Well, since "endsWith" has a first
           argument type of "String", that means all String types now have the
           "endsWith" method:
         */
        trace("foobar".endsWith("bar") + " should be true!");

        /*
           This technique enables a good deal of expression for certain types,
           while limiting the scope of modifications to a single file.

           Note that the String instance is *not* modified in the run time.
           The newly attached method is not really part of the attached
           instance, and the compiler still generates code equivalent to a
           static method.
         */
      }

}

```

We're still only scratching the surface here of what Haxe can do.  For a formal
overiew of all Haxe features, checkout the [online
manual](http://haxe.org/manual), the [online api](http://api.haxe.org/), and
"haxelib", the [haxe library repo] (http://lib.haxe.org/).

For more advanced topics, consider checking out:

* [Abstract types](http://haxe.org/manual/abstracts)
* [Macros](http://haxe.org/manual/macros), and [Compiler Macros](http://haxe.org/manual/macros_compiler)
* [Tips and Tricks](http://haxe.org/manual/tips_and_tricks)


Finally, please join us on [the mailing list](https://groups.google.com/forum/#!forum/haxelang), on IRC [#haxe on
freenode](http://webchat.freenode.net/), or on
[Google+](https://plus.google.com/communities/103302587329918132234).


---
category: tool
tool: amd
contributors:
    - ["Frederik Ring", "https://github.com/m90"]
filename: learnamd-hd.js
lang: hd
---
## एएमडी के साथ प्रारंभ करना

एपीआई को परिभाषित करने के लिए एक तंत्र को निर्दिष्ट ** ** अतुल्यकालिक मॉड्यूल परिभाषा
जावास्क्रिप्ट मॉड्यूल ऐसे मॉड्यूल और इसकी अतुल्यकालिक निर्भरता से भरा हुआ है। यह ब्राउज़र पर्यावरण जहां के लिए विशेष रूप से अच्छी तरह से अनुकूल है, और प्रदर्शन , प्रयोज्य, डीबगिंग, और क्रॉस-डोमेन जैसे मॉड्यूल्स को जल्दी सिंक्रनाइज़ लोडिंग करता hai।

### मूल अवधारणा
```javascript
// बुनियादी एएमडी एपीआई दो तरीकों लेकिन कुछ भी नहीं होते : ` define` और` require`
// और सभी मॉड्यूल परिभाषा और खपत के बारे में है :
// `define` एक मॉड्यूल को परिभाषित करता है
// ` require` निर्भरता का एक सेट का आयात करता है और
// पारित कर दिया कॉलबैक में उन्हें सेवन करती है

// एक नया नाम देकर हम मॉड्यूल को परिभाषित करने का उपयोग करके शुरू करते हैं
// जिसकी कोई निर्भरता है । हम एक नाम से गुजर रहा है ऐसा करेंगे
// और एक कारखाने समारोह को परिभाषित करने के लिए :
define('awesomeAMD', function(){
  var isAMDAwesome = function(){
    return true;
  };
// एक मॉड्यूल के कारखाने समारोह की मान है
  // जब प्राप्त होगा क्या अन्य मॉड्यूल या आवश्यकता कॉल
  // हमारे ` awesomeAMD` मॉड्यूल की आवश्यकता होती है ।
  // निर्यात मूल्य कुछ भी हो सकता है, (निर्माता ) काम करता है,
  // वस्तुओं, पुरातन, (जो कि बहुत ज्यादा मदद नहीं करेगा , हालांकि) भी अपरिभाषित ।
  return isAMDAwesome;
});

// अब, हमारे ` awesomeAMD` मॉड्यूल पर निर्भर करता है कि किसी अन्य मॉड्यूल परिभाषित करते हैं।
// हमारे परिभाषित करने के लिए एक अतिरिक्त तर्क है कि नोटिस
अब // मॉड्यूल की निर्भरता :
define('loudmouth', ['awesomeAMD'], function(awesomeAMD){
// निर्भरता कारखाने के तर्कों को पारित हो जाएगा
  // क्रम में वे निर्दिष्ट कर रहे हैं
  var tellEveryone = function(){
    if (awesomeAMD()){
      alert('This is sOoOo rad!');
    } else {
      alert('Pretty dull, isn\'t it?');
    }
  };
  return tellEveryone;
});

// हम अब परिभाषित का उपयोग करने के लिए कैसे जानते हैं के रूप में, के लिए ` require` का उपयोग करते हैं
// हमारे कार्यक्रम बंद किक । ` require` के हस्ताक्षर है :(arrayOfDependencies, callback)`.
require(['loudmouth'], function(loudmouth){
  loudmouth();
});

// इस ट्यूटोरियल रन कोड बनाने के लिए है, चलो एक बहुत ही बुनियादी लागू करते हैं
// (गैर अतुल्यकालिक ) की मौके पर यहीं एएमडी के संस्करण:
function define(name, deps, factory){
// निर्भरता के बिना मॉड्यूल नियंत्रित किया जाता है कैसे नोटिस
  define[name] = require(factory ? deps : [], factory || deps);
}

function require(deps, callback){
  var args = [];
 // पहले की जरूरत है सभी निर्भरता पुनः प्राप्त करते हैं
  // आवश्यकता कॉल द्वारा
  for (var i = 0; i < deps.length; i++){
    args[i] = define[deps[i]];
  }
// सभी कॉलबैक की निर्भरता को संतुष्ट
  return callback.apply(null, args);
}
// आप यहाँ कार्रवाई में इस कोड को देख सकते हैं: http://jsfiddle.net/qap949pd/
```

### Require.js के साथ वास्तविक दुनिया के उपयोग

परिचयात्मक उदाहरण के विपरीत, ` require.js` (सबसे लोकप्रिय एएमडी पुस्तकालय ) वास्तव में लागू करता है ** ** Amd ** में  *A * **, आप XHR के माध्यम से  मॉड्यूल और उनकी निर्भरता लोड करने के लिए सक्षम करने के लिए :
```javascript
/* file: app/main.js */
require(['modules/someClass'], function(SomeClass){
  // निर्भरता लोड होने तक कॉलबैक टाल दिया गया है
  var thing = new SomeClass();
});
console.log('So here we are, waiting!'); // this will run first
```

परंपरा के अनुसार , आप आमतौर पर एक फाइल में एक मॉड्यूल में ही रखते है । ` require.js` फ़ाइल पथ पर आधारित मॉड्यूल नाम को हल कर सकते हैं , तो आप अपने मॉड्यूल के नाम करने की जरूरत नहीं है , लेकिन बस उनके स्थान का उपयोग कर उन्हें संदर्भित कर सकते हैं । उदाहरण के `में someClass` आपके विन्यास की ` baseUrl` के सापेक्ष ` modules` फ़ोल्डर में माना गया है :

* app/
  * main.js
  * modules/
    * someClass.js
    * someHelpers.js
    * ...
  * daos/
    * things.js
    * ...

इसका मतलब यह है कि हम एक मॉड्यूल आईडी निर्दिष्ट किए बिना ` someClass` परिभाषित कर सकते हैं :

```javascript
/* file: app/modules/someClass.js */
define(['daos/things', 'modules/someHelpers'], function(thingsDao, helpers){
  // module definition, of course, will also happen asynchronously
  function SomeClass(){
    this.method = function(){/**/};
    // ...
  }
  return SomeClass;
});
```
अपने ` main.js` में डिफ़ॉल्ट पथ मानचित्रण व्यवहार का उपयोग ` requirejs.config ( configObj ) ` में परिवर्तन करने के लिए:

```javascript
/* file: main.js */
requirejs.config({
  baseUrl : 'app',
  paths : {
    // आप भी अन्य स्थानों से मॉड्यूल लोड कर सकते हैं
    jquery : '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
});
require(['jquery', 'coolLibFromBower', 'modules/someHelpers'], function($, coolLib, helpers){
  //एक ` main` फ़ाइल में कम से कम एक बार की आवश्यकता को फोन करने की जरूरत है,
  // अन्यथा कोई कोड कभी चलेंगे
  coolLib.doFancyStuffWith(helpers.transform($('#foo')));
});
```
` require.js` आधारित क्षुधा आमतौर पर एक डाटा विशेषता के रूप में ` require.js` स्क्रिप्ट टैग को पारित कर दिया है कि एक एकल प्रवेश बिंदु (` main.js` ) होगा। यह स्वचालित रूप से भरी हुई है और pageload पर क्रियान्वित किया जाएगा :

```html
<!DOCTYPE html>
<html>
<head>
  <title>A hundred script tags? Never again!</title>
</head>
<body>
  <script src="require.js" data-main="app/main"></script>
</body>
</html>
```

### R.js का उपयोग कर एक पूरी परियोजना का अनुकूलन

कई लोगों को विकास के दौरान समझदार कोड संगठन के लिए एएमडी का उपयोग कर पसंद करते हैं, लेकिन अभी भी पेज लोड पर XHRs के सैकड़ों करने के बजाय उत्पादन में एक भी स्क्रिप्ट फ़ाइल जहाज करने के लिए चाहते हैं।

(राइनो भी समर्थन किया है, तो आप शायद Node.js में चलेगा ) ` require.js` ( अपनी परियोजना की निर्भरता ग्राफ का विश्लेषण , और अपने सभी मॉड्यूल युक्त एक एकल फाइल निर्माण कर सकते हैं कि ` r.js` नामक एक स्क्रिप्ट के साथ आता है ठीक से minified और उपभोग के लिए तैयार है, ) नाम दिया है।
Install it using `npm`:
```shell
$ npm install requirejs -g
```

अब आप एक विन्यास फाइल के साथ फ़ीड कर सकते हैं:
```shell
$ r.js -o app.build.js
```

हमारे ऊपर के उदाहरण के लिए विन्यास की तरह लग सकता है:
```javascript
/* file : app.build.js */
({
  name : 'main', // प्रवेश बिंदु के नाम
  out : 'main-built.js', // फ़ाइल का नाम करने के लिए उत्पादन में लिखने के लिए
  baseUrl : 'app',
  paths : {
    // ` empty :` का उपयोग कर , यह अभी भी समन्वय से लोड किया जाना चाहिए कि r.js बताता है
    // main.js में निर्दिष्ट स्थान
    jquery : 'empty:',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
})
```

उत्पादन में बनाया फ़ाइल का उपयोग करने के लिए, बस ` Data-main` स्वैप:
```html
<script src="require.js" data-main="app/main-built"></script>
```

एक अविश्वसनीय रूप से विस्तृत [निर्माण विकल्पों में से अवलोकन] (https://github.com/jrburke/r.js/blob/master/build/example.build.js) GitHub रेपो में उपलब्ध है।

### विषय इस ट्यूटोरियल में शामिल नहीं
* [लोडर प्लगइन्स / रूपांतरण] (http://requirejs.org/docs/plugins.html)
* [CommonJS शैली लोड हो रहा है और निर्यात] (http://requirejs.org/docs/commonjs.html)
* [उन्नत विन्यास] (http://requirejs.org/docs/api.html#config)
* [शिम विन्यास (गैर एएमडी मॉड्यूल लोडिंग)] (http://requirejs.org/docs/api.html#config-shim)
* [सीएसएस लदान और require.js साथ अनुकूलन] (http://requirejs.org/docs/optimization.html#onecss)
* (Https://github.com/jrburke/almond) [बनाता है के लिए almond.js का प्रयोग]

### अग्रिम पठन:

* [सरकारी कल्पना] (https://github.com/amdjs/amdjs-api/wiki/AMD)
* [क्यों एएमडी?] (Http://requirejs.org/docs/whyamd.html)
* [यूनिवर्सल मॉड्यूल परिभाषा] (https://github.com/umdjs/umd)

### कार्यान्वयन:

* [Require.js] (http://requirejs.org)
* [डोजो टूलकिट] (http://dojotoolkit.org/documentation/tutorials/1.9/modules/)
* [Cujo.js] (http://cujojs.com/)
* [Curl.js] (https://github.com/cujojs/curl)
* [Lsjs] (https://github.com/zazl/lsjs)
* [एमडी] (https://github.com/alexlawrence/mmd)
---
language: D
filename: learnd-hd.d
contributors:
    - ["Nick Papanastasiou", "www.nickpapanastasiou.github.io"]
lang: hd
---

```c
//क्या आ रहा है पता है ...
module hello;

import std.stdio;

void main(string[] args) {
    writeln("Hello, World!");
}
```

अगर आप मेरे जैसे हैं और इंटरनेट पर समय बहुत अधिक समय खर्च करते हैं, तो आप बाधाओं के बारे में सुना है
के बारे में [डी ] ( http://dlang.org/ )। डी प्रोग्रामिंग भाषा में एक आधुनिक, सामान्य प्रयोजन है ,
सब कुछ के लिए समर्थन कम स्तर की सुविधाओं से करने के साथ बहु - प्रतिमान भाषा
अर्थपूर्ण उच्च स्तरीय चीजें ।

D सक्रिय रूप से सुपर स्मार्ट लोगों का एक बड़ा समूह द्वारा विकसित की है और नेतृत्व द्वारा किया जाता है
[ वाल्टर ब्राइट ] ( https://en.wikipedia.org/wiki/Walter_Bright ) और
[ आंद्रेई Alexandrescu ] ( https://en.wikipedia.org/wiki/Andrei_Alexandrescu )।
जिस तरह की है कि सभी के साथ बाहर, चलो कुछ उदाहरणों पर गौर करते हैं!


```c
import std.stdio;

void main() {

    for(int i = 0; i < 10000; i++) {
        writeln(i);
    }

    // 'auto' can be used for inferring types.
    auto n = 1;

    // संख्यात्मक literals स्पष्टता के लिए एक अंकों विभाजक के रूप में '_' का उपयोग कर सकते हैं।
    while(n < 10_000) {
        n += n;
    }

    do {
        n -= (n / 2);
    } while(n > 0);
    // लिए और जब तक अच्छा कर रहे हैं, लेकिन D में हम 'foreach' छोरों पसंद करते हैं।
    // '..' पहला मान सहित एक सतत श्रृंखला बनाता है,
    // लेकिन पिछले छोड़कर।
    foreach(i; 1..1_000_000) {
        if(n % 2 == 0)
            writeln(i);
    }

    // वहाँ भी 'foreach_reverse' आप पीछे की ओर पाश करना चाहते हैं।
    foreach_reverse(i; 1..int.max) {
        if(n % 2 == 1) {
            writeln(i);
        } else {
            writeln("No!");
        }
    }
}
```

हम ' struct`, `class`,` union`, और `` enum` साथ नए प्रकार परिभाषित कर सकते हैं। Structs और unions
मूल्य से कार्य करने के लिए पारित कर रहे हैं (यानी नकल) और वर्गों के संदर्भ द्वारा पारित कर रहे हैं। इसके अलावा,
हम प्रकारों और मानों दोनों पर करने के लिए टेम्पलेट का उपयोग कर सकते हैं!

```c
// इधर, 'T' एक प्रकार पैरामीटर है। लगता है कि '&lt;+T&gt;' C++ / C/ Java से।
struct LinkedList(T) {
    T data = null;

    // '!'का प्रयोग करें , एक पैरामिट्रीकृत प्रकार इन्स्तांत । फिर, '<T >' लगता है।
    LinkedList!(T)* next;
}

class BinTree(T) {
    T data = null;

// केवल एक टेम्पलेट पैरामीटर नहीं है, तो  , हम कोष्ठकों छोड़ सकते हैं।
    BinTree!T left;
    BinTree!T right;
}

enum Day {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

// उपयोग उर्फ प्रकार (alias) के लिए संक्षिप्त बनाने के लिए।
alias IntList = LinkedList!int;
alias NumTree = BinTree!double;

//हम के रूप में अच्छी तरह से कार्य टेम्पलेट्स बना सकते हैं!
T max(T)(T a, T b) {
    if(a < b)
        return b;

    return a;
}

// संदर्भ द्वारा पारित सुनिश्चित करने के लिए रेफरी कीवर्ड का प्रयोग करें । यही कारण है कि यहां तक ​​कि 'A' और 'B' , तो है
//मान प्रकार वे हमेशा ' swap()' के संदर्भ द्वारा पारित हो जाएगा  हैं ।
void swap(T)(ref T a, ref T b) {
    auto temp = a;

    a = b;
    b = temp;
}

// टेम्पलेट्स के साथ, हम भी मूल्यों पर परमेटेराइज़  कर सकते हैं , न सिर्फ types.With टेम्पलेट्स, हम भी नहीं है, बस प्रकार , मूल्यों पर parameterize कर सकते हैं।
class Matrix(uint m, uint n, T = int) {
    T[m] rows;
    T[n] columns;
}

auto mat = new Matrix!(3, 3);

```

Classes की बात हो रही है , एक दूसरे के लिए गुणों के बारे में बात करते हैं। एक संपत्ति
एक value की तरह कार्य कर सकते हैं कि एक समारोह में मोटे तौर पर है, इसलिए हम कर सकते हैं
के शब्दों के साथ पॉड संरचनाओं की वाक्य रचना (` structure.x = 7` ) है
मनुष्य और सेटर तरीकों ( ` object.setX (7) `) !

```c
// Consider a class parameterized on types 'T' & 'U'.
class MyClass(T, U) {
    T _data;
    U _other;
}

// And "getter" and "setter" methods like so:
class MyClass(T, U) {
    T _data;
    U _other;

    // भवन निर्माताओं हमेशा नामित कर रहे हैं 'this'.
    this(T t, U u) {
        //यह नीचे सेटर तरीकों से मुलाकात करेंगे।
        data = t;
        other = u;
    }

    // getters
    @property T data() {
        return _data;
    }

    @property U other() {
        return _other;
    }

    // setters
    @property void data(T t) {
        _data = t;
    }

    @property void other(U u) {
        _other = u;
    }
}

//और हम इस तरह से उन का उपयोग करें :
void main() {
    auto mc = new MyClass!(int, string)(7, "seven");

   करने के लिए लिखने के लिए मानक पुस्तकालय से
   // आयात ' stdio ' मॉड्यूल
    // सांत्वना (आयात एक गुंजाइश के लिए स्थानीय हो सकता है) ।
    import std.stdio;

    // Call the getters to fetch the values.
    writefln("Earlier: data = %d, str = %s", mc.data, mc.other);

    // Call the setters to assign new values.
    mc.data = 8;
    mc.other = "eight";

    // Call the getters again to fetch the new values.
    writefln("Later: data = %d, str = %s", mc.data, mc.other);
}
```

गुणों के साथ, हम तर्क की किसी भी राशि को जोड़ सकते हैं
हमारे मनुष्य और सेटर तरीकों, और की साफ वाक्य रचना रखना
सीधे सदस्यों तक पहुँचने !

हमारे निपटान पर अन्य वस्तु उन्मुख उपहार
` interface`s , ` सार class`es शामिल
और ` तरीकों override`ing । डी सिर्फ जावा की तरह विरासत करता है:
आप कृपया के रूप में कई इंटरफेस को लागू करने, एक वर्ग का विस्तार ।

हम डी एस OOP सुविधाओं देखा , लेकिन स्विच गियर छोड़ दिया । डी प्रस्तावों
प्रथम श्रेणी के कार्यों के साथ कार्यात्मक प्रोग्रामिंग, ` pure`
काम करता है, और अपरिवर्तनीय डेटा । इसके अलावा, अपने पसंदीदा के सभी
कार्यात्मक एल्गोरिदम ( नक्शा, फिल्टर , कम करने और मित्र हो सकते हैं)
अद्भुत ` std.algorithm` मॉड्यूल में पाया!

```c
import std.algorithm : map, filter, reduce;
import std.range : iota; // builds an end-exclusive range

void main() {
    // हम भी ints के वर्गों की एक सूची का योग मुद्रित करना चाहते हैं
    // 1 से 100 के लिए आसान करने के लिए!

    // बस टेम्पलेट पैरामीटर के रूप में लैम्ब्डा भाव के पास!
    // आप आप की तरह किसी भी पुराने समारोह पारित कर सकते हैं , लेकिन lambdas यहाँ सुविधाजनक हैं।
    auto num = iota(1, 101).filter!(x => x % 2 == 0)
                           .map!(y => y ^^ 2)
                           .reduce!((a, b) => a + b);

    writeln(num);
}
```

हम NUM गणना करने के लिए एक अच्छा Haskellian पाइपलाइन का निर्माण करने के लिए मिला सूचना कैसे ?
यही कारण है कि एक डी नवाचार करने के लिए धन्यवाद वर्दी समारोह कॉल सिंटेक्स के रूप में जानते हैं।
UFCS के साथ, हम एक विधि के रूप में एक समारोह कॉल लिखने के लिए चुन सकते हैं
या मुफ्त समारोह कॉल ! वाल्टर इस पर एक अच्छा लेख लिखा था
[यहाँ ।] ( http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394 )
संक्षेप में, आप जिनकी पहली पैरामीटर कार्यों कॉल कर सकते हैं
एक विधि के रूप में ग्रुप ए की किसी भी अभिव्यक्ति पर कुछ प्रकार एक की है ।

मैं समानता चाहते । समानता की तरह कोई और? ज़रूर तुम करना। चलो कुछ करते हैं!
```c
import std.stdio;
import std.parallelism : parallel;
import std.math : sqrt;

void main() {
 // हम हमारे सरणी में वर्गमूल हर नंबर ले जाना चाहता हूँ ,
 // हम उपलब्ध है के रूप में और के रूप में कई कोर का लाभ ले।
    auto arr = new double[1_000_000];

   // संदर्भ के द्वारा एक सूचकांक , और एक सरणी तत्व का प्रयोग
    // और सिर्फ सरणी पर समानांतर फोन!
    foreach(i, ref elem; parallel(arr)) {
        ref = sqrt(i + 1.0);
    }
}


```
---
language: HQ9+
filename: hq9+.html
contributors:
    - ["Alexey Nazaroff", "https://github.com/rogaven"]
---

HQ9+ is a joke programming language created by Cliff Biffle. It has only four commands and it isn't Turing-complete.

```
There is only 4 commands, represented by next characters
H: print "Hello, world!"
Q: print the program's source code (a Quine)
9: print the lyrics to "99 Bottles of Beer"
+: add one to the accumulator (the value of the accumulator cannot be accessed)
Any other character is ignored.

Ok. Let's write some program:
  HQ9

Result:
  Hello world!
  HQ9

HQ9+ is very simple, but allows you to do some things that are very difficult
in other languages. For example, here is a program that creates three copies of
itself on the screen:
  QQQ

This produces:
  QQQ
  QQQ
  QQQ
```

And that's all. There are a lot of interpreters for HQ9+. Below you can find one of them

+ [One of online interpreters](https://almnet.de/esolang/hq9plus.php)
+ [HQ9+ official website](http://cliffle.com/esoterica/hq9plus.html)
---
language: html
filename: learnhtml.html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
translators:
    - ["Robert Steed", "https://github.com/robochat"]
---

HTML stands for HyperText Markup Language. 
It is a language which allows us to write pages for the world wide web.
It is a markup language, it enables us to write webpages using code to indicate how text and data should be displayed.
In fact, html files are simple text files.
What is this markup? It is a method of organising the page's data by surrounding it with opening tags and closing tags.
This markup serves to give significance to the text that it encloses. 
Like other computer languages, HTML has many versions. Here we will talk about HTML5.

**NOTE :**  You can test the different tags and elements as you progress through the tutorial on a site like [codepen](http://codepen.io/pen/) in order to see their effects, understand how they work and familiarise yourself with the language.
This article is concerned principally with HTML syntax and some useful tips.


```html
<!-- Comments are enclosed like this line! -->

<!-- #################### The Tags #################### -->
   
<!-- Here is an example HTML file that we are going to analyse. -->

<!doctype html>
	<html>
		<head>
			<title>My Site</title>
		</head>
		<body>
			<h1>Hello, world!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">Come look at what this shows</a>
			<p>This is a paragraph.</p>
			<p>This is another paragraph.</p>
			<ul>
				<li>This is an item in a non-enumerated list (bullet list)</li>
				<li>This is another item</li>
				<li>And this is the last item on the list</li>
			</ul>
		</body>
	</html>

<!-- An HTML file always starts by indicating to the browser that the page is HTML. -->
<!doctype html>

<!-- After this, it starts by opening an <html> tag. -->
<html>

<!-- that will be closed at the end of the file with </html>. -->
</html>

<!-- Nothing should appear after this final tag. -->

<!-- Inside (between the opening and closing tags <html></html>), we find: -->

<!-- A header defined by <head> (it must be closed with </head>). -->
<!-- The header contains some description and additional information which are not displayed; this is metadata. -->

<head>
	<title>My Site</title><!-- The tag <title> indicates to the browser the title to show in browser window's title bar and tab name. -->
</head>

<!-- After the <head> section, we find the tag - <body> -->
<!-- Until this point, nothing described will show up in the browser window. -->
<!-- We must fill the body with the content to be displayed. -->

<body>
	<h1>Hello, world!</h1> <!-- The h1 tag creates a title. -->
	<!-- There are also subtitles to <h1> from the most important (h2) to the most precise (h6). -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">Come look at what this shows</a> <!-- a hyperlink to the url given by the attribute href="" -->
	<p>This is a paragraph.</p> <!-- The tag <p> lets us include text in the html page. -->
	<p>This is another paragraph.</p>
	<ul> <!-- The tag <ul> creates a bullet list. -->
	<!-- To have a numbered list instead we would use <ol> giving 1. for the first element, 2. for the second, etc. -->
		<li>This is an item in a non-enumerated list (bullet list)</li>
		<li>This is another item</li>
		<li>And this is the last item on the list</li>
	</ul>
</body>

<!-- And that's it, creating an HTML file can be simple. -->

<!-- But it is possible to add many additional types of HTML tags. -->

<!-- To insert an image. -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- The source of the image is indicated using the attribute src="" -->
<!-- The source can be an URL or even path to a file on your computer. -->

<!-- It is also possible to create a table. -->

<table> <!-- We open a <table> element. -->
	<tr> <!-- <tr> allows us to create a row. -->
		<th>First Header</th> <!-- <th> allows us to give a title to a table column. -->
		<th>Second Header</th>
	</tr>
	<tr>
		<td>first row, first column</td> <!-- <td> allows us to create a table cell. -->
		<td>first row, second column</td>
	</tr>
	<tr>
		<td>second row, first column</td>
		<td>second row, second column</td>
	</tr>
</table>

```

## Usage

HTML is written in files ending with `.html`.

## To Learn More 

* [wikipedia](https://en.wikipedia.org/wiki/HTML)
* [HTML tutorial](https://developer.mozilla.org/en-US/docs/Web/HTML)
* [W3School](http://www.w3schools.com/html/html_intro.asp)
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
  - ["Tamás Diószegi", "http://github.com/ditam"]
lang: hu-hu
filename: coffeescript-hu.coffee
---

A CoffeeScript egy apró nyelv ami egy-az-egyben egyenértékű Javascript kódra fordul, és így futásidőben már nem szükséges interpretálni.
Mint a JavaScript egyik követője, a CoffeeScript mindent megtesz azért, hogy olvasható, jól formázott és jól futó JavaScript kódot állítson elő, ami minden JavaScript futtatókörnyezetben jól működik.

Rézletekért lásd még a [CoffeeScript weboldalát](http://coffeescript.org/), ahol egy teljes CoffeScript tutorial is található.

```coffeescript
# A CoffeeScript egy hipszter nyelv.
# Követi több modern nyelv trendjeit.
# Így a kommentek, mint Ruby-ban és Python-ban, a szám szimbólummal kezdődnek.

###
A komment blokkok ilyenek, és közvetlenül '/ *' és '* /' jelekre fordítódnak
az eredményül kapott JavaScript kódban.

Mielőtt tovább olvasol, jobb, ha a JavaScript alapvető szemantikájával
tisztában vagy.

(A kód példák alatt kommentként látható a fordítás után kapott JavaScript kód.)
###

# Értékadás:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# Feltételes utasítások:
number = -42 if opposite #=> if(opposite) { number = -42; }

# Függvények:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

fill = (container, liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."
#=>var fill;
#
#fill = function(container, liquid) {
#  if (liquid == null) {
#    liquid = "coffee";
#  }
#  return "Filling the " + container + " with " + liquid + "...";
#};

# Szám tartományok:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# Objektumok:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#    "root": Math.sqrt,
#    "square": square,
#    "cube": function(x) { return x * square(x); }
#   };

# "Splat" jellegű függvény-paraméterek:
race = (winner, runners...) ->
  print winner, runners
#=>race = function() {
#    var runners, winner;
#    winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#    return print(winner, runners);
#  };

# Létezés-vizsgálat:
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# Tömb értelmezések: (array comprehensions)
cubes = (math.cube num for num in list)
#=>cubes = (function() {
#	  var _i, _len, _results;
#	  _results = [];
# 	for (_i = 0, _len = list.length; _i < _len; _i++) {
#		  num = list[_i];
#		  _results.push(math.cube(num));
#	  }
#	  return _results;
# })();

foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
#=>foods = ['broccoli', 'spinach', 'chocolate'];
#
#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
#  food = foods[_k];
#  if (food !== 'chocolate') {
#    eat(food);
#  }
#}
```

## További források

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: Go
lang: hu-hu
filename: learngo-hu.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
translators:
    - ["Szabó Krisztián", "https://github.com/thenonameguy/"]
    - ["Árpád Goretity", "https://github.com/H2CO3"]
---

A Go programozási nyelv az életszerű feladatok könnyebb elvégzése miatt született.
A mai legújabb programozási trendeket elkerülve,
praktikus megoldást nyújt a valós, üzleti problémákra.

C-szerű szintaktikával és statikus típuskezeléssel rendelkezik.
A fordító szempillantás alatt végez és egy gyorsan futó,statikus futtatható állományt hoz létre.
A nyelv könnyen érthető, folyamatok közötti csatornákon áthaladó üzenetekkel kommunikáló konkurens programozást tesz lehetővé, így könnyen ki lehet használni
a mai számítógépek több magos processzorait, ez nagy rendszerek építéséhez ideális.

A Go alap könyvtára mindenre területre kiterjed, ennek köszönhetően a nyelvnek egyre növekvő tábora van.

```go
// Egy soros komment
/* Több
   soros komment */

// Minden forrás fájl egy csomag-definícióval kezdődik, ez hasonlít a Python
// csomagkezelésére
// A main egy különleges csomagnév, ennek a fordítása futtatható állományt hoz
// létre egy könyvtár helyett.
package main

// Az import rész meghatározza melyik csomagokat kívánjuk használni ebben a
// forrásfájlban
import (
    "fmt"      // A Go alap könyvtárának része
    "net/http" // Beépített webszerver!
    "strconv"  // Stringek átalakítására szolgáló csomag
)

// Függvénydeklarálás, a main nevű függvény a program kezdőpontja.
func main() {
    // Println kiírja a beadott paramétereket a standard kimenetre.
    // Ha más csomagot függvényeit akarjuk használni, akkor azt jelezni kell a
    // csomag nevével
    fmt.Println("Hello world!")

    // Meghívunk egy másik függvényt ebből a csomagból
    beyondHello()
}

// A függvények paraméterei zárójelek között vannak.
// Ha nincsenek paraméterek, akkor is kötelező a zárójel-pár.
func beyondHello() {
    var x int // Változó deklaráció, használat előtt muszáj ezt megtenni.
    x = 3     // Változó értékadás
    // "Rövid" deklaráció is létezik, ez az érték alapján deklarálja,
    // definiálja és értéket is ad a változónak
    y := 4
    sum, prod := learnMultiple(x, y)        // a függvényeknek több
                                            // visszatérési értéke is lehet
    fmt.Println("sum:", sum, "prod:", prod) // egyszerű kiíratás
    learnTypes()
}

// A funkcióknak elnevezett visszatérési értékük is lehet
func learnMultiple(x, y int) (sum, prod int) {
    return x + y, x * y // visszatérünk két értékkel
    /*
    sum = x + y
    prod = x * y
    return
    Ez ugyanezzel az eredménnyel járt volna, mint a fenti sor.
    Üres return esetén, az elnevezett visszatérési változók 
    aktuális értékeikkel térnek vissza. */
}

// Beépített típusok
func learnTypes() {
    // Rövid deklarálás az esetek többségében elég lesz a változókhoz
    s := "Tanulj Go-t!" // string típus

    s2 := `A "nyers" stringekben lehetnek
    újsorok is!` // de ettől még ez is ugyanolyan string mint az s, nincs külön
                 // típusa

    // nem ASCII karakterek.  Minden Go forrás UTF-8 és a stringek is azok.
    g := 'Σ' // rúna(rune) típus, megegyezik az uint32-vel, egy UTF-8 karaktert
             // tárol

    f := 3.14195 // float64, az IEEE-754 szabványnak megfelelő 64-bites
                 // lebegőpontos szám
    c := 3 + 4i  // complex128, belsőleg két float64-gyel tárolva

    // Var szintaxis változótípus-definiálással
    var u uint = 7 // unsigned, az implementáció dönti el mekkora, akárcsak az
                   // int-nél
    var pi float32 = 22. / 7

    // Rövid deklarásnál átalakítás is lehetséges
    n := byte('\n') // byte típus, ami megegyezik az uint8-al

    // A tömböknek fordítás-időben fixált méretük van
    var a4 [4]int           // egy tömb 4 int-tel, mind 0-ra inicializálva
    a3 := [...]int{3, 1, 5} // egy tömb 3 int-tel, láthatóan inicalizálva egyedi
                            // értékekre

    // A "szeleteknek" (slices) dinamikus a méretük. A szeleteknek és a tömböknek is
    // megvannak az előnyeik de a szeleteket sokkal gyakrabban használjuk.
    s3 := []int{4, 5, 9}    // vesd össze a3-mal, nincsenek pontok.
    s4 := make([]int, 4)    // allokál 4 int-et, mind 0-ra inicializálva
    var d2 [][]float64      // ez csak deklaráció, semmi sincs még allokálva
    bs := []byte("a slice") // típus konverzió szintaxisa

    p, q := learnMemory() // deklarál két mutatót (p,q), két int-re
    fmt.Println(*p, *q)   // * követi a mutatót. Ez a sor kiírja a két int értékét.

    // A map a dinamikusan növelhető asszociatív tömb része a nyelvnek, hasonlít
    // a hash és dictionary típusokra más nyelvekben.
    m := map[string]int{"three": 3, "four": 4}
    m["one"] = 1

    // A felhasználatlan változók fordítás-idejű hibát okoznak a Go-ban.
    // Az aláhúzással "használod" a változókat, de eldobod az értéküket.
    _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
    // Kiíratás is természetesen használatnak minősül
    fmt.Println(s, c, a4, s3, d2, m)

    learnFlowControl()
}

// A Go nyelvben szemétgyűjtés (garbage collection) működik. Megtalálhatók benne
// mutatók, de nincs pointeraritmetika. Ez azt jelenti, hogy üres (null) mutatóval még
// mindig hibázhatsz, de hozzáadni/műveleteket végezni már nem lehet.
func learnMemory() (p, q *int) {
    // Elnevezett visszatérési változóknak int-re mutató a típusa
    p = new(int) // a beépített "new" funkció, egy típusnak elegendő memóriát
                 // allokál, és visszaad rá egy mutatót.
    // Az allokált int nullázva van, p többé nem üres mutató.
    s := make([]int, 20) // allokáljunk 20 int változót egy memóriaterületen.
    s[3] = 7             // adjunk értéket az egyiknek
    r := -2              // hozzánk létre egy lokális változót
    return &s[3], &r     // A & megadja a memóriacímét a változónak
}

func expensiveComputation() int {
    return 1e6
}

func learnFlowControl() {
    // Az elágazásoknak kötelező a kapcsos zárójel, a zárójel nem szükséges.
    if true {
        fmt.Println("megmondtam")
    }
    // A kód formátumát a nyelvvel járó "go" parancssori program "go fmt"
    // parancsa szabványosítja
    if false {
        // így lehet
    } else {
        // if/else-t csinálni
    }
    // Használjunk switchet a hosszabb elágazások alkalmazása helyett.
    x := 1
    switch x {
    case 0:
    case 1:
        // Az "esetek" nem "esnek át", tehát
    case 2:
        // ez nem fog lefutni, nincs szükség break-ekre.
    }
    // A for ciklus sem használ zárójeleket
    for x := 0; x < 3; x++ { 
        fmt.Println("iteráció", x)
    }
    // itt az x == 1.

    // A for az egyetlen ciklus fajta a Go-ban, de több formája van.
    for { // végtelen ciklus
        break    // csak vicceltem
        continue // soha nem fut le
    }

    //Akárcsak a for-nál, az if-nél is lehet rövid deklarálással egy lokális változót létrehozni, 
    //ami a blokk összes if/else szerkezetén keresztül érvényes marad. 
    if y := expensiveComputation(); y > x {
        x = y
    }
    // Függvényeket használhatjuk closure-ként is.
    xBig := func() bool {
        return x > 100 // a switch felett deklarált x-et használjuk itt
    }
    fmt.Println("xBig:", xBig()) // igaz (utoljára 1e6 lett az értéke az x-nek)
    x /= 1e5                     // így most már x == 10
    fmt.Println("xBig:", xBig()) // 10 pedig kisebb mint 100, tehát hamis

    // Ha nagyon-nagyon szükséges, akkor használhatjuk a jó öreg goto-t.
    goto love
love:

    learnInterfaces() // Itt kezdődnek az érdekes dolgok!
}

// Definiáljuk a Stringert egy olyan interfésznek, amelynek egy metódusa van, a
// String, ami visszatér egy stringgel.
type Stringer interface {
    String() string
}

// Definiáljuk a pair-t egy olyan struktúrának amelynek két int változója van,
// x és y.
type pair struct {
    x, y int
}

// Definiáljunk egy metódust a pair struktúrának, ezzel teljesítve a Stringer interfészt.
func (p pair) String() string { // p lesz a "fogadó" (receiver)
    // Sprintf az fmt csomag egy publikus függvénye, műkődése megegyezik a C-s
    // megfelelőjével. A pontokkal érjük el a mindenkori p struktúra elemeit
    return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
    // A kapcsos zárójellel jelezzük, hogy egyből inicializálni
    // szeretnénk a struktúra változóit a sorrendnek megfelelően.
    p := pair{3, 4}
    fmt.Println(p.String()) // meghívjuk a p String metódusát.
    var i Stringer          // deklaráljuk i-t Stringer típusú interfésznek
    i = p                   // lehetséges, mert a pair struktúra eleget tesz a
                            // Stringer interfésznek
    // Meghívjuk i String metódusát, az eredmény ugyanaz, mint az előbb.
    fmt.Println(i.String())

    // Az fmt csomag függvényei automatikusan meghívják a String függvényt
    // hogy megtudják egy objektum szöveges reprezentációját.
    fmt.Println(p) // ugyan az az eredmény mint az előbb, a Println meghívja
                   // a String metódust.
    fmt.Println(i) // dettó

    learnErrorHandling()
}

func learnErrorHandling() {
    // ", ok" szokásos megoldás arra, hogy jól működött-e a függvény.
    m := map[int]string{3: "three", 4: "four"}
    if x, ok := m[1]; !ok { // ok hamis lesz, mert az 1 nincs benne a map-ban.
        fmt.Println("nincs meg")
    } else {
        fmt.Print(x) // x lenne az érték, ha benne lenne a map-ban.
    }
    // A hiba érték többet is elmond a függvény kimeneteléről, mint hogy minden
    // "ok" volt-e
    if _, err := strconv.Atoi("non-int"); err != nil { // _ eldobja az értéket,
                                                       // úgy se lesz jó jelen
                                                       // esetben
        // kiírja, hogy "strconv.ParseInt: parsing "non-int": invalid syntax"
        fmt.Println(err)
    }
    // Az interfészekre még visszatérünk, addig is jöjjön a konkurens programozás!
    learnConcurrency()
}

// c egy csatorna, egy konkurens-biztos kommunikációs objektum.
func inc(i int, c chan int) {
    c <- i + 1 // <- a "küldés" operátor, ha a bal oldalán csatorna van, így
               // i+1-et küld be a csatornába
}

// Az inc-et fogjuk arra használni, hogy konkurensen megnöveljünk számokat
func learnConcurrency() {
    // Ugyanaz a make függvény, amivel korábban szeleteket hoztunk létre.
    // A make allokál map-eket, szeleteket és csatornákat.
    c := make(chan int)
    // Indítsunk három konkurens goroutine-t.  A számok konkurensen lesznek 
    // megnövelve, ha a számítógép képes rá és jól be van állítva, akkor pedig
    // paralellizálva/egymás mellett. Mind a 3 ugyanabba a csatornába küldi az
    // eredményeket.
    go inc(0, c) // A go utasítás indít el goroutine-okat.
    go inc(10, c)
    go inc(-805, c)
    // Beolvassuk 3x a csatornából az eredményeket és kiírjuk őket a kimenetre.
    // Nem lehet tudni milyen sorrendben fognak érkezni az eredmények!
    fmt.Println(<-c, <-c, <-c) // hogyha a jobb oldalon csatorna van, akkor a 
                               // "<-" a beolvasó/kapó operátor

    cs := make(chan string)       // még egy csatorna, ez stringekkel kommunikál
    cc := make(chan chan string)  // egy csatorna csatornával
    go func() { c <- 84 }()       // indítsunk egy új goroutine-t, csak azért
                                  // hogy küldjünk egy számot
    go func() { cs <- "wordy" }() // ugyanez, csak a cs csatornába stringet
                                  // küldünk
    // A select olyan mint a switch, csak feltételek helyett csatorna műveletek
    // vannak. Véletlenszerűen kiválasztja az első olyan esetet, ahol létrejöhet
    // kommunikáció.
    select {
    case i := <-c: // a megkapott értéket el lehet tárolni egy változóban
        fmt.Println("ez egy", i)
    case <-cs: // vagy el lehet dobni az értékét
        fmt.Println("ez egy string volt")
    case <-cc: // üres csatorna, soha nem fog rajta semmi se érkezni
        fmt.Println("sose futok le :'( ")
    }
    // Ezen a ponton vagy c vagy a cs goroutine-ja lefutott.
    // Amelyik hamarabb végzett, annak a megfelelő case-e lefutott, a másik
    // blokkolva vár.

    learnWebProgramming() // a Go képes rá. Te is képes akarsz rá lenni.
}

// Egy függvény a http csomagból elindít egy webszervert.
func learnWebProgramming() {
    // A ListenAndServe első paramétre egy TCP port, amin kiszolgálunk majd.
    // Második paramétere egy interfész, pontosabban a http.Handler interfész.
    err := http.ListenAndServe(":8080", pair{})
    fmt.Println(err) // nem felejtjük el kiírni az esetleges hibákat!
}

// Csináljunk a pair-ból egy http.Handler-t úgy, hogy implementáljuk az
// egyetlen metódusát, a ServeHTTP-t.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // Minden kapcsolatra elküldjük ezt a http.ResponseWriter-rel
    w.Write([]byte("Megtanultad a Go-t Y perc alatt!"))
}
```

## További olvasmányok

Minden Go-val kapcsolatos megtaláható a [hivatalos Go weboldalon](http://golang.org/).
Ott követhetsz egy tutorialt, játszhatsz a nyelvvel az interneten, és sok érdekességet olvashatsz.

A nyelv specifikációját kifejezetten érdemes olvasni, viszonylag rövid és sokat tanul belőle az ember.

Ha pedig jobban bele akarod vetni magad a Go-ba, akkor a legjobb praktikákat kilesheted a standard könyvtárból.
TIPP: a dokumentációban kattints egy függvény nevére és rögtön megmutatja a hozzá tartozó kódot!

Ha pedig a nyelvnek egy bizonyos részéről szeretnél hasonló leírást találni, akkor a
[gobyexample.com](https://gobyexample.com/)-on megtalálod, amit keresel.
---
language: ruby
lang: hu-hu
filename: learnruby-hu.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
translators:
  - ["Zsolt Prontvai", "https://github.com/prozsolt"]
---

```ruby
# Ez egy komment

=begin
Ez egy többsoros komment
Senki sem használja
Neked sem kellene
=end

# Először is: Minden objektum

# A számok objektumok

3.class #=> Fixnum

3.to_s #=> "3"


# Néhány alapvető számtani művelet
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32

# A számtani művelet csak szintaktikus cukor
# az objektumon történő függvény hívásra
1.+(3) #=> 4
10.* 5 #=> 50

# A speciális értékek objektumok
nil # Nincs itt semmi látnivaló
true # igaz
false # hamis

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Egyenlőség
1 == 1 #=> true
2 == 1 #=> false

# Egyenlőtlenség
1 != 1 #=> false
2 != 1 #=> true

# A false-on kívül, nil az egyetlen hamis érték

!nil   #=> true
!false #=> true
!0     #=> false

# Még több összehasonlítás
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Logikai operátorok
true && false #=> false
true || false #=> true
!true #=> false

# A logikai operátoroknak alternatív verziójuk is van sokkal kisebb
# precedenciával. Ezeket arra szánták, hogy több állítást összeláncoljanak
# amíg egyikük igaz vagy hamis értékkel nem tér vissza.

# `csinalj_valami_mast` csak akkor fut le, ha `csinalj_valamit` igaz értékkel
# tért vissza.
csinalj_valamit() and csinalj_valami_mast()
# `log_error` csak akkor fut le, ha `csinalj_valamit` hamis értékkel
# tért vissza.
csinalj_valamit() or log_error()


# A sztringek objektumok

'Én egy sztring vagyok'.class #=> String
"Én is egy sztring vagyok".class #=> String

helykitolto = 'interpolációt használhatok'
"Sztring #{helykitolto}, ha dupla időzőjelben van a sztringem"
#=> "Sztring interpolációt használhatok, ha dupla időzőjelben van a sztringem"

# A szimpla idézőjelet preferáljuk, ahol csak lehet,
# mert a dupla idézőjel extra számításokat végez.

# Kombinálhatunk sztringeket, de nem számokkal
'hello ' + 'world'  #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"

# kiírás a kimenetre
puts "Írok"

# Változók
x = 25 #=> 25
x #=> 25

# Értékadás az adott értékkel tér vissza
# Ez azt jelenti, hogy használhatunk többszörös értékadást:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Konvencióból, snake_case változó neveket használj
snake_case = true

# Leíró változó neveket használj
ut_a_projekt_gyokerehez = '/jo/nev/'
ut = '/rossz/nev/'

# A szimbólumok (objektumok)
# A szimbólumok megváltoztathatatlan, újra felhasználható konstans,
# mely belsőleg egész számként reprezentált. Sokszor sztring helyett használják,
# hogy effektíven közvetítsünk konkrét, értelmes értékeket

:fuggoben.class #=> Symbol

statusz = :fuggoben

statusz == :fuggoben #=> true

statusz == 'fuggoben' #=> false

statusz == :jovahagyott #=> false

# Tömbök

# Ez egy tömb
tomb = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# A tömmbök különböző tipusú dolgokat tartalmazhat

[1, 'hello', false] #=> [1, "hello", false]

# Tömbök indexelhetőek
# Az elejéről
tomb[0] #=> 1
tomb[12] #=> nil

# Akárcsak a számtani műveletek [var] hozzáférés
# is csak szintaktikus cukor
# a [] függvény hívására az objektumon
tomb.[] 0 #=> 1
tomb.[] 12 #=> nil

# A végéről
tomb[-1] #=> 5

# Kezdőértékkel és hosszal
tomb[2, 3] #=> [3, 4, 5]

# Tömb megfordítása
a=[1,2,3]
a.reverse! #=> [3,2,1]

# Vagy tartománnyal
tomb[1..3] #=> [2, 3, 4]

# Így adhatunk a tömbhöz
tomb << 6 #=> [1, 2, 3, 4, 5, 6]
# Vagy így
tomb.push(6) #=> [1, 2, 3, 4, 5, 6]

# Ellenőrízük, hogy a tömb tartalmaz egy elemet
tomb.include?(1) #=> true

# Hash-ek a ruby elsődleges szótárjai kulcs/érték párokkal
# Hash-eket kapcsos zárójellel jelöljük
hash = { 'szin' => 'zold', 'szam' => 5 }

hash.keys #=> ['szin', 'szam']

# Hash-ekben könnyen kreshetünk a kulcs segítségével:
hash['szin'] #=> 'zold'
hash['szam'] #=> 5

# Nem létező kulcsra keresve nil-t kapunk:
hash['nincs itt semmi'] #=> nil

# Ruby 1.9-től, egy külnleges szintaxist is használhatunk a szimbólumot
# használunk kulcsnak

uj_hash = { defcon: 3, action: true }

uj_hash.keys #=> [:defcon, :action]

# Ellenőrizzük, hogy az adott kulcs és érték bene-e van a hash-ben
uj_hash.has_key?(:defcon) #=> true
uj_hash.has_value?(3) #=> true

# Tip: A tömbök és hash-ek is felsorolhatóak
# Sok közös függvényük van, akár az each, map, count, és több

# Kontroll Struktúrák

if true
  'ha állítás'
elsif false
  'különben ha, opcionális'
else
  'különben, szintén opcionális'
end

for szamlalo in 1..5
  puts "iteracio #{szamlalo}"
end
#=> iteracio 1
#=> iteracio 2
#=> iteracio 3
#=> iteracio 4
#=> iteracio 5

# HOWEVER, No-one uses for loops.
# Instead you should use the "each" method and pass it a block.
# A block is a bunch of code that you can pass to a method like "each".
# It is analogous to lambdas, anonymous functions or closures in other
# programming languages.
#
# The "each" method of a range runs the block once for each element of the range.
# The block is passed a counter as a parameter.
# Calling the "each" method with a block looks like this:

(1..5).each do |counter|
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# You can also surround blocks in curly brackets:
(1..5).each { |counter| puts "iteration #{counter}" }

# The contents of data structures can also be iterated using each.
array.each do |element|
  puts "#{element} is part of the array"
end
hash.each do |key, value|
  puts "#{key} is #{value}"
end

counter = 1
while counter <= 5 do
  puts "iteration #{counter}"
  counter += 1
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

jegy = '4'

case jegy
when '5'
  puts 'Kitünő'
when '4'
  puts 'Jó'
when '3'
  puts 'Közepes'
when '2'
  puts 'Elégsége'
when '1'
  puts 'Elégtelen'
else
  puts 'Alternatív értékelés, hm?'
end
#=> "Jó"

# case-ek tartományokat is használhatnak
jegy = 82
case jegy
when 90..100
  puts 'Hurrá!'
when 80...90
  puts 'Jó munka'
else
  puts 'Megbuktál!'
end
#=> "Jó munka"

# kivétel kezelés:
begin
  # kód ami kivételt dobhat
  raise NoMemoryError, 'Megtelt a memória'
rescue NoMemoryError => kivetel_valtozo
  puts 'NoMemoryError-t dobott', kivetel_valtozo
rescue RuntimeError => mas_kivetel_valtozo
  puts 'RuntimeError dobott most'
else
  puts 'Ez akkor fut ha nem dob kivételt'
ensure
  puts 'Ez a kód mindenképpen lefut'
end

# Függvények

def ketszeres(x)
  x * 2
end

# Függvények (és egyébb blokkok) implicit viszatértnek az utolsó értékkel
ketszeres(2) #=> 4

# Zárójelezés opcionális, ha az eredmény félreérthetetlen
ketszeres 3 #=> 6

ketszeres ketszeres 3 #=> 12

def osszeg(x, y)
  x + y
end

# Függvény argumentumait vesszővel választjuk el.
osszeg 3, 4 #=> 7

osszeg osszeg(3, 4), 5 #=> 12

# yield
# Minden függvénynek van egy implicit, opcionális block paramétere
# 'yield' kulcsszóval hívhatjuk

def korulvesz
  puts '{'
  yield
  puts '}'
end

korulvesz { puts 'hello world' }

# {
# hello world
# }


# Fuggvénynek átadhatunk blokkot
# "&" jelöli az átadott blokk referenciáját
def vendegek(&block)
  block.call 'valami_argumentum'
end

# Argumentum lisát is átadhatunk, ami tömbé lesz konvertálva
# Erre való a splat operátor ("*")
def vendegek(*array)
  array.each { |vendeg| puts vendeg }
end

# Osztályt a class kulcsszóval definiálhatunk
class Ember

  # Az osztály változó. Az osztály minden példánnyával megvan osztva
  @@faj = 'H. sapiens'

  # Alap inicializáló
  def initialize(nev, kor = 0)
    # Hozzárendeli az argumentumot a "nev" példány változóhoz
    @nev = nev
    # Ha nem adtunk meg kort akkor az alapértemezet értéket fogja használni
    @kor = kor
  end

  # Alap setter függvény
  def nev=(nev)
    @nev = nev
  end

  # Alap getter függvény
  def nev
    @nev
  end

  # A fönti funkcionalítást az attr_accessor függvénnyel is elérhetjük
  attr_accessor :nev

  # Getter/setter függvények egyenként is kreálhatóak
  attr_reader :nev
  attr_writer :nev

  # Az osztály függvények "self"-et hasznalnak, hogy megkülönböztessék magukat a
  # példány függvényektől
  # Az osztályn hívhatóak, nem a példányon
  def self.mond(uzenet)
    puts uzenet
  end

  def faj
    @@faj
  end
end


# Példányosítsuk az osztályt
jim = Ember.new('Jim Halpert')

dwight = Ember.new('Dwight K. Schrute')

# Hívjunk meg pár függvényt
jim.faj #=> "H. sapiens"
jim.nev #=> "Jim Halpert"
jim.nev = "Jim Halpert II" #=> "Jim Halpert II"
jim.nev #=> "Jim Halpert II"
dwight.faj #=> "H. sapiens"
dwight.nev #=> "Dwight K. Schrute"

# Hívjuk meg az osztály függvényt
Ember.mond('Hi') #=> "Hi"

# Változók szókjait az elnevezésük definiálja
# $ kezdetű változók globálisak
$var = "Én egy globális változó vagyok"
defined? $var #=> "global-variable"

# Változók amik @-al kezdődnek példány szkópjuk van
@var = "Én egy példány változó vagyok"
defined? @var #=> "instance-variable"

# Változók amik @@-al kezdődnek példány szkópjuk van
@@var = "Én egy osztály változó vagyok"
defined? @@var #=> "class variable"

# Változók amik nagy betűvel kezdődnek a konstansok
Var = "Konstans vagyok"
defined? Var #=> "constant"

# Az osztály is objetum. Tehát az osztálynak lehet példány változója
# Az osztályváltozón osztozik minden pédány és leszármazott

# Ős osztály
class Ember
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(ertek)
    @@foo = ertek
  end
end

# Leszarmazott osztály
class Dolgozo < Ember
end

Ember.foo # 0
Dolgozo.foo # 0

Ember.foo = 2 # 2
Dolgozo.foo # 2

# Az osztálynak példány változóját nem látja az osztály leszármazottja.

class Ember
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(ertek)
    @bar = ertek
  end
end

class Doctor < Ember
end

Ember.bar # 0
Doctor.bar # nil

module ModulePelda
  def foo
    'foo'
  end
end

# Modulok include-olása a fügvényeiket az osztály példányaihoz köti.
# Modulok extend-elésa a fügvényeiket magához az osztályhoz köti.

class Szemely
  include ModulePelda
end

class Konyv
  extend ModulePelda
end

Szemely.foo     # => NoMethodError: undefined method `foo' for Szemely:Class
Szemely.new.foo # => 'foo'
Konyv.foo       # => 'foo'
Konyv.new.foo   # => NoMethodError: undefined method `foo'

# Callback-ek végrehajtódnak amikor include-olunk és extend-elünk egy modult

module ConcernPelda
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class Valami
  include ConcernPelda
end

Valami.bar     # => 'bar'
Valami.qux     # => NoMethodError: undefined method `qux'
Valami.new.bar # => NoMethodError: undefined method `bar'
Valami.new.qux # => 'qux'
```

## Egyéb források

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338)
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - A régebbi [ingyenes változat](http://ruby-doc.com/docs/ProgrammingRuby/) elérhető online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide)
---
language: TypeScript
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
translators:
    - ["Tamás Diószegi", "https://github.com/ditam"]
filename: learntypescript-hu.ts
lang: hu-hu
---

A TypeScript nyelv a JavaScript nyelven írt nagy méretű alkalmazások fejlesztését kívánja megkönnyíteni.
A TypeScript olyan, más nyelvekből ismert gyakori fogalmakat ad hozzá a JavaScripthez, mint például osztályok, interfészek, generikusság, és (opcionális) statikus típusosság.
A JavaScript egy befoglaló halmazát képzi: minden JavaScript kód érvényes TypeScript kód, így könnyen hozzáadható meglévő projektekhez. A TypeScript fordító kimenetként JavaScript kódot állít elő.

Ez a dokumentum a TypeScript által hozzáadott új szintaxissal foglalkozik, nem pedig a [Javascripttel](../javascript/).

Hogy kipróbáld a TypeScript fordítót, látogass el a [Játszótérre avagy Playground-ra](http://www.typescriptlang.org/Playground) ahol kódot írhatsz automatikus kódkiegészítéssel, és közvetlenül láthatod az előállított JavaScript kódot.

```js
// 3 alapvető típus létezik TypeScriptben
var isDone: boolean = false;
var lines: number = 42;
var name: string = "Anders";

// Amikor nem lehet a típust előre tudni, használható az "Any" típus
var notSure: any = 4;
notSure = "talán mégis sztring lesz";
notSure = false; // tévedtem, mégis boolean

// Kollekciókból létezik típusos és generikus tömb
var list: number[] = [1, 2, 3];
// ugyanez a generikus típus használatával
var list: Array<number> = [1, 2, 3];

// Enumerált típusok:
enum Color {Red, Green, Blue};
var c: Color = Color.Green;

// Végül, "void" használható a visszatérési értékkel nem bíró függvényeknél
function bigHorribleAlert(): void {
  alert("Kis idegesítő doboz vagyok!");
}

// A függvények elsőrangú (first-class) típusok, használható a vastag nyilas
// lambda szintaxis,
// a compiler pedig kikövetkezteti a típusokat (inferred types)

// A következők egyenértékűek, ugyanaz a szignatúra kerül kikövetkeztetésre, és
// így ugyanaz a JavaScript kód lesz előállítva
var f1 = function(i: number): number { return i * i; }
// Következtetett visszatérési értékkel
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
// Következtetett visszatérési értékkel
var f4 = (i: number) => { return i * i; }
// Következtetett visszatérési értékkel, 
// ebben az egysoros formában nem szükséges a return kulcsszó
var f5 = (i: number) =>  i * i;

// Az interfészek szerkezeti alapon működnek, vagyis minden objektum, ahol 
// jelen vannak a megfelelő mezők kompatibilis az interfésszel
interface Person {
  name: string;
  // Az opcionális tagokat "?" jelöli
  age?: number;
  // És persze függvények is:
  move(): void;
}

// Egy objektum, ami megvalósítja a "Person" interfészt
// Tekinthető Personnek, hiszen van name és move mezője
var p: Person = { name: "Bobby", move: () => {} };
// Egy objektum, ahol az opcionális mező is jelen van:
var validPerson: Person = { name: "Bobby", age: 42, move: () => {} };
// Ez viszont nem Person, mert az age mező típusa nem szám!
var invalidPerson: Person = { name: "Bobby", age: true };

// Az interfészekkel függvény típusok is leírhatóak:
interface SearchFunc {
  (source: string, subString: string): boolean;
}
// Csak a paraméterek típusai számítanak, a neveik nem.
var mySearch: SearchFunc;
mySearch = function(src: string, sub: string) {
  return src.search(sub) != -1;
}

// Osztályok - a mezők alapértelmezésben publikusak
class Point {
    // Mezők
    x: number;

    // Konstruktor - a public/private kulcsszavak ebben a kontextusban
    // legenerálják a mezőkhöz szükséges kódot a konstruktorban.
    // Ebben a példában az "y" ugyanúgy definiálva lesz, mint az "x", csak 
    // kevesebb kóddal.
    // Alapértelmezett (default) értékek is megadhatóak.

    constructor(x: number, public y: number = 0) {
        this.x = x;
    }

    // Metódusok
    dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

    // Statikus mezők
    static origin = new Point(0, 0);
}

var p1 = new Point(10 ,20);
var p2 = new Point(25); //y itt 0 lesz

// Öröklés
class Point3D extends Point {
    constructor(x: number, y: number, public z: number = 0) {
        super(x, y); // Szükséges az ősosztály konstruktorának explicit hívása
    }

    // Felülírás
    dist() {
        var d = super.dist();
        return Math.sqrt(d * d + this.z * this.z);
    }
}

// Modulok
// ("." használható az almodulok számára)
module Geometry {
  export class Square {
    constructor(public sideLength: number = 0) {
    }
    area() {
      return Math.pow(this.sideLength, 2);
    }
  }
}

var s1 = new Geometry.Square(5);

// Új lokális név definiálása a module számára
import G = Geometry;

var s2 = new G.Square(10);

// Generikus típusok
// Osztályok
class Tuple<T1, T2> {
    constructor(public item1: T1, public item2: T2) {
    }
}

// Interfészek
interface Pair<T> {
    item1: T;
    item2: T;
}

// és függvények
var pairToTuple = function<T>(p: Pair<T>) {
    return new Tuple(p.item1, p.item2);
};

var tuple = pairToTuple({ item1:"hello", item2:"world"});

// definíciós fájl hivatkozása:
/// <reference path="jquery.d.ts" />

```

## További források
 * [TypeScript hivatalos weboldala] (http://www.typescriptlang.org/)
 * [TypeScript nyelv specifikációja (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [Forráskód GitHubon] (https://github.com/Microsoft/TypeScript)
 * [Definitely Typed - típusdefiníciók gyűjteménye] (http://definitelytyped.org/)
---
language: yaml
filename: learnyaml-hu.yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
translators:
  - ["Tamás Diószegi", "https://github.com/ditam"]
lang: hu-hu
---

A YAML egy adat sorosító nyelv, amit úgy terveztek, hogy közvetlenül is 
olvasható és írható legyen emberi szemmel.

A JSON formátum egy szigorú befoglaló halmazát alkotja, kiegészítve azt
szintaktikai jelentéssel bíró sortörésekkel és indentációval,
a Pythonhoz hasonlóan. A Pythonnal ellentétben azonban a YAML nem engedélyezi
a közvetlen tab karakterek jelenlétét.

Megjegyzés: UTF-8 ékezetes betűk használhatóak, ha a fájl kódlása megfelelő,
a kódolást a tartalomban explicit nem kell (és nem is lehet) feltüntetni.

```yaml
# A kommentek YAML-ban így néznek ki.

##################
# Skalár típusok #
##################

# A gyökér objektumunk (az egész dokumentumra értve) egy map,
# ami a más nyelvekből ismert dictionary, hash vagy object típusokkal egyenértékű.
kulcs: érték
masik_kulcs: Másik érték jön ide.
egy_szam: 100
tudomanyos_jelolessel: 1e+12
boolean: true
null_value: null
kulcs benne szóközökkel: érték
# Látható, hogy a sztringeket nem szükséges idézőjelek közé zárni, bár szabad.
Továbbá: "Idézőjelekkel megadott sztring."
"A kulcs is lehet idézőjeles.": "Hasznos lehet, ha ':'-ot akarsz a kulcsban."

# Többsoros sztringek írhatóak 'literal block'-ként ('|' jelet használva)
# vagy 'folded block'-ként is ('>' jelet használva).
literal_block: |
    Ez az egész szöveg-blokk lesz az értéke a literal_block kulcsnak,
    a sortöréseket megtartva.

    Az ilyen sztringet az indentáció visszahúzása zárja le, a behúzás pedig
    eltávolításra kerül.

        A 'még jobban' behúzott részek megtartják a behúzásukat - 
        ezeknek a soroknak 4 szóköz behúzása lesz.
folded_style: >
    Az az egész szöveg-blokk lesz az értéke a 'folded_style' kulcsnak, de
    ezúttal minden sortörés egy szóközre lesz cserélve.

    Az üres sorok, mint a fenti, új sor karakterre cserélődnek.

        A 'még jobban' behúzott sorok megtartják a sortöréseiket, -
        ez a szöveg két sorban jelenik meg.

######################
# Gyűjtemény típusok #
######################

# Egymásba ágyazás a behúzás változtatásával érhető el.
beagyazott_map:
    key: value
    another_key: Another Value
    masik_beagyazott_map:
        hello: hello

# A mapeknek nem csak sztring kulcsaik lehetnek.
0.25: lebegőpontos kulcs

# A kulcsok lehetnek többsoros objektumok is, ? jellel jelezve a kulcs kezdetét
? |
    Ez itt egy
    többsoros kulcs
: és ez az értéke

# Szintén engedélyezett a kollekció típusok használata kulcsként, de egyéb
# nyelvekben ez gyakran problémákat fog okozni.

# Szekvenciák (listákkal vagy tömbökkel egyenértékűek) így néznek ki:
egy_szekvencia:
    - Item 1
    - Item 2
    - 0.5 # Többféle típust is tartalmazhat
    - Item 4
    - key: value
      another_key: another_value
    -
        - Ez egy szekvencia
        - egy másik szekvenciába ágyazva

# Mivel a YAML a JSON befoglaló halmazát alkotja, JSON szintaxisú
# mapek és szekvenciák is használhatóak:
json_map: {"key": "value"}
json_seq: [3, 2, 1, "takeoff"]

#########################
# EXTRA YAML KÉPESSÉGEK #
#########################

# A YAML-ben ún. 'anchor'-ök segítségével könnyen lehet duplikálni
# tartalmakat a dokumentumon belül. A következő kulcsok azonos értékkel bírnak:
anchored_tartalom: &anchor_neve Ez a sztring két kulcs értéke is lesz.
másik_anchor: *anchor_neve

# Vannak a YAML-ben tagek is, amivel explicit lehet típusokat jelölni.
explicit_string: !!str 0.5
# Bizonyos implementációk nyelv-specifikus tageket tartalmaznak, mint
# például ez a Python komplex szám típusának jelölésére:
python_complex_number: !!python/complex 1+2j

######################
# EXTRA YAML TÍPUSOK #
######################

# Nem a sztringek és a számok az egyedüli skalár típusok YAML-ben.
# ISO-formátumú dátumok és dátumot jelölő literal kifejezések is értelmezettek.
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14

# A !!binary tag jelöli, hogy egy sztring valójában base64-kódolású
# reprezentációja egy bináris blob-nak
gif_file: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# Létezik a YAML-ban egy halmaz típus (set) is, ami így néz ki:
set:
    ? elem1
    ? elem2
    ? elem3

# Mint Pythonban, a halmazok null értékekkel feltöltött mapek, vagyis a fenti
# halmaz egyenértékű a következővel:
set2:
    elem1: null
    elem2: null
    elem3: null
```---
language: hy
filename: learnhy.hy
contributors:
    - ["Abhishek L", "http://twitter.com/abhishekl"]
    - ["Zirak", "http://zirak.me"]
---

Hy is a lisp dialect built on top of python. This is achieved by
converting hy code to python's abstract syntax tree (ast). This allows
hy to call native python code or python to call native hy code as well

This tutorial works for hy ≥ 0.9.12, with some corrections for hy 0.11.

```clojure
;; this gives an gentle introduction to hy for a quick trial head to
;; http://try-hy.appspot.com
;;
; Semicolon comments, like other LISPS

;; s-expression basics
; lisp programs are made of symbolic expressions or sexps which
; resemble
(some-function args)
; now the quintessential hello world
(print "hello world")

;; simple data types
; All simple data types are exactly similar to their python counterparts
; which
42 ; => 42
3.14 ; => 3.14
True ; => True
4+10j ; => (4+10j) a complex number

; lets start with some really simple arithmetic
(+ 4 1) ;=> 5
; the operator is applied to all arguments, like other lisps
(+ 4 1 2 3) ;=> 10
(- 2 1) ;=> 1
(* 4 2) ;=> 8
(/ 4 1) ;=> 4
(% 4 2) ;=> 0 the modulo operator
; power is represented by ** operator like python
(** 3 2) ;=> 9
; nesting forms will do the expected thing
(+ 2 (* 4 2)) ;=> 10
; also logical operators and or not and equal to etc. do as expected
(= 5 4) ;=> False
(not (= 5 4)) ;=> True

;; variables
; variables are set using setv, variable names can use utf-8 except
; for ()[]{}",'`;#|
(setv a 42)
(setv π 3.14159)
(def *foo* 42)
;; other container data types
; strings, lists, tuples & dicts
; these are exactly same as python's container types
"hello world" ;=> "hello world"
; string operations work similar to python
(+ "hello " "world") ;=> "hello world"
; lists are created using [], indexing starts at 0
(setv mylist [1 2 3 4])
; tuples are immutable data structures
(setv mytuple (, 1 2))
; dictionaries are key value pairs
(setv dict1 {"key1" 42 "key2" 21})
; :name can be used to define keywords in hy which can be used for keys
(setv dict2 {:key1 41 :key2 20})
; use `get' to get the element at an index/key
(get mylist 1) ;=> 2
(get dict1 "key1") ;=> 42
; Alternatively if keywords were used they can directly be called
(:key1 dict2) ;=> 41

;; functions and other program constructs
; functions are defined using defn, the last sexp is returned by default
(defn greet [name]
  "A simple greeting" ; an optional docstring
  (print "hello " name))

(greet "bilbo") ;=> "hello bilbo"

; functions can take optional arguments as well as keyword arguments
(defn foolists [arg1 &optional [arg2 2]]
  [arg1 arg2])

(foolists 3) ;=> [3 2]
(foolists 10 3) ;=> [10 3]

; you can use rest arguments and kwargs too:
(defn something-fancy [wow &rest descriptions &kwargs props]
  (print "Look at" wow)
  (print "It's" descriptions)
  (print "And it also has:" props))

(something-fancy "My horse" "amazing" :mane "spectacular")
  
; you use apply instead of the splat operators:
(apply something-fancy ["My horse" "amazing"] { "mane" "spectacular" })

; anonymous functions are created using `fn' or `lambda' constructs
; which are similar to `defn'
(map (fn [x] (* x x)) [1 2 3 4]) ;=> [1 4 9 16]

;; Sequence operations
; hy has some builtin utils for sequence operations etc.
; retrieve the first element using `first' or `car'
(setv mylist [1 2 3 4])
(setv mydict {"a" 1 "b" 2})
(first mylist) ;=> 1

; slice lists using slice
(slice mylist 1 3) ;=> [2 3]
; or, in hy 0.11, use cut instead:
(cut mylist 1 3) ;=> [2 3]

; get elements from a list or dict using `get'
(get mylist 1) ;=> 2
(get mydict "b") ;=> 2
; list indexing starts from 0 same as python
; assoc can set elements at keys/indexes
(assoc mylist 2 10) ; makes mylist [1 2 10 4]
(assoc mydict "c" 3) ; makes mydict {"a" 1 "b" 2 "c" 3}
; there are a whole lot of other core functions which makes working with
; sequences fun

;; Python interop
;; import works just like in python
(import datetime)
(import [functools [partial reduce]]) ; imports fun1 and fun2 from module1
(import [matplotlib.pyplot :as plt]) ; doing an import foo as bar
; all builtin python methods etc. are accessible from hy
; a.foo(arg) is called as (.foo a arg)
(.split (.strip "hello world  ")) ;=> ["hello" "world"]

; there is a shortcut for executing multiple functions on a value called the
; "threading macro", denoted by an arrow:
(-> "hello world  " (.strip) (.split)) ;=> ["hello" "world]
; the arrow passes the value along the calls as the first argument, for instance:
(-> 4 (* 3) (+ 2))
; is the same as:
(+ (* 4 3) 2)

; there is also a "threading tail macro", which instead passes the value as the
; second argument. compare:
(-> 4 (- 2) (+ 1)) ;=> 3
(+ (- 4 2) 1) ;=> 3
; to:
(->> 4 (- 2) (+ 1)) ;=> -1
(+ 1 (- 2 4)) ;=> -1

;; Conditionals
; (if condition (body-if-true) (body-if-false)
(if (= passcode "moria")
  (print "welcome")
  (print "Speak friend, and Enter!"))

; nest multiple if else if clauses with cond
(cond
 [(= someval 42)
  (print "Life, universe and everything else!")]
 [(> someval 42)
  (print "val too large")]
 [(< someval 42)
  (print "val too small")])

; group statements with do, these are executed sequentially
; forms like defn have an implicit do
(do
 (setv someval 10)
 (print "someval is set to " someval)) ;=> 10

; create lexical bindings with `let', all variables defined thusly
; have local scope
(let [[nemesis {"superman" "lex luther"
                "sherlock" "moriarty"
                "seinfeld" "newman"}]]
  (for [(, h v) (.items nemesis)]
	(print (.format "{0}'s nemesis was {1}" h v))))

;; classes
; classes are defined in the following way
(defclass Wizard [object]
  [[--init-- (fn [self spell]
             (setv self.spell spell) ; init the spell attr
             None)]
   [get-spell (fn [self]
              self.spell)]])

; or, in hy 0.11:
(defclass Wizard [object]
  (defn --init-- [self spell]
    (setv self.spell spell))

  (defn get-spell [self]
    self.spell))

;; do checkout hylang.org
```

### Further Reading

This tutorial is just a very basic introduction to hy/lisp/python.

Hy docs are here: [http://hy.readthedocs.org](http://hy.readthedocs.org)

Hy's GitHub repo: [http://github.com/hylang/hy](http://github.com/hylang/hy)

On freenode irc #hy, twitter hashtag #hylang
---
language: asciidoc
contributors:
    - ["Ryan Mavilia", "http://unoriginality.rocks/"]
translators:
    - ["Rizky Luthfianto", "http://github.com/rilut"]
filename: asciidoc-id.md
lang: id-id
---

AsciiDoc adalah bahasa markup yang mirip dengan Markdown dan dapat digunakan untuk apa saja, untuk menulis buku maupun blog. Dibuat pada tahun 2002 oleh Stuart Rackham, bahasa ini sederhana tetapi memungkinkan sejumlah besar kustomisasi.

Kepala Dokumen

Kepala Dokumen adalah opsional dan tidak dapat berisi baris kosong. Harus diimbangi konten, setidaknya satu baris kosong.

Hanya Judul 

```
= Judul Dokumen

Kalimat pertama dokumen.
```

Judul dan Penulis

```
= Judul Dokumen
Pertama terakhir <first.last@learnxinyminutes.com>

Awal dokumen ini.
```

Banyak Penulis

```
= Judul Dokumen
John Doe <john@go.com>; Jane Doe <jane@yo.com>; Black Beard <beardy@pirate.com>

Memulai dokumen dengan banyak penulis.
```

Garis Revisi (membutuhkan garis penulis)

```
= Judul Dokumen V1
Manusia Kentang <keripik@renyah.com>
v1.0, 2016/01/13

Artikel tentang keripik ini akan menjadi menyenangkan.
```

Paragraf

```
Anda tidak perlu sesuatu yang istimewa untuk paragraf.

Tambahkan baris kosong antara paragraf untuk memisahkan mereka.

Untuk membuat baris kosong, tambahkan: +
dan Anda akan mendapat satu baris kosong!
```

Memformat Teks

```
_underscore menciptakan miring_
*Tanda bintang untuk tebal*
*_Gabungkan biar makin asyik_*
`Penggunaan tanda petik untuk menandakan monospace`
`*Monospace tebal*`
```

Judul bagian

```
= Level 0 (hanya dapat digunakan dalam header dokumen)

== Level 1 <h2>

=== Level 2 <h3>

==== Level 3 <h4>

===== Level 4 <h5>

====== Level 5 <h6>

======= Level 6 <h7>

```

Daftar

Untuk membuat daftar bullet, gunakan tanda bintang.

```
* foo
* bar
* baz
```

Untuk membuat daftar bernomor, gunakan titik.

```
. Item 1
. item 2
. Item 3
```

Anda bisa membuat daftar bersarang dengan menambahkan tanda bintang atau titik tambahan hingga lima kali.

```
* Foo 1
** Foo 2
*** Foo 3
**** Foo 4
***** Foo 5

. foo 1
.. Foo 2
... Foo 3
.... Foo 4
..... Foo 5
```
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
filename: coffeescript-id.coffee
translators:
  - ["Rizky Luthfianto", "http://github.com/rilut"]
lang: id-id
---

CoffeeScript adalah bahasa sederhana yang diterjemahkan saat kompilasi ke dalam JavaScript,
dan bukan diterjemahkan pada saat *runtime*.  
CoffeeScript mencoba agar kode JavaScript yang dihasilkan tetap mudah dibaca
dan kompatibel dengan semua *runtime* JavaScript.

Lihat juga [website CoffeeScript](http://coffeescript.org/) yang memiliki tutorial lengkap tentang CoffeeScript.

```CoffeeScript
# CoffeeScript adalah bahasa hipster.
# Mengikuti tren bahasa modern lainnya.
# Sehingga, seperti Ruby dan Python, untuk komentar digunakan tanda pagar.

###
Ini adalah contoh blok komentar, yang nanti diterjemahkan langsung ke '/ *' dan '* /'
pada kode JavaScript yang dihasilkan.

Anda diharapkan sedikit memahami semantik JavaScript sebelum melanjutkan tutorial ini.
###

# Pengisian nilai variabel:
angka = 42 #=> var angka = 42;
kebalikan = true #=> var kebalikan = true;

# Kondisi:
angka = -42 if kebalikan #=> if(kebalikan) { angka = -42; }

# Fungsi:
kuadrat = (x) -> x * x #=> var kuadrat = function(x) { return x * x; }

isi = (wadah, cairan = "kopi") ->
  "Mengisi #{wadah} dengan #{cairan}..."
#=>var isi;
#
#isi = function(wadah, cairan) {
#  if (cairan == null) {
#    cairan = "kopi";
#  }
#  return "Mengisi " + wadah + " dengan " + cairan + "...";
#};

# Rentang:
list = [1..5] # => var list = [1, 2, 3, 4, 5];

# Objek:
fungsi_matematika =
  akar:   Math.sqrt
  kuadrat: kuadrat
  kubik:   (x) -> x * kuadrat x
#=> var fungsi_matematika = {
#    "akar": Math.sqrt,
#    "kuadrat": kuadrat,
#    "kubik": function(x) { return x * kuadrat(x); }
#   };

# *Splat*:
balapan = (pemenang, pelari...) ->
  print pemenang, pelari
#=>balapan = function() {
#    var pelari, pemenang;
#    pemenang = arguments[0], pelari = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#    return print(pemenang, pelari);
#  };

# Cek keberadaan:
alert "Elvis ada!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("Elvis ada!"); }

# Komprehensi *array*:
kubik_kubik = (fungsi_matematika.kubik angka for angka in list)
#=>kubik_kubik = (function() {
#     var _i, _len, _hasil;
#     _hasil = [];
#     for (_i = 0, _len = list.length; _i < _len; _i++) {
#         angka = list[_i];
#         _hasil.push(fungsi_matematika.kubik(angka));
#     }
#     return _hasil;
#})();

sayur_sayuran = ['brokoli', 'bayam', 'kemangi']
makan sayuran for sayuran in sayur_sayuran when sayuran isnt 'kemangi'
#=>sayur_sayuran = ['brokoli', 'bayam', 'kemangi'];
#
#for (_k = 0, _len2 = sayur_sayuran.length; _k < _len2; _k++) {
#  sayuran = sayur_sayuran[_k];
#  if (sayuran !== 'kemangi') {
#    makan(sayuran);
#  }
#}
```

## Referensi Tambahan

- [Smooth CoffeeScript (EN)] (http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto (EN)] (https://leanpub.com/coffeescript-ristretto/read)
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
translators:
    - ["Eka Y Saputra", "http://github.com/ekajogja"]
lang: id-id
filename: learncss-id.css
---

Pada mulanya, web tidak memiliki elemen visual, murni teks saja.
Tapi seiring perkembangan peramban, laman web dengan elemen visual menjadi umum.
CSS adalah bahasa standar yang ada untuk menjaga keterpisahan antara
konten (HTML) serta tampilan-dan-kesan laman web.

Singkatnya, fungsi CSS ialah menyajikan sintaks yang memampukan kita
untuk memilih elemen tertentu dalam sebuah laman HTML
dan menerapkan berbagai properti visual bagi elemen tersebut.

Seperti bahasa lainnya, CSS memiliki banyak versi.
Di artikel ini, kita fokus pada CSS2.0 - yang meskipun bukan versi termutakhir
namun paling kompatibel dan didukung secara luas.

**CATATAN:** Lantaran keluaran dari CSS berwujud efek-efek visual,
maka untuk mempelajarinya, kita perlu mencoba berbagai hal dalam dunia olah CSS
semisal [dabblet](http://dabblet.com/).
Fokus utama artikel ini ialah pada sintaks dan sejumlah tips umum.


```css
/* komentar terletak diantara sepasang tanda garis miring dan bintang,
persis seperti larik ini! */

/* ####################
   ## SELEKTOR
   ####################*/

/* Secara garis besar, statemen utama dalam CSS sangat sederhana */
selektor { properti: nilai; /* properti lainnya */ }

/* selektor berfungsi untuk memilih suatu elemen dalam sebuah laman.

Kita juga bisa memilih semua elemen di sebuah halaman! */
* { color:red; }

/*
Dengan menentukan sebuah elemen seperti ini pada sebuah laman:

<div class='suatu-class class2' id='suatuId' attr='nilai' />
*/

/* kita bisa memilih elemen berdasarkan nama class-nya */
.suatu-class { }

/*atau dengan dua class sekaligus! */
.suatu-class.class2 { }

/* atau dengan nama tag-nya */
div { }

/* atau id-nya */
#suatuId { }

/* atau - jika ada - dengan attribute-nya! */
[attr] { font-size:smaller; }

/* atau jika attribute tersebut memiliki nilai spesifik */
[attr='nilai'] { font-size:smaller; }

/* dibuka dengan sebuah nilai*/
[attr^='nil'] { font-size:smaller; }

/* atau ditutup dengan nilai */
[attr$='ai'] { font-size:smaller; }

/* atau bahkan disisipi nilai */
[attr~='la'] { font-size:smaller; }


/* dan yang lebih penting lagi, kita bisa mengombinasikannya sekaligus
dengan syarat tidak ada spasi diantara selektor-selektor. sebab adanya spasi
akan membuat selektor itu memiliki makna yang berbeda.*/
div.suatu-class[attr$='ai'] { }

/* kita juga bisa memilih sebuah elemen berdasarkan posisi elemen induknya.*/

/*sebuah elemen yang merupakan anak langsung dari elemen induk (diseleksi dng
cara yang sama) */
div.suatu-induk > .-suatu-class {}

/* atau salah satu induk elemennya dalam hirarki elemen */
/* berikut ini dimaksudkan pada elemen manapun dengan class "class-entah" dan
merupakan anak elemen dari suatu div dengan class "induk-entah" PADA LEVEL
HIRARKI MANAPUN */
div.suatu-induk .suatu-class {}

/* peringatan: selektor yang sama jika tanpa ada spasi akan bermakna lain.
misalnya? */
div.suatu-induk.suatu-class {}

/* kita juga bisa memilih sebuah elemen berdasarkan saudara elemen yang muncul
tepat sebelumnya */
.aku-muncul-tepat-sebelum + .elemen-ini { }

/*atau saudara elemen manapun yang pernah muncul selang beberapa elemen
sebelumnya */
.aku-pernah-muncul-sebelum ~ .elemen-ini {}

/* Ada beberapa pseudo-class yang memampukan kita memilih suatu elemen
berdasarkan perilaku lamannya (bukan struktur lamannya) */

/* semisal ketika sebuah elemen ditimpa hover (pointer mouse) */
:hover {}

/* atau link yang sudah pernah diklik*/
:visited {}

/* atau link yang belum pernah diklik*/
:link {}

/* atau elemen input yang menjadi fokus */
:focus {}


/* ####################
   ## PROPERTI
   ####################*/

selektor {
    
    /* Unit */
    width: 50%; /* dalam persen */
    font-size: 2em; /* angka kali jumlah font-size saat ini */
    width: 200px; /* dalam pixel */
    font-size: 20pt; /* dalam point */
    width: 5cm; /* dalam centimeter */
    width: 50mm; /* dalam milimeter */
    width: 5in; /* dalam inci */
    
    /* Warna */
    background-color: #F6E;  /* dalam short hex */
    background-color: #F262E2; /* dalam format long hex */
    background-color: tomato; /* warna yang sudah punya konvensi nama */
    background-color: rgb(255, 255, 255); /* dalam rgb */
    background-color: rgb(10%, 20%, 50%); /* dalam persen rgb */
    background-color: rgba(255, 0, 0, 0.3); /* dalam rgb semi-transparan*/
    
    /* Gambar */
    background-image: url(/folder-gambar/image.jpg);
    
    /* Font */
    font-family: Arial;
    font-family: "Courier New"; /* jika nama font memiliki spasi,
    							ia diketik dalam tanda petik ganda */
    font-family: "Courier New", Trebuchet, Arial; /* jika font pertama tidak
    							ditemukan, peramban menggunakan font berikutnya,
    							demikian secara berturut-turut */
}

```

## Penggunaan

Simpan semua CSS yang hendak kita pakai dengan ekstensi `.css`.

```xml
<!-- kita harus menautkan file css itu ke laman di bagian <head>: -->
<link rel='stylesheet' type='text/css' href='folder/namafile.css' />

<!-- kita juga bisa mengetik CSS secara inline di dalam markup.
Namun, sebisa mungkin metode ini dihindari. -->
<style>
   selektor { properti:nilai; }
</style>

<!-- atau langsung mengetik properti CSS pada sebuah elemen. 
Metode ini harus dihindari sebisa mungkin. -->
<div style='properti:nilai;'>
</div>

```

## Prioritas

Kita tahu bahwa sebuah elemen bisa dipilih dengan lebih dari satu selektor, 
serta bisa diberi lebih dari satu properti.
Dalam kasus seperti ini, hanya salah satu properti saja yang akan diterapkan
pada elemen dengan prioritas tertentu.

Dengan susunan CSS:

```css

/*A*/
p.class1[attr='nilai']

/*B*/
p.class1 {}

/*C*/
p.class2 {}

/*D*/
p {}

/*E*/
p { properti: nilai !important; }

```

dan susunan markup:

```xml
<p style='/*F*/ properti:nilai;' class='class1 class2' attr='nilai'>
</p>
```

Maka prioritas penerapan style-nya ialah sbb.:  
Ingat, penerapan ini untuk masing-masing **properti**,
bukan keseluruhan larik.

* `E` prioritas pertama sebab ada kata `!important`.  
	Dianjurkan untuk menghindari kata ini jika tidak benar-benar perlu.
* `F` prioritas kedua sebab ia diketik secara inline.
* `A` prioritas ketiga sebab selektor ini lebih spesifik dibanding yang lain.  
	lebih spesifik = lebih banyak unsur selektor. contoh ini punya 3 unsur:
	1 tagname `p` + 1 nama class `class1` + 1 attribute `attr='nilai'`
* `C` prioritas berikutnya sebab meski sama spesifik dengan `B` namun
	ia muncul lebih akhir.
* Lalu `B`
* dan terakhir baru `D`.

## Kompatibilitas

Sebagian besar fitur dalam CSS2 (dan lambat laun juga CSS3) kompatibel dengan
semua peramban dan perangkat. Namun selalu vital untuk memastikan kompatibilitas
unsur dan nilai yang kita ketikkan dalam CSS dengan peramban yang ditargetkan.

[QuirksMode CSS](http://www.quirksmode.org/css/) ialah salah satu sumber terbaik untuk memeriksa kompatibilitas CSS dan peramban.

## Referensi Lanjut

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)

---
language: java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Jakukyo Friel", "http://weakish.github.io"]
    - ["Madison Dickson", "http://github.com/mix3d"]
    - ["Simon Morgan", "http://sjm.io/"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
    - ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
    - ["Rachel Stiyer", "https://github.com/rstiyer"]
filename: LearnJava-id.java
translators:
  - ["Ahmad Zafrullah", "https://github.com/23Pstars"]
lang: id-id
---

Java adalah bahasa pemrograman yang memiliki tujuan umum dan berorientasi kelas dan objek.
[Baca lebih lanjut.](http://docs.oracle.com/javase/tutorial/java/)

```java
// Komentar satu baris diawali dengan // (dua garis miring)
/*
Ini adalah contoh komentar banyak-baris.
*/
/**
Ini adalah contoh komentar JavaDoc. Digunakan untuk mendeskripsikan sebuah kelas,
atau beberapa sifat dari kelas tersebut.
*/

// Menyertakan kelas ArrayList dalam paket java.util
import java.util.ArrayList;
// Menyertakan semua kelas yang ada dalam paket java.security
import java.security.*;

// Setiap dokumen .java sebuah kelas publik dengan nama yang sama dengan nama kelas.
public class BelajarJava {

    // Untuk menjalankan program java, program harus memiliki sebuah method utama (main) sebagai awalan.
    public static void main (String[] args) {

        // System.out.println() digunakan untuk menampilkan satu baris teks.
        System.out.println("Halo Dunia!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // System.out.print() hanya menampilkan teks tanpa baris baru.
        System.out.print("Halo ");
        System.out.print("Dunia");

        // System.out.printf() memudahkan dalam mengatur format penampilan.
        System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159

        ///////////////////////////////////////
        // Variabel
        ///////////////////////////////////////

        /*
        *  Deklarasi Variabel
        */
        // Deklarasi variabel menggunakan format <tipe> <nama>
        int nilai;
        // Deklarasi banyak variabel menggunakan format yang sama <tipe> <nama1>, <tipe> <nama2>, <tipe> <nama3>
        int nilai1, nilai2, nilai3;

        /*
        *  Inisialisasi Variabel
        */

        // Inisialisasi sebuah variabel menggunakan <tipe> <nama> = <nilai>
        int nilai = 1;
        // Inisialisasi banyak variabel menggunakan format yang sama <tipe> <nama1>, <nama2>, <nama3> = <nilai>
        int nilai1, nilai2, nilai3;
        nilai1 = nilai2 = nilai3 = 1;

        /*
        *  Tipe Variabel
        */
        // Byte - 8 bit signed untuk bilangan bulat komplemen 2
        // (-128 <= byte <= 127)
        byte nilaiByte = 100;

        // Short - 8 bit signed untuk bilangan bulat komplemen 2
        // (-32,768 <= short <= 32,767)
        short nilaiShort = 10000;

        // Integer - 32 bit signed untuk bilangan bulat komplemen 2
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int nilaiInt = 1;

        // Long - 64 bit signed untuk bilangan bulat komplemen 2
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long nilaiLong = 100000L;
        // Karakter "L" pada akhir nilai menyatakan tipe Long;
        // selainnya akan dianggap sebagai nilai bilangan bulat.

        // Catatan: Java tidak memiliki tipe unsigned.

        // Float - Presisi-satu 32-bit standar IEEE 754 untuk Floating Point
        // 2^-149 <= float <= (2-2^-23) * 2^127
        float nilaiFloat = 234.5f;
        // Karakter "f" atau "F" pada akhir nilai menyatakan tipe Float;
        // selainnya akan dianggap sebagai nilai double.

        // Double - Presisi-dua 64-bit standar IEEE 754 untuk Floating Point
        // 2^-1074 <= x <= (2-2^-52) * 2^1023
        double nilaiDouble = 123.4;

        // Boolean - true & false
        boolean nilaiBoolean = true;
        boolean nilaiBoolean = false;

        // Char - Sebuah karakter Unicode 16-bit
        char nilaiChar = 'A';

        // Variabel "final" tidak dapat di-set kembali nilainya pada objek lain,
        final int WAKTU_SAYA_BEKERJA_TIAP_MINGGU = 9001;
        // tapi dapat dilakukan inisialisasi diwaktu yang lain.
        final double E;
        E = 2.71828;


        // BigInteger - Bilangan bulat yang memiliki presisi dinamis
        //
        // BigInteger adalah tipe data yang memungkinkan pembuat program untuk memanipulasi
        // bilangan bulat lebih panjang dari 64-bit. Bilangan bulat tersebut tersimpan dalam
        // bentuk kumpulan byte (array) dan dimanipulasi menggunakan fungsi yang sudah tersedia
        // pada BigInteger
        //
        // BigInteger dapat diinisialisasi menggunakan kumpulan byte atau teks.
        
        BigInteger nilaiBigInteger = new BigInteger(kumpulanByte);


        // BigDecimal - Bilangan signed desimal yang memiliki presisi dinamis
        //
        // Tipe BigDecimal memiliki dua bagian: sebuah bilangan bulat dengan nilai presisi
        // dinamis tanpa skala dan sebuah bilangan bulat skala 32-bit.

        // BigDecimal memungkinkan pembuat program untuk memegang kontrol penuh
        // terhadap batas desimal. BigDecimal baik digunakan untuk nilai tukar mata uang
        // dimana sangat mementingkan presisi nilai desimal.
        //
        // BigDecimal dapat diinisialisasi dengan int, long, double, String,
        // atau dengan melakukan inisialisasi nilai tanpa skala (BigInteger) 
        // dan nilai dengan skala (int). 

        BigDecimal nilaiBigDecimal = new BigDecimal(nilaiBigInteger, nilaiInt);
        
        // Perlu diperhatikan konstruktor yang digunakan apakah float atau double
        // karena dapat mengakibatkan ketidak-akurasian float/double yang akan digunakan
        // dalam BigDecimal. Sebaiknya gunakan nilai String pada konstruktor
        // jika membutuhkan nilai pasti.
        
        BigDecimal sepuluhSen = new BigDecimal("0.1");


        // Strings
        String nilaiString1 = "Ini adalah contoh String!";

        // Karakter \n berfungsi untuk membuat baris baru 
        String nilaiString2 = "Menampilkan baris baru?\nTidak masalah!";
        // Karakter \t berfungsi untuk membuat tab antar karakter
        String nilaiString3 = "Ingin menambahkan sebuah tab?\tTidak masalah!";
        System.out.println(nilaiString1);
        System.out.println(nilaiString2);
        System.out.println(nilaiString3);

        // Larik (array)
        // Ukuran array harus ditentukan ketika instansiasi
        // Format berikut adalah beberapa cara deklarasi array
        // <tipe data>[] <nama variabel> = new <tipe data>[<ukuran array>];
        // <tipe data> <nama variabel>[] = new <tipe data>[<ukuran array>];
        int[] barisAngka = new int[10];
        String[] barisString = new String[1];
        boolean barisBoolean[] = new boolean[100];

        // Cara lain untuk mendeklarasikan dan menginisialisasi sebuah array
        int[] y = {9000, 1000, 1337};
        String nama[] = {"Andi", "Budi", "Agus"};
        boolean bools[] = new boolean[] {true, false, false};

        // Indeks sebuah array - Mengakses sebuah elemen
        System.out.println("barisAngka @ 0: " + barisAngka[0]);

        // Array menggunakan indeks 0 yang tetap.
        barisAngka[1] = 1;
        System.out.println("barisAngka @ 1: " + barisAngka[1]); // => 1

        // Lainnya yang perlu diketahui
        // ArrayLists - Sama seperti array biasa, namum penggunaannya sudah ditentukan,
        //              dan ukurannya dapat berubah-ubah.
        // LinkedLists - Implementasi dari doubly-linked list. Semua operasi yang digunakan
        //               hampir sama dengan operasi yang dimiliki oleh sebuah doubly-linked list.
        // Maps - Sebuah kumpulan objek yang menyatakan hubungan antara kunci dan nilai. Map merupakan
        //        sebuah interface sehingga tidak dapat diinstansiasi. Jenis kunci dan nilai yang digunakan
        //        pada Map harus spesifik pada saat instansiasi ketika diimplementasikan pada sebuah kelas.
        //        Setiap kunci hanya memiliki sebuah nilai, dan hanya muncul sekali.
        // HashMaps - Kelas ini menggunakan tabel-hash untuk mengimplementasikan interface Map.
        //            Hal ini memungkinkan waktu eksekusi ketika melakukan operasi dasar (mengakses
        //            dan menambahkan elemen) menjadi konstan, meskipun memiliki banyak set data.

        ///////////////////////////////////////
        // Operator
        ///////////////////////////////////////
        System.out.println("\n->Operator");

        int i1 = 1, i2 = 2; // Cara singkat untuk deklarasi banyak nilai

        // Kemudahan dalam artimatika
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (int/int menghasilkan int juga)
        System.out.println("1/2 = " + (i1 / (double)i2)); // => 0.5

        // Modulus
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Operator Perbandingan
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Operator Boolean
        System.out.println("3 > 2 && 2 > 3? " + ((3 > 2) && (2 > 3))); // => false
        System.out.println("3 > 2 || 2 > 3? " + ((3 > 2) || (2 > 3))); // => true
        System.out.println("!(3 == 2)? " + (!(3 == 2))); // => true

        // Operator Bitwise
        /*
        ~      Unary bitwise complement
        <<     Signed left shift
        >>     Signed/Arithmetic right shift
        >>>    Unsigned/Logical right shift
        &      Bitwise AND
        ^      Bitwise exclusive OR
        |      Bitwise inclusive OR
        */

        // Peningkatan
        int i = 0;
        System.out.println("\n->Pengurangan/Peningkatan");
        // Operator ++ dan -- masing-masing melakukan peningkatan dan penurunan 1 nilai.
        // Jika diletakkan sebelum variabel, maka akan di tambah/kurang 1 sebelum dilakukan perintah lainnya;
        // jika setelah variabel, maka akan ditambah/kurang 1 setelah dilakukan perintah lainnya;
        System.out.println(i++); // i = 1, prints 0 (peningkatan setelahnya)
        System.out.println(++i); // i = 2, prints 2 (peningkatan sebelumnya)
        System.out.println(i--); // i = 1, prints 2 (pengurangan setelahnya)
        System.out.println(--i); // i = 0, prints 0 (pengurangan sebelumnya)

        ///////////////////////////////////////
        // Struktur Kontrol
        ///////////////////////////////////////
        System.out.println("\n->Struktur Kontrol");

        // Perintah "if" hampir sama dengan bahasa C
        int j = 10;
        if (j == 10) {
            System.out.println("Saya ditampilkan");
        } else if (j > 10) {
            System.out.println("Saya tidak ditampilkan");
        } else {
            System.out.println("Saya juga tidak ditampilkan");
        }

        // Perulangan "while"
        int fooWhile = 0;
        while(fooWhile < 100) {
            System.out.println(fooWhile);
            // Tingkatkan penghitung
            // 100 kali iterasi, fooWhile 0,1,3,...,99
            fooWhile++;
        }
        System.out.println("Nilai fooWhile: " + fooWhile);

        // Perulangan "do...while"
        int fooDoWhile = 0;
        do {
            System.out.println(fooDoWhile);
            // Tingkatkan penghitung
            // 99 kali iterasi, fooDoWhile 0->99
            fooDoWhile++;
        } while(fooDoWhile < 100);
        System.out.println("Nilai fooDoWhile: " + fooDoWhile);

        // Perulangan "for"
        // Struktur perulangan "for" => for(<awal_pernyataan>; <kondisi>; <langkah/tahapan>)
        for (int fooFor = 0; fooFor < 10; fooFor++) {
            System.out.println(fooFor);
            // 10 kali iterasi, foofor 0-9
        }
        System.out.println("Nilai fooFor: " + fooFor);
        
        // Perulangan "for" bertingkat dengan label "exit"
        outer:
        for (int i = 0; i < 10; i++) {
          for (int j = 0; j < 10; j++) {
            if (i == 5 && j ==5) {
              break outer;
              // Menghentikan semua perulangan, tidak hanya perulangan bagian dalam saja 
            }
          }
        }
        
        // Perulangan "for each"
        // Perulangan "for" juga dapat melakukan iterasi terhadap larik (array) dari objek
        // yang mana mengimplementasikan interface Ieterable.
        int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        // Struktur perulangan "for each" => for (<objek> : <iterable>)
        // dibaca: setiap elemen dalam iterable
        // catatan: tipe objek harus sama dengan tipe iterable

        for (int bar : fooList) {
            System.out.println(bar);
            // Melakukan interasi sebanyak 9 kali dan menampilkan 1-9 tiap baris
        }

        // "switch case"
        // "switch" dapat digunakan pada byte, short, char, dan tipe data bilangan bulat (int).
        // "switch" juga dapat digunakan pada tipe "enum" (dijelaskan nanti), kelas String,
        // dan beberapa kelas khusus yang mengandung tipe data primitif:
        // Character, Byte, Short, dan Integer.
        int bulan = 3;
        String bulanString;
        switch (bulan) {
            case 1: bulanString = "Januari";
                    break;
            case 2: bulanString = "Februari";
                    break;
            case 3: bulanString = "Maret";
                    break;
            default: bulanString = "Bulan lainnya";
                     break;
        }
        System.out.println("Hasil switch case: " + bulanString);
        
        // Mulai dari Java 7 keatas, "switch" memiliki format:
        String jawabanSaya = "mungkin";
        switch(jawabanSaya) {
            case "ya":
                System.out.println("Anda menjawab ya.");
                break;
            case "tidak":
                System.out.println("Anda menjawab tidak.");
                break;
            case "mungkin":
                System.out.println("Anda menjawab mungkin.");
                break;
            default:
                System.out.println("Anda menjawab " + jawabanSaya);
                break;
        }

        // Pengkondisian dengan cara singkat
        // Karakter '?' dapat digunakan untuk penilaian atau logika secara cepat antara dua pernyataan.
        // Dibaca "Jika (pernyataan) adalah benar, gunakan <nilai pertama>, sisanya gunakan <nilai kedua>
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println(bar); // Menampilkan A, karena pernyataannya benar


        ////////////////////////////////////////
        // Konversi Data dan Tipe Data (Typecasting)
        ////////////////////////////////////////

        // Konversi Data

        // Konversi String ke Integer
        Integer.parseInt("123");    // menghasilkan nilai versi Integer dari "123"

        // Konversi Integer ke String
        Integer.toString(123);      // menghasilkan nilai versi String dari 123

        // Untuk konversi lainnya silakan coba kelas berikut:
        // Double
        // Long
        // String

        // Typecasting
        // Objek dalam Java juga dapat dikonversi, banyak penjelasan dan aturan
        // dengan beberapa konsep sederhana. Silakan cek di alamat berikut:
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Kelas dan Fungsi
        ///////////////////////////////////////

        System.out.println("\n->Kelas & Fungsi");

        // (penjelasan mengenai kelas "Sepeda" ada dibawah)

        // Gunakan "new" untuk melakukan instansiasi pada kelas
        Sepeda laju = new Sepeda();

        // Memanggil method objek
        laju.tambahKecepatan(3); // Dapat juga digunakan "setter" dan "getter" method
        laju.setIrama(100);

        // Method "toString()" menghasilkan representasi string dari objek.
        System.out.println("informasi jalur: " + laju.toString());

        // Dua Pasang Inisialisasi
        // Bahasa Java tidak memiliki sintaks untuk membuat koleksi dari "static" sekaligus
        // dengan mudah, kecuali dengan cara berikut:

        private static final Set<String> NEGARA = new HashSet<String>();
        static {
           validCodes.add("INDONESIA");
           validCodes.add("MALAYSIA");
           validCodes.add("SINGAPURA");
        }

        // Terdapat cara yang baik untuk menulis skrip dengan mudah,
        // dengan menggunakan Dua-Kurung Kurawal Inisialisasi (Double Brace Initialization)

        private static final Set<String> NEGARA = new HashSet<String>() {{
            add("INDONESIA");
            add("MALAYSIA");
            add("SINGAPURA");
        }}

        // Kurung kurawal yang pertama membuat sebuah AnonymousInnerClas
        // dan kurung kurawal yang kedua mendeklarasikan instance dari blok
        // inisialisasi. Blok ini kemudian dipanggil ketika InnerClass dibentuk.
        // Cara ini tidak hanya berfungsi pada koleksi data, juga dapat digunakan
        // pada semua kelas bukan-"final".

    } // Akhir dari method utama
} // Akhir dari kelas BelajarJava


// Kelas bukan-"public" lainnya dapat dimasukkan kedalam satu dokumen .java,
// namun tidak dianjurkan, sebaiknya memisahkan menjadi beberapa dokumen terpisah.

// Sintaks pendeklarasian kelas:
//<public/private/protected> class <nama kelas> {
//    // isi data, konstruktor, dan fungsi.
//    // dalam Java, fungsi biasa disebut juga "method"
// }

class Sepeda {

    // Variabel dari kelas Sepeda
    public int irama;       // Public: dapat diakses dari manapun
    private int kecepatan;  // Private: hanya dapat diakses dari dalam kelas
    protected int rodaGigi;     // Protected: dapat diakses dari dalam kelas dan turunan kelas
    String nama;            // Default: hanya dapat diakses kelas yang berada dalam paket yang sama

    static String namaKelas;    // Variabel "static"

    // Blok Static
    // Java tidak memiliki implementasi untuk konstruktor "static", namun
    // memiliki blok status yang dapat digunakan untuk inisialisasi variabel
    // dalam kelas (variabel "static").
    // Blok ini akan dipanggil secara otomatis ketika kelas dijalankan.
    static {
        namaKelas = "Sepeda";
    }

    // Konstruktor adalah salah satu cara untuk membuat kelas
    // Ini adalah bagian konstruktor
    public Sepeda() {
        // Dapat juga dipanggil konstruktor lainnya:
        // this(1, 50, 5, "Bontrager");
        rodaGigi = 1;
        irama = 50;
        kecepatan = 5;
        nama = "Bontrager";
    }

    // Ini adalah bagian konstruktor yang menggunakan argumen (parameter)
    public Sepeda(int iramaAwal, int kecepatanAwal, int rodaGigiAwal,
        String nama) {
        this.rodaGigi = rodaGigiAwal;
        this.irama = iramaAwal;
        this.kecepatan = kecepatanAwal;
        this.nama = nama;
    }

    // Sintaks untuk method:
    // <public/private/protected> <tipe kembalian> <nama fungsi>(<args>)

    // Kelas Java terkadang mengimplementasikan "getters" dan "setters" untuk data.

    // Sintaks untuk deklarasi method:
    // <public/private/protected> <tipe kembalian> <nama fungsi>(<args>)
    public int getIrama() {
        return irama;
    }

    // Tipe "void" tidak memiliki kembalian (return) nilai
    public void setIrama(int nilaiBaru) {
        irama = nilaiBaru;
    }

    public void setRodaGigi(int nilaiBaru) {
        rodaGigi = nilaiBaru;
    }

    public void tambahKecepatan(int nilaiTambahan) {
        kecepatan += nilaiTambahan;
    }

    public void kurangiKecepatan(int nilaiPengurangan) {
        kecepatan -= nilaiPengurangan;
    }

    public void setNama(String namaBaru) {
        nama = namaBaru;
    }

    public String getNama() {
        return nama;
    }

    // Method untuk menampilkan nilai dari tiap atribut yang dimiliki objek Sepeda.
    @Override // Diturunkan dari kelas "Object" (Pustaka Java).
    public String toString() {
        return "roda gigi: " + rodaGigi + " irama: " + irama + " kecepatan: " + kecepatan +
            " nama: " + nama;
    }
} // akhir dari kelas Sepeda

// PennyFarthing adalah kelas turunan dari Sepeda
class PennyFarthing extends Sepeda {
    // (Penny Farthings adalah sepeda dengan roda depan yang besar,
    // dan tidak memiliki roda gigi.)
    // (Penny Farthings are those bicycles with the big front wheel.
    // They have no gears.)

    public PennyFarthing(int startCadence, int startSpeed) {
        // Call the parent constructor with super
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // You should mark a method you're overriding with an @annotation.
    // To learn more about what annotations are and their purpose check this
    // out: http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setRodaGigi(int rodaGigi) {
        roda rodaGigi = 0;
    }
}

// Interfaces
// Sintaks untuk deklarasi Interface
// <level akses> interface <nama interface> extends <interface induk> {
//     // Konstan
//     // Deklarasi method
// }

// Contoh - Makanan:
public interface dapatDimakan {
    public void makan();    // Setiap kelas yang menggunakan interface "dapatDimakan",
                            // harus mengimplementasikan method "makan".
}

public interface dapatDicerna {
    public void cerna();
}


// Membuat kelas dengan mengimplementasikan dua interface dalam satu waktu.
public class Buah implements dapatDimakan, dapatDicerna {
  
    @Override
    public void makan() {
        // ...
    }

    @Override
    public void cerna() {
        // ...
    }
}

// Dalam Java, kelas hanya dapat diturunkan sekali, tapi dapat mengimplementasikan
// banyak interface. Contoh:
public class ContohKelas extends ContohKelasInduk implements InterfaceSatu,
    InterfaceDua {

    @Override
    public void MethodInterfaceSatu() {
    }

    @Override
    public void MethodInterfaceDua() {
    }

}

// Kelas Abstrak (Abstract)
// Sintaks untuk deklarasi kelas abstrak
// Abstract Class declaration syntax
// <level akses> abstract <nama kelas abstrak> extends <induk kelas abstrak> {
//     // Konstan dan variabel
//     // Deklarasi method

// Menjadikan kelas sebagai abstrak adalah memungkinkan kelas berisi method abstrak
// yang harus didefinisikan pada kelas turunannya. Mirip dengan Interface, kelas abstrak
// tidak dapat dilakukan instansiasi, namun harus diturunkan pada kelas lain dan method abstrak
// harus didefinisikan. Perbedaannya dengan Interface ialah kelas abstrak dapat berisi method
// kongkrit dan method abstrak. Pada Interface method tidak dapat memiliki isi, artinya hanya
// method statis, dan variabel langsung ditentukan menjadi final, tidak seperti kelas abstrak.
// Kelas abstrak juga dapat memiliki method "main".

public abstract class Hewan
{
    public abstract void bersuara();

    // Method biasa dapat memiliki isi
    public void makan()
    {
        System.out.println("Saya adalah hewan dan Saya makan.");
        // Catatan: Kita dapat mengakses variabel private yang ada disini.
        umur = 30;
    }

    // Tidak perlu dilakukan inisialisasi, berbeda dengan Interface
    // sebuah variabel adalah final dan harus dilakukan inisialisasi. 
    protected int umur;

    public void tampilkanUmur()
    {
        System.out.println(umur);  
    }

    // Kelas abstrak dapat memiliki fungsi utama (main).
    public static void main(String[] args)
    {
        System.out.println("Saya adalah kelas abstrak!");
    }
}

class Kucing extends Hewan
{
    // Catatan: kelas ini harus melakukan override method abstrak
    // yang ada pada kelas abstrak (induk).
    @Override
    public void bersuara()
    {
        System.out.println("Moe");
        // umur = 30;	==> ERROR!	umur merupakan variabel private pada abstrak Hewan
    }

    // CATATAN: Akan muncul error jika menggunakan 
    // keterangan @Override pada method utama (main),
    // Java tidak mengizinkan hal tersebut.
    // Kejadian ini sering disebut sebagai METHOD HIDING.
    // Pertanyaan-jawaban yang menarik dapat dilihat: http://stackoverflow.com/questions/16313649/
    public static void main(String[] args)
    {
        Kucing moe = new Kucing();
        noe.bersuara();
        moe.makan();
        moe.tampilkanUmur();
    }
}

// Kelas Final

// Sintaks untuk deklarasi kelas Final
// <level akses> final <nama kelas final> {
//     // Konstann dan variabel
//     // Deklarasi method
// }

// Kelas Final merupakan kelas yang tidak dapat diturunkan sehingga menjadikan
// method tersebut turunan method terakhir. Disisi lain, kelas final merupakan
// lawan dari kelas abstrak karena kelas abstrak dapat diturunkan lagi, sedangkan
// kelas final tidak dapat diturunkan lagi.
public final class Serigala extends Hewan
{
    // Catatan: method abstrak harus di-override pada kelas abstrak.
    @Override
    public void bersuara()
    {
        System.out.println("Auuww");
    }
}

// Method Final
public abstract class Mamalia()
{
    // Sintaks untuk method final:
    // <level akses> final <tipe kembalian> <nama fungsi>(<args>)

    // Method final, seperti kelas final tidak dapat di-override oleh kelas turunan,
    // sehingga menjadikannya implementasi terakhir dari method.
    public final boolean apakahBerdarahDingin()
    {
        return true;
    }
}


// Tipe Enum
//
// Tipe Enum merupakan tipe data spesial yang memungkinkan sebuah nilai dijadikan
// konstan awal (predefined). Variabel setidaknya harus memiliki nilai yang sama
// dengan salah satu dari enum-enum yang telah ditentukan. Karena nilainya merupakan
// konstan, untuk itu penamaannya menggunakan huruf kapital (uppercase). Dalam Java,
// Enum didefinisikan dengan kata kunci "enum". Contohnya nama-nama hari dalam semunggu:

public enum Hari {
    SENIN, SELASA, RABU, KAMIS,
    JUMAT, SABTU, MUNGGU 
}

// Cara menggunakan Enum:
public class CobaEnum {
    
    // Variabel Enum
    Hari hari;
    
    // Konstruktor
    public CobaEnum(Hari hari) {
        this.hari = hari;
    }
    
    public void tampilkanKeterangan() {
        switch (day) {
            case SENIN:
                System.out.println("Senin adalah hari yang menyebalkan.");
                break;
                    
            case JUMAT:
                System.out.println("Jumat adalah hari yang singkat.");
                break;
                         
            case SABTU: 
            case MINGGU:
                System.out.println("Akhir pekan adalah hari yang menyenangkan.");
                break;
                        
            default:
                System.out.println("Hari kerja yang biasa saja.");
                break;
        }
    }
    
    public static void main(String[] args) {
        CobaEnum hariPertama = new CobaEnum(Hari.SENIN);
        hariPertama.tampilkanKeterangan();      // Senin adalah hari yang menyebalkan.
        CobaEnum hariKetiga = new CobaEnum(Hari.RABU);
        hariPertama.tampilkanKeterangan();      // Hari kerja yang biasa saja.
    }
}

// Tipe enum memiliki banyak kegunaan selain yang dicontohkan diatas.
// Tipe enum dapat memiliki isi seperti method dan variabel.
// Penjelasan lebih detail di https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

```

## Referensi Lainnya

Link-link berikut hanya menyediakan pemahaman lebih lanjut mengenai topik diatas. 
Tip, trik, dan contoh lainnya dapat melakukan pencarian melalui Google atau mesin pencari yang lain. 

**Panduan resmi Oracle**

* [Java Tutorial Trail from Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Java Access level modifiers](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Object-Oriented Programming Concepts](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Inheritance](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Abstraction](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Interfaces](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html)

**Tutorial dan Praktik Online**

* [Learneroo.com - Learn Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)


**Buku**:

* [Head First Java](http://www.headfirstlabs.com/books/hfjava/)

* [Thinking in Java](http://www.mindview.net/Books/TIJ/)

* [Objects First with Java](http://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660)

* [Java The Complete Reference](http://www.amazon.com/gp/product/0071606300)
---
language: json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
filename: learnjson-id.json
translators:
  - ["Rizky Luthfianto", "https://github.com/rilut"]
  - ["Ahmad Zafrullah", "https://github.com/23Pstars"]
lang: id-id
---

JSON adalah format pertukaran data yang sangat sederhana. Sebagaimana dikutip dari [json.org](http://json.org), JSON mudah untuk dibaca atau ditulis oleh manusia, dan mudah diuraikan dan diproses oleh mesin.

Sebuah format JSON setidaknya memiliki:
* Sebuah pasangan nama atau nilai dinyatakan dengan karakter (`{ }`). Dibeberapa bahasa pemrograman, karakter ini sering digunakan sebagai object, record, struct, dictionary, hash table, keyed list, atau associative array.
* Daftar nilai dinyatakan dengan karakter (`[ ]`). Dibeberapa bahasa pemrograman, karakter ini sering digunakan sebagai array, vector, list, atau sequence.

Format JSON murni tidak memiliki komentar, namun beberapa pengurai (parser) dapat mengenali komentar seperti yang digunakan oleh bahasa C (`//`, `/**/`). Beberapa pengurai lainnya juga memiliki toleransi terhadap akhiran sisa koma (seperti koma yang terdapat pada akhir elemen dari larik atau properti terakhir dari objek), tapi koma tersebut memang seharusnya diabaikan untuk dukungan yang lebih baik.

Dalam tutorial ini, semuanya menggunakan format JSON murni.

Tipe data yang didukung oleh JSON:

* Teks: `"halo"`, `"\"tanda petik.\""`, `"\u0abe"`, `"baris baru.\n"`
* Angka: `23`, `0.11`, `12e10`, `3.141e-10`, `1.23e+4`
* Objek: `{ "kunci": "nilai" }`
* Larik: `["nilai"]`
* Lainnya: `true`, `false`, `null`

```json
{
  "kunci": "nilai",
  
  "kunci": "harus selalu diapit tanda kutip",
  "angka": 0,
  "strings": "Halø, dunia. Semua karaktor unicode diperbolehkan, terumasuk \"escaping\".",
  "punya tipe data boolean?": true,
  "nilai kosong": null,

  "angka besar": 1.2e+100,

  "obyek": {
    "komentar": "Most of your structure will come from objects.",

    "array": [0, 1, 2, 3, "Array bisa berisi apapun.", 5],

    "obyek lainnya": {
      "komentar": "Obyek-obyek JSON dapat dibuat bersarang, sangat berguna."
    }
  },

  "iseng-iseng": [
    {
      "sumber potassium": ["pisang"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "gaya alternatif": {
    "komentar": "lihat ini!"
  , "posisi tanda koma": "tak masalah. selama sebelum nilai berikutnya, valid-valid saja"
  , "komentar lainnya": "betapa asyiknya"
  },

  "singkat": "Dan Anda selesai! Sekarang Anda tahu apa saja yang disediakan oleh JSON."
}
```

## Referensi lebih labjut

* [JSON.org](http://json.org/json-id.html) semua keindahan JSON dijelaskan dalam bentuk alur-grafis (bahasa indonesia).
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Tasya Aditya Rukmana", "http://github.com/tadityar"]
lang: id-id
filename: markdown-id.md
---

Markdown dibuat oleh John Gruber pada tahun 2004. Tujuannya untuk menjadi syntax yang mudah dibaca dan ditulis yang dapat berubah menjadi HTML (dan sekarang berbagai format lainnya) dengan mudah.

Beri masukan sebanyak-banyaknya! / Jangan sungkan untuk melakukan fork dan pull request!


```markdown
<!-- Markdown adalah superset dari HTML, jadi setiap berkas HTML adalah markdown yang
valid, ini berarti kita dapat menggunakan elemen HTML dalam markdown, seperti elemen
komentar, dan ia tidak akan terpengaruh parser markdown. Namun, jika Anda membuat
elemen HTML di berkas markdown Anda, Anda tidak dapat menggunakan syntax markdown
di dalam konten elemen tersebut. -->

<!-- Markdown juga bervariasi dalam implementasinya dari berbagai parser. Panduan ini
akan mencoba untuk mengklarifikasikan kapan suatu fitur universal atau spesifik
terhadap parser tertentu -->

<!-- Header -->
<!-- Anda dapat membuat elemen HTML <h1> sampai <h6> dengan mudah dengan mendahului
teks yang diinginkan untuk elemen tersebut oleh sejumlah tanda pagar (#) -->
# Ini adalah <h1>
## Ini adalah <h2>
### Ini adalah <h3>
#### Ini adalah <h4>
##### Ini adalah <h5>
###### Ini adalah <h6>

<!-- Markdown juga menyediakan dua cara alternatif untuk menandai h1 and h2 -->
Ini adalah h1
=============

Ini adalah h2
-------------

<!-- Ragam teks simpel -->
<!-- Teks dapat diubah menjadi miring atau tebal dengan mudah menggunakan markdown -->

*Ini adalah teks miring.*
_Dan juga teks ini._

**Ini adalah teks tebal.**
__Dan juga teks ini.__

***Ini adalah teks dengan keduanya.***
**_Dan juga ini!_**
*__Dan ini!__*

<!-- Di markdown ala Github, yang digunakan untuk me-render berkas markdown pada
Github, kita juga punya coretan: -->

~~Teks ini dirender dengan coretan.~~

<!-- Paragraf adalah satu atau beberapa baris teks yang dipisahkan oleh satu atau
beberapa baris kosong. -->

Ini adalah paragraf. Saya mengetik dalam paragraf, bukankah ini menyenangkan?

Sekarang saya ada di paragraf 2.
Saya juga masih ada dalam paragraf 2!


Saya ada di paragraf 3!

<!-- Jika Anda ingin memasukkan tag HTML <br />, Anda dapat mengakhiri sebuah
paragraf dengan dua atau lebih spasi lalu memulai paragraf yang baru. -->

Aku diakhiri dua spasi (soroti aku untuk melihatnya).

Ada sebuah <br /> diatasku!

<!-- Kutipan mudah dibuat dengan karakter >. -->

> Ini adalah kutipan. Anda dapat
> membungkusnya secara manual dan meletakkan `>` sebelum tiap baris atau Anda dapat membuat baris yang sangat panjang dan membuatnya membungkus secara otomatis.
> Tidak ada masalah selama ia diawali dengan `>`.

> Anda juga dapat menggunakan lebih dari satu level
>> indentasi!
> Sangat rapi bukan?

<!-- Daftar -->
<!-- Daftar tak beraturan dapat dibuat dengan bintang, plus, atau strip -->

* Item
* Item
* Item lainnya

atau

+ Item
+ Item
+ Satu lagi item

or

- Item
- Item
- Item terakhir

<!-- List beraturan dibuat dengan angka diikuti titik -->

1. Item satu
2. Item dua
3. Item tiga

<!-- Anda tidak diharuskan melabeli item dengan benar dan markdown akan tetap
me-render angka sesuai urutan, namun mungkin hal ini kurang baik -->

1. Item satu
1. Item dua
1. Item tida
<!-- (Ini dirender sama seperti contoh di atas) -->

<!-- Anda juga dapat menggunakan sublist -->

1. Item satu
2. Item dua
3. Item tiga
    * Sub-item
    * Sub-item
4. Item empat

<!-- Bahkan ada daftar tugas. Ini membuat kotak centang HTML. -->

Kotak di bawah tanpa 'x' adalah kotak centang HTML yang belum diisi.
- [ ] Tugas pertama selesai.
- [ ] Tugas kedua yang harus diselesaikan
Kotak centang HTML berikut telah diisi.
- [x] Tugas ini telah diselesaikan

<!-- Blok kode -->
<!-- Anda dapat menandai blok kode (yang menggunakan elemen <code>) dengan mengindentasi
sebuah garis dengan empat spasi atau tab -->

    Ini adalah kode
    Dan ini juga

<!-- Anda juga dapat me-re-tab (atau menambahkan empat spasi tambahan) untuk indentasi
di dalam kode Anda -->

    array_ku.each do |item|
        puts item
    end

<!-- Sebaris kode dapat dibuat dengan karakter backtick ` -->

John bahkan tidak tahu apa fungsi dari `go_to()` !

<!-- Di Markdown ala Github, Anda dapat menggunakan syntax spesial untuk kode -->

\`\`\`ruby <!-- kecuali hapus backlash tersebut ketika melakukannya, hanya ```ruby ! -->
def foobar
    puts "Halo Dunia!"
end
\`\`\` <!-- Disini juga, tidak ada backslashes, hanya ``` -->

<!-- Teks di atas tidak membutuhkan indentasi, plus Github akan menggunakan syntax
highlighting dari bahasa yang digunakan setelah ``` -->

<!-- Horizontal rule (<hr />) -->
<!-- Horizontal rules ditambahkan dengan mudah oleh beberapa bintang atau strip,
dengan atau tanpa spasi. -->

***
---
- - -
****************

<!-- Tautan -->
<!-- Salah satu hal terbaik dari markdown adalah mudahnya membuat tautan. Letakkan
teks yang akan di tampilkan di dalam kurung siku [] diikuti oleh url-nya dalam kurung () -->

[Klik aku!](http://test.com/)

<!-- Anda juga dapat menambahkan judul link dengan tanda kutip di dalam kurung -->

[Klik aku!](http://test.com/ "Link to Test.com")

<!-- Path relatif juga bisa. -->

[Pergi ke musik](/music/).

<!-- Markdown juga mendukung tautan gara referal -->

[Klik link ini][link1] untuk info lebih banyak!
[Juga cek link ini][foobar] jika Anda mau.

[link1]: http://test.com/ "Keren!"
[foobar]: http://foobar.biz/ "OK!"

<!-- Judulnya juga bisa dalam kutip satu atau kurung, atau dihilangkan sepenuhnya.
Referensinya juga bisa di mana saja di dokumen anda dan IF referensinya bisa jadi
apa saja selama ia unik. -->

<!-- Ada juga "penamaan implisit" yang membuat Anda dapat menggunakan teks tautan sebagai id -->

[Ini][] adalah tautan.

[ini]: http://thisisalink.com/

<!-- Tapi ia tidak lazim digunakan. -->

<!-- Gambar -->
<!-- Gambar digunakan sama seperti tautan namun dengan tanda seru di depannya! -->

![Ini adalah atribut alt dari gambar saya](http://imgur.com/myimage.jpg "Judul opsional")

<!-- Dan gaya referensi juga bekerja seperti yang diharapkan -->

![Ini adalah atribut alt.][myimage]

[myimage]: relative/urls/cool/image.jpg "jika Anda membutuhkan judul, disini"

<!-- Lain-lain -->
<!-- Tautan otomatis -->

<http://testwebsite.com/> sama dengan
[http://testwebsite.com/](http://testwebsite.com/)

<!-- Tautan otomatis untuk email -->

<foo@bar.com>

<!-- Melewati karakter -->

Saya ingin mengetik *teks ini dikelilingi tanda bintang* tapi saya tidak mau teksnya menjadi
miring, jadi saya melakukan: \*teks ini dikelilingi tanda bintang\*.

<!-- Tombol keyboard -->
<!-- Pada Markdown ala Github, Anda dapat menggunakan tag <kbd> untuk merepresentasikan tombol
keyboard -->

Komputer Anda hang? Coba kirim sebuah
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>

<!-- Tabel -->
<!-- Tabel hanya tersedia pada Markdown ala Github dan sedikit merepotkan, namun jika Anda
sangat menginginkannya: -->

| Kol1         | Kol2     | Kol3          |
| :----------- | :------: | ------------: |
| Rata-kiri    | Tengah   | Rata-Kanan    |
| blah         | blah     | blah          |

<!-- atau, untuk hasil yang sama -->

Kol 1 | Kol2 | Kol3
:-- | :-: | --:
Ugh ini sangat jelek | buat ia | berhenti

<!-- Selesai! -->

```

Untuk info lebih lanjut, cek post syntax resmi John Gruber [di sini](http://daringfireball.net/projects/markdown/syntax) dan contekan hebat Adam Pritchard's [di sini](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: PHP
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
filename: learnphp-id.php
translators:
  - ["Ahmad Zafrullah", "https://github.com/23Pstars"]
lang: id-id
---

Dokumen ini menjelaskan tentang PHP5 keatas.

```php
<?php // Skrip PHP harus diawali dengan tag <?php

// Jika dokumen PHP hanya mengandung kode PHP, sebaiknya tidak menggunakan
// tag penutup PHP untuk menghindari ketidaksengajaan tampilnya sesuatu.

// Dua garis miring diawal digunakan untuk komentar satu baris.

/*
     Membatasi teks dalam garis miring-bintang dan bintang-garis miring
     membuat komentar untuk banyak-baris sekaligus.
*/

// Gunakan "echo" or "print" untuk menampilkan sesuatu
print('Halo '); // Menampilkan "Halo " tanpa baris baru

// () boleh tidak digunakan dalam menggunakan "print" dan "echo"
echo "Dunia\n"; // Menampilkan "Dunia" dengan baris baru
// (semua perintah harus diakhiri dengan titik koma)

// Apapun yang berada diluar tag <?php akan ditampilkan secara otomatis
?>
Halo Dunia, lagi!
<?php


/************************************
 * Tipe Data & Variabel
 */

// Variabel diawali dengan simnbol $.
// Nama variabel yang benar diawali dengan huruf atau garis-bawah,
// diikuti dengan beberapa huruf, angka, dan garis-bawah lainnya.

// Nilai Boolean adalah case-insensitive
$boolean = true;  // atau TRUE atau True
$boolean = false; // atau FALSE atau False

// Nilai Integer
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (awalan 0 menandakan bilangan Oktal)
$int4 = 0x0F; // => 15 (awalan 0x menandakan bilangan Heksadesimal)
// Bilangan Biner Integer tersedia mulai dari PHP 5.4.0.
$int5 = 0b11111111; // 255 (awalan 0b menandakan bilangan Biner)

// Nilai Floats (dikenal juga sebagai Doubles)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// Menghapus variable
unset($int1);

// Aritmatika
$jumlah         = 1 + 1; // 2
$selisih        = 2 - 1; // 1
$perkalian      = 2 * 2; // 4
$pembagian      = 2 / 1; // 2

// Aritmatika singkat
$angka = 0;
$angka += 1;        // Menjumlahkan $angka dengan 1
echo $angka++;      // Menampilkan 1 (dijumlahkan dengan 1 setelah ditampilkan)
echo ++$angka;      // Menampilkan 3 (dijumlahkan dengan 1 sebelum ditampilkan)
$angka /= $float;   // Membagi dan menyimpan hasil pembagian pada $angka;

// String biasanya diawali dan ditutup dengan petik satu.
$sgl_quotes = '$String'; // => '$String'

// Hindari menggunakan petik dua kecuali menyertakan variabel lain
$dbl_quotes = "Ini adalah $sgl_quotes."; // => 'Ini adalah $String.'

// Karakter khusus hanya berlaku pada petik dua
$berfungsi          = "Ini mengandung \t karakter tab.";
$tidak_berfungsi    = 'Ini hanya mengandung garis miring dan huruf t: \t';

// Batasi variabel dengan kurung kurawal jika diperlukan
$uang = "Saya memiliki $${angka} di Bank.";

// Sejak PHP 5.3, nowdocs dapat digunakan untuk tak-terinterpolasi banyak-baris
$nowdoc = <<<'END'
Banyak baris
string
END;

// Heredocs akan melakukan interpolasi
$heredoc = <<<END
Banyak baris
$sgl_quotes
END;

// Menyambung string dapat dilakukan menggunakan .
echo 'String ini ' . 'tersambung';

// String dapat dijadikan parameter pada "echo"
echo 'Banyak', 'Parameter', 'String';  // Menampilkan 'BanyakParameterString'


/********************************
 * Konstan
 */

// Sebuah konstan didifinisikan menggunakan fungsi define()
// dan tidak bisa diganti/rubah selama program berjalan!

// Nama konstan yang benar diawali dengan huruf dan garis-bawah,
// diikuti dengan beberapa huruf, angka, atau garis-bawah.
define("FOO", "sesuatu");

// Mengakses konstan memungkinkan untuk dapat dipanggil tanpa menggunakan simbol $
echo FOO; // Menampilkan 'sesuatu'
echo 'Keluaran ini adalah ' . FOO;  // Menampilkan 'Keluaran ini adalah sesuatu'



/********************************
 * Larik (Array)
 */

// Semua larik dalam PHP bersifat asosiatif (saling berhubungan).

// Berfungsi pada semua versi PHP
$asosiatif = array('Satu' => 1, 'Dua' => 2, 'Tiga' => 3);

// Pada PHP 5.4 diperkenalkan cara penulisan (sintaks) baru
$asosiatif = ['Satu' => 1, 'Dua' => 2, 'Tiga' => 3];

echo $asosiatif['Satu']; // menampilkan 1

// Daftar literal secara tidak langsung ditentukan oleh kunci integer
$larik = ['Satu', 'Dua', 'Tiga'];
echo $larik[0]; // => "Satu"

// Menambahkan sebuah elemen pada akhir larik
$larik[] = 'Empat';
// atau
array_push($larik, 'Lima');

// Menghapus elemen dari larik
unset($larik[3]);

/********************************
 * Keluaran
 */

echo('Halo Dunia!');
// Menampilkan Halo Dunia! ke "stdout".
// "stdout" adalah sebuah halaman web ketika dijalankan dalam peramban (browser).

print('Halo Dunia!'); // Sama seperti "echo"

// "echo" dan "print" merupakan bahasa konstruksi, jadi tanda kurung dapat dihilangkan
echo 'Halo Dunia!';
print 'Halo Dunia!';

$paragraf = 'paragraf';

echo 100;           // Menampilkan variabel skalar secara langsung
echo $paragraf;     // atau sebuat variabel

// Jika PHP tag-singkat telah dikonfigurasi, atau versi PHP yang digunakan
// adalah 5.4.0 keatas, dapat digunakan sintaks "echo" singkat

?>
<p><?= $paragraf ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $x sekarang berisi nilai yang sama dengan $y
$z = &$y;
// $z sekarang berisi referensi ke $y. Mengubah nilai dari $z
// akan mengubah nilai dari $y juga, begitupun sebaliknya.
// $x tetap tidak berubah sebagaimana nilai asli dari $y

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0

// Menampilkan tipe dan nilai dari variabel ke "stdout"
var_dump($z); // prints int(0)

// Menampilkan variabel ke "stdout" dalam format yang mudah dibaca
print_r($larik); // menampilkan: Array ( [0] => Satu [1] => Dua [2] => Tiga )

/********************************
 * Logika
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// menegaskan lemparan sebuah peringatan jika pernyataan tidak benar

// Perbandingan berikut akan selalu benar, meskipun memiliki tipe yang berbeda.
assert($a == $b); // kesamaan
assert($c != $a); // ketidak-samaan
assert($c <> $a); // versi lain dari ketidak-samaan
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// Dibawah ini hanya akan bernilai benar jika nilainya memiliki tipe yang sama.
assert($c === $d);
assert($a !== $d);
assert(1 === '1');
assert(1 !== '1');

// Operator 'Spaceship' (sejak PHP 7)
// Mengembalikan 0 jika nilai pada kedua sisi adalah sama
// Mengembalikan 1 jika nilai pada sisi kiri lebih besar
// Mengembalikan -1 jika nilai pada sisi kanan lebih besar

$a = 100;
$b = 1000;

echo $a <=> $a; // 0 karena keduanya sama
echo $a <=> $b; // -1 karena $a < $b
echo $b <=> $a; // 1 karena $b > $a

// Variabel dapat dikonversi menjadi tipe lain, sesuai penggunaannya.

$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2 (string dipaksa menjadi integer)

$string = 'satu';
echo $string + $string; // => 0
// Menghasilkan 0 karena operator (+) tidak dapat memaksa string 'satu' menjadi sebuah integer

// Perubahan tipe dapat dimanfaatkan untuk diperlakukan sebagai tipe lainnya

$boolean = (boolean) 1; // => true

$nol = 0;
$boolean = (boolean) $nol; // => false

// Terdapat juga fungsi khusus untuk melakukan perubahan terhadap beberapa tipe
$integer = 5;
$string = strval($integer);

$var = null; // Nilai Null


/********************************
 * Struktur Kontrol
 */

if (true) {
    print 'Saya tampil';
}

if (false) {
    print 'Saya tidak tampil';
} else {
    print 'Saya tampil';
}

if (false) {
    print 'Tidak tampil';
} elseif(true) {
    print 'Tampil';
}

// operator ternary
print (false ? 'Tidak tampil' : 'Tampil');

// cara pintas operator ternary mulai dirilis sejak PHP 5.3
// persamaan dari "$x ? $x : 'Kerjakan'"
$x = false;
print($x ?: 'Kerjakan');

// operator null coalesce sejak PHP 7
$a = null;
$b = 'Ditampilkan';
echo $a ?? 'a belum di-set'; // menampilkan 'a belum di-set'
echo $b ?? 'b belum di-set'; // menampilkan 'Ditampilkan'


$x = 0;
if ($x === '0') {
    print 'Tidak ditampilkan';
} elseif($x == '1') {
    print 'Tidak ditampilkan';
} else {
    print 'Tampil';
}


// Alternatif sintaks untuk kebutuhan templat:
?>

<?php if ($x): ?>
Ini ditampilkan jika pengujian benar.
<?php else: ?>
Selain tersebut ini yang akan ditampilkan.
<?php endif; ?>

<?php

// Gunakan "switch" untuk menghemat logika.
switch ($x) {
    case '0':
        print 'Switch mendukung tipe paksaan';
        break; // Kata kunci "break" harus disertakan, jika tidak
               // maka logika tersebut akan berlanjut ke bagian "dua" dan "tiga"
    case 'dua':
    case 'tiga':
        // Lakukan sesuatu jika $x bernilai "dua" atau "tiga"
        break;
    default:
        // Aksi cadangan
}

// "while", "do...while" dan perulangan "for"
$i = 0;
while ($i < 5) {
    echo $i++;
}; // Menampilkan "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // Menampilkan "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // Menampilkan "0123456789"

echo "\n";

$roda = ['sepeda' => 2, 'mobil' => 4];

// Perulangan "foreach" dapat melakukan iterasi pada larik (array) 
foreach ($roda as $jumlah_roda) {
    echo $jumlah_roda;
} // Menampilkan "24"

echo "\n";

// Iterasi dapat dilakukan terhadap "key" (kunci) dan "value" (nilai)
foreach ($roda as $mesin => $jumlah_roda) {
    echo "$mesin memiliki $jumlah_roda buah roda";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // Menghentikan proses perulangan
    }
    echo $i++;
} // Menampilkan "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // Melewati tahapan iterasi saat ini
    }
    echo $i;
} // Menampilkan "0124"


/********************************
 * Fungsi
 */

// Fungsi didefinisikan dengan "function":
function fungsi_saya () {
    return 'Halo';
}

echo fungsi_saya(); // => "Halo"

// Nama fungsi yang baik dan benar diawali dengan sebuah huruf atau garis-bawah, diikuti oleh
// beberapa huruf, angka, atau garis-bawah.

function jumlah ($x, $y = 1) { // $y merupakan opsional, jika tidak ditentukan akan bernilai 1
    $hasil = $x + $y;
    return $hasil;
}

echo jumlah(4); // => 5
echo jumlah(4, 2); // => 6

// $hasil tidak dapat diakses dari luar fungsi
// print $hasil; // Akan menghasilkan sebuah "warning".

// Sejak PHP 5.3 fungsi dapat dideklarasikan menjadi tanpa-nama (anonymous);
$inc = function ($x) {
    return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
    echo "$x - $y - $z";
}

// Fungsi dapat mengembalikan fungsi juga
function bar ($x, $y) {
    // Gunakan "use" untuk mengakses variabel diluar fungsi
    return function ($z) use ($x, $y) {
        foo($x, $y, $z);
    };
}

$bar = bar('A', 'B');
$bar('C'); // Menampilkan "A - B - C"

// Fungsi uang memiliki nama dapat dipanggil berdasarkan string
$nama_fungsi = 'jumlah';
echo $nama_fungsi(1, 2); // => 3
// Bermanfaat untuk menentukan fungsi mana yang akan dipanggil secara dinamis.
// Atau, dapat juga menggunakan fungsi call_user_func(callable $callback [, $parameter [, ... ]]);

// Akses semua parameter yang dikirim ke sebuah fungsi
function parameter() {
    $jumlah_param = func_num_args();
    if( $jumlah_param > 0 ) {
        echo func_get_arg(0) . ' | ';
    }
    $daftar_param = func_get_args();
    foreach( $daftar_param as $kunci => $param ) {
        echo $kunci . ' - ' . $param . ' | ';
    }
}

parameter('Halo', 'Dunia'); // Halo | 0 - Halo | 1 - Dunia |

// Sejak PHP 5.6, mendapatkan jumlah variabel yang ada pada parameter 
function variabel($kata, ...$daftar) {
	echo $kata . " || ";
	foreach ($daftar as $item) {
		echo $item . ' | ';
	}
}

variable("Pemisah", "Halo", "Dunia") // Pemisah || Halo | Dunia | 

/********************************
 * Penyertaan ("include")
 */

<?php
// Skrip PHP yang berada dalam dokumen "include" juga harus dibuka dengan tag PHP.

include 'dokumen-saya.php';
// Kode yang ada dalam dokumen-saya.php sekarang dapat diakses dari cakupan saat ini.
// Jika dokumen tidak dapat disertakan (include, seperti dokumen tidak ditemukan), maka pesan peringatan akan muncul.

include_once 'dokumen-saya.php';
// Jika dokumen-saya telah disertakan (include) oleh perintah sebelumnya, maka
// dokumen tersebut tidak akan disertakan lagi. Ini bertujuan untuk menghindari kesalahan 
// yang diakibatkan oleh deklarasi ganda.

require 'dokumen-saya.php';
require_once 'dokumen-saya.php';
// Memiliki fungsi yang sama dengan "include", namun jika dokumen tidak ditemukan 
// atau tidak dapat disertakan maka akan menghasilkan pesan kesalahan fatal.

// Isi dari dokumen-saya.php:
<?php

return 'Apapun yang kamu suka.';
// akhir dari dokumen

// "include" dan "require" dapat mengembalikan sebuah nilai.
$nilai = include 'dokumen-saya.php';

// Dokumen akan disertakan berdasarkan lokasi direktori dokumen (file path) yang diberikan, jika tidak didefinisikan
// maka akan digunakan konfigurasi dari "include_path". Jika dokumen tidak ditemukan dalam "include_path",
// fungsi include akan melakukan pengecekan pada direktori yang sama dengan dokumen yang menggunakan fungsi include tersebut,
// jika tidak ditemukan juga maka pesan gagal akan dimunculkan.
/* */

/********************************
 * Kelas (class)
 */

// Kelas didefinisikan dengan kata "class"

class KelasSaya
{
    const NILAI_KONSTAN = 'nilai'; // Sebuah konstan

    static $nilaiStatis = 'statis';

    // Variabel statis dan hak jenis aksesnya
    public static $variabelStatisPublik = 'nilaiStatisPublik';
    // Hanya dapat diakses dalam kelas
    private static $variabelStatisPrivat = 'nilaiStatisPrivat';
    // Dapat diakses dalam kelas dan kelas turunan
    protected static $variabelStatisTerlindungi = 'nilaiStatisTerlindungi';

    // Properti harus mendeklarasikan hak aksesnya
    public $properti    = 'publik';
    public $PropertiInstansi;
    protected $variabel = 'terlindungi'; // Dapat diakses dari kelas itu sendiri dan kelas turunannya
    private $variabel   = 'tersembunyi';   // Hanya dapat diakses dari kelas itu sendiri

    // Membuat konstruktor dengan perintah __construct
    public function __construct($PropertiInstansi) {
        // Akses variabel instansi menggunakan perintah $this
        $this->PropertiInstansi = $PropertiInstansi;
    }

    // Method dideklarasikan sebagai fungsi didalam kelas
    public function methodSaya()
    {
        print 'KelasSaya';
    }

    // Perintah "final" membuat sebuah fungsi tidak dapat di-override oleh kelas turunannya
    final function tidakDapatDiOverride()
    {
    }

/*
 * Deklarasi properti atau method pada kelas sebagai statis membuat properti atau method tersebut
 * dapat diakses tanpa melakukan instansiasi kelas. Properti statis tidak dapat diakses melalui
 * objek kelas yang hasil instansiasi, sedangkan method statis bisa.
 */

    public static function methodStatisSaya()
    {
        print 'Saya adalah statis';
    }
}

// Konstan pada kelas dapat diakses secara statis
echo KelasSaya::NILAI_KONSTAN;      // Menampilkan 'nilai'

echo KelasSaya::$nilaiStatis;       // Menampilkan 'statis'
KelasSaya::methodStatisSaya();      // Menampilkan 'Saya adalah statis'

// Instansi kelas menggunakan perintah "new"
$kelas_saya = new KelasSaya('Sebuah properti instansiasi');
// Tanda kurung adalah opsional jika tidak ingin menggunakan argumen.

// Akses anggota kelas menggunakan ->
echo $kelas_saya->properti;             // => "publik"
echo $kelas_saya->propertiInstansi;     // => "Sebuah properti instansi"
$kelas_saya->methodSaya();              // => "KelasSaya"

// Menurunkan kelas menggunakan kata kunci "extends"
class KelasSayaLainnya extends KelasSaya
{
    function tampilkanPropertiTerlindungi()
    {
        echo $this->properti;
    }

    // "override" terhadap sebuah method
    function methodSaya()
    {
        parent::methodSaya();
        print ' > KelasSayaLainnya';
    }
}

$kelas_saya_lainnya = new KelasSayaLainnya('Instansiasi properti');
$kelas_saya_lainnya->tampilkanPropertiTerlindung();     // => Menampilkan "terlindungi"
$kelas_saya_lainnya->methodSaya();                      // Menampilkan "KelasSaya > KelasSayaLainnya"

final class SayaTidakBisaDiturunkan
{
}

// Gunakan method ajaib (magic method) untuk membuat fungsi "getters" dan "setters"
class PetaKelasSaya
{
    private $properti;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new PetaKelasSaya();
echo $x->properti;          // akan memanggil method __get()
$x->properti = 'Sesuatu';   // akan memanggil method __set();

// Kelas dapat dijadikan abstrak (menggunakan kata kunci "abstract"), atau
// meng-implementasikan interfaces (menggunakan kata kunci "implements").
// Sebuah interface dideklarasikan dengan perintah "interface".

interface InterfaceSatu
{
    public function kerjakanSesuatu();
}

interface InterfaceDua
{
    public function kerjakanYangLain();
}

// interface dapat diturunkan
interface InterfaceTiga extends InterfaceDua
{
    public function kerjakanYangBerbeda();
}

abstract class KelasAbstrakSaya implements InterfaceSatu
{
    public $x = 'kerjakanSesuatu';
}

class KelasKongkritSaya extends KelasAbstrakSaya implements InterfaceTwo
{
    public function kerjakanSesuatu()
    {
        echo $x;
    }

    public function kerjakanYangLain()
    {
        echo 'kerjakanYangLain';
    }
}

// Kelas dapat diimplementasikan pada banyak interface
class KelasLainnya implements InterfaceSatu, InterfaceDua
{
    public function kerjakanSesuatu()
    {
        echo 'kerjakanSesuatu';
    }

    public function kerjakanYangLain()
    {
        echo 'kerjakanYangLain';
    }
}


/********************************
 * Sifat (Traits)
 */

// Traits mulai tersedia sejak PHP 5.4.0 dan dideklarasikan menggunakan kata kunci "trait"

trait TraitSaya
{
    public function methodTraitSaya()
    {
        print 'Saya menggunakan Trait';
    }
}

class KelasTraitSaya
{
    use TraitSaya;
}

$kls = new KelasTraitSaya();
$kls->methodTraitSaya();    // menampilkan "Saya menggunakan Trait"


/********************************
 * Namespaces
 */

// Bagian ini telah dibatasi, karena deklarasi "namespace"
// karena harus ditempatkan diawal dokumen.

<?php

// Secara default, kelas tersedia sebagai namespace umum, dan dapat
// secara khusus dipanggil dengan garis-miring terbalik (backslash).

$kls = new \KelasSaya();


// Menentukan namespace untuk sebuah dokumen
namespace Saya\Namespace;

class KelasSaya
{
}

// (dari dokumen lainnya)
$kls = new Saya\Namespace\KelasSaya;

// Atau dari dalam namespace lainnya.
namespace Saya\Lainnya\Namespace;

use Saya\Namespace\KelasSaya;

$kls = new KelasSaya();

// Namespace dapat menggunakan alias

namespace Saya\Lainnya\Namespace;

use Saya\Namespace as SuatuKelasLainnya;

$kls = new SuatuKelasLainnya\KelasSaya();


/**********************
* Late Static Binding
*
*/

class KelasInduk {
    public static function siapa() {
        echo "Ini adalah " . __CLASS__ . "\n";
    }
    public static function coba() {
        // kata kunci "self" merujuk pada method yang berada dalam satu kelas
        self::who();
        // kata kunci "static" merujuk pada method yang berada di kelas dimana method itu dijalankan
        static::who();
    }
}

KelasInduk::coba();
/*
Ini adalah KelasInduk
Ini adalah KelasInduk
*/

class KelasAnak extends KelasInduk {
    public static function siapa() {
        echo "Tapi ini adalah " . __CLASS__ . "\n";
    }
}

KelasAnak::tes();
/*
Ini adalah KelasInduk
Tapi ini adalah KelasAnak
*/

/**********************
*  Magic constants
*  
*/

// Mendapatkan nama dari suatu kelas. Harus dideklarasikan didalam kelas tersebut.
echo "Nama kelas ini adalah " . __CLASS__;

// Mendapatkan alamat lengkap direktori
echo "Alamat direktori ini adalah " . __DIR__;

    // Beberapa yang banyak digunakan
    require __DIR__ . '/vendor/autoload.php';

// Mendapatkan alamat lengkap dokumen
echo "Alamat dokumen ini adalah " . __FILE__;

// Mendapatkan nama fungsi
echo "Nama fungsi ini adalah " . __FUNCTION__;

// Mendapatkan nomor baris perintah
echo "Nomor baris perintah ini adalah " . __LINE__;

// Mendapatkan nama method. Hanya mengembalikan sebuah nilai jika berada didalam trait atau deklarasi objek.
echo "Nama method ini adalah " . __METHOD__;

// Mendapatkan nama namespace 
echo "Namespace saat ini adalah " . __NAMESPACE__;

// Mendapatkan nama dari trait. Hanya mengembalikan sebuah nilai jika berada didalam trait atau deklarasi objek.
echo "Namespace saat ini adalah " . __TRAIT__;

/**********************
*  Penanganan Kesalahan (Error)
*  
*/

// Penanganan error sederhana menggunakan "try...catch"

try {
    // Kerjakan sesuatu
} catch (Exception $e) {
    // Penanganan exception
}

// Menggunakan "try...catch" blok pada namespace

try {
    // Kerjakan sesuatu
} catch (\Exception $e) {
    // Penanganan exception
}

// Exception khusus

class ExceptionSaya extends Exception {}

try {

    $kondisi = true;

    if ($kondisi) {
        throw new ExceptionSaya('Terjadi sesuatu');
    }

} catch (ExceptionSaya $e) {
    // Penanganan untuk exception khusus
}

```

## Informasi lainnya

Kunjungi [Dokumentasi resmi PHP](http://www.php.net/manual/) untuk referensi dan masukan komunitas.

Jika anda tertarik untuk belajar lebih dalam, kunjungi 
[PHP The Right Way](http://www.phptherightway.com/).

Jika anda terbiasa dengan manajemen paket, kunjungi
[Composer](http://getcomposer.org/).

Untuk standar umum, kunjungi PHP Framework Interoperability Group's
[PSR standards](https://github.com/php-fig/fig-standards).
---
category: tool
tool: PyQt
language: Python
filename: learnqt-id.py
contributors:
    - ["Nathan Hughes", "https://github.com/sirsharpest"]
translators:
    - ["Rizky Luthfianto", "http://github.com/rilut"]
lang: id-id
---

**Qt** adalah framework terkenal untuk pengembangan perangkat lunak *cross-platform* yang dapat dijalankan pada berbagai platform perangkat lunak dan perangkat keras dengan sedikit atau tanpa perubahan dalam kode, dengan tetap memiliki kekuatan dan kecepatan aplikasi *native*. **Qt** ditulis dalam bahasa C++.


Tulisan ini diadaptasi dari **Intro Qt untuk C++** oleh [Aleksey Kholovchuk](https://github.com/vortexxx192). Kode-kode yang tertulis di sini akan menghasilkan fungsionalitas yang sama. Bedanya, versi ini dibangun menggunakan **PyQt**!

```Python
import sys
from PyQt4 import QtGui
	
def window():
# Buat objek aplikasi
    app = QtGui.QApplication(sys.argv)
# Buat sebuah widget, sebagai tempat di mana label kita akan ditempatkan
    w = QtGui.QWidget()
# Tambahkan label untuk widget
    b = QtGui.QLabel(w)
# Set teks untuk label
    b.setText("Halo, Dunia!")
# Set parameter penempatan dan ukuran
    w.setGeometry(100, 100, 200, 50)
    b.move(50, 20)
# Set judul pada jendela
    w.setWindowTitle("PyQt")
# Tampilkan segalanya
    w.show()
# Jalankan apa yang telah kita atur. Setelah semua selesai kita atur.
    sys.exit(app.exec_())

if __name__ == '__main__':
    window()
```

Untuk menunjukkan beberapa fitur yang lebih canggih di **PyQt**, kita akan membangun elemen tambahan.
Di sini, kita akan membuat Kotak Popup Dialog, yang berguna untuk meminta pengguna untuk mengkonfirmasi keputusan atau untuk menampilkan informasi.

```Python 
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


def window():
    app = QApplication(sys.argv)
    w = QWidget()
    # Buat tombol b dan tempelkan pada widget w
    b = QPushButton(w)
    b.setText("Tekan aku!")
    b.move(50, 50)
    # Perintahkan tombol b untuk memanggil fungsi ini ketika diklik
    # Perhatikan bahwa kita tidak menggunakan simbol "()" pada pemanggilan fungsi kali ini
    b.clicked.connect(ShowDialog)
    w.setWindowTitle("Dialog PyQt")
    w.show()
    sys.exit(app.exec_())

# Fungsi ini akan membuat jendela dialog dengan tombol
# yang menunggu untuk diklik untuk keluar dari program
def ShowDialog():
    d = QDialog()
    b1 = QPushButton("ok", d)
    b1.move(50, 50)
    d.setWindowTitle("Dialog")
    # Modalitas ini memberitahu popup untuk memblokir induk saat ini aktif
    d.setWindowModality(Qt.ApplicationModal)
    # Pada klik, kita ingin seluruh proses untuk berhenti
    b1.clicked.connect(sys.exit)
    d.exec_()

if __name__ == '__main__':
    window()
```
---
language: ruby
filename: learnruby-id.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
  - ["Gabriel Halley", "https://github.com/ghalley"]
  - ["Persa Zula", "http://persazula.com"]
  - ["Jake Faris", "https://github.com/farisj"]
translators:
  - ["Ukaza Perdana", "https://github.com/ukazap"]
lang: id-id
---

```ruby
# Ini adalah sebuah komentar

=begin
Ini adalah komentar multibaris
Tak seorang pun menggunakannya
Kamu juga tidak perlu
=end

# Pertama-tama dan yang terpenting: Semuanya adalah objek.

# Angka adalah objek

3.class #=> Fixnum

3.to_s #=> "3"


# Beberapa aritmetika dasar
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
5 % 3 #=> 2

# Operator-operator bitwise
3 & 5 #=> 1
3 | 5 #=> 7
3 ^ 5 #=> 6

# Aritmetika tidak lain adalah pemanis sintaks (syntactic sugar)
# untuk memanggil sebuah metode pada suatu objek
1.+(3) #=> 4
10.* 5 #=> 50

# Nilai-nilai khusus adalah objek
nil # setara dengan "null" di bahasa-bahasa lain
true # kebenaran
false # ketidakbenaran

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Kesamaan
1 == 1 #=> true
2 == 1 #=> false

# Ketidaksamaan
1 != 1 #=> false
2 != 1 #=> true

# selain false itu sendiri, nil adalah nilai lain yang "salah"
!nil   #=> true
!false #=> true
!0     #=> false

# Perbandingan lain
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Operator pembanding yang dikombinasikan ("spaceship operator")
1 <=> 10 #=> -1
10 <=> 1 #=> 1
1 <=> 1 #=> 0

# Operator-operator logika
true && false #=> false
true || false #=> true
!true #=> false

# Terdapat versi-versi operator logika yang berbeda dengan lebih sedikit awalan.
# Mereka digunakan sebagai kendali alur untuk merangkai beberapa pernyataan
# hingga salah satunya mengembalikan (return) nilai true atau false.

# `lakukan_suatu_lainnya` hanya dipanggil jika `lakukan_sesuatu` berhasil.
lakukan_sesuatu() and lakukan_suatu_lainnya()
# `catat_error` hanya dipanggil jika `lakukan_sesuatu` gagal.
lakukan_sesuatu() or catat_error()


# String adalah objek

'Aku adalah string'.class #=> String
"Aku juga adalah string".class #=> String

wadah = 'menggunakan string interpolation'
"Aku bisa #{wadah} ketika memakai tanda kutip ganda"
#=> "Aku bisa menggunakan string interpolation ketika memakai tanda kutip ganda"

# Gunakan tanda kutip tunggal daripada tanda kutip ganda jika memungkinkan
# String bertanda kutip ganda melakukan kalkulasi tambahan di dalam

# Kombinasikan string, tapi tidak dengan angka
'halo ' + 'dunia'  #=> "halo dunia"
'halo ' + 3 #=> TypeError: can't convert Fixnum into String
'halo ' + 3.to_s #=> "halo 3"

# Kombinasikan string dengan operator
'halo ' * 3 #=> "halo halo halo "

# Membubuhkan ke string
'halo' << ' dunia' #=> "halo dunia"

# cetak ke output dan buat baris baru (newline) di akhir
puts "Aku mencetak!"
#=> Aku mencetak!
#=> nil

# cetak ke output tanpa baris baru
print "Aku mencetak!"
#=> Aku mencetak! => nil

# Variabel
x = 25 #=> 25
x #=> 25

# Catat bahwa pemberian nilai mengembalikan nilai yang diberikan
# Artinya kamu bisa melakukan pemberian nilai secara jamak:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Berdasarkan adat, gunakan gaya snake_case untuk menulis nama variabel
snake_case = true

# Gunakan nama variabel yang deskriptif
path_to_project_root = '/good/name/'
path = '/bad/name/'

# Simbol (adalah objek)
# Simbol adalah konstanta yang dapat didaur ulang yang tidak dapat diubah
# (immutable), secara internal diwakili oleh nilai integer. Seringkali 
# digunakan sebagai pengganti string untuk menyampaikan nilai yang mengandung
# makna spesifik secara efisien.

:menunggu.class #=> Symbol

status = :menunggu

status == :menunggu #=> true

status == 'menunggu' #=> false

status == :diterima #=> false

# Array

# Ini adalah sebuah array
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Array bisa menampung item dengan beragam tipe

[1, 'halo', false] #=> [1, "halo", false]

# Array bisa di-indeks-kan
# Dari depan
array[0] #=> 1
array.first #=> 1
array[12] #=> nil

# Sama dengan aritmetika, pengaksesan [var]
# hanyalah pemanis sintaks
# untuk memanggil metode [] pada suatu objek
array.[] 0 #=> 1
array.[] 12 #=> nil

# Dari belakang
array[-1] #=> 5
array.last #=> 5

# Dengan indeks awal dan panjang (jumlah item)
array[2, 3] #=> [3, 4, 5]

# Membalik sebuah Array
a=[1,2,3]
a.reverse! #=> [3,2,1]

# Atau menggunakan jangkauan (range)
array[1..3] #=> [2, 3, 4]

# Tambahkan ke array seperti ini
array << 6 #=> [1, 2, 3, 4, 5, 6]
# Atau seperti ini
array.push(6) #=> [1, 2, 3, 4, 5, 6]

# Periksa apakah suatu item ada dalam sebuah array
array.include?(1) #=> true

# Hash adalah kamus utama Ruby berupa pasangan kunci/nilai (key/value pair).
# Hash ditandai dengan kurung kurawal:
hash = { 'warna' => 'hijau', 'angka' => 5 }

hash.keys #=> ['warna', 'angka']

# Nilai dalam Hash bisa diperoleh menggunakan kunci:
hash['warna'] #=> 'hijau'
hash['angka'] #=> 5

# Meminta hash untuk kunci yang tidak ada akan mengembalikan nil:
hash['tidak ada di sini'] #=> nil

# Sejak Ruby 1.9, ada sintaks khusus ketika menggunakan simbol sebagai kunci:

hash_baru = { defcon: 3, action: true }

hash_baru.keys #=> [:defcon, :action]

# Periksa ada/atau tidaknya kunci dan nilai dalam hash
hash_baru.key?(:defcon) #=> true
hash_baru.value?(3) #=> true

# Tip: Baik array maupun hash adalah Enumerable
# Mereka berbagi banyak metode yang berguna diantaranya each, map, count, dll.

# Struktur-struktur kendali

if true
  'pernyataan if'
elsif false
  'else if, opsional'
else
  'else, opsional juga'
end

for penghitung in 1..5
  puts "iterasi #{penghitung}"
end
#=> iterasi 1
#=> iterasi 2
#=> iterasi 3
#=> iterasi 4
#=> iterasi 5

# NAMUN, tidak ada orang yang menggunakan pengulangan for.
# Sebagai ganti, gunakan metode "each" dan memberinya sebuah blok (block).
# Blok adalah serangkaian kode yang bisa dimasukkan ke metode seperti "each".
# Ia serupa dengan lambda, fungsi anonim atau closure di bahasa lainnya.
#
# Metode "each" dari range menjalankan blok untuk setiap elemen dari range.
# Bloknya diberikan penghitung sebagai parameter.
# Memanggil metode "each" dengan blok terlihat seperti ini:

(1..5).each do |penghitung|
  puts "iterasi #{penghitung}"
end
#=> iterasi 1
#=> iterasi 2
#=> iterasi 3
#=> iterasi 4
#=> iterasi 5

# Kamu juga bisa mengurung blok dalam kurung kurawal:
(1..5).each { |penghitung| puts "iterasi #{penghitung}" }

# Isi dari struktur-struktur data juga bisa di-iterasi menggunakan each.
array.each do |elemen|
  puts "#{elemen} adalah bagian dari array"
end
hash.each do |kunci, nilai|
  puts "#{kunci} adalah #{nilai}"
end

# Jika kamu masih membutuhkan indeks, bisa menggunakan "each_with_index"
# dan definisikan variabel indeks
array.each_with_index do |elemen, indeks|
  puts "#{elemen} adalah nomor #{indeks} dalam array"
end

penghitung = 1
while penghitung <= 5 do
  puts "iterasi #{penghitung}"
  penghitung += 1
end
#=> iterasi 1
#=> iterasi 2
#=> iterasi 3
#=> iterasi 4
#=> iterasi 5

# Ada kumpulan fungsi pengulangan lainnya yang berguna di Ruby,
# contohnya "map", "reduce", "inject", daftarnya sangat panjang. Map,
# misalnya, mengambil array yang di-iterasi-nya, melakukan sesuatu pada
# setiap elemen sesuai definisi pada blok, dan mengembalikan array baru.
array = [1,2,3,4,5]
berganda = array.map do |elemen|
  elemen * 2
end
puts berganda
#=> [2,4,6,8,10]
puts array
#=> [1,2,3,4,5]

nilai = 'B'

case nilai
when 'A'
  puts 'Pertahankan, nak'
when 'B'
  puts 'Semoga lebih beruntung di lain waktu'
when 'C'
  puts 'Kamu bisa lebih baik'
when 'D'
  puts 'Susah payah'
when 'F'
  puts 'Kamu gagal!'
else
  puts 'Sistem penilaian lainnya, heh?'
end
#=> "Semoga lebih beruntung di lain waktu"

# case juga bisa menggunakan range
nilai = 82
case nilai
when 90..100
  puts 'Hore!'
when 80...90
  puts 'Cukup bagus'
else
  puts 'Kamu gagal!'
end
#=> "Cukup bagus"

# penanganan kesalahan (exception handling):
begin
  # kode di sini yang mungkin membangkitkan exception
  raise NoMemoryError, 'Kamu kehabisan memori.'
rescue NoMemoryError => variabel_exception
  puts 'NoMemoryError dibangkitkan', variabel_exception
rescue RuntimeError => variabel_exception_lainnya
  puts 'RuntimeError dibangkitkan sekarang'
else
  puts 'Ini dijalankan bila tidak ada exceptions sama sekali'
ensure
  puts 'Kode ini akan berjalan bagaimanapun juga'
end

# Fungsi (atau metode)

def gandakan(x)
  x * 2
end

# Fungsi dan semua blok secara tersirat mengembalikan nilai pernyataan terakhir
gandakan(2) #=> 4

# Tanda kurung bersifat optional, boleh ditiadakan jika tidak ambigu
gandakan 3 #=> 6

gandakan gandakan 3 #=> 12

def jumlah(x, y)
  x + y
end

# Argumen-argumen dari metode dipisahkan dengan koma
sum 3, 4 #=> 7

sum sum(3, 4), 5 #=> 12

# yield
# Semua metode secara tersirat mempunyai parameter blok opsional
# yang bisa dipanggil dengan kata kunci 'yield'

def kurung
  puts '{'
  yield
  puts '}'
end

kurung { puts 'halo dunia' }

# {
# halo dunia
# }


# Kamu bisa memasukkan blok ke sebuah fungsi
# "&" adalah penanda blok yang masuk
def tamu_tamu(&blok)
  blok.call 'beberapa_argumen'
end

# Kamu bisa memasukkan daftar argumen yang akan dikonversi menjadi array
# Itulah gunanya operator splat ("*")
def tamu_tamu(*array)
  array.each { |tamu| puts tamu }
end

# Bila metode mengembalikan array, bisa memberi nilai dengan destrukturisasi
# (destructuring assignment):
def makanan
    ['tempe penyet', 'sayur asam', 'nasi goreng']
end
sarapan, makan_siang, makan_malam = makanan
sarapan #=> 'tempe penyet'
makan_malam #=> 'nasi goreng'

# Menurut adat, nama metode yang mengembalikan boolean diakhiri tanda tanya
5.even? # false
5.odd? # true

# Dan jika suatu metode berakhiran tanda seru, ia melakukan sesuatu yang merusak
# seperti mengubah penerimanya. Banyak metode mempunyai versi ! untuk melakukan
# perubahan dan versi non-! untuk sekedar mengembalikan perubahannya
nama_perusahaan = "Putra Sejahtera"
nama_perusahaan.upcase #=> "PUTRA SEJAHTERA"
nama_perusahaan #=> "Putra Sejahtera"
nama_perusahaan.upcase! # kali ini kita benar-benar mengubah nama_perusahaan!
nama_perusahaan #=> "PUTRA SEJAHTERA"


# Definisikan kelas menggunakan kata kunci class
class Manusia

  # Variabel kelas. Ini dibagi oleh semua instans (instance) dari kelas ini.
  @@spesies = 'H. sapiens'

  # Inisialisasi dasar
  def initialize(nama, usia = 0)
    # Berikan argumen ke variabel instans "nama" dalam instans ini
    @nama = nama
    # Jika tidak diberi usia, nilai default dalam daftar argumen digunakan.
    @usia = usia
  end

  # Metode setter dasar
  def nama=(nama)
    @nama = nama
  end

  # Metode getter dasar
  def nama
    @nama
  end

  # Fungsi di atas bisa disingkat dengan metode attr_accessor sebagai berikut
  attr_accessor :nama

  # Metode getter/setter juga bisa dibuat secara terpisah seperti ini
  attr_reader :nama
  attr_writer :nama

  # Metode kelas menggunakan self untuk membedakannya dari metode instans.
  # Ia hanya bisa dipanggil pada kelas, bukan pada instans-nya.
  def self.katakan(pesan)
    puts pesan
  end

  def spesies
    @@spesies
  end
end


# Membuat instans kelas
jim = Manusia.new('Jim Halpert')

dwight = Manusia.new('Dwight K. Schrute')

# Mari panggil beberapa metode
jim.spesies #=> "H. sapiens"
jim.nama #=> "Jim Halpert"
jim.nama = "Jim Halpert II" #=> "Jim Halpert II"
jim.nama #=> "Jim Halpert II"
dwight.spesies #=> "H. sapiens"
dwight.nama #=> "Dwight K. Schrute"

# Panggil metode kelas
Manusia.katakan('Hai') #=> "Hai"

# Lingkup variabel didefinisikan berdasarkan bagaimana kita memberikannya nama
# Variabel yang berawalan $ memiliki lingkup global
$var = "Aku adalah variabel global"
defined? $var #=> "global-variable"

# Variabel yang berawalan @ memiliki lingkup instans
@var = "Aku adalah variabel instans"
defined? @var #=> "instance-variable"

# Variabel yang berawalan @@ memiliki lingkup kelas
@@var = "Aku adalah variabel kelas"
defined? @@var #=> "class variable"

# Variabel yang berawalan huruf kapital adalah konstanta
Var = "Aku adalah konstanta"
defined? Var #=> "constant"

# Kelas juga adalah objek sehingga kelas bisa memiliki variabel instans.
# Variabel kelas dibagi diantara kelas dan semua pewarisnya.

# kelas dasar
class Manusia
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(nilai)
    @@foo = nilai
  end
end

# kelas turunan
class Buruh < Manusia
end

Manusia.foo # 0
Buruh.foo # 0

Manusia.foo = 2 # 2
Buruh.foo # 2

# Variabel instans milik kelas tidak dibagikan dengan pewaris kelas tersebut.

class Manusia
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(nilai)
    @bar = nilai
  end
end

class Dokter < Manusia
end

Manusia.bar # 0
Dokter.bar # nil

module ContohModul
  def foo
    'foo'
  end
end

# Include modul mengikat metode-metodenya pada instans-instans kelas
# Extend modul mengikat metode-metodenya pada kelas

class Orang
  include ContohModul
end

class Buku
  extend ContohModul
end

Orang.foo     # => NoMethodError: undefined method `foo' for Orang:Class
Orang.new.foo # => 'foo'
Buku.foo       # => 'foo'
Buku.new.foo   # => NoMethodError: undefined method `foo'

# Callbacks dijalankan ketika meng-include dan meng-extend sebuah modul

module ContohUrusan
  def self.included(base)
    base.extend(MetodeKelas)
    base.send(:include, MetodeInstans)
  end

  module MetodeKelas
    def bar
      'bar'
    end
  end

  module MetodeInstans
    def qux
      'qux'
    end
  end
end

class Sesuatu
  include ContohUrusan
end

Sesuatu.bar     # => 'bar'
Sesuatu.qux     # => NoMethodError: undefined method `qux'
Sesuatu.new.bar # => NoMethodError: undefined method `bar'
Sesuatu.new.qux # => 'qux'
```

## Sumber tambahan

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - Varian dari referensi ini dengan tantangan dalam browser.
- [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Belajar Ruby melalui serangkaian tutorial interaktif.
- [Dokumentasi resmi](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - Edisi lama yang [gratis](http://ruby-doc.com/docs/ProgrammingRuby/) tersedia online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - Panduan penulisan kode Ruby oleh komunitas.
- [Try Ruby](http://tryruby.org) - Pelajari dasar bahasa pemrograman Ruby, secara interaktif di browser.
---
language: SmallBASIC
filename: learnsmallbasic-id.bas
contributors:
    - ["Chris Warren-Smith", "http://smallbasic.sourceforge.net"]
translators:
    - ["Rizky Luthfianto", "http://github.com/rilut"]
lang: id-id
---

## Tentang

SmallBASIC adalah *interpreter* bahasa BASIC yang mudah dan cepat dipelajari yang ideal untuk perhitungan sehari-hari, skrip dan prototipe. Fitur SmallBASIC termasuk trigonometri, matriks dan fungsi aljabar, yang dibangun di IDE, *library* string yang canggih, sistem, suara, dan perintah grafis bersama dengan sintaks pemrograman terstruktur.

## Pengembangan

SmallBASIC pada awalnya dikembangkan oleh Nicholas Christopoulos pada akhir tahun 1999 untuk Palm Pilot. pengembangan proyek telah dilanjutkan oleh Chris Warren-Smith sejak sekitar tahun 2005.

Versi SmallBASIC telah dibuat untuk sejumlah perangkat genggam termasuk Franklin eBookman dan Nokia 770. Juga berbagai versi desktop yang telah dirilis berdasarkan berbagai GUI. Platform yang didukung saat ini adalah Linux dan Windows berbasis SDL2 dan Android berbasis NDK. Sebuah versi baris perintah pada desktop juga tersedia, meskipun tidak biasanya dirilis dalam bentuk biner.

Sekitar tahun 2008, sebuah perusahaan merilis lingkungan pemrograman BASIC dengan nama yang mirip. SmallBASIC tidak berhubungan dengan itu.

```
REM ini adalah komentar
'dan ini juga komentar

REM mencetak kalimat
print "halo"
? "Tanda ? adalah singkatan dari PRINT"

REM Struktur kontrol
FOR index = 0 TO 10 STEP 2
  ? "Ini adalah nomor baris"; indeks
NEXT
J=0
REPEAT
 J++
UNTIL J=10
WHILE J>0
 J--
WEND

REM Pernyataan "Select case"
Select Case "Cool"
 Case "null", 1,2,3,4,5,6,7,8,"Cool","blah"
 Case "Not cool"
   PRINT "Epic fail"
 Case Else
   PRINT "Fail"
End Select

REM menangkap kesalahan dengan TRY / CATCH
Try
  fn = Freefile
  Open filename For Input As #fn
Catch err
  Print "gagal membuka file"
End Try

REM Fungsi dan subrutin buatan pengguna
func add2(x, y)
  'Variabel dapat dinyatakan sebagai lokal dalam lingkup/scope dari SUB atau FUNC
  local k
  k = "k akan lenyap ketika FUNC ini mengembalikan nilai"
  add2 = x + y
akhir
Print add2(5,5)
sub cetak_ini(ini)
  print ini
end
cetak_ini "INI"

REM Menampilkan garis dan piksel
At 0,ymax/2+txth("Q")
Color 1: ? "sin(x)":
Color 8: ? "cos(x)":
Color 12: ? "tan(x)"
Line 0,ymax/2,xmax,ymax/2
For i=0 to xmax
  Pset i,ymax/2-sin(i*2*pi/ymax)*ymax/4 color 1
  Pset i,ymax/2-cos(i*2*pi/ymax)*ymax/4 color 8
  Pset i,ymax/2-tan(i*2*pi/ymax)*ymax/4 color 12
Next
showpage

REM SmallBASIC cocok untuk bereksperimen dengan fraktal dan efek menarik lainnya
Delay 3000
Randomize
ff = 440.03
For j = 0 to 20
  r = rnd * 1000 % 255
  b = rnd * 1000 % 255
  g = rnd * 1000 % 255
  c = rgb(r,b,g)
  ff += 9.444
  for i=0 to 25000
    f += ff
    x = min(xmax, -x + cos(f*i))
    y = min(ymax, -y + sin(f*i))
    pset x, y color c
    if (i%1000==0) then
      showpage
    fi
  next
Next j

REM Untuk sejarawan komputer, SmallBASIC dapat menjalankan program
REM dari buku dan majalah komputer lama, misalnya:
10 LET A=9
20 LET B=7
30 PRINT A*B
40 PRINT A/B

REM SmallBASIC juga memiliki dukungan untuk beberapa konsep modern seperti JSON
aa = array("{\"kucing\":{\"nama\":\"harry\"},\"peliharaan\":\"true\"}")
If (ismap(aa) == false) Then
  throw "bukan tipe data map"
End If
Print aa

PAUSE

```

## Artikel

* [Persiapan](http://smallbasic.sourceforge.net/?q=node/1573)
* [Selamat Datang di SmallBASIC](http://smallbasic.sourceforge.net/?q=node/838)

## GitHub

* [Source code](https://github.com/smallbasic/SmallBASIC)
* [Referensi snapshot](http://smallbasic.github.io/)
---
language: xml
filename: learnxml-id.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Rizky Luthfianto", "https://github.com/rilut"]
  - ["Ahmad Zafrullah", "https://github.com/23Pstars"]
lang: id-id
---

XML adalah bahasa markup yang dirancang untuk menyimpan dan mengirim data. XML mudah dibaca oleh manusia dan mesin.

Tidak seperti HTML, XML tidak menentukan bagaimana menampilkan atau format data, hanya membawanya.

Terdapat perbedaan antara **konten** dan **markup**. Singkatnya, konten dapat berupa apapun dan markup adalah sebagai penentu.
 
## Definisi dan Pendahuluan
 
Dokumen XML pada dasarnya disusun oleh *elemen* yang dapat memiliki *atribut* untuk menjelaskan elemen tersebut dan dapat memiliki beberapa konten tekstual atau beberapa elemen sebagai anak-nya. Setiap dokumen XML hendaknya memiliki satu elemen akar, yang menjadi induk dari semua elemen dalam dokumen XML.
 
Pengurai XML dirancang menjadi sangat ketat, dan akan berhenti melakukan penguraian terhadap dokumen yang cacat. Oleh karena itu semua dokumen XML harus mengikuti [Aturan Sintaks XML](http://www.w3schools.com/xml/xml_syntax.asp).

```xml
<!-- Ini adalah komentar. Komentar harus memiliki dua tanda penghubung secara berurutan (-). -->
<!-- Komentar dapat renggang
  menjadi banyak baris -->

<!-- Elemen -->
<!-- Elemen merupakan komponen dasar dari XML. Ada dua tipe dari elemen, kosong: -->
<elemen1 atribut="nilai" /> <!-- Elemen kosong tidak memiliki konten apapun -->
<!-- dan tidak-kosong: -->
<elemen2 atribut="nilai">Konten</elemen2>
<!-- Nama elemen hanya dapat berupa huruf dan angka saja. -->

<kosong /> <!-- Elemen yang terdiri dari tag elemen kosong… -->
<!-- …tidak memiliki content apapun dan murni markup. -->

<tidakkosong> <!-- Atau, elemen ini memiliki tag pembuka… -->
  <!-- …suatu konten… -->
</tidakkosong> <!-- dan sebuah tag penutup. -->

<!-- Nama elemen merupakan *case sensitive*. -->
<elemen />
<!-- …tidak sama dengan elemen sebelumnya -->
<eLEMEN />

<!-- Atribut -->
<!-- Sebuah atribut merupakan hubungan kunci-nilai yang terdapat pada elemen. -->
<elemen atribut="nilai" lainnya="nilaiLainnya" banyakNilai="daftar nilai ber-spasi" />
<!-- Sebuah atribut digunakan hanya sekali dalam sebuah elemen. Dan hanya memiliki satu nilai.
  Salah satu solusi untuk mengatasi permasalahan tersebut adalah dengan menggunakan daftar nilai ber-spasi. -->

<!-- Elemen bersarang -->
<!-- Konten dari sebuah elemen dapat berupa elemen lainnya:: -->
<ayah>
  <anak>Teks</anak>
  <oranglain />
</ayah>
<!-- Mengikuti standar tatanan pohon. Setiap elemen disebut *node*.
  Induk yang berada satu tingkat diatasnya disebut *parent*, keturunan yang berada satu tingkat dibawahnya disebut *children*.
  Elemen yang berada pada *parent* yang sama disebut Saudara (*siblings*). -->

<!-- XML mempertahankan spasi. -->
<anak>
  Teks
</anak>
<!-- …tidak sama dengan -->
<anak>Teks</anak>
```
 

## Dokumen XML

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- XML prolog, boleh tidak digunakan namun direkomendasikan untuk digunakan. -->
<tokobuku>
  <buku category="MEMASAK">
    <judul lang="en">Everyday Italian</judul>
    <pengarang>Giada De Laurentiis</pengarang>
    <tahun>2005</tahun>
    <harga>30.00</harga>
  </buku>
  <buku category="ANAK">
    <judul lang="en">Harry Potter</judul>
    <pengarang>J K. Rowling</pengarang>
    <tahun>2005</tahun>
    <harga>29.99</harga>
  </buku>
  <buku category="WEB">
    <judul lang="en">Learning XML</judul>
    <pengarang>Erik T. Ray</pengarang>
    <tahun>2003</tahun>
    <harga>39.95</harga>
  </buku>
</tokobuku>

<!-- Di atas adalah contoh file XML biasa.
   Dimulai dengan deklarasi, menginformasikan beberapa metadata (opsional).
  
   XML menggunakan struktur pohon. Di atas, simpul akar adalah 'tokobuku',
   yang memiliki tiga node anak, para 'buku'. Node-node tersebut dapat memiliki
   node-node anak, dan seterusnya ...
  
   Node dibuat menggunakan tag buka/tutup, dan node-node anak hanya
   berada di antara tag buka dan tutup .-->


<!-- XML membawa dua jenis data:
   1 - Atribut -> Itu metadata tentang sebuah node.
       Biasanya, parser XML menggunakan informasi ini untuk menyimpan data dengan
       benar. Hal ini ditandai dengan muncul dengan format nama = "nilai" dalam pembukaan tag.
   2 - Elemen -> Itu data yang murni.
       Itulah yang parser akan mengambil dari file XML.
       Elemen muncul antara tag membuka dan menutup.-->
      
  
<!-- Di bawah ini, unsur dengan dua atribut-->
<file type="gif" id="4293">komputer.gif</file>


```

## Dokumen yang well-formated & Validasi

Sebuah dokumen XML disebut well-formated jika sintaksisnya benar.
Namun, juga mungkin untuk mendefinisikan lebih banyak batasan dalam dokumen,
menggunakan definisi dokumen, seperti DTD dan XML Schema.

Sebuah dokumen XML yang mengikuti definisi dokumen disebut valid,
jika sesuai dokumen itu.

Dengan alat ini, Anda dapat memeriksa data XML di luar logika aplikasi.

```xml

<!-- Di bawah, Anda dapat melihat versi sederhana dari dokumen tokobuku,
  dengan penambahan definisi DTD .-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catatan SYSTEM "tokobuku.dtd">
<tokobuku>
  <buku category="MEMASAK">
    <judul >Everyday Italian</judul>
    <harga>30.00</harga>
  </buku>
</tokobuku>

<!-- This DTD could be something like:-->

<!DOCTYPE catatan
[
<!ELEMENT tokobuku (buku+)>
<!ELEMENT buku (judul,harga)>
<!ATTLIST buku category CDATA "Sastra">
<!ELEMENT judul (#PCDATA)>
<!ELEMENT harga (#PCDATA)>
]>


<!-- DTD dimulai dengan deklarasi.
  Berikut, node akar dinyatakan, membutuhkan 1 atau lebih anak node 'buku'.
  Setiap 'buku' harus berisi tepat satu 'judul' dan 'harga' dan atribut
  disebut 'kategori', dengan "Sastra" sebagai nilai default.
  Node yang 'judul' dan 'harga' mengandung karakter data diurai .-->

<!-- DTD dapat dideklarasikan di dalam file XML itu sendiri .-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE catatan
[
<!ELEMENT tokobuku (buku+)>
<!ELEMENT buku (judul,harga)>
<!ATTLIST buku category CDATA "Sastra">
<!ELEMENT judul (#PCDATA)>
<!ELEMENT harga (#PCDATA)>
]>

<tokobuku>
  <buku category="MEMASAK">
    <judul >Everyday Italian</judul>
    <harga>30.00</harga>
  </buku>
</tokobuku>
```
## Kompatibilitas DTD dan Definisi Skema XML

Dukungan untuk DTD dapat ditemukan dimana-mana karena sudah sangat lama. Namun sayangnya, fitur XML terkini seperti *namespaces* tidak didukung oleh DTD. XML Xchema Definitions (XSDs) bertujuan untuk mengganti DTD dalam mendefinisikan tatabahasa dokumen XML.

## Sumber

* [Validasi dokumen XML](http://www.xmlvalidation.com)

## Bacaan lainnya

* [XML Schema Definitions Tutorial](http://www.w3schools.com/schema/)
* [DTD Tutorial](http://www.w3schools.com/xml/xml_dtd_intro.asp)
* [XML Tutorial](http://www.w3schools.com/xml/default.asp)
* [Using XPath queries to parse XML](http://www.w3schools.com/xml/xml_xpath.asp)
---
language: Inform7
contributors:
    - ["Hyphz", "http://github.com/hyphz/"]
filename: LearnInform.Inform
---
Inform 7 is a natural language based language created by Graham Nelson and Emily Short for writing text adventures, but also potentially usable for other text based applications, especially data backed ones.

```
"LearnInform" by Hyphz

[This is a comment.]

[Inform 7 is a language designed for building text adventures.
It can be used for other purposes too, although the default 
library builds a text adventure. Inform 7 is object oriented.]

[This creates a class by subclassing. "Value" is the universal subclass,
but "object" is the most basic that behaves like an OO object.]
A datablock is a kind of object. 

[Classes can have properties.]
A datablock can be broken. [This creates a boolean property.]
A datablock is usually not broken. [This sets its default value.]
A datablock can be big or small. [This creates an enumerated property.]
A datablock is usually small. [This sets its default value.]
A datablock has a number called the sequence number. [This creates a typed property.]
A datablock has some text called the name. ["Some text" means a string.]
A datablock has a datablock called the chain. [Declared classes become types.]

[This creates a global named instance.]
Block1 is a datablock.
The sequence number of Block1 is 1.
The name of Block1 is "Block One."

[Functions and procedures are defined as "phrases".]
To do the thing everyone does with their first program:
	say "Hello World.". [Full stop indicates the end, indent indicates the scope.]
	
To dump (the block - a datablock): [That's how we create a parameter.]
	say the sequence number of the block;
	say the name of the block;
	if the block is broken, say "(Broken)".
		
To toggle (the block - a datablock):
	if the block is broken: [Conditional.]
		now the block is not broken; [Updating a property.]
	else:
		now the block is broken.
		
[Multiple parameters.]
To fix (the broken block - a datablock) using (the repair block - a datablock):
	if the broken block is not broken, stop; [Comma for a non indented single command.]
	if the repair block is broken, stop;
	now the sequence number of the broken block is the sequence number of the repair block;
	now the broken block is not broken.

[Because of its text adventure origins, Inform 7 doesn't generally allow objects
to be created dynamically, although there's a language extension that enables it.]	
Block2 is a datablock. 
Block2 is broken.
The sequence number of Block2 is 2.
The name of Block2 is "Block two."

To demonstrate calling a phrase with two parameters:
	Let the second block be block2; [Local pointer variable.]
	fix the second block using Block1;
	say the sequence number of the second block. [1.]
	
[Lists.]	
To show how to use list types:
	let the list be a list of datablocks;
	add Block1 to the list;
	add Block2 to the list;
	say the list; ["Block1 and Block2"]
	[Membership.]
	if Block1 is listed in the list:
		say "Block1 is there.";
	[Loop.]
	repeat with the block running through the list:
		dump the block;  [1 Block One. 1 Block Two.]
		[Remember block two's sequence number was changed above.]
	let X be entry 2 of the list; [Counting starts at 1.]
	dump X; ["1 Block two."]
	remove X from the list;
	say the list. [Block1]
		
[Here's how we define a function and do arithmetic.]

To decide which number is the sum of all numbers up to (X - a number) (this is summing up):
	let the total so far be a number;
	repeat with the current number running from 1 to X:
		now the total so far is the total so far + the current number;
	decide on the total so far. [This is the return statement.]
	
[ We have higher order functions too. ]

To demonstrate a higher order function:
	say summing up applied to {1, 2, 3, 4}.

To decide which number is the result of applying (phrase - phrase A -> A) twice to (B - a value of kind A):
	let b1 be phrase applied to B;
	let b2 be phrase applied to b1;
	decide on b2.
	
To demonstrate defining a higher order function:
	let X be 5;
	say the result of applying summing up twice to X.

[ Rulebooks allow a number of functions which apply to the same type under different conditions to be stacked. ]

Datablock validation rules is a datablock based rulebook.

A datablock validation rule for a broken datablock: rule fails.
A datablock validation rule for a datablock (called the block): 
	dump the block;
	rule succeeds.
		
To demonstrate invoking a rulebook:
	follow datablock validation rules for Block1;
	follow datablock validation rules for Block2.
	
[ Objects can also have relations, which resemble those in a relational database. ]
A dog is a kind of thing.
Rover is a dog.
The kennel is a container. [This is a built in base class.]
Rover is in the kennel. [This creates an inbuilt relation called "containment".]

[We can create relations by declaring their type.]

Guide dog ownership relates one dog to one person. [One-to-one.]
Property ownership relates various things to one person. [Many-to-one.]
Friendship relates various people to various people.  [Many-to-many.]

[To actually use them we must assign verbs or prepositions to them.]

The verb to own means the property ownership relation.
The verb to be the guide dog of means the guide dog ownership relation.
The verb to be guided by means the reversed guide dog ownership relation. 
The verb to be friends with means the friendship relation.

Edward is a person. A person can be blind. Edward is blind.
Edward is guided by Rover.
Benny is a person. Edward is friends with Benny.

To demonstrate looking something up with a relation:
	repeat with the dog running through things that are the guide dog of Edward:
		say the dog;
	repeat with the friend running through things that are friends with Edward:
		say the friend.

[We can also define relations that exist procedurally.]

Helpfulness relates a person (called the helper) to a person (called the helpee) when the helpee is blind and the helper is not blind.
The verb to be helpful to means the helpfulness relation.
To demonstrate using a procedural relation:
	repeat with the helper running through people that are helpful to Edward:
		say the helper.
	

[ Interface to the text adventure harness to allow the above code to be run. ]
Tutorial room is a room. 
"A rather strange room full of buttons. Push them to run the exercises, or turn on the robot to run them all."
A button is a kind of thing. A button is fixed in place. 

The red button is a button in tutorial room. 
Instead of pushing the red button, do the thing everyone does with their first program.
The green button is a button in tutorial room. 
Instead of pushing the green button, demonstrate calling a phrase with two parameters.
The blue button is a button in tutorial room. 
Instead of pushing the blue button, show how to use list types.
The cyan button is a button in tutorial room.
Instead of pushing the cyan button, say the sum of all numbers up to 5.
The purple button is a button in tutorial room.
Instead of pushing the purple button, demonstrate a higher order function.
The black button is a button in tutorial room.
Instead of pushing the black button, demonstrate defining a higher order function.
The white button is a button in tutorial room.
Instead of pushing the white button, demonstrate invoking a rulebook.
The puce button is a button in tutorial room.
Instead of pushing the puce button, demonstrate looking something up with a relation.
The orange button is a button in tutorial room.
Instead of pushing the orange button, demonstrate using a procedural relation.

The robot is an object in tutorial room.
Instead of switching on the robot:
	say "The robot begins to frantically flail its arms about.";
	repeat with button running through buttons in the tutorial room:
		say "The robot randomly hits [the button].";
		try pushing button.
```

##Ready For More?

* [Inform 7](http://www.inform7.com/)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
    - ["Jonathan Wang", "https://github.com/Jonathansw"]   
    - ["Leo Rudberg", "https://github.com/LOZORD"]
    - ["Betsy Lorton", "https://github.com/schbetsy"]
    - ["John Detter", "https://github.com/jdetter"]
filename: LearnBash-it.sh
translators:
    - ["Robert Margelli", "http://github.com/sinkswim/"]
    - ["Tommaso Pifferi", "http://github.com/neslinesli93/"]
lang: it-it
---

Bash è il nome della shell di unix, la quale è stata distribuita anche come shell del sistema oprativo GNU e la shell di default su Linux e Mac OS X.
Quasi tutti gli esempi sottostanti possono fare parte di uno shell script o eseguiti direttamente nella shell.

[Per saperne di più.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# La prima riga dello script è lo shebang il quale dice al sistema come eseguire
# lo script: http://it.wikipedia.org/wiki/Shabang
# Come avrai già immaginato, i commenti iniziano con #. Lo shebang stesso è un commento.

# Semplice esempio ciao mondo:
echo Ciao mondo!

# Ogni comando inizia su una nuova riga, o dopo un punto e virgola:
echo 'Questa è la prima riga'; echo 'Questa è la seconda riga'

# Per dichiarare una variabile:
Variabile="Una stringa"

# Ma non così:
Variabile = "Una stringa"
# Bash stabilirà che Variabile è un comando da eseguire e darà un errore
# perchè non esiste.

# Usare la variabile:
echo $Variabile
echo "$Variabile"
echo '$Variabile'
# Quando usi la variabile stessa - assegnala, esportala, oppure — scrivi
# il suo nome senza $. Se vuoi usare il valore della variabile, devi usare $.
# Nota che ' (singolo apice) non espande le variabili!

# Espansione dei parametri ${ }:
echo ${Variabile}
# Questo è un esempio semplice dell'espansione dei parametri.
# L'espansione dei parametri prende il valore di una variabile, ed appunto lo "espande" o lo stampa.
# Durante l'espansione il valore o il parametro passato possono essere modificati.
# Sotto ci sono altri esempi che analizzano l'uso dell'espansione dei parametri.

# Sostituzione di stringhe nelle variabili
echo ${Variabile/Una/A}
# Questo sostituirà la prima occorrenza di "Una" con "La"

# Sottostringa di una variabile
Lunghezza=7
echo ${Variabile:0:Lunghezza}
# Questo ritornerà solamente i primi 7 caratteri

# Valore di default per la variabile
echo ${Foo:-"ValoreDiDefaultSeFooMancaOppureÈVuoto"}
# Questo funziona per null (Foo=), stringa vuota (Foo=""), zero (Foo=0) ritorna 0
# Nota: viene ritornato il valore di default, il contenuto della variabile pero' non cambia.

# Espansione delle graffe { }
# Viene usata per generare stringe in modo arbitrario
echo {1..10}
echo {a..z}
# Con questi comandi viene stampato l'intervallo dal valore iniziale al valore finale (i numeri da 1 a 10, le lettere dell'alfabeto)

# Variabili builtin:
# Ci sono delle variabili builtin molto utili, come
echo "Valore di ritorno dell'ultimo programma eseguito: $?"
echo "PID dello script: $$"
echo "Numero di argomenti: $#"
echo "Argomenti dello script: $@"
echo "Argomenti dello script separati in variabili distinte: $1 $2..."

# Adesso che sappiamo come stampare a schermo, e come usare le variabili, possiamo andare avanti con le basi di bash!
# Per conoscere la directory su cui siamo posizionati, è sufficiente usare `pwd`.
# `pwd` è l'acronimo di "print working directory", ovvero "stampa la directory corrente".
# Possiamo anche usare la variabile builtin `$PWD`.
# Prova questi due esempi, e vedi che il risultato è lo stesso:
echo "Sono dentro $(pwd)" # esegue `pwd` ed interpola l'output
echo "Sono dentro $PWD" # interpola direttamente la variabile builtin

# Se c'è troppo testo nel terminale, ottenuto scrivendo comandi oppure eseguendo uno script, il comando `clear` pulisce lo schermo
clear
# Puoi utilizzare anche Ctrl-L al posto di clear

# Leggere un valore di input:
echo "Come ti chiami?"
read Nome # Nota che non abbiamo dovuto dichiarare una nuova variabile
echo Ciao, $Nome!

# Classica struttura if:
# usa 'man test' per maggiori informazioni sulle condizionali
if [ $Nome -ne $USER ]
then
    echo "Il tuo nome non è lo username"
else
    echo "Il tuo nome è lo username"
fi

# Nota: se $Name è vuoto, la condizione precedente viene interpretata come:
if [ -ne $USER ]
# che genera un errore di sintassi. Quindi il metodo sicuro per usare
# variabili che possono contenere stringhe vuote è il seguente:
if [ "$Name" -ne $USER ] ...
# che viene interpretato come:
if [ "" -ne $USER ] ...
# e dunque funziona correttamente.

# C'è anche l'esecuzione condizionale
echo "Sempre eseguito" || echo "Eseguito solo se la prima condizione fallisce"
echo "Sempre eseguito" && echo "Eseguito solo se la prima condizione NON fallisce"

# Per usare && e || con l'if, c'è bisogno di piu' paia di parentesi quadre:
if [ "$Nome" == "Steve" ] && [ "$Eta" -eq 15 ]
then
    echo "Questo verrà eseguito se $Nome è Steve E $Eta è 15."
fi

if [ "$Nome" == "Daniya" ] || [ "$Nome" == "Zach" ]
then
    echo "Questo verrà eseguito se $Nome è Daniya O Zach."
fi

# Le espressioni sono nel seguente formato:
echo $(( 10 + 5 ))

# A differenza di altri linguaggi di programmazione, bash è una shell - quindi lavora nel contesto
# della cartella corrente. Puoi elencare i file e le cartelle nella cartella
# corrente con il comando ls:
ls

# Questi comandi hanno opzioni che controllano la loro esecuzione:
ls -l # Elenca tutti i file e le cartelle su una riga separata
ls -t # Ordina i contenuti della cartella in base all'ultima data di modifica (ordine decrescente)
ls -R # Esegue `ls` in modo ricorsivo all'interno di questa cartella e tutte le sottocartelle

# I risultati del comando precedente possono essere passati al comando successivo come input.
# Il comando grep filtra l'input con il pattern passato. Ecco come possiamo elencare i
# file .txt nella cartella corrente:
ls -l | grep "\.txt"

# Usa `cat` per stampare il contenuto dei file a schermo:
cat file.txt

# Possiamo leggere il contenuto di un file e memorizzarlo in una variabile, sempre usando `cat`:
Contenuti=$(cat file.txt)
echo "INIZIO DEL FILE\n$Contenuti\nFINE DEL FILE"

# Usa `cp` per copiare file o cartelle da un punto all'altro del sistema.
# `cp` crea NUOVE versioni dei file, quindi le modifiche della copia non hanno effetto sull'originale, e viceversa.
# Nota che il file (o la cartella) di destinazione vengono sovrascritte se già esistono!
cp fileSorgente.txt copia.txt
cp -r cartellaSorgente/ destinazione/ # copia ricorsiva

# Se hai bisogno di trasferire file tra computer, puoi usare `scp` o `sftp`.
# `scp` ha una sintassi simile a `cp`.
# `sftp` invece è più interattivo.

# Usa `mv` per spostare file o cartella da un punto all'altro del sistema.
# `mv` è simile a `cp`, ma cancella il file(o la cartella) sorgente.
# `mv` è molto utile anche per rinominare i file!
mv s0rg3nt3.txt dst.txt # mi spiace anonymous...

# Dal momento che bash lavora nel contesto della cartella corrente, potresti voler eseguire il comando dentro a qualche altra cartella. Per fare questo si usa `cd`:
cd ~ # va nella cartella Home
cd .. # va nella cartella "padre"
      # (ad esempio da /home/user/Download a /home/user)
cd /home/user/Documenti # entra nella cartella specificata
cd ~/Documenti/.. # siamo sempre nella cartella home... vero?

# Usa le subshell per lavorare in cartelle diverse contemporaneamente
(echo "All'inizio sono qua: $PWD") && (cd cartella; echo "Adesso invece sono qua: $PWD")
pwd # siamo sempre nella prima cartella

# Usa `mkdir` per creare nuove cartelle
mkdir nuovaCartella
# Il flag `-p` indica la creazione delle cartelle intermedie, se non esistono.
mkdir nuovaCartella/con/tante/cartelle/intermedie

# Puoi redirezionare l'input e l'output del comando (stdin, stdout, e stderr).
# Leggi da stdin finchè ^EOF$ e sovrascrivi hello.py con le righe
# comprese tra "EOF":
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Esegui hello.py con diverse redirezioni stdin, stdout, e stderr:
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# Lo output error sovrascriverà il file se esiste,
# se invece vuoi appendere usa ">>":
python hello.py >> "output.out" 2>> "error.err"

# Sovrascrivi output.out, appendi a error.err, e conta le righe:
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# Esegui un comando e stampa il suo file descriptor (esempio: /dev/fd/123)
# vedi: man fd
echo <(echo "#ciaomondo")

# Sovrascrivi output.out con "#helloworld":
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# Pulisci i file temporanei verbosamente (aggiungi '-i' per la modalità interattiva)
# Attenzione: il comando `rm` non può essere annullato!
rm -v output.out error.err output-and-error.log
rm -r cartellaTemporanea/ # cancella ricorsivamente

# I comandi possono essere sostituiti con altri comandi usando $( ):
# Il comando seguente mostra il numero di file e cartelle nella
# cartella corrente.
echo "Ci sono $(ls | wc -l) oggetti qui."

# Lo stesso puo' essere usato usando backticks `` ma non possono essere innestati - il modo migliore
# è usando $( ).
echo "Ci sono `ls | wc -l` oggetti qui."

# Bash utilizza uno statemente case che funziona in maniera simile allo switch in Java e C++:
case "$Variabile" in 
    #Lista di pattern per le condizioni che vuoi soddisfare
    0) echo "C'è uno zero.";;
    1) echo "C'è un uno.";;
    *) echo "Non è null.";;
esac

# I cicli for iterano per ogni argomento fornito:
# I contenuti di $Variabile sono stampati tre volte.
for Variabile in {1..3}
do
    echo "$Variabile"
done

# O scrivilo con il "ciclo for tradizionale":
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Possono essere usati anche per agire su file..
# Questo eseguirà il comando 'cat' su file1 e file2
for Variabile in file1 file2
do
    cat "$Variabile"
done

# ..o dall'output di un comando
# Questo eseguirà cat sull'output di ls.
for Output in $(ls)
do
    cat "$Output"
done

# while loop:
while [ true ]
do
    echo "corpo del loop..."
    break
done

# Puoi anche definire funzioni
# Definizione:
function foo ()
{
    echo "Gli argomenti funzionano come gli argomenti dello script: $@"
    echo "E: $1 $2..."
    echo "Questa è una funzione"
    return 0
}

# o semplicemente
bar ()
{
    echo "Un altro modo per dichiarare funzioni!"
    return 0
}

# Per chiamare la funzione
foo "Il mio nome è" $Nome

# Ci sono un sacco di comandi utili che dovresti imparare:
# stampa le ultime 10 righe di file.txt
tail -n 10 file.txt
# stampa le prime 10 righe di file.txt
head -n 10 file.txt
# ordina le righe di file.txt
sort file.txt
# riporta o ometti le righe ripetute, con -d le riporta
uniq -d file.txt
# stampa solamente la prima colonna prima del carattere ','
cut -d ',' -f 1 file.txt
# sostituisce ogni occorrenza di 'okay' con 'great' in file.txt (compatible con le regex)
sed -i 's/okay/great/g' file.txt
# stampa su stdout tutte le righe di file.txt che soddisfano una certa regex
# L'esempio stampa le righe che iniziano con "foo" e che finiscono con "bar"
grep "^foo.*bar$" file.txt
# passa l'opzione "-c" per stampare invece il numero delle righe che soddisfano la regex
grep -c "^foo.*bar$" file.txt
# Altre opzioni utili possono essere:
grep -r "^foo.*bar$" someDir/ # esegue `grep` ricorsivamente nella cartella
grep -n "^foo.*bar$" file.txt # stampa il numero delle righe del file
grep -rI "^foo.*bar$" someDir/ # esegue `grep` ricorsivamente nella cartella, ignorando i file non testuali
# Esegue la stessa ricerca iniziale, ma filtrando solo le righe che contengono la stringa "baz"
grep "^foo.*bar$" file.txt | grep -v "baz"

# se vuoi letteralmente cercare la stringa,
# e non la regex, usa fgrep (o grep -F)
fgrep "foobar" file.txt

# Il comando trap permette di eseguire un comando quando un segnale viene ricevuto dal tuo script.
# In questo esempio, trap eseguirà rm se uno dei tre segnali (SIGHUP, SIGINT o SIGTERM) viene ricevuto.
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM

# `sudo` viene usato per eseguire comandi come superuser, ovvero come utente che ha maggiori privilegi all'interno del sistema
$NOME1=$(whoami)
$NOME2=$(sudo whoami)
echo "Ero $NOME1, poi sono diventato più potente: $NOME2"

# Leggi la documentazione dei builtin di bash con il builtin 'help' di bash:
help
help help
help for
help return
help source
help .

# Leggi la manpage di bash con man
apropos bash
man 1 bash
man bash

# Leggi la documentazione con info (? per help)
apropos info | grep '^info.*('
man info
info info
info 5 info

# Leggi la documentazione di bash:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: bf
filename: learnbf-it.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Ivan Sala", "http://slavni96.github.io/"]
    - ["Christian Grasso", "http://chris54721.net"]
lang: it-it
---

Brainfuck è un linguaggio di programmazione
[Turing equivalente](https://it.wikipedia.org/wiki/Turing_equivalenza)
estremamente minimale, composto da solo 8 comandi.

Puoi provarlo nel tuo browser utilizzando
[brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/).

```

Qualsiasi carattere diverso da "><+-.,[]" (escludendo gli apici)
viene ignorato.
Branfuck è caratterizzato da un array di 30,000 celle inizializzate a zero
e da un puntatore che punta alla cella corrente.

Vi sono otto comandi:
+ : Incrementa il valore della cella attuale di uno.
- : Decrementa il valore della cella attuale di uno.
> : Sposta il puntatore sulla cella seguente (sulla destra).
< : Sposta il puntatore sulla cella precendete (sulla sinistra).
. : Stampa il valore ASCII della cella corrente. (es. 65 = 'A')
, : Legge un singolo carattere come input e lo salva nella cella corrente.
[ : Se il valore della cella corrente è zero, prosegue fino alla ] corrispondente.
    Altrimenti, passa alla prossima istruzione.
] : Se il valore della cella corrente è zero, passa alla prossima istruzione.
    Altrimenti, torna indietro fino alla [ corrispondente.

[ e ] formano un ciclo while. Ovviamente dovranno essere bilanciati.
(Ad ogni [ dovrà corrispondere una ])

Ecco alcuni semplici esempi di programmi scritti in Brainfuck:

++++++ [ > ++++++++++ < - ] > +++++ .

Questo programma stampa in output la lettera 'A'. Prima di tutto, incrementa
la cella #1 fino al valore 6. La cella #1 verrà utilizzata per il ciclo.
Poi, entra nel ciclo ([) e si sposta alla cella #2. Incrementa la cella #2 10
volte, torna alla cella #1, e decrementa quest'ultima.
Il ciclo si ripete 6 volte (la cella #1 viene decrementata 6 volte prima di
raggiungere lo 0, quindi prosegue oltre la corrispondente ]).

A questo punto, siamo sulla cella #1, che ha valore 0, mentre la cella #2 ha
valore 60. Ci spostiamo sulla cella #2, la incrementiamo per 5 volte, ottenendo
il valore 65, quindi stampiamo il valore della cella #2.
Il valore 65 equivale ad 'A' in ASCII, per cui viene stampato 'A' nel terminale.


, [ > + < - ] > .

Questo programma legge un carattere come input dall'utente, quindi salva il
carattere nella cella #1. Dopodichè entra in un ciclo. Si sposta alla cella #2,
incrementa quest'ultima, torna alla cella #1, e decrementa quest'ultima.
Il ciclo continua fino a quando la cella #1 diventa 0, e quindi la cella #2
avrà il valore iniziale della cella #1. Infine, visto che ci troviamo sulla
cella #1 alla fine del ciclo, si sposta sulla cella #2 e stampa il valore in
ASCII.

Gli spazi nel codice sovrastante sono presenti solo a scopo di ottenere
una maggiore leggibilità. Lo stesso programma poteva essere scritto senza spazi:

,[>+<-]>.

Proviamo, adesso, a capire cosa fa invece questo programma:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Il programma legge 2 numeri come input dall'utente, e li moltiplica.

Innanzitutto, legge in input i due numeri. Poi entra nel ciclo più esterno
basandosi sulla cella #1. Quindi si sposta sulla cella #2, e inizia il ciclo
più interno basandosi sul valore della cella #2, incrementando la cella #3.
Arrivati a questo punto abbiamo un problema: alla fine del ciclo interno
la cella #2 avrà valore 0. Ciò impedirà di eseguire nuovamente il ciclo interno.
Per ovviare a questo problema, incrementiamo anche la cella #4, e copiamo il
valore di quest'ultima nella cella #2.
Il risultato sarà infine contenuto nella cella #3.
```

E questo è brainfuck. Non è così difficile, eh? Se vuoi, ora puoi scrivere per
divertimento altri programmi in brainfuck, oppure scrivere un interprete
brainfuck in un altro linguaggio. L'interprete è abbastanza semplice da
implementare, ma se sei veramente masochista, prova ad implementare un interprete brainfuck... in brainfuck.
---
language: c++
filename: learncpp-it.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Matt Kline", "https://github.com/mrkline"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Connor Waters", "http://github.com/connorwaters"]
translators:
    - ["Robert Margelli", "http://github.com/sinkswim/"]
    - ["Tommaso Pifferi", "http://github.com/neslinesli93/"]
lang: it-it
---

Il C++ è un linguaggio di programmazione il quale,
[secondo il suo inventore Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote),
è stato progettato per

- essere un "miglior C"
- supportare l'astrazione dei dati
- supportare la programmazione orientata agli oggetti
- supportare la programmazione generica

Nonostante la sintassi possa risultare più difficile o complessa di linguaggi più recenti,
è usato in maniera vasta poichè viene compilato in istruzioni macchina che possono
essere eseguite direttamente dal processore ed offre un controllo stretto sull'hardware (come il linguaggio C)
ed allo stesso tempo offre caratteristiche ad alto livello come i generici, le eccezioni, e le classi.
Questa combinazione di velocità e funzionalità rende il C++
uno dei più utilizzati linguaggi di programmazione.

```c++
//////////////////
// Confronto con il C
//////////////////

// Il C++ è _quasi_ un superset del C e con esso condivide la sintassi di base per
// la dichiarazione di variabili, tipi primitivi, e funzioni.

// Proprio come nel C, l'inizio del programma è una funzione chiamata
// main con un intero come tipo di ritorno,
// Questo valore serve come stato d'uscita del programma.
// Vedi http://it.wikipedia.org/wiki/Valore_di_uscita per maggiori informazioni.
int main(int argc, char** argv)
{
    // Gli argomenti a linea di comando sono passati tramite argc e argv così come
    // avviene in C.
    // argc indica il numero di argomenti,
    // e argv è un array di stringhe in stile-C (char*)
    // che rappresenta gli argomenti.
    // Il primo argomento è il nome che è stato assegnato al programma.
    // argc e argv possono essere omessi se non hai bisogno di argomenti,
    // in questa maniera la funzione avrà int main() come firma.

    // Lo stato di uscita 0 indica successo.
    return 0;
}

// Tuttavia, il C++ varia nei seguenti modi:

// In C++, i caratteri come letterali sono dei char.
sizeof('c') == sizeof(char) == 1

// In C, i caratteri come letterali sono degli interi.
sizeof('c') == sizeof(int)


// C++ ha prototipizzazione rigida
void func(); // funziona che non accetta argomenti

// In C
void func(); // funzione che può accettare un qualsiasi numero di argomenti

// Usa nullptr invece di NULL in C++
int* ip = nullptr;

// Gli header C standard sono disponibili in C++,
// ma sono prefissati con "c" e non hanno il suffisso ".h".
#include <cstdio>

int main()
{
    printf("Ciao, mondo!\n");
    return 0;
}

///////////////////////////////
// Overloading per le funzioni
//////////////////////////////

// Il C++ supporta l'overloading per le funzioni
// sia dato che ogni funzione accetta parametri diversi.

void print(char const* myString)
{
    printf("Stringa %s\n", myString);
}

void print(int myInt)
{
    printf("Il mio int è %d", myInt);
}

int main()
{
    print("Ciao"); // Viene chiamata void print(const char*)
    print(15); //  Viene chiamata void print(int)
}

////////////////////////
// Argomenti di default
///////////////////////

// Puoi fornire argomenti di default per una funzione
// se non sono forniti dal chiamante.

void faiQualcosaConInteri(int a = 1, int b = 4)
{
    // fai qualcosa con gli interi qui
}

int main()
{
    faiQualcosaConInteri();      // a = 1,  b = 4
    faiQualcosaConInteri(20);    // a = 20, b = 4
    faiQualcosaConInteri(20, 5); // a = 20, b = 5
}

// Gli argomenti di default devono essere alla fine della lista degli argomenti.

void dichiarazioneInvalida(int a = 1, int b) // Errore!
{
}


/////////////
// Namespaces
/////////////

// I namespaces forniscono visibilità separata per dichiarazioni di variabili, funzioni,
// ed altro.
// I namespaces possono essere annidati.

namespace Primo {
    namespace Annidato {
        void foo()
        {
            printf("Questa è Primo::Annidato::foo\n");
        }
    } // fine di namespace Annidato
} // fine di namespace Primo

namespace Secondo {
    void foo()
    {
        printf("Questa è Secondo::foo\n");
    }
}

void foo()
{
    printf("Questa è foo globale\n");
}

int main()
{
    // Include tutti i simboli del namespace Secondo nello scope attuale.
    // Osserva che chiamare semplicemente foo() non va più bene perché è ambiguo:
    // bisogna specificare se vogliamo chiamare foo definita nel namespace Secondo
    // o foo definita nel livello principale del programma.

    using namespace Secondo;

    Secondo::foo(); // stampa "Questa è Secondo::foo"
    Primo::Annidato::foo(); // stampa "Questa è Primo::Annidato::foo"
    ::foo(); // stampa "Questa è foo globale"
}

///////////////
// Input/Output
///////////////

// L'input e l'output in C++ utilizza gli streams
// cin, cout, e cerr i quali rappresentano stdin, stdout, e stderr.
// << è l'operatore di inserzione >> è l'operatore di estrazione.

#include <iostream> // Include gli streams di I/O

using namespace std; // Gli streams sono nel namespace std (libreria standard)

int main()
{
   int myInt;

   // Stampa su stdout (o terminalee/schermo)
   cout << "Inserisci il tuo numero preferito:\n";
   // Prende l'input
   cin >> myInt;

   // cout può anche essere formattato
   cout << "Il tuo numero preferito è " << myInt << "\n";
   // stampa "Il tuo numero preferito è <myInt>"

    cerr << "Usato per messaggi di errore";
}

////////////
// Stringhe
///////////

// Le stringhe in C++ sono oggetti ed hanno molte funzioni membro
#include <string>

using namespace std; // Anche le stringhe sono contenute nel namespace std (libreria standard)

string myString = "Ciao";
string myOtherString = " Mondo";

// + è usato per la concatenazione.
cout << myString + myOtherString; // "Ciao Mondo"

cout << myString + " Bella"; // "Ciao Bella"

// le stringhe in C++ possono essere modificate.
myString.append(" Mario");
cout << myString; // "Ciao Mario"


///////////////
// Riferimenti
//////////////

// Oltre ai puntatori come quelli in C,
// il C++ ha i _riferimenti_.
// Questi non sono tipi puntatori che non possono essere riassegnati una volta settati
// e non possono essere null.
// Inoltre, essi hanno la stessa sintassi della variabile stessa:
// * non è necessario per la dereferenziazione e
// & ("indirizzo di") non è usato per l'assegnamento.

using namespace std;

string foo = "Io sono foo";
string bar = "Io sono bar";


string& fooRef = foo; // Questo crea un riferimento a foo.
fooRef += ". Ciao!"; // Modifica foo attraverso il riferimento
cout << fooRef; // Stampa "Io sono foo. Ciao!"

// Non riassegna "fooRef". Questo è come scrivere "foo = bar", e
//   foo == "Io sono bar"
// dopo questa riga.
cout << &fooRef << endl; // Stampa l'indirizzo di foo
fooRef = bar;
cout << &fooRef << endl; // Stampa lo stesso l'indirizzo di foo
cout << fooRef;  // Stampa "Io sono bar"

// L'indirizzo di fooRef rimane lo stesso, ovvero si riferisce ancora a foo.


const string& barRef = bar; // Crea un riferimento const a bar.
// Come in C, i valori const (i puntatori e i riferimenti) non possono essere modificati.
barRef += ". Ciao!"; // Errore, i riferimenti const non possono essere modificati.

// Facciamo un piccolo excursus: prima di approfondire ancora i riferimenti, è necessario
// introdurre il concetto di oggetto temporaneo. Supponiamo di avere il seguente codice:
string tempObjectFun() { ... }
string retVal = tempObjectFun();

// Nella seconda riga si ha che:
//   - un oggetto di tipo stringa viene ritornato da tempObjectFun
//   - viene costruita una nuova stringa, utilizzando l'oggetto ritornato come
//     argomento per il costruttore
//   - l'oggetto ritornato da tempObjectFun viene distrutto
// L'oggetto ritornato da tempObjectFun viene detto oggetto temporaneo.
// Un oggetto temporaneo viene creato quando una funzione ritorna un oggetto, e viene
// distrutto quando l'espressione che lo racchiude termina la sua esecuzione - questo
// comportamento viene definito dallo standard, ma i compilatori possono modificarlo
// a piacere. Cerca su google "return value optimization" se vuoi approfondire.
// Dunque nel seguente codice:
foo(bar(tempObjectFun()))

// dando per scontato che foo e bar esistano, l'oggetto ritornato da tempObjectFun
// è passato a bar ed è distrutto prima dell'invocazione di foo.

// Tornando ai riferimenti, c'è un'eccezione a quanto appena detto.
// Infatti un oggetto temporaneo "viene distrutto quando l'espressione
// che lo racchiude termina la sua esecuzione", tranne quando è legato ad un
// riferimento di tipo const. In tal caso la sua vita viene estesa per tutto
// lo scope attuale:

void constReferenceTempObjectFun() {
    // constRef riceve l'oggetto temporaneo, che non viene distrutto fino
    // alla fine di questa funzione.
    const string& constRef = tempObjectFun();
    ...
}

// Un altro tipo di riferimento introdotto nel C++11 è specifico per gli
// oggetti temporanei. Non puoi dichiarare una variabile di quel tipo, ma
// ha la precedenza nella risoluzione degli overload:

void someFun(string& s) { ... }  // Riferimento normale
void someFun(string&& s) { ... }  // Riferimento ad un oggetto temporaneo

string foo;
someFun(foo);  // Chiama la versione con il riferimento normale
someFun(tempObjectFun());  // Chiama la versione con il riferimento temporaneo

// Ad esempio potrai vedere questi due costruttori per std::basic_string:
basic_string(const basic_string& other);
basic_string(basic_string&& other);

// L'idea è che se noi costruiamo una nuova stringa a partire da un oggetto temporaneo
// (che in ogni caso verrà distrutto), possiamo avere un costruttore più efficiente
// che in un certo senso "recupera" parti di quella stringa temporanea.
// Ci si riferisce a questo concetto come "move semantics".

/////////////////////
// Enum
/////////////////////

// Gli enum sono un modo per assegnare un valore ad una costante, e sono
// principalmente usati per rendere il codice più leggibile.
enum ETipiMacchine
{
  AlfaRomeo,
  Ferrari,
  SUV,
  Panda
};

ETipiMacchine GetPreferredCarType()
{
    return ETipiMacchine::Ferrari;
}

// Dal C++11 in poi c'è un modo molto semplice per assegnare un tipo ad un enum,
// che può essere utile per la serializzazione dei dati o per convertire gli enum
// tra il tipo desiderato e le rispettive costanti.
enum ETipiMacchine : uint8_t
{
  AlfaRomeo, // 0
  Ferrari, // 1
  SUV = 254, // 254
  Ibrida // 255
};

void WriteByteToFile(uint8_t InputValue)
{
    // Serializza InputValue in un file
}

void WritePreferredCarTypeToFile(ETipiMacchine InputCarType)
{
    // L'enum viene implicitamente convertito ad un uint8_t poiché
    // è stato dichiarato come tale
    WriteByteToFile(InputCarType);
}

// D'altro canto potresti voler evitare che un enum venga accidentalmente convertito
// in un intero o in un altro tipo, quindi è possibile create una classe enum che
// impedisce la conversione implicita.
enum class ETipiMacchine : uint8_t
{
  AlfaRomeo, // 0
  Ferrari, // 1
  SUV = 254, // 254
  Ibrida // 255
};

void WriteByteToFile(uint8_t InputValue)
{
    // Serializza InputValue in un file
}

void WritePreferredCarTypeToFile(ETipiMacchine InputCarType)
{
    // Il compilatore darà errore anche se ETipiMacchine è un uint8_t: questo
    // perchè abbiamo dichiarato l'enum come "enum class"!
    WriteByteToFile(InputCarType);
}

//////////////////////////////////////////////////
// Classi e programmazione orientata agli oggetti
/////////////////////////////////////////////////

// Primo esempio delle classi
#include <iostream>

// Dichiara una classe.
// Le classi sono in genere dichiara in un header file (.h o .hpp).
class Cane {
    // Variabili e funzioni membro sono private di default.
    std::string nome;
    int peso;

// Tutti i membri dopo questo sono pubblici (public)
// finchè "private:" o "protected:" non compaiono.
public:

    // Costruttore di default
    Cane();

    // Dichiarazioni di funzioni membro (le implentazioni sono a seguito)
    // Nota che stiamo usando std::string invece di porre
    // using namespace std;
    // sopra.
    // Mai usare uno statement "using namespace" in uno header.
    void impostaNome(const std::string& nomeCane);

    void impostaPeso(int pesoCane);

    // Le funzioni che non modificano lo stato dell'oggetto
    // dovrebbero essere marcate come const.
    // Questo permette di chiamarle con un riferimento const all'oggetto.
    // Inoltre, nota che le funzioni devono essere dichiarate espliciamente come _virtual_
    // per essere sovrascritte in classi derivate.
    // Le funzioni non sono virtual di default per motivi di performance.
    virtual void print() const;

    // Le funzioni possono essere definite anche all'interno del corpo della classe.
    // Le funzioni definite in questo modo sono automaticamente inline.
    void abbaia() const { std::cout << nome << " abbaia!\n"; }

    // Assieme con i costruttori, il C++ fornisce i distruttori.
    // Questi sono chiamati quando un oggetto è rimosso o esce dalla visibilità.
    // Questo permette paradigmi potenti come il RAII
    // (vedi sotto)
    // I distruttori devono essere virtual per permettere a classi di essere
    // derivate da questa; altrimenti, il distruttore della classe derivata
    // non viene chiamato se l'oggetto viene distrutto tramite un riferimento alla
    // classe da cui ha ereditato o tramite un puntatore.
    virtual ~Dog();

}; // Un punto e virgola deve seguire la definizione della funzione

// Le funzioni membro di una classe sono generalmente implementate in files .cpp .
Cane::Cane()
{
    std::cout << "Un cane è stato costruito\n";
}

// Gli oggetti (ad esempio le stringhe) devono essere passati per riferimento
// se li stai modificando o come riferimento const altrimenti.
void Cane::impostaNome(const std::string& nomeCane)
{
    nome = nomeCane;
}

void Cane::impostaPeso(int pesoCane)
{
    peso = pesoCane;
}

// Notare che "virtual" è solamente necessario nelle dichiarazioni, non nelle definizioni.
void Cane::print() const
{
    std::cout << "Il cane è " << nome << " e pesa " << peso << "kg\n";
}

Cane::~Cane()
{
    std::cout << "Ciao ciao " << nome << "\n";
}

int main() {
    Cane myDog; // stampa "Un cane è stato costruito"
    myDog.impostaNome("Barkley");
    myDog.impostaPeso(10);
    myDog.print(); // stampa "Il cane è Barkley e pesa 10 kg"
    return 0;
} // stampa "Ciao ciao Barkley"

// Ereditarietà:

// Questa classe eredita tutto ciò che è public e protected dalla classe Cane,
// ma anche ciò che privato: tuttavia non potrà accedere direttamente a membri/metodi
// privati se non c'è un metodo pubblico o privato che permetta di farlo.
class MioCane : public Cane {

    void impostaProprietario(const std::string& proprietarioCane);

    // Sovrascrivi il comportamento della funzione print per tutti i MioCane. Vedi
    // http://it.wikipedia.org/wiki/Polimorfismo_%28informatica%29
    // per una introduzione più generale se non sei familiare con
    // il polimorfismo.
    // La parola chiave override è opzionale ma fa sì che tu stia effettivamente
    // sovrascrivendo il metodo nella classe base.
    void print() const override;

private:
    std::string proprietario;
};

// Nel frattempo, nel file .cpp corrispondente:

void MioCane::impostaProprietario(const std::string& proprietarioCane)
{
    proprietario = proprietarioCane;
}

void MioCane::print() const
{
    Cane::print(); // Chiama la funzione print nella classe base Cane
    std::cout << "Il cane è di " << proprietario << "\n";
    // stampa "Il cane è <nome> e pesa <peso>"
    //        "Il cane è di <proprietario>"
}

///////////////////////////////////////////////////
// Inizializzazione ed Overloading degli Operatori
//////////////////////////////////////////////////

// In C++ puoi sovrascrivere il comportamento di operatori come +, -, *, /, ecc...
// Questo è possibile definendo una funzione che viene chiamata
// ogniqualvolta l'operatore è usato.

#include <iostream>
using namespace std;

class Punto {
public:
    // Così si assegna alle variabili membro un valore di default.
    double x = 0;
    double y = 0;

    // Definisce un costruttore di default che non fa nulla
    // ma inizializza il Punto ai valori di default (0, 0)
    Punto() { };

    // La sintassi seguente è nota come lista di inizializzazione
    // ed è il modo appropriato di inizializzare i valori membro della classe
    Punto (double a, double b) :
        x(a),
        y(b)
    { /* Non fa nulla eccetto inizializzare i valori */ }

    // Sovrascrivi l'operatore +.
    Punto operator+(const Punto& rhs) const;

    // Sovrascrivi l'operatore +=
    Punto& operator+=(const Punto& rhs);

    // Avrebbe senso aggiungere gli operatori - e -=,
    // ma li saltiamo per rendere la guida più breve.
};

Punto Punto::operator+(const Punto& rhs) const
{
    // Crea un nuovo punto come somma di questo e di rhs.
    return Punto(x + rhs.x, y + rhs.y);
}

Punto& Punto::operator+=(const Punto& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

int main () {
    Punto su (0,1);
    Punto destro (1,0);
    // Questo chiama l'operatore + di Punto
    // Il Punto su chiama la funzione + con destro come argomento
    Punto risultato = su + destro;
    // Stampa "Risultato è spostato in (1,1)"
    cout << "Risultato è spostato (" << risultato.x << ',' << risultato.y << ")\n";
    return 0;
}

/////////////////
// Templates
////////////////

// Generalmente i templates in C++ sono utilizzati per programmazione generica, anche se
// sono molto più potenti dei costrutti generici in altri linguaggi. Inoltre,
// supportano specializzazione esplicita e parziale, classi in stile funzionale,
// e sono anche complete per Turing.

// Iniziamo con il tipo di programmazione generica con cui forse sei familiare. Per
// definire una classe o una funzione che prende un parametro di un dato tipo:
template<class T>
class Box {
public:
    // In questa classe, T può essere usato come qualsiasi tipo.
    void inserisci(const T&) { ... }
};

// Durante la compilazione, il compilatore in effetti genera copie di ogni template
// con i parametri sostituiti, e così la definizione completa della classe deve essere
// presente ad ogni invocazione. Questo è il motivo per cui vedrai le classi template definite
// interamente in header files.

// Per instanziare una classe template sullo stack:
Box<int> intBox;

// e puoi usarla come aspettato:
intBox.inserisci(123);

//Puoi, ovviamente, innestare i templates:
Box<Box<int> > boxOfBox;
boxOfBox.inserisci(intBox);

// Fino al C++11, devi porre uno spazio tra le due '>', altrimenti '>>'
// viene visto come l'operatore di shift destro.

// Qualche volta vedrai
// template<typename T>
// invece. La parole chiavi 'class' e 'typename' sono _generalmente_
// intercambiabili in questo caso. Per una spiegazione completa, vedi
// http://en.wikipedia.org/wiki/Typename
// (si, quella parola chiave ha una sua pagina di Wikipedia propria).

// Similmente, una funzione template:
template<class T>
void abbaiaTreVolte(const T& input)
{
    input.abbaia();
    input.abbaia();
    input.abbaia();
}

// Nota che niente è specificato relativamente al tipo di parametri. Il compilatore
// genererà  e poi verificherà il tipo di ogni invocazione del template, così che
// la funzione di cui sopra funzione con ogni tipo 'T' che ha const 'abbaia' come metodo!

Cane fluffy;
fluffy.impostaNome("Fluffy")
abbaiaTreVolte(fluffy); // Stampa "Fluffy abbaia" tre volte.

// I parametri template non devono essere classi:
template<int Y>
void stampaMessaggio() {
  cout << "Impara il C++ in " << Y << " minuti!" << endl;
}

// E poi esplicitamente specializzare i template per avere codice più efficiente. Ovviamente,
// la maggior parte delle casistiche reali non sono così triviali.
// Notare che avrai comunque bisogna di dichiarare la funzione (o classe) come un template
// anche se hai esplicitamente specificato tutti i parametri.
template<>
void stampaMessaggio<10>() {
  cout << "Impara il C++ più velocemente in soli 10 minuti!" << endl;
}

printMessage<20>();  // Stampa "impara il C++ in 20 minuti!"
printMessage<10>();  // Stampa "Impara il C++ più velocemente in soli 10 minuti!"                                   

////////////////////////////
// Gestione delle eccezioni
///////////////////////////

// La libreria standard fornisce un paio di tipi d'eccezioni
// (vedi http://en.cppreference.com/w/cpp/error/exception)
// ma ogni tipo può essere lanciato come eccezione
#include <exception>
#include <stdexcept>

// Tutte le eccezioni lanciate all'interno del blocco _try_ possono essere catturate dai successivi
// handlers _catch_.
try {
    // Non allocare eccezioni nello heap usando _new_.
    throw std::runtime_error("C'è stato un problema.");
}

// Cattura le eccezioni come riferimenti const se sono oggetti
catch (const std::exception& ex)
{
    std::cout << ex.what();
}

// Cattura ogni eccezioni non catturata dal blocco _catch_ precedente
catch (...)
{
    std::cout << "Catturata un'eccezione sconosciuta";
    throw; // Rilancia l'eccezione
}

///////
// RAII
///////

// RAII sta per "Resource Allocation Is Initialization".
// Spesso viene considerato come il più potente paradigma in C++.
// È un concetto semplice: un costruttore di un oggetto
// acquisisce le risorse di tale oggetto ed il distruttore le rilascia.

// Per comprendere come questo sia vantaggioso,
// consideriamo una funzione che usa un gestore di file in C:
void faiQualcosaConUnFile(const char* nomefile)
{
    // Per cominciare, assumiamo che niente possa fallire.

    FILE* fh = fopen(nomefile, "r"); // Apri il file in modalità lettura.

    faiQualcosaConIlFile(fh);
    faiQualcosAltroConEsso(fh);

    fclose(fh); // Chiudi il gestore di file.
}

// Sfortunatamente, le cose vengono complicate dalla gestione degli errori.
// Supponiamo che fopen fallisca, e che faiQualcosaConUnFile e
// faiQualcosAltroConEsso ritornano codici d'errore se falliscono.
//  (Le eccezioni sono la maniera preferita per gestire i fallimenti,
//   ma alcuni programmatori, specialmente quelli con un passato in C,
//   non sono d'accordo con l'utilità delle eccezioni).
// Adesso dobbiamo verificare che ogni chiamata per eventuali fallimenti e chiudere il gestore di file
// se un problema è avvenuto.
bool faiQualcosaConUnFile(const char* nomefile)
{
    FILE* fh = fopen(nomefile, "r"); // Apre il file in modalità lettura
    if (fh == nullptr) // Il puntatore restituito è null in caso di fallimento.
        return false; // Riporta il fallimento al chiamante.

    // Assumiamo che ogni funzione ritorni false se ha fallito
    if (!faiQualcosaConIlFile(fh)) {
        fclose(fh); // Chiude il gestore di file così che non sprechi memoria.
        return false; // Propaga l'errore.
    }
    if (!faiQualcosAltroConEsso(fh)) {
        fclose(fh); // Chiude il gestore di file così che non sprechi memoria.
        return false; // Propaga l'errore.
    }

    fclose(fh); // Chiudi il gestore di file così che non sprechi memoria.
    return true; // Indica successo
}

// I programmatori C in genere puliscono questa procedura usando goto:
bool faiQualcosaConUnFile(const char* nomefile)
{
    FILE* fh = fopen(nomefile, "r");
    if (fh == nullptr)
        return false;

    if (!faiQualcosaConIlFile(fh))
        goto fallimento;

    if (!faiQualcosAltroConEsso(fh))
        goto fallimento;

    fclose(fh); // Chiude il file
    return true; // Indica successo

fallimento:
    fclose(fh);
    return false; // Propaga l'errore
}

// Se le funzioni indicano errori usando le eccezioni,
// le cose sono un pò più pulite, ma sono sempre sub-ottimali.
void faiQualcosaConUnFile(const char* nomefile)
{
    FILE* fh = fopen(nomefile, "r"); // Apre il file in modalità lettura
    if (fh == nullptr)
        throw std::runtime_error("Errore nell'apertura del file.");

    try {
        faiQualcosaConIlFile(fh);
        faiQualcosAltroConEsso(fh);
    }
    catch (...) {
        fclose(fh); // Fai sì che il file venga chiuso se si ha un errore.
        throw; // Poi rilancia l'eccezione.
    }

    fclose(fh); // Chiudi il file
    // Tutto è andato bene
}

// Confronta questo con l'utilizzo della classe C++ file stream (fstream)
// fstream usa i distruttori per chiudere il file.
// Come detto sopra, i distruttori sono automaticamente chiamati
// ogniqualvolta un oggetto esce dalla visibilità.
void faiQualcosaConUnFile(const std::string& nomefile)
{
    // ifstream è l'abbreviazione di input file stream
    std::ifstream fh(nomefile); // Apre il file

    // Fai qualcosa con il file
    faiQualcosaConIlFile(fh);
    faiQualcosAltroConEsso(fh);

} // Il file viene chiuso automaticamente chiuso qui dal distruttore

// Questo ha vantaggi _enormi_:
// 1. Può succedere di tutto ma
//    la risorsa (in questo caso il file handler) verrà ripulito.
//    Una volta che scrivi il distruttore correttamente,
//    È _impossibile_ scordarsi di chiudere l'handler e sprecare memoria.
// 2. Nota che il codice è molto più pulito.
//    Il distruttore gestisce la chiusura del file dietro le scene
//    senza che tu debba preoccupartene.
// 3. Il codice è sicuro da eccezioni.
//    Una eccezione può essere lanciata in qualunque punto nella funzione e la ripulitura
//    avverrà lo stesso.

// Tutto il codice C++ idiomatico usa RAII in maniera vasta su tutte le risorse.
// Esempi aggiuntivi includono
// - Utilizzo della memoria con unique_ptr e shared_ptr
// - I contenitori - la lista della libreria standard,
//   vettori (i.e. array auto-aggiustati), mappe hash, e così via
//   sono tutti automaticamente distrutti con i loro contenuti quando escono dalla visibilità.
// - I mutex usano lock_guard e unique_lock

// I contenitori che utilizzano chiavi non-primitive (classi personalizzate)
// richiedono la funzione di confronto nell'oggetto stesso, o tramite un puntatore a funzione.
// Le chiavi primitive hanno funzioni di confronto già definite, ma puoi sovrascriverle.
class Foo {
public:
    int j;
    Foo(int a) : j(a) {}
};
struct funzioneDiConfronto {
    bool operator()(const Foo& a, const Foo& b) const {
        return a.j < b.j;
    }
};
// Questo non è permesso, anche se qualche compilatore potrebbe non dare problemi
//std::map<Foo, int> fooMap;
std::map<Foo, int, funzioneDiConfronto> fooMap;
fooMap[Foo(1)]  = 1;
fooMap.find(Foo(1)); -- vero

///////////////////////////////////////
// Espressioni Lambda (C++11 e superiori)
///////////////////////////////////////

// Le espressioni lambda (più semplicemente "lambda") sono utilizzate
// per definire una funzione anonima nel punto in cui viene invocata, o
// dove viene passata come argomento ad una funzione

// Ad esempio, consideriamo l'ordinamento di un vettore costituito da una
// coppia di interi, utilizzando il secondo elemento per confrontare
vector<pair<int, int> > tester;
tester.push_back(make_pair(3, 6));
tester.push_back(make_pair(1, 9));
tester.push_back(make_pair(5, 0));

// Passiamo una lambda come terzo argomento alla funzione di ordinamento
// `sort` è contenuta nell'header <algorithm>
sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs) {
    return lhs.second < rhs.second;
});

// Nota bene la sintassi utilizzata nelle lambda:
// [] serve per "catturare" le variabili.
// La "Lista di Cattura" definisce tutte le variabili esterne che devono essere disponibili
// all'interno della funzione, e in che modo.
// La lista può contenere:
//     1. un valore: [x]
//     2. un riferimento: [&x]
//     3. qualunque variabile nello scope corrente, per riferimento [&]
//     4. qualunque variabile nello scope corrente, per valore [=]
// Esempio:

vector<int> id_cani;
// numero_cani = 3;
for(int i = 0; i < 3; i++) {
    id_cani.push_back(i);
}

int pesi[3] = {30, 50, 10};

// Mettiamo che vuoi ordinare id_cani in base al peso dei cani
// Alla fine, id_cani sarà: [2, 0, 1]

// Le lambda vengono in aiuto

sort(id_cani.begin(), id_cani.end(), [&pesi](const int &lhs, const int &rhs) {
    return pesi[lhs] < pesi[rhs];
});
// Nota come abbiamo catturato "pesi" per riferimento nell'esempio.
// Altre informazioni sulle lambda in C++: http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11

///////////////////////////////
// Ciclo For semplificato(C++11 e superiori)
///////////////////////////////

// Puoi usare un ciclo for per iterare su un tipo di dato contenitore
int arr[] = {1, 10, 3};

for(int elem: arr) {
    cout << elem << endl;
}

// Puoi usare "auto" senza preoccuparti del tipo degli elementi nel contenitore
// Ad esempio:

for(auto elem: arr) {
    // Fai qualcosa con `elem`
}

///////////////////////
// Roba divertente
//////////////////////

// Aspetti del C++ che potrebbero sbalordire i nuovi arrivati (e anche qualche veterano).
// Questa sezione è, sfortunatamente, selvaggiamente incompleta; il C++ è uno dei linguaggi
// più facili con cui puoi spararti da solo nel piede.

// Puoi sovrascrivere metodi privati!
class Foo {
  virtual void bar();
};
class FooSub : public Foo {
  virtual void bar();  // Sovrascrive Foo::bar!
};


// 0 == false == NULL (la maggior parte delle volte)!
bool* pt = new bool;
*pt = 0; // Setta il valore puntato da 'pt' come falso.
pt = 0;  // Setta 'pt' al puntatore null. Entrambe le righe vengono compilate senza warnings.

// nullptr dovrebbe risolvere alcune di quei problemi:
int* pt2 = new int;
*pt2 = nullptr; // Non compila
pt2 = nullptr;  // Setta pt2 a null.

// C'è un'eccezione per i bool.
// Questo permette di testare un puntatore a null con if(!ptr), ma
// come conseguenza non puoi assegnare nullptr a un bool direttamente!
*pt = nullptr;  // Questo compila, anche se '*pt' è un bool!


// '=' != '=' != '='!
// Chiama Foo::Foo(const Foo&) o qualche variante (vedi "move semantics")
// del costruttore di copia.
Foo f2;
Foo f1 = f2;

// Chiama Foo::Foo(const Foo&) o qualche variante, ma solo copie di 'Foo' che fanno parte di
// 'fooSub'. Ogni altro membro di 'fooSub' viene scartato. Questo comportamento
// orribile viene chiamato "object slicing."
FooSub fooSub;
Foo f1 = fooSub;

// Chiama Foo::operator=(Foo&) o una sua variante.
Foo f1;
f1 = f2;


///////////////////////////////////////
// Tuple (C++11 e superiori)
///////////////////////////////////////

#include<tuple>

// Concettualmente le tuple sono simili alle strutture del C, ma invece di avere
// i membri rappresentati con dei nomi, l'accesso agli elementi avviene tramite
// il loro ordine all'interno della tupla.

// Cominciamo costruendo una tupla.
// Inserire i valori in una tupla
auto prima = make_tuple(10, 'A');
const int maxN = 1e9;
const int maxL = 15;
auto seconda = make_tuple(maxN, maxL);

// Vediamo gli elementi contenuti nella tupla "prima"
cout << get<0>(prima) << " " << get<1>(prima) << "\n"; // stampa : 10 A

// Vediamo gli elementi contenuti nella tupla "seconda"
cout << get<0>(seconda) << " " << get<1>(seconda) << "\n"; // stampa: 1000000000 15

// Estrarre i valori dalla tupla, salvandoli nelle variabili
int primo_intero;
char primo_char;
tie(primo_intero, primo_char) = prima;
cout << primo_intero << " " << primo_char << "\n";  // stampa : 10 A

// E' possibile creare tuple anche in questo modo
tuple<int, char, double> terza(11, 'A', 3.14141);

// tuple_size ritorna il numero di elementi in una tupla (come constexpr)
cout << tuple_size<decltype(terza)>::value << "\n"; // stampa: 3

// tuple_cat concatena gli elementi di tutte le tuple, nell'esatto ordine
// in cui sono posizionati all'interno delle tuple stesse
auto tupla_concatenata = tuple_cat(prima, seconda, terza);
// tupla_concatenata diventa = (10, 'A', 1e9, 15, 11, 'A' ,3.14141)

cout << get<0>(tupla_concatenata) << "\n"; // stampa: 10
cout << get<3>(tupla_concatenata) << "\n"; // stampa: 15
cout << get<5>(tupla_concatenata) << "\n"; // stampa: 'A'


/////////////////////
// Contenitori
/////////////////////

// I Contenitori della "Standard Template Library", ovvero la libreria standard
// dei template contenuti nel C++, sono template predefiniti.
// I Contenitori si occupano di come allocare lo spazio per gli elementi contenuti,
// e forniscono funzioni per accedervi e manipolarli

// Vediamo alcuni tipi di contenitori:

// Vector (array dinamici/vettori)
// Permettono di definire un vettore, o una lista di oggetti, a runtime
#include<vector>
vector<Tipo_Dato> nome_vettore; // usato per inizializzare un vettore
cin >> val;
nome_vettore.push_back(val); // inserisce il valore di "val" nel vettore

// Per iterare in un vettore, abbiamo due possibilità:
// Ciclo normale
for(int i=0; i<nome_vettore.size(); i++)
// Cicla dall'indice zero fino all'ultimo

// Iteratore
vector<Tipo_Dato>::iterator it; // inizializza l'iteratore per il vettore
for(it=nome_vettore.begin(); it!=nome_vettore.end();++it)
// Nota che adesso non cicla più sugli indici, ma direttamente sugli elementi!

// Per accedere agli elementi del vettore
// Operatore []
var = nome_vettore[indice]; // Assegna a "var" il valore del vettore all'indice dato


// Set (insiemi)
// Gli insiemi sono contenitori che memorizzano elementi secondo uno specifico ordine.
// Gli insiemi vengono per lo più utilizzati per memorizzare valori unici, secondo
// un ordine, senza scrivere ulteriore codice.

#include<set>
set<int> insieme;    // Inizializza un insieme di interi
insieme.insert(30);  // Inserisce il valore 30 nell'insieme
insieme.insert(10);  // Inserisce il valore 10 nell'insieme
insieme.insert(20);  // Inserisce il valore 20 nell'insieme
insieme.insert(30);  // Inserisce il valore 30 nell'insieme
// Gli elementi dell'insieme sono:
//  10 20 30

// Per cancellare un elemento
insieme.erase(20);  // Cancella l'elemento con valore 20
// L'insieme contiene adesso: 10 30

// Per iterare su un insieme, usiamo gli iteratori
set<int>::iterator it;
for(it=insieme.begin();it<insieme.end();it++) {
    cout << *it << endl;
}
// Stampa:
// 10
// 30

// Per svuotare il contenitore usiamo il metodo "clear"
insieme.clear();
cout << insieme.size();
// Stampa: 0

// Nota: per permettere elementi duplicati, possiamo usare "multiset"

// Map (mappa/tabella di hash)
// Le mappe servono per memorizzare un elemento, detto chiave, a cui viene
// associato un valore, il tutto secondo uno specifico ordine.

#include<map>
map<char, int> mia_mappa;  // Inizializza una mappa che usa i char come chiave, e gli interi come valore

mia_mappa.insert(pair<char,int>('A',1));
// Inserisce il valore 1 per la chiave A
mia_mappa.insert(pair<char,int>('Z',26));
// Inserisce il valore 26 per la chiave Z

// Per iterare
map<char,int>::iterator it;
for (it=mia_mappa.begin(); it!=mia_mappa.end(); ++it)
    std::cout << it->first << "->" << it->second << '\n';
// Stampa:
// A->1
// Z->26

// Per trovare il valore corrispondente ad una data chiave
it = mia_mappa.find('Z');
cout << it->second;
// Stampa: 26


///////////////////////////////////
// Operatori logici e bitwise(bit-a-bit)
//////////////////////////////////

// La maggior parte di questi operatori in C++ sono gli stessi degli altri linguaggi

// Operatori logici

// Il C++ usa la "Short-circuit evaluation" per le espressioni booleane. Cosa significa?
// In pratica, in una condizione con due argomenti, il secondo viene considerato solo se
// il primo non basta a determinate il valore finale dell'espresione.

true && false // Effettua il **and logico** e ritorna falso
true || false // Effettua il **or logico** e ritorna vero
! true        // Effettua il **not logico** e ritorna falso

// Invece di usare i simboli, si possono usare le keyword equivalenti
true and false // Effettua il **and logico** e ritorna falso
true or false  // Effettua il **or logico** e ritorna vero
not true       // Effettua il **not logico** e ritorna falso

// Operatori bitwise(bit-a-bit)

// **<<** Operatore di Shift a Sinistra
// << sposta i bit a sinistra
4 << 1 // Sposta a sinistra di 1 i bit di 4, ottenendo 8
// x << n in pratica realizza x * 2^n


// **>>** Operatore di Shift a Destra
// >> sposta i bit a destra
4 >> 1 // Sposta a destra di 1 i bit di 4, ottenendo 2
// x >> n in pratica realizza x / 2^n

~4    // Effettua il NOT bit-a-bit
4 | 3 // Effettua il OR bit-a-bit
4 & 3 // Effettua il AND bit-a-bit
4 ^ 3 // Effettua il XOR bit-a-bit

// Le keyword equivalenti sono
compl 4    // Effettua il NOT bit-a-bit
4 bitor 3  // Effettua il OR bit-a-bit
4 bitand 3 // Effettua il AND bit-a-bit
4 xor 3    // Effettua il XOR bit-a-bit

```
Letture consigliate:

Un riferimento aggiornato del linguaggio può essere trovato qui
<http://cppreference.com/w/cpp>

Risorse addizionali possono essere trovate qui <http://cplusplus.com>
---
language: coffeescript
contributors:
  - ["Luca 'Kino' Maroni", "http://github.com/kino90"]
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
filename: coffeescript-it.coffee
lang: it-it
---

CoffeeScript è un piccolo linguaggio che compila direttamente nell'equivalente
JavaScript, non c'è nessuna interpretazione a runtime. Come possibile 
successore di Javascript, CoffeeScript fa il suo meglio per restituire 
un codice leggibile, ben stampato e performante in ogni ambiente JavaScript.

Guarda anche [il sito di CoffeeScript](http://coffeescript.org/), che ha una 
guida completa a CoffeeScript.

```coffeescript
# CoffeeScript è un linguaggio hipster.
# Segue le mode di alcuni linguaggi moderni.
# Quindi i commenti sono come quelli di Ruby e Python, usano il cancelletto.

###
I blocchi di commenti sono definiti con tre cancelletti, che vengono tradotti 
direttamente in `/*` e `*/` nel codice JavaScript risultante.

Prima di continuare devi conoscere la maggior parte
delle semantiche JavaScript.
###

# Assegnamento:
numero   = 42 #=> var numero = 42;
contrario = true #=> var contrario = true;

# Condizioni:
numero = -42 if contrario #=> if(contrario) { numero = -42; }

# Funzioni:
quadrato = (x) -> x * x #=> var quadrato = function(x) { return x * x; }

riempi = (contenitore, liquido = "caffè") ->
  "Sto riempiendo #{contenitore} con #{liquido}..."
#=>var riempi;
#
#riempi = function(contenitore, liquido) {
#  if (liquido == null) {
#    liquido = "caffè";
#  }
#  return "Sto riempiendo " + contenitore + " con " + liquido + "...";
#};

# Intervalli:
lista = [1..5] #=> var lista = [1, 2, 3, 4, 5];

# Oggetti:
matematica =
  radice:   Math.sqrt
  quadrato: quadrato
  cubo:   (x) -> x * quadrato x
#=> var matematica = {
#     "radice": Math.sqrt,
#     "quadrato": quadrato,
#     "cubo": function(x) { return x * quadrato(x); }
#   }

# Splats:
gara = (vincitore, partecipanti...) ->
  print vincitore, partecipanti
#=>gara = function() {
#    var partecipanti, vincitore;
#    vincitore = arguments[0], partecipanti = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#    return print(vincitore, partecipanti);
#  };

# Esistenza:
alert "Lo sapevo!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("Lo sapevo!"); }

# Comprensione degli Array:
cubi = (matematica.cubo num for num in lista)
#=>cubi = (function() {
#	   var _i, _len, _results;
#	   _results = [];
#    for (_i = 0, _len = lista.length; _i < _len; _i++) {
#      num = lista[_i];
#      _results.push(matematica.cubo(num));
#    }
#    return _results;
#  })();

cibi = ['broccoli', 'spinaci', 'cioccolato']
mangia cibo for cibo in cibi when cibo isnt 'cioccolato'
#=>cibi = ['broccoli', 'spinaci', 'cioccolato'];
#
#for (_k = 0, _len2 = cibi.length; _k < _len2; _k++) {
#  cibo = cibi[_k];
#  if (cibo !== 'cioccolato') {
#    mangia(cibo);
#  }
#}
```

## Altre risorse

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: elixir
contributors:
    - ["Luca 'Kino' Maroni", "https://github.com/kino90"]
    - ["Joao Marques", "http://github.com/mrshankly"]
    - ["Dzianis Dashkevich", "https://github.com/dskecse"]
translators:
    - ["Tommaso Pifferi","http://github.com/neslinesli93"]
filename: learnelixir-it.ex
lang: it-it
---

Elixir è un linguaggio funzionale moderno, costruito sulla VM Erlang.
È totalmente compatibile con Erlang, ma con una sintassi più standard
e molte altre funzionalità.

```elixir

# I commenti su una riga iniziano con un cancelletto.

# Non esistono commenti multilinea,
# ma puoi concatenare più commenti.

# Per usare la shell di elixir usa il comando `iex`.
# Compila i tuoi moduli con il comando `elixirc`.

# Entrambi i comandi dovrebbero già essere nel tuo PATH se hai installato 
# elixir correttamente.

## ---------------------------
## -- Tipi di base
## ---------------------------

# Numeri
3    # intero (Integer)
0x1F # intero
3.0  # decimale (Float)

# Atomi, che sono literals, una costante con un nome. Iniziano con `:`.
:ciao # atomo (Atom)

# Tuple che sono salvate in celle di memoria contigue.
{1,2,3} # tupla (Tuple)

# Possiamo accedere ad un elemento di una tupla con la funzione `elem`:
elem({1, 2, 3}, 0) #=> 1

# Liste, che sono implementate come liste concatenate (o linked list).
[1,2,3] # lista (List)

# Possiamo accedere alla testa (head) e alla coda (tail) delle liste così:
[testa | coda] = [1,2,3]
testa #=> 1
coda #=> [2,3]

# In Elixir, proprio come in Erlang, il simbolo `=` denota pattern matching e
# non un assegnamento.
#
# Questo significa che la parte sinistra (pattern) viene confrontata alla
# parte destra.
#
# Questo spiega il funzionamento dell'esempio dell'accesso alla lista di prima.

# Un pattern match darà errore quando le parti non combaciano, ad esempio se
# le tuple hanno dimensione differente.
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# Ci sono anche i binari 
<<1,2,3>> # binari (Binary)

# Stringhe e liste di caratteri
"ciao" # stringa (String)
'ciao' # lista di caratteri (List)

# Stringhe multilinea
"""
Sono una stringa
multi-linea.
"""
#=> "Sono una stringa\nmulti-linea.\n"

# Le stringhe sono tutte codificate in UTF-8:
"cìaò" 
#=> "cìaò"

# le stringhe in realtà sono dei binari, e le liste di caratteri sono liste.
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# `?a` in elixir restituisce il valore ASCII della lettera `a`
?a #=> 97

# Per concatenare liste si usa `++`, per binari si usa `<>`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'ciao ' ++ 'mondo'  #=> 'ciao mondo'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"ciao " <> "mondo"  #=> "ciao mondo"

# Gli intervalli sono rappresentati come `inizio..fine` (estremi inclusi)
1..10 #=> 1..10 (Range)
minore..maggiore = 1..10 # Puoi fare pattern matching anche sugli intervalli
[minore, maggiore] #=> [1, 10]

## ---------------------------
## -- Operatori
## ---------------------------

# Un po' di matematica
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# In elixir l'operatore `/` restituisce sempre un decimale.

# Per fare una divisione intera si usa `div`
div(10, 2) #=> 5

# Per ottenere il resto di una divisione si usa `rem`
rem(10, 3) #=> 1

# Ci sono anche gli operatori booleani: `or`, `and` e `not`.
# Questi operatori si aspettano un booleano come primo argomento.
true and true #=> true
false or true #=> true
# 1 and true    #=> ** (ArgumentError) argument error

# Elixir fornisce anche `||`, `&&` e `!` che accettano argomenti
# di qualsiasi tipo. 
# Tutti i valori tranne `false` e `nil` saranno valutati come true.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil
!true #=> false

# Per i confronti abbiamo: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` e `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# `===` e `!==` sono più rigidi quando si confrontano interi e decimali:
1 == 1.0  #=> true
1 === 1.0 #=> false

# Possiamo anche confrontare tipi di dato diversi:
1 < :ciao #=> true

# L'ordine generale è definito sotto:
# numeri < atomi < riferimenti < funzioni < porte < pid < tuple < liste 
#   < stringhe di bit

# Per citare Joe Armstrong su questo: "L'ordine non è importante,
# ma è importante che sia definito un ordine."

## ---------------------------
## -- Controllo di flusso
## ---------------------------

# espressione `se` (`if`)
if false do
  "Questo non si vedrà mai"
else
  "Questo sì"
end

# c'è anche un `se non` (`unless`)
unless true do
  "Questo non si vedrà mai"
else
  "Questo sì"
end

# Ti ricordi il pattern matching? 
# Moltre strutture di controllo di flusso in elixir si basano su di esso.

# `case` ci permette di confrontare un valore a diversi pattern:
case {:uno, :due} do
  {:quattro, :cinque} ->
    "Questo non farà match"
  {:uno, x} ->
    "Questo farà match e binderà `x` a `:due`"
  _ ->
    "Questo farà match con qualsiasi valore"
end

# Solitamente si usa `_` se non si ha bisogno di utilizzare un valore.
# Ad esempio, se ci serve solo la testa di una lista:
[testa | _] = [1,2,3]
testa #=> 1

# Per aumentare la leggibilità possiamo usarlo in questo modo:
[testa | _coda] = [:a, :b, :c]
testa #=> :a

# `cond` ci permette di verificare più condizioni allo stesso momento.
# Usa `cond` invece di innestare più espressioni `if`.
cond do
  1 + 1 == 3 ->
    "Questa stringa non si vedrà mai"
  2 * 5 == 12 ->
    "Nemmeno questa"
  1 + 2 == 3 ->
    "Questa sì!"
end

# È pratica comune mettere l'ultima condizione a `true`, che farà sempre match
cond do
  1 + 1 == 3 ->
    "Questa stringa non si vedrà mai"
  2 * 5 == 12 ->
    "Nemmeno questa"
  true ->
    "Questa sì! (essenzialmente funziona come un else)"
end

# `try/catch` si usa per gestire i valori lanciati (throw), 
# Supporta anche una clausola `after` che è invocata in ogni caso.
try do
  throw(:ciao)
catch
  message -> "Ho ricevuto #{message}."
after
  IO.puts("Io sono la clausola 'after'.")
end
#=> Io sono la clausola 'after'
# "Ho ricevuto :ciao"

## ---------------------------
## -- Moduli e Funzioni
## ---------------------------

# Funzioni anonime (notare il punto)
quadrato = fn(x) -> x * x end
quadrato.(5) #=> 25

# Accettano anche guardie e condizioni multiple.
# le guardie ti permettono di perfezionare il tuo pattern matching, 
# sono indicate dalla parola chiave `when`:
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir fornisce anche molte funzioni, disponibili nello scope corrente.
is_number(10)    #=> true
is_list("ciao") #=> false
elem({1,2,3}, 0) #=> 1

# Puoi raggruppare delle funzioni all'interno di un modulo.
# All'interno di un modulo usa `def` per definire le tue funzioni.
defmodule Matematica do
  def somma(a, b) do
    a + b
  end

  def quadrato(x) do
    x * x
  end
end

Matematica.somma(1, 2)  #=> 3
Matematica.quadrato(3) #=> 9

# Per compilare il modulo 'Matematica' salvalo come `matematica.ex` e usa 
# `elixirc`.
# nel tuo terminale: elixirc matematica.ex

# All'interno di un modulo possiamo definire le funzioni con `def` e funzioni
# private con `defp`.
# Una funzione definita con `def` è disponibile per essere invocata anche da 
# altri moduli, una funziona privata può essere invocata solo localmente.
defmodule MatematicaPrivata do
  def somma(a, b) do
    esegui_somma(a, b)
  end

  defp esegui_somma(a, b) do
    a + b
  end
end

MatematicaPrivata.somma(1, 2)    #=> 3
# MatematicaPrivata.esegui_somma(1, 2) #=> ** (UndefinedFunctionError)

# Anche le dichiarazioni di funzione supportano guardie e condizioni multiple:
defmodule Geometria do
  def area({:rettangolo, w, h}) do
    w * h
  end

  def area({:cerchio, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometria.area({:rettangolo, 2, 3}) #=> 6
Geometria.area({:cerchio, 3})       #=> 28.25999999999999801048
# Geometria.area({:cerchio, "non_un_numero"})
#=> ** (FunctionClauseError) no function clause matching in Geometria.area/1

# A causa dell'immutabilità dei dati, la ricorsione è molto frequente in elixir
defmodule Ricorsione do
  def somma_lista([testa | coda], accumulatore) do
    somma_lista(coda, accumulatore + testa)
  end

  def somma_lista([], accumulatore) do
    accumulatore
  end
end

Ricorsione.somma_lista([1,2,3], 0) #=> 6

# I moduli di Elixir supportano attributi. Ci sono degli attributi incorporati
# e puoi anche aggiungerne di personalizzati.
defmodule Modulo do
  @moduledoc """
  Questo è un attributo incorporato in un modulo di esempio.
  """

  @miei_dati 100 # Questo è un attributo personalizzato .
  IO.inspect(@miei_dati) #=> 100
end

## ---------------------------
## -- Strutture ed Eccezioni
## ---------------------------


# Le Strutture (Structs) sono estensioni alle mappe che portano 
# valori di default, garanzia alla compilazione e polimorfismo in Elixir.
defmodule Persona do
  defstruct nome: nil, eta: 0, altezza: 0
end

luca = %Persona{ nome: "Luca", eta: 24, altezza: 185 }
#=> %Persona{eta: 24, altezza: 185, nome: "Luca"}

# Legge al valore di 'nome'
luca.nome #=> "Luca"

# Modifica il valore di eta
luca_invecchiato = %{ luca | eta: 25 }
#=> %Persona{eta: 25, altezza: 185, nome: "Luca"}

# Il blocco `try` con la parola chiave `rescue` è usato per gestire le eccezioni
try do
  raise "un errore"
rescue
  RuntimeError -> "Salvato un errore di Runtime"
  _error -> "Questo salverà da qualsiasi errore"
end

# Tutte le eccezioni hanno un messaggio
try do
  raise "un errore"
rescue
  x in [RuntimeError] ->
    x.message
end

## ---------------------------
## -- Concorrenza
## ---------------------------

# Elixir si basa sul modello degli attori per la concorrenza. 
# Tutto ciò di cui abbiamo bisogno per scrivere programmi concorrenti in elixir
# sono tre primitive: creare processi, inviare messaggi e ricevere messaggi.

# Per creare un nuovo processo si usa la funzione `spawn`, che riceve una
# funzione come argomento.
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>

# `spawn` restituisce un pid (identificatore di processo). Puoi usare questo
# pid per inviare messaggi al processo.
# Per passare messaggi si usa l'operatore `send`.
# Perché tutto questo sia utile dobbiamo essere capaci di ricevere messaggi, 
# oltre ad inviarli. Questo è realizzabile con `receive`:

# Il blocco `receive do` viene usato per mettersi in ascolto di messaggi
# ed elaborarli quando vengono ricevuti. Un blocco `receive do` elabora
# un solo messaggio ricevuto: per fare elaborazione multipla di messaggi, 
# una funzione con un blocco `receive do` al suo intero dovrà chiamare
# ricorsivamente sé stessa per entrare di nuovo nel blocco `receive do`.
defmodule Geometria do
  def calcolo_area do
    receive do
      {:rettangolo, w, h} ->
        IO.puts("Area = #{w * h}")
        calcolo_area()
      {:cerchio, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        calcolo_area()
    end
  end
end

# Compila il modulo e crea un processo che esegue `calcolo_area` nella shell
pid = spawn(fn -> Geometria.calcolo_area() end) #=> #PID<0.40.0>
# Alternativamente
pid = spawn(Geometria, :calcolo_area, [])

# Invia un messaggio a `pid` che farà match su un pattern nel blocco in receive 
send pid, {:rettangolo, 2, 3}
#=> Area = 6
#   {:rettangolo,2,3}

send pid, {:cerchio, 2}
#=> Area = 12.56000000000000049738
#   {:cerchio,2}

# Anche la shell è un processo. Puoi usare `self` per ottenere il pid corrente
self() #=> #PID<0.27.0>
```

## Referenze

* [Getting started guide](http://elixir-lang.org/getting_started/1.html) dalla [pagina web ufficiale di elixir](http://elixir-lang.org)
* [Documentazione Elixir](http://elixir-lang.org/docs/master/)
* ["Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) di Dave Thomas
* [Elixir Cheat Sheet](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf)
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) di Fred Hebert
* ["Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) di Joe Armstrong
---
category: tool
tool: git
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Leo Rudberg" , "http://github.com/LOZORD"]
    - ["Betsy Lorton" , "http://github.com/schbetsy"]
    - ["Bruno Volcov", "http://github.com/volcov"]
translators:
    - ["Christian Grasso", "http://chris54721.net"]
filename: LearnGit-it.txt
lang: it-it
---

Git è un sistema di
[controllo versione distribuito](https://it.wikipedia.org/wiki/Controllo_versione_distribuito)
e di gestione del codice sorgente.

Git esegue una serie di _snapshot_ per salvare lo stato di un progetto, così
facendo può fornirti la possibilità di gestire il tuo codice e di salvarne lo
stato assegnando delle versioni.

## Basi del controllo versione

### Cos'è il controllo versione?

Il controllo versione (_Version Control_ o _Versioning_) è un sistema che
registra le modifiche apportate a uno o più file nel tempo.

### Controllo versione centralizzato e distribuito

* Il controllo versione centralizzato si concentra sulla sincronizzazione, il
  monitoraggio e il backup dei file.
* Il controllo versione distribuito si concentra sulla condivisione delle
  modifiche. Ogni modifica ha un identificatore univoco.
* I sistemi distribuiti non hanno una struttura definita. Si potrebbe creare
  ad esempio un sistema centralizzato simile a SVN utilizzando Git.

[Ulteriori informazioni](http://git-scm.com/book/it/v1/Per-Iniziare-Il-Controllo-di-Versione)

### Perchè usare Git?

* Consente di lavorare offline.
* Collaborare con altre persone è semplice!
* Utilizzare i branch (rami di sviluppo) è semplice!
* Git è veloce.
* Git è flessibile.

## Architettura di Git

### Repository

Un insieme di file, cartelle, registrazioni della cronologia e versioni.
Immaginalo come una struttura dati del codice, con la caratteristica che ogni
"elemento" del codice ti fornisce accesso alla sua cronologia delle revisioni,
insieme ad altre cose.

Un repository comprende la cartella .git e il working tree.

### Cartella .git (componente del repository)

La cartella .git contiene tutte le configurazioni, i log, i rami e altro.
[Lista dettagliata](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### Working Tree (componente del repository)

Si tratta semplicemente delle cartelle e dei file presenti nel repository.
Spesso viene indicato come "directory di lavoro" ("working directory").

### Index (componente della cartella .git)

L'Index è l'area di staging di Git. Si tratta di un livello che separa il
working tree dal repository. Ciò fornisce agli sviluppatori più controllo su
cosa viene inviato al repository.

### Commit

Un commit è uno snapshot di una serie di modifiche apportate al working tree.
Ad esempio, se hai aggiunto 5 file e ne hai rimossi 2, ciò sarà registrato in
un commit. Il commit può essere pushato (inviato) o meno ad altri repository.

### Branch (ramo)

Un branch (ramo) è essenzialmente un puntatore all'ultimo commit che hai
effettuato. Effettuando altri commit, il puntatore verrà automaticamente
aggiornato per puntare all'ultimo commit.

### Tag

Un tag è un contrassegno applicato a un punto specifico nella cronologia dei
commit. Di solito i tag vengono utilizzati per contrassegnare le versioni
rilasciate (v1.0, v1.1, etc.).

### HEAD e head (componenti della cartella .git)

HEAD (in maiuscolo) è un puntatore che punta al branch corrente. Un repository
può avere solo 1 puntatore HEAD *attivo*.

head (in minuscolo) è un puntatore che può puntare a qualsiasi commit. Un
repository può avere un numero qualsiasi di puntatori head.

### Stadi di Git
* _Modified_ - Un file è stato modificato, ma non è ancora stato effettuato
  un commit per registrare le modifiche nel database di Git
* _Staged_ - Un file modificato è stato contrassegnato per essere incluso nel
  prossimo commit
* _Committed_ - È stato effettuato un commit e le modifiche sono state
  registrate nel database di Git

## Comandi

### init

Crea un repository Git vuoto. Le impostazioni e le informazioni del repository
sono salvate nella cartella ".git".

```bash
$ git init
```

### config

Utilizzato per configurare le impostazioni, sia specifiche del repository, sia
a livello globale. Le impostazioni globali sono salvate in `~/.gitconfig`.

```bash
$ git config --global user.email "email@example.com"
$ git config --global user.name "Nome utente"
```

[Ulteriori informazioni su git config](http://git-scm.com/docs/git-config)

### help

Fornisce una documentazione molto dettagliata di ogni comando.

```bash
# Mostra i comandi più comuni
$ git help

# Mostra tutti i comandi disponibili
$ git help -a

# Documentazione di un comando specifico
# git help <nome_comando>
$ git help add
$ git help commit
$ git help init
# oppure git <nome_comando> --help
$ git add --help
$ git commit --help
$ git init --help
```

### Ignorare file

Per impedire intenzionalmente che file privati o temporanei vengano inviati
al repository Git.

```bash
$ echo "temp/" >> .gitignore
$ echo "privato.txt" >> .gitignore
```


### status

Mostra le differenza tra lo stato attuale del working tree e l'attuale commit
HEAD.

```bash
$ git status
```

### add

Aggiunge file alla staging area, ovvero li contrassegna per essere inclusi nel
prossimo commit. Ricorda di aggiungere i nuovi file, altrimenti non saranno
inclusi nei commit!

```bash
# Aggiunge un file nella directory attuale
$ git add HelloWorld.java

# Aggiunge un file in una sottocartella
$ git add /path/to/file/HelloWorld.c

# Il comando supporta le espressioni regolari
$ git add ./*.java

# Aggiunge tutti i file non ancora contrassegnati
$ git add --all
```

Questo comando contrassegna soltanto i file, senza effettuare un commit.

### branch

Utilizzato per gestire i branch (rami). Puoi visualizzare, modificare, creare o
eliminare branch utilizzando questo comando.

```bash
# Visualizza i branch e i remote
$ git branch -a

# Crea un nuovo branch
$ git branch nuovoBranch

# Elimina un branch
$ git branch -d nomeBranch

# Rinomina un branch
$ git branch -m nomeBranch nuovoNomeBranch

# Permette di modificare la descrizione di un branch
$ git branch nomeBranch --edit-description
```

### tag

Utilizzato per gestire i tag.

```bash
# Visualizza i tag esistenti
$ git tag
# Crea un nuovo tag
# L'opzione -m consente di specificare una descrizione per il tag.
# Se l'opzione -m non viene aggiunta, Git aprirà un editor per consentire
# l'inserimento del messaggio.
$ git tag -a v2.0 -m 'Versione 2.0'
# Mostra informazioni relative a un tag
# Include informazioni sul creatore del tag, la data di creazione, e il
# messaggio assegnato al tag oltre alle informazioni sul commit.
$ git show v2.0
```

### checkout

Consente di cambiare branch o ripristinare i file a una revisione specifica.
Tutti i file nel working tree vengono aggiornati per corrispondere alla versione
presente nel branch o nel commit specificato.

```bash
# Effettua il checkout di un repository - il branch predefinito è 'master'
$ git checkout
# Effettua il checkout di un branch specifico
$ git checkout nomeBranch
# Crea un nuovo branch e ne effettua il checkout
# Equivalente a "git branch <nomeBranch>; git checkout <nomeBranch>"
$ git checkout -b nuovoBranch
```

### clone

Clona, o copia, un repository esistente in una nuova directory. Inoltre,
aggiunge dei branch _remote-tracking_, utilizzati per monitorare i branch
remoti corrispondenti a quelli locali, e consentendo così di inviare le
modifiche al repository remoto.

```bash
# Clona learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git
# Clona solo l'ultima revisione di un repository
$ git clone --depth 1 https://github.com/adambard/learnxinyminutes-docs.git
# Clona solo un branch specifico
$ git clone -b master-cn https://github.com/adambard/learnxinyminutes-docs.git --single-branch
```

### commit

Effettua uno _snapshot_ dello stato attuale del working tree e registra le
modifiche in un nuovo commit. Il commit contiene, oltre alle modifiche apportate,
anche l'autore e una descrizione.

```bash
# Crea un nuovo commit con un messaggio
$ git commit -m "Aggiunta la funzione multiplyNumbers() in HelloWorld.c"

# Aggiunge (git add) automaticamente i file modificati o eliminati (ESCLUSI
# i nuovi file) e quindi effettua il commit
$ git commit -a -m "Modificato foo.php e rimosso bar.php"

# Modifica l'ultimo commit (il comando elimina il commit precedente e lo
# sostituisce con uno nuovo)
$ git commit --amend -m "Messaggio corretto"
```

### diff

Mostra la differenza tra un file nel working tree e la sua versione nell'index,
in un branch o ad un commit specifico.

```bash
# Mostra la differenza tra il working tree e l'index
$ git diff

# Mostra la differenza tra l'index e il commit più recente
$ git diff --cached

# Mostra la differenza tra il working tree e un commit specifico
$ git diff <commit>

# Mostra la differenza tra due commit
$ git diff <commit1> <commit2>
```

### grep

Consente di effettuare una ricerca veloce nel repository.

```bash
# Cerca "variableName" nei file Java
$ git grep 'variableName' -- '*.java'

# Cerca una riga contenente "arrayListName" E "add" oppure "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
```

Impostazioni relative a `git grep`:

```bash
# Mostra il numero delle righe
$ git config --global grep.lineNumber true

# Rende i risultati più leggibili
$ git config --global alias.g "grep --break --heading --line-number"
```

### log

Mostra la cronologia dei commit inviati al repository.

```bash
# Mostra tutti i commit
$ git log

# Mostra ogni commit su una sola riga
$ git log --oneline

# Mostra solo i commit legati ai merge
$ git log --merges
```

### merge

Effettua un "merge", ovvero unisce le modifiche di un branch in quello attuale.

```bash
# Unisce il branch specificato a quello attuale
$ git merge nomeBranch

# Genera un commit in ogni caso dopo aver eseguito il merge
$ git merge --no-ff nomeBranch
```

### mv

Rinomina o sposta un file.

```bash
# Rinomina un file
$ git mv HelloWorld.c HelloNewWorld.c

# Sposta un file
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# Forza l'esecuzione del comando
# Se un file "nuovoNomeFile" esiste già nella directory, verrà sovrascritto
$ git mv -f nomeFile nuovoNomeFile
```

### pull

Aggiorna il repository effettuando il merge delle nuove modifiche.

```bash
# Aggiorna il branch attuale dal remote "origin"
$ git pull

# Di default, git pull aggiorna il branch attuale effettuando il merge
# delle nuove modifiche presenti nel branch remote-tracking corrispondente
$ git pull

# Aggiorna le modifiche dal branch remoto, quindi effettua il rebase dei commit
# nel branch locale
# Equivalente a: "git pull <remote> <branch>; git rebase <branch>"
$ git pull origin master --rebase
```

### push

Invia ed effettua il merge delle modifiche da un branch locale ad uno remoto.

```bash
# Invia ed effettua il merge delle modifiche dal branch "master"
# al remote "origin".
# git push <remote> <branch>
$ git push origin master

# Di default, git push invia ed effettua il merge delle modifiche
# dal branch attuale al branch remote-tracking corrispondente
$ git push

# Per collegare il branch attuale ad uno remoto, basta aggiungere l'opzione -u
$ git push -u origin master
```

### stash

Salva lo stato attuale del working tree in una lista di modifiche non ancora
inviate al repository con un commit che possono essere applicate nuovamente
in seguito.

Questo comando può essere utile se, ad esempio, mentre stai effettuando delle
modifiche non ancora completate, hai bisogno di aggiornare il repository locale
con `git pull`. Poichè non hai ancora effettuato il commit di tutte le modifiche,
non sarà possibile effettuare il pull. Tuttavia, puoi utilizzare `git stash` per
salvare temporaneamente le modifiche e applicarle in seguito.

```bash
$ git stash
```

Ora puoi effettuare il pull:

```bash
$ git pull
```

A questo punto, come già suggerito dall'output del comando `git stash`, puoi
applicare le modifiche:

```bash
$ git stash apply
```

Infine puoi controllare che tutto sia andato bene:

```bash
$ git status
```

Puoi visualizzare gli accantonamenti che hai effettuato finora utilizzando:

```bash
$ git stash list
```

### rebase (attenzione)

Applica le modifiche effettuate su un branch su un altro branch.
*Non effettuare il rebase di commit che hai già inviato a un repository pubblico!*

```bash
# Effettua il rebase di experimentBranch in master
$ git rebase master experimentBranch
```

[Ulteriori informazioni](https://git-scm.com/book/it/v1/Diramazioni-in-Git-Rifondazione)

### reset (attenzione)

Effettua il reset del commit HEAD attuale ad uno stato specifico.
Questo comando consente di annullare `merge`, `pull`, `commit`, `add` e altro.
Tuttavia, può essere pericoloso se non si sa cosa si sta facendo.

```bash
# Effettua il reset della staging area (annullando le aggiunte e le rimozioni
# di file dal repository, senza modificare il working tree)
$ git reset

# Effettua il reset completo della staging area, ovvero annulla qualsiasi
# modifica al repository eliminando definitivamente anche tutte le modifiche
# ai file non inviate e ripristinando il working tree
$ git reset --hard

# Effettua il reset del branch attuale al commit specificato (lasciando il
# working tree intatto)
$ git reset 31f2bb1

# Effettua il reset completo del branch attuale al commit specificato,
# eliminando qualsiasi modifica non inviata
$ git reset --hard 31f2bb1
```

### rm

Consente di rimuovere un file dal working tree e dal repository.
Per eliminare un file solo dal working tree ma non dal repository, è invece
necessario utilizzare `/bin/rm`.

```bash
# Elimina un file nella directory attuale
$ git rm HelloWorld.c

# Elimina un file da una sottocartella
$ git rm /pather/to/the/file/HelloWorld.c
```
---
name: Go
language: Go
filename: learngo-it.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
    - ["Alexej Friesen", "https://github.com/heyalexej"]
    - ["Clayton Walker", "https://github.com/cwalk"]
translators:
    - ["Tommaso Pifferi","http://github.com/neslinesli93"]
lang: it-it
---

Go è stato creato per avere tra le mani uno strumento in grado di arrivare
al punto, nel modo più veloce ed efficiente possibile. Non è all'ultima
moda tra i linguaggi di programmazione, ma è una delle migliori soluzioni
per risolvere in maniera efficace i problemi di tutti i giorni.

Go presenta alcuni concetti già presenti nei linguaggi imperativi con
tipizzazione statica. Compila velocemente ed esegue altrettanto veloce.
Aggiunge la concorrenza in maniera diretta e semplice da capire, per far
forza sulle CPU multi-core di oggigiorno. Presenta caratteristiche utili
per la programmazione in larga scala.

Go comes with a great standard library and an enthusiastic community.

```go
// Commento su riga singola
/* Commento
 su riga multipla */

// In cima a ogni file è necessario specificare il package.
// Main è un package speciale che identifica un eseguibile anziché una libreria.
package main

// Con import sono dichiarate tutte le librerie a cui si fa riferimento 
// all'interno del file.
import (
	"fmt"       // Un package nella libreria standard di Go.
	"io/ioutil" // Implementa alcune funzioni di utility per l'I/O.
	m "math"    // Libreria matematica, con alias locale m
	"net/http"  // Sì, un web server!
	"strconv"   // Package per la conversione di stringhe.
)

// Una definizione di funzione. Il main è speciale: è il punto di ingresso
// per il programma. Amalo o odialo, ma Go usa le parentesi graffe.
func main() {
    // Println stampa una riga a schermo.
    // Questa funzione è all'interno del package fmt.
	fmt.Println("Ciao mondo!")

    // Chiama un'altra funzione all'interno di questo package.
	oltreIlCiaoMondo()
}

// Le funzioni ricevono i parametri all'interno di parentesi tonde.
// Se la funzione non riceve parametri, vanno comunque messe le parentesi (vuote).
func oltreIlCiaoMondo() {
	var x int // Dichiarazione di una variabile. Ricordati di dichiarare sempre le variabili prima di usarle!
	x = 3     // Assegnazione di una variabile.
    // E' possibile la dichiarazione "rapida" := per inferire il tipo, dichiarare e assegnare contemporaneamente.
	y := 4
    // Una funzione che ritorna due valori.
	somma, prod := imparaMoltepliciValoriDiRitorno(x, y)
	fmt.Println("somma:", somma, "prodotto:", prod)    // Semplice output.
	imparaTipi()                                       // < y minuti, devi imparare ancora!
}

/* <- commento su righe multiple
Le funzioni possono avere parametri e ritornare (molteplici!) valori.
Qua, x e y sono gli argomenti, mentre somma e prod sono i valori ritornati.
Da notare il fatto che x e somma vengono dichiarati come interi.
*/
func imparaMoltepliciValoriDiRitorno(x, y int) (somma, prod int) {
	return x + y, x * y // Ritorna due valori.
}

// Ecco alcuni tipi presenti in Go
func imparaTipi() {
    // La dichiarazione rapida di solito fa il suo lavoro.
	str := "Impara il Go!" // Tipo stringa.

	s2 := `Una stringa letterale
puo' includere andata a capo.` // Sempre di tipo stringa.

    // Stringa letterale non ASCII. I sorgenti Go sono in UTF-8.
	g := 'Σ' // Il tipo runa, alias per int32, è costituito da un code point unicode.

	f := 3.14195 // float64, un numero in virgola mobile a 64-bit (IEEE-754)

	c := 3 + 4i  // complex128, rappresentato internamente con due float64.

    // Inizializzare le variabili con var.
	var u uint = 7 // Senza segno, ma la dimensione dipende dall'implementazione (come l'int)
	var pi float32 = 22. / 7 

    // Sintassi per la conversione.
	n := byte('\n') // Il tipo byte è un alias per uint8.

    // I vettori hanno dimensione fissa, stabilita durante la compilazione.
	var a4 [4]int           // Un vettore di 4 interi, tutti inizializzati a 0.
	a3 := [...]int{3, 1, 5} // Un vettore inizializzato con una dimensione fissa pari a 3, i cui elementi sono 3, 1 e 5.

    // Gli slice hanno dimensione variabile. Vettori e slice hanno pro e contro,
    // ma generalmente si tende a usare più spesso gli slice.
	s3 := []int{4, 5, 9}    // La differenza con a3 è che qua non ci sono i 3 punti all'interno delle parentesi quadre.
	s4 := make([]int, 4)    // Alloca uno slice di 4 interi, tutti inizializzati a 0.
	var d2 [][]float64      // Semplice dichiarazione, non vengono fatte allocazioni.
	bs := []byte("uno slice") // Sintassi per la conversione.

    // Poiché gli slice sono dinamici, è possibile aggiungere elementi
    // quando è necessario. Per farlo, si usa la funzione append(). Il primo
    // argomento è lo slice a cui stiamo aggiungendo elementi. Di solito
    // lo slice viene aggiornato, senza fare una copia, come nell'esempio:
	s := []int{1, 2, 3}		// Il risultato è uno slice di dimensione 3.
	s = append(s, 4, 5, 6)	// Aggiunge 3 elementi: lo slice ha dimensione 6.
	fmt.Println(s) // Lo slice aggiornato è [1 2 3 4 5 6]
    // Per aggiungere un altro slice, invece che elencare gli elementi uno ad
    // uno, è possibile passare alla funzione append un riferimento ad uno
    // slice, oppure uno slice letterale: in questo caso si usano i tre punti,
    // dopo lo slice, a significare "prendi ciascun elemento dello slice":
	s = append(s, []int{7, 8, 9}...) // Il secondo argomento è uno slice letterale.
	fmt.Println(s)	// Lo slice aggiornato è [1 2 3 4 5 6 7 8 9]

	p, q := imparaLaMemoria() // Dichiara due puntatori a intero: p e q.
	fmt.Println(*p, *q)   // * dereferenzia un puntatore. Questo stampa due interi.

    // Una variabile di tipo map è un vettore associativo di dimensione variabile,
    // e funzionano come le tabelle di hash o i dizionari in altri linguaggi.
	m := map[string]int{"tre": 3, "quattro": 4}
	m["uno"] = 1

    // Le variabili dichiarate e non usate sono un errore in Go.
    // L'underscore permette di "usare" una variabile, scartandone il valore.
	_, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs
	// Stampare a schermo ovviamente significa usare una variabile.
	fmt.Println(s, c, a4, s3, d2, m)

	imparaControlloDiFlusso() // Torniamo in carreggiata.
}

// In Go è possibile associare dei nomi ai valori di ritorno di una funzione.
// Assegnare un nome al tipo di dato ritornato permette di fare return in vari
// punti all'interno del corpo della funzione, ma anche di usare return senza
// specificare in modo esplicito che cosa ritornare.
func imparaValoriDiRitornoConNome(x, y int) (z int) {
	z = x * y
	return // z è implicito, perchè compare nella definizione di funzione.
}

// Go è dotato di garbage collection. Ha i puntatori, ma non l'aritmetica dei
// puntatori. Puoi fare errori coi puntatori a nil, ma non puoi direttamente
// incrementare un puntatore.
func imparaLaMemoria() (p, q *int) {
    // I valori di ritorno (con nome) p e q sono puntatori a int.
	p = new(int) // La funzione new si occupa di allocare memoria.
    // L'int allocato viene inizializzato a 0, dunque p non è più nil.
	s := make([]int, 20) // Alloca 20 int come un singolo blocco di memoria.
	s[3] = 7             // Ne assegna uno.
	r := -2              // Dichiara un'altra variabile locale
	return &s[3], &r     // & "prende" l'indirizzo di un oggetto.
}

func calcoloCostoso() float64 {
	return m.Exp(10)
}

func imparaControlloDiFlusso() {
    // L'istruzione if richiede parentesi graffe per il corpo, mentre non ha
    // bisogno di parentesi tonde per la condizione.
	if true {
		fmt.Println("te l'ho detto")
	}
    // Eseguendo "go fmt" da riga di comando, il codice viene formattato
    // in maniera standard.
	if false {
		// :(
	} else {
		// :D
	}
    // L'istruzione switch serve ad evitare tanti if messi in cascata.
	x := 42.0
	switch x {
	case 0:
	case 1:
	case 42:
		// Quando è soddisfatta la condizione all'interno di un case, il
        // programma esce dal switch senza che siano specificate istruzioni
        // di tipo "break". In Go infatti di default non è presente il
        // cosiddetto "fall through" all'interno dell'istruzione switch.
        // Tuttavia, il linguaggio mette a disposizione la parola chiave
        // fallthrough per permettere, in casi particolari, questo comportamento.
	case 43:
		// Non si arriva qua.
	default:
		// Il caso di default è opzionale.
	}
    // Come l'if, anche il for non usa parentesi tonde per la condizione.
    // Le variabili dichiarate all'interno di if/for sono locali al loro scope.
	for x := 0; x < 3; x++ { // ++ è un'istruzione!
		fmt.Println("ciclo numero", x)
	}
	// x == 42 qua.

    // Il for è l'unica istruzione per ciclare in Go, ma ha varie forme.
	for { // Ciclo infinito.
		break    // Scherzavo.
		continue // Non si arriva qua.
	}

    // Puoi usare range per ciclare su un vettore, slice, stringa, mappa o canale.
    // range ritorna uno (per i canali) o due valori (vettore, slice, stringa, mappa).
	for chiave, valore := range map[string]int{"uno": 1, "due": 2, "tre": 3} {
        // per ogni coppia dentro la mappa, stampa chiave e valore
		fmt.Printf("chiave=%s, valore=%d\n", chiave, valore)
	}

    // Come nel for, := dentro la condizione dell'if è usato per dichiarare
    // e assegnare y, poi testare se y > x.
	if y := calcoloCostoso(); y > x {
		x = y
	}
	// Le funzioni letterali sono closure.
	xGrande := func() bool {
		return x > 10000 // Si riferisce a x dichiarata sopra al switch (vedi sopra).
	}
	fmt.Println("xGrande:", xGrande()) // true (abbiamo assegnato e^10 a x).
	x = 1.3e3                          // Adesso x == 1300
	fmt.Println("xGrande:", xGrande()) // false ora.

    // Inoltre le funzioni letterali possono essere definite e chiamate
    // inline, col ruolo di parametri di funzione, a patto che:
    // a) la funzione letterale venga chiamata subito (),
    // b) il valore ritornato è in accordo con il tipo dell'argomento.
	fmt.Println("Somma e raddoppia due numeri: ",
		func(a, b int) int {
			return (a + b) * 2
		}(10, 2)) // Chiamata con argomenti 10 e 2
	// => Somma e raddoppia due numeri: 24

	// Quando ti servirà, lo amerai.
	goto amore
amore:

	imparaFabbricaDiFunzioni() // Una funzione che ritorna un'altra funzione è divertente!
	imparaDefer()              // Un tour veloce di una parola chiave importante.
	imparaInterfacce()         // Arriva la roba buona!
}

func imparaFabbricaDiFunzioni() {
    // Questi due blocchi di istruzioni sono equivalenti, ma il secondo è più semplice da capire.
	fmt.Println(fabbricaDiFrasi("estate")("Una bella giornata", "giornata!"))

	d := fabbricaDiFrasi("estate")
	fmt.Println(d("Una bella", "giornata!"))
	fmt.Println(d("Un pigro", "pomeriggio!"))
}

// I decoratori sono comuni in alcuni linguaggi. Si può fare lo stesso in Go
// con le funzioni letterali che accettano argomenti.
func fabbricaDiFrasi(miaStringa string) func(prima, dopo string) string {
	return func(prima, dopo string) string {
		return fmt.Sprintf("%s %s %s", prima, miaStringa, dopo) // Nuova stringa
	}
}

func imparaDefer() (ok bool) {
    // Le istruzioni dette "deferred" (rinviate) sono eseguite
    // appena prima che la funzione ritorni.
	defer fmt.Println("le istruzioni 'deferred' sono eseguite in ordine inverso (LIFO).")
	defer fmt.Println("\nQuesta riga viene stampata per prima perché")
    // defer viene usato di solito per chiudere un file, così la funzione che
    // chiude il file viene messa vicino a quella che lo apre.
	return true
}

// Definisce Stringer come un'interfaccia con un metodo, String.
type Stringer interface {
	String() string
}

// Definisce coppia come una struct con due campi interi, chiamati x e y.
type coppia struct {
	x, y int
}

// Definisce un metodo sul tipo coppia, che adesso implementa Stringer.
func (p coppia) String() string { // p viene definito "ricevente"
    // Sprintf è un'altra funzione del package ftm.
    // La notazione con il punto serve per richiamare i campi di p.
	return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func imparaInterfacce() {
	// Brace syntax is a "struct literal". It evaluates to an initialized
	// struct. The := syntax declares and initializes p to this struct.
    // Le parentesi graffe sono usate per le cosiddette "struct letterali".
    // Con :=, p viene dichiarata e inizializzata a questa struct.
	p := coppia{3, 4}
	fmt.Println(p.String()) // Chiama il metodo String di p, che è di tipo coppia.
	var i Stringer          // Dichiara i come interfaccia Stringer.
	i = p                   // Valido perchè coppia implementa Stringer.
    // Chiama il metodo String di i, che è di tipo Stringer. Output uguale a sopra.
	fmt.Println(i.String())

	// Functions in the fmt package call the String method to ask an object
	// for a printable representation of itself.
    // Le funzioni dentro al package fmt chiamano il metodo String per
    // chiedere ad un oggetto una rappresentazione in stringhe di sé stesso.
	fmt.Println(p) // Output uguale a sopra. Println chiama il metodo String.
	fmt.Println(i) // Output uguale a sopra.

	imparaParametriVariadici("grande", "imparando", "qua!")
}

// Le funzioni possono avere parametri variadici (ovvero di lunghezza variabile).
func imparaParametriVariadici(mieStringhe ...interface{}) {
    // Cicla su ogni valore variadico.
    // L'underscore serve a ignorare l'indice del vettore.
	for _, param := range mieStringhe {
		fmt.Println("parametro:", param)
	}

    // Passa un valore variadico come parametro variadico.
	fmt.Println("parametri:", fmt.Sprintln(mieStringhe...))

	imparaGestioneErrori()
}

func imparaGestioneErrori() {
    // La sintassi ", ok" è usata per indicare se qualcosa ha funzionato o no.
	m := map[int]string{3: "tre", 4: "quattro"}
	if x, ok := m[1]; !ok { // ok sarà false perchè 1 non è dentro la mappa.
		fmt.Println("qua non c'è nessuno!")
	} else {
		fmt.Print(x) // x sarebbe il valore che corrisponde alla chiave 1, se fosse nella mappa.
	}
    // Un errore non riporta soltanto "ok" ma è più specifico riguardo al problema.
	if _, err := strconv.Atoi("non_intero"); err != nil { // _ scarta il valore
		// stampa 'strconv.ParseInt: parsing "non_intero": invalid syntax'
		fmt.Println(err)
	}
    // Approfondiremo le interfacce un'altra volta. Nel frattempo,
	imparaConcorrenza()
}

// c è un canale, un oggetto per comunicare in modo concorrente e sicuro.
func inc(i int, c chan int) {
	c <- i + 1 // <- è l'operatore di "invio" quando un canale sta a sinistra.
}

// Useremo inc per incrementare alcuni numeri in modo concorrente.
func imparaConcorrenza() {
    // Stessa funzione usata prima per creare uno slice. Make alloca e
    // inizializza slice, mappe e canali.
	c := make(chan int)
    // Lancia tre goroutine. I numeri saranno incrementati in modo concorrente,
    // forse in parallelo se la macchina lo supporta. Tutti e tre inviano dati
    // sullo stesso canale.
	go inc(0, c) // go è un'istruzione che avvia una goroutine.
	go inc(10, c)
	go inc(-805, c)
    // Legge tre risultati dal canale e li stampa a schermo.
    // Non si conosce a priori l'ordine in cui i risultati arriveranno!
	fmt.Println(<-c, <-c, <-c) // <- è l'operatore di "ricevuta" quando
    // un canale sta a destra.

	cs := make(chan string)       // Un altro canale, gestisce le stringhe.
	ccs := make(chan chan string) // Un canale che gestisce canali di stringhe.
	go func() { c <- 84 }()       // Lancia una goroutine, solo per inviare un valore.
	go func() { cs <- "parolina" }() // Stessa cosa ma per cs.
    // select è simile a switch, ma ogni case riguarda un'operazione su un
    // canale. Seleziona, in modo random, uno tra i canali che sono pronti
    // a comunicare.
	select {
	case i := <-c: // Il valore ricevuto può essere assegnato a una variabile,
		fmt.Printf("E' un %T", i)
	case <-cs: // oppure il valore ricevuto può essere scartato.
		fmt.Println("E' una stringa.")
	case <-ccs: // Canale vuoto, non pronto per comunicare.
		fmt.Println("Non succede niente.")
	}
    // A questo punto un valore è stato preso da c o cs. Una delle tue goroutine
    // cominciate sopra ha completato l'esecuzione, l'altra rimarrà bloccata.

	imparaProgrammazioneWeb() // Se lo fa Go, lo puoi fare anche tu.
}

// Una funzione all'interno del package http avvia un webserver.
func imparaProgrammazioneWeb() {

    // Il primo parametro di ListenAndServe è l'indirizzo TCP su cui ascoltare.
    // Il secondo parametro è un'interfaccia, precisamente http.Handler.
	go func() {
		err := http.ListenAndServe(":8080", coppia{})
		fmt.Println(err) // Non ignorare gli errori.
	}()

	richiediServer()
}

// Per rendere coppia un http.Handler basta implementare il metodo ServeHTTP.
func (p coppia) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // Il server fornisce dati con un metodo di http.ResponseWriter.
	w.Write([]byte("Hai imparato Go in Y minuti!"))
}

func richiediServer() {
	risposta, err := http.Get("http://localhost:8080")
	fmt.Println(err)
	defer risposta.Body.Close()
	corpo, err := ioutil.ReadAll(risposta.Body)
	fmt.Printf("\nIl webserver dice: `%s`", string(corpo))
}
```

## Letture consigliate

La risorsa più importante per imparare il Go è il [sito ufficiale di Go](http://golang.org/).
Qui puoi seguire i tutorial, scrivere codice in modo interattivo, e leggere tutti i dettagli.
Oltre al tour, [la documentazione](https://golang.org/doc/) contiene informazioni su
come scrivere ottimo codice in Go, documentazione sui package e sui comandi, e
la cronologia delle release.

Anche il documento che definisce il linguaggio è un'ottima lettura. E' semplice
da leggere e incredibilmente corto (rispetto ad altri documenti riguardanti
la creazione di linguaggi).

Puoi giocare con il codice visto finora nel [Go playground](https://play.golang.org/p/Am120Xe7qf).
Prova a cambiarlo e ad eseguirlo dal browser!
Osserva che puoi usare [https://play.golang.org](https://play.golang.org) come
una [REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) per scrivere
codice all'interno del browser, senza neanche installare Go!

Una lettura importante per capire Go in modo più profondo è il [codice 
sorgente della libreria standard](http://golang.org/src/pkg/). Infatti è
molto ben documentato e costituisce quanto più chiaro e conciso ci sia riguardo
gli idiomi e le buone pratiche del Go. Inoltre, clickando sul nome di una
funzione [nella documentazione](http://golang.org/pkg/) compare il relativo
codice sorgente!

Un'altra ottima risorsa per imparare è [Go by example](https://gobyexample.com/).

Go Mobile aggiunge il supporto per lo sviluppo mobile (Android e iOS).
In questo modo è possibile scrivere un'app mobile nativa in Go, oppure
una libreria che contiene binding da un package scritto in Go, e che può
essere richiamata da Java(Android) e Objective-C(iOS). Visita la pagina di
[Go Mobile](https://github.com/golang/go/wiki/Mobile) per maggiori informazioni.
---
language: java
filename: LearnJava-it.java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Madison Dickson", "http://github.com/mix3d"]
translators:
    - ["Ivan Sala","http://github.com/slavni96"]
    - ["Tommaso Pifferi","http://github.com/neslinesli93"]
lang: it-it
---

Java è un linguaggio di programmazione orientato ad oggetti,
concorrente, basato su classi e adatto a svariati scopi. 
[Per saperne di più](http://docs.oracle.com/javase/tutorial/java/index.html)

```java
// I commenti su singola linea incominciano con //
/*
I commenti su piu' linee invece sono cosi'
*/
/**
I commenti per la documentazione JavaDoc si fanno cosi'.
Vengono usati per descrivere una classe o alcuni suoi attributi.
*/

// Per importare la classe ArrayList conenuta nel package java.util
import java.util.ArrayList;
// Per importare tutte le classi contenute nel package java.security
import java.security.*;

// Ogni file .java contiene una classe pubblica, con lo stesso nome del file
public class LearnJava {

    // Un programma deve avere un metodo main come punto di partenza.
    // Tuttavia si possono creare anche file senza main, che però
    // per essere usati devono essere richiamati da altri file.
    public static void main (String[] args) {

        // Per stampare a schermo si usa System.out.println
        System.out.println("Ciao Mondo!");
        System.out.println(
            "Intero [integer]: " + 10 +
            " Reale [double]: " + 3.14 +
            " Booleano [boolean]: " + true);

        // Se non si vuole andare a capo, si puo' usare System.out.print
        System.out.print("Ciao ");
        System.out.print("Mondo ");

        // Per stampare del testo formattato, si puo' usare System.out.printf
        System.out.printf("pi greco = %.5f", Math.PI); // => pi greco = 3.14159

        ///////////////////////////////////////
        // Variabili
        ///////////////////////////////////////

        /*
        *  Dichiarazione delle Variabili
        */
        // Per dichiarare una variabile basta fare <tipoDato> <nomeVariabile>
        int fooInt;
        // Per dichiarare piu' di una variabile dello lo stesso tipo si usa:
        // <tipoDato> <nomeVariabile1>, <nomeVariabile2>, <nomeVariabile3>
        int fooInt1, fooInt2, fooInt3;

        /*
        *  Inizializzazione delle Variabili
        */

        // Per inizializzare una variabile si usa
        // <tipoDato> <nomeVariabile> = <valore>
        int fooInt = 1;
        // Per inizializzare piu' di una variabile dello lo stesso tipo
        // si usa <tipoDato> <nomeVariabile1>, <nomeVariabile2>, <nomeVariabile3> = <valore>
        int fooInt1, fooInt2, fooInt3;
        fooInt1 = fooInt2 = fooInt3 = 1;

        /*
        *  Tipi di Variabili
        */
        // Byte - intero con segno a 8 bit (in complemento a 2)
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short - intero con segno a 16 bit (in complemento a 2)
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - intero con segno a 32 bit (in complemento a 2)
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int fooInt = 1;

        // Long - intero con segno a 64 bit (in complemento a 2)
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L viene usato per indicare che il valore e' di tipo Long;
        // altrimenti il valore viene considerato come intero.

        // Nota: Java non dispone di interi senza segno.

        // Float - Numero in virgola mobile a 32 bit con precisione singola (IEEE 754)
        // 2^-149 <= float <= (2-2^-23) * 2^127
        float fooFloat = 234.5f;
        // f o F indicano the la variabile e' di tipo float;
        // altrimenti il valore viene considerato come double.

        // Double - Numero in virgola mobile a 64 bit con precisione doppia (IEEE 754)
        // 2^-1074 <= x <= (2-2^-52) * 2^1023
        double fooDouble = 123.4;

        // Boolean - Puo' assumere il valore vero (true) o falso (false)
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - Un singolo carattere Unicode a 16-bit
        char fooChar = 'A';

        // Le variabili precedute da final possono essere inizializzate una volta sola,
        final int HOURS_I_WORK_PER_WEEK = 9001;
        // pero' e' possibile dichiararle e poi inizializzarle in un secondo momento.
        final double E;
        E = 2.71828;


        // BigInteger - Interi a precisione arbitraria
        //
        // BigInteger e' un tipo di dato che permette ai programmatori di
        // gestire interi piu' grandi di 64 bit. Internamente, le variabili
        // di tipo  BigInteger vengono memorizzate come un vettore di byte e 
        // vengono manipolate usando funzioni dentro la classe BigInteger.
        //
        // Una variabile di tipo BigInteger puo' essere inizializzata usando
        // un array di byte oppure una stringa.
        
        BigInteger fooBigInteger = new BigDecimal(fooByteArray);

        // BigDecimal - Numero con segno, immutabile, a precisione arbitraria
        //
        // Una variabile di tipo BigDecimal e' composta da due parti: un intero
        // a precisione arbitraria detto 'non scalato', e un intero a 32 bit
        // che rappresenta la 'scala', ovvero la potenza di 10 con cui
        // moltiplicare l'intero non scalato.
        //
        // I BigDecimal permettono un controllo completo sull'arrotondamento
        // dei numeri. Essi sono molto usati in ambito finanziario, nella
        // gestione delle valute, e in ogni altro posto in cui serve
        // precisione esatta.
        //
        // Le variabili di tipo BigDecimal possono essere inizializzate con un
        // int, long, double o String, oppure con un intero non scalato
        // (di tipo BigInteger) e una scala (int).

        BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt);



        // Stringhe
        String fooString = "Questa e' la mia stringa!";

        // \n e' un carattere di escape che rappresenta l'andare a capo
        String barString = "Stampare su una nuova riga?\nNessun problema!";
        // \t e' un carattere di escape che aggiunge un tab
        String bazString = "Vuoi aggiungere un tab?\tNessun problema!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // Vettori
        // La dimensione di un array deve essere decisa in fase di
        // istanziazione. Per dichiarare un array si puo' fare in due modi:
        // <tipoDato>[] <nomeVariabile> = new <tipoDato>[<dimensioneArray>];
        // <tipoDato> <nomeVariabile>[] = new <tipoDato>[<dimensioneArray>];
        int[] intArray = new int[10];
        String[] stringArray = new String[1];
        boolean boolArray[] = new boolean[100];

        // Un altro modo per dichiarare ed insieme inizializzare un vettore.
        int[] y = {9000, 1000, 1337};
        String names[] = {"Gianni", "Anna", "Luca", "Cristina"};
        boolean bools[] = new boolean[] {true, false, false};

        // Per accedere ad un elemento di un vettore
        System.out.println("intArray @ 0: " + intArray[0]);

        // I vettori non sono immutabili (ma la loro dimensione si!)
        // e gli indici partono da 0.
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // Ci sono altri tipo di dato interessanti.
        // ArrayList - Simili ai vettori, pero' offrono altre funzionalita',
        //             e la loro dimensione puo' essere modificata.
        // LinkedList - Si tratta di una lista linkata doppia, e come tale
        //              implementa tutte le operazioni del caso.
        // Map - Un insieme di oggetti che fa corrispondere delle chiavi
        //       a dei valori. Non permette l'inserimento di chiavi uguali.
        // HashMap - Questa classe usa una tabella di hash per implementare
        //           l'interfaccia di tipo Map. Questo permette di effettuare
        //           operazioni basilari, come inserimento e cancellazione,
        //           in tempo costante anche su insiemi molto grandi.

        ///////////////////////////////////////
        // Operatori
        ///////////////////////////////////////
        System.out.println("\n->Operatori");

        int i1 = 1, i2 = 2; // Dichiarazone multipla in contemporanea

        // L'aritmetica e' lineare.
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 
            // (con 0.5 arrotonda per difetto)

        // Modulo
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Operatori di confronto
        System.out.println("3 == 2? " + (3 == 2)); // => falso
        System.out.println("3 != 2? " + (3 != 2)); // => vero
        System.out.println("3 > 2? " + (3 > 2)); // => vero
        System.out.println("3 < 2? " + (3 < 2)); // => falso
        System.out.println("2 <= 2? " + (2 <= 2)); // => vero
        System.out.println("2 >= 2? " + (2 >= 2)); // => vero

        // Operatori binari orientati ai bit
        // effettuano le operazioni logiche confrontando, i bit degli operandi:
        /*
        ~       complemento 
        <<      shift sinistro con segno
        >>      shift destro con segno
        >>>     shift destro senza segno
        &       AND Binario Bitwise AND
        ^       OR Esclusivo
        |       OR Incusivo
        */

        // Incrementare e Decrementare
        int i = 0;
        System.out.println("\n->Incrementare/Decrementare");
        // Gli operatori ++ e -- incrementano e decrementano rispettivamente di 1.
        // Se posizionati prima della variabile, incrementano, quindi riportano.
        // Se si trovano dopo la variabile, riporano, e quindi incrementano. 
        System.out.println(i++); //i = 1, Stampa 0 (post-incremento)
        System.out.println(++i); //i = 2, Stampa 2 (pre-incremento)
        System.out.println(i--); //i = 1, Stampa 2 (post-decremento)
        System.out.println(--i); //i = 0, Stampa 0 (pre-decremento)

        ///////////////////////////////////////
        // Strutture di controllo
        ///////////////////////////////////////
        System.out.println("\n->Strutture di controllo");

        // La dichiarazione dell'If e'' C-like.
        int j = 10;
        if (j == 10){
            System.out.println("Io vengo stampato");
        } else if (j > 10) {
            System.out.println("Io no");
        } else {
            System.out.println("E io neppure");
        }

        // Struttura While
        int fooWhile = 0;
        while(fooWhile < 100)
        {
            //System.out.println(fooWhile);
            //Incrementa il contatore
            //Si ripete per 100 volte, fooWhile 0,1,2...99
            fooWhile++;
        }
        System.out.println("Valore di fooWhile: " + fooWhile);

        // Struttura Do While
        int fooDoWhile = 0;
        do
        {
            //System.out.println(fooDoWhile);
            //Incrementa il contaore
            //Si repete per 99 volte, fooDoWhile 0->99
            fooDoWhile++;
        }while(fooDoWhile < 100);
        System.out.println("Valore di fooWhile: " + fooDoWhile);

        // Struttura For
        int fooFor;
        //Struttura For => for(<Situazione iniziale>; <Condizione>; <passo>)
        for(fooFor=0; fooFor<10; fooFor++){
            //System.out.println(fooFor);
            //Itera 10 volte, fooFor 0->9
        }
        System.out.println("Valore di fooFor: " + fooFor);

        // Struttura For Each
        // Una iterazione automatica attraverso un array o una lista di oggetti
        int[] fooList = {1,2,3,4,5,6,7,8,9};
        //struttura for each => for(<oggetto> : <oggetto dell'attay>)
        //si legge: per ogni oggetto dell'array fai...
        //Nota: il tipo dell'oggetto deve essere uguale a quello dell'array

        for( int bar : fooList ){
            //System.out.println(bar);
            //Itera 9 volte e stampa 1-9 andando a capo.
        }

        // Struttura Switch Case
        // La struttura switch lavora con byte, short, char e int.
        // Se funziona con i char funzionera ovviamente anche con le stringhe.
        int mese = 3;
        String stringaMese;
        switch (mese){
            case 1:
                    stringaMese = "Genneio";
                    break;
            case 2:
                    stringaMese = "Febbraio";
                    break;
            case 3:
                    stringaMese = "Marzo";
                    break;
            default:
                    stringaMese = "Altri mesi";
                    break;
        }
        System.out.println("Risultato del costrutto switch: " + stringaMese);

        // Condizioni brevi
        // Si puo' usare l'operatore '?' per un rapido assegnamento
        // o per operazioni logiche.
        // Si legge: 
        // Se (condizione) e' vera, usa <primo valore>, altrimenti usa <secondo valore>
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println("Se la condizione e' vera stampa A: "+bar); 
        // Stampa A, perche' la condizione e' vera.


        /////////////////////////////////////////
        // Convertire i tipi di tati e Typcasting
        /////////////////////////////////////////

        // Convertire tipi di dati

        // Stringhe ad interi
        Integer.parseInt("123");//Riporta una versione intera di "123"

        // Interi a Stringhe
        Integer.toString(123);//Riporta la stringa "123"
        // Per altre conversioni guarda le seguenti classi
        // Double
        // Long
        // String

        // Typecasting
        // Vi sono molti dettagli che non si possono spiegare qui, 
        // java dispone di una ottima documentazione
        // Sentiti libero di leggerla
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Classi e funzioni
        ///////////////////////////////////////

        System.out.println("\n->Classi & Funzioni");

        // (Di seguito la definizione della classe Bicicletta)

        // Instanziare una nuova classe
        Bicicletta percorso = new Bicicletta();

        // Chiamare metodi
        percorso.accellera(3); // Si usano sempre metodi set... get...
        percorso.setCadenza(100);

        // toString riporta la rappresenzazione dell'oggetto 
        // come se fosse una stringa
        System.out.println("percorso info: " + percorso.toString());

    } // Fine metodo main
} // Fine classe LearnJava


// Si possono inculdere altre anche delle classi non pubbliche (private)
// oltre a quella pubblica principale, in un file .java

// Sintassi per dichiarare una classe:
// <public/private/protected> class <Nome classe>{
//    //dati, variabili, costruttori, funzioni, tutto qua.
//    //le funzioni sono chiamate come i metodi.
// }

class Bicicletta {

    // Variabili della bicicletta
    public int cadenza; 
      // Public: Puo' essere richiamato da qualsiasi classe
    private int velocita; 
      // Private: e'' accessibile solo dalla classe dove e'' stato inizializzato
    protected int ingranaggi; 
      // Protected: e'' visto sia dalla classe che dalle sottoclassi
    String nome; 
      // default: e'' accessibile sono all'interno dello stesso package

    // I costruttori vengono usati per creare variabili
    // Questo e'' un costruttore
    public Bicicletta() {
        ingranaggi = 1;
        cadenza = 50;
        velocita = 5;
        nome = "Bontrager";
    }

    // Questo e'' un costruttore che richiede parametri
    public Bicicletta(int cadenza, int velocita, int ingranaggi, String nome) {
        this.ingranaggi = ingranaggi;
        this.cadenza = cadenza;
        this.velocita = velocita;
        this.nome = nome;
    }

    // Sintassi delle funzioni:
    // <public/private/protected> <tipo di ritorino> <nome della funzione>(<parametri>)

    // Le classi in java spesso implementano delle funzioni o metodo
    // 'get...' o 'set...' 

    // Dichiarazione di un metodo
    // <scope> <tipo di ritorno> <nome del metodo>(<parametri>)
    public int getCandenza() {
        return cadenza;
    }

    // i medodi (void) non necessitano di riportare un valore
    public void setCadenza(int nuovoValore) {
        cadenza = nuovoValore;
    }

    public void setIngranaggi(int nuovoValore) {
        ingranaggi = nuovoValore;
    }

    public void accellera(int incrementa) {
        velocita += incrementa;
    }

    public void decellera(int decrementa) {
        velocita -= decrementa;
    }

    public void setNome(String nuovoNome) {
        nome = nuovoNome;
    }

    public String getNome() {
        return nome;
    }

    //Medoto per visualizzare gli attributi dell'oggetto
    @Override
    public String toString() {
        return "Ingranaggi: " + ingranaggi +
                " Cadenza: " + cadenza +
                " Velocita: " + velocita +
                " Nome: " + nome;
    }
} // Fine classe bicicletta

// PennyFarthing e'' una sottoclasse della bicicletta
class PennyFarthing extends Bicicletta {
    // (Sono quelle biciclette con un unica ruota enorme
    // Non hanno ingranaggi.)

    public PennyFarthing(int cadenzaIniziale, int velocitaIniziale){
        // Richiamo il costruttore del padre con super
        super(cadenzaIniziale, velocitaIniziale, 0, "PennyFarthing");
    }

    // Bisogna contrassegnre un medodo che si sta riscrivendo 
    // con una @annotazione
    // Per saperne di piu' sulle annotazioni
    // Vedi la guida: http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setIngranaggi(int ingranaggi) {
        ingranaggi = 0;
    }

}
/*
//Interfacce
//Sintassi per dichiarare una interfaccia
//<livello di accesso> interface <nome dell'interfaccia> extends <super-interfaccia> {
//    	//Costanti
//		//Dichiarazioni dei metodi
//}

//Esempi- Cibo:
interface Commestibile {
	public void mangia(); 
        //Ogni classe che implementa questa interfaccia
        //deve implementare questo metodo.
        }
interface Digeribile {
	public void digerisci();
}

//Possiamo quindi creare una classe che implementa entrambe le interfaccie
class Frutta implements Commestibile, Digestibile {
	public void mangia() {
		//...
	}

	public void digerisci() {
		//... 
	}
}

//In Java si puo' estendere solo una classe, ma si possono implementare 
//piu' interfaccie, per esempio:
class ClasseEsempio extends AltraClasse implements PrimaInterfaccia, SecondaInterfaccia {
	public void MetodoPrimaInterfaccia() {

	}

	public void MetodoSecondaInterfaccia() {

	}
}
*/
```
## Letture future

I link di seguito sono solo per capire l'argomento, cerca pure su Google degli esempi specifici


**Guida ufficiale di Oracle [solo in inglese]**:

* [Java Tutorial Trail from Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Java Access level modifiers](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Object-Oriented Programming Concepts](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Inheritance](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Abstraction](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Interfaces](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html)


**Tutorial Online [in inglese]**

* [Learneroo.com - Learn Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)


**Libri [in italiano]** :

* [Java la guida completa](http://www.amazon.it/Java-guida-completa-Herbert-Schildt/dp/8838667667/ref=sr_1_1?ie=UTF8&qid=1393422296&sr=8-1&keywords=java)

* [Thinking in java](http://www.amazon.it/Thinking-Java-1-Bruce-Eckel/dp/8871923030/ref=sr_1_8?ie=UTF8&qid=1393422296&sr=8-8&keywords=java)

* [Manuale di Java 7](http://www.amazon.com/gp/product/0071606300)
---
language: json
filename: learnjson-it.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
translators:
    - ["Robert Margelli", "http://github.com/sinkswim/"]
    - ["Christian Grasso", "http://chris54721.net"]
lang: it-it
---

JSON è un formato per l'interscambio di dati estremamente semplice, per cui questo sarà
con molta probabilità il più semplice Learn X in Y Minutes.

Nella sua forma più pura JSON non ha commenti, ma molti parser accettano
commenti in stile C (//, /\* \*/). Per lo scopo prefissato, tuttavia, tutto sarà
100% JSON valido. Fortunatamente, si spiega da sè.

I tipi supportati da JSON comprendono: numeri, stringhe, boolean, array, oggetti e null.
I browser supportati sono: Firefox (Mozilla) 3.5+, Internet Explorer 8+, Google Chrome,
Opera 10+, Safari 4+.
I file JSON sono salvati nel formato ".json". Il MIME type per documenti JSON è
"application/json". Gli svantaggi del JSON includono l'assenza di una definizione dei tipi
e di una sorta di [DTD](https://it.wikipedia.org/wiki/Document_Type_Definition).

```json
{
  "chiave": "valore",
  
  "chiavi": "devono sempre essere racchiuse tra doppi apici",
  "numeri": 0,
  "stringhe": "Ciaø, møndø. Tutti i caratteri Unicode sono permessi, insieme all'\"escaping\".",
  "ha booleani?": true,
  "il nulla": null,

  "numero grande": 1.2e+100,

  "oggetti": {
    "commento": "La maggior parte della tua struttura viene dagli oggetti.",

    "array": [0, 1, 2, 3, "Gli array possono contenere qualsiasi cosa.", 5],

    "un altro oggetto": {
      "commento": "Queste cose possono essere annidate, molto utile."
    }
  },

  "sciocchezze": [
    {
      "sorgenti di potassio": ["banane"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "stile alternativo": {
    "commento": "Guarda qua!"
  , "posizione della virgola": "non conta - se è prima della chiave successiva, allora è valida"
  , "un altro commento": "che bello"
  },

  "è stato molto breve": "Ed hai finito. Adesso sai tutto cio che JSON ha da offrire."
}
```
---
language: Logtalk
filename: learnlogtalk-it.lgt
contributors:
    - ["Paulo Moura", "http://github.com/pmoura"]
translators:
    - ["Ugo Chirico", "https://github.com/ugochirico"]
lang: it-it
---

Logtalk è un linguaggio di programmazione logica orientata agli oggetti che estende il linguaggio Prolog con le moderne tecniche di Object-Oriented Programming quali incapsulamento, ereditarietà e riutilizzo del codice, senza compromettere le caratteristiche di programmazione dichiarativa del Prolog. Logtalk è implementato in codice altamente portabile e utilizza i più moderni standard di conformità del Prolog rispetto al compilatore backend.

Per mantenere una dimensione ragionevole, questo tutorial presuppone necessariamente che il lettore abbia una conoscenza del linguaggio Prolog ed è inoltre focalizzato esclusivamente sulla descrizione delle caratteristiche object-oriented di Logtalk.

# Sintassi

Logtalk utilizza la sintassi standard del linguaggio Prolog con l'aggiunta di un paio di operatori e di alcune direttive per una curva di apprendimento morbida e per assicurare ampia portabilità. Una conseguenza importante è che il codice Prolog può essere facilmente incapsulato in oggetti con poche o nessuna modifica. Inoltre, Logtalk può interpretare come oggetti Logtalk, in modo trasparente, la maggior parte dei moduli Prolog già esistenti.

I principali operatori sono:

* `::/2` - per inviare un messaggio ad un oggetto
* `::/1` - per inviare un messaggio a se stesso _self_ (cioè all'oggetto che riceverà il messaggio)
* `^^/1` - _super_ per chiamare un predicato ereditato o importato

Alcune delle più importanti entità e direttive saranno introdotte nelle sezioni successive.

# Entità e Ruoli

Logtalk tratta gli oggetti, i protocolli e le categorie come entità di prima classe. I rapporti tra le entità definiscono i _patterns of code reuse_ ossia i modelli di riutilizzo del codice e i  _roles_ ossia i ruoli svolti da tali entità. Ad esempio, quando un oggetto istanzia un altro oggetto, il primo oggetto assume il ruolo di istanza e il secondo oggetto assume il ruolo di classe. Una relazione di tipo _extends_ tra due oggetti implica che entrambi gli oggetti svolgano il ruolo di prototipi, in cui uno di loro estende l'altro, che diventa quindi suo prototipo padre.

# Definizione di un oggetto

Un oggetto incapsula le dichiarazioni e le definizioni dei predicati. Gli oggetti possono essere creati in modo dinamico, ma di solito sono dichiarati come statici e definiti nel codice sorgente. Un singolo file sorgente può contenere un qualsiasi numero di definizioni di entità. Ecco un semplice oggetto `list` che definisce un membro pubblico `member/2`:

```logtalk
:- object(list).

	:- public(member/2).
	member(Head, [Head| _]).
	member(Head, [_| Tail]) :-
		member(Head, Tail).

:- end_object.
```

# Compilazione dei file sorgenti

Supponendo che il codice di cui sopra per l'oggetto `list` venga salvato in un file` list.lgt`, esso può essere compilato e caricato utilizzando il predicato predefiniti `logtalk_load/1` o la sua abbreviazione `{}/1`, con il percorso del file come argomento (l'estensione può essere omessa):

```logtalk
?- {list}.
yes
```

# Inviare un messaggio ad un oggetto

L'operatore infisso `::/2` è usato per inviare messaggi ad un oggetto. Analogamente al Prolog, è possibile fare backtracking per le soluzioni alternative:

```logtalk
?- list::member(X, [1,2,3]).
X = 1 ;
X = 2 ;
X = 3
yes
```

Analogamente alla programmazione object-oriented, logtalk consente anche l'Incapsulamento. 
Un predicato può essere dichiarata pubblico, protetto o privato. Può anche essere _local_ quando non esiste una direttiva specifica per esso all'interno dello scope. Per esempio:

```logtalk
:- object(scopes).

	:- private(bar/0).
	bar.

	local.

:- end_object.
```

Assumendo che l'oggetto è salvato nel file `scopes.lgt`:

```logtalk
?- {scopes}.
yes

?- catch(scopes::bar, Error, true).
Error = error(
	permission_error(access, private_predicate, bar/0),
	logtalk(scopes::bar, user)
)
yes

?- catch(scopes::local, Error, true).
Error = error(
	existence_error(predicate_declaration, local/0),
	logtalk(scopes::local, user)
)
yes
```

Quando il predicato in un messaggio non è noto per l'oggetto (il ruolo dell'oggetto determina le procedure di ricerca), si ha un errore.
Per esempio:

```logtalk
?- catch(scopes::unknown, Error, true).
Error = error(
	existence_error(predicate_declaration, unknown/0),
	logtalk(scopes::unknown, user)
)
yes
```

Un punto fondamentale da capire è che le direttive che specificano il predicato nello scope specificano la semantica di chiamata (_calling_) del predicato, e non la semantica di definizione (_definition_). Ad esempio, se un oggetto ha il ruolo di una classe e dichiara un predicato privato, tale predicato può essere definito nelle sue sottoclassi e nelle istanze * ma * può essere chiamato solo nelle sue istanza (_from_) dalla classe.

# Definizione e implementazione di un protocollo

Un Protocollo contiene le dichiarazioni dei predicati che possono essere implementati da un qualsivoglia numero di oggetti e categorie:

```logtalk
:- protocol(listp).

	:- public(member/2).

:- end_protocol.

:- object(list,
	implements(listp)).

	member(Head, [Head| _]).
	member(Head, [_| Tail]) :-
		member(Head, Tail).

:- end_object.
```

Lo scope dei predicati di un protocollo può essere ristretto usando implementazioni protected e private. Ad esempio:

```logtalk
:- object(stack,
	implements(private::listp)).

:- end_object.
```

Difatti, tutte le relazioni tra entità (nella direttiva di apertura di un entità) possono essere definite come public (default), protected, o private.

# Prototipi

Un oggetto senza una istanza o senza una relazione di specializzazione con un altro oggetto interpreta il ruolo di prototipo. Un prototipo può estendere un altro oggetto, il suo prototipo genitore.

```logtalk
% clyde, our prototypical elephant
:- object(clyde).

	:- public(color/1).
	color(grey).

	:- public(number_of_legs/1).
	number_of_legs(4).

:- end_object.

% fred, another elephant, is like clyde, except that he's white
:- object(fred,
	extends(clyde)).

	color(white).

:- end_object.
```

Per rispondere ad un messaggio inviato ad un oggetto che ha il ruolo di prototipo, si cerca prima una risposta nel prototipo stesso e se il prototipo non sa rispondere si passa all'eventuale prototipo genitore (se esiste):

```logtalk
?- fred::number_of_legs(N).
N = 4
yes

?- fred::color(C).
C = white
yes
```

Un messaggio è valido se il relativo predicato è dichiarato in un oggetto (e se il mittente è nel campo di applicazione), ma fallirà, piuttosto che lanciare un errore, se il predicato non è definito. Questa è chiamata la _closed-world assumption_. Ad esempio, si consideri il seguente oggetto, salvato in un file `foo.lgt`:

```logtalk
:- object(foo).

	:- public(bar/0).

:- end_object.
```

Caricando il file e cercando di chiamare il predicato `bar/0` questo fallisce come previsto. Si noti che ciò è diverso dal chiamare un predicato sconosciuto _unknown_, che invece genera un errore:

```logtalk
?- {foo}.
yes

?- foo::bar.
no

?- catch(foo::baz, Error, true).
Error = error(
	existence_error(predicate_declaration, baz/0),
	logtalk(foo::baz, user)
)
yes
```

# Classi e istanze

Per definire gli oggetti nei ruoli di classi e/o istanze, un oggetto deve avere almeno un istanziazione o una relazione di specializzazione con un altro oggetto. Gli oggetti che hanno il ruolo di meta-classi possono essere utilizzati quando abbiamo bisogno di usare una classe come se fosse un'istanza. Il seguente esempio mostra come creare dinamicamente nuovi oggetti in fase di esecuzione:

```logtalk
% a simple, generic, metaclass defining a new/2 predicate for its instances
:- object(metaclass,
	instantiates(metaclass)).

	:- public(new/2).
	new(Instance, Clauses) :-
		self(Class),
		create_object(Instance, [instantiates(Class)], [], Clauses).

:- end_object.

% a simple class defining age/1 and name/1 predicate for its instances
:- object(person,
	instantiates(metaclass)).

	:- public([
		age/1, name/1
	]).

	% a default value for age/1
	age(42).

:- end_object.

% a static instance of the class person
:- object(john,
	instantiates(person)).

	name(john).
	age(12).

:- end_object.
```

Nel rispondere ad un messaggio inviato ad un oggetto ha assunto il ruolo di istanza, tal messaggio viene convalidato partendo dalla sua classe e andando a ritroso nella gerarchia, se necessario, fino alle sue superclassi. Supponendo che il messaggio sia valido, allora si cerca una risposta a partire dall'istanza stessa:

```logtalk
?- person::new(Instance, [name(paulo)]).
Instance = o1
yes

?- o1::name(Name).
Name = paulo
yes

?- o1::age(Age).
Age = 42
yes

?- john::age(Age).
Age = 12
yes
```

# Categorie

Una categoria è un'unità atomica di codice riutilizzabile. Una categoria è usata per incapsulare una insieme coesivo (_cohesive_) di dichiarazioni e di definizioni di predicato ed è atta ad implementare una singola (_single_) funzionalità che può essere importata in qualsiasi oggetto. Una categoria può quindi essere concepita come il concetto duale di protocollo. Nel seguente esempio, si definiscono prima le categorie che rappresentano i motori di auto e poi si importano tali categorie negli oggetti auto:

```logtalk
% a protocol describing engine characteristics
:- protocol(carenginep).

	:- public([
		reference/1,
		capacity/1,
		cylinders/1,
		horsepower_rpm/2,
		bore_stroke/2,
		fuel/1
	]).

:- end_protocol.

% a typical engine defined as a category
:- category(classic,
	implements(carenginep)).

	reference('M180.940').
	capacity(2195).
	cylinders(6).
	horsepower_rpm(94, 4800).
	bore_stroke(80, 72.8).
	fuel(gasoline).

:- end_category.

% a souped up version of the previous engine
:- category(sport,
	extends(classic)).

	reference('M180.941').
	horsepower_rpm(HP, RPM) :-
		^^horsepower_rpm(ClassicHP, ClassicRPM),	% "super" call
		HP is truncate(ClassicHP*1.23),
		RPM is truncate(ClassicRPM*0.762).

:- end_category.

% with engines (and other components), we may start "assembling" some cars
:- object(sedan,
	imports(classic)).

:- end_object.

:- object(coupe,
	imports(sport)).

:- end_object.
```

Le Categorie sono compilate in modo indipendente e, quindi, consentono l'importazione di oggetti da aggiornare mediante il semplice aggiornamento delle categorie importate, senza richiedere pertanto la ricompilazione dell'oggetto. Le Categorie forniscono anche la _runtime transparency_,  cioè il protocollo della categoria si aggiunge al protocollo degli oggetti che importano tale categoria:

```logtalk
?- sedan::current_predicate(Predicate).
Predicate = reference/1 ;
Predicate = capacity/1 ;
Predicate = cylinders/1 ;
Predicate = horsepower_rpm/2 ;
Predicate = bore_stroke/2 ;
Predicate = fuel/1
yes
```

# Hot patching

Le categorie possono essere anche usate per modificare gli oggetti al volo (_hot-patch_). Una categoria può aggiungere nuovi predicati ad un oggetto e/o sostituire le definizioni dei predicati dell'oggetto. Ad esempio, si consideri il seguente oggetto:

```logtalk
:- object(buggy).

	:- public(p/0).
	p :- write(foo).

:- end_object.
```

Si supponga che l'oggetto stampi la stringa sbagliata quando riceve il messaggio `p/0`:

```logtalk
?- {buggy}.
yes

?- buggy::p.
foo
yes
```

Se il codice sorgente dell'oggetto non è disponibile e bisogna correggere l'applicazione che sta eseguendo il codice dell'oggetto, si può semplicemente definire una categoria che corregge il predicato non corretto:

```logtalk
:- category(patch,
	complements(buggy)).

	% fixed p/0 def
	p :- write(bar).

:- end_category.
```

Dopo la compilazione e il caricamento della categoria nell'applicazione in esecuzione si ottiene:

```logtalk
?- {patch}.
yes

?- buggy::p.
bar
yes
```

Poiché l'hot-patching pregiudica forzatamente l'incapsulamento, un apposito flag di compilazione `complementary` può essere impostato (a livello globale o per un singolo oggetto) per consentire, limitare o prevenire l'hot-patching.

# Oggetti Parametrici e Categorie

Gli oggetti e le categorie possono essere parametrizzati utilizzando come identificativo un compound-term al posto di un atomo. Oggetti e parametri di una categoria sono variabili logiche _logical variables_ condivise con tutti i predicati incapsulati. Ecco un esempio con cerchi geometrici:

```logtalk
:- object(circle(_Radius, _Color)).

	:- public([
		area/1, perimeter/1
	]).

	area(Area) :-
		parameter(1, Radius),
		Area is pi*Radius*Radius.

	perimeter(Perimeter) :-
		parameter(1, Radius),
		Perimeter is 2*pi*Radius.

:- end_object.
```

Oggetti parametrici possono essere utilizzati come qualsiasi altro oggetto e di solito forniscono i valori da assegnare ai parametri quando si invia un messaggio:

```logtalk
?- circle(1.23, blue)::area(Area).
Area = 4.75291
yes
```

Gli oggetti parametrici forniscono anche un modo semplice per associare un insieme di predicati con un semplice predicato Prolog. Fatti Prolog possono essere interpretati come oggetti proxy parametrici ( _parametric object proxies_) quando hanno lo stesso funtore e arietà degli identificatori di oggetti parametrici. Per lavorare con i proxy viene fornita una sintassi maneggevole. Per esempio, si prendano le seguenti clausole per il predicato `circle/2`:

```logtalk
circle(1.23, blue).
circle(3.71, yellow).
circle(0.39, green).
circle(5.74, black).
circle(8.32, cyan).
```

Con queste clausole, si può facilmente calcolare, ad esempio, un elenco con le aree di tutti i cerchi:

```logtalk
?- findall(Area, {circle(_, _)}::area(Area), Areas).
Areas = [4.75291, 43.2412, 0.477836, 103.508, 217.468]
yes
```

In pratica, il costrutto `{Goal}::Message` prova il goal `Goal`, instanziando le variabili interne e inviando un messaggio `Message` al termine risultante.

# Eventi and monitor

Logtalk supporta l'_event-driven programming_ mediante la definizione di eventi e di monitor. Un evento è semplicemente l'invio di un messaggio ad un oggetto. Un monitor è un gestore di un evento. L'evento (con l'invio di un messaggio) è un'attività atomica, ed è preceduta da un evento _before_ e da un evento _after_. Il monitor gestisce tali eventi mediante i predicati, `before/3` e `after/3`, che sono chiamati rispettivamente prima e dopo il verificarsi dell'evento. Un monitor può inoltre interrogare, registrare e cancellare un evento nel registro eventi a livello di sistema il quale che associa gli eventi con i monitor. Ad esempio, un semplice tracer per ogni messaggio inviato utilizzando il costrutto `::/2` può essere definito come:

```logtalk
:- object(tracer,
	implements(monitoring)).    % built-in protocol for event handlers

	:- initialization(define_events(_, _, _, _, tracer)).

	before(Object, Message, Sender) :-
		write('call: '), writeq(Object), write(' <-- '), writeq(Message),
		write(' from '), writeq(Sender), nl.

	after(Object, Message, Sender) :-
		write('exit: '), writeq(Object), write(' <-- '), writeq(Message),
		write(' from '), writeq(Sender), nl.

:- end_object.
```

Supponendo che l'oggetto `tracer` e l'oggetto `list` definito in precedenza siano stati già compilati e caricati, si possono osservare i gestori di eventi in azione durante l'invio di un messaggio:

```logtalk
?- list::member(X, [1,2,3]).

call: list <-- member(X, [1,2,3]) from user
exit: list <-- member(1, [1,2,3]) from user
X = 1 ;
exit: list <-- member(2, [1,2,3]) from user
X = 2 ;
exit: list <-- member(3, [1,2,3]) from user
X = 3
yes
```

Gli eventi possono essere impostati e cancellati dinamicamente in fase di esecuzione chiamando i predicati predefiniti `define_events/5` e` abolish_events/5` .

La programmazione event-driven può essere vista come una forma di _computational reflection_. Si noti però che gli eventi sono generati solo quando si utilizza il costrutto di controllo per l'invio di messaggi `::/2`.

# Espressioni lambda

Logtalk supporta anche le espressioni lambda. I parametri della espressioni lambda sono rappresentati mediante una lista con l'operatore infisso `(>>)/2` che collega i parametri alla relativa lambda espressione. Ecco alcuni semplici esempi di che usano i meta-predicati.


```logtalk
?- {library(metapredicates_loader)}.
yes

?- meta::map([X,Y]>>(Y is 2*X), [1,2,3], Ys).
Ys = [2,4,6]
yes
```

Logtalk supporta anche il _currying_:

```logtalk
?- meta::map([X]>>([Y]>>(Y is 2*X)), [1,2,3], Ys).
Ys = [2,4,6]
yes
```

Infine, le variabili libere Lambda possono essere espresso usando la sintassi estesa `{Free1, ...}/[Parameter1, ...]>>Lambda`.

# Macro

I Termini e goal nel file sorgente possono essere _estesi_ al momento della compilazione specificando una hook ad un oggetto (_hook object_) che definisce le regole di riscrittura dei termini e riscrittura dei quesiti. Ad esempio, si consideri il seguente oggetto semplice, salvato nel file `source.lgt`:

```logtalk
:- object(source).

	:- public(bar/1).
	bar(X) :- foo(X).

	foo(a). foo(b). foo(c).

:- end_object.
```

Si supponga il seguente hook all'oggetto, salvato nel file `my_macros.lgt`, che estende le clausole e chiama il predicato locale  `foo/1`:

```logtalk
:- object(my_macros,
	implements(expanding)).    % built-in protocol for expanding predicates

	term_expansion(foo(Char), baz(Code)) :-
		char_code(Char, Code). % standard built-in predicate

	goal_expansion(foo(X), baz(X)).

:- end_object.
```

Dopo aver caricato il file contenente la macro, si può espandere il nostro file sorgente usando il flag del compilatore `hook`:

```logtalk
?- logtalk_load(my_macros), logtalk_load(source, [hook(my_macros)]).
yes

?- source::bar(X).
X = 97 ;
X = 98 ;
X = 99
true
```

La libreria Logtalk fornisce infine il supporto per combinare hook agli oggetti utilizzando diversi modi (ad esempio, definendo una pipeline di espansioni).


# Maggiori informazioni

Visita il [Sito web di Logtalk (en)](http://logtalk.org) per maggiori informazioni.
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Jacopo Andrea Giola", "http://geekpanda.net"]
filename: markdown-it.md
lang: it-it
---

Markdown è stato creato da John Gruber nel 2004. Il suo scopo è quello di essere una sintassi facile da leggere e scrivere, e che può essere convertita in HTML (ad oggi anche in molti altri formati).

Mandate tutto il feedback che volete! / Sentitevi liberi di forkare o di mandare pull request!


```markdown
<!-- Markdown è un superset di HTML, quindi ogni file HTML è a sua volta un file Markdown valido. Questo significa che possiamo usare elementi di HTML in Markdown, come per esempio i commenti, e questi non saranno modificati dal parser di Markdown. State attenti però, se inserite un elemento HTML nel vostro file Markdown, non potrete usare la sua sintassi all'interno del contenuto dell'elemento. -->

<!-- L'implementazione di Markdown inoltre cambia da parser a parser. In questa guida cercheremo di indicare quando una feature è universale e quando sono specifiche ad un certo parser. -->

<!-- Titoli -->
<!-- Potete creare gli elementi HTML da <h1> ad <h6> facilmente, basta che inseriate un egual numero di caratteri cancelletto (#) prima del testo che volete all'interno dell'elemento -->
# Questo è un <h1>
## Questo è un <h2>
### Questo è un <h3>
#### Questo è un <h4>
##### Questo è un <h5>
###### Questo è un <h6>

<!-- Markdown inoltre fornisce due alternative per indicare gli elementi h1 e h2 -->
Questo è un h1
==============

Questo è un h2
--------------

<!-- Stili di testo semplici -->
<!-- Il testo può essere stilizzato in corsivo o grassetto usando markdown -->

*Questo testo è in corsivo.*
_Come pure questo._

**Questo testo è in grassetto.**
__Come pure questo.__

***Questo testo è stilizzato in entrabmi i modi.***
**_Come questo!_**
*__E questo!__*

<!-- In Github Flavored Markdown, che è utilizzato per renderizzare i file markdown su
Github, è presente anche lo stile barrato -->

~~Questo testo è barrato.~~

<!-- I paragrafi sono uno o più linee di testo addiacenti separate da una o più righe vuote. -->

Qeusto è un paragrafo. Sto scrivendo in un paragrafo, non è divertente?

Ora sono nel paragrafo 2.
Anche questa linea è nel paragrafo 2!


Qui siamo nel paragrafo 3!

<!-- Se volete inserire l'elemento HTML <br />, potete terminare la linea con due o più spazi e poi iniziare un nuovo paragrafo. -->

Questa frase finisce con due spazi (evidenziatemi per vederli).  

C'è un <br /> sopra di me!

<!-- Le citazioni sono semplici da inserire, basta usare il carattere >. -->

> Questa è una citazione. Potete
> mandare a capo manualmente le linee e inserire un `>` prima di ognuna, oppure potete usare una sola linea e lasciare che vada a capo automaticamente.
> Non c'è alcuna differenza, basta che iniziate ogni riga con `>`.

> Potete utilizzare anche più di un livello
>>  di indentazione!
> Quanto è comodo?

<!-- Liste -->
<!-- Le liste non ordinate possono essere inserite usando gli asterischi, il simbolo più o dei trattini -->

* Oggetto
* Oggetto
* Altro oggetto

oppure

+ Oggetto
+ Oggetto
+ Un altro oggetto

oppure

- Oggetto
- Oggetto
- Un ultimo oggetto

<!-- Le liste ordinate invece, sono inserite con un numero seguito da un punto. -->

1. Primo oggetto
2. Secondo oggetto
3. Terzo oggetto

<!-- Non dovete nemmeno mettere i numeri nell'ordine giusto, markdown li visualizzerà comunque nell'ordine corretto, anche se potrebbe non essere una buona idea. -->

1. Primo oggetto
1. Secondo oggetto
1. Terzo oggetto
<!-- (Questa lista verrà visualizzata esattamente come quella dell'esempio prima) -->

<!-- Potete inserire anche sotto liste -->

1. Primo oggetto
2. Secondo oggetto
3. Terzo oggetto
    * Sotto-oggetto
    * Sotto-oggetto
4. Quarto oggetto

<!-- Sono presenti anche le task list. In questo modo è possibile creare checkbox in HTML. -->

I box senza la 'x' sono checkbox HTML ancora da completare.
- [ ] Primo task da completare.
- [ ] Secondo task che deve essere completato.
Il box subito sotto è una checkbox HTML spuntata.
- [x] Questo task è stato completato.

<!-- Estratti di codice -->
<!-- Potete inserire un estratto di codice (che utilizza l'elemento <code>) indentando una linea con quattro spazi oppure con un carattere tab -->

    Questa è una linea di codice
    Come questa

<!-- Potete inoltre inserire un altro tab (o altri quattro spazi) per indentare il vostro codice -->

    my_array.each do |item|
        puts item
    end

<!-- Codice inline può essere inserito usando il carattere backtick ` -->

Giovanni non sapeva neppure a cosa servisse la funzione `go_to()`!

<!-- In Github Flavored Markdown, potete inoltre usare una sintassi speciale per il codice -->

\`\`\`ruby <!-- In realtà dovete rimuovere i backslash, usate solo ```ruby ! -->
def foobar
    puts "Hello world!"
end
\`\`\` <!-- Anche qui, niente backslash, solamente ``` -->

<!-- Se usate questa sintassi, il testo non richiederà di essere indentanto, inoltre Github userà la  syntax highlighting del linguaggio specificato dopo i ``` iniziali -->

<!-- Linea orizzontale (<hr />) -->
<!-- Le linee orizzontali sono inserite facilemtne usanto tre o più asterischi o trattini senza spazi consecutivi e senza spazi. -->

***
---
- - -
****************

<!-- Link -->
<!-- Una delle funzionalità migliori di markdown è la facilità con cui si possono inserire i link. Mettete il testo da visualizzare fra parentesi quadre [] seguite dall'url messo fra parentesi tonde () -->

[Cliccami!](http://test.com/)

<!-- Potete inoltre aggiungere al link un titolo mettendolo fra doppie apici dopo il link -->

[Cliccami!](http://test.com/ "Link a Test.com")

<!-- La sintassi funziona anche i path relativi. -->

[Vai a musica](/music/).

<!-- Markdown supporta inoltre anche la possibilità di aggiungere i link facendo riferimento ad altri punti del testo -->

[Apri questo link][link1] per più informazioni!
[Guarda anche questo link][foobar] se ti va.

[link1]: http://test.com/ "Bello!"
[foobar]: http://foobar.biz/ "Va bene!"

<!-- Il titolo può anche essere inserito in apici singoli o in parentesi, oppure omesso interamente. Il riferimento può essere inserito in un punto qualsiasi del vostro documento e l'identificativo del riferimento può essere lungo a piacere a patto che sia univoco. -->

<!-- Esiste anche un "identificativo implicito" che vi permette di usare il testo del link come id -->

[Questo][] è un link.

[Questo]: http://thisisalink.com/

<!-- Ma non è comunemente usato. -->

<!-- Immagini -->
<!-- Le immagini sono inserite come i link ma con un punto esclamativo inserito prima delle parentesi quadre! -->

![Qeusto è il testo alternativo per l'immagine](http://imgur.com/myimage.jpg "Il titolo opzionale")

<!-- E la modalità a riferimento funziona esattamente come ci si aspetta -->

![Questo è il testo alternativo.][myimage]

[myimage]: relative/urls/cool/image.jpg "Se vi serve un titolo, lo mettete qui"

<!-- Miscellanea -->
<!-- Auto link -->

<http://testwebsite.com/> è equivalente ad
[http://testwebsite.com/](http://testwebsite.com/)

<!-- Auto link per le email -->

<foo@bar.com>

<!-- Caratteri di escaping -->

Voglio inserire *questo testo circondato da asterischi* ma non voglio che venga renderizzato in corsivo, quindi lo inserirò così: \*questo testo è circondato da asterischi\*.

<!-- Combinazioni di tasti -->
<!-- In Github Flavored Markdown, potete utilizzare il tag <kbd> per raffigurare i tasti della tastiera -->

Il tuo computer è crashato? Prova a premere
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Canc</kbd>

<!-- Tabelle -->
<!-- Le tabelle sono disponibili solo in Github Flavored Markdown e sono leggeremente complesse, ma se proprio volete inserirle fate come segue: -->

| Col1                 | Col2     | Col3               |
| :------------------- | :------: | -----------------: |
| Allineato a sinistra | Centrato | Allineato a destra |
| blah                 | blah     | blah               |

<!-- oppure, per lo stesso risultato -->

Col 1 | Col2 | Col3
:-- | :-: | --:
È una cosa orrenda | fatela | finire in fretta

<!-- Finito! -->

```

Per altre informazioni, leggete il post ufficiale di John Gruber sulla sintassi [qui](http://daringfireball.net/projects/markdown/syntax) e il magnifico cheatsheet di Adam Pritchard [qui](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: Matlab
contributors:
    - ["mendozao", "http://github.com/mendozao"]
    - ["jamesscottbrown", "http://jamesscottbrown.com"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
translators:
    - ["Samuele Gallerani", "http://github.com/fontealpina"]
lang: it-it
filename: matlab-it.md
---

MATLAB sta per MATrix LABoratory ed è un potente linguaggio per il calcolo numerico comunemente usato in ingegneria e matematica.

```matlab
% I commenti iniziano con il segno percentuale.

%{
I commenti multilinea
assomigliano a
qualcosa
del genere
%}

% i comandi possono essere spezzati su più linee, usando '...':
 a = 1 + 2 + ...
 + 4

% i comandi possono essere passati al sistema operativo
!ping google.com

who % Mostra tutte le variabili in memoria
whos % Mostra tutte le variabili in memoria, con i loro tipi
clear % Cancella tutte le tue variabili dalla memoria
clear('A') % Cancella una particolare variabile
openvar('A') % Apre la variabile in un editor di variabile

clc % Cancella il contenuto della Command Window
diary % Attiva il log della Command Window su file
ctrl-c % Interrompe il calcolo corrente

edit('myfunction.m') % Apre la funzione/script nell'editor
type('myfunction.m') % Stampa il codice della funzione/script sulla Command Window

profile on 	% Attiva la profilazione del codice
profile off 	% Disattiva la profilazione del codice
profile viewer 	% Apre il profilatore

help comando 	% Mostra la documentazione di comando sulla Command Window
doc comando 	% Mostra la documentazione di comando sulla Help Window
lookfor comando % Cerca comando nella prima linea di commento di tutte le funzioni
lookfor comando -all % Cerca comando in tutte le funzioni


% Formattazione dell'output
format short 	% 4 decimali in un numero float
format long 	% 15 decimali
format bank 	% Solo due cifre decimali - per calcoli finaziari
fprintf('text') % Stampa "text" a terminale
disp('text') 	% Stampa "text" a terminale

% Variabili ed espressioni
miaVariabile = 4 % Il pannello Workspace mostra la nuova variabile creata
miaVariabile = 4; % Il punto e virgola evita che l'output venga stampato sulla Command Window
4 + 6  		% ans = 10
8 * myVariable 	% ans = 32
2 ^ 3 		% ans = 8
a = 2; b = 3;
c = exp(a)*sin(pi/2) % c = 7.3891

% La chiamata di funzioni può essere fatta in due modi differenti:
% Sintassi standard di una funzione:
load('myFile.mat', 'y') % argomenti tra parentesi, separati da virgole
% Sintassi di tipo comando:
load myFile.mat y 	% Non ci sono parentesi e gli argometi sono separati da spazi
% Notare la mancanza di apici nella sintassi di tipo comando: gli input sono sempre passati come
% testo letterale - non è possibile passare valori di variabili. Inoltre non può ricevere output:
[V,D] = eig(A);  % Questa non ha una forma equivalente con una sintassi di tipo comando
[~,D] = eig(A);  % Se si vuole solo D e non V



% Operatori logici
1 > 5 % ans = 0
10 >= 10 % ans = 1
3 ~= 4 % Not equal to -> ans = 1
3 == 3 % equal to -> ans = 1
3 > 1 && 4 > 1 % AND -> ans = 1
3 > 1 || 4 > 1 % OR -> ans = 1
~1 % NOT -> ans = 0

% Gli operatori logici possono essere applicati alle matrici:
A > 5
% Per ogni elemento, se la condizione è vera, quell'elemento vale 1 nella matrice risultante
A( A > 5 )
% Restituisce un vettore contenente gli elementi in A per cui la condizione è vera

% Stringhe
a = 'MyString'
length(a) % ans = 8
a(2) % ans = y
[a,a] % ans = MyStringMyString


% Celle
a = {'one', 'two', 'three'}
a(1) % ans = 'one' - ritorna una cella
char(a(1)) % ans = one - ritorna una stringa

% Strutture
A.b = {'one','two'};
A.c = [1 2];
A.d.e = false;

% Vettori
x = [4 32 53 7 1]
x(2) % ans = 32, gli indici in Matlab iniziano da 1, non da 0
x(2:3) % ans = 32 53
x(2:end) % ans = 32 53 7 1

x = [4; 32; 53; 7; 1] % Vettore colonna

x = [1:10] % x = 1 2 3 4 5 6 7 8 9 10

% Matrici
A = [1 2 3; 4 5 6; 7 8 9]
% Le righe sono separate da punto e virgola, mentre gli elementi sono separati da spazi
% A =

%     1     2     3
%     4     5     6
%     7     8     9

A(2,3) % ans = 6, A(row, column)
A(6) % ans = 8
% (implicitamente concatena le colonne in un vettore, e quindi gli indici sono riferiti al vettore)


A(2,3) = 42 % Aggiorna riga 2 colonna 3 con 42
% A =

%     1     2     3
%     4     5     42
%     7     8     9

A(2:3,2:3) % Crea una nuova matrice a partire da quella precedente
%ans =

%     5     42
%     8     9

A(:,1) % Tutte le righe nella colonna 1
%ans =

%     1
%     4
%     7

A(1,:) % Tutte le colonne in riga 1
%ans =

%     1     2     3

[A ; A] % Concatenazione di matrici (verticalmente)
%ans =

%     1     2     3
%     4     5    42
%     7     8     9
%     1     2     3
%     4     5    42
%     7     8     9

% è equivalente a
vertcat(A,A);


[A , A] % Concatenazione di matrici (orrizontalmente)

%ans =

%     1     2     3     1     2     3
%     4     5    42     4     5    42
%     7     8     9     7     8     9

% è equivalente a
horzcat(A,A);


A(:, [3 1 2]) % Ripristina le colonne della matrice originale
%ans =

%     3     1     2
%    42     4     5
%     9     7     8

size(A) % ans = 3 3

A(1, :) =[] % Rimuove la prima riga della matrice
A(:, 1) =[] % Rimuove la prima colonna della matrice

transpose(A) % Traspone la matrice, equivale a:
A one
ctranspose(A) % Trasposizione hermitiana della matrice
% (ovvero il complesso coniugato di ogni elemento della matrice trasposta)




% Aritmetica Elemento per Elemento vs. Artimetica Matriciale
% Gli operatori aritmetici da soli agliscono sull'intera matrice. Quando sono preceduti
% da un punto, allora agiscono su ogni elemento. Per esempio:
A * B % Moltiplicazione matriciale
A .* B % Moltiplica ogni elemento di A per il corrispondente elemento di B

% Ci sono diverse coppie di funzioni, in cui una agisce su ogni elemento, e
% l'altra (il cui nome termina con m) agisce sull'intera matrice.
exp(A) % Calcola l'esponenziale di ogni elemento
expm(A) % Calcola la matrice esponenziale
sqrt(A) % Calcola la radice quadrata di ogni elementotake the square root of each element
sqrtm(A) % Trova la matrice di cui A nè è la matrice quadrata


% Plot di grafici
x = 0:.10:2*pi; % Crea un vettore che inizia a 0 e termina 2*pi con incrementi di .1
y = sin(x);
plot(x,y)
xlabel('x axis')
ylabel('y axis')
title('Plot of y = sin(x)')
axis([0 2*pi -1 1]) % x range da 0 a 2*pi, y range da -1 a 1

plot(x,y1,'-',x,y2,'--',x,y3,':') % Per stampare più funzioni in unico plot
legend('Line 1 label', 'Line 2 label') % Aggiunge un etichetta con il nome delle curve

% Metodo alternativo per stampare funzioni multiple in un unico plot.
% mentre 'hold' è on, i comandi sono aggiunti al grafico esistene invece di sostituirlo
plot(x, y)
hold on
plot(x, z)
hold off

loglog(x, y) % Un plot di tipo log-log
semilogx(x, y) % Un plot con asse x logaritmico
semilogy(x, y) % Un plot con asse y logaritmico

fplot (@(x) x^2, [2,5]) % Stampa la funzione x^2 da x=2 a x=5

grid on % Mostra la griglia, disattivare con 'grid off'
axis square % Rende quadrata la regione individuata dagli assi
axis equal % Iposta l'aspetto del grafico in modo che le unità degli assi siano le stesse

scatter(x, y); % Scatter-plot
hist(x); % Istogramma

z = sin(x);
plot3(x,y,z); % Stampa una linea 3D

pcolor(A) % Heat-map di una matrice: stampa una griglia di rettangoli, colorati in base al valore
contour(A) % Contour plot di una matrice
mesh(A) % Stampa come una superfice di mesh

h = figure	% Crea un nuovo oggetto figura, con handle f
figure(h) % Rende la figura corrispondente al handle h la figura corrente
close(h) % Chiude la figura con handle h
close all % Chiude tutte le figure
close % Chiude la figura corrente

shg % Riutilizza una finestra grafica già esistente, o se necessario ne crea una nuova
clf clear % Pulisce la figura corrente, e resetta le proprietà della figura

% Le proprietà possono essere impostate e modificate attraverso l'handle della figura.
% Si può salvare l'handle della figura quando viene creata.
% La funzione gcf restituisce un handle alla figura attuale.
h = plot(x, y); % Si può salvare un handle della figura quando viene creata
set(h, 'Color', 'r')
% 'y' yellow; 'm' magenta, 'c' cyan, 'r' red, 'g' green, 'b' blue, 'w' white, 'k' black
set(h, 'LineStyle', '--')
 % '--' linea continua, '---' tratteggiata, ':' puntini, '-.' trattino-punto, 'none' nessuna linea
get(h, 'LineStyle')


% La funzione gca restituisce un handle degli assi della figura corrente
set(gca, 'XDir', 'reverse'); % Inverte la direzione dell'asse x

% Per creare una figura che contiene diverse sottofigure, usare subplot
subplot(2,3,1); % Seleziona la prima posizione in una griglia 2 per 3 di sottofigure
plot(x1); title('First Plot') % Stampa qualcosa in questa posizione
subplot(2,3,2); % Seleziona la seconda posizione nella griglia
plot(x2); title('Second Plot') % Stampa qualcosa in questa posizione


% Per usare funzioni o script, devono essere nel tuo path o nella directory corrente
path % Mostra il path corrente
addpath /path/to/dir % Aggiunge al path
rmpath /path/to/dir % Rimuove dal path
cd /path/to/move/into % Cambia directory


% Le variabili possono essere salvate in file .mat
save('myFileName.mat') % Salva le variabili nel tuo Workspace
load('myFileName.mat') % Carica variabili salvate nel tuo Workspace

% M-file Scripts
% I file di script sono file esterni che contengono una sequenza di istruzioni.
% Permettono di evitare di scrivere ripetutamente lo stesso codice nella Command Window
% Hanno estensione .m

% M-file Functions
% Come gli script, hanno la stessa estensione .m
% Ma possono accettare argomenti di input e restituire un output.
% Inoltre, hanno un proprio workspace (differente scope delle variabili).
% Il nome della funzione dovrebbe coincidere con il nome del file (quindi salva questo esempio come double_input.m).
% 'help double_input.m' restituisce i commenti sotto alla linea iniziale della funzione
function output = double_input(x)
	%double_input(x) restituisce il doppio del valore di x
	output = 2*x;
end
double_input(6) % ans = 12


% Si possono anche avere sottofunzioni e funzioni annidate.
% Le sottofunzioni sono nello stesso file della funzione primaria, e possono solo essere
% chiamate da funzioni nello stesso file. Le funzioni annidate sono definite dentro ad altre
% funzioni, e hanno accesso ad entrambi i workspace.

% Se si vuole creare una funzione senza creare un nuovo file si può usare una
% funzione anonima. Utile quando si vuole definire rapidamente una funzione da passare ad
% un'altra funzione (es. stampa con fplot, valutare un integrale indefinito
% con quad, trovare le radici con fzenzro, o trovare il minimo con fminsearch).
% Esempio che restituisce il quadrato del proprio input, assegnato all'handle sqr:
sqr = @(x) x.^2;
sqr(10) % ans = 100
doc function_handle % scopri di più

% Input dell'utente
a = input('Enter the value: ')

% Ferma l'esecuzione del file e cede il controllo alla tastiera: l'utente può esaminare
% o cambiare variabili. Digita 'return' per continuare l'esecuzione, o 'dbquit' per uscire
keyboard

% Importarare dati (anche xlsread/importdata/imread per excel/CSV/image file)
fopen(filename)

% Output
disp(a) % Stampa il valore della variabile a
disp('Hello World') % Stampa una stringa
fprintf % Stampa sulla Command Window con più controllo

% Istruzioni condizionali (le parentesi sono opzionali, ma un buon stile)
if (a > 15)
	disp('Maggiore di 15')
elseif (a == 23)
	disp('a è 23')
else
	disp('nessuna condizione verificata')
end

% Cicli
% NB. Ciclare su elementi di vettori/matrici è lento!
% Dove possibile, usa funzioni che agiscono sull'intero vettore/matrice
for k = 1:5
	disp(k)
end

k = 0;
while (k < 5)
	k = k + 1;
end

% Misurare la durata dell'esecuzione del codice: 'toc' stampa il tempo trascorso da quando 'tic' è stato chiamato
tic
A = rand(1000);
A*A*A*A*A*A*A;
toc

% Connessione a un Database MySQL
dbname = 'database_name';
username = 'root';
password = 'root';
driver = 'com.mysql.jdbc.Driver';
dburl = ['jdbc:mysql://localhost:8889/' dbname];
javaclasspath('mysql-connector-java-5.1.xx-bin.jar');
% xx dipende dalla versione, download disponibile all'indirizzo http://dev.mysql.com/downloads/connector/j/
conn = database(dbname, username, password, driver, dburl);
sql = ['SELECT * from table_name where id = 22'] % Esempio istruzione sql
a = fetch(conn, sql) % conterra i tuoi dati


% Funzioni matematiche comuni
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
exp(x)
sqrt(x)
log(x)
log10(x)
abs(x)
min(x)
max(x)
ceil(x)
floor(x)
round(x)
rem(x)
rand % Numeri pseudocasuali uniformemente distribuiti
randi % Numeri interi pseudocasuali uniformemente distrubuiti
randn % Numeri pseudocasuali distrbuiti normalmente

% Costanti comuni
pi
NaN
inf

% Risolvere equazioni matriciali
% Gli operatori \ e / sono equivalenti alle funzioni mldivide e mrdivide
x=A\b % Risolve Ax=b. Più veloce e più accurato numericamente rispetto ad usare inv(A)*b.
x=b/A % Risolve xA=b

inv(A) % Calcola la matrice inversa
pinv(A) % Calcola la matrice pseudo-inversa

% Funzioni comuni su matrici
zeros(m,n) % Matrice m x n di zeri
ones(m,n) % Matrice m x n di uni
diag(A) % Estrae gli elementi della diagonale della matrice A
diag(x) % Costruisce una matrice con elementi diagonali uguali agli elementi di x, e zero negli altri elementi
eye(m,n) % Matrice identità
linspace(x1, x2, n) % Ritorna n punti equamente distanziati, con minimo x1 e massimo x2
inv(A) % Matrice inversa di A
det(A) % Determinante di A
eig(A) % Autovalori e autovettori di A
trace(A) % Traccia della matrice -  equivalente a sum(diag(A))
isempty(A) % Verifica se l'array è vuoto
all(A) % Verifica se tutti gli elementi sono nonzero o veri
any(A) % Verifica se almento un elemento è nonzero o vero
isequal(A, B) % Verifica l'uguaglianza di due array
numel(A) % Numero di elementi nella matrice
triu(x) % Ritorna la parte triangolare superiore di x
tril(x) % Ritorna la parte triangolare inferiore di x
cross(A,B) %  Ritorna il prodotto vettoriale dei vettori A e B
dot(A,B) % Ritorna il prodotto scalare di due vettori (devono avere la stessa lunghezza)
transpose(A) % Ritorna la trasposta di A
fliplr(A) % Capovolge la matrice da sinistra a destra
flipud(A) % Capovolge la matrice da sopra a sotto

% Fattorizzazione delle matrici
[L, U, P] = lu(A) % Decomposizione LU: PA = LU, L è il triangolo inferiore, U è il triangolo superiore, P è la matrice di permutazione
[P, D] = eig(A) % Auto-decomposizione: AP = PD, le colonne di P sono autovettori e gli elementi sulle diagonali di D sono autovalori
[U,S,V] = svd(X) % SVD: XV = US, U e V sono matrici unitarie, S ha gli elementi della diagonale non negativi in ordine decrescente

% Funzioni comuni su vettori
max     % elemento più grande
min     % elemento più piccolo
length  % lunghezza del vettore
sort    % ordina in modo crescente
sum     % somma degli elementi
prod    % prodotto degli elementi
mode	% valore moda
median  % valore mediano
mean    % valore medio
std     % deviazione standard
perms(x) % lista tutte le permutazioni di elementi di x


% Classi
% Matlab supporta la programmazione orientata agli oggetti.
% La classe deve essere messa in un file con lo stesso nome della classe e estensione .m
% Per iniziare, creiamo una semplice classe per memorizzare waypoint GPS
% Inizio WaypointClass.m
classdef WaypointClass % Il nome della classe.
  properties % Le proprietà della classe funzionano come Strutture
    latitude
    longitude
  end
  methods
    % Questo metodo che ha lo stesso nome della classe è il costruttore
    function obj = WaypointClass(lat, lon)
      obj.latitude = lat;
      obj.longitude = lon;
    end

    % Altre funzioni che usano l'oggetto Waypoint
    function r = multiplyLatBy(obj, n)
      r = n*[obj.latitude];
    end

    % Se si vuole aggiungere due oggetti Waypoint insieme senza chiamare
    % una funzione speciale si può sovradefinire una funzione aritmetica di Matlab come questa:
    function r = plus(o1,o2)
      r = WaypointClass([o1.latitude] +[o2.latitude], ...
                        [o1.longitude]+[o2.longitude]);
    end
  end
end
% End WaypointClass.m

% Si può creare un oggetto della classe usando un costruttore
a = WaypointClass(45.0, 45.0)

% Le proprietà della classe si comportano esattamente come una Struttura Matlab.
a.latitude = 70.0
a.longitude = 25.0

% I metodi possono essere chiamati allo stesso modo delle funzioni
ans = multiplyLatBy(a,3)

% Il metodo può anche essere chiamato usando una notazione con punto. In questo caso, l'oggetto
% non necessita di essere passato al metodo.
ans = a.multiplyLatBy(a,1/3)

% Le funzioni Matlab possono essere sovradefinite per gestire oggetti.
% Nel metodo sopra, è stato sovradefinito come Matlab gestisce
% l'addizione di due oggetti Waypoint.
b = WaypointClass(15.0, 32.0)
c = a + b

```

## Di più su Matlab

* Sito ufficiale [http://http://www.mathworks.com/products/matlab/](http://www.mathworks.com/products/matlab/)
* Forum ufficiale di MATLAB: [http://www.mathworks.com/matlabcentral/answers/](http://www.mathworks.com/matlabcentral/answers/)
---
language: python
filename: learnpython-it.py
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
    - ["Amin Bandali", "http://aminbandali.com"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["evuez", "http://github.com/evuez"]
translators:
    - ["Ale46", "http://github.com/Ale46/"]
    - ["Tommaso Pifferi", "http://github.com/neslinesli93/"]
lang: it-it
---
Python è stato creato da Guido Van Rossum agli inizi degli anni 90. Oggi è uno dei più popolari
linguaggi esistenti. Mi sono innamorato di Python per la sua chiarezza sintattica. E' sostanzialmente
pseudocodice eseguibile.

Feedback sono altamente apprezzati! Potete contattarmi su [@louiedinh](http://twitter.com/louiedinh) oppure [at] [google's email service]

Nota: questo articolo è riferito a Python 2.7 in modo specifico, ma dovrebbe andar
bene anche per Python 2.x. Python 2.7 sta raggiungendo il "fine vita", ovvero non sarà
più supportato nel 2020. Quindi è consigliato imparare Python utilizzando Python 3.
Per maggiori informazioni su Python 3.x, dai un'occhiata al [tutorial di Python 3](http://learnxinyminutes.com/docs/python3/).

E' possibile anche scrivere codice compatibile sia con Python 2.7 che con Python 3.x,
utilizzando [il modulo `__future__`](https://docs.python.org/2/library/__future__.html) di Python.
Il modulo `__future__` permette di scrivere codice in Python 3, che può essere eseguito
utilizzando Python 2: cosa aspetti a vedere il tutorial di Python 3?

```python

# I commenti su una sola linea iniziano con un cancelletto

""" Più stringhe possono essere scritte
    usando tre ", e sono spesso usate
    come commenti
"""

####################################################
## 1. Tipi di dati primitivi ed Operatori
####################################################

# Hai i numeri
3  # => 3

# La matematica è quello che vi aspettereste
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20
35 / 5  # => 7

# La divisione è un po' complicata. E' una divisione fra interi in cui viene 
# restituito in automatico il risultato intero.
5 / 2  # => 2

# Per le divisioni con la virgola abbiamo bisogno di parlare delle variabili floats.
2.0     # Questo è un float
11.0 / 4.0  # => 2.75 ahhh...molto meglio

# Il risultato di una divisione fra interi troncati positivi e negativi
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # funziona anche per i floats
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# E' possibile importare il modulo "division" (vedi la sezione 6 di questa guida, Moduli)
# per effettuare la divisione normale usando solo '/'.
from __future__ import division
11/4    # => 2.75  ...divisione normale
11//4   # => 2 ...divisione troncata

# Operazione Modulo
7 % 3 # => 1

# Elevamento a potenza (x alla y-esima potenza)
2**4 # => 16

# Forzare le precedenze con le parentesi
(1 + 3) * 2  # => 8

# Operatori Booleani
# Nota "and" e "or" sono case-sensitive
True and False #=> False
False or True #=> True

# Note sull'uso di operatori Bool con interi
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# nega con not
not True  # => False
not False  # => True

# Uguaglianza è ==
1 == 1  # => True
2 == 1  # => False

# Disuguaglianza è !=
1 != 1  # => False
2 != 1  # => True

# Altri confronti
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# I confronti possono essere concatenati!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# Le stringhe sono create con " o '
"Questa è una stringa."
'Anche questa è una stringa.'

# Anche le stringhe possono essere sommate!
"Ciao " + "mondo!"  # => Ciao mondo!"
# Le stringhe possono essere sommate anche senza '+'
"Ciao " "mondo!"  # => Ciao mondo!"

# ... oppure moltiplicate
"Hello" * 3  # => "HelloHelloHello"

# Una stringa può essere considerata come una lista di caratteri
"Questa è una stringa"[0]  # => 'Q'

# Per sapere la lunghezza di una stringa
len("Questa è una stringa")  # => 20

# Formattazione delle stringhe con %
# Anche se l'operatore % per le stringe sarà deprecato con Python 3.1, e verrà rimosso
# successivamente, può comunque essere utile sapere come funziona
x = 'mela'
y = 'limone'
z = "La cesta contiene una %s e un %s" % (x,y)

# Un nuovo modo per fomattare le stringhe è il metodo format.
# Questo metodo è quello consigliato
"{} è un {}".format("Questo", "test")
"{0} possono essere {1}".format("le stringhe", "formattate")
# Puoi usare delle parole chiave se non vuoi contare
"{nome} vuole mangiare {cibo}".format(nome="Bob", cibo="lasagna")

# None è un oggetto
None  # => None

# Non usare il simbolo di uguaglianza "==" per comparare oggetti a None
# Usa "is" invece
"etc" is None  # => False
None is None  # => True

# L'operatore 'is' testa l'identità di un oggetto. Questo non è
# molto utile quando non hai a che fare con valori primitivi, ma lo è
# quando hai a che fare con oggetti.
 
# Qualunque oggetto può essere usato nei test booleani
# I seguenti valori sono considerati falsi:
#     - None
#     - Lo zero, come qualunque tipo numerico (quindi 0, 0L, 0.0, 0.j)
#     - Sequenze vuote (come '', (), [])
#     - Contenitori vuoti (tipo {}, set())
#     - Istanze di classi definite dall'utente, che soddisfano certi criteri
#       vedi: https://docs.python.org/2/reference/datamodel.html#object.__nonzero__
#       
# Tutti gli altri valori sono considerati veri: la funzione bool() usata su di loro, ritorna True.
bool(0)  # => False
bool("")  # => False


####################################################
## 2. Variabili e Collections
####################################################

# Python ha una funzione di stampa
print "Sono Python. Piacere di conoscerti!" # => Sono Python. Piacere di conoscerti!

# Un modo semplice per ricevere dati in input dalla riga di comando
variabile_stringa_input = raw_input("Inserisci del testo: ") # Ritorna i dati letti come stringa
variabile_input = input("Inserisci del testo: ") # Interpreta i dati letti come codice python
# Attenzione: bisogna stare attenti quando si usa input()
# Nota: In python 3, input() è deprecato, e raw_input() si chiama input()

# Non c'è bisogno di dichiarare una variabile per assegnarle un valore
una_variabile = 5    # Convenzionalmente si usa caratteri_minuscoli_con_underscores
una_variabile  # => 5

# Accedendo ad una variabile non precedentemente assegnata genera un'eccezione.
# Dai un'occhiata al Control Flow per imparare di più su come gestire le eccezioni.
un_altra_variabile  # Genera un errore di nome

# if può essere usato come un'espressione
# E' l'equivalente dell'operatore ternario in C
"yahoo!" if 3 > 2 else 2  # => "yahoo!"

# Liste immagazzinano sequenze
li = []
# Puoi partire con una lista pre-riempita
altra_li = [4, 5, 6]

# Aggiungi cose alla fine di una lista con append
li.append(1)    # li ora è [1]
li.append(2)    # li ora è [1, 2]
li.append(4)    # li ora è [1, 2, 4]
li.append(3)    # li ora è [1, 2, 4, 3]
# Rimuovi dalla fine della lista con pop
li.pop()        # => 3 e li ora è [1, 2, 4]
# Rimettiamolo a posto
li.append(3)    # li ora è [1, 2, 4, 3] di nuovo.

# Accedi ad una lista come faresti con un array
li[0]  # => 1
# Assegna nuovo valore agli indici che sono già stati inizializzati con =
li[0] = 42
li[0]  # => 42
li[0] = 1  # Nota: è resettato al valore iniziale
# Guarda l'ultimo elemento
li[-1]  # => 3

# Guardare al di fuori dei limiti è un IndexError
li[4]  # Genera IndexError

# Puoi guardare gli intervalli con la sintassi slice (a fetta).
# (E' un intervallo chiuso/aperto per voi tipi matematici.)
li[1:3]  # => [2, 4]
# Ometti l'inizio
li[2:]  # => [4, 3]
# Ometti la fine
li[:3]  # => [1, 2, 4]
# Seleziona ogni seconda voce
li[::2]   # =>[1, 4]
# Copia al contrario della lista
li[::-1]   # => [3, 4, 2, 1]
# Usa combinazioni per fare slices avanzate
# li[inizio:fine:passo]

# Rimuovi arbitrariamente elementi da una lista con "del"
del li[2]   # li è ora [1, 2, 3]
# Puoi sommare le liste
li + altra_li   # => [1, 2, 3, 4, 5, 6]
# Nota: i valori per li ed altra_li non sono modificati.

# Concatena liste con "extend()"
li.extend(altra_li)   # Ora li è [1, 2, 3, 4, 5, 6]

# Rimuove la prima occorrenza di un elemento
li.remove(2)  # Ora li è [1, 3, 4, 5, 6]
li.remove(2)  # Emette un ValueError, poichè 2 non è contenuto nella lista

# Inserisce un elemento all'indice specificato
li.insert(1, 2)  # li è di nuovo [1, 2, 3, 4, 5, 6]

# Ritorna l'indice della prima occorrenza dell'elemento fornito
li.index(2)  # => 1
li.index(7)  # Emette un ValueError, poichè 7 non è contenuto nella lista

# Controlla l'esistenza di un valore in una lista con "in"
1 in li   # => True

# Esamina la lunghezza con "len()"
len(li)   # => 6


# Tuple sono come le liste ma immutabili.
tup = (1, 2, 3)
tup[0]   # => 1
tup[0] = 3  # Genera un TypeError

# Puoi fare tutte queste cose da lista anche sulle tuple
len(tup)   # => 3
tup + (4, 5, 6)   # => (1, 2, 3, 4, 5, 6)
tup[:2]   # => (1, 2)
2 in tup   # => True

# Puoi scompattare le tuple (o liste) in variabili
a, b, c = (1, 2, 3)     # a è ora 1, b è ora 2 and c è ora 3
d, e, f = 4, 5, 6       # puoi anche omettere le parentesi
# Le tuple sono create di default se non usi le parentesi
g = 4, 5, 6             # => (4, 5, 6)
# Guarda come è facile scambiare due valori
e, d = d, e     # d è ora 5 ed e è ora 4


# Dizionari immagazzinano mappature
empty_dict = {}
# Questo è un dizionario pre-riempito
filled_dict = {"uno": 1, "due": 2, "tre": 3}

# Accedi ai valori con []
filled_dict["uno"]   # => 1

# Ottieni tutte le chiavi come una lista con "keys()"
filled_dict.keys()   # => ["tre", "due", "uno"]
# Nota - Nei dizionari l'ordine delle chiavi non è garantito.
# Il tuo risultato potrebbe non essere uguale a questo.

# Ottieni tutt i valori come una lista con "values()"
filled_dict.values()   # => [3, 2, 1]
# Nota - Come sopra riguardo l'ordinamento delle chiavi.

# Ottieni tutte le coppie chiave-valore, sotto forma di lista di tuple, utilizzando "items()"
filled_dicts.items()    # => [("uno", 1), ("due", 2), ("tre", 3)]

# Controlla l'esistenza delle chiavi in un dizionario con "in"
"uno" in filled_dict   # => True
1 in filled_dict   # => False

# Cercando una chiave non esistente è un KeyError
filled_dict["quattro"]   # KeyError

# Usa il metodo "get()" per evitare KeyError
filled_dict.get("uno")   # => 1
filled_dict.get("quattro")   # => None
# Il metodo get supporta un argomento di default quando il valore è mancante
filled_dict.get("uno", 4)   # => 1
filled_dict.get("quattro", 4)   # => 4
# nota che filled_dict.get("quattro") è ancora => None
# (get non imposta il valore nel dizionario)

# imposta il valore di una chiave con una sintassi simile alle liste
filled_dict["quattro"] = 4  # ora, filled_dict["quattro"] => 4

# "setdefault()" aggiunge al dizionario solo se la chiave data non è presente
filled_dict.setdefault("five", 5)  # filled_dict["five"] è impostato a 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] è ancora 5


# Sets immagazzina ... sets (che sono come le liste, ma non possono contenere doppioni)
empty_set = set()
# Inizializza un "set()" con un po' di valori
some_set = set([1, 2, 2, 3, 4])   # some_set è ora set([1, 2, 3, 4])

# l'ordine non è garantito, anche se a volta può sembrare ordinato
another_set = set([4, 3, 2, 2, 1])  # another_set è ora set([1, 2, 3, 4])

# Da Python 2.7, {} può essere usato per dichiarare un set
filled_set = {1, 2, 2, 3, 4}   # => {1, 2, 3, 4}

# Aggiungere elementi ad un set
filled_set.add(5)   # filled_set è ora {1, 2, 3, 4, 5}

# Fai intersezioni su un set con &
other_set = {3, 4, 5, 6}
filled_set & other_set   # => {3, 4, 5}

# Fai unioni su set con |
filled_set | other_set   # => {1, 2, 3, 4, 5, 6}

# Fai differenze su set con -
{1, 2, 3, 4} - {2, 3, 5}   # => {1, 4}

# Effettua la differenza simmetrica con ^
{1, 2, 3, 4} ^ {2, 3, 5}  # => {1, 4, 5}

# Controlla se il set a sinistra contiene quello a destra
{1, 2} >= {1, 2, 3} # => False

# Controlla se il set a sinistra è un sottoinsieme di quello a destra
{1, 2} <= {1, 2, 3} # => True

# Controlla l'esistenza in un set con in
2 in filled_set   # => True
10 in filled_set   # => False


####################################################
## 3. Control Flow
####################################################

# Dichiariamo una variabile
some_var = 5

# Questo è un controllo if. L'indentazione è molto importante in python!
# stampa "some_var è più piccola di 10"
if some_var > 10:
    print "some_var è decisamente più grande di 10."
elif some_var < 10:    # Questa clausola elif è opzionale.
    print "some_var è più piccola di 10."
else:           # Anche questo è opzionale.
    print "some_var è precisamente 10."


"""
I cicli for iterano sulle liste
stampa:
    cane è un mammifero
    gatto è un mammifero
    topo è un mammifero
"""
for animale in ["cane", "gatto", "topo"]:
    # Puoi usare {0} per interpolare le stringhe formattate. (Vedi di seguito.)
    print "{0} è un mammifero".format(animale)

"""
"range(numero)" restituisce una lista di numeri
da zero al numero dato
stampa:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
"range(lower, upper)" restituisce una lista di numeri
dal più piccolo (lower) al più grande (upper)
stampa:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print i

"""
I cicli while vengono eseguiti finchè una condizione viene a mancare
stampa:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Forma compatta per x = x + 1

# Gestisci le eccezioni con un blocco try/except

# Funziona da Python 2.6 in su:
try:
    # Usa "raise" per generare un errore
    raise IndexError("Questo è un errore di indice")
except IndexError as e:
    pass    # Pass è solo una non-operazione. Solitamente vorrai fare un recupero.
except (TypeError, NameError):
    pass    # Eccezioni multiple possono essere gestite tutte insieme, se necessario.
else:   # Clausola opzionale al blocco try/except. Deve seguire tutti i blocchi except
    print "Tutto ok!"   # Viene eseguita solo se il codice dentro try non genera eccezioni
finally: #  Eseguito sempre
    print "Possiamo liberare risorse qui"

# Invece di try/finally per liberare risorse puoi usare il metodo with
with open("myfile.txt") as f:
    for line in f:
        print line

####################################################
## 4. Funzioni
####################################################

# Usa "def" per creare nuove funzioni
def aggiungi(x, y):
    print "x è {0} e y è {1}".format(x, y)
    return x + y    # Restituisce valori con il metodo return

# Chiamare funzioni con parametri
aggiungi(5, 6)   # => stampa "x è 5 e y è 6" e restituisce 11

# Un altro modo per chiamare funzioni  è con parole chiave come argomenti
aggiungi(y=6, x=5)   # Le parole chiave come argomenti possono arrivare in ogni ordine.


# Puoi definire funzioni che accettano un numero variabile di argomenti posizionali
# che verranno interpretati come tuple usando il *
def varargs(*args):
    return args

varargs(1, 2, 3)   # => (1, 2, 3)


# Puoi definire funzioni che accettano un numero variabile di parole chiave
# come argomento, che saranno interpretati come un dizionario usando **
def keyword_args(**kwargs):
    return kwargs

# Chiamiamola per vedere cosa succede
keyword_args(big="foot", loch="ness")   # => {"big": "foot", "loch": "ness"}


# Puoi farle entrambi in una volta, se ti va
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) stampa:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Quando chiami funzioni, puoi fare l'opposto di args/kwargs!
# Usa * per sviluppare gli argomenti posizionale ed usa ** per espandere gli argomenti parola chiave
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)   # equivalente a foo(1, 2, 3, 4)
all_the_args(**kwargs)   # equivalente a foo(a=3, b=4)
all_the_args(*args, **kwargs)   # equivalente a foo(1, 2, 3, 4, a=3, b=4)

# puoi passare args e kwargs insieme alle altre funzioni che accettano args/kwargs
# sviluppandoli, rispettivamente, con * e **
def pass_all_the_args(*args, **kwargs):
    all_the_args(*args, **kwargs)
    print varargs(*args)
    print keyword_args(**kwargs)

# Funzioni Scope
x = 5

def set_x(num):
    # La variabile locale x non è uguale alla variabile globale x
    x = num # => 43
    print x # => 43

def set_global_x(num):
    global x
    print x # => 5
    x = num # la variabile globable x è ora 6
    print x # => 6

set_x(43)
set_global_x(6)

# Python ha funzioni di prima classe
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)   # => 13

# Ci sono anche funzioni anonime
(lambda x: x > 2)(3)   # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5

# Esse sono incluse in funzioni di alto livello
map(add_10, [1, 2, 3])   # => [11, 12, 13]
map(max, [1, 2, 3], [4, 2, 1])   # => [4, 2, 3]

filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]

# Possiamo usare la comprensione delle liste per mappe e filtri
[add_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]   # => [6, 7]

# Puoi fare anche la comprensione di set e dizionari
{x for x in 'abcddeef' if x in 'abc'}  # => {'d', 'e', 'f'}
{x: x**2 for x in range(5)}  # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


####################################################
## 5. Classi
####################################################

# Usiamo una sottoclasse da un oggetto per avere una classe.
class Human(object):

    # Un attributo della classe. E' condiviso da tutte le istanze delle classe
    species = "H. sapiens"

    # Costruttore base, richiamato quando la classe viene inizializzata.
    # Si noti che il doppio leading e gli underscore finali denotano oggetti
    # o attributi che sono usati da python ma che vivono nello spazio dei nome controllato
    # dall'utente. Non dovresti usare nomi di questo genere.
    def __init__(self, name):
        # Assegna l'argomento all'attributo name dell'istanza
        self.name = name

        # Inizializza una proprietà
        self.age = 0

    # Un metodo dell'istanza. Tutti i metodi prendo "self" come primo argomento
    def say(self, msg):
        return "{0}: {1}".format(self.name, msg)

    # Un metodo della classe è condiviso fra tutte le istanze
    # Sono chiamate con la classe chiamante come primo argomento
    @classmethod
    def get_species(cls):
        return cls.species

    # Un metodo statico è chiamato senza una classe od una istanza di riferimento
    @staticmethod
    def grunt():
        return "*grunt*"

    # Una proprietà è come un metodo getter.
    # Trasforma il metodo age() in un attributo in sola lettura, che ha lo stesso nome
    @property
    def age(self):
        return self._age

    # Questo metodo permette di modificare la proprietà
    @age.setter
    def age(self, age):
        self._age = age

    # Questo metodo permette di cancellare la proprietà
    @age.deleter
    def age(self):
        del self._age

# Instanziare una classe
i = Human(name="Ian")
print i.say("hi")     # stampa "Ian: hi"

j = Human("Joel")
print j.say("hello")  # stampa "Joel: hello"

# Chiamare metodi della classe
i.get_species()   # => "H. sapiens"

# Cambiare l'attributo condiviso
Human.species = "H. neanderthalensis"
i.get_species()   # => "H. neanderthalensis"
j.get_species()   # => "H. neanderthalensis"

# Chiamare il metodo condiviso
Human.grunt()   # => "*grunt*"

# Aggiorna la proprietà
i.age = 42

# Ritorna il valore della proprietà
i.age # => 42

# Cancella la proprietà
del i.age
i.age  # => Emette un AttributeError


####################################################
## 6. Moduli
####################################################

# Puoi importare moduli
import math
print math.sqrt(16)  # => 4

# Puoi ottenere specifiche funzione da un modulo
from math import ceil, floor
print ceil(3.7)  # => 4.0
print floor(3.7)   # => 3.0

# Puoi importare tutte le funzioni da un modulo
# Attenzione: questo non è raccomandato
from math import *

# Puoi abbreviare i nomi dei moduli
import math as m
math.sqrt(16) == m.sqrt(16)   # => True
# puoi anche verificare che le funzioni sono equivalenti
from math import sqrt
math.sqrt == m.sqrt == sqrt  # => True

# I moduli di Python sono normali file python. Ne puoi
# scrivere di tuoi ed importarli. Il nome del modulo
# è lo stesso del nome del file.

# Potete scoprire quali funzioni e attributi
# definiscono un modulo
import math
dir(math)

# Se nella cartella corrente hai uno script chiamato math.py,
# Python caricherà quello invece del modulo math.
# Questo succede perchè la cartella corrente ha priorità
# sulle librerie standard di Python


####################################################
## 7. Avanzate
####################################################

# Generatori
# Un generatore appunto "genera" valori solo quando vengono richiesti,
# invece di memorizzarli tutti subito fin dall'inizio

# Il metodo seguente (che NON è un generatore) raddoppia tutti i valori e li memorizza
# dentro `double_arr`. Se gli oggetti iterabili sono grandi, il vettore risultato
# potrebbe diventare enorme!
def double_numbers(iterable):
    double_arr = []
    for i in iterable:
        double_arr.append(i + i)

# Eseguendo il seguente codice, noi andiamo a raddoppiare prima tutti i valori, e poi
# li ritorniamo tutti e andiamo a controllare la condizione
for value in double_numbers(range(1000000)):  # `test_senza_generatore`
    print value
    if value > 5:
        break

# Invece, potremmo usare un generatore per "generare" il valore raddoppiato non
# appena viene richiesto
def double_numbers_generator(iterable):
    for i in iterable:
        yield i + i

# Utilizzando lo stesso test di prima, stavolta però con un generatore, ci permette
# di iterare sui valori e raddoppiarli uno alla volta, non appena vengono richiesti dalla
# logica del programma. Per questo, non appena troviamo un valore > 5, usciamo dal ciclo senza
# bisogno di raddoppiare la maggior parte dei valori del range (MOLTO PIU VELOCE!)
for value in double_numbers_generator(xrange(1000000)):  # `test_generatore`
    print value
    if value > 5:
        break

# Nota: hai notato l'uso di `range` in `test_senza_generatore` e `xrange` in `test_generatore`?
# Proprio come `double_numbers_generator` è la versione col generatore di `double_numbers`
# Abbiamo `xrange` come versione col generatore di `range`
# `range` ritorna un array di 1000000 elementi
# `xrange` invece genera 1000000 valori quando lo richiediamo/iteriamo su di essi

# Allo stesso modo della comprensione delle liste, puoi creare la comprensione
# dei generatori.
values = (-x for x in [1,2,3,4,5])
for x in values:
    print(x)  # stampa -1 -2 -3 -4 -5

# Puoi anche fare il cast diretto di una comprensione di generatori ad una lista.
values = (-x for x in [1,2,3,4,5])
gen_to_list = list(values)
print(gen_to_list)  # => [-1, -2, -3, -4, -5]


# Decoratori
# in questo esempio beg include say
# Beg chiamerà say. Se say_please è True allora cambierà il messaggio
# ritornato
from functools import wraps

def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Per favore! Sono povero :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Puoi comprarmi una birra?"
    return msg, say_please


print say()  # Puoi comprarmi una birra?
print say(say_please=True)  # Puoi comprarmi una birra? Per favore! Sono povero :(
```

## Pronto per qualcosa di più?

### Gratis Online

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)
* [LearnPython](http://www.learnpython.org/)
* [Fullstack Python](https://www.fullstackpython.com/)

### Libri cartacei

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
---
category: tool
tool: ruby ecosystem
contributors:
    - ["Jon Smock", "http://github.com/jonsmock"]
    - ["Rafal Chmiel", "http://github.com/rafalchmiel"]
translators:
    - ["Cristian Achille", "http://github.com/blackdev1l/"]
lang: it-it
---

Generalmente chi usa ruby ha l'esigenza di avere differenti versioni di Ruby
installate, gestire le proprie gemme, e le loro dipendenze.

## Manager Ruby

Alcune piattaforme hanno Ruby pre-installato o disponibile come pacchetto.
Molti sviluppatori Ruby non usano questi pacchetti, o se lo fanno, li usano solo
per installare dei manager Ruby, i quali permettono di installare e gestire più
versioni di Ruby in base al progetto su cui si lavora.

Di seguito i più famosi manager Ruby:

* [RVM](https://rvm.io/) - Installa e permette di utilizzare diverse versioni di
  Ruby. RVM Ha anche il concetto di gemsets i quali isolano completamente l'ambiente di sviluppo del progetto.
* [ruby-build](https://github.com/sstephenson/ruby-build) - Installa solamente
  multiple versioni di ruby. Usa questo se vuoi maggior controllo sull'installazione di Ruby.
* [rbenv](https://github.com/sstephenson/rbenv) -
  Permette solo la scelta di quale versione Ruby utilizzare. Usato insieme a ruby-build.
  Utilizza questo per un maggior controllo su quale versione di Ruby utilizzare.
* [chruby](https://github.com/postmodern/chruby) -
  Permette solo la scelta di quale Ruby utilizzare, simile a rbenv.

## Ruby Versions

Ruby fu creato da Yukihiro "Matz" Matsumoto, il [BDFL](https://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life),
(acronimo inglese che sta per "Benevolo dittatore a vita") , seppur
ultimamente non è più del tutto vera; l'implementazione di Ruby
è detta  MRI (Matz' Reference Implementation), e dunque quando si legge di una
versione Ruby, essa si riferisce sempre al rilascio di una MRI

Le tre maggiori versioni di Ruby in uso sono:

* 2.0.0 - Rilasciata nel febbraio 2013. La maggior parte delle librerie e
  framework supportano la 2.0.0
* 1.9.3 - Rilasciata nel ottobre 2011. QUesta è la versione che molti
  svluppatori usano, il supporto è
  [concluso](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/)
* 1.8.7 - Il supporto per Ruby 1.8.7 è
  [concluso](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/).

I cambiamenti tra la 1.8.7 a la 1.9.x sono maggiori di quelli tra la 1.9.3 a la
2.0.0. Per esempio, nella 1.9 vengono introdotti encodings e bytecode VM.
Esistono ancora dei progetti basati sulla 1.8.7, ma stanno diventando una
minoranza man mano che la community si trasferisce alle versioni 1.92 e
1.9.3

## Ruby Implementations

L'ecosistema Ruby gode di molte implementazioni differenti di Ruby, ognuna con
particolari punti di forza, da chiarire che ogni implementazione è scritta con
un linguaggio diverso, ma esse *sono tutte Ruby*. Ogni implementazione ha
feature extra ma tutte esse possono eseguire file ruby. Per esempio, JRuby è
scritto in Java, ma non devi conoscere java per usarlo.

Implementazioni mature e compatibili:

* [MRI](https://github.com/ruby/ruby) - Scritto in C, Questa è l'implementazione
  standard di Ruby, per definizione è 100% compatibile (con se stessa). Tutte le
  altre implemetazioni mantengono la compatibilità con MRI
  (vedere [RubySpec](#rubyspec) sotto).
* [JRuby](http://jruby.org/) - Scritto in Java e Ruby, Questa implementazione è
  molto veloce e robusta, la forza di JRuby consiste nell'interoperabilità
  tra JVM/Java, permettendo l'utilizzo di struemnti Java già esistenti, progetti
  e linguaggi
* [Rubinius](http://rubini.us/) - Scritto principalmente in Ruby con un
  c++ bytecode VM, molto matura e veloce, permette alcune feature riguardo VM.

Mediamente mature e compatibili:

* [Maglev](http://maglev.github.io/) - Sviluppata sui Gemstone, è una Smalltalk
VM, Smalltalk è degli strumenti molto utili, e questo progetto cerca di portare
questi strumenti nello sviluppo Ruby.
* [RubyMotion](http://www.rubymotion.com/) - Porta ruby nello sviluppo iOS.

Poco mature e compatibili:

* [Topaz](http://topazruby.com/) - Scritto in RPython (usando PyPy come
  toolchain) Topaz è un progetto ancora giovane e non compatibile, ha le
  possibilità di diventare una implementazione Ruby molto performante
* [IronRuby](http://ironruby.net/) - Scritto in C# e prendendo di mira la
  piattaforma .NET, lo sviluppo sembra fermo da quando Microsoft ha rimosso il
  suo supporto.

Le implementazioni Ruby possono avere una propria versione, ma hanno sempre come
target una specifica versione di MRI. Molte implementazioni hanno l'abilità di
selezionare una versione specifica di MRI.

##RubySpec

La maggior parte delle implementazioni Ruby dipendono pesantemente su
[RubySpec](http://rubyspec.org/). Ruby non ha una specifica ufficiale, quindi la
community ha scritto una specifica eseguibile in Ruby per testare la compatibilità
con MRI.

## RubyGems

[RubyGems](http://rubygems.org/) è un package manager gestito dalla communtiy
per Ruby. Rubygems viene installato con Ruby, quindi non c'è bisogno di
scaricarlo separatamente.

I pacchetti Ruby sono chiamate "gemme", e possono essere hostate dalla community
su RubyGems.org . Ogni gemma contiene il codice sorgente e del metadata, tra cui
la versione, le dipendenze, autor* e licenz*.

## Bundler

[Bundler](http://bundler.io/) è un risolvitore di dipendenze, Esso usa il Gemfile
di un progetto per cercare le dipendenze, dopo di che ottiene le dipendenze delle
dipendenze ricorsivamente, Questo procedimento viene eseguito finchè tutte le
dipendenze sono state risolte e scaricate, o si fermerà se un conflitto verrà
trovato.

Bundler genererà un error se troverà dipendenze in conflitto, Per esempio,
se la gemma A richiede la versione 3 o maggiore della gemma Z, ma la gemma B
richiede la versione 2, Bundler ti notificherà del conflitto. Questo diventa
di aiuto nel momento in cui si hanno molte gemme nel progetto, il che porta a
un grande grafo di dipendenza da risolvere.

# Testing

Il testing è un pezzo fondamentale della cultura Ruby, Ruby viene installato con
il proprio testing framework chiamato minitest (O TestUnit per ruby 1.8.x).
Esistono molte librerie con obiettivi differenti

* [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html) - Testing frameowrk rilasciato insieme a Ruby 1.8.x
* [minitest](http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html) - Testing frameowrk rilasciato insieme a Ruby 1.9/2.0
* [RSpec](http://rspec.info/) - Un testing framework che si concentra nella chiarezza
* [Cucumber](http://cukes.info/) - Un BDD testing framework che estrapola testo formattato in Gherkin

## Sii cordiale

La community Ruby è orgogliosa di essere una communtiy aperta, accogliente e
variegata. Matz stesso è estremamente amichevole, e la generosità degli sviluppatori
Ruby è fantastica.
---
language: rust
contributors:
    - ["Carlo Milanesi", "http://github.com/carlomilanesi"]
lang: it-it
filename: rust-it.html.markdown
---

Rust è un linguaggio di programmazione sviluppato da Mozilla Research.
Rust combina il controllo a basso livello sulle prestazioni con alcune comodità
ad alto livello e stringenti garanzie di sicurezza.

Rust raggiunge questi obiettivi senza richiedere la garbage collection né una grossa
libreria di supporto run-time, rendendo così possibile l'uso di librerie scritte in Rust
come rimpiazzo di librerie scritte in C.

La prima versione pubblica di Rust, la 0.1, è stata rilasciata nel gennaio 2012, e per 3 anni
lo sviluppo è proceduto così rapidamente che l'utilizzo delle versioni
stabili veniva scoraggiato, e piuttosto si consigliava di utilizzare le versioni notturne
(nightly build).

Il 15 maggio 2015, la versione 1.0 di Rust è stata rilasciata con la garanzia
che nelle successive versioni 1.x non ci sarebbero state modifiche che avrebbero reso
incompatibile il codice scritto per tale versione.
Nelle nightly build sono attualmente disponibili migliorie al tempo di compilazione
e ad altri aspetti del compilatore. Rust ha adottato un modello di rilascio a scaglioni
con rilasci regolari ogni sei settimane. Per esempio, la versione 1.1 beta è stata resa
disponibile contestualmente al rilascio della versione stabile 1.0.

Sebbene Rust sia un linguaggio di livello relativamente basso, Rust ha alcuni concetti
di programmazione funzionale che solitamente si trovano solo nei linguaggi di livello più alto.
Ciò rende Rust non solo veloce, ma anche facile ed comodo da usare.

```rust
// I commenti che stanno su una sola riga sono fatti così...
/* ...mentre così sono fatti
i commenti che richiedono
più righe */

///////////////////
// 1. Fondamenti //
///////////////////

// Funzioni
// `i32` è il tipo per gli interi a 32-bit con segno
fn add2(x: i32, y: i32) -> i32 {
    // return implicito (senza punto-e-virgola)
    x + y
}

// Funzione "main"
fn main() {
    // Numeri //

    // Binding (ossia "variabili") immutabili
    let x: i32 = 1;

    // Suffissi intero/virgola mobile
    let y: i32 = 13i32;
    let f: f64 = 1.3f64;

    // Inferenza di tipo
    // La maggior parte delle volte, il compilatore Rust può inferire
    // di quale tipo sia l'espressione usata per inizializzare un binding,
    // e quindi non è necessario specificare esplicitamente il tipo.
    // In tutto questo tutorial, i tipi vengono specificati esplicitamente in molti posti,
    // ma solo a scopo dimostrativo. La maggior parte delle volte se ne potrebbe
    // fare a meno, grazie all'inferenza di tipo.
    let implicito_x = 1;
    let implicito_f = 1.3;

    // Aritmetica
    let somma = x + y + 13;

    // Variabile mutevole
    let mut mutevole = 1;
    mutevole = 4;
    mutevole += 2;

    // Stringhe //

    // Letterali di stringa
    let x: &str = "Ciao mondo!";

    // Stampa
    println!("{} {}", f, x); // 1.3 Ciao mondo!

    // Una `String` – una stringa allocata nello heap
    let s: String = "Ciao mondo".to_string();

    // Uno slice (fetta) di stringa – una vista immutabile
    // all'interno di un'altra stringa.
    // Uno slice è una coppia immutabile di puntatori al buffer contenuto
    // nella stringa - non contiene dei caratteri, solo dei puntatori a
    // un buffer statico o a un buffer contenuto in un altro oggetto (in questo caso, `s`)
    let s_slice: &str = &s;

    println!("{} - {}", s, s_slice); // Ciao mondo - Ciao mondo

    // Vettori/array //

    // Un array di lunghezza fissa
    let quattro_int: [i32; 4] = [1, 2, 3, 4];

    // Un array dinamico (vettore)
    let mut vettore: Vec<i32> = vec![1, 2, 3, 4];
    vettore.push(5);

    // Uno slice – una vista immutabile all'interno di un vettore o di un array
    // E' molto simile a uno slice di stringa, ma per i vettori
    let slice: &[i32] = &vettore;

    // Usa `{:?}` per stampare qualcosa a scopo di debugging
    println!("{:?} {:?}", vettore, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    // Tuple //

    // Una tupla è un insieme ordinato di dimensione fissa di valori aventi tipi eventualmente diversi
    let x: (i32, &str, f64) = (1, "ciao", 3.4);

    // Il `let` che destruttura
    let (a, b, c) = x;
    println!("{} {} {}", a, b, c); // 1 ciao 3.4

    // Indicizzazione
    println!("{}", x.1); // ciao

    /////////////
    // 2. Tipi //
    /////////////

    // Strutture
    struct Point {
        x: i32,
        y: i32,
    }

    let origine: Punto = Punto { x: 0, y: 0 };

    // Ana struct con campi senza nome, chiamata ‘tuple struct’
    struct Punto2(i32, i32);

    let origine2 = Punto2(0, 0);

    // Enum basilare, analoga a quelle del linguaggio C
    enum Direzione {
        Sinistra,
        Destra,
        Su,
        Giu,
    }

    let su = Direzione::Su;

    // Enum con campi
    enum OpzionaleI32 {
        UnI32(i32),
        Niente,
    }

    let due: OpzionaleI32 = OpzionaleI32::UnI32(2);
    let niente = OpzionaleI32::Niente;

    // Generici //

    struct Foo<T> { bar: T }

    // Questo è definito nella libreria standard come `Option`
    enum Opzionale<T> {
        QualcheValore(T),
        NessunValore,
    }

    // Metodi //

    impl<T> Foo<T> {
        // I metodi di oggetto prendono un parametro `self` esplicito
        fn get_bar(self) -> T {
            self.bar
        }
    }

    let a_foo = Foo { bar: 1 };
    println!("{}", a_foo.get_bar()); // 1

    // I trait (tratti), noti come "interfacce" o "mixin" in altri linguaggi

    trait Maneggiamento<T> {
        fn maneggia(self) -> Option<T>;
    }

    impl<T> Maneggiamento<T> for Foo<T> {
        fn maneggia(self) -> Option<T> {
            Some(self.bar)
        }
    }

    let altro_foo = Foo { bar: 1 };
    println!("{:?}", altro_foo.maneggia()); // Some(1)

    /////////////////////////
    // 3. Pattern matching //
    /////////////////////////

    let foo = OpzionaleI32::UnI32(1);
    match foo {
        OpzionaleI32::UnI32(n) => println!("E' un i32: {}", n),
        OpzionaleI32::Niente  => println!("Non vale niente!"),
    }

    // Pattern matching avanzato
    struct FooBar { x: i32, y: OpzionaleI32 }
    let bar = FooBar { x: 15, y: OpzionaleI32::UnI32(32) };

    match bar {
        FooBar { x: 0, y: OpzionaleI32::UnI32(0) } =>
            println!("I numeri valgono zero!"),
        FooBar { x: n, y: OpzionaleI32::UnI32(m) } if n == m =>
            println!("I numeri sono identici"),
        FooBar { x: n, y: OpzionaleI32::UnI32(m) } =>
            println!("Numeri diversi: {} {}", n, m),
        FooBar { x: _, y: OpzionaleI32::Niente } =>
            println!("Il secondo numbero non vale niente!"),
    }

    ///////////////////////////////////////////
    // 4. Flusso di controllo (Control flow) //
    ///////////////////////////////////////////

    // Ciclo/iterazione con `for`
    let array = [1, 2, 3];
    for i in array.iter() {
        println!("{}", i);
    }

    // Range
    for i in 0u32..10 {
        print!("{} ", i);
    }
    println!("");
    // Stampa `0 1 2 3 4 5 6 7 8 9 `

    // `if`
    if 1 == 1 {
        println!("La matematica funziona!");
    } else {
        println!("Oh no...");
    }

    // `if` come espressione
    let value = if true {
        "bene"
    } else {
        "male"
    };

    // Ciclo `while`
    while 1 == 1 {
        println!("L'universo sta funzionando regolarmente.");
    }

    // Ciclo infinito
    loop {
        println!("Ciao!");
    }

    /////////////////////////////////////////////////
    // 5. La sicurezza della memoria e i puntatori //
    /////////////////////////////////////////////////

    // Puntatore posseduto (owned) – solamente una cosa sola per volta può ‘possedere’ questo puntatore
    // Ciò significa che quando il `Box` abbandona il suo scope, verrà automaticamente deallocato in sicurezza.
    let mut mio: Box<i32> = Box::new(3);
    *mio = 5; // dereference
    // Qui, `adesso_e_mio` acquisisce la proprietà di `mio`. In altre parole, `mio` viene spostato.
    let mut adesso_e_mio = mio;
    *adesso_e_mio += 2;

    println!("{}", adesso_e_mio); // 7
    // println!("{}", mio); // questo non compilerebbe perché `adesso_e_mio` adesso possiede il puntatore

    // Riferimento (reference) – un puntatore immutabile che si riferisce ad altri dati
    // Quando un riferimento viene preso a un valore, diciamo che quel valore
    // è stato ‘preso in prestito’ (borrowed).
    // Mentre un valore è preso in prestito immutabilmente, non può venire mutato né spostato.
    // Un prestito dura fino alla fine dello scope in cui è stato creato.
    let mut var = 4;
    var = 3;
    let ref_var: &i32 = &var;

    println!("{}", var); // Diversamente da `mio`, `var` può ancora essere usato
    println!("{}", *ref_var);
    // var = 5; // questo non compilerebbe, perché `var` è stato preso in prestito
    // *ref_var = 6; // neanche questo, perché `ref_var` è un riferimento immutabile

    // Riferimento immutabile
    // Mentre un valore è preso in presto mutevolmente, non può essere acceduto in nessun modo.
    let mut var2 = 4;
    let ref_var2: &mut i32 = &mut var2;
    *ref_var2 += 2;         // '*' serve a puntare al binding var2, preso in presto mutevolmente

    println!("{}", *ref_var2); // 6
    // var2 non compilerebbe. ref_var2 è di tipo &mut i32, e quindi 
    // immagazzina un riferimento a un i32, e non il valore stesso.
    // var2 = 2; // questo non compilerebbe, perché `var2` è stato preso in prestito
}
```

## Ulteriori letture

C'è molto di più in Rust — questi sono solo i fondamenti di Rust, che servono a capire
le cose più importanti.

Purtroppo c'è pochissima documentazione in italiano, tra cui:
(https://www.mozillaitalia.org/home/2015/05/30/primi-passi-con-rust/)

Però ce n'è parecchia in inglese. Per saperne di più, leggi [The Rust Programming
Language](http://doc.rust-lang.org/book/index.html) e tieni d'occhio l'area di interesse di Reddit (subreddit)
[/r/rust](http://reddit.com/r/rust).

Puoi anche provare a programmare in varie versioni di Rust usando il compilatore online al sito ufficiale
[Rust playpen](http://play.rust-lang.org).
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
translators:
    - ["akirahirose", "https://twitter.com/akirahirose"]
filename: LearnBash-jp.sh
lang: ja-jp
---

Bash はunixシェルの1つです。GNUオペレーションシステムのシェルとして配布されています。
LinuxやMac OS Xの、デフォルトシェルにもなっています。
以下にある例は、ほぼ全部シェルスクリプトの一部として使えます。また、一部はそのままシェルから実行できます。

[ちゃんとした説明は、こちらをどうぞ](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# 最初の行はShebang(シェバング、シバン)というもので、システムに対して何を使って実行するのかを教えるためのものです
# ちゃんとした説明、こちらをどうぞ: http://en.wikipedia.org/wiki/Shebang_(Unix)
# 既にお気づきのように、コメント行は#で始まります. Shebangもコメントです

# まずは Hello world です
echo Hello world!

# コマンド毎に改行を入れるか、セミコロンで区切ります
echo 'This is the first line'; echo 'This is the second line'

# 変数の宣言はこのようにやります
VARIABLE="Some string"

# が、以下のようにやってはダメです
VARIABLE = "Some string"
# このように（空白を入れて）書くと、Bash はVARIABLEを実行するべきコマンドとみなし、実行します。
# そして、VARIABLEというコマンドはない（はずな）ので、エラーになります


# 変数の使い方
echo $VARIABLE
echo "$VARIABLE"
echo '$VARIABLE'
# 変数の値（中身）を使わず、変数名だけを使うとき（代入するときや渡すときなど）は、$なしで変数名を書いてください
# 変数の値（中身）を使うときは、$をつけます
# 上記例の最後にある、' (シングルクォート) で囲んだ場合は、変数の値は表示されません!

# 変数値の文字列置換
echo ${VARIABLE/Some/A}
# 最初に出てくる "Some" を "A" で置換します

# 変数値の一部を取り出します
echo ${VARIABLE:0:7}
# 最初の7文字だけを取り出します

# デフォルトの変数値設定(訳注:シェル実行時に引数で変数値を設定できるが、設定しなかった場合の値を指定できる）
echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"}
# 上記は、FOO番目の引数がnullだったとき(FOO番目=)や、空文字が指定されたとき(FOO番目="")に、変数に0を入れます
# ( 当然ですが、引数に0を指定したとき(FOO番目=0) も、0は入ります）

# 組込み変数
# 以下のような便利な組込み変数があります
echo "Last program return value: $?"
echo "Script's PID: $$"
echo "Number of arguments: $#"
echo "Scripts arguments: $@"
echo "Scripts arguments separated in different variables: $1 $2..."

# 入力値の読み込み
echo "What's your name?"
read NAME # 新しく変数を宣言する必要はないことに注意
echo Hello, $NAME!

# 普通のif文も使えます
# 利用できる判定条件については、'man test' で参照してください
if [ $NAME -ne $USER ]
then
    echo "Your name is your username"
else
    echo "Your name isn't your username"
fi

# 他にも、条件判定ができます
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# 数式は以下のように書きます
echo $(( 10 + 5 ))

# 他のプログラム言語とは違ってbashはシェルなので、現在いるディレクトリ位置が異なると、異なる結果になります
# lsコマンドで、現在いるディレクトリにあるファイルと、ディレクトリのリストが取得できます
ls

# これらのコマンドには、実行オプションがいろいろあります
ls -l # ファイルとディレクトリのリストを行に分けて表示します

# あるコマンド実行による返値を、次のコマンドの入力値としてつかえます
# 例えばですが、lsコマンドの返値を、grepコマンドによって指定したルールに基づいてフィルタできます。
# 以下は、現在いるディレクトリにある、.txtファイルのリストを表示する例です
ls -l | grep "\.txt"

# コマンドに対する入力元や出力先、またエラー出力先などを変更できます
python2 hello.py < "input.in"
python2 hello.py > "output.out"
python2 hello.py 2> "error.err"
# 出力先として指定したファイルが既に存在する場合は、上書きされます
# もしもファイルに追記したい場合は、代わりに">>" を使ってください

# コマンド文中で、$()内に別コマンドを入れると、その別コマンドの返値をコマンド文の一部として使う事ができます
# 次のコマンドは、現在いるディレクトリにあるファイルの数を表示します
echo "There are $(ls | wc -l) items here."

# バッククォート(backticks) `` でも同じことができますが、入れ子にはできません
# そのため、$()がお勧めです
echo "There are `ls | wc -l` items here."

# BashはJavaやC++のように、case文による分岐ができます
case "$VARIABLE" in
    #分岐条件として使いたいパターンを並べてください
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# 指定した回数、処理を繰り返し
# 変数の値 $VARIABLE が3回表示されます
for VARIABLE in {1..3}
do
    echo "$VARIABLE"
done

# while ループです
while [true]
do
    echo "loop body here..."
    break
done

# 関数の定義もできます
function foo ()
{
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
}

# 以下のように、もっと簡単な書き方もあります
bar ()
{
    echo "Another way to declare functions!"
    return 0
}

# 自作関数を呼びます
foo "My name is" $NAME

# 他にもいろいろと、知っておくと便利なコマンドがあります
# file.txtの最後10行を表示します
tail -n 10 file.txt
# file.txtの最初10行を表示します
head -n 10 file.txt
# file.txt's の行を並び替えます
sort file.txt
# 重複している行を表示するか、削除できます。-dオプションをつけると、表示します
uniq -d file.txt
# 1行ごとに、','が最初に出てくる前の部分を表示します
cut -d ',' -f 1 file.txt

```
---
language: Julia
contributors:
    - ["Leah Hanson", "http://leahhanson.us"]
translators:
    - ["Yuichi Motoyama", "https://github.com/yomichi"]
filename: learnjulia-jp.jl
lang: ja-jp
---

Julia は科学技術計算向けに作られた、同図像性を持った(homoiconic) プログラミング言語です。
マクロによる同図像性や第一級関数などの抽象化機能の恩恵を受けつつ、低階層をも扱えますが、
それでいてPython 並に学習しやすく、使いやすい言語となっています。

この文章は、Julia の2013年10月18日現在の開発バージョンを元にしています。

```ruby

# ハッシュ（シャープ）記号から改行までは単一行コメントとなります。
#= 複数行コメントは、
   '#=' と '=#' とで囲むことで行えます。
   #= 
   入れ子構造にすることもできます。
   =#
=#

####################################################
## 1. 基本的な型と演算子
####################################################

# Julia ではすべて式となります。

# 基本となる数値型がいくつかあります。
3 # => 3 (Int64)
3.2 # => 3.2 (Float64)
2 + 1im # => 2 + 1im (Complex{Int64})
2//3 # => 2//3 (Rational{Int64})

# 一般的な中置演算子が使用可能です。
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
5 / 2 # => 2.5 # 整数型同士の割り算の結果は、浮動小数点数型になります
div(5, 2) # => 2 # 整数のまま割り算するには、 div を使います
5 \ 35 # => 7.0
2 ^ 2 # => 4 # べき乗です。排他的論理和ではありません
12 % 10 # => 2

# 丸括弧で演算の優先順位をコントロールできます
(1 + 3) * 2 # => 8

# ビット演算
~2 # => -3   # ビット反転
3 & 5 # => 1 # ビット積
2 | 4 # => 6 # ビット和
2 $ 4 # => 6 # ビット排他的論理和
2 >>> 1 # => 1 # 右論理シフト
2 >> 1  # => 1 # 右算術シフト
2 << 1  # => 4 # 左シフト

# bits 関数を使うことで、数の二進表現を得られます。
bits(12345)
# => "0000000000000000000000000000000000000000000000000011000000111001"
bits(12345.0)
# => "0100000011001000000111001000000000000000000000000000000000000000"

# ブール値が用意されています
true
false

# ブール代数
!true # => false
!false # => true
1 == 1 # => true
2 == 1 # => false
1 != 1 # => false
2 != 1 # => true
1 < 10 # => true
1 > 10 # => false
2 <= 2 # => true
2 >= 2 # => true
# 比較演算子をつなげることもできます
1 < 2 < 3 # => true
2 < 3 < 2 # => false

# 文字列は " で作れます
"This is a string."

# 文字リテラルは ' で作れます
'a'

# 文字列は文字の配列のように添字アクセスできます
"This is a string"[1] # => 'T' # Julia では添字は 1 から始まります
# ただし、UTF8 文字列の場合は添字アクセスではうまくいかないので、
# イテレーションを行ってください(map 関数や for ループなど)

# $ を使うことで、文字列に変数や、任意の式を埋め込めます。
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"

# 他にも、printf マクロを使うことでも変数を埋め込めます。
@printf "%d is less than %f" 4.5 5.3 # 5 is less than 5.300000

# 出力も簡単です
println("I'm Julia. Nice to meet you!")

####################################################
## 2. 変数と配列、タプル、集合、辞書
####################################################

# 変数の宣言は不要で、いきなり変数に値を代入・束縛できます。
some_var = 5 # => 5
some_var # => 5

# 値に束縛されていない変数を使おうとするとエラーになります。
try
    some_other_var # => ERROR: some_other_var not defined
catch e
    println(e)
end

# 変数名は数字や記号以外の文字から始めます。
# その後は、数字やアンダースコア(_), 感嘆符(!)も使えます。
SomeOtherVar123! = 6 # => 6

# Unicode 文字も使えます。
☃ = 8 # => 8
# ギリシャ文字などを使うことで数学的な記法が簡単にかけます。
2 * π # => 6.283185307179586

# Julia における命名習慣について:
#
# * 変数名における単語の区切りにはアンダースコアを使っても良いですが、
#   使わないと読みにくくなる、というわけではない限り、
#   推奨はされません。
#
# * 型名は大文字で始め、単語の区切りにはキャメルケースを使います。
#
# * 関数やマクロの名前は小文字で書きます。
#   単語の分かち書きにはアンダースコアをつかわず、直接つなげます。
#
# * 内部で引数を変更する関数は、名前の最後に ! をつけます。
#   この手の関数は、しばしば「破壊的な関数」とか「in-place な関数」とか呼ばれます。


# 配列は、1 から始まる整数によって添字付けられる、値の列です。
a = Int64[] # => 0-element Int64 Array

# 一次元配列（列ベクトル）は、角括弧 [] のなかにカンマ , 区切りで値を並べることで作ります。
b = [4, 5, 6] # => 3-element Int64 Array: [4, 5, 6]
b[1] # => 4
b[end] # => 6

# 二次元配列は、空白区切りで作った行を、セミコロンで区切ることで作ります。
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]

# 配列の末尾に値を追加するには push! を、
# 他の配列を結合するには append! を使います。
push!(a,1)     # => [1]
push!(a,2)     # => [1,2]
push!(a,4)     # => [1,2,4]
push!(a,3)     # => [1,2,4,3]
append!(a,b) # => [1,2,4,3,4,5,6]

# 配列の末尾から値を削除するには pop! を使います。
pop!(b)        # => 6 and b is now [4,5]

# 一旦元に戻しておきましょう。
push!(b,6)   # b is now [4,5,6] again.

a[1] # => 1 # Julia では添字は0 ではなく1 から始まること、お忘れなく!

# end は最後の添字を表す速記法です。
# 添字を書く場所ならどこにでも使えます。
a[end] # => 6

# 先頭に対する削除・追加は shift!, unshift! です。
shift!(a) # => 1 and a is now [2,4,3,4,5,6]
unshift!(a,7) # => [7,2,4,3,4,5,6]

# ! で終わる関数名は、その引数を変更するということを示します。
arr = [5,4,6] # => 3-element Int64 Array: [5,4,6]
sort(arr) # => [4,5,6]; arr is still [5,4,6]
sort!(arr) # => [4,5,6]; arr is now [4,5,6]

# 配列の範囲外アクセスをすると BoundsError が発生します。
try
    a[0] # => ERROR: BoundsError() in getindex at array.jl:270
    a[end+1] # => ERROR: BoundsError() in getindex at array.jl:270
catch e
    println(e)
end

# エラーが発生すると、どのファイルのどの行で発生したかが表示されます。
# 標準ライブラリで発生したものでもファイル名と行数が出ます。
# ソースからビルドした場合など、標準ライブラリのソースが手元にある場合は
# base/ ディレクトリから探し出して見てください。

# 配列は範囲オブジェクトから作ることもできます。
a = [1:5] # => 5-element Int64 Array: [1,2,3,4,5]

# 添字として範囲オブジェクトを渡すことで、
# 配列の部分列を得ることもできます。
a[1:3] # => [1, 2, 3]
a[2:end] # => [2, 3, 4, 5]

# 添字を用いて配列から値の削除をしたい場合は、splice! を使います。
arr = [3,4,5]
splice!(arr,2) # => 4 ; arr is now [3,5]

# 配列の結合は append! です。
b = [1,2,3]
append!(a,b) # Now a is [1, 2, 3, 4, 5, 1, 2, 3]

# 配列内に指定した値があるかどうかを調べるのには in を使います。
in(1, a) # => true

# length で配列の長さを取得できます。
length(a) # => 8

# 変更不可能 (immutable) な値の組として、タプルが使えます。
tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple.
tup[1] # => 1
try:
    tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
catch e
    println(e)
end

# 配列に関する関数の多くが、タプルでも使えます。
length(tup) # => 3
tup[1:2] # => (1,2)
in(2, tup) # => true

# タプルから値をばらして(unpack して) 複数の変数に代入できます。
a, b, c = (1, 2, 3) # => (1,2,3)  # a is now 1, b is now 2 and c is now 3

# 丸括弧なしでもタプルになります。
d, e, f = 4, 5, 6 # => (4,5,6)

# ひとつの値だけからなるタプルは、その値自体とは区別されます。
(1,) == 1 # => false
(1) == 1 # => true

# 値の交換もタプルを使えば簡単です。
e, d = d, e  # => (5,4) # d is now 5 and e is now 4


# 辞書 (Dict) は、値から値への変換の集合です。
empty_dict = Dict() # => Dict{Any,Any}()

# 辞書型リテラルは次のとおりです。
filled_dict = ["one"=> 1, "two"=> 2, "three"=> 3]
# => Dict{ASCIIString,Int64}

# [] を使ったアクセスができます。
filled_dict["one"] # => 1

# すべての鍵（添字）は keys で得られます。
keys(filled_dict)
# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# 必ずしも辞書に追加した順番には並んでいないことに注意してください。

# 同様に、values はすべての値を返します。
values(filled_dict)
# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# 鍵と同様に、必ずしも辞書に追加した順番には並んでいないことに注意してください。

# in や haskey を使うことで、要素や鍵が辞書の中にあるかを調べられます。
in(("one", 1), filled_dict) # => true
in(("two", 3), filled_dict) # => false
haskey(filled_dict, "one") # => true
haskey(filled_dict, 1) # => false

# 存在しない鍵を問い合わせると、エラーが発生します。
try
    filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489
catch e
    println(e)
end

# get 関数を使い、鍵がなかった場合のデフォルト値を与えておくことで、
# このエラーを回避できます。
get(filled_dict,"one",4) # => 1
get(filled_dict,"four",4) # => 4

# 集合 (Set) は一意な値の、順序付けられていない集まりです。
empty_set = Set() # => Set{Any}()
# 集合の初期化
filled_set = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4)

# 集合への追加
push!(filled_set,5) # => Set{Int64}(5,4,2,3,1)

# in で、値が既に存在するかを調べられます。
in(2, filled_set) # => true
in(10, filled_set) # => false

# 積集合や和集合、差集合を得る関数も用意されています。
other_set = Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3)
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4)


####################################################
## 3. 制御構文
####################################################

# まずは変数を作ります。
some_var = 5

# if 構文です。Julia ではインデントに意味はありません。
if some_var > 10
    println("some_var is totally bigger than 10.")
elseif some_var < 10    # elseif 節は省略可能です。
    println("some_var is smaller than 10.")
else                    # else 節も省略可能です。
    println("some_var is indeed 10.")
end
# => "some var is smaller than 10" と出力されます。

# for ループによって、反復可能なオブジェクトを走査できます。
# 反復可能なオブジェクトの型として、
# Range, Array, Set, Dict, String などがあります。
for animal=["dog", "cat", "mouse"]
    println("$animal is a mammal")
    # $ を使うことで文字列に変数の値を埋め込めます。
    # You can use $ to interpolate variables or expression into strings
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# for = の代わりに for in を使うこともできます
for animal in ["dog", "cat", "mouse"]
    println("$animal is a mammal")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# 辞書ではタプルが返ってきます。
for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$(a[1]) is a $(a[2])")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# タプルのアンパック代入もできます。
for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$k is a $v")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# while ループは、条件式がtrue となる限り実行され続けます。
x = 0
while x < 4
    println(x)
    x += 1  # Shorthand for x = x + 1
end
# prints:
#   0
#   1
#   2
#   3

# 例外は try/catch で捕捉できます。
try
   error("help")
catch e
   println("caught it $e")
end
# => caught it ErrorException("help")


####################################################
## 4. 関数
####################################################

# function キーワードを次のように使うことで、新しい関数を定義できます。
#function name(arglist)
#  body...
#end
function add(x, y)
    println("x is $x and y is $y")

    # 最後に評価された式の値が、関数全体の返り値となります。
    x + y
end

add(5, 6) # => 11 after printing out "x is 5 and y is 6"

# 可変長引数関数も定義できます。
function varargs(args...)
    return args
    # return キーワードを使うことで、好きな位置で関数から抜けられます。
end
# => varargs (generic function with 1 method)

varargs(1,2,3) # => (1,2,3)

# ... はsplat と呼ばれます
# （訳注：「ピシャッという音（名詞）」「衝撃で平らにする（動詞）」）
# 今回は関数定義で使いましたが、関数呼び出しに使うこともできます。
# その場合、配列やタプルの要素を開いて、複数の引数へと割り当てることとなります。
Set([1,2,3])    # => Set{Array{Int64,1}}([1,2,3]) # 「整数の配列」の集合
Set([1,2,3]...) # => Set{Int64}(1,2,3) # 整数の集合

x = (1,2,3)     # => (1,2,3)
Set(x)          # => Set{(Int64,Int64,Int64)}((1,2,3)) # タプルの集合
Set(x...)       # => Set{Int64}(2,3,1)


# 引数に初期値を与えることで、オプション引数をもった関数を定義できます。
function defaults(a,b,x=5,y=6)
    return "$a $b and $x $y"
end

defaults('h','g') # => "h g and 5 6"
defaults('h','g','j') # => "h g and j 6"
defaults('h','g','j','k') # => "h g and j k"
try
    defaults('h') # => ERROR: no method defaults(Char,)
    defaults() # => ERROR: no methods defaults()
catch e
    println(e)
end

# キーワード引数を持った関数も作れます。
function keyword_args(;k1=4,name2="hello") # ; が必要なことに注意
    return ["k1"=>k1,"name2"=>name2]
end

keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4]
keyword_args(k1="mine") # => ["k1"=>"mine","name2"=>"hello"]
keyword_args() # => ["name2"=>"hello","k1"=>4]

# もちろん、これらを組み合わせることもできます。
function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo")
    println("normal arg: $normal_arg")
    println("optional arg: $optional_positional_arg")
    println("keyword arg: $keyword_arg")
end

all_the_args(1, 3, keyword_arg=4)
# prints:
#   normal arg: 1
#   optional arg: 3
#   keyword arg: 4

# Julia では関数は第一級関数として、値として扱われます。
function create_adder(x)
    adder = function (y)
        return x + y
    end
    return adder
end

# ラムダ式によって無名関数をつくれます。
(x -> x > 2)(3) # => true

# 先ほどの create_adder と同じもの
function create_adder(x)
    y -> x + y
end

# 中の関数に名前をつけても構いません。
function create_adder(x)
    function adder(y)
        x + y
    end
    adder
end

add_10 = create_adder(10)
add_10(3) # => 13


# いくつかの高階関数が定義されています。
map(add_10, [1,2,3]) # => [11, 12, 13]
filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7]

# map の代わりとしてリスト内包表記も使えます。
[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13]
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]

####################################################
## 5. 型
####################################################

# Julia ではすべての値にひとつの型がついています。
# 変数に、ではなくて値に、です。
# typeof 関数を使うことで、値が持つ型を取得できます。
typeof(5) # => Int64

# 型自身もまた、第一級の値であり、型を持っています。
typeof(Int64) # => DataType
typeof(DataType) # => DataType
# DataType は型を表現する型であり、DataType 自身もDataType 型の値です。

# 型はドキュメント化や最適化、関数ディスパッチのために使われます。
# 静的な型チェックは行われません。

# 自分で新しい型を定義することもできます。
# 他の言語で言う、構造体やレコードに近いものになっています。
# 型定義には type キーワードを使います。
# type Name
#   field::OptionalType
#   ...
# end
type Tiger
  taillength::Float64
  coatcolor # 型注釈を省略した場合、自動的に :: Any として扱われます。
end

# 型を定義すると、その型のプロパティすべてを、定義した順番に
# 引数として持つデフォルトコンストラクタが自動的に作られます。
tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange")

# 型名がそのままコンストラクタ名（関数名）となります。
sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire")

# このような、構造体スタイルの型は、具体型(concrete type)と呼ばれます。
# 具体型はインスタンス化可能ですが、派生型(subtype)を持つことができません。
# 具体型の他には抽象型(abstract type)があります。

# abstract Name
abstract Cat # 型の階層図の途中の一点を指し示す名前となります。

# 抽象型はインスタンス化できませんが、派生型を持つことができます。
# 例えば、 Number は以下の派生型を持つ抽象型です。
subtypes(Number) # => 6-element Array{Any,1}:
                 #     Complex{Float16}
                 #     Complex{Float32}
                 #     Complex{Float64}
                 #     Complex{T<:Real}
                 #     ImaginaryUnit
                 #     Real
subtypes(Cat) # => 0-element Array{Any,1}

# すべての型は、直接的にはただひとつの基本型(supertype) を持ちます。
# super 関数でこれを取得可能です。
typeof(5) # => Int64
super(Int64) # => Signed
super(Signed) # => Real
super(Real) # => Number
super(Number) # => Any
super(super(Signed)) # => Number
super(Any) # => Any
# Int64 を除き、これらはすべて抽象型です。

# <: は派生形を表す演算子です。
# これを使うことで派生型を定義できます。
type Lion <: Cat # Lion は 抽象型 Cat の派生型
  mane_color
  roar::String
end

# 型名と同じ名前の関数を定義し、既に存在するコンストラクタを呼び出して、
# 必要とする型の値を返すことによって、
# デフォルトコンストラクタ以外のコンストラクタを作ることができます。

Lion(roar::String) = Lion("green",roar)
# 型定義の外側で定義されたコンストラクタなので、外部コンストラクタと呼ばれます。

type Panther <: Cat # Panther も Cat の派生型
  eye_color
  Panther() = new("green")
  # Panther は内部コンストラクタとしてこれのみを持ち、
  # デフォルトコンストラクタを持たない
end
# 内部コンストラクタを使うことで、どのような値が作られるのかをコントロールすることができます。
# 出来る限り、外部コンストラクタを使うべきです。

####################################################
## 6. 多重ディスパッチ
####################################################

# Julia では、すべての名前付きの関数は総称的関数(generic function) です。
# これは、関数はいくつかの細かいメソッドの集合である、という意味です。
# 例えば先の Lion 型のコンストラクタ Lion は、Lion という関数の1つのメソッドです。

# コンストラクタ以外の例をみるために、新たに meow 関数を作りましょう。

# Lion, Panther, Tiger 型それぞれに対する meow 関数のメソッド定義
function meow(animal::Lion)
  animal.roar # 型のプロパティには . でアクセスできます。
end

function meow(animal::Panther)
  "grrr"
end

function meow(animal::Tiger)
  "rawwwr"
end

# meow 関数の実行
meow(tigger) # => "rawwr"
meow(Lion("brown","ROAAR")) # => "ROAAR"
meow(Panther()) # => "grrr"

# 型の階層関係を見てみましょう
issubtype(Tiger,Cat) # => false
issubtype(Lion,Cat) # => true
issubtype(Panther,Cat) # => true

# 抽象型 Cat の派生型を引数にとる関数
function pet_cat(cat::Cat)
  println("The cat says $(meow(cat))")
end

pet_cat(Lion("42")) # => prints "The cat says 42"
try
    pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,)
catch e
    println(e)
end

# オブジェクト指向言語では、一般的にシングルディスパッチが用いられます。
# つまり、関数に複数あるメソッドのうちにどれが呼ばれるかは、
# その第一引数（もしくは、 . や -> の前にある値の型）によってのみ決定されます。
# 一方でJulia では、すべての引数の型が、このメソッド決定に寄与します。

# 多変数関数を定義して、この辺りを見て行きましょう。
function fight(t::Tiger,c::Cat)
  println("The $(t.coatcolor) tiger wins!")
end
# => fight (generic function with 1 method)

fight(tigger,Panther()) # => prints The orange tiger wins!
fight(tigger,Lion("ROAR")) # => prints The orange tiger wins!

# 第二引数の Cat が実際は Lion だった時に、挙動が変わるようにします。
fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!")
# => fight (generic function with 2 methods)

fight(tigger,Panther()) # => prints The orange tiger wins!
fight(tigger,Lion("ROAR")) # => prints The green-maned lion wins!

# 別に Tiger だけが戦う必要もないですね。
fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))")
# => fight (generic function with 3 methods)

fight(Lion("balooga!"),Panther()) # => prints The victorious cat says grrr
try
  fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion)
catch
end

# 第一引数にも Cat を許しましょう。
fight(c::Cat,l::Lion) = println("The cat beats the Lion")
# => Warning: New definition
#    fight(Cat,Lion) at none:1
# is ambiguous with
#    fight(Lion,Cat) at none:2.
# Make sure
#    fight(Lion,Lion)
# is defined first.
#fight (generic function with 4 methods)

# 警告が出ましたが、これは次の対戦で何が起きるのかが不明瞭だからです。
fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The victorious cat says rarrr
# Julia のバージョンによっては、結果が違うかもしれません。

fight(l::Lion,l2::Lion) = println("The lions come to a tie")
fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The lions come to a tie


# Julia が生成する LLVM 内部表現や、アセンブリを調べることもできます。

square_area(l) = l * l      # square_area (generic function with 1 method)

square_area(5) #25

# square_area に整数を渡すと何が起きる？
code_native(square_area, (Int32,))  
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1              # Prologue
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    movsxd  RAX, EDI        # l を取得
	#	    imul    RAX, RAX        # l*l を計算して RAX に入れる
	#	    pop RBP                 # Base Pointer を元に戻す
	#	    ret                     # 終了。RAX の中身が結果

code_native(square_area, (Float32,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vmulss  XMM0, XMM0, XMM0  # 単精度浮動小数点数演算 (AVX)
	#	    pop RBP
	#	    ret

code_native(square_area, (Float64,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vmulsd  XMM0, XMM0, XMM0 # 倍精度浮動小数点数演算 (AVX)
	#	    pop RBP
	#	    ret
	#	

# Julia では、浮動小数点数と整数との演算では
# 自動的に浮動小数点数用の命令が生成されることに注意してください。
# 円の面積を計算してみましょう。
circle_area(r) = pi * r * r     # circle_area (generic function with 1 method)
circle_area(5)                  # 78.53981633974483

code_native(circle_area, (Int32,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vcvtsi2sd   XMM0, XMM0, EDI          # Load integer (r) from memory
	#	    movabs  RAX, 4593140240              # Load pi
	#	    vmulsd  XMM1, XMM0, QWORD PTR [RAX]  # pi * r
	#	    vmulsd  XMM0, XMM0, XMM1             # (pi * r) * r
	#	    pop RBP
	#	    ret
	#

code_native(circle_area, (Float64,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	    movabs  RAX, 4593140496
	#	Source line: 1
	#	    vmulsd  XMM1, XMM0, QWORD PTR [RAX]
	#	    vmulsd  XMM0, XMM1, XMM0
	#	    pop RBP
	#	    ret
	#	
```

## より勉強するために

[公式ドキュメント](http://docs.julialang.org/en/latest/manual/) (英語)にはより詳細な解説が記されています。

Julia に関して助けが必要ならば、[メーリングリスト](https://groups.google.com/forum/#!forum/julia-users) が役に立ちます。
みんな非常に親密に教えてくれます。

---
language: PHP
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
translators:
    - ["Kazushige Tominaga", "https://github.com/kazu9su"]
filename: learnphp-jp.php
lang: ja-jp
---

このドキュメントでは、 PHP 5+ について説明します。

```php
<?php // PHPのコードは、<?php タグで囲む必要があります。

// もしあなたのphpファイルがPHPのみで構成される場合、
// 意図しない出力を防ぐために、phpの閉じタグは省略するのがベストプラクティスです。
// 一行のコメントを書く場合、2つのスラッシュで始めます。
# ハッシュ(ポンド記号として知られる)を使いたいでしょうが、//のほうが一般的です
/*
    スラッシュとアスタリスク、アスタリスクとスラッシュで囲むと、
    複数行のコメントを書けます。
*/

// 出力をプリントしたい場合は、"echo" か "print" を使います。
print('Hello '); // これは "Hello " という改行なしの文字列をプリントします。

// カッコ()はprintとecho関数に置いては省略できます。
echo "World\n"; // これは改行ありの"World"という文字列をプリントします。
// (全ての命令文では、セミコロンを最後に付けることが必須です。)

// <?php タグの外側にあるものは、自動的にプリントされます。
?>
Hello World Again!
<?php


/************************************
 * 型と変数について
 */

// 変数は"$"マークで始まります
// 有効な変数名にするには、文字またはアンダースコア(_)で始めて,
// その後はどんな数字でも、文字でも、アンダースコアで続けても構いません

//ブーリアン値は大文字、小文字問いません
$boolean = true;  // or TRUE or True
$boolean = false; // or FALSE or False

// 数値
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (先頭の0は8進法を示す)
$int4 = 0x0F; // => 15 (先頭の0xは16進法を示す)

// floats(浮動小数) (別名double)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// 変数の削除
unset($int1);

// 計算式
$sum        = 1 + 1; // 2
$difference = 2 - 1; // 1
$product    = 2 * 2; // 4
$quotient   = 2 / 1; // 2

// 式の省略
$number = 0;
$number += 1;      // $numberに1加算Increment $number by 1
echo $number++;    // 1 がプリントされる(式の評価の後に加算される)
echo ++$number;    // 3 がプリントされる(式の評価の前に加算される)
$number /= $float; // 割り算した結果の商を$numberに割り当てる

// 文字列はシングルクォートで囲むのが望ましいです
$sgl_quotes = '$String'; // => '$String'

// 文字列中に、他の変数を埋め込みたい場合以外は、ダブルクォートを使用するのはやめましょう
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// Special characters are only escaped in double quotes
// 特殊文字はダブルクォートによってのみ、エスケープされます
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// 必要があれば、変数を波括弧で囲みます
$money = "I have $${number} in the bank.";

// PHP 5.3から、nowdocs形式が変数の挿入をしない複数行の文字列の定義に使用できます
$nowdoc = <<<'END'
Multi line
string
END;

// ヒアドキュメント形式なら、文字列中に変数の挿入を行えます。
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// 文字列の連結は . で行います
echo 'This string ' . 'is concatenated';

// 別々のパラメータとしてechoに渡すこともできます
echo 'Multiple', 'Parameters', 'Valid';

/********************************
 * 定数
 */

// 定数は define() を使って定義します
// また、実行中は変更することができないので注意が必要です！

// 有効は定数は文字かアンダースコアで始めます
// それ移行のは、どんな数値でも文字列でもアンダースコアでも構いません
define("FOO",     "something");

// 定義した名前をそのまま($はつけずに)使用することで、定数にアクセスできます
// access to a constant is possible by direct using the chosen name
echo 'This outputs '.FOO;


/********************************
 * 配列
 */

// PHPの配列はすべて連想配列です

// 連想配列は、他の言語ではハッシュ(ハッシュマップ)として知られています

// すべてのバージョンのPHPで動作します
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);

// PHP 5.4 から、新しいシンタックスが導入されました
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];

echo $associative['One']; // 1とプリントされます

// キーを指定しないシンプルな配列にも、自動的に数値キーが振られます
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"

// 配列の最後に要素を追加する
$array[] = 'Four';
// または、次のようにも書けます
array_push($array, 'Five');

// 配列から要素を削除
unset($array[3]);

/********************************
 * 出力
 */

echo('Hello World!');
// 標準出力にHello World! とプリントします
// 標準出力はブラウザーで実行していればWebページに出力されます
// Stdout is the web page if running in a browser.

print('Hello World!'); // echoの結果と同じです

// echo は言語自体の構成要素であり、括弧なしで呼び出せます
// echo is actually a language construct, so you can drop the parentheses.
echo 'Hello World!';
print 'Hello World!'; // printも同様です

$paragraph = 'paragraph';

echo 100;        // スカラー数値を直接出力します
echo $paragraph; // 変数も使用できます

// PHPタグの短縮型が設定されているか、使用しているPHPのバージョンが
// 5.4.0 以上であれば、短縮echoシンタックスを使用できます
?>
<p><?= $paragraph ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $xに$yの値を代入します
$z = &$y;
// $zは$yへの参照です。
// $zの値を変更すると$yの値も変更されるでしょう。逆も同様です。
// $xは$yの最初の値を変わらず保持しています

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0

// 変数の型と値を標準出力へダンプします
var_dump($z); // int(0) と出力されます

// 人間が読めるフォーマットで変数を標準出力にプリントします
print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three )

/********************************
 * ロジック
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// assertは引数がfalseの場合、Exceptionを投げます

//これらの比較は型が違ったとしても、常に真です。
assert($a == $b); // equality
assert($c != $a); // inequality
assert($c <> $a); // alternative inequality
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// 次の比較は値が等しく、かつ同じ型である場合のみ真です
assert($c === $d);
assert($a !== $d);
assert(1 === '1');
assert(1 !== '1');

// spaceship演算子はPHP7から使用可能です
$a = 100;
$b = 1000;

echo $a <=> $a; // 等しいので0になります
echo $a <=> $b; // $a < $b なので -1 です
echo $b <=> $a; // $b > $a なので 1 です

// 変数は使用するコンテキストによって、変換されます

$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2 (文字列は強制的に数値として処理されます)

$string = 'one';
echo $string + $string; // => 0
// '+'演算子は文字列'one'を数値にキャストできないので、0と出力されます

// 型のキャスティングによって、変数を指定したもう一つの型として扱うことができます
// Type casting can be used to treat a variable as another type

$boolean = (boolean) 1; // => true

$zero = 0;
$boolean = (boolean) $zero; // => false

// 型をキャストするため専用の関数も存在します
$integer = 5;
$string = strval($integer);

$var = null; // Null値


/********************************
 * 制御構造
 */

if (true) {
    print 'I get printed';
}

if (false) {
    print 'I don\'t';
} else {
    print 'I get printed';
}

if (false) {
    print 'Does not get printed';
} elseif(true) {
    print 'Does';
}

// 三項演算子
print (false ? 'Does not get printed' : 'Does');

// PHP 5.3から、三項演算子の短縮形が使用できます
// $x ? $x : 'Does'と同義です
$x = false;
print($x ?: 'Does');

// null合体演算子はPHP 7から使用できます
$a = null;
$b = 'Does print';
echo $a ?? 'a is not set'; // prints 'a is not set'
echo $b ?? 'b is not set'; // prints 'Does print'


$x = 0;
if ($x === '0') {
    print 'Does not print';
} elseif($x == '1') {
    print 'Does not print';
} else {
    print 'Does print';
}



// :を用いる別の構文はテンプレートで有用です
?>

<?php if ($x): ?>
この部分はifが真のとき表示されます
<?php else: ?>
それ以外の場合は、この部分が表示されます
<?php endif; ?>

<?php

// いくつかのロジックを保存するにはswitchを使用します
switch ($x) {
    case '0':
        print 'Switch does type coercion';
        break; // breakを書く必要があります。
               // でなければ、次の'two', 'three'のcase文を続けて実行することになります。
    case 'two':
    case 'three':
        // 変数が'two'または'three'の場合、何かを実行します
        break;
    default:
        //デフォルトで何かを実行します
}

// while, do,  forの構文は、おそらく他の言語とも共通なものです
$i = 0;
while ($i < 5) {
    echo $i++;
}; // Prints "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // Prints "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // Prints "0123456789"

echo "\n";

$wheels = ['bicycle' => 2, 'car' => 4];

//Foreachループによって、 配列を反復処理できます
foreach ($wheels as $wheel_count) {
    echo $wheel_count;
} // Prints "24"

echo "\n";

// 値と同じ様に、keyも反復処理できます
foreach ($wheels as $vehicle => $wheel_count) {
    echo "A $vehicle has $wheel_count wheels";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // Exit out of the while loop
    }
    echo $i++;
} // Prints "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // Skip this iteration of the loop
    }
    echo $i;
} // Prints "0124"


/********************************
 * 関数
 */

// 関数を"function"で定義します
function my_function () {
    return 'Hello';
}

echo my_function(); // => "Hello"

// 有効な関数名は、文字またはアンダースコアで始めます。それ以降は
// どれだけ長い文字、数値、アンダースコアを続けても構いません

function add ($x, $y = 1) { // $yはオプショナルな値であり、デフォルトで 1 です
    $result = $x + $y;
    return $result;
}

echo add(4); // => 5
echo add(4, 2); // => 6

// $result には、関数の外からアクセス出来ません
// print $result; // エラーになります

// PHP 5.3 から、無名関数が使えます
$inc = function ($x) {
    return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
    echo "$x - $y - $z";
}

// 関数は、関数を返すことができます
function bar ($x, $y) {
    // 関数外の変数を利用したいときは、'use'を使います
    return function ($z) use ($x, $y) {
        foo($x, $y, $z);
    };
}

$bar = bar('A', 'B');
$bar('C'); // Prints "A - B - C"

// 文字列を使って、定義済みの関数を呼び出すことができます
$function_name = 'add';
echo $function_name(1, 2); // => 3

// プログラミング中に、動的に動かす関数を決める場合に便利です。
// もしくは、call_user_func(callable $callback [, $parameter [, ... ]]) を使っても同じことができます


// 特に指定しなくても、渡された引数を受け取ることもできます
function parameters() {
    $numargs = func_num_args();
    if ($numargs > 0) {
        echo func_get_arg(0) . ' | ';
    }
    $args_array = func_get_args();
    foreach ($args_array as $key => $arg) {
        echo $key . ' - ' . $arg . ' | ';
    }
}

parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |

/********************************
 * ファイルの読み込み
 */

<?php
// 読み込まれたファイル中のPHPは、同じくPHPのオープンタグで始める必要があります

include 'my-file.php';
// my-file.php中のコードは、現在のスコープの中で使用可能です
// もしファイルを読み込めなければ (例:file not found)、警告が発せられます

include_once 'my-file.php';
// my-file.phpのコードが既にどこかで読み込まれていれば、
// ファイルを読み込みません。これは、クラスの多重定義のエラーを防ぎます

require 'my-file.php';
require_once 'my-file.php';
// include()と同じように、require()はもしファイルを読み込むことができなければ、
// 致命的エラーの原因となります

// my-include.phpの内容
<?php

return 'Anything you like.';
// End file

// include()とrequire()は一つの値を返します
$value = include 'my-include.php';

// ファイルは与えられたファイルパスを基に読み込まれます。
// ファイルパスを指定しない場合は、include_path の設定を利用します。
// もしファイルがinclude_path中に見つからない場合は、
// 呼び出し元スクリプトのディレクトリと作業ディレクトリの中も探します。
// それでも見つからない場合、失敗します。
/* */

/********************************
 * クラス
 */

// クラスはclassキーワードで定義します

class MyClass
{
    const MY_CONST      = 'value'; // クラス定数です

    static $staticVar   = 'static';

    // スタティック変数とアクセス制限
    public static $publicStaticVar = 'publicStatic';
    // クラス内でのみアクセス可能
    private static $privateStaticVar = 'privateStatic';
    // そのクラスと子クラスで参照可能
    protected static $protectedStaticVar = 'protectedStatic';

    // プロパティはアクセス制限を宣言する必要があります
    public $property    = 'public';
    public $instanceProp;
    protected $prot = 'protected'; // そのクラスと子クラスで参照可能
    private $priv   = 'private';   // クラス内でのみアクセス可能

    // __constructでコンストラクターを生成します
    public function __construct($instanceProp) {
        // $thisでインスタンス変数にアクセスします
        $this->instanceProp = $instanceProp;
    }

    // メソッドはクラス内で関数として定義されます
    public function myMethod()
    {
        print 'MyClass';
    }

    // finalキーワードは関数の上書きを禁止します
    final function youCannotOverrideMe()
    {
    }

/*
 * クラスプロパティまたはメソッドをstaticとして作成すれば、
 * クラスをインスタンス化(newすること)しなくてもアクセスできます。
 * プロパティをstaticとして定義すると、
 * インスタンス化されたクラスオブジェクトを通してのアクセスはできなくなります。
 */

    public static function myStaticMethod()
    {
        print 'I am static';
    }
}

// クラス定数は、いつでも静的にアクセスできます。
echo MyClass::MY_CONST;    // Outputs 'value';

echo MyClass::$staticVar;  // Outputs 'static';
MyClass::myStaticMethod(); // Outputs 'I am static';

// クラスをインスタンス化するには、newを使います。
$my_class = new MyClass('An instance property');
// 括弧はもし引数を渡す必要がなければ省略可能です。

// ->を使ってクラスのメンバにアクセスします。
echo $my_class->property;     // => "public"
echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod();        // => "MyClass"


// extendsを使用してクラスを継承します。
class MyOtherClass extends MyClass
{
    function printProtectedProperty()
    {
        echo $this->prot;
    }

    // メソッドを上書きします。
    function myMethod()
    {
        parent::myMethod();
        print ' > MyOtherClass';
    }
}

$my_other_class = new MyOtherClass('Instance prop');
$my_other_class->printProtectedProperty(); // => Prints "protected"
$my_other_class->myMethod();               // Prints "MyClass > MyOtherClass"

final class YouCannotExtendMe
{
}

// 「マジックメソッド」を使ってゲッターとセッターを生成できます。
class MyMapClass
{
    private $property;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MyMapClass();
echo $x->property; // __get() メソッドを使用します
$x->property = 'Something'; // __set() メソッドを使用します

// クラスは抽象クラスにもできます(abstractキーワードを使用します)し、
// インターフェースを実装することもできます(implementsキーワードを使用します)。
// インターフェースはinterfaceキーワードで定義します。

interface InterfaceOne
{
    public function doSomething();
}

interface InterfaceTwo
{
    public function doSomethingElse();
}

// インターフェースは継承することができます
interface InterfaceThree extends InterfaceTwo
{
    public function doAnotherContract();
}

abstract class MyAbstractClass implements InterfaceOne
{
    public $x = 'doSomething';
}

class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
{
    public function doSomething()
    {
        echo $x;
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


// クラスは1つ以上のインターフェースを実装できます。
class SomeOtherClass implements InterfaceOne, InterfaceTwo
{
    public function doSomething()
    {
        echo 'doSomething';
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


/********************************
 * トレイト
 */

// トレイトはPHP 5.4.0 以上で使用可能で、traitキーワードで定義します。

trait MyTrait
{
    public function myTraitMethod()
    {
        print 'I have MyTrait';
    }
}

class MyTraitfulClass
{
    use MyTrait;
}

$cls = new MyTraitfulClass();
$cls->myTraitMethod(); // Prints "I have MyTrait"


/********************************
 *  名前空間
 */

// このセクションは名前空間の定義はファイルの先頭で宣言される必要があるため、
// 独立しています。
// そのケースには当てはまらないふりをして続けましょう。

<?php

// デフォルトでは、クラスはグローバルな名前空間に存在し、
// バックスラッシュによって明確にコールできます。

$cls = new \MyClass();



// ファイルに名前空間をセットします
namespace My\Namespace;

class MyClass
{
}

// (別のファイルからの呼び出し)
$cls = new My\Namespace\MyClass;

// 異なる名前空間からの呼び出し
namespace My\Other\Namespace;

use My\Namespace\MyClass;

$cls = new MyClass();

// 名前空間に別名をつけることもできます

namespace My\Other\Namespace;

use My\Namespace as SomeOtherNamespace;

$cls = new SomeOtherNamespace\MyClass();

/**********************
*  エラーハンドリング
*  
*/

// シンプルなエラーハンドリングは、try catchを使えば行えます

try {
    // 処理を実行します
} catch ( Exception $e) {
    // 例外を処理します
}

// try catchを名前空間を持った環境で使用するときは、次のようにします。

try { 
    // Do something
    // 処理を実行します
} catch (\Exception $e) { 
    // 例外を処理します
}

// 例外のカスタマイズ

class MyException extends Exception {}

try {
    
    $condition = true; 
    
    if ($condition) {
        throw new MyException('Something just happend');
    }
    
} catch (MyException $e) {
    // Handle my exception
}

```

## より詳しい情報

リファレンスを見るため、またコミュニティへの情報提供のために、 [PHP公式ドキュメント](http://www.php.net/manual/) を訪れてみてください。

もし最新のベストプラクティスに興味があるなら、
[PHP The Right Way](http://www.phptherightway.com/)を訪れてみてください。


もしあなたがよいパッケージマネジメント・システムを持つ言語で開発経験があるのなら、
[Composer](http://getcomposer.org/)も確かめてみてください。


共通基準を知るためには、PHP Framework Interoperability Groupの
[PSR standards](https://github.com/php-fig/fig-standards)にも訪れてみてください。
---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
    - ["evuez", "http://github.com/evuez"]
translators:
    - ["kakakaya", "https://github.com/kakakaya"]
filename: learnpython3-jp.py
lang: ja-jp
---

90年代の初め、Guido Van RossumによってPythonは作成されました。現在となっては、最も有名な言語の1つです。
私は構文の明快さによって、Pythonと恋に落ちました。
以下は基本的に実行可能な疑似コードです。

フィードバッグは大歓迎です! [@louiedinh](http://twitter.com/louiedinh) または louiedinh [at] [google's email service] にご連絡下さい!

Note: この記事はPython 3に内容を絞っています。もし古いPython 2.7を学習したいなら、 [こちら](http://learnxinyminutes.com/docs/python/) をご確認下さい。

```python

# 1行のコメントは番号記号(#)から始まります。

""" 複数行の文字は、"を3つ繋げることで
    書くことができます。
    また、これはコメントとしてもよく使われます。
"""

####################################################
# 1. プリミティブ型と演算子
####################################################

# 数字です
3                               # => 3

# 四則演算はあなたの期待通りに動きます。
1 + 1                           # => 2
8 - 1                           # => 7
10 * 2                          # => 20
35 / 5                          # => 7.0

# 整数除算の結果は、正負に関わらず小数の切り捨てが行われます。
5 // 3                          # => 1
5.0 // 3.0                      # => 1.0 # 浮動小数点でも同様に動作します。
-5 // 3                         # => -2
-5.0 // 3.0                     # => -2.0

# 除算の結果は常に浮動小数点になります。
10.0 / 3                        # => 3.3333333333333335

# 剰余の計算
7 % 3                           # => 1

# 冪乗 (x**y, x の y 乗)
2**4                            # => 16

# 括弧により、計算の順番を優先させられます。
(1 + 3) * 2                     # => 8

# 真偽値はプリミティブ型です(大文字から始まっていることに注意!)
True
False

# not で真偽を反転させられます。
not True                        # => False
not False                       # => True

# ブール演算
# 注意: "and" と "or" は小文字です
True and False                  # => False
False or True                   # => True

# 整数でブール演算をするときのメモ
0 and 2                         # => 0
-5 or 0                         # => -5
0 == False                      # => True
2 == True                       # => False
1 == True                       # => True

# 値が等しいか確認するには ==
1 == 1                          # => True
2 == 1                          # => False

# 値が等しくないか確認するには !=
1 != 1                          # => False
2 != 1                          # => True

# 他の比較方法
1 < 10                          # => True
1 > 10                          # => False
2 <= 2                          # => True
2 >= 2                          # => True

# 比較は連結させられます!
1 < 2 < 3                       # => True
2 < 3 < 2                       # => False

# (is vs. ==)
# "is" は、2つの変数が同一のオブジェクトを参照しているか確認します。
# 一方 "==" は、それぞれが参照する2つのオブジェクトが同じ値を持つか確認します。
a = [1, 2, 3, 4]     # a は新しいリストの [1, 2, 3, 4] を指します。
b = a                # b は a が指すリストを指します。
b is a               # => True, a と b は同一のオブジェクトを参照しています。
b == a               # => True, a と b が参照するオブジェクトの値は等しいです。
b = [1, 2, 3, 4]     # b は新しいリストの [1, 2, 3, 4] を指します。
b is a               # => False, a と b は別々のオブジェクトを参照しています。
b == a               # => True, a と b が参照するオブジェクトの値は等しいです。

# " または ' を使って文字列を作成します。
"This is a string."
'This is also a string.'

# 文字列も加算をすることができます!でも、あまり行わないように。
"Hello " + "world!"             # => "Hello world!"
# '+' を使わなくても連結はできます。
"Hello " "world!"               # => "Hello world!"

# 文字列は文字のリストであるかのように扱うことができます。
"This is a string"[0]           # => 'T'

# 文字列の長さを得るにはこのようにします。
len("This is a string")         # => 16

# .format で文字列のフォーマットを行えます
"{} can be {}".format("Strings", "interpolated")  # => "Strings can be interpolated"

# 入力を減らすために、フォーマットするときに引数を繰り返し使うことができます。
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
# => "Jack be nimble, Jack be quick, Jack jump over the candle stick"

# 引数の順番を数えるのがお嫌い？キーワード引数をどうぞ。
"{name} wants to eat {food}".format(name="Bob", food="lasagna")  # => "Bob wants to eat lasagna"

# もし Python 3 のコードを Python 2.5以下でも使う必要があるなら、
# 旧式のフォーマット方法を使うこともできます。
"%s can be %s the %s way" % ("Strings", "interpolated", "old")  # => "Strings can be interpolated the old way"


# None はオブジェクトです(大文字からです!)
None                            # => None

# オブジェクトがNoneであるか確認するのに "==" 演算子を使わないように。
# 代わりに "is" を使いましょう。オブジェクトの素性を確認できます。
"etc" is None                   # => False
None is None                    # => True

# None や 0 、空の 文字列/リスト/辞書/タプル は全て False として評価されます。
# 他の全ての値は True になります。
bool(0)                         # => False
bool("")                        # => False
bool([])                        # => False
bool({})                        # => False
bool(())                        # => False

####################################################
# 2. Variables and Collections
####################################################

# Python にはprint関数があります。
print("I'm Python. Nice to meet you!")  # => I'm Python. Nice to meet you!

# 標準では、print関数は最後に改行を出力します。
# この動作を変更するためには、オプション引数を利用します。
print("Hello, World", end="!")  # => Hello, World!

# コンソールから入力を得るための簡単な例
input_string_var = input("Enter some data: ")  # 入力を文字列として返します
# Note: Python の初期のバージョンでは、 input() は raw_input() という名前で存在します。

# 変数に代入する前に宣言する必要はありません。
# 慣例的に、小文字でアンダースコア区切り ( lower_case_with_underscores ) の変数が使われます。
some_var = 5
some_var                        # => 5

# 代入されていない変数へのアクセスは例外を引き起こします。
# 例外の取り扱いについては、3章の制御の流れをご確認ください。
some_unknown_var                # NameError を送出します

# ifは式として使用できます。
# C言語の「?:(三項演算子)」に対応する例:
"yahoo!" if 3 > 2 else 2        # => "yahoo!"

# リストは順序を保存します。
li = []
# 値の入っているリストも作成できます。
other_li = [4, 5, 6]

# append により、リストの末尾にものを入れられます。
li.append(1)                    # li is now [1]
li.append(2)                    # li is now [1, 2]
li.append(4)                    # li is now [1, 2, 4]
li.append(3)                    # li is now [1, 2, 4, 3]
# pop でリストの末尾から取り除けます。
li.pop()                        # => 3 and li is now [1, 2, 4]
# 元に戻しましょう!
li.append(3)                    # li is now [1, 2, 4, 3] again.

# 配列のように、リストにアクセスできます。
li[0]                           # => 1
# 最後の要素を参照できます。
li[-1]                          # => 3

# 範囲外の要素を参照すると IndexError になります。
li[4]                           # IndexError が発生します

# スライス構文により範囲を参照できます。
li[1:3]                         # => [2, 4]
# 先端を取り除く
li[2:]                          # => [4, 3]
# 末尾を取り除く
li[:3]                          # => [1, 2, 4]
# 1つ飛ばしで選択する
li[::2]                         # =>[1, 4]
# 反転したリストを得る
li[::-1]                        # => [3, 4, 2, 1]
# これらの任意の組み合わせにより、より複雑なスライスを作ることができます。
# li[start:end:step]

# スライスにより、深いコピーを1階層分行うことができます。
li2 = li[:]          # => li2 = [1, 2, 4, 3] だが、 (li2 is li) はFalseになる。

# "del"によりリストから任意の要素を削除できます。
del li[2]                       # li は [1, 2, 3] になりました。

# "remove"で最初に出現する要素を削除できます。
li.remove(2)                    # li は [1, 3] になりました。
li.remove(2)                    # 2はリストの中に存在しないので、 ValueError が発生します。

# 要素を好きなところに挿入できます。
li.insert(1, 2)                 # li は [1, 2, 3] に戻りました。

# "index"で引数の要素が最初に出現する場所のインデックスを得られます。
li.index(2)                     # => 1
li.index(4)                     # 4はリストの中に存在しないので、 ValueError が発生します。

# リスト同士を足すこともできます。
# Note: li と other_li の値は変更されません。
li + other_li                   # => [1, 2, 3, 4, 5, 6]

# "extend()"で他のリストを連結することができます。
li.extend(other_li)             # li は [1, 2, 3, 4, 5, 6] になります。

# リストの中に値が存在するか、 "in" で確認できます。
1 in li                         # => True

# 長さは "len()" で確認できます。
len(li)                         # => 6


# タプルはリストのようなものですが、不変であるという違いがあります。
tup = (1, 2, 3)
tup[0]                      # => 1
tup[0] = 3                  # 内容を変更しようとすると TypeError が発生します。

# 長さが1のタプルを作成するには、要素の後にカンマを付ける必要があります。
# しかし、それ以外の長さなら、例え長さが0でもそのようにする必要はありません。
type((1))                       # => <class 'int'>
type((1,))                      # => <class 'tuple'>
type(())                        # => <class 'tuple'>

# 大抵のリスト操作はタプルでも行うことができます。
len(tup)                        # => 3
tup + (4, 5, 6)                 # => (1, 2, 3, 4, 5, 6)
tup[:2]                         # => (1, 2)
2 in tup                        # => True

# タプルやリストから複数の変数に代入することができます。
a, b, c = (1, 2, 3)             # a, b, c にはそれぞれ 1, 2, 3 が代入されました。
# 拡張記法もあります。
a, *b, c = (1, 2, 3, 4)         # a は 1 、 b は [2, 3] 、c は4 になります。
# 括弧を作成しなくてもデフォルトでタプルが作成されます。
d, e, f = 4, 5, 6
# 2つの変数を交換するのがどれほど簡単か見てみましょう。
e, d = d, e                     # d は 5 、 e は e になります。


# 辞書はマップ(キーと値の組み合わせ)を保存できます。
empty_dict = {}
# 値が入っている辞書を直接作成することもできます。
filled_dict = {"one": 1, "two": 2, "three": 3}

# キーは不変の型である必要があります。
# これは、高速化のため、キーを定数のハッシュ値に変換できるようにするためです。
# 不変の型の例として、int、float、string、tupleなどが上げられます。
invalid_dict = {[1, 2, 3]: "123"}  # => list はハッシュ化できないので、 TypeError が発生します。
valid_dict = {(1, 2, 3): [1, 2, 3]}  # 一方、キーに対応する値はどのような型でも利用できます。

# [] で 値を取り出せます。
filled_dict["one"]              # => 1

# "keys()"により、全てのキーを反復可能な形式で取り出せます。
# これをリストにするために、"list()"で囲んでいます。これについては後程解説します。
# Note: 辞書のキーの順番は考慮されていません。実行した結果がこれと異なる場合があります。
list(filled_dict.keys())        # => ["three", "two", "one"]

# "values()"により、全ての値を反復可能な形式で取り出せます。
# 前と同じように、これをリストにするために、"list()"で囲んでいます。
# Note: 辞書の値の順番は考慮されていません。実行した結果がこれと異なる場合があります。
list(filled_dict.values())      # => [3, 2, 1]


# "in" により、辞書のキーが存在するか確認できます。
"one" in filled_dict            # => True
1 in filled_dict                # => False

# 存在しないキーで辞書を参照すると KeyError になります。
filled_dict["four"]             # KeyError

# "get()" メソッドを使うことで KeyError を回避できます。
filled_dict.get("one")          # => 1
filled_dict.get("four")         # => None
# get ではキーが存在しなかったときのデフォルト値を指定できます。
filled_dict.get("one", 4)       # => 1
filled_dict.get("four", 4)      # => 4

# "setdefault()" で、キーが存在しなかった場合のみ、値を設定できます。
filled_dict.setdefault("five", 5)  # filled_dict["five"] は 5 になりました。
filled_dict.setdefault("five", 6)  # filled_dict["five"] は 5 のままです。

# 辞書にマップを追加する
filled_dict.update({"four": 4})  # => {"one": 1, "two": 2, "three": 3, "four": 4}
# filled_dict["four"] = 4        # 辞書に追加する別の方法

# del により辞書からキーを削除できます。
del filled_dict["one"]          # "one" キーを辞書から削除します。

# Python 3.5 以降では、追加の値を取り出す方法があります。
{'a': 1, **{'b': 2}}            # => {'a': 1, 'b': 2}
{'a': 1, **{'a': 2}}            # => {'a': 2}


# set では集合を表現できます。
empty_set = set()
# 集合を一連の値で初期化する例です。辞書に似ていますね？ごめんなさい。
some_set = {1, 1, 2, 2, 3, 4}   # some_set is now {1, 2, 3, 4}

# 辞書のキーのように、集合の値は不変である必要があります。
invalid_set = {[1], 1}         # => list はハッシュ化できないので、 TypeError が送出されます。
valid_set = {(1,), 1}

# 新しい値を集合にセットできます。
filled_set = some_set

# 集合に新しい要素を追加できます。
filled_set.add(5)               # filled_set は {1, 2, 3, 4, 5} になりました。

# & により、集合同士の共通部分が得られます。
other_set = {3, 4, 5, 6}
filled_set & other_set          # => {3, 4, 5}

# | により、集合同士の合併が得られます。
filled_set | other_set          # => {1, 2, 3, 4, 5, 6}

# - により、集合同士の差集合が得られます。
{1, 2, 3, 4} - {2, 3, 5}        # => {1, 4}

# ^ により、集合同士の対象差が得られます。
{1, 2, 3, 4} ^ {2, 3, 5}        # => {1, 4, 5}

# 左の集合が右の集合の上位集合であるか確認。
{1, 2} >= {1, 2, 3}             # => False

# 左の集合が右の集合の部分集合であるか確認。
{1, 2} <= {1, 2, 3}             # => True

# in により値が集合の中に存在するか確認できます。
2 in filled_set                 # => True
10 in filled_set                # => False


####################################################
# 3. 制御の流れとiterable
####################################################

# まずは変数を作りましょう。
some_var = 5

# これはif文です。インデントがPythonでは特徴的ですね!
# 以下の例では"some_var is smaller than 10"と出力されます。
if some_var > 10:
    print("some_var is totally bigger than 10.")
elif some_var < 10:             # この elif 節はオプションです。
    print("some_var is smaller than 10.")
else:                           # この else 節もオプションです。
    print("some_var is indeed 10.")


"""
for ループはリストの要素を反復することができます。
出力:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # format() を使って文字列に変数を挿入して出力できます。
    print("{} is a mammal".format(animal))

"""
"range(数値)" は、ゼロから与えられた数値までのiterableを返します。
出力:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
"range(lower, upper)" は、 lower の数値から upper の数値までのiterableを返します。
upper の数値は含まれません。
出力:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print(i)

"""
"range(lower, upper, step)" は、lower の数値から upper の数値までが、
step 刻みで表現されるiterableを返します
step が与えられない場合、デフォルトは1になります。
出力:
    4
    6
"""
for i in range(4, 8, 2):
    print(i)
"""

while によるループは条件が成立しなくなるまで実行されます。
出力:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1                      # x = x + 1 の省略記法

# try/except ブロックにより、例外を扱う
try:
    # "raise" により例外を発生させます。
    raise IndexError("This is an index error")
except IndexError as e:
    pass  # pass は、何もしないという命令(no-op)に相当します。普通、ここで例外に対処します。
except (TypeError, NameError):
    pass                 # もし必要なら、複数の種類の例外を一緒に処理できます。
else:  # try/except ブロックへのオプションの節。他の全てのexceptブロックより後に置かなければなりません。
    print("All good!")        # tryで例外が発生しなかった場合のみ実行されます。
finally:  # 例外が発生したか、しなかったか、どのような例外だったかに関らず実行されます。
    print("We can clean up resources here")

# try/finallyでリソースの始末をする代わりに、 with 文を使うこともできます。
with open("myfile.txt") as f:
    for line in f:
        print(line)

# Pythonは、iterableと呼ばれる基本的な抽象化が提供しています。
# iterableは、シーケンスとして取り扱えるオブジェクトです。
# range関数で返されるオブジェクトもiterableの一種です。
filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable)  # => dict_keys(['one', 'two', 'three']). これはiterableインタフェースを実装するオブジェクトです。

# iterableでループを行うことができます。
for i in our_iterable:
    print(i)                    # Prints one, two, three

# しかし、インデックスで要素を参照することはできません。
our_iterable[1]                 # TypeError が発生します。

# iterableは、iteratorの作り方がわかるオブジェクトです。
our_iterator = iter(our_iterable)

# iterator は、要素を取り出したときの状態を覚えるオブジェクトです。
# "next()"により次の要素を取り出せます。
next(our_iterator)              # => "one"

# 反復(iterate)する度に、状態を更新します。
next(our_iterator)              # => "two"
next(our_iterator)              # => "three"

# iteratorが自身の持つ全てのデータを返したあとは、 StopIteration 例外を発生させます。
next(our_iterator)              # StopIteration が発生します。

# "list()"を呼ぶことにより、iteratorの全ての要素を得られます。
list(filled_dict.keys())        # => ["one", "two", "three"]


####################################################
# 4. 関数
####################################################

# 新しい関数を作成するには "def" を使います。
def add(x, y):
    print("x is {} and y is {}".format(x, y))
    return x + y                # return 文で値を返します。

# 引数付きで関数を呼んでみましょう。
add(5, 6)                    # => "x is 5 and y is 6" と出力し、 11 を返します。

# キーワード引数で関数を呼ぶこともできます。
add(y=6, x=5)                   # キーワード引数を使うと任意の順番で引数を指定できます。


# 可変数の位置引数を持つ関数を定義できます。
def varargs(*args):
    return args

varargs(1, 2, 3)  # => (1, 2, 3)


# 可変数のキーワード引数を持つ関数を定義できます。
def keyword_args(**kwargs):
    return kwargs

# 何が起こるか、試してみましょう
keyword_args(big="foot", loch="ness")  # => {"big": "foot", "loch": "ness"}


# お望みなら、両方一気にやることもできます。
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) prints:
    (1, 2)
    {"a": 3, "b": 4}
"""

# 関数を呼ぶとき、 args/kwargs の逆のことをすることができます!
# * を使ってタプルを展開したり、 ** を使って辞書を展開できます。
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)             # foo(1, 2, 3, 4) に対応します。
all_the_args(**kwargs)          # foo(a=3, b=4) に対応します。
all_the_args(*args, **kwargs)   # foo(1, 2, 3, 4, a=3, b=4) に対応します。


# タプルで複数の値を返す
def swap(x, y):    # 括弧を使わずに、複数の値をタプルとして返すことができます。
    return y, x    # (Note: 括弧は使わなくてもいいですが、使うこともできます。)


x = 1
y = 2
x, y = swap(x, y)               # => x = 2, y = 1
# (x, y) = swap(x,y)  # このように、括弧は使っても使わなくてもいいです。


# 関数のスコープ
x = 5


def set_x(num):
    # ローカル変数の x はグローバル変数の x とは異なります
    x = num                     # => 43
    print(x)                    # => 43


def set_global_x(num):
    global x
    print(x)                    # => 5
    x = num                     # グローバル変数の x に 6 が代入されました。
    print(x)                    # => 6

set_x(43)
set_global_x(6)


# Pythonは第一級関数をサポートします。
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)                       # => 13

# 無名関数もサポートしています。
(lambda x: x > 2)(3)                 # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1)  # => 5

# 高階関数も組込まれています。
list(map(add_10, [1, 2, 3]))         # => [11, 12, 13]
list(map(max, [1, 2, 3], [4, 2, 1]))  # => [4, 2, 3]

list(filter(lambda x: x > 5, [3, 4, 5, 6, 7]))  # => [6, 7]

# map や filter の代わりに、リスト内包表記を使うことができます。
# リスト内包表記は、出力を別のリスト内包表記にネストさせることができます。
[add_10(i) for i in [1, 2, 3]]        # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]  # => [6, 7]

# 集合(set)や辞書も内包表記ができます。
{x for x in 'abcddeef' if x not in 'abc'}  # => {'d', 'e', 'f'}
{x: x**2 for x in range(5)}                # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


####################################################
# 5. モジュール
####################################################

# Pythonではモジュールをインポートできます。
import math
print(math.sqrt(16))            # => 4.0

# モジュールの中から特定の関数をインポートすることもできます。
from math import ceil, floor
print(ceil(3.7))                # => 4.0
print(floor(3.7))               # => 3.0

# 全部の関数をモジュールからインポートすることができます。
# Warning: この方法は推奨されません。
from math import *

# 短い名前でモジュールをインポートすることができます。
import math as m
math.sqrt(16) == m.sqrt(16)     # => True

# Pythonのモジュールは実際には単なるPythonのファイルです。
# 自分で書くことも、インポートすることもできます。
# ファイル名がそのままモジュール名になります。

# モジュールで定義されている関数と属性を調べることができます。
import math
dir(math)

# もし、現在書いているスクリプトと同じフォルダに「math.py」という
# Pythonのスクリプトが存在する場合、そのmath.pyが
# 組み込みのPythonモジュールの代わりに読み込まれるでしょう。
# これは、ローカルのフォルダはPythonの組み込みライブラリよりも
# 優先度が高いため発生するのです。


####################################################
# 6. クラス
####################################################

# クラスを作成するために、"class"という演算子を使います。
class Human:

    # クラスの属性です。このクラスの全てのインスタンスで共有されます。
    species = "H. sapiens"

    # 標準的なイニシャライザで、このクラスがインスタンスを作成するときは毎回呼ばれます。
    # 2つのアンダースコアがオブジェクトや属性の前後についているとき、これらはPythonによって利用され、
    # ユーザーの名前空間には存在しないということに注意してください。
    # __init__ や __str__ 、 __repr__ のようなメソッド(やオブジェクト、属性)は、
    # magic methods (または dunder methods)と呼ばれます。
    # このような名前を自分で発明しないほうがよいでしょう。
    def __init__(self, name):
        # 引数をインスタンスのname属性に設定します。
        self.name = name

        # プロパティの初期化
        self.age = 0

    # インスタンスメソッド。全てのメソッドは"self"を最初の引数に取ります。
    def say(self, msg):
        print("{name}: {message}".format(name=self.name, message=msg))

    # 別のインスタンスメソッドの例。
    def sing(self):
        return 'yo... yo... microphone check... one two... one two...'

    # クラスメソッドは全てのインスタンスで共有されます。
    # クラスメソッドではクラスを最初の引数として呼ばれます。
    @classmethod
    def get_species(cls):
        return cls.species

    # スタティックメソッドはクラスやインスタンスを参照せずに呼ばれます。
    @staticmethod
    def grunt():
        return "*grunt*"

    # プロパティはgetterのようなものです。
    # age() メソッドを同名の読取専用属性に変換します。
    @property
    def age(self):
        return self._age

    # プロパティを設定できるようにします。
    @age.setter
    def age(self, age):
        self._age = age

    # プロパティを削除できるようにします。
    @age.deleter
    def age(self):
        del self._age


# Pythonインタプリタがソースファイルを読み込んだとき、全てのコードを実行します。
# この __name__ による確認により、このモジュールがメインのプログラムである場合にのみ、
# このコードブロックが実行されるようにします。
if __name__ == '__main__':
    # クラスのインスタンスを作成します。
    i = Human(name="Ian")
    i.say("hi")                 # "Ian: hi"
    j = Human("Joel")
    j.say("hello")              # "Joel: hello"
    # i と j はHumanのインスタンスです。別の言葉で言うなら、これらはHumanのオブジェクトです。

    # クラスメソッドを呼んでみましょう。
    i.say(i.get_species())      # "Ian: H. sapiens"
    # 共有属性を変更してみましょう。
    Human.species = "H. neanderthalensis"
    i.say(i.get_species())      # => "Ian: H. neanderthalensis"
    j.say(j.get_species())      # => "Joel: H. neanderthalensis"

    # スタティックメソッドを呼んでみましょう。
    print(Human.grunt())            # => "*grunt*"
    print(i.grunt())                # => "*grunt*"

    # インスタンスのプロパティを更新してみましょう。
    i.age = 42
    # プロパティを取得してみましょう。
    i.say(i.age)                    # => 42
    j.say(j.age)                    # => 0
    # プロパティを削除してみましょう。
    del i.age
    # i.age                         # => AttributeError が発生します。


####################################################
# 6.1 多重継承
####################################################

# 別のクラスを定義します。
class Bat:

    species = 'Baty'

    def __init__(self, can_fly=True):
        self.fly = can_fly

    # このクラスも say メソッドを持ちます。
    def say(self, msg):
        msg = '... ... ...'
        return msg

    # 同様に、独自のメソッドも与えましょう。
    def sonar(self):
        return '))) ... ((('

if __name__ == '__main__':
    b = Bat()
    print(b.say('hello'))
    print(b.fly)

# ファイル単位のモジュール化を利用するために、上記のクラスを別々のファイルに配置することができます。
# ここでは、human.pyとbat.pyを作成してみましょう。

# 他のファイルから関数をインポートするために、次のような形式を利用してください。
# from "拡張子無しのファイル名" import "関数またはクラス"

# superhero.py
from human import Human
from bat import Bat


# BatmanはHumanとBatの両方を継承します。
class Batman(Human, Bat):

    # Batmanは species のクラス属性に独自の値を持ちます。
    species = 'Superhero'

    def __init__(self, *args, **kwargs):
        # 通常、属性を継承するにはsuper()を呼び出します。
        #     super(Batman, self).__init__(*args, **kwargs)
        # しかし、ここでは多重継承を行っているので、 super() はMRO(メソッド解決順序)の次の基本クラスにのみ動作します。
        # なので、全ての祖先に対して明示的に __init__ を呼ぶことにします。
        # *args と **kwargs を使うことで、それぞれの継承元が
        # たまねぎの皮を剥がすごとく、引数を用いることができます。
        Human.__init__(self, 'anonymous', *args, **kwargs)
        Bat.__init__(self, *args, can_fly=False, **kwargs)
        # 名前の属性の値を上書きします。
        self.name = 'Sad Affleck'

    def sing(self):
        return 'nan nan nan nan nan batman!'


if __name__ == '__main__':
    sup = Batman()

    # インスタンスの型を調べてみましょう。
    if isinstance(sup, Human):
        print('I am human')
    if isinstance(sup, Bat):
        print('I am bat')
    if type(sup) is Batman:
        print('I am Batman')

    # getattr() や super() の両方で使われるMROを取得します。
    # この属性は動的であり、更新が可能です。
    print(Batman.__mro__)  # => (<class '__main__.Batman'>, <class 'human.Human'>, <class 'bat.Bat'>, <class 'object'>)

    # 親メソッドを呼び出しますが、独自のクラス属性を参照します。
    print(sup.get_species())    # => Superhero

    # オーバーロードされたメソッドを呼び出します。
    print(sup.sing())           # => nan nan nan nan nan batman!

    # 継承順により、Humanから継承されたメソッドを呼び出します。
    sup.say('I agree')          # => Sad Affleck: I agree

    # 2番目の先祖にのみ存在するメソッドを呼び出してみます。
    print(sup.sonar())          # => ))) ... (((

    # 継承されたクラス属性
    sup.age = 100
    print(sup.age)

    # デフォルト値が上書きされて、2番目の先祖から継承された属性
    print('Can I fly? ' + str(sup.fly))


####################################################
# 7. 発展的内容
####################################################

# ジェネレータは遅延をするコードの作成に役立ちます。
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# 次の値を処理するのに必要なデータしか読み込まないので、ジェネレータはメモリをあまり消費しません。
# この性質により、他の方法では非常に多くのメモリを消費するような操作が可能になります。
for i in double_numbers(range(1, 900000000)):  # `range` もジェネレータの1つです。
    print(i)
    if i >= 30:
        break

# リスト内包表記のように、ジェネータ内包表記を作成することもできます。
values = (-x for x in [1, 2, 3, 4, 5])
for x in values:
    print(x)                    # prints -1 -2 -3 -4 -5

# ジェネレータ内包表記から直接リストを作成することもできます。
values = (-x for x in [1, 2, 3, 4, 5])
gen_to_list = list(values)
print(gen_to_list)  # => [-1, -2, -3, -4, -5]

# デコレータ
# この例では`beg` が `say` を `wraps`します。
# もし say_please が True なら、出力が変更されます。
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Please! I am poor :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Can you buy me a beer?"
    return msg, say_please


print(say())                 # Can you buy me a beer?
print(say(say_please=True))  # Can you buy me a beer? Please! I am poor :(
```

## Ready For More?

### Free Online

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Course](http://www.python-course.eu/index.php)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)
* [A curated list of awesome Python frameworks, libraries and software](https://github.com/vinta/awesome-python)
* [30 Python Language Features and Tricks You May Not Know About](http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html)
* [Official Style Guide for Python](https://www.python.org/dev/peps/pep-0008/)
* [Python 3 Computer Science Circles](http://cscircles.cemc.uwaterloo.ca/)
* [Dive Into Python 3](http://www.diveintopython3.net/index.html)
* [A Crash Course in Python for Scientists](http://nbviewer.jupyter.org/gist/anonymous/5924718)
---
language: R
contributors:
    - ["e99n09", "http://github.com/e99n09"]
    - ["isomorphismes", "http://twitter.com/isomorphisms"]
translators:
    - ["akirahirose", "https://twitter.com/akirahirose"]
filename: learnr-jp.r
lang: ja-jp
---


R は統計計算用の言語です。
データの取得やクリーニング、統計処理やグラフ作成をするために便利な、たくさんのライブラリがあります。また、LaTeX文書からRコマンドを呼び出すこともできます


```r
# コメント行は、#で開始します


# 複数行をまとめてコメントにすることはできないので、
# コメントを複数の行に分けたい場合、このように、単に毎行をコメントにしてください


# WindowsやMacでは、 COMMAND-ENTERで、コマンドを1行実行できます






#############################################################################
# プログラミングがわからなくとも使えるコマンド類
#############################################################################


# この節では、プログラミングがわからなくとも使える便利なRコマンドを紹介します
# 全てを理解できなくとも、まずはやってみましょう！


data()                # 既にロードされているデータを閲覧します
data(rivers)        # "北米にある大きな川の長さ"データを取得します
ls()                # "rivers" がワークスペースに表示されました
head(rivers)        # データの先頭部分です
# 735 320 325 392 524 450


length(rivers)        # 何本の川がデータにある?
# 141
summary(rivers) # 統計的に要約するとどうなる?
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  135.0   310.0   425.0   591.2   680.0  3710.0 


# 茎葉図（ヒストグラムに似た図）を描く
stem(rivers)


#  The decimal point is 2 digit(s) to the right of the |
#
#   0 | 4
#   2 | 011223334555566667778888899900001111223333344455555666688888999
#   4 | 111222333445566779001233344567
#   6 | 000112233578012234468
#   8 | 045790018
#  10 | 04507
#  12 | 1471
#  14 | 56
#  16 | 7
#  18 | 9
#  20 | 
#  22 | 25
#  24 | 3
#  26 | 
#  28 | 
#  30 | 
#  32 | 
#  34 | 
#  36 | 1


stem(log(rivers)) # このデータは、正規分布でも対数正規分布でもないので、注意！
# 特に正規分布原理主義のみなさん


#  The decimal point is 1 digit(s) to the left of the |
#
#  48 | 1
#  50 | 
#  52 | 15578
#  54 | 44571222466689
#  56 | 023334677000124455789
#  58 | 00122366666999933445777
#  60 | 122445567800133459
#  62 | 112666799035
#  64 | 00011334581257889
#  66 | 003683579
#  68 | 0019156
#  70 | 079357
#  72 | 89
#  74 | 84
#  76 | 56
#  78 | 4
#  80 | 
#  82 | 2


# ヒストグラム作成
hist(rivers, col="#333333", border="white", breaks=25) # これらのパラメータをつかいます
hist(log(rivers), col="#333333", border="white", breaks=25) # いろいろな使い方ができます


# 別のロード済データでやってみましょう。Rには、いろいろなデータがロードされています。
data(discoveries)
plot(discoveries, col="#333333", lwd=3, xlab="Year",
     main="Number of important discoveries per year")
plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year",
     main="Number of important discoveries per year")


# 年次のソートだけではなく、
# 標準的な並べ替えもできます
sort(discoveries)
#  [1]  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2
# [26]  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3
# [51]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  4  4  4  4  4  4  4
# [76]  4  4  4  4  5  5  5  5  5  5  5  6  6  6  6  6  6  7  7  7  7  8  9 10 12


stem(discoveries, scale=2)
# 
#  The decimal point is at the |
#
#   0 | 000000000
#   1 | 000000000000
#   2 | 00000000000000000000000000
#   3 | 00000000000000000000
#   4 | 000000000000
#   5 | 0000000
#   6 | 000000
#   7 | 0000
#   8 | 0
#   9 | 0
#  10 | 0
#  11 | 
#  12 | 0


max(discoveries)
# 12
summary(discoveries)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#    0.0     2.0     3.0     3.1     4.0    12.0 


# サイコロを振ります
round(runif(7, min=.5, max=6.5))
# 1 4 6 1 4 6 4
# 私と同じrandom.seed(31337)を使わない限りは、別の値になります


# ガウス分布を9回生成します
rnorm(9)
# [1]  0.07528471  1.03499859  1.34809556 -0.82356087  0.61638975 -1.88757271
# [7] -0.59975593  0.57629164  1.08455362






##################################################
# データ型と基本計算
##################################################


# ここからは、プログラミングをつかうチュートリアルです
# この節ではRで重要なデータ型（データクラス）の、整数型、数字型、文字型、論理型と因子（ファクター）型をつかいます
# 他にもいろいろありますが、これらの必要最小限なものから始めましょう


# 整数型
# 整数型はLで指定します
5L # 5
class(5L) # "integer"
# (?class を実行すると、class()関数について、さらなる情報が得られます)
# Rでは、この5Lのような1つの値は、長さ1のベクトルとして扱われます
length(5L) # 1
# 整数型のベクトルはこのようにつくります
c(4L, 5L, 8L, 3L) # 4 5 8 3
length(c(4L, 5L, 8L, 3L)) # 4
class(c(4L, 5L, 8L, 3L)) # "integer"


# 数字型
# 倍精度浮動小数点数です
5 # 5
class(5) # "numeric"
# しつこいですが、すべてはベクトルです
# 1つ以上の要素がある数字のベクトルも、作ることができます
c(3,3,3,2,2,1) # 3 3 3 2 2 1
# 指数表記もできます
5e4 # 50000
6.02e23 # アボガドロ数
1.6e-35 # プランク長
# 無限大、無限小もつかえます
class(Inf)        # "numeric"
class(-Inf)        # "numeric"
# 例のように、"Inf"を使ってください。integrate( dnorm(x), 3, Inf);
# Z-スコア表が必要なくなります


# 基本的な計算
# 数を計算できます
# 整数と整数以外の数字を両方使った計算をすると、結果は整数以外の数字になります
10L + 66L # 76      # 整数足す整数は整数
53.2 - 4  # 49.2    # 整数引く数字は数字
2.0 * 2L  # 4       # 数字かける整数は数字
3L / 4    # 0.75    # 整数割る数字は数字
3 %% 2          # 1       # 二つの数字を割った余りは数字
# 不正な計算は "not-a-number"になります
0 / 0 # NaN
class(NaN) # "numeric"
# 長さが1より大きなベクター同士の計算もできます
# どちらかが長い場合、短い方は何度も繰り返して使われます
c(1,2,3) + c(1,2,3) # 2 4 6

# 文字
# Rでは、文字列と文字に区別がありません
"Horatio" # "Horatio"
class("Horatio") # "character"
class('H') # "character"
# 上記は両方とも、長さ1のベクターです
# 以下は、より長い場合です
c('alef', 'bet', 'gimmel', 'dalet', 'he')
# =>
# "alef"   "bet"    "gimmel" "dalet"  "he"
length(c("Call","me","Ishmael")) # 3
# 正規表現処理を文字ベクターに適用できます
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
# Rはいくつかの文字ベクターを組み込みで持っています
letters
# =>
#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [20] "t" "u" "v" "w" "x" "y" "z"
month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"


# 論理
# Rでは、Booleanは論理（logical）型です
class(TRUE)        # "logical"
class(FALSE)        # "logical"
# 以下は比較演算子の例です
TRUE == TRUE        # TRUE
TRUE == FALSE        # FALSE
FALSE != FALSE        # FALSE
FALSE != TRUE        # TRUE
# 無いデータ (NA) も論理型です
class(NA)        # "logical"
# 以下のようにすると、複数の要素を持つ、論理型ベクターが返ります
c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE
c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE


# 因子（ファクター）
# 因子型は、カテゴリカルデータ用の型です
# 因子には、子供の学年のように順序がつけられるものか、性別のように順序がないものがあります
factor(c("female", "female", "male", "NA", "female"))
#  female female male   NA     female
# Levels: female male NA
# "levels" は、カテゴリカルデータがとりうる値を返します
levels(factor(c("male", "male", "female", "NA", "female"))) # "female" "male"   "NA" 
# 因子ベクターの長さが1ならば、そのlevelも1です
length(factor("male")) # 1
length(levels(factor("male"))) # 1
# 因子型は、この後で紹介するデータフレーム（というデータ型）内で、よくみられます
data(infert) # "Infertility after Spontaneous and Induced Abortion"
levels(infert$education) # "0-5yrs"  "6-11yrs" "12+ yrs"


# NULL
# "NULL" は特殊な型なのですが、ベクターを空にするときに使います
class(NULL)        # NULL
parakeet
# =>
# [1] "beak"     "feathers" "wings"    "eyes"    
parakeet <- NULL
parakeet
# =>
# NULL


# 型の強制
# 型の強制とは、ある値を、強制的に別の型として利用する事です
as.character(c(6, 8)) # "6" "8"
as.logical(c(1,0,1,1)) # TRUE FALSE  TRUE  TRUE
# さまざまな要素が入っているベクターに対して型の強制を行うと、おかしなことになります
c(TRUE, 4) # 1 4
c("dog", TRUE, 4) # "dog"  "TRUE" "4"
as.numeric("Bilbo")
# =>
# [1] NA
# Warning message:
# NAs introduced by coercion 


# 追記: ここで紹介したのは、基本的な型だけです
# 実際には、日付（dates）や時系列（time series）など、いろいろな型があります






##################################################
# 変数、ループ、もし/ほかに（if/else）
##################################################


# 変数は、ある値を後で使うために入れておく、箱のようなものです
# 箱に入れることを、変数に値を代入する、といいます
# 変数を使うと、ループや関数、if/else 分岐を利用できます


# 変数
# 代入する方法はいろいろあります
x = 5 # これはできます
y <- "1" # これがおすすめです
TRUE -> z # これも使えますが、ちょっとわかりにくいですね


# ループ
# forでループできます
for (i in 1:4) {
  print(i)
}
# whileでループできます
a <- 10
while (a > 4) {
        cat(a, "...", sep = "")
        a <- a - 1
}
# Rでは、forやwhileは遅いことを覚えておいてください
# ベクターを丸ごと処理する（つまり、行全体や、列全体を指定して処理する）か、
# 後述する、apply()系の関数を使うのが、速度的にはお勧めです


# IF/ELSE
# ごく普通のif文です
if (4 > 3) {
        print("4 is greater than 3")
} else {
        print("4 is not greater than 3")
}
# =>
# [1] "4 is greater than 3"


# 関数
# 以下のように定義します
jiggle <- function(x) {
        x = x + rnorm(1, sd=.1)        #すこしだけ（制御された）ノイズを入れます
        return(x)
}
# 他の関数と同じように、呼びます
jiggle(5)        # 5±ε.  set.seed(2716057)をすると、jiggle(5)==5.005043






###########################################################################
# データ構造: ベクター、行列、データフレーム、配列
###########################################################################


# 1次元


# まずは基本からです。ご存じベクターからです
vec <- c(8, 9, 10, 11)
vec        #  8  9 10 11
# 特定の要素を、[角括弧]による指定で取り出せます
# (Rでは、最初の要素は1番目と数えます)
vec[1]                # 8
letters[18]        # "r"
LETTERS[13]        # "M"
month.name[9]        # "September"
c(6, 8, 7, 5, 3, 0, 9)[3]        # 7
# 特定のルールに当てはまる要素を見つけることもできます
which(vec %% 2 == 0)        # 1 3
# 最初か最後の数個を取り出すこともできます
head(vec, 1)        # 8
tail(vec, 2)        # 10 11
# ある値がベクターにあるかどうかをみることができます
any(vec == 10) # TRUE
# ベクターの数より大きなインデックスを指定すると、NAが返ります
vec[6]        # NA
# ベクターの長さは、length()で取得できます
length(vec)        # 4
# ベクター全体、または1部に対して、操作ができます
vec * 4        # 16 20 24 28
vec[2:3] * 5        # 25 30
any(vec[2:3] == 8) # FALSE
# R には、ベクターにある値を要約するための様々な関数があります
mean(vec)        # 9.5
var(vec)        # 1.666667
sd(vec)                # 1.290994
max(vec)        # 11
min(vec)        # 8
sum(vec)        # 38
# 他にも、ベクター関連ではいろいろな関数があります。以下はベクターをつくるための方法です
5:15        # 5  6  7  8  9 10 11 12 13 14 15
seq(from=0, to=31337, by=1337)
# =>
#  [1]     0  1337  2674  4011  5348  6685  8022  9359 10696 12033 13370 14707
# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751


# 2次元配列 (すべての値が同じ型の場合)


# 同じ型の値が含まれる2次元配列は、このように作れます
mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# ベクターとは違い、2次元配列の型名は"matrix"です。
class(mat) # => "matrix"
# 最初の行
mat[1,]        # 1 4
# 最初の列に対する操作
3 * mat[,1]        # 3 6 9
# 特定のセルを取り出し
mat[3,2]        # 6


# 2次元配列全体を転置します
t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6


# 2次元配列の積
mat %*% t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]   17   22   27
# [2,]   22   29   36
# [3,]   27   36   45


# cbind() は、複数のベクターを、別々の列に並べて2次元配列を作ります
mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
mat2
# =>
#      [,1] [,2]   
# [1,] "1"  "dog"  
# [2,] "2"  "cat"  
# [3,] "3"  "bird" 
# [4,] "4"  "dog"
class(mat2)        # matrix
# ここでいま1度、2次元配列内の型について注意してください!
# 2次元配列にある値は、すべて同じ型にする必要があります。そのため、すべて文字型に変換されています
c(class(mat2[,1]), class(mat2[,2]))


# rbind() は、複数のベクターを、別々の行に並べて2次元配列を作ります
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    4    5
# [2,]    6    7    0    4
# 全ての値は同じ型になります。上記例は幸い、強制変換がされないものでした


# 2次元配列 (いろいろな型を含む場合)


# 異なる型の値を含む配列をつくりたい場合、データフレームを使ってください
# データフレームは、統計処理を行うプログラムをする際にとても便利です
# Pythonでも、 "pandas"というパッケージにて、似たものが利用可能です


students <- data.frame(c("Cedric","Fred","George","Cho","Draco","Ginny"),
                       c(3,2,2,1,0,-1),
                       c("H", "G", "G", "R", "S", "G"))
names(students) <- c("name", "year", "house") #カラム名
class(students)        # "data.frame"
students
# =>
#     name year house
# 1 Cedric    3     H
# 2   Fred    2     G
# 3 George    2     G
# 4    Cho    1     R
# 5  Draco    0     S
# 6  Ginny   -1     G
class(students$year)        # "numeric"
class(students[,3])        # "factor"
# 行と列の数をみます
nrow(students)        # 6
ncol(students)        # 3
dim(students)        # 6 3
# このdata.frame() 関数は、デフォルトでは文字列ベクターを因子ベクターに変換します
# stringsAsFactors = FALSE に設定してからデータフレームを作成すると、変換されません
?data.frame


# データフレームの1部を取り出すには、いろいろな（変な）、似たような方法があります
students$year        # 3  2  2  1  0 -1
students[,2]        # 3  2  2  1  0 -1
students[,"year"]        # 3  2  2  1  0 -1


# データフレームの拡張版が、データテーブルです。
# 大きなデータやパネルデータ、データセットの結合が必要な場合には、データテーブルを使うべきです。
# 以下に駆け足で説明します
install.packages("data.table") # CRANからパッケージをダウンロードします
require(data.table) # ロードします
students <- as.data.table(students)
students # 若干異なる出力がされることに注意
# =>
#      name year house
# 1: Cedric    3     H
# 2:   Fred    2     G
# 3: George    2     G
# 4:    Cho    1     R
# 5:  Draco    0     S
# 6:  Ginny   -1     G
students[name=="Ginny"] # name == "Ginny"の行を取り出します
# =>
#     name year house
# 1: Ginny   -1     G
students[year==2] # year == 2の行を取り出します
# =>
#      name year house
# 1:   Fred    2     G
# 2: George    2     G
# データテーブルは、二つのデータセットを結合するのにも便利です
# 結合用に、生徒データが入った別のデータテーブルをつくります
founders <- data.table(house=c("G","H","R","S"),
                       founder=c("Godric","Helga","Rowena","Salazar"))
founders
# =>
#    house founder
# 1:     G  Godric
# 2:     H   Helga
# 3:     R  Rowena
# 4:     S Salazar
setkey(students, house)
setkey(founders, house)
students <- founders[students] # 二つのデータテーブルを、"house"をキーとして結合します
setnames(students, c("house","houseFounderName","studentName","year"))
students[,order(c("name","year","house","houseFounderName")), with=F]
# =>
#    studentName year house houseFounderName
# 1:        Fred    2     G           Godric
# 2:      George    2     G           Godric
# 3:       Ginny   -1     G           Godric
# 4:      Cedric    3     H            Helga
# 5:         Cho    1     R           Rowena
# 6:       Draco    0     S          Salazar


# データテーブルは、要約を作るのも簡単です
students[,sum(year),by=house]
# =>
#    house V1
# 1:     G  3
# 2:     H  3
# 3:     R  1
# 4:     S  0


# データフレームやデータテーブルから列を消したい場合は、NULL値を代入します
students$houseFounderName <- NULL
students
# =>
#    studentName year house
# 1:        Fred    2     G
# 2:      George    2     G
# 3:       Ginny   -1     G
# 4:      Cedric    3     H
# 5:         Cho    1     R
# 6:       Draco    0     S


# データテーブルから行を消す場合は、以下のように除く行を指定すればできます
students[studentName != "Draco"]
# =>
#    house studentName year
# 1:     G        Fred    2
# 2:     G      George    2
# 3:     G       Ginny   -1
# 4:     H      Cedric    3
# 5:     R         Cho    1
# データフレームの場合も同様です
students <- as.data.frame(students)
students[students$house != "G",]
# =>
#   house houseFounderName studentName year
# 4     H            Helga      Cedric    3
# 5     R           Rowena         Cho    1
# 6     S          Salazar       Draco    0


# 多次元 (すべての値が同じ型の場合)


# 配列を並べて、N次元の表を作ります
# 配列なので、すべての値は同じ型にする必要があります
# ちなみに、以下のようにすれば2次元配列・2次元表も作成可能です
array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4))
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    8    3
# [2,]    2    5    9    6
# 2次元配列を並べて、3次元配列を作ることもできます
array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2))
# =>
# , , 1
#
#      [,1] [,2]
# [1,]    2    8
# [2,]  300    9
# [3,]    4    0
#
# , , 2
#
#      [,1] [,2]
# [1,]    5   66
# [2,]   60    7
# [3,]    0  847


# リスト（多次元、不完全または複数の型が使われているもの)


# ついにRのリストです
list1 <- list(time = 1:40)
list1$price = c(rnorm(40,.5*list1$time,4)) # random
list1
# リストの要素は以下のようにして取得できます
list1$time # ある方法
list1[["time"]] # 別の方法
list1[[1]] # また別の方法
# =>
#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# [34] 34 35 36 37 38 39 40
# 他のベクターと同じく、1部を取り出すことができます
list1$price[4]


# リストは、Rで1番効率的なデータ型ではありません
# 特別な理由がない限りは、リストの代わりにデータフレームを使うべきです
# リストは、線形回帰関数の返値として、しばしば使われています


##################################################
# apply() 系の関数
##################################################


# matは覚えていますよね？
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# apply(X, MARGIN, FUN) は、行列Xの行（MARGIN=1で指定)または列（MARGIN=2で指定)に対して、関数FUNを実行します
# Rで、このように指定してXの全行または全列に関数を実行するのは、forやwhileループを使うよりも、遥かに速いです
apply(mat, MAR = 2, jiggle)
# =>
#      [,1] [,2]
# [1,]    3   15
# [2,]    7   19
# [3,]   11   23
# 他にも便利な関数があります。?lapply, ?sapply で確認してみてください


# apply()系関数の使い方は、ちょっとややこしいです（みんなそう思ってます）。なので、あまり怖がりすぎないでください


# plyr パッケージは、*apply() 系の関数を置き換えて（さらに改善して）いこうとしています
install.packages("plyr")
require(plyr)
?plyr






#########################
# データロード
#########################


# "pets.csv"は、インターネット上に置いてあるファイルです
# (しかし、自分のPCにあるのと同じぐらい簡単に扱う事ができます)
pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv")
pets
head(pets, 2) # 最初の2行
tail(pets, 1) # 最後の行


# データフレームか行列をcsvファイルとして保存します
write.csv(pets, "pets2.csv") # 新しくcsvファイルを作ります
# ワーキングディレクトリを、setwd()で設定します。　ワーキングディレクトリは getwd()で確認可能です


# ?read.csv や ?write.csv を入力すると、よりたくさんの情報を確認できます






#########################
# プロット
#########################


# Rに組込まれているプロット関数をつかいます
# 散布図!
plot(list1$time, list1$price, main = "fake data")
# 回帰図!
linearModel <- lm(price  ~ time, data = list1)
linearModel # outputs result of regression
# 回帰直線を既存の図上に引きます
abline(linearModel, col = "red")
# いろいろな散布図をつくって、確認できます
plot(linearModel)
# ヒストグラム！
hist(rpois(n = 10000, lambda = 5), col = "thistle")
# 棒グラフ！
barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow"))


# GGPLOT2
# 上記の組込み関数を使うよりも、もっときれいな図を描くこともできます
# ggplot2 パッケージを使って、より多くのよい図を描いてみましょう
install.packages("ggplot2")
require(ggplot2)
?ggplot2
pp <- ggplot(students, aes(x=house))
pp + geom_histogram()
ll <- as.data.table(list1)
pp <- ggplot(ll, aes(x=time,price))
pp + geom_point()
# ggplot2 には、素晴らしい関連ドキュメントがそろっています (http://docs.ggplot2.org/current/)






```


## Rの入手方法


* RとR GUIはこちら [http://www.r-project.org/](http://www.r-project.org/)
* [RStudio](http://www.rstudio.com/ide/) 別のGUI
---
language: java
contributors:
    - ["Jake Prather", "https://github.com/JakeHP"]
    - ["Jakukyo Friel", "https://weakish.github.io"]
    - ["Madison Dickson", "https://github.com/mix3d"]
    - ["Simon Morgan", "https://sjm.io/"]
    - ["Zachary Ferguson", "https://github.com/zfergus2"]
    - ["Cameron Schermerhorn", "https://github.com/cschermerhorn"]
    - ["Rachel Stiyer", "https://github.com/rstiyer"]
    - ["Michael Dähnert", "https://github.com/JaXt0r"]
    - ["Rob Rose", "https://github.com/RobRoseKnows"]
    - ["Sean Nam", "https://github.com/seannam"]
filename: LearnJava.java
---

Java is a general-purpose, concurrent, class-based, object-oriented computer
programming language.
[Read more here.](https://docs.oracle.com/javase/tutorial/java/)

```java
// Single-line comments start with //

/*
Multi-line comments look like this.
*/

/**
 * JavaDoc comments look like this. Used to describe the Class or various
 * attributes of a Class.
 * Main attributes:
 *
 * @author         Name (and contact information such as email) of author(s).
 * @version     Current version of the program.
 * @since        When this part of the program was first added.
 * @param         For describing the different parameters for a method.
 * @return        For describing what the method returns.
 * @deprecated  For showing the code is outdated or shouldn't be used.
 * @see         Links to another part of documentation.
*/

// Import ArrayList class inside of the java.util package
import java.util.ArrayList;
// Import all classes inside of java.security package
import java.security.*;

// Each .java file contains one outer-level public class, with the same name
// as the file.
public class LearnJava {

    // In order to run a java program, it must have a main method as an entry
    // point.
    public static void main(String[] args) {

    ///////////////////////////////////////
    // Input/Output
    ///////////////////////////////////////

        /*
        * Output
        */

        // Use System.out.println() to print lines.
        System.out.println("Hello World!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // To print without a newline, use System.out.print().
        System.out.print("Hello ");
        System.out.print("World");

        // Use System.out.printf() for easy formatted printing.
        System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159

        /*
         * Input
         */

        // use Scanner to read input
        // must import java.util.Scanner;
        Scanner scanner = new Scanner(System.in);

        // read string input
        String name = scanner.next();

        // read byte input
        byte numByte = scanner.nextByte();

        // read int input
        int numInt = scanner.nextInt();

        // read long input
        float numFloat = scanner.nextFloat();

        // read double input
        double numDouble = scanner.nextDouble();

        // read boolean input
        boolean bool = scanner.nextBoolean();

        ///////////////////////////////////////
        // Variables
        ///////////////////////////////////////

        /*
        *  Variable Declaration
        */
        // Declare a variable using <type> <name>
        int fooInt;
        // Declare multiple variables of the same
        // type <type> <name1>, <name2>, <name3>
        int fooInt1, fooInt2, fooInt3;

        /*
        *  Variable Initialization
        */

        // Initialize a variable using <type> <name> = <val>
        int barInt = 1;
        // Initialize multiple variables of same type with same
        // value <type> <name1>, <name2>, <name3>
        // <name1> = <name2> = <name3> = <val>
        int barInt1, barInt2, barInt3;
        barInt1 = barInt2 = barInt3 = 1;

        /*
        *  Variable types
        */
        // Byte - 8-bit signed two's complement integer
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // If you would like to interpret a byte as an unsigned integer
        // then this simple operation can help
        int unsignedIntLessThan256 = 0xff & fooByte;
        // this contrasts a cast which can be negative.
        int signedInt = (int) fooByte;

        // Short - 16-bit signed two's complement integer
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - 32-bit signed two's complement integer
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int bazInt = 1;

        // Long - 64-bit signed two's complement integer
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L is used to denote that this variable value is of type Long;
        // anything without is treated as integer by default.

        // Note: byte, short, int and long are signed. They can have positive and negative values.
        // There are no unsigned variants.
        // char, however, is 16-bit unsigned.

        // Float - Single-precision 32-bit IEEE 754 Floating Point
        // 2^-149 <= float <= (2-2^-23) * 2^127
        float fooFloat = 234.5f;
        // f or F is used to denote that this variable value is of type float;
        // otherwise it is treated as double.

        // Double - Double-precision 64-bit IEEE 754 Floating Point
        // 2^-1074 <= x <= (2-2^-52) * 2^1023
        double fooDouble = 123.4;

        // Boolean - true & false
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - A single 16-bit Unicode character
        char fooChar = 'A';

        // final variables can't be reassigned to another object,
        final int HOURS_I_WORK_PER_WEEK = 9001;
        // but they can be initialized later.
        final double E;
        E = 2.71828;

        // BigInteger - Immutable arbitrary-precision integers
        //
        // BigInteger is a data type that allows programmers to manipulate
        // integers longer than 64-bits. Integers are stored as an array of
        // of bytes and are manipulated using functions built into BigInteger
        //
        // BigInteger can be initialized using an array of bytes or a string.
        BigInteger fooBigInteger = new BigInteger(fooByteArray);

        // BigDecimal - Immutable, arbitrary-precision signed decimal number
        //
        // A BigDecimal takes two parts: an arbitrary precision integer
        // unscaled value and a 32-bit integer scale
        //
        // BigDecimal allows the programmer complete control over decimal
        // rounding. It is recommended to use BigDecimal with currency values
        // and where exact decimal precision is required.
        //
        // BigDecimal can be initialized with an int, long, double or String
        // or by initializing the unscaled value (BigInteger) and scale (int).
        BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt);

        // Be wary of the constructor that takes a float or double as
        // the inaccuracy of the float/double will be copied in BigDecimal.
        // Prefer the String constructor when you need an exact value.
        BigDecimal tenCents = new BigDecimal("0.1");

        // Strings
        String fooString = "My String Is Here!";

        // \n is an escaped character that starts a new line
        String barString = "Printing on a new line?\nNo Problem!";
        // \t is an escaped character that adds a tab character
        String bazString = "Do you want to add a tab?\tNo Problem!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // String Building
        // #1 - with plus operator
        // That's the basic way to do it (optimized under the hood)
        String plusConcatenated = "Strings can " + "be concatenated " + "via + operator.";
        System.out.println(plusConcatenated);
        // Output: Strings can be concatenated via + operator.

        // #2 - with StringBuilder
        // This way doesn't create any intermediate strings. It just stores the string pieces, and ties them together
        // when toString() is called.
        // Hint: This class is not thread safe. A thread-safe alternative (with some impact on performance) is StringBuffer.
        StringBuilder builderConcatenated = new StringBuilder();
        builderConcatenated.append("You ");
        builderConcatenated.append("can use ");
        builderConcatenated.append("the StringBuilder class.");
        System.out.println(builderConcatenated.toString()); // only now is the string built
        // Output: You can use the StringBuilder class.

        // StringBuilder is efficient when the fully constructed String is not required until the end of some processing.
        StringBuilder stringBuilder = new StringBuilder();
        String inefficientString = "";
        for (int i = 0 ; i < 10; i++) {
            stringBuilder.append(i).append(" ");
            inefficientString += i + " ";
        }
        System.out.println(inefficientString);
        System.out.println(stringBuilder.toString());
        // inefficientString requires a lot more work to produce, as it generates a String on every loop iteration.
        // Simple concatenation with + is compiled to a StringBuilder and toString()
        // Avoid string concatenation in loops.

        // #3 - with String formatter
        // Another alternative way to create strings. Fast and readable.
        String.format("%s may prefer %s.", "Or you", "String.format()");
        // Output: Or you may prefer String.format().

        // Arrays
        // The array size must be decided upon instantiation
        // The following formats work for declaring an array
        // <datatype>[] <var name> = new <datatype>[<array size>];
        // <datatype> <var name>[] = new <datatype>[<array size>];
        int[] intArray = new int[10];
        String[] stringArray = new String[1];
        boolean boolArray[] = new boolean[100];

        // Another way to declare & initialize an array
        int[] y = {9000, 1000, 1337};
        String names[] = {"Bob", "John", "Fred", "Juan Pedro"};
        boolean bools[] = {true, false, false};

        // Indexing an array - Accessing an element
        System.out.println("intArray @ 0: " + intArray[0]);

        // Arrays are zero-indexed and mutable.
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // Other data types worth checking out
        // ArrayLists - Like arrays except more functionality is offered, and
        //              the size is mutable.
        // LinkedLists - Implementation of doubly-linked list. All of the
        //               operations perform as could be expected for a
        //               doubly-linked list.
        // Maps - A set of objects that map keys to values. Map is
        //        an interface and therefore cannot be instantiated.
        //        The type of keys and values contained in a Map must
        //        be specified upon instantiation of the implementing
        //        class. Each key may map to only one corresponding value,
        //        and each key may appear only once (no duplicates).
        // HashMaps - This class uses a hashtable to implement the Map
        //            interface. This allows the execution time of basic
        //            operations, such as get and insert element, to remain
        //            constant even for large sets.
        // TreeMap - This class is a sorted tree structure. It implements a red
        //           black tree and sorts the entries based on the key value or
        //           the comparator provided while creating the object

        ///////////////////////////////////////
        // Operators
        ///////////////////////////////////////
        System.out.println("\n->Operators");

        int i1 = 1, i2 = 2; // Shorthand for multiple declarations

        // Arithmetic is straightforward
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (int/int returns int)
        System.out.println("1/2 = " + (i1 / (double)i2)); // => 0.5

        // Modulo
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Comparison operators
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Boolean operators
        System.out.println("3 > 2 && 2 > 3? " + ((3 > 2) && (2 > 3))); // => false
        System.out.println("3 > 2 || 2 > 3? " + ((3 > 2) || (2 > 3))); // => true
        System.out.println("!(3 == 2)? " + (!(3 == 2))); // => true

        // Bitwise operators!
        /*
        ~      Unary bitwise complement
        <<     Signed left shift
        >>     Signed/Arithmetic right shift
        >>>    Unsigned/Logical right shift
        &      Bitwise AND
        ^      Bitwise exclusive OR
        |      Bitwise inclusive OR
        */

        // Increment operators
        int i = 0;
        System.out.println("\n->Inc/Dec-rementation");
        // The ++ and -- operators increment and decrement by 1 respectively.
        // If they are placed before the variable, they increment then return;
        // after the variable they return then increment.
        System.out.println(i++); // i = 1, prints 0 (post-increment)
        System.out.println(++i); // i = 2, prints 2 (pre-increment)
        System.out.println(i--); // i = 1, prints 2 (post-decrement)
        System.out.println(--i); // i = 0, prints 0 (pre-decrement)

        ///////////////////////////////////////
        // Control Structures
        ///////////////////////////////////////
        System.out.println("\n->Control Structures");

        // If statements are c-like
        int j = 10;
        if (j == 10) {
            System.out.println("I get printed");
        } else if (j > 10) {
            System.out.println("I don't");
        } else {
            System.out.println("I also don't");
        }

        // While loop
        int fooWhile = 0;
        while(fooWhile < 100) {
            System.out.println(fooWhile);
            // Increment the counter
            // Iterated 100 times, fooWhile 0,1,2...99
            fooWhile++;
        }
        System.out.println("fooWhile Value: " + fooWhile);

        // Do While Loop
        int fooDoWhile = 0;
        do {
            System.out.println(fooDoWhile);
            // Increment the counter
            // Iterated 99 times, fooDoWhile 0->99
            fooDoWhile++;
        } while(fooDoWhile < 100);
        System.out.println("fooDoWhile Value: " + fooDoWhile);

        // For Loop
        // for loop structure => for(<start_statement>; <conditional>; <step>)
        for (int fooFor = 0; fooFor < 10; fooFor++) {
            System.out.println(fooFor);
            // Iterated 10 times, fooFor 0->9
        }
        System.out.println("fooFor Value: " + fooFor);

        // Nested For Loop Exit with Label
        outer:
        for (int i = 0; i < 10; i++) {
          for (int j = 0; j < 10; j++) {
            if (i == 5 && j ==5) {
              break outer;
              // breaks out of outer loop instead of only the inner one
            }
          }
        }

        // For Each Loop
        // The for loop is also able to iterate over arrays as well as objects
        // that implement the Iterable interface.
        int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        // for each loop structure => for (<object> : <iterable>)
        // reads as: for each element in the iterable
        // note: the object type must match the element type of the iterable.
        for (int bar : fooList) {
            System.out.println(bar);
            //Iterates 9 times and prints 1-9 on new lines
        }

        // Switch Case
        // A switch works with the byte, short, char, and int data types.
        // It also works with enumerated types (discussed in Enum Types), the
        // String class, and a few special classes that wrap primitive types:
        // Character, Byte, Short, and Integer.
        // Starting in Java 7 and above, we can also use the String type.
        int month = 3;
        String monthString;
        switch (month) {
            case 1: monthString = "January";
                    break;
            case 2: monthString = "February";
                    break;
            case 3: monthString = "March";
                    break;
            default: monthString = "Some other month";
                     break;
        }
        System.out.println("Switch Case Result: " + monthString);


        // Try-with-resources (Java 7+)
        // Try-catch-finally statements work as expected in Java but in Java 7+
        // the try-with-resources statement is also available. Try-with-resources
        // simplifies try-catch-finally statements by closing resources
        // automatically.

        // In order to use a try-with-resources, include an instance of a class
        // in the try statement. The class must implement java.lang.AutoCloseable.
        try (BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) {
            // You can attempt to do something that could throw an exception.
            System.out.println(br.readLine());
            // In Java 7, the resource will always be closed, even if it throws
            // an Exception.
        } catch (Exception ex) {
            //The resource will be closed before the catch statement executes.
            System.out.println("readLine() failed.");
        }
        // No need for a finally statement in this case, the BufferedReader is
        // already closed. This can be used to avoid certain edge cases where
        // a finally statement might not be called.
        // To learn more:
        // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html


        // Conditional Shorthand
        // You can use the '?' operator for quick assignments or logic forks.
        // Reads as "If (statement) is true, use <first value>, otherwise, use
        // <second value>"
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println("bar : " + bar); // Prints "bar : A", because the 
        // statement is true.
        // Or simply
        System.out.println("bar : " + (foo < 10 ? "A" : "B"));
        

        ////////////////////////////////////////
        // Converting Data Types
        ////////////////////////////////////////

        // Converting data

        // Convert String To Integer
        Integer.parseInt("123");//returns an integer version of "123"

        // Convert Integer To String
        Integer.toString(123);//returns a string version of 123

        // For other conversions check out the following classes:
        // Double
        // Long
        // String

        ///////////////////////////////////////
        // Classes And Functions
        ///////////////////////////////////////

        System.out.println("\n->Classes & Functions");

        // (definition of the Bicycle class follows)

        // Use new to instantiate a class
        Bicycle trek = new Bicycle();

        // Call object methods
        trek.speedUp(3); // You should always use setter and getter methods
        trek.setCadence(100);

        // toString returns this Object's string representation.
        System.out.println("trek info: " + trek.toString());

        // Double Brace Initialization
        // The Java Language has no syntax for how to create static Collections
        // in an easy way. Usually you end up in the following way:
        private static final Set<String> COUNTRIES = new HashSet<String>();
        static {
           COUNTRIES.add("DENMARK");
           COUNTRIES.add("SWEDEN");
           COUNTRIES.add("FINLAND");
        }

        // But there's a nifty way to achieve the same thing in an
        // easier way, by using something that is called Double Brace
        // Initialization.
        private static final Set<String> COUNTRIES = new HashSet<String>() {{
            add("DENMARK");
            add("SWEDEN");
            add("FINLAND");
        }}

        // The first brace is creating a new AnonymousInnerClass and the
        // second one declares an instance initializer block. This block
        // is called when the anonymous inner class is created.
        // This does not only work for Collections, it works for all
        // non-final classes.

    } // End main method
} // End LearnJava class

// You can include other, non-public outer-level classes in a .java file,
// but it is not good practice. Instead split classes into separate files.

// Class Declaration Syntax:
// <public/private/protected> class <class name> {
//    // data fields, constructors, functions all inside.
//    // functions are called as methods in Java.
// }

class Bicycle {

    // Bicycle's Fields/Variables
    public int cadence; // Public: Can be accessed from anywhere
    private int speed;  // Private: Only accessible from within the class
    protected int gear; // Protected: Accessible from the class and subclasses
    String name; // default: Only accessible from within this package
    static String className; // Static class variable

    // Static block
    // Java has no implementation of static constructors, but
    // has a static block that can be used to initialize class variables
    // (static variables).
    // This block will be called when the class is loaded.
    static {
        className = "Bicycle";
    }

    // Constructors are a way of creating classes
    // This is a constructor
    public Bicycle() {
        // You can also call another constructor:
        // this(1, 50, 5, "Bontrager");
        gear = 1;
        cadence = 50;
        speed = 5;
        name = "Bontrager";
    }
    // This is a constructor that takes arguments
    public Bicycle(int startCadence, int startSpeed, int startGear,
        String name) {
        this.gear = startGear;
        this.cadence = startCadence;
        this.speed = startSpeed;
        this.name = name;
    }

    // Method Syntax:
    // <public/private/protected> <return type> <function name>(<args>)

    // Java classes often implement getters and setters for their fields

    // Method declaration syntax:
    // <access modifier> <return type> <method name>(<args>)
    public int getCadence() {
        return cadence;
    }

    // void methods require no return statement
    public void setCadence(int newValue) {
        cadence = newValue;
    }
    public void setGear(int newValue) {
        gear = newValue;
    }
    public void speedUp(int increment) {
        speed += increment;
    }
    public void slowDown(int decrement) {
        speed -= decrement;
    }
    public void setName(String newName) {
        name = newName;
    }
    public String getName() {
        return name;
    }

    //Method to display the attribute values of this Object.
    @Override // Inherited from the Object class.
    public String toString() {
        return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
            " name: " + name;
    }
} // end class Bicycle

// PennyFarthing is a subclass of Bicycle
class PennyFarthing extends Bicycle {
    // (Penny Farthings are those bicycles with the big front wheel.
    // They have no gears.)

    public PennyFarthing(int startCadence, int startSpeed) {
        // Call the parent constructor with super
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // You should mark a method you're overriding with an @annotation.
    // To learn more about what annotations are and their purpose check this
    // out: http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGear(int gear) {
        this.gear = 0;
    }
}

// Object casting
// Since the PennyFarthing class is extending the Bicycle class, we can say
// a PennyFarthing is a Bicycle and write :
// Bicycle bicycle = new PennyFarthing();
// This is called object casting where an object is taken for another one. There
// are lots of details and deals with some more intermediate concepts here:
// https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

// Interfaces
// Interface declaration syntax
// <access-level> interface <interface-name> extends <super-interfaces> {
//     // Constants
//     // Method declarations
// }

// Example - Food:
public interface Edible {
    public void eat(); // Any class that implements this interface, must
                       // implement this method.
}

public interface Digestible {
    public void digest();
    // Since Java 8, interfaces can have default method.
    public void defaultMethod() {
        System.out.println("Hi from default method ...");
    }
}

// We can now create a class that implements both of these interfaces.
public class Fruit implements Edible, Digestible {
    @Override
    public void eat() {
        // ...
    }

    @Override
    public void digest() {
        // ...
    }
}

// In Java, you can extend only one class, but you can implement many
// interfaces. For example:
public class ExampleClass extends ExampleClassParent implements InterfaceOne,
    InterfaceTwo {
    @Override
    public void InterfaceOneMethod() {
    }

    @Override
    public void InterfaceTwoMethod() {
    }

}

// Abstract Classes

// Abstract Class declaration syntax
// <access-level> abstract class <abstract-class-name> extends
// <super-abstract-classes> {
//     // Constants and variables
//     // Method declarations
// }

// Marking a class as abstract means that it contains at least one abstract
// method that must be defined in a child class. Similar to interfaces, abstract
// classes cannot be instantiated, but instead must be extended and the abstract
// methods defined. Different from interfaces, abstract classes can contain a
// mixture of concrete and abstract methods. Methods in an interface cannot
// have a body, unless the method is static, and variables are final by default,
// unlike an abstract class. Also abstract classes CAN have the "main" method.
public abstract class Animal
{
    public abstract void makeSound();

    // Method can have a body
    public void eat()
    {
        System.out.println("I am an animal and I am Eating.");
        // Note: We can access private variable here.
        age = 30;
    }

    // No need to initialize, however in an interface
    // a variable is implicitly final and hence has
    // to be initialized.
    private int age;

    public void printAge()
    {
        System.out.println(age);
    }

    // Abstract classes can have main function.
    public static void main(String[] args)
    {
        System.out.println("I am abstract");
    }
}

class Dog extends Animal
{
    // Note still have to override the abstract methods in the
    // abstract class.
    @Override
    public void makeSound()
    {
        System.out.println("Bark");
        // age = 30;    ==> ERROR!    age is private to Animal
    }

    // NOTE: You will get an error if you used the
    // @Override annotation here, since java doesn't allow
    // overriding of static methods.
    // What is happening here is called METHOD HIDING.
    // Check out this SO post: http://stackoverflow.com/questions/16313649/
    public static void main(String[] args)
    {
        Dog pluto = new Dog();
        pluto.makeSound();
        pluto.eat();
        pluto.printAge();
    }
}

// Final Classes

// Final Class declaration syntax
// <access-level> final <final-class-name> {
//     // Constants and variables
//     // Method declarations
// }

// Final classes are classes that cannot be inherited from and are therefore a
// final child. In a way, final classes are the opposite of abstract classes
// because abstract classes must be extended, but final classes cannot be
// extended.
public final class SaberToothedCat extends Animal
{
    // Note still have to override the abstract methods in the
    // abstract class.
    @Override
    public void makeSound()
    {
        System.out.println("Roar");
    }
}

// Final Methods
public abstract class Mammal()
{
    // Final Method Syntax:
    // <access modifier> final <return type> <function name>(<args>)

    // Final methods, like, final classes cannot be overridden by a child
    // class, and are therefore the final implementation of the method.
    public final boolean isWarmBlooded()
    {
        return true;
    }
}

// Enum Type
//
// An enum type is a special data type that enables for a variable to be a set
// of predefined constants. The variable must be equal to one of the values
// that have been predefined for it. Because they are constants, the names of
// an enum type's fields are in uppercase letters. In the Java programming
// language, you define an enum type by using the enum keyword. For example,
// you would specify a days-of-the-week enum type as:
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

// We can use our enum Day like that:
public class EnumTest {
    // Variable Enum
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;
            case FRIDAY:
                System.out.println("Fridays are better.");
                break;
            case SATURDAY:
            case SUNDAY:
                System.out.println("Weekends are best.");
                break;
            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs(); // => Mondays are bad.
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs(); // => Midweek days are so-so.
    }
}

// Enum types are much more powerful than we show above.
// The enum body can include methods and other fields.
// You can see more at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

```

## Further Reading

The links provided here below are just to get an understanding of the topic, feel free to Google and find specific examples.

**Official Oracle Guides**:

* [Java Tutorial Trail from Sun / Oracle](https://docs.oracle.com/javase/tutorial/index.html)

* [Java Access level modifiers](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Object-Oriented Programming Concepts](https://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Polymorphism](https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Abstraction](https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Exceptions](https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Interfaces](https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java Code Conventions](https://www.oracle.com/technetwork/java/codeconvtoc-136057.html)

* New features in Java 8:
    * [Lambda expressions (functional programming)](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
    * [Date and time API (java.time package)](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html)

**Online Practice and Tutorials**

* [Learneroo.com - Learn Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)

**Books**:

* [Head First Java](http://www.headfirstlabs.com/books/hfjava/)

* [Thinking in Java](http://www.mindview.net/Books/TIJ/)

* [Objects First with Java](https://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660)

* [Java The Complete Reference](https://www.amazon.com/gp/product/0071606300)
---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
filename: javascript.js
---

JavaScript was created by Netscape's Brendan Eich in 1995. It was originally
intended as a simpler scripting language for websites, complementing the use of
Java for more complex web applications, but its tight integration with Web pages
and built-in support in browsers has caused it to become far more common than
Java in web frontends.

JavaScript isn't just limited to web browsers, though: Node.js, a project that
provides a standalone runtime for Google Chrome's V8 JavaScript engine, is
becoming more and more popular.

JavaScript has a C-like syntax, so if you've used languages like C or Java,
a lot of the basic syntax will already be familiar. Despite this, and despite
the similarity in name, JavaScript's object model is significantly different to
Java's.

```js
// Single-line comments start with two slashes.
/* Multiline comments start with slash-star,
   and end with star-slash */

// Statements can be terminated by ;
doStuff();

// ... but they don't have to be, as semicolons are automatically inserted
// wherever there's a newline, except in certain cases.
doStuff()

// Because those cases can cause unexpected results, we'll keep on using
// semicolons in this guide.

///////////////////////////////////
// 1. Numbers, Strings and Operators

// JavaScript has one number type (which is a 64-bit IEEE 754 double).
// Doubles have a 52-bit mantissa, which is enough to store integers
// up to about 9✕10¹⁵ precisely.
3; // = 3
1.5; // = 1.5

// Some basic arithmetic works as you'd expect.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// Including uneven division.
5 / 2; // = 2.5

// And modulo division.
10 % 2; // = 0
30 % 4; // = 2
18.5 % 7; // = 4.5

// Bitwise operations also work; when you perform a bitwise operation your float
// is converted to a signed int *up to* 32 bits.
1 << 2; // = 4

// Precedence is enforced with parentheses.
(1 + 3) * 2; // = 8

// There are three special not-a-real-number values:
Infinity; // result of e.g. 1/0
-Infinity; // result of e.g. -1/0
NaN; // result of e.g. 0/0, stands for 'Not a Number'

// There's also a boolean type.
true;
false;

// Strings are created with ' or ".
'abc';
"Hello, world";

// Negation uses the ! symbol
!true; // = false
!false; // = true

// Equality is ===
1 === 1; // = true
2 === 1; // = false

// Inequality is !==
1 !== 1; // = false
2 !== 1; // = true

// More comparisons
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Strings are concatenated with +
"Hello " + "world!"; // = "Hello world!"

// ... which works with more than just strings
"1, 2, " + 3; // = "1, 2, 3"
"Hello " + ["world", "!"] // = "Hello world,!"

// and are compared with < and >
"a" < "b"; // = true

// Type coercion is performed for comparisons with double equals...
"5" == 5; // = true
null == undefined; // = true

// ...unless you use ===
"5" === 5; // = false
null === undefined; // = false

// ...which can result in some weird behaviour...
13 + !0; // 14
"13" + !0; // '13true'

// You can access characters in a string with `charAt`
"This is a string".charAt(0);  // = 'T'

// ...or use `substring` to get larger pieces.
"Hello world".substring(0, 5); // = "Hello"

// `length` is a property, so don't use ().
"Hello".length; // = 5

// There's also `null` and `undefined`.
null;      // used to indicate a deliberate non-value
undefined; // used to indicate a value is not currently present (although
           // `undefined` is actually a value itself)

// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.
// Note that 0 is falsy and "0" is truthy, even though 0 == "0".

///////////////////////////////////
// 2. Variables, Arrays and Objects

// Variables are declared with the `var` keyword. JavaScript is dynamically
// typed, so you don't need to specify type. Assignment uses a single `=`
// character.
var someVar = 5;

// If you leave the var keyword off, you won't get an error...
someOtherVar = 10;

// ...but your variable will be created in the global scope, not in the scope
// you defined it in.

// Variables declared without being assigned to are set to undefined.
var someThirdVar; // = undefined

// If you want to declare a couple of variables, then you could use a comma
// separator
var someFourthVar = 2, someFifthVar = 4;

// There's shorthand for performing math operations on variables:
someVar += 5; // equivalent to someVar = someVar + 5; someVar is 10 now
someVar *= 10; // now someVar is 100

// and an even-shorter-hand for adding or subtracting 1
someVar++; // now someVar is 101
someVar--; // back to 100

// Arrays are ordered lists of values, of any type.
var myArray = ["Hello", 45, true];

// Their members can be accessed using the square-brackets subscript syntax.
// Array indices start at zero.
myArray[1]; // = 45

// Arrays are mutable and of variable length.
myArray.push("World");
myArray.length; // = 4

// Add/Modify at specific index
myArray[3] = "Hello";

// JavaScript's objects are equivalent to "dictionaries" or "maps" in other
// languages: an unordered collection of key-value pairs.
var myObj = {key1: "Hello", key2: "World"};

// Keys are strings, but quotes aren't required if they're a valid
// JavaScript identifier. Values can be any type.
var myObj = {myKey: "myValue", "my other key": 4};

// Object attributes can also be accessed using the subscript syntax,
myObj["my other key"]; // = 4

// ... or using the dot syntax, provided the key is a valid identifier.
myObj.myKey; // = "myValue"

// Objects are mutable; values can be changed and new keys added.
myObj.myThirdKey = true;

// If you try to access a value that's not yet set, you'll get undefined.
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. Logic and Control Structures

// The `if` structure works as you'd expect.
var count = 1;
if (count == 3){
    // evaluated if count is 3
} else if (count == 4){
    // evaluated if count is 4
} else {
    // evaluated if it's not either 3 or 4
}

// As does `while`.
while (true){
    // An infinite loop!
}

// Do-while loops are like while loops, except they always run at least once.
var input;
do {
    input = getInput();
} while (!isValid(input))

// The `for` loop is the same as C and Java:
// initialization; continue condition; iteration.
for (var i = 0; i < 5; i++){
    // will run 5 times
}

// Breaking out of labeled loops is similar to Java
outer:
for (var i = 0; i < 10; i++) {
    for (var j = 0; j < 10; j++) {
        if (i == 5 && j ==5) {
            break outer;
            // breaks out of outer loop instead of only the inner one
        }
    }
}

// The for/in statement allows iteration over properties of an object.
var description = "";
var person = {fname:"Paul", lname:"Ken", age:18};
for (var x in person){
    description += person[x] + " ";
} // description = 'Paul Ken 18 '

// && is logical and, || is logical or
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear";
}
if (colour == "red" || colour == "blue"){
    // colour is either red or blue
}

// && and || "short circuit", which is useful for setting default values.
var name = otherName || "default";

// The `switch` statement checks for equality with `===`.
// Use 'break' after each case
// or the cases after the correct one will be executed too.
grade = 'B';
switch (grade) {
  case 'A':
    console.log("Great job");
    break;
  case 'B':
    console.log("OK job");
    break;
  case 'C':
    console.log("You can do better");
    break;
  default:
    console.log("Oy vey");
    break;
}


///////////////////////////////////
// 4. Functions, Scope and Closures

// JavaScript functions are declared with the `function` keyword.
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"

// Note that the value to be returned must start on the same line as the
// `return` keyword, otherwise you'll always return `undefined` due to
// automatic semicolon insertion. Watch out for this when using Allman style.
function myFunction(){
    return // <- semicolon automatically inserted here
    {thisIsAn: 'object literal'}
}
myFunction(); // = undefined

// JavaScript functions are first class objects, so they can be reassigned to
// different variable names and passed to other functions as arguments - for
// example, when supplying an event handler:
function myFunction(){
    // this code will be called in 5 seconds' time
}
setTimeout(myFunction, 5000);
// Note: setTimeout isn't part of the JS language, but is provided by browsers
// and Node.js.

// Another function provided by browsers is setInterval
function myFunction(){
    // this code will be called every 5 seconds
}
setInterval(myFunction, 5000);

// Function objects don't even have to be declared with a name - you can write
// an anonymous function definition directly into the arguments of another.
setTimeout(function(){
    // this code will be called in 5 seconds' time
}, 5000);

// JavaScript has function scope; functions get their own scope but other blocks
// do not.
if (true){
    var i = 5;
}
i; // = 5 - not undefined as you'd expect in a block-scoped language

// This has led to a common pattern of "immediately-executing anonymous
// functions", which prevent temporary variables from leaking into the global
// scope.
(function(){
    var temporary = 5;
    // We can access the global scope by assigning to the "global object", which
    // in a web browser is always `window`. The global object may have a
    // different name in non-browser environments such as Node.js.
    window.permanent = 10;
})();
temporary; // raises ReferenceError
permanent; // = 10

// One of JavaScript's most powerful features is closures. If a function is
// defined inside another function, the inner function has access to all the
// outer function's variables, even after the outer function exits.
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!";
    // Inner functions are put in the local scope by default, as if they were
    // declared with `var`.
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout is asynchronous, so the sayHelloInFiveSeconds function will
    // exit immediately, and setTimeout will call inner afterwards. However,
    // because inner is "closed over" sayHelloInFiveSeconds, inner still has
    // access to the `prompt` variable when it is finally called.
}
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s

///////////////////////////////////
// 5. More about Objects; Constructors and Prototypes

// Objects can contain functions.
var myObj = {
    myFunc: function(){
        return "Hello world!";
    }
};
myObj.myFunc(); // = "Hello world!"

// When functions attached to an object are called, they can access the object
// they're attached to using the `this` keyword.
myObj = {
    myString: "Hello world!",
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = "Hello world!"

// What this is set to has to do with how the function is called, not where
// it's defined. So, our function doesn't work if it isn't called in the
// context of the object.
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// Inversely, a function can be assigned to the object and gain access to it
// through `this`, even if it wasn't attached when it was defined.
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"

// We can also specify a context for a function to execute in when we invoke it
// using `call` or `apply`.

var anotherFunc = function(s){
    return this.myString + s;
}
anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"

// The `apply` function is nearly identical, but takes an array for an argument
// list.

anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!"

// This is useful when working with a function that accepts a sequence of
// arguments and you want to pass an array.

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// But, `call` and `apply` are only temporary. When we want it to stick, we can
// use `bind`.

var boundFunc = anotherFunc.bind(myObj);
boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"

// `bind` can also be used to partially apply (curry) a function.

var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// When you call a function with the `new` keyword, a new object is created, and
// made available to the function via the `this` keyword. Functions designed to be
// called like that are called constructors.

var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// Unlike most other popular object-oriented languages, JavaScript has no 
// concept of 'instances' created from 'class' blueprints; instead, JavaScript
// combines instantiation and inheritance into a single concept: a 'prototype'.

// Every JavaScript object has a 'prototype'. When you go to access a property
// on an object that doesn't exist on the actual object, the interpreter will
// look at its prototype.

// Some JS implementations let you access an object's prototype on the magic
// property `__proto__`. While this is useful for explaining prototypes it's not
// part of the standard; we'll get to standard ways of using prototypes later.
var myObj = {
    myString: "Hello world!"
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// This works for functions, too.
myObj.myFunc(); // = "hello world!"

// Of course, if your property isn't on your prototype, the prototype's
// prototype is searched, and so on.
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

// There's no copying involved here; each object stores a reference to its
// prototype. This means we can alter the prototype and our changes will be
// reflected everywhere.
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

// The for/in statement allows iteration over properties of an object,
// walking up the prototype chain until it sees a null prototype.
for (var x in myObj){
    console.log(myObj[x]);
}
///prints:
// Hello world!
// 43
// [Function: myFunc]

// To only consider properties attached to the object itself
// and not its prototypes, use the `hasOwnProperty()` check.
for (var x in myObj){
    if (myObj.hasOwnProperty(x)){
        console.log(myObj[x]);
    }
}
///prints:
// Hello world!

// We mentioned that `__proto__` was non-standard, and there's no standard way to
// change the prototype of an existing object. However, there are two ways to
// create a new object with a given prototype.

// The first is Object.create, which is a recent addition to JS, and therefore
// not available in all implementations yet.
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// The second way, which works anywhere, has to do with constructors.
// Constructors have a property called prototype. This is *not* the prototype of
// the constructor function itself; instead, it's the prototype that new objects
// are given when they're created with that constructor and the new keyword.
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function(){
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// Built-in types like strings and numbers also have constructors that create
// equivalent wrapper objects.
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// Except, they aren't exactly equivalent.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // This code won't execute, because 0 is falsy.
}
if (new Number(0)){
   // This code will execute, because wrapped numbers are objects, and objects
   // are always truthy.
}

// However, the wrapper objects and the regular builtins share a prototype, so
// you can actually add functionality to a string, for instance.
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// This fact is often used in "polyfilling", which is implementing newer
// features of JavaScript in an older subset of JavaScript, so that they can be
// used in older environments such as outdated browsers.

// For instance, we mentioned that Object.create isn't yet available in all
// implementations, but we can still use it with this polyfill:
if (Object.create === undefined){ // don't overwrite it if it exists
    Object.create = function(proto){
        // make a temporary constructor with the right prototype
        var Constructor = function(){};
        Constructor.prototype = proto;
        // then use it to create a new, appropriately-prototyped object
        return new Constructor();
    }
}
```

## Further Reading

The [Mozilla Developer Network][1] provides excellent documentation for
JavaScript as it's used in browsers. Plus, it's a wiki, so as you learn more you
can help others out by sharing your own knowledge.

MDN's [A re-introduction to JavaScript][2] covers much of the concepts covered
here in more detail. This guide has quite deliberately only covered the
JavaScript language itself; if you want to learn more about how to use
JavaScript in web pages, start by learning about the [Document Object Model][3].

[Learn Javascript by Example and with Challenges][4] is a variant of this
reference with built-in challenges.

[JavaScript Garden][5] is an in-depth guide of all the counter-intuitive parts
of the language.

[JavaScript: The Definitive Guide][6] is a classic guide and reference book.

[Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with
attached terminal

[Eloquent Javascript - The Annotated Version][9] by Gordon Zhu is also a great
derivative of Eloquent Javascript with extra explanations and clarifications for
some of the more complicated examples.

[Javascript: The Right Way][10] is a guide intended to introduce new developers
to JavaScript and help experienced developers learn more about its best practices.

In addition to direct contributors to this article, some content is adapted from
Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the
Mozilla Developer Network.


[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript
[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
[3]: https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core
[4]: http://www.learneroo.com/modules/64/nodes/350
[5]: http://bonsaiden.github.io/JavaScript-Garden/
[6]: http://www.amazon.com/gp/product/0596805527/
[7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
[8]: http://eloquentjavascript.net/
[9]: http://watchandcode.com/courses/eloquent-javascript-the-annotated-version
[10]: http://jstherightway.org/
---
category: tool
tool: jquery
contributors:
    - ["Sawyer Charles", "https://github.com/xssc"]
filename: jquery.js
---

jQuery is a JavaScript library that helps you "do more, write less". It makes many common JavaScript tasks and makes them easier to write. jQuery is used by many big companies and developers everywhere. It makes AJAX, event handling, document manipulation, and much more, easier and faster.

Because jQuery is a JavaScript library you should [learn JavaScript first](https://learnxinyminutes.com/docs/javascript/)

```js


///////////////////////////////////
// 1. Selectors

// Selectors in jQuery are used to select an element
var page = $(window); // Selects the whole viewport

// Selectors can also be CSS selector
var paragraph = $('p'); // Selects all paragraph elements
var table1 = $('#table1'); // Selects element with id 'table1'
var squares = $('.square'); // Selects all elements with the class 'square'
var square_p = $('p.square') // Selects paragraphs with the 'square' class


///////////////////////////////////
// 2. Events and Effects
// jQuery is very good at handling what happens when an event is triggered
// A very common event used is the ready event on the document
// You can use the 'ready' method to wait until the element has finished loading
$(document).ready(function(){
  // Code won't execute until the document is loaded
});
// You can also use defined functions
function onAction() {
  // This is executed when the event is triggered
}
$('#btn').click(onAction); // Invokes onAction on click

// Some other common events are:
$('#btn').dblclick(onAction); // Double click
$('#btn').hover(onAction); // Hovering over
$('#btn').focus(onAction); // On focus
$('#btn').blur(onAction); // Losses focus
$('#btn').submit(onAction); // On submit
$('#btn').select(onAction); // When an element is selected
$('#btn').keydown(onAction); // When a key is pushed down
$('#btn').keyup(onAction); // When a key is released
$('#btn').keypress(onAction); // When a key is pressed
$('#btn').mousemove(onAction); // When the mouse is moved
$('#btn').mouseenter(onAction); // Mouse enters the element
$('#btn').mouseleave(onAction); // Mouse leaves the element


// These can all also trigger the event instead of handling it
// by simply not giving any parameters
$('#btn').dblclick(); // Fires double click on the element

// You can handle multiple events while only using the selector once
$('#btn').on(
  {dblclick: myFunction1} // Triggered on double click
  {blur: myFunction1} // Triggered on blur
);

// You can move and hide elements with some effect methods
$('.table').hide(); // Hides the element(s)

// Note: calling a function in these methods will still hide the element
$('.table').hide(function(){
    // Element hidden then function executed
});

// You can store selectors in variables
var tables = $('.table');

// Some basic document manipulation methods are:
tables.hide(); // Hides element(s)
tables.show(); // Shows (un-hides) element(s)
tables.toggle(); // Changes the hide/show state
tables.fadeOut(); // Fades out
tables.fadeIn(); // Fades in
tables.fadeToggle(); // Fades in or out
tables.fadeTo(0.5); // Fades to an opacity (between 0 and 1)
tables.slideUp(); // Slides up
tables.slideDown(); // Slides down
tables.slideToggle(); // Slides up or down

// All of the above take a speed (milliseconds) and callback function
tables.hide(1000, myFunction); // 1 second hide animation then function

// fadeTo has a required opacity as its second parameter
tables.fadeTo(2000, 0.1, myFunction); // 2 sec. fade to 0.1 opacity then function

// You can get slightly more advanced with the animate method
tables.animate({margin-top:"+=50", height: "100px"}, 500, myFunction);
// The animate method takes an object of css and values to end with,
// optional options parameter to tune the animation,
// and of course the callback function

///////////////////////////////////
// 3. Manipulation

// These are similar to effects but can do more
$('div').addClass('taming-slim-20'); // Adds class taming-slim-20 to all div 

// Common manipulation methods
$('p').append('Hello world'); // Adds to end of element
$('p').attr('class'); // Gets attribute
$('p').attr('class', 'content'); // Sets attribute
$('p').hasClass('taming-slim-20'); // Returns true if it has the class
$('p').height(); // Gets height of element or sets height


// For many manipulation methods, getting info on an element
// will ONLY get the first matching element
$('p').height(); // Gets only the first 'p' tag's height

// You can use each to loop through all the elements
var heights = [];
$('p').each(function() {
  heights.push($(this).height()); // Adds all 'p' tag heights to array
});


```
---
language: json
filename: learnjson.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
  - ["Michael Neth", "https://github.com/infernocloud"]
---

JSON is an extremely simple data-interchange format. As [json.org](http://json.org) says, it is easy for humans to read and write and for machines to parse and generate.

A piece of JSON must represent either:

* A collection of name/value pairs (`{ }`). In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
* An ordered list of values (`[ ]`). In various languages, this is realized as an array, vector, list, or sequence.
 an array/list/sequence (`[ ]`) or a dictionary/object/associated array (`{ }`).

JSON in its purest form has no actual comments, but most parsers will accept C-style (`//`, `/* */`) comments. Some parsers also tolerate a trailing comma (i.e. a comma after the last element of an array or the after the last property of an object), but they should be avoided for better compatibility.

For the purposes of this tutorial, everything is going to be 100% valid JSON. Luckily, it kind of speaks for itself.

Supported data types:

* Strings: `"hello"`, `"\"A quote.\""`, `"\u0abe"`, `"Newline.\n"`
* Numbers: `23`, `0.11`, `12e10`, `3.141e-10`, `1.23e+4`
* Objects: `{ "key": "value" }`
* Arrays: `["Values"]`
* Miscellaneous: `true`, `false`, `null`

```json
{
  "key": "value",

  "keys": "must always be enclosed in double quotes",
  "numbers": 0,
  "strings": "Hellø, wørld. All unicode is allowed, along with \"escaping\".",
  "has bools?": true,
  "nothingness": null,

  "big number": 1.2e+100,

  "objects": {
    "comment": "Most of your structure will come from objects.",

    "array": [0, 1, 2, 3, "Arrays can have anything in them.", 5],

    "another object": {
      "comment": "These things can be nested, very useful."
    }
  },

  "silliness": [
    {
      "sources of potassium": ["bananas"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "alternative style": {
    "comment": "check this out!"
  , "comma position": "doesn't matter, if it's before the next key, it's valid"
  , "another comment": "how nice"
  },



  "whitespace": "Does not matter.",



  "that was short": "And done. You now know everything JSON has to offer."
}
```

## Further Reading

* [JSON.org](http://json.org) All of JSON beautifully explained using flowchart-like graphics.
---
language: Julia
contributors:
    - ["Leah Hanson", "http://leahhanson.us"]
    - ["Pranit Bauva", "http://github.com/pranitbauva1997"]
filename: learnjulia.jl
---

Julia is a new homoiconic functional language focused on technical computing.
While having the full power of homoiconic macros, first-class functions, and low-level control, Julia is as easy to learn and use as Python.

This is based on Julia 0.4.

```ruby

# Single line comments start with a hash (pound) symbol.
#= Multiline comments can be written
   by putting '#=' before the text  and '=#'
   after the text. They can also be nested.
=#

####################################################
## 1. Primitive Datatypes and Operators
####################################################

# Everything in Julia is an expression.

# There are several basic types of numbers.
3 # => 3 (Int64)
3.2 # => 3.2 (Float64)
2 + 1im # => 2 + 1im (Complex{Int64})
2//3 # => 2//3 (Rational{Int64})

# All of the normal infix operators are available.
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
5 / 2 # => 2.5 # dividing an Int by an Int always results in a Float
div(5, 2) # => 2 # for a truncated result, use div
5 \ 35 # => 7.0
2 ^ 2 # => 4 # power, not bitwise xor
12 % 10 # => 2

# Enforce precedence with parentheses
(1 + 3) * 2 # => 8

# Bitwise Operators
~2 # => -3   # bitwise not
3 & 5 # => 1 # bitwise and
2 | 4 # => 6 # bitwise or
2 $ 4 # => 6 # bitwise xor
2 >>> 1 # => 1 # logical shift right
2 >> 1  # => 1 # arithmetic shift right
2 << 1  # => 4 # logical/arithmetic shift left

# You can use the bits function to see the binary representation of a number.
bits(12345)
# => "0000000000000000000000000000000000000000000000000011000000111001"
bits(12345.0)
# => "0100000011001000000111001000000000000000000000000000000000000000"

# Boolean values are primitives
true
false

# Boolean operators
!true # => false
!false # => true
1 == 1 # => true
2 == 1 # => false
1 != 1 # => false
2 != 1 # => true
1 < 10 # => true
1 > 10 # => false
2 <= 2 # => true
2 >= 2 # => true
# Comparisons can be chained
1 < 2 < 3 # => true
2 < 3 < 2 # => false

# Strings are created with "
"This is a string."

# Julia has several types of strings, including ASCIIString and UTF8String.
# More on this in the Types section.

# Character literals are written with '
'a'

# Some strings can be indexed like an array of characters
"This is a string"[1] # => 'T' # Julia indexes from 1
# However, this is will not work well for UTF8 strings,
# so iterating over strings is recommended (map, for loops, etc).

# $ can be used for string interpolation:
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
# You can put any Julia expression inside the parentheses.

# Another way to format strings is the printf macro.
@printf "%d is less than %f" 4.5 5.3 # 5 is less than 5.300000

# Printing is easy
println("I'm Julia. Nice to meet you!")

# String can be compared lexicographically
"good" > "bye" # => true
"good" == "good" # => true
"1 + 2 = 3" == "1 + 2 = $(1+2)" # => true

####################################################
## 2. Variables and Collections
####################################################

# You don't declare variables before assigning to them.
some_var = 5 # => 5
some_var # => 5

# Accessing a previously unassigned variable is an error
try
    some_other_var # => ERROR: some_other_var not defined
catch e
    println(e)
end

# Variable names start with a letter or underscore.
# After that, you can use letters, digits, underscores, and exclamation points.
SomeOtherVar123! = 6 # => 6

# You can also use certain unicode characters
☃ = 8 # => 8
# These are especially handy for mathematical notation
2 * π # => 6.283185307179586

# A note on naming conventions in Julia:
#
# * Word separation can be indicated by underscores ('_'), but use of
#   underscores is discouraged unless the name would be hard to read
#   otherwise.
#
# * Names of Types begin with a capital letter and word separation is shown
#   with CamelCase instead of underscores.
#
# * Names of functions and macros are in lower case, without underscores.
#
# * Functions that modify their inputs have names that end in !. These
#   functions are sometimes called mutating functions or in-place functions.

# Arrays store a sequence of values indexed by integers 1 through n:
a = Int64[] # => 0-element Int64 Array

# 1-dimensional array literals can be written with comma-separated values.
b = [4, 5, 6] # => 3-element Int64 Array: [4, 5, 6]
b = [4; 5; 6] # => 3-element Int64 Array: [4, 5, 6]
b[1] # => 4
b[end] # => 6

# 2-dimensional arrays use space-separated values and semicolon-separated rows.
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]

# Arrays of a particular Type
b = Int8[4, 5, 6] # => 3-element Int8 Array: [4, 5, 6]

# Add stuff to the end of a list with push! and append!
push!(a,1)     # => [1]
push!(a,2)     # => [1,2]
push!(a,4)     # => [1,2,4]
push!(a,3)     # => [1,2,4,3]
append!(a,b) # => [1,2,4,3,4,5,6]

# Remove from the end with pop
pop!(b)        # => 6 and b is now [4,5]

# Let's put it back
push!(b,6)   # b is now [4,5,6] again.

a[1] # => 1 # remember that Julia indexes from 1, not 0!

# end is a shorthand for the last index. It can be used in any
# indexing expression
a[end] # => 6

# we also have shift and unshift
shift!(a) # => 1 and a is now [2,4,3,4,5,6]
unshift!(a,7) # => [7,2,4,3,4,5,6]

# Function names that end in exclamations points indicate that they modify
# their argument.
arr = [5,4,6] # => 3-element Int64 Array: [5,4,6]
sort(arr) # => [4,5,6]; arr is still [5,4,6]
sort!(arr) # => [4,5,6]; arr is now [4,5,6]

# Looking out of bounds is a BoundsError
try
    a[0] # => ERROR: BoundsError() in getindex at array.jl:270
    a[end+1] # => ERROR: BoundsError() in getindex at array.jl:270
catch e
    println(e)
end

# Errors list the line and file they came from, even if it's in the standard
# library. If you built Julia from source, you can look in the folder base
# inside the julia folder to find these files.

# You can initialize arrays from ranges
a = [1:5;] # => 5-element Int64 Array: [1,2,3,4,5]

# You can look at ranges with slice syntax.
a[1:3] # => [1, 2, 3]
a[2:end] # => [2, 3, 4, 5]

# Remove elements from an array by index with splice!
arr = [3,4,5]
splice!(arr,2) # => 4 ; arr is now [3,5]

# Concatenate lists with append!
b = [1,2,3]
append!(a,b) # Now a is [1, 2, 3, 4, 5, 1, 2, 3]

# Check for existence in a list with in
in(1, a) # => true

# Examine the length with length
length(a) # => 8

# Tuples are immutable.
tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple.
tup[1] # => 1
try:
    tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
catch e
    println(e)
end

# Many list functions also work on tuples
length(tup) # => 3
tup[1:2] # => (1,2)
in(2, tup) # => true

# You can unpack tuples into variables
a, b, c = (1, 2, 3) # => (1,2,3)  # a is now 1, b is now 2 and c is now 3

# Tuples are created even if you leave out the parentheses
d, e, f = 4, 5, 6 # => (4,5,6)

# A 1-element tuple is distinct from the value it contains
(1,) == 1 # => false
(1) == 1 # => true

# Look how easy it is to swap two values
e, d = d, e  # => (5,4) # d is now 5 and e is now 4


# Dictionaries store mappings
empty_dict = Dict() # => Dict{Any,Any}()

# You can create a dictionary using a literal
filled_dict = Dict("one"=> 1, "two"=> 2, "three"=> 3)
# => Dict{ASCIIString,Int64}

# Look up values with []
filled_dict["one"] # => 1

# Get all keys
keys(filled_dict)
# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# Note - dictionary keys are not sorted or in the order you inserted them.

# Get all values
values(filled_dict)
# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# Note - Same as above regarding key ordering.

# Check for existence of keys in a dictionary with in, haskey
in(("one" => 1), filled_dict) # => true
in(("two" => 3), filled_dict) # => false
haskey(filled_dict, "one") # => true
haskey(filled_dict, 1) # => false

# Trying to look up a non-existent key will raise an error
try
    filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489
catch e
    println(e)
end

# Use the get method to avoid that error by providing a default value
# get(dictionary,key,default_value)
get(filled_dict,"one",4) # => 1
get(filled_dict,"four",4) # => 4

# Use Sets to represent collections of unordered, unique values
empty_set = Set() # => Set{Any}()
# Initialize a set with values
filled_set = Set([1,2,2,3,4]) # => Set{Int64}(1,2,3,4)

# Add more values to a set
push!(filled_set,5) # => Set{Int64}(5,4,2,3,1)

# Check if the values are in the set
in(2, filled_set) # => true
in(10, filled_set) # => false

# There are functions for set intersection, union, and difference.
other_set = Set([3, 4, 5, 6]) # => Set{Int64}(6,4,5,3)
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
setdiff(Set([1,2,3,4]),Set([2,3,5])) # => Set{Int64}(1,4)


####################################################
## 3. Control Flow
####################################################

# Let's make a variable
some_var = 5

# Here is an if statement. Indentation is not meaningful in Julia.
if some_var > 10
    println("some_var is totally bigger than 10.")
elseif some_var < 10    # This elseif clause is optional.
    println("some_var is smaller than 10.")
else                    # The else clause is optional too.
    println("some_var is indeed 10.")
end
# => prints "some var is smaller than 10"


# For loops iterate over iterables.
# Iterable types include Range, Array, Set, Dict, and AbstractString.
for animal=["dog", "cat", "mouse"]
    println("$animal is a mammal")
    # You can use $ to interpolate variables or expression into strings
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# You can use 'in' instead of '='.
for animal in ["dog", "cat", "mouse"]
    println("$animal is a mammal")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for a in Dict("dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal")
    println("$(a[1]) is a $(a[2])")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for (k,v) in Dict("dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal")
    println("$k is a $v")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# While loops loop while a condition is true
x = 0
while x < 4
    println(x)
    x += 1  # Shorthand for x = x + 1
end
# prints:
#   0
#   1
#   2
#   3

# Handle exceptions with a try/catch block
try
   error("help")
catch e
   println("caught it $e")
end
# => caught it ErrorException("help")


####################################################
## 4. Functions
####################################################

# The keyword 'function' creates new functions
#function name(arglist)
#  body...
#end
function add(x, y)
    println("x is $x and y is $y")

    # Functions return the value of their last statement
    x + y
end

add(5, 6) # => 11 after printing out "x is 5 and y is 6"

# Compact assignment of functions
f_add(x, y) = x + y # => "f (generic function with 1 method)"
f_add(3, 4) # => 7

# Function can also return multiple values as tuple
f(x, y) = x + y, x - y
f(3, 4) # => (7, -1)

# You can define functions that take a variable number of
# positional arguments
function varargs(args...)
    return args
    # use the keyword return to return anywhere in the function
end
# => varargs (generic function with 1 method)

varargs(1,2,3) # => (1,2,3)

# The ... is called a splat.
# We just used it in a function definition.
# It can also be used in a function call,
# where it will splat an Array or Tuple's contents into the argument list.
add([5,6]...) # this is equivalent to add(5,6)

x = (5,6)     # => (5,6)
add(x...)     # this is equivalent to add(5,6)


# You can define functions with optional positional arguments
function defaults(a,b,x=5,y=6)
    return "$a $b and $x $y"
end

defaults('h','g') # => "h g and 5 6"
defaults('h','g','j') # => "h g and j 6"
defaults('h','g','j','k') # => "h g and j k"
try
    defaults('h') # => ERROR: no method defaults(Char,)
    defaults() # => ERROR: no methods defaults()
catch e
    println(e)
end

# You can define functions that take keyword arguments
function keyword_args(;k1=4,name2="hello") # note the ;
    return Dict("k1"=>k1,"name2"=>name2)
end

keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4]
keyword_args(k1="mine") # => ["k1"=>"mine","name2"=>"hello"]
keyword_args() # => ["name2"=>"hello","k1"=>4]

# You can combine all kinds of arguments in the same function
function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo")
    println("normal arg: $normal_arg")
    println("optional arg: $optional_positional_arg")
    println("keyword arg: $keyword_arg")
end

all_the_args(1, 3, keyword_arg=4)
# prints:
#   normal arg: 1
#   optional arg: 3
#   keyword arg: 4

# Julia has first class functions
function create_adder(x)
    adder = function (y)
        return x + y
    end
    return adder
end

# This is "stabby lambda syntax" for creating anonymous functions
(x -> x > 2)(3) # => true

# This function is identical to create_adder implementation above.
function create_adder(x)
    y -> x + y
end

# You can also name the internal function, if you want
function create_adder(x)
    function adder(y)
        x + y
    end
    adder
end

add_10 = create_adder(10)
add_10(3) # => 13


# There are built-in higher order functions
map(add_10, [1,2,3]) # => [11, 12, 13]
filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7]

# We can use list comprehensions for nicer maps
[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13]
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]

####################################################
## 5. Types
####################################################

# Julia has a type system.
# Every value has a type; variables do not have types themselves.
# You can use the `typeof` function to get the type of a value.
typeof(5) # => Int64

# Types are first-class values
typeof(Int64) # => DataType
typeof(DataType) # => DataType
# DataType is the type that represents types, including itself.

# Types are used for documentation, optimizations, and dispatch.
# They are not statically checked.

# Users can define types
# They are like records or structs in other languages.
# New types are defined using the `type` keyword.

# type Name
#   field::OptionalType
#   ...
# end
type Tiger
  taillength::Float64
  coatcolor # not including a type annotation is the same as `::Any`
end

# The default constructor's arguments are the properties
# of the type, in the order they are listed in the definition
tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange")

# The type doubles as the constructor function for values of that type
sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire")

# These struct-style types are called concrete types
# They can be instantiated, but cannot have subtypes.
# The other kind of types is abstract types.

# abstract Name
abstract Cat # just a name and point in the type hierarchy

# Abstract types cannot be instantiated, but can have subtypes.
# For example, Number is an abstract type
subtypes(Number) # => 2-element Array{Any,1}:
                 #     Complex{T<:Real}
                 #     Real
subtypes(Cat) # => 0-element Array{Any,1}

# AbstractString, as the name implies, is also an abstract type
subtypes(AbstractString)    # 8-element Array{Any,1}:
                            #  Base.SubstitutionString{T<:AbstractString}
                            #  DirectIndexString
                            #  RepString
                            #  RevString{T<:AbstractString}
                            #  RopeString
                            #  SubString{T<:AbstractString}
                            #  UTF16String
                            #  UTF8String

# Every type has a super type; use the `super` function to get it.
typeof(5) # => Int64
super(Int64) # => Signed
super(Signed) # => Integer
super(Integer) # => Real
super(Real) # => Number
super(Number) # => Any
super(super(Signed)) # => Real
super(Any) # => Any
# All of these type, except for Int64, are abstract.
typeof("fire") # => ASCIIString
super(ASCIIString) # => DirectIndexString
super(DirectIndexString) # => AbstractString
# Likewise here with ASCIIString

# <: is the subtyping operator
type Lion <: Cat # Lion is a subtype of Cat
  mane_color
  roar::AbstractString
end

# You can define more constructors for your type
# Just define a function of the same name as the type
# and call an existing constructor to get a value of the correct type
Lion(roar::AbstractString) = Lion("green",roar)
# This is an outer constructor because it's outside the type definition

type Panther <: Cat # Panther is also a subtype of Cat
  eye_color
  Panther() = new("green")
  # Panthers will only have this constructor, and no default constructor.
end
# Using inner constructors, like Panther does, gives you control
# over how values of the type can be created.
# When possible, you should use outer constructors rather than inner ones.

####################################################
## 6. Multiple-Dispatch
####################################################

# In Julia, all named functions are generic functions
# This means that they are built up from many small methods
# Each constructor for Lion is a method of the generic function Lion.

# For a non-constructor example, let's make a function meow:

# Definitions for Lion, Panther, Tiger
function meow(animal::Lion)
  animal.roar # access type properties using dot notation
end

function meow(animal::Panther)
  "grrr"
end

function meow(animal::Tiger)
  "rawwwr"
end

# Testing the meow function
meow(tigger) # => "rawwr"
meow(Lion("brown","ROAAR")) # => "ROAAR"
meow(Panther()) # => "grrr"

# Review the local type hierarchy
issubtype(Tiger,Cat) # => false
issubtype(Lion,Cat) # => true
issubtype(Panther,Cat) # => true

# Defining a function that takes Cats
function pet_cat(cat::Cat)
  println("The cat says $(meow(cat))")
end

pet_cat(Lion("42")) # => prints "The cat says 42"
try
    pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,)
catch e
    println(e)
end

# In OO languages, single dispatch is common;
# this means that the method is picked based on the type of the first argument.
# In Julia, all of the argument types contribute to selecting the best method.

# Let's define a function with more arguments, so we can see the difference
function fight(t::Tiger,c::Cat)
  println("The $(t.coatcolor) tiger wins!")
end
# => fight (generic function with 1 method)

fight(tigger,Panther()) # => prints The orange tiger wins!
fight(tigger,Lion("ROAR")) # => prints The orange tiger wins!

# Let's change the behavior when the Cat is specifically a Lion
fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!")
# => fight (generic function with 2 methods)

fight(tigger,Panther()) # => prints The orange tiger wins!
fight(tigger,Lion("ROAR")) # => prints The green-maned lion wins!

# We don't need a Tiger in order to fight
fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))")
# => fight (generic function with 3 methods)

fight(Lion("balooga!"),Panther()) # => prints The victorious cat says grrr
try
  fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion)
catch
end

# Also let the cat go first
fight(c::Cat,l::Lion) = println("The cat beats the Lion")
# => Warning: New definition
#    fight(Cat,Lion) at none:1
# is ambiguous with
#    fight(Lion,Cat) at none:2.
# Make sure
#    fight(Lion,Lion)
# is defined first.
#fight (generic function with 4 methods)

# This warning is because it's unclear which fight will be called in:
fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The victorious cat says rarrr
# The result may be different in other versions of Julia

fight(l::Lion,l2::Lion) = println("The lions come to a tie")
fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The lions come to a tie


# Under the hood
# You can take a look at the llvm  and the assembly code generated.

square_area(l) = l * l      # square_area (generic function with 1 method)

square_area(5) #25

# What happens when we feed square_area an integer?
code_native(square_area, (Int32,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1              # Prologue
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    movsxd  RAX, EDI        # Fetch l from memory?
	#	    imul    RAX, RAX        # Square l and store the result in RAX
	#	    pop RBP                 # Restore old base pointer
	#	    ret                     # Result will still be in RAX

code_native(square_area, (Float32,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vmulss  XMM0, XMM0, XMM0  # Scalar single precision multiply (AVX)
	#	    pop RBP
	#	    ret

code_native(square_area, (Float64,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vmulsd  XMM0, XMM0, XMM0 # Scalar double precision multiply (AVX)
	#	    pop RBP
	#	    ret
	#
# Note that julia will use floating point instructions if any of the
# arguments are floats.
# Let's calculate the area of a circle
circle_area(r) = pi * r * r     # circle_area (generic function with 1 method)
circle_area(5)                  # 78.53981633974483

code_native(circle_area, (Int32,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vcvtsi2sd   XMM0, XMM0, EDI          # Load integer (r) from memory
	#	    movabs  RAX, 4593140240              # Load pi
	#	    vmulsd  XMM1, XMM0, QWORD PTR [RAX]  # pi * r
	#	    vmulsd  XMM0, XMM0, XMM1             # (pi * r) * r
	#	    pop RBP
	#	    ret
	#

code_native(circle_area, (Float64,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	    movabs  RAX, 4593140496
	#	Source line: 1
	#	    vmulsd  XMM1, XMM0, QWORD PTR [RAX]
	#	    vmulsd  XMM0, XMM1, XMM0
	#	    pop RBP
	#	    ret
	#
```

## Further Reading

You can get a lot more detail from [The Julia Manual](http://docs.julialang.org/en/latest/#Manual-1)

The best place to get help with Julia is the (very friendly) [Discourse forum](https://discourse.julialang.org/).
---
language: kdb+
contributors:
    - ["Matt Doherty", "https://github.com/picodoc"]
    - ["Jonny Press", "https://github.com/jonnypress"]
filename: learnkdb.q
---

The q language and its database component kdb+ were developed by Arthur Whitney
and released by Kx systems in 2003. q is a descendant of APL and as such is
very terse and a little strange looking for anyone from a "C heritage" language
background. Its expressiveness and vector oriented nature make it well suited
to performing complex calculations on large amounts of data (while also
encouraging some amount of [code
golf](https://en.wikipedia.org/wiki/Code_golf)). The fundamental structure in
the language is not the object but instead the list, and tables are built as
collections of lists. This means - unlike most traditional RDBMS systems -
tables are column oriented.  The language has both an in-memory and on-disk
database built in, giving a large amount of flexibility. kdb+ is most widely
used in the world of finance to store, analyze, process and retrieve large
time-series data sets.

The terms *q* and *kdb+* are usually used interchangeably, as the two are not
separable so this distinction is not really useful.

All Feedback welcome!  You can reach me at matt.doherty@aquaq.co.uk, or Jonny
at jonny.press@aquaq.co.uk

To learn more about kdb+ you can join the [Personal kdb+](https://groups.google.com/forum/#!forum/personal-kdbplus) or [TorQ kdb+](https://groups.google.com/forum/#!forum/kdbtorq) group.

```
/ Single line comments start with a forward-slash
/ These can also be used in-line, so long as at least one whitespace character
/ separates it from text to the left
/
  A forward-slash on a line by itself starts a multiline comment
  and a backward-slash on a line by itself terminates it
\

/ Run this file in an empty directory


////////////////////////////////////
// Basic Operators and Datatypes  //
////////////////////////////////////

/ We have integers, which are 8 byte by default
3 / => 3

/ And floats, also 8 byte as standard.  Trailing f distinguishes from int
3.0 / => 3f

/ 4 byte numerical types can also be specified with trailing chars
3i / => 3i
3.0e / => 3e

/ Math is mostly what you would expect
1+1 / => 2
8-1 / => 7
10*2 / => 20
/ Except division, which uses percent (%) instead of forward-slash (/)
35%5 / => 7f  (the result of division is always a float)

/ For integer division we have the keyword div
4 div 3 / => 1

/ Modulo also uses a keyword, since percent (%) is taken
4 mod 3 / => 1

/ And exponentiation...
2 xexp 4 / => 16

/ ...and truncating...
floor 3.14159 / => 3

/ ...getting the absolute value...
abs -3.14159 / => 3.14159
/ ...and many other things
/ see http://code.kx.com/wiki/Reference for more

/ q has no operator precedence, everything is evaluated right to left
/ so results like this might take some getting used to
2*1+1 / => 4 / (no operator precedence tables to remember!)

/ Precedence can be modified with parentheses (restoring the 'normal' result)
(2*1)+1 / => 3

/ Assignment uses colon (:) instead of equals (=)
/ No need to declare variables before assignment
a:3
a / => 3

/ Variables can also be assigned in-line
/ this does not affect the value passed on
c:3+b:2+a:1 / (data "flows" from right to left)
a / => 1
b / => 3
c / => 6

/ In-place operations are also as you might expect
a+:2
a / => 3

/ There are no "true" or "false" keywords in q
/ boolean values are indicated by the bit value followed by b
1b / => true value
0b / => false value

/ Equality comparisons use equals (=) (since we don't need it for assignment)
1=1 / => 1b
2=1 / => 0b

/ Inequality uses <>
1<>1 / => 0b
2<>1 / => 1b

/ The other comparisons are as you might expect
1<2 / => 1b
1>2 / => 0b
2<=2 / => 1b
2>=2 / => 1b

/ Comparison is not strict with regard to types...
42=42.0 / => 1b

/ ...unless we use the match operator (~)
/ which only returns true if entities are identical
42~42.0 / => 0b

/ The not operator returns true if the underlying value is zero
not 0b / => 1b
not 1b / => 0b
not 42 / => 0b
not 0.0 / => 1b

/ The max operator (|) reduces to logical "or" for bools
42|2.0 / => 42f
1b|0b / => 1b

/ The min operator (&) reduces to logical "and" for bools
42&2.0 / => 2f
1b&0b / => 0b

/ q provides two ways to store character data
/ Chars in q are stored in a single byte and use double-quotes (")
ch:"a"
/ Strings are simply lists of char (more on lists later)
str:"This is a string"
/ Escape characters work as normal
str:"This is a string with \"quotes\""

/ Char data can also be stored as symbols using backtick (`)
symbol:`sym
/ Symbols are NOT LISTS, they are an enumeration
/ the q process stores internally a vector of strings
/ symbols are enumerated against this vector
/ this can be more space and speed efficient as these are constant width

/ The string function converts to strings
string `symbol / => "symbol"
string 1.2345 / => "1.2345"

/ q has a time type...
t:01:00:00.000
/ date type...
d:2015.12.25
/ and a datetime type (among other time types)
dt:2015.12.25D12:00:00.000000000

/ These support some arithmetic for easy manipulation
dt + t / => 2015.12.25D13:00:00.000000000
t - 00:10:00.000 / => 00:50:00.000
/ and can be decomposed using dot notation
d.year / => 2015i
d.mm / => 12i
d.dd / => 25i
/ see http://code.kx.com/wiki/JB:QforMortals2/atoms#Temporal_Data for more

/ q also has an infinity value so div by zero will not throw an error
1%0 / => 0w
-1%0 / => -0w

/ And null types for representing missing values
0N / => null int
0n / => null float
/ see http://code.kx.com/wiki/JB:QforMortals2/atoms#Null_Values for more

/ q has standard control structures
/ if is as you might expect (; separates the condition and instructions)
if[1=1;a:"hi"]
a / => "hi"
/ if-else uses $ (and unlike if, returns a value)
$[1=0;a:"hi";a:"bye"] / => "bye"
a / => "bye"
/ if-else can be extended to multiple clauses by adding args separated by ;
$[1=0;a:"hi";0=1;a:"bye";a:"hello again"]
a / => "hello again"


////////////////////////////////////
////      Data Structures       ////
////////////////////////////////////

/ q is not an object oriented language
/ instead complexity is built through ordered lists
/ and mapping them into higher order structures: dictionaries and tables

/ Lists (or arrays if you prefer) are simple ordered collections
/ they are defined using parentheses () and semi-colons (;)
(1;2;3) / => 1 2 3
(-10.0;3.14159e;1b;`abc;"c")
/ => -10f
/ => 3.14159e
/ => 1b
/ => `abc
/ => "c"  (mixed type lists are displayed on multiple lines)
((1;2;3);(4;5;6);(7;8;9))
/ => 1 2 3
/ => 4 5 6
/ => 7 8 9

/ Lists of uniform type can also be defined more concisely
1 2 3 / => 1 2 3
`list`of`syms / => `list`of`syms
`list`of`syms ~ (`list;`of;`syms) / => 1b

/ List length
count (1;2;3) / => 3
count "I am a string" / => 13 (string are lists of char)

/ Empty lists are defined with parentheses
l:()
count l / => 0

/ Simple variables and single item lists are not equivalent
/ parentheses syntax cannot create a single item list (they indicate precedence)
(1)~1 / => 1b
/ single item lists can be created using enlist
singleton:enlist 1
/ or appending to an empty list
singleton:(),1
1~(),1 / => 0b

/ Speaking of appending, comma (,) is used for this, not plus (+)
1 2 3,4 5 6 / => 1 2 3 4 5 6
"hello ","there" / => "hello there"

/ Indexing uses square brackets []
l:1 2 3 4
l[0] / => 1
l[1] / => 2
/ indexing out of bounds returns a null value rather than an error
l[5] / => 0N
/ and indexed assignment
l[0]:5
l / => 5 2 3 4

/ Lists can also be used for indexing and indexed assignment
l[1 3] / => 2 4
l[1 3]: 1 3
l / => 5 1 3 3

/ Lists can be untyped/mixed type
l:(1;2;`hi)
/ but once they are uniformly typed, q will enforce this
l[2]:3
l / => 1 2 3
l[2]:`hi / throws a type error
/ this makes sense in the context of lists as table columns (more later)

/ For a nested list we can index at depth
l:((1;2;3);(4;5;6);(7;8;9))
l[1;1] / => 5

/ We can elide the indexes to return entire rows or columns
l[;1] / => 2 5 8
l[1;] / => 4 5 6

/ All the functions mentioned in the previous section work on lists natively
1+(1;2;3) / => 2 3 4 (single variable and list)
(1;2;3) - (3;2;1) / => -2 0 2 (list and list)

/ And there are many more that are designed specifically for lists
avg 1 2 3 / => 2f
sum 1 2 3 / => 6
sums 1 2 3 / => 1 3 6 (running sum)
last 1 2 3 / => 3
1 rotate 1 2 3 / => 2 3 1
/ etc.
/ Using and combining these functions to manipulate lists is where much of the
/ power and expressiveness of the language comes from

/ Take (#), drop (_) and find (?) are also useful working with lists
l:1 2 3 4 5 6 7 8 9
l:1+til 9 / til is a useful shortcut for generating ranges
/ take the first 5 elements
5#l / => 1 2 3 4 5
/ drop the first 5
5_l / => 6 7 8 9
/ take the last 5
-5#l / => 5 6 7 8 9
/ drop the last 5
-5_l / => 1 2 3 4
/ find the first occurrence of 4
l?4 / => 3
l[3] / => 4

/ Dictionaries in q are a generalization of lists
/ they map a list to another list (of equal length)
/ the bang (!) symbol is used for defining a dictionary
d:(`a;`b;`c)!(1;2;3)
/ or more simply with concise list syntax
d:`a`b`c!1 2 3
/ the keyword key returns the first list
key d / => `a`b`c
/ and value the second
value d / => 1 2 3

/ Indexing is identical to lists
/ with the first list as a key instead of the position
d[`a] / => 1
d[`b] / => 2

/ As is assignment
d[`c]:4
d
/ => a| 1
/ => b| 2
/ => c| 4

/ Arithmetic and comparison work natively, just like lists
e:(`a;`b;`c)!(2;3;4)
d+e
/ => a| 3
/ => b| 5
/ => c| 8
d-2
/ => a| -1
/ => b| 0
/ => c| 2
d > (1;1;1)
/ => a| 0
/ => b| 1
/ => c| 1

/ And the take, drop and find operators are remarkably similar too
`a`b#d
/ => a| 1
/ => b| 2
`a`b _ d
/ => c| 4
d?2
/ => `b

/ Tables in q are basically a subset of dictionaries
/ a table is a dictionary where all values must be lists of the same length
/ as such tables in q are column oriented (unlike most RDBMS)
/ the flip keyword is used to convert a dictionary to a table
/ i.e. flip the indices
flip `c1`c2`c3!(1 2 3;4 5 6;7 8 9)
/ => c1 c2 c3
/ => --------
/ => 1  4  7
/ => 2  5  8
/ => 3  6  9
/ we can also define tables using this syntax
t:([]c1:1 2 3;c2:4 5 6;c3:7 8 9)
t
/ => c1 c2 c3
/ => --------
/ => 1  4  7
/ => 2  5  8
/ => 3  6  9

/ Tables can be indexed and manipulated in a similar way to dicts and lists
t[`c1]
/ => 1 2 3
/ table rows are returned as dictionaries
t[1]
/ => c1| 2
/ => c2| 5
/ => c3| 8

/ meta returns table type information
meta t
/ => c | t f a
/ => --| -----
/ => c1| j
/ => c2| j
/ => c3| j
/ now we see why type is enforced in lists (to protect column types)
t[1;`c1]:3
t[1;`c1]:3.0 / throws a type error

/ Most traditional databases have primary key columns
/ in q we have keyed tables, where one table containing key columns
/ is mapped to another table using bang (!)
k:([]id:1 2 3)
k!t
/ => id| c1 c2 c3
/ => --| --------
/ => 1 | 1  4  7
/ => 2 | 3  5  8
/ => 3 | 3  6  9

/ We can also use this shortcut for defining keyed tables
kt:([id:1 2 3]c1:1 2 3;c2:4 5 6;c3:7 8 9)

/ Records can then be retrieved based on this key
kt[1]
/ => c1| 1
/ => c2| 4
/ => c3| 7
kt[`id!1]
/ => c1| 1
/ => c2| 4
/ => c3| 7


////////////////////////////////////
////////     Functions      ////////
////////////////////////////////////

/ In q the function is similar to a mathematical map, mapping inputs to outputs
/ curly braces {} are used for function definition
/ and square brackets [] for calling functions (just like list indexing)
/ a very minimal function
f:{x+x}
f[2] / => 4

/ Functions can be anonymous and called at point of definition
{x+x}[2] / => 4

/ By default the last expression is returned
/ colon (:) can be used to specify return
{x+x}[2] / => 4
{:x+x}[2] / => 4
/ semi-colon (;) separates expressions
{r:x+x;:r}[2] / => 4

/ Function arguments can be specified explicitly (separated by ;)
{[arg1;arg2] arg1+arg2}[1;2] / => 3
/ or if omitted will default to x, y and z
{x+y+z}[1;2;3] / => 6

/ Built in functions are no different, and can be called the same way (with [])
+[1;2] / => 3
<[1;2] / => 1b

/ Functions are first class in q, so can be returned, stored in lists etc.
{:{x+y}}[] / => {x+y}
(1;"hi";{x+y})
/ => 1
/ => "hi"
/ => {x+y}

/ There is no overloading and no keyword arguments for custom q functions
/ however using a dictionary as a single argument can overcome this
/ allows for optional arguments or differing functionality
d:`arg1`arg2`arg3!(1.0;2;"my function argument")
{x[`arg1]+x[`arg2]}[d] / => 3f

/ Functions in q see the global scope
a:1
{:a}[] / => 1

/ However local scope obscures this
a:1
{a:2;:a}[] / => 2
a / => 1

/ Functions cannot see nested scopes (only local and global)
{local:1;{:local}[]}[] / throws error as local is not defined in inner function

/ A function can have one or more of its arguments fixed (projection)
f:+[4]
f[4] / => 8
f[5] / => 9
f[6] / => 10


////////////////////////////////////
//////////     q-sql      //////////
////////////////////////////////////

/ q has its own syntax for manipulating tables, similar to standard SQL
/ This contains the usual suspects of select, insert, update etc.
/ and some new functionality not typically available
/ q-sql has two significant differences (other than syntax) to normal SQL:
/ - q tables have well defined record orders
/ - tables are stored as a collection of columns
/   (so vectorized column operations are fast)
/ a full description of q-sql is a little beyond the scope of this intro
/ so we will just cover enough of the basics to get you going

/ First define ourselves a table
t:([]name:`Arthur`Thomas`Polly;age:35 32 52;height:180 175 160;sex:`m`m`f)

/ equivalent of SELECT * FROM t
select from t / (must be lower case, and the wildcard is not necessary)
/ => name   age height sex
/ => ---------------------
/ => Arthur 35  180    m
/ => Thomas 32  175    m
/ => Polly  52  160    f

/ Select specific columns
select name,age from t
/ => name   age
/ => ----------
/ => Arthur 35
/ => Thomas 32
/ => Polly  52

/ And name them (equivalent of using AS in standard SQL)
select charactername:name, currentage:age from t
/ => charactername currentage
/ => ------------------------
/ => Arthur        35
/ => Thomas        32
/ => Polly         52

/ This SQL syntax is integrated with the q language
/ so q can be used seamlessly in SQL statements
select name, feet:floor height*0.032, inches:12*(height*0.032) mod 1 from t
/ => name   feet inches
/ => ------------------
/ => Arthur 5    9.12
/ => Thomas 5    7.2
/ => Polly  5    1.44

/ Including custom functions
select name, growth:{[h;a]h%a}[height;age] from t
/ => name   growth
/ => ---------------
/ => Arthur 5.142857
/ => Thomas 5.46875
/ => Polly  3.076923

/ The where clause can contain multiple statements separated by commas
select from t where age>33,height>175
/ => name   age height sex
/ => ---------------------
/ => Arthur 35  180    m

/ The where statements are executed sequentially (not the same as logical AND)
select from t where age<40,height=min height
/ => name   age height sex
/ => ---------------------
/ => Thomas 32  175    m
select from t where (age<40)&(height=min height)
/ => name age height sex
/ => -------------------

/ The by clause falls between select and from
/ and is equivalent to SQL's GROUP BY
select avg height by sex from t
/ => sex| height
/ => ---| ------
/ => f  | 160
/ => m  | 177.5

/ If no aggreation function is specified, last is assumed
select by sex from t
/ => sex| name   age height
/ => ---| -----------------
/ => f  | Polly  52  160
/ => m  | Thomas 32  175

/ Update has the same basic form as select
update sex:`male from t where sex=`m
/ => name   age height sex
/ => ----------------------
/ => Arthur 35  180    male
/ => Thomas 32  175    male
/ => Polly  52  160    f

/ As does delete
delete from t where sex=`m
/ => name  age height sex
/ => --------------------
/ => Polly 52  160    f

/ None of these sql operations are carried out in place
t
/ => name   age height sex
/ => ---------------------
/ => Arthur 35  180    m
/ => Thomas 32  175    m
/ => Polly  52  160    f

/ Insert however is in place, it takes a table name, and new data
`t insert (`John;25;178;`m) / => ,3
t
/ => name   age height sex
/ => ---------------------
/ => Arthur 35  180    m
/ => Thomas 32  175    m
/ => Polly  52  160    f
/ => John   25  178    m

/ Upsert is similar (but doesn't have to be in-place)
t upsert (`Chester;58;179;`m)
/ => name    age height sex
/ => ----------------------
/ => Arthur  35  180    m
/ => Thomas  32  175    m
/ => Polly   52  160    f
/ => John    25  178    m
/ => Chester 58  179    m

/ it will also upsert dicts or tables
t upsert `name`age`height`sex!(`Chester;58;179;`m)
t upsert (`Chester;58;179;`m)
/ => name    age height sex
/ => ----------------------
/ => Arthur  35  180    m
/ => Thomas  32  175    m
/ => Polly   52  160    f
/ => John    25  178    m
/ => Chester 58  179    m

/ And if our table is keyed
kt:`name xkey t
/ upsert will replace records where required
kt upsert ([]name:`Thomas`Chester;age:33 58;height:175 179;sex:`f`m)
/ => name   | age height sex
/ => -------| --------------
/ => Arthur | 35  180    m
/ => Thomas | 33  175    f
/ => Polly  | 52  160    f
/ => John   | 25  178    m
/ => Chester| 58  179    m

/ There is no ORDER BY clause in q-sql, instead use xasc/xdesc
`name xasc t
/ => name   age height sex
/ => ---------------------
/ => Arthur 35  180    m
/ => John   25  178    m
/ => Polly  52  160    f
/ => Thomas 32  175    m

/ Most of the standard SQL joins are present in q-sql, plus a few new friends
/ see http://code.kx.com/wiki/JB:QforMortals2/queries_q_sql#Joins
/ the two most important (commonly used) are lj and aj

/ lj is basically the same as SQL LEFT JOIN
/ where the join is carried out on the key columns of the left table
le:([sex:`m`f]lifeexpectancy:78 85)
t lj le
/ => name   age height sex lifeexpectancy
/ => ------------------------------------
/ => Arthur 35  180    m   78
/ => Thomas 32  175    m   78
/ => Polly  52  160    f   85
/ => John   25  178    m   78

/ aj is an asof join.  This is not a standard SQL join, and can be very powerful
/ The canonical example of this is joining financial trades and quotes tables
trades:([]time:10:01:01 10:01:03 10:01:04;sym:`msft`ibm`ge;qty:100 200 150)
quotes:([]time:10:01:00 10:01:01 10:01:01 10:01:03;
          sym:`ibm`msft`msft`ibm; px:100 99 101 98)
aj[`time`sym;trades;quotes]
/ => time     sym  qty px
/ => ---------------------
/ => 10:01:01 msft 100 101
/ => 10:01:03 ibm  200 98
/ => 10:01:04 ge   150
/ for each row in the trade table, the last (prevailing) quote (px) for that sym
/ is joined on.
/ see http://code.kx.com/wiki/JB:QforMortals2/queries_q_sql#Asof_Join

////////////////////////////////////
/////     Extra/Advanced      //////
////////////////////////////////////

////// Adverbs //////
/ You may have noticed the total lack of loops to this point
/ This is not a mistake!
/ q is a vector language so explicit loops (for, while etc.) are not encouraged
/ where possible functionality should be vectorized (i.e. operations on lists)
/ adverbs supplement this, modifying the behaviour of functions
/ and providing loop type functionality when required
/ (in q functions are sometimes referred to as verbs, hence adverbs)
/ the "each" adverb modifies a function to treat a list as individual variables
first each (1 2 3;4 5 6;7 8 9)
/ => 1 4 7

/ each-left (\:) and each-right (/:) modify a two-argument function
/ to treat one of the arguments and individual variables instead of a list
1 2 3 +\: 1 2 3
/ => 2 3 4
/ => 3 4 5
/ => 4 5 6
1 2 3 +/: 1 2 3
/ => 2 3 4
/ => 3 4 5
/ => 4 5 6

/ The true alternatives to loops in q are the adverbs scan (\) and over (/)
/ their behaviour differs based on the number of arguments the function they
/ are modifying receives.  Here I'll summarise some of the most useful cases
/ a single argument function modified by scan given 2 args behaves like "do"
{x * 2}\[5;1] / => 1 2 4 8 16 32 (i.e. multiply by 2, 5 times)
{x * 2}/[5;1] / => 32 (using over only the final result is shown)

/ If the first argument is a function, we have the equivalent of "while"
{x * 2}\[{x<100};1] / => 1 2 4 8 16 32 64 128 (iterates until returns 0b)
{x * 2}/[{x<100};1] / => 128 (again returns only the final result)

/ If the function takes two arguments, and we pass a list, we have "for"
/ where the result of the previous execution is passed back into the next loop
/ along with the next member of the list
{x + y}\[1 2 3 4 5] / => 1 3 6 10 15 (i.e. the running sum)
{x + y}/[1 2 3 4 5] / => 15 (only the final result)

/ There are other adverbs and uses, this is only intended as quick overview
/ http://code.kx.com/wiki/JB:QforMortals2/functions#Adverbs

////// Scripts //////
/ q scripts can be loaded from a q session using the "\l" command
/ for example "\l learnkdb.q" will load this script
/ or from the command prompt passing the script as an argument
/ for example "q learnkdb.q"

////// On-disk data //////
/ Tables can be persisted to disk in several formats
/ the two most fundamental are serialized and splayed
t:([]a:1 2 3;b:1 2 3f)
`:serialized set t / saves the table as a single serialized file
`:splayed/ set t / saves the table splayed into a directory

/ the dir structure will now look something like:
/ db/
/ ├── serialized
/ └── splayed
/     ├── a
/     └── b

/ Loading this directory (as if it was as script, see above)
/ loads these tables into the q session
\l .
/ the serialized table will be loaded into memory
/ however the splayed table will only be mapped, not loaded
/ both tables can be queried using q-sql
select from serialized
/ => a b
/ => ---
/ => 1 1
/ => 2 2
/ => 3 3
select from splayed / (the columns are read from disk on request)
/ => a b
/ => ---
/ => 1 1
/ => 2 2
/ => 3 3
/ see http://code.kx.com/wiki/JB:KdbplusForMortals/contents for more

////// Frameworks //////
/ kdb+ is typically used for data capture and analysis.
/ This involves using an architecture with multiple processes
/ working together.  kdb+ frameworks are available to streamline the setup
/ and configuration of this architecture and add additional functionality
/ such as disaster recovery, logging, access, load balancing etc.
/ https://github.com/AquaQAnalytics/TorQ
```

## Want to know more?

* [*q for mortals* q language tutorial](http://code.kx.com/wiki/JB:QforMortals2/contents)
* [*kdb for mortals* on disk data tutorial](http://code.kx.com/wiki/JB:KdbplusForMortals/contents)
* [q language reference](http://code.kx.com/wiki/Reference)
* [Online training courses](http://training.aquaq.co.uk/)
* [TorQ production framework](https://github.com/AquaQAnalytics/TorQ)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
    - ["Jonathan Wang", "https://github.com/Jonathansw"]   
    - ["Leo Rudberg", "https://github.com/LOZORD"]
    - ["Betsy Lorton", "https://github.com/schbetsy"]
    - ["John Detter", "https://github.com/jdetter"]
translators:
    - ["Wooseop Kim", "https://github.com/linterpreteur"]
filename: LearnBash-kr.sh
lang: ko-kr
---

Bash는 유닉스 셸의 이름이며, 리눅스와 맥 OS X의 기본 셸로 그리고 GNU 운영체제를 위한 셸로서 배포되었습니다.
이하의 거의 모든 예시들은 셸 스크립트의 일부이거나 셸에서 바로 실행할 수 있습니다.

[(영어) 이곳에서 더 알아보세요.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# 스크립트의 첫 줄은 시스템에게 스크립트의 실행법을 알려주는 '셔뱅'입니다.
# https://ko.wikipedia.org/wiki/%EC%85%94%EB%B1%85
# 이미 보았듯이 주석은 #으로 시작합니다. 셔뱅 또한 주석입니다.

# 간단한 헬로 월드
echo 헬로 월드!

# 각각의 명령어는 개행 혹은 세미콜론 이후에 시작됩니다.
echo '첫번째 줄'; echo '두번째 줄'

# 변수 선언은 다음과 같습니다.
Variable="어떤 문자열"

# 하지만 다음은 틀린 형태입니다.
Variable = "어떤 문자열"
# Bash는 Variable이 실행해야 하는 명령어라고 판단할 것이고, 해당 명령어를 찾을
# 수 없기 때문에 에러를 발생시킬 것입니다.

# 다음도 같습니다.
Variable= '어떤 문자열'
# Bash는 '어떤 문자열'이 실행해야 하는 명령어라고 판단하여 에러를 발생시킬 것입니다.
# (이 경우에 'Variable=' 부분은 '어떤 문자열' 명령어의 스코프에서만 유효한
# 변수 할당으로 해석됩니다.)

# 변수 사용은 다음과 같습니다.
echo $Variable
echo "$Variable"
echo '$Variable'
# 할당, 내보내기 등 변수 자체를 사용할 때에는 $ 없이 이름을 적습니다.
# 변수의 값을 사용할 때에는 $를 사용해야 합니다.
# 작은 따옴표는 변수를 확장시키지 않는다는 사실에 주의하세요.
# (역자 주: '$Variable'은 변수 Variable의 값이 아닌 문자열 "$Variable"입니다.)

# 인수 확장은 ${ }입니다.
echo ${Variable}
# 이는 인수 확장의 간단한 예시입니다.
# 인수 확장은 변수로부터 값을 받아 그 값을 "확장"하거나 출력합니다.
# 확장을 통해 인수나 그 값이 변경될 수 있습니다.
# 이하는 확장에 대한 다른 예시들입니다.

# 변수에서의 문자열 치환
echo ${Variable/Some/A}
# 처음으로 나타나는 "Some"를 "A"로 치환합니다.

# 변수의 부분열
Length=7
echo ${Variable:0:Length}
# 변수 값에서 처음 7개 문자만을 반환합니다.

# 변수의 기본값
echo ${Foo:-"Foo가_없거나_비어_있을_때의_기본값"}
# null(Foo=) 값이나 빈 문자열(Foo="")일 경우에만 작동합니다. 0은 (Foo=0)은 0입니다.
# 기본값을 반환할 뿐 변수 값을 변경하지는 않는다는 사실에 주목하세요.

# 중괄호 확장 { }
# 임의의 문자열을 생성합니다.
echo {1..10}
echo {a..z}
# 시작 값으로부터 끝 값까지의 범위를 출력합니다.

# 내장 변수
# 유용한 내장 변수들이 있습니다.
echo "마지막 프로그램의 반환값: $?"
echo "스크립트의 PID: $$"
echo "스크립트에 넘겨진 인자의 개수: $#"
echo "스크립트에 넘겨진 모든 인자: $@"
echo "각각 변수로 쪼개진 스크립트 인자: $1 $2..."

# echo와 변수의 사용법을 알게 되었으니,
# bash의 기초를 조금 더 배워봅시다!

# 현재 디렉토리는 `pwd` 명령어로 알 수 있습니다.
# `pwd`는 "print working directory(작업 디렉토리 출력)"의 약자입니다.
# 내장 변수`$PWD`를 사용할 수도 있습니다.
# 이하는 모두 동일합니다.
echo "I'm in $(pwd)" # `pwd`를 실행하여 문자열에 보간
echo "I'm in $PWD" # 변수를 보간

# 터미널이나 결과의 출력물이 너무 많다면
# 명령어 `clear`를 이용해 화면을 지울 수 있습니다.
clear
# 컨트롤+L 또한 화면을 지울 수 있습니다.

# 입력 값 읽기
echo "이름이 뭐에요?"
read Name # 변수 선언이 필요 없다는 데 주목하세요.
echo $Name님, 안녕하세요!

# 평범한 if 구조도 있습니다.
# 'man test'로 조건문에 대해 더 알아보세요.
if [ $Name != $USER ]
then
    echo "사용자가 아닙니다."
else
    echo "사용자입니다."
fi

# $Name이 비어 있다면, bash는 위의 조건을 다음과 같이 인식합니다.
if [ != $USER ]
# 이는 문법적으로 유효하지 않습니다.
# 따라서 bash에서 비어 있을 수 있는 변수를 "안전하게" 사용하는 법은 다음과 같습니다.
if [ "$Name" != $USER ] ...
# $Name이 비어 있다면 bash는
if [ "" != $USER ] ...
# 와 같이 인식하여 예상한 대로 동작합니다.

# 조건부 실행도 있습니다.
echo "항상 실행" || echo "첫 명령어가 실패해야 실행"
echo "항상 실행" && echo "첫 명령어가 실패하지 않아야 실행"

# if문과 함께 &&와 ||을 사용하려면, 대괄호가 여러 쌍 필요합니다.
if [ "$Name" == "철수" ] && [ "$Age" -eq 15 ]
then
    echo "$Name이 철수이고 $Age가 15일 때 실행"
fi

if [ "$Name" == "민희" ] || [ "$Name" == "상민" ]
then
    echo "$Name이 민희이거나 상민일 때 실행"
fi

# 표현식은 다음 형식으로 표기됩니다.
echo $(( 10 + 5 ))

# 다른 프로그래밍 언어와는 달리, bash는 셸이기 때문에 현재 디렉토리의 컨텍스트에서
# 실행됩니다. 현재 디렉토리의 파일과 디렉토리를 ls 명령어로 나열할 수 있습니다.
ls

# 다음은 실행을 제어하는 옵션의 예시입니다.
ls -l # 모든 파일과 디렉토리를 분리된 줄에 나열
ls -t # 디렉토리 내용을 마지막으로 수정된 날짜(내림차순)에 따라 정렬
ls -R # 이 디렉토리와 그 안의 모든 디렉토리에 대해 재귀적으로 `ls` 실행

# 이전 명령어의 결과는 다음 명령어에 입력될 수 있습니다.
# grep 명령어는 입력을 주어진 패턴에 따라 필터링합니다. 다음은 현재 디렉토리의
# .txt 파일을 나열하는 방법입니다.
ls -l | grep "\.txt"

# `cat`을 이용해 stdout으로 파일을 출력합니다.
cat file.txt

# `cat`으로 파일을 읽을 수도 있습니다.
Contents=$(cat file.txt)
echo "파일 시작\n$Contents\n파일 끝"

# `cp`를 이용해 파일이나 디렉토리를 다른 곳으로 복사할 수 있습니다.
# `cp`는 원본의 새로운 버전을 생성하므로 사본을 편집하는 것은
# 원본에 영향을 주지 않으며 그 반대도 마찬가지입니다.
# 목표 위치에 이미 파일이 있다면 덮어쓰게 됩니다.
cp srcFile.txt clone.txt
cp -r srcDirectory/ dst/ # 재귀적으로 복사

# 컴퓨터 간에 파일을 공유하려고 한다면 `scp` 혹은 `sftp`를 사용합니다.
# `scp`는 `cp`와 매우 유사하게 동작하며
# `sftp`는 더 상호작용적입니다.

# `mv`로 파일 혹은 디렉토리를 다른 곳으로 이동합니다.
# `mv`는 `cp`와 유사하지만 원본을 삭제합니다.
# 또한 `mv`로 파일의 이름을 바꿀 수도 있습니다.
mv s0urc3.txt dst.txt # sorry, l33t hackers...

# bash는 현재 디렉토리의 컨텍스트에서 실행되기 때문에, 다른 디렉토리에서 명령어를
# 실행하고 싶으실 수 있습니다. cd를 이용해 위치를 변경합니다.
cd ~    # 홈 디렉토리로 변경
cd ..   # 한 디렉토리 위로 이동
        # (즉 /home/username/Downloads에서 /home/username로)
cd /home/username/Documents   # 특정 디렉토리로 이동
cd ~/Documents/..    # 아직도 홈 디렉토리... 아닌가??

# 서브셸로 디렉토리를 넘어서 작업할 수도 있습니다.
(echo "처음엔 여기 $PWD") && (cd 어딘가; echo "이제는 여기 $PWD")
pwd # 아직도 첫 디렉토리에 있음

# `mkdir`로 새 디렉토리를 만듭니다.
mkdir myNewDir
# `-p` 플래그는 필요하다면 해당 디렉토리의 경로 중간에 있는 디렉토리를 생성합니다.
mkdir -p myNewDir/with/intermediate/directories

# (stdin, stdout, stderr로) 명령어의 입출력을 리디렉션할 수 있습니다.
# stdin의 내용을 ^EOF$까지 읽고 hello.py에 그 내용을 덮어씁니다.
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# stdin, stdoutk, stderr을 다양한 방법으로 리디렉션하여 hello.py를 실행합니다.
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# 출력 오류는 이미 파일이 있을 경우 덮어쓰지만,
# 덮어쓰는 대신에 내용에 추가하고 싶다면 ">>"를 사용합니다.
python hello.py >> "output.out" 2>> "error.err"

# output.out에 덮어쓰고, error.err에 추가하고, 줄을 세기
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# 명령어를 실행하고 그 파일 디스크립터를 출력 (예: /dev/fd/123)
# man fd 참고
echo <(echo "#helloworld")

# output.out을 "#helloworld"으로 덮어쓰기
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# 임시 파일을 지울 수 있습니다. ('-i'로 대화식 실행)
# 경고: `rm` 명령어는 되돌릴 수 없습니다.
rm -v output.out error.err output-and-error.log
rm -r tempDir/ # 재귀적으로 삭제

# 다른 명령어에서 $()을 이용해 명령어를 치환할 수도 있습니다.
# 다음 명령어는 현재 디렉토리의 파일 및 디렉토리의 수를 표시합니다.
echo "$(ls | wc -l)개 항목이 있습니다."

# 백틱(``)을 이용할 수도 있지만 이 방식을 이용하면 중첩할 수 없기 때문에
# $()을 사용하는 것이 더 좋습니다.
echo "`ls | wc -l`개 항목이 있습니다."

# 자바나 C++의 switch와 비슷하게 동작하는 case 문을 사용할 수 있습니다.
case "$Variable" in
    # 충족시킬 조건을 나열
    0) echo "0입니다.";;
    1) echo "1입니다.";;
    *) echo "널이 아닌 값입니다.";;
esac

# for 반복문은 주어진 인자만큼 반복합니다.
# 다음은 $Variable을 세 번 출력합니다.
for Variable in {1..3}
do
    echo "$Variable"
done

# 혹은 "전통적인 for 반복문" 방식을 쓸 수도 있습니다.
for ((a=1; a <= 3; a++))
do
    echo $a
done

# 파일에도 적용될 수 있습니다.
# 다음은 file1과 file2에 'cat' 명령어를 실행합니다.
for Variable in file1 file2
do
    cat "$Variable"
done

# 혹은 명령어의 결과에도 이용할 수 있습니다.
# 다음은 ls의 결과를 cat합니다.
for Output in $(ls)
do
    cat "$Output"
done

# while 반복문
while [ true ]
do
    echo "반복문 몸체"
    break
done

# 함수를 정의할 수도 있습니다.
# 정의:
function foo ()
{
    echo "인자는 함수 인자처럼 작동합니다. $@"
    echo "그리고 $1 $2..."
    echo "함수입니다."
    return 0
}

# 혹은 단순하게
bar ()
{
    echo "함수를 선언하는 다른 방법"
    return 0
}

# 함수 호출
foo "My name is" $Name

# 몇 가지 유용한 명령어를 알아두면 좋습니다.
# file.txt의 마지막 10줄 출력
tail -n 10 file.txt
# file.txt의 첫 10줄 출력
head -n 10 file.txt
# file.txt 줄 별로 정렬
sort file.txt
# 중복되는 줄을 생략하거나 -d를 이용하여 보고
uniq -d file.txt
# ',' 문자 이전의 첫 열만 출력
cut -d ',' -f 1 file.txt
# file.txt에서 'okay'를 모두 'great'로 교체 (정규식 호환)
sed -i 's/okay/great/g' file.txt
# file.txt에서 정규식에 맞는 모든 줄을 stdin에 출력
# 다음 예시는 "foo"로 시작해 "bar"로 끝나는 줄 출력
grep "^foo.*bar$" file.txt
# "-c" 옵션을 넘겨 줄 번호를 대신 출력
grep -c "^foo.*bar$" file.txt
# 다른 유용한 옵션
grep -r "^foo.*bar$" someDir/ # 재귀적으로 `grep`
grep -n "^foo.*bar$" file.txt # 줄 번호 매기기
grep -rI "^foo.*bar$" someDir/ # 재귀적으로 `grep`하되 바이너리 파일은 무시
# 같은 검색으로 시작하여 "baz"를 포함하는 줄만 필터
grep "^foo.*bar$" file.txt | grep -v "baz"

# 정규식이 아니라 문자열로 검색하고 싶다면
# fgrep 혹은 grep -F
fgrep "foobar" file.txt

# trap 명령어로 스크립트에서 신호를 받을 때 명령어를 실행할 수 있습니다.
# 다음 명령어는 셋 중 한 가지 신호를 받으면 rm 명령어를 실행합니다.
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM

# `sudo`를 통해 슈퍼이용자로 명령어를 실행합니다.
NAME1=$(whoami)
NAME2=$(sudo whoami)
echo "$NAME1였다가 더 강한 $NAME2가 되었다"

# 'help' 명령어로 내장 문서를 읽을 수 있습니다.
help
help help
help for
help return
help source
help .

# man으로 매뉴얼을 읽을 수도 있습니다.
apropos bash
man 1 bash
man bash

# info 명령어로 문서를 읽습니다. (?로 도움말)
apropos info | grep '^info.*('
man info
info info
info 5 info

# bash의 info 문서를 읽어 보세요.
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: bf
filename: learnbf-kr.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["JongChan Choi", "http://0xABCDEF.com/"]
    - ["Peter Lee", "http://peterjlee.com/"]
lang: ko-kr
---

Brainfuck(문장을 시작하는 단어가 아닌이상 첫글자는 대문자를 사용하지 않습니다)은
여덟가지 명령어만으로 튜링-완전한 최소주의 프로그래밍 언어입니다.

```
"><+-.,[]" 이외의 문자들은 무시됩니다. (쌍따옴표는 제외)

브레인퍽은 30,000 칸 짜리의 0으로 초기화된 배열과,
현재 칸을 가르키는 포인터로 표현됩니다.

여덟가지의 명령어는 다음과 같습니다:
+ : 포인터가 가르키는 현재 칸의 값을 1 증가시킵니다.
- : 포인터가 가르키는 현재 칸의 값을 1 감소시킵니다.
> : 포인터가 다음 칸(오른쪽 칸)을 가르키도록 이동시킵니다.
< : 포인터가 이전 칸(왼쪽 칸)을 가르키도록 이동시킵니다.
. : 현재 칸의 값을 ASCII 문자로 출력합니다. (즉, 65 = 'A')
, : 하나의 문자를 입력받고 그 값을 현재 칸에 대입합니다.
[ : 현재 칸의 값이 0이면 짝이 맞는 ] 명령으로 넘어갑니다.
    0이 아니면 다음 명령어로 넘어갑니다.
] : 현재 칸의 값이 0이면 다음 명령어로 넘어갑니다.
    0이 아니면 짝이 맞는 [ 명령으로 다시 돌아갑니다.

[이랑 ]은 while 루프를 만들어냅니다. 무조건, 짝이 맞아야 합니다.

몇가지 간단한 브레인퍽 프로그램을 보겠습니다.

++++++ [ > ++++++++++ < - ] > +++++ .

이 프로그램은 문자 'A'를 출력합니다. 처음에는, 반복할 횟수를 정하기 위한 값을
만들기 위해 첫번째 칸의 값을 6으로 증가시킵니다. 그리고 루프로 들어가서([)
두번째 칸으로 넘어갑니다. 루프 안에서는 두번째 칸의 값을 10 증가시키고,
다시 첫번째 칸으로 넘어가서 값을 1 감소시킵니다. 이 루프는 여섯번 돕니다.
(첫번째 칸의 값을 6번 감소시켜서 0이 될 때 까지는 ] 명령을 만날 때마다
루프의 시작 지점으로 돌아갑니다)

이 시점에서, 두번째 칸의 값은 60이고, 포인터는 값이 0인 첫번째 칸에 위치합니다.
여기서 두번째 칸으로 넘어간 다음 값을 5 증가시키면 두번째 칸의 값이 65가 되고,
65는 문자 'A'에 대응하는 아스키 코드이기 때문에, 두번째 칸의 값을 출력하면
터미널에 'A'가 출력됩니다.

, [ > + < - ] > .

이 프로그램은 사용자로부터 문자 하나를 입력받아 첫번째 칸에 집어넣습니다.
그리고 루프에 들어가서, 두번째 칸으로 넘어가 값을 한 번 증가시킨 다음,
다시 첫번째 칸으로 넘어가서 값을 한 번 감소시킵니다.
이는 첫번째 칸의 값이 0이 될 때까지 지속되며,
두번째 칸은 첫번째 칸이 갖고있던 값을 가지게 됩니다.
루프가 종료되면 포인터는 첫번째 칸을 가르키기 때문에 두번째 칸으로 넘어가고,
해당 아스키 코드에 대응하는 문자를 출력합니다.

또한 공백문자는 순전히 가독성을 위해서 작성되었다는 것을 기억하세요.
다음과 같이 작성해도 똑같이 돌아갑니다:

,[>+<-]>.

한 번 돌려보고 아래의 프로그램이 실제로 무슨 일을 하는지 맞춰보세요:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

이 프로그램은 두 개의 숫자를 입력받은 뒤, 그 둘을 곱합니다.

위 코드는 일단 두 번의 입력을 받고, 첫번째 칸의 값만큼 바깥 루프를 돕니다.
그리고 루프 안에서 다시 두번째 칸의 값만큼 안쪽의 루프를 돕니다.
그리고 그 루프에서는 세번째 칸의 값을 증가시키는데, 문제가 하나 있습니다:
내부 루프가 한 번 끝나게 되면 두번째 칸의 값은 0이 됩니다.
그럼 다시 바깥 루프를 돌 때에 안쪽의 루프를 돌지 않게 되는데, 이를 해결하려면
네번째 칸의 값도 같이 증가시킨 다음, 그 값을 두번째 칸으로 옮기면 됩니다.
그러면 세번째 칸에 곱셈의 결과가 남습니다.
```

여기까지 브레인퍽이었습니다. 참 쉽죠?
재미삼아 브레인퍽 프로그램이나 다른 언어로 브레인퍽 인터프리터를 작성해보세요.
인터프리터 구현은 간단한 편인데,
사서 고생하는 것을 즐기는 편이라면 한 번 작성해보세요… 브레인퍽으로.
---
language: clojure
filename: learnclojure-kr.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["netpyoung", "http://netpyoung.github.io/"]
lang: ko-kr
---

Clojure는 Java 가상머신을 위해 개발된 Lisp 계통의 언어입니다
이는 Common Lisp보다 순수 [함수형 프로그래밍](https://en.wikipedia.org/wiki/Functional_programming)을 더욱 강조했으며, 
상태를 있는 그대로 다루기 위해 다양한 [STM](https://en.wikipedia.org/wiki/Software_transactional_memory) 을 지원하는 프로그램들을 갖췄습니다.

이를 조합하여, 병행처리(concurrent processing)를 매우 단순하게 처리할 수 있으며,
대게 자동으로 처리될 수 있도록 만들 수 있습니다.

(Clojure 1.2 이상의 버전이 필요로 합니다.)


```clojure
; 주석은 세미콜론(;)으로 시작합니다.

; Clojure는 "폼(forms)"으로 구성되었으며,
; 폼은 괄호로 감싸져있으며, 공백으로 구분된 것들이 나열된 것입니다.
;
; clojure의 reader는 첫번째로 오는 것을
; 함수 혹은 매크로를 호출하는 것, 그리고 나머지를 인자라고 가정합니다.

; namespace를 지정하기 위해, 파일에서 우선적으로 호출해야될 것은 ns입니다.
(ns learnclojure)

; 간단한 예제들:

; str 은 인자로 받은 것들을 하나의 문자열로 만들어줍니다.
(str "Hello" " " "World") ; => "Hello World"

; 직관적인 수학 함수들을 갖고 있습니다.
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; = 로 동일성을 판별할 수 있습니다.
(= 1 1) ; => true
(= 2 1) ; => false

; 논리연산을 위한 not 역시 필요합니다.
(not true) ; => false

; 중첩된 폼(forms)은 기대한대로 동작합니다.
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; 타입
;;;;;;;;;;;;;

; Clojure는 부울(boolean), 문자열, 숫자를 위해 Java의 object 타입을 이용합니다.
; `class` 를 이용하여 이를 확인할 수 있습니다.
(class 1) ; 정수는 기본적으로 java.lang.Long입니다.
(class 1.); 소수는 java.lang.Double입니다.
(class ""); 문자열은 쌍따옴표로 감싸져 있으며, java.lang.String입니다.
(class false) ; 부울값은 java.lang.Boolean입니다.
(class nil); nil은 "null"값입니다.

; 데이터 리스트 자체를 만들고자 한다면,
; '를 이용하여 평가(evaluate)되지 않도록 막아야 합니다.
'(+ 1 2) ; => (+ 1 2)
; (quote (+ 1 2)) 를 줄여서 쓴것

; quote 가 된 리스트를 평가할 수 도 있습니다.
(eval '(+ 1 2)) ; => 3

; 컬렉션(Collections) & 시퀀스(Sequences)
;;;;;;;;;;;;;;;;;;;

; 리스트(List)는 연결된(linked-list) 자료구조이며, 벡터(Vector)는 배열이 뒤로붙는(array-backed) 자료구조입니다.
; 리스트와 벡터 모두 java 클래스입니다!
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; 간단하게 (1 2 3)로 리스트를 나타낼 수 있지만,
; reader가 함수라고 여기지 못하게 quote(')를 해줘야 합니다.
; 따라서, (list 1 2 3)는 '(1 2 3)와 같습니다.

; "컬렉션"은 단순하게 데이터의 그룹입니다.
; 리스트와 벡터 모두 컬렉션입니다:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; "시퀀스" (seq) 는 데이터 리스트를 추상적으로 기술한 것입니다.
; 리스트는 시퀀스입니다.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; 시퀀스는 접근하고자 하는 항목만 제공해주면 됩니다.
; 따라서, 시퀀스는 lazy 할 수 있습니다 -- 무한하게 늘어나는 것을 정의할 수 있습니다:
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (an infinite series)
(take 4 (range)) ;  (0 1 2 3)

; cons 를 이용하여 리스트나 벡터의 시작부에 항목을 추가할 수 있습니다.
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; conj 는 컬렉션에 가장 효율적인 방식으로 항목을 추가합니다.
; 리스트는 시작부분에 삽입하고, 벡터는 끝부분에 삽입합니다.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; concat 을 이용하여 리스트와 벡터를 서로 합칠 수 있습니다.
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; filter, map 을 이용하여 컬렉션을 다룰 수 있습니다.
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; reduce 를 이용하여 줄여나갈 수 있습니다.
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; reduce 는 초기 값을 인자로 취할 수 도 있습니다.
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; 함수
;;;;;;;;;;;;;;;;;;;;;

; fn 을 이용하여 함수를 만들 수 있습니다 .
; 함수는 항상 마지막 문장을 반환합니다.
(fn [] "Hello World") ; => fn

; (정의한 것을 호출하기 위해선, 괄호가 더 필요합니다.)
((fn [] "Hello World")) ; => "Hello World"

; def 를 이용하여 var 를 만들 수 있습니다.
(def x 1)
x ; => 1

; var 에 함수를 할당시켜보겠습니다.
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; defn 을 이용하여 짧게 쓸 수 도 있습니다.
(defn hello-world [] "Hello World")

; [] 는 함수의 인자 목록을 나타냅니다.
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; 약자(shorthand)를 써서 함수를 만들 수 도 있습니다:
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; 함수가 다양한 인자를 받도록 정의할 수 도 있습니다.
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; 함수는 여러 인자를 시퀀스로 취할 수 있습니다.
(defn count-args [& args]
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"

; 개별적으로 받는 것과, 시퀀스로 취하는 것을 같이 쓸 수 도 있습니다.
(defn hello-count [name & args]
  (str "Hello " name ", you passed " (count args) " extra args"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"


; 맵(Maps)
;;;;;;;;;;

; 해쉬맵(hash map)과 배열맵(array map)은 공통된 인터페이스를 공유합니다.
; 해쉬맵은 찾기가 빠르지만, 키의 순서가 유지되지 않습니다.
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; 배열맵은 여러 연산을 거쳐 자연스레 해쉬맵이 됩니다.
; 만일 이게 커진다 하더라도, 걱정할 필요가 없습니다.

; 맵은 해쉬가 가능한 타입이라면 어떠한 것이든 키로써 활용이 가능하지만, 보통 키워드를 이용하는 것이 가장 좋습니다.
; 키워드(Keyword)는 문자열과 비슷하지만, 보다 효율적인 면이 있습니다.
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; 여기서, 쉽표가 공백으로 취급되며, 아무 일도 하지 않는다는 것을 주목하시기 바랍니다.

; 맵에서 값을 얻어오기 위해선, 함수로써 맵을 호출해야 합니다.
(stringmap "a") ; => 1
(keymap :a) ; => 1

; 키워드 역시 맵에서 함수를 얻어올 때 사용할 수 있습니다!
(:b keymap) ; => 2

; 하지만, 문자열로는 하면 안됩니다.
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; 없는 값을 얻어오고자 하면, nil이 반환됩니다.
(stringmap "d") ; => nil

; assoc 를 이용하여 해쉬맵에 새로운 키를 추가할 수 있습니다.
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; 하지만, 변경할 수 없는(immutable) clojure 타입이라는 것을 기억해야 합니다!
keymap ; => {:a 1, :b 2, :c 3}

; dissoc 를 이용하여 키를 제거할 수 있습니다.
(dissoc keymap :a :b) ; => {:c 3}

; 쎗(Set:집합)
;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; conj 로 항목을 추가할 수 있습니다.
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; disj 로 제거할 수 도 있습니다.
(disj #{1 2 3} 1) ; => #{2 3}

; 존재하는지 확인할 목적으로, 쎗을 함수로 사용할 수 도 있습니다.
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; clojure.sets 네임스페이스(namespace)에는 더 많은 함수들이 있습니다.

; 유용한 폼(forms)
;;;;;;;;;;;;;;;;;

; clojure에선, if 와 매크로(macro)를 가지고,
; 다른 여러 논리 연산들을 만들 수 있습니다.
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; let 을 이용하여 임시적으로 바인딩(binding)을 구축할 수 있습니다.
(let [a 1 b 2]
  (> a b)) ; => false

; do 로 문단을 묶을 수 도 있습니다.
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; 함수는 암시적으로 do 를 가지고 있습니다.
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; let 역시 그러합니다.
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")

; 모듈(Modules)
;;;;;;;;;;;;;;;

; "use" 를 이용하여 module에 있는 모든 함수들을 얻어올 수 있습니다.
(use 'clojure.set)

; 이제 쎗(set:집합)연산을 사용 할 수 있습니다.
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; 함수들 중에 일 부분만을 가져올 수 도 있습니다.
(use '[clojure.set :only [intersection]])

; require 를 이용하여 모듈을 import할 수 있습니다.
(require 'clojure.string)

; / 를 이용하여 모듈에 있는 함수를 호출 할 수 있습니다.
; 여기, clojure.string 라는 모듈에, blank? 라는 함수가 있습니다.
(clojure.string/blank? "") ; => true

; import시, 모듈에 짧은 이름을 붙여줄 수 있습니다.
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (#"" denotes a regular expression literal)

; :require 를 이용하여, 네임스페이스에서 require 를 사용할 수 있습니다.
; 아레와 같은 방법을 이용하면, 모듈을 quote하지 않아도 됩니다.
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java는 유용한 많은 표준 라이브러리를 가지고 있으며,
; 이를 어떻게 활용할 수 있는지 알아보도록 하겠습니다.

; import 로 java 모듈을 불러올 수 있습니다.
(import java.util.Date)

; ns 와 함께 import 를 할 수 도 있습니다.
(ns test
  (:import java.util.Date
           java.util.Calendar))

; 새로운 인스턴스를 만들기 위해선, 클래스 이름 끝에 "."을 찍습니다.
(Date.) ; <a date object>

; . 을 이용하여 메소드를 호출할 수 있습니다.
; 아니면, 줄여서 ".메소드"로도 호출 할 수 있습니다.
(. (Date.) getTime) ; <a timestamp>
(.getTime (Date.)) ; exactly the same thing.

; / 를 이용하여 정적메소드를 호출 할 수 있습니다.
(System/currentTimeMillis) ; <a timestamp> (system is always present)

; doto 를 이용하여 상태가 변하는(mutable) 클래스들을 좀 더 편하게(tolerable) 다룰 수 있습니다.
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => A Date. set to 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; Software Transactional Memory 는 clojure가 영구적인(persistent) 상태를 다루는 방식입니다.
; clojure가 이용하는 몇몇 자료형(construct)이 있습니다.

; 가장 단순한 것은 atom 입니다. 초기 값을 넣어보도록 하겠습니다.
(def my-atom (atom {}))

; swap! 으로 atom을 갱신(update)할 수 있습니다!
; swap! 은 함수를 인자로 받아, 그 함수에 대해 현재 atom에 들어있는 값을 첫번째 인자로,
; 나머지를 두번째 인자로 하여 호출합니다.
(swap! my-atom assoc :a 1) ; Sets my-atom to the result of (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Sets my-atom to the result of (assoc {:a 1} :b 2)

; '@' 를 이용하여 atom을 역참조(dereference)하여 값을 얻을 수 있습니다.
my-atom  ;=> Atom<#...> (atom 객체가 반환됩니다.)
@my-atom ; => {:a 1 :b 2}

; 여기 atom을 이용한 단순한 카운터가 있습니다.
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; STM을 구성하는 다른 것들에는 ref 와 agent 가 있습니다.
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
```

### 읽어볼거리

부족한 것이 많았지만, 다행히도 채울 수 있는 것들이 많이 있습니다.

Clojure.org에 많은 문서들이 보관되어 있습니다:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org는 core 함수들에 대해 다양한 예제와 문서를 보유하고 있습니다:
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure는 clojure/FP 스킬을 올릴 수 있는 좋은 길입니다:
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org는 많고 많은 문서들을 보유하고 있습니다:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: coffeescript
category: language
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
filename: coffeescript-kr.coffee
translators:
    - ["wikibook", "http://wikibook.co.kr"]
lang: ko-kr
---

``` coffeescript
# 커피스크립트(CoffeeScript)는 최신 유행을 따르는 언어입니다.
# 커피스크립트는 여러 현대 언어의 트렌드를 따르는데,
# 그래서 주석을 작성할 때는 루비나 파이썬과 같이 해시를 씁니다.

###
블록 주석은 이처럼 작성하며, 자바스크립트 코드로 만들어지도록
'/ *'와 '* /'로 직접적으로 변환됩니다.

계속하기에 앞서 자바스크립트 시맨틱을 대부분 이해하고 있어야 합니다.
###

# 할당:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# 조건문:
number = -42 if opposite #=> if(opposite) { number = -42; }

# 함수:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

# 범위:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# 객체:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#  "root": Math.sqrt,
#  "square": square,
#  "cube": function(x) { return x * square(x); }
#}

# 가변 인자(splat):
race = (winner, runners...) ->
  print winner, runners

# 존재 여부 확인:
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# 배열 조건 제시법(comprehensions):
cubes = (math.cube num for num in list) #=> ...
```
---
language: erlang
contributors:
    - ["Giovanni Cappellotto", "http://www.focustheweb.com/"]
filename: learnerlang-kr.erl
translators:
    - ["Taesung Jung", "https://github.com/tsj"]
lang: ko-kr
---

```erlang
% 퍼센트 기호는 한 줄 주석을 시작한다.

%% 두 개의 퍼센트 문자는 함수의 주석에 사용된다.

%%% 세 개의 퍼센트 문자는 모듈의 주석에 사용된다.

% Erlang에선 3가지 유형의 문장 부호를 사용한다.
% 쉼표(`,`)는 함수 호출에서 인수, 데이터 생성자(constructors), 패턴을 구분한다.
% 마침표(`.`)(다음에 오는 공백)는 셸에서 함수 전체와 식을 구분한다.
% 세미콜론(`;`)은 절을 구분한다. 몇 가지 문맥(contexts)에서 절이 발견된다:
% 함수 정의와 `case`, `if`, `try..catch`, 그리고 `receive` 식


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. 변수와 패턴 매칭
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Erlang에서 새로운 변수는 `=` 문장에 의해 바인딩 된다.
Num = 42.  % 모든 변수 이름은 반드시 대문자로 시작해야 한다.

% Erlang은 단일 할당 변수(single-assignment variables)를 가진다;
% 만약 다른 값을 `Num` 변수에 할당하려고 시도하면 오류가 발생한다.
Num = 43. % ** 예외 오류: 우변의 값 43과 매칭되지 않음

% 대부분 언어에서 `=`는 할당문을 나타낸다. 그러나 Erlang에서 
% `=`는 패턴 매칭 연산자를 나타낸다. 비어 있는 변수가 `=` 연산자의 좌변에
% 사용되면 바인드(할당) 된다, 그러나 바인드 변수가 좌변에 사용된 경우에
% 다음 행동은 그 바인드 변수가 관측된다.
% `Lhs = Rhs`의 진짜 의미: 우변(`Rhs`)을 평가하고, 그리고
% 그 결과를 좌변(`Lhs`)의 패턴과 매치시켜라.
Num = 7 * 6.

% 부동 소수점 수.
Pi = 3.14159.

% Atom은 숫자가 아닌 서로 다른 상숫값을 표현하는 데 사용한다. Atom은
% 소문자로 시작하고, 연속적인 영숫자(alphanumeric) 문자나 밑줄(`_`) 또는
% 골뱅이(`@`) 기호가 따라온다.
Hello = hello.
OtherNode = example@node.

% 영숫자 값이 아닌 Atom은 작은따옴표로 묶여서 작성될 수 있다.
AtomWithSpace = 'some atom with space'.

% Tuple은 C의 struct와 비슷하다.
Point = {point, 10, 45}.

% Tuple에서 어떤 값을 추출하려면, 패턴 매칭 연산자 `=`를 사용한다.
{point, X, Y} = Point.  % X = 10, Y = 45

% 관심 없는 변수를 위해 자리 표시자(placeholder) `_`를 사용할 수 있다.
% 기호 `_`는 익명 변수(anonymous variable)라 부른다. 일반적인 변수들과
% 다르게 같은 패턴에서 여러 번 나오더라도 동일한 값으로 바인드되지 않아도 된다.
Person = {person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}.
{_, {_, {_, Who}, _}, _} = Person.  % Who = joe

% List를 만들기 위해서 List의 원소는 대괄호([])로 둘러싸고 쉼표(,)로 구분한다.
% List의 각각의 원소는 어떤 타입도 가능하다.
% List의 첫 번째 원소는 List의 HEAD이다. 만약 List의 HEAD를 제거하면,
% 남은 부분은 List의 TAIL이라 부른다.
ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].

% 만약 `T`가 List이면, `[H|T]`도 HEAD가 `H`이고 TAIL이 `T`인 List이다.
% 세로 막대(`|`)는 List의 HEAD와 TAIL을 분리한다. `[]`는 빈 List다.
% List의 원소들은 패턴 매칭 연산으로 추출할 수 있다.
% 만약 비어있지 않은 List `L`이 있을 때, `[X|Y] = L` 식의 `X`와 `Y`가
% 바인드되지 않은 변수이면, List의 HEAD는 X에 그리고 TAIL은 Y로 추출된다.
[FirstThing|OtherThingsToBuy] = ThingsToBuy.
% FirstThing = {apples, 10}
% OtherThingsToBuy = [{pears, 6}, {milk, 3}]

% Erlang에는 문자열(String)이 없다. 문자열은 사실 정수의 List일 뿐이다.
% 문자열은 큰따옴표(`"`)로 묶인다.
Name = "Hello".
[72, 101, 108, 108, 111] = "Hello".


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. 순차 프로그래밍
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Erlang에서 Module은 코드의 기본 단위이다. 우리가 작성한 모든 함수는
% Module에 담긴다. Module은 확장자가 `.erl`인 파일에 저장된다.
% 코드가 실행되기 전에 Module은 컴파일되어야 한다. 컴파일된 Module은
% `.beam` 확장자를 가진다.
-module(geometry).
-export([area/1]). % Module로부터 내보내진(exported) 함수의 List

% 함수 `area`는 두 개의 절로 구성된다. 절은 세미콜론(`;`)으로 구분되며,
% 마지막 절은 마침표-공백(dot-whitespace)으로 끝난다.
% 각 절은 서문(head)과 본문(body)을 가진다. 서문은 함수의 이름에 이어서
% 패턴이(괄호 속에) 따라온다. 본문은 연속적인 식으로 구성되고,
% 연속적인 식은 서문의 패턴과 호출한 인수가 성공적으로 매치되면 평가된다.
% 패턴은 함수 정의가 나타나는 순서대로 매치된다.
area({rectangle, Width, Ht}) -> Width * Ht;
area({circle, R})            -> 3.14159 * R * R.

% geometry.erl 파일의 코드 컴파일
c(geometry).  % {ok,geometry}

% 호출하려는 함수를 정확히 알아내기 위해 함수 이름을 Module 이름과 함께
% 명시하는 것이 필요하다.
geometry:area({rectangle, 10, 5}).  % 50
geometry:area({circle, 1.4}).  % 6.15752

% Erlang에서, 같은 Module에 이름이 같고 Arity(인수의 갯수)가 다른
% 두 함수는 전혀 다른 함수를 나타낸다.
-module(lib_misc).
-export([sum/1]). % Arity가 1인 내보내진(export) 함수 `sum`
                  % 하나의 인수만 받음: 정수의 List
sum(L) -> sum(L, 0).
sum([], N)    -> N;
sum([H|T], N) -> sum(T, H+N).

% Fun은 "익명(anonymous)" 함수다. 이름이 없어서 이렇게 부른다.
% 그러나, 변수에 할당될 수 있다.
Double = fun(X) -> 2 * X end. % `Double`은 익명 함수를 가리킨다:
                              % #Fun<erl_eval.6.17052888>
Double(2).  % 4

% 함수는 인수로 Fun을 받거나, Fun을 반환할 수 있다.
Mult = fun(Times) -> ( fun(X) -> X * Times end ) end.
Triple = Mult(3).
Triple(5).  % 15

% List 해석(List comprehensions)은 Fun, Map, Filter 없이 List를 만드는 식이다.
% 표기법 `[F(X) || X <- L]`은 `F(X)`의 List라는 의미이다.
% 이때 `X`는 List `L`로부터 가져온다.
L = [1,2,3,4,5].
[2 * X || X <- L].  % [2,4,6,8,10]
% List 해석은 Generator와 생성된 값들의 부분 집합을 선택하는 Filter를 가질 수 있다.
EvenNumbers = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4]

% Guard는 패턴 매칭의 능력을 향상시키는데 사용할 수 있는 구조다.
% Guard를 사용하면, 패턴에 있는 변수에 대해 간단한 검사와 비교를 수행할 수 있다.
% 함수 정의의 서문(head)에 `when` 키워드로 시작되는 Guard를 사용할 수도 있고,
% 또는 식이 허용되는 언어의 어떤 곳에도 사용될 수 있다.
max(X, Y) when X > Y -> X;
max(X, Y) -> Y.

% Guard는 쉼표(`,`)로 구분된 연속된 Guard 식이다.
% 모든 Guard 식 `GuardExpr1`, `GuardExpr2`, ..., `GuardExprN`이
% `true`로 평가된다면, Guard `GuardExpr1`, `GuardExpr2`, ..., `GuardExprN`는
% 참이다.
is_cat(A) when is_atom(A), A =:= cat -> true;
is_cat(A) -> false.
is_dog(A) when is_atom(A), A =:= dog -> true;
is_dog(A) -> false.

% `=:=` 연산자는 여기서 자세히 다루지 않을 것이다; 두 개의 Erlang 식의 값이 같고
% *그리고* 같은 타입인지 검사하는 데 사용된다고만 알면 된다.
% `==` 연산자의 작동과 대조할 것:
1 + 2 =:= 3.   % true
1 + 2 =:= 3.0. % false
1 + 2 ==  3.0. % true

% 연속적인 Guard는 단일 Guard 또는 세미콜론(`;`)으로 구분된 연속된 Guard다.
% Guard `G1; G2; ...; Gn` 중에 적어도 하나의 Guard가 `true`로 평가된다면,
% 연속적인 Guard `G1; G2; ...; Gn`는 참이다.
is_pet(A) when is_atom(A), (A =:= dog);(A =:= cat) -> true;
is_pet(A)                                          -> false.

% 주의: 모든 유효한 Erlang 식이 Guard 식으로 사용될 수 있는 것은 아니다;
% 특히, 함수 `is_cat`과 `is_dog`는 `is_pet`의 정의 안에 있는 
% 연속적인 Guard 사이에 사용될 수 없다.
% 연속적인 Guard에 허용되는 식의 자세한 설명은 Erlang 레퍼런스 메뉴얼
% [section](http://erlang.org/doc/reference_manual/expressions.html#id81912)
% 을 참조하라.

% Record는 Tuple 안에 이름과 특정 요소를 연결하는 방법을 제공한다.
% Record 정의는 Erlang 소스 코드 파일에 포함되거나 Erlang 소스 코드 파일에
% 포함될 수 있는 확장자가 `.hrl`인 파일에 집어넣을 수 있다.
-record(todo, {
  status = reminder,  % 기본 값
  who = joe,
  text
}).

% Record를 사용할 수 있기 전에 Record 정의를 반드시 셸로 읽어 들여야 한다.
% 셸로 읽어 들이기 위해 셸 함수 `rr`(read records의 약자)을 사용한다.
rr("records.hrl").  % [todo]

% Record 생성과 수정
X = #todo{}.
% #todo{status = reminder, who = joe, text = undefined}
X1 = #todo{status = urgent, text = "Fix errata in book"}.
% #todo{status = urgent, who = joe, text = "Fix errata in book"}
X2 = X1#todo{status = done}.
% #todo{status = done, who = joe, text = "Fix errata in book"}

% `case` 식
% `filter`는 List `L`의 원소 `X` 중에서 `P(X)`가 참인 모든 `X`의 List를 반환한다.
filter(P, [H|T]) ->
  case P(H) of
    true -> [H|filter(P, T)];
    false -> filter(P, T)
  end;
filter(P, []) -> [].
filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]). % [2, 4]

% `if` 식.
max(X, Y) ->
  if
    X > Y -> X;
    X < Y -> Y;
    true -> nil
  end.

% 주의: 적어도 if 식의 Guard 중의 하나는 반드시 `true`로 평가되어야 한다. 
% 그렇지 않으면 예외가 발생한다.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3. 예외
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 예외는 내부에 에러가 생겼거나 명시적으로 `throw(Exception)`,
% `exit(Exception)` 또는 `erlang:error(Exception)`를 호출하면
% 시스템에 의해 발생한다.
generate_exception(1) -> a;
generate_exception(2) -> throw(a);
generate_exception(3) -> exit(a);
generate_exception(4) -> {'EXIT', a};
generate_exception(5) -> erlang:error(a).

% Erlang은 예외를 잡는 두 가지 방법을 가지고 있다. 한 가지는
% 예외를 발생시키는 함수의 호출 부분을 `try...catch` 식으로 감싸는 것이다.
catcher(N) ->
  try generate_exception(N) of
    Val -> {N, normal, Val}
  catch
    throw:X -> {N, caught, thrown, X};
    exit:X -> {N, caught, exited, X};
    error:X -> {N, caught, error, X}
  end.

% 다른 방법은 그 호출 부분을 `catch` 식으로 감싸는 것이다.
% 예외를 잡았을 때, 그 예외는 오류를 설명하는 Tuple로 변환된다.
catcher(N) -> catch generate_exception(N).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 4. 병행성
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Erlang은 병행성을 위해 Actor 모델을 사용한다. Erlang에서 병행 프로그램을
% 작성하는 데 필요한 모든 것은 3가지 기본 형식(primitivies)이다:
% 프로세스 생성, 메시지 보내기, 메시지 받기

% 새로운 프로세스를 시작하기 위해, 함수를 인수로 받는 `spawn` 함수를 사용한다.

F = fun() -> 2 + 2 end. % #Fun<erl_eval.20.67289768>
spawn(F). % <0.44.0>

% `spawn`은 pid(프로세스 식별자)를 반환한다. 이 pid를 프로세스로
% 메시지를 보내는 데 사용할 수 있다. 메시지 전달을 위해, `!` 연산자를 사용한다.
% 위의 기능이 유용하려면, 메시지를 받을 수 있어야 한다. 메시지를 받는 것은
% `receive` 메커니즘을 사용한다.

-module(calculateGeometry).
-compile(export_all).
calculateArea() ->
    receive
      {rectangle, W, H} ->
        W * H;
      {circle, R} ->
        3.14 * R * R;
      _ ->
        io:format("We can only calculate area of rectangles or circles.")
    end.

% Module을 컴파일하고 셸에서 `calculateArea`를 평가한 프로세스를 생성한다.
c(calculateGeometry).
CalculateArea = spawn(calculateGeometry, calculateArea, []).
CalculateArea ! {circle, 2}. % 12.56000000000000049738

% 셸도 마찬가지로 프로세스이다. 현재 pid를 얻기 위해서 `self`를 사용할 수 있다.
self(). % <0.41.0>

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 5. EUnit과 테스트
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% EUnit의 테스트 생성기(generators)와 assert 매크로를 이용해
% 단위 테스트를 작성할 수 있다.
-module(fib).
-export([fib/1]).
-include_lib("eunit/include/eunit.hrl").

fib(0) -> 1;
fib(1) -> 1;
fib(N) when N > 1 -> fib(N-1) + fib(N-2).

fib_test_() ->
    [?_assert(fib(0) =:= 1),
     ?_assert(fib(1) =:= 1),
     ?_assert(fib(2) =:= 2),
     ?_assert(fib(3) =:= 3),
     ?_assert(fib(4) =:= 5),
     ?_assert(fib(5) =:= 8),
     ?_assertException(error, function_clause, fib(-1)),
     ?_assert(fib(31) =:= 2178309)
    ].

% EUnit은 Erlang 셸에서 테스트를 실행할 수 있게 
% 자동으로 test() 함수를 내보낸다(export).
fib:test()

% Erlang의 유명한 빌드 툴인 Rebar는 EUnit과 호환된다.
% ```
% rebar eunit
% ```

```

## 참조

* ["Learn You Some Erlang for great good!"](http://learnyousomeerlang.com/)
* ["Programming Erlang: Software for a Concurrent World" by Joe Armstrong](http://pragprog.com/book/jaerlang/programming-erlang)
* [조 암스트롱, 김석준 역, "프로그래밍 얼랭: Software for a Concurrent World", 인사이트](http://ebook.insightbook.co.kr/book/23)
* [Erlang/OTP Reference Documentation](http://www.erlang.org/doc/)
* [Erlang - Programming Rules and Conventions](http://www.erlang.se/doc/programming_rules.shtml)
---
name: Go
category: language
language: Go
filename: learngo-kr.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
translators:
    - ["Jongmin Kim", "http://github.com/atomaths"]
    - ["Peter Lee", "http://github.com/ins429"]
lang: ko-kr
---

Go는 어떤 일을 잘 끝낼 수 있도록 하기위해 만들어졌다. Go가 잘 알려진 최신의
트렌드는 아니지만, 실세계의 문제들을 해결하기 위해서는 가장
새롭고 빠른 방법이다.

Go는 정적 타이핑(static typing)의 명령형 언어들(imperative languages)이
갖고 있는 특징과 유사한 개념들을 가지고 있다. Go는 컴파일과 실행속도가
빠르며, 오늘날의 멀티코어 CPU를 위해 이해하기 쉬운 동시성(concurrency)
기능이 추가되었다. 그리고 큰 스케일의 프로그래밍에도 도움이 되는
기능들을 가지고 있다.

또한 Go에는 훌륭한 표준 라이브러리와 열정적인 커뮤니티가 있다.

```go
// 한 줄 주석
/* 여러 줄
   주석 */

// 모든 Go 소스 파일은 package로 시작한다.
// 패키지 이름 중 main은 라이브러리가 아닌 실행파일을 선언하는 특별한 이름이다.
package main

// import는 이 Go 소스 파일 내에서 참조하는 라이브러리 패키지들을 선언한다.
import (
    "fmt"      // Go 표준 라이브러리에 있는 패키지
    "net/http" // 표준 라이브러리에는 웹 서버 패키지도 있다! (클라이언트도 있음)
    "strconv"  // 문자열 변환 패키지
)

// 함수 선언. main은 실행 프로그램에서 시작점이 되는 특별한 함수다.
// 중괄호를 사용한다.
func main() {
    // Println은 표준 출력으로 개행을 출력한다.
    // fmt 패키지를 통해 이용할 수 있다.
    fmt.Println("Hello world!")

    // 다른 함수를 호출한다.
    beyondHello()
}

// 함수에 파라미터가 없더라도 빈 괄호는 있어야 한다.
func beyondHello() {
    var x int // 변수 선언. 변수는 사용하기 전에 선언해야 한다.
    x = 3     // 변수에 값 할당.
    // 짧은 선언(short declaration)으로 := 를 사용하는데,
    // 이렇게 값을 할당하면 값의 타입에 따라 변수의 타입이 결정된다.
    y := 4
    sum, prod := learnMultiple(x, y)        // 함수는 두 개 이상의 리턴 값을 줄 수 있다.
    fmt.Println("sum:", sum, "prod:", prod) // 간단한 출력
    learnTypes()                            // 잠시 후에 좀더 자세히!
}

// 함수는 파라미터들을 가질 수 있고, 복수개의 값을 리턴할 수 있다.
func learnMultiple(x, y int) (sum, prod int) {
    return x + y, x * y // 두 개의 값을 리턴.
}

// 내장 타입과 리터럴
func learnTypes() {
    // 짧은 선언은 유용하다.
    s := "Learn Go!" // string 타입

    s2 := `역따옴표 안의 string 리터럴은
개행을 포함할 수 있다.` // 같은 string 타입

    // non-ASCII 리터럴. Go 소스는 UTF-8로 작성해야 한다.
    g := 'Σ' // 유니코드 코드 포인트를 담고 있고, int32 타입의 가칭(alias)인 rune 타입

    f := 3.14195 // float64, an IEEE-754 64-bit 부동소수 타입
    c := 3 + 4i  // complex128, 내부적으로는 두 개의 float64 타입으로 표현됨

    // 초기값과 함께 사용하는 var 키워드.
    var u uint = 7 // unsigned, 하지만 int에 따른 구현의존적인 크기
    var pi float32 = 22. / 7

    // 짧은 선언으로 변환(conversion)하는 문법.
    // Go에서는 type casting 이라고 하지않고 type conversion 이라고 함.
    n := byte('\n') // byte는 uint8의 가칭(alias)

    // 배열은 컴파일 시에 크기가 정해진다.
    var a4 [4]int           // 모두 0으로 초기화되는 int 타입 4개짜리 배열
    a3 := [...]int{3, 1, 5} // 3, 1, 5로 초기화되는 int 타입 3개짜리 배열

    // 슬라이스(slice)라고 하는 타입은 배열에 대한 가변 크기를 가진다.
    // 배열, 슬라이스 각자 장점이 있지만, 슬라이스가 더 많이 사용된다.
    s3 := []int{4, 5, 9}    // 위의 a3와 비교해보면 생략부호(...)가 없다.
    s4 := make([]int, 4)    // 모두 0으로 초기화되는 int 4개에 대한 슬라이스를 할당.
    var d2 [][]float64      // 여기에서는 선언만 있고 할당은 없다.
    bs := []byte("a slice") // string 타입을 byte 슬라이스 타입으로 형변환(type conversion)

    p, q := learnMemory() // int에 대한 포인터 타입인 p와 q를 선언
    fmt.Println(*p, *q)   // C에서처럼 *는 포인터를 따라가 값을 참조한다. 여기서는 두 개의 int를 출력.

    // 맵(map)은 다른 언어의 해시(hash)나 딕셔너리(dictionary)처럼 가변의 연관배열 타입.
    m := map[string]int{"three": 3, "four": 4}
    m["one"] = 1

    // 선언만 하고 사용하지 않는 변수가 있다면 Go에서는 컴파일 시 에러가 난다.
    // 언더바를 이용해서 변수를 사용한 것처럼 하고 그 값은 무시해버릴 수 있다.
    _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
    // 물론 출력을 하면 변수로 취급한다.
    fmt.Println(s, c, a4, s3, d2, m)

    learnFlowControl() // 잠시 후에 다시 나옴
}

// Go는 가비지 컬렉션 기능을 JVM 같은 곳이 아닌 실행파일 런타임에 포함하고 있다.
// 그리고 포인터는 있지만, 포인터 연산(*p++ 같은)은 없다.
// 그래서 nil 포인터 접근같은 것 때문에 실수를 할 수는 있지만
// 포인터 연산으로 인한 실수는 없게 된다.
func learnMemory() (p, q *int) {
    // 지명된 리턴 값(named return value)인 p와 q는 int에 대한 포인터 타입이다.
    p = new(int) // 내장함수인 new는 메모리를 할당해준다.
    // 메모리 할당된 int는 0으로 초기화 되고, p는 이제 nil이 아니다.
    s := make([]int, 20) // 메모리의 단일 블록으로 20개의 int 공간을 할당한다.
    s[3] = 7             // 그중 하나에 값을 준다.
    r := -2              // 또다른 로컬 변수를 선언한다.
    return &s[3], &r     // &는 어떤 대상체의 메모리 주소를 가져오게 된다.
}

func expensiveComputation() int {
    return 1e6
}

func learnFlowControl() {
    // if문에 중괄호는 필요하지만, 조건이 들어갈 곳에 소괄호는 쓰지 않는다.
    if true {
        fmt.Println("told ya")
    }
    // 모든 Go 소스의 코드 포맷팅은 "go fmt" 커맨드라인 명령으로 소스코드의 포맷을 맞춘다.
    if false {
        // pout
    } else {
        // gloat
    }
    // if-else 체인 형태보다 switch 사용이 권장된다.
    x := 1
    switch x {
    case 0:
    case 1:
        // case 안에서는 break가 없어도 자동으로 다음 case로 내려가지 않는다.
        // 자동으로 내려가게 하려면 fallthrough 키워드를 사용한다.
    case 2:
        // x는 1이므로 여기는 실행되지 않음.
    }
    // if 에서처럼 for 에서도 양쪽에 소괄호를 쓰지 않는다.
    for x := 0; x < 3; x++ { // ++ 은 실행을 제어하는 하나의 구문(statement)이다.
        fmt.Println("iteration", x)
    }
    // 여기서 x는 1이다. 위 for에서 x는 for 안의 블록 범위에 있기 때문.

    // For is the only loop statement in Go, but it has alternate forms.
    // for 는 Go에서 유일한 루프 구문이지만 다양한 형태로 조건을 주거나 while 
    // 처럼 쓸 수도 있다.
    for { // 무한루프
        break    // 여기서 곧바로 break를 한 건 단지
        continue // break, continue를 루프 안에서 쓸 수 있다는 것을 보여주기 위함.
    }
    // for 에서처럼 if 에서 := 를 사용하는것은 y에 먼저 값을 대입하고,
    // 그리고 y > x를 검사한다는 의미.
    if y := expensiveComputation(); y > x {
        x = y
    }
    // 함수 리터럴은 클로저다.
    xBig := func() bool {
        return x > 100 // 위 switch 문 바로 위에 있는 x를 참조한다.
    }
    fmt.Println("xBig:", xBig()) // true (x에 1e6를 대입했었다.)
    x /= 1e5 // x는 10이 된다.
    fmt.Println("xBig:", xBig()) // 이제 xBig()의 결과는 false가 된다.

    // `goto`가 필요하다면, 좋아하게 될지도...
    goto love
love:

    learnDefer()      // defer에 대해
    learnInterfaces() // 곧이어서 좋은 기능에 대한 설명이 나올 거다.
}

func learnDefer() (ok bool) {
    // deferred statements are executed just before the function returns.
    // 연기된(deferred) 구문은 함수가 리턴하기 직전에 실행된다.
    defer fmt.Println("deferred statements execute in reverse (LIFO) order.") // 연기된 구문은 LIFO순으로 실행된다.
    defer fmt.Println("\nThis line is being printed first because")           // 이 줄이 먼저 실행된다.
    // defer는 주로 파일을 닫는데 사용된다. 
    // 파일을 닫는함수를 파일을 여는함수에 가까이 둘수 있다.
    return true
}

// String 이라는 메서드 하나를 가진 Stringer 라는 인터페이스 타입을 정의하자.
type Stringer interface {
    String() string
}

// x와 y라는 이름의 int 타입 필드를 가진 pair라는 struct를 정의하자.
type pair struct {
    x, y int
}

// pair 타입에 메서드 String을 정의하자.
// 이제 pair는 Stringer 인터페이스를 구현(implement)한 것이 되었다.
func (p pair) String() string { // 여기서 p는 리시버(receiver)라고 부른다.
    // Sprintf는 fmt 패키지 안에 있는 외부로 공개된(exported) 함수다.
    // 점(.)으로 p의 필드들을 참조할 수 있다.
    return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
    // 중괄호 문법은 "구조체 리터럴(struct literal)"인데, 초기화된 구조체로
    // 취급하게 해준다. := 문법으로 p를 이 구조체로 선언하고 초기화한다.
    p := pair{3, 4}
    fmt.Println(p.String()) // 타입 pair인 p의 String 메서드를 호출.
    var i Stringer          // Stringer 인터페이스 타입 i를 선언.
    i = p                   // pair는 Stringer를 구현했기 때문에 이 대입은 유효하다.
    // 타입 Stringer인 i의 String 메서드 호출. 결과는 위와 같다.
    fmt.Println(i.String())

    // fmt 패키지의 함수들을 통해 어떤 객체를 출력해보려고 할 때,
    // fmt 패키지 내에서는 그 객체가 가진 String 메서드를 호출하도록 되어 있다.
    fmt.Println(p) // 결과는 위와 같다. Println은 String 메서드를 호출한다.
    fmt.Println(i) // 결과는 위와 같다.

    learnVariadicParams("great", "learning", "here!")
}

// 함수는 가변 인수(variadic) 파라미터를 가질수 있다.
func learnVariadicParams(myStrings ...interface{}) {
    // 가변 인수를 차례로 반복한다.
    // 여기서 언더바(언더스코어, `_`)는 배열의 인덱스 인수를 무시한다.
    // The underbar here is ignoring the index argument of the array.
    for _, param := range myStrings {
          fmt.Println("param:", param)
    }

    // 가변 인수 값을 가변인수 파라미터로 보내기.
    fmt.Println("params:", fmt.Sprintln(myStrings...))

    learnErrorHandling()
}

func learnErrorHandling() {
    // ", ok" (comma okay)표현은 무언가가 맞는 것인지 아닌지 확인하는데 사용된다.
    m := map[int]string{3: "three", 4: "four"}
    if x, ok := m[1]; !ok { // 이 map 안에 키가 1인 것은 없으므로 ok는 false가 된다.
        fmt.Println("no one there")
    } else {
        fmt.Print(x) // 만일 1이 map에 있었다면 x는 키 1의 값이 들어가게 된다.
    }

    // Go에서는 함수가 복수 개의 리턴 값을 줄 수 있다는 점을 활용해 함수의 두 번째 리턴
    // 값으로 error를 리턴해주고 그 error가 nil 인지 아닌지 확인하는 관례가 있다.
    // 이때 이 error 값은 단지 위에서처럼 함수의 결과가 성공했는지 실패했는지를 확인하는
    // 것뿐만 아니라 실패 시 어떤 문제가 있었는지 확인할 수 있는 수단도 된다.
    if _, err := strconv.Atoi("non-int"); err != nil { // _ 는 값을 안 쓰고 버린다는 의미.
        // "strconv.ParseInt: parsing "non-int": invalid syntax" 이런 에러가 출력된다.
        fmt.Println(err)
    }
    // 인터페이스에 대해 잠시 후에 다시 잠깐 볼 것이다.
    learnConcurrency()
}

// c는 goroutine 간의 통신을 위한 채널(channel)이다.
func inc(i int, c chan int) {
    c <- i + 1 // 채널이 <- 이 연산자 왼쪽에 온다면 그 채널로 데이터를 보낸다는 의미다.
}

// 우리는 어떤 숫자들을 동시에 증가시키기 위해 inc 함수를 사용할 것이다.
func learnConcurrency() {
    // make는 slice, map, channel 타입들에 대해 메모리를 할당하고 초기화를 한다.
    // Go에는 메모리 할당 방법으로 new와 make가 있다.
    c := make(chan int)
    // 3개의 동시에 실행되는 goroutine를 시작한다. 만약 실행하고 있는 머신이
    // 멀티코어 CPU를 가지고 있고 올바르게 설정되어(GOMAXPROCS) 있다면
    // 숫자가 정말로 병렬적으로 증가하게 될 것이다.
    go inc(0, c) // go는 새로운 goroutine을 시작하는 구문이다.
    go inc(10, c)
    go inc(-805, c)
    // 채널로부터 3개의 결과를 읽어 출력한다.
    // 결과가 어떤 순서로 오는지는 알 수 없다.
    fmt.Println(<-c, <-c, <-c) // 채널이 <- 연산자 오른쪽에 있는 건, 채널로부터 데이터를 받는 연산이다.

    cs := make(chan string)       // string을 다루는 또 다른 채널
    cc := make(chan chan string)  // string 채널의 채널
    go func() { c <- 84 }()       // c 채널로 값을 보내는 goroutine 시작.
    go func() { cs <- "wordy" }() // cs 채널로 값을 보내느 goroutine 시작.
    // select 구문은 switch 문과 비슷하지만, case에서 채널 연산에 관한 일을 한다.
    // select의 case들은 채널통신을 할 준비가 된 case 하나가 무작위로 선택되어 
    // 그 부분이 실행된다.
    select {
    case i := <-c: // 채널로부터 받아진 값은 변수에 대입할 수 있다.
        fmt.Printf("it's a %T", i)
    case <-cs: // 또는 받은 값을 그냥 버릴 수도 있다.
        fmt.Println("it's a string")
    case <-cc: // 통신할 준비가 되어 있지 않은 비어있는 채널.
        fmt.Println("didn't happen.")
    }
    // 여기서는 c나 cs 채널로부터 값 하나를 받을 수 있다. 위에서 실행한 두 개의
    // goroutine 중 하나가 완료되면 다른 하나는 블락된 상태로 있게 된다.

    learnWebProgramming() // Go에서는 웹 서버쪽 개발도 쉽게 할 수 있다.
}

// http 패키지의 함수 하나로 웹 서버를 실행시킨다.
func learnWebProgramming() {
    // ListenAndServe의 첫 번째 파라미터는 listen 하기 위한 TCP 주소고,
    // 두 번째 파라미터는 http.Handler 인터페이스다.
    err := http.ListenAndServe(":8080", pair{})
    fmt.Println(err) // don't ignore errors
}

// http.Handler의 하나 뿐인 메서드, ServeHTTP를 pair에서 구현한다.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // http.ResponseWriter의 메서드로 클라이언트에게 데이터를 보낸다.
    w.Write([]byte("You learned Go in Y minutes!"))
}
```

## 더 읽어볼 것들

Go에 대한 모든 것들은 [Go 공식 웹 사이트](http://golang.org/)를 참고하자.
여기에는 따라해볼 튜토리얼, 웹 기반의 인터랙티브 실행환경과 많은 읽을거리들이 있다.

Go 언어 자체에 대한 스펙도 읽어보기를 적극 추천한다. 읽기 쉽게 되어있고
그리 길지는 않다.

Go 소스코드에 대해 좀더 알아보고 싶다면 [Go 표준 라이브러리](http://golang.org/src/pkg/)를
분석해보기 바란다. 이해하기 쉽게 문서화되어 있고, Go 스타일 그리고 Go에서의
관례 배우기에 가장 좋은 방법일 것이다. 또는 [문서](http://golang.org/pkg/) 안에서
함수 이름 하나를 클릭해보면 소스코드를 브라우저에서 살펴볼 수도 있다.

Go를 배울수 있는 또하나의 좋은 방법은 [Go by example](https://gobyexample.com/).
---
language: java
filename: java-kr.java
category: language
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
    - ["wikibook", "http://wikibook.co.kr"]
lang: ko-kr
---

자바는 일반 목적으로 사용할 수 있고 동시성을 지원하며, 클래스 기반의 객체지향 컴퓨터 프로그래밍 언어입니다.
[더 자세한 사항](http://docs.oracle.com/javase/tutorial/java/index.html)

```java
// 한 줄짜리 주석은 //로 시작합니다.
/*
여러 줄 주석은 다음과 같은 형태입니다.
*/
/**
자바독(JavaDoc) 주석은 이렇게 생겼습니다. 자바독 주석은 클래스나 클래스의
다양한 속성을 기술하는 데 사용됩니다.
*/

// java.util 패키지 안에 있는 ArrayList 클래스를 임포트합니다.
import java.util.ArrayList;
// java.security 패키지 안에 있는 모든 클래스를 임포트합니다.
import java.security.*;

// 각 .java 파일에는 공용(public) 클래스가 들어 있으며, 클래스의 이름은
// 파일명과 동일합니다.
public class LearnJava {

    // 프로그램에는 반드시 진입점 역할을 하는 main 메서드가 하나 있어야 합니다.
    public static void main (String[] args) {

        // System.out.println을 이용해 한 줄을 출력합니다.
        System.out.println("Hello World!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // 줄바꿈 없이 뭔가를 출력하려면 System.out.print를 사용합니다.
        System.out.print("Hello ");
        System.out.print("World");


        ///////////////////////////////////////
        // 타입 & 변수
        ///////////////////////////////////////

        // <타입> <이름>과 같은 형태로 변수를 선언합니다.
        // Byte - 부호가 있는 8비트 2의 보수 정수
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short - 부호가 있는 16비트 2의 보수 정수
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - 부호가 있는 32비트 2의 보수 정수
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int fooInt = 1;

        // Long - 부호가 있는 64비트 2의 보수 정수
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L은 이 변수의 값이 Long 타입임을 나타내는 데 사용됩니다.
        // L이 없는 것들은 기본적으로 정수로 간주됩니다.

        // 참고: 자바에는 부호 없는(unsigned) 타입이 없습니다.

        // Float - 단정도 32비트 IEEE 754 부동 소수점 수
        float fooFloat = 234.5f;
        // f는 이 변수의 값이 float 타입임을 나타내는 데 사용됩니다.
        // f를 지정하지 않으면 double로 간주됩니다.

        // Double - 배정도 64비트 IEEE 754 부동 소수점 수
        double fooDouble = 123.4;

        // Boolean - 참(true) & 거짓(false)
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - 단일 16비트 유니코드 문자
        char fooChar = 'A';

        // 변수를 변경할 수 없게 만들려면 final을 지정합니다.
        final int HOURS_I_WORK_PER_WEEK = 9001;

        // 문자열
        String fooString = "My String Is Here!";

        // \n은 새로운 줄을 시작하는 이스케이프 문자입니다.
        String barString = "Printing on a new line?\nNo Problem!";
        // \t는 탭 문자를 추가하는 이스케이프 문자입니다.
        String bazString = "Do you want to add a tab?\tNo Problem!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // 배열
        // 배열의 크기는 반드시 선언할 때 결정해야 합니다.
        // 배열을 선언하는 형식은 다음과 같습니다.
        //<자료형> [] <변수명> = new <자료형>[<배열 크기>];
        int [] intArray = new int[10];
        String [] stringArray = new String[1];
        boolean [] booleanArray = new boolean[100];

        // 배열을 선언하고 초기화하는 또 다른 방법
        int [] y = {9000, 1000, 1337};

        // 배열 인덱스 - 요소에 접근
        System.out.println("intArray @ 0: " + intArray[0]);

        // 배열의 인덱스는 0에서부터 시작하며 변경 가능합니다.
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // 기타 참고할 만한 자료구조
        // ArrayLists - 좀 더 많은 기능을 제공하고 크기를 변경 가능하다는 점을
        //              제외하면 배열과 비슷합니다.
        // LinkedLists
        // Maps
        // HashMaps

        ///////////////////////////////////////
        // 연산자
        ///////////////////////////////////////
        System.out.println("\n->Operators");

        int i1 = 1, i2 = 2; // 다중 선언의 축약형

        // 산술 연산은 이해하기 어렵지 않습니다.
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (0.5를 잘라 버립니다)

        // 나눗셈
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // 비교 연산자
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // 비트 연산자!
        /*
        ~       단항 보수 연산
        <<      산술적 왼쪽 시프트
        >>      산술적 오른쪽 시프트
        >>>     논리적 오른쪽 시프트
        &       비트 단위 논리곱(AND)
        ^       비트 단위 배타적 논리합(OR)
        |       비트 단위 논리합(OR)
        */

        // 증감 연산자
        int i = 0;
        System.out.println("\n->Inc/Dec-rementation");
        System.out.println(i++); //i = 1. 후치 증가 연산
        System.out.println(++i); //i = 2. 전치 증가 연산
        System.out.println(i--); //i = 1. 후치 감소 연산
        System.out.println(--i); //i = 0. 전치 감소 연산

        ///////////////////////////////////////
        // 제어 구조
        ///////////////////////////////////////
        System.out.println("\n->Control Structures");

        // if 문은 C 언어와 비슷합니다.
        int j = 10;
        if (j == 10){
            System.out.println("I get printed");
        } else if (j > 10) {
            System.out.println("I don't");
        } else {
            System.out.println("I also don't");
        }

        // while 루프
        int fooWhile = 0;
        while(fooWhile < 100)
        {
            // System.out.println(fooWhile);
            // 카운터를 증가
            // 99번 반복, fooWhile 0->99
            fooWhile++;
        }
        System.out.println("fooWhile Value: " + fooWhile);

        // do-while 루프
        int fooDoWhile = 0;
        do
        {
            // System.out.println(fooDoWhile);
            // 카운터를 증가
            // 99번 반복, fooDoWhile 0->99
            fooDoWhile++;
        }while(fooDoWhile < 100);
        System.out.println("fooDoWhile Value: " + fooDoWhile);

        // for 루프
        int fooFor;
        // for 루프 구조 => for(<초기식>; <조건식>; <증감식>)
        for(fooFor=0; fooFor<10; fooFor++){
            // System.out.println(fooFor);
            // 10번 반복, fooFor 0->9
        }
        System.out.println("fooFor Value: " + fooFor);

        // switch-case 문
        // switch는 byte, short, char, int 자료형을 대상으로 동작합니다.
        // 아울러 열거형을 비롯해 String 클래스 및 원시 타입을 감싼 Character, 
        // Byte, Short, Integer와 같은 몇 가지 특별한 클래스에 대해서도 동작합니다.
        int month = 3;
        String monthString;
        switch (month){
            case 1:
                    monthString = "January";
                    break;
            case 2:
                    monthString = "February";
                    break;
            case 3:
                    monthString = "March";
                    break;
            default:
                    monthString = "Some other month";
                    break;
        }
        System.out.println("Switch Case Result: " + monthString);


        ///////////////////////////////////////
        // 자료형 변환과 형변환
        ///////////////////////////////////////

        // 데이터 변환

        // 문자열에서 정수로 변환
        Integer.parseInt("123");// 정수 버전의 "123"을 반환

        // 정수를 문자열로 변환
        Integer.toString(123);// 문자열 버전의 123을 반환

        // 다른 변환에 대해서는 아래 클래스를 확인해 보세요.
        // Double
        // Long
        // String

        // 형변환
        // 자바 객체 또한 형변환할 수 있으며, 이와 관련해서 알아야 할 세부사항이
        // 많을뿐더러 다소 중급 수준에 해당하는 개념들도 다뤄야 합니다.
        // 이와 관련된 사항은 아래 링크를 참고하세요.
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // 클래스와 함수
        ///////////////////////////////////////

        System.out.println("\n->Classes & Functions");

        // (Bicycle 클래스의 정의)

        // 클래스를 인스턴스화하려면 new를 사용합니다.
        Bicycle trek = new Bicycle();

        // 객체의 메서드를 호출합니다.
        trek.speedUp(3); // 항상 설정자 메서드와 접근자 메서드를 사용해야 합니다.
        trek.setCadence(100);

        // 현재 객체의 값을 표시할 때는 관례적으로 toString을 사용합니다.
        System.out.println("trek info: " + trek.toString());

    } // main 메서드 끝
} // LearnJava 클래스 끝


// .java 파일 안에 다른 비공개 클래스를 포함할 수 있습니다.


// 클래스 선언 문법:
// <public/private/protected> class <클래스명>{
//    // 데이터 필드, 생성자, 함수가 모두 이곳에 들어갑니다.
//    // 자바에서는 함수를 메서드라고 부릅니다.
// }

class Bicycle {

    // Bicycle의 필드와 변수
    public int cadence; // Public: 어느 곳에서도 접근할 수 있습니다.
    private int speed;  // Private: 클래스 안에서만 접근할 수 있습니다.
    protected int gear; // Protected: 현재 클래스와 하위 클래스에서 접근할 수 있습니다.
    String name; // default: 현재 패키지 안에서만 접근할 수 있습니다.

    // 생성자는 클래스를 생성하는 방법 중 하나입니다.
    // 다음은 기본 생성자입니다.
    public Bicycle() {
        gear = 1;
        cadence = 50;
        speed = 5;
        name = "Bontrager";
    }

    // 다음은 구체화된 생성자입니다(인자를 담고 있습니다)
    public Bicycle(int startCadence, int startSpeed, int startGear, String name) {
        this.gear = startGear;
        this.cadence = startCadence;
        this.speed = startSpeed;
        this.name = name;
    }

    // 함수 문법:
    // <public/private/protected> <반환형> <함수명>(<인자>)

    // 자바 클래스는 필드에 대해 접근자 메서드와 설정자 메서드를 구현할 때가 많습니다.

    // 메서드 선언 문법:
    // <유효범위> <반환형> <메서드명>(<인자>)
    public int getCadence() {
        return cadence;
    }

    // void 메서드는 반환형이 필요하지 않습니다.
    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void speedUp(int increment) {
        speed += increment;
    }

    public void slowDown(int decrement) {
        speed -= decrement;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    // 현재 객체의 속성값을 표시하는 메서드
    @Override
    public String toString() {
        return "gear: " + gear +
                " cadence: " + cadence +
                " speed: " + speed +
                " name: " + name;
    }
} // Bicycle 클래스의 끝

// PennyFarthing은 Bicycle의 하위 클래스입니다.
class PennyFarthing extends Bicycle {
    // (페니 파딩은 앞바퀴가 굉장히 큰 자전거입니다. 기어가 없죠.)

    public PennyFarthing(int startCadence, int startSpeed){
        // super를 이용해 부모 생성자를 호출합니다.
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // @annotation을 이용해 재정의하는 메서드를 표시해야 합니다.
    // 애노테이션과 애노테이션의 용도에 관한 자세한 내용은 아래 링크를 참고하세요.
    // 애노테이션: http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGear(int gear) {
        gear = 0;
    }

}

```

## 기타 참고자료

다음 링크를 통해 다양한 주제를 이해하고 구글을 통해 구체적인 예제들을 찾아보세요.

공부할 만한 기타 주제:

* [썬/오라클의 자바 자습서](http://docs.oracle.com/javase/tutorial/index.html)

* [자바 접근 제한자](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [객체 지향 프로그래밍 개념](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [상속(Inheritance)](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [다형성(Polymorphism)](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [추상화(Abstraction)](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [예외(Exceptions)](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [인터페이스(Interfaces)](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [제네릭(Generics)](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [자바 코딩 관례(Java Code Conventions)](http://www.oracle.com/technetwork/java/codeconv-138413.html)
---
language: javascript
category: language
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
translators:
    - ["wikibook", "http://wikibook.co.kr"]
filename: javascript-kr.js
lang: ko-kr
---

자바스크립트는 넷스케이프의 브렌던 아이크(Brendan Eich)가 1995년에 만들었습니다. 
원래 자바스크립트는 웹사이트를 위한 단순한 스크립트 언어를 목표로 만들어졌는데, 
좀 더 복잡한 웹 애플리케이션을 만들기 위해 자바를 보완하는 역할이었지만 
웹 페이지와의 긴밀한 상호작용과 브라우저에 대한 지원 기능 덕분에 웹 프론트엔드에서 
자바보다 훨씬 더 보편적으로 쓰이게 됐습니다. 

그렇지만 자바스크립트는 웹 브라우저에만 국한되지 않습니다. 구글 크롬의 V8 자바스크립트 
엔진을 위한 독립형 런타임을 제공하는 Node.js는 점점 인기를 얻고 있습니다.

피드백 주시면 대단히 감사하겠습니다! [@adambrenecki](https://twitter.com/adambrenecki)나 
[adam@brenecki.id.au](mailto:adam@brenecki.id.au)를 통해 저와 만나실 수 있습니다.

```js
// 주석은 C와 비슷합니다. 한 줄짜리 주석은 두 개의 슬래시로 시작하고,
/* 여러 줄 주석은 슬래시 별표로 시작해서
   별표 슬래시로 끝납니다. */

// 구문은 세미콜론(;)으로 끝낼 수 있습니다.
doStuff();

// 하지만 꼭 그럴 필요는 없는데, 특정 경우를 제외하고
// 새 줄이 시작할 때마다 세미콜론이 자동으로 삽입되기 때문입니다. 
doStuff()

// 여기서는 세미콜론을 생략하겠습니다. 세미콜론을 생략할지 여부는
// 개인적인 취향이나 프로젝트의 스타일 가이드를 따릅니다.

///////////////////////////////////
// 1. 숫자, 문자열, 연산자

// 자바스크립트에는 단 하나의 숫자 타입(64비트 IEEE 754 배정도 숫자)만이
// 있습니다.
3 // = 3
1.5 // = 1.5

// 모든 기초 산술 연산은 기대한 대로 동작합니다.
1 + 1 // = 2
8 - 1 // = 7
10 * 2 // = 20
35 / 5 // = 7

// 나누어 떨어지지 않는 나눗셈도 포함됩니다.
5 / 2 // = 2.5

// 비트 연산도 지원됩니다. float을 대상으로 비트 연산을 수행하면
// 32비트까지 부호가 있는 int로 변환됩니다.
1 << 2 // = 4

// 괄호를 이용하면 우선순위를 지정할 수 있습니다. 
(1 + 3) * 2 // = 8

// 실제 숫자가 아닌 특별한 세 가지 값이 있습니다.
Infinity // 1/0 1/0과 같은 연산의 결과
-Infinity // -1/0과 같은 연산의 결과
NaN // 0/0과 같은 연산의 결과

// 불린 타입도 있습니다.
true
false

// 문자열은 '나 "로 생성합니다.
'abc'
"Hello, world"

// 부정 연산에는 ! 기호를 이용합니다.
!true // = false
!false // = true

// 동일성 연산은 ==
1 == 1 // = true
2 == 1 // = false

// 불일치 연산은 !=
1 != 1 // = false
2 != 1 // = true

// 그 밖의 비교 연산
1 < 10 // = true
1 > 10 // = false
2 <= 2 // = true
2 >= 2 // = true

// 문자열은 +로 연결할 수 있습니다.
"Hello " + "world!" // = "Hello world!"

// 그리고 <와 >로 비교할 수 있습니다.
"a" < "b" // = true

// 비교 시 타입 강제변환이 수행됩니다. 
"5" == 5 // = true

// ===를 쓰지 않는다면 말이죠.
"5" === 5 // = false

// charAt을 이용하면 문자열 내의 문자에 접근할 수 있습니다.
"This is a string".charAt(0)

// null과 undefined도 있습니다.
null // 의도적으로 값이 아님을 나타내는 데 사용합니다.
undefined // 값이 아직 설정되지 않음을 나타내는 데 사용합니다.

// null, undefinded, NaN, 0, ""은 거짓이며, 그 밖의 다른 모든 값은 참입니다.
// 참고로 0은 거짓이며, "0"은 참입니다(심지어 0 == "0"이더라도).

///////////////////////////////////
// 2. 변수, 배열, 객체

// 변수는 var 키워드로 선언합니다. 자바스크립트는 동적 타입 언어라서
// 타입을 지정할 필요가 없습니다. 값을 할당할 때는 = 문자 하나를 사용합니다.
var someVar = 5

// var 키워드를 지정하지 않아도 오류는 발생하지 않습니다.
someOtherVar = 10

// 그렇지만 변수가 여러분이 정의한 유효범위가 아니라 
// 전역 유효범위에 생성됩니다.

// 값을 할당하지 않은 채로 선언한 변수는 undefined로 설정됩니다. 
var someThirdVar // = undefined

// 변수에 수학 연산을 수행하는 축약형 표현은 다음과 같습니다.
someVar += 5 // someVar = someVar + 5;와 같음. 이제 someVar는 10. 
someVar *= 10 // somVar는 100

// 1을 더하거나 빼는 훨씬 더 짧은 표현도 있습니다.
someVar++ // 이제 someVar는 101
someVar-- // 다시 100으로 되돌아감

// 배열은 순차적인 임의 타입 값의 목록입니다.
var myArray = ["Hello", 45, true]

// 배열의 멤버는 대괄호로 둘러싼 인덱스를 이용해 접근할 수 있습니다.
// 배열의 인덱스는 0부터 시작합니다.
myArray[1] // = 45

// 자바스크립트의 객체는 다른 언어의 '사전'이나 '맵'과 같습니다.
// 즉, 키-값 쌍으로 구성된 비순차 컬렉션입니다.
{key1: "Hello", key2: "World"}

// 키는 문자열이지만 유효한 자바스크립트 식별자일 경우
// 작은따옴표는 필요하지 않습니다. 값은 어떤 타입이든 사용할 수 있습니다.
var myObj = {myKey: "myValue", "my other key": 4}

// 객체 속성에도 인덱스를 이용해 접근할 수 있습니다.
myObj["my other key"] // = 4

// 또는 키가 유효한 식별자일 경우 점 표기법을 이용해 접근할 수 있습니다.
myObj.myKey // = "myValue"

// 객체는 변경 가능합니다. 즉, 값을 변경하거나 새 키를 추가할 수 있습니다.
myObj.myThirdKey = true

// 설정되지 않은 값에 접근하려고 하면 undefined가 반환됩니다.
myObj.myFourthKey // = undefined

///////////////////////////////////
// 3. 로직과 제어 구조

// if 구조는 여러분이 예상한 대로 동작합니다.
var count = 1
if (count == 3){
    // count가 3일 경우 평가됨
} else if (count == 4) {
    // count가 4일 경우 평가됨
} else {
    // count가 3이나 4가 아닌 경우에 평가됨
}

// while도 마찬가지입니다.
while (true) {
    // 무한 루프!
}

// do-while 문은 항상 최소 한 번은 실행된다는 점을 제외하면
// while 문과 비슷합니다.
var input
do {
    input = getInput()
} while (!isValid(input))

// for 문은 C와 자바의 for 문과 같습니다.
// 초기화식; 지속 조건; 증감식
for (var i = 0; i < 5; i++){
    // 5번 실행됨
}

// &&는 논리 and이고 ||는 논리 or입니다.
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear"
}
if (colour == "red" || colour == "blue"){
    // 색은 빨강이거나 파랑
}

// &&와 ||은 "단축 평가"를 수행하는데, 기본값을 설정할 때 유용합니다.
var name = otherName || "default"

///////////////////////////////////
// 4. 함수, 유효범위, 클로저

// 자바스크립트 함수는 function 키워드로 선언합니다.
function myFunction(thing){
    return thing.toUpperCase()
}
myFunction("foo") // = "FOO"

// 함수는 "익명"으로, 즉 이름 없이 정의할 수도 있습니다.
function(thing){
    return thing.toLowerCase()
}
// (함수를 가리키는 이름이 없기 때문에 함수를 호출할 수 없습니다)

// 자바스크립트 함수는 일급 객체이므로 다른 변수에 재할당하고
// 다른 함수에 인자로 전달할 수 있습니다. 가령, 이벤트 핸들러를 만들 경우
function myFunction(){
    // 이 코드는 5초 내에 호출됨
}
setTimeout(myFunction, 5000)

// 다른 함수를 호출할 때 직접적으로 함수 구문을 작성할 수도 있습니다.

setTimeout(function myFunction(){
    // 이 코드는 5초 내에 호출됨
}, 5000)

// 자바스크립트에는 함수 유효범위가 있습니다. 
// 함수는 자체적인 유효범위를 가지지만 다른 블록은 유효범위를 가지지 않습니다.
if (true){
    var i = 5
}
i // = 5 - 블록 유효범위를 지원하는 언어에서는 undefined가 아닙니다.

// 이것은 "즉시 실행되는 익명 함수"라는 공통 패턴으로 이어지는데,
// 이 패턴은 임시 변수가 전역 유효범위로 유출되는 것을 방지합니다.
(function(){
    var temporary = 5
    // '전역 객체'에 할당하는 식으로 전역 유효범위에 접근할 수 있는데,
    // 브라우저에서 전역 객체는 항상 'window'입니다. 전역 객체는 
    // Node.js와 같은 브라우저가 아닌 환경에서는 다른 이름일 수도 있습니다.
    window.permanent = 10
    // 또는 앞에서 언급했다시피 var 키워드를 뺄 수도 있습니다.
    permanent2 = 15
})()
temporary // ReferenceError 발생
permanent // = 10
permanent2 // = 15

// 자바스크립트의 강력한 기능 중 하나는 클로저(closure)입니다.
// 함수가 다른 함수 안에서 정의되면 안쪽에 정의된 함수는 바깥 함수의
// 모든 변수에 접근할 수 있습니다.
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!"
    function inner(){
        alert(prompt)
    }
    setTimeout(inner, 5000)
    // setTimeout은 비동기적으로 동작하므로 이 함수는 5초 동안
    // 기다리지 않고 실행을 마칩니다. 하지만 5초가 지나면 inner에서도
    // prompt의 값에 접근할 수 있습니다.
}
sayHelloInFiveSeconds("Adam") // 5초 내로 "Hello, Adam!"이라고 적힌 팝업이 표시됨

///////////////////////////////////
// 5. 객체 심화; 생성자와 프로토타입

// 객체는 함수를 포함할 수 있습니다.
var myObj = {
    myFunc: function(){
        return "Hello world!"
    }
}
myObj.myFunc() // = "Hello world!"

// 객체에 포함된 함수가 호출되면 함수에서는 this 키워드를 이용해
// 해당 함수가 포함된 객체에 접근할 수 있습니다.
myObj = {
    myString: "Hello world!",
    myFunc: function(){
        return this.myString
    }
}
myObj.myFunc() // = "Hello world!"

// 여기서 설정한 것은 함수가 정의된 곳이 아닌 함수가 호출되는 
// 방식과 관련이 있습니다. 그래서 아래 함수는 객체 컨텍스트에서 
// 호출되지 않으면 동작하지 않습니다.
var myFunc = myObj.myFunc
myFunc() // = undefined

// 반대로 함수는 객체에 할당하고 this를 통해 해당 객체에 접근할 수 있습니다.
// 함수를 정의할 때 객체에 추가되지 않았더라도 마찬가지입니다.
var myOtherFunc = function(){
    return this.myString.toUpperCase()
}
myObj.myOtherFunc = myOtherFunc
myObj.myOtherFunc() // = "HELLO WORLD!"

// new 키워드로 함수를 호출하면 새로운 객체가 생성되고 this를 통해
// 함수에서 사용할 수 있게 됩니다. 이런 식으로 설계된 함수를 생성자라 합니다.

var MyConstructor = function(){
    this.myNumber = 5
}
myNewObj = new MyConstructor() // = {myNumber: 5}
myNewObj.myNumber // = 5

// 모든 자바스크립트 객체는 'prototype'을 가지고 있습니다. 어떤 객체에 대해
// 실제 객체에는 존재하지 않는 프로퍼티에 접근하면 인터프리터는 프로로타입에서
// 해당 프로퍼티를 찾습니다.

// 일부 자바스크립트 구현체에서는 __proto__라는 마법의 프로퍼티로
// 객체의 프로토타입에 접근하는 것을 허용하기도 합니다. 프로토타입을 
// 설명하기에는 이런 내용도 도움되겠지만 __proto__는 표준에 포함돼 
// 있지 않습니다. 나중에 프로토타입을 사용하는 표준 방법을 살펴보겠습니다.
var myObj = {
    myString: "Hello world!",
}
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
}
myObj.__proto__ = myPrototype
myObj.meaningOfLife // = 42

// 이 방법은 함수에도 통합니다.
myObj.myFunc() // = "hello world!"

// 물론 프로퍼티가 프로토타입에 존재하지 않으면
// 프로토타입의 프로토타입을 찾는 식으로 진행됩니다.
myPrototype.__proto__ = {
    myBoolean: true
}
myObj.myBoolean // = true

// 여기서 복사는 일어나지 않습니다. 각 객체에는 프로토타입에 대한
// 참조가 보관돼 있습니다. 이는 프로토타입을 변경하면 변경사항이 
// 모든 곳에 반영된다는 의미입니다.
myPrototype.meaningOfLife = 43
myObj.meaningOfLife // = 43

// 앞에서 __proto__가 표준에 포함돼 있지 않다고 이야기했는데, 
// 기존 객체의 프로토타입을 변경하는 표준 방법은 없습니다. 
// 하지만 특정 프로토타입을 가지고 새로운 객체를 생성하는 두 가지
// 방법이 있습니다.

// 첫 번째 방법은 Object.create를 이용하는 것인데, 
// Object.create는 최근에 자바스크립트에 추가된 것이라서 아직까지 
// 모든 구현체에서 이용할 수 있는 것은 아닙니다.
var myObj = Object.create(myPrototype)
myObj.meaningOfLife // = 43

// 두 번째 방법은 어디서나 통하는 방법인데, 생성자와 관련이 있습니다.
// 생성자에는 prototype이라는 프로퍼티가 있습니다. 이 프로퍼티는
// 생성자 함수 자체의 프로토타입이 *아니고* 생성자와 new 키워드를 이용해
// 객체가 생성될 때 새로운 객체가 받는 프로토타입입니다.
myConstructor.prototype = {
    getMyNumber: function(){
        return this.myNumber
    }
}
var myNewObj2 = new myConstructor()
myNewObj2.getMyNumber() // = 5

// 문자열과 숫자와 같은 내장 타입에도 동등한 래퍼 객체를
// 생성하는 생성자가 있습니다.
var myNumber = 12
var myNumberObj = new Number(12)
myNumber == myNumberObj // = true

// 하지만 정확히 같지는 않습니다.
typeof myNumber // = 'number'
typeof myNumberObj // = 'object'
myNumber === myNumberObj // = false
if (0){
    // 0은 거짓이라서 이 코드는 실행되지 않습니다.
}

// 하지만 래퍼 객체와 일반 내장 함수는 프로토타입을 공유하기 때문에 
// 가령 문자열에 실제로 기능을 추가할 수 있습니다.
String.prototype.firstCharacter = function(){
    return this.charAt(0)
}
"abc".firstCharacter() // = "a"

// 이러한 사실은 기존 자바스크립트 버전에서 자바스크립트의
// 새로운 기능을 구현하는 "폴리필(polyfilling)"에 자주 이용되므로
// 오래된 버전의 브라우저와 같이 기존 환경에서 사용될 수 있습니다.

// 예를 들어, Object.create가 모든 구현체에서 사용 가능한 것은 아니라고 
// 했지만 아래의 폴리필을 이용해 Object.create를 여전히 사용할 수 있습니다.
if (Object.create === undefined){ // 이미 존재하면 덮어쓰지 않음
    Object.create = function(proto){
        // 올바른 프로토타입을 가지고 임시 생성자를 만듬
        var Constructor = function(){}
        Constructor.prototype = proto
        // 그런 다음 임시 생성자를 이용해 새로운 적절한 프로토타입을
        // 포함한 객체를 생성
        return new Constructor()
    }
}
```

## 기타 참고 자료

[모질라 개발자 네트워크](https://developer.mozilla.org/en-US/docs/Web/JavaScript)에서는 
자바스크립트에 대한 훌륭한 문서를 제공합니다. 더불어 위키 형식이라서 좀 더 많은 사항을 
배우게 되면 여러분만의 지식을 공유함으로써 다른 사람들에게 도움을 줄 수도 있습니다.

MDN의 ['자바스크립트 재입문'](https://developer.mozilla.org/ko/docs/A_re-introduction_to_JavaScript)에서는 
여기서 다룬 개념의 상당수를 더욱 자세히 다루고 있습니다. 이 자료에서는 자바스크립트 언어 자체에 
대해서만 상당히 신중하게 다뤘습니다. 웹 페이지에서 자바스크립트를 사용하는 방법을 배우고 싶다면 
[문서 객체 모델(Document Object Model)](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)에 
관해 배우는 것으로 시작하길 바랍니다.

[자바스크립트 가든](http://bonsaiden.github.io/JavaScript-Garden/)에서는 자바스크립트 언어에서
직관에 어긋나는 모든 부분들을 심도 있게 다룹니다.

더불어 이 글에 직접적으로 기여한 분들로, 내용 중 일부는 이 사이트에 있는 
루이 딘(Louie Dihn)의 파이썬 튜토리얼과 모질라 개발자 네트워크에 있는 
[자바스크립트 튜토리얼](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)을 참고했습니다.
---
language: json
filename: learnjson-kr.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
  - ["Michael Neth", "https://github.com/infernocloud"]
translators:
  - ["Wooseop Kim", "https://github.com/linterpreteur"]
lang: ko-kr
---

JSON은 아주 간단한 데이터 교환 포맷입니다. [json.org](http://json.org/json-ko.html)에 의하면, 사람이 읽고 쓰기 쉬우며 기계가 분석하고 생성하기 쉽습니다.

JSON 한 개는 반드시 이하의 둘 중 하나를 나타내야 합니다.
* 이름과 값 쌍의 모임(`{ }`). 이는 다양한 언어에서 객체, 레코드, 구조체, 딕셔너리, 해시 테이블, 키 리스트, 혹은 연관 배열로 구현됩니다.
* 값에 순서가 있는 리스트 (`[ ]`). 이는 다양한 언어에서 배열, 벡터, 리스트, 시퀀스로 구현됩니다.

순수한 JSON은 사실 주석이 없지만 대부분의 파서는 C 스타일의 주석(`//`, `/* */`)도 받아들일 겁니다. 일부 파서는 꼬리에 오는 쉼표, 즉 배열의 마지막 원소 혹은 객체의 마지막 속성 다음에 오는 쉼표도 인정하겠지만, 호환성을 위해 쓰지 않는 것이 좋습니다.

이 튜토리얼의 목적에 따라 모든 것은 100% 유효한 JSON입니다. 다행스럽게도 JSON은 다소 자기서술적입니다.

지원하는 데이터 형:

* 문자열: `"안녕"`, `"\"따옴표.\""`, `"\u0abe"`, `"개행 문자.\n"`
* 수: `23`, `0.11`, `12e10`, `3.141e-10`, `1.23e+4`
* 객체: `{ "키": "값" }`
* 배열: `["값 값 값"]`
* 기타: `true`, `false`, `null`

```json
{
  "키": "값",

  "키는": "반드시 큰따옴표 안에 있어야 합니다.",
  "수": 0,
  "문자열": "Hellø, wørld. 모든 유니코드와 \"탈출 문자\"가 지원됩니다.",
  "부울도 있나?": true,
  "아무 것도 없는 건": null,

  "큰 수": 1.2e+100,

  "객체": {
    "주석": "문서 구조의 대부분은 객체가 될 것입니다.",

    "배열": [0, 1, 2, 3, "배열 안에는 무엇이든 올 수 있습니다.", 5],

    "다른 객체": {
      "주석": "객체는 객체를 포함할 수 있습니다. 아주 유용하죠."
    }
  },

  "장난이지롱": [
    {
      "칼륨이 풍부한": ["바나나"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "다른 방식": {
    "주석": "여기 보세요!"
  , "쉼표의 위치는": "상관 없습니다. 다음 키 전에만 온다면 유효합니다."
  , "다른 주석": "참 좋죠"
  },

  "공백은": "상관이 없습니다.",

  "짧았죠": "끝입니다. JSON의 모든 것을 터득하셨습니다."
}
```

## 더 읽기

* [JSON.org](http://json.org/json-ko.html) 플로우차트와 같은 그래픽을 이용해 설명한 JSON의 모든 것.
---
language: kotlin
contributors:
    - ["S Webber", "https://github.com/s-webber"]
translators: 
    - ["Alan Jeon", "https://github.com/skyisle"]
lang: ko-kr
filename: LearnKotlin-kr.kt
---

Kotlin 은 정적 타입 프로그래밍 언어로 JVM, 안드로이드, 브라우져를 지원하며 Java 와 100% 상호 운용이 가능합니다.
[자세한 내용은 다음을 참고하세요.](https://kotlinlang.org/)

```kotlin
// 한 줄짜리 주석은 // 로 시작합니다.
/*
여러 줄 주석은 이와 같이 표시합니다. 
*/

// "package" 예약어는 자바와 동일하게 사용됩니다.
package com.learnxinyminutes.kotlin

/*
Kotlin 프로그램의 진입점은 main 이라는 함수명으로 지정됩니다. 
이 함수에 명령행 인수가 배열로 전달됩니다.
*/
fun main(args: Array<String>) {
    /*
    값을 선언할때는 "var" 또는 "val"이 사용됩니다.
    "var"와는 다르게 "val"로 선언된 변수에는 값을 재할당 할 수 없습니다.
    */
    val fooVal = 10 // fooVal 에 다른 값을 다시 할당 할 수 없습니다.
    var fooVar = 10
    fooVar = 20 // fooVar 에는 선언 이후에도 다른 값을 할당 할 수 있습니다

    /*
    대부분의 경우, Kotlin 에서는 변수 타입을 판단할 수 있기때문에 명시적으로 지정해 주지 않을 수 있습니다.
    다음과 같이 변수의 타입을 명시적으로 지정할 수 있습니다.
    */
    val foo: Int = 7

    /*
    문자형은 Java와 비슷하게 표시될 수 있습니다.
    이스케이핑에는 백슬래시를 사용합니다.
    */
    val fooString = "My String Is Here!"
    val barString = "Printing on a new line?\nNo Problem!"
    val bazString = "Do you want to add a tab?\tNo Problem!"
    println(fooString)
    println(barString)
    println(bazString)

    /*
    Raw 문자열은 쌍따옴표 3개(""")로 표기합니다.
    Raw 문자열에는 줄바꿈이나 모든 다른 문자들을 사용할 수 있습니다. 
    */
    val fooRawString = """
fun helloWorld(val name : String) {
   println("Hello, world!")
}
"""
    println(fooRawString)

    /*
    문자열은 템플릿 표현식을 포함할 수 있습니다.
    템플릿은 달러 기호($)로 시작합니다. 
    */
    val fooTemplateString = "$fooString has ${fooString.length} characters"
    println(fooTemplateString)

    /*
    변수가 null 값을 가지려면 이를 명시적으로 선언해야 합니다.
    변수 선언시 타입에 ? 표시를 붙여 nullable 을 표시합니다.
    ?. 연산자를 사용해 nullable 변수에 접근합니다.
    ?: 연산자를 이용해서 변수 값이 null 일때 사용할 값을 지정합니다.
    */
    var fooNullable: String? = "abc"
    println(fooNullable?.length) // => 3
    println(fooNullable?.length ?: -1) // => 3
    fooNullable = null
    println(fooNullable?.length) // => null
    println(fooNullable?.length ?: -1) // => -1

    /*
    함수는 "fun" 예약어를 사용해 선언합니다.
    함수명 이후 괄호 안에 인자를 기술합니다.
    함수 인자에 기본 값을 지정할 수도 있습니다.
    함수에 리턴값이 있을 때, 필요한 경우 인자 뒤에 타입을 명시합니다.
    */
    fun hello(name: String = "world"): String {
        return "Hello, $name!"
    }
    println(hello("foo")) // => Hello, foo!
    println(hello(name = "bar")) // => Hello, bar!
    println(hello()) // => Hello, world!

    /*
    함수에 가변 인자를 넘기려면 인자에 "vararg" 예약어를 사용합니다. 
    */
    fun varargExample(vararg names: Int) {
        println("Argument has ${names.size} elements")
    }
    varargExample() // => 인자가 0개 인 경우
    varargExample(1) // => 인자가 1개인 경우
    varargExample(1, 2, 3) // => 인자가 3개인 경우

    /*
    함수가 단일 표현식으로 이루어진 경우에 중괄호를 생략할 수 있습니다.
    이때 함수 구현부는 = 기호 이후에 기술합니다.
    */
    fun odd(x: Int): Boolean = x % 2 == 1
    println(odd(6)) // => false
    println(odd(7)) // => true

    // 리턴 타입이 유추 가능한 경우 이를 명시하지 않아도 됩니다. 
    fun even(x: Int) = x % 2 == 0
    println(even(6)) // => true
    println(even(7)) // => false

    // 함수는 함수를 인자를 받을 수 있고 함수를 리턴할 수 있습니다. 
    fun not(f: (Int) -> Boolean): (Int) -> Boolean {
        return {n -> !f.invoke(n)}
    }
    // 함수는 :: 연산자를 사용해서 다른 함수에 인자로 넘길 수 있습니다. 
    val notOdd = not(::odd)
    val notEven = not(::even)
    // 람다식을 인자로 사용할 수 있습니다. 
    val notZero = not {n -> n == 0}
    /*
    하나의 인자를 가지는 람다식의 선언부와 -> 연산자는 생략될 수 있습니다.
    이때 그 인자명은 it로 지정됩니다.
    */
    val notPositive = not {it > 0}
    for (i in 0..4) {
        println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}")
    }

    // "class" 예약어는 클래스를 선언할 때 사용됩니다. 
    class ExampleClass(val x: Int) {
        fun memberFunction(y: Int): Int {
            return x + y
        }

        infix fun infixMemberFunction(y: Int): Int {
            return x * y
        }
    }
    /*
    새로운 객체를 생성하기 위해서는 생성자를 바로 호출합니다.
    Kotlin 에서는 new 예약어가 없다는 걸 기억하세요.
    */
    val fooExampleClass = ExampleClass(7)
    // 맴버 함수는 dot 표기로 호출할 수 있습니다. 
    println(fooExampleClass.memberFunction(4)) // => 11
    /*
    함수 선언에 "infix" 예약어를 사용하면 이 함수를 중위 표현식(infix notation)으로 호출할 수 있습니다 
    */
    println(fooExampleClass infixMemberFunction 4) // => 28

    /*
    데이터 클래스로 데이터만을 가지고 있는 클래스를 손쉽게 선언할 수 있습니다.
    "hashCode"/"equals" 와 "toString" 는 자동으로 생성됩니다.
    */
    data class DataClassExample (val x: Int, val y: Int, val z: Int)
    val fooData = DataClassExample(1, 2, 4)
    println(fooData) // => DataClassExample(x=1, y=2, z=4)

    // 데이터 클래스는 copy 함수를 가지고 있습니다. 
    val fooCopy = fooData.copy(y = 100)
    println(fooCopy) // => DataClassExample(x=1, y=100, z=4)

    // 객체를 여러 변수로 분리할 수 있습니다. 
    val (a, b, c) = fooCopy
    println("$a $b $c") // => 1 100 4
    
    // "for" 루프에서 변수 분리 하기 
    for ((a, b, c) in listOf(fooData)) {
        println("$a $b $c") // => 1 100 4
    }
    
    val mapData = mapOf("a" to 1, "b" to 2)
    // Map.Entry 또한 키와 값으로 분리가 가능합니다. 
    for ((key, value) in mapData) {
        println("$key -> $value")
    }

    // "with" 함수는 JavaScript 의 "with" 구문과 비슷하게 사용됩니다. 
    data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
    val fooMutableData = MutableDataClassExample(7, 4, 9)
    with (fooMutableData) {
        x -= 2
        y += 2
        z--
    }
    println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8)

    /*
    "listOf" 함수로 리스트를 만들 수 있습니다.
    리스트는 변경 불가능(immutable)하게 만들어져 항목의 추가 삭제가 불가능합니다.
    */
    val fooList = listOf("a", "b", "c")
    println(fooList.size) // => 3
    println(fooList.first()) // => a
    println(fooList.last()) // => c
    // 각 항목은 인덱스로 접근이 가능합니다. 
    println(fooList[1]) // => b

    // 변경가능한(mutable) 리스트는 "mutableListOf" 함수로 만들 수 있습니다. 
    val fooMutableList = mutableListOf("a", "b", "c")
    fooMutableList.add("d")
    println(fooMutableList.last()) // => d
    println(fooMutableList.size) // => 4

    // 집합(set)은 "setOf" 함수로 만들 수 있습니다.
    val fooSet = setOf("a", "b", "c")
    println(fooSet.contains("a")) // => true
    println(fooSet.contains("z")) // => false

    // 맵은 "mapOf" 함수로 만들 수 있습니다.
    val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9)
    // 맵은 키를 통해 그 값에 접근할 수 있습니다. Map values can be accessed by their key.
    println(fooMap["a"]) // => 8

    /*
    시퀀스는 지연 평가되는 컬랙션을 말합니다. Sequences represent lazily-evaluated collections.
    "generateSequence" 를 사용해 시퀀스를 만들 수 있습니다. We can create a sequence using the "generateSequence" function.
    */
    val fooSequence = generateSequence(1, { it + 1 })
    val x = fooSequence.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // 다음은 시퀀스를 사용해서 피보나치 수열을 생성하는 예입니다.
    fun fibonacciSequence(): Sequence<Long> {
        var a = 0L
        var b = 1L

        fun next(): Long {
            val result = a + b
            a = b
            b = result
            return a
        }

        return generateSequence(::next)
    }
    val y = fibonacciSequence().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

    // Kotlin 은 컬랙션에서 사용할 수 있는 고차(higher-order)함수를 제공합니다.
    val z = (1..9).map {it * 3}
                  .filter {it < 20}
                  .groupBy {it % 2 == 0}
                  .mapKeys {if (it.key) "even" else "odd"}
    println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]}

    // "for" 루프는 이터레이터를 제공하는 어떤 것과도 함께 사용할 수 있습니다.
    for (c in "hello") {
        println(c)
    }

    // "while" 루프는 다른 언어들과 동일하게 사용됩니다.
    var ctr = 0
    while (ctr < 5) {
        println(ctr)
        ctr++
    }
    do {
        println(ctr)
        ctr++
    } while (ctr < 10)

    /*
    "if"는 값을 리턴하는 표현으로 사용될 수 있습니다.
    그래서 Kotlin 에서는 삼항 ?: 연산자가 필요하지 않습니다.
    */
    val num = 5
    val message = if (num % 2 == 0) "even" else "odd"
    println("$num is $message") // => 5 is odd

    // "when"은 "if-else if" 를 대체할때 사용할 수 있습니다.
    val i = 10
    when {
        i < 7 -> println("first block")
        fooString.startsWith("hello") -> println("second block")
        else -> println("else block")
    }

    // "when"은 인수와 함께 사용될 수 있습니다.
    when (i) {
        0, 21 -> println("0 or 21")
        in 1..20 -> println("in the range 1 to 20")
        else -> println("none of the above")
    }

    // "when"은 값을 리턴하는 함수처럼 사용될 수 있습니다.
    var result = when (i) {
        0, 21 -> "0 or 21"
        in 1..20 -> "in the range 1 to 20"
        else -> "none of the above"
    }
    println(result)

    /*
    객체가 어떤 타입인지를 확인하기 위해 "is" 연산자를 사용할 수 있습니다.
    타입 체크를 통과하면 객체의 명시적인 형변환 없이도 그 타입 값으로 사용될 수 있습니다.
    이를 스마트 변환(Smartcast)이라 부릅니다.
    */
    fun smartCastExample(x: Any) : Boolean {
        if (x is Boolean) {
            // x is automatically cast to Boolean
            return x
        } else if (x is Int) {
            // x is automatically cast to Int
            return x > 0
        } else if (x is String) {
            // x is automatically cast to String
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(smartCastExample("Hello, world!")) // => true
    println(smartCastExample("")) // => false
    println(smartCastExample(5)) // => true
    println(smartCastExample(0)) // => false
    println(smartCastExample(true)) // => true

    // 스마트 변환은 when 블럭과도 함께 사용됩니다.
    fun smartCastWhenExample(x: Any) = when (x) {
        is Boolean -> x
        is Int -> x > 0
        is String -> x.isNotEmpty()
        else -> false
    }

    /*
    확장(Extensions)을 이용해 클래스에 새로운 기능을 추가할 수 있습니다.
    C#에서의 확장 매서드와 유사합니다.
    */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("Hello, world!".remove('l')) // => Heo, word!

    println(EnumExample.A) // => A
    println(ObjectExample.hello()) // => hello
}

// Enum 클래스는 자바의 enum 타입과 유사합니다.
enum class EnumExample {
    A, B, C
}

/*
"object" 예약어는 싱클톤 객체를 생성할 때 사용됩니다. 
객체를 새로 생성할 수는 없지만 이름을 가지고 접근해 사용할 수 있습니다.
이는 스칼라의 싱글톤 객체와 유사합니다.
*/
object ObjectExample {
    fun hello(): String {
        return "hello"
    }
}

fun useObject() {
    ObjectExample.hello()
    val someRef: Any = ObjectExample // 객체의 이름을 그대로 사용합니다.
}

```

### 더 알아보기

* [Kotlin tutorials (EN)](https://kotlinlang.org/docs/tutorials/)
* [Try Kotlin in your browser (EN)](http://try.kotlinlang.org/)
* [A list of Kotlin resources (EN)](http://kotlin.link/)
---
language: Lua
category: language
contributors:
    - ["Tyler Neylon", "http://tylerneylon.com/"]
translators:
    - ["wikibook", "http://wikibook.co.kr"]
lang: ko-kr
filename: learnlua-kr.lua
---

```lua
-- 대시 두 개는 한 줄짜리 주석을 의미합니다.

--[[
     [와 ]를 두 개씩 추가하면 여러 줄 주석이 됩니다.
--]]

----------------------------------------------------
-- 1. 변수와 흐름 제어
----------------------------------------------------

num = 42  -- 모든 숫자는 double입니다.
-- 놀랄 필요는 없습니다. 64비트 double은 
-- 정확한 int 값을 저장하기 위해 52비트로 구성돼 
-- 있습니다. 52비트 이하의 int 값에 대해서는 
-- 장비 정밀도와 관련된 문제가 생기지 않습니다.

s = 'walternate'  -- 파이썬과 같은 불변 문자열
t = "큰따옴표를 써도 됩니다"
u = [[ 이중 대괄호는
       여러 줄 문자열을
       나타냅니다.]]
t = nil  -- 미정의 t. 루아는 가비지 컬렉션을 지원합니다.

-- 블록은 do/end와 같은 키워드로 나타냅니다:
while num < 50 do
  num = num + 1  --  ++나 += 유형의 연산자는 쓸 수 없습니다.
end

-- If 절:
if num > 40 then
  print('40 이상')
elseif s ~= 'walternate' then  -- ~=은 '같지 않다'입니다.
  -- 동일성 검사는 파이썬과 마찬가지로 ==입니다. 
  -- 문자열에도 쓸 수 있습니다.
  io.write('not over 40\n')  -- 기본적으로 stdout에 씁니다.
else
  -- 변수는 기본적으로 전역 변수입니다.
  thisIsGlobal = 5  -- 낙타 표기법이 일반적입니다.

  -- 변수를 지역 변수로 만드는 방법은 다음과 같습니다:
  local line = io.read()  -- 다음 stdin 줄을 읽습니다

  -- 문자열 연결에는 .. 연산자를 씁니다:
  print('겨울이 오고 있습니다, ' .. line)
end

-- 미정의 변수는 nil을 반환합니다.
-- 다음 코드를 실행해도 오류가 나지 않습니다:
foo = anUnknownVariable  -- 이제 foo는 nil입니다.

aBoolValue = false

-- nil과 false만이 거짓값입니다; 0과 ''은 참입니다!
if not aBoolValue then print('twas false') end

-- 'or'와 'and'는 단축 평가(short-circuit)됩니다.
-- 다음 코드는 C/자바스크립트의 a?b:c 연산자와 비슷합니다:
ans = aBoolValue and 'yes' or 'no'  --> 'no'

karlSum = 0
for i = 1, 100 do  -- 범위에는 마지막 요소도 포함됩니다.
  karlSum = karlSum + i
end

-- 카운트 다운을 할 때는 "100, 1, -1"을 범위로 씁니다.
fredSum = 0
for j = 100, 1, -1 do fredSum = fredSum + j end

-- 일반적으로 범위는 begin, end[, step]입니다.

-- 또 다른 반복문 구문은 다음과 같습니다:
repeat
  print('미래의 방식')
  num = num - 1
until num == 0


----------------------------------------------------
-- 2. 함수
----------------------------------------------------

function fib(n)
  if n < 2 then return n end
  return fib(n - 2) + fib(n - 1)
end

-- 클로저와 익명 함수도 사용할 수 있습니다:
function adder(x)
  -- 반환된 함수는 adder가 호출될 때 생성되고 x의
  -- 값이 유지됩니다:
  return function (y) return x + y end
end
a1 = adder(9)
a2 = adder(36)
print(a1(16))  --> 25
print(a2(64))  --> 100

-- 반환문, 함수 호출, 할당문은 길이가 다른
-- 값의 리스트에 대해서도 모두 동작합니다.
-- 리스트에 값이 더 적을 때는 nil이 할당/반환되고
-- 리스트에 값이 더 많을 때는 나머지 값은 버려집니다.

x, y, z = 1, 2, 3, 4
-- 이제 x = 1, y = 2, z = 3이고 4는 버려집니다.

function bar(a, b, c)
  print(a, b, c)
  return 4, 8, 15, 16, 23, 42
end

x, y = bar('zaphod')  --> "zaphod  nil nil"가 출력
-- 이제 x = 4, y = 8이고 15~42의 값은 버려집니다.

-- 함수는 일급 객체이고, 지역/전역 유효범위를 가질
-- 수 있습니다. 아래의 두 함수는 같습니다:
function f(x) return x * x end
f = function (x) return x * x end

-- 그리고 아래의 두 함수도 마찬가지입니다:
local function g(x) return math.sin(x) end
local g; g  = function (x) return math.sin(x) end
-- 'local g'라고 선언하면 g를 지역 함수로 만듭니다.

-- 그나저나 삼각 함수는 라디안 단위로 동작합니다.

-- 함수를 호출할 때 문자열 매개변수를 하나만 전달한다면
-- 괄호를 쓰지 않아도 됩니다:
print 'hello'  -- 잘 동작합니다.


----------------------------------------------------
-- 3. 테이블
----------------------------------------------------

-- 테이블 = 루아의 유일한 복합 자료구조로서, 연관 배열입니다.
-- PHP의 배열이나 자바스크립트의 객체와 비슷하며,
-- 리스트로도 사용할 수 있는 해시 기반의 딕셔너리입니다.

-- 테이블을 딕셔너리/맵으로 사용하기:

-- 딕셔너리 리터럴은 기본적으로 문자열 키를 가집니다:
t = {key1 = 'value1', key2 = false}

-- 문자열 키에는 자바스크립트와 유사한 점 표기법을 쓸 수 있습니다:
print(t.key1)  -- 'value1'을 출력.
t.newKey = {}  -- 새 키/값 쌍을 추가.
t.key2 = nil   -- 테이블에서 key2를 제거.

-- (nil이 아닌) 값을 키로 사용하는 리터럴 표기법:
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28])  -- "tau"가 출력

-- 키 매칭은 기본적으로 숫자와 문자열에 대해서는 값으로 하지만
-- 테이블에 대해서는 식별자로 합니다.
a = u['@!#']  -- Now a = 'qbert'.
b = u[{}]     -- We might expect 1729, but it's nil:
a = u['@!#']  -- 이제 a는 'qbert'입니다.
b = u[{}]     -- 1729를 예상했겠지만 nil입니다:
-- 탐색이 실패하기 때문에 b는 nil입니다. 탐색이 실패하는 이유는
-- 사용된 키가 원본 값을 저장할 때 사용한 키와 동일한 객체가 아니기
-- 때문입니다. 따라서 문자열 및 숫자가 좀 더 이식성 있는 키입니다.

-- 테이블 하나를 매개변수로 취하는 함수를 호출할 때는 괄호가 필요하지 않습니다:
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'}  -- 'Sonmi~451'를 출력.

for key, val in pairs(u) do  -- 테이블 순회
  print(key, val)
end

-- _G는 모든 전역 멤버에 대한 특별한 테이블입니다.
print(_G['_G'] == _G)  -- 'true'가 출력

-- 테이블을 리스트/배열로 사용하기:

-- 리스트 리터럴은 암묵적으로 int 키로 설정됩니다:
v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do  -- #v는 리스트 v의 크기입니다.
  print(v[i])  -- 인덱스가 1에서 시작합니다!! 제정신이 아닙니다!
end
-- 'list'는 실제 타입이 아닙니다. v는 연속된 정수형 키가 포함된
-- 테이블이고 리스트로 취급될 뿐입니다.

----------------------------------------------------
-- 3.1 메타테이블과 메타메서드
----------------------------------------------------

-- 테이블은 테이블에 연산자 오버로딩을 가능하게 하는 메타테이블을
-- 가질 수 있습니다. 나중에 메타테이블이 어떻게 자바스크립트 
-- 프로토타입과 같은 행위를 지원하는지 살펴보겠습니다.

f1 = {a = 1, b = 2}  -- 분수 a/b를 표현
f2 = {a = 2, b = 3}

-- 다음 코드는 실패합니다:
-- s = f1 + f2

metafraction = {}
function metafraction.__add(f1, f2)
  sum = {}
  sum.b = f1.b * f2.b
  sum.a = f1.a * f2.b + f2.a * f1.b
  return sum
end

setmetatable(f1, metafraction)
setmetatable(f2, metafraction)

s = f1 + f2  -- f1의 메타테이블을 대상으로 __add(f1, f2)를 호출

-- f1과 f2는 자바스크립트의 프로토타입과 달리 각 메타테이블에 대한 
-- 키가 없어서 getmetatable(f1)과 같이 받아와야 합니다.
-- 메타테이블은 __add 같은 루아가 알고 있는 키가 지정된 일반 테이블입니다.

-- 그렇지만 다음 줄은 s가 메타테이블을 가지고 있지 않기 때문에 실패합니다.
-- t = s + s
-- 아래와 같이 클래스와 유사한 패턴은 이러한 문제가 발생하지 않습니다.

-- 메타테이블에 대한 __index는 점을 이용한 탐색을 오버로드합니다:
defaultFavs = {animal = 'gru', food = 'donuts'}
myFavs = {food = 'pizza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal  -- 동작합니다! 고마워요, 메타테이블!

-- 직접적인 메타테이블 탐색이 실패할 경우 메타테이블의 __index 값을 이용해
-- 재시도하고, 이런 과정이 반복됩니다.

-- __index 값은 좀 더 세분화된 탐색을 위해 function(tbl, key)가
-- 될 수도 있습니다.

-- __index, __add, ...의 값을 메타메서드라고 합니다.
-- 다음은 메타메서드를 가진 테이블의 전체 목록입니다.

-- __add(a, b)                     for a + b
-- __sub(a, b)                     for a - b
-- __mul(a, b)                     for a * b
-- __div(a, b)                     for a / b
-- __mod(a, b)                     for a % b
-- __pow(a, b)                     for a ^ b
-- __unm(a)                        for -a
-- __concat(a, b)                  for a .. b
-- __len(a)                        for #a
-- __eq(a, b)                      for a == b
-- __lt(a, b)                      for a < b
-- __le(a, b)                      for a <= b
-- __index(a, b)  <fn이나 테이블>  for a.b
-- __newindex(a, b, c)             for a.b = c
-- __call(a, ...)                  for a(...)

----------------------------------------------------
-- 3.2 클래스 형태의 테이블과 상속
----------------------------------------------------

-- 루아에는 클래스가 내장돼 있지 않으며, 테이블과 메타테이블을
-- 이용해 클래스를 만드는 다양한 방법이 있습니다.

-- 다음 예제에 대한 설명은 하단을 참조합니다.

Dog = {}                                   -- 1.

function Dog:new()                         -- 2.
  newObj = {sound = 'woof'}                -- 3.
  self.__index = self                      -- 4.
  return setmetatable(newObj, self)        -- 5.
end

function Dog:makeSound()                   -- 6.
  print('I say ' .. self.sound)
end

mrDog = Dog:new()                          -- 7.
mrDog:makeSound()  -- 'I say woof'         -- 8.

-- 1. Dog는 클래스처럼 동작합니다. 실제로는 테이블입니다.
-- 2. function 테이블명:fn(...)은
--    function 테이블명.fn(self, ...)과 같습니다.
--    :는 self라는 첫 번째 인자를 추가할 뿐입니다.
--    self가 값을 어떻게 얻는지 궁금하다면 아래의 7과 8을 읽어보세요.
-- 3. newObj는 Dog 클래스의 인스턴스가 됩니다.
-- 4. self = 인스턴스화되는 클래스.
--    주로 self = Dog이지만 상속을 이용하면 이것을 바꿀 수 있습니다. 
--    newObj의 메타테이블과 self의 __index를 모두 self에 설정하면
--    newObj가 self의 함수를 갖게 됩니다.
-- 5. 참고: setmetatable은 첫 번째 인자를 반환합니다.
-- 6. :는 2에서 설명한 것과 같이 동작하지만 이번에는 self가 
--    클래스가 아닌 인스턴스라고 예상할 수 있습니다.
-- 7. Dog.new(Dog)과 같으므로 new()에서는 self = Dog입니다.
-- 8. mrDog.makeSound(mrDog)과 같으므로 self = mrDog입니다.

----------------------------------------------------

-- 상속 예제:

LoudDog = Dog:new()                           -- 1.

function LoudDog:makeSound()
  s = self.sound .. ' '                       -- 2.
  print(s .. s .. s)
end

seymour = LoudDog:new()                       -- 3.
seymour:makeSound()  -- 'woof woof woof'      -- 4.

-- 1. LoudDog은 Dog의 메서드와 변수를 갖게 됩니다.
-- 2. self는 new()에서 'sound' 키를 가집니다. 3을 참고하세요.
-- 3. LoudDog.new(LoudDog)과 같고, LoudDog은 'new' 키가 없지만
--    메타테이블에서 __index = Dog이기 때문에 Dog.new(LoudDog)으로
--    변환됩니다.
--    결과: seymour의 메타테이블은 LoudDog이고 LoudDog.__index는
--    LoudDog입니다. 따라서 seymour.key는 seymour.key, 
--    LoudDog.key, Dog.key와 같을 것이며, 지정한 키에 어떤 테이블이
--    오든 상관없을 것입니다.
-- 4. 'makeSound' 키는 LoudDog에서 발견할 수 있습니다. 
--    이것은 LoudDog.makeSound(seymour)와 같습니다.

-- 필요할 경우, 하위 클래스의 new()는 기반 클래스의 new()와 유사합니다.
function LoudDog:new()
  newObj = {}
  -- newObj를 구성
  self.__index = self
  return setmetatable(newObj, self)
end

----------------------------------------------------
-- 4. 모듈
----------------------------------------------------


--[[ 여기서 주석을 제거하면 이 스크립트의 나머지 부분은 
--   실행 가능한 상태가 됩니다.
```

```lua
-- mod.lua 파일의 내용이 다음과 같다고 가정해 봅시다.
local M = {}

local function sayMyName()
  print('이소룡')
end

function M.sayHello()
  print('안녕하세요')
  sayMyName()
end

return M

-- 또 다른 파일에서는 mod.lua의 기능을 이용할 수 있습니다.
local mod = require('mod')  -- mod.lua 파일을 실행

-- require는 모듈을 포함시키는 표준화된 방법입니다.
-- require는 다음과 같이 동작합니다:     (캐싱돼 있지 않을 경우. 하단 참조)
-- mod.lua가 함수의 본문처럼 되므로 mod.lua 안의 지역 멤버는
-- 밖에서 볼 수 없습니다.

-- 다음 코드가 동작하는 것은 mod가 mod.lua의 M과 같기 때문입니다.
mod.sayHello()  -- 이소룡 씨에게 인사를 건넵니다.

-- 다음 코드를 실행하면 오류가 발생합니다.
-- sayMyName는 mod.lua 안에서만 존재하기 때문입니다:
mod.sayMyName()  -- 오류

-- require의 반환값은 캐싱되므로 require를 여러 번 실행해도
-- 파일은 최대 한 번만 실행됩니다.

-- mod2.lua에 "print('Hi')"가 들어 있다고 가정해 봅시다.
local a = require('mod2')  -- Hi!를 출력
local b = require('mod2')  -- print를 실행하지 않음. a=b

-- dofile은 require와 비슷하지만 캐싱을 하지 않습니다:
dofile('mod2')  --> Hi!
dofile('mod2')  --> Hi! (require와 달리 다시 한번 실행됨)

-- loadfile은 루아 파일을 읽어들이지만 실행하지는 않습니다
f = loadfile('mod2')  -- f()를 호출해야 mod2.lua가 실행됩니다.

-- loadstring은 문자열에 대한 loadfile입니다.
g = loadstring('print(343)')  -- 함수를 반환합니다.
g()  -- 343이 출력됩니다. 그전까지는 아무것도 출력되지 않습니다.

--]]

```

## 참고자료

루아를 배우는 일이 흥미진진했던 이유는 <a href="http://love2d.org/">Love 2D 게임 엔진</a>을 이용해 
게임을 만들 수 있었기 때문입니다. 이것이 제가 루아를 배운 이유입니다.

저는 <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">BlackBulletIV의 "프로그래머를 위한 루아"</a>로
시작했습니다. 그다음으로 공식 <a href="http://www.lua.org/pil/contents.html">"프로그래밍 루아"</a> 책을 읽었습니다.
그렇게 루아를 배웠습니다.

lua-users.org에 있는 <a href="http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">짧은 루아 레퍼런스</a>를
읽어두면 도움될지도 모르겠습니다.

여기서는 표준 라이브러리에 관해서는 다루지 않았습니다.

* <a href="http://lua-users.org/wiki/StringLibraryTutorial">string 라이브러리</a>
* <a href="http://lua-users.org/wiki/TableLibraryTutorial">table 라이브러리</a>
* <a href="http://lua-users.org/wiki/MathLibraryTutorial">math 라이브러리</a>
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io 라이브러리</a>
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os 라이브러리</a>

그나저나 이 파일 전체는 유효한 루아 프로그램입니다. 이 파일을
learn.lua로 저장한 후 "lua learn.lua"를 실행해 보세요!

이 글은 tylerneylon.com에 처음으로 써본 글이며, 
<a href="https://gist.github.com/tylerneylon/5853042">GitHub의 Gist</a>에서도 확인할 수 있습니다.
루아로 즐거운 시간을 보내세요!
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
    - ["Jacob Ward", "http://github.com/JacobCWard/"]
filename: markdown-kr.md
lang: ko-kr
---

마크다운은 2004년에 존 그루버가 창시했습니다. HTML으로 (그리고 이제는 다른 다양한 형식으로도) 쉽게 변환되는 읽고 쓰기 쉬운 문법입니다.

마크다운은 또한 파서마다 구현이 다양합니다. 본 문서는 어떤 기능이 보편적인지,
혹은 어떤 기능이 특정 파서에 종속되어 있는지 명확히 하고자 합니다.

- [HTML 요소](#html-elements)
- [제목](#headings)
- [간단한 텍스트 꾸미기](#simple-text-styles)
- [문단](#paragraphs)
- [목록](#lists)
- [코드](#code-blocks)
- [수평선](#horizontal-rule)
- [링크](#links)
- [이미지](#images)
- [기타](#miscellany)

## HTML 요소
HTML은 마크다운의 수퍼셋입니다. 모든 HTML 파일은 유효한 마크다운이라는 것입니다.
```markdown
<!--따라서 주석과 같은 HTML 요소들을 마크다운에 사용할 수 있으며, 마크다운 파서에 영향을
받지 않을 것입니다. 하지만 마크다운 파일에서 HTML 요소를 만든다면 그 요소의 안에서는
마크다운 문법을 사용할 수 없습니다.-->
```
## 제목

텍스트 앞에 붙이는 우물 정 기호(#)의 갯수에 따라 `<h1>`부터 `<h6>`까지의 HTML 요소를
손쉽게 작성할 수 있습니다.
```markdown
# <h1>입니다.
## <h2>입니다.
### <h3>입니다.
#### <h4>입니다.
##### <h5>입니다.
###### <h6>입니다.
```
또한 h1과 h2를 나타내는 다른 방법이 있습니다.
```markdown
h1입니다.
=============

h2입니다.
-------------
```
## 간단한 텍스트 꾸미기

마크다운으로 쉽게 텍스트를 기울이거나 굵게 할 수 있습니다.
```markdown
*기울인 텍스트입니다.*
_이 텍스트도 같습니다._

**굵은 텍스트입니다.**
__이 텍스트도 같습니다.__

***기울인 굵은 텍스트입니다.***
**_이 텍스트도 같습니다._**
*__이것도 같습니다.__*
```
깃헙 전용 마크다운에는 취소선도 있습니다.
```markdown
~~이 텍스트에는 취소선이 그려집니다.~~
```
## 문단

문단은 하나 이상의 빈 줄로 구분되는, 한 줄 이상의 인접한 텍스트입니다.

```markdown
문단입니다. 문단에 글을 쓰다니 재밌지 않나요?

이제 두 번째 문단입니다.
아직도 두 번째 문단입니다.

나는 세 번째 문단!
```
HTML `<br />` 태그를 삽입하고 싶으시다면, 두 개 이상의 띄어쓰기로 문단을 끝내고
새 문단을 시작할 수 있습니다.

```markdown
띄어쓰기 두 개로 끝나는 문단 (마우스로 긁어 보세요).  

이 위에는 `<br />` 태그가 있습니다.
```

인용문은 > 문자로 쉽게 쓸 수 있습니다.

```markdown
> 인용문입니다. 수동으로 개행하고서
> 줄마다 `>`를 칠 수도 있고 줄을 길게 쓴 다음에 저절로 개행되게 내버려 둘 수도 있습니다.
> `>`로 시작하기만 한다면 차이가 없습니다.

> 한 단계 이상의 들여쓰기를
>> 사용할 수도 있습니다.
> 깔끔하죠?
```

## 목록
순서가 없는 목록은 별표, 더하기, 하이픈을 이용해 만들 수 있습니다.
```markdown
* 이거
* 저거
* 그거
```

또는

```markdown
+ 이거
+ 저거
+ 그거
```

또는

```markdown
- 이거
- 저거
- 그거
```

순서가 있는 목록은 숫자와 마침표입니다.

```markdown
1. 하나
2. 둘
3. 셋
```

숫자를 정확히 붙이지 않더라도 제대로 된 순서로 보여주겠지만, 좋은 생각은 아닙니다.

```markdown
1. 하나
1. 둘
1. 셋
```
(위의 예시와 똑같이 나타납니다.)

목록 안에 목록이 올 수도 있습니다.

```markdown
1. 하나
2. 둘
3. 셋
    * 이거
    * 저거
4. 넷
```

심지어 할 일 목록도 있습니다. HTML 체크박스가 만들어집니다.

```markdown
x가 없는 박스들은 체크되지 않은 HTML 체크박스입니다.
- [ ] 첫 번째 할 일
- [ ] 두 번째 할 일
이 체크박스는 체크된 HTML 체크박스입니다.
- [x] 완료된 일
```

## 코드

띄어쓰기 네 개 혹은 탭 한 개로 줄을 들여씀으로서 (`<code> 요소를 사용하여`) 코드를
나타낼 수 있습니다.

```markdown
    puts "Hello, world!"
```

탭을 더 치거나 띄어쓰기를 네 번 더 함으로써 코드를 들여쓸 수 있습니다.

```markdown
    my_array.each do |item|
        puts item
    end
```

인라인 코드는 백틱 문자를 이용하여 나타냅니다. `

```markdown
철수는 `go_to()` 함수가 뭘 했는지도 몰랐어!
```

깃헙 전용 마크다운에서는 코드를 나타내기 위해 특별한 문법을 쓸 수 있습니다.

<pre>
<code class="highlight">&#x60;&#x60;&#x60;ruby
def foobar
    puts "Hello world!"
end
&#x60;&#x60;&#x60;</code></pre>

위의 경우에 들여쓰기가 필요없을 뿐 아니라 \`\`\` 뒤에 특정해 준 언어의 문법에 따라
색을 입혀줄 것입니다.

## 수평선

수평선(`<hr/>`)은 셋 이상의 별표나 하이픈을 이용해 쉽게 나타낼 수 있습니다.
띄어쓰기가 포함될 수 있습니다.
```markdown
***
---
- - -
****************
```
## 링크

마크다운의 장점 중 하나는 링크를 만들기 쉽다는 것입니다. 대괄호 안에 나타낼 텍스트를 쓰고
괄호 안에 URL을 쓰면 됩니다.

```markdown
[클릭](http://test.com/)
```

괄호 안에 따옴표를 이용해 링크에 제목을 달 수도 있습니다.

```markdown
[클릭](http://test.com/ "test.com으로 가기")
```

상대 경로도 유효합니다.

```markdown
[music으로 가기](/music/).
```

참조하는 식으로 링크를 걸 수도 있습니다.

<pre><code class="highlight">&#x5b;<span class="nv">이 </span>][<span class="ss">링크</span>]에서 더 알아보세요!
&#x5b;<span class="nv">원하신다면 </span>][<span class="ss">foobar</span>]도 참고하세요.

&#x5b;<span class="nv">링크</span>]: <span class="sx">http://test.com/</span> <span class="nn">"좋아!"</span>
&#x5b;<span class="nv">foobar</span>]: <span class="sx">http://foobar.biz/</span> <span class="nn">"됐다!"</span></code></pre>

제목은 작은 따옴표나 괄호에 들어갈 수도 있고, 완전히 생략할 수도 있습니다. 참조는 문서의
어느 곳에든 올 수 있고 참조 ID는 유일하다면 무엇이든 될 수 있습니다.

링크 텍스트를 ID로 사용하는 "묵시적 이름"도 있습니다.

<pre><code class="highlight">&#x5b;<span class="nv">이것</span>][]은 링크입니다.

&#x5b;<span class="nv">이것</span>]: <span class="sx">http://thisisalink.com/</span></code></pre>

하지만 보통 그렇게 추천하지는 않습니다.

## 이미지
이미지는 링크와 같지만 앞에 느낌표가 붙습니다.

```markdown
![이미지의 alt 속성](http://imgur.com/myimage.jpg "제목")
```

참조 방식도 가능합니다.

<pre><code class="highlight">!&#x5b;<span class="nv">alt 속성</span>][<span class="ss">이미지</span>]

&#x5b;<span class="nv">이미지</span>]: <span class="sx">relative/urls/cool/image.jpg</span> <span class="nn">"제목이 필요하다면 여기에"</span></code></pre>

## 기타
### 자동 링크

```markdown
<http://testwebsite.com/>와
[http://testwebsite.com/](http://testwebsite.com/)는 동일합니다.
```

### 이메일 자동 링크
```markdown
<foo@bar.com>
```
### 탈출 문자

```markdown
*별표 사이에 이 텍스트*를 치고 싶지만 기울이고 싶지는 않다면
이렇게 하시면 됩니다. \*별표 사이에 이 텍스트\*.
```

### 키보드 키

깃헙 전용 마크다운에서는 `<kbd>` 태그를 이용해 키보드 키를 나타낼 수 있습니다.

```markdown
컴퓨터가 멈췄다면 눌러보세요.
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>
```

### 표

표는 깃헙 전용 마크다운에서만 쓸 수 있고 다소 복잡하지만, 정말 쓰고 싶으시다면
```markdown
| 1열      | 2열        | 3열       |
| :--------| :-------: | --------: |
| 왼쪽 정렬 | 가운데 정렬 | 오른쪽 정렬 |
| 머시기    | 머시기     | 머시기     |
```
혹은
```markdown
1열 | 2열 | 3열
:-- | :-: | --:
으악 너무 못생겼어 | 그만 | 둬
```
---
추가 정보를 위해, 존 그루버의 공식 문법 [(영어) 문서](http://daringfireball.net/projects/markdown/syntax)와 애덤 프릿차드의 훌륭한 [(영어) 치트싯](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)을 확인하세요.
---
language: PHP
category: language
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
filename: learnphp-kr.php
translators:
    - ["wikibook", "http://wikibook.co.kr"]
lang: ko-kr
---

이 문서에서는 PHP 5+를 설명합니다.


```php
<?php // PHP 코드는 반드시 <?php 태그로 감싸야 합니다.

// php 파일에 PHP 코드만 들어 있다면 닫는 태그를 생략하는 것이 관례입니다.

// 슬래시 두 개는 한 줄 주석을 의미합니다.

# 해시(파운드 기호로도 알려진)도 같은 역할을 하지만 //이 더 일반적으로 쓰입니다.

/*
     텍스트를 슬래시-별표와 별표-슬래시로 감싸면 
     여러 줄 주석이 만들어집니다.
*/

// 출력결과를 표시하려면 "echo"나 "print"를 사용합니다.
print('Hello '); // 줄바꿈 없이 "Hello "를 출력합니다.

// ()는 print와 echo를 사용할 때 선택적으로 사용할 수 있습니다.
echo "World\n"; // "World"를 출력한 후 줄바꿈합니다.
// (모든 구문은 반드시 세미콜론으로 끝나야 합니다.)

// <?php 태그 밖의 내용은 모두 자동으로 출력됩니다.
?>
Hello World Again!
<?php


/************************************
 * 타입과 변수
 */

// 변수명은 $ 기호로 시작합니다.
// 유효한 변수명은 문자나 밑줄(_)로 시작하고,
// 이어서 임의 개수의 숫자나 문자, 밑줄이 옵니다.

// 불린값은 대소문자를 구분합니다.
$boolean = true;  // 또는 TRUE나 True
$boolean = false; // 또는 FALSE나 False

// Integer
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (a leading 0 denotes an octal number)
$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal)

// Float (doubles로도 알려짐)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// 산술 연산
$sum        = 1 + 1; // 2
$difference = 2 - 1; // 1
$product    = 2 * 2; // 4
$quotient   = 2 / 1; // 2

// 축약형 산술 연산
$number = 0;
$number += 1;      // $number를 1만큼 증가
echo $number++;    // 1을 출력(평가 후 증가)
echo ++$number;    // 3 (평가 전 증가)
$number /= $float; // 나눗셈 후 몫을 $number에 할당

// 문자열은 작은따옴표로 감싸야 합니다.
$sgl_quotes = '$String'; // => '$String'

// 다른 변수를 포함할 때를 제외하면 큰따옴표 사용을 자제합니다.
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// 특수 문자는 큰따옴표에서만 이스케이프됩니다.
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// 필요할 경우 변수를 중괄호로 감쌉니다.
$money = "I have $${number} in the bank.";

// PHP 5.3부터는 여러 줄 문자열을 생성하는 데 나우닥(nowdoc)을 사용할 수 있습니다.
$nowdoc = <<<'END'
Multi line
string
END;

// 히어닥(heredoc)에서는 문자열 치환을 지원합니다.
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// 문자열을 연결할 때는 .을 이용합니다.
echo 'This string ' . 'is concatenated';


/********************************
 * 상수
 */
 
// 상수는 define()을 이용해 정의되며,
// 런타임 동안 절대 변경될 수 없습니다!

// 유효한 상수명은 문자나 밑줄로 시작하고,
// 이어서 임의 개수의 숫자나 문자, 밑줄이 옵니다.
define("FOO",     "something");

// 상수명을 이용해 직접 상수에 접근할 수 있습니다.
echo 'This outputs '.FOO;


/********************************
 * 배열
 */

// PHP의 모든 배열은 연관 배열(associative array, 해시맵)입니다.

// 일부 언어에서 해시맵으로도 알려진 연관 배열은

// 모든 PHP 버전에서 동작합니다.
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);

// PHP 5.4에서는 새로운 문법이 도입됐습니다.
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];

echo $associative['One']; // 1을 출력

// 리스트 리터럴은 암시적으로 정수형 키를 할당합니다.
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"


/********************************
 * 출력
 */

echo('Hello World!');
// 표준출력(stdout)에 Hello World!를 출력합니다.
// 브라우저에서 실행할 경우 표준출력은 웹 페이지입니다.

print('Hello World!'); // echo과 동일

// echo는 실제로 언어 구성물에 해당하므로, 괄호를 생략할 수 있습니다.
echo 'Hello World!';
print 'Hello World!'; // 똑같이 출력됩니다.

$paragraph = 'paragraph';

echo 100;        // 스칼라 변수는 곧바로 출력합니다.
echo $paragraph; // 또는 변수의 값을 출력합니다.

// 축약형 여는 태그를 설정하거나 PHP 버전이 5.4.0 이상이면
// 축약된 echo 문법을 사용할 수 있습니다.
?>
<p><?= $paragraph ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // 이제 $x의 값은 $y의 값과 같습니다.
$z = &$y;
// $z는 이제 $y에 대한 참조를 담고 있습니다. $z의 값을 변경하면
// $y의 값도 함께 변경되며, 그 반대도 마찬가지입니다.
// $x는 $y의 원래 값을 그대로 유지합니다.

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0


/********************************
 * 로직
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// assert는 인자가 참이 아닌 경우 경고를 출력합니다.

// 다음과 같은 비교는 항상 참이며, 타입이 같지 않더라도 마찬가지입니다.
assert($a == $b); // 동일성 검사
assert($c != $a); // 불일치성 검사
assert($c <> $a); // 또 다른 불일치성 검사
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// 다음과 같은 코드는 값과 타입이 모두 일치하는 경우에만 참입니다.
assert($c === $d);
assert($a !== $d);
assert(1 == '1');
assert(1 !== '1');

// 변수는 어떻게 사용하느냐 따라 다른 타입으로 변환될 수 있습니다.

$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2 (문자열이 강제로 정수로 변환됩니다)

$string = 'one';
echo $string + $string; // => 0
// + 연산자는 'one'이라는 문자열을 숫자로 형변환할 수 없기 때문에 0이 출력됩니다.

// 한 변수를 다른 타입으로 처리하는 데 형변환을 사용할 수 있습니다.

$boolean = (boolean) 1; // => true

$zero = 0;
$boolean = (boolean) $zero; // => false

// 대다수의 타입을 형변환하는 데 사용하는 전용 함수도 있습니다.
$integer = 5;
$string = strval($integer);

$var = null; // 널 타입


/********************************
 * 제어 구조
 */

if (true) {
    print 'I get printed';
}

if (false) {
    print 'I don\'t';
} else {
    print 'I get printed';
}

if (false) {
    print 'Does not get printed';
} elseif(true) {
    print 'Does';
}

// 사항 연산자
print (false ? 'Does not get printed' : 'Does');

$x = 0;
if ($x === '0') {
    print 'Does not print';
} elseif($x == '1') {
    print 'Does not print';
} else {
    print 'Does print';
}



// 다음과 같은 문법은 템플릿에 유용합니다.
?>

<?php if ($x): ?>
This is displayed if the test is truthy.
<?php else: ?>
This is displayed otherwise.
<?php endif; ?>

<?php

// 특정 로직을 표현할 때는 switch를 사용합니다.
switch ($x) {
    case '0':
        print 'Switch does type coercion';
        break; // break을 반드시 포함해야 하며, break를 생략하면
               // 'two'와 'three' 케이스로 넘어갑니다.
    case 'two':
    case 'three':
        // 변수가 'two'나 'three'인 경우에 실행될 코드를 작성합니다.
        break;
    default:
        // 기본값으로 실행될 코드를 작성
}

// while과 do...while, for 문이 아마 더 친숙할 것입니다.
$i = 0;
while ($i < 5) {
    echo $i++;
}; // "01234"를 출력

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // "01234"를 출력

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // "0123456789"를 출력

echo "\n";

$wheels = ['bicycle' => 2, 'car' => 4];

// foreach 문은 배영를 순회할 수 있습니다.
foreach ($wheels as $wheel_count) {
    echo $wheel_count;
} // "24"를 출력

echo "\n";

// 키와 값을 동시에 순회할 수 있습니다.
foreach ($wheels as $vehicle => $wheel_count) {
    echo "A $vehicle has $wheel_count wheels";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // while 문을 빠져나옴
    }
    echo $i++;
} // "012"를 출력

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // 이번 순회를 생략
    }
    echo $i;
} // "0124"를 출력


/********************************
 * 함수
 */

// "function"으로 함수를 정의합니다.
function my_function () {
  return 'Hello';
}

echo my_function(); // => "Hello"

// 유효한 함수명은 문자나 밑줄로 시작하고, 이어서 
// 임의 개수의 문자나 숫자, 밑줄이 옵니다.

function add ($x, $y = 1) { // $y는 선택사항이고 기본값은 1입니다.
  $result = $x + $y;
  return $result;
}

echo add(4); // => 5
echo add(4, 2); // => 6

// 함수 밖에서는 $result에 접근할 수 없습니다.
// print $result; // 이 코드를 실행하면 경고가 출력됩니다.

// PHP 5.3부터는 익명 함수를 선언할 수 있습니다.
$inc = function ($x) {
  return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
  echo "$x - $y - $z";
}

// 함수에서는 함수를 반환할 수 있습니다.
function bar ($x, $y) {
  // 'use'를 이용해 바깥 함수의 변수를 전달합니다.
  return function ($z) use ($x, $y) {
    foo($x, $y, $z);
  };
}

$bar = bar('A', 'B');
$bar('C'); // "A - B - C"를 출력

// 문자열을 이용해 이름이 지정된 함수를 호출할 수 있습니다.
$function_name = 'add';
echo $function_name(1, 2); // => 3
// 프로그램 방식으로 어느 함수를 실행할지 결정할 때 유용합니다.
// 아니면 call_user_func(callable $callback [, $parameter [, ... ]]);를 사용해도 됩니다.

/********************************
 * 인클루드
 */

<?php
// 인클루드된 파일 내의 PHP 코드도 반드시 PHP 여는 태그로 시작해야 합니다.

include 'my-file.php';
// my-file.php 안의 코드는 이제 현재 유효범위에서 이용할 수 있습니다.
// 파일을 인클루드할 수 없으면(예: 파일을 찾을 수 없음) 경고가 출력됩니다.

include_once 'my-file.php';
// my-file.php 안의 코드가 다른 곳에 인클루드됐다면 다시 인클루드되지는 않습니다.
// 따라서 클래스 선언이 여러 번 되어 발생하는 문제가 일어나지 않습니다.

require 'my-file.php';
require_once 'my-file.php';
// require()는 include()와 같지만 파일을 인클루드할 수 없을 경우
// 치명적인 오류가 발생한다는 점이 다릅니다.

// my-include.php의 내용
<?php

return 'Anything you like.';
// 파일의 끝

// include와 require는 값을 반환할 수도 있습니다.
$value = include 'my-include.php';

// 파일은 지정된 파일 경로를 토대로 인클루드되거나, 혹은 아무것도 명시하지 않은 경우
// include_path라는 설정 지시지를 따릅니다. include_path에서 파일을 발견할 수 없으면
// include는 마지막으로 실패하기 전에 호출 스크립트 자체의 디렉터리와 현재 작업 디렉터리를 확인합니다.
/* */

/********************************
 * 클래스
 */

// 클래스는 class라는 키워드로 정의합니다.

class MyClass
{
    const MY_CONST      = 'value'; // 상수

    static $staticVar   = 'static';

    // 프로퍼티에는 반드시 가시성을 선언해야 합니다.
    public $property    = 'public';
    public $instanceProp;
    protected $prot = 'protected'; // 이 클래스와 하위 클래스에서 접근할 수 있음
    private $priv   = 'private';   // 이 클래스 내에서만 접근 가능

    // __construct로 생성자를 만듭니다.
    public function __construct($instanceProp) {
        // $this로 인스턴스 변수에 접근합니다.
        $this->instanceProp = $instanceProp;
    }

    // 메서드는 클래스 안의 함수로서 선언됩니다.
    public function myMethod()
    {
        print 'MyClass';
    }

    final function youCannotOverrideMe()
    {
    }

    public static function myStaticMethod()
    {
        print 'I am static';
    }
}

echo MyClass::MY_CONST;    // 'value' 출력
echo MyClass::$staticVar;  // 'static' 출력
MyClass::myStaticMethod(); // 'I am static' 출력

// new를 사용해 클래스를 인스턴스화합니다.
$my_class = new MyClass('An instance property');
// 인자를 전달하지 않을 경우 괄호를 생략할 수 있습니다.

// ->를 이용해 클래스 멤버에 접근합니다
echo $my_class->property;     // => "public"
echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod();        // => "MyClass"


// "extends"를 이용해 클래스를 확장합니다.
class MyOtherClass extends MyClass
{
    function printProtectedProperty()
    {
        echo $this->prot;
    }

    // 메서드 재정의
    function myMethod()
    {
        parent::myMethod();
        print ' > MyOtherClass';
    }
}

$my_other_class = new MyOtherClass('Instance prop');
$my_other_class->printProtectedProperty(); // => "protected" 출력
$my_other_class->myMethod();               // "MyClass > MyOtherClass" 출력

final class YouCannotExtendMe
{
}

// "마법 메서드(magic method)"로 설정자 메서드와 접근자 메서드를 만들 수 있습니다.
class MyMapClass
{
    private $property;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MyMapClass();
echo $x->property; // __get() 메서드를 사용
$x->property = 'Something'; // __set() 메서드를 사용

// 클래스는 추상화하거나(abstract 키워드를 사용해) 
// 인터페이스를 구현할 수 있습니다(implments 키워드를 사용해).
// 인터페이스는 interface 키워드로 선언합니다.

interface InterfaceOne
{
    public function doSomething();
}

interface InterfaceTwo
{
    public function doSomethingElse();
}

// 인터페이스는 확장할 수 있습니다.
interface InterfaceThree extends InterfaceTwo
{
    public function doAnotherContract();
}

abstract class MyAbstractClass implements InterfaceOne
{
    public $x = 'doSomething';
}

class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
{
    public function doSomething()
    {
        echo $x;
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


// 클래스에서는 하나 이상의 인터페이스를 구현할 수 있습니다.
class SomeOtherClass implements InterfaceOne, InterfaceTwo
{
    public function doSomething()
    {
        echo 'doSomething';
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


/********************************
 * 특성
 */

// 특성(trait)은 PHP 5.4.0부터 사용 가능하며, "trait"으로 선언합니다.

trait MyTrait
{
    public function myTraitMethod()
    {
        print 'I have MyTrait';
    }
}

class MyTraitfulClass
{
    use MyTrait;
}

$cls = new MyTraitfulClass();
$cls->myTraitMethod(); // "I have MyTrait"을 출력


/********************************
 * 네임스페이스
 */

// 이 부분은 별도의 영역인데, 파일에서 처음으로 나타나는 문장은 
// 네임스페이스 선언이어야 하기 때문입니다. 여기서는 그런 경우가 아니라고 가정합니다.

<?php

// 기본적으로 클래스는 전역 네임스페이스에 존재하며,
// 백슬래시를 이용해 명시적으로 호출할 수 있습니다.

$cls = new \MyClass();



// 파일에 대한 네임스페이스를 설정합니다.
namespace My\Namespace;

class MyClass
{
}

// (다른 파일에 들어 있는 코드)
$cls = new My\Namespace\MyClass;

// 또는 다른 네임스페이스 내에서 접근하는 경우
namespace My\Other\Namespace;

use My\Namespace\MyClass;

$cls = new MyClass();

// 혹은 네임스페이스에 별칭을 붙일 수도 있습니다.

namespace My\Other\Namespace;

use My\Namespace as SomeOtherNamespace;

$cls = new SomeOtherNamespace\MyClass();

*/

```

## 더 자세한 정보

레퍼런스와 커뮤니티 관련 내용은 [공식 PHP 문서](http://www.php.net/manual/)를 참고하세요.

최신 모범 사례에 관심이 있다면 [PHP The Right Way](http://www.phptherightway.com/)를 참고하세요.

PHP를 익히기 전에 다른 훌륭한 패키지 관리자를 지원하는 언어를 사용해본 적이 있다면 [컴포저(Composer)](http://getcomposer.org/)를 확인해 보세요.

공통 표준이 궁금하다면 PHP 프레임워크 상호운용성 그룹의 [PSR 표준](https://github.com/php-fig/fig-standards)을 참고하세요.
---
language: python
category: language
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
filename: learnpython-ko.py
translators:
    - ["wikibook", "http://wikibook.co.kr"]
lang: ko-kr
---

파이썬은 귀도 반 로섬이 90년대에 만들었습니다. 파이썬은 현존하는 널리 사용되는 언어 중 하나입니다.
저는 문법적 명료함에 반해 파이썬을 사랑하게 됐습니다. 파이썬은 기본적으로 실행 가능한 의사코드입니다.

피드백 주시면 정말 감사하겠습니다! [@louiedinh](http://twitter.com/louiedinh)나
louiedinh [at] [구글의 이메일 서비스]를 통해 저에게 연락하시면 됩니다.

참고: 이 글은 구체적으로 파이썬 2.7에 해당하는 내용을 담고 있습니다만
파이썬 2.x에도 적용할 수 있을 것입니다. 파이썬 3을 다룬 튜토리얼도 곧 나올 테니 기대하세요!

```python
# 한 줄짜리 주석은 해시로 시작합니다.
""" 여러 줄 문자열은 "를 세 개 써서 시작할 수 있고,
    주석으로 자주 사용됩니다.
"""

####################################################
## 1. 기본 자료형과 연산자
####################################################

# 숫자
3 #=> 3

# 수학 연산은 예상하신 대로입니다.
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# 나눗셈은 약간 까다롭습니다. 정수로 나눈 다음 결과값을 자동으로 내림합니다.
5 / 2 #=> 2

# 나눗셈 문제를 해결하려면 float에 대해 알아야 합니다.
2.0     # 이것이 float입니다.
11.0 / 4.0 #=> 2.75 훨씬 낫네요

# 괄호를 이용해 연산자 우선순위를 지정합니다.
(1 + 3) * 2 #=> 8

# 불린(Boolean) 값은 기본형입니다.
True
False

# not을 이용해 부정합니다.
not True #=> False
not False #=> True

# 동일성 연산자는 ==입니다.
1 == 1 #=> True
2 == 1 #=> False

# 불일치 연산자는 !=입니다.
1 != 1 #=> False
2 != 1 #=> True

# 그밖의 비교 연산자는 다음과 같습니다.
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# 비교 연산을 연결할 수도 있습니다!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# 문자열은 "나 '로 생성합니다.
"This is a string."
'This is also a string.'

# 문자열도 연결할 수 있습니다!
"Hello " + "world!" #=> "Hello world!"

# 문자열은 문자로 구성된 리스트로 간주할 수 있습니다.
"This is a string"[0] #=> 'T'

# %는 다음과 같이 문자열을 형식화하는 데 사용할 수 있습니다:
"%s can be %s" % ("strings", "interpolated")

# 문자열을 형식화하는 새로운 방법은 format 메서드를 이용하는 것입니다.
# 이 메서드를 이용하는 방법이 더 선호됩니다.
"{0} can be {1}".format("strings", "formatted")
# 자릿수를 세기 싫다면 키워드를 이용해도 됩니다.
"{name} wants to eat {food}".format(name="Bob", food="lasagna")

# None은 객체입니다.
None #=> None

# 객체와 None을 비교할 때는 동일성 연산자인 `==`를 사용해서는 안 됩니다.
# 대신 `is`를 사용하세요.
"etc" is None #=> False
None is None  #=> True

# 'is' 연산자는 객체의 식별자를 검사합니다.
# 기본형 값을 다룰 때는 이 연산자가 그다지 유용하지 않지만
# 객체를 다룰 때는 매우 유용합니다.

# None, 0, 빈 문자열/리스트는 모두 False로 평가됩니다.
# 그밖의 다른 값은 모두 True입니다
0 == False  #=> True
"" == False #=> True


####################################################
## 2. 변수와 컬렉션
####################################################

# 뭔가를 출력하는 것은 상당히 쉽습니다.
print "I'm Python. Nice to meet you!"


# 변수에 값을 할당하기 전에 변수를 반드시 선언하지 않아도 됩니다.
some_var = 5    # 명명관례는 '밑줄이_포함된_소문자'입니다.
some_var #=> 5

# 미할당된 변수에 접근하면 예외가 발생합니다.
# 예외 처리에 관해서는 '제어 흐름'을 참고하세요.
some_other_var  # 이름 오류가 발생

# 표현식으로도 사용할 수 있습니다.
"yahoo!" if 3 > 2 else 2 #=> "yahoo!"

# 리스트는 순차 항목을 저장합니다.
li = []
# 미리 채워진 리스트로 시작할 수도 있습니다.
other_li = [4, 5, 6]

# append를 이용해 리스트 끝에 항목을 추가합니다.
li.append(1)    #li는 이제 [1]입니다.
li.append(2)    #li는 이제 [1, 2]입니다.
li.append(4)    #li는 이제 [1, 2, 4]입니다.
li.append(3)    #li는 이제 [1, 2, 4, 3]입니다.
# pop을 이용해 끝에서부터 항목을 제거합니다.
li.pop()        #=> 3이 반환되고 li는 이제 [1, 2, 4]입니다.
# 다시 넣어봅시다
li.append(3)    # li는 이제 다시 [1, 2, 4, 3]가 됩니다.

# 배열에서 했던 것처럼 리스트에도 접근할 수 있습니다.
li[0] #=> 1
# 마지막 요소를 봅시다.
li[-1] #=> 3

# 범위를 벗어나서 접근하면 IndexError가 발생합니다.
li[4] # IndexError가 발생

# 슬라이스 문법을 통해 범위를 지정해서 값을 조회할 수 있습니다.
# (이 문법을 통해 간편하게 범위를 지정할 수 있습니다.)
li[1:3] #=> [2, 4]
# 앞부분을 생략합니다.
li[2:] #=> [4, 3]
# 끝부분을 생략합니다.
li[:3] #=> [1, 2, 4]

# del로 임의의 요소를 제거할 수 있습니다.
del li[2] # li is now [1, 2, 3]

# 리스트를 추가할 수도 있습니다.
li + other_li #=> [1, 2, 3, 4, 5, 6] - 참고: li와 other_li는 그대로 유지됩니다.

# extend로 리스트를 연결합니다.
li.extend(other_li) # 이제 li는 [1, 2, 3, 4, 5, 6]입니다.

# in으로 리스트 안에서 특정 요소가 존재하는지 확인합니다.
1 in li #=> True

# len으로 길이를 검사합니다.
len(li) #=> 6

# 튜플은 리스트와 비슷하지만 불변성을 띱니다.
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # TypeError가 발생

# 튜플에 대해서도 리스트에서 할 수 있는 일들을 모두 할 수 있습니다.
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# 튜플(또는 리스트)을 변수로 풀 수 있습니다.
a, b, c = (1, 2, 3)     # 이제 a는 1, b는 2, c는 3입니다
# 괄호를 빼면 기본적으로 튜플이 만들어집니다.
d, e, f = 4, 5, 6
# 이제 두 값을 바꾸는 게 얼마나 쉬운지 확인해 보세요.
e, d = d, e     # 이제 d는 5이고 e는 4입니다.

# 딕셔너리는 매핑을 저장합니다.
empty_dict = {}
# 다음은 값을 미리 채운 딕셔너리입니다.
filled_dict = {"one": 1, "two": 2, "three": 3}

# []를 이용해 값을 조회합니다.
filled_dict["one"] #=> 1

# 모든 키를 리스트로 구합니다.
filled_dict.keys() #=> ["three", "two", "one"]
# 참고 - 딕셔너리 키의 순서는 보장되지 않습니다.
# 따라서 결과가 이와 정확히 일치하지 않을 수도 있습니다.

# 모든 값을 리스트로 구합니다.
filled_dict.values() #=> [3, 2, 1]
# 참고 - 키 순서와 관련해서 위에서 설명한 내용과 같습니다.

# in으로 딕셔너리 안에 특정 키가 존재하는지 확인합니다.
"one" in filled_dict #=> True
1 in filled_dict #=> False

# 존재하지 않는 키를 조회하면 KeyError가 발생합니다.
filled_dict["four"] # KeyError

# get 메서드를 이용하면 KeyError가 발생하지 않습니다.
filled_dict.get("one") #=> 1
filled_dict.get("four") #=> None
# get 메서드는 값이 누락된 경우 기본 인자를 지원합니다.
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4

# setdefault 메서드는 딕셔너리에 새 키-값 쌍을 추가하는 안전한 방법입니다.
filled_dict.setdefault("five", 5) #filled_dict["five"]는 5로 설정됩니다.
filled_dict.setdefault("five", 6) #filled_dict["five"]는 여전히 5입니다.


# 세트는 집합을 저장합니다.
empty_set = set()
# 다수의 값으로 세트를 초기화합니다.
some_set = set([1,2,2,3,4]) # 이제 some_set는 set([1, 2, 3, 4])입니다.

# 파이썬 2.7부터는 {}를 세트를 선언하는 데 사용할 수 있습니다.
filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}

# 세트에 항목을 추가합니다.
filled_set.add(5) # 이제 filled_set는 {1, 2, 3, 4, 5}입니다.

# &을 이용해 교집합을 만듭니다.
other_set = {3, 4, 5, 6}
filled_set & other_set #=> {3, 4, 5}

# |를 이용해 합집합을 만듭니다.
filled_set | other_set #=> {1, 2, 3, 4, 5, 6}

# -를 이용해 차집합을 만듭니다.
{1,2,3,4} - {2,3,5} #=> {1, 4}

# in으로 세트 안에 특정 요소가 존재하는지 검사합니다.
2 in filled_set #=> True
10 in filled_set #=> False


####################################################
## 3. 제어 흐름
####################################################

# 변수를 만들어 봅시다.
some_var = 5

# 다음은 if 문입니다. 파이썬에서는 들여쓰기가 대단히 중요합니다!
# 다음 코드를 실행하면 "some_var is smaller than 10"가 출력됩니다.
if some_var > 10:
    print "some_var is totally bigger than 10."
elif some_var < 10:    # elif 절은 선택사항입니다.
    print "some_var is smaller than 10."
else:           # 이 부분 역시 선택사항입니다.
    print "some_var is indeed 10."


"""
for 루프는 리스트를 순회합니다.
아래 코드는 다음과 같은 내용을 출력합니다:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # %로 형식화된 문자열에 값을 채워넣을 수 있습니다.
    print "%s is a mammal" % animal
    
"""
`range(number)`는 숫자 리스트를 반환합니다.
이때 숫자 리스트의 범위는 0에서 지정한 숫자까지입니다.
아래 코드는 다음과 같은 내용을 출력합니다:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
while 루프는 조건이 더는 충족되지 않을 때까지 진행됩니다.
prints:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # x = x + 1의 축약형

# try/except 블록을 이용한 예외 처리

# 파이썬 2.6 및 상위 버전에서 동작하는 코드
try:
    # raise를 이용해 오류를 발생시킵니다
    raise IndexError("This is an index error")
except IndexError as e:
    pass    # pass는 단순 no-op 연산입니다. 보통 이곳에 복구 코드를 작성합니다.


####################################################
## 4. 함수
####################################################

# 새 함수를 만들 때 def를 사용합니다.
def add(x, y):
    print "x is %s and y is %s" % (x, y)
    return x + y    # return 문을 이용해 값을 반환합니다.

# 매개변수를 전달하면서 함수를 호출
add(5, 6) #=> "x is 5 and y is 6"가 출력되고 11이 반환됨

# 함수를 호출하는 또 다른 방법은 키워드 인자를 지정하는 방법입니다.
add(y=6, x=5)   # 키워드 인자는 순서에 구애받지 않습니다.

# 위치 기반 인자를 임의 개수만큼 받는 함수를 정의할 수 있습니다.
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# 키워드 인자를 임의 개수만큼 받는 함수 또한 정의할 수 있습니다.
def keyword_args(**kwargs):
    return kwargs

# 이 함수를 호출해서 어떤 일이 일어나는지 확인해 봅시다.
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# 원한다면 한 번에 두 가지 종류의 인자를 모두 받는 함수를 정의할 수도 있습니다.
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4)를 실행하면 다음과 같은 내용이 출력됩니다:
    (1, 2)
    {"a": 3, "b": 4}
"""

# 함수를 호출할 때 varargs/kwargs와 반대되는 일을 할 수 있습니다!
# *를 이용해 튜플을 확장하고 **를 이용해 kwargs를 확장합니다.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # foo(1, 2, 3, 4)와 같음
all_the_args(**kwargs) # foo(a=3, b=4)와 같음
all_the_args(*args, **kwargs) # foo(1, 2, 3, 4, a=3, b=4)와 같음

# 파이썬에는 일급 함수가 있습니다
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# 게다가 익명 함수도 있습니다.
(lambda x: x > 2)(3) #=> True

# 내장된 고차 함수(high order function)도 있습니다.
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# 맵과 필터에 리스트 조건 제시법(list comprehensions)을 사용할 수 있습니다.
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. 클래스
####################################################

# 클래스를 하나 만들기 위해 특정 객체의 하위 클래스를 만들 수 있습니다.
class Human(object):

    # 클래스 속성은 이 클래스의 모든 인스턴스에서 공유합니다.
    species = "H. sapiens"

    # 기본 초기화자
    def __init__(self, name):
        # 인자를 인스턴스의 name 속성에 할당합니다.
        self.name = name

    # 모든 인스턴스 메서드에서는 self를 첫 번째 인자로 받습니다.
    def say(self, msg):
       return "%s: %s" % (self.name, msg)

    # 클래스 메서드는 모든 인스턴스에서 공유합니다.
    # 클래스 메서드는 호출하는 클래스를 첫 번째 인자로 호출됩니다.
    @classmethod
    def get_species(cls):
        return cls.species

    # 정적 메서드는 클래스나 인스턴스 참조 없이도 호출할 수 있습니다.
    @staticmethod
    def grunt():
        return "*grunt*"


# 클래스 인스턴스화
i = Human(name="Ian")
print i.say("hi")     # "Ian: hi"가 출력됨

j = Human("Joel")
print j.say("hello")  # "Joel: hello"가 출력됨

# 클래스 메서드를 호출
i.get_species() #=> "H. sapiens"

# 공유 속성을 변경
Human.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# 정적 메서드를 호출
Human.grunt() #=> "*grunt*"


####################################################
## 6. 모듈
####################################################

# 다음과 같이 모듈을 임포트할 수 있습니다.
import math
print math.sqrt(16) #=> 4

# 모듈의 특정 함수를 호출할 수 있습니다.
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# 모듈의 모든 함수를 임포트할 수 있습니다.
# Warning: this is not recommended
from math import *

# 모듈 이름을 축약해서 쓸 수 있습니다.
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# 파이썬 모듈은 평범한 파이썬 파일에 불과합니다.
# 직접 모듈을 작성해서 그것들을 임포트할 수 있습니다. 
# 모듈의 이름은 파일의 이름과 같습니다.

# 다음과 같은 코드로 모듈을 구성하는 함수와 속성을 확인할 수 있습니다.
import math
dir(math)


```

## 더 배울 준비가 되셨습니까?

### 무료 온라인 참고자료

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)

### 파이썬 관련 도서

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
---

language: racket
filename: learnracket-kr.rkt
contributors:
  - ["th3rac25", "https://github.com/voila"]
  - ["Eli Barzilay", "https://github.com/elibarzilay"]
  - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
  - ["Duong H. Nguyen", "https://github.com/cmpitg"]
translators:
  - ["KIM Taegyoon", "https://github.com/kimtg"]
lang: ko-kr
---

Racket 은 Lisp/Scheme 계열의 일반 목적의, 다중 패러다임 프로그래밍 언어이다.

```racket
#lang racket ; 우리가 사용하는 언어를 정의한다.

;;; 주석

;; 한 줄 주석은 세미콜론으로 시작한다.

#| 블록 주석
   은 여러 줄에 걸칠 수 있으며...
    #|
       중첩될 수 있다!
    |#
|#

;; S-expression 주석은 아래 식을 버리므로,
;; 디버깅할 때 식을 주석화할 때 유용하다.
#; (이 식은 버려짐)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. 근본 자료형과 연산자
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 숫자
9999999999999999999999 ; 정수
#b111                  ; 이진수 => 7
#o111                  ; 팔진수 => 73
#x111                  ; 16진수 => 273
3.14                   ; 실수
6.02e+23
1/2                    ; 분수
1+2i                   ; 복소수

;; 함수 적용은 이렇게 쓴다: (f x y z ...)
;; 여기에서 f는 함수이고 x, y, z는 피연산자이다.
;; 글자 그대로의 데이터 리스트를 만들고 싶다면 평가를 막기 위해 '를 쓰시오.
'(+ 1 2) ; => (+ 1 2)
;; 이제, 산술 연산 몇 개
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(expt 2 3) ; => 8
(quotient 5 2) ; => 2
(remainder 5 2) ; => 1
(/ 35 5) ; => 7
(/ 1 3) ; => 1/3
(exact->inexact 1/3) ; => 0.3333333333333333
(+ 1+2i  2-3i) ; => 3-1i

;;; 불린
#t ; 참
#f ; 거짓 -- #f가 아닌 것은 참
(not #t) ; => #f
(and 0 #f (error "doesn't get here")) ; => #f
(or #f 0 (error "doesn't get here"))  ; => 0

;;; 문자
#\A ; => #\A
#\λ ; => #\λ
#\u03BB ; => #\λ

;;; 문자열은 고정 길이의 문자 배열이다.
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; 백슬래시는 탈출 문자이다.
"Foo\tbar\41\x21\u0021\a\r\n" ; C 탈출 문자, 유니코드 포함
"λx:(μα.α→α).xx"              ; 유니코드 문자 포함 가능

;; 문자열은 붙여질 수 있다!
(string-append "Hello " "world!") ; => "Hello world!"

;; 문자열은 문자의 리스트처럼 취급될 수 있다.
(string-ref "Apple" 0) ; => #\A

;; format은 문자열을 형식화하기 위해 사용된다:
(format "~a can be ~a" "strings" "formatted")

;; 인쇄는 쉽다.
(printf "I'm Racket. Nice to meet you!\n")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. 변수
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; define으로 변수를 만든다.
;; 변수명으로 다음 문자를 사용할 수 없다: ()[]{}",'`;#|\
(define some-var 5)
some-var ; => 5

;; 유니코드 문자도 사용 가능하다.
(define ⊆ subset?)
(⊆ (set 3 2) (set 1 2 3)) ; => #t

;; 앞에서 정의되지 않은 변수에 접근하면 예외가 발생한다.
; x ; => x: undefined ...

;; 지역 변수: `me'는 (let ...) 안에서만 "Bob"이다.
(let ([me "Bob"])
  "Alice"
  me) ; => "Bob"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. 구조체(Struct)와 모음(Collection)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 구조체
(struct dog (name breed age))
(define my-pet
  (dog "lassie" "collie" 5))
my-pet ; => #<dog>
(dog? my-pet) ; => #t
(dog-name my-pet) ; => "lassie"

;;; 쌍 (불변)
;; `cons'는 쌍을 만들고, `car'와 `cdr'는 첫번째와
;; 두번째 원소를 추출한다.
(cons 1 2) ; => '(1 . 2)
(car (cons 1 2)) ; => 1
(cdr (cons 1 2)) ; => 2

;;; 리스트

;; 리스트는 연결-리스트 데이터 구조이며, `cons' 쌍으로 만들어지며
;; `null' (또는 '()) 로 리스트의 끝을 표시한다.
(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3)
;; `list'는 편리한 가변인자 리스트 생성자이다.
(list 1 2 3) ; => '(1 2 3)
;; 글자 그대로의 리스트 값에는 인용부호를 쓴다.
'(1 2 3) ; => '(1 2 3)

;; 리스트의 앞에 항목을 추가하기 위하여 `cons'를 사용한다.
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; 리스트들을 붙이기 위해 `append'를 사용한다.
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; 리스트는 매우 기본적인 자료형이기 때문에, 리스트에 대해 적용되는 많은 기능들이 있다.
;; 예를 들어:
(map add1 '(1 2 3))          ; => '(2 3 4)
(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33)
(filter even? '(1 2 3 4))    ; => '(2 4)
(count even? '(1 2 3 4))     ; => 2
(take '(1 2 3 4) 2)          ; => '(1 2)
(drop '(1 2 3 4) 2)          ; => '(3 4)

;;; 벡터

;; 벡터는 고정 길이의 배열이다.
#(1 2 3) ; => '#(1 2 3)

;; `vector-append'를 사용하여 벡터들을 붙인다.
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; 집합

;; 리스트로부터 집합 만들기
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)

;; 원소를 추가하려면 `set-add'를 사용한다.
;; (함수적: 확장된 집합을 반환하며, 원래의 입력을 변경하지 않는다.)
(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)

;; 원소를 삭제하려면 `set-remove'
(set-remove (set 1 2 3) 1) ; => (set 2 3)

;; 존재 여부를 조사하려면 `set-member?'
(set-member? (set 1 2 3) 1) ; => #t
(set-member? (set 1 2 3) 4) ; => #f

;;; 해시

;; 불변의 해시 테이블을 만든다. (가변 예제는 아래에)
(define m (hash 'a 1 'b 2 'c 3))

;; 값 꺼내기
(hash-ref m 'a) ; => 1

;; 없는 값을 꺼내는 것은 예외를 발생시킨다.
; (hash-ref m 'd) => no value found

;; 키가 없을 때 반환할 기본값을 지정할 수 있다.
(hash-ref m 'd 0) ; => 0

;; `hash-set'을 사용하여 불변의 해시 테이블을 확장
;; (원래 것을 변경하지 않고 확장된 해시를 반환한다.)
(define m2 (hash-set m 'd 4))
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))

;; 이 해시들은 불변이라는 점을 기억하시오!
m ; => '#hash((b . 2) (a . 1) (c . 3))  <-- no `d'

;; `hash-remove'로 키를 삭제 (이것도 함수적)
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. 함수
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; `lambda'로 함수를 만든다.
;; 함수는 항상 마지막 식을 반환한다.
(lambda () "Hello World") ; => #<procedure>
;; 유니코드 `λ'도 사용 가능
(λ () "Hello World")     ; => same function

;; 모든 함수를 호출할 때는 괄호를 쓴다, lambda 식도 포함하여.
((lambda () "Hello World")) ; => "Hello World"
((λ () "Hello World"))      ; => "Hello World"

;; 변수에 함수를 할당
(define hello-world (lambda () "Hello World"))
(hello-world) ; => "Hello World"

;; 문법적 설탕을 사용하여 함수 정의를 더 짧게할 수 있다:
(define (hello-world2) "Hello World")

;; 위에서 ()는 함수의 인자 리스트이다.
(define hello
  (lambda (name)
    (string-append "Hello " name)))
(hello "Steve") ; => "Hello Steve"
;; ... 또는, 설탕 친 정의로:
(define (hello2 name)
  (string-append "Hello " name))

;; 가변인자 함수에는 `case-lambda'를 사용한다.
(define hello3
  (case-lambda
    [() "Hello World"]
    [(name) (string-append "Hello " name)]))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"
;; ... 또는 선택적 인자에 기본값 지정
(define (hello4 [name "World"])
  (string-append "Hello " name))

;; 함수는 추가 인자를 리스트에 포장할 수 있다.
(define (count-args . args)
  (format "You passed ~a args: ~a" (length args) args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
;; ... 설탕 안 친 `lambda' 형식으로는:
(define count-args2
  (lambda args
    (format "You passed ~a args: ~a" (length args) args)))

;; 일반 인자와 포장된 인자를 섞을 수 있다.
(define (hello-count name . args)
  (format "Hello ~a, you passed ~a extra args" name (length args)))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"
;; ... 설탕 안 친 것:
(define hello-count2
  (lambda (name . args)
    (format "Hello ~a, you passed ~a extra args" name (length args))))

;; 키워드 인자
(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
  (format "~a ~a, ~a extra args" g name (length args)))
(hello-k)                 ; => "Hello World, 0 extra args"
(hello-k 1 2 3)           ; => "Hello World, 3 extra args"
(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
                                         ; => "Hi Finn, 6 extra args"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. 동등성
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 숫자에는 `='를 사용하시오.
(= 3 3.0) ; => #t
(= 2 1) ; => #f

;; 개체의 동등성에는 `eq?'를 사용하시오.
(eq? 3 3) ; => #t
(eq? 3 3.0) ; => #f
(eq? (list 3) (list 3)) ; => #f

;; 모음에는 `equal?'을 사용하시오.
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
(equal? (list 'a 'b) (list 'b 'a)) ; => #f

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. 흐름 제어하기
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 조건

(if #t               ; 조사 식
    "this is true"   ; 그러면 식
    "this is false") ; 아니면 식
; => "this is true"

;; 조건에서는 #f가 아니면 참으로 취급된다.
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'yep

;; `cond'는 연속하여 조사하여 값을 선택한다.
(cond [(> 2 2) (error "wrong!")]
      [(< 2 2) (error "wrong again!")]
      [else 'ok]) ; => 'ok

;;; 양식 맞춤

(define (fizzbuzz? n)
  (match (list (remainder n 3) (remainder n 5))
    [(list 0 0) 'fizzbuzz]
    [(list 0 _) 'fizz]
    [(list _ 0) 'buzz]
    [_          #f]))

(fizzbuzz? 15) ; => 'fizzbuzz
(fizzbuzz? 37) ; => #f

;;; 반복

;; 반복은 (꼬리-) 재귀로 한다.
(define (loop i)
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i))))
(loop 5) ; => i=5, i=6, ...

;; 이름 있는 let으로도...
(let loop ((i 0))
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i)))) ; => i=0, i=1, ...

;; Racket은 매우 유연한 `for' 형식을 가지고 있다:
(for ([i 10])
  (printf "i=~a\n" i)) ; => i=0, i=1, ...
(for ([i (in-range 5 10)])
  (printf "i=~a\n" i)) ; => i=5, i=6, ...

;;; 다른 Sequence들을 순회하는 반복
;; `for'는 여러 가지의 sequence를 순회할 수 있다:
;; 리스트, 벡터, 문자열, 집합, 해시 테이블 등...

(for ([i (in-list '(l i s t))])
  (displayln i))

(for ([i (in-vector #(v e c t o r))])
  (displayln i))

(for ([i (in-string "string")])
  (displayln i))

(for ([i (in-set (set 'x 'y 'z))])
  (displayln i))

(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
  (printf "key:~a value:~a\n" k v))

;;; 더 복잡한 반복

;; 여러 sequence에 대한 병렬 순회 (가장 짧은 것 기준으로 중단)
(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x 1:y 2:z

;; 중첩 반복
(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z

;; 조건
(for ([i 1000]
      #:when (> i 5)
      #:unless (odd? i)
      #:break (> i 10))
  (printf "i=~a\n" i))
; => i=6, i=8, i=10

;;; 함축
;; `for' 반복과 비슷하며, 결과만 수집한다.

(for/list ([i '(1 2 3)])
  (add1 i)) ; => '(2 3 4)

(for/list ([i '(1 2 3)] #:when (even? i))
  i) ; => '(2)

(for/list ([i 10] [j '(x y z)])
  (list i j)) ; => '((0 x) (1 y) (2 z))

(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10))
  i) ; => '(6 8 10)

(for/hash ([i '(1 2 3)])
  (values i (number->string i)))
; => '#hash((1 . "1") (2 . "2") (3 . "3"))

;; 반복의 값을 수집하는 여러 가지 방법이 있다:
(for/sum ([i 10]) (* i i)) ; => 285
(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000
(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t
(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t
;; 임의의 조합을 사용하려면 `for/fold'를 사용:
(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10
;; (이것은 명령형 반복문을 대체하기도 한다.)

;;; 예외

;; 예외를 잡으려면 `with-handlers' 형식을 사용
(with-handlers ([exn:fail? (lambda (exn) 999)])
  (+ 1 "2")) ; => 999
(with-handlers ([exn:break? (lambda (exn) "no time")])
  (sleep 3)
  "phew") ; => "phew", but if you break it => "no time"

;; 예외나 다른 값을 던지려면 `raise'를 사용
(with-handlers ([number?    ; catch numeric values raised
                 identity]) ; return them as plain values
  (+ 1 (raise 2))) ; => 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. 변경
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 기존 변수에 새 값을 할당하려면 `set!'을 사용한다.
(define n 5)
(set! n (add1 n))
n ; => 6

;; 명시적인 가변 값을 사용하려면 box 사용 (다른 언어의 포인터나 참조와 비슷함)
(define n* (box 5))
(set-box! n* (add1 (unbox n*)))
(unbox n*) ; => 6

;; 많은 Racket 자료형은 불변이다 (쌍, 리스트 등). 그러나 어떤 것들은
;; 가변과 불변형이 둘 다 있다. (string, vector, hash table 등)

;; `vector'나 `make-vector'로 가변 벡터를 생성한다.
(define vec (vector 2 2 3 4))
(define wall (make-vector 100 'bottle-of-beer))
;; 칸을 변경하려면 vector-set!을 사용한다.
(vector-set! vec 0 1)
(vector-set! wall 99 'down)
vec ; => #(1 2 3 4)

;; 비어 있는 가변 해시 테이블을 만들고 조작한다.
(define m3 (make-hash))
(hash-set! m3 'a 1)
(hash-set! m3 'b 2)
(hash-set! m3 'c 3)
(hash-ref m3 'a)   ; => 1
(hash-ref m3 'd 0) ; => 0
(hash-remove! m3 'a)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. 모듈
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 모듈은 코드를 여러 파일과 재사용 가능한 라이브러리로 조직하게 한다.
;; 여기서 우리는 서브-모듈을 사용한다. 이 글이 만드는 전체 모듈("lang" 줄 부터 시작)에 포함된 모듈이다.

(module cake racket/base ; racket/base 기반의 `cake' 모듈 정의

  (provide print-cake) ; 모듈이 노출(export)시키는 함수

  (define (print-cake n)
    (show "   ~a   " n #\.)
    (show " .-~a-. " n #\|)
    (show " | ~a | " n #\space)
    (show "---~a---" n #\-))

  (define (show fmt n ch) ; 내부 함수
    (printf fmt (make-string n ch))
    (newline)))

;; `require'를 사용하여 모듈에서 모든 `provide'된 이름을 사용한다.
(require 'cake) ; '는 지역 지역 서브-모듈을 위한 것이다.
(print-cake 3)
; (show "~a" 1 #\A) ; => 에러, `show'가 export되지 않았음

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. 클래스와 개체
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 클래스 fish%를 생성한다. (-%는 클래스 정의에 쓰이는 관용구)
(define fish%
  (class object%
    (init size) ; 초기화 인자
    (super-new) ; 상위 클래스 초기화
    ;; 필드
    (define current-size size)
    ;; 공용 메서드
    (define/public (get-size)
      current-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

;; fish%의 인스턴스를 생성한다.
(define charlie
  (new fish% [size 10]))

;; 개체의 메서드를 호출하기 위해 `send'를 사용한다.
(send charlie get-size) ; => 10
(send charlie grow 6)
(send charlie get-size) ; => 16

;; `fish%'는 보통의 "일급" 값이며, mixin을 줄 수 있다.
(define (add-color c%)
  (class c%
    (init color)
    (super-new)
    (define my-color color)
    (define/public (get-color) my-color)))
(define colored-fish% (add-color fish%))
(define charlie2 (new colored-fish% [size 10] [color 'red]))
(send charlie2 get-color)
;; 또는, 이름 없이:
(send (new (add-color fish%) [size 10] [color 'red]) get-color)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 9. 매크로
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 매크로는 언어의 문법을 확장할 수 있게 한다.

;; while 반복문을 추가하자.
(define-syntax-rule (while condition body ...)
  (let loop ()
    (when condition
      body ...
      (loop))))

(let ([i 0])
  (while (< i  10)
    (displayln i)
    (set! i (add1 i))))

;; 매크로는 위생적이다. 즉, 기존 변수를 침범할 수 없다.
(define-syntax-rule (swap! x y) ; -!는 변경의 관용구
  (let ([tmp x])
    (set! x y)
    (set! y tmp)))

(define tmp 2)
(define other 3)
(swap! tmp other)
(printf "tmp = ~a; other = ~a\n" tmp other)
;; `tmp` 변수는 이름 충돌을 피하기 위해 `tmp_1`로 이름이 변경된다. 
;; (let ([tmp_1 tmp])
;;   (set! tmp other)
;;   (set! other tmp_1))

;; 하지만 그것들은 단지 코드 변형일 뿐이다. 예를 들어:
(define-syntax-rule (bad-while condition body ...)
  (when condition
    body ...
    (bad-while condition body ...)))
;; 이 매크로는 엉터리다: 무한 코드를 생성하며,
;; 이것을 사용하려고 하면 컴파일러가 무한 반복에 빠진다.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 10. 계약(Contract)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 계약은 모듈에서 노출된 값에 대해 제약을 부여한다.

(module bank-account racket
  (provide (contract-out
            [deposit (-> positive? any)] ; 값은 양수여야 함
            [balance (-> positive?)]))

  (define amount 0)
  (define (deposit a) (set! amount (+ amount a)))
  (define (balance) amount)
  )

(require 'bank-account)
(deposit 5)

(balance) ; => 5

;; 양수가 아닌 값을 예치하려고 하는 고객은 비난받는다.
;; (deposit -5) ; => deposit: contract violation
;; expected: positive?
;; given: -5
;; more details....

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 11. 입력과 출력
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Racket은 이 "port"라는 개념이 있다. 이것은 다른 언어의
;; 파일 서술자 (file descriptor)와 매우 비슷하다.

;; "/tmp/tmp.txt"를 열고 "Hello World"를 기록한다.
;; 그 파일이 이미 있다면 에러를 발생시킨다.
(define out-port (open-output-file "/tmp/tmp.txt"))
(displayln "Hello World" out-port)
(close-output-port out-port)

;; "/tmp/tmp.txt"에 붙이기
(define out-port (open-output-file "/tmp/tmp.txt"
                                   #:exists 'append))
(displayln "Hola mundo" out-port)
(close-output-port out-port)

;; 파일에서 다시 읽기
(define in-port (open-input-file "/tmp/tmp.txt"))
(displayln (read-line in-port))
; => "Hello World"
(displayln (read-line in-port))
; => "Hola mundo"
(close-input-port in-port)

;; 다르게, call-with-output-file을 사용하면, 명시적으로 파일을 닫지 않아도 된다.
(call-with-output-file "/tmp/tmp.txt"
  #:exists 'update ; 내용을 다시 쓴다.
  (λ (out-port)
    (displayln "World Hello!" out-port)))

;; call-with-input-file은 입력에 대해 같은 방식으로 작동한다.
(call-with-input-file "/tmp/tmp.txt"
  (λ (in-port)
    (displayln (read-line in-port))))
```

## 더 읽을거리

더 배우고 싶으면, [Getting Started with Racket](http://docs.racket-lang.org/getting-started/)도 보시오.
---
category: tool
tool: vim
contributors:
    - ["RadhikaG", "https://github.com/RadhikaG"]
translators:
    - ["Wooseop Kim", "https://github.com/linterpreteur"]
filename: LearnVim-kr.txt
lang: ko-kr
---

[Vim](http://www.vim.org)
(Vi IMproved)은 유닉스의 인기 있는 vi 에디터의 클론입니다. Vim은 속도와 생산성을 위해
설계된 텍스트 에디터로, 대부분의 유닉스 기반 시스템에 내장되어 있습니다. 다양한 단축 키를 통해
파일 안에서 빠르게 이동하고 편집할 수 있습니다.

## Vim 조작의 기본

```
    vim <filename>   # vim으로 <filename> 열기
    :q               # vim 종료
    :w               # 현재 파일 저장
    :wq              # 파일 저장 후 종료
    :q!              # 저장하지 않고 종료
                     # ! *강제로* :q를 실행하여, 저장 없이 종료
    :x               # 파일 저장 후 종료 (짧은 :wq)

    u                # 동작 취소
    CTRL+R           # 되돌리기

    h                # 한 글자 왼쪽으로 이동
    j                # 아래로 한 줄 이동
    k                # 위로 한 줄 이동
    l                # 한 글자 오른쪽으로 이동

    # 줄 안에서의 이동

    0                # 줄 시작으로 이동
    $                # 줄 끝으로 이동
    ^                # 줄의 공백이 아닌 첫 문자로 이동

    # 텍스트 검색

    /word            # 커서 뒤에 나타나는 해당 단어를 모두 하이라이트
    ?word            # 커서 앞에 나타나는 해당 단어를 모두 하이라이트
    n                # 해당 단어를 검색 후 다음으로 나타나는 위치로 이동
    N                # 이전에 나타나는 위치로 이동

    :%s/foo/bar/g    # 파일 모든 줄에 있는 'foo'를 'bar'로 치환
    :s/foo/bar/g     # 현재 줄에 있는 'foo'를 'bar'로 치환

    # 문자로 이동

    f<character>     # <character>로 건너뛰기
    t<character>     # <character>의 바로 뒤로 건너뛰기 

    # 예를 들어,  
    f<               # <로 건너뛰기
    t<               # <의 바로 뒤로 건너뛰기
    
    # 단어 단위로 이동

    w                # 한 단어 오른쪽으로 이동
    b                # 한 단어 왼쪽으로 이동
    e                # 현재 단어의 끝으로 이동

    # 기타 이동 명령어

    gg               # 파일 맨 위로 이동
    G                # 파일 맨 아래로 이동
    :NUM             # 줄 수 NUM(숫자)로 가기
    H                # 화면 꼭대기로 이동
    M                # 화면 중간으로 이동
    L                # 화면 바닥으로 이동
```

## 모드

Vim은 **모드**의 개념에 기초를 두고 있습니다.

명령어 모드   - vim을 시작하면 처음에 이 모드입니다. 이동과 명령어 입력에 사용합니다.
삽입 모드     - 파일을 수정합니다.
비주얼 모드   - 텍스트를 하이라이트하고 그 텍스트에 대한 작업을 합니다.
실행 모드     - ':' 이후 명령어를 입력합니다.

```
    i                # 커서 위치 앞에서 삽입 모드로 변경
    a                # 커서 위치 뒤에서 삽입 모드로 변경
    v                # 비주얼 모드로 변경    
    :                # 실행 모드로 변경
    <esc>            # 현재 모드를 벗어나 명령어 모드로 변경

    # 복사와 붙여넣기

    y                # 선택한 객체 복사(Yank)
    yy               # 현재 줄 복사
    d                # 선택한 객체 삭제
    dd               # 현재 줄 삭제
    p                # 커서 위치 뒤에 복사한 텍스트 붙여넣기
    P                # 커서 위치 뒤에 복사한 텍스트 붙여넣기
    x                # 현재 커서 위치의 문자 삭제
```

## vim의 문법

Vim의 명령어는 '서술어-수식어-목적어'로 생각할 수 있습니다.

서술어     - 취할 동작 
수식어     - 동작을 취할 방식
목적어     - 동작을 취할 객체

'서술어', '수식어', '목적어'의 예시는 다음과 같습니다.

```
    # '서술어'
 
    d                # 지운다
    c                # 바꾼다
    y                # 복사한다
    v                # 선택한다

    # '수식어'

    i                # 안에
    a                # 근처에
    NUM              # (숫자)
    f                # 찾아서 그곳에
    t                # 찾아서 그 앞에
    /                # 문자열을 커서 뒤로 찾아서
    ?                # 문자열을 커서 앞으로 찾아서

    # '목적어'

    w                # 단어를
    s                # 문장을
    p                # 문단을
    b                # 블락을
    
    # 예시 '문장' (명령어)

    d2w              # 단어 2개를 지운다
    cis              # 문장 안을 바꾼다
    yip              # 문단 안을 복사한다
    ct<              # 여는 괄호까지 바꾼다
                     # 현재 위치에서 다음 여는 괄호까지의 텍스트를 바꾼다
    d$               # 줄 끝까지 지운다
```

## 몇 가지 트릭

        <!--TODO: Add more!-->
```
    >                # 선택한 영역 한 칸 들여쓰기
    <                # 선택한 영역 한 칸 내어쓰기
    :earlier 15m     # 15분 전의 상태로 되돌리기
    :later 15m       # 위의 명령어를 취소
    ddp              # 이어지는 줄과 위치 맞바꾸기 (dd 후 p)
    .                # 이전 동작 반복
    :w !sudo tee %   # 현재 파일을 루트 권한으로 저장
```

## 매크로

매크로는 기본적으로 녹화할 수 있는 동작을 말합니다.
매크로를 녹화하기 시작하면, 끝날 때까지 **모든** 동작과 명령어가 녹화됩니다.
매크로를 호출하면 선택한 텍스트에 대해 정확히 같은 순서의 동작과 명령어가 실행됩니다.

```
    qa               # 'a'라는 이름의 매크로 녹화 시작
    q                # 녹화 중지
    @a               # 매크로 실행
```

### ~/.vimrc 설정

.vimrc 파일은 Vim이 시작할 때의 설정을 결정합니다.

다음은 ~/.vimrc 파일의 예시입니다.

```
" ~/.vimrc 예시
" 2015.10 

" vim이 iMprove 되려면 필요
set nocompatible

" 자동 들여쓰기 등을 위해 파일 명으로부터 타입 결정
filetype indent plugin on

" 신택스 하이라이팅 켜기
syntax on

" 커맨드 라인 완성 향상
set wildmenu

" 대문자를 썼을 때가 아니면 대소문자 구분하지 않고 검색
set ignorecase
set smartcase

" 줄넘김을 했을 때 파일에 따른 들여쓰기가 켜져 있지 않다면
" 현재 줄과 같은 들여쓰기를 유지
set autoindent

" 좌측에 줄 번호 표시
set number

" 들여쓰기 설정 (개인 기호에 따라 변경)

" 탭 하나와 시각적으로 같을 스페이스 개수
set tabstop=4

" 편집할 때 탭 하나에 들어갈 스페이스 수
set softtabstop=4

" 들여쓰기 혹은 내어쓰기 작업(>>, <<)을 했을 때 움직일 스페이스 개수
set shiftwidth=4

" 탭을 스페이스로 변환
set expandtab

" 들여쓰기와 정렬에 자동 탭 및 스페이스 사용
set smarttab
```

### 참고 자료

[(영어) Vim 홈페이지](http://www.vim.org/index.php)

`$ vimtutor`

[(영어) vim 입문과 기초](https://danielmiessler.com/study/vim/)

[(영어) 엄마가 말해주지 않은 Vim의 어두운 구석들 (Stack Overflow 게시물)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about)

[(영어) 아치 리눅스 위키](https://wiki.archlinux.org/index.php/Vim)
---
language: xml
filename: learnxml-kr.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
  - ["Rachel Stiyer", "https://github.com/rstiyer"]
  - ["Deepanshu Utkarsh", "https://github.com/duci9y"]
translators:
  - ["Wooseop Kim", "https://github.com/linterpreteur"]
lang: ko-kr
---

XML은 데이터를 저장하고 전송하기 위해 설계된 마크업 언어입니다. 인간과 기계 모두가 읽을 수 있도록 만들어졌습니다.

XML은 HTML과는 달리 데이터를 보여주는 방법이나 그 형식을 특정하지 않습니다. 단지 데이터를 담을 뿐입니다.

차이는 **내용**과 **마크업**에 있습니다. 내용은 무엇이든 될 수 있지만, 마크업은 정의되어 있습니다.

## 기초 정의 및 도입

XML 문서는 기본적으로 자신을 설명하는 *속성*을 가질 수 있으며 자식으로서 텍스트 혹은 다른 요소를 가질 수 있는 *요소*들로 이루어집니다. 모든 XML 문서는 반드시 루트 요소를 가져야 합니다. 루트 요소는 문서에 있는 모든 다른 요소들의 조상입니다.

XML 파서는 매우 엄격하게 설계되어 있으므로 문서의 형식이 틀렸다면 파싱을 멈출 것입니다. 그러므로 모든 XML 문서는 [(영어) XML 문법 규칙](http://www.w3schools.com/xml/xml_syntax.asp)을 따른다고 보장할 수 있습니다.

```xml
<!-- 주석에는 두 개의 연속된 하이픈(-)이 들어갈 수 없습니다. -->
<!-- 주석은 여러 줄로
  이어질 수 있습니다. -->

<!-- 요소 -->
<!-- 요소는 XML의 기본적 구성품입니다. 요소에는 두 개의 유형이 있습니다. -->
<element1 attribute="value" /> <!-- 빈 요소는 내용을 담지 않습니다. -->
<!-- 그리고 비지 않은 요소가 있습니다. -->
<element2 attribute="value">내용</element2>
<!-- 요소 이름에는 알파벳과 숫자만이 허용됩니다. -->

<empty /> <!-- 요소는 어떠한 내용도 없이 순수한 마크업인 -->
<!-- 빈 요소 태그로 구성될 수 있습니다. -->

<notempty> <!-- 혹은 여는 태그와 -->
  <!-- 내용, -->
</notempty> <!-- 그리고 닫는 태그로 구성될 수도 잇습니다. -->

<!-- 요소 이름은 대소문자를 구별합니다. -->
<element />
<eLEMENT />
<!-- 둘은 같지 않습니다. -->

<!-- 속성 -->
<!-- 속성은 요소 안에 존재하는 키와 값의 쌍입니다. -->
<element attribute="value" another="anotherValue" many="space-separated list" />
<!-- 속성은 원소에서 단 한 번만 나타날 수 있습니다. 속성은 단 하나의 값만 갖습니다.
  이에 대한 흔한 해결책은 공백으로 구분된 리스트를 포함하는 것입니다. -->

<!-- 중첩 요소 -->
<!-- 한 요소의 내용은 다른 요소들을 포함할 수 있습니다. -->
<parent>
  <child>Text</child>
  <emptysibling />
</parent>
<!-- 표준적인 트리 명칭이 사용됩니다. 각각의 요소는 노드라고 부릅니다.
  한 단계 위의 조상은 부모이며, 한 단계 아래의 후손은 자식입니다.
  같은 부모 요소를 가진 요소들은 자매입니다. -->

<!-- XML은 공백을 보존합니다. -->
<child>
  Text
</child>
<child>Text</child>
<!-- 둘은 같지 않습니다. -->
```

## XML 문서

XML이 유용한 것은 인간도 읽을 수 있다는 것입니다. 다음의 문서는 에릭 레이의 XML 배우기를 포함해 세 권의 책을 파는 서점을 정의한다는 것을 알 수 있습니다. XML 파서 없이도 이렇게 쉽습니다.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- XML 프롤로그라는 것입니다. 필수는 아니지만, 권장됩니다. -->
<bookstore>
  <book category="COOKING">
    <title lang="ko">매일 이탈리아 요리</title>
    <author>지아다 데 라우렌티스</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="ko">해리 포터</title>
    <author>J K 롤링</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="ko">XML 배우기</title>
    <author>에릭 레이</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
```

## 적격성과 유효성

XML 문서는 문법적으로 정확할 경우 *적격*합니다. 하지만 문서 유형 정의(DTD)를 이용하여 문서에 제약을 더 추가할 수 있습니다. 한 문서의 요소와 속성이 DTD 안에 정의되어 있고 그 파일에 특정된 문법을 따른다면 *적격*할 뿐만 아니라 그 DTD에 대하여 *유효*하다고 말합니다.

```xml
<!-- DTD를 외부에 선언: -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore SYSTEM "Bookstore.dtd">
<!-- bookstore가 루트 요소이며 'Bookstore.dtd'가 DTD 파일의
  경로임을 선언합니다. -->
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
</bookstore>

<!-- DTD 파일 -->
<!ELEMENT bookstore (book+)>
<!-- bookstore 요소는 하나 이상의 book 요소를 자식으로 가질 수 있습니다. -->
<!ELEMENT book (title, price)>
<!-- 각각의 book은 title과 price를 자식으로 반드시 갖습니다. -->
<!ATTLIST book category CDATA "Literature">
<!-- book은 category 속성을 가져야 합니다. 그렇지 않다면 그 기본값은 'Literature'입니다. -->
<!ELEMENT title (#PCDATA)>
<!-- title 요소는 반드시 PCDATA만 포함해야 합니다. 즉,
  파서가 읽을 텍스트만을 포함해야 하며 자식을 포함할 수 없습니다.
  CDATA와 비교해 보세요. -->
<!ELEMENT price (#PCDATA)>
]>

<!-- DTD는 XML 파일 안에 선언될 수도 있습니다. -->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE bookstore [
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>

<bookstore>
  <book category="COOKING">
    <title>Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>
```

## DTD 호환성과 XML 스키마 정의

DTD는 오래되었기 때문에 지원이 광범위합니다. 불행히도 네임스페이스와 같은 현대적 XML 기능은 DTD에서 지원하지 않습니다. XML 스키마 정의(XSD)가 XML 문서의 문법을 정의하기 위한 DTD의 대체재입니다.

## Resources

* [(영어) Validate your XML](http://www.xmlvalidation.com)

## Further Reading

* [(영어) XML 스키마 정의 튜토리얼](http://www.w3schools.com/xml/xml_schema.asp)
* [(영어) DTD 튜토리얼](http://www.w3schools.com/xml/xml_dtd_intro.asp)
* [(영어) XML 튜토리얼](http://www.w3schools.com/xml/default.asp)
* [(영어) XPath 쿼리로 XML 파싱하기](http://www.w3schools.com/xml/xml_xpath.asp)
---
language: yaml
filename: learnyaml-kr.yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
  - ["Suhas SG", "https://github.com/jargnar"]
translators:
  - ["Wooseop Kim", "https://github.com/linterpreteur"]
lang: ko-kr
---

YAML은 인간이 직접 쓰고 읽을 수 있도록 설계된 데이터 직렬화 언어입니다.

YAML은 마치 파이썬처럼 개행과 들여쓰기에 문법적으로 의미를 준 JSON의 엄격한 수퍼셋입니다.
하지만 파이썬과는 달리 YAML은 탭 문자를 전혀 허용하지 않습니다.

```yaml
# YAML의 주석은 이런 식입니다.

############
# 스칼라 형 #
############

# 문서 내내 이어질 루트 객체는 맵입니다.
# 맵은 다른 언어의 딕셔너리, 해시, 혹은 객체에 해당됩니다.
키: 값
다른_키: 다른 값이 여기 옵니다.
숫자_값: 100
# 숫자 1을 값으로 가지기 위해서는 따옴표에 담아야 합니다.
# 그러지 않는다면 YAML 파서는 그것을 참 값을 가지는 불리언으로 해석할 것입니다.
과학적_표기법: 1e+12
불리언: true
널_값: null
띄어서 쓴 키: 값
# 문자열에 따옴표를 칠 필요는 없습니다. 하지만 칠 수도 있습니다.
하지만: "따옴표에 담은 문자열"
"키도 따옴표에 담을 수 있습니다.": "키에 ':'을 넣고 싶다면 유용합니다."

# 여러 줄의 문자열은 (|을 이용한) '리터럴 블락' 혹은 (>을 이용한) '접은 블락'으로
# 쓸 수 있습니다.
리터럴_블락: |
    개행을 포함한 이 모든 덩어리가 '리터럴_블락' 키에 대응하는 값이 될 것입니다.
    
    리터럴 값은 들여쓰기가 끝날 때까지 계속되며 들여쓰기는 문자열에 포함되지
    않습니다.
    
        '들여쓰기를 더 한' 줄은 나머지 들여쓰기를 유지합니다.
        이 줄은 띄어쓰기 4개만큼 들여쓰기 됩니다.
접는_방식: >
    이 텍스트 덩어리가 전부 '접는_방식' 키의 값이 되지만, 이번에는 모든 개행 문자가
    띄어쓰기 하나로 대체됩니다.

    위와 같이 텅 빈 줄은 개행 문자로 바뀝니다.

        '더 들여쓴' 줄 역시 개행 문자를 유지합니다.
        이 텍스트는 두 줄에 걸쳐 나타날 것입니다.

##########
# 모임 형 #
##########

# 중첩은 들여쓰기로 가능합니다.
중첩된_맵:
    키: 값
    다른_키: 다른 값
    다른_중첩된_맵:
        안녕: 안녕

# 맵은 반드시 문자열 키를 가지는 것은 아닙니다.
0.25: 실수형 키

# 키는 여러 줄에 걸친 객체와 같이 복합적일 수도 있습니다.
# ?와 그 뒤의 띄어쓰기로 복합 키의 시작을 나타냅니다.
? |
    여러 줄짜리
    키
: 그리고 그 값

# YAML은 복합 키 문법으로 연속열 간의 매핑을 지원합니다.
# 일부 파서는 지원하지 않을 수 있습니다.
# 예시
? - 맨체스터 유나이티드
  - 레알 마드리드
: [ 2001-01-01, 2002-02-02 ]

# 리스트 혹은 배열에 대응되는 연속열은 다음과 같습니다.
연속열:
    - 하나
    - 둘
    - 0.5 # 연속열은 다른 형을 포함 가능
    - 넷
    - 키: 값
      다른_키: 다른_값
    -
        - 연속열 안의
        - 또 다른 연속열

# YAML은 JSON의 수퍼셋이기 때문에, JSON식으로 맵과 연속열을 작성할 수도
# 있습니다.
제이슨_맵: {"키": "값"}
제이슨_열: [3, 2, 1, "발사"]

#################
# 기타 YAML 기능 #
#################

# YAML은 '앵커'라는 편리한 기능이 있습니다. 앵커를 이용하면 문서에서
# 손쉽게 내용을 복제할 수 있습니다. 이 키들은 같은 값을 갖습니다.
앵커된_내용: &앵커_이름 이 문자열은 두 키의 값으로 나타납니다.
다른_앵커: *앵커_이름

# 앵커는 속성을 복제하거나 상속할 수 있습니다.
기반: &기반
    이름: 모두 이름이 같다

멍멍: &멍멍
    <<: *기반
    나이: 10

야옹: &야옹
    <<: *기반
    나이: 20

# 멍멍이와 야옹이는 같은 이름, '모두 이름이 같다'를 같습니다.

# 또한 YAML에는 명시적으로 형을 선언할 수 있는 태그가 있습니다.
명시적_문자열: !!str 0.5
# 파이썬의 복소수 형을 나타내는 다음 태그처럼, 일부 파서는 언어에 종속된 태그를
# 구현합니다.
파이썬_복소수: !!python/complex 1+2j

# YAML 복합 키를 언어 종속 태그와 함께 사용할 수도 있습니다.
? !!python/tuple [5, 7]
: 오십칠
# 파이썬에서의 {(5, 7): '오십칠'} 객체

###############
# 기타 YAML 형 #
###############

# Strings and numbers aren't the only scalars that YAML can understand.
# YAML이 이해할 수 있는 스칼라는 문자열과 수만 있는 것은 아닙니다.
# ISO 형식 날짜와 시간 리터럴 또한 해석됩니다.
시간: 2001-12-15T02:59:43.1Z
띄어쓰기_한_시간: 2001-12-14 21:59:43.10 -5
날짜: 2002-12-14

# !!binary 태그는 문자열이 실제로는 base64로 인코딩된
# 이진수 객체(BLOB)라는 것을 나타냅니다.
이미지_파일: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML에는 다음과 같은 집합도 있습니다.
집합:
    ? 하나
    ? 둘
    ? 셋

# 파이썬과 마찬가지로 집합은 단지 널 값을 갖는 맵입니다. 위는 다음과 같습니다.
집합2:
    하나: null
    둘: null
    셋: null
```

### 더 읽기

+ [(영어) YAML 공식 사이트](http://yaml.org/)
+ [(영어) 온라인 YAML 검사기](http://codebeautify.org/yaml-validator)
---
language: kotlin
contributors:
    - ["S Webber", "https://github.com/s-webber"]
filename: LearnKotlin.kt
---

Kotlin is a statically typed programming language for the JVM, Android and the
browser. It is 100% interoperable with Java.
[Read more here.](https://kotlinlang.org/)

```kotlin
// Single-line comments start with //
/*
Multi-line comments look like this.
*/

// The "package" keyword works in the same way as in Java.
package com.learnxinyminutes.kotlin

/*
The entry point to a Kotlin program is a function named "main".
The function is passed an array containing any command line arguments.
*/
fun main(args: Array<String>) {
    /*
    Declaring values is done using either "var" or "val".
    "val" declarations cannot be reassigned, whereas "vars" can.
    */
    val fooVal = 10 // we cannot later reassign fooVal to something else
    var fooVar = 10
    fooVar = 20 // fooVar can be reassigned

    /*
    In most cases, Kotlin can determine what the type of a variable is,
    so we don't have to explicitly specify it every time.
    We can explicitly declare the type of a variable like so:
    */
    val foo: Int = 7

    /*
    Strings can be represented in a similar way as in Java.
    Escaping is done with a backslash.
    */
    val fooString = "My String Is Here!"
    val barString = "Printing on a new line?\nNo Problem!"
    val bazString = "Do you want to add a tab?\tNo Problem!"
    println(fooString)
    println(barString)
    println(bazString)

    /*
    A raw string is delimited by a triple quote (""").
    Raw strings can contain newlines and any other characters.
    */
    val fooRawString = """
fun helloWorld(val name : String) {
   println("Hello, world!")
}
"""
    println(fooRawString)

    /*
    Strings can contain template expressions.
    A template expression starts with a dollar sign ($).
    */
    val fooTemplateString = "$fooString has ${fooString.length} characters"
    println(fooTemplateString) // => My String Is Here! has 18 characters 

    /*
    For a variable to hold null it must be explicitly specified as nullable.
    A variable can be specified as nullable by appending a ? to its type.
    We can access a nullable variable by using the ?. operator.
    We can use the ?: operator to specify an alternative value to use
    if a variable is null.
    */
    var fooNullable: String? = "abc"
    println(fooNullable?.length) // => 3
    println(fooNullable?.length ?: -1) // => 3
    fooNullable = null
    println(fooNullable?.length) // => null
    println(fooNullable?.length ?: -1) // => -1

    /*
    Functions can be declared using the "fun" keyword.
    Function arguments are specified in brackets after the function name.
    Function arguments can optionally have a default value.
    The function return type, if required, is specified after the arguments.
    */
    fun hello(name: String = "world"): String {
        return "Hello, $name!"
    }
    println(hello("foo")) // => Hello, foo!
    println(hello(name = "bar")) // => Hello, bar!
    println(hello()) // => Hello, world!

    /*
    A function parameter may be marked with the "vararg" keyword
    to allow a variable number of arguments to be passed to the function.
    */
    fun varargExample(vararg names: Int) {
        println("Argument has ${names.size} elements")
    }
    varargExample() // => Argument has 0 elements
    varargExample(1) // => Argument has 1 elements
    varargExample(1, 2, 3) // => Argument has 3 elements

    /*
    When a function consists of a single expression then the curly brackets can
    be omitted. The body is specified after a = symbol.
    */
    fun odd(x: Int): Boolean = x % 2 == 1
    println(odd(6)) // => false
    println(odd(7)) // => true

    // If the return type can be inferred then we don't need to specify it.
    fun even(x: Int) = x % 2 == 0
    println(even(6)) // => true
    println(even(7)) // => false

    // Functions can take functions as arguments and return functions.
    fun not(f: (Int) -> Boolean): (Int) -> Boolean {
        return {n -> !f.invoke(n)}
    }
    // Named functions can be specified as arguments using the :: operator.
    val notOdd = not(::odd)
    val notEven = not(::even)
    // Lambda expressions can be specified as arguments.
    val notZero = not {n -> n == 0}
    /*
    If a lambda has only one parameter
    then its declaration can be omitted (along with the ->).
    The name of the single parameter will be "it".
    */
    val notPositive = not {it > 0}
    for (i in 0..4) {
        println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}")
    }

    // The "class" keyword is used to declare classes.
    class ExampleClass(val x: Int) {
        fun memberFunction(y: Int): Int {
            return x + y
        }

        infix fun infixMemberFunction(y: Int): Int {
            return x * y
        }
    }
    /*
    To create a new instance we call the constructor.
    Note that Kotlin does not have a "new" keyword.
    */
    val fooExampleClass = ExampleClass(7)
    // Member functions can be called using dot notation.
    println(fooExampleClass.memberFunction(4)) // => 11
    /*
    If a function has been marked with the "infix" keyword then it can be
    called using infix notation.
    */
    println(fooExampleClass infixMemberFunction 4) // => 28

    /*
    Data classes are a concise way to create classes that just hold data.
    The "hashCode"/"equals" and "toString" methods are automatically generated.
    */
    data class DataClassExample (val x: Int, val y: Int, val z: Int)
    val fooData = DataClassExample(1, 2, 4)
    println(fooData) // => DataClassExample(x=1, y=2, z=4)

    // Data classes have a "copy" function.
    val fooCopy = fooData.copy(y = 100)
    println(fooCopy) // => DataClassExample(x=1, y=100, z=4)

    // Objects can be destructured into multiple variables.
    val (a, b, c) = fooCopy
    println("$a $b $c") // => 1 100 4
    
    // destructuring in "for" loop
    for ((a, b, c) in listOf(fooData)) {
        println("$a $b $c") // => 1 100 4
    }
    
    val mapData = mapOf("a" to 1, "b" to 2)
    // Map.Entry is destructurable as well
    for ((key, value) in mapData) {
        println("$key -> $value")
    }

    // The "with" function is similar to the JavaScript "with" statement.
    data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
    val fooMutableData = MutableDataClassExample(7, 4, 9)
    with (fooMutableData) {
        x -= 2
        y += 2
        z--
    }
    println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8)

    /*
    We can create a list using the "listOf" function.
    The list will be immutable - elements cannot be added or removed.
    */
    val fooList = listOf("a", "b", "c")
    println(fooList.size) // => 3
    println(fooList.first()) // => a
    println(fooList.last()) // => c
    // Elements of a list can be accessed by their index.
    println(fooList[1]) // => b

    // A mutable list can be created using the "mutableListOf" function.
    val fooMutableList = mutableListOf("a", "b", "c")
    fooMutableList.add("d")
    println(fooMutableList.last()) // => d
    println(fooMutableList.size) // => 4

    // We can create a set using the "setOf" function.
    val fooSet = setOf("a", "b", "c")
    println(fooSet.contains("a")) // => true
    println(fooSet.contains("z")) // => false

    // We can create a map using the "mapOf" function.
    val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9)
    // Map values can be accessed by their key.
    println(fooMap["a"]) // => 8

    /*
    Sequences represent lazily-evaluated collections.
    We can create a sequence using the "generateSequence" function.
    */
    val fooSequence = generateSequence(1, { it + 1 })
    val x = fooSequence.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // An example of using a sequence to generate Fibonacci numbers:
    fun fibonacciSequence(): Sequence<Long> {
        var a = 0L
        var b = 1L

        fun next(): Long {
            val result = a + b
            a = b
            b = result
            return a
        }

        return generateSequence(::next)
    }
    val y = fibonacciSequence().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

    // Kotlin provides higher-order functions for working with collections.
    val z = (1..9).map {it * 3}
                  .filter {it < 20}
                  .groupBy {it % 2 == 0}
                  .mapKeys {if (it.key) "even" else "odd"}
    println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]}

    // A "for" loop can be used with anything that provides an iterator.
    for (c in "hello") {
        println(c)
    }

    // "while" loops work in the same way as other languages.
    var ctr = 0
    while (ctr < 5) {
        println(ctr)
        ctr++
    }
    do {
        println(ctr)
        ctr++
    } while (ctr < 10)

    /*
    "if" can be used as an expression that returns a value.
    For this reason the ternary ?: operator is not needed in Kotlin.
    */
    val num = 5
    val message = if (num % 2 == 0) "even" else "odd"
    println("$num is $message") // => 5 is odd

    // "when" can be used as an alternative to "if-else if" chains.
    val i = 10
    when {
        i < 7 -> println("first block")
        fooString.startsWith("hello") -> println("second block")
        else -> println("else block")
    }

    // "when" can be used with an argument.
    when (i) {
        0, 21 -> println("0 or 21")
        in 1..20 -> println("in the range 1 to 20")
        else -> println("none of the above")
    }

    // "when" can be used as a function that returns a value.
    var result = when (i) {
        0, 21 -> "0 or 21"
        in 1..20 -> "in the range 1 to 20"
        else -> "none of the above"
    }
    println(result)

    /*
    We can check if an object is a particular type by using the "is" operator.
    If an object passes a type check then it can be used as that type without
    explicitly casting it.
    */
    fun smartCastExample(x: Any) : Boolean {
        if (x is Boolean) {
            // x is automatically cast to Boolean
            return x
        } else if (x is Int) {
            // x is automatically cast to Int
            return x > 0
        } else if (x is String) {
            // x is automatically cast to String
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(smartCastExample("Hello, world!")) // => true
    println(smartCastExample("")) // => false
    println(smartCastExample(5)) // => true
    println(smartCastExample(0)) // => false
    println(smartCastExample(true)) // => true

    // Smartcast also works with when block
    fun smartCastWhenExample(x: Any) = when (x) {
        is Boolean -> x
        is Int -> x > 0
        is String -> x.isNotEmpty()
        else -> false
    }

    /*
    Extensions are a way to add new functionality to a class.
    This is similar to C# extension methods.
    */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("Hello, world!".remove('l')) // => Heo, word!

    println(EnumExample.A) // => A
    println(ObjectExample.hello()) // => hello
}

// Enum classes are similar to Java enum types.
enum class EnumExample {
    A, B, C
}

/*
The "object" keyword can be used to create singleton objects.
We cannot instantiate it but we can refer to its unique instance by its name.
This is similar to Scala singleton objects.
*/
object ObjectExample {
    fun hello(): String {
        return "hello"
    }
}

fun useObject() {
    ObjectExample.hello()
    val someRef: Any = ObjectExample // we use objects name just as is
}

```

### Further Reading

* [Kotlin tutorials](https://kotlinlang.org/docs/tutorials/)
* [Try Kotlin in your browser](http://try.kotlinlang.org/)
* [A list of Kotlin resources](http://kotlin.link/)
---
language: latex
contributors:
    - ["Chaitanya Krishna Ande", "http://icymist.github.io"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
    - ["Sricharan Chiruvolu", "http://sricharan.xyz"]
    - ["Ramanan Balakrishnan", "https://github.com/ramananbalakrishnan"]
    - ["Svetlana Golubeva", "https://attillax.github.io/"]
filename: learn-latex.tex
---

```tex
% All comment lines start with %
% There are no multi-line comments

% LaTeX is NOT a "What You See Is What You Get" word processing software like
% MS Word, or OpenOffice Writer

% Every LaTeX command starts with a backslash (\)

% LaTeX documents start with a defining the type of document it's compiling
% Other document types include book, report, presentations, etc.
% The options for the document appear in the [] brackets. In this case
% it specifies we want to use 12pt font.
\documentclass[12pt]{article}

% Next we define the packages the document uses.
% If you want to include graphics, colored text, or
% source code from another language file into your document, 
% you need to enhance the capabilities of LaTeX. This is done by adding packages. 
% I'm going to include the float and caption packages for figures
% and hyperref package for hyperlinks
\usepackage{caption}
\usepackage{float}
\usepackage{hyperref}

% We can define some other document properties too!
\author{Chaitanya Krishna Ande, Colton Kohnke, Sricharan Chiruvolu \& \\
Svetlana Golubeva}
\date{\today}
\title{Learn \LaTeX \hspace{1pt} in Y Minutes!}

% Now we're ready to begin the document
% Everything before this line is called "The Preamble"
\begin{document} 
% if we set the author, date, title fields, we can have LaTeX 
% create a title page for us.
\maketitle

% If we have sections, we can create table of contents. We have to compile our
% document twice to make it appear in right order.
% It is a good practice to separate the table of contents form the body of the 
% document. To do so we use \newpage command
\newpage
\tableofcontents

\newpage

% Most research papers have abstract, you can use the predefined commands for this.
% This should appear in its logical order, therefore, after the top matter,
% but before the main sections of the body. 
% This command is available in the document classes article and report.
\begin{abstract}
 \LaTeX \hspace{1pt} documentation written as \LaTeX! How novel and totally not
 my idea!
\end{abstract}

% Section commands are intuitive. 
% All the titles of the sections are added automatically to the table of contents.
\section{Introduction}
Hello, my name is Colton and together we're going to explore \LaTeX!

\section{Another section}
This is the text for another section. I think it needs a subsection.

\subsection{This is a subsection} % Subsections are also intuitive.
I think we need another one

\subsubsection{Pythagoras}
Much better now.
\label{subsec:pythagoras}

% By using the asterisk we can suppress LaTeX's inbuilt numbering.
% This works for other LaTeX commands as well. 
\section*{This is an unnumbered section} 
However not all sections have to be numbered!

\section{Some Text notes}
%\section{Spacing} % Need to add more information about space intervals
\LaTeX \hspace{1pt} is generally pretty good about placing text where it should
go. If 
a line \\ needs \\ to \\ break \\ you add \textbackslash\textbackslash 
\hspace{1pt} to the source code. \\ 

\section{Lists}
Lists are one of the easiest things to create in \LaTeX! I need to go shopping
tomorrow, so let's make a grocery list.
\begin{enumerate} % This creates an "enumerate" environment.
  % \item tells the enumerate to increment
  \item Salad.
  \item 27 watermelon.
  \item A single jackrabbit.
  % we can even override the item number by using []
  \item[how many?] Medium sized squirt guns.

  Not a list item, but still part of the enumerate.

\end{enumerate} % All environments must have an end.

\section{Math}

One of the primary uses for \LaTeX \hspace{1pt} is to produce academic articles
or technical papers. Usually in the realm of math and science. As such, 
we need to be able to add special symbols to our paper! \\

Math has many symbols, far beyond what you can find on a keyboard;
Set and relation symbols, arrows, operators, and Greek letters to name a few.\\

Sets and relations play a vital role in many mathematical research papers.
Here's how you state all x that belong to X, $\forall$ x $\in$ X. \\
% Notice how I needed to add $ signs before and after the symbols. This is 
% because when writing, we are in text-mode. 
% However, the math symbols only exist in math-mode. 
% We can enter math-mode from text mode with the $ signs.
% The opposite also holds true. Variable can also be rendered in math-mode.
% We can also enter math mode with \[\]

\[a^2 + b^2 = c^2 \]

My favorite Greek letter is $\xi$. I also like $\beta$, $\gamma$ and $\sigma$.
I haven't found a Greek letter yet that \LaTeX \hspace{1pt} doesn't know
about! \\

Operators are essential parts of a mathematical document: 
trigonometric functions ($\sin$, $\cos$, $\tan$), 
logarithms and exponentials ($\log$, $\exp$), 
limits ($\lim$), etc. 
have per-defined LaTeX commands. 
Let's write an equation to see how it's done: 
$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$ \\

Fractions (Numerator-denominators) can be written in these forms:

% 10 / 7
$$ ^{10}/_{7} $$

% Relatively complex fractions can be written as
% \frac{numerator}{denominator}
$$ \frac{n!}{k!(n - k)!} $$ \\

We can also insert equations in an ``equation environment''.

% Display math with the equation 'environment'
\begin{equation} % enters math-mode
    c^2 = a^2 + b^2.
    \label{eq:pythagoras} % for referencing
\end{equation} % all \begin statements must have an end statement

We can then reference our new equation! 
Eqn.~\ref{eq:pythagoras} is also known as the Pythagoras Theorem which is also
the subject of Sec.~\ref{subsec:pythagoras}. A lot of things can be labeled: 
figures, equations, sections, etc.

Summations and Integrals are written with sum and int commands:

% Some LaTeX compilers will complain if there are blank lines
% In an equation environment.
\begin{equation} 
  \sum_{i=0}^{5} f_{i}
\end{equation} 
\begin{equation} 
  \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x
\end{equation} 

\section{Figures}

Let's insert a Figure. Figure placement can get a little tricky. 
I definitely have to lookup the placement options each time.

\begin{figure}[H] % H here denoted the placement option. 
    \centering % centers the figure on the page
    % Inserts a figure scaled to 0.8 the width of the page.
    %\includegraphics[width=0.8\linewidth]{right-triangle.png} 
    % Commented out for compilation purposes. Please use your imagination.
    \caption{Right triangle with sides $a$, $b$, $c$}
    \label{fig:right-triangle}
\end{figure}

\subsection{Table}
We can also insert Tables in the same way as figures.

\begin{table}[H]
  \caption{Caption for the Table.}
  % the {} arguments below describe how each row of the table is drawn.
  % Again, I have to look these up. Each. And. Every. Time.
  \begin{tabular}{c|cc} 
    Number &  Last Name & First Name \\ % Column rows are separated by &
    \hline % a horizontal line
    1 & Biggus & Dickus \\
    2 & Monty & Python
  \end{tabular}
\end{table}

\section{Getting \LaTeX \hspace{1pt} to not compile something (i.e. Source Code)}
Let's say we want to include some code into our \LaTeX \hspace{1pt} document,
we would then need \LaTeX \hspace{1pt} to not try and interpret that text and
instead just print it to the document. We do this with a verbatim 
environment. 

% There are other packages that exist (i.e. minty, lstlisting, etc.)
% but verbatim is the bare-bones basic one.
\begin{verbatim} 
  print("Hello World!")
  a%b; % look! We can use % signs in verbatim. 
  random = 4; #decided by fair random dice roll
\end{verbatim}

\section{Compiling} 

By now you're probably wondering how to compile this fabulous document 
and look at the glorious glory that is a \LaTeX \hspace{1pt} pdf.
(yes, this document actually does compile). \\
Getting to the final document using \LaTeX \hspace{1pt} consists of the following 
steps:
  \begin{enumerate}
    \item Write the document in plain text (the ``source code'').
    \item Compile source code to produce a pdf. 
     The compilation step looks like this (in Linux): \\
     \begin{verbatim} 
        > pdflatex learn-latex.tex
     \end{verbatim}
  \end{enumerate}

A number of \LaTeX \hspace{1pt}editors combine both Step 1 and Step 2 in the 
same piece of software. So, you get to see Step 1, but not Step 2 completely.
Step 2 is still happening behind the scenes\footnote{In cases, where you use
references (like Eqn.~\ref{eq:pythagoras}), you may need to run Step 2
multiple times, to generate an intermediary *.aux file.}.
% Also, this is how you add footnotes to your document!

You write all your formatting information in plain text in Step 1.
The compilation part in Step 2 takes care of producing the document in the
format you defined in Step 1.

\section{Hyperlinks}
We can also insert hyperlinks in our document. To do so we need to include the
package hyperref into preamble with the command:
\begin{verbatim} 
    \usepackage{hyperref}
\end{verbatim}

There exists two main types of links: visible URL \\
\url{https://learnxinyminutes.com/docs/latex/}, or  
\href{https://learnxinyminutes.com/docs/latex/}{shadowed by text}
% You can not add extra-spaces or special symbols into shadowing text since it 
% will cause mistakes during the compilation

This package also produces list of tumbnails in the output pdf document and 
active links in the table of contents.

\section{End}

That's all for now!

% Most often, you would want to have a references section in your document.
% The easiest way to set this up would be by using the bibliography section
\begin{thebibliography}{1}
  % similar to other lists, the \bibitem command can be used to list items
  % each entry can then be cited directly in the body of the text
  \bibitem{latexwiki} The amazing \LaTeX \hspace{1pt} wikibook: {\em 
https://en.wikibooks.org/wiki/LaTeX}
  \bibitem{latextutorial} An actual tutorial: {\em http://www.latex-tutorial.com}
\end{thebibliography}

% end the document
\end{document}
```

## More on LaTeX

* The amazing LaTeX wikibook: [https://en.wikibooks.org/wiki/LaTeX](https://en.wikibooks.org/wiki/LaTeX)
* An actual tutorial: [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/)
---
language: less
filename: learnless.less
contributors:
  - ["Saravanan Ganesh", "http://srrvnn.me"]
---

Less is a CSS pre-processor, that adds features such as variables, nesting, mixins and more.
Less (and other preprocessors, such as [Sass](http://sass-lang.com/)) help developers to write maintainable and DRY (Don't Repeat Yourself) code.

```css


//Single line comments are removed when Less is compiled to CSS.

/*Multi line comments are preserved. */



/* Variables
==============================*/


/* You can store a CSS value (such as a color) in a variable.
   Use the '@' symbol to create a variable. */

@primary-color: #a3a4ff;
@secondary-color: #51527f;
@body-font: 'Roboto', sans-serif;

/* You can use the variables throughout your stylesheet.
   Now if you want to change a color, you only have to make the change once.*/

body {
	background-color: @primary-color;
	color: @secondary-color;
	font-family: @body-font;
}

/* This would compile to: */

body {
	background-color: #a3a4ff;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/* This is much more maintainable than having to change the color
   each time it appears throughout your stylesheet. */



/* Mixins
==============================*/


/* If you find you are writing the same code for more than one
   element, you might want to reuse that easily.*/

.center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* You can use the mixin by simply adding the selector as a style */

div {
	.center;
	background-color: @primary-color;
}

/* Which would compile to: */

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
}
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #a3a4ff;
}

/* You can omit the mixin code from being compiled by adding parenthesis
   after the selector */

.center() {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
}

div {
  .center;
  background-color: @primary-color;
}

/* Which would compile to: */
div {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
  background-color: #a3a4ff;
}



/* Nesting
==============================*/


/* Less allows you to nest selectors within selectors */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #f00;
	}
}

/* '&' will be replaced by the parent selector. */
/* You can also nest pseudo-classes. */
/* Keep in mind that over-nesting will make your code less maintainable.
   Best practices recommend going no more than 3 levels deep when nesting.
   For example: */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Compiles to: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/* Functions
==============================*/


/* Less provides functions that can be used to accomplish a variety of
   tasks. Consider the following: */

/* Functions can be invoked by using their name and passing in the
   required arguments. */

body {
  width: round(10.25px);
}

.header {
	background-color: lighten(#000, 0.5);
}

.footer {
  background-color: fadeout(#000, 0.25)
}

/* Compiles to: */

body {
  width: 10px;
}

.header {
  background-color: #010101;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* You may also define your own functions. Functions are very similar to
   mixins. When trying to choose between a function or a mixin, remember
   that mixins are best for generating CSS while functions are better for
   logic that might be used throughout your Less code. The examples in
   the 'Math Operators' section are ideal candidates for becoming a reusable
   function. */

/* This function calculates the average of two numbers: */

.average(@x, @y) {
  @average-result: ((@x + @y) / 2);
}

div {
  .average(16px, 50px); // "call" the mixin
  padding: @average-result;    // use its "return" value
}

/* Compiles to: */

div {
  padding: 33px;
}



/*Extend (Inheritance)
==============================*/


/*Extend is a way to share the properties of one selector with another. */

.display {
  height: 50px;
}

.display-success {
  &:extend(.display);
	border-color: #22df56;
}

/* Compiles to: */
.display,
.display-success {
  height: 50px;
}
.display-success {
  border-color: #22df56;
}

/* Extending a CSS statement is preferable to creating a mixin
   because of the way it groups together the classes that all share
   the same base styling. If this was done with a mixin, the properties
   would be duplicated for each statement that
   called the mixin. While it won't affect your workflow, it will
   add unnecessary bloat to the files created by the Less compiler. */



/*Partials and Imports
==============================*/


/* Less allows you to create partial files. This can help keep your Less
   code modularized. Partial files conventionally begin with an '_',
   e.g. _reset.less. and are imported into a main less file that gets
   compiled into CSS */

/* Consider the following CSS which we'll put in a file called _reset.less */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Less offers @import which can be used to import partials into a file.
   This differs from the traditional CSS @import statement which makes
   another HTTP request to fetch the imported file. Less takes the
   imported file and combines it with the compiled code. */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* Compiles to: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/* Math Operations
==============================*/


/* Less provides the following operators: +, -, *, /, and %. These can
   be useful for calculating values directly in your Less files instead
   of using values that you've already calculated by hand. Below is an example
   of a setting up a simple two column design. */

@content-area: 960px;
@main-content: 600px;
@sidebar-content: 300px;

@main-size: @main-content / @content-area * 100%;
@sidebar-size: @sidebar-content / @content-area * 100%;
@gutter: 100% - (@main-size + @sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: @main-size;
}

.sidebar {
  width: @sidebar-size;
}

.gutter {
  width: @gutter;
}

/* Compiles to: */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}


```

## Practice Less

If you want to play with Less in your browser, check out:
* [Codepen](http://codepen.io/)
* [LESS2CSS](http://lesscss.org/less-preview/)

## Compatibility

Less can be used in any project as long as you have a program to compile it into CSS. You'll want to verify that the CSS you're using is compatible with your target browsers.

[QuirksMode CSS](http://www.quirksmode.org/css/) and [CanIUse](http://caniuse.com) are great resources for checking compatibility.

## Further reading
* [Official Documentation](http://lesscss.org/features/)
* [Less CSS - Beginner's Guide](http://www.hongkiat.com/blog/less-basic/)
---

language: "Lisp Flavoured Erlang(LFE)"
filename: lispflavourederlang.lfe
contributors:
  - ["Pratik Karki", "https://github.com/prertik"]
---

Lisp Flavoured Erlang(LFE) is a functional, concurrent, general-purpose programming 
language and Lisp dialect(Lisp-2) built on top of Core Erlang and the Erlang Virtual Machine(BEAM). 

LFE can be obtained from [LFE](https://github.com/rvirding/lfe)

The classic starting point is [LFE DOCS.](http://docs.lfe.io)

Another new site is being built to replace it.[LFE DEV.](http://docs.lfe.io/dev)



```lisp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 0. Syntax
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; General form.

;; Lisp comprises of two syntax called: the ATOM and the S-expression.
;; `forms` are known as grouped S-expressions.

8  ; an atom; it evaluates to itself

:ERLANG ;Atom; evaluates to the symbol :ERLANG.

t  ; another atom which denotes true.

(* 2 21) ; an S- expression

'(8 :foo t)  ;another one


;;; Comments

;; Single line comments start with a semicolon; use two for normal
;; comments, three for section comments, and four fo file-level
;; comments.

;; Block Comment

   #| comment text |#

;;; Environment

;; LFE is the de-facto standard.

;; Libraries can be used directly from the Erlang ecosystem. Rebar3 is the build tool.

;; LFE is usually developed with a text editor(preferably Emacs) and a REPL
;; (Read Evaluate Print Loop) running at the same time. The REPL 
;; allows for interactive exploration of the program as it is "live"
;; in the system.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 1. Literals and Special Syntactic Rules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Integers

1234 -123	        ; Regular decimal notation
#b0 #b10101	        ; Binary notation
#0 #10101	        ; Binary notation (alternative form)
#o377 #o-111	    ; Octal notation
#d123456789 #d+123	; Explicitly decimal notation
#xc0ffe 0x-01	    ; Hexadecimal notation
#2r1010 #8r377 	    ;Notation with explicit base (up to 36)
#\a #$ #\ä #\🐭     ;Character notation (the value is the Unicode code point of the character)
#\x1f42d;	        ;Character notation with the value in hexadecimal

;;; Floating point numbers
1.0 +2.0 -1.5 1.0e10 1.111e-10     

;;; Strings

"any text between double quotes where \" and other special characters like \n can be escaped".
; List String
"Cat: \x1f639;" ; writing unicode in string for regular font ending with semicolon.

#"This is a binary string \n with some \"escaped\" and quoted (\x1f639;) characters"
; Binary strings are just strings but function different in the VM. 
; Other ways of writing it are:  #B("a"), #"a", and #B(97).


;;; Character escaping

\b	; => Backspace
\t	; => Tab
\n	; => Newline
\v	; => Vertical tab
\f	; => Form Feed
\r	; => Carriage Return
\e	; => Escape
\s	; => Space
\d	; => Delete

;;; Binaries
;; It is used to create binaries with any contents.
#B((#"a" binary) (#"b" binary))	               ; #"ab" (Evaluated form)

;;; Lists are: () or (foo bar baz)

;;; Tuples are written in: #(value1 value2 ...). Empty tuple #() is also valid.

;;; Maps are written as: #M(key1 value1 key2 value2 ...). Empty map #M() is also valid.

;;; Symbols: Things that cannot be parsed. Eg: foo, Foo, foo-bar, :foo
| foo | ; explicit construction of symbol by wrapping vertical bars.

;;; Evaluation 

;; #.(... some expression ...). E.g. '#.(+ 1 1) will evaluate the (+ 1 1) while it            ;; reads the expression and then be effectively '2.

;; List comprehension in LFE REPL

lfe> (list-comp
          ((<- x '(0 1 2 3)))
          (trunc (math:pow 3 x)))
       (1 3 9 27)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Core forms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; These forms are same as those found at Common Lisp and Scheme.

(quote e)
(cons head tail)
(car e)
(cdr e)
(list e ... )
(tuple e ... )
(binary seg ... )
(map key val ...), (map-get m k), (map-set m k v ...), (map-update m k v ...)

(lambda (arg ...) ...)
  (match-lambda
    ((arg ... ) {{(when e ...)}} ...) ; Matches clauses
    ... )
(let ((pat {{(when e ...)}} e)
      ...)
  ... )
(let-function ((name lambda|match-lambda) ; Only define local
               ... )                      ; functions
  ... )
(letrec-function ((name lambda|match-lambda) ; Only define local
                  ... )                      ; functions
  ... )
(let-macro ((name lambda-match-lambda) ; Only define local
            ...)                       ; macros
  ...)
(progn ... )
(if test true-expr {{false-expr}})
(case e
  (pat {{(when e ...)}} ...)
   ... ))
(receive
  (pat {{(when e ...)}} ... )
  ...
  (after timeout ... ))
(catch ... )
(try
  e
  {{(case ((pat {{(when e ...)}} ... )
          ... ))}}
  {{(catch
     ; Next must be tuple of length 3!
     (((tuple type value ignore) {{(when e ...)}}
      ... )
     ... )}}
  {{(after ... )}})

(funcall func arg ... )
(call mod func arg ... ) - Call to Erlang Mod:Func(Arg, ... )
(define-module name declaration ... )
(extend-module declaration ... ) - Define/extend module and declarations.
(define-function name lambda|match-lambda)
(define-macro name lambda|match-lambda) - Define functions/macros at top-level.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Macros are part of the language to allow you to create abstractions 
;; on top of the core language and standard library that move you closer 
;; toward being able to directly express the things you want to express.

;; Top-level function

(defun name (arg ...) ...)

;; Adding comments in functions

(defun name
  "Toplevel function with pattern-matching arguments"
  ((argpat ...) ...)
  ...)

;; Top-level macro

(defmacro name (arg ...) ...)
(defmacro name arg ...)

;; Top-level macro with pattern matching arguments

(defmacro name
  ((argpat ...) ...)
  ...)

;; Top-level macro using Scheme inspired syntax-rules format 

(defsyntax name
  (pat exp)
  ...)

;;; Local macros in macro or syntax-rule format

(macrolet ((name (arg ... ) ... )
            ... )
    ... )
    
(syntaxlet ((name (pat exp) ...)
             ...)
 ...)

;; Like CLISP

(prog1 ...)
(prog2 ...)

;; Erlang LFE module

(defmodule name ...)

;; Erlang LFE record

(defrecord name ...)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Patterns and Guards
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Using patterns in LFE compared to that of Erlang

;; Erlang                     ;; LFE
;; {ok, X}                       (tuple 'ok x)
;; error                         'error
;; {yes, [X|Xs]}                 (tuple 'yes (cons x xs))
;; <<34,F/float>>                (binary 34 (f float))
;; [P|Ps]=All                    (= (cons p ps) all)

  _    ; => is don't care while pattern matching
  
  (= pattern1 pattern2)     ; => easier, better version of pattern matching
  
;; Guards

;; Whenever pattern occurs(let, case, receive, lc, etc) it can be followed by an optional
;; guard which has the form (when test ...).

(progn gtest ...)             ;; => Sequence of guard tests
(if gexpr gexpr gexpr)
(type-test e)
(guard-bif ...)               ;; => Guard BIFs, arithmetic, boolean and comparison operators

;;; REPL

lfe>(set (tuple len status msg) #(8 ok "Trillian"))
    #(8 ok "Trillian")
lfe>msg
    "Trillian"

;;; Program illustrating use of Guards

(defun right-number?
        ((x) (when (orelse (== x 42) (== x 276709)))
          'true)
        ((_) 'false))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; A simple function using if.

(defun max (x y)
  "The max function."
  (if (>= x y) x y))

;; Same function using more clause

(defun max
  "The max function."
  ((x y) (when (>= x y)) x)
  ((x y) y))

;; Same function using similar style but using local functions defined by flet or fletrec

(defun foo (x y)
  "The max function."
  (flet ((m (a b) "Local comment."
            (if (>= a b) a b)))
    (m x y)))

;; LFE being Lisp-2 has separate namespaces for variables and functions
;; Both variables and function/macros are lexically scoped.
;; Variables are bound by lambda, match-lambda and let.
;; Functions are bound by top-level defun, flet and fletrec.
;; Macros are bound by top-level defmacro/defsyntax and by macrolet/syntaxlet.

;; (funcall func arg ...) like CL to call lambdas/match-lambdas 
;; (funs) bound to variables are used.

;; separate bindings and special for apply.
apply _F (...), 
apply _F/3 ( a1, a2, a3 )
    
;; Cons'ing in function heads
(defun sum (l) (sum l 0))
  (defun sum
    (('() total) total)
    (((cons h t) total) (sum t (+ h total))))
    
;; ``cons`` literal instead of constructor form
      (defun sum (l) (sum l 0))
      (defun sum
        (('() total) total)
        ((`(,h . ,t) total) (sum t (+ h total))))

;; Matching records in function heads

(defun handle_info
  (('ping (= (match-state remote-pid 'undefined) state))
    (gen_server:cast (self) 'ping)
    `#(noreply ,state))
  (('ping state)
   `#(noreply ,state)))

;; Receiving Messages
      (defun universal-server ()
        (receive
          ((tuple 'become func)
           (funcall func))))
           
;; another way for receiving messages

 (defun universal-server ()
        (receive
          (`#(become ,func)
            (funcall func))))

;; Composing a complete function for specific tasks

(defun compose (f g)
  (lambda (x)
   (funcall f
     (funcall g x))))

(defun check ()
  (let* ((sin-asin (compose #'sin/1 #'asin/1))
         (expected (sin (asin 0.5)))
         (compose-result (funcall sin-asin 0.5)))
    (io:format "Expected answer: ~p~n" (list expected))
    (io:format "Answer with compose: ~p~n" (list compose-result))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Concurrency
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Message passing as done by Erlang's light-weight "processes".

(defmodule messenger-back
 (export (print-result 0) (send-message 2)))

(defun print-result ()
  (receive
    ((tuple pid msg)
      (io:format "Received message: '~s'~n" (list msg))
      (io:format "Sending message to process ~p ...~n" (list pid))
      (! pid (tuple msg))
      (print-result))))

(defun send-message (calling-pid msg)
  (let ((spawned-pid (spawn 'messenger-back 'print-result ())))
    (! spawned-pid (tuple calling-pid msg))))
    
;; Multiple simultaneous HTTP Requests:

(defun parse-args (flag)
  "Given one or more command-line arguments, extract the passed values.

  For example, if the following was passed via the command line:

    $ erl -my-flag my-value-1 -my-flag my-value-2

  One could then extract it in an LFE program by calling this function:

    (let ((args (parse-args 'my-flag)))
      ...
      )
  In this example, the value assigned to the arg variable would be a list
  containing the values my-value-1 and my-value-2."
  (let ((`#(ok ,data) (init:get_argument flag)))
    (lists:merge data)))

(defun get-pages ()
  "With no argument, assume 'url parameter was passed via command line."
  (let ((urls (parse-args 'url)))
    (get-pages urls)))

(defun get-pages (urls)
  "Start inets and make (potentially many) HTTP requests."
  (inets:start)
  (plists:map
    (lambda (x)
      (get-page x)) urls))

(defun get-page (url)
  "Make a single HTTP request."
  (let* ((method 'get)
         (headers '())
         (request-data `#(,url ,headers))
         (http-options ())
         (request-options '(#(sync false))))
    (httpc:request method request-data http-options request-options)
    (receive
      (`#(http #(,request-id #(error ,reason)))
       (io:format "Error: ~p~n" `(,reason)))
      (`#(http #(,request-id ,result))
       (io:format "Result: ~p~n" `(,result))))))


;; Check out Erlang's documentation for more concurrency and OTP docs.
```

## Further Reading

*    [LFE DOCS](http://docs.lfe.io)
*    [LFE GitBook](https://lfe.gitbooks.io/reference-guide/index.html)
*    [LFE Wiki](https://en.wikipedia.org/wiki/LFE_(programming_language))

## Extra Info
*    [LFE PDF](http://www.erlang-factory.com/upload/presentations/61/Robertvirding-LispFlavouredErlang.pdf)
*    [LFE mail](https://groups.google.com/d/msg/lisp-flavoured-erlang/XA5HeLbQQDk/TUHabZCHXB0J)

## Credits

Lots of thanks to Robert Virding for creating LFE, Duncan McGreggor for documenting it and other LFE contributors who made LFE awesome.

---
language: LiveScript
filename: learnLivescript.ls
contributors:
    - ["Christina Whyte", "http://github.com/kurisuwhyte/"]
---

LiveScript is a functional compile-to-JavaScript language which shares
most of the underlying semantics with its host language. Nice additions
comes with currying, function composition, pattern matching and lots of
other goodies heavily borrowed from languages like Haskell, F# and
Scala.

LiveScript is a fork of [Coco][], which is itself a fork of
[CoffeeScript][]. The language is stable, and a new version is in active
development to bring a plethora of new niceties!

[Coco]: http://satyr.github.io/coco/
[CoffeeScript]: http://coffeescript.org/

Feedback is always welcome, so feel free to reach me over at
[@kurisuwhyte](https://twitter.com/kurisuwhyte) :)


```coffeescript
# Just like its CoffeeScript cousin, LiveScript uses number symbols for
# single-line comments.

/*
 Multi-line comments are written C-style. Use them if you want comments
 to be preserved in the JavaScript output.
 */
```
```coffeescript
# As far as syntax goes, LiveScript uses indentation to delimit blocks,
# rather than curly braces, and whitespace to apply functions, rather
# than parenthesis.


########################################################################
## 1. Basic values
########################################################################

# Lack of value is defined by the keyword `void` instead of `undefined`
void            # same as `undefined` but safer (can't be overridden)

# No valid value is represented by Null.
null


# The most basic actual value is the logical type:
true
false

# And it has a plethora of aliases that mean the same thing:
on; off
yes; no


# Then you get numbers. These are double-precision floats like in JS.
10
0.4     # Note that the leading `0` is required

# For readability, you may use underscores and letter suffixes in a
# number, and these will be ignored by the compiler.
12_344km


# Strings are immutable sequences of characters, like in JS:
"Christina"             # apostrophes are okay too!
"""Multi-line
   strings
   are
   okay
   too."""

# Sometimes you want to encode a keyword, the backslash notation makes
# this easy:
\keyword                # => 'keyword'


# Arrays are ordered collections of values.
fruits =
  * \apple
  * \orange
  * \pear

# They can be expressed more concisely with square brackets:
fruits = [ \apple, \orange, \pear ]

# You also get a convenient way to create a list of strings, using
# white space to delimit the items.
fruits = <[ apple orange pear ]>

# You can retrieve an item by their 0-based index:
fruits[0]       # => "apple"

# Objects are a collection of unordered key/value pairs, and a few other
# things (more on that later).
person =
  name: "Christina"
  likes:
    * "kittens"
    * "and other cute stuff"

# Again, you can express them concisely with curly brackets:
person = {name: "Christina", likes: ["kittens", "and other cute stuff"]}

# You can retrieve an item by their key:
person.name     # => "Christina"
person["name"]  # => "Christina"


# Regular expressions use the same syntax as JavaScript:
trailing-space = /\s$/          # dashed-words become dashedWords

# Except you can do multi-line expressions too!
# (comments and whitespace just gets ignored)
funRE = //
        function\s+(.+)         # name
        \s* \((.*)\) \s*        # arguments
        { (.*) }                # body
        //


########################################################################
## 2. Basic operations
########################################################################

# Arithmetic operators are the same as JavaScript's:
1 + 2   # => 3
2 - 1   # => 1
2 * 3   # => 6
4 / 2   # => 2
3 % 2   # => 1


# Comparisons are mostly the same too, except that `==` is the same as
# JS's `===`, where JS's `==` in LiveScript is `~=`, and `===` enables
# object and array comparisons, and also stricter comparisons:
2 == 2          # => true
2 == "2"        # => false
2 ~= "2"        # => true
2 === "2"       # => false

[1,2,3] == [1,2,3]        # => false
[1,2,3] === [1,2,3]       # => true

+0 == -0     # => true
+0 === -0    # => false

# Other relational operators include <, <=, > and >=

# Logical values can be combined through the logical operators `or`,
# `and` and `not`
true and false  # => false
false or true   # => true
not false       # => true


# Collections also get some nice additional operators
[1, 2] ++ [3, 4]                # => [1, 2, 3, 4]
'a' in <[ a b c ]>              # => true
'name' of { name: 'Chris' }     # => true


########################################################################
## 3. Functions
########################################################################

# Since LiveScript is functional, you'd expect functions to get a nice
# treatment. In LiveScript it's even more apparent that functions are
# first class:
add = (left, right) -> left + right
add 1, 2        # => 3

# Functions which take no arguments are called with a bang!
two = -> 2
two!

# LiveScript uses function scope, just like JavaScript, and has proper
# closures too. Unlike JavaScript, the `=` works as a declaration
# operator, and will always declare the variable on the left hand side.

# The `:=` operator is available to *reuse* a name from the parent
# scope.


# You can destructure arguments of a function to quickly get to
# interesting values inside a complex data structure:
tail = ([head, ...rest]) -> rest
tail [1, 2, 3]  # => [2, 3]

# You can also transform the arguments using binary or unary
# operators. Default arguments are also possible.
foo = (a = 1, b = 2) -> a + b
foo!    # => 3

# You could use it to clone a particular argument to avoid side-effects,
# for example:
copy = (^^target, source) ->
  for k,v of source => target[k] = v
  target
a = { a: 1 }
copy a, { b: 2 }        # => { a: 1, b: 2 }
a                       # => { a: 1 }


# A function may be curried by using a long arrow rather than a short
# one:
add = (left, right) --> left + right
add1 = add 1
add1 2          # => 3

# Functions get an implicit `it` argument, even if you don't declare
# any.
identity = -> it
identity 1      # => 1

# Operators are not functions in LiveScript, but you can easily turn
# them into one! Enter the operator sectioning:
divide-by-two = (/ 2)
[2, 4, 8, 16].map(divide-by-two) .reduce (+)


# Not only of function application lives LiveScript, as in any good
# functional language you get facilities for composing them:
double-minus-one = (- 1) . (* 2)

# Other than the usual `f . g` mathematical formulae, you get the `>>`
# and `<<` operators, that describe how the flow of values through the
# functions.
double-minus-one = (* 2) >> (- 1)
double-minus-one = (- 1) << (* 2)


# And talking about flow of value, LiveScript gets the `|>` and `<|`
# operators that apply a value to a function:
map = (f, xs) --> xs.map f
[1 2 3] |> map (* 2)            # => [2 4 6]

# You can also choose where you want the value to be placed, just mark
# the place with an underscore (_):
reduce = (f, xs, initial) --> xs.reduce f, initial
[1 2 3] |> reduce (+), _, 0     # => 6


# The underscore is also used in regular partial application, which you
# can use for any function:
div = (left, right) -> left / right
div-by-two = div _, 2
div-by-two 4      # => 2


# Last, but not least, LiveScript has back-calls, which might help
# with some callback-based code (though you should try more functional
# approaches, like Promises):
readFile = (name, f) -> f name
a <- readFile 'foo'
b <- readFile 'bar'
console.log a + b

# Same as:
readFile 'foo', (a) -> readFile 'bar', (b) -> console.log a + b


########################################################################
## 4. Patterns, guards and control-flow
########################################################################

# You can branch computations with the `if...else` expression:
x = if n > 0 then \positive else \negative

# Instead of `then`, you can use `=>`
x = if n > 0 => \positive
    else        \negative

# Complex conditions are better-off expressed with the `switch`
# expression, though:
y = {}
x = switch
  | (typeof y) is \number => \number
  | (typeof y) is \string => \string
  | 'length' of y         => \array
  | otherwise             => \object      # `otherwise` and `_` always matches.

# Function bodies, declarations and assignments get a free `switch`, so
# you don't need to type it again:
take = (n, [x, ...xs]) -->
                        | n == 0 => []
                        | _      => [x] ++ take (n - 1), xs


########################################################################
## 5. Comprehensions
########################################################################

# While the functional helpers for dealing with lists and objects are
# right there in the JavaScript's standard library (and complemented on
# the prelude-ls, which is a "standard library" for LiveScript),
# comprehensions will usually allow you to do this stuff faster and with
# a nice syntax:
oneToTwenty = [1 to 20]
evens       = [x for x in oneToTwenty when x % 2 == 0]

# `when` and `unless` can be used as filters in the comprehension.

# Object comprehension works in the same way, except that it gives you
# back an object rather than an Array:
copy = { [k, v] for k, v of source }


########################################################################
## 4. OOP
########################################################################

# While LiveScript is a functional language in most aspects, it also has
# some niceties for imperative and object oriented programming. One of
# them is class syntax and some class sugar inherited from CoffeeScript:
class Animal
  (@name, kind) ->
    @kind = kind
  action: (what) -> "*#{@name} (a #{@kind}) #{what}*"

class Cat extends Animal
  (@name) -> super @name, 'cat'
  purr: -> @action 'purrs'

kitten = new Cat 'Mei'
kitten.purr!      # => "*Mei (a cat) purrs*"

# Besides the classical single-inheritance pattern, you can also provide
# as many mixins as you would like for a class. Mixins are just plain
# objects:
Huggable =
  hug: -> @action 'is hugged'

class SnugglyCat extends Cat implements Huggable

kitten = new SnugglyCat 'Purr'
kitten.hug!     # => "*Mei (a cat) is hugged*"
```

## Further reading

There's just so much more to LiveScript, but this should be enough to
get you started writing little functional things in it. The
[official website](http://livescript.net/) has a lot of information on the
language, and a nice online compiler for you to try stuff out!

You may also want to grab yourself some
[prelude.ls](http://gkz.github.io/prelude-ls/), and check out the `#livescript`
channel on the Freenode network.
---
language: Logtalk
contributors:
    - ["Paulo Moura", "http://github.com/pmoura"]
filename: learnlogtalk.lgt
---

Logtalk is an object-oriented logic programming language that extends and leverages Prolog with modern code encapsulation and code reuse mechanisms without compromising its declarative programming features. Logtalk is implemented in highly portable code and can use most modern and standards compliant Prolog implementations as a back-end compiler.

To keep its size reasonable, this tutorial necessarily assumes that the reader have a working knowledge of Prolog and is biased towards describing Logtalk object-oriented features.

# Syntax

Logtalk uses standard Prolog syntax with the addition of a few operators and directives for a smooth learning curve and wide portability. One important consequence is that Prolog code can be easily encapsulated in objects with little or no changes. Moreover, Logtalk can transparently interpret most Prolog modules as Logtalk objects.

The main operators are:

* `::/2` - sending a message to an object
* `::/1` - sending a message to _self_ (i.e. to the object that received the message being processed)
* `^^/1` - _super_ call (of an inherited or imported predicate)

Some of the most important entity and predicate directives will be introduced in the next sections.

# Entities and roles

Logtalk provides _objects_, _protocols_, and _categories_ as first-class entities. Relations between entities define _patterns of code reuse_ and the _roles_ played by the entities. For example, when an object _instantiates_ another object, the first object plays the role of an instance and the second object plays the role of a class. An _extends_ relation between two objects implies that both objects play the role of prototypes, with one of them extending the other, its parent prototype.

# Defining an object

An object encapsulates predicate declarations and definitions. Objects can be created dynamically but are usually static and defined in source files. A single source file can contain any number of entity definitions. A simple object, defining a list member public predicate:

```logtalk
:- object(list).

	:- public(member/2).
	member(Head, [Head| _]).
	member(Head, [_| Tail]) :-
		member(Head, Tail).

:- end_object.
```

# Compiling source files

Assuming that the code above for the `list` object is saved in a `list.lgt` file, it can be compiled and loaded using the `logtalk_load/1` built-in predicate or its abbreviation, `{}/1`, with the file path as argument (the extension can be omitted):

```logtalk
?- {list}.
yes
```

# Sending a message to an object

The `::/2` infix operator is used to send a message to an object. As in Prolog, we can backtrack for alternative solutions:

```logtalk
?- list::member(X, [1,2,3]).
X = 1 ;
X = 2 ;
X = 3
yes
```

Encapsulation is enforced. A predicate can be declared _public_, _protected_, or _private_. It can also be _local_ when there is no scope directive for it. For example:

```logtalk
:- object(scopes).

	:- private(bar/0).
	bar.

	local.

:- end_object.
```

Assuming the object is saved in a `scopes.lgt` file:

```logtalk
?- {scopes}.
yes

?- catch(scopes::bar, Error, true).
Error = error(
	permission_error(access, private_predicate, bar/0),
	logtalk(scopes::bar, user)
)
yes

?- catch(scopes::local, Error, true).
Error = error(
	existence_error(predicate_declaration, local/0),
	logtalk(scopes::local, user)
)
yes
```

When the predicate in a message is unknown for the object (the role it plays determines the lookup procedures), we also get an error. For example:

```logtalk
?- catch(scopes::unknown, Error, true).
Error = error(
	existence_error(predicate_declaration, unknown/0),
	logtalk(scopes::unknown, user)
)
yes
```

A subtle point is that predicate scope directives specify predicate _calling_ semantics, not _definition_ semantics. For example, if an object playing the role of a class declares a predicate private, the predicate can be defined in subclasses and instances *but* can only be called in its instances _from_ the class.

# Defining and implementing a protocol

Protocols contain predicate declarations that can be implemented by any number of objects and categories:

```logtalk
:- protocol(listp).

	:- public(member/2).

:- end_protocol.

:- object(list,
	implements(listp)).

	member(Head, [Head| _]).
	member(Head, [_| Tail]) :-
		member(Head, Tail).

:- end_object.
```

The scope of the protocol predicates can be restricted using protected or private implementation. For example:

```logtalk
:- object(stack,
	implements(private::listp)).

:- end_object.
```

In fact, all entity relations (in an entity opening directive) can be qualified as public (the default), protected, or private.

# Prototypes

An object without an _instantiation_ or _specialization_ relation with another object plays the role of a prototype. A prototype can _extend_ another object, its parent prototype.

```logtalk
% clyde, our prototypical elephant
:- object(clyde).

	:- public(color/1).
	color(grey).

	:- public(number_of_legs/1).
	number_of_legs(4).

:- end_object.

% fred, another elephant, is like clyde, except that he's white
:- object(fred,
	extends(clyde)).

	color(white).

:- end_object.
```

When answering a message sent to an object playing the role of a prototype, we validate the message and look for an answer first in the prototype itself and, if not found, we delegate to the prototype parents if any:

```logtalk
?- fred::number_of_legs(N).
N = 4
yes

?- fred::color(C).
C = white
yes
```

A message is valid if the corresponding predicate is declared (and the sender is within scope) but it will fail, rather then throwing an error, if the predicate is not defined. This is called the _closed-world assumption_. For example, consider the following object, saved in a `foo.lgt` file:

```logtalk
:- object(foo).

	:- public(bar/0).

:- end_object.
```

Loading the file and trying to call the `bar/0` predicate fails as expected. Note that this is different from calling an _unknown_ predicate, which results in an error:

```logtalk
?- {foo}.
yes

?- foo::bar.
no

?- catch(foo::baz, Error, true).
Error = error(
	existence_error(predicate_declaration, baz/0),
	logtalk(foo::baz, user)
)
yes
```

# Classes and instances

In order to define objects playing the role of classes and/or instances, an object must have at least an instantiation or a specialization relation with another object. Objects playing the role of meta-classes can be used when we need to see a class also as an instance. We use the following example to also illustrate how to dynamically create new objects at runtime:

```logtalk
% a simple, generic, metaclass defining a new/2 predicate for its instances
:- object(metaclass,
	instantiates(metaclass)).

	:- public(new/2).
	new(Instance, Clauses) :-
		self(Class),
		create_object(Instance, [instantiates(Class)], [], Clauses).

:- end_object.

% a simple class defining age/1 and name/1 predicate for its instances
:- object(person,
	instantiates(metaclass)).

	:- public([
		age/1, name/1
	]).

	% a default value for age/1
	age(42).

:- end_object.

% a static instance of the class person
:- object(john,
	instantiates(person)).

	name(john).
	age(12).

:- end_object.
```

When answering a message sent to an object playing the role of an instance, we validate the message by starting in its class and going up to its class superclasses if necessary. Assuming that the message is valid, then we look for an answer starting in the instance itself:

```logtalk
?- person::new(Instance, [name(paulo)]).
Instance = o1
yes

?- o1::name(Name).
Name = paulo
yes

?- o1::age(Age).
Age = 42
yes

?- john::age(Age).
Age = 12
yes
```

# Categories

A category is a fine grained unit of code reuse, used to encapsulate a _cohesive_ set of predicate declarations and definitions, implementing a _single_ functionality, that can be imported into any object. A category can thus be seen as the dual concept of a protocol. In the following example, we define categories representing car engines and then import them into car objects:

```logtalk
% a protocol describing engine characteristics
:- protocol(carenginep).

	:- public([
		reference/1,
		capacity/1,
		cylinders/1,
		horsepower_rpm/2,
		bore_stroke/2,
		fuel/1
	]).

:- end_protocol.

% a typical engine defined as a category
:- category(classic,
	implements(carenginep)).

	reference('M180.940').
	capacity(2195).
	cylinders(6).
	horsepower_rpm(94, 4800).
	bore_stroke(80, 72.8).
	fuel(gasoline).

:- end_category.

% a souped up version of the previous engine
:- category(sport,
	extends(classic)).

	reference('M180.941').
	horsepower_rpm(HP, RPM) :-
		^^horsepower_rpm(ClassicHP, ClassicRPM),	% "super" call
		HP is truncate(ClassicHP*1.23),
		RPM is truncate(ClassicRPM*0.762).

:- end_category.

% with engines (and other components), we may start "assembling" some cars
:- object(sedan,
	imports(classic)).

:- end_object.

:- object(coupe,
	imports(sport)).

:- end_object.
```

Categories are independently compiled and thus allow importing objects to be updated by simple updating the imported categories without requiring object recompilation. Categories also provide _runtime transparency_. I.e. the category protocol adds to the protocol of the objects importing the category:

```logtalk
?- sedan::current_predicate(Predicate).
Predicate = reference/1 ;
Predicate = capacity/1 ;
Predicate = cylinders/1 ;
Predicate = horsepower_rpm/2 ;
Predicate = bore_stroke/2 ;
Predicate = fuel/1
yes
```

# Hot patching

Categories can be also be used for hot-patching objects. A category can add new predicates to an object and/or replace object predicate definitions. For example, consider the following object:

```logtalk
:- object(buggy).

	:- public(p/0).
	p :- write(foo).

:- end_object.
```

Assume that the object prints the wrong string when sent the message `p/0`:

```logtalk
?- {buggy}.
yes

?- buggy::p.
foo
yes
```

If the object source code is not available and we need to fix an application running the object code, we can simply define a category that fixes the buggy predicate:

```logtalk
:- category(patch,
	complements(buggy)).

	% fixed p/0 def
	p :- write(bar).

:- end_category.
```

After compiling and loading the category into the running application we will now get:

```logtalk
?- {patch}.
yes

?- buggy::p.
bar
yes
```

As hot-patching forcefully breaks encapsulation, there is a `complements` compiler flag that can be set (globally or on a per-object basis) to allow, restrict, or prevent it.

# Parametric objects and categories

Objects and categories can be parameterized by using as identifier a compound term instead of an atom. Object and category parameters are _logical variables_ shared with all encapsulated predicates. An example with geometric circles:

```logtalk
:- object(circle(_Radius, _Color)).

	:- public([
		area/1, perimeter/1
	]).

	area(Area) :-
		parameter(1, Radius),
		Area is pi*Radius*Radius.

	perimeter(Perimeter) :-
		parameter(1, Radius),
		Perimeter is 2*pi*Radius.

:- end_object.
```

Parametric objects are used just as any other object, usually providing values for the parameters when sending a message:

```logtalk
?- circle(1.23, blue)::area(Area).
Area = 4.75291
yes
```

Parametric objects also provide a simple way of associating a set of predicates with a plain Prolog predicate. Prolog facts can be interpreted as _parametric object proxies_ when they have the same functor and arity as the identifiers of parametric objects. Handy syntax is provided to for working with proxies. For example, assuming the following clauses for a `circle/2` predicate:

```logtalk
circle(1.23, blue).
circle(3.71, yellow).
circle(0.39, green).
circle(5.74, black).
circle(8.32, cyan).
```

With these clauses loaded, we can easily compute for example a list with the areas of all the circles:

```logtalk
?- findall(Area, {circle(_, _)}::area(Area), Areas).
Areas = [4.75291, 43.2412, 0.477836, 103.508, 217.468]
yes
```

The `{Goal}::Message` construct proves `Goal`, possibly instantiating any variables in it, and sends `Message` to the resulting term.

# Events and monitors

Logtalk supports _event-driven programming_ by allowing defining events and monitors for those events. An event is simply the sending of a message to an object. Interpreting message sending as an atomic activity, a _before_ event and an _after_ event are recognized. Event monitors define event handler predicates, `before/3` and `after/3`, and can query, register, and delete a system-wide event registry that associates events with monitors. For example, a simple tracer for any message being sent using the `::/2` control construct can be defined as:

```logtalk
:- object(tracer,
	implements(monitoring)).    % built-in protocol for event handlers

	:- initialization(define_events(_, _, _, _, tracer)).

	before(Object, Message, Sender) :-
		write('call: '), writeq(Object), write(' <-- '), writeq(Message),
		write(' from '), writeq(Sender), nl.

	after(Object, Message, Sender) :-
		write('exit: '), writeq(Object), write(' <-- '), writeq(Message),
		write(' from '), writeq(Sender), nl.

:- end_object.
```

Assuming that the `tracer` object and the `list` object defined earlier are compiled and loaded, we can observe the event handlers in action by sending a message:

```logtalk
?- list::member(X, [1,2,3]).

call: list <-- member(X, [1,2,3]) from user
exit: list <-- member(1, [1,2,3]) from user
X = 1 ;
exit: list <-- member(2, [1,2,3]) from user
X = 2 ;
exit: list <-- member(3, [1,2,3]) from user
X = 3
yes
```

Events can be set and deleted dynamically at runtime by calling the `define_events/5` and `abolish_events/5` built-in predicates.

Event-driven programming can be seen as a form of _computational reflection_. But note that events are only generated when using the `::/2` message-sending control construct.

# Lambda expressions

Logtalk supports lambda expressions. Lambda parameters are represented using a list with the `(>>)/2` infix operator connecting them to the lambda. Some simple examples using library meta-predicates:

```logtalk
?- {library(metapredicates_loader)}.
yes

?- meta::map([X,Y]>>(Y is 2*X), [1,2,3], Ys).
Ys = [2,4,6]
yes
```

Currying is also supported:

```logtalk
?- meta::map([X]>>([Y]>>(Y is 2*X)), [1,2,3], Ys).
Ys = [2,4,6]
yes
```

Lambda free variables can be expressed using the extended syntax `{Free1, ...}/[Parameter1, ...]>>Lambda`.

# Macros

Terms and goals in source files can be _expanded_ at compile time by specifying a _hook object_ that defines term-expansion and goal-expansion rules. For example, consider the following simple object, saved in a `source.lgt` file:

```logtalk
:- object(source).

	:- public(bar/1).
	bar(X) :- foo(X).

	foo(a). foo(b). foo(c).

:- end_object.
```

Assume the following hook object, saved in a `my_macros.lgt` file, that expands clauses and calls to the `foo/1` local predicate:

```logtalk
:- object(my_macros,
	implements(expanding)).    % built-in protocol for expanding predicates

	term_expansion(foo(Char), baz(Code)) :-
		char_code(Char, Code). % standard built-in predicate

	goal_expansion(foo(X), baz(X)).

:- end_object.
```

After loading the macros file, we can then expand our source file with it using the `hook` compiler flag:

```logtalk
?- logtalk_load(my_macros), logtalk_load(source, [hook(my_macros)]).
yes

?- source::bar(X).
X = 97 ;
X = 98 ;
X = 99
true
```

The Logtalk library provides support for combining hook objects using different workflows (for example, defining a pipeline of expansions).

# Further information

Visit the [Logtalk website](http://logtalk.org) for more information.
---
language: LOLCODE
filename: learnLOLCODE.lol
contributors:
    - ["abactel", "https://github.com/abactel"]
---

LOLCODE is an esoteric programming language designed to resemble the speech of [lolcats](https://upload.wikimedia.org/wikipedia/commons/a/ab/Lolcat_in_folder.jpg?1493656347257).

```
BTW This is an inline comment
BTW All code must begin with `HAI <language version>` and end with `KTHXBYE`

HAI 1.3
CAN HAS STDIO? BTW Importing standard headers

OBTW
     ==========================================================================
     ================================= BASICS =================================
     ==========================================================================
TLDR

BTW Displaying text:
VISIBLE "HELLO WORLD"

BTW Declaring variables:
I HAS A MESSAGE ITZ "CATZ ARE GOOD"
VISIBLE MESSAGE

OBTW
    (This is a codeblock.) Variables are dynamically typed so you don't need to
    declare their type. A variable's type matches its content. These are the
    types:
TLDR

I HAS A STRING  ITZ "DOGZ ARE GOOOD" BTW type is YARN
I HAS A INTEGER ITZ 42               BTW type is NUMBR
I HAS A FLOAT   ITZ 3.1415           BTW type is NUMBAR
I HAS A BOOLEAN ITZ WIN              BTW type is TROOF
I HAS A UNTYPED                      BTW type is NOOB

BTW Accepting user input:
I HAS A AGE
GIMMEH AGE
BTW The variable is stored as a YARN. To convert it into NUMBR:
AGE IS NOW A NUMBR

OBTW
     ==========================================================================
     ================================== MATH ==================================
     ==========================================================================
TLDR

BTW LOLCODE uses polish notation style math.

BTW Basic mathematical notation:

SUM OF 21 AN 33         BTW 21 + 33
DIFF OF 90 AN 10        BTW 90 - 10
PRODUKT OF 12 AN 13     BTW 12 * 13
QUOSHUNT OF 32 AN 43    BTW 32 / 43
MOD OF 43 AN 64         BTW 43 modulo 64
BIGGR OF 23 AN 53       BTW max(23, 53)
SMALLR OF 53 AN 45      BTW min(53, 45)

BTW Binary notation:

BOTH OF WIN AN WIN           BTW and: WIN if x=WIN, y=WIN
EITHER OF FAIL AN WIN        BTW or: FAIL if x=FAIL, y=FAIL
WON OF WIN AN FAIL           BTW xor: FAIL if x=y
NOT FAIL                     BTW unary negation: WIN if x=FAIL
ALL OF WIN AN WIN MKAY   BTW infinite arity AND
ANY OF WIN AN FAIL MKAY  BTW infinite arity OR

BTW Comparison:

BOTH SAEM "CAT" AN "DOG"             BTW WIN if x == y
DIFFRINT 732 AN 184                  BTW WIN if x != y
BOTH SAEM 12 AN BIGGR OF 12 AN 4     BTW x >= y
BOTH SAEM 43 AN SMALLR OF 43 AN 56   BTW x <= y
DIFFRINT 64 AN SMALLR OF 64 AN 2     BTW x > y
DIFFRINT 75 AN BIGGR OF 75 AN 643    BTW x < y

OBTW
     ==========================================================================
     ============================== FLOW CONTROL ==============================
     ==========================================================================
TLDR

BTW If/then statement:
I HAS A ANIMAL
GIMMEH ANIMAL
BOTH SAEM ANIMAL AN "CAT", O RLY?
    YA RLY
        VISIBLE "YOU HAV A CAT"
    MEBBE BOTH SAEM ANIMAL AN "MAUS"
        VISIBLE "NOM NOM NOM. I EATED IT."
    NO WAI
        VISIBLE "AHHH IS A WOOF WOOF"
OIC

BTW Case statement:
I HAS A COLOR
GIMMEH COLOR
COLOR, WTF?
    OMG "R"
        VISIBLE "RED FISH"
        GTFO
    OMG "Y"
        VISIBLE "YELLOW FISH"
        BTW Since there is no `GTFO` the next statements will also be tested
    OMG "G"
    OMG "B"
        VISIBLE "FISH HAS A FLAVOR"
        GTFO
    OMGWTF
        VISIBLE "FISH IS TRANSPARENT OHNO WAT"
OIC

BTW For loop:
I HAS A TEMPERATURE
GIMMEH TEMPERATURE
TEMPERATURE IS NOW A NUMBR
IM IN YR LOOP UPPIN YR ITERATOR TIL BOTH SAEM ITERATOR AN TEMPERATURE
    VISIBLE ITERATOR
IM OUTTA YR LOOP

BTW While loop:
IM IN YR LOOP NERFIN YR ITERATOR WILE DIFFRINT ITERATOR AN -10
    VISIBLE ITERATOR
IM OUTTA YR LOOP

OBTW
     =========================================================================
     ================================ Strings ================================
     =========================================================================
TLDR

BTW Linebreaks:
VISIBLE "FIRST LINE :) SECOND LINE"

BTW Tabs:
VISIBLE ":>SPACES ARE SUPERIOR"

BTW Bell (goes beep):
VISIBLE "NXT CUSTOMER PLS :o"

BTW Literal double quote:
VISIBLE "HE SAID :"I LIKE CAKE:""

BTW Literal colon:
VISIBLE "WHERE I LIVE:: CYBERSPACE"

OBTW
     =========================================================================
     =============================== FUNCTIONS ===============================
     =========================================================================
TLDR

BTW Declaring a new function:
HOW IZ I SELECTMOVE YR MOVE BTW `MOVE` is an argument
    BOTH SAEM MOVE AN "ROCK", O RLY?
        YA RLY
            VISIBLE "YOU HAV A ROCK"
        NO WAI
            VISIBLE "OH NO IS A SNIP-SNIP"
    OIC
    GTFO BTW This returns NOOB
IF U SAY SO

BTW Declaring a function and returning a value:
HOW IZ I IZYELLOW
    FOUND YR "YELLOW"
IF U SAY SO

BTW Calling a function:
I IZ IZYELLOW MKAY

KTHXBYE
```

## Further reading:

-   [LCI compiler](https://github.com/justinmeza/lci)
-   [Official spec](https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md)
---
language: json
filename: learnjson-lt.json
lang: lt-lt
contributors:
  - ["Zygimantus", "https://github.com/zygimantus"]
---

JSON („džeisonas“) yra itin paprastas duomenų mainų formatas, todėl tai bus pati lengviausia „Learn X in Y Minutes“ pamoka.

JSON savo gryniausioje formoje neturi jokių komentarų, tačiau dauguma analizatorių priimtų C stiliaus komentarus (`//`, `/* */`). Kai kurie analizatoriai taip pat toleruoja gale esantį kablelį, pvz., kablelis po kiekvieno masyvo paskutinio elemento arba po paskutinio objekto lauko, tačiau jų reikėtų vengti dėl geresnio suderinamumo.

JSON reikšmė privalo būti skaičius, eilutė, masyvas, objektas arba viena reikšmė iš šių: true, false, null.

Palaikančios naršyklės yra: Firefox 3.5+, Internet Explorer 8.0+, Chrome 1.0+, Opera 10.0+, and Safari 4.0+.

Failo plėtinys JSON failams yra „.json“, o MIME tipas yra „application/json“.

Dauguma programavimo kalbų palaiko JSON duomenų serializaciją (kodavimą) ir deserializaciją (dekodavimą) į natyviasias duomenų struktūras. Javascript turi visišką JSON teksto kaip duomenų manipuliavimo palaikymą.

Daugiau informacijos galima rasti http://www.json.org/

JSON yra pastatytas iš dviejų struktūrų:
* Vardų/reikšmių porų rinkinys. Daugomoje kalbų, tai yra realizuojama kaip objektas, įrašas, struktūra, žodynas, hash lentelė, sąrašas su raktais arba asociatyvusis masyvas.
* Rūšiuotas reikšmių sąrašas. Daugumoje kalbų, toks sąrašas yra realizuojama kaip masyvas, vektorius, sąrašas arba seka.

Objektas su įvairiomis vardo/reikšmės poromis.

```json
{
  "raktas": "reikšmė",

  "raktai": "privalo visada būti uždaryti dvigubomis kabutėmis",
  "skaičiai": 0,
  "eilutės": "Labas, pasauli. Visas unikodas yra leidžiamas, kartu su  \"vengimu\".",
  "turi logiką?": true,
  "niekas": null,

  "didelis skaičius": 1.2e+100,

  "objektai": {
    "komentaras": "Dauguma tavo struktūrų ateis iš objektų.",

    "masyvas": [0, 1, 2, 3, "Masyvas gali turėti bet ką savyje.", 5],

    "kitas objektas": {
      "komentaras": "Šie dalykai gali būti įdedami naudingai."
    }
  },

  "kvailumas": [
    {
      "kalio šaltiniai": ["bananai"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "alternativus stilius": {
    "komentaras": "tik pažiūrėk!"
  , "kablelio padėti": "nesvarbi - kol jis prieš kitą raktą, tada teisingas"
  , "kitas komentaras": "kaip gražu"
  }
}
```

Paprastas reikšmių masyvas pats savaime yra galiojantis JSON.

```json
[1, 2, 3, "tekstas", true]
```

Objektai taip pat gali būti masyvų dalis.

```json
[{"vardas": "Jonas", "amžius": 25}, {"vardas": "Eglė", "amžius": 29}, {"vardas": "Petras", "amžius": 31}]
```
---
category: tool
tool: tmux
filename: tmux-lt.json
contributors:
    - ["mdln", "https://github.com/mdln"]
translators:
    - ["Zygimantus", "https://github.com/zygimantus"]
lang: lt-lt

---


[tmux](http://tmux.sourceforge.net)
yra terminalo daugintuvas: jis leidžia vienu metu sukurti, turėti
ir valdyti kelis terminalus viename ekrane. tmux gali būti atjungtas
nuo ekrano ir veikti fone, o vėliau gali būti vėl prijungtas.


```

  tmux [komanda]     # Vykdyti komandą
                     # 'tmux' be komandų sukurs naują sesiją

    new              # Sukurti naują sesiją
     -s "Session"    # Sukurti pavadintą sesiją
     -n "Window"     # Sukurti pavadintą langą
     -c "/dir"       # Pradėti nurodytoje direktorijoje

    attach           # Priskirti paskutinę/prienamą sesiją
     -t "#"          # Priskirti nurodytą sesiją
     -d              # Atjungti sesiją nuo kitų langų

    ls               # Aktyvių sesijų sąrašas
     -a              # Visų aktyvių sesijų sąrašas

    lsw              # Langų sąrašas
     -a              # Visų langų sąrašas
     -s              # Visų langų sesijoje sąrašas

    lsp              # Skydelių sąrašas
     -a              # Visų skydelių sąrašas
     -s              # Visų skydelių sesijoje sąrašas
     -t              # Visų skydelių taikinyje sąrašas

    kill-window      # Užbaigti dabartinį langą
     -t "#"          # Užbaigti nurodytą langą
     -a              # Užbaigti visus langus
     -a -t "#"       # Užbaigti visus langus, bet ne taikinį

    kill-session     # Užbaigti dabartinę sesiją
     -t "#"          # Užbaigti nurodytą sesiją
     -a              # Užbaigti visas sesijas
     -a -t "#"       # Užbaigti visas sesijas, bet ne taikinį

```


### Klavišai

Priskirta tmux sesija yra valdoma klavišų kompinacijomis.

```
----------------------------------------------------------------------
  (C-b) = Ctrl + b    # Kombinacija reikalinga norint naudoti klavišus

  (M-1) = Meta + 1 -or- Alt + 1
----------------------------------------------------------------------

  ?                  # Rodo visų klavišų kombinacijų sąrašą
  :                  # Įjungiama tmux komandinė eilutė
  r                  # Priverstinai perpiešiamas prijungtas klientas
  c                  # Sukurti naują langą

  !                  # Iškelia esamą skydelį iš lango.
  %                  # Perskelia esamą skydelį į du, kairįjį ir dešinį
  "                  # Perskelia esamą skydelį į du, viršutinį ir apatinį

  n                  # Pakeičia į kitą langą
  p                  # Pakeičia į buvusį langą
  {                  # Apkeičia dabartinį skydėlį su buvusiu
  }                  # Apkeičia dabartinį skydėlį su sekančiu

  s                  # Pasirinkti naują sesiją prijungtam klientui interaktyviai
  w                  # Pasirinkti dabartinį langą interaktyviai
  0 to 9             # Pasirinkti langą nuo 0 iki 9

  d                  # Atjungti dabartinį klientą
  D                  # Pasirinkti klientą, kurį atjungti

  &                  # Užbaigti dabartinį langą
  x                  # Užbaigti dabartinį skydelį

  Up, Down           # Pakeisti į skydelį viršuje, apačioje, kairėje arba
                     dešinėje
  Left, Right

  M-1 to M-5         # Rikiuoti skydelius:
                       # 1) even-horizontal
                       # 2) even-vertical
                       # 3) main-horizontal
                       # 4) main-vertical
                       # 5) tiled

  C-Up, C-Down       # Keisti esamo skydelio dydį vienos ląstelės žingsniu
  C-Left, C-Right

  M-Up, M-Down       # Keisti esamo skydelio dydį penkių ląstelių žingsniu
  M-Left, M-Right

```


### Configuring ~/.tmux.conf

tmux.conf gali būti nustatytas automatiškai paleidimo metu, panašiai kaip ir
.vimrc arba init.el.

```
# Pavyzdys tmux.conf
# 2014.10


### General
###########################################################################

# Enable UTF-8
setw -g utf8 on
set-option -g status-utf8 on

# Scrollback/History limit
set -g history-limit 2048

# Index Start
set -g base-index 1

# Mouse
set-option -g mouse-select-pane on

# Force reload of config file
unbind r
bind r source-file ~/.tmux.conf


### Keybinds
###########################################################################

# Unbind C-b as the default prefix
unbind C-b

# Set new default prefix
set-option -g prefix `

# Return to previous window when prefix is pressed twice
bind C-a last-window
bind ` last-window

# Allow swapping C-a and ` using F11/F12
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Keybind preference
setw -g mode-keys vi
set-option -g status-keys vi

# Moving between panes with vim movement keys
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Window Cycle/Swap
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# Easy split pane commands
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# Activate inner-most session (when nesting tmux) to send commands
bind a send-prefix


### Theme
###########################################################################

# Statusbar Color Palatte
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# Pane Border Color Palette
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Message Color Palette
set-option -g message-fg black
set-option -g message-bg green

# Window Status Color Palette
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-content-attr default
setw -g window-status-content-fg yellow
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### UI
###########################################################################

# Notification
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# Automatically set window titles
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not)

# Statusbar Adjustments
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# Show performance counters in statusbar
# Requires https://github.com/thewtex/tmux-mem-cpu-load/
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```


### Šaltiniai

[Tmux | Home](http://tmux.sourceforge.net)

[Tmux Manual page](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)

[Display CPU/MEM % in statusbar](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)

[tmuxinator - Manage complex tmux sessions](https://github.com/tmuxinator/tmuxinator)
---
language: Lua
contributors:
    - ["Tyler Neylon", "http://tylerneylon.com/"]
filename: learnlua.lua
---

```lua
-- Two dashes start a one-line comment.

--[[
     Adding two ['s and ]'s makes it a
     multi-line comment.
--]]
--------------------------------------------------------------------------------
-- 1. Variables and flow control.
--------------------------------------------------------------------------------

num = 42  -- All numbers are doubles.
-- Don't freak out, 64-bit doubles have 52 bits for storing exact int
-- values; machine precision is not a problem for ints that need < 52 bits.

s = 'walternate'  -- Immutable strings like Python.
t = "double-quotes are also fine"
u = [[ Double brackets
       start and end
       multi-line strings.]]
t = nil  -- Undefines t; Lua has garbage collection.

-- Blocks are denoted with keywords like do/end:
while num < 50 do
  num = num + 1  -- No ++ or += type operators.
end

-- If clauses:
if num > 40 then
  print('over 40')
elseif s ~= 'walternate' then  -- ~= is not equals.
  -- Equality check is == like Python; ok for strs.
  io.write('not over 40\n')  -- Defaults to stdout.
else
  -- Variables are global by default.
  thisIsGlobal = 5  -- Camel case is common.

  -- How to make a variable local:
  local line = io.read()  -- Reads next stdin line.

  -- String concatenation uses the .. operator:
  print('Winter is coming, ' .. line)
end

-- Undefined variables return nil.
-- This is not an error:
foo = anUnknownVariable  -- Now foo = nil.

aBoolValue = false

-- Only nil and false are falsy; 0 and '' are true!
if not aBoolValue then print('twas false') end

-- 'or' and 'and' are short-circuited. This is similar to the a?b:c operator
-- in C/js:
ans = aBoolValue and 'yes' or 'no'  --> 'no'

karlSum = 0
for i = 1, 100 do  -- The range includes both ends.
  karlSum = karlSum + i
end

-- Use "100, 1, -1" as the range to count down:
fredSum = 0
for j = 100, 1, -1 do fredSum = fredSum + j end

-- In general, the range is begin, end[, step].

-- Another loop construct:
repeat
  print('the way of the future')
  num = num - 1
until num == 0

--------------------------------------------------------------------------------
-- 2. Functions.
--------------------------------------------------------------------------------

function fib(n)
  if n < 2 then return n end
  return fib(n - 2) + fib(n - 1)
end

-- Closures and anonymous functions are ok:
function adder(x)
  -- The returned function is created when adder is called, and remembers the
  -- value of x:
  return function (y) return x + y end
end
a1 = adder(9)
a2 = adder(36)
print(a1(16))  --> 25
print(a2(64))  --> 100

-- Returns, func calls, and assignments all work with lists that may be
-- mismatched in length. Unmatched receivers are nil; unmatched senders are
-- discarded.

x, y, z = 1, 2, 3, 4
-- Now x = 1, y = 2, z = 3, and 4 is thrown away.

function bar(a, b, c)
  print(a, b, c)
  return 4, 8, 15, 16, 23, 42
end

x, y = bar('zaphod')  --> prints "zaphod  nil nil"
-- Now x = 4, y = 8, values 15..42 are discarded.

-- Functions are first-class, may be local/global. These are the same:
function f(x) return x * x end
f = function (x) return x * x end

-- And so are these:
local function g(x) return math.sin(x) end
local g = function(x) return math.sin(x) end
-- Equivalent to local function g(x)..., except referring to g in the function
-- body won't work as expected.
local g; g  = function (x) return math.sin(x) end
-- the 'local g' decl makes g-self-references ok.

-- Trig funcs work in radians, by the way.

-- Calls with one string param don't need parens:
print 'hello'  -- Works fine.

-- Calls with one table param don't need parens either (more on tables below):
print {} -- Works fine too.

--------------------------------------------------------------------------------
-- 3. Tables.
--------------------------------------------------------------------------------

-- Tables = Lua's only compound data structure; they are associative arrays.
-- Similar to php arrays or js objects, they are hash-lookup dicts that can
-- also be used as lists.

-- Using tables as dictionaries / maps:

-- Dict literals have string keys by default:
t = {key1 = 'value1', key2 = false}

-- String keys can use js-like dot notation:
print(t.key1)  -- Prints 'value1'.
t.newKey = {}  -- Adds a new key/value pair.
t.key2 = nil   -- Removes key2 from the table.

-- Literal notation for any (non-nil) value as key:
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28])  -- prints "tau"

-- Key matching is basically by value for numbers and strings, but by identity
-- for tables.
a = u['@!#']  -- Now a = 'qbert'.
b = u[{}]     -- We might expect 1729, but it's nil:
-- b = nil since the lookup fails. It fails because the key we used is not the
-- same object as the one used to store the original value. So strings &
-- numbers are more portable keys.

-- A one-table-param function call needs no parens:
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'}  -- Prints 'Sonmi~451'.

for key, val in pairs(u) do  -- Table iteration.
  print(key, val)
end

-- _G is a special table of all globals.
print(_G['_G'] == _G)  -- Prints 'true'.

-- Using tables as lists / arrays:

-- List literals implicitly set up int keys:
v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do  -- #v is the size of v for lists.
  print(v[i])  -- Indices start at 1 !! SO CRAZY!
end
-- A 'list' is not a real type. v is just a table with consecutive integer
-- keys, treated as a list.

--------------------------------------------------------------------------------
-- 3.1 Metatables and metamethods.
--------------------------------------------------------------------------------

-- A table can have a metatable that gives the table operator-overloadish
-- behaviour. Later we'll see how metatables support js-prototype behaviour.

f1 = {a = 1, b = 2}  -- Represents the fraction a/b.
f2 = {a = 2, b = 3}

-- This would fail:
-- s = f1 + f2

metafraction = {}
function metafraction.__add(f1, f2)
  local sum = {}
  sum.b = f1.b * f2.b
  sum.a = f1.a * f2.b + f2.a * f1.b
  return sum
end

setmetatable(f1, metafraction)
setmetatable(f2, metafraction)

s = f1 + f2  -- call __add(f1, f2) on f1's metatable

-- f1, f2 have no key for their metatable, unlike prototypes in js, so you must
-- retrieve it as in getmetatable(f1). The metatable is a normal table with
-- keys that Lua knows about, like __add.

-- But the next line fails since s has no metatable:
-- t = s + s
-- Class-like patterns given below would fix this.

-- An __index on a metatable overloads dot lookups:
defaultFavs = {animal = 'gru', food = 'donuts'}
myFavs = {food = 'pizza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal  -- works! thanks, metatable

--------------------------------------------------------------------------------
-- Direct table lookups that fail will retry using the metatable's __index
-- value, and this recurses.

-- An __index value can also be a function(tbl, key) for more customized
-- lookups.

-- Values of __index,add, .. are called metamethods.
-- Full list. Here a is a table with the metamethod.

-- __add(a, b)                     for a + b
-- __sub(a, b)                     for a - b
-- __mul(a, b)                     for a * b
-- __div(a, b)                     for a / b
-- __mod(a, b)                     for a % b
-- __pow(a, b)                     for a ^ b
-- __unm(a)                        for -a
-- __concat(a, b)                  for a .. b
-- __len(a)                        for #a
-- __eq(a, b)                      for a == b
-- __lt(a, b)                      for a < b
-- __le(a, b)                      for a <= b
-- __index(a, b)  <fn or a table>  for a.b
-- __newindex(a, b, c)             for a.b = c
-- __call(a, ...)                  for a(...)

--------------------------------------------------------------------------------
-- 3.2 Class-like tables and inheritance.
--------------------------------------------------------------------------------

-- Classes aren't built in; there are different ways to make them using
-- tables and metatables.

-- Explanation for this example is below it.

Dog = {}                                   -- 1.

function Dog:new()                         -- 2.
  local newObj = {sound = 'woof'}          -- 3.
  self.__index = self                      -- 4.
  return setmetatable(newObj, self)        -- 5.
end

function Dog:makeSound()                   -- 6.
  print('I say ' .. self.sound)
end

mrDog = Dog:new()                          -- 7.
mrDog:makeSound()  -- 'I say woof'         -- 8.

-- 1. Dog acts like a class; it's really a table.
-- 2. "function tablename:fn(...)" is the same as
--    "function tablename.fn(self, ...)", The : just adds a first arg called
--    self. Read 7 & 8 below for how self gets its value.
-- 3. newObj will be an instance of class Dog.
-- 4. "self" is the class being instantiated. Often self = Dog, but inheritance
--    can change it. newObj gets self's functions when we set both newObj's
--    metatable and self's __index to self.
-- 5. Reminder: setmetatable returns its first arg.
-- 6. The : works as in 2, but this time we expect self to be an instance
--    instead of a class.
-- 7. Same as Dog.new(Dog), so self = Dog in new().
-- 8. Same as mrDog.makeSound(mrDog); self = mrDog.

--------------------------------------------------------------------------------

-- Inheritance example:

LoudDog = Dog:new()                           -- 1.

function LoudDog:makeSound()
  local s = self.sound .. ' '                 -- 2.
  print(s .. s .. s)
end

seymour = LoudDog:new()                       -- 3.
seymour:makeSound()  -- 'woof woof woof'      -- 4.

--------------------------------------------------------------------------------
-- 1. LoudDog gets Dog's methods and variables.
-- 2. self has a 'sound' key from new(), see 3.
-- 3. Same as "LoudDog.new(LoudDog)", and converted to "Dog.new(LoudDog)" as
--    LoudDog has no 'new' key, but does have "__index = Dog" on its metatable.
--    Result: seymour's metatable is LoudDog, and "LoudDog.__index = Dog". So
--    seymour.key will equal seymour.key, LoudDog.key, Dog.key, whichever
--    table is the first with the given key.
-- 4. The 'makeSound' key is found in LoudDog; this is the same as
--    "LoudDog.makeSound(seymour)".

-- If needed, a subclass's new() is like the base's:
function LoudDog:new()
  local newObj = {}
  -- set up newObj
  self.__index = self
  return setmetatable(newObj, self)
end

--------------------------------------------------------------------------------
-- 4. Modules.
--------------------------------------------------------------------------------


--[[ I'm commenting out this section so the rest of this script remains
--   runnable.
```

```lua
-- Suppose the file mod.lua looks like this:
local M = {}

local function sayMyName()
  print('Hrunkner')
end

function M.sayHello()
  print('Why hello there')
  sayMyName()
end

return M

-- Another file can use mod.lua's functionality:
local mod = require('mod')  -- Run the file mod.lua.

-- require is the standard way to include modules.
-- require acts like:     (if not cached; see below)
local mod = (function ()
  <contents of mod.lua>
end)()
-- It's like mod.lua is a function body, so that locals inside mod.lua are
-- invisible outside it.

-- This works because mod here = M in mod.lua:
mod.sayHello()  -- Says hello to Hrunkner.

-- This is wrong; sayMyName only exists in mod.lua:
mod.sayMyName()  -- error

-- require's return values are cached so a file is run at most once, even when
-- require'd many times.

-- Suppose mod2.lua contains "print('Hi!')".
local a = require('mod2')  -- Prints Hi!
local b = require('mod2')  -- Doesn't print; a=b.

-- dofile is like require without caching:
dofile('mod2')  --> Hi!
dofile('mod2')  --> Hi! (runs again, unlike require)

-- loadfile loads a lua file but doesn't run it yet.
f = loadfile('mod2')  -- Calling f() runs mod2.lua.

-- loadstring is loadfile for strings.
g = loadstring('print(343)')  -- Returns a function.
g()  -- Prints out 343; nothing printed before now.

--]]

```
## References

I was excited to learn Lua so I could make games
with the <a href="http://love2d.org/">Love 2D game engine</a>. That's the why.

I started with <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">BlackBulletIV's Lua for programmers</a>.
Next I read the official <a href="http://www.lua.org/pil/contents.html">Programming in Lua</a> book.
That's the how.

It might be helpful to check out the <a href="http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">Lua short
reference</a> on lua-users.org.

The main topics not covered are standard libraries:

* <a href="http://lua-users.org/wiki/StringLibraryTutorial">string library</a>
* <a href="http://lua-users.org/wiki/TableLibraryTutorial">table library</a>
* <a href="http://lua-users.org/wiki/MathLibraryTutorial">math library</a>
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a>
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a>

By the way, the entire file is valid Lua; save it
as learn.lua and run it with "lua learn.lua" !

This was first written for tylerneylon.com, and is
also available as a <a href="https://gist.github.com/tylerneylon/5853042">github gist</a>. Have fun with Lua!
---
language: make
contributors:
    - ["Robert Steed", "https://github.com/robochat"]
filename: Makefile
---

A Makefile defines a graph of rules for creating a target (or targets).
Its purpose is to do the minimum amount of work needed to update a
target to the most recent version of the source. Famously written over a
weekend by Stuart Feldman in 1976, it is still widely used (particularly
on Unix and Linux) despite many competitors and criticisms.

There are many varieties of make in existence, however this article 
assumes that we are using GNU make which is the standard on Linux.

```make

# Comments can be written like this.

# File should be named Makefile and then can be run as `make <target>`.
# Otherwise we use `make -f "filename" <target>`.

# Warning - only use TABS to indent in Makefiles, never spaces!

#-----------------------------------------------------------------------
# Basics
#-----------------------------------------------------------------------

# Rules are of the format
# target: <prerequisite>
# where prerequisites are optional.

# A rule - this rule will only run if file0.txt doesn't exist.
file0.txt:
	echo "foo" > file0.txt
	# Even comments in these 'recipe' sections get passed to the shell.
	# Try `make file0.txt` or simply `make` - first rule is the default.

# This rule will only run if file0.txt is newer than file1.txt.
file1.txt: file0.txt
	cat file0.txt > file1.txt
	# use the same quoting rules as in the shell.
	@cat file0.txt >> file1.txt
	# @ stops the command from being echoed to stdout.
	-@echo 'hello'
	# - means that make will keep going in the case of an error.
	# Try `make file1.txt` on the commandline.

# A rule can have multiple targets and multiple prerequisites
file2.txt file3.txt: file0.txt file1.txt
	touch file2.txt
	touch file3.txt

# Make will complain about multiple recipes for the same rule. Empty
# recipes don't count though and can be used to add new dependencies.

#-----------------------------------------------------------------------
# Phony Targets
#-----------------------------------------------------------------------

# A phony target. Any target that isn't a file.
# It will never be up to date so make will always try to run it.
all: maker process

# We can declare things out of order.
maker:
	touch ex0.txt ex1.txt

# Can avoid phony rules breaking when a real file has the same name by
.PHONY: all maker process
# This is a special target. There are several others.

# A rule with a dependency on a phony target will always run
ex0.txt ex1.txt: maker

# Common phony targets are: all make clean install ...

#-----------------------------------------------------------------------
# Automatic Variables & Wildcards
#-----------------------------------------------------------------------

process: file*.txt	#using a wildcard to match filenames
	@echo $^	# $^ is a variable containing the list of prerequisites
	@echo $@	# prints the target name
	#(for multiple target rules, $@ is whichever caused the rule to run)
	@echo $<	# the first prerequisite listed
	@echo $?	# only the dependencies that are out of date
	@echo $+	# all dependencies including duplicates (unlike normal)
	#@echo $|	# all of the 'order only' prerequisites

# Even if we split up the rule dependency definitions, $^ will find them
process: ex1.txt file0.txt
# ex1.txt will be found but file0.txt will be deduplicated.

#-----------------------------------------------------------------------
# Patterns
#-----------------------------------------------------------------------

# Can teach make how to convert certain files into other files.

%.png: %.svg
	inkscape --export-png $^

# Pattern rules will only do anything if make decides to create the
# target.

# Directory paths are normally ignored when matching pattern rules. But
# make will try to use the most appropriate rule available.
small/%.png: %.svg
	inkscape --export-png --export-dpi 30 $^

# make will use the last version for a pattern rule that it finds.
%.png: %.svg
	@echo this rule is chosen

# However make will use the first pattern rule that can make the target
%.png: %.ps
	@echo this rule is not chosen if *.svg and *.ps are both present

# make already has some pattern rules built-in. For instance, it knows
# how to turn *.c files into *.o files.

# Older makefiles might use suffix rules instead of pattern rules
.png.ps:
	@echo this rule is similar to a pattern rule.

# Tell make about the suffix rule
.SUFFIXES: .png

#-----------------------------------------------------------------------
# Variables
#-----------------------------------------------------------------------
# aka. macros

# Variables are basically all string types

name = Ted
name2="Sarah"

echo:
	@echo $(name)
	@echo ${name2}
	@echo $name    # This won't work, treated as $(n)ame.
	@echo $(name3) # Unknown variables are treated as empty strings.

# There are 4 places to set variables.
# In order of priority from highest to lowest:
# 1: commandline arguments
# 2: Makefile
# 3: shell environment variables - make imports these automatically.
# 4: make has some predefined variables

name4 ?= Jean
# Only set the variable if environment variable is not already defined.

override name5 = David
# Stops commandline arguments from changing this variable.

name4 +=grey
# Append values to variable (includes a space).

# Pattern-specific variable values (GNU extension).
echo: name2 = Sara # True within the matching rule
	# and also within its remade recursive dependencies
	# (except it can break when your graph gets too complicated!)

# Some variables defined automatically by make.
echo_inbuilt:
	echo $(CC)
	echo ${CXX)}
	echo $(FC)
	echo ${CFLAGS)}
	echo $(CPPFLAGS)
	echo ${CXXFLAGS}
	echo $(LDFLAGS)
	echo ${LDLIBS}

#-----------------------------------------------------------------------
# Variables 2
#-----------------------------------------------------------------------

# The first type of variables are evaluated each time they are used.
# This can be expensive, so a second type of variable exists which is
# only evaluated once. (This is a GNU make extension)

var := hello
var2 ::=  $(var) hello
#:= and ::= are equivalent.

# These variables are evaluated procedurally (in the order that they
# appear), thus breaking with the rest of the language !

# This doesn't work
var3 ::= $(var4) and good luck
var4 ::= good night

#-----------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------

# make has lots of functions available.

sourcefiles = $(wildcard *.c */*.c)
objectfiles = $(patsubst %.c,%.o,$(sourcefiles))

# Format is $(func arg0,arg1,arg2...)

# Some examples
ls:	* src/*
	@echo $(filter %.txt, $^)
	@echo $(notdir $^)
	@echo $(join $(dir $^),$(notdir $^))

#-----------------------------------------------------------------------
# Directives
#-----------------------------------------------------------------------

# Include other makefiles, useful for platform specific code
include foo.mk

sport = tennis
# Conditional compilation
report:
ifeq ($(sport),tennis)
	@echo 'game, set, match'
else
	@echo "They think it's all over; it is now"
endif

# There are also ifneq, ifdef, ifndef

foo = true

ifdef $(foo)
bar = 'hello'
endif
```

### More Resources

+ [gnu make documentation](https://www.gnu.org/software/make/manual/)
+ [software carpentry tutorial](http://swcarpentry.github.io/make-novice/)
+ learn C the hard way [ex2](http://c.learncodethehardway.org/book/ex2.html) [ex28](http://c.learncodethehardway.org/book/ex28.html)
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
    - ["Jacob Ward", "http://github.com/JacobCWard/"]
filename: markdown.md
---


Markdown was created by John Gruber in 2004. It's meant to be an easy to read 
and write syntax which converts easily to HTML (and now many other formats as 
well).

Markdown also varies in implementation from one parser to a next. This
guide will attempt to clarify when features are universal or when they are
specific to a certain parser.

- [HTML Elements](#html-elements)
- [Headings](#headings)
- [Simple Text Styles](#simple-text-styles)
- [Paragraphs](#paragraphs)
- [Lists](#lists)
- [Code blocks](#code-blocks)
- [Horizontal rule](#horizontal-rule)
- [Links](#links)
- [Images](#images)
- [Miscellany](#miscellany)

## HTML Elements
Markdown is a superset of HTML, so any HTML file is valid Markdown.

```markdown
<!--This means we can use HTML elements in Markdown, such as the comment 
element, and they won't be affected by a markdown parser. However, if you 
create an HTML element in your markdown file, you cannot use markdown syntax 
within that element's contents.-->
```

## Headings

You can create HTML elements `<h1>` through `<h6>` easily by prepending the
text you want to be in that element by a number of hashes (#).

```markdown
# This is an <h1>
## This is an <h2>
### This is an <h3>
#### This is an <h4>
##### This is an <h5>
###### This is an <h6>
```
Markdown also provides us with two alternative ways of indicating h1 and h2.

```markdown
This is an h1
=============

This is an h2
-------------
```

## Simple text styles

Text can be easily styled as italic or bold using markdown.

```markdown
*This text is in italics.*
_And so is this text._

**This text is in bold.**
__And so is this text.__

***This text is in both.***
**_As is this!_**
*__And this!__*
```

In GitHub Flavored Markdown, which is used to render markdown files on
GitHub, we also have strikethrough:

```markdown
~~This text is rendered with strikethrough.~~
```
## Paragraphs

Paragraphs are a one or multiple adjacent lines of text separated by one or
multiple blank lines.

```markdown
This is a paragraph. I'm typing in a paragraph isn't this fun?

Now I'm in paragraph 2.
I'm still in paragraph 2 too!


I'm in paragraph three!
```

Should you ever want to insert an HTML `<br />` tag, you can end a paragraph
with two or more spaces and then begin a new paragraph.

```markdown
I end with two spaces (highlight me to see them).

There's a <br /> above me!
```

Block quotes are easy and done with the > character.

```markdown
> This is a block quote. You can either
> manually wrap your lines and put a `>` before every line or you can let your lines get really long and wrap on their own.
> It doesn't make a difference so long as they start with a `>`.

> You can also use more than one level
>> of indentation?
> How neat is that?

```

## Lists
Unordered lists can be made using asterisks, pluses, or hyphens.

```markdown
* Item
* Item
* Another item

or

+ Item
+ Item
+ One more item

or

- Item
- Item
- One last item
```

Ordered lists are done with a number followed by a period.

```markdown
1. Item one
2. Item two
3. Item three
```

You don't even have to label the items correctly and Markdown will still
render the numbers in order, but this may not be a good idea.

```markdown
1. Item one
1. Item two
1. Item three
```
(This renders the same as the above example)

You can also use sublists

```markdown
1. Item one
2. Item two
3. Item three
    * Sub-item
    * Sub-item
4. Item four
```

There are even task lists. This creates HTML checkboxes.

```markdown
Boxes below without the 'x' are unchecked HTML checkboxes.
- [ ] First task to complete.
- [ ] Second task that needs done
This checkbox below will be a checked HTML checkbox.
- [x] This task has been completed
```

## Code blocks

You can indicate a code block (which uses the `<code>` element) by indenting
a line with four spaces or a tab.

```markdown
    This is code
    So is this
```

You can also re-tab (or add an additional four spaces) for indentation
inside your code

```markdown
    my_array.each do |item|
        puts item
    end
```

Inline code can be created using the backtick character `

```markdown
John didn't even know what the `go_to()` function did!
```

In GitHub Flavored Markdown, you can use a special syntax for code

<pre>
<code class="highlight">&#x60;&#x60;&#x60;ruby
def foobar
    puts "Hello world!"
end
&#x60;&#x60;&#x60;</code></pre>

The above text doesn't require indenting, plus GitHub will use syntax
highlighting of the language you specify after the \`\`\`

## Horizontal rule

Horizontal rules (`<hr/>`) are easily added with three or more asterisks or 
hyphens, with or without spaces.

```markdown
***
---
- - -
****************
```

## Links

One of the best things about markdown is how easy it is to make links. Put
the text to display in hard brackets [] followed by the url in parentheses ()

```markdown
[Click me!](http://test.com/)
```
You can also add a link title using quotes inside the parentheses.

```markdown
[Click me!](http://test.com/ "Link to Test.com")
```
Relative paths work too.

```markdown
[Go to music](/music/).
```

Markdown also supports reference style links.

<pre><code class="highlight">&#x5b;<span class="nv">Click this link</span>][<span class="ss">link1</span>] for more info about it!
&#x5b;<span class="nv">Also check out this link</span>][<span class="ss">foobar</span>] if you want to.

&#x5b;<span class="nv">link1</span>]: <span class="sx">http://test.com/</span> <span class="nn">"Cool!"</span>
&#x5b;<span class="nv">foobar</span>]: <span class="sx">http://foobar.biz/</span> <span class="nn">"Alright!"</span></code></pre>

The title can also be in single quotes or in parentheses, or omitted
entirely. The references can be anywhere in your document and the reference IDs
can be anything so long as they are unique.

There is also "implicit naming" which lets you use the link text as the id.

<pre><code class="highlight">&#x5b;<span class="nv">This</span>][] is a link.

&#x5b;<span class="nv">this</span>]: <span class="sx">http://thisisalink.com/</span></code></pre>

But it's not that commonly used.

## Images
Images are done the same way as links but with an exclamation point in front!

```markdown
![This is the alt-attribute for my image](http://imgur.com/myimage.jpg "An optional title")
```

And reference style works as expected.

<pre><code class="highlight">!&#x5b;<span class="nv">This is the alt-attribute.</span>][<span class="ss">myimage</span>]

&#x5b;<span class="nv">myimage</span>]: <span class="sx">relative/urls/cool/image.jpg</span> <span class="nn">"if you need a title, it's here"</span></code></pre>
## Miscellany
### Auto-links

```markdown
<http://testwebsite.com/> is equivalent to
[http://testwebsite.com/](http://testwebsite.com/)
```

### Auto-links for emails

```markdown
<foo@bar.com>
```

### Escaping characters

```markdown
I want to type *this text surrounded by asterisks* but I don't want it to be
in italics, so I do this: \*this text surrounded by asterisks\*.
```

### Keyboard keys

In GitHub Flavored Markdown, you can use a `<kbd>` tag to represent keyboard 
keys.

```markdown
Your computer crashed? Try sending a
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>
```
### Tables

Tables are only available in GitHub Flavored Markdown and are slightly
cumbersome, but if you really want it:

```markdown
| Col1         | Col2     | Col3          |
| :----------- | :------: | ------------: |
| Left-aligned | Centered | Right-aligned |
| blah         | blah     | blah          |
```
or, for the same results

```markdown
Col 1 | Col2 | Col3
:-- | :-: | --:
Ugh this is so ugly | make it | stop
```

---
For more info, check out John Gruber's official post of syntax [here](http://daringfireball.net/projects/markdown/syntax) and Adam Pritchard's great cheatsheet [here](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: Matlab
filename: learnmatlab.mat
contributors:
    - ["mendozao", "http://github.com/mendozao"]
    - ["jamesscottbrown", "http://jamesscottbrown.com"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
    - ["Claudson Martins", "http://github.com/claudsonm"]
---

MATLAB stands for MATrix LABoratory. It is a powerful numerical computing language commonly used in engineering and mathematics.

If you have any feedback please feel free to reach me at
[@the_ozzinator](https://twitter.com/the_ozzinator), or
[osvaldo.t.mendoza@gmail.com](mailto:osvaldo.t.mendoza@gmail.com).

```matlab
%% Code sections start with two percent signs. Section titles go on the same line.
% Comments start with a percent sign.

%{
Multi line comments look
something
like
this
%}

% Two percent signs denote the start of a new code section
% Individual code sections can be run by moving the cursor to the section followed by
% either clicking the "Run Section" button
% or     using Ctrl+Shift+Enter (Windows) or Cmd+Shift+Return (OS X)

%% This is the start of a code section
%  One way of using sections is to separate expensive but unchanging start-up code like loading data
load myFile.mat y

%% This is another code section
%  This section can be edited and run repeatedly on its own, and is helpful for exploratory programming and demos
A = A * 2;
plot(A);

%% Code sections are also known as code cells or cell mode (not to be confused with cell arrays)


% commands can span multiple lines, using '...':
 a = 1 + 2 + ...
 + 4

% commands can be passed to the operating system
!ping google.com

who % Displays all variables in memory
whos % Displays all variables in memory, with their types
clear % Erases all your variables from memory
clear('A') % Erases a particular variable
openvar('A') % Open variable in variable editor

clc % Erases the writing on your Command Window
diary % Toggle writing Command Window text to file
ctrl-c % Abort current computation

edit('myfunction.m') % Open function/script in editor
type('myfunction.m') % Print the source of function/script to Command Window

profile on 	% turns on the code profiler
profile off 	% turns off the code profiler
profile viewer 	% Open profiler

help command 	% Displays documentation for command in Command Window
doc command 	% Displays documentation for command in Help Window
lookfor command % Searches for command in the first commented line of all functions
lookfor command -all % searches for command in all functions


% Output formatting
format short 	% 4 decimals in a floating number
format long 	% 15 decimals
format bank 	% only two digits after decimal point - for financial calculations
fprintf('text') % print "text" to the screen
disp('text') 	% print "text" to the screen

% Variables & Expressions
myVariable = 4 	% Notice Workspace pane shows newly created variable
myVariable = 4; % Semi colon suppresses output to the Command Window
4 + 6  		% ans = 10
8 * myVariable 	% ans = 32
2 ^ 3 		% ans = 8
a = 2; b = 3;
c = exp(a)*sin(pi/2) % c = 7.3891

% Calling functions can be done in either of two ways:
% Standard function syntax:
load('myFile.mat', 'y') % arguments within parentheses, separated by commas
% Command syntax:
load myFile.mat y 	% no parentheses, and spaces instead of commas
% Note the lack of quote marks in command form: inputs are always passed as
% literal text - cannot pass variable values. Also, can't receive output:
[V,D] = eig(A);  % this has no equivalent in command form
[~,D] = eig(A);  % if you only want D and not V



% Logicals
1 > 5 % ans = 0
10 >= 10 % ans = 1
3 ~= 4 % Not equal to -> ans = 1
3 == 3 % equal to -> ans = 1
3 > 1 && 4 > 1 % AND -> ans = 1
3 > 1 || 4 > 1 % OR -> ans = 1
~1 % NOT -> ans = 0

% Logicals can be applied to matrices:
A > 5
% for each element, if condition is true, that element is 1 in returned matrix
A( A > 5 )
% returns a vector containing the elements in A for which condition is true

% Strings
a = 'MyString'
length(a) % ans = 8
a(2) % ans = y
[a,a] % ans = MyStringMyString


% Cells
a = {'one', 'two', 'three'}
a(1) % ans = 'one' - returns a cell
char(a(1)) % ans = one - returns a string

% Structures
A.b = {'one','two'};
A.c = [1 2];
A.d.e = false;

% Vectors
x = [4 32 53 7 1]
x(2) % ans = 32, indices in Matlab start 1, not 0
x(2:3) % ans = 32 53
x(2:end) % ans = 32 53 7 1

x = [4; 32; 53; 7; 1] % Column vector

x = [1:10] % x = 1 2 3 4 5 6 7 8 9 10
x = [1:2:10] % Increment by 2, i.e. x = 1 3 5 7 9

% Matrices
A = [1 2 3; 4 5 6; 7 8 9]
% Rows are separated by a semicolon; elements are separated with space or comma
% A =

%     1     2     3
%     4     5     6
%     7     8     9

A(2,3) % ans = 6, A(row, column)
A(6) % ans = 8
% (implicitly concatenates columns into vector, then indexes into that)


A(2,3) = 42 % Update row 2 col 3 with 42
% A =

%     1     2     3
%     4     5     42
%     7     8     9

A(2:3,2:3) % Creates a new matrix from the old one
%ans =

%     5     42
%     8     9

A(:,1) % All rows in column 1
%ans =

%     1
%     4
%     7

A(1,:) % All columns in row 1
%ans =

%     1     2     3

[A ; A] % Concatenation of matrices (vertically)
%ans =

%     1     2     3
%     4     5    42
%     7     8     9
%     1     2     3
%     4     5    42
%     7     8     9

% this is the same as
vertcat(A,A);


[A , A] % Concatenation of matrices (horizontally)

%ans =

%     1     2     3     1     2     3
%     4     5    42     4     5    42
%     7     8     9     7     8     9

% this is the same as
horzcat(A,A);


A(:, [3 1 2]) % Rearrange the columns of original matrix
%ans =

%     3     1     2
%    42     4     5
%     9     7     8

size(A) % ans = 3 3

A(1, :) =[] % Delete the first row of the matrix
A(:, 1) =[] % Delete the first column of the matrix

transpose(A) % Transpose the matrix, which is the same as:
A one
ctranspose(A) % Hermitian transpose the matrix
% (the transpose, followed by taking complex conjugate of each element)
A' % Concise version of complex transpose
A.' % Concise version of transpose (without taking complex conjugate)




% Element by Element Arithmetic vs. Matrix Arithmetic
% On their own, the arithmetic operators act on whole matrices. When preceded
% by a period, they act on each element instead. For example:
A * B % Matrix multiplication
A .* B % Multiple each element in A by its corresponding element in B

% There are several pairs of functions, where one acts on each element, and
% the other (whose name ends in m) acts on the whole matrix.
exp(A) % exponentiate each element
expm(A) % calculate the matrix exponential
sqrt(A) % take the square root of each element
sqrtm(A) %  find the matrix whose square is A


% Plotting
x = 0:.10:2*pi; % Creates a vector that starts at 0 and ends at 2*pi with increments of .1
y = sin(x);
plot(x,y)
xlabel('x axis')
ylabel('y axis')
title('Plot of y = sin(x)')
axis([0 2*pi -1 1]) % x range from 0 to 2*pi, y range from -1 to 1

plot(x,y1,'-',x,y2,'--',x,y3,':') % For multiple functions on one plot
legend('Line 1 label', 'Line 2 label') % Label curves with a legend

% Alternative method to plot multiple functions in one plot.
% while 'hold' is on, commands add to existing graph rather than replacing it
plot(x, y)
hold on
plot(x, z)
hold off

loglog(x, y) % A log-log plot
semilogx(x, y) % A plot with logarithmic x-axis
semilogy(x, y) % A plot with logarithmic y-axis

fplot (@(x) x^2, [2,5]) % plot the function x^2 from x=2 to x=5

grid on % Show grid; turn off with 'grid off'
axis square % Makes the current axes region square
axis equal % Set aspect ratio so data units are the same in every direction

scatter(x, y); % Scatter-plot
hist(x); % Histogram
stem(x); % Plot values as stems, useful for displaying discrete data
bar(x); % Plot bar graph

z = sin(x);
plot3(x,y,z); % 3D line plot

pcolor(A) % Heat-map of matrix: plot as grid of rectangles, coloured by value
contour(A) % Contour plot of matrix
mesh(A) % Plot as a mesh surface

h = figure % Create new figure object, with handle h
figure(h) % Makes the figure corresponding to handle h the current figure
close(h) % close figure with handle h
close all % close all open figure windows
close % close current figure window

shg % bring an existing graphics window forward, or create new one if needed
clf clear % clear current figure window, and reset most figure properties

% Properties can be set and changed through a figure handle.
% You can save a handle to a figure when you create it.
% The function get returns a handle to the current figure
h = plot(x, y); % you can save a handle to a figure when you create it
set(h, 'Color', 'r')
% 'y' yellow; 'm' magenta, 'c' cyan, 'r' red, 'g' green, 'b' blue, 'w' white, 'k' black
set(h, 'LineStyle', '--')
 % '--' is solid line, '---' dashed, ':' dotted, '-.' dash-dot, 'none' is no line
get(h, 'LineStyle')


% The function gca returns a handle to the axes for the current figure
set(gca, 'XDir', 'reverse'); % reverse the direction of the x-axis

% To create a figure that contains several axes in tiled positions, use subplot
subplot(2,3,1); % select the first position in a 2-by-3 grid of subplots
plot(x1); title('First Plot') % plot something in this position
subplot(2,3,2); % select second position in the grid
plot(x2); title('Second Plot') % plot something there


% To use functions or scripts, they must be on your path or current directory
path % display current path
addpath /path/to/dir % add to path
rmpath /path/to/dir % remove from path
cd /path/to/move/into % change directory


% Variables can be saved to .mat files
save('myFileName.mat') % Save the variables in your Workspace
load('myFileName.mat') % Load saved variables into Workspace

% M-file Scripts
% A script file is an external file that contains a sequence of statements.
% They let you avoid repeatedly typing the same code in the Command Window
% Have .m extensions

% M-file Functions
% Like scripts, and have the same .m extension
% But can accept input arguments and return an output
% Also, they have their own workspace (ie. different variable scope).
% Function name should match file name (so save this example as double_input.m).
% 'help double_input.m' returns the comments under line beginning function
function output = double_input(x)
	%double_input(x) returns twice the value of x
	output = 2*x;
end
double_input(6) % ans = 12


% You can also have subfunctions and nested functions.
% Subfunctions are in the same file as the primary function, and can only be
% called by functions in the file. Nested functions are defined within another
% functions, and have access to both its workspace and their own workspace.

% If you want to create a function without creating a new file you can use an
% anonymous function. Useful when quickly defining a function to pass to
% another function (eg. plot with fplot, evaluate an indefinite integral
% with quad, find roots with fzero, or find minimum with fminsearch).
% Example that returns the square of its input, assigned to the handle sqr:
sqr = @(x) x.^2;
sqr(10) % ans = 100
doc function_handle % find out more

% User input
a = input('Enter the value: ')

% Stops execution of file and gives control to the keyboard: user can examine
% or change variables. Type 'return' to continue execution, or 'dbquit' to exit
keyboard

% Reading in data (also xlsread/importdata/imread for excel/CSV/image files)
fopen(filename)

% Output
disp(a) % Print out the value of variable a
disp('Hello World') % Print out a string
fprintf % Print to Command Window with more control

% Conditional statements (the parentheses are optional, but good style)
if (a > 15)
	disp('Greater than 15')
elseif (a == 23)
	disp('a is 23')
else
	disp('neither condition met')
end

% Looping
% NB. looping over elements of a vector/matrix is slow!
% Where possible, use functions that act on whole vector/matrix at once
for k = 1:5
	disp(k)
end

k = 0;
while (k < 5)
	k = k + 1;
end

% Timing code execution: 'toc' prints the time since 'tic' was called
tic
A = rand(1000);
A*A*A*A*A*A*A;
toc

% Connecting to a MySQL Database
dbname = 'database_name';
username = 'root';
password = 'root';
driver = 'com.mysql.jdbc.Driver';
dburl = ['jdbc:mysql://localhost:8889/' dbname];
javaclasspath('mysql-connector-java-5.1.xx-bin.jar'); %xx depends on version, download available at http://dev.mysql.com/downloads/connector/j/
conn = database(dbname, username, password, driver, dburl);
sql = ['SELECT * from table_name where id = 22'] % Example sql statement
a = fetch(conn, sql) %a will contain your data


% Common math functions
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
exp(x)
sqrt(x)
log(x)
log10(x)
abs(x) %If x is complex, returns magnitude
min(x)
max(x)
ceil(x)
floor(x)
round(x)
rem(x)
rand % Uniformly distributed pseudorandom numbers
randi % Uniformly distributed pseudorandom integers
randn % Normally distributed pseudorandom numbers

%Complex math operations
abs(x) 	 % Magnitude of complex variable x
phase(x) % Phase (or angle) of complex variable x
real(x)  % Returns the real part of x (i.e returns a if x = a +jb)
imag(x)  % Returns the imaginary part of x (i.e returns b if x = a+jb)
conj(x)  % Returns the complex conjugate 


% Common constants
pi
NaN
inf

% Solving matrix equations (if no solution, returns a least squares solution)
% The \ and / operators are equivalent to the functions mldivide and mrdivide
x=A\b % Solves Ax=b. Faster and more numerically accurate than using inv(A)*b.
x=b/A % Solves xA=b

inv(A) % calculate the inverse matrix
pinv(A) % calculate the pseudo-inverse

% Common matrix functions
zeros(m,n) % m x n matrix of 0's
ones(m,n) % m x n matrix of 1's
diag(A) % Extracts the diagonal elements of a matrix A
diag(x) % Construct a matrix with diagonal elements listed in x, and zeroes elsewhere
eye(m,n) % Identity matrix
linspace(x1, x2, n) % Return n equally spaced points, with min x1 and max x2
inv(A) % Inverse of matrix A
det(A) % Determinant of A
eig(A) % Eigenvalues and eigenvectors of A
trace(A) % Trace of matrix - equivalent to sum(diag(A))
isempty(A) % Tests if array is empty
all(A) % Tests if all elements are nonzero or true
any(A) % Tests if any elements are nonzero or true
isequal(A, B) % Tests equality of two arrays
numel(A) % Number of elements in matrix
triu(x) % Returns the upper triangular part of x
tril(x) % Returns the lower triangular part of x
cross(A,B) %  Returns the cross product of the vectors A and B
dot(A,B) % Returns scalar product of two vectors (must have the same length)
transpose(A) % Returns the transpose of A
fliplr(A) % Flip matrix left to right
flipud(A) % Flip matrix up to down

% Matrix Factorisations
[L, U, P] = lu(A) % LU decomposition: PA = LU,L is lower triangular, U is upper triangular, P is permutation matrix
[P, D] = eig(A) % eigen-decomposition: AP = PD, P's columns are eigenvectors and D's diagonals are eigenvalues
[U,S,V] = svd(X) % SVD: XV = US, U and V are unitary matrices, S has non-negative diagonal elements in decreasing order

% Common vector functions
max     % largest component
min     % smallest component
length  % length of a vector
sort    % sort in ascending order
sum     % sum of elements
prod    % product of elements
mode    % modal value
median  % median value
mean    % mean value
std     % standard deviation
perms(x) % list all permutations of elements of x
find(x) % Finds all non-zero elements of x and returns their indexes, can use comparison operators, 
        % i.e. find( x == 3 ) returns indexes of elements that are equal to 3
        % i.e. find( x >= 3 ) returns indexes of elements greater than or equal to 3


% Classes
% Matlab can support object-oriented programming. 
% Classes must be put in a file of the class name with a .m extension. 
% To begin, we create a simple class to store GPS waypoints.
% Begin WaypointClass.m
classdef WaypointClass % The class name.
  properties % The properties of the class behave like Structures
    latitude 
    longitude 
  end
  methods 
    % This method that has the same name of the class is the constructor. 
    function obj = WaypointClass(lat, lon)
      obj.latitude = lat;
      obj.longitude = lon;
    end

    % Other functions that use the Waypoint object
    function r = multiplyLatBy(obj, n)
      r = n*[obj.latitude];
    end

    % If we want to add two Waypoint objects together without calling
    % a special function we can overload Matlab's arithmetic like so:
    function r = plus(o1,o2)
      r = WaypointClass([o1.latitude] +[o2.latitude], ...
                        [o1.longitude]+[o2.longitude]);
    end
  end
end
% End WaypointClass.m

% We can create an object of the class using the constructor
a = WaypointClass(45.0, 45.0)

% Class properties behave exactly like Matlab Structures.
a.latitude = 70.0
a.longitude = 25.0

% Methods can be called in the same way as functions
ans = multiplyLatBy(a,3)

% The method can also be called using dot notation. In this case, the object 
% does not need to be passed to the method.
ans = a.multiplyLatBy(a,1/3)

% Matlab functions can be overloaded to handle objects. 
% In the method above, we have overloaded how Matlab handles 
% the addition of two Waypoint objects.
b = WaypointClass(15.0, 32.0)
c = a + b

```

## More on Matlab

* [The official website](http://www.mathworks.com/products/matlab/)
* [The official MATLAB Answers forum](http://www.mathworks.com/matlabcentral/answers/)
* [Loren on the Art of MATLAB](http://blogs.mathworks.com/loren/)
* [Cleve's Corner](http://blogs.mathworks.com/cleve/)

---
category: tool
tool: messagepack
filename: learnmessagepack.mpac
contributors:
  - ["Gabriel Chuan", "https://github.com/gczh"]
---

MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. The benefits over other formats is that it's faster and smaller. 

In MessagePack, small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves. This makes MessagePack useful for efficient transmission over wire.

```

# 0. Understanding The Structure ====

JSON, 40 Bytes UTF-8
	
----------------------------------------------
| {"name":"John Doe","age":12}		         |
----------------------------------------------
|  {"         | 7B 22                        |
|    name     | 6E 61 6D 65                  |
|  ":"        | 22 3A 22                     |
|    John Doe | 4A 6F 68 6E 20 44 6F 65      |
|  ","        | 22 2C 22                     |
|    age      | 61 67 65                     |
|  ":         | 22 3A 20                     |
|    12       | 31 32                        |
|  }          | 7D                           |
----------------------------------------------


MessagePack, 27 Bytes UTF-8
	
----------------------------------------------
| ‚¤name¨John Doe£age.12                     |
----------------------------------------------
|  ‚¤         | 82 84                        |
|    name     | 6E 61 6D 65                  |
|  ¨          | A8                           |
|    John Doe | 4A 6F 68 6E 20 44 6F 65      |
|  £          | A3                           |
|    age      | 61 67 65                     |
|  .          | 0C                           |
|    12       | 31 32                        |
----------------------------------------------

# 1. JAVA ====

""" Installing with Maven
"""

<dependencies>
  ...
  <dependency>
    <groupId>org.msgpack</groupId>
    <artifactId>msgpack</artifactId>
    <version>${msgpack.version}</version>
  </dependency>
  ...
</dependencies>


""" Simple Serialization/Deserialization
"""

// Create serialize objects.
List<String> src = new ArrayList<String>();
src.add("msgpack");
src.add("kumofs");

MessagePack msgpack = new MessagePack();
// Serialize
byte[] raw = msgpack.write(src);

// Deserialize directly using a template
List<String> dst1 = msgpack.read(raw, Templates.tList(Templates.TString));
System.out.println(dst1.get(0));
System.out.println(dst1.get(1));

// Or, Deserialze to Value then convert type.
Value dynamic = msgpack.read(raw);
List<String> dst2 = new Converter(dynamic)
    .read(Templates.tList(Templates.TString));
System.out.println(dst2.get(0));
System.out.println(dst2.get(1));


# 2. RUBY ====

""" Installing the Gem
"""

gem install msgpack

""" Streaming API
"""

# serialize a 2-element array [e1, e2]
pk = MessagePack::Packer.new(io)
pk.write_array_header(2).write(e1).write(e2).flush

# deserialize objects from an IO
u = MessagePack::Unpacker.new(io)
u.each { |obj| ... }

# event-driven deserialization
def on_read(data)
  @u ||= MessagePack::Unpacker.new
  @u.feed_each(data) { |obj| ... }
end

# 3. NODE.JS ====

""" Installing with NPM
"""

npm install msgpack5 --save

""" Using in Node
"""

var msgpack = require('msgpack5')() // namespace our extensions
  , a       = new MyType(2, 'a')
  , encode  = msgpack.encode
  , decode  = msgpack.decode

msgpack.register(0x42, MyType, mytipeEncode, mytipeDecode)

console.log(encode({ 'hello': 'world' }).toString('hex'))
// 81a568656c6c6fa5776f726c64
console.log(decode(encode({ 'hello': 'world' })))
// { hello: 'world' }
console.log(encode(a).toString('hex'))
// d5426161
console.log(decode(encode(a)) instanceof MyType)
// true
console.log(decode(encode(a)))
// { value: 'a', size: 2 }

function MyType(size, value) {
  this.value = value
  this.size  = size
}

function mytipeEncode(obj) {
  var buf = new Buffer(obj.size)
  buf.fill(obj.value)
  return buf
}

function mytipeDecode(data) {
  var result = new MyType(data.length, data.toString('utf8', 0, 1))
    , i

  for (i = 0; i < data.length; i++) {
    if (data.readUInt8(0) != data.readUInt8(i)) {
      throw new Error('should all be the same')
    }
  }

  return result
}

```


# References

- [MessagePack](http://msgpack.org/index.html)
- [MsgPack vs. JSON: Cut your client-server exchange traffic by 50% with one line of code](http://indiegamr.com/cut-your-data-exchange-traffic-by-up-to-50-with-one-line-of-code-msgpack-vs-json/)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
filename: LearnBash-ms.sh
translators:
    - ["hack1m", "https://github.com/hack1m"]
lang: ms-my   
---

Bash adalah nama daripada unix shell, yang mana telah diagihkan sebagai shell untuk sistem operasi GNU dan sebagai shell lalai pada Linux dan Mac OS X. Hampir semua contoh di bawah boleh menjadi sebahagian daripada skrip shell atau dijalankan terus dalam shell.

[Baca lebih lanjut di sini.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# Baris pertama daripada skrip ialah shebang yang mana memberitahu sistem bagaimana untuk melaksana
# skrip: http://en.wikipedia.org/wiki/Shebang_(Unix)
# Seperti yang anda sudah gambarkan, komen bermula dengan #. Shebang juga ialah komen.

# Contoh mudah hello world:
echo Hello world!

# Setiap arahan bermula pada baris baru, atau selepas semikolon:
echo 'This is the first line'; echo 'This is the second line'

# Mengisytihar pembolehubah kelihatan seperti ini:
Variable="Some string"

# Tetapi bukan seperti ini:
Variable = "Some string"
# Bash akan memutuskan yang pembolehubah adalah arahan ia mesti laksanakan dan memberi ralat
# kerana ia tidak boleh dijumpai.

# Atau seperti ini:
Variable= 'Some string'
# Bash akan memutuskan yang ‘Beberapa rentetan’ adalah arahan ia mesti laksanakan dan memberi
# ralat kerana ia tidak dijumpai. (Dalam kes ini ‘Variable=' sebahagian dilihat
# sebagai penetapan pembolehubah sah hanya untuk skop ‘Beberapa rentetan’
# arahan.)

# Menggunakan pembolehubah:
echo $Variable
echo "$Variable"
echo '$Variable'
# Apabila anda guna pembolehubah itu sendiri - menetapkan, mengeksport, atau lain-lain - anda menulis
# nama ia tanpa $. Atau anda ingin menggunakan nilai pembolehubah, anda mesti guna $.
# Perlu diingatkan ‘(Petikan tunggal) tidak akan memperluaskan pembolehubah!

# Penggantian rentetan dalam pembolehubah
echo ${Variable/Some/A}
# Ini akan menukarkan sebutan pertama bagi "Some" dengan "A"

# Subrentetan daripada pembolehubah
Length=7
echo ${Variable:0:Length}
# Ini akan kembalikan hanya 7 aksara pertama pada nilai

# Nilai lalai untuk pembolehubah
echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
# Ini berfungsi untuk null (Foo=) dan rentetan kosong (Foo=“”); sifar (Foo=0) kembali 0.
# Perlu diingatkan ia hanya kembalikan nilai lalai dan tidak mengubah nilai pembolehubah.

# Pembolehubah terbina:
# Terdapat beberapa pembolehubah terbina berguna, seperti
echo "Last program's return value: $?"
echo "Script's PID: $$"
echo "Number of arguments passed to script: $#"
echo "All arguments passed to script: $@"
echo "Script's arguments separated into different variables: $1 $2..."

# Membaca nilai dari input:
echo "What's your name?"
read Name # Perlu diingatkan kita tidak perlu isytihar pembolehubah baru
echo Hello, $Name!

# Kita ada yang biasa jika struktur:
# guna 'man test' untuk maklumat lanjut tentang bersyarat
if [ $Name -ne $USER ]
then
    echo "Your name isn't your username"
else
    echo "Your name is your username"
fi

# Terdapat juga pelaksanaan bersyarat
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# Untuk guna && dan || bersama kenyataan ‘if’, anda perlu beberapa pasang daripada tanda kurung siku:
if [ $Name == "Steve" ] && [ $Age -eq 15 ]
then
    echo "This will run if $Name is Steve AND $Age is 15."
fi

if [ $Name == "Daniya" ] || [ $Name == "Zach" ]
then
    echo "This will run if $Name is Daniya OR Zach."
fi

# Eskspresi ia ditandai dengan format berikut:
echo $(( 10 + 5 ))

# Tidak seperti bahasa pengaturcaraan lain, bash adalah shell jadi ia berfungsi dalam konteks
# daripada direktori semasa. Anda boleh menyenaraikan fail dan direktori dalam direktori
# semasa dengan arahan ini:
ls

# Arahan ini mempunyai opsyen yang mengawal perlaksanaannya:
ls -l # Senarai setiap fail dan direktori pada baris yang berbeza

# Keputusan arahan sebelum boleh diberikan kepada arahan selepas sebagai input.
# arahan grep menapis input dengan memberi paten. Ini bagaimana kita boleh senaraikan
# fail .txt di dalam direktori semasa:
ls -l | grep "\.txt"

# Anda boleh mengubah hala arahan input dan output (stdin, stdout, dan stderr).
# Baca dari stdin sampai ^EOF$ dan menulis ganti hello.py dengan baris
# antara “EOF":
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Jalankan hello.py dengan pelbagai penghantaran semula stdin, stdout, dan stderr:
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# Output ralat akan menulis ganti fail jika ia wujud,
# jika anda ingin menambah sebaliknya, guna ‘>>”:
python hello.py >> "output.out" 2>> "error.err"

# Menulis ganti output.out, menambah ke error.err, dan mengira baris:
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# Jalankan arahan dan cetak fail Deskriptor (e.g. /dev/fd/123)
# lihat: man fd
echo <(echo "#helloworld")

# Menulis ganti output.out dengan “#helloworld":
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# Membersihkan fail semantara keseluruhan (tambah ‘-i’ untuk interaktif)
rm -v output.out error.err output-and-error.log

# Arahan boleh digantikan dalam arahan lain menggunakan $():
# Arahan berikut memaparkan jumlah fail dan direktori dalam
# direktori semasa.
echo "There are $(ls | wc -l) items here."

# Perkara yang sama boleh dilakukan dengan menggunakan backticks `` tetapi ia tidak boleh bersarang - cara yang terbaik
# ialah menggunakan $( ).
echo "There are `ls | wc -l` items here."

# Bash menggunakan penyataan case yang berfungsi sama seperti ‘switch’ pada Java dan C++:
case "$Variable" in
    # Senarai paten untuk syarat yang ada ingin ketemui
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# ‘for loops iterate' untuk sebanyak mana argumen yang ditetapkan:
# Kandungan dari $Variable dicetakan sebanyak tiga kali.
for Variable in {1..3}
do
    echo "$Variable"
done

# Atau tulis ia cara "traditional for loop":
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Ia juga boleh digunakan untuk bertindak ke atas fail..
# Ini akan menjalankan arahan 'cat' pada file1 dan file2
for Variable in file1 file2
do
    cat "$Variable"
done

# ..atau output daripada arahan
# Ini akan 'cat' output dari ls.
for Output in $(ls)
do
    cat "$Output"
done

# while loop:
while [ true ]
do
    echo "loop body here..."
    break
done

# Anda juga boleh mendefinasikan fungsi
# Definasi:
function foo ()
{
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
}

# atau lebih mudah
bar ()
{
    echo "Another way to declare functions!"
    return 0
}

# Memanggil fungsi
foo "My name is" $Name

# Terdapat banyak arahan yang berguna yang perlu anda belajar:
# cetak 10 baris terakhir dalam file.txt
tail -n 10 file.txt
# cetak 10 baris pertama dalam file.txt
head -n 10 file.txt
# menyusun baris fail.txt
sort file.txt
# laporan atau meninggalkan garisan berulang, dengan -d ia melaporkan
uniq -d file.txt
# cetak hanya kolum pertama sebelum aksara ','
cut -d ',' -f 1 file.txt
# menggantikan setiap kewujudan 'okay' dengan 'great' dalam file.txt, (serasi regex)
sed -i 's/okay/great/g' file.txt
# cetak ke stdoout semua baris dalam file.txt yang mana sepadan beberapa regex
# contoh cetak baris yang mana bermula dengan “foo” dan berakhir dengan “bar”
grep "^foo.*bar$" file.txt
# beri opsyen “-c” untuk sebaliknya mencetak jumlah baris sepadan regex
grep -c "^foo.*bar$" file.txt
# jika anda secara literal mahu untuk mencari rentetan,
# dan bukannya regex, guna fgrep (atau grep -F)
fgrep "^foo.*bar$" file.txt


# Baca dokumentasi Bash shell terbina dengan 'help' terbina:
help
help help
help for
help return
help source
help .

# Baca dokumentasi Bash manpage dengan man
apropos bash
man 1 bash
man bash

# Baca dokumentasi info dengan info (? for help)
apropos info | grep '^info.*('
man info
info info
info 5 info

# Baca dokumentasi bash info:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: clojure
filename: learnclojure-ms.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Burhanuddin Baharuddin", "https://github.com/burhanloey"]
lang: ms-my
---

Clojure ialah salah satu bahasa pengaturcaraan dalam keluarga Lisp yang dibangunkan untuk Java Virtual Machine. Ia lebih
menekankan kepada konsep [functional programming](https://en.wikipedia.org/wiki/Functional_programming) jika dibandingkan
dengan Common Lisp, tetapi juga menyediakan kemudahan [STM](https://en.wikipedia.org/wiki/Software_transactional_memory) 
untuk mengendalikan *state* apabila diperlukan.

Gabungan tersebut membolehkan Clojure untuk mengendalikan beberapa proses serentak (*concurrency*) dengan mudah,
dan kebiasaannya secara automatik.

(Anda perlukan Clojure versi 1.2 ke atas)


```clojure
; Komen bermula dengan koma bertitik (semicolon).

; Clojure ditulis dalam bentuk yang seragam, iaitu
; senarai perkataan di dalam kurungan (parentheses), dipisahkan dengan ruang kosong (whitespace).
;
; Pembaca Clojure akan menganggap bahawa perkataan pertama dalam senarai tersebut
; sebagai `function` atau `macro` untuk digunakan, dan yang selebihnya sebagai arguments.

; Panggilan pertama di dalam fail Clojure mestilah bermula dengan ns, untuk menentukan `namespace`
(ns learnclojure)

; Contoh-contoh asas yang lain:

; str akan mewujudkan sebuah string daripada beberapa `argument`
(str "Hello" " " "World") ; => "Hello World"

; Operasi matematik pun mudah
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; Tanda = boleh digunakan untuk membuat perbandingan yang sama
(= 1 1) ; => true
(= 2 1) ; => false

; Gunakan not untuk mengubah lojik
(not true) ; => false

; Bentuk `nested` berfungsi seperti yang dijangkakan
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; Type (Jenis Data)
;;;;;;;;;;;;;

; Clojure menggunakan jenis `object` dari Java untuk `boolean`, `string` dan nombor.
; Gunakan `class` untuk memeriksa jenis sesebuah data.
(class 1) ; Secara default jenis data untuk `Integer` ialah java.lang.Long
(class 1.); Jenis data untuk Float pula ialah java.lang.Double
(class ""); `String` sentiasa berada dalam tanda petikan (quotation mark), dan merupakan java.lang.String
(class false) ; `Boolean` ialah java.lang.Boolean
(class nil); Nilai "null" dipanggil nil

; Jika mahu membuat senarai data secara harfiah, gunakan ' untuk elakkan senarai tersebut
; daripada terus berfungsi
'(+ 1 2) ; => (+ 1 2)
; (singkatan untuk (quote (+ 1 2)))

; Senarai data secara harfiah boleh berfungsi menggunakan eval
(eval '(+ 1 2)) ; => 3

; Collection & Sequence (Koleksi & Urutan)
;;;;;;;;;;;;;;;;;;;

; `List` ialah struktur data `linked-list`, manakala `Vector` pula berasaskan `array`.
; `Vector` dan `List` juga merupakan class dari Java!
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; Sesebuah list boleh ditulis seperti (1 2 3), tetapi kita perlu meletakkan '
; untuk mengelakkannya daripada berfungsi.
; Juga, (list 1 2 3) adalah sama dengan '(1 2 3)

; "Collections" hanyalah kumpulan data
; Kedua-dua list dan vector ialah collection:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; "Sequences" (seq) ialah kriteria untuk sesebuah senarai data.
; Hanya list yang dikira sebagai seq.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; Sesebuah seq hanya perlukan satu kemasukan data untuk diakses.
; Jadi, seq yang boleh jadi `lazy` (malas) -- boleh menjadi tidak terkira (infinite):
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (tiada penghujung)
(take 4 (range)) ;  (0 1 2 3)

; Gunakan cons untuk menambah sesuatu di awal sesebuah list atau vector
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; Conj akan menambah sesuatu ke dalam collection dengan paling berkesan.
; Untuk list, data tersebut dimasukkan di permulaan. Untuk vector, dimasukkan di pengakhiran.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; Gunakan concat untuk menggabungkan list atau vector
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; Gunakan filter dan map untuk berinteraksi dengan data di dalam collection
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; Gunakan reduce untuk dikecilkan (kepada satu nilai)
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; Reduce boleh diberi nilai permulaan
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; Function
;;;;;;;;;;;;;;;;;;;;;

; Gunakan fn untuk membuat `function`. Sesebuah function pasti memulangkan semula
; hasil daripada barisan yang terakhir.
(fn [] "Hello World") ; => fn

; (Anda perlukan satu lagi kurungan supaya function tersebut dikira)
((fn [] "Hello World")) ; => "Hello World"

; Anda boleh membuat var menggunakan def
(def x 1)
x ; => 1

; Tetapkan sebuah function ke dalam var
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; Proses di atas boleh diringkaskan menggunakan defn
(defn hello-world [] "Hello World")

; Tanda [] merupakan senarai argument untuk function tersebut.
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; Cara ini juga boleh digunakan untuk membuat function dengan lebih ringkas:
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; Anda juga boleh membuat satu function yang mempunyai beberapa bilangan argument
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; Function boleh diberi argument ekstra dalam bentuk seq
(defn count-args [& args]
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"

; Anda boleh letakkan sekali argument biasa dan argument ekstra
(defn hello-count [name & args]
  (str "Hello " name ", you passed " (count args) " extra args"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"


; Map
;;;;;;;;;;

; Hash map dan array map menggunakan `interface` yang sama. Hash map lebih laju untuk diakses
; tetapi tidak mengekalkan urutan.
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Arraymap akan bertukar menjadi hashmap secara automatik untuk kebanyakan operasi
; apabila mereka menjadi semakin besar, jadi anda tidak perlu bimbang.

; Map boleh menggunakan apa-apa sahaja jenis data sebagai key, tetapi kebiasaannya keyword adalah yang terbaik
; Keyword adalah sama seperti string cuma lebih efisyen
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; Oh, sebelum terlupa, tanda koma di atas hanya dianggap seperti whitespace, tak buat apa-apa.
; Dapatkan nilai daripada map dengan menggunakannya seperti function
(stringmap "a") ; => 1
(keymap :a) ; => 1

; Keyword juga boleh digunakan untuk mendapatkan nilai daripada map tersebut!
(:b keymap) ; => 2

; Jangan cuba teknik di atas menggunakan string, tak jadi.
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; Apabila key yang digunakan tidak wujud, map akan memberi nil
(stringmap "d") ; => nil

; Gunakan assoc untuk menambah key yang baru ke dalam hash-map
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; Tetapi ingat, data dalam clojure adalah `immutable` (tidak berubah)!
keymap ; => {:a 1, :b 2, :c 3}

; Gunakan dissoc untuk membuang key
(dissoc keymap :a :b) ; => {:c 3}

; Set
;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; Tambah data menggunakan conj
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; Buang data menggunakan disj
(disj #{1 2 3} 1) ; => #{2 3}

; Periksa kewujudan data dengan menggunakan set tersebut sebagai function:
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; Ada pelbagai lagi function untuk set di namespace clojure.sets.

; Form yang berguna
;;;;;;;;;;;;;;;;;

; Lojik dalam clojure hanyalah sebuah macro, dan kelihatan seperti
; yang lain
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; Gunakan let untuk membuat binding sementara
(let [a 1 b 2]
  (> a b)) ; => false

; Kumpulkan beberapa statement sekali menggunakan do
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; Function sebenarnya ada do secara tersirat
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; Let pun sama
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")


; Gunakan `threading macro` (-> dan ->>) untuk menulis penggubahan data
; dengan lebih jelas.

; Macro "thread-first" (->) memasukkan hasil perkiraan ke setiap form
; yang selepasnya, sebagai argument pertama (item yang kedua)
(->  
   {:a 1 :b 2} 
   (assoc :c 3) ;=> (assoc {:a 1 :b 2} :c 3)
   (dissoc :b)) ;=> (dissoc (assoc {:a 1 :b 2} :c 3) :b)

; Code di atas boleh ditulis seperti ini:
; (dissoc (assoc {:a 1 :b 2} :c 3) :b)
; dan hasilnya ialah {:a 1 :c 3}

; Yang dua anak panah pula membuat benda yang sama, tetapi memasukkan hasil perkiraan 
; setiap baris ke pengakhiran form selepasnya. Cara ini berguna untuk operasi 
; yang melibatkan collection:
(->>
   (range 10)
   (map inc)     ;=> (map inc (range 10)
   (filter odd?) ;=> (filter odd? (map inc (range 10))
   (into []))    ;=> (into [] (filter odd? (map inc (range 10)))
                 ; Result: [1 3 5 7 9]

; Jika anda mahu lebih fleksibel untuk meletakkan hasil perkiraan,
; anda boleh menggunakan macro `as->`. Dengan menggunakan macro tersebut,
; anda boleh menentukan nama untuk output dan menggunakannya semula
; ke dalam operasi berangkai:

(as-> [1 2 3] input
  (map inc input);=> You can use last transform's output at the last position
  (nth input 2) ;=>  and at the second position, in the same expression
  (conj [4 5 6] input [8 9 10])) ;=> or in the middle !



; Module
;;;;;;;;;;;;;;;

; Gunakan "use" untuk mendapatkan semua function daripada sesebuah module
(use 'clojure.set)

; Sekarang kita boleh menggunakan operasi untuk set
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; Anda juga boleh memilih sebahagian daripada function untuk diimport
(use '[clojure.set :only [intersection]])

; Gunakan require untuk mengimport sesebuah module
(require 'clojure.string)

; Gunakan / untuk menggunakan function daripada module
; Di sini, nama module tersebut ialah clojure.string dan function-nya ialah blank?
(clojure.string/blank? "") ; => true

; Anda juga boleh memberi nama yang lebih ringkas untuk module semasa import
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (#"" ialah ungkapan untuk regular expression, regex)

; Anda boleh menggunakan require (dan use, tetapi elakkan) daripada namespace menggunakan :require.
; Anda tidak perlu menulis semula nama module dengan cara ini.
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java mengandungi banyak standard library yang kita boleh manfaatkan, jadi
; anda patut tahu bagaimana untuk menggunakannya.

; Gunakan import untuk load module java
(import java.util.Date)

; Anda juga boleh import menggunakan ns.
(ns test
  (:import java.util.Date
           java.util.Calendar))

; Gunakan nama class berserta "." di hujungnya untuk membuat object baru
(Date.) ; <object date>

; Gunakan . untuk menggunakan method. Atau gunakan shortcut seperti ".method"
(. (Date.) getTime) ; <sebuah timestamp>
(.getTime (Date.)) ; sama sahaja.

; Gunakan / untuk menggunakan static method
(System/currentTimeMillis) ; <sebuah timestamp> (System sentiasa wujud dalam Java)

; Gunakan doto untuk menjadikan proses yang melibatkan class mutable (boleh berubah) lebih mudah
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => Sebuah Date. yang ditetapkan kepada 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; Software Transactional Memory ialah mekanisme dalam Clojure untuk mengendalikan
; state yang kekal berterusan. Ada beberapa kaedah dalam Clojure yang menggunakan teknik tersebut.

; Atom adalah yang paling mudah. Letakkannya sewaktu meletakkan nilai permulaan.
(def my-atom (atom {}))

; Kemas kini sebuah atom menggunakan swap!.
; swap! mengambil satu function dan menggunakannya menggunakan nilai asal atom
; sebagai argument pertama, dan argument selebihnya sebagai argument kedua
(swap! my-atom assoc :a 1) ; Tetapkan my-atom kepada hasil perkiraan (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Tetapkan my-atom kepada hasil perkiraan (assoc {:a 1} :b 2)

; Gunakan '@' untuk mendapatkan nilai daripada atom
my-atom  ;=> Atom<#...> (memberi object atom itu sendiri)
@my-atom ; => {:a 1 :b 2}

; Ini adalah contoh untuk mengira menggunakan atom
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; Kaedah lain yang menggunakan STM ialah ref dan agent.
; Ref: http://clojure.org/refs
; Agent: http://clojure.org/agents
```

### Bacaan Lanjut

Ini masih belum lengkap, tetapi harap-harap cukup untuk membuatkan anda lebih bersedia.

Clojure.org mempunyai banyak artikel:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org mempunyai dokumentasi berserta contoh untuk menggunakan kebanyakan function teras:
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure ialah cara yang baik untuk mengasah skill Clojure dan functional programming:
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org (yup, serius) juga mengandungi beberapa artikel sebagai permulaan:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
filename: coffeescript-ms.coffee
translators:
    - ["hack1m", "https://github.com/hack1m"]
lang: ms-my
---

CoffeeScript adalah bahasa kecil yang menyusun/kompil satu-per-satu menjadi setara JavaScript, dan tidak ada interpretasi di runtime.
Sebagai salah satu pengganti kepada JavaScript, CoffeeScript mencuba yang terbaik untuk output kod JavaScript yang mudah dibaca, cantik-dicetak dan berfungsi lancar, yang mana berfungsi baik pada setiap runtime JavaScript.

Lihat juga [Laman sesawang CoffeeScript](http://coffeescript.org/), yang mana ada tutorial lengkap untuk CoffeeScript.

```coffeescript
# CoffeeScript adalah bahasa hipster.
# Ia beredar mengikut trend kebanyakkan bahasa moden.
# Jadi komen sama seperti Ruby dan Python, ia menggunakan simbol nombor.

###
Blok komen seperti ini, dan ia terjemah terus ke '/ *'s dan '* /'s
untuk keputusan kod JavaScript.

Sebelum meneruskan anda perlu faham kebanyakkan daripada
JavaScript adalah semantik.
###

# Menetapkan:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# Bersyarat:
number = -42 if opposite #=> if(opposite) { number = -42; }

# Fungsi:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

fill = (container, liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."
#=>var fill;
#
#fill = function(container, liquid) {
#  if (liquid == null) {
#    liquid = "coffee";
#  }
#  return "Filling the " + container + " with " + liquid + "...";
#};

# Julat:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# Objek:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#    "root": Math.sqrt,
#    "square": square,
#    "cube": function(x) { return x * square(x); }
#   };

# Splats:
race = (winner, runners...) ->
  print winner, runners
#=>race = function() {
#    var runners, winner;
#    winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#    return print(winner, runners);
#  };

# Kewujudan:
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# Pemahaman array:
cubes = (math.cube num for num in list)
#=>cubes = (function() {
#	  var _i, _len, _results;
#	  _results = [];
# 	for (_i = 0, _len = list.length; _i < _len; _i++) {
#		  num = list[_i];
#		  _results.push(math.cube(num));
#	  }
#	  return _results;
# })();

foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
#=>foods = ['broccoli', 'spinach', 'chocolate'];
#
#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
#  food = foods[_k];
#  if (food !== 'chocolate') {
#    eat(food);
#  }
#}
```

## Sumber tambahan

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
filename: javascript-ms.js
translators:
    - ["abdalim", "https://github.com/abdalim"]
lang: ms-my
---

Javascript dicipta oleh Brendan Eich dari Netscape pada 1995. Pada awalnya, ia
dicipta sebagai bahasa skrip yang ringkas untuk laman web, melengkapi penggunaan
Java untuk aplikasi web yang lebih rumit, namun begitu, integrasi rapat pada
halaman web dan sokongan tersedia dalam pelayar web telah menyebabkan ia menjadi
lebih kerap digunakan berbanding Java pada bahagian hadapan laman web.

Namun begitu, Javascript tidak terhad pada pelayar web; Node.js, sebuah projek
yang menyediakan 'runtime' berdiri sendiri untuk enjin V8 Google Chrome sedang
kian mendapat sambutan yang hangat.

```js
// Komentar adalah seperti dalam C. Komentar sebaris bermula dengan dua sengkang
/* dan komentar banyak baris bermula dengan sengkang-bintang
   dan berakhir dengan bintang-sengkang */

// Pernyataan boleh ditamatkan dengan ';'
doStuff();

// ... tetapi ia tidak wajib, kerana koma bertitik secara automatik akan
// dimasukkan dimana tempat yang ada baris baru, kecuali dalam kes - kes
// tertentu.
doStuff()

// Disebabkan kes - kes itu boleh menyebabkan hasil yang tidak diduga, kami
// akan sentiasa menggunakan koma bertitik dalam panduan ini.

///////////////////////////////////
// 1. Nombor, String dan Operator

// Javascript mempunyai satu jenis nombor (iaitu 64-bit IEEE 754 double).
// Double mempunyai 52-bit mantissa, iaitu ia cukup untuk menyimpan integer
//    sehingga 9✕10¹⁵ secara tepatnya.
3; // = 3
1.5; // = 1.5

// Sebahagian aritmetic asas berfungsi seperti yang anda jangkakan.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// Termasuk pembahagian tidak rata.
5 / 2; // = 2.5

// Dan pembahagian modulo.
10 % 2; // = 0
30 % 4; // = 2
18.5 % 7; // = 4.5

// Operasi bitwise juga boleh digunakan; bila anda melakukan operasi bitwise,
// float anda akan ditukarkan kepada int bertanda *sehingga* 32 bit.
1 << 2; // = 4

// Keutamaan ditekankan menggunakan kurungan.
(1 + 3) * 2; // = 8

// Terdapat tiga nilai nombor-tidak-nyata istimewa
Infinity; // hasil operasi seperti 1/0
-Infinity; // hasil operasi seperti -1/0
NaN; // hasil operasi seperti 0/0, bermaksud 'Bukan Sebuah Nombor'

// Terdapat juga jenis boolean
true;
false;

// Talian dicipta dengan ' atau ''.
'abc';
"Hello, world";

// Penafian menggunakan simbol !
!true; // = tidak benar
!false; // = benar

// Sama ialah ===
1 === 1; // = benar
2 === 1; // = tidak benar

// Tidak sama ialah !==
1 !== 1; // = tidak benar
2 !== 1; // = benar

// Lagi perbandingan
1 < 10; // = benar
1 > 10; // = tidak benar
2 <= 2; // = benar
2 >= 2; // = benar

// Talian disambungkan dengan +
"Hello " + "world!"; // = "Hello world!"

// dan dibandingkan dengan < dan >
"a" < "b"; // = benar

// Paksaan jenis dilakukan untuk perbandingan menggunakan dua sama dengan...
"5" == 5; // = benar
null == undefined; // = benar

// ...melainkan anda menggunakan ===
"5" === 5; // = tidak benar
null === undefined; // = tidak benar

// ...yang boleh menghasilkan keputusan yang pelik...
13 + !0; // 14
"13" + !0; // '13true'

// Anda boleh akses huruf dalam perkataan dengan `charAt`
"This is a string".charAt(0);  // = 'T'

// ...atau menggunakan `substring` untuk mendapatkan bahagian yang lebih besar.
"Hello world".substring(0, 5); // = "Hello"

// `length` adalah ciri, maka jangan gunakan ().
"Hello".length; // = 5

// Selain itu, terdapat juga `null` dan `undefined`.
null;      // digunakan untuk menandakan bukan-nilai yang disengajakan
undefined; // digunakan untuk menandakan nilai yang tidak wujud pada waktu ini (walaupun `undefined` adalah nilai juga)

// false, null, undefined, NaN, 0 dan "" adalah tidak benar; semua selain itu adalah benar.
// Peringatan, 0 adalah tidak benar dan "0" adalah benar, walaupun 0 == "0".

///////////////////////////////////
// 2. Pembolehubah, Array dan Objek

// Pembolehubah digunakan dengan kata kunci 'var'. Javascript ialah sebuah
// bahasa aturcara yang jenisnya dinamik, maka anda tidak perlu spesifikasikan
// jenis pembolehubah. Penetapan menggunakan satu '=' karakter.
var someVar = 5;

// jika anda tinggalkan kata kunci var, anda tidak akan dapat ralat...
someOtherVar = 10;

// ...tetapi pembolehubah anda akan dicipta di dalam skop global, bukan di
// dalam skop anda menciptanya.

// Pembolehubah yang dideklarasikan tanpa ditetapkan sebarang nilai akan
// ditetapkan kepada undefined.
var someThirdVar; // = undefined

// jika anda ingin mendeklarasikan beberapa pembolehubah, maka anda boleh
// menggunakan koma sebagai pembahagi
var someFourthVar = 2, someFifthVar = 4;

// Terdapat cara mudah untuk melakukan operasi - operasi matematik pada
// pembolehubah:
someVar += 5; // bersamaan dengan someVar = someVar +5; someVar sama dengan 10 sekarang
someVar *= 10; // sekarang someVar bernilai 100

// dan cara lebih mudah untuk penambahan atau penolakan 1
someVar++; // sekarang someVar ialah 101
someVar--; // kembali kepada 100

// Array adalah senarai nilai yang tersusun, yang boleh terdiri daripada
// pembolehubah pelbagai jenis.
var myArray = ["Hello", 45, true];

// Setiap ahli array boleh diakses menggunakan syntax kurungan-petak.
// Indeks array bermula pada sifar.
myArray[1]; // = 45

// Array boleh diubah dan mempunyai panjang yang tidak tetap dan boleh ubah.
myArray.push("World");
myArray.length; // = 4

// Tambah/Ubah di index yang spesifik
myArray[3] = "Hello";

// Objek javascript adalah sama dengan "dictionaries" atau "maps" dalam bahasa
// aturcara yang lain: koleksi pasangan kunci-nilai yang tidak mempunyai
// sebarang susunan.
var myObj = {key1: "Hello", key2: "World"};

// Kunci adalah string, tetapi 'quote' tidak diperlukan jika ia adalah pengecam
// javascript yang sah. Nilai boleh mempunyai sebarang jenis.
var myObj = {myKey: "myValue", "my other key": 4};

// Ciri - ciri objek boleh juga diakses menggunakan syntax subskrip (kurungan-
// petak),
myObj["my other key"]; // = 4

// ... atau menggunakan syntax titik, selagi kuncinya adalah pengecam yang sah.
myObj.myKey; // = "myValue"

// Objek adalah boleh diubah; nilai boleh diubah dan kunci baru boleh ditambah.
myObj.myThirdKey = true;

// Jika anda cuba untuk akses nilai yang belum ditetapkan, anda akan mendapat
// undefined.
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. Logik dan Struktur Kawalan

// Syntax untuk bahagian ini adalah hampir sama dengan Java.

// Struktur `if` berfungsi seperti yang anda jangkakan.
var count = 1;
if (count == 3){
    // dinilai jika count ialah 3
} else if (count == 4){
    // dinilai jika count ialah 4
} else {
    // dinilai jika count bukan 3 atau 4
}

// Sama juga dengan `while`.
while (true){
    // Sebuah ulangan yang tidak terhingga!
    // An infinite loop!
}

// Ulangan do-while adalah sama dengan ulangan while, kecuali ia akan diulang
// sekurang-kurangnya sekali.
var input;
do {
    input = getInput();
} while (!isValid(input))

// Ulangan `for` adalah sama dengan C dan Java:
// Persiapan; kondisi untuk bersambung; pengulangan.
for (var i = 0; i < 5; i++){
    // akan berulang selama 5 kali
}

// Pernyataan ulangan For/In akan mengulang setiap ciri seluruh jaringan
// 'prototype'
var description = "";
var person = {fname:"Paul", lname:"Ken", age:18};
for (var x in person){
    description += person[x] + " ";
}

// Jika anda cuma mahu mengambil kira ciri - ciri yang ditambah pada objek it
// sendiri dan bukan 'prototype'nya, sila gunakan semakan hasOwnProperty()
var description = "";
var person = {fname:"Paul", lname:"Ken", age:18};
for (var x in person){
    if (person.hasOwnProperty(x)){
        description += person[x] + " ";
    }
}

// for/in tidak sepatutnya digunakan untuk mengulang sebuah Array di mana
// indeks susunan adalah penting.
// Tiada sebarang jaminan bahawa for/in akan mengembalikan indeks dalam
// mana - mana susunan

// && adalah logikal dan, || adalah logikal atau
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear";
}
if (colour == "red" || colour == "blue"){
    // warna adalah sama ada 'red' atau 'blue'
}

// && dan || adalah "lintar pintas", di mana ia berguna untuk menetapkan
// nilai asal.
var name = otherName || "default";


// Pernyataan `switch` menyemak persamaan menggunakan `===`.
// gunakan pernyataan `break` selepas setiap kes
// atau tidak, kes - kes selepas kes yang betul akan dijalankan juga.
grade = 'B';
switch (grade) {
  case 'A':
    console.log("Great job");
    break;
  case 'B':
    console.log("OK job");
    break;
  case 'C':
    console.log("You can do better");
    break;
  default:
    console.log("Oy vey");
    break;
}


///////////////////////////////////
// 4. Functions, Skop dan Closures

// Function javascript dideklarasikan dengan kata kunci `function`.
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"

// Perhatikan yang nilai yang dikembalikan mesti bermula pada baris yang sama
// dengan kata kunci `return`, jika tidak, anda akan sentiasa mengembalikan
// `undefined` disebabkan kemasukan 'semicolon' secara automatik. Sila berjaga -
// jaga dengan hal ini apabila menggunakan Allman style.
function myFunction(){
    return // <- semicolon dimasukkan secara automatik di sini
    {thisIsAn: 'object literal'}
}
myFunction(); // = undefined

// Function javascript adalah objek kelas pertama, maka ia boleh diberikan
// nama pembolehubah yang lain dan diberikan kepada function yang lain sebagai
// input - sebagai contoh, apabila membekalkan pengendali event:
function myFunction(){
    // kod ini akan dijalankan selepas 5 saat
}
setTimeout(myFunction, 5000);
// Nota: setTimeout bukan sebahagian daripada bahasa JS, tetapi ia disediakan
// oleh pelayar web dan Node.js.

// Satu lagi function yang disediakan oleh pelayar web adalah setInterval
function myFunction(){
    // kod ini akan dijalankan setiap 5 saat
}
setInterval(myFunction, 5000);

// Objek function tidak perlu dideklarasikan dengan nama - anda boleh menulis
// function yang tidak bernama didalam input sebuah function lain.
setTimeout(function(){
    // kod ini akan dijalankan dalam 5 saat
}, 5000);

// Javascript mempunyai skop function; function mempunyai skop mereka
// tersendiri tetapi blok tidak.
if (true){
    var i = 5;
}
i; // = 5 - bukan undefined seperti yang anda jangkakan di dalam bahasa blok-skop

// Ini telah menyebabkan corak biasa iaitu "immediately-executing anonymous
// functions", yang mengelakkan pembolehubah sementara daripada bocor ke
// skop global.
(function(){
    var temporary = 5;
    // Kita boleh akses skop global dengan menetapkan nilai ke "objek global",
    // iaitu dalam pelayar web selalunya adalah `window`. Objek global mungkin
    // mempunyai nama yang berlainan dalam alam bukan pelayar web seperti Node.js.
    window.permanent = 10;
})();
temporary; // akan menghasilkan ralat ReferenceError
permanent; // = 10

// Salah satu ciri terhebat Javascript ialah closure. Jika sebuah function
// didefinisikan di dalam sebuah function lain, function yang di dalam akan
// mempunyai akses kepada semua pembolehubah function yang di luar, mahupun
// selepas function yang di luar tersebut selesai.
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!";
    // Function dalam diletakkan di dalam skop lokal secara asal, seperti
    // ia dideklarasikan dengan `var`.
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout adalah tak segerak atau asinkroni, maka function sayHelloInFiveSeconds akan selesai serta merta, dan setTimeout akan memanggil
    // inner selepas itu. Walaubagaimanapun, disebabkan inner terletak didalam
    // sayHelloInFiveSeconds, inner tetap mempunyai akses kepada pembolehubah
    // `prompt` apabila ia dipanggil.
}
sayHelloInFiveSeconds("Adam"); // akan membuka sebuah popup dengan "Hello, Adam!" selepas 5s

///////////////////////////////////
// 5. Lagi tentang Objek, Constructor dan Prototype

// Objek boleh mengandungi function.
var myObj = {
    myFunc: function(){
        return "Hello world!";
    }
};
myObj.myFunc(); // = "Hello world!"

// Apabila function sesebuah object dipanggil, ia boleh mengakses objek asalnya
// dengan menggunakan kata kunci `this`.
myObj = {
    myString: "Hello world!",
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = "Hello world!"

// Nilai sebenar yang ditetapkan kepada this akan ditentukan oleh bagaimana
// sesebuah function itu dipanggil, bukan dimana ia didefinisikan. Oleh it,
// sesebuah function tidak akan berfungsi jika ia dipanggil bukan pada konteks
// objeknya.
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// Sebaliknya, sebuah function boleh ditetapkan kepada objek dan mendapat akses
// kepada objek itu melalui `this`, walaupun ia tidak ditetapkan semasa ia
// didefinisikan.
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"

// Kita juga boleh menentukan konteks untuk sebuah function dijalankan apabila
// ia dipanggil menggunakan `call` atau `apply`.

var anotherFunc = function(s){
    return this.myString + s;
}
anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"

// Function `apply` adalah hampir sama, tetapi ia mengambil sebuah array
// sebagai senarai input.

anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!"

// Ini sangat berguna apabila menggunakan sebuah function yang menerima senarai
// input dan anda mahu menggunakan sebuah array sebagai input.

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// Tetapi, `call` dan `apply` adalah hanya sementara, sebagaimana hidup ini.
// Apabila kita mahu ia kekal, kita boleh menggunakan `bind`.

var boundFunc = anotherFunc.bind(myObj);
boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"

// `bind` boleh juga digunakan untuk menggunakan sebuah function tidak
// sepenuhnya (curry).

var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// Apabila anda memanggil sebuah function dengan kata kunci `new`, sebuah
// objek baru akan dicipta dan dijadikan tersedia kepada function itu melalui
// kata kunci `this`. Function yang direka bentuk untuk dipanggil sebegitu rupa
// dikenali sebagai constructors.

var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// Setiap objek JavaScript mempunyai `prototype`. Apabila anda akses sesuatu
// ciri sebuah objek yang tidak wujud dalam objek sebenar itu, interpreter akan
// mencari ciri itu didalam `prototype`nya.

// Sebahagian implementasi JS membenarkan anda untuk akses prototype sebuah
// objek pada ciri istimewa `__proto__`. Walaupun ini membantu dalam menerangkan
// mengenai prototypes, ia bukan sebahagian dari piawai; kita akan melihat
// cara - cara piawai untuk menggunakan prototypes nanti.
var myObj = {
    myString: "Hello world!"
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// Ini berfungsi untuk function juga.
myObj.myFunc(); // = "hello world!"

// Sudah pasti, jika ciri anda bukan pada prototype anda, prototype kepada
// prototype anda akan disemak, dan seterusnya.
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

// Tiada penyalinan terlibat disini; setiap objek menyimpan rujukan kepada
// prototypenya sendiri. Ini bermaksud, kita boleh mengubah prototypenya dan
// pengubahsuaian itu akan dilihat dan berkesan dimana sahaja.
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

// Kami menyatakan yang `__proto__` adalah bukan piawai, dan tiada cara rasmi
// untuk mengubah prototype sesebuah objek. Walaubagaimanapun, terdapat dua
// cara untuk mencipta objek baru dengan sesebuah prototype.

// Yang pertama ialah Object.create, yang merupakan tambahan terbaru pada JS,
// dan oleh itu tiada dalam semua implementasi buat masa ini.
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// Cara kedua, yang boleh digunakan dimana sahaja, adalah berkaitan dengan
// constructor. Constructors mempunyai sebuah ciri yang dipanggil prototype.
// Ini *bukan* prototype constructor terbabit; tetapi, ia adalah prototype yang
// diberikan kepada objek baru apabila ia dicipta menggunakan constructor dan
// kata kunci new.
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function(){
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// Jenis yang terbina sedia seperti string dan nombor juga mempunyai constructor
// yang mencipta objek pembalut yang serupa.
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// Kecuali, mereka sebenarnya tak sama sepenuhnya.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // Kod ini tidak akan dilaksanakan, kerana 0 adalah tidak benar.
}

// Walaubagaimanapun, pembalut objek dan jenis terbina yang biasa berkongsi
// prototype, maka sebagai contoh, anda sebenarnya boleh menambah fungsi
// kepada string.
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// Fakta ini selalu digunakan dalam "polyfilling", iaitu melaksanakan fungsi
// baru JavaScript didalam subset JavaScript yang lama, supaya ia boleh
// digunakan di dalam persekitaran yang lama seperti pelayar web yang lama.

// Sebagai contoh, kami menyatakan yang Object.create belum lagi tersedia
// di semua implementasi, tetapi kita masih boleh menggunakannya dengan polyfill:
if (Object.create === undefined){ // jangan ganti jika ia sudah wujud
    Object.create = function(proto){
        // buat satu constructor sementara dengan prototype yang betul
        var Constructor = function(){};
        Constructor.prototype = proto;
        // kemudian gunakannya untuk mencipta objek baru yang diberikan
        // prototype yang betul
        return new Constructor();
    }
}
```
## Bacaan Lanjut

[Mozilla Developer Network][1] menyediakan dokumentasi yang sangat baik untuk
JavaScript kerana ia digunakan di dalam pelayar - pelayar web. Tambahan pula,
ia adalah sebuah wiki, maka, sambil anda belajar lebih banyak lagi, anda boleh
membantu orang lain dengan berkongsi pengetahuan anda.

[A re-introduction to JavaScript][2] oleh MDN meliputi semua konsep yang
diterangkan di sini dengan lebih terperinci. Panduan ini menerangkan bahasa
aturcara JavaScript dengan agak mudah; jika anda mahu belajar lebih lanjut
tentang menggunakan JavaScript didalam laman web, mulakan dengan mempelajari
tentang [Document Object Model][3].

[Learn Javascript by Example and with Challenges][4] adalah variasi panduan ini
dengan cabaran yang tersedia pakai.

[JavaScript Garden][5] pula adalah panduan yang lebih terperinci mengenai
semua bahagian bahasa aturcara ini yang bertentangan dengan naluri atau
kebiasaan.

[JavaScript: The Definitive Guide][6] adalah panduan klasik dan buku rujukan.

Selain daripada penyumbang terus kepada artikel ini, sebahagian kandungannya
adalah adaptasi daripada tutorial Python Louie Dinh di dalam laman web ini,
dan [JS Tutorial][7] di Mozilla Developer Network.


[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript
[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
[3]: https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core
[4]: http://www.learneroo.com/modules/64/nodes/350
[5]: http://bonsaiden.github.io/JavaScript-Garden/
[6]: http://www.amazon.com/gp/product/0596805527/
[7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
---
language: json
filename: learnjson-ms.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
  - ["Michael Neth", "https://github.com/infernocloud"]
translators:
    - ["abdalim", "https://github.com/abdalim"]
lang: ms-my
---

Disebabkan JSON adalah format pertukaran-data yang sangat ringkas, panduan ini
kemungkinan besar adalah Learn X in Y Minutes yang paling mudah.

JSON dalam bentuk paling aslinya sebenarnya tidak mempunyai sebarang komentar,
tetapi kebanyakan pembaca menerima komen dalam bentuk C (`\\`,`/* */`). Beberapa
pembaca juga bertoleransi terhadap koma terakhir (iaitu koma selepas elemen
terakhir di dalam array atau selepas ciri terakhir sesuatu objek), tetapi semua
ini harus dielakkan dan dijauhkan untuk keserasian yang lebih baik.

Untuk tujuan ini bagaimanapun, semua di dalam panduan ini adalah 100% JSON yang
sah. Luckily, it kind of speaks for itself.

Sebuah nilai JSON harus terdiri dari salah satu, iaitu, nombor, string, array,
objek atau salah satu dari nama literal berikut: true, false, null.

Pelayar web yang menyokong adalah: Firefox 3.5+, Internet Explorer 8.0+, Chrome
1.0+, Opera 10.0+, dan Safari 4.0+.

Sambungan fail untuk fail - fail JSON adalah ".json" dan jenis MIME untuk teks
JSON adalah "application/json".

Banyak bahasa aturcara mempunyai fungsi untuk menyirikan (mengekod) dan
menyah-sirikan (men-dekod) data JSON kepada struktur data asal. Javascript
mempunyai sokongon tersirat untuk memanipulasi teks JSON sebagai data.

Maklumat lebih lanjut boleh dijumpai di http://www.json.org/

JSON dibina pada dua struktur:
* Sebuah koleksi pasangan nama/nilai. Di dalam pelbagai bahasa aturcara, ini
direalisasikan sebagai objek, rekod, "struct", "dictionary", "hash table",
senarai berkunci, atau "associative array".
* Sebuah senarai nilai yang tersusun. Dalam kebanyakan bahasa aturcara, ini
direalisasikan sebagai array, vektor, senarai atau urutan.

Sebuah objek dengan pelbagai pasangan nama/nilai.

```json
{
  "kunci": "nilai",

  "kekunci": "harus sentiasa dibalut dengan 'double quotes'",
  "nombor": 0,
  "strings": "Hellø, wørld. Semua unicode dibenarkan, bersama \"escaping\".",
  "ada bools?": true,
  "tiada apa - apa": null,

  "nombor besar": 1.2e+100,

  "objek": {
    "komen": "Sebahagian besar struktur akan terdiri daripada objek.",

    "array": [0, 1, 2, 3, "Array boleh mempunyai sebarang jenis data di dalamnya.", 5],

    "objek lain": {
      "komen": "Objek boleh dibina dengan pelbagai lapisan, sangat berguna."
    }
  },

  "kebendulan": [
    {
      "punca potassium": ["pisang"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "stail alternatif": {
    "komen": "cuba lihat ini!"
  , "posisi koma": "tidak mengapa - selagi ia adalah sebelum nama atau kunci seterusnya, maka ia sah"
  , "komen lain": "sungguh bagus"
  }
}
```

Sebuah array sahaja yang mengandungi nilai - nilai juga adalah JSON yang sah.

```json
[1, 2, 3, "text", true]
```

Objek - objek boleh menjadi sebahagian dari array juga.

```json
[{"nama": "Abe", "umur": 25}, {"nama": "Jemah", "umur": 29}, {"name": "Yob", "umur": 31}]
```
---
language: sass
filename: learnsass-ms.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
translators:
  - ["hack1m", "https://github.com/hack1m"]
lang: ms-my
---

Sass ialah bahasa sambungan CSS yang menambah ciri-ciri seperti pembolehubah, bersarang, mixins dan banyak lagi.
Sass (dan prapemproses lain, seperti [Less](http://lesscss.org/)) membantu pembangun untuk menulis kod mampu diselenggara dan DRY (Don't Repeat Yourself).

Sass mempunyai dua perbezaan pilihan sintaks untuk dipilih. SCSS, yang mana mempunyai sintaks yang sama seperti CSS tetapi dengan ditambah ciri-ciri Sass. Atau Sass (sintaks asal), yang menggunakan indentasi bukannya tanda kurung dakap dan semikolon.
Tutorial ini ditulis menggunakan SCSS.

```scss

//Komen baris tunggal dikeluarkan apabila Sass dikompil ke CSS.

/*Komen multi dikekalkan. */



/*Pembolehubah
==============================*/



/* Anda boleh menyimpan nilai CSS (seperti warna) dalam pembolehubah.
Guna simbol '$' untuk membuat pembolehubah. */

$primary-color: #A3A4FF;
$secondary-color: #51527F;
$body-font: 'Roboto', sans-serif;

/* Anda boleh mengguna pembolehubah diseluruh lembaran gaya anda.
Kini jika anda ingin mengubah warna, anda hanya perlu membuat perubahan sekali.*/

body {
	background-color: $primary-color;
	color: $secondary-color;
	font-family: $body-font;
}

/* Ia akan dikompil kepada: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/* Ini jauh lebih mampu diselenggara daripada perlu menukar warna
setiap yang ada diseluruh lembaran gaya anda. */



/*Mixins
==============================*/



/* Jika anda jumpa yang anda menulis kod yang sama pada lebih dari satu
elemen, anda mungkin ingin menyimpan kod itu di dalam mixin.

Guna arahan '@mixin', tambah dengan nama untuk mixin anda.*/

@mixin center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* Anda boleh guna mixin bersama '@include' dan nama mixin. */

div {
	@include center;
	background-color: $primary-color;
}

/*Ia akan dikompil kepada: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}


/* Anda boleh guna mixins untuk membuat singkatan property. */

@mixin size($width, $height) {
	width: $width;
	height: $height;
}

/*Yang mana anda boleh seru dengan memberi argumen lebar dan tinggi. */

.rectangle {
	@include size(100px, 60px);
}

.square {
	@include size(40px, 40px);
}

/* Ia dikompil kepada: */
.rectangle {
  width: 100px;
  height: 60px;
}

.square {
  width: 40px;
  height: 40px;
}




/*Extend (Inheritance)
==============================*/



/*Extend ialah jalan untuk berkongsi sifat dengan satu pemilih dengan yang lain. */

.display {
	@include size(5em, 5em);
	border: 5px solid $secondary-color;
}

.display-success {
	@extend .display;
	border-color: #22df56;
}

/* Dikompil kepada: */
.display, .display-success {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F;
}

.display-success {
  border-color: #22df56;
}




/*Bersarang
==============================*/



/*Sass membenarkan anda untuk sarangkan pemilih dengan pemilih */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #FF0000;		
	}
}

/* '&' akan digantikan dengan pemilih induk. */
/* Anda juga boleh sarangkan kelas-pseudo. */
/* Perlu diingat terlebih bersarang akan membuat kod anda kurang mampu diselenggara.
Sebagai contoh: */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Dikompil kepada: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}




```



## SASS atau Sass?
Adakah anda tertanya-tanya sama ada Sass adalah akronim atau tidak? Anda mungkin tidak perlu, tetapi saya akan memberitahu. Nama bahasa ini adalah perkataan, "Sass", dan tidak akronim.
Kerana orang sentiasa menulis ia sebagai "Sass", pencipta bahasa bergurau memanggilnya "Syntactically Awesome StyleSheets".

## Berlatih Sass
Jika anda ingin bermain dengan Sass di pelayar anda, lihat [SassMeister](http://sassmeister.com/).
Anda boleh guna salah satu sintaks, hanya pergi ke tetapan dan pilih sama ada Sass atau SCSS.


## Bacaan lanjut
* [Dokumentasi Rasmi](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way](http://thesassway.com/) menyediakan tutorial (asas-lanjutan) dan artikel.
---
language: xml
filename: learnxml-ms.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
    - ["hack1m", "https://github.com/hack1m"]
lang: ms-my   
---

XML adalah bahasa markup direka untuk menyimpan dan mengangkutan data.

Tidak seperti HTML, XML tidak menyatakan bagaimana paparan atau mengformat data, hanya membawanya.

* Sintaks XML

```xml
<!-- Komen di XML seperti ini -->

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

<!-- Di atas adalah fail XML biasa.
  Ia bermula dengan perisytiharan, memaklumkan beberapa metadata (pilihan).

  XML menggunakan struktur pokok, Di atas, nod akar ialah ‘bookstore’, yang mana mempunyai tiga nod anak, semua ‘books’. Nod itu mempunyai lebih nod anak (atau anak-anak), dan seterusnya…

  Nod dibuat menggunakan tag pembuka/penutup, dan anak-anak hanya nod antara
  pembuka dan penutup tag.-->


  <!-- XML membawa dua jenis data:
   1 - Atribut -> Iaitu metadata mengenai nod.
       Biasanya, penghurai XML menggunakan informasi untuk menyimpan data dengan betul.
       Ia mempunyai ciri-ciri yang dipaparkan bersama format name=“value” dalam tag
       pembuka.

   2 - Elemen -> Iaitu data tulen.
       Iaitu apa penghurai akan menerima daripada fail XML.
       Elemen memaparkan diantara pembuka dan penutup tag. —>


<!-- Di bawah, elemen dengan dua atribut -->
<file type="gif" id="4293">computer.gif</file>


```

* Dokumen Format sempurna x Pengesahan

Satu dokumen XML adalah format sempurna jika ia adalah sintaksis yang betul.
Walau bagaimanapun, ia mungkin menyuntik lebih banyak kekangan dalam dokumen itu,
menggunakan definasi dokumen, seperti DTD dan Skema XML.

Satu dokumen XML yang mana mengikut definasi dokumen dipanggil sah,
mengenai dokumen itu.

Dengan alat ini, anda boleh menyemak data XML di luar logik aplikasi.

```xml

<!-- Dibawah, anda boleh melihat versi ringkas daripada dokumen bookstore,
  dengan tambahan definisi DTD. -->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Bookstore.dtd">
<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>

<!-- DTD boleh menjadi sesuatu seperti ini: -->

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>


<!-- DTD bermula dengan pengisytiharan.
  Berikut, nod akar diisytihar, memerlukan 1 atau lebih nod anak ‘book’.
  Setiap ‘book’ harus mengandungi betul-betul satu ‘title’ dan ‘price’ dan atribut
  dipanggil ‘category’, bersama “Literature" sebagai nilai lalai ia.
  Nod ‘title’ dan ‘price’  mengandungi aksara data terhurai.-—>

<!-- DTD boleh diisytiharkan di dalam fail XML itu sendiri. -->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>

<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>
```
---
language: neat
contributors:
    - ["Feep", "https://github.com/FeepingCreature"]
filename: LearnNeat.nt
---

Neat is basically a smaller version of D1 with some experimental syntax and a focus on terseness without losing the basic C-like syntax.

[Read more here.](https://github.com/FeepingCreature/fcc/wiki)

```c
// single line comments start with //
/*
  multiline comments look like this
*/
/+
  or this
  /+ these can be nested too, same as D +/
+/

// Module name. This has to match the filename/directory.
module LearnNeat;

// Make names from another module visible in this one.
import std.file;
// You can import multiple things at once.
import std.math, std.util;
// You can even group up imports!
import std.(process, socket);

// Global functions!
void foo() { }

// Main function, same as in C.
// string[] == "array of strings".
// "string" is just an alias for char[],
void main(string[] args) {
  // Call functions with "function expression".
  writeln "Hello World";
  // You can do it like in C too... if you really want.
  writeln ("Hello World");
  // Declare a variable with "type identifier"
  string arg = ("Hello World");
  writeln arg;
  // (expression, expression) forms a tuple.
  // There are no one-value tuples though.
  // So you can always use () in the mathematical sense.
  // (string) arg; <- is an error

  /*
    byte: 8 bit signed integer
      char: 8 bit UTF-8 byte component.
    short: 16 bit signed integer
    int: 32 bit signed integer
    long: 64 bit signed integer

    float: 32 bit floating point
    double: 64 bit floating point
    real: biggest native size floating point (80 bit on x86).

    bool: true or false
  */
  int a = 5;
  bool b = true;
  // as in C, && and || are short-circuit evaluating.
  b = b && false;
  assert(b == false);
  // "" are "format strings". So $variable will be substituted at runtime
  // with a formatted version of the variable.
  writeln "$a";
  // This will just print $a.
  writeln `$a`;
  // you can format expressions with $()
  writeln "$(2+2)";
  // Note: there is no special syntax for characters.
  char c = "a";
  // Cast values by using type: expression.
  // There are three kinds of casts:
  // casts that just specify conversions that would be happening automatically
  // (implicit casts)
  float f = float:5;
  float f2 = 5; // would also work
  // casts that require throwing away information or complicated computation -
  // those must always be done explicitly
  // (conversion casts)
  int i = int:f;
  // int i = f; // would not work!
  // and, as a last attempt, casts that just reinterpret the raw data.
  // Those only work if the types have the same size.
  string s = "Hello World";
  // Arrays are (length, pointer) pairs.
  // This is a tuple type. Tuple types are (type, type, type).
  // The type of a tuple expression is a tuple type. (duh)
  (int, char*) array = (int, char*): s;
  // You can index arrays and tuples using the expression[index] syntax.
  writeln "pointer is $(array[1]) and length is $(array[0])";
  // You can slice them using the expression[from .. to] syntax.
  // Slicing an array makes another array.
  writeln "$(s[0..5]) World";
  // Alias name = expression gives the expression a name.
  // As opposed to a variable, aliases do not have an address
  // and can not be assigned to. (Unless the expression is assignable)
  alias range = 0 .. 5;
  writeln "$(s[range]) World";
  // You can iterate over ranges.
  for int i <- range {
    write "$(s[i])";
  }
  writeln " World";
  // Note that if "range" had been a variable, it would be 'empty' now!
  // Range variables can only be iterated once.
  // The syntax for iteration is "expression <- iterable".
  // Lots of things are iterable.
  for char c <- "Hello" { write "$c"; }
  writeln " World";
  // For loops are "for test statement";
  alias test = char d <- "Hello";
  for test write "$d";
  writeln " World\t\x05"; // note: escapes work
  // Pointers: function the same as in C, btw. The usual.
  // Do note: the pointer star sticks with the TYPE, not the VARIABLE!
  string* p;
  assert(p == null); // default initializer
  p = &s;
  writeln "$(*p)";
  // Math operators are (almost) standard.
  int x = 2 + 3 * 4 << 5;
  // Note: XOR is "xor". ^ is reserved for exponentiation (once I implement that).
  int y = 3 xor 5;
  int z = 5;
  assert(z++ == 5);
  assert(++z == 7);
  writeln "x $x y $y z $z";
  // As in D, ~ concatenates.
  string hewo = "Hello " ~ "World";
  // == tests for equality, "is" tests for identity.
  assert  (hewo == s);
  assert !(hewo is s);
  // same as
  assert  (hewo !is s);

  // Allocate arrays using "new array length"
  int[] integers = new int[] 10;
  assert(integers.length == 10);
  assert(integers[0] == 0); // zero is default initializer
  integers = integers ~ 5; // This allocates a new array!
  assert(integers.length == 11);

  // This is an appender array.
  // Instead of (length, pointer), it tracks (capacity, length, pointer).
  // When you append to it, it will use the free capacity if it can.
  // If it runs out of space, it reallocates - but it will free the old array automatically.
  // This makes it convenient for building arrays.
  int[auto~] appender;
  appender ~= 2;
  appender ~= 3;
  appender.free(); // same as {mem.free(appender.ptr); appender = null;}

  // Scope variables are automatically freed at the end of the current scope.
  scope int[auto~] someOtherAppender;
  // This is the same as:
  int[auto~] someOtherAppender2;
  onExit { someOtherAppender2.free; }

  // You can do a C for loop too
  // - but why would you want to?
  for (int i = 0; i < 5; ++i) { }
  // Otherwise, for and while are the same.
  while int i <- 0..4 {
    assert(i == 0);
    break; // continue works too
  } then assert(false); // if we hadn't break'd, this would run at the end
  // This is the height of loopdom - the produce-test-consume loop.
  do {
    int i = 5;
  } while (i == 5) {
    assert(i == 5);
    break; // otherwise we'd go back up to do {
  }

  // This is a nested function.
  // Nested functions can access the surrounding function.
  string returnS() { return s; }
  writeln returnS();

  // Take the address of a function using &
  // The type of a global function is ReturnType function(ParameterTypeTuple).
  void function() foop = &foo;

  // Similarly, the type of a nested function is ReturnType delegate(ParameterTypeTuple).
  string delegate() returnSp = &returnS;
  writeln returnSp();
  // Class member functions and struct member functions also fit into delegate variables.
  // In general, delegates are functions that carry an additional context pointer.
  // ("fat pointers" in C)

  // Allocate a "snapshot" with "new delegate".
  // Snapshots are not closures! I used to call them closures too,
  // but then my Haskell-using friends yelled at me so I had to stop.
  // The difference is that snapshots "capture" their surrounding context
  // when "new" is used.
  // This allows things like this
  int delegate(int) add(int a) {
    int add_a(int b) { return a + b; }
    // This does not work - the context of add_a becomes invalid
    // when add returns.
    // return &add_a;
    // Instead:
    return new &add_a;
  }
  int delegate(int) dg = add 2;
  assert (dg(3) == 5);
  // or
  assert (((add 2) 3) == 5);
  // or
  assert (add 2 3 == 5);
  // add can also be written as
  int delegate(int) add2(int a) {
    // this is an implicit, nameless nested function.
    return new λ(int b) { return a + b; }
  }
  // or even
  auto add3(int a) { return new λ(int b) -> a + b; }
  // hahahaaa
  auto add4 = λ(int a) -> new λ(int b) -> a + b;
  assert(add4 2 3 == 5);
  // If your keyboard doesn't have a λ (you poor sod)
  // you can use \ too.
  auto add5 = \(int a) -> new \(int b) -> a + b;
  // Note!
  auto nestfun = λ() { } // There is NO semicolon needed here!
  // "}" can always substitute for "};".
  // This provides syntactic consistency with built-in statements.


  // This is a class.
  // Note: almost all elements of Neat can be used on the module level
  //       or just as well inside a function.
  class C {
    int a;
    void writeA() { writeln "$a"; }
    // It's a nested class - it exists in the context of main().
    // so if you leave main(), any instances of C become invalid.
    void writeS() { writeln "$s"; }
  }
  C cc = new C;
  // cc is a *reference* to C. Classes are always references.
  cc.a = 5; // Always used for property access.
  auto ccp = &cc;
  (*ccp).a = 6;
  // or just
  ccp.a = 7;
  cc.writeA();
  cc.writeS(); // to prove I'm not making things up
  // Interfaces work same as in D, basically. Or Java.
  interface E { void doE(); }
  // Inheritance works same as in D, basically. Or Java.
  class D : C, E {
    override void writeA() { writeln "hahahahaha no"; }
    override void doE() { writeln "eeeee"; }
    // all classes inherit from Object. (toString is defined in Object)
    override string toString() { return "I am a D"; }
  }
  C cd = new D;
  // all methods are always virtual.
  cd.writeA();
  E e = E:cd; // dynamic class cast!
  e.doE();
  writeln "$e"; // all interfaces convert to Object implicitly.

  // Templates!
  // Templates are parameterized namespaces, taking a type as a parameter.
  template Templ(T) {
    alias hi = 5, hii = 8;
    // Templates always have to include something with the same name as the template
    // - this will become the template's _value_.
    // Static ifs are evaluated statically, at compile-time.
    // Because of this, the test has to be a constant expression,
    // or something that can be optimized to a constant.
    static if (types-equal (T, int)) {
      alias Templ = hi;
    } else {
      alias Templ = hii;
    }
  }
  assert(Templ!int == 5);
  assert(Templ!float == 8);
}
```

## Topics Not Covered

 * Extended iterator types and expressions
 * Standard library
 * Conditions (error handling)
 * Macros
---
language: Nim
filename: learnNim.nim
contributors:
    - ["Jason J. Ayala P.", "http://JasonAyala.com"]
    - ["Dennis Felsing", "http://felsin9.de/nnis/"]
---

Nim (formerly Nimrod) is a statically typed, imperative programming language
that gives the programmer power without compromises on runtime efficiency.

Nim is efficient, expressive, and elegant.

```nim
var                     # Declare (and assign) variables,
  letter: char = 'n'    # with or without type annotations
  lang = "N" & "im"
  nLength : int = len(lang)
  boat: float
  truth: bool = false

let            # Use let to declare and bind variables *once*.
  legs = 400   # legs is immutable.
  arms = 2_000 # _ are ignored and are useful for long numbers.
  aboutPi = 3.15

const            # Constants are computed at compile time. This provides
  debug = true   # performance and is useful in compile time expressions.
  compileBadCode = false

when compileBadCode:            # `when` is a compile time `if`
  legs = legs + 1               # This error will never be compiled.
  const input = readline(stdin) # Const values must be known at compile time.

discard 1 > 2 # Note: The compiler will complain if the result of an expression
              # is unused. `discard` bypasses this.

discard """
This can work as a multiline comment.
Or for unparsable, broken code
"""

#
# Data Structures
#

# Tuples

var
  child: tuple[name: string, age: int]   # Tuples have *both* field names
  today: tuple[sun: string, temp: float] # *and* order.

child = (name: "Rudiger", age: 2) # Assign all at once with literal ()
today.sun = "Overcast"            # or individual fields.
today.temp = 70.1

# Sequences

var
  drinks: seq[string]

drinks = @["Water", "Juice", "Chocolate"] # @[V1,..,Vn] is the sequence literal

drinks.add("Milk")

if "Milk" in drinks:
  echo "We have Milk and ", drinks.len - 1, " other drinks"

let myDrink = drinks[2]

#
# Defining Types
#

# Defining your own types puts the compiler to work for you. It's what makes
# static typing powerful and useful.

type
  Name = string # A type alias gives you a new type that is interchangeable
  Age = int     # with the old type but is more descriptive.
  Person = tuple[name: Name, age: Age] # Define data structures too.
  AnotherSyntax = tuple
    fieldOne: string
    secondField: int

var
  john: Person = (name: "John B.", age: 17)
  newage: int = 18 # It would be better to use Age than int

john.age = newage # But still works because int and Age are synonyms

type
  Cash = distinct int    # `distinct` makes a new type incompatible with its
  Desc = distinct string # base type.

var
  money: Cash = 100.Cash # `.Cash` converts the int to our type
  description: Desc  = "Interesting".Desc

when compileBadCode:
  john.age  = money        # Error! age is of type int and money is Cash
  john.name = description  # Compiler says: "No way!"

#
# More Types and Data Structures
#

# Enumerations allow a type to have one of a limited number of values

type
  Color = enum cRed, cBlue, cGreen
  Direction = enum # Alternative formatting
    dNorth
    dWest
    dEast
    dSouth
var
  orient = dNorth # `orient` is of type Direction, with the value `dNorth`
  pixel = cGreen # `pixel` is of type Color, with the value `cGreen`

discard dNorth > dEast # Enums are usually an "ordinal" type

# Subranges specify a limited valid range

type
  DieFaces = range[1..20] # Only an int from 1 to 20 is a valid value
var
  my_roll: DieFaces = 13

when compileBadCode:
  my_roll = 23 # Error!

# Arrays

type
  RollCounter = array[DieFaces, int]  # Array's are fixed length and
  DirNames = array[Direction, string] # indexed by any ordinal type.
  Truths = array[42..44, bool]
var
  counter: RollCounter
  directions: DirNames
  possible: Truths

possible = [false, false, false] # Literal arrays are created with [V1,..,Vn]
possible[42] = true

directions[dNorth] = "Ahh. The Great White North!"
directions[dWest] = "No, don't go there."

my_roll = 13
counter[my_roll] += 1
counter[my_roll] += 1

var anotherArray = ["Default index", "starts at", "0"]

# More data structures are available, including tables, sets, lists, queues,
# and crit bit trees.
# http://nim-lang.org/docs/lib.html#collections-and-algorithms

#
# IO and Control Flow
#

# `case`, `readLine()`

echo "Read any good books lately?"
case readLine(stdin)
of "no", "No":
  echo "Go to your local library."
of "yes", "Yes":
  echo "Carry on, then."
else:
  echo "That's great; I assume."

# `while`, `if`, `continue`, `break`

import strutils as str # http://nim-lang.org/docs/strutils.html
echo "I'm thinking of a number between 41 and 43. Guess which!"
let number: int = 42
var
  raw_guess: string
  guess: int
while guess != number:
  raw_guess = readLine(stdin)
  if raw_guess == "": continue # Skip this iteration
  guess = str.parseInt(raw_guess)
  if guess == 1001:
    echo("AAAAAAGGG!")
    break
  elif guess > number:
    echo("Nope. Too high.")
  elif guess < number:
    echo(guess, " is too low")
  else:
    echo("Yeeeeeehaw!")

#
# Iteration
#

for i, elem in ["Yes", "No", "Maybe so"]: # Or just `for elem in`
  echo(elem, " is at index: ", i)

for k, v in items(@[(person: "You", power: 100), (person: "Me", power: 9000)]):
  echo v

let myString = """
an <example>
`string` to
play with
""" # Multiline raw string

for line in splitLines(myString):
  echo(line)

for i, c in myString:       # Index and letter. Or `for j in` for just letter
  if i mod 2 == 0: continue # Compact `if` form
  elif c == 'X': break
  else: echo(c)

#
# Procedures
#

type Answer = enum aYes, aNo

proc ask(question: string): Answer =
  echo(question, " (y/n)")
  while true:
    case readLine(stdin)
    of "y", "Y", "yes", "Yes":
      return Answer.aYes  # Enums can be qualified
    of "n", "N", "no", "No":
      return Answer.aNo
    else: echo("Please be clear: yes or no")

proc addSugar(amount: int = 2) = # Default amount is 2, returns nothing
  assert(amount > 0 and amount < 9000, "Crazy Sugar")
  for a in 1..amount:
    echo(a, " sugar...")

case ask("Would you like sugar in your tea?")
of aYes:
  addSugar(3)
of aNo:
  echo "Oh do take a little!"
  addSugar()
# No need for an `else` here. Only `yes` and `no` are possible.

#
# FFI
#

# Because Nim compiles to C, FFI is easy:

proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.}

let cmp = strcmp("C?", "Easy!")
```

Additionally, Nim separates itself from its peers with metaprogramming,
performance, and compile-time features.

## Further Reading

* [Home Page](http://nim-lang.org)
* [Download](http://nim-lang.org/download.html)
* [Community](http://nim-lang.org/community.html)
* [FAQ](http://nim-lang.org/question.html)
* [Documentation](http://nim-lang.org/documentation.html)
* [Manual](http://nim-lang.org/docs/manual.html)
* [Standard Library](http://nim-lang.org/docs/lib.html)
* [Rosetta Code](http://rosettacode.org/wiki/Category:Nim)
---
language: nix
filename: learn.nix
contributors:
    - ["Chris Martin", "http://chris-martin.org/"]
---

Nix is a simple functional language developed for the
[Nix package manager](https://nixos.org/nix/) and
[NixOS](https://nixos.org/).

You can evaluate Nix expressions using
[nix-instantiate](https://nixos.org/nix/manual/#sec-nix-instantiate)
or [`nix-repl`](https://github.com/edolstra/nix-repl).

```
with builtins; [

  #  Comments
  #=========================================

  # Inline comments look like this.

  /* Multi-line comments
     look like this. */


  #  Booleans
  #=========================================

  (true && false)               # And
  #=> false

  (true || false)               # Or
  #=> true

  (if 3 < 4 then "a" else "b")  # Conditional
  #=> "a"


  #  Integers
  #=========================================

  # Integers are the only numeric type.

  1 0 42 (-3)       # Some integers

  (4 + 6 + 12 - 2)  # Addition
  #=> 20

  (7 / 2)           # Division
  #=> 3


  #  Strings
  #=========================================

  "Strings literals are in double quotes."

  "
    String literals can span
    multiple lines.
  "

  ''
    This is called an "indented string" literal.
    It intelligently strips leading whitespace.
  ''

  ''
    a
      b
  ''
  #=> "a\n  b"

  ("ab" + "cd")   # String concatenation
  #=> "abcd"

  # Antiquotation lets you embed values into strings.
  ("Your home directory is ${getEnv "HOME"}")
  #=> "Your home directory is /home/alice"


  #  Paths
  #=========================================

  # Nix has a primitive data type for paths.
  /tmp/tutorials/learn.nix

  # A relative path is resolved to an absolute path at parse
  # time, relative to the file in which it occurs.
  tutorials/learn.nix
  #=> /the-base-path/tutorials/learn.nix

  # A path must contain at least one slash, so a relative
  # path for a file in the same directory needs a ./ prefix,
  ./learn.nix
  #=> /the-base-path/learn.nix

  # The / operator must be surrounded by whitespace if
  # you want it to signify division.

  7/2        # This is a path literal
  (7 / 2)    # This is integer division


  #  Imports
  #=========================================

  # A nix file contains a single top-level expression with no free
  # variables. An import expression evaluates to the value of the
  # file that it imports.
  (import /tmp/foo.nix)

  # Imports can also be specified by strings.
  (import "/tmp/foo.nix")

  # Import paths must be absolute. Path literals
  # are automatically resolved, so this is fine.
  (import ./foo.nix)

  # But this does not happen with strings.
  (import "./foo.nix")
  #=> error: string ‘foo.nix’ doesn't represent an absolute path


  #  Let
  #=========================================

  # `let` blocks allow us to bind values to variables.
  (let x = "a"; in
    x + x + x)
  #=> "aaa"

  # Bindings can refer to each other, and their order does not matter.
  (let y = x + "b";
       x = "a"; in
    y + "c")
  #=> "abc"

  # Inner bindings shadow outer bindings.
  (let a = 1; in
    let a = 2; in
      a)
  #=> 2


  #  Functions
  #=========================================

  (n: n + 1)      # Function that adds 1

  ((n: n + 1) 5)  # That same function, applied to 5
  #=> 6

  # There is no syntax for named functions, but they
  # can be bound by `let` blocks like any other value.
  (let succ = (n: n + 1); in succ 5)
  #=> 6

  # A function has exactly one argument.
  # Multiple arguments can be achieved with currying.
  ((x: y: x + "-" + y) "a" "b")
  #=> "a-b"

  # We can also have named function arguments,
  # which we'll get to later after we introduce sets.


  #  Lists
  #=========================================

  # Lists are denoted by square brackets.

  (length [1 2 3 "x"])
  #=> 4

  ([1 2 3] ++ [4 5])
  #=> [1 2 3 4 5]

  (concatLists [[1 2] [3 4] [5]])
  #=> [1 2 3 4 5]

  (head [1 2 3])
  #=> 1
  (tail [1 2 3])
  #=> [2 3]

  (elemAt ["a" "b" "c" "d"] 2)
  #=> "c"

  (elem 2 [1 2 3])
  #=> true
  (elem 5 [1 2 3])
  #=> false

  (filter (n: n < 3) [1 2 3 4])
  #=> [ 1 2 ]


  #  Sets
  #=========================================

  # A "set" is an unordered mapping with string keys.
  { foo = [1 2]; bar = "x"; }

  # The . operator pulls a value out of a set.
  { a = 1; b = 2; }.a
  #=> 1

  # The // operator merges two sets.
  ({ a = 1; } // { b = 2; })
  #=> { a = 1; b = 2; }

  # Values on the right override values on the left.
  ({ a = 1; b = 2; } // { a = 3; c = 4; })
  #=> { a = 3; b = 2; c = 4; }

  # The rec keyword denotes a "recursive set",
  # in which attributes can refer to each other.
  (let a = 1; in     { a = 2; b = a; }.b)
  #=> 1
  (let a = 1; in rec { a = 2; b = a; }.b)
  #=> 2

  # Nested sets can be defined in a piecewise fashion.
  {
    a.b   = 1;
    a.c.d = 2;
    a.c.e = 3;
  }.a.c
  #=> { d = 2; e = 3; }

  # An attribute's descendants cannot be assigned in this
  # way if the attribute itself has been directly assigned.
  {
    a = { b = 1; };
    a.c = 2;
  }
  #=> error: attribute ‘a’ already defined


  #  With
  #=========================================

  # The body of a `with` block is evaluated with
  # a set's mappings bound to variables.
  (with { a = 1; b = 2; };
    a + b)
  # => 3

  # Inner bindings shadow outer bindings.
  (with { a = 1; b = 2; };
    (with { a = 5; };
      a + b))
  #=> 7

  # This first line of tutorial starts with "with builtins;"
  # because builtins is a set the contains all of the built-in
  # functions (length, head, tail, filter, etc.). This saves
  # us from having to write, for example, "builtins.length"
  # instead of just "length".


  #  Set patterns
  #=========================================

  # Sets are useful when we need to pass multiple values
  # to a function.
  (args: args.x + "-" + args.y) { x = "a"; y = "b"; }
  #=> "a-b"

  # This can be written more clearly using set patterns.
  ({x, y}: x + "-" + y) { x = "a"; y = "b"; }
  #=> "a-b"

  # By default, the pattern fails on sets containing extra keys.
  ({x, y}: x + "-" + y) { x = "a"; y = "b"; z = "c"; }
  #=> error: anonymous function called with unexpected argument ‘z’

  # Adding ", ..." allows ignoring extra keys.
  ({x, y, ...}: x + "-" + y) { x = "a"; y = "b"; z = "c"; }
  #=> "a-b"


  #  Errors
  #=========================================

  # `throw` causes evaluation to abort with an error message.
  (2 + (throw "foo"))
  #=> error: foo

  # `tryEval` catches thrown errors.
  (tryEval 42)
  #=> { success = true; value = 42; }
  (tryEval (2 + (throw "foo")))
  #=> { success = false; value = false; }

  # `abort` is like throw, but it's fatal; it cannot be caught.
  (tryEval (abort "foo"))
  #=> error: evaluation aborted with the following error message: ‘foo’

  # `assert` evaluates to the given value if true;
  # otherwise it throws a catchable exception.
  (assert 1 < 2; 42)
  #=> 42
  (assert 1 > 2; 42)
  #=> error: assertion failed at (string):1:1
  (tryEval (assert 1 > 2; 42))
  #=> { success = false; value = false; }


  #  Impurity
  #=========================================

  # Because repeatability of builds is critical to the Nix package
  # manager, in which, functional purity is emphasized in the Nix
  # language. But there are a few impurities.

  # You can refer to environment variables.
  (getEnv "HOME")
  #=> "/home/alice"

  # The trace function is used for debugging. It prints the first
  # argument to stderr and evaluates to the second argument.
  (trace 1 2)
  #=> trace: 1
  #=> 2

  # You can write files into the Nix store. Although impure, this is
  # fairly safe because the file name is derived from the hash of
  # its contents. You can read files from anywhere. In this example,
  # we write a file into the store, and then read it back out.
  (let filename = toFile "foo.txt" "hello!"; in
    [filename (builtins.readFile filename)])
  #=> [ "/nix/store/ayh05aay2anx135prqp0cy34h891247x-foo.txt" "hello!" ]

  # We can also download files into the Nix store.
  (fetchurl "https://example.com/package-1.2.3.tgz")
  #=> "/nix/store/2drvlh8r57f19s9il42zg89rdr33m2rm-package-1.2.3.tgz"

]
```

### Further Reading

* [Nix Manual - Nix expression language]
  (https://nixos.org/nix/manual/#ch-expression-language)

* [James Fisher - Nix by example - Part 1: The Nix expression language]
  (https://medium.com/@MrJamesFisher/nix-by-example-a0063a1a4c55)

* [Susan Potter - Nix Cookbook - Nix By Example]
  (http://funops.co/nix-cookbook/nix-by-example/)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
translators:
    - ["Jeroen Deviaene", "https://www.github.com/jerodev"]
lang: nl-nl
filename: LearnBash-nl.sh
---

Bash is de naam van den unix shell, deze wordt gebruikt voor het GNU operating system en is de standaard shell op Linux en Mac OS X.
Bijna alle voorbeelden hier onder kunnen deel uitmaken van een shell script of kunnen uitgevoerd worden in de shell.

[Lees er meer over hier.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# De eerste lijn in het schript is shebang, deze laat het systeem weten hoe
# het script uitgevoerd moet worden: http://en.wikipedia.org/wiki/Shebang_(Unix)
# Zoals je kan zien wordt # gebruikt om een commentaar lijn te starten.

# Simpel hello world voorbeeld:
echo Hello world!

# Elke command start op een nieuwe lijn, of achter een puntkomma (;):
echo 'Dit is de eerste lijn'; echo 'Dit is de tweede lijn'

# Een varialbe declareren gebeurt op volgende manier:
Variabele="Een string"

# Maar niet op deze manier:
Variabele = "Een string"
# Bash ziet variable als een commando en zal een error geven omdat dit commando
# niet bestaat.

# Of op deze manier:
Variabele= 'Een string'
# Bash zal 'Een string' zien als een commanda en een error geven omdat het niet
# gevonden kan worden.

# Variabelen gebruiken:
echo $Variabele
echo "$Variabele"
echo '$Variabele'
# Wanneer je een variable wil toekennen, exporteren of nog anders gebruik je 
# de naam zonder '$'. Als je de waarde van de variabele wilt, gebruik je een 
# '$' voor de naam.

# Strings vervangen in variables
echo ${Variabele/Een/De}
# Dit zal 'Een' vervangen door 'De' in de string

# Substring
Length=7
echo ${Variabele:0:Length}
# Dit zal de eerste 7 tekens van de string weergeven.

# Standaard waarde voor variabele
echo ${Foo:-"StandaardwaardeAlsFooLeegIsOfNietBestaat"}
# Dit werkt voor null en lege strings (Foo=""). Dit werkt niet voor 0 (Foo=0).
# Merk op dat dit enkel de waarde retourneerd en de variable niet aanpast.


# Ingebouwde variabelen:
# Er zijn enkele zeer handige ingebouwde variabelen, zoals:
echo "Return waarde van laatste programma: $?"
echo "PID van dit script: $$"
echo "Aantal argumenten voor dit script: $#"
echo "Alle argumenten voor dit script: $@"
echo "Argumenten voor dit script in apparte variabelen: $1 $2..."

# Een waarde lezen via input:
echo "Wat is uw naam?"
read Naam # Merk op dat we geen variabele gedeclareerd hebben
echo Hallo, $Naam!

# We hebben ook if structuren
# Gebruik 'man test' voor meer informatie over condities.
if [ $Naam -ne $USER ]
then
    echo "Uw naam is niet gelijk aan de gebruikersnaam"
else
    echo "Uw naam is de gebruikersnaam"
fi

# MERK OP: als $Naam leeg is ziet bash het bovenstaande als volgt:
if [ -ne $USER ]
# dit is ongeldige syntax
# Dus de beter manier om dit te schrijven is
if [ "$Naam" -ne $USER ] ...
# Als naam nu leeg is, ziet bash nu nog steeds
if [ "" -ne $USER ] ...
# Dit werkt wel zoals het hoort

# Er is ook conditionele executie
echo "Altijd uitvoeren" || echo "Enkel uitvoeren als vorige command mislukt"
echo "Altijd uitvoeren" && echo "Enkel uitvoeren als vorige command NIET mislukt"

# Om && en || te gebruiken in if structuren moeten vierkante haken gebruikt worden:
if [ "$Naam" == "Steve" ] && [ "$Leeftijd" -eq 15 ]
then
    echo "Dit wordt uitgevoerd als $Naam Steve is en $Leeftijd 15 is."
fi

# Expressies worden gemaakt met volgende syntax:
echo $(( 10 + 5 ))

# Bash werkt steeds in de context van een huidige map in het bestandssysteem.
# Bestanden en mappen in de huidige map kunnen weergegeven worden met het ls
# commando.
ls

# Commandos hebben opties die de uitvoer beinvloeden
ls -l # Lijst elk bestand en map op een nieuwe lijn.

# Resultaten van een vorig commando kunnen doorgegeven worden aan een volgend 
# commando als input.
# Het grep commando filter de input met een bepaald patroon. Op deze manier kunnen
# we alle .txt bestanden weergeven in de huidige map.
ls -l | grep "\.txt"

# Commando's kunnen gekoppeld worden met andere commando's door gebruik te maken van
# $( ):
# Het volgende commando geeft het aantal bestanden weer in de huidige map
echo "Er zijn hier $(ls | wc -l) bestanden."

# Het zelfde kan gedaan worden met `, maar die kunnen niet genest worden. De methode
# bij voorkeur is om $( ) te gebruiken.
echo "Er zijn hier `ls | wc -l` bestanden."

# Bash heeft een case statement dat werkt zoals in Java en C++
case "$Variabele" in
    0) echo "Er is een 0";;
    1) echo "Er is een 1";;
    *) echo "Er is iets";;
esac

# For lussen itereren over de gegeven argumenten
# De waarde van $Variabele wordt hier drie keer afgeprint
for Variable in {1..3}
do
    echo "$Variabele"
done

# Of schrijf een traditionele for loop op deze manier
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Lussen kunnen ook gebruikt worden met bestanden
# Deze lus voert het cat commando uit op file1 en file2
for Variable in file1 file2
do
    cat "$Variable"
done

# Of met het output commando
for Output in $(ls)
do
    cat "$Output"
done

# while lus:
while [ true ]
do
    echo "body van de lus..."
    break
done

# Je kan ook functies aanmaken
# Defenitie:
function foo ()
{
    echo "Alle argumenten: $@"
    echo "Apparte argumenten: $1 $2..."
    echo "Dit is een functie"
    return 0
}

# Of simpeler
bar ()
{
    echo "Dit is een andere manier om functies te maken."
    return 0
}

# Functies oproepen
foo "Mijn naam is" $Naam

# Enkele zeer handige commando's die je moet kennen
# print de laatste 10 lijnen van file.txt
tail -n 10 file.txt
# print de eerste 10 lijnen van file.txt
head -n 10 file.txt
# Sorteer de lijnen in file.txt
sort file.txt
# Vind dubbele lijnen in file.txt
uniq -d file.txt
# Print de eerste kolom voor het ',' karakter
cut -d ',' -f 1 file.txt
# Vervang elke 'okay' met 'great' in file.txt (werkt ook met regex)
sed -i 's/okay/great/g' file.txt
# Print alle lijnen die voldoen aan de regex naar stdout
grep "^foo.*bar$" file.txt


# Gebruik de ingebouwde help functies door het help commando te gebruiken:
help
help help
help for
help return
help source
help .

# Lees de bash documentatie met het man commando:
apropos bash
man 1 bash
man bash

# Lees bash info documentatie:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: bf
filename: learnbf-nl.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Jelle Besseling", "https://github.com/Jell-E"]
lang: nl-nl
---

Brainfuck (schrijf je niet met een hoofdletter behalve aan het begin van een
zin) is een extreem
minimalistische Turing-complete programmeertaal met maar acht commando's.

```
Elk karakter behalve "><+-.,[]" (en de quotes) wordt genegeerd.

Brainfuck wordt gerepresenteerd door een array met 30.000 cellen die initieel
gevuld is met nullen en een pointer die wijst naar de huidige cel.

Dit zijn de acht commando's:
+ : Verhoog de huidige cell met 1.
- : Verminder de huidige cell met 1.
> : Beweeg de pointer naar de volgende cell (één naar rechts).
< : Beweeg de pointer naar de vorige cell (één naar links).
. : Print de huidige cell als een ASCII karakter(d.w.z. 65 = 'A').
, : Lees een karakter in de huidige cell.
[ : Als de huidige cell nul is ga dan naar de bijbehorende ] .
    Als het geen nul is, ga dan gewoon verder.
] : Als de huidige cell nul is ga dan gewoon verder.
    Als het geen nul is, ga dan terug naar de bijbehorende [ .

[ en ] maken een while loop. Ze moeten uiteraard wel gebalanceerd zijn

Laten we een kijkje nemen naar een paar brainfuck programma's.

++++++ [ > ++++++++++ < - ] > +++++ .

Dit programma print het karakter 'A'. Eerst verhoogt het cell #1 tot 6.
Cell #1 wordt gebruikt om te loopen. Dan begint het de loop ([) en gaat
naar cell #2. Het verhoogt cell #2 tien keer, gaat terug naar cell #1, en
verlaagt cell #1. Deze loop gebeurt zes keer (na zes keer staat cell #1
weer op nul, waarna het doorgaat naar het einde van de loop (]) en
verder gaat).

De pointer staat nu weer op cell #1, deze heeft een waarde van 0, en cell #2
heeft een waarde van 60. > beweegt de pointer naar cell #2, daarna verhoogt
het de cell vijf keer, waardoor het een waarde van 65 bevat, en print dan
de waarde van cell #2. 65 is 'A' in ASCII, dus 'A' wordt geprint in de terminal.


, [ > + < - ] > .

Dit programma leest een karakter van de gebruiker in put en kopieert dat
karakter in cel #1. Dan start de loop. Ga naar cel #2, verhoog de waarde in
cel #2, ga terug naar cel #1, en verklein de waarde in cel #1. Dit gaat door
totdat cel #1 nul is en cel #2 de oude waarde heeft van cell #1. Omdat we
op cel #1 staan verplaatst > de pointer één naar rechts en . print het
karakter in cel #2.

Houd wel in gedachten dat de spaties alleen zijn voor leesbaarheid, je kan het
bovenstaande programma net zo goed schrijven als:

,[>+<-]>.

Probeer maar eens te bedenken wat het volgende programma doet:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Dit programma neemt twee getallen als input, en vermenigvuldigt ze.

In het begin leest het twee karakters in cel #1 en #2. Dan start het de
buitenste loop, met als teller cel #1. Het beweegt naar cel #2, dan start het
de binnenste loop met als teller cel #2, daar verhoogd het cel #3. Maar
dan is er een probleem als cel #2 nul wordt aan het einde van de binnenste loop.
Om dit op te lossen wordt ook cel #4 verhoogd naar het oorspronkelijke getal
uit cel #2 en daarna wordt cel #4 weer gekopieerd naar cell #2.
Het resultaat komt in cel #3 te staan.
```

En dat is dan brainfuck. Niet heel moeilijk, toch? Je kan zelf voor de lol
brainfuck programma's gaan schrijven, of je kan een interpreter schrijven
voor brainfuck in een andere taal. Het is namelijk redelijk makkelijk om te
implementeren aangezien brainfuck maar acht commando's heeft. En als je een
masochist bent kan je ook nog proberen om brainfuck te implementeren… in
brainfuck.
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
  - ["Jelle Besseling", "https://github.com/Jell-E"]
  - ["D.A.W. de Waal", "http://github.com/diodewaal"]
  - ["Sam van Kampen", "http://tehsvk.net"]
filename: coffeescript-nl.coffee
lang: nl-nl
---

CoffeeScript is een kleine programmeertaal die direct compileert naar
JavaScript en er is geen interpretatie tijdens het uitvoeren.
CoffeeScript probeert om leesbare, goed geformatteerde en goed draaiende
JavaScript code te genereren, die in elke JavaScript-runtime werkt, als een
opvolger van JavaScript.

Op [de CoffeeScript-website](http://coffeescript.org/), staat een
volledigere tutorial voor CoffeeScript.

``` coffeescript
# CoffeeScript is een taal voor hipsters.
# Het gaat mee met alle trends van moderne talen.
# Commentaar begint dus met een hekje, net zoals bij Python en Ruby.

###
Blokken commentaar maak je zo, ze vertalen naar JavaScripts */ en /*
in de uitvoer van de CoffeeScript-compiler.

Het is belangrijk dat je ongeveer snapt hoe JavaScript
werkt voordat je verder gaat.
###

# Toewijzing:
getal         = 42 #=> var getal = 42;
tegengestelde = true #=> var tegengestelde = true;

# Voorwaarden:
getal = -42 if tegengestelde #=> if(tegengestelde) { getal = -42; }

# Functies:
kwadraat = (x) -> x * x #=> var kwadraat = function(x) { return x * x; }

vul = (houder, vloeistof = "koffie") ->
  "Nu de #{houder} met #{vloeistof} aan het vullen..."
#=>var vul;
#
#vul = function(houder, vloeistof) {
#  if (vloeistof == null) {
#    vloeistof = "koffie";
#  }
#  return "Nu de " + houder + " met " + vloeistof + " aan het vullen...";
#};

# Reeksen:
lijst = [1..5] #=> var lijst = [1, 2, 3, 4, 5];

# Objecten:
wiskunde =
  wortel:   Math.sqrt
  kwadraat: kwadraat
  derdemacht:   (x) -> x * kwadraat x
#=> var wiskunde = {
#  "wortel": Math.sqrt,
#  "kwadraat": kwadraat,
#  "derdemacht": function(x) { return x * kwadraat(x); }
#}

# "Splats":
wedstrijd = (winnaar, lopers...) ->
  print winnaar, lopers
#=>wedstrijd = function() {
#  var lopers, winnaar;
#  winnaar = arguments[0], lopers = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#  return print(winnaar, lopers);
#};

# Aanwezigheid:
alert "Ik wist het!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# Lijstabstracties:
derdemachten = (wiskunde.derdemacht num for num in lijst)
#=>derdemachten = (function() {
#	var _i, _len, _results;
#	_results = [];
# 	for (_i = 0, _len = lijst.length; _i < _len; _i++) {
#		num = list[_i];
#		_results.push(wiskunde.derdemacht(num));
#	}
#	return _results;
#  })();

etenswaren = ['broccoli', 'spinazie', 'chocolade']
eet eten for eten in etenswaren when eten isnt 'chocolade'
#=>etenswaren = ['broccoli', 'spinazie', 'chocolade'];
#
#for (_k = 0, _len2 = etenswaren.length; _k < _len2; _k++) {
#  eten = etenswaren[_k];
#  if (eten !== 'chocolade') {
#    eet(eten);
#  }
#}
```

## Handige links (in het Engels):

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: json
filename: learnjson-nl.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
translators:
  - ["Niels van Velzen", "https://nielsvanvelzen.me"]
lang: nl-nl
---

Gezien JSON een zeer eenvouding formaat heeft zal dit een van de simpelste
Learn X in Y Minutes ooit zijn.

JSON heeft volgens de specificaties geen commentaar, ondanks dat hebben de
meeste parsers support voor C-stijl (`//`, `/* */`) commentaar.
Sommige parsers staan zelfs trailing komma's toe.
(Een komma na het laatste element in een array of ahter de laatste eigenshap van een object).
Het is wel beter om dit soort dingen te vermijden omdat het niet overal zal werken.

In het voorbeeld zal alleen 100% geldige JSON gebruikt worden.

Data types gesupport door JSON zijn: nummers, strings, booleans, arrays, objecten en null.
Gesupporte browsers zijn: Firefox(Mozilla) 3.5, Internet Explorer 8, Chrome, Opera 10, Safari 4.
De extensie voor JSON bestanden is ".json". De MIME type is "application/json"
Enkele nadelen van JSON zijn het gebrek een type definities en een manier van DTD.

```json
{
  "sleutel": "waarde",

  "sleutels": "zijn altijd in quotes geplaatst",
  "nummers": 0,
  "strings": "Hallø, wereld. Alle unicode karakters zijn toegestaan, samen met \"escaping\".",
  "boolean": true,
  "niks": null,

  "groot nummer": 1.2e+100,

  "objecten": {
    "commentaar": "In JSON gebruik je vooral objecten voor je strutuur",

    "array": [0, 1, 2, 3, "Arrays kunnen alles in zich hebben.", 5],

    "nog een object": {
      "commentaar": "Objecten kunnen genest worden, erg handig."
    }
  },

  "dwaasheid": [
    {
      "bronnen van kalium": ["bananen"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "alternatieve stijl": {
    "commentaar": "Kijk dit!"
  , "De komma positie": "maakt niet uit zolang het er maar is"
  , "nog meer commentaar": "wat leuk"
  },

  "dat was kort": "En nu ben je klaar, dit was alles wat je moet weten over JSON."
}
```
---
language: json
filename: learnjson-nl.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
  - ["Mathieu De Coster", "https://github.com/m-decoster"]
lang: nl-nl
---

Aangezien JSON een extreem eenvoudig datauitwisselingsformaat is, zal dit waarschijnlijk
de meest eenvoudige Learn X in Y Minutes ooit zijn.

Puur JSON heeft geen commentaar, maar de meeste parsers zullen commentaar in de stijl
van C (`//`, `/* */`) aanvaarden. In dit voorbeeld zal alles 100% correcte JSON zijn.
Gelukkig spreekt het meeste voor zichzelf.

```json
{
  "key": "value",

  "keys": "moeten altijd tussen dubbele aanhalingstekens staan",
  "getallen": 0,
  "strings": "Hellø, world. Alle Unicode-karakters zijn toegelaten, zo ook \"escaping\".",
  "heeft json booleans?": true,
  "niets": null,

  "groot getal": 1.2e+100,

  "objecten": {
    "commentaar": "De meeste structuur wordt gemaakt met objecten.",

    "array": [0, 1, 2, 3, "Arrays kunnen eender wat bevatten.", 5],

    "nog een object": {
      "commentaar": "Hoe handig, we kunnen objecten nesten."
    }
  },

  "dwaasheid": [
    {
      "bronnen van kalium": ["bananen"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "alternatieve stijl": {
    "commentaar": "kijk hier eens naar!"
  , "komma locatie": "maakt niet uit - zo lang het voor de value komt, is alles in orde"
  , "nog commentaar": "hoe leuk"
  },

  "dat was kort": "Je bent klaar. Je kent nu alles dat JSON kan aanbieden."
}
```
---
language: markdown
filename: markdown-nl.md
contributors:
  - ["Dan Turkel", "http://danturkel.com/"]
translators:
  - ["Jeroen Deviaene", "https://www.github.com/jerodev"]
lang: nl-nl
---

Markdown is gecreëerd door John Gruber in 2004. Het is bedoeld om met een gemakkelijke te lezen en 
schrijven syntax te zijn die gemakkelijk omgevormd kan worden naar HTML (en op heden verschillende
andere formaten)

```markdown
<!-- Markdown erft over van HTML, dus ieder HTML bestand is een geldig Markdown
bestand. Dit betekend ook dat html elementen gebruikt kunnen worden in Markdown
zoals het commentaar element. Echter, als je een html element maakt in een Markdown 
document kan je geen markdown gebruiken voor de waarde van dat element. -->

<!-- Markdown varieert ook van implementatie tot implementatie en per parser. Deze
tutorial zal de universele functies van Markdown -->

<!-- Headers -->
<!-- Je kan de HTML elementen <h1> tot <h6> gemakkelijk maken door voor de titel
een aantal hashes (#) te plaatsen gelijk aan het level van de header.
# Dit is een <h1>
## Dit is een <h2>
### Dit is een <h3>
#### Dit is een <h4>
##### Dit is een <h5>
###### Dit is een <h6>

<!-- Markdown heeft ook een alternatieve manier om h1 en h2 te maken -->
Dit is een h1
=============

Dit is een h2
-------------

<!-- Simpele tekst stijlen -->
<!-- Tekst kan heel gemakelijk gestyled worden cursief of bold met markdown -->

*Deze tekst is cursief*
_Deze tekst ook_

**Deze tekst is vet gedrukt**
__En deze tekst ook!__

***Deze tekst is zowel bold als schuin gedrukt***
**_Deze ook!_**
*__En zelfs deze!__*

<!-- In de github versie van markdown, die gebruikt wordt om markdown te renderen
op Github, is er ook doorstrepen -->

~~Deze tekst wordt doorstreept.~~

<!-- Paragrafen worden onderscheiden door een of meerdere lege lijnen -->

Dit is een paragraaf. 

Dit is paragraaf 2.
Dit is nog steeds paragraaf 2!


Hallo, ik ben paragraaf 3.

<!-- Citaten zijn gemakkelijk te maken met het '>' karakter. -->

> Dit is een citaat. Je kan alle lijnen manueel starten met een '>'.
> Of je kan de lijn heel heel, heel, lang laten worden zodat de parser deze automatisch zal afbreken en op een nieuwe lijn plaatsen.
> Het maakt niet uit, zolang je start met een '>'.

> Je kan ook in niveaus werken
>> Niveau 2
> Hoe leuk is dat?

<!-- Lijsten -->
<!-- Niet geordende lijsten kunnen gemaakt worden met sterretjes, plussen of streepjes -->

* Item
* Item
* Nog een item

of

+ Item
+ Item
+ Nog een item

of

- Item
- Item
- Een laatste item

<!-- Geordende lijsten kunnen gemaakt wroden met cijfers -->

1. Item een
2. Item twee
3. Item drie

<!-- Het getal moet zelfs niet overeen komen met het item in de lijst, markdown zal
automatisch de nummers aanpassen -->

1. Item een
1. Item twe
1. Item drie
<!-- (De output is gelijk aan de vorige lijst) -->

<!-- Je kan ook gebruik maken van sub lijsten -->

1. Item een
2. Item twee
3. Item drie
    * Sub-item
    * Sub-item
4. Item vier

<!-- Er zijn zelfs todo lijsten. Dit genereert HTML checkboxen. -->

Boxen zonder een 'x' zijn niet aangevinkt
- [ ] Eerste to-do item.
- [ ] Tweede to-do item
Dit item zal aangevinkt zijn in de gerenderde html.
- [x] Deze taak is uitgevoerd

<!-- Code blokken -->
<!-- Een code block kan aangeduid worden door vier spaties of een tab -->

    Dit is code
    En dit ook

<!-- Extra tabs kunnen gebruikt worden om tabs in de code aan te geven -->

    my_array.each do |item|
        puts item
    end

<!-- Inline code kan aangeduid worden met ` -->

John wist zelfs niet dat de `go_to()` functie bestond!

<!-- In Github Markdown kan je een speciale syntax gebruiken die aangeeft welke
taal gebruikt wordt in het code blok. -->

\`\`\`ruby <!-- Wis de backslashes om dit te doen, juist ```ruby ! -->
def foobar
    puts "Hello world!"
end
\`\`\` <!-- Hier ook, geen backslashes, juist ``` -->

<!-- Voor bovenstaande tekst moet geen tab gebruikt worden. Plus, Github zal syntax
highlighting gebruiken voor deze specifieke taal. Hier, Ruby.

<!-- Horizontale lijn (<hr />) -->
<!-- Horizontale lijnen kunnen gemakkelijk toegevoegd worden door drie of meer
sterretjes, of streepjes te plaatsen -->

***
---
- - -
****************

<!-- Links -->
<!-- Een van de beste dingen in Markdown is hoe simpel het is om links te maken.
plaats de tekst om weer te geven tussen [ en ] gevolgd door de link tussen ( en ) -->

[Klik mij!](http://test.com/)

<!-- Een titel kan ook toegevoegd worden aan de link met aanhalingstekens -->

[Klik mij!](http://test.com/ "Titel voor de link")

<!-- Relative paden werken ook -->

[Naar de muziek](/music/).

<!-- Links kunnen ook gelegd worden met referenties -->

[Klik deze link][link1] voor meer info!
[Beijk ook dit][foobar] als je echt wil.

[link1]: http://test.com/ "Cool!"
[foobar]: http://foobar.biz/ "Tof!"


<!-- Afbeeldingen -->
<!-- Afbeeldingen worden toegevoegd op exact de zelfde manier als links maar met een 
uitroepteken aan het begin van de lijn. -->

![Dit is de alt waarde van een afbeelding](http://imgur.com/myimage.jpg "Optionele titel")

<!-- Referenties werkt ook zals bij links -->

![Dit is de alt waarde][myimage]

[myimage]: relative/urls/cool/image.jpg "als een titel nodig is, staat deze hier"

<!-- Varia -->
<!-- Auto-links -->

<http://testwebsite.com/> is gelijk aan
[http://testwebsite.com/](http://testwebsite.com/)

<!-- Auto-links for emails -->

<foo@bar.com>

<!-- Karakters escapen -->

Als je sterretjes wil gebruiken in je tekst zoals *dit* dan zal dit schuingedrukt weergegeven
worden.
Dit kan je oplossen met backslashes: \*dit\* staat tussen sterretjes

<!-- Toetsenbord toetsen -->
<!-- In Github Markdown, kan je <kbd> gebruiken om toetsenbord toetsen weer te geven -->

Loopt je computer vast? Probeer volgende toetsen combinatie:
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>

<!-- Tabellen -->
<!-- Tabellen zijn momenteel enkel beschikbaar in Github Markdown en zijn redelijk omslachtig.
Maar als je er echt wilt toevoegen: -->

| Col1             | Col2        | Col3              |
| :--------------- | :---------: | ----------------: |
| Links uitgelijnt | Gecentreerd | Rechts uitgelijnt |
| blah             | blah        | blah              |

<!-- of, Voor het zelfde resultaat -->

Col 1 | Col2 | Col3
:-- | :-: | --:
Zeer | Lelijke | Code!

<!-- The end! -->

```

Voor meer info, bekijk de officiële post van John Gruber [hier](http://daringfireball.net/projects/markdown/syntax) en de handige cheatsheet van Adam Pritchard [hier](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: TypeScript
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
filename: learntypescript-nl.ts
translators:
  - ["Niels van Velzen", "https://nielsvanvelzen.me"]
lang: nl-nl
---

TypeScript is een taal gericht op het versoepelen van de ontwikkeling van
grote applicaties gemaakt in JavaScript.
TypeScript voegt veelgebruikte technieken zoals klassen, modules, interfaces,
generieken en statische typen toe aan JavaScript.
TypeScript is een superset van JavaScript: alle JavaScript code is geldige
TypeScript code waardoor de overgang van JavaScript naar TypeScript wordt versoepeld.

Dit artikel focust zich alleen op de extra's van TypeScript tegenover [JavaScript] (../javascript-nl/).

Om de compiler van TypeScript te kunnen proberen kun je naar de [Playground] (http://www.typescriptlang.org/Playground) gaan.
Hier kun je automatisch aangevulde code typen in TypeScript en de JavaScript variant bekijken.

```js
// Er zijn 3 basis typen in TypeScript
var isKlaar: boolean = false;
var lijnen: number = 42;
var naam: string = "Peter";

// Wanneer het type onbekend is gebruik je "Any"
var nietZeker: any = 4;
nietZeker = "misschien een string";
nietZeker = false; // Toch een boolean

// Voor collecties zijn er "typed arrays"
var lijst: number[] = [1, 2, 3];
// of generieke arrays
var lijst: Array<number> = [1, 2, 3];

// Voor enumeraties:
enum Kleur {Rood, Groen, Blauw};
var c: Kleur = Kleur.Groen;

// Als laatst, "void" wordt gebruikt voor als een functie geen resultaat geeft
function groteVerschrikkelijkeMelding(): void {
  alert("Ik ben een vervelende melding!");
}

// Functies zijn eersteklas ?, supporten de lambda "fat arrow" syntax en
// gebruiken gebruiken "type inference"

// Het volgende is allemaal hetzelfde
var f1 = function(i: number): number { return i * i; }
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
var f4 = (i: number) => { return i * i; }
// Omdat we maar 1 lijn gebruiken hoeft het return keyword niet gebruikt te worden
var f5 = (i: number) =>  i * i;

// Interfaces zijn structureel, elk object wat de eigenschappen heeft
// is een gebruiker van de interface
interface Persoon {
  naam: string;
  // Optionele eigenschappen worden gemarkeerd met "?"
  leeftijd?: number;
  // En natuurlijk functies
  verplaats(): void;
}

// Object die gebruikt maakt van de "Persoon" interface
// Kan gezien worden als persoon sinds het de naam en verplaats eigenschappen bevat
var p: Persoon = { naam: "Bobby", verplaats: () => {} };
// Object met de optionele leeftijd eigenschap
var geldigPersoon: Persoon = { naam: "Bobby", leeftijd: 42, verplaats: () => {} };
// Ongeldig persoon vanwege de leeftijds type
var ongeldigPersoon: Persoon = { naam: "Bobby", leeftijd: true };

// Interfaces kunnen ook een functie ype beschrijven
interface ZoekFunc {
  (bron: string, subString: string): boolean;
}
// Alleen de parameters types zijn belangrijk, namen maken niet uit.
var mySearch: ZoekFunc;
mySearch = function(src: string, sub: string) {
  return src.search(sub) != -1;
}

// Classes - leden zijn standaard publiek
class Punt {
    // Eigenschappen
    x: number;

    // Constructor - de publieke / prive trefwoorden in deze context zullen
    // eigenschappen in de klasse kunnen aanmaken zonder ze te defineren.
    // In dit voorbeeld zal "y" net als "x" gedefineerd worden met minder code.
    // Standaard waardes zijn ook gesupport

    constructor(x: number, public y: number = 0) {
        this.x = x;
    }

    // Functies
    dist(): number { return Math.sqrt(this.x * this.x + this.y * this.y); }

    // Statische leden
    static origin = new Punt(0, 0);
}

var p1 = new Punt(10 ,20);
var p2 = new Punt(25); // y zal de waarde 0 krijgen

// Overnemen
class Punt3D extends Punt {
    constructor(x: number, y: number, public z: number = 0) {
        super(x, y); // Constructor van ouder aanroepen (Punt)
    }

    // Overschrijven
    dist(): number {
        var d = super.dist();
        return Math.sqrt(d * d + this.z * this.z);
    }
}

// Modules werken ongeveer hetzelfde als namespaces
// met "." kan je submodules defineren
module Geometrie {
  export class Vierkant {
    constructor(public zijLengte: number = 0) {
    }

    oppervlakte() {
      return Math.pow(this.zijLengte, 2);
    }
  }
}

var s1 = new Geometrie.Vierkant(5);

// Local alias for referencing a module
import G = Geometrie;

var s2 = new G.Vierkant(10);

// Generieken
// Classes
class Tupel<T1, T2> {
    constructor(public item1: T1, public item2: T2) {
    }
}

// Interfaces
interface Paar<T> {
    item1: T;
    item2: T;
}

// En functies
var paarNaarTupel = function<T>(p: Paar<T>) {
    return new Tupel(p.item1, p.item2);
};

var tupel = paarNaarTupel({ item1: "hallo", item2: "wereld" });

// Refferentie naar een definitie bestand:
/// <reference path="jquery.d.ts" />

```

## Verder lezen (engels)
 * [TypeScript Official website] (http://www.typescriptlang.org/)
 * [TypeScript language specifications (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [Source Code on GitHub] (https://github.com/Microsoft/TypeScript)
 * [Definitely Typed - repository for type definitions] (http://definitelytyped.org/)
---
language: xml
filename: learnxml-nl.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Frank van Gemeren", "https://github.com/frvge"]
lang: nl-nl
---

XML is een markuptaal die ontwikkeld is om data in te bewaren en data mee te 
verzenden.

Anders dan HTML specificeert XML niet hoe data getoond of geformatteerd moet worden. 
Het bevat de data slechts.

* XML Syntax

```xml
<!-- Dit is commentaar in XML -->

<?xml version="1.0" encoding="UTF-8"?>
<boekenwinkel>
  <boek categorie="KOKEN">
    <title taal="nl">Alledaags Italiaans</titel>
    <auteur>Giada De Laurentiis</auteur>
    <jaar>2005</jaar>
    <prijs>30.00</prijs>
  </boek>
  <boek categorie="KINDEREN">
    <titel taal="nl">Harry Potter</titel>
    <auteur>J K. Rowling</auteur>
    <jaar>2005</jaar>
    <prijs>29.99</prijs>
  </boek>
  <boek categorie="WEB">
    <titel taal="en">Learning XML</titel>
    <auteur>Erik T. Ray</auteur>
    <jaar>2003</jaar>
    <prijs>39.95</prijs>
  </boek>
</boekenwinkel>

<!-- Hierboven staat een standaard XML bestand.
  Het begint met een declaratie die optionele metadata bevat.

  XML werkt met een boomstructuur. De stamknoop hierboven is 'boekenwinkel'. 
  Deze heeft drie kinderen die allemaal 'boek' zijn. Deze knopen hebben op 
  hun beurt weer kinderen, enzovoort...

  Knopen hebben open- en sluittags. Kinderen zijn knopen die zich tussen de 
  open- en sluittags van hun ouders bevinden. -->

<!-- XML bevat two soorten data:
  1 - Attributen -> Dit is metadata van een knoop.
      Deze informatie wordt meestal door de XML parser gebruikt om de data op
      de juiste manier op te slaan. Je herkent het door de syntax in de vorm 
      van naam="waarde" in de open tag.
  2 - Elementen -> Dit is de pure data
      Deze gegevens worden door de parser uit het XML bestand gehaald.
      Elementen staan tussen de open- en sluittags. -->


<!-- Hieronder staat een element met twee attributen -->
<bestand type="gif" id="4293">computer.gif</bestand>


```

* Grammaticaal correcte documenten x Validatie

Een XML document is "grammaticaal correct" of "well-formatted" als de 
syntax correct is. Het is ook mogelijk om meer structuur in het document 
aan te brengen met document definities zoals DTD en XML Schema.

Een XML document dat aan een document definitie voldoet wordt "valide" volgens 
die document definitie genoemd.

Met deze gereedschappen kan je de XML data buiten je applicatie logica 
controleren.

```xml

<!-- Hieronder staat een versimpelde versie voor een boekenwinkel document,
  met een toevoeging van een DTD definitie. -->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "boekenwinkel.dtd">
<boekenwinkel>
  <boek categorie="KOKEN">
    <titel>Alledaags Italiaans</titel>
    <prijs>30.00</prijs>
  </boek>
</boekenwinkel>

<!-- De DTD kan er als volgt uitzien:-->

<!DOCTYPE note
[
<!ELEMENT boekenwinkel (boek+)>
<!ELEMENT boek (titel,prijs)>
<!ATTLIST boek categorie CDATA "Literatuur">
<!ELEMENT titel (#PCDATA)>
<!ELEMENT prijs (#PCDATA)>
]>


<!-- De DTD begint met een declaratie.
  Hierna volgt de declaratie van de stamknoop, die 1 of meer 'boek' kinderen 
  moet bevatten. 
  Elk 'boek' moet precies 1 'titel' en 'prijs' element bevatten en een attribuut 
  'categorie' hebben waarvan 'Literatuur' de standaard waarde is.
  De 'titel' en 'prijs' knopen bevatten parsed character data.-->

<!-- De DTD kan ook in het XML bestand zelf gedeclareerd worden.-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT boekenwinkel (boek+)>
<!ELEMENT boek (titel,prijs)>
<!ATTLIST boek categorie CDATA "Literatuur">
<!ELEMENT titel (#PCDATA)>
<!ELEMENT prijs (#PCDATA)>
]>

<boekenwinkel>
  <boek categorie="KOKEN">
    <titel>Alledaags Italiaans</titel>
    <prijs>30.00</prijs>
  </boek>
</boekenwinkel>
```
---
language: yaml
filename: learnyaml-nl.yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
translators:
  - ["Niels van Velzen", "https://nielsvanvelzen.me"]
  - ["Sam van Kampen", "http://tehsvk.net"]
lang: nl-nl
---

YAML is een dataserialisatietaal ontworpen om snel te kunnen worden begrepen door mensen.

Het is een strikte superset van JSON en bevat nieuwe regels en een strikte manier van inspringen die lijkt op de manier van Python. In tegenstelling tot Python kan je alleen geen tabtekens gebruiken.

```yaml
# Commentaar in YAML ziet er zo uit

##################
# SCALAIRE TYPES #
##################

# Ons hoofdobject (Wat in het hele document gebruikt wordt) is een map;
# dit staat gelijk aan een dictionary, hash of object in andere talen.
sleutel: waarde
nog_een_sleutel: Een andere waarde
nummer_waarde: 100
wetenschappelijke_waarde: 1e+12
boolean_waarde: true
null_waarde: null
sleutel met spaties: waarde
# Merk op dat strings niet verplicht in quotes moeten, maar dit kan wel.
quote_waarde: "Een string in quotes"
"Ook sleutels kunnen in quotes": "Dit is bijvoorbeeld handig als je een dubbelepunt wilt gebruiken in je key"

# Tekst over meerdere lijnen kan je schrijven als een 'letterlijk blok' (met |)
# Of een 'gevouwen blok' (met >)
letterlijk_blok: |
    Dit hele blok met tekst is de waarde van de 'letterlijk_blok'-sleutel,
    met nieuwe lijnen behouden.

    Het blok blijft door gaan tot het geeindigd wordt door korter in te springen.

        Lijnen die groter zijn ingesprongen behouden dit.
gevouwen_stijl: >
    Dit blok met tekst zal de waarde zijn van 'gevouwen_stijl',
    maar deze keer zullen alle nieuwe lijnen worden vervangen met een spatie.

    Lege lijnen, zoals hierboven, zullen worden vertaald naar een nieuwe lijn.

        Meer ingesprongen lijnen zullen hun nieuwe lijnen ook behouden,
        deze tekst zal over 2 lijnen te zien zijn.

##################
# COLLECTIETYPES #
##################

# Nesten wordt bereikt met inspringen.
geneste_map:
    sleutel: waarde
    andere_sleutel: andere waarde
    andere_geneste_map:
        hallo: wereld

# In een map hoeft de sleutel geen string te zijn.
0.25: een float als sleutel

# Sleutels kunnen ook meerdere lijnen gebruiken met behulp van het vraagteken
? |
    Dit is een sleutel
    met meerdere lijnen
: en dit is de waarde

# YAML staat ook collectietypes toe in sleutels, maar veel programmeertalen
# zullen hierover klagen.

# Sequences (gelijk aan lijsten of arrays) zien er zo uit:
een_sequence:
    - Item 1
    - Item 2
    - 0.5 # sequences kunnen meerdere typen waardes bevatten.
    - Item 4
    - sleutel: waarde
      andere_sleutel: andere waarde
    -
        - Dit is een sequence
        - in een andere sequence

# Doordat YAML een superset van JSON is kan je ook mappen en
# sequences volgens de JSON-stijl maken:
json_map: {"sleutel": "waarde"}
json_seq: [3, 2, 1, "takeoff"]

#######################
# EXTRA YAML-FUNCTIES #
#######################

# YAML heeft ook een handige functie genaamd 'anchors' (ankers), deze laten je
# makkelijk de waarde van ergens anders in je document kopieëren. Beide sleutels
# krijgen dezelfde waarde:
geankert_content: &anker_naam Deze string zal verschijnen als waarde voor de twee sleutels
andere_anker: *anker_naam

# YAML heeft ook tags, deze gebruik je om een expliciet type te verklaren
expliciete_string: !!str 0.5
# Sommige parsers gebruiken taalspecifieke tags, zoals deze voor Python's
# complexe nummertype:
python_complex_nummer: !!python/complex 1+2j

#######################
# EXTRA TYPES IN YAML #
#######################

# Strings en nummers zijn niet de enige types die YAML begrijpt.
# ISO opgemaakte datum- en datumtijdnotaties werken ook:
datumtijd: 2001-12-15T02:59:43.1Z
datumtijd_met_spaties: 2001-12-14 21:59:43.10 -5
datum: 2002-12-14

# De !!binary tag geeft aan dat de string een base64-gecodeerde
# binary blob is.
gif_bestand: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML heeft ook een settype, dat ziet er zo uit:
set:
    ? item1
    ? item2
    ? item3

# Zoals in Python zijn sets gewoon mappen met nulwaarden;
# bovenstaand is gelijk aan:
set2:
    item1: null
    item2: null
    item3: null
```
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
filename: LearnBash-no.sh
translators:
    - ["Andreas Lindahl Flåten", "https://github.com/anlif"]
lang: no-nb
---
Bash er navnet på unix skallet, som også var distribuert som skallet for GNU 
operativsystemet og som standard skall på de fleste Linux distribusjoner og 
Mac OS X.

[Les mer her.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# Den første linjen i et bash skript starter med '#!' (shebang) 
# etterfulgt av stien til bash http://en.wikipedia.org/wiki/Shebang_(Unix)
# Kommentarer starter med #.

# Enkelt hello world eksempel:
echo Hello world!

# Hver kommando starter på en ny linje, eller etter et semikolon:
echo 'Dette er den første linjen'; echo 'Dette er en andre linjen'

# Deklarering av en variabel ser slik ut:
VARIABLE="En tekststreng"

# Men ikke slik:
VARIABLE = "En tekststreng"
# Bash vil tolke dette som at VARIABLE er en kommando den skal kjøre 
# og gi en feilmelding dersom kommandoen ikke finnes

# Bruk av den nydeklarerte variabelen:
echo $VARIABLE
echo "$VARIABLE"
echo '$VARIABLE'
# Når du bruker variabelen, for eksempel setter verdien eller eksporterer den, 
# skriver du navnet dens uten $. Hvis du vil bruke variabelens verdi, 
# skriver du $ før variabelnavnet.

# Strenginnhold i en variabel kan erstattes på følgende måte:
echo ${VARIABLE/tull/ball}
# Dette vil erstatte første forekomst av 'tull' med 'ball'

# Substreng av en variabel:
echo ${VARIABLE:0:7}
# Dette vil returnere de første 7 tegnene i en strengvariabel

# Å angi en standardverdi dersom en variabel er udeklarert gjøres slik:
echo ${FOO:-"StandardVerdiDersomFOOErTom"}
# Dette fungerer for null (FOO=), tom streng (FOO="") og tallet null (FOO=0)

# Det finnes en rekke hendige innebygde variable, eksempel: 
echo "Siste programs returnerte verdi: $?"
echo "Skript's PID: $$"
echo "Antall argumenter: $#"
echo "Alle argumenter til skriptet: $@"
echo "Argumenter til skriptet i egne variable: $1 $2..."

# Lesing av input:
echo "Hva heter du?"
read NAME # variabelen NAME blir automatisk deklarert av 'read' kommandoen
echo Hei, $NAME!

# if setninger ser slik ut:
# se 'man test' for mer informasjon om betingelser
if [ $NAME -ne $USER ]
then
    echo "Your name isn't your username"
else
    echo "Your name is your username"
fi

# Det finnes også betinget eksekvering
echo "Kjøres alltid" || echo "Kjøres kun dersom første kommando feilet"
echo "Kjøres alltid" && echo "Kjøres kun dersom første kommando IKKE feilet"

# For å bruke && (logisk OG) og || (logisk ELLER) sammen med if setninger, 
# trenger man par av firkantklammer [] på hver side av et logisk uttrykk:
if [ $NAME == "Steve" ] && [ $AGE -eq 15 ]
then
    echo "Dette kjører dersom $NAME er Steve OG $AGE er lik 15."
fi

if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ]
then
    echo "Dette kjører dersom $NAME er Daniya ELLER Zach."
fi

# Matematiske uttrykk skrives slik:
echo $(( 10 + 5 ))

# Ulikt de fleste programmeringsspråk, så er bash et skall - det medfører at en 
# kommando i et skript kjører i en bestemt mappe i filsystemet. Du kan skrive
# ut innholdet i nåværende mappe med ls kommandoen:
ls

# Kommandoen har parametre som kontrollerer hvordan kommandoen utføres:
ls -l # Skriv hver fil og mappe på sin egen linje

# Resultatet av forrige kommando kan bli sendt til neste kommando som input.
# grep kommandoen filtrerer input ved hjelp av et regulært uttrykk.
# Ved å bruke grep kan vi skrive ut kun .txt filer på følgende måte:
ls -l | grep "\.txt" # lær mer om grep ved å skrive 'man grep'

# Input og output fra filer kan dirigeres (stdin, stdout og stderr).
# 'cat' kommandoen uten argumenter skriver fra stdin til stdout.
# I det følgende eksempelet overskrives filen hello.py med linjene mellom EOF.
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Kjør hello.py (et python skript) 
# med ulike stdin, stdout, and stderr omdirigeringer:
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# '>' operatoren overskriver filen dersom den finnes.
# Hvis du heller vil legge til på slutten av en eksisterende fil, bruk '>>'
python hello.py >> "output.out" 2>> "error.err"

# Overskriv output.txt, legg til error.err, og tell antall linjer med 'wc':
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# Run a command and print its file descriptor (e.g. /dev/fd/123)
# Kjør en kommando og print tilhørende 'file descriptor'
# se 'man fd'
echo <(echo "#helloworld")

# Ulike måter å overskrive output.out med '#helloworld':
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# Slett noen filer med økt verbositet '-v', legg til '-i' for interaktiv modus
rm -v output.out error.err output-and-error.log

# Kommandoer kan kjøres i deklarasjonen av andre kommandoer ved å bruke $( ):
# Følgende kommando skriver antall filer og mapper i nåværende mappe
echo "There are $(ls | wc -l) items here."

# Det samme kan gjøres med backticks `` men de kan ikke være nøstede,
# det anbefales å bruke $( ) slik som i forrige eksempel.
echo "There are `ls | wc -l` items here."

# Bash har en 'case' setning som fungerer omtrent som en 'switch' i Java/C:
case "$VARIABLE" in 
    # Skriv ønskede match med tilhørende kommandoer
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# for løkker kan iterere over en mengde argumenter:
for VARIABLE in {1..3}
do
    echo "$VARIABLE"
done

# Eller vi kan skrive en for løkke omtrent slik det kan gjøres i Java/C:
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Man kan også iterere over resultatet av en annen kommando.
for OUTPUT in $(ls)
do
    cat "$OUTPUT"
done

# while løkke, se if setninger:
while [ true ]
do
    echo "loop body here..."
    break
done

# Man kan også definere funksjoner.
# Definisjon:
function foo ()
{
    echo "Argumenter fungerer akkurat som skript argumenter: $@"
    echo "Og: $1 $2..."
    echo "Dette er en funksjon"
    return 0
}

# eller bare:
bar ()
{
    echo "En annen måte å deklarere en funksjon."
    return 0
}

# Å kalle en funksjon:
foo "Mitt navn er" $NAME

# Det er mange nyttige kommandoer du bør lære deg:
# "tail" skriver ut slutten av en fil, i dette tilfellet de siste 10 linjene
tail -n 10 file.txt
# skriv ut de første 10 linjene av file.txt
head -n 10 file.txt
# sorter linjene i file.txt ("man sort")
sort file.txt
# skriv ut eller fjern repeterte linjer, med -d skrives de ut
uniq -d file.txt
# skriver kun den første kolonnen før ',' tegnet
cut -d ',' -f 1 file.txt
# erstatter hvert tilfelle av 'bjarne' med 'alfa' i file.txt,
# sed støtter regulære uttrykk ("man sed").
sed -i 's/bjarne/alfa/g' file.txt
# skriv til stdout alle linjer i file.txt som matches av et regulært uttrykk
# eksempelet skriver ut alle linjer som begynner med "foo" og slutter med "bar"
grep "^foo.*bar$" file.txt
# skriv "-c" hvis du heller vil vite antall linjer som matcher
grep -c "^foo.*bar$" file.txt
# hvis du vil matche en bestemt streng, og ikke et regulært uttrykk
# bruker du enten "fgrep" eller ekvivalenten "grep -f"
fgrep "^foo.*bar$" file.txt 


# Les Bash sin egen dokumentasjon om innebygde konstruksjoner:
help
help help
help for
help return
help source
help .

# Les Bash sin "manpage":
apropos bash
man 1 bash
man bash

# Les "info" dokumentasjon:
apropos info | grep '^info.*('
man info
info info
info 5 info

# Les bash sin info dokumentasjon:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
--- 
language: json
filename: learnjson-no.json
lang: no-nb
contributors:
  - ["Ole Mathias Heggem", "https://github.com/msbone"]
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
---

JSON er en enkel tekstbasert standard for datautveksling.
Den er opprinnelig avledet fra JavaScript for å representere enkle datastrukturer.
Standarden er imidlertid uavhengig av JavaScript eller andre programmeringsspråk.

JSON i sin reneste form har ingen faktiske kommentarer, men de fleste parsere vil akseptere
C-stil (`//`, `/* */`) kommentarer.

```json
{
  "nøkkel": "verdi",
  
  "nøkler": "må alltid være i doble anførselstegn",
  "tall": 0,
  "strings": "Hellø, wørld. Alt unicode er godkjent, også \"escaping\".",
  "har bools?": true,
  "ingenting": null,

  "stort tall": 1.2e+100,

  "objekt": {
    "kommentar": "Meste av strukturen kommer ifra objekt.",

    "array": [0, 1, 2, 3, "Arrays kan inneholde alt.", 5],

    "nytt object": {
      "comment": "Ny kommentar"
    }
  },

  "tull": [
    {
      "Kilde til Kalium": ["bananer"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "Alternativ": {
    "Kommentar": "Sjekk ut ditta!"
  , "plassering av komma": "Sålenge den er før verdien er det gyldig"
  , "Enda en kommentar": "TØFT!"
  },

  "Ferdig": "Da er den korte innledninga til JSON ferdig"
}
```
---
language: Objective-C
contributors:
    - ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
    - ["Yannick Loriot", "https://github.com/YannickL"]
    - ["Levi Bostian", "https://github.com/levibostian"]
    - ["Clayton Walker", "https://github.com/cwalk"]
    - ["Fernando Valverde", "http://visualcosita.xyz"]
filename: LearnObjectiveC.m
---

Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch.
It is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.

```objective-c
// Single-line comments start with //

/*
Multi-line comments look like this
*/

// XCode supports pragma mark directive that improve jump bar readability
#pragma mark Navigation Functions // New tag on jump bar named 'Navigation Functions'
#pragma mark - Navigation Functions // Same tag, now with a separator

// Imports the Foundation headers with #import
// Use <> to import global files (in general frameworks)
// Use "" to import local files (from project)
#import <Foundation/Foundation.h>
#import "MyClass.h"

// If you enable modules for iOS >= 7.0 or OS X >= 10.9 projects in
// Xcode 5 you can import frameworks like that:
@import Foundation;

// Your program's entry point is a function called
// main with an integer return type
int main (int argc, const char * argv[])
{
    // Create an autorelease pool to manage the memory into the program
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // If using automatic reference counting (ARC), use @autoreleasepool instead:
    @autoreleasepool {

    // Use NSLog to print lines to the console
    NSLog(@"Hello World!"); // Print the string "Hello World!"

    ///////////////////////////////////////
    // Types & Variables
    ///////////////////////////////////////

    // Primitive declarations
    int myPrimitive1  = 1;
    long myPrimitive2 = 234554664565;

    // Object declarations
    // Put the * in front of the variable names for strongly-typed object declarations
    MyClass *myObject1 = nil;  // Strong typing
    id       myObject2 = nil;  // Weak typing
    // %@ is an object
    // 'description' is a convention to display the value of the Objects
    NSLog(@"%@ and %@", myObject1, [myObject2 description]); // prints => "(null) and (null)"

    // String
    NSString *worldString = @"World";
    NSLog(@"Hello %@!", worldString); // prints => "Hello World!"
    // NSMutableString is a mutable version of the NSString object
    NSMutableString *mutableString = [NSMutableString stringWithString:@"Hello"];
    [mutableString appendString:@" World!"];
    NSLog(@"%@", mutableString); // prints => "Hello World!"

    // Character literals
    NSNumber *theLetterZNumber = @'Z';
    char theLetterZ            = [theLetterZNumber charValue]; // or 'Z'
    NSLog(@"%c", theLetterZ);

    // Integral literals
    NSNumber *fortyTwoNumber = @42;
    int fortyTwo             = [fortyTwoNumber intValue]; // or 42
    NSLog(@"%i", fortyTwo);

    NSNumber *fortyTwoUnsignedNumber = @42U;
    unsigned int fortyTwoUnsigned    = [fortyTwoUnsignedNumber unsignedIntValue]; // or 42
    NSLog(@"%u", fortyTwoUnsigned);

    NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42];
    short fortyTwoShort           = [fortyTwoShortNumber shortValue]; // or 42
    NSLog(@"%hi", fortyTwoShort);

    NSNumber *fortyOneShortNumber   = [NSNumber numberWithShort:41];
    unsigned short fortyOneUnsigned = [fortyOneShortNumber unsignedShortValue]; // or 41
    NSLog(@"%u", fortyOneUnsigned);

    NSNumber *fortyTwoLongNumber = @42L;
    long fortyTwoLong            = [fortyTwoLongNumber longValue]; // or 42
    NSLog(@"%li", fortyTwoLong);

    NSNumber *fiftyThreeLongNumber   = @53L;
    unsigned long fiftyThreeUnsigned = [fiftyThreeLongNumber unsignedLongValue]; // or 53
    NSLog(@"%lu", fiftyThreeUnsigned);

    // Floating point literals
    NSNumber *piFloatNumber = @3.141592654F;
    float piFloat           = [piFloatNumber floatValue]; // or 3.141592654f
    NSLog(@"%f", piFloat); // prints => 3.141592654
    NSLog(@"%5.2f", piFloat); // prints => " 3.14"

    NSNumber *piDoubleNumber = @3.1415926535;
    double piDouble          = [piDoubleNumber doubleValue]; // or 3.1415926535
    NSLog(@"%f", piDouble);
    NSLog(@"%4.2f", piDouble); // prints => "3.14"

    // NSDecimalNumber is a fixed-point class that's more precise than float or double
    NSDecimalNumber *oneDecNum = [NSDecimalNumber decimalNumberWithString:@"10.99"];
    NSDecimalNumber *twoDecNum = [NSDecimalNumber decimalNumberWithString:@"5.002"];
    // NSDecimalNumber isn't able to use standard +, -, *, / operators so it provides its own:
    [oneDecNum decimalNumberByAdding:twoDecNum];
    [oneDecNum decimalNumberBySubtracting:twoDecNum];
    [oneDecNum decimalNumberByMultiplyingBy:twoDecNum];
    [oneDecNum decimalNumberByDividingBy:twoDecNum];
    NSLog(@"%@", oneDecNum); // prints => 10.99 as NSDecimalNumber is immutable

    // BOOL literals
    NSNumber *yesNumber = @YES;
    NSNumber *noNumber  = @NO;
    // or
    BOOL yesBool = YES;
    BOOL noBool  = NO;
    NSLog(@"%i", yesBool); // prints => 1

    // Array object
    // May contain different data types, but must be an Objective-C object
    NSArray *anArray      = @[@1, @2, @3, @4];
    NSNumber *thirdNumber = anArray[2];
    NSLog(@"Third number = %@", thirdNumber); // prints => "Third number = 3"
    // Since Xcode 7, NSArray objects can be typed (Generics)
    NSArray<NSString *> *stringArray = @[@"hello", @"world"];
    // NSMutableArray is a mutable version of NSArray, allowing you to change
    // the items in the array and to extend or shrink the array object.
    // Convenient, but not as efficient as NSArray.
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:2];
    [mutableArray addObject:@"Hello"];
    [mutableArray addObject:@"World"];
    [mutableArray removeObjectAtIndex:0];
    NSLog(@"%@", [mutableArray objectAtIndex:0]); // prints => "World"

    // Dictionary object
    NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
    NSObject *valueObject     = aDictionary[@"A Key"];
    NSLog(@"Object = %@", valueObject); // prints => "Object = (null)"
    // Since Xcode 7, NSDictionary objects can be typed (Generics)
    NSDictionary<NSString *, NSNumber *> *numberDictionary = @{@"a": @1, @"b": @2};
    // NSMutableDictionary also available as a mutable dictionary object
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:2];
    [mutableDictionary setObject:@"value1" forKey:@"key1"];
    [mutableDictionary setObject:@"value2" forKey:@"key2"];
    [mutableDictionary removeObjectForKey:@"key1"];

    // Change types from Mutable To Immutable
    //In general [object mutableCopy] will make the object mutable whereas [object copy] will make the object immutable
    NSMutableDictionary *aMutableDictionary = [aDictionary mutableCopy];
    NSDictionary *mutableDictionaryChanged = [mutableDictionary copy];


    // Set object
    NSSet *set = [NSSet setWithObjects:@"Hello", @"Hello", @"World", nil];
    NSLog(@"%@", set); // prints => {(Hello, World)} (may be in different order)
    // Since Xcode 7, NSSet objects can be typed (Generics)
    NSSet<NSString *> *stringSet = [NSSet setWithObjects:@"hello", @"world", nil];
    // NSMutableSet also available as a mutable set object
    NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:2];
    [mutableSet addObject:@"Hello"];
    [mutableSet addObject:@"Hello"];
    NSLog(@"%@", mutableSet); // prints => {(Hello)}

    ///////////////////////////////////////
    // Operators
    ///////////////////////////////////////

    // The operators works like in the C language
    // For example:
    2 + 5; // => 7
    4.2f + 5.1f; // => 9.3f
    3 == 2; // => 0 (NO)
    3 != 2; // => 1 (YES)
    1 && 1; // => 1 (Logical and)
    0 || 1; // => 1 (Logical or)
    ~0x0F; // => 0xF0 (bitwise negation)
    0x0F & 0xF0; // => 0x00 (bitwise AND)
    0x01 << 1; // => 0x02 (bitwise left shift (by 1))

    ///////////////////////////////////////
    // Control Structures
    ///////////////////////////////////////

    // If-Else statement
    if (NO)
    {
        NSLog(@"I am never run");
    } else if (0)
    {
        NSLog(@"I am also never run");
    } else
    {
        NSLog(@"I print");
    }

    // Switch statement
    switch (2)
    {
        case 0:
        {
            NSLog(@"I am never run");
        } break;
        case 1:
        {
            NSLog(@"I am also never run");
        } break;
        default:
        {
            NSLog(@"I print");
        } break;
    }

    // While loops statements
    int ii = 0;
    while (ii < 4)
    {
        NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value
    } // prints => "0,"
      //           "1,"
      //           "2,"
      //           "3,"

    // For loops statements
    int jj;
    for (jj=0; jj < 4; jj++)
    {
        NSLog(@"%d,", jj);
    } // prints => "0,"
      //           "1,"
      //           "2,"
      //           "3,"

    // Foreach statements
    NSArray *values = @[@0, @1, @2, @3];
    for (NSNumber *value in values)
    {
        NSLog(@"%@,", value);
    } // prints => "0,"
      //           "1,"
      //           "2,"
      //           "3,"

    // Object for loop statement. Can be used with any Objective-C object type
    for (id item in values) {
        NSLog(@"%@,", item);
    } // prints => "0,"
      //           "1,"
      //           "2,"
      //           "3,"

    // Try-Catch-Finally statements
    @try
    {
        // Your statements here
        @throw [NSException exceptionWithName:@"FileNotFoundException"
                            reason:@"File Not Found on System" userInfo:nil];
    } @catch (NSException * e) // use: @catch (id exceptionName) to catch all objects.
    {
        NSLog(@"Exception: %@", e);
    } @finally
    {
        NSLog(@"Finally. Time to clean up.");
    } // prints => "Exception: File Not Found on System"
      //           "Finally. Time to clean up."

    // NSError objects are useful for function arguments to populate on user mistakes.
    NSError *error = [NSError errorWithDomain:@"Invalid email." code:4 userInfo:nil];

    ///////////////////////////////////////
    // Objects
    ///////////////////////////////////////

    // Create an object instance by allocating memory and initializing it
    // An object is not fully functional until both steps have been completed
    MyClass *myObject = [[MyClass alloc] init];

    // The Objective-C model of object-oriented programming is based on message
    // passing to object instances
    // In Objective-C one does not simply call a method; one sends a message
    [myObject instanceMethodWithParameter:@"Steve Jobs"];

    // Clean up the memory you used into your program
    [pool drain];

    // End of @autoreleasepool
    }

    // End the program
    return 0;
}

///////////////////////////////////////
// Classes And Functions
///////////////////////////////////////

// Declare your class in a header file (MyClass.h):
// Class declaration syntax:
// @interface ClassName : ParentClassName <ImplementedProtocols>
// {
//    type name; <= variable declarations;
// }
// @property type name; <= property declarations
// -/+ (type) Method declarations; <= Method declarations
// @end
@interface MyClass : NSObject <MyProtocol> // NSObject is Objective-C's base object class.
{
    // Instance variable declarations (can exist in either interface or implementation file)
    int count; // Protected access by default.
    @private id data; // Private access (More convenient to declare in implementation file)
    NSString *name;
}
// Convenient notation for public access variables to auto generate a setter method
// By default, setter method name is 'set' followed by @property variable name
@property int propInt; // Setter method name = 'setPropInt'
@property (copy) id copyId; // (copy) => Copy the object during assignment
// (readonly) => Cannot set value outside @interface
@property (readonly) NSString *roString; // Use @synthesize in @implementation to create accessor
// You can customize the getter and setter names instead of using default 'set' name:
@property (getter=lengthGet, setter=lengthSet:) int length;

// Methods
+/- (return type)methodSignature:(Parameter Type *)parameterName;

// + for class methods:
+ (NSString *)classMethod;
+ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight;

// - for instance methods:
- (NSString *)instanceMethodWithParameter:(NSString *)string;
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;

// Constructor methods with arguments:
- (id)initWithDistance:(int)defaultDistance;
// Objective-C method names are very descriptive. Always name methods according to their arguments

@end // States the end of the interface


// To access public variables from the implementation file, @property generates a setter method
// automatically. Method name is 'set' followed by @property variable name:
MyClass *myClass = [[MyClass alloc] init]; // create MyClass object instance
[myClass setCount:10];
NSLog(@"%d", [myClass count]); // prints => 10
// Or using the custom getter and setter method defined in @interface:
[myClass lengthSet:32];
NSLog(@"%i", [myClass lengthGet]); // prints => 32
// For convenience, you may use dot notation to set and access object instance variables:
myClass.count = 45;
NSLog(@"%i", myClass.count); // prints => 45

// Call class methods:
NSString *classMethodString = [MyClass classMethod];
MyClass *classFromName = [MyClass myClassFromName:@"Hello"];

// Call instance methods:
MyClass *myClass = [[MyClass alloc] init]; // Create MyClass object instance
NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Hello"];

// Selectors
// Way to dynamically represent methods. Used to call methods of a class, pass methods
// through functions to tell other classes they should call it, and to save methods
// as a variable
// SEL is the data type. @selector() returns a selector from method name provided
// methodAParameterAsString:andAParameterAsNumber: is method name for method in MyClass
SEL selectorVar = @selector(methodAParameterAsString:andAParameterAsNumber:);
if ([myClass respondsToSelector:selectorVar]) { // Checks if class contains method
    // Must put all method arguments into one object to send to performSelector function
    NSArray *arguments = [NSArray arrayWithObjects:@"Hello", @4, nil];
    [myClass performSelector:selectorVar withObject:arguments]; // Calls the method
} else {
    // NSStringFromSelector() returns a NSString of the method name of a given selector
    NSLog(@"MyClass does not have method: %@", NSStringFromSelector(selectedVar));
}

// Implement the methods in an implementation (MyClass.m) file:
@implementation MyClass {
    long distance; // Private access instance variable
    NSNumber height;
}

// To access a public variable from the interface file, use '_' followed by variable name:
_count = 5; // References "int count" from MyClass interface
// Access variables defined in implementation file:
distance = 18; // References "long distance" from MyClass implementation
// To use @property variable in implementation, use @synthesize to create accessor variable:
@synthesize roString = _roString; // _roString available now in @implementation

// Called before calling any class methods or instantiating any objects
+ (void)initialize
{
    if (self == [MyClass class]) {
        distance = 0;
    }
}

// Counterpart to initialize method. Called when an object's reference count is zero
- (void)dealloc
{
    [height release]; // If not using ARC, make sure to release class variable objects
    [super dealloc];  // and call parent class dealloc
}

// Constructors are a way of creating instances of a class
// This is a default constructor which is called when the object is initialized.
- (id)init
{
    if ((self = [super init])) // 'super' used to access methods from parent class
    {
        self.count = 1; // 'self' used for object to call itself
    }
    return self;
}
// Can create constructors that contain arguments:
- (id)initWithDistance:(int)defaultDistance
{
    distance = defaultDistance;
    return self;
}

+ (NSString *)classMethod
{
    return @"Some string";
}

+ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight
{
    height = defaultHeight;
    return [[self alloc] init];
}

- (NSString *)instanceMethodWithParameter:(NSString *)string
{
    return @"New string";
}

- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number
{
    return @42;
}

// Objective-C does not have private method declarations, but you can simulate them.
// To simulate a private method, create the method in the @implementation but not in the @interface.
- (NSNumber *)secretPrivateMethod {
    return @72;
}
[self secretPrivateMethod]; // Calls private method

// Methods declared into MyProtocol
- (void)myProtocolMethod
{
    // statements
}

@end // States the end of the implementation

///////////////////////////////////////
// Categories
///////////////////////////////////////
// A category is a group of methods designed to extend a class. They allow you to add new methods
// to an existing class for organizational purposes. This is not to be mistaken with subclasses.
// Subclasses are meant to CHANGE functionality of an object while categories instead ADD
// functionality to an object.
// Categories allow you to:
// -- Add methods to an existing class for organizational purposes.
// -- Allow you to extend Objective-C object classes (ex: NSString) to add your own methods.
// -- Add ability to create protected and private methods to classes.
// NOTE: Do not override methods of the base class in a category even though you have the ability
// to. Overriding methods may cause compiler errors later between different categories and it
// ruins the purpose of categories to only ADD functionality. Subclass instead to override methods.

// Here is a simple Car base class.
@interface Car : NSObject

@property NSString *make;
@property NSString *color;

- (void)turnOn;
- (void)accelerate;

@end

// And the simple Car base class implementation:
#import "Car.h"

@implementation Car

@synthesize make = _make;
@synthesize color = _color;

- (void)turnOn {
    NSLog(@"Car is on.");
}
- (void)accelerate {
    NSLog(@"Accelerating.");
}

@end

// Now, if we wanted to create a Truck object, we would instead create a subclass of Car as it would
// be changing the functionality of the Car to behave like a truck. But lets say we want to just add
// functionality to this existing Car. A good example would be to clean the car. So we would create
// a category to add these cleaning methods:
// @interface filename: Car+Clean.h (BaseClassName+CategoryName.h)
#import "Car.h" // Make sure to import base class to extend.

@interface Car (Clean) // The category name is inside () following the name of the base class.

- (void)washWindows; // Names of the new methods we are adding to our Car object.
- (void)wax;

@end

// @implementation filename: Car+Clean.m (BaseClassName+CategoryName.m)
#import "Car+Clean.h" // Import the Clean category's @interface file.

@implementation Car (Clean)

- (void)washWindows {
    NSLog(@"Windows washed.");
}
- (void)wax {
    NSLog(@"Waxed.");
}

@end

// Any Car object instance has the ability to use a category. All they need to do is import it:
#import "Car+Clean.h" // Import as many different categories as you want to use.
#import "Car.h" // Also need to import base class to use it's original functionality.

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        Car *mustang = [[Car alloc] init];
        mustang.color = @"Red";
        mustang.make = @"Ford";

        [mustang turnOn]; // Use methods from base Car class.
        [mustang washWindows]; // Use methods from Car's Clean category.
    }
    return 0;
}

// Objective-C does not have protected method declarations but you can simulate them.
// Create a category containing all of the protected methods, then import it ONLY into the
// @implementation file of a class belonging to the Car class:
@interface Car (Protected) // Naming category 'Protected' to remember methods are protected.

- (void)lockCar; // Methods listed here may only be created by Car objects.

@end
//To use protected methods, import the category, then implement the methods:
#import "Car+Protected.h" // Remember, import in the @implementation file only.

@implementation Car

- (void)lockCar {
    NSLog(@"Car locked."); // Instances of Car can't use lockCar because it's not in the @interface.
}

@end

///////////////////////////////////////
// Extensions
///////////////////////////////////////
// Extensions allow you to override public access property attributes and methods of an @interface.
// @interface filename: Shape.h
@interface Shape : NSObject // Base Shape class extension overrides below.

@property (readonly) NSNumber *numOfSides;

- (int)getNumOfSides;

@end
// You can override numOfSides variable or getNumOfSides method to edit them with an extension:
// @implementation filename: Shape.m
#import "Shape.h"
// Extensions live in the same file as the class @implementation.
@interface Shape () // () after base class name declares an extension.

@property (copy) NSNumber *numOfSides; // Make numOfSides copy instead of readonly.
-(NSNumber)getNumOfSides; // Make getNumOfSides return a NSNumber instead of an int.
-(void)privateMethod; // You can also create new private methods inside of extensions.

@end
// The main @implementation:
@implementation Shape

@synthesize numOfSides = _numOfSides;

-(NSNumber)getNumOfSides { // All statements inside of extension must be in the @implementation.
    return _numOfSides;
}
-(void)privateMethod {
    NSLog(@"Private method created by extension. Shape instances cannot call me.");
}

@end

// Starting in Xcode 7.0, you can create Generic classes,
// allowing you to provide greater type safety and clarity
// without writing excessive boilerplate.
@interface Result<__covariant A> : NSObject

- (void)handleSuccess:(void(^)(A))success
              failure:(void(^)(NSError *))failure;

@property (nonatomic) A object;

@end

// we can now declare instances of this class like
Result<NSNumber *> *result;
Result<NSArray *> *result;

// Each of these cases would be equivalent to rewriting Result's interface
// and substituting the appropriate type for A
@interface Result : NSObject
- (void)handleSuccess:(void(^)(NSArray *))success
              failure:(void(^)(NSError *))failure;
@property (nonatomic) NSArray * object;
@end

@interface Result : NSObject
- (void)handleSuccess:(void(^)(NSNumber *))success
              failure:(void(^)(NSError *))failure;
@property (nonatomic) NSNumber * object;
@end

// It should be obvious, however, that writing one
//  Class to solve a problem is always preferable to writing two

// Note that Clang will not accept generic types in @implementations,
// so your @implemnation of Result would have to look like this:

@implementation Result

- (void)handleSuccess:(void (^)(id))success
              failure:(void (^)(NSError *))failure {
  // Do something
}

@end


///////////////////////////////////////
// Protocols
///////////////////////////////////////
// A protocol declares methods that can be implemented by any class.
// Protocols are not classes themselves. They simply define an interface
// that other objects are responsible for implementing.
// @protocol filename: "CarUtilities.h"
@protocol CarUtilities <NSObject> // <NSObject> => Name of another protocol this protocol includes.
    @property BOOL engineOn; // Adopting class must @synthesize all defined @properties and
    - (void)turnOnEngine; // all defined methods.
@end
// Below is an example class implementing the protocol.
#import "CarUtilities.h" // Import the @protocol file.

@interface Car : NSObject <CarUtilities> // Name of protocol goes inside <>
    // You don't need the @property or method names here for CarUtilities. Only @implementation does.
- (void)turnOnEngineWithUtilities:(id <CarUtilities>)car; // You can use protocols as data too.
@end
// The @implementation needs to implement the @properties and methods for the protocol.
@implementation Car : NSObject <CarUtilities>

@synthesize engineOn = _engineOn; // Create a @synthesize statement for the engineOn @property.

- (void)turnOnEngine { // Implement turnOnEngine however you would like. Protocols do not define
    _engineOn = YES; // how you implement a method, it just requires that you do implement it.
}
// You may use a protocol as data as you know what methods and variables it has implemented.
- (void)turnOnEngineWithCarUtilities:(id <CarUtilities>)objectOfSomeKind {
    [objectOfSomeKind engineOn]; // You have access to object variables
    [objectOfSomeKind turnOnEngine]; // and the methods inside.
    [objectOfSomeKind engineOn]; // May or may not be YES. Class implements it however it wants.
}

@end
// Instances of Car now have access to the protocol.
Car *carInstance = [[Car alloc] init];
[carInstance setEngineOn:NO];
[carInstance turnOnEngine];
if ([carInstance engineOn]) {
    NSLog(@"Car engine is on."); // prints => "Car engine is on."
}
// Make sure to check if an object of type 'id' implements a protocol before calling protocol methods:
if ([myClass conformsToProtocol:@protocol(CarUtilities)]) {
    NSLog(@"This does not run as the MyClass class does not implement the CarUtilities protocol.");
} else if ([carInstance conformsToProtocol:@protocol(CarUtilities)]) {
    NSLog(@"This does run as the Car class implements the CarUtilities protocol.");
}
// Categories may implement protocols as well: @interface Car (CarCategory) <CarUtilities>
// You may implement many protocols: @interface Car : NSObject <CarUtilities, CarCleaning>
// NOTE: If two or more protocols rely on each other, make sure to forward-declare them:
#import "Brother.h"

@protocol Brother; // Forward-declare statement. Without it, compiler will throw error.

@protocol Sister <NSObject>

- (void)beNiceToBrother:(id <Brother>)brother;

@end

// See the problem is that Sister relies on Brother, and Brother relies on Sister.
#import "Sister.h"

@protocol Sister; // These lines stop the recursion, resolving the issue.

@protocol Brother <NSObject>

- (void)beNiceToSister:(id <Sister>)sister;

@end


///////////////////////////////////////
// Blocks
///////////////////////////////////////
// Blocks are statements of code, just like a function, that are able to be used as data.
// Below is a simple block with an integer argument that returns the argument plus 4.
int (^addUp)(int n); // Declare a variable to store the block.
void (^noParameterBlockVar)(void); // Example variable declaration of block with no arguments.
// Blocks have access to variables in the same scope. But the variables are readonly and the
// value passed to the block is the value of the variable when the block is created.
int outsideVar = 17; // If we edit outsideVar after declaring addUp, outsideVar is STILL 17.
__block long mutableVar = 3; // __block makes variables writable to blocks, unlike outsideVar.
addUp = ^(int n) { // Remove (int n) to have a block that doesn't take in any parameters.
    NSLog(@"You may have as many lines in a block as you would like.");
    NSSet *blockSet; // Also, you can declare local variables.
    mutableVar = 32; // Assigning new value to __block variable.
    return n + outsideVar; // Return statements are optional.
}
int addUp = addUp(10 + 16); // Calls block code with arguments.
// Blocks are often used as arguments to functions to be called later, or for callbacks.
@implementation BlockExample : NSObject

 - (void)runBlock:(void (^)(NSString))block {
    NSLog(@"Block argument returns nothing and takes in a NSString object.");
    block(@"Argument given to block to execute."); // Calling block.
 }

 @end


///////////////////////////////////////
// Memory Management
///////////////////////////////////////
/*
For each object used in an application, memory must be allocated for that object. When the application
is done using that object, memory must be deallocated to ensure application efficiency.
Objective-C does not use garbage collection and instead uses reference counting. As long as
there is at least one reference to an object (also called "owning" an object), then the object
will be available to use (known as "ownership").

When an instance owns an object, its reference counter is increments by one. When the
object is released, the reference counter decrements by one. When reference count is zero,
the object is removed from memory.

With all object interactions, follow the pattern of:
(1) create the object, (2) use the object, (3) then free the object from memory.
*/

MyClass *classVar = [MyClass alloc]; // 'alloc' sets classVar's reference count to one. Returns pointer to object
[classVar release]; // Decrements classVar's reference count
// 'retain' claims ownership of existing object instance and increments reference count. Returns pointer to object
MyClass *newVar = [classVar retain]; // If classVar is released, object is still in memory because newVar is owner
[classVar autorelease]; // Removes ownership of object at end of @autoreleasepool block. Returns pointer to object

// @property can use 'retain' and 'assign' as well for small convenient definitions
@property (retain) MyClass *instance; // Release old value and retain a new one (strong reference)
@property (assign) NSSet *set; // Pointer to new value without retaining/releasing old (weak reference)

// Automatic Reference Counting (ARC)
// Because memory management can be a pain, Xcode 4.2 and iOS 4 introduced Automatic Reference Counting (ARC).
// ARC is a compiler feature that inserts retain, release, and autorelease automatically for you, so when using ARC,
// you must not use retain, release, or autorelease
MyClass *arcMyClass = [[MyClass alloc] init];
// ... code using arcMyClass
// Without ARC, you will need to call: [arcMyClass release] after you're done using arcMyClass. But with ARC,
// there is no need. It will insert this release statement for you

// As for the 'assign' and 'retain' @property attributes, with ARC you use 'weak' and 'strong'
@property (weak) MyClass *weakVar; // 'weak' does not take ownership of object. If original instance's reference count
// is set to zero, weakVar will automatically receive value of nil to avoid application crashing
@property (strong) MyClass *strongVar; // 'strong' takes ownership of object. Ensures object will stay in memory to use

// For regular variables (not @property declared variables), use the following:
__strong NSString *strongString; // Default. Variable is retained in memory until it leaves it's scope
__weak NSSet *weakSet; // Weak reference to existing object. When existing object is released, weakSet is set to nil
__unsafe_unretained NSArray *unsafeArray; // Like __weak, but unsafeArray not set to nil when existing object is released

```
## Further Reading

[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C)

[Programming with Objective-C. Apple PDF book](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf)

[Programming with Objective-C for iOS](https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/ObjectiveC.html)

[Programming with Objective-C for Mac OSX](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html)

[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started)
---
language: OCaml
filename: learnocaml.ml
contributors:
    - ["Daniil Baturin", "http://baturin.org/"]
---

OCaml is a strictly evaluated functional language with some imperative
features.

Along with StandardML and its dialects it belongs to ML language family.
F# is also heavily influenced by OCaml.

Just like StandardML, OCaml features both an interpreter, that can be
used interactively, and a compiler.
The interpreter binary is normally called "ocaml" and the compiler is "ocamlopt".
There is also a bytecode compiler, "ocamlc", but there are few reasons to use it.

It is strongly and statically typed, but instead of using manually written
type annotations, it infers types of expressions using Hindley-Milner algorithm.
It makes type annotations unnecessary in most cases, but can be a major
source of confusion for beginners.

When you are in the top level loop, OCaml will print the inferred type
after you enter an expression.

```
# let inc x = x	+ 1 ;;
val inc : int -> int = <fun>
# let a = 99 ;;
val a : int = 99
```

For a source file you can use "ocamlc -i /path/to/file.ml" command
to print all names and type signatures.

```
$ cat sigtest.ml
let inc x = x + 1
let add x y = x + y

let a = 1

$ ocamlc -i ./sigtest.ml
val inc : int -> int
val add : int -> int -> int
val a : int
```

Note that type signatures of functions of multiple arguments are
written in curried form. A function that takes multiple arguments can be
represented as a composition of functions that take only one argument.
The "f(x,y) = x + y" function from the example above applied to
arguments 2 and 3 is equivalent to the "f0(y) = 2 + y" function applied to 3.
Hence the "int -> int -> int" signature.


```ocaml
(*** Comments ***)

(* Comments are enclosed in (* and *). It's fine to nest comments. *)

(* There are no single-line comments. *)


(*** Variables and functions ***)

(* Expressions can be separated by a double semicolon symbol, ";;".
   In many cases it's redundant, but in this tutorial we use it after
   every expression for easy pasting into the interpreter shell.
   Unnecessary use of expression separators in source code files
   is often considered to be a bad style. *)

(* Variable and function declarations use "let" keyword. *)
let x = 10 ;;

(* OCaml allows single quote characters in identifiers.
   Single quote doesn't have a special meaning in this case, it's often used
   in cases when in other languages one would use names like "foo_tmp". *)
let foo = 1 ;;
let foo' = foo * 2 ;;

(* Since OCaml compiler infers types automatically, you normally don't need to
   specify argument types explicitly. However, you can do it if
   you want or need to. *)
let inc_int (x: int) : int = x + 1 ;;

(* One of the cases when explicit type annotations may be needed is
   resolving ambiguity between two record types that have fields with
   the same name. The alternative is to encapsulate those types in
   modules, but both topics are a bit out of scope of this
   tutorial. *)

(* You need to mark recursive function definitions as such with "rec" keyword. *)
let rec factorial n =
    if n = 0 then 1
    else n * factorial (n-1)
;;

(* Function application usually doesn't need parentheses around arguments *)
let fact_5 = factorial 5 ;;

(* ...unless the argument is an expression. *)
let fact_4 = factorial (5-1) ;;
let sqr2 = sqr (-2) ;;

(* Every function must have at least one argument.
   Since some functions naturally don't take any arguments, there's
   "unit" type for it that has the only one value written as "()" *)
let print_hello () = print_endline "hello world" ;;

(* Note that you must specify "()" as argument when calling it. *)
print_hello () ;;

(* Calling a function with insufficient number of arguments
   does not cause an error, it produces a new function. *)
let make_inc x y = x + y ;; (* make_inc is int -> int -> int *)
let inc_2 = make_inc 2 ;;   (* inc_2 is int -> int *)
inc_2 3 ;; (* Evaluates to 5 *)

(* You can use multiple expressions in function body.
   The last expression becomes the return value. All other
   expressions must be of the "unit" type.
   This is useful when writing in imperative style, the simplest
   form of it is inserting a debug print. *)
let print_and_return x =
    print_endline (string_of_int x);
    x
;;

(* Since OCaml is a functional language, it lacks "procedures".
   Every function must return something. So functions that
   do not really return anything and are called solely for their
   side effects, like print_endline, return value of "unit" type. *)


(* Definitions can be chained with "let ... in" construct.
   This is roughly the same to assigning values to multiple
   variables before using them in expressions in imperative
   languages. *)
let x = 10 in
let y = 20 in
x + y ;;

(* Alternatively you can use "let ... and ... in" construct.
   This is especially useful for mutually recursive functions,
   with ordinary "let .. in" the compiler will complain about
   unbound values. *)
let rec
  is_even = function
  | 0 -> true
  | n -> is_odd (n-1)
and
  is_odd = function
  | 0 -> false
  | n -> is_even (n-1)
;;

(* Anonymous functions use the following syntax: *)
let my_lambda = fun x -> x * x ;;

(*** Operators ***)

(* There is little distintion between operators and functions.
   Every operator can be called as a function. *)

(+) 3 4  (* Same as 3 + 4 *)

(* There's a number of built-in operators. One unusual feature is
   that OCaml doesn't just refrain from any implicit conversions
   between integers and floats, it also uses different operators
   for floats. *)
12 + 3 ;; (* Integer addition. *)
12.0 +. 3.0 ;; (* Floating point addition. *)

12 / 3 ;; (* Integer division. *)
12.0 /. 3.0 ;; (* Floating point division. *)
5 mod 2 ;; (* Remainder. *)

(* Unary minus is a notable exception, it's polymorphic.
   However, it also has "pure" integer and float forms. *)
- 3 ;; (* Polymorphic, integer *)
- 4.5 ;; (* Polymorphic, float *)
~- 3 (* Integer only *)
~- 3.4 (* Type error *)
~-. 3.4 (* Float only *)

(* You can define your own operators or redefine existing ones.
   Unlike SML or Haskell, only selected symbols can be used
   for operator names and first symbol defines associativity
   and precedence rules. *)
let (+) a b = a - b ;; (* Surprise maintenance programmers. *)

(* More useful: a reciprocal operator for floats.
   Unary operators must start with "~". *)
let (~/) x = 1.0 /. x ;;
~/4.0 (* = 0.25 *)


(*** Built-in data structures ***)

(* Lists are enclosed in square brackets, items are separated by
   semicolons. *)
let my_list = [1; 2; 3] ;;

(* Tuples are (optionally) enclosed in parentheses, items are separated
   by commas. *)
let first_tuple = 3, 4 ;; (* Has type "int * int". *)
let second_tuple = (4, 5) ;;

(* Corollary: if you try to separate list items by commas, you get a list
   with a tuple inside, probably not what you want. *)
let bad_list = [1, 2] ;; (* Becomes [(1, 2)] *)

(* You can access individual list items with the List.nth function. *)
List.nth my_list 1 ;;

(* There are higher-order functions for lists such as map and filter. *)
List.map (fun x -> x * 2) [1; 2; 3] ;;
List.filter (fun x -> x mod 2 = 0) [1; 2; 3; 4] ;;

(* You can add an item to the beginning of a list with the "::" constructor
   often referred to as "cons". *)
1 :: [2; 3] ;; (* Gives [1; 2; 3] *)

(* Arrays are enclosed in [| |] *)
let my_array = [| 1; 2; 3 |] ;;

(* You can access array items like this: *)
my_array.(0) ;;


(*** Strings and characters ***)

(* Use double quotes for string literals. *)
let my_str = "Hello world" ;;

(* Use single quotes for character literals. *)
let my_char = 'a' ;;

(* Single and double quotes are not interchangeable. *)
let bad_str = 'syntax error' ;; (* Syntax error. *)

(* This will give you a single character string, not a character. *)
let single_char_str = "w" ;;

(* Strings can be concatenated with the "^" operator. *)
let some_str = "hello" ^ "world" ;;

(* Strings are not arrays of characters.
   You can't mix characters and strings in expressions.
   You can convert a character to a string with "String.make 1 my_char".
   There are more convenient functions for this purpose in additional
   libraries such as Core.Std that may not be installed and/or loaded
   by default. *)
let ocaml = (String.make 1 'O') ^ "Caml" ;;

(* There is a printf function. *)
Printf.printf "%d %s" 99 "bottles of beer" ;;

(* Unformatted read and write functions are there too. *)
print_string "hello world\n" ;;
print_endline "hello world" ;;
let line = read_line () ;;


(*** User-defined data types ***)

(* You can define types with the "type some_type =" construct. Like in this
   useless type alias: *)
type my_int = int ;;

(* More interesting types include so called type constructors.
   Constructors must start with a capital letter. *)
type ml = OCaml | StandardML ;;
let lang = OCaml ;;  (* Has type "ml". *)

(* Type constructors don't need to be empty. *)
type my_number = PlusInfinity | MinusInfinity | Real of float ;;
let r0 = Real (-3.4) ;; (* Has type "my_number". *)

(* Can be used to implement polymorphic arithmetics. *)
type number = Int of int | Float of float ;;

(* Point on a plane, essentially a type-constrained tuple *)
type point2d = Point of float * float ;;
let my_point = Point (2.0, 3.0) ;;

(* Types can be parameterized, like in this type for "list of lists
   of anything". 'a can be substituted with any type. *)
type 'a list_of_lists = 'a list list ;;
type int_list_list = int list_of_lists ;;

(* Types can also be recursive. Like in this type analogous to
   built-in list of integers. *)
type my_int_list = EmptyList | IntList of int * my_int_list ;;
let l = IntList (1, EmptyList) ;;


(*** Pattern matching ***)

(* Pattern matching is somewhat similar to switch statement in imperative
   languages, but offers a lot more expressive power.

   Even though it may look complicated, it really boils down to matching
   an argument against an exact value, a predicate, or a type constructor.
   The type system is what makes it so powerful. *)

(** Matching exact values.  **)

let is_zero x =
    match x with
    | 0 -> true
    | _ -> false  (* The "_" pattern means "anything else". *)
;;

(* Alternatively, you can use the "function" keyword. *)
let is_one = function
| 1 -> true
| _ -> false
;;

(* Matching predicates, aka "guarded pattern matching". *)
let abs x =
    match x with
    | x when x < 0 -> -x
    | _ -> x
;;

abs 5 ;; (* 5 *)
abs (-5) (* 5 again *)

(** Matching type constructors **)

type animal = Dog of string | Cat of string ;;

let say x =
    match x with
    | Dog x -> x ^ " says woof"
    | Cat x -> x ^ " says meow"
;;

say (Cat "Fluffy") ;; (* "Fluffy says meow". *)

(** Traversing data structures with pattern matching **)

(* Recursive types can be traversed with pattern matching easily.
   Let's see how we can traverse a data structure of the built-in list type.
   Even though the built-in cons ("::") looks like an infix operator,
   it's actually a type constructor and can be matched like any other. *)
let rec sum_list l =
    match l with
    | [] -> 0
    | head :: tail -> head + (sum_list tail)
;;

sum_list [1; 2; 3] ;; (* Evaluates to 6 *)

(* Built-in syntax for cons obscures the structure a bit, so we'll make
   our own list for demonstration. *)

type int_list = Nil | Cons of int * int_list ;;
let rec sum_int_list l =
  match l with
      | Nil -> 0
      | Cons (head, tail) -> head + (sum_int_list tail)
;;

let t = Cons (1, Cons (2, Cons (3, Nil))) ;;
sum_int_list t ;;

```

## Further reading

* Visit the official website to get the compiler and read the docs: <http://ocaml.org/>
* Try interactive tutorials and a web-based interpreter by OCaml Pro: <http://try.ocamlpro.com/>
* Read "OCaml for the skeptical" course: <http://www2.lib.uchicago.edu/keith/ocaml-class/home.html>
---

language: Paren
filename: learnparen.paren
contributors:
  - ["KIM Taegyoon", "https://github.com/kimtg"]
  - ["Claudson Martins", "https://github.com/claudsonm"]
---

[Paren](https://bitbucket.org/ktg/paren) is a dialect of Lisp. It is designed to be an embedded language.

Some examples are from <http://learnxinyminutes.com/docs/racket/>.

```scheme
;;; Comments
# comments

;; Single line comments start with a semicolon or a sharp sign

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Primitive Datatypes and Operators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Numbers
123 ; int
3.14 ; double
6.02e+23 ; double
(int 3.14) ; => 3 : int
(double 123) ; => 123 : double

;; Function application is written (f x y z ...)
;; where f is a function and x, y, z, ... are operands
;; If you want to create a literal list of data, use (quote) to stop it from
;; being evaluated
(quote (+ 1 2)) ; => (+ 1 2)
;; Now, some arithmetic operations
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(^ 2 3) ; => 8
(/ 5 2) ; => 2
(% 5 2) ; => 1
(/ 5.0 2) ; => 2.5

;;; Booleans
true ; for true
false ; for false
(! true) ; => false
(&& true false (prn "doesn't get here")) ; => false
(|| false true (prn "doesn't get here")) ; => true

;;; Characters are ints.
(char-at "A" 0) ; => 65
(chr 65) ; => "A"

;;; Strings are fixed-length array of characters.
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; backslash is an escaping character
"Foo\tbar\r\n" ; includes C escapes: \t \r \n

;; Strings can be added too!
(strcat "Hello " "world!") ; => "Hello world!"

;; A string can be treated like a list of characters
(char-at "Apple" 0) ; => 65

;; Printing is pretty easy
(pr "I'm" "Paren. ") (prn "Nice to meet you!")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; You can create or set a variable using (set)
;; a variable name can use any character except: ();#"
(set some-var 5) ; => 5
some-var ; => 5

;; Accessing a previously unassigned variable is an exception
; x ; => Unknown variable: x : nil

;; Local binding: Use lambda calculus! 'a' and 'b' are bound to '1' and '2' only within the (fn ...)
((fn (a b) (+ a b)) 1 2) ; => 3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Collections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Lists

;; Lists are vector-like data structures. (Random access is O(1).)
(cons 1 (cons 2 (cons 3 (list)))) ; => (1 2 3)
;; 'list' is a convenience variadic constructor for lists
(list 1 2 3) ; => (1 2 3)
;; and a quote can also be used for a literal list value
(quote (+ 1 2)) ; => (+ 1 2)

;; Can still use 'cons' to add an item to the beginning of a list
(cons 0 (list 1 2 3)) ; => (0 1 2 3)

;; Lists are a very basic type, so there is a *lot* of functionality for
;; them, a few examples:
(map inc (list 1 2 3))          ; => (2 3 4)
(filter (fn (x) (== 0 (% x 2))) (list 1 2 3 4))    ; => (2 4)
(length (list 1 2 3 4))     ; => 4

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use 'fn' to create functions.
;; A function always returns the value of its last expression
(fn () "Hello World") ; => (fn () Hello World) : fn

;; Use parentheses to call all functions, including a lambda expression
((fn () "Hello World")) ; => "Hello World"

;; Assign a function to a var
(set hello-world (fn () "Hello World"))
(hello-world) ; => "Hello World"

;; You can shorten this using the function definition syntactic sugar:
(defn hello-world2 () "Hello World")

;; The () in the above is the list of arguments for the function
(set hello
  (fn (name)
    (strcat "Hello " name)))
(hello "Steve") ; => "Hello Steve"

;; ... or equivalently, using a sugared definition:
(defn hello2 (name)
  (strcat "Hello " name))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Equality
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; for numbers use '=='
(== 3 3.0) ; => true
(== 2 1) ; => false

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Control Flow
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Conditionals

(if true               ; test expression
    "this is true"   ; then expression
    "this is false") ; else expression
; => "this is true"

;;; Loops

;; for loop is for number
;; (for SYMBOL START END STEP EXPR ..)
(for i 0 10 2 (pr i "")) ; => prints 0 2 4 6 8 10
(for i 0.0 10 2.5 (pr i "")) ; => prints 0 2.5 5 7.5 10

;; while loop
((fn (i)
  (while (< i 10)
    (pr i)
    (++ i))) 0) ; => prints 0123456789

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Mutation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use 'set' to assign a new value to a variable or a place
(set n 5) ; => 5
(set n (inc n)) ; => 6
n ; => 6
(set a (list 1 2)) ; => (1 2)
(set (nth 0 a) 3) ; => 3
a ; => (3 2)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Macros let you extend the syntax of the language.
;; Paren macros are easy.
;; In fact, (defn) is a macro.
(defmacro setfn (name ...) (set name (fn ...)))
(defmacro defn (name ...) (def name (fn ...)))

;; Let's add an infix notation
(defmacro infix (a op ...) (op a ...))
(infix 1 + 2 (infix 3 * 4)) ; => 15

;; Macros are not hygienic, you can clobber existing variables!
;; They are code transformations.
```
---
language: PCRE
filename: pcre.txt
contributors:
    - ["Sachin Divekar", "http://github.com/ssd532"]
    
---

A regular expression (regex or regexp for short) is a special text string for describing a search pattern. e.g. to extract domain name from a string we can say `/^[a-z]+:/` and it will match `http:` from `http://github.com/`.  

PCRE (Perl Compatible Regular Expressions) is a C library implementing regex. It was written in 1997 when Perl was the de-facto choice for complex text processing tasks. The syntax for patterns used in PCRE closely resembles Perl. PCRE syntax is being used in many big projects including PHP, Apache, R to name a few.


There are two different sets of metacharacters:
* Those that are recognized anywhere in the pattern except within square brackets
```
  \      general escape character with several uses
  ^      assert start of string (or line, in multiline mode)
  $      assert end of string (or line, in multiline mode)
  .      match any character except newline (by default)
  [      start character class definition
  |      start of alternative branch
  (      start subpattern
  )      end subpattern
  ?      extends the meaning of (
         also 0 or 1 quantifier
         also quantifier minimizer
  *      0 or more quantifier
  +      1 or more quantifier
         also "possessive quantifier"
  {      start min/max quantifier
```

* Those that are recognized within square brackets. Outside square brackets. They are also called as character classes.
 
```
 
  \      general escape character
  ^      negate the class, but only if the first character
  -      indicates character range
  [      POSIX character class (only if followed by POSIX syntax)
  ]      terminates the character class
  
```  

PCRE provides some generic character types, also called as character classes. 
```
  \d     any decimal digit
  \D     any character that is not a decimal digit
  \h     any horizontal white space character
  \H     any character that is not a horizontal white space character
  \s     any white space character
  \S     any character that is not a white space character
  \v     any vertical white space character
  \V     any character that is not a vertical white space character
  \w     any "word" character
  \W     any "non-word" character
```

## Examples

We will test our examples on following string `66.249.64.13 - - [18/Sep/2004:11:07:48 +1000] "GET /robots.txt HTTP/1.0" 200 468 "-" "Googlebot/2.1"`. It is a standard Apache access log.

| Regex | Result          | Comment |
| :---- | :-------------- | :------ |
| GET   | GET | GET matches the characters GET literally (case sensitive) |
| \d+.\d+.\d+.\d+ | 66.249.64.13 | `\d+` match a digit [0-9] one or more times defined by `+` quantifier, `\.` matches `.` literally |
| (\d+\.){3}\d+ | 66.249.64.13 | `(\d+\.){3}` is trying to match group (`\d+\.`) exactly three times. |
| \[.+\] | [18/Sep/2004:11:07:48 +1000] | `.+` matches any character (except newline), `.` is any character |
| ^\S+ | 66.249.64.13 | `^` means start of the line, `\S+` matches any number of non-space characters |
| \+[0-9]+ | +1000 | `\+` matches the character `+` literally. `[0-9]` character class means single number. Same can be achieved using `\+\d+` |

All these examples can be tried at https://regex101.com/ 

1. Copy the example string in `TEST STRING` section
2. Copy regex code in `Regular Expression` section 
3. The web application will show the matching result


## Further Reading


---
name: perl
category: language
language: perl
filename: learnperl.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
    - ["Dan Book", "http://github.com/Grinnz"]
---

Perl 5 is a highly capable, feature-rich programming language with over 25 years of development.

Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

```perl
# Single line comments start with a number sign.

#### Strict and warnings

use strict;
use warnings;

# All perl scripts and modules should include these lines. Strict causes
# compilation to fail in cases like misspelled variable names, and
# warnings will print warning messages in case of common pitfalls like
# concatenating to an undefined value.

#### Perl variable types

#  Variables begin with a sigil, which is a symbol showing the type.
#  A valid variable name starts with a letter or underscore,
#  followed by any number of letters, numbers, or underscores.

### Perl has three main variable types: $scalar, @array, and %hash.

## Scalars
#  A scalar represents a single value:
my $animal = "camel";
my $answer = 42;
my $display = "You have $answer ${animal}s.\n";

# Scalar values can be strings, integers or floating point numbers, and
# Perl will automatically convert between them as required.

# Strings in single quotes are literal strings. Strings in double quotes
# will interpolate variables and escape codes like "\n" for newline.

## Arrays
#  An array represents a list of values:
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed   = ("camel", 42, 1.23);

# Array elements are accessed using square brackets, with a $ to
# indicate one value will be returned.
my $second = $animals[1];

# The size of an array is retrieved by accessing the array in a scalar
# context, such as assigning it to a scalar variable or using the
# "scalar" operator.

my $num_animals = @animals;
print "Number of numbers: ", scalar(@numbers), "\n";

# Arrays can also be interpolated into double-quoted strings, and the
# elements are separated by a space character by default.

print "We have these numbers: @numbers\n";

# Be careful when using double quotes for strings containing symbols
# such as email addresses, as it will be interpreted as a variable.

my @example = ('secret', 'array');
my $oops_email = "foo@example.com"; # 'foosecret array.com'
my $ok_email = 'foo@example.com';

## Hashes
#   A hash represents a set of key/value pairs:

my %fruit_color = ("apple", "red", "banana", "yellow");

#  You can use whitespace and the "=>" operator to lay them out more
#  nicely:

my %fruit_color = (
  apple  => "red",
  banana => "yellow",
);

# Hash elements are accessed using curly braces, again with the $ sigil.
my $color = $fruit_color{apple};

# All of the keys or values that exist in a hash can be accessed using
# the "keys" and "values" functions.
my @fruits = keys %fruit_color;
my @colors = values %fruit_color;

# Scalars, arrays and hashes are documented more fully in perldata.
# (perldoc perldata).

#### References

# More complex data types can be constructed using references, which
# allow you to build arrays and hashes within arrays and hashes.

my $array_ref = \@array;
my $hash_ref = \%hash;
my @array_of_arrays = (\@array1, \@array2, \@array3);

# You can also create anonymous arrays or hashes, returning a reference:

my $fruits = ["apple", "banana"];
my $colors = {apple => "red", banana => "yellow"};

# References can be dereferenced by prefixing the appropriate sigil.

my @fruits_array = @$fruits;
my %colors_hash = %$colors;

# As a shortcut, the arrow operator can be used to dereference and
# access a single value.

my $first = $array_ref->[0];
my $value = $hash_ref->{banana};

# See perlreftut and perlref for more in-depth documentation on
# references.

#### Conditional and looping constructs

# Perl has most of the usual conditional and looping constructs.

if ($var) {
  ...
} elsif ($var eq 'bar') {
  ...
} else {
  ...
}

unless (condition) {
  ...
}
# This is provided as a more readable version of "if (!condition)"

# the Perlish post-condition way
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;

#  while
while (condition) {
  ...
}


# for loops and iteration
for my $i (0 .. $max) {
  print "index is $i";
}

for my $element (@elements) {
  print $element;
}

map {print} @elements;

# implicitly

for (@elements) {
  print;
}

# iterating through a hash (for and foreach are equivalent)

foreach my $key (keys %hash) {
  print $key, ': ', $hash{$key}, "\n";
}

# the Perlish post-condition way again
print for @elements;

# iterating through the keys and values of a referenced hash
print $hash_ref->{$_} for keys %$hash_ref;

#### Regular expressions

# Perl's regular expression support is both broad and deep, and is the
# subject of lengthy documentation in perlrequick, perlretut, and
# elsewhere. However, in short:

# Simple matching
if (/foo/)       { ... }  # true if $_ contains "foo"
if ($x =~ /foo/) { ... }  # true if $x contains "foo"

# Simple substitution

$x =~ s/foo/bar/;         # replaces foo with bar in $x
$x =~ s/foo/bar/g;        # replaces ALL INSTANCES of foo with bar in $x


#### Files and I/O

# You can open a file for input or output using the "open()" function.

# For reading:
open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";
# For writing (clears file if it exists):
open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
# For writing (appends to end of file):
open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";

# You can read from an open filehandle using the "<>" operator.  In
# scalar context it reads a single line from the filehandle, and in list
# context it reads the whole file in, assigning each line to an element
# of the list:

my $line  = <$in>;
my @lines = <$in>;

# You can write to an open filehandle using the standard "print"
# function.

print $out @lines;
print $log $msg, "\n";

#### Writing subroutines

# Writing subroutines is easy:

sub logger {
  my $logmessage = shift;

  open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";

  print $logfile $logmessage;
}

# Now we can use the subroutine just as any other built-in function:

logger("We have a logger subroutine!");

#### Modules

# A module is a set of Perl code, usually subroutines, which can be used
# in other Perl code. It is usually stored in a file with the extension
# .pm so that Perl can find it.

package MyModule;
use strict;
use warnings;

sub trim {
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
}

1;

# From elsewhere:

use MyModule;
MyModule::trim($string);

# The Exporter module can help with making subroutines exportable, so
# they can be used like this:

use MyModule 'trim';
trim($string);

# Many Perl modules can be downloaded from CPAN (http://www.cpan.org/)
# and provide a range of features to help you avoid reinventing the
# wheel.  A number of popular modules like Exporter are included with
# the Perl distribution itself. See perlmod for more details on modules
# in Perl.

#### Objects

# Objects in Perl are just references that know which class (package)
# they belong to, so that methods (subroutines) called on it can be
# found there. The bless function is used in constructors (usually new)
# to set this up. However, you never need to call it yourself if you use
# a module like Moose or Moo (see below).

package MyCounter;
use strict;
use warnings;

sub new {
  my $class = shift;
  my $self = {count => 0};
  return bless $self, $class;
}

sub count {
  my $self = shift;
  return $self->{count};
}

sub increment {
  my $self = shift;
  $self->{count}++;
}

1;

# Methods can be called on a class or object instance with the arrow
# operator.

use MyCounter;
my $counter = MyCounter->new;
print $counter->count, "\n"; # 0
$counter->increment;
print $counter->count, "\n"; # 1

# The modules Moose and Moo from CPAN can help you set up your object
# classes. They provide a constructor and simple syntax for declaring
# attributes. This class can be used equivalently to the one above.

package MyCounter;
use Moo; # imports strict and warnings

has 'count' => (is => 'rwp', default => 0, init_arg => undef);

sub increment {
  my $self = shift;
  $self->_set_count($self->count + 1);
}

1;

# Object-oriented programming is covered more thoroughly in perlootut,
# and its low-level implementation in Perl is covered in perlobj.
```

#### FAQ

perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use.

#### Further Reading

 - [perl-tutorial](http://perl-tutorial.org/)
 - [Learn at www.perl.com](http://www.perl.org/learn.html)
 - [perldoc](http://perldoc.perl.org/)
 - and perl built-in : `perldoc perlintro`
---
category: language
language: perl6
filename: learnperl6.p6
contributors:
    - ["vendethiel", "http://github.com/vendethiel"]
    - ["Samantha McVey", "https://cry.nu"]
---

Perl 6 is a highly capable, feature-rich programming language made for at
least the next hundred years.

The primary Perl 6 compiler is called [Rakudo](http://rakudo.org), which runs on
the JVM and [the MoarVM](http://moarvm.com).

Meta-note : the triple pound signs are here to denote headlines,
double paragraphs, and single notes.

`#=>` represents the output of a command.

```perl6
# Single line comment start with a pound

#`(
  Multiline comments use #` and a quoting construct.
  (), [], {}, 「」, etc, will work.
)
```

## Variables

```perl6
# In Perl 6, you declare a lexical variable using `my`
my $variable;
# Perl 6 has 4 kinds of variables:
```

### Scalars

```perl6
# Scalars represent a single value. They start with a `$`

my $str = 'String';
# double quotes allow for interpolation (which we'll see later):
my $str2 = "String";

# Variable names can contain but not end with simple quotes and dashes,
# and can contain (and end with) underscores :
# my $weird'variable-name_ = 5; # works !

my $bool = True; # `True` and `False` are Perl 6's boolean values.
my $inverse = !$bool; # You can invert a bool with the prefix `!` operator
my $forced-bool = so $str; # And you can use the prefix `so` operator
                           # which turns its operand into a Bool
```

### Arrays and Lists

```perl6
# Arrays represent multiple values. Their name start with `@`.
# Lists are similar but are an immutable type

my @array = 'a', 'b', 'c';
# equivalent to :
my @letters = <a b c>; # array of words, delimited by space.
                     # Similar to perl5's qw, or Ruby's %w.
my @array = 1, 2, 3;

say @array[2]; # Array indices start at 0 -- This is the third element

say "Interpolate all elements of an array using [] : @array[]";
#=> Interpolate all elements of an array using [] : 1 2 3

@array[0] = -1; # Assign a new value to an array index
@array[0, 1] = 5, 6; # Assign multiple values

my @keys = 0, 2;
@array[@keys] = @letters; # Assignment using an array containing index values
say @array; #=> a 6 b
```

### Hashes, or key-value Pairs.

```perl6
# Hashes are pairs of keys and values.
# You can construct a Pair object using the syntax `Key => Value`.
# Hash tables are very fast for lookup, and are stored unordered.
# Keep in mind that keys get "flattened" in hash context, and any duplicated
# keys are deduplicated.
my %hash = 1 => 2,
           3 => 4;
my %hash = foo => "bar", # keys get auto-quoted
            "some other" => "value", # trailing commas are okay
            ;
# Even though hashes are internally stored differently than arrays,
# Perl 6 allows you to easily create a hash from an even numbered array:
my %hash = <key1 value1 key2 value2>;

my %hash = key1 => 'value1', key2 => 'value2'; # same result as above

# You can also use the "colon pair" syntax:
# (especially handy for named parameters that you'll see later)
my %hash = :w(1),    # equivalent to `w => 1`
                     # this is useful for the `True` shortcut:
           :truey,   # equivalent to `:truey(True)`, or `truey => True`
                     # and for the `False` one:
           :!falsey, # equivalent to `:falsey(False)`, or `falsey => False`
           ;

say %hash{'key1'}; # You can use {} to get the value from a key
say %hash<key2>;   # If it's a string, you can actually use <>
                   # (`{key1}` doesn't work, as Perl6 doesn't have barewords)
```

## Subs

```perl6
# subroutines or functions as most other languages call them are
# created with the `sub` keyword.
sub say-hello { say "Hello, world" }

# You can provide (typed) arguments.
# If specified, the type will be checked at compile-time if possible,
#  otherwise at runtime.
sub say-hello-to(Str $name) {
    say "Hello, $name !";
}

# A sub returns the last value of the block.
sub return-value {
    5;
}
say return-value; # prints 5
sub return-empty {
}
say return-empty; # prints Nil

# Some control flow structures produce a value, like if:
sub return-if {
	if True {
		"Truthy";
	}
}
say return-if; # prints Truthy

# Some don't, like for:
sub return-for {
    for 1, 2, 3 { }
}
say return-for; # prints Nil


## A sub can have optional arguments:
sub with-optional($arg?) { # the "?" marks the argument optional
  say "I might return `(Any)` (Perl's 'null'-like value) if I don't have
        an argument passed, or I'll return my argument";
  $arg;
}
with-optional; # returns Any
with-optional(); # returns Any
with-optional(1); # returns 1

## You can also give them a default value when they're not passed:
sub hello-to($name = "World") {
  say "Hello, $name !";
}
hello-to; #=> Hello, World !
hello-to(); #=> Hello, World !
hello-to('You'); #=> Hello, You !

## You can also, by using a syntax akin to the one of hashes
## (yay unified syntax !), pass *named* arguments to a `sub`.
# They're optional, and will default to "Any".
sub with-named($normal-arg, :$named) {
  say $normal-arg + $named;
}
with-named(1, named => 6); #=> 7
# There's one gotcha to be aware of, here:
# If you quote your key, Perl 6 won't be able to see it at compile time,
#  and you'll have a single Pair object as a positional parameter,
#  which means this fails:
with-named(1, 'named' => 6);

with-named(2, :named(5)); #=> 7

# To make a named argument mandatory, you can use `?`'s inverse, `!`
sub with-mandatory-named(:$str!)  {
  say "$str !";
}
with-mandatory-named(str => "My String"); #=> My String !
with-mandatory-named;   # run time error: "Required named parameter not passed"
with-mandatory-named(3);# run time error:"Too many positional parameters passed"

## If a sub takes a named boolean argument ...
sub takes-a-bool($name, :$bool) {
  say "$name takes $bool";
}
# ... you can use the same "short boolean" hash syntax:
takes-a-bool('config', :bool); # config takes True
takes-a-bool('config', :!bool); # config takes False

## You can also provide your named arguments with defaults:
sub named-def(:$def = 5) {
  say $def;
}
named-def; #=> 5
named-def(def => 15); #=> 15

# Since you can omit parenthesis to call a function with no arguments,
#  you need "&" in the name to store `say-hello` in a variable.
my &s = &say-hello;
my &other-s = sub { say "Anonymous function !" }

# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many"
sub as-many($head, *@rest) {  #`*@` (slurpy) will "take everything else"
# Note: you can have parameters *before* a slurpy one (like here),
# but not *after*.
  say @rest.join(' / ') ~ " !";
}
say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday !
                                           # Note that the splat (the *) did not
                                           # consume the parameter before.

## You can call a function with an array using the
# "argument list flattening" operator `|`
# (it's not actually the only role of this operator, but it's one of them)
sub concat3($a, $b, $c) {
  say "$a, $b, $c";
}
concat3(|@array); #=> a, b, c
                  # `@array` got "flattened" as a part of the argument list
```

## Containers

```perl6
# In Perl 6, values are actually stored in "containers".
# The assignment operator asks the container on the left to store the value on
#  its right. When passed around, containers are marked as immutable.
# Which means that, in a function, you'll get an error if you try to
#  mutate one of your arguments.
# If you really need to, you can ask for a mutable container using `is rw`:
sub mutate($n is rw) {
  $n++;
  say "\$n is now $n !";
}

my $m = 42;
mutate $m; # $n is now 43 !

# This works because we are passing the container $m to mutate.  If we try
# to just pass a number instead of passing a variable it won't work because
# there is no container being passed and integers are immutable by themselves:

mutate 42; # Parameter '$n' expected a writable container, but got Int value

# If what you want a copy instead, use `is copy`.

# A sub itself returns a container, which means it can be marked as rw:
my $x = 42;
sub x-store() is rw { $x }
x-store() = 52; # in this case, the parentheses are mandatory
                # (else Perl 6 thinks `x-store` is an identifier)
say $x; #=> 52
```

## Control Flow Structures
### Conditionals

```perl6
# - `if`
# Before talking about `if`, we need to know which values are "Truthy"
# (represent True), and which are "Falsey" (or "Falsy") -- represent False.
# Only these values are Falsey: 0, (), {}, "", Nil, A type (like `Str` or `Int`)
# and of course False itself.
# Every other value is Truthy.
if True {
  say "It's true !";
}

unless False {
  say "It's not false !";
}

# As you can see, you don't need parentheses around conditions.
# However, you do need the brackets around the "body" block:
# if (true) say; # This doesn't work !

# You can also use their postfix versions, with the keyword after:
say "Quite truthy" if True;

# - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages)
#   returns $value-if-true if the condition is true and $value-if-false
#   if it is false.
#   my $result = $value condition ?? $value-if-true !! $value-if-false;

my $age = 30;
say $age > 18 ?? "You are an adult" !! "You are under 18";
```

### given/when, or switch

```perl6
# - `given`-`when` looks like other languages' `switch`, but is much more
# powerful thanks to smart matching and Perl 6's "topic variable", $_.
#
# This variable contains the default argument of a block,
#  a loop's current iteration (unless explicitly named), etc.
#
# `given` simply puts its argument into `$_` (like a block would do),
#  and `when` compares it using the "smart matching" (`~~`) operator.
#
# Since other Perl 6 constructs use this variable (as said before, like `for`,
# blocks, etc), this means the powerful `when` is not only applicable along with
# a `given`, but instead anywhere a `$_` exists.

given "foo bar" {
  say $_; #=> foo bar
  when /foo/ { # Don't worry about smart matching yet – just know `when` uses it
               # This is equivalent to `if $_ ~~ /foo/`.
    say "Yay !";
  }
  when $_.chars > 50 { # smart matching anything with True is True,
                       # i.e. (`$a ~~ True`) 
                       # so you can also put "normal" conditionals.
                       # This `when` is equivalent to this `if`:
                       #  if $_ ~~ ($_.chars > 50) {...}
                       # Which means:
                       #  if $_.chars > 50 {...}
    say "Quite a long string !";
  }
  default { # same as `when *` (using the Whatever Star)
    say "Something else"
  }
}
```

### Looping constructs

```perl6
# - `loop` is an infinite loop if you don't pass it arguments,
# but can also be a C-style `for` loop:
loop {
  say "This is an infinite loop !";
  last; # last breaks out of the loop, like the `break` keyword in other
        # languages
}

loop (my $i = 0; $i < 5; $i++) {
  next if $i == 3; # `next` skips to the next iteration, like `continue`
                   # in other languages. Note that you can also use postfix
                   # conditionals, loops, etc.
  say "This is a C-style for loop !";
}

# - `for` - Passes through an array
for @array -> $variable {
  say "I've got $variable !";
}

# As we saw with given, for's default "current iteration" variable is `$_`.
# That means you can use `when` in a `for` just like you were in a `given`.
for @array {
  say "I've got $_";

  .say; # This is also allowed.
        # A dot call with no "topic" (receiver) is sent to `$_` by default
  $_.say; # the above and this are equivalent.
}

for @array {
  # You can...
  next if $_ == 3; # Skip to the next iteration (`continue` in C-like languages).
  redo if $_ == 4; # Re-do the iteration, keeping the same topic variable (`$_`).
  last if $_ == 5; # Or break out of a loop (like `break` in C-like languages).
}

# The "pointy block" syntax isn't specific to for.
# It's just a way to express a block in Perl6.
if long-computation() -> $result {
  say "The result is $result";
}
```

## Operators

```perl6
## Since Perl languages are very much operator-based languages,
## Perl 6 operators are actually just funny-looking subroutines, in syntactic
##  categories, like infix:<+> (addition) or prefix:<!> (bool not).

## The categories are:
# - "prefix": before (like `!` in `!True`).
# - "postfix": after (like `++` in `$a++`).
# - "infix": in between (like `*` in `4 * 3`).
# - "circumfix": around (like `[`-`]` in `[1, 2]`).
# - "post-circumfix": around, after another term (like `{`-`}` in `%hash{'key'}`)

## The associativity and precedence list are explained below.

# Alright, you're set to go !

## * Equality Checking

# - `==` is numeric comparison
3 == 4; # False
3 != 4; # True

# - `eq` is string comparison
'a' eq 'b';
'a' ne 'b'; # not equal
'a' !eq 'b'; # same as above

# - `eqv` is canonical equivalence (or "deep equality")
(1, 2) eqv (1, 3);

# - Smart Match Operator: `~~`
# Aliases the left hand side to $_ and then evaluates the right hand side.
# Here are some common comparison semantics:

# String or Numeric Equality

'Foo' ~~ 'Foo'; # True if strings are equal.
12.5 ~~ 12.50; # True if numbers are equal.

# Regex - For matching a regular expression against the left side.
# Returns a (Match) object, which evaluates as True if regexp matches.

my $obj = 'abc' ~~ /a/;
say $obj; # ｢a｣
say $obj.WHAT; # (Match)

# Hashes
'key' ~~ %hash; # True if key exists in hash

# Type - Checks if left side "has type" (can check superclasses and roles)

1 ~~ Int; # True

# Smart-matching against a boolean always returns that boolean (and will warn).

1 ~~ True; # True
False ~~ True; # True

# # General syntax is $arg ~~ &bool-returning-function;
# For a complete list of combinations, use this table:
# http://perlcabal.org/syn/S03.html#Smart_matching

# You also, of course, have `<`, `<=`, `>`, `>=`.
# Their string equivalent are also available : `lt`, `le`, `gt`, `ge`.
3 > 4;

## * Range constructors
3 .. 7; # 3 to 7, both included
# `^` on either side them exclusive on that side :
3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`)
# This also works as a shortcut for `0..^N`:
^10; # means 0..^10

# This also allows us to demonstrate that Perl 6 has lazy/infinite arrays,
#  using the Whatever Star:
my @array = 1..*; # 1 to Infinite ! `1..Inf` is the same.
say @array[^10]; # you can pass arrays as subscripts and it'll return
                 #  an array of results. This will print
                 # "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !)
# Note : when reading an infinite list, Perl 6 will "reify" the elements
# it needs, then keep them in memory. They won't be calculated more than once.
# It also will never calculate more elements that are needed.
# Trying 

# An array subscript can also be a closure.
# It'll be called with the length as the argument
say join(' ', @array[15..*]); #=> 15 16 17 18 19
# which is equivalent to:
say join(' ', @array[-> $n { 15..$n }]);
# Note: if you try to do either of those with an infinite array,
#       you'll trigger an infinite loop (your program won't finish)

# You can use that in most places you'd expect, even assigning to an array
my @numbers = ^20;

# Here numbers increase by "6"; more on `...` operator later.
my @seq =  3, 9 ... * > 95; # 3 9 15 21 27 [...] 81 87 93 99;
@numbers[5..*] = 3, 9 ... *; # even though the sequence is infinite,
                             # only the 15 needed values will be calculated.
say @numbers; #=> 0 1 2 3 4 3 9 15 21 [...] 81 87
              # (only 20 values)

## * And &&, Or ||
3 && 4; # 4, which is Truthy. Calls `.Bool` on `4` and gets `True`.
0 || False; # False. Calls `.Bool` on `0`

## * Short-circuit (and tight) versions of the above
#    Returns the first argument that evaluates to False, or the last argument.

my ( $a, $b, $c ) = 1, 0, 2;
$a && $b && $c; # Returns 0, the first False value

# || Returns the first argument that evaluates to True
$b || $a; # 1

# And because you're going to want them,
#  you also have compound assignment operators:
$a *= 2; # multiply and assignment. Equivalent to $a = $a * 2;
$b %%= 5; # divisible by and assignment
@array .= sort; # calls the `sort` method and assigns the result back
```

## More on subs !

```perl6
# As we said before, Perl 6 has *really* powerful subs. We're going to see
# a few more key concepts that make them better than in any other language :-).
```

### Unpacking !

```perl6
# It's the ability to "extract" arrays and keys (AKA "destructuring").
# It'll work in `my`s and in parameter lists.
my ($f, $g) = 1, 2;
say $f; #=> 1
my ($, $, $h) = 1, 2, 3; # keep the non-interesting anonymous
say $h; #=> 3

my ($head, *@tail) = 1, 2, 3; # Yes, it's the same as with "slurpy subs"
my (*@small) = 1;

sub unpack_array(@array [$fst, $snd]) {
  say "My first is $fst, my second is $snd ! All in all, I'm @array[].";
  # (^ remember the `[]` to interpolate the array)
}
unpack_array(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 2 3


# If you're not using the array itself, you can also keep it anonymous,
#  much like a scalar:
sub first-of-array(@ [$fst]) { $fst }
first-of-array(@small); #=> 1
first-of-array(@tail); # Throws an error "Too many positional parameters passed"
                       # (which means the array is too big).

# You can also use a slurp ...
sub slurp-in-array(@ [$fst, *@rest]) { # You could keep `*@rest` anonymous
  say $fst + @rest.elems; # `.elems` returns a list's length.
                          # Here, `@rest` is `(3,)`, since `$fst` holds the `2`.
}
slurp-in-array(@tail); #=> 3

# You could even extract on a slurpy (but it's pretty useless ;-).)
sub fst(*@ [$fst]) { # or simply : `sub fst($fst) { ... }`
  say $fst;
}
fst(1); #=> 1
fst(1, 2); # errors with "Too many positional parameters passed"

# You can also destructure hashes (and classes, which you'll learn about later !)
# The syntax is basically `%hash-name (:key($variable-to-store-value-in))`.
# The hash can stay anonymous if you only need the values you extracted.
sub key-of(% (:value($val), :qua($qua))) {
  say "Got val $val, $qua times.";
}

# Then call it with a hash: (you need to keep the brackets for it to be a hash)
key-of({value => 'foo', qua => 1});
#key-of(%hash); # the same (for an equivalent `%hash`)

## The last expression of a sub is returned automatically
# (though you may use the `return` keyword, of course):
sub next-index($n) {
  $n + 1;
}
my $new-n = next-index(3); # $new-n is now 4

# This is true for everything, except for the looping constructs
# (due to performance reasons): there's reason to build a list
#  if we're just going to discard all the results.
# If you still want to build one, you can use the `do` statement prefix:
#  (or the `gather` prefix, which we'll see later)
sub list-of($n) {
  do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`)
    $_ # current loop iteration
  }
}
my @list3 = list-of(3); #=> (0, 1, 2)
```

### lambdas

```perl6
## You can create a lambda with `-> {}` ("pointy block") or `{}` ("block")
my &lambda = -> $argument { "The argument passed to this lambda is $argument" }
# `-> {}` and `{}` are pretty much the same thing, except that the former can
# take arguments, and that the latter can be mistaken as a hash by the parser.

# We can, for example, add 3 to each value of an array using map:
my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument

# A sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`):
# A block doesn't have a "function context" (though it can have arguments),
#  which means that if you return from it,
#  you're going to return from the parent function. Compare:
sub is-in(@array, $elem) {
  # this will `return` out of the `is-in` sub
  # once the condition evaluated to True, the loop won't be run anymore
  map({ return True if $_ == $elem }, @array);
}
sub truthy-array(@array) {
  # this will produce an array of `True` and `False`:
  # (you can also say `anon sub` for "anonymous subroutine")
  map(sub ($i) { if $i { return True } else { return False } }, @array);
  # ^ the `return` only returns from the anonymous `sub`
}

# You can also use the "whatever star" to create an anonymous function
# (it'll stop at the furthest operator in the current expression)
my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }`
my @arrayplus3 = map(*+*+3, @array); # Same as `-> $a, $b { $a + $b + 3 }`
                                     # also `sub ($a, $b) { $a + $b + 3 }`
say (*/2)(4); #=> 2
              # Immediately execute the function Whatever created.
say ((*+3)/5)(5); #=> 1.6
                  # works even in parens !

# But if you need to have more than one argument (`$_`)
#  in a block (without wanting to resort to `-> {}`),
#  you can also use the implicit argument syntax, `$^` :
map({ $^a + $^b + 3 }, @array); # equivalent to following:
map(sub ($a, $b) { $a + $b + 3 }, @array); # (here with `sub`)

# Note : those are sorted lexicographically.
# `{ $^b / $^a }` is like `-> $a, $b { $b / $a }`
```

### About types...

```perl6
# Perl6 is gradually typed. This means you can specify the type
#  of your variables/arguments/return types, or you can omit them
#  and they'll default to "Any".
# You obviously get access to a few base types, like Int and Str.
# The constructs for declaring types are "class", "role",
#  which you'll see later.

# For now, let us examine "subset":
# a "subset" is a "sub-type" with additional checks.
# For example: "a very big integer is an Int that's greater than 500"
# You can specify the type you're subtyping (by default, Any),
#  and add additional checks with the "where" keyword:
subset VeryBigInteger of Int where * > 500;
```

### Multiple Dispatch

```perl6
# Perl 6 can decide which variant of a `sub` to call based on the type of the
# arguments, or on arbitrary preconditions, like with a type or a `where`:

# with types
multi sub sayit(Int $n) { # note the `multi` keyword here
  say "Number: $n";
}
multi sayit(Str $s) { # a multi is a `sub` by default
  say "String: $s";
}
sayit("foo"); # prints "String: foo"
sayit(True); # fails at *compile time* with
             # "calling 'sayit' will never work with arguments of types ..."

# with arbitrary precondition (remember subsets?):
multi is-big(Int $n where * > 50) { "Yes !" } # using a closure
multi is-big(Int $ where 10..50) { "Quite." } # Using smart-matching
                                              # (could use a regexp, etc)
multi is-big(Int $) { "No" }

subset Even of Int where * %% 2;

multi odd-or-even(Even) { "Even" } # The main case using the type.
                                   # We don't name the argument.
multi odd-or-even($) { "Odd" } # "else"

# You can even dispatch based on a positional's argument presence !
multi with-or-without-you(:$with!) { # You need make it mandatory to
                                     # be able to dispatch against it.
  say "I can live ! Actually, I can't.";
}
multi with-or-without-you {
  say "Definitely can't live.";
}
# This is very, very useful for many purposes, like `MAIN` subs (covered later),
#  and even the language itself is using it in several places.
#
# - `is`, for example, is actually a `multi sub` named `trait_mod:<is>`,
#  and it works off that.
# - `is rw`, is simply a dispatch to a function with this signature:
# sub trait_mod:<is>(Routine $r, :$rw!) {}
#
# (commented because running this would be a terrible idea !)
```

## Scoping

```perl6
# In Perl 6, unlike many scripting languages, (such as Python, Ruby, PHP),
# you must declare your variables before using them. The `my` declarator
# you have learned uses "lexical scoping". There are a few other declarators,
# (`our`, `state`, ..., ) which we'll see later.
# This is called "lexical scoping", where in inner blocks,
#  you can access variables from outer blocks.
my $file_scoped = 'Foo';
sub outer {
  my $outer_scoped = 'Bar';
  sub inner {
    say "$file_scoped $outer_scoped";
  }
  &inner; # return the function
}
outer()(); #=> 'Foo Bar'

# As you can see, `$file_scoped` and `$outer_scoped` were captured.
# But if we were to try and use `$bar` outside of `foo`,
# the variable would be undefined (and you'd get a compile time error).
```

## Twigils

```perl6
# There are many special `twigils` (composed sigil's) in Perl 6.
# Twigils define the variables' scope.
# The * and ? twigils work on standard variables:
# * Dynamic variable
# ? Compile-time variable
# The ! and the . twigils are used with Perl 6's objects:
# ! Attribute (class member)
# . Method (not really a variable)

# `*` Twigil: Dynamic Scope
# These variables use the`*` twigil to mark dynamically-scoped variables.
# Dynamically-scoped variables are looked up through the caller, not through
# the outer scope

my $*dyn_scoped_1 = 1;
my $*dyn_scoped_2 = 10;

sub say_dyn {
  say "$*dyn_scoped_1 $*dyn_scoped_2";
}

sub call_say_dyn {
  my $*dyn_scoped_1 = 25; # Defines $*dyn_scoped_1 only for this sub.
  $*dyn_scoped_2 = 100; # Will change the value of the file scoped variable.
  say_dyn(); #=> 25 100 $*dyn_scoped 1 and 2 will be looked for in the call.
             # It uses the value of $*dyn_scoped_1 from inside this sub's lexical
             # scope even though the blocks aren't nested (they're call-nested).
}
say_dyn(); #=> 1 10
call_say_dyn(); #=> 25 100
                # Uses $*dyn_scoped_1 as defined in call_say_dyn even though
                # we are calling it from outside.
say_dyn(); #=> 1 100 We changed the value of $*dyn_scoped_2 in call_say_dyn
           #         so now its value has changed.
```

## Object Model

```perl6
# To call a method on an object, add a dot followed by the method name:
# => $object.method
# Classes are declared with the `class` keyword. Attributes are declared
# with the `has` keyword, and methods declared with `method`.
# Every attribute that is private uses the ! twigil for example: `$!attr`.
# Immutable public attributes use the `.` twigil.
#   (you can make them mutable with `is rw`)
# The easiest way to remember the `$.` twigil is comparing it to how methods
# are called.

# Perl 6's object model ("SixModel") is very flexible,
# and allows you to dynamically add methods, change semantics, etc ...
# (these will not all be covered here, and you should refer to:
# https://docs.perl6.org/language/objects.html.

class Attrib-Class {
  has $.attrib; # `$.attrib` is immutable.
               # From inside the class, use `$!attrib` to modify it.
  has $.other-attrib is rw; # You can mark a public attribute `rw`.
  has Int $!private-attrib = 10;

  method get-value {
    $.attrib + $!private-attrib;
  }

  method set-value($param) { # Methods can take parameters
    $!attrib = $param;   # This works, because `$!` is always mutable.
	# $.attrib = $param; # Wrong: You can't use the `$.` immutable version.

    $.other-attrib = 5; # This works, because `$.other-attrib` is `rw`.
  }

  method !private-method {
    say "This method is private to the class !";
  }
};

# Create a new instance of Attrib-Class with $.attrib set to 5 :
# Note: you can't set private-attribute from here (more later on).
my $class-obj = Attrib-Class.new(attrib => 5);
say $class-obj.get-value; #=> 15
#$class-obj.attrib = 5; # This fails, because the `has $.attrib` is immutable
$class-obj.other-attrib = 10; # This, however, works, because the public
                              # attribute is mutable (`rw`).
```

### Object Inheritance

```perl6
#  Perl 6 also has inheritance (along with multiple inheritance)
#  While `method`'s are inherited, `submethod`'s are not.
#  Submethods are useful for object construction and destruction tasks,
#  such as BUILD, or methods that must be overridden by subtypes.
#  We will learn about BUILD later on.

class Parent {
  has $.age;
  has $.name;
  # This submethod won't be inherited by Child.
  submethod favorite-color {
    say "My favorite color is Blue";
  }
  # This method is inherited
  method talk { say "Hi, my name is $!name" }
}
# Inheritance uses the `is` keyword
class Child is Parent {
  method talk { say "Goo goo ga ga" }
  # This shadows Parent's `talk` method, This child hasn't learned to speak yet!
}
my Parent $Richard .= new(age => 40, name => 'Richard');
$Richard.favorite-color; #=> "My favorite color is Blue"
$Richard.talk; #=> "Hi, my name is Richard"
# # $Richard is able to access the submethod, he knows how to say his name.

my Child $Madison .= new(age => 1, name => 'Madison');
$Madison.talk; # prints "Goo goo ga ga" due to the overridden method.
# $Madison.favorite-color does not work since it is not inherited

# When you use `my T $var`, `$var` starts off with `T` itself in it,
# so you can call `new` on it.
# (`.=` is just the dot-call and the assignment operator:
#  `$a .= b` is the same as `$a = $a.b`)
# Also note that `BUILD` (the method called inside `new`)
#  will set parent properties too, so you can pass `val => 5`.
```

### Roles, or Mixins

```perl6
# Roles are supported too (also called Mixins in other languages)
role PrintableVal {
  has $!counter = 0;
  method print {
    say $.val;
  }
}

# you "import" a mixin (a "role") with "does":
class Item does PrintableVal {
  has $.val;

  # When `does`-ed, a `role` literally "mixes in" the class:
  #  the methods and attributes are put together, which means a class can access
  #  the private attributes/methods of its roles (but not the inverse !):
  method access {
    say $!counter++;
  }

  # However, this:
  # method print {}
  # is ONLY valid when `print` isn't a `multi` with the same dispatch.
  # (this means a parent class can shadow a child class's `multi print() {}`,
  #  but it's an error if a role does)

  # NOTE: You can use a role as a class (with `is ROLE`). In this case, methods
  # will be shadowed, since the compiler will consider `ROLE` to be a class.
}
```

## Exceptions

```perl6
# Exceptions are built on top of classes, in the package `X` (like `X::IO`).
# In Perl6 exceptions are automatically 'thrown'
open 'foo'; #> Failed to open file foo: no such file or directory
# It will also print out what line the error was thrown at and other error info

# You can throw an exception using `die`:
die 'Error!'; #=> Error!
# Or more explicitly:
die X::AdHoc.new(payload => 'Error!');

# In Perl 6, `orelse` is similar to the `or` operator, except it only matches
# undefined variables instead of anything evaluating as false.
# Undefined values include: `Nil`, `Mu` and `Failure` as well as `Int`, `Str`
# and other types that have not been initialized to any value yet.
# You can check if something is defined or not using the defined method:
my $uninitialized;
say $uninitiazilzed.defined; #> False
# When using `orelse` it will disarm the exception and alias $_ to that failure
# This will avoid it being automatically handled and printing lots of scary
# error messages to the screen.
# We can use the exception method on $_ to access the exception
open 'foo' orelse say "Something happened {.exception}";
# This also works:
open 'foo' orelse say "Something happened $_"; #> Something happened
              #>  Failed to open file foo: no such file or directory
# Both of those above work but in case we get an object from the left side that
# is not a failure we will probably get a warning.  We see below how we can use
# `try` and `CATCH` to be more specific with the exceptions we catch.
```

### Using `try` and `CATCH`

```perl6
# By using `try` and `CATCH` you can contain and handle exceptions without
# disrupting the rest of the program. `try` will set the last exception to
# the special variable `$!` Note: This has no relation to $!variables.
try open 'foo';
say "Well, I tried! $!" if defined $!; #> Well, I tried! Failed to open file
                                       #foo: no such file or directory
# Now, what if we want more control over handling the exception?
# Unlike many other languages, in Perl 6, you put the `CATCH` block *within*
# the block to `try`. Similar to how $_ was set when we 'disarmed' the
# exception with orelse, we also use $_ in the CATCH block.
# Note: ($! is only set *after* the `try` block)
# By default, a `try` has a `CATCH` block that catches
# any exception (`CATCH { default {} }`).

try { my $a = (0 %% 0);  CATCH { say "Something happened: $_" } }
 #=> Something happened: Attempt to divide by zero using infix:<%%>

# You can redefine it using `when`s (and `default`)
# to handle the exceptions you want:
try {
  open 'foo';
  CATCH {     # In the `CATCH` block, the exception is set to $_
    when X::AdHoc { say "Error: $_" }
     #=>Error: Failed to open file /dir/foo: no such file or directory

    # Any other exception will be re-raised, since we don't have a `default`
    # Basically, if a `when` matches (or there's a `default`) marks the
	# exception as
    # "handled" so that it doesn't get re-thrown from the `CATCH`.
    # You still can re-throw the exception (see below) by hand.
  }
}

# There are also some subtleties to exceptions. Some Perl 6 subs return a
# `Failure`, which is a kind of "unthrown exception". They're not thrown until
# you tried to look at their content, unless you call `.Bool`/`.defined` on
# them - then they're handled.
# (the `.handled` method is `rw`, so you can mark it as `False` back yourself)
#
# You can throw a `Failure` using `fail`. Note that if the pragma `use fatal`
# is on, `fail` will throw an exception (like `die`).
fail "foo"; # We're not trying to access the value, so no problem.
try {
  fail "foo";
  CATCH {
    default { say "It threw because we tried to get the fail's value!" }
  }
}

# There is also another kind of exception: Control exceptions.
# Those are "good" exceptions, which happen when you change your program's flow,
#  using operators like `return`, `next` or `last`.
# You can "catch" those with `CONTROL` (not 100% working in Rakudo yet).
```

## Packages

```perl6
# Packages are a way to reuse code. Packages are like "namespaces", and any
#  element of the six model (`module`, `role`, `class`, `grammar`, `subset`
#  and `enum`) are actually packages. (Packages are the lowest common denominator)
# Packages are important - especially as Perl is well-known for CPAN,
#  the Comprehensive Perl Archive Network.

# You can use a module (bring its declarations into scope) with `use`
use JSON::Tiny; # if you installed Rakudo* or Panda, you'll have this module
say from-json('[1]').perl; #=> [1]

# You should not declare packages using the `package` keyword (unlike Perl 5).
# Instead, use `class Package::Name::Here;` to declare a class, or if you only want to
#  export variables/subs, you can use `module`.

module Hello::World { # Bracketed form
                      # If `Hello` doesn't exist yet, it'll just be a "stub",
                      #  that can be redeclared as something else later.
  # ... declarations here ...
}
unit module Parse::Text; # file-scoped form

grammar Parse::Text::Grammar { # A grammar is a package, which you could `use`
}                    # You will learn more about grammars in the regex section

# As said before, any part of the six model is also a package.
# Since `JSON::Tiny` uses (its own) `JSON::Tiny::Actions` class, you can use it:
my $actions = JSON::Tiny::Actions.new;

# We'll see how to export variables and subs in the next part:
```

## Declarators

```perl6
# In Perl 6, you get different behaviors based on how you declare a variable.
# You've already seen `my` and `has`, we'll now explore the others.

## * `our` declarations happen at `INIT` time -- (see "Phasers" below)
# It's like `my`, but it also creates a package variable.
# (All packagish things (`class`, `role`, etc) are `our` by default)
module Var::Increment {
  our $our-var = 1; # Note: you can't put a type constraint like Int on an
  my $my-var = 22;  # `our` variable.
  our sub Inc {

    our sub available { # If you try to make inner `sub`s `our`...
                        # Better know what you're doing (Don't !).
      say "Don't do that. Seriously. You'll get burned.";
    }

    my sub unavailable { # `my sub` is the default
      say "Can't access me from outside, I'm 'my'!";
    }
    say ++$our-var; # Increment the package variable and output its value
  }

}
say $Var::Increment::our-var; #=> 1 This works
say $Var::Increment::my-var; #=> (Any) This will not work.

Var::Increment::Inc; #=> 2
Var::Increment::Inc; #=> 3 # Notice how the value of $our-var was
                         # retained.
Var::Increment::unavailable; #> Could not find symbol '&unavailable'

## * `constant` (happens at `BEGIN` time)
# You can use the `constant` keyword to declare a compile-time variable/symbol:
constant Pi = 3.14;
constant $var = 1;

# And if you're wondering, yes, it can also contain infinite lists.
constant why-not = 5, 15 ... *;
say why-not[^5]; #=> 5 15 25 35 45

## * `state` (happens at run time, but only once)
# State variables are only initialized one time
# (they exist in other languages such as C as `static`)
sub fixed-rand {
  state $val = rand;
  say $val;
}
fixed-rand for ^10; # will print the same number 10 times

# Note, however, that they exist separately in different enclosing contexts.
# If you declare a function with a `state` within a loop, it'll re-create the
#  variable for each iteration of the loop. See:
for ^5 -> $a {
  sub foo {
    state $val = rand; # This will be a different value for every value of `$a`
  }
  for ^5 -> $b {
    say foo; # This will print the same value 5 times, but only 5.
             # Next iteration will re-run `rand`.
  }
}
```

## Phasers

```perl6
# Phasers in Perl 6 are blocks that happen at determined points of time in your
# program.  They are called phasers because they mark a change in the phase
# of a program.  For example, when the program is compiled, a for loop runs,
# you leave a block, or an exception gets thrown. (`CATCH` is actually a phaser !)
# Some of them can be used for their return values, some of them can't
# (those that can have a "[*]" in the beginning of their explanation text).
# Let's have a look !

## * Compile-time phasers
BEGIN { say "[*] Runs at compile time, as soon as possible, only once" }
CHECK { say "[*] Runs at compile time, as late as possible, only once" }

## * Run-time phasers
INIT { say "[*] Runs at run time, as soon as possible, only once" }
END { say "Runs at run time, as late as possible, only once" }

## * Block phasers
ENTER { say "[*] Runs everytime you enter a block, repeats on loop blocks" }
LEAVE { say "Runs everytime you leave a block, even when an exception
    happened. Repeats on loop blocks." }

PRE {
    say "Asserts a precondition at every block entry,
        before ENTER (especially useful for loops)";
    say "If this block doesn't return a truthy value,
        an exception of type X::Phaser::PrePost is thrown.";
}
# example:
for 0..2 {
    PRE { $_ > 1 } # This is going to blow up with "Precondition failed"
}

POST {
    say "Asserts a postcondition at every block exit,
        after LEAVE (especially useful for loops)";
    say "If this block doesn't return a truthy value,
        an exception of type X::Phaser::PrePost is thrown, like PRE.";
}
for 0..2 {
    POST { $_ < 2 } # This is going to blow up with "Postcondition failed"
}

## * Block/exceptions phasers
sub {
    KEEP { say "Runs when you exit a block successfully (without throwing an exception)" }
    UNDO { say "Runs when you exit a block unsuccessfully (by throwing an exception)" }
}

## * Loop phasers
for ^5 {
  FIRST { say "[*] The first time the loop is run, before ENTER" }
  NEXT { say "At loop continuation time, before LEAVE" }
  LAST { say "At loop termination time, after LEAVE" }
}

## * Role/class phasers
COMPOSE { "When a role is composed into a class. /!\ NOT YET IMPLEMENTED" }

# They allow for cute tricks or clever code ...:
say "This code took " ~ (time - CHECK time) ~ "s to compile";

# ... or clever organization:
sub do-db-stuff {
  $db.start-transaction; # start a new transaction
  KEEP $db.commit; # commit the transaction if all went well
  UNDO $db.rollback; # or rollback if all hell broke loose
}
```

## Statement prefixes

```perl6
# Those act a bit like phasers: they affect the behavior of the following code.
# Though, they run in-line with the executable code, so they're in lowercase.
# (`try` and `start` are theoretically in that list, but explained somewhere else)
# Note: all of these (except start) don't need explicit brackets `{` and `}`.

# - `do` (that you already saw) - runs a block or a statement as a term
# You can't normally use a statement as a value (or "term"):
#
#    my $value = if True { 1 } # `if` is a statement - parse error
#
# This works:
my $a = do if True { 5 } # with `do`, `if` is now a term.

# - `once` - Makes sure a piece of code only runs once
for ^5 { once say 1 }; #=> 1
                       # Only prints ... once.
# Like `state`, they're cloned per-scope
for ^5 { sub { once say 1 }() } #=> 1 1 1 1 1
                                # Prints once per lexical scope

# - `gather` - Co-routine thread
# Gather allows you to `take` several values in an array,
#  much like `do`, but allows you to take any expression.
say gather for ^5 {
  take $_ * 3 - 1;
  take $_ * 3 + 1;
} #=> -1 1 2 4 5 7 8 10 11 13
say join ',', gather if False {
  take 1;
  take 2;
  take 3;
} # Doesn't print anything.

# - `eager` - Evaluate statement eagerly (forces eager context)
# Don't try this at home:
#
#    eager 1..*; # this will probably hang for a while (and might crash ...).
#
# But consider:
constant thrice = gather for ^3 { say take $_ }; # Doesn't print anything
# versus:
constant thrice = eager gather for ^3 { say take $_ }; #=> 0 1 2
```

## Iterables

```perl6
# Iterables are objects that can be iterated similar to the `for` construct
# `flat`, flattens iterables:
say (1, 10, (20, 10) ); #> (1 10 (20 10)) Notice how grouping is maintained
say (1, 10, (20, 10) ).flat; #> (1 10 20 10) Now the iterable is flat

# - `lazy` - Defer actual evaluation until value is fetched (forces lazy context)
my @lazy-array = (1..100).lazy;
say @lazy-array.is-lazy; #> True # Check for laziness with the `is-lazy` method.
say @lazy-array; #> [...] List has not been iterated on!
my @lazy-array { .print }; # This works and will only do as much work as is
# needed.
[//]: # ( TODO explain that gather/take and map are all lazy)
# - `sink` - An `eager` that discards the results (forces sink context)
constant nilthingie = sink for ^3 { .say } #=> 0 1 2
say nilthingie.perl; #=> Nil

# - `quietly` blocks will suppress warnings:
quietly { warn 'This is a warning!' }; #=> No output

# - `contend` - Attempts side effects under STM
# Not yet implemented !
```

## More operators thingies !

```perl6
## Everybody loves operators ! Let's get more of them

# The precedence list can be found here:
# https://docs.perl6.org/language/operators#Operator_Precedence
# But first, we need a little explanation about associativity:

# * Binary operators:
$a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c`
$a ! $b ! $c; # with a right-associative `!`, this is `$a ! ($b ! $c)`
$a ! $b ! $c; # with a non-associative `!`, this is illegal
$a ! $b ! $c; # with a chain-associative `!`, this is `($a ! $b) and ($b ! $c)`
$a ! $b ! $c; # with a list-associative `!`, this is `infix:<>`

# * Unary operators:
!$a! # with left-associative `!`, this is `(!$a)!`
!$a! # with right-associative `!`, this is `!($a!)`
!$a! # with non-associative `!`, this is illegal
```

### Create your own operators !

```perl6
# Okay, you've been reading all of that, so I guess I should try
#  to show you something exciting.
# I'll tell you a little secret (or not-so-secret):
# In Perl 6, all operators are actually just funny-looking subroutines.

# You can declare an operator just like you declare a sub:
sub prefix:<win>($winner) { # refer to the operator categories
                            # (yes, it's the "words operator" `<>`)
  say "$winner Won !";
}
win "The King"; #=> The King Won !
                # (prefix is before)

# you can still call the sub with its "full name"
say prefix:<!>(True); #=> False

sub postfix:<!>(Int $n) {
  [*] 2..$n; # using the reduce meta-operator ... See below ;-) !
}
say 5!; #=> 120
        # Postfix operators (after) have to come *directly* after the term.
        # No whitespace. You can use parentheses to disambiguate, i.e. `(5!)!`


sub infix:<times>(Int $n, Block $r) { # infix in the middle
  for ^$n {
    $r(); # You need the explicit parentheses to call the function in `$r`,
          #  else you'd be referring at the variable itself, like with `&r`.
  }
}
3 times -> { say "hello" }; #=> hello
                            #=> hello
                            #=> hello
                            # You're very recommended to put spaces
                            # around your infix operator calls.

# For circumfix and post-circumfix ones
sub circumfix:<[ ]>(Int $n) {
  $n ** $n
}
say [5]; #=> 3125
         # circumfix is around. Again, no whitespace.

sub postcircumfix:<{ }>(Str $s, Int $idx) {
  # post-circumfix is
  # "after a term, around something"
  $s.substr($idx, 1);
}
say "abc"{1}; #=> b
              # after the term `"abc"`, and around the index (1)

# This really means a lot -- because everything in Perl 6 uses this.
# For example, to delete a key from a hash, you use the `:delete` adverb
#  (a simple named argument underneath):
%h{$key}:delete;
# equivalent to:
postcircumfix:<{ }>(%h, $key, :delete); # (you can call operators like that)
# It's *all* using the same building blocks!
# Syntactic categories (prefix infix ...), named arguments (adverbs), ...,
#  - used to build the language - are available to you.

# (you are, obviously, recommended against making an operator out of
# *everything* -- with great power comes great responsibility)
```

### Meta operators !

```perl6
# Oh boy, get ready. Get ready, because we're delving deep
#  into the rabbit's hole, and you probably won't want to go
#  back to other languages after reading that.
#  (I'm guessing you don't want to already at that point).
# Meta-operators, as their name suggests, are *composed* operators.
# Basically, they're operators that apply another operator.

## * Reduce meta-operator
# It's a prefix meta-operator that takes a binary function and
#  one or many lists. If it doesn't get passed any argument,
#  it either returns a "default value" for this operator
#  (a meaningless value) or `Any` if there's none (examples below).
#
# Otherwise, it pops an element from the list(s) one at a time, and applies
#  the binary function to the last result (or the list's first element)
#  and the popped element.
#
# To sum a list, you could use the reduce meta-operator with `+`, i.e.:
say [+] 1, 2, 3; #=> 6
# equivalent to `(1+2)+3`
say [*] 1..5; #=> 120
# equivalent to `((((1*2)*3)*4)*5)`.

# You can reduce with any operator, not just with mathematical ones.
# For example, you could reduce with `//` to get
#  the first defined element of a list:
say [//] Nil, Any, False, 1, 5; #=> False
                                # (Falsey, but still defined)


# Default value examples:
say [*] (); #=> 1
say [+] (); #=> 0
            # meaningless values, since N*1=N and N+0=N.
say [//];   #=> (Any)
            # There's no "default value" for `//`.

# You can also call it with a function you made up, using double brackets:
sub add($a, $b) { $a + $b }
say [[&add]] 1, 2, 3; #=> 6

## * Zip meta-operator
# This one is an infix meta-operator than also can be used as a "normal"
# operator.  It takes an optional binary function (by default, it just creates
# a pair), and will pop one value off of each array and call its binary function
# on these until it runs out of elements. It returns an array with all of these
# new elements.
(1, 2) Z (3, 4); # ((1, 3), (2, 4)), since by default, the function makes an array
1..3 Z+ 4..6; # (5, 7, 9), using the custom infix:<+> function

# Since `Z` is list-associative (see the list above),
#  you can use it on more than one list
(True, False) Z|| (False, False) Z|| (False, False); # (True, False)

# And, as it turns out, you can also use the reduce meta-operator with it:
[Z||] (True, False), (False, False), (False, False); # (True, False)


## And to end the operator list:

## * Sequence operator
# The sequence operator is one of Perl 6's most powerful features:
# it's composed of first, on the left, the list you want Perl 6 to deduce from
#  (and might include a closure), and on the right, a value or the predicate
#  that says when to stop (or Whatever for a lazy infinite list).
my @list = 1, 2, 3 ... 10; # basic deducing
#my @list = 1, 3, 6 ... 10; # this dies because Perl 6 can't figure out the end
my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element
                            # (the iteration when the predicate matches).
my @list = 1, 3, 9 ... * > 30; # you can use a predicate
                               # (with the Whatever Star, here).
my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above)

my @fib = 1, 1, *+* ... *; # lazy infinite list of fibonacci series,
                           #  computed using a closure!
my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above)
my @fib = 1, 1, { $^a + $^b } ... *; #(... also equivalent to the above)
# $a and $b will always take the previous values, meaning here
#  they'll start with $a = 1 and $b = 1 (values we set by hand).
#  then $a = 1 and $b = 2 (result from previous $a+$b), and so on.

say @fib[^10]; #=> 1 1 2 3 5 8 13 21 34 55
               # (using a range as the index)
# Note : as for ranges, once reified, elements aren't re-calculated.
# That's why `@primes[^100]` will take a long time the first time you print
#  it, then be instant.
```

## Regular Expressions

```perl6
# I'm sure a lot of you have been waiting for this one.
# Well, now that you know a good deal of Perl 6 already, we can get started.
# First off, you'll have to forget about "PCRE regexps" (perl-compatible regexps).
#
# IMPORTANT: Don't skip them because you know PCRE. They're different.
# Some things are the same (like `?`, `+`, and `*`),
#  but sometimes the semantics change (`|`).
# Make sure you read carefully, because you might trip over a new behavior.
#
# Perl 6 has many features related to RegExps. After all, Rakudo parses itself.
# We're first going to look at the syntax itself,
#  then talk about grammars (PEG-like), differences between
#  `token`, `regex` and `rule` declarators, and some more.
# Side note: you still have access to PCRE regexps using the `:P5` modifier.
#  (we won't be discussing this in this tutorial, however)
#
# In essence, Perl 6 natively implements PEG ("Parsing Expression Grammars").
# The pecking order for ambiguous parses is determined by a multi-level
#  tie-breaking test:
#  - Longest token matching. `foo\s+` beats `foo` (by 2 or more positions)
#  - Longest literal prefix. `food\w*` beats `foo\w*` (by 1)
#  - Declaration from most-derived to less derived grammars
#     (grammars are actually classes)
#  - Earliest declaration wins
say so 'a' ~~ /a/; #=> True
say so 'a' ~~ / a /; #=> True #  More readable with some spaces!

# In all our examples, we're going to use the smart-matching operator against
#  a regexp. We're converting the result using `so`, but in fact, it's
#  returning a `Match` object. They know how to respond to list indexing,
#  hash indexing, and return the matched string.
# The results of the match are available as `$/` (implicitly lexically-scoped).
# You can also use the capture variables which start at 0:
#    `$0`, `$1', `$2`...
#
# You can also note that `~~` does not perform start/end checking
#  (meaning the regexp can be matched with just one char of the string),
#  we're going to explain later how you can do it.

# In Perl 6, you can have any alphanumeric as a literal,
# everything else has to be escaped, using a backslash or quotes.
say so 'a|b' ~~ / a '|' b /; # `True`. Wouldn't mean the same if `|` wasn't escaped
say so 'a|b' ~~ / a \| b /; # `True`. Another way to escape it.

# The whitespace in a regexp is actually not significant,
#  unless you use the `:s` (`:sigspace`, significant space) adverb.
say so 'a b c' ~~ / a  b  c /; #> `False`. Space is not significant here
say so 'a b c' ~~ /:s a b c /; #> `True`. We added the modifier `:s` here.
# If we use only one space between strings in a regex, Perl 6 will warn us:
say so 'a b c' ~~ / a b c /; #> 'False' #> Space is not significant here; please
# use quotes or :s (:sigspace) modifier (or, to suppress this warning, omit the
# space, or otherwise change the spacing)
# To fix this and make the spaces less ambiguous,  either use at least two
# spaces between strings or use the `:s` adverb.

# As we saw before, we can embed the `:s` inside the slash delimiters, but we can
# also put it outside of them if we specify `m` for 'match':
say so 'a b c' ~~ m:s/a  b  c/; #> `True`
# By using `m` to specify 'match' we can also use delimiters other than slashes:
say so 'abc' ~~ m{a  b  c}; #> `True`
# Use the :i adverb to specify case insensitivity:
say so 'ABC' ~~ m:i{a  b  c}; #> `True`
# It is, however, important as for how modifiers (that you're gonna see just below)
#  are applied ...

## Quantifying - `?`, `+`, `*` and `**`.
# - `?` - 0 or 1
so 'ac' ~~ / a  b  c /; # `False`
so 'ac' ~~ / a  b?  c /; # `True`, the "b" matched 0 times.
so 'abc' ~~ / a  b?  c /; # `True`, the "b" matched 1 time.

# ... As you read just before, whitespace is important because it determines
#  which part of the regexp is the target of the modifier:
so 'def' ~~ / a  b  c? /; # `False`. Only the `c` is optional
so 'def' ~~ / a  b?  c /; # `False`. Whitespace is not significant
so 'def' ~~ / 'abc'? /; # `True`. The whole "abc" group is optional.

# Here (and below) the quantifier applies only to the `b`

# - `+` - 1 or more
so 'ac' ~~ / a  b+  c /; # `False`; `+` wants at least one matching
so 'abc' ~~ / a  b+  c /; # `True`; one is enough
so 'abbbbc' ~~ / a  b+  c /; # `True`, matched 4 "b"s

# - `*` - 0 or more
so 'ac' ~~ / a  b*  c /; # `True`, they're all optional.
so 'abc' ~~ / a  b*  c /; # `True`
so 'abbbbc' ~~ / a  b*  c /; # `True`
so 'aec' ~~ / a  b*  c /; # `False`. "b"(s) are optional, not replaceable.

# - `**` - (Unbound) Quantifier
# If you squint hard enough, you might understand
#  why exponentation is used for quantity.
so 'abc' ~~ / a  b**1  c /; # `True` (exactly one time)
so 'abc' ~~ / a  b**1..3  c /; # `True` (one to three times)
so 'abbbc' ~~ / a  b**1..3  c /; # `True`
so 'abbbbbbc' ~~ / a  b**1..3  c /; # `False` (too much)
so 'abbbbbbc' ~~ / a  b**3..*  c /; # `True` (infinite ranges are okay)

# - `<[]>` - Character classes
# Character classes are the equivalent of PCRE's `[]` classes, but
#  they use a more perl6-ish syntax:
say 'fooa' ~~ / f <[ o a ]>+ /; #=> 'fooa'
# You can use ranges:
say 'aeiou' ~~ / a <[ e..w ]> /; #=> 'ae'
# Just like in normal regexes, if you want to use a special character, escape it
#  (the last one is escaping a space)
say 'he-he !' ~~ / 'he-' <[ a..z \! \  ]> + /; #=> 'he-he !'
# You'll get a warning if you put duplicate names
#  (which has the nice effect of catching the wrote quoting:)
'he he' ~~ / <[ h e ' ' ]> /; # Warns "Repeated characters found in characters class"

# You can also negate them ... (equivalent to `[^]` in PCRE)
so 'foo' ~~ / <-[ f o ]> + /; # False

# ... and compose them: :
so 'foo' ~~ / <[ a..z ] - [ f o ]> + /; # False (any letter except f and o)
so 'foo' ~~ / <-[ a..z ] + [ f o ]> + /; # True (no letter except f and o)
so 'foo!' ~~ / <-[ a..z ] + [ f o ]> + /; # True (the + doesn't replace the left part)
```

### Grouping and capturing

```perl6
# Group: you can group parts of your regexp with `[]`.
# These groups are *not* captured (like PCRE's `(?:)`).
so 'abc' ~~ / a [ b ] c /; # `True`. The grouping does pretty much nothing
so 'foo012012bar' ~~ / foo [ '01' <[0..9]> ] + bar /;
# The previous line returns `True`.
# We match the "012" 1 or more time (the `+` was applied to the group).

# But this does not go far enough, because we can't actually get back what
#  we matched.
# Capture: We can actually *capture* the results of the regexp, using parentheses.
so 'fooABCABCbar' ~~ / foo ( 'A' <[A..Z]> 'C' ) + bar /; # `True`. (using `so` here, `$/` below)

# So, starting with the grouping explanations.
# As we said before, our `Match` object is available as `$/`:
say $/; # Will print some weird stuff (we'll explain) (or "Nil" if nothing matched).

# As we also said before, it has array indexing:
say $/[0]; #=> ｢ABC｣ ｢ABC｣
           # These weird brackets are `Match` objects.
           # Here, we have an array of these.
say $0; # The same as above.

# Our capture is `$0` because it's the first and only one capture in the regexp.
# You might be wondering why it's an array, and the answer is simple:
# Some capture (indexed using `$0`, `$/[0]` or a named one) will be an array
#  IFF it can have more than one element
#  (so, with `*`, `+` and `**` (whatever the operands), but not with `?`).
# Let's use examples to see that:

# Note: We quoted A B C to demonstrate that the whitespace between them isn't significant.
#       If we want the whitespace to *be* significant there, we can use the :sigspace modifier.
so 'fooABCbar' ~~ / foo ( "A" "B" "C" )? bar /; # `True`
say $/[0]; #=> ｢ABC｣
say $0.WHAT; #=> (Match)
             # There can't be more than one, so it's only a single match object.
so 'foobar' ~~ / foo ( "A" "B" "C" )? bar /; #=> True
say $0.WHAT; #=> (Any)
             # This capture did not match, so it's empty
so 'foobar' ~~ / foo ( "A" "B" "C" ) ** 0..1 bar /; # `True`
say $0.WHAT; #=> (Array)
             # A specific quantifier will always capture an Array,
             #  may it be a range or a specific value (even 1).

# The captures are indexed per nesting. This means a group in a group will be nested
#  under its parent group: `$/[0][0]`, for this code:
'hello-~-world' ~~ / ( 'hello' ( <[ \- \~ ]> + ) ) 'world' /;
say $/[0].Str; #=> hello~
say $/[0][0].Str; #=> ~

# This stems from a very simple fact: `$/` does not contain strings, integers or arrays,
#  it only contains match objects. These contain the `.list`, `.hash` and `.Str` methods.
#  (but you can also just use `match<key>` for hash access
#    and `match[idx]` for array access)
say $/[0].list.perl; #=> (Match.new(...),).list
                     # We can see it's a list of Match objects. Those contain
                     # a bunch of infos: where the match started/ended,
                     #    the "ast" (see actions later), etc.
                     # You'll see named capture below with grammars.

## Alternatives - the `or` of regexps
# WARNING: They are DIFFERENT from PCRE regexps.
so 'abc' ~~ / a [ b | y ] c /; # `True`. Either "b" or "y".
so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ...

# The difference between this `|` and the one you're used to is LTM.
# LTM means "Longest Token Matching". This means that the engine will always
#  try to match as much as possible in the strng
'foo' ~~ / fo | foo /; # `foo`, because it's longer.
# To decide which part is the "longest", it first splits the regex in two parts:
# The "declarative prefix" (the part that can be statically analyzed)
#  and the procedural parts.
# Declarative prefixes include alternations (`|`), conjunctions (`&`),
#  sub-rule calls (not yet introduced), literals, characters classes and quantifiers.
# The latter include everything else: back-references, code assertions,
#  and other things that can't traditionnaly be represented by normal regexps.
#
# Then, all the alternatives are tried at once, and the longest wins.
# Examples:
# DECLARATIVE | PROCEDURAL
/ 'foo' \d+     [ <subrule1> || <subrule2> ] /;
# DECLARATIVE (nested groups are not a problem)
/ \s* [ \w & b ] [ c | d ] /;
# However, closures and recursion (of named regexps) are procedural.
# ... There are also more complicated rules, like specificity
#  (literals win over character classes)

# Note: the first-matching `or` still exists, but is now spelled `||`
'foo' ~~ / fo || foo /; # `fo` now.
```

## Extra: the MAIN subroutine

```perl6
# The `MAIN` subroutine is called when you run a Perl 6 file directly.
# It's very powerful, because Perl 6 actually parses the arguments
#  and pass them as such to the sub. It also handles named argument (`--foo`)
#  and will even go as far as to autogenerate a `--help`
sub MAIN($name) { say "Hello, $name !" }
# This produces:
#    $ perl6 cli.pl
#    Usage:
#      t.pl <name>

# And since it's a regular Perl 6 sub, you can haz multi-dispatch:
# (using a "Bool" for the named argument so that we can do `--replace`
#  instead of `--replace=1`)
subset File of Str where *.IO.d; # convert to IO object to check the file exists

multi MAIN('add', $key, $value, Bool :$replace) { ... }
multi MAIN('remove', $key) { ... }
multi MAIN('import', File, Str :$as) { ... } # omitting parameter name
# This produces:
#    $ perl6 cli.pl
#    Usage:
#      t.pl [--replace] add <key> <value>
#      t.pl remove <key>
#      t.pl [--as=<Str>] import (File)
# As you can see, this is *very* powerful.
# It even went as far as to show inline the constants.
# (the type is only displayed if the argument is `$`/is named)
```

## APPENDIX A:
### List of things

```perl6
# It's considered by now you know the Perl6 basics.
# This section is just here to list some common operations,
#  but which are not in the "main part" of the tutorial to bloat it up

## Operators


## * Sort comparison
# They return one value of the `Order` enum : `Less`, `Same` and `More`
#  (which numerify to -1, 0 or +1).
1 <=> 4; # sort comparison for numerics
'a' leg 'b'; # sort comparison for string
$obj eqv $obj2; # sort comparison using eqv semantics

## * Generic ordering
3 before 4; # True
'b' after 'a'; # True

## * Short-circuit default operator
# Like `or` and `||`, but instead returns the first *defined* value :
say Any // Nil // 0 // 5; #=> 0

## * Short-circuit exclusive or (XOR)
# Returns `True` if one (and only one) of its arguments is true
say True ^^ False; #=> True
## * Flip Flop
# The flip flop operators (`ff` and `fff`, equivalent to P5's `..`/`...`).
#  are operators that take two predicates to test:
# They are `False` until their left side returns `True`, then are `True` until
#  their right side returns `True`.
# Like for ranges, you can exclude the iteration when it became `True`/`False`
#  by using `^` on either side.
# Let's start with an example :
for <well met young hero we shall meet later> {
  # by default, `ff`/`fff` smart-match (`~~`) against `$_`:
  if 'met' ^ff 'meet' { # Won't enter the if for "met"
                        #  (explained in details below).
    .say
  }

  if rand == 0 ff rand == 1 { # compare variables other than `$_`
    say "This ... probably will never run ...";
  }
}
# This will print "young hero we shall meet" (excluding "met"):
#  the flip-flop will start returning `True` when it first encounters "met"
#  (but will still return `False` for "met" itself, due to the leading `^`
#   on `ff`), until it sees "meet", which is when it'll start returning `False`.

# The difference between `ff` (awk-style) and `fff` (sed-style) is that
#  `ff` will test its right side right when its left side changes to `True`,
#  and can get back to `False` right away
#  (*except* it'll be `True` for the iteration that matched) -
# While `fff` will wait for the next iteration to
#  try its right side, once its left side changed:
.say if 'B' ff 'B' for <A B C B A>; #=> B B
                                    # because the right-hand-side was tested
                                    # directly (and returned `True`).
                                    # "B"s are printed since it matched that time
                                    #  (it just went back to `False` right away).
.say if 'B' fff 'B' for <A B C B A>; #=> B C B
                                    # The right-hand-side wasn't tested until
                                    #  `$_` became "C"
                                    # (and thus did not match instantly).

# A flip-flop can change state as many times as needed:
for <test start print it stop not printing start print again stop not anymore> {
  .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop",
                                           #=> "print it print again"
}

# you might also use a Whatever Star,
# which is equivalent to `True` for the left side or `False` for the right:
for (1, 3, 60, 3, 40, 60) { # Note: the parenthesis are superfluous here
                            # (sometimes called "superstitious parentheses")
 .say if $_ > 50 ff *; # Once the flip-flop reaches a number greater than 50,
                       #  it'll never go back to `False`
                       #=> 60 3 40 60
}

# You can also use this property to create an `If`
#  that'll not go through the first time :
for <a b c> {
  .say if * ^ff *; # the flip-flop is `True` and never goes back to `False`,
                   #  but the `^` makes it *not run* on the first iteration
                   #=> b c
}


# - `===` is value identity and uses `.WHICH` on the objects to compare them
# - `=:=` is container identity and uses `VAR()` on the objects to compare them

```

If you want to go further, you can:

 - Read the [Perl 6 Docs](https://docs.perl6.org/). This is a great
 resource on Perl6.  If you are looking for something, use the search bar.
 This will give you a dropdown menu of all the pages referencing your search
 term (Much better than using Google to find Perl 6 documents!)
 - Read the [Perl 6 Advent Calendar](http://perl6advent.wordpress.com/). This
 is a great source of Perl 6 snippets and explanations.  If the docs don't
 describe something well enough, you may find more detailed information here.
 This information may be a bit older but there are many great examples and
 explanations.  Posts stopped at the end of 2015 when the language was declared
 stable and Perl 6.c was released.
 - Come along on `#perl6` at `irc.freenode.net`. The folks here are always helpful.
 - Check the [source of Perl 6's functions and classes](https://github.com/rakudo/rakudo/tree/nom/src/core). Rakudo is mainly written in Perl 6 (with a lot of NQP, "Not Quite Perl", a Perl 6 subset easier to implement and optimize).
 - Read [the language design documents](http://design.perl6.org). They explain P6 from an implementor point-of-view, but it's still very interesting.
---
category: tool
tool: composer
contributors:
    - ["Brett Taylor", "https://github.com/glutnix"]
filename: LearnComposer.sh
---

[Composer](https://getcomposer.org/) is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

# Installing

```sh
# Installs the composer.phar binary into the current directory
curl -sS https://getcomposer.org/installer | php
# If you use this approach, you will need to invoke composer like this:
php composer.phar about

# Installs the binary into ~/bin/composer
# Note: make sure ~/bin is in your shell's PATH environment variable
curl -sS https://getcomposer.org/installer | php -- --install-dir=~/bin --filename=composer
```

Windows users should follow the [Windows installation instructions](https://getcomposer.org/doc/00-intro.md#installation-windows)

## Confirming installation

```sh
# Check version and list options
composer

# Get more help for options
composer help require

# Check if Composer is able to do the things it needs, and if it's up to date
composer diagnose
composer diag # shorthand

# Updates the Composer binary to the latest version
composer self-update
composer self # shorthand
```

# Usage

Composer stores your project dependencies in `composer.json`. You can edit this file, but it is best to let Composer manage it for you.

```sh
# Create a new project in the current folder
composer init
# runs an interactive questionnaire asking you for details about your project.  Leaving them blank is fine unless you are making other projects dependent on this one.

# If a composer.json file already exists, download the dependencies
composer install

# To download the just the production dependencies, i.e. excluding development dependencies
composer install --no-dev

# Add a production dependency to this project
composer require guzzlehttp/guzzle
# will figure out what the latest version of guzzlehttp/guzzle is, download it, and add the new dependency to composer.json's require field.

composer require guzzlehttp/guzzle:6.0.*
# will download the latest version matching the pattern (eg. 6.0.2) and add the dependency to composer.json's require field

composer require --dev phpunit/phpunit:~4.5.0
# will require as a development dependency. Will use the latest version >=4.5.0 and < 4.6.0

composer require-dev phpunit/phpunit:^4.5.0
# will require as a development dependency. Will use the latest version >=4.5.0 and < 5.0

# For more information on Composer version matching, see [Composer's documentation on Versions](https://getcomposer.org/doc/articles/versions.md) for more details

# To see what packages are available to install and currently installed
composer show

# To see what packages are currently installed
composer show --installed

# To find a package with 'mailgun' in its name or description
composer search mailgun
```

[Packagist.org](https://packagist.org/) is the main repository for Composer packages. Search there for existing third-party packages.

## `composer.json` vs `composer.lock`

The `composer.json` file stores your project's floating version preferences for each dependency, along with other information.

The `composer.lock` file stores exactly which version it has downloaded for each dependency. Never edit this file.

If you include the `composer.lock` file in your git repository, every developer will install the currently used version of the dependency. Even when a new version of a dependency is released, Composer will continue to download the version recorded in the lock file.

```sh
# If you want to update all the dependencies to their newest version still matching your version preferences
composer update

# If you want the new version of a particular dependency:
composer update phpunit/phpunit

# If you wish to migrate a package to a newer version preference, you may need to remove the older package and its dependencies first.
composer remove --dev phpunit/phpunit
composer require --dev phpunit/phpunit:^5.0

```

## Autoloader

Composer creates an autoloader class you can require from your application. You can make instances of classes via their namespace.

```php
require __DIR__ . '/vendor/autoload.php';

$mailgun = new Mailgun\Mailgun("key");
```

### PSR-4 Autoloader

You can add your own namespaces to the autoloader.

In `composer.json`, add a 'autoload' field:

```json
{
  "autoload": {
    "psr-4": {"Acme\\": "src/"}
  }
}
```
This will tell the autoloader to look for anything in the `\Acme\` namespace within the `src` folder.

You can also [use PSR-0, a Classmap or just a list of files to include](https://getcomposer.org/doc/04-schema.md#autoload). There is also the `autoload-dev` field for development-only namespaces.

When adding or modifying the autoload key, you will need to rebuild the autoloader:

```sh
composer dump-autoload
composer dump # shorthand

# Optimizes PSR0 and PSR4 packages to be loaded with classmaps too. Slow to run, but improves performance on production.
composer dump-autoload --optimize --no-dev
```

# Composer's Cache

```sh
# Composer will retain downloaded packages to use in the future. Clear it with:
composer clear-cache
```

# Troubleshooting

```sh
composer diagnose
composer self-update
composer clear-cache
```

## Topics not (yet) covered in this tutorial

* Creating and distributing your own packages on Packagist.org or elsewhere
* Pre- and post- script hooks: run tasks when certain composer events take place

### References

* [Composer - Dependency Manager for PHP](https://getcomposer.org/)
* [Packagist.org](https://packagist.org/)
---
language: PHP
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
filename: learnphp.php
---

This document describes PHP 5+.

```php
<?php // PHP code must be enclosed with <?php tags

// If your php file only contains PHP code, it is best practice
// to omit the php closing tag to prevent accidental output.

// Two forward slashes start a one-line comment.

# So will a hash (aka pound symbol) but // is more common

/*
     Surrounding text in slash-asterisk and asterisk-slash
     makes it a multi-line comment.
*/

// Use "echo" or "print" to print output
print('Hello '); // Prints "Hello " with no line break

// () are optional for print and echo
echo "World\n"; // Prints "World" with a line break
// (all statements must end with a semicolon)

// Anything outside <?php tags is echoed automatically
?>
Hello World Again!
<?php


/************************************
 * Types & Variables
 */

// Variables begin with the $ symbol.
// A valid variable name starts with a letter or underscore,
// followed by any number of letters, numbers, or underscores.

// Boolean values are case-insensitive
$boolean = true;  // or TRUE or True
$boolean = false; // or FALSE or False

// Integers
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (a leading 0 denotes an octal number)
$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal)
// Binary integer literals are available since PHP 5.4.0.
$int5 = 0b11111111; // 255 (a leading 0b denotes a binary number)

// Floats (aka doubles)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// Delete variable
unset($int1);

// Arithmetic
$sum        = 1 + 1; // 2
$difference = 2 - 1; // 1
$product    = 2 * 2; // 4
$quotient   = 2 / 1; // 2

// Shorthand arithmetic
$number = 0;
$number += 1;      // Increment $number by 1
echo $number++;    // Prints 1 (increments after evaluation)
echo ++$number;    // Prints 3 (increments before evaluation)
$number /= $float; // Divide and assign the quotient to $number

// Strings should be enclosed in single quotes;
$sgl_quotes = '$String'; // => '$String'

// Avoid using double quotes except to embed other variables
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// Special characters are only escaped in double quotes
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// Enclose a variable in curly braces if needed
$apples = "I have {$number} apples to eat.";
$oranges = "I have ${number} oranges to eat.";
$money = "I have $${number} in the bank.";

// Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners
$nowdoc = <<<'END'
Multi line
string
END;

// Heredocs will do string interpolation
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// String concatenation is done with .
echo 'This string ' . 'is concatenated';

// Strings can be passed in as parameters to echo
echo 'Multiple', 'Parameters', 'Valid';  // Returns 'MultipleParametersValid'


/********************************
 * Constants
 */

// A constant is defined by using define()
// and can never be changed during runtime!

// a valid constant name starts with a letter or underscore,
// followed by any number of letters, numbers, or underscores.
define("FOO", "something");

// access to a constant is possible by calling the chosen name without a $
echo FOO; // Returns 'something'
echo 'This outputs ' . FOO;  // Returns 'This outputs something'



/********************************
 * Arrays
 */

// All arrays in PHP are associative arrays (hashmaps in some languages)

// Works with all PHP versions
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);

// PHP 5.4 introduced a new syntax
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];

echo $associative['One']; // prints 1

// Add an element to an associative array
$associative['Four'] = 4;

// List literals implicitly assign integer keys
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"

// Add an element to the end of an array
$array[] = 'Four';
// or
array_push($array, 'Five');

// Remove element from array
unset($array[3]);

/********************************
 * Output
 */

echo('Hello World!');
// Prints Hello World! to stdout.
// Stdout is the web page if running in a browser.

print('Hello World!'); // The same as echo

// echo and print are language constructs too, so you can drop the parentheses
echo 'Hello World!';
print 'Hello World!';

$paragraph = 'paragraph';

echo 100;        // Echo scalar variables directly
echo $paragraph; // or variables

// If short open tags are configured, or your PHP version is
// 5.4.0 or greater, you can use the short echo syntax
?>
<p><?= $paragraph ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $x now contains the same value as $y
$z = &$y;
// $z now contains a reference to $y. Changing the value of
// $z will change the value of $y also, and vice-versa.
// $x will remain unchanged as the original value of $y

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0

// Dumps type and value of variable to stdout
var_dump($z); // prints int(0)

// Prints variable to stdout in human-readable format
print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three )

/********************************
 * Logic
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// assert throws a warning if its argument is not true

// These comparisons will always be true, even if the types aren't the same.
assert($a == $b); // equality
assert($c != $a); // inequality
assert($c <> $a); // alternative inequality
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// The following will only be true if the values match and are the same type.
assert($c === $d);
assert($a !== $d);
assert(1 === '1');
assert(1 !== '1');

// 'Spaceship' operator (since PHP 7)
// Returns 0 if values on either side are equal
// Returns 1 if value on the left is greater
// Returns -1 if the value on the right is greater

$a = 100;
$b = 1000;

echo $a <=> $a; // 0 since they are equal
echo $a <=> $b; // -1 since $a < $b
echo $b <=> $a; // 1 since $b > $a

// Variables can be converted between types, depending on their usage.

$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2 (strings are coerced to integers)

$string = 'one';
echo $string + $string; // => 0
// Outputs 0 because the + operator cannot cast the string 'one' to a number

// Type casting can be used to treat a variable as another type

$boolean = (boolean) 1; // => true

$zero = 0;
$boolean = (boolean) $zero; // => false

// There are also dedicated functions for casting most types
$integer = 5;
$string = strval($integer);

$var = null; // Null value


/********************************
 * Control Structures
 */

if (true) {
    print 'I get printed';
}

if (false) {
    print 'I don\'t';
} else {
    print 'I get printed';
}

if (false) {
    print 'Does not get printed';
} elseif (true) {
    print 'Does';
}

// ternary operator
print (false ? 'Does not get printed' : 'Does');

// ternary shortcut operator since PHP 5.3
// equivalent of "$x ? $x : 'Does'""
$x = false;
print($x ?: 'Does');

// null coalesce operator since php 7
$a = null;
$b = 'Does print';
echo $a ?? 'a is not set'; // prints 'a is not set'
echo $b ?? 'b is not set'; // prints 'Does print'


$x = 0;
if ($x === '0') {
    print 'Does not print';
} elseif ($x == '1') {
    print 'Does not print';
} else {
    print 'Does print';
}



// This alternative syntax is useful for templates:
?>

<?php if ($x): ?>
This is displayed if the test is truthy.
<?php else: ?>
This is displayed otherwise.
<?php endif; ?>

<?php

// Use switch to save some logic.
switch ($x) {
    case '0':
        print 'Switch does type coercion';
        break; // You must include a break, or you will fall through
               // to cases 'two' and 'three'
    case 'two':
    case 'three':
        // Do something if $variable is either 'two' or 'three'
        break;
    default:
        // Do something by default
}

// While, do...while and for loops are probably familiar
$i = 0;
while ($i < 5) {
    echo $i++;
} // Prints "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // Prints "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // Prints "0123456789"

echo "\n";

$wheels = ['bicycle' => 2, 'car' => 4];

// Foreach loops can iterate over arrays
foreach ($wheels as $wheel_count) {
    echo $wheel_count;
} // Prints "24"

echo "\n";

// You can iterate over the keys as well as the values
foreach ($wheels as $vehicle => $wheel_count) {
    echo "A $vehicle has $wheel_count wheels";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // Exit out of the while loop
    }
    echo $i++;
} // Prints "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // Skip this iteration of the loop
    }
    echo $i;
} // Prints "0124"


/********************************
 * Functions
 */

// Define a function with "function":
function my_function () {
    return 'Hello';
}

echo my_function(); // => "Hello"

// A valid function name starts with a letter or underscore, followed by any
// number of letters, numbers, or underscores.

function add ($x, $y = 1) { // $y is optional and defaults to 1
    $result = $x + $y;
    return $result;
}

echo add(4); // => 5
echo add(4, 2); // => 6

// $result is not accessible outside the function
// print $result; // Gives a warning.

// Since PHP 5.3 you can declare anonymous functions;
$inc = function ($x) {
    return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
    echo "$x - $y - $z";
}

// Functions can return functions
function bar ($x, $y) {
    // Use 'use' to bring in outside variables
    return function ($z) use ($x, $y) {
        foo($x, $y, $z);
    };
}

$bar = bar('A', 'B');
$bar('C'); // Prints "A - B - C"

// You can call named functions using strings
$function_name = 'add';
echo $function_name(1, 2); // => 3
// Useful for programatically determining which function to run.
// Or, use call_user_func(callable $callback [, $parameter [, ... ]]);


// You can get the all the parameters passed to a function
function parameters() {
    $numargs = func_num_args();
    if ($numargs > 0) {
        echo func_get_arg(0) . ' | ';
    }
    $args_array = func_get_args();
    foreach ($args_array as $key => $arg) {
        echo $key . ' - ' . $arg . ' | ';
    }
}

parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |

// Since PHP 5.6 you can get a variable number of arguments
function variable($word, ...$list) {
	echo $word . " || ";
	foreach ($list as $item) {
		echo $item . ' | ';
	}
}

variable("Separate", "Hello", "World"); // Separate || Hello | World |

/********************************
 * Includes
 */

<?php
// PHP within included files must also begin with a PHP open tag.

include 'my-file.php';
// The code in my-file.php is now available in the current scope.
// If the file cannot be included (e.g. file not found), a warning is emitted.

include_once 'my-file.php';
// If the code in my-file.php has been included elsewhere, it will
// not be included again. This prevents multiple class declaration errors

require 'my-file.php';
require_once 'my-file.php';
// Same as include(), except require() will cause a fatal error if the
// file cannot be included.

// Contents of my-include.php:
<?php

return 'Anything you like.';
// End file

// Includes and requires may also return a value.
$value = include 'my-include.php';

// Files are included based on the file path given or, if none is given,
// the include_path configuration directive. If the file isn't found in
// the include_path, include will finally check in the calling script's
// own directory and the current working directory before failing.
/* */

/********************************
 * Classes
 */

// Classes are defined with the class keyword

class MyClass
{
    const MY_CONST      = 'value'; // A constant

    static $staticVar   = 'static';

    // Static variables and their visibility
    public static $publicStaticVar = 'publicStatic';
    // Accessible within the class only
    private static $privateStaticVar = 'privateStatic';
    // Accessible from the class and subclasses
    protected static $protectedStaticVar = 'protectedStatic';

    // Properties must declare their visibility
    public $property    = 'public';
    public $instanceProp;
    protected $prot = 'protected'; // Accessible from the class and subclasses
    private $priv   = 'private';   // Accessible within the class only

    // Create a constructor with __construct
    public function __construct($instanceProp)
    {
        // Access instance variables with $this
        $this->instanceProp = $instanceProp;
    }

    // Methods are declared as functions inside a class
    public function myMethod()
    {
        print 'MyClass';
    }

    // final keyword would make a function unoverridable
    final function youCannotOverrideMe()
    {
    }

    // Magic Methods

    // what to do if Object is treated as a String
    public function __toString()
    {
        return $property;
    }

    // opposite to __construct()
    // called when object is no longer referenced
    public function __destruct()
    {
        print "Destroying";
    }

/*
 * Declaring class properties or methods as static makes them accessible without
 * needing an instantiation of the class. A property declared as static can not
 * be accessed with an instantiated class object (though a static method can).
 */

    public static function myStaticMethod()
    {
        print 'I am static';
    }
}

// Class constants can always be accessed statically
echo MyClass::MY_CONST;    // Outputs 'value';

echo MyClass::$staticVar;  // Outputs 'static';
MyClass::myStaticMethod(); // Outputs 'I am static';

// Instantiate classes using new
$my_class = new MyClass('An instance property');
// The parentheses are optional if not passing in an argument.

// Access class members using ->
echo $my_class->property;     // => "public"
echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod();        // => "MyClass"


// Extend classes using "extends"
class MyOtherClass extends MyClass
{
    function printProtectedProperty()
    {
        echo $this->prot;
    }

    // Override a method
    function myMethod()
    {
        parent::myMethod();
        print ' > MyOtherClass';
    }
}

$my_other_class = new MyOtherClass('Instance prop');
$my_other_class->printProtectedProperty(); // => Prints "protected"
$my_other_class->myMethod();               // Prints "MyClass > MyOtherClass"

final class YouCannotExtendMe
{
}

// You can use "magic methods" to create getters and setters
class MyMapClass
{
    private $property;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MyMapClass();
echo $x->property; // Will use the __get() method
$x->property = 'Something'; // Will use the __set() method

// Classes can be abstract (using the abstract keyword) or
// implement interfaces (using the implements keyword).
// An interface is declared with the interface keyword.

interface InterfaceOne
{
    public function doSomething();
}

interface InterfaceTwo
{
    public function doSomethingElse();
}

// interfaces can be extended
interface InterfaceThree extends InterfaceTwo
{
    public function doAnotherContract();
}

abstract class MyAbstractClass implements InterfaceOne
{
    public $x = 'doSomething';
}

class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
{
    public function doSomething()
    {
        echo $x;
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


// Classes can implement more than one interface
class SomeOtherClass implements InterfaceOne, InterfaceTwo
{
    public function doSomething()
    {
        echo 'doSomething';
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


/********************************
 * Traits
 */

// Traits are available from PHP 5.4.0 and are declared using "trait"

trait MyTrait
{
    public function myTraitMethod()
    {
        print 'I have MyTrait';
    }
}

class MyTraitfulClass
{
    use MyTrait;
}

$cls = new MyTraitfulClass();
$cls->myTraitMethod(); // Prints "I have MyTrait"


/********************************
 * Namespaces
 */

// This section is separate, because a namespace declaration
// must be the first statement in a file. Let's pretend that is not the case

<?php

// By default, classes exist in the global namespace, and can
// be explicitly called with a backslash.

$cls = new \MyClass();



// Set the namespace for a file
namespace My\Namespace;

class MyClass
{
}

// (from another file)
$cls = new My\Namespace\MyClass;

//Or from within another namespace.
namespace My\Other\Namespace;

use My\Namespace\MyClass;

$cls = new MyClass();

// Or you can alias the namespace;

namespace My\Other\Namespace;

use My\Namespace as SomeOtherNamespace;

$cls = new SomeOtherNamespace\MyClass();


/**********************
* Late Static Binding
*
*/

class ParentClass
{
    public static function who()
    {
        echo "I'm a " . __CLASS__ . "\n";
    }

    public static function test()
    {
        // self references the class the method is defined within
        self::who();
        // static references the class the method was invoked on
        static::who();
    }
}

ParentClass::test();
/*
I'm a ParentClass
I'm a ParentClass
*/

class ChildClass extends ParentClass
{
    public static function who()
    {
        echo "But I'm " . __CLASS__ . "\n";
    }
}

ChildClass::test();
/*
I'm a ParentClass
But I'm ChildClass
*/

/**********************
*  Magic constants
*  
*/

// Get current class name. Must be used inside a class declaration.
echo "Current class name is " . __CLASS__;

// Get full path directory of a file
echo "Current directory is " . __DIR__;

    // Typical usage
    require __DIR__ . '/vendor/autoload.php';

// Get full path of a file
echo "Current file path is " . __FILE__;

// Get current function name
echo "Current function name is " . __FUNCTION__;

// Get current line number
echo "Current line number is " . __LINE__;

// Get the name of the current method. Only returns a value when used inside a trait or object declaration.
echo "Current method is " . __METHOD__;

// Get the name of the current namespace
echo "Current namespace is " . __NAMESPACE__;

// Get the name of the current trait. Only returns a value when used inside a trait or object declaration.
echo "Current trait is " . __TRAIT__;

/**********************
*  Error Handling
*  
*/

// Simple error handling can be done with try catch block

try {
    // Do something
} catch (Exception $e) {
    // Handle exception
}

// When using try catch blocks in a namespaced environment use the following

try {
    // Do something
} catch (\Exception $e) {
    // Handle exception
}

// Custom exceptions

class MyException extends Exception {}

try {

    $condition = true;

    if ($condition) {
        throw new MyException('Something just happened');
    }

} catch (MyException $e) {
    // Handle my exception
}

```

## More Information

Visit the [official PHP documentation](http://www.php.net/manual/) for reference
and community input.

If you're interested in up-to-date best practices, visit
[PHP The Right Way](http://www.phptherightway.com/).

If you're coming from a language with good package management, check out
[Composer](http://getcomposer.org/).

For common standards, visit the PHP Framework Interoperability Group's
[PSR standards](https://github.com/php-fig/fig-standards).
---
category: language
language: bf
filename: learnbf-pl.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Jakub Młokosiewicz", "https://github.com/hckr"]
lang: pl-pl

---

Brainfuck (pisane małymi literami, za wyjątkiem początku zdania) jest bardzo 
minimalistycznym, kompletnym w sensie Turinga, językiem programowania.
Zawiera zaledwie 8 poleceń.

Możesz przetesotwać brainfucka w swojej przeglądarce, korzystając z narzędzia 
[brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/).

```
Wszystkie znaki oprócz "><+-.,[]" (wyłączając znaki zapytania) są ignorowane.

Pamięć w brainfucku jest reprezentowana przez tablicę 30.000 komórek
zainicjalizowanych zerami, ze wskaźnikiem pokazującym na aktualną komórkę.

Oto osiem poleceń brainfucka:
+ : inkrementuje (zwiększa o jeden) wartość aktualnie wskazywanej komórki
- : dekrementuje (zmniejsza o jeden) wartość aktualnie wskazywanej komórki
> : przesuwa wskaźnik na następną komórkę (w prawo)
< : przesuwa wskaźnik na poprzednią komórkę (w lewo)
. : wyświetla wartość bieżącej komórki (w formie znaku ASCII, np. 65 = 'A')
, : wczytuje (jeden) znak z wejścia do bieżącej komórki
    (konkretnie jego numer z tabeli ASCII)
[ : jeśli wartość w bieżącej komórce jest rózna zero, przechodzi do
    odpowiadającego ]; w przeciwnym wypdaku przechodzi do następnej instrukcji
] : Jeśli wartość w bieżącej komórce jest rózna od zera, przechodzi do
    następnej instrukcji; w przeciwnym wypdaku przechodzi do odpowiadającego [

[ i ] oznaczają pętlę while. Oczywiście każda pętla rozpoczęta [
musi być zakończona ].

Zobaczmy kilka prostych programów w brainfucku.


++++++ [ > ++++++++++ < - ] > +++++ .

Ten program wypisuje literę 'A'. Najpierw zwiększa wartość komórki #1 do 6.
Komórka #1 będzie wykorzystana w pętli. Następnie program wchodzi w pętlę ([)
i przechodzi do komórki #2. Pętla wykonuje się sześć razy (komórka #1 jest
dekrementowana sześć razy, nim osiągnie wartość zero, kiedy to program
przechodzi do odpowiadającego ] i wykonuje kolejne instrukcje).

W tym momencie wskaźnik pokazuje na komórkę #1, mającą wartość 0, podczas gdy
komórka #2 ma wartość 60. Przesuwamy wskaźnik na komórkę #2, inkrementujemy ją
pięć razy, uzyskując wartość 65. Następnie wyświetlamy wartość komórki #2.
65 to 'A' w tabeli ASCII, więc właśnie ten znak jest wypisany na konsolę.


, [ > + < - ] > .

Ten program wczytuje znak z wejścia i umieszcza jego kod ASCII w komórce #1.
Następnie zaczyna się pętla, w której znajdują się następujące instrukcje: 
przesunięcie wskaźnika na komórkę #2, inkrementacja wartości komóri #2,
powrót do komórki #1 i dekrementacja wartości komórki #1. Instrukcje pętli
wykonują się aż wartość komórki #1 osiągnie zero, a komórka #2 osiągnie
poprednią wartość komórki #1. Ponieważ na końcu pętli wskaźnik pokazuje na
komórkę #1, po pętli następuje instrukcja przejścia do komórki #2 i wysłanie
jej wartości (w formie znaku ASCII) na wyjście.

Zauważ, że odstępy służą wyłącznie poprawie czytelności.
Równie dobrze można powyższy program zapisać tak:

,[>+<-]>.


Spróbuj odgadnąć, co robi poniższy program:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Ten program pobiera z wejścia dwie liczby i je mnoży.

Po wczytaniu dwóch wejść (do komórek #1 i #2) następuje pętla zewnętrzna,
warunkowana wartością komórki #1. Następnie program przechodzi do komórki #2
i rozpoczyna pętlę wewnętrzną z warunkiem zakończenia w komórce #2,
inkrementującą komórkę #3. Tu jednak pojawia się problem: w chwili zakończenia
wewnętrznej pętli komórka #2 ma wartość zero. W takim razie wewętrzna pętla
nie wywoła się następny raz. Aby rozwiązać ten problem, inkrementujemy także
wartość komórki #4, a następnie kopiujemy jej wartość do komórki #2.
Ostatecznie wynik działania znajduje się w komórce #3.
```

I to właśnie jest brainfuck. Nie taki trudny, co? W ramach rozrywki możesz
napisać własne programy w brainfucku. Możesz też napisać interpreter brainfucka
w innym języku. Implementacja interpretera to dość proste zadanie. Jeśli
jesteś masochistą, spróbuj napisać interpreter brainfucka w... brainfucku.
---
category: language
filename: haskell-pl.hs
language: Haskell
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["Remigiusz Suwalski", "https://github.com/remigiusz-suwalski"]
lang: pl-pl

---

Haskell został zaprojektowany jako praktyczny, czysto funkcyjny język 
programowania. Jest znany przede wszystkim ze względu na jego monady oraz system
typów, ale ja lubię do niego wracać przez jego elegancję. Sprawił on, że 
programowanie jest prawdziwą przyjemnością.

```haskell
-- Komentarze jednolinijkowe zaczynają się od dwóch myślników
{- Komentarze wielolinijkowe należy 
zamykać w bloki klamrami.
-}

----------------------------------------------------
-- 1. Podstawowe typy danych oraz operatory
----------------------------------------------------

-- Mamy liczby
3 -- 3

-- Podstawowe działania działają tak, jak powinny
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- dzielenie domyślnie zwraca ,,dokładny'' wynik
35 / 4 -- 8.75

-- dzielenie całkowitoliczbowe
35 `div` 4 -- 8

-- wartości logiczne także są podstawowym typem danych:
True
False

-- operacje logiczne: negacja oraz porównania
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- W powyższych przykładach, `not` jest funkcją przyjmującą jeden argument.
-- Haskell nie potrzebuje nawiasów, by wywołać funkcję: argumenty są po prostu
-- wypisywane jeden za drugim. Ogólnie wygląda to tak:
-- funkcja arg1 arg2 arg3...
-- Sekcja poświęcona funkcjom zawiera informacje, jak stworzyć własne.

-- Łańcuchy znaków (stringi) i pojedyncze znaki:
"To jest lancuch."
'a' -- znak
'Nie mozna laczyc apostrofow z lancuchami.' -- błąd!

-- Łańcuchy można sklejać
"Hello " ++ "world!" -- "Hello world!"

-- Łańcuch jest listą własnych znaków
['H', 'e', 'l', 'l', 'o'] -- "Hello"
"To jest lancuch" !! 0 -- 'T'

----------------------------------------------------
-- 2. Listy oraz krotki
----------------------------------------------------

-- Wszystkie elementy listy muszą być tego samego typu.
-- Poniższe dwie listy są identyczne:
[1, 2, 3, 4, 5]
[1..5]

-- Zakresy są uniwersalne.
['A'..'F'] -- "ABCDEF"

-- Przy tworzeniu zakresów można określić krok.
[0,2..10] -- [0, 2, 4, 6, 8, 10]
[5..1] -- To nie zadziała, gdyż w Haskellu zakresy tworzone są domyślnie rosnąco
[5,4..1] -- [5, 4, 3, 2, 1]

-- indeksowanie listy od zera
[1..10] !! 3 -- 4

-- Można nawet tworzyć listy nieskończone!
[1..] -- lista wszystkich liczb naturalnych

-- Nieskończone listy mają prawo działać, ponieważ Haskell cechuje się leniwym 
-- wartościowaniem. To oznacza, że obliczane są jedynie te elementy listy, 
-- których istotnie potrzebujemy. Możemy poprosić o tysiączny element i 
-- dostaniemy go:

[1..] !! 999 -- 1000

-- Haskell wyznaczył pierwsze tysiąc elementów listy, ale cała jej reszta 
-- jeszcze nie istnieje! Nie zostanie obliczona ich wartość, póki nie zajdzie
-- taka potrzeba.

-- łączenie dwóch list
[1..5] ++ [6..10]

-- dodawanie pojedynczego elementu na początek listy
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- więcej operacji na listach
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- list comprehensions
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- z dodatkowym warunkiem
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- każdy element krotki może być innego typu, jednak sama krotka musi być stałej 
-- długości. Przykładowo:
("haskell", 1)

-- dostęp do elementów pary (krotki długości 2):
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. Funkcje
----------------------------------------------------
-- Prosta funkcja przyjmująca dwa argumenty
add a b = a + b

-- Pamiętaj, że podczas stosowania ghci, interpretera Haskella, wszelkie 
-- definicje muszą zostać poprzedzone słowem `let`, na przykład:
-- let add a b = a + b

-- Używanie funkcji:
add 1 2 -- 3

-- Nazwę funkcji można podać między dwoma argumentami, ale wtedy musi zostać
-- otoczona grawisami:
1 `add` 2 -- 3

-- Nazwa funkcji nie musi zawierać żadnych liter, przykładem czego jest 
-- operator dzielenia:
(//) a b = a `div` b
35 // 4 -- 8

-- Strażnicy: prosty sposób na rozbijanie funkcji na przypadki
fib x
  | x < 2 = 1
  | otherwise = fib (x - 1) + fib (x - 2)

-- Dopasowanie wzorca jest podobne. Haskell sam automatycznie wybierze, która
-- z poniższych definicji fib powinna zostać użyta:
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- Dopasowanie z krotkami:
foo (x, y) = (x + 1, y + 2)

-- Dopasowanie z listami. Tutaj `x` jest pierwszym elementem listy, 
-- natomiast `xs` to jej reszta (ogon). Poniższa funkcja nakłada funkcję
-- na każdy z elementów listy:
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- Funkcje anonimowe tworzone są przy użyciu w-tył-ciachu, po którym następują
-- wszystkie argumenty:
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- używanie zwijania z anonimowymi funkcjami: foldl1 zwija z lewej strony,
-- przyjmując jako wartość początkową zbieracza pierwszy element listy.
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. Więcej funkcji
----------------------------------------------------

-- częściowe nakładanie: jeśli funkcja nie otrzyma wszystkich swoich argumentów, 
-- zostaje cześciowo nałożona - zwraca funkcję, która przyjmuje pozostałe,
-- brakujące argumenty.

add a b = a + b
foo = add 10 -- foo jest teraz funkcją, która przyjmuje liczbę, zwiększa ją o 10
foo 5 -- 15

-- Inny sposób na zapisanie tego samego:
foo = (10+)
foo 5 -- 15

-- składanie funkcji:
-- operator `.` składa wiele funkcji w jedną.
-- Dla przykładu, foo jest funkcją, która powiększa swój argument o 10, mnoży 
-- tak uzyskaną liczbę przez 4 i zwraca wynik:
foo = (4*) . (10+)

-- 4*(10 + 5) = 60
foo 5 -- 60

-- ustalanie kolejności
-- Haskell posiada inny operator, `$`, który nakłada funkcję do podanego
-- parametru. W przeciwieństwie do zwykłego lewostronnie łącznego nakładania 
-- funkcji, którego priorytet jest najwyższy (10), operator `$` posiada
-- priorytet 0 i jest prawostronnie łączny. Tak niski priorytet oznacza, że
-- wyrażenie po prawej traktowane jest jako parametr funkcji po lewej

-- wcześniej
even (fib 7) -- fałsz

-- równoważnie
even $ fib 7 -- fałsz

-- składanie funkcji
even . fib $ 7 -- fałsz


----------------------------------------------------
-- 5. Sygnatury typów
----------------------------------------------------

-- Haskell posiada wyjątkowo silny system typów, w którym każde poprawne 
-- wyrażenie ma swój typ.

-- Kilka podstawowych typów:
5 :: Integer
"hello" :: String
True :: Bool

-- Funkcje też są określonego typu.
-- `not` przyjmuje wartość logiczną i taką też zwraca:
-- not :: Bool -> Bool

-- Przykład funkcji przyjmującej dwa argumenty
-- add :: Integer -> Integer -> Integer

-- Dobrą praktyką podczas definiowania wartości jest napisanie nad nią
-- także jej typu:
double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. Wyrażenia warunkowe
----------------------------------------------------

-- wyrażenie warunkowe
haskell = if 1 == 1 then "wspaniale" else "paskudnie" -- haskell = "wspaniale"

-- wyrażenie warunkowe można rozbić na wiele linii, 
-- ale trzeba uważać na wcięcia w kodzie
haskell = if 1 == 1
            then "wspaniale"
            else "paskudnie"

-- rozpatrywanie przypadków: oto jak można parsować argumenty z linii poleceń:
case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"

-- Haskell zastępuje pętle (których nie ma) rekurencyjnymi wywołaniami funkcji.
-- map aplikuje funkcję do każdego elementu listy:

map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- możesz zdefiniować funkcję for przy użyciu map:
for array func = map func array

-- a następnie użyć jej:
for [0..5] $ \i -> show i

-- mogliśmy użyć krótszego zapisu bez zmiany działania funkcji for:
for [0..5] show

-- Do redukcji listy służy polecenie foldl (foldr):
-- foldl <fn> <initial value> <list>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- Jest to równoważne z:
(2 * (2 * (2 * 4 + 1) + 2) + 3)

-- foldl składa od od lewej strony, foldr od prawej
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- To zaś równoważne jest:
(2 * 1 + (2 * 2 + (2 * 3 + 4)))

----------------------------------------------------
-- 7. Typy danych
----------------------------------------------------

-- Oto jak tworzy się nowe typy danych w Haskellu:

data Color = Red | Blue | Green

-- Teraz można używać ich we własnych funkcjach:

say :: Color -> String
say Red = "You are Red!"
say Blue = "You are Blue!"
say Green =  "You are Green!"

-- Twoje typy danych mogą posiadać nawet parametry:

data Maybe a = Nothing | Just a

-- Wszystkie poniższe są typu Maybe
Just "hello"    -- typu `Maybe String`
Just 1          -- typu `Maybe Int`
Nothing         -- typu `Maybe a` for any `a`

----------------------------------------------------
-- 8. Haskell IO
----------------------------------------------------

-- Chociaż obsługa wejścia i wyjścia nie może zostać wyjaśniona przez poznaniem
-- monad, spróbujemy zrobić to częściowo

-- Wykonanie programu napisanego w Haskellu wywołuje funkcję `main` 
-- Musi zwrócić wartość typu `IO a` dla pewnego `a`. Przykład:

main :: IO ()
main = putStrLn $ "Hello, sky! " ++ (say Blue)
-- putStrLn has type String -> IO ()

-- Najłatwiej obsłużyć wejście i wyjście, kiedy program zostanie 
-- zaimplementowany jako funkcja String -> String. Funkcja
--    interact :: (String -> String) -> IO ()
-- pobiera pewien tekst, wykonuje na nim operacje, po czym wypisuje wynik.

countLines :: String -> String
countLines = show . length . lines

main' = interact countLines

-- Możesz myśleć o wartości typu `IO ()` jako reprezentującej ciąg czynności,
-- które komputer ma wykonać, zupełnie niczym program komputerowy w imperatywnym
-- języku programowania. Akcje można łączyć przy użyciu notacji `do`:

sayHello :: IO ()
sayHello = do
   putStrLn "What is your name?"
   name <- getLine -- this gets a line and gives it the name "name"
   putStrLn $ "Hello, " ++ name

-- Ćwiczenie: napisz własną wersję `interact`, 
-- która czyta tylko jedną linię wejścia.

-- Kod w `sayHello` nigdy się nie wykona. Jedyną akcją, która zostanie 
-- uruchomiona, jest wartość `main`.
-- Aby uruchomić `sayHello`, należy zastąpić poprzednią definicję `main` przez
--   main = sayHello

-- Spróbujmy lepiej zrozumieć, jak działa funkcja `getLine`, której właśnie
-- użyliśmy. Jej typem jest
--    getLine :: IO String
-- Możesz myśleć o wartości typu `IO a` jako reprezentującej program, który
-- wygeneruje wartość typu `a`, poza wszystkim innym, co jeszcze zrobi.
-- Możemy także tworzyć własne akcje typu `IO String`:

action :: IO String
action = do
   putStrLn "This is a line. Duh"
   input1 <- getLine
   input2 <- getLine
   -- The type of the `do` statement is that of its last line.
   -- `return` is not a keyword, but merely a function
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- Możemy użyć tego tak jak używaliśmy `getLine`:

main'' = do
    putStrLn "I will echo two lines!"
    result <- action
    putStrLn result
    putStrLn "This was all, folks!"

-- Typ `IO` jest przykładem monady. Sposób w jakim Haskell używa monad do 
-- obsługi wejścia i wyjścia pozwala mu być czysto funkcyjnym językiem.
-- Każda funkcja, która wchodzi w interakcje ze światem zewnętrznym, oznaczana
-- jest jako `IO` w jej sygnaturze typu, co umożliwia odróżnianie funkcji
-- czystych od zależnych od świata lub modyfikujących stan.

-- To naprawdę użyteczna własność, dzięki której jesteśmy w stanie uruchamiać
-- czyste funkcje jednocześnie.

----------------------------------------------------
-- 9. Interaktywne środowisko programowania
----------------------------------------------------

-- Aby uruchomić repl (read-eval-print loop, interaktywne środowisko), należy
-- wpisać `ghci`. Można już programować. Do definiowania nowych wartości służy
-- słowo kluczowe `let`:

let foo = 5

-- Do sprawdzania typów dowolnej wartości (wyrażenia) wykorzystuje się `:t`:

> :t foo
foo :: Integer

-- Działania takie jak `+`, `:` czy `$`, są funkcjami.
-- Przed sprawdzeniem ich typu należy otoczyć je nawiasami:

> :t (:)
(:) :: a -> [a] -> [a]

-- Dodatkowych informacji dostarcza `:i`:

> :i (+)
class Num a where
  (+) :: a -> a -> a
  ...
    -- Defined in ‘GHC.Num’
infixl 6 +

-- Można nawet wykonywać akcje typu `IO ()`!

> sayHello
What is your name?
Friend!
Hello, Friend!

```

Pominęliśmy wiele aspektów Haskella, wliczając w to monady. To właśnie one 
sprawiają, że programowanie w Haskellu sprawia tyle frajdy. Na zakończenie
pokażę Tobie implementację algorytmu quicksort w Haskellu:

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

Haskell może zostać zainstalowany na co najmniej dwa sposoby:
 - tradycyjnie [przy użyciu Cabala](http://www.haskell.org/platform/),
 - nowocześnie [z pomocą Stack](https://www.stackage.org/install).

Godnymi poleceniami wprowadzeniami są wspaniałe
[Learn you a Haskell](http://learnyouahaskell.com/) albo
[Real World Haskell](http://book.realworldhaskell.org/).
---
category: language
language: json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
  - ["Michael Neth", "https://github.com/infernocloud"]
translators:
  - ["Michał Mitrosz", "https://github.com/Voltinus"]
lang: pl-pl
filename: learnjson-pl.json
---

JSON to bardzo prosty format wymiany danych. Jak jest napisane na [json.org](http://json.org), jest łatwy do pisania i czytania dla ludzi i do parsowania i generowania dla maszyn.

Kod JSON musi zawierać któreś z poniższych:
* Zbiór par nazwa/wartość (`{ }`). W różnych językach jest to obiekt, rekord, struktura, słownik, tablica mieszająca, lista z kluczami, lub tablica asocjacyjna.
* Uporządkowana lista wartości (`[ ]`). W różnych językach jest to tablica, wektor, lista, lub sekwencja.
 tablica/lista/sekwencja (`[ ]`) lub słownik/obiekt/tablica asocjacyjna (`{ }`).

JSON w swojej czystej postaci nie ma komentarzy, ale większość parserów akceptuje komentarze w stylu C (`//`, `/* */`). Niektóre parsery pozwalają także na końcowy przecinek (np. przecinek po ostatnim elemencie w tablicy lub po ostatiej własności obiektu), ale powinien on być omijany dla lepszej kompatybilności.

Dla celów tego poradnika wszystko będzie 100% kodem JSON. Na szczęście, to samo mówi za siebie.

Wspierane typy danych:

* Łańcuchy znaków: `"witaj"`, `"\"Cytat.\""`, `"\u0abe"`, `"Nowa linia.\n"`
* Liczby: `23`, `0.11`, `12e10`, `3.141e-10`, `1.23e+4`
* Obiekty: `{ "klucz": "wartość" }`
* Tablice: `["Wartości"]`
* Inne: `true`, `false`, `null`

```json
{
  "klucz": "wartość",

  "klucze": "muszą być zawsze zamknięte w podwójnych cudzysłowach",
  "liczby": 0,
  "łańcuchy": "Hellø, wørld. Wszystkie znaki unicode są dozwolone, razem z \"sekwencjami escape\".",
  "wartości logiczne?": true,
  "nic": null,

  "duża liczba": 1.2e+100,

  "obiekty": {
    "komentarz": "Większość twojej struktury będzie zbudowana z obiektów.",

    "tablica": [0, 1, 2, 3, "Tablice mogą mieć wewnątrz cokolwiek", 5],

    "inny obiekt": {
      "komentarz": "Elementy mogą się w sobie zawierać, bardzo użyteczne"
    }
  },

  "głupota": [
    {
      "źródła potasu": ["banany"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "styl alternatywny": {
    "komentarz": "sprawdź to!"
  , "pozycja przecinka": "nie ma znaczenia, o ile jest przed następnym kluczem, jest poprawnie"
  , "następny komentarz": "jak ładnie"
  },



  "znaki białe": "nie mają znaczenia",



  "to było krótkie": "I gotowe. Wiesz już wszystko o formacie JSON."
}
```

## Dalsza lektura

* [JSON.org](http://json.org) Cały JSON pięknie wytłumaczony na podstawie grafik przypominających schematy blokowe.
---
name: perl
category: language
language: perl
filename: learnperl-pl.pm
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
    - ["Dan Book", "http://github.com/Grinnz"]
translators:
    - ["Michał Kupczyński", "http://github.com/ukoms"]
lang: pl-pl

---

Perl 5 jest wysoce użytecznym, bogatym w wiele opcji językiem programowania
z ponad 25 latami nieustannego rozwoju.

Perl 5 używany jest na ponad 100 różnych platformach (od przenośnych do w
pełni stacjonarnych) i nadaje się zarówno do szybkiego prototypowania jak
i projektów deweloperskich prowadzonych na szeroką skalę.

```perl

# Pojedyncza linia komentarza zaczyna się od znaku hasha (płotka) "#".

#### Typy zmiennych w Perlu

# Zmienna zaczyna się od symbolu dolara "$".
# Prawidłowa nazwa zmiennej zaczyna się od litery lub podkreślnika "_",
# po których następuje dowolna ilość liter, cyfr i podkreślników.

### W Perlu występują trzy główne typy zmiennych: skalary, tablice i hasze.

## Skalary
#  Skalar przechowuje pojedynczą wartość:
my $zwierze = "wielbłąd";
my $odpowiedź = 42;

# Wartości skalarne mogą być ciągami znaków, liczbami całkowitymi lub
# zmiennoprzecinkowymi, zaś Perl automatycznie dokonuje konwersji pomiędzy nimi,
# w zależności od wykonywanego kodu/kontekstu.

## Tablice
#  Tablica przechowuje listę wartości:
my @zwierzęta = ("wielbłąd", "alpaka", "sowa");
my @liczby    = (23, 42, 69);
my @mieszanka = ("wielbłąd", 42, 1.23);

## Hasze
#  Hasz przechowuje zestawy par klucz-wartość:
my %kolor_owocu = ('jabłko', 'czerwony', 'banan', 'żółty');

# Możesz używać białych znaków (spacje, tabulatory) i operatora strzałki "=>"
# by czytelniej sformatować zapis hasza:
my %kolor_owocu = (
    jabłko  => 'czerwony',
    banan => 'żółty',
);

# Skalary, tablice i hasze są bardziej wyczerpująco udokumentowane w dokumencie
# [perldoc perldata](http://perldoc.perl.org/perldata.html).

# Bardziej złożone typy danych mogą być stworzone poprzez używanie referencji,
# które pozwalają Ci zbudować listy i hasze wewnątrz list i haszy.

#### Warunki logiczne i pętle

# W Perlu występują typowe warunki i pętle.
if ($var) {
    ...
} elsif ($var eq 'bar') {
    ...
} else {
    ...
}

unless (warunek) {
    ...
}
# Powyższy zapis jest równoznaczny zapisowi "if (!warunek)"

# Perlowy skrócony zapis warunków:
print "Siema!" if $rozochocony;
print "Nie mamy bananów" unless $banany;

# Pętla while
while (warunek) {
    ...
}

# Pętle for oraz foreach
for ($i = 0; $i <= $max; $i++) {
    ...
}

foreach (@tablica) {
    print "Tym elementem jest $_\n";
}

# lub

foreach my $iterator (@tablica) {
    print "Iterowanym elementem jest $iterator\n";
}

#### Wyrażenia regularne

# Perlowe wyrażenia regularne są tematem tak rozległym, jak wymagającym.
# Istnieje ogromna ilość dokumentacji w artykułach takich jak
# [perlrequick](http://perldoc.perl.org/perlrequick.html),
# [perlretut](http://perldoc.perl.org/perlretut.html) i inne.
# W dużym skrócie, podstawy perlowych wyrażeń regularnych są następujące:

# Proste dopasowanie:
if (/foo/)       { ... }  # prawda jeżeli $_ zawiera "foo"
if ($a =~ /foo/) { ... }  # prawda jeżeli $a zawiera "foo"

# Prosta zamiana:
# Zamienia "foo" na "bar" w zmiennej $a
$a =~ s/foo/bar/;
# Zamienia WSZYSTKIE WYSTĄPIENIA "foo" na "bar" w zmiennej $a
$a =~ s/foo/bar/g;

#### Pliki i I/O

# Możesz otworzyć plik do odczytu lub zapisu używając funkcji "open ()".
open (my $odczyt,    "<",  "odczyt.txt") or die "Błąd otwierania input.txt: $!";
open (my $zapis,     ">",  "zapis.txt")  or die "Błąd otwierania output.txt: $!";
open (my $dopisanie, ">>", "my.log")     or die "Błąd otwierania my.log: $!";

# Pliki możesz odczytywać z otworzonego handlera używając operatora "<>"
# (operator diamentowy). W kontekście skalarnym (przypisanie wyniku do skalara)
# operator ten zczytuje pojedynczą linię pliku, w kontekście listowym
# (przypisanie wyniku do tablicy) zczytuje całą zawartość pliku, przypisując
# każdą linię jako kolejny element listy:
my $linia = <$in>;
my @linie = <$in>;

#### Perlowe funkcje (procedury)

# Pisanie funkcji (procedur) jest proste:
sub logger {
    my $wiadomosc_do_loga = shift;
    open (my HANDLER, ">>", "my.log") or die "Błąd otwierania my.log: $!";
    print HANDLER $wiadomosc_do_loga;
}

# Teraz można używać napisanej funkcji, tak jak każdej innej wbudowanej
# funkcji perlowej:
logger ("Mamy funkcję perlową");

```

#### Używanie modułów perlowych

Moduły perlowe dostarczają szeroki wachlarz możliwości, byś nie musiał
wynajdywać koła na nowo. Moduły te można pobrać z [CPAN](http://www.cpan.org).
Sam Perl zawiera w swoich dystrybucjach kilka najpopularniejszych modułów
z repozytorium [CPAN](http://www.cpan.org).

Najczęściej zadawane pytania [perlfaq](http://perldoc.perl.org/perlfaq.html)
- zawierają pytania i odpowiedzi dotyczące wielu typowo realizowanych zadań.
Często znajdziesz tam również sugestie dotyczące użycia najlepszego modułu
z repozytorium CPAN do zrealizowania konkretnego zadania.


#### Do doczytania

 - [perl-tutorial](http://perl-tutorial.org/)
 - [Naucz się Perla na www.perl.com](http://www.perl.org/learn.html)
 - [perldoc](http://perldoc.perl.org/)
 - wbudowane w Perla: `perldoc perlintro`
---
name: python
category: language
language: python
filename: learnpython-pl.py
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
    - ["Amin Bandali", "http://aminbandali.com"]
    - ["Andre Polykanine", "https://github.com/Oire"]
translators:
    - ["Dominik Krzemiński", "https://github.com/dokato"]
lang: pl-pl
---

Python został opracowany przez Guido Van Rossuma na początku lat 90-tych.
Obecnie jest jednym z najbardziej popularnych języków programowania.
Zakochałem się w Pythonie dzięki porządkowi, jaki utrzymywany jest w kodzie.
To po prostu wykonywalny pseudokod.

Zapraszam do kontaktu. Złapiecie nas na:
- kontakt polski: raymon92 [at] [google's email service]
- kontakt angielski: [@louiedinh](http://twitter.com/louiedinh) lub louiedinh [at] [google's email service]

Uwaga: Ten artykuł odnosi się do wersji Pythona 2.7, ale powinien
działać w wersjach 2.x. Dla wersji 3.x znajdziesz odpowiedni artykuł na stronie głównej.

```python
# -*- coding: utf-8 -*-

# Pojedyncze komentarze oznaczamy takim symbolem.

""" Wielolinijkowe napisy zapisywane są przy użyciu
    potrójnych cudzysłowów i często
    wykorzystywane są jako komentarze.
"""

####################################################
## 1. Podstawowe typy danych i operatory
####################################################

# Liczby to liczby
3  # => 3

# Matematyka jest intuicyjna
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20
35 / 5  # => 7

# Dzielenie może być kłopotliwe. Poniższe działanie to dzielenie
# całkowitoliczbowe(int) i wynik jest automatycznie zaokrąglany.
5 / 2  # => 2

# Aby to naprawić, musimy powiedzieć nieco o liczbach zmiennoprzecinkowych.
2.0     # To liczba zmiennoprzecinkowa, tzw. float
11.0 / 4.0  # => 2.75 ahhh...znacznie lepiej

# Wynik dzielenia całkowitoliczbowego jest obcinany dla liczb
# dodatnich i ujemnych.
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # działa też na floatach
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# Operator modulo - wyznaczanie reszty z dzielenia
7 % 3 # => 1

# Potęgowanie (x do potęgi y-tej)
2**4 # => 16

# Wymuszanie pierwszeństwa w nawiasach
(1 + 3) * 2  # => 8

# Operacje logiczne
# Zauważ, że przy "and" i "or" trzeba zwracać uwagę na rozmiar liter
True and False #=> False  # Fałsz
False or True #=> True   # Prawda

# Zauważ, że operatorów logicznych można używać z intami
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
k1 == True #=> True

# aby zanegować, użyj "not"
not True  # => False
not False  # => True

# Równość ==
1 == 1  # => True
2 == 1  # => False

# Nierówność !=
1 != 1  # => False
2 != 1  # => True

# Więcej porównań
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# Porównania można układać w łańcuch!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# Napisy (typ string) tworzone są przy użyciu cudzysłowów " lub '
"Jestem napisem."
'Ja też jestem napisem.'

# Napisy można dodawać!
"Witaj " + "świecie!"  # => "Witaj świecie!"

# ... a nawet mnożyć
"Hej" * 3  # => "HejHejHej"

# Napis może być traktowany jako lista znaków
"To napis"[0]  # => 'T'

# % może być używane do formatowania napisów:
"%s są %s" % ("napisy", "fajne")

# Jednak nowszym sposobem formatowania jest metoda "format".
# Ta metoda jest obecnie polecana:
"{0} są {1}".format("napisy", "fajne")
# Jeśli nie chce ci się liczyć, użyj słów kluczowych.
"{imie} chce zjeść {jadlo}".format(imie="Bob", jadlo="makaron")

# None jest obiektem
None  # => None

# Nie używaj "==" w celu porównania obiektów z None
# Zamiast tego użyj "is"
"etc" is None  # => False
None is None  # => True

# Operator 'is' testuje identyczność obiektów. Nie jest to zbyt 
# pożyteczne, gdy działamy tylko na prostych wartościach,
# ale przydaje się, gdy mamy do czynienia z obiektami.

# None, 0 i pusty napis "" są odpowiednikami logicznego False.
# Wszystkie inne wartości są uznawane za prawdę (True)
bool(0)  # => False
bool("")  # => False


####################################################
## 2. Zmienne i zbiory danych
####################################################

# Python ma instrukcję wypisującą "print" we wszystkich wersjach 2.x, ale
# została ona usunięta z wersji 3.
print "Jestem Python. Miło Cię poznać!"
# Python ma też funkcję "print" dostępną w wersjach 2.7 i 3...
# ale w 2.7 musisz dodać import (odkomentuj):
# from __future__ import print_function
print("Ja też jestem Python! ")

# Nie trzeba deklarować zmiennych przed przypisaniem.
jakas_zmienna = 5    # Konwencja mówi: używaj małych liter i znaków podkreślenia _
jakas_zmienna  # => 5

# Próba dostępu do niezadeklarowanej zmiennej da błąd.
# Przejdź do sekcji Obsługa wyjątków, aby dowiedzieć się więcej...
inna_zmienna  # Wyrzuca nazwę błędu

# "if" może być użyte jako wyrażenie
"huraaa!" if 3 > 2 else 2  # => "huraaa!"

# Listy:
li = []
# Możesz zacząć od wypełnionej listy
inna_li = [4, 5, 6]

# Dodaj na koniec, używając "append"
li.append(1)    # li to teraz [1]
li.append(2)    # li to teraz [1, 2]
li.append(4)    # li to teraz [1, 2, 4]
li.append(3)    # li to teraz [1, 2, 4, 3]
# Usuwanie z konca da "pop"
li.pop()        # => 3 a li stanie się [1, 2, 4]
# Dodajmy ponownie
li.append(3)    # li to znowu [1, 2, 4, 3].

# Dostęp do list jak do każdej tablicy
li[0]  # => 1
# Aby nadpisać wcześniej wypełnione miejsca w liście, użyj znaku =
li[0] = 42
li[0]  # => 42
li[0] = 1  # Uwaga: ustawiamy starą wartość
# Tak podglądamy ostatni element
li[-1]  # => 3

# Jeżeli wyjdziesz poza zakres...
li[4]  # ... zobaczysz IndexError

# Możesz też tworzyć wycinki.
li[1:3]  # => [2, 4]
# Bez początku
li[2:]  # => [4, 3]
# Omijamy koniec
li[:3]  # => [1, 2, 4]
# Wybierz co drugi
li[::2]   # =>[1, 4]
# Odwróć listę
li[::-1]   # => [3, 4, 2, 1]
# Użyj kombinacji powyższych aby tworzyć bardziej skomplikowane wycinki
# li[poczatek:koniec:krok]

# Usuń element używając "del"
del li[2]   # li to teraz [1, 2, 3]

# Listy można dodawać
li + inna_li   # => [1, 2, 3, 4, 5, 6]
# Uwaga: wartości oryginalnych list li i inna_li się nie zmieniają.

# Do łączenia list użyj "extend()"
li.extend(other_li)   # li to teraz [1, 2, 3, 4, 5, 6]

# Sprawdź, czy element jest w liście używając "in"
1 in li   # => True

# "len()" pokazuje długość listy
len(li)   # => 6


# Krotki (tuple) są jak listy, ale nie można ich modyfikować.
tup = (1, 2, 3)
tup[0]   # => 1
tup[0] = 3  # wyrzuci TypeError

# Ale wielu akcji dla list możesz używać przy krotkach
len(tup)   # => 3
tup + (4, 5, 6)   # => (1, 2, 3, 4, 5, 6)
tup[:2]   # => (1, 2)
2 in tup   # => True

# Można rozpakować krotki i listy do poszczególych zmiennych
a, b, c = (1, 2, 3)     # a to teraz 1, b jest 2, a c to 3
# Jeżeli zapomnisz nawiasów, automatycznie tworzone są krotki
d, e, f = 4, 5, 6
# Popatrz jak prosto zamienić wartości
e, d = d, e     # d to teraz 5 a e to 4


# Słowniki są również pożyteczne
pusty_slownik = {}
# Tu tworzymy wypełniony:
pelen_slownik = {"raz": 1, "dwa": 2, "trzy": 3}

# Podglądany wartość
pelen_slownik["one"]   # => 1

# Wypisz wszystkie klucze, używając "keys()"
pelen_slownik.keys()   # => ["trzy", "dwa", "raz"]
# Uwaga: słowniki nie zapamiętują kolejności kluczy.

# A teraz wszystkie wartości "values()"
pelen_slownik.values()   # => [3, 2, 1]
# Uwaga: to samo dotyczy wartości.

# Sprawdzanie czy klucz występuje w słowniku za pomocą "in"
"raz" in pelen_slownik   # => True
1 in pelen_slownik   # => False

# Próba dobrania się do nieistniejącego klucza da KeyError
pelen_slownik["cztery"]   # KeyError

# Użyj metody "get()", aby uniknąć błędu KeyError
pelen_slownik.get("raz")   # => 1
pelen_slownik.get("cztery")   # => None
# Metoda get zwraca domyślną wartość gdy brakuje klucza
pelen_slownik.get("one", 4)   # => 1
pelen_slownik.get("cztery", 4)   # => 4
# zauważ, że pelen_slownik.get("cztery") wciąż zwraca => None
# (get nie ustawia wartości słownika)

# przypisz wartość do klucza podobnie jak w listach
pelen_slownik["cztery"] = 4  # teraz: pelen_slownik["cztery"] => 4

# "setdefault()" wstawia do słownika tylko jeśli nie było klucza
pelen_slownik.setdefault("piec", 5)  # pelen_slownik["piec"] daje 5
pelen_slownik.setdefault("piec", 6)  # pelen_slownik["piec"] to wciąż 5


# Teraz zbiory (set) - działają jak zwykłe listy, ale bez potórzeń
pusty_zbior = set()
# Inicjalizujemy "set()" pewnymi wartościami
jakis_zbior = set([1, 2, 2, 3, 4])   # jakis_zbior to teraz set([1, 2, 3, 4])

# kolejność nie jest zachowana, nawet gdy wydaje się posortowane
inny_zbior = set([4, 3, 2, 2, 1])  # inny_zbior to set([1, 2, 3, 4])

# Od Pythona 2.7 nawiasy klamrowe {} mogą być użyte do deklarowania zbioru
pelen_zbior = {1, 2, 2, 3, 4}   # => {1, 2, 3, 4}

# Dodaj więcej elementów przez "add()"
pelen_zbior.add(5)   # pelen_zbior is now {1, 2, 3, 4, 5}

# Znajdź przecięcie (część wspólną) zbiorów, używając &
inny_zbior = {3, 4, 5, 6}
pelen_zbior & other_set   # => {3, 4, 5}

# Suma zbiorów |  
pelen_zbior | other_set   # => {1, 2, 3, 4, 5, 6}

# Różnicę zbiorów da znak -
{1, 2, 3, 4} - {2, 3, 5}   # => {1, 4}

# Sprawdzanie obecności w zbiorze: "in".
2 in pelen_zbior   # => True
10 in pelen_zbior   # => False


####################################################
## 3. Kontrola przepływu
####################################################

# Tworzymy zmienną jakas_zm
jakas_zm = 5

# Tutaj widzisz wyrażenie warunkowe "if". Wcięcia w Pythonie są ważne!
# Poniższy kod wypisze "jakas_zm jest mniejsza niż 10"
if jakas_zm > 10:
    print("jakas_zm jest wieksza niż 10")
elif some_var < 10:    # Opcjonalna klauzula elif
    print("jakas_zm jest mniejsza niż 10")
else:           # Również opcjonalna klauzula else
    print("jakas_zm jest równa 10")


"""
Pętla for iteruje po elementach listy, wypisując:
    pies to ssak
    kot to ssak
    mysz to ssak
"""
for zwierze in ["pies", "kot", "mysz"]:
    # Użyj metody format, aby umieścić wartość zmiennej w ciągu
    print("{0} to ssak".format(zwierze))

"""
"range(liczba)" zwraca listę liczb
z przedziału od zera do wskazanej liczby (bez niej):
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
While to pętla, która jest wykonywana, dopóki spełniony jest warunek:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Skrót od x = x + 1

# Wyjątki wyłapujemy, używając try i except

# Działa w Pythonie 2.6 i wyższych:
try:
    # Użyj "raise" aby wyrzucić wyjątek
    raise IndexError("To błąd indeksu")
except IndexError as e:
    pass    # Pass to brak reakcji na błąd. Zwykle opisujesz tutaj, jak program ma się zachować w przypadku błędu.
except (TypeError, NameError):
    pass    # kilka wyjątków można przechwycić jednocześnie.
else:   # Opcjonalna część bloku try/except. Musi wystąpić na końcu
    print "Wszystko ok!"   # Zadziała tylko, gdy program nie napotka wyjatku.


####################################################
## 4. Funkcje
####################################################

# Użyj "def", aby stworzyć nową funkcję
def dodaj(x, y):
    print("x to %s, a y to %s" % (x, y))
    return x + y    # słowo kluczowe return zwraca wynik działania

# Tak wywołuje się funkcję z parametrami:
dodaj(5, 6)   # => wypisze "x to 5, a y to 6" i zwróci 11

# Innym sposobem jest wywołanie z parametrami nazwanymi.
dodaj(y=6, x=5)   # tutaj kolejność podania nie ma znaczenia.


# Można też stworzyć funkcję, które przyjmują zmienną liczbę parametrów pozycyjnych,
# które zostaną przekazana jako krotka, pisząc w definicji funkcji "*args"
def varargs(*args):
    return args

varargs(1, 2, 3)   # => (1, 2, 3)


# Można też stworzyć funkcję, które przyjmują zmienną liczbę parametrów
# nazwanych kwargs, które zostaną przekazane jako słownik, pisząc w definicji funkcji "**kwargs"
def keyword_args(**kwargs):
    return kwargs

# Wywołajmy to i sprawdźmy co się dzieje
keyword_args(wielka="stopa", loch="ness")   # => {"wielka": "stopa", "loch": "ness"}


# Możesz też przyjmować jednocześnie zmienną liczbę parametrów pozycyjnych i nazwanych
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) wypisze:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Użyj * aby rozwinąć parametry z krotki args
# i użyj ** aby rozwinąć parametry nazwane ze słownika kwargs.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)   # odpowiednik foo(1, 2, 3, 4)
all_the_args(**kwargs)   # odpowiednik foo(a=3, b=4)
all_the_args(*args, **kwargs)   # odpowiednik foo(1, 2, 3, 4, a=3, b=4)

# Możesz podać parametry args i kwargs do funkcji równocześnie
# przez rozwinięcie odpowiednio * i **
def pass_all_the_args(*args, **kwargs):
    all_the_args(*args, **kwargs)
    print varargs(*args)
    print keyword_args(**kwargs)

# Zasięg zmiennych
x = 5

def setX(num):
    # Lokalna zmienna x nie jest tym samym co zmienna x
    x = num # => 43
    print x # => 43
    
def setGlobalX(num):
    global x
    print x # => 5
    x = num # globalna zmienna to teraz 6
    print x # => 6

setX(43)
setGlobalX(6)

# Można tworzyć funkcje wewnętrzne i zwrócić je jako wynik
def rob_dodawacz(x):
    def dodawacz(y):
        return x + y
    return dodawacz

dodaj_10 = rob_dodawacz(10)
dodaj_10(3)   # => 13

# Są również funkcje anonimowe "lambda"
(lambda x: x > 2)(3)   # => True

# Python ma też wbudowane funkcje wyższego rzędu (przyjmujące inną funkcje jako parametr)
map(add_10, [1, 2, 3])   # => [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]

# Można używać wyrażeń listowych (list comprehensions) do mapowania i filtrowania
[add_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]   # => [6, 7]


####################################################
## 5. Klasy
####################################################

# Wszystkie klasy są podklasą object
class Czlowiek(object):

    # Atrybut klasy. Występuje we wszystkich instancjach klasy.
    gatunek = "H. sapiens"
    
    # Podstawowa inicjalizacja - wywoływana podczas tworzenia instacji.
    # Zauważ, że podwójne podkreślenia przed i za nazwą oznaczają
    # specjalne obiekty lub atrybuty wykorzystywane wewnętrznie przez Pythona.
    # Nie używaj ich we własnych metodach.
    def __init__(self, nazwa):
        # przypisz parametr "nazwa" do atrybutu instancji
        self.nazwa = nazwa

    # Metoda instancji. Wszystkie metody przyjmują "self" jako pierwszy argument
    def mow(self, wiadomosc):
        return "%s: %s" % (self.nazwa, wiadomosc)

    # Metoda klasowa współdzielona przez instancje.
    # Przyjmuje wywołującą klasę jako pierwszy argument.
    @classmethod
    def daj_gatunek(cls):
        return cls.gatunek

    # Metoda statyczna jest wywoływana bez argumentów klasy czy instancji.
    @staticmethod
    def grunt():
        return "*grunt*"


# Instancja klasy
i = Czlowiek(name="Ian")
print(i.mow("cześć"))     # wypisze "Ian: cześć"

j = Czlowiek("Joel")
print(j.mow("cześć"))  # wypisze "Joel: cześć"

# Wywołujemy naszą metodę klasową
i.daj_gatunek()   # => "H. sapiens"

# Zmieniamy wspólny parametr
Czlowiek.gatunek = "H. neanderthalensis"
i.daj_gatunek()   # => "H. neanderthalensis"
j.daj_gatunek()   # => "H. neanderthalensis"

# Wywołanie metody statycznej
Czlowiek.grunt()   # => "*grunt*"


####################################################
## 6. Moduły
####################################################

# Tak importuje się moduły:
import math
print(math.sqrt(16))  # => 4

# Można podać konkretne funkcje, np. ceil, floor z modułu math
from math import ceil, floor
print(ceil(3.7))  # => 4.0
print(floor(3.7))   # => 3.0

# Można zaimportować wszystkie funkcje z danego modułu.
# Uwaga: nie jest to polecane, bo później w kodzie trudno połapać się,
# która funkcja pochodzi z którego modułu.
from math import *

# Można skracać nazwy modułów.
import math as m
math.sqrt(16) == m.sqrt(16)   # => True
# sprawdźmy czy funkcje są równoważne
from math import sqrt
math.sqrt == m.sqrt == sqrt  # => True

# Moduły Pythona to zwykłe skrypty napisane w tym języku. Możesz
# pisać własne i importować je. Nazwa modułu to nazwa pliku.

# W ten sposób sprawdzisz jakie funkcje wchodzą w skład modułu.
import math
dir(math)


####################################################
## 7. Zaawansowane
####################################################

# Generatory pomagają tworzyć tzw. "leniwy kod"
def podwojne_liczby(iterowalne):
    for i in iterowalne:
        yield i + i

# Generatory tworzą wartości w locie.
# Zamiast generować wartości raz i zapisywać je (np. w liście), 
# generator tworzy je na bieżąco, w wyniku iteracji. To oznacza,
# że w poniższym przykładzie wartości większe niż 15 nie będą przetworzone 
# w funkcji "podwojne_liczby".
# Zauważ, że xrange to generator, który wykonuje tę samą operację co range.
# Stworzenie listy od 1 do 900000000 zajęłoby sporo czasu i pamięci,
# a xrange tworzy obiekt generatora zamiast budować całą listę jak range.

# Aby odróżnić nazwę zmiennej od nazwy zarezerwowanej w Pythonie, używamy
# zwykle na końcu znaku podkreślenia
xrange_ = xrange(1, 900000000)

# poniższa pętla będzie podwajać liczby aż do 30 
for i in podwojne_liczby(xrange_):
    print(i)
    if i >= 30:
        break


# Dekoratory
# w tym przykładzie "beg" jest nakładką na "say"
# Beg wywołuje say. Jeśli say_please jest prawdziwe, wtedy zwracana wartość
# zostanie zmieniona

from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Proszę! Jestem spłukany :(")
        return msg
    return wrapper


@beg
def say(say_please=False):
    msg = "Kupisz mi piwo?"
    return msg, say_please


print(say())  # Kupisz mi piwo?
print(say(say_please=True))  # Kupisz mi piwo? Proszę! Jestem spłukany :(
```

## Gotowy na więcej?
### Polskie

* [Zanurkuj w Pythonie](http://pl.wikibooks.org/wiki/Zanurkuj_w_Pythonie)
* [LearnPythonPl](http://www.learnpython.org/pl/)

### Angielskie:
#### Darmowe źródła online

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)

#### Inne

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: ruby
filename: learnruby-pl.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
  - ["Gabriel Halley", "https://github.com/ghalley"]
  - ["Persa Zula", "http://persazula.com"]
translators:
    - ["Marcin Klocek", "https://github.com/mklocek"]
lang: pl-pl
---

```ruby
# To jest komentarz

=begin 
To jest wielolinijkowy komentarz
Nikt ich nie używa
Ty też nie powinieneś
=end

# Przede wszystkim: Wszystko jest obiektem.

# Liczby są obiektami

3.class #=> Fixnum

3.to_s #=> "3"


# Trochę podstawowej arytmetyki
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
5 % 3 #=> 2
5 ^ 6 #=> 3

# Arytmetyka jest zastąpeniem składni
# metod wywoływanych na obiektach
1.+(3) #=> 4
10.* 5 #=> 50

# Wartości specjalne są obiektami
nil # To na prawdę jest niczym
true # prawda
false # fałsz

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Równość
1 == 1 #=> true
2 == 1 #=> false

# Nierówność
1 != 1 #=> false
2 != 1 #=> true

# jedyną 'fałszywą' wartością poza false, jest nil 

!nil   #=> true
!false #=> true
!0     #=> false

# Więcej porównań
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Operatory logiczne
true && false #=> false
true || false #=> true
!true #=> false

# Istnieją alternatywne wersje operatorów logicznych ze znacznie mniejszym
# pierwszeństwem. Używane są by kontrolować wyrażenia w łańcuchach wyrażeń
# aż jedno z nich wróci true lub false.

# `zrob_cos_innego` wywołaj tylko wtedy gdy `zrob_cos` zakończy się sukcesem.
zrob_cos_innego() and zrob_cos()
# `log_error` wywołaj tylko wtedy gdy `zrob_cos` nie zakończy się sukcesem.
zrob_cos() or log_error()


# Stringi są obiektami

'Jestem stringiem.'.class #=> String
"Ja również jestem stringiem.".class #=> String

wypelnienie = 'użyć interpolacji stringa'
"Potrafię #{wypelnienie} używając podwójnych cudzysłowów."
#=> "Potrafię użyć interpolacji stringa używając podwójnych cudzysłowów."

# Staraj się zapisywać stringi za pomocą apostrof, zamiast cudzysłowów tam, gdzie to możliwe
# Cudzysłowy wykonują dodatkowe wewnętrzne operacje


# Łączenie stringów, ale nie liczb
'hej ' + 'świecie'  #=> "hej świecie"
'hej ' + 3 #=> TypeError: can't convert Fixnum into String
'hej ' + 3.to_s #=> "hej 3"

# Łączenie stringów i operatorów
'hej ' * 3 #=> "hej hej hej "

# Dodawanie do stringa
'hej' << ' świecie' #=> "hej świecie"

# wydrukowanie wartości wraz z nową linią na końcu
puts "Drukuję!"
#=> Drukuję!
#=> nil

# wydrukowanie wartości bez nowej linii na końcu 
print "Drukuję!"
#=> Drukuję! => nill

# Zmienne
x = 25 #=> 25
x #=> 25

# Zauważ, że przypisanie zwraca przypisywaną wartość
# To znaczy, że możesz wykonać wielokrotne przypisanie:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Zwyczajowo, używaj notacji nazwa_zmiennej dla nazw zmiennych
nazwa_zmiennej = true

# Używaj opisowych nazw zmiennych
sciezka_do_projektu = '/dobra/nazwa/'
sciezka = '/zla/nazwa/'

# Symbole (są obiektami)
# Symbole są niezmiennymi, wielokrotnie używanymi stałymi reprezentowanymi wewnętrznie jako 
# liczby całkowite. Często używane są zamiast stringów w celu wydajniejszego przekazywania danych

:oczekujacy.class #=> Symbol

status = :oczekujacy

status == :oczekujacy #=> true

status == 'oczekujacy' #=> false

status == :zatwierdzony #=> false

# Tablice

# To jest tablica
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Tablice mogą zwierać różne typy danych

[1, 'hej', false] #=> [1, "hej", false]

# Tablice mogę być indeksowane
# Od początku
tablica[0] #=> 1
tablica.first #=> 1
tablica[12] #=> nil

# Podobnie jak przy arytmetyce, dostęp poprzez [zmienna]
# jest tylko czytelniejszą składnią
# dla wywoływania metody [] na obiekcie
tablica.[] 0 #=> 1
tablica.[] 12 #=> nil

# Od końca
tablica[-1] #=> 5
tablica.last #=> 5

# Z początkowym indeksem i długością
tablica[2, 3] #=> [3, 4, 5]

# Odwrotność tablicy
a=[1,2,3]
a.reverse! #=> [3,2,1]

# Lub zakres
array[1..3] #=> [2, 3, 4]

# Dodawanie do tablicy w taki sposób
tablica << 6 #=> [1, 2, 3, 4, 5, 6]
# Lub taki
tablica.push(6) #=> [1, 2, 3, 4, 5, 6]

# Sprawdzanie, czy tablica zawiera element
tablica.include?(1) #=> true

# Hasze są Ruby'owymi podstawowymi słownikami z parami klucz/wartość.
# Hasze są zapisywane za pomocą nawiasów klamrowych
hasz = { 'kolor' => 'zielony', 'numer' => 5 }

hasz.keys #=> ['kolor', 'numer']

# Można szybko sprawdzić zawartość hasza za pomocą kluczy:
hasz['kolor'] #=> 'zielony'
hasz['numer'] #=> 5

# Sprawdzenie wartośći dla nieistniejącego klucza zwraca nil:
hasz['nic tutaj nie ma'] #=> nil

# Od wersji 1.9, Ruby posiada specjalną składnię, gdy używamy symboli jako kluczy:

nowy_hasz = { stan: 3, akcja: true }

nowy_hasz.keys #=> [:stan, :akcja]

# Sprawdzenie istnienia kluczy i wartości w haszu
new_hash.has_key?(:defcon) #=> true
new_hash.has_value?(3) #=> true

# Wskazówka: Zarówno tablice, jak i hasze, są policzalne
# Współdzielą wiele metod takich jak each, map, count, i inne

# Instrukcje warunkowe

if true
  'wyrażenie if'
elsif false
  'wyrażenie if, opcjonalne'
else
  'wyrażenie else, również opcjonalne'
end

for licznik in 1..5
  puts "powtórzenie #{licznik}"
end
#=> powtórzenie 1
#=> powtórzenie 2
#=> powtórzenie 3
#=> powtórzenie 4
#=> powtórzenie 5

# JEDNAKŻE, Nikt nie używa pętli for.
# Zamiast tego, powinno się używać metody "each" i podawać jej blok.
# Blok jest kawałkiem kodu, który możesz podać metodzie podobnej do "each".
# Jest analogiczny do wyrażeń lambda, funkcji anonimowych lub zamknięć w innych
# językach programowania.
#
# Metoda "each" danego zakresu, wykonuje blok dla każdego elementu w zakresie.
# Do bloku zostaje przekazany licznik jako parametr.
# Wykonanie metody "each" z przekazaniem bloku wygląda następująco:

(1..5).each do |licznik|
  puts "powtórzenie #{licznik}"
end
#=> powtórzenie 1
#=> powtórzenie 2
#=> powtórzenie 3
#=> powtórzenie 4
#=> powtórzenie 5

# Możesz również otoczyć blok nawiasami klamrowymi:
(1..5).each { |licznik| puts "powtórzenie #{licznik}" }

# Zawartość struktur danych również może być powtarzana używając each.
tablica.each do |element|
  puts "#{element} jest częścią tablicy"
end
hasz.each do |klucz, wartosc|
  puts "#{klucz} jest #{wartosc}"
end

# Jeśli nadal potrzebujesz indeksum, możesz użyć "each_with_index" i zdefiniować 
# zmienną odpowiadającą indeksowi
tablica.each_with_index do |element, indeks|
  puts "#{element} jest numerem #{indeks} w tablicy"
end

licznik = 1
while licznik <= 5 do
  puts "powtórzenie #{licznik}"
  licznik += 1
end
#=> powtórzenie 1
#=> powtórzenie 2
#=> powtórzenie 3
#=> powtórzenie 4
#=> powtórzenie 5

# W Ruby istnieje dużo pomocnych funkcji wykonujących pętle,
# na przykład "map", "reduce", "inject" i wiele innych. Map,
# w każdym wywołaniu, pobiera tablicę, na której wykonuję pętlę,
# wykonuje kod zapisany za pomocą bloku i zwraca całkowicie nową tablicę.
tablica = [1,2,3,4,5]
podwojone = tablica.map do |element|
  element * 2
end
puts podwojona
#=> [2,4,6,8,10]
puts tablica
#=> [1,2,3,4,5]

ocena = 2

case ocena
when 1
  puts 'Dobra robota, masz wolne'
when 2
  puts 'Następnym razem będziesz miał więcej szczęścia'
when 3
  puts 'Możesz to zrobić lepiej'
when 4
  puts 'Przebrnąłeś'
when 5
  puts 'Oblałeś!'
else
  puts 'Inny system oceniania?'
end
#=> "Następnym razem będziesz miał więcej szczęścia"

# case może również użwać zakresów
ocena = 82
case ocena
when 90..100
  puts 'Hurra!'
when 80...90
  puts 'Dobra robota'
else
  puts 'Oblałeś!'
end
#=> "Dobra robota"

# obsługa błędów:
begin
  # kod, który może wywołać wyjątek
  raise NoMemoryError, 'Zabrakło pamięci.'
rescue NoMemoryError => zmienna_wyjatku
  puts 'Został wywołany NoMemoryError', zmienna_wyjatku
rescue RuntimeError => inna_zmienna_wyjatku
  puts 'Teraz został wywołany RuntimeError'
else
  puts 'To zostanie uruchomione, jeśli nie wystąpi żaden wyjątek'
ensure
  puts 'Ten kod wykona się zawsze'
end

# Funkcje

def podwojenie(x)
  x * 2
end

# Funkcje (i wszystkie bloki) zawsze zwracają wartość ostatniego wyrażenia
podwojenie(2) #=> 4

# Okrągłe nawiady są opcjonalne, gdy wynik jest jednoznaczny
podwojenie 3 #=> 6

podwojenie podwojenie 3 #=> 12

def suma(x, y)
  x + y
end

# Argumenty metod są oddzielone przecinkami
suma 3, 4 #=> 7

suma suma(3, 4), 5 #=> 12

# yield
# Wszystkie metody mają ukryty, opcjonalny parametr bloku,
# który może być wykonany używając słowa kluczowego 'yield'

def otoczenie
  puts '{'
  yield
  puts '}'
end

otoczenie { puts 'hej świecie' }

# {
# hej świecie
# }


# Możesz przekazać blok do funkcji
# "&" oznacza referencję to przekazanego bloku
def goscie(&blok)
  blok.call 'jakis_argument'
end

# Możesz przekazać listę argumentów, które będę przekonwertowane na tablicę
# Do tego służy operator ("*")
def goscie(*tablica)
  tablica.each { |gosc| puts gosc }
end

# Definiowanie klas używając słowa kluczowego class
class Czlowiek

  # Zmienna klasowa. Jest współdzielona przez wszystkie instancje tej klasy.
  @@gatunek = 'H. sapiens'

  # Podstawowe inicjalizowanie
  def initialize(imie, wiek = 0)
    # Przypisanie argumentu do zmiennej danej instancji o nazwie "imie"
    @imie = imie
    # Jeśli nie podano wieku, zostanie użyta domyślna wartość z listy argumentów.
    @wiek = wiek
  end

  # Podstawowa metoda przypisująca wartość
  def imie=(imie)
    @imie = imie
  end

  # Podstawowa metoda pobierająca wartość
  def imie
    @imie
  end

  # Powyższa funkcjonalność może być zastąpiona używając metody attr_accessor w taki sposób
  attr_accessor :imie

  # Metody przypisujące/pobierające mogą być stworzone indywidualnie
  attr_reader :imie
  attr_writer :imie

  # Metody klasowe używają self aby odróżnić się od metody instancji.
  # To może być wywołane na klasie, nie na instancji.
  def self.powiedz(wiadomosc)
    puts wiadomosc
  end

  def gatunek
    @@gatunek
  end
end


# Tworzenie instancji klasy
jim = Czlowiek.new('Jim Halpert')

dwight = Czlowiek.new('Dwight K. Schrute')

# Wywołajmy parę metod
jim.gatunek #=> "H. sapiens"
jim.imie #=> "Jim Halpert"
jim.imie = "Jim Halpert II" #=> "Jim Halpert II"
jim.imie #=> "Jim Halpert II"
dwight.gatunek #=> "H. sapiens"
dwight.imie #=> "Dwight K. Schrute"

# Wywołanie metody klasowej
Czlowiek.powiedz('Cześć') #=> "Cześć"

# Zasięg zmiennej jest definiowany poprzez jej nazwę.
# Zmienne, które zaczynają się na $ mają zasięg globalny
$zmienna = "Jestem zmienną globalną"
defined? $zmienna #=> "global-variable"

# Zmienne zczynające się na @ mają zasięg danej instancji
@zmienna = "Jestem zmienną instancji"
defined? @zmienna #=> "instance-variable"

# Zmienne, które zaczynają się na @@ mają zasięg danej klasy
@@zmienna = "Jestem zmienną klasową"
defined? @@zmienna #=> "class variable"

# Zmienne, które zaczynają się na dużą literę, są stałymi
Zmienna = "Jestem stałą"
defined? Zmienna #=> "constant"

# Klasa jest również obiektem w ruby. Może więc mieć zmienne instancji.
# Zmienna klasowa może być współdzielona między klasą i jej potomstwem.

# podstawowa klasa
class Czlowiek
  @@cokolwiek = 0

  def self.cokolwiek
    @@cokolwiek
  end

  def self.cokolwiek=(wartosc)
    @@cokolwiek = wartosc
  end
end

# klasa pochodna
class Pracownik < Czlowiek
end

Czlowiek.cokolwiek # 0
Pracownik.cokolwiek # 0

Czlowiek.cokolwiek = 2 # 2
Pracownik.cokolwiek # 2

# Zmienna instancji danej klasy nie jest współdzielona przez jej potomstwo.

class Czlowiek
  @cos = 0

  def self.cos
    @cos
  end

  def self.cos=(wartosc)
    @cos = wartosc
  end
end

class Doktor < Czlowiek
end

Czlowiek.cos # 0
Doktor.cos # nil

module PrzykladowyModul
  def cokolwiek
    'cokolwiek'
  end
end

# Włączanie modułów łączy ich metody z metodami instancji klasy
# Rozszerzanie modułów łączy ich metody z metodami klasy

class Osoba
  include PrzykladowyModul
end

class Ksiazka
  extend PrzykladowyModul
end

Osoba.cokolwiek         # => NoMethodError: undefined method `cokolwiek' for Osoba:Class
Osoba.new.cokolwiek     # => 'cokolwiek'
Ksiazka.cokolwiek       # => 'cokolwiek'
Ksiazka.new.cokolwiek   # => NoMethodError: undefined method `cokolwiek'

# Gdy włączamy lub rozszerzamy muduły, wykonywane są tzw. wywołania zwrotne

module PrzykladowyModul
  def self.included(baza)
    baza.extend(MotodyKlasowe)
    baza.send(:include, MetodyInstancji)
  end

  module MotodyKlasowe
    def cos
      'cos'
    end
  end

  module MetodyInstancji
    def xyz
      'xyz'
    end
  end
end

class Cokolwiek
  include PrzykladowyModul
end

Cokolwiek.cos     # => 'cos'
Cokolwiek.xyz     # => NoMethodError: undefined method `xyz'
Cokolwiek.new.cos # => NoMethodError: undefined method `cos'
Cokolwiek.new.xyz # => 'qux'
```

## Dodatkowe źródła
### Polskie

- [Dokumentacja](https://www.ruby-lang.org/pl/documentation/quickstart/)

### Angielskie

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges.
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.
---
category: tool
tool: vim
contributors:
    - ["RadhikaG", "https://github.com/RadhikaG"]
translators:
    - ["Adam Bobowski", "https://github.com/Bobowski"]
lang: pl-pl
filename: LearnVim-pl.txt
---


[Vim](http://www.vim.org)
(Vi IMproved) jest klonem popularnego edytora vi dla systemów Unix. 
Zaprojektowany z myślą o prędkości edycji i zwiększeniu produktywności jest 
wszechobecny na systemach UNIXopodobnych. Posiada wiele skrótów klawiszowych 
do szybkiej nawigacji do wybranych miejsc w plikach oraz szybkiej edycji 
danych fragmentów.

## Podstawy nawigacji w Vim

```
    vim <nazwapliku> # Otwórz <nazwapliku> w vim
    :q               # Zamknij vim
    :w               # Zapisz aktualny plik
    :wq              # Zapisz i wyjdź z vim
    :q!              # Wyjdź bez zapisywania
                     # ! *wymusza* wykonanie :q, dlatego nie wykonuje zapisu
    :x               # Zapisz i wyjdź, krótszy odpowiednik :wq

    u                # Cofnij operację
    CTRL+R           # Powtórz operację

    h                # Przesuń kursor w lewo
    j                # Przesuń kursor w dół
    k                # Przesuń kursor w górę
    l                # Przesuń kursor w prawo

    # Poruszanie w obrębie jednej lini

    0                # Skocz do początku linii
    $                # Skocz do końca linii
    ^                # Skocz do pierwszego niebiałego znaku

    # Wyszukiwanie w tekście

    /slowo           # Zaznacza wszystkie wystąpienia słowa za kursorem
    ?slowo           # Zaznacza wszystkie wystąpienia słowa przed kursorem
    n                # Przemieszcza kursor do następnego wystąpienia słowa
    N                # Przemieszcza kursor do poprzedniego wystąpenia słowa

    :%s/foo/bar/g    # Zamień 'foo' na 'bar' w każdej linii tekstu
    :s/foo/bar/g     # Zamień 'foo' na 'bar' w aktualnej linii

    # Skoki do znaków

    f<znak>          # Skocz do przodu i zatrzymaj się na <znak>
    t<znak>          # Skocz do przodu i zatrzymaj się przed <znak> 

    # Na przykład,    
    f<               # Skocz do przodu i zatrzymaj się na <
    t<               # Skocz do przodu i zatrzymaj się przed <
    
    # Moving by word

    w                # Przesuń kursor do przodu o jedno słowo
    b                # Przesuń kursor do tyłu o jedno słowo
    e                # Przesuń kursor do końca aktualnego słowa

    # Inne znaki do przemieszczania się

    gg               # Skocz na początek pliku
    G                # Skocz na koniec pliku
    :NUM             # Skocz do linii o numerze NUM
    H                # Skocz na górę ekranu
    M                # Skocz na środek ekranu
    L                # Skocz na dół ekranu
```

## Tryby:

Vim oparty jest na koncepcji **trybów**.

Command Mode - (Tryb komend) vim zaczyna w tym trybie, używany do nawigacji i wpisywania komend
Insert Mode  - (Tryb wprowadzania) używany do wprowadzania zmian w pliku
Visual Mode  - (Tryb wizualny) używany do zaznaczania tekstu i wykonywania komend na nim
Ex Mode      - (Tryb Ex) 

```
    i                # Przechodzi to trybu wprowadzania, przed pozycją kursora
    a                # Przechodzi do trybu wprowadzania, za pozycją kursora
    v                # Przechodzi do trybu wizualnego
    :                # Przechodzi do trybu ex
    <esc>            # Wychodzi z dowolnego aktywnego trybu do trybu komend

    # Kopiowanie i wklejanie tekstu

    y                # Skopiuj zaznaczony tekst
    yy               # Skopiuj aktualną linię
    d                # Usuń zaznaczony tekst
    dd               # Usuń aktualną linię
    p                # Wklej skopiowany tekst za kursorem
    P                # Wklej skopiowany tekst przed kursorem
    x                # Usuń znak pod kursorem
```

## 'Gramatyka' vim'a

Vim można traktować jako zbiór komend w formacie 'Akcja-Modyfikator-Obiekt', gdzie:

Akcja       - jedna z dostępnych akcji
Modyfikator - w jaki sposób wykonywana jest akcja
Obiekt      - obiekt na którym wykonywana jest akcja

Kilka ważnych przykładów Akcji, Modyfikatorów i Obiektów:

```
    # 'Akcje'
 
    d                # Usuń
    c                # Zmień
    y                # Skopiuj
    v                # Zaznacz

    # 'Modyfikatory'

    i                # W środku
    a                # Dookoła
    NUM              # Liczba
    f                # Szuka czegoś i zatrzymuje się na tym
    t                # Szuka czegoś i zatrzymuje się przed tym
    /                # Znajduje napis od kursora naprzód
    ?                # Znajduje napis przed kursorem

    # 'Obiekty'

    w                # Słowo
    s                # Zdanie
    p                # Paragraf
    b                # Blok
    
    # Przykładowe 'zdania'

    d2w              # Usuń 2 słowa
    cis              # Zmień w zdaniu
    yip              # Skopiuj paragraf w którym jest kursor
    ct<              # Zamień na <
    d$               # Usuń tekst do końca linii
```

## Pewne skróty i triki

        <!--TODO: Dodać więcej!-->
```
    >                # Zrób wcięcie zaznaczonego bloku
    <                # Usuń wcięcie zaznaczonego bloku
    :earlier 15m     # Przywróć dokument do stanu z przed 15 minut 
    :later 15m       # Odwróć efekt poprzedniej komendy
    ddp              # Zamień kolejnością kolejne linie, dd potem p
    .                # Powtórz poprzednią komendę
```

## Makra

Makra są właściwie nagrywanymi akcjami. Gdy zaczynasz nagrywać makro, nagrywa ono
**każdą** akcję i komendę jaką wykonasz do momentu przerwania nagrywania.
Wywołanie makra wykonuje dokładnie te same operacje w tej samej kolejności.

```
    qa               # Zacznij nagrywać makro 'a'
    q                # Przerwij nagrywanie
    @a               # Odtwórz makro 'a'
```

### Konfiguracja ~/.vimrc

Plik .vimrc może być użyty do skonfigurowania Vim'a przy jego starcie

Poniżej zamieszczono przykładowy plik ~/.vimrc:

```
" Przykładowy ~/.vimrc
" 2016.10 

" Wymagane aby korzystać z opcji iMproved
set nocompatible

" Na podstawie typu pliku włącza inteligentne wcięcia i inne.
filetype indent plugin on

" Włącz podkreślanie składni
syntax on

" Lepsze uzupełnianie składni komend
set wildmenu

" Wyszukiwanie będzie ignorować wielkość liter poza przypadkami gdy użyjemy wielkich liter
set ignorecase
set smartcase

" Po otwarciu pliku gdzie nie jest zdefiniowane zachowanie wcięć
" zostanie zachowane wcięcie takie samo jak w aktualnej linii
set autoindent

" Wyświetlaj numer lini
set number

" Opcje wcięć, zmień w zależności od osobistych upodobań

" Szerokość TAB w spacjach
set tabstop=4

" Liczba spacji w TAB podczas edycji
set softtabstop=4

" Liczba spacji gdy wykonywane są operacje wcięcia (>> i <<)
set shiftwidth=4

" Zamieniaj tabulatory na spacje
set expandtab

" Aktywuj inteligentne tabulatory i spacje do wcięć i wyrównań
set smarttab
```

### Odniesienia [ENG]

[Vim | Home](http://www.vim.org/index.php)

`$ vimtutor`

[A vim Tutorial and Primer](https://danielmiessler.com/study/vim/)

[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about)

[Arch Linux Wiki](https://wiki.archlinux.org/index.php/Vim)
---
language: xml
filename: learnxml-pl.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:  
  - ["Tomasz Janiszewski", "https://github.com/janisz"]  
lang: pl-pl
---

XML (_Extensible Markup Language_) to rozszerzalny język znaczników, stworzony
do przechowywania i transportu danych.

W przeciwieństwie do HTML, XML nie specyfikuje w jaki sposób wyświetlić dane, a
tylko je przechowuje.

* Składnia XML

```xml
<!-- Komentarze w XML wyglądają jak ten -->

<?xml version="1.0" encoding="UTF-8"?>
<ksiegarnia>
  <ksiazka kategoria="GOTOWANIE">
    <tytul lang="pl">Codzienny Włoski</tytul>
    <autor>Giada De Laurentiis</autor>
    <rok>2005</rok>
    <cena>30.00</cena>
  </ksiazka>
  <ksiazka kategoria="DZIECI">
    <tytul lang="pl">Harry Potter</tytul>
    <autor>J K. Rowling</autor>
    <rok>2005</rok>
    <cena>29.99</cena>
  </ksiazka>
  <ksiazka kategoria="WEB">
    <tytul lang="pl">Nauka XML</tytul>
    <autor>Erik T. Ray</autor>
    <rok>2003</rok>
    <cena>39.95</cena>
  </ksiazka>
</ksiegarnia>

<!-- Powyżej jest typowy plik XML.
  Zaczyna się od deklaracji zawierającej metadane (opcjonalne).  

  XML używa drzewiastej struktury. Powyżej, głównym wierzchołkiem jest
  'ksiegarnia' , która zawiera trzy (3) węzły potomne, wszystkie 'ksiazki',
  które zawierają swoje węzły potomne, i tak dalej...

  Węzły są tworzone używające otwierających/zamykających znaczników.
  Węzły potomne znajdują się pomiędzy otwierającym i zamykającym znacznikiem.
-->

<!-- XML przechowuje dwa typy danych
  1 - Atrybuty -> metadane o węźle
      Zazwyczaj parser XML używa tych informacji do przechowywania danych we
      właściwy sposób. Atrybuty nadawane są poprzez wpisanie ich w otwierajacym
      znaczniku.
  2 - Elementy -> to są czyste dane.
      Dane, które parser otrzymuje z pliku XML.
      Elementy są deklarowane pomiędzy otwierajacym i zamykającym znacznikiem,
      bez nawiasów. -->

<!-- Poniższy element ma dwa atrybuty -->
<plik type="gif" id="4293">komputer.gif</plik>


```

* Dobrze sformatowany dokument i walidacja

Dokument XML jest dobrze sformatowany gdy jest syntaktycznie poprawny.
Jednakże możliwe jest wstrzykiwanie większej liczby ograniczeń w dokumencie,
używając definicji takich jak DTD i XML Schema.

Dokument XML, który jest zgodny ze swoją definicją jest poprawny.


Korzystając z tych narzędzi możesz sprawdzić dane zawarte w dokumencie poza
logiką aplikacji.

```xml


<!-- Poniżej jest uproszczona wersja dokumentu księgarni,
  z dodatkową definicją DTD.-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE notatka SYSTEM "Ksiegarnia.dtd">
<ksiegarnia>
  <ksiazka kategoria="GOTOWANIE">
    <tytul >Everyday Italian</tytul>
    <cena>30.00</cena>
  </ksiazka>
</ksiegarnia>

<!-- DTD może wyglądać następująco:-->

<!DOCTYPE notatka
[
<!ELEMENT ksiegarnia (ksiazka+)>
<!ELEMENT ksiazka (tytul,cena)>
<!ATTLIST ksiazka kategoria CDATA "Literatura">
<!ELEMENT tytul (#PCDATA)>
<!ELEMENT cena (#PCDATA)>
]>


<!-- DTD zaczyna się od deklaracji
  Zaczynając od góry, główny węzeł jest zadeklarowany jako wymagający jednego
  lub więcej węzłów potomnych typu 'ksiżka'.
  Każda 'ksiażka' powinna zawierać dokładnie jeden 'tytuł' i 'cene' oraz atrybut
  'kategoria' z 'literaturą' jako wartość domyślna.
  'tytuł' i 'cena' to pola typu parsowalnych zmiennyc znakowych, co oznacza że
  użyte znaczniki zostaną zinterpretowane &lt; zamienione <. -->

<!-- DTD moze być deklarowane wewnątrz pliku XML. -->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE notatka
[
<!ELEMENT ksiegarnia (ksiazka+)>
<!ELEMENT ksiazka (tytul,cena)>
<!ATTLIST ksiazka kategoria CDATA "Literatura">
<!ELEMENT tytul (#PCDATA)>
<!ELEMENT cena (#PCDATA)>
]>

<ksiegarnia>
  <ksiazka kategoria="GOTOWANIE">
    <tytul >Everyday Italian</tytul>
    <cena>30.00</cena>
  </ksiazka>
</ksiegarnia>
```
---
language: pogoscript
contributors:
    - ["Tim Macfarlane", "http://github.com/refractalize"]
filename: learnPogo.pogo
---

Pogoscript is a little language that emphasises readability, DSLs and provides excellent asynchronous primitives for writing connected JavaScript applications for the browser or server.

``` javascript
// defining a variable
water temperature = 24

// re-assigning a variable after its definition
water temperature := 26

// functions allow their parameters to be placed anywhere
temperature at (a) altitude = 32 - a / 100

// longer functions are just indented
temperature at (a) altitude :=
    if (a < 0)
        water temperature
    else
        32 - a / 100

// calling a function
current temperature = temperature at 3200 altitude

// this function constructs a new object with methods
position (x, y) = {
    x = x
    y = y

    distance from position (p) =
        dx = self.x - p.x
        dy = self.y - p.y
        Math.sqrt (dx * dx + dy * dy)
}

// `self` is similar to `this` in JavaScript with the
// exception that `self` isn't redefined in each new
// function definition
// `self` just does what you expect

// calling methods
position (7, 2).distance from position (position (5, 1))

// as in JavaScript, objects are hashes too
position.'x' == position.x == position.('x')

// arrays
positions = [
    position (1, 1)
    position (1, 2)
    position (1, 3)
]

// indexing an array
positions.0.y

n = 2
positions.(n).y

// strings
poem = 'Tail turned to red sunset on a juniper crown a lone magpie cawks.
        Mad at Oryoki in the shrine-room -- Thistles blossomed late afternoon.
        Put on my shirt and took it off in the sun walking the path to lunch.
        A dandelion seed floats above the marsh grass with the mosquitos.
        At 4 A.M. the two middleaged men sleeping together holding hands.
        In the half-light of dawn a few birds warble under the Pleiades.
        Sky reddens behind fir trees, larks twitter, sparrows cheep cheep cheep
        cheep cheep.'

// that's Allen Ginsburg

// interpolation
outlook = 'amazing!'
console.log "the weather tomorrow is going to be #(outlook)"

// regular expressions
r/(\d+)m/i
r/(\d+) degrees/mg

// operators
true @and true
false @or true
@not false
2 < 4
2 >= 2
2 > 1

// plus all the javascript ones

// to define your own
(p1) plus (p2) =
    position (p1.x + p2.x, p1.y + p2.y)

// `plus` can be called as an operator
position (1, 1) @plus position (0, 2)
// or as a function
(position (1, 1)) plus (position (0, 2))

// explicit return
(x) times (y) = return (x * y)

// new
now = @new Date ()

// functions can take named optional arguments
spark (position, color: 'black', velocity: {x = 0, y = 0}) = {
    color = color
    position = position
    velocity = velocity
}

red = spark (position 1 1, color: 'red')
fast black = spark (position 1 1, velocity: {x = 10, y = 0})

// functions can unsplat arguments too
log (messages, ...) =
    console.log (messages, ...)

// blocks are functions passed to other functions.
// This block takes two parameters, `spark` and `c`,
// the body of the block is the indented code after the
// function call

render each @(spark) into canvas context @(c)
    ctx.begin path ()
    ctx.stroke style = spark.color
    ctx.arc (
        spark.position.x + canvas.width / 2
        spark.position.y
        3
        0
        Math.PI * 2
    )
    ctx.stroke ()

// asynchronous calls

// JavaScript both in the browser and on the server (with Node.js)
// makes heavy use of asynchronous IO with callbacks. Async IO is
// amazing for performance and making concurrency simple but it
// quickly gets complicated.
// Pogoscript has a few things to make async IO much much easier

// Node.js includes the `fs` module for accessing the file system.
// Let's list the contents of a directory

fs = require 'fs'
directory listing = fs.readdir! '.'

// `fs.readdir()` is an asynchronous function, so we can call it
// using the `!` operator. The `!` operator allows you to call
// async functions with the same syntax and largely the same
// semantics as normal synchronous functions. Pogoscript rewrites
// it so that all subsequent code is placed in the callback function
// to `fs.readdir()`.

// to catch asynchronous errors while calling asynchronous functions

try
    another directory listing = fs.readdir! 'a-missing-dir'
catch (ex)
    console.log (ex)

// in fact, if you don't use `try catch`, it will raise the error up the
// stack to the outer-most `try catch` or to the event loop, as you'd expect
// with non-async exceptions

// all the other control structures work with asynchronous calls too
// here's `if else`
config =
    if (fs.stat! 'config.json'.is file ())
        JSON.parse (fs.read file! 'config.json' 'utf-8')
    else
        {
            color: 'red'
        }

// to run two asynchronous calls concurrently, use the `?` operator.
// The `?` operator returns a *future* which can be executed to
// wait for and obtain the result, again using the `!` operator

// we don't wait for either of these calls to finish
a = fs.stat? 'a.txt'
b = fs.stat? 'b.txt'

// now we wait for the calls to finish and print the results
console.log "size of a.txt is #(a!.size)"
console.log "size of b.txt is #(b!.size)"

// futures in Pogoscript are analogous to Promises
```

That's it.

Download [Node.js](http://nodejs.org/) and `npm install pogo`.

There is plenty of documentation on [http://pogoscript.org/](http://pogoscript.org/), including a [cheat sheet](http://pogoscript.org/cheatsheet.html), a [guide](http://pogoscript.org/guide/), and how [Pogoscript translates to Javascript](http://featurist.github.io/pogo-examples/). Get in touch on the [google group](http://groups.google.com/group/pogoscript) if you have questions!
---
category: tool
tool: powershell
contributors:
    - ["Wouter Van Schandevijl", "https://github.com/laoujin"]
filename: LearnPowershell.ps1
---

PowerShell is the Windows scripting language and configuration management
framework from Microsoft built on the .NET Framework. Windows 7 and up ship
with PowerShell.  
Nearly all examples below can be a part of a shell script or executed directly
in the shell.

A key difference with Bash is that it is mostly objects that you manipulate
rather than plain text.

[Read more here.](https://technet.microsoft.com/en-us/library/bb978526.aspx)

If you are uncertain about your environment:

```powershell
Get-ExecutionPolicy -List
Set-ExecutionPolicy AllSigned
# Execution policies include:
# - Restricted: Scripts won't run.
# - RemoteSigned: Downloaded scripts run only if signed by a trusted publisher. 
# - AllSigned: Scripts need to be signed by a trusted publisher.
# - Unrestricted: Run all scripts.
help about_Execution_Policies # for more info

# Current PowerShell version:
$PSVersionTable
```

Getting help:

```powershell
# Find commands
Get-Command about_* # alias: gcm
Get-Command -Verb Add
Get-Alias ps
Get-Alias -Definition Get-Process

Get-Help ps | less # alias: help
ps | Get-Member # alias: gm

Show-Command Get-EventLog # Display GUI to fill in the parameters

Update-Help # Run as admin
```

The tutorial starts here:

```powershell
# As you already figured, comments start with #

# Simple hello world example:
echo Hello world!
# echo is an alias for Write-Output (=cmdlet)
# Most cmdlets and functions follow the Verb-Noun naming convention

# Each command starts on a new line, or after a semicolon:
echo 'This is the first line'; echo 'This is the second line'

# Declaring a variable looks like this:
$aString="Some string"
# Or like this:
$aNumber = 5 -as [double]
$aList = 1,2,3,4,5
$aString = $aList -join '--' # yes, -split exists also
$aHashtable = @{name1='val1'; name2='val2'}

# Using variables:
echo $aString
echo "Interpolation: $aString"
echo "$aString has length of $($aString.Length)"
echo '$aString'
echo @"
This is a Here-String
$aString
"@
# Note that ' (single quote) won't expand the variables!
# Here-Strings also work with single quote

# Builtin variables:
# There are some useful builtin variables, like
echo "Booleans: $TRUE and $FALSE"
echo "Empty value: $NULL"
echo "Last program's return value: $?"
echo "Exit code of last run Windows-based program: $LastExitCode"
echo "The last token in the last line received by the session: $$"
echo "The first token: $^"
echo "Script's PID: $PID"
echo "Full path of current script directory: $PSScriptRoot"
echo 'Full path of current script: ' + $MyInvocation.MyCommand.Path
echo "FUll path of current directory: $Pwd"
echo "Bound arguments in a function, script or code block: $PSBoundParameters"
echo "Unbound arguments: $($Args -join ', ')."
# More builtins: `help about_Automatic_Variables`

# Inline another file (dot operator)
. .\otherScriptName.ps1


### Control Flow
# We have the usual if structure:
if ($Age -is [string]) {
	echo 'But.. $Age cannot be a string!'
} elseif ($Age -lt 12 -and $Age -gt 0) {
	echo 'Child (Less than 12. Greater than 0)'
} else {
	echo 'Adult'
}

# Switch statements are more powerful compared to most languages
$val = "20"
switch($val) {
  { $_ -eq 42 }           { "The answer equals 42"; break }
  '20'                    { "Exactly 20"; break }
  { $_ -like 's*' }       { "Case insensitive"; break }
  { $_ -clike 's*'}       { "clike, ceq, cne for case sensitive"; break }
  { $_ -notmatch '^.*$'}  { "Regex matching. cnotmatch, cnotlike, ..."; break }
  { 'x' -contains 'x'}    { "FALSE! -contains is for lists!"; break }
  default                 { "Others" }
}

# The classic for
for($i = 1; $i -le 10; $i++) {
  "Loop number $i"
}
# Or shorter
1..10 | % { "Loop number $_" }

# PowerShell also offers
foreach ($var in 'val1','val2','val3') { echo $var }
# while () {}
# do {} while ()
# do {} until ()

# Exception handling
try {} catch {} finally {}
try {} catch [System.NullReferenceException] {
	echo $_.Exception | Format-List -Force
}


### Providers
# List files and directories in the current directory
ls # or `dir`
cd ~ # goto home

Get-Alias ls # -> Get-ChildItem
# Uh!? These cmdlets have generic names because unlike other scripting
# languages, PowerShell does not only operate in the current directory.
cd HKCU: # go to the HKEY_CURRENT_USER registry hive

# Get all providers in your session
Get-PSProvider


### Pipeline
# Cmdlets have parameters that control their execution:
Get-ChildItem -Filter *.txt -Name # Get just the name of all txt files
# Only need to type as much of a parameter name until it is no longer ambiguous
ls -fi *.txt -n # -f is not possible because -Force also exists
# Use `Get-Help Get-ChildItem -Full` for a complete overview

# Results of the previous cmdlet can be passed to the next as input.
# `$_` is the current object in the pipeline object.
ls | Where-Object { $_.Name -match 'c' } | Export-CSV export.txt
ls | ? { $_.Name -match 'c' } | ConvertTo-HTML | Out-File export.html

# If you get confused in the pipeline use `Get-Member` for an overview
# of the available methods and properties of the pipelined objects:
ls | Get-Member
Get-Date | gm

# ` is the line continuation character. Or end the line with a |
Get-Process | Sort-Object ID -Descending | Select-Object -First 10 Name,ID,VM `
	| Stop-Process -WhatIf

Get-EventLog Application -After (Get-Date).AddHours(-2) | Format-List

# Use % as a shorthand for ForEach-Object
(a,b,c) | ForEach-Object `
	-Begin { "Starting"; $counter = 0 } `
	-Process { "Processing $_"; $counter++ } `
	-End { "Finishing: $counter" }

# Get-Process as a table with three columns
# The third column is the value of the VM property in MB and 2 decimal places
# Computed columns can be written more verbose as:
# `@{name='lbl';expression={$_}`
ps | Format-Table ID,Name,@{n='VM(MB)';e={'{0:n2}' -f ($_.VM / 1MB)}} -autoSize


### Functions
# The [string] attribute is optional.
function foo([string]$name) {
	echo "Hey $name, have a function"
}

# Calling your function
foo "Say my name"

# Functions with named parameters, parameter attributes, parsable documentation
<#
.SYNOPSIS
Setup a new website
.DESCRIPTION
Creates everything your new website needs for much win
.PARAMETER siteName
The name for the new website
.EXAMPLE
New-Website -Name FancySite -Po 5000
New-Website SiteWithDefaultPort
New-Website siteName 2000 # ERROR! Port argument could not be validated
('name1','name2') | New-Website -Verbose
#>
function New-Website() {
	[CmdletBinding()]
	param (
		[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
		[Alias('name')]
		[string]$siteName,
		[ValidateSet(3000,5000,8000)]
		[int]$port = 3000
	)
	BEGIN { Write-Verbose 'Creating new website(s)' }
	PROCESS { echo "name: $siteName, port: $port" }
	END { Write-Verbose 'Website(s) created' }
}


### It's all .NET
# A PS string is in fact a .NET System.String
# All .NET methods and properties are thus available
'string'.ToUpper().Replace('G', 'ggg')
# Or more powershellish
'string'.ToUpper() -replace 'G', 'ggg'

# Unsure how that .NET method is called again?
'string' | gm

# Syntax for calling static .NET methods
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

# Note that .NET functions MUST be called with parentheses
# while PS functions CANNOT be called with parentheses.
# If you do call a cmdlet/PS function with parentheses,
# it is the same as passing a single parameter list
$writer = New-Object System.IO.StreamWriter($path, $true)
$writer.Write([Environment]::NewLine)
$writer.Dispose()

### IO
# Reading a value from input:
$Name = Read-Host "What's your name?"
echo "Hello, $Name!"
[int]$Age = Read-Host "What's your age?"

# Test-Path, Split-Path, Join-Path, Resolve-Path
# Get-Content filename # returns a string[]
# Set-Content, Add-Content, Clear-Content
Get-Command ConvertTo-*,ConvertFrom-*


### Useful stuff
# Refresh your PATH
$env:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + 
	";" + [System.Environment]::GetEnvironmentVariable("Path", "User")

# Find Python in path
$env:PATH.Split(";") | Where-Object { $_ -like "*python*"}

# Change working directory without having to remember previous path
Push-Location c:\temp # change working directory to c:\temp
Pop-Location # change back to previous working directory
# Aliases are: pushd and popd

# Unblock a directory after download
Get-ChildItem -Recurse | Unblock-File

# Open Windows Explorer in working directory
ii .

# Any key to exit
$host.UI.RawUI.ReadKey()
return

# Create a shortcut
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($link)
$Shortcut.TargetPath = $file
$Shortcut.WorkingDirectory = Split-Path $file
$Shortcut.Save()
```


Configuring your shell

```powershell
# $Profile is the full path for your `Microsoft.PowerShell_profile.ps1`
# All code there will be executed when the PS session starts
if (-not (Test-Path $Profile)) {
	New-Item -Type file -Path $Profile -Force
	notepad $Profile
}
# More info: `help about_profiles`
# For a more useful shell, be sure to check the project PSReadLine below
```

Interesting Projects  

* [Channel9](https://channel9.msdn.com/Search?term=powershell%20pipeline#ch9Search&lang-en=en) PowerShell tutorials
* [PSGet](https://github.com/psget/psget) NuGet for PowerShell
* [PSReadLine](https://github.com/lzybkr/PSReadLine/) A bash inspired readline implementation for PowerShell (So good that it now ships with Windows10 by default!)
* [Posh-Git](https://github.com/dahlbyk/posh-git/) Fancy Git Prompt (Recommended!)
* [PSake](https://github.com/psake/psake) Build automation tool
* [Pester](https://github.com/pester/Pester) BDD Testing Framework
* [Jump-Location](https://github.com/tkellogg/Jump-Location) Powershell `cd` that reads your mind
* [PowerShell Community Extensions](http://pscx.codeplex.com/) (Dead)

Not covered  

* WMI: Windows Management Intrumentation (Get-CimInstance)  
* Multitasking: Start-Job -scriptBlock {...}, 
* Code Signing
* Remoting (Enter-PSSession/Exit-PSSession; Invoke-Command)
---
language: prolog
filename: learnprolog.pl
contributors:
    - ["hyphz", "http://github.com/hyphz/"]
---

Prolog is a logic programming language first specified in 1972, and refined into multiple modern implementations.

```
% This is a comment.

% Prolog treats code entered in interactive mode differently
% to code entered in a file and loaded ("consulted").
% This code must be loaded from a file to work as intended.
% Lines that begin with ?- can be typed in interactive mode.
% A bunch of errors and warnings will trigger when you load this file
% due to the examples which are supposed to fail - they can be safely
% ignored.

% Output is based on SWI-prolog 7.2.3. Different Prologs may behave
% differently.

% Prolog is based on the ideal of logic programming.
% A subprogram (called a predicate) represents a state of the world.
% A command (called a goal) tells Prolog to make that state of the world
%   come true, if possible.

% As an example, here is a definition of the simplest kind of predicate:
% a fact.

magicNumber(7).
magicNumber(9).
magicNumber(42).

% This introduces magicNumber as a predicate and says that it is true
% with parameter 7, 9, or 42, but no other parameter. Note that
% predicate names must start with lower case letters. We can now use
% interactive mode to ask if it is true for different values:

?- magicNumber(7).                 % True
?- magicNumber(8).                 % False
?- magicNumber(9).		   % True

% Some older Prologs may display "Yes" and "No" instead of True and
% False.

% What makes Prolog unusual is that we can also tell Prolog to _make_
% magicNumber true, by passing it an undefined variable. Any name
% starting with a capital letter is a variable in Prolog.

?- magicNumber(Presto).              % Presto = 7 ;
				     % Presto = 9 ;
                                     % Presto = 42.

% Prolog makes magicNumber true by assigning one of the valid numbers to
% the undefined variable Presto. By default it assigns the first one, 7.
% By pressing ; in interactive mode you can reject that solution and
% force it to assign the next one, 9. Pressing ; again forces it to try
% the last one, 42, after which it no longer accepts input because this
% is the last solution. You can accept an earlier solution by pressing .
% instead of ;.

% This is Prolog's central operation: unification. Unification is
% essentially a combination of assignment and equality! It works as
% follows:
%  If both sides are bound (ie, defined), check equality.
%  If one side is free (ie, undefined), assign to match the other side.
%  If both sides are free, abort because this can't be resolved.
% The = sign in Prolog represents unification, so:

?- 2 = 3.                            % False - equality test
?- X = 3.	                     % X = 3 - assignment
?- X = 2, X = Y.		     % X = Y = 2 - two assignments
                                     % Note Y is assigned to, even though it is
                                     % on the right hand side, because it is free
?- X = 3, X = 2.                     % False
				     % First acts as assignment and binds X=3
				     % Second acts as equality because X is bound
                                     % Since 3 does not equal 2, gives False
                                     % Thus in Prolog variables are immutable
?- X = 3+2.                          % X = 3+2 - unification can't do arithmetic
?- X is 3+2.                         % X = 5 - "is" does arithmetic.
?- 5 = X+2.			     % This is why = can't do arithmetic -
                                     % because Prolog can't solve equations
?- 5 is X+2.                         % Error. Unlike =, the right hand side of IS
                                     % must always be bound, thus guaranteeing
                                     % no attempt to solve an equation.

% Any unification, and thus any predicate in Prolog, can either:
% Succeed (return True) without changing anything,
%   because an equality-style unification was true
% Succeed (return True) and bind one or more variables in the process,
%   because an assignment-style unification was made true
% or Fail (return False)
%   because an equality-style unification was false
% (Failure can never bind variables)

% The ideal of being able to give any predicate as a goal and have it
% made true is not always possible, but can be worked toward. For
% example, Prolog has a built in predicate plus which represents
% arithmetic addition but can reverse simple additions.

?- plus(1, 2, 3).		     % True
?- plus(1, 2, X).                    % X = 3 because 1+2 = X.
?- plus(1, X, 3).		     % X = 2 because 1+X = 3.
?- plus(X, 2, 3).		     % X = 1 because X+1 = 3.
?- plus(X, 5, Y).		     % Error - although this could be solved,
                                     % the number of solutions is infinite,
                                     % which most predicates try to avoid.

% When a predicate such as magicNumber can give several solutions, the
% overall compound goal including it may have several solutions too.

?- magicNumber(X), plus(X,Y,100).    % X = 7, Y = 93 ;
                                     % X = 9, Y = 91 ;
                                     % X = 42, Y = 58 .
% Note: on this occasion it works to pass two variables to plus because
% only Y is free (X is bound by magicNumber).

% However, if one of the goals is fully bound and thus acts as a test,
% then solutions which fail the test are rejected.
?- magicNumber(X), X > 40.           % X = 42
?- magicNumber(X), X > 100.          % False

% To see how Prolog actually handles this, let's introduce the print
% predicate. Print always succeeds, never binds any variables, and
% prints out its parameter as a side effect.

?- print("Hello").                   % "Hello" true.
?- X = 2, print(X).                  % 2 true.
?- X = 2, print(X), X = 3.	     % 2 false - print happens immediately when
				     % it is encountered, even though the overall
				     % compound goal fails (because 2 != 3,
                                     % see the example above).

% By using Print we can see what actually happens when we give a
% compound goal including a test that sometimes fails.
?- magicNumber(X), print(X), X > 40. % 7 9 42 X = 42 .

% MagicNumber(X) unifies X with its first possibility, 7.
% Print(X) prints out 7.
% X > 40 tests if 7 > 40. It is not, so it fails.
% However, Prolog remembers that magicNumber(X) offered multiple
% solutions. So it _backtracks_ to that point in the code to try
% the next solution, X = 9.
% Having backtracked it must work through the compound goal
% again from that point including the Print(X). So Print(X) prints out
% 9.
% X > 40 tests if 9 > 40 and fails again.
% Prolog remembers that magicNumber(X) still has solutions and
% backtracks. Now X = 42.
% It works through the Print(X) again and prints 42.
% X > 40 tests if 42 > 40 and succeeds so the result bound to X
% The same backtracking process is used when you reject a result at
% the interactive prompt by pressing ;, for example:

?- magicNumber(X), print(X), X > 8.  % 7 9 X = 9 ;
				     % 42 X = 42.

% As you saw above we can define our own simple predicates as facts.
% More complex predicates are defined as rules, like this:

nearby(X,Y) :- X = Y.
nearby(X,Y) :- Y is X+1.
nearby(X,Y) :- Y is X-1.

% nearby(X,Y) is true if Y is X plus or minus 1.
% However this predicate could be improved. Here's why:

?- nearby(2,3).			      % True ; False.
% Because we have three possible definitions, Prolog sees this as 3
% possibilities. X = Y fails, so Y is X+1 is then tried and succeeds,
% giving the True answer. But Prolog still remembers there are more
% possibilities for nearby() (in Prolog terminology, "it has a
% choice point") even though "Y is X-1" is doomed to fail, and gives us
% the option of rejecting the True answer, which doesn't make a whole
% lot of sense.

?- nearby(4, X).                      % X = 4 ;
                                      % X = 5 ;
                                      % X = 3. Great, this works
?- nearby(X, 4).		      % X = 4 ;
				      % error
% After rejecting X = 4 prolog backtracks and tries "Y is X+1" which is
% "4 is X+1" after substitution of parameters. But as we know from above
% "is" requires its argument to be fully instantiated and it is not, so
% an error occurs.

% One way to solve the first problem is to use a construct called the
% cut, !, which does nothing but which cannot be backtracked past.

nearbychk(X,Y) :- X = Y, !.
nearbychk(X,Y) :- Y is X+1, !.
nearbychk(X,Y) :- Y is X-1.

% This solves the first problem:
?- nearbychk(2,3).                    % True.

% But unfortunately it has consequences:
?- nearbychk(2,X).		      % X = 2.
% Because Prolog cannot backtrack past the cut after X = Y, it cannot
% try the possibilities "Y is X+1" and "Y is X-1", so it only generates
% one solution when there should be 3.
% However if our only interest is in checking if numbers are nearby,
% this may be all we need, thus the name nearbychk.
% This structure is used in Prolog itself from time to time (for example
% in list membership).

% To solve the second problem we can use built-in predicates in Prolog
% to verify if a parameter is bound or free and adjust our calculations
% appropriately.
nearby2(X,Y) :- nonvar(X), X = Y.
nearby2(X,Y) :- nonvar(X), Y is X+1.
nearby2(X,Y) :- nonvar(X), Y is X-1.
nearby2(X,Y) :- var(X), nonvar(Y), nearby2(Y,X).

% We can combine this with a cut in the case where both variables are
% bound, to solve both problems.
nearby3(X,Y) :- nonvar(X), nonvar(Y), nearby2(X,Y), !.
nearby3(X,Y) :- nearby2(X,Y).

% However when writing a predicate it is not normally necessary to go to
% these lengths to perfectly support every possible parameter
% combination. It suffices to support parameter combinations we need to
% use in the program. It is a good idea to document which combinations
% are supported. In regular Prolog this is informally in structured
% comments, but in some Prolog variants like Visual Prolog and Mercury
% this is mandatory and checked by the compiler.

% Here is the structured comment declaration for nearby3:

%%	nearby3(+X:Int, +Y:Int) is semidet.
%%	nearby3(+X:Int, -Y:Int) is multi.
%%	nearby3(-X:Int, +Y:Int) is multi.

% For each variable we list a type. The + or - before the variable name
% indicates if the parameter is bound (+) or free (-). The word after
% "is" describes the behaviour of the predicate:
%   semidet - can succeed once or fail
%     ( Two specific numbers are either nearby or not )
%   multi - can succeed multiple times but cannot fail
%     ( One number surely has at least 3 nearby numbers )
%  Other possibilities are:
%    det - always succeeds exactly once (eg, print)
%    nondet - can succeed multiple times or fail.
% In Prolog these are just structured comments and strictly informal but
% extremely useful.

% An unusual feature of Prolog is its support for atoms. Atoms are
% essentially members of an enumerated type that are created on demand
% whenever an unquoted non variable value is used. For example:
character(batman).           % Creates atom value batman
character(robin).	     % Creates atom value robin
character(joker).            % Creates atom value joker
character(darthVader).	     % Creates atom value darthVader
?- batman = batman.	     % True - Once created value is reused
?- batman = batMan.          % False - atoms are case sensitive
?- batman = darthVader.      % False - atoms are distinct

% Atoms are popular in examples but were created on the assumption that
% Prolog would be used interactively by end users - they are less
% useful for modern applications and some Prolog variants abolish them
% completely. However they can be very useful internally.

% Loops in Prolog are classically written using recursion.
% Note that below, writeln is used instead of print because print is
% intended for debugging.

%%	countTo(+X:Int) is det.
%%	countUpTo(+Value:Int, +Limit:Int) is det.
countTo(X) :- countUpTo(1,X).
countUpTo(Value, Limit) :- Value = Limit, writeln(Value), !.
countUpTo(Value, Limit) :- Value \= Limit, writeln(Value),
	NextValue is Value+1,
	countUpTo(NextValue, Limit).

?- countTo(10).                        % Outputs 1 to 10

% Note the use of multiple declarations in countUpTo to create an
% IF test. If Value = Limit fails the second declaration is run.
% There is also a more elegant syntax.

%%	countUpTo2(+Value:Int, +Limit:Int) is det.
countUpTo2(Value, Limit) :- writeln(Value),
	Value = Limit -> true ; (
			     NextValue is Value+1,
			     countUpTo2(NextValue, Limit)).

?- countUpTo2(1,10).		      % Outputs 1 to 10

% If a predicate returns multiple times it is often useful to loop
% through all the values it returns. Older Prologs used a hideous syntax
% called a "failure-driven loop" to do this, but newer ones use a higher
% order function.

%%	countTo2(+X:Int) is det.
countTo2(X) :- forall(between(1,X,Y),writeln(Y)).

?- countTo2(10).                      % Outputs 1 to 10

% Lists are given in square brackets. Use memberchk to check membership.
% A group is safe if it doesn't include Joker or does include Batman.
%%	safe(Group:list(atom)) is det.
safe(Group) :- memberchk(joker, Group) -> memberchk(batman, Group) ; true.

?- safe([robin]).		      % True
?- safe([joker]).                     % False
?- safe([joker, batman]).             % True

% The member predicate works like memberchk if both arguments are bound,
% but can accept free variables and thus can be used to loop through
% lists.

?- member(X, [1,2,3]).                  % X = 1 ; X = 2 ; X = 3 .
?- forall(member(X,[1,2,3]),
	  (Y is X+1, writeln(Y))).      % 2 3 4

% The maplist function can be used to generate lists based on other
% lists. Note that the output list is a free variable, causing an
% undefined value to be passed to plus, which is then bound by
% unification. Also notice the use of currying on the plus predicate -
% it's a 3 argument predicate, but we specify only the first, because
% the second and third are filled in by maplist.

?- maplist(plus(1), [2,3,4], Output).   % Output = [3, 4, 5].
```

##Ready For More?

* [SWI-Prolog](http://www.swi-prolog.org/)
---
category: tool
tool: amd
contributors:
    - ["Frederik Ring", "https://github.com/m90"]
translators:
    - ["Felipe Tarijon", "http://nanoincub.com/"]
lang: pt-br
filename: learnamd-pt.js
---

## Começando com AMD

A API de Definição de Módulos Assíncrona **Asynchronous Module Definition** 
especifica um mecanismo para definição de módulos em JavaScript para os quais o
módulo e suas dependências podem ser carregados de forma assíncrona. Isso é
particularmente bem adequado para o ambiente do browser onde o carregamento de
módulos de forma síncrona fica sujeito a problemas de performance, usabilidade,
debugging e problemas de acesso em requisições cross-domain.

### Conceito básico
```javascript
// O básico da API de AMD consiste de nada mais que dois métodos: `define` e `require`
// e isso é tudo sobre a definição de módulo e consumo:
// `define(id?, dependências?, factory)` define um módulo
// `require(dependências, callback)` importa uma série de dependências e
// consome elas no callback passado como parâmetro.

// Vamos começar usando o define para definir um novo módulo
// que não tem dependências. Nós vamos fazer isso passando um nome
// e uma função factory para definir:
define('awesomeAMD', function(){
  var isAMDAwesome = function(){
    return true;
  };
  // O valor retornado da função de factory do módulo é
  // o que os outros módulos ou chamadas de require irão
  // receber quando requisitarem nosso módulo `awesomeAMD`.
  // O valor exportado pode ser qualquer coisa, (construtor) funções,
  // objetos, primitives, até mesmo undefined (apesar de que não irão ajudar muito).
  return isAMDAwesome;
});

// Agora, vamos definir outro módulo que depende do nosso módulo `awesomeAMD`.
// Perceba que existe um argumento adicional definindo nossas dependências do
// módulo agora:
define('loudmouth', ['awesomeAMD'], function(awesomeAMD){
  // dependências serão passadas como argumentos da factory
  // na ordem que elas forem especificadas
  var tellEveryone = function(){
    if (awesomeAMD()){
      alert('Isso é tãaaao loko!');
    } else {
      alert('Bem estúpido, né não?');
    }
  };
  return tellEveryone;
});

// Agora que nós sabemos como usar o define, vamos usar o `require` para
// começar nosso programa. A assinatura do `require` é `(arrayDedependências, callback)`.
require(['loudmouth'], function(loudmouth){
  loudmouth();
});

// Para fazer esse tutorial executável, vamos implementar uma versão muito básica
// (não-assíncrona) de AMD bem aqui nesse lugar:
function define(nome, deps, factory){
  // perceba como os módulos sem dependências são manipulados
  define[nome] = require(factory ? deps : [], factory || deps);
}

function require(deps, callback){
  var args = [];
  // primeiro vamos recuperar todas as dependências necessárias
  // pela chamada requerida
  for (var i = 0; i < deps.length; i++){
    args[i] = define[deps[i]];
  }
  // corresponder todas as dependências da função de callback
  return callback.apply(null, args);
}
// você pode ver esse código em ação aqui: http://jsfiddle.net/qap949pd/
```

### Uso na vida real com require.js

Em contraste com o exemplo introdutório, `require.js` (a biblioteca mais popular de AMD) na verdade implementa o **A** do **AMD**, permitindo que você carregue os módulos e suas
dependências via XHR:

```javascript
/* file: app/main.js */
require(['modules/algumaClasse'], function(AlgumaClasse){
  // o callback é deferido até que a dependencia seja carregada
  var coisa = new AlgumaClasse();
});
console.log('Então aqui estamos nós, esperando!'); // isso vai rodar primeiro
```

Por convenção, você geralmente guarda um módulo em um arquivo. `require.js` pode resolver nome de módulos baseado no caminho das pastas, então você não precisa nomear os seus módulos, mas sim simplesmente referenciar eles usando sua origem. No exemplo `algumaClasse` é adotado a pasta `modules`, relativa a configuração da sua `baseUrl`:

* app/
  * main.js
  * modules/
    * algumaClasse.js
    * algunsHelpers.js
    * ...
  * daos/
    * coisas.js
    * ...

Isso significa que nós podemos definir `algumaClasse` sem especificar o id de um módulo:

```javascript
/* arquivo: app/modules/algumaClasse.js */
define(['daos/coisas', 'modules/algunsHelpers'], function(coisasDao, helpers){
  // definição de módulo, claro, irá acontecer também de forma assíncrona
  function AlgumaClasse(){
    this.metodo = function(){/**/};
    // ...
  }
  return AlgumaClasse;
});
```
Para alterar o comportamento padrão de mapeamento de caminho de pastas utilize
`requirejs.config(configObj)` em seu `main.js`:

```javascript
/* arquivo: main.js */
requirejs.config({
  baseUrl : 'app',
  paths : {
    // você pode também carregar módulos de outros locais
    jquery : '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
});
require(['jquery', 'coolLibFromBower', 'modules/algunsHelpers'], function($, coolLib, helpers){
  // um arquivo `main` precisa chamar o require pelo menos uma vez,
  // caso contrário, o código jamais rodará
  coolLib.facaAlgoDoidoCom(helpers.transform($('#foo')));
});
```
Apps baseados em `require.js` geralmente terão u´m único ponto de acesso (`main.js`) que é passado à tag script do `require.js` como um data-attribute. Ele vai ser automaticamente carregado e executado com o carregamento da página:

```html
<!DOCTYPE html>
<html>
<head>
  <title>Umas 100 tags de script? Nunca mais!</title>
</head>
<body>
  <script src="require.js" data-main="app/main"></script>
</body>
</html>
```

### Otimizando um projeto inteiro utilizando r.js

Muitas pessoas preferem usar AMD para sanar a organização do código durante o desenvolvimento, mas continuam querendo colocar um único arquivo de script em produção ao invés de realizarem centenas de requisições XHRs no carregamento da página.

`require.js` vem com um script chamado `r.js` (que você vai provavelmente rodar em node.js, embora Rhino suporte também) que você pode analisar o gráfico de dependências de seu projeto, e fazer em um único arquivo contendo todos os seus módulos (corretamente nomeados), minificados e prontos para serem consumidos.

Instale-o utilizando `npm`:
```shell
$ npm install requirejs -g
```

Agora você pode alimentá-lo com um arquivo de configuração:
```shell
$ r.js -o app.build.js
```

Para o nosso exemplo acima a configuração pode ser essa:
```javascript
/* file : app.build.js */
({
  name : 'main', // nome do ponto de acesso
  out : 'main-built.js', // nome o arquivo para gravar a saída
  baseUrl : 'app',
  paths : {
    // `empty:` fala para o r.js que isso ainda deve ser baixado da CDN, usando
    // o local especificado no `main.js`
    jquery : 'empty:',
    coolLibFromBower : '../bower_components/cool-lib/coollib'
  }
})
```

Para usar o arquivo gerado, em produção, simplesmente troque o `data-main`:
```html
<script src="require.js" data-main="app/main-built"></script>
```

Uma incrível e detalhada visão geral [de build options](https://github.com/jrburke/r.js/blob/master/build/example.build.js) está disponível no repositório do GitHub.

### Tópicos não abordados nesse tutorial
* [Plugins de carregamento / transforms](http://requirejs.org/docs/plugins.html)
* [CommonJS style carregamento e exportação](http://requirejs.org/docs/commonjs.html)
* [Configuração avançada](http://requirejs.org/docs/api.html#config)
* [Shim configuration (carregando módulos sem AMD)](http://requirejs.org/docs/api.html#config-shim)
* [Carregando e otimizando CSS com require.js](http://requirejs.org/docs/optimization.html#onecss)
* [Usando almond.js para builds](https://github.com/jrburke/almond)

### Outras leituras:

* [Especificação oficial](https://github.com/amdjs/amdjs-api/wiki/AMD)
* [Por quê AMD?](http://requirejs.org/docs/whyamd.html)
* [Universal Module Definition](https://github.com/umdjs/umd)

### Implementações:

* [require.js](http://requirejs.org)
* [dojo toolkit](http://dojotoolkit.org/documentation/tutorials/1.9/modules/)
* [cujo.js](http://cujojs.com/)
* [curl.js](https://github.com/cujojs/curl)
* [lsjs](https://github.com/zazl/lsjs)
* [mmd](https://github.com/alexlawrence/mmd)
---
language: asciidoc
contributors:
    - ["Ryan Mavilia", "http://unoriginality.rocks/"]
translators:
    - ["David Lima", "https://github.com/davelima"]
lang: pt-br
filename: asciidoc-pt.md
---

AsciiDoc é uma linguagem de marcação similar ao Markdown e pode ser
usada para qualquer coisa, de livros até blogs. Criada em 2002 por
Stuart Rackham, a linguagem é simples mas facilita muito a customização.

Cabeçalho do documento

Cabeçalhos são opcionais e não podem conter linhas em branco.
Devem estar separados do conteúdo com pelo menos uma linha em branco.

Apenas Título

```
= Título do documento

Primeira sentência do documento.
```

Título e Autor

```
= Título do Documento
Nome Sobrenome <nome.sobrenome@learnxinyminutes.com>

Início do documento.
```

Múltiplos Autores

```
= Título do Documento
John Doe <john@go.com>; Jane Doe<jane@yo.com>; Black Beard <beardy@pirate.com>

Início do documento com múltiplos autores.
```

Linhas de revisão (requer uma linha de autor)

```
= Documento V1
Potato Man <chip@crunchy.com>
v1.0, 2016-01-13

Este artigo sobre batatas será divertido.
```

Parágrafos

```
Você não precisa fazer nada especial para criar um parágrafo.

Adicione uma linha em branco entre os parágrafos para separá-los.

Para criar uma linha em branco adicione um +
e você terá uma quebra de linha!
```

Formatando texto

```
_underscore é itálico_
*asterisco é negrito*
*_você pode combinar efeitos_*
`use crase para fonte monoespaçada`
`*fonte monoespaçada em negrito*`
```

Título de seções

```
= Nível 0 (Use apenas no cabeçalho do documento)

== Nível 1 <h2>

=== Nível 2 <h3>

==== Nível 3 <h4>

===== Nível 4 <h5>

====== Nível 5 <h6>

======= Nível 6  <h7>

```

Listas

Para criar uma lista com marcadores use asteriscos.

```
* foo
* bar
* baz
```

Para criar uma lista númerada use pontos.

```
. item 1
. item 2
. item 3
```

Você pode criar listas dentro de listas adicionando
asteriscos ou pontos extras em até 5 níveis.

```
* foo 1
** foo 2
*** foo 3
**** foo 4
***** foo 5

. foo 1
.. foo 2
... foo 3
.... foo 4
..... foo 5
```
---
category: Algorithms & Data Structures
name: Asymptotic Notation
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
    - ["João Farias", "https://github.com/JoaoGFarias"]
lang: pt-br
---

# Notação Assintótica

## O que é?

Notação Assintótica é uma linguagem que nos permite analisar o tempo de execução
 de um algoritmo através da indentificação de seu comportamento com o
 crescimento da entrada oferecida. Isso também é conhecido como taxa de
 crescimento do algoritmo. O algoritmo de repente torna-se lento quando o
 tamanho da entrada cresce? O algoritmo mantém, em geral, seu tempo de execução
 rápido mesmo com aumento da entrada? Notação Assintótica nos dá a habilidade de
 responder estas questões.

## Quais são as alternativas para responder a estas questões?

Um modo seria contar o número de operações primitivas com diferentes tamanhos de
 entrada. Apesar desta ser uma solução válida, o trabalho que ela requer, mesmo para algoritmos simples, não a justifica.

 Outro modo é fisicamente medir a quantidade de tempo que um algoritmo requer
 para terminar com diferentes tamanhos de entrada. Entretanto, a precisão e
 relatividade (tempo obtido seria relativo apenas à máquina onde ocorreu a
   execução) deste método está limitado a variáveis de ambiente, como hardware,
   poder de processamento, etc.

## Tipos de Notação Assintótica

Na primeira seção desse documento, descrevemos como Notação Assintótica identifica o comportamento de um algoritmo
 a medida que o tamanho da entrada cresce. Imaginemos um algoritmo como uma função
 *f*, *n* como o tamanho da entrada e *f(n)* sendo o tempo de execução. Então,
 para dado algoritmo *f*, com entrada de tamanho *n*, você terá tempo de execução
 *f(n)*. Isto resulta em um gráfico onde a coordernada Y é o tempo de execução
, a coordernada X representa o tamanho da entrada e os pontos representao o tempo
de execução para dado tamanho de entrada.

Você pode representar a função, ou o algoritmo, com Notação Assintótica de várias
maneiras. Você pode representar um algoritmo nas formas de Melhor Caso, Pior Caso
ou Caso Médio.
A maneira mais comum de analisar um algoritmo é pelo Pior Caso. Você tipicamente
não avalia o melhor caso, porque essas condições não são atingidas com frequência.
Um bom exemplo disto seria em algoritmos de ordenação; especificamente, na adição
de elementos à árvores. O melhor caso na maioria de algoritmos pode ser de apenas
uma operação. Entretanto, na maioria dos casos, o elemento a ser adicionado terá
que percorrer a árvore de forma apropriada, o que pode causar a analise de um
ramo inteiro.
Este é o pior caso, e isto é o que você está se preparando.

### Tipos de funções, limites e simplificação

```
Função Logarítmica - log n
Função Linear - an + b
Função Quadrática - an^2 + bn + c
Função Polinomial - an^z + . . . + an^2 + a*n^1 + a*n^0, onde *z* é uma constante
Função Exponencial -  a^n, onde a é alguma constante
```
Estas são as funções básicas de crescimento usadas em várias notações. A lista
 começa com a de crescimento mais lento (logarítima, a de execução mais rápida)
e segue para a de crescimento mais rápido (exponencial, de execução mais lenta).
Repare que enquando *n*, a entrada, cresce, cada uma dessas funções cresce mais
rápido que quadrático, polinimial e exponencial, comparadas com logaritma e linear.

Uma nota extremamente importante para notações é tentar usar os termos mais simples.
Isto significa descartar constantes e termos de ordem mais baixa, pois quando o
tamanho da entrada cresce para o infinito (limites matemáticos), os termos de ordem
mais baixa e constantes tornam-se irrelevantes. Por exemplo, se você tiver uma
constante muito grande, 2^9001, a simplificação não afeterá sua notação.

Já que queremos as formas mais simples, mudemos nossa tabela um pouco...

```
Função Logarítmica - log n
Função Linear - n
Função Quadrática - n^2
Função Polinomial - n^z, onde *z* é uma constante
Função Exponencial - a^n, onde *a* é uma constante
```

### Big-O

Big-O, também escrita como O, é uma Notação Assintótica para o pior caso. Digamos
*f(n)* seja o tempo de exeução de um algoritmo e *g(n)) um tempo de complexidade
arbritário que você quer relacionar com seu algoritmo. *f(n)* é O(g(n)), se, para
quando constante real c (c > 0), *f(n)* <= *c g(n)* para todo tamanho de entrada
n (n > 0).


*Exemplo 1*

```
f(n) = 3log n + 100
g(n) = log n
```

`f(n)` é O(g(n))?

`3 log n + 100` é  O(log n)?

Vejamos a definição de Big-O:

```
3log n + 100 <= c * log n
```

Há alguma constante c que satisfaça a definição para todo n?

```
3log n + 100 <= 150 * log n, n > 2 (Indefinido em n = 1)
```

Sim! A definição de Big-I for atentida, portante `f(n)` é `O(g(n))`.

*Exemplo 2*

```
f(n) = 3*n^2
g(n) = n
```

`f(n)` é O(g(n))?

`3 * n^2` é O(n)?
Vejamos a definição de Big-O:

```
3 * n^2 <= c * n
```

Há alguma constante c que satisfaça a definição para todo n?

Não, não há. `f(n)` não é O(g(n)).

### Big-Omega
Big-Omega, também escrita como Ω, é uma Notação Assintótica para o melhor caso.

`f(n)`é Ω(g(n)), se para qualquer constante real c (c > 0), `f(n)` é >= `c g(n)` para todo tamanho de entrada n (n > 0).

Sinta-se livre para adicionar mais exemplos. Big-O é a notação primária usada para medir complexidade de algoritmos.

### Notas Finais
É difícil manter esse tipo de tópico curto e você deveria ler os livros e artigos listados abaixo. Eles cobrem muito mais profundamente definições e exemplos. Mais x='Algoritms & Data Structures' virá; teremos um documento sobre analisar código em breve.

## Livros

* [Algorithms](http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X)
* [Algorithm Design](http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651)

## Artigos Online

* [MIT](http://web.mit.edu/16.070/www/lecture/big_o.pdf)
* [KhanAcademy](https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation)
---
category: Algorithms & Data Structures
name: Asymptotic Notation
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators: 
    - ["Carolina Knoll", "http://github.com/carolinaknoll"]
lang: pt-br
---

# Aprenda X em Y minutos
## Onde X=Notação Assintótica

# Notações Assintóticas
## O que são?

Notações assintóticas são notações matemáticas que nos permitem analisar tempo de execução  
de um algoritmo, identificando o seu comportamento de acordo como o tamanho de entrada para  
o algoritmo aumenta. Também é conhecido como taxa de "crescimento" de um algoritmo. O algoritmo  
simplesmente se torna incrivelmente lento conforme o seu tamanho aumenta? Será que pode-se na  
maior parte manter o seu tempo de execução rápido mesmo quando o tamanho de entrada aumenta?  
A notação assintótica nos dá a capacidade de responder a essas perguntas. 

## Além desta, existem outras alternativas para responder a essas perguntas?

Uma forma seria a de contar o número de operações primitivas em diferentes tamanhos de entrada. 
Embora esta seja uma solução válida, a quantidade de trabalho necessário, mesmo para algoritmos 
simples, não justifica a sua utilização.

Outra maneira é a de medir fisicamente a quantidade de tempo que leva para se executar um algoritmo 
de diferentes tamanhos. No entanto, a precisão e a relatividade (já que tempos obtidos só teriam 
relação à máquina em que eles foram testados) deste método estão ligadas a variáveis ambientais, 
tais como especificações de hardware, poder de processamento, etc.

## Tipos de Notação Assintótica

Na primeira seção deste documento nós descrevemos como uma notação assintótica identifica o comportamento  
de um algoritmo como as alterações de tamanho de entrada (input). Imaginemos um algoritmo como uma função  
f, n como o tamanho da entrada, e f (n) sendo o tempo de execução. Assim, para um determinado algoritmo f,  
com tamanho de entrada n você obtenha algum tempo de execução resultante f (n). Isto resulta num gráfico,  
em que o eixo Y representa o tempo de execução, o eixo X é o tamanho da entrada, e os pontos marcados são  
os resultantes da quantidade de tempo para um dado tamanho de entrada.

Pode-se rotular uma função ou algoritmo com uma notação assintótica de diversas maneiras diferentes.  
Dentre seus exemplos, está descrever um algoritmo pelo seu melhor caso, pior caso, ou caso equivalente.  
O mais comum é o de analisar um algoritmo pelo seu pior caso. Isso porque você normalmente não avaliaria  
pelo melhor caso, já que essas condições não são as que você está planejando. Um bom exemplo disto é o de  
algoritmos de ordenação; especificamente, a adição de elementos a uma estrutura de tipo árvore. O melhor  
caso para a maioria dos algoritmos pode ser tão simples como uma única operação. No entanto, na maioria   
dos casos, o elemento que você está adicionando terá de ser ordenado de forma adequada através da árvore,  
o que poderia significar a análise de um ramo inteiro. Este é o pior caso, e é por ele que precisamos seguir.  

### Tipos de funções, limites, e simplificação

```
Função Logaritmica - log n  
Função Linear - an + b  
Função Quadrática - an^2 + bn + c  
Função Polinomial - an^z + . . . + an^2 + a*n^1 + a*n^0, onde z é uma constante  
Função Exponencial - a^n, onde a é uma constante  
```

Estas são algumas classificações básicas de crescimento de função usados em várias notações. A lista  
começa com a função crescimento mais lento (logarítmica, com tempo de execução mais rápido) e vai até  
a mais rápida (exponencial, com tempo de execução mais lento). Observe que 'n', ou nossa entrada,  
cresce em cada uma dessas funções, e o resultado claramente aumenta muito mais rapidamente em função  
quadrática, polinomial e exponencial, em comparação com a logarítmica e a linear.

Uma observação de boa importância é que, para as notações a serem discutidas, deve-se fazer o melhor  
para utilizar termos mais simples. Isto significa desrespeitar constantes, e simplificar termos de  
ordem, porque, como o tamanho da entrada (ou n no nosso f (n) exemplo) aumenta infinitamente (limites  
matemáticos), os termos em ordens mais baixas e constantes são de pouca ou nenhuma importância. Dito  
isto, se você possui constantes com valor 2^9001, ou alguma outra quantidade ridícula, inimaginável,  
perceberá que a simplificação distorcerá a precisão de sua notação.

Já que nós queremos a forma mais simples, vamos modificar nossas funções um pouco.

```
Logaritmica - log n  
Linear - n  
Quadrática - n^2  
Polinomial - n^z, onde z é uma constante  
Exponencial - a^n, onde a é uma constante
```

### O Grande-O

Grande-O, geralmente escrita como O, é uma Notação Assintótica para o pior caso para uma dada função. Digamos  
que `f(n)` é o tempo de execução de seu algoritmo, e `g(n)` é uma complexidade de tempo arbitrário que você está  
tentando se relacionar com o seu algoritmo. `f(n)` será O(g(n)), se, por qualquer constante real c (c > 0),  
`f(n)` <= `c g(n)` para cada tamanho de entrada n (n > 0).  

*Exemplo 1*

```
f(n) = 3log n + 100  
g(n) = log n
```

É `f(n)` um O(g(n))?  
É 3 `log n + 100` igual a O(log n)?  
Vamos checar na definição de Grande-O.  

```
3log n + 100 <= c * log n
```

Existe alguma constante c que satisfaça isso para todo n?

```
3log n + 100 <= 150 * log n, n > 2 (indefinido em n = 1)
```

Sim! A definição de Grande-O foi satisfeita. Sendo assim, `f(n)` é O(g(n)).

*Exemplo 2*

```
f(n) = 3 * n^2
g(n) = n
```

É `f(n)` um O(g(n))? 
É `3 * n^2` um O(n)? 
Vamos ver na definição de Grande-O.

```
3 * n^2 <= c * n
```

Existe alguma constante que satisfaça isso para todo n? 
Não, não existe. `f(n)` NÃO É O(g(n)).

### Grande-Omega

Grande-Omega, comumente escrito como Ω, é uma Notação Assintótica para o melhor caso, ou  
uma taxa de crescimento padrão para uma determinada função.

`f(n)` é Ω(g(n)), se, por qualquer constante c real (c > 0), `f(n)` é >= `c g(n)` para cada  
tamanho de entrada n (n > 0).

Sinta-se livre para pesquisar recursos adicionais e obter mais exemplos sobre este assunto!  
Grande-O é a notação primária utilizada para tempo de execução de algoritmos, de modo geral.  

### Notas de Finalização

É complicado exibir este tipo de assunto de forma tão curta, então é definitivamente recomendado  
pesquisar além dos livros e recursos on-line listados. Eles serão capazes de analisar o assunto com  
uma profundidade muito maior, além de ter definições e exemplos. Mais sobre onde X="Algoritmos e  
Estruturas de Dados" está a caminho: Haverá conteúdo focado na análise de exemplos de códigos reais  
em breve. 

## Livros

* [Algorithms] (http://www.amazon.com/Algorithms-4th-Robert-Sedgewick/dp/032157351X)  
* [Algorithm Design] (http://www.amazon.com/Algorithm-Design-Foundations-Analysis-Internet/dp/0471383651)

## Recursos Online

* [MIT] (http://web.mit.edu/16.070/www/lecture/big_o.pdf)  
* [KhanAcademy] (https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
translators:
    - ["Davidson Mizael", "https://github.com/davidsonmizael"]
filename: LearnBash-pt_br.sh
lang: pt-br
---

Tutorial de shell em português

Bash é o nome do shell do Unix, que também é distribuido como shell do sistema 
operacional GNU e como shell padrão para Linux e Mac OS X. Praticamente todos 
os exemplos abaixo podem fazer parte de um shell script e pode ser executados
diretamente no shell.

[Leia mais sobre](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# A primeira linha do script é o shebang, que conta para o sistema como executar
# o script: http://en.wikipedia.org/wiki/Shebang_(Unix)
# Como você já deve ter percebido, comentários começam com #. 
# Shebang também é um comentário.

# Exemplo simples de hello world:
echo Hello World!

# Cada comando começa com uma nova linha, ou após um ponto virgula:
echo 'Essa é a primeira linha'; echo 'Essa é a segunda linha'

# A declaração de variáveis é mais ou menos assim
Variavel="Alguma string"

# Mas não assim:
Variavel = "Alguma string"
# Bash interpretará Variavel como um comando e tentará executar e lhe retornar
# um erro porque o comando não pode ser encontrado.

# Ou assim:
Variavel= 'Alguma string'
# Bash interpretará 'Alguma string' como um comando e tentará executar e lhe retornar
# um erro porque o comando não pode ser encontrado. (Nesse caso a a parte 'Variavel=' 
# é vista com uma declaração de variável valida apenas para o escopo do comando 'Uma string').

# Usando a variável:
echo $Variavel
echo "$Variavel"
echo '$Variavel'
# Quando você usa a variável em si — declarando valor, exportando, etc — você escreve
# seu nome sem o $. Se você quer usar o valor da variável você deve usar o $.
# Note que ' (aspas simples) não expandirão as variáveis!

# Substituição de strings em variáveis
echo ${Variavel/Alguma/Uma}
# Isso substituirá a primeira ocorrência de "Alguma" por "Uma"

# Substring de uma variável
Tamanho=7
echo ${Variavel:0:Tamanho}
# Isso retornará apenas os 7 primeiros caractéres da variável

# Valor padrão de uma variável
echo ${Foo:-"ValorPadraoSeFooNaoExistirOuEstiverVazia"}
# Isso funciona para nulo (Foo=) e (Foo=""); zero (Foo=0) retorna 0.
# Note que isso apenas retornar o valor padrão e não mudar o valor da variável.

# Variáveis internas
# Tem algumas variáveis internas bem uteis, como
echo "O ultimo retorno do programa: $?"
echo "PID do script: $$"
echo "Numero de argumentos passados para o script $#"
echo "Todos os argumentos passados para o script $@"
echo "Os argumentos do script em variáveis diferentes: $1, $2..."

# Lendo o valor do input:
echo "Qual o seu nome?"
read Nome # Note que nós não precisamos declarar a variável
echo Ola, $Nome

# Nós temos a estrutura if normal:
# use 'man test' para mais infomações para as condicionais
if [ $Nome -ne $USER ]
then
	echo "Seu nome não é o seu username"
else
	echo "Seu nome é seu username"
fi

# Tem também execução condicional
echo "Sempre executado" || echo "Somente executado se o primeiro falhar"
echo "Sempre executado" && "Só executado se o primeiro NÃO falhar"

# Para usar && e || com o if, você precisa multiplicar os pares de colchetes
if [ $Nome == "Estevao"] && [ $Idade -eq 15]
then
	echo "Isso vai rodar se $Nome é igual Estevao E $Idade é 15."
fi

fi [ $Nome == "Daniela" ] || [ $Nome = "Jose" ] 
then
	echo "Isso vai rodar se $Nome é Daniela ou Jose."
fi

# Expressões são denotadas com o seguinte formato
echo $(( 10 + 5))

# Diferentemente das outras linguagens de programação, bash é um shell, então ele 
# funciona no diretório atual. Você pode listar os arquivos e diretórios no diretório
# atual com o comando ls:
ls

#Esse comando tem opções que controlam sua execução
ls -l # Lista todo arquivo e diretorio em linhas separadas

# Os resultados do comando anterior pode ser passado para outro comando como input.
# O comando grep filtra o input com o padrão passado. É assim que listamos apenas
# os arquivos .txt no diretório atual:
ls -l | grep "\.txt"

# Você pode redirecionar o comando de input e output (stdin, stdout e stderr).
# Lê o stdin até ^EOF$ e sobrescreve hello.py com as linhas entre "EOF":
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ imprt print_function
import sys
print("#stdout", file=sys.stdout)
print("stderr", file=sys.stderr)
for line in sys.stdin:
	print(line, file=sys.stdout)
EOF

# Rode hello.py com várias instruções stdin, stdout e stderr:
python hello.py < "input.in"
python hello.py > "ouput.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# O erro no output sobrescreverá o arquivo se existir,
# se ao invés disso você quiser complementar, use ">>":
python hello.py >> "output.out" 2>> "error.err"

# Sobrescreve output.out, complemente para error.err e conta as linhas
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

#Roda um comando e imprime o desencriptador (e.g. /dev/fd/123)
# veja: man fd
echo <(echo "#helloworld")

# Sobrescreve ouput.out com "#helloworld":
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out > /dev/null

# Limpa os arquivos temporários detalhando quais foram deletados (use '-i' para confirmar exclusão)
rm -v output.out error.err output-and-error.log

# Comando podem ser substituídos por outros comandos usando $( ):
# O comando a seguir mostra o número de arquivos e diretórios no diretorio atual
echo "Existem $(ls | wc -l) itens aqui."

# O mesmo pode ser feito usando crase `` mas elas não podem ser aninhadas - dá se 
# preferência ao uso do $( )
echo "Existem `ls | wc -l` itens aqui."

# Bash usa o comando case que funciona de uma maneira similar ao switch de Java e C++:
case "$Variavel" in
	# Lista de parametros para condições que você quer encontrar
	0) echo "Isso é um Zero.";;
	1) echo "Isso é um Um.";;
	*) echo "Isso não é null.";;
esac

# loops for iteragem para quantos argumentos passados:
# O conteudo de $Variavel é exibido três vezes.
for Variavel in {1..3}
do
	echo "$Variavel"
done

# Ou use o loop da "maneira tradicional":
for ((a=1; a <= 3; a++))
do
	echo $a
done

# Eles também podem ser usados em arquivos...
# Isso irá rodar o comando 'cat' em arquivo1 e arquivo2
for Variavel in arquivo1 arquivo2
do
	cat "$Variavel"
done

# ...ou o output de um comando
# Isso irá usar cat no output do ls.
for Output in $(ls)
do
	cat "$Output"
done

# loop while:
while [ true ]
do
	echo "corpo do loop aqui..."
	break
done

# Você também pode usar funções
# Definição:
function foo() {
	echo "Argumentos funcionam bem assim como os dos scripts: $@"
	echo "E: $1 $2..."
	echo "Isso é uma função"
	return 0
}

# ou simplesmente
bar () {
	echo "Outro jeito de declarar funções!"
	return 0
}

# Chamando sua função
foo "Meu nome é" $Nome

# Existe um monte de comandos úteis que você deveria aprender:
# exibe as 10 ultimas linhas de arquivo.txt
tail -n 10 arquivo.txt
# exibe as primeiras 10 linhas de arquivo.txt
head -n 10 arquivo.txt
# ordena as linhas de arquivo.txt
sort arquivo.txt
# reporta ou omite as linhas repetidas, com -d você as reporta
uniq -d arquivo.txt
# exibe apenas a primeira coluna após o caráctere ','
cut -d ',' -f 1 arquivo.txt
# substitui todas as ocorrencias de 'okay' por 'legal' em arquivo.txt (é compativel com regex)
sed -i 's/okay/legal/g' file.txt
# exibe para o stdout todas as linhas do arquivo.txt que encaixam com o regex
# O exemplo exibe linhas que começam com "foo" e terminam com "bar"
grep "^foo.*bar$" arquivo.txt
# passe a opção "-c" para ao invês de imprimir o numero da linha que bate com o regex
grep -c "^foo.*bar$" arquivo.txt
# se você quer literalmente procurar por uma string,
# e não pelo regex, use fgrep (ou grep -F)
fgrep "^foo.*bar$" arquivo.txt


# Leia a documentação interna do shell Bash com o comando interno 'help':
help
help help
help for 
help return
help source
help .

# Leia a página principal da documentação com man
apropos bash
man 1 bash
man bash

# Leia a documentação de informação com info (? para ajuda)
apropos info | grep '^info.*('
man info
info info 
info 5 info 

#Leia a documentação informativa do Bash:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash 
```
---
language: bf
filename: learnbf-pt.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Suzane Sant Ana", "http://github.com/suuuzi"]
    - ["Rodrigo Muniz", "http://github.com/muniz95"]
lang: pt-br
---

Brainfuck (em letras minúsculas, exceto no início de frases) é uma linguagem de
programação Turing-completa extremamente simples com apenas 8 comandos.

```
Qualquer caractere exceto "><+-.,[]" (sem contar as aspas) é ignorado.

Brainfuck é representado por um vetor com 30 000 células inicializadas em zero
e um ponteiro de dados que aponta para a célula atual.

Existem 8 comandos:
+ : Incrementa o valor da célula atual em 1.
- : Decrementa o valor da célula atual em 1.
> : Move o ponteiro de dados para a célula seguinte (célula à direita).
< : Move o ponteiro de dados para a célula anterior (célula à esquerda).
. : Imprime o valor ASCII da célula atual. (ex. 65 = 'A').
, : Lê um único caractere para a célula atual.
[ : Se o valor da célula atual for zero, salta para o ] correspondente.
    Caso contrário, passa para a instrução seguinte.
] : Se o valor da célula atual for zero, passa para a instrução seguinte.
    Caso contrário, volta para a instrução relativa ao [ correspondente.

[ e ] formam um ciclo while. Obviamente, devem ser equilibrados.

Vamos ver alguns exemplos básicos em brainfuck:

++++++ [ > ++++++++++ < - ] > +++++ .

Este programa imprime a letra 'A'. Primeiro incrementa a célula #1 para 6.
A célula #1 será usada num ciclo. Depois é iniciado o ciclo ([) e move-se
o ponteiro de dados para a célula #2. O valor da célula #2 é incrementado 10
vezes, move-se o ponteiro de dados de volta para a célula #1, e decrementa-se
a célula #1. Este ciclo acontece 6 vezes (são necessários 6 decrementos para
a célula #1 chegar a 0, momento em que se salta para o ] correspondente,
continuando com a instrução seguinte).

Nesta altura estamos na célula #1, cujo valor é 0, enquanto a célula #2
tem o valor 60. Movemos o ponteiro de dados para a célula #2, incrementa-se 5
vezes para um valor final de 65, e então é impresso o valor da célula #2. O valor
65 corresponde ao caractere 'A' em ASCII, então 'A' é impresso no terminal.

, [ > + < - ] > .

Este programa lê um caractere e copia o seu valor para a célula #1. Um ciclo é
iniciado. Movemos o ponteiro de dados para a célula #2, incrementamos o valor na
célula #2, movemos o ponteiro de dados de volta para a célula #1 e finalmente
decrementamos o valor na célula #1. Isto continua até o valor na célula #1 ser
igual a 0 e a célula #2 ter o antigo valor da célula #1. Como o ponteiro de
dados está apontando para a célula #1 no fim do ciclo, movemos o ponteiro para a
célula #2 e imprimimos o valor em ASCII.

Os espaços servem apenas para tornar o programa mais legível. Podemos escrever
o mesmo programa da seguinte maneira:

,[>+<-]>.

Tente descobrir o que este programa faz:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Este programa lê dois números e os multiplica.

Basicamente o programa pede dois caracteres ao usuário. Depois é iniciado um
ciclo exterior controlado pelo valor da célula #1. Movemos o ponteiro de dados
para a célula #2 e inicia-se o ciclo interior controlado pelo valor da célula
#2, incrementando o valor da célula #3. Porém existe um problema, no final do
ciclo interior: a célula #2 tem o valor 0. Para resolver este problema o valor da
célula #4 é também incrementado e copiado para a célula #2.
```

E isto é brainfuck. Simples, não? Por divertimento você pode escrever os
seus próprios programas em brainfuck, ou então escrever um interpretador de
brainfuck em outra linguagem. O interpretador é relativamente fácil de se
implementar, mas caso você seja masoquista, tente escrever um interpretador de
brainfuck… em brainfuck.
---
category: Algorithms & Data Structures
name: Binary Search
contributors:
    - ["Abhishek Jaisingh", "http://github.com/abhishekjiitr"]
translators:
    - ["Claudson Martins", "https://github.com/claudsonm"]
lang: pt-br
---

# Busca Binária

## Por Que Busca Binária?

Operações de busca são um dos principais problemas na Ciência da Computação. 
Atualmente existem mais de 1 trilhão de buscas por ano, e nós precisamos de 
algoritmos que possam realizá-las rapidamente. Busca binária é um dos algoritmos 
fundamentais em ciência da computação. A fim de explorá-la, iremos primeiro 
construir um conhecimento teórico, e então utilizá-lo para implementar o 
algoritmo apropriadamente.

## Introdução

Uma abordagem simples para implementar uma busca é realizar uma busca linear, 
mas algoritmos nessa abordagem levam muito tempo, o qual cresce linearmente de 
acordo com a quantidade ou número de dados. Por exemplo, iniciando do elemento 
mais a esquerda de arr[] e um a um comparar x com cada elemento de arr[], se x 
coincide com um elemento, retornar seu índice. Se x não coincide com nenhum dos 
elementos, retornar -1.

```
Busca Linear: O (n)               Tempo Linear

Busca Binária: O ( log(n) )		   Tempo Logarítmico

```
```
def busca(arr, x):
 
    for i in range(len(arr)):
 
        if arr[i] == x:
            return i
 
    return -1

```
## Algoritmo de Busca Binária

O pré-requisito básico para que uma busca binária funcione é que os dados que se
desejam buscar devem estar ordenados (em qualquer ordem).

### Pseudocódigo

```
A ideia da busca binária é usar a informação de que o array está ordenado e 
reduzir a complexidade de tempo para O(Log n). Nós basicamente ignoramos metade 
dos elementos após uma comparação.

1) Compare x com o elemento do meio.
2) Se x coincide com o elemento do meio, retorne o índice do meio.
3) Senão Se x é maior que o elemento do meio, então x só pode estar no lado 
direito do elemento do meio. Portanto nós pulamos para a metade direita.
4) Senão (x é menor) pulamos para a metade esquerda.

Essa é a ideia da implementação recursiva da busca binária.

```

### Considerações Finais

Existem outras formas de busca binária que são muito úteis.

## Recursos Online

* [GeeksforGeeks](http://www.geeksforgeeks.org/the-ubiquitous-binary-search-set-1/)
* [Topcoder Tutorial](https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/)
---
language: c++
filename: learncpp-pt.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Matt Kline", "https://github.com/mrkline"]
translators:
    - ["Miguel Araújo", "https://github.com/miguelarauj1o"]
lang: pt-br
---

C++ é uma linguagem de programação de sistemas que,
[de acordo com seu inventor Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote),
foi concebida para

- ser um "C melhor"
- suportar abstração de dados
- suportar programação orientada a objetos
- suportar programação genérica

Embora sua sintaxe pode ser mais difícil ou complexa do que as linguagens mais
recentes, C++ é amplamente utilizado porque compila para instruções nativas que
podem ser executadas diretamente pelo processador e oferece um controlo rígido sobre hardware (como C), enquanto oferece recursos de alto nível, como os
genéricos, exceções e classes. Esta combinação de velocidade e funcionalidade
faz C++ uma das linguagens de programação mais utilizadas.

```c++
//////////////////
// Comparação com C
//////////////////

// C ++ é quase um super conjunto de C e compartilha sua sintaxe básica para
// declarações de variáveis, tipos primitivos, e funções. No entanto, C++ varia
// em algumas das seguintes maneiras:

// A função main() em C++ deve retornar um int, embora void main() é aceita
// pela maioria dos compiladores (gcc, bumbum, etc.)
// Este valor serve como o status de saída do programa.
// Veja http://en.wikipedia.org/wiki/Exit_status para mais informações.

int main(int argc, char** argv)
{
    // Argumentos de linha de comando são passados em pelo argc e argv da mesma
    // forma que eles estão em C.
    // argc indica o número de argumentos,
    // e argv é um array de strings, feito C (char*) representado os argumentos
    // O primeiro argumento é o nome pelo qual o programa foi chamado.
    // argc e argv pode ser omitido se você não se importa com argumentos,
    // dando a assinatura da função de int main()

    // Uma saída de status de 0 indica sucesso.
    return 0;
}

// Em C++, caracteres literais são um byte.
sizeof('c') == 1

// Em C, caracteres literais são do mesmo tamanho que ints.
sizeof('c') == sizeof(10)

// C++ tem prototipagem estrita
void func(); // função que não aceita argumentos

// Em C
void func(); // função que pode aceitar qualquer número de argumentos

// Use nullptr em vez de NULL em C++
int* ip = nullptr;

// Cabeçalhos padrão C estão disponíveis em C++,
// mas são prefixados com "c" e não têm sufixo .h

#include <cstdio>

int main()
{
    printf("Hello, world!\n");
    return 0;
}

///////////////////////
// Sobrecarga de função
///////////////////////

// C++ suporta sobrecarga de função
// desde que cada função tenha parâmetros diferentes.

void print(char const* myString)
{
    printf("String %s\n", myString);
}

void print(int myInt)
{
    printf("My int is %d", myInt);
}

int main()
{
    print("Hello"); // Funciona para void print(const char*)
    print(15); // Funciona para void print(int)
}

/////////////////////////////
// Parâmetros padrão de função
/////////////////////////////

// Você pode fornecer argumentos padrões para uma função se eles não são
// fornecidos pelo chamador.

void doSomethingWithInts(int a = 1, int b = 4)
{
    // Faça alguma coisa com os ints aqui
}

int main()
{
    doSomethingWithInts();      // a = 1,  b = 4
    doSomethingWithInts(20);    // a = 20, b = 4
    doSomethingWithInts(20, 5); // a = 20, b = 5
}

// Argumentos padrões devem estar no final da lista de argumentos.

void invalidDeclaration(int a = 1, int b) // Erro!
{
}


/////////////
// Namespaces (nome de espaços)
/////////////

// Namespaces fornecem escopos distintos para variável, função e outras
// declarações. Namespaces podem estar aninhados.

namespace First {
    namespace Nested {
        void foo()
        {
            printf("This is First::Nested::foo\n");
        }
    } // Fim do namespace aninhado
} // Fim do namespace First

namespace Second {
    void foo()
    {
        printf("This is Second::foo\n")
    }
}

void foo()
{
    printf("This is global foo\n");
}

int main()
{
    // Assuma que tudo é do namespace "Second" a menos que especificado de
    // outra forma.
    using namespace Second;

    foo(); // imprime "This is Second::foo"
    First::Nested::foo(); // imprime "This is First::Nested::foo"
    ::foo(); // imprime "This is global foo"
}

///////////////
// Entrada/Saída
///////////////

// C ++ usa a entrada e saída de fluxos (streams)
// cin, cout, and cerr representa stdin, stdout, and stderr.
// << É o operador de inserção e >> é o operador de extração.

#include <iostream> // Inclusão para o I/O streams

using namespace std; // Streams estão no namespace std (biblioteca padrão)

int main()
{
   int myInt;

   // Imprime na saída padrão (ou terminal/tela)
   cout << "Enter your favorite number:\n";
   // Pega a entrada
   cin >> myInt;

   // cout também pode ser formatado
   cout << "Your favorite number is " << myInt << "\n";
   // imprime "Your favorite number is <myInt>"

    cerr << "Usado para mensagens de erro";
}

//////////
// Strings
//////////

// Strings em C++ são objetos e têm muitas funções de membro
#include <string>

using namespace std; // Strings também estão no namespace std (bib. padrão)

string myString = "Hello";
string myOtherString = " World";

// + é usado para concatenação.
cout << myString + myOtherString; // "Hello World"

cout << myString + " You"; // "Hello You"

// Em C++, strings são mutáveis e têm valores semânticos.
myString.append(" Dog");
cout << myString; // "Hello Dog"


/////////////
// Referência
/////////////

// Além de indicadores como os de C, C++ têm _referências_. Esses são tipos de
// ponteiro que não pode ser reatribuída uma vez definidos e não pode ser nulo.
// Eles também têm a mesma sintaxe que a própria variável: Não * é necessário
// para _dereferencing_ e & (endereço de) não é usado para atribuição.

using namespace std;

string foo = "I am foo";
string bar = "I am bar";


string& fooRef = foo; // Isso cria uma referência para foo.
fooRef += ". Hi!"; // Modifica foo através da referência
cout << fooRef; // Imprime "I am foo. Hi!"

// Não realocar "fooRef". Este é o mesmo que "foo = bar", e foo == "I am bar"
// depois desta linha.

fooRef = bar;

const string& barRef = bar; // Cria uma referência const para bar.
// Como C, valores const (e ponteiros e referências) não podem ser modificado.
barRef += ". Hi!"; // Erro, referência const não pode ser modificada.

//////////////////////////////////////////
// Classes e programação orientada a objeto
//////////////////////////////////////////

// Primeiro exemplo de classes
#include <iostream>

// Declara a classe.
// As classes são geralmente declarado no cabeçalho arquivos (.h ou .hpp).
class Dog {
    // Variáveis de membro e funções são privadas por padrão.
    std::string name;
    int weight;

// Todos os membros a seguir este são públicos até que "private:" ou
// "protected:" é encontrado.
public:

    // Construtor padrão
    Dog();

    // Declarações de função Membro (implementações a seguir)
    // Note que usamos std :: string aqui em vez de colocar
    // using namespace std;
    // acima.
    // Nunca coloque uma declaração "using namespace" em um cabeçalho.
    void setName(const std::string& dogsName);

    void setWeight(int dogsWeight);

    // Funções que não modificam o estado do objecto devem ser marcadas como
    // const. Isso permite que você chamá-los se for dada uma referência const
    // para o objeto. Além disso, observe as funções devem ser explicitamente
    // declarados como _virtual_, a fim de ser substituídas em classes
    // derivadas. As funções não são virtuais por padrão por razões de
    // performance.

    virtual void print() const;

    // As funções também podem ser definidas no interior do corpo da classe.
    // Funções definidas como tal são automaticamente embutidas.
    void bark() const { std::cout << name << " barks!\n" }

    // Junto com os construtores, C++ fornece destruidores.
    // Estes são chamados quando um objeto é excluído ou fica fora do escopo.
    // Isto permite paradigmas poderosos, como RAII
    // (veja abaixo)
    // Destruidores devem ser virtual para permitir que as classes de ser
    // derivada desta.
    virtual ~Dog();

}; // Um ponto e vírgula deve seguir a definição de classe.

// Funções membro da classe geralmente são implementados em arquivos .cpp.
void Dog::Dog()
{
    std::cout << "A dog has been constructed\n";
}

// Objetos (como strings) devem ser passados por referência
// se você pretende modificá-los, ou com const caso contrário.
void Dog::setName(const std::string& dogsName)
{
    name = dogsName;
}

void Dog::setWeight(int dogsWeight)
{
    weight = dogsWeight;
}

// Observe que "virtual" só é necessária na declaração, não a definição.
void Dog::print() const
{
    std::cout << "Dog is " << name << " and weighs " << weight << "kg\n";
}

void Dog::~Dog()
{
    std::cout << "Goodbye " << name << "\n";
}

int main() {
    Dog myDog; // imprime "A dog has been constructed"
    myDog.setName("Barkley");
    myDog.setWeight(10);
    myDog.printDog(); // imprime "Dog is Barkley and weighs 10 kg"
    return 0;
} // imprime "Goodbye Barkley"

// herança:

// Essa classe herda tudo público e protegido da classe Dog
class OwnedDog : public Dog {

    void setOwner(const std::string& dogsOwner)

    // Substituir o comportamento da função de impressão de todas OwnedDogs.
    // Ver http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping
    // Para uma introdução mais geral, se você não estiver familiarizado com o
    // polimorfismo subtipo. A palavra-chave override é opcional, mas torna-se
    // na verdade você está substituindo o método em uma classe base.
    void print() const override;

private:
    std::string owner;
};

// Enquanto isso, no arquivo .cpp correspondente:

void OwnedDog::setOwner(const std::string& dogsOwner)
{
    owner = dogsOwner;
}

void OwnedDog::print() const
{
    Dog::print(); // Chame a função de impressão na classe Dog base de
    std::cout << "Dog is owned by " << owner << "\n";
    // Prints "Dog is <name> and weights <weight>"
    //        "Dog is owned by <owner>"
}

//////////////////////////////////////////
// Inicialização e Sobrecarga de Operadores
//////////////////////////////////////////

// Em C ++, você pode sobrecarregar o comportamento dos operadores, tais como
// +, -, *, /, etc. Isto é feito através da definição de uma função que é
// chamado sempre que o operador é usado.

#include <iostream>
using namespace std;

class Point {
public:
    // Variáveis membro pode ser dado valores padrão desta maneira.
    double x = 0;
    double y = 0;

    // Define um construtor padrão que não faz nada
    // mas inicializar o Point para o valor padrão (0, 0)
    Point() { };

    // A sintaxe a seguir é conhecido como uma lista de inicialização
    // e é a maneira correta de inicializar os valores de membro de classe
    Point (double a, double b) :
        x(a),
        y(b)
    { /* Não fazer nada, exceto inicializar os valores */ }

    // Sobrecarrega o operador +.
    Point operator+(const Point& rhs) const;

    // Sobrecarregar o operador +=.
    Point& operator+=(const Point& rhs);

    // Ele também faria sentido para adicionar os operadores - e -=,
    // mas vamos pular para sermos breves.
};

Point Point::operator+(const Point& rhs) const
{
    // Criar um novo ponto que é a soma de um e rhs.
    return Point(x + rhs.x, y + rhs.y);
}

Point& Point::operator+=(const Point& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

int main () {
    Point up (0,1);
    Point right (1,0);
    // Isto chama que o operador ponto +
    // Ressalte-se a chamadas (função)+ com direito como seu parâmetro...
    Point result = up + right;
    // Imprime "Result is upright (1,1)"
    cout << "Result is upright (" << result.x << ',' << result.y << ")\n";
    return 0;
}

/////////////////////////
// Tratamento de Exceções
/////////////////////////

// A biblioteca padrão fornece alguns tipos de exceção
// (see http://en.cppreference.com/w/cpp/error/exception)
// mas qualquer tipo pode ser jogado como uma exceção
#include <exception>

// Todas as exceções lançadas dentro do bloco try pode ser capturado por
// manipuladores de captura subseqüentes
try {
    // Não aloca exceções no heap usando _new_.
    throw std::exception("A problem occurred");
}
// Capturar exceções por referência const se eles são objetos
catch (const std::exception& ex)
{
  std::cout << ex.what();
// Captura qualquer exceção não capturada pelos blocos _catch_ anteriores
} catch (...)
{
    std::cout << "Exceção desconhecida encontrada";
    throw; // Re-lança a exceção
}

///////
// RAII
///////

// RAII significa alocação de recursos é de inicialização.
// Muitas vezes, é considerado o paradigma mais poderoso em C++, e é o
// conceito simples que um construtor para um objeto adquire recursos daquele
// objeto e o destruidor liberá-los.

// Para entender como isso é útil,
// Considere uma função que usa um identificador de arquivo C:
void doSomethingWithAFile(const char* filename)
{
    // Para começar, assuma que nada pode falhar.

    FILE* fh = fopen(filename, "r"); // Abra o arquivo em modo de leitura.

    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

    fclose(fh); // Feche o arquivo.
}

// Infelizmente, as coisas são levemente complicadas para tratamento de erros.
// Suponha que fopen pode falhar, e que doSomethingWithTheFile e
// doSomethingElseWithIt retornam códigos de erro se eles falharem. (As
// exceções são a forma preferida de lidar com o fracasso, mas alguns
// programadores, especialmente aqueles com um conhecimento em C, discordam
// sobre a utilidade de exceções). Agora temos que verificar cada chamada para
// o fracasso e fechar o identificador de arquivo se ocorreu um problema.

bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Abra o arquivo em modo de leitura
    if (fh == nullptr) // O ponteiro retornado é nulo em caso de falha.
        return false; // Relate o fracasso para o chamador.

    // Suponha cada função retorne false, se falhar
    if (!doSomethingWithTheFile(fh)) {
        fclose(fh); // Feche o identificador de arquivo para que ele não vaze.
        return false; // Propague o erro.
    }
    if (!doSomethingElseWithIt(fh)) {
        fclose(fh); // Feche o identificador de arquivo para que ele não vaze.
        return false; // Propague o erro.
    }

    fclose(fh); // Feche o identificador de arquivo para que ele não vaze.
    return true; // Indica sucesso
}

// Programadores C frequentemente limpam isso um pouco usando Goto:
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r");
    if (fh == nullptr)
        return false;

    if (!doSomethingWithTheFile(fh))
        goto failure;

    if (!doSomethingElseWithIt(fh))
        goto failure;

    fclose(fh); // Close the file
    return true; // Indica sucesso

failure:
    fclose(fh);
    return false; // Propague o erro.
}

// Se as funções indicam erros usando exceções,
// as coisas são um pouco mais limpo, mas ainda abaixo do ideal.
void doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Abra o arquivo em modo de leitura.
    if (fh == nullptr)
        throw std::exception("Não pode abrir o arquivo.");

    try {
        doSomethingWithTheFile(fh);
        doSomethingElseWithIt(fh);
    }
    catch (...) {
        fclose(fh); // Certifique-se de fechar o arquivo se ocorrer um erro.
        throw; // Em seguida, re-lance a exceção.
    }

    fclose(fh); // Feche o arquivo
    // Tudo ocorreu com sucesso!
}

// Compare isso com o uso de C++ classe fluxo de arquivo (fstream) fstream usa
// seu destruidor para fechar o arquivo. Lembre-se de cima que destruidores são
// automaticamente chamado sempre que um objeto cai fora do âmbito.
void doSomethingWithAFile(const std::string& filename)
{
    // ifstream é curto para o fluxo de arquivo de entrada
    std::ifstream fh(filename); // Abra o arquivo

    // faça alguma coisa com o arquivo
    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

} // O arquivo é automaticamente fechado aqui pelo destructor

// Isto tem _grandes_ vantagens:
// 1. Não importa o que aconteça,
//    o recurso (neste caso, o identificador de ficheiro) irá ser limpo.
//    Depois de escrever o destruidor corretamente,
//    É _impossível_ esquecer de fechar e vazar o recurso
// 2. Nota-se que o código é muito mais limpo.
//    As alças destructor fecham o arquivo por trás das cenas
//    sem que você precise se preocupar com isso.
// 3. O código é seguro de exceção.
//    Uma exceção pode ser jogado em qualquer lugar na função e a limpeza
//    irá ainda ocorrer.

// Todos códigos C++ usam RAII extensivamente para todos os recursos.
// Outros exemplos incluem
// - Memória usa unique_ptr e shared_ptr
// - Contentores - a lista da biblioteca ligada padrão,
//   vetor (i.e. array de autodimensionamento), mapas hash, e assim por diante
//   tudo é automaticamente destruído quando eles saem de escopo
// - Mutex usa lock_guard e unique_lock


/////////////////////
// Templates
/////////////////////

// Templates em C++ são utilizados para programação genérica, ou seja,
// utilizar um tipo de dado genérico onde possa suportar qualquer entrada.
// Por exemplo, invés de criar uma função que apenas some inteiros, você
// poderá fazer uma função que soma double, float e inteiros em uma única
// definição para reutilizar código.

// Definimos um função que utiliza um "typename"
template<class T>
T soma(T a, T b) {
  return A + B;
}

// E agora para executá-la
int i=5, j=6, k;
double f=2.0, g=0.5, h;
k=sum<int>(i,j);
h=sum<double>(f,g);

// Deste modo, não precisamos fazer overload nas funções! (:
```
Leitura Adicional:

Uma referência atualizada da linguagem pode ser encontrada em
<http://cppreference.com/w/cpp>

Uma fonte adicional pode ser encontrada em <http://cplusplus.com>
---
language: c
filename: learnc.c
contributors:
    - ["Adam Bard", "http://adambard.com/"]
    - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"]
translators:
    - ["João Farias", "https://github.com/JoaoGFarias"]
    - ["Elton Viana", "https://github.com/eltonvs"]
    - ["Cássio Böck", "https://github.com/cassiobsilva"]
lang: pt-br
filename: c-pt.el
---

Ah, C. Ainda é **a** linguagem de computação de alta performance.

C é a linguagem de mais baixo nível que a maioria dos programadores
utilizarão, e isso dá a ela uma grande velocidade bruta. Apenas fique
atento se este manual de gerenciamento de memória e C vai te levar
tão longe quanto precisa.

```c
// Comentários de uma linha iniciam-se com // - apenas disponível a partir do C99

/*
Comentários de múltiplas linhas se parecem com este.
Funcionam no C89 também.
*/

// Constantes: #define <palavra-chave>
#define DAY_IN_YEAR 365

//enumerações também são modos de definir constantes.
enum day {DOM = 1, SEG, TER, QUA, QUI, SEX, SAB};
// SEG recebe 2 automaticamente, TER recebe 3, etc.

// Cabeçalhos são inclusos com #include
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// (Nomes dos arquivos entre <colchetes> são cabeçalhos para bibliotecas padrão de C.)
// Para cabeçalhos próprios, use aspas ao invés de colchetes:
#include "minha_biblioteca.h"

// Declare assinaturas das funções no início do arquivo .h ou no topo
// do seu arquivo .c.
void funcao_1(char c);
int funcao_2(void);

// Deve-se declarar um 'protótipo de função' antes do main() quando as ocorrências
// dessas funções estão após sua função main()
int soma_dois_ints(int x1, int x2); // protótipo de função

// O ponto de entrada do teu programa é uma função
// chamada main, com tipo de retorno inteiro
int main() {
	// Usa-se printf para escrever na tela,
	// para "saída formatada"
	// %d é um inteiro, \n é uma nova linha
    printf("%d\n", 0); // => Imprime 0
	// Todos as declarações devem acabar com
	// ponto e vírgula

    ///////////////////////////////////////
    // Tipos
    ///////////////////////////////////////

    // ints normalmente tem 4 bytes
    int x_int = 0;

    // shorts normalmente tem 2 bytes
    short x_short = 0;

    // chars sempre tem um byte
    char x_char = 0;
    char y_char = 'y'; // Literais de caracter são cercados por '

    // longs tem entre 4 e 8 bytes; longs long tem garantia
    // de ter pelo menos 64 bits
    long x_long = 0;
    long long x_long_long = 0;

    // floats são normalmente números de ponto flutuante
	// com 32 bits
    float x_float = 0.0;

    // doubles são normalmente números de ponto flutuante
	// com 64 bits
    double x_double = 0.0;

    // Tipos inteiros podem ser sem sinal.
    unsigned short ux_short;
    unsigned int ux_int;
    unsigned long long ux_long_long;

	// caracteres dentro de aspas simples são inteiros
	// no conjunto de caracteres da máquina.
    '0' // => 48 na tabela ASCII.
    'A' // => 65 na tabela ASCII.

	// sizeof(T) devolve o tamanho de uma variável do tipo T em bytes
	// sizeof(obj) devolve o tamanho de uma expressão (variável, literal, etc.).
    printf("%zu\n", sizeof(int)); // => 4 (na maioria das máquinas com palavras de 4 bytes)

	// Se o argumento do operador `sizeof` é uma expressão, então seus argumentos
	// não são avaliados (exceto em VLAs (veja abaixo)).
	// O valor devolve, neste caso, é uma constante de tempo de compilação.
    int a = 1;
	// size_t é um inteiro sem sinal com pelo menos 2 bytes que representa
	// o tamanho de um objeto.
    size_t size = sizeof(a++); // a++ não é avaliada.
    printf("sizeof(a++) = %zu where a = %d\n", size, a);
    // imprime "sizeof(a++) = 4 onde a = 1" (quando em uma arquitetura de 32 bits)

	// Arrays precisam ser inicializados com um tamanho concreto
    char meu_char_array[20]; // Este array ocupa 1 * 20 = 20 bytes
    int meu_int_array[20]; // Este array ocupa 4 * 20 = 80 bytes
                           // (assumindo palavras de 4 bytes)

	// Você pode inicializar um array com 0 desta forma:
    char meu_array[20] = {0};

	// Indexar um array é semelhante a outras linguagens
	// Melhor dizendo, outras linguagens são semelhantes a C
    meu_array[0]; // => 0

	// Array são mutáveis; são apenas memória!
    meu_array[1] = 2;
    printf("%d\n", meu_array[1]); // => 2

	// No C99 (e como uma features opcional em C11), arrays de tamanho variável
	// VLA (do inglês), podem ser declarados também. O tamanho destes arrays
	// não precisam ser uma constante de tempo de compilação:
    printf("Entre o tamanho do array: "); // Pergunta ao usuário pelo tamanho
    char buf[0x100];
    fgets(buf, sizeof buf, stdin);

    // strtoul transforma a string em um inteiro sem sinal
    size_t size = strtoul(buf, NULL, 10);
    int var_length_array[size]; // declara o VLA
    printf("sizeof array = %zu\n", sizeof var_length_array);

    // Uma possível saída para esse programa seria:
    // > Entre o tamanho do array: 10
    // > sizeof array = 40

	// String são apenas arrays de caracteres terminados por um
	// byte nulo (0x00), representado em string pelo caracter especial '\0'.
	// (Não precisamos incluir o byte nulo em literais de string; o compilador
	// o insere ao final do array para nós.)
    char uma_string[20] = "Isto é uma string";
	// Observe que 'é' não está na tabela ASCII
	// A string vai ser salva, mas a saída vai ser estranha
	// Porém, comentários podem conter acentos
    printf("%s\n", uma_string); // %s formata a string

    printf("%d\n", uma_string[17]); // => 0
    // i.e., byte #18 é 0 (assim como o 19°, 20°, 21°...)

	// Se temos caracteres entre aspas simples, temos um caracter literal.
	// Seu tipo é `int`, *não* `char` (por razões históricas).
    int cha = 'a'; // ok
    char chb = 'a'; // ok também (conversão implícita de int para char)

	// Arrays multi-dimensionais:
    int multi_array[2][5] = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 0}
    };
    // Acesso a elementos:
    int array_int = multi_array[0][2]; // => 3

    ///////////////////////////////////////
    // Operadores
    ///////////////////////////////////////

    // Atalho para multiplas declarações:
    int i1 = 1, i2 = 2;
    float f1 = 1.0, f2 = 2.0;

    int a, b, c;
    a = b = c = 0;

    // Aritimética é óbvia
    i1 + i2; // => 3
    i2 - i1; // => 1
    i2 * i1; // => 2
    i1 / i2; // => 0 (0.5, porém, é truncado para 0)

    f1 / f2; // => 0.5, mais ou menos epsilon
    // Números e cálculos de ponto flutuante não são exatos

    // Modulo também existe
    11 % 3; // => 2

	// Operadores de comparação provavelmente são familiares,
	// porém não há tipo booleano em C. Usa-se ints no lugar.
	// (Ou _Bool or bool em C99.)
	// 0 é falso e qualquer outra coisa é verdadeiro
	// (Os operadores de comparação devolvem 0 ou 1.)
    // Comparison operators are probably familiar, but
    3 == 2; // => 0 (falso)
    3 != 2; // => 1 (verdadeiro)
    3 > 2; // => 1
    3 < 2; // => 0
    2 <= 2; // => 1
    2 >= 2; // => 1

	// C não é Python - comparações não se encadeiam.
    int a = 1;
    // Errado:
    int entre_0_e_2 = 0 < a < 2;
    // Correto:
    int entre_0_e_2 = 0 < a && a < 2;

    // Lógica funciona sobre ints
    !3; // => 0 (Não lógico)
    !0; // => 1
    1 && 1; // => 1 (E lógico)
    0 && 1; // => 0
    0 || 1; // => 1 (Ou lógico)
    0 || 0; // => 0

    //Expressão condicional ternária ( ? : )
    int a = 5;
    int b = 10;
    int z;
    z = (a > b) ? a : b; // => 10 "se a > b retorne a, senão retorne b."

    //Operadores de incremento e decremento:
    char *s = "iLoveC";
    int j = 0;
    s[j++]; // => "i". Retorna o j-ésimo item de s E DEPOIS incrementa o valor de j.
    j = 0;
    s[++j]; // => "L". Incrementa o valor de j. E DEPOIS retorna o j-ésimo item de s.
    // o mesmo com j-- e --j

    // Operadores bit a bit!
    ~0x0F; // => 0xF0 (negação bit a bit, "complemento de 1")
    0x0F & 0xF0; // => 0x00 (bit a bit E)
    0x0F | 0xF0; // => 0xFF (bit a bit OU)
    0x04 ^ 0x0F; // => 0x0B (bit a bit OU EXCLUSIVO)
    0x01 << 1; // => 0x02 (bit a bit shift para esquerda (por 1))
    0x02 >> 1; // => 0x01 (bit a bit shift para direita (por 1))

	// Cuidado quando fizer shift em inteiro com sinal - o seguinte é indefinido:
	// - Fazer shift sobre um bit de sinal de um inteiro com sinal (int a = 1 << 32)
	// - Fazer shift a esquerda sobre um número negativo (int a = -1 << 2)
	// - Fazer shift maior que a largura do tipo de LHS:
	//   int a = 1 << 32; // Indefinido se int é de tamanho 32 bits

    ///////////////////////////////////////
    // Estruturas de Controle
    ///////////////////////////////////////

    if (0) {
      printf("Nunca rodará\n");
    } else if (0) {
      printf("Também nunca rodará\n");
    } else {
      printf("Eu serei impresso\n");
    }

    // Loops while existem
    int ii = 0;
    while (ii < 10) { //QUALQUER valor diferente de 0 é verdadeiro
        printf("%d, ", ii++); // ii++ incrementa o valor de ii APÓS usá-lo
    } // => imprime "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    int kk = 0;
    do {
        printf("%d, ", kk);
    } while (++kk < 10); // ++kk incrementa o valor de kk ANTES de usá-lo
    // => imprime "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    // Loops for também
    int jj;
    for (jj=0; jj < 10; jj++) {
        printf("%d, ", jj);
    } // => imprime "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    // *****NOTAS*****:
	// Loops e Funções PRECISAM ter um corpo. Se nenhum corpo é necessário:
    int i;
    for (i = 0; i <= 5; i++) {
        ; // Use ponto e vírgula para agir como um corpo (declaração nula)
    }
    // Ou
    for (i = 0; i <= 5; i++);

	// Criando branchs com escolhas múltiplas: switch()
    switch (alguma_expressao_integral) {
    case 0: // labels precisam ser expressões integrais **constantes**
        faca_algo();
        break; // Sem break, o controle continua após a label
    case 1:
        faca_outra_coisa();
        break;
    default:
        // Se `alguma_expressao_integral` não coincidir com nenhuma label
        fputs("erro!\n", stderr);
        exit(-1);
        break;
    }


    ///////////////////////////////////////
    // Cast de tipos
    ///////////////////////////////////////

	// Todo valor em C tem um tipo, mas você pode fazer um cast de um valor em outro tipo
	// se você quiser (com algumas restrições).

    int x_hex = 0x01; // Você pode colocar valores hexadecimais em variáveis

	// Cast entre tipos tentará preservar seus valores numéricos
    printf("%d\n", x_hex); // => Imprime 1
    printf("%d\n", (short) x_hex); // => Imprime 1
    printf("%d\n", (char) x_hex); // => Imprime 1

	// Tipos irão ter overflow sem aviso
    printf("%d\n", (unsigned char) 257); // => 1 (Max char = 255 se char tem 8 bits)

	// Para determinar o valor máximo de um `char`, de um `signed char` e de
	// um `unisigned char`, respectivamente, use as macros CHAR_MAX, SCHAR_MAX
	// e UCHAR_MAX de <limits.h>

	// Tipos inteiros podem sofrer cast para pontos-flutuantes e vice-versa.
    printf("%f\n", (float)100); // %f formata um float
    printf("%lf\n", (double)100); // %lf formata um double
    printf("%d\n", (char)100.0);

    ///////////////////////////////////////
    // Ponteiros
    ///////////////////////////////////////

	// Um ponteiro é uma variável declarada para armazenar um endereço de memória.
	// Sua declaração irá também dizer o tipo de dados para o qual ela aponta. Você
	// Pode usar o endereço de memória de suas variáveis, então, brincar com eles.

    int x = 0;
    printf("%p\n", (void *)&x); // Use & para usar o endereço de uma variável
    // (%p formata um objeto ponteiro do tipo void *)
    // => Imprime algum endereço de memória;

	// Ponteiros começam com * na sua declaração
    int *px, nao_eh_um_ponteiro; // px é um ponteiro para um int
    px = &x; // armazena o endereço de x em px
    printf("%p\n", (void *)px); // => Imprime algum endereço de memória
    printf("%zu, %zu\n", sizeof(px), sizeof(nao_eh_um_ponteiro));
    // => Imprime "8, 4" em um sistema típico de 64 bits

	// Para pegar um valor no endereço apontado por um ponteiro,
	// coloque * na frente para de-referenciá-lo.
	// Nota: sim, é confuso usar '*' _tanto_ para declaração de ponteiro
	// como para de-referenciá-lo.
    printf("%d\n", *px); // => Imprime 0, o valor de x

	// Você também pode mudar o valor que o ponteiro está apontando.
	// Temos que cercar a de-referência entre parênteses, pois
	// ++ tem uma precedência maior que *.
    (*px)++; // Incrementa o valor que px está apontando por 1
    printf("%d\n", *px); // => Imprime 1
    printf("%d\n", x); // => Imprime 1

	// Arrays são uma boa maneira de alocar um bloco contínuo de memória
    int x_array[20]; // Declara um array de tamanho 20 (não pode-se mudar o tamanho
    int xx;
    for (xx = 0; xx < 20; xx++) {
        x_array[xx] = 20 - xx;
    } //Inicializa x_array com 20, 19, 18,... 2, 1

	// Declara um ponteiro do tipo int e inicialize ele para apontar para x_array
    int* x_ptr = x_array;
	// x_ptr agora aponta para o primeiro elemento do array (o inteiro 20).
	// Isto funciona porque arrays são apenas ponteiros para seus primeiros elementos.
	// Por exemplo, quando um array é passado para uma função ou é atribuído a um
	// ponteiro, ele transforma-se (convertido implicitamente) em um ponteiro.
	// Exceções: quando o array é o argumento de um operador `&` (endereço-de):
    // Exceptions: when the array is the argument of the `&` (address-of) operator:
    int arr[10];
    int (*ptr_to_arr)[10] = &arr; // &arr não é do tipo `int *`!
                                  // É do tipo "ponteiro para array" (de `int`s).
    // ou quando o array é uma string literal usada para inicializar um array de char:
    char arr[] = "foobarbazquirk";
	// ou quando é um argumento dos operadores `sizeof` ou `alignof`:
    int arr[10];
    int *ptr = arr; // equivalente a int *ptr = &arr[0];
    printf("%zu, %zu\n", sizeof arr, sizeof ptr); // provavelmente imprime "40, 4" ou "40, 8"

	// Ponteiros podem ser incrementados ou decrementados baseado no seu tipo
	// (isto é chamado aritmética de ponteiros
    printf("%d\n", *(x_ptr + 1)); // => Imprime 19
    printf("%d\n", x_array[1]); // => Imprime 19

	// Você também pode alocar dinamicamente blocos de memória com a função
	// da biblioteca padrão malloc, a qual recebe um argumento do tipo size_t
	// representando o número de bytes a ser alocado (geralmente da heap, apesar de
	// isto poder não ser verdadeiro em, e.g., sistemas embarcados - o C padrão diz
	// nada sobre isso).
    int *my_ptr = malloc(sizeof(*my_ptr) * 20);
    for (xx = 0; xx < 20; xx++) {
        *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx
    } //Inicializa a memória com 20, 19, 18, 17... 2, 1 (como ints)

	// Dereferenciar memória que você não alocou cria
	// "resultados imprevisíveis" - o programa é dito ter um "comportamento indefinido"
    printf("%d\n", *(my_ptr + 21)); // => Imprime quem-sabe-o-que? Talvez até quebre o programa.

	// Quando se termina de usar um bloco de memória alocado, você pode liberá-lo,
	// ou ninguém mais será capaz de usá-lo até o fim da execução
	// (Isto chama-se "memory leak"):
    free(my_ptr);

	// Strings são arrays de char, mas elas geralmente são representadas
	// como um ponteiro para char (com o apontador para o primeiro elemento do array).
	// É boa prática usar `const char *' quando de-referenciando uma literal string,
	// dado que elas não deverão ser modificadas (i.e. "foo"[0] = 'a' é ILEGAL.)
    const char *my_str = "Esta é a minha literal string";
    printf("%c\n", *my_str); // => 'T'

	// Este não é o caso se a string for um array
	// (potencialmente inicializado com um literal string)
	// que reside em uma memória de escrita, como em:
    char foo[] = "foo";
    foo[0] = 'a'; // Isto é legal, foo agora contém "aoo"

    funcao_1();
} // fim da função main

///////////////////////////////////////
// Funções
///////////////////////////////////////

//Sintaxe de declaração de funções:
// <tipo de retorno> <nome da função>(<argumentos>)

int soma_dois_int(int x1, int x2)
{
    return x1 + x2; // Use return para retornar um valor
}

/*
Funções são chamadas por valor. Quando uma função é chamada, os argumentos passados
para a função são cópias dos argumento originais (a não ser arrays). Qualquer coisa
que você faz nos argumentos de uma função não alteram o valor do argumento original
onde a função foi chamada.

Use ponteiros se você precisa alterar os valores dos argumentos originais

Exemplo: reversão de string in-place
*/

// Uma função void não retorna valor algum
void str_reverse(char *str_in)
{
    char tmp;
    int ii = 0;
    size_t len = strlen(str_in); // `strlen()` é parte da biblioteca padrão C
    for (ii = 0; ii < len / 2; ii++) {
        tmp = str_in[ii];
        str_in[ii] = str_in[len - ii - 1]; // iiº char do final
        str_in[len - ii - 1] = tmp;
    }
}

/*
char c[] = "Isto é um teste.";
str_reverse(c);
printf("%s\n", c); // => ".etset mu é otsI"
*/

// Se estiver referenciando variáveis externas à função, use a palavra-chave extern.
int i = 0;
void testFunc() {
    extern int i; //i aqui agora está usando a variável externa
}

// Faça variáveis externas privadas para o código-fonte com static:
static int i = 0; // Outros arquivos usando testFunc() não podem acessar a variável i
void testFunc() {
    extern int i;
}
//**Você pode declarar funções como static para torná-las privadas**


///////////////////////////////////////
// Tipos definidos pelo usuário e structs
///////////////////////////////////////

// Typedefs podem ser usadas para criar apelidos para tipos
typedef int meu_tipo;
meu_tipo var_meu_tipo = 0;

// Structs são apenas coleções de dados, os membros são alocados sequencialmente,
// na ordem que são escritos:
struct retangulo {
    int altura;
    int largura;
};

// Geralmente não é verdade que
// sizeof(struct retangulo) == sizeof(int) + sizeof(int)
// devido ao potencial de preenchimento entre os membros da estrutura
// (isto é por razões de alinhamento). [1]

void funcao_1()
{
    struct retangulo meu_retan;

    // Acesse os membros da estrutura com .
    meu_retan.altura = 10;
    meu_retan.largura = 20;

	// Você pode declarar ponteiros para structs
    struct retangulo *meu_retan_ptr = &meu_retan;

	// Use de-referenciamento para setar os membros da
	// struct apontada...
    (*meu_retan_ptr).altura = 30;

	// ... ou ainda melhor: prefira usar o atalho -> para melhorar legibilidade
    meu_retan_ptr->largura = 10; // O mesmo que (*meu_retan_ptr).largura = 10;
}

//Você pode aplicar um typedef para uma struct por conveniência
typedef struct retangulo retan;

int area(retan r)
{
    return r.largura * r.altura;
}

// Se você tiver structus grande, você pode passá-las "por ponteiro"
// para evitar cópia de toda a struct:
int area(const retan *r)
{
    return r->largura * r->altura;
}

///////////////////////////////////////
// Ponteiros para funções
///////////////////////////////////////
/*
Em tempo de execução, funções são localizadas em endereços de memória
conhecidos. Ponteiros para funções são como qualquer outro ponteiro
(apenas guardam endereços de memória), mas podem ser usados para invocar funções
diretamente e passá-las para por toda parte.
Entretanto, a sintaxe de definição por ser um pouco confusa.

Exemplo: use str_reverso através de um ponteiro
*/
void str_reverso_através_ponteiro(char *str_entrada) {
    // Define uma variável de ponteiro para função, nomeada f.
    void (*f)(char *); //Assinatura deve ser exatamente igual à função alvo.
    f = &str_reverso; //Atribue o endereço da função em si (determinado em tempo de execução.
    // f = str_reverso; Também funciona - função tornam-se ponteiros, assim como arrays
    (*f)(str_entrada); // Chamando a função através do ponteiro
    // f(str_entrada); // Esta é uma sintaxe alternativa, mas equivalente.
}

/*
Desde que as assinaturas das funções sejam compatíveis, você pode atribuir qualquer
função ao mesmo ponteiro. Ponteiros para funções são geralmente um typedef por
simplicidade e legibilidade, como segue:
*/

typedef void (*minha_função_type)(char *);

// Declarando o ponteiro:
// ...
// minha_função_type f;

//Caracteres especiais:
'\a' // Alerta (sino)
'\n' // Nova linha
'\t' // Tab (justifica texto a esquerda)
'\v' // Tab vertical
'\f' // Nova linha (formfeed)
'\r' // Retorno de carroça
'\b' // Backspace
'\0' // Caracter nulo. Geralmente colocado ao final de string em C.
     //   oi\n\0. \0 é usado por convenção para marcar o fim da string.
'\\' // Barra invertida
'\?' // Interrogação
'\'' // Aspas simples
'\"' // Aspas duplas
'\xhh' // Número hexadecimal. Exemplo: '\xb' = tab vertical
'\ooo' // Número octal. Exemplo: '\013' = tab vertical

// formatando impressão:
"%d"    // inteiro
"%3d"   // inteiro com pelo menos 3 dígitos (justifica texto a direita)
"%s"    // string
"%f"    // ponto-flutuante
"%ld"   // long
"%3.2f" // ponto-flutuante com pelo menos 3 dígitos a esquerda e 2 a direita
"%7.4s" // (também pode-se fazer com strings)
"%c"    // char
"%p"    // ponteiro
"%x"    // hexadecimal
"%o"    // octal
"%%"    // imprime %

///////////////////////////////////////
// Ordem de avaliação
///////////////////////////////////////

//-----------------------------------------------------------//
//        Operadores                       | Associatividade //
//-----------------------------------------------------------//
// () [] -> .                        | esquerda para direita //
// ! ~ ++ -- + = *(type)sizeof       | direita para esqureda //
// * / %                             | esquerda para direita //
// + -                               | esquerda para direita //
// << >>                             | esquerda para direita //
// < <= > >=                         | esquerda para direita //
// == !=                             | esquerda para direita //
// &                                 | esquerda para direita //
// ^                                 | esquerda para direita //
// |                                 | esquerda para direita //
// &&                                | esquerda para direita //
// ||                                | esquerda para direita //
// ?:                                | direita para esqureda //
// = += -= *= /= %= &= ^= |= <<= >>= | direita para esqureda //
// ,                                 | esquerda para direita //
//-----------------------------------------------------------//

```

## Leitura adicional

É recomendado ter uma cópia de [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language).
Este é *o* livro sobre C, escrito pelos criadores da linguage. Mas cuidado - ele é antigo e contém alguns erros (bem,
ideias que não são consideradas boas hoje) ou práticas mudadas.

Outra boa referência é [Learn C the hard way](http://c.learncodethehardway.org/book/).

Se você tem uma pergunta, leia [compl.lang.c Frequently Asked Questions](http://c-faq.com).

É importante usar espaços e indentação adequadamente e ser consistente com seu estilo de código em geral.
Código legível é melhor que código 'esperto' e rápido. Para adotar um estilo de código bom e são, veja
[Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle).

Além disso, Google é teu amigo.
[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member
---
language: clojure
filename: learnclojure-pt.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Raphael Bezerra do Nascimento"]
lang: pt-br
---

Como todas as Lisps, a inerente [homoiconicity](https://en.wikipedia.org/wiki/Homoiconic)
do Clojure lhe dá acesso a toda a extensão da linguagem 
para escrever rotinas de geração de código chamados "macros". Macros fornecem uma poderosa forma de adequar a linguagem 
às suas necessidades.

Pórem Tenha cuidado. É considerado má pratica escrever uma macro quando uma função vai fazer. Use uma macro apenas 
quando você precisar do controle sobre quando ou se os argumentos para um formulário será avaliado.

Você vai querer estar familiarizado com Clojure. Certifique-se de entender tudo em 
[Clojure em Y Minutos](/docs/clojure/).

```clojure
;; Defina uma macro utilizando defmacro. Sua macro deve ter como saida uma lista que possa
;; ser avaliada como codigo Clojure.
;;
;; Essa macro é a mesma coisa que se você escrever (reverse "Hello World")
(defmacro my-first-macro []
  (list reverse "Hello World"))

;; Inspecione o resultado de uma macro utilizando macroexpand or macroexpand-1.
;;
;; Note que a chamada deve utilizar aspas simples.
(macroexpand '(my-first-macro))
;; -> (#<core$reverse clojure.core$reverse@xxxxxxxx> "Hello World")

;; Você pode avaliar o resultad de macroexpand diretamente:
(eval (macroexpand '(my-first-macro)))
; -> (\d \l \o \r \W \space \o \l \l \e \H)

;; mas você deve usar esse mais suscinto, sintax como de função:
(my-first-macro)  ; -> (\d \l \o \r \W \space \o \l \l \e \H)

;; Você pode tornar as coisas mais faceis pra você, utilizando a sintaxe de citação mais suscinta
;; para criar listas nas suas macros:
(defmacro my-first-quoted-macro []
  '(reverse "Hello World"))

(macroexpand '(my-first-quoted-macro))
;; -> (reverse "Hello World")
;; Note que reverse não é mais uma função objeto, mas um simbolo.

;; Macros podem ter argumentos.
(defmacro inc2 [arg]
  (list + 2 arg))

(inc2 2) ; -> 4

;; Mas se você tentar fazer isso com uma lista entre aspas simples, você vai receber um erro, por que o 
;; argumento irá entra aspas simples também. Para contornar isso, Clojure prover uma maneira de utilizar aspas simples 
;; em macros: `. Dentro `, você pode usar ~ para chegar ao escopo externo.
(defmacro inc2-quoted [arg]
  `(+ 2 ~arg))

(inc2-quoted 2)

;; Você pode usar os argumentos de destruturação habituais. Expandir lista de variaveis usando ~@
(defmacro unless [arg & body]
  `(if (not ~arg)
     (do ~@body))) ; Lembrar o do!

(macroexpand '(unless true (reverse "Hello World")))
;; ->
;; (if (clojure.core/not true) (do (reverse "Hello World")))

;; (unless) avalia e retorna seu corpo, se o primeiro argumento é falso.
;; caso contrario, retorna nil

(unless true "Hello") ; -> nil
(unless false "Hello") ; -> "Hello"

;; Usado sem cuidados, macros podem fazer muito mal por sobreporem suas variaveis
(defmacro define-x []
  '(do
     (def x 2)
     (list x)))

(def x 4)
(define-x) ; -> (2)
(list x) ; -> (2)

;;s Para evitar isso, use gensym para receber um identificador unico
(gensym 'x) ; -> x1281 (ou outra coisa)

(defmacro define-x-safely []
  (let [sym (gensym 'x)]
    `(do
       (def ~sym 2)
       (list ~sym))))

(def x 4)
(define-x-safely) ; -> (2)
(list x) ; -> (4)

;; Você pode usar # dentro de ` para produzir uma gensym para cada simbolo automaticamente
(defmacro define-x-hygenically []
  `(do
     (def x# 2)
     (list x#)))

(def x 4)
(define-x-hygenically) ; -> (2)
(list x) ; -> (4)

;; É típico o uso de funções de auxilio com macros. Vamos criar um pouco
;; Vamos criar um pouco para nos ajudar a suportar uma sintaxe aritmética inline (estupida)
(declare inline-2-helper)
(defn clean-arg [arg]
  (if (seq? arg)
    (inline-2-helper arg)
    arg))

(defn apply-arg
  "Given args [x (+ y)], return (+ x y)"
  [val [op arg]]
  (list op val (clean-arg arg)))

(defn inline-2-helper
  [[arg1 & ops-and-args]]
  (let [ops (partition 2 ops-and-args)]
    (reduce apply-arg (clean-arg arg1) ops)))

;; Podemos testar isso imediatamente, sem criar uma macro
(inline-2-helper '(a + (b - 2) - (c * 5))) ; -> (- (+ a (- b 2)) (* c 5))

; Entretanto, temos que tornar isso uma macro caso quisermos que isso seja rodado em tempo de compilação
(defmacro inline-2 [form]
  (inline-2-helper form)))

(macroexpand '(inline-2 (1 + (3 / 2) - (1 / 2) + 1)))
; -> (+ (- (+ 1 (/ 3 2)) (/ 1 2)) 1)

(inline-2 (1 + (3 / 2) - (1 / 2) + 1))
; -> 3 (Na verdade, 3N, desde que o numero ficou convertido em uma fração racional com /

```

### Leitura adicional

Escrevendo Macros de [Clojure para o Brave e True](http://www.braveclojure.com/)
[http://www.braveclojure.com/writing-macros/](http://www.braveclojure.com/writing-macros/)

Documentos oficiais 
[http://clojure.org/macros](http://clojure.org/macros)

Quando utilizar macros? 
[http://dunsmor.com/lisp/onlisp/onlisp_12.html](http://dunsmor.com/lisp/onlisp/onlisp_12.html)
---
language: clojure
filename: learnclojure-pt.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Mariane Siqueira Machado", "https://twitter.com/mariane_sm"]
lang: pt-br
---

Clojure é uma linguagem da família do Lisp desenvolvida para a JVM (máquina virtual Java). Possui uma ênfase muito mais forte em [programação funcional] (https://pt.wikipedia.org/wiki/Programa%C3%A7%C3%A3o_funcional) pura do que Common Lisp, mas inclui diversas utilidades [STM](https://en.wikipedia.org/wiki/Software_transactional_memory) para lidar com estado a medida que isso se torna necessário.

Essa combinação permite gerenciar processamento concorrente de maneira muito simples, e frequentemente de maneira automática.

(Sua versão de clojure precisa ser pelo menos 1.2)


```clojure
; Comentários começam por ponto e vírgula

; Clojure é escrito em "forms", os quais são simplesmente 
; listas de coisas dentro de parênteses, separados por espaços em branco.

; O "reader" (leitor) de Clojure presume que o primeiro elemento de 
; uma par de parênteses é uma função ou macro, e que os resto são argumentos.

: A primeira chamada de um arquivo deve ser ns, para configurar o namespace (espaço de nomes)
(ns learnclojure)

; Alguns exemplos básicos:

; str cria uma string concatenando seus argumentos
(str "Hello" " " "World") ; => "Hello World"

; Cálculos são feitos de forma direta e intuitiva
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; Você pode comparar igualdade utilizando =
(= 1 1) ; => true
(= 2 1) ; => false

; Negação para operações lógicas
(not true) ; => false

; Aninhar "forms" funciona como esperado
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; Tipos
;;;;;;;;;;;;;

; Clojure usa os tipos de objetos de Java para booleanos, strings e números.
; Use `class` para inspecioná-los
(class 1) ; Literais Integer são java.lang.Long por padrão
(class 1.); Literais Float são java.lang.Double
(class ""); Strings são sempre com aspas duplas, e são java.lang.String
(class false) ; Booleanos são java.lang.Boolean
(class nil); O valor "null" é chamado nil

; Se você quiser criar um lista de literais, use aspa simples para 
; ela não ser avaliada
'(+ 1 2) ; => (+ 1 2)
; (que é uma abreviação de (quote (+ 1 2)))

; É possível avaliar uma lista com aspa simples
(eval '(+ 1 2)) ; => 3

; Coleções e sequências
;;;;;;;;;;;;;;;;;;;

; Listas são estruturas encadeadas, enquanto vetores são implementados como arrays.
; Listas e Vetores são classes Java também!
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; Uma lista é escrita como (1 2 3), mas temos que colocar a aspa 
; simples para impedir o leitor (reader) de pensar que é uma função.
; Também, (list 1 2 3) é o mesmo que '(1 2 3)

; "Coleções" são apenas grupos de dados
; Listas e vetores são ambos coleções:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; "Sequências" (seqs) são descrições abstratas de listas de dados.
; Apenas listas são seqs.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; Um seq precisa apenas prover uma entrada quando é acessada.
; Portanto, já que seqs podem ser avaliadas sob demanda (lazy) -- elas podem definir séries infinitas:
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (uma série infinita)
(take 4 (range)) ;  (0 1 2 3)

; Use cons para adicionar um item no início de uma lista ou vetor
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; Conj adiciona um item em uma coleção sempre do jeito mais eficiente.
; Para listas, elas inserem no início. Para vetores, é inserido no final.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; Use concat para concatenar listas e vetores
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; Use filter, map para interagir com coleções
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; Use reduce para reduzi-los
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; Reduce pode receber um argumento para o valor inicial
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; Funções
;;;;;;;;;;;;;;;;;;;;;

; Use fn para criar novas funções. Uma função sempre retorna
; sua última expressão.
(fn [] "Hello World") ; => fn

; (É necessário colocar parênteses para chamá-los)
((fn [] "Hello World")) ; => "Hello World"

; Você pode atribuir valores a variáveis utilizando def
(def x 1)
x ; => 1

; Atribua uma função para uma var
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; Você pode abreviar esse processo usando defn
(defn hello-world [] "Hello World")

; O [] é uma lista de argumentos para um função.
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; Você pode ainda usar essa abreviação para criar funcões:
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; Vocé pode ter funções multi-variadic, isto é, com um número variável de argumentos
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; Funções podem agrupar argumentos extras em uma seq
(defn count-args [& args]
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"

; Você pode misturar argumentos regulares e argumentos em seq
(defn hello-count [name & args]
  (str "Hello " name ", you passed " (count args) " extra args"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"


; Mapas
;;;;;;;;;;

; Hash maps e array maps compartilham uma mesma interface. Hash maps são mais
; rápidos para pesquisa mas não mantém a ordem da chave.
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Arraymaps pode automaticamente se tornar hashmaps através da maioria das
; operações se eles ficarem grandes o suficiente, portanto não há necessida de 
; se preocupar com isso.

;Mapas podem usar qualquer valor que se pode derivar um hash como chave


; Mapas podem usar qualquer valor em que se pode derivar um hash como chave, 
; mas normalmente palavras-chave (keywords) são melhores.
; Keywords são como strings mas com algumas vantagens.
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; A propósito, vírgulas são sempre tratadas como espaçoes em branco e não fazem nada.

; Recupere o valor de um mapa chamando ele como uma função
(stringmap "a") ; => 1
(keymap :a) ; => 1

; Uma palavra-chave pode ser usada pra recuperar os valores de um mapa
(:b keymap) ; => 2

; Não tente isso com strings
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; Buscar uma chave não presente retorna nil
(stringmap "d") ; => nil

; Use assoc para adicionar novas chaves para hash-maps
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; Mas lembre-se, tipos em Clojure são sempre imutáveis!
keymap ; => {:a 1, :b 2, :c 3}

; Use dissoc para remover chaves
(dissoc keymap :a :b) ; => {:c 3}

; Conjuntos
;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; Adicione um membro com conj
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; Remova um membro com disj
(disj #{1 2 3} 1) ; => #{2 3}

; Test por existência usando set como função:
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; Existem muitas outras funções no namespace clojure.sets

; Forms úteis
;;;;;;;;;;;;;;;;;

; Construções lógicas em Clojure são como macros, e 
; se parecem com as demais
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; Use let para criar um novo escopo associando sîmbolos a valores (bindings)
(let [a 1 b 2]
  (> a b)) ; => false

; Agrupe comandos juntos com "do"
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; Funções tem um do implícito
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; Assim como let
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")

; Módulos
;;;;;;;;;;;;;;;

; Use "use" para poder usar todas as funções de um modulo
(use 'clojure.set)

; Agora nós podemos usar operações com conjuntos
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; Você pode escolher um subconjunto de funções para importar
(use '[clojure.set :only [intersection]])

; Use require para importar um módulo
(require 'clojure.string)

; Use / para chamar funções de um módulo
; Aqui, o módulo é clojure.string e a função é blank?
(clojure.string/blank? "") ; => true

; Você pode dar para um módulo um nome mais curto no import
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (#"" denota uma expressão regular literal)

; Você pode usar require (e até "use", mas escolha require) de um namespace utilizando :require.
; Não é necessário usar aspa simples nos seus módulos se você usar desse jeito.
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java tem uma biblioteca padrão enorme e muito útil, 
; portanto é importante aprender como utiliza-la.

; Use import para carregar um modulo java
(import java.util.Date)

; Você pode importar usando ns também.
(ns test
  (:import java.util.Date
           java.util.Calendar))

; Use o nome da clase com um "." no final para criar uma nova instância
(Date.) ; <a date object>

; Use . para chamar métodos. Ou, use o atalho ".method"
(. (Date.) getTime) ; <a timestamp>
(.getTime (Date.)) ; exatamente a mesma coisa.

; Use / para chamar métodos estáticos
(System/currentTimeMillis) ; <a timestamp> (o módulo System está sempre presente)

; Use doto para pode lidar com classe (mutáveis) de forma mais tolerável
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => A Date. set to 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; Software Transactional Memory é o mecanismo que Clojure usa para gerenciar
; estado persistente. Tem algumas construções em Clojure que o utilizam.

; O atom é o mais simples. Passe pra ele um valor inicial
(def my-atom (atom {}))

; Atualize o atom com um swap!.
; swap! pega uma funçnao and chama ela com o valor atual do atom
; como primeiro argumento, e qualquer argumento restante como o segundo
(swap! my-atom assoc :a 1) ; Coloca o valor do átomo my-atom como o resultado de  (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Coloca o valor do átomo my-atom como o resultado de (assoc {:a 1} :b 2)

; Use '@' para desreferenciar um atom e acessar seu valor
my-atom  ;=> Atom<#...> (Retorna o objeto do Atom)
@my-atom ; => {:a 1 :b 2}

; Abaixo um contador simples usando um atom
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; Outras construção STM são refs e agents.
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
```

### Leitura adicional

Esse tutorial está longe de ser exaustivo, mas deve ser suficiente para que você possa começar.

Clojure.org tem vários artigos:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org tem documentação com exemplos para quase todas as funções principais (pertecentes ao core):
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure é um grande jeito de aperfeiçoar suas habilidades em Clojure/Programação Funcional:
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org tem um bom número de artigos para iniciantes:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
    - ["Miguel Araújo", "https://github.com/miguelarauj1o"]
lang: pt-br
filename: learncoffeescript-pt.coffee
---

CoffeeScript é uma pequena linguagem que compila um-para-um para o JavaScript 
equivalente, e não há interpretação em tempo de execução. Como um dos sucessores 
de JavaScript, CoffeeScript tenta o seu melhor para exibir uma saída legível, 
bem-impressa e bom funcionamento dos códigos JavaScript em todo o tempo de 
execução JavaScript.

Veja também [site do CoffeeScript](http://coffeescript.org/), que tem um tutorial 
completo sobre CoffeeScript.

``` coffeescript
#CoffeeScript é uma linguagem moderna
#Segue as tendências de muitas linguagens modernas
#Assim, os comentários são iguais a Ruby e Python, eles usam símbolos numéricos.

### 
Os comentários em bloco são como estes, e eles traduzem diretamente para '/ *'s e 
'* /'s para o código JavaScript que resulta...

Você deveria entender mais de semântica de JavaScript antes de continuar... 
###

# Tarefa: 
numero = 42 #=> número var = 42; 
oposto = true #=> var oposto = true;

# Condições: 
numero = -42 if oposto #=> if (oposto) {número = -42;}

# Funções: 
quadrado = (x) -> x * x #=> var quadrado = function (x) {return x * x;}

preencher = (recipiente, líquido = "coffee") ->
  "Preenchendo o #{recipiente} with #{líquido}..."
#=>var preencher;
#
#preencher = function(recipiente, líquido) {
#  if (líquido == null) {
#    líquido = "coffee";
#  }
#  return "Preenchendo o " + recipiente + " with " + líquido + "...";
#};

# Alcances: 
list = [1 .. 5] #=> lista var = [1, 2, 3, 4, 5];

# Objetos:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#  "root": Math.sqrt,
#  "square": square,
#  "cube": function(x) { return x * square(x); }
#}

# Splats:
corrida = (vencedor, corredores...) ->
  print vencedor, corredores
#=>corrida = function() {
#  var corredores, vencedor;
#  vencedor = arguments[0], corredores = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#  return print(vencedor, corredores);
#};

# Existências:
alert "Eu sabia!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("Eu sabia!"); }

# Compressão de Matrizes:
cubes = (math.cube num for num in list) 
#=>cubes = (function() {
#   var _i, _len, _results;
#   _results = [];
#   for (_i = 0, _len = list.length; _i < _len; _i++) {
#       num = list[_i];
#       _results.push(math.cube(num));
#   }
#   return _results;
#  })();

comidas = ['brócolis', 'espinafre', 'chocolate']
eat alimento for alimento in comidas when alimento isnt 'chocolate'
#=>comidas = ['brócolis', 'espinafre', 'chocolate'];
#
#for (_k = 0, _len2 = comidas.length; _k < _len2; _k++) {
#  alimento = comidas[_k];
#  if (alimento !== 'chocolate') {
#    eat(alimento);
#  }

## Recursos adicionais

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)---
language: "Common Lisp"
filename: commonlisp-pt.lisp
contributors:
  - ["Paul Nathan", "https://github.com/pnathan"]
translators:
  - ["Édipo Luis Féderle", "https://github.com/edipofederle"]
lang: pt-br
---

ANSI Common Lisp é uma linguagem de uso geral, multi-paradigma, designada
para uma variedade de aplicações na indústria. É frequentemente citada
como uma linguagem de programação programável.


O ponto inicial clássico é [Practical Common Lisp e livremente disponível](http://www.gigamonkeys.com/book/)

Outro livro recente e popular é o
[Land of Lisp](http://landoflisp.com/).


```common_lisp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 0. Sintaxe
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; "Form" Geral


;; Lisp tem dois pedaços fundamentais de sintaxe: o ATOM e S-expression.
;; Tipicamente, S-expressions agrupadas são chamadas de `forms`.


10  ; um atom; é avaliado para ele mesmo

:THING ;Outro atom; avaliado para o símbolo :thing.

t ; outro atom, denotado true.

(+ 1 2 3 4) ; uma s-expression

'(4 :foo  t)  ;outra s-expression


;;; Comentários

;; Comentários de uma única linha começam com ponto e vírgula; usar dois para
;; comentários normais, três para comentários de seção, e quadro para comentários
;; em nível de arquivo.

#| Bloco de comentário
   pode abranger várias linhas e...
    #|
       eles podem ser aninhados
    |#
|#

;;; Ambiente

;; Existe uma variedade de implementações; a maioria segue o padrão.
;; CLISP é um bom ponto de partida.

;; Bibliotecas são gerenciadas através do Quicklisp.org's Quicklisp sistema.

;; Common Lisp é normalmente desenvolvido com um editor de texto e um REPL
;; (Read Evaluate Print Loop) rodando ao mesmo tempo. O REPL permite exploração
;; interativa do programa como ele é "ao vivo" no sistema.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 1. Tipos Primitivos e Operadores
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Símbolos

'foo ; => FOO Perceba que um símbolo é automáticamente convertido para maiúscula.

;; Intern manualmente cria um símbolo a partir de uma string.

(intern "AAAA") ; => AAAA

(intern "aaa") ; => |aaa|

;;; Números
9999999999999999999999 ; inteiro
#b111                  ; binário => 7
#o111                  ; octal => 73
#x111                  ; hexadecimal => 273
3.14159s0              ; single
3.14159d0              ; double
1/2                    ; ratios
#C(1 2)                ; números complexos


;; Funções são escritas como (f x y z ...)
;; onde f é uma função e x, y, z, ... são operadores
;; Se você quiser criar uma lista literal de dados, use ' para evitar 
;; que a lista seja avaliada - literalmente, "quote" os dados.
'(+ 1 2) ; => (+ 1 2)
;; Você também pode chamar uma função manualmente:
(funcall #'+ 1 2 3) ; => 6
;; O mesmo para operações aritiméticas
(+ 1 1)              ; => 2
(- 8 1)              ; => 7
(* 10 2)             ; => 20
(expt 2 3)           ; => 8
(mod 5 2)            ; => 1
(/ 35 5)             ; => 7
(/ 1 3)              ; => 1/3
(+ #C(1 2) #C(6 -4)) ; => #C(7 -2)

                     ;;; Booleans
t                    ; para true (qualquer valor não nil é true)
nil                  ; para false - e para lista vazia
(not nil)            ; => t
(and 0 t)            ; => t
(or 0 nil)           ; => 0

                     ;;; Caracteres
#\A                  ; => #\A
#\λ                  ; => #\GREEK_SMALL_LETTER_LAMDA
#\u03BB              ; => #\GREEK_SMALL_LETTER_LAMDA

;;; String são arrays de caracteres com tamanho fixo.
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; barra é um escape de caracter

;; String podem ser concatenadas também!
(concatenate 'string "Hello " "world!") ; => "Hello world!"

;; Uma String pode ser tratada como uma sequência de caracteres
(elt "Apple" 0) ; => #\A

;; format pode ser usado para formatar strings
(format nil "~a can be ~a" "strings" "formatted")

;; Impimir é bastante fácil; ~% indica nova linha
(format t "Common Lisp is groovy. Dude.~%")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Variáveis
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Você pode criar uma global (escopo dinâmico) usando defparameter
;; um nome de variável pode conter qualquer caracter, exceto: ()",'`;#|\

;; Variáveis de escopo dinâmico devem ter asteriscos em seus nomes!

(defparameter *some-var* 5)
*some-var* ; => 5

;; Você pode usar caracteres unicode também.
(defparameter *AΛB* nil)


;; Acessando uma variável anteriormente não ligada é um
;; comportamento não definido (mas possível). Não faça isso.

;; Ligação local: `me` é vinculado com "dance with you" somente dentro
;; de (let ... ). Let permite retornar o valor do último `form` no form let.

(let ((me "dance with you"))
  me)
;; => "dance with you"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Estruturas e Coleções
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Estruturas
(defstruct dog name breed age)
(defparameter *rover*
    (make-dog :name "rover"
              :breed "collie"
              :age 5))
*rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5)

(dog-p *rover*) ; => t  ;; ewww)
(dog-name *rover*) ; => "rover"

;; Dog-p, make-dog, e dog-name foram todas criadas por defstruct!

;;; Pares
;; `cons' constroi pares, `car' and `cdr' extrai o primeiro
;; e o segundo elemento
(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB)
(car (cons 'SUBJECT 'VERB)) ; => SUBJECT
(cdr (cons 'SUBJECT 'VERB)) ; => VERB

;;; Listas

;; Listas são estruturas de dados do tipo listas encadeadas, criadas com `cons'
;; pares e terminam `nil' (ou '()) para marcar o final da lista
(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3)
;; `list' é um construtor conveniente para listas
(list 1 2 3) ; => '(1 2 3)
;; e a quote (') também pode ser usado para um valor de lista literal
'(1 2 3) ; => '(1 2 3)

;; Ainda pode-se usar `cons' para adicionar um item no começo da lista.
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; Use `append' para - surpreendentemente - juntar duas listas
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; Ou use concatenate -

(concatenate 'list '(1 2) '(3 4))

;; Listas são um tipo muito central, então existe uma grande variedade de
;; funcionalidades para eles, alguns exemplos:
(mapcar #'1+ '(1 2 3))             ; => '(2 3 4)
(mapcar #'+ '(1 2 3) '(10 20 30))  ; => '(11 22 33)
(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4)
(every #'evenp '(1 2 3 4))         ; => nil
(some #'oddp '(1 2 3 4))           ; => T
(butlast '(subject verb object))   ; => (SUBJECT VERB)


;;; Vetores

;; Vector's literais são arrays de tamanho fixo.
#(1 2 3) ; => #(1 2 3)

;; Use concatenate para juntar dois vectors
(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; Arrays

;; Ambos vetores e strings são um caso especial de arrays.

;; 2D arrays

(make-array (list 2 2))

;; (make-array '(2 2)) também funciona.

; => #2A((0 0) (0 0))

(make-array (list 2 2 2))

; => #3A(((0 0) (0 0)) ((0 0) (0 0)))

;; Cuidado - os valores de inicialição padrões são
;; definidos pela implementção. Aqui vai como defini-lós.

(make-array '(2) :initial-element 'unset)

; => #(UNSET UNSET)

;; E, para acessar o element em 1,1,1 -
(aref (make-array (list 2 2 2)) 1 1 1)

; => 0

;;; Vetores Ajustáveis

;; Vetores ajustáveis tem a mesma representação impressa que os vectores
;;  de tamanho fixo
(defparameter *adjvec* (make-array '(3) :initial-contents '(1 2 3)
      :adjustable t :fill-pointer t))
      
*adjvec* ; => #(1 2 3)

;; Adicionando novo elemento
(vector-push-extend 4 *adjvec*) ; => 3

*adjvec* ; => #(1 2 3 4)



;;; Ingenuamente, conjuntos são apenas listas:

(set-difference '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1)
(intersection '(1 2 3 4) '(4 5 6 7)) ; => 4
(union '(1 2 3 4) '(4 5 6 7))        ; => (3 2 1 4 5 6 7)
(adjoin 4 '(1 2 3 4))     ; => (1 2 3 4)

;; Mas você irá querer usar uma estrutura de dados melhor que uma lista encadeada.
;; para performance.

;;; Dicionários são implementados como hash tables

;; Cria um hash table
(defparameter *m* (make-hash-table))

;; seta um valor
(setf (gethash 'a *m*) 1)

;; Recupera um valor
(gethash 'a *m*) ; => 1, t

;; Detalhe - Common Lisp  tem multiplos valores de retorno possíveis. gethash
;; retorna t no segundo valor se alguma coisa foi encontrada, e nil se não.

;; Recuperando um valor não presente retorna nil
 (gethash 'd *m*) ;=> nil, nil

;; Você pode fornecer um valor padrão para uma valores não encontrados
(gethash 'd *m* :not-found) ; => :NOT-FOUND

;; Vamos tratas múltiplos valores de rotorno aqui.

(multiple-value-bind
      (a b)
    (gethash 'd *m*)
  (list a b))
; => (NIL NIL)

(multiple-value-bind
      (a b)
    (gethash 'a *m*)
  (list a b))
; => (1 T)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Funções
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use `lambda' para criar funções anônimas
;; Uma função sempre retorna um valor da última expressão avaliada.
;; A representação exata impressão de uma função varia de acordo ...

(lambda () "Hello World") ; => #<FUNCTION (LAMBDA ()) {1004E7818B}>

;; Use funcall para chamar uma função lambda.
(funcall (lambda () "Hello World")) ; => "Hello World"

;; Ou Apply
(apply (lambda () "Hello World") nil) ; => "Hello World"

;; "De-anonymize" a função
(defun hello-world ()
   "Hello World")
(hello-world) ; => "Hello World"

;; O () acima é a lista de argumentos da função.
(defun hello (name)
   (format nil "Hello, ~a " name))

(hello "Steve") ; => "Hello, Steve"

;; Funções podem ter argumentos opcionais; eles são nil por padrão

(defun hello (name &optional from)
    (if from
        (format t "Hello, ~a, from ~a" name from)
        (format t "Hello, ~a" name)))

 (hello "Jim" "Alpacas") ;; => Hello, Jim, from Alpacas

;; E os padrões podem ser configurados...
(defun hello (name &optional (from "The world"))
   (format t "Hello, ~a, from ~a" name from))

(hello "Steve")
; => Hello, Steve, from The world

(hello "Steve" "the alpacas")
; => Hello, Steve, from the alpacas


;; E é claro, palavras-chaves são permitidas também... frequentemente mais
;; flexivel que &optional.

(defun generalized-greeter (name &key (from "the world") (honorific "Mx"))
    (format t "Hello, ~a ~a, from ~a" honorific name from))

(generalized-greeter "Jim")   ; => Hello, Mx Jim, from the world

(generalized-greeter "Jim" :from "the alpacas you met last summer" :honorific "Mr")
; => Hello, Mr Jim, from the alpacas you met last summer

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Igualdade
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Common Lisp tem um sistema sofisticado de igualdade. Alguns são cobertos aqui.

;; Para número use `='
(= 3 3.0) ; => t
(= 2 1) ; => nil

;; para identidade de objeto (aproximadamente) use `eql`
(eql 3 3) ; => t
(eql 3 3.0) ; => nil
(eql (list 3) (list 3)) ; => nil

;; para listas, strings, e para pedaços de vetores use `equal'
(equal (list 'a 'b) (list 'a 'b)) ; => t
(equal (list 'a 'b) (list 'b 'a)) ; => nil

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Fluxo de Controle
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Condicionais

(if t                ; testa a expressão
    "this is true"   ; então expressão
    "this is false") ; senão expressão
; => "this is true"

;; Em condicionais, todos valores não nulos são tratados como true
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'YEP

;; `cond' encadeia uma série de testes para selecionar um resultado
(cond ((> 2 2) (error "wrong!"))
      ((< 2 2) (error "wrong again!"))
      (t 'ok)) ; => 'OK

;; Typecase é um condicional que escolhe uma de seus cláusulas com base do tipo
;; do seu valor

(typecase 1
  (string :string)
  (integer :int))

; => :int

;;; Interação

;; Claro que recursão é suportada:

(defun walker (n)
  (if (zerop n)
      :walked
      (walker (1- n))))

(walker 5) ; => :walked

;; Na maioria das vezes, nós usamos DOTLISO ou LOOP

(dolist (i '(1 2 3 4))
  (format t "~a" i))

; => 1234

(loop for i from 0 below 10
      collect i)

; => (0 1 2 3 4 5 6 7 8 9)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Mutação
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use `setf' para atribuir um novo valor para uma variável existente. Isso foi
;; demonstrado anteriormente no exemplo da hash table.

(let ((variable 10))
    (setf variable 2))
 ; => 2


;; Um bom estilo Lisp é para minimizar funções destrutivas e para evitar 
;; mutação quando razoável.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Classes e Objetos
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Sem clases Animal, vamos usar os veículos de transporte de tração
;; humana mecânicos.

(defclass human-powered-conveyance ()
  ((velocity
    :accessor velocity
    :initarg :velocity)
   (average-efficiency
    :accessor average-efficiency
   :initarg :average-efficiency))
  (:documentation "A human powered conveyance"))

;; defcalss, seguido do nome, seguido por uma list de superclass,
;; seguido por um uma 'slot list', seguido por qualidades opcionais como
;; :documentation

;; Quando nenhuma lista de superclasse é setada, uma lista padrão para
;; para o objeto padrão é usada. Isso *pode* ser mudado, mas não até você
;; saber o que está fazendo. Olhe em Art of the Metaobject Protocol
;; para maiores informações.

(defclass bicycle (human-powered-conveyance)
  ((wheel-size
    :accessor wheel-size
    :initarg :wheel-size
    :documentation "Diameter of the wheel.")
   (height
    :accessor height
    :initarg :height)))

(defclass recumbent (bicycle)
  ((chain-type
    :accessor chain-type
    :initarg  :chain-type)))

(defclass unicycle (human-powered-conveyance) nil)

(defclass canoe (human-powered-conveyance)
  ((number-of-rowers
    :accessor number-of-rowers
    :initarg :number-of-rowers)))


;; Chamando DESCRIBE na classe human-powered-conveyance no REPL dá:

(describe 'human-powered-conveyance)

; COMMON-LISP-USER::HUMAN-POWERED-CONVEYANCE
;  [symbol]
;
; HUMAN-POWERED-CONVEYANCE names the standard-class #<STANDARD-CLASS
;                                                    HUMAN-POWERED-CONVEYANCE>:
;  Documentation:
;    A human powered conveyance
;  Direct superclasses: STANDARD-OBJECT
;  Direct subclasses: UNICYCLE, BICYCLE, CANOE
;  Not yet finalized.
;  Direct slots:
;    VELOCITY
;      Readers: VELOCITY
;      Writers: (SETF VELOCITY)
;    AVERAGE-EFFICIENCY
;      Readers: AVERAGE-EFFICIENCY
;      Writers: (SETF AVERAGE-EFFICIENCY)

;; Note o comportamento reflexivo disponível para você! Common Lisp é
;; projetada para ser um sistema interativo.

;; Para definir um métpdo, vamos encontrar o que nossa cirunferência da
;; roda da bicicleta usando a equação: C = d * pi

(defmethod circumference ((object bicycle))
  (* pi (wheel-size object)))

;; pi já é definido para a gente em Lisp!

;; Vamos supor que nós descobrimos que o valor da eficiência do número
;; de remadores em uma canoa é aproximadamente logarítmica. Isso provavelmente
;; deve ser definido no construtor / inicializador.

;; Veja como initializar sua instância após Common Lisp ter construído isso:

(defmethod initialize-instance :after ((object canoe) &rest args)
  (setf (average-efficiency object)  (log (1+ (number-of-rowers object)))))

;; Em seguida, para a construção de uma ocorrência e verificar a eficiência média ...

(average-efficiency (make-instance 'canoe :number-of-rowers 15))
; => 2.7725887




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Macros permitem que você estenda a sintaxe da lingaugem

;; Common Lisp não vem com um loop WHILE - vamos adicionar um.
;; Se obedecermos nossos instintos 'assembler', acabamos com:

(defmacro while (condition &body body)
    "Enquanto `condition` é verdadeiro, `body` é executado.

`condition` é testado antes de cada execução do `body`"
    (let ((block-name (gensym)))
        `(tagbody
           (unless ,condition
               (go ,block-name))
           (progn
           ,@body)
           ,block-name)))

;; Vamos dar uma olhada em uma versão alto nível disto:


(defmacro while (condition &body body)
    "Enquanto `condition` for verdadeira, `body` é executado.

`condition` é testado antes de cada execução do `body`"
  `(loop while ,condition
         do
         (progn
            ,@body)))

;; Entretanto, com um compilador moderno, isso não é preciso; o LOOP
;; 'form' compila igual e é bem mais fácil de ler.

;; Noteq ue ``` é usado , bem como `,` e `@`. ``` é um operador 'quote-type'
;; conhecido como 'quasiquote'; isso permite o uso de `,` . `,` permite "unquoting"
;; e variáveis. @ interpolará listas.

;; Gensym cria um símbolo único garantido que não existe em outras posições
;; o sistema. Isto é porque macros são expandidas em tempo de compilação e
;; variáveis declaradas na macro podem colidir com as variáveis usadas na
;; código regular.

;; Veja Practical Common Lisp para maiores informações sobre macros.
```


## Leitura Adicional

[Continua em frente com  Practical Common Lisp book.](http://www.gigamonkeys.com/book/)


## Créditos

Muitos  agradecimentos ao pessoal de Schema por fornecer um grande ponto de partida
o que facilitou muito a migração para Common Lisp.

- [Paul Khuong](https://github.com/pkhuong) pelas grandes revisões.
---
language: c#
filename: csharp-pt.cs
contributors:
    - ["Robson Alves", "http://robsonalves.net/"]    
lang: pt-br
---

C# é uma linguagem elegante e altamente tipado orientada a objetos que permite aos desenvolvedores criarem uma variedade de aplicações seguras e robustas que são executadas no .NET Framework.

[Read more here.](http://msdn.microsoft.com/pt-br/library/vstudio/z1zx9t92.aspx)

```c#
// Comentário de linha única começa com //
/*
Múltipas linhas é desta forma
*/
/// <summary>
/// Esta é uma documentação comentário XML que pode ser usado para gerar externo
/// documentação ou fornecer ajuda de contexto dentro de um IDE
/// </summary>
//public void MethodOrClassOrOtherWithParsableHelp() {}

// Especificar qual namespace seu código irá usar
// Os namespaces a seguir são padrões do .NET Framework Class Library 
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.IO;

// Mas este aqui não é :
using System.Data.Entity;
// Para que consiga utiliza-lo, você precisa adicionar novas referências
// Isso pode ser feito com o gerenciador de pacotes NuGet : `Install-Package EntityFramework`

// Namespaces são escopos definidos para organizar o códgo em "pacotes" or "módulos"
// Usando este código a partir de outra arquivo de origem: using Learning.CSharp;
namespace Learning.CSharp
{
    // Cada .cs deve conter  uma classe com o mesmo nome do arquivo
    // você está autorizado a contrariar isto, mas evite por sua sanidade.
    public class AprenderCsharp
    {
        // Sintaxe Básica - Pule para as CARACTERÍSTICAS INTERESSANTES se você ja usou Java ou C++ antes.
        public static void Syntax()
        {
            // Use Console.WriteLine para apresentar uma linha
            Console.WriteLine("Hello World");
            Console.WriteLine(
                "Integer: " + 10 +
                " Double: " + 3.14 +
                " Boolean: " + true);

            // Para apresentar sem incluir uma nova linha, use Console.Write
            Console.Write("Hello ");
            Console.Write("World");

            ///////////////////////////////////////////////////
            // Tpos e Variáveis
            //
            // Declare uma variável usando <tipo> <nome>
            ///////////////////////////////////////////////////

            // Sbyte - Signed 8-bit integer
            // (-128 <= sbyte <= 127)
            sbyte fooSbyte = 100;

            // Byte - Unsigned 8-bit integer
            // (0 <= byte <= 255)
            byte fooByte = 100;

            // Short - 16-bit integer
            // Signed - (-32,768 <= short <= 32,767)
            // Unsigned - (0 <= ushort <= 65,535)
            short fooShort = 10000;
            ushort fooUshort = 10000;

            // Integer - 32-bit integer
            int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647)
            uint fooUint = 1; // (0 <= uint <= 4,294,967,295)

            // Long - 64-bit integer
            long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
            ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
            // Numbers default to being int or uint depending on size.
            // L is used to denote that this variable value is of type long or ulong

            // Double - Double-precision 64-bit IEEE 754 Floating Point
            double fooDouble = 123.4; // Precision: 15-16 digits

            // Float - Single-precision 32-bit IEEE 754 Floating Point
            float fooFloat = 234.5f; // Precision: 7 digits
            // f is used to denote that this variable value is of type float

            // Decimal - a 128-bits data type, with more precision than other floating-point types,
            // suited for financial and monetary calculations
            decimal fooDecimal = 150.3m;

            // Boolean - true & false
            bool fooBoolean = true; // or false

            // Char - A single 16-bit Unicode character
            char fooChar = 'A';

            // Strings - ao contrário dos anteriores tipos base, que são todos os tipos de valor,
            // Uma string é um tipo de referência. Ou seja, você pode configurá-lo como nulo
            string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs)";
            Console.WriteLine(fooString);

            //  Você pode acessar todos os caracteres de string com um indexador: 
            char charFromString = fooString[1]; // => 'e'
            // Strings são imutáveis: você não pode fazer fooString[1] = 'X';

            // Compare strings com sua atual cultura, ignorando maiúsculas e minúsculas
            string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);

            // Formatando, baseado no sprintf
            string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);

            // Datas e formatações
            DateTime fooDate = DateTime.Now;
            Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));

            // Você pode juntar um string em mais de duas linhas com o símbolo @. Para escapar do " use ""
            string bazString = @"Here's some stuff
on a new line! ""Wow!"", the masses cried";

            // Use const ou read-only para fazer uma variável imutável
            // os valores da const são calculados durante o tempo de compilação
            const int HoursWorkPerWeek = 9001;

            ///////////////////////////////////////////////////
            // Estrutura de Dados
            ///////////////////////////////////////////////////

            // Matrizes - zero indexado
            // O tamanho do array pode ser decidido ainda na declaração
            // O formato para declarar uma matriz é o seguinte: 
            // <tipodado>[] <var nome> = new <tipodado>[<array tamanho>];
            int[] intArray = new int[10];

            // Outra forma de declarar & inicializar uma matriz
            int[] y = { 9000, 1000, 1337 };

            // Indexando uma matriz - Acessando um elemento
            Console.WriteLine("intArray @ 0: " + intArray[0]);
            // Matriz são alteráveis
            intArray[1] = 1;

            // Listas
            // Listas são usadas frequentemente tanto quanto matriz por serem mais flexiveis
            // O formato de declarar uma lista é o seguinte:
            // List<tipodado> <var nome> = new List<tipodado>();
            List<int> intList = new List<int>();
            List<string> stringList = new List<string>();
            List<int> z = new List<int> { 9000, 1000, 1337 }; // inicializar
            // O <> são para genéricos - Confira está interessante seção do material

            // Lista não possuem valores padrão.
            // Um valor deve ser adicionado antes e depois acessado pelo indexador
            intList.Add(1);
            Console.WriteLine("intList @ 0: " + intList[0]);

            // Outras estruturas de dados para conferir:
            // Pilha/Fila
            // Dicionário (uma implementação de map de hash)
            // HashSet
            // Read-only Coleção
            // Tuple (.Net 4+)

            ///////////////////////////////////////
            // Operadores
            ///////////////////////////////////////
            Console.WriteLine("\n->Operators");

            int i1 = 1, i2 = 2; // Forma curta para declarar diversas variáveis

            // Aritmética é clara
            Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3

            // Modulo
            Console.WriteLine("11%3 = " + (11 % 3)); // => 2

            // Comparações de operadores
            Console.WriteLine("3 == 2? " + (3 == 2)); // => falso
            Console.WriteLine("3 != 2? " + (3 != 2)); // => verdadeiro
            Console.WriteLine("3 > 2? " + (3 > 2)); // => verdadeiro
            Console.WriteLine("3 < 2? " + (3 < 2)); // => falso
            Console.WriteLine("2 <= 2? " + (2 <= 2)); // => verdadeiro
            Console.WriteLine("2 >= 2? " + (2 >= 2)); // => verdadeiro

            // Operadores bit a bit (bitwise)
            /*
            ~       Unário bitwise complemento
            <<      Signed left shift
            >>      Signed right shift
            &       Bitwise AND
            ^       Bitwise exclusivo OR
            |       Bitwise inclusivo OR
            */

            // Incrementações
            int i = 0;
            Console.WriteLine("\n->Inc/Dec-rementation");
            Console.WriteLine(i++); //i = 1. Post-Incrementation
            Console.WriteLine(++i); //i = 2. Pre-Incrementation
            Console.WriteLine(i--); //i = 1. Post-Decrementation
            Console.WriteLine(--i); //i = 0. Pre-Decrementation

            ///////////////////////////////////////
            // Estrutura de Controle
            ///////////////////////////////////////
            Console.WriteLine("\n->Control Structures");

            // Declaração if é como a linguagem C
            int j = 10;
            if (j == 10)
            {
                Console.WriteLine("I get printed");
            }
            else if (j > 10)
            {
                Console.WriteLine("I don't");
            }
            else
            {
                Console.WriteLine("I also don't");
            }

            // Operador Ternário
            // Um simples if/else pode ser escrito da seguinte forma
            // <condição> ? <verdadeiro> : <falso>
            int toCompare = 17;
            string isTrue = toCompare == 17 ? "True" : "False";

            // While loop
            int fooWhile = 0;
            while (fooWhile < 100)
            {
                //Iterated 100 times, fooWhile 0->99
                fooWhile++;
            }

            // Do While Loop
            int fooDoWhile = 0;
            do
            {
                // Inicia a interação 100 vezes, fooDoWhile 0->99
                if (false)
                    continue; // pule a intereção atual para apróxima

                fooDoWhile++;

                if (fooDoWhile == 50)
                    break; // Interrompe o laço inteiro

            } while (fooDoWhile < 100);

            //estrutura de loop for  => for(<declaração para começar>; <condicional>; <passos>)
            for (int fooFor = 0; fooFor < 10; fooFor++)
            {
                //Iterado 10 vezes, fooFor 0->9
            }

            // For Each Loop
            // Estrutura do foreach  => foreach(<Tipo Iterador> <Nome do Iterador> in <enumerable>)
            // O laço foreach  percorre sobre qualquer objeto que implementa IEnumerable ou IEnumerable<T>
            // Toda a coleção de tipos  (Array, List, Dictionary...) no .Net framework
            // implementa uma ou mais destas interfaces.
            // (O ToCharArray() pode ser removido, por que uma string também implementa IEnumerable)
            foreach (char character in "Hello World".ToCharArray())
            {
                //Iterated over all the characters in the string
            }

            // Switch Case
            // Um switch funciona com os tipos de dados byte, short, char, e int.
            // Isto também funcional com tipos enumeradors (discutidos em Tipos Enum),
            // A classe String, and a few special classes that wrap
            // tipos primitívos: Character, Byte, Short, and Integer.
            int month = 3;
            string monthString;
            switch (month)
            {
                case 1:
                    monthString = "January";
                    break;
                case 2:
                    monthString = "February";
                    break;
                case 3:
                    monthString = "March";
                    break;
                // You can assign more than one case to an action
                // But you can't add an action without a break before another case
                // (if you want to do this, you would have to explicitly add a goto case x
                case 6:
                case 7:
                case 8:
                    monthString = "Summer time!!";
                    break;
                default:
                    monthString = "Some other month";
                    break;
            }

            ///////////////////////////////////////
            // Converting Data Types And Typecasting
            ///////////////////////////////////////

            // Converting data

            // Convert String To Integer
            // this will throw a FormatException on failure
            int.Parse("123");//returns an integer version of "123"

            // try parse will default to type default on failure
            // in this case: 0
            int tryInt;
            if (int.TryParse("123", out tryInt)) // Function is boolean
                Console.WriteLine(tryInt);       // 123

            // Convert Integer To String
            // Convert class has a number of methods to facilitate conversions
            Convert.ToString(123);
            // or
            tryInt.ToString();

            // Casting
            // Cast decimal 15 to a int
            // and then implicitly cast to long
            long x = (int) 15M;
        }

        ///////////////////////////////////////
        // CLASSES - see definitions at end of file
        ///////////////////////////////////////
        public static void Classes()
        {
            // See Declaration of objects at end of file

            // Use new to instantiate a class
            Bicycle trek = new Bicycle();

            // Call object methods
            trek.SpeedUp(3); // You should always use setter and getter methods
            trek.Cadence = 100;

            // ToString is a convention to display the value of this Object.
            Console.WriteLine("trek info: " + trek.Info());

            // Instantiate a new Penny Farthing
            PennyFarthing funbike = new PennyFarthing(1, 10);
            Console.WriteLine("funbike info: " + funbike.Info());

            Console.Read();
        } // End main method

        // CONSOLE ENTRY A console application must have a main method as an entry point
        public static void Main(string[] args)
        {
            OtherInterestingFeatures();
        }

        //
        // INTERESTING FEATURES
        //

        // DEFAULT METHOD SIGNATURES

        public // Visibility
        static // Allows for direct call on class without object
        int // Return Type,
        MethodSignatures(
            int maxCount, // First variable, expects an int
            int count = 0, // will default the value to 0 if not passed in
            int another = 3,
            params string[] otherParams // captures all other parameters passed to method
        )
        {
            return -1;
        }

        // Methods can have the same name, as long as the signature is unique
        // A method that differs only in return type is not unique
        public static void MethodSignatures(
            ref int maxCount, // Pass by reference
            out int count)
        {
            count = 15; // out param must be assigned before control leaves the method
        }

        // GENERICS
        // The classes for TKey and TValue is specified by the user calling this function.
        // This method emulates the SetDefault of Python
        public static TValue SetDefault<TKey, TValue>(
            IDictionary<TKey, TValue> dictionary,
            TKey key,
            TValue defaultItem)
        {
            TValue result;
            if (!dictionary.TryGetValue(key, out result))
                return dictionary[key] = defaultItem;
            return result;
        }

        // You can narrow down the objects that are passed in
        public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
        {
            // We can iterate, since T is a IEnumerable
            foreach (var item in toPrint)
                // Item is an int
                Console.WriteLine(item.ToString());
        }

        public static void OtherInterestingFeatures()
        {
            // OPTIONAL PARAMETERS
            MethodSignatures(3, 1, 3, "Some", "Extra", "Strings");
            MethodSignatures(3, another: 3); // explicity set a parameter, skipping optional ones

            // BY REF AND OUT PARAMETERS
            int maxCount = 0, count; // ref params must have value
            MethodSignatures(ref maxCount, out count);

            // EXTENSION METHODS
            int i = 3;
            i.Print(); // Defined below

            // NULLABLE TYPES - great for database interaction / return values
            // any value type (i.e. not a class) can be made nullable by suffixing a ?
            // <type>? <var name> = <value>
            int? nullable = null; // short hand for Nullable<int>
            Console.WriteLine("Nullable variable: " + nullable);
            bool hasValue = nullable.HasValue; // true if not null

            // ?? is syntactic sugar for specifying default value (coalesce)
            // in case variable is null
            int notNullable = nullable ?? 0; // 0

            // IMPLICITLY TYPED VARIABLES - you can let the compiler work out what the type is:
            var magic = "magic is a string, at compile time, so you still get type safety";
            // magic = 9; will not work as magic is a string, not an int

            // GENERICS
            //
            var phonebook = new Dictionary<string, string>() {
                {"Sarah", "212 555 5555"} // Add some entries to the phone book
            };

            // Calling SETDEFAULT defined as a generic above
            Console.WriteLine(SetDefault<string,string>(phonebook, "Shaun", "No Phone")); // No Phone
            // nb, you don't need to specify the TKey and TValue since they can be
            // derived implicitly
            Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555

            // LAMBDA EXPRESSIONS - allow you to write code in line
            Func<int, int> square = (x) => x * x; // Last T item is the return value
            Console.WriteLine(square(3)); // 9

            // ERROR HANDLING - coping with an uncertain world
            try
            {
                var funBike = PennyFarthing.CreateWithGears(6);

                // will no longer execute because CreateWithGears throws an exception
                string some = "";
                if (true) some = null;
                some.ToLower(); // throws a NullReferenceException
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("Not so much fun now!");
            }
            catch (Exception ex) // catch all other exceptions
            {
                throw new ApplicationException("It hit the fan", ex);
                // throw; // A rethrow that preserves the callstack
            }
            // catch { } // catch-all without capturing the Exception
            finally
            {
                // executes after try or catch
            }

            // DISPOSABLE RESOURCES MANAGEMENT - let you handle unmanaged resources easily.
            // Most of objects that access unmanaged resources (file handle, device contexts, etc.)
            // implement the IDisposable interface. The using statement takes care of
            // cleaning those IDisposable objects for you.
            using (StreamWriter writer = new StreamWriter("log.txt"))
            {
                writer.WriteLine("Nothing suspicious here");
                // At the end of scope, resources will be released.
                // Even if an exception is thrown.
            }

            // PARALLEL FRAMEWORK
            // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
            var websites = new string[] {
                "http://www.google.com", "http://www.reddit.com",
                "http://www.shaunmccarthy.com"
            };
            var responses = new Dictionary<string, string>();

            // Will spin up separate threads for each request, and join on them
            // before going to the next step!
            Parallel.ForEach(websites,
                new ParallelOptions() {MaxDegreeOfParallelism = 3}, // max of 3 threads
                website =>
            {
                // Do something that takes a long time on the file
                using (var r = WebRequest.Create(new Uri(website)).GetResponse())
                {
                    responses[website] = r.ContentType;
                }
            });

            // This won't happen till after all requests have been completed
            foreach (var key in responses.Keys)
                Console.WriteLine("{0}:{1}", key, responses[key]);

            // DYNAMIC OBJECTS (great for working with other languages)
            dynamic student = new ExpandoObject();
            student.FirstName = "First Name"; // No need to define class first!

            // You can even add methods (returns a string, and takes in a string)
            student.Introduce = new Func<string, string>(
                (introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo));
            Console.WriteLine(student.Introduce("Beth"));

            // IQUERYABLE<T> - almost all collections implement this, which gives you a lot of
            // very useful Map / Filter / Reduce style methods
            var bikes = new List<Bicycle>();
            bikes.Sort(); // Sorts the array
            bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels)); // Sorts based on wheels
            var result = bikes
                .Where(b => b.Wheels > 3) // Filters - chainable (returns IQueryable of previous type)
                .Where(b => b.IsBroken && b.HasTassles)
                .Select(b => b.ToString()); // Map - we only this selects, so result is a IQueryable<string>

            var sum = bikes.Sum(b => b.Wheels); // Reduce - sums all the wheels in the collection

            // Create a list of IMPLICIT objects based on some parameters of the bike
            var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
            // Hard to show here, but you get type ahead completion since the compiler can implicitly work
            // out the types above!
            foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome))
                Console.WriteLine(bikeSummary.Name);

            // ASPARALLEL
            // And this is where things get wicked - combines linq and parallel operations
            var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
            // this will happen in parallel! Threads will automagically be spun up and the
            // results divvied amongst them! Amazing for large datasets when you have lots of
            // cores

            // LINQ - maps a store to IQueryable<T> objects, with delayed execution
            // e.g. LinqToSql - maps to a database, LinqToXml maps to an xml document
            var db = new BikeRepository();

            // execution is delayed, which is great when querying a database
            var filter = db.Bikes.Where(b => b.HasTassles); // no query run
            if (42 > 6) // You can keep adding filters, even conditionally - great for "advanced search" functionality
                filter = filter.Where(b => b.IsBroken); // no query run

            var query = filter
                .OrderBy(b => b.Wheels)
                .ThenBy(b => b.Name)
                .Select(b => b.Name); // still no query run

            // Now the query runs, but opens a reader, so only populates are you iterate through
            foreach (string bike in query)
                Console.WriteLine(result);



        }

    } // End LearnCSharp class

    // You can include other classes in a .cs file

    public static class Extensions
    {
        // EXTENSION FUNCTIONS
        public static void Print(this object obj)
        {
            Console.WriteLine(obj.ToString());
        }
    }

    // Class Declaration Syntax:
    // <public/private/protected/internal> class <class name>{
    //    //data fields, constructors, functions all inside.
    //    //functions are called as methods in Java.
    // }

    public class Bicycle
    {
        // Bicycle's Fields/Variables
        public int Cadence // Public: Can be accessed from anywhere
        {
            get // get - define a method to retrieve the property
            {
                return _cadence;
            }
            set // set - define a method to set a proprety
            {
                _cadence = value; // Value is the value passed in to the setter
            }
        }
        private int _cadence;

        protected virtual int Gear // Protected: Accessible from the class and subclasses
        {
            get; // creates an auto property so you don't need a member field
            set;
        }

        internal int Wheels // Internal: Accessible from within the assembly
        {
            get;
            private set; // You can set modifiers on the get/set methods
        }

        int _speed; // Everything is private by default: Only accessible from within this class.
                    // can also use keyword private
        public string Name { get; set; }

        // Enum is a value type that consists of a set of named constants
        // It is really just mapping a name to a value (an int, unless specified otherwise).
        // The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
        // An enum can't contain the same value twice.
        public enum BikeBrand
        {
            AIST,
            BMC,
            Electra = 42, //you can explicitly set a value to a name
            Gitane // 43
        }
        // We defined this type inside a Bicycle class, so it is a nested type
        // Code outside of this class should reference this type as Bicycle.Brand

        public BikeBrand Brand; // After declaring an enum type, we can declare the field of this type

        // Decorate an enum with the FlagsAttribute to indicate that multiple values can be switched on
        [Flags] // Any class derived from Attribute can be used to decorate types, methods, parameters etc
        public enum BikeAccessories
        {
            None = 0,
            Bell = 1,
            MudGuards = 2, // need to set the values manually!
            Racks = 4,
            Lights = 8,
            FullPackage = Bell | MudGuards | Racks | Lights
        }

        // Usage: aBike.Accessories.HasFlag(Bicycle.BikeAccessories.Bell)
        // Before .NET 4: (aBike.Accessories & Bicycle.BikeAccessories.Bell) == Bicycle.BikeAccessories.Bell
        public BikeAccessories Accessories { get; set; }

        // Static members belong to the type itself rather then specific object.
        // You can access them without a reference to any object:
        // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated);
        public static int BicyclesCreated { get; set; }

        // readonly values are set at run time
        // they can only be assigned upon declaration or in a constructor
        readonly bool _hasCardsInSpokes = false; // read-only private

        // Constructors are a way of creating classes
        // This is a default constructor
        public Bicycle()
        {
            this.Gear = 1; // you can access members of the object with the keyword this
            Cadence = 50;  // but you don't always need it
            _speed = 5;
            Name = "Bontrager";
            Brand = BikeBrand.AIST;
            BicyclesCreated++;
        }

        // This is a specified constructor (it contains arguments)
        public Bicycle(int startCadence, int startSpeed, int startGear,
                       string name, bool hasCardsInSpokes, BikeBrand brand)
            : base() // calls base first
        {
            Gear = startGear;
            Cadence = startCadence;
            _speed = startSpeed;
            Name = name;
            _hasCardsInSpokes = hasCardsInSpokes;
            Brand = brand;
        }

        // Constructors can be chained
        public Bicycle(int startCadence, int startSpeed, BikeBrand brand) :
            this(startCadence, startSpeed, 0, "big wheels", true, brand)
        {
        }

        // Function Syntax:
        // <public/private/protected> <return type> <function name>(<args>)

        // classes can implement getters and setters for their fields
        // or they can implement properties (this is the preferred way in C#)

        // Method parameters can have default values.
        // In this case, methods can be called with these parameters omitted
        public void SpeedUp(int increment = 1)
        {
            _speed += increment;
        }

        public void SlowDown(int decrement = 1)
        {
            _speed -= decrement;
        }

        // properties get/set values
        // when only data needs to be accessed, consider using properties.
        // properties may have either get or set, or both
        private bool _hasTassles; // private variable
        public bool HasTassles // public accessor
        {
            get { return _hasTassles; }
            set { _hasTassles = value; }
        }

        // You can also define an automatic property in one line
        // this syntax will create a backing field automatically.
        // You can set an access modifier on either the getter or the setter (or both)
        // to restrict its access:
        public bool IsBroken { get; private set; }

        // Properties can be auto-implemented
        public int FrameSize
        {
            get;
            // you are able to specify access modifiers for either get or set
            // this means only Bicycle class can call set on Framesize
            private set;
        }

        // It's also possible to define custom Indexers on objects.
        // All though this is not entirely useful in this example, you
        // could do bicycle[0] which yields "chris" to get the first passenger or
        // bicycle[1] = "lisa" to set the passenger. (of this apparent quattrocycle)
        private string[] passengers = { "chris", "phil", "darren", "regina" };

        public string this[int i]
        {
            get {
                return passengers[i];
            }

            set {
                return passengers[i] = value;
            }
        }

        //Method to display the attribute values of this Object.
        public virtual string Info()
        {
            return "Gear: " + Gear +
                    " Cadence: " + Cadence +
                    " Speed: " + _speed +
                    " Name: " + Name +
                    " Cards in Spokes: " + (_hasCardsInSpokes ? "yes" : "no") +
                    "\n------------------------------\n"
                    ;
        }

        // Methods can also be static. It can be useful for helper methods
        public static bool DidWeCreateEnoughBycles()
        {
            // Within a static method, we only can reference static class members
            return BicyclesCreated > 9000;
        } // If your class only needs static members, consider marking the class itself as static.


    } // end class Bicycle

    // PennyFarthing is a subclass of Bicycle
    class PennyFarthing : Bicycle
    {
        // (Penny Farthings are those bicycles with the big front wheel.
        // They have no gears.)

        // calling parent constructor
        public PennyFarthing(int startCadence, int startSpeed) :
            base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra)
        {
        }

        protected override int Gear
        {
            get
            {
                return 0;
            }
            set
            {
                throw new InvalidOperationException("You can't change gears on a PennyFarthing");
            }
        }

        public static PennyFarthing CreateWithGears(int gears)
        {
            var penny = new PennyFarthing(1, 1);
            penny.Gear = gears; // Oops, can't do this!
            return penny;
        }

        public override string Info()
        {
            string result = "PennyFarthing bicycle ";
            result += base.ToString(); // Calling the base version of the method
            return result;
        }
    }

    // Interfaces only contain signatures of the members, without the implementation.
    interface IJumpable
    {
        void Jump(int meters); // all interface members are implicitly public
    }

    interface IBreakable
    {
        bool Broken { get; } // interfaces can contain properties as well as methods & events
    }

    // Class can inherit only one other class, but can implement any amount of interfaces
    class MountainBike : Bicycle, IJumpable, IBreakable
    {
        int damage = 0;

        public void Jump(int meters)
        {
            damage += meters;
        }

        public bool Broken
        {
            get
            {
                return damage > 100;
            }
        }
    }

    /// <summary>
    /// Used to connect to DB for LinqToSql example.
    /// EntityFramework Code First is awesome (similar to Ruby's ActiveRecord, but bidirectional)
    /// http://msdn.microsoft.com/en-us/data/jj193542.aspx
    /// </summary>
    public class BikeRepository : DbContext
    {
        public BikeRepository()
            : base()
        {
        }

        public DbSet<Bicycle> Bikes { get; set; }
    }
} // End Namespace
```

## Topics Not Covered

 * Attributes
 * async/await, yield, pragma directives
 * Web Development
 	* ASP.NET MVC & WebApi (new)
 	* ASP.NET Web Forms (old)
 	* WebMatrix (tool)
 * Desktop Development
 	* Windows Presentation Foundation (WPF) (new)
 	* Winforms (old)

## Further Reading

 * [DotNetPerls](http://www.dotnetperls.com)
 * [C# in Depth](http://manning.com/skeet2)
 * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
 * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
 * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx)
 * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
 * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials)
 * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
 * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)
 * [C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx)
---
language: css
filename: learncss-pt.css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
    - ["Geoffrey Liu", "https://github.com/g-liu"]
    - ["Connor Shea", "https://github.com/connorshea"]
    - ["Deepanshu Utkarsh", "https://github.com/duci9y"]
translators:
    - ["Gabriel Gomes", "https://github.com/gabrielgomesferraz"]
lang: pt-br
---

Nos primeiros dias da web não havia elementos visuais, apenas texto puro. Mas com maior desenvolvimento de navegadores da web, páginas web totalmente visuais também se tornou comum.

CSS ajuda a manter a separação entre o conteúdo (HTML) e o look-and-feel de uma página web.

CSS permite atingir diferentes elementos em uma página HTML e atribuir diferentes propriedades visuais para eles.

Este guia foi escrito para CSS2, embora CSS3 está rapidamente se tornando popular.

**NOTA:** Porque CSS produz resultados visuais, a fim de aprender, você precisa tentar de tudo em um playground CSS como [dabblet](http://dabblet.com/).
O foco principal deste artigo é sobre a sintaxe e algumas dicas gerais.

```css
/* Comentários aparecem dentro do slash-asterisk, tal como esta linha!
   não há "comentários de uma linha"; este é o único estilo de comentário * /

/* ####################
   ## SELETORES
   #################### */

/* O seletor é usado para direcionar um elemento em uma página.
   seletor { propriedade: valor; / * Mais propriedades ... * / }

/*
Abaixo um elemento de exemplo:

<div class='class1 class2' id='anID' attr='value' otherAttr='pt-br foo bar' />
*/

/* Você pode direciona-lo usando uma das suas classes CSS */
.class1 { }

/* ou ambas as classes! */
.class1.class2 { }

/* ou o seu nome */
div { }

/* ou o seu id */
#anID { }

/* ou utilizando o fator de que tem um atributo!*/
[attr] { font-size:smaller; }

/* ou que o atributo tem um valor específico */
[attr='value'] { font-size:smaller; }

/* começa com um valor (CSS 3) */
[attr^='val'] { font-size:smaller; }

/* ou terminando com um valor (CSS 3) */
[attr$='ue'] { font-size:smaller; }


/* Ou contém um valor em uma lista separada por espaços */
[otherAttr ~ = 'foo'] {}
[otherAttr ~ = 'bar'] {}

/* Ou contém um valor em uma lista separada por hífen, ou seja, "-" (U + 002D) */
[otherAttr | = 'en'] {font-size: smaller; }


/* Você pode concatenar diferentes seletores para criar um seletor mais estreito. Não
   colocar espaços entre eles. */
classe div.some [attr $ = 'ue'] {}

/* Você pode selecionar um elemento que é filho de outro elemento */
div.some-parent> .class-name {}

/* Ou um descendente de um outro elemento. As crianças são os descendentes diretos de
   seu elemento pai, apenas um nível abaixo da árvore. Pode ser qualquer descendentes
   nivelar por baixo da árvore. */
div.some-parent class-name {}

/* Atenção: o mesmo seletor sem espaço tem um outro significado.
   Você consegue adivinhar o que? */
div.some-parent.class-name {}

/* Você também pode selecionar um elemento com base em seu irmão adjacente */
.i am just-antes + .Este elemento {}

/* Ou qualquer irmão que o precede */
.i am-qualquer-elemento antes ~ .Este elemento {}

/* Existem alguns selectores chamado pseudo classes que podem ser usados para selecionar um
   elemento quando ele está em um determinado estado */

/* Por exemplo, quando o cursor passa sobre um elemento */
seletor:hover {}

/* Ou um link foi visitado */
seletor:visited {}

/* Ou não tenha sido visitado */
seletor:link {}

/* Ou um elemento em foco */
seletor:focus {}

/* Qualquer elemento que é o primeiro filho de seu pai */
seletor:first-child {}

/* Qualquer elemento que é o último filho de seu pai */
seletor:last-child {}

/* Assim como pseudo classes, pseudo elementos permitem que você estilo certas partes de um documento */

/* Corresponde a um primeiro filho virtual do elemento selecionado */
seletor::before {}

/* Corresponde a um último filho virtual do elemento selecionado */
seletor::after {}

/* Nos locais apropriados, um asterisco pode ser utilizado como um curinga para selecionar todos
   elemento */
* {} /* */ Todos os elementos
.parent * {} /* */ todos os descendentes
.parent> * {} /* */ todas as crianças

/* ####################
   ## PROPRIEDADES
   #################### */

seletor {

    /* Unidades de comprimento pode ser absoluta ou relativa. */

    /* Unidades relativas */
    width: 50%; /* Percentagem de largura elemento pai */
    font-size: 2em; /* Múltiplos de font-size original de elemento */
    font-size: 2rem; /* Ou do elemento raiz font-size */
    font-size: 2vw; /* Múltiplos de 1% da largura da janela de exibição (CSS 3) */
    font-size: 2vh; /* Ou a sua altura */
    font-size: 2vmin; /* Qualquer um de VH ou um VW é menor */
    font-size: 2vmax; /* Ou superior */

    /* Unidades absolutas */
    width: 200px; /* píxeis */
    font-size: 20pt; /* Pontos */
    width: 5cm; /* Centímetros */
    min-width: 50mm; /* Milímetros */
    max-width: 5 polegadas; /* Polegadas */

    /* Cores */
    color: # F6E; /* Formato hexadecimal curto */
    color: # FF66EE; /* Formato hexadecimal longo */
    color: tomato; /* Uma cor nomeada */
    color: rgb (255, 255, 255); /* Como valores rgb */
    color: RGB (10%, 20%, 50%); /* Como porcentagens rgb */
    color: rgba (255, 0, 0, 0,3); /* Como valores RGBA (CSS 3) NOTA: 0 <a <1 */
    color: transparent; /* Equivale a definir o alfa a 0 */
    color: HSL (0, 100%, 50%); /* Como porcentagens HSL (CSS 3) */
    color: HSLA (0, 100%, 50%, 0,3); /* Como porcentagens HSLA com alfa */

    /* Imagens como fundos de elementos */
    background-image: url (/img-path/img.jpg); /* Citações dentro url () opcional */

    /* Fontes */
    font-family: Arial;
    /* Se o nome da família de fonte tem um espaço, deve ser citado */
    font-family: "Courier New";
    /* Se o primeiro não for encontrada, o navegador usa o próximo, e assim por diante */
    font-family: "Courier New", Trebuchet, Arial, sans-serif;
}
```

## Uso

Guardar uma folha de estilo CSS com a extensão `.css`.

```xml
<!-- Você precisa incluir o arquivo css no da sua página <head>. Isto é o
     método recomendado. Consulte http://stackoverflow.com/questions/8284365 -->
<link rel='stylesheet' type='text/css' href='path/to/style.css' />

<!-- Você também pode incluir alguns CSS inline na sua marcação. -->
<style>
   a { color: purple; }
</style>

<!-- Ou diretamente definir propriedades CSS no elemento. -->
<div style="border: 1px solid red;">
</div>
```

## Precedência ou Cascata

Um elemento pode ser alvo de vários seletores e pode ter um conjunto de propriedades em que mais de uma vez. Nestes casos, uma das regras tem precedência sobre os outros. Geralmente, uma regra em um seletor mais específico têm precedência sobre um menos específico, e uma regra que ocorre mais tarde na folha de estilo substitui uma anterior.

Este processo é chamado de cascata, portanto, as Fichas de nome de estilo em cascata.

Dado o seguinte CSS:

```css
/* UMA */
p.class1[attr="value"]

/* B */
p.class1 {}

/* C */
p.class2 {}

/* D */
p { }

/* E */
p { property: value !important; }
```

e a seguinte marcação:

```xml
<p style='/*F*/ property:value;' class='class1 class2' attr='value' />
```

A precedência de estilo é a seguinte. Lembre-se, a precedência é para cada  **propriedade**, não para todo o bloco.

* `E` tem a precedência mais alta por causa de uma palavra-chave`!important`. É recomendável que você evitar seu uso.
* `F` é a próxima, porque é um estilo interno.
* `A` é a próxima, porque é mais" específico "do que qualquer outra coisa. Tem 3 especificadores: O nome do elemento `p`, o seu `class1` classe, um atributo `attr='value'`.
* `C` está próximo, mesmo que ele tenha a mesma especificidade que `B`. Isso é porque ele aparece depois de `B`.
* `B` é o próximo.
* `D` é a última.

## Compatibilidade

A maior parte dos recursos do CSS 2 (e muitos em CSS 3) estão disponíveis em todos os navegadores e dispositivos. Mas é sempre boa prática para verificar antes de usar um novo recurso.

## Recursos

* Para executar uma verificação de compatibilidade rápida, [CanIUse](http://caniuse.com).
* CSS Playground [Dabblet](http://dabblet.com/).
* [Documentação CSS Mozilla Developer Rede](https://developer.mozilla.org/en-US/docs/Web/CSS)
* [Codrops 'Referência CSS](http://tympanus.net/codrops/css_reference/)

## Leitura adicional

* [Entendendo Estilo Precedência em CSS: Especificidade, Herança, eo Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [Selecionando elementos usando atributos](https://css-tricks.com/almanac/selectors/a/attribute/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - O empilhamento context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
* [SASS](http://sass-lang.com/) e [menos](http://lesscss.org/) para CSS pré-processamento
* [CSS-Tricks](https://css-tricks.com)
---
category: Algorithms & Data Structures
name: Dynamic Programming
contributors:
    - ["Akashdeep Goel", "http://github.com/akashdeepgoel"]
translators:
    - ["Claudson Martins", "https://github.com/claudsonm"]
lang: pt-br
---

# Programação Dinâmica

## Introdução

Programação Dinâmica é uma técnica poderosa utilizada para resolver uma classe 
particular de problemas como veremos. A ideia é bastante simples, se você 
solucionou um problema com uma dada entrada, então salve o resultado para 
referência futura, e também para evitar resolver o mesmo problema novamente.

Sempre se lembre!!
"Aqueles que não conseguem lembrar o passado estão condenados a repeti-lo"

## Maneiras de Solucionar tais Problemas

1. Top-Down (De cima para baixo): Começe solucionando o problema quebrando-o em 
partes. Se você perceber que o problema já foi resolvido, então simplemente 
pegue a resposta salva. Se ainda não foi resolvido, solucione-o e salve a 
resposta. Isso é geralmente fácil de pensar e muito intuitivo. É geralmente 
referenciado como Memorização.

2. Bottom-Up (De baixo para cima): Analise o problema e veja a ordem em que os 
subproblemas são resolvidos e começe a solucionar dos problemas mais triviais, 
até o problema dado. Neste processo, é garantido que os subproblemas são 
resolvidos antes de resoler o problema. Isto é referenciado como Programação Dinâmica.

## Exemplo de Programação Dinâmica

O problema da subsequência crescente máxima consiste em encontrar a maior 
subsequência crescente de uma dada sequência. Dada uma sequência 
S= {a1 , a2 , a3, a4, ... , an-1, an} nós temos que encontrar o maior subconjunto 
de forma que para todo j e i,  j < i no subconjunto aj < ai. Antes de mais nada 
nós temos que encontrar o valor das maiores subsequências (LSi) para cada índice 
i com o último elemento da sequência sendo ai. Então a maior LSi será a maior 
subsequência na sequência dada. Para começar LSi é atribuído a um pois ai é 
elemento da sequência (último elemento). Então para todo j tal que j < i e aj < 
ai, nós procuramos o maior LSj e o adicionamos a LSi. Portanto o algoritmo tem 
complexidade de tempo O(n2). O pseudocódigo para procurar o comprimento da 
subsequência crescente máxima: A complexidade desse algoritmo poderia ser 
reduzida utilizando uma estrutura de dados melhor que um array. Armazenando o 
array antecedente e uma variável como maiorSequenciasAteAgora e seu índice 
ajudariam a poupar muito tempo.
Um conceito similar poderia ser aplicado ao procurar o maior caminho em um 
grafo acíclico dirigido.
---------------------------------------------------------------------------
```
 for i=0 to n-1
            LS[i]=1
            for j=0 to i-1
                        if (a[i] >  a[j] and LS[i]<LS[j])
                                    LS[i] = LS[j]+1
 for i=0 to n-1
            if (largest < LS[i])
```

### Alguns Problemas Famosos de Programação Dinâmica
```
Floyd Warshall Algorithm - Tutorial and C Program source code:http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code 

Integer Knapsack Problem - Tutorial and C Program source code: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem 

Longest Common Subsequence - Tutorial and C Program source code : http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence 
```

## Recursos Online (EN)

* [codechef](https://www.codechef.com/wiki/tutorial-dynamic-programming)
---
language: elisp
contributors:
    - ["Bastien Guerry", "http://bzg.fr"]
translators:
    - ["Lucas Tadeu Teixeira", "http://ltt.me"]
lang: pt-br
filename: learn-emacs-lisp-pt.el
---

```scheme
;; Introdução ao Emacs Lisp em 15 minutos (v0.2d)
;;
;; Autor: Bastien / @bzg2 / http://bzg.fr
;;
;; Antes de começar, leia este texto escrito Peter Norvig:
;; http://norvig.com/21-days.html
;;
;; Agora instale GNU Emacs 24.3:
;;
;; Debian: apt-get install emacs (ou veja as instruções da sua distribuição)
;; OSX: http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg
;; Windows: http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip
;;
;; Informações mais gerais podem ser encontradas em:
;; http://www.gnu.org/software/emacs/#Obtaining

;; Aviso importante:
;;
;; Realizar este tutorial não danificará seu computador, a menos
;; que você fique tão irritado a ponto de jogá-lo no chão. Neste caso,
;; me abstenho de qualquer responsabilidade. Divirta-se!

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Abra o Emacs.
;;
;; Aperte a tecla `q' para ocultar a mensagem de boas vindas.
;;
;; Agora olhe para a linha cinza na parte inferior da janela:
;;
;; "*scratch*" é o nome do espaço de edição em que você se encontra.
;; Este espaço de edição é chamado "buffer".
;;
;; O buffer de rascunho (i.e., "scratch") é o buffer padrão quando
;; o Emacs é aberto. Você nunca está editando arquivos: você está
;; editando buffers que você pode salvar em um arquivo.
;;
;; "Lisp interaction" refere-se a um conjunto de comandos disponíveis aqui.
;;
;; O Emacs possui um conjunto de comandos embutidos (disponíveis em
;; qualquer buffer) e vários subconjuntos de comandos disponíveis
;; quando você ativa um modo específico. Aqui nós utilizamos
;; `lisp-interaction-mode', que possui comandos para interpretar e navegar
;; em código Elisp.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Pontos e vírgulas iniciam comentários em qualquer parte de uma linha.
;;
;; Programas codificados em Elisp são compostos por expressões simbólicas
;; (conhecidas também por "sexps"):
(+ 2 2)

;; Esta expressão simbólica significa "Some 2 e 2".

;; "Sexps" são envoltas em parêntese, possivelmente aninhados:
(+ 2 (+ 1 1))

;; Uma expressão simbólica contém átomos ou outras expressões
;; simbólicas. Nos exemplos acima, 1 e 2 são átomos;
;; (+ 2 (+ 1 1)) e (+ 1 1) são expressões simbólicas.

;; No modo `lisp-interaction-mode' você pode interpretar "sexps".
;; Posicione o cursor logo após o parêntese de fechamento e,
;; então, segure apertado Ctrl e aperte a tecla j ("C-j", em resumo).

(+ 3 (+ 1 2))
;;           ^ posicione o cursor aqui
;; `C-j' => 6

;; `C-j' insere o resultado da interpretação da expressão no buffer.

;; `C-xC-e' exibe o mesmo resultado na linha inferior do Emacs,
;; chamada de "mini-buffer".  Nós geralmente utilizaremos `C-xC-e',
;; já que não queremos poluir o buffer com texto desnecessário.

;; `setq' armazena um valor em uma variável:
(setq my-name "Bastien")
;; `C-xC-e' => "Bastien" (texto exibido no mini-buffer)

;; `insert' insere "Hello!" na posição em que se encontra seu cursor:
(insert "Hello!")
;; `C-xC-e' => "Hello!"

;; Nós executamos `insert' com apenas um argumento ("Hello!"), mas
;; mais argumentos podem ser passados --  aqui utilizamos dois:

(insert "Hello" " world!")
;; `C-xC-e' => "Hello world!"

;; Você pode utilizar variávies no lugar de strings:
(insert "Hello, I am " my-name)
;; `C-xC-e' => "Hello, I am Bastien"

;; Você pode combinar "sexps" em funções:
(defun hello () (insert "Hello, I am " my-name))
;; `C-xC-e' => hello

;; Você pode interpretar chamadas de funções:
(hello)
;; `C-xC-e' => Hello, I am Bastien

;; Os parêntesis vazios na definição da função significam que ela
;; não aceita argumentos. Mas sempre utilizar `my-name' é um tédio!
;; Vamos dizer à função para aceitar um argumento (o argumento é
;; chamado "name"):

(defun hello (name) (insert "Hello " name))
;; `C-xC-e' => hello

;; Agora vamos executar a função com a string "you" como o valor
;; para seu único parâmetro:
(hello "you")
;; `C-xC-e' => "Hello you"

;; Aí sim!

;; Respire um pouco.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Agora mude para um novo buffer chamado "*test*":

(switch-to-buffer-other-window "*test*")
;; `C-xC-e'
;; => [a tela exibirá duas janelas e o cursor estará no buffer *test*]

;; Posicione o mouse sobre a janela superior e clique com o botão
;; esquerdo para voltar. Ou você pode utilizar `C-xo' (i.e. segure
;; ctrl-x e aperte o) para voltar para a outra janela, de forma interativa.

;; Você pode combinar várias "sexps" com `progn':
(progn
  (switch-to-buffer-other-window "*test*")
  (hello "you"))
;; `C-xC-e'
;; => [A tela exibirá duas janelas e o cursor estará no buffer *test*]

;; Agora, se você não se importar, pararei de pedir que você aperte
;; `C-xC-e': faça isso para cada "sexp" que escrevermos.

;; Sempre volte para o buffer *scratch* com o mouse ou `C-xo'.

;; Frequentemente, é útil apagar o conteúdo do buffer:
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "there"))

;; Ou voltar para a outra janela:
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "you")
  (other-window 1))

;; Você pode armazenar um valor em uma variável local utilizando `let':
(let ((local-name "you"))
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello local-name)
  (other-window 1))

;; Neste caso, não é necessário utilizar `progn' já que `let' combina
;; várias "sexps".

;; Vamos formatar uma string:
(format "Hello %s!\n" "visitor")

;; %s é um espaço reservado para uma string, substituído por "visitor".
;; \n é um caractere de nova linha.

;; Vamos refinar nossa função utilizando `format':
(defun hello (name)
  (insert (format "Hello %s!\n" name)))

(hello "you")

;; Vamos criar outra função que utilize `let':
(defun greeting (name)
  (let ((your-name "Bastien"))
    (insert (format "Hello %s!\n\nI am %s."
                    name       ; the argument of the function
                    your-name  ; the let-bound variable "Bastien"
                    ))))

;; E executá-la:
(greeting "you")

;; Algumas funções são interativas:
(read-from-minibuffer "Enter your name: ")

;; Ao ser interpretada, esta função retorna o que você digitou no prompt.

;; Vamos fazer nossa função `greeting' pedir pelo seu nome:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (insert (format "Hello!\n\nI am %s and you are %s."
                    from-name ; the argument of the function
                    your-name ; the let-bound var, entered at prompt
                    ))))

(greeting "Bastien")

;; Vamos finalizá-la fazendo-a exibir os resultados em outra janela:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (insert (format "Hello %s!\n\nI am %s." your-name from-name))
    (other-window 1)))

;; Agora teste-a:
(greeting "Bastien")

;; Respire um pouco.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Vamos armazenar uma lista de nomes:
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))

;; Pegue o primeiro elemento desta lista utilizando `car':
(car list-of-names)

;; Pegue uma lista de todos os elementos, exceto o primeiro, utilizando
;; `cdr':
(cdr list-of-names)

;; Adicione um elemento ao início da lista com `push':
(push "Stephanie" list-of-names)

;; NOTA: `car' e `cdr' não modificam a lista, `push' sim.
;; Esta é uma diferença importante: algumas funções não têm qualquer
;; efeito colateral (como `car'), enquanto outras sim (como `push').

;; Vamos executar `hello' para cada elemento em `list-of-names':
(mapcar 'hello list-of-names)

;; Refine `greeting' para saudar todos os nomes em `list-of-names':
(defun greeting ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (mapcar 'hello list-of-names)
    (other-window 1))

(greeting)

;; Você se lembra da função `hello' que nós definimos lá em cima? Ela
;; recebe um argumento, um nome. `mapcar' executa `hello', sucessivamente,
;; utilizando cada elemento de `list-of-names' como argumento para `hello'.

;; Agora vamos arrumar, um pouco, o que nós temos escrito no buffer:

(defun replace-hello-by-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (search-forward "Hello")
      (replace-match "Bonjour"))
    (other-window 1))

;; (goto-char (point-min)) vai para o início do buffer.
;; (search-forward "Hello") busca pela string "Hello".
;; (while x y) interpreta a(s) sexp(s) y enquanto x retornar algo.
;; Se x retornar `nil' (nada), nós saímos do laço.

(replace-hello-by-bonjour)

;; Você deveria ver todas as ocorrências de "Hello" no buffer *test*
;; substituídas por "Bonjour".

;; Você deveria, também, receber um erro: "Search failed: Hello".
;;
;; Para evitar este erro, você precisa dizer ao `search-forward' se ele
;; deveria parar de buscar em algum ponto no buffer, e se ele deveria
;; falhar de forma silenciosa quando nada fosse encontrado:

;; (search-forward "Hello" nil t) dá conta do recado:

;; O argumento `nil' diz: a busca não está limitada a uma posição.
;; O argumento `t' diz: falhe silenciosamente quando nada for encontrado.

;; Nós utilizamos esta "sexp" na função abaixo, que não gera um erro:

(defun hello-to-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    ;; Say hello to names in `list-of-names'
    (mapcar 'hello list-of-names)
    (goto-char (point-min))
    ;; Replace "Hello" by "Bonjour"
    (while (search-forward "Hello" nil t)
      (replace-match "Bonjour"))
    (other-window 1))

(hello-to-bonjour)

;; Vamos colorir os nomes:

(defun boldify-names ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (re-search-forward "Bonjour \\(.+\\)!" nil t)
      (add-text-properties (match-beginning 1)
                           (match-end 1)
                           (list 'face 'bold)))
    (other-window 1))

;; Esta função introduz `re-search-forward': ao invés de buscar
;; pela string "Bonjour", você busca por um padrão utilizando uma
;; "expressão regular" (abreviada pelo prefixo "re-").

;; A expressão regular é "Bonjour \\(.+\\)!" e lê-se:
;; a string "Bonjour ", e
;; um grupo de               | que é o \\( ... \\)
;;   quaisquer caracteres    | que é o .
;;   possivelmente repetidos | que é o +
;; e a string "!".

;; Preparado? Teste!

(boldify-names)

;; `add-text-properties' adiciona... propriedades de texto, como uma fonte.

;; OK, terminamos por aqui. Feliz Hacking!

;; Se você quiser saber mais sobre uma variável ou função:
;;
;; C-h v uma-variável RET
;; C-h f uma-função RET
;;
;; Para ler o manual de Emacs Lisp que vem com o Emacs:
;;
;; C-h i m elisp RET
;;
;; Para ler uma introdução online ao Emacs Lisp:
;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html

;; Agradecimentos a estas pessoas por seu feedback e sugestões:
;; - Wes Hardaker
;; - notbob
;; - Kevin Montuori
;; - Arne Babenhauserheide
;; - Alan Schmitt
;; - LinXitoW
;; - Aaron Meurer
```
---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
    - ["Dzianis Dashkevich", "https://github.com/dskecse"]
translators:
    - ["Rodrigo Muniz", "http://github.com/muniz95"]
lang: pt-br
filename: learnelixir-pt.ex
---

Elixir é uma linguagem funcional moderna construída no topo da Erlang VM.
É totalmente compatível com Erlang, porém conta com uma sintaxe mais padronizada
e muitos outros recursos.

```elixir

# Comentários de linha única começam com um símbolo de número.

# Não há comentários de múltiplas linhas,
# mas você pode empilhar os comentários.

# Para usar o shell do elixir use o comando `iex`.
# Compile seus módulos com o comando `elixirc`.

# Ambos devem estar em seu path se você instalou o Elixir corretamente.

## ---------------------------
## -- Tipos Básicos
## ---------------------------

# Há números
3    # integer
0x1F # integer
3.0  # float

# Atoms, que são literais, uma constante com nome. Elas começam com `:`.
:hello # atom

# Tuplas que são guardadas contiguamente em memória.
{1,2,3} # tupla

# Podemos acessar um elemento de uma tupla om a função `elem`:
elem({1, 2, 3}, 0) #=> 1

# Listas que são implementadas como listas ligadas.
[1,2,3] # lista

# Podemos acessar a primeira posição (head) e o resto (tail) de uma lista como a seguir:
[head | tail] = [1,2,3]
head #=> 1
tail #=> [2,3]

# Em elixir, bem como em Erlang, o sinal `=` denota pattern match,
# e não uma atribuição.
#
# Isto significa que o que estiver à esquerda (pattern) é comparado com o que
# estiver à direita.
#
# É assim que o exemplo acima de acesso à head e tail de uma lista funciona.

# Um pattern match retornará erro quando os lados não conferem, como neste exemplo
# onde as tuplas tem diferentes tamanhos.
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# Também há binários
<<1,2,3>> # binary

# Strings e char lists
"hello" # string
'hello' # char list

# Strings de múltiplas linhas
"""
Strings
de múltiplas
linhas.
"""
#=> "Strings\nde múltiplas\nlinhas"

# Strings são sempre codificadas em UTF-8:
"héllò" #=> "héllò"

# Strings são de fato apenas binários, e char lists apenas listas.
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# `?a` em elixir retorna o valor ASCII para a letra `a`
?a #=> 97

# Para concatenar listas use `++`, para binários use `<>`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'hello ' ++ 'world'  #=> 'hello world'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"hello " <> "world"  #=> "hello world"

# Ranges são representados como `início..fim` (ambos inclusivos)
1..10 #=> 1..10
menor..maior = 1..10 # Pattern matching pode ser usada em ranges também
[lower, upper] #=> [1, 10]

## ---------------------------
## -- Operadores
## ---------------------------

# Matemática básica
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# Em elixir o operador `/` sempre retorna um float.

# Para divisão de inteiros use `div`
div(10, 2) #=> 5

# Para obter o resto da divisão use `rem`
rem(10, 3) #=> 1

# Há também operadores booleanos: `or`, `and` e `not`.
# Estes operadores esperam um booleano como primeiro argumento.
true and true #=> true
false or true #=> true
# 1 and true    #=> ** (ArgumentError) argument error

# Elixir também fornece `||`, `&&` e `!` que aceitam argumentos de qualquer tipo.
# Todos os valores exceto `false` e `nil` serão avaliados como true.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil
!true #=> false

# Para comparações temos: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` e `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# `===` e `!==` são mais estritos ao comparar integers e floats:
1 == 1.0  #=> true
1 === 1.0 #=> false

# Podemos comparar também dois tipos de dados diferentes:
1 < :hello #=> true

# A regra de ordenação no geral é definida abaixo:
# number < atom < reference < functions < port < pid < tuple < list < bit string

# Ao citar Joe Armstrong nisto: "A ordem de fato não é importante,
# mas que uma ordem total esteja bem definida é importante."

## ---------------------------
## -- Fluxo de Controle
## ---------------------------

# expressão `if` 
if false do
  "Isso nunca será visto"
else
  "Isso será"
end

# Também há `unless`
unless true do
  "Isso nunca será visto"
else
  "Isso será"
end

# Lembra do patter matching? Muitas estruturas de fluxo de controle em elixir contam com ela.

# `case` nos permite comparar um valor com muitos patterns:
case {:um, :dois} do
  {:quatro, :cinco} ->
    "Isso não corresponde"
  {:um, x} ->
    "Isso corresponde e vincula `x` a `:dois`"
  _ ->
    "Isso corresponde a qualquer valor"
end

# É comum vincular o valor a `_` se não precisamos dele.
# Por exemplo, se apenas a head de uma lista nos interessa:
[head | _] = [1,2,3]
head #=> 1

# Para melhor legibilidade podemos fazer o seguinte:
[head | _tail] = [:a, :b, :c]
head #=> :a

# `cond` nos permite verificar várias condições ao mesmo tempo.
# Use `cond` em vez de aninhar vários `if`'s.
cond do
  1 + 1 == 3 ->
    "Nunca serei visto"
  2 * 5 == 12 ->
    "Nem eu"
  1 + 2 == 3 ->
    "Mas eu serei"
end

# É comum definir a última condição igual a `true`, que sempre irá corresponder.
cond do
  1 + 1 == 3 ->
    "Nunca serei visto"
  2 * 5 == 12 ->
    "Nem eu"
  true ->
    "Mas eu serei (isso é essencialmente um else)"
end

# `try/catch` é usado para capturar valores que são lançados, também suporta uma
# cláusula `after` que é invocada havendo um valor capturado ou não.
try do
  throw(:hello)
catch
  message -> "Deu #{mensagem}."
after
  IO.puts("Sou o after.")
end
#=> Sou o after
# "Deu :hello"

## ---------------------------
## -- Módulos e Funções
## ---------------------------

# Funções Anônimas (repare o ponto)
square = fn(x) -> x * x end
square.(5) #=> 25

# Elas também aceitam várias cláusulas e guards.
# Guards permitem ajustes finos de pattern matching,
# sendo indicados pela palavra `when`:
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir também fornece várias funções embutidas.
# Estas estão disponíveis no escopo atual.
is_number(10)    #=> true
is_list("ola") #=> false
elem({1,2,3}, 0) #=> 1

# Você pode agrupar algumas funções em um módulo. Dentro de um módulo use `def`
# para definir suas funções.
defmodule Math do
  def sum(a, b) do
    a + b
  end

  def square(x) do
    x * x
  end
end

Math.sum(1, 2)  #=> 3
Math.square(3) #=> 9

# Para compilar o módulo Math salve-o como `math.ex` e use `elixirc`
# em seu terminal: elixirc math.ex

# Dentro de um módulo podemos definir funções com `def` e funções privadas com `defp`.
# Uma função definida com `def` pode ser invocada por outros módulos,
# já uma função privada pode ser invocada apenas localmente.
defmodule PrivateMath do
  def sum(a, b) do
    do_sum(a, b)
  end

  defp do_sum(a, b) do
    a + b
  end
end

PrivateMath.sum(1, 2)    #=> 3
# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)

# Declarações de funções também suportam guards cláusulas múltiplas:
defmodule Geometry do
  def area({:rectangle, w, h}) do
    w * h
  end

  def area({:circle, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometry.area({:rectangle, 2, 3}) #=> 6
Geometry.area({:circle, 3})       #=> 28.25999999999999801048
# Geometry.area({:circle, "not_a_number"})
#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1

# Devido à imutabilidade, recursão é uma grande parte do elixir
defmodule Recursion do
  def sum_list([head | tail], acc) do
    sum_list(tail, acc + head)
  end

  def sum_list([], acc) do
    acc
  end
end

Recursion.sum_list([1,2,3], 0) #=> 6

# Módulos do elixir suportam atributos, hpa atributos embutidos e você
# pode também adicionar os seus próprios.
defmodule MyMod do
  @moduledoc """
  Este é um atributo embutido em um módulo de exemplo.
  """

  @my_data 100 # Este é um atributo customizado.
  IO.inspect(@my_data) #=> 100
end

## ---------------------------
## -- Structs e Exceptions
## ---------------------------

# Structs são extensões no topo de mapas que trazem valores padrão,
# garantias em tempo de compilação e polimorfismo para o Elixir.
defmodule Pessoa do
  defstruct nome: nil, idade: 0, peso: 0
end

joe_info = %Pessoa{ nome: "Joe", idade: 30, peso: 180 }
#=> %Pessoa{idade: 30, peso: 180, nome: "Joe"}

# Acessa o valor de nome
joe_info.name #=> "Joe"

# Atualiza o valor de idade
older_joe_info = %{ joe_info | idade: 31 }
#=> %Pessoa{idade: 31, peso: 180, nome: "Joe"}

# O bloco `try` com a palavra `rescue` é usado para manipular exceções
try do
  raise "algum erro"
rescue
  RuntimeError -> "resgatado um erro em tempo de execução"
  _error -> "isso resgatará qualquer erro"
end

# Toda exceção possui uma mensagem
try do
  raise "algum erro"
rescue
  x in [RuntimeError] ->
    x.message
end

## ---------------------------
## -- Concorrência
## ---------------------------

# Elixir conta com o modelo de ator para concorrência. Tudo o que precisamos para
# escrever programas concorrentes em elixir são três primitivos: spawning processes,
# sending messages e receiving messages.

# Para iniciar um novo processo usamos a função `spawn`, a qual leva uma função
# como argumento.
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>

# `spawn` retorna um pid (process identifier), você pode usar esse pid para enviar
# mensagens ao processo. Para envio de mensagens usamos o operador `send`.
# Para tudo isso ser útil precisamos estar aptos a receber mensagens. Isto é
# realizado com o mecanismo `receive`:
defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Area = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        area_loop()
    end
  end
end

# Compile o módulo e crie um processo que avalie `area_loop` no shell
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>

# Envia uma mensagem ao `pid` correspondente a um pattern na declaração de recebimento
send pid, {:rectangle, 2, 3}
#=> Area = 6
#   {:rectangle,2,3}

send pid, {:circle, 2}
#=> Area = 12.56000000000000049738
#   {:circle,2}

# O shell também é um processo, você pode usar `self` para obter o pid atual
self() #=> #PID<0.27.0>
```

## Referências

* [Getting started guide](http://elixir-lang.org/getting_started/1.html) da [página do elixir](http://elixir-lang.org)
* [Elixir Documentation](http://elixir-lang.org/docs/master/)
* ["Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) por Dave Thomas
* [Elixir Cheat Sheet](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf)
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) por Fred Hebert
* ["Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) por Joe Armstrong
---
language: Elm
contributors:
    - ["Max Goldstein", "http://maxgoldste.in/"]
translators:
    - ["Marcel dos Santos", "https://twitter.com/marcelgsantos"]
lang: pt-br
filename: learnelm-pt.elm
---

Elm é uma linguagem de programação funcional reativa que compila para (client-side)
JavaScript. Elm é estaticamente tipada, significando que o compilador captura a
maioria dos erros imediatamente e fornece uma mensagem de erro limpa e compreensível.
Elm é excelente para projetar interfaces de usuário e jogos para a web.


```haskell
-- Comentários de uma linha começam com dois traços.
{- Comentários de múltiplas linhas podem ser delimitados em um bloco como este.
{- Eles podem ser aninhados. -}
-}

{-- O Básico --}

-- Operações Aritméticas
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20

-- Cada número literal sem um ponto decimal pode ser um Int ou um Float.
33 / 2 -- 16.5 com divisão de ponto flutuante
33 // 2 -- 16 com divisão inteira

-- Exponenciação
5 ^ 2 -- 25

-- Booleanos
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- Strings e caracteres
"Esta é uma string porque ela utiliza aspas duplas."
'a' -- caracteres entre aspas simples

-- Strings podem ser anexadas.
"Olá " ++ "mundo!" -- "Olá mundo!"

{-- Listas, Tuplas e Registros --}

-- Cada elemento em uma lista deve ter o mesmo tipo.
["the", "quick", "brown", "fox"]
[1, 2, 3, 4, 5]
-- O segundo exemplo também pode ser escrito com dois pontos.
[1..5]

-- Junte listas da mesma forma que strings.
[1..5] ++ [6..10] == [1..10] -- True

-- Para adicionar um item utilize "cons".
0 :: [1..5] -- [0, 1, 2, 3, 4, 5]

-- A cabeça e a cauda de uma lista são retornadas como uma Maybe. Em vez de
-- verificar cada valor para ver se ele é nulo, você lida com os valores
-- faltantes explicitamente.
List.head [1..5] -- Just 1
List.tail [1..5] -- Just [2, 3, 4, 5]
List.head [] -- Nothing
-- List.functionName siginifica que a função faz parte do módulo List.

-- Cada elemento em uma tupla pode ser de um tipo diferente, mas uma tupla
-- tem um comprimento fixo.
("elm", 42)

-- Acesse os elementos de um par com as funções first e second.
-- (Este é um atalho; nós iremos para o "caminho real" em breve.)
fst ("elm", 42) -- "elm"
snd ("elm", 42) -- 42

-- Uma tupla vazia ou "unidade" às vezes é utilizada como um placeholder.
-- É o único valor de seu tipo, também chamado de "Unit".
()

-- Registros são como tuplas mas os campos possuem nomes. A ordem dos campos
-- não importa. Observe que os valores dos registros utilizam sinais de igual,
-- e não dois-pontos.
{ x = 3, y = 7 }

-- Acesse um campo com um ponto e o nome do campo.
{ x = 3, y = 7 }.x -- 3

-- Ou com uma função acessora, que é um ponto e o nome do próprio campo.
.y { x = 3, y = 7 } -- 7

-- Atualiza os campos de um registro. (Ele já deve ter os campos.)
{ person |
  name = "George" }

-- Atualiza vários campos de uma só vez utilizando os valores atuais.
{ particle |
  position = particle.position + particle.velocity,
  velocity = particle.velocity + particle.acceleration }

{-- Fluxo de Controle --}

-- Declarações if sempre devem ter um else e os valores devem ser do mesmo tipo.
if powerLevel > 9000 then
  "WHOA!"
else
  "meh"

-- Declarações if podem ser encadeadas.
if n < 0 then
  "n é negativo"
else if n > 0 then
  "n é positivo"
else
  "n é zero"

-- Utilize declarações case para casar padrões entre diferentes possibilidades.
case aList of
  [] -> "casa com uma lista vazia"
  [x]-> "casa com uma lista de exatamente um item, " ++ toString x
  x::xs -> "casa com uma lista de pelo menos um item cuja cabeça é " ++ toString x
-- O casamento do padrão acontece na ordem. Se colocarmos [x] por último, ele
-- nunca iria casar porque x::xs também casa (xs seria a lista vazia). Os
-- casamentos não "falham".
-- O compilador irá alertá-lo sobre casos faltantes ou extras.

-- Casa padrão com um Maybe.
case List.head aList of
  Just x -> "A cabeça é " ++ toString x
  Nothing -> "A lista estava vazia."

{-- Funções --}

-- A sintaxe do Elm é muito mínima, baseando-se principalmente em espaços em
-- branco em vez de parênteses e chaves. Não existe a palavra-chave "return".

-- Define uma função com seu nome, argumentos, um sinal de igual e o corpo.
multiply a b =
  a * b

-- Aplica (chama) uma função passando seus argumentos (vírgulas não necessárias).
multiply 7 6 -- 42

-- Aplica parcialmente uma função passando somente alguns de seus argumentos.
-- Dando, em seguida, um novo nome a função.
double =
  multiply 2

-- Constantes são semelhantes, exceto que não há argumentos.
answer =
  42

-- Passa funções como argumentos para outras funções.
List.map double [1..4] -- [2, 4, 6, 8]

-- Ou escreva uma função anônima.
List.map (\a -> a * 2) [1..4] -- [2, 4, 6, 8]

-- Você pode casar um padrão na definição de funções quando há somente um caso.
-- Esta função recebe uma tupla em vez de dois argumentos.
-- Esta é a maneira que você normalmente vai desempacotar/extrair valores de tuplas.
area (width, height) =
  width * height

area (6, 7) -- 42

-- Utilize chaves para casar o padrão de nomes de campos de um registro.
-- Utilize let para definir valores intermediários.
volume {width, height, depth} =
  let
    area = width * height
  in
    area * depth

volume { width = 3, height = 2, depth = 7 } -- 42

-- Funções podem ser recursivas.
fib n =
  if n < 2 then
    1
  else
    fib (n - 1) + fib (n - 2)

List.map fib [0..8] -- [1, 1, 2, 3, 5, 8, 13, 21, 34]

-- Outra função recursiva (utilize List.length em um código de verdade).
listLength aList =
  case aList of
    [] -> 0
    x::xs -> 1 + listLength xs

-- Chamadas de funções acontecem antes de qualquer operador infixo.
-- Os parênteses indicam a precendência.
cos (degrees 30) ^ 2 + sin (degrees 30) ^ 2 -- 1
-- Primeiro degrees é aplicada em 30, então o resultado é passado para as
-- funções de trigonometria, que então é elevado ao quadrado e, por fim, a
-- adição acontece.

{-- Tipos e Anotações de Tipos --}

-- O compilador irá inferir o tipo de cada valor em seu programa.
-- Tipos iniciam com letra maiúscula. Leia x : T como "x é do tipo T".
-- Alguns tipos comuns que você pode ver no REPL do Elm.
5 : Int
6.7 : Float
"hello" : String
True : Bool

-- Funções têm tipos também. Leia -> como "vai para". Pense no tipo mais à
-- direita como o tipo do valor de retorno e os outros como argumentos.
not : Bool -> Bool
round : Float -> Int

-- Quando você define um valor, é uma boa prática escrever seu tipo acima dele.
-- A anotação é uma forma de documentação, que é verifica pelo compilador.
double : Int -> Int
double x = x * 2

-- Argumentos de uma função são passados entre parênteses.
-- Tipos com letra minúscula são tipos variáveis: eles podem ser de qualquer
-- tipo, desde que cada chamada seja consistente.
List.map : (a -> b) -> List a -> List b
-- "List.map é do tipo a-vai-para-b, vai para lista de a e vai para lista de b."

-- Existem três tipos especiais com minúsculas: number, comparable e appendable.
-- Numbers permite que você utilize aritmética em Ints e Floats.
-- Comparable permite você ordenar números e strings, como a < b.
-- Appendable permite que coisas possam ser combinadas com a ++ b.

{-- Type Aliases e Union Types --}

-- Quando você escreve um registro ou uma tupla, seu tipo já existe.
-- (Observe que os tipos de um registro utilizam dois-pontos e os valores de um
-- registro utilizam igual.)
origin : { x : Float, y : Float, z : Float }
origin =
  { x = 0, y = 0, z = 0 }

-- Você pode dar um bom nome para tipos existentes com um type alias.
type alias Point3D =
  { x : Float, y : Float, z : Float }

-- Se você cria um alias para um registro, você pode usar o nome como uma
-- função construtora.
otherOrigin : Point3D
otherOrigin =
  Point3D 0 0 0

-- Mas ele ainda é do mesmo tipo, então você pode compará-los.
origin == otherOrigin -- True

-- Por outro lado, a definição de um union type cria um tipo que não existia
-- antes. Um union type é chamado assim porque ele pode ser uma de muitas
-- possibilidades. Cada uma das possibilidades é representada como uma "tag".
type Direction =
  North | South | East | West

-- As tags podem levar outros valores de tipos conhecidos. Isso pode trabalhar
-- recursivamente.
type IntTree =
  Leaf | Node Int IntTree IntTree
-- "Leaf" e "Node" são as tags. Tudo após uma tag é um tipo.

-- As tags podem ser usadas como valores ou funções.
root : IntTree
root =
  Node 7 Leaf Leaf

-- Union types (e type aliases) podem utilizar tipos variáveis.
type Tree a =
  Leaf | Node a (Tree a) (Tree a)
-- "O tipo árvore-de-a é uma folha ou um nó de a, árvore-de-a e árvore-de-a."

-- Casa padrão com union tags. As tags maiúsculas serão casadas de maneira exa-
-- ta. As variáveis minúsculas irão casar com qualquer coisa. Sublinhado também
-- casa com qualquer coisa, mas siginifica que você não o está utilizando.
leftmostElement : Tree a -> Maybe a
leftmostElement tree =
  case tree of
    Leaf -> Nothing
    Node x Leaf _ -> Just x
    Node _ subtree _ -> leftmostElement subtree

-- Isso é praticamente a própria linguagem. Agora vamos ver como organizar e
-- executar seu código.

{-- Módulos e Imports --}

-- As bibliotecas internas são organizadas em módulos, assim como quaisquer
-- bibliotecas de terceiros que você possa utilizar. Para grandes projetos,
-- você pode definir seus próprios módulos.

-- Coloque isso no topo do arquivo. Se for omitido, você está no Main.
module Name where

-- Por padrão, tudo é exportado. Você pode especificar as exportações de forma
-- explícita.
module Name (MyType, myValue) where

-- Um padrão comum é exportar um union type mas não suas tags. Isto é conhecido
-- como "tipo opaco" e é frequentemente utilizado em bibliotecas.

-- Importe código de outros módulos para utilizá-lo no seu código.
-- Coloque Dict no escopo para você poder chamar Dict.insert.
import Dict

-- Importe o módulo Dict e o tipo Dict para que suas anotações não tenham que
-- dizer Dict.Dict. Você ainda pode utilizar Dict.insert.
import Dict exposing (Dict)

-- Renomeie um import.
import Graphics.Collage as C

{-- Portas --}

-- Uma porta indica que você estará se comunicando com o mundo exterior.
-- Portas são permitidas somente no módulo Main.

-- Uma porta de entrada é apenas uma assinatura de tipo.
port clientID : Int

-- Uma porta de saída tem uma definição.
port clientOrders : List String
port clientOrders = ["Books", "Groceries", "Furniture"]

-- Não vamos entrar em detalhes, mas você configura callbacks no JavaScript
-- para enviar nas portas de entrada e receber nas portas de saída.

{-- Ferramentas de Linha de Comando --}

-- Compila um arquivo.
$ elm make MyFile.elm

-- A primeira vez que você fizer isso, o Elm instalará as bibliotecas internas
-- e criará o elm-package.json, onde a informação sobre seu projeto é mantida.

-- O reactor é um servidor que compila e roda seus arquivos.
-- Clique na chave ao lado dos nomes de arquivo para entrar no depurador de
-- viagem no tempo.
$ elm reactor

-- Teste expressões simples no Read-Eval-Print Loop.
$ elm repl

-- Pacotes são identificados pelo usuário e nome do repositório no GitHub.
-- Instale um novo pacote e registre-o no elm-package.json.
$ elm package install evancz/elm-html

-- Veja o que mudou entre as versões de um pacote.
$ elm package diff evancz/elm-html 3.0.0 4.0.2
-- O gerenciador de pacotes do Elm obriga o versionamento semântico, logo
-- mudanças de versões no minor nunca quebrará o seu build!
```

A linguagem Elm é supreendentemente pequena. Agora você pode olhar para quase
qualquer código-fonte em Elm e ter uma ideia aproximada do que está acontecendo.
No entanto, as possibilidades para código resistente a erros e de fácil
refatoração são infinitas!

Aqui estão algumas referências utéis.

* O [site do Elm](http://elm-lang.org/). Ele inclui:
  * Links para os [instaladores](http://elm-lang.org/install)
  * [Documentação](http://elm-lang.org/docs), incluindo [a referência de sintaxe](http://elm-lang.org/docs/syntax)
  * Muitos [exemplos](http://elm-lang.org/examples) úteis

* Documentação para as [bibliotecas internas do Elm](http://package.elm-lang.org/packages/elm-lang/core/latest/). Tome nota de:
  * [Basics](http://package.elm-lang.org/packages/elm-lang/core/latest/Basics), que é importada por padrão
  * [Maybe](http://package.elm-lang.org/packages/elm-lang/core/latest/Maybe) e seu primo [Result](http://package.elm-lang.org/packages/elm-lang/core/latest/Result), comumente utilizados para valores faltantes e manipulação de erros
  * Estruturas de dados como [List](http://package.elm-lang.org/packages/elm-lang/core/latest/List), [Array](http://package.elm-lang.org/packages/elm-lang/core/latest/Array), [Dict](http://package.elm-lang.org/packages/elm-lang/core/latest/Dict) e [Set](http://package.elm-lang.org/packages/elm-lang/core/latest/Set)
  * [Codificação](http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Encode) e [decodificação](http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Decode) JSON

* [A Arquitetura Elm](https://github.com/evancz/elm-architecture-tutorial#the-elm-architecture). Uma dissertação pelo criador do Elm com exemplos sobre como organizar código em componentes.

* A [lista de e-mail do Elm](https://groups.google.com/forum/#!forum/elm-discuss). Todos são amigáveis e solícitos.

* [Escopo em Elm](https://github.com/elm-guides/elm-for-js/blob/master/Scope.md#scope-in-elm) e [Como Ler uma Anotação de Tipo](https://github.com/elm-guides/elm-for-js/blob/master/How%20to%20Read%20a%20Type%20Annotation.md#how-to-read-a-type-annotation). Mais sobre guias da comunidade sobre o básico de Elm escrito por desenvolvedores JavaScript.

Saia e escreva algum código Elm!
---
language: erlang
filename: learnerlang-pt.erl
contributors:
    - ["Giovanni Cappellotto", "http://www.focustheweb.com/"]
translators:
    - ["Guilherme Heuser Prestes", "http://twitter.com/gprestes"]
lang: pt-br
---

```erlang
% Símbolo de porcento começa comentários de uma linha.

%% Dois caracteres de porcento devem ser usados para comentar funções.

%%% Três caracteres de porcento devem ser usados para comentar módulos.

% Nós usamos três tipos de pontuação em Erlang.
% Vírgulas (`,`) separam argumentos em chamadas de função, construtores de
% dados, e padrões.
% Pontos finais (`.`) separam totalmente funções e expressões no prompt.
% Ponto e vírgulas (`;`) separam cláusulas. Nós encontramos cláusulas em
% vários contextos: definições de função e em expressões com `case`, `if`,
% `try..catch` e `receive`.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. Variáveis e casamento de padrões.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Num = 42.  % Todos nomes de variáveis devem começar com uma letra maiúscula.

% Erlang tem atribuição única de variáveis, se você tentar atribuir um valor
% diferente à variável `Num`, você receberá um erro.
Num = 43. % ** exception error: no match of right hand side value 43

% Na maioria das linguagens, `=` denota um comando de atribuição. Em Erlang, no
% entanto, `=` denota uma operação de casamento de padrão. `Lhs = Rhs` realmente
% significa isso: avalia o lado direito (Rhs), e então casa o resultado com o
% padrão no lado esquerdo (Lhs).
Num = 7 * 6.

% Número de ponto flutuante.
Pi = 3.14159.

% Átomos são usados para representar diferentes valores constantes não
% numéricos. Átomos começam com letras minúsculas seguidas por uma sequência de
% caracteres alfanuméricos ou sinais de subtraço (`_`) ou arroba (`@`).
Hello = hello.
OtherNode = example@node.

% Átomos com valores alfanuméricos podem ser escritos colocando aspas por fora
% dos átomos.
AtomWithSpace = 'some atom with space'.

% Tuplas são similares a structs em C.
Point = {point, 10, 45}.

% Se nós queremos extrair alguns valores de uma tupla, nós usamos o operador `=`.
{point, X, Y} = Point.  % X = 10, Y = 45

% Nós podemos usar `_` para ocupar o lugar de uma variável que não estamos interessados.
% O símbolo `_` é chamado de variável anônima. Ao contrário de variáveis regulares,
% diversas ocorrências de _ no mesmo padrão não precisam se amarrar ao mesmo valor.
Person = {person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}.
{_, {_, {_, Who}, _}, _} = Person.  % Who = joe

% Nós criamos uma lista colocando valores separados por vírgula entre colchetes.
% Cada elemento de uma lista pode ser de qualquer tipo.
% O primeiro elemento de uma lista é a cabeça da lista. Se removermos a cabeça
% da lista, o que sobra é chamado de cauda da lista.
ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].

% Se `T` é uma lista, então `[H|T]` também é uma lista, com cabeça `H` e cauda `T`.
% A barra vertical (`|`) separa a cabeça de uma lista de sua cauda.
% `[]` é uma lista vazia.
% Podemos extrair elementos de uma lista com uma operação de casamento de
% padrão. Se temos uma lista não-vazia `L`, então a expressão `[X|Y] = L`, onde
% `X` e `Y` são variáveis desamarradas, irá extrair a cabeça de uma lista para
% `X` e a cauda da lista para `Y`.
[FirstThing|OtherThingsToBuy] = ThingsToBuy.
% FirstThing = {apples, 10}
% OtherThingsToBuy = {pears, 6}, {milk, 3}

% Não existe o tipo string em Erlang. Strings são somente listas de inteiros.
% Strings são representadas dentro de aspas duplas (`"`).
Name = "Hello".
[72, 101, 108, 108, 111] = "Hello".


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. Programação sequencial.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Módulos são a unidade básica de código em Erlang. Todas funções que
% escrevemos são armazenadas em módulos. Módulos são armazenados em arquivos
% com extensão `.erl`.
% Módulos devem ser compilados antes que o código possa ser rodado. Um módulo
% compilado tem a extensão `.beam`.
-module(geometry).
-export([area/1]). % lista de funções exportadas de um módulo.

% A função `area` consiste de duas cláusulas. As cláusulas são separadas por um
% ponto e vírgula, e a cláusula final é terminada por um ponto final.
% Cada cláusula tem uma cabeça em um corpo; a cabeça consiste de um nome de
% função seguido por um padrão (entre parêntesis), e o corpo consiste de uma
% sequência de expressões, que são avaliadas se o padrão na cabeça é um par bem
% sucedido dos argumentos da chamada. Os padrões são casados na ordem que
% aparecem na definição da função.
area({rectangle, Width, Ht}) -> Width * Ht;
area({circle, R})            -> 3.14159 * R * R.

% Compila o código no arquivo geometry.erl.
c(geometry).  % {ok,geometry}

% Nós precisamos incluir o nome do módulo junto com o nome da função de maneira
% a identificar exatamente qual função queremos chamar.
geometry:area({rectangle, 10, 5}).  % 50
geometry:area({circle, 1.4}).  % 6.15752

% Em Erlang, duas funções com o mesmo nome e diferentes aridades (números de
% argumentos) no mesmo módulo representam funções totalmente diferentes.
-module(lib_misc).
-export([sum/1]). % exporta a função `sum` de aridade 1 aceitando um argumento: lista de inteiros.
sum(L) -> sum(L, 0).
sum([], N)    -> N;
sum([H|T], N) -> sum(T, H+N).

% Funs são funções "anônimas". Elas são chamadas desta maneira por que elas não
% têm nome. No entanto podem ser atribuídas a variáveis.
Double = fun(X) -> 2*X end. % `Double` aponta para uma função anônima com referência: #Fun<erl_eval.6.17052888>
Double(2).  % 4

% Funções aceitam funs como seus argumentos e podem retornar funs.
Mult = fun(Times) -> ( fun(X) -> X * Times end ) end.
Triple = Mult(3).
Triple(5).  % 15

% Compreensão de lista são expressões que criam listas sem precisar usar funs,
% maps, ou filtros.
% A notação `[F(X) || X <- L]` significa "a lista de `F(X)` onde `X` é tomada
% da lista `L`."
L = [1,2,3,4,5].
[2*X || X <- L].  % [2,4,6,8,10]
% Uma compreensão de lista pode ter geradores e filtros que selecionam
% subconjuntos dos valores gerados.
EvenNumbers = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4]

% Sentinelas são contruções que podemos usar para incrementar o poder de
% casamento de padrão. Usando sentinelas, podemos executar testes simples e
% comparações nas variáveis em um padrão.
% Você pode usar sentinelas nas cabeças das definições de função onde eles são
% introduzidos pela palavra-chave `when`, ou você pode usá-los em qualquer
% lugar na linguagem onde uma expressão é permitida.
max(X, Y) when X > Y -> X;
max(X, Y) -> Y.

% Um sentinela é uma série de expressões sentinelas, separadas por
% vírgulas (`,`).
% O sentinela `GuardExpr1, GuardExpr2, ..., GuardExprN` é verdadeiro se todas
% expressões sentinelas `GuardExpr1, GuardExpr2, ...` forem verdadeiras.
is_cat(A) when is_atom(A), A =:= cat -> true;
is_cat(A) -> false.
is_dog(A) when is_atom(A), A =:= dog -> true;
is_dog(A) -> false.

% Uma `sequência sentinela` é um sentinela ou uma série de sentinelas separados
% por ponto e vírgula (`;`). A sequência sentinela `G1; G2; ...; Gn` é
% verdadeira se pelo menos um dos sentinelas `G1, G2, ...` for verdadeiro.
is_pet(A) when is_dog(A); is_cat(A) -> true;
is_pet(A) -> false.

% Registros provêem um método para associar um nome com um elemento particular
% em uma tupla.
% Definições de registro podem ser incluídas em arquivos fonte Erlang ou em
% arquivos com extensão `.hrl`, que então são incluídos em arquivos fonte Erlang.
-record(todo, {
  status = reminder,  % Default value
  who = joe,
  text
}).

% Nós temos que ler definições de registro no prompt antes que possamos definir
% um registro. Nós usamos a função de prompt `rr` (abreviação de read records)
% para fazer isso.
rr("records.hrl").  % [todo]

% Criando e atualizando registros:
X = #todo{}.
% #todo{status = reminder, who = joe, text = undefined}
X1 = #todo{status = urgent, text = "Fix errata in book"}.
% #todo{status = urgent, who = joe, text = "Fix errata in book"}
X2 = X1#todo{status = done}.
% #todo{status = done,who = joe,text = "Fix errata in book"}

% Expressões `case`.
% A função `filter` retorna uma lista de todos elementos `X` em uma lista `L`
% para qual `P(X)` é verdadeiro.
filter(P, [H|T]) ->
  case P(H) of
    true -> [H|filter(P, T)];
    false -> filter(P, T)
  end;
filter(P, []) -> [].
filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]). % [2, 4]

% Expressões `if`.
max(X, Y) ->
  if
    X > Y -> X;
    X < Y -> Y;
    true -> nil;
  end.

% Aviso: pelo menos um dos sentinelas na expressão `if` deve retornar
% verdadeiro; Caso contrário, uma exceção será levantada.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3. Exceções.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Exceções são levantadas pelo sistema quando erros internos são encontrados ou
% explicitamente em código pela chamada `throw(Exception)`, `exit(Exception)`
% ou `erlang:error(Exception)`.
generate_exception(1) -> a;
generate_exception(2) -> throw(a);
generate_exception(3) -> exit(a);
generate_exception(4) -> {'EXIT', a};
generate_exception(5) -> erlang:error(a).

% Erlang tem dois métodos para capturar uma exceção. Uma é encapsular a chamada
% para a função que levanta uma exceção dentro de uma expressão `try...catch`.
catcher(N) ->
  try generate_exception(N) of
    Val -> {N, normal, Val}
  catch
    throw:X -> {N, caught, thrown, X};
    exit:X -> {N, caught, exited, X};
    error:X -> {N, caught, error, X}
  end.

% O outro é encapsular a chamada em uma expressão `catch`. Quando você captura
% uma exceção, é convertida em uma tupla que descreve o erro.
catcher(N) -> catch generate_exception(N).

```

## Referências

* ["Learn You Some Erlang for great good!"](http://learnyousomeerlang.com/)
* ["Programming Erlang: Software for a Concurrent World" by Joe Armstrong](http://pragprog.com/book/jaerlang2/programming-erlang)
* [Erlang/OTP Reference Documentation](http://www.erlang.org/doc/)
* [Erlang - Programming Rules and Conventions](http://www.erlang.se/doc/programming_rules.shtml)

---
category: tool
tool: git
lang: pt-br
filename: LearnGit-br.txt
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Leo Rudberg" , "http://github.com/LOZORD"]
    - ["Betsy Lorton" , "http://github.com/schbetsy"]
    - ["Bruno Volcov", "http://github.com/volcov"]
translators:
  - ["Suzane Sant Ana", "http://github.com/suuuzi"]
  - ["Bruno Volcov", "http://github.com/volcov"]
---

Git é um sistema distribuido de gestão para código fonte e controle de versões.

Funciona através de uma série de registos de estado do projeto e usa esse
registo para permitir funcionalidades de versionamento e gestão de código
fonte.

## Conceitos de versionamento

### O que é controle de versão

Controle de versão (*source control*) é um processo de registo de alterações
a um arquivo ou conjunto de arquivos ao longo do tempo.

### Controle de versão:  Centralizado VS Distribuído

* Controle de versão centralizado foca na sincronização, registo e *backup*
de arquivos.
* Controle de versão distribuído foca em compartilhar alterações. Cada
alteração é associada a um *id* único.
* Sistemas distribuídos não têm estrutura definida. É possivel ter um sistema
centralizado ao estilo SVN usando git.

[Informação adicional (EN)](http://git-scm.com/book/en/Getting-Started-About-Version-Control)

### Por que usar git?

* Permite trabalhar offline.
* Colaborar com outros é fácil!
* Criar *branches* é fácil!
* Fazer *merge* é fácil!
* Git é rápido.
* Git é flexivel.

## Git - Arquitetura


### Repositório

Um conjunto de arquivos, diretórios, registos históricos, *commits* e
referências. Pode ser descrito como uma estrutura de dados de código fonte
com a particularidade de cada elemento do código fonte permitir acesso ao
histórico das suas alterações, entre outras coisas.

Um repositório git é constituído pelo diretório .git e a *working tree*

### Diretório .git (componente do repositório)

O repositório .git contém todas as configurações, *logs*, *branches*,
referências e outros.

[Lista detalhada (EN)](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### *Working Tree* (componente do repositório)

A *Working Tree* é basicamente a listagem dos diretórios e arquivos do repositório. É chamada também de diretório do projeto.

### *Index* (componente do diretório .git)

O *Index* é a camada da interface no git. É o elemento que separa
o diretório do projeto do repositório git. Isto permite aos programadores um
maior controle sobre o que é registado no repositório git.

### *Commit*

Um *commit** de git é um registo de um cojunto de alterações ou manipulações nos arquivos do projeto.
Por exemplo, ao adicionar cinco arquivos e remover outros 2, estas alterações
serão gravadas num *commit* (ou registo). Este *commit* pode então ser enviado
para outros repositórios ou não!

### *Branch*

Um *branch* é essencialmente uma referência que aponta para o último *commit*
efetuado. Na medida que são feitos novos commits, esta referência é atualizada
automaticamente e passa a apontar para o commit mais recente.

### *Tag*

Uma tag é uma marcação em um ponto específico da história. Geralmente as
pessoas usam esta funcionalidade para marcar pontos de release (v2.0, e por aí vai)

### *HEAD* e *head* (componentes do diretório .git)

*HEAD* é a referência que aponta para o *branch* em uso. Um repositório só tem
uma *HEAD* activa.
*head* é uma referência que aponta para qualquer *commit*. Um repositório pode
ter um número indefinido de *heads*

### Recursos conceituais (EN)

* [Git para Cientistas de Computação](http://eagain.net/articles/git-for-computer-scientists/)
* [Git para Designers](http://hoth.entp.com/output/git_for_designers.html)

## Comandos

### *init*

Cria um repositório Git vazio. As definições, informação guardada e outros do
repositório git são guardados em uma pasta chamada ".git".

```bash
$ git init
```

### *config*

Permite configurar as definições, sejam as definições do repositório, sistema
ou configurações globais.

```bash
# Imprime e define algumas variáveis de configuração básicas (global)
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "MyEmail@Zoho.com"
$ git config --global user.name "My Name"
```

[Aprenda mais sobre git config. (EN)](http://git-scm.com/docs/git-config)

### help

Para visualizar rapidamente o detalhamento de cada comando ou apenas lembrar da semântica.

```bash
# Ver rapidamente os comandos disponiveis
$ git help

# Ver todos os comandos disponíveis
$ git help -a

# Usar o *help* para um comando específico
# git help <comando_aqui>
$ git help add
$ git help commit
$ git help init
```

### status

Apresenta as diferenças entre o arquivo *index* (a versão corrente
do repositório) e o *commit* da *HEAD* atual.


```bash
# Apresenta o *branch*, arquivos não monitorados, alterações e outras
# diferenças
$ git status

# Para aprender mais detalhes sobre git *status*
$ git help status
```

### add

Adiciona arquivos ao repositório corrente. Se os arquivos novos não forem
adicionados através de `git add` ao repositório, então eles não serão
incluidos nos commits!

```bash
# adiciona um arquivo no diretório do projeto atual
$ git add HelloWorld.java

# adiciona um arquivo num sub-diretório
$ git add /path/to/file/HelloWorld.c

# permite usar expressões regulares!
$ git add ./*.java
```

### branch

Gerencia os *branches*. É possível ver, editar, criar e apagar branches com este
comando.

```bash
# listar *branches* existentes e remotos
$ git branch -a

# criar um novo *branch*
$ git branch myNewBranch

# apagar um *branch*
$ git branch -d myBranch

# alterar o nome de um *branch*
# git branch -m <oldname> <newname>
$ git branch -m myBranchName myNewBranchName

# editar a descrição de um *branch*
$ git branch myBranchName --edit-description
```

### Tag

Gerencia as *tags*

```bash
# Listar tags
$ git tag
# Criar uma tag anotada.
# O parâmetro -m define uma mensagem, que é armazenada com a tag.
# Se você não especificar uma mensagem para uma tag anotada,
# o Git vai rodar seu editor de texto para você digitar alguma coisa.
$ git tag -a v2.0 -m 'minha versão 2.0'
# Mostrar informações sobre a tag
# O comando mostra a informação da pessoa que criou a tag,
# a data de quando o commit foi taggeado,
# e a mensagem antes de mostrar a informação do commit.
$ git show v2.0
# Enviar uma tag para o repositório remoto
$ git push origin v2.0
# Enviar várias tags para o repositório remoto
$ git push origin --tags
```

### checkout

Atualiza todos os arquivos no diretório do projeto para que fiquem iguais
à versão do index ou do *branch* especificado.

```bash
# Checkout de um repositório - por padrão para o branch master
$ git checkout
# Checkout de um branch especifico
$ git checkout branchName
# Cria um novo branch e faz checkout para ele.
# Equivalente a: "git branch <name>; git checkout <name>"
$ git checkout -b newBranch
```

### clone

Clona ou copia um repositório existente para um novo diretório. Também
adiciona *branches* de monitoramento remoto para cada *branch* no repositório
clonado o que permite enviar alterações para um *branch* remoto.

```bash
# Clona learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git
```

### commit

Guarda o conteudo atual do index num novo *commit*. Este *commit* contém
as alterações feitas e a mensagem criada pelo usuário.

```bash
# commit com uma mensagem
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"
```

### diff

Apresenta as diferenças entre um arquivo no repositório do projeto, *index*
e *commits*

```bash
# Apresenta a diferença entre o diretório atual e o index
$ git diff

# Apresenta a diferença entre o index e os commits mais recentes
$ git diff --cached

# Apresenta a diferença entre o diretório atual e o commit mais recente
$ git diff HEAD
```

### grep

Permite procurar facilmente num repositório

Configurações opcionais:

```bash
# Define a apresentação de números de linha nos resultados do grep
$ git config --global grep.lineNumber true

# Agrupa os resultados da pesquisa para facilitar a leitura
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Pesquisa por "variableName" em todos os arquivos java
$ git grep 'variableName' -- '*.java'

# Pesquisa por uma linha que contém "arrayListName" e "add" ou "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
```

O Google é seu amigo; para mais exemplos:
[Git Grep Ninja (EN)](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Apresenta commits do repositório.

```bash
# Apresenta todos os commits
$ git log

# Apresenta X commits
$ git log -n 10

# Apresenta apenas commits de merge
$ git log --merges
```

### merge

"Merge" junta as alterações de commits externos com o *branch* atual.

```bash
# Junta o branch especificado com o atual
$ git merge branchName

# Para gerar sempre um commit ao juntar os branches
$ git merge --no-ff branchName
```

### mv

Alterar o nome ou mover um arquivo.

```bash
# Alterar o nome de um arquivo
$ git mv HelloWorld.c HelloNewWorld.c

# Mover um arquivo
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# Forçar a alteração de nome ou mudança local
# "existingFile" já existe no directório, será sobrescrito.
$ git mv -f myFile existingFile
```

### pull

Puxa alterações de um repositório e as junta com outro branch

```bash
# Atualiza o repositório local, juntando as novas alterações
# do repositório remoto 'origin' e branch 'master'
# git pull <remote> <branch>
# git pull => aplica a predefinição => git pull origin master
$ git pull origin master

# Juntar alterações do branch remote e fazer rebase commits do branch
# no repositório local, como: "git pull <remote> <branch>, git rebase <branch>"
$ git pull origin master --rebase
```

### push

Enviar e juntar alterações de um branch para o seu branch correspondente
num repositório remoto.

```bash
# Envia e junta as alterações de um repositório local
# para um remoto denominado "origin" no branch "master".
# git push <remote> <branch>
# git push => aplica a predefinição => git push origin master
$ git push origin master
```

### rebase (cautela!)

Pega em todas as alterações que foram registadas num branch e volta a
aplicá-las em outro branch.
*Não deve ser feito rebase de commits que foram enviados para um repositório
público*

```bash
# Faz Rebase de experimentBranch para master
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch
```

[Leitura adicional (EN).](http://git-scm.com/book/en/Git-Branching-Rebasing)

### reset (cuidado!)

Restabelece a HEAD atual ao estado definido. Isto permite reverter *merges*,
*pulls*, *commits*, *adds* e outros. É um comando muito poderoso mas também
perigoso quando não há certeza do que se está fazendo.

```bash
# Restabelece a camada intermediária de registo para o último
# commit (o diretório fica sem alterações)
$ git reset

# Restabelece a camada intermediária de registo para o último commit, e
# sobrescreve o projeto atual
$ git reset --hard

# Move a head do branch atual para o commit especificado, sem alterar o projeto.
# todas as alterações ainda existem no projeto
$ git reset 31f2bb1

# Inverte a head do branch atual para o commit especificado
# fazendo com que este esteja em sintonia com o diretório do projeto
# Remove alterações não registadas e todos os commits após o commit especificado
$ git reset --hard 31f2bb1
```

### rm

O oposto de git add, git rm remove arquivos do branch atual.

```bash
# remove HelloWorld.c
$ git rm HelloWorld.c

# Remove um arquivo de um sub-directório
$ git rm /pather/to/the/file/HelloWorld.c
```

## Informação complementar (EN)

* [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1)

* [git-scm - Video Tutorials](http://git-scm.com/videos)

* [git-scm - Documentation](http://git-scm.com/docs)

* [Atlassian Git - Tutorials & Workflows](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)
---
name: Go
category: language
language: Go
filename: learngo-pt.go
lang: pt-br
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Jose Donizetti", "https://github.com/josedonizetti"]
translators:
    - ["Nuno Antunes", "https://github.com/ntns"]
---

A linguagem Go foi criada a partir da necessidade de ver trabalho feito. Não 
é a última moda em ciências da computação, mas é a mais recente e mais rápida
forma de resolver os problemas do mundo real.

Tem conceitos familiares de linguagens imperativas com tipagem estática. É 
rápida a compilar e rápida a executar, acrescentando mecanismos de concorrência
fáceis de entender para tirar partido dos CPUs multi-core de hoje em dia, e tem
recursos para ajudar com a programação em larga escala.

Go vem com uma biblioteca padrão exaustiva e uma comunidade entusiasta.

```go
// Comentário de uma linha
/* Comentário de
   várias linhas */

// A cláusula package aparece no início de cada arquivo.
// Main é um nome especial declarando um executável ao invés de uma biblioteca.
package main

// A cláusula Import declara os pacotes referenciados neste arquivo.
import (
    "fmt"      // Um pacote da biblioteca padrão da linguagem Go
    "net/http" // Sim, um servidor web!
    "strconv"  // Conversão de Strings
)

// Definição de uma função. Main é especial. É o ponto de entrada para o
// programa executável. Goste-se ou não, a linguagem Go usa chavetas.
func main() {
    // A função Println envia uma linha para stdout.
    // É necessário qualifica-la com o nome do pacote, fmt.
    fmt.Println("Olá Mundo!")

    // Chama outra função dentro deste pacote.
    beyondHello()
}

// As funções declaram os seus parâmetros dentro de parênteses. Se a função
// não receber quaisquer parâmetros, é obrigatório usar parênteses vazios.
func beyondHello() {
    var x int // Declaração de variável. Tem de ser declarada antes de usar.
    x = 3     // Atribuição de variável.
    // Declarações "curtas" usam := para inferir o tipo, declarar e atribuir.
    y := 4
    sum, prod := learnMultiple(x, y)        // a função retorna dois valores
    fmt.Println("soma:", sum, "produto:", prod) 
    learnTypes()                            // continuar a aprender!
}

// As funções podem receber parâmetros e retornar (vários!) valores.
func learnMultiple(x, y int) (sum, prod int) {
    return x + y, x * y // retorna dois valores
}

// Alguns tipos e literais básicos.
func learnTypes() {
    // Declarações "curtas" geralmente servem para o que pretendemos.
    s := "Aprender Go!" // tipo string 

    s2 := `Uma string em "bruto" 
pode incluir quebras de linha.` // mesmo tipo string

    // literal não-ASCII. A linguagem Go utiliza de raiz a codificação UTF-8.
    g := 'Σ' // tipo rune, um alias para int32, que contém um código unicode

    f := 3.14195 // float64, número de vírgula flutuante de 64bit (IEEE-754)
    c := 3 + 4i  // complex128, representado internamente com dois float64s

    // Declaração de variáveis, com inicialização.
    var u uint = 7 // inteiro sem sinal, tamanho depende da implementação do Go
    var pi float32 = 22. / 7

    // Sintaxe de conversão de tipo, com declaração "curta".
    n := byte('\n') // byte é um alias para uint8

    // Os arrays têm tamanho fixo e definido antes da compilação. 
    var a4 [4]int           // um array de 4 ints, inicializado com ZEROS
    a3 := [...]int{3, 1, 5} // um array de 3 ints, inicializado como mostrado

    // As slices têm tamanho dinâmico. Os arrays e as slices têm cada um as
    // suas vantagens mas o uso de slices é muito mais comum.
    s3 := []int{4, 5, 9}   // compare com a3. sem reticências aqui
    s4 := make([]int, 4)   // aloca uma slice de 4 ints, inicializada com ZEROS
    var d2 [][]float64     // declaração apenas, nada é alocado
    bs := []byte("uma slice") // sintaxe de conversão de tipos

    p, q := learnMemory() // learnMemory retorna dois apontadores para int.
    fmt.Println(*p, *q)   // * segue um apontador. isto imprime dois ints.

    // Os maps são um tipo de matriz associativa, semelhante aos tipos hash
    // ou dictionary que encontramos noutras linguagens.
    m := map[string]int{"três": 3, "quatro": 4}
    m["um"] = 1

    // As variáveis não usadas são um erro em Go.
    // O traço inferior permite "usar" uma variável, mas descarta o seu valor.
    _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
    // Enviar para o stdout conta como utilização de uma variável.
    fmt.Println(s, c, a4, s3, d2, m)

    learnFlowControl() 
}

// A linguagem Go é totalmente garbage collected. Tem apontadores mas não 
// permite que os apontadores sejam manipulados com aritmética. Pode-se cometer
// um erro com um apontador nulo, mas não por incrementar um apontador.
func learnMemory() (p, q *int) {
    // A função retorna os valores p e q, que são do tipo apontador para int.
    p = new(int) // a função new aloca memória, neste caso para um int.
    // O int alocado é inicializado com o valor 0, p deixa de ser nil.
    s := make([]int, 20) // alocar 20 ints como um único bloco de memória
    s[3] = 7             // atribui o valor 7 a um deles
    r := -2              // declarar outra variável local
    return &s[3], &r     // & obtém o endereço de uma variável.
}

func expensiveComputation() int {
    return 1e6
}

func learnFlowControl() {
    // As instruções if exigem o uso de chavetas, e não requerem parênteses.
    if true {
        fmt.Println("eu avisei-te")
    }
    // A formatação do código-fonte é "estandardizada" através do comando
    // da linha de comandos "go fmt."
    if false {
        // reclamar
    } else {
        // exultar
    }
    // Preferir o uso de switch em vez de ifs em cadeia.
    x := 1
    switch x {
    case 0:
    case 1:
        // os cases não fazem "fall through"
    case 2:
        // esta linha só é executada se e só se x=2
    }
    // Tal como a instrução if, a instrução for não usa parênteses.
    for x := 0; x < 3; x++ { // x++ é uma instrução, nunca uma expressão
        fmt.Println("iteração", x)
    }
    // note que, x == 1 aqui.

    // A instrução for é a única para ciclos, mas assume várias formas.
    for { // ciclo infinito
        break    // brincadeirinha
        continue // nunca executado
    }
    // O uso de := numa instrução if permite criar uma variável local,
    // que existirá apenas dentro do bloco if.
    if y := expensiveComputation(); y > x {
        x = y
    }
    // As funções podem ser closures.
    xBig := func() bool {
        return x > 100 // referencia x, declarado acima da instrução switch.
    }
    fmt.Println("xBig:", xBig()) // true (1e6 é o último valor de x)
    x /= 1e5                     // agora temos x == 10 
    fmt.Println("xBig:", xBig()) // false

    // Quando for mesmo necessário, pode usar o velho goto.
    goto love
love:

    learnInterfaces() // Mais coisas interessantes chegando!
}

// Define Stringer como uma interface consistindo de um método, String.
type Stringer interface {
    String() string
}

// Define pair como uma struct com dois campos ints chamados x e y.
type pair struct {
    x, y int
}

// Define um método para o tipo pair. O tipo pair implementa agora a 
// interface Stringer.
func (p pair) String() string { // p é chamado de "receptor"
    // Sprintf é outra função pública no pacote fmt.
    // Uso de pontos para referenciar os campos de p.
    return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
    // Uma struct pode ser inicializada com os valores dos seus campos dentro
    // de chavetas, seguindo a mesma ordem com que os campos foram definidos.
    p := pair{3, 4}
    fmt.Println(p.String()) // chama o método String de p, que tem tipo pair.
    var i Stringer          // declara i do tipo interface Stringer.
    i = p                   // válido, porque pair implementa Stringer
    // Chama o método String de i, que tem tipo Stringer. Mesmo que acima.
    fmt.Println(i.String())

    // As funções no pacote fmt chamam o método String para pedir a um objecto
    // uma representação textual de si mesmo.
    fmt.Println(p) // mesmo que acima. Println chama o método String.
    fmt.Println(i) // mesmo que acima.

    learnErrorHandling()
}

func learnErrorHandling() {
    // ", ok" forma idiomática usada para saber se algo funcionou ou não.
    m := map[int]string{3: "três", 4: "quatro"}
    if x, ok := m[1]; !ok { // ok vai ser false porque 1 não está no map m.
        fmt.Println("ninguem lá")
    } else {
        fmt.Print(x) // x seria o valor, se 1 estivesse no map.
    }
    // Um valor de erro comunica mais informação sobre o problema.
    if _, err := strconv.Atoi("non-int"); err != nil { // _ descarta o valor
        // imprime "strconv.ParseInt: parsing "non-int": invalid syntax"
        fmt.Println(err)
    }
    // Vamos revisitar as interfaces um pouco mais tarde. Entretanto,
    learnConcurrency()
}

// c é um channel, um objecto para comunicação concurrency-safe.
func inc(i int, c chan int) {
    c <- i + 1 // <- é operador "enviar" quando um channel aparece à esquerda.
}

// Vamos usar a função inc para incrementar números de forma concorrente.
func learnConcurrency() {
    // A mesma função make usada anteriormente para alocar uma slice.
    // Make aloca e inicializa slices, maps, e channels.
    c := make(chan int)
    // Inicia três goroutines concorrentes. Os números serão incrementados de
    // forma concorrente, talvez em paralelo se a máquina for capaz e estiver
    // configurada correctamente. As três goroutines enviam para o mesmo canal.
    go inc(0, c) // go é a instrução para iniciar uma goroutine.
    go inc(10, c)
    go inc(-805, c)
    // Lê três resultados do channel c e imprime os seus valores.
    // Não se pode dizer em que ordem os resultados vão chegar!
    fmt.Println(<-c, <-c, <-c) // channel na direita, <- é operador "receptor".

    cs := make(chan string)       // outro channel, este lida com strings.
    cc := make(chan chan string)  // channel que lida com channels de strings.
    go func() { c <- 84 }()       // inicia uma goroutine para enviar um valor
    go func() { cs <- "palavroso" }() // outra vez, para o channel cs desta vez
    // A instrução select tem uma sintaxe semelhante à instrução switch mas
    // cada caso envolve uma operação com channels. Esta instrução seleciona, 
    // de forma aleatória, um caso que esteja pronto para comunicar.
    select {
    case i := <-c: // o valor recebido pode ser atribuído a uma variável
        fmt.Printf("é um %T", i)
    case <-cs: // ou o valor recebido pode ser descartado
        fmt.Println("é uma string")
    case <-cc: // channel vazio, não se encontra pronto para comunicar.
        fmt.Println("não aconteceu")
    }
    // Neste ponto um valor foi recebido de um dos channels c ou cs. Uma das
    // duas goroutines iniciadas acima completou, a outra continua bloqueada.

    learnWebProgramming() // Go faz. Você quer faze-lo também.
}

// Basta apenas uma função do pacote http para iniciar um servidor web.
func learnWebProgramming() {
    // O primeiro parâmetro de ListenAndServe é o endereço TCP onde escutar.
    // O segundo parâmetro é uma interface, especificamente http.Handler.
    err := http.ListenAndServe(":8080", pair{})
    fmt.Println(err) // não ignorar erros
}

// Tornar pair um http.Handler ao implementar o seu único método, ServeHTTP.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // Servir dados com um método de http.ResponseWriter
    w.Write([]byte("Aprendeu Go em Y minutos!"))
}
```

## Leitura Recomendada

A principal fonte de informação é o [web site oficial Go](http://golang.org/).
Lá é possível seguir o tutorial, experimentar de forma iterativa, e ler muito.

A própria especificação da linguagem é altamente recomendada. É fácil de ler e
incrivelmente curta (em relação ao que é habitual hoje em dia).

Na lista de leitura para os aprendizes de Go deve constar o [código fonte da 
biblioteca padrão](http://golang.org/src/pkg/). Exaustivamente documentado, é
a melhor demonstração de código fácil de ler e de perceber, do estilo Go, e da
sua escrita idiomática. Ou então clique no nome de uma função na [documentação]
(http://golang.org/pkg/) e veja o código fonte aparecer!

Outra ótima fonte para aprender Go é o [Go by example](https://gobyexample.com/).
Apesar de ser em inglês, é possível recodificar os exemplos para aprender sobre
a linguagem.
---
language: Groovy
category: language
filename: learngroovy-pt.groovy
contributors:
    - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"]
translators:
    - ["João Farias", "https://github.com/JoaoGFarias"]
lang: pt-br
---

Groovy - Uma linguagem dinâmica para a plataforma Java. [Leia mais aqui.](http://www.groovy-lang.org/)

```groovy

/*
  Prepara-se:

  1) Instale a máquina virtual de Groovy - http://gvmtool.net/
  2) Intalse o Groovy: gvm install groovy
  3) Inicie o console groovy digitando: groovyConsole

*/

// Comentário de uma linha inicia-se com duas barras
/*
Comentário de múltiplas linhas são assim.
*/

// Olá Mundo!
println "Olá mundo!"

/*
  Variáveis:

  Você pode atribuir valores a variáveis para uso posterior
*/

def x = 1
println x

x = new java.util.Date()
println x

x = -3.1499392
println x

x = false
println x

x = "Groovy!"
println x

/*
  Coleções e mapeamentos
*/

//Criando uma lista vazia
def tecnologias = []

/*** Adicionando elementos à lista ***/

// Assim como Java
tecnologias.add("Grails")

// Shift para esquerda adiciona e retorna a lista
tecnologias << "Groovy"

// Adição de múltiplos elementos
tecnologias.addAll(["Gradle","Griffon"])

/*** Removendo elementos da lista ***/

// Assim como Java
tecnologias.remove("Griffon")

// Subtração também funciona
tecnologias = technologies - 'Grails'

/*** Iterando sobre listas ***/

// Itera sobre os elementos da lista
tecnologias.each { println "Tecnologias: $it"}
tecnologias.eachWithIndex { it, i -> println "$i: $it"}

/*** Checando os elementos da lista ***/

//Avalia se a lista contém o elemento 'Groovy'
contem = tecnologias.contains( 'Groovy' )

// Ou
contem = 'Groovy' in tecnologias

// Checagem por múltiplos elementos
tecnologias.containsAll(['Groovy','Grails'])

/*** Ordenando listas ***/

// Ordena a lista (altera a lista in-place)
tecnologias.sort()

// Para ordenar a lista sem alterar a original
tecnologiasOrdenadas = tecnologias.sort( false )

/*** Manipulando listas ***/

//Substitue todos os elementos da lista
Collections.replaceAll(tecnologias, 'Gradle', 'gradle')

//Desorganiza a lista
Collections.shuffle(tecnologias, new Random())

//Limpa a lista
technologies.clear()

//Criando um mapeamento vazio
def devMap = [:]

//Adicionando valores
devMap = ['nome':'Roberto', 'framework':'Grails', 'linguagem':'Groovy']
devMap.put('ultimoNome','Perez')

//Iterando sobre os elementos do mapeamento
devMap.each { println "$it.key: $it.value" }
devMap.eachWithIndex { it, i -> println "$i: $it"}

//Avalia se um mapeamento contém uma chave
assert devMap.containsKey('nome')

//Avalia se um mapeamento contém um valor
assert devMap.containsValue('Roberto')

//Pega as chaves de um mapeamento
println devMap.keySet()

//Pega os valores de um mapeamento
println devMap.values()

/*
  Groovy Beans

  GroovyBeans são JavaBeans com uma sintaxe muito mais simples.

  Quando Groovy é compilado para bytecode, as seguintes regras são usadas:

    * Se o nome é declarado com um modificador de acesso(public, private or
      protected) então um atributo é gerado.

    * Um nome declarado sem modificador de acesso gera um campo privado com
      getter e setter públicos (ou seja, uma propriedade).

    * Se uma propriedade é declarada como final, um campo private final é criado
      e o setter não é gerado.

    * Você pode declarar uma propriedade e também declarar seus próprios getters
      e setters.

    * Você pode declarar uma propriedade e um campo com o mesmo nome, a propriedade
      usará este campo.

    * Se você quer uma propriedade private ou protected, você deve prover seus
      próprios getters e setter, que devem ser declarados como private ou protected.

    * Se você acessar uma propriedade dentro da classe e esta propriedade é definida
      em tempo de compilação com 'this', implícito ou explícito (por exemplo,
      this.foo, ou simplesmente foo), Groovy acessará este campo diretamente, sem
      passar pelo getter ou setter.

    * Se você acessar uma propriedade que não existe usando foo, explicitamente ou
      implicitamente, então Groovy irá acessar esta propriedade através da meta
      classe, o que pode falhar em tempo de execução.

*/

class Foo {
    // propriedade de leitura, apenas
    final String nome = "Roberto"

    // propriedade de leitura, apenas, com getter e setter públicos
    String linguagem
    protected void setLinguagem(String linguagem) { this.linguagem = linguagem }

    // propriedade tipada dinamicamente
    def ultimoNome
}

/*
  Condicionais e loops
*/

//Groovy suporta a sintaxe if-else
def x = 3

if(x==1) {
    println "Um"
} else if(x==2) {
    println "Dois"
} else {
    println "X é maior que Dois"
}

//Groovy também suporta o operador ternário
def y = 10
def x = (y > 1) ? "functionou" : "falhou"
assert x == "functionou"

//Loop 'for'
//Itera sobre um intervalo (range)
def x = 0
for (i in 0 .. 30) {
    x += i
}

//Itera sobre uma lista
x = 0
for( i in [5,3,2,1] ) {
    x += i
}

//Itera sobre um array
array = (0..20).toArray()
x = 0
for (i in array) {
    x += i
}

//Itera sobre um mapa
def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
x = 0
for ( e in map ) {
    x += e.value
}

/*
  Operadores

  Sobrecarregamento de Operadores para uma lsita dos operadores comuns que
  Grooby suporta:
  http://www.groovy-lang.org/operators.html#Operator-Overloading

  Operadores Groovy úteis
*/
//Operador de espalhamento: invoca uma ação sobre todos os itens de um
//objeto agregador.
def tecnologias = ['Groovy','Grails','Gradle']
tecnologias*.toUpperCase() // = to tecnologias.collect { it?.toUpperCase() }

//Operador de navegação segura: usado para evitar NullPointerException.
def usuario = User.get(1)
def nomeUsuario = usuario?.nomeUsuario


/*
  Closures
  Um closure, em Grooby, é como um "bloco de código" ou um ponteiro para método.
  É um pedação de código que é definido e executado em um momento posterior.

  Mais informação em: http://www.groovy-lang.org/closures.html
*/
//Exemplo:
def clos = { println "Hello World!" }

println "Executando o closure:"
clos()

//Passando parêmetros para um closure
def soma = { a, b -> println a+b }
soma(2,4)

//Closdures por referir-se a variáveis que não estão listadas em sua
//lista de parêmetros.
def x = 5
def multiplicarPor = { num -> num * x }
println multiplicarPor(10)

// Se você tiver um closure que tem apenas um argumento, você pode omitir
// o parâmetro na definição do closure
def clos = { print it }
clos( "oi" )

/*
  Groovy pode memorizar resultados de closures [1][2][3]
*/
def cl = {a, b ->
    sleep(3000) //  simula processamento
    a + b
}

mem = cl.memoize()

def chamaClosure(a, b) {
    def inicio = System.currentTimeMillis()
    mem(a, b)
    println "Os inputs(a = $a, b = $b) - tomam ${System.currentTimeMillis() - inicio} msecs."
}

chamaClosure(1, 2)
chamaClosure(1, 2)
chamaClosure(2, 3)
chamaClosure(2, 3)
chamaClosure(3, 4)
chamaClosure(3, 4)
chamaClosure(1, 2)
chamaClosure(2, 3)
chamaClosure(3, 4)

/*
  Expando

  A classe Expando é um bean dinâmico que permite adicionar propriedade e 
  closures como métodos a uma instância desta classe

  http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/
  def usuario = new Expando(nome:"Roberto")
  assert 'Roberto' == nome.name

  nome.lastName = 'Pérez'
  assert 'Pérez' == nome.lastName

  nome.showInfo = { out ->
      out << "Name: $name"
      out << ", Last name: $lastName"
  }

  def sw = new StringWriter()
  println nome.showInfo(sw)


/*
  Metaprogramação (MOP)
*/

//Usando a ExpandoMetaClasse para adicionar comportamento
String.metaClass.testAdd = {
    println "adicionamos isto"
}

String x = "teste"
x?.testAdd()

//Interceptando chamadas a métodos
class Test implements GroovyInterceptable {
    def soma(Integer x, Integer y) { x + y }

    def invocaMetodo(String name, args) {
        System.out.println "Invoca método $name com argumentos: $args"
    }
}

def teste = new Test()
teste?.soma(2,3)
teste?.multiplica(2,3)

//Groovy suporta propertyMissing para lidar com tentativas de resolução de
//propriedades.
class Foo {
   def propertyMissing(String nome) { nome }
}
def f = new Foo()

assertEquals "boo", f.boo

/*
  TypeChecked e CompileStatic
  Groovy, por natureza, é e sempre será uma linguagem dinâmica, mas ela também
  suporta typecheked e compilestatic

  Mais informações: http://www.infoq.com/articles/new-groovy-20
*/
//TypeChecked
import groovy.transform.TypeChecked

void testeMethod() {}

@TypeChecked
void test() {
    testeMethod()

    def nome = "Roberto"

    println noomee

}

//Outro exemplo:
import groovy.transform.TypeChecked

@TypeChecked
Integer test() {
    Integer num = "1"

    Integer[] numeros = [1,2,3,4]

    Date dia = numeros[1]

    return "Teste"

}

//Exemplo de CompileStatic :
import groovy.transform.CompileStatic

@CompileStatic
int soma(int x, int y) {
    x + y
}

assert soma(2,5) == 7


```

## Referências

[Groovy documentation](http://www.groovy-lang.org/documentation.html)

[Groovy web console](http://groovyconsole.appspot.com/)

Junte-se a um [grupo de usuários Groovy](http://www.groovy-lang.org/usergroups.html)

## Livro

* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook)

* [Groovy in Action] (http://manning.com/koenig2/)

* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)

[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html



---
language: Hack
contributors:
    - ["Stephen Holdaway", "https://github.com/stecman"]
    - ["David Lima", "https://github.com/davelima"]
translators:
    - ["David Lima", "https://github.com/davelima"]
lang: pt-br
filename: learnhack-pt.hh
---

Hack é uma linguagem baseada no PHP e roda numa máquina virtual chamada HHVM.
Hack é quase completamente interoperável com códigos PHP existentes e adiciona
alguns recursos úteis de linguagens estaticamente tipadas.

Somente recursos específicos da linguagem Hack serão abordados aqui. Detalhes
sobre a sintaxe do PHP estão disponíveis no
[artigo PHP](http://learnxinyminutes.com/docs/php/) neste site.

```php
<?hh

// A sintaxe do Hack é ativada apenas em arquivos que comecem com <?hh
// Marcadores <?hh não podem ser incluídos em páginas HTML, diferente de <?php.
// Usar o marcador "<?hh //strict" coloca o verificador de tipo no modo estrito.


// Indução de tipo de parâmetros escalares
function repeat(string $palavra, int $contagem)
{
    $palavra = trim($palavra);
    return str_repeat($palavra . ' ', $contagem);
}

// Indução de tipo para valores de retorno
function add(...$numeros) : int
{
    return array_sum($numeros);
}

// Funções que não retornam nada são induzidas com "void"
function truncate(resource $recurso) : void
{
    // ...
}

// Induções de tipo devem permitir nulos de forma explícita
function identity(?string $stringOuNulo) : ?string
{
    return $stringOuNulo;
}

// Induções de tipo podem ser especificadas em propriedades de classes
class PropriedadesComTipos
{
    public ?string $nome;
    
    protected int $id;

    private float $pontuacao = 100.0;

    // O verificador de tipos do Hack reforça que propriedades tipadas devem
    // ter um valor padrão ou devem ser definidos no construtor
    public function __construct(int $id)
    {
        $this->id = $id;
    }
}


// Funções anônimas (lambdas)
$multiplicador = 5;
array_map($y ==> $y * $multiplicador, [1, 2, 3]);


// Genéricos
class Caixa<T>
{
    protected T $dados;

    public function __construct(T $dados) {
        $this->dados = $dados;
    }

    public function pegaDados(): T {
        return $this->dados;
    }
}

function abreCaixa(Caixa<int> $caixa) : int
{
    return $caixa->pegaDados();
}


// Formas
//
// Hack adiciona o conceito de formas para definir arrays com uma estrutura
// e tipos de dados garantidos
type Point2D = shape('x' => int, 'y' => int);

function distancia(Point2D $a, Point2D $b) : float
{
    return sqrt(pow($b['x'] - $a['x'], 2) + pow($b['y'] - $a['y'], 2));
}

distancia(
    shape('x' => -1, 'y' => 5),
    shape('x' => 2, 'y' => 50)
);


// Pseudônimos de tipos
//
// Hack adiciona vários recursos para criação de pseudônimos, tornando tipos complexos
// mais fáceis de entender
newtype VectorArray = array<int, Vector<int>>;

// Um tuple contendo dois inteiros
newtype Point = (int, int);

function adicionaPontos(Point $p1, Point $p2) : Point
{
    return tuple($p1[0] + $p2[0], $p1[1] + $p2[1]);
}

adicionaPontos(
    tuple(1, 2),
    tuple(5, 6)
);


// enums em classes
enum TipoDePista : int
{
    Estrada = 0;
    Rua = 1;
    Alameda = 2;
    Avenida = 3;
}

function getTipoDePista() : TipoDePista
{
    return TipoDePista::Alameda;
}


// Especificação de argumentos no construtor (Argument Promotion)
// 
// Para evitar que propriedades sejam definidas em mais de um lugar, e
// construtores que só definem propriedades, o Hack adiciona uma sintaxe para
// definir as propriedades e o construtor ao mesmo tempo.
class ArgumentPromotion
{
    public function __construct(public string $nome,
                                protected int $idade,
                                private bool $legal) {}
}

class SemArgumentPromotion
{
    public string $nome;

    protected int $idade;

    private bool $legal;

    public function __construct(string $nome, int $idade, bool $legal)
    {
        $this->nome = $nome;
        $this->idade = $idade;
        $this->legal = $legal;
    }
}


// Multi-tarefas cooperativo
//
// Duas novas palavras-chave ("async" e "await") podem ser usadas para
// trabalhar com multi-tarefas.
// Obs. Isto não envolve threads - apenas permite a transferência de controle
async function printCooperativo(int $inicio, int $fim) : Awaitable<void>
{
    for ($i = $inicio; $i <= $fim; $i++) { 
        echo "$i ";

        // Permite que outras tarefas façam algo
        await RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0);
    }
}

// Imprime "1 4 7 2 5 8 3 6 9"
AwaitAllWaitHandle::fromArray([
    printCooperativo(1, 3),
    printCooperativo(4, 6),
    printCooperativo(7, 9)
])->getWaitHandle()->join();


// Atributos
//
// Atributos são uma forma de definir metadados para funções.
// Hack tem alguns atributos especiais que possuem comportamentos úteis.

// O atributo especial __Memoize faz com que o resultado da função fique em cache
<<__Memoize>>
function tarefaDemorada() : ?string
{
    return file_get_contents('http://exemplo.com');
}

// O corpo da função só é executado uma vez aqui:
tarefaDemorada();
tarefaDemorada();


// O atributo especial __ConsistentConstruct faz com que o Hack certifique-se
// de que a assinatura do construtor seja a mesma em todas as subclasses
<<__ConsistentConstruct>>
class FooConsistente
{
    public function __construct(int $x, float $y)
    {
        // ...
    }

    public function algumMetodo()
    {
        // ...
    }
}

class BarConsistente extends FooConsistente
{
    public function __construct(int $x, float $y)
    {
        // O verificador de tipos do Hack exige que os construtores pai
        // sejam chamados
        parent::__construct($x, $y);

        // ...
    }

    // A anotação __Override é uma anotação opcional que faz com que o
    // verificador de tipos do Hack sobrescreva um método em uma classe pai
    // ou um trait. Sem __Override, definir este método causará um erro,
    // pois ele já foi definido na classe pai (FooConsistente):
    <<__Override>>
    public function algumMetodo()
    {
        // ...
    }
}

class SubclasseFooInvalida extends FooConsistente
{
    // Caso o construtor não combine com o construtor da classe pai, o
    // verificador de tipos acusará um erro:
    //
    //  "Este objeto é incompatível com o objeto FooConsistente porque algum(ns)
    //   dos seus métodos são incompatíveis"
    //  
    public function __construct(float $x)
    {
        // ...
    }

    // Usar a anotação __Override em um método que não existe na classe pai
    // causará um erro do verificador de tipos:
    //  "SubclasseFooInvalida::outroMetodo() está marcada para sobrescrever;
    //   nenhuma definição não-privada foi encontrada ou a classe pai foi
    //   definida em código não-<?hh"
    //  
    <<__Override>>
    public function outroMetodo()
    {
        // ...
    }
}


// Traits podem implementar interfaces (não suportado pelo PHP)
interface InterfaceGatinho
{
    public function brinca() : void;
}

trait TraitGato implements InterfaceGatinho
{
    public function brinca() : void
    {
        // ...
    }
}

class Samuel
{
    use TraitGato;
}


$gato = new Samuel();
$gato instanceof InterfaceGatinho === true; // True

```

## Mais informações

Visite a [documentação do Hack](http://docs.hhvm.com/manual/en/hacklangref.php)
para ver explicações detalhadas dos recursos que Hack adiciona ao PHP, ou o [site oficial do Hack](http://hanlang.org/)
para outras informações.

Visite o [site oficial do HHVM](http://hhvm.com/) para aprender a instalar o HHVM.

Visite [este artigo](http://docs.hhvm.com/manual/en/hack.unsupported.php) para ver
os recursos do PHP que o Hack não suporta e ver incompatibilidades entre Hack e PHP.
---
language: Haskell
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["Lucas Tonussi", "http://www.inf.ufsc.br/~tonussi/"]
lang: pt-br
filename: learnhaskell-pt.hs
---

As linguagens funcionais são linguagens de programação com base em avaliação
de funções matemáticas (expressões), evitando-se o conceito de mudança de
estado com alteração de dados. Neste aspecto, este paradigma é oposto ao
paradigma imperativo que se baseia em alterações de estados.

A programação funcional começou no cálculo lambda, que foi base teórica para
o desenvolvimento deste paradigma de programação.


```haskell
-- Para comentar a linha basta dois traços seguidos.

{- Abre chaves traço e traço fecha chaves cria um campo
   para comentário em múltiplas linhas.
-}

----------------------------------------------------
-- 1. Tipos Primitivos de Dados e Operadores
----------------------------------------------------

-- Numerais

0 -- 3
1 -- 1
2 -- 2 ...

-- Alguns Operadores Fundamentais

7 + 7 -- 7 mais 7
7 - 7 -- 7 menos 7
7 * 7 -- 7 vezes 7
7 / 7 -- 7 dividido por 7

-- Divisões não são inteiras, são fracionádas por padrão da linguagem
28736 / 82374 -- 0.3488479374559934


-- Divisão inteira
82374 `div` 28736 -- 2

-- Divisão modular
82374 `mod` 28736 -- 24902

-- Booleanos como tipo primitivo de dado
True -- Verdadeiro
False -- Falso

-- Operadores unitário
not True -- Nega uma verdade
not False -- Nega uma falácia


-- Operadores binários
7 == 7 -- 7 é igual a 7 ?
7 /= 7 -- 7 é diferente de 7 ?
7 < 7 -- 7 é menor que 7 ?
7 > 7 -- 7 é maior que 7 ?


{- Haskell é uma linguagem que tem uma sintáxe bastante familiar na
   matemática, por exemplo em chamadas de funções você tem:

   NomeFunção ArgumentoA ArgumentoB ArgumentoC ...
-}

-- Strings e Caractéres
"Texto entre abre áspas e fecha áspas define uma string"
'a' -- Caractere
'A' -- Caractere

'Strings entre aspas simples sobe um erro' -- Erro léxico!

-- Concatenação de Strings
"StringA" ++ "StringB" -- "StringAStringB"

-- Concatenação de Caracteres
"haskell" == ['h','a','s','k','e','l','l'] -- True
"haskell" == 'h':'a':'s':'k':'e':'l':'l':[] -- True

-- Você pode listar uma string pelos seus caractéres
"AbBbbcAbbcbBbcbcb" !! 0 -- 'A'
"AbBbbcAbbcbBbcbcb" !! 1 -- 'b'
"AbBbbcAbbcbBbcbcb" !! 2 -- 'B'

----------------------------------------------------
-- 2. Listas e Túplas
----------------------------------------------------

-- A construção de uma lista precisa ser de elementos homogêneos
[1, 2, 3, 4, 5] -- Homogênea
[1, a, 2, b, 3] -- Heterogênea (Erro)

-- Haskell permite que você crie sequências
[1..5]

{- Haskell usa avaliação preguiçosa o que
   permite você ter listas "infinitas".
-}

-- Uma lista "infinita" cuja razão é 1
[1..]

-- O 777º elemento de uma lista de razão 1
[1..] !! 777 -- 778

-- União de listas [lista_0] ++ [lista_1] ++ [lista_i]
[1..5] ++ [6..10] ++ [1..4] -- [1,2,3,4,5,6,7,8,9,10,1,2,3,4]

-- Adiciona um cabeçalho a sua lista e desloca a cauda
0:[1..10] -- [0, 1, 2, 3, 4, 5]
'a':['a'..'e'] -- "aabcde"

-- Indexação em uma lista
[0..] !! 5 -- 5

-- Operadores de Listas usuais
head ['a'..'e'] -- Qual o cabeçalho da lista ?
tail ['a'..'e'] -- Qual a cauda da lista ?
init ['a'..'e'] -- Qual a lista menos o último elemento ?
last ['a'..'e'] -- Qual o último elemento ?

-- Compreensão de Lista (List Comprehension)

{- Uma lista pode ser especificada
   pela definição de eus elementos.
   A compreensão de listas é feita
   com um construtor de listas que
   utiliza conceitos  e notações
   da teoria dos conjuntos.

   Exemplo:

   A = { x**2 | X pertence aos Naturais && x é par}
-}

let par x = mod x 2 == 0
let constroi_lista = [x * x | x <- [9 ..39], par x]
-- [100,144,196,256,324,400,484,576,676,784,900,1024,1156,1296,1444]

par 4 -- True
par 3 -- False


-- Listas com regras
{- Para todo x se x é elemento da lista
   faça 2 vezes x mas componha a lista
   com apenas aqueles elementos cujo
   2*x é maior que 4
-}
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- Tuplas
("Q", "Gamma", "b", "Sigma", "delta", "q0", "F") -- 7-Tuple Turing Machine

-- Retirando da tupla

{- Com as funções fst (primeiro) e snd (segundo)
   você só pode enviar por parâmetro uma tupla
   bi-dimensional ou seja, 2 dimensões == (x,y)
-}

fst ((2,3), [2,3]) -- (2,3)
snd ((2,3), [4,3]) -- [4,3]


----------------------------------------------------
-- 3. Funções em Haskell
----------------------------------------------------

-- Uma função simples que toma duas variáveis
{- Haskell trabalha em cima de recursão
   Portanto certifique-se que você
   Entende como recurssão funciona.
-}

soma a b = a + b -- Função que vai em um programa.hs

{- Dentro do GHCi (Interpretador Haskell)
   Você terá que fazer da seguinte maneira-- Podemos criar nos

   Prelude> let soma a b = a + b
   Prelude> soma 7 7 -- 14
-}

let constroi_lista = [x * x | x <- [9 ..39], par x]

{- Você pode usar crases para chamar
   Funcões de maneira diferente
-}

7 `soma` 7 -- 14

{- Haskell permite que você crie os
   seus próprios operadores baseados
   nos já existendes
-}

let (~/\) a b = a `mod` b
15^13 ~/\ 432 -- 759375

-- Casamento de Padrões em Tuplas
coordenadas (x, y) = (x + 13, y - 31)

{- Haskell trabalha com casamento de padrões onde dada
   um conjunto de funções definidas de diferentes maneiras
   Haskell vai procurar por aquela que trabalha o seu tipo
   de entrada.
-}

-- Guardas "|" É um jeito simples de representar funções recursivas

let fatorial n | n == 0 = 1 | otherwise = fatorial (n - 1) * n -- Teste: fatorial 5

-- Ainda podemos fazer:

let fatorial 0 = 1
let fatorial n = fatorial (n - 1) * n

{- Podemos criar nossos próprios Mapeadores
   Onde `primeiro` é o primeiro elemento de
   uma lista é `resto`  é o resto da lista.
-}

mapa mapeador _ [] = []
mapa mapeador (primeiro : resto) = mapeador primeiro : (mapa mapeador resto)

{- Uma função anônima é uma função sem um nome.
   É uma abstração do cálculo lambda:

   \x -> x + 1
   λ.x (x + 1)

   Em Haskell Barra Invertida é um jeito para
   se escrever Lambda (λ). Uma ótima pedida
   Para entender Haskell e outras linguagens como Lisp
   É estudar Cálculo Lambda, é um entendimento matemático
   mais apurado. E do ponto de vista computacional é
   bastante interessante. Em EXTRAS você encontrará
   Links para aprender Cálculo Lambda.
-}

(\x -> x + 1) 4 -- 5


{- Algumas vezes é mais conveniente usar expressões lambda
   do que definir um nome para uma função. Na matemática
   Nomes são muito simbólicos. Isso acontece bastante
   quando você estiver trabalhando `map` ou `foldl` / `foldr`
-}

-- Sem usar expressões anônimas !
listaSomaUm lst = map somaUm' lst where somaUm' x = x + 1

-- Usando expressões anônimas !
listaSomaUm' lst = map (\x -> x + 1) lst

----------------------------------------------------
-- 4. Mais Funções
----------------------------------------------------

{- Currying: Se você não passar todos os argumentos
   para uma função, ela irá ser "currificada". O que
   significa que irá retornar a função que pega o resto
   dos elementos.
-}

soma a b = a + b
foo = soma 10 -- foo ganha a propriedade "currying"
foo 5 -- 15

-- Outra maneira
foo = (+10)
foo 5 -- 15

{- Composição de Funções
   O (.) encadeia funções! Por exemplo,
   aqui foo é uma função que recebe um valor.
   Ela soma 10 a ela, multiplica o resultado por 5
   e então retorna o resultado final.
-}
foo = (*5) . (+10)

-- (5 + 10) * 5 = 75
foo 5 -- 75

{- Concertando precedência:
   Haskell tem outra função chamada `$`. Isso altera a precedência
   de computação. Ou seja Haskell computa o que está sendo sinalizado com $
   da esquerda para a direita . Você pode usar `.` e `$` para se livrar
   de parentízação desnecessária.
-}

(even (fatorial 3)) -- true

-- Usando `.` e `$`
even . fatorial $ 3 -- true

----------------------------------------------------
-- 5. Tipos
----------------------------------------------------

-- Haskell é fortemente tipado e tudo tem uma assinatura típica.

-- Tipos Básicos:
460 :: Integer
"music" :: String
True :: Bool

{- Funções também tem tipos.
   `not` recebe um booleano e retorna um booleano:
   not :: Bool -> Bool
-}

{- Aqui temos uma função que recebe dois argumentos
   soma :: Integer -> Integer -> Integer
-}

{- Quando você define um valor em Haskell
   uma boa prática de programação é escrever
   o TIPO acima dessa mesma. Como segue:
-}

double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. Controle de Fluxo e IF-THEN-ELSE
----------------------------------------------------

-- Blocos IF-THEN-ELSE
let valor_alternado = if 144 `mod` 6 == 4 then "acertou" else "errou" -- errou

-- É legal identar quando você tem múltiplos branchs para acontecer

let valor_alternado = if 144 `mod` 6 == 4
                      then "acertou"
                      else "errou"

-- Blocos CASE

{- caso <argumento> seja :
        <ajuda>  -> mostra_ajuda
        <inicia> -> inicia_programa
        <_>      -> putStrLn "ExArgumentoInvalido"

    Onde `_` Significa Qualquer Outra Coisa.
-}


case args of
     "ajuda"  -> mostra_ajuda
     "inicia" -> inicia_programa
     _        -> putStrLn "ExArgumentoInvalido"

{- Haskell não funciona na base de loops pois ele é
   fortemente baseado em funcões recursivas e cálculo lambda

   Use `map` uma função build-in do interpretador
   para, por exemplo, mapear uma lista:
-}
map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- Você pode criar um FOR-LOOP usando map
let for array funcao = map funcao array
for [0..5] $ \i -> show i

-- Ou ainda (Pesquise sobre show em Haskell):
for [0..5] show


{- foldl computação é feita esquerda para direita
   foldr computação é feita  direita para esquerda

   Você pode usar foldl or foldr a fim de reduzir uma lista
   fold(l||r) <funcao> <valor inicial> <lista>
-}

-- Fold Left
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- Pensando Recursivamente Esquerda-Direita
(2 * (2 * (2 * 4 + 1) + 2) + 3) -- 43

-- Fold Right
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- Pensando Recursivamente Direita-Esquerda
(2 * 3 + (2 * 2 + (2 * 1 + 4)))

----------------------------------------------------
-- 7. Declaração de Dados
----------------------------------------------------

{- Vamos começar definindo um tipo de
   dado que é uma cor rgb então ela
   tem valores para vermelho azul e verde
   ela é composta desses 3 comprimentos
   Vamos usar `data` e `say` que são built-in:

   Haskell pede que você user letra
   maiuscula para tipos (types) ou classes (Class)

   Por favor, visite: http://www.haskell.org/haskellwiki/Type
   E de uma olhada na fórmula genérica de declaração de dados.
-}

data Cor = Vermelho | Azul | Verde

-- say :: Color -> String

let say Vermelho = "Vermelho"
let say Azul = "Azul"
let say Verde = "Verde"

{- O seu tipo de dados por receber parâmetros também
   vamos com um exemplo usando `data` e a Classe `Maybe`.
-}

data Maybe a = Nothing | Just a

-- Just e Nothing são todos derivadas de Maybe
Just "hello" -- tipo `Maybe String`
Just 1       -- tipo `Maybe Int`
Nothing      -- tipo `Maybe a` para algum `a`


----------------------------------------------------
-- 8. Mônadas
----------------------------------------------------

{- As mônadas permitem que o programador construa computações
   sando os blocos de comando sequenciais, os quais, por sua vez,
   podem ter outras sequencias de computações. Para entender melhor
   a classe Monads você precisa ler um pouco mais sobre Classes em
   Haskell e o polímofirmo ad hoc do Haskell.

   A Classe Mônada padrão em Haskell é a seguinte:
-}

class Monad m where
  (>>=)  :: m a -> (a -> m b) -> m b
  (>>)   :: m a -> m b -> m b
  return :: m -> m a
  fail   :: String -> m a

  -- Definição completa mínima:
  -- (>>=), return

  m >> k = m >>= \_ -> k
  fail s = error s

{- Como exemplo, a função le_imprime opera com a função ">=" da
   classe mônada, a qual repassa o retorno obtido com a função
   getLine para uma função lambda \e qualquer.

   GHC-BASICS
   Cria um arquivo chamado le_imprime.hs
   compile: ghc --make -c -O Programa_Haskell_Principal.hs
   execute: ./Programa_Haskell_Principal
-}

le_imprime :: IO ()
le_imprime = getLine >>= \e -> putStrLn e -- le_imprime = getLine >>= putStrLn

{- Mônadas abrem a possibilidade de criar computações
   no estilo imperativo dentro de um grande programa funcional

   Leis das Mônadas:

   1. return a >>= k  =  k a
   2. m >>= return  =  m
   3. m >>= (\x -> k x >>= h)  =  (m >>= k) >>= h
-}

-- O operador >> é chamada então (p -> q, p então q)
let m >> n = m >>= \_ -> n


----------------------------------------------------
-- 9. Haskell Entrada/Saída
----------------------------------------------------

{- Quando um programa Haskell é executado a função `main` é
   chamada. E ela precisa retornar um valor do tipo IO().
-}

module Main where
  main :: IO ()
  main = putStrLn $ "Oi Glasgow!"

-- Ou simplesmente:

main = putStrLn $ "Oi Glasgow!"

{- putStrLn é do tipo String -> IO()

   É o jeito mais fácil de conseguir E/S se você implementar
   o seu programa como uma função de String para String.

   A função:
      interact :: (String -> String) -> IO ()
   Joga texto, roda a função nela mesma, e imprime a saída
-}

module Main where
  contadorLinhas = show . length . lines
  main = interact contadorLinhas

-- Use a notação `do` para encadear ações. Por exemplo:

diga_oi :: IO ()

diga_oi = do

  putStrLn "Qual eh o seu nome?"
  name <- getLine
  putStrLn $ "Oi, " ++ name

main = diga_oi

{- Exercício! Escreva sua própria versão
   onde irá ler apenas uma linhas de input.

   Vamos entender melhor como `getLine` funciona?
      getLine :: IO String
   Pense que o valor do tipo `IO a` representando um
   programa de computador que irá gerar um valor do tipo `a`
   quando for ele executado.
   
   Nós podemos guardar e reusar isso apenas apontando `<-`.
   Nós podemos também cria nossas próprias ações do tipo `IO String`
-}

nova_acao :: IO String

nova_acao = do
  putStrLn "Uma string curta o bastante."
  entra1 <- getLine
  entra2 <- getLine
  -- return :: String -> IO String
  return (entra1 ++ "\n" ++ entra2)

{- Nós podemos usar da seguinte maneira
   como acabamos de usar `getLine`, exemplo:
-}

main'' = do
  putStrLn "String A"
  result <- action
  putStrLn result
  putStrLn "String B"

----------------------------------------------------
-- 9. O Haskell REPL (Read Eval Print Loop)
----------------------------------------------------

{- Digite dhci no seu terminal
   para começar o interpretador
   lembre-se que para definir
   funções e variáveis em haskell
   pelo interpretador você precisar
   iniciar com `let`
-}

Prelude> let foo = 1.4

-- Você pode ver o tipo de algo usando `:t`:

Prelude> :t foo
foo :: Double
```


# Extra

Compilador e Interpretador Haskell

* [GHC](http://www.haskell.org/ghc/docs/latest/html/users_guide/index.html)
* [GHC/GHCi](http://www.haskell.org/haskellwiki/GHC)
* [Haskell em 5 Passos !!!](http://www.haskell.org/haskellwiki/Haskell_in_5_steps)

Instale Haskell [Aqui!](http://www.haskell.org/platform/).

Aplicações Haskell Muito Interessantes:

* [Música e Som](http://www.haskell.org/haskellwiki/Applications_and_libraries/Music_and_sound)
* [Haskell SuperCollider Servidor](https://github.com/kaoskorobase/hsc3-server)
* [Haskell SuperCollider Cliente](http://hackage.haskell.org/package/hsc3)
* [Física e Matemática](http://www.haskell.org/haskellwiki/Applications_and_libraries/Mathematics)
* [Jogos](http://www.haskell.org/haskellwiki/Applications_and_libraries/Games)
* [Bio Informática](http://www.haskell.org/haskellwiki/Applications_and_libraries/Bioinformatics)
* [Muitos Outras Aplicações](http://www.haskell.org/haskellwiki/Libraries_and_tools)

Comunidade Haskell
* [Musica das Mônadas](http://www.haskell.org/haskellwiki/Music_of_monads)
* [Entendendo Mônadas](https://en.wikibooks.org/wiki/Haskell/Understanding_monads)

Tutoriais:

* [Mapeadores](http://www.haskell.org/ghc/docs/6.12.2/html/libraries/containers-0.3.0.0/Data-Map.html)
* [Aprenda Haskell!](http://haskell.tailorfontela.com.br/chapters)
* [Fundação Teórica da Linguagem Haskell](http://www.haskell.org/haskellwiki/Lambda_calculus)
* [Classe Maybe](http://www.haskell.org/haskellwiki/Maybe)
* [Zvon Referência Haskell](http://www.zvon.org/other/haskell/)

Obtenha Também Haskell Wiki Book [Aqui!](https://en.wikibooks.org/wiki/Haskell)

Leia Sobre As Mônadas [Aqui!](http://www.haskell.org/haskellwiki/Monads)

Livro: Haskell Uma Abordagem Prática - Claudio Cesar de Sá e Márcio Ferreira da Silva
---
language: hy
filename: learnhy-pt.hy
contributors:
    - ["Abhishek L", "http://twitter.com/abhishekl"]
translators:
    - ["Miguel Araújo", "https://github.com/miguelarauj1o"]
lang: pt-br
---

Hy é um dialeto de Lisp escrito sobre Python. Isto é possível convertendo
código Hy em árvore sintática abstrata python (ast). Portanto, isto permite
hy chamar código python nativo e vice-versa.

Este tutorial funciona para hy ≥ 0.9.12

```clojure
;; Isso dá uma introdução básica em hy, como uma preliminar para o link abaixo
;; http://try-hy.appspot.com
;;
; Comentários em ponto-e-vírgula, como em outros LISPS

;; s-noções básicas de expressão
; programas Lisp são feitos de expressões simbólicas ou sexps que se assemelham
(some-function args)
; agora o essencial "Olá mundo"
(print "hello world")

;; Tipos de dados simples
; Todos os tipos de dados simples são exatamente semelhantes aos seus homólogos 
; em python que
42 ; => 42
3.14 ; => 3.14
True ; => True
4+10j ; => (4+10j) um número complexo

; Vamos começar com um pouco de aritmética muito simples
(+ 4 1) ;=> 5
; o operador é aplicado a todos os argumentos, como outros lisps
(+ 4 1 2 3) ;=> 10
(- 2 1) ;=> 1
(* 4 2) ;=> 8
(/ 4 1) ;=> 4
(% 4 2) ;=> 0 o operador módulo
; exponenciação é representado pelo operador ** como python
(** 3 2) ;=> 9
; formas aninhadas vão fazer a coisa esperada
(+ 2 (* 4 2)) ;=> 10
; também operadores lógicos e ou não e igual etc. faz como esperado
(= 5 4) ;=> False
(not (= 5 4)) ;=> True

;; variáveis
; variáveis são definidas usando SETV, nomes de variáveis podem usar utf-8, exceto
; for ()[]{}",'`;#|
(setv a 42)
(setv π 3.14159)
(def *foo* 42)
;; outros tipos de dados de armazenamento
; strings, lists, tuples & dicts
; estes são exatamente os mesmos tipos de armazenamento de python
"hello world" ;=> "hello world"
; operações de string funcionam semelhante em python
(+ "hello " "world") ;=> "hello world"
; Listas são criadas usando [], a indexação começa em 0
(setv mylist [1 2 3 4])
; tuplas são estruturas de dados imutáveis
(setv mytuple (, 1 2))
; dicionários são pares de valores-chave
(setv dict1 {"key1" 42 "key2" 21})
; :nome pode ser utilizado para definir palavras-chave em hy que podem ser utilizados para as chaves
(setv dict2 {:key1 41 :key2 20})
; usar 'get' para obter o elemento em um índice/key
(get mylist 1) ;=> 2
(get dict1 "key1") ;=> 42
; Alternativamente, se foram utilizadas palavras-chave que podem ser chamadas diretamente
(:key1 dict2) ;=> 41

;; funções e outras estruturas de programa
; funções são definidas usando defn, o último sexp é devolvido por padrão
(defn greet [name]
  "A simple greeting" ; uma docstring opcional
  (print "hello " name))

(greet "bilbo") ;=> "hello bilbo"

; funções podem ter argumentos opcionais, bem como argumentos-chave
(defn foolists [arg1 &optional [arg2 2]]
  [arg1 arg2])

(foolists 3) ;=> [3 2]
(foolists 10 3) ;=> [10 3]

; funções anônimas são criados usando construtores 'fn' ou 'lambda'
; que são semelhantes para 'defn'
(map (fn [x] (* x x)) [1 2 3 4]) ;=> [1 4 9 16]

;; operações de sequência
; hy tem algumas utils embutidas para operações de sequência, etc.
; recuperar o primeiro elemento usando 'first' ou 'car'
(setv mylist [1 2 3 4])
(setv mydict {"a" 1 "b" 2})
(first mylist) ;=> 1

; corte listas usando 'slice'
(slice mylist 1 3) ;=> [2 3]

; obter elementos de uma lista ou dict usando 'get'
(get mylist 1) ;=> 2
(get mydict "b") ;=> 2
; lista de indexação começa a partir de 0, igual em python
; assoc pode definir elementos em chaves/índices
(assoc mylist 2 10) ; faz mylist [1 2 10 4]
(assoc mydict "c" 3) ; faz mydict {"a" 1 "b" 2 "c" 3}
; há toda uma série de outras funções essenciais que torna o trabalho com 
; sequências uma diversão

;; Python interop
;; importação funciona exatamente como em python
(import datetime)
(import [functools [partial reduce]]) ; importa fun1 e fun2 do module1
(import [matplotlib.pyplot :as plt]) ; fazendo uma importação em foo como em bar
; todos os métodos de python embutidas etc. são acessíveis a partir hy
; a.foo(arg) is called as (.foo a arg)
(.split (.strip "hello world  ")) ;=> ["hello" "world"]

;; Condicionais
; (if condition (body-if-true) (body-if-false)
(if (= passcode "moria")
  (print "welcome")
  (print "Speak friend, and Enter!"))

; aninhe múltiplas cláusulas 'if else if' com cond
(cond
 [(= someval 42)
  (print "Life, universe and everything else!")]
 [(> someval 42)
  (print "val too large")]
 [(< someval 42)
  (print "val too small")])

; declarações de grupo com 'do', essas são executadas sequencialmente
; formas como defn tem um 'do' implícito
(do
 (setv someval 10)
 (print "someval is set to " someval)) ;=> 10

; criar ligações lexicais com 'let', todas as variáveis definidas desta forma
; tem escopo local
(let [[nemesis {"superman" "lex luther"
                "sherlock" "moriarty"
                "seinfeld" "newman"}]]
  (for [(, h v) (.items nemesis)]
	(print (.format "{0}'s nemesis was {1}" h v))))

;; classes
; classes são definidas da seguinte maneira
(defclass Wizard [object]
  [[--init-- (fn [self spell]
             (setv self.spell spell) ; init a mágica attr
             None)]
   [get-spell (fn [self]
              self.spell)]])

;; acesse hylang.org
```

### Outras Leituras

Este tutorial é apenas uma introdução básica para hy/lisp/python.

Docs Hy: [http://hy.readthedocs.org](http://hy.readthedocs.org)

Repo Hy no GitHub: [http://github.com/hylang/hy](http://github.com/hylang/hy)

Acesso ao freenode irc com #hy, hashtag no twitter: #hylang
---

language: java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Madison Dickson", "http://github.com/mix3d"]
translators:
    - ["Victor Kléber Santos L. Melo", "http://victormelo.com.br/blog"]
    - ["Renê Douglas N. de Morais", "mailto:rene.douglas.bsi@gmail.com"]
lang: pt-br
filename: LearnJava-pt.java

---

Java é uma linguagem de programação de propósito geral, concorrente, baseada em classes e orientada a objetos.
[Leia mais aqui](http://docs.oracle.com/javase/tutorial/java/index.html)

```java
// Comentários de uma linha começam com //
/*
Comentários de várias linhas são feitos dessa forma.
*/
/**
Comentários JavaDoc são feitos assim. São usados para descrever a Classe ou os atributos da Classe.
*/

// Importa a classe ArrayList que está dentro do pacote java.util
import java.util.ArrayList;
// Importa todas as classes que estão dentro do pacote java.security
import java.security.*;

// Cada arquivo .java contém uma classe pública, com o mesmo nome do arquivo.
public class LearnJava {

    // Um programa precisa ter um método main como um ponto de entrada.
    public static void main (String[] args) {

        // O System.out.println é usado para imprimir no console
        System.out.println("Olá Mundo!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // Para imprimir sem inserir uma nova lina, use o System.out.print
        System.out.print("Olá ");
        System.out.print("Mundo");


        ///////////////////////////////////////
        // Tipos & Variáveis
        ///////////////////////////////////////

        // Declara-se variáveis usando <tipo> <nome> [
        // Byte - inteiro de 8 bits com sinal complementado a dois
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short - inteiro de 16 bits com sinal complementado a dois
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - inteiro de 32 bits com sinal complementado a dois
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int fooInt = 1;

        // Long - inteiro de 64 bits com sinal complementado a dois
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L é usado para indicar que o valor da variável é do tipo Long;
        // sem o L, tudo é tratado como inteiro por padrão.

        // Nota: Java não tem tipos sem sinal

        // Float - Ponto Flutuante 32-bits, de precisão simples no padrão IEEE 754
        float fooFloat = 234.5f;
        // f é usado para indicar que o valor da variável é do tipo float;
        // caso contrário, ela é tratada como double.

        // Double - Ponto Flutuante 64-bits, de precisão dupla no padrão IEEE 754
        double fooDouble = 123.4;

        // Boolean - true & false
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - Um caractere Unicode de 16 bits
        char fooChar = 'A';

        // Usa-se o final para fazer com que a variável seja imutável.
        final int HORAS_QUE_TRABALHEI_POR_SEMANA = 9001;

        // Strings
        String fooString = "Aqui está minha String!";

        // \n é um caractere de escape que inicia uma nova linha
        String barString = "Imprimir em uma nova linha?\nSem problemas!";
        // \t é um caractere de escape que adiciona um caractere de tabulação
        String bazString = "Você quer adicionar tabulação?\tSem problemas!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // Arrays
        //O tamanho do array precisa ser determinado na sua declaração
        //O formato para declarar um array é:
        //<tipo de dado> [] <nome da variável> = new <tipo de dado>[<tamanho do array>];
        int [] intArray = new int[10];
        String [] stringArray = new String[1];
        boolean [] booleanArray = new boolean[100];

        // Outra maneira de declarar e inicializar um array
        int [] y = {9000, 1000, 1337};

        // Indexando um array - Acessando um elemento
        System.out.println("intArray no índice 0: " + intArray[0]);

        // O primeiro termo de um array é o 0 e eles são mutáveis.
        intArray[1] = 1;
        System.out.println("intArray no índice 1: " + intArray[1]); // => 1

        // Outras estruturas que devem ser vistas
        // ArrayLists - São parecidos com os arrays, porém oferecem mais funcionalidades
        //             e o tamanho é mutável.
        // LinkedLists
        // Maps
        // HashMaps

        ///////////////////////////////////////
        // Operadores
        ///////////////////////////////////////
        System.out.println("\n->Operadores");

        int i1 = 1, i2 = 2; // Forma abreviada de escrever múltiplas declarações.

        // Aritmética é feita da forma convencional
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (0.5 arredondado para baixo)

        // Módulo
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Operadores de comparação
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Operadores bit-a-bit!
        /*
        ~       Complemento de um
        <<      Deslocamento a esquerda com sinal
        >>      Deslocamento a direita com sinal
        >>>     Deslocamento a direita sem sinal
        &       E bit-a-bit
        |       OU bit-a-bit
        ^       OU exclusivo bit-a-bit
        */

        // Incrementações
        int i = 0;
        System.out.println("\n->Inc/Dec-rementação");
        System.out.println(i++); //i = 1. Pós-Incrementação
        System.out.println(++i); //i = 2. Pre-Incrementação
        System.out.println(i--); //i = 1. Pós-Decrementação
        System.out.println(--i); //i = 0. Pre-Decrementação

        ///////////////////////////////////////
        // Estruturas de Controle
        ///////////////////////////////////////
        System.out.println("\n->Estruturas de Controle");

        // Os comandos If são parecidos com o da linguagem C
        int j = 10;
        if (j == 10){
            System.out.println("Eu serei impresso");
        } else if (j > 10) {
            System.out.println("Eu não");
        } else {
            System.out.println("Eu também não");
        }

        // O Loop While
        int fooWhile = 0;
        while(fooWhile < 100)
        {
            //System.out.println(fooWhile);
            //Incrementando o contador
            //Iteração feita 99 vezes, fooWhile 0->99
            fooWhile++;
        }
        System.out.println("Valor do fooWhile: " + fooWhile);

        // O Loop Do While
        int fooDoWhile = 0;
        do
        {
            //System.out.println(fooDoWhile);
            //Incrementando o contador
            //Iteração feita 99 vezes, fooDoWhile 0->99
            fooDoWhile++;
        }while(fooDoWhile < 100);
        System.out.println("Valor do fooDoWhile: " + fooDoWhile);

        // O Loop For
        int fooFor;
        //estrutura do loop for => for(<operação_de_início>; <condição>; <passo>)
        for(fooFor=0; fooFor<10; fooFor++){
            //System.out.println(fooFor);
            //Iteração feita 10 vezes, fooFor 0->9
        }
        System.out.println("Valor do fooFor: " + fooFor);

        // O Loop For Each
        // Itera automaticamente por um array ou lista de objetos.
        int[] fooList = {1,2,3,4,5,6,7,8,9};
        //estrutura do loop for each => for(<objeto> : <array_de_objeto>)
        //lê-se: para cada objeto no array
        //nota: o tipo do objeto deve ser o mesmo do array.

        for( int bar : fooList ){
            //System.out.println(bar);
            //Itera 9 vezes e imprime 1-9 em novas linhas
        }

        // Switch
        // Um switch funciona com os tipos de dados: byte, short, char e int
        // Ele também funciona com tipos enumerados (vistos em tipos Enum)
        // como também a classe String e algumas outras classes especiais
        // tipos primitivos: Character, Byte, Short e Integer
        int mes = 3;
        String mesString;
        switch (mes){
            case 1:
                    mesString = "Janeiro";
                    break;
            case 2:
                    mesString = "Fevereiro";
                    break;
            case 3:
                    mesString = "Março";
                    break;
            default:
                    mesString = "Algum outro mês";
                    break;
        }
        System.out.println("Resultado do Switch: " + mesString);

        // Condição de forma abreviada.
        // Você pode usar o operador '?' para atribuições rápidas ou decisões lógicas.
        // Lê-se "Se (declaração) é verdadeira, use <primeiro valor>
        // caso contrário, use <segundo valor>".
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println(bar); //Imprime A, pois a condição é verdadeira.


        ///////////////////////////////////////
        // Convertendo tipos de dados e Casting
        ///////////////////////////////////////

        //Conversão de Dados

        //Convertendo String para Inteiro.
        Integer.parseInt("123");//retorna uma versão inteira de "123".

        //Convertendo Inteiro para String
        Integer.toString(123);//retorna uma versão String de 123.

        // Para outras conversões confira as seguintes classes
        // Double
        // Long
        // String

        // Casting
        // Você pode também converter objetos java, há vários detalhes e
        // lida com alguns conceitos intermediários
        // Dê uma olhada no link:
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Classes e Métodos
        ///////////////////////////////////////

        System.out.println("\n->Classes e Métodos");

        // (segue a definição da classe Bicicleta)

        // Use o new para instanciar uma classe
        Bicicleta caloi = new Bicicleta(); // Objeto caloi criado.

        // Chame os métodos do objeto
        caloi.aumentarVelocidade(3); // Você deve sempre usar métodos para modificar variáveis
        caloi.setRitmo(100);

        // toString é uma convenção para mostrar o valor deste objeto.
        System.out.println("informações de caloi: " + caloi.toString());

    } // Fim do método main
} // Fim da classe LearnJava


// Você pode incluir outras classes que não são públicas num arquivo .java


// Sintaxe de declaração de Classe.
// <public/private/protected> class <nome da classe>{
//   // atributos, construtores e todos os métodos.
//   // funções são chamadas de métodos em Java.
// }

class Bicicleta {

    // Atributos/Variáveis da classe Bicicleta.
    public int ritmo;          // Public: Pode ser acessada em qualquer lugar.
    private int velocidade;    // Private: Apenas acessível a classe.
    protected int catraca; // Protected: Acessível a classe e suas subclasses.
    String nome;               // default: Apenas acessível ao pacote.

    // Construtores são uma forma de criação de classes
    // Este é o construtor padrão.
    public Bicicleta() {
        catraca = 1;
        ritmo = 50;
        velocidade = 5;
        nome = "Bontrager";
    }

    // Este é um construtor específico (ele contém argumentos).
    public Bicicleta (int ritmoInicial, int velocidadeInicial, int catracaInicial, String nome) {
        this.catraca = catracaInicial;
        this.ritmo = ritmoInicial;
        this.velocidade = velocidadeInicial;
        this.nome = nome;
    }

    // Sintaxe de um método:
    // <public/private/protected> <tipo de retorno> <nome do método>(<args>) //  

    // Classes em Java costumam implementar métodos getters e setters para seus campos.

    // Sintaxe de declaração de métodos
    // <escopo> <tipo de retorno> <nome do método>(<args>) //   
    public int getRitmo() {
        return ritmo;
    }

    //  Métodos do tipo void não requerem declaração de retorno.
    public void setRitmo(int novoValor) {
        ritmo = novoValor;
    }

    public void setEquipamento(int novoValor) {
        catraca = novoValor;
    }

    public void aumentarVelocidade(int incremento) {
        velocidade += incremento;
    }

    public void diminuirVelocidade(int decremento) {
        velocidade -= decremento;
    }

    public void setNome(String novoNome) {
        nome = novoNome;
    }

    public String getNome() {
        return nome; // retorna um dado do tipo String.
    }

    //Método para mostrar os valores dos atributos deste objeto.
    @Override
    public String toString() {
        return "catraca: " + catraca +
                " ritmo: " + ritmo +
                " velocidade: " + velocidade +
                " nome: " + nome;
    }
} // fim classe Bicicleta

// Velocipede é uma subclasse de bicicleta.
class Velocipede extends Bicicleta {
    // (Velocípedes são bicicletas com rodas dianteiras grandes
    // Elas não possuem catraca.)

    public Velocipede(int ritmoInicial, int velocidadeInicial){
        // Chame o construtor do pai (construtor de Bicicleta) com o comando super.
        super(ritmoInicial, velocidadeInicial, 0, "PennyFarthing");
    }

    // Você pode marcar um método que você está substituindo com uma @annotation
    // Para aprender mais sobre o que são as annotations e sua finalidade
    // dê uma olhada em: http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setEquipamento(int catraca) {
        catraca = 0;
    }

}

// Interfaces
// Sintaxe de declaração de Interface
// <nível de acesso> interface <nome-da-interface> extends <super-interfaces> {
// // Constantes
// // Declarações de método
//}

// Exemplo - Comida:
public interface Comestivel {
    public void comer(); // Qualquer classe que implementa essa interface, deve
                         // implementar este método.
}

public interface Digestivel {
    public void digerir();
    // Em Java 8, interfaces podem ter métodos default.
    // public void digerir() {
    //     System.out.println("digerindo ...");
    // }
}


// Agora podemos criar uma classe que implementa ambas as interfaces.
public class Fruta implements Comestivel, Digestivel {

    @Override
    public void comer() {
        // ...
    }

    @Override
    public void digerir() {
        // ...
    }
}

// Em Java, você pode estender somente uma classe, mas você pode implementar muitas
// interfaces. Por exemplo:
public class ClasseExemplo extends ExemploClassePai implements InterfaceUm,
    InterfaceDois {

    @Override
    public void InterfaceUmMetodo() {
    }

    @Override
    public void InterfaceDoisMetodo() {
    }

}

// Classes abstratas

// Sintaxe de declaração de classe abstrata
// <Nível de acesso> abstract <nome-da-classe-abstrata> extends <estende super-abstratas-classes> {
// // Constantes e variáveis
// // Declarações de método
//}

// Marcar uma classe como abstrata significa que ela contém métodos abstratos que devem
// ser definidos em uma classe filha. Semelhante às interfaces, classes abstratas não podem
// ser instanciadas, ao invés disso devem ser estendidas e os métodos abstratos
// definidos. Diferente de interfaces, classes abstratas podem conter uma mistura de
// métodos concretos e abstratos. Métodos em uma interface não podem ter um corpo,
// a menos que o método seja estático, e as variáveis sejam finais, por padrão, ao contrário de um
// classe abstrata. Classes abstratas também PODEM ter o método "main".

public abstract class Animal
{
    public abstract void fazerSom();

    // Método pode ter um corpo
    public void comer()
    {
        System.out.println("Eu sou um animal e estou comendo.");  
        //Nota: Nós podemos acessar variáveis privadas aqui.
        idade = 30;
    }

    // Não há necessidade de inicializar, no entanto, em uma interface
    // a variável é implicitamente final e, portanto, tem
    // de ser inicializada.
    protected int idade;

    public void mostrarIdade()
    {
        System.out.println(idade);  
    }

    //Classes abstratas podem ter o método main.
    public static void main(String[] args)
    {
        System.out.println("Eu sou abstrata");
    }
}

class Cachorro extends Animal
{

    // Nota: ainda precisamos substituir os métodos abstratos na
    // classe abstrata.
    @Override
    public void fazerSom()
    {
        System.out.println("Bark");
        // idade = 30;    ==> ERRO!  idade é privada de Animal
    }

    // NOTA: Você receberá um erro se usou a
    // anotação Override aqui, uma vez que java não permite
    // sobrescrita de métodos estáticos.
    // O que está acontecendo aqui é chamado de "esconder o método".
    // Vejá também este impressionante SO post: http://stackoverflow.com/questions/16313649/
    public static void main(String[] args)
    {
        Cachorro pluto = new Cachorro();
        pluto.fazerSom();
        pluto.comer();
        pluto.mostrarIdade();
    }
}

// Classes Finais

// Sintaxe de declaração de classe final
// <nível de acesso> final <nome-da-classe-final> {
// // Constantes e variáveis
// // Declarações de método
//}

// Classes finais são classes que não podem ser herdadas e são, portanto, um
// filho final. De certa forma, as classes finais são o oposto de classes abstratas,
// porque classes abstratas devem ser estendidas, mas as classes finais não podem ser
// estendidas.
public final class TigreDenteDeSabre extends Animal
{
    // Nota: Ainda precisamos substituir os métodos abstratos na
    // classe abstrata.
    @Override
    public void fazerSom();
    {
        System.out.println("Roar");
    }
}

// Métodos Finais
public abstract class Mamifero()
{
    // Sintaxe de Métodos Finais:
    // <modificador-de-acesso> final <tipo-de-retorno> <nome-do-método>(<argumentos>)

    // Métodos finais, como classes finais, não podem ser substituídos por uma classe filha,
    // e são, portanto, a implementação final do método.
    public final boolean EImpulsivo()
    {
        return true;
    }
}


// Tipo Enum
//
// Um tipo enum é um tipo de dado especial que permite a uma variável ser um conjunto de constantes predefinidas. A 
// variável deve ser igual a um dos valores que foram previamente definidos para ela.
// Por serem constantes, os nomes dos campos de um tipo de enumeração estão em letras maiúsculas.
// Na linguagem de programação Java, você define um tipo de enumeração usando a palavra-chave enum. Por exemplo, você poderia
// especificar um tipo de enum dias-da-semana como:

public enum Dia {
    DOMINGO, SEGUNDA, TERÇA, QUARTA,
    QUINTA, SEXTA, SABADO 
}

// Nós podemos usar nosso enum Dia assim:

public class EnumTeste {

    // Variável Enum
    Dia dia;

    public EnumTeste(Dia dia) {
        this.dia = dia;
    }

    public void digaComoE() {
        switch (dia) {
            case SEGUNDA:
                System.out.println("Segundas são ruins.");
                break;

            case SEXTA:
                System.out.println("Sextas são melhores.");
                break;

            case SABADO: 
            case DOMINGO:
                System.out.println("Finais de semana são os melhores.");
                break;

            default:
                System.out.println("Dias no meio da semana são mais ou menos.");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTeste primeiroDia = new EnumTeste(Dia.SEGUNDA);
        primeiroDia.digaComoE(); // => Segundas-feiras são ruins.
        EnumTeste terceiroDia = new EnumTeste(Dia.QUARTA);
        terceiroDia.digaComoE(); // => Dias no meio da semana são mais ou menos.
    }
}

// Tipos Enum são muito mais poderosos do que nós mostramos acima.
// O corpo de um enum pode incluir métodos e outros campos.
// Você pode ver mais em https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

```

## Leitura Recomendada

Os links fornecidos aqui abaixo são apenas para ter uma compreensão do tema, use o Google e encontre exemplos específicos.

Outros tópicos para pesquisar:

* [Tutorial Java para Sun Trail / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Modificadores de acesso do Java](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Coceitos de Programação Orientada à Objetos](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Herança](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Polimorfismo](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Abstração](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Exceções](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Interfaces](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Tipos Genéricos](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Conversões de código Java](http://www.oracle.com/technetwork/java/codeconv-138413.html)

Livros:

* [Use a cabeça, Java] (http://www.headfirstlabs.com/books/hfjava/)

Apostila:

* [Java e Orientação a Objetos] (http://www.caelum.com.br/apostila-java-orientacao-objetos/)

* [Java para Desenvolvimento Web] (https://www.caelum.com.br/apostila-java-web/)
---
language: javascript
filename: javascript-pt.js
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
translators:
    - ["Willian Justen", "http://willianjusten.com.br"]
lang: pt-br
---

JavaScript foi criada por Brendan Eich, funcionário da Netscape na época, em 1995. Ela
foi originalmente criada para ser uma linguagem de script para websites,
complementando o uso de Java para aplicações web mais complexas, mas a sua
integração com páginas web e seu suporte nativo nos browsers fez com que
ela se tornasse mais comum que Java no frontend web.

Javascript não é somente limitada a browsers web, existindo o Node.js,
que é um projeto que fornece um interpretador baseado no motor V8 do Google 
Chrome e está se tornando cada vez mais famoso.

Feedback são muito apreciados! Você me encontrar em
[@adambrenecki](https://twitter.com/adambrenecki), ou
[adam@brenecki.id.au](mailto:adam@brenecki.id.au).

```js
// Comentários são como em C. Comentários de uma linha começam com duas barras,
/* e comentários de múltplas linhas começam com barra-asterisco
   e fecham com asterisco-barra */

// comandos podem ser terminados com  ;
facaAlgo();

// ... mas eles não precisam ser, o ponto-e-vírgula é automaticamente
// inserido quando há uma nova linha, exceto alguns casos.
facaAlgo()

// Como esses casos podem causar resultados inesperados, vamos continuar 
// a usar ponto-e-vírgula neste guia.

///////////////////////////////////
// 1. Números, Strings e Operadores

// Javascript tem um tipo de número (que é o 64-bit IEEE 754 double).
// Doubles tem uma mantissa 52-bit, que é suficiente para guardar inteiros
// acima de 9✕10¹⁵ precisamente.
3; // = 3
1.5; // = 1.5

// A aritmética básica funciona como seria de se esperar.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// Inclusive divisão desigual.
5 / 2; // = 2.5

// Operadores Bitwise também funcionam; quando você faz uma operação bitwise
// seu float é convertido para um int de até 32 bits.
1 << 2; // = 4

// A precedência é aplicada com parênteses.
(1 + 3) * 2; // = 8

// Existem três especiais valores não-é-número-real:
Infinity; // resultado de 1/0
-Infinity; // resultado de -1/0
NaN; // resultado de 0/0

// Existe também o tipo booleano.
true;
false;

// Strings são criados com ' ou ".
'abc';
"Olá, mundo";

// Negação usa o símbolo !
!true; // = false
!false; // = true

// Igualdade é o sinal de ===
1 === 1; // = true
2 === 1; // = false

// Desigualdade é o sinal de !==
1 !== 1; // = false
2 !== 1; // = true

// Mais comparações
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Strings são concatenadas com +
"Olá " + "mundo!"; // = "Olá mundo!"

// e comparadas com < e >
"a" < "b"; // = true

// A comparação de tipos não é feita com o uso de ==...
"5" == 5; // = true
null == undefined; // = true

// ...a menos que use ===
"5" === 5; // = false
null === undefined; // = false 

// ...isso pode resultar em comportamentos estranhos...
13 + !0; // 14
"13" + !0; // '13true'

// Você pode acessar caracteres de uma String usando o `charAt`
"Isto é uma String".charAt(0);  // = 'I'

// ...ou usar `substring` para pegar pedaços maiores.
"Olá mundo".substring(0, 3); // = "Olá"

// `length` é uma propriedade, portanto não use ().
"Olá".length; // = 3

// Existe também o `null` e o `undefined`.
null;      // usado para indicar um valor não considerado
undefined; // usado para indicar um valor que não é a atualmente definido
           // (entretando `undefined` é considerado de fato um valor

// false, null, undefined, NaN, 0 and "" são valores falsos;
// qualquer outro valor é verdadeiro
// Note que 0 é falso e "0" é verdadeiro, até mesmo 0 == "0".

///////////////////////////////////
// 2. Variáveis, Arrays e Objetos

// Variáveis são declaradas com a palavra-chave `var`. O Javascript é 
// dinâmicamente tipado, portanto você não precisa especificar o tipo.
// Atribuições usam um simples caracter de `=`.
var someVar = 5;

// se você deixar de colocar a palavra-chave var, você não irá receber um erro...
someOtherVar = 10;

// ...mas sua variável será criada no escopo global, não no escopo em que você
// definiu ela.

// Variáveis declaradas sem receberem um valor são definidas como `undefined`.
var someThirdVar; // = undefined

// Existe um shorthand para operações matemáticas em variáveis:
someVar += 5; // equivalente a someVar = someVar + 5; someVar é 10 agora
someVar *= 10; // agora someVar é 100

// e um para adição e subtração de 1
someVar++; // agora someVar é 101
someVar--; // volta para 100

// Arrays são listas ordenadas de valores, de qualquer tipo.
var myArray = ["Olá", 45, true];

// Seus membros podem ser acessados usando a sintaxe de colchetes.
// O indíce de um Array começa pelo 0.
myArray[1]; // = 45

// Arrays são mutáveis e de tamanho variável.
myArray.push("World");
myArray.length; // = 4

// Adicionar/modificar em um índice específico
myArray[3] = "Hello";

// Objetos de Javascript são equivalentes aos dicionários ou maps de outras
// linguagens: uma coleção não ordenada de pares chave-valor.
var myObj = {chave1: "Olá", chave2: "Mundo"};

// Chaves são strings, mas as aspas não são necessárias se elas são
// identificadores válidos no Javascript. Valores podem ser de qualquer tipo.
var myObj = {myKey: "myValue", "my other key": 4};

// Atributos de objetos também podem ser acessados com a sintaxe de colchetes.
myObj["my other key"]; // = 4

// ... ou usando a sintaxe de ponto, passando a chave que é um identificador
// válido.
myObj.myKey; // = "myValue"

// Objetos são mutáveis, valores podem ser modificados e novas chaves 
// adicionadas.
myObj.myThirdKey = true;

// Se você tentar acessar um valor que não foi determinado ainda, você irá
// receber `undefined`.
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. Lógica e Estruturas de Controle

// A sintaxe para essa seção é quase idêntica a maioria das linguagens.

// A estrutura `if` funciona como deveria ser.
var count = 1
if (count == 3){
    // executa se count é 3
} else if (count == 4){
    // executa se count é 4
} else {
    // executa se count não é 3 nem 4
}

// Como se faz um `while`.
while (true){
    // Um loop infinito!
}

// Os loops do-while são como os loops de while, exceto quando eles sempre
// executam pelo menos uma vez.
do {
    input = getInput();
} while (!isValid(input))

// O loop `for` é o mesmo de C e Java:
// inicialização, condição para continuar; iteração
for (var i = 0; i < 5; i++){
    // vai rodar cinco vezes
}

// && é o `e` lógico , || é o `ou` lógico
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear";
}
if (cor == "red" || cor == "blue"){
    // cor é vermelha OU azul
}

// && e || "pequeno circuito", é útil para determinar valores padrões.
var name = otherName || "padrão";

// O `switch` checa pela igualdade com `===`.
// Use `break` após cada `case`
grade = 'B';
switch (grade) {
  case 'A':
    console.log("Great job");
    break;
  case 'B':
    console.log("OK job");
    break;
  case 'C':
    console.log("You can do better");
    break;
  default:
    console.log("Oy vey");
    break;
}


///////////////////////////////////
// 4. Funções, Escopos e Closures

// Funções Javascript são declaradas com a palavra-chave `function`.
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"

// Repare que o valor a ser retornado deve começar na mesma linha que
// a palavra-chave `return`, senão você sempre irá retornar `undefined` 
// visto que o ponto-e-vírgula é inserido automáticamente nas quebras de 
// linha. Preste atenção quando usar o estilo Allman.
function myFunction()
{
    return // <- ponto-e-vírgula adicionado automaticamente aqui
    {
        thisIsAn: 'object literal'
    }
}
myFunction(); // = undefined

// Funções Javascript são objetos de primeira classe, portanto elas podem
// ser atribuídas a nomes de variáveis e serem passadas para outras funções
// como argumentos - por exemplo, quando criamos um manipulador de eventos:
function myFunction(){
    // este código será chamado em 5 segundos
}
setTimeout(myFunction, 5000);
// Nota: `setTimeout` não é parte da linguagem Javascript, mas é provido pelos
// browsers e o Node.js.

// Objetos de funções não precisam nem serem declarados com nome - você pode 
// escrever a definição de uma função anônima diretamente nos argumentos de 
// outra função.
setTimeout(function(){
    // este código será chamado em 5 segundos
}, 5000);

// O Javascript tem escopo de função; as funções tem seu próprio escopo, 
// mas outros blocos não.
if (true){
    var i = 5;
}
i; // = 5 - não `undefined` como você esperaria numa linguagem de blogo-escopo

// Isso levou a padrão comum chamado de IIFE (Imediately Invoked Function 
// Expression) ou (Expressão de Função Invocada Imediatamente), que previne
// que variáveis temporárias vazem para o escopo global.
(function(){
    var temporary = 5;
    // Nós podemos acessar o escopo global definindo o "objeto global", que
    // no browser vai ser sempre `window`. O objeto global pode ter um nome
    // diferente para ambiente não-browser como o Node.js.
    window.permanent = 10;
})();
temporary; // levanta um erro de referência inexiste
permanent; // = 10

// Uma das principais características do Javascript é a closure. Que é
// uma função definida dentro de outra função, a função interna pode acessar
// todas as variáveis da função externa, mesmo depois da função de fora
// finalizar sua execução.
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!";

    // Funções internas são colocadas no escopo local por padrão, assim como
    // se fossem declaradas com `var`. 
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // `setTimeout` é assíncrono, portanto a função `sayHelloInFiveSeconds`
    // vai sair imediatamente, e o `setTimeout` irá chamar a interna depois.
    // Entretanto. como a interna é fechada dentro de "sayHelloInFiveSeconds",
    // a interna permanece podendo acessar a variável `prompt` quando depois
    // de chamada.
}
sayHelloInFiveSeconds("Adam"); // Vai abrir um popup com "Hello, Adam!" em 5s

///////////////////////////////////
// 5. Mais sobre Objetos; Construtores e Prototypes

// Objetos podem conter funções.
var myObj = {
    myFunc: function(){
        return "Olá mundo!";
    }
};
myObj.myFunc(); // = "Olá mundo!"

// Quando uma função ligada a um objeto é chamada, ela pode acessar o objeto
// da qual foi ligada usando a palavra-chave `this`. 
myObj = {
    myString: "Olá mundo!",
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = "Olá mundo!"

// O `this` só funciona para dentro do escopo do objeto, portanto, se chamarmos 
// um método do objeto fora de seu escopo, este não irá funcionar.
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// Inversamente, uma função pode ser atribuída a um objeto e ganhar a acesso
// através do `this`, até mesmo se ela não for chamada quando foi definida.
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "OLÁ MUNDO!"

// Nós podemos também especificar um contexto onde a função irá executar, 
// usando o `call` ou `apply`.

var anotherFunc = function(s){
    return this.myString + s;
}
anotherFunc.call(myObj, " E Olá Lua!"); // = "Olá mundo! E Olá Lua!"

// A função `apply` é praticamente a mesma coisa,  mas ela pega um array
// como lista de argumentos.

anotherFunc.apply(myObj, [" E Olá Sol!"]); // = "Olá mundo! E Olá Sol!"

// Isto é util quando trabalhamos com uma função que aceita uma sequência de
// argumentos e você quer passar um array.

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// Mas, o `call` e `apply` são somente temporários. Quando você quiser que 
// permaneça sempre no escopo, use `bind`.

var boundFunc = anotherFunc.bind(myObj);
boundFunc(" E Olá Saturno!"); // = "Olá mundo! E Olá Saturno!"

// `bind` também pode ser usado para parcialmente aplicar (curry) uma função.

var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// Quando você invoca uma função com a palavra-chave `new`, um novo objeto
// é criado, e fica disponível para a função pela palavra-chave `this`. 
// Funções são desenhadas para serem invocadas como se invocam os construtores.

var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// Todo objeto JavaScript possui um `prototype`. Quando você tenta acessar 
// uma propriedade de um objeto que não existe no objeto atual, o interpretador
// vai olhar imediatamente para o seu prototype.

// Algumas implementações em JS deixam você acessar o objeto prototype com a 
// propriedade mágica `__proto__`. Enquanto isso é util para explicar 
// prototypes, não é parte de um padrão; nós vamos falar de algumas formas de 
// usar prototypes depois.

var myObj = {
    myString: "Olá Mundo!"
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// Isto funciona para funções, também.
myObj.myFunc(); // = "olá mundo!"

// É claro, se sua propriedade não está em seu prototype, 
// o prototype do prototype será procurado e por aí vai.
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

// Não há cópia envolvida aqui; cada objeto guarda uma referência do
// prototype. Isso significa que podemos alterar o prototype e nossas mudanças
// serão refletidas em qualquer lugar.
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43


// Nós mencionamos que o `__proto__` não é uma forma padrão, e não há uma 
// forma padrão de mudar o prototype de um objeto já existente. Entretanto, 
// existem duas formas de se criar um objeto com um dado prototype.

// A primeira forma é `Object.create`, que é uma adição recente do JS,
// e ainda não está disponível em todas as implementações.
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// A segunda forma, que funciona em qualquer lugar, é feita com construtores.
// Construtores tem uma propriedade chamada prototype. Este *não* é o prototype
// do construtor em si; ao invés disso, ele é o prototype dos novos objetos
// criados pelo construtor.
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function(){
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// Tipos originais da linguagem como strings e números também possuem
// construtores equivalentes. 
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// Exceto, que eles não são totalmente equivalentes.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // O código não vai executar, porque 0 é um valor falso.
}

// Entretanto, esses objetos encapsulados e as funções originais compartilham
// um mesmo prototype, portanto você pode adicionar funcionalidades a uma string,
// por exemplo.
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// Esse fato é usado para criar os chamados `polyfills`, que implementam
// uma nova característica do Javascript em uma versão mais velha, para que
// assim funcionem em ambientes mais velhos como browsers ultrapassados.

// Havíamos mencionado que `Object.create` não estava ainda disponível em 
// todos as implementações, mas nós podemos usá-lo com esse polyfill:
if (Object.create === undefined){ // Não o sobrescreve se já existir
    Object.create = function(proto){
        // faz um construtor temporário com o prototype certo
        var Constructor = function(){};
        Constructor.prototype = proto;
        // então utiliza o new para criar um objeto prototype apropriado
        return new Constructor();
    }
}
```

## Leitura Adicional

O [Mozilla Developer
Network](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript) dispõe de uma
excelente documentação sobre Javascript e seu uso nos browsers. E mais, 
é uma wiki, portanto conforme você vai aprendendo, mais você pode ir ajudando
os outros compartilhando do seu conhecimento.

[Uma re-introdução do JavaScript pela MDN]
(https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
cobre muito dos conceitos abordados aqui em mais detalhes. Este guia fala
somente sobre a linguagem JavaScript em si; se você quiser aprender mais
sobre e como usar o JavaScript em páginas na web, comece aprendendo sobre
[Document Object
Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)

[Aprenda Javascript por Exemplos e com Desafios](http://www.learneroo.com/modules/64/nodes/350) é uma
variação desse guia com desafios.

[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) é um guia 
profundo de todas as partes do JavaScript.

[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) é o guia clássico
/ livro de referência. 

Parte desse artigo foi adaptado do tutorial de Python do Louie Dinh que está
nesse site e do [Tutorial de JS](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
da Mozilla Developer Network.
---
category: tool
tool: jquery
contributors:
    - ["Sawyer Charles", "https://github.com/xssc"]
translators:
    - ["Nikolas Silva", "https://github.com/nikolassilva"]
filename: jquery-pt.js
lang: pt-br
---

jQuery é uma biblioteca JavaScript que te ajuda a "fazer mais, escrevendo menos". Ela faz com que muitas tarefas comuns em JavaScript sejam mais simples de escrever. jQuery é usado por grandes empresas e desenvolvedores do mundo todo. Ela torna o AJAX, manipulação de eventos, manipulação do DOM, entre outros, mais fácil e rápido.

Pelo jQuery ser uma biblioteca JavaScript você deve [aprende-lo primeiro](https://learnxinyminutes.com/docs/pt-br/javascript-pt/)

```js


///////////////////////////////////
// 1. Seletores

// Seletores no jQuery são usados para selecionar um elemento
var page = $(window); // Seleciona toda a viewport

// Seletores também podem ser na forma do CSS
var paragraph = $('p'); // Seleciona todos elementos de parágrafo
var table1 = $('#table1'); // Seleciona o elemento com id 'table1'
var squares = $('.square'); // Seleciona todos elementos com classe 'square'
var square_p = $('p.square') // Seleciona todos elementos de parágrafo com a classe 'square'


///////////////////////////////////
// 2. Eventos e Efeitos
// jQuery é muito bom em manipular o que acontece quando um evento é disparado
// Um evento muito usado é o 'ready'  
// Você pode usar o método ready para esperar até que um elemento tenha terminado de carregar
$(document).ready(function(){
  // O código não será executado até que o documento carregue
});
// Você também pode usar funções declaradas
function onAction() {
  // Isso será executado quando um evento for disparado
}
$('#btn').click(onAction); // Chama 'onAction' quando o elemento receber um clique

// Outros eventos comuns são:
$('#btn').dblclick(onAction); // Clique duplo
$('#btn').hover(onAction); // Mouse sobre elemento
$('#btn').focus(onAction); // Elemento recebe foco
$('#btn').blur(onAction); // Elemento perde foco
$('#btn').submit(onAction); // Envio de formulário
$('#btn').select(onAction); // Quando o elemento é selecionado
$('#btn').keydown(onAction); // Quando uma tecla é segurada
$('#btn').keyup(onAction); // Quando uma tecla é solta
$('#btn').keypress(onAction); // Quando uma tecla é pressionada
$('#btn').mousemove(onAction); // Quando o mouse é movido
$('#btn').mouseenter(onAction); // Quando o mouse entra no elemento
$('#btn').mouseleave(onAction); // Quando o mouse sai do elemento


// Eles também podem disparar os eventos em vez de manipulá-los,
// simplesmente deixando de passar os parâmetros
$('#btn').dblclick(); // Dispara um clique duplo no elemento

// Você pode manipular múltiplos eventos usando o seletor apenas uma vez
$('#btn').on(
  {dblclick: myFunction1} // Disparado num clique duplo
  {blur: myFunction1} // Disparado quando perder o foco
);

// Você pode mover e esconder elementos com alguns métodos de efeito
$('.table').hide(); // Esconde o elemento

// Nota: chamar uma função nesse método ainda irá esconder o elemento
$('.table').hide(function(){
    // Elemento é escondido e a função é executada
});

// Você pode guardar seletores em variáveis
var tables = $('.table');

// Alguns métodos básicos de manipulação do DOM:
tables.hide(); // Esconde elemento(s)
tables.show(); // Exibe elemento(s)
tables.toggle(); // Alterna entre esconder/exibir
tables.fadeOut(); // Efeito fade out
tables.fadeIn(); // Efeito fade in
tables.fadeToggle(); // Alterna entre fade out/in
tables.fadeTo(0.5); // Efeito fade com opacidade específica (entre 0 e 1)
tables.slideUp(); // Efeito de deslize pra cima
tables.slideDown(); // Efeito de deslize pra baixo
tables.slideToggle(); // Alterna entre deslizar pra cima/baixo

// Todos os métodos acima levam velocidade (em milissegundos) e uma função callback
tables.hide(1000, myFunction); // Esconde o elemento em 1 segundo e chama a função

// No fadeTo é obrigatório definir a opacidade como segundo parâmetro
tables.fadeTo(2000, 0.1, myFunction); // 2 segundos de fade para 0.1 de opacidade e chama a função

// Você pode fazer animações mais avançadas com o método animate
tables.animate({'margin-top':"+=50", height: "100px"}, 500, myFunction);
// O método animate leva um objeto com valores CSS,
// um parâmetro de opções para melhorar a animação
// e uma função callback, como de costume

///////////////////////////////////
// 3. Manipulação

// São similares aos efeitos, mas podem fazer mais
$('div').addClass('taming-slim-20'); // Adiciona a classe taming-slim-20 em todas as divs

// Métodos comuns de manipulação
$('p').append('Hello world'); // Adiciona ao final do elemento
$('p').attr('class'); // Obtém o valor de um atributo
$('p').attr('class', 'content'); // Define o valor de um atributo
$('p').hasClass('taming-slim-20'); // Retorna true se tiver a classe
$('p').height(); // Obtém/define a altura do elemento


// Pra maioria dos métodos de manipulação, pegar o valor de um 
// elemento só afetará o primeiro deles
$('p').height(); // Obtém a altura da primeira tag 'p'

// Você pode usar o método each pra percorrer os elementos
var heights = [];
$('p').each(function() {
  heights.push($(this).height()); // Adiciona a altura das tags 'p' na array
});


```
---
language: json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["Francisco Marques", "https://github.com/ToFran"]
translators:
  - ["Miguel Araújo", "https://github.com/miguelarauj1o"]
lang: pt-br
filename: learnjson-pt.json
---

Como JSON é um formato de intercâmbio de dados, este será, muito provavelmente, o
"Learn X in Y minutes" mais simples existente.

JSON na sua forma mais pura não tem comentários, mas a maioria dos analisadores 
aceitarão comentários no estilo C (//, /\* \*/). No entanto estes devem ser evitados para otimizar a compatibilidade.

Um valor JSON pode ser um numero, uma string, um array, um objeto, um booleano (true, false) ou null.

Os browsers suportados são: Firefox 3.5+, Internet Explorer 8.0+, Chrome 1.0+, Opera 10.0+, e Safari 4.0+.

A extensão dos ficheiros JSON é “.json” e o tipo de mídia de Internet (MIME) é “application/json”.

Mais informação em: http://www.json.org/

```json
{
  "chave": "valor",
  
  "chaves": "deve ser sempre entre aspas (junto ou separado)",
  "números": 0,
  "strings": "Olá, mundo. Todo o padrão UNICODE é permitido, junto com \"escapando\".",
  "possui booleano?": true,
  "nada": null,

  "número grande": 1.2e+100,

  "objetos": {
    "comentário": "A maior parte da sua estrutura virá de objetos.",

    "array": [0, 1, 2, 3, "Arrays podem ter qualquer coisa em si.", 5],

    "outro objeto": {
      "comentário": "Estas coisas podem ser aninhadas, muito úteis."
    }
  },

  "tolice": [
    {
      "fonte de potássio": ["bananas"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "estilo alternativo": {
  "comentário": "verificar isso!"
  , "posição da vírgula": "não importa - enquanto é antes do valor, então é válido"
  , "outro comentário": "que bom"
  },

  "que foi curto": "E, você está feito. Você já sabe tudo que JSON tem para oferecer."
}
```
---
language: Julia
filename: learnjulia-pt.jl
contributors:
    - ["Leah Hanson", "http://leahhanson.us"]
translators:
    - ["Davidson Mizael", "https://github.com/davidsonmizael"]
lang: pt-br
---

Julia é uma linguagem homoiconic funcional focada na computação tecnica. Ao mesmo tempo que ela tem todo o poder dos homoiconic macros, funções de primeira classe, e controle de baixo nivel, Julia é tão facil para aprender e usar quanto Python.

Este tutorial é baseado no Julia 0.3.

```ruby
# Linhas únicas de comentários começam com o simbolo hash(jogo da velha).
#= Comentários de multiplas linhas podem ser escritos
   colocando '#=' antes do texto e '=#'
   após o texto. Eles também podem ser agrupados
=#

####################################################
## 1. Tipos primitivos e operadores
####################################################

# Tudo em Julia é uma expressão.

# Há muitos tipos básicos de numeros.
3 # => 3 (Int64)
3.2 # => 3.2 (Float64)
2 + 1im # => 2 + 1im (Complex{Int64})
2//3 # => 2//3 (Rational{Int64})

# Todos os operadores inseguros normais estão disponiveis.
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
5 / 2 # => 2.5 # dividir um Int por um Int resulta em um float
div(5, 2) # => 2 # para um restultado truncado, use div
5 \ 35 # => 7.0
2 ^ 2 # => 4 # elevado,não o opeardor binário xor
12 % 10 # => 2

# Impõe a priodidade nos parenteses
(1 + 3) * 2 # => 8

# Operadores binarios
~2 # => -3   # not
3 & 5 # => 1 # and
2 | 4 # => 6 # or
2 $ 4 # => 6 # xor
2 >>> 1 # => 1 # deslocamento lógico de bits a direita
2 >> 1  # => 1 # deslocamento aritmético de bits a direita
2 << 1  # => 4 # deslocamento lógico/aritmético de bits a esquerda

# Você pode usar a função bits para ver a representação binária de um numero.
bits(12345)
# => "0000000000000000000000000000000000000000000000000011000000111001"
bits(12345.0)
# => "0100000011001000000111001000000000000000000000000000000000000000"

# Valores booleanos são primitivos.
true
false

# Operadores booleanos
!true # => false
!false # => true
1 == 1 # => true
2 == 1 # => false
1 != 1 # => false
2 != 1 # => true
1 < 10 # => true
1 > 10 # => false
2 <= 2 # => true
2 >= 2 # => true
# Comparações podem ser encadeadas
1 < 2 < 3 # => true
2 < 3 < 2 # => false

# Strings são criadas com  "
"Isso é uma String."

# Caracteres literais são escritos com '
'a'

# Uma string pode ser indexada como um vetor de caracteres
"Isso é uma string"[1] # => 'I' # Julia começa a indexar a partir do 1
# Porém isso não funcionará direito com strings em UTF8,
# portanto é recomendado usar iterações sobre uma string (map, loops com for, etc). 

# $ pode ser usado para interpolação de string:
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
# Você pode usar qualquer expressão Julia dentro dos parenteses.

# Outro jeito de formatar strings é com um macro no printf.
@printf "%d é menor que %f" 4.5 5.3 # 5 é menor que 5.300000

# Escrever na tela é fácil
println("Eu sou Julia. Prazer em conhece-lo!")

####################################################
## 2. Variáveis e coleções
####################################################

#Você não declara variáveis antes de atribui-lás.
some_var = 5 # => 5
some_var # => 5

# Acessando a variável anterior não iniciada é um erro
try
    some_other_var # => ERROR: some_other_var não definida 
catch e
    println(e)
end

# Nomes de variáveis começam com uma letra.
# Depois disso, você pode usar letras, digitos, underscores e pontos de exclamação.
SomeOtherVar123! = 6 # => 6

# Você também pode usar caractéres unicode
☃ = 8 # => 8
# Estes são especialmente reservados para notações matemáticas.
2 * π # => 6.283185307179586

# Uma nota na convenção de nomes em Julia:
#
# * A separação de palavras pode ser feita por underscores ('_'), mas o uso
#   de underscore é desencorajado a menos que o nome da variável seja dificil
#   de ler.
#
# * Os nomes de tipos começam com letra maiúscula e a separação de letras é 
#   feita a partir de CamelCase no lugar de underscores.
#
# * Nomes de funções e macros são em minúsculo, sem underscore.
#
# * Funções que modificam a própria entrada tem nomes que terminam em !. Estas
#   funções são chamadas as vezes de funções de mutação ou função in-place.

# Vetores armazenam uma sequencia de valores indexados por integer de 1 a n:
a = Int64[] # => 0-element Int64 Array

# 1-Vetores dimensionais literais podem ter seus valores separados por virgula.
b = [4, 5, 6] # => 3-element Int64 Array: [4, 5, 6]
b[1] # => 4
b[end] # => 6

# 2-Vetores dimensionais usam espaço para separar valores e ponto e virgula para linhas.
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]

# Adiciona-se coisas ao final de uma lista com push! e append!
push!(a,1)     # => [1]
push!(a,2)     # => [1,2]
push!(a,4)     # => [1,2,4]
push!(a,3)     # => [1,2,4,3]
append!(a,b) # => [1,2,4,3,4,5,6]

# Remove-se do final com pop!
pop!(b)        # => 6 e 'b' agora é [4,5]

# Vamos coloca-lo de novo
push!(b,6)   # 'b' agora é [4,5,6] de novo.

a[1] # => 1 # lembre-se que Julia indexa a partir de 1, não 0.

# end é um atalho para a ultima posição. Pode ser usada em qualquer
# expressão indexada.
a[end] # => 6

# nós também temos shift e unshift
shift!(a) # => 1 e 'a' agora é [2,4,3,4,5,6]
unshift!(a,7) # => [7,2,4,3,4,5,6]

# Funções que terminam com ponto de exclamação indicam que elas modificam
# seus argumentos.
arr = [5,4,6] # => 3-element Int64 Array: [5,4,6]
sort(arr) # => [4,5,6]; 'arr' continua [5,4,6]
sort!(arr) # => [4,5,6]; 'arr' agora é [4,5,6]

# Olhar além dos limites é um BoundsError
try
    a[0] # => ERROR: BoundsError() in getindex at array.jl:270
    a[end+1] # => ERROR: BoundsError() in getindex at array.jl:270
catch e
    println(e)
end

# Erros listam a linha e o nome do arquivo que ele está, mesmo se for uma 
# biblioteca padrão. Se você construiu Julia pelo source, você pode olhar na 
# pasta base dentro da pasta do Julia para encontrar esses arquivos.

# Você pode inicializar vetores com limites
a = [1:5;] # => 5-element Int64 Array: [1,2,3,4,5]

# Você pode ver até um limite com a sintaxe separada
a[1:3] # => [1, 2, 3]
a[2:end] # => [2, 3, 4, 5]

# Remova elementos de um array pelo index com splice!
arr = [3,4,5]
splice!(arr,2) # => 4 ; arr is now [3,5]

# Concatene listas com append!
b = [1,2,3]
append!(a,b) # 'a' agora é [1, 2, 3, 4, 5, 1, 2, 3]

# Cheque se um valor existe me uma lista com in
in(1, a) # => true

# Veja o tamanho com lenght
length(a) # => 8

# Tuples não podem ser mudados.
tup = (1, 2, 3) # => (1,2,3) # um tuple (Int64,Int64,Int64).
tup[1] # => 1
try:
    tup[1] = 3 # => ERROR: não há metodo setindex!((Int64,Int64,Int64),Int64,Int64)
catch e
    println(e)
end

# Muitas litas de funções também trabalham com tuples
length(tup) # => 3
tup[1:2] # => (1,2)
in(2, tup) # => true

#Você pode desempacotar tuples para variáveis.
a, b, c = (1, 2, 3) # => (1,2,3)  # 'a' agora é 1, 'b' agora é 2 e 'c' agora é 3

# Tuplas são criados mesmo se você deixar fora dos parenteses
d, e, f = 4, 5, 6 # => (4,5,6)

# Uma tupla de um elemento é diferente do valor que ele contém
(1,) == 1 # => false
(1) == 1 # => true

# Olhe como é facil pra trocar dois valores
e, d = d, e  # => (5,4) # 'd' agora é 5 e 'e' agora é 4

# Dicionários armazenam mapeamentos
empty_dict = Dict() # => Dict{Any,Any}()

# Você pode criar um dicionário usando um literal
filled_dict = ["one"=> 1, "two"=> 2, "three"=> 3]
# => Dict{ASCIIString,Int64}

# Veja os valores com []
filled_dict["one"] # => 1

# Pegue todas as chaves
keys(filled_dict)
# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# Nota - as chaves dos dicionários não são ordenadas nem estão na ordem que você as inseriu.

# Pegue todos os valores
values(filled_dict)
# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# Nota - A mesma coisa que na nota acima sobre a ordenação das chaves.

# Cheque pela existencia de chaves em um dicionário com in e haskey
in(("one", 1), filled_dict) # => true
in(("two", 3), filled_dict) # => false
haskey(filled_dict, "one") # => true
haskey(filled_dict, 1) # => false

# Procurar por uma chave não existente irá gerar um erro
try
    filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489
catch e
    println(e)
end

# Use o método get para escapar desse erro passando um valor padrão
# get(dictionary,key,default_value)
get(filled_dict,"one",4) # => 1
get(filled_dict,"four",4) # => 4

# Use sets para representar coleções de valores unicos e não ordenados
empty_set = Set() # => Set{Any}()
# Inicialize um set com valores
filled_set = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4)

# Adicione mais valores para um set
push!(filled_set,5) # => Set{Int64}(5,4,2,3,1)

# Cheque se um valor está no set
in(2, filled_set) # => true
in(10, filled_set) # => false

# Não há funções para interseção de set, união e diferença.
other_set = Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3)
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4)

####################################################
## 3. Controle de fluxo
####################################################

# Vamos fazer uma variável
some_var = 5

# Aqui está um if. Identação nao é importante em Julia.
if some_var > 10
    println("some_var é totalmente maior que 10.")
elseif some_var < 10    # Essa clausula elseif é opcional.
    println("some_var é menor que 10.")
else                    # A clausula else é opcional também.
    println("some_var é literalmente 10.")
end
# => exibe "some_var é menor que 10"

# Loops for repetem sobre variaveis iteráveis.
# Tipos iterativos incluem Range, Array, set Dict e String.
for animal=["dog", "cat", "mouse"]
    println("$animal is a mammal")
	# Você pode interpolar variáveis usando $ ou expressões em strings
end
# exibe:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# Você pode usar 'in' no lugar de '='.
for animal in ["dog", "cat", "mouse"]
    println("$animal is a mammal")
end
# exibe:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$(a[1]) is a $(a[2])")
end
# exibe:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$k is a $v")
end
# exibe:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# Loops while circulam enquanto a condição é true
x = 0
while x < 4
    println(x)
    x += 1  # Abreveação para x = x + 1
end
# exibe:
#   0
#   1
#   2
#   3

# Trate exceções com um bloco try/catch
try
   error("help")
catch e
   println("caught it $e")
end
# => caught it ErrorException("help")


####################################################
## 4. Funções
####################################################

# A palavra chave 'function' cria novas funções
#function name(arglist)
#  corpo...
#end
function add(x, y)
    println("x is $x and y is $y")

	# Funções retornam o valor da sua ultima declaração
t    x + y
end

add(5, 6) # => 11 after printing out "x is 5 and y is 6"

# Você pode definir funções que tomam um numero incerto de
# argumentos 
function varargs(args...)
    return args
    # use a palavra chave return para retornar um valor em qualquer parte da função
end
# => varargs (generic function with 1 method)

varargs(1,2,3) # => (1,2,3)

# O ... é chamado de splat.
# Nós apenas o usamos na definição de uma função.
# Também pode ser usado na chamada de uma função,
# onde ela vai abrir um Array ou o conteúdo de um Tuple na lista de argumentos.
Set([1,2,3])    # => Set{Array{Int64,1}}([1,2,3]) # produz um Set de Arrays
Set([1,2,3]...) # => Set{Int64}(1,2,3) # isso é equivalente a Set(1,2,3)

x = (1,2,3)     # => (1,2,3)
Set(x)          # => Set{(Int64,Int64,Int64)}((1,2,3)) # um Set de Tuples
Set(x...)       # => Set{Int64}(2,3,1)

# Você pode definir funções com argumentos posicionais opcionais.
function defaults(a,b,x=5,y=6)
    return "$a $b and $x $y"
end

defaults('h','g') # => "h g and 5 6"
defaults('h','g','j') # => "h g and j 6"
defaults('h','g','j','k') # => "h g and j k"
try
    defaults('h') # => ERROR: no method defaults(Char,)
    defaults() # => ERROR: no methods defaults()
catch e
    println(e)
end

# Você pode definir funções que tomam argumentos como palavras chaves
function keyword_args(;k1=4,name2="hello") # note the ;
    return ["k1"=>k1,"name2"=>name2]
end

keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4]
keyword_args(k1="mine") # => ["k1"=>"mine","name2"=>"hello"]
keyword_args() # => ["name2"=>"hello","k1"=>4]

# Você pode combinar todos os tipos de argumentos em uma só função
function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo")
    println("normal arg: $normal_arg")
    println("optional arg: $optional_positional_arg")
    println("keyword arg: $keyword_arg")
end

all_the_args(1, 3, keyword_arg=4)
# exibe:
#   normal arg: 1
#   optional arg: 3
#   keyword arg: 4

# Julia tem funções de primeira classe
function create_adder(x)
    adder = function (y)
        return x + y
    end
    return adder
end

# Isso é "sintexe furiosa de lambda" pra criar funções anônimas.
(x -> x > 2)(3) # => true

#Esta função é identica a implementação da create_adder acima.
function create_adder(x)
    y -> x + y
end

# Você também pode nomear funções internas, se você quiser
function create_adder(x)
    function adder(y)
        x + y
    end
    adder
end

add_10 = create_adder(10)
add_10(3) # => 13


# Há 
# There are built-in higher order functions
map(add_10, [1,2,3]) # => [11, 12, 13]
filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7]

# Nós podemos usar listas de compreensão para melhores mapeamentos
[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13]
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]

####################################################
## 5. Tipos
####################################################

#Julia tem um sistema de tipos.
# Todo valor tem um tipo. Variaveis não tem tipos próprios.
# Você pode usar a função 'typeof' para pegar o valor.
typeof(5) # => Int64

# Tipos são valores de primeira classe.
typeof(Int64) # => DataType
typeof(DataType) # => DataType
# DataType é o tipo que representa tipos, incluindo ele mesmo.

# Tipos são usados para documentação, optimização e envio
# Eles não são estaticamente checados.

# Usuários podem definir tipos
# Eles são como records ou structs em outras linguagens.
# Novos tipos são definidos usando a palavra chave 'type'

# type Name
#   field::OptionalType
#   ...
# end
type Tiger
  taillength::Float64
  coatcolor # não incluindo uma notação type é o mesmo que '::Any'
end

# Os argumentos padrões de um construtor são as propriedades
# do tipo na ordem que eles são listados na definição.
tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange")

# O tipo double como construtor de função para valores desse tipo
# The type doubles as the constructor function for values of that type
sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire")

# Esses tipos no estilo struct são chamados tipos concretos
# Eles podem ser instanciados, mas não podem ter subtipos.
# O outro tipo de tipos são os tipos abstratos.

# abstract Name
abstract Cat # apenas um nome e um ponto na hierarquia de tipo

# Tipos abstratos podem ser instanciados, mas não podem ter subtipos.
# Por exemplo, Number é um tipo abstrato
subtypes(Number) # => 6-element Array{Any,1}:
                 #     Complex{Float16}
                 #     Complex{Float32}
                 #     Complex{Float64}
                 #     Complex{T<:Real}
                 #     ImaginaryUnit
                 #     Real
subtypes(Cat) # => 0-element Array{Any,1}

# Todo tipo tem um super tipo; use a função 'super' para pegá-lo.
typeof(5) # => Int64
super(Int64) # => Signed
super(Signed) # => Real
super(Real) # => Number
super(Number) # => Any
super(super(Signed)) # => Number
super(Any) # => Any
# Todos esss tipos, exceto o Int64, são abstratos.

# <: é o operador de subtipagem
type Lion <: Cat # Lion é um subtipo de Cat
  mane_color
  roar::String
end

# Você pode definir mais construtores para seu tipo
# É só definir uma função com o mesmo nome do tipo
# e chamar um construtor existente para pegar o valor do tipo correto
Lion(roar::String) = Lion("green",roar)
# Isso é um construtor externo porque ele está fora da definição do tipo

type Panther <: Cat # Panther também é um subtipo de Cat
  eye_color
  Panther() = new("green")
# Panthers terão apenas esse construtor, e não construtor padrão.
end
# Usando construtores internos, como Panther faz, lhe da o controle
# sobre como os valores dos tipos são criados.
# Quando possivel, você deve usar construtores externos mais do que internos.

####################################################
## 6. Multiple-Dispatch
####################################################


# Em Julia todas as funções nomeadas são funções genericas
# Isso significa que elas são construidas de muitos métodos pequenos
# Cada construtor para Lion é um metodo da função genérica Lion.Lion.

# Para um exemplo sem construtor, vamos fazer a função meow

# Definição para Lion, Panther e Tiger
function meow(animal::Lion)
  animal.roar #propriedades do tipo de acesso usando a notação ponto '.'
end

function meow(animal::Panther)
  "grrr"
end

function meow(animal::Tiger)
  "rawwwr"
end

# Testando a função meow
meow(tigger) # => "rawwr"
meow(Lion("brown","ROAAR")) # => "ROAAR"
meow(Panther()) # => "grrr"

# Revendo o tipo local de hierarchy
issubtype(Tiger,Cat) # => false
issubtype(Lion,Cat) # => true
issubtype(Panther,Cat) # => true

# Definindo uma função que recebe Cats
function pet_cat(cat::Cat)
  println("The cat says $(meow(cat))")
end

pet_cat(Lion("42")) # => exibe "The cat says 42"
try
    pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,)
catch e
    println(e)
end

# Em linguagens orientadas a objeto, envio unico é comúm
# isso significa que o método é selecionado baseado no tipo do seu primeiro argumento
# Em Julia todos os tipos de argumentos contribuem na seleção do melhor método


# Vamos definir uma função com mais argumentos, então poderemos ver a diferença
function fight(t::Tiger,c::Cat)
  println("The $(t.coatcolor) tiger wins!")
end
# => fight (generic function with 1 method)

fight(tigger,Panther()) # => exibe The orange tiger wins!
fight(tigger,Lion("ROAR")) # => exibir The orange tiger wins!

# Vamos mudar o comportamento quando o gato é especificamente um leão
fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!")
# => fight (generic function with 2 methods)

fight(tigger,Panther()) # => exobe The orange tiger wins!
fight(tigger,Lion("ROAR")) # => exobe The green-maned lion wins!

# Nós não precisamos de um tigre para brigar
fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))")
# => fight (generic function with 3 methods)

fight(Lion("balooga!"),Panther()) # => exibe The victorious cat says grrr
try
  fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion)
catch
end

# Aliás, vamos deixar o gato ir primeiro
fight(c::Cat,l::Lion) = println("The cat beats the Lion")
# => Warning: New definition
#    fight(Cat,Lion) at none:1
# is ambiguous with
#    fight(Lion,Cat) at none:2.
# Make sure
#    fight(Lion,Lion)
# is defined first.
#fight (generic function with 4 methods)

# Este aviso é porque não está claro qual método fight será chamado em:
fight(Lion("RAR"),Lion("brown","rarrr")) # => exibe The victorious cat says rarrr
# O resultado pode ser diferente em outras versões de Julia

fight(l::Lion,l2::Lion) = println("The lions come to a tie")
fight(Lion("RAR"),Lion("brown","rarrr")) # => exibe The lions come to a tie


# Embaixo dos panos
# Você pode olhar o llvm e o código assembly gerado.

square_area(l) = l * l      # square_area (generic function with 1 method)

square_area(5) #25

# O que acontece quando alimentamos square_area com um inteiro?
# What happens when we feed square_area an integer?
code_native(square_area, (Int32,))  
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1              # Prólogo
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       movsxd  RAX, EDI        # Busca l na memoria?
    #       imul    RAX, RAX        # Faz o quadrado de l e armazena o resultado em RAX
    #       pop RBP                 # Restaura o ponteiro de base antigo
    #       ret                     # O resultado continua em RAX

code_native(square_area, (Float32,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       vmulss  XMM0, XMM0, XMM0  # Múltiplicação escalar unica de precisão (AVX)
    #       pop RBP
    #       ret

code_native(square_area, (Float64,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       vmulsd  XMM0, XMM0, XMM0 # Duplicação ecalar de precisão multipla(AVX)
    #       pop RBP
    #       ret
    #   
# Note que Julia usará instruções de ponto flutuante se quaser um dos
# argumentos forem float
# Vamos calcular a área de um circulo
circle_area(r) = pi * r * r     # circle_area (generic function with 1 method)
circle_area(5)                  # 78.53981633974483

code_native(circle_area, (Int32,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       vcvtsi2sd   XMM0, XMM0, EDI          # Carrega inteiro (r) da memória
    #       movabs  RAX, 4593140240              # Carrega pi
    #       vmulsd  XMM1, XMM0, QWORD PTR [RAX]  # pi * r
    #       vmulsd  XMM0, XMM0, XMM1             # (pi * r) * r
    #       pop RBP
    #       ret
    #

code_native(circle_area, (Float64,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #       movabs  RAX, 4593140496
    #   Source line: 1
    #       vmulsd  XMM1, XMM0, QWORD PTR [RAX]
    #       vmulsd  XMM0, XMM1, XMM0
    #       pop RBP
    #       ret
    #   
```

## Extras

Você pode ver mais um monte de detalhes no [manual de Julia] (http://docs.julialang.org/en/latest/manual/)
O melhor lugar pra pedir ajuda em Julia é a (muito amigável) [mailing list](https://groups.google.com/forum/#!forum/julia-users).
---
language: kotlin
filename: LearnKotlin-pt.kt
contributors:
    - ["S Webber", "https://github.com/s-webber"]
translators:
    - ["Márcio Torres", "https://github.com/marciojrtorres"]
lang: pt-br
---

Kotlin é uma linguagem de programação estaticamente tipada para a JVM, Android e navegadores web. Ela é 100% interoperável com Java.
[Leia mais aqui.](https://kotlinlang.org/)

```kotlin
// Comentários de uma linha iniciam com //
/*
Comentários multilinha se parecem com este.
*/

// A palavra-chave "package" funciona do mesmo modo que no Java.
package com.learnxinyminutes.kotlin

/*
O ponto de entrada para um programa em Kotlin é uma função chamada "main"
Esta função recebe um vetor contendo quaisquer argumentos da linha de comando
*/
fun main(args: Array<String>) {
    /*
    A declaração de valores pode ser feita tanto com "var" como "val"
    Declarações com "val" não podem ser reatribuídas, enquanto com "var" podem.
    */
    val umVal = 10 // não se poderá reatribuir qualquer coisa a umVal
    var umVar = 10
    umVar = 20 // umVar pode ser reatribuída, mas respeitando o tipo

    /*
    Na maioria dos casos Kotlin pode inferir o tipo, então não é preciso sempre
    especificar o tipo explicitamente, mas quando o fazemos é assim:
    */
    val umInteiro: Int = 7

    /*
    Strings podem ser representadas de forma semelhante a Java.
    A contrabarra realiza o "escape", da mesma forma.
    */
    val umaString = "Minha String está aqui!"
    val outraString = "Imprimir na outra linha?\nSem problema!"
    val maisString = "Você quer adicionar um tab?\tSem problema!"
    println(umaString)
    println(outraString)
    println(maisString)

    /*
    Uma string bruta é delimitada com três aspas (""").
    Strings brutas podem conter novas linhas e outros caracteres.
    */
    val umaStringBruta = """
fun olaMundo(val nome : String) {
   println("Olá, mundo!")
}
"""
    println(umaStringBruta)

    /*
    As strings podem conter expressões modelo (template).
    Uma expressão modelo começa com um cifrão ($).
    É semelhante à interpolação de Strings em Ruby.
    */
    val umaStringModelo = "$umaString tem ${umaString.length} caracteres"
    println(umaStringModelo)

    /*
    Para uma variável receber null deve-se explicitamente declara-la
    como anulável.
    A declaração de anulável é realizada incluindo uma "?" ao fim do tipo.
    Pode-se acessar uma variável anulável usando o operador "?."
    Usa-se o operador "?:" (também conhecido como operador Elvis) para
    atribuir um valor alternativo para quando uma variável é nula.
    */
    var umaVariavelAnulavel: String? = "abc"
    println(umaVariavelAnulavel?.length) // => 3
    println(umaVariavelAnulavel?.length ?: -1) // => 3
    umaVariavelAnulavel = null
    println(umaVariavelAnulavel?.length) // => null
    println(umaVariavelAnulavel?.length ?: -1) // => -1

    /*
    Funções podem ser declaradas usando a palavra-chave "fun"
    Os parâmetros da função são declarados entre parênteses logo
    após o nome da função.
    Os parâmetros da função podem ter opcionalmente um valor padrão.
    O tipo de retorno da função, se necessário, é especificado após os argumentos.
    */
    fun ola(nome: String = "mundo"): String {
        return "Olá, $nome!"
    }
    println(ola("você")) // => Olá, você!
    println(ola(nome = "tu")) // => Olá, tu!
    println(ola()) // => Olá, mundo!

    /*
    Um parâmetro pode ser declarado com a palavra-chave "vararg" para
    permitir que seja passado um número variável de argumentos.
    */
    fun exemploVarArg(vararg numeros: Int) {
        println("Foram recebidos ${numeros.size} argumentos")
    }
    exemploVarArg() // => Passando nenhum argumento (0 argumentos)
    exemploVarArg(1) // => Passando 1 argumento
    exemploVarArg(1, 2, 3) // => Passando 3 argumentos

    /*
    Quando uma função consiste numa única expressão as chaves
    podem ser omitidas e o corpo declarado após o símbolo de "="
    */
    fun impar(x: Int): Boolean = x % 2 == 1
    println(impar(6)) // => false
    println(impar(7)) // => true

    // O tipo de retorno não precisa ser declarado se pode ser inferido.
    fun impar(x: Int) = x % 2 == 0
    println(impar(6)) // => true
    println(impar(7)) // => false

    // Funções podem receber e retornar outras funções
    fun nao(f: (Int) -> Boolean): (Int) -> Boolean {
        return {n -> !f.invoke(n)}
    }
    // Funções nomeadas podem ser passadas como argumento usando o operador "::"
    val naoImpar = nao(::impar)
    val naoPar = nao(::par)
    // Expressões Lambda podem ser usadas como argumentos
    val naoZero = nao {n -> n == 0}
    /*
    Se uma lambda têm apenas um parâmetro sua declaração pode ser omitida,
    incluindo o símbolo "->".
    Neste caso o nome do único parâmetro deve ser "it".
    */
    val naoPositivo = nao {it > 0}
    for (i in 0..4) {
        println("${naoImpar(i)} ${naoPar(i)} ${naoZero(i)} ${naoPositivo(i)}")
    }

    // A palavra-chave "class" é usada para declarar classes
    class ClasseExemplo(val x: Int) {
        fun funcaoMembro(y: Int): Int { // ou "método"
            return x + y
        }

        infix fun funcaoMembroInfixa(y: Int): Int {
            return x * y
        }
    }
    /*
    Para criar uma nova instância chama-se o construtor.
    Note que Kotlin não tem a palavra-chave "new".
    */
    val umaInstanciaDaClasseExemplo = ClasseExemplo(7)
    // Funções membro (métodos) podem ser chamados usando a notação ponto "."
    println(umaInstanciaDaClasseExemplo.funcaoMembro(4)) // => 11
    /*
    Se uma função foi declarada com a palavra-chave "infix" então
    ela pode ser invocada com a notação infixa.
    */
    println(umaInstanciaDaClasseExemplo funcaoMembroInfixa 4) // => 28

    /*
    Classes de dados são um modo sucinto de criar classes que servem apenas
    para guardas informações.
    Os métodos "hashCode", "equals" e "toString" são gerados automaticamente.
    */
    data class ExemploClasseDados (val x: Int, val y: Int, val z: Int)
    val objetoDados = ExemploClasseDados(1, 2, 4)
    println(objetoDados) // => ExemploClasseDados(x=1, y=2, z=4)

    // Classes de dados têm uma função "copy"
    val dadosCopia = objetoDados.copy(y = 100)
    println(dadosCopia) // => ExemploClasseDados(x=1, y=100, z=4)

    // Objetos podem ser desestruturados em múltiplas variáveis.
    val (a, b, c) = dadosCopia
    println("$a $b $c") // => 1 100 4

    // desestruturando em um laço "for"
    for ((a, b, c) in listOf(objetoDados)) {
        println("$a $b $c") // => 1 100 4
    }

    val mapaDados = mapOf("a" to 1, "b" to 2)
    // Map.Entry também é desestruturável
    for ((chave, valor) in mapaDados) {
        println("$chave -> $valor")
    }

    // A função "with" é semelhante à declaração "with" do JavaScript
    data class ExemploClasseDadosMutaveis (var x: Int, var y: Int, var z: Int)
    val objDadosMutaveis = ExemploClasseDadosMutaveis(7, 4, 9)
    with (objDadosMutaveis) {
        x -= 2
        y += 2
        z--
    }
    println(objDadosMutaveis) // => ExemploClasseDadosMutaveis(x=5, y=6, z=8)

    /*
    Pode-se criar uma lista usando a função "listOf".
    A lista é imutável, isto é, elementos não podem ser adicionados ou removidos.
    */
    val umaLista = listOf("a", "b", "c")
    println(umaLista.size) // => 3
    println(umaLista.first()) // => a
    println(umaLista.last()) // => c
    // Elementos de uma lista podem ser acessados pelo índice
    println(umaLista[1]) // => b

    // Uma lista mutável pode ser criada com a função "mutableListOf".
    val umaListaMutavel = mutableListOf("a", "b", "c")
    umaListaMutavel.add("d")
    println(umaListaMutavel.last()) // => d
    println(umaListaMutavel.size) // => 4

    // Similarmente, pode-se criar um conjunto com a função "setOf".
    val umConjunto = setOf("a", "b", "c")
    println(umConjunto.contains("a")) // => true
    println(umConjunto.contains("z")) // => false

    // Da mesma forma que um mapa com a função "mapOf".
    val umMapa = mapOf("a" to 8, "b" to 7, "c" to 9)
    // Os valores contidos no mapa podem ser acessados pela sua chave.
    println(umMapa["a"]) // => 8

    /*
    Sequências representam coleções avaliadas "preguiçosamente" (sob demanda).
    Pode-se criar uma sequência usando a função "generateSequence".
    */
    val umaSequencia = generateSequence(1, { it + 1 })
    val x = umaSequencia.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // Um exemplo de uma sequência usada para gerar Números de Fibonacci:
    fun sequenciaFibonacci(): Sequence<Long> {
        var a = 0L
        var b = 1L

        fun proximo(): Long {
            val resultado = a + b
            a = b
            b = resultado
            return a
        }

        return generateSequence(::proximo)
    }
    val y = sequenciaFibonacci().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

    // Kotlin oferece funções de alta-ordem para trabalhar com coleções.
    val z = (1..9).map {it * 3}
                  .filter {it < 20}
                  .groupBy {it % 2 == 0}
                  .mapKeys {if (it.key) "par" else "impar"}
    println(z) // => {impar=[3, 9, 15], par=[6, 12, 18]}

    // Um "for" pode ser usado com qualquer coisa que ofereça um "iterator"
    for (c in "salve") {
        println(c)
    }

    // O "while" funciona da mesma forma que em outras linguagens.
    var contador = 0
    while (contador < 5) {
        println(contador)
        contador++
    }
    do {
        println(contador)
        contador++
    } while (contador < 10)

    /*
    "if" pode ser usado como uma expressão que retorna um valor.
    Por este motivo o operador ternário "? :" não é necessário em Kotlin.
    */
    val numero = 5
    val mensagem = if (numero % 2 == 0) "par" else "impar"
    println("$numero é $mensagem") // => 5 é impar

    // "when" pode ser usado como alternativa às correntes de "if-else if".
    val i = 10
    when {
        i < 7 -> println("primeiro block")
        umaString.startsWith("oi") -> println("segundo block")
        else -> println("bloco else")
    }

    // "when" pode ser usado com um argumento.
    when (i) {
        0, 21 -> println("0 ou 21")
        in 1..20 -> println("entre 1 e 20")
        else -> println("nenhum dos anteriores")
    }

    // "when" pode ser usada como uma função que retorna um valor.
    var resultado = when (i) {
        0, 21 -> "0 ou 21"
        in 1..20 -> "entre 1 e 20"
        else -> "nenhum dos anteriores"
    }
    println(resultado)

    /*
    Pode-se verificar se um objeto é de um certo tipo usando o operador "is".
    Se o objeto passar pela verificação então ele pode ser usado como
    este tipo, sem a necessidade de uma coerção (cast) explícita (SmartCast).
    */
    fun exemploSmartCast(x: Any) : Boolean {
        if (x is Boolean) {
            // x é automaticamente coagido para Boolean
            return x
        } else if (x is Int) {
            // x é automaticamente coagido para Int
            return x > 0
        } else if (x is String) {
            // x é automaticamente coagido para String
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(exemploSmartCast("Olá, mundo!")) // => true
    println(exemploSmartCast("")) // => false
    println(exemploSmartCast(5)) // => true
    println(exemploSmartCast(0)) // => false
    println(exemploSmartCast(true)) // => true

    // O Smartcast também funciona com blocos "when"
    fun exemploSmartCastComWhen(x: Any) = when (x) {
        is Boolean -> x
        is Int -> x > 0
        is String -> x.isNotEmpty()
        else -> false
    }

    /*
    As extensões são uma maneira nova de adicionar funcionalidades a classes.
    Elas são similares aos "extension methods" da linguagem C#.
    */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("olá, mundo!".remove('o')) // => lá, mund!

    println(ExemploEnum.A) // => A
    println(ExemploObjeto.ola()) // => olá
}

// Classes Enum são similares aos "enum types" do Java.
enum class ExemploEnum {
    A, B, C
}

/*
A palavra-chave "object" pode ser usar para criar Singletons.
Eles não são instanciados, mas podem referenciar sua instância única pelo nome.
É semelhante aos "singleton objects" da linguagem Scala.
*/
object ExemploObjeto {
    fun ola(): String {
        return "olá"
    }
}

fun usaObjeto() {
    ExemploObjeto.ola()
    val algumaReferencia: Any = ExemploObjeto // usa-se o nome diretamente
}

```

### Leitura Adicional

* [Tutoriais de Kotlin](https://kotlinlang.org/docs/tutorials/)(EN)
* [Experimente Kotlin no seu navegador](http://try.kotlinlang.org/)(EN)
* [Uma lista de material sobre Kotlin](http://kotlin.link/)(EN)
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Miguel Araújo", "https://github.com/miguelarauj1o"]
lang: pt-br    
filename: learnmarkdown-pt.md
---

Markdown foi criado por John Gruber in 2004. Originado para ser fácil de ler e 
escrever sintaxe que converte facilmente em HTML (hoje, suporta outros formatos também).

Dê-me feedback tanto quanto você quiser! / Sinta-se livre para a garfar (fork) e 
puxar o projeto (pull request)

```markdown
<!-- Markdown é um superconjunto do HTML, de modo que qualquer arvquivo HTML é 
um arquivo Markdown válido, isso significa que nós podemos usar elementos HTML 
em Markdown, como o elemento de comentário, e eles não serão afetados pelo analisador
de remarcação. No entanto, se você criar um elemento HTML em seu arquivo Markdown, você
não pode usar sintaxe remarcação dentro desse conteúdo do elemento.-->

<!--Markdown também varia de implementação de um analisador para uma próxima.
Este guia vai tentar esclarecer quando as características são universais, ou quando eles são 
específico para um determinado parser -->

<!-- Cabeçalhos -->
<!-- Você pode criar elementos HTML <h1> até <h6> facilmente antecedendo o texto
que deseja estar nesse elemento por um número de hashes (#) -->
# Isto é um cabeçalho <h1>
## Isto é um cabeçalho <h2>
### Isto é um cabeçalho <h3>
#### Isto é um cabeçalho <h4>
##### Isto é um cabeçalho <h5>
###### Isto é um cabeçalho <h6>

<!-- Markdown também nos fornece duas maneiras alternativas de indicar h1 e h2 -->
Isto é um cabeçalho h1
======================

Isto é um cabeçalho h2
----------------------

<!-- Estilos de texto simples --> 
<!-- O texto pode ser facilmente denominado como remarcação itálico, negrito ou tachado usando -->

*Este texto está em itálico*
_E este também está._

**Este texto está em negrito**
__E este também está._

***Este texto está em negrito e itálico.***
**_E este também está_**
*--Danouse! Este também__*

<!-- Em GitHub Flavored Markdown, que é usado para processar arquivos Markdown 
GitHub, nós também temos: --> 

~~Este texto é processado com tachado.~~

<!-- Os parágrafos estão uma ou várias linhas adjacentes de texto separadas por 
uma ou múltiplas linhas em branco. -->

Este é um parágrafo. Eu estou digitando em um parágrafo, não é legal?

Agora, eu estou no parágrado 2.
... Ainda continuo no parágrafo 2! :)

Eu estou no parágrafo três.

<!-- Se você quiser inserir uma tag HTML <br />, você pode acabar com um parágrafo 
com dois ou mais espaços e, em seguida, começar um novo parágrafo -->

Termino com dois espaços (destacar-me para vê-los). 

Há um <br /> acima de mim!

<!-- Bloco de citações são fáceis e feito com o caractere >. -->

> Este é um bloco de citação. Você pode 
> Enrolar manualmente suas linhas e colocar um `>` antes de cada linha ou você pode
> deixar suas linhas ficarem muito longas e enrolar por conta própria. Não faz diferença, 
> desde que eles começam com um `>`.

> Você também pode usar mais de um nível 
>> De recuo? 
> Como pura é isso?

<!-- Listas --> 
<!-- As listas não ordenadas podem ser feitas usando asteriscos, positivos ou hífens -->

* Item
* Item
* Outro item

ou

+ Item
+ Item
+ Outro item

ou

- Item
- Item
- Um último item

<!-- Listas ordenadas são feitas com um número seguido por um ponto -->

1. Item um
2. Item dois
3. Tem três

<!-- Você não tem poder para rotular os itens corretamente e a remarcação será ainda
tornar os números em ordem, mas isso pode não ser uma boa idéia -->

1. Item um
1. Item dois
1. Item três
<!-- (Isto é processado da mesma forma que o exemplo acima) -->

<!-- Você também pode usar subtítulos -->

1. Item um
2. Item dois
3. Item três
    * Sub-item
    * Sub-item
4. Item quatro

<!-- blocos de código --> 
<!-- Você pode indicar um bloco de código (que utiliza o elemento <code>) pelo recuo
uma linha com quatro espaços ou uma guia -->

	Isto é código
	É assim, sacou?

<!-- Você pode também re-guia (ou adicionar mais quatro espaços adicionais) para o recuo 
dentro do seu código -->

	my_array.each do |item|
        puts item
    end	

<!-- Código embutido pode ser criada usando o caractere de crase ` -->

John não sabia nem o que o função 'goto()' fazia!

<!-- Em GitHub Flavored Markdown, você pode usar uma sintaxe especial para o código -->

\`\`\`ruby <!-- exceto remover essas barras invertidas quando você faz isso, apenas ```
ruby! --> 
def foobar
    puts "Hello world!"
end
\`\`\` <!-- Aqui também, não barras invertidas, apenas ``` -->

<-- O texto acima não requer recuo, mas o GitHub vai usar a sintaxe 
destacando do idioma que você especificar após a ``` -->

<!-- Regra Horizontal (<hr />) --> 
<!-- Regras horizontais são facilmente adicionados com três ou mais asteriscos ou hífens,
com ou sem espaços. -->

***
---
- - - 
****************

<!-- Links --> 
<!-- Uma das melhores coisas sobre a remarcação é o quão fácil é fazer ligações. Colocar 
o texto a ser exibido entre parênteses rígidos [] seguido pela url em parênteses () -->

[Click aqui!](http://test.com/)

<!-- Você também pode adicionar um título link usando aspas dentro dos parênteses -->

[Click aqui!](http://test.com/ "Link para Test.com")

<!-- Caminhos relativos funcionam também. -->

[Ir para música](/música/).

<!-- Markdown também suporta ligações de estilo de referência -->

[Clique neste link] [link1] para mais informações sobre isso!
[Além disso, verifique este link] [foobar] se você quiser.

[link1]: http://test.com/ "Legal!"
[foobar]: http://foobar.biz/ "OK!"

<!-- O título também pode estar entre aspas simples ou entre parênteses, ou omitido 
inteiramente. As referências podem estar em qualquer lugar no documento e os IDs de referência 
pode ser qualquer um, desde que eles são únicos. -->

<!-- Existe também o "nomear implícita", que permite que você use o texto do link como o id --> 

[Este] [] é um link. 

[este]: http://thisisalink.com/ 

<!-- Mas não são usados normalmente--> 

<!-- Imagens --> 
<!-- As imagens são feitas da mesma forma que as ligações, mas com um ponto de exclamação na frente! -->

![Este é pairar-texto (texto alternativo) para minha imagem](http://imgur.com/myimage.jpg "Um título opcional") 

<!-- E estilo de referência funciona como esperado --> 

![Este é o pairar-texto.][Myimage] 

[myimage]: relative/urls/legal/image.jpg "se você precisa de um título, é aqui" 

<!-- Miscelânea --> 
<!-- Auto-links --> 

<http://testwebsite.com/> é equivalente a 
[http://testwebsite.com/](http://testwebsite.com/) 

<!-- Auto-links para e-mails --> 

<foo@bar.com> 

<!-- Escapando caracteres --> 

Quero digitar * Este texto entre asteriscos *, mas eu não quero que ele seja 
em itálico, então eu faço o seguinte: \*Este texto entre asteriscos \*.

<!-- Tabelas --> 
<!-- Tabelas estão disponíveis apenas no GitHub Flavored Markdown e são ligeiramente 
complicadas, mas se você realmente quer: -->

| Col1         | Col2     | Col3          |
| :----------- | :------: | ------------: |
| esquerda-alin| Centrado | direita-alinh |
| blah         | blah     | blah          |

<!-- Ou, para os mesmos resultados -->

Col 1 | Col2 | Col3
:-- | :-: | --:
Ugh isso é tão feio | faça isto | parar

<!-- O fim! -->

```
Para mais informações, confira o post oficial de John Gruber de sintaxe [aqui](http://daringfireball.net/projects/markdown/syntax)
e de Adam Pritchard grande cheatsheet [aqui](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).
---
language: Matlab
contributors:
    - ["mendozao", "http://github.com/mendozao"]
    - ["jamesscottbrown", "http://jamesscottbrown.com"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
translators:
    - ["Claudson Martins", "https://github.com/claudsonm"]
lang: pt-br
filename: learnmatlab-pt.mat

---

MATLAB significa MATrix LABoratory. É uma poderosa linguagem de computação numérica geralmente utilizada em engenharia e matemática.

Se você tem algum feedback, por favor fique a vontade para me contactar via
[@the_ozzinator](https://twitter.com/the_ozzinator), ou
[osvaldo.t.mendoza@gmail.com](mailto:osvaldo.t.mendoza@gmail.com).

```matlab
% Comentários iniciam com um sinal de porcentagem

%{
Comentários de múltiplas linhas
parecem
com
algo assim
%}

% Comandos podem ocupar várinhas linhas, usando '...':
 a = 1 + 2 + ...
 + 4

% Comandos podem ser passados para o sistema operacional
!ping google.com

who % Exibe todas as variáveis na memória
whos % Exibe todas as variáveis na memória, com seus tipos
clear % Apaga todas as suas variáveis da memória
clear('A') % Apaga uma variável em particular
openvar('A') % Abre a variável no editor de variável

clc % Apaga o conteúdo escrito na sua janela de comando
diary % Alterna o conteúdo escrito na janela de comando para um arquivo de texto
ctrl-c % Aborta a computação atual

edit('minhafuncao.m') % Abre a função/script no editor
type('minhafuncao.m') % Imprime o código-fonte da função/script na janela de comando

profile on    % Ativa o perfil de código
profile off 	% Desativa o perfil de código
profile viewer 	% Visualiza os resultados na janela de Profiler

help comando 	% Exibe a documentação do comando na janela de comando
doc comando 	% Exibe a documentação do comando na janela de ajuda
lookfor comando % Procura por comando na primeira linha comentada de todas as funções
lookfor comando -all % Procura por comando em todas as funções


% Formatação de saída
format short 	% 4 casas decimais em um número flutuante
format long 	% 15 casas decimais
format bank 	% 2 dígitos após o ponto decimal - para cálculos financeiros
fprintf('texto') % Imprime na tela "texto"
disp('texto') 	% Imprime na tela "texto"

% Variáveis & Expressões
minhaVariavel = 4  % O painel Workspace mostra a variável recém-criada
minhaVariavel = 4; % Ponto e vírgula suprime a saída para a janela de comando
4 + 6  		% Resposta = 10
8 * minhaVariavel 	% Resposta = 32
2 ^ 3 		% Resposta = 8
a = 2; b = 3;
c = exp(a)*sin(pi/2) % c = 7.3891

% A chamada de funções pode ser feita por uma das duas maneiras:
% Sintaxe de função padrão:
load('arquivo.mat', 'y') % Argumentos entre parênteses, separados por vírgula
% Sintaxe de comando:
load arquivo.mat y 	% Sem parênteses, e espaços ao invés de vírgulas
% Observe a falta de aspas na forma de comando: entradas são sempre passadas
% como texto literal - não pode passar valores de variáveis.
% Além disso, não pode receber saída:
[V,D] = eig(A);  % Isto não tem um equivalente na forma de comando
[~,D] = eig(A);  % Se você só deseja D e não V



% Operadores Lógicos e Relacionais
1 > 5 % Resposta = 0
10 >= 10 % Resposta = 1
3 ~= 4 % Diferente de -> Resposta = 1
3 == 3 % Igual a -> Resposta = 1
3 > 1 && 4 > 1 % E -> Resposta = 1
3 > 1 || 4 > 1 % OU -> Resposta = 1
~1 % NOT -> Resposta = 0

% Operadores Lógicos e Relacionais podem ser aplicados a matrizes
A > 5
% Para cada elemento, caso seja verdade, esse elemento será 1 na matriz retornada
A( A > 5 )
% Retorna um vetor com os elementos de A para os quais a condição é verdadeira

% Cadeias de caracteres (Strings)
a = 'MinhaString'
length(a) % Resposta = 11
a(2) % Resposta = i
[a,a] % Resposta = MinhaStringMinhaString


% Vetores de células
a = {'um', 'dois', 'três'}
a(1) % Resposta = 'um' - retorna uma célula
char(a(1)) % Resposta = um - retorna uma string

% Estruturas
A.b = {'um','dois'};
A.c = [1 2];
A.d.e = false;

% Vetores
x = [4 32 53 7 1]
x(2) % Resposta = 32, índices no Matlab começam por 1, não 0
x(2:3) % Resposta = 32 53
x(2:end) % Resposta = 32 53 7 1

x = [4; 32; 53; 7; 1] % Vetor coluna

x = [1:10] % x = 1 2 3 4 5 6 7 8 9 10

% Matrizes
A = [1 2 3; 4 5 6; 7 8 9]
% Linhas são separadas por um ponto e vírgula;
% Elementos são separados com espaço ou vírgula
% A =

%     1     2     3
%     4     5     6
%     7     8     9

A(2,3) % Resposta = 6, A(linha, coluna)
A(6) % Resposta = 8
% (implicitamente encadeia as colunas do vetor, e então as indexa)


A(2,3) = 42 % Atualiza a linha 2 coluna 3 com o valor 42
% A =

%     1     2     3
%     4     5     42
%     7     8     9

A(2:3,2:3) % Cria uma nova matriz a partir da antiga
%Resposta =

%     5     42
%     8     9

A(:,1) % Todas as linhas na coluna 1
%Resposta =

%     1
%     4
%     7

A(1,:) % Todas as colunas na linha 1
%Resposta =

%     1     2     3

[A ; A] % Concatenação de matrizes (verticalmente)
%Resposta =

%     1     2     3
%     4     5    42
%     7     8     9
%     1     2     3
%     4     5    42
%     7     8     9

% Isto é o mesmo de
vertcat(A,A);


[A , A] % Concatenação de matrizes (horizontalmente)

%Resposta =

%     1     2     3     1     2     3
%     4     5    42     4     5    42
%     7     8     9     7     8     9

% Isto é o mesmo de
horzcat(A,A);


A(:, [3 1 2]) % Reorganiza as colunas da matriz original
%Resposta =

%     3     1     2
%    42     4     5
%     9     7     8

size(A) % Resposta = 3 3

A(1, :) =[] % Remove a primeira linha da matriz
A(:, 1) =[] % Remove a primeira coluna da matriz

transpose(A) % Transposta a matriz, que é o mesmo de:
A one
ctranspose(A) % Transposta a matriz
% (a transposta, seguida pelo conjugado complexo de cada elemento)




% Aritmética Elemento por Elemento vs. Aritmética com Matriz
% Naturalmente, os operadores aritméticos agem em matrizes inteiras. Quando
% precedidos por um ponto, eles atuam em cada elemento. Por exemplo:
A * B % Multiplicação de matrizes
A .* B % Multiplica cada elemento em A por seu correspondente em B

% Existem vários pares de funções nas quais uma atua sob cada elemento, e a
% outra (cujo nome termina com m) age na matriz por completo.
exp(A) % Exponencia cada elemento
expm(A) % Calcula o exponencial da matriz
sqrt(A) % Tira a raiz quadrada de cada elemento
sqrtm(A) %  Procura a matriz cujo quadrado é A


% Gráficos
x = 0:.10:2*pi; % Vetor que começa em 0 e termina em 2*pi com incrementos de 0,1
y = sin(x);
plot(x,y)
xlabel('eixo x')
ylabel('eixo y')
title('Gráfico de y = sin(x)')
axis([0 2*pi -1 1]) % x vai de 0 a 2*pi, y vai de -1 a 1

plot(x,y1,'-',x,y2,'--',x,y3,':') % Para várias funções em um só gráfico
legend('Descrição linha 1', 'Descrição linha 2') % Curvas com uma legenda

% Método alternativo para traçar várias funções em um só gráfico:
% Enquanto 'hold' estiver ativo, os comandos serão adicionados ao gráfico
% existente ao invés de o substituirem.
plot(x, y)
hold on
plot(x, z)
hold off

loglog(x, y) % Plotar em escala loglog
semilogx(x, y) % Um gráfico com eixo x logarítmico
semilogy(x, y) % Um gráfico com eixo y logarítmico

fplot (@(x) x^2, [2,5]) % Plotar a função x^2 para x=2 até x=5

grid on % Exibe as linhas de grade; Oculta com 'grid off'
axis square % Torna quadrada a região dos eixos atuais
axis equal % Taxa de proporção onde as unidades serão as mesmas em todas direções

scatter(x, y); % Gráfico de dispersão ou bolha
hist(x); % Histograma

z = sin(x);
plot3(x,y,z); % Plotar em espaço em 3D

pcolor(A) % Mapa de calor da matriz: traça uma grade de retângulos, coloridos pelo valor
contour(A) % Plotar de contorno da matriz
mesh(A) % Plotar malha 3D

h = figure	% Cria uma nova figura objeto, com identificador h
figure(h) % Cria uma nova janela de figura com h
close(h) % Fecha a figura h
close all % Fecha todas as janelas de figuras abertas
close % Fecha a janela de figura atual

shg % Traz uma janela gráfica existente para frente, ou cria uma nova se necessário
clf clear % Limpa a janela de figura atual e redefine a maioria das propriedades da figura

% Propriedades podem ser definidas e alteradas através de um identificador.
% Você pode salvar um identificador para uma figura ao criá-la.
% A função gcf retorna o identificador da figura atual
h = plot(x, y); % Você pode salvar um identificador para a figura ao criá-la
set(h, 'Color', 'r')
% 'y' amarelo; 'm' magenta, 'c' ciano, 'r' vermelho, 'g' verde, 'b' azul, 'w' branco, 'k' preto
set(h, 'LineStyle', '--')
 % '--' linha sólida, '---' tracejada, ':' pontilhada, '-.' traço-ponto, 'none' sem linha
get(h, 'LineStyle')


% A função gca retorna o identificador para os eixos da figura atual
set(gca, 'XDir', 'reverse'); % Inverte a direção do eixo x

% Para criar uma figura que contém vários gráficos use subplot, o qual divide
% a janela de gráficos em m linhas e n colunas.
subplot(2,3,1); % Seleciona a primeira posição em uma grade de 2-por-3
plot(x1); title('Primeiro Plot') % Plota algo nesta posição
subplot(2,3,2); % Seleciona a segunda posição na grade
plot(x2); title('Segundo Plot') % Plota algo ali


% Para usar funções ou scripts, eles devem estar no caminho ou na pasta atual
path % Exibe o caminho atual
addpath /caminho/para/pasta % Adiciona o diretório ao caminho
rmpath /caminho/para/pasta % Remove o diretório do caminho
cd /caminho/para/mudar % Muda o diretório


% Variáveis podem ser salvas em arquivos *.mat
save('meuArquivo.mat') % Salva as variáveis do seu Workspace
load('meuArquivo.mat') % Carrega as variáveis em seu Workspace

% Arquivos M (M-files)
% Um arquivo de script é um arquivo externo contendo uma sequência de instruções.
% Eles evitam que você digite os mesmos códigos repetidamente na janela de comandos.
% Possuem a extensão *.m

% Arquivos M de Funções (M-file Functions)
% Assim como scripts e têm a mesma extensão *.m
% Mas podem aceitar argumentos de entrada e retornar uma saída.
% Além disso, possuem seu próprio workspace (ex. diferente escopo de variáveis).
% O nome da função deve coincidir com o nome do arquivo (salve o exemplo como dobra_entrada.m)
% 'help dobra_entrada.m' retorna os comentários abaixo da linha de início da função
function output = dobra_entrada(x)
	%dobra_entrada(x) retorna duas vezes o valor de x
	output = 2*x;
end
dobra_entrada(6) % Resposta = 12


% Você também pode ter subfunções e funções aninhadas.
% Subfunções estão no mesmo arquivo da função primária, e só podem ser chamados
% por funções dentro do arquivo. Funções aninhadas são definidas dentro de
% outras funções, e têm acesso a ambos workspaces.

% Se você quer criar uma função sem criar um novo arquivo, você pode usar uma
% função anônima. Úteis para definir rapidamente uma função para passar a outra
% função (ex. plotar com fplot, avaliar uma integral indefinida com quad,
% procurar raízes com fzero, ou procurar mínimo com fminsearch).
% Exemplo que retorna o quadrado de sua entrada, atribuído ao identificador sqr:
sqr = @(x) x.^2;
sqr(10) % Resposta = 100
doc function_handle % Saiba mais

% Entrada do usuário
a = input('Digite o valor: ')

% Para a execução do arquivo e passa o controle para o teclado: o usuário pode
% examinar ou alterar variáveis. Digite 'return' para continuar a execução, ou 'dbquit' para sair
keyboard

% Leitura de dados (ou xlsread/importdata/imread para arquivos excel/CSV/imagem)
fopen(nomedoarquivo)

% Saída
disp(a) % Imprime o valor da variável a
disp('Olá Mundo') % Imprime a string
fprintf % Imprime na janela de comandos com mais controle

% Estruturas Condicionais (os parênteses são opicionais, porém uma boa prática)
if (a > 15)
	disp('Maior que 15')
elseif (a == 23)
	disp('a é 23')
else
	disp('Nenhuma condição reconheceu')
end

% Estruturas de Repetição
% Nota: fazer o loop sobre elementos de um vetor/matriz é lento!
% Sempre que possível, use funções que atuem em todo o vetor/matriz de uma só vez.
for k = 1:5
	disp(k)
end

k = 0;
while (k < 5)
	k = k + 1;
end

% Tempo de Execução de Código (Timing Code Execution): 'toc' imprime o tempo
% passado desde que 'tic' foi chamado.
tic
A = rand(1000);
A*A*A*A*A*A*A;
toc

% Conectando a uma base de dados MySQL
dbname = 'nome_base_de_dados';
username = 'root';
password = 'root';
driver = 'com.mysql.jdbc.Driver';
dburl = ['jdbc:mysql://localhost:8889/' dbname];
%Abaixo, o xx depende da versão, download disponível em http://dev.mysql.com/downloads/connector/j/
javaclasspath('mysql-connector-java-5.1.xx-bin.jar');
conn = database(dbname, username, password, driver, dburl);
sql = ['SELECT * FROM nome_tabela WHERE id = 22'] % Exemplo de uma consulta SQL
a = fetch(conn, sql) %a will contain your data


% Funções Matemáticas Comuns
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
exp(x)
sqrt(x)
log(x)
log10(x)
abs(x)
min(x)
max(x)
ceil(x)
floor(x)
round(x)
rem(x)
rand % Números pseudo-aleatórios uniformemente distribuídos
randi % Inteiros pseudo-aleatórios uniformemente distribuídos
randn % Números pseudo-aleatórios normalmente distribuídos

% Constantes Comuns
pi
NaN
inf

% Resolvendo equações matriciais (se não houver solução, retorna uma solução de mínimos quadrados)
% Os operadores \ e / são equivalentes às funções mldivide e mrdivide
x=A\b % Resolve Ax=b. Mais rápido e numericamente mais preciso do que inv(A)*b.
x=b/A % Resolve xA=b

inv(A) % Calcula a matriz inversa
pinv(A) % Calcula a pseudo-inversa

% Funções Matriciais Comuns
zeros(m,n) % Matriz de zeros m x n
ones(m,n) % Matriz de 1's m x n
diag(A) % Extrai os elementos diagonais da matriz A
diag(x) % Constrói uma matriz com os elementos diagonais listados em x, e zero nas outras posições
eye(m,n) % Matriz identidade
linspace(x1, x2, n) % Retorna n pontos igualmente espaçados, com min x1 e max x2
inv(A) % Inverso da matriz A
det(A) % Determinante da matriz A
eig(A) % Valores e vetores próprios de A
trace(A) % Traço da matriz - equivalente a sum(diag(A))
isempty(A) % Testa se a matriz está vazia
all(A) % Testa se todos os elementos são diferentes de zero ou verdadeiro
any(A) % Testa se algum elemento é diferente de zero ou verdadeiro
isequal(A, B) % Testa a igualdade de duas matrizes
numel(A) % Número de elementos na matriz
triu(x) % Retorna a parte triangular superior de x
tril(x) % Retorna a parte triangular inferior de x
cross(A,B) %  Retorna o produto cruzado das matrizes A e B
dot(A,B) % Retorna o produto escalar de duas matrizes (devem possuir mesmo tamanho)
transpose(A) % Retorna a matriz transposta de A
fliplr(A) % Inverte a matriz da esquerda para a direita
flipud(A) % Inverte a matriz de cima para baixo

% Fatorações de Matrizes
% Decomposição LU: PA = LU,L é triangular inferior, U é triangular superior, P é a matriz de permutação
[L, U, P] = lu(A)
% Decomposição em Autovalores: AP = PD, colunas de P são autovetores e as diagonais de D são autovalores
[P, D] = eig(A)
% SVD: XV = US, U e V são matrizes unitárias, S possui elementos não negativos na diagonal em ordem decrescente
[U,S,V] = svd(X)

% Funções Vetoriais Comuns
max     % Maior componente
min     % Menor componente
length  % Tamanho do vetor
sort    % Ordena em orcer ascendente
sum     % Soma de elementos
prod    % Produto de elementos
mode	% Valor modal
median  % Valor mediano
mean    % Valor médio
std     % Desvio padrão
perms(x) % Lista todas as permutações de elementos de x


% Classes
% Matlab pode suportar programação orientada a objetos.
% Classes devem ser colocadas em um arquivo de mesmo nome com a extensão *.m
% Para começar, criamos uma simples classe que armazena posições de GPS
% Início ClassePosicoesGPS.m
classdef ClassePosicoesGPS % O nome da classe.
  properties % As propriedades da classe comportam-se como estruturas
    latitude 
    longitude 
  end
  methods 
    % Este método que tem o mesmo nome da classe é o construtor.
    function obj = ClassePosicoesGPS(lat, lon)
      obj.latitude = lat;
      obj.longitude = lon;
    end

    % Outras funções que usam os objetos de PosicoesGPS
    function r = multiplicarLatPor(obj, n)
      r = n*[obj.latitude];
    end

    % Se quisermos somar dois objetos de PosicoesGPS juntos sem chamar
    % uma função especial nós podemos sobrepor a aritmética do Matlab, desta maneira:
    function r = plus(o1,o2)
      r = ClassePosicoesGPS([o1.latitude] +[o2.latitude], ...
                        [o1.longitude]+[o2.longitude]);
    end
  end
end
% End ClassePosicoesGPS.m

% Podemos criar um objeto da classe usando o construtor
a = ClassePosicoesGPS(45.0, 45.0)

% Propriedades da classe se comportam exatamente como estruturas Matlab
a.latitude = 70.0
a.longitude = 25.0

% Métodos podem ser chamados da mesma forma que funções
ans = multiplicarLatPor(a,3)

% O método também pode ser chamado usando a notação de ponto. Neste caso,
% o objeto não precisa ser passado para o método.
ans = a.multiplicarLatPor(a,1/3)

% Funções do Matlab podem ser sobrepostas para lidar com objetos.
% No método abaixo, nós sobrepomos a forma como o Matlab lida com a soma de
% dois objetos PosicoesGPS.
b = ClassePosicoesGPS(15.0, 32.0)
c = a + b

```

## Mais sobre Matlab

* O site oficial [http://http://www.mathworks.com/products/matlab/](http://www.mathworks.com/products/matlab/)
* O fórum oficial de respostas: [http://www.mathworks.com/matlabcentral/answers/](http://www.mathworks.com/matlabcentral/answers/)

---
language: Paren
filename: learnparen-pt.paren
contributors:
  - ["KIM Taegyoon", "https://github.com/kimtg"]
translators:
    - ["Claudson Martins", "https://github.com/claudsonm"]
lang: pt-br
---

[Paren](https://bitbucket.org/ktg/paren) é um dialeto do Lisp. É projetado para ser uma linguagem embutida.

Alguns exemplos foram retirados de <http://learnxinyminutes.com/docs/racket/>.

```scheme
;;; Comentários
# Comentários

;; Comentários de única linha começam com um ponto e vírgula ou cerquilha

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Tipos de Dados Primitivos e Operadores
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Números
123 ; inteiro
3.14 ; double
6.02e+23 ; double
(int 3.14) ; => 3 : inteiro
(double 123) ; => 123 : double

;; O uso de funções é feito da seguinte maneira (f x y z ...)
;; onde f é uma função e x, y, z, ... são os operandos
;; Se você quiser criar uma lista literal de dados, use (quote) para impedir
;; que sejam interpretados
(quote (+ 1 2)) ; => (+ 1 2)
;; Agora, algumas operações aritméticas
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(^ 2 3) ; => 8
(/ 5 2) ; => 2
(% 5 2) ; => 1
(/ 5.0 2) ; => 2.5

;;; Booleanos
true ; para verdadeiro
false ; para falso
(! true) ; => falso
(&& true false (prn "não chega aqui")) ; => falso
(|| false true (prn "não chega aqui")) ; => verdadeiro

;;; Caracteres são inteiros.
(char-at "A" 0) ; => 65
(chr 65) ; => "A"

;;; Strings são arrays de caracteres de tamanho fixo.
"Olá, mundo!"
"Sebastião \"Tim\" Maia"   ; Contra-barra é um caractere de escape
"Foo\tbar\r\n" ; Inclui os escapes da linguagem C: \t \r \n

;; Strings podem ser concatenadas também!
(strcat "Olá " "mundo!") ; => "Olá mundo!"

;; Uma string pode ser tratada como uma lista de caracteres
(char-at "Abacaxi" 0) ; => 65

;; A impressão é muito fácil
(pr "Isso é" "Paren. ") (prn "Prazer em conhecê-lo!")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Variáveis
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Você pode criar ou definir uma variável usando (set)
;; o nome de uma variável pode conter qualquer caracter, exceto: ();#"
(set alguma-variavel 5) ; => 5
alguma-variavel ; => 5

;; Acessar uma variável ainda não atribuída gera uma exceção
; x ; => Unknown variable: x : nil

;; Ligações locais: Utiliza cálculo lambda! 
;; 'a' e 'b' estão ligados a '1' e '2' apenas dentro de (fn ...)
((fn (a b) (+ a b)) 1 2) ; => 3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Coleções
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Listas

;; Listas são estruturas de dados semelhantes a vetores. (A classe de comportamento é O(1).)
(cons 1 (cons 2 (cons 3 (list)))) ; => (1 2 3)
;; 'list' é uma variação conveniente para construir listas
(list 1 2 3) ; => (1 2 3)
;; Um quote também pode ser usado para uma lista de valores literais
(quote (+ 1 2)) ; => (+ 1 2)

;; Você ainda pode utilizar 'cons' para adicionar um item ao início da lista
(cons 0 (list 1 2 3)) ; => (0 1 2 3)

;; Listas são um tipo muito básico, portanto existe *enorme* funcionalidade
;; para elas, veja alguns exemplos:
(map inc (list 1 2 3))          ; => (2 3 4)
(filter (fn (x) (== 0 (% x 2))) (list 1 2 3 4))    ; => (2 4)
(length (list 1 2 3 4))     ; => 4

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Funções
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use 'fn' para criar funções.
;; Uma função sempre retorna o valor de sua última expressão
(fn () "Olá Mundo") ; => (fn () Olá Mundo) : fn

;; Use parênteses para chamar todas as funções, incluindo uma expressão lambda
((fn () "Olá Mundo")) ; => "Olá Mundo"

;; Atribuir uma função a uma variável
(set ola-mundo (fn () "Olá Mundo"))
(ola-mundo) ; => "Olá Mundo"

;; Você pode encurtar isso utilizando a definição de função açúcar sintático:
(defn ola-mundo2 () "Olá Mundo")

;; Os () acima é a lista de argumentos para a função
(set ola
  (fn (nome)
    (strcat "Olá " nome)))
(ola "Steve") ; => "Olá Steve"

;; ... ou equivalente, usando a definição açucarada:
(defn ola2 (nome)
  (strcat "Olá " name))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Igualdade
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Para números utilize '=='
(== 3 3.0) ; => verdadeiro
(== 2 1) ; => falso

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Controle de Fluxo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Condicionais

(if true 				; Testa a expressão
    "isso é verdade"	; Então expressão
    "isso é falso") 	; Senão expressão
; => "isso é verdade"

;;; Laços de Repetição

;; O laço for é para número
;; (for SÍMBOLO INÍCIO FIM SALTO EXPRESSÃO ..)
(for i 0 10 2 (pr i "")) ; => Imprime 0 2 4 6 8 10
(for i 0.0 10 2.5 (pr i "")) ; => Imprime 0 2.5 5 7.5 10

;; Laço while
((fn (i)
  (while (< i 10)
    (pr i)
    (++ i))) 0) ; => Imprime 0123456789

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Mutação
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use 'set' para atribuir um novo valor a uma variável ou local
(set n 5) ; => 5
(set n (inc n)) ; => 6
n ; => 6
(set a (list 1 2)) ; => (1 2)
(set (nth 0 a) 3) ; => 3
a ; => (3 2)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Macros lhe permitem estender a sintaxe da linguagem.
;; Os macros no Paren são fáceis.
;; Na verdade, (defn) é um macro.
(defmacro setfn (nome ...) (set nome (fn ...)))
(defmacro defn (nome ...) (def nome (fn ...)))

;; Vamos adicionar uma notação infixa
(defmacro infix (a op ...) (op a ...))
(infix 1 + 2 (infix 3 * 4)) ; => 15

;; Macros não são higiênicos, você pode sobrescrever as variáveis já existentes!
;; Eles são transformações de códigos.
```
---
name: perl
category: language
language: perl
filename: learnperl-pt.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
translators:
    - ["Miguel Araújo", "https://github.com/miguelarauj1o"]
lang: pt-br
---

Perl 5 é, uma linguagem de programação altamente capaz, rica em recursos, com mais de 25 anos de desenvolvimento.

Perl 5 roda em mais de 100 plataformas, de portáteis a mainframes e é adequada tanto para prototipagem rápida, quanto em projetos de desenvolvimento em grande escala.

```perl
# Comentários de uma linha começam com um sinal de número.

#### Tipos de variáveis em Perl

# Variáveis iniciam com um sigilo, que é um símbolo que mostra o tipo.
# Um nome de variável válido começa com uma letra ou sublinhado,
# seguido por qualquer número de letras, números ou sublinhados.

### Perl has three main variable types: $scalar, @array, e %hash.

## Scalars
# Um scalar representa um valor único:
my $animal = "camelo";
my $resposta = 42;

# Valores scalar podem ser strings, inteiros ou números ponto-flutuantes e
# Perl vai automaticamente converter entre eles quando for preciso.

## Arrays
# Um array representa uma lista de valores:
my @animais = ("camelo", "vaca", "boi");
my @números = (23, 42, 69);
my @misturado   = ("camelo", 42, 1.23);

## Hashes
# Um hash representa um conjunto de pares chave/valor:

my %fruta_cor = ("maçã", "vermelho", "banana", "amarelo");

# Você pode usar o espaço em branco e o operador "=>" para colocá-los de
# maneira mais agradável:

my %fruta_cor = (
  maçã  => "vermelho",
  banana => "amarelo",
);

# Scalars, arrays and hashes são documentados mais profundamentes em perldata.
# (perldoc perldata).

# Mais tipos de dados complexos podem ser construídas utilizando referências,
# o que permite que você crie listas e hashes dentro de listas e hashes.

#### Condicionais e construtores de iteração

# Perl possui a maioria das construções condicionais e de iteração habituais.

if ($var) {
  ...
} elsif ($var eq 'bar') {
  ...
} else {
  ...
}

unless (condição) {
  ...
}
# Isto é fornecido como uma versão mais legível de "if (!condition)"

# A forma Perlish pós-condição
print "Yow!" if $zippy;
print "Nós não temos nenhuma banana" unless $bananas;

#  while
while (condição) {
  ...
}

# for
for (my $i = 0; $i < $max; $i++) {
  print "valor é $i";
}

for (my $i = 0; $i < @elements; $i++) {
  print "Elemento atual é " . $elements[$i];
}

for my $element (@elements) {
  print $element;
}

# implícito

for (@elements) {
  print;
}

#### Expressões regulares

# O suporte a expressões regulares do Perl é ao mesmo tempo amplo e profundo,
# e é objeto de longa documentação em perlrequick, perlretut, e em outros
# lugares. No entanto, em suma:

# Casamento simples
if (/foo/)       { ... }  # verdade se $_ contém "foo"
if ($a =~ /foo/) { ... }  # verdade se $a contém "foo"

# Substituição simples

$a =~ s/foo/bar/;         # substitui foo com bar em $a
$a =~ s/foo/bar/g;        # substitui TODAS AS INSTÂNCIAS de foo com bar em $a

#### Arquivos e I/O

# Você pode abrir um arquivo para entrada ou saída usando a função "open()".

open(my $in,  "<",  "input.txt")  ou desistir "Não pode abrir input.txt: $!";
open(my $out, ">",  "output.txt") ou desistir "Não pode abrir output.txt: $!";
open(my $log, ">>", "my.log")     ou desistir "Não pode abrir my.log: $!";

# Você pode ler de um arquivo aberto usando o operador "<>". No contexto
# scalar, ele lê uma única linha do arquivo, e em contexto de lista lê o
# arquivo inteiro, atribuindo cada linha a um elemento da lista:

my $linha  = <$in>;
my @linhas = <$in>;

#### Escrevendo subrotinas

# Escrever subrotinas é fácil:

sub logger {
  my $mensagem = shift;

  open my $arquivo, ">>", "my.log" or die "Não poderia abrir my.log: $!";

  print $arquivo $ensagem;
}

# Agora nós podemos usar a subrotina como qualquer outra função construída:

logger("Nós temos uma subrotina de log!");
```

#### Usando módulos Perl

Módulos Perl provê uma lista de recursos para lhe ajudar a evitar redesenhar
a roda, e tudo isso pode ser baixado do CPAN (http://www.cpan.org/). Um número
de módulos populares podem ser incluídos com a própria distribuição do Perl.

perlfaq contém questões e respostas relacionadas a muitas tarefas comuns, e frequentemente provê sugestões para um bom números de módulos CPAN.

#### Leitura Adicional

 - [perl-tutorial](http://perl-tutorial.org/)
 - [Learn at www.perl.com](http://www.perl.org/learn.html)
 - [perldoc](http://perldoc.perl.org/)
 - and perl built-in : `perldoc perlintro`
---
category: tool
tool: composer
contributors:
    - ["Brett Taylor", "https://github.com/glutnix"]
translators:
    - ["David Lima", "https://github.com/davelima"]
lang: pt-br
filename: LearnComposer-pt.sh
---

[Composer](https://getcomposer.org/) é uma ferramenta de gerenciamento de dependências para PHP. Ele permite que você defina as bibliotecas que seu projeto precisa, e então ele as gerencia (instala/atualiza) para você.

# Instalando

```sh
# Instala o binário composer.phar no diretório atual
curl -sS https://getcomposer.org/installer | php
# Se você fizer desta forma, você precisará chamar o composer assim:
php composer.phar about

# Instala o binário em ~/bin/composer
# Nota: certifique-se de que ~/bin está na variável de ambiente PATH do seu shell
curl -sS https://getcomposer.org/installer | php -- --install-dir=~/bin --filename=composer
```

Usuários Windows devem seguir as Instruções de instalação para Windows:
https://getcomposer.org/doc/00-intro.md#installation-windows (EN)

## Confirmando a instalação

```sh
# Verifica a versão e lista as opções
composer

# Para obter ajuda com os comandos
composer help require

# Verifica se o Composer tem as permissões necessárias e se está atualizado
composer diagnose
composer diag # atalho

# Atualiza o binário do Composer para a última versão
composer self-update
composer self # atalho
```

# Modo de uso

O Composer armazena as dependências do seu projeto em `composer.json`.
Você pode editar este arquivo, mas é recomendável deixar que o Composer faça isso.

```sh
# Cria um novo projeto na pasta atual
composer init
# Executa um questionário interativo, te pedindo detalhes sobre o projeto.
# Você pode deixar o questionário em branco, desde que não haja outros projetos dependendo deste.

# Se um arquivo composer.json já existir, baixa as dependências
composer install

# Para baixar apenas as dependências de produção, excluindo as de desenvolvimento
composer install --no-dev

# Adiciona uma dependência de produção ao projeto
composer require guzzlehttp/guzzle
# O Composer se encarrega de verificar qual é a última versão de
# guzzlehttp/guzzle, baixar e adicionar a nova dependência no
# campo 'require' do composer.json

composer require guzzlehttp/guzzle:6.0.*
# O composer baixa a última versão que combine com o padrão informado (6.0.2, por exemplo)
# e adiciona essa dependência ao campo 'require' do arquivo composer.json

composer require --dev phpunit/phpunit:~4.5.0
# O composer irá baixar a dependencia como desenvolvimento,
# usando a versão mais recente >= 4.5.0 e < 4.6.0

composer require-dev phpunit/phpunit:^4.5.0
# O composer irá baixar a dependencia como desenvolvimento,
# usando a versão mais recente >= 4.5.0 e < 5.0

# Para mais informações sobre os padrões de versões, veja a
# Documentação sobre Versões do Composer: https://getcomposer.org/doc/articles/versions.md (EN)

# Para ver quais pacotes estão disopníveis e quais estão instalados
composer show

# Para ver quais pacotes estão instalados
composer show --installed

# Para encontrar um pacote que tenha 'mailgun' no nome ou descrição
composer search mailgun
```

[Packagist.org](https://packagist.org/) é o repositório principal para pacotes Composer. Pesquise aqui por pacotes existentes.

## `composer.json` vs `composer.lock`

O arquivo `composer.json` armazena as preferências de de versão de cada dependência, além de outras informações

O arquivo `composer.lock` armazena exatamente qual versão foi baixada para cada dependência. Nunca altere este arquivo.

Se você incluir o arquivo `composer.lock` no seu repositório git, todos os desenvolvedores irão instalar a mesma versão das dependências que você.
Mesmo se uma nova versão for lançada, o Composer baixará apenas a versão salva no arquivo de lock.

```sh
# Atualizar todas as dependências para a versão mais recente (ainda dentro das preferências definidas)
composer update

# Para atualizar a versão de uma dependência específica:
composer update phpunit/phpunit

# Para migrar um pacote para uma nova preferência de versão, você pode precisar
# remover o pacote antigo e suas dependências primeiro
composer remove --dev phpunit/phpunit
composer require --dev phpunit/phpunit:^5.0

```

## Autoloader

O Composer cria uma classe autoloader que você pode usar na sua aplicação.
Você pode instanciar as classes pelos seus namespaces.

```php
require __DIR__ . '/vendor/autoload.php';

$mailgun = new Mailgun\Mailgun("key");
```

### Autoloader da PSR-4

Você pode adicionar seus próprios namespaces ao autoloader.

No `composer.json`, adicione um campo 'autoload':

```json
{
  "autoload": {
    "psr-4": {"Acme\\": "src/"}
  }
}
```
Isso irá dizer ao autoloader para buscar na pasta `src` tudo o que estiver no namespace `\Acme\`.

Você também pode [usar a PSR-0, um mapa de classes ou apenas listar os arquivos para incluir](https://getcomposer.org/doc/04-schema.md#autoload),
e pode usar o campo `autoload-dev` para namespaces de desenvolvimento.

Ao adicionar ou alterar alguma chave de autoload, você precisará recriar o autoloader

```sh
composer dump-autoload
composer dump # shorthand

# Otimiza pacotes PSR-0 e PSR-4 para carregar com mapas de classes também.
# É mais demorado, mas melhora a performance em produção.
composer dump-autoload --optimize --no-dev
```

# O cache do Composer

```sh
# O Composer irá evitar baixar pacotes caso eles estejam no cache. Para limpar o cache:
composer clear-cache
```

# Resolução de problemas

```sh
composer diagnose
composer self-update
composer clear-cache
```

## Tópicos (ainda) não falados neste tutorial

* Criando e distribuindo seus próprios pacotes no Packagist.org ou qualquer lugar
* Hooks Pré- e Pós-: rodar tarefas específicas em determinados eventos do Composer

### Referências

* [Composer - O gerenciador de dependências do PHP](https://getcomposer.org/) (EN)
* [Packagist.org](https://packagist.org/) (EN)
---
language: PHP
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
translators:
    - ["Abdala Cerqueira", "http://abda.la"]
    - ["Raquel Diniz", "http://twitter.com/raquelrdiniz"]
lang: pt-br
filename: php-pt.html.markdown
---

Este documento descreve PHP 5+.

```php
<?php // O código PHP deve estar incluso na tag <?php

// Se o arquivo PHP só contém código PHP, a melhor prática
// é omitir a tag de fechamento PHP.

// Duas barras iniciam o comentário de uma linha.

# O hash (aka pound symbol) também inicia, mas // é mais comum.

/*
     O texto envolto por barra-asterisco e asterisco-barra
     faz um comentário de múltiplas linhas.
*/

// Utilize "echo" ou "print" para imprimir a saída.
print('Olá '); // Imprime "Olá " sem quebra de linha.
print 'Olá '; // Não tem a necessidade de utilizar as chaves.

// () são opcionais para print e echo
echo "Mundo\n"; // Imprime "Mundo" com quebra de linha.
echo ("Mundo\n"); // Podemos tambem utilizar com chaves no echo.
// (Todas as declarações devem terminar com um ponto e vírgula.)

// Qualquer coisa fora da tag <?php é impresso automaticamente.
?>
Olá mundo novamente!
<?php


/************************************
 * Tipos e variáveis
 */

// Variáveis começam com o símbolo $.
// Um nome de variável válido se inicia com uma letra ou sublinhado,
// seguido por qualquer quantidade de letras, números ou sublinhados.

// Valores booleanos não diferenciam maiúsculo de minúsculo (case-insensitive).
$boolean = true;  // ou TRUE ou True
$boolean = false; // ou FALSE ou False

// Inteiros
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (um 0 denota um número octal)
$int4 = 0x0F; // => 15 (um 0x denota um literal hex)

// Flutuantes - Floats (aka doubles)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// Excluir variável.
unset($int1);

// Aritmética
$soma      = 1 + 1; // 2
$diferenca = 2 - 1; // 1
$produto   = 2 * 2; // 4
$quociente = 2 / 1; // 2

// Taquigrafia aritmética
$numero = 0;
$numero += 1;      // Incrementa $number em 1
echo $numero++;    // Imprime 1 (incrementa após a avaliação)
echo ++$numero;    // Imprime 3 (incrementa antes da avaliação)
$numero /= $float; // Divide e atribui o quociente de $numero

// Strings podem ser colocadas entre aspas simples.
$sgl_quotes = '$String'; // => '$String'

// Evite o uso de aspas duplas, exceto para incorporar outras variáveis
$dbl_quotes = "Esta é uma $sgl_quotes."; // => 'Esta é uma $String.'

// Os caracteres especiais só são escapados entre aspas duplas.
$escapado    = "Este contém um \t caractere tab."; 
echo $escapado; //Imprime: Este contém um     caractere tab.
$naoescapado = 'Este contém somente a barra e o t: \t'; 
echo $naoescapado; //Imprime: Este contém somente a barra e o t: \t

// Coloque uma variável entre chaves se necessário.
$dinheiro = "Eu tenho $${numero} no banco.";

// Desde o PHP 5.3, nowdocs podem ser usados para múltiplas linhas sem análise
$nowdoc = <<<'FIM'
múltiplas linhas
string
FIM;

// Heredocs farão a análise
$heredoc = <<<FIM
múltiplas linhas
$sgl_quotes
FIM;

// Concatenação de string é feita com .
echo 'Esta string ' . 'é concatenada'; //Imprime: 'Esta string é concatenada'


/********************************
 * Constantes
 */

// Uma constante é definida usando define()
// e nunca pode ser mudada durante a execução!

// Um nome de constante válida começa com uma letra ou sublinhado,
// seguido por qualquer quantidade de letras, números ou sublinhados.
define("FOO",     "alguma coisa");

// Acesso a uma constante é possível usando diretamente o nome escolhido
echo 'Isto sairá '.FOO; //Imprime: Isto sairá alguma coisa


/********************************
 * Arrays
 */

// Todos os arrays em PHP são arrays associativos (hashmaps),

// Funciona com todas as versões do PHP
$associativo = array('Um' => 1, 'Dois' => 2, 'Tres' => 3);

// PHP 5.4 introduziu uma nova sintaxe
$associativo = ['Um' => 1, 'Dois' => 2, 'Tres' => 3];

echo $associativo['Um']; // Imprime 1.

// Uma lista de literais atribui chaves inteiras implicitamente
$array = ['Um', 'Dois', 'Tres'];
echo $array[0]; // Imprime => "Um"

// Adiciona um elemento no final do array
$array[] = 'Quatro';

// Remove um elemento do array.
unset($array[3]);

/********************************
 * Saída
 */

echo('Olá Mundo!');
// Imprime Olá Mundo! para stdout.
// Stdout é uma página web se executado em um navegador.

print('Olá Mundo!'); // O mesmo que o echo.

// echo é atualmente um construtor de linguagem, então você pode 
// remover os parênteses.
echo 'Olá Mundo!'; // Imprime: Olá Mundo!
print 'Olá Mundo!'; // O print também é - Imprime: Olá Mundo! 

$paragrafo = 'parágrafo';

echo 100;        // Imprime valores escalares diretamente
echo $paragrafo; // ou variáveis

// Se a abertura de tags curtas está configurada, ou sua versão do PHP é
// 5.4.0 ou maior, você pode usar a sintaxe de echo curto
?>
<p><?= $paragrafo ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $x agora contém o mesmo valor de $y
$z = &$y;
// $z agora contém uma referência para $y. Mudando o valor de
// $z irá mudar o valor de $y também, e vice-versa.
// $x irá permanecer inalterado com o valor original de $y

echo $x; // Imprime => 2
echo $z; // Imprime => 2
$y = 0;
echo $x; // Imprime => 2
echo $z; // Imprime => 0

// Despeja tipos e valores de variável para o stdout
var_dump($z); // imprime int(0)

// Imprime variáveis para stdout em formato legível para humanos
print_r($array); // imprime: Array ( [0] => Um [1] => Dois [2] => Tres )

/********************************
 * Lógica
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// assert lança um aviso se o seu argumento não é verdadeiro

// Estas comparações serão sempre verdadeiras, mesmo que os tipos 
// não sejam os mesmos.
assert($a == $b); // igualdade
assert($c != $a); // desigualdade
assert($c <> $a); // alternativa para desigualdade
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// A seguir, só serão verdadeiras se os valores correspondem e são do mesmo tipo.
assert($c === $d);
assert($a !== $d);
assert(1 == '1');
assert(1 !== '1');

// As variáveis podem ser convertidas entre tipos, dependendo da sua utilização.

$inteiro = 1;
echo $inteiro + $inteiro; // Imprime => 2

$string = '1';
echo $string + $string; // Imprime => 2 (strings são coagidas para inteiros)

$string = 'one';
echo $string + $string; // Imprime => 0
// Imprime 0 porque o operador + não pode fundir a string 'um' para um número

// Tipo de fundição pode ser utilizado para tratar uma variável 
// como um outro tipo

$booleano = (boolean) 1; // => true

$zero = 0;
$booleano = (boolean) $zero; // => false

// Há também funções dedicadas para fundir a maioria dos tipos
$inteiro = 5;
$string = strval($inteiro);

$var = null; // valor Null


/********************************
 * Estruturas de controle
 */

if (true) {
    print 'Eu fico impresso';
}

if (false) {
    print 'Eu não\'t';
} else {
    print 'Eu fico impresso';
}

if (false) {
    print 'Não fica impresso';
} elseif(true) {
    print 'Fica';
}

// operadores ternários
print (false ? 'Não fica impresso' : 'Fica');

$x = 0;
if ($x === '0') {
    print 'Não imprime';
} elseif($x == '1') {
    print 'Não imprime';
} else {
    print 'Imprime';
}



// Esta sintaxe alternativa é útil para modelos (templates)
?>

<?php if ($x): ?>
Isto é exibido se o teste for verdadeiro.
<?php else: ?>
Isto é apresentado caso contrário.
<?php endif; ?>

<?php

// Use switch para salvar alguma lógica.
switch ($x) {
    case '0':
        print 'Switch faz coerção de tipo';
        break; // Você deve incluir uma pausa, ou você vai cair
               // no caso 'dois' e 'tres'
    case 'dois':
    case 'tres':
        // Faz alguma coisa, se a variável é 'dois' ou 'tres'
        break;
    default:
        // Faz algo por padrão
}

// While, do...while e for são repetições provavelmente familiares
$i = 0;
while ($i < 5) {
    echo $i++;
}; // Imprime "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // Imprime "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // Imprime "0123456789"

echo "\n";

$rodas = ['bicicleta' => 2, 'carro' => 4];

// Repetições foreach podem iterar sobre arrays
foreach ($rodas as $contador_rodas) {
    echo $contador_rodas;
} // Imprime "24"

echo "\n";

// Você pode iterar sobre as chaves, bem como os valores
foreach ($rodas as $veiculo => $contador_rodas) {
    echo "O $veiculo tem $contador_rodas rodas";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // Sai da repetição
    }
    echo $i++;
} // Imprime "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // Ignora esta iteração da repetição
    }
    echo $i;
} // Imprime "0124"


/********************************
 * Functions
 */

// Define a função com "function":
function minha_funcao () {
  return 'Olá';
}

echo minha_funcao(); // => "Olá"

// Um nome de função válido começa com uma letra ou sublinhado,
// seguido por qualquer quantidade de letras, números ou sublinhados.

function adicionar($x, $y = 1) { // $y é opcional e o valor padrão é 1
  $resultado = $x + $y;
  return $resultado;
}

echo adicionar(4); // => 5
echo adicionar(4, 2); // => 6

// $resultado não é acessível fora da função
// print $resultado; // Dá uma aviso.

// Desde o PHP 5.3 você pode declarar funções anônimas
$inc = function ($x) {
  return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
  echo "$x - $y - $z";
}

// Funções podem retornar funções
function bar ($x, $y) {
  // Utilize 'use' para trazer variáveis de fora
  return function ($z) use ($x, $y) {
    foo($x, $y, $z);
  };
}

$bar = bar('A', 'B');
$bar('C'); // Imprime "A - B - C"

// Você pode chamar funções nomeadas usando strings
$nome_funcao = 'add';
echo $nome_funcao(1, 2); // => 3
// Útil para dinamicamente determinar qual função será executada.
// Ou utilize call_user_func(callable $callback [, $parameter [, ... ]]);

/********************************
 * Includes (Incluir)
 */

<?php
// PHP dentro de arquivos incluídos também deve começar com uma tag 
// de abertura do PHP.

include 'meu-arquivo.php';
// O código meu-arquivo.php já está disponível no escopo atual.
// Se o arquivo não pode ser incluído (por exemplo, arquivo não encontrado), 
//um aviso é emitido.

include_once 'meu-arquivo.php';
// Se o código no meu-arquivo.php foi incluído em outro lugar, ele não vai
// ser incluído novamente. Isso evita vários erros de declaração de classe

require 'meu-arquivo.php';
require_once 'meu-arquivo.php';
// Faz o mesmo que o include(), exceto que o require() irá causar um erro fatal
// se o arquivo não puder ser incluído

// Conteúdo de meu-include.php:
<?php

return 'Qualquer coisa que você quiser.';
// Fim do arquivo

// Includes e requires também podem retornar um valor.
$valor = include 'meu-include.php';

// Arquivos são incluídos com base no caminho determinado ou,
// se este não for passado, com base na diretiva de configuração include_path.
// Se o arquivo não é encontrado no include_path, o include vai finalmente
// verificar no próprio diretório do script chamado e no diretório
// de trabalho atual antes de falhar.
/* */

/********************************
 * Classes
 */

// As classes são definidas com a palavra-chave class

class MinhaClasse
{
    const MINHA_CONST      = 'valor'; // Uma constante

    static $valorEstatico   = 'estatico';

    // Variáveis estáticas e sua visibilidade
    public static $valorEstaticoPublico = 'estaticoPublico';
    // Acessível somente dentro da classe
    private static $valorEstaticoPrivado = 'estaticoPrivado';
    // Acessível a partir da classe e subclasses
    protected static $valorEstaticoProtegido = 'estaticoProtegido';

    // Propriedades devem declarar a sua visibilidade
    public $propriedade    = 'publica';
    public $propInstancia;
    protected $prot = 'protegida'; // Acessível a partir da classe e subclasses
    private $priv   = 'privada';   // Acessível somente dentro da classe

    // Criar um construtor com o __construct
    public function __construct($propInstancia) {
        // Acesse variável de instância utilizando $this
        $this->propInstancia = $propInstancia;
    }

    // Métodos são declarados como funções dentro de uma classe
    public function meuMetodo()
    {
        print 'MinhaClasse';
    }

    //palavra-chave final faz uma função não poder ser sobrescrita
    final function voceNaoPodeMeSobrescrever()
    {
    }

/*
 * Declarando propriedades ou métodos de classe como estáticos faz deles 
 * acessíveis sem precisar instanciar a classe. A propriedade declarada
 * como estática não pode ser acessada com um objeto
 * instanciado da classe (embora métodos estáticos possam).
*/

    public static function meuMetodoEstatico()
    {
        print 'Eu sou estatico';
    }
}

echo MinhaClasse::MINHA_CONST;    // Imprime 'valor';
echo MinhaClasse::$valorEstatico; // Imprime 'estatico';
MinhaClasse::meuMetodoEstatico(); // Imprime 'Eu sou estatico';

// Instantiate classes using new
$minha_classe = new MinhaClasse('Uma propriedade de instância');
// Os parênteses são opcionais, se não passar algum argumento.

// Acesse membros da classe utilizando ->
echo $minha_classe->propriedade;  // => "publica"
echo $minha_classe->instanceProp; // => "Uma propriedade de instância"
$minha_classe->meuMetodo();       // => "MinhaClasse"


// Estender classes usando "extends"
class MinhaOutraClasse extends MinhaClasse
{
    function imprimePropriedadeProtegida()
    {
        echo $this->prot;
    }

    // Sobrescrever um método
    function meuMetodo()
    {
        parent::meuMetodo();
        print ' > MinhaOutraClasse';
    }
}

$minha_outra_classe = new MinhaOutraClasse('Propriedade de instância');
$minha_outra_classe->imprimePropriedadeProtegida(); // => Prints "protegida"
$minha_outra_classe->myMethod(); // Prints "MinhaClasse > MinhaOutraClasse"

final class VoceNaoPodeMeEstender
{
}

// Você pode usar "métodos mágicos" para criar getters e setters
class MinhaClasseMapa
{
    private $propriedade;

    public function __get($chave)
    {
        return $this->$chave;
    }

    public function __set($chave, $valor)
    {
        $this->$chave = $valor;
    }
}

$x = new MinhaClasseMapa();
echo $x->propriedade; // Irá usar o método __get()
$x->propriedade = 'Algo'; // Irá usar o método __set()

// Classes podem ser abstratas (usando a palavra-chave abstract) ou
// implementar interfaces (usando a palavra-chave implements).
// Uma interface é declarada com a palavra-chave interface.

interface InterfaceUm
{
    public function fazAlgo();
}

interface InterfaceDois
{
    public function fazOutraCoisa();
}

// interfaces podem ser estendidas
interface InterfaceTres extends InterfaceDois
{
    public function fazOutroContrato();
}

abstract class MinhaClasseAbstrata implements InterfaceUm
{
    public $x = 'fazAlgo';
}

class MinhaClasseConcreta extends MinhaClasseAbstrata implements InterfaceDois
{
    public function fazAlgo()
    {
        echo $x;
    }

    public function fazOutraCoisa()
    {
        echo 'fazOutraCoisa';
    }
}


// Classes podem implementar mais de uma interface
class UmaOutraClasse implements InterfaceUm, InterfaceDois
{
    public function fazAlgo()
    {
        echo 'fazAlgo';
    }

    public function fazOutraCoisa()
    {
        echo 'fazOutraCoisa';
    }
}


/********************************
 * Traits (Traços)
 */

// Traits estão disponíveis a partir de PHP 5.4.0 e 
// são declarados usando "trait"

trait MeuTraco
{
    public function meuMetodoDeTraco()
    {
        print 'Eu tenho MeuTraco';
    }
}

class MinhaClasseTracada
{
    use MeuTraco;
}

$cls = new MinhaClasseTracada();
$cls->meuMetodoDeTraco(); // Imprime "Eu tenho MeuTraco"


/********************************
 * Namespaces (Espaço nominal)
 */

// Esta seção é separada porque a declaração de espaços nominais
// deve ser a primeira instrução em um arquivo. Vamos fingir, aqui não é o caso

<?php

// Por padrão, as classes existem no espaço nominal global e podem
// ser explicitamente chamadas com uma barra invertida.

$cls = new \MinhaClasse();



// Definir o espaço nominal para o arquivo
namespace Meu\Espaconominal;

class MinhaClasse
{
}

// (de outro arquivo)
$cls = new Meu\Espaconominal\MinhaClasse;

//Ou de dentro de outro espaço nominal.
namespace Meu\Outro\Espaconominal;

use My\Espaconominal\MinhaClasse;

$cls = new MinhaClasse();

//Ou você pode usar como apelido de espaço nominal;

namespace Meu\Outro\Espaconominal;

use Meu\Espaconominal as OutroEspaconominal;

$cls = new OutroEspaconominal\MinhaClasse();

*/

```

## Mais informações

Visite a [documentação oficial do PHP](http://www.php.net/manual/) 
para referência e participação da comunidade.

Se você estiver interessado em melhores práticas atualizadas, visite
[PHP The Right Way](http://www.phptherightway.com/).

Se você está vindo de uma linguagem com bom gerenciamento de pacotes, confira
[Composer](http://getcomposer.org/).

Para os padrões comuns, visite o Grupo de Interoperabilidade de Framework PHP
[PSR standards](https://github.com/php-fig/fig-standards).
---
language: python
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["Vilson Vieira", "http://automata.cc"]
lang: pt-br
filename: learnpython-pt.py
---

Python foi criado por Guido Van Rossum no começo dos anos 90. Atualmente é uma
das linguagens de programação mais populares. Eu me apaixonei por Python, por
sua clareza de sintaxe. É basicamente pseudocódigo executável.

Comentários serão muito apreciados! Você pode me contactar em
[@louiedinh](http://twitter.com/louiedinh) ou louiedinh [arroba]
[serviço de email do google]

Nota: Este artigo usa Python 2.7 especificamente, mas deveria ser aplicável a
qualquer Python 2.x. Logo haverá uma versão abordando Python 3!

```python
# Comentários de uma linha começam com cerquilha (ou sustenido)
""" Strings de várias linhas podem ser escritas
    usando três ", e são comumente usadas
    como comentários
"""

####################################################
## 1. Tipos de dados primitivos e operadores
####################################################

# Você usa números normalmente
3 #=> 3

# Operadores matemáticos são aqueles que você já está acostumado
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# A divisão é um pouco estranha. A divisão de números inteiros arredonda
# para baixo o resultado, automaticamente
5 / 2 #=> 2

# Para concertar a divisão, precisamos aprender sobre números de ponto
# flutuante (conhecidos como 'float').
2.0     # Isso é um 'float'
11.0 / 4.0 #=> 2.75 ahhh... muito melhor

# Forçamos a precedência de operadores usando parênteses
(1 + 3) * 2 #=> 8

# Valores booleanos (ou 'boolean') são também tipos primitivos
True
False

# Negamos usando 'not'
not True #=> False
not False #=> True

# Testamos igualdade usando '=='
1 == 1 #=> True
2 == 1 #=> False

# E desigualdade com '!='
1 != 1 #=> False
2 != 1 #=> True

# Mais comparações
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# As comparações podem ser encadeadas!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Strings são criadas com " ou '
"Isso é uma string."
'Isso também é uma string.'

# Strings podem ser somadas (ou melhor, concatenadas)!
"Olá " + "mundo!" #=> "Olá mundo!"

# Uma string pode ser tratada como uma lista de caracteres
"Esta é uma string"[0] #=> 'E'

# O caractere % pode ser usado para formatar strings, desta forma:
"%s podem ser %s" % ("strings", "interpoladas")

# Um jeito novo de formatar strings é usando o método 'format'.
# Esse método é o jeito mais usado
"{0} podem ser {1}".format("strings", "formatadas")
# Você pode usar palavras-chave (ou 'keywords') se você não quiser contar.
"{nome} quer comer {comida}".format(nome="João", comida="lasanha")

# 'None' é um objeto
None #=> None

# Não use o operador de igualdade `==` para comparar objetos com 'None'
# Ao invés disso, use `is`
"etc" is None #=> False
None is None  #=> True

# O operador 'is' teste a identidade de um objeto. Isso não é
# muito útil quando estamos lidando com valores primitivos, mas é
# muito útil quando lidamos com objetos.

# None, 0, e strings/listas vazias são todas interpretadas como 'False'.
# Todos os outros valores são 'True'
0 == False  #=> True
"" == False #=> True


####################################################
## 2. Variáveis e Coleções
####################################################

# Imprimir na tela é muito fácil
print "Eu sou o Python. Prazer em te conhecer!"


# Nós não precisamos declarar variáveis antes de usá-las, basta usar!
alguma_variavel = 5    # A convenção é usar caixa_baixa_com_sobrescritos
alguma_variavel #=> 5

# Acessar uma variável que não teve nenhum valor atribuído anteriormente é
# uma exceção.
# Veja a seção 'Controle' para aprender mais sobre tratamento de exceção.
outra_variavel  # Gera uma exceção de erro de nome

# 'if' pode ser usado como uma expressão
"uepa!" if 3 > 2 else 2 #=> "uepa!"

# Listas armazenam sequências de elementos
lista = []
# Você pode inicializar uma lista com valores
outra_lista = [4, 5, 6]

# Adicione elementos no final da lista usando 'append'
lista.append(1)    # lista é agora [1]
lista.append(2)    # lista é agora [1, 2]
lista.append(4)    # lista é agora [1, 2, 4]
lista.append(3)    # lista é agora [1, 2, 4, 3]
# Remova elementos do fim da lista usando 'pop'
lista.pop()        #=> 3 e lista é agora [1, 2, 4]
# Vamos adicionar o elemento novamente
lista.append(3)    # lista agora é [1, 2, 4, 3] novamente.

# Acesse elementos de uma lista através de seu índices
lista[0] #=> 1
# Acesse o último elemento com índice negativo!
lista[-1] #=> 3

# Tentar acessar um elemento fora dos limites da lista gera uma exceção
# do tipo 'IndexError'
lista[4] # Gera uma exceção 'IndexError'

# Você pode acessar vários elementos ao mesmo tempo usando a sintaxe de
# limites
# (Para quem gosta de matemática, isso é um limite fechado/aberto)
lista[1:3] #=> [2, 4]
# Você pode omitir o fim se quiser os elementos até o final da lista
lista[2:] #=> [4, 3]
# O mesmo para o início
lista[:3] #=> [1, 2, 4]

# Remova um elemento qualquer de uma lista usando 'del'
del lista[2] # lista agora é [1, 2, 3]

# Você pode somar listas (obs: as listas originais não são modificadas)
lista + outra_lista #=> [1, 2, 3, 4, 5, 6]

# Você também pode concatenar usando o método 'extend' (lista será modificada!)
lista.extend(outra_lista) # Agora lista é [1, 2, 3, 4, 5, 6]

# Para checar se um elemento pertence a uma lista, use 'in'
1 in lista #=> True

# Saiba quantos elementos uma lista possui com 'len'
len(lista) #=> 6


# Tuplas são iguais a listas, mas são imutáveis
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # Isso gera uma exceção do tipo TypeError

# Você pode fazer nas tuplas todas aquelas coisas fez com a lista
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Você pode 'desempacotar' tuplas (ou listas) em variáveis, associando cada
# elemento da tupla/lista a uma variável correspondente
a, b, c = (1, 2, 3)     # a agora é 1, b agora é 2, c agora é 3
# Tuplas são criadas por padrão, mesmo se você não usar parênteses
d, e, f = 4, 5, 6
# Sabendo disso, veja só como é fácil trocar os valores de duas variáveis!
e, d = d, e     # d agora é 5, e agora é 4


# Dicionários armazenam 'mapeamentos' (do tipo chave-valor)
dicionario_vazio = {}
# Aqui criamos um dicionário já contendo valores
dicionario = {"um": 1, "dois": 2, "três": 3}

# Acesse valores usando []
dicionario["um"] #=> 1

# Retorna uma lista com todas as chaves do dicionário
dicionario.keys() #=> ["três", "dois", "um"]
# Nota: A ordem das chaves não é garantida.
# O resultado no seu interpretador não necessariamente será igual a esse.

# Retorna uma lista com todos os valores do dicionário
dicionario.values() #=> [3, 2, 1]
# Nota: A mesma nota acima sobre a ordenação é válida aqui.

# Veja se uma chave qualquer está em um dicionário usando 'in'
"um" in dicionario #=> True
1 in dicionario #=> False

# Tentar acessar uma chave que não existe gera uma exceção do tipo 'KeyError'
dicionario["quatro"] # Gera uma exceção KeyError

# Você pode usar o método 'get' para evitar gerar a exceção 'KeyError'.
# Ao invés de gerar essa exceção, irá retornar 'None' se a chave não existir.
dicionario.get("um") #=> 1
dicionario.get("quatro") #=> None
# O método 'get' suporta um argumento que diz qual valor deverá ser
# retornado se a chave não existir (ao invés de 'None').
dicionario.get("um", 4) #=> 1
dicionario.get("quatro", 4) #=> 4

# O método 'setdefault' é um jeito seguro de adicionar um novo par
# chave-valor a um dicionário, associando um valor padrão imutável à uma chave
dicionario.setdefault("cinco", 5) # dicionario["cinco"] é definido como 5
dicionario.setdefault("cinco", 6) # dicionario["cinco"] ainda é igual a 5


# Conjuntos (ou sets) armazenam ... bem, conjuntos
# Nota: lembre-se que conjuntos não admitem elementos repetidos!
conjunto_vazio = set()
# Podemos inicializar um conjunto com valores
conjunto = set([1, 2, 2, 3, 4]) # conjunto é set([1, 2, 3, 4]), sem repetição!

# Desde o Python 2.7, {} pode ser usado para declarar um conjunto
conjunto = {1, 2, 2, 3, 4} # => {1 2 3 4}

# Adicione mais ítens a um conjunto com 'add'
conjunto.add(5) # conjunto agora é {1, 2, 3, 4, 5}

# Calcule a intersecção de dois conjuntos com &
outro_conj = {3, 4, 5, 6}
conjunto & outro_conj #=> {3, 4, 5}

# Calcule a união de dois conjuntos com |
conjunto | outro_conj #=> {1, 2, 3, 4, 5, 6}

# E a diferença entre dois conjuntos com -
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Veja se um elemento existe em um conjunto usando 'in'
2 in conjunto #=> True
10 in conjunto #=> False


####################################################
## 3. Controle
####################################################

# Para começar, vamos apenas criar uma variável
alguma_var = 5

# Aqui está uma expressão 'if'. Veja como a identação é importante em Python!
# Esses comandos irão imprimir "alguma_var é menor que 10"
if alguma_var > 10:
    print "some_var é maior que 10."
elif some_var < 10:    # Esse 'elif' é opcional
    print "some_var é menor que 10."
else:           # Esse 'else' também é opcional
    print "some_var é igual a 10."


"""
Laços (ou loops) 'for' iteram em listas.
Irá imprimir:
    cachorro é um mamífero
    gato é um mamífero
    rato é um mamífero
"""
for animal in ["cachorro", "gato", "rato"]:
    # Você pode usar % para interpolar strings formatadas
    print "%s é um mamífero" % animal
    
"""
A função `range(um número)` retorna uma lista de números
do zero até o número dado.
Irá imprimir:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
Laços 'while' executam enquanto uma condição dada for verdadeira.
Irá imprimir:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Isso é um atalho para a expressão x = x + 1

# Tratamos excessões usando o bloco try/except
# Funciona em Python 2.6 e versões superiores:

try:
    # Use 'raise' para gerar um erro
    raise IndexError("Isso é um erro de índice")
except IndexError as e:
    pass    # Pass é um operador que não faz nada, deixa passar.
            # Usualmente você iria tratar a exceção aqui...


####################################################
## 4. Funções
####################################################

# Use 'def' para definir novas funções
def soma(x, y):
    print "x é %s e y é %s" % (x, y)
    return x + y    # Retorne valores usando 'return'

# Chamando funções com parâmetros
soma(5, 6) #=> imprime "x é 5 e y é 6" e retorna o valor 11

# Um outro jeito de chamar funções é especificando explicitamente os valores
# de cada parâmetro com chaves
soma(y=6, x=5)   # Argumentos com chaves podem vir em qualquer ordem.

# Você pode definir funções que recebem um número qualquer de argumentos
# (respeitando a sua ordem)
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# Você também pode definir funções que recebem um número qualquer de argumentos
# com chaves
def args_com_chaves(**ch_args):
    return ch_args

# Vamos chamar essa função para ver o que acontece
args_com_chaves(pe="grande", lago="Ness") #=> {"pe": "grande", "lago": "Ness"}

# Você pode fazer as duas coisas ao mesmo tempo, se desejar
def todos_args(*args, **ch_wargs):
    print args
    print ch_args
"""
todos_args(1, 2, a=3, b=4) imprime:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Quando você chamar funções, pode fazer o oposto do que fizemos até agora!
# Podemos usar * para expandir tuplas de argumentos e ** para expandir
# dicionários de argumentos com chave.
args = (1, 2, 3, 4)
ch_args = {"a": 3, "b": 4}
todos_args(*args)  # equivalente a todos_args(1, 2, 3, 4)
todos_args(**ch_args) # equivalente a todos_args(a=3, b=4)
todos_args(*args, **ch_args) # equivalente a todos_args(1, 2, 3, 4, a=3, b=4)

# Em Python, funções são elementos de primeira ordem (são como objetos, 
# strings ou números)
def cria_somador(x):
    def somador(y):
        return x + y
    return somador

soma_10 = cria_somador(10)
soma_10(3) #=> 13

# Desta forma, existem também funções anônimas
(lambda x: x > 2)(3) #=> True

# E existem funções de alta ordem por padrão
map(soma_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]
reduce(lambda x, y: x + y, [3, 4, 5, 6, 7]) #=> 25

# Nós podemos usar compreensão de listas para mapear e filtrar também
[soma_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Classes
####################################################

# Para criar uma nova classe, devemos herdar de 'object'
class Humano(object):

    # Um atributo de classe. Ele é compartilhado por todas as instâncias dessa
    # classe
    especie = "H. sapiens"

    # Definimos um inicializador básico
    def __init__(self, nome):
        # Atribui o valor de argumento dado a um atributo da instância
        self.nome = nome

    # Um método de instância. Todos os métodos levam 'self' como primeiro
    # argumento
    def diga(self, msg):
       return "%s: %s" % (self.nome, msg)

    # Um método de classe é compartilhado por todas as instâncias
    # Eles são chamados passando o nome da classe como primeiro argumento
    @classmethod
    def get_especie(cls):
        return cls.especie

    # Um método estático é chamado sem uma referência a classe ou instância
    @staticmethod
    def ronca():
        return "*arrrrrrr*"


# Instancie uma classe
i = Humano(nome="Ivone")
print i.diga("oi")     # imprime "Ivone: oi"

j = Human("Joel")
print j.say("olá")  #prints out "Joel: olá"

# Chame nosso método de classe
i.get_especie() #=> "H. sapiens"

# Modifique um atributo compartilhado
Humano.especie = "H. neanderthalensis"
i.get_especie() #=> "H. neanderthalensis"
j.get_especie() #=> "H. neanderthalensis"

# Chame o método estático
Humano.ronca() #=> "*arrrrrrr*"


####################################################
## 6. Módulos
####################################################

# Você pode importar módulos
import math
print math.sqrt(16) #=> 4

# Você pode importar funções específicas de um módulo
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# Você também pode importar todas as funções de um módulo
# Atenção: isso não é recomendado!
from math import *

# Você pode usar apelidos para os módulos, encurtando seus nomes
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Módulos em Python são apenas arquivos Python. Você
# pode escrever o seu próprio módulo e importá-lo. O nome do
# módulo será o mesmo que o nome do arquivo.

# Você pode descobrir quais funções e atributos
# estão definidos em um módulo qualquer.
import math
dir(math)


```

## Pronto para mais?

### Online e gratuito

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)

### Livros impressos

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
translators:
    - ["Paulo Henrique Rodrigues Pinheiro", "http://www.sysincloud.it"]
lang: pt-br
filename: learnpython3-pt.py
---

Python foi criado por Guido Van Rossum nos anos 1990. Ele é atualmente uma
das mais populares linguagens em existência. Eu fiquei morrendo de amor
pelo Python por sua clareza sintática. É praticamente pseudocódigo executável.

Suas opiniões são grandemente apreciadas. Você pode encontrar-me em
[@louiedinh](http://twitter.com/louiedinh) ou louiedinh [em]
[serviço de e-mail do google].

Observação: Este artigo trata de Python 3 especificamente. Verifique
[aqui](http://learnxinyminutes.com/docs/pt-br/python-pt/) se você pretende
aprender o velho Python 2.7.

```python

# Comentários em uma única linha começam com uma cerquilha (também conhecido por sustenido).

""" Strings de várias linhas podem ser escritas
    usando três ", e são comumente usadas
    como comentários.
"""

####################################################
## 1. Tipos de dados primitivos e operadores
####################################################

# Você usa números normalmente
3  # => 3

# Matemática é como você espera que seja
1 + 1   # => 2
8 - 1   # => 7
10 * 2  # => 20

# Números inteiros por padrão, exceto na divisão, que retorna número
# de ponto flutuante (float).
35 / 5  # => 7.0

# O resultado da divisão inteira arredonda para baixo tanto para números
# positivos como para negativos.
5 // 3       # => 1
5.0 // 3.0   # => 1.0 # funciona em float também
-5 // 3      # => -2
-5.0 // 3.0  # => -2.0

# Quando você usa um float, o resultado é float.
3 * 2.0  # => 6.0

# operador módulo
7 % 3  # => 1

# Exponenciação (x**y, x elevado à potência y)
2**4  # => 16

# Determine a precedência usando parêntesis
(1 + 3) * 2  # => 8

# Valores lógicos são primitivos (Atenção à primeira letra maiúscula)
True
False

# negação lógica com not
not True   # => False
not False  # => True

# Operadores lógicos
# Observe que "and" e "or" são sensíveis a maiúsculas e minúsculas
True and False  # => False
False or True   # => True

# Observe a utilização de operadores lógicos com números inteiros
0 and 2     # => 0
-5 or 0     # => -5
0 == False  # => True
2 == True   # => False
1 == True   # => True

# Igualdade é ==
1 == 1  # => True
2 == 1  # => False

# Diferença é !=
1 != 1  # => False
2 != 1  # => True

# Mais comparações
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# Comparações podem ser agrupadas
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# (operador 'is' e operador '==') is verifica se duas referenciam um
# mesmo objeto, mas == verifica se as variáveis apontam para o
# mesmo valor.
a = [1, 2, 3, 4]  # Referência a uma nova lista, [1, 2, 3, 4]
b = a             # b referencia o que está referenciado por a
b is a            # => True, a e b referenciam o mesmo objeto
b == a            # => True, objetos a e b tem o mesmo conteúdo
b = [1, 2, 3, 4]  # Referência a uma nova lista, [1, 2, 3, 4]
b is a            # => False, a e b não referenciam o mesmo objeto
b == a            # => True, objetos a e b tem o mesmo conteúdo

# Strings são criadas com " ou '
"Isto é uma string."
'Isto também é uma string.'

# Strings também podem ser somadas! Mas tente não fazer isso.
"Olá " + "mundo!"  # => "Olá mundo!"
# Strings podem ser somadas sem usar o '+'
"Olá " "mundo!"    # => "Olá mundo!"

# Uma string pode ser manipulada como se fosse uma lista de caracteres
"Isso é uma string"[0]  # => 'I'

# .format pode ser usado para formatar strings, dessa forma:
"{} podem ser {}".format("Strings", "interpoladas")  # => "Strings podem ser interpoladas"

# Você pode repetir os argumentos para digitar menos.
"Seja ágil {0}, seja rápido {0}, salte sobre o {1} {0}".format("Jack", "castiçal")
# => "Seja ágil Jack, seja rápido Jack, salte sobre o castiçal Jack."

# Você pode usar palavras-chave se quiser contar.
"{nome} quer comer {comida}".format(nome="Beto", comida="lasanha")  # => "Beto quer comer lasanha"

# Se você precisa executar seu código Python3 com um interpretador Python 2.5 ou acima, você pode usar a velha forma para formatação de texto:
"%s podem ser %s da forma %s" % ("Strings", "interpoladas", "antiga")  # => "Strings podem ser interpoladas da forma antiga"


# None é um objeto
None  # => None

# Não use o operador de igualdade "==" para comparar objetos com None
# Use "is" para isso. Ele checará pela identidade dos objetos.
"etc" is None  # => False
None is None   # => True

# None, 0, e strings/listas/dicionários vazios todos retornam False.
# Qualquer outra coisa retorna True
bool(0)   # => False
bool("")  # => False
bool([])  # => False
bool({})  # => False


####################################################
## 2. Variáveis e coleções
####################################################

# Python tem uma função print
print("Eu sou o Python. Prazer em conhecer!")  # => Eu sou o Python. Prazer em conhecer!

# Por padrão a função print também imprime o caractere de nova linha ao final.
# Use o argumento opcional end para mudar o caractere final.
print("Olá, Mundo", end="!")  # => Olá, Mundo!

# Forma simples para capturar dados de entrada via console
input_string_var = input("Digite alguma coisa: ") # Retorna o que foi digitado em uma string
# Observação: Em versões antigas do Python, o método input() era chamado raw_input()

# Não é necessário declarar variáveis antes de iniciá-las
# È uma convenção usar letras_minúsculas_com_sublinhados
alguma_variavel = 5
alguma_variavel  # => 5

# Acessar uma variável que não tenha sido inicializada gera uma exceção.
# Veja Controle de Fluxo para aprender mais sobre tratamento de exceções.
alguma_variavel_nao_inicializada  # Gera a exceção NameError

# Listas armazenam sequencias
li = []
# Você pode iniciar com uma lista com alguns valores
outra_li = [4, 5, 6]

# Adicionar conteúdo ao fim da lista com append
li.append(1)    # li agora é [1]
li.append(2)    # li agora é [1, 2]
li.append(4)    # li agora é [1, 2, 4]
li.append(3)    # li agora é [1, 2, 4, 3]
# Remover do final da lista com pop
li.pop()        # => 3 e agora li é [1, 2, 4]
# Vamos colocá-lo lá novamente!
li.append(3)    # li agora é [1, 2, 4, 3] novamente.

# Acessar uma lista da mesma forma que você faz com um array
li[0]   # => 1
# Acessa o último elemento
li[-1]  # => 3

# Acessando além dos limites gera um IndexError
li[4]  # Gera o IndexError

# Você pode acessar vários elementos com a sintaxe de limites
# (É um limite fechado, aberto pra você que gosta de matemática.)
li[1:3]   # => [2, 4]
# Omitindo o final
li[2:]    # => [4, 3]
# Omitindo o início
li[:3]    # => [1, 2, 4]
# Selecione cada segunda entrada
li[::2]   # => [1, 4]
# Tenha uma cópia em ordem invertida da lista
li[::-1]  # => [3, 4, 2, 1]
# Use qualquer combinação dessas para indicar limites complexos
# li[inicio:fim:passo]

# Faça uma cópia profunda de um nível usando limites
li2 = li[:]  # => li2 = [1, 2, 4, 3] mas (li2 is li) resultará em False.

# Apague elementos específicos da lista com "del"
del li[2]  # li agora é [1, 2, 3]

# Você pode somar listas
# Observação: valores em li e other_li não são modificados.
li + other_li  # => [1, 2, 3, 4, 5, 6]

# Concatene listas com "extend()"
li.extend(other_li)  # Agora li é [1, 2, 3, 4, 5, 6]

# Verifique se algo existe na lista com "in"
1 in li  # => True

# Examine  tamanho com "len()"
len(li)  # => 6


# Tuplas são como l istas, mas imutáveis.
tup = (1, 2, 3)
tup[0]      # => 1
tup[0] = 3  # Gera um TypeError

# Observe que uma tupla de tamanho um precisa ter uma vírgula depois do
# último elemento mas tuplas de outros tamanhos, mesmo vazias, não precisa,.
type((1))   # => <class 'int'>
type((1,))  # => <class 'tuple'>
type(())    # => <class 'tuple'>

# Você pode realizar com tuplas a maior parte das operações que faz com listas
len(tup)         # => 3
tup + (4, 5, 6)  # => (1, 2, 3, 4, 5, 6)
tup[:2]          # => (1, 2)
2 in tup         # => True

# Você pode desmembrar tuplas (ou listas) em variáveis.
a, b, c = (1, 2, 3)  # a é 1, b é 2 e c é 3
# Por padrão, tuplas são criadas se você não coloca parêntesis.
d, e, f = 4, 5, 6
# Veja como é fácil permutar dois valores
e, d = d, e  # d é 5, e é 4

# Dicionários armazenam mapeamentos
empty_dict = {}
# Aqui está um dicionário preenchido na definição da referência
filled_dict = {"um": 1, "dois": 2, "três": 3}

# Observe que chaves para dicionários devem ser tipos imutáveis. Isto é para
# assegurar que a chave pode ser convertida para uma valor hash constante para
# buscas rápidas.
# Tipos imutáveis incluem inteiros, flotas, strings e tuplas.
invalid_dict = {[1,2,3]: "123"}  # => Gera um TypeError: unhashable type: 'list'
valid_dict = {(1,2,3):[1,2,3]}   # Já os valores, podem ser de qualquer tipo.

# Acesse valores com []
filled_dict["um"]  # => 1

# Acesse todas as chaves como um iterável com "keys()". É necessário encapsular
# a chamada com um list() para transformá-las em uma lista. Falaremos sobre isso
# mais adiante. Observe que a ordem de uma chave de dicionário não é garantida.
# Por isso, os resultados aqui apresentados podem não ser exatamente como os
# aqui apresentados.
list(filled_dict.keys())  # => ["três", "dois", "um"]


# Acesse todos os valores de um iterável com "values()". Novamente, é
# necessário encapsular ele com list() para não termos um iterável, e sim os
# valores. Observe que, como foi dito acima, a ordem dos elementos não é
# garantida.
list(filled_dict.values())  # => [3, 2, 1]


# Verifique a existência de chaves em um dicionário com "in"
"um" in filled_dict  # => True
1 in filled_dict      # => False

# Acessar uma chave inexistente gera um KeyError
filled_dict["quatro"]  # KeyError

# Use o método "get()" para evitar um KeyError
filled_dict.get("um")      # => 1
filled_dict.get("quatro")     # => None
# O método get permite um parâmetro padrão para quando não existir a chave
filled_dict.get("um", 4)   # => 1
filled_dict.get("quatro", 4)  # => 4

# "setdefault()" insere em dicionário apenas se a dada chave não existir
filled_dict.setdefault("cinco", 5)  # filled_dict["cinco"] tem valor 5
filled_dict.setdefault("cinco", 6)  # filled_dict["cinco"] continua 5

# Inserindo em um dicionário
filled_dict.update({"quatro":4})  # => {"um": 1, "dois": 2, "três": 3, "quatro": 4}
#filled_dict["quatro"] = 4        #outra forma de inserir em um dicionário

# Remova chaves de um dicionário com del
del filled_dict["um"]  # Remove a chave "um" de filled_dict


# Armazenamento em sets... bem, são conjuntos
empty_set = set()
# Inicializa um set com alguns valores. Sim, ele parece um dicionário. Desculpe.
some_set = {1, 1, 2, 2, 3, 4}  # some_set agora é {1, 2, 3, 4}

# Da mesma forma que chaves em um dicionário, elementos de um set devem ser
# imutáveis.
invalid_set = {[1], 1}  # => Gera um TypeError: unhashable type: 'list'
valid_set = {(1,), 1}

# Pode definir novas variáveis para um conjunto
filled_set = some_set

# Inclua mais um item no set
filled_set.add(5)  # filled_set agora é {1, 2, 3, 4, 5}

# Faça interseção de conjuntos com &
other_set = {3, 4, 5, 6}
filled_set & other_set  # => {3, 4, 5}

# Faça união de conjuntos com |
filled_set | other_set  # => {1, 2, 3, 4, 5, 6}

# Faça a diferença entre conjuntos com -
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# Verifique a existência em um conjunto com in
2 in filled_set   # => True
10 in filled_set  # => False



####################################################
## 3. Controle de fluxo e iteráveis
####################################################

# Iniciemos um variável
some_var = 5

# Aqui está uma expressão if. Indentação é significante em python!
# imprime "somevar é menor que10"
if some_var > 10:
    print("some_var é absolutamente maior que 10.")
elif some_var < 10:    # Esta cláusula elif é opcional.
    print("some_var é menor que 10.")
else:                  # Isto também é opcional.
    print("some_var é, de fato, 10.")


"""
Laços for iteram sobre listas
imprime:
    cachorro é um mamífero
    gato é um mamífero
    rato é um mamífero
"""
for animal in ["cachorro", "gato", "rato"]:
    # Você pode usar format() para interpolar strings formatadas
    print("{} é um mamífero".format(animal))

"""
"range(número)" retorna um iterável de números
de zero até o número escolhido
imprime:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
"range(menor, maior)" gera um iterável de números
começando pelo menor até o maior
imprime:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print(i)

"""
"range(menor, maior, passo)" retorna um iterável de números
começando pelo menor número até o maior númeno, pulando de
passo em passo. Se o passo não for indicado, o valor padrão é um.
imprime:
    4
    6
"""
for i in range(4, 8, 2):
    print(i)
"""

Laços while executam até que a condição não seja mais válida.
imprime:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Maneira mais curta para for x = x + 1

# Lide com exceções com um bloco try/except
try:
    # Use "raise" para gerar um erro
    raise IndexError("Isto é um erro de índice")
except IndexError as e:
    pass                 # Pass é um não-operador. Normalmente você usa algum código de recuperação aqui.
except (TypeError, NameError):
    pass                 # Varias exceções podem ser gerenciadas, se necessário.
else:                    # Cláusula opcional para o bloco try/except. Deve estar após todos os blocos de exceção.
    print("Tudo certo!")   # Executa apenas se o código em try não gera exceção
finally:                 #  Sempre é executado
    print("Nós podemos fazer o código de limpeza aqui.")

# Ao invés de try/finally para limpeza você pode usar a cláusula with
with open("myfile.txt") as f:
    for line in f:
        print(line)

# Python provê uma abstração fundamental chamada Iterável.
# Um iterável é um objeto que pode ser tratado como uma sequência.
# O objeto retornou a função range, um iterável.

filled_dict = {"um": 1, "dois": 2, "três": 3}
our_iterable = filled_dict.keys()
print(our_iterable)  # => range(1,10). Esse é um objeto que implementa nossa interface iterável.

# Nós podemos percorrê-la.
for i in our_iterable:
    print(i)  # Imprime um, dois, três

# Mas não podemos acessar os elementos pelo seu índice.
our_iterable[1]  # Gera um TypeError

# Um iterável é um objeto que sabe como criar um iterador.
our_iterator = iter(our_iterable)

# Nosso iterador é um objeto que pode lembrar o estado enquanto nós o percorremos.
# Nós acessamos o próximo objeto com "next()".
next(our_iterator)  # => "um"

# Ele mantém o estado enquanto nós o percorremos.
next(our_iterator)  # => "dois"
next(our_iterator)  # => "três"

# Após o iterador retornar todos os seus dados, ele gera a exceção StopIterator
next(our_iterator)  # Gera StopIteration

# Você pode capturar todos os elementos de um iterador aplicando list() nele.
list(filled_dict.keys())  # => Retorna ["um", "dois", "três"]


####################################################
## 4. Funções
####################################################

# Use "def" para criar novas funções.
def add(x, y):
    print("x é {} e y é {}".format(x, y))
    return x + y  # Retorne valores com a cláusula return

# Chamando funções com parâmetros
add(5, 6)  # => imprime "x é 5 e y é 6" e retorna 11

# Outro meio de chamar funções é com argumentos nomeados
add(y=6, x=5)  # Argumentos nomeados podem aparecer em qualquer ordem.

# Você pode definir funções que pegam um número variável de argumentos
# posicionais
def varargs(*args):
    return args

varargs(1, 2, 3)  # => (1, 2, 3)

# Você pode definir funções que pegam um número variável de argumentos nomeados
# também
def keyword_args(**kwargs):
    return kwargs

# Vamos chamá-lo para ver o que acontece
keyword_args(peh="grande", lago="ness")  # => {"peh": "grande", "lago": "ness"}


# Você pode fazer ambos simultaneamente, se você quiser
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) imprime:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Quando chamar funções, você pode fazer o oposto de args/kwargs!
# Use * para expandir tuplas e use ** para expandir dicionários!
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)            # equivalente a foo(1, 2, 3, 4)
all_the_args(**kwargs)         # equivalente a foo(a=3, b=4)
all_the_args(*args, **kwargs)  # equivalente a foo(1, 2, 3, 4, a=3, b=4)

# Retornando múltiplos valores (com atribuição de tuplas)
def swap(x, y):
    return y, x  # Retorna múltiplos valores como uma tupla sem os parêntesis.
                 # (Observação: os parêntesis foram excluídos mas podem estar
                 # presentes)

x = 1
y = 2
x, y = swap(x, y)     # => x = 2, y = 1
# (x, y) = swap(x,y)  # Novamente, os parêntesis foram excluídos mas podem estar presentes.

# Escopo de função
x = 5

def setX(num):
    # A variável local x não é a mesma variável global x
    x = num    # => 43
    print (x)  # => 43

def setGlobalX(num):
    global x
    print (x)  # => 5
    x = num    # variável global x agora é 6
    print (x)  # => 6

setX(43)
setGlobalX(6)


# Python tem funções de primeira classe
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)   # => 13

# Também existem as funções anônimas
(lambda x: x > 2)(3)                  # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1)  # => 5

# TODO - Fix for iterables
# Existem funções internas de alta ordem
map(add_10, [1, 2, 3])          # => [11, 12, 13]
map(max, [1, 2, 3], [4, 2, 1])  # => [4, 2, 3]

filter(lambda x: x > 5, [3, 4, 5, 6, 7])  # => [6, 7]

# Nós podemos usar compreensão de lista para interessantes mapas e filtros
# Compreensão de lista armazena a saída como uma lista que pode ser uma lista
# aninhada
[add_10(i) for i in [1, 2, 3]]         # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]  # => [6, 7]

####################################################
## 5. Classes
####################################################


# Nós usamos o operador "class" para ter uma classe
class Human:

    # Um atributo de classe. Ele é compartilhado por todas as instâncias dessa
    # classe.
    species = "H. sapiens"

    # Construtor básico, é chamado quando esta classe é instanciada.
    # Note que dois sublinhados no início e no final de uma identificados
    # significa objetos ou atributos que são usados pelo python mas vivem em
    # um namespace controlado pelo usuário. Métodos (ou objetos ou atributos)
    # como: __init__, __str__, __repr__, etc. são chamados métodos mágicos (ou
    # algumas vezes chamados métodos dunder - "double underscore")
    # Você não deve usar nomes assim por sua vontade.
    def __init__(self, name):
        @ Atribui o argumento ao atributo da  instância
        self.name = name

    # Um método de instância. Todos os métodos tem "self" como primeiro
    # argumento
    def say(self, msg):
        return "{name}: {message}".format(name=self.name, message=msg)

    # Um método de classe é compartilhado por todas as instâncias
    # Eles são chamados com a classe requisitante como primeiro argumento
    @classmethod
    def get_species(cls):
        return cls.species

    # Um método estático é chamado sem uma referência a classe ou instância
    @staticmethod
    def grunt():
        return "*grunt*"


# Instancie uma classe
i = Human(name="Ian")
print(i.say("oi"))     # imprime "Ian: oi"

j = Human("Joel")
print(j.say("olá"))  # imprime "Joel: olá"

# Chama nosso método de classe
i.get_species()  # => "H. sapiens"

# Altera um atributo compartilhado
Human.species = "H. neanderthalensis"
i.get_species()  # => "H. neanderthalensis"
j.get_species()  # => "H. neanderthalensis"

# Chama o método estático
Human.grunt()    # => "*grunt*"


####################################################
## 6. Módulos
####################################################

# Você pode importar módulos
import math
print(math.sqrt(16))  # => 4

# Você pode importar apenas funções específicas de um módulo
from math import ceil, floor
print(ceil(3.7))   # => 4.0
print(floor(3.7))  # => 3.0

# Você pode importar todas as funções de um módulo para o namespace atual
# Atenção: isso não é recomendado
from math import *

# Você pode encurtar o nome dos módulos
import math as m
math.sqrt(16) == m.sqrt(16)  # => True

# Módulos python são apenas arquivos python comuns. Você
# pode escrever os seus, e importá-los. O nome do
# módulo é o mesmo nome do arquivo.

# Você pode procurar que atributos e funções definem um módulo.
import math
dir(math)


####################################################
## 7. Avançado
####################################################

# Geradores podem ajudar você a escrever código "preguiçoso"
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# Um gerador cria valores conforme necessário.
# Ao invés de gerar e retornar todos os valores de uma só vez ele cria um em
# cada interação. Isto significa que valores maiores que 15 não serão
# processados em double_numbers.
# Nós usamos um sublinhado ao final do nome das variáveis quando queremos usar
# um nome que normalmente colide com uma palavra reservada do python.
range_ = range(1, 900000000)
# Multiplica por 2 todos os números até encontrar um resultado >= 30
for i in double_numbers(range_):
    print(i)
    if i >= 30:
        break


# Decoradores
# Neste exemplo beg encapsula say
# beg irá chamar say. Se say_please é verdade então ele irá mudar a mensagem
# retornada
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Por favor! Eu sou pobre :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Você me paga uma cerveja?"
    return msg, say_please


print(say())                # Você me paga uma cerveja?
print(say(say_please=True)) # Você me paga uma cerveja? Por favor! Eu sou pobre :(
```

## Pronto para mais?

### Free Online

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)
* [Python Course](http://www.python-course.eu/index.php)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)
* [A curated list of awesome Python frameworks, libraries and software](https://github.com/vinta/awesome-python)
* [30 Python Language Features and Tricks You May Not Know About](http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html)
* [Official Style Guide for Python](https://www.python.org/dev/peps/pep-0008/)

### Dead Tree

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
---
category: tool
tool: ruby ecosystem
contributors:
    - ["Jon Smock", "http://github.com/jonsmock"]
    - ["Rafal Chmiel", "http://github.com/rafalchmiel"]
translators:
    - ["Claudson Martins", "http://github.com/claudsonm"]
lang: pt-br

---

Pessoas utilizando Ruby geralmente têm uma forma de instalar diferentes versões
do Ruby, gerenciar seus pacotes (ou gemas) e as dependências das gemas.

## Gerenciadores Ruby

Algumas plataformas possuem o Ruby pré-instalado ou disponível como um pacote.
A maioria dos "rubistas" não os usam, e se usam, é apenas para inicializar outro
instalador ou implementação do Ruby. Ao invés disso, rubistas tendêm a instalar
um gerenciador para instalar e alternar entre diversas versões do Ruby e seus
ambientes de projeto.

Abaixo estão os gerenciadores Ruby mais populares:

* [RVM](https://rvm.io/) - Instala e alterna entre os rubies. RVM também possui
  o conceito de gemsets para isolar os ambientes dos projetos completamente.
* [ruby-build](https://github.com/sstephenson/ruby-build) - Apenas instala os
  rubies. Use este para um melhor controle sobre a instalação de seus rubies.
* [rbenv](https://github.com/sstephenson/rbenv) - Apenas alterna entre os rubies.
  Usado com o ruby-build. Use este para um controle mais preciso sobre a forma
  como os rubies são carregados.
* [chruby](https://github.com/postmodern/chruby) - Apenas alterna entre os rubies.
  A concepção é bastante similar ao rbenv. Sem grandes opções sobre como os
  rubies são instalados.

## Versões do Ruby

O Ruby foi criado por Yukihiro "Matz" Matsumoto, que continua a ser uma espécie
de [BDFL](https://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life), embora
isso esteja mudando recentemente. Como resultado, a implementação de referência
do Ruby é chamada de MRI (Matz' Reference Implementation), e quando você ver uma
versão do Ruby, ela está se referindo a versão de lançamento do MRI.

As três principais versões do Ruby em uso são:

* 2.0.0 - Lançada em Fevereiro de 2013. Maioria das principais bibliotecas e 
  suporte a frameworks 2.0.0.
* 1.9.3 - Lançada em Outubro de 2011. Está é a versão mais utilizada pelos rubistas
  atualmente. Também [aposentada](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/).
* 1.8.7 - O Ruby 1.8.7 foi
  [aposentado](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/).

A diferença entre a versão 1.8.7 para 1.9.x é muito maior do que a da 1.9.3 para
a 2.0.0. Por exemplo, a série 1.9 introduziu encodes e uma VM bytecode. Ainda
existem projetos na versão 1.8.7, mas eles estão tornando-se uma pequena minoria
pois a maioria da comunidade migrou para a versão, pelo menos, 1.9.2 ou 1.9.3.

## Implementações Ruby

O ecossistema Ruby conta com várias diferentes implementações do Ruby, cada uma
com pontos fortes e estados de compatibilidade. Para ser claro, as diferentes
implementações são escritas em diferentes linguagens, mas *todas elas são Ruby*.
Cada implementação possui hooks especiais e recursos extra, mas todas elas
executam arquivos normais do Ruby tranquilamente. Por exemplo, JRuby é escrita
em Java, mas você não precisa saber Java para utilizá-la.

Muito maduras/compatíveis:

* [MRI](https://github.com/ruby/ruby) - Escrita em C, esta é a implementação de
  referência do Ruby. Por definição, é 100% compatível (consigo mesma). Todos os
  outros rubies mantêm compatibilidade com a MRI (veja [RubySpec](#rubyspec) abaixo).
* [JRuby](http://jruby.org/) - Escrita em Java e Ruby, esta implementação
  robusta é um tanto rápida. Mais importante ainda, o ponto forte do JRuby é a
  interoperabilidade com JVM/Java, aproveitando ferramentas JVM, projetos, e
  linguagens existentes.
* [Rubinius](http://rubini.us/) - Escrita principalmente no próprio Ruby, com
  uma VM bytecode em C++. Também madura e rápida. Por causa de sua implementação
  em Ruby, ela expõe muitos recursos da VM na rubyland.

Medianamente maduras/compatíveis:

* [Maglev](http://maglev.github.io/) - Construída em cima da Gemstone, uma
  máquina virtual Smalltalk. O Smalltalk possui algumas ferramentas impressionantes,
  e este projeto tenta trazer isso para o desenvolvimento Ruby.
* [RubyMotion](http://www.rubymotion.com/) - Traz o Ruby para o desenvolvimento iOS.

Pouco maduras/compatíveis:

* [Topaz](http://topazruby.com/) - Escrita em RPython (usando o conjunto de
  ferramentas PyPy), Topaz é bastante jovem e ainda não compatível. Parece ser
  promissora como uma implementação Ruby de alta performance.
* [IronRuby](http://ironruby.net/) - Escrita em C# visando a plataforma .NET,
  o trabalho no IronRuby parece ter parado desde que a Microsoft retirou seu apoio.

Implementações Ruby podem ter seus próprios números de lançamento, mas elas
sempre focam em uma versão específica da MRI para compatibilidade. Diversas
implementações têm a capacidade de entrar em diferentes modos (1.8 ou 1.9, por
exemplo) para especificar qual versão da MRI focar.

## RubySpec

A maioria das implementações Ruby dependem fortemente da [RubySpec](http://rubyspec.org/).
Ruby não tem uma especificação oficial, então a comunidade tem escrito
especificações executáveis em Ruby para testar a compatibilidade de suas
implementações com a MRI.

## RubyGems

[RubyGems](http://rubygems.org/) é um gerenciador de pacotes para Ruby mantido
pela comunidade. RubyGems vem com o Ruby, portanto não é preciso baixar separadamente.

Os pacotes do Ruby são chamados de "gemas", e elas podem ser hospedadas pela 
comunidade em RubyGems.org. Cada gema contém seu código-fonte e alguns metadados,
incluindo coisas como versão, dependências, autor(es) e licença(s).

## Bundler

[Bundler](http://bundler.io/) é um gerenciador de dependências para as gemas.
Ele usa a Gemfile de um projeto para encontrar dependências, e então busca as
dependências dessas dependências de forma recursiva. Ele faz isso até que todas 
as dependências sejam resolvidas e baixadas, ou para se encontrar um conflito.

O Bundler gerará um erro se encontrar um conflito entre dependências. Por exemplo, 
se a gema A requer versão 3 ou maior que a gema Z, mas a gema B requer a versão 
2, o Bundler irá notificá-lo que há um conflito. Isso se torna extremamente útil
quando diversas gemas começam a referenciar outras gemas (que referem-se a outras
gemas), o que pode formar uma grande cascata de dependências a serem resolvidas.

# Testes

Testes são uma grande parte da cultura do Ruby. O Ruby vem com o seu próprio 
framework de teste de unidade chamado minitest (ou TestUnit para Ruby versão 1.8.x). 
Existem diversas bibliotecas de teste com diferentes objetivos.

* [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html) - 
  Framework de testes "Unit-style" para o Ruby 1.8 (built-in)
* [minitest](http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html) - 
  Framework de testes para o Ruby 1.9/2.0 (built-in)
* [RSpec](http://rspec.info/) - Um framework de testes que foca na expressividade
* [Cucumber](http://cukes.info/) - Um framework de testes BDD que analisa testes Gherkin formatados

## Seja Legal

A comunidade Ruby orgulha-se de ser uma comunidade aberta, diversa, e receptiva.
O próprio Matz é extremamente amigável, e a generosidade dos rubistas em geral
é incrível.
---
language: ruby
lang: pt-br
filename: learnruby-pt.rb
contributors:
  - ["Bruno Henrique - Garu", "http://garulab.com"]
  - ["Jean Matheus Souto", "http://jeanmatheussouto.github.io"]
translators:
  - ["Katyanna Moura", "https://twitter.com/amelie_kn"]
  - ["Alan Peterson Carvalho Silva", "https://twitter.com/DemonKart"]
---

```ruby
# Isso é um comentário

=begin
Isso é um comentário multilinha
Ninguém os usa
Você não deve usar também
=end

# Primeiro e principal: Tudo é um objeto.

# Números são objetos

3.class #=> Fixnum

3.to_s #=> "3"


# Um pouco de aritmética básica

1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# Aritmética é apenas açúcar sintático
# para chamar um método de um objeto
1.+(3) #=> 4
10.* 5 #=> 50

# Valores especiais são objetos
nil # Nada para ver aqui
true # verdadeiro
false # falso

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Igualdade
1 == 1 #=> true
2 == 1 #=> false

# Desigualdade
1 != 1 #=> false
2 != 1 #=> true
!true  #=> false
!false #=> true

# além de 'false', 'nil' é o único outro valor falso

!nil   #=> true
!false #=> true
!0     #=> false

# Mais comparações
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Strings são objects

'Eu sou uma string'.class #=> String
"Eu também sou uma string".class #=> String

placeholder = "usar interpolação de string"
"Eu posso #{placeholder} quando estiver usando aspas duplas"
#=> "Eu posso usar insterpolação de string quando estiver usando aspas duplas"

# imprime para output (saída)
puts "Estou imprimindo"

# Variáveis
x = 25 #=> 25
x #=> 25

# Note que uma atribuição retorna o valor atribuido
# Isso significa que você pode fazer múltiplas atribuições:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Por convenção, use snake_case para nomes de variáveis
snake_case = true

# Use nomes de variáveis descritivos
caminho_para_a_raiz_do_projeto = '/bom/nome/'
caminho = '/nome/ruim/'

# Símbolos (são objetos)
# Símbolos são imutáveis, são constantes reutilizáveis representados
# internamente por um valor inteiro. Eles são frequentemente usados no
# lugar de strings para transmitir com eficiência os valores específicos
# e significativos

:pendente.class #=> Symbol

status = :pendente

status == :pendente #=> true

status == 'pendente' #=> false

status == :aprovado #=> false

# Arrays

# Isso é um array
[1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Arrays podem conter diferentes tipos de itens

array = [1, "Oi", false] #=> => [1, "Oi", false]

# Arrays podem ser indexados
# a partir do começo
array[0] #=> 1
array[12] #=> nil

# Como aritmética, o acesso via [var]
# é apenas açúcar sintático
# para chamar o método [] de um objeto
array.[] 0 #=> 1
array.[] 12 #=> nil

# a partir do final
array[-1] #=> 5

# Com um índice de começo e fim
array[2, 4] #=> [3, 4, 5]

# Ou com um intervalo de valores
array[1..3] #=> [2, 3, 4]

# Adicionar a um array como este
array << 6 #=> [1, 2, 3, 4, 5, 6]

# Hashes são o principal dicionário de Ruby com pares de chaves(keys)/valor(value).
# Hashes são simbolizados com chaves "{}"
hash = {'cor' => 'verde', 'numero' => 5}

hash.keys #=> ['cor', 'numero']

# Hashes podem ser rapidamente pesquisados pela chave (key)
hash['cor'] #=> 'verde'
hash['numero'] #=> 5

# Procurar em um hash por uma chave que não existe retorna nil:
hash['nada aqui'] #=> nil

# Interar sobre hashes com o método #each:

hash.each do |k, v|
  puts "#{k} é #{v}"
end

# Desde o Ruby 1.9, temos uma sintaxe especial quando usamos símbolos como chaves (keys)

novo_hash = {defcon: 3, acao: true}

novo_hash.keys #=> [:defcon, :acao]

# Dica: Tanto Arrays quanto Hashes são Enumerable.
# Eles compartilham um monte de métodos úteis como each, map, count e mais

# Estruturas de controle

if true
  "Se verdadeiro"
elsif false
 "else if, opcional"
else
 "else, também é opcional"
end

for contador in 1..5
  puts "interação #{contador}"
end
#=> contador 1
#=> contador 2
#=> contador 3
#=> contador 4
#=> contador 5

# PORÉM
# Ninguém usa para loops
# Use "each" em vez, dessa forma:

(1..5).each do |contador|
  puts "interação #{contador}"
end
#=> contador 1
#=> contador 2
#=> contador 3
#=> contador 4
#=> contador 5

contador = 1
while contador <= 5 do
  puts "interação #{contador}"
  contador += 1
end
#=> contador 1
#=> contador 2
#=> contador 3
#=> contador 4
#=> contador 5

grau = 'B'

case grau
when 'A'
  puts "Um longo caminho a percorrer, pequeno gafanhoto"
when 'B'
  puts "Melhor sorte da próxima vez"
when 'C'
  puts "Você pode fazer melhor"
when 'D'
  puts "Scraping through"
when 'F'
  puts "Você falhou"
else
  puts "Alternative grading system, eh?"
end

# Funções

def dobrar(x)
  x * 2
end

# Funções (e todos os blocos) retornam implicitamente o valor da última linha
dobrar(2) #=> 4

# Parênteses são opicionais onde o resultado é claro
dobrar 3 #=> 6

dobrar dobrar 3 #=> 12

def somar(x,y)
  x + y
end

# Argumentos de métodos são separados por uma vírgula
somar 3, 4 #=> 7

somar(3,4), 5 #=> 12

# yield
# Todos os métodos possuem implicitamente um paramêtro opcional que é um bloco
# ele pode ser chamado com a palavra chave 'yield'

def ao_redor
  puts "{"
  yield
  puts "}"
end

ao_redor { puts 'Olá mundo' }

# {
# Olá mundo
# }


# Define uma classe com a palavra chave 'class'

class Humano

  # Uma variável de classe. Ela é compartilhada por todas as instâncias dessa classe
  @@especies = "H. sapiens"

  # Inicialização básica (contructor)
  def initialize(nome, idade=0)
    # Atribui o argumento para a variável de instância "nome" do objeto
    @nome = nome
    # Se a idade não for passada, nós definimos um valor padrão na lista de argumentos
    @idade = idade
  end

  # Método básico para atribuir valor
  def nome=(nome)
    @nome = nome
  end

  # Método básico de resgatar valor
  def nome
    @nome
  end

  # Um método de classe usa a palavra chave self para se diferenciar dos métodos de instância.
  # Ele só pode ser chamado na classe, não na instancia
  def self.diz(msg)
    puts "#{msg}"
  end

  def especies
    @@especies
  end

end


# Instanciando uma classe
jim = Humano.new("Jim Halpert")

dwight = Humano.new("Dwight K. Schrute")

# Vamos chamar um par de métodos
jim.especies #=> "H. sapiens"

jim.nome #=> "Jim Halpert"

jim.nome = "Jim Halpert II" #=> "Jim Halpert II"

jim.nome #=> "Jim Halpert II"

dwight.especies #=> "H. sapiens"

dwight.nome #=> "Dwight K. Schrute"

# Chamar o método de classe
Humano.diz("Oi") #=> "Oi"

# Uma classe também é objeto em Ruby. Então uma classe pode possuir variável de instância
# Variáveis de classe são compartilhadas entre a classe e todos os seus descendentes.


# Classe base
class Humano
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

#  classe filha
class Trabalhador < Humano
end

Humano.foo # 0
Trabalhador.foo # 0

Humano.foo = 2 # 2
Trabalhador.foo # 2

# Uma variável de instância não é compartilhada por suas classes descendentes.

class Humano
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doutor < Humano
end

Humano.bar # 0
Doutor.bar # nil

---

module ModuloDeExemplo
  def foo
    'foo'
  end
end

# Incluir (include) módulos conecta seus métodos às instâncias da classe 
# Herdar (extend) módulos conecta seus métodos à classe em si

class Pessoa
  include ExemploDeModulo
end

class Livro
  extend ExemploDeModulo
end

Pessoa.foo     # => NoMethodError: undefined method `foo' for Pessoa:Class
Pessoa.new.foo # => 'foo'
Livro.foo       # => 'foo'
Livro.new.foo   # => NoMethodError: undefined method `foo'

# Callbacks são executados ao incluir e herdar um módulo

module ExemploDeConceito
  def self.included(base)
    base.extend(MetodosDeClasse)
    base.send(:include, MetodosDeInstancia)
  end

  module MetodosDeClasse
    def bar
      'bar'
    end
  end

  module MetodosDeInstancia
    def qux
      'qux'
    end
  end
end

class Algo
  include ExemploDeConceito
end

Algo.bar     # => 'bar'
Algo.qux     # => NoMethodError: undefined method `qux'
Algo.new.bar # => NoMethodError: undefined method `bar'
Algo.new.qux # => 'qux'
```

## Recursos adicionais

- [Aprenda Ruby com desafios](http://www.learneroo.com/modules/61/nodes/338) - Uma coleção de desafios para testar a linguagem.
- [Documentação oficial](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby a partir de outras linguagens](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/)- Um mais antigo [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) e tambem uma versão online disponível.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - Uma versão colaborativa de um *style-guide*
---
language: sass
filename: learnsass-pt.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
  - ["Sean Corrales", "https://github.com/droidenator"]
translators:
  - ["Gabriel Gomes", "https://github.com/gabrielgomesferraz"]
  - ["Cássio Böck", "https://github.com/cassiobsilva"]
lang: pt-br
---

Sass é uma linguagem de extensão CSS que adiciona recursos, como variáveis, aninhamento, mixins e muito mais.
Sass (e outros pré-processadores, como [Less](http://lesscss.org/)) ajudam os desenvolvedores a escrever código de fácil manutenção e DRY (Do not Repeat Yourself).

Sass tem duas opções de sintaxe diferentes para escolher. SCSS, que tem a mesma sintaxe de CSS, mas com os recursos adicionais de Sass. Ou Sass (a sintaxe original), que usa o recuo, em vez de chaves e ponto e vírgula.
Este tutorial é escrito usando SCSS.

Se você já está familiarizado com CSS3, você será capaz de pegar Sass de forma relativamente rápida. Ele não fornece quaisquer novas opções de estilo, mas sim as ferramentas para escrever sua CSS de forma mais eficiente e fazer a manutenção mais fácilmente.

```scss


// Comentários de linha única são removidos quando Sass é compilado para CSS.

/* Comentários multi-line são preservados. */



/*Variáveis
==============================*/



/* É possível armazenar um valor CSS (tais como a cor) de uma variável.
Use o símbolo "$" para criar uma variável. */

$primary-color: #A3A4FF;
$secondary-color: #51527F;
$body-font: 'Roboto', sans-serif;

/* Você pode usar as variáveis em toda a sua folha de estilo.
Agora, se você quer mudar a cor, você só tem que fazer a mudança uma vez. */

body {
	background-color: $primary-color;
	color: $secondary-color;
	font-family: $body-font;
}

/* Quando compilar ficaria assim: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/ * Este é muito mais fácil de manter do que ter de mudar a cor
cada vez que aparece em toda a sua folha de estilo. * /




/*Mixins
==============================*/



/* Se você achar que você está escrevendo o mesmo código para mais de um
elemento, você pode querer armazenar esse código em um mixin.

Use a diretiva '@mixin', além de um nome para o seu mixin. */

@mixin center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* Você pode usar o mixin com '@include' e o nome mixin. */

div {
	@include center;
	background-color: $primary-color;
}

/* Apoś compilar ficaria assim: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}


/* Você pode usar mixins para criar uma propriedade estenográfica. */

@mixin size($width, $height) {
	width: $width;
	height: $height;
}

/* O que você pode invocar passando argumentos de largura e altura. */

.rectangle {
	@include size(100px, 60px);
}

.square {
	@include size(40px, 40px);
}

/* Isso compilado ficará assim: */
.rectangle {
  width: 100px;
  height: 60px;
}

.square {
  width: 40px;
  height: 40px;
}



/*Funções
==============================*/



/* Sass fornece funções que podem ser utilizados para realizar uma variedade de
    tarefas. Considere o seguinte */

/* Funções pode ser chamado usando seu nome e passando o
    argumentos necessários */
body {
  width: round(10.25px);
}

.footer {
  background-color: fade_out(#000000, 0.25)
}

/* Compiles to: */

body {
  width: 10px;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* Você também pode definir suas próprias funções. As funções são muito semelhantes aos
   mixins. Ao tentar escolher entre uma função ou um mixin, lembre-
   que mixins são os melhores para gerar CSS enquanto as funções são melhores para
   lógica que pode ser usado em todo o seu código Sass. Os exemplos
   seção Operadores Math 'são candidatos ideais para se tornar um reutilizável
   função. */

/* Esta função terá um tamanho de destino eo tamanho do pai e calcular
   e voltar a percentagem */

@function calculate-percentage($target-size, $parent-size) {
  @return $target-size / $parent-size * 100%;
}

$main-content: calculate-percentage(600px, 960px);

.main-content {
  width: $main-content;
}

.sidebar {
  width: calculate-percentage(300px, 960px);
}

/* Compila para: */

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}



/* Extend (Herança)
============================== */



/*Extend é uma maneira de compartilhar as propriedades de um seletor com outro. */

.display {
	@include size(5em, 5em);
	border: 5px solid $secondary-color;
}

.display-success {
	@extend .display;
	border-color: #22df56;
}

/* Compiles to: */
.display, .display-success {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F;
}

.display-success {
  border-color: #22df56;
}

/* Ampliando uma declaração CSS é preferível a criação de um mixin
   por causa da maneira agrupa as classes que todos compartilham
   o mesmo estilo base. Se isso for feito com um mixin, a largura,
   altura, e a borda seria duplicado para cada instrução que
   o chamado mixin. Enquanto isso não irá afetar o seu fluxo de trabalho, será
   adicionar inchaço desnecessário para os arquivos criados pelo compilador Sass. */



/*Assentamento
==============================*/



/ * Sass permite seletores ninhos dentro seletores * /

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #FF0000;
	}
}

/* '&' será substituído pelo selector pai. */
/* Você também pode aninhar pseudo-classes. */
/* Tenha em mente que o excesso de nidificação vai fazer seu código menos sustentável.
Essas práticas também recomendam não vai mais de 3 níveis de profundidade quando nidificação.
Por exemplo: */


ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Compila para: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/*Parciais e Importações
==============================*/


/* Sass permite criar arquivos parciais. Isso pode ajudar a manter seu Sass
   código modularizado. Arquivos parciais deve começar com um '_', por exemplo, _reset.css.
   Parciais não são geradas em CSS. */



/* Considere o seguinte CSS que nós vamos colocar em um arquivo chamado _reset.css */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Sass oferece @import que pode ser usado para importar parciais em um arquivo.
   Isso difere da declaração CSS @import tradicional, que faz
   outra solicitação HTTP para buscar o arquivo importado. Sass converte os
   importadas arquivo e combina com o código compilado. */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* Compiles to: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/*Placeholder Selectors
==============================*/  


/* Os espaços reservados são úteis na criação de uma declaração CSS para ampliar. Se você
   queria criar uma instrução CSS que foi usado exclusivamente com @extend,
   Você pode fazer isso usando um espaço reservado. Espaços reservados começar com um '%' em vez
   de '.' ou '#'. Espaços reservados não aparece no CSS compilado. * /

%content-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  @extend %content-window;
  background-color: #0000ff;
}

/* Compilado para: */

.message-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  background-color: #0000ff;
}



/*Operações Math
============================== * /


/* Sass fornece os seguintes operadores: +, -, *, /, e %. estes podem
   ser úteis para calcular os valores diretamente no seu Sass arquivos em vez
   de usar valores que você já calculados pela mão. Abaixo está um exemplo
   de uma criação de um projeto simples de duas colunas. * /

$content-area: 960px;
$main-content: 600px;
$sidebar-content: 300px;

$main-size: $main-content / $content-area * 100%;
$sidebar-size: $sidebar-content / $content-area * 100%;
$gutter: 100% - ($main-size + $sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: $main-size;
}

.sidebar {
  width: $sidebar-size;
}

.gutter {
  width: $gutter;
}

/* Compiles to: */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}


```



## SASS ou Sass?
Alguma vez você já se perguntou se Sass é um acrônimo ou não? Você provavelmente não tem, mas vou dizer-lhe de qualquer maneira. O nome do idioma é uma palavra, "Sass", e não uma sigla.
Porque as pessoas estavam constantemente a escrevê-lo como "SASS", o criador da linguagem de brincadeira chamou de "StyleSheets Sintaticamente Incríveis".


## Prática Sass
Se você quiser jogar com Sass em seu navegador, vá para [SassMeister](http://sassmeister.com/).
Você pode usar uma sintaxe, basta ir para as configurações e selecionar Sass ou SCSS.


## Compatibilidade

Sass pode ser usado em qualquer projeto, desde que você tenha um programa para compilá-lo
em CSS. Você vai querer verificar se o CSS que você está usando é compatível
com os seus navegadores de destino.

[QuirksMode CSS](http://www.quirksmode.org/css/) e [CanIUse](http://caniuse.com) são ótimos recursos para verificação de compatibilidade.


## Leitura
* [Official Documentation](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way](http://thesassway.com/) fornece tutoriais (iniciante avançados) e artigos.
---
language: Scala
filename: learnscala-pt.scala
contributors:
    - ["George Petrov", "http://github.com/petrovg"]
    - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Ha-Duong Nguyen", "http://reference-error.org"]
translators:
    - ["Francieli Viane", "https://github.com/FFrancieli"]
lang: pt-br
---

Scala - a linguagem escalável

```scala

/////////////////////////////////////////////////
// 0. O básico
/////////////////////////////////////////////////
/*
  Configurando o Scala:

  1) Baixe o instalador do Scala - http://www.scala-lang.org/downloads
  2) Extraia (unzip ou tar) para sua localização favorita e coloque o subdiretório
  bin na variável de ambiente `PATH`
*/

/*
  Tente o REPL

  Scala tem uma ferramenta chamada REPL (Read-Eval-Print Loop) que é análogo a
  interpretadores de linha de comando de outras linguagens. Você pode digitar
  qualquer expressão de Scala e o resultado será calculado e impresso.

  O REPL é uma ferramenta muito conveniente para testar e verificar código. Use-o
  enquanto você lê o tutorial para explorar os conceitos rapidamente por conta própria.
*/

//Inicialize um REPL de Scala executando o comando scala no terminal. Você deve ver o prompt:
$ scala
scala>

//Por padrão, cada expressão que você executa é salva como um novo valor enumerado:
scala> 2 + 2
res0: Int = 4

// Valores padrões podem ser reutilizados. Observe o tipo do valor exibido no resultado...
scala> res0 + 2
res1: Int = 6

// Scala é uma linguagem fortemente tipada. Você pode usar o REPL para verfificar o tipo
// sem avaliar uma expressão.

scala> :type (true, 2.0)
(Boolean, Double)

// As sessões do REPL podem ser salvas
scala> :save /sites/repl-test.scala

//Arquivos podem ser carregados no REPL
scala> :load /sites/repl-test.scala
Loading /sites/repl-test.scala...
res2: Int = 4
res3: Int = 6

// Você pode pesquisar em seu histórico recente
scala> :h?
1 2 + 2
2 res0 + 2
3 :save /sites/repl-test.scala
4 :load /sites/repl-test.scala
5 :h?

// Agora que você já sabe brincar, vamos aprender um pouco de Scala...

/////////////////////////////////////////////////
// 1. Introdução
/////////////////////////////////////////////////

// Comentários de uma linha começam com duas barras

/*
  Comentários com múltiplas linhas, como você já pode ver, são assim.
*/

// Imprimir e forçar uma linha na próxima impressão
println("Hello world!")
println(10)
// Hello world!
// 10

//Imprimir sem forçar uma nova linha na próxima impressão
print("Hello world")
print(10)
// Hello world10

//A declaração de valores pode ser feita usando tanto o var quanto o val.
// Declarações feitas com `val` são imutáveis, enquanto que declarações feitas
// com var são mutáveis. Imutabilidade é uma coisa boa.
val x = 10 // x is now 10
x = 20     // error: reassignment to val
var y = 10
 = 20     // y agora é 20

/*
  Scala é uma linguagem estaticamente tipada. Observe ainda que nas declarações
  acima nós não especificamos um tipo. Isso se deve a um recurso da linguagem
  chamado de inferência. Na maioria dos casos, o compilador do Scala consegue
  adivinhar qual tipo é, de forma que você não precisa digitar sempre. Nós
  podemos declarar o tipo da variável de maneira explícita asim:
*/

val z: Int = 10
val a: Double = 1.0

// Note que a conversão automática de Int para Double, o resultado é 10.0, não 10
val b: Double = 10

//Valores booleanos
true
false

//Operações booleanas
!true         // false
!false        // true
true == false // false
10 > 5        // true

// Matemática é como o de costume
1 + 1   // 2
2 - 1   // 1
5 * 3   // 15
6 / 2   // 3
6 / 4   // 1
6.0 / 4 // 1.5
6 / 4.0 // 1.5

// Calcular uma expressão no REPL te dá o tipo e o valor do resultado
1 + 7

/* A linha acima resulta em:
  scala> 1 + 7
  res29: Int = 8

  Isso significa que o resultado ao culcular  1 + 7 é um objeto do tipo Int com
  valor 8.

  Note que "res29" é o nome de uma variável gerada sequencialmente para guardar
  os resultados das expressões que você executa, logo seu nome pode ser
  diferente.
*/

"Strings em Scala são delimitadas por aspas duplas"
'a' // Um caractere em Scala
// 'Strings com aspas simples não existem em Scala.' <= isso causa um erro.

// Strings possuem os métodos comuns de Java definidos
"hello world".length
"hello world".substring(2, 6)
"hello world".replace("C", "3")

// Elas também possuem alguns métodos extras do Scala. Veja também:
scala.collection.immutable.StringOps
"hello world".take(5)
"hello world".drop(5)

//Interpolação de string: observe o prefixo "s"
val n = 45
s"We have $n apples" // => "We have 45 apples"

// Também é possível ter expressões dentro de interpolação de strings
val a = Array(11, 9, 6)
s"My second daughter is ${a(0) - a(2)} years old."    // => "My second daughter is 5 years old."
s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples."
s"Power of 2: ${math.pow(2, 2)}"                      // => "Power of 2: 4"

// Formatação de strings interpoladas com o prefixo "f"
f"Power of 5: ${math.pow(5, 2)}%1.0f"         // "Power of 5: 25"
f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122: 11.0454"


// Strings cruas, ignorando caracteres especiais
raw"New line feed: \n. Carriage return: \r." // => "New line feed: \n. Carriage return: \r."

//Alguns caracteres precisam ser "escapados", ex. uma aspa dupla dentro de uma string

"They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown""

// Aspas triplas permitem strings a abrangerem múltiplas linhas e conter Aspas
val html = """<form id="daform">
                <p>Press belo', Joe</p>
                <input type="submit">
              </form>"""


/////////////////////////////////////////////////
// 2. Funções
/////////////////////////////////////////////////

// Funções são definidas da seguinte maneira:
//
//   def nomeDaFuncao(args ...): TipoDeRetorno = {body ...}
//
// Se você vem de linguagens mais tradicionais, note a omissão da palavra chave
//return. Em Scala a última expressão no bloco da função é o valor de retorno
def sumOfSquares(x: Int, y: Int): Int = {
  val x2 = x * x
  val y2 = y * y
  x2 + y2
}

// As { } podem ser omitidas se o corpo da função possui apenas uma expressão:
def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y

// A sintaxe para chamar funções é familiar:
sumOfSquares(3, 4)  // => 25

// Você poode usar o nome dos parâmetros para especificá-los numa ordem diferente
def subtract(x: Int, y: Int): Int = x - y

subtract(10, 3)     // => 7
subtract(y=10, x=3) // => -7

// Na maioria dos casos (sendo funções recursivas a a exceção mais notável) o
// tipo de retorno da função pode ser omitido, e o mesmo tipo de inferência que
// vimos nas variáveis funcionará com o valor de retorno da função:
def sq(x: Int) = x * x  // O compilador consegue adivinhar que o tipo de retorno é Int

// Funções podem ter parâmetros padrão:
def addWithDefault(x: Int, y: Int = 5) = x + y
addWithDefault(1, 2) // => 3
addWithDefault(1)    // => 6

// Funções anônimas são semelhantes a essa:
(x: Int) => x * x

// Diferente de defs, até mesmo a entrada de funções anônimas podem ser omitidas
// se o contexto deixar isso claro. Observe o tipo "Int => Int", que significa
// uma função que recebe umn Int e retorna um Int.
val sq: Int => Int = x => x * x

// Se cada argumento na sua função anônima é usado apenas uma vez, Scala te fornece
// uma maneira ainda mais curta de definí-lo. Estas funções anônimas acabam por
// ser muito comuns, como será mostrado na sessão de estrutura de dados.
val addOne: Int => Int = _ + 1
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)

addOne(5)      // => 6
weirdSum(2, 4) // => 16

// A palavra chave return existe em Scala, mas só retorna do def mais profundo que o cerca.
//AVISO: O uso do return em Scala é propenso a erros e deve ser evitado.
//Não há efeito em funções anônimas. Per exemplo:
def foo(x: Int): Int = {
  val anonFunc: Int => Int = { z =>
    if (z > 5)
      return z // Esta linha faz Z retornar o valor de foo!
    else
      z + 2    // Esta linha retorna o valor de anonFunc
  }
  anonFunc(x) // Esta linha retorna o valor de foo
}

/////////////////////////////////////////////////
// 3. Controle de Fluxo
/////////////////////////////////////////////////

1 to 5
val r = 1 to 5
r.foreach(println)

r foreach println
///N.B.: Scala é bem flexível quando se fala de pontos e parêntesis - estude as regras
//separadamente. Isso ajuda a escrever DSLs e APIs que são lidas como inglês.

(5 to 1 by -1) foreach (println)

// Um loop while
var i = 0
while (i < 10) { println("i " + i); i += 1 }

while (i < 10) { println("i " + i); i += 1 }   // Sim, de novo. O que aconteceu? Por quê?

i    // Exibe o valor de i. Note que o while é um loop no senso clássico -
     // executa sequencialmente enquanto muda a variável do loop. While é muito
     // rápido, mas usar os combinadores e compreenões acima é mais fácil
     // para entender e paralizar

// Um loop do-while
i = 0
do {
  println("i ainda é menor que 10")
  i += 1
} while (i < 10)


// Recursão é a forma mais idiomática de repetir uma ação em Scala (assim como na
// maioria das linguagens de programação funcional)
// Funções recursivas precisam de um tipo de retorno explícito, o compilador não
// consegue inferir;
// Aqui está o Unit
def showNumbersInRange(a: Int, b: Int): Unit = {
  print(a)
  if (a < b)
    showNumbersInRange(a + 1, b)
}
showNumbersInRange(1, 14)

// Condicionais

al x = 10

if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println("yeah") else println("nay")

println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"

/////////////////////////////////////////////////
// 4. Estrutura de Dados
/////////////////////////////////////////////////

val a = Array(1, 2, 3, 5, 8, 13)
a(0)     // Int = 1
a(3)     // Int = 5
a(21)    // Lança uma exceção

val safeM = m.withDefaultValue("no lo se")
safeM("bottle")   // java.lang.String = no lo se

val s = Set(1, 3, 7)
s(0)      // Boolean = false
s(1)      // Boolean = true

/* Veja a documantação do map aqui -
 * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
 * e garanta que você leia
 */

 // Tuplas

 (1, 2)

 (4, 3, 2)

 (1, 2, "three")

 (a, 2, "three")

 //Por que ter isso?
 val divideInts = (x: Int, y: Int) => (x / y, x % y)

//A função divideInts te dá o resultado e o resultado
divideInts(10, 3)    // (Int, Int) = (3,1)

//Para acessar os elementos de uma tupla, use _._n onde n é o índex do elemento

val d = divideInts(10, 3)    // (Int, Int) = (3,1)

d._1    // Int = 3
d._2    // Int = 1

// Alternativamente, você pode atribuir múltiplas variáveis para uma tupla, o
// que é mais conveniente e legível em muitos casos
val (div, mod) = divideInts(10, 3)

div     // Int = 3
mod     // Int = 1

/////////////////////////////////////////////////
// 5. Object Oriented Programming
/////////////////////////////////////////////////

/*
    Tudo o que vimos até agora neste tutorial foram expressões simples (valores, funções, etc).
    Essas expressões são boas para digitar no interpretador da linha de comando para
    testes rápidos, mas elas não podem existir por si só em um arquivo Scala. Por exemplo,
    você não pode ter simplesmente "val x = 5" em um arquivo Scala. Ao invés disso, os únicos
    construtores de alto nível permitidos em Scala são:

    - objects
    - classes
    - case classes
    - traits

    E agora vamos explicar o que é cada um deles.
*/

//classes são similares a classes em outras linguagens. Os argumentos do construtor
// são declarados logo depois do nome da classe e a inicialização é feita no corpo da classe.

class Dog(br: String) {
  // codigo do construtor aqui
  var breed: String = br

  // Define um método chamado bark que retorna uma String
  def bark = "Woof, woof!"

  // Assume-se que os métodos e valores são públicos. As palavras chave "protected"
  // e "private" também estão disponíveis.
  private def sleep(hours: Int) =
    println(s"I'm sleeping for $hours hours")

  // Métodos abstratos são simplesmente métodos sem corpo. Se a gente remover o
  // comentário da próxima linha a classe Dog teria que ser declarada como abstrata

  //   abstract class Dog(...) { ... }
  // def chaseAfter(what: String): String
}

// A palavra chave "object" cria um tipo e uma instância singlenton desse tipo.
// É comum para classes em Scala ter um "companion object" (objeto companheiro),
// onde, por exemlo, o comportamento é capturado pelas classes em si, mas o comportamento
// relacionado a toda instância da classe vai em objects. A diferença é semelhante
// a métodos versus métodos estáticos em outras linguagens. Note que objects e
// classes podem ter o mesmo nome.

object Dog {
  def allKnownBreeds = List("pitbull", "shepherd", "retriever")
  def createDog(breed: String) = new Dog(breed)
}

// Case classes são classes que possuem uma funcionalidade extra incorporada.
// Uma dúvida comum para iniciantes em Scala é quando usar classes e quando usar
// case classes. A linha é bem tênue, mas em geral classes tendem a focar em encapsulamento,
// polimorfismo e comportamento. Os valores nestas classes tendem a ser privados e
// apenas métodos ficam expostos. O propósito primário de uma case class é guardar
// dados imutáveis. Às vezes as case classes possuem alguns poucos métodos, os quais
// raramente possuem efeitos colaterais (side effects).
case class Person(name: String, phoneNumber: String)

// Cria uma nova instância. Observe que case classes não precisam de usar "new" ao serem instanciadas
val george = Person("George", "1234")
val kate = Person("Kate", "4567")

// Com case classes você ganha algumas regalias, como getters:
// With case classes, you get a few perks for free, like getters:
george.phoneNumber  // => "1234"

// Verificação de igualdade por campo (sem a necessidade de sobrescrever o método equals)
Person("George", "1234") == Person("Kate", "1236")  // => false

// Uma maneira fácil de copiar
// otherGeorge == Person("george", "9876")
val otherGeorge = george.copy(phoneNumber = "9876")

// E muitas outras. Case classes também possuem pattern matching de graça. Veja no próximo tópico.

// Traits a caminho.


/////////////////////////////////////////////////
// 6. Pattern Matching
/////////////////////////////////////////////////

// Pattern matching é um recurso muito poderoso e muito usado em Scala. Aqui
// mostramos como o seu pattern se adequa a uma case class.
// NB: Diferente de outras linguagens, Scala não precisa de quebras. Entrar em
// todas as condições do pattern matching simples não acontece.

def matchPerson(person: Person): String = person match {
  // Enrão você especifica os padrões
  case Person("George", number) => "We found George! His number is " + number
  case Person("Kate", number)   => "We found Kate! Her number is " + number
  case Person(name, number)     => "We matched someone : " + name + ", phone : " + number
}

val email = "(.*)@(.*)".r  // Define uma regex para o próximo exemplo.

// Pattern matching pode parecer com o comando switch nas liguagens da família C,
// mas é muito mais poderoso. Em Scala você pode encontrar mais correpondências:

def matchEverything(obj: Any): String = obj match {
  // Você pode encontrar valores correspondentes:
  case "Hello world" => "Got the string Hello world"

  // Você pode fazer correspondência por tipo:
  case x: Double => "Got a Double: " + x

  // Você pode especificar condições:
  case x: Int if x > 10000 => "Got a pretty big number!"

  // Você pode encontrar correspondência com case classes, como fizemos antes:
  case Person(name, number) => s"Got contact info for $name!"

  // Você pode encontrar correspondências por regex:
  case email(name, domain) => s"Got email address $name@$domain"

  // Você pode encontrar correspondencias por tuplas:
  case (a: Int, b: Double, c: String) => s"Got a tuple: $a, $b, $c"

  // Você pode encontrar corresponências por estruturas de dados:
  case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c"

  // Você pode aninhar padrões:
  case List(List((1, 2, "YAY"))) => "Got a list of list of tuple"

  // Retornar qualquer valor (padrão - default) caso nenhuma das possibilidades é correspondente.
  case _ => "Got unknown object"

  // Na verdade, você pode fazer correspondência de padrão de qualquer objeto que
  // tenha o método "unnaply". Este recurso é tão poderoso que o Scala te deixa
  // criar funções inteiras como patterns:

  val patternFunc: Person => String = {
    case Person("George", number) => s"George's number: $number"
    case Person(name, number) => s"Random person's number: $number"
  }
}

/////////////////////////////////////////////////
// 7. Programação Funcional
/////////////////////////////////////////////////

// Scala permite que métodos e funções recebam ou retornem outras funções ou métodos.

val add10: Int => Int = _ + 10 // A function taking an Int and returning an Int
List(1, 2, 3) map add10 // List(11, 12, 13) - add10 is applied to each element

// Funções anônimas podem ser usadas ao invés de funções com nomes:
List(1, 2, 3) map (x => x + 10)

// E o símbolo underline ("_") pode ser usado quando há apenas um argumento para a função anônima.
List(1, 2, 3) map (_ + 10)

// Se tanto o bloco animo quanto a função que você estiver usando receberem apenas
// um argumento, você pode inclusive omitir o símbolo _
List(1, 2, 3) map (_ + 10)

// Combinadores

s.map(sq)

val sSquared = s. map(sq)

sSquared.filter(_ < 10)

sSquared.reduce (_+_)

// A função filter recebe um predicado (uma função do tipo A -> Boolean) e seleciona
// todos os elementos que satisfazem o predicado.
List(1, 2, 3) filter (_ > 2) // List(3)
case class Person(name: String, age: Int)
List(
  Person(name = "Dom", age = 23),
  Person(name = "Bob", age = 30)
).filter(_.age > 25) // List(Person("Bob", 30))

// Scala tem o método foreach definido em algumas collections em específico, o qual
// recebe um tipo e retorna Unit (um método void)
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println

/* NB Ests não são laços for. A semântica dos laços for é 'repetir' enquanto um
  for-comprehension define um relacionamento entre dois conjuntos de dados */


/////////////////////////////////////////////////
// 8. Implicits
/////////////////////////////////////////////////

/* ALERTA ALERTA:
  Implicits são um conjunto de recursos poderosos de Scala e consequentemente é
  fácil abusar deles. Iniciantes em Scala deveriam resistir a tentação de usá-los
  até que eles entendam não apenas como eles funcionam mas também as melhores práticas
  deles. Incluimos uma sessão neste tutorial sobre isso porque implicits são tão
  corriqueiros em bibliotecas do Scala que é impossível fazer qualqeuer coisa expressiva
  sem utilizar uma biblioteca que usa implicits. Isto é para você entender e trabalhar
  com implicits. Não declare seus próprios implicits por conta própria.
*/

// qualquer valor (val, function, objects, etc) pode ser declarado para ser implícito
// usando a, você adivinhou, palavra chave "implicit". Usaremos a classe Dog definida
// na sessão 5 para os próximos exemplos.
implicit val myImplicitInt = 100
implicit def myImplicitFunction(breed: String) = new Dog("Golden " + breed)

// A palavra chave implicit não muda o comportamento do valor por si só, então
// os valores acima podem ser usados como de costume.
myImplicitInt + 2                   // => 102
myImplicitFunction("Pitbull").breed // => "Golden Pitbull"

A diferença é que agora esses valores são elegíveis para serem usados quando outra
// parte do código "precisa" de um valor implícito. Uma situação é uma função
// com argumentos implícitos:
def sendGreetings(toWhom: String)(implicit howMany: Int) =
  s"Hello $toWhom, $howMany blessings to you and yours!"

// Se fornecermos um valor para "howMany" a função se comporta como sempre
sendGreetings("John")(1000)  // => "Hello John, 1000 blessings to you and yours!"

// Mas se omitirmos o parâmetro implícito um valor implícito de mesmo tipo é usado,
// neste caso, "myImplicitInt":
sendGreetings("Jane")  // => "Hello Jane, 100 blessings to you and yours!"

// Parâmetros implícitos de funções nos permitem simular type classes em outras
//linguagens funcionais. As linhas abaixo são a mesma coisa:
// def foo[T](implicit c: C[T]) = ...
// def foo[T : C] = ...

// Outro caso no qual o compilador procura por um implicit é quando você tem obj.method(...)
// mas "obj" não possui "method" como um método. Neste caso, se houver uma conversão
// de implicit do tipo A => B, onde A é o tipo do "obj" e B tem um método chamado
// "method", a conversão é aplicada. Então, tendo myImplicitFunction acima em escopo, podemos dizer:
"Retriever".breed // => "Golden Retriever"
"Sheperd".bark    // => "Woof, woof!"

// Aqui, a String é convertida para Dog usando nossa função acima, então o método
// apropriado é chamado. Isso é um recurso extremamente poderoso, mas de novo, não
// é para ser usado de maneira leviana. Na verdade, quando você define a função
// implícita, o seu compilador deve exibir um aviso de que você não deveria fazer isso,
// a menos que você realmente saiba o que você está fazendo.

/////////////////////////////////////////////////
// 9. Misc
/////////////////////////////////////////////////

// Importando coisas
import scala.collection.immutable.List

// Importando todos os sub pacotes
import scala.collection.immutable._

// Importando várias classes em um único comando
import scala.collection.immutable.{List, Map}

// Renomeando um import usando '=>'
import scala.collection.immutable.{List => ImmutableList}

// Importa todas as classes, com exceção de algumas. O import abaixo importa todas as classes excluindo Map e Set:
import scala.collection.immutable.{Map => _, Set => _, _}

// Classes Java também podem ser importadas. A syntaxe de Scala pode ser usada:
import java.swing.{JFrame, JWindow}

// O ponto de entrada do seu programa é definido em um arquivo Scala usando um object com um único método main:
object Application {
  def main(args: Array[String]): Unit = {
    // o código fica aqui
  }
}

// Arquivos podem ter múltiplas classes e objects. Compile com scalac

// Entrada e saída

// Para ler um arquivo linha a linha
import scala.io.Source
for(line <- Source.fromFile("myfile.txt").getLines())
  println(line)

// Para escrever um arquivo use o PrintWriter do Javaval writer = new PrintWriter("myfile.txt")
writer.write("Writing line for line" + util.Properties.lineSeparator)
writer.write("Another line here" + util.Properties.lineSeparator)
writer.close()

##  Recursos adicionais

* [Scala for the impatient](http://horstmann.com/scala/)
* [Twitter Scala school](http://twitter.github.io/scala_school/)
* [Documentação de Scala](http://docs.scala-lang.org/)
* [Tente Scala no seu navegador](http://scalatutorials.com/tour/)
* Junte [Scala user group](https://groups.google.com/forum/#!forum/scala-user)
```
---
language: self
contributors:
    - ["Russell Allen", "http://github.com/russellallen"]
translators:
    - ["Ricardo de Almeida Gonzaga", "http://github.com/ricardotk"]
lang: pt-br
filename: learnself-br.self
---

Self é um protótipo rápido baseado em linguagem orientada a objeto a qual roda em sua própria JIT vm. A maior parte do desenvolvimento é feito através de interações com objetos ativos, através de um ambiente visual de desenvolvimento chamado *morphic* com navegador e depurador integrados.

Tudo em Self é um objeto. Toda computação é feita através do envio de mensagens para objetos. Objetos em Self podem ser compreendidos como conjuntos de slots de chave-valor.

# Construindo objetos

O analisador interno de Self pode construir objetos, incluindo objetos método.

```
"Isto é um comentario"

"A string:"
'Isto é uma string com caracteres \'escapados\'.\n'

"Um inteiro de 30 bits"
23

"Um float de 30 bits"
3.2

"-20"
-14r16

"Um objeto o qual entende apenas uma menssagem, 'x' que retorna 20"
(|
  x = 20.
|)

"Um objeto o qual tambem entende 'x', que atribui o slot x"
(|
  x <- 20.
|)

"Um objeto o qual entende o método 'doubleX' que 
dobra o valor de x e então retorna o objeto"
(|
  x <- 20.
  doubleX = (x: x * 2. self)
|)

"Um objeto o qual entende todas as mensagens 
que 'traits point' entende". O analisador  
procura 'traits point' enviando as mensagens 
'traits' e 'point' para um objeto conhecido
chamado de 'lobby'. Ele procura pelo objeto
'true' também enviando a mensagem 'true'
para o 'lobby'."" 
(|     parent* = traits point.
       x = 7.
       y <- 5.
       isNice = true.
|)
```

# Enviando mensagens para objetos

Mensagens podem ser unárias, binárias ou palavras-chave. Precedência é nesta ordem. Diferentemente de Smalltalk, a precedência de mensagens binárias precisam ser especificadas, e todas as palavras-chave após a primeira devem começar com letra maiúscula. Mensagens são separadas de seu destinatário através de espaço em branco.

```
"mensagem unária, envia 'printLine' para o objeto '23' 
o qual escreve a string '23' para stdout e retorna o objeto recebido (ie 23)"
23 printLine

"envia a mensagem '+' com '7' para '23', em seguida a mensagem '*' com '8' para o resultado"
(23 + 7) * 8 

"envia 'power:' para '2' com '8' retorna 256"
2 power: 8 

"envia 'keyOf:IfAbsent:' para 'hello' com argumentos 'e' e '-1'. 
Retorna 1, o índice de 'e' em 'hello'."
'hello' keyOf: 'e' IfAbsent: -1 
```

# Blocos

Self define controle de fluxo assim como Smalltalk e Ruby por meio de blocos. Blocos são computações atrasadas da forma:

```
[|:x. localVar| x doSomething with: localVar]
```

Exemplos do uso de um bloco:

```
"retorna 'HELLO'"
'hello' copyMutable mapBy: [|:c| c capitalize] 

"retorna 'Nah'"
'hello' size > 5 ifTrue: ['Yay'] False: ['Nah'] 

"retorna 'HaLLO'"
'hello' copyMutable mapBy: [|:c| 
   c = 'e' ifTrue: [c capitalize]
            False: ['a']]
```

Múltiplas expressões são separadas por ponto final. ^ retorna imediatamente.

```
"retorna An 'E'! How icky!"
'hello' copyMutable mapBy: [|:c. tmp <- ''| 
   tmp: c capitalize.
   tmp = 'E' ifTrue: [^ 'An \'E\'! How icky!'].
   c capitalize
   ]
```

Blocos são realizados (completados) ao enviá-los a mensagem 'value' e herdando (imcumbir a) seus contextos:
```
"retorna 0"
[|x|
    x: 15.
    "Repetidamente envia 'value' para o primeiro bloco enquanto o resultado do
     envio de 'value' para o segundo bloco é o objeto 'true'"
    [x > 0] whileTrue: [x: x - 1]. 
    x
] value
```

# Métodos

Métodos são como blocos, porém eles não estão dentro de um contexto, ao invés disto são armazenados como valores de slots. Diferentemente de Smalltalk, métodos por padrão retornam o seu valor final e não 'self'.

```
"Aqui está um objeto com um slot atribuível 'x' e um método 'reduceXTo: y'.
Enviar a mensagem 'reduceXTo: 10' para este objeto colocará 
o objeto '10' no slot 'x' e retornará o objeto original"
(| 
    x <- 50.
    reduceXTo: y = (
        [x > y] whileTrue: [x: x - 1]. 
        self)
|)
.
```

# Protótipos

Não existem classes em Self. A maneira de obter um objeto é encontrar um protótipo e copia-lo.

```
| d |
d: dictionary copy.
d at: 'hello' Put: 23 + 8.
d at: 'goodbye' Put: 'No!.
"Imprime No!"
( d at: 'goodbye' IfAbsent: 'Yes! ) printLine.
"Imprime 31"
( d at: 'hello' IfAbsent: -1 ) printLine.
```

# Informações adicionais

O [Manual do Self](http://handbook.selflanguage.org) tem muito mais informações, e nada melhor do que por a mão na massa com Self através de sua [homepage](http://www.selflanguage.org).
---
language: Solidity
filename: learnSolidity.sol
contributors:
  - ["Nemil Dalal", "https://www.nemil.com"]
  - ["Joseph Chow", ""]
translators:
    - ["João Farias", "http://thatsabug.com/"]
lang: pt-br
---

Solidity permite você programar para a [Ethereum]
(https://www.ethereum.org/), uma máquina virtual baseada na tecnologia blockhain
para criação e execução de contratos inteligentes, sem necessidade de partes
centralizadas ou de confiança.

Solidity é uma linguagem de contratos estaticamente tipaada com similaridade com
Javascript e C. Como objetos em programação orientada a objetos, cada contrato
possue variáveis de estado, funções e tipos de dados comuns. Funcionalidades
particulares de contratados incluem cláusuras modificadoras (guarda), notifica
dores de eventos para listerners e variáveis globais customizadas.


Exemplos de contratos Ethereum incluem crowdfunding, votações e audições cegas.

Erros em código Solidity causam grandes riscos e custos; portanto, você
deve ser muito cuidado com teste e divulgação. DEVIDO ÀS CONSTANTES MUDANÇAS
NO ETHEREUM, ESTE DOCUMENTOS PROVAVELMENTE NÃO ESTARÁ ATUALIZADO, VOCÊ DEVE
ACOMPANHAR A CHATROOM DO SOLIDITY E O BLOG DO ETHEREUM PARA NOTÍCIAS ATUALIZADAS.
TODO CÓDIGO AQUI É ENTREGUE COMO ESTÁ, COM SUBSTANCIAL RISCO DE ERRROS E PADRÕES
DE CÓDIGO DEPRECADOS.

Diferentemente de outros tipo de código, você também deve adicionar padrões
como pausa, deprecação e retração para reduzir riscos. Este documento discute
sintaxe, então, muito padrões populares são excluídos.

Como Solidity e Ethereum ainda estão sob desenvolvimento, funcionalidades beta
e experimentais são tipicamente marcadas e sujeitas à mudanças. Pull requests
são bem-vindos.

```javascript
// Primeiramente, um contrato de um Banco simples
// Permite depósitos, retiradas e checagens de saldo

// banco_simples.sol (note a extensão .sol)

/* **** INCICIO DO EXEMPLO **** */

// Declare a versão do compilador.
pragma solidity ^0.4.2;

// Inicie com comentários Natspec (as três barras)
// usados para documentação - e como dados descritivos para elementos/ação de UI

/// @title BancoSimples
/// @author nemild

/* 'contrato' tem similadirades com 'classes' em outras linguagens (variáveis de
class, herança, etc.) */

contract BancoSimples { // CamelCase
    // Declare variáveis de estado fora da função, para persistí-la durante a
    // duração do contrato

    // dicionário que mapeia endereços para saldos
    // tenha cuidado  sobre ataques de overflow com números

    mapping (address => uint) private saldos;

    // "private" significa que outros contratos não podem acessar saldos
    // diretamente, mas o dado ainda é visível para outras partes da blockchain

    address public dono;

    // ´public´ é legível (mas sem acesso de escrita) por usuários e contratos

    // Eventos - ações públicas para ouvintes externo
    event LogRealizacaoDeDeposito(address numeroDaConta, uint quantidade);

    // Construtor, pode receber uma ou várias variáveis; apenas uma opção é
    // permitidas

    function BancoSimples() {
        // msg dá detalhes sobre a mensagem mandada pelo contrato
        // msg.sender é um chamador do contrato (endereço do criador do
        // contrato)

        dono = msg.sender;
    }

    /// @notice Deposita ether no banco
    /// @return O saldo do usuário após o depósito

    function deposito() public returns (uint) {
        saldos[msg.sender] += msg.value;

        // Sem necessidade de "this." ou "self." para variáveis de estado
        // todos as variáveis são inciadas com seu valor default

        LogRealizacaoDeDeposito(msg.sender, msg.value); // dispara evento

        return saldos[msg.sender];
    }

    /// @notice Retira ether do banco
    /// @dev Isto não retorna nenhum ether excendente
    /// @param quantidadeDeRetirada quantidade que você quer retirar
    /// @return O saldo restante do usuário
    function retirada(uint quantidadeDeRetirada) public returns (uint saldoRestate) {
        if(saldos[msg.sender] >= quantidadeDeRetirada) {

            // Observe como deduzimos o saldo imediatamente, antes de enviar -
            // devido ao risco de uma chamada recursiva que permite o chamador
            // pedir um valor maior que seu saldo

            saldos[msg.sender] -= quantidadeDeRetirada;

            if (!msg.sender.send(quantidadeDeRetirada)) {
                // incremente de volta só se falhar, como pode estar enviando
                // para o contrato que substituiu 'enviar' no final
                // do recebimento
                saldos[msg.sender] += quantidadeDeRetirada;
            }
        }

        return saldos[msg.sender];
    }

    /// @notice Retorna o saldo
    /// @return O saldo do usuário
    // 'constant' evita que a função edite variáveis de estado
    // permite a função executar localmente/fora da blockchain
    function saldo() constant returns (uint) {
        return saldos[msg.sender];
    }

    // Função de fallback - Chamada se outras funções não forem chamadas ou
    // se ether sem dados forem enviados
    // Tipicamente chamada quando dados inválidos são enviados
    // Adicionada para que ether enviado neste contrato seja revertido se o
    // contrato falhar. Se não existisse, o dinheiro do enviante é transferido
    // para o contrato
    function () {
        throw; // 'throw' reverte o estao para antes da chamada
    }
}
// ** FIM DO EXEMPLO **

// Agora, o básico de Solidity


//1 TIPO DE DADOS E MÉTODOS ASSOCIADOS
// uint é usado para quantidade de moeda (não existem doubles ou floats)
// e para datas (no sistema de tempo Unix)

uint x;

// int de 256 bits, não pode ser mudado após sua instanciação
int constant a = 8;
int256 constant a = 8; // mesmo efeito, mas aqui os 256 bits são explícitos
uint constant VERSÃO_ID = 0x123A1; // uma constante hexadecimal

// com 'constant', o compilador substitui cada ocorrência com o valor

// Para int e uint, é possível determinar o espaço explicitamente, em intervalos
// de 8 a 256, e.g., int8, int16, int24
uint8 b;
int64 c;
uint248 e;

// Cuidado contra overflows, e proteja-se contra esse tipo de ataque

// Não há funções randômicas padrão, use outros contratos para este objetivo

// Casting de tipo
int x = int(b);

bool b = true; // ou então 'var b = true;' para inferição de tipo

// Endereços - comportam 20 bytes/160 bits endereços Ethereum
// Não permite operações aritiméticas
address public dono;

// Tipos de contas:
// Conta de contrato: endereço criado ao criar (função do endereço do criador,
// número da transação)
// Conta externa: (pessoa/entidade externa): endereç criado a partir de chave
// pública

// Adicione o campo 'public' para indicar visibilidade pública/externa
// um getter é automaticamente criado, mas NÃO um setter

// Todos os endereços podem enviar ether
dono.send(ALGUM_SALDO); // returna falso caso falhe
if (dono.send) {} // LEMBRE-SE: encapsule num 'if', dado que endereços de
// contrato tem funções executadas no envio e estas podem falhar
//Também certifique-se que os saldos deduzidos ANTES de tentar enviar, dado que
// há um risco de chamada recursiva que pode drenar um contrato

// pode sobrescrever seu próprio

// Pode verificar o saldo
dona.balance; // o saldo do dono (usuário ou contrato)

// Bytes permitidos de 1 a 32
byte a; // byte é o mesmo que bytes1
bytes2 b;
bytes32 c;

// Bytes dinamicamente criados

bytes m; // Um array especial, mesmo que byte[] (mas mais comprimido)

// Mais custoso que byte1-byte32, então, prefira estes quando possível

// mesmo que bytes, mas não permite verificar tamanho ou acesso por indíce (por
// enquanto)

string n = "oi"; // guardado em UTF8, note as aspas duplas, não simples

// funções de string serão adicionadas no futuro
// prefira bytes32/bytes, dado que UTF8 usa mais espaço

// Inferência de tipo
// var não infere tipos baseados na primeira atribuição,
// não pode ser usado em paramêtros de funções

var a = true;

// use com cuidado, inferência pode resultar em tipos errados
// e.g., um int8, quando um contador precisa de int16

// var pode ser usado para assinalar uma função a uma variável
function a(uint x) returns (uint) {
    return x * 2;
}
var f = a;
f(22); // chamada

// por padrão, todos os valores são inicializados com 0

// Delete pode ser chamada na maioria dos tipos
// (NÃO destroi o valor, mas retorna para o valor 0, o incial)

uint x = 5;

// Desestruturação/Tuplas
(x, y) = (2, 7); // assinada/troca múltiplos valores

// 2. ESTRUTURAS DE DADOS
// Arrays

bytes32[5] apelidos; // array estático
bytes32[] nomes; // array dinâmico
uint novoTamanho = nomes.push("João"); // adicionando retorna o novo tamanho do

// Tamanho
nomes.length; // pega o tamanho
nomes.length = 1; // tamanhos pode ser alterados (para arrays dinâmicos)

// arrays multidimensionais
uint x[][5]; // array com 5 arrays dinâmicos como elementos (ordem da maioria
// das linguagens)

// Dicionários (qualquer tipo para qualquer tipo)
mapping (string => uint) public saldos;
saldos["charles"] = 1;
console.log(saldos["ada"]); // é 0, toda chave não assinalada retorna zero
// 'public' permite o seguinte contrato
nomeDoContrato.saldos("charles"); // retorna 1
// 'public' cria um getter (mas não um setter) como o seguinte
function saldos(string _conta) returns (uint saldo) {
    return saldos[_conta];
}

// Mapeamentos aninhados
mapping (endereco => mapping (endereco => uint)) public guardioes;

// Para deletar
delete saldos["John"];
delete saldos; // assinala zero para todas as chaves

// Diferentemente de outras linguages, NÃO É POSSÍVEL iterar sobre todos os
// elementos de um mapeamento, sem saber previamente as chaves - é possível
// construir estruturas de dados personalizadas para fazer isso

// Structs e enums
struct Banco {
    address dono;
    uint saldo;
}
Banco b = Banco({
    dono: msg.sender,
    saldo: 5
});
// ou
Banco c = Banco(msg.sender, 5);

c.quantidade = 5; // cria novo valor
delete b;
// assinala todos os valores do enum para zero, exceto mapeamentos

// Enums
enum Estado { Criado, Travado, Inativo }; // geralmente usado para máquina de
// estados
Estado public estado; // Declara variável para enum
estado = Estado.Criado;
// enums podem ser explicitamente convertidas em ints
uint estadoCriado = uint(Estado.Criado); //  0

// Localização de dados: Memória vs. disco vs. pilha - todos os tipos complexos
// (arrays, structs) tem uma localização de dados
// 'memória' não é persistida, 'disco' é
// Padrão é 'disco' para variáveis locais e de estado; 'memória' para paramêtros
// de função. Pilha guarda pequena variáveis locais

// a maioria dos tipos podem ter sua localização de dados explicitamente assinalos

// 3. Operações simples
// Comparações, operadores binários e aritimétricos são providos
// exponenciação: **
// ou exclusivo: ^
// negação binária: ~

// 4. Variáveis Globais de nota
// ** this **
this; // endereço do contrato
// geralmente usado no final do contrato para enviar o saldo restante às partes
this.balance;
this.algumFuncao(); // chamada de função externa via call, não via jump interno

// ** msg - Mensagem corrente recebida pelo contrato ** **
msg.sender; // endereço do enviador
msg.value; // quantidade de ether provida para este contrato em wei
msg.data; // bytes, todos os dados da chamada
msg.gas; // gas restante

// ** tx - Esta transação **
tx.origin; // endereço do enviador da transação
tx.gasprice; // valor do gas da transação

// ** block - Informação do bloco corrente **
now; // tempo corrente (aproxiamdo), substituto para block.timestamp
//(usa tempo do Unix)
block.number; // número do bloco corrente
block.difficulty; // dificuldade do bloco corrente
block.blockhash(1); // retorna bytes32, só funciona para os 256 blocos mais
//recentes
block.gasLimit();

// ** storage - Hash de disco persistente **
storage['abc'] = 'def'; // mapea palavras de 256 bits em palavras de 256 bits


// 4. FUNÇÕES E MAIS
// A. Funções
// Funções simples
function incremento(uint x) returns (uint) {
    x += 1;
    return x;
}

// Funções podem retornar muito argumentos, e podem especificar argumentos
// retornados sem necessidade de explicitamente usar return
function incremento(uint x, uint y) returns (uint x, uint y) {
    x += 1;
    y += 1;
}
// Chamando a função anterior
uint (a,b) = incremento(1,1);

// 'constant' indica que uam função não altera variáveis persistidas
// Funções constantes são executadas localmente, não na blockchain
uint y;

function incremento(uint x) constant returns (uint x) {
    x += 1;
    y += 1; // Esta linha deve falhar
    // y é uma variável de estado e não pode ser alterada por uma função local
}

// 'Especificadores de visibilidade de funções'
// Estes podem substituitir 'constante', incluíndo:
// public - visbilidade externa e interna (padrão)
// private - apenas visível no contrato corrente
// internal - apenas visível no contrato corrente e seus derivados

// Functions hosteada - e pode ser assinalada para variável
function a() {
    var z = b;
    b();
}

function b() {

}

// Prefira loops sobre recursões (pilha de chamada é no máximo 1024)

// B. Eventos
// Eventos podem notificar partes externas; facilmente buscáveis e acessáveis
// de fora da blockchain (com clientes leves)
// tipicamente declarados após os parâmetros do contrato

// Tipicamente, com letra maiúscula - e adicione Log na frente para
// ser explicito e previnir confusão na chamada da função

// Declaração
event LogEnvio(address indexed de, address indexed para, uint quantidade);
// Observe a letra maíscula no início do nome

// Chamada
Envio(de, para, quantidade);

// Para partes externas (um contrato ou entidade externo), observe:
Coin.Envio().watch({}, '', function(erro, resultado) {
    if (!erro) {
        console.log("Moeda transferida: " + resultado.args.quantidade +
            " moedas enviadas de " + resultado.args.de +
            " para " + resultado.args.para + ".");
        console.log("Saldo atual:\n" +
            "Enviador: " + Coin.balances.call(resultado.args.de) +
            "Recebedor: " + Coin.balances.call(resultado.args.para));
    }
}
// Paradigma comum para um contrato depender de outro (e.g., um contrato que
// depende da taxa de troca provida por outro)

// C. ModifiCadores
// MOdificadores validam entradas de funções, como saldo mínimo e autorização
// do usuário; semelhantes a guardas em outras linguagens

// '_' (subtraço) geralmente incluído como última linha do corpo, indica que a
// função sendo chamada deve ser colocada ali
modifier apenasDepois(uint _tempo) { if (agora <= _tempo) throw; _ }
modifier apenasDono { if (msg.sender == dono) _ }
// geralmente usado para máquina de estado
modifier apenasSeEmEstado (Estado estadoCorrente)
{ if (estadoCorrente != Estado.A) _ }

// Concatenado logo após a chamada da função
function mudeDona(novoDono)
apenasDepois(algumTempo)
apenasDono()
apenasSeEmEstado(Estado.A)
{
    dono = novoDono;
}

// subtração pode ser incluído antes do final do corpo, mas retorno explícitos
// pode ser ignorado, então, tome cuidado
modifier chequeValor(uint quantidade) {
    _
    if (msg.value > quantidade) {
        uint quantidadeASerDevolvida = quantidade - msg.value;
        if (!msg.sender.send(quantidadeASerDevolvida)) {
            throw;
        }
    }
}

// 6. BRANCHES E LOOPS

// Todas as lógicas básicas de bloco funcionam - incluindo if/else,
// while, break, continue, return - mas não há switch

// A sintaxe é semelhante a Javascript, mas sem conversão de tipos
// de não-booleanos para booleanos (operadores de comparação precisam
// utilizar valores booleanos)

// Loops que dependem o comportamento do usuário exigem cuidado - dado
// que contratos tem uma quantidade máxima de gas para cada bloco de
// de código - falhas acontecerão caso ele seja excedido
// Por exemplo:
for(uint x = 0; x < listaDeEnderecoDeRefundo.length; x++) {
    if (!listaDeEnderecoDeRefundo[x].send(ALGUMA_QUANTIDADE)) {
       throw;
    }
}

// Dois erros acima:
// 1. Uma falha no enviar para o loop completamente, travando dinheiro
// 2. Este loop pode ser abitrariamente longo (basado na quando que
// o usuário precisa de refundo), portanto, pode falhar quando exceder
// a quantidade máxima de gas do bloco
// Ao invés disso, você deve deixar as pessoas retirarem
// individualmente de suas subcontas e marcarem a retirada


// 7. OBJETOS/CONTRATOS

// A. Chamando um contrato externo
contract FonteDeInformacoes {
    function info() returns (uint ret) { return 42; }
}

contract Consumidor {
    FonteDeInformacoes fonte; // aponta para um contrato na blockchain

    // Assinala variável para uma instância do contrato
    function setFonte(address endereco) {
        // Cast automático, cuidado; construtor não é chamado
        fonte = FonteDeInformacoes(endereco);
    }

    // Assinala variável para uma nova instância do contrato
    function createNewFeed() {
        fonte = new FonteDeInformacoes(); // nova instância criada
        // construtor é chamado
    }

    function chameFonte() {
        // último parenteses chama o contrato, podendo adicionar
        // opcionalmente valores ou gas
        fonte.info.value(10).gas(800)();
    }
}

// B. Herança

// Ordem importa, último contrato herdado (i.e., 'def') pode
// sobrescrever partes de contratos previamente herdados
contract MeuContratdo is abc, def("um argumento personalizado def") {

// sobrescrevendo função
    function z() {
        if (msg.sender == dono) {
            def.z(); // chama função sobrescrita de def
            super.z(); // chama função do pai imeadiato
        }
    }
}

// função abstrata
function umaFuncaoAbstrata(uint x);
// não pode ser compilada, usada em contratos base/abstratos que
// então, a implementam

// C. Import

import "filename";
import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol";

// 'Import' está sobre desenvolvimento
// Atualmente não pode ser usada na linha de comando


// 8.OUTRAS PALAVRAS-CHAVE

// A. Throwing
// Throwing
throw; // reverte estado e dinheiro NÃO-USADO é devolvido ao usuários
// Atualmente não pode ser capturado

// Um padrão de design comum é:
if (!endereco.send(123)) {
    throw;
}

// B. Selfdestruct
// auto-destroe o contrato corrente, enviando fundos para o endereço
// (geralmente o criador)
selfdestruct(ALGUM_ENDERECO);

// remove disco/código dos blocos corrente e futuros
// ajuda clientes leves, mas dados persistidos continuam no blockchain

// Padrão comum, permite ao dono finalizar o contrato e receber fundos
// restantes
function remover() {
    if(msg.sender == criador) { // Apenas o criador do contrato pode
                                // fazer isso
        selfdestruct(criador); // Inativa o contrato e retorna os fundos
    }
}

// Talvez queria desativar o contrato manualmente, ao invés de usar
// selfdestruct (ether enviado para contratos selfdestructed é perdido)


// 9. NOTAS SOBRE DESIGN DE CONTRATOS

// A. Obfuscação
// Todas as variáveis são publicamente visíveis na blockchain, então
// qualquer coisa privada precisa ser obfuscada (e.g., hash com segredo)

// Passo-a-pass: 1. Comprometa-se com algo, 2. Revele compromisso
sha3("quantidade_de_lance", "algum segredo"); // compromisso

// chame a função reveal (revelar) do contrato no futuros
// mostrando o lance mais o segredo para foi hasheado com SHA3
reveal(100, "meuSegredo");

// B. Otimização de disco
// Escrever na blockchain pode ser caro, dado que os dados são guardados
// para sempre. É encorajado que contratos inteligentes usem memória (
// enventualmente, compilação será melhor, mas por enquanto é benéfico
// usar estruturas de dados simples - armazenando minimamente na
// blockchain)

// Custo pode ser alto para item como arrays multidimensionais
// (custo para guardar os dados - não declarar variáveis)

// C. Acesso de dados da blockchain

// Não pode restringir humanos ou computadores de ler os conteúdos
// de transações ou estado de transações

// Enquanto 'private' previne outros *contratos* de ler dados ]
// diretamente - qualquer outra parte ainda pode ler dados da blockchain

// Todos os dados são armazedos na blockchain, para que qualquer um
// possa observar dados antigos e mudanças

// D. Jobs Cron
// Contratos deve ser manualmente chamados para lidar com agendamentos
// baseados em tempo; podendo criar código externo para pingar
// regularmente ou prover incentivos (ether) para outros fazê-lo

// E. Padrão Observador
// O Padrão Observador permite que você registre um inscritor e
// registre uma função a ser chamada pelo Oráculo (nota, Oráculos pagam
// pela ação executada). Similarmente à subscrição em Pub/sub

// Este é um contrato abstrato, tanto as classes cliente como a
// servidor importam o cliente que deve ser implementado
contract AlgumOraculoCallback {
    function OraculoCallback(int _valor, uint _tempo, bytes32 info) external;
}

contract AlgumOráculo {
    AlgumOraculoCallback[] callbacks; // array com todos os inscritos

    // Registra inscrito
    function addInscrito(AlgumOraculoCallback a) {
        callbacks.push(a);
    }

    function notificar(valor, tempo, info) private {
        for(uint i = 0;i < callbacks.length; i++) {
            // todos os inscritos precisam implementar AlgumOraculoCallback
            callbacks[i].OraculoCallback(valor, tempo, info);
        }
    }

    function facaAlgo() public {
        // Código para fazer algo

        // Notifica todos os inscrito
        notificar(_valor, _tempo, _info);
    }
}

// Agora, seu contrato cliente pode addInscrito importando
// AlgumOraculoCallback e registrando algum Oráculo

// F. Máquinas de estado
// veja o exemplo abaixo para o enum Estado e o modificador noEstado

// *** EXEMPLO: Um exemplo de crowdfunding example (similar ao
// Kickstarter) ***
// ** INCIO DO EXEMPLO **

// FundadorDoCrowdFunding.sol

/// @title FundadorDoCrowdFunding
/// @author nemild
contract FundadorDoCrowdFunding {
    // Variáveis assinaladas na crição pelo criador
    address public criador;
    address public recipiente; // criador pode ser diferente do Recipiente
    uint public minALevantar; // requisito para pagar, pelo contrário
                              // os doadores recebem o dinheiro de volta
    string urlDaCampanha;
    byte constant versao = 1;

    // Estruturas de dados
    enum Estado {
        LevantandoFundos,
        RefundoExpirado,
        Sucesso
    }
    struct Contribuicao {
        uint quantidade;
        address contribuidor;
    }

    // Variáveis de Estado
    State public estado = Estado.LevantandoFundos; // incializado na criação
    uint public totalLevantado;
    uint public levantadoPor;
    uint public completadoEm;
    Contribution[] contribuidores;

    event LogRecebimentoDeFundos(address endereco,
                                 uint quantidade,
                                 uint totalAtual);
    event LogFundosPagos(address enderecoDoRecebedor);

    modifier noEstado(Estado _estado) {
        if (estado != _estado) throw;
        _
    }

    modifier eOCriador() {
        if (msg.sender != criador) throw;
        _
    }

    // Aguarda 6 meses após o final do contrato para destruí-lo
    modifier noFimDoContrato() {
    if(!((estado == Estado.RefundoExpirado || estado == Estado.Sucesso) &&
        completadoEm + 6 months < now)) {
            throw;
        }
        _
    }

    function FundadorDoCrowdFunding(
        uint tempoEmHorasParaFundraising,
        string _urlDaCampanha,
        address _recipiente,
        uint _minALevantar)
    {
        criador = msg.sender;
        recipiente = _recipiente;
        urlDaCampanha = _urlDaCampanha;
        minALevantar = _minALevantar;
        levantadoPor = now + (tempoEmHorasParaFundraising * 1 hours);
    }

    function contribuir()
    public
    noEstado(Estado.LevantandoFundos)
    {
        contribuidores.push(
            Contribuicao({
                quantidade: msg.value,
                contribuidor: msg.sender
            }) // use array, para podermos iterar
        );
        totalLevantado += msg.value;

        LogRecebimentoDeFundos(msg.sender, msg.value, totalRaised);

        verifiqueSeLevantamentoFoiCompletadoOuExpirado();
        return contribuicoes.length - 1; // retorna id
    }

    function verifiqueSeLevantamentoFoiCompletadoOuExpirado() {
        if (totalLevantado > minALevantar) {
            estado = Estado.Sucesso;
            pagar();

            // pode incentivar enviador que iniciou a mudanção de estado
        } else if ( now > levantadoPor )  {
            estado = Estado.RefundoExpirado; // backers podem coletar
                                             // o fundo chamando receberRefundo(id)
        }
        completadoEm = now;
    }

    function pagar()
    public
    emEstado(Estado.Sucesso)
    {
        if(!recipiente.send(this.balance)) {
            throw;
        }


        LogFundosPagos(fundRecipient);
    }

    function receberRefundo(id)
    public
    emEstado(Estado.RefundoExpirado)
    {
        if (contribuicoes.length <= id || id < 0 || contribuicoes[id].amount == 0 ) {
            throw;
        }

        uint quantidadeDeRefundo = contribuicoes[id].amount;
        contribuicoes[id].amount = 0;

        if(!contribuicoes[id].contribuidor.send(quantidadeParaEnviar)) {
            contribuicoes[id].amount = quantidadeParaEnviar;
            return false;
        }

      return true;
    }

    function removerContrato()
    public
    eOCriador()
    noFimDoContrato()
    {
        selfdestruct(msg.sender);
        // criador recebe todo o dinheiro restante{

    }

    function () { throw; }
}
// ** FIM DO EXEMPLO **

// 10. OUTRAS FUNÇÕES NATIVAS

// Unidades monetárias
// Moeda é definida usando wei, menor quantidade de ether
uint quantidadeMin = 1 wei;
uint a = 1 finney; // 1 ether == 1000 finney
// Para outras unidades, veja: http://ether.fund/tool/converter

// Unidades temporais
1 == 1 second // segundos
1 minutes == 60 seconds // Minutos

// Pode multiplicar uma variável de tempo, dado que unidades não são guardadas
// na variável
uint x = 5;
(x * 1 days); // 5 dias

// Cuidado com o salto de segundos / anos com declarações de igualdade para o tempo
// (em vez disso, prefira maior que / menor que)

// Criptografia
// Todas as string passadas são concatenadas antes de realizar hashing
sha3("ab", "cd");
ripemd160("abc");
sha256("def");

// 11. Segurança

// Bugs são desastrosos para contratos Ethereum - até padrões Solidity populares
// podem ser considerados anti-padrões

// Veja links para segurança no final deste documento

// 12. FUNÇÕES DE BAIXO NÍVELS
// call - baixo nível, geralmente não usada, não tem segurança de tipos
booleanSucesso = algumEnderecoDeContrato.call('nome_da_funcao', 'arg1', 'arg2');

// callcode - Código no endereço alvo executado no *contexto* do contrato
// de chamada. Fornece funcionalidade de biblioteca
algumEnderecoDeContrato.callcode('nome_da_funcao');


// 13. NOTAS DE ESTILO
// Baseado no guia de estilo PEP8 do Python

// Resumo rápido:
// 4 espaços para identação
// Duas linhas separam declaração de contratos (e outras declarações de alto nível)
// Evite espaços estranhos entre parênteses
// Pode omitir chaves curly para uma declaração de linha(if, for, etc)
// else deve ser colocado na própria linha


// 14. COMENTÁRIOS NATSPEC
// usado para documentação, comentários e UIs externos

// Contrato natspec - sempre acima da definição do contrato
/// @title Título do Contrato
/// @author Nome do autor

// Função natspec
/// @notice informações sobre o que funciona; mostrado quando a função é executada
/// @dev Documentação de função para desenvolvedor

// Parâmetro de função / valor de retorno  natspec
/// @param algumParametro Alguma descrição do que o parametro faz
/// @return Descrição do valor de retorno
```

## Recursos adicionais
- [Documetanção Solidity](https://solidity.readthedocs.org/en/latest/)
- [Guia de Estilo do Solidity](https://ethereum.github.io/solidity//docs/style-guide/):
 O guia de estilo Ethereum é derivado do guia de estilo do Python [pep8](https://www.python.org/dev/peps/pep-0008/).
- [Editor de Browser Solidity](http://chriseth.github.io/browser-solidity/)
- [Gitter Solidity Chat room](https://gitter.im/ethereum/solidity)
- [Estratégias de projeto modular para contratos Ethereum](https://docs.erisindustries.com/tutorials/solidity/)

## Contratos de Exemplo
- [Dapp Bin](https://github.com/ethereum/dapp-bin)
- [Solidity Baby Step Contracts](https://github.com/fivedogit/solidity-baby-steps/tree/master/contracts)
- [ConsenSys Contracts](https://github.com/ConsenSys/dapp-store-contracts)
- [State of Dapps](http://dapps.ethercasts.com/)

## Segurança
- [Thinking About Smart Contract Security](https://blog.ethereum.org/2016/06/19/thinking-smart-contract-security/)
- [Smart Contract Security](https://blog.ethereum.org/2016/06/10/smart-contract-security/)
- [Hacking Distributed Blog](http://hackingdistributed.com/)

## Informação excluída intencionalmente
- Libraries

## Estilo
- [PEP8](https://www.python.org/dev/peps/pep-0008/) é usado como guia de estilo,
 incluindo sua filosofia geral

## Editores
- [Vim Solidity](https://github.com/tomlion/vim-solidity)
- Snippets de Editores ([Ultisnips format](https://gist.github.com/nemild/98343ce6b16b747788bc))

## Trabalhos Futuros
- Novas palavras-chave: protected, inheritable
- Lista de padrões de design comuns (throttling, RNG, atualização de versão)
- Padrões anti-segurança comuns


Sinta-se a vontade para enviar um pull request com quaisquer edições - ou email
para nemild - / at- / gmail
---
language: swift
filename: learnswift-pt.swift
contributors:
    - ["Grant Timmerman", "http://github.com/grant"]
    - ["Christopher Bess", "http://github.com/cbess"]
translators:
    - ["Mariane Siqueira Machado", "https://twitter.com/mariane_sm"]
lang: pt-br

---

Swift é uma linguagem de programação para desenvolvimento de aplicações no iOS e OS X criada pela Apple. Criada para
coexistir com Objective-C e para ser mais resiliente a código com erros, Swift foi apresentada em 2014 na Apple's
developer conference WWDC. Foi construída com o compilador LLVM já incluído no Xcode 6 beta.

O livro oficial [Swift Programming Language] (https://itunes.apple.com/us/book/swift-programming-language/id881256329) da
Apple já está disponível via IBooks (apenas em inglês).

Confira também o tutorial completo de Swift da Apple [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/RoadMapiOS/index.html), também disponível apenas em inglês.

```swift
// importa um módulo
import UIKit

//
// MARK: Noções básicas
//

// Xcode supporta anotações para seu código e lista elas na barra de atalhos
// MARK: Marca uma sessão
// TODO: Faça algo logo
// FIXME: Conserte esse código

println("Hello, world")

// Valores em variáveis (var) podem ter seu valor alterado depois de declarados.
// Valores em constantes (let) NÃO podem ser alterados depois de declarados.

var myVariable = 42
let øπΩ = "value" // nomes de variáveis em unicode
let π = 3.1415926
let convenience = "keyword" // nome de variável contextual
let weak = "keyword"; let override = "another keyword" // comandos podem ser separados por uma ponto e vírgula
let `class` = "keyword" // Crases permitem que palavras-chave seja usadas como nome de variáveis
let explicitDouble: Double = 70
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "some text " + String(myVariable) // Coerção
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // Interpolação de strings

// Constrói valores específicos
// Utiliza configuração de build -D
#if false
    println("Not printed")
    let buildValue = 3
#else
    let buildValue = 7
#endif
println("Build value: \(buildValue)") // Build value: 7

/*
    Optionals fazem parte da linguagem e permitem que você armazene um
    valor `Some` (algo) ou `None` (nada).

    Como Swift requer que todas as propriedades tenham valores, até mesmo nil deve
    ser explicitamente armazenado como um valor Optional.

    Optional<T> é uma enum.
*/
var someOptionalString: String? = "optional" // Pode ser nil
// o mesmo acima, mas ? é um operador pós-fixado (açúcar sintático)
var someOptionalString2: Optional<String> = "optional"

if someOptionalString != nil {
    // Eu não sou nil
    if someOptionalString!.hasPrefix("opt") {
        println("has the prefix")
    }

    let empty = someOptionalString?.isEmpty
}
someOptionalString = nil

// Optional implicitamente desempacotado (unwrapped)
var unwrappedString: String! = "Valor é esperado."
// o mesmo acima, mas ? é um operador pósfixado (açúcar sintático)
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Valor é esperado."

if let someOptionalStringConstant = someOptionalString {
    // Tem `Some` (algum) valor, não nil
    if !someOptionalStringConstant.hasPrefix("ok") {
        // não possui o prefixo
    }
}

// Swift tem suporte para armazenar um valor de qualquer tipo.
// AnyObject == id
// Ao contrário de Objective-C `id`, AnyObject funciona com qualquer valor (Class, Int, struct, etc)
var anyObjectVar: AnyObject = 7
anyObjectVar = "Mudou o valor para string, não é uma boa prática, mas é possível."

/*
Comentário aqui
    /*
        Comentários aninhados também são suportados
    */
*/

//
// MARK: Coleções
//

/*
    Tipos Array e Dicionário são structs. Portanto `let` e `var`
    também indicam se são mutáveis (var) ou imutáveis (let) quando declarados
    com esses tipos.
*/

// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
let emptyArray = [String]() // imutável
var emptyMutableArray = [String]() // mutável


// Dicionário
var occupations = [
    "Malcolm": "Captain",
    "kaylee": "Mechanic"
]
occupations["Jayne"] = "Public Relations"
let emptyDictionary = [String: Float]() // imutável
var emptyMutableDictionary = [String: Float]() // mutável


//
// MARK: Controle de fluxo
//

// laço for (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
    if value == 1 {
        println("One!")
    } else {
        println("Not one!")
    }
}

// laço for (dicionário)
var dict = ["one": 1, "two": 2]
for (key, value) in dict {
    println("\(key): \(value)")
}

// laço for (alcance)
for i in -1...shoppingList.count {
    println(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// use ..< para excluir o último número

// laço while (enquanto)
var i = 1
while i < 1000 {
    i *= 2
}

// laço do-while
do {
    println("hello")
} while 1 == 2

// Switch
let vegetable = "red pepper"
switch vegetable {
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(x)?"
default: // required (in order to cover all possible input)
    let vegetableComment = "Everything tastes good in soup."
}


//
// MARK: Funções
//

// Funções são tipos de primeira classe, o que significa que eles podem ser aninhados
// em funções e podem ser passados como parâmetros

// Funções Swift com cabeçalhos doc (formato como reStructedText)
/**
Uma operação de saudação

- Um bullet em documentos
- Outro bullet

:param: nome A nome
:param: dia A dia
:returns: Uma string contendo o nome e o dia.
*/
func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")

// Função que retorna múltiplos items em uma tupla
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// Ignore valores de Tuplas (ou outros valores) usando _ (underscore)
let (_, price1, _) = pricesTuple // price1 == 3.69
println(price1 == pricesTuple.1) // true
println("Gas price: \(price)")

// Número variável de argumentos
func setup(numbers: Int...) {
    // é um array
    let number = numbers[0]
    let argCount = numbers.count
}

// Passando e retornando funções
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

// passagem por referência
func swapTwoInts(inout a: Int, inout b: Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(&someIntA, &someIntB)
println(someIntB) // 7


//
// MARK: Closures
//
var numbers = [1, 2, 6]

// Funções são casos especiais de closures ({})

// Exemplo de closure.
// `->` separa argumentos e tipo de retorno
// `in` separa o cabeçalho do closure do seu corpo
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})

// Quando o tipo é conhecido, como abaixo, nós podemos fazer o seguinte
numbers = numbers.map({ number in 3 * number })
// Ou até mesmo isso
//numbers = numbers.map({ $0 * 3 })

print(numbers) // [3, 6, 18]

// Closure restante
numbers = sorted(numbers) { $0 > $1 }

print(numbers) // [18, 6, 3]

// Super atalho, já que o operador < infere os tipos

numbers = sorted(numbers, < )

print(numbers) // [3, 6, 18]

//
// MARK: Estruturas
//

// Estruturas e classes tem funcionalidades muito similares
struct NamesTable {
    let names: [String]

    // Custom subscript
    subscript(index: Int) -> String {
        return names[index]
    }
}

// Estruturas possuem um inicializador auto-gerado automático (implícito)
let namesTable = NamesTable(names: ["Me", "Them"])
//let name = namesTable[2]
//println("Name is \(name)") // Name is Them

//
// MARK: Classes
//

// Classes, Estruturas e seus membros possuem três níveis de modificadores de acesso
// Eles são: internal (default), public, private

public class Shape {
    public func getArea() -> Int {
        return 0;
    }
}

// Todos os métodos e propriedades de uma classe são públicos.
// Se você só precisa armazenar dados em um objeto estruturado, use `struct`

internal class Rect: Shape {
    var sideLength: Int = 1

    // Getter e setter personalizado
    private var perimeter: Int {
        get {
            return 4 * sideLength
        }
        set {
            // `newValue` é uma variável implicita disponível para os setters
            sideLength = newValue / 4
        }
    }

    // Carregue uma propriedade sob demanda (lazy)
    // subShape permanece nil (não inicializado) até seu getter ser chamado
    lazy var subShape = Rect(sideLength: 4)

    // Se você não precisa de um getter e setter personalizado,
    // mas ainda quer roda código antes e depois de configurar
    // uma propriedade, você  pode usar `willSet` e `didSet`
    var identifier: String = "defaultID" {
        // o argumento `willSet` será o nome da variável para o novo valor
        willSet(someIdentifier) {
            print(someIdentifier)
        }
    }

    init(sideLength: Int) {
        self.sideLength = sideLength
         // sempre chame super.init por último quand inicializar propriedades personalizadas (custom)
        super.init()
    }

    func shrink() {
        if sideLength > 0 {
            sideLength -= 1
        }
    }

    override func getArea() -> Int {
        return sideLength * sideLength
    }
}

// Uma classe básica `Square` que estende `Rect`
class Square: Rect {
    convenience init() {
        self.init(sideLength: 5)
    }
}

var mySquare = Square()
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4

// Compara instâncias, não é o mesmo que == o qual compara objetos
if mySquare === mySquare {
    println("Yep, it's mySquare")
}


//
// MARK: Enums
//

// Enums podem opcionalmente ser de um tipo específico ou não.
// Podem conter métodos do mesmo jeito que classes.

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func getIcon() -> String {
        switch self {
        case .Spades: return "♤"
        case .Hearts: return "♡"
        case .Diamonds: return "♢"
        case .Clubs: return "♧"
        }
    }
}


//
// MARK: Protocolos
//

// `protocol` pode requerer que os tipos que se adequam tenham
// propriedades de instância, métodos, operadores e subscripts.
protocol ShapeGenerator {
    var enabled: Bool { get set }
    func buildShape() -> Shape
}

// Protocolos declarados com @objc permitem funções opcionais,
// que permitem verificar a confomidade
@objc protocol TransformShape {
    optional func reshaped()
    optional func canReshape() -> Bool
}

class MyShape: Rect {
    var delegate: TransformShape?

    func grow() {
        sideLength += 2

        if let allow = self.delegate?.canReshape?() {
            // test for delegate then for method
            // testa por delegação e então por método
            self.delegate?.reshaped?()
        }
    }
}


//
// MARK: Outros
//

// `extension`s: Adicionam uma funcionalidade extra para um tipo já existente.

// Square agora "segue" o protocolo `Printable`
extension Square: Printable {
    var description: String {
        return "Area: \(self.getArea()) - ID: \(self.identifier)"
    }
}

println("Square: \(mySquare)")

// Você pode também estender tipos embutidos (built-in)
extension Int {
    var customProperty: String {
        return "This is \(self)"
    }

    func multiplyBy(num: Int) -> Int {
        return num * self
    }
}

println(7.customProperty) // "This is 7"
println(14.multiplyBy(2)) // 42

// Generics: Similar com Java e C#. Use a palavra-chave `where` para
// especificar os requisitos do generics.

func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
    for (index, value) in enumerate(array) {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let foundAtIndex = findIndex([1, 2, 3, 4], 3)
println(foundAtIndex == 2) // true

// Operadores:
// Operadores personalizados (custom) podem começar com os seguintes caracteres:
//      / = - + * % < > ! & | ^ . ~
// ou
// Unicode math, símbolo, seta, e caracteres tipográficos ou de desenho.
prefix operator !!! {}

// Um operador de prefixo que triplica o comprimento do lado do quadrado
// quando usado
prefix func !!! (inout shape: Square) -> Square {
    shape.sideLength *= 3
    return shape
}

// valor atual
println(mySquare.sideLength) // 4

// Troca o comprimento do lado usando um operador personalizado !!!, aumenta o lado por 3
!!!mySquare
println(mySquare.sideLength) // 12

```
---
category: tool
tool: tmux
contributors:
    - ["mdln", "https://github.com/mdln"]
translators:
    - ["Luis Custodio", "http://luiscustodio.com"]
lang: pt-br
filename: LearnTmux-pt.txt
---

O [tmux](http://tmux.sourceforge.net) é um multiplexador de terminal,
ele permite criar vários terminais e gerenciar tudo na mesma interface.
tmux pode também rodar em background e depois ser recuperado(exibido) novamente.

```

  tmux [command]     # Roda um [comando]
                     # 'tmux' sem comandos irá criar uma nova seção

    new              # Cria uma nova seção
     -s "Nome"       # Cria uma nova seção com nome "Nome"
     -n "Janela"     # Cria uma janela com o nome "Janela"
     -c "/dir"       # Inícia em uma pasta específica

    attach           # Acopla a última seção disponível
     -t "#"          # Acopla a seção com nome "#"
     -d              # Separa (Desacopla) a seção de outras instâncias.

    ls               # Lista todas as seções
     -a              # Lista todas as seções abertas

    lsw              # Lista as janelas
     -a              # Lista todas as janelas
     -s              # Lista todas janleas em uma seção

    lsp              # Lista os painéis
     -a              # Lista todos os painéis
     -s              # Lista todos os painéis em uma seção
     -t "#"          # Lista os painéis chamados "#"

    kill-window      # Encerrar a janela atual
     -t "#"          # Encerrar a janela chamada "#"
     -a              # Encerrar todas as janelas
     -a -t "#"       # Encerrar todas as janelas exceto a "#"

    kill-session     # Encerrar seção atual
     -t "#"          # Encerrar seção com nome "#"
     -a              # Encerrar todas as seções
     -a -t "#"       # Encerrar todas as seções exceto a "#"

```

### Teclas de atalhos (comandos)

As seções tmux acopladas são controladas através de teclas de atalho. (prefix key)

```
----------------------------------------------------------------------
  (C-b) = Ctrl + b    # Abre a opção de receber comandos(atalhos).

  (M-1) = Meta + 1 -or- Alt + 1
----------------------------------------------------------------------

  ?           # Lista todos os comandos.
  :           # Acessa o prompt command do tmux
  r           # Força a reinicialização do cliente acoplado.
  c           # Cria uma nova janela.

  !           # Retira o painel atual da janela.
  %           # Divide o painel atual em dois. Esquerda e direita.
  "           # Divide o painel atual em dois. Para cima e para baixo.

  n           # Muda para a próxima janela.
  p           # Muda para a janela anterior.
  {           # Troca o painel atual pelo anterior.
  }           # Troca o painel corrent pelo posterior.

  s           # Seleciona uma nova seção para o cliente acoplado iterativamente.
  w           # Seleciona a janela atual iterativamente.
  0 to 9      # Seleciona a janela de 0 à 9.

  d           # Separa o cliente atual.
  D           # Seleciona um cliente a ser separado.

  &           # Encerra a janela atual.
  x           # Encerra o painel atual.

  Up, Down    # Move para o painel acima, abaixo, a esquerda ou a direita.
  Left, Right

  M-1 to M-5  # Organiza os paines:
                       # 1) Horizontalmente de maneira igual
                       # 2) Verticalmente de maineira igual.
                       # 3) Principal horizontalmente
                       # 4) Principal verticamente.
                       # 5) Mosaico

  C-Up, C-Down    # Altera o tamanho do painel atual em uma célula.
  C-Left, C-Right

  M-Up, M-Down    # Altera o tamanho do painel atual em cinco células.
  M-Left, M-Right

```


### Configurando ~/.tmux.conf

Existe um arquivo chamado tmux.conf, ele pode ser usado para definir opções no
 momento de inicialização, da mesma maneira que .vimrc, init.el, .bash_profile são usados.


```
# Exemplo tmux.conf
# 2015.12


### General
###########################################################################

# Limite da história de comandos
set -g history-limit 2048

# Indíce de inicialização
set -g base-index 1

# Mouse
set-option -g -q mouse on

# Recarregar o arquivo de configuração sem a necessidade de reiniciar o programa
unbind r
bind r source-file ~/.tmux.conf


### Keybinds / Comandos
###########################################################################

# Desvincular C-b como prefixo padrão.
unbind C-b

# Define um novo prefixo padrão.
set-option -g prefix `

# Voltar janela anterior quando comando for usado duas vezes.
bind C-a last-window
bind ` last-window

# Fazer com que F11 e F12 alterem o comportamento de C-a e `
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Preferencia de comandos
setw -g mode-keys vi
set-option -g status-keys vi

# Alternar enter painéis com teclas de orientaçao do vim
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Iterar entre as Janelas
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# Dividir painéis
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# Habilitar a sub-seção a enviar comandos.
bind a send-prefix


### Theme // Estilo
###########################################################################

# Paleta de cores para a barra de status
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# Paleta de cores para bordas do painel
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Palta de cores para mensagem
set-option -g message-fg black
set-option -g message-bg green

# Paleta de cores para janela de status
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### UI
###########################################################################

# Notificações
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# Definir automaticamente o título de janelas
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not)

# Ajustes na barra de status
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# Mostrar indicativos de performance na barra de status
# Requires https://github.com/thewtex/tmux-mem-cpu-load/
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```


### Referências

[Tmux | Início](http://tmux.sourceforge.net)

[Manual Tmux (em inglês)](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)

[Mostrar CPU/MEM % in statusbar](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)

Possui uma sugestão? Uma correção, talvez? Abra um issue no Repositório GitHub, ou então faça um pull request.
---
language: TypeScript
filename: learntypescript-pt.ts
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
translators:
  - ["Gabriel Gomes", "https://github.com/gabrielgomesferraz"]
lang: pt-br
---

TypeScript is a language that aims at easing development of large scale applications written in JavaScript.
TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript.
It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. The TypeScript compiler emits JavaScript.

This article will focus only on TypeScript extra syntax, as opposed to [JavaScript] (../javascript/).


Typescript é uma linguagem que visa facilitar o desenvolvimento de aplicações em grande escala escritos em JavaScript.
Typescript acrescenta conceitos comuns como classes, módulos, interfaces, genéricos e (opcional) tipagem estática para JavaScript.
É um super conjunto de JavaScript: todo o código JavaScript é o código do texto dactilografado válido para que possa ser adicionados diretamente a qualquer projeto. O compilador emite typescript JavaScript.

Este artigo irá se concentrar apenas em texto datilografado sintaxe extra, ao contrário de [JavaScript](javascript-pt.html.markdown).

Para testar compilador do texto datilografado, de cabeça para o [Parque](http://www.typescriptlang.org/Playground), onde você vai ser capaz de escrever código, ter auto conclusão e ver diretamente o JavaScript emitida.

```js
// Existem 3 tipos básicos no TypeScript
var isDone: boolean = false;
var lines: number = 42;
var name: string = "Anders";

// Quando é impossível saber, há o "Qualquer" tipo
var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // Ok, definitivamente um boolean

// Para coleções, não são matrizes e matrizes genéricas digitado
var list: number[] = [1, 2, 3];
// Como alternativa, usando o tipo de matriz genérica
var list: Array<number> = [1, 2, 3];

// Para enumerações:
enum Color {Red, Green, Blue};
var c: Color = Color.Green;

// Por último, "vazio" é utilizado no caso especial de uma função que não retorna nada
function bigHorribleAlert(): void {
  alert("I'm a little annoying box!");
}

// Funções são cidadãos de primeira classe, apoiar a sintaxe lambda "seta gordura" e
// Tipo de uso inferência

// A seguir são equivalentes, a mesma assinatura será inferido pelo
// Compilador, e mesmo JavaScript será emitido
var f1 = function(i: number): number { return i * i; }
// Tipo de retorno inferida
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
// Tipo de retorno inferida
var f4 = (i: number) => { return i * i; }
// Tipo de retorno inferido, one-liner significa nenhuma palavra-chave retorno necessário
var f5 = (i: number) =>  i * i;

// Interfaces são estruturais, qualquer coisa que tenha as propriedades é compatível com
// A interface
interface Person {
  name: string;
  // Propriedades opcionais, marcado com um "?"
  age?: number;
  // E de funções curso
  move(): void;
}

// Objeto que implementa a "Pessoa" Interface
// Pode ser tratado como uma pessoa desde que tem o nome e mover propriedades
var p: Person = { name: "Bobby", move: () => {} };
// Os objetos que têm a propriedade opcional:
var validPerson: Person = { name: "Bobby", age: 42, move: () => {} };
// Não é uma pessoa porque a idade não é um número
var invalidPerson: Person = { name: "Bobby", age: true };

// Interfaces também pode descrever um tipo de função
interface SearchFunc {
  (source: string, subString: string): boolean;
}
// Somente tipos dos parâmetros são importantes, os nomes não são importantes.
var mySearch: SearchFunc;
mySearch = function(src: string, sub: string) {
  return src.search(sub) != -1;
}

// Classes - membros são públicos por padrão
class Point {
  // Propriedades
    x: number;

    // Construtor - the public/private keywords in this context will generate
    // o código clichê para a propriedade e a inicialização no
    // construtor.
    // Neste exemplo, "y" será definida como "X" é, mas com menos código
    // Os valores padrão também são suportados.

    constructor(x: number, public y: number = 0) {
        this.x = x;
    }

    // Funções
    dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

    // Membros Estáticos
    static origin = new Point(0, 0);
}

var p1 = new Point(10 ,20);
var p2 = new Point(25); //y será 0

// Herança
class Point3D extends Point {
    constructor(x: number, y: number, public z: number = 0) {
        super(x, y); // Chamada explícita para o construtor da super classe é obrigatória
    }

    // Sobrescrever
    dist() {
        var d = super.dist();
        return Math.sqrt(d * d + this.z * this.z);
    }
}

// Módulos, "." pode ser utilizado como separador de sub módulos
module Geometry {
  export class Square {
    constructor(public sideLength: number = 0) {
    }
    area() {
      return Math.pow(this.sideLength, 2);
    }
  }
}

var s1 = new Geometry.Square(5);

// Alias no local para fazer referência a um módulo
import G = Geometry;

var s2 = new G.Square(10);

// Genericos
// Classes
class Tuple<T1, T2> {
    constructor(public item1: T1, public item2: T2) {
    }
}

// Interfaces
interface Pair<T> {
    item1: T;
    item2: T;
}

// e funções
var pairToTuple = function<T>(p: Pair<T>) {
    return new Tuple(p.item1, p.item2);
};

var tuple = pairToTuple({ item1:"hello", item2:"world"});

// Incluindo referências a um arquivo de definição:
/// <reference path="jquery.d.ts" />

```

## Leitura adicional
 * [TypeScript site oficial](http://www.typescriptlang.org/)
 * [TypeScript especificações de idioma (pdf)](http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg - Apresentando texto datilografado no Canal 9](http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [Código fonte no GitHub](https://github.com/Microsoft/TypeScript)
 * [Definitivamente datilografado - repositório de definições de tipo](http://definitelytyped.org/)
---
category: tool
tool: vim
contributors:
    - ["RadhikaG", "https://github.com/RadhikaG"]
translators:
    - ["David Lima", "https://github.com/davelima"]
lang: pt-br
filename: LearnVim-pt.txt
---


[Vim](http://www.vim.org)
(Vi IMproved - Vi Melhorado) é um clone do editor vi para Unix. Ele é um
editor de texto projetado para ter velocidade e produtividade, e está presente
na maioria dos systemas UNIX. O editor tem um grande número de atalhos de teclado
para agilizar a navegação para pontos específicos no arquivo, além de edição rápida.

## Navegação do Vim: o básico

```
    vim <nome-do-arquivo>   # Abre <nome-do-arquivo> no vim
    :q               # Fecha o vim
    :w               # Salva o arquivo atual
    :wq              # Salva o arquivo e fecha o vim
    :q!              # Fecha o vim e descarta as alterações no arquivo
                     # ! *força* :q a executar, fechando o vim sem salvar antes
    :x               # Salvar o arquivo e fechao vim (atalho para :wq)

    u                # Desfazer
    CTRL+R           # Refazer

    h                # Move o cursor para a esquerda
    j                # Move o cursor para baixo
    k                # Move o cursor para cima
    l                # Move o cursor para a direita

    # Movendo o cursor dentro da linha

    0                # Move para o início da linha
    $                # Move para o final da linha
    ^                # Move para o primeiro caractere da linha (ignora caracteres em branco)

    # Pesquisa no texto

    /palavra         # Destaca todas as ocorrências de 'palavra' após o cursor
    ?palavra         # Destaca todas as ocorrências de 'palavra' antes do cursor
    n                # Move o cursor para a próxima ocorrência após a pesquisa
    N                # Move o cursor para a ocorrência anterior após a pesquisa

    :%s/foo/bar/g    # Substitui 'foo' por 'bar' no arquivo inteiro
    :s/foo/bar/g     # Substitui 'foo' por 'bar' na linha atual

    # Pulando para caracteres específicos

    f<caracter>      # Posiciona o cursor no próximo <caracter>
    t<character>     # Posiciona o cursor antes do próximo <caracter> 

    # Por exemplo,    
    f<               # Posiciona o cursor no <
    t<               # Posiciona o cursor logo antes do <
    
    # Movendo por palavras

    w                # Move o cursor uma palavra a diante
    b                # Move o cursor uma palavra atrás
    e                # Move o cursor ao fim da palavra atual

    # Outros caracteres para mover o cursor no arquivo

    gg               # Move para o topo do arquivo
    G                # Move para o final do arquivo
    :NUM             # Move para a linha NUM (NUM é qualquer número)
    H                # Move para o topo da tela visível
    M                # Move para o meio da tela visível
    L                # Move para o final da tela visível
```

## Modos:

O Vim é baseado no conceito de **modos**.

Modo Comando  - usado para navegar e escrever comandos - o Vim já inicia nesse modo
Modo Inserção - usado para fazer alterações no arquivo
Modo Visual   - usado para destacar textos e executar comandos neles
Modo Ex       - usado para ir a linha com ':' no final da tela para executar comandos 

```
    i                # Coloca o Vim no Modo Inserção, logo antes do cursor
    a                # Coloca o Vim no Modo Inserção, logo após o cursor
    v                # Coloca o Vim no Modo Visual 
    :                # Coloca o Vim no Modo Ex
    <esc>            # Sai de qualquer modo que você estiver, e coloca o Vim no Modo Comando

    # Copiando e colando texto

    y                # Coloca a seleção atual na área de transferência
    yy               # Coloca a linha atual na área de transferência
    d                # Deleta a seleção tual
    dd               # Deleta a linha atual
    p                # Cola o texto copiado após a posição do cursor
    P                # Cola o texto copiado antes da posição do cursor
    x                # Deleta o caractere que está na posição do cursor
```

## A 'Gramática' do Vim

Podemos pensar no Vim como uma série de comendos
em um formato 'Verbo-Modificador-Nome', onde:

Verbo       - sua ação 
Modificador - como você executará sua ação
Nome        - o objeto onde você vai executar sua acão

Alguns exemplos importantes de 'Verbos', 'Modificadores' e 'Nomes':

```
    # 'Verbos'
 
    d                # Apagar (Delete)
    c                # Alterar (Change)
    y                # Copiar (Yank)
    v                # Seleção Visual

    # 'Modificadores'

    i                # Dentro (Inside)
    a                # Em torno de (Around)
    NUM              # Número (NUM qualquer número)
    f                # Pesquisa algo e posiciona o cursor acima do resultado
    t                # Pesquisa algo e posiciona o cursor logo antes do resultado
    /                # Encontra algo a frente do cursor
    ?                # Encontra algo antes do cursor

    # 'Nomes'

    w                # Palavra (word)
    s                # Sentência
    p                # Parágrafo
    b                # Bloco
    
    # Exemplos de comandos

    d2w              # Apaga 2 palavras
    cis              # Altera dentro de uma sentência
    yip              # Coloca o parágrafo atual da área de transferência)
    ct<              # Altera para '<'
                     # Altera todo o texto a partir da posição do cursor até o próximo '<'
    d$               # Apaga tudo da posição do cursor até o final da linha
```

## Alguns atalhos e dicas

        <!--TODO: Adicionar mais!-->
```
    >                # Adiciona um bloco de indentação
    <                # Remove um bloco de indentação
    :earlier 15m     # Reverte o documento para como ele estava há 15 minutos atrás
    :later 15m       # Reverte o comando acima
    ddp              # Troca linhas consecutivas de posição, dd e depois p
    .                # Repete a última ação
```

## Macros

Macros, basicamente, são ações graváveis.
Quando você começa a gravar uma macro, ele salva **toda** ação e comando
que você usar, até que você pare de gravar. Ao executar uma macro, ele aplica
exatamente a mesma sequencia de ações e comandos na seleção atual.

```
    qa               # Inicia a gravação de uma macro chamado 'a'
    q                # Para a gravação
    @a               # Executa a macro
```

### Configurando o ~/.vimrc

O arquivo .vimrc pode ser usado para configurar o Vim no seu início.

Exemplo de arquivo ~/.vimrc

```
" Exemplo de ~/.vimrc
" 2015.10 

" Obrigatório para rodar apenas no Vim (Vi Improved)
set nocompatible

" Determina o tipo de arquivo pelo nome para habilitar indentação automática, etc
filetype indent plugin on

" Habilita sintaxe colorida
syntax on

" Ativa um 'auto-completar' melhor para a linha de comando
set wildmenu

" Faz as buscas não diferenciarem maiúsculas-minúsculas (case insensitive)
" Exceto quando você usar letras maiúsculas
set ignorecase
set smartcase

" Quando criar uma nova linha e a indentação por tipo de arquivo estiver
" desabilitada, mantem a mesma indentação da linha atual
set autoindent

" Mostra o número das linhas à esquerda
set number

" Opções de indentação, aqui você pode mudar como preferir

" Número de espaços visíveis por TAB
set tabstop=4

" Número de espaços por TAB ao editar um arquivo
set softtabstop=4

" Número de espaços usados nas funções de indentação (>> e <<)
set shiftwidth=4

" Converte TABs em espaços
set expandtab

" Habilita indentação/alinhamento inteligente
set smarttab
```

### Referências

[Vim | Home](http://www.vim.org/index.php) (EN)

`$ vimtutor pt`

[Vim: um tutorial/cartilha](https://danielmiessler.com/study/vim/) (EN)

[O que são as partes sombrias do Vim que sua mãe nunca te explicou? (tópico no Stack Overflow)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about) (EN)

[Arch Linux Wiki](https://wiki.archlinux.org/index.php/Vim) (EN)
---
language: Visual Basic
contributors:
    - ["Brian Martin", "http://brianmartin.biz"]
translators:
    - ["AdrianoJP", "https://github.com/AdrianoJP"]
lang: pt-br
filename: learnvisualbasic-pt.vb
---

```vb
Module Module1

module Module1

    Sub Main ()
        ' Uma visão geral de console de aplicativos do Visual Basic antes de 
        ' mergulharmos mais profundamente na linguagem
        ' Aspas simples começam comentários.
        ' Para Navegar este tutorial dentro do compilador do Visual Basic, 
        ' eu criei um sistema de navegação.
        ' Este sistema de navegação vai ser explicado conforme avançarmos no
        ' tutorial, e você vai entender o que isso significa.
        Console.Title = (" Saiba X em Y Minutes" )
        Console.WriteLine ( "NAVEGAÇÃO" ) 'Mostrar
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine ("1. Saída Olá Mundo" )
        Console.WriteLine ("2. Entrada Olá Mundo" )
        Console.WriteLine ("3. Cálculando números inteiros " )
        Console.WriteLine ("4. Calculando números decimais " )
        Console.WriteLine ("5 . Calculadora de Trabalho " )
        Console.WriteLine ("6. Usando Do While Loops " )
        Console.WriteLine ("7. Usando Para While Loops " )
        Console.WriteLine ("8 . Declarações condicionais " )
        Console.WriteLine ("9. Selecione uma bebida" )
        Console.WriteLine ("50 . About" )
        Console.WriteLine ("Por favor, escolha um número da lista acima " )
        Seleção Dim As String = Console.ReadLine
        Select A seleção dos casos
            Caso "1" 'Output HelloWorld
                Console.clear () ' Limpa a aplicação e abre o sub privado
                HelloWorldOutput () ' Nome Private Sub, Abre Private Sub
            Caso "2" 'Olá entrada
                Console.clear ( )
                HelloWorldInput ( )
            Caso de "3" 'Calculando Números Inteiros
                Console.clear ( )
                CalculatingWholeNumbers ( )
            Caso "4" ' Números decimais Calculting
                Console.clear ( )
                CalculatingDecimalNumbers ( )
            Caso "5" ' Calcculator Trabalho
                Console.clear ( )
                WorkingCalculator ( )
            Caso "6" 'Usando Do While Loops
                Console.clear ( )
                UsingDoWhileLoops ( )
            Caso de "7" 'Usando pois enquanto Loops
                Console.clear ( )
                UsingForLoops ( )
            Caso "8" ' Instruções condicionais
                Console.clear ( )
                ConditionalStatement ( )
            Caso "9" "Declaração If / Else
                Console.clear ( )
                IfElseStatement () ' Selecione uma bebida
            Caso "50" 'Quem caixa de msg
                Console.clear ( )
                Console.Title = (" Saiba X em Y Minutos :: Quem " )
                MsgBox (" Este tutorial é de Brian Martin ( @ BrianMartinn " )
                Console.clear ( )
                Main ()
                Console.ReadLine ()

        End Select
    End Sub

    ' Um - Eu estou usando números para ajudar com a navegação acima quando eu voltar
    ' depois de construí-lo .

    " Nós usamos subs privadas para separar diferentes seções do programa.
    Private Sub HelloWorldOutput ()
        ' Título de aplicativo do console
        Console.Title = " Olá Mundo Ouput | Saiba X em Y Minutes"
        'Use Console.Write ("") ou Console.WriteLine ("") para imprimir saídas.
        " Seguido por Console.Read () alternativamente Console.ReadLine ()
        ' Console.ReadLine () imprime a saída para o console.
        Console.WriteLine ( "Olá Mundo" )
        Console.ReadLine ()
    End Sub

    ' Dois
    Private Sub HelloWorldInput ()
        Console.Title = " Olá Mundo YourName | Saiba X em Y Minutes"
        ' Variáveis
        'Os dados inseridos por um usuário precisa ser armazenada .
        ' As variáveis ​​também começar com um Dim e terminar com um Como VariableType .

        ' Neste tutorial, nós queremos saber o que o seu nome, e faça o programa
        ' Responder ao que é dito.
        Nome de usuário Dim As String
        " Nós usamos string como string é uma variável de texto baseado .
        Console.WriteLine (" Olá, Qual é o seu nome? ") ' Peça ao usuário seu nome.
        username = Console.ReadLine () ' armazena o nome usuários.
        Console.WriteLine (" Olá " + nome do usuário) " A saída é Olá ' Seu nome '
        Console.ReadLine () ' Outsputs acima.
        ' O código acima irá lhe fazer uma pergunta seguiu imprimindo sua resposta.
        " Outras variáveis ​​incluem Integer e usamos inteiro para números inteiros.
    End Sub

    "Três
    Sub CalculatingWholeNumbers particulares ()
        Console.Title = " Cálculo de Números Inteiros | Saiba X em Y Minutes"
        Console.Write ("Primeiro número:") 'Digite um número inteiro, 1, 2, 50, 104 ect
        Dim a As Integer = Console.ReadLine ()
        Console.Write ("Segundo número:") 'Enter segundo número inteiro.
        Dim b As Integer = Console.ReadLine ()
        Dim c As Integer = a + b
        Console.WriteLine ( c)
        Console.ReadLine ()
        " O texto acima é uma calculadora simples
    End Sub

    'Quatro
    Sub CalculatingDecimalNumbers particulares ()
        Console.Title = " Calculando com duplo | Saiba X em Y Minutes"
        ' Claro que gostaria de ser capaz de somar decimais .
        " Por isso, poderia mudar o acima de Integer para Double.

        " Digite um número inteiro , 1,2 , 2,4 , 50,1 , 104,9 ect
        Console.Write ("Primeiro número:")
        Dim a As Double = Console.ReadLine
        Console.Write ("Segundo número:") 'Enter segundo número inteiro.
        Dim b As Double = Console.ReadLine
        Dim c As Double = a + b
        Console.WriteLine ( c)
        Console.ReadLine ()
        " Portanto, o programa acima pode adicionar até 1,1-2,2
    End Sub

    ' Cinco
    Private Sub WorkingCalculator ()
        Console.Title = " A Calculadora de Trabalho | Saiba X em Y Minutes"
        " No entanto, se você gostaria que a calculadora para subtrair, dividir , múltiplos e
        ' somar.
        ' Copie e cole o código acima novamente .
        Console.Write ("Primeiro número:")
        Dim a As Double = Console.ReadLine
        Console.Write ("Segundo número:") 'Enter segundo número inteiro.
        Dim b As Integer = Console.ReadLine
        Dim c As Integer = a + b
        Dim d As Integer = a * b
        Dim e As Integer = a - b
        Dim f As Integer = a / b

        " Ao adicionar as linhas abaixo , somos capazes de calcular a subtração ,
        ' multply bem como dividir os valores de a e b
        Console.Gravar ( a.ToString ( ) + " + " + b.ToString ( ) )
        'Queremos pad as respostas para a esquerda por três espaços.
        Console.WriteLine (" =" + c.ToString.PadLeft (3) )
        Console.Gravar ( a.ToString ( ) + " * " + b.ToString ( ) )
        Console.WriteLine (" =" + d.ToString.PadLeft (3) )
        Console.Gravar ( a.ToString ( ) + " - " + b.ToString ( ) )
        Console.WriteLine (" =" + e.ToString.PadLeft (3) )
        Console.Write ( a.ToString () + "/" + b.ToString ())
        Console.WriteLine (" =" + e.ToString.PadLeft (3) )
        Console.ReadLine ()

    End Sub

    ' Seis
    Sub UsingDoWhileLoops particulares ()
        ' Assim como o sub privado anterior
        ' Desta vez, perguntar se o usuário deseja continuar ( Sim ou Não ? )
        ' Estamos usando Do While Loop , como não temos certeza se o usuário quer usar o
        'programa mais de uma vez .
        Console.Title = " UsingDoWhileLoops | Saiba X em Y Minutes"
        Dim resposta As String ' Nós usamos a variável " String" como a resposta é um texto
        Do ' Começamos o programa com
            Console.Write ("Primeiro número:")
            Dim a As Double = Console.ReadLine
            Console.Write ("Segundo número:")
            Dim b As Integer = Console.ReadLine
            Dim c As Integer = a + b
            Dim d As Integer = a * b
            Dim e As Integer = a - b
            Dim f As Integer = a / b

            Console.Gravar ( a.ToString ( ) + " + " + b.ToString ( ) )
            Console.WriteLine (" =" + c.ToString.PadLeft (3) )
            Console.Gravar ( a.ToString ( ) + " * " + b.ToString ( ) )
            Console.WriteLine (" =" + d.ToString.PadLeft (3) )
            Console.Gravar ( a.ToString ( ) + " - " + b.ToString ( ) )
            Console.WriteLine (" =" + e.ToString.PadLeft (3) )
            Console.Write ( a.ToString () + "/" + b.ToString ())
            Console.WriteLine (" =" + e.ToString.PadLeft (3) )
            Console.ReadLine ()
            ' Faça a pergunta , se o usuário deseja continuar? Infelizmente,
            "é sensível a maiúsculas.
            Console.Write ( "Deseja continuar? (Sim / não )")
            " O programa pega a variável e imprime e começa de novo.
            answer = Console.ReadLine
        " O comando para a variável para trabalhar seria , neste caso, " sim "
        Loop While resposta = "yes"

    End Sub

    ' Sete
    Sub UsingForLoops particulares ()
        ' Às vezes, o programa só precisa ser executado uma vez.
        " Neste programa vamos estar em contagem regressiva a partir de 10.

        Console.Title = " Usando Para Loops | Saiba X em Y Minutes"
        'Declare variável e qual o número que deve contar para baixo na etapa 1,
        ' Passo -2, -3 Passo ect.
        Para i As Integer = 10 para 0 passo -1
            Console.WriteLine ( i.ToString ) ' Imprime o valor do contador
        Next i ' Calcular novo valor
        Console.WriteLine ( "Start ") ' Vamos começar o bebê programa !
        Console.ReadLine () ' POW ! - Talvez eu fiquei um pouco animado, então :)
    End Sub

    ' Oito
    Private Sub ConditionalStatement ()
        Console.Title = " Instruções condicionais | Saiba X em Y Minutes"
        UserName Dim As String = Console.ReadLine
        Console.WriteLine (" Olá, Qual é o seu nome? ") ' Peça ao usuário seu nome.
        username = Console.ReadLine () ' armazena o nome usuários.
        Se userName = " Adam " Então
            Console.WriteLine (" Olá Adam " )
            Console.WriteLine (" Obrigado por criar este site útil " )
            Console.ReadLine ()
        outro
            Console.WriteLine (" Olá " + nome do usuário)
            Console.WriteLine (" Você check-out www.learnxinyminutes.com " )
            Console.ReadLine () ' Fins e imprime a declaração acima .
        End If
    End Sub

    'Nove
    Private Sub IfElseStatement ()
    Console.Title = "Se Declaração / Else | Saiba X em Y Minutes"
        'Às vezes é importante ter em conta mais de duas alternativas.
        'Às vezes, há um bom número de outros.
        'Quando este for o caso , mais do que uma if seria necessária .
        'Uma instrução if é ótimo para máquinas de venda automática . Quando o usuário digita um código.
        ' A1 , A2, A3 , ect para selecionar um item.
        'Todas as opções podem ser combinadas em uma única if.

        Seleção Dim Valor String = Console.ReadLine ' para a seleção
            Console.WriteLine (" A1. Para Soda " )
            Console.WriteLine (" A2. Para Fanta " )
            Console.WriteLine (" A3 . Para Guaraná" )
            Console.WriteLine (" A4. Para Coca Diet" )
            Console.ReadLine ()
            Se a seleção = "A1" Então
                Console.WriteLine (" soda " )
                Console.ReadLine ()
            Seleção ElseIf = " A2 " Então
                Console.WriteLine (" fanta " )
                Console.ReadLine ()
            Seleção ElseIf = " A3 " Então
                Console.WriteLine ( "guaraná" )
                Console.ReadLine ()
            Seleção ElseIf = " A4 " Então
                Console.WriteLine ( "coca diet" )
                Console.ReadLine ()
            outro
                Console.WriteLine (" Por favor seleccione um produto" )
                Console.ReadLine ()
            End If

    End Sub

End Module

```

##Referências

Aprendi Visual Basic no aplicativo de console. Isso me permitiu entender os princípios da programação de computador para continuar a aprender outras linguagens de programação facilmente.

Eu criei um tutorial mais aprofundado do <a href="http://www.vbbootcamp.co.uk/" Title="Visual Basic Tutorial">Visual Basic</a> para aqueles que gostariam de saber mais.

Toda a sintaxe deste tutorial é válida. Copie e cole o código no compilador do Visual Basic e execute (com o F5) o programa.
---
language: xml
filename: learnxml-pt.xml
contributors:
    - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
    - ["Miguel Araújo", "https://github.com/miguelarauj1o"]
lang: pt-br
---

XML é uma linguagem de marcação projetada para armazenar e transportar dados.

Ao contrário de HTML, XML não especifica como exibir ou formatar os dados, 
basta carregá-lo.

* Sintaxe XML

```xml
<!-- Comentários em XML são feitos desta forma -->

<?xml version="1.0" encoding="UTF-8"?>
<livraria>
	<livro category="COZINHA">
		<titulo lang="en">Everyday Italian</titulo>
		<autor>Giada De Laurentiis</autor>
		<year>2005</year>
		<preco>30.00</preco>
	</livro>
	<livro category="CRIANÇAS">
		<titulo lang="en">Harry Potter</titulo>
		<autor>J K. Rowling</autor>
		<year>2005</year>
		<preco>29.99</preco>
	</livro>
	<livro category="WEB">
		<titulo lang="en">Learning XML</titulo>
		<autor>Erik T. Ray</autor>
		<year>2003</year>
		<preco>39.95</preco>
	</livro>
</livraria>

<!-- Um típico arquivo XML é mostrado acima.
	Ele começa com uma declaração, informando alguns metadados (opcional).
	
	XML usa uma estrutura de árvore. Acima, o nó raiz é "Livraria", que tem
	três nós filhos, todos os 'Livros'. Esses nós tem mais nós filhos, 
	e assim por diante...
	
	Nós são criados usando tags abre/fecha, filhos são justamente os nós que 
	estão entre estes nós. -->


<!-- XML traz dois tipos de dados:
	1 - Atributos -> Isso é metadados sobre um nó.
			Normalmente, o parser XML usa esta informação para armazenar os dados
			corretamente. Caracteriza-se por aparecer em parênteses dentro da tag 
			de abertura.
	2 - Elementos -> É dados puros.
			Isso é o que o analisador irá recuperar a partir do arquivo XML. 
			Elementos aparecem entre as tags de abertura e fechamento, 
			sem parênteses. -->
			
	
<!-- Abaixo, um elemento com dois atributos -->
<arquivo type="gif" id="4293">computer.gif</arquivo>


```

* Documento bem formatado x Validação

Um documento XML é bem formatado se estiver sintaticamente correto.No entanto,
é possível injetar mais restrições no documento, utilizando definições de 
documentos, tais como DTD e XML Schema.

Um documento XML que segue uma definição de documento é chamado válido, sobre 
esse documento.

Com esta ferramenta, você pode verificar os dados XML fora da lógica da aplicação.

```xml

<!-- Abaixo, você pode ver uma versão simplificada do documento livraria,
com a adição de definição DTD.-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "livraria.dtd">
<livraria>
	<livro category="COOKING">
		<titulo >Everyday Italian</titulo>
		<preco>30.00</preco>
	</livro>
</livraria>

<!-- Este DTD poderia ser algo como:-->

<!DOCTYPE note
[
<!ELEMENT livraria (livro+)>
<!ELEMENT livro (titulo,preco)>
<!ATTLIST livro category CDATA "Literature">
<!ELEMENT titulo (#PCDATA)>
<!ELEMENT preco (#PCDATA)>
]>


<!-- O DTD começa com uma declaração.
	Na sequência, o nó raiz é declarado, o que requer uma ou mais crianças nós 
	'Livro'. Cada 'Livro' deve conter exatamente um 'titulo' e um 'preco' e um 
	atributo chamado "categoria", com "Literatura", como o valor padrão.
	Os nós "título" e "preço" contêm um conjunto de dados de caráter analisados.-->

<!-- O DTD poderia ser declarado dentro do próprio arquivo XML .-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT livraria (livro+)>
<!ELEMENT livro (titulo,preco)>
<!ATTLIST livro category CDATA "Literature">
<!ELEMENT titulo (#PCDATA)>
<!ELEMENT preco (#PCDATA)>
]>

<livraria>
	<livro category="COOKING">
		<titulo >Everyday Italian</titulo>
		<preco>30.00</preco>
	</livro>
</livraria>
```
---
language: yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
translators:
  - ["Rodrigo Russo", "https://github.com/rodrigozrusso"]
filename: learnyaml-pt.yaml
lang: pt-br
---

YAML é uma linguagem de serialização de dados projetado para ser diretamente gravável e
legível por seres humanos.

É um estrito subconjunto de JSON, com a adição de sintaticamente
novas linhas e recuo significativos, como Python. Ao contrário de Python, no entanto,
YAML não permite caracteres de tabulação literais em tudo.

```yaml
# Commentários em YAML são como este.

###################
# TIPOS ESCALARES #
###################

# Nosso objeto raiz (que continua por todo o documento) será um mapa,
# o que equivale a um dicionário, hash ou objeto em outras linguagens.
chave: valor
outra_chave: Outro valor vai aqui.
u_valor_numerico: 100
notacao_cientifica: 1e+12
boleano: true
valor_nulo: null
chave com espaco: valor
# Observe que strings não precisam de aspas. Porém, elas podem ter.
porem: "Uma string, entre aspas."
"Chaves podem estar entre aspas tambem.": "É útil se você quiser colocar um ':' na sua chave."

# Seqüências de várias linhas podem ser escritos como um 'bloco literal' (utilizando |),
# ou em um 'bloco compacto' (utilizando '>').
bloco_literal: |
    Todo esse bloco de texto será o valor da chave 'bloco_literal',
    preservando a quebra de com linhas.

    O literal continua até de-dented, e a primeira identação é 
    removida.

        Quaisquer linhas que são 'mais identadas' mantém o resto de suas identações - 
        estas linhas serão identadas com 4 espaços.
estilo_compacto: >
    Todo esse bloco de texto será o valor de 'estilo_compacto', mas esta
    vez, todas as novas linhas serão substituídas com espaço simples.

    Linhas em branco, como acima, são convertidas em um carater de nova linha.

        Linhas 'mais-indentadas' mantém suas novas linhas também -
        este texto irá aparecer em duas linhas.

####################
# TIPOS DE COLEÇÃO #
####################

# Texto aninhado é conseguido através de identação.
um_mapa_aninhado:
    chave: valor
    outra_chave: Outro valor
    outro_mapa_aninhado:
        ola: ola

# Mapas não tem que ter chaves com string.
0.25: uma chave com valor flutuante

# As chaves podem ser também objetos multi linhas, utilizando ? para indicar o começo de uma chave.
? |
    Esta é uma chave
    que tem várias linhas
: e este é o seu valor

# também permite tipos de coleção de chaves, mas muitas linguagens de programação
# vão reclamar.

# Sequências (equivalente a listas ou arrays) semelhante à isso:
uma_sequencia:
    - Item 1
    - Item 2
    - 0.5 # sequencias podem conter tipos diferentes.
    - Item 4
    - chave: valor
      outra_chave: outro_valor
    -
        - Esta é uma sequencia
        - dentro de outra sequencia

# Como YAML é um super conjunto de JSON, você também pode escrever mapas JSON de estilo e
# sequencias:
mapa_json: {"chave": "valor"}
json_seq: [3, 2, 1, "decolar"]

##########################
# RECURSOS EXTRA DO YAML #
##########################

# YAML também tem um recurso útil chamado "âncoras", que permitem que você facilmente duplique
# conteúdo em seu documento. Ambas estas chaves terão o mesmo valor:
conteudo_ancora: & nome_ancora Essa string irá aparecer como o valor de duas chaves.
outra_ancora: * nome_ancora

# YAML também tem tags, que você pode usar para declarar explicitamente os tipos.
string_explicita: !! str 0,5
# Alguns analisadores implementam tags específicas de linguagem, como este para Python de
# Tipo de número complexo.
numero_complexo_em_python: !! python / complex 1 + 2j

####################
# YAML TIPOS EXTRA #
####################

# Strings e números não são os únicos que escalares YAML pode entender.
# Data e 'data e hora' literais no formato ISO também são analisados.
datetime: 2001-12-15T02: 59: 43.1Z
datetime_com_espacos 2001/12/14: 21: 59: 43.10 -5
Data: 2002/12/14

# A tag !!binary indica que a string é na verdade um base64-encoded (condificado)
# representação de um blob binário.
gif_file: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML também tem um tipo de conjunto, o que se parece com isso:
set:
    ? item1
    ? item2
    ? item3

# Como Python, são apenas conjuntos de mapas com valors nulos; o acima é equivalente a:
set2:
    item1: nulo
    item2: nulo
    item3: nulo
```
---
language: brainfuck
filename: brainfuck-pt.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Joao Marques", "http://github.com/mrshankly"]
lang: pt-pt
---

Brainfuck (não capitalizado excepto no início de uma frase) é uma linguagem de
programação Turing-completa extremamente simples com apenas 8 comandos.

```
Qualquer caractere excepto "><+-.,[]" (não contar com as aspas) é ignorado.

Brainfuck é representado por um vector com 30 000 células inicializadas a zero
e um ponteiro de dados que aponta para a célula actual.

Existem 8 comandos:
+ : Incrementa o valor da célula actual em 1.
- : Decrementa o valor da célula actual em 1.
> : Move o ponteiro de dados para a célula seguinte (célula à direita).
< : Move o ponteiro de dados para a célula anterior (célula à esquerda).
. : Imprime o valor ASCII da célula actual. (ex. 65 = 'A').
, : Lê um único caractere para a célula actual.
[ : Se o valor da célula actual for zero, salta para o ] correspondente.
    Caso contrário, passa para a instrução seguinte.
] : Se o valor da célula actual for zero, passa para a instrução seguinte.
    Caso contrário, volta para a instrução relativa ao [ correspondente.

[ e ] formam um ciclo while. Obviamente, devem ser equilibrados.

Vejamos alguns programas básicos de brainfuck.

++++++ [ > ++++++++++ < - ] > +++++ .

Este programa imprime a letra 'A'. Primeiro incrementa a célula #1 para 6.
A célula #1 será usada num ciclo. Depois é iniciado o ciclo ([) e move-se
o ponteiro de dados para a célula #2. Incrementa-se o valor da célula #2 10
vezes, move-se o ponteiro de dados de volta para a célula #1, e decrementa-se
a célula #1. Este ciclo acontece 6 vezes (são necessários 6 decrementos para
a célula #1 chegar a 0, momento em que se salta para o ] correspondente,
continuando com a instrução seguinte).

Nesta altura encontramo-nos na célula #1, cujo valor é 0, enquanto a célula #2
tem o valor 60. Movemos o ponteiro de dados para a célula #2, incrementa-se 5
vezes para um valor final de 65, é então impresso o valor da célula #2. Ao valor
65 corresponde o caractere 'A' em ASCII, 'A' é então impresso para o terminal.

, [ > + < - ] > .

Este programa lê um caractere e copia o seu valor para a célula #1. Um ciclo é
iniciado. Movemos o ponteiro de dados para a célula #2, incrementamos o valor na
célula #2, movemos o ponteiro de dados de volta para a célula #1, finalmente
decrementamos o valor na célula #1. Isto continua até o valor na célula #1 ser
igual a 0 e a célula #2 ter o antigo valor da célula #1. Como o ponteiro de
dados está a apontar para a célula #1 no fim do ciclo, movemos o ponteiro para a
célula #2 e imprimimos o valor em ASCII.

Os espaços servem apenas para tornar o programa mais legível. Podemos escrever
o mesmo programa da seguinte maneira:

,[>+<-]>.

Tenta descobrir o que este programa faz:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Este programa lê dois números e multiplica-os.

Basicamente o programa pede dois caracteres ao utilizador. Depois é iniciado um
ciclo exterior controlado pelo valor da célula #1. Movemos o ponteiro de dados
para a célula #2 e inicia-se o ciclo interior controlado pelo valor da célula
#2, incrementando o valor da célula #3. Contudo, existe um problema, no final do
ciclo interior a célula #2 tem o valor 0. Para resolver este problema o valor da
célula #4 é também incrementado e copiado para a célula #2.
```

Fica então explicado brainfuck. Simples, não? Por divertimento podes escrever os
teus próprios programas em brainfuck, ou então escrever um interpretador de
brainfuck noutra linguagem. O interpretador é relativamente fácil de se
implementar, mas se fores masoquista, tenta escrever um interpretador de
brainfuck… em brainfuck.
---
category: tool
tool: git
lang: pt-pt
filename: LearnGit-pt.txt
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
  - ["Rafael Jegundo", "http://rafaeljegundo.github.io/"]
---

Git é um sistema distribuido de gestão para código fonte e controlo de versões.

Funciona através de uma série de registos de estado do projecto e usa esse
registo para permitir funcionalidades de versionamento e gestão de código
fonte.

## Conceitos de versionamento

### O que é controlo de versões

Controlo de versões (*source control*) é um processo de registo de alterações
a um ficheiro ou conjunto de ficheiros ao longo do tempo.

### Controlo de versões:  Centralizado VS Distribuido

* Controlo de versões centralizado foca-se na sincronização, registo e *backup*
de ficheiros.
* Controlo de versões distribuido foca-se em partilhar alterações. Cada
alteração é associada a um *id* único.
* Sistemas distribuidos não têm estrutura definida. É possivel ter um sistema
centralizado ao estilo SVN usando git.

[Informação adicional (EN)](http://git-scm.com/book/en/Getting-Started-About-Version-Control)

### Porquê usar git?

* Permite trabalhar offline.
* Colaborar com outros é fácil!
* Criar *branches* é fácil!
* Fazer *merge* é fácil!
* Git é rápido.
* Git é flexivel.

## Git - Arquitectura


### Repositório

Um conjunto de ficheiros, directórios, registos históricos, *commits* e
referências. Pode ser imaginado como uma estrutura de dados de código fonte
com a particularidade de cada elemento do código fonte permitir acesso ao
histórico das suas alterações, entre outras coisas.

Um repositório git é constituido pelo directório .git e a *working tree*

### Directório .git (componente do repositório)

O repositório .git contém todas as configurações, *logs*, *branches*,
referências e outros.

[Lista detalhada (EN)](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### *Working Tree* (componente do repositório)

Isto é basicamente os directórios e ficheiros do repositório. É frequentemente
referido como o directório do projecto.

### *Index* (componente do directório .git)

O *Index* é a camada de interface no git. Consistente num elemento que separa
o directório do projecto do repositório git. Isto permite aos programadores um
maior controlo sobre o que é registado no repositório git.

### *Commit*

Um *commit** de git é um registo de um cojunto de alterações ou manipulações nos ficheiros do projecto.
Por exemplo, ao adicionar cinco ficheiros e remover outros 2, estas alterações
serão gravadas num *commit* (ou registo). Este *commit* pode então ser enviado
para outros repositórios ou não!

### *Branch*

Um *branch* é essencialmente uma referência que aponta para o último *commit*
efetuado. À medida que são feitos novos commits, esta referência é atualizada
automaticamente e passa a apontar para o commit mais recente.

### *HEAD* e *head* (componentes do directório .git)

*HEAD* é a referência que aponta para o *branch* em uso. Um repositório só tem
uma *HEAD* activa.
*head* é uma referência que aponta para qualquer *commit*. Um repositório pode
ter um número indefinido de *heads*

### Recursos conceptuais (EN)

* [Git para Cientistas de Computação](http://eagain.net/articles/git-for-computer-scientists/)
* [Git para Designers](http://hoth.entp.com/output/git_for_designers.html)

## Comandos

### *init*

Cria um repositório Git vazio. As definições, informação guardada e outros do
repositório git são guardados num directório (pasta) denominado ".git".

```bash
$ git init
```

### *config*

Permite configurar as definições, sejam as definições do repositório, sistema
ou configurações globais.

```bash
# Imprime e define algumas variáveis de configuração básicas (global)
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "MyEmail@Zoho.com"
$ git config --global user.name "My Name"
```

[Aprenda mais sobre git config. (EN)](http://git-scm.com/docs/git-config)

### help

Para aceder rapidamente a um guia extremamente detalhada sobre cada comando.
Ou para dar apenas uma lembraça rápida de alguma semântica.

```bash
# Ver rapidamente os comandos disponiveis
$ git help

# Ver todos os comandos disponiveis
$ git help -a

# Requerer *help* sobre um comando especifico - manual de utilizador
# git help <command_here>
$ git help add
$ git help commit
$ git help init
```

### status

Apresenta as diferenças entre o ficheiro *index* (no fundo a versão corrente
do repositório) e o *commit* da *HEAD* atual.


```bash
# Apresenta o *branch*, ficheiros não monitorizados, alterações e outras
# difereças
$ git status

# Para aprender mais detalhes sobre git *status*
$ git help status
```

### add

Adiciona ficheiros ao repositório corrente. Se os ficheiros novos não forem
adicionados através de `git add` ao repositório, então eles não serão
incluidos nos commits!

```bash
# adiciona um ficheiro no directório do projecto atual
$ git add HelloWorld.java

# adiciona um ficheiro num sub-directório
$ git add /path/to/file/HelloWorld.c

# permite usar expressões regulares!
$ git add ./*.java
```

### branch

Gere os *branches*. É possível ver, editar, criar e apagar branches com este
comando.

```bash
# listar *branches* existentes e remotos
$ git branch -a

# criar um novo *branch*
$ git branch myNewBranch

# apagar um *branch*
$ git branch -d myBranch

# alterar o nome de um *branch*
# git branch -m <oldname> <newname>
$ git branch -m myBranchName myNewBranchName

# editar a descrição de um *branch*
$ git branch myBranchName --edit-description
```

### checkout

Atualiza todos os ficheiros no directório do projecto de forma a ficarem iguais
à versão do index ou do *branch* especificado.

```bash
# Checkout de um repositório - por predefinição para o branch master
$ git checkout
# Checkout de um branch especifico
$ git checkout branchName
# Cria um novo branch e faz checkout para ele.
# Equivalente a: "git branch <name>; git checkout <name>"
$ git checkout -b newBranch
```

### clone

Clona ou copia um repositório existente para um novo directório. Também
adiciona *branches* de monitorização remota para cada *branch* no repositório
clonado o que permite enviar alterações para um *branch* remoto.

```bash
# Clona learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git
```

### commit

Guarda o conteudo atual do index num novo *commit*. Este *commit* contém
as alterações feitas e a mensagem criada pelo utilizador.

```bash
# commit com uma mensagem
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"
```

### diff

Apresenta as diferenças entre um ficheiro no repositório do projecto, *index*
e *commits*

```bash
# Apresenta a diferença entre o directório atual e o index
$ git diff

# Apresenta a diferença entre o index e os commits mais recentes
$ git diff --cached

# Apresenta a diferença entre o directório atual e o commit mais recente
$ git diff HEAD
```

### grep

Permite procurar facilmente num repositório

Configurações opcionais:

```bash
# Obrigado a Travis Jeffery por estas
# Define a apresentação de números de linha nos resultados do grep
$ git config --global grep.lineNumber true

# Torna os resultados da pesquisa mais fáceis de ler, agrupando-os
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Pesquisa por "variableName" em todos os ficheiros de java
$ git grep 'variableName' -- '*.java'

# Pesquisa por uma linha que contém "arrayListName" e "add" ou "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
```

Google é teu amigo; para mais exemplos:
[Git Grep Ninja (EN)](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Apresenta commits do repositório.

```bash
# Apresenta todos os commits
$ git log

# Apresenta X commits
$ git log -n 10

# Apresenta apenas commits de merge
$ git log --merges
```

### merge

"Merge" (junta) as alterações de commits externos com o *branch* atual.

```bash
# Junta o branch especificado com o atual
$ git merge branchName

# Para gerar sempre um commit ao juntar os branches
$ git merge --no-ff branchName
```

### mv

Alterar o nome ou mover um ficheiro.

```bash
# Alterar o nome de um ficheiro
$ git mv HelloWorld.c HelloNewWorld.c

# Mover um ficheiro
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# Forçar a alteração de nome ou mudança local
# "existingFile" já existe no directório, será sobre-escrito.
$ git mv -f myFile existingFile
```

### pull

Puxa alterações de um repositório e junta-as com outro branch

```bash
# Atualiza o repositório local, juntando as novas alterações
# do repositório remoto 'origin' e branch 'master'
# git pull <remote> <branch>
# git pull => aplica a predefinição => git pull origin master
$ git pull origin master

# Juntar alterações do branch remote e fazer rebase commits do branch
# no repositório local, como: "git pull <remote> <branch>, git rebase <branch>"
$ git pull origin master --rebase
```

### push

Enviar e juntar alterações de um branch para o seu branch correspondente
num repositório remoto.

```bash
# Envia e junta as alterações de um repositório local
# para um remoto denominado "origin" no branch "master".
# git push <remote> <branch>
# git push => aplica a predefinição => git push origin master
$ git push origin master
```

### rebase (cautela!)

Pega em todas as alterações que foram registadas num branch e volta a
aplicá-las em outro branch.
*Não deve ser feito rebase de commits que foram enviados para um repositório
público*

```bash
# Faz Rebase de experimentBranch para master
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch
```

[Additional Reading (EN).](http://git-scm.com/book/en/Git-Branching-Rebasing)

### reset (cautela!)

Restabelece a HEAD atual ao estado definido. Isto permite reverter *merges*,
*pulls*, *commits*, *adds* e outros. É um comando muito poderoso mas também
perigoso se não há certeza quanto ao que se está a fazer.

```bash
# Restabelece a camada intermediária de registo para o último
# commit (o directório fica sem alterações)
$ git reset

# Restabelece a camada intermediária de registo para o último commit, e
# sobre-escreve o projecto atual
$ git reset --hard

# Move a head do branch atual para o commit especificado, sem alterar o projecto.
# todas as alterações ainda existem no projecto
$ git reset 31f2bb1

# Inverte a head do branch atual para o commit especificado
# fazendo com que este esteja em sintonia com o directório do projecto
# Remove alterações não registadas e todos os commits após o commit especificado
$ git reset --hard 31f2bb1
```

### rm

O oposto de git add, git rm remove ficheiros do branch atual.

```bash
# remove HelloWorld.c
$ git rm HelloWorld.c

# Remove um ficheiro de um sub-directório
$ git rm /pather/to/the/file/HelloWorld.c
```

## Informação complementar (EN)

* [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1)

* [git-scm - Video Tutorials](http://git-scm.com/videos)

* [git-scm - Documentation](http://git-scm.com/docs)

* [Atlassian Git - Tutorials & Workflows](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)
---
language: Scala
filename: learnscala-pt.scala
contributors:
    - ["George Petrov", "http://github.com/petrovg"]
    - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Ha-Duong Nguyen", "http://reference-error.org"]
translators:
    - ["João Costa", "http://joaocosta.eu"]
lang: pt-pt
---

Scala - a linguagem escalável

```scala

/*
  Prepare tudo:

  1) Faça Download do Scala - http://www.scala-lang.org/downloads
  2) Faça unzip/untar para onde preferir e coloque o subdirectório `bin` na
     variável de ambiente `PATH`
  3) Inicie a REPL de Scala correndo o comando `scala`. Deve aparecer:

  scala>

  Isto é chamado de REPL (Read-Eval-Print Loop / Lê-Avalia-Imprime Repete).
  Pode escrever qualquer expressão de Scala e o resultado será imprimido.
  Vamos mostrar ficheiros de Scala mais à frente neste tutorial mas, para já,
  vamos começar com os básicos.

*/


/////////////////////////////////////////////////
// 1. Basicos
/////////////////////////////////////////////////

// Uma linha de comentários é marcada com duas barras

/*
  Comentários de multiplas linhas, como se pode ver neste exemplo, são assim.
*/

// Imprimir, forçando uma nova linha no final
println("Hello world!")
println(10)

// Imprimir, sem forçar uma nova linha no final
print("Hello world")

// Valores são declarados com var ou val.
// As declarações val são imutáveis, enquanto que vars são mutáveis.
// A immutabilidade é uma propriedade geralmente vantajosa.
val x = 10 // x é agora 10
x = 20     // erro: reatribuição de um val
var y = 10
y = 20     // y é agora 12

/*
  Scala é uma linguagem estaticamente tipada, no entanto, nas declarações acima
  não especificamos um tipo. Isto é devido a uma funcionalidade chamada
  inferência de tipos. Na maior parte dos casos, o compilador de scala consegue
  inferir qual o tipo de uma variável, pelo que não o temos de o declarar sempre.
  Podemos declarar o tipo de uma variável da seguinte forma:
*/
val z: Int = 10
val a: Double = 1.0

// Note a conversão automática de Int para Double: o resultado é 10.0, não 10
val b: Double = 10

// Valores booleanos
true
false

// Operações booleanas
!true         // false
!false        // true
true == false // false
10 > 5        // true

// A matemática funciona da maneira habitual
1 + 1   // 2
2 - 1   // 1
5 * 3   // 15
6 / 2   // 3
6 / 4   // 1
6.0 / 4 // 1.5


// Avaliar expressões na REPL dá o tipo e valor do resultado

1 + 7

/* A linha acima resulta em:

  scala> 1 + 7
  res29: Int = 8

  Isto significa que o resultado de avaliar 1 + 7 é um objecto do tipo Int com
  o valor 8.

  Note que "res29" é um nome de uma variavel gerado sequencialmente para
  armazenar os resultados das expressões que escreveu, por isso o resultado
  pode ser ligeiramente diferente.
*/

"Strings em scala são rodeadas por aspas"
'a' // Um caracter de Scala
// 'Strings entre plicas não existem' <= Isto causa um erro

// Strings tem os métodos de Java habituais definidos
"olá mundo".length
"olá mundo".substring(2, 6)
"olá mundo".replace("á", "é")

// Para além disso, também possuem métodos de Scala.
// Ver: scala.collection.immutable.StringOps
"olá mundo".take(5)
"olá mundo".drop(5)

// Interpolação de Strings: repare no prefixo "s"
val n = 45
s"Temos $n maçãs" // => "Temos 45 maçãs"

// Expressões dentro de Strings interpoladas também são possíveis
val a = Array(11, 9, 6)
s"A minha segunda filha tem ${a(0) - a(2)} anos." // => "A minha segunda filha tem 5 anos."
s"Temos o dobro de ${n / 2.0} em maçãs."          // => "Temos o dobro de 22.5 em maçãs."
s"Potência de 2: ${math.pow(2, 2)}"               // => "Potência de 2: 4"

// Strings interpoladas são formatadas com o prefixo "f"
f"Potência de 5: ${math.pow(5, 2)}%1.0f"     // "Potência de 5: 25"
f"Raíz quadrada 122: ${math.sqrt(122)}%1.4f" // "Raíz quadrada de 122: 11.0454"

// Strings prefixadas com "raw" ignoram caracteres especiais
raw"Nova linha: \n. Retorno: \r." // => "Nova Linha: \n. Retorno: \r."

// Alguns caracteres tem de ser "escapados", e.g. uma aspa dentro de uma string:
"Esperaram fora do  \"Rose and Crown\"" // => "Esperaram fora do "Rose and Crown""

// Strings rodeadas por três aspas podem-se estender por varias linhas e conter aspas
val html = """<form id="daform">
                <p>Carrega aqui, Zé</p>
                <input type="submit">
              </form>"""


/////////////////////////////////////////////////
// 2. Funções
/////////////////////////////////////////////////

// Funções são definidas como:
//
//   def nomeDaFuncao(args...): TipoDeRetorno = { corpo... }
//
// Se vem de linugagens mais tradicionais, repare na omissão da palavra
// return keyword. Em Scala, a ultima expressão de um bloco é o seu
// valor de retorno
def somaQuadrados(x: Int, y: Int): Int = {
  val x2 = x * x
  val y2 = y * y
  x2 + y2
}

// As { } podem ser omitidas se o corpo da função for apenas uma expressão:
def somaQuadradosCurto(x: Int, y: Int): Int = x * x + y * y

// A sintaxe para chamar funções deve ser familiar:
somaQuadrados(3, 4)  // => 25

// Na maior parte dos casos (sendo funções recursivas a principal excepção), o
// tipo de retorno da função pode ser omitido, sendo que a inferencia de tipos
// é aplicada aos valores de retorno
def quadrado(x: Int) = x * x  // O compilador infere o tipo de retorno Int

// Funções podem ter parâmetros por omissão:
def somaComOmissão(x: Int, y: Int = 5) = x + y
somaComOmissão(1, 2) // => 3
somaComOmissão(1)    // => 6


// Funções anónimas são definidas da seguinte forma:
(x: Int) => x * x

// Ao contrário de defs, o tipo de input de funções anónimas pode ser omitido
// se o contexto o tornar óbvio. Note que o tipo "Int => Int" representa uma
// funão que recebe Int e retorna Int.
val quadrado: Int => Int = x => x * x

// Funcões anónimas são chamadas como funções normais:
quadrado(10)   // => 100

// Se cada argumento de uma função anónima for usado apenas uma vez, existe
// uma forma ainda mais curta de os definir. Estas funções anónumas são
// extremamente comuns, como será visto na secção sobre estruturas de dados.
val somaUm: Int => Int = _ + 1
val somaEstranha: (Int, Int) => Int = (_ * 2 + _ * 3)

somaUm(5)          // => 6
somaEstranha(2, 4) // => 16


// O código return existe em Scala, mas apenas retorna do def mais interior
// que o rodeia.
// AVISO: Usar return em Scala deve ser evitado, pois facilmente leva a erros.
// Não tem qualquer efeito em funções anónimas, por exemplo:
def foo(x: Int): Int = {
  val funcAnon: Int => Int = { z =>
    if (z > 5)
      return z // Esta linha faz com que z seja o retorno de foo!
    else
      z + 2    // Esta linha define o retorno de funcAnon
  }
  funcAnon(x)  // Esta linha define o valor de retorno de foo
}


/////////////////////////////////////////////////
// 3. Controlo de fluxo
/////////////////////////////////////////////////

1 to 5
val r = 1 to 5
r.foreach(println)

r foreach println
// NB: Scala é bastante brando no que toca a pontos e parentisis - estude as
// regras separadamente. Isto permite escrever APIs e DSLs bastante legiveis

(5 to 1 by -1) foreach (println)

// Ciclos while
var i = 0
while (i < 10) { println("i " + i); i += 1 }

while (i < 10) { println("i " + i); i += 1 }   // Sim, outra vez. O que aconteceu? Porquê?

i    // Mostra o valor de i. Note que o while é um ciclo no sentido clássico -
     // executa sequencialmente enquanto muda uma variável. Ciclos while são
     // rápidos, por vezes até mais que ciclos de Java, mas combinadores e
     // compreensões (usados anteriormente) são mais fáceis de entender e
     // paralelizar

// Um ciclo do while
i = 0
do {
  println("i ainda é menor que 10")
  i += 1
} while (i < 10)

// A forma idiomática em Scala de definir acções recorrentes é através de
// recursão em cauda.
// Funções recursivas necessitam de um tipo de retorno definido explicitamente.
// Neste caso, é Unit.
def mostraNumerosEntre(a: Int, b: Int): Unit = {
  print(a)
  if (a < b)
    mostraNumerosEntre(a + 1, b)
}
mostraNumerosEntre(1, 14)


// Condicionais

val x = 10

if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println ("yeah") else println("nay")

println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"


/////////////////////////////////////////////////
// 4. Estruturas de dados
/////////////////////////////////////////////////

val a = Array(1, 2, 3, 5, 8, 13)
a(0)
a(3)
a(21)    // Lança uma excepção

val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
m("fork")
m("spoon")
m("bottle")       // Lança uma excepção

val safeM = m.withDefaultValue("no lo se")
safeM("bottle")

val s = Set(1, 3, 7)
s(0)
s(1)

/* Veja a documentação de mapas de scala em -
 * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
 * e verifique que a consegue aceder
 */


// Tuplos

(1, 2)

(4, 3, 2)

(1, 2, "três")

(a, 2, "três")

// Porquê ter isto?
val divideInts = (x: Int, y: Int) => (x / y, x % y)

divideInts(10, 3) // A função divideInts returna o resultado e o resto

// Para aceder aos elementos de um tuplo, pode-se usar _._n, onde n é o indice
// (começado em 1) do elemento
val d = divideInts(10, 3)

d._1

d._2


/////////////////////////////////////////////////
// 5. Programação Orientada a Objectos
/////////////////////////////////////////////////

/*
  Aparte: Até agora tudo o que fizemos neste tutorial foram expressões simples
  (valores, funções, etc). Estas expressões são suficientes para executar no
  interpretador da linha de comandos para testes rápidos, mas não podem existir
  isoladas num ficheiro de Scala. Por exemplo, não é possivel correr um
  ficheiro scala que apenas contenha "val x = 5". Em vez disso, as únicas
  construções de topo permitidas são:

  - object
  - class
  - case class
  - trait

  Vamos agora explicar o que são:
*/

// Classes são semelhantes a classes noutras linguagens. Os argumentos do
// construtor são declarados após o nome da classe, sendo a inicialização feita
// no corpo da classe.
class Cão(rc: String) {
  // Código de construção
  var raça: String = rc

  // Define um método chamado "ladra", que retorna uma String
  def ladra = "Woof, woof!"

  // Valores e métodos são assumidos como públicos, mas é possivel usar
  // os códigos "protected" and "private".
  private def dormir(horas: Int) =
    println(s"Vou dormir por $horas horas")

  // Métodos abstractos são métodos sem corpo. Se descomentarmos a próxima
  // linha, a classe Cão é declarada como abstracta
  //   abstract class Cão(...) { ... }
  // def persegue(oQue: String): String
}

val oMeuCão = new Cão("greyhound")
println(oMeuCão.raça)  // => "greyhound"
println(oMeuCão.ladra) // => "Woof, woof!"


// O termo "object" cria um tipo e uma instancia singleton desse tipo. É comum
// que classes de Scala possuam um "objecto companheiro", onde o comportamento
// por instância é capturado nas classes, equanto que o comportamento
// relacionado com todas as instancias dessa classe ficam no objecto.
// A diferença é semelhante a métodos de classes e métodos estáticos noutras
// linguagens. Note que objectos e classes podem ter o mesmo nome.
object Cão {
  def raçasConhecidas = List("pitbull", "shepherd", "retriever")
  def criarCão(raça: String) = new Cão(raça)
}


// Case classes são classes com funcionalidades extra incluidas. Uma questão
// comum de iniciantes de scala é quando devem usar classes e quando devem usar
// case classes. A linha é difusa mas, em geral, classes tendem a concentrar-se
// em encapsulamento, polimorfismo e comportamento. Os valores nestas classes
// tendem a ser privados, sendo apenas exposotos métodos. O propósito principal
// das case classes é armazenarem dados imutáveis. Geralmente possuem poucos
// métods, sendo que estes raramente possuem efeitos secundários.
case class Pessoa(nome: String, telefone: String)

// Cria uma nova instancia. De notar que case classes não precisam de "new"
val jorge = Pessoa("Jorge", "1234")
val cátia = Pessoa("Cátia", "4567")

// Case classes trazem algumas vantagens de borla, como acessores:
jorge.telefone  // => "1234"

// Igualdade por campo (não é preciso fazer override do .equals)
Pessoa("Jorge", "1234") == Pessoa("Cátia", "1236")  // => false

// Cópia simples
// outroJorge == Person("jorge", "9876")
val outroJorge = jorge.copy(telefone = "9876")

// Entre outras. Case classes também suportam correspondência de padrões de
// borla, como pode ser visto de seguida.


// Traits em breve!


/////////////////////////////////////////////////
// 6. Correspondência de Padrões
/////////////////////////////////////////////////

// A correspondência de padrões é uma funcionalidade poderosa e bastante
// utilizada em Scala. Eis como fazer correspondência de padrões numa case class:
// Nota: Ao contrário de outras linguagens, cases em scala não necessitam de
// breaks, a computação termina no primeiro sucesso.

def reconhecePessoa(pessoa: Pessoa): String = pessoa match {
  // Agora, especifique os padrões:
  case Pessoa("Jorge", tel) => "Encontramos o Jorge! O seu número é " + tel
  case Pessoa("Cátia", tel) => "Encontramos a Cátia! O seu número é " + tel
  case Pessoa(nome, tel)    => "Econtramos alguém : " + nome + ", telefone : " + tel
}

val email = "(.*)@(.*)".r  // Define uma regex para o próximo exemplo.

// A correspondência de padrões pode parecer familiar aos switches em linguagens
// derivadas de C, mas é muto mais poderoso. Em Scala, é possível fazer
// correspondências com muito mais:
def correspondeTudo(obj: Any): String = obj match {
  // Pode-se corresponder valores:
  case "Olá mundo" => "Recebi uma string Olá mundo."

  // Corresponder por tipo:
  case x: Double => "Recebi um Double: " + x

  // Corresponder tendo em conta condições especificas:
  case x: Int if x > 10000 => "Recebi um número bem grande!"

  // Fazer correspondências com case classes (visto anteriormente):
  case Pessoa(nome, tel) => s"Recebi o contacto para $nome!"

  // Fazer correspondência com expressões regulares:
  case email(nome, dominio) => s"Recebi o endereço de email $nome@$dominio"

  // Corresponder tuplos:
  case (a: Int, b: Double, c: String) => s"Recebi o tuplo: $a, $b, $c"

  // Corresponder estruturas de dados:
  case List(1, b, c) => s"Recebi uma lista de 3 elementos começada em 1: 1, $b, $c"

  // Combinar padrões:
  case List(List((1, 2, "YAY"))) => "Recebi uma lista de lista de triplo"
}

// Na realidade, é possível fazer correspondência com qualquer objecto que
// defina o método "unapply". Esta funcionalidade é tão poderosa que permite
// definir funções sob a forma de padrões:
val funcPaddrao: Pessoa => String = {
  case Pessoa("Jorge", tel) => s"Número do Jorge: $tel"
  case Pessoa(nome, tel)    => s"Número de alguém: $tel"
}


/////////////////////////////////////////////////
// 7. Programação Funcional
/////////////////////////////////////////////////

// Scala permite que funções e métodos retornem, ou recebam como parámetros,
// outras funções ou métodos

val soma10: Int => Int = _ + 10 // Função que recebe um Int e retorna um Int
List(1, 2, 3) map soma10 // List(11, 12, 13) - soma10 é aplicado a cada elemento

// Funções anónimas também podem ser usadas
List(1, 2, 3) map (x => x + 10)

// Sendo que o símbolo _ também pode ser usado se a função anónima só receber
// um argumento. Este fica com o valor da variável
List(1, 2, 3) map (_ + 10)

// Se tanto o bloco como a função apenas receberem um argumento, o próprio
// _ pode ser omitido
List("Dom", "Bob", "Natalia") foreach println


// Combinadores

s.map(quadrado)

val sQuadrado = s.map(quadrado)

sQuadrado.filter(_ < 10)

sQuadrado.reduce (_+_)

// O método filter recebe um predicado (uma função de A => Boolean) e escolhe
// todos os elementos que satisfazem o predicado
List(1, 2, 3) filter (_ > 2) // List(3)
case class Pessoa(nome: String, idade: Int)
List(
  Pessoa(nome = "Dom", idade = 23),
  Pessoa(nome = "Bob", idade = 30)
).filter(_.idade > 25) // List(Pessoa("Bob", 30))


// O método foreach recebe uma função de A => Unit, executando essa função em
// cada elemento da colecção
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println

// Compreensões For

for { n <- s } yield quadrado(n)

val nQuadrado2 = for { n <- s } yield quadrado(n)

for { n <- nQuadrado2 if n < 10 } yield n

for { n <- s; nQuadrado = n * n if nQuadrado < 10} yield nQuadrado

/* Nota: isto não são ciclos for: A semântica de um ciclo é 'repetir', enquanto
   que uma compreensão define a relação entre dois conjuntos de dados. */


/////////////////////////////////////////////////
// 8. Implicitos
/////////////////////////////////////////////////

/* AVISO IMPORTANTE: Implicitos são um conjunto de funcionalidades muito
 * poderosas em Scala, que podem ser fácilmente abusadas. Iniciantes devem
 * resistir a tentação de usá-los até que compreendam não só como funcionam,
 * mas também as melhores práticas. Apenas incluimos esta secção no tutorial
 * devido a estes serem tão comuns em bibliotecas de Scala que muitas delas
 * se tornam impossíveis de usar sem conhecer implicitos. Este capítulo serve
 * para compreender como trabalhar com implicitos, não como declará-los.
*/

// Qualquer valor (vals, funções, objectos, etc) pode ser declarado como
// implicito usando a palavra "implicit". Vamos usar a classe Cão da secção 5
// nestes exemplos

implicit val oMeuIntImplicito = 100
implicit def aMinhaFunçãoImplicita(raça: String) = new Cão("Golden " + raça)

// Por si só, a palavra implicit não altera o comportamento de um valor, sendo
// que estes podem ser usados da forma habitual.
oMeuIntImplicito + 2                   // => 102
aMinhaFunçãoImplicita("Pitbull").raça // => "Golden Pitbull"

// A diferença é que estes valores podem ser utilizados quando outro pedaço de
// código "necessite" de uma valor implicito. Um exemplo são argumentos
// implicitos de funções:
def enviaCumprimentos(aQuem: String)(implicit quantos: Int) =
  s"Olá $aQuem, $quantos cumprimentos para ti e para os teus!"

// Se dermos um valor a "quantos", a função comporta-se normalmente
enviaCumprimentos("João")(1000)  // => "Olá João, 1000 cumprimentos para ti e para os teus!"

// Mas, se omitirmos o parâmetro implicito, um valor implicito do mesmo tipo é
// usado, neste caso, "oMeuInteiroImplicito"
enviaCumprimentos("Joana")  // => "Olá Joana, 100 cumprimentos para ti e para os teus!"

// Parâmentros implicitos de funções permitem-nos simular classes de tipos de
// outras linguagens funcionais. Isto é tão comum que tem a sua própria notação.
// As seguintes linhas representam a mesma coisa
// def foo[T](implicit c: C[T]) = ...
// def foo[T : C] = ...


// Outra situação em que o compilador prouca um implicito é se encontrar uma
// expressão
//    obj.método(...)
// mas "obj" não possuir um método chamado "método". Neste cso, se houver uma
// conversão implicita A => B, onde A é o tipo de obj, e B possui um método
// chamado "método", a conversão é aplicada. Ou seja, tendo
// aMinhaFunçãoImplicita definida, podemos dizer
"Retriever".raça // => "Golden Retriever"
"Sheperd".ladra  // => "Woof, woof!"

// Neste caso, a String é primeiro convertida para Cão usando a nossa funão,
// sendo depois chamado o método apropriado. Esta é uma funcionalidade
// incrivelmente poderosa, sendo que deve ser usada com cautela. Na verdade,
// ao definir a função implicita, o compilador deve lançar um aviso a insisitir
// que só deve definir a função se souber o que está a fazer.


/////////////////////////////////////////////////
// 9. Misc
/////////////////////////////////////////////////

// Importar coisas
import scala.collection.immutable.List

// Importar todos os "sub pacotes"
import scala.collection.immutable._

// Importar multiplas classes numa linha
import scala.collection.immutable.{List, Map}

// Renomear uma classe importada usando '=>'
import scala.collection.immutable.{List => ImmutableList}

// Importar todas as classes excepto algumas. Set e Map são excluidos:
import scala.collection.immutable.{Map => _, Set => _, _}

// O ponto de entrada de um programa em Scala é definido por un ficheiro .scala
// com um método main:
object Aplicação {
  def main(args: Array[String]): Unit = {
    // código aqui.
  }
}

// Ficheiros podem conter várias classes o objectos. Compilar com scalac




// Input e output

// Ler um ficheiro linha a linha
import scala.io.Source
for(linha <- Source.fromFile("ficheiro.txt").getLines())
  println(linha)

// Escrever um ficheiro usando o PrintWriter de Java
val writer = new PrintWriter("ficheiro.txt")
writer.write("Escrevendo linha por linha" + util.Properties.lineSeparator)
writer.write("Outra linha aqui" + util.Properties.lineSeparator)
writer.close()

```

## Mais recursos

* [Scala for the impatient](http://horstmann.com/scala/)
* [Twitter Scala school](http://twitter.github.io/scala_school/)
* [The scala documentation](http://docs.scala-lang.org/)
* [Try Scala in your browser](http://scalatutorials.com/tour/)
* Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user)
---
language: swift
filename: learnswift-pt.swift
contributors:
  - ["Grant Timmerman", "http://github.com/grant"]
  - ["Christopher Bess", "http://github.com/cbess"]
  - ["Joey Huang", "http://github.com/kamidox"]
  - ["Anthony Nguyen", "http://github.com/anthonyn60"]
  - ["Clayton Walker", "https://github.com/cwalk"]
translators:
  - ["João Costa", "https://github.com/joaofcosta"]
lang: pt-pt
---

Swift é uma linguagem de programação criada pela Apple para o desenvolvimento em iOS e OS X.
Desenhada de forma a coexistir com Objective-C e ser mais resiliente contra código errôneo, a linguagem Swift foi introduzida em 2014 na conferência para desenvolvedores WWDC da Apple.
Swift usa o compilador LLVM incluido no XCode 6+.

O livro oficial [Swift Programming Language](https://itunes.apple.com/us/book/swift-programming-language/id881256329) da Apple está agora disponivel via iBooks.

Consulta também o [guia de iniciação](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/) da Apple, que contêm um tutorial completo em Swift.

```swift
// importar um módulo
import UIKit

//
// MARK: Básico
//

// O Xcode suporta landmarks para anotação de código e lista-as na jump bar
// MARK: Marco de secção (MARK)
// TODO: Algo a fazer em breve
// FIXME: Reparar este código

// Em Swift 2, println e print foram unidos num só método print. O print automaticamente acrescenta uma nova linha.
print("Hello, world") // println mudou para print
print("Hello, world", appendNewLine: false) // imprimir sem acrescentar uma nova linha

// variáveis (var) podem ser modificadas depois de inicializadas
// constantes (let) NÂO podem ser modificadas depois de inicializadas

var myVariable = 42
let øπΩ = "value" // nomes de variáveis em unicode
let π = 3.1415926
let convenience = "keyword" // nome de variável contextual
let weak = "keyword"; let override = "another keyword" // expressões podem ser separadas com ';'
let `class` = "keyword" // plicals permitem que keywords sejam usadas como nomes de vartiáveis
let explicitDouble: Double = 70
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "some text " + String(myVariable) // Casting
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // interpolação de Strings

// Valores especificos à build
// usam a configuração de build -D
#if false
    print("Not printed")
    let buildValue = 3
#else
    let buildValue = 7
#endif
print("Build value: \(buildValue)") // Build value: 7

/*
    Optionals são um dos recursos de Swift, Optionals tanto podem conter
    um valor ou conter nil (sem valor) que indica que não existe um valor.
    Adicionar um ponto de exclamção (?) após definir o tipo declara
    esse valor como um Optional.

    Como Swift requere que todas as propriedades tenham um valor, até nil
    tem que ser explicitamente guardado como um valor Optional.

    Optional<T> é uma enumeração.
*/
var someOptionalString: String? = "optional" // Pode assumir o valor nil
// Igual ao de cima, mas ? é um operando pósfixo (açúcar sintático)
var someOptionalString2: Optional<String> = "optional"

if someOptionalString != nil {
    // Não sou nil
    if someOptionalString!.hasPrefix("opt") {
        print("has the prefix")
    }

    let empty = someOptionalString?.isEmpty
}
someOptionalString = nil

/*
    Tentar usar ! para aceder a Optional com valor não existente, ou seja, nil,
    causa em erro de execução.
    É necessário ter sempre a certeza que um Optional não tem valor nil
    antes de usar ! para fazer 'force-unwrap' ao seu valor.
*/

// Optional implicitamente desembrulhado
var unwrappedString: String! = "Value is expected."
// O mesmo de cima, mas ! é um operando pósfixo (mais açúcar sintático)
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Value is expected."

if let someOptionalStringConstant = someOptionalString {
    // Tem um valor diferente de nil
    if !someOptionalStringConstant.hasPrefix("ok") {
        // Não tem o prefixo
    }
}

// Swift tem suporte para guardar valores de qualquer tipo.
// AnyObject == id
// Ao contrátio do `id` de Objective-C, AnyObject funciona com qualquer valor (Class, Int, struct, etc.)
var anyObjectVar: AnyObject = 7
anyObjectVar = "Changed value to a string, not good practice, but possible."

/*
    Comentar aqui

    /*
        Também é possível fazer comentários aninhados
    */
*/

//
// MARK: Coleções (Collections)
//

/*
    Os tipos Array e Dictionary são structs e, portanto, `let` e `var`
    também indicam se eles são mutáveis (var) or imutáveis (let)
    na altura em que se declaram estes tipos.
*/

// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
let emptyArray = [String]() // let == imutável
let emptyArray2 = Array<String>() // mesmo de cima
var emptyMutableArray = [String]() // var == mutável


// Dictionary
var occupations = [
    "Malcolm": "Captain",
    "kaylee": "Mechanic"
]
occupations["Jayne"] = "Public Relations"
let emptyDictionary = [String: Float]() // let == imutável
let emptyDictionary2 = Dictionary<String, Float>() // mesmo de cima
var emptyMutableDictionary = [String: Float]() // var == mutável


//
// MARK: Controlo de Fluxo (Control Flow)
//

// for loop (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
    if value == 1 {
        print("One!")
    } else {
        print("Not one!")
    }
}

// for loop (dictionary)
var dict = ["one": 1, "two": 2]
for (key, value) in dict {
    print("\(key): \(value)")
}

// ciclo for (limite)
for i in -1...shoppingList.count {
    print(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// usar ..< para excluir o último número

// ciclo while
var i = 1
while i < 1000 {
    i *= 2
}

// ciclo do-whie
do {
    print("hello")
} while 1 == 2

// Switch
// Muito poderoso, imagine `if`s com açúcar sintático
// Funciona para String, instâncias de objectos e primitivas (Int, Double, etc.)
let vegetable = "red pepper"
switch vegetable {
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let localScopeValue where localScopeValue.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(localScopeValue)?"
default: // obrigatório (de forma a cobrir todos os possíveis inputs)
    let vegetableComment = "Everything tastes good in soup."
}


//
// MARK: Funções (Functions)
//

// Funções são tipos de primeira classe, o que significa que podem ser
// aninhadas dentro de outras funções e passadas como argumento

// Função em Swift com documentação no header

/**
    Função de cumprimento.

    - Um ponto em documentação
    - Outro ponto na documentação

    :param: nome Um nome
    :param: dia Um dia
    :returns: Uma string com um cumprimento contendo o nome e o dia.
*/
func greet(nome: String, dia: String) -> String {
    return "Hello \(nome), today is \(dia)."
}
greet("Bob", "Tuesday")

// Semelhante ao método de cima excepto ao comportamento dos argumentos
func greet2(#nomeObrigatório: String, nomeArgumentoExterno nomeArgumentoLocal: String) -> String {
    return "Hello \(nomeObrigatório), the day is \(nomeArgumentoLocal)"
}
greet2(nomeObrigatório:"John", nomeArgumentoExterno: "Sunday")

// Função que devolve vários itens num tuplo
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// Ignorar tuplos ou outros valores usando _ (underscore)
let (_, price1, _) = pricesTuple // price1 == 3.69
print(price1 == pricesTuple.1) // true
print("Gas price: \(price)")

// Argumentos variáveis
func setup(numbers: Int...) {
    // é um array
    let number = numbers[0]
    let argCount = numbers.count
}

// Passar e devolver funções
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

// Passar por referência (inout)
func swapTwoInts(inout a: Int, inout b: Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(&someIntA, &someIntB)
print(someIntB) // 7


//
// MARK: Closures
//
var numbers = [1, 2, 6]

// Funções são casos especiais de closures ({})

// Exemplo de um Closure.
// `->` separa o argumento e o tipo de retorno.
// `in` separa o cabeçalho do closure do corpo do closure.
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})

// Quando o tipo é conhecido, como em cima, podemos fazer o seguinte
numbers = numbers.map({ number in 3 * number })
// Ou até mesmo isto
//numbers = numbers.map({ $0 * 3 })

print(numbers) // [3, 6, 18]

// Closure à direita (Trailing closure)
numbers = sorted(numbers) { $0 > $1 }

print(numbers) // [18, 6, 3]

// Super curto, pois o operador < consegue inferir o tipo

numbers = sorted(numbers, < )

print(numbers) // [3, 6, 18]

//
// MARK: Estruturas (Structures)
//

// Estruturas (struct) e classes (class) têm capacidades muito semelhantes
struct NamesTable {
    let names = [String]()

    // Custom subscript
    subscript(index: Int) -> String {
        return names[index]
    }
}

// Estruturas têm um inicializador implicito que é automaticamente gerado
let namesTable = NamesTable(names: ["Me", "Them"])
let name = namesTable[1]
print("Name is \(name)") // Name is Them

//
// MARK: Classes
//

// Classes, estruturas e os seus membros têm três níveis de controlo de acesso
// Nomeadamente: interno (predefinição)(internal) , público (public), privado (private)

public class Shape {
    public func getArea() -> Int {
        return 0;
    }
}

// Todos os métodos e propriedades de uma classe são públicos.
// Se só for necessário guarda dados num
// objecto estruturado, então é melhor usar uma `struct`

internal class Rect: Shape {
    var sideLength: Int = 1

    // Propriedade getter e setter personalizado
    private var perimeter: Int {
        get {
            return 4 * sideLength
        }
        set {
            // `newValue` é uma variável implicita disponível aos setters
            sideLength = newValue / 4
        }
    }

    // Carregar preguiçosamente uma propriedade
    // subShape permanece a nil (unintialized) até o getter ser invocado
    lazy var subShape = Rect(sideLength: 4)

    // Se não for necessário um getter e setter personalizado,
    // mas se quiser correr o código antes e depois de modificar ou aceder
    // uma propriedade, é possível usar `willSet` e `didSet`
    var identifier: String = "defaultID" {
        // o argumento de `willSet` é o nome da variável para o novo valor
        willSet(someIdentifier) {
            print(someIdentifier)
        }
    }

    init(sideLength: Int) {
        self.sideLength = sideLength
        // invocar super.init no final do método de inicialização
        super.init()
    }

    func shrink() {
        if sideLength > 0 {
            sideLength -= 1
        }
    }

    override func getArea() -> Int {
        return sideLength * sideLength
    }
}

// A class `Square` estende (extends) a classe `Rect` (hierarquia)
class Square: Rect {
    convenience init() {
        self.init(sideLength: 5)
    }
}

var mySquare = Square()
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4

// Cast de uma instância de `Square` para `Shape`
let aShape = mySquare as Shape

// Compara instâncias, não é igual a == , visto que == compara objects (igual a)
if mySquare === mySquare {
    print("Yep, it's mySquare")
}

// Inicializador (init) com Optional
class Circle: Shape {
    var radius: Int
    override func getArea() -> Int {
        return 3 * radius * radius
    }

    // Colocar um ponto de interrpgação depois de `init` cria um inicializador
    // Optional, o qual pode retornar nil
    init?(radius: Int) {
        self.radius = radius
        super.init()

        if radius <= 0 {
            return nil
        }
    }
}

var myCircle = Circle(radius: 1)
print(myCircle?.getArea())    // Optional(3)
print(myCircle!.getArea())    // 3
var myEmptyCircle = Circle(radius: -1)
print(myEmptyCircle?.getArea())    // "nil"
if let circle = myEmptyCircle {
    // Não vai executar pois a variável myEmptyCircle é igual a nil
    print("circle is not nil")
}


//
// MARK: Enumerações (Enums)
//

// Enums pode opcionalmente ser um tipo especifico ou não.
// Enums podem conter métodos tal como as classes.

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func getIcon() -> String {
        switch self {
        case .Spades: return "♤"
        case .Hearts: return "♡"
        case .Diamonds: return "♢"
        case .Clubs: return "♧"
        }
    }
}

// Os valores de Enum permitem syntax reduzida, não é preciso escrever o tipo do enum
// quando a variável é explicitamente definida.
var suitValue: Suit = .Hearts

// Enums que não sejam inteiros obrigam a atribuições valor bruto (raw value) diretas
enum BookName: String {
    case John = "John"
    case Luke = "Luke"
}
print("Name: \(BookName.John.rawValue)")

// Enum com valores associados
enum Furniture {
    // Associar com um inteiro (Int)
    case Desk(height: Int)
    // Associar com uma String e um Int
    case Chair(String, Int)

    func description() -> String {
        switch self {
        case .Desk(let height):
            return "Desk with \(height) cm"
        case .Chair(let brand, let height):
            return "Chair of \(brand) with \(height) cm"
        }
    }
}

var desk: Furniture = .Desk(height: 80)
print(desk.description())     // "Desk with 80 cm"
var chair = Furniture.Chair("Foo", 40)
print(chair.description())    // "Chair of Foo with 40 cm"


//
// MARK: Protocolos (Protocols)
//

// Protocolos (`protcol`s) obrigam a que os tipos tenham
// propriedades de instância, métodos de instância, métodos de tipo,
// operadores e subscripts específicos.

protocol ShapeGenerator {
    var enabled: Bool { get set }
    func buildShape() -> Shape
}

// Protocolos definidos com @objc permitem funções com optional
// que permitem verificar se existem conformidade
@objc protocol TransformShape {
    optional func reshaped()
    optional func canReshape() -> Bool
}

class MyShape: Rect {
    var delegate: TransformShape?

    func grow() {
        sideLength += 2

        // Coloca um ponto de interrogação após uma propriedade opcional, método
        // ou subscript para graciosamente ignorar um valor nil e retornar nil
        // em vez de provoar um erro em tempo de execução ("optional chaining").
        if let allow = self.delegate?.canReshape?() {
            // testar o delegate e depois o método
            self.delegate?.reshaped?()
        }
    }
}


//
// MARK: Outro
//

// extensões (`extension`s): Adiciona funcionalidade extra a um tipo já existente.

// Square agora "conforma" com o protocolo `Printable`
extension Square: Printable {
    var description: String {
        return "Area: \(self.getArea()) - ID: \(self.identifier)"
    }
}

print("Square: \(mySquare)")

// Também é possível extender tipos já embutidos
extension Int {
    var customProperty: String {
        return "This is \(self)"
    }

    func multiplyBy(num: Int) -> Int {
        return num * self
    }
}

print(7.customProperty) // "This is 7"
print(14.multiplyBy(3)) // 42

// Generics: Semelhante a Java e C#. Usa a palavra-chave `where` para
// especificar requisitos do `generics`.

func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
    for (index, value) in enumerate(array) {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let foundAtIndex = findIndex([1, 2, 3, 4], 3)
print(foundAtIndex == 2) // true

// Operadores:
// Operadores personalizados podem começar com caracteres:
//      / = - + * % < > ! & | ^ . ~
// ou
// Caracteres Unicode matemáticos, símbolos, setas, dingbat e
// caracteres de desenho linha/caixa.
operador prefixo !!! {}

// Um operador prefixo que triplica o comprimento do lado quando usado
prefix func !!! (inout shape: Square) -> Square {
    shape.sideLength *= 3
    return shape
}

// valor atual
print(mySquare.sideLength) // 4

// muda o comprimento deste lado usando o operador personalizado !!!, aumenta
// o comprimento 3x
!!!mySquare
print(mySquare.sideLength) // 12

// Operadores também podem ser generics
infix operator <-> {}
func <-><T: Equatable> (inout a: T, inout b: T) {
    let c = a
    a = b
    b = c
}

var foo: Float = 10
var bar: Float = 20

foo <-> bar
print("foo is \(foo), bar is \(bar)") // "foo is 20.0, bar is 10.0"
```
---
language: purescript
filename: purescript.purs
contributors:
    - ["Fredrik Dyrkell", "http://www.lexicallyscoped.com"]
    - ["Thimoteus", "https://github.com/Thimoteus"]
---

PureScript is a small strongly, statically typed language compiling to Javascript.

* Learn more at [http://www.purescript.org/](http://www.purescript.org/)
* Documentation: [http://pursuit.purescript.org/](http://pursuit.purescript.org/)
* Book: Purescript by Example, [https://leanpub.com/purescript/](https://leanpub.com/purescript/)

All the noncommented lines of code can be run in the PSCI REPL, though some will
require the `--multi-line-mode` flag.

```haskell

--
-- 1. Primitive datatypes that corresponds to their Javascript
-- equivalents at runtime.

import Prelude
-- Numbers
1.0 + 7.2*5.5 :: Number -- 40.6
-- Ints
1 + 2*5 :: Int -- 11
-- Types are inferred, so the following works fine
9.0/2.5 + 4.4 -- 8.0
-- But Ints and Numbers don't mix, so the following won't
5/2 + 2.5 -- Expression 2.5 does not have type Int
-- Hexadecimal literals
0xff + 1 -- 256
-- Unary negation
6 * -3 -- -18
6 * negate 3 -- -18
-- Modulus, from purescript-math (Math)
3.0 % 2.0 -- 1.0
4.0 % 2.0 -- 0.0
-- Inspect the type of an expression in psci
:t 9.5/2.5 + 4.4 -- Prim.Number

-- Booleans
true :: Boolean -- true
false :: Boolean -- false
-- Negation
not true -- false
23 == 23 -- true
1 /= 4 -- true
1 >= 4 -- false
-- Comparisons < <= > >=
-- are defined in terms of compare
compare 1 2 -- LT
compare 2 2 -- EQ
compare 3 2 -- GT
-- Conjunction and Disjunction
true && (9 >= 19 || 1 < 2) -- true

-- Strings
"Hellow" :: String -- "Hellow"
-- Multiline string without newlines, to run in psci use the --multi-line-mode flag
"Hellow\
\orld" -- "Helloworld"
-- Multiline string with newlines
"""Hello
world""" -- "Hello\nworld"
-- Concatenate
"such " <> "amaze" -- "such amaze"

--
-- 2. Arrays are Javascript arrays, but must be homogeneous

[1,1,2,3,5,8] :: Array Number -- [1,1,2,3,5,8]
[true, true, false] :: Array Boolean -- [true,true,false]
-- [1,2, true, "false"] won't work
-- `Cannot unify Prim.Int with Prim.Boolean`
-- Cons (prepend)
1 : [2,4,3] -- [1,2,4,3]

-- Requires purescript-arrays (Data.Array)
-- and purescript-maybe (Data.Maybe)

-- Safe access return Maybe a
head [1,2,3] -- Just (1)
tail [3,2,1] -- Just ([2,1])
init [1,2,3] -- Just ([1,2])
last [3,2,1] -- Just (1)
-- Array access - indexing
[3,4,5,6,7] !! 2 -- Just (5)
-- Range
1..5 -- [1,2,3,4,5]
length [2,2,2] -- 3
drop 3 [5,4,3,2,1] -- [2,1]
take 3 [5,4,3,2,1] -- [5,4,3]
append [1,2,3] [4,5,6] -- [1,2,3,4,5,6]

--
-- 3. Records are Javascript objects, with zero or more fields, which
-- can have different types.
-- In psci you have to write `let` in front of the function to get a
-- top level binding.
let book = {title: "Foucault's pendulum", author: "Umberto Eco"}
-- Access properties
book.title -- "Foucault's pendulum"

let getTitle b = b.title
-- Works on all records with a title (but doesn't require any other field)
getTitle book -- "Foucault's pendulum"
getTitle {title: "Weekend in Monaco", artist: "The Rippingtons"} -- "Weekend in Monaco"
-- Can use underscores as shorthand
_.title book -- "Foucault's pendulum"
-- Update a record
let changeTitle b t = b {title = t}
getTitle (changeTitle book "Ill nome della rosa") -- "Ill nome della rosa"

--
-- 4. Functions
-- In psci's multiline mode
let sumOfSquares :: Int -> Int -> Int
    sumOfSquares x y = x*x + y*y
sumOfSquares 3 4 -- 25
let myMod x y = x % y
myMod 3.0 2.0 -- 1.0
-- Infix application of function
3 `mod` 2 -- 1

-- function application has higher precedence than all other
-- operators
sumOfSquares 3 4 * sumOfSquares 4 5 -- 1025

-- Conditional
let abs' n = if n>=0 then n else -n
abs' (-3) -- 3

-- Guarded equations
let abs'' n | n >= 0    = n
            | otherwise = -n

-- Pattern matching

-- Note the type signature, input is a list of numbers. The pattern matching
-- destructures and binds the list into parts.
-- Requires purescript-lists (Data.List)
let first :: forall a. List a -> a
    first (Cons x _) = x
first (toList [3,4,5]) -- 3
let second :: forall a. List a -> a
    second (Cons _ (Cons y _)) = y
second (toList [3,4,5]) -- 4
let sumTwo :: List Int -> List Int
    sumTwo (Cons x (Cons y rest)) = x + y : rest
fromList (sumTwo (toList [2,3,4,5,6])) :: Array Int -- [5,4,5,6]

-- sumTwo doesn't handle when the list is empty or there's only one element in
-- which case you get an error.
sumTwo [1] -- Failed pattern match

-- Complementing patterns to match
-- Good ol' Fibonacci
let fib 1 = 1
    fib 2 = 2
    fib x = fib (x-1) + fib (x-2)
fib 10 -- 89

-- Use underscore to match any, where you don't care about the binding name
let isZero 0 = true
    isZero _ = false

-- Pattern matching on records
let ecoTitle {author = "Umberto Eco", title = t} = Just t
    ecoTitle _ = Nothing

ecoTitle book -- Just ("Foucault's pendulum")
ecoTitle {title: "The Quantum Thief", author: "Hannu Rajaniemi"} -- Nothing
-- ecoTitle requires both field to type check:
ecoTitle {title: "The Quantum Thief"} -- Object lacks required property "author"

-- Lambda expressions
(\x -> x*x) 3 -- 9
(\x y -> x*x + y*y) 4 5 -- 41
let sqr = \x -> x*x

-- Currying
let myAdd x y = x + y -- is equivalent with
let myAdd' = \x -> \y -> x + y
let add3 = myAdd 3
:t add3 -- Prim.Int -> Prim.Int

-- Forward and backward function composition
-- drop 3 followed by taking 5
(drop 3 >>> take 5) (1..20) -- [4,5,6,7,8]
-- take 5 followed by dropping 3
(drop 3 <<< take 5) (1..20) -- [4,5]

-- Operations using higher order functions
let even x = x `mod` 2 == 0
filter even (1..10) -- [2,4,6,8,10]
map (\x -> x + 11) (1..5) -- [12,13,14,15,16]

-- Requires purescript-foldable-traversable (Data.Foldable)

foldr (+) 0 (1..10) -- 55
sum (1..10) -- 55
product (1..10) -- 3628800

-- Testing with predicate
any even [1,2,3] -- true
all even [1,2,3] -- false

```
---
category: tool
tool: PyQT
filename: learnpyqt.py
contributors:
    - ["Nathan Hughes", "https://github.com/sirsharpest"]
---

**Qt** is a widely-known framework for developing cross-platform software that can be run on various software and hardware platforms with little or no change in the code, while having the power and speed of native applications. Though **Qt** was originally written in *C++*.


This is an adaption on the C++ intro to QT by [Aleksey Kholovchuk](https://github.com/vortexxx192
), some of the code examples should result in the same functionality
this version just having been done using pyqt! 

```python
import sys
from PyQt4 import QtGui
	
def window():
	# Create an application object 
    app = QtGui.QApplication(sys.argv)
	# Create a widget where our label will be placed in
    w = QtGui.QWidget()
	# Add a label to the widget 
    b = QtGui.QLabel(w)
	# Set some text for the label 
    b.setText("Hello World!")
	# Give some size and placement information 
    w.setGeometry(100, 100, 200, 50)
    b.move(50, 20)
	# Give our window a nice title 
    w.setWindowTitle("PyQt")
	# Have everything display
    w.show()
	# Execute what we have asked for, once all setup
    sys.exit(app.exec_())

if __name__ == '__main__':
    window()

```

In order to get some of the more advanced features in **pyqt** we need to start looking at building additional elements. 
Here we show how to introduce a dialog popup box, useful for asking the user to confirm a decision or to provide information.

```Python 
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


def window():
    app = QApplication(sys.argv)
    w = QWidget()
    # Create a button and attach to widget w
    b = QPushButton(w)
    b.setText("Press me")
    b.move(50, 50)
    # Tell b to call this function when clicked
    # notice the lack of "()" on the function call
    b.clicked.connect(showdialog)
    w.setWindowTitle("PyQt Dialog")
    w.show()
    sys.exit(app.exec_())
	
# This function should create a dialog window with a button
# that waits to be clicked and then exits the program
def showdialog():
    d = QDialog()
    b1 = QPushButton("ok", d)
    b1.move(50, 50)
    d.setWindowTitle("Dialog")
    # This modality tells the popup to block the parent whilst it's active
    d.setWindowModality(Qt.ApplicationModal)
    # On click I'd like the entire process to end
    b1.clicked.connect(sys.exit)
    d.exec_()

if __name__ == '__main__':
    window()
```
---
language: python
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
    - ["Amin Bandali", "https://aminb.org"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["evuez", "http://github.com/evuez"]
    - ["asyne", "https://github.com/justblah"]
    - ["habi", "http://github.com/habi"]
filename: learnpython.py
---

Python was created by Guido Van Rossum in the early 90s. It is now one of the
most popular languages in existence. I fell in love with Python for its
syntactic clarity. It's basically executable pseudocode.

Feedback would be highly appreciated! You can reach me at [@louiedinh](http://twitter.com/louiedinh)
or louiedinh [at] [google's email service]

Note: This article applies to Python 2.7 specifically, but should be applicable
to Python 2.x. Python 2.7 is reaching end of life and will stop being
maintained in 2020, it is though recommended to start learning Python with
Python 3. For Python 3.x, take a look at the [Python 3 tutorial](http://learnxinyminutes.com/docs/python3/).

It is also possible to write Python code which is compatible with Python 2.7
and 3.x at the same time, using Python [`__future__` imports](https://docs.python.org/2/library/__future__.html). `__future__` imports
allow you to write Python 3 code that will run on Python 2, so check out the
Python 3 tutorial.

```python

# Single line comments start with a number symbol.

""" Multiline strings can be written
    using three "s, and are often used
    as comments
"""

####################################################
# 1. Primitive Datatypes and Operators
####################################################

# You have numbers
3  # => 3

# Math is what you would expect
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20
35 / 5  # => 7

# Division is a bit tricky. It is integer division and floors the results
# automatically.
5 / 2  # => 2

# To fix division we need to learn about floats.
2.0  # This is a float
11.0 / 4.0  # => 2.75 ahhh...much better

# Result of integer division truncated down both for positive and negative.
5 // 3  # => 1
5.0 // 3.0  # => 1.0 # works on floats too
-5 // 3  # => -2
-5.0 // 3.0  # => -2.0

# Note that we can also import division module(Section 6 Modules)
# to carry out normal division with just one '/'.
from __future__ import division

11 / 4  # => 2.75  ...normal division
11 // 4  # => 2 ...floored division

# Modulo operation
7 % 3  # => 1

# Exponentiation (x to the yth power)
2 ** 4  # => 16

# Enforce precedence with parentheses
(1 + 3) * 2  # => 8

# Boolean Operators
# Note "and" and "or" are case-sensitive
True and False  # => False
False or True  # => True

# Note using Bool operators with ints
0 and 2  # => 0
-5 or 0  # => -5
0 == False  # => True
2 == True  # => False
1 == True  # => True

# negate with not
not True  # => False
not False  # => True

# Equality is ==
1 == 1  # => True
2 == 1  # => False

# Inequality is !=
1 != 1  # => False
2 != 1  # => True

# More comparisons
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# Comparisons can be chained!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# Strings are created with " or '
"This is a string."
'This is also a string.'

# Strings can be added too!
"Hello " + "world!"  # => "Hello world!"
# Strings can be added without using '+'
"Hello " "world!"  # => "Hello world!"

# ... or multiplied
"Hello" * 3  # => "HelloHelloHello"

# A string can be treated like a list of characters
"This is a string"[0]  # => 'T'

# You can find the length of a string
len("This is a string")  # => 16

# String formatting with %
# Even though the % string operator will be deprecated on Python 3.1 and removed
# later at some time, it may still be good to know how it works.
x = 'apple'
y = 'lemon'
z = "The items in the basket are %s and %s" % (x, y)

# A newer way to format strings is the format method.
# This method is the preferred way
"{} is a {}".format("This", "placeholder")
"{0} can be {1}".format("strings", "formatted")
# You can use keywords if you don't want to count.
"{name} wants to eat {food}".format(name="Bob", food="lasagna")

# None is an object
None  # => None

# Don't use the equality "==" symbol to compare objects to None
# Use "is" instead
"etc" is None  # => False
None is None  # => True

# The 'is' operator tests for object identity. This isn't
# very useful when dealing with primitive values, but is
# very useful when dealing with objects.

# Any object can be used in a Boolean context.
# The following values are considered falsey:
#    - None
#    - zero of any numeric type (e.g., 0, 0L, 0.0, 0j)
#    - empty sequences (e.g., '', (), [])
#    - empty containers (e.g., {}, set())
#    - instances of user-defined classes meeting certain conditions
#      see: https://docs.python.org/2/reference/datamodel.html#object.__nonzero__
#
# All other values are truthy (using the bool() function on them returns True).
bool(0)  # => False
bool("")  # => False


####################################################
# 2. Variables and Collections
####################################################

# Python has a print statement
print "I'm Python. Nice to meet you!"  # => I'm Python. Nice to meet you!

# Simple way to get input data from console
input_string_var = raw_input(
    "Enter some data: ")  # Returns the data as a string
input_var = input("Enter some data: ")  # Evaluates the data as python code
# Warning: Caution is recommended for input() method usage
# Note: In python 3, input() is deprecated and raw_input() is renamed to input()

# No need to declare variables before assigning to them.
some_var = 5  # Convention is to use lower_case_with_underscores
some_var  # => 5

# Accessing a previously unassigned variable is an exception.
# See Control Flow to learn more about exception handling.
some_other_var  # Raises a name error

# if can be used as an expression
# Equivalent of C's '?:' ternary operator
"yahoo!" if 3 > 2 else 2  # => "yahoo!"

# Lists store sequences
li = []
# You can start with a prefilled list
other_li = [4, 5, 6]

# Add stuff to the end of a list with append
li.append(1)  # li is now [1]
li.append(2)  # li is now [1, 2]
li.append(4)  # li is now [1, 2, 4]
li.append(3)  # li is now [1, 2, 4, 3]
# Remove from the end with pop
li.pop()  # => 3 and li is now [1, 2, 4]
# Let's put it back
li.append(3)  # li is now [1, 2, 4, 3] again.

# Access a list like you would any array
li[0]  # => 1
# Assign new values to indexes that have already been initialized with =
li[0] = 42
li[0]  # => 42
li[0] = 1  # Note: setting it back to the original value
# Look at the last element
li[-1]  # => 3

# Looking out of bounds is an IndexError
li[4]  # Raises an IndexError

# You can look at ranges with slice syntax.
# (It's a closed/open range for you mathy types.)
li[1:3]  # => [2, 4]
# Omit the beginning
li[2:]  # => [4, 3]
# Omit the end
li[:3]  # => [1, 2, 4]
# Select every second entry
li[::2]  # =>[1, 4]
# Reverse a copy of the list
li[::-1]  # => [3, 4, 2, 1]
# Use any combination of these to make advanced slices
# li[start:end:step]

# Remove arbitrary elements from a list with "del"
del li[2]  # li is now [1, 2, 3]

# You can add lists
li + other_li  # => [1, 2, 3, 4, 5, 6]
# Note: values for li and for other_li are not modified.

# Concatenate lists with "extend()"
li.extend(other_li)  # Now li is [1, 2, 3, 4, 5, 6]

# Remove first occurrence of a value
li.remove(2)  # li is now [1, 3, 4, 5, 6]
li.remove(2)  # Raises a ValueError as 2 is not in the list

# Insert an element at a specific index
li.insert(1, 2)  # li is now [1, 2, 3, 4, 5, 6] again

# Get the index of the first item found
li.index(2)  # => 1
li.index(7)  # Raises a ValueError as 7 is not in the list

# Check for existence in a list with "in"
1 in li  # => True

# Examine the length with "len()"
len(li)  # => 6

# Tuples are like lists but are immutable.
tup = (1, 2, 3)
tup[0]  # => 1
tup[0] = 3  # Raises a TypeError

# You can do all those list thingies on tuples too
len(tup)  # => 3
tup + (4, 5, 6)  # => (1, 2, 3, 4, 5, 6)
tup[:2]  # => (1, 2)
2 in tup  # => True

# You can unpack tuples (or lists) into variables
a, b, c = (1, 2, 3)  # a is now 1, b is now 2 and c is now 3
d, e, f = 4, 5, 6  # you can leave out the parentheses
# Tuples are created by default if you leave out the parentheses
g = 4, 5, 6  # => (4, 5, 6)
# Now look how easy it is to swap two values
e, d = d, e  # d is now 5 and e is now 4

# Dictionaries store mappings
empty_dict = {}
# Here is a prefilled dictionary
filled_dict = {"one": 1, "two": 2, "three": 3}

# Look up values with []
filled_dict["one"]  # => 1

# Get all keys as a list with "keys()"
filled_dict.keys()  # => ["three", "two", "one"]
# Note - Dictionary key ordering is not guaranteed.
# Your results might not match this exactly.

# Get all values as a list with "values()"
filled_dict.values()  # => [3, 2, 1]
# Note - Same as above regarding key ordering.

# Get all key-value pairs as a list of tuples with "items()"
filled_dict.items()  # => [("one", 1), ("two", 2), ("three", 3)]

# Check for existence of keys in a dictionary with "in"
"one" in filled_dict  # => True
1 in filled_dict  # => False

# Looking up a non-existing key is a KeyError
filled_dict["four"]  # KeyError

# Use "get()" method to avoid the KeyError
filled_dict.get("one")  # => 1
filled_dict.get("four")  # => None
# The get method supports a default argument when the value is missing
filled_dict.get("one", 4)  # => 1
filled_dict.get("four", 4)  # => 4
# note that filled_dict.get("four") is still => None
# (get doesn't set the value in the dictionary)

# set the value of a key with a syntax similar to lists
filled_dict["four"] = 4  # now, filled_dict["four"] => 4

# "setdefault()" inserts into a dictionary only if the given key isn't present
filled_dict.setdefault("five", 5)  # filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] is still 5

# Sets store ... well sets (which are like lists but can contain no duplicates)
empty_set = set()
# Initialize a "set()" with a bunch of values
some_set = set([1, 2, 2, 3, 4])  # some_set is now set([1, 2, 3, 4])

# order is not guaranteed, even though it may sometimes look sorted
another_set = set([4, 3, 2, 2, 1])  # another_set is now set([1, 2, 3, 4])

# Since Python 2.7, {} can be used to declare a set
filled_set = {1, 2, 2, 3, 4}  # => {1, 2, 3, 4}

# Add more items to a set
filled_set.add(5)  # filled_set is now {1, 2, 3, 4, 5}

# Do set intersection with &
other_set = {3, 4, 5, 6}
filled_set & other_set  # => {3, 4, 5}

# Do set union with |
filled_set | other_set  # => {1, 2, 3, 4, 5, 6}

# Do set difference with -
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# Do set symmetric difference with ^
{1, 2, 3, 4} ^ {2, 3, 5}  # => {1, 4, 5}

# Check if set on the left is a superset of set on the right
{1, 2} >= {1, 2, 3}  # => False

# Check if set on the left is a subset of set on the right
{1, 2} <= {1, 2, 3}  # => True

# Check for existence in a set with in
2 in filled_set  # => True
10 in filled_set  # => False


####################################################
#  3. Control Flow
####################################################

# Let's just make a variable
some_var = 5

# Here is an if statement. Indentation is significant in python!
# prints "some_var is smaller than 10"
if some_var > 10:
    print "some_var is totally bigger than 10."
elif some_var < 10:  # This elif clause is optional.
    print "some_var is smaller than 10."
else:  # This is optional too.
    print "some_var is indeed 10."

"""
For loops iterate over lists
prints:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # You can use {0} to interpolate formatted strings. (See above.)
    print "{0} is a mammal".format(animal)

"""
"range(number)" returns a list of numbers
from zero to the given number
prints:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
"range(lower, upper)" returns a list of numbers
from the lower number to the upper number
prints:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print i

"""
While loops go until a condition is no longer met.
prints:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Shorthand for x = x + 1

# Handle exceptions with a try/except block

# Works on Python 2.6 and up:
try:
    # Use "raise" to raise an error
    raise IndexError("This is an index error")
except IndexError as e:
    pass  # Pass is just a no-op. Usually you would do recovery here.
except (TypeError, NameError):
    pass  # Multiple exceptions can be handled together, if required.
else:  # Optional clause to the try/except block. Must follow all except blocks
    print "All good!"  # Runs only if the code in try raises no exceptions
finally:  # Execute under all circumstances
    print "We can clean up resources here"

# Instead of try/finally to cleanup resources you can use a with statement
with open("myfile.txt") as f:
    for line in f:
        print line


####################################################
# 4. Functions
####################################################

# Use "def" to create new functions
def add(x, y):
    print "x is {0} and y is {1}".format(x, y)
    return x + y  # Return values with a return statement


# Calling functions with parameters
add(5, 6)  # => prints out "x is 5 and y is 6" and returns 11

# Another way to call functions is with keyword arguments
add(y=6, x=5)  # Keyword arguments can arrive in any order.


# You can define functions that take a variable number of
# positional args, which will be interpreted as a tuple by using *
def varargs(*args):
    return args


varargs(1, 2, 3)  # => (1, 2, 3)


# You can define functions that take a variable number of
# keyword args, as well, which will be interpreted as a dict by using **
def keyword_args(**kwargs):
    return kwargs


# Let's call it to see what happens
keyword_args(big="foot", loch="ness")  # => {"big": "foot", "loch": "ness"}


# You can do both at once, if you like
def all_the_args(*args, **kwargs):
    print args
    print kwargs


"""
all_the_args(1, 2, a=3, b=4) prints:
    (1, 2)
    {"a": 3, "b": 4}
"""

# When calling functions, you can do the opposite of args/kwargs!
# Use * to expand positional args and use ** to expand keyword args.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)  # equivalent to foo(1, 2, 3, 4)
all_the_args(**kwargs)  # equivalent to foo(a=3, b=4)
all_the_args(*args, **kwargs)  # equivalent to foo(1, 2, 3, 4, a=3, b=4)


# you can pass args and kwargs along to other functions that take args/kwargs
# by expanding them with * and ** respectively
def pass_all_the_args(*args, **kwargs):
    all_the_args(*args, **kwargs)
    print varargs(*args)
    print keyword_args(**kwargs)


# Function Scope
x = 5


def set_x(num):
    # Local var x not the same as global variable x
    x = num  # => 43
    print x  # => 43


def set_global_x(num):
    global x
    print x  # => 5
    x = num  # global var x is now set to 6
    print x  # => 6


set_x(43)
set_global_x(6)


# Python has first class functions
def create_adder(x):
    def adder(y):
        return x + y

    return adder


add_10 = create_adder(10)
add_10(3)  # => 13

# There are also anonymous functions
(lambda x: x > 2)(3)  # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1)  # => 5

# There are built-in higher order functions
map(add_10, [1, 2, 3])  # => [11, 12, 13]
map(max, [1, 2, 3], [4, 2, 1])  # => [4, 2, 3]

filter(lambda x: x > 5, [3, 4, 5, 6, 7])  # => [6, 7]

# We can use list comprehensions for nice maps and filters
[add_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]  # => [6, 7]

# You can construct set and dict comprehensions as well.
{x for x in 'abcddeef' if x in 'abc'}  # => {'a', 'b', 'c'}
{x: x ** 2 for x in range(5)}  # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


####################################################
# 5. Classes
####################################################

# We subclass from object to get a class.
class Human(object):
    # A class attribute. It is shared by all instances of this class
    species = "H. sapiens"

    # Basic initializer, this is called when this class is instantiated.
    # Note that the double leading and trailing underscores denote objects
    # or attributes that are used by python but that live in user-controlled
    # namespaces. You should not invent such names on your own.
    def __init__(self, name):
        # Assign the argument to the instance's name attribute
        self.name = name

        # Initialize property
        self.age = 0

    # An instance method. All methods take "self" as the first argument
    def say(self, msg):
        return "{0}: {1}".format(self.name, msg)

    # A class method is shared among all instances
    # They are called with the calling class as the first argument
    @classmethod
    def get_species(cls):
        return cls.species

    # A static method is called without a class or instance reference
    @staticmethod
    def grunt():
        return "*grunt*"

    # A property is just like a getter.
    # It turns the method age() into an read-only attribute
    # of the same name.
    @property
    def age(self):
        return self._age

    # This allows the property to be set
    @age.setter
    def age(self, age):
        self._age = age

    # This allows the property to be deleted
    @age.deleter
    def age(self):
        del self._age


# Instantiate a class
i = Human(name="Ian")
print i.say("hi")  # prints out "Ian: hi"

j = Human("Joel")
print j.say("hello")  # prints out "Joel: hello"

# Call our class method
i.get_species()  # => "H. sapiens"

# Change the shared attribute
Human.species = "H. neanderthalensis"
i.get_species()  # => "H. neanderthalensis"
j.get_species()  # => "H. neanderthalensis"

# Call the static method
Human.grunt()  # => "*grunt*"

# Update the property
i.age = 42

# Get the property
i.age  # => 42

# Delete the property
del i.age
i.age  # => raises an AttributeError

####################################################
# 6. Modules
####################################################

# You can import modules
import math

print math.sqrt(16)  # => 4

# You can get specific functions from a module
from math import ceil, floor

print ceil(3.7)  # => 4.0
print floor(3.7)  # => 3.0

# You can import all functions from a module.
# Warning: this is not recommended
from math import *

# You can shorten module names
import math as m

math.sqrt(16) == m.sqrt(16)  # => True
# you can also test that the functions are equivalent
from math import sqrt

math.sqrt == m.sqrt == sqrt  # => True

# Python modules are just ordinary python files. You
# can write your own, and import them. The name of the
# module is the same as the name of the file.

# You can find out which functions and attributes
# defines a module.
import math

dir(math)


# If you have a Python script named math.py in the same
# folder as your current script, the file math.py will
# be loaded instead of the built-in Python module.
# This happens because the local folder has priority
# over Python's built-in libraries.


####################################################
# 7. Advanced
####################################################

# Generators
# A generator "generates" values as they are requested instead of storing
# everything up front

# The following method (*NOT* a generator) will double all values and store it
# in `double_arr`. For large size of iterables, that might get huge!
def double_numbers(iterable):
    double_arr = []
    for i in iterable:
        double_arr.append(i + i)
    return double_arr


# Running the following would mean we'll double all values first and return all
# of them back to be checked by our condition
for value in double_numbers(range(1000000)):  # `test_non_generator`
    print value
    if value > 5:
        break


# We could instead use a generator to "generate" the doubled value as the item
# is being requested
def double_numbers_generator(iterable):
    for i in iterable:
        yield i + i


# Running the same code as before, but with a generator, now allows us to iterate
# over the values and doubling them one by one as they are being consumed by
# our logic. Hence as soon as we see a value > 5, we break out of the
# loop and don't need to double most of the values sent in (MUCH FASTER!)
for value in double_numbers_generator(xrange(1000000)):  # `test_generator`
    print value
    if value > 5:
        break

# BTW: did you notice the use of `range` in `test_non_generator` and `xrange` in `test_generator`?
# Just as `double_numbers_generator` is the generator version of `double_numbers`
# We have `xrange` as the generator version of `range`
# `range` would return back and array with 1000000 values for us to use
# `xrange` would generate 1000000 values for us as we request / iterate over those items

# Just as you can create a list comprehension, you can create generator
# comprehensions as well.
values = (-x for x in [1, 2, 3, 4, 5])
for x in values:
    print(x)  # prints -1 -2 -3 -4 -5 to console/terminal

# You can also cast a generator comprehension directly to a list.
values = (-x for x in [1, 2, 3, 4, 5])
gen_to_list = list(values)
print(gen_to_list)  # => [-1, -2, -3, -4, -5]

# Decorators
# A decorator is a higher order function, which accepts and returns a function.
# Simple usage example – add_apples decorator will add 'Apple' element into
# fruits list returned by get_fruits target function.
def add_apples(func):
    def get_fruits():
        fruits = func()
        fruits.append('Apple')
        return fruits
    return get_fruits

@add_apples
def get_fruits():
    return ['Banana', 'Mango', 'Orange']

# Prints out the list of fruits with 'Apple' element in it:
# Banana, Mango, Orange, Apple
print ', '.join(get_fruits())

# in this example beg wraps say
# Beg will call say. If say_please is True then it will change the returned
# message
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Please! I am poor :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Can you buy me a beer?"
    return msg, say_please


print say()  # Can you buy me a beer?
print say(say_please=True)  # Can you buy me a beer? Please! I am poor :(
```

## Ready For More?

### Free Online

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)
* [LearnPython](http://www.learnpython.org/)
* [Fullstack Python](https://www.fullstackpython.com/)

### Dead Tree

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
    - ["evuez", "http://github.com/evuez"]
filename: learnpython3.py
---

Python was created by Guido van Rossum in the early 90s. It is now one of the most popular
languages in existence. I fell in love with Python for its syntactic clarity. It's basically
executable pseudocode.

Feedback would be highly appreciated! You can reach me at [@louiedinh](http://twitter.com/louiedinh) or louiedinh [at] [google's email service]

Note: This article applies to Python 3 specifically. Check out [here](http://learnxinyminutes.com/docs/python/) if you want to learn the old Python 2.7

```python

# Single line comments start with a number symbol.

""" Multiline strings can be written
    using three "s, and are often used
    as documentation.
"""

####################################################
## 1. Primitive Datatypes and Operators
####################################################

# You have numbers
3  # => 3

# Math is what you would expect
1 + 1   # => 2
8 - 1   # => 7
10 * 2  # => 20
35 / 5  # => 7.0

# Result of integer division truncated down both for positive and negative.
5 // 3       # => 1
5.0 // 3.0   # => 1.0 # works on floats too
-5 // 3      # => -2
-5.0 // 3.0  # => -2.0

# The result of division is always a float
10.0 / 3  # => 3.3333333333333335

# Modulo operation
7 % 3  # => 1

# Exponentiation (x**y, x to the yth power)
2**3  # => 8

# Enforce precedence with parentheses
(1 + 3) * 2  # => 8

# Boolean values are primitives (Note: the capitalization)
True
False

# negate with not
not True   # => False
not False  # => True

# Boolean Operators
# Note "and" and "or" are case-sensitive
True and False  # => False
False or True   # => True

# Note using Bool operators with ints
# False is 0 and True is 1
# Don't mix up with bool(ints) and bitwise and/or (&,|)
0 and 2     # => 0
-5 or 0     # => -5
0 == False  # => True
2 == True   # => False
1 == True   # => True
-5 != False != True #=> True

# Equality is ==
1 == 1  # => True
2 == 1  # => False

# Inequality is !=
1 != 1  # => False
2 != 1  # => True

# More comparisons
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# Comparisons can be chained!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# (is vs. ==) is checks if two variables refer to the same object, but == checks
# if the objects pointed to have the same values.
a = [1, 2, 3, 4]  # Point a at a new list, [1, 2, 3, 4]
b = a             # Point b at what a is pointing to
b is a            # => True, a and b refer to the same object
b == a            # => True, a's and b's objects are equal
b = [1, 2, 3, 4]  # Point b at a new list, [1, 2, 3, 4]
b is a            # => False, a and b do not refer to the same object
b == a            # => True, a's and b's objects are equal

# Strings are created with " or '
"This is a string."
'This is also a string.'

# Strings can be added too! But try not to do this.
"Hello " + "world!"  # => "Hello world!"
# String literals (but not variables) can be concatenated without using '+'
"Hello " "world!"    # => "Hello world!"

# A string can be treated like a list of characters
"This is a string"[0]  # => 'T'

# You can find the length of a string
len("This is a string")  # => 16

# .format can be used to format strings, like this:
"{} can be {}".format("Strings", "interpolated")  # => "Strings can be interpolated"

# You can repeat the formatting arguments to save some typing.
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
# => "Jack be nimble, Jack be quick, Jack jump over the candle stick"

# You can use keywords if you don't want to count.
"{name} wants to eat {food}".format(name="Bob", food="lasagna")  # => "Bob wants to eat lasagna"

# If your Python 3 code also needs to run on Python 2.5 and below, you can also
# still use the old style of formatting:
"%s can be %s the %s way" % ("Strings", "interpolated", "old")  # => "Strings can be interpolated the old way"


# None is an object
None  # => None

# Don't use the equality "==" symbol to compare objects to None
# Use "is" instead. This checks for equality of object identity.
"etc" is None  # => False
None is None   # => True

# None, 0, and empty strings/lists/dicts/tuples all evaluate to False.
# All other values are True
bool(0)   # => False
bool("")  # => False
bool([])  # => False
bool({})  # => False
bool(())  # => False

####################################################
## 2. Variables and Collections
####################################################

# Python has a print function
print("I'm Python. Nice to meet you!")  # => I'm Python. Nice to meet you!

# By default the print function also prints out a newline at the end.
# Use the optional argument end to change the end string.
print("Hello, World", end="!")  # => Hello, World!

# Simple way to get input data from console
input_string_var = input("Enter some data: ") # Returns the data as a string
# Note: In earlier versions of Python, input() method was named as raw_input()

# There are no declarations, only assignments.
# Convention is to use lower_case_with_underscores
some_var = 5
some_var  # => 5

# Accessing a previously unassigned variable is an exception.
# See Control Flow to learn more about exception handling.
some_unknown_var  # Raises a NameError

# if can be used as an expression
# Equivalent of C's '?:' ternary operator
"yahoo!" if 3 > 2 else 2  # => "yahoo!"

# Lists store sequences
li = []
# You can start with a prefilled list
other_li = [4, 5, 6]

# Add stuff to the end of a list with append
li.append(1)    # li is now [1]
li.append(2)    # li is now [1, 2]
li.append(4)    # li is now [1, 2, 4]
li.append(3)    # li is now [1, 2, 4, 3]
# Remove from the end with pop
li.pop()        # => 3 and li is now [1, 2, 4]
# Let's put it back
li.append(3)    # li is now [1, 2, 4, 3] again.

# Access a list like you would any array
li[0]   # => 1
# Look at the last element
li[-1]  # => 3

# Looking out of bounds is an IndexError
li[4]  # Raises an IndexError

# You can look at ranges with slice syntax.
# The start index is included, the end index is not
# (It's a closed/open range for you mathy types.)
li[1:3]   # => [2, 4]
# Omit the end
li[2:]    # => [4, 3]
# Omit the beginning
li[:3]    # => [1, 2, 4]
# Select every second entry
li[::2]   # =>[1, 4]
# Return a reversed copy of the list
li[::-1]  # => [3, 4, 2, 1]
# Use any combination of these to make advanced slices
# li[start:end:step]

# Make a one layer deep copy using slices
li2 = li[:]  # => li2 = [1, 2, 4, 3] but (li2 is li) will result in false.

# Remove arbitrary elements from a list with "del"
del li[2]  # li is now [1, 2, 3]

# Remove first occurrence of a value
li.remove(2)  # li is now [1, 3]
li.remove(2)  # Raises a ValueError as 2 is not in the list

# Insert an element at a specific index
li.insert(1, 2)  # li is now [1, 2, 3] again

# Get the index of the first item found matching the argument
li.index(2)  # => 1
li.index(4)  # Raises a ValueError as 4 is not in the list

# You can add lists
# Note: values for li and for other_li are not modified.
li + other_li  # => [1, 2, 3, 4, 5, 6]

# Concatenate lists with "extend()"
li.extend(other_li)  # Now li is [1, 2, 3, 4, 5, 6]

# Check for existence in a list with "in"
1 in li  # => True

# Examine the length with "len()"
len(li)  # => 6


# Tuples are like lists but are immutable.
tup = (1, 2, 3)
tup[0]      # => 1
tup[0] = 3  # Raises a TypeError

# Note that a tuple of length one has to have a comma after the last element but
# tuples of other lengths, even zero, do not.
type((1))   # => <class 'int'>
type((1,))  # => <class 'tuple'>
type(())    # => <class 'tuple'>

# You can do most of the list operations on tuples too
len(tup)         # => 3
tup + (4, 5, 6)  # => (1, 2, 3, 4, 5, 6)
tup[:2]          # => (1, 2)
2 in tup         # => True

# You can unpack tuples (or lists) into variables
a, b, c = (1, 2, 3)  # a is now 1, b is now 2 and c is now 3
# You can also do extended unpacking
a, *b, c = (1, 2, 3, 4)  # a is now 1, b is now [2, 3] and c is now 4
# Tuples are created by default if you leave out the parentheses
d, e, f = 4, 5, 6
# Now look how easy it is to swap two values
e, d = d, e  # d is now 5 and e is now 4


# Dictionaries store mappings from keys to values
empty_dict = {}
# Here is a prefilled dictionary
filled_dict = {"one": 1, "two": 2, "three": 3}

# Note keys for dictionaries have to be immutable types. This is to ensure that
# the key can be converted to a constant hash value for quick look-ups.
# Immutable types include ints, floats, strings, tuples.
invalid_dict = {[1,2,3]: "123"}  # => Raises a TypeError: unhashable type: 'list'
valid_dict = {(1,2,3):[1,2,3]}   # Values can be of any type, however.

# Look up values with []
filled_dict["one"]  # => 1

# Get all keys as an iterable with "keys()". We need to wrap the call in list()
# to turn it into a list. We'll talk about those later.  Note - Dictionary key
# ordering is not guaranteed. Your results might not match this exactly.
list(filled_dict.keys())  # => ["three", "two", "one"]


# Get all values as an iterable with "values()". Once again we need to wrap it
# in list() to get it out of the iterable. Note - Same as above regarding key
# ordering.
list(filled_dict.values())  # => [3, 2, 1]


# Check for existence of keys in a dictionary with "in"
"one" in filled_dict  # => True
1 in filled_dict      # => False

# Looking up a non-existing key is a KeyError
filled_dict["four"]  # KeyError

# Use "get()" method to avoid the KeyError
filled_dict.get("one")      # => 1
filled_dict.get("four")     # => None
# The get method supports a default argument when the value is missing
filled_dict.get("one", 4)   # => 1
filled_dict.get("four", 4)  # => 4

# "setdefault()" inserts into a dictionary only if the given key isn't present
filled_dict.setdefault("five", 5)  # filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] is still 5

# Adding to a dictionary
filled_dict.update({"four":4})  # => {"one": 1, "two": 2, "three": 3, "four": 4}
filled_dict["four"] = 4         # another way to add to dict

# Remove keys from a dictionary with del
del filled_dict["one"]  # Removes the key "one" from filled dict

# From Python 3.5 you can also use the additional unpacking options
{'a': 1, **{'b': 2}}  # => {'a': 1, 'b': 2}
{'a': 1, **{'a': 2}}  # => {'a': 2}



# Sets store ... well sets
empty_set = set()
# Initialize a set with a bunch of values. Yeah, it looks a bit like a dict. Sorry.
some_set = {1, 1, 2, 2, 3, 4}  # some_set is now {1, 2, 3, 4}

# Similar to keys of a dictionary, elements of a set have to be immutable.
invalid_set = {[1], 1}  # => Raises a TypeError: unhashable type: 'list'
valid_set = {(1,), 1}

# Add one more item to the set
filled_set = some_set
filled_set.add(5)  # filled_set is now {1, 2, 3, 4, 5}

# Do set intersection with &
other_set = {3, 4, 5, 6}
filled_set & other_set  # => {3, 4, 5}

# Do set union with |
filled_set | other_set  # => {1, 2, 3, 4, 5, 6}

# Do set difference with -
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# Do set symmetric difference with ^
{1, 2, 3, 4} ^ {2, 3, 5}  # => {1, 4, 5}

# Check if set on the left is a superset of set on the right
{1, 2} >= {1, 2, 3} # => False

# Check if set on the left is a subset of set on the right
{1, 2} <= {1, 2, 3} # => True

# Check for existence in a set with in
2 in filled_set   # => True
10 in filled_set  # => False



####################################################
## 3. Control Flow and Iterables
####################################################

# Let's just make a variable
some_var = 5

# Here is an if statement. Indentation is significant in Python!
# Convention is to use four spaces, not tabs.
# This prints "some_var is smaller than 10"
if some_var > 10:
    print("some_var is totally bigger than 10.")
elif some_var < 10:    # This elif clause is optional.
    print("some_var is smaller than 10.")
else:                  # This is optional too.
    print("some_var is indeed 10.")


"""
For loops iterate over lists
prints:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # You can use format() to interpolate formatted strings
    print("{} is a mammal".format(animal))

"""
"range(number)" returns an iterable of numbers
from zero to the given number
prints:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
"range(lower, upper)" returns an iterable of numbers
from the lower number to the upper number
prints:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print(i)

"""
"range(lower, upper, step)" returns an iterable of numbers
from the lower number to the upper number, while incrementing
by step. If step is not indicated, the default value is 1.
prints:
    4
    6
"""
for i in range(4, 8, 2):
    print(i)
"""

While loops go until a condition is no longer met.
prints:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Shorthand for x = x + 1

# Handle exceptions with a try/except block
try:
    # Use "raise" to raise an error
    raise IndexError("This is an index error")
except IndexError as e:
    pass                 # Pass is just a no-op. Usually you would do recovery here.
except (TypeError, NameError):
    pass                 # Multiple exceptions can be handled together, if required.
else:                    # Optional clause to the try/except block. Must follow all except blocks
    print("All good!")   # Runs only if the code in try raises no exceptions
finally:                 #  Execute under all circumstances
    print("We can clean up resources here")

# Instead of try/finally to cleanup resources you can use a with statement
with open("myfile.txt") as f:
    for line in f:
        print(line)

# Python offers a fundamental abstraction called the Iterable.
# An iterable is an object that can be treated as a sequence.
# The object returned by the range function, is an iterable.

filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable)  # => dict_keys(['one', 'two', 'three']). This is an object that implements our Iterable interface.

# We can loop over it.
for i in our_iterable:
    print(i)  # Prints one, two, three

# However we cannot address elements by index.
our_iterable[1]  # Raises a TypeError

# An iterable is an object that knows how to create an iterator.
our_iterator = iter(our_iterable)

# Our iterator is an object that can remember the state as we traverse through it.
# We get the next object with "next()".
next(our_iterator)  # => "one"

# It maintains state as we iterate.
next(our_iterator)  # => "two"
next(our_iterator)  # => "three"

# After the iterator has returned all of its data, it raises a StopIteration exception
next(our_iterator)  # Raises StopIteration

# You can grab all the elements of an iterator by calling list() on it.
list(filled_dict.keys())  # => Returns ["one", "two", "three"]


####################################################
## 4. Functions
####################################################

# Use "def" to create new functions
def add(x, y):
    print("x is {} and y is {}".format(x, y))
    return x + y  # Return values with a return statement

# Calling functions with parameters
add(5, 6)  # => prints out "x is 5 and y is 6" and returns 11

# Another way to call functions is with keyword arguments
add(y=6, x=5)  # Keyword arguments can arrive in any order.

# You can define functions that take a variable number of
# positional arguments
def varargs(*args):
    return args

varargs(1, 2, 3)  # => (1, 2, 3)

# You can define functions that take a variable number of
# keyword arguments, as well
def keyword_args(**kwargs):
    return kwargs

# Let's call it to see what happens
keyword_args(big="foot", loch="ness")  # => {"big": "foot", "loch": "ness"}


# You can do both at once, if you like
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) prints:
    (1, 2)
    {"a": 3, "b": 4}
"""

# When calling functions, you can do the opposite of args/kwargs!
# Use * to expand tuples and use ** to expand kwargs.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)            # equivalent to foo(1, 2, 3, 4)
all_the_args(**kwargs)         # equivalent to foo(a=3, b=4)
all_the_args(*args, **kwargs)  # equivalent to foo(1, 2, 3, 4, a=3, b=4)

# Returning multiple values (with tuple assignments)
def swap(x, y):
    return y, x  # Return multiple values as a tuple without the parenthesis.
                 # (Note: parenthesis have been excluded but can be included)

x = 1
y = 2
x, y = swap(x, y)     # => x = 2, y = 1
# (x, y) = swap(x,y)  # Again parenthesis have been excluded but can be included.

# Function Scope
x = 5

def set_x(num):
    # Local var x not the same as global variable x
    x = num    # => 43
    print(x)   # => 43

def set_global_x(num):
    global x
    print(x)   # => 5
    x = num    # global var x is now set to 6
    print(x)   # => 6

set_x(43)
set_global_x(6)


# Python has first class functions
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)   # => 13

# There are also anonymous functions
(lambda x: x > 2)(3)                  # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1)  # => 5

# There are built-in higher order functions
list(map(add_10, [1, 2, 3]))          # => [11, 12, 13]
list(map(max, [1, 2, 3], [4, 2, 1]))  # => [4, 2, 3]

list(filter(lambda x: x > 5, [3, 4, 5, 6, 7]))  # => [6, 7]

# We can use list comprehensions for nice maps and filters
# List comprehension stores the output as a list which can itself be a nested list
[add_10(i) for i in [1, 2, 3]]         # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]  # => [6, 7]

# You can construct set and dict comprehensions as well.
{x for x in 'abcddeef' if x not in 'abc'}  # => {'d', 'e', 'f'}
{x: x**2 for x in range(5)}  # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


####################################################
## 5. Modules
####################################################

# You can import modules
import math
print(math.sqrt(16))  # => 4.0

# You can get specific functions from a module
from math import ceil, floor
print(ceil(3.7))   # => 4.0
print(floor(3.7))  # => 3.0

# You can import all functions from a module.
# Warning: this is not recommended
from math import *

# You can shorten module names
import math as m
math.sqrt(16) == m.sqrt(16)  # => True

# Python modules are just ordinary Python files. You
# can write your own, and import them. The name of the
# module is the same as the name of the file.

# You can find out which functions and attributes
# are defined in a module.
import math
dir(math)

# If you have a Python script named math.py in the same
# folder as your current script, the file math.py will
# be loaded instead of the built-in Python module.
# This happens because the local folder has priority
# over Python's built-in libraries.


####################################################
## 6. Classes
####################################################

# We use the "class" statement to create a class
class Human:

    # A class attribute. It is shared by all instances of this class
    species = "H. sapiens"

    # Basic initializer, this is called when this class is instantiated.
    # Note that the double leading and trailing underscores denote objects
    # or attributes that are used by Python but that live in user-controlled
    # namespaces. Methods(or objects or attributes) like: __init__, __str__,
    # __repr__ etc. are called special methods (or sometimes called dunder methods)
    # You should not invent such names on your own.
    def __init__(self, name):
        # Assign the argument to the instance's name attribute
        self.name = name

        # Initialize property
        self._age = 0

    # An instance method. All methods take "self" as the first argument
    def say(self, msg):
        print ("{name}: {message}".format(name=self.name, message=msg))

    # Another instance method
    def sing(self):
        return 'yo... yo... microphone check... one two... one two...'

    # A class method is shared among all instances
    # They are called with the calling class as the first argument
    @classmethod
    def get_species(cls):
        return cls.species

    # A static method is called without a class or instance reference
    @staticmethod
    def grunt():
        return "*grunt*"

    # A property is just like a getter.
    # It turns the method age() into an read-only attribute of the same name.
    # There's no need to write trivial getters and setters in Python, though.
    @property
    def age(self):
        return self._age

    # This allows the property to be set
    @age.setter
    def age(self, age):
        self._age = age

    # This allows the property to be deleted
    @age.deleter
    def age(self):
        del self._age


# When a Python interpreter reads a source file it executes all its code.
# This __name__ check makes sure this code block is only executed when this
# module is the main program.
if __name__ == '__main__':
    # Instantiate a class
    i = Human(name="Ian")
    i.say("hi")                     # "Ian: hi"
    j = Human("Joel")
    j.say("hello")                  # "Joel: hello"
    # i and j are instances of type Human, or in other words: they are Human objects

    # Call our class method
    i.say(i.get_species())          # "Ian: H. sapiens"
    # Change the shared attribute
    Human.species = "H. neanderthalensis"
    i.say(i.get_species())          # => "Ian: H. neanderthalensis"
    j.say(j.get_species())          # => "Joel: H. neanderthalensis"

    # Call the static method
    print(Human.grunt())            # => "*grunt*"
    
    # Cannot call static method with instance of object 
    # because i.grunt() will automatically put "self" (the object i) as an argument
    print(i.grunt())                # => TypeError: grunt() takes 0 positional arguments but 1 was given
                                    
    # Update the property for this instance
    i.age = 42
    # Get the property
    i.say(i.age)                    # => "Ian: 42"
    j.say(j.age)                    # => "Joel: 0"
    # Delete the property
    del i.age
    # i.age                         # => this would raise an AttributeError


####################################################
## 6.1 Multiple Inheritance
####################################################

# Another class definition
class Bat:

    species = 'Baty'

    def __init__(self, can_fly=True):
        self.fly = can_fly

    # This class also has a say method
    def say(self, msg):
        msg = '... ... ...'
        return msg

    # And its own method as well
    def sonar(self):
        return '))) ... ((('

if __name__ == '__main__':
    b = Bat()
    print(b.say('hello'))
    print(b.fly)

# To take advantage of modularization by file you could place the classes above in their own files,
# say, human.py and bat.py

# To import functions from other files use the following format
# from "filename-without-extension" import "function-or-class"

# superhero.py
from human import Human
from bat import Bat

# Batman inherits from both Human and Bat
class Batman(Human, Bat):

    # Batman has its own value for the species class attribute
    species = 'Superhero'

    def __init__(self, *args, **kwargs):
        # Typically to inherit attributes you have to call super:
        #super(Batman, self).__init__(*args, **kwargs)      
        # However we are dealing with multiple inheritance here, and super()
        # only works with the next base class in the MRO list.
        # So instead we explicitly call __init__ for all ancestors.
        # The use of *args and **kwargs allows for a clean way to pass arguments,
        # with each parent "peeling a layer of the onion".
        Human.__init__(self, 'anonymous', *args, **kwargs)
        Bat.__init__(self, *args, can_fly=False, **kwargs)
        # override the value for the name attribute
        self.name = 'Sad Affleck'

    def sing(self):
        return 'nan nan nan nan nan batman!'


if __name__ == '__main__':
    sup = Batman()

    # Instance type checks
    if isinstance(sup, Human):
        print('I am human')
    if isinstance(sup, Bat):
        print('I am bat')
    if type(sup) is Batman:
        print('I am Batman')

    # Get the Method Resolution search Order used by both getattr() and super().
    # This attribute is dynamic and can be updated
    print(Batman.__mro__)       # => (<class '__main__.Batman'>, <class 'human.Human'>, <class 'bat.Bat'>, <class 'object'>)

    # Calls parent method but uses its own class attribute
    print(sup.get_species())    # => Superhero

    # Calls overloaded method
    print(sup.sing())           # => nan nan nan nan nan batman!

    # Calls method from Human, because inheritance order matters
    sup.say('I agree')          # => Sad Affleck: I agree

    # Call method that exists only in 2nd ancestor
    print(sup.sonar())          # => ))) ... (((

    # Inherited class attribute
    sup.age = 100
    print(sup.age)

    # Inherited attribute from 2nd ancestor whose default value was overridden.
    print('Can I fly? ' + str(sup.fly))



####################################################
## 7. Advanced
####################################################

# Generators help you make lazy code.
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# Generators are memory-efficient because they only load the data needed to
# process the next value in the iterable. This allows them to perform
# operations on otherwise prohibitively large value ranges.
# NOTE: `range` replaces `xrange` in Python 3.
for i in double_numbers(range(1, 900000000)):  # `range` is a generator.
    print(i)
    if i >= 30:
        break

# Just as you can create a list comprehension, you can create generator
# comprehensions as well.
values = (-x for x in [1,2,3,4,5])
for x in values:
    print(x)  # prints -1 -2 -3 -4 -5 to console/terminal

# You can also cast a generator comprehension directly to a list.
values = (-x for x in [1,2,3,4,5])
gen_to_list = list(values)
print(gen_to_list)  # => [-1, -2, -3, -4, -5]


# Decorators
# In this example `beg` wraps `say`. If say_please is True then it
# will change the returned message.
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Please! I am poor :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Can you buy me a beer?"
    return msg, say_please


print(say())                 # Can you buy me a beer?
print(say(say_please=True))  # Can you buy me a beer? Please! I am poor :(
```

## Ready For More?

### Free Online

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Course](http://www.python-course.eu/index.php)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)
* [A curated list of awesome Python frameworks, libraries and software](https://github.com/vinta/awesome-python)
* [30 Python Language Features and Tricks You May Not Know About](http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html)
* [Official Style Guide for Python](https://www.python.org/dev/peps/pep-0008/)
* [Python 3 Computer Science Circles](http://cscircles.cemc.uwaterloo.ca/)
* [Dive Into Python 3](http://www.diveintopython3.net/index.html)
* [A Crash Course in Python for Scientists](http://nbviewer.jupyter.org/gist/anonymous/5924718)
---
category: tool
tool: Statistical Computing with Python
contributors:
    - ["e99n09", "https://github.com/e99n09"]
---

This is a tutorial on how to do some typical statistical programming tasks using Python. It's intended for people basically familiar with Python and experienced at statistical programming in a language like R, Stata, SAS, SPSS, or MATLAB.

```python



# 0. Getting set up ====

""" Get set up with IPython and pip install the following: numpy, scipy, pandas,
    matplotlib, seaborn, requests.
        Make sure to do this tutorial in the IPython notebook so that you get
    the inline plots and easy documentation lookup.
"""

# 1. Data acquisition ====

""" One reason people choose Python over R is that they intend to interact a lot
    with the web, either by scraping pages directly or requesting data through
    an API. You can do those things in R, but in the context of a project
    already using Python, there's a benefit to sticking with one language.
"""

import requests  # for HTTP requests (web scraping, APIs)
import os

# web scraping
r = requests.get("https://github.com/adambard/learnxinyminutes-docs")
r.status_code  # if 200, request was successful
r.text  # raw page source
print(r.text)  # prettily formatted
# save the page source in a file:
os.getcwd()  # check what's the working directory
f = open("learnxinyminutes.html", "wb")
f.write(r.text.encode("UTF-8"))
f.close()

# downloading a csv
fp = "https://raw.githubusercontent.com/adambard/learnxinyminutes-docs/master/"
fn = "pets.csv"
r = requests.get(fp + fn)
print(r.text)
f = open(fn, "wb")
f.write(r.text.encode("UTF-8"))
f.close()

""" for more on the requests module, including APIs, see
    http://docs.python-requests.org/en/latest/user/quickstart/
"""

# 2. Reading a CSV file ====

""" Wes McKinney's pandas package gives you 'DataFrame' objects in Python. If
    you've used R, you will be familiar with the idea of the "data.frame" already.
"""

import pandas as pd
import numpy as np
import scipy as sp
pets = pd.read_csv(fn)
pets
#        name  age  weight species
# 0    fluffy    3      14     cat
# 1  vesuvius    6      23    fish
# 2       rex    5      34     dog

""" R users: note that Python, like most normal programming languages, starts
    indexing from 0. R is the unusual one for starting from 1.
"""

# two different ways to print out a column
pets.age
pets["age"]

pets.head(2)  # prints first 2 rows
pets.tail(1)  # prints last row

pets.name[1]  # 'vesuvius'
pets.species[0]  # 'cat'
pets["weight"][2]  # 34

# in R, you would expect to get 3 rows doing this, but here you get 2:
pets.age[0:2]
# 0    3
# 1    6

sum(pets.age) * 2  # 28
max(pets.weight) - min(pets.weight)  # 20

""" If you are doing some serious linear algebra and number-crunching, you may
    just want arrays, not DataFrames. DataFrames are ideal for combining columns
    of different types.
"""

# 3. Charts ====

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline

# To do data visualization in Python, use matplotlib

plt.hist(pets.age);

plt.boxplot(pets.weight);

plt.scatter(pets.age, pets.weight)
plt.xlabel("age")
plt.ylabel("weight");

# seaborn sits atop matplotlib and makes plots prettier

import seaborn as sns

plt.scatter(pets.age, pets.weight)
plt.xlabel("age")
plt.ylabel("weight");

# there are also some seaborn-specific plotting functions
# notice how seaborn automatically labels the x-axis on this barplot
sns.barplot(pets["age"])

# R veterans can still use ggplot
from ggplot import *
ggplot(aes(x="age",y="weight"), data=pets) + geom_point() + labs(title="pets")
# source: https://pypi.python.org/pypi/ggplot

# there's even a d3.js port: https://github.com/mikedewar/d3py

# 4. Simple data cleaning and exploratory analysis ====

""" Here's a more complicated example that demonstrates a basic data
    cleaning workflow leading to the creation of some exploratory plots
    and the running of a linear regression.
        The data set was transcribed from Wikipedia by hand. It contains
    all the Holy Roman Emperors and the important milestones in their lives
    (birth, death, coronation, etc.).
        The goal of the analysis will be to explore whether a relationship
    exists between emperor birth year and emperor lifespan.
    data source: https://en.wikipedia.org/wiki/Holy_Roman_Emperor
"""

# load some data on Holy Roman Emperors
url = "https://raw.githubusercontent.com/e99n09/R-notes/master/data/hre.csv"
r = requests.get(url)
fp = "hre.csv"
with open(fp, "wb") as f:
    f.write(r.text.encode("UTF-8"))

hre = pd.read_csv(fp)

hre.head()
"""
   Ix      Dynasty        Name        Birth             Death Election 1
0 NaN  Carolingian   Charles I  2 April 742    28 January 814        NaN
1 NaN  Carolingian     Louis I          778       20 June 840        NaN
2 NaN  Carolingian   Lothair I          795  29 September 855        NaN
3 NaN  Carolingian    Louis II          825     12 August 875        NaN
4 NaN  Carolingian  Charles II  13 June 823     6 October 877        NaN

  Election 2      Coronation 1   Coronation 2 Ceased to be Emperor
0        NaN   25 December 800            NaN       28 January 814
1        NaN  11 September 813  5 October 816          20 June 840
2        NaN       5 April 823            NaN     29 September 855
3        NaN        Easter 850     18 May 872        12 August 875
4        NaN   29 December 875            NaN        6 October 877

  Descent from whom 1 Descent how 1 Descent from whom 2 Descent how 2
0                 NaN           NaN                 NaN           NaN
1           Charles I           son                 NaN           NaN
2             Louis I           son                 NaN           NaN
3           Lothair I           son                 NaN           NaN
4             Louis I           son                 NaN           NaN
"""

# clean the Birth and Death columns

import re  # module for regular expressions

rx = re.compile(r'\d+$')  # match trailing digits

""" This function applies the regular expression to an input column (here Birth,
    Death), flattens the resulting list, converts it to a Series object, and
    finally converts the type of the Series object from string to integer. For
    more information into what different parts of the code do, see:
      - https://docs.python.org/2/howto/regex.html
      - http://stackoverflow.com/questions/11860476/how-to-unlist-a-python-list
      - http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html
"""

def extractYear(v):
    return(pd.Series(reduce(lambda x, y: x + y, map(rx.findall, v), [])).astype(int))

hre["BirthY"] = extractYear(hre.Birth)
hre["DeathY"] = extractYear(hre.Death)

# make a column telling estimated age
hre["EstAge"] = hre.DeathY.astype(int) - hre.BirthY.astype(int)

# simple scatterplot, no trend line, color represents dynasty
sns.lmplot("BirthY", "EstAge", data=hre, hue="Dynasty", fit_reg=False);

# use scipy to run a linear regression
from scipy import stats
(slope, intercept, rval, pval, stderr) = stats.linregress(hre.BirthY, hre.EstAge)
# code source: http://wiki.scipy.org/Cookbook/LinearRegression

# check the slope
slope  # 0.0057672618839073328

# check the R^2 value:
rval**2  # 0.020363950027333586

# check the p-value
pval  # 0.34971812581498452

# use seaborn to make a scatterplot and plot the linear regression trend line
sns.lmplot("BirthY", "EstAge", data=hre);

""" For more information on seaborn, see
      - http://web.stanford.edu/~mwaskom/software/seaborn/
      - https://github.com/mwaskom/seaborn
    For more information on SciPy, see
      - http://wiki.scipy.org/SciPy
      - http://wiki.scipy.org/Cookbook/
    To see a version of the Holy Roman Emperors analysis using R, see
      - http://github.com/e99n09/R-notes/blob/master/holy_roman_emperors_dates.R
"""

```

If you want to learn more, get _Python for Data Analysis_ by Wes McKinney. It's a superb resource and I used it as a reference when writing this tutorial.

You can also find plenty of interactive IPython tutorials on subjects specific to your interests, like Cam Davidson-Pilon's <a href="http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/" Title="Probabilistic Programming and Bayesian Methods for Hackers">Probabilistic Programming and Bayesian Methods for Hackers</a>.

Some more modules to research:
   - text analysis and natural language processing: nltk, http://www.nltk.org
   - social network analysis: igraph, http://igraph.org/python/
---
category: tool
tool: Qt Framework
language: c++
filename: learnqt.cpp
contributors:
    - ["Aleksey Kholovchuk", "https://github.com/vortexxx192"]
    
---

**Qt** is a widely-known framework for developing cross-platform software that can be run on various software and hardware platforms with little or no change in the code, while having the power and speed of native applications. Though **Qt** was originally written in *C++*, there are its ports to other languages: *[PyQt](https://learnxinyminutes.com/docs/pyqt/)*, *QtRuby*, *PHP-Qt*, etc.

**Qt** is great for creating applications with graphical user interface (GUI). This tutorial is how to do it in *C++*.

```c++
/*
 * Let's start classically
 */

// all headers from Qt framework start with capital letter 'Q'
#include <QApplication>
#include <QLineEdit>

int main(int argc, char *argv[]) {
	 // create an object to manage application-wide resources
    QApplication app(argc, argv);

    // create line edit widget and show it on screen
    QLineEdit lineEdit("Hello world!");
    lineEdit.show();

    // start the application's event loop
    return app.exec();
}
```

GUI-related part of **Qt** is all about *widgets* and *connections* between them.

[READ MORE ABOUT WIDGETS](http://doc.qt.io/qt-5/qtwidgets-index.html)

```c++
/*
 * Let's create a label and a button.
 * A label should appear when a button is pressed.
 * 
 * Qt code is speaking for itself.
 */
 
#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QDialog dialogWindow;
    dialogWindow.show();
    
    // add vertical layout 
    QVBoxLayout layout;
    dialogWindow.setLayout(&layout);  

    QLabel textLabel("Thanks for pressing that button");
    layout.addWidget(&textLabel);
    textLabel.hide();

    QPushButton button("Press me");
    layout.addWidget(&button);
    
    // show hidden label when the button is pressed
    QObject::connect(&button, &QPushButton::pressed,
                     &textLabel, &QLabel::show);

    return app.exec();
}
```

Notice that *QObject::connect* part. This method is used to connect *SIGNALS* of one objects to *SLOTS* of another.

**Signals** are being emitted when certain things happen with objects, like *pressed* signal is emitted when user presses on QPushButton object.

**Slots** are *actions* that might be performed in response to received signals.

[READ MORE ABOUT SLOTS AND SIGNALS](http://doc.qt.io/qt-5/signalsandslots.html)


Next, let's learn that we can not only use standard widgets but also extend their behaviour using inheritance. Let's create a button and count how many times it was pressed. For this purpose we define our own class *CounterLabel*.  It must be declared in separate file because of specific Qt architecture.

```c++
// counterlabel.hpp

#ifndef COUNTERLABEL
#define COUNTERLABEL

#include <QLabel>

class CounterLabel : public QLabel {
    Q_OBJECT  // Qt-defined macros that must be present in every custom widget

public:
    CounterLabel() : counter(0) {
        setText("Counter has not been increased yet");  // method of QLabel
    }

public slots:
    // action that will be called in response to button press
    void increaseCounter() {
        setText(QString("Counter value: %1").arg(QString::number(++counter)));
    }

private:
    int counter;
};

#endif // COUNTERLABEL
```

```c++
// main.cpp
// Almost the same as in previous example

#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QString>
#include "counterlabel.hpp"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QDialog dialogWindow;
    dialogWindow.show();

    QVBoxLayout layout;
    dialogWindow.setLayout(&layout);

    CounterLabel counterLabel;
    layout.addWidget(&counterLabel);

    QPushButton button("Push me once more");
    layout.addWidget(&button);
    QObject::connect(&button, &QPushButton::pressed,
                     &counterLabel, &CounterLabel::increaseCounter);

    return app.exec();
}
```

That's it! Of course, Qt framework is much much larger than the part that was covered in this tutorial, so be ready to read and practice.

## Further reading

- [Qt 4.8 tutorials](http://doc.qt.io/qt-4.8/tutorials.html)
- [Qt 5 tutorials](http://doc.qt.io/qt-5/qtexamplesandtutorials.html)

Good luck and have fun!
---
language: R
contributors:
    - ["e99n09", "http://github.com/e99n09"]
    - ["isomorphismes", "http://twitter.com/isomorphisms"]
    - ["kalinn", "http://github.com/kalinn"]
filename: learnr.r
---

R is a statistical computing language. It has lots of libraries for uploading and cleaning data sets, running statistical procedures, and making graphs. You can also run `R` commands within a LaTeX document.

```r

# Comments start with number symbols.

# You can't make multi-line comments,
# but you can stack multiple comments like so.

# in Windows you can use CTRL-ENTER to execute a line.
# on Mac it is COMMAND-ENTER



#############################################################################
# Stuff you can do without understanding anything about programming
#############################################################################

# In this section, we show off some of the cool stuff you can do in
# R without understanding anything about programming. Do not worry
# about understanding everything the code does. Just enjoy!

data()	        # browse pre-loaded data sets
data(rivers)	# get this one: "Lengths of Major North American Rivers"
ls()	        # notice that "rivers" now appears in the workspace
head(rivers)	# peek at the data set
# 735 320 325 392 524 450

length(rivers)	# how many rivers were measured?
# 141
summary(rivers) # what are some summary statistics?
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#  135.0   310.0   425.0   591.2   680.0  3710.0

# make a stem-and-leaf plot (a histogram-like data visualization)
stem(rivers)

#  The decimal point is 2 digit(s) to the right of the |
#
#   0 | 4
#   2 | 011223334555566667778888899900001111223333344455555666688888999
#   4 | 111222333445566779001233344567
#   6 | 000112233578012234468
#   8 | 045790018
#  10 | 04507
#  12 | 1471
#  14 | 56
#  16 | 7
#  18 | 9
#  20 |
#  22 | 25
#  24 | 3
#  26 |
#  28 |
#  30 |
#  32 |
#  34 |
#  36 | 1

stem(log(rivers)) # Notice that the data are neither normal nor log-normal!
# Take that, Bell curve fundamentalists.

#  The decimal point is 1 digit(s) to the left of the |
#
#  48 | 1
#  50 |
#  52 | 15578
#  54 | 44571222466689
#  56 | 023334677000124455789
#  58 | 00122366666999933445777
#  60 | 122445567800133459
#  62 | 112666799035
#  64 | 00011334581257889
#  66 | 003683579
#  68 | 0019156
#  70 | 079357
#  72 | 89
#  74 | 84
#  76 | 56
#  78 | 4
#  80 |
#  82 | 2

# make a histogram:
hist(rivers, col="#333333", border="white", breaks=25) # play around with these parameters
hist(log(rivers), col="#333333", border="white", breaks=25) # you'll do more plotting later

# Here's another neat data set that comes pre-loaded. R has tons of these.
data(discoveries)
plot(discoveries, col="#333333", lwd=3, xlab="Year",
     main="Number of important discoveries per year")
plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year",
     main="Number of important discoveries per year")

# Rather than leaving the default ordering (by year),
# we could also sort to see what's typical:
sort(discoveries)
#  [1]  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2
# [26]  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3
# [51]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  4  4  4  4  4  4  4
# [76]  4  4  4  4  5  5  5  5  5  5  5  6  6  6  6  6  6  7  7  7  7  8  9 10 12

stem(discoveries, scale=2)
#
#  The decimal point is at the |
#
#   0 | 000000000
#   1 | 000000000000
#   2 | 00000000000000000000000000
#   3 | 00000000000000000000
#   4 | 000000000000
#   5 | 0000000
#   6 | 000000
#   7 | 0000
#   8 | 0
#   9 | 0
#  10 | 0
#  11 |
#  12 | 0

max(discoveries)
# 12
summary(discoveries)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#    0.0     2.0     3.0     3.1     4.0    12.0

# Roll a die a few times
round(runif(7, min=.5, max=6.5))
# 1 4 6 1 4 6 4
# Your numbers will differ from mine unless we set the same random.seed(31337)

# Draw from a standard Gaussian 9 times
rnorm(9)
# [1]  0.07528471  1.03499859  1.34809556 -0.82356087  0.61638975 -1.88757271
# [7] -0.59975593  0.57629164  1.08455362



##################################################
# Data types and basic arithmetic
##################################################

# Now for the programming-oriented part of the tutorial.
# In this section you will meet the important data types of R:
# integers, numerics, characters, logicals, and factors.
# There are others, but these are the bare minimum you need to
# get started.

# INTEGERS
# Long-storage integers are written with L
5L # 5
class(5L) # "integer"
# (Try ?class for more information on the class() function.)
# In R, every single value, like 5L, is considered a vector of length 1
length(5L) # 1
# You can have an integer vector with length > 1 too:
c(4L, 5L, 8L, 3L) # 4 5 8 3
length(c(4L, 5L, 8L, 3L)) # 4
class(c(4L, 5L, 8L, 3L)) # "integer"

# NUMERICS
# A "numeric" is a double-precision floating-point number
5 # 5
class(5) # "numeric"
# Again, everything in R is a vector;
# you can make a numeric vector with more than one element
c(3,3,3,2,2,1) # 3 3 3 2 2 1
# You can use scientific notation too
5e4 # 50000
6.02e23 # Avogadro's number
1.6e-35 # Planck length
# You can also have infinitely large or small numbers
class(Inf)	# "numeric"
class(-Inf)	# "numeric"
# You might use "Inf", for example, in integrate(dnorm, 3, Inf);
# this obviates Z-score tables.

# BASIC ARITHMETIC
# You can do arithmetic with numbers
# Doing arithmetic on a mix of integers and numerics gives you another numeric
10L + 66L # 76      # integer plus integer gives integer
53.2 - 4  # 49.2    # numeric minus numeric gives numeric
2.0 * 2L  # 4       # numeric times integer gives numeric
3L / 4    # 0.75    # integer over numeric gives numeric
3 %% 2	  # 1       # the remainder of two numerics is another numeric
# Illegal arithmetic yields you a "not-a-number":
0 / 0 # NaN
class(NaN) # "numeric"
# You can do arithmetic on two vectors with length greater than 1,
# so long as the larger vector's length is an integer multiple of the smaller
c(1,2,3) + c(1,2,3) # 2 4 6
# Since a single number is a vector of length one, scalars are applied 
# elementwise to vectors
(4 * c(1,2,3) - 2) / 2 # 1 3 5
# Except for scalars, use caution when performing arithmetic on vectors with 
# different lengths. Although it can be done, 
c(1,2,3,1,2,3) * c(1,2) # 1 4 3 2 2 6
# Matching lengths is better practice and easier to read
c(1,2,3,1,2,3) * c(1,2,1,2,1,2) 

# CHARACTERS
# There's no difference between strings and characters in R
"Horatio" # "Horatio"
class("Horatio") # "character"
class('H') # "character"
# Those were both character vectors of length 1
# Here is a longer one:
c('alef', 'bet', 'gimmel', 'dalet', 'he')
# =>
# "alef"   "bet"    "gimmel" "dalet"  "he"
length(c("Call","me","Ishmael")) # 3
# You can do regex operations on character vectors:
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
# R has several built-in character vectors:
letters
# =>
#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [20] "t" "u" "v" "w" "x" "y" "z"
month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

# LOGICALS
# In R, a "logical" is a boolean
class(TRUE)	# "logical"
class(FALSE)	# "logical"
# Their behavior is normal
TRUE == TRUE	# TRUE
TRUE == FALSE	# FALSE
FALSE != FALSE	# FALSE
FALSE != TRUE	# TRUE
# Missing data (NA) is logical, too
class(NA)	# "logical"
# Use | and & for logic operations.
# OR
TRUE | FALSE	# TRUE
# AND
TRUE & FALSE	# FALSE
# Applying | and & to vectors returns elementwise logic operations
c(TRUE,FALSE,FALSE) | c(FALSE,TRUE,FALSE) # TRUE TRUE FALSE
c(TRUE,FALSE,TRUE) & c(FALSE,TRUE,TRUE) # FALSE FALSE TRUE
# You can test if x is TRUE
isTRUE(TRUE)	# TRUE
# Here we get a logical vector with many elements:
c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE
c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE

# FACTORS
# The factor class is for categorical data
# Factors can be ordered (like childrens' grade levels) or unordered (like gender)
factor(c("female", "female", "male", NA, "female"))
#  female female male   <NA>   female
# Levels: female male
# The "levels" are the values the categorical data can take
# Note that missing data does not enter the levels
levels(factor(c("male", "male", "female", NA, "female"))) # "female" "male"
# If a factor vector has length 1, its levels will have length 1, too
length(factor("male")) # 1
length(levels(factor("male"))) # 1
# Factors are commonly seen in data frames, a data structure we will cover later
data(infert) # "Infertility after Spontaneous and Induced Abortion"
levels(infert$education) # "0-5yrs"  "6-11yrs" "12+ yrs"

# NULL
# "NULL" is a weird one; use it to "blank out" a vector
class(NULL)	# NULL
parakeet = c("beak", "feathers", "wings", "eyes")
parakeet
# =>
# [1] "beak"     "feathers" "wings"    "eyes"
parakeet <- NULL
parakeet
# =>
# NULL

# TYPE COERCION
# Type-coercion is when you force a value to take on a different type
as.character(c(6, 8)) # "6" "8"
as.logical(c(1,0,1,1)) # TRUE FALSE  TRUE  TRUE
# If you put elements of different types into a vector, weird coercions happen:
c(TRUE, 4) # 1 4
c("dog", TRUE, 4) # "dog"  "TRUE" "4"
as.numeric("Bilbo")
# =>
# [1] NA
# Warning message:
# NAs introduced by coercion

# Also note: those were just the basic data types
# There are many more data types, such as for dates, time series, etc.



##################################################
# Variables, loops, if/else
##################################################

# A variable is like a box you store a value in for later use.
# We call this "assigning" the value to the variable.
# Having variables lets us write loops, functions, and if/else statements

# VARIABLES
# Lots of way to assign stuff:
x = 5 # this is possible
y <- "1" # this is preferred
TRUE -> z # this works but is weird

# LOOPS
# We've got for loops
for (i in 1:4) {
  print(i)
}
# We've got while loops
a <- 10
while (a > 4) {
	cat(a, "...", sep = "")
	a <- a - 1
}
# Keep in mind that for and while loops run slowly in R
# Operations on entire vectors (i.e. a whole row, a whole column)
# or apply()-type functions (we'll discuss later) are preferred

# IF/ELSE
# Again, pretty standard
if (4 > 3) {
	print("4 is greater than 3")
} else {
	print("4 is not greater than 3")
}
# =>
# [1] "4 is greater than 3"

# FUNCTIONS
# Defined like so:
jiggle <- function(x) {
	x = x + rnorm(1, sd=.1)	#add in a bit of (controlled) noise
	return(x)
}
# Called like any other R function:
jiggle(5)	# 5±ε. After set.seed(2716057), jiggle(5)==5.005043



###########################################################################
# Data structures: Vectors, matrices, data frames, and arrays
###########################################################################

# ONE-DIMENSIONAL

# Let's start from the very beginning, and with something you already know: vectors.
vec <- c(8, 9, 10, 11)
vec	#  8  9 10 11
# We ask for specific elements by subsetting with square brackets
# (Note that R starts counting from 1)
vec[1]		# 8
letters[18]	# "r"
LETTERS[13]	# "M"
month.name[9]	# "September"
c(6, 8, 7, 5, 3, 0, 9)[3]	# 7
# We can also search for the indices of specific components,
which(vec %% 2 == 0)	# 1 3
# grab just the first or last few entries in the vector,
head(vec, 1)	# 8
tail(vec, 2)	# 10 11
# or figure out if a certain value is in the vector
any(vec == 10) # TRUE
# If an index "goes over" you'll get NA:
vec[6]	# NA
# You can find the length of your vector with length()
length(vec)	# 4
# You can perform operations on entire vectors or subsets of vectors
vec * 4	# 16 20 24 28
vec[2:3] * 5	# 25 30
any(vec[2:3] == 8) # FALSE
# and R has many built-in functions to summarize vectors
mean(vec)	# 9.5
var(vec)	# 1.666667
sd(vec)		# 1.290994
max(vec)	# 11
min(vec)	# 8
sum(vec)	# 38
# Some more nice built-ins:
5:15	# 5  6  7  8  9 10 11 12 13 14 15
seq(from=0, to=31337, by=1337)
# =>
#  [1]     0  1337  2674  4011  5348  6685  8022  9359 10696 12033 13370 14707
# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751

# TWO-DIMENSIONAL (ALL ONE CLASS)

# You can make a matrix out of entries all of the same type like so:
mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# Unlike a vector, the class of a matrix is "matrix", no matter what's in it
class(mat) # => "matrix"
# Ask for the first row
mat[1,]	# 1 4
# Perform operation on the first column
3 * mat[,1]	# 3 6 9
# Ask for a specific cell
mat[3,2]	# 6

# Transpose the whole matrix
t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6

# Matrix multiplication
mat %*% t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]   17   22   27
# [2,]   22   29   36
# [3,]   27   36   45

# cbind() sticks vectors together column-wise to make a matrix
mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
mat2
# =>
#      [,1] [,2]
# [1,] "1"  "dog"
# [2,] "2"  "cat"
# [3,] "3"  "bird"
# [4,] "4"  "dog"
class(mat2)	# matrix
# Again, note what happened!
# Because matrices must contain entries all of the same class,
# everything got converted to the character class
c(class(mat2[,1]), class(mat2[,2]))

# rbind() sticks vectors together row-wise to make a matrix
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    4    5
# [2,]    6    7    0    4
# Ah, everything of the same class. No coercions. Much better.

# TWO-DIMENSIONAL (DIFFERENT CLASSES)

# For columns of different types, use a data frame
# This data structure is so useful for statistical programming,
# a version of it was added to Python in the package "pandas".

students <- data.frame(c("Cedric","Fred","George","Cho","Draco","Ginny"),
                       c(3,2,2,1,0,-1),
                       c("H", "G", "G", "R", "S", "G"))
names(students) <- c("name", "year", "house") # name the columns
class(students)	# "data.frame"
students
# =>
#     name year house
# 1 Cedric    3     H
# 2   Fred    2     G
# 3 George    2     G
# 4    Cho    1     R
# 5  Draco    0     S
# 6  Ginny   -1     G
class(students$year)	# "numeric"
class(students[,3])	# "factor"
# find the dimensions
nrow(students)	# 6
ncol(students)	# 3
dim(students)	# 6 3
# The data.frame() function converts character vectors to factor vectors
# by default; turn this off by setting stringsAsFactors = FALSE when
# you create the data.frame
?data.frame

# There are many twisty ways to subset data frames, all subtly unalike
students$year	# 3  2  2  1  0 -1
students[,2]	# 3  2  2  1  0 -1
students[,"year"]	# 3  2  2  1  0 -1

# An augmented version of the data.frame structure is the data.table
# If you're working with huge or panel data, or need to merge a few data
# sets, data.table can be a good choice. Here's a whirlwind tour:
install.packages("data.table") # download the package from CRAN
require(data.table) # load it
students <- as.data.table(students)
students # note the slightly different print-out
# =>
#      name year house
# 1: Cedric    3     H
# 2:   Fred    2     G
# 3: George    2     G
# 4:    Cho    1     R
# 5:  Draco    0     S
# 6:  Ginny   -1     G
students[name=="Ginny"] # get rows with name == "Ginny"
# =>
#     name year house
# 1: Ginny   -1     G
students[year==2] # get rows with year == 2
# =>
#      name year house
# 1:   Fred    2     G
# 2: George    2     G
# data.table makes merging two data sets easy
# let's make another data.table to merge with students
founders <- data.table(house=c("G","H","R","S"),
                       founder=c("Godric","Helga","Rowena","Salazar"))
founders
# =>
#    house founder
# 1:     G  Godric
# 2:     H   Helga
# 3:     R  Rowena
# 4:     S Salazar
setkey(students, house)
setkey(founders, house)
students <- founders[students] # merge the two data sets by matching "house"
setnames(students, c("house","houseFounderName","studentName","year"))
students[,order(c("name","year","house","houseFounderName")), with=F]
# =>
#    studentName year house houseFounderName
# 1:        Fred    2     G           Godric
# 2:      George    2     G           Godric
# 3:       Ginny   -1     G           Godric
# 4:      Cedric    3     H            Helga
# 5:         Cho    1     R           Rowena
# 6:       Draco    0     S          Salazar

# data.table makes summary tables easy
students[,sum(year),by=house]
# =>
#    house V1
# 1:     G  3
# 2:     H  3
# 3:     R  1
# 4:     S  0

# To drop a column from a data.frame or data.table,
# assign it the NULL value
students$houseFounderName <- NULL
students
# =>
#    studentName year house
# 1:        Fred    2     G
# 2:      George    2     G
# 3:       Ginny   -1     G
# 4:      Cedric    3     H
# 5:         Cho    1     R
# 6:       Draco    0     S

# Drop a row by subsetting
# Using data.table:
students[studentName != "Draco"]
# =>
#    house studentName year
# 1:     G        Fred    2
# 2:     G      George    2
# 3:     G       Ginny   -1
# 4:     H      Cedric    3
# 5:     R         Cho    1
# Using data.frame:
students <- as.data.frame(students)
students[students$house != "G",]
# =>
#   house houseFounderName studentName year
# 4     H            Helga      Cedric    3
# 5     R           Rowena         Cho    1
# 6     S          Salazar       Draco    0

# MULTI-DIMENSIONAL (ALL ELEMENTS OF ONE TYPE)

# Arrays creates n-dimensional tables
# All elements must be of the same type
# You can make a two-dimensional table (sort of like a matrix)
array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4))
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    8    3
# [2,]    2    5    9    6
# You can use array to make three-dimensional matrices too
array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2))
# =>
# , , 1
#
#      [,1] [,2]
# [1,]    2    8
# [2,]  300    9
# [3,]    4    0
#
# , , 2
#
#      [,1] [,2]
# [1,]    5   66
# [2,]   60    7
# [3,]    0  847

# LISTS (MULTI-DIMENSIONAL, POSSIBLY RAGGED, OF DIFFERENT TYPES)

# Finally, R has lists (of vectors)
list1 <- list(time = 1:40)
list1$price = c(rnorm(40,.5*list1$time,4)) # random
list1
# You can get items in the list like so
list1$time # one way
list1[["time"]] # another way
list1[[1]] # yet another way
# =>
#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# [34] 34 35 36 37 38 39 40
# You can subset list items like any other vector
list1$price[4]

# Lists are not the most efficient data structure to work with in R;
# unless you have a very good reason, you should stick to data.frames
# Lists are often returned by functions that perform linear regressions

##################################################
# The apply() family of functions
##################################################

# Remember mat?
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# Use apply(X, MARGIN, FUN) to apply function FUN to a matrix X
# over rows (MAR = 1) or columns (MAR = 2)
# That is, R does FUN to each row (or column) of X, much faster than a
# for or while loop would do
apply(mat, MAR = 2, jiggle)
# =>
#      [,1] [,2]
# [1,]    3   15
# [2,]    7   19
# [3,]   11   23
# Other functions: ?lapply, ?sapply

# Don't feel too intimidated; everyone agrees they are rather confusing

# The plyr package aims to replace (and improve upon!) the *apply() family.
install.packages("plyr")
require(plyr)
?plyr



#########################
# Loading data
#########################

# "pets.csv" is a file on the internet
# (but it could just as easily be a file on your own computer)
pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv")
pets
head(pets, 2) # first two rows
tail(pets, 1) # last row

# To save a data frame or matrix as a .csv file
write.csv(pets, "pets2.csv") # to make a new .csv file
# set working directory with setwd(), look it up with getwd()

# Try ?read.csv and ?write.csv for more information



#########################
# Statistical Analysis
#########################

# Linear regression!
linearModel <- lm(price  ~ time, data = list1)
linearModel # outputs result of regression
# =>
# Call:
# lm(formula = price ~ time, data = list1)
# 
# Coefficients:
# (Intercept)         time  
#      0.1453       0.4943  
summary(linearModel) # more verbose output from the regression
# =>
# Call:
# lm(formula = price ~ time, data = list1)
#
# Residuals:
#     Min      1Q  Median      3Q     Max 
# -8.3134 -3.0131 -0.3606  2.8016 10.3992 
#
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept)  0.14527    1.50084   0.097    0.923    
# time         0.49435    0.06379   7.749 2.44e-09 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 4.657 on 38 degrees of freedom
# Multiple R-squared:  0.6124,	Adjusted R-squared:  0.6022 
# F-statistic: 60.05 on 1 and 38 DF,  p-value: 2.44e-09
coef(linearModel) # extract estimated parameters
# =>
# (Intercept)        time 
#   0.1452662   0.4943490 
summary(linearModel)$coefficients # another way to extract results
# =>
#              Estimate Std. Error    t value     Pr(>|t|)
# (Intercept) 0.1452662 1.50084246 0.09678975 9.234021e-01
# time        0.4943490 0.06379348 7.74920901 2.440008e-09
summary(linearModel)$coefficients[,4] # the p-values 
# =>
#  (Intercept)         time 
# 9.234021e-01 2.440008e-09 

# GENERAL LINEAR MODELS
# Logistic regression
set.seed(1)
list1$success = rbinom(length(list1$time), 1, .5) # random binary
glModel <- glm(success  ~ time, data = list1, 
	family=binomial(link="logit"))
glModel # outputs result of logistic regression
# =>
# Call:  glm(formula = success ~ time, 
#	family = binomial(link = "logit"), data = list1)
#
# Coefficients:
# (Intercept)         time  
#     0.17018     -0.01321  
# 
# Degrees of Freedom: 39 Total (i.e. Null);  38 Residual
# Null Deviance:	    55.35 
# Residual Deviance: 55.12 	 AIC: 59.12
summary(glModel) # more verbose output from the regression
# =>
# Call:
# glm(formula = success ~ time, 
#	family = binomial(link = "logit"), data = list1)

# Deviance Residuals: 
#    Min      1Q  Median      3Q     Max  
# -1.245  -1.118  -1.035   1.202   1.327  
# 
# Coefficients:
#             Estimate Std. Error z value Pr(>|z|)
# (Intercept)  0.17018    0.64621   0.263    0.792
# time        -0.01321    0.02757  -0.479    0.632
# 
# (Dispersion parameter for binomial family taken to be 1)
#
#     Null deviance: 55.352  on 39  degrees of freedom
# Residual deviance: 55.121  on 38  degrees of freedom
# AIC: 59.121
# 
# Number of Fisher Scoring iterations: 3


#########################
# Plots
#########################

# BUILT-IN PLOTTING FUNCTIONS
# Scatterplots!
plot(list1$time, list1$price, main = "fake data")
# Plot regression line on existing plot
abline(linearModel, col = "red")
# Get a variety of nice diagnostics
plot(linearModel)
# Histograms!
hist(rpois(n = 10000, lambda = 5), col = "thistle")
# Barplots!
barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow"))

# GGPLOT2
# But these are not even the prettiest of R's plots
# Try the ggplot2 package for more and better graphics
install.packages("ggplot2")
require(ggplot2)
?ggplot2
pp <- ggplot(students, aes(x=house))
pp + geom_histogram()
ll <- as.data.table(list1)
pp <- ggplot(ll, aes(x=time,price))
pp + geom_point()
# ggplot2 has excellent documentation (available http://docs.ggplot2.org/current/)



```

## How do I get R?

* Get R and the R GUI from [http://www.r-project.org/](http://www.r-project.org/)
* [RStudio](http://www.rstudio.com/ide/) is another GUI
---

language: racket
filename: learnracket.rkt
contributors:
  - ["th3rac25", "https://github.com/voila"]
  - ["Eli Barzilay", "https://github.com/elibarzilay"]
  - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
  - ["Duong H. Nguyen", "https://github.com/cmpitg"]
  - ["Keyan Zhang", "https://github.com/keyanzhang"]
---

Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family.

Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3rac25) or th3rac25 [at] [google's email service]


```racket
#lang racket ; defines the language we are using

;;; Comments

;; Single line comments start with a semicolon

#| Block comments
   can span multiple lines and...
    #|
       they can be nested!
    |#
|#

;; S-expression comments discard the following expression,
;; useful to comment expressions when debugging
#; (this expression is discarded)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Primitive Datatypes and Operators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Numbers
9999999999999999999999 ; integers
#b111                  ; binary => 7
#o111                  ; octal => 73
#x111                  ; hexadecimal => 273
3.14                   ; reals
6.02e+23
1/2                    ; rationals
1+2i                   ; complex numbers

;; Function application is written (f x y z ...)
;; where f is a function and x, y, z, ... are operands
;; If you want to create a literal list of data, use ' to stop it from
;; being evaluated
'(+ 1 2) ; => (+ 1 2)
;; Now, some arithmetic operations
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(expt 2 3) ; => 8
(quotient 5 2) ; => 2
(remainder 5 2) ; => 1
(/ 35 5) ; => 7
(/ 1 3) ; => 1/3
(exact->inexact 1/3) ; => 0.3333333333333333
(+ 1+2i  2-3i) ; => 3-1i

;;; Booleans
#t ; for true
#f ; for false -- any value other than #f is true
(not #t) ; => #f
(and 0 #f (error "doesn't get here")) ; => #f
(or #f 0 (error "doesn't get here"))  ; => 0

;;; Characters
#\A ; => #\A
#\λ ; => #\λ
#\u03BB ; => #\λ

;;; Strings are fixed-length array of characters.
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; backslash is an escaping character
"Foo\tbar\41\x21\u0021\a\r\n" ; includes C escapes, Unicode
"λx:(μα.α→α).xx"              ; can include Unicode characters

;; Strings can be added too!
(string-append "Hello " "world!") ; => "Hello world!"

;; A string can be treated like a list of characters
(string-ref "Apple" 0) ; => #\A

;; format can be used to format strings:
(format "~a can be ~a" "strings" "formatted")

;; Printing is pretty easy
(printf "I'm Racket. Nice to meet you!\n")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; You can create a variable using define
;; a variable name can use any character except: ()[]{}",'`;#|\
(define some-var 5)
some-var ; => 5

;; You can also use unicode characters
(define ⊆ subset?)
(⊆ (set 3 2) (set 1 2 3)) ; => #t

;; Accessing a previously unassigned variable is an exception
; x ; => x: undefined ...

;; Local binding: `me' is bound to "Bob" only within the (let ...)
(let ([me "Bob"])
  "Alice"
  me) ; => "Bob"

;; let* is like let, but allows you to use previous bindings in creating later bindings
(let* ([x 1]
       [y (+ x 1)])
       (* x y))

;; finally, letrec allows you to define recursive and mutually recursive functions
(letrec ([is-even? (lambda (n)
                       (or (zero? n)
                           (is-odd? (sub1 n))))]
           [is-odd? (lambda (n)
                      (and (not (zero? n))
                           (is-even? (sub1 n))))])
    (is-odd? 11))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Structs and Collections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Structs
; By default, structs are immutable
(struct dog (name breed age))
(define my-pet
  (dog "lassie" "collie" 5))
my-pet ; => #<dog>
; returns whether the variable was constructed with the dog constructor
(dog? my-pet) ; => #t
; accesses the name field of the variable constructed with the dog constructor
(dog-name my-pet) ; => "lassie"

; You can explicitly declare a struct to be mutable with the #:mutable option
(struct rgba-color (red green blue alpha) #:mutable)
(define burgundy
   (rgba-color 144 0 32 1.0))
(set-rgba-color-green! burgundy 10)
(rgba-color-green burgundy) ; => 10

;;; Pairs (immutable)
;; `cons' constructs pairs, `car' and `cdr' extract the first
;; and second elements
(cons 1 2) ; => '(1 . 2)
(car (cons 1 2)) ; => 1
(cdr (cons 1 2)) ; => 2

;;; Lists

;; Lists are linked-list data structures, made of `cons' pairs and end
;; with a `null' (or '()) to mark the end of the list
(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3)
;; `list' is a convenience variadic constructor for lists
(list 1 2 3) ; => '(1 2 3)
;; a quote can also be used for a literal list value
'(1 2 3) ; => '(1 2 3)
;; a quasiquote (represented by the backtick character) with commas 
;; can be used to evaluate functions
`(1 ,(+ 1 1) 3) ; => '(1 2 3)

;; With lists, car/cdr work slightly differently
(car '(1 2 3)) ; => 1
(cdr '(1 2 3)) ; => '(2 3)

;; Racket also has predefined functions on top of car and cdr, to extract parts of a list
(cadr (list 1 2 3)) ; => 2
(car (cdr (list 1 2 3))) ; => 2

(cddr (list 1 2 3)) ; => '(3)
(cdr (cdr (list 1 2 3))) ; => '(3)

(caddr (list 1 2 3)) ; => 3
(car (cdr (cdr (list 1 2 3)))) ; => 3

;; Can still use `cons' to add an item to the beginning of a list
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; Use `append' to add lists together
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; Lists are a very basic type, so there is a *lot* of functionality for
;; them, a few examples:
(map add1 '(1 2 3))          ; => '(2 3 4)
(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33)
(filter even? '(1 2 3 4))    ; => '(2 4)
(count even? '(1 2 3 4))     ; => 2
(take '(1 2 3 4) 2)          ; => '(1 2)
(drop '(1 2 3 4) 2)          ; => '(3 4)

;;; Vectors

;; Vectors are fixed-length arrays
#(1 2 3) ; => '#(1 2 3)

;; Use `vector-append' to add vectors together
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; Sets

;; Create a set from a list
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)

;; Add a member with `set-add'
;; (Functional: returns the extended set rather than mutate the input)
(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)

;; Remove one with `set-remove'
(set-remove (set 1 2 3) 1) ; => (set 2 3)

;; Test for existence with `set-member?'
(set-member? (set 1 2 3) 1) ; => #t
(set-member? (set 1 2 3) 4) ; => #f

;;; Hashes

;; Create an immutable hash table (mutable example below)
(define m (hash 'a 1 'b 2 'c 3))

;; Retrieve a value
(hash-ref m 'a) ; => 1

;; Retrieving a non-present value is an exception
; (hash-ref m 'd) => no value found

;; You can provide a default value for missing keys
(hash-ref m 'd 0) ; => 0

;; Use `hash-set' to extend an immutable hash table
;; (Returns the extended hash instead of mutating it)
(define m2 (hash-set m 'd 4))
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))

;; Remember, these hashes are immutable!
m ; => '#hash((b . 2) (a . 1) (c . 3))  <-- no `d'

;; Use `hash-remove' to remove keys (functional too)
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use `lambda' to create functions.
;; A function always returns the value of its last expression
(lambda () "Hello World") ; => #<procedure>
;; Can also use a unicode `λ'
(λ () "Hello World")     ; => same function

;; Use parens to call all functions, including a lambda expression
((lambda () "Hello World")) ; => "Hello World"
((λ () "Hello World"))      ; => "Hello World"

;; Assign a function to a var
(define hello-world (lambda () "Hello World"))
(hello-world) ; => "Hello World"

;; You can shorten this using the function definition syntactic sugar:
(define (hello-world2) "Hello World")

;; The () in the above is the list of arguments for the function
(define hello
  (lambda (name)
    (string-append "Hello " name)))
(hello "Steve") ; => "Hello Steve"
;; ... or equivalently, using a sugared definition:
(define (hello2 name)
  (string-append "Hello " name))

;; You can have multi-variadic functions too, using `case-lambda'
(define hello3
  (case-lambda
    [() "Hello World"]
    [(name) (string-append "Hello " name)]))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"
;; ... or specify optional arguments with a default value expression
(define (hello4 [name "World"])
  (string-append "Hello " name))

;; Functions can pack extra arguments up in a list
(define (count-args . args)
  (format "You passed ~a args: ~a" (length args) args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
;; ... or with the unsugared `lambda' form:
(define count-args2
  (lambda args
    (format "You passed ~a args: ~a" (length args) args)))

;; You can mix regular and packed arguments
(define (hello-count name . args)
  (format "Hello ~a, you passed ~a extra args" name (length args)))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"
;; ... unsugared:
(define hello-count2
  (lambda (name . args)
    (format "Hello ~a, you passed ~a extra args" name (length args))))

;; And with keywords
(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
  (format "~a ~a, ~a extra args" g name (length args)))
(hello-k)                 ; => "Hello World, 0 extra args"
(hello-k 1 2 3)           ; => "Hello World, 3 extra args"
(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
                                         ; => "Hi Finn, 6 extra args"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Equality
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; for numbers use `='
(= 3 3.0) ; => #t
(= 2 1)   ; => #f

;; `eq?' returns #t if 2 arguments refer to the same object (in memory),
;; #f otherwise.
;; In other words, it's a simple pointer comparison.
(eq? '() '()) ; => #t, since there exists only one empty list in memory
(let ([x '()] [y '()])
  (eq? x y))  ; => #t, same as above

(eq? (list 3) (list 3)) ; => #f
(let ([x (list 3)] [y (list 3)])
  (eq? x y))            ; => #f — not the same list in memory!

(let* ([x (list 3)] [y x])
  (eq? x y)) ; => #t, since x and y now point to the same stuff

(eq? 'yes 'yes) ; => #t
(eq? 'yes 'no)  ; => #f

(eq? 3 3)   ; => #t — be careful here
            ; It’s better to use `=' for number comparisons.
(eq? 3 3.0) ; => #f

(eq? (expt 2 100) (expt 2 100))               ; => #f
(eq? (integer->char 955) (integer->char 955)) ; => #f

(eq? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #f

;; `eqv?' supports the comparison of number and character datatypes.
;; for other datatypes, `eqv?' and `eq?' return the same result.
(eqv? 3 3.0)                                   ; => #f
(eqv? (expt 2 100) (expt 2 100))               ; => #t
(eqv? (integer->char 955) (integer->char 955)) ; => #t

(eqv? (string-append "foo" "bar") (string-append "foo" "bar"))   ; => #f

;; `equal?' supports the comparison of the following datatypes:
;; strings, byte strings, pairs, mutable pairs, vectors, boxes,
;; hash tables, and inspectable structures.
;; for other datatypes, `equal?' and `eqv?' return the same result.
(equal? 3 3.0)                                                   ; => #f
(equal? (string-append "foo" "bar") (string-append "foo" "bar")) ; => #t
(equal? (list 3) (list 3))                                       ; => #t

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Control Flow
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Conditionals

(if #t               ; test expression
    "this is true"   ; then expression
    "this is false") ; else expression
; => "this is true"

;; In conditionals, all non-#f values are treated as true
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'yep

;; `cond' chains a series of tests to select a result
(cond [(> 2 2) (error "wrong!")]
      [(< 2 2) (error "wrong again!")]
      [else 'ok]) ; => 'ok

;;; Pattern Matching

(define (fizzbuzz? n)
  (match (list (remainder n 3) (remainder n 5))
    [(list 0 0) 'fizzbuzz]
    [(list 0 _) 'fizz]
    [(list _ 0) 'buzz]
    [_          #f]))

(fizzbuzz? 15) ; => 'fizzbuzz
(fizzbuzz? 37) ; => #f

;;; Loops

;; Looping can be done through (tail-) recursion
(define (loop i)
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i))))
(loop 5) ; => i=5, i=6, ...

;; Similarly, with a named let
(let loop ((i 0))
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i)))) ; => i=0, i=1, ...

;; See below how to add a new `loop' form, but Racket already has a very
;; flexible `for' form for loops:
(for ([i 10])
  (printf "i=~a\n" i)) ; => i=0, i=1, ...
(for ([i (in-range 5 10)])
  (printf "i=~a\n" i)) ; => i=5, i=6, ...

;;; Iteration Over Other Sequences
;; `for' allows iteration over many other kinds of sequences:
;; lists, vectors, strings, sets, hash tables, etc...

(for ([i (in-list '(l i s t))])
  (displayln i))

(for ([i (in-vector #(v e c t o r))])
  (displayln i))

(for ([i (in-string "string")])
  (displayln i))

(for ([i (in-set (set 'x 'y 'z))])
  (displayln i))

(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
  (printf "key:~a value:~a\n" k v))

;;; More Complex Iterations

;; Parallel scan of multiple sequences (stops on shortest)
(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x 1:y 2:z

;; Nested loops
(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z

;; Conditions
(for ([i 1000]
      #:when (> i 5)
      #:unless (odd? i)
      #:break (> i 10))
  (printf "i=~a\n" i))
; => i=6, i=8, i=10

;;; Comprehensions
;; Very similar to `for' loops -- just collect the results

(for/list ([i '(1 2 3)])
  (add1 i)) ; => '(2 3 4)

(for/list ([i '(1 2 3)] #:when (even? i))
  i) ; => '(2)

(for/list ([i 10] [j '(x y z)])
  (list i j)) ; => '((0 x) (1 y) (2 z))

(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10))
  i) ; => '(6 8 10)

(for/hash ([i '(1 2 3)])
  (values i (number->string i)))
; => '#hash((1 . "1") (2 . "2") (3 . "3"))

;; There are many kinds of other built-in ways to collect loop values:
(for/sum ([i 10]) (* i i)) ; => 285
(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000
(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t
(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t
;; And to use any arbitrary combination, use `for/fold'
(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10
;; (This can often replace common imperative loops)

;;; Exceptions

;; To catch exceptions, use the `with-handlers' form
(with-handlers ([exn:fail? (lambda (exn) 999)])
  (+ 1 "2")) ; => 999
(with-handlers ([exn:break? (lambda (exn) "no time")])
  (sleep 3)
  "phew") ; => "phew", but if you break it => "no time"

;; Use `raise' to throw exceptions or any other value
(with-handlers ([number?    ; catch numeric values raised
                 identity]) ; return them as plain values
  (+ 1 (raise 2))) ; => 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Mutation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use `set!' to assign a new value to an existing variable
(define n 5)
(set! n (add1 n))
n ; => 6

;; Use boxes for explicitly mutable values (similar to pointers or
;; references in other languages)
(define n* (box 5))
(set-box! n* (add1 (unbox n*)))
(unbox n*) ; => 6

;; Many Racket datatypes are immutable (pairs, lists, etc), some come in
;; both mutable and immutable flavors (strings, vectors, hash tables,
;; etc...)

;; Use `vector' or `make-vector' to create mutable vectors
(define vec (vector 2 2 3 4))
(define wall (make-vector 100 'bottle-of-beer))
;; Use vector-set! to update a slot
(vector-set! vec 0 1)
(vector-set! wall 99 'down)
vec ; => #(1 2 3 4)

;; Create an empty mutable hash table and manipulate it
(define m3 (make-hash))
(hash-set! m3 'a 1)
(hash-set! m3 'b 2)
(hash-set! m3 'c 3)
(hash-ref m3 'a)   ; => 1
(hash-ref m3 'd 0) ; => 0
(hash-remove! m3 'a)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Modules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Modules let you organize code into multiple files and reusable
;; libraries; here we use sub-modules, nested in the whole module that
;; this text makes (starting from the "#lang" line)

(module cake racket/base ; define a `cake' module based on racket/base

  (provide print-cake) ; function exported by the module

  (define (print-cake n)
    (show "   ~a   " n #\.)
    (show " .-~a-. " n #\|)
    (show " | ~a | " n #\space)
    (show "---~a---" n #\-))

  (define (show fmt n ch) ; internal function
    (printf fmt (make-string n ch))
    (newline)))

;; Use `require' to get all `provide'd names from a module
(require 'cake) ; the ' is for a local submodule
(print-cake 3)
; (show "~a" 1 #\A) ; => error, `show' was not exported

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. Classes and Objects
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Create a class fish% (-% is idiomatic for class bindings)
(define fish%
  (class object%
    (init size) ; initialization argument
    (super-new) ; superclass initialization
    ;; Field
    (define current-size size)
    ;; Public methods
    (define/public (get-size)
      current-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

;; Create an instance of fish%
(define charlie
  (new fish% [size 10]))

;; Use `send' to call an object's methods
(send charlie get-size) ; => 10
(send charlie grow 6)
(send charlie get-size) ; => 16

;; `fish%' is a plain "first class" value, which can get us mixins
(define (add-color c%)
  (class c%
    (init color)
    (super-new)
    (define my-color color)
    (define/public (get-color) my-color)))
(define colored-fish% (add-color fish%))
(define charlie2 (new colored-fish% [size 10] [color 'red]))
(send charlie2 get-color)
;; or, with no names:
(send (new (add-color fish%) [size 10] [color 'red]) get-color)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 9. Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Macros let you extend the syntax of the language

;; Let's add a while loop
(define-syntax-rule (while condition body ...)
  (let loop ()
    (when condition
      body ...
      (loop))))

(let ([i 0])
  (while (< i  10)
    (displayln i)
    (set! i (add1 i))))

;; Macros are hygienic, you cannot clobber existing variables!
(define-syntax-rule (swap! x y) ; -! is idiomatic for mutation
  (let ([tmp x])
    (set! x y)
    (set! y tmp)))

(define tmp 2)
(define other 3)
(swap! tmp other)
(printf "tmp = ~a; other = ~a\n" tmp other)
;; The variable `tmp` is renamed to `tmp_1`
;; in order to avoid name conflict
;; (let ([tmp_1 tmp])
;;   (set! tmp other)
;;   (set! other tmp_1))

;; But they are still code transformations, for example:
(define-syntax-rule (bad-while condition body ...)
  (when condition
    body ...
    (bad-while condition body ...)))
;; this macro is broken: it generates infinite code, if you try to use
;; it, the compiler will get in an infinite loop

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 10. Contracts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Contracts impose constraints on values exported from modules

(module bank-account racket
  (provide (contract-out
            [deposit (-> positive? any)] ; amounts are always positive
            [balance (-> positive?)]))

  (define amount 0)
  (define (deposit a) (set! amount (+ amount a)))
  (define (balance) amount)
  )

(require 'bank-account)
(deposit 5)

(balance) ; => 5

;; Clients that attempt to deposit a non-positive amount are blamed
;; (deposit -5) ; => deposit: contract violation
;; expected: positive?
;; given: -5
;; more details....

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 11. Input & output
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Racket has this concept of "port", which is very similar to file
;; descriptors in other languages

;; Open "/tmp/tmp.txt" and write "Hello World"
;; This would trigger an error if the file's already existed
(define out-port (open-output-file "/tmp/tmp.txt"))
(displayln "Hello World" out-port)
(close-output-port out-port)

;; Append to "/tmp/tmp.txt"
(define out-port (open-output-file "/tmp/tmp.txt"
                                   #:exists 'append))
(displayln "Hola mundo" out-port)
(close-output-port out-port)

;; Read from the file again
(define in-port (open-input-file "/tmp/tmp.txt"))
(displayln (read-line in-port))
; => "Hello World"
(displayln (read-line in-port))
; => "Hola mundo"
(close-input-port in-port)

;; Alternatively, with call-with-output-file you don't need to explicitly
;; close the file
(call-with-output-file "/tmp/tmp.txt"
  #:exists 'update ; Rewrite the content
  (λ (out-port)
    (displayln "World Hello!" out-port)))

;; And call-with-input-file does the same thing for input
(call-with-input-file "/tmp/tmp.txt"
  (λ (in-port)
    (displayln (read-line in-port))))
```

## Further Reading

Still up for more? Try [Getting Started with Racket](http://docs.racket-lang.org/getting-started/)
# [Learn X in Y minutes][1]

[![Build Status](https://travis-ci.org/adambard/learnxinyminutes-docs.svg?branch=master)](https://travis-ci.org/adambard/learnxinyminutes-docs)

Whirlwind tours of (several, hopefully many someday) popular and
ought-to-be-more-popular programming languages, presented as valid, commented
code and explained as they go.

## We need YOU!...

... to write more inline code tutorials. Just grab an existing file from this
repo and copy the formatting (don't worry, it's all very simple). Make a new
file, send a pull request, and if it passes muster I'll get it up pronto.
Remember to fill in the "contributors" fields so you get credited properly!

## Contributing

All contributions are welcome, from the tiniest typo to a brand new article.
Translations in all languages are welcome (or, for that matter, original
articles in any language). Send a pull request or open an issue any time of day
or night.

**Please prepend the tag `[language/lang-code]` to your issues and pull
requests.** For example, `[python/en]` for English Python. This will help
everyone pick out things they care about.

We're happy for any contribution in any form, but if you're making more than one
major change (i.e. translations for two different languages) it would be super
cool of you to make a separate pull request for each one so that someone can
review them more effectively and/or individually.

For a detailed style guide, please review the full [CONTRIBUTING][2] guidelines.

## License

Contributors retain copyright to their work, and can request removal at any
time. By uploading a doc here, you agree to publish your work under the default
[Creative Commons Attribution-ShareAlike 3.0 Unported][3] licensing included on
each doc page.

Anything not covered by the above -- basically, this README -- you can use as
you wish, I guess.


[1]: http://learnxinyminutes.com
[2]: /CONTRIBUTING.markdown
[3]: http://creativecommons.org/licenses/by-sa/3.0/deed.en_US
---
name: Red
category: language
language: Red
filename: learnred.red
contributors:
    - ["Arnold van Hofwegen", "https://github.com/iArnold"]
---


Red was created out of the need to get work done, and the tool the author wanted to use, the language of REBOL, had a couple of drawbacks.
It was not Open Sourced at that time and it is an interpreted language, what means that it is on average slow compared to a compiled language.

Red, together with its C-level dialect Red/System, provides a language that covers the entire programming space you ever need to program something in.
Red is a language heavily based on the language of REBOL. Where Red itself reproduces the flexibility of the REBOL language, the underlying language Red will be built upon,
Red/System, covers the more basic needs of programming like C can, being closer to the metal.

Red will be the world's first Full Stack Programming Language. This means that it will be an effective tool to do (almost) any programming task on every level
from the metal to the meta without the aid of other stack tools.
Furthermore Red will be able to cross-compile Red source code without using any GCC like toolchain
from any platform to any other platform. And it will do this all from a binary executable that is supposed to stay under 1 MB.

Ready to learn your first Red?

```
All text before the header will be treated as comment, as long as you avoid
using the word "red" starting with a capital "R" in this pre-header text.
This is a temporary shortcoming of the used lexer but most of the time you
start your script or program with the header itself.

The header of a red script is the capitalized word "red" followed by a
whitespace character followed by a block of square brackets []. The block of
brackets can be filled with useful information about this script or program:
the author's name, the filename, the version, the license, a summary of what
the program does or any other files it needs. The red/System header is just
like the red header, only saying "red/System" and not "red".
```
```red
Red []

;this is a commented line

print "Hello Red World"    ; this is another comment

comment {
    This is a multiline comment.
    You just saw the Red version of the "Hello World" program.
}

; Your program's entry point is the first executable code that is found
; no need to restrict this to a 'main' function.

; Valid variable names start with a letter and can contain numbers,
; variables containing only capital A through F and numbers and ending with 'h'
; are forbidden, because that is how hexadecimal numbers are expressed in Red
; and Red/System.

; assign a value to a variable using a colon ":"
my-name: "Red"
reason-for-using-the-colon: {Assigning values using the colon makes
 the equality sign "=" exclusively usable for comparisons purposes,
 exactly what "=" was intended for in the first place!
 Remember this y = x + 1 and x = 1 => y = 2 stuff from school?
}
is-this-name-valid?: true

; print output using print, or prin for printing without a newline or linefeed
; at the end of the printed text.

prin " My name is " print my-name
My name is Red

print ["My name is " my-name lf]
My name is Red

; If you haven't already noticed: statements do NOT end with a semicolon ;-)

;
; Datatypes
;
; If you know Rebol, you probably have noticed it has lots of datatypes. Red
; does not have yet all those types, but as Red want to be close to Rebol it
; will have a lot of datatypes.
; You can recognize types by the exclamation sign at the end. But beware
; names ending with an exclamation sign are allowed.
; Some of the available types are integer! string! block!

; Declaring variables before using them?
; Red knows by itself what variable is best to use for the data you want to
; use it for.
; A variable declaration is not always necessary.
; It is considered good coding practise to declare your variables,
; but it is not forced upon you by Red.
; You can declare a variable and specify its type. a variable's type
; determines its size in bytes.

; Variables of integer! type are usually 4 bytes or 32 bits
my-integer: 0
; Red's integers are signed. No support for unsigned atm but that will come.

; To find out the type of variable use type?
type? my-integer
integer!

; A variable can be initialized using another variable that gets initialized
; at the same time.
i2: 1 + i1: 1

; Arithmetic is straightforward
i1 + i2 ; result 3
i2 - i1 ; result 1
i2 * i1 ; result 2
i1 / i2 ; result 0 (0.5, but truncated towards 0)

; Comparison operators are probably familiar, and unlike in other languages
; you only need a single '=' sign for comparison.
; There is a boolean like type in Red. It has values true and false, but also
; the values on/off or yes/no can be used

3 = 2 ; result false
3 != 2 ; result true
3 > 2 ; result true
3 < 2 ; result false
2 <= 2 ; result true
2 >= 2 ; result true

;
; Control Structures
;
; if
; Evaluate a block of code if a given condition is true. IF does not return
; any value, so cannot be used in an expression.
if a < 0 [print "a is negative"]

; either
; Evaluate a block of code if a given condition is true, else evaluate an
; alternative block of code. If the last expressions in both blocks have the
; same type, EITHER can be used inside an expression.
either a > 0 [
   msg: "positive"
][
   either a = 0 [
       msg: "zero"
   ][
       msg: "negative"
   ]
]

print ["a is " msg lf]

; There is an alternative way to write this
; (Which is allowed because all code paths return a value of the same type):

msg: either a > 0 [
   "positive"
][
   either a = 0 [
       "zero"
   ][
       "negative"
   ]
]
print ["a is " msg lf]

; until
; Loop over a block of code until the condition at end of block, is met.
; UNTIL does not return any value, so it cannot be used in an expression.
c: 5
until [
   prin "o"
   c: c - 1
   c = 0    ; the condition to end the until loop
]
;   will output:
ooooo
; Note that the loop will always be evaluated at least once, even if the
; condition is not met from the beginning.

; while
; While a given condition is met, evaluate a block of code.
; WHILE does not return any value, so it cannot be used in an expression.
c: 5
while [c > 0][
   prin "o"
   c: c - 1
]
; will output:
ooooo

;
; Functions
;
; function example
twice: function [a [integer!] /one return: [integer!]][
        c: 2
        a: a * c
        either one [a + 1][a]
]
b: 3
print twice b   ; will output 6.

; Import external files with #include and filenames start with a % sign
#include %includefile.red
; Now the functions in the included file can be used too.

```

## Further Reading

The main source for information about Red is the [Red language homepage](http://www.red-lang.org).

The source can be found on [github](https://github.com/red/red).

The Red/System language specification can be found [here](http://static.red-lang.org/red-system-specs-light.html).

To learn more about Rebol and Red join the [chat on Gitter](https://gitter.im/red/red). And if that is not working for you drop a mail to us on the [Red mailing list](mailto: red-langNO_SPAM@googlegroups.com) (remove NO_SPAM).

Browse or ask questions on [Stack Overflow](stackoverflow.com/questions/tagged/red).

Maybe you want to try Red right away? That is possible on the [try Rebol and Red site](http://tryrebol.esperconsultancy.nl).

You can also learn Red by learning some [Rebol](http://www.rebol.com/docs.html).
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
translators:
    - ["Adrian Bordinc", "https://github.com/ellimist"]
lang: ro-ro
filename: LearnBash-ro.sh
---

Bash este numele shell-ului UNIX, care a fost de asemenea distribuit drept shell pentru sistemul de operare GNU și ca shell implicit pentru Linux si Mac OS X.
Aproape toate exemplele de mai jos pot fi parte dintr-un script sau pot fi executate direct in linia de comanda.

[Citește mai multe:](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# Prima linie din script se numeste "shebang" 
# care spune sistemului cum să execute scriptul
# http://en.wikipedia.org/wiki/Shebang_(Unix)
# După cum te-ai prins deja, comentariile încep cu #. 
# Shebang este de asemenea un comentariu.

# Exemplu simplu de hello world:
echo Hello world!

# Fiecare comandă începe pe o linie nouă, sau după punct și virgula ;
echo 'Prima linie'; echo 'A doua linie'

# Declararea unei variabile se face astfel:
VARIABLE="Niște text"

# DAR nu așa:
VARIABLE = "Niste text"
# Bash va crede că VARIABLE este o comandă care trebuie executată și va
# returna o eroare pentru că nu va putea fi găsita.

# Folosind variabila:
echo $VARIABLE
echo "$VARIABLE"
echo '$VARIABLE'
# Atunci când folosesti variabila, o atribui, o exporți sau altfel, 
# numele ei se scrie fără $.
# Daca vrei sa folosesti valoarea variabilei, atunci trebuie să folosești $.
# Atentie la faptul că ' (apostrof) nu va inlocui variabla cu valoarea ei.

# Inlocuirea de caractere în variabile
echo ${VARIABLE/Niște/Un}
# Asta va înlocui prima apariție a "Niște" cu "Un" în variabila de mai sus.

# Substring dintr-o variabilă
echo ${VARIABLE:0:7}
# Asta va returna numai primele 7 caractere din variabila.

# Valoarea implicita a unei variabile:
echo ${FOO:-"ValoareaImplicitaDacaFOOLipseșteSauEGoală"}
# Asta functionează pentru null (FOO=),
# sir de caractere gol (FOO=""), zero (FOO=0) returnează 0

# Variabile pre-existente
echo "Ulima valoare returnată de ultimul program rulat: $?"
echo "ID-ul procesului (PID) care rulează scriptul: $$"
echo "Numărul de argumente: $#"
echo "Argumentele scriptului: $@"
echo "Argumentele scriptului separate în variabile: $1 $2..."

# Citind o valoare din consolă
echo "Care e numele tău?"
read NAME # Observă faptul că nu a trebuit să declarăm o variabilă nouă
echo Salut, $NAME!

# Avem obisnuita instructiune "if"
# Folosește "man test" pentru mai multe informații 
# despre instrucținea conditionala
if [ $NAME -ne $USER ]
then
    echo "Numele tău este username-ul tău"
else
    echo "Numele tău nu este username-ul tău"
fi

# Există, de asemenea, și executarea conditională de comenzi
echo "Întotdeauna executat" || echo "Executat dacă prima instrucțiune eșuează"
echo "Întotdeauna executat" && echo "Executat dacă prima instrucțiune NU esuează"

# Expresiile apar în urmatorul format
echo $(( 10 + 5 ))

# Spre deosebire de alte limbaje de programare, bash este un shell - așa că 
# funcționează in contextul directorului curent. Poți vedea fișiere și directoare
# din directorul curent folosind comanda "ls":
ls

# Aceste comenzi au optiuni care le controlează execuțiă
ls -l # Listează fiecare fișier și director pe o linie separată

# Rezultatele comenzii anterioare pot fi 
# trimise următoarei comenzi drept argument
# Comanda grep filtrează argumentele trimise cu sabloane. 
# Astfel putem vedea fiserele .txt din directorul curent.
ls -l | grep "\.txt"

# De asemenea, poți redirecționa date de intrare spre sau erori/date de ieșire
# dinspre o comandă
python2 hello.py < "intrare.in"
python2 hello.py > "ieșire.out"
python2 hello.py 2> "erori.err"
# Output-ul va suprascrie fișierul dacă acesta există.
# Daca vrei să fie concatenate datele poți folosi ">>" în loc de ">"

# Comenzile pot fi înlocuite în interiorul altor comenzi folosind $( ):
# Urmatoarea comandă afișează numărul de fișiere 
# și directoare din directorul curent
echo "Sunt $(ls | wc -l) fișiere aici."

# Același lucru se poate obține folosind apostroful inversat ``,
# dar nu pot fi folosite limbricate, așa ca modalitatea 
# preferată este de a folosi $( )
echo "Sunt `ls | wc -l` fișiere aici."

# Bash folosește o instrucțiune 'case' care funcționeaza 
# în mod similar cu instructiunea switch din Java si C++
case "$VARIABLE" in 
    0) echo "Este un zero.";;
    1) echo "Este un unu.";;
    *) echo "Nu este null";;
esac

# Instrucțiunea 'for' parcurge toate elementele trimise:
# Conținutul variabilei $VARIABLE este printat de 3 ori
for VARIABLE in {1..3}
do
    echo "$VARIABLE"
done

# Buclă while:
while [true]
do
    echo "în interiorul iterației aici..."
    break
done

# De asemenea poți defini funcții
# Definiție:
function foo ()
{
    echo "Argumentele funcționeaza ca și argumentele scriptului: $@"
    echo "Si: $1 $2..."
    echo "Asta este o funcție"
    return 0
}

# sau mai simplu:
bar ()
{
    echo "Altă metodă de a declara o funcție"
    return 0
}

# Invocarea unei funcții:
foo "Numele meu este: " $NAME

# Sunt o multime de comenzi utile pe care ar trebui să le inveți:
tail -n 10 file.txt
# afișează ultimele 10 linii din fișierul file.txt

head -n 10 file.txt
# afișează primele 10 linii din fișierul file.txt

sort file.txt
# sortează liniile din file.txt

uniq -d file.txt
# raporteaza sau omite liniile care se repetă. Cu -d le raporteaza

cut -d ',' -f 1 file.txt
# printează doar prima coloană inainte de caracterul ","
```
---
language: bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Petru Dimitriu", "http://petru-dimitriu.github.io"]
filename: bf-ro.html
lang: ro-ro
---

Brainfuck (un nume propriu care nu primește majusculă inițială decât la începutul
propoziției) este un limbaj de programare Turing-comple extrem de minimalist cu
doar 8 instrucțiuni.

Puteți încerca brainfuck în navigatorul dumneavoastră cu [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/).

```
Orice caracter in afara de "><+-.,[]" (fara ghilimele) este ignorat.

Brainfuck se reprezinta ca un vector de 30 000 de celule initializate cu zero
si un pointer de date care trimite spre celula curenta.

Exista opt comenzi:
+ : Incrementeaza valoarea celulei curente cu 1.
- : Decrementeaza valoarea celulei curente cu 1.
> : Muta pointerul de date la urmatoarea celula (o celula la dreapta).
< : Muta pointerul de date la celula precedenta (o celula la stanga).
. : Afiseaza valoarea caracterului ASCII din celul caurenta (ex. 65 = 'A').
, : Citeste un singur caracter si plaseaza valoarea lui in celula curenta.
[ : Daca valoarea in celula curenta este zero, sare la urmatorul caracter ] .
    Altfel, merge la urmatoarea instructiune.
] : Daca valoarea in celula curenta este zero, sare la urmatoarea
	instructiune.
    Altfel, se intoarce la instructiunea de dupa caracterul [ precedent .

[ and ] formeaza un ciclu. Evident, trebuie ca parantezarea sa fie corecta.

Sa privim cateva programe brainfuck simple.

++++++ [ > ++++++++++ < - ] > +++++ .

Acest program afiseaza litera 'A'. Mai intai, incrementeaza celula #1 pana
la valoarea 6. Celula #1 va fi folosita pentru ciclare. Apoi, intra in ciclu
([) si muta pointerul la celula #2. Incrementeaza celula #2 de 10 ori,
muta pointerul la celula #1 si decrementeaza celula #1. Acest ciclu parcurge
6 iteratii (este nevoie de 6 decrementari pentru ca celula #1 sa ajunga la 0),
iar dupa aceea se trece la caracterul ] corespunzator si se continua executia.

In acest moment, ne aflam in celula #1, care are valoarea 0, in timp ce celula
#2 are valoarea 60. Ne mutam pe celula #2, incrementam de 5 ori, pentru a
obtine valoarea 65, si apoi afisam valoarea celulei #2. 65 este codul ASCII
pentru 'A', deci se afiseaza 'A' in terminal.

, [ > + < - ] > .

Acest program citeste un caracter de la intrarea utilizator si copiaza caracterul
in celula #1. Apoi incepem un ciclu. Se muta pointerul in celula #2, se
incremneteaza valoarea de la celula #2, se muta inapoi la celula #1, se
decrementeaza valoarea de la celula #1. Aceasta continua pana cand celula #1 este
0 iar celula #2 retine vechea valoare a celulei #1. Deoarece ne aflam in celula
#1 la sfarsitul ciclului, ne mutam pe celula #2 si afisam simbolul corespunzator
in ASCII.

Aveti in vedere ca spatiile sunt doar pentru usurinta citirii. La fel de bine
programul ar fi putut fi scris astfel:

,[>+<-]>.

Incercati sa va dati seama ce face acest program:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Acest program citeste doua numere ca intrare si le inmulteste.

Pe scurt, programul citeste doua date de intrare, apoi incepe ciclul
mare, a carui conditie se afla in celula #1; apoi se muta la celula #2
si incepe un ciclu imbricat a carui conditie de reluare se afla in
celula #2, si care incrementeaza celula #3. Totusi aici intervine o
problema: La sfarsitul ciclului imbricat, celula #2 este zero. In
acest caz, celula ciclul imbricat nu va mai functiona data viitoare.
Pentru a rezolva aceasta problema, incrementam celula si #4, si
recopiem celula #4 in celula #2. In final, celula #3 este rezultatul.

```

Așadar acesta este limbajul brainfuck. Nu e atât de greu, nu? Pentru
amuzament, puteți să scrieți propriile dumneavoastră limbaje, sau puteți
scrie un interpretor pentru brainfuck într-un alt limbaj. Interpretorul
este destul de ușor de implementat, dar dacă sunteți masochist, încercați
să implementați un interpretor de brainfuck… în brainfuck.
---
language: clojure
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Bogdan Paun", "http://twitter.com/bgdnpn"]
filename: learnclojure-ro.clj
lang: ro-ro
---

Clojure este un limbaj din familia Lisp dezvoltat pentru Masina Virtuala Java
(Java Virtual Machine - JVM). Pune un accent mult mai puternic pe
[programarea funcionala](https://en.wikipedia.org/wiki/Functional_programming)
pura decat Common Lisp, dar include utilitare [STM](https://en.wikipedia.org/wiki/Software_transactional_memory)
pentru a gestiona starea, atunci cand aceasta apare.

Combinatia aceasta ii permite sa gestioneze procese concurente foarte usor,
de multe ori in mod automat.

(Aveti nevoie deo versiune Clojure 1.2 sau mai noua)


```clojure
; Comentariile incep cu punct si virgula.

; Clojure se scrie in "forme", care nu sunt decat
; liste de lucruri in interiorul unor paranteze, separate prin spatii.
;
; Reader-ul Clojure presupune ca primul lucru este o
; functie sau un macro de apelat, iar restul sunt argumente.

; Prima apelare intr-un fisier ar trebui sa fie ns, pentru a configura namespace-ul
(ns learnclojure)

; Mai multe exemple de baza:

; str va crea un string folosint toate argumentele sale
(str "Hello" " " "World") ; => "Hello World"

; Matematica este simpla
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; Egalitatea este =
(= 1 1) ; => true
(= 2 1) ; => false

; Folosim si not pentru logica
(not true) ; => false

; Formele imbricate functioneaza asa
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; Tipuri
;;;;;;;;;;;;;

; Clojure foloseste sistemul de obiecte Java pentru boolean, string si numere.
; Folositi `class` pentru a le inspecta.
(class 1) ; Numere intregi sunt jaba.lang.Long, in mod normal
(class 1.); Numelere reale sunt java.lang.Double
(class ""); Sirurile de caractere sunt mere intre apostrofuri duble, si sunt java.lang.String
(class false) ; Booleanele sunt java.lang.Boolean
(class nil); Valoarea "null" este numita nil

; Daca doriti sa creati o lista de date literale, folositi ' pentru a preveni
; evaluarea ei
'(+ 1 2) ; => (+ 1 2)
; (prescurtare pentru (quote (+ 1 2)))

; Puteti evalua o lista cu apostrof
(eval '(+ 1 2)) ; => 3

; Colectii & Secvente
;;;;;;;;;;;;;;;;;;;

; Listele sunt structuri de date lista-inlantuita, spre deosebire de Vectori
; Vectorii si Listele sunt si ele clase Java!
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; O liste ar putea fi scrisa direct ca (1 2 3), dar trebuie sa folosim apostrof
; pentru a preveni reader-ul din a crede ca e o functie.
; De asemenea, (list 1 2 3) este acelasi lucru cu '(1 2 3)

; "Colectiile" sunt grupuri de date
; Atat listele cat si vectorii sunt colectii:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; "Sequences" (seqs) are abstract descriptions of lists of data.
; Only lists are seqs.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; O secventa necesita un punct de intrare doar cand este accesata.
; Deci, secventele, care pot fi "lazy" -- pot defini serii infinite:
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (o serie infinita)
(take 4 (range)) ;  (0 1 2 3)

; Folositi cons pentru a adauga un element la inceputul unei liste sau unui vector
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; Conj va adauga un element unei colectii in modul cel mai eficient.
; Pentru liste, aceastea sunt inserate la inceput. Pentru vectori, sunt inserate la final.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; Folositi concat pentru a uni liste sau vectori
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; Folositi filter, map pentru a interactiona cu colectiile
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; Folositi reduce pentru a le reduce
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; Reduce poate lua un argument valoare-initiala
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; Functii
;;;;;;;;;;;;;;;;;;;;;

; Folositi fn pentru a crea functii noi. O functie returneaza intotdeauna
; ultima sa instructiune.
(fn [] "Hello World") ; => fn

; (Necesita paranteze suplimentare pentru a fi apelata)
((fn [] "Hello World")) ; => "Hello World"

; Puteti crea o variabila folosind def
(def x 1)
x ; => 1

; Atribuiti o functie unei variabile
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; Puteti scurta acest proces folosind defn
(defn hello-world [] "Hello World")

; Elementul [] este lista de argumente a functiei.
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; Puteti, de asemenea, folosi aceasta prescurtare pentru a crea functii:
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; Puteti avea si functii cu mai multe variabile
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; Functiile pot primi mai mult argumente dintr-o secventa
(defn count-args [& args]
  (str "Ati specificat " (count args) " argumente: " args))
(count-args 1 2 3) ; => "Ati specificat 3 argumente: (1 2 3)"

; Puteti interschimba argumente normale si argumente-secventa
(defn hello-count [name & args]
  (str "Salut " name ", ati specificat " (count args) " argumente extra"))
(hello-count "Finn" 1 2 3)
; => "Salut Finn, ai specificat 3 argumente extra"


; Maps (Dictionare)
;;;;;;;;;;

; Hash maps si Array maps impart o interfata. Hash maps au cautari mai rapide
; dar nu retin ordinea cheilor.
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Arraymaps de vin automat hashmaps prin majoritatea operatiilor
; daca sunt suficient de mari, asa ca nu trebuie sa va preocupe acest aspect.

; Dictionarele pot folosi orice tip hashable ca si cheie, dar cuvintele cheie
; (keywords) sunt, de obicei, cele mai indicate. Cuvintele cheie sunt ca niste
; siruri de caractere cu un plus de eficienta
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; Apropo, virgulele sunt intotdeauna considerate echivalente cu spatiile.

; Apelati un dictionar (map) ca pe o functie pentru a primi o valoare anume
(stringmap "a") ; => 1
(keymap :a) ; => 1

; Cuvintele cheie pot fi folosite si ele pentru a "cere" dictionarului valorile lor!
(:b keymap) ; => 2

; Nu incercati asta cu siruri de caractere.
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; Recuperarea unei chei inexistente returneaza nil
(stringmap "d") ; => nil

; Folositi assoc pentru a adauga nou chei unui ductionar
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; Dar retineti ca tipurile sunt imuabile in clojure
keymap ; => {:a 1, :b 2, :c 3}

; Folositi dissoc pentru a elimina chei
(dissoc keymap :a :b) ; => {:c 3}

; Seturi (multimi)
;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; Adaugati un membru cu conj
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; Eliminati unul cu disj
(disj #{1 2 3} 1) ; => #{2 3}

; Testati existenta unuia folosing setul ca o functie:
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; Exista mai multe functii in namespace-ul clojure.sets.

; Forme utile
;;;;;;;;;;;;;;;;;

; In Clojure constructiile logice sunt macro-uri, si arata ca
; oricare alta forma
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; Folositi let pentru a crea atribuiri temporare
(let [a 1 b 2]
  (> a b)) ; => false

; Grupati instructiuni impreuna folosind do
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; Functiile contin un do implicit
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; Asemanator pentru let
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")

; Module
;;;;;;;;;;;;;;;

; Folositi "use" pentru a recupera toate functiile dintr-un modul
(use 'clojure.set)

; Acum putem folosi operatiuni pe seturi
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; Puteri de asemenea alege un subset al functiilor de importat
(use '[clojure.set :only [intersection]])

; Folositi require pentru a importa un modul
(require 'clojure.string)

; Folositi / pentru a apela functii dintr-un modul
; In acest caz, modulul este clojure.string, iar functia este blank?
(clojure.string/blank? "") ; => true

; Puteti atribui un nume mai scurt unui modul in momentul importului
(require '[clojure.string :as str])
(str/replace "Acesta este un test." #"[a-o]" str/upper-case) ; => "ACEstA EstE un tEst."
; (#"" denota o expresie regulata)

; Puteti folsi require (sau use, contraindicat) dintr-un namespace folosind :require.
; Nu trebuie sa folositi apostrof pentru module daca procedati astfel.
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java are o biblioteca standard imensa si folositoare, deci
; ar fi util sa stiti cum sa o folositi.

; Folositi import pentru a incarca un modul Java
(import java.util.Date)

; Puteti importa si dintr-un namesopace.
(ns test
  (:import java.util.Date
           java.util.Calendar))

; Folositi numele clasei cu "." la final pentru a crea o noua instanta
(Date.) ; <a date object>

; Folositi . pentru a apela metode. Pe scurt, folositi ".method"
(. (Date.) getTime) ; <a timestamp>
(.getTime (Date.)) ; exact acelasi lucru.

; Folositi / pentru a apela metode statice
(System/currentTimeMillis) ; <a timestamp> (System este prezent intotdeauna)

; Folositi doto pentru a gestiona clase (mutable) mai usor
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => A Date. set to 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; Software Transactional Memory este un mecanism folost de Clojure pentru
; a gestiona stari persistente. Sunt putine instante in care este folosit.

; Un atom este cel mai simplu exemplu. Dati-i o valoare initiala
(def my-atom (atom {}))

; Modificati-l cu swap!.
; swap! primeste o functie si o apeleaza cu valoarea actuala a atomului
; ca prim argument si orice argumente suplimentare ca al doilea
(swap! my-atom assoc :a 1) ; Atomul ia valoarea rezultata din (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Atomul ia valoarea rezultata din (assoc {:a 1} :b 2)

; Folositi '@' pentru a dereferentia atomul si a-i recupera valoarea
my-atom  ;=> Atom<#...> (Returmeaza obiectul Atom)
@my-atom ; => {:a 1 :b 2}

; Aici avem un contor simplu care foloseste un atom
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; Alte utilizari ale STM sunt referintele (refs) si agentii (agents).
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
```

### Lectura suplimentara

Lista nu este in niciun caz exhaustiva, dar speram ca este suficienta pentru
a va oferi un inceput bun in Clojure.

Clojure.org contine multe articole:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org contine documentatie cu exemple pentru majoritatea functiilor de baza:
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure este o metoda excelenta pentru a exersa Clojure/FP (Programarea Functionala):
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org are un numar de article pentru incepatori:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
    - ["Bogdan Lazar", "http://twitter.com/tricinel"]
filename: coffeescript-ro.coffee
lang: ro-ro
---

CoffeeScript este un limbaj de programare care este compilat in Javascript. Nu exista un interpretator la runtime-ul aplicatiei. Fiind unul din successorii Javascript, CoffeeScript incearca sa compileze Javascript usor de citit si performant.

Mai cititi si [website-ul CoffeeScript](http://coffeescript.org/), care contine un tutorial complet Coffeescript.

```coffeescript
# CoffeeScript este un limbaj de hipster.
# Se foloseste de trendurile multor limbaje moderne de programare.
# Comentarii sunt ca in Ruby sau Python.

###
Comentariile in bloc sunt create cu `###`, iar acestea sunt transformate in `/*` si `*/` pentru Javascript

Ar trebuie sa intelegeti Javascript pentru a continua cu acest ghid.
###

# Atribuirea valorilor:
numar   = 42 #=> var numar = 42;
opus = true #=> var opus = true;

# Conditii:
numar = -42 if opus #=> if(opus) { numar = -42; }

# Functii:
laPatrat = (x) -> x * x #=> var laPatrat = function(x) { return x * x; }

plin = (recipient, lichid = "cafea") ->
  "Umplem #{recipient} cu #{cafea}..."
#=>var plin;
#
#plin = function(recipient, lichid) {
#  if (lichid == null) {
#    lichid = "cafea";
#  }
#  return "Umplem " + recipient + " cu " + lichid + "...";
#};

# Liste:
lista = [1..5] #=> var lista = [1, 2, 3, 4, 5];

# Obiecte:
matematica =
  radacina:   Math.sqrt
  laPatrat: laPatrat
  cub:   (x) -> x * square x
#=> var matematica = {
#    "radacina": Math.sqrt,
#    "laPatrat": laPatrat,
#    "cub": function(x) { return x * square(x); }
#   };

# Splats:
cursa = (castigator, alergatori...) ->
  print castigator, alergatori
#=>cursa = function() {
#    var alergatori, castigator;
#    castigator = arguments[0], alergatori = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#    return print(castigator, alergatori);
#  };

# Verificarea existentei:
alert "Stiam eu!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("Stiam eu!"); }

# Operatiuni cu matrice:
cuburi = (math.cube num for num in list)
#=>cuburi = (function() {
#	  var _i, _len, _results;
#	  _results = [];
# 	for (_i = 0, _len = list.length; _i < _len; _i++) {
#		  num = list[_i];
#		  _results.push(math.cube(num));
#	  }
#	  return _results;
# })();

alimente = ['broccoli', 'spanac', 'ciocolata']
mananca aliment for aliment in alimente when aliment isnt 'ciocolata'
#=>alimente = ['broccoli', 'spanac', 'ciocolata'];
#
#for (_k = 0, _len2 = alimente.length; _k < _len2; _k++) {
#  aliment = alimente[_k];
#  if (aliment !== 'ciocolata') {
#    eat(aliment);
#  }
#}
```

## Resurse aditionale

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: Haskell
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["Petru Dimitriu", "http://petru-dimitriu.github.io"]
lang: ro-ro
filename: haskell-ro.html
---

Haskell este un limbaj de programare practic, pur funcțional.

```haskell
-- Comentariile pe o singura linie incep cu 2 cratime.
{- Comentariile multilinie
  se scriu astfel.
-}

----------------------------------------------------
-- 1. Tipuri de date primitive si operatori
----------------------------------------------------

-- Exista numere
3 -- 3

-- Matematica functioneaza ca de obicei
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- Impartirea este cu virgula
35 / 4 -- 8.75

-- Impartirea cu rest
35 `div` 4 -- 8

-- Valorile booleene sunt primitive
True
False

-- Operatii logice
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- In exemplele de mai sus, `not` este o functie ce primeste o valoare.
-- In Haskell nu se pun paranteze pentru apelurile de functie. Toate
-- argumentele sunt insirate dupa numele functiei. Sablonul general este:
-- func arg1 arg2 arg3
-- Vedeti sectiunea despre functii pentru a afla cum sa scrieti propria functie.

-- Caractere si siruri de caractere
"Acesta este un sir de caractere"
'a' -- un caracter
'Nu se pot folosi apostroafe pentru siruri.' -- eroare!

-- Sirurile pot fi concatenate
"Hello " ++ "world!" -- "Hello world!"

-- Un string e de fapt o lista de caractere
['H', 'e', 'l', 'l', 'o'] -- "Hello"
"Acesta este un string" !! 0 -- 'A'


----------------------------------------------------
-- 2. Liste si tupli
----------------------------------------------------

-- Fiecare element dintr-o lista trebuie sa aiba acelasi tip.
-- Urmatoarele liste sunt identice.
[1, 2, 3, 4, 5]
[1..5]

-- Intervalele sunt versatile.
['A'..'F'] -- "ABCDEF"

-- Se poate specifica un pas pentru intervale.
[0,2..10] -- [0, 2, 4, 6, 8, 10]
[5..1] -- Aceasta nu functioneaza deoarece pasul implicit este incrementarea.
[5,4..1] -- [5, 4, 3, 2, 1]

-- indexarea intr-o lista este de la zero
[1..10] !! 3 -- se obtine 4

-- Se pot crea liste infinite
[1..] -- lista tuturor numerelor naturale

-- Listele infinite functioneaza pentru ca Haskell foloseste "evaluare lenesa"
-- adica evalueaza lucrurile doar cand este nevoie de ele. Deci se poate
-- cere al 1000-lea element din lista infinita a numerelor naturale astfel:

[1..] !! 999 -- rezulta 1000

-- Haskell a evaluat elementele 1 - 1000 din lista... dar restul elementelor
-- acestei liste "infinite" nu exista inca! Haskell nu le va evalua pana
-- nu va fi nevoie de ele.

-- concatenarea a doua liste
[1..5] ++ [6..10]

-- alipirea la capul listei
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- operatii cu liste
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- intelegerea listelor
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- folosind o conditie
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- Fiecare element dintr-un tuplu poate fi de un tip diferit
-- dar un tuplu are lungime fixa
-- Un tuplu:
("haskell", 1)

-- accesarea elementelor unui tuplu pereche
fst ("haskell", 1) -- "haskell" (first)
snd ("haskell", 1) -- 1 (second)

----------------------------------------------------
-- 3. Functii
----------------------------------------------------
-- O functie simpla ce sumeaza doua variabile
add a b = a + b

-- Aveti in vedere ca daca folositi ghci (interpretorul Haskell)
-- trebuie sa scrieti in fata si `let`, adica
-- let add a b = a + b

-- Apelarea functiei
add 1 2 -- rezulta 3

-- Numele functiei se poate pune si intre argumente
-- folosind apostrof intors:
1 `add` 2 -- 3

-- Se pot defini functii fara litere in denumire! Astfel se pot
-- defini noi operatori! De exemplu, iata un operator care realizeaza
-- impartirea intreaga
(//) a b = a `div` b
35 // 4 -- rezulta 8

-- Guards: o metoda usoara de a crea ramuri de executie
fib x
  | x < 2 = 1
  | otherwise = fib (x - 1) + fib (x - 2)

-- Potrivirea sirurilor se face similar. Aici am definit 3 definitii
-- pentru fib. Haskell o va alege automat pe prima care se potriveste
-- cu sablonul valorii.
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- Potrivirea in tupli:
foo (x, y) = (x + 1, y + 2)

-- Potrivirea in liste. Aici `x` este primul element al listei,
-- iar `xs` este restul litei. Putem scrie propria functie
-- de mapare
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- Functiile anonime sunt create folosind un backslash urmat
-- de toate argumentele.
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- utilizarea fold (denumit `inject` in alte limbaje) cu o functie
-- anonima. foldl1 inseamna pliere la stanga, folosind prima valoare
-- din lista drept valoarea initiala pentru acumulator
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. Mai multe functii
----------------------------------------------------

-- aplicare partiala; daca nu se introduc toate argumentele unei functii,
-- este "aplicata partial", adica returneaza o noua functie ce primeste
-- restul argumentelor, avand deja setate argumentele introduse

add a b = a + b
foo = add 10 -- foo este o functie ce primeste un numar si ii aduna 10
foo 5 -- 15

-- alta maniera de a scrie acelasi lucru
foo = (10+)
foo 5 -- 15

-- compunerea functiilor
-- operatorul `.` inlantuieste functiile.
-- De exeplu, aici foo este o functie care aduna 10 unui numar, il inmul
-- teste cu 4 si returneaza rezultatul calcului
foo = (4*) . (10+)

-- 4*(10 + 5) = 60
foo 5 -- 60

-- alterarea precedentei
-- Haskell detine un operator numit `$`. Acest operator aplica o functie
-- unui parametru dat. Fata de aplicarea standard a functiilor, care
-- foloseste prioritatea maxim posibila 10 si este asociativa la stanga,
-- operatorul `$` are prioritatea 0 si este asociativ la dreapta.
-- Aceasta inseamna ca expresia de la dreapta este aplicata ca parametru
-- functiei din stanga

-- inainte
even (fib 7) -- false

-- echivalent
even $ fib 7 -- false

-- compunerea functiilor
even . fib $ 7 -- false


----------------------------------------------------
-- 5. Type signatures
----------------------------------------------------

-- Haskell are un sistem de tipuri de date foarte puternic; fiecare expresie
-- valida are un tip.

-- Cateva tipuri de baza:
5 :: Integer
"hello" :: String
True :: Bool

-- Functiile au tipuri de asemenea.
-- `not` primeste un boolean si returneaza un boolean.
-- not :: Bool -> Bool

-- Iata o functie ce primeste doi intregi
-- add :: Integer -> Integer -> Integer

-- Cand se defineste o valoare, este bine sa se precizeze tipul ei deasupra.
double :: Integer -> Integer
double x = x * 2

---------------------------------------------------------
-- 6. Controlul executiei si instructiunile conditionale
---------------------------------------------------------

-- expresia conditionala if
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- cand expresiile sunt pe mai multe linii, este importanta indentarea
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- expresiile de tip case; iata cum se verifica argumentele programului
case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"


-- Haskell nu foloseste cicluri, ci recursie
-- map aplica o functie fiecarui element dintr-o lista

map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- se poate face o functie for folosind map
for array func = map func array

-- si apoi se poate folosi astfel:
for [0..5] $ \i -> show i

-- se poate scrie si asa:
for [0..5] show

-- Se poate folosi foldl sau foldr pentru a reduce o lista
-- foldl <fn> <valoare initiala> <lista>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- Acelasi lucru ca a scrie
(2 * (2 * (2 * 4 + 1) + 2) + 3)

-- foldl functioneaza spre stanga, foldr spre dreapta
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- Acealsi lucru ca:
(2 * 1 + (2 * 2 + (2 * 3 + 4)))

----------------------------------------------------
-- 7. Tipuri de date
----------------------------------------------------

-- Iata cum se creeaza un tip de date in Haskell

data Culoare = Rosu | Albastru | Verde

-- Acum poate fi folosit in functii


spune :: Culoare -> String
spune Rosu = "Esti Rosu!"
spune Albastru = "Esti Albastru!"
spune Verde =  "Esti Verde!"

-- Tipul de date poate avea si parametri.

data Maybe a = Nothing | Just a

-- Toate acestea sunt de tipul Maybe
Just "hello"    -- de tipul `Maybe String`
Just 1          -- de tipul `Maybe Int`
Nothing         -- de tipul `Maybe a` pentru oricare `a`

----------------------------------------------------
-- 8. IO in Haskell
----------------------------------------------------

-- Desi IO nu se poate explica intru totul fara a explica monadele,
-- nu este atat de greu de explicat pentru o idee de baza.

-- Cand se executa un program Haskell, se apeleaza `main`.
-- Trebuie sa returneze o valoare de tio `IO a` pentru un anumit tip `a`.
-- De exemplu:

main :: IO ()
main = putStrLn $ "Hello, sky! " ++ (say Blue)
-- putStrLn are tipul String -> IO ()

-- Cel mai usor se lucreaza cu IO daca se implementeaza programul
-- ca o functie de la String la String. Functia
--    interact :: (String -> String) -> IO ()
-- citeste un text, executa o functie asupra ei, apoi afiseaza
-- iesirea.

countLines :: String -> String
countLines = show . length . lines

main' = interact countLines

-- O valoare de tipul `IO ()` poate fi privita ca reprezentand
-- o secventa de actiuni pe care care computerul sa le execute,
-- similar cu felul in care un program este scris intr-un limbaj
-- imperativ. Putem folosi notatia `do` pentru a inlantui actiunile.
-- De exemplu:

sayHello :: IO ()
sayHello = do
   putStrLn "What is your name?"
   name <- getLine -- citeste o linie
   putStrLn $ "Hello, " ++ name

-- Exercise: Scrieti propria functie `interact` care citeste
--           o singura linie de la intrare.


-- Codul din `sayHello` nu va fi niciodata executat. Singura actiunile
-- care este executata este valoarea lui `main`.
-- Pentru a rula `sayHello.`, eliminati definitia de mai sus a `main`.
-- si inlocuiti-o cu
--   main = sayHello

-- Sa intelegem mai bine cum functioneaza functia `getLine`.
-- Tipul ei este:
--    getLine :: IO String
-- Pueti privi o valoare de tipul `IO a` ca fiind un program
-- de computer care va genera o valoare de tipul `a` cand
-- este executata (pe langa orice altceva face). O putem denumi
-- si refolosi utilizand `<-`. De asemenea putem face propriile
-- actiuni te tipul `IO String`:

action :: IO String
action = do
   putStrLn "Aceasta e o linie."
   input1 <- getLine
   input2 <- getLine
   --Tipul instructiunii `do` este cel de pe ultima sa linie.
   -- `return` nu este un cuvant cheie, ci o functie
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- Putem folosi aceasta exact cum am folosit `getLine`:

main'' = do
    putStrLn "I will echo two lines!"
    result <- action
    putStrLn result
    putStrLn "This was all, folks!"

-- Tipul `IO` este un exemplu de "monada". Felul in care Haskell foloseste
-- o monada pentru a realiza opeartii de intrare si iesire il face un limbaj
-- pur functional. Orice functie care interactioneaza cu exteriorul (adica
-- realieaza IO) este marcata ca `IO` in semnatura ei. Aceasta ne permite
-- sa spunem ce functii sunt "pure", adica nu interactioneaza cu exteriorul.

-- Aceasta este o facilitate foarte puternica, deoarece este usor sa
-- se ruleze functii pure concurent; asadar, concurenta in Haskell se face usor

----------------------------------------------------
-- 9. REPL in Haskell
----------------------------------------------------

-- Se porneste introducand `ghci`.
-- Dupa aceasta, se poate introduce cod Haskell.
-- Toate valorile noi trebuie precedate de `let`.

let foo = 5

-- Puteti vedea tipul oricarei valori sau expresii cu `:t`.

> :t foo
foo :: Integer

-- Operatorii, precum `+`, `:` si `$` sunt functii.
-- Tipul lor poate fi observat punand operatorii intre paranteze.

> :t (:)
(:) :: a -> [a] -> [a]

-- Se pot obtine informatii despre fiecare nume folosind `:i`

> :i (+)
class Num a where
  (+) :: a -> a -> a
  ...
    -- Defined in ‘GHC.Num’
infixl 6 +

--De asemenea se poate executa orice actiune de tipul `IO ()`

> sayHello
What is your name?
Friend!
Hello, Friend!

```
Mai sunt multe de spus despre Haskell, printre care typclasses și monade.
Acestea sunt marile idei care fac programarea în Haskell atât de interesantă.
Vă las un exemplu final în Haskell: o variantă de implementare a sortării rapide
(quicksort) în Haskell:

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

Există două maniere populare de a instala Haskell: prin [instalarea bazată pe Cabal](http://www.haskell.org/platform/), și prin mai noul [proces bazat pe Stack](https://www.stackage.org/install).

Se poate găsi o introducere în Haskell mult mai blândă la adresele
[Learn you a Haskell](http://learnyouahaskell.com/) sau
[Real World Haskell](http://book.realworldhaskell.org/).
---
language: json
filename: learnjson-ro.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
    - ["Serban Constantin", "https://github.com/fuzzmz"]
lang: ro-ro
---

Deoarece JSON este un fromat foarte simplu de schimb de date acesta va fi
probabil cel mai simplu Invata X in Y minute.

JSON in forma cea mai pura nu contine comentarii insa majoritatea parserelor
vor accepta comentarii in stil C (`//`, `/* */`). Pentru acest caz insa totul
va fi JSON 100% valid. Din fericire codul vorbeste de la sine.

```json
{
  "cheie": "valoare",
  
  "chei": "trebuie mereu inconjurate de ghilimele",
  "numere": 0,
  "stringuri": "Bunã. Tot setul unicode este permis, chiar si \"escaping\".",
  "are booleane?": true,
  "nimic": null,

  "numere mari": 1.2e+100,

  "obiecte": {
    "comentariu": "Majoritatea structurii va veni din obiecte.",

    "vectori": [0, 1, 2, 3, "Vectorii pot avea orice in ei.", 5],

    "alt obiect": {
      "comentariu": "Lucrurile pot fi subordonate. Foarte util."
    }
  },

  "glumite": [
    {
      "surse de potasiu": ["banane"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],
  
  "stil alternativ": {
    "comentariu": "ia uite la asta!"
  , "pozitia virgulei": "nu conteaza - daca e inaintea valorii atunci e valida"
  , "alt comentariu": "ce dragut"
  },

  "a fost scurt": "Am terminat. Acum stii tot ce are JSON de oferit."
}
```
---
language: latex
contributors:
    - ["Chaitanya Krishna Ande", "http://icymist.github.io"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
    - ["Sricharan Chiruvolu", "http://sricharan.xyz"]
    - ["Ramanan Balakrishnan", "https://github.com/ramananbalakrishnan"]
translators:
    - ["Petru Dimitriu", "http://petru-dimitriu.github.io"]
filename: learn-latex-ro.tex
lang: ro-ro
---

```tex
% Toate comentariile încep cu %
% Nu există comentarii multi-linie

% LaTeX NU este un program software de procesare text de tipul
% "What You See Is What You Get"
% precum MS Word, sau OpenOffice Writer

% Toate comenzile LaTeX încep cu backslash. (\)

% Documentele LaTeX încep cu o linie care definește tipul documentului
% care urmează a fi compilat. Alte tipuri de documente sunt book (carte),
% presentation (prezentare), etc. Opțiunile pentru document apar
% între paranteze drepte. În acest caz, specificăm că vrem să folosim
% un corp de text (font) de 12 puncte.
\documentclass[12pt]{article}

% Mai apoi definim pachetele pe care documentul le folosește.
% Dacă vreți să includeți grafice, text colorat sau
% cod sursă din alt fișier în documentul dumneavoastră,
% trebuie să îmbogățiți capabilitățile LaTeX. Aceasta se realizează
% adăugând pachete. Voi include pachetele float și caption pentru
% imagini.
\usepackage{caption}
\usepackage{float}
% această comandă este necesară atunci când vreți să scrieți codul
% sursă folosind diacrtice! (cum e cazul aici, unde translatorul
% a vrut să scrie neapărat folosind diacriticele românești)
\usepackage[utf8]{inputenc}

% De asemenea, putem defini și alte proprietăți pentru documente.
\author{Chaitanya Krishna Ande, Colton Kohnke \& Sricharan Chiruvolu \\ Traducere de Petru Dimitriu}
\date{\today}
\title{Învățați LaTeX în Y minute!}

% Suntem gata să începem documentul.
% Tot ce se află înaintea acestei linii se numește "Preambul"
\begin{document}
% dacă am setat autorul, data și titlul, putem cere LaTeX să
% creeze o pagină de titlu
\maketitle

% Cele mai multe documente științifice au un rezumat; puteți folosi comenzile
% predefinite pentru acesta. Acesta ar trebui să apară, așa cum ar fi logic,
% după titlu, dar înainte de secțiunile principale ale corpului.
% Această comandă este disponibilă în clasele de document article și report.
\begin{abstract}
 Documentațue LaTeX scrisă în LaTeX. O idee nicidecum nouă și nicidecum a mea!
\end{abstract}

% Comenzile pentru secțiuni sunt intuitive.
% Toate titlurile secțiunilor sunt adăugate automat la tabla de materii (cuprins).
\section{Introducere}
Salut, mă numesc Petru. Astăzi vom învăța împreună LaTeX!

\section{Altă secțiune}
Acesta este textul pentru altă secțiune. Vom face o subsecțiune.

\subsection{Aceasta este o subsecțiune}
Și încă una.

\subsubsection{Pitagora}
Mult mai bine.
\label{subsec:pitagora}

% Folosind asteriscul putem suprima numărătoarea automată a LaTeX.
% Aceasta funcționează și pentru alte comenzi LaTeX.
\section*{Secțiune fără numerotare}
Totuși nu toate secțiunile trebuie să fie nenumerotate!

\section{Note despre text}
În general LaTeX se pricepe să pună textul unde trebuie. Dacă o linie are \\
nevoie \\ să \\ fie \\ întreruptă, puteți adăuga două caractere backslash
la codul sursă.

\section{Liste}
Listele sunt printre cel mai simplu de făcut lucruri în LaTeX! Mâine merg la
cumpărături așa că fac o listă:
\begin{enumerate} % Aceasta creează un mediu "enumerate"
  % \item spune mediului "enumerate" să incrementeze
  \item salată
  \item 27 pepeni
  \item un singur iepuroi
  % putem suprascrie numărul elementului folosind []
  \item[câte?] conserve de ton

  Nu este un element din listă, dar încă face parte din "enumerate".

\end{enumerate} % Toate mediile trebuie să aibă o instrucțiune de încheiere.

\section{Matematică}

Una dintre principalele întrebuințări ale LaTeX este realizarea
articolelor academice sau a fișelor tehnice, de obicei aflate în
universul matematicii și științelor exacte. Astfel, trebuie să putem
adăuga simboluri speciale în documentul nostru! \\

Matematica are multe simboluri, mult mai multe decât se găsesc
pe o tastatură - printre ele, simboluri pentru mulțimi și relații,
săgeți, operatori și litere grecești.\\

Mulțimile și relațiile sunt esențiale în lucrările științifce matematice.
Iată cum se scrie: toți y aparținând lui X.\\
$\forall$ x $\in$ X. \\

% Observați cum am avut nevoie să pun semnul $ înainte și după simboluri.
% Aceasta pentru că atunci când scriem, suntem în modul text (text-mode).
% Totuși simbolurile matematice există numai în modul matematic (math-mode).
% Când ne aflăm în text-mode, putem scrie texte în math-mode punând $ înainte
% și după simboluri. La fel și viceversa. Și variabilele pot fi redate
% în math-mode. Putem intra în math-mode și scriind \[\].

\[a^2 + b^2 = c^2 \]

Îmi place litera $\xi$. De asemenea îmi plac $\beta$, $\gamma$ și
$\sigma$. Nu există nicio literă grecească necunoscută pentru LaTeX!

Operatorii sunt esențiali într-un document matematic!
funcțiile trigonometrice ($\sin$, $\cos$, $\tan$),
logaritmii și exponențialele ($\log$, $\exp$),
limitele ($\lim$), etc.
au comenzi definite în LaTeX pentru fiecare.
Să vedem cum scriem o ecuație: \\

$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$

Fracțiile (numărător - numitor) pot fi scrise astfel:
% 10 / 7
$^{10}/_{7}$ \\

% Fracții relativ complexe pot fi scrie ca
% \frac{numărător}{numitor}
$\frac{n!}{k!(n - k)!}$ \\

Putem insera ecuații și într-un "mediu pentru ecuații".

% Afișează text matematic într-un mediu pentru ecuații.
\begin{equation} % intră în math-mode
    c^2 = a^2 + b^2.
    \label{eq:pitagora} % pentru referențiere
\end{equation}
% toate instrucțiunile cu \begin trebuie să fie cuplate cu o instrucțiune cu \end

Putem referenția noua noastră ecuație!
~\ref{eq:pitagora} este cunoscută și ca Teorema lui Pitagora, despre care vorbim și la Sec.~\ref{subsec:pitagora}. Multe lucruri prot fi etichetate:
figuri, ecuații, secțiuni, etc.

Sumele discrete și integralele se scriu cu comenzile sum și int.

% Unele compilatoare LaTeX nu acceptă să existe linii goale
% într-un mediu pentru ecuații.
\begin{equation}
  \sum_{i=0}^{5} f_{i}
\end{equation}
\begin{equation}
  \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x
\end{equation}

\section{Figuri}

Să inserăm o figură. Așezarea figurilor poate fi ușor dificilă.
Eu trebuie să mă uit peste opțiunile de așezare de fiecare dată.

\begin{figure}[H] % H denumește opțiunle de așezare
    \centering % centrează figura pe pagină
    % Inserează o figură scalată la 0.8 din lățimea paginii.
    %\includegraphics[width=0.8\linewidth]{right-triangle.png}
    % Comentat pentru a nu împiedica fișierul să compileze.
    \caption{Triunghi dreptunghic cu laturile $a$, $b$, $c$}
    \label{fig:right-triangle}
\end{figure}

\subsection{Tabel}
Putem insera tabele la fel cum inserăm figuri.

\begin{table}[H]
  \caption{Descriere pentru tabel}
  % argumentele {} controlează cum vor fi afișate coloanele
  \begin{tabular}{c|cc}
    Număr &  Nume & Prenume \\ % Numele coloanelor sunt separate prin $
    \hline % a linie orizonală
    1 & Popescu & Ion \\
    2 & Sima & Felix
  \end{tabular}
\end{table}

% \section{Hyperlinkuri} % În curând

\section{Cum facem ca LaTeX să nu compileze ceva (de exemplu cod sursă)}
Să zicem că vrem să includem niște cod în documentul nostru LaTeX.
Vom avea nevoie ca LaTeX să nu încerce să interpreteze acel cod,
ci doar să îl redea în document. Vom face asta cu un mediu verbatim.

% Există și alte pachete (i.e. minty, lstlisting, etc.)
% dar verbatim este pachetul cel mai simplu.
\begin{verbatim}
  print("Salut lume!")
  a%b; % hei! putem folosi % în verbatim
  random = 4;
\end{verbatim}

\section{Compilarea}
Acum vă întrebați cum se compilează acest document minunat și să vă
minunați de rezultat, un PDF LaTeX. (da, documentul acesta chiar
compilează). \\
Realizarea documentului cu LaTeX va parcurge următorii pași:
  \begin{enumerate}
    \item Se scrie documentul în text simplu. (codul sursă)
    \item Se compilează documentul pentru a produce un PDF.
     Compilarea arată cam așa în Linux:\\
     \begin{verbatim}
        $pdflatex learn-latex.tex learn-latex.pdf
     \end{verbatim}
  \end{enumerate}

Anumite editoare pentru LaTeX combină pașii 1 și 2 în același produs software.
Așadar, dacă vreți să vedeți realizați pasul 1 dar nu și pasul 2, el se poate
realiza "în spate".

Scrieți toate informațiile de formatare în pasul 1. Compilarea din pasul 2
se ocupă de producerea documentului în formatul definit în pasul 1.

\section{Final}

Asta e tot pentru moment!

% De multe ori veți vrea să aveți o secțiune cu bibliografie în document.
% Cea mai ușoară modalitate este folosind mediul thebibliography.
\begin{thebibliography}{1}
  % Similar celorlalte liste, comanda \bibitem e folosită pentru a înșirui
  % elemente; fiecare element poate fi citat în interiorul textului
  \bibitem{latexwiki} Uimitoarea carte wiki LaTeX: {\em https://en.wikibooks.org/wiki/LaTeX}
  \bibitem{latextutorial} Un tutorial propriu-zis: {\em http://www.latex-tutorial.com}
\end{thebibliography}

% încheie documentul
\end{document}
```

## Mai multe despre LaTeX

* Uimitoarea carte wiki LaTeX: [https://en.wikibooks.org/wiki/LaTeX](https://en.wikibooks.org/wiki/LaTeX)
* Un tutorial propriu-zis: [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/)
---
language: python
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["Ovidiu Ciule", "https://github.com/ociule"]
filename: learnpython-ro.py
lang: ro-ro
---

Python a fost creat de Guido Van Rossum la începutul anilor '90. Python a
devenit astăzi unul din cele mai populare limbaje de programare.
M-am indrăgostit de Python pentru claritatea sa sintactică. Python este aproape
pseudocod executabil.

Opinia dumneavoastră este binevenită! Puteţi sa imi scrieţi la [@ociule](http://twitter.com/ociule)
sau ociule [at] [google's email service]

Notă: Acest articol descrie Python 2.7, dar este util şi pentru Python 2.x.
O versiune Python 3 va apărea în curând, în limba engleză mai întâi.

```python
# Comentariile pe o singură linie încep cu un caracter diez.
""" Şirurile de caractere pe mai multe linii pot fi încadrate folosind trei caractere ", şi sunt des
    folosite ca şi comentarii pe mai multe linii.
"""

####################################################
## 1. Operatori şi tipuri de date primare 
####################################################

# Avem numere
3 #=> 3

# Matematica se comportă cum ne-am aştepta
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# Împărţirea este un pic surprinzătoare. Este de fapt împărţire pe numere
# întregi şi rotunjeşte
# automat spre valoarea mai mică
5 / 2 #=> 2

# Pentru a folosi împărţirea fără rest avem nevoie de numere reale 
2.0     # Acesta e un număr real
11.0 / 4.0 #=> 2.75 ahhh ... cum ne aşteptam 

# Ordinea operaţiilor se poate forţa cu paranteze
(1 + 3) * 2 #=> 8

# Valoriile boolene sunt şi ele valori primare
True
False

# Pot fi negate cu operatorul not
not True #=> False
not False #=> True

# Egalitatea este ==
1 == 1 #=> True
2 == 1 #=> False

# Inegalitate este !=
1 != 1 #=> False
2 != 1 #=> True

# Comparaţii
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# Comparaţiile pot fi inlănţuite! 
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Şirurile de caractere pot fi încadrate cu " sau '
"Acesta e un şir de caractere."
'Şi acesta este un şir de caractere.'

# Şirurile de caractere pot fi adăugate!
"Hello " + "world!" #=> "Hello world!"

# Un şir de caractere poate fi folosit ca o listă 
"Acesta e un şir de caractere"[0] #=> 'A'

# Caracterul % (procent) poate fi folosit pentru a formata şiruri de caractere :
"%s pot fi %s" % ("şirurile", "interpolate")

# O metodă mai nouă de a formata şiruri este metoda "format"
# Este metoda recomandată
"{0} pot fi {1}".format("şirurile", "formatate")
# Puteţi folosi cuvinte cheie dacă nu doriţi sa număraţi 
"{nume} vrea să mănânce {fel}".format(nume="Bob", fel="lasagna")

# "None", care reprezintă valoarea nedefinită, e un obiect
None #=> None

# Nu folosiţi operatorul == pentru a compara un obiect cu None
# Folosiţi operatorul "is"
"etc" is None #=> False
None is None  #=> True

# Operatorul "is" testeaza dacă obiectele sunt identice.
# Acastă operaţie nu e foarte folositoare cu tipuri primare,
# dar e foarte folositoare cu obiecte.

# None, 0, şi şiruri de caractere goale sunt evaluate ca si fals, False. 
# Toate celelalte valori sunt adevărate, True.
0 == False  #=> True
"" == False #=> True


####################################################
## 2. Variabile şi colecţii
####################################################

# Printarea este uşoară 
print "Eu sunt Python. Încântat de cunoştinţă!" 


# Nu este nevoie sa declari variabilele înainte de a le folosi 
o_variabila = 5    # Convenţia este de a folosi caractere_minuscule_cu_underscore 
o_variabila #=> 5

# Dacă accesăm o variabilă nefolosită declanşăm o excepţie.
# Vezi secţiunea Control de Execuţie pentru mai multe detalii despre excepţii.
alta_variabila # Declanşează o eroare de nume 

# "If" poate fi folosit într-o expresie.
"yahoo!" if 3 > 2 else 2 #=> "yahoo!"

# Listele sunt folosite pentru colecţii
li = []
# O listă poate avea valori de la început 
alta_li = [4, 5, 6]

# Se adaugă valori la sfârşitul lister cu append 
li.append(1)    #li e acum [1]
li.append(2)    #li e acum [1, 2]
li.append(4)    #li e acum [1, 2, 4]
li.append(3)    #li este acum [1, 2, 4, 3]
# Se şterg de la sfarşit cu pop 
li.pop()        #=> 3 şi li e acum [1, 2, 4]
# Să o adaugăm înapoi valoarea
li.append(3)    # li e din nou [1, 2, 4, 3]

# Putem accesa valorile individuale dintr-o listă cu operatorul index 
li[0] #=> 1
# Valoarea speciala -1 pentru index accesează ultima valoare
li[-1] #=> 3

# Dacă depaşim limitele listei declanşăm o eroare IndexError
li[4] # Declanşează IndexError

# Putem să ne uităm la intervale folosind sintaxa de "felii" 
# În Python, intervalele sunt închise la început si deschise la sfârşit. 
li[1:3] #=> [2, 4]
# Fără început
li[2:] #=> [4, 3]
# Fără sfarşit
li[:3] #=> [1, 2, 4]

# Putem şterge elemente arbitrare din lista cu operatorul "del" care primeşte indexul lor
del li[2] # li e acum [1, 2, 3]

# Listele pot fi adăugate
li + alta_li #=> [1, 2, 3, 4, 5, 6] - Notă: li si alta_li nu sunt modificate! 

# Concatenăm liste cu "extend()"
li.extend(alta_li) # Acum li este [1, 2, 3, 4, 5, 6]

# Se verifică existenţa valorilor in lista cu "in" 
1 in li #=> True

# Şi lungimea cu "len()"
len(li) #=> 6


# Tuplele sunt ca şi listele dar imutabile 
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # Declanşează TypeError

# Pot fi folosite ca şi liste 
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Tuplele pot fi despachetate 
a, b, c = (1, 2, 3)     # a este acum 1, b este acum 2 şi c este acum 3
# Tuplele pot fi folosite şi fără paranteze 
d, e, f = 4, 5, 6
# Putem inversa valori foarte uşor! 
e, d = d, e     # d este acum 5 şi e este acum 4


# Dicţionarele stochează chei şi o valoare pentru fiecare cheie
dict_gol = {}
# Şi un dicţionar cu valori 
dict_cu_valori = {"unu": 1, "doi": 2, "trei": 3}

# Căutaţi valori cu []
dict_cu_valori["unu"] #=> 1

# Obţinem lista cheilor cu "keys()"
dict_cu_valori.keys() #=> ["trei", "doi", "unu"]
# Notă - ordinea cheilor obţinute cu keys() nu este garantată. 
# Puteţi obţine rezultate diferite de exemplul de aici. 

# Obţinem valorile cu values() 
dict_cu_valori.values() #=> [3, 2, 1]
# Notă - aceeaşi ca mai sus, aplicată asupra valorilor.

# Verificăm existenţa unei valori cu "in" 
"unu" in dict_cu_valori #=> True
1 in dict_cu_valori #=> False

# Accesarea unei chei care nu exista declanşează o KeyError
dict_cu_valori["four"] # KeyError

# Putem folosi metoda "get()" pentru a evita KeyError
dict_cu_valori.get("one") #=> 1
dict_cu_valori.get("four") #=> None
# Metoda get poate primi ca al doilea argument o valoare care va fi returnată
# când cheia nu este prezentă. 
dict_cu_valori.get("one", 4) #=> 1
dict_cu_valori.get("four", 4) #=> 4

# "setdefault()" este o metodă pentru a adăuga chei-valori fără a le modifica, dacă cheia există deja
dict_cu_valori.setdefault("five", 5) #dict_cu_valori["five"] este acum 5
dict_cu_valori.setdefault("five", 6) #dict_cu_valori["five"] exista deja, nu este modificată, tot 5


# Set este colecţia mulţime
set_gol = set()
# Putem crea un set cu valori
un_set = set([1,2,2,3,4]) # un_set este acum set([1, 2, 3, 4]), amintiţi-vă ca mulţimile garantează unicatul!

# În Python 2.7, {} poate fi folosit pentru un set 
set_cu_valori = {1, 2, 2, 3, 4} # => {1 2 3 4}

# Putem adăuga valori cu add 
set_cu_valori.add(5) # set_cu_valori este acum {1, 2, 3, 4, 5}

# Putem intersecta seturi 
alt_set = {3, 4, 5, 6}
set_cu_valori & alt_set #=> {3, 4, 5}

# Putem calcula uniunea cu |
set_cu_valori | alt_set #=> {1, 2, 3, 4, 5, 6}

# Diferenţa între seturi se face cu -
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Verificăm existenţa cu "in" 
2 in set_cu_valori #=> True
10 in set_cu_valori #=> False


####################################################
## 3. Controlul Execuţiei
####################################################

# O variabilă 
o_variabila = 5

# Acesta este un "if". Indentarea este importanta în python!
# Printează "o_variabila este mai mică ca 10"
if o_variabila > 10:
    print "o_variabila e mai mare ca 10."
elif o_variabila < 10:    # Clauza elif e opţională. 
    print "o_variabila este mai mică ca 10."
else:           # Şi else e opţional.
    print "o_variabila este exact 10."


"""
Buclele "for" pot fi folosite pentru a parcurge liste
Vom afişa:
    câinele este un mamifer 
    pisica este un mamifer
    şoarecele este un mamifer
"""
for animal in ["câinele", "pisica", "şoarecele"]:
    # Folosim % pentru a compune mesajul 
    print "%s este un mamifer" % animal
    
"""
"range(număr)" crează o lista de numere 
de la zero la numărul dat
afişează:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
While repetă pana când condiţia dată nu mai este adevărată. 
afişează:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Prescurtare pentru x = x + 1

# Recepţionăm excepţii cu blocuri try/except 

# Acest cod e valid in Python > 2.6:
try:
    # Folosim "raise" pentru a declanşa o eroare 
    raise IndexError("Asta este o IndexError")
except IndexError as e:
    pass    # Pass nu face nimic. În mod normal aici ne-am ocupa de eroare. 


####################################################
## 4. Funcţii
####################################################

# Folosim "def" pentru a defini funcţii 
def add(x, y):
    print "x este %s şi y este %s" % (x, y)
    return x + y    # Funcţia poate returna valori cu "return" 

# Apelăm funcţia "add" cu parametrii 
add(5, 6) #=> Va afişa "x este 5 şi y este 6" şi va returna 11 

# Altă cale de a apela funcţii: cu parametrii numiţi 
add(y=6, x=5)   # Ordinea parametrilor numiţi nu contează 

# Putem defini funcţii care primesc un număr variabil de parametrii nenumiţi
# Aceşti parametrii nenumiţi se cheamă si poziţinali
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# Şi putem defini funcţii care primesc un număr variabil de parametrii numiţi
def keyword_args(**kwargs):
    return kwargs

# Hai să vedem cum merge 
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# Se pot combina 
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) va afişa:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Când apelăm funcţii, putem face inversul args/kwargs!
# Folosim * pentru a expanda tuple şi ** pentru a expanda kwargs.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # echivalent cu foo(1, 2, 3, 4)
all_the_args(**kwargs) # echivalent cu foo(a=3, b=4)
all_the_args(*args, **kwargs) # echivalent cu foo(1, 2, 3, 4, a=3, b=4)

# În Python, funcţiile sunt obiecte primare
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# Funcţiile pot fi anonime 
(lambda x: x > 2)(3) #=> True

# Există funcţii de ordin superior (care operează pe alte funcţii) predefinite 
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Putem folosi scurtături de liste pentru a simplifica munca cu map si filter 
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Clase
####################################################

# Moştenim object pentru a crea o nouă clasă 
class Om(object):

    # Acesta este un atribut al clasei. Va fi moştenit de toate instanţele. 
    species = "H. sapiens"

    # Constructor (mai degrabă, configurator de bază) 
    def __init__(self, nume):
        # Valoarea parametrului este stocată in atributul instanţei 
        self.nume = nume

    # Aceasta este o metoda a instanţei.
    # Toate metodele primesc "self" ca si primul argument.
    def spune(self, mesaj):
       return "%s: %s" % (self.nume, mesaj)

    # O metodă a clasei. Este partajată de toate instanţele.
    # Va primi ca si primul argument clasa căreia îi aparţine. 
    @classmethod
    def get_species(cls):
        return cls.species

    # O metoda statica nu primeste un argument automat. 
    @staticmethod
    def exclama():
        return "*Aaaaaah*"


# Instanţiem o clasă 
i = Om(nume="Ion")
print i.spune("salut")  # afişează: "Ion: salut"

j = Om("George")
print j.spune("ciau")   # afişează George: ciau"

# Apelăm metoda clasei 
i.get_species() #=> "H. sapiens"

# Modificăm atributul partajat 
Om.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# Apelăm metoda statică 
Om.exclama() #=> "*Aaaaaah*"


####################################################
## 6. Module
####################################################

# Pentru a folosi un modul, trebuie importat 
import math
print math.sqrt(16) #=> 4

# Putem importa doar anumite funcţii dintr-un modul 
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# Putem importa toate funcţiile dintr-un modul, dar nu este o idee bună
# Nu faceţi asta!
from math import *

# Numele modulelor pot fi modificate la import, de exemplu pentru a le scurta 
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Modulele python sunt pur şi simplu fişiere cu cod python. 
# Puteţi sa creaţi modulele voastre, şi sa le importaţi.
# Numele modulului este acelasi cu numele fişierului.

# Cu "dir" inspectăm ce funcţii conţine un modul
import math
dir(math)


```

## Doriţi mai mult?

### Gratis online, în limba engleză

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)

### Cărţi, în limba engleză

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: ruby
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
translators:
  - ["Adrian Bordinc", "https://github.com/ellimist"]
filename: learnruby-ro.rb
lang: ro-ro
---

```ruby
# Acesta este un comentariu

=begin
Acesta este un comentariu pe mai multe linii
Nimeni nu le foloseste
Si nici tu nu ar trebui sa o faci
=end

# In primul rand: totul este un obiect

# Numerele sunt obiecte

3.class #=> Fixnum

3.to_s #=> "3"


# Aritmetica de baza
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# Aritmetica este doar "zahar sintactic"
# pentru a putea chema metode pe un obiect
1.+(3) #=> 4
10.* 5 #=> 50

# Valorile speciale sunt obiecte
nil # Nimic
true # true
false # false

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Egalitate
1 == 1 #=> true
2 == 1 #=> false

# Inegalitate
1 != 1 #=> false
2 != 1 #=> true
!true  #=> false
!false #=> true

# Excluzand "false", "nil" este singura valoare "falsa"

!nil   #=> true
!false #=> true
!0     #=> false

# Mai multe comparatii
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Sirurule de caractere sunt obiecte

'Sunt un sir de caractere'.class #=> String
"Si eu sunt un sir de caractere".class #=> String

fi_inlocuit = "fi inlocuit"
"Pot #{fi_inlocuit} atunci cand folosesc dublu apostrof"
#=> "Pot fi inlocuit atunci cand folosesc dublu apostrof"


# Printeaza 
puts "Afisez rezultate!"

# Variabile
x = 25 #=> 25
x #=> 25

# Retineti faptul ca atribuire unei valori, o si returneaza pe aceasta
# Asta inseamna ca poti sa faci atribuire multipla:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Prin conventie se foloseste "snake_case" in denumirea variabilelor
snake_case = true

# Folositi nume descriptive pentru variablie
adresa_radacina_proiect = '/nume/bun/'
adresa = '/nume/nu atat de bun/'

# Simbolurile (sunt obiecte)
# Simbolurile sunt constante imutabile, reutilizabile, reprezentate intern 
# de o valoare numerica. Sunt deseori folosite in locul sirurilor de caractere 
# pentru a da un nume reprezentativ unei valori

:exemplu_simbol.class #=> Symbol

status = :exemplu_simbol

status == :exemplu_simbol #=> adevarat

status == 'exemplu_simbol' #=> fals

status == :aprobat #=> fals

# Vectori

# Acesta este un vector
vector = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Vectorii pot contine diferite tipuri de date

[1, "salut", false] #=> [1, "salut", false]

# Vectorii pot fi indexati
# de la inceput
vector[0] #=> 1
vector[12] #=> nil

# Ca si aritmetica, accessul [valoare]
# este doar "zahar sintactic"
# pentru a chema metoda [] a unui obiect
vector.[] 0 #=> 1
vector.[] 12 #=> nil

# De la sfarsit
vector[-1] #=> 5

# Cu un index de inceput si o lungime
vector[2, 3] #=> [3, 4, 5]

# Sau cu un interval
vector[1..3] #=> [2, 3, 4]

# Adauga elemente intr-un vector in felul urmator:
vector << 6 #=> [1, 2, 3, 4, 5, 6]

# Hash-urile sunt dictionarele din Ruby cu perechi cheie/valoare.
# Hash-urile sunt notate cu acolade
hash = {'culoare' => 'verde', 'numar' => 5}

hash.keys #=> ['culoare', 'numar']

# Poti lua valoare unui element dintr-un hash foarte rapid folosind cheia
hash['culoare'] #=> 'verde'
hash['numar'] #=> 5

# Incercand sa accesezi un element dintr-un hash 
# printr-o cheie care nu exista va returna "nil".
hash['nimic_aici'] #=> nil

# Incepand cu Ruby 1.9, este o sintaxa speciala 
# pentru atunci cand se folosesc simboluri drept chei:

hash_nou = { defcon: 3, actiune: true}

hash_now.keys #=> [:defcon, :actiune]

# Pont: Atat vectorii (Array) si hash-urile (Hash) sunt enumerabile (Enumerable)
# Ele impart o multime de metode utile precum each, map, count si altele


# Structuri de control

if true
  "instructiune if"
elsif false
  "else if, optional"
else
  "else, de asemenea optional"
end

for numar in 1..5
  puts "iteratia #{numar}"
end
#=> iteratia 1
#=> iteratia 2
#=> iteratia 3
#=> iteratia 4
#=> iteratia 5

# TOTUSI, Nici una nu foloseste instructiunea for
# In locul acesteia ar trebui sa folosesti metoda "each" si sa ii trimiti un block
# Un bloc este o bucata de cod pe care o poti trimite unei metode precum "each".
# Este analog pentru "lambda", functii anonime,
# sau closures in alte limbaje de programare.
#
# Metoda "each" a unui interval, ruleaza block-ul o data
# pentru fiecare element din interval.
# Block-ul primeste ca si parametru un index
# Invocand metoda "each" cu un block, arata in urmatorul fel:

(1..5).each do |index|
  puts "iteratia #{index}"
end
#=> iteratia 1
#=> iteratia 2
#=> iteratia 3
#=> iteratia 4
#=> iteratia 5

# Poti de asemenea sa pui block-ul intre acolade
(1..5).each {|index| puts "iteratia #{index}"}

# Continutul unei structuri de date poate fi parcurs folosind "each".
array.each do |element|
  puts "#{element} parte din vector"
end
hash.each do |cheie, valoare|
  puts "#{cheie} este #{valoare}"
end

index = 1
while index <= 5 do
  puts "iteratia #{index}"
  index += 1
end
#=> iteratia 1
#=> iteratia 2
#=> iteratia 3
#=> iteratia 4
#=> iteratia 5

nota = 'B'

case nota
when 'A'
  puts "Bravo pustiule!"
when 'B'
  puts "Mai mult noroc data viitoare"
when 'C'
  puts "Poti mai mult"
when 'D'
  puts "Incet, incet..."
when 'F'
  puts "Ai esuat!"
else
  puts "Sistem de notare alternativ?!"
end

# Functii

def dublu(x)
  x * 2
end

# Functille (si toate block-urile) 
# returneaza implicit valoarea ultimei instructiuni
dublu(2) #=> 4

# Parantezele sunt optionale cand rezultatul nu este ambiguu
dublu 3 #=> 6

dublu dublu 3 #=> 12

def suma(x,y)
  x + y
end

# Argumentele metodei sunt separate printr-o virgula
suma 3, 4 #=> 7

suma suma(3,4), 5 #=> 12

# yield
# Toate metodele au un parametru block, implicit si optional
# care poate fi invocat folosit cuvantul cheie 'yield'

def incercuieste
  puts "{"
  yield
  puts "}"
end

incercuieste { puts 'Salut Mihai!' }

# {
# Salut Mihai!
# }


# Poti trimite un block unei functii.
# "&" marcheaza o referinta trimisa unui block
def vizitatori(&block)
 block.call "un_parametru" 
end
 
# Poti trimite o lista de argumente, care va fi convertita intr-un vector (array).
# Pentru asta se foloseste ("*")
def vizitatori(*vector)
 vector.each { |vizitator| puts "#{vizitator}" }
end

# Defineste o clasa folosind cuvantul cheie "class"
class Om

  # O variabila apartinand clasei. Este folosita in toate instantele clasei
  @@specie = "H. sapiens"

  # Constructor
  def initialize(nume, varsta=0)
    # Atribuie argumentul, variabilei "nume", care apartine doar unei instante
    @nume = nume
    # Daca varsta nu este data, o sa ii atribuim valoarea implicita
    # din lista de argumente (0, in cazul nostru)
    @varsta = varsta
  end

  # Metoda pentru a seta valoarea unei variabile
  def nume=(nume)
    @nume = nume
  end

  # Metoda pentru a lua valoarea unei variabile
  def nume
    @nume
  end

  # Functionalitatea de mai sus poate fi obtinuta 
  # folosing metoda "attr_accessor" dupa cum urmeaza:
  attr_accessor :nume

  # Metodele pentru a lua si a seta valoarea unei variabile 
  # pot fi de asemenea obtinute individial:
  attr_reader :nume
  attr_writer :nume

  # O metoda apartinand unei clase foloseste "self" pentru a se diferentia 
  # de metodele unei instante ale clasei respective
  # Poate fi invocata doar pe clasa, si nu pe o instanta a acesteia
  def self.spune(msg)
    puts "#{msg}"
  end

  def specie
    @@specie
  end

end


# Creaza o instanta a unei clase
ion = Om.new("Ionut Popescu")

eugen = Om.new("Eugen Ionescu")

# Sa invocam niste metode
ion.specie #=> "H. sapiens"
ion.nume #=> "Ionut Popescu"
ion.nume = "Ionut Popescu JR." #=> "Ionut Popescu JR."
ion.nume #=> "Ionut Popescu JR."
eugen.specie #=> "H. sapiens"
eugen.nume #=> "Eugen Ionescu"

# Invoca o metoda a unei clase
Om.spune("Salut") #=> "Salut"


# Scopul unei variabile este definit de modul in care le numim
# Variabilele care incep cu $ au scop global
$var = "Sunt o variabila globala"
defined? $var #=> "global-variable"

# Variabilele care incep cu @ apartin unei instante
@var = "Sunt o variabila a unei instante"
defined? @var #=> "instance-variable"

# Variabilele care incep cu @@ apartin unei clase
@@var = "Sunt variabila unei clase"
defined? @@var #=> "class variable"

# Variabilele care incep cu litera mare sunt constante
Var = "Sunt o constanta"
defined? Var #=> "constant"

# Clasele sunt de asemenea obiecte in ruby. Astfel incat clasele 
# pot avea variabile care apartin unei instante
# O variabila care apartine unei clase poate fi accesata de toate 
# instantele acesteia si de clasele care o extind

# clasa parinte
class Om
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(valoare)
    @@foo = valoare
  end
end

#  clasa copil
class Muncitor < Om
end

Om.foo # 0
Muncitor.foo # 0

Om.foo = 2 # 2
Muncitor.foo # 2

# Variabilele care apartin unei instante ale unei clase, 
# nu sunt impartite de (copii acesteia) clasele care o extind
class Om
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(valoare)
    @bar = valoare
  end
end

class Doctor < Om
end

Om.bar # 0
Doctor.bar # nil

module ExempluModul
  def foo
    'foo'
  end
end

# Incluzand modulul instantei unui obiect
# Extinzand modulul unei instante ale unei clase

class Persoana
  include ExempluModul
end

class Carte
  extend ExempluModul
end

Persoana.foo     # => NoMethodError: undefined method `foo' for Persoana:Class
Persoana.new.foo # => 'foo'
Carte.foo       # => 'foo'
Carte.new.foo   # => NoMethodError: undefined method `foo'

# Callbacks atunci cand includerea si extinderea unui modul sunt executate

module ModulExempluCallBack
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class CevaRelevant
  include ModulExempluCallBack
end

CevaRelevant.bar     # => 'bar'
CevaRelevant.qux     # => NoMethodError: undefined method `qux'
CevaRelevant.new.bar # => NoMethodError: undefined method `bar'
CevaRelevant.new.qux # => 'qux'
```
---
language: xml
filename: learnxml-ro.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
    - ["Serban Constantin", "https://github.com/fuzzmz"]
lang: ro-ro
---

XML este un limbaj de markup ce are ca scop stocarea si transportul de date.

Spre deosebire de HTML, XML nu specifica cum sa fie afisata sau formatata
informatia, ci doar o transporta.

* Sintaxa XML

```xml
<!-- Comentariile in XML arata asa -->

<?xml version="1.0" encoding="UTF-8"?>
<librarie>
  <carte categorie="GATIT">
    <titlu limba="ro">Mancaruri italiene</titlu>
    <autor>Giada De Laurentiis</autor>
    <an>2005</an>
    <pret>30.00</pret>
  </carte>
  <carte categorie="COPII">
    <titlu limba="ro">Harry Potter</titlu>
    <autor>J K. Rowling</autor>
    <an>2005</an>
    <pret>29.99</pret>
  </carte>
  <carte categorie="WEB">
    <titlu limba="ro">Invata XML</titlu>
    <autor>Erik T. Ray</autor>
    <an>2003</an>
    <pret>39.95</pret>
  </carte>
</librarie>

<!-- Deasupra este un fisier XML obisnuit.
  Incepe cu o declaratie ce adauga niste metadata (optional).
  
  XML foloseste o structura arborescenta. Deasupra, nodul de baza este
  'librarie', care are trei noduri copil, toate 'carti'. Acele noduri au la
  randul lor noduri copii si asa mai departe...

  Nodurile sunt create folosind taguri deschise/inchise, iar copii sunt doar
  noduri intre tagurile de deschis si inchis.-->  


<!-- XML transporta doua tipuri de date:
  1 - Atribute -> Metadata despre un nod.
      In general, parserul XML foloseste aceasta informatie sa stocheze
      proprietatile datelor.
      Este caracterizat de aparitia in paranteze in cadrul tagului deschis
  2 - Elemente -> Date pure.
      Asta este ceea ce parserul va extrage din documentul XML.
      Elementele apar intre tagurile deschis si inchis, fara paranteze. -->
      
  
<!-- Dedesubt, un element cu doua atribute -->
<file type="gif" id="4293">computer.gif</file>


```

* Document bine formatat x Validare

Un document XML este bine formatat daca este corect sintactic.
Cu toate astea este posibil sa injectam mai multe constrangeri in document
folosind definitii precum DTD si XML Schema.

Un document XML ce foloseste o definitie de document este numit valid in
contextul documentului. 

Cu acest tool poti verifica datele XML in afara codului aplicatiei.

```xml

<!-- Dedesubt este o versiune simplificata a documentului librarie, 
  cu aditia definitiei DTD.-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Librarie.dtd">
<librarie>
  <carte categorie="GATIT">
    <titlu >Everyday Italian</titlu>
    <pret>30.00</pret>
  </carte>
</librarie>

<!-- DTD-ul poate fi ceva similar cu:-->

<!DOCTYPE note
[
<!ELEMENT librarie (carte+)>
<!ELEMENT carte (titlu,pret)>
<!ATTLIST carte categorie CDATA "Literatura">
<!ELEMENT titlu (#PCDATA)>
<!ELEMENT pret (#PCDATA)>
]>


<!-- DTD-ul incepe cu o declaratie.
  Dupa, nodul de baza este declarat, cerand unul sau mai multe noduri copii
  de tipul 'carte'.
  Fiecare 'carte' trebuie sa contina exact un 'titlu' si 'pret' si un atribut
  numit 'categorie', cu "Literatura" ca valoare implicita.
  Nodurile 'titlu' si 'pret' contin parsed character data.-->

<!-- DTD-ul poate fi declara si in interiorul fisierului XML.-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT librarie (carte+)>
<!ELEMENT carte (titlu,pret)>
<!ATTLIST carte categorie CDATA "Literatura">
<!ELEMENT titlu (#PCDATA)>
<!ELEMENT pret (#PCDATA)>
]>

<librarie>
  <carte categorie="GATIT">
    <titlu >Everyday Italian</titlu>
    <pret>30.00</pret>
  </carte>
</librarie>
```
---
language: restructured text (RST)
contributors:
    - ["DamienVGN", "https://github.com/martin-damien"]
    - ["Andre Polykanine", "https://github.com/Oire"]
filename: restructuredtext.rst
---

RST is a file format formely created by Python community to write documentation (and so, is part of Docutils).

RST files are simple text files with lightweight syntax (comparing to HTML).


## Installation

To use Restructured Text, you will have to install [Python](http://www.python.org) and the `docutils` package.

`docutils` can be installed using the commandline:

```bash
$ easy_install docutils
```

If your system has `pip`, you can use it too:

```bash
$ pip install docutils
```


## File syntax

A simple example of the file syntax:

```
.. Lines starting with two dots are special commands. But if no command can be found, the line is considered as a comment

=========================================================
Main titles are written using equals signs over and under
=========================================================

Note that there must be as many equals signs as title characters.

Title are underlined with equals signs too
==========================================

Subtitles with dashes
---------------------

And sub-subtitles with tildes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can  put text in *italic* or in **bold**, you can "mark" text as code with double backquote ``: ``print()``.

Lists are as simple as in Markdown:

- First item
- Second item
    - Sub item

or

* First item
* Second item
    * Sub item

Tables are really easy to write:

=========== ========
Country     Capital
=========== ========
France      Paris
Japan       Tokyo
=========== ========

More complex tabless can be done easily (merged columns and/or rows) but I suggest you to read the complete doc for this :)

There are multiple ways to make links:

- By adding an underscore after a word : Github_ and by adding the target URL after the text (this way has the advantage to not insert unnecessary URLs inside readable text).
- By typing a full comprehensible URL : https://github.com/ (will be automatically converted to a link)
- By making a more Markdown-like link: `Github <https://github.com/>`_ .

.. _Github https://github.com/

```


## How to Use It

RST comes with docutils where you have `rst2html`, for example:

```bash
$ rst2html myfile.rst output.html
```

*Note : On some systems the command could be rst2html.py*

But there are more complex applications that use the RST format:

- [Pelican](http://blog.getpelican.com/), a static site generator
- [Sphinx](http://sphinx-doc.org/), a documentation generator
- and many others


## Readings

- [Official quick reference](http://docutils.sourceforge.net/docs/user/rst/quickref.html)
---
category: Algorithms & Data Structures
name: Asymptotic Notation
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Divay Prakash", "http://github.com/divayprakash"]
translators:
    - ["pru-mike", "http://gihub.com/pru-mike"]
lang: ru-ru
---

# О-cимволика

## Что это такое?

О-cимволика или асимптотическая запись это система символов позволяющая оценить
время выполнения алгоритма, устанавливая зависимость времени выполнения от
увеличения объема входных данных, так же известна как оценка
сложности алгоритмов. Быстро-ли алгоритм станет невероятно медленным, когда
объем входных данных увеличится? Будет-ли алгоритм выполняться достаточно быстро,
если объем входных данных возрастет? О-символика позволяет ответить на эти
вопросы.

## Можно-ли по-другому найти ответы на эти вопросы?

Один способ это подсчитать число элементарных операций в зависимости от
различных объемов входных данных. Хотя это и приемлемое решение, тот объем
работы которого оно потребует, даже для простых алгоритмов, делает его 
использование неоправданным.

Другой способ это измерить какое время алгоритм потребует для завершения на
различных объемах входных данных. В тоже время, точность и относительность
(полученное время будет относиться только к той машине на которой оно
вычислено) этого метода зависит от среды выполнения: компьютерного аппаратного
обеспечения, мощности процессора и т.д.

## Виды О-символики

В первом разделе этого документа мы определили, что О-символика
позволяет оценивать алгоритмы в зависимости от изменения размера входных
данных. Представим что алгоритм это функция f, n размер входных данных и 
f(n) время выполнения. Тогда для данного алгоритма f c размером входных
данных n получим какое-то результирующее время выполнения f(n).
Из этого можно построить график, где ось Y время выполнения, ось X размер входных
данных и точки на графике это время выполнения для заданного размера входных
данных.

С помощью О-символики можно оценить функцию или алгоритм
несколькими различными способами. Например можно оценить алгоритм исходя
из нижней оценки, верхней оценки, тождественной оценки. Чаще всего встречается
анализ на основе верхней оценки. Как правило не используется нижняя оценка,
потому что она не подходит под планируемые условия. Отличный пример алгоритмы
сортировки, особенно добавление элементов в древовидную структуру. Нижняя оценка
большинства таких алгоритмов может быть дана как одна операция. В то время как в
большинстве случаев, добавляемые элементы должны быть отсортированы
соответствующим образом при помощи дерева, что может потребовать обхода целой
ветви. Это и есть худший случай, для которого планируется верхняя оценка.

### Виды функций, пределы и упрощения

```
Логарифмическая функция - log n
Линейная функция - an + b
Квадратическая функция - an^2 + bn +c
Полиномиальная функция - an^z + . . . + an^2 + a*n^1 + a*n^0, где z константа
Экспоненциальная функция - a^n, где a константа
```

Приведены несколько базовых функций используемых при определении сложности в
различных оценках. Список начинается с самой медленно возрастающей функции
(логарифм, наиболее быстрое время выполнения) и следует до самой быстро
возрастающей функции (экспонента, самое медленное время выполнения). Отметим,
что в то время как 'n' или размер входных данных, возрастает в каждой из этих функций,
результат намного быстрее возрастает в квадратической, полиномиальной
и экспоненциальной по сравнению с логарифмической и линейной.

Крайне важно понимать, что при использовании описанной далее нотации необходимо
использовать упрощенные выражения.
Это означает, что необходимо отбрасывать константы и слагаемые младших порядков,
потому что если размер входных данных (n в функции f(n) нашего примера)
увеличивается до бесконечности (в пределе), тогда слагаемые младших порядков
и константы становятся пренебрежительно малыми. Таким образом, если есть
константа например размера 2^9001 или любого другого невообразимого размера,
надо понимать, что её упрощение внесёт значительные искажения в точность
оценки.

Т.к. нам нужны упрощенные выражения, немного скорректируем нашу таблицу...

```
Логарифм - log n
Линейная функция - n
Квадратическая функция - n^2
Полиномиальная функция - n^z, где z константа
Экспонента - a^n, где a константа
```

### О-Большое
О-Большое, записывается как **О**, это асимптотическая запись для оценки худшего
случая или для ограничения заданой функции сверху. Это позволяет сделать 
_**асимптотическую оценку верхней границы**_ скорости роста времени выполнения 
алгоритма. Допустим `f(n)` время выполнения алгоритма и `g(n)` заданная временная
сложность которая проверяется для алгоритма. Тогда `f(n)` это O(g(n)), если
существуют действительные константы с (с > 0) и n<sub>0</sub>, такие
что `f(n)` <= `c g(n)` выполняется для всех n начиная с некоторого n<sub>0</sub> (n > n<sub>0</sub>).

*Пример 1*

```
f(n) = 3log n + 100
g(n) = log n
```

Является-ли `f(n)` O(g(n))?
Является-ли `3 log n + 100` O(log n)?
Посмотрим на определение О-Большого:

```
3log n + 100 <= c * log n
```

Существуют-ли константы c, n<sub>0</sub> такие что выражение верно для всех n > n<sub>0</sub>

```
3log n + 100 <= 150 * log n, n > 2 (неопределенно для n = 1)
```

Да! По определению О-Большого `f(n)` является O(g(n)).

*Пример 2*

```
f(n) = 3 * n^2
g(n) = n
```

Является-ли `f(n)` O(g(n))?
Является-ли `3 * n^2` O(n)?
Посмотрим на определение О-Большого:

```
3 * n^2 <= c * n
```

Существуют-ли константы c, n<sub>0</sub> такие что выражение верно для всех n > n<sub>0</sub>?
Нет, не существуют. `f(n)` НЕ ЯВЛЯЕТСЯ O(g(n)).

### Омега-Большое
Омега-Большое, записывается как **Ω**, это асимптотическая запись для оценки
лучшего случая или для ограничения заданой функции снизу. Это позволяет сделать
_**асимптотическую оценку нижней границы**_ скорости роста времени выполнения 
алгоритма. 

`f(n)` принадлежит Ω(g(n)), если существуют действительные константы
с (с > 0) и <sub>0</sub> (n<sub>0</sub> > 0), такие что `f(n)` >= `c g(n)` для всех n > n<sub>0</sub>.

### Примечание

Асимптотические оценки сделаные при помощи О-Большое и Омега-Большое могут
как быть так и не быть точными. Для того что бы обозначить что границы не
являются асимптотически точными используются записи о-малое и омега-малое.

### О-Малое
O-Малое, записывается как **о**, это асимптотическая запись для оценки верхней
границы времени выполнения алгоритма, при условии что граница не является
асимптотически точной.

`f(n)` является o(g(n)), если можно подобрать такие действительные константы,
что для всех c (c > 0) найдется n<sub>0</sub> (n<sub>0</sub> > 0), так
что `f(n)` < `c g(n)` выполняется для всех n (n > n<sub>0</sub>).

Определения О-символики для О-Большое и О-Малое похожи. Главное отличие в том,
что если f(n) = O(g(n)), тогда условие f(n) <= c g(n) выполняется если _**существует**_
константа c > 0, но если f(n) = o(g(n)), тогда условие f(n) < c g(n) выполняется
для _**всех**_ констант с > 0.

### Омега-малое
Омега-малое, записывается как **ω**, это асимптотическая запись для оценки
верней границы времени выполнения алгоритма, при условии что граница не является
асимптотически точной.

`f(n)` является ω(g(n)), если можно подобрать такие действительные константы,
что для всех c (c > 0) найдется n<sub>0</sub> (n<sub>0</sub> > 0), так
что `f(n)` > `c g(n)` выполняется для всех n (n > n<sub>0</sub>)

Определения Ω-символики и ω-символики похожи. Главное отличие в том, что
если f(n) = Ω(g(n)), тогда условие f(n) >= c g(n) выполняется если _**существует**_
константа c > 0, но если f(n) = ω(g(n)), тогда условие f(n) > c g(n)
выполняется для _**всех**_ констант с > 0.

### Тета
Тета, записывается как **Θ**, это асимптотическая запись для оценки
_***асимптотически точной границы***_ времени выполнения алгоритма.

`f(n)` является Θ(g(n)), если для некоторых действительных
констант c1, c2 и n<sub>0</sub> (c1 > 0, c2 > 0, n<sub>0</sub> > 0),
`c1 g(n)` < `f(n)` < `c2 g(n)` для всех n (n > n<sub>0</sub>).

∴ `f(n)` является Θ(g(n)) означает что `f(n)` является O(g(n))
и `f(n)` является Ω(g(n)).

О-Большое основной инструмент для анализа сложности алгоритмов.
Так же смотрите примеры по ссылкам.

### Заключение
Такую тему сложно изложить кратко, поэтому обязательно стоит пройти по ссылкам и
посмотреть дополнительную литературу. В них дается более глубокое описание с
определениями и примерами.


## Дополнительная литература

* [Алгоритмы на Java](https://www.ozon.ru/context/detail/id/18319699/)
* [Алгоритмы. Построение и анализ](https://www.ozon.ru/context/detail/id/33769775/)

## Ссылки

* [Оценки времени исполнения. Cимвол O()](http://algolist.manual.ru/misc/o_n.php)
* [Асимптотический анализ и теория вероятностей](https://www.lektorium.tv/course/22903)

## Ссылки (Eng)

* [Algorithms, Part I](https://www.coursera.org/learn/algorithms-part1)
* [Cheatsheet 1](http://web.mit.edu/broder/Public/asymptotics-cheatsheet.pdf)
* [Cheatsheet 2](http://bigocheatsheet.com/)

---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
translators:
    - ["Andrey Samsonov", "https://github.com/kryzhovnik"]
    - ["Andre Polykanine", "https://github.com/Oire"]
filename: LearnBash-ru.sh
lang: ru-ru
---

Bash - это командная оболочка unix (unix shell), которая распространялась как оболочка для операционной системы GNU и используется в качестве оболочки по умолчанию для Linux и Mac OS X.
Почти все нижеприведенные примеры могут быть частью shell-скриптов или исполнены напрямую в shell.

[Подробнее.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# Первая строка скрипта - это shebang, который сообщает системе, как исполнять
# этот скрипт: http://en.wikipedia.org/wiki/Shebang_(Unix)
# Как вы уже поняли, комментарии начинаются с #. Shebang - тоже комментарий.

# Простой пример hello world:
echo Hello world!

# Отдельные команды начинаются с новой строки или разделяются точкой с запятой:
echo 'Это первая строка'; echo 'Это вторая строка'

# Вот так объявляется переменная:
VARIABLE="Просто строка"

# но не так:
VARIABLE = "Просто строка"
# Bash решит, что VARIABLE - это команда, которую он должен исполнить,
# и выдаст ошибку, потому что не сможет найти ее.

# и не так:
VARIABLE= 'Просто строка'
# Тут Bash решит, что 'Просто строка' - это команда, которую он должен исполнить,
# и выдаст ошибку, потому что не сможет найти такой команды
# (здесь 'VARIABLE=' выглядит как присвоение значения переменной,
# но только в контексте исполнения команды 'Просто строка').

# Использование переменой:
echo $VARIABLE
echo "$VARIABLE"
echo '$VARIABLE'
# Когда вы используете переменную - присваиваете, экспортируете и т.д. -
# пишите её имя без $. А для получения значения переменной используйте $.
# Заметьте, что ' (одинарные кавычки) не раскрывают переменные в них.

# Подстановка строк в переменные
echo ${VARIABLE/Просто/A}
# Это выражение заменит первую встреченную подстроку "Просто" на "A"

# Взять подстроку из переменной
LENGTH=7
echo ${VARIABLE:0:LENGTH}
# Это выражение вернет только первые 7 символов переменной VARIABLE

# Значение по умолчанию
echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"}
# Это сработает при отсутствующем значении (FOO=) и пустой строке (FOO="");
# ноль (FOO=0) вернет 0.
# Заметьте, что в любом случае значение самой переменной FOO не изменится.

# Встроенные переменные:
# В bash есть полезные встроенные переменные, например
echo "Последнее возвращенное значение: $?"
echo "PID скрипта: $$"
echo "Количество аргументов: $#"
echo "Аргументы скрипта: $@"
echo "Аргументы скрипта, распределённые по отдельным переменным: $1 $2..."

# Чтение аргументов из устройста ввода:
echo "Как Вас зовут?"
read NAME # Обратите внимание, что нам не нужно определять новую переменную
echo Привет, $NAME!

# У нас есть обычная структура if:
# наберите 'man test' для получения подробной информации о форматах условия
if [ $NAME -ne $USER ]
then
    echo "Имя не совпадает с именем пользователя"
else
    echo "Имя совпадает с именем пользователя"
fi

# Примечание: если $Name пустой, bash интерпретирует код как:
if [ -ne $USER ]
# а это ошибочная команда
# поэтому такие переменные нужно использовать так:
if [ "$Name" -ne $USER ] ...
# когда $Name пустой, bash видит код как:
if [ "" -ne $USER ] ...
# что работает правильно

# Также есть условное исполнение
echo "Исполнится всегда" || echo "Исполнится, если первая команда завершится ошибкой"
echo "Исполнится всегда" && echo "Исполнится, если первая команда выполнится удачно"

# Можно использовать && и || в выражениях if, когда нужно несколько пар скобок:
if [ $NAME == "Steve" ] && [ $AGE -eq 15 ]
then
    echo "Исполнится, если $NAME равно Steve И $AGE равно 15."
fi

if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ]
then
    echo "Исполнится, если $NAME равно Daniya ИЛИ Zach."
fi

# Выражения обозначаются таким форматом:
echo $(( 10 + 5 ))

# В отличие от других языков программирования, Bash - это командная оболочка,
# а значит, работает в контексте текущей директории.
# Вы можете просматривать файлы и директории в текущей директории командой ls:
ls

# У этой команды есть опции:
ls -l # Показать каждый файл и директорию на отдельной строке

# Результат предыдущей команды может быть направлен на вход следующей.
# Команда grep фильтрует ввод по шаблону.
# Так мы можем просмотреть только *.txt файлы в текущей директории:
ls -l | grep "\.txt"

# Вы можете перенаправить ввод и вывод команды (stdin, stdout и stderr).
# Следующая команда означает: читать из stdin, пока не встретится ^EOF$, и
# перезаписать hello.py следующим строками (до строки "EOF"):
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Запуск hello.py с разными вариантами перенаправления потоков
# стандартных ввода, вывода и ошибок:
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# Поток ошибок перезапишет файл, если этот файл существует,
# поэтому, если вы хотите дописывать файл, используйте ">>":
python hello.py >> "output.out" 2>> "error.err"

# Переписать output.txt, дописать error.err и сосчитать строки:
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# Запустить команду и вывести ее файловый дескриптор (смотрите: man fd)
echo <(echo "#helloworld")

# Перезаписать output.txt строкой "#helloworld":
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# Подчистить временные файлы с подробным выводом ('-i' - интерактивый режим)
rm -v output.out error.err output-and-error.log

# Команды могут быть подставлены в строку с помощью $( ):
# следующие команды выводят число файлов и директорий в текущей директории.
echo "Здесь $(ls | wc -l) элементов."

# То же самое можно сделать с использованием обратных кавычек,
# но они не могут быть вложенными, поэтому предпочтительно использовать $( ).
echo "Здесь `ls | wc -l` элементов."

# В Bash есть структура case, которая похожа на switch в Java и C++:
case "$VARIABLE" in 
    # Перечислите шаблоны для условий, которые хотите отловить
    0) echo "Тут ноль.";;
    1) echo "Тут один.";;
    *) echo "Это не пустое значение.";;
esac

# Цикл for перебирает элементы переданные в аргументе:
# Содержимое $VARIABLE будет напечатано три раза.
for VARIABLE in {1..3}
do
    echo "$VARIABLE"
done

# Или с использованием "традиционного" синтаксиса цикла for:
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Цикл for можно использовать для действий с файлами.
# Запустим команду 'cat' для файлов file1 и file2
for VARIABLE in file1 file2
do
    cat "$VARIABLE"
done

# ... или выводом из команд
# Запустим cat для вывода из ls.
for OUTPUT in $(ls)
do
    cat "$OUTPUT"
done

# Цикл while:
while [ true ]
do
    echo "тело цикла здесь..."
    break
done

# Вы можете определять функции
# Определение:
function foo ()
{
    echo "Аргументы работают также, как аргументы скрипта: $@"
    echo "и: $1 $2..."
    echo "Это функция"
    return 0
}

# или просто
bar ()
{
    echo "Другой способ определить функцию!"
    return 0
}

# Вызов функции
foo "Мое имя" $NAME

# Есть много полезных команд, которые нужно знать:
# напечатать последние 10 строк файла file.txt
tail -n 10 file.txt
# напечатать первые 10 строк файла file.txt
head -n 10 file.txt
# отсортировать строки file.txt
sort file.txt
# отобрать или наоборот пропустить повторяющиеся строки (с опцией -d отбирает)
uniq -d file.txt
# напечатать только первую колонку перед символом ','
cut -d ',' -f 1 file.txt
# заменить каждое 'okay' на 'great' в файле file.txt (regex поддерживается)
sed -i 's/okay/great/g' file.txt
# вывести в stdout все строки из file.txt, совпадающие с шаблоном regex;
# этот пример выводит строки, которые начинаются на "foo" и оканчиваются "bar"
grep "^foo.*bar$" file.txt
# передайте опцию -c чтобы вывести число строк, в которых совпал шаблон
grep -c "^foo.*bar$" file.txt
# чтобы искать по строке, а не шаблону regex, используйте fgrep (или grep -F)
fgrep "^foo.*bar$" file.txt 

# Читайте встроенную документацию оболочки Bash командой 'help':
help
help help
help for
help return
help source
help .

# Читайте Bash man-документацию
apropos bash
man 1 bash
man bash

# Читайте документацию info (? для помощи)
apropos info | grep '^info.*('
man info
info info
info 5 info

# Читайте bash info документацию:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: bf
filename: learnbf-ru.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Dmitry Bessonov", "https://github.com/TheDmitry"]
lang: ru-ru
---

Brainfuck (пишется маленькими буквами, кроме начала предложения) - это очень
маленький Тьюринг-полный язык программирования лишь с 8 командами.

Вы можете испытать brainfuck в вашем браузере с помощью [brainfuck-визуализатора](http://fatiherikli.github.io/brainfuck-visualizer/).

```
Любой символ, кроме "><+-.,[]", игнорируется, за исключением кавычек.

Brainfuck представлен массивом из 30000 ячеек, инициализированных нулями,
и указателем с позицией в текущей ячейке.

Всего восемь команд:
+ : Увеличивает значение на единицу в текущей ячейке.
- : Уменьшает значение на единицу в текущей ячейке.
> : Смещает указатель данных на следующую ячейку (ячейку справа).
< : Смещает указатель данных на предыдущую ячейку (ячейку слева).
. : Печатает ASCII символ из текущей ячейки (напр. 65 = 'A').
, : Записывает один входной символ в текущую ячейку.
[ : Если значение в текущей ячейке равно нулю, то пропустить все команды
    до соответствующей ] . В противном случае, перейти к следующей инструкции.
] : Если значение в текущей ячейке равно нулю, то перейти к следующей инструкции.
    В противном случае, вернуться назад к соответствующей [ .

[ и ] образуют цикл while. Естественно, они должны быть сбалансированы.

Давайте рассмотрим некоторые базовые brainfuck-программы.

++++++ [ > ++++++++++ < - ] > +++++ .

Эта программа выводит букву 'A'. Сначала программа увеличивает значение
ячейки №1 до 6. Ячейка №1 будет использоваться циклом. Затем программа входит
в цикл ([) и переходит к ячейке №2. Ячейка №2 увеличивается до 10, переходим
назад к ячейке №1 и уменьшаем ячейку №1. Этот цикл проходит 6 раз (ячейка №1
уменьшается до нуля, и с этого места пропускает инструкции до соответствующей ]
и идет дальше).

В этот момент мы находимся в ячейке №1, которая имеет значение 0, значение
ячейки №2 пока 60. Мы переходим на ячейку №2, увеличиваем 5 раз, до значения 65,
и затем выводим значение ячейки №2. Код 65 является символом 'A' в кодировке ASCII,
так что 'A' выводится на терминал.


, [ > + < - ] > .

Данная программа считывает символ из пользовательского ввода и копирует символ
в ячейку №1. Затем мы начинаем цикл. Переходим к ячейке №2, увеличиваем значение
ячейки №2, идем назад к ячейке №1 и уменьшаем значение ячейки №1. Это продолжается
до тех пор, пока ячейка №1 не равна 0, а ячейка №2 сохраняет старое значение
ячейки №1. Мы завершаем цикл на ячейке №1, поэтому переходим в ячейку №2 и
затем выводим символ ASCII.

Также имейте в виду, что пробелы здесь исключительно для читабельности. Вы можете
легко написать и так:

,[>+<-]>.

Попытайтесь разгадать, что следующая программа делает:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Программа принимает два числа на вход и умножает их.

Суть в том, что программа сначала читает два ввода. Затем начинается внешний цикл,
сохраняя ячейку №1. Затем программа перемещается в ячейку №2, и начинается
внутренний цикл с сохранением ячейки №2, увеличивая ячейку №3. Однако появляется
проблема: В конце внутреннего цикла ячейка №2 равна нулю. В этом случае,
внутренний цикл не будет работать уже в следующий раз. Чтобы решить эту проблему,
мы также увеличим ячейку №4, а затем копируем ячейку №4 в ячейку №2.
Итак, ячейка №3 - результат.
```

Это и есть brainfuck. Не так уж сложно, правда? Забавы ради, вы можете написать
свою собственную brainfuck-программу или интерпретатор на другом языке.
Интерпретатор достаточно легко реализовать, но если вы мазохист, попробуйте
написать brainfuck-интерпретатор... на языке brainfuck.
---
category: Algorithms & Data Structures
name: Binary Search
contributors:
    - ["Abhishek Jaisingh", "http://github.com/abhishekjiitr"]
translators:
    - ["Evan K.", "https://github.com/justblah"]
lang: ru-ru    
---

# Двоичный (бинарный) поиск

## Зачем использовать двоичный поиск?

Поиск является одной из главных проблем в области вычислительной техники. На сегодняшний день осуществляется более одного триллиона поисковых запросов в год, поэтому нам нужны алгоритмы, которые могут делать это очень быстро. Двоичный поиск является одним из фундаментальных алгоритмов в информатике. Для его изучения мы освоим теорию, а затем используем её для реализации алгоритма.

## Вступление

Самый простой вариант поиска – линейный поиск, но этот подход занимает много времени, и растет линейно, пропорционально набору данных. Пример реализации – начинаем с крайнего левого элемента массива S, один за другим сравниваем искомое значение X с каждым элементом массива S, если X совпадает с элементом S, возвращаем индекс. Если X не совпадает ни с одним из элементов массива S, возвращаем -1.

```
Линейный поиск: O (n)            Линейная сложность

Двоичный поиск: O ( log(n) )		 Логарифмическая сложность

```
```
def search(arr, x):

    for i in range(len(arr)):

        if arr[i] == x:
            return i

    return -1

```

## Алгоритм двоичного поиска

Для корректной работы двоичного поиска набор данных для поиска должен быть отсортирован (в любом порядке).

### Алгоритм

```
Главная идея двоичного поиска заключается в использовании информации о том, что массив уже отсортирован,
что и позволяет упростить сложность алгоритма до O(Logn). Мы попросту отбрасываем половину элементов набора сразу после одного сравнения.
1) Сравнить X с элементом в середине набора S.
2) Если X равен элементу в середине - возвращаем индекс среднего элемента.
3) Если значение X больше, чем средний элемент набора, значит X находится в правой части набора. Повторяем алгоритм для правой половины набора.
4) В противном случае (X меньше) повторяем алгоритм для левой половины набора.
Это и есть рекурсивная реализация двоичного поиска.

```

### На заметку

Существует и другая форма двоичного поиска, которая можеть быть полезна.

## На почитать

* [Проектирование, реализация и примеры](https://ru.wikipedia.org/wiki/%D0%94%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D1%8B%D0%B9_%D0%BF%D0%BE%D0%B8%D1%81%D0%BA)
* [Описание алгоритма ИТМО](http://neerc.ifmo.ru/wiki/index.php?title=%D0%A6%D0%B5%D0%BB%D0%BE%D1%87%D0%B8%D1%81%D0%BB%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9_%D0%B4%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D1%8B%D0%B9_%D0%BF%D0%BE%D0%B8%D1%81%D0%BA)
* [Ошибки при реализации бинарного поиска](https://habrahabr.ru/post/146228/)
---
language: c++
filename: learncpp-ru.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Matt Kline", "https://github.com/mrkline"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Connor Waters", "http://github.com/connorwaters"]
translators:
    - ["Bohdan Shtepan", "http://modern-dev.com"]
lang: ru-ru
---

C++ - компилируемый, статически типизированный язык программирования общего назначения, который,
[как заявляет создатель языка Бьёрн Страуструп](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote),
был разработан как

- "лучшая замена C"
- язык с поддержкой абстракции данных
- язык с поддержкой объектно-ориентированого программирования
- язык с поддержкой обобщенного программирования

Хотя его синтаксис может показаться более трудным или сложным для понимания, чем в более современных языках,
он широко применяется, так как код, написанный на C++, компилируется в набор инструкций, которые могут быть выполнены напрямую
процессором. C++ широко используется для разработки программного обеспечения, являясь одним из самых популярных языков
программирования. Область его применения включает создание операционных систем, разнообразных прикладных программ, драйверов
устройств, приложений для встраиваемых систем, высокопроизводительных серверов, а также развлекательных приложений (игр).

```c++
//////////////////
// Сравнение с C
//////////////////

// C++ практически представляет собой надмножество C и имеет схожий синтаксис
// для объявления переменных, примитивов и функций.

// Так же, как и в С, точкой входа в программу является функция с именем main,
// которая возвращает целочисленное значение.
// Это значение является кодом ответа программы.
// Смотрите https://goo.gl/JYGKyv для более подробной информации.
int main(int argc, char** argv)
{
    // Аргументы командной строки, переданные в программу, хранятся в переменных
	// argc и argv, так же, как и в C.
    // argc указывает на количество аргументов,
    // а argv является массивом C-подобных строк (char*), который непосредсвенно
	// содержит аргументы.
    // Первым аргументом всегда передается имя программы.
    // argc и argv могут быть опущены, если вы не планируете работать с аругментами
	// коммандной строки.
	// Тогда сигнатура функции будет иметь следующий вид: int main()

    // Возвращаемое значение 0 указывает на успешное завершение программы.
    return 0;
}

// Тем не менее, C++ имеет свои отличия:

// В C++ символьные литералы имеют тип char.
sizeof('c') == sizeof(char) == 1

// В C символьные литералы - целые числа.
sizeof('c') == sizeof(int)


// C++ имеет строгое прототипирование.
void func(); // функция, которая не принимает аргументов.

// В языке C
void func(); // функция, которая может принять сколько угодно аргументов.

// Использование nullptr вместо NULL в C++.
int* ip = nullptr;

// Стандартные заголовочные файлы С доступны в С++,
// но с префиксом "с" и не имеют суффикса .h.
#include <cstdio>

int main()
{
    printf("Hello, world!\n");
    return 0;
}

///////////////////////
// Перегрузка функций
///////////////////////

// С++ поддерживает перегрузку функций, при условии,
// что каждая функция принимает различные параметры.

void print(char const* myString)
{
    printf("String %s\n", myString);
}

void print(int myInt)
{
    printf("My int is %d", myInt);
}

int main()
{
    print("Hello"); // Использование void print(const char*)
    print(15); // Использование void print(int)
}

/////////////////////////////
// Аргументы функций по умолчанию
/////////////////////////////

// Вы можете предоставить аргументы по умолчанию для функции,
// если они не предоставлены при вызове функции.

void doSomethingWithInts(int a = 1, int b = 4)
{
    // Здесь что-то делаем с числами
}

int main()
{
    doSomethingWithInts();      // a = 1,  b = 4
    doSomethingWithInts(20);    // a = 20, b = 4
    doSomethingWithInts(20, 5); // a = 20, b = 5
}

// Аргументы по умолчанию должны быть в конце списка аргументов.

void invalidDeclaration(int a = 1, int b) // Ошибка!
{
}


/////////////
// Пространства имен
/////////////

// Пространства имен предоставляют отдельные области для переменной,
// функции и других объявлений.
// Пространства имен могут быть вложенными.

namespace First {
    namespace Nested {
        void foo()
        {
            printf("This is First::Nested::foo\n");
        }
    } // конец пространства имен Nested
} // конец пространства имен First

namespace Second {
    void foo()
    {
        printf("This is Second::foo\n")
    }
}

void foo()
{
    printf("This is global foo\n");
}

int main()
{
    // Включает все функци из пространства имен Second в текущую область видимости.
    // Обратите внимание, что простой вызов foo() больше не работает,
    // так как теперь не ясно, вызываем ли мы foo из пространства имен Second, или
	// из глобальной области видимости.
    using namespace Second;

    Second::foo(); // напечатает "This is Second::foo"
    First::Nested::foo(); // напечатает "This is First::Nested::foo"
    ::foo(); // напечатает "This is global foo"
}

///////////////
// Ввод и вывод
///////////////

// Ввод и вывод в C++ использует потоки
// cin, cout и cerr представляют потоки stdin, stdout и stderr.
// << - оператор вставки, >> - оператор извлечения.

#include <iostream> // Включение файла для работы с потоками Ввода\Вывода.

using namespace std; // Потоки доступны в пространстве имен std (стандартная библиотека)

int main()
{
   int myInt;

   // Выводит в stdout (или в терминал/на экран)
   cout << "Enter your favorite number:\n";
   // Принимает ввод
   cin >> myInt;

   // cout может принимать форматирование
   cout << "Your favorite number is " << myInt << "\n";
   // напечатает "Your favorite number is <myInt>"

    cerr << "Used for error messages";
}

//////////
// Строки
//////////

// Строки в C++ являются объектами и имеют много функций-членов.
#include <string>

using namespace std; // Строки также доступны в пространстве имен std (стандартная библиотека)

string myString = "Hello";
string myOtherString = " World";

// + используется для конкатенации строк.
cout << myString + myOtherString; // "Hello World"

cout << myString + " You"; // "Hello You"

// Строки в C++ могут изменяться и имеют семантику значений.
myString.append(" Dog");
cout << myString; // "Hello Dog"


/////////////
// Ссылки
/////////////

// Кроме указателей, доступных в C,
// C++ имеет _ссылки_.
// Это такой тип указателя, который не может быть переназначен после инициализации
// и не может иметь значения null.
// Ссылки имеют схожий с переменными синтаксис:
// * больше не используется для разыменования и
// & (адрес) не используется для назначения.

using namespace std;

string foo = "I am foo";
string bar = "I am bar";


string& fooRef = foo; // Здесь создается ссылка на foo.
fooRef += ". Hi!"; // Изменяет foo по ссылке
cout << fooRef; // Печатает "I am foo. Hi!"

// Не переназначает "fooRef". Это то же самое, что и "foo = bar", и
//   foo == "I am bar"
// после этой строчки.
cout << &fooRef << endl; // Печатает адрес foo
fooRef = bar;
cout << &fooRef << endl; // По-прежнему печатает адрес foo
cout << fooRef;  // Печатает "I am bar"

// Адрес fooRef остается тем же, то есть он по-прежнему ссылается на foo.


const string& barRef = bar; // Создает константную ссылку.
// Так же, как и в C, константные значения (а также указатели и ссылки) не могут быть изменены.
barRef += ". Hi!"; // Ошибка, константная ссылка не может быть изменена.

// Обходной путь: Прежде чем мы рассмотрим указатели более детально, нам нужно ознакомиться
// с концепцией, известной как "временный объект". Представьте, что мы имеем следующий код
string tempObjectFun() { ... }
string retVal = tempObjectFun();

// Вот что на самом деле происходит во второй строке:
//   - tempObjectFun возвращает строковый объект
//   - из возвращаемого объекта создается новая строка в качестве аргумента конструктору
//   - возвращаемый объект уничтожается
// Возвращаемый объект называется временным объектом. Временные объекты создаются,
// когда функция возвращает объект, и уничтожаются в конце выполнения обрамляющего
// выражения (По крайней мере, так это описывает спецификация, хотя компиляторы могут
// изменять это поведение. Для более подробной информации смотрите "оптимизация
// возвращаемого значения".) Таким образом в этом коде:
foo(bar(tempObjectFun()))

// предполагая, что foo и bar существуют, объект, возвращаемый tempObjectFun, передается
// в bar, и уничтожается перед вызовом foo.

// Возвращаемся к указателям. Исключением для правила "в конце выполнения обрамляющего
// выражения" является временный объект, привязанный к ссылке const, в этом случае
// его жизненный цикл продлевается до текущей области видимости:

void constReferenceTempObjectFun() {
  // constRef получает временный объект, и он действителен до конца этой функции.
  const string& constRef = tempObjectFun();
  ...
}

// В C++11 предоставлен еще один тип ссылок специально для временных объектов.
// objects. Вы не можете объявить переменную этого типа, но он имеет приоритет
// в резолюции перегрузки:

void someFun(string& s) { ... }  // Обычная ссылка
void someFun(string&& s) { ... }  // Ссылка на временный объект

string foo;
someFun(foo);  // Выполняет версию с обычной ссылкой
someFun(tempObjectFun());  // Выполняет версию с временной ссылкой.

// Например, существуют следующие две версии конструктора std::basic_string:
basic_string(const basic_string& other);
basic_string(basic_string&& other);

// Идея в том, что если мы конструируем новую строку из временного объекта (который
// так или иначе будет уничтожен), мы можем использовать более эффективный конструктор,
// который "спасает" части этой временной строки. Эта концепция была названа
// "move semantics".

/////////////////////
// Перечисления
/////////////////////

// Перечисления - способ объявления констант и установки их значений, в основном
// использующийся для упрощения чтения кода.
enum ECarTypes
{
  Sedan,
  Hatchback,
  SUV,
  Wagon
};

ECarTypes GetPreferredCarType()
{
	return ECarTypes::Hatchback;
}

// На момент выхода C++11 есть простой способ назначения типа перечисления, что
// полезно в случае сериализации данных и преобразований между конечным типом и
// соответствующими константами.
enum ECarTypes : uint8_t
{
  Sedan, // 0
  Hatchback, // 1
  SUV = 254, // 254
  Hybrid // 255
};

void WriteByteToFile(uint8_t InputValue)
{
	// Сериализуем InputValue в файл
}

void WritePreferredCarTypeToFile(ECarTypes InputCarType)
{
	// Перечисление неявно преобразуется в uint8_t из-за ранее объявленного
	// типа перечисления.
	WriteByteToFile(InputCarType);
}

// С другой стороны, чтобы избежать случайного приведения к целочисленному типу или
// другому перечислению, вы можете создать класс перечисления, который не будет
// преобразовываться неявно.
enum class ECarTypes : uint8_t
{
  Sedan, // 0
  Hatchback, // 1
  SUV = 254, // 254
  Hybrid // 255
};

void WriteByteToFile(uint8_t InputValue)
{
	// Сериализуем InputValue в файл
}

void WritePreferredCarTypeToFile(ECarTypes InputCarType)
{
	// Хотя ECarTypes имеет тип uint8_t, код не будет скомпилирован из-за того,
	// что перечисление было объявлено как класс перечисления.
	WriteByteToFile(InputCarType);
}

//////////////////////////////////////////
// Классы и объектно-ориентированное программирование
//////////////////////////////////////////

// Пример классов
#include <iostream>

// Объявление класса.
// Обычно классы объявляют в заголовочном (.h или .hpp) файле.
class Dog {
    // Переменные-члены и функции являются приватными по умолчанию.
    std::string name;
    int weight;

// Все члены после этой сроки являются открытыми
// пока "private:" или "protected:" не будет объявлено.
public:

    // Конструктор по умолчанию
    Dog();

    // Объявление функций-членов
    // Обратите внимание, мы используем std::string здесь вместо использования
    // using namespace std;
    // выше.
    // Никогда не размещайте выражение "using namespace" в заголовке.
    void setName(const std::string& dogsName);

    void setWeight(int dogsWeight);

    // Функции, которые не изменяют состояние объекта,
    // должны быть помечены как const.
    // Это позволяет вызывать их, если дана const ссылка на объект.
    // Обратите внимание, функции должны быть явно объявлены как _virtual_,
    // если вы хотите перегрузить их в производных классах.
    // Функции не являются виртуальными по умолчанию для повышения производительности.
    virtual void print() const;

    // Также функции могут быть определены внутри тела класса.
    // Функции, определенные следующим образом, автоматически встроены.
    void bark() const { std::cout << name << " barks!\n"; }

    // Наряду с конструкторами, в C++ есть деструкторы.
    // Они вызываются, когда объект удаляется или выпадает из области видимости.
    // Это активирует мощную парадигму программирования, известную как RAII
    // (смотрите ниже)
    // Деструктор должен быть виртуальным, если класс будет производным.
    // Если он не виртуальный, тогда деструктор производного класса не будет вызван,
    // если объект удален по ссылке или указателю базового класса.
    virtual ~Dog();

}; // Определение класса должно завершаться точкой с запятой.

// Функции-члены класса, как правило, реализуются в .cpp файлах.
Dog::Dog()
{
    std::cout << "A dog has been constructed\n";
}

// Объекты (такие как строки) должны передаваться по ссылке если вы будете
// изменять их, или const-ссылке если нет.
void Dog::setName(const std::string& dogsName)
{
    name = dogsName;
}

void Dog::setWeight(int dogsWeight)
{
    weight = dogsWeight;
}

// Обратите внимание, "virtual" требуется только в объявлении, не в определении.
void Dog::print() const
{
    std::cout << "Dog is " << name << " and weighs " << weight << "kg\n";
}

Dog::~Dog()
{
    std::cout << "Goodbye " << name << "\n";
}

int main() {
    Dog myDog; // Печатает "A dog has been constructed"
    myDog.setName("Barkley");
    myDog.setWeight(10);
    myDog.print(); // Печатает "Dog is Barkley and weighs 10 kg"
    return 0;
} // Печатает "Goodbye Barkley"

// Интерфейсы:

// Этот класс наследует все открытые и защищенные члены класса Dog
// так же, как и все закрытые, но не может непосредственно получить доступ к закрытым
// членам\методам без открытых или защищенных методов для этого.
class OwnedDog : public Dog {

    void setOwner(const std::string& dogsOwner);

    // Переопределяем поведение функции печати для всех OwnedDog. Смотрите
    // https://goo.gl/3kuH2x для боле общего введения, если вы не знакомы
    // с концепцией полиморфизма подтипов (включения).
    // Ключевое слово override является необязательным, но указывает, что метод
    // на самом деле перегружается в базовом классе.
    void print() const override;

private:
    std::string owner;
};

// Тем временем, в соответствующем .cpp файле:

void OwnedDog::setOwner(const std::string& dogsOwner)
{
    owner = dogsOwner;
}

void OwnedDog::print() const
{
    Dog::print(); // Вызывает функцию print в базовом классе Dog
    std::cout << "Dog is owned by " << owner << "\n";
    // Печатает "Dog is <name> and weights <weight>"
    //        "Dog is owned by <owner>"
}

//////////////////////////////////////////
// Инициализация и перегрузка операторов.
//////////////////////////////////////////

// В C++ вы можете перегрузить поведение таких операторов: +, -, *, / и др..
// Это делается путем определения функции, которая вызывается,
// когда используется оператор.

#include <iostream>
using namespace std;

class Point {
public:
    // Значения по умолчанию для переменных-членов могут быть установлены
	// следующим образом.
    double x = 0;
    double y = 0;

    // Определяем новый конструктор, который инициализирует Point со значениями
    // по умолчанию (0, 0)
    Point() { };

    // Следующий синтаксис известен как список инициализации и является верным способом
	// инициализировать значения членов класса.
    Point (double a, double b) :
        x(a),
        y(b)
    { /* Ничего не делайте, кроме инициализации значений */ }

    // Перегружаем оператор +.
    Point operator+(const Point& rhs) const;

    // Перегружаем оператор +=.
    Point& operator+=(const Point& rhs);

    // Имеет смысл добавить перегрузку операторов - и -=,
    // но для краткости мы опустим эти детали.
};

Point Point::operator+(const Point& rhs) const
{
    // Создает новую точку, которая является суммой этой точки и rhs.
    return Point(x + rhs.x, y + rhs.y);
}

Point& Point::operator+=(const Point& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

int main () {
    Point up (0,1);
    Point right (1,0);
    // Здесь происходит вызов оператора + класса Point
    // Точка "up" вызывает + (функция) с параметром "right"
    Point result = up + right;
    // Печатает "Result is upright (1,1)"
    cout << "Result is upright (" << result.x << ',' << result.y << ")\n";
    return 0;
}

/////////////////////
// Шаблоны
/////////////////////

// Шаблоны в С++, в основном, используются для обобщенного программирования, хотя
// они гораздо более мощны, чем дженерики в других языках. Они также поддерживают
// явные, частные и функциональные типы классов; на самом деле, они являются
// тьюринг-полным языком, встроенным в C++!

// Мы начнем с наиболее распространенного типа обобщенного программирования. Чтобы
// определить класс или функцию, которая принимает параметр типа:
template<class T>
class Box {
public:
    // В этом классе T может быть любого типа.
    void insert(const T&) { ... }
};

// Во время компиляции компилятор фактически генерирует копии каждого шаблона
// с замещенными параметрами, поэтому полное определение класса должно присутствовать
// при каждом вызове. Именно поэтому классы шаблонов полностью определены в
// заголовочных файлах.

// Чтобы создать экземпляр класса шаблона на стеке:
Box<int> intBox;

// и вы можете использовать его, как и ожидалось:
intBox.insert(123);

// Вы, конечно, можете использовать вложенные шаблоны:
Box<Box<int> > boxOfBox;
boxOfBox.insert(intBox);

// Вплоть до С++11, вы должны были ставить пробел между двумя символами '>', иначе '>>'
// принимался парсером, как оператор сдвига вправо.

// Иногда вы можете увидеть
//   template<typename T>
// вместо этого. В этом случае ключевые слова 'class' и 'typename' _в основном_
// взаимозаменяемыми. Для более подробной информации смотрите
//   http://en.wikipedia.org/wiki/Typename
// (да-да, это ключевое слово имеет собственную страничку на вики).

// Аналогичным образом, шаблонная функция:
template<class T>
void barkThreeTimes(const T& input)
{
    input.bark();
    input.bark();
    input.bark();
}

// Обратите внимание, что здесь ничего не указано о типе параметра. Компилятор
// будет генерировать и затем проверять на тип каждый вызов шаблона, поэтому
// данная функция работает с любым типом 'T', который имеет метод 'bark'.

Dog fluffy;
fluffy.setName("Fluffy");
barkThreeTimes(fluffy); // Печатает "Fluffy barks" три раза.

//Параметры шаблона не должны быть классами:
template<int Y>
void printMessage() {
  cout << "Learn C++ in " << Y << " minutes!" << endl;
}

// В конце концов, вы можете явно специализировать шаблоны для более эффективного
// кода. Конечно, большинство реальных случаев использования специализации
// не так тривиально, как это. Обратите внимание, вам все еще нужно явно объявить
// функцию (или класс) в качестве шаблона, даже если вы явно указали все параметры.
template<>
void printMessage<10>() {
  cout << "Learn C++ faster in only 10 minutes!" << endl;
}

printMessage<20>();  // Печатает "Learn C++ in 20 minutes!"
printMessage<10>();  // Печатает "Learn C++ faster in only 10 minutes!"


/////////////////////
// Обработка исключений
/////////////////////

// Стандартная библиотека предоставляет несколько типов исключений
// (смотрите http://en.cppreference.com/w/cpp/error/exception)
// но, в принципе, любой тип может быть брошен в качестве исключения.
#include <exception>
#include <stdexcept>

// Все исключения, брошенные в блоке _try_ могут быть пойманы в последующем блоке
// _catch_.
try {
    // Не выделяйте память в куче для исключений с помощью ключевого слова _new_.
    throw std::runtime_error("A problem occurred");
}

// Поймайте исключение по константной ссылке, если оно является объектом
catch (const std::exception& ex)
{
    std::cout << ex.what();
}

// Ловит любое исключение, не пойманное предыдущим блоком _catch_
catch (...)
{
    std::cout << "Unknown exception caught";
    throw; // Повторный выброс исключения
}

///////
// Получение ресурса есть инициализация (RAII)
///////

// Программная идиома объектно-ориентированного программирования, смысл которой
// заключается в том, что с помощью тех или иных программных механизмов получение
// некоторого ресурса неразрывно совмещается с инициализацией, а освобождение -
// с уничтожением объекта.

// Чтобы понять, на сколько это полезно,
// рассмотрим функцию, которая использует обработчик файлов в С:
void doSomethingWithAFile(const char* filename)
{
    // Для начала, предположим, ничего не может потерпеть неудачу.

    FILE* fh = fopen(filename, "r"); // Открываем файл в режиме чтения.

    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

    fclose(fh); // Закрываем обработчик файла.
}

// К сожалению, вещи быстро осложняются обработкой ошибок.
// Предположим, fopen может потерпеть неудачу, тогда doSomethingWithTheFile и
// doSomethingElseWithIt вернут коды ошибок, если потерпят неудачу.
//  (Исключения являются предпочтительным способом обработки ошибок,
//   но некоторые программисты, особенно те, кто имеет большой опыт работы с С,
//   не согласны с аргументами о полезности исключений).
// Теперь мы должны проверить каждый вызов на наличие ошибок и закрыть обработчик
// файла, если он есть.
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Открывает файл в режиме чтения
    if (fh == nullptr) // В случае неудачи возвращаемый указатель принимает значение null.
        return false; // Сообщает о неудаче вызывающему.

    // Предположим, каждая функция возвращает false в случае неудачи
    if (!doSomethingWithTheFile(fh)) {
        fclose(fh); // Закрываем обработчик файла, чтобы не было утечек
        return false; // Сообщает об ошибке.
    }
    if (!doSomethingElseWithIt(fh)) {
        fclose(fh); // Закрываем обработчик файла, чтобы не было утечек
        return false; // Сообщает об ошибке.
    }

    fclose(fh); // Закрываем обработчик файла, чтобы не было утечек
    return true; // Указывает на успех
}

// C-программисты часто упорядочивают это с помощью goto:
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r");
    if (fh == nullptr)
        return false;

    if (!doSomethingWithTheFile(fh))
        goto failure;

    if (!doSomethingElseWithIt(fh))
        goto failure;

    fclose(fh); // Закрываем файл.
    return true; // Указывает на успех

failure:
    fclose(fh);
    return false; // Сообщает об ошибке.
}

// Если функции указывают на ошибки с помощью исключений, вещи становятся проще,
// но все еще не оптимальны.
void doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // Открываем файл в режиме чтения
    if (fh == nullptr)
        throw std::runtime_error("Could not open the file.");

    try {
        doSomethingWithTheFile(fh);
        doSomethingElseWithIt(fh);
    }
    catch (...) {
        fclose(fh); // Убедитесь, что закрываете файл, если происходит ошибка.
        throw; // Затем повторно бросает исключение.
    }

    fclose(fh); // Закрываем файл.
    // Успех
}

// Сравните это с использованием класса потока файла (fstream) в С++, который
// использует свой деструктор, чтобы закрыть файл. Еще раз взгляните выше,
// деструктор вызывается автоматически, когда объект выпадает из области видимости.
void doSomethingWithAFile(const std::string& filename)
{
    // ifstream определяет файловый поток
    std::ifstream fh(filename); // Открыть файл

    // Что-то делать с файлом
    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

} // Здесь файл автоматически закрывается в деструкторе.

// Это имеет _огромнейшие_ преимущества:
// 1. Неважно, что произойдет,
//    ресурсы (в данном случае дескриптор файла) будут очищены.
//    После того, как вы правильно напишете деструктор,
//    Больше будет _невозможно_ закрыть обработчик файлов или допустить утечку.
// 2. Обратите внимание, что код намного проще.
//    Деструктор закрывает файловый поток "за кулисами", и вам больше не нужно об
//     этом беспокоиться.
// 3. Код устойчив к исключениям.
//    Исключение может быть брошено в любом месте в функции, и это никак не повлияет
//    на очистку.

// Весь идиоматический код на С++ широко использует RAII для всех ресурсов.
// Дополнительные примеры включат:
// - Использование памяти unique_ptr и shared_ptr
// - Контейнеры - стандартная библиотека связанных списков, векторы
//   (т.е. самоизменяемые массивы), хэш-таблицы и все остальное автоматически
//    уничтожается сразу же, когда выходит за пределы области видимости.
// - Ипользование мьютексов lock_guard и unique_lock

// Контейнеры с пользовательскими классами в качестве ключей требуют
// сравнивающих функций в самом объекте или как указатель на функцию. Примитивы
// имеют компараторы по умолчанию, но вы можете перегрузить их.
class Foo {
public:
	int j;
	Foo(int a) : j(a) {}
};
struct compareFunction {
    bool operator()(const Foo& a, const Foo& b) const {
        return a.j < b.j;
    }
};
// это не допускается (хотя это может варьироваться в зависимости от компилятора)
// std::map<Foo, int> fooMap;
std::map<Foo, int, compareFunction> fooMap;
fooMap[Foo(1)]  = 1;
fooMap.find(Foo(1)); //true

/////////////////////
// Веселые вещи
/////////////////////

// Аспекты С++, которые могут быть удивительными для новичков (и даже для некоторых
// ветеранов). Этот раздел, к сожалению, очень неполон. С++ является одним из самых
// простых языков, где очень легко выстрелить себе в ногу.

// Вы можете перегрузить приватные методы!
class Foo {
  virtual void bar();
};
class FooSub : public Foo {
  virtual void bar();  // Перегружает Foo::bar!
};


// 0 == false == NULL (в основном)!
bool* pt = new bool;
*pt = 0; // Устанавливает значение указателя 'pt' в false.
pt = 0;  // Устанавливает значение 'pt' в нулевой указатель. Обе строки проходят
		// компиляцию без ошибок.

// nullptr приходит на помощь:
int* pt2 = new int;
*pt2 = nullptr; // Не пройдет компиляцию
pt2 = nullptr;  // Устанавливает pt2 в null.

// Существует исключение для булевых значений.
// Это позволит вам проверить указатели с помощью if(!ptr),
// но как следствие вы можете установить nullptr в bool напрямую!
*pt = nullptr;  // Это по прежнему проходит компиляцию, даже если '*pt' - bool!


// '=' != '=' != '='!
// Вызывает Foo::Foo(const Foo&) или некий вариант (смотрите "move semantics")
// конструктора копирования.
Foo f2;
Foo f1 = f2;

// Вызывает Foo::Foo(const Foo&) или вариант, но копирует только часть 'Foo' из
// 'fooSub'. Любые другие члены 'fooSub' пропускаются. Иногда это ужасное поведение
// называют "object slicing."
FooSub fooSub;
Foo f1 = fooSub;

// Вызывает Foo::operator=(Foo&) или вариант.
Foo f1;
f1 = f2;


// Как по-настоящему очистить контейнер:
class Foo { ... };
vector<Foo> v;
for (int i = 0; i < 10; ++i)
  v.push_back(Foo());

// В следующей точке размер v устанавливается в 0, но деструктор не вызывается
// и не происходит очистка ресурсов!
v.empty();
v.push_back(Foo());  // Новые значения копируются в первый вставленный Foo

// Настоящее уничтожение всех значений v. Смотрите раздел о временном объекте
// для объяснения того, как это работает.
v.swap(vector<Foo>());

```
## Дальнейшее чтение:

Наиболее полное и обновленное руководство по С++ можно найти на
<http://cppreference.com/w/cpp>

Дополнительные ресурсы могут быть найдены на <http://cplusplus.com>
---
language: c
filename: learnc-ru.c
contributors:
    - ["Adam Bard", "http://adambard.com/"]
    - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"]
translators:
    - ["Evlogy Sutormin", "http://evlogii.com"]
lang: ru-ru
---

Что ж, Си всё ещё является лидером среди современных высокопроизводительных языков.

Для большинства программистов, Си – это самый низкоуровневый язык на котором они когда-либо писали,
но этот язык даёт больше, чем просто повышение производительности.
Держите это руководство в памяти и вы сможете использовать Си максимально эффективно.

```c
// Однострочный комментарий начинается с // - доступен только после С99.

/*
Многострочный комментарий выглядит так. Работает начиная с С89.
*/

// Импорт файлов происходит с помощью **#include**
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Файлы <в угловых скобочках> будут подключаться из стандартной библиотеки.
// Свои файлы необходимо подключать с помощью "двойных кавычек".
#include "my_header.h"

// Объявление функций должно происходить в .h файлах или вверху .c файла.
void function_1();
void function_2();

// Точка входа в программу – это функция main.
int main() {
    // для форматированного вывода в консоль используется printf
    // %d – означает, что будем выводить целое число, \n переводит указатель вывода
    // на новую строчку
    printf("%d\n", 0); // => напечатает "0"
    // Каждый оператор заканчивается точкой с запятой.

    ///////////////////////////////////////
    // Типы
    ///////////////////////////////////////

    // int обычно имеет длину 4 байта
    int x_int = 0;

    // short обычно имеет длину 2 байта
    short x_short = 0;

    // char гарантированно имеет длину 1 байта
    char x_char = 0;
    char y_char = 'y'; // Символьные литералы заключаются в кавычки ''

    // long как правило занимает от 4 до 8 байт
    // long long занимает как минимум 64 бита
    long x_long = 0;
    long long x_long_long = 0; 

    // float это 32-битное число с плавающей точкой
    float x_float = 0.0;

    // double это 64-битное число с плавающей точкой
    double x_double = 0.0;

    // Целые типы могут быть беззнаковыми.
    unsigned short ux_short;
    unsigned int ux_int;
    unsigned long long ux_long_long;

    // sizeof(T) возвращает размер переменной типа Т в байтах.
    // sizeof(obj) возвращает размер объекта obj в байтах.
    printf("%zu\n", sizeof(int)); // => 4 (на большинстве машин int занимает 4 байта)

    // Если аргуметом sizeof будет выражение, то этот аргумент вычисляется
    // ещё во время компиляции кода (кроме динамических массивов).
    int a = 1;
    // size_t это беззнаковый целый тип который использует как минимум 2 байта
    // для записи размера объекта
    size_t size = sizeof(a++); // a++ не выполнится
    printf("sizeof(a++) = %zu, где a = %d\n", size, a);
    // выведет строку "sizeof(a++) = 4, где a = 1" (на 32-битной архитектуре)

    // Можно задать размер массива при объявлении.
    char my_char_array[20]; // Этот массив занимает 1 * 20 = 20 байт
    int my_int_array[20]; // Этот массив занимает 4 * 20 = 80 байт (сумма 4-битных слов)

    // Можно обнулить массив при объявлении.
    char my_array[20] = {0};

    // Индексация массива происходит также как и в других Си-подобных языках.
    my_array[0]; // => 0

    // Массивы изменяемы. Это просто память как и другие переменные.
    my_array[1] = 2;
    printf("%d\n", my_array[1]); // => 2

    // В C99 (а также опционально в C11), массив может быть объявлен динамически.
    // Размер массива не обязательно должен быть рассчитан на этапе компиляции.
    printf("Enter the array size: "); // спрашиваем юзера размер массива
    char buf[0x100];
    fgets(buf, sizeof buf, stdin);
    size_t size = strtoul(buf, NULL, 10); // strtoul парсит строку в беззнаковое целое
    int var_length_array[size]; // объявление динамического массива
    printf("sizeof array = %zu\n", sizeof var_length_array);
    // Вывод программы (в зависимости от архитектуры) будет таким:
    // > Enter the array size: 10
    // > sizeof array = 40

    // Строка – это просто массив символов, оканчивающийся нулевым (NUL (0x00)) байтом
    // представляемым в строке специальным символом '\0'.
    // Нам не нужно вставлять нулевой байт в строковой литерал,
    // компилятор всё сделает за нас.
    char a_string[20] = "This is a string";
    printf("%s\n", a_string); // %s обозначает вывод строки

    printf("%d\n", a_string[16]); // => 0
    // байт #17 тоже равен 0 (а также 18, 19, и 20)

    // Если между одинарными кавычками есть символ – это символьный литерал,
    // но это тип int, а не char (по историческим причинам).
    
    int cha = 'a'; // хорошо
    char chb = 'a'; // тоже хорошо (подразумевается преобразование int в char)

    ///////////////////////////////////////
    // Операторы
    ///////////////////////////////////////

    // Можно использовать множественное объявление.
    int i1 = 1, i2 = 2;
    float f1 = 1.0, f2 = 2.0;

    // Арифметика обычная
    i1 + i2; // => 3
    i2 - i1; // => 1
    i2 * i1; // => 2
    i1 / i2; // => 0 (0.5, но обрезается до 0)

    f1 / f2; // => 0.5, плюс-минус погрешность потому что,
    // цифры с плавающей точкой вычисляются неточно!

    // Остаток от деления
    11 % 3; // => 2

    // Операции сравнения вам уже знакомы, но в Си нет булевого типа.
    // Вместо него используется int. 0 это false, всё остальное это true.
    // Операции сравнения всегда возвращают 1 или 0.
    3 == 2; // => 0 (false)
    3 != 2; // => 1 (true)
    3 > 2; // => 1
    3 < 2; // => 0
    2 <= 2; // => 1
    2 >= 2; // => 1

    // Си это не Питон – операции сравнения могут быть только парными.
    int a = 1;
    // ОШИБКА:
    int between_0_and_2 = 0 < a < 2;
    // Правильно:
    int between_0_and_2 = 0 < a && a < 2;

    // Логика 
    !3; // => 0 (логическое НЕ)
    !0; // => 1
    1 && 1; // => 1 (логическое И)
    0 && 1; // => 0
    0 || 1; // => 1 (логическое ИЛИ)
    0 || 0; // => 0

    // Битовые операторы
    ~0x0F; // => 0xF0 (побитовое отрицание)
    0x0F & 0xF0; // => 0x00 (побитовое И)
    0x0F | 0xF0; // => 0xFF (побитовое ИЛИ)
    0x04 ^ 0x0F; // => 0x0B (исключающее ИЛИ (XOR))
    0x01 << 1; // => 0x02 (побитовый сдвиг влево (на 1))
    0x02 >> 1; // => 0x01 (побитовый сдвиг вправо (на 1))

    // Будьте осторожны при сдвиге беззнакового int, эти операции не определены:
    // - сдвиг в знаковый бит у целого числа (int a = 1 << 32)
    // - сдвиг влево отрицательных чисел (int a = -1 << 2)

    ///////////////////////////////////////
    // Структуры ветвления
    ///////////////////////////////////////

    // Условный оператор
    if (0) {
      printf("I am never run\n");
    } else if (0) {
      printf("I am also never run\n");
    } else {
      printf("I print\n");
    }

    // Цикл с предусловием
    int ii = 0;
    while (ii < 10) {
        printf("%d, ", ii++); // инкрементация происходит после того как
                              // значение ii передано ("postincrement")
    } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");
    
    //Цикл с постусловием
    int kk = 0;
    do {
        printf("%d, ", kk);
    } while (++kk < 10); // инкрементация происходит перед тем как
                         // передаётся значение kk ("preincrement")
    // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    // Цикл со счётчиком
    int jj;
    for (jj=0; jj < 10; jj++) {
        printf("%d, ", jj);
    } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    // Ветвление с множественным выбором
    switch (some_integral_expression) {
    case 0: // значения должны быть целыми константами (и могут быть выражениями)
        do_stuff();
        break; // если не написать break; то управление будет передано следующему блоку
    case 1:
        do_something_else();
        break;
    default:
        // если не было совпадения, то выполняется блок default:
        fputs("ошибка!\n", stderr);
        exit(-1);
        break;
    }
    
    ///////////////////////////////////////
    // Форматирование вывода
    ///////////////////////////////////////

    // Каждое выражение в Си имеет тип, но вы можете привести один тип к другому, 
    // если хотите (с некоторыми искажениями).

    int x_hex = 0x01; // Вы можете назначать переменные с помощью шестнадцатеричного кода.

    // Приведение типов будет пытаться сохранять цифровые значения.
    printf("%d\n", x_hex); // => Prints 1
    printf("%d\n", (short) x_hex); // => Prints 1
    printf("%d\n", (char) x_hex); // => Prints 1

    // Типы могут переполняться без вызова предупреждения.
    printf("%d\n", (unsigned char) 257); // => 1 (Max char = 255 if char is 8 bits long)

    // Для определения максимального значения типов `char`, `signed char` и `unisigned char`,
    // соответственно используйте CHAR_MAX, SCHAR_MAX и UCHAR_MAX макросы из <limits.h>
    
    // Целые типы могут быть приведены к вещественным и наоборот.
    printf("%f\n", (float)100); // %f formats a float
    printf("%lf\n", (double)100); // %lf formats a double
    printf("%d\n", (char)100.0);

    ///////////////////////////////////////
    // Указатели
    ///////////////////////////////////////

    // Указатель – это переменная которая хранит адрес в памяти.
    // При объявлении указателя указывается тип данных переменной на которую он будет ссылаться.
    // Вы можете получить адрес любой переменной, а потом работать с ним.

    // Используйте & для получения адреса переменной.
    int x = 0;
    printf("%p\n", (void *)&x); // => Напечатает адрес в памяти, где лежит переменная x
    // (%p выводит указатель на void *)

    // Для объявления указателя нужно поставить * перед именем.
    int *px, not_a_pointer; // px это указатель на int
    px = &x; // сохранит адрес x в px
    printf("%p\n", (void *)px); // => Напечатает адрес в памяти, где лежит переменная px
    printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer));
    // => Напечатает "8, 4" в 64 битной системе

    // Для того, чтобы получить значение по адресу, напечатайте * перед именем.
    // Да, * используется при объявлении указателя и для получении значения по адресу
    // немного запутано, но вы привыкнете.
    printf("%d\n", *px); // => Напечатает 0, значение перемененной x

    // Вы также можете изменять значение, на которое указывает указатель.
    (*px)++; // Инкрементирует значение на которое указывает px на единицу
    printf("%d\n", *px); // => Напечатает 1
    printf("%d\n", x); // => Напечатает 1

    // Массивы удобно использовать для большого количества однотипных данных.
    int x_array[20];
    int xx;
    for (xx = 0; xx < 20; xx++) {
        x_array[xx] = 20 - xx;
    } // Объявление x_array с значениями 20, 19, 18,... 2, 1

    // Объявление указателя на int с адресом массива.
    int* x_ptr = x_array;
    // x_ptr сейчас указывает на первый элемент массива (со значением 20). 
    // Это работает, потому что при обращении к имени массива возвращается 
    // указатель на первый элемент.
    // Например, когда массив передаётся в функцию или присваивается указателю, он
    // неяввно преобразуется в указатель.
    // Исключения: когда массив является аргументом для оператор '&':
    int arr[10];
    int (*ptr_to_arr)[10] = &arr; // &arr не является 'int *'!
                                  // он является "указателем на массив" (из десяти 'int'ов).
    // или когда массив это строчный литерал, используемый при объявлении массива символов:
    char arr[] = "foobarbazquirk";
    // или когда массив является аргументом `sizeof` или `alignof` операторов:
    int arr[10];
    int *ptr = arr; // то же самое что и "int *ptr = &arr[0];"
    printf("%zu %zu\n", sizeof arr, sizeof ptr); // напечатает "40, 4" или "40, 8"

    // Декрементация и инкрементация указателей зависит от их типа
    // (это называется арифметика указателей)
    printf("%d\n", *(x_ptr + 1)); // => Напечатает 19
    printf("%d\n", x_array[1]); // => Напечатает 19

    // Вы также можете динамически выделять несколько блоков памяти с помощью
    // функции malloc из стандартной библиотеки, которая принимает один 
    // аргумент типа size_t – количество байт необходимых для выделения.
    int *my_ptr = malloc(sizeof(*my_ptr) * 20);
    for (xx = 0; xx < 20; xx++) {
        *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx
    } // Выделяет память для 20, 19, 18, 17... 2, 1 (как int'ы)

    // Работа с памятью с помощью указателей может давать неожиданные и
    // непредсказуемые результаты.
    printf("%d\n", *(my_ptr + 21)); // => Напечатает кто-нибудь-знает-что? 
                                    // Скорей всего программа вылетит.

    // Когда вы закончили работать с памятью, которую ранее выделили, вам необходимо 
    // освободить её, иначе это может вызвать утечку памяти или ошибки.
    free(my_ptr);

    // Строки это массивы символов, но обычно они представляются как 
    // указатели на символ (как указатели на первый элемент массива).
    // Хорошей практикой считается использование `const char *' при объявлении 
    // строчного литерала. При таком подходе литерал не может быть изменён.
    // (например "foo"[0] = 'a' вызовет ошибку!)

    const char *my_str = "This is my very own string literal";
    printf("%c\n", *my_str); // => 'T'

    // Это не работает, если строка является массивом
    // (потенциально задаваемой с помощью строкового литерала)
    // который находиться в перезаписываемой части памяти:

    char foo[] = "foo";
    foo[0] = 'a'; // это выполнится и строка теперь "aoo"

    void function_1()
} // конец функции main()

///////////////////////////////////////
// Функции
///////////////////////////////////////

// Синтаксис объявления функции:
// <возвращаемый тип> <имя функции>(аргументы)

int add_two_ints(int x1, int x2) {
    return x1 + x2; // Используйте return для возврата значения
}

/*
Данные в функцию передаются "по значению", но никто не мешает
вам передавать в функцию указатели и менять данные по указателям.

Например: инвертировать строку прямо в функции
*/

// void означает, что функция ничего не возвращает
void str_reverse(char *str_in) {
    char tmp;
    int ii = 0;
    size_t len = strlen(str_in); // `strlen()` является частью стандартной библиотеки
    for (ii = 0; ii < len / 2; ii++) {
        tmp = str_in[ii];
        str_in[ii] = str_in[len - ii - 1]; // ii-тый символ с конца
        str_in[len - ii - 1] = tmp;
    }
}

char c[] = "This is a test.";
str_reverse(c);
printf("%s\n", c); // => Выведет ".tset a si sihT"

///////////////////////////////////////
// Типы и структуры определяемые пользователем
///////////////////////////////////////

// typedef используется для задания стандартным типам своих названий
typedef int my_type;
my_type my_type_var = 0;

// Структуры это просто коллекция данных, память выделяется последовательно,
// в том порядке в котором записаны данные.
struct rectangle {
    int width;
    int height;
};

// sizeof(struct rectangle) == sizeof(int) + sizeof(int) – не всегда верно
// из-за особенностей компиляции (необычное поведение при отступах)[1].

void function_1() {
    struct rectangle my_rec;

    // Доступ к структурам через точку
    my_rec.width = 10;
    my_rec.height = 20;

    // Вы можете объявить указатель на структуру
    struct rectangle *my_rec_ptr = &my_rec;

    // Можно получить доступ к структуре и через указатель
    (*my_rec_ptr).width = 30;

    // ... или ещё лучше: используйте оператор -> для лучшей читабельночти
    my_rec_ptr->height = 10; // то же что и "(*my_rec_ptr).height = 10;"
}

// Вы можете применить typedef к структуре, для удобства.
typedef struct rectangle rect;

int area(rect r) {
    return r.width * r.height;
}

// Если вы имеете большую структуру, можно получить доступ к ней "по указателю",
// чтобы избежать копирования всей структуры.
int area(const rect *r) {
    return r->width * r->height;
}

///////////////////////////////////////
// Указатели на функции
///////////////////////////////////////

/*
Во время исполнения функции находятся по известным адресам в памяти.
Указатель на функцию может быть использован для непосредственного вызова функции.
Однако синтаксис может сбивать с толку.

Пример: использование str_reverse по указателю
*/

void str_reverse_through_pointer(char *str_in) {
    // Определение функции через указатель.
    void (*f)(char *); // Сигнатура должна полностью совпадать с целевой функцией.
    f = &str_reverse; // Присвоить фактический адрес (во время исполнения)
    // "f = str_reverse;" тоже будет работать.
    //Имя функции (как и массива) возвращает указатель на начало.
    (*f)(str_in); // Просто вызываем функцию через указатель.
    // "f(str_in);" или вот так
}
```

## На почитать

Лучше всего найдите копию [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language)
Это **книга** написанная создателями Си. Но будьте осторожны, она содержит идеи которые больше не считаются хорошими.

Другой хороший ресурс: [Learn C the hard way](http://c.learncodethehardway.org/book/).

Если у вас появился вопрос, почитайте [compl.lang.c Frequently Asked Questions](http://c-faq.com).

Очень важно использовать правильные отступы и ставить пробелы в нужных местах.
Читаемый код лучше чем красивый или быстрый код.
Чтобы научиться писать хороший код, почитайте [Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle).

Также не забывайте, что [Google](http://google.com) и [Яндекс](http://yandex.ru) – ваши хорошие друзья.

[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member
---
language: clojure
filename: learnclojure-ru.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Alexey Pirogov", "http://twitter.com/alex_pir"]
lang: ru-ru
---

Clojure, это представитель семейства Lisp-подобных языков, разработанный
для Java Virtual Machine. Язык идейно гораздо ближе к чистому
[функциональному программированию](https://ru.wikipedia.org/wiki/%D0%A4%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D0%BE%D0%BD%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B5_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5) чем его прародитель Common Lisp, но в то же время обладает набором инструментов для работы с состоянием,
таких как [STM](https://ru.wikipedia.org/wiki/Software_transactional_memory).

Благодаря такому сочетанию технологий в одном языке, разработка программ,
предполагающих конкурентное выполнение, значительно упрощается
и даже может быть автоматизирована.

(Последующие примеры кода предполагают выполнение в Clojure версии 1.2 и выше)


```clojure
; Комментарии начинаются символом ";".

; Код на языке Clojure записывается в виде "форм",
; которые представляют собой обычные списки элементов, разделенных пробелами,
; заключённые в круглые скобки
;
; Clojure Reader (инструмент языка, отвечающий за чтение исходного кода),
; анализируя форму, предполагает, что первым элементом формы (т.е. списка)
; является функция или макрос, который следует вызвать, передав ему
; в качестве аргументов остальные элементы списка-формы.

; Первым вызовом в файле должен быть вызов функции ns,
; которая отвечает за выбор текущего пространства имен (namespace)
(ns learnclojure-ru)

; Несколько простых примеров:

; str объединяет в единую строку все свои аргументы
(str "Hello" " " "World") ; => "Hello World"

; Арифметика тоже выглядит несложно
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; Проверка на равенство (Equality)
(= 1 1) ; => true
(= 2 1) ; => false

; Для булевой логики вам может понадобиться not
(not true) ; => false

; Вложенные формы, конечно же, допустимы и работают вполне предсказуемо
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; Типы
;;;;;;;;;;;;;

; Clojure использует типы Java для представления булевых значений,
; строк и чисел
; Узнать тип мы можем, использую функцию `class
(class 1)     ; Целочисленные литералы типа по-умолчанию являются java.lang.Long
(class 1.)    ; Числа с плавающей точкой, это java.lang.Double
(class "")    ; Строки всегда заключаются в двойные кавычки
              ; и представляют собой java.lang.String
(class false) ; Булевы значения, это экземпляры java.lang.Boolean
(class nil)   ; "Пустое" значение называется "nil"

; Если Вы захотите создать список из чисел, вы можете просто
; предварить форму списка символом "'", который подскажет Reader`у,
; что эта форма не требует вычисления
'(+ 1 2) ; => (+ 1 2)
; ("'", это краткая запись формы (quote (+ 1 2))

; "Квотированный" список можно вычислить, передав его функции eval
(eval '(+ 1 2)) ; => 3

; Коллекции и Последовательности
;;;;;;;;;;;;;;;;;;;

; Списки (Lists) в clojure структурно представляют собой "связанные списки",
; тогда как Векторы (Vectors), устроены как массивы.
; Векторы и Списки тоже являются классами Java!
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; Список может быть записан, как (1 2 3), но в этом случае
; он будет воспринят reader`ом, как вызов функции.
; Есть два способа этого избежать:
; '(1 2 3)     - квотирование,
; (list 1 2 3) - явное конструирование списка с помощью функции list.

; "Коллекции", это некие наборы данных
; И списки, и векторы являются коллекциями:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; "Последовательности" (seqs), это абстракция над наборами данных,
; элементы которых "упакованы" последовательно.
; Списки - последовательности, а вектора - нет.
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; Любая seq предоставляет доступ только к началу последовательности данных,
; не предоставляя информацию о её длине.
; При этом последовательности могут быть и бесконечными,
; т.к. являются ленивыми и предоставляют данные только по требованию!
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (бесконечная последовательность!)
(take 4 (range)) ;  (0 1 2 3)

; Добавить элемент в начало списка или вектора можно с помощью функции cons
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; Функция conj добавляет элемент в коллекцию
; максимально эффективным для неё способом.
; Для списков эффективно добавление в начло, а для векторов - в конец.
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; Функция concat объединяет несколько списков и векторов в единый список
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; Работать с коллекциями удобно с помощью функций filter и map
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; reduce поможет "свернуть" коллекцию
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; Вызывая reduce, мы можем указать начальное значение
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; Функции
;;;;;;;;;;;;;;;;;;;;;

; Функция создается специальной формой fn.
; "Тело" функции может состоять из нескольких форм,
; но результатом вызова функции всегда будет результат вычисления
; последней из них.
(fn [] "Hello World") ; => fn

; (Вызов функции требует "оборачивания" fn-формы в форму вызова)
((fn [] "Hello World")) ; => "Hello World"

; Назначить значению имя можно специальной формой def
(def x 1)
x ; => 1

; Назначить имя можно любому значению, в т.ч. и функции:
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; Поскольку именование функций - очень частая операция,
; clojure позволяет, сделать это проще:
(defn hello-world [] "Hello World")

; Вектор [] в форме описания функции, следующий сразу за именем,
; описывает параметры функции:
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; Одна функция может иметь сразу несколько наборов аргументов:
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; Также функция может иметь набор аргументов переменной длины
(defn count-args [& args] ; args будет содержать seq аргументов
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"

; Можно комбинировать оба подхода задания аргументов
(defn hello-count [name & args]
  (str "Hello " name ", you passed " (count args) " extra args"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"

; Для создания анонимных функций есть специальный синтаксис:
; функциональные литералы
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; такие функциональные литералы удобно использовать с map, filter и reduce
(map #(* 10 %1) [1 2 3 5])          ; => (10 20 30 50)
(filter #(> %1 3) [1 2 3 4 5 6 7])  ; => (4 5 6 7)
(reduce #(str %1 "," %2) [1 2 3 4]) ; => "1,2,3,4"

; Отображения (Maps)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Hash maps и array maps имеют одинаковый интерфейс.
; Hash maps производят поиск по ключу быстрее, но не сохраняют порядок ключей
(class {:a 1 :b 2 :c 3})          ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; Array maps автоматически преобразуются в hash maps,
; как только разрастутся до определенного размера

; Отображения могут использовать в качестве ключей любые хэшируемые значения,
; однако предпочтительными являются ключи,
; являющиеся "ключевыми словами" (keywords)
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; Предыдущий пример содержит запятые в коде, однако reader не использует их,
; при обработке литералов - запятые просто воспринимаются,
; как "пробельные символы" (whitespaces)

; Отображение может выступать в роли функции, возвращающей значение по ключу
(stringmap "a")          ; => 1
(keymap :a)              ; => 1

; При попытке получить отсутствующее значение, будет возвращён nil
(stringmap "d") ; => nil

; Иногда бывает удобно указать конкретное значение по-умолчанию:
({:a 1 :b 2} :c "Oops!") ; => "Oops!"

; Keywords тоже могут использоваться в роли функций!
(:b keymap) ; => 2

; Однако этот фокус не пройдёт со строками.
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; Добавить пару ключ-значение в отображение можно функцией assoc
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; Но всегда следует помнить, что значения в Clojure - неизменяемые!
keymap ; => {:a 1, :b 2, :c 3} - оригинал не был затронут

; dissoc позволяет исключить значение по ключу
(dissoc keymap :a :b) ; => {:c 3}

; Множества (Sets)
;;;;;;;;;;;;;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; Добавляются элементы посредством conj
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; Исключаются - посредством disj
(disj #{1 2 3} 1) ; => #{2 3}

; Вызов множества, как функции, позволяет проверить
; принадлежность элемента этому множеству:
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; В пространстве имен clojure.sets
; содержится множество функций для работы с множествами

; Полезные формы
;;;;;;;;;;;;;;;;;

; Конструкции ветвления в clojure, это обычные макросы
; и подобны их собратьям в других языках:
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; Специальная форма let позволяет присвоить имена значениям локально.
; При этом все изменения будут видны только вложенным формам:
(def a 10)
(let [a 1 b 2]
  (> a b)) ; => false

; Несколько форм можно объединить в одну форму посредством do
; Значением do-формы будет значение последней формы из списка вложенных в неё:
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; Множество макросов содержит внутри себя неявную do-форму.
; Пример - макрос определения функции:
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; Ещё один пример - let:
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")

; Модули
;;;;;;;;;

; Форма "use" позволяет добавить в текущее пространство имен
; все имена (вместе со значениями) из указанного модуля:
(use 'clojure.set)

; Теперь нам доступны операции над множествами:
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; use позволяет указать, какие конкретно имена
; должны быть импортированы из модуля:
(use '[clojure.set :only [intersection]])

; Также модуль может быть импортирован формой require
(require 'clojure.string)

; После этого модуль становится доступен в текущем пространстве имен,
; а вызов его функций может быть осуществлен указанием полного имени функции:
(clojure.string/blank? "") ; => true

; Импортируемому модулю можно назначить короткое имя:
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (Литерал вида #"" обозначает регулярное выражение)

; Вместо отдельной формы require (и use, хотя это и не приветствуется) можно
; указать необходимые модули прямо в форме ns:
(ns test
  (:require
    [clojure.string :as str] ; Внимание: при указании внутри формы ns
    [clojure.set :as set]))  ; имена пакетов не квотируются!

; Java
;;;;;;;

; Стандартная библиотека Java очень богата,
; и всё это богатство доступно и для Clojure!

; import позволяет импортировать модули Java
(import java.util.Date)

; В том числе и из ns
(ns test
  (:import java.util.Date
           java.util.Calendar))

; Имя класса, сопровождаемое символом "." позволяет
; инстанцировать объекты Java-классов:
(Date.) ; <a date object>

; форма . позволяет вызывать методы:
(. (Date.) getTime) ; <a timestamp>
(.getTime (Date.))  ; а можно и так

; Статические методы вызываются как функции модуля:
(System/currentTimeMillis) ; <a timestamp> (Модуль system всегда доступен!)

; doto позволяет удобно работать с объектами, изменяющими свое состояние
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => A Date. set to 2000-01-01 00:00:00

; Работа с изменяемым сотоянием
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Clojure предоставляет набор инструментов
; для работы с изменяемым состоянием: Software Transactional Memory.
; Структуры STM представлены тремя типами:
; - атомы (atoms)
; - агенты (agents)
; - ссылки (references)

; Самые простые хранители состояния - атомы:
(def my-atom (atom {})) ; {} - начальное состояние атома

; Обновляется атом посредством swap!.
; swap! применяет функцию аргумент к текущему значению
; атома и помещает в атом результат
(swap! my-atom assoc :a 1) ; Обновляет my-atom, помещая в него (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Обновляет my-atom, помещая в него (assoc {:a 1} :b 2)

; Получить значение атома можно посредством '@'
; (провести так называемую операцию dereference)
my-atom  ;=> Atom<#...> (Возвращает объект типа Atom)
@my-atom ; => {:a 1 :b 2}

; Пример реализации счётчика на атоме
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; С другими STM-конструкциями - refs и agents - можно ознакомиться тут:
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
```

### Для будущего чтения

Это руководство не претендует на полноту, но мы смеем надеяться, способно вызвать интерес к дальнейшему изучению языка.

Clojure.org - сайт содержит большое количество статей по языку:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org - сайт документации языка с примерами использования функций:
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure - отличный способ закрепить навыки программирования на clojure, решая задачи вместе с коллегами со всего мира:
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org (да, именно) неплохой перечень статей для начинающих:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
  - ["asaskevich", "http://github.com/asaskevich"]
filename: learncoffee-ru.coffee
lang: ru-ru
---

CoffeeScript - это небольшой язык, который компилируется один-в-один в эквивалентный код на языке JavaScript, а потому он не интерпретируется во время исполнения JavaScript кода.
Ключевой особенностью CoffeeScript является то, что он пытается создать читабельный, качественно оформленный и плавный JavaScript код, прекрасно работающий в любой среде JavaScript.

Также загляните на официальный сайт [языка](http://coffeescript.org/), где можно найти весьма полное учебное пособие по CoffeeScript.

```coffeescript
# CoffeeScript - язык хипстеров.
# Язык использует самое модное из множества современных языков.
# Эти комментарии по стилю похожи на комментарии Ruby или Python, они используют "решетку" в качестве знака комментария.

###
Блоки комментариев выделяются тремя символами "решетки", в результирующем JavaScript коде они будут преобразованы в  '/ * и '* /'.

Перед тем, как идти далее, Вам нужно понимать семантику JavaScript.
###

# Присвоение:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# Условия:
number = -42 if opposite #=> if(opposite) { number = -42; }

# Функции:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

fill = (container, liquid = "coffee") ->
  "Заполняем #{container} жидкостью #{liquid}..."
#=>var fill;
#
#fill = function(container, liquid) {
#  if (liquid == null) {
#    liquid = "coffee";
#  }
#  return "Заполняем " + container + " жидкостью " + liquid + "...";
#};

# Списки и диапазоны:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# Объекты:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#  "root": Math.sqrt,
#  "square": square,
#  "cube": function(x) { return x * square(x); }
#}

# Многоточия:
race = (winner, runners...) ->
  print winner, runners
#=>race = function() {
#  var runners, winner;
#  winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#  return print(winner, runners);
#};

# Проверка на существование объекта:
alert "Так и знал!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("Так и знал!"); }

# Итерации по массивам:
cubes = (math.cube num for num in list) 
#=>cubes = (function() {
#	var _i, _len, _results;
#	_results = [];
# 	for (_i = 0, _len = list.length; _i < _len; _i++) {
#		num = list[_i];
#		_results.push(math.cube(num));
#	}
#	return _results;
#  })();

foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
#=>foods = ['broccoli', 'spinach', 'chocolate'];
#
#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
#  food = foods[_k];
#  if (food !== 'chocolate') {
#    eat(food);
#  }
#}
```

## На почитать

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
- [CoffeeScript на русском](http://cidocs.ru/coffeescript/)
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
    - ["Geoffrey Liu", "https://github.com/g-liu"]
filename: learncss-ru.css
lang: ru-ru
---

В свои ранние дни веб состоял в основном из чистого текста. С развитием браузеров
веб-страницы с графическими элементами стали обычным делом.
CSS - язык, разграничивающий содержимое (HTML) и внешний вид веб-страниц.

Если коротко, то CSS предоставляет синтаксис, позволяющий выбирать различные
HTML элементы и определять их внешний вид.

Как и у других языков, у CSS много версий. Мы описываем CSS2.0 - не самую новую,
но самую поддерживаемую и распространенную версию.

**ВАЖНО:** Так как результатом применения CSS является изменение внешнего вида
элементов, постарайтесь использовать CSS-песочницы при изучении языка.
Например [dabblet](http://dabblet.com/).
В данной статье рассматриваются в первую очередь синтаксис и общие рекомендации.


```css
/* Для комментариев используется слеш-астериск, как на этой строчке.
   В CSS нет однострочных комментариев; все комментарии записываются таким способом */

/* ####################
   ## СЕЛЕКТОРЫ
   #################### */

/* Выражения в CSS очень просты */
селектор { атрибут: значение; /* другие атрибуты...*/ }

/* селекторы используются для выбора элементов на странице

Чтобы выбрать все элементы, используйте астериск: */
* { color:red; }

/*
Если на странице присутствует такой элемент:

<div class='some-class class2' id='someId' attr='value' otherAttr='en-us foo bar' />
*/

/* его можно выбрать по одному классу */
.some-class { }

/* или по обоим классам */
.some-class.class2 { }

/* по названию тега */
div { }

/* по идентификатору */
#someId { }

/* по имеющемуся атрибуту */
[attr] { font-size:smaller; }

/* или по атрибуту с определенным значением */
[attr='value'] { font-size:smaller; }

/* можно выбрать атрибуты, начинающиеся с определенного значения (CSS3) */
[attr^='val'] { font-size:smaller; }

/* или заканчивающиеся определенным значением (CSS3) */
[attr$='ue'] { font-size:smaller; }

/* содержащие отделенное пробелами значение в названии атрибута (CSS3)  */
[otherAttr~='foo'] { font-size:smaller; }

/* можно выбрать атрибут как с точным, так и со стоящим после значения “-” (U+002D) */
[otherAttr|='en'] { font-size:smaller; }


/* Более того, все это можно использовать вместе - между разными частями
не должно быть пробелов, иначе селектор будет иметь совершенно иное значение  */
div.some-class[attr$='ue'] { }

/* Вы можете выбрать элемент по его родителю */

/* прямой потомок другого элемента (выбранного с помощью селектора) */
div.some-parent > .class-name {}

/* потомок любого родителя в дереве элементов
   следующая строка означает: "любой элемент класса "class-name",
   являющийся потомком div-элемента класса "some-parent"
   НЕЗАВИСИМО ОТ УРОВНЯ ВЛОЖЕННОСТИ" */
div.some-parent .class-name {}

/* важно: этот же селектор без пробелов имеет иное значение
   можете догадаться, какое? */
div.some-parent.class-name {}

/* вы можете выбрать элемент по первому предшествующему
   родственному элементу */
.i-am-before + .this-element { }

/* или любому предшествующему родственнику перед элементом */
.i-am-any-before ~ .this-element {}


/* Существуют псевдо-классы, позволяющие изменять внешний вид элемента
   в зависимости от событий, произошедших с элементом */

/* например, когда курсор наведен на элемент */
element:hover {}

/* когда пользователь проходил по ссылке ранее */
element:visited {}

/* или еще не проходил по ней */
element:link {}

/* выбранное поле ввода (input) */
element:focus {}


/* ####################
   ## АТРИБУТЫ
   #################### */

selector {
    
    /* Единицы измерения */
    width: 50%; /* проценты */
    font-size: 2em; /* умножается на высоту шрифта (2em - в два раза больше) */
    width: 200px; /* пиксели */
    font-size: 20pt; /* пункты */
    width: 5cm; /* сантиметры */
    min-width: 50mm; /* миллиметры */
    max-width: 5in; /* дюймы */
    height: 0.2vh; /* умножается на высоту окна браузера (CSS3) */
    width: 0.4vw; /* умножается на ширину окна браузера (CSS3) */
    min-height: 0.1vmin; /* наименьшее из vh и vw (CSS3) */
    max-width: 0.3vmax; /* наибольшее из vh и vw (CSS3) */
    
    /* Цвета */
    background-color: #F6E;  /* сокращенная запись шестнадцатеричного кода */
    background-color: #F262E2; /* стандартная запись шестнадцатеричного кода */
    background-color: tomato; /* название цвета */
    background-color: rgb(255, 255, 255); /* цветовая модель rgb */
    background-color: rgb(10%, 20%, 50%); /* цветовая модель rgb в процентах */
    background-color: rgba(255, 0, 0, 0.3); /* цветовая модель rgb (последний аргумент отвечает за прозрачность цвета) (CSS3) */
    background-color: transparent; /* прозрачный цвет */
    background-color: hsl(0, 100%, 50%); /* в формате hsl (CSS3) */
    background-color: hsla(0, 100%, 50%, 0.3); /* в формате hsl (последний аргумент отвечает за непрозрачность цвета) (CSS3) */

    
    /* Изображения */
    background-image: url(/path-to-image/image.jpg); /* кавычки внутри url() опциональны */
    
    /* Шрифты */
    font-family: Arial;
    font-family: "Courier New"; /* если в названии есть пробелы, заключите его в кавычки */
    font-family: "Courier New", Trebuchet, Arial, sans-serif; /* если шрифт не найден,
                             будет использован следующий за ним в списке */
}

```

## Использование

Сохраните готовый файл с расширением .css

```xml
<!-- добавьте css файл в тег <head> на странице: -->
<link rel='stylesheet' type='text/css' href='path/to/style.css' />

<!-- Можно использовать встроенные стили. Рекомендуется избегать подобного подхода. -->
<body>
  <style>
     a { color: purple; }
  </style>
</body>

<!-- Можно установить стиль элемента напрямую.
Используйте этот способ только в случае крайней необходимости. -->
<div style="border: 1px solid red;">
</div>

```

## Приоритет

Как вы заметили, внешний вид элемента может определяться несколькими селекторами,
а значение атрибута элемента может быть установлено больше одного раза.
В подобных случаях одно из значений оказывается приоритетнее остальных.

Если взять следующую таблицу стилей:

```css
/*A*/
p.class1[attr='value']

/*B*/
p.class1 {}

/*C*/
p.class2 {}

/*D*/
p {}

/*E*/
p { property: value !important; }

```

и следующую разметку:

```xml
<p style='/*F*/ property:value;' class='class1 class2' attr='value'>
</p>
```

Приоритет стилей будет таким:
Помните: приоритет выставляется для **атрибута**, а не для всего блока стилей.

* `E` имеет наивысший приоритет благодаря ключевому слову `!important`.
    Используйте только в случае крайней необходимости.
* `F` идет следующим, так как является встроенным стилем.
* `A` следующий, как самый конкретизированный.  
    конкретизированный == большее количество определителей.
    В этом примере 3 определителя: 1 тег `p` +   
    название класса `class1` + 1 атрибут `attr='value'`
* `C` следующий. Несмотря на одинаковое с `B` количество определителей,
    `C` определен позже.
* Затем `B`
* И последний `D`.

## Совместимость

Несмотря на то, что большая часть функций CSS2 (а также CSS3) поддерживается всеми
браузерами и устройствами, не забывайте проверять совместимость CSS-правил
с современными браузерами.

[QuirksMode CSS](http://www.quirksmode.org/css/) замечательно подходит для этого.

To run a quick compatibility check, [CanIUse](http://caniuse.com) is a great resource.

## Ресурсы для самостоятельного изучения

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
---
language: D
filename: learnd-ru.d
contributors:
    - ["Anton Pastukhov", "http://dprogramming.ru/"]
    - ["Robert Brights-Gray", "http://lhs-blog.info/"]
    - ["Andre Polykanine", "http://oire.me/"]
lang: ru-ru
---

D - современный компилируемый язык общего назначения с Си-подобным синтаксисом,
который сочетает удобство, продуманный дизайн и высокую производительность.
D - это С++, сделанный правильно.

```c
// Welcome to D! Это однострочный комментарий

/* многострочный
   комментарий  */

/+
    // вложенные комментарии

    /* еще вложенные
       комментарии */

    /+
        // мало уровней вложенности? Их может быть сколько угодно.
    +/
+/

/*
    Имя модуля. Каждый файл с исходным кодом на D — модуль.
    Если имя не указано явно, то предполагается, что оно совпадает с именем
    файла. Например, для файла "test.d" имя модуля будет "test", если явно
    не указать другое
 */
module app;

// импорт модуля. Std — пространство имен стандартной библиотеки (Phobos)
import std.stdio;

// можно импортировать только нужные части, не обязательно модуль целиком
import std.exception : enforce;

// точка входа в программу — функция main, аналогично C/C++
void main()
{
    writeln("Hello, world!");
}



/*** типы и переменные ***/

int a; // объявление переменной типа int (32 бита)
float b = 12.34; // тип с плавающей точкой
double c = 56.78; // тип с плавающей точкой (64 бита)

/*
    Численные типы в D, за исключением типов с плавающей точкой и типов
    комплексных чисел, могут быть беззнаковыми.
    В этом случае название типа начинается с префикса "u"
*/
uint d = 10; ulong e = 11;
bool b = true; // логический тип
char d = 'd';  // UTF-символ, 8 бит. D поддерживает UTF "из коробки"
wchar e = 'é';   // символ UTF-16
dchar f;       // и даже UTF-32, если он вам зачем-то понадобится

string s = "для строк есть отдельный тип, это не просто массив char-ов из Си";
wstring ws = "поскольку у нас есть wchar, должен быть и  wstring";
dstring ds = "...и dstring, конечно";

string кириллица = "Имена переменных должны быть в Unicode, но не обязательно на латинице.";

typeof(a) b = 6; // typeof возвращает тип своего выражения.
                 // В результате, b имеет такой же тип, как и a

// Тип переменной, помеченной ключевым словом auto,
// присваивается компилятором исходя из значения этой переменной
auto x = 1;              // Например, тип этой переменной будет int.
auto y = 1.1;            // этой — double
auto z = "Zed is dead!"; // а этой — string

int[3] arr = [1, 2, 3]; // простой одномерный массив с фиксированным размером
int[] arr2 = [1, 2, 3, 4]; // динамический массив
int[string] aa = ["key1": 5, "key2": 6]; // ассоциативный массив

/*
   Строки и массивы в D — встроенные типы. Для их использования не нужно
   подключать ни внешние, ни даже стандартную библиотеку, хотя в последней
   есть множество дополнительных инструментов для работы с ними.
 */
immutable int ia = 10;  // неизменяемый тип,
                        // обозначается ключевым словом immutable
ia += 1;                // — вызовет ошибку на этапе компиляции

// перечислимый (enumerable) тип,
// более правильный способ работы с константами в D
enum myConsts = { Const1, Const2, Const3 };

// свойства типов
writeln("Имя типа               : ", int.stringof); // int
writeln("Размер в байтах        : ", int.sizeof);   // 4
writeln("Минимальное значение   : ", int.min);      // -2147483648
writeln("Максимальное значение : ", int.max);      // 2147483647
writeln("Начальное значение     : ", int.init);     // 0. Это значение,
                                                    // присвоенное по умолчанию

// На самом деле типов в D больше, но все мы здесь описывать не будем,
// иначе не уложимся в Y минут.



/*** Приведение типов ***/

// to!(имя типа)(выражение) - для большинства конверсий
import std.conv : to; // функция "to" - часть стандартной библиотеки, а не языка
double d = -1.75;
short s = to!short(d); // s = -1

/*
   cast - если вы знаете, что делаете. Кроме того, это единственный способ
   преобразования типов-указателей в "обычные" и наоборот
*/
void* v;
int* p = cast(int*)v;

// Для собственного удобства можно создавать псевдонимы
// для различных встроенных объектов
alias int newInt; // теперь можно обращаться к newInt так, как будто бы это int
newInt a = 5;

alias newInt = int; // так тоже допустимо
alias uint[2] pair; // дать псевдоним можно даже сложным структурам данных



/*** Операторы ***/

int x = 10; // присваивание
x = x + 1;  // 11
x -= 2;     // 9
x++;        // 10
++x;        // 11
x *= 2;     // 22
x /= 2;     // 11
x = x ^^ 2; // 121 (возведение в степень)
x ^^= 2;    // 1331 (то же самое)

string str1 = "Hello";
string str2 = ", world!";
string hw = str1 ~ str2; // Конкатенация строк

int[] arr = [1, 2, 3];
arr ~= 4; // [1, 2, 3, 4] - добавление элемента в конец массива



/*** Логика и сравнения ***/

int x = 0; int y = 1;

x == y;         // false
x > y;          // false
x < y;          // true
x >= y;         // false
x != y;         // true. ! — логическое "не"
x > 0 || x < 1; // true. || — логическое "или"
x > 0 && x < 1; // false && — логическое "и"
x ^ y           // true; ^ - xor (исключающее "или")

// Тернарный оператор
auto y = (x > 10) ? 1 : 0; // если x больше 10, то y равен 1,
                           // в противном случае y равен нулю


/***  Управляющие конструкции  ***/

// if - абсолютно привычен
if (a == 1) {
    // ..
} else if (a == 2) {
    // ..
} else {
    // ..
}

// switch
switch (a) {
    case 1:
        // делаем что-нибудь
        break;
    case 2:
        // делаем что-нибудь другое
        break;
    case 3:
        // делаем что-нибудь еще
        break;
    default:
        // default обязателен, без него будет ошибка компиляции
        break;
}

// в D есть констукция "final switch". Она не может содержать секцию "defaul"
// и применяется, когда все перечисляемые в switch варианты должны быть
// обработаны явным образом

int dieValue = 1;
final switch (dieValue) {
    case 1:
        writeln("You won");
        break;

    case 2, 3, 4, 5:
        writeln("It's a draw");
        break;

    case 6:
        writeln("I won");
        break;
}

// while
while (a > 10) {
    // ..
    if (number == 42) {
        break;
    }
}

while (true) {
    // бесконечный цикл
}

// do-while
do {
    // ..
} while (a == 10);

// for
for (int number = 1; number < 11; ++number) {
    writeln(number); // все абсолютно стандартно
}

for ( ; ; ) {
    // секции могут быть пустыми. Это бесконечный цикл в стиле Си
}

// foreach - универсальный и самый "правильный" цикл в D
foreach (element; array) {
    writeln(element); // для простых массивов
}

foreach (key, val; aa) {
    writeln(key, ": ", val); // для ассоциативных массивов
}

foreach (c; "hello") {
    writeln(c); // hello. Поскольку строки - это вариант массива,
                // foreach применим и к ним
}

foreach (number; 10..15) {
    writeln(number); // численные интервалы можно указывать явным образом
                     // этот цикл выведет значения с 10 по 14, но не 15,
                     // поскольку диапазон не включает в себя верхнюю границу
}

// foreach_reverse - в обратную сторону
auto container = [1, 2, 3];
foreach_reverse (element; container) {
    writefln("%s ", element); // 3, 2, 1
}

// foreach в массивах и им подобных структурах не меняет сами структуры
int[] a = [1, 2 ,3 ,4 ,5];
foreach (elem; array) {
    elem *= 2; // сам массив останется неизменным
}

writeln(a); // вывод: [1, 2, 3, 4, 5] Т.е изменений нет

// добавление ref приведет к тому, что массив будет изменяться
foreach (ref elem; array) {
    elem *= 2;
}

writeln(a); // [2, 4, 6, 8, 10]

// foreach умеет рассчитывать индексы элементов
int[] a = [1, 2, 3, 4, 5];
foreach (ind, elem; array) {
    writeln(ind, " ", elem); // через ind - доступен индекс элемента,
                             // а через elem - сам элемент
}



/*** Функции ***/

test(42); // Что, вот так сразу? Разве мы где-то уже объявили эту функцию?

// Нет, вот она. Это не Си, здесь объявление функции не обязательно должно быть
// до первого вызова
int test(int argument) {
    return argument * 2;
}


// В D используется единый синтаксис вызова функций
// (UFCS, Uniform Function Call Syntax), поэтому так тоже можно:
int var = 42.test();

// и даже так, если у функции нет аргументов:
int var2 = 42.test;

// можно выстраивать цепочки:
int var3 = 42.test.test;

/*
    Аргументы в функцию передаются по значению (т.е. функция работает не с
    оригинальными значениями, переданными ей, а с их локальными копиями.
    Исключение составляют объекты классов, которые передаются по ссылке.
    Кроме того, любой параметр можно передать в функцию по ссылке с помощью
    ключевого слова "ref"
*/
int var = 10;

void fn1(int arg) {
    arg += 1;
}

void fn2(ref int arg) {
    arg += 1;
}

fn1(var); // var все еще = 10
fn2(var); // теперь var = 11

// Возвращаемое значение тоже может быть auto,
// если его можно "угадать" из контекста
auto add(int x, int y) {
    return x + y;
}

auto z = add(x, y); // тип int - компилятор вывел его автоматически

// Значения аргументов по умолчанию
float linearFunction(float k, float x, float b = 1)
{
    return k * x + b;
}

auto linear1 = linearFunction(0.5, 2, 3); // все аргументы используются
auto linear2 = linearFunction(0.5, 2);    // один аргумент пропущен, но в функции
                                          // он все равно использован и равен 1

// допускается описание вложенных функций
float quarter(float x) {
    float doubled(float y) {
        return y * y;
    }

    return doubled(doubled(x));
}

// функции с переменным числом аргументов
int sum(int[] a...)
{
    int s = 0;
    foreach (elem; a) {
        s += elem;
    }
    return s;
}

auto sum1 = sum(1);
auto sum2 = sum(1,2,3,4);

/*
   модификатор "in" перед аргументами функций говорит о том, что функция имеет
    право их только просматривать. При попытке модификации такого аргумента
    внутри функции - получите ошибку
*/
float printFloat(in float a)
{
    writeln(a);
}
printFloat(a); // использование таких функций - самое обычное

// модификатор "out" позволяет вернуть из функции несколько результатов
// без посредства глобальных переменных или массивов
uint remMod(uint a, uint b, out uint modulus)
{
    uint remainder = a / b;
    modulus = a % b;
    return remainder;
}

uint modulus;                   // пока в этой переменной ноль
uint rem = remMod(5, 2, modulus); // наша "хитрая" функция, и теперь
                                // в modulus - остаток от деления
writeln(rem, " ", modulus);     // вывод: 2 1



/*** Структуры, классы, базовое ООП ***/

// Объявление структуры. Структуры почти как в Си
struct MyStruct {
    int a;
    float b;

    void multiply() {
        return a * b;
    }
}

MyStruct str1; // Объявление переменной с типом MyStruct
str1.a = 10;   // Обращение к полю
str1.b = 20;
auto result = str1.multiply();
MyStruct str2 = {4, 8}          // Объявление + инициализация  в стиле Си
auto str3 = MyStruct(5, 10);    // Объявление + инициализация  в стиле D


// области видимости полей и методов - 3 способа задания
struct MyStruct2 {
    public int a;

    private:
        float b;
        bool c;

    protected {
        float multiply() {
            return a * b;
        }
    }
    /*
        в дополнение к знакомым public, private и protected, в D есть еще
        область видимости "package". Поля и методы с этим атрибутом будут
        доступны изо всех модулей, включенных в "пакет" (package), но не
        за его пределами. package - это "папка", в которой может храниться
        несколько модулей. Например, в "import.std.stdio", "std" - это
        package, в котором есть модуль stdio (и еще множество других)
    */
    package:
        string d;

    /* помимо этого, имеется еще один модификатор - export, который позволяет
    использовать объявленный с ним идентификатор даже вне самой программы !
    */
    export:
         string description;
}

// Конструкторы и деструкторы
struct MyStruct3 {
    this() { // конструктор. Для структур его не обязательно указывать явно,
             // в этом случае пустой конструктор добавляется компилятором
        writeln("Hello, world!");
    }


    // а вот это конструкция - одна из интересных идиом и представляет собой
    // конструктор копирования, т.е конструктор, возвращающий копию структуры.
    // Работает только в структурах.
    this(this)
    {
        return this;
    }

    ~this() { // деструктор, также необязателен
        writeln("Awww!");
    }
}

// Объявление простейшего класса
class MyClass {
    int a;  // в D по умолчанию данные-члены являются public
    float b;
}

auto mc = new MyClass(); // ...и создание его экземпляра
auto mc2 = new MyClass; // ... тоже сработает

// Конструктор
class MyClass2 {
    int a;
    float b;

    this(int a, float b) {
        this.a = a; // ключевое слово "this" - ссылка на объект класса
        this.b = b;
    }
}

auto mc2 = new MyClass2(1, 2.3);

// Классы могут быть вложенными
class Outer
{
    int m;

    class Inner
    {
        int foo()
        {
            return m; // можно обращаться к полям "внешнего" класса
        }
    }
}

// наследование
class Base {
    int a = 1;
    float b = 2.34;


    // это статический метод, т.е метод который можно вызывать, обращаясь
    // к классу напрямую, а не через создание экземпляра объекта
    static void multiply(int x, int y)
    {
        writeln(x * y);
    }
}

Base.multiply(2, 5); // используем статический метод. Результат: 10

class Derived : Base {
    string c = "Поле класса - наследника";


    // override означает то, что наследник предоставит свою реализацию метода,
    // переопределив метод базового класса
    override static void multiply(int x, int y)
    {
        super.multiply(x, y);  // super - это ссылка на класс-предок, или базовый класс
        writeln(x * y * 2);
    }
}

auto mc3 = new Derived();
writeln(mc3.a); // 1
writeln(mc3.b); // 2.34
writeln(mc3.c); // Поле класса - наследника

// Финальный класс, наследовать от него нельзя
// кроме того, модификатор final работает не только для классов, но и для методов
// и даже для модулей !
final class FC {
    int a;
}

class Derived : FC { // это вызовет ошибку
    float b;
}

// Абстрактный класс не может быть истанциирован, но может иметь наследников
abstract class AC {
    int a;
}

auto ac = new AC(); // это вызовет ошибку

class Implementation : AC {
    float b;

     // final перед методом нефинального класса означает запрет возможности
     // переопределения метода
    final void test()
    {
       writeln("test passed !");
    }
}

auto impl = new Implementation(); // ОК



/*** Примеси (mixins) ***/

// В D можно вставлять код как строку, если эта строка известна на этапе
// компиляции. Например:
void main() {
   mixin(`writeln("Hello World!");`);
}

// еще пример
string print(string s) {
   return `writeln("` ~ s ~ `");`;
}

void main() {
   mixin (print("str1"));
   mixin (print("str2"));
}



/*** Шаблоны ***/

/*
    Шаблон функции. Эта функция принимает аргументы разных типов, которые
    подставляются вместо T на этапе компиляции. "T" - это не специальный
    символ, а просто буква. Вместо "T" может быть любое слово, кроме ключевого.
 */
void print(T)(T value) {
   writefln("%s", value);
}

void main() {
   print(42);     // В одну и ту же функцию передается: целое
   print(1.2);    // ...число с плавающей точкой,
   print("test"); // ...строка
}

// "Шаблонных" параметров может быть сколько угодно
void print(T1, T2)(T1 value1, T2 value2) {
   writefln(" %s %s", value1, value2);
}

void main() {
   print(42, "Test");
   print(1.2, 33);
}

// Шаблон класса
class Stack(T)
{
    private:
        T[] elements;

    public:
        void push(T element) {
            elements ~= element;
        }

        void pop() {
            --elements.length;
        }

        T top() const @property {
            return elements[$ - 1];
        }

        size_t length() const @property {
            return elements.length;
        }
}

void main() {
    /*
        восклицательный знак - признак шаблона. В данном случае мы создаем
        класс и указываем, что "шаблонное" поле будет иметь тип string
    */
   auto stack = new Stack!string;

   stack.push("Test1");
   stack.push("Test2");

   writeln(stack.top);
   writeln(stack.length);

   stack.pop;
   writeln(stack.top);
   writeln(stack.length);
}



/*** Диапазоны (ranges) ***/

/*
   Диапазоны - это абстракция, которая позволяет легко использовать разные
   алгоритмы с разными структурами данных. Вместо того, чтобы определять свои
   уникальные алгоритмы для каждой структуры, мы можем просто указать для нее
   несколько единообразных функций, определяющих, _как_ мы получаем доступ
   к элементам контейнера, вместо того, чтобы описывать внутреннее устройство
   этого контейнера. Сложно? На самом деле не очень.

    Простейший вид диапазона - Input Range. Для того, чтобы превратить любой
    контейнер в Input Range, достаточно реализовать для него 3 метода:
    - empty - проверяет, пуст ли контейнер
    - front - дает доступ к первому элементу контейнера
    - popFront - удаляет из контейнера первый элемент
*/
struct Student
{
    string name;
    int number;
    string toString() {
        return format("%s(%s)", name, number);
    }
}

struct School
{
    Student[] students;
}

struct StudentRange
{
    Student[] students;

    this(School school) {
        this.students = school.students;
    }

    bool empty() {
        return students.length == 0;
    }

    Student front() {
        return students[0];
    }

    void popFront() {
        students = students[1 .. $];
    }
}

void main(){
    auto school = School([
            Student("Mike", 1),
            Student("John", 2) ,
            Student("Dan", 3)
    ]);
    auto range = StudentRange(school);
    writeln(range);                     // [Mike(1), John(2), Dan(3)]
    writeln(school.students.length);    // 3
    writeln(range.front());             // Mike(1)
    range.popFront();
    writeln(range.empty());             // false
    writeln(range);                     // [John(2), Dan(3)]
}
/*
   Смысл в том, что нам не так уж важно внутреннее устройство контейнера, если
   у нас есть унифицированные методы доступа к его элементам.
   Кроме Input Range в D есть и другие типы диапазонов, которые требуют
   реализации большего числа методов, зато дают больше контроля. Это большая
   тема и мы не будем в подробностях освещать ее здесь.

   Диапазоны - это важная часть D, они используются в нем повсеместно.
*/
```
## Что дальше?

- [Официальный сайт](http://dlang.org/)
- [Онлайн-книга](http://ddili.org/ders/d.en/)
- [Официальная вики](http://wiki.dlang.org/)
---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
    - ["Dzianis Dashkevich", "https://github.com/dskecse"]
    - ["Ryan Plant", "https://github.com/ryanplant-au"]
translator:
    - ["Ev Bogdanov", "https://github.com/evbogdanov"]
filename: learnelixir-ru.ex
lang: ru-ru
---

Elixir — современный функциональный язык программирования, который работает на
виртуальной машине Erlang. Elixir полностью совместим с Erlang, но обладает
дружелюбным синтаксисом и предлагает больше возможностей.

```elixir

# Однострочные комментарии начинаются с символа решётки.

# Для многострочных комментариев отдельного синтаксиса нет,
# поэтому просто используйте несколько однострочных комментариев.

# Запустить интерактивную Elixir-консоль (аналог `irb` в Ruby) можно
# при помощи команды `iex`.
# Чтобы скомпилировать модуль, воспользуйтесь командой `elixirc`.

# Обе команды будут работать из терминала, если вы правильно установили Elixir.

## ---------------------------
## -- Базовые типы
## ---------------------------

# Числа
3    # целое число
0x1F # целое число
3.0  # число с плавающей запятой

# Атомы, которые являются нечисловыми константами. Они начинаются с символа `:`.
:hello # атом

# Кортежи, которые хранятся в памяти последовательно.
{1,2,3} # кортеж

# Получить доступ к элементу кортежа мы можем с помощью функции `elem`:
elem({1, 2, 3}, 0) #=> 1

# Списки, которые реализованы как связные списки.
[1,2,3] # список

# У каждого непустого списка есть голова (первый элемент списка)
# и хвост (все остальные элементы списка):
[head | tail] = [1,2,3]
head #=> 1
tail #=> [2,3]

# В Elixir, как и в Erlang, знак `=` служит для сопоставления с образцом,
# а не для операции присваивания.
#
# Это означает, что выражение слева от знака `=` (образец) сопоставляется с
# выражением справа.
#
# Сопоставление с образцом позволило нам получить голову и хвост списка
# в примере выше.

# Если выражения слева и справа от знака `=` не удаётся сопоставить, будет
# брошена ошибка. Например, если кортежи разных размеров.
{a, b, c} = {1, 2} #=> ** (MatchError)

# Бинарные данные
<<1,2,3>>

# Вы столкнётесь с двумя видами строк:
"hello" # Elixir-строка (заключена в двойные кавычки)
'hello' # Erlang-строка (заключена в одинарные кавычки)

# Все строки представлены в кодировке UTF-8:
"привет" #=> "привет"

# Многострочный текст
"""
Я текст на несколько
строк.
"""
#=> "Я текст на несколько\nстрок.\n"

# Чем Elixir-строки отличаются от Erlang-строк? Elixir-строки являются бинарными
# данными.
<<?a, ?b, ?c>> #=> "abc"
# Erlang-строка — это на самом деле список.
[?a, ?b, ?c]   #=> 'abc'

# Оператор `?` возвращает целое число, соответствующее данному символу.
?a #=> 97

# Для объединения бинарных данных (и Elixir-строк) используйте `<>`
<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"hello " <> "world"  #=> "hello world"

# Для объединения списков (и Erlang-строк) используйте `++`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'hello ' ++ 'world'  #=> 'hello world'

# Диапазоны записываются как `начало..конец` (оба включительно)
1..10 #=> 1..10

# Сопоставление с образцом применимо и для диапазонов:
lower..upper = 1..10
[lower, upper] #=> [1, 10]

# Карты (известны вам по другим языкам как ассоциативные массивы, словари, хэши)
genders = %{"david" => "male", "gillian" => "female"}
genders["david"] #=> "male"

# Для карт, где ключами выступают атомы, доступен специальный синтаксис
genders = %{david: "male", gillian: "female"}
genders.gillian #=> "female"

## ---------------------------
## -- Операторы
## ---------------------------

# Математические операции
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# В Elixir оператор `/` всегда возвращает число с плавающей запятой.

# Для целочисленного деления применяйте `div`
div(10, 2) #=> 5

# Для получения остатка от деления к вашим услугам `rem`
rem(10, 3) #=> 1

# Булевые операторы: `or`, `and`, `not`.
# В качестве первого аргумента эти операторы ожидают булевое значение.
true and true #=> true
false or true #=> true
1 and true    #=> ** (BadBooleanError)

# Elixir также предоставляет `||`, `&&` и `!`, которые принимают аргументы
# любого типа. Всё, кроме `false` и `nil`, считается `true`.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil
!true #=> false

# Операторы сравнения: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<`, `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# Операторы `===` и `!==` более строгие. Разница заметна, когда мы сравниваем
# числа целые и с плавающей запятой:
1 == 1.0  #=> true
1 === 1.0 #=> false

# Elixir позволяет сравнивать значения разных типов:
1 < :hello #=> true

# При сравнении разных типов руководствуйтесь следующим правилом:
# число < атом < ссылка < функция < порт < процесс < кортеж < список < строка

## ---------------------------
## -- Порядок выполнения
## ---------------------------

# Условный оператор `if`
if false do
  "Вы этого никогда не увидите"
else
  "Вы увидите это"
end

# Противоположный ему условный оператор `unless`
unless true do
  "Вы этого никогда не увидите"
else
  "Вы увидите это"
end

# Помните сопоставление с образцом?
# Многие конструкции в Elixir построены вокруг него.

# `case` позволяет сравнить выражение с несколькими образцами:
case {:one, :two} do
  {:four, :five} ->
    "Этот образец не совпадёт"
  {:one, x} ->
    "Этот образец совпадёт и присвоит переменной `x` значение `:two`"
  _ ->
    "Этот образец совпадёт с чем угодно"
end

# Символ `_` называется анонимной переменной. Используйте `_` для значений,
# которые в текущем выражении вас не интересуют. Например, вам интересна лишь
# голова списка, а хвост вы желаете проигнорировать:
[head | _] = [1,2,3]
head #=> 1

# Для лучшей читаемости вы можете написать:
[head | _tail] = [:a, :b, :c]
head #=> :a

# `cond` позволяет проверить сразу несколько условий за раз.
# Используйте `cond` вместо множественных операторов `if`.
cond do
  1 + 1 == 3 ->
    "Вы меня никогда не увидите"
  2 * 5 == 12 ->
    "И меня"
  1 + 2 == 3 ->
    "Вы увидите меня"
end

# Обычно последним условием идёт `true`, которое выполнится, если все предыдущие
# условия оказались ложны.
cond do
  1 + 1 == 3 ->
    "Вы меня никогда не увидите"
  2 * 5 == 12 ->
    "И меня"
  true ->
    "Вы увидите меня (по сути, это `else`)"
end

# Обработка ошибок происходит в блоках `try/catch`.
# Elixir также поддерживает блок `after`, который выполнится в любом случае.
try do
  throw(:hello)
catch
  message -> "Поймана ошибка с сообщением #{message}."
after
  IO.puts("Я выполнюсь всегда")
end
#=> Я выполнюсь всегда
# "Поймана ошибка с сообщением hello."

## ---------------------------
## -- Модули и функции
## ---------------------------

# Анонимные функции (обратите внимание на точку при вызове функции)
square = fn(x) -> x * x end
square.(5) #=> 25

# Анонимные функции принимают клозы и гарды.
#
# Клозы (от англ. clause) — варианты исполнения функции. 
#
# Гарды (от англ. guard) — охранные выражения, уточняющие сопоставление с
# образцом в функциях. Гарды следуют после ключевого слова `when`.
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# В Elixir много встроенных функций.
# Они доступны в текущей области видимости.
is_number(10)    #=> true
is_list("hello") #=> false
elem({1,2,3}, 0) #=> 1

# Вы можете объединить несколько функций в модуль. Внутри модуля используйте `def`,
# чтобы определить свои функции.
defmodule Math do
  def sum(a, b) do
    a + b
  end

  def square(x) do
    x * x
  end
end

Math.sum(1, 2) #=> 3
Math.square(3) #=> 9

# Чтобы скомпилировать модуль Math, сохраните его в файле `math.ex`
# и наберите в терминале: `elixirc math.ex`

defmodule PrivateMath do
  # Публичные функции начинаются с `def` и доступны из других модулей.
  def sum(a, b) do
    do_sum(a, b)
  end

  # Приватные функции начинаются с `defp` и доступны только внутри своего модуля.
  defp do_sum(a, b) do
    a + b
  end
end

PrivateMath.sum(1, 2)    #=> 3
PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)

# Функции внутри модуля тоже принимают клозы и гарды
defmodule Geometry do
  def area({:rectangle, w, h}) do
    w * h
  end

  def area({:circle, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometry.area({:rectangle, 2, 3})        #=> 6
Geometry.area({:circle, 3})              #=> 28.25999999999999801048
Geometry.area({:circle, "not_a_number"}) #=> ** (FunctionClauseError)

# Из-за неизменяемых переменных в Elixir важную роль играет рекурсия
defmodule Recursion do
  def sum_list([head | tail], acc) do
    sum_list(tail, acc + head)
  end

  def sum_list([], acc) do
    acc
  end
end

Recursion.sum_list([1,2,3], 0) #=> 6

# Модули в Elixir поддерживают атрибуты.
# Атрибуты бывают как встроенные, так и ваши собственные.
defmodule MyMod do
  @moduledoc """
  Это встроенный атрибут
  """

  @my_data 100 # А это ваш атрибут
  IO.inspect(@my_data) #=> 100
end

# Одна из фишек языка — оператор `|>`
# Он передаёт выражение слева в качестве первого аргумента функции справа:
Range.new(1,10)
|> Enum.map(fn x -> x * x end)
|> Enum.filter(fn x -> rem(x, 2) == 0 end)
#=> [4, 16, 36, 64, 100]

## ---------------------------
## -- Структуры и исключения
## ---------------------------

# Структуры — это расширения поверх карт, привносящие в Elixir значения по
# умолчанию, проверки на этапе компиляции и полиморфизм.
defmodule Person do
  defstruct name: nil, age: 0, height: 0
end

joe_info = %Person{ name: "Joe", age: 30, height: 180 }
#=> %Person{age: 30, height: 180, name: "Joe"}

# Доступ к полю структуры
joe_info.name #=> "Joe"

# Обновление поля структуры
older_joe_info = %{ joe_info | age: 31 }
#=> %Person{age: 31, height: 180, name: "Joe"}

# Блок `try` с ключевым словом `rescue` используется для обработки исключений
try do
  raise "какая-то ошибка"
rescue
  RuntimeError -> "перехвачена ошибка рантайма"
  _error -> "перехват любой другой ошибки"
end
#=> "перехвачена ошибка рантайма"

# У каждого исключения есть сообщение
try do
  raise "какая-то ошибка"
rescue
  x in [RuntimeError] ->
    x.message
end
#=> "какая-то ошибка"

## ---------------------------
## -- Параллелизм
## ---------------------------

# Параллелизм в Elixir построен на модели акторов. Для написания
# параллельной программы нам понадобятся три вещи:
# 1. Создание процессов
# 2. Отправка сообщений
# 3. Приём сообщений

# Новый процесс создаётся функцией `spawn`, которая принимает функцию
# в качестве аргумента.
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f)            #=> #PID<0.40.0>

# `spawn` возвращает идентификатор процесса (англ. process identifier, PID).
# Вы можете использовать PID для отправки сообщений этому процессу. Сообщения
# отправляются через оператор `send`. А для приёма сообщений используется
# механизм `receive`:

# Блок `receive do` ждёт сообщений и обработает их, как только получит. Блок
# `receive do` обработает лишь одно полученное сообщение. Чтобы обработать
# несколько сообщений, функция, содержащая блок `receive do`, должна рекурсивно
# вызывать себя.

defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Площадь = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Площадь = #{3.14 * r * r}")
        area_loop()
    end
  end
end

# Скомпилируйте модуль и создайте процесс
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>
# Альтернативно
pid = spawn(Geometry, :area_loop, [])

# Отправьте сообщение процессу
send pid, {:rectangle, 2, 3}
#=> Площадь = 6
#   {:rectangle,2,3}

send pid, {:circle, 2}
#=> Площадь = 12.56
#   {:circle,2}

# Кстати, интерактивная консоль — это тоже процесс.
# Чтобы узнать текущий PID, воспользуйтесь встроенной функцией `self`
self() #=> #PID<0.27.0>

## ---------------------------
## -- Агенты
## ---------------------------

# Агент — это процесс, который следит за некоторым изменяющимся значением.

# Создайте агента через `Agent.start_link`, передав ему функцию.
# Начальным состоянием агента будет значение, которое эта функция возвращает.
{ok, my_agent} = Agent.start_link(fn -> ["красный", "зелёный"] end)

# `Agent.get` принимает имя агента и анонимную функцию `fn`, которой будет
# передано текущее состояние агента. В результате вы получите то, что вернёт
# анонимная функция.
Agent.get(my_agent, fn colors -> colors end) #=> ["красный", "зелёный"]

# Похожим образом вы можете обновить состояние агента
Agent.update(my_agent, fn colors -> ["синий" | colors] end)
```

## Ссылки

* [Официальный сайт](http://elixir-lang.org)
* [Шпаргалка по языку](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf)
* [Книга "Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir)
* [Книга "Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/)
* [Книга "Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang)
---
language: erlang
contributors:
    - ["Giovanni Cappellotto", "http://www.focustheweb.com/"]
translators:
    - ["Nikita Kalashnikov", "https://root.yuuzukiyo.net/"]
filename: learnerlang-ru.erl
lang: ru-ru
---

```erlang
% Символ процента предваряет однострочный комментарий.

%% Два символа процента обычно используются для комментариев к функциям.

%%% Три символа процента используются для комментариев к модулям.

% Пунктуационные знаки, используемые в Erlang:
% Запятая (`,`) разделяет аргументы в вызовах функций, структурах данных и
% образцах.
% Точка (`.`) (с пробелом после неё) разделяет функции и выражения в
% оболочке.
% Точка с запятой (`;`) разделяет выражения в следующих контекстах:
% формулы функций, выражения `case`, `if`, `try..catch` и `receive`.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. Переменные и сопоставление с образцом.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Num = 42.  % Все названия переменных начинаются с большой буквы.

% Erlang использует единичное присваивание переменным. Если вы попытаетесь
% присвоить другое значение переменной `Num`, вы получите ошибку.
Num = 43. % ** exception error: no match of right hand side value 43

% В большинстве языков `=` обозначает операцию присвоения. В отличие от них, в
% Erlang `=` — операция сопоставления с образцом. `Lhs = Rhs` на самом
% деле подразумевает «вычисли правую часть выражения (Rhs) и затем сопоставь
% результат с образцом слева (Lhs)».
Num = 7 * 6.

% Числа с плавающей точкой.
Pi = 3.14159.

% Атомы используются для представления различных нечисловых констант. Названия
% атомов начинаются с буквы в нижнем регистре, за которой могут следовать другие
% буквы английского алфавита, цифры, символ подчёркивания (`_`) или «собака»
% (`@`).
Hello = hello.
OtherNode = example@node.

% Если в имени атома нужно использовать другие символы, кроме допустимых,
% имя атома необходимо взять в одинарные кавычки (`'`).
AtomWithSpace = 'some atom with space'.

% Кортежы подобны структурам в языке C.
Point = {point, 10, 45}.

% Если нужно извлечь определённые данные из кортежа, используется оператор
% сопоставления с образцом — `=`.
{point, X, Y} = Point.  % X = 10, Y = 45

% Символ `_` может использоваться как «заполнитель» для переменных, значения
% которых в текущем выражении нас не интересуют. Он называется анонимной
% переменной. В отличие от остальных переменных, множественные использования
% `_` в одном образце не требуют, чтобы все значения, присваевыемые этой
% переменной, были идентичными.
Person = {person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}.
{_, {_, {_, Who}, _}, _} = Person.  % Who = joe

% Список создаётся путём заключения его элементов в квадратные скобки и
% разделения их запятыми. Отдельные элементы списка могут быть любого типа.
% Первый элемент списка называется головой списка. Список, получающийся в
% результате отделения головы, называется хвостом списка.
ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].

% Если `T` — список, то `[H|T]` — тоже список, где `H` является головой, а `T` —
% хвостом. Вертикальная черта (`|`) разделяет голову и хвост списка.
% `[]` — пустой список.
% Мы можем извлекать элементы из списка с помощью сопоставления с образцом.
% Если у нас есть непустой список `L`, тогда выражение `[X|Y] = L`, где `X` и
% `Y` — свободные (не связанные с другими значениям) переменные, извлечёт голову
% списка в `X` и его хвост в `Y`.
[FirstThing|OtherThingsToBuy] = ThingsToBuy.
% FirstThing = {apples, 10}
% OtherThingsToBuy = {pears, 6}, {milk, 3}

% В Erlang нет строк как отдельного типа. Все используемые в программах строки
% являются обычным списком целых чисел. Строковые значения всегда должны быть в
% двойных кавычках (`"`).
Name = "Hello".
[72, 101, 108, 108, 111] = "Hello".


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. Последовательное программирование.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Модуль — основная единица кода в Erlang. В них пишутся и сохраняются все
% функции. Модули хранятся в файлах с расширением `.erl`.
% Модули должны быть скомпилированы перед тем, как использовать код из них.
% Скомпилированный файл модуля имеет разрешение `.beam`.
-module(geometry).
-export([area/1]). % список функций, экспортируемых из модуля.

% Функция `area` состоит из двух формул (clauses). Формулы отделяются друг от
% друга точкой с запятой, после последнего определения должна стоять точка с
% пробелом после неё.
% Каждое определение имеет заголовок и тело. Заголовок состоит из названия
% функции и образца (в скобках); тело состоит из последовательных выражений,
% вычисляемых, когда аргументы функции совпадают с образцом в заголовке.
% Сопоставление с образцами в заголовках происходит в том порядке, в котором
% они перечислены в определении функции.
area({rectangle, Width, Ht}) -> Width * Ht;
area({circle, R})            -> 3.14159 * R * R.

% Компиляция файла с исходным кодом geometry.erl.
c(geometry).  % {ok,geometry}

% Необходимо указывать имя модуля вместе с именем функции для определения, какую
% именно фукнцию мы хотим вызвать.
geometry:area({rectangle, 10, 5}).  % 50
geometry:area({circle, 1.4}).  % 6.15752

% В Erlang две функции с разной арностью (числом аргументов) в пределах одного
% модуля представляются как две разные функции.
-module(lib_misc).
-export([sum/1]). % экспорт функции `sum` с арностью 1, принимающую один аргумент.
sum(L) -> sum(L, 0).
sum([], N)    -> N;
sum([H|T], N) -> sum(T, H+N).

% Fun'ы — анонимные функции, называемые так по причине отсутствия имени. Зато
% их можно присваивать переменным.
Double = fun(X) -> 2*X end. % `Double` указывает на анонимную функцию с идентификатором: #Fun<erl_eval.6.17052888>
Double(2).  % 4

% Функции могут принимать fun'ы как параметры и возвращать их в качестве
% результата вычислений.
Mult = fun(Times) -> ( fun(X) -> X * Times end ) end.
Triple = Mult(3).
Triple(5).  % 15

% Выделения списоков (list comprehensions) — выражения, создающие списки без
% применения анонимных функций, фильтров или map'ов.
% Запись `[F(X) || X <- L]` значит «список `F(X)`, где `X` последовательно
% выбирается из списка `L`».
L = [1,2,3,4,5].
[2*X || X <- L].  % [2,4,6,8,10]
% В выделениях списков могут быть генераторы и фильтры для отделения подмножеств
% генерируемых значений.
EvenNumbers = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4]

% Охранные выражения используются для простых проверок переменных в образцах,
% что значительно расширяет возможности сопоставления. Они могут использоваться
% в заголовках определений функций, предварённые ключевым словом `when`, а также
% в условных конструкциях.
max(X, Y) when X > Y -> X;
max(X, Y) -> Y.

% Охранные выражения можно группировать, разделяя запятой.
% Последовательность `GuardExpr1, GuardExpr2, ..., GuardExprN` является истинной
% только в том случае, когда все выражения, которые она содержат, являются
% истинными.
is_cat(A) when is_atom(A), A =:= cat -> true;
is_cat(A) -> false.
is_dog(A) when is_atom(A), A =:= dog -> true;
is_dog(A) -> false.

% Последовательность охранных выражений, разделённых точками с запятой, является
% истинной в том случае, если хотя бы одно выражение из списка `G1; G2; ...; Gn`
% является истинным.
is_pet(A) when is_dog(A); is_cat(A) -> true;
is_pet(A) -> false.

% Записи предоставляют возможность именования определённых элементов в кортежах.
% Определения записей могут быть включены в исходный код модулей Erlang или же
% в заголовочные файлы с расширением `.hrl`.
-record(todo, {
  status = reminder,  % Значение по умолчанию.
  who = joe,
  text
}).

% Для чтения определений записей из файлов в оболочке можно использовать команду
% `rr`.
rr("records.hrl").  % [todo]

% Создание и изменение записей.
X = #todo{}.
% #todo{status = reminder, who = joe, text = undefined}
X1 = #todo{status = urgent, text = "Fix errata in book"}.
% #todo{status = urgent, who = joe, text = "Fix errata in book"}
X2 = X1#todo{status = done}.
% #todo{status = done,who = joe,text = "Fix errata in book"}

% Условное выражение `case`.
% Функция `filter` возвращет список всех элементов `X` из списка `L`, для
% которых выражение `P(X)` является истинным.
filter(P, [H|T]) ->
  case P(H) of
    true -> [H|filter(P, T)];
    false -> filter(P, T)
  end;
filter(P, []) -> [].
filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]). % [2, 4]

% Условное выражение `if`.
max(X, Y) ->
  if
    X > Y -> X;
    X < Y -> Y;
    true -> nil;
  end.

% Внимание: в выражении `if` должно быть как минимум одно охранное выраженние,
% вычисляющееся в true, иначе возникнет исключение.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3. Обработка исключений.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Исключения возникают в случае внутренних ошибок системы или вызываются
% непосредственно из кода программы с помощью вызовов `throw(Exception)`,
% `exit(Exception)` или `erlang:error(Exception)`.
generate_exception(1) -> a;
generate_exception(2) -> throw(a);
generate_exception(3) -> exit(a);
generate_exception(4) -> {'EXIT', a};
generate_exception(5) -> erlang:error(a).

% В Erlang есть два способа обработки исключений. Первый заключается в
% использовании выражения `try..catch` в функции, в которой возможен выброс
% исключения.
catcher(N) ->
  try generate_exception(N) of
    Val -> {N, normal, Val}
  catch
    throw:X -> {N, caught, thrown, X};
    exit:X -> {N, caught, exited, X};
    error:X -> {N, caught, error, X}
  end.

% Второй способ заключается в использовании `catch`. Во время поимки исключения
% оно преобразуется в кортеж с информацией об ошибке.
catcher(N) -> catch generate_exception(N).

```

## Ссылки:

* ["Learn You Some Erlang for great good!"](http://learnyousomeerlang.com/)
* ["Programming Erlang: Software for a Concurrent World" by Joe Armstrong](http://pragprog.com/book/jaerlang/programming-erlang)
* [Erlang/OTP Reference Documentation](http://www.erlang.org/doc/)
* [Erlang - Programming Rules and Conventions](http://www.erlang.se/doc/programming_rules.shtml)
---
language: forth
contributors:
    - ["Horse M.D.", "http://github.com/HorseMD/"]
translators:
    - ["Dmitrii Kuznetsov", "https://github.com/torgeek"]
filename: learnforth-ru.fs
lang: ru-ru
---

Форт создан Чарлзом Муром в 70-е годы. Это императивный, стековый язык программирования и среда исполнения программ. Использовался в таких проектах как Open Firmware. Продолжает применятся в проектах. Применяется в НАСА.

Внимание: эта материал использует реализацию Форта - Gforth, но большая часть написанного будет работать в других средах.

```
\ Это комментарий
( Это тоже комментарий, но использыется для предоределённых слов )

\ --------------------------------- Прекурсор --------------------------------

\ Всё программирование на Форте заключается в манипулировании 
\ параметрами на стеке.
5 2 3 56 76 23 65    \ ok

\ Эти числа добавляются в стек слева направо
.s    \ <7> 5 2 3 56 76 23 65 ok

\ В Форте всё - это слова-команды или числа. Слова разделяются любым числом
\ пробелов и переходов на новую строку. Длина слова не больше 31 литеры.

\ ---------------------------- Базовая арифметика ----------------------------

\ Арифметика (фактически все ключевые слова требуют данных) - это манипуляция 
\ данными на стеке.
5 4 +    \ ok

\ `.` показывает верхнее значение в стеке:
.    \ 9 ok

\ Ещё примеры арифметических выражений:
6 7 * .        \ 42 ok
1360 23 - .    \ 1337 ok
12 12 / .      \ 1 ok
13 2 mod .     \ 1 ok

99 negate .    \ -99 ok
-99 abs .      \ 99 ok
52 23 max .    \ 52 ok
52 23 min .    \ 23 ok

\ --------------------------- Манипуляции со стеком ---------------------------

\ Естественно, когда мы работаем со стеком, то используем 
\ больше полезных методов:

3 dup -          \ дублировать верхний элемент в стеке 
                 \ (1-й становится эквивалентным 2-му): 3 - 3
2 5 swap /       \ поменять местами верхний элемент со 2-м элементом: 5 / 2
6 4 5 rot .s     \ сменять по очереди 3-и верхних элемента: 4 5 6
4 0 drop 2 /     \ снять верхний элемент (не печатается на экране): 4 / 2
1 2 3 nip .s     \ снять второй элемент (подобно исключению элемента):   1 3

\ ------------------ Более продвинутые манипуляции со стеком ------------------

1 2 3 4 tuck   \ дублировать верхний елемент стека во вторую позицию: 
               \ 1 2 4 3 4 ok
1 2 3 4 over   \ диблировать второй елемент наверх стека: 
               \ 1 2 3 4 3 ok
1 2 3 4 2 roll \ *переместить* элемент в заданной позиции наверх стека:
               \ 1 3 4 2 ok
1 2 3 4 2 pick \ *дублировать* элемент в заданной позиции наверх: 
               \ 1 2 3 4 2 ok

\ Внимание! Обращения к стеку индексируются с нуля.

\ --------------------------- Создание новых слов -----------------------------

\ Определение новых слов через уже известные. Двоеточие `:` переводит Форт 
\ в режим компиляции выражения, которое заканчивается точкой с запятой `;`.
: square ( n -- n ) dup * ;    \ ok
5 square .                     \ 25 ok

\ Мы всегда можем посмотреть, что содержится в слове:
see square     \ : square dup * ; ok

\ -------------------------------- Зависимости --------------------------------

\ -1 == true, 0 == false. Однако, некоторые ненулевые значения 
\ обрабатываются как true:
42 42 =    \ -1 ok
12 53 =    \ 0 ok

\ `if` это компилируемое слово. `if` <stuff to do> `then` <rest of program>.
: ?>64 ( n -- n ) dup 64 > if ." Больше чем 64!" then ;   
\ ok
100 ?>64                                                  
\ Больше чем 64! ok

\ Else:
: ?>64 ( n -- n ) dup 64 > if ." Больше чем 64!" else ." меньше чем 64!" then ;
100 ?>64    \ Больше чем 64! ok
20 ?>64     \ меньше чем 64! ok

\ ------------------------------------ Циклы -----------------------------------

\ `do` это тоже компилируемое слово.
: myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok
myloop
\ Hello!
\ Hello!
\ Hello!
\ Hello!
\ Hello! ok

\ `do` предполагает наличие двух чисел на стеке: конечное и начальное число.

\ Мы можем назначить в цикле переменную `i` для значения индекса:
: one-to-12 ( -- ) 12 0 do i . loop ;     \ ok
one-to-12                                 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 ok

\ `?do` работает подобным образом, за исключением пропуска начального 
\ и конечного значения индекса цикла.
: squares ( n -- ) 0 ?do i square . loop ;   \ ok
10 squares                                   \ 0 1 4 9 16 25 36 49 64 81 ok

\ Изменение "шага" цикла проиводится командой `+loop`:
: threes ( n n -- ) ?do i . 3 +loop ;    \ ok
15 0 threes                             \ 0 3 6 9 12 ok

\ Запуск бесконечного цикла - `begin` <stuff to do> <flag> `until`:
: death ( -- ) begin ." Вы всё ещё здесь?" 0 until ;    \ ok

\ ---------------------------- Переменные и память ----------------------------

\ Используйте `variable`, что бы объявить `age` в качестве переменной.
variable age    \ ok

\ Затем мы запишем число 21 в переменную 'age' (возраст) словом `!`.
21 age !    \ ok

\ В заключении мы можем напечатать значение переменной прочитав его словом `@`, 
\ которое добавит значение на стек или использовать слово `?`, 
\ что бы прочитать и распечатать в одно действие.
age @ .    \ 21 ok
age ?      \ 21 ok

\ Константы объявляются аналогично, за исключем того, что мы не должны 
\ беспокоиться о выделении адреса в памяти:
100 constant WATER-BOILING-POINT    \ ok
WATER-BOILING-POINT .               \ 100 ok

\ ---------------------------------- Массивы ----------------------------------

\ Создание массива похоже на объявление переменной, но нам нужно выделить
\ больше памяти.

\ Вы можете использовать слова `2 cells allot` для создания массива 
\ размером 3 элемента:
variable mynumbers 2 cells allot    \ ok

\ Инициализировать все значения в 0
mynumbers 3 cells erase    \ ok

\ В качестве альтернативы мы можем использовать `fill`:
mynumbers 3 cells 0 fill

\ или мы можем пропустить все слова выше и инициализировать массив 
\ нужными значениями:
create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!)

\ ... что эквивалентно:

\ Ручная запись значений по индексам ячеек:
64 mynumbers 0 cells + !      \ ok
9001 mynumbers 1 cells + !    \ ok
1337 mynumbers 2 cells + !    \ ok

\ Чтение значений по индексу:
0 cells mynumbers + ?    \ 64 ok
1 cells mynumbers + ?    \ 9001 ok

\ Мы можем просто сделать собственное слово для манипуляции массивом:
: of-arr ( n n -- n ) cells + ;    \ ok
mynumbers 2 of-arr ?               \ 1337 ok

\ Которую тоже можно использовать для записи значений:
20 mynumbers 1 of-arr !    \ ok
mynumbers 1 of-arr ?       \ 20 ok

\ ------------------------------ Стек возвратов ------------------------------

\ Стек возвратов используется для удержания ссылки,
\ когда одно слово запускает другое, например, в цикле.

\ Мы всегда видим это, когда используем `i`, которая возвращает дубль верхнего
\ значения стека. `i` это эквивалент `r@`.
: myloop ( -- ) 5 0 do r@ . loop ;    \ ok

\ Так же как при чтении мы можем добавить ссылку в стек возвратов и удалить её:
5 6 4 >r swap r> .s    \ 6 5 4 ok

\ Внимание: так как Форт использует стек возвратов для указателей на слово `>r`
\ следует всегда пользоваться `r>`.

\ ---------------- Операции над числами с плавающей точкой --------------------

\ Многие фортовцы стараются избегать использование слов с вещественными числами.
8.3e 0.8e f+ f.    \ 9.1 ok

\ Обычно мы просто используем слово 'f', когда обращаемся к вещественным числам:
variable myfloatingvar    \ ok
4.4e myfloatingvar f!     \ ok
myfloatingvar f@ f.       \ 4.4 ok

\ ---------- В завершение несколько полезных замечаний и слов -----------------

\ Указание несуществующего слова очистит стек. Тем не менее, есть специальное 
\ слово для этого:
clearstack

\ Очистка экрана:
page

\ Загрузка форт-файла:
\ s" forthfile.fs" included

\ Вы можете вывести список всех слов словаря Форта (это большой список!):
words

\ Выход из Gforth:
bye

```

##Готовы к большему?

* [Начала Форта (англ.)](http://www.forth.com/starting-forth/)
* [Простой Форт (англ.)](http://www.murphywong.net/hello/simple.htm)
* [Мышление Форта (англ.)](http://thinking-forth.sourceforge.net/)
* [Учебники Форта (рус.)](http://wiki.forth.org.ru/УчебникиФорта)
---
language: Go
filename: learngo-ru.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["Christopher Bess", "https://github.com/cbess"]
    - ["Jesse Johnson", "https://github.com/holocronweaver"]
    - ["Quint Guvernator", "https://github.com/qguv"]
translators:
    - ["Artem Medeusheyev", "https://github.com/armed"]
    - ["Valery Cherepanov", "https://github.com/qumeric"]
lang: ru-ru
---

Go - это язык общего назначения, целью которого является удобство, простота,
конкурентность. Это не тренд в компьютерных науках, а новейший и быстрый
способ решать насущные проблемы.

Концепции Go схожи с другими императивными статически типизированными языками.
Быстро компилируется и быстро исполняется, имеет лёгкие в понимании конструкции
для создания масштабируемых и многопоточных программ.

Может похвастаться отличной стандартной библиотекой и большим комьюнити, полным
энтузиастов.

```go
// Однострочный комментарий
/* Многострочный
   комментарий */

// Ключевое слово package присутствует в начале каждого файла.
// Main это специальное имя, обозначающее исполняемый файл, нежели библиотеку.
package main

// Import предназначен для указания зависимостей этого файла.
import (
    "fmt"      // Пакет в стандартной библиотеке Go
    "io/ioutil" // Реализация функций ввод/ввывода.
    "net/http" // Да, это веб-сервер!
    "strconv"  // Конвертирование типов в строки и обратно
    m "math"   // Импортировать math под локальным именем m.
)

// Объявление функции. Main это специальная функция, служащая точкой входа для
// исполняемой программы. Нравится вам или нет, но Go использует фигурные
// скобки.
func main() {
    // Println выводит строку в stdout.
    // Данная функция находится в пакете fmt.
    fmt.Println("Hello world!")

    // Вызов другой функции из текущего пакета.
    beyondHello()
}

// Функции содержат входные параметры в круглых скобках.
// Пустые скобки все равно обязательны, даже если параметров нет.
func beyondHello() {
    var x int // Переменные должны быть объявлены до их использования.
    x = 3     // Присвоение значения переменной.
    // Краткое определение := позволяет объявить переменную с автоматической
    // подстановкой типа из значения.
    y := 4
    sum, prod := learnMultiple(x, y)        // Функция возвращает два значения.
    fmt.Println("sum:", sum, "prod:", prod) // Простой вывод.
    learnTypes()                            // < y minutes, learn more!
}

// Функция, имеющая входные параметры и возвращающая несколько значений.
func learnMultiple(x, y int) (sum, prod int) {
    return x + y, x * y // Возврат двух значений.
}

// Некоторые встроенные типы и литералы.
func learnTypes() {
    // Краткое определение переменной говорит само за себя.
    s := "Learn Go!" // Тип string.

    s2 := `"Чистый" строковой литерал
может содержать переносы строк` // Тоже тип данных string

    // Символ не из ASCII. Исходный код Go в кодировке UTF-8.
    g := 'Σ' // тип rune, это алиас для типа int32, содержит символ юникода.

    f := 3.14195 // float64, 64-х битное число с плавающей точкой (IEEE-754).
    c := 3 + 4i  // complex128, внутри себя содержит два float64.

    // Синтаксис var с инициализациями.
    var u uint = 7 // Беззнаковое, но размер зависит от реализации, как и у int.
    var pi float32 = 22. / 7

    // Синтаксис приведения типа с кратким определением
    n := byte('\n') // byte – это алиас для uint8.

    // Массивы имеют фиксированный размер на момент компиляции.
    var a4 [4]int           // массив из 4-х int, инициализирован нулями.
    a3 := [...]int{3, 1, 5} // массив из 3-х int, ручная инициализация.

    // Слайсы (slices) имеют динамическую длину. И массивы, и слайсы имеют свои
    // преимущества, но слайсы используются гораздо чаще.
    s3 := []int{4, 5, 9}    // Сравните с a3, тут нет троеточия.
    s4 := make([]int, 4)    // Выделение памяти для слайса из 4-х int (нули).
    var d2 [][]float64      // Только объявление, память не выделяется.
    bs := []byte("a slice") // Синтаксис приведения типов.

    p, q := learnMemory() // Объявление p и q как указателей на int.
    fmt.Println(*p, *q)   // * извлекает указатель. Печатает два int-а.

    // Map, также как и словарь или хеш из некоторых других языков, является 
    // ассоциативным массивом с динамически изменяемым размером.
    m := map[string]int{"three": 3, "four": 4}
    m["one"] = 1

    delete(m, "three") // Встроенная функция, удаляет элемент из map-а.

    // Неиспользуемые переменные в Go являются ошибкой.
    // Нижнее подчёркивание позволяет игнорировать такие переменные.
    _, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
    // Вывод считается использованием переменной.
    fmt.Println(s, c, a4, s3, d2, m)

    learnFlowControl() // Идем дальше.
}

// У Go есть полноценный сборщик мусора. В нем есть указатели, но нет арифметики
// указателей. Вы можете допустить ошибку с указателем на nil, но не с
// инкрементацией указателя.
func learnMemory() (p, q *int) {
    // Именованные возвращаемые значения p и q являются указателями на int.
    p = new(int) // Встроенная функция new выделяет память.
    // Выделенный int проинициализирован нулём, p больше не содержит nil.
    s := make([]int, 20) // Выделение единого блока памяти под 20 int-ов.
    s[3] = 7             // Присвоить значение одному из них.
    r := -2              // Определить ещё одну локальную переменную.
    return &s[3], &r     // Амперсанд(&) обозначает получение адреса переменной.
}

func expensiveComputation() float64 {
    return m.Exp(10)
}

func learnFlowControl() {
    // If-ы всегда требуют наличие фигурных скобок, но не круглых.
    if true {
        fmt.Println("told ya")
    }
    // Форматирование кода стандартизировано утилитой "go fmt".
    if false {
        // Будущего нет.
    } else {
        // Жизнь прекрасна.
    }
    // Используйте switch вместо нескольких if-else.
    x := 42.0
    switch x {
    case 0:
    case 1:
    case 42:
        // Case-ы в Go не "проваливаются" (неявный break).
    case 43:
        // Не выполнится.
    }
    // For, как и if не требует круглых скобок
    // Переменные, объявленные в for и if являются локальными.
    for x := 0; x < 3; x++ { // ++ – это операция.
        fmt.Println("итерация", x)
    }
    // Здесь x == 42.

    // For – это единственный цикл в Go, но у него есть альтернативные формы.
    for { // Бесконечный цикл.
        break    // Не такой уж и бесконечный.
        continue // Не выполнится.
    }
    // Как и в for, := в if-е означает объявление и присвоение значения y,
    // проверка y > x происходит после.
    if y := expensiveComputation(); y > x {
        x = y
    }
    // Функции являются замыканиями.
    xBig := func() bool {
        return x > 10000 // Ссылается на x, объявленный выше switch.
    }
    fmt.Println("xBig:", xBig()) // true (т.к. мы присвоили x = e^10).
    x = 1.3e3                    // Тут х == 1300
    fmt.Println("xBig:", xBig()) // Теперь false.

    // Метки, куда же без них, их все любят.
    goto love
love:

    learnDefer()      // Быстрый обзор важного ключевого слова.
    learnInterfaces() // О! Интерфейсы, идём далее.
}

func learnDefer() (ok bool) {
    // Отложенные(deferred) выражения выполняются сразу перед тем, как функция
    // возвратит значение.
    defer fmt.Println("deferred statements execute in reverse (LIFO) order.")
    defer fmt.Println("\nThis line is being printed first because")
    // defer широко используется для закрытия файлов, чтобы закрывающая файл
    // функция находилась близко к открывающей.
    return true
}

// Объявление Stringer как интерфейса с одним методом, String.
type Stringer interface {
    String() string
}

// Объявление pair как структуры с двумя полями x и y типа int.
type pair struct {
    x, y int
}

// Объявление метода для типа pair. Теперь pair реализует интерфейс Stringer.
func (p pair) String() string { // p в данном случае называют receiver-ом.
    // Sprintf – ещё одна функция из пакета fmt.
    // Обращение к полям p через точку.
    return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
    // Синтаксис с фигурными скобками это "литерал структуры". Он возвращает
    // проинициализированную структуру, а оператор := присваивает её p.
    p := pair{3, 4}
    fmt.Println(p.String()) // Вызов метода String у переменной p типа pair.
    var i Stringer          // Объявление i как типа с интерфейсом Stringer.
    i = p                   // Валидно, т.к. pair реализует Stringer.
    // Вызов метода String у i типа Stringer. Вывод такой же, что и выше.
    fmt.Println(i.String())

    // Функции в пакете fmt сами всегда вызывают метод String у объектов для
    // получения строкового представления о них.
    fmt.Println(p) // Вывод такой же, что и выше. Println вызывает метод String.
    fmt.Println(i) // Вывод такой же, что и выше.

    learnVariadicParams("Учиться", "учиться", "и ещё раз учиться!")
}

// Функции могут иметь варьируемое количество параметров.
func learnVariadicParams(myStrings ...interface{}) {
    // Вывести все параметры с помощью итерации.
    for _, param := range myStrings {
        fmt.Println("param:", param)
    }

    // Передать все варьируемые параметры.
    fmt.Println("params:", fmt.Sprintln(myStrings...))

    learnErrorHandling()
}

func learnErrorHandling() {
    // Идиома ", ok" служит для обозначения корректного срабатывания чего-либо.
    m := map[int]string{3: "three", 4: "four"}
    if x, ok := m[1]; !ok { // ok будет false, потому что 1 нет в map-е.
        fmt.Println("тут никого нет")
    } else {
        fmt.Print(x) // x содержал бы значение, если бы 1 был в map-е.
    }
    // Идиома ", err" служит для обозначения была ли ошибка или нет.
    if _, err := strconv.Atoi("non-int"); err != nil { // _ игнорирует значение
        // выведет "strconv.ParseInt: parsing "non-int": invalid syntax"
        fmt.Println(err)
    }
    // Мы ещё обратимся к интерфейсам чуть позже, а пока...
    learnConcurrency()
}

// c – это тип данных channel (канал), объект для конкурентного взаимодействия.
func inc(i int, c chan int) {
    c <- i + 1 // когда channel слева, <- являтся оператором "отправки".
}

// Будем использовать функцию inc для конкурентной инкрементации чисел.
func learnConcurrency() {
    // Тот же make, что и в случае со slice. Он предназначен для выделения
    // памяти и инициализации типов slice, map и channel.
    c := make(chan int)
    // Старт трех конкурентных goroutine. Числа будут инкрементированы
    // конкурентно и, может быть параллельно, если машина правильно
    // сконфигурирована и позволяет это делать. Все они будут отправлены в один
    // и тот же канал.
    go inc(0, c) // go начинает новую горутину.
    go inc(10, c)
    go inc(-805, c)
    // Считывание всех трех результатов из канала и вывод на экран.
    // Нет никакой гарантии в каком порядке они будут выведены.
    fmt.Println(<-c, <-c, <-c) // канал справа, <- обозначает "получение".

    cs := make(chan string)       // другой канал, содержит строки.
    cc := make(chan chan string)  // канал каналов со строками.
    go func() { c <- 84 }()       // пуск новой горутины для отправки значения
    go func() { cs <- "wordy" }() // ещё раз, теперь для cs
    // Select тоже что и switch, но работает с каналами. Он случайно выбирает
    // готовый для взаимодействия канал.
    select {
    case i := <-c: // полученное значение можно присвоить переменной
        fmt.Printf("это %T", i)
    case <-cs: // либо значение можно игнорировать
        fmt.Println("это строка")
    case <-cc: // пустой канал, не готов для коммуникации.
        fmt.Println("это не выполнится.")
    }
    // В этой точке значение будет получено из c или cs. Одна горутина будет
    // завершена, другая останется заблокированной.

    learnWebProgramming() // Да, Go это может.
}

// Всего одна функция из пакета http запускает web-сервер.
func learnWebProgramming() {
    // У ListenAndServe первый параметр это TCP адрес, который нужно слушать.
    // Второй параметр это интерфейс типа http.Handler.
    err := http.ListenAndServe(":8080", pair{})
    fmt.Println(err) // не игнорируйте сообщения об ошибках
}

// Реализация интерфейса http.Handler для pair, только один метод ServeHTTP.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // Обработка запроса и отправка данных методом из http.ResponseWriter
    w.Write([]byte("You learned Go in Y minutes!"))
}

func requestServer() {
    resp, err := http.Get("http://localhost:8080")
    fmt.Println(err)
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Printf("\nWebserver said: `%s`", string(body))
}
```

## Что дальше

Основа всех основ в Go это [официальный веб сайт](http://golang.org/).
Там можно пройти туториал, поиграться с интерактивной средой Go и почитать
объёмную документацию.

Для живого ознакомления рекомендуется почитать исходные коды [стандартной
библиотеки Go](http://golang.org/src/pkg/). Отлично задокументированная, она
является лучшим источником для чтения и понимания Go, его стиля и идиом. Либо
можно, кликнув на имени функции в [документации](http://golang.org/pkg/),
перейти к ее исходным кодам.
---
language: Haskell
filename: haskell-ru.hs
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["Aleksey Pirogov", "http://astynax.github.io"]
lang: ru-ru
---

Haskell разрабатывался, как чистый функциональный язык программирования, применимый на практике. Язык известен благодаря своей системе типов, и "знаменит" благодаря монадам. [Меня][autor] же Haskell заставляет возвращаться к себе снова и снова именно своей элегантностью и [я][autor] получаю истинное удовольствие, программируя на Haskell.

```haskell
-- Однострочные комментарии начинаются с двух дефисов
{- Многострочный комментарий
заключается в пару фигурных скобок с дефисами с внутренней стороны.
-}

-------------------------------------------------------
-- 1. Примитивные типы и простейшие операции над ними
-------------------------------------------------------

-- Числа объявляются просто
3 -- 3

-- Арифметика тоже выглядит вполне ожидаемо
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- Операция деления всегда возвращает действительное число
35 / 4 -- 8.75

-- Делим нацело так
35 `div` 4 -- 8

-- Булевы значения - тоже примитивные значения
True
False

-- Булева алгебра
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- В примере выше `not`, это функция, принимающая один аргумент.
-- При вызове функции в Haskell список аргументов
-- не нужно заключать в скобки - аргументы просто
-- перечисляются через пробелы сразу после имени функции.
-- Т.о. типичный вызов выглядит так:
-- func arg1 arg2 arg3...
-- Ниже же будет показано, как определять свои функции.

-- Строки и символы
"Это строка."
'ы' -- а это символ
'Нельзя заключать длинные строки в одинарные кавычки.' -- ошибка!

-- Строки можно конкатенировать
"Привет" ++ ", Мир!" -- "Привет, Мир!"

-- При этом строки - это просто списки символов!
"Я - строка!" !! 0 -- 'Я'


----------------------------------------------------
-- 2. Списки и Кортежи
----------------------------------------------------

-- Все элементы списка в Haskell
-- должны иметь один и тот же тип.

-- Эти два списка - эквивалентны:
[1, 2, 3, 4, 5]
[1..5]

-- Haskell позволяет определять даже бесконечные списки!
[1..] -- список всех натуральных чисел!

-- Бесконечные списки возможно в Haskell потому, что он "ленив".
-- В Haskell все вычисления производятся тогда и только тогда,
-- когда их результат потребуется.
-- Эта стратегия так и называется - "lazy evaluation".
-- Скажем, если вам нужен тысячный элемент из
-- списка натуральных чисел (бесконечного) и вы напишете так:

[1..] !! 999 -- 1000

-- То Haskell вычислит элементы этого списка от 1 до 1000...
-- ... и остановится, ведь последующие элементы пока не нужны.
-- Это значит, что остальные элементы нашего
-- "бесконечного" списка не будут вычисляться! По крайней мере,
-- пока не понадобятся и они.

-- Списки можно объединять
[1..5] ++ [6..10]

-- И добавлять значения в начало
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- А можно обратиться по индексу
[0..] !! 5 -- 5

-- Вот ещё несколько функций, часто используемых со списками
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- list comprehensions - "формулы" для описания списков
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- можно указать условие попадания элементов в список
[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10]

-- Списки могут даже состоять из других списков
[[1,2,3],[4,5,6]] !! 1 !! 2 -- 6 (вторая строка, третий столбец)

-- Кортежи позволяют своим элементам иметь различные типы,
-- но при этом кортежи имеют фиксированную длину.
-- Кортеж:
("haskell", 1)

-- Часто кортежи из двух элементов называются "парами".
-- Элементы пары можно получать так:
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. Функции
----------------------------------------------------
-- Простая функция, принимающая два аргумента
add a b = a + b

-- Внимание!
-- Если вы используете ghci (интерактивный интерпретатор Haskell),
-- вам нужно использовать ключевое слово `let`, примерно так:
-- let add a b = a + b

-- Вызовем нашу функцию
add 1 2 -- 3

-- Функцию можно поместить между первым и вторым аргументами,
-- если заключить её имя в обратные кавычки
1 `add` 2 -- 3

{- Вы можете также определять функции, имя которых
вообще не содержит букв! Таки функции и называются "операторами",
и, да, вы можете определять свои операторы!
Скажем, оператор целочисленного деления можно определить так -}
(//) a b = a `div` b
35 // 4 -- 8
{- Здесь оператор заключен в скобки - как говорят,
поставлен в префиксную позицию.
В префиксной позиции оператор можно не только определять,
но и вызывать -}
(+) 1 2 -- 3

-- Охранные выражения (guards) порой удобны,
-- если наша функция ветвится
fib x
  | x < 2 = x
  | otherwise = fib (x - 1) + fib (x - 2)

{- Сопоставление с образцом (pattern matching)
чем-то напоминает охранные выражения.
Здесь мы видим три определения функции fib.
При вызове функции по имени Haskell использует
первое определение, к образцу которого
"подойдет" набор аргументов -}
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- Pattern matching для кортежей выглядит так
foo (x, y) = (x + 1, y + 2)

{- Pattern matching для списков устроен чуть сложнее.
Пусть `x` - первый элемент списка, а `xs` - остальные элементы.
Тогда операции `head` и `tail` могут быть определены так -}
myHead (x:xs) = x
myTail (x:xs) = xs

-- Функцию отображения мы можем написать так
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- При сопоставлении происходит привязка
-- элементов значения с именами в образце
fstPlusThird (a : _ : b : _) = a + b
fstPlusThird [1,2,3,4,5] -- 4
-- Значения, для которых вместо имени указано `_`,
-- игнорируются. Это удобно, когда важен сам факт
-- совпадения образца
oneElem [_] = True
oneElem _ = False

startsWith x (y:_) = x == y
startsWith _ _ = False

startsWith 'H' "Hello!" -- True
startsWith 'H' "hello!" -- False

{- Обратите внимание на тот факт,
что первый аргумент нашей функции `myMap` - тоже функция!
Функции, подобно `myMap`, принимающие другие функции
в качестве параметров, или, скажем, возвращающие функции
в качестве результата, называются
Функциями Высших Порядков (ФВП, High Order Functions, HOF)
-}

-- Вместе с ФВП часто используются анонимные функции
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
-- Такие функции описываются в виде
-- \arg1 arg1 .. -> expression

-- Популярные в других языках ФВП присутствуют и в Haskell
map (\x -> x * 10) [1..5] -- [10, 20, 30, 40, 50]
filter (\x -> x > 2) [1..5] -- [3, 4, 5]

{- Функция свертки
(она же `reduce` или `inject` в других языках)
в Haskell представлены функциями `foldr` и `foldl`.
Суть свертки можно представить так:

foldl f x0 [x1,x2,x3] -> (f (f (f x0 x1) x2) x3)
foldr f x0 [x1,x2,x3] -> (f x1 (f x2 (f x3 x0)))

Здесь x0 - начальное значения так называемого "аккумулятора"
-}
-- Эти два вызова дают одинаковый результат
foldr (\x acc -> acc + x) 0 [1..5] -- 15
foldl (\acc x -> acc + x) 0 [1..5] -- 15
-- Тут можно даже заменить анонимную функцию на оператор
foldr (+) 0 [1..5] -- 15
foldl (+) 0 [1..5] -- 15

-- Зато здесь разница видна
foldr (\x acc -> (x + 10) : acc) [] [1..3] -- [11, 12, 13]
foldl (\acc x -> (x + 10) : acc) [] [1..3] -- [13, 12, 11]

{- Часто в качестве начального значения
удобно брать крайнее значение списка (крайнее слева или справа).
Для этого есть пара функций - `foldr1` и `foldl1`  -}
foldr1 (+) [1..5] -- 15
foldl1 (+) [1..5] -- 15

----------------------------------------------------
-- 4. Больше о функциях
----------------------------------------------------

{- Каррирование (currying)
Если в Haskell при вызове функции передать не все аргументы,
Функция становится "каррированой" - результатом вызова станет
новая функция, которая при вызове и примет оставшиеся аргументы -}

add a b = a + b
foo = add 10 -- теперь foo будет принимать число
             -- и добавлять к нему 10
foo 5 -- 15

-- Для операторов можно "опустить" любой из двух аргументов
-- Используя этот факт можно определить
-- функцию `foo` из кода выше несколько иначе
foo = (+10)
foo 5 -- 15

-- Поупражняемся
map (10-) [1..3] -- [9, 8, 7]
filter (<5) [1..10] -- [1, 2, 3, 4]

{- Композиция функций
Функция (.) соединяет пару функций в цепочку.
К примеру, можно соединить функцию, добавляющую 10,
с функцией, умножающей на 5 -}
foo = (*5) . (+10)

-- (5 + 10) * 5 = 75
foo 5 -- 75

{- Управление приоритетом вычисления
В Haskell есть функция `$`, которая применяет
свой первый аргумент ко второму с наименьшим приоритетом
(обычное применение функций имеет наивысший приоритет)
Эта функция часто позволяет избежать использования
"лишних" скобок -}
head (tail (tail "abcd")) -- 'c'
head $ tail $ tail "abcd" -- 'c'
-- того же эффекта иногда можно достичь использованием композиции
(head . tail . tail) "abcd" -- 'c'
head . tail . tail $ "abcd" -- 'c'
{- Тут стоит сразу запомнить, что композиция функций
возвращает именно новую функцию, как в последнем примере.
Т.е. можно делать так -}
third = head . tail . tail
-- но не так
third = head $ tail $ tail -- (head (tail (tail))) - ошибка!

----------------------------------------------------
-- 5. Сигнатуры типов
----------------------------------------------------

{- Haskell обладает очень сильной системой типов.
И типизация в Haskell - строгая. Каждое выражение имеет тип,
который может быть описан сигнатурой.
Сигнатура записывается в форме
expression :: type signature
-}

-- Типы примитивов
5 :: Integer
"hello" :: String
True :: Bool

{- Функции тоже имеют тип
`not` принимает булево значение и возвращает булев результат
not :: Bool -> Bool

Вот функция двух аргументов
add :: Integer -> Integer -> Integer

Тут то мы и видим предпосылки к каррированию: тип
на самом деле выглядит так (скобки просто обычно опускаются)
add :: (Integer -> Integer) -> Integer
т.е. функция принимает аргумент,
и возвращает функцию от второго аргумента! -}

-- Считается хорошим тоном указывать сигнатуру определений,
-- которые доступны другим разработчикам (публичны). Пример:
double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. Управление потоком исполнения
----------------------------------------------------

-- Выражение `if`
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- Выражение `if` можно записать и в несколько строк.
-- Соблюдайте отступы!
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- Так как `if` - выражение, ветка `else` обязательна!
-- И более того, результаты выражений в ветках `then` и `else`
-- должны иметь одинаковый тип!

-- `case`-выражение выглядит так
case args of -- парсим аргументы командной строки
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"

-- При вычислении результата `case`-выражения производится
-- сопоставление с образцом:
fib x = case x of
          1 -> 1
          2 -> 1
          _ -> fib (x - 1) + fib (x - 2)

-- В Haskell нет циклов - вместо них используются рекурсия,
-- отображение, фильтрация и свертка (map/filter/fold)
map (*2) [1..5] -- [2, 4, 6, 8, 10]

for array func = map func array
for [0..3] $ \i -> show i -- ["0", "1", "2", "3"]
for [0..3] show           -- ["0", "1", "2", "3"]

----------------------------------------------------
-- 7. Пользовательские типы данных
----------------------------------------------------

-- Создадим свой Haskell-тип данных

data Color = Red | Blue | Green

-- Попробуем использовать

say :: Color -> String
say Red   = "You are Red!"
say Blue  = "You are Blue!"
say Green = "You are Green!"

-- Типы могут иметь параметры (параметры типов)

data Maybe a = Nothing | Just a

-- Все эти выражения имеют тип `Maybe`
Just "hello"    -- :: `Maybe String`
Just 1          -- :: `Maybe Int`
Nothing         -- :: `Maybe a` для любого `a`

-- Типы могут быть достаточно сложными
data Figure = Rectangle (Int, Int) Int Int
            | Square (Int, Int) Int
            | Point (Int, Int)

area :: Figure -> Int
area (Point     _)     = 0
area (Square    _ s)   = s * s
area (Rectangle _ w h) = w * h

----------------------------------------------------
-- 8. Ввод-вывод в Haskell
----------------------------------------------------

-- Полноценно объяснить тему ввода-вывода невозможно
-- без объяснения монад, но для использования в простых случаях
-- вводного описания будет достаточно.

-- Когда программа на Haskell выполняется,
-- вызывается функция с именем `main`.
-- Эта функция должна вернуть значение типа `IO ()`
-- Например

main :: IO ()
main = putStrLn $ "Hello, sky! " ++ (say Blue)
-- `putStrLn` имеет тип `String -> IO ()`

-- Проще всего реализовать программу с вводом-выводом (IO),
-- если вы реализуете функцию с типом `String -> String`.
-- Далее ФВП
--    interact :: (String -> String) -> IO ()
-- сделает всё за нас!

countLines :: String -> String
countLines = show . length . lines
-- здесь `lines` разделяет строку на список строк
-- по символу перевода строки

main' :: IO ()
main' = interact countLines

{- Вы можете думать о типе `IO ()`,
как о некотором представлении последовательности
действий, которые должен совершить компьютер.
Такое представление напоминает программу
на императивном языке программирования. Для описания
такой последовательности используется `do`-нотация -}

sayHello :: IO ()
sayHello = do
   putStrLn "What is your name?"
   name <- getLine -- запрашиваем строку и связываем с "name"
   putStrLn $ "Hello, " ++ name

-- Упражнение:
--     напишите свою реализацию функции `interact`,
--     которая запрашивает и обрабатывает только одну строку

{- Код функции `sayHello` не будет исполняться
при её определении. Единственное место, где IO-действия
могут быть произведены - функция `main`!
Чтобы эта программа выполнила действия в функции `sayHello`,
закомментируйте предыдущее определение функции `main`
и добавьте новое определение:

main = sayHello -}

{- Давайте подробнее рассмотрим, как работает функция `getLine`
Её тип:
   getLine :: IO String
Вы можете думать, что значение типа `IO a` представляет
собой компьютерную программу, в результате выполнения которой
генерируется значение типа `a`, в дополнение
к остальным эффектам, производимым при выполнении - таким как
печать текста на экран. Это значение типа `a` мы можем
сохранить с помощью оператора `<-`. Мы даже можем реализовать
свое действие, возвращающее значение: -}

action :: IO String
action = do
   putStrLn "This is a line. Duh"
   input1 <- getLine
   input2 <- getLine
   -- Тип блока `do` будет соответствовать типу последнего
   -- выполненного в блоке выражения.
   -- Заметим, что `return` - не ключевое слово, а функция
   -- типа `a -> IO a`
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- Теперь это действие можно использовать вместо `getLine`:

main'' = do
    putStrLn "I will echo two lines!"
    result <- action
    putStrLn result
    putStrLn "This was all, folks!"

{- Тип `IO` - пример "монады". Языку Haskell нужны монады,
чтобы оставаться преимущественно чистым функциональным языком.
Любые функции, взаимодействующие с внешним миром
(производящие ввод-вывод) имеют `IO` в своих сигнатурах.
Это позволяет судить о функции как о "чистой" - такая не будет
производить ввод-вывод. В ином случая функция - не "чистая".

Такой подход позволяет очень просто разрабатывать многопоточные
программы - чистые функции, запущенные параллельно
не будут конфликтовать между собой в борьбе за ресурсы. -}

----------------------------------------------------
-- 9. Haskell REPL
----------------------------------------------------

{- Интерактивная консоль Haskell запускается командой `ghci`.
Теперь можно вводить строки кода на Haskell.
Связывание значений с именами производится
с помощью выражения `let`: -}

let foo = 5

-- Тип значения или выражения можно узнать
-- с помощью команды `:t`:

>:t foo
foo :: Integer

-- Также можно выполнять действия с типом `IO ()`

> sayHello
What is your name?
Friend!
Hello, Friend!

```

Многое о Haskell, например классы типов и монады невозможно уместить в столь короткую статью. Огромное количество очень интересных идей лежит в основе языка, и именно благодаря этому фундаменту на языке так приятно писать код. Позволю себе привести ещё один маленький пример кода на Haskell - реализацию быстрой сортировки:

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

Haskell прост в установке, забирайте [здесь](http://www.haskell.org/platform/) и пробуйте! Это же так интересно!.

Более глубокое погрузиться в язык позволят прекрасные книги
[Learn you a Haskell](http://learnyouahaskell.com/) и
[Real World Haskell](http://book.realworldhaskell.org/).

[autor]: http://adit.io имеется в виду автор оригинального текста Adit Bhargava *(примечание переводчика)*
---
language: html
filename: learnhtml-ru.html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
translators:
    - ["Lana Tim", "https://github.com/LanaTim"]
lang: ru-ru
---

HTML расшифровывается как Hypertext Markup Language(гипертекстовый язык разметки).
Это язык используют для написания страниц для World Wide Web(всемирной паутины).
Это язык разметки позволяет писать веб-страниц с помощью кода, чтобы определять, 
как должны быть отображены текст и данные.
На самом деле, HTML файлы представляют собой простые текстовые файлы.
Что такое разметка? Это способ организации данных страницы,
путем открытия и закрытия тегов(помещая данные внутрь этих тегов).
Эта разметка служит, чтобы придать значение тексту, который он окружает.
Как и в других языках программирования, HTML имеет много версий. Здесь мы будем говорить о HTML5.


**Примечание:** Вы можете тестировать различные теги и элементы по мере продвижения 
через учебник на сайте, как [codepen](http://codepen.io/pen/) для того, чтобы увидеть 
их влияние, понять, как они работают и ознакомиться с языком.
В данной статье рассматривается в основном HTML синтаксис и некоторые полезные советы.

```html
<!-- Комментарии заключаются как эта лини\! -->

<!-- #################### Теги #################### -->
   
<!-- Ниже приведен пример HTML-файл, который мы будем анализировать. -->

<!doctype html>
	<html>
		<head>
			<title>Мой сайт</title>
		</head>
		<body>
			<h1>Привет, мир!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">
				Переходите сюда, чтоб посмотреть как это выглядит.
			</a>
			<p>Это параграф.</p>
			<p>Это другой параграф.</p>
			<ul>
				<li>Это элемент не нумерованного списка (маркированный список)</li>
				<li>Это другой элемент</li>
				<li>Это последний элемент в списке</li>
			</ul>
		</body>
	</html>

<!-- HTML-файл всегда начинается с указанием браузеру, что страница это HTML. -->
<!doctype html>

<!-- После этого, страница начинается с <html> тега. -->
<html>

<!-- страница будет закрыта в конце с помощью тега </html>. -->
</html>

<!-- Ничто не должно появиться после этого заключительного тега. -->

<!-- Внутри (между открывающим и закрывающим тегами <html> </ html>), мы находим: -->

<!-- Заголовок определяется <head> (it он должен быть закрыт </head>). -->
<!-- Заголовок содержит описание и дополнительную информацию, которая не отображается; это метаданные. -->

<head>
	<title>Мой сайт</title><!-- Тег <title> указывает браузеру заголовок, чтобы показать в строке заголовка названия и вкладки браузера окна. -->
</head>

<!-- После секция <head>, находится секция - <body> -->
<!-- До этого момента, ничего описаное не будет отображаться в окне браузера. -->
<!-- Мы должны наполнить <body>  содержанием, которое будет отображаться. -->

<body>
	<h1>Hello, world!</h1> <!-- Тег h1 создает заголовок. -->
	<!-- Так же существуют другие заголовки от имеющего большее значение <h1> по убыванию к  <h6>. -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">Переходите сюда, чтоб посмотреть как это выглядит.</a>
	<!--гиперссылка на URL, заданнf атрибутом href="" -->
	<p>Это параграф.</p> <!-- Тег <p> позволяет нам добавдять текст на странице HTML. -->
	<p>Это другой параграф.</p>
	<ul> <!-- Тег <ul> создает маркированный список. -->
	<!-- Для того, чтобы иметь пронумерованный список лучше использовать <ol> 
		тогда первый элемент будет иметь значение 1. для второго элемента, 2. и так далее. -->
		<li>Это элемент в не нумерованном списке (маркированный список)</li>
		<li>Это еще один элемент</li>
		<li>И это последний пункт в списке</li>
	</ul>
</body>

<!-- Вот так просто можно создать HTML страницу. -->

<!--Но можно добавить множество дополнительных типов HTML тегов. -->

<!-- Для вставки картинки -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- Источник изображения указывается с помощью атрибута src="" -->
<!-- Источником может быть URL или даже путь к файлу на вашем компьютере. -->

<!-- Кроме того, можно создать таблицу. -->

<table> <!-- Мы открыли <table> элемент. -->
	<tr> <!-- <tr> позволяет создать ряд. -->
		<th>Первый заголовок</th> <!-- <th> позволяет дать название для столбца таблицы. -->
		<th>Второй заголовок</th>
	</tr>
	<tr>
		<td>Первый ряд, первая колонка</td> <!-- <td> позволяет нам создать ячейку таблицы. -->
		<td>Первый ряд, вторая колонка</td>
	</tr>
	<tr>
		<td>Второй ряв, первая колонка</td>
		<td>Второй ряд, вторая колонка</td>
	</tr>
</table>

```

## Применение

HTML файлы имеют окончание(расширение) `.html`.

## Узнать больше

* [википедиа](https://ru.wikipedia.org/wiki/HTML)
* [HTML учебник](https://developer.mozilla.org/ru/docs/Web/HTML)
* [htmlbook](http://htmlbook.ru/)
---
language: java
filename: LearnJava-ru.java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Madison Dickson", "http://github.com/mix3d"]
translators:
    - ["Sergey Gaykov", "https://github.com/gaykov"]
lang: ru-ru

---

Java - это объектно-ориентированный язык программирования общего назначения,
основанный на классах и поддерживающий параллельное программирование.
[Подробнее читайте здесь.](http://docs.oracle.com/javase/tutorial/java/index.html)

```java
// Однострочные комментарии начинаются с //.
/*
Многострочные комментарии
выглядят так.
*/
/**
JavaDoc-комментарии выглядят так. Они используются для описания класса
и его членов.
*/

// Импорт класса ArrayList из пакета java.util.
import java.util.ArrayList;
// Импорт всех классов из пакета java.security.
import java.security.*;

// Каждый .java файл содержит один публичный класс, имя которого совпадает с
// именем файла. 
public class LearnJavaRu {

    // Программа должна содержать метод main, который является точкой входа.
    public static void main (String[] args) {

        // System.out.println используется для печати строк.
        System.out.println("Hello World!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // Чтобы печатать что-либо, не заканчивая переводом строки,
        // используйте System.out.print.
        System.out.print("Hello ");
        System.out.print("World");

        // Используйте System.out.printf() для печати с форматированием
        System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159

        ///////////////////////////////////////
        // Переменные
        ///////////////////////////////////////

        /*
        *  Объявление переменных
        */
        // Переменные объявляются с использованием <тип> <имя>
        int fooInt;
        // Одновременное объявление нескольких переменных одного типа
        // <type> <name1>, <name2>, <name3>
        int fooInt1, fooInt2, fooInt3;

        /*
        *  Инициализация переменных
        */

        // объявление и инициализация переменной <type> <name> = <val>
        int fooInt = 1;
        int fooInt1, fooInt2, fooInt3;
        // инициализация нескольких переменных одного типа
        // <type> <name1>, <name2>, <name3> = <val>
        fooInt1 = fooInt2 = fooInt3 = 1;

        /*
        *  Типы переменных
        */
        // Byte - 8-битное целое число.
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short - 16-битное целое число.
        // (-32,768 <= short <= 32,767)
        short fooShort = 10000;

        // Integer - 32-битное целое число.
        // (-2,147,483,648 <= int <= 2,147,483,647)
        int fooInt = 1;

        // Long - 64-битное целое число.
        // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L используется для указания на то, что переменная имеет тип long;
        // По умолчанию, числа без L являются integer.

        // Замечание: в Java нет беззнаковых типов.

        // Float - 32-битное IEEE 754 число с плавающей запятой с одинарной степенью точности.
        float fooFloat = 234.5f;
        // f используется для указания на то, что переменная имеет тип float;
        // иначе, число являлось бы double.

        // Double - 64-битное IEEE 754 число с плавающей запятой с двойной степенью точности.
        double fooDouble = 123.4;

        // Boolean - true или false
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char - Простой 16-битный символ Unicode.
        char fooChar = 'A';

        // Переменным final не может быть присвоен другой объект.
        final int HOURS_I_WORK_PER_WEEK = 9001;

        // Строки.
        String fooString = "My String Is Here!";

        // \n - это экранированный символ, который означает начало новой строки.
        String barString = "Printing on a new line?\nNo Problem!";
        // \t - это экранированный символ, который добавляет символ табуляции.
        String bazString = "Do you want to add a tab?\tNo Problem!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // Массивы
        // Размер массива должен быть указан при объявлении.
        // Объявлять массив можно в следующих форматах:
        //<тип данных> [] <имя> = new <тип данных>[<размер массива>];
        //<тип данных> <имя>[] = new <тип данных>[<размер массива>];
        int [] intArray = new int[10];
        String [] stringArray = new String[1];
        boolean boolArray [] = new boolean[100];

        // Другой способ объявления и инициализации массива:
        int [] y = {9000, 1000, 1337};
        String names [] = {"Bob", "John", "Fred", "Juan Pedro"};
        boolean bools[] = new boolean[] {true, false, false};

        // Индексация массива - доступ к элементу.
        System.out.println("intArray @ 0: " + intArray[0]);

        // Массивы изменяемы и индекс в них начинается с 0.
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // Дополнительно.
        // ArrayLists - похож на массив, но предлагает больше возможностей,
        //             его размер изменяемый.
        // LinkedLists - реализация двусвязного списка. Все операции
        //             выполняются так, как ожидается от двусвязного
        //             списка.
        // Maps        - набор объектов, в которых присутствует связь
        //             ключ-значение. В Map ключ не может дублироваться.
        //             Каждый ключ связан только с одним значением.
        // HashMaps    - этот класс использует хэш-таблицу для реализации
        //             интерфейса Map. Это позволяет сохранить постоянной
        //             скорость выполнения базовых операций, таких как
        //             добавление и удаление элементов, вне зависимости
        //             от размера множества. 

        ///////////////////////////////////////
        // Операторы
        ///////////////////////////////////////
        System.out.println("\n->Операторы");

        int i1 = 1, i2 = 2; // Сокращение для множественного объявления.

        // Арифметика в Java проста.
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (0.5 округлено)

        // Остаток от деления
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Операторы сравнения.
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Побитовые операторы!
        /*
        ~       Унарное побитовое дополнение.
        <<      Знаковый сдвиг влево.
        >>      Знаковый сдвиг вправо.
        >>>     Беззнаковый сдвиг вправо.
        &       Побитовое И.
        ^       Побитовое исключающее ИЛИ.
        |       Побитовое ИЛИ.
        */

        // Операторы инкремента.
        int i = 0;
        System.out.println("\n->Inc/Dec-rementation");
        // Операторы ++ и -- увеличивают и уменьшают значение на 1 соответственно.
        // Если они находятся перед переменной, сначала происходит
        // увеличение/уменьшение, затем операция, если после,
        // то сначала выполняется операция, затем увеличение/уменьшение.
        System.out.println(i++); //i = 1, напечатает 0 (пост-инкремент)
        System.out.println(++i); //i = 2, напечатает 2 (пре-инкремент)
        System.out.println(i--); //i = 1, напечатает 2 (пост-декремент)
        System.out.println(--i); //i = 0, напечатает 0 (пре-декремент)

        ///////////////////////////////////////
        // Контролирующие операторы.
        ///////////////////////////////////////
        System.out.println("\n->Контролирующие операторы");

        // Оператор if такой же, как и в С.
        int j = 10;
        if (j == 10){
            System.out.println("Я напечатаюсь!");
        } else if (j > 10) {
            System.out.println("Я нет.");
        } else {
            System.out.println("И я тоже нет.");
        }

        // Цикл while.
        int fooWhile = 0;
        while(fooWhile < 100)
        {
            // System.out.println(fooWhile);
            // Увеличить счетчик.
            // Будет пройдено 100 итераций, fooWhile 0,1,2...99
            fooWhile++;
        }
        System.out.println("Значение fooWhile: " + fooWhile);

        // Цикл Do While.
        int fooDoWhile = 0;
        do
        {
            // System.out.println(fooDoWhile);
            // Увеличить счетчик.
            // Будет пройдено 100 итераций, fooDoWhile 0->99
            fooDoWhile++;
        } while(fooDoWhile < 100);
        System.out.println("Значение fooDoWhile: " + fooDoWhile);

        // Цикл for.
        int fooFor;
        // Структура цикла for => for(<начальное_состояние>; <условие>; <шаг>)
        for(fooFor=0; fooFor<10; fooFor++){
            // System.out.println(fooFor);
            // Пройдет 10 итераций., fooFor 0->9
        }
        System.out.println("Значение fooFor: " + fooFor);

        // Цикл For Each
        // Автоматический проход через массив или список объектов.
        int[] fooList = {1,2,3,4,5,6,7,8,9};
        // Структура цикла for each => for(<объект> : <объект_массив>)
        // читается как: для каждого объекта в массиве
        // заметка: тип объекта должен совпадать с типом массива.

        for( int bar : fooList ){
            System.out.println(bar);
            //Пройдет 9 итераций и напечатает 1-9 на новых строках.
        }

        // Switch Case
        // switch работает с типами byte, short, char и int.
        // Также он работает с перечислениями,
        // классом String (с Java 7) и с некоторыми классами-обертками над
        // примитивными типами: Character, Byte, Short и Integer.
        int month = 3;
        String monthString;
        switch (month){
            case 1:
                    monthString = "Январь";
                    break;
            case 2:
                    monthString = "Февраль";
                    break;
            case 3:
                    monthString = "Март";
                    break;
            default:
                    monthString = "Другой месяц";
                    break;
        }
        System.out.println("Результат Switch Case: " + monthString);

        // Сокращенный синтаксис условного оператора.
        // Вы можете использовать этот синтаксис для быстрого присвоения
        // или логических переходов.
        // Читается так: "Если (условие) истинно, использовать <значение 1>,
        // в ином случае, использовать <значение 2>"
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println(bar); // Напечатает А, потому что условие истинно


        ///////////////////////////////////////
        // Преобразование и приведение типов данных.
        ///////////////////////////////////////

        // Преобразование данных.

        // Преобразование строки в число.
        Integer.parseInt("123"); // Вернет числовое представление "123".

        // Преобразование числа в строку
        Integer.toString(123); // Вернет строковое представление 123.

        // Для других преобразований смотрите следующие классы:
        // Double
        // Long
        // String

        // Приведение типов
        // Вы так же можете приводить типы в Java.
        // Подробнее об этом можно узнать по ссылке:
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Классы и Функции
        ///////////////////////////////////////

        System.out.println("\n->Классы и Функции");

        // (Класс Bicycle определен ниже)

        // Для создания экземпляра класса используется new.
        Bicycle trek = new Bicycle();

        // Вызов методов объекта.
        trek.speedUp(3); // Вы должны всегда использовать сеттеры и геттеры.
        trek.setCadence(100);

        // toString возвращает строковое представление объекта.
        System.out.println("trek info: " + trek.toString());

    } // Конец метода main.
} // Конец класса LearnJavaRu.


// Вы можете включать другие, не публичные классы в .java файл.


// Синтаксис объявления класса:
// <public/private/protected> class <имя класса>{
//    // Поля с данными, конструкторы, функции, все внутри.
//    // Функции называют методами в Java.
// }

class Bicycle {

    // Поля/Переменные класса Bicycle.
    public int cadence;// Публичные(public): Доступны из любого места.
    private int speed; // Приватные(private): Доступны только внутри класса.
    protected int gear;// Защищенные(protected): Доступ из класса и наследников.
    String name; // по умолчанию: Доступны только внутри пакета.

    // Конструкторы - способ создания класса.
    // Это конструктор:
    public Bicycle() {
        gear = 1;
        cadence = 50;
        speed = 5;
        name = "Bontrager";
    }

    // Это конструктор, который принимает аргументы:
    public Bicycle(int startCadence, int startSpeed, int startGear, String name) {
        this.gear = startGear;
        this.cadence = startCadence;
        this.speed = startSpeed;
        this.name = name;
    }

    // Синтаксис функций:
    // <public/private/protected> <тип возвращаемого значения> <имя>(<аргументы>)

    // Классы в Java часто реализуют сеттеры и геттеры для своих полей.

    // Синтаксис определения метода:
    // <модификатор доступа> <тип возвращаемого значения> <имя метода>(<аргументы>)
    public int getCadence() {
        return cadence;
    }

    // void-методы не возвращают значений.
    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void speedUp(int increment) {
        speed += increment;
    }

    public void slowDown(int decrement) {
        speed -= decrement;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    //Метод для отображения значений атрибутов объекта.
    @Override
    public String toString() {
        return "gear: " + gear +
                " cadence: " + cadence +
                " speed: " + speed +
                " name: " + name;
    }
} // конец класса Bicycle.

// PennyFarthing - это класс, наследованный от Bicycle
class PennyFarthing extends Bicycle {
    // (Penny Farthings - это такие велосипеды с большим передним колесом,
    // у них нет передач.)

    public PennyFarthing(int startCadence, int startSpeed){
        // Вызов конструктора родительского класса.
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // Вы должны пометить метод, который переопределяете, при помощи @аннотации
    // Чтобы узнать о том, что такое аннотации и зачем они нужны, почитайте:
    // http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGear(int gear) {
        gear = 0;
    }

}

// Интерфейсы
// Синтаксис определения интерфейса: 
// <модификатор доступа> interface <имя интерфейса> extends <базовый интерфейс> {
//     // Константы
//     // Определение методов
// }

// Пример - Еда:
public interface Edible {
    // Любой класс, реализующий этот интерфейс, должен реализовать этот метод.
    public void eat();
}

public interface Digestible {
    public void digest();
}


// Сейчас мы можем создать класс, реализующий оба эти интерфейса.
public class Fruit implements Edible, Digestible {
    public void eat() {
        //...
    }

    public void digest() {
        //... 
    }
}

// В Java Вы можете наследовать только один класс, однако можете реализовывать
// несколько интерфейсов. Например:
public class ExampleClass extends ExampleClassParent implements InterfaceOne, InterfaceTwo {
    public void InterfaceOneMethod() {

    }

    public void InterfaceTwoMethod() {

    }
}

```

## Почитать еще

Здесь приведены ссылки только для того, чтобы получить общее представление о Java. Гуглите, чтобы найти какие-либо конкретные примеры.

**Официальные руководства Oracle**:

* [Java Tutorial Trail from Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Модификаторы доступа в Java](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [Концепции объектно-ориентированного программирования](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Наследование](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Полиморфизм](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Абстракция](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Исключения](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Интерфейсы](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html)

**Уроки онлайн**

* [Learneroo.com - Изучение Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)


**Книги**:

* [Head First Java](http://www.headfirstlabs.com/books/hfjava/)

* [Objects First with Java](http://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660)

* [Java The Complete Reference](http://www.amazon.com/gp/product/0071606300)


---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
filename: javascript-ru.js
translators:
  - ["Alexey Gonchar", "http://github.com/finico"]
  - ["Andre Polykanine", "https://github.com/Oire"]
lang: ru-ru
---

JavaScript был создан в 1995 году Бренданом Айком, работавшим в компании Netscape.
Изначально он был задуман как простой язык сценариев для веб-сайтов, дополняющий
Java для более сложных веб-приложений, но его тесная интеграция с веб-страницами
и встроенная поддержка браузерами привели к тому, что он стал более распространённым,
чем Java в веб-интерфейсах.

JavaScript не ограничивается только веб-браузером: например, Node.js, программная
платформа, позволяющая выполнять JavaScript, основанная на движке V8 от браузера
Google Chrome, становится все более популярной.

```js
// Си-подобные комментарии. Однострочные комментарии начинаются с двух символов слэш,
/* а многострочные комментарии начинаются с последовательности слэш-звёздочка
   и заканчиваются символами звёздочка-слэш */

// Инструкции могут заканчиваться точкой с запятой ;
doStuff();

// ... но она необязательна, так как точки с запятой автоматически вставляются
// везде, где есть символ новой строки, за некоторыми исключениями.
doStuff()

// Так как эти исключения могут привести к неожиданным результатам, мы будем всегда
// использовать точку с запятой в этом руководстве.

///////////////////////////////////
// 1. Числа, Строки и Операторы

// В JavaScript только один тип числа (64-bit IEEE 754 double).
// Он имеет 52-битную мантиссу, которой достаточно для хранения целых чисел
// с точностью вплоть до 9✕10¹⁵.
3; // = 3
1.5; // = 1.5

// Некоторые простые арифметические операции работают так, как вы ожидаете.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004 (а некоторые - нет)
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// Включая деление с остатком.
5 / 2; // = 2.5

// Побитовые операции также имеются; когда вы проводите такую операцию,
// ваше число с плавающей запятой переводится в целое со знаком 
// длиной *до* 32 разрядов.
1 << 2; // = 4

// Приоритет в выражениях можно явно задать скобками.
(1 + 3) * 2; // = 8

// Есть три специальных значения, которые не являются реальными числами:
Infinity; // "бесконечность", например, результат деления на 0
-Infinity; // "минус бесконечность", результат деления отрицательного числа на 0
NaN; // "не число", например, результат деления 0/0

// Существует также логический тип.
true;
false;

// Строки создаются при помощи двойных или одинарных кавычек.
'абв';
"Привет, мир!";

// Для логического отрицания используется восклицательный знак.
!true; // = false
!false; // = true

// Строгое равенство ===
1 === 1; // = true
2 === 1; // = false

// Строгое неравенство !==
1 !== 1; // = false
2 !== 1; // = true

// Другие операторы сравнения
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Строки объединяются при помощи оператора +
"привет, " + "мир!"; // = "Привет, мир!"

// и сравниваются при помощи < и >
"a" < "b"; // = true

// Проверка равенства с приведением типов осуществляется двойным символом равно
"5" == 5; // = true
null == undefined; // = true

// ...если только не использовать ===
"5" === 5; // = false
null === undefined; // = false 

// ...приведение типов может привести к странному поведению...
13 + !0; // 14
"13" + !0; // '13true'

// Вы можете получить доступ к любому символу строки, используя метод charAt
"Это строка".charAt(0);  // = 'Э'

// ...или используйте метод substring, чтобы получить более крупные части
"Привет, мир".substring(0, 6); // = "Привет"

// length - это свойство, для его получения не нужно использовать ()
"Привет".length; // = 6

// Также есть null и undefined
null; // Намеренное отсутствие значения
undefined; // используется для обозначения переменных, не имеющих
           // присвоенного значения (хотя непосредственно undefined
           // является значением)

// false, null, undefined, NaN, 0 и "" — это ложь; всё остальное - истина.
// Следует отметить, что 0 — это ложь, а "0" — истина, несмотря на то, что
// 0 == "0".

///////////////////////////////////
// 2. Переменные, Массивы и Объекты

// Переменные объявляются при помощи ключевого слова var. JavaScript — язык с
// динамической типизацией, поэтому не нужно явно указывать тип. Для присваивания
// значения переменной используется символ =
var someVar = 5;

// если вы пропустите слово var, вы не получите сообщение об ошибке, ...
someOtherVar = 10;

// ...но ваша переменная будет создана в глобальном контексте, а не в текущем,
// где вы ее объявили.

// Переменным, объявленным без присвоения, устанавливается значение undefined.
var someThirdVar; // = undefined

// У математических операций есть сокращённые формы:
someVar += 5; // как someVar = someVar + 5; someVar теперь имеет значение 10
someVar *= 10; // теперь someVar имеет значение 100

// Ещё более краткая запись увеличения и уменьшения на единицу:
someVar++; // теперь someVar имеет значение 101
someVar--; // вернулись к 100

// Массивы — это нумерованные списки, содержащие значения любого типа.
var myArray = ["Привет", 45, true];

// Их элементы могут быть доступны при помощи синтаксиса с квадратными скобками.
// Индексы массивов начинаются с нуля.
myArray[1]; // = 45

// Массивы можно изменять, как и их длину,
myArray.push("Мир");
myArray.length; // = 4

// добавлять и редактировать определённый элемент
myArray[3] = "Привет";

// Объекты в JavaScript похожи на словари или ассоциативные массивы в других
// языках: неупорядоченный набор пар ключ-значение.
var myObj = {key1: "Привет", key2: "Мир"};

// Ключи — это строки, но кавычки необязательны, если строка удовлетворяет
// ограничениям для имён переменных. Значения могут быть любых типов.
var myObj = {myKey: "myValue", "my other key": 4};

// Атрибуты объектов можно также получить, используя квадратные скобки
myObj["my other key"]; // = 4

// или через точку, при условии, что ключ является допустимым идентификатором.
myObj.myKey; // = "myValue"

// Объекты изменяемы; можно изменять значения и добавлять новые ключи.
myObj.myThirdKey = true;

// Если вы попытаетесь получить доступ к несуществующему значению,
// вы получите undefined.
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. Логика и управляющие конструкции.

// Синтаксис для этого раздела почти такой же, как в Java.

// Условная конструкция работает, как и следовало ожидать,
var count = 1;
if (count == 3) {
    // выполняется, если count равен 3
} else if (count == 4) {
    // выполняется, если count равен 4
} else {
    // выполняется, если не 3 и не 4
}

// ...как и цикл while.
while (true){
    // Бесконечный цикл!
}

// Цикл do-while такой же, как while, но он всегда выполняется хотя бы раз.
var input
do {
    input = getInput();
} while (!isValid(input))

// цикл for такой же, как в C и Java:
// инициализация; условие; шаг.
for (var i = 0; i < 5; i++) {
    // выполнится 5 раз
}

// && — это логическое И, || — это логическое ИЛИ
if (house.size == "big" && house.color == "blue") {
    house.contains = "bear";
}
if (color == "red" || color == "blue") {
    // цвет красный или синий
}

// && и || используют сокращённое вычисление, что полезно
// для задания значений по умолчанию.
var name = otherName || "default";

// Оператор switch выполняет проверку на равенство при помощи ===
// используйте break, чтобы прервать выполнение после каждого case, 
// или выполнение пойдёт далее даже после правильного варианта. 
grade = 4;
switch (grade) {
  case 5:
    console.log("Отлично");
    break;
  case 4:
    console.log("Хорошо");
    break;
  case 3:
    console.log("Можете и лучше");
    break;
  default:
    console.log("Ой-вей!");
    break;
}


///////////////////////////////////
// 4. Функции, Область видимости и Замыкания

// Функции в  JavaScript объявляются при помощи ключевого слова function.
function myFunction(thing) {
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"

// Обратите внимание, что значение, которое будет возвращено, должно начинаться
// на той же строке, что и ключевое слово return, в противном случае вы будете
// всегда возвращать undefined по причине автоматической вставки точки с запятой.
// Следите за этим при использовании стиля форматирования Allman.
function myFunction()
{
    return // <- здесь точка с запятой вставится автоматически
    {
        thisIsAn: 'object literal'
    }
}
myFunction(); // = undefined

// В JavaScript функции — это объекты первого класса, поэтому они могут быть
// присвоены различным именам переменных и передаваться другим функциям 
// в качестве аргументов, например, когда назначается обработчик события:
function myFunction() {
    // этот код будет вызван через 5 секунд
}
setTimeout(myFunction, 5000);
// Примечание: setTimeout не является частью языка, но реализован в браузерах и Node.js

// Функции не обязательно должны иметь имя при объявлении — вы можете написать
// анонимное определение функции непосредственно в аргументе другой функции.
setTimeout(function() {
    // этот код будет вызван через 5 секунд
}, 5000);

// В JavaScript есть область видимости; функции имеют свою область
// видимости, а другие блоки — нет.
if (true) {
    var i = 5;
}
i; // = 5, а не undefined, как ожидалось бы в языках с блочной областью видимости

// Это привело к общепринятому шаблону "самозапускающихся анонимных функций",
// которые препятствуют проникновению переменных в глобальную область видимости
(function() {
    var temporary = 5;
    // Мы можем получить доступ к глобальной области для записи в «глобальный объект»,
    // который в веб-браузере всегда window. Глобальный объект может иметь другое
    // имя в таких платформах, как Node.js
    window.permanent = 10;
})();
temporary; // вызовет сообщение об ошибке с типом ReferenceError
permanent; // = 10

// Одной из самых мощных возможностей JavaScript являются замыкания. Если функция
// определена внутри другой функции, то внутренняя функция имеет доступ к 
// переменным внешней функции даже после того, как контекст выполнения выйдет из
// внешней функции.
function sayHelloInFiveSeconds(name) {
    var prompt = "Привет, " + name + "!";
    // Внутренние функции по умолчанию помещаются в локальную область видимости,
    // как если бы они были объявлены с помощью var.
    function inner() {
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout асинхронна, поэтому функция sayHelloInFiveSeconds сразу выйдет,
    // после чего setTimeout вызовет функцию inner. Однако поскольку функция inner
    // «замкнута» вокруг sayHelloInFiveSeconds, она по-прежнему имеет доступ к переменной prompt
    // на то время, когда она наконец будет вызвана.
}
sayHelloInFiveSeconds("Адам"); // Через 5 с откроется окно «Привет, Адам!»

///////////////////////////////////
// 5. Подробнее об объектах; Конструкторы и Прототипы

// Объекты могут содержать в себе функции.
var myObj = {
    myFunc: function() {
        return "Привет, мир!";
    }
};
myObj.myFunc(); // = "Привет, мир!"

// Когда вызываются функции, прикреплённые к объекту, они могут получить доступ
// к этому объекту с помощью ключевого слова this.
myObj = {
    myString: "Привет, мир!",
    myFunc: function() {
        return this.myString;
    }
};
myObj.myFunc(); // = "Привет, мир!"

// Значение this зависит от того, как функция вызывается, 
// а не от того, где она определена. Таким образом, наша функция не работает, 
// если она вызывается не в контексте объекта.
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// И наоборот, функция может быть присвоена объекту и получать доступ к нему
// через this, даже если она не была прикреплена к нему при объявлении.
var myOtherFunc = function() {
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "ПРИВЕТ, МИР!"

// Мы можем также указать контекст для выполнения функции при её вызове,
// используя call или apply.
var anotherFunc = function(s) {
    return this.myString + s;
}
anotherFunc.call(myObj, " И привет, Луна!"); // = "Привет, мир! И привет, Луна!"

// Функция apply почти такая же, но принимает в качестве списка аргументов массив.
anotherFunc.apply(myObj, [" И привет, Солнце!"]); // = "Привет, мир! И привет, Солнце!"

// Это полезно при работе с функцией, которая принимает последовательность
// аргументов, а вы хотите передать массив.
Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (Ой-ой!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// Но call и apply — только временные. Когда мы хотим связать функцию с объектом, 
// мы можем использовать bind.
var boundFunc = anotherFunc.bind(myObj);
boundFunc(" И привет, Сатурн!"); // = "Привет, мир! И привет, Сатурн!"

// Bind также может использоваться для частичного применения (каррирования) функции
var product = function(a, b) { return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// Когда вы вызываете функцию с помощью ключевого слова new, создается новый объект,
// доступный функции при помощи this. Такие функции называют конструкторами.
var MyConstructor = function() {
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// У каждого объекта в JavaScript есть прототип. Когда вы хотите получить
// доступ к свойству объекта, которое не существует в этом объекте, интерпретатор
// будет искать это свойство в прототипе.

// Некоторые реализации языка позволяют получить доступ к прототипу объекта
// через «магическое» свойство __proto__. Несмотря на то, что это может быть полезно
// для понимания прототипов, это не часть стандарта; мы увидим стандартные способы
// использования прототипов позже.
var myObj = {
    myString: "Привет, мир!"
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function() {
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// Для функций это тоже работает.
myObj.myFunc(); // = "Привет, мир!"

// Если интерпретатор не найдёт свойство в прототипе, то продожит поиск
// в прототипе прототипа и так далее.
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

// Здесь не участвует копирование; каждый объект хранит ссылку на свой прототип.
// Это означает, что мы можем изменить прототип, и наши изменения будут отражены везде.
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

// Мы упомянули, что свойство __proto__ нестандартно, и нет никакого стандартного
// способа, чтобы изменить прототип существующего объекта. Однако есть два
// способа создать новый объект с заданным прототипом.

// Первый способ — это Object.create, который появился в JavaScript недавно,
// а потому доступен ещё не во всех реализациях языка.
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// Второй способ, который работает везде, имеет дело с конструкторами.
// У конструкторов есть свойство с именем prototype. Это *не* прототип
// функции-конструктора; напротив, это прототип для новых объектов, которые
// будут созданы с помощью этого конструктора и ключевого слова new.
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function() {
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// У встроенных типов, таких, как строки и числа, также есть конструкторы, которые
// создают эквивалентные объекты-обёртки.
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// За исключением того, что они не в точности равны.
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0) {
    // Этот код не выполнится, потому что 0 - это ложь.
}

// Впрочем, объекты-обёртки и встроенные типы имеют общие прототипы,
// поэтому вы можете расширить функционал строк, например:
String.prototype.firstCharacter = function() {
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// Это часто используется в т.н. полифилах, которые реализуют новые возможности
// JavaScript в старой реализации языка, так что они могут быть использованы в
// старых средах, таких, как устаревшие браузеры.

// Например, мы упомянули, что Object.create доступен не во всех реализациях, но
// мы сможем использовать его с помощью такого полифила:
if (Object.create === undefined) { // не перезаписываем метод, если он существует
    Object.create = function(proto) {
        // создаём временный конструктор с правильным прототипом
        var Constructor = function(){};
        Constructor.prototype = proto;
        // затем используем его для создания нового,
        // правильно прототипированного объекта
        return new Constructor();
    }
}
```

## Что ещё почитать

[Современный учебник JavaScript (Илья Кантор)](http://learn.javascript.ru) — 
качественный учебник по JavaScript на русском языке.

[Mozilla Developer Network](https://developer.mozilla.org/ru/docs/Web/JavaScript) —
предоставляет отличную документацию для JavaScript, как он используется в браузерах.
Кроме того, это вики, поэтому, если вы знаете больше, вы можете помочь другим,
делясь своими знаниями.

[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/ru/) — это
подробное руководство по всем неинтуитивным особенностей языка.

[Stack Overflow](http://stackoverflow.com/questions/tagged/javascript) — можно
найти ответ почти на любой ваш вопрос, а если его нет, то задать вопрос самому.
---
language: json
filename: learnjson-ru.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
  - ["Dmitry Bessonov", "https://github.com/TheDmitry"]
lang: ru-ru
---

JSON - это очень простой формат обмена данными, и это будет самый легкий
курс из когда-либо представленных "Learn X in Y Minutes".

В чистом виде у JSON нет фактических комментариев, но большинство парсеров примут
комментарии в Си-стиле (//, /\* \*/). Для таких целей, конечно, все правильно
будет на 100% с точки зрения JSON. К счастью, в нашем случае данные скажут сами за себя.

```json
{
  "ключ": "значение",
  
  "ключи": "должны всегда заключаться в двойные кавычки",
  "числа": 0,
  "строки": "Пρивет, миρ. Допускаются все unicode-символы вместе с \"экранированием\".",
  "содержит логический тип?": true,
  "ничего": null,

  "большое число": 1.2e+100,

  "объекты": {
    "комментарий": "Большинство ваших структур будут представлять из себя объекты.",

    "массив": [0, 1, 2, 3, "Массивы могут содержать в себе любой тип.", 5],

    "другой объект": {
      "комментарий": "Они могут быть вложенными, и это очень полезно."
    }
  },

  "бессмыслие": [
    {
      "источники калия": ["бананы"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "нео"],
      [0, 0, 0, 1]
    ]
  ],
  
  "альтернативный стиль": {
    "комментарий": "проверьте это!"
  , "позиция запятой": "неважна, хоть и перед значением, все равно правильно"
  , "еще один комментарий": "как хорошо"
  },

  "это было недолго": "И вы справились. Теперь вы знаете все о JSON."
}
```
---
language: Julia
contributors:
    - ["Leah Hanson", "http://leahhanson.us"]
translators:
    - ["Sergey Skovorodkin", "https://github.com/skovorodkin"]
filename: learnjulia-ru.jl
lang: ru-ru
---

Julia — гомоиконный функциональный язык программирования для технических расчётов.
Несмотря на полную поддержку гомоиконных макросов, функций первого класса и конструкций управления низкого уровня, этот язык так же прост в изучении и применении, как и Python.

Документ описывает текущую dev-версию Julia от 18-о октября 2013 года.

```ruby

# Однострочные комментарии начинаются со знака решётки.

####################################################
## 1. Примитивные типы данных и операторы
####################################################

# Всё в Julia — выражение.

# Простые численные типы
3 # => 3 (Int64)
3.2 # => 3.2 (Float64)
2 + 1im # => 2 + 1im (Complex{Int64})
2//3 # => 2//3 (Rational{Int64})

# Доступны все привычные инфиксные операторы
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
5 / 2 # => 2.5 # деление Int на Int всегда возвращает Float
div(5, 2) # => 2 # для округления к нулю используется div
5 \ 35 # => 7.0
2 ^ 2 # => 4 # возведение в степень
12 % 10 # => 2

# С помощью скобок можно изменить приоритет операций
(1 + 3) * 2 # => 8

# Побитовые операторы
~2 # => -3   # НЕ (NOT)
3 & 5 # => 1 # И (AND)
2 | 4 # => 6 # ИЛИ (OR)
2 $ 4 # => 6 # сложение по модулю 2 (XOR)
2 >>> 1 # => 1 # логический сдвиг вправо
2 >> 1  # => 1 # арифметический сдвиг вправо
2 << 1  # => 4 # логический/арифметический сдвиг влево

# Функция bits возвращает бинарное представление числа
bits(12345)
# => "0000000000000000000000000000000000000000000000000011000000111001"
bits(12345.0)
# => "0100000011001000000111001000000000000000000000000000000000000000"

# Логические значения являются примитивами
true
false

# Булевы операторы
!true # => false
!false # => true
1 == 1 # => true
2 == 1 # => false
1 != 1 # => false
2 != 1 # => true
1 < 10 # => true
1 > 10 # => false
2 <= 2 # => true
2 >= 2 # => true
# Сравнения можно объединять цепочкой
1 < 2 < 3 # => true
2 < 3 < 2 # => false

# Строки объявляются с помощью двойных кавычек — "
"This is a string."

# Символьные литералы создаются с помощью одинарных кавычек — '
'a'

# Строки индексируются как массивы символов
"This is a string"[1] # => 'T' # Индексы начинаются с единицы
# Индексирование не всегда правильно работает для UTF8-строк,
# поэтому рекомендуется использовать итерирование (map, for-циклы и т.п.).

# Для строковой интерполяции используется знак доллара ($):
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
# В скобках можно использовать любое выражение языка.

# Другой способ форматирования строк — макрос printf
@printf "%d is less than %f" 4.5 5.3 # 5 is less than 5.300000

####################################################
## 2. Переменные и коллекции
####################################################

# Вывод
println("I'm Julia. Nice to meet you!")

# Переменные инициализируются без предварительного объявления
some_var = 5 # => 5
some_var # => 5

# Попытка доступа к переменной до инициализации вызывает ошибку
try
    some_other_var # => ERROR: some_other_var not defined
catch e
    println(e)
end

# Имена переменных начинаются с букв.
# После первого символа можно использовать буквы, цифры, 
# символы подчёркивания и восклицательные знаки.
SomeOtherVar123! = 6 # => 6

# Допустимо использование unicode-символов
☃ = 8 # => 8
# Это особенно удобно для математических обозначений
2 * π # => 6.283185307179586

# Рекомендации по именованию:
# * имена переменных в нижнем регистре, слова разделяются символом 
#   подчёркивания ('\_');
#
# * для имён типов используется CamelCase;
#
# * имена функций и макросов в нижнем регистре
#   без разделения слов символом подчёркивания;
#
# * имя функции, изменяющей переданные ей аргументы (in-place function),
#   оканчивается восклицательным знаком.

# Массив хранит последовательность значений, индексируемых с единицы до n:
a = Int64[] # => пустой массив Int64-элементов

# Одномерный массив объявляется разделёнными запятой значениями.
b = [4, 5, 6] # => массив из трёх Int64-элементов: [4, 5, 6]
b[1] # => 4
b[end] # => 6

# Строки двумерного массива разделяются точкой с запятой.
# Элементы строк разделяются пробелами.
matrix = [1 2; 3 4] # => 2x2 Int64 Array: [1 2; 3 4]

# push! и append! добавляют в список новые элементы
push!(a,1)     # => [1]
push!(a,2)     # => [1,2]
push!(a,4)     # => [1,2,4]
push!(a,3)     # => [1,2,4,3]
append!(a,b) # => [1,2,4,3,4,5,6]

# pop! удаляет из списка последний элемент
pop!(b)        # => возвращает 6; массив b снова равен [4,5]

# Вернём 6 обратно
push!(b,6)   # b снова [4,5,6].

a[1] # => 1 # индексы начинаются с единицы!

# Последний элемент можно получить с помощью end
a[end] # => 6

# Операции сдвига
shift!(a) # => 1 and a is now [2,4,3,4,5,6]
unshift!(a,7) # => [7,2,4,3,4,5,6]

# Восклицательный знак на конце названия функции означает,
# что функция изменяет переданные ей аргументы.
arr = [5,4,6] # => массив из 3 Int64-элементов: [5,4,6]
sort(arr) # => [4,5,6]; но arr равен [5,4,6]
sort!(arr) # => [4,5,6]; а теперь arr — [4,5,6]

# Попытка доступа за пределами массива выбрасывает BoundsError
try
    a[0] # => ERROR: BoundsError() in getindex at array.jl:270
    a[end+1] # => ERROR: BoundsError() in getindex at array.jl:270
catch e
    println(e)
end

# Вывод ошибок содержит строку и файл, где произошла ошибка,
# даже если это случилось в стандартной библиотеке.
# Если вы собрали Julia из исходных кодов, 
# то найти эти файлы можно в директории base.

# Создавать массивы можно из последовательности
a = [1:5] # => массив из 5 Int64-элементов: [1,2,3,4,5]

# Срезы
a[1:3] # => [1, 2, 3]
a[2:] # => [2, 3, 4, 5]
a[2:end] # => [2, 3, 4, 5]

# splice! удаляет элемент из массива
# Remove elements from an array by index with splice!
arr = [3,4,5]
splice!(arr,2) # => 4 ; arr теперь равен [3,5]

# append! объединяет списки
b = [1,2,3]
append!(a,b) # теперь a равен [1, 2, 3, 4, 5, 1, 2, 3]

# Проверка на вхождение
in(1, a) # => true

# Длина списка
length(a) # => 8

# Кортеж — неизменяемая структура.
tup = (1, 2, 3) # => (1,2,3) # кортеж (Int64,Int64,Int64).
tup[1] # => 1
try:
    tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
catch e
    println(e)
end

# Многие функции над списками работают и для кортежей
length(tup) # => 3
tup[1:2] # => (1,2)
in(2, tup) # => true

# Кортежи можно распаковывать в переменные
a, b, c = (1, 2, 3) # => (1,2,3)  # a = 1, b = 2 и c = 3

# Скобки из предыдущего примера можно опустить
d, e, f = 4, 5, 6 # => (4,5,6)

# Кортеж из одного элемента не равен значению этого элемента
(1,) == 1 # => false
(1) == 1 # => true

# Обмен значений
e, d = d, e  # => (5,4) # d = 5, e = 4


# Словари содержат ассоциативные массивы
empty_dict = Dict() # => Dict{Any,Any}()

# Для создания словаря можно использовать литерал
filled_dict = ["one"=> 1, "two"=> 2, "three"=> 3]
# => Dict{ASCIIString,Int64}

# Значения ищутся по ключу с помощью оператора []
filled_dict["one"] # => 1

# Получить все ключи
keys(filled_dict)
# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# Заметьте, словарь не запоминает порядок, в котором добавляются ключи.

# Получить все значения.
values(filled_dict)
# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# То же касается и порядка значений.

# Проверка вхождения ключа в словарь
in(("one", 1), filled_dict) # => true
in(("two", 3), filled_dict) # => false
haskey(filled_dict, "one") # => true
haskey(filled_dict, 1) # => false

# Попытка обратиться к несуществующему ключу выбросит ошибку
try
    filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489
catch e
    println(e)
end

# Используйте метод get со значением по умолчанию, чтобы избежать этой ошибки
# get(dictionary,key,default_value)
get(filled_dict,"one",4) # => 1
get(filled_dict,"four",4) # => 4

# Для коллекций неотсортированных уникальных элементов используйте Set
empty_set = Set() # => Set{Any}()
# Инициализация множества
filled_set = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4)

# Добавление элементов
push!(filled_set,5) # => Set{Int64}(5,4,2,3,1)

# Проверка вхождения элементов во множество
in(2, filled_set) # => true
in(10, filled_set) # => false

# Функции для получения пересечения, объединения и разницы.
other_set = Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3)
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4)


####################################################
## 3. Поток управления
####################################################

# Создадим переменную
some_var = 5

# Выражение if. Отступы не имеют значения.
if some_var > 10
    println("some_var is totally bigger than 10.")
elseif some_var < 10    # Необязательная ветка elseif.
    println("some_var is smaller than 10.")
else                    # else-ветка также опциональна.
    println("some_var is indeed 10.")
end
# => prints "some var is smaller than 10"


# Цикл for проходит по итерируемым объектам
# Примеры итерируемых типов: Range, Array, Set, Dict и String.
for animal=["dog", "cat", "mouse"]
    println("$animal is a mammal")
    # Для вставки значения переменной или выражения в строку используется $
end
# Выведет:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# Другой вариант записи.
for animal in ["dog", "cat", "mouse"]
    println("$animal is a mammal")
end
# Выведет:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$(a[1]) is a $(a[2])")
end
# Выведет:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$k is a $v")
end
# Выведет:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# Цикл while выполняется до тех пор, пока верно условие
x = 0
while x < 4
    println(x)
    x += 1  # Короткая запись x = x + 1
end
# Выведет:
#   0
#   1
#   2
#   3

# Обработка исключений
try
   error("help")
catch e
   println("caught it $e")
end
# => caught it ErrorException("help")


####################################################
## 4. Функции
####################################################

# Для определения новой функции используется ключевое слово 'function'
#function имя(аргументы)
#  тело...
#end
function add(x, y)
    println("x is $x and y is $y")

    # Функция возвращает значение последнего выражения
    x + y
end

add(5, 6) # => Вернёт 11, напечатав "x is 5 and y is 6"

# Функция может принимать переменное количество позиционных аргументов.
function varargs(args...)
    return args
    # для возвращения из функции в любом месте используется 'return'
end
# => varargs (generic function with 1 method)

varargs(1,2,3) # => (1,2,3)

# Многоточие (...) — это splat.
# Мы только что воспользовались им в определении функции.
# Также его можно использовать при вызове функции,
# где он преобразует содержимое массива или кортежа в список аргументов.
Set([1,2,3])    # => Set{Array{Int64,1}}([1,2,3]) # формирует множество массивов
Set([1,2,3]...) # => Set{Int64}(1,2,3) # эквивалентно Set(1,2,3)

x = (1,2,3)     # => (1,2,3)
Set(x)          # => Set{(Int64,Int64,Int64)}((1,2,3)) # множество кортежей
Set(x...)       # => Set{Int64}(2,3,1)


# Опциональные позиционные аргументы
function defaults(a,b,x=5,y=6)
    return "$a $b and $x $y"
end

defaults('h','g') # => "h g and 5 6"
defaults('h','g','j') # => "h g and j 6"
defaults('h','g','j','k') # => "h g and j k"
try
    defaults('h') # => ERROR: no method defaults(Char,)
    defaults() # => ERROR: no methods defaults()
catch e
    println(e)
end

# Именованные аргументы
function keyword_args(;k1=4,name2="hello") # обратите внимание на ;
    return ["k1"=>k1,"name2"=>name2]
end

keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4]
keyword_args(k1="mine") # => ["k1"=>"mine","name2"=>"hello"]
keyword_args() # => ["name2"=>"hello","k2"=>4]

# В одной функции можно совмещать все виды аргументов
function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo")
    println("normal arg: $normal_arg")
    println("optional arg: $optional_positional_arg")
    println("keyword arg: $keyword_arg")
end

all_the_args(1, 3, keyword_arg=4)
# Выведет:
#   normal arg: 1
#   optional arg: 3
#   keyword arg: 4

# Функции в Julia первого класса 
function create_adder(x)
    adder = function (y)
        return x + y
    end
    return adder
end

# Анонимная функция
(x -> x > 2)(3) # => true

# Эта функция идентичная предыдущей версии create_adder
function create_adder(x)
    y -> x + y
end

# Если есть желание, можно воспользоваться полным вариантом
function create_adder(x)
    function adder(y)
        x + y
    end
    adder
end

add_10 = create_adder(10)
add_10(3) # => 13


# Встроенные функции высшего порядка
map(add_10, [1,2,3]) # => [11, 12, 13]
filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7]

# Списковые сборки
[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13]
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]

####################################################
## 5. Типы
####################################################

# Julia has a type system.
# Каждое значение имеет тип, но переменные не определяют тип значения.
# Функция `typeof` возвращает тип значения.
typeof(5) # => Int64

# Types are first-class values
# Типы являются значениями первого класса
typeof(Int64) # => DataType
typeof(DataType) # => DataType
# Тип DataType представляет типы, включая себя самого.

# Типы используются в качестве документации, для оптимизации и организации.
# Статически типы не проверяются.

# Пользователь может определять свои типы
# Типы похожи на структуры в других языках
# Новые типы определяются с помощью ключевого слова `type`

# type Name
#   field::OptionalType
#   ...
# end
type Tiger
  taillength::Float64
  coatcolor # отсутствие типа равносильно `::Any`
end

# Аргументы конструктора по умолчанию — свойства типа
# в порядке их определения.
tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange")

# Тип объекта по сути является конструктором значений такого типа
sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire")

# Эти типы, похожие на структуры, называются конкретными.
# Можно создавать объекты таких типов, но не их подтипы.
# Другой вид типов — абстрактные типы.

# abstract Name
abstract Cat # просто имя и точка в иерархии типов

# Объекты абстрактных типов создавать нельзя, 
# но зато от них можно наследовать подтипы.
# Например, Number — это абстрактный тип.
subtypes(Number) # => 6 элементов в массиве Array{Any,1}:
                 #     Complex{Float16}
                 #     Complex{Float32}
                 #     Complex{Float64}
                 #     Complex{T<:Real}
                 #     ImaginaryUnit
                 #     Real
subtypes(Cat) # => пустой массив Array{Any,1}

# У всех типов есть супертип. Для его определения есть функция `super`.
typeof(5) # => Int64
super(Int64) # => Signed
super(Signed) # => Real
super(Real) # => Number
super(Number) # => Any
super(super(Signed)) # => Number
super(Any) # => Any
# Все эти типы, за исключением Int64, абстрактные.

# Для создания подтипа используется оператор <:
type Lion <: Cat # Lion — это подтип Cat
  mane_color
  roar::String
end

# У типа может быть несколько конструкторов.
# Для создания нового определите функцию с именем, как у типа,
# и вызовите имеющийся конструктор.
Lion(roar::String) = Lion("green",roar)
# Мы создали внешний (т.к. он находится вне определения типа) конструктор.

type Panther <: Cat # Panther — это тоже подтип Cat
  eye_color

  # Определим свой конструктор вместо конструктора по умолчанию
  Panther() = new("green")
end
# Использование внутренних конструкторов позволяет
# определять, как будут создаваться объекты типов.
# Но по возможности стоит пользоваться внешними конструкторами.

####################################################
## 6. Мультиметоды
####################################################

# Все именованные функции являются generic-функциями,
# т.е. все они состоят из разных методов.
# Каждый конструктор типа Lion — это метод generic-функции Lion.

# Приведём пример без использования конструкторов, создадим функцию meow

# Определения Lion, Panther и Tiger
function meow(animal::Lion)
  animal.roar # доступ к свойству типа через точку
end

function meow(animal::Panther)
  "grrr"
end

function meow(animal::Tiger)
  "rawwwr"
end

# Проверка
meow(tigger) # => "rawwr"
meow(Lion("brown","ROAAR")) # => "ROAAR"
meow(Panther()) # => "grrr"

# Вспомним иерархию типов
issubtype(Tiger,Cat) # => false
issubtype(Lion,Cat) # => true
issubtype(Panther,Cat) # => true

# Определим функцию, принимающую на вход объекты типа Cat
function pet_cat(cat::Cat)
  println("The cat says $(meow(cat))")
end

pet_cat(Lion("42")) # => выведет "The cat says 42"
try
    pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,)
catch e
    println(e)
end

# В объектно-ориентированных языках распространена одиночная диспетчеризация —
# подходящий метод выбирается на основе типа первого аргумента.
# В Julia все аргументы участвуют в выборе нужного метода.

# Чтобы понять разницу, определим функцию с несколькими аргументами.
function fight(t::Tiger,c::Cat)
  println("The $(t.coatcolor) tiger wins!")
end
# => fight (generic function with 1 method)

fight(tigger,Panther()) # => выведет The orange tiger wins!
fight(tigger,Lion("ROAR")) # => выведет The orange tiger wins!

# Переопределим поведение функции, если Cat-объект является Lion-объектом
fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!")
# => fight (generic function with 2 methods)

fight(tigger,Panther()) # => выведет The orange tiger wins!
fight(tigger,Lion("ROAR")) # => выведет The green-maned lion wins!

# Драться можно не только с тиграми!
fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))")
# => fight (generic function with 3 methods)

fight(Lion("balooga!"),Panther()) # => выведет The victorious cat says grrr
try
  fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion)
catch
end

# Вообще, пускай кошачьи могут первыми проявлять агрессию
fight(c::Cat,l::Lion) = println("The cat beats the Lion")
# => Warning: New definition
#    fight(Cat,Lion) at none:1
# is ambiguous with
#    fight(Lion,Cat) at none:2.
# Make sure
#    fight(Lion,Lion)
# is defined first.
#fight (generic function with 4 methods)

# Предупреждение говорит, что неясно, какой из методов вызывать:
fight(Lion("RAR"),Lion("brown","rarrr")) # => выведет The victorious cat says rarrr
# Результат может оказаться разным в разных версиях Julia

fight(l::Lion,l2::Lion) = println("The lions come to a tie")
fight(Lion("RAR"),Lion("brown","rarrr")) # => выведет The lions come to a tie


# Под капотом
# Язык позволяет посмотреть на сгенерированные ассемблерный и LLVM-код.

square_area(l) = l * l      # square_area (generic function with 1 method)

square_area(5) #25

# Что происходит, когда мы передаём функции square_area целое число?
code_native(square_area, (Int32,))  
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1              # Вводная часть
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    movsxd  RAX, EDI        # 
	#	    imul    RAX, RAX        # 
	#	    pop RBP                 #
	#	    ret                     #

code_native(square_area, (Float32,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vmulss  XMM0, XMM0, XMM0  # Произведение чисел одинарной точности (AVX)
	#	    pop RBP
	#	    ret

code_native(square_area, (Float64,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vmulsd  XMM0, XMM0, XMM0 # Произведение чисел двойной точности (AVX)
	#	    pop RBP
	#	    ret
	#	
# Если хотя бы один из аргументов является числом с плавающей запятой,
# то Julia будет использовать соответствующие инструкции.
# Вычислим площать круга
circle_area(r) = pi * r * r     # circle_area (generic function with 1 method)
circle_area(5)                  # 78.53981633974483

code_native(circle_area, (Int32,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	Source line: 1
	#	    vcvtsi2sd   XMM0, XMM0, EDI          # Загрузить целое число (r)
	#	    movabs  RAX, 4593140240              # Загрузить pi
	#	    vmulsd  XMM1, XMM0, QWORD PTR [RAX]  # pi * r
	#	    vmulsd  XMM0, XMM0, XMM1             # (pi * r) * r
	#	    pop RBP
	#	    ret
	#

code_native(circle_area, (Float64,))
	#	    .section    __TEXT,__text,regular,pure_instructions
	#	Filename: none
	#	Source line: 1
	#	    push    RBP
	#	    mov RBP, RSP
	#	    movabs  RAX, 4593140496
	#	Source line: 1
	#	    vmulsd  XMM1, XMM0, QWORD PTR [RAX]
	#	    vmulsd  XMM0, XMM1, XMM0
	#	    pop RBP
	#	    ret
	#
```

## Что дальше?

Для более подробной информации читайте [документацию по языку](http://docs.julialang.org/en/latest/manual/)

Если вам нужна помощь, задавайте вопросы в [списке рассылки](https://groups.google.com/forum/#!forum/julia-users).
---
language: kotlin
filename: LearnKotlin-ru.kt
lang: ru-ru
contributors:
    - ["S Webber", "https://github.com/s-webber"]
translators:
    - ["Vadim Toptunov", "https://github.com/VadimToptunov"]
---

Kotlin - статистически типизированный язык для JVM, Android и браузера. Язык полностью cjdvtcnbv c Java. 
[Более детальная информация здесь.](https://kotlinlang.org/)

```kotlin
// Однострочные комментарии начинаются с //
/*
А вот так выглядят многострочные комментарии.
*/

// Ключевое слово "package" действует и используется // абсолютно также, как и в Java.
package com.learnxinyminutes.kotlin

/*
Точкой входа в программу на языке Kotlin является функция "main".
Приведенная ниже функция передает массив, содержащий любые аргументы из командной строки.
*/
fun main(args: Array<String>) {
    /*
    Объявление значений производится с помощью или "var", или "val".
    Значения объявленные с помощью "val" не могут быть изменены или перезаписаны, в то время как объявленные с помощью "var" - могут.
    */
    val fooVal = 10 // мы не можем потом изменить значение fooVal на какое-либо иное
    var fooVar = 10
    fooVar = 20 // значение fooVar затем может быть изменено.

    /*
    В большинстве случаев Kotlin самостоятельно может определить тип переменной, поэтому нам не нужно явно указывать его каждый раз.
    Мы можем явно объявить тип переменной следующим образом:
    */
    val foo: Int = 7

    /*
    Строки могут быть представлены тем же образом, что и в Java.
    Для экранирования используется обратный слэш.
    */
    val fooString = "My String Is Here!"
    val barString = "Printing on a new line?\nNo Problem!"
    val bazString = "Do you want to add a tab?\tNo Problem!"
    println(fooString)
    println(barString)
    println(bazString)

    /*
    Необработанная строка разделяется тройной кавычкой (""").
    Необработанные строки могут содержать символы новой строки и любые другие символы.
    */
    val fooRawString = """
fun helloWorld(val name : String) {
   println("Hello, world!")
}
"""
    println(fooRawString)

    /*
    Строки могут содержать в себе шаблонные выражения.
    Шаблонные выражения начинаются со знака доллара ($).
    */
    val fooTemplateString = "$fooString has ${fooString.length} characters"
    println(fooTemplateString)

    /*
    Переменная, которая содержит null должна быть явно обозначена как nullable.
    Переменная может быть обозначена как nullable с помощью добавления знака вопроса(?) к ее типу.
    Мы можем получить доступ к nullable переменной используя оператор ?. .
    Для того, чтобы указать иное значение, если переменная является null, мы используем оператор ?: .
    */
    var fooNullable: String? = "abc"
    println(fooNullable?.length) // => 3
    println(fooNullable?.length ?: -1) // => 3
    fooNullable = null
    println(fooNullable?.length) // => null
    println(fooNullable?.length ?: -1) // => -1

    /*
    Функции могут быть объявлены с помощью ключевого слова "fun".
    Аргументы функции указываются в скобках после имени функции.
    Аргументы функции также могу иметь и значение по умолчанию.
    Если требуется, то тип возвращаемого функцией значения, может быть указан после аргументов.
    */
    fun hello(name: String = "world"): String {
        return "Hello, $name!"
    }
    println(hello("foo")) // => Hello, foo!
    println(hello(name = "bar")) // => Hello, bar!
    println(hello()) // => Hello, world!

    /*
    Параметр функции может быть отмечен с помощью ключевого слова "vararg", для того чтобы позволить аргументам попасть в функцию.
    */
    fun varargExample(vararg names: Int) {
        println("Argument has ${names.size} elements")
    }
    varargExample() // => Argument has 0 elements
    varargExample(1) // => Argument has 1 elements
    varargExample(1, 2, 3) // => Argument has 3 elements

    /*
    Если функция состоит из одиночного выражения, фигурные скобки могут быть опущены. Тело функции указывается после знака = .
    */
    fun odd(x: Int): Boolean = x % 2 == 1
    println(odd(6)) // => false
    println(odd(7)) // => true

    // Если возвращаемый тип может быть выведен, то нам не нужно его дополнительно указывать.
    fun even(x: Int) = x % 2 == 0
    println(even(6)) // => true
    println(even(7)) // => false

    // Функции могут брать другие функции в качестве аргументов, а также могут возвращать функции. 
    fun not(f: (Int) -> Boolean): (Int) -> Boolean {
        return {n -> !f.invoke(n)}
    }
    // Именованные функции могут быть определены в качестве аргументов с помощью оператора :: .
    val notOdd = not(::odd)
    val notEven = not(::even)
    // Lambda-выражения могут быть определены в качестве аргументов.
    val notZero = not {n -> n == 0}
    /*
    Если lambda-выражение имеет только один параметр, то ее определение может быть опущено (вместе с ->).
    Имя этого единственного параметра будет "it".
    */
    val notPositive = not {it > 0}
    for (i in 0..4) {
        println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}")
    }

    // Ключевое слово "class" используется для 
    // объявления классов.
    class ExampleClass(val x: Int) {
        fun memberFunction(y: Int): Int {
            return x + y
        }

        infix fun infixMemberFunction(y: Int): Int {
            return x * y
        }
    }
    /*
    Чтобы создать новый экземпляр класса, нужно вызвать конструктор.
    Обратите внимание, что в Kotlin нет ключевого слова "new".
    */
    val fooExampleClass = ExampleClass(7)
    // Функции-члены могут быть вызваны с использованием точечной нотации.
    println(fooExampleClass.memberFunction(4)) // => 11
    /*
    В случае, если функция была помечена ключевым словом "infix", она может быть вызвана с помощью инфиксной нотации. 
    */
    println(fooExampleClass infixMemberFunction 4) // => 28

    /*
    Data-классы - это компактный способ создать классы, которые лишь хранят данные.
    Методы "hashCode"/"equals" и "toString" генерируютсяч автоматически. 
    */
    data class DataClassExample (val x: Int, val y: Int, val z: Int)
    val fooData = DataClassExample(1, 2, 4)
    println(fooData) // => DataClassExample(x=1, y=2, z=4)

    // Data-классы обладают функцией "copy".
    val fooCopy = fooData.copy(y = 100)
    println(fooCopy) // => DataClassExample(x=1, y=100, z=4)

    // Объекты могут быть деструктурированы на множество переменных.
    val (a, b, c) = fooCopy
    println("$a $b $c") // => 1 100 4
    
    // Деструктурирование в цикле "for"
    for ((a, b, c) in listOf(fooData)) {
        println("$a $b $c") // => 1 100 4
    }
    
    val mapData = mapOf("a" to 1, "b" to 2)
    // Map.Entry также может быть дествуктурирован
    for ((key, value) in mapData) {
        println("$key -> $value")
    }

    // Функция "with" аналогична оператору "with" в JavaScript.
    data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
    val fooMutableData = MutableDataClassExample(7, 4, 9)
    with (fooMutableData) {
        x -= 2
        y += 2
        z--
    }
    println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8)

    /*
    Можно создать список с помощью функции "ListOf".
    Этот список будет неизменяемым, т.е. элементы не могут быть удалены или добавлены в него.
    */
    val fooList = listOf("a", "b", "c")
    println(fooList.size) // => 3
    println(fooList.first()) // => a
    println(fooList.last()) // => c
    // Элементы списка доступны по их индексу в нем. 
    println(fooList[1]) // => b

    // Изменяемый список может быть создан спомощью функции "mutableListOf".
    val fooMutableList = mutableListOf("a", "b", "c")
    fooMutableList.add("d")
    println(fooMutableList.last()) // => d
    println(fooMutableList.size) // => 4

    // Мы можем создать набор, используя функцию "setOf". 
    val fooSet = setOf("a", "b", "c")
    println(fooSet.contains("a")) // => true
    println(fooSet.contains("z")) // => false

    // Мы можем создать отображение (map), используя функцию "mapOf".
    val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9)
    // Получить доступ к значениям отображения (map) можно с помощью их ключа. 
    println(fooMap["a"]) // => 8

    /*
    Последовательности представляют собой коллекции с ленивой оценкой.
    Мы можем создать последовательность, используя функцию "generateSequence".
    */
    val fooSequence = generateSequence(1, { it + 1 })
    val x = fooSequence.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // Пример использования последовательности для генерации чисел Фибоначчи:
    fun fibonacciSequence(): Sequence<Long> {
        var a = 0L
        var b = 1L

        fun next(): Long {
            val result = a + b
            a = b
            b = result
            return a
        }

        return generateSequence(::next)
    }
    val y = fibonacciSequence().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

    // Kotlin предоставляет функции высшего порядка для работы с коллекциями.
    val z = (1..9).map {it * 3}
                  .filter {it < 20}
                  .groupBy {it % 2 == 0}
                  .mapKeys {if (it.key) "even" else "odd"}
    println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]}

    // Цикл "for" может использоваться со всем, что предоставляет  итератор.
    for (c in "hello") {
        println(c)
    }

    // Циклы "while" работают также, как и в других языках.
    var ctr = 0
    while (ctr < 5) {
        println(ctr)
        ctr++
    }
    do {
        println(ctr)
        ctr++
    } while (ctr < 10)

    /*
    "if" может быть использован в качестве выражения, которое возвращает значение.
    По этой причине в Kotlin тернарный оператор ?: не нужен.
    */
    val num = 5
    val message = if (num % 2 == 0) "even" else "odd"
    println("$num is $message") // => 5 is odd

    // "when" может быть использован как альтернатива цепочке "if-else if".
    val i = 10
    when {
        i < 7 -> println("first block")
        fooString.startsWith("hello") -> println("second block")
        else -> println("else block")
    }

    // "when" может быть использован с аргументами.
    when (i) {
        0, 21 -> println("0 or 21")
        in 1..20 -> println("in the range 1 to 20")
        else -> println("none of the above")
    }

    // "when" также может быть использовано как функция, возвращающая значение.
    var result = when (i) {
        0, 21 -> "0 or 21"
        in 1..20 -> "in the range 1 to 20"
        else -> "none of the above"
    }
    println(result)

    /*
    Мы можем проверить, что объект принадлежит к определенному типу, используя оператор "is".
    Если объект проходит проверку типа, то он может использоваться как этот тип без явной его  передачи.
    */
    fun smartCastExample(x: Any) : Boolean {
        if (x is Boolean) {
            // x is automatically cast to Boolean
            return x
        } else if (x is Int) {
            // x is automatically cast to Int
            return x > 0
        } else if (x is String) {
            // x is automatically cast to String
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(smartCastExample("Hello, world!")) // => true
    println(smartCastExample("")) // => false
    println(smartCastExample(5)) // => true
    println(smartCastExample(0)) // => false
    println(smartCastExample(true)) // => true

    // Smartcast также работает с блоком "when"
    fun smartCastWhenExample(x: Any) = when (x) {
        is Boolean -> x
        is Int -> x > 0
        is String -> x.isNotEmpty()
        else -> false
    }

    /*
    Расширения - это способ добавить новый функционал к классу. 
    Это то же самое, что методы расширений в C#.
    */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("Hello, world!".remove('l')) // => Heo, word!

    println(EnumExample.A) // => A
    println(ObjectExample.hello()) // => hello
}

// Enum-классы схожи с типами enum в Java.
enum class EnumExample {
    A, B, C
}

/*
Ключевое слово "object" может использоваться для создания одноэлементных объектов.
Мы не можем его инстанцировать, но можем вызывать его уникальный экземпляр по имени.
Это похоже на одиночные объекты Scala.
*/
object ObjectExample {
    fun hello(): String {
        return "hello"
    }
}

fun useObject() {
    ObjectExample.hello()
    val someRef: Any = ObjectExample // we use objects name just as is
}

```

### Дальнейшее чтение:

* [Учебные материалы по Kotlin](https://kotlinlang.org/docs/tutorials/)
* [Попробуй Kotlin в своем браузере](http://try.kotlinlang.org/)
* [Список ресурсов по языку Kotlin](http://kotlin.link/)
---
language: Lua
filename: learnlua-ru.lua
contributors:
    - ["Tyler Neylon", "http://tylerneylon.com/"]
translators:
    - ["Max Solomonov", "https://vk.com/solomonovmaksim"]
    - ["Max Truhonin", "https://vk.com/maximmax42"]
    - ["Konstantin Gromyko", "https://vk.com/id0x1765d79"]
    - ["Stanislav Gromov", "https://vk.com/id156354391"]
lang: ru-ru
---

```lua
-- Два дефиса начинают однострочный комментарий.

--[[
    Добавление двух квадратных скобок
    делает комментарий многострочным.
--]]
--------------------------------------------------------------------------------
-- 1. Переменные, циклы и условия.
--------------------------------------------------------------------------------

num = 42  -- Все числа имеют тип double.
-- Не волнуйтесь, в 64-битных double 52 бита
-- отведено под хранение целой части числа;
-- точность не является проблемой для
-- целочисленных значений, занимающих меньше 52 бит.

s = 'walternate'  -- Неизменные строки, как в Python.
t = "Двойные кавычки также приветствуются"
u = [[ Двойные квадратные скобки
       начинают и заканчивают
       многострочные значения.]]
t = nil  -- Удаляет определение переменной t; в Lua есть сборка мусора.

-- Блоки обозначаются ключевыми словами, такими как do/end:
while num < 50 do
  num = num + 1  -- Операторов ++ и += нет.
end

-- Ветвление "если":
if num > 40 then
  print('больше 40')
elseif s ~= 'walternate' then  -- ~= обозначает "не равно".
  -- Проверка равенства это ==, как в Python; работает для строк.
  io.write('не больше 40\n')  -- По умолчанию вывод в stdout.
else
  -- По умолчанию переменные являются глобальными.
  thisIsGlobal = 5  -- Стиль CamelСase является общим.

  -- Как сделать переменную локальной:
  local line = io.read()  -- Считывает введённую строку.

  -- Для конкатенации строк используется оператор .. :
  print('Зима пришла, ' .. line)
end

-- Неопределённые переменные возвращают nil.
-- Этот пример не является ошибочным:
foo = anUnknownVariable  -- Теперь foo = nil.

aBoolValue = false

-- Только значения nil и false являются ложными; 0 и '' являются истинными!
if not aBoolValue then print('это значение ложно') end

-- Для 'or' и 'and' действует принцип "какой оператор дальше,
-- тот и применяется". Это действует аналогично оператору a?b:c в C/js:
ans = aBoolValue and 'yes' or 'no'  --> 'no'

karlSum = 0
for i = 1, 100 do  -- Здесь указан диапазон, ограниченный с двух сторон.
  karlSum = karlSum + i
end

-- Используйте "100, 1, -1" как нисходящий диапазон:
fredSum = 0
for j = 100, 1, -1 do fredSum = fredSum + j end

-- В основном, диапазон устроен так: начало, конец[, шаг].

-- Другая конструкция цикла:
repeat
  print('путь будущего')
  num = num - 1
until num == 0

--------------------------------------------------------------------------------
-- 2. Функции.
--------------------------------------------------------------------------------

function fib(n)
  if n < 2 then return n end
  return fib(n - 2) + fib(n - 1)
end

-- Вложенные и анонимные функции являются нормой:
function adder(x)
  -- Возращаемая функция создаётся, когда вызывается функция adder,
  -- и запоминает значение переменной x:
  return function (y) return x + y end
end
a1 = adder(9)
a2 = adder(36)
print(a1(16))  --> 25
print(a2(64))  --> 100

-- Возвраты, вызовы функций и присвоения работают со списками,
-- которые могут иметь разную длину.
-- Лишние получатели принимают значение nil, а лишние значения игнорируются.

x, y, z = 1, 2, 3, 4
-- Теперь x = 1, y = 2, z = 3, а 4 просто отбрасывается.

function bar(a, b, c)
  print(a, b, c)
  return 4, 8, 15, 16, 23, 42
end

x, y = bar('zaphod')  --> выводит "zaphod  nil nil"
-- Теперь x = 4, y = 8, а значения 15..42 отбрасываются.

-- Функции могут быть локальными и глобальными. Эти строки делают одно и то же:
function f(x) return x * x end
f = function (x) return x * x end

-- Эти тоже:
local function g(x) return math.sin(x) end
local g = function(x) return math.sin(x) end
-- Эквивалентно для local function g(x)..., однако ссылки на g
-- в теле функции не будут работать, как ожидалось.
local g; g  = function (x) return math.sin(x) end
-- 'local g' будет прототипом функции.

-- Кстати, тригонометрические функции работают с радианами.

-- Вызов функции с одним строковым параметром не требует круглых скобок:
print 'hello'  -- Работает без ошибок.

-- Вызов функции с одним табличным параметром также
-- не требует круглых скобок (про таблицы в след. части):
print {} -- Тоже сработает.

--------------------------------------------------------------------------------
-- 3. Таблицы.
--------------------------------------------------------------------------------

-- Таблица = единственная составная структура данных в Lua;
-- представляет собой ассоциативный массив.
-- Подобно массивам в PHP или объектам в JS, они представляют собой
-- хеш-таблицы, которые также можно использовать в качестве списков.


-- Использование словарей:

-- Литералы имеют ключ по умолчанию:
t = {key1 = 'value1', key2 = false}

-- Строковые ключи используются, как в точечной нотации в JS:
print(t.key1)  -- Печатает 'value1'.
t.newKey = {}  -- Добавляет новую пару ключ/значение.
t.key2 = nil   -- Удаляет key2 из таблицы.

-- Литеральная нотация для любого значения ключа (кроме nil):
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28])  -- пишет "tau"

-- Ключ соответствует значению для чисел и строк, но при
-- использовании таблицы в качестве ключа берётся её экземпляр.
a = u['@!#']  -- Теперь a = 'qbert'.
b = u[{}]     -- Вы могли ожидать 1729, но получится nil:
-- b = nil, т.к. ключ не будет найден.
-- Это произойдёт потому, что за ключ мы использовали не тот же самый объект,
-- который был использован для сохранения оригинального значения.
-- Поэтому строки и числа удобнее использовать в качестве ключей.

-- Вызов функции с одной таблицей в качестве аргумента
-- не требует круглых скобок:
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'}  -- Печатает 'Sonmi~451'.

for key, val in pairs(u) do  -- Цикл по таблице.
  print(key, val)
end

-- _G - это таблица со всеми глобалями.
print(_G['_G'] == _G)  -- Печатает 'true'.

-- Использование таблиц, как списков / массивов:

-- Список значений с неявно заданными целочисленными ключами:
v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do  -- #v - размер списка v.
  print(v[i])  -- Нумерация начинается с 1 !!
end

-- Список не является отдельным типом. v - всего лишь таблица
-- с последовательными целочисленными ключами, воспринимаемая как список.

--------------------------------------------------------------------------------
-- 3.1 Метатаблицы и метаметоды.
--------------------------------------------------------------------------------

-- Таблицу можно связать с метатаблицей, задав ей поведение, как при
-- перегрузке операторов. Позже мы увидим, что метатаблицы поддерживают
-- поведение, как в js-прототипах.
f1 = {a = 1, b = 2}  -- Представляет дробь a/b.
f2 = {a = 2, b = 3}

-- Это не сработает:
-- s = f1 + f2

metafraction = {}
function metafraction.__add(f1, f2)
  local sum = {}
  sum.b = f1.b * f2.b
  sum.a = f1.a * f2.b + f2.a * f1.b
  return sum
end

setmetatable(f1, metafraction)
setmetatable(f2, metafraction)

s = f1 + f2  -- вызвать __add(f1, f2) на метатаблице от f1

-- f1, f2 не имеют ключа для своих метатаблиц в отличии от прототипов в js,
-- нужно получить его через getmetatable(f1). Метатаблица - обычная таблица
-- поэтому с ключами, известными для Lua (например, __add).

-- Но следущая строка будет ошибочной т.к в s нет метатаблицы:
-- t = s + s
-- Похожий на классы подход, приведенный ниже, поможет это исправить.

-- __index перегружает в метатаблице просмотр через точку:
defaultFavs = {animal = 'gru', food = 'donuts'}
myFavs = {food = 'pizza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal  -- работает! спасибо, мета-таблица.

--------------------------------------------------------------------------------
-- При неудаче прямой табличный поиск попытается использовать
-- значение __index в метатаблице, причём это рекурсивно.

-- Значение __index также может быть функцией
-- function(tbl, key) для настраиваемого поиска.

-- Значения типа __index, __add, ... называются метаметодами.
-- Ниже приведён полный список метаметодов.

-- __add(a, b)                          для a + b
-- __sub(a, b)                          для a - b
-- __mul(a, b)                          для a * b
-- __div(a, b)                          для a / b
-- __mod(a, b)                          для a % b
-- __pow(a, b)                          для a ^ b
-- __unm(a)                             для -a
-- __concat(a, b)                       для a .. b
-- __len(a)                             для #a
-- __eq(a, b)                           для a == b
-- __lt(a, b)                           для a < b
-- __le(a, b)                           для a <= b
-- __index(a, b) <функция или таблица>  для a.b
-- __newindex(a, b, c)                  для a.b = c
-- __call(a, ...)                       для a(...)

--------------------------------------------------------------------------------
-- 3.2 Классоподобные таблицы и наследование.
--------------------------------------------------------------------------------

-- В Lua нет поддержки классов на уровне языка,
-- однако существуют разные способы их создания с помощью
-- таблиц и метатаблиц.

-- Ниже приведён один из таких способов.

Dog = {}                                   -- 1.

function Dog:new()                         -- 2.
  local newObj = {sound = 'woof'}          -- 3.
  self.__index = self                      -- 4.
  return setmetatable(newObj, self)        -- 5.
end

function Dog:makeSound()                   -- 6.
  print('I say ' .. self.sound)
end

mrDog = Dog:new()                          -- 7.
mrDog:makeSound()  -- 'I say woof'         -- 8.

-- 1. Dog похоже на класс, но на самом деле это таблица.
-- 2. "function tablename:fn(...)" - то же самое, что и
--    "function tablename.fn(self, ...)", просто : добавляет первый аргумент
--    перед собой. См. пункты 7 и 8, чтобы понять, как self получает значение.
-- 3. newObj - это экземпляр класса Dog.
-- 4. "self" - экземпляр класса. Зачастую self = Dog, но с помощью наследования
--    это можно изменить. newObj получит свои функции, когда мы установим
--    метатаблицу для newObj и __index для self на саму себя.
-- 5. Напоминание: setmetatable возвращает первый аргумент.
-- 6. : работает, как в пункте 2, но в этот раз мы ожидаем,
--    что self будет экземпляром, а не классом.
-- 7. То же самое, что и Dog.new(Dog), поэтому self = Dog в new().
-- 8. То же самое, что mrDog.makeSound(mrDog); self = mrDog.
--------------------------------------------------------------------------------

-- Пример наследования:

LoudDog = Dog:new()                           -- 1.

function LoudDog:makeSound()
  local s = self.sound .. ' '                 -- 2.
  print(s .. s .. s)
end

seymour = LoudDog:new()                       -- 3.
seymour:makeSound()  -- 'woof woof woof'      -- 4.

--------------------------------------------------------------------------------
-- 1. LoudDog получит методы и переменные класса Dog.
-- 2. В self будет ключ 'sound' из new(), см. пункт 3.
-- 3. То же самое, что и "LoudDog.new(LoudDog)", конвертированное
--    в "Dog.new(LoudDog)", поскольку в LoudDog нет ключа 'new',
--    но в его метатаблице есть "__index = Dog".
--    Результат: Метатаблицей для seymour стала LoudDog,
--    а "LoudDog.__index = Dog". Поэтому seymour.key будет равно
--    seymour.key, LoudDog.key, Dog.key, в зависимости от того,
--    какая таблица будет первой с заданным ключом.
-- 4. Ключ 'makeSound' находится в LoudDog;
--    то же самое, что и "LoudDog.makeSound(seymour)".

-- При необходимости функция new() в подклассе
-- может быть похожа на аналог в базовом классе.
function LoudDog:new()
  local newObj = {}
  -- установить newObj
  self.__index = self
  return setmetatable(newObj, self)
end

--------------------------------------------------------------------------------
-- 4. Модули.
--------------------------------------------------------------------------------


--[[ Я закомментировал этот раздел, чтобы остальная часть скрипта осталась
--   работоспособной.
```

```lua
-- Предположим, файл mod.lua будет выглядеть так:
local M = {}

local function sayMyName()
  print('Hrunkner')
end

function M.sayHello()
  print('Привет, ')
  sayMyName()
end

return M

-- Другой файл может использовать функционал mod.lua:
local mod = require('mod')  -- Запустим файл mod.lua.

-- require - стандартный способ подключения модулей.
-- require ведёт себя так:     (если не кэшировано, см. ниже)
local mod = (function ()
  <содержимое mod.lua>
end)()
-- Файл mod.lua воспринимается, как тело функции, поэтому
-- все локальные переменные и функции внутри него не видны за его пределами.

-- Это работает, так как здесь mod = M в mod.lua:
mod.sayHello()  -- Выведет "Привет, Hrunkner".

-- Это будет ошибочным; sayMyName доступна только в mod.lua:
mod.sayMyName()  -- ошибка

-- Значения, возвращаемые require, кэшируются,
-- поэтому содержимое файла выполняется только 1 раз,
-- даже если он подключается с помощью require много раз.

-- Предположим, mod2.lua содержит "print('Hi!')".
local a = require('mod2')  -- Выведет "Hi!"
local b = require('mod2')  -- Ничего не выведет; a=b.

-- dofile, в отличии от require, работает без кэширования:
dofile('mod2')  --> Hi!
dofile('mod2')  --> Hi! (запустится снова)

-- loadfile загружает файл, но не запускает его.
f = loadfile('mod2')  -- Вызов f() запустит содержимое mod2.lua.

-- loadstring - это loadfile для строк.
g = loadstring('print(343)')  -- Вернет функцию.
g()  -- Напишет 343.

--]]

```
## Примечание (от автора)

Мне было интересно изучить Lua, чтобы делать игры при помощи <a href="http://love2d.org/">игрового движка LÖVE</a>.

Я начинал с <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">BlackBulletIV's Lua for programmers</a>.
Затем я прочитал официальную <a href="http://www.lua.org/pil/contents.html">Документацию по Lua</a>.

Также может быть полезной <a href="http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">Краткая справка по Lua</a> на lua-users.org.

Ещё из основных тем не охвачены стандартные библиотеки:

* <a href="http://lua-users.org/wiki/StringLibraryTutorial">библиотека string</a>
* <a href="http://lua-users.org/wiki/TableLibraryTutorial">библиотека table</a>
* <a href="http://lua-users.org/wiki/MathLibraryTutorial">библиотека math</a>
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">библиотека io</a>
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">библиотека os</a>

Кстати, весь файл написан на Lua; сохраните его как learn.lua и запустите при помощи "lua learn.lua" !

Изначально эта статья была написана для tylerneylon.com.
Также она доступна как <a href="https://gist.github.com/tylerneylon/5853042">github gist</a>. Удачи с Lua!
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
    - ["Jacob Ward", "http://github.com/JacobCWard/"]
translators:
    - ["Pirogov Alexey", "http://twitter.com/alex_pir"]
    - ["Andre Polykanine", "https://github.com/Oire"]
filename: markdown-ru.md
lang: ru-ru
---

Язык разметки Markdown создан Джоном Грубером (англ. John Gruber)
в 2004 году.
Авторы задавались целью создать максимально удобочитаемый
и удобный в публикации облегчённый язык разметки,
пригодный для последующего преобразования в HTML
(а также и в другие форматы).

Также реализации Markdown варьируют в зависимости от парсера.
В этом руководстве будет указано, какие функции универсальны для языка,
а какие зависят от конкретного парсера.

- [HTML-элементы](#html-elements)
- [Заголовки](#headings)
- [Простейшая стилизация текста](#simple-text-styles)
- [Абзацы](#paragraphs)
- [Списки](#lists)
- [Блоки кода](#code-blocks)
- [Горизонтальный разделитель](#horizontal-rule)
- [Ссылки](#links)
- [Изображения](#images)
- [Разное](#miscellany)

## HTML-элементы
Markdown является надмножеством HTML, поэтому любой HTML-файл является
корректным документом Markdown.

 ```markdown
<!-- Это позволяет использовать напрямую
любые элементы HTML-разметки, такие, например, как этот комментарий.
  Встроенные в документ HTML-элементы не затрагиваются парсером Markdown
и попадают в итоговый HTML без изменений. Однако следует понимать,
что эта же особенность не позволяет использовать разметку Markdown внутри
HTML-элементов -->

## Заголовки 

HTML-элементы от <h1> до <h6> размечаются очень просто:
текст, который должен стать заголовком, предваряется
соответствующим количеством символов "#":

```markdown
# Это заголовок h1
## Это заголовок h2
### Это заголовок h3
#### Это заголовок h4
##### Это заголовок h5
###### Это заголовок h6
```
Markdown позволяет размечать заголовки <h1> и <h2> ещё одним способом:

```markdown
Это заголовок h1
================

А это заголовок h2
------------------
```

## Простейшая стилизация текста

Текст легко сделать полужирным и/или курсивным:

```markdown
*Этот текст будет выведен курсивом.*
_Так же, как этот._

**А этот текст будет полужирным.**
__И этот тоже.__

***Полужирный курсив.***
**_И тут!_**
*__И даже здесь!__*
```

В Github Flavored Markdown, стандарте, который используется в Github,
текст также можно сделать зачёркнутым:

```markdown
~~Зачёркнутый текст.~~
```

## Абзацы

Абзацами являются любые строки, следующие друг за другом.
Разделяются же абзацы одной или несколькими пустыми строками:

```markdown
Это абзац. Я печатаю в абзаце, разве это не прикольно?

А тут уже абзац №2.
Эта строка всё ещё относится к абзацу №2!


О, а вот это уже абзац №3!
```

Для вставки принудительных переносов можно завершить абзац двумя дополнительными пробелами:

```markdown
Эта строка завершается двумя пробелами (выделите, чтобы увидеть!).  

Над этой строкой есть <br />!
```

Цитаты размечаются с помощью символа «>»:

```markdown
> Это цитата. В цитатах можно
> принудительно переносить строки, вставляя «>» в начало каждой следующей строки. А можно просто оставлять их достаточно длинными, и такие длинные строки будут перенесены автоматически.
> Разницы между этими двумя подходами к переносу строк нет, коль скоро
> каждая строка начинается с символа «>»

> А ещё цитаты могут быть многоуровневыми:
>> как здесь
>>> и здесь :)
> Неплохо?
```

## Списки
Маркированные списки размечаются вставкой в начало каждого элемента
одного из символов «*», «+» или «-»:
(символ должен быть одним и тем же для всех элементов)

```markdown
* Список,
* Размеченный
* Звёздочками

либо

+ Список,
+ Размеченный
+ Плюсами

либо

- Список,
- Размеченный
- Дефисами
```

В нумерованных списках каждая строка начинается
с числа и точки вслед за ним:

```markdown
1. Первый элемент
2. Второй элемент
3. Третий элемент
```

Заметьте, нумеровать элементы корректно необязательно. Достаточно указать
любое число в начале каждого элемента, и парсер пронумерует элементы сам!
Правда, злоупотреблять этим не стоит :)

```markdown
1. Первый элемент
1. Второй элемент
1. Третий элемент
```
(Этот список будет отображён так же, как и предыдущий!)

Списки могут быть вложенными:

```markdown
1. Введение
2. Начало работы
3. Примеры использования
    * Простые
    * Сложные
4. Заключение
```

Можно даже делать списки задач. Блок ниже создаёт HTML-флажки. 

```markdown
Для отметки флажка используйте «x»
- [ ] Первая задача
- [ ] Вторая задача
Этот флажок ниже будет отмечен
- [x] Задача была завершена
```

## Блоки кода

Фрагменты исходного кода (обычно отмечаемые тегом `<code>`) выделяются просто:
каждая строка блока должна иметь отступ в четыре пробела либо в один символ табуляции.

```markdown
    Это код,
    причём многострочный
```

Вы также можете делать дополнительные отступы, добавляя символы табуляции
или по четыре пробела:

```markdown
    my_array.each do |item|
        puts item
    end
```

Иногда бывает нужно вставить фрагмент кода прямо в строку текста,
не выделяя код в блок. Для этого фрагменты кода нужно обрамлять
символами «`»:

```markdown
Ваня даже не знал, что делает функция `go_to()`!
```

В Github Flavored Markdown для блоков кода можно использовать
специальный синтаксис:

<pre>
<code class="highlight">&#x60;&#x60;&#x60;ruby
def foobar
    puts "Привет, мир!"
end
&#x60;&#x60;&#x60;</code></pre>

Во фрагменте, приведённом выше, отступ не требуется.
Кроме того, Github подсветит синтаксис языка, указанного после \`\`\`

## Горизонтальный разделитель

Разделители (`<hr>`) добавляются вставкой строки из трёх и более
(одинаковых) символов «*» или «-», с пробелами или без них:

```markdown
***
---
- - -
****************
```

## Ссылки

Одной из сильных сторон Markdown можно смело считать то,
как просто размечаются гиперссылки. Для создания ссылки укажите
текст ссылки, заключив его в квадратные скобки,
и сразу после — URL-адрес, заключенный в круглые

```markdown
[Ссылка!](http://test.com/)
```
Также для ссылки можно указать всплывающую подсказку (`title`), используя
кавычки внутри круглых скобок:

```markdown
[Ссылка!](http://test.com/ "Ссылка на Test.com")
```
Относительные пути тоже возможны:

```markdown
[Перейти к музыке](/music/).
```

Markdown также позволяет размечать ссылку в виде сноски:

<pre><code class="highlight">&#x5b;<span class="nv">Щёлкните эту ссылку</span>][<span class="ss">link1</span>] для подробной информации!
&#x5b;<span class="nv">Также посмотрите эту ссылку,</span>][<span class="ss">foobar</span>] если хотите.

&#x5b;<span class="nv">link1</span>]: <span class="sx">http://test.com/</span> <span class="nn">"Круто!"</span>
&#x5b;<span class="nv">foobar</span>]: <span class="sx">http://foobar.biz/</span> <span class="nn">"Нормально!"</span></code></pre>

`Title` также может быть в одинарных кавычках или круглых скобках, а также
отсутствовать вовсе. Ссылки на сноски могут быть в любом месте документа,
а идентификаторы могут быть какими угодно, лишь бы они были уникальными.

Существует также неявное именование, когда ссылка является идентификатором.

<pre><code class="highlight">&#x5b;<span class="nv">Это</span>][] ссылка.

&#x5b;<span class="nv">это</span>]: <span class="sx">http://thisisalink.com/</span></code></pre>

Правда, эта возможность не очень распространена.

## Изображения
Разметка изображений очень похожа на разметку ссылок.
Нужно всего лишь добавить перед ссылкой восклицательный знак!

```markdown
![Альтернативный текст для изображения](http://imgur.com/myimage.jpg "Подсказка")
```
Изображения тоже могут быть оформлены, как сноски.

<pre><code class="highlight">!&#x5b;<span class="nv">Это альтернативный текст.</span>][<span class="ss">myimage</span>]

&#x5b;<span class="nv">myimage</span>]: <span class="sx">relative/urls/cool/image.jpg</span> <span class="nn">"Если нужна подсказка, её можно добавить"</span></code></pre>
## Разное
### Автоссылки

```markdown
Ссылка вида <http://testwebsite.com/> эквивалентна
[http://testwebsite.com/](http://testwebsite.com/)
```

### Автоссылки для адресов электронной почты

```markdown
<foo@bar.com>
```

### Экранирование символов

```markdown
Я хочу напечатать *текст, заключённый в звёздочки*, но я не хочу,
чтобы он был курсивным. Тогда я делаю так:
\*Текст, заключённый в звёздочки\*
```

### Клавиши на клавиатуре
В Github Flavored Markdown для представления клавиш на клавиатуре
вы можете использовать тег `<kbd>`.

```markdown
Ваш компьютер завис? Попробуйте нажать
<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>
```

### Таблицы
Таблицы официально поддерживаются только в GitHub Flavored Markdown,
да и синтаксис имеют не слишком удобный.
Но если очень нужно, размечайте таблицы так:

```markdown
| Столбец 1    | Столбец 2    | Столбец 3    |
| :----------- | :----------: | -----------: |
| Выравнивание | Выравнивание | Выравнивание |
| влево        | по центру    | вправо       |
```
Или более компактно

```markdown
Столбец 1|Столбец 2|Столбец 3
:--|:-:|--:
Выглядит|это|страшновато...
```

Ну вот и всё!

За более подробной информацией обращайтесь к [статье](http://daringfireball.net/projects/markdown/syntax) Джона Грубера о синтаксисе Markdown.

Также часто бывает полезной отличная ["шпаргалка"](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) по Markdown от Адама Притчарда.
---
language: Nim
filename: learnNim-ru.nim
contributors:
    - ["Jason J. Ayala P.", "http://JasonAyala.com"]
    - ["Dennis Felsing", "http://felsin9.de/nnis/"]
translators:
    - ["Nomadic", "https://github.com/n0madic"]
lang: ru-ru
---

Nim (ранее известный, как Nimrod) — язык программирования со статической
типизацией, поддерживающий процедурный, объектно-ориентированный,
функциональный и обобщённый стили программирования.

Nim эффективный, выразительный и элегантный.

```nim
var                     # Объявление (и присваивание) переменных,
  letter: char = 'n'    # с указанием типа или без
  lang = "N" & "im"
  nLength : int = len(lang)
  boat: float
  truth: bool = false

let            # Используйте let *сразу* для объявления и связывания переменных.
  legs = 400   # legs неизменяемый.
  arms = 2_000 # Символ _ игнорируется и удобен для длинных чисел.
  aboutPi = 3.15

const            # Константы вычисляются во время компиляции. Это обеспечивает
  debug = true   # производительность и полезно в выражениях этапа компиляции.
  compileBadCode = false

when compileBadCode:            # `when` это `if` этапа компиляции.
  legs = legs + 1               # Эта ошибка никогда не будет скомпилирована.
  const input = readline(stdin) # Значения констант должны быть известны во
                                # время компиляции.

discard 1 > 2 # Примечание. Компилятор будет жаловаться, если результат
              # выражения не используется. `discard` обходит это.

discard """
Это может использоваться как многострочный комментарий.
Или для не поддающегося синтаксическому анализу, сломанного кода
"""

#
# Структуры данных
#

# Кортежи

var
  child: tuple[name: string, age: int]   # Кортежи определяют *как* имя поля
  today: tuple[sun: string, temp: float] # так *и* порядок полей.

child = (name: "Rudiger", age: 2) # Присвоить все сразу литералом ()
today.sun = "Overcast"            # или отдельно по полям.
today.temp = 70.1

# Последовательности

var
  drinks: seq[string]

drinks = @["Water", "Juice", "Chocolate"] # @[V1,..,Vn] является литералом
                                          # последовательности

drinks.add("Milk")

if "Milk" in drinks:
  echo "We have Milk and ", drinks.len - 1, " other drinks"

let myDrink = drinks[2]

#
# Определение типов
#

# Определение собственных типов позволяет компилятору работать на вас.
# Это то, что делает статическую типизацию мощной и полезной.

type
  Name = string # Псевдоним типа дает вам новый тип, который равнозначен
  Age = int     # старому типу, но более нагляден.
  Person = tuple[name: Name, age: Age] # Определение структур данных.
  AnotherSyntax = tuple
    fieldOne: string
    secondField: int

var
  john: Person = (name: "John B.", age: 17)
  newage: int = 18 # Было бы лучше использовать Age, чем int

john.age = newage # Но это все же работает, потому что int и Age синонимы.

type
  Cash = distinct int    # `distinct` делает новый тип несовместимым с его
  Desc = distinct string # базовым типом.

var
  money: Cash = 100.Cash # `.Cash` преобразует int в наш тип
  description: Desc  = "Interesting".Desc

when compileBadCode:
  john.age  = money        # Error! age is of type int and money is Cash
  john.name = description  # Компилятор говорит: "Нельзя!"

#
# Дополнительные типы и структуры данных
#

# Перечисления позволяют типу иметь одно из ограниченного числа значений

type
  Color = enum cRed, cBlue, cGreen
  Direction = enum # Альтернативный формат
    dNorth
    dWest
    dEast
    dSouth
var
  orient = dNorth # `orient` имеет тип Direction, со значением `dNorth`
  pixel = cGreen # `pixel` имеет тип Color, со значением `cGreen`

discard dNorth > dEast # Перечисления обычно являются "порядковыми" типами

# Поддиапазоны определяют ограниченный допустимый диапазон

type
  DieFaces = range[1..20] # Допустимым значением являются только int от 1 до 20
var
  my_roll: DieFaces = 13

when compileBadCode:
  my_roll = 23 # Error!

# Arrays

type
  RollCounter = array[DieFaces, int]  # Массивы фиксированной длины и
  DirNames = array[Direction, string] # индексируются любым порядковым типом.
  Truths = array[42..44, bool]
var
  counter: RollCounter
  directions: DirNames
  possible: Truths

possible = [false, false, false] # Массивы создаются литералом [V1,..,Vn]
possible[42] = true

directions[dNorth] = "Ahh. The Great White North!"
directions[dWest] = "No, don't go there."

my_roll = 13
counter[my_roll] += 1
counter[my_roll] += 1

var anotherArray = ["Default index", "starts at", "0"]

# Доступны другие структуры данных, в том числе таблицы, множества,
# списки, очереди и crit-bit деревья.
# http://nim-lang.org/docs/lib.html#collections-and-algorithms (EN)

#
# IO и поток управления выполнением
#

# `case`, `readLine()`

echo "Read any good books lately?"
case readLine(stdin)
of "no", "No":
  echo "Go to your local library."
of "yes", "Yes":
  echo "Carry on, then."
else:
  echo "That's great; I assume."

# `while`, `if`, `continue`, `break`

import strutils as str # http://nim-lang.org/docs/strutils.html (EN)
echo "I'm thinking of a number between 41 and 43. Guess which!"
let number: int = 42
var
  raw_guess: string
  guess: int
while guess != number:
  raw_guess = readLine(stdin)
  if raw_guess == "": continue # Пропустить эту итерацию
  guess = str.parseInt(raw_guess)
  if guess == 1001:
    echo("AAAAAAGGG!")
    break
  elif guess > number:
    echo("Nope. Too high.")
  elif guess < number:
    echo(guess, " is too low")
  else:
    echo("Yeeeeeehaw!")

#
# Итерации (циклы)
#

for i, elem in ["Yes", "No", "Maybe so"]: # Или просто `for elem in`
  echo(elem, " is at index: ", i)

for k, v in items(@[(person: "You", power: 100), (person: "Me", power: 9000)]):
  echo v

let myString = """
an <example>
`string` to
play with
""" # Многострочная "сырая" строка

for line in splitLines(myString):
  echo(line)

for i, c in myString:       # Индекс и символ. Или `for j in` только для символов
  if i mod 2 == 0: continue # Компактная форма `if`
  elif c == 'X': break
  else: echo(c)

#
# Процедуры
#

type Answer = enum aYes, aNo

proc ask(question: string): Answer =
  echo(question, " (y/n)")
  while true:
    case readLine(stdin)
    of "y", "Y", "yes", "Yes":
      return Answer.aYes  # Перечисления могут быть квалифицированы
    of "n", "N", "no", "No":
      return Answer.aNo
    else: echo("Please be clear: yes or no")

proc addSugar(amount: int = 2) = # Значение поумолчанию 2, ничего не возвращает
  assert(amount > 0 and amount < 9000, "Crazy Sugar")
  for a in 1..amount:
    echo(a, " sugar...")

case ask("Would you like sugar in your tea?")
of aYes:
  addSugar(3)
of aNo:
  echo "Oh do take a little!"
  addSugar()
# Здесь нет необходимости в `else`. Возможны только `yes` и `no`.

#
# FFI (интерфейс внешних функций)
#

# Так как Nim компилируется в C, то FFI делается очень просто:

proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.}

let cmp = strcmp("C?", "Easy!")
```

Кроме того, Nim выделяется среди себе подобных метапрограммированием,
производительностью, функциями этапа компиляции.

## Дальнейшее чтение (EN)

* [Домашняя страница](http://nim-lang.org)
* [Скачать](http://nim-lang.org/download.html)
* [Сообщество](http://nim-lang.org/community.html)
* [FAQ](http://nim-lang.org/question.html)
* [Документация](http://nim-lang.org/documentation.html)
* [Руководство](http://nim-lang.org/docs/manual.html)
* [Стандартная библиотека](http://nim-lang.org/docs/lib.html)
* [Rosetta Code](http://rosettacode.org/wiki/Category:Nim)
---
language: Objective-C
filename: LearnObjectiveC-ru.m
contributors:
    - ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
    - ["Yannick Loriot", "https://github.com/YannickL"]
    - ["Levi Bostian", "https://github.com/levibostian"]
translators:
    - ["Evlogy Sutormin", "http://evlogii.com"]
    - ["Dmitry Bessonov", "https://github.com/TheDmitry"]
lang: ru-ru
---

Objective-C — основной язык программирования, используемый корпорацией Apple
для операционных систем OS X и iOS и их соответствующих фреймворках Cocoa и
Cocoa Touch.
Он является объектно-ориентированным языком программирования общего назначения,
который добавляет обмен сообщениями в Smalltalk-стиле к языку программирования C.

```objective-c
// Однострочные комментарии начинаются с //

/*
Так выглядят многострочные комментарии
*/

// Импорт заголовочных файлов фреймворка Foundation с помощью #import
// Используйте <>, чтобы импортировать глобальные файлы (обычно фреймворки)
// Используйте "", чтобы импортировать локальные файлы (из проекта)
#import <Foundation/Foundation.h>
#import "MyClass.h"

// Если вы включили модули для iOS >= 7.0 или OS X >= 10.9 проектов в
// Xcode 5, вы можете импортировать фреймворки подобным образом:
@import Foundation;

// Точка входа в программу - это функция main,
// которая возвращает целый тип
int main (int argc, const char * argv[])
{
    // Создание autorelease pool для управления памятью в программе
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // В место этого воспользуйтесь @autoreleasepool, если вы используете
    // автоматический подсчет ссылок (ARC)
    @autoreleasepool {
    
    // Используйте NSLog для печати в консоль
    NSLog(@"Привет Мир!"); // Напечатает строку "Привет Мир!"
 
    ///////////////////////////////////////
    // Типы и переменные
    ///////////////////////////////////////
    
    // Объявление простых типов
    int myPrimitive1  = 1;
    long myPrimitive2 = 234554664565;
    
    // Объявление объектов
    // Помещайте * в начало названия объекта для строго типизированного объявления
    MyClass *myObject1 = nil;  // Строгая типизация	
    id       myObject2 = nil;  // Слабая типизация 
    // %@ – это объект
    // 'description' - это общий для всех объектов метод вывода данных
    NSLog(@"%@ and %@", myObject1, [myObject2 description]); // напечатает "(null) and (null)"
    
    // Строка
    NSString *worldString = @"Мир";
    NSLog(@"Привет %@!", worldString); // напечатает "Привет Мир!"
    // NSMutableString - это изменяемая версия NSString-объекта
    NSMutableString *mutableString = [NSMutableString stringWithString:@"Привет"];
    [mutableString appendString:@" Мир!"];
    NSLog(@"%@", mutableString); // напечатает => "Привет Мир!"
    
    // Символьные литералы
    NSNumber *theLetterZNumber = @'Z';
    char theLetterZ            = [theLetterZNumber charValue]; // или 'Z'
    NSLog(@"%c", theLetterZ);

    // Целочисленные литералы
    NSNumber *fortyTwoNumber = @42;
    int fortyTwo             = [fortyTwoNumber intValue]; // или '42'
    NSLog(@"%i", fortyTwo);
    
    // Беззнаковый целочисленный литерал
    NSNumber *fortyTwoUnsignedNumber = @42U;
    unsigned int fortyTwoUnsigned    = [fortyTwoUnsignedNumber unsignedIntValue]; // или 42
    NSLog(@"%u", fortyTwoUnsigned);

    NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42];
    short fortyTwoShort           = [fortyTwoShortNumber shortValue]; // или 42
    NSLog(@"%hi", fortyTwoShort);
    
    NSNumber *fortyOneShortNumber   = [NSNumber numberWithShort:41];
    unsigned short fortyOneUnsigned = [fortyOneShortNumber unsignedShortValue]; // или 41
    NSLog(@"%u", fortyOneUnsigned);
    
    NSNumber *fortyTwoLongNumber = @42L;
    long fortyTwoLong            = [fortyTwoLongNumber longValue]; // или 42
    NSLog(@"%li", fortyTwoLong);
    
    NSNumber *fiftyThreeLongNumber   = @53L;
    unsigned long fiftyThreeUnsigned = [fiftyThreeLongNumber unsignedLongValue]; // или 53
    NSLog(@"%lu", fiftyThreeUnsigned);

    // Вещественный литерал
    NSNumber *piFloatNumber = @3.141592654F;
    float piFloat           = [piFloatNumber floatValue]; // или 3.141592654f
    NSLog(@"%f", piFloat); // напечатает 3.141592654
    NSLog(@"%5.2f", piFloat); // напечатает " 3.14"
    
    NSNumber *piDoubleNumber = @3.1415926535;
    double piDouble                 = [piDoubleNumber doubleValue]; // или 3.1415926535
    NSLog(@"%f", piDouble);
    NSLog(@"%4.2f", piDouble); // напечатает "3.14"

    // NSDecimalNumber - это класс с фиксированной точкой, который является
    // более точным, чем float или double
    NSDecimalNumber *oneDecNum = [NSDecimalNumber decimalNumberWithString:@"10.99"];
    NSDecimalNumber *twoDecNum = [NSDecimalNumber decimalNumberWithString:@"5.002"];
    // NSDecimalNumber не способен использовать стандартные +, -, *, / операторы,
    // поэтому он предоставляет свои собственные:
    [oneDecNum decimalNumberByAdding:twoDecNum]; 
    [oneDecNum decimalNumberBySubtracting:twoDecNum];
    [oneDecNum decimalNumberByMultiplyingBy:twoDecNum];
    [oneDecNum decimalNumberByDividingBy:twoDecNum];
    NSLog(@"%@", oneDecNum); // напечатает "10.99", т.к. NSDecimalNumber - изменяемый
    
    // BOOL (булевый) литерал
    NSNumber *yesNumber = @YES;
    NSNumber *noNumber  = @NO;
    // или
    BOOL yesBool = YES;
    BOOL noBool  = NO;
    NSLog(@"%i", yesBool); // напечатает 1
    
    // Массив
    // Может содержать различные типы данных, но должен быть объектом Objective-C
    NSArray *anArray      = @[@1, @2, @3, @4];
    NSNumber *thirdNumber = anArray[2];
    NSLog(@"Третье число = %@", thirdNumber); // Напечатает "Третье число = 3"
    // NSMutableArray - это изменяемая версия NSArray, допускающая вам изменять
    // элементы в массиве и расширять или сокращать массив.
    // Удобный, но не эффективный как NSArray.
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:2];
    [mutableArray addObject:@"Привет"];
    [mutableArray addObject:@"Мир"];
    [mutableArray removeObjectAtIndex:0];
    NSLog(@"%@", [mutableArray objectAtIndex:0]); // напечатает "Мир"

    // Словарь
    NSDictionary *aDictionary = @{ @"ключ1" : @"значение1", @"ключ2" : @"значение2" };
    NSObject *valueObject     = aDictionary[@"Ключ"];
    NSLog(@"Объект = %@", valueObject); // Напечатает "Объект = (null)"
    // NSMutableDictionary тоже доступен, как изменяемый словарь
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:2];
    [mutableDictionary setObject:@"значение1" forKey:@"ключ1"];
    [mutableDictionary setObject:@"значение2" forKey:@"ключ2"];
    [mutableDictionary removeObjectForKey:@"ключ1"];

    // Множество
    NSSet *set = [NSSet setWithObjects:@"Привет", @"Привет", @"Мир", nil];
    NSLog(@"%@", set); // напечатает {(Hello, World)} (порядок может отличаться)
    // NSMutableSet тоже доступен, как изменяемое множество
    NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:2];
    [mutableSet addObject:@"Привет"];
    [mutableSet addObject:@"Привет"];
    NSLog(@"%@", mutableSet); // напечатает => {(Привет)}
    
    ///////////////////////////////////////
    // Операторы
    ///////////////////////////////////////
    
    // Операторы работают также как в Си.
    // Например:
    2 + 5; // => 7
    4.2f + 5.1f; // => 9.3f
    3 == 2; // => 0 (НЕТ)
    3 != 2; // => 1 (ДА)
    1 && 1; // => 1 (логическое И)
    0 || 1; // => 1 (логическое ИЛИ)
    ~0x0F; // => 0xF0 (побитовое отрицание)
    0x0F & 0xF0; // => 0x00 (побитовое И)
    0x01 << 1; // => 0x02 (побитовый сдвиг влево (на 1))

    ///////////////////////////////////////
    // Структуры ветвления
    ///////////////////////////////////////

    // Условный оператор
    if (NO)
    {
        NSLog(@"Я никогда не выполнюсь");
    } else if (0)
    {
        NSLog(@"Я тоже никогда не выполнюсь");
    } else
    {
        NSLog(@"Я напечатаюсь");
    }

    // Ветвление с множественным выбором
    switch (2)
    {
        case 0:
        {
            NSLog(@"Я никогда не выполнюсь");
        } break;
        case 1:
        {
            NSLog(@"Я тоже никогда не выполнюсь");
        } break;
        default:
        {
            NSLog(@"Я напечатаюсь");
        } break;
    }
    
    // Цикл с предусловием
    int ii = 0;
    while (ii < 4)
    {
        NSLog(@"%d,", ii++); // ii++ инкрементирует ii после передачи значения
    } // => напечатает "0," 
      //               "1,"
      //               "2,"
      //               "3,"

    // Цикл со счётчиком
    int jj;
    for (jj=0; jj < 4; jj++)
    {
        NSLog(@"%d,", jj);
    } // => напечатает "0," 
      //               "1,"
      //               "2,"
      //               "3,"
     
    // Цикл просмотра           
    NSArray *values = @[@0, @1, @2, @3];
    for (NSNumber *value in values)
    {
        NSLog(@"%@,", value);
    } // => напечатает "0," 
      //               "1,"
      //               "2,"
      //               "3,"

    // Цикл for для объектов. Может использоваться с любым объектом Objective-C
    for (id item in values) { 
        NSLog(@"%@,", item); 
    } // напечатает => "0," 
      //           "1,"
      //           "2,"
      //           "3,"      
      
    // Обработка исключений
    @try
    {
        // Ваше исключение здесь
        @throw [NSException exceptionWithName:@"FileNotFoundException"
                            reason:@"Файл не найден в системе" userInfo:nil];
    } @catch (NSException * e)
    {
        NSLog(@"Исключение: %@", e);
    } @finally
    {
        NSLog(@"В конце отводится время для очистки.");
    } // => напечатает "Исключение: Файл не найден в системе"
      //               "В конце отводится время для очистки."
 
    // NSError - это полезные объекты для аргументов функции, чтобы заполнить их
    // пользовательскими ошибками.
    NSError *error = [NSError errorWithDomain:@"Неправильный эл. адрес." code:4 userInfo:nil];
 
    ///////////////////////////////////////
    // Объекты
    ///////////////////////////////////////
    
    // Создание объектов через выделение памяти и инициализацию.
    // Объект не является полнофункциональным пока обе части не выполнятся.
    MyClass *myObject = [[MyClass alloc] init];
        
    // В Objective-C модель ООП базируется на передаче сообщений.
    // В Objective-C Вы не просто вызваете метод; вы посылаете сообщение.
    [myObject instanceMethodWithParameter:@"Стив Джобс"];

    // Очищайте память, перед завершением работы программы.
    [pool drain];
    
    // Конец @autoreleasepool
    }
    
    // Конец программы.
    return 0;
}

///////////////////////////////////////
// Классы и функции
///////////////////////////////////////

// Объявляйте свой класс в файле МойКласс.h
// Синтаксис объявления:
// @interface ИмяКласса : ИмяКлассаРодителя <ИмплементируемыеПротоколы>
// {
//    тип имя; <= Объявление переменных;
// }
// @property тип имя; <= объявление свойств
// -/+ (тип) Объявление метода(ов).
// @end
@interface MyClass : NSObject <MyProtocol> // NSObject - это базовый класс в Objective-C.
{
    // Объявления экземпляров переменных (может существовать в файлах интерфейса или реализвации)
    int count; // По умолчанию защищенный доступ.
    @private id data; // Приватный доступ (Намного удобнее объявлять в файле реализации)
    NSString *name;
}
// Удобное обозначение для переменных с открытым (public) доступом
// автоматически генерируется сеттер-метод
// По умолчанию название сеттер-метода начинается с 'set' с последующим именем
// переменной из @property
@property int propInt; // Имя сеттер-метода = 'setPropInt'
@property (copy) id copyId; // (copy) => Скопировать объект в ходе присвоения.
// (readonly) => Не позволяет установить значение вне @interface
@property (readonly) NSString *roString; // Используйте @synthesize 
                               // в @implementation, чтобы создать аксессор
// Вы можете настроить геттер и сеттер имена вместо используемого 'set'-имени по умолчанию:
@property (getter=lengthGet, setter=lengthSet:) int length;

// Методы
+/- (возвращаемый тип)сигнатураМетода:(Параметр типа *)имяПараметра;

// + для методов класса
+ (NSString *)classMethod;
+ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight;

// - для методов объекта
- (NSString *)instanceMethodWithParameter:(NSString *)string;
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;

// Методы-конструктор с аргументом:
- (id)initWithDistance:(int)defaultDistance;
// В Objective-C имена методов очень описательные. Всегда имена методов соответствуют своим аргументам

@end // Устанавливает конец интерфейса (interface)


// Чтобы обратиться к открытым (public) переменным из файла реализации, @property генерирует сеттер-метод
// автоматически. Название метода - это 'set' с последующим именем переменной из @property:
MyClass *myClass = [[MyClass alloc] init]; // создает экземпляр объекта класса MyClass
[myClass setCount:10]; 
NSLog(@"%d", [myClass count]); // напечатает => 10
// Или используйте свой геттер и сеттер методы, которые определены в @interface:
[myClass lengthSet:32];
NSLog(@"%i", [myClass lengthGet]); // напечатает => 32
// Для удобства вы можете использовать точечную нотацию,
// чтобы установить и получить доступ к переменным объекта:
myClass.count = 45;
NSLog(@"%i", myClass.count); // напечатает => 45

// Вызов методов класса:
NSString *classMethodString = [MyClass classMethod];
MyClass *classFromName = [MyClass myClassFromName:@"Привет"];

// Вызов методов экземпляра:
MyClass *myClass = [[MyClass alloc] init]; // Создает экземпляр объекта MyClass
NSString *stringFromInstanceMethod = [myClass instanceMethodWithParameter:@"Привет"];

// Селекторы
// Это способ динамически представить методы. Используйте для вызова методов класса, передайте методы
// через функции, чтобы сказать другим классам, что они должны вызвать их и сохранить методы
// как переменные
// SEL - это тип данных. @selector() вернет селектор из предоставленного имени метода
// methodAParameterAsString:andAParameterAsNumber: - это название метода в MyClass
SEL selectorVar = @selector(methodAParameterAsString:andAParameterAsNumber:); 
if ([myClass respondsToSelector:selectorVar]) { // Проверяет содержит ли класс метод
    // Необходимо установить все аргументы метода в один объект, что отправить его в performSelector-функцию
    NSArray *arguments = [NSArray arrayWithObjects:@"Привет", @4, nil];
    [myClass performSelector:selectorVar withObject:arguments]; // Вызывает метод
} else {
    // NSStringFromSelector() вернет NSString название метода полученного селектором
    NSLog(@"MyClass не содержит метод: %@", NSStringFromSelector(selectedVar));
}

// Имплементируйте методы в файле MyClass.m:
@implementation MyClass {
    long distance; // Переменная экземпляра с закрытым (private) доступом
    NSNumber height;
}

// Для доступа к public переменной, объявленной в интерфейсе, используйте '_' перед названием переменной:
_count = 5; // Ссылается на "int count" из интерфейса MyClass
// Получение доступа к переменной, объявленной в реализации происходит следующим образом:
distance = 18; // Ссылается на "long distance" из реализации MyClass
// Для использования в иплементации переменной, объявленной в интерфейсе с помощью @property,
// следует использовать @synthesize для создания переменной аксессора:
@synthesize roString = _roString; // Теперь _roString доступна в @implementation (реализации интерфейса)

// Вызывается в первую очередь, перед вызовом других медотов класса или инициализации других объектов
+ (void)initialize 
{
    if (self == [MyClass class]) {
        distance = 0;
    }
}

// Вызывается при высвобождении памяти под объектом
- (void)dealloc
{
    [height release]; // Если не используется ARC, убедитесь в освобождении переменных объекта класса
    [super dealloc];  // and call parent class dealloc
}

// Конструкторы – это способ создания объектов класса.
// Это конструктор по умолчанию, который вызывается, когда объект инициализируется.
- (id)init
{
    if ((self = [super init])) // 'super' используется для того, чтобы обратиться к методам родительского класса
    {
        self.count = 1; // 'self' используется для вызова самого себя
    }
    return self;
}
// Можно создать конструкторы, которые содержат аргументы:
- (id)initWithDistance:(int)defaultDistance 
{
    distance = defaultDistance;
    return self;
}

+ (NSString *)classMethod
{
    return [[self alloc] init];
}

+ (MyClass *)myClassFromHeight:(NSNumber *)defaultHeight 
{
    height = defaultHeight;
    return [[self alloc] init];
}

- (NSString *)instanceMethodWithParameter:(NSString *)string
{
    return @"Новая строка";
}

- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number
{
    return @42;
}

// Objective-C не содержит объявление приватных методов, но вы можете имитировать их.
// Чтобы сымитировать приватный метод, создайте метод в @implementation, но не в @interface.
- (NSNumber *)secretPrivateMethod {
    return @72;
}
[self secretPrivateMethod]; // Вызывает приватный метод

// Методы объявленные в МyProtocol (см. далее)
- (void)myProtocolMethod
{
    // операторы
}

@end // Устанавливает конец реализации (implementation)

///////////////////////////////////////
// Категории
///////////////////////////////////////
// Категория - это группа методов предназначенные для того, чтобы расширить класс. Они позволяют вам добавить новые методы
// к существующему классу для организационных целей. Это не стоит путать с подклассами.
// Подклассы предназначены для ИЗМЕНЕНИЯ функциональности объекта пока как категории ДОБАВЛЯЮТ
// функциональность в объект.
// Категории позволяют вам:
// -- Добавлять методы в существующий класс для организационных целей.
// -- Допускает вам расширять объекты Objective-C классов (напр.: NSString) добавить ваши собственные методы.
// -- Добавляет возможность создать защищенные и закрытые методы классов. 
// ПРИМЕЧАНИЕ: Не переопределяйте методы базового класса в категории даже если у вас есть возможность это сделать
// to. Переопределение методов может привести к ошибкам компиляции позднее между различными категориями и это 
// нарушает цель категорий, чтобы добавлять только функциональность. Вместо этого подклассы переопределяют методы.

// Здесь простой базовый класс Car.
@interface Car : NSObject

@property NSString *make;
@property NSString *color;

- (void)turnOn;
- (void)accelerate;

@end

// И реализация базового класса Car:
#import "Car.h"

@implementation Car

@synthesize make = _make;
@synthesize color = _color;

- (void)turnOn {
    NSLog(@"Машина заведена.");
}
- (void)accelerate {
    NSLog(@"Ускорение.");
}

@end

// Теперь, если мы хотим создать объект Truck - грузовик, мы должны создать подкласс класса Car, что
// изменит функционал Car и позволит вести себя подобно грузовику. Но что, если мы хотим только добавить
// определенный функционал в уже существующий класс Car? Например - чистка автомобиля. Мы просто создадим
// категорию, которая добавит несколько методов для чистки автомобиля в класс Car:
// @interface ИмяФайла: Car+Clean.h (ИмяБазовогоКласса+ИмяКатегории.h)
#import "Car.h" // Убедитесь в том, что базовый класс импортирован для расширения.

@interface Car (Clean) // Имя категории внутри (), следующие после имени базового класса.

- (void)washWindows; // Названия новых методов, которые мы добавляем в наш объект Car.
- (void)wax;

@end

// @implementation имя файла: Car+Clean.m (ИмяБазовогоКласса+ИмяКатегории.m)
#import "Car+Clean.h" // Импортируйте Очистку файл @interface категории.

@implementation Car (Clean)

- (void)washWindows {
    NSLog(@"Окна промыли.");
}
- (void)wax {
    NSLog(@"Воском натерли.");
}

@end 

// Любой экземпляр объекта Car имеет возможность воспользоваться категорией. Все, что нужно сделать, это импортировать ее:
#import "Car+Clean.h" // Импортировать как множество различных категорий, как вы хотите использовать.
#import "Car.h" // Кроме того, необходимо импортировать базовый класс для использования его оригинальные функциональные возможности.

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        Car *mustang = [[Car alloc] init];
        mustang.color = @"Красный";
        mustang.make = @"Форд";

        [mustang turnOn]; // Используйте методы из базового класса Car.
        [mustang washWindows]; // Используйте методы категории Clean из класса Car.
    }
    return 0; 
}

// Objective-C не поддерживает объявление защищенных методов, но вы можете имитировать их.
// Создайте категорию, содержащую все защищенные методы, затем импортируйте ее только в
// @implementation-файле класса, относящегося к классу Car:
@interface Car (Protected) // Наименование категории с помощью 'Protected'
// дает знать, что методы защищенные.

- (void)lockCar; // Здесь перечисляются методы, которые должны быть созданы
// только с помощью объектов класса Car.

@end
// Чтобы воспользоваться защищенными методами, импортируйте категорию, затем реализуйте методы:
#import "Car+Protected.h" // Запомните, делайте импорт только в файле с @implementation.

@implementation Car 

- (void)lockCar {
    NSLog(@"Машина закрыта."); // Экземпляры класса Car не могут использовать
// метод lockCar, потому что он объявлен не в @interface.
}

@end

///////////////////////////////////////
// Расширения
///////////////////////////////////////
// Расширения позволяют вам переопределять атрибуты свойств и методов
// с открытым доступом в @interface.
// @interface имя файла: Shape.h
@interface Shape : NSObject // Расширение базового класса Shape переопределяет
                                        // свои поля ниже.

@property (readonly) NSNumber *numOfSides;

- (int)getNumOfSides;

@end
// Вы можете переопределить numOfSides-переменную или getNumOfSides-метод
// Внесение изменений с помощью расширения делается следующим образом:
// @implementation имя файла: Shape.m
#import "Shape.h"
// Расширения "живут" в том же файле, где и @implementation класса.
@interface Shape () // После имени базового класса скобки () объявляют расширение.

@property (copy) NSNumber *numOfSides; // Делает numOfSides-свойство
             // копирующим (copy) вместо свойства только для чтения (readonly).
-(NSNumber)getNumOfSides; // Изменяет метод getNumOfSides так,
                         // чтобы он возвращал объект NSNumber вместо типа int.
-(void)privateMethod; // Вы также можете создать новый закрытый метод
                              // внутри расширения.

@end
// Главный @implementation:
@implementation Shape

@synthesize numOfSides = _numOfSides;

-(NSNumber)getNumOfSides { // Все операторы внутри расширения
                                         // должны быть в @implementation.
    return _numOfSides;
}
-(void)privateMethod {
    NSLog(@"Закрытый метод созданный с помощью расширения.");
    NSLog(@"Экземпляр Shape не может вызвать этот метод.");
}

@end

///////////////////////////////////////
// Протоколы
///////////////////////////////////////
// Протокол объявляет методы, которые могут быть реализованы с помощью
// любого класса. Протоколы сами по себе не являются классами. Они просто
// определяют интерфейс, который должен быть реализован другими объектами.
// @protocol имя файла: "CarUtilities.h"
@protocol CarUtilities <NSObject> // <NSObject> => Имя другого протокола,
// который включен в этот протокол.
    @property BOOL engineOn; // Адаптирующий класс должен определить
// все @synthesize для @property и
    - (void)turnOnEngine; // определить все методы.
@end
// Ниже пример класса, реализующий протокол.
#import "CarUtilities.h" // Импорт файла с @protocol.

@interface Car : NSObject <CarUtilities> // Внутри <> имя протокола
// Здесь вам не нужно указывать @property или имена методов для CarUtilities.
// Они нужны только для @implementation.
- (void)turnOnEngineWithUtilities:(id <CarUtilities>)car; // Вы также можете
// указать тип протоколов.
@end
// В @implementation нужно реализовать все @property и методы для протокола.
@implementation Car : NSObject <CarUtilities>

@synthesize engineOn = _engineOn; // Создайте @synthesize-оператор
// для "@property engineOn".

- (void)turnOnEngine { // Реализуйте turnOnEngine как вам угодно. Протоколы
// не определят,
    _engineOn = YES; // как вам реализовать метод, он только требует,
// чтобы вы реализовали его.
}
// Вы можете использовать протокол как данные, если вы знаете, что он реализует
// методы и переменные.
- (void)turnOnEngineWithCarUtilities:(id <CarUtilities>)objectOfSomeKind { 
    [objectOfSomeKind engineOn]; // У вас есть доступ к переменным объекта
    [objectOfSomeKind turnOnEngine]; // и методам.
    [objectOfSomeKind engineOn]; // Может или не может быть значение YES. Класс
// реализует как нужно.
}

@end
// Экземпляры класса Car сейчас имеют доступ к протоколу.
Car *carInstance = [[Car alloc] init];
[carInstance setEngineOn:NO];
[carInstance turnOnEngine];
if ([carInstance engineOn]) {
    NSLog(@"Двигатель запущен."); // напечатает => "Двигатель запущен."
}
// Убедитись в том, что объект типа 'id' реализует протокол перед вызовом методов протокола:
if ([myClass conformsToProtocol:@protocol(CarUtilities)]) {
    NSLog(@"Не работает, т.к. класс MyClass не реализует протокол CarUtilities.");
} else if ([carInstance conformsToProtocol:@protocol(CarUtilities)]) {
    NSLog(@"Работает как класс Car, который реализует протокол CarUtilities.");
}
// Категории тоже могут реализовать протоколы:
// @interface Car (CarCategory) <CarUtilities>
// Вы можете реализовать много протоколов:
// @interface Car : NSObject <CarUtilities, CarCleaning>
// ЗАМЕЧАНИЕ: Если два или более протоколов полагаются друг на друга,
// убедитесь, что они ранее объявлены:
#import "Brother.h"

@protocol Brother; // Оператор раннего объявления. Без него компилятор
// выдаст ошибку.

@protocol Sister <NSObject>

- (void)beNiceToBrother:(id <Brother>)brother;

@end

// Рассмотрите проблему, где протокол Sister полагается на протокол Brother,
// а Brother полагается на Sister.
#import "Sister.h"

@protocol Sister; // Эти строки предотвращают рекурсию, решая этим проблему.

@protocol Brother <NSObject>
 
- (void)beNiceToSister:(id <Sister>)sister;

@end


///////////////////////////////////////
// Блоки
///////////////////////////////////////
// Блоки - это операторы кода, наподобие функции, которую возможно использовать
// как данные.
// Ниже простой блок с целочисленным аргументом, и возвращает аргумент плюс 4.
int (^addUp)(int n); // Объявите переменную, чтобы сохранить блок.
void (^noParameterBlockVar)(void); // Пример объявления блока-переменной
// без аргументов.
// Блоки имею доступ к переменным в той же области видимости. Но переменные
// будут только для чтения, и значения переданных в блок станут значением
// переменной, когда блок создастся.
int outsideVar = 17; // Если мы редактируем outsideVar после объявления addUp,
// outsideVar остается равным 17.
__block long mutableVar = 3; // __block делают переменные перезаписываемыми
// в блоках, в отличие от outsideVar.
addUp = ^(int n) { // Удалите (int n) в блоке, чтобы не принимать
// какие-либо параметры.
    NSLog(@"Вы можете иметь столько строк в блоке, сколько вы хотели.");
    NSSet *blockSet; // Также вы можете объявить локальные переменные.
    mutableVar = 32; // Присвоить новое значение к __block-переменной.
    return n + outsideVar; // Необязательный оператор возврата.
}
int addUp = add(10 + 16); // Вызывает блок кода с аргументами.
// Блоки часто используются как аргументы функции, чтобы позже их вызвать, или
// как функции обратного вызова (callbacks).
@implementation BlockExample : NSObject 
 
- (void)runBlock:(void (^)(NSString))block {
    NSLog(@"В аргументе блок ничего не возвращает и принимает NSString-объект.");
    block(@"Аргумент передан блоку на исполнение."); // Вызов блока.
}

@end
 
 
///////////////////////////////////////
// Управление памятью
///////////////////////////////////////
/* 
Для каждого объекта, используемого в приложении, должна быть выделена память
для таких объектов. Когда приложение прекращает использование объекта, память
должна быть освобождена, чтобы гарантировать эффективность приложения.
Objective-C не использует сборщик мусора, а вместо этого применяет подсчет ссылок.
Пока существует по крайней мере одна ссылка на объект (также называется
"владение" объектом), то объект будет доступен к использованию (еще известно
как "право владения").

Когда экземпляр владеет объектом, его ссылка увеличивается на один. Когда
объекта освобождается, счетчик ссылки уменьшается на один. Когда счетчик ссылки
равен нулю, объект удаляется из памяти.

Над всеми объектами взаимодействуют, следуя паттерну:
(1) создание объекта, (2) использование объекта, (3) затем освобождение объекта из памяти.
*/

MyClass *classVar = [MyClass alloc]; // 'alloc' устанавливает счетчик ссылки
// объекта classVar на 1 и возвращает указатель на объект.
[classVar release]; // Уменьшает счетчик ссылки объекта classVar
// 'retain' заявляет право собственности на существующий экземпляр объекта
// и увеличивает счетчик ссылки. Затем вернет указатель на объект.
MyClass *newVar = [classVar retain]; // Если classVar освободится, объект
// останется в памяти, потому что newVar - владелец
[classVar autorelease]; // Удалит право на владение объектом
// в конце @autoreleasepool блока. Вернет указатель на объект.

// @property может использовать 'retain' и 'assign' тоже для маленького
// удобного определения
@property (retain) MyClass *instance; // Освободит старое значение и сохранит
// одно новое (строгая ссылка)
@property (assign) NSSet *set; // Укажет на новое значение
// без сохранения/освобождения старого значения (слабая ссылка)

// Автоматический подсчет ссылок (ARC)
// Управление памятью может быть трудным, поэтому в Xcode 4.2 и iOS 4 введен
// автоматический подсчет ссылок (ARC).
// ARC - это особенность компилятора, который помещает "retain", "release"
// и "autorelease" автоматически за вас тогда, когда используется ARC,
// вам не нужно больше обращаться к "retain", "release" или "autorelease"
MyClass *arcMyClass = [[MyClass alloc] init];
// ... код, использующий объект arcMyClass
// Без ARC, вам нужно было бы вызвать: [arcMyClass release] после того, как вы
// завершите работу с объектом arcMyClass. Но с ARC,
// теперь этого не нужно делать. Он будет помещать release-вызов за вас

// Что касается 'assign' и 'retain' @property атрибутов, в ARC вы должны
// использовать 'weak' и 'strong'
@property (weak) MyClass *weakVar; // 'weak' не принимает право на владение
// объектом. Если исходный счетчик ссылки экземпляра обнуляется,
// weakVar-свойство автоматически примет значение nil,
// во избежание падения приложения
@property (strong) MyClass *strongVar; // 'strong' принимает право на владение
// объектом. Гарантирует, что объект останется в памяти для использования

// Для обычных переменных (не объявленных с помощью @property), используйте
// следующий способ:
__strong NSString *strongString; // По умолчанию. Переменная сохраняется в памяти,
// пока она не покинет область видимости
__weak NSSet *weakSet; // Слабая ссылка на существующий объект. Когда существующий
// объект освобождается, weakSet принимает nil
__unsafe_unretained NSArray *unsafeArray; // Похож на __weak, но unsafeArray
// не принимает nil, когда существующий объект освобождается

```
## На почитать

[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C)

[Learning Objective-C](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/)

[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started)

[iOS разработчик: Обзор книг для новичка](http://habrahabr.ru/post/166213/)

[Хочешь быть iOS разработчиком? Будь им!](http://www.pvsm.ru/ios/12662/print/)
---
language: Paren
filename: learnparen-ru.paren
contributors:
  - ["KIM Taegyoon", "https://github.com/kimtg"]
translators:
  - ["Dmitry Bessonov", "https://github.com/TheDmitry"]
lang: ru-ru
---

[Paren](https://bitbucket.org/ktg/paren) - это диалект языка Лисп. Он спроектирован как встроенный язык.

Примеры взяты <http://learnxinyminutes.com/docs/racket/>.

```scheme
;;; Комментарии
# комментарии

;; Однострочные комментарии начинаются с точки с запятой или символа решетки

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Примитивные типы данных и операторы
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Числа
123 ; int
3.14 ; double
6.02e+23 ; double
(int 3.14) ; => 3 : int
(double 123) ; => 123 : double

;; Обращение к функции записывается так: (f x y z ...),
;; где f - функция, а x, y, z, ... - операнды
;; Если вы хотите создать буквальный список данных, используйте (quote), чтобы
;; предотвратить ненужные вычисления
(quote (+ 1 2)) ; => (+ 1 2)
;; Итак, некоторые арифметические операции
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(^ 2 3) ; => 8
(/ 5 2) ; => 2
(% 5 2) ; => 1
(/ 5.0 2) ; => 2.5

;;; Логический тип
true ; обозначает истину
false ; обозначает ложь
(! true) ; => false
(&& true false (prn "досюда не доходим")) ; => false
(|| false true (prn "досюда не доходим")) ; => true

;;; Символы - это числа (int).
(char-at "A" 0) ; => 65
(chr 65) ; => "A"

;;; Строки - это массив символов с фиксированной длиной.
"Привет, мир!"
"Benjamin \"Bugsy\" Siegel"   ; обратная косая черта экранирует символ
"Foo\tbar\r\n" ; включает управляющие символы в стиле Cи: \t \r \n

;; Строки тоже могут объединяться!
(strcat "Привет " "мир!") ; => "Привет мир!"

;; Строка может трактоваться подобно списку символов
(char-at "Apple" 0) ; => 65

;; Выводить информацию достаточно легко
(pr "Я" "Paren. ") (prn "Приятно познакомиться!")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. Переменные
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Вы можете создать или инициализировать переменную, используя (set)
;; имя переменной может содержать любой символ, исключая: ();#"
(set some-var 5) ; => 5
some-var ; => 5

;; Обращение к переменной, прежде не определенной, вызовет исключение
; x ; => Неизвестная переменная: x : nil

;; Локальное связывание: Используйте лямбда-вычисление! `a' и `b' связывается
;; с `1' и `2' только в пределах (fn ...)
((fn (a b) (+ a b)) 1 2) ; => 3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Коллекции
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Списки

;; Списки подобны динамическому массиву (vector). (произвольный доступ равен O(1).)
(cons 1 (cons 2 (cons 3 (list)))) ; => (1 2 3)
;; `list' - это удобный конструктор списков с переменным числом элементов
(list 1 2 3) ; => (1 2 3)
;; и quote может также использоваться для литеральных значений списка
(quote (+ 1 2)) ; => (+ 1 2)

;; Можно еще использовать `cons', чтобы добавить элемент в начало списка
(cons 0 (list 1 2 3)) ; => (0 1 2 3)

;; Списки являются основным типом, поэтому для них предусмотрено *много* функций
;; немного примеров из них:
(map inc (list 1 2 3))          ; => (2 3 4)
(filter (fn (x) (== 0 (% x 2))) (list 1 2 3 4))    ; => (2 4)
(length (list 1 2 3 4))     ; => 4

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Функции
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Используйте `fn' для создания функций.
;; Функция всегда возвращает значение своего последнего выражения
(fn () "Привет Мир") ; => (fn () Привет Мир) : fn

;; Используйте скобки, чтобы вызвать все функции, в том числе лямбда-выражение
((fn () "Привет Мир")) ; => "Привет Мир"

;; Назначить функцию переменной
(set hello-world (fn () "Привет Мир"))
(hello-world) ; => "Привет Мир"

;; Вы можете сократить это, используя синтаксический сахар определения функции:
(defn hello-world2 () "Привет Мир")

;; Как и выше, () - это список аргументов для функции
(set hello
  (fn (name)
    (strcat "Привет " name)))
(hello "Стив") ; => "Привет Стив"

;; ... или, что эквивалентно, используйте синтаксический сахар определения:
(defn hello2 (name)
  (strcat "Привет " name))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Равенство
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; для чисел используйте `=='
(== 3 3.0) ; => true
(== 2 1) ; => false

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. Поток управления
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Условный оператор

(if true               ; проверка выражения
    "это - истина"   ; тогда это выражение
    "это - ложь") ; иначе другое выражение
; => "это - истина"

;;; Циклы

;; Цикл for для чисел
;; (for ИДЕНТИФИКАТОР НАЧАЛО КОНЕЦ ШАГ ВЫРАЖЕНИЕ ..)
(for i 0 10 2 (pr i "")) ; => печатает 0 2 4 6 8 10
(for i 0.0 10 2.5 (pr i "")) ; => печатает 0 2.5 5 7.5 10

;; Цикл while
((fn (i)
  (while (< i 10)
    (pr i)
    (++ i))) 0) ; => печатает 0123456789

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. Изменение
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Используйте `set', чтобы назначить новое значение переменной или памяти
(set n 5) ; => 5
(set n (inc n)) ; => 6
n ; => 6
(set a (list 1 2)) ; => (1 2)
(set (nth 0 a) 3) ; => 3
a ; => (3 2)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. Макросы
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Макросы позволяют вам расширять синтаксис языка.
;; Paren-макросы легкие.
;; Фактически, (defn) - это макрос.
(defmacro setfn (name ...) (set name (fn ...)))
(defmacro defn (name ...) (def name (fn ...)))

;; Давайте добавим инфиксную нотацию
(defmacro infix (a op ...) (op a ...))
(infix 1 + 2 (infix 3 * 4)) ; => 15

;; Макросы приводят к неясному коду, т.е. вы можете затереть существующие переменные!
;; Они являются кодопреобразующей конструкцией.
```
---
category: language
language: perl
filename: learnperl-ru.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
translators:
    - ["Elena Bolshakova", "http://github.com/liruoko"]
lang: ru-ru
---

Perl 5 -- высокоуровневый мощный язык с 25-летней историей. 
Особенно хорош для обработки разнообразных текстовых данных. 

Perl 5 работает более чем на 100 платформах, от портативных устройств
до мейнфреймов, и подходит как для быстрого прототипирования, 
так и для крупных проектов. 

```perl
# Комментарии начинаются с символа решетки.


#### Типы переменных в Perl

#  Скалярные переменные начинаются с знака доллара $.
#  Имя переменной состоит из букв, цифр и знаков подчеркивания, 
#  начиная с буквы или подчеркивания. 

### В Perl три основных типа переменных: скаляры, массивы, хеши.

## Скаляры
#  Скаляр хранит отдельное значение:
my $animal = "camel";
my $answer = 42;

# Скаляры могут быть строками, целыми и вещественными числами.
# Когда требуется, Perl автоматически выполняет преобразования к нужному типу.

## Массивы
#  Массив хранит список значений:
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed   = ("camel", 42, 1.23);


## Хеши
#   Хеш хранит набор пар ключ/значение:

my %fruit_color = ("apple", "red", "banana", "yellow");

#  Можно использовать оператор "=>" для большей наглядности:

my %fruit_color = (
        apple  => "red",
        banana => "yellow",
        );

# Важно: вставка и поиск в хеше выполняются за константное время, 
# независимо от его размера.

# Скаляры, массивы и хеши подробно описаны в разделе perldata
# (perldoc perldata).

# Более сложные структуры данных можно получить, если использовать ссылки.
# С помощью ссылок можно получить массив массивов хешей, в которых хранятся другие хеши.

#### Условные операторы и циклы

# В Perl есть большинсво привычных условных и циклических конструкций.

if ( $var ) {
    ...
} elsif ( $var eq 'bar' ) {
    ...
} else {
    ...
}

unless ( condition ) {
                   ...
               }
# Это более читаемый вариант для "if (!condition)"

# Специфические Perl-овые пост-условия: 
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;

#  while
  while ( condition ) {
                   ...
               }


# for, foreach
for ($i = 0; $i <= $max; $i++) {
                   ...
               }

foreach (@array) {
                   print "This element is $_\n";
               }

for my $el (@array) {
                   print "This element is $el\n";
               }

#### Регулярные выражения

# Регулярные выражения занимают важное место в Perl-е,
# и подробно описаны в разделах документации perlrequick, perlretut и других.
# Вкратце:

# Сопоставление с образцом
if (/foo/)       { ... }  # выполняется, если $_ содержит "foo"
if ($a =~ /foo/) { ... }  # выполняется, если $a содержит "foo"

# Простые замены

$a =~ s/foo/bar/;         # заменяет foo на bar в строке $a
$a =~ s/foo/bar/g;        # заменяет ВСЕ ВХОЖДЕНИЯ foo на bar в строке $a


#### Файлы и ввод-вывод

# Открыть файл на чтение или запись можно с помощью функции "open()".

open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";
open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";

# Читать из файлового дескриптора можно с помощью оператора "<>".  
# В скалярном контексте он читает одну строку из файла, в списковом -- 
# читает сразу весь файл, сохраняя по одной строке в элементе массива:

my $line  = <$in>;
my @lines = <$in>;

#### Подпрограммы (функции)

# Объявить функцию просто:

sub logger {
    my $logmessage = shift;
    open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
    print $logfile $logmessage;
}

# Теперь можно использовать эту функцию так же, как и встроенные:

logger("We have a logger subroutine!");
```

#### Perl-модули

Perl-овые модули предоставляют широкий набор функциональности, 
так что вы можете не изобретать заново велосипеды, а просто скачать 
нужный модуль с CPAN (http://www.cpan.org/). 
Некоторое количество самых полезных модулей включено в стандартную 
поставку Perl.

Раздел документации perlfaq содержит вопросы и ответы о многих частых 
задачах, и часто предлагает подходящие CPAN-модули.


#### Unicode

Вам наверняка понадобится работать с не-ASCII текстами.
Добавьте эти прагмы в начало скрипта:

```perl
use utf8;
use open ':std' => ':utf8';
```

Подробнее читайте в perldoc, разделы perlunicode и open.


#### strict, warnings

Прагмы strict и warnings включают полезные проверки во время компиляции:

```perl
use strict;
use warnings;
```

Подробнее смотрите perldoc strict и perldoc warnings.


#### Смотрите также

 - [perl-tutorial](http://perl-tutorial.org/)
 - [обучающий раздел на www.perl.com](http://www.perl.org/learn.html)
 - [perldoc в вебе](http://perldoc.perl.org/)
 - встроенная справка : `perldoc perlintro`
---
language: PHP
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
translators:
    - ["SlaF", "https://github.com/SlaF"]
    - ["Corpsee", "https://github.com/corpsee"]
lang: ru-ru
filename: learnphp-ru.php
---

Этот документ описывает версию PHP 5 и выше.

```php
<?php // PHP код должен быть заключен в теги <?php

// Если ваш файл содержит только PHP-код, то можно
пропустить закрывающий ?>

// А так начинаются комментарии

# Это тоже комментарий но // предпочтительнее

/*
	Окруженный /* и */ текст превращается
	в многострочный комментарий
*/

// Используйте "echo" или "print" для вывода.
print('Hello '); // Напечатать "Hello " без перевода строки

// () необязательно применять для print и echo
echo "World\n"; // Напечатать "World" и перейти на новую строку.
// (все утверждения должны заканчиваться точкой с запятой)

// Любые символы за пределами закрывающего тега выводятся автоматически:
?>
Hello World Again!
<?php


/************************************
 * Типы и Переменные
 */

// Переменные начинаются с символа $.
// Правильное имя переменной начинается с буквы или символа подчеркивания,
// за которым следует любое количество букв, цифр или символов подчеркивания.
// Не рекомендуется использовать кириллические символы в именах (прим. пер.)

// Логические значения нечувствительны к регистру
$boolean = true;  // или TRUE или True
$boolean = false; // или FALSE или False

// Целые числа
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (ведущий 0 обозначает восьмеричное число)
$int4 = 0x0F; // => 15 (ведущие символы 0x означают шестнадцатеричное число)

// Двоичная запись integer доступна начиная с PHP 5.4.0.
$int5 = 0b11111111; // 255 (0b в начале означает двоичное число)

// Дробные числа
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// Арифметика
$sum        = 1 + 1; // 2
$difference = 2 - 1; // 1
$product    = 2 * 2; // 4
$quotient   = 2 / 1; // 2

// Арифметические сокращения
$number = 0;
$number += 1;      // Увеличивает $number на 1
echo $number++;    // Печатает 1 (инкрементируется после вывода)
echo ++$number;    // Печатает 3 (инкрементируется до вывода)
$number /= $float; // Делится и результат присваивается $number

// Строки должны быть заключены в одинарные кавычки;
$sgl_quotes = '$String'; // => '$String'

// Избегайте двойных кавычек за исключением случаев интерполирования переменной
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// Специальные (escape) символы работают только в двойных кавычках
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// Заключайте переменные в фигурные скобки, если это необходимо
$apples = "I have {$number} apples to eat.";
$oranges = "I have ${number} oranges to eat.";
$money = "I have $${number} in the bank.";

// Начиная с PHP 5.3, синтаксис nowdocs может использоваться для
// неинтерполированного многострочного текста
$nowdoc = <<<'END'
Multi line
string
END;

// Heredocs поддерживает интерполяцию переменных
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// Строки соединяются при помощи .
echo 'This string ' . 'is concatenated';

// echo можно передавать строки как параметры
echo 'Multiple', 'Parameters', 'Valid'; // печатает 'MultipleParametersValid'


/********************************
 * Константы
 */
 
// Константа определяется при помощи define()
// и никогда не может быть изменена во время выполнения программы!

// Правильное имя константы начинается с буквы или символа подчеркивания
// и содержит любое колличество букв, цифр или символов подчеркивания.
define("FOO", "something");

// Доступ к константе возможен через прямое указание её имени без знака $
echo FOO; // печатает 'something'
echo 'This outputs ' . FOO; // печатает 'This outputs something'

/********************************
 * Массивы
 */

// Все массивы в PHP - это ассоциативные массивы

// Ассоциативные массивы, известные в других языках как HashMap.

// Работает во всех версиях РHP
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);

// В PHP 5.4 появился новый синтаксис
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];

echo $associative['One']; // печатает 1
// Добавить элемент в ассоциативный массив
$associative['Four'] = 4;


// Список тоже содержит целочисленные ключи
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"

// Добавить элемент в конец массива
$array[] = 'Four';
// или
array_push($array, 'Five');
// удалить элемент из массива
unset($array[3]);

/********************************
 * Вывод
 */

echo('Hello World!');
// Печатает Hello World! на stdout.
// Stdout это веб-страница запущенная в браузере.

print('Hello World!'); // Аналогично echo

// echo - это конструкция языка, вы можете опустить скобки.
echo 'Hello World!';
print 'Hello World!'; // Выводит Hello World!

$paragraph = 'paragraph';

echo 100;        // Печать скалярной переменной напрямую
echo $paragraph; // или печать переменной

// Если короткие теги разрешены, или ваша версия PHP >= 5.4
// вы можете использовать сокращенный синтаксис echo
?>
<p><?= $paragraph ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $x теперь содержит значение переменной $y
$z = &$y;
// $z содержит ссылку на $y. Изменение значения $z затронет значение $y и наоборот.
// Значение $x остается неизменным. 

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0

// Вывести тип и значение переменной в stdout
var_dump($z); // печатает int(0)
// Напечатать переменную в stdout в удобочитаемом виде
print_r($array); // печатает: Array ( [0] => One [1] => Two [2] => Three )

/********************************
 * Логические выражения
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// Утверждение (assert) выдает предупреждение, если его аргумент не true

// Эти сравнения всегда будут истинными, даже если типы будут различаться
assert($a == $b); // "равно"
assert($c != $a); // "не равно"
assert($c <> $a); // другое обозначение "не равно"
assert($a < $c); // меньше
assert($c > $b); // больше
assert($a <= $b); // меньше или равно
assert($c >= $d); // больше или равно

// Следующие утверждения истинны, если переменные имеют одинаковые тип.
assert($c === $d);
assert($a !== $d);
assert(1 == '1');
assert(1 !== '1');

// 'Spaceship' оператор (с PHP 7) используется для сравнения двух выражений.
// Возвращает -1, 0 или 1, когда выражение слева меньше, равно или больше
// выражения справа.
$a = 100;
$b = 1000;

echo $a <=> $a; // 0, выражения равны
echo $a <=> $b; // -1, $a < $b
echo $b <=> $a; // 1, $b > $a
// Переменные могут изменять тип в зависимости от их использования.
$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2 (строка превращается в число)

// Выводится 0 по той причине, что оператор + не может привести строку 'one' к
// числовому типу
$string = 'one';
echo $string + $string; // => 0

// Приведение типов (type casting) может быть использовано для преобразование
// переменной в другой тип
$boolean = (boolean) 1; // => true

$zero = 0;
$boolean = (boolean) $zero; // => false

// Также существуют функции выполняющие приведение типов
$integer = 5;
$string = strval($integer);
$float = floatval($integer);

$var = null; // Null

// И похожая по действию функция
$integer = 10;
$boolen = settype($integer, "string") // теперь $integer имеет строковый тип

// settype возвращает true, если преобразование удалось и false в противном случае

/********************************
 * Управляющие структуры
 */

if (true) {
    print 'I get printed';
}

if (false) {
    print 'I don\'t';
} else {
    print 'I get printed';
}

if (false) {
    print 'Does not get printed';
} elseif(true) {
    print 'Does';
}

// Тернарный оператор
print (false ? 'Does not get printed' : 'Does');

// сокращенная запись тернарного оператора с PHP 5.3
// эквивалентно "$x ? $x : 'Does'"
$x = false;
print($x ?: 'Does');

$x = 0;
if ($x === '0') {
    print 'Does not print';
} elseif($x == '1') {
    print 'Does not print';
} else {
    print 'Does print';
}

// Альтернативный синтаксис полезный для шаблонов
?>

<?php if ($x): ?>
This is displayed if the test is truthy.
<?php else: ?>
This is displayed otherwise.
<?php endif; ?>

<?php

// Использование switch.
switch ($x) {
    case '0':
        print 'Switch использует неточное сравнение';
        break; // вы должны использовать break, иначе PHP будет продолжать
               // исполнять команды следующих секций case 'two' и 'three'
    case 'two':
    case 'three':
        // делаем что-то, если $x == 'two' или $x == 'three'
        break;
    default:
        // делаем что-то по умолчанию
}

// Циклы: while, do...while и for
$i = 0;
while ($i < 5) {
    echo $i++;
}; // печатает "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // печатает "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // печатает "0123456789"

echo "\n";

$wheels = ['bicycle' => 2, 'car' => 4];

// Циклы foreach могут обходить массивы
foreach ($wheels as $wheel_count) {
    echo $wheel_count;
} // Напечатает "24"

echo "\n";

// Вы можете обходить как ключи, так и их значения
foreach ($wheels as $vehicle => $wheel_count) {
    echo "A $vehicle has $wheel_count wheels";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // выйти из цикла while
    }
    echo $i++;
} // Напечатает "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // пропустить текущую итерацию цикла
    }
    echo $i;
} // печатает "0124"


/********************************
 * Функции
 */

// Определение функции:
function my_function () {
  return 'Hello';
}

echo my_function(); // => "Hello"

// Правильное имя функции начинается с буквы или символа подчеркивания
// и состоит из букв, цифр или символов подчеркивания.

function add ($x, $y = 1) { // $y по умолчанию равно 1
  $result = $x + $y;
  return $result;
}

echo add(4); // => 5
echo add(4, 2); // => 6

// $result недоступен за пределами функции
// print $result; // Выдает предупреждение

// Начиная с PHP 5.3 вы можете объявлять анонимные функции:
$inc = function ($x) {
  return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
  echo "$x - $y - $z";
}

// Функции могут возвращать функции
function bar ($x, $y) {
  // Используйте 'use' для передачи внешних переменных
  return function ($z) use ($x, $y) {
    foo($x, $y, $z);
  };
}

$bar = bar('A', 'B');
$bar('C'); // Prints "A - B - C"

// Вы можете вызывать именованные функции используя строки
$function_name = 'add';
echo $function_name(1, 2); // => 3
// Полезно для программного определения запущенной функции.
// Или используйте call_user_func(callable $callback [, $parameter [, ... ]]);


/********************************
 * Включения
 */

<?php
// PHP код внутри включаемого файла должен начинаться с тега PHP.

include 'my-file.php';
// Код в файле my-file.php теперь доступен в текущем пространстве имен.
// Если файл не удалось подключить, то будет выдано предупреждение.

include_once 'my-file.php';
// Если код в файле my-file.php уже был подключен, он не будет подключен повторно.
// Это предотвращает ошибку повторного подключения файла.

require 'my-file.php';
require_once 'my-file.php';

// Действует также как и include(), но если файл не удалось подключить,
// функция выдает фатальную ошибку

// Содержимое файла my-include.php:
<?php

return 'Anything you like.';
// Конец файла

// Эти функции могут также возвращать значения.
$value = include 'my-include.php';

// Имена файлов содержат их путь в файловой системе, или если передано просто
// имя файла, PHP обращается к директиве include_path. Если файл не найден в
// include_path, предпринимается попытка поиска в папке, где выполняется скрипт
// или в текущей рабочей директории. Если не в одном из этих мест файл не
// найден - выдается ошибка
/* */

/********************************
 * Классы
 */

// Классы определяются при помощи ключевого слова "class"

class MyClass
{
    const MY_CONST      = 'value'; // Константа

    static $staticVar   = 'static';

    // Свойства объявляются с указанием их видимости
    public $property    = 'public';
    public $instanceProp;
    protected $prot = 'protected'; // Свойство доступно только потомкам и самому классу
    private $priv   = 'private';   // Свойство доступно только самому классу

    // Конструктор описывается с помощью __construct
    public function __construct($instanceProp) {
        // Доступ к эземпляру класса с помощью $this
        $this->instanceProp = $instanceProp;
    }

    // Методы объявляются как функции принадлежащие классу
    public function myMethod()
    {
        print 'MyClass';
    }

    final function youCannotOverrideMe()
    {
    }

    public static function myStaticMethod()
    {
        print 'I am static';
    }
}

echo MyClass::MY_CONST;    // Выведет 'value';
echo MyClass::$staticVar;  // Выведет 'static';
MyClass::myStaticMethod(); // Выведет 'I am static';

// Создание нового экземпляра класса используя new
$my_class = new MyClass('An instance property');

// Если аргументы отсутствуют, можно не ставить круглые скобки

// Доступ к членам класса используя ->
echo $my_class->property;     // => "public"
echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod();        // => "MyClass"

// Наследование классов используя "extends"
class MyOtherClass extends MyClass
{
    function printProtectedProperty()
    {
        echo $this->prot;
    }

    // Переопределение родительского метода
    function myMethod()
    {
        parent::myMethod();
        print ' > MyOtherClass';
    }
}

$my_other_class = new MyOtherClass('Instance prop');
$my_other_class->printProtectedProperty(); // => Выведет "protected"
$my_other_class->myMethod();               // Выведет "MyClass > MyOtherClass"

final class YouCannotExtendMe
{
}

// Вы можете использовать "магические методы" для создания геттеров и сеттеров
class MyMapClass
{
    private $property;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MyMapClass();
echo $x->property; // Будет использован метод __get()
$x->property = 'Something'; // Будет использован метод __set()

// Классы могут быть абстрактными (используя ключевое слово abstract)
// или реализовывать интерфейсы (используя ключевое слово implements).
// Интерфейсы определяются при помощи ключевого слова interface

interface InterfaceOne
{
    public function doSomething();
}

interface InterfaceTwo
{
    public function doSomethingElse();
}

// Интерфейсы могут быть расширены
interface InterfaceThree extends InterfaceTwo
{
    public function doAnotherContract();
}

abstract class MyAbstractClass implements InterfaceOne
{
    public $x = 'doSomething';
}

class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
{
    public function doSomething()
    {
        echo $x;
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}

// Классы могут реализовывать более одного интерфейса
class SomeOtherClass implements InterfaceOne, InterfaceTwo
{
    public function doSomething()
    {
        echo 'doSomething';
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


/********************************
 * Трейты
 */

// Трейты появились в PHP 5.4 и объявляются при помощи ключевого слова trait

trait MyTrait
{
    public function myTraitMethod()
    {
        print 'I have MyTrait';
    }
}

class MyTraitfulClass
{
    use MyTrait;
}

$cls = new MyTraitfulClass();
$cls->myTraitMethod(); // Напечатает "I have MyTrait"


/********************************
 * Пространства имен
 */

// Это секция особая, ведь объявление пространства имен
// должно быть самым первым в файле. Позвольте сделать вид, что это не так

<?php

// По умолчанию, классы существуют в глобальном пространстве имен и могут быть
// вызваны с обратным слешем.

$cls = new \MyClass();

// Задание пространства имен файла
namespace My\Namespace;

class MyClass
{
}

// (из другого файла)
$cls = new My\Namespace\MyClass;

// Или внутри другого пространства имен.
namespace My\Other\Namespace;

use My\Namespace\MyClass;

$cls = new MyClass();

// Или вы можете создать псевдоним для пространства имен:
namespace My\Other\Namespace;

use My\Namespace as SomeOtherNamespace;

$cls = new SomeOtherNamespace\MyClass();


/**********************
* Позднее статическое связывание.
*
*/

class ParentClass
{
    public static function who()
    {
        echo "I'm a " . __CLASS__ . "\n";
    }

    public static function test()
    {
        // self ссылается на класс в котором определен метод.
        self::who();
        // static ссылается на класс в котором метод вызван.
        static::who();
    }
}

ParentClass::test();
/*
I'm a ParentClass
I'm a ParentClass
*/

class ChildClass extends ParentClass
{
    public static function who()
    {
        echo "But I'm " . __CLASS__ . "\n";
    }
}

ChildClass::test();
/*
I'm a ParentClass
But I'm ChildClass
*/


/**********************
*  Магические константы
*
*/

// Возвращает имя текущего класса. Должно быть использовано внутри класса.
echo "Current class name is " . __CLASS__;

// Возвращает полный путь текущей папки из которой вызван файл.
echo "Current directory is " . __DIR__;

    // Обычно используют в таких случаях:
    require __DIR__ . '/vendor/autoload.php';

// Возвращает полный путь к текущему файлу.
echo "Current file path is " . __FILE__;

// Возвращает имя текущей функции.
echo "Current function name is " . __FUNCTION__;

// Возвращает номер текущей линии.
echo "Current line number is " . __LINE__;

// Возвращает имя текущего метода. Возвращает только если вызван внутри метода.
echo "Current method is " . __METHOD__;

// Возвращает имя текущего пространства имен.
echo "Current namespace is " . __NAMESPACE__;

// Возвращает имя текущего трейта.
// Возвращает только если испольщуется внутри трейта.
echo "Current namespace is " . __TRAIT__;


/**********************
*  Обработка ошибок
*  
*/

// Простую обработку ошибок можно произвести спомощью try catch блока.

try {
    // Выполняем что-то
} catch (Exception $e) {
    // Обработка исключения
}

// При использовании try catch блока в области вилимости, стоит использовать
// следующий подход:

try {
    // Do something
} catch (\Exception $e) {
    // Обработка исключения
}

// Специальное(кастомное) исключение - exceptions

class MyException extends Exception {}

try {

    $condition = true;

    if ($condition) {
        throw new MyException('Something just happend');
    }

} catch (MyException $e) {
    // Обработка исключения
}

```

## Смотрите также:
Посетите страницу [официальной документации PHP](http://www.php.net/manual/) для справки.

Если вас интересуют полезные приемы использования PHP посетите [PHP The Right Way](http://www.phptherightway.com/).

Если вы раньше пользовались языком с хорошей организацией пакетов, посмотрите [Composer](http://getcomposer.org/).

Для изучения стандартов использования языка посетите PHP Framework Interoperability Group's [PSR standards](https://github.com/php-fig/fig-standards).
---
category: tool
tool: PyQT
lang: ru-ru
filename: learnpyqt-ru.py
contributors:
    - ["Nathan Hughes", "https://github.com/sirsharpest"]
translators:
    - ["Vadim Toptunov", "https://github.com/VadimToptunov"]
---

**Qt** - широко известный кросс-платформенный фреймворк для разработки программного обеспечения,
который может быть использован на различных софтварных и хардварных платформах без какого-либо
изменения в коде. Данный фреймворк при этом обладает мощью и скоростью нативных приложений. 
Qt и был изначально написан на *C++*.

Данный текст является адаптацией введения в Qt на C++ под авторством Алексея Ковальчука для pyqt.


```python

def window():
    # Создайте объект приложения 
    app = QtGui.QApplication(sys.argv)
    # Создайте виджет, где будет находиться наш лейбл
    w = QtGui.QWidget()
    # Добавьте лейбл в виджет
    b = QtGui.QLabel(w)
    # Задайте текст для лейбла
    b.setText("Hello World!")
    # Задайте информация о размере и расположении 
    w.setGeometry(100, 100, 200, 50)
    b.move(50, 20)
    # Задайте заголовок окна 
    w.setWindowTitle("PyQt")
    # Все ранее написанное выводится на экран
    w.show()
    # Настройка
    sys.exit(app.exec_())

if __name__ == '__main__':
    window()

```

Для того, чтобы получить более продвинутые функции приложения в pyqt, нам необходимо 
обратить внимание на создание дополнительных элементов. Ниже представлено создание всплывающего диалогового окна, которое просит пользователя подтвердить его решение или предоставить какую-либо 
информацию.

```Python 
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


def window():
    app = QApplication(sys.argv)
    w = QWidget()
    # Создайте кнопку и прикрепите ее к виджету w
    b = QPushButton(w)
    b.setText("Press me")
    b.move(50, 50)
    # Укажите b вызвать эту функцию при клике мышкой
    # Заметьте, что в вызове функции отсутствуют "()"
    b.clicked.connect(showdialog)
    w.setWindowTitle("PyQt Dialog")
    w.show()
    sys.exit(app.exec_())

Данная функция должна создавать диалоговое окно с кнопкой, которая ждет клика по себе 
и затем завершает программу.

def showdialog():
    d = QDialog()
    b1 = QPushButton("ok", d)
    b1.move(50, 50)
    d.setWindowTitle("Dialog")
    # Эта модальность сообщает всплывающему окну блокировать родительский элемент, пока он активен
    d.setWindowModality(Qt.ApplicationModal)
    # Процесс завершается по клику мышкой
    b1.clicked.connect(sys.exit)
    d.exec_()

if __name__ == '__main__':
    window()
```
---
language: python
lang: ru-ru
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["Yury Timofeev", "http://twitter.com/gagar1n"]
    - ["Andre Polykanine", "https://github.com/Oire"]
filename: learnpython-ru.py
---

Язык Python был создан Гвидо ван Россумом в начале 90-х. Сейчас это один из
самых популярных языков. Я влюбился в Python за понятный и доходчивый синтаксис  — это
почти исполняемый псевдокод.

С благодарностью жду ваших отзывов: [@louiedinh](http://twitter.com/louiedinh)
или louiedinh [at] [почтовый сервис Google]

Замечание: Эта статья относится к Python 2.7, но должно работать и в других версиях Python 2.x.
Чтобы изучить Python 3.x, обратитесь к статье по Python 3.

```python
# Однострочные комментарии начинаются с символа решётки.
""" Многострочный текст может быть 
    записан, используя 3 знака " и обычно используется
    в качестве встроенной документации
"""

####################################################
## 1. Примитивные типы данных и операторы
####################################################

# У вас есть числа
3 #=> 3

# Математика работает вполне ожидаемо
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# А вот деление немного сложнее. В этом случае происходит деление 
# целых чисел, и результат автоматически округляется в меньшую сторону.
5 / 2 #=> 2

# Чтобы делить правильно, сначала нужно немного узнать о числах
# с плавающей запятой.
2.0     # Это число с плавающей запятой
11.0 / 4.0 #=> 2.75 Вооот... Так гораздо лучше

# Результат целочисленного деления округляется в меньшую сторону
# как для положительных, так и для отрицательных чисел.
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # работает и для чисел с плавающей запятой
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# Остаток от деления
7 % 3 # => 1

# Возведение в степень
2**4 # => 16

# Приоритет операций указывается скобками
(1 + 3) * 2 #=> 8

# Логические операторы
# Обратите внимание: ключевые слова «and» и «or» чувствительны к регистру букв
True and False #=> False
False or True #=> True

# Обратите внимание, что логические операторы используются и с целыми числами
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# Для отрицания используется ключевое слово not
not True #=> False
not False #=> True

# Равенство — это ==
1 == 1 #=> True
2 == 1 #=> False

# Неравенство — это !=
1 != 1 #=> False
2 != 1 #=> True

# Ещё немного сравнений
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# Сравнения могут быть записаны цепочкой!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Строки определяются символом " или '
"Это строка."
'Это тоже строка.'

# И строки тоже можно складывать!
"Привет " + "мир!" #=> "Привет мир!"

# ... или умножать
"Привет" * 3  # => "ПриветПриветПривет"

# Со строкой можно работать, как со списком символов
"Это строка"[0] #=> 'Э'

# Символ % используется для форматирования строк, например:
"%s могут быть %s" % ("строки", "интерполированы")

# Новый способ форматирования строк — использование метода format.
# Это предпочитаемый способ.
"{0} могут быть {1}".format("строки", "форматированы")

# Если вы не хотите считать, можете использовать ключевые слова.
"{name} хочет есть {food}".format(name="Боб", food="лазанью")

# None является объектом
None #=> None

# Не используйте оператор равенства '=='' для сравнения 
# объектов с None. Используйте для этого «is»
"etc" is None #=> False
None is None  #=> True

# Оператор 'is' проверяет идентичность объектов. Он не 
# очень полезен при работе с примитивными типами, но 
# зато просто незаменим при работе с объектами.

# None, 0 и пустые строки/списки равны False.
# Все остальные значения равны True
0 == False  #=> True
"" == False #=> True


####################################################
## 2. Переменные и коллекции
####################################################

# В Python есть оператор print, доступный в версиях 2.x, но удалённый в версии 3
print "Я Python. Приятно познакомиться!"
# В Python также есть функция print(), доступная в версиях 2.7 и 3,
# Но для версии 2.7 нужно добавить следующий импорт модуля (раскомментируйте)):
# from __future__ import print_function
print("Я тоже Python! ")

# Объявлять переменные перед инициализацией не нужно.
some_var = 5    # По соглашению используется нижний_регистр_с_подчёркиваниями
some_var #=> 5

# При попытке доступа к неинициализированной переменной
# выбрасывается исключение.
# См. раздел «Поток управления» для информации об исключениях.
some_other_var  # Выбрасывает ошибку именования

# if может быть использован как выражение
"yahoo!" if 3 > 2 else 2 #=> "yahoo!"

# Списки хранят последовательности
li = []
# Можно сразу начать с заполненного списка
other_li = [4, 5, 6]

# строка разделена в список
a="adambard"
list(a) #=> ['a','d','a','m','b','a','r','d']

# Объекты добавляются в конец списка методом append
li.append(1)    # [1]
li.append(2)    # [1, 2]
li.append(4)    # [1, 2, 4]
li.append(3)    # [1, 2, 4, 3]
# И удаляются с конца методом pop
li.pop()        #=> возвращает 3 и li становится равен [1, 2, 4]
# Положим элемент обратно
li.append(3)    # [1, 2, 4, 3].

# Обращайтесь со списком, как с обычным массивом
li[0] #=> 1
# Присваивайте новые значения уже инициализированным индексам с помощью =
li[0] = 42
li[0]  # => 42
li[0] = 1  # Обратите внимание: возвращаемся на исходное значение
# Обратимся к последнему элементу
li[-1] #=> 3

# Попытка выйти за границы массива приведёт к ошибке индекса
li[4] # Выдаёт IndexError

# Можно обращаться к диапазону, используя так называемые срезы
# (Для тех, кто любит математику, это называется замкнуто-открытый интервал).
li[1:3] #=> [2, 4]
# Опускаем начало
li[2:] #=> [4, 3]
# Опускаем конец
li[:3] #=> [1, 2, 4]
# Выбираем каждый второй элемент
li[::2]   # =>[1, 4]
# Переворачиваем список
li[::-1]   # => [3, 4, 2, 1]
# Используйте сочетания всего вышеназванного для выделения более сложных срезов
# li[начало:конец:шаг]

# Удаляем произвольные элементы из списка оператором del
del li[2] # li теперь [1, 2, 3]

# Вы можете складывать, или, как ещё говорят, конкатенировать списки
li + other_li #=> [1, 2, 3, 4, 5, 6]  — Замечание: li и other_li не изменяются
# Обратите внимание: значения li и other_li при этом не изменились.

# Объединять списки можно методом extend
li.extend(other_li) # Теперь li содержит [1, 2, 3, 4, 5, 6]

# Проверить элемент на вхождение в список можно оператором in
1 in li #=> True

# Длина списка вычисляется функцией len
len(li) #=> 6


# Кортежи — это такие списки, только неизменяемые
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # Выдаёт TypeError

# Всё то же самое можно делать и с кортежами
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Вы можете распаковывать кортежи (или списки) в переменные
a, b, c = (1, 2, 3)     # a == 1, b == 2 и c == 3
# Кортежи создаются по умолчанию, если опущены скобки
d, e, f = 4, 5, 6
# Обратите внимание, как легко поменять местами значения двух переменных
e, d = d, e     # теперь d == 5, а e == 4

#  Словари содержат ассоциативные массивы
empty_dict = {}
# Вот так описывается предзаполненный словарь
filled_dict = {"one": 1, "two": 2, "three": 3}

# Значения извлекаются так же, как из списка, с той лишь разницей,
# что индекс — у словарей он называется ключом — не обязан быть числом
filled_dict["one"] #=> 1

# Можно получить все ключи в виде списка с помощью метода keys
filled_dict.keys() #=> ["three", "two", "one"]
# Замечание: сохранение порядка ключей в словаре не гарантируется
# Ваши результаты могут не совпадать с этими.

# Можно получить и все значения в виде списка, используйте метод values
filled_dict.values() #=> [3, 2, 1]
# То же самое замечание насчёт порядка ключей справедливо и здесь

# При помощи оператора in можно проверять ключи на вхождение в словарь
"one" in filled_dict #=> True
1 in filled_dict #=> False

# Попытка получить значение по несуществующему ключу выбросит ошибку ключа
filled_dict["four"] # KeyError

# Чтобы избежать этого, используйте метод get()
filled_dict.get("one") #=> 1
filled_dict.get("four") #=> None
# Метод get также принимает аргумент по умолчанию, значение которого будет
# возвращено при отсутствии указанного ключа
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4
# Обратите внимание, что filled_dict.get("four") всё ещё => None
# (get не устанавливает значение элемента словаря)

# Присваивайте значение ключам так же, как и в списках
filled_dict["four"] = 4  # теперь filled_dict["four"] => 4

# Метод setdefault() вставляет пару ключ-значение, только если такого ключа нет
filled_dict.setdefault("five", 5) #filled_dict["five"] возвращает 5
filled_dict.setdefault("five", 6) #filled_dict["five"] по-прежнему возвращает 5


# Множества содержат... ну, в общем, множества
# (которые похожи на списки, только в них не может быть дублирующихся элементов)
empty_set = set()
# Инициализация множества набором значений
some_set = set([1,2,2,3,4]) # some_set теперь равно set([1, 2, 3, 4])

# Порядок сортировки не гарантируется, хотя иногда они выглядят отсортированными
another_set = set([4, 3, 2, 2, 1])  # another_set теперь set([1, 2, 3, 4])

# Начиная с Python 2.7, вы можете использовать {}, чтобы объявить множество
filled_set = {1, 2, 2, 3, 4} # => {1, 2, 3, 4}

# Добавление новых элементов в множество
filled_set.add(5) # filled_set равно {1, 2, 3, 4, 5}

# Пересечение множеств: &
other_set = {3, 4, 5, 6}
filled_set & other_set #=> {3, 4, 5}

# Объединение множеств: |
filled_set | other_set #=> {1, 2, 3, 4, 5, 6}

# Разность множеств: -
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Проверка на вхождение во множество: in
2 in filled_set #=> True
10 in filled_set #=> False


####################################################
## 3. Поток управления
####################################################

# Для начала заведём переменную
some_var = 5

# Так выглядит выражение if. Отступы в python очень важны!
# результат: «some_var меньше, чем 10»
if some_var > 10:
    print("some_var намного больше, чем 10.")
elif some_var < 10:    # Выражение elif необязательно.
    print("some_var меньше, чем 10.")
else:           # Это тоже необязательно.
    print("some_var равно 10.")


"""
Циклы For проходят по спискам

Результат:
    собака — это млекопитающее
    кошка — это млекопитающее
    мышь — это млекопитающее
"""
for animal in ["собака", "кошка", "мышь"]:
    # Можете использовать оператор % для интерполяции форматированных строк
    print("%s — это млекопитающее" % animal)
    
"""
«range(число)» возвращает список чисел
от нуля до заданного числа
Результат:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
Циклы while продолжаются до тех пор, пока указанное условие не станет ложным.
Результат:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Краткая запись для x = x + 1

# Обрабатывайте исключения блоками try/except

# Работает в Python 2.6 и выше:
try:
    # Чтобы выбросить ошибку, используется raise
    raise IndexError("Это ошибка индекса")
except IndexError as e:
    # pass — это просто отсутствие оператора. Обычно здесь происходит
    # восстановление после ошибки.
    pass
except (TypeError, NameError):
    pass    # Несколько исключений можно обработать вместе, если нужно.
else:   # Необязательное выражение. Должно следовать за последним блоком except
    print("Всё хорошо!")   # Выполнится, только если не было никаких исключений



####################################################
## 4. Функции
####################################################

# Используйте def для создания новых функций
def add(x, y):
    print("x равен %s, а y равен %s" % (x, y))
    return x + y    # Возвращайте результат с помощью ключевого слова return

# Вызов функции с аргументами
add(5, 6) #=> выводит «x равен 5, а y равен 6» и возвращает 11

# Другой способ вызова функции — вызов с именованными аргументами
add(y=6, x=5)   # Именованные аргументы можно указывать в любом порядке.

# Вы можете определить функцию, принимающую переменное число аргументов,
# которые будут интерпретированы как кортеж, если вы не используете *
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# А также можете определить функцию, принимающую переменное число
# именованных аргументов, которые будут интерпретированы как словарь,
# если вы не используете **
def keyword_args(**kwargs):
    return kwargs

# Вызовем эту функцию и посмотрим, что из этого получится
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# Если хотите, можете использовать оба способа одновременно
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) выводит:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Вызывая функции, можете сделать наоборот!
# Используйте символ * для распаковки кортежей и ** для распаковки словарей
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # эквивалентно foo(1, 2, 3, 4)
all_the_args(**kwargs) # эквивалентно foo(a=3, b=4)
all_the_args(*args, **kwargs) # эквивалентно foo(1, 2, 3, 4, a=3, b=4)

# вы можете передавать переменное число позиционных или именованных аргументов
# другим функциям, которые их принимают, распаковывая их с помощью
# * или ** соответственно
def pass_all_the_args(*args, **kwargs):
    all_the_args(*args, **kwargs)
    print varargs(*args)
    print keyword_args(**kwargs)

# Область определения функций
x = 5

def setX(num):
    # Локальная переменная x — это не то же самое, что глобальная переменная x
    x = num # => 43
    print (x) # => 43
    
def setGlobalX(num):
    global x
    print (x) # => 5
    x = num # Глобальная переменная x теперь равна 6
    print (x) # => 6

setX(43)
setGlobalX(6)

# В Python функции — «объекты первого класса»
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# Также есть и анонимные функции
(lambda x: x > 2)(3) #=> True

# Есть встроенные функции высшего порядка
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Для удобного отображения и фильтрации можно использовать списочные включения
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Классы
####################################################

# Чтобы получить класс, мы наследуемся от object.
class Human(object):

    # Атрибут класса. Он разделяется всеми экземплярами этого класса
    species = "H. sapiens"

    # Обычный конструктор, вызывается при инициализации экземпляра класса
    # Обратите внимание, что двойное подчёркивание в начале и в конце имени
    # означает объекты и атрибуты, которые используются Python, но находятся
    # в пространствах имён, управляемых пользователем.
    # Не придумывайте им имена самостоятельно.
    def __init__(self, name):
        # Присваивание значения аргумента атрибуту класса name
        self.name = name

    # Метод экземпляра. Все методы принимают self в качестве первого аргумента
    def say(self, msg):
       return "%s: %s" % (self.name, msg)

    # Метод класса разделяется между всеми экземплярами
    # Они вызываются с указыванием вызывающего класса в качестве первого аргумента
    @classmethod
    def get_species(cls):
        return cls.species

    # Статический метод вызывается без ссылки на класс или экземпляр
    @staticmethod
    def grunt():
        return "*grunt*"


# Инициализация экземпляра класса
i = Human(name="Иван")
print(i.say("привет"))     # Выводит: «Иван: привет»

j = Human("Пётр")
print(j.say("Привет"))  # Выводит: «Пётр: привет»

# Вызов метода класса
i.get_species() #=> "H. sapiens"

# Изменение разделяемого атрибута
Human.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# Вызов статического метода
Human.grunt() #=> "*grunt*"


####################################################
## 6. Модули
####################################################

# Вы можете импортировать модули
import math
print(math.sqrt(16)) #=> 4

# Вы можете импортировать отдельные функции модуля
from math import ceil, floor
print(ceil(3.7))  #=> 4.0
print(floor(3.7)) #=> 3.0

# Можете импортировать все функции модуля.
# (Хотя это и не рекомендуется)
from math import *

# Можете сокращать имена модулей
import math as m
math.sqrt(16) == m.sqrt(16) #=> True
# Вы также можете убедиться, что функции эквивалентны
from math import sqrt
math.sqrt == m.sqrt == sqrt  # => True

# Модули в Python — это обычные Python-файлы. Вы
# можете писать свои модули и импортировать их. Название
# модуля совпадает с названием файла.

# Вы можете узнать, какие функции и атрибуты определены 
# в модуле
import math
dir(math)

####################################################
## 7. Дополнительно
####################################################

# Генераторы помогут выполнить ленивые вычисления
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# Генератор создаёт значения на лету.
# Он не возвращает все значения разом, а создаёт каждое из них при каждой
# итерации.  Это значит, что значения больше 15 в double_numbers
# обработаны не будут.
# Обратите внимание: xrange — это генератор, который делает то же, что и range.
# Создание списка чисел от 1 до 900000000 требует много места и времени.
# xrange создаёт объект генератора, а не список сразу, как это делает range.
# Если нам нужно имя переменной, совпадающее с ключевым словом Python,
# мы используем подчёркивание в конце
xrange_ = xrange(1, 900000000)

# Будет удваивать все числа, пока результат не превысит 30
for i in double_numbers(xrange_):
    print(i)
    if i >= 30:
        break


# Декораторы
# В этом примере beg оборачивает say
# Метод beg вызовет say. Если say_please равно True,
# он изменит возвращаемое сообщение
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, " Пожалуйста! У меня нет денег :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Вы не купите мне пива?"
    return msg, say_please


print(say())  # Вы не купите мне пива?
print(say(say_please=True)) # Вы не купите мне пива? Пожалуйста! У меня нет денег :(

```

## Хотите ещё?

### Бесплатные онлайн-материалы

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Официальная документация](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)

### Платные

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: python3
lang: ru-ru
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
    - ["Steven Basart", "http://github.com/xksteven"]
translators:
    - ["Andre Polykanine", "https://github.com/Oire"]
filename: learnpython3-ru.py
---

Язык Python был создан Гвидо ван Россумом в начале 90-х. Сейчас это один из
самых популярных языков. Я влюбился в Python за понятный и доходчивый синтаксис  — это
почти что исполняемый псевдокод.

С благодарностью жду ваших отзывов: [@louiedinh](http://twitter.com/louiedinh)
или louiedinh [at] [почтовый сервис Google]

Замечание: Эта статья относится только к Python 3.
Если вы хотите изучить Python 2.7, обратитесь к другой статье.

```python
# Однострочные комментарии начинаются с символа решётки.
""" Многострочный текст может быть 
    записан, используя 3 знака " и обычно используется
    в качестве встроенной документации
"""

####################################################
## 1. Примитивные типы данных и операторы
####################################################

# У вас есть числа
3 #=> 3

# Математика работает вполне ожидаемо
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20

# Кроме деления, которое по умолчанию возвращает число с плавающей запятой
35 / 5  # => 7.0

# Результат целочисленного деления округляется в меньшую сторону
# как для положительных, так и для отрицательных чисел.
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # работает и для чисел с плавающей запятой
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# Когда вы используете числа с плавающей запятой, 
# результатом будет также число с плавающей запятой
3 * 2.0 # => 6.0

# Остаток от деления
7 % 3 # => 1

# Возведение в степень
2**4 # => 16

# Приоритет операций указывается скобками
(1 + 3) * 2 #=> 8

# Для логических (булевых) значений существует отдельный примитивный тип
True
False

# Для отрицания используется ключевое слово not
not True #=> False
not False #=> True

# Логические операторы
# Обратите внимание: ключевые слова «and» и «or» чувствительны к регистру букв
True and False #=> False
False or True #=> True

# Обратите внимание, что логические операторы используются и с целыми числами
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# Равенство — это ==
1 == 1 #=> True
2 == 1 #=> False

# Неравенство — это !=
1 != 1 #=> False
2 != 1 #=> True

# Ещё немного сравнений
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# Сравнения могут быть записаны цепочкой:
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Строки определяются символом " или '
"Это строка."
'Это тоже строка.'

# И строки тоже могут складываться! Хотя лучше не злоупотребляйте этим.
"Привет " + "мир!" #=> "Привет мир!"

# Со строкой можно работать, как со списком символов
"Это строка"[0] #=> 'Э'

# Метод format используется для форматирования строк:
"{0} могут быть {1}".format("строки", "форматированы")

# Вы можете повторять аргументы форматирования, чтобы меньше печатать.
"Ехал {0} через реку, видит {0} - в реке {1}! Сунул {0} руку в реку, {1} за руку греку цап!".format("грека", "рак")
#=> "Ехал грека через реку, видит грека - в реке рак! Сунул грека руку в реку, рак за руку греку цап!"
# Если вы не хотите считать, можете использовать ключевые слова.
"{name} хочет есть {food}".format(name="Боб", food="лазанью")

# Если ваш код на Python 3 нужно запускать также и под Python 2.5 и ниже,
# вы также можете использовать старый способ форматирования:
"%s можно %s %s способом" % ("строки", "интерполировать", "старым")

# None является объектом
None #=> None

# Не используйте оператор равенства '==' для сравнения 
# объектов с None. Используйте для этого 'is'
"etc" is None #=> False
None is None  #=> True

# Оператор «is» проверяет идентичность объектов. Он не 
# очень полезен при работе с примитивными типами, но 
# зато просто незаменим при работе с объектами.

# None, 0 и пустые строки/списки/словари приводятся к False.
# Все остальные значения равны True
bool(0)  # => False
bool("")  # => False
bool([]) #=> False
bool({}) #=> False


####################################################
## 2. Переменные и коллекции
####################################################

# В Python есть функция Print
print("Я Python. Приятно познакомиться!")

# Объявлять переменные перед инициализацией не нужно.
# По соглашению используется нижний_регистр_с_подчёркиваниями
some_var = 5
some_var #=> 5

# При попытке доступа к неинициализированной переменной
# выбрасывается исключение.
# Об исключениях см. раздел «Поток управления и итерируемые объекты».
some_unknown_var  # Выбрасывает ошибку именования

# Списки хранят последовательности
li = []
# Можно сразу начать с заполненного списка
other_li = [4, 5, 6]

# Объекты добавляются в конец списка методом append
li.append(1)    # [1]
li.append(2)    # [1, 2]
li.append(4)    # [1, 2, 4]
li.append(3)    # [1, 2, 4, 3]
# И удаляются с конца методом pop
li.pop()        #=> возвращает 3 и li становится равен [1, 2, 4]
# Положим элемент обратно
li.append(3)    # [1, 2, 4, 3].

# Обращайтесь со списком, как с обычным массивом
li[0] #=> 1
# Обратимся к последнему элементу
li[-1] #=> 3

# Попытка выйти за границы массива приведёт к ошибке индекса
li[4] # Выдаёт IndexError

# Можно обращаться к диапазону, используя так называемые срезы
# (Для тех, кто любит математику, это называется замкнуто-открытый интервал).
li[1:3] #=> [2, 4]
# Опускаем начало
li[2:] #=> [4, 3]
# Опускаем конец
li[:3] #=> [1, 2, 4]
# Выбираем каждый второй элемент
li[::2]   # =>[1, 4]
# Переворачиваем список
li[::-1]   # => [3, 4, 2, 1]
# Используйте сочетания всего вышеназванного для выделения более сложных срезов
# li[начало:конец:шаг]

# Удаляем произвольные элементы из списка оператором del
del li[2] # [1, 2, 3]

# Вы можете складывать, или, как ещё говорят, конкатенировать списки
# Обратите внимание: значения li и other_li при этом не изменились.
li + other_li #=> [1, 2, 3, 4, 5, 6]  — Замечание: li и other_li не изменяются

# Объединять списки можно методом extend
li.extend(other_li) # Теперь li содержит [1, 2, 3, 4, 5, 6]

# Проверить элемент на вхождение в список можно оператором in
1 in li #=> True

# Длина списка вычисляется функцией len
len(li) #=> 6


# Кортежи — это такие списки, только неизменяемые
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # Выдаёт TypeError

# Всё то же самое можно делать и с кортежами
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Вы можете распаковывать кортежи (или списки) в переменные
a, b, c = (1, 2, 3)     # a == 1, b == 2 и c == 3
# Кортежи создаются по умолчанию, если опущены скобки
d, e, f = 4, 5, 6
# Обратите внимание, как легко поменять местами значения двух переменных
e, d = d, e     # теперь d == 5, а e == 4


#  Словари содержат ассоциативные массивы
empty_dict = {}
# Вот так описывается предзаполненный словарь
filled_dict = {"one": 1, "two": 2, "three": 3}

# Значения извлекаются так же, как из списка, с той лишь разницей,
# что индекс — у словарей он называется ключом — не обязан быть числом
filled_dict["one"] #=> 1

# Все ключи в виде списка получаются с помощью метода keys(). 
# Его вызов нужно обернуть в list(), так как обратно мы получаем
# итерируемый объект, о которых поговорим позднее.
list(filled_dict.keys())   # => ["three", "two", "one"]
# Замечание: сохранение порядка ключей в словаре не гарантируется
# Ваши результаты могут не совпадать с этими.

# Все значения в виде списка можно получить с помощью values().
# И снова нам нужно обернуть вызов в list(), чтобы превратить
# итерируемый объект в список.
list(filled_dict.values())   # => [3, 2, 1]
# То же самое замечание насчёт порядка ключей справедливо и здесь

# При помощи оператора in можно проверять ключи на вхождение в словарь
"one" in filled_dict #=> True
1 in filled_dict #=> False

# Попытка получить значение по несуществующему ключу выбросит ошибку ключа
filled_dict["four"] # KeyError

# Чтобы избежать этого, используйте метод get()
filled_dict.get("one") #=> 1
filled_dict.get("four") #=> None
# Метод get также принимает аргумент по умолчанию, значение которого будет
# возвращено при отсутствии указанного ключа
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4

# Метод setdefault вставляет пару ключ-значение, только если такого ключа нет
filled_dict.setdefault("five", 5) #filled_dict["five"] возвращает 5
filled_dict.setdefault("five", 6) #filled_dict["five"] по-прежнему возвращает 5

# Добавление элементов в словарь
filled_dict.update({"four":4}) #=> {"one": 1, "two": 2, "three": 3, "four": 4}
#filled_dict["four"] = 4  # Другой способ добавления элементов

# Удаляйте ключи из словаря с помощью оператора del
del filled_dict["one"]  # Удаляет ключ «one» из словаря


# Множества содержат... ну, в общем, множества
empty_set = set()
# Инициализация множества набором значений.
# Да, оно выглядит примерно как словарь… ну извините, так уж вышло.
filled_set = {1, 2, 2, 3, 4} # => {1, 2, 3, 4}

# Множеству можно назначать новую переменную
filled_set = some_set

# Добавление новых элементов в множество
filled_set.add(5) # filled_set равно {1, 2, 3, 4, 5}

# Пересечение множеств: &
other_set = {3, 4, 5, 6}
filled_set & other_set #=> {3, 4, 5}

# Объединение множеств: |
filled_set | other_set #=> {1, 2, 3, 4, 5, 6}

# Разность множеств: -
{1,2,3,4} - {2,3,5} #=> {1, 4}

# Проверка на вхождение во множество: in
2 in filled_set #=> True
10 in filled_set #=> False


####################################################
## 3. Поток управления и итерируемые объекты
####################################################

# Для начала заведём переменную
some_var = 5

# Так выглядит выражение if. Отступы в python очень важны!
# результат: «some_var меньше, чем 10»
if some_var > 10:
    print("some_var намного больше, чем 10.")
elif some_var < 10:    # Выражение elif необязательно.
    print("some_var меньше, чем 10.")
else:           # Это тоже необязательно.
    print("some_var равно 10.")


# Циклы For проходят по спискам. Результат:
    # собака — это млекопитающее
    # кошка — это млекопитающее
    # мышь — это млекопитающее
for animal in ["собака", "кошка", "мышь"]:
    # Можете использовать format() для интерполяции форматированных строк
    print("{} — это млекопитающее".format(animal))
    
"""
«range(число)» возвращает список чисел
от нуля до заданного числа
Результат:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
Циклы while продолжаются до тех пор, пока указанное условие не станет ложным.
Результат:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Краткая запись для x = x + 1

# Обрабатывайте исключения блоками try/except
try:
    # Чтобы выбросить ошибку, используется raise
    raise IndexError("Это ошибка индекса")
except IndexError as e:
    # pass — это просто отсутствие оператора. Обычно здесь происходит
    # восстановление после ошибки.
    pass
except (TypeError, NameError):
    pass    # Несколько исключений можно обработать вместе, если нужно.
else:   # Необязательное выражение. Должно следовать за последним блоком except
    print("Всё хорошо!")   # Выполнится, только если не было никаких исключений

# Python предоставляет фундаментальную абстракцию,
# которая называется итерируемым объектом (an iterable).
# Итерируемый объект — это объект, который воспринимается как последовательность.
# Объект, который возвратила функция range(), итерируемый.
filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable) #=> range(1,10). Это объект, реализующий интерфейс iterable

# Мы можем проходить по нему циклом.
for i in our_iterable:
    print(i)    # Выводит one, two, three

# Но мы не можем обращаться к элементу по индексу.
our_iterable[1]  # Выбрасывает ошибку типа

# Итерируемый объект знает, как создавать итератор.
our_iterator = iter(our_iterable)

# Итератор может запоминать состояние при проходе по объекту.
# Мы получаем следующий объект, вызывая функцию __next__.
our_iterator.__next__()  #=> "one"

# Он сохраняет состояние при вызове __next__.
our_iterator.__next__()  #=> "two"
our_iterator.__next__()  #=> "three"

# Возвратив все данные, итератор выбрасывает исключение StopIterator
our_iterator.__next__() # Выбрасывает исключение остановки итератора

# Вы можете получить сразу все элементы итератора, вызвав на нём функцию list().
list(filled_dict.keys())  #=> Возвращает ["one", "two", "three"]


####################################################
## 4. Функции
####################################################

# Используйте def для создания новых функций
def add(x, y):
    print("x равен %s, а y равен %s" % (x, y))
    return x + y    # Возвращайте результат с помощью ключевого слова return

# Вызов функции с аргументами
add(5, 6) #=> выводит «x равен 5, а y равен 6» и возвращает 11

# Другой способ вызова функции — вызов с именованными аргументами
add(y=6, x=5)   # Именованные аргументы можно указывать в любом порядке.

# Вы можете определить функцию, принимающую переменное число аргументов
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)


# А также можете определить функцию, принимающую переменное число
# именованных аргументов
def keyword_args(**kwargs):
    return kwargs

# Вызовем эту функцию и посмотрим, что из этого получится
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# Если хотите, можете использовать оба способа одновременно
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) выводит:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Вызывая функции, можете сделать наоборот!
# Используйте символ * для распаковки кортежей и ** для распаковки словарей
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # эквивалентно foo(1, 2, 3, 4)
all_the_args(**kwargs) # эквивалентно foo(a=3, b=4)
all_the_args(*args, **kwargs) # эквивалентно foo(1, 2, 3, 4, a=3, b=4)

# Область определения функций
x = 5

def setX(num):
    # Локальная переменная x — это не то же самое, что глобальная переменная x
    x = num # => 43
    print (x) # => 43
    
def setGlobalX(num):
    global x
    print (x) # => 5
    x = num # Глобальная переменная x теперь равна 6
    print (x) # => 6

setX(43)
setGlobalX(6)

# В Python функции — «объекты первого класса»
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# Также есть и анонимные функции
(lambda x: x > 2)(3) #=> True

# Есть встроенные функции высшего порядка
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Для удобного отображения и фильтрации можно использовать списочные включения
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]

####################################################
## 5. Классы
####################################################

# Чтобы получить класс, мы наследуемся от object.
class Human(object):

    # Атрибут класса. Он разделяется всеми экземплярами этого класса
    species = "H. sapiens"

    # Обычный конструктор, вызывается при инициализации экземпляра класса
    # Обратите внимание, что двойное подчёркивание в начале и в конце имени
    # означает объекты и атрибуты, которые используются Python, но находятся
    # в пространствах имён, управляемых пользователем.
    # Не придумывайте им имена самостоятельно.
    def __init__(self, name):
        # Присваивание значения аргумента атрибуту класса name
        self.name = name

    # Метод экземпляра. Все методы принимают self в качестве первого аргумента
    def say(self, msg):
        return "{name}: {message}".format(name=self.name, message=msg)

    # Метод класса разделяется между всеми экземплярами
    # Они вызываются с указыванием вызывающего класса в качестве первого аргумента
    @classmethod
    def get_species(cls):
        return cls.species

    # Статический метод вызывается без ссылки на класс или экземпляр
    @staticmethod
    def grunt():
        return "*grunt*"


# Инициализация экземпляра класса
i = Human(name="Иван")
print(i.say("привет"))     # Выводит: «Иван: привет»

j = Human("Пётр")
print(j.say("Привет"))  # Выводит: «Пётр: привет»

# Вызов метода класса
i.get_species() #=> "H. sapiens"

# Изменение разделяемого атрибута
Human.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# Вызов статического метода
Human.grunt() #=> "*grunt*"


####################################################
## 6. Модули
####################################################

# Вы можете импортировать модули
import math
print(math.sqrt(16)) #=> 4.0

# Вы можете импортировать отдельные функции модуля
from math import ceil, floor
print(ceil(3.7))  #=> 4.0
print(floor(3.7)) #=> 3.0

# Можете импортировать все функции модуля.
# (Хотя это и не рекомендуется)
from math import *

# Можете сокращать имена модулей
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Модули в Python — это обычные Python-файлы. Вы
# можете писать свои модули и импортировать их. Название
# модуля совпадает с названием файла.

# Вы можете узнать, какие функции и атрибуты определены 
# в модуле
import math
dir(math)

####################################################
## 7. Дополнительно
####################################################

# Генераторы помогут выполнить ленивые вычисления
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# Генератор создаёт значения на лету.
# Он не возвращает все значения разом, а создаёт каждое из них при каждой
# итерации.  Это значит, что значения больше 15 в double_numbers
# обработаны не будут.
# Обратите внимание: range — это тоже генератор.
# Создание списка чисел от 1 до 900000000 требует много места и времени.
# Если нам нужно имя переменной, совпадающее с ключевым словом Python,
# мы используем подчёркивание в конце
range_ = range(1, 900000000)

# Будет удваивать все числа, пока результат не превысит 30
for i in double_numbers(range_):
    print(i)
    if i >= 30:
        break


# Декораторы
# В этом примере beg оборачивает say
# Метод beg вызовет say. Если say_please равно True,
# он изменит возвращаемое сообщение
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, " Пожалуйста! У меня нет денег :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Вы не купите мне пива?"
    return msg, say_please


print(say())  # Вы не купите мне пива?
print(say(say_please=True)) # Вы не купите мне пива? Пожалуйста! У меня нет денег :(

```

## Хотите ещё?

### Бесплатные онлайн-материалы

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [Официальная документация](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/3/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)

### Платные

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
category: tool
tool: Qt Framework
language: c++
filename: learnqt-ru.cpp
contributors:
    - ["Aleksey Kholovchuk", "https://github.com/vortexxx192"]
translators:
    - ["Evan K.", "https://github.com/justblah"]
lang: ru-ru
---

**Qt** является широко известным фреймворком для разработки кросс-платформенного программного обеспечения, которое может быть запущено на различных программно-аппаратных платформах практически без изменений в коде, сохраняя при этом мощность и скорость нативных приложений. Хоть **Qt** и был изначально написан на *C++*, у него есть реализации и на других языках: *PyQt*, *QtRuby*, *PHP-Qt* и т.д.

**Qt** отлично подходит для создания приложений с графическим пользовательским интерфейсом (GUI). Это руководство о том, как сделать это на *C++*.

```c++
/*
 * Начнём по-старинке
 */

// все header файлы импортированные из Qt начинаются с заглавной 'Q'
#include <QApplication>
#include <QLineEdit>

int main(int argc, char *argv[]) {
    // создаем объект для управления данными приложения
    QApplication app(argc, argv);

    // создаем редактируемую строку и отобразим её на экране
    QLineEdit lineEdit("Hello world!");
    lineEdit.show();

    // запускаем цикл для обработки событий (event loop)
    return app.exec();
}
```

GUI часть **Qt** полностью состоит из *виджетов* и *связей* между ними.

[(EN) ПОДРОБНЕЕ О ВИДЖЕТАХ](http://doc.qt.io/qt-5/qtwidgets-index.html)

```c++
/*
 * В этом примере мы отобразим надпись с кнопкой.
 * Надпись будет появляться после нажатия на кнопку.
 *
 * Код на Qt говорит сам за себя.
 */

#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QDialog dialogWindow;
    dialogWindow.show();

    // добавляем вертикальное расположение
    QVBoxLayout layout;
    dialogWindow.setLayout(&layout);  

    QLabel textLabel("Thanks for pressing that button");
    layout.addWidget(&textLabel);
    textLabel.hide();

    QPushButton button("Press me");
    layout.addWidget(&button);

    // отображаем скрытую надпись после нажатия на кнопку
    QObject::connect(&button, &QPushButton::pressed,
                     &textLabel, &QLabel::show);

    return app.exec();
}
```
Обратите внимание на метод *QObject::connect*. Этот метод соединяет *СИГНАЛЫ* одного объекта со *СЛОТАМИ* другого.

**Сигналы** отправляются когда с объектами происходят отпределённые события, например, сигнал *нажатие* отправляется когда пользователь нажимает на объект типа QPushButton.

**Слоты** это *действия*, которые могут быть выполнены в ответ на полученные сигналы.

[(EN) ПОДРОБНЕЕ О СЛОТАХ И СИГНАЛАХ](http://doc.qt.io/qt-4.8/signalsandslots.html)


Далее рассмотрим, как можно не только использовать стандартные виджеты, но и расширять их поведение с помощью наследования. Давайте создадим кнопку и посчитаем, сколько раз она была нажата. Для этого мы определяем наш собственный класс *CounterLabel*. Он должен быть объявлен в отдельном файле из-за специфической архитектуры Qt.

```c++
// counterlabel.hpp

#ifndef COUNTERLABEL
#define COUNTERLABEL

#include <QLabel>

class CounterLabel : public QLabel {
    Q_OBJECT  // макрос Qt, обязателен для всех виджетов

public:
    CounterLabel() : counter(0) {
        setText("Counter has not been increased yet");  // метод QLabel
    }

public slots:
    // действие, которое будет вызвано в ответ на нажатие
    void increaseCounter() {
        setText(QString("Counter value: %1").arg(QString::number(++counter)));
    }

private:
    int counter;
};

#endif // COUNTERLABEL
```

```c++
// main.cpp
// Почти тоже самое, что и в предыдущем примере

#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QString>
#include "counterlabel.hpp"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QDialog dialogWindow;
    dialogWindow.show();

    QVBoxLayout layout;
    dialogWindow.setLayout(&layout);

    CounterLabel counterLabel;
    layout.addWidget(&counterLabel);

    QPushButton button("Push me once more");
    layout.addWidget(&button);
    QObject::connect(&button, &QPushButton::pressed,
                     &counterLabel, &CounterLabel::increaseCounter);

    return app.exec();
}
```

## На почитать
Это всё! Конечно, фреймворк Qt намного объемнее, чем часть, которая была рассмотрена в этом руководстве, так что будьте готовы читать и практиковаться.

[(EN) ДОКУМЕНТАЦИЯ](http://wiki.qt.io/Main/ru)

Удачи!
---
language: ruby
lang: ru-ru
filename: learnruby-ru.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
translators:
  - ["Alexey Makarov", "https://github.com/Anakros"]
---

```ruby
# Это комментарий

=begin
Это многострочный комментарий
Никто их не использует
И они не рекомендуются к использованию
=end

# Первое и самое главное: Всё является объектом.

# Числа это объекты

3.class #=> Fixnum

3.to_s #=> "3"


# Немного простой арифметики
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# Арифметика -- это синтаксический сахар
# над вызовом метода для объекта
1.+(3) #=> 4
10.* 5 #=> 50

# Логические величины -- это объекты
nil # Здесь ничего нет
true # истина
false # ложь

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Операция равенства
1 == 1 #=> true
2 == 1 #=> false

# Операция неравенства
1 != 1 #=> false
2 != 1 #=> true
!true  #=> false
!false #=> true

# nil -- имеет такое же логическое значение, как и false

!nil   #=> true
!false #=> true
!0     #=> false

# Больше операций сравнения
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Строки -- это объекты

'Я строка'.class #=> String
"Я тоже строка".class #=> String

placeholder = "использовать интерполяцию строк"
"Я могу #{placeholder}, когда создаю строку с двойными кавычками"
#=> "Я могу использовать интерполяцию строк,
# когда создаю строку с двойными кавычками"


# печатать в стандартный вывод
puts "Я печатаюсь!"

# Переменные
x = 25 #=> 25
x #=> 25

# Присваивание значения возвращает то самое присвоенное значение.
# Это позволяет делать множественные присваивания:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# По соглашению, используйте snake_case для имён переменных
snake_case = true

# Используйте подробные имена для переменных
# Но не переборщите!
path_to_project_root = '/good/name/'
path = '/bad/name/'

# Идентификаторы (тоже объекты)

# Идентификаторы -- это неизменяемые, многоразовые константы. 
# Для каждого идентификатора (кроме текста) сохраняется цифровой хэш.
# При последующем использовании идентификатора, заместо создания нового объекта,
# будет найден уже существующий по цифровому хэшу.
# Они часто используются вместо строк для ускорения работы приложений

:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# Массивы

# Это массив
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Массив может содержать различные типы значений

[1, "hello", false] #=> [1, "hello", false]

# Значение в массиве можно получить по индексу с левой границы
array[0] #=> 1
array[12] #=> nil

# Как и арифметика, доступ к значению в массиве
# это синтаксический сахар над вызовом метода для объекта
array.[] 0 #=> 1
array.[] 12 #=> nil

# Также, можно получить по индексу с правой границы
array[-1] #=> 5

# С заданными левой и правой границами индексов
array[2, 4] #=> [3, 4, 5]

# Или с использованием диапазона значений
array[1..3] #=> [2, 3, 4]

# Вот так можно добавить значение в массив
array << 6 #=> [1, 2, 3, 4, 5, 6]

# Хэши -- это массив пар "ключ => значение".
# Хэши объявляются с использованием фигурных скобок:
hash = {'color' => 'green', 'number' => 5}

hash.keys #=> ['color', 'number']
hash.values #=> ['green', 5]

# Значение в хэше легко может быть найдено по ключу:
hash['color'] #=> 'green'
hash['number'] #=> 5

# Поиск по ключу, которого в хэше нет вернёт nil:
hash['nothing here'] #=> nil

# начиная с Ruby 1.9, существует специальный синтаксис 
# при использовании идентификаторов как ключей хэша:

new_hash = { defcon: 3, action: true}

new_hash.keys #=> [:defcon, :action]

# Массивы и Хэши -- перечисляемые типы данных
# У них есть много полезных методов, например: each, map, count и другие

# Управление ходом выполнения (Управляющие структуры)

if true
  "Если истина"
elsif false
  "Иначе, если ложь (опционально)"
else
  "Во всех других случаях"
end

for counter in 1..5
  puts "итерация #{counter}"
end
#=> итерация 1
#=> итерация 2
#=> итерация 3
#=> итерация 4
#=> итерация 5

# Однако, никто не использует "for" для циклов.
# Вместо него Вы должны использовать метод "each" вместе с блоком кода.
#
# Блок кода -- это один из вариантов создания замыканий (лямбды,
# анонимные функции).
# Блок может только передаваться методу, сам по себе он существовать не может.
# "for" не имеет своей области видимости и все переменные, объявленные в нём
# будут доступны отовсюду. "each" вместе с блоком создаёт свою область видимости

# Метод "each" для диапазона значений запускает блок кода один раз
# для каждого из значений диапазона
# Блок передаёт счётчик (counter) в качестве параметра.
# Вызов метода "each" с блоком выглядит следующим образом:

(1..5).each do |counter|
  puts "итерация #{counter}"
end
#=> итерация 1
#=> итерация 2
#=> итерация 3
#=> итерация 4
#=> итерация 5

# Вы также можете ограничивать блоки фигурными скобками:
(1..5).each {|counter| puts "итерация #{counter}"}

# Содержимое структурных данных также можно перебирать используя "each":
array.each do |element|
  puts "#{element} -- часть массива"
end
hash.each do |key, value|
  puts "#{key} -- это #{value}"
end

counter = 1
while counter <= 5 do
  puts "итерация #{counter}"
  counter += 1
end
#=> итерация 1
#=> итерация 2
#=> итерация 3
#=> итерация 4
#=> итерация 5

grade = 'B'

case grade
when 'A'
  puts "Так держать, детка!"
when 'B'
  puts "Тебе повезёт в следующий раз"
when 'C'
  puts "Ты можешь сделать лучше"
when 'D'
  puts "Выскоблил последнее"
when 'F'
  puts "Ты провалился!"
else
  puts "Альтернативная система оценок, да?"
end

# Функции

def double(x)
  x * 2
end

# Функции (и все блоки) неявно возвращают значение последней операции
double(2) #=> 4

# Скобки необязательны, если возвращаемый результат однозначен
double 3 #=> 6

double double 3 #=> 12

def sum(x,y)
  x + y
end

# Аргументы метода разделены запятой
sum 3, 4 #=> 7

sum sum(3,4), 5 #=> 12

# yield
# Все методы имеют неявный, опциональный параметр,
# который может быть вызван с помощью инструкции "yield"

def surround
  puts "{"
  yield
  puts "}"
end

surround { puts 'hello world' }

# {
# hello world
# }


# Определение класса с помощью ключевого слова "class"
class Human

  # Переменная класса, она является общей для всех экземпляров класса
  @@species = "H. sapiens"

  # Базовый метод-конструктор
  def initialize(name, age=0)
    # Присвоить аргумент "name" переменной "name" экземпляра класса
    @name = name
    # Если аргумент "age" не задан,
    # мы используем значение по умолчанию из списка аргументов
    @age = age
  end

  # Базовый метод установки значения для переменной (setter)
  def name=(name)
    @name = name
  end

  # Базовый метод получения значения переменной (getter)
  def name
    @name
  end

  # Метод класса определяется с ключевым словом "self",
  # чтобы можно было отличить его от метода экземпляра класса.
  # Он может быть вызван только на уровне класса, но не экземпляра.
  def self.say(msg)
    puts "#{msg}"
  end

  def species
    @@species
  end

end


# Создание экземпляра класса
jim = Human.new("Jim Halpert")

dwight = Human.new("Dwight K. Schrute")

# Давайте вызовем несколько методов
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# Вызов метода класса
Human.say("Hi") #=> "Hi"

# Область видимости переменной определяется тем, как мы даём имя переменной.
# Переменные, имя которых начинается с "$" имеют глобальную область видимости
$var = "I'm a global var"
defined? $var #=> "global-variable"

# Переменная экземпляра класса, она видна только в экземпляре
@var = "I'm an instance var"
defined? @var #=> "instance-variable"

# Переменная класса, видна для всех экземпляров этого класса и в самом классе
@@var = "I'm a class var"
defined? @@var #=> "class variable"

# Имена переменных с большой буквы используются для создания констант
Var = "I'm a constant"
defined? Var #=> "constant"

# Класс тоже объект в Ruby. Класс может иметь переменные экземпляра.
# Переменная класса доступна в классе, его экземплярах и его потомках.

# Пример класса
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

#  Производный класс (класс-потомок)
class Worker < Human
end

Human.foo # 0
Worker.foo # 0

Human.foo = 2 # 2
Worker.foo # 2

# Переменная экземпляра класса недоступна в потомках этого класса.

class Human
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doctor < Human
end

Human.bar # 0
Doctor.bar # nil

module ModuleExample
  def foo
    'foo'
  end
end

# Включение модулей в класс добавляет их методы в экземпляр класса
# Или в сам класс, зависит только от метода подключения
class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => NoMethodError: undefined method `foo'

# Коллбэки при подключении модуля

module ConcernExample
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class Something
  include ConcernExample
end

Something.bar     # => 'bar'
Something.qux     # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
```
---
language: swift
contributors:
  - ["Grant Timmerman", "http://github.com/grant"]
  - ["Christopher Bess", "http://github.com/cbess"]
  - ["Joey Huang", "http://github.com/kamidox"]
  - ["Alexey Nazaroff", "http://github.com/rogaven"]
filename: learnswift-ru.swift
translators:
  - ["Dmitry Bessonov", "https://github.com/TheDmitry"]
  - ["Alexey Nazaroff", "https://github.com/rogaven"]
lang: ru-ru
---

Swift - это язык программирования, созданный компанией Apple, для приложений
под iOS и OS X. Разработанный, чтобы сосуществовать с Objective-C и
быть более устойчивым к ошибочному коду, Swift был представлен в 2014 году на
конференции разработчиков Apple, WWDC. Приложения на Swift собираются
с помощью LLVM-компилятора, включенного в Xcode 6+.

Официальная книга по [языку программирования Swift](https://itunes.apple.com/us/book/swift-programming-language/id881256329) от Apple доступна в iBooks.

Смотрите еще [начальное руководство](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/RoadMapiOS/index.html) Apple, которое содержит полное учебное пособие по Swift.

```swift
// Версия Swift: 3.0

// импорт модуля
import UIKit

//
// MARK: Основы
//

// Xcode поддерживает маркеры, чтобы давать примечания своему коду
// и вносить их в список обозревателя (Jump Bar)
// MARK: Метка раздела
// MARK: - Метка с разделителем
// TODO: Сделайте что-нибудь вскоре
// FIXME: Исправьте этот код

// Начиная со второй версии Swift, println и print объединены в методе print.
// Перенос строки теперь добавляется в конец автоматически.
print("Привет, мир!") // println – теперь просто print
print("Привет, мир!", terminator: "") // вывод текста без переноса строки

// переменные (var), значение которых можно изменить после инициализации
// константы (let), значение которых нельзя изменить после инициализации

var myVariable = 42
let øπΩ = "значение" // именование переменной символами unicode
let π = 3.1415926
let convenience = "Ключевое слово" // контекстное имя переменной
let weak = "Ключевое слово"; let override = "еще ключевое слово" // операторы
                                      // могут быть отделены точкой с запятой
let `class` = "Ключевое слово" // обратные апострофы позволяют использовать
                               // ключевые слова в именовании переменных
let explicitDouble: Double = 70
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "некоторый текст " + String(myVariable) // Приведение типа
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // Вставка переменных в строку

// Сборка особых значений
// используя ключ -D сборки конфигурации
#if false
    print("Не печатается")
    let buildValue = 3
#else
    let buildValue = 7
#endif
print("Значение сборки: \(buildValue)") // Значение сборки: 7

/*
    Опционалы - это особенность языка Swift, которая допускает вам сохранять
    `некоторое` или `никакое` значения.

    Язык Swift требует, чтобы каждое свойство имело значение, поэтому даже nil
    должен быть явно сохранен как опциональное значение.

    Optional<T> является перечислением.
*/
var someOptionalString: String? = "опционал" // Может быть nil
// как и выше, только ? - это постфиксный оператор (синтаксический сахар)
var someOptionalString2: Optional<String> = "опционал"

if someOptionalString != nil {
    // я не nil
    if someOptionalString!.hasPrefix("opt") {
        print("содержит префикс")
    }

    let empty = someOptionalString?.isEmpty
}
someOptionalString = nil

/*
Использование ! для доступа к несуществующему опциональному значению генерирует
рантайм ошибку. Всегда проверяйте, что опционал содержит не пустое значение,
перед тем как раскрывать его через !.
*/

// неявная развертка опциональной переменной
var unwrappedString: String! = "Ожидаемое значение."
// как и выше, только ! - постфиксный оператор (с еще одним синтаксическим сахаром)
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Ожидаемое значение."

// If let конструкции -
// If let это специальная конструкция в Swift, которая позволяет проверить Optional
// справа от `=` непустой, и если это так - разворачивает его и присваивает левой части.
if let someOptionalStringConstant = someOptionalString {
    // имеется некоторое (`Some`) значение, не `nil`
    if !someOptionalStringConstant.hasPrefix("ok") {
        // нет такого префикса
    }
}

// Swift поддерживает сохранение значения любого типа
// Для этих целей есть два ключевых слова `Any` и `AnyObject`
// AnyObject == id
// `Any` же, в отличие от `id` в Objective-C, `Any` работает с любым значением (Class, Int, struct и т.д.)
var anyVar: Any = 7
anyVar = "Изменять значение на строку не является хорошей практикой, но возможно."
let anyObjectVar: AnyObject = Int(1) as NSNumber

/*
    Комментируйте здесь

    /*
        Вложенные комментарии тоже поддерживаются
    */
*/

//
// MARK: Коллекции
//

/*
    Массив (Array) и словарь (Dictionary) являются структурами (struct). Так
    `let` и `var` также означают, что они изменяются (var) или не изменяются (let)
    при объявлении переменных этих типов.
*/

// Массив
var shoppingList = ["сом", "вода", "лимоны"]
shoppingList[1] = "бутылка воды"
let emptyArray = [String]() // let == неизменный
let emptyArray2 = Array<String>() // как и выше
var emptyMutableArray = [String]() // var == изменяемый
var explicitEmptyMutableStringArray: [String] = [] // так же как и выше


// Словарь
var occupations = [
    "Malcolm": "Капитан",
    "kaylee": "Техник"
]
occupations["Jayne"] = "Связи с общественностью"
let emptyDictionary = [String: Float]() // let == неизменный
let emptyDictionary2 = Dictionary<String, Float>() // как и выше
var emptyMutableDictionary = [String: Float]() // var == изменяемый
var explicitEmptyMutableDictionary: [String: Float] = [:] // то же


//
// MARK: Поток управления
//

// С помощью "," можно указать дополнительные условия для раскрытия
// опциональных значений.
let someNumber = Optional<Int>(7)
if let num = someNumber, num > 3 {
    print("Больше 3х")
}

// цикл for для массива
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
    if value == 1 {
        print("Один!")
    } else {
        print("Не один!")
    }
}

// цикл for для словаря
var dict = ["один": 1, "два": 2]
for (key, value) in dict {
    print("\(key): \(value)")
}

// цикл for для диапазона чисел
for i in -1...shoppingList.count {
    print(i)
}
shoppingList[1...2] = ["бифштекс", "орехи пекан"]
// используйте ..< для исключения последнего числа

// цикл while
var i = 1
while i < 1000 {
    i *= 2
}

// цикл do-while
repeat {
    print("привет")
} while 1 == 2

// Переключатель
// Очень мощный оператор, представляйте себе операторы `if` с синтаксическим
// сахаром
// Они поддерживают строки, объекты и примитивы (Int, Double, etc)
let vegetable = "красный перец"
switch vegetable {
case "сельдерей":
    let vegetableComment = "Добавьте немного изюма, имитируя муравьев на бревнышке."
case "огурец", "кресс-салат":
    let vegetableComment = "Было бы неплохо сделать бутерброд с чаем."
case let localScopeValue where localScopeValue.hasSuffix("перец"):
    let vegetableComment = "Это острый \(localScopeValue)?"
default: // обязательный (чтобы предусмотреть все возможные вхождения)
    let vegetableComment = "В супе все овощи вкусные."
}


//
// MARK: Функции
//

// Функции являются типом первого класса, т.е. они могут быть вложены в функциях
// и могут передаваться между собой

// Функция с документированным заголовком Swift (формат Swift-модифицированный Markdown)

/**
    Операция приветствия

    - Маркер в документировании
    - Еще один маркер в документации

    - Parameter name	: Это имя
    - Parameter day	: Это день
    - Returns : Строка, содержащая значения name и day.
*/
func greet(name: String, day: String) -> String {
    return "Привет \(name), сегодня \(day)."
}
greet(name: "Боб", day: "вторник")

// как и выше, кроме обращения параметров функции
func greet2(name: String, externalParamName localParamName: String) -> String {
    return "Привет \(name), сегодня \(localParamName)"
}
greet2(name: "Иван", externalParamName: "Воскресенье")

// Функция, которая возвращает множество элементов в кортеже
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// Пропускайте значения кортежей с помощью подчеркивания _
let (_, price1, _) = pricesTuple // price1 == 3.69
print(price1 == pricesTuple.1) // вывод: true
print("Цена газа: \(price)")

// Именованные параметры кортежа
func getGasPrices2() -> (lowestPrice: Double, highestPrice: Double, midPrice: Double) {
    return (1.77, 37.70, 7.37)
}
let pricesTuple2 = getGasPrices2()
let price2 = pricesTuple2.lowestPrice
let (_, price3, _) = pricesTuple2
print(pricesTuple2.highestPrice == pricesTuple2.1) // вывод: true
print("Самая высокая цена за газ: \(pricesTuple2.highestPrice)")

// guard утверждения
func testGuard() {
    // guards обеспечивают прерывание дальнейшего выполнения функции,
    // позволяя держать обработчики ошибок рядом с проверкой условия
    // Объявляемая переменная находится в той же области видимости, что и guard.
    guard let aNumber = Optional<Int>(7) else {
        return
    }

    print("число равно \(aNumber)")
}
testGuard()

// Переменное число аргументов
func setup(numbers: Int...) {
    // это массив
    let number = numbers[0]
    let argCount = numbers.count
}

// Передача и возврат функций
func makeIncrementer() -> ((Int) -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

// передача по ссылке
func swapTwoInts(a: inout Int, b: inout Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(a: &someIntA, b: &someIntB)
print(someIntB) // 7


//
// MARK: Замыкания
//
var numbers = [1, 2, 6]

// Функции - это частный случай замыканий ({})

// Пример замыкания.
// `->` отделяет аргументы и возвращаемый тип
// `in` отделяет заголовок замыкания от тела замыкания
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})

// Когда тип известен, как и выше, мы можем сделать так
numbers = numbers.map({ number in 3 * number })
// Или даже так
//numbers = numbers.map({ $0 * 3 })

print(numbers) // [3, 6, 18]

// Хвостовое замыкание
numbers = numbers.sorted { $0 > $1 }

print(numbers) // [18, 6, 3]

// Суперсокращение, поскольку оператор < выполняет логический вывод типов

numbers = numbers.sorted(by: <)

print(numbers) // [3, 6, 18]

//
// MARK: Структуры
//

// Структуры и классы имеют очень похожие характеристики
struct NamesTable {
    let names: [String]

    // Пользовательский индекс
    subscript(index: Int) -> String {
        return names[index]
    }
}

// У структур автогенерируемый (неявно) инициализатор
let namesTable = NamesTable(names: ["Иван", "Яков"])
let name = namesTable[1]
print("Имя :\(name)") // Имя: Яков

//
// MARK: Обработка ошибок
//

// Протокол `Error` используется для перехвата выбрасываемых ошибок
enum MyError: Error {
    case BadValue(msg: String)
    case ReallyBadValue(msg: String)
}

// фунции помеченные словом `throws` должны вызываться с помощью `try`
func fakeFetch(value: Int) throws -> String {
    guard 7 == value else {
        throw MyError.ReallyBadValue(msg: "Действительно плохое значение")
    }

    return "тест"
}

func testTryStuff() {
    // предполагается, что не будет выброшено никаких ошибок,
    // в противном случае мы получим рантайм исключение
    let _ = try! fakeFetch(value: 7)

    // Если возникает ошибка, то выполнение продолжится. Но если значение равно nil,
    // то результат будет опционалом
    let _ = try? fakeFetch(value: 7)

    do {
        // обычно try оператор, позволяющий обработать ошибку в `catch` блоке
        try fakeFetch(value: 1)
    } catch MyError.BadValue(let msg) {
        print("Ошибка: \(msg)")
    } catch {
        // все остальное
    }
}
testTryStuff()

//
// MARK: Классы
//

// Классы, структуры и их члены имеют трехуровневый контроль доступа
// Уровни: internal (по умолчанию), public, private

public class Shape {
    public func getArea() -> Int {
        return 0
    }
}

// Все методы и свойства класса являются открытыми (public).
// Если вам необходимо содержать только данные
// в структурированном объекте, вы должны использовать `struct`

internal class Rect: Shape {
    var sideLength: Int = 1

    // Пользовательский сеттер и геттер
    private var perimeter: Int {
        get {
            return 4 * sideLength
        }
        set {
            // `newValue` - неявная переменная, доступная в сеттере
            sideLength = newValue / 4
        }
    }

    // Вычисляемые свойства должны быть объявлены с помощью `var`, ведь они могут меняться
    var smallestSideLength: Int {
        return self.sideLength - 1
    }

    // Ленивая загрузка свойства
    // свойство subShape остается равным nil (неинициализированным),
    // пока не вызовется геттер
    lazy var subShape = Rect(sideLength: 4)

    // Если вам не нужны пользовательские геттеры и сеттеры,
    // но все же хотите запустить код перед и после вызовов геттера или сеттера
    // свойств, вы можете использовать `willSet` и `didSet`
    var identifier: String = "defaultID" {
        // аргумент у `willSet` будет именем переменной для нового значения
        willSet(someIdentifier) {
            print(someIdentifier)
        }
    }

    init(sideLength: Int) {
        self.sideLength = sideLength
        // последним всегда вызывается super.init, когда init с параметрами
        super.init()
    }

    func shrink() {
        if sideLength > 0 {
            sideLength -= 1
        }
    }

    override func getArea() -> Int {
        return sideLength * sideLength
    }
}

// Простой класс `Square` наследует `Rect`
class Square: Rect {
    convenience init() {
        self.init(sideLength: 5)
    }
}

var mySquare = Square()
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4

// преобразование объектов
let aShape = mySquare as Shape

// сравнение экземпляров, в отличие от ==, которая проверяет эквивалентность
if mySquare === mySquare {
    print("Ага, это mySquare")
}

// Опциональная инициализация (init)
class Circle: Shape {
    var radius: Int
    override func getArea() -> Int {
        return 3 * radius * radius
    }

    // Поместите постфиксный знак вопроса после `init` - это и будет опциональная инициализация,
    // которая может вернуть nil
    init?(radius: Int) {
        self.radius = radius
        super.init()

        if radius <= 0 {
            return nil
        }
    }
}

var myCircle = Circle(radius: 1)
print(myCircle?.getArea())    // Optional(3)
print(myCircle!.getArea())    // 3
var myEmptyCircle = Circle(radius: -1)
print(myEmptyCircle?.getArea())    // "nil"
if let circle = myEmptyCircle {
    // не будет выполняться, поскольку myEmptyCircle равен nil
    print("circle не nil")
}


//
// MARK: Перечисления
//

// Перечисления могут быть определенного или своего типа.
// Они могут содержать методы подобно классам.

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func getIcon() -> String {
        switch self {
        case .Spades: return "♤"
        case .Hearts: return "♡"
        case .Diamonds: return "♢"
        case .Clubs: return "♧"
        }
    }
}

// Значения перечислений допускают сокращенный синтаксис, нет необходимости
// указывать тип перечисления, когда переменная объявляется явно
var suitValue: Suit = .Hearts

// Значения нецелочисленных перечислений должны быть указаны явно
// или могут выводится с помощью функции `rawValue` из имени
enum BookName: String {
    case John
    case Luke = "Лука"
}
print("Имя: \(BookName.John.rawValue)")

// Перечисление (enum) со связанными значениями
enum Furniture {
    // Связать с типом Int
    case Desk(height: Int)
    // Связать с типами String и Int
    case Chair(String, Int)

    func description() -> String {
        switch self {
        case .Desk(let height):
            return "Письменный стол высотой \(height) см."
        case .Chair(let brand, let height):
            return "Стул марки \(brand) высотой \(height) см."
        }
    }
}

var desk: Furniture = .Desk(height: 80)
print(desk.description())     // "Письменный стол высотой 80 см."
var chair = Furniture.Chair("Foo", 40)
print(chair.description())    // "Стул марки Foo высотой 40 см."


//
// MARK: Протоколы
//

// `protocol` может потребовать, чтобы у соответствующих типов
// были определенные свойства экземпляра, методы экземпляра, тип методов,
// операторы и индексы.

protocol ShapeGenerator {
    var enabled: Bool { get set }
    func buildShape() -> Shape
}

// Протоколы, объявленные с @objc, допускают необязательные функции,
// которые позволяют вам проверять на соответствие. Для функций также необходимо указать @objc
@objc protocol TransformShape {
    @objc optional func reshape()
    @objc optional func canReshape() -> Bool
}

class MyShape: Rect {
    var delegate: TransformShape?

    func grow() {
        sideLength += 2

        // Размещайте знак вопроса перед опционным свойством, методом
        // или индексом, чтобы не учитывать nil-значение и возвратить nil
        // вместо выбрасывания ошибки выполнения (т.н. "опционная цепочка")
        if let reshape = self.delegate?.canReshape?(), reshape {
            // проверка делегата на выполнение метода
            self.delegate?.reshape?()
        }
    }
}


//
// MARK: Прочее
//

// `extension`s: Добавляет расширенный функционал к существующему типу

// Класс Square теперь "соответствует" протоколу `CustomStringConvertible`
extension Square: CustomStringConvertible {
    var description: String {
        return "Площадь: \(self.getArea()) - ID: \(self.identifier)"
    }
}

print("Объект Square: \(mySquare)")

// Вы также можете расширить встроенные типы
extension Int {
    var customProperty: String {
        return "Это \(self)"
    }

    func multiplyBy(num: Int) -> Int {
        return num * self
    }
}

print(7.customProperty) // "Это 7"
print(14.multiplyBy(num: 3)) // 42

// Обобщения: Подобно языкам Java и C#. Используйте ключевое слово `where`,
// чтобы определить условия обобщений.

func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
    for (index, value) in array.enumerated() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let foundAtIndex = findIndex(array: [1, 2, 3, 4], valueToFind: 3)
print(foundAtIndex == 2) // вывод: true

// Операторы:
// Пользовательские операторы могут начинаться с символов:
//      / = - + * % < > ! & | ^ . ~
// или
// Unicode- знаков математики, символов, стрелок, декорации и линий/кубов,
// нарисованных символов.
prefix operator !!!

// Префиксный оператор, который утраивает длину стороны, когда используется
prefix func !!! (shape: inout Square) -> Square {
    shape.sideLength *= 3
    return shape
}

// текущее значение
print(mySquare.sideLength) // 4

// Используя пользовательский оператор !!!, изменится длина стороны
// путем увеличения размера в 3 раза
!!!mySquare
print(mySquare.sideLength) // 12

// Операторы также могут быть обобщенными
infix operator <->
func <-><T: Equatable> (a: inout T, b: inout T) {
    let c = a
    a = b
    b = c
}

var foo: Float = 10
var bar: Float = 20

foo <-> bar
print("foo это \(foo), bar это \(bar)") // "foo = 20.0, bar = 10.0"
```
---
category: tool
tool: tmux
contributors:
  - ["mdln", "https://github.com/mdln"]
translators:
  - ["Davydov Anton", "https://github.com/davydovanton"]
filename: LearnTmux-ru.txt
lang: ru-ru
---

[tmux](http://tmux.sourceforge.net) - терминальный мультиплексор.
Он позволяет создавать, получать доступ и контролировать любое
количество терминалов из единого окна.
Сессия tmux также может быть свернута в фоновый режим, и она
будет работать в фоне, а после к ней можно будет подключиться.


```

  tmux [command]     # Запуск команды 'tmux'
                     # без какой-либо команды создаст новую сессию

    new              # Создать новую сессию
     -s "Session"    # Создать именованную сессию
     -n "Window"     # Создать именованное окно
     -c "/dir"       # Запустить сессию в конкретной директории

    attach           # Подключиться к последней/существующей сессии
     -t "№"          # Подключиться к определенной сессии
     -d              # Завершить определенную сессию

    ls               # Список открытых сессий
     -a              # Список всех открытых сессий

    lsw              # Список окон
     -a              # Список всех окон
     -s              # Список всех окон в сессии

    lsp              # Список панелей
     -a              # Список всех панелей
     -s              # Список всех панелей в сессии
     -t              # Список всех панелей для конкретного объекта

    kill-window      # Закрыть текущее окно
     -t "#"          # Закрыть конкретное окно
     -a              # Закрыть все окна
     -a -t "#"       # Закрыть все окна, кроме конкретного

    kill-session     # Завершить текущую сессию
     -t "#"          # Завершить конкретную сессию
     -a              # Завершить все сессии
     -a -t "#"       # Завершить все сессии, кроме конкретной

```


### "Горячие" клавиши

Способ, с помощью которого контролируется любая tmux
сессия, - комбинация клавиш, называемая 'Префиксом'.

```
----------------------------------------------------------------------
  (C-b) = Ctrl + b    # 'Префикс' необходим для
                      # использования горячих клавиш

  (M-1) = Meta + 1 -или- Alt + 1
----------------------------------------------------------------------

  ?                    # Список всех горячих клавиш
  :                    # Начать ввод в командной строке tmux
  r                    # Принудительная перерисовка текущего клиента
  c                    # Создать новое окно

  !                    # Переместить текущую панель в отдельное окно
  %                    # Разделить текущую панель на две: левую и правую
  "                    # Разделить текущую панель на две: верхнюю и нижнюю

  n                    # Переместиться на следующее окно
  p                    # Переместиться на предыдущее окно
  {                    # Заменить текущую панель на предыдущую
  }                    # Заменить текущую панель на следующую

  s                    # Интерактивный выбор запущенных сессий
  w                    # Интерактивный выбор текущего окна
  от 0 до 9            # Выбрать окно номер 0..9

  d                    # Отключить текущий клиент
  D                    # Выбрать клиент, который будет отключен

  &                    # Закрыть текущее окно
  x                    # Закрыть текущую панель

  Стрелки вверх, вниз  # Переместиться на панель выше, ниже, левее
  влево, вправо        # или правее

  M-1 to M-5           # Расставить панели:
                         # 1) выровнять по горизонтали
                         # 2) выровнять по вертикали
                         # 3) основное горизонтально
                         # 4) основное вертикально
                         # 5) мозаикой

  C-Up, C-Down         # Изменение размера текущей панели с шагом в одну
  C-Left, C-Right      # колонку

  M-Up, M-Down         # Изменение размера текущей панели с шагом в пять
  M-Left, M-Right      # колонок

```


### Настройка ~/.tmux.conf

Файл tmux.conf может быть использован для автоматической установки
опций при старте, как, например, .vimrc или init.el.

```
# Пример файла tmux.conf
# 2014.10


### Общее
###########################################################################

# Включить поддержку UTF-8
setw -g utf8 on
set-option -g status-utf8 on

# Установить лимит истории
set -g history-limit 2048

# Порядковый номер первой панели
set -g base-index 1

# Включить поддержку мыши
set-option -g mouse-select-pane on

# Принудительная перезагрузка конфигурационного файла
unbind r
bind r source-file ~/.tmux.conf


### Горячие клавиши
###########################################################################

# Отменить комбинацию C-b как стандартный префикс
unbind C-b

# Установить новую комбинацию как префикс
set-option -g prefix `

# Вернуть предыдущее окно, если префикс был нажат два раза
bind C-a last-window
bind ` last-window

# Разрешить замену C-a и ` на F11/F12
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Настройки клавиш
setw -g mode-keys vi
set-option -g status-keys vi

# Перемещение между панелями, как в vim
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Переключить/Заменить окно
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# Комманды, упрощающие разделением панелей
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# Активировать центральную сессию (когда вложенный tmux) для отправки команд
bind a send-prefix


### Цветовая схема
###########################################################################

# Цветовая палитра строки состояния
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# Цветовая палитра окантовки панели
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Цветовая палитра сообщений
set-option -g message-fg black
set-option -g message-bg green

# Цветовая палитра статус окна
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-content-attr default
setw -g window-status-content-fg yellow
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### Интерфейс
###########################################################################

# Уведомления
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# Автоматическая установка заголовка окна
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not)

# Настройки строки состояния
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# Показывать системные характеристики в статусбаре
# Требует https://github.com/thewtex/tmux-mem-cpu-load/
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```

### Ссылки

[Tmux | Домашняя страница](http://tmux.sourceforge.net)

[Страница мануала Tmux](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)

[Отображение CPU/MEM % в статусбаре](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)
---
language: TypeScript
lang: ru-ru
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
translators:
    - ["Fadil Mamedov", "https://github.com/fadilmamedov"]
    - ["Andre Polykanine", "https://github.com/Oire"]
filename: learntypescript-ru.ts
---

TypeScript — это язык программирования, целью которого является лёгкая разработка широкомасштабируемых JavaScript-приложений.
TypeScript добавляет в Javascript общие концепции, такие, как классы, модули, интерфейсы, обобщённое программирование и (опционально) статическую типизацию.  
Это надмножество языка JavaScript: весь JavaScript-код является валидным TypeScript-кодом, следовательно, может быть добавлен бесшовно в любой проект. 
Компилятор TypeScript генерирует JavaScript-код.

Эта статья концентрируется только на синтаксисе TypeScript, в противовес статье о [JavaScript](javascript-ru/).

Для тестирования компилятора TypeScript пройдите по ссылке в [песочницу](http://www.typescriptlang.org/Playground). 
Там вы можете написать код (с поддержкой автодополнения) и сразу же увидеть сгенерированный JavaScript код.

```js
// В TypeScript есть 3 базовых типа
var isDone: boolean = false;
var lines: number = 42;
var name: string = "Андерс";

// Тип «any» для случаев, когда заранее неизвестен тип переменной
var notSure: any = 4;
notSure = "а может быть, строка";
notSure = false; // а теперь логический тип

// Для коллекций есть типизированные массивы и обобщённые массивы
var list: number[] = [1, 2, 3];
// Как альтернатива, использование обобщённого массива
var list: Array<number> = [1, 2, 3];

// Перечисления:
enum Color {Red, Green, Blue};
var c: Color = Color.Green;

// Наконец, «void» используется для обозначения того, что функция ничего не возвращает
function bigHorribleAlert(): void {
  alert("Я маленькое надоедливое окошко!");
}

// Функции — это объекты первого класса. Они поддерживают лямбда-синтаксис (=>)
// и используют вывод типов (type inference)

// Следующие строки кода являются эквивалентными, компилятором предполагается
// одинаковая сигнатура, на выходе генерируется одинаковый JavaScript-код
var f1 = function(i: number): number { return i * i; }
// Предполагается возвращаемый тип
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
// Предполагается возвращаемый тип
var f4 = (i: number) => { return i * i; }
// Предполагается возвращаемый тип, в однострочной функции ключевое слово «return» не нужно
var f5 = (i: number) =>  i * i;

// Интерфейсы являются структурными; всё, что имеет свойства, совместимо с интерфейсом
interface Person {
  name: string;
  // Опциональные свойства, помеченные символом «?»
  age?: number;
  // И, конечно, функции
  move(): void;
}

// Объект, который реализует интерфейс «Person»
// К нему можно обращаться, как к «Person», так как он имеет свойства «name» и «move»
var p: Person = { name: "Бобби", move: () => {} };
// Объекты, которые могут иметь опциональные свойства:
var validPerson: Person = { name: "Бобби", age: 42, move: () => {} };
// Это не «Person», поскольку «age» не является числовым значением
var invalidPerson: Person = { name: "Бобби", age: true };

// Интерфейсы могут также описывать функциональный тип
interface SearchFunc {
  (source: string, subString: string): boolean;
}
// Важны только типы параметров, имена — нет.
var mySearch: SearchFunc;
mySearch = function(src: string, sub: string) {
  return src.search(sub) != -1;
}

// Классы. Члены класса по умолчанию являются публичными
class Point {
  	// Свойства
    x: number;

    // Конструктор — ключевые слова public/private в данном контексте сгенерируют
    // шаблонный код для свойства и для инициализации в конструкторе
    // В данном примере «y» будет определён так же, как и «x», но меньшим количеством кода
    // Значения по умолчанию также поддерживаются

    constructor(x: number, public y: number = 0) {
        this.x = x;
    }

    // Функции
    dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

    // Статические члены
    static origin = new Point(0, 0);
}

var p1 = new Point(10 ,20);
var p2 = new Point(25); //y будет равен 0

// Наследование
class Point3D extends Point {
    constructor(x: number, y: number, public z: number = 0) {
        super(x, y); // Явный вызов конструктора базового класса обязателен
    }

    // Перегрузка
    dist() {
        var d = super.dist();
        return Math.sqrt(d * d + this.z * this.z);
    }
}

// Модули, знак «.» может быть использован как разделитель для обозначения подмодулей
module Geometry {
  export class Square {
    constructor(public sideLength: number = 0) {
    }
    area() {
      return Math.pow(this.sideLength, 2);
    }
  }
}

var s1 = new Geometry.Square(5);

// Локальный псевдоним для ссылки на модуль
import G = Geometry;

var s2 = new G.Square(10);

// Обобщённое программирование
// Классы
class Tuple<T1, T2> {
    constructor(public item1: T1, public item2: T2) {
    }
}

// Интерфейсы
interface Pair<T> {
    item1: T;
    item2: T;
}

// И функции
var pairToTuple = function<T>(p: Pair<T>) {
    return new Tuple(p.item1, p.item2);
};

var tuple = pairToTuple({ item1:"hello", item2:"world"});

// Включение ссылки на файл определения:
/// <reference path="jquery.d.ts" />

```

## Для дальнейшего чтения
 * [Официальный веб-сайт TypeScript](http://www.typescriptlang.org/)
 * [Спецификация языка TypeScript (pdf)](http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg — Introducing TypeScript на Channel 9](http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [Исходный код на GitHub](https://github.com/Microsoft/TypeScript)
 * [Definitely Typed — репозиторий определений типов](http://definitelytyped.org/)
---
category: tool
tool: vim
contributors:
    - ["RadhikaG", "https://github.com/RadhikaG"]
translators:
    - ["Anton Slukovskiy", "https://github.com/slukovskiy"]
filename: LearnVim-ru.txt
lang: ru-ru
---

[Vim](http://www.vim.org)
(Vi IMproved) это клон полулярного текстового редактора для Unix. Он разработан
с целью повышения скорости и продуктивности и повсеместно используется в 
большинство Юникс-подобных систем. В нем имеется множество клавиатурных 
сочетаний для быстрой навигации к определенным точкам в файле и быстрого 
редактирования.

## Основы навигации в vim

```
    vim <filename>   # Открыть <filename> в vim
    :q               # Выйти из vim
    :w               # Сохранить текущий файл
    :wq              # Сохранить и выйти
    :q!              # Выйти из vim не сохраняя файл

    :x               # Сохранить файл и выйти из vim, короткая версия :wq

    u                # Отмена последней команды
    CTRL+R           # Отмена отмены

    h                # Переместить курсор на один символ влево
    j                # Переместить курсор на один символ вниз
    k                # Переместить курсор на один символ вверх
    l                # Переместить курсор на один символ вправо

    # Перемещение по строке

    0                # Переместить курсор к началу строки
    $                # Переместить курсор к концу строки
    ^                # Переместить курсор к первому непустому символу в строке

    # Поиск в тексте

    /<word>          # Подсветить все вхождения <word>  в тексте после курсора
    ?<word>          # Подсветить все вхождения <word>  в тексте до курсора
    n                # Передвигает курсор к следующему вхождения искомого слова
    N                # Передвигает курсор к предыдущему вхождения искомого слова

    :%s/foo/bar/g    # Меняет «foo» на «bar» во всем файле
    :s/foo/bar/g     # Меняет «foo» на «bar» на текущей строке

    # Переходы к символу

    f<character>     # Перенести курсор к <character>
    t<character>     # Перенести курсор вперед и остановиться прямо
                     # перед <character>

    # Например,
    f<               # Перести курсор и остановиться на <
    t<               # Перенсти курсор и остановиться прямо перед <
    
    # Перемещение по словам

    w                # Переместиться вперед на одно слово
    b                # Перенеститься назад на одно слово
    e                # Перейти к концу текущего слова

    # Другие команды для передвижения по тексту

    gg               # Перейти к началу файла
    G                # Перейти к концу файла
    :NUM             # Перейти к строке под номером NUM 
                     # (NUM может быть любым числом)
    H                # Переместить курсор к верхнему краю экрана
    M                # Переместить курсор к середине экрана
    L                # Переместить курсор к нижнему краю экрана
```

## Режимы:

Vim основывается на концепте **режимов**.

Командный режим - vim запускается в этом режиме по-умолчанию, используется для 
навигации и ввода команд.
Режим ввода - используется для внесения изменений в файле.
Визуальный режим - используется для подсветки текста и выполнения операций над ним.
Режим командной строки - используется для ввода команд в нижнем углу экрана после символа «:».

```
    i                # Переводит vim в режим вставки перед позицией курсора
    a                # Переводит vim в режим вставки после позиции курсора
    v                # Переводит vim в визуальный режим
    :                # Переводит vim в режим командной строки
    <esc>            # Выходит из любого режима в котором вы находитесь 
                     # в командный режим

    # Копирование и вставка текста

    y                # Скопировать выделенное
    yy               # Скопировать текущую строку
    d                # Удалить выделенное
    dd               # Удалить текущую строку
    p                # Вставить скопированный текст после текущей позиции курсора
    P                # Вставить скопированный текст перед текущей позицией курсора
    x                # Удалить символ под текущей позицией курсора
```

## «Грамматика» vim

Vim можно рассматривать как набор команд в формате «Глагол-Модификатор-Существительное», где:

Глагол - ваше действие.
Модификатор - то как вы его выполняете.
Существительное - объект над которым вы его выполняете.

Несколько важных пример «Глаголов», «Модификаторов», и «Существительных»:

```
    # «Глаголы»
 
    d                # Удалить
    c                # Изменить
    y                # Скопировать
    v                # Визуально выделить

    # «Модификаторы»

    i                # Внутри
    a                # Снаружи
    NUM              # Число
    f                # Ищет что-то и останавливается на нем
    t                # Ищет что-то и останавливается перед ним
    /                # Ищет строку после курсора
    ?                # Ищет строку перед курсором

    # «Существительные»

    w                # Слово
    s                # Предложение
    p                # Параграф
    b                # Блок
    
    # Образцы «предложений» или команд

    d2w              # Удалить 2 слова
    cis              # Изменить объемлющее предложение
    yip              # Скопировать объемлющий параграф
    ct<              # Изменяет текст от курсора до следующей открывающей скобки
    d$               # Удалить все от положения курсора до конца строки
```

## Некоторые сокращения и хитрости

        <!--TODO: Add more!-->
```
    >                # Сдвинуть выделенное на один отступ вправо
    <                # Сдвинуть выделенное на один отступ влево
    :earlier 15m     # Возвращает документ к состоянию в котором он был 
                     # 15 минут назад
    :later 15m       # Отменяет предыдущую команду
    ddp              # Меняет позиции последовательных строк, сначала dd затем p
    .                # Повторяет предыдущее действие
```

## Макросы

Макросы это просто записываемые действия.
Во время записи макросы запоминают **все** действия и команды до тех пор пока 
запись не будет остановлена.При вызове макрос применяет ту же самую последовательность 
действий и команд на выделенном тексте.

```
    qa               # Начать запись макроса под именем «a»
    q                # Закончить запись
    @a               # Выполнить макрос 
```

### Настройка ~/.vimrc

Файл .vimrc может использоваться для настройки Vim при запуске.

Вот пример файла ~/.vimrc:

```
" Пример ~/.vimrc
" 2015.10 

" Отключает совместимость со старым vi
set nocompatible

" Определяет тип файла по его имени для разрешения автоматических отступов и т. д.
filetype indent plugin on

" Включает подсветку синтаксиса
syntax on

" Улучшенное автодополнение команд 
set wildmenu

" Использовать поиск не чувствительный к регистру 
" за исключением использования заглавный букв
set ignorecase
set smartcase

" Копирует отступы с текущей строки при добавлении новой
set autoindent

" Показывать номера строк
set number

" Настройки отступов, изменяйте по собственному вкусу

" Количество видимых пробелов на один символ табуляции
set tabstop=4

" Количество пробелов в символе табуляции при редактировании
set softtabstop=4

" Количество пробелов в отступе при использовании операций >> и << 
set shiftwidth=4

" Конвертировать символы табуляции в пробелы
set expandtab

" Включить умную табуляцию и пробелы для отступов и выравнивания
set smarttab
```

### Ссылки

[Vim | Home (EN)](http://www.vim.org/index.php)

`$ vimtutor`

[A vim Tutorial and Primer (EN)](https://danielmiessler.com/study/vim/)

[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread) (EN)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about)

[Arch Linux Wiki](https://wiki.archlinux.org/index.php/Vim_%28%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%29)
---
language: xml
filename: learnxml-ru.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Dmitry Bessonov", "https://github.com/TheDmitry"]
lang: ru-ru
---

XML - это язык разметки, предназначенный для хранения и передачи данных.

В отличие от HTML, XML не определяет, как отображать или форматировать данные, он только содержит их.

* XML-Синтаксис

```xml
<!-- Комментарии в XML выглядят вот так -->

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="КУЛИНАРИЯ">
    <title lang="ru">Итальянская кухня каждый день</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="ДЕТИ">
    <title lang="ru">Гарри Поттер</title>
    <author>Дж. К. Роулинг</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="ВСЕМИРНАЯ ПАУТИНА">
    <title lang="ru">Изучаем XML</title>
    <author>Эрик Рэй</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

<!-- Вышеописанный документ - типичный XML-файл.
  Он начинается с определения, объявляющего о некоторых метаданных (необязательно).
  
  XML использует древовидную структуру. У вышеописанного документа
  корневой узел - 'bookstore', который содержит три дочерних узла - все 'book'-узлы.
  Такие узлы содержат еще дочерние узлы и т.д.
  
  Узлы создаются с помощью открывающих/закрывающих тегов,
  а дочерние узлы - это узлы между открывающимися и закрывающимися тегами.-->


<!-- XML содержит в себе два типа данных:
  1 - Атрибуты -> Это метаданные узлов.
      Обычно XML-парсер использует эту информацию, чтобы хранить свойства данных.
      Атрибут изображается путем вписывания его в скобки в пределах открытого тега
  2 - Элементы -> Это чистые данные.
      Это то, что парсер извлечет из XML-файла.
      Элементы идут между открытыми и закрытыми тегами без скобок. -->
      
  
<!-- Ниже элемент с двумя атрибутами -->
<file type="gif" id="4293">компьютер.gif</file>


```

* Хорошо отформатированный документ x Проверка достоверности

XML-документ хорошо отформатирован, если он синтаксически верный.
Впрочем, в документ возможно ввести больше ограничений,
используя определения документа, вроде DTD и XML-схемы.

XML-документ, который следует описанию документа, называется корректным,
относительно этого документа. 

С таким инструментом вы можете проверить XML-данные вне логики приложения.

```xml

<!-- Ниже вы можете увидеть упрощенную версию документа книжного магазина,
  с дополнением DTD-определения.-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Bookstore.dtd">
<bookstore>
  <book category="КУЛИНАРИЯ">
    <title >Итальянская кухня каждый день</title>
    <price>30.00</price>
  </book>
</bookstore>

<!-- Этот DTD может быть чем-то вроде:-->

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Литература">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>


<!-- DTD начинается с объявления.
  Затем объявляется корневой узел, требующий 1 или более дочерних узлов 'book'.
  Каждый 'book' должен содержать точно один 'title' и 'price', и атрибут,
  называемый 'category', со значением "Литература" по умолчанию.
  Узлы 'title' и 'price' содержат анализируемые символьные данные.-->

<!-- DTD может быть объявлен в самом XML-файле.-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Литература">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>

<bookstore>
  <book category="КУЛИНАРИЯ">
    <title >Итальянская кухня каждый день</title>
    <price>30.00</price>
  </book>
</bookstore>
```

## DTD совместимость и определение XML Schema(схем/структуры)
 
Поддержка DTDs является повсеместным, потому что это довольно старая технология. К сожалению, современные функции XML как пространств имен(namespaces) не поддерживаются DTDs. Определения XML-схемы (XSDs) предназначены для замены DTDs которая в свою очередь предназначена для определения грамматики XML-документа.

## Ресурсы

* [Валидатор XML (ссылка на английском языке)](http://www.xmlvalidation.com)

## Для будущего прочтения

* [XML Schema Definitions Tutorial (ссылка на английском языке)](http://www.w3schools.com/schema/)
* [DTD руководство (ссылка на английском языке)](http://www.w3schools.com/xml/xml_dtd_intro.asp)
* [XML руководство (ссылка на английском языке)](http://www.w3schools.com/xml/default.asp)
* [использование XPath запросов для парсинга XML (ссылка на английском языке)](http://www.w3schools.com/xml/xml_xpath.asp)
---
category: tool
tool: ruby ecosystem
contributors:
    - ["Jon Smock", "http://github.com/jonsmock"]
    - ["Rafal Chmiel", "http://github.com/rafalchmiel"]

---

People using Ruby generally have a way to install different Ruby versions,
manage their packages (or gems), and manage their gem dependencies.

## Ruby Managers

Some platforms have Ruby pre-installed or available as a package. Most rubyists
do not use these, or if they do, they only use them to bootstrap another Ruby
installer or implementation. Instead rubyists tend to install a Ruby manager to
install and switch between many versions of Ruby and their projects' Ruby
environments.

The following are the popular Ruby environment managers:

* [RVM](https://rvm.io/) - Installs and switches between rubies. RVM also has
  the concept of gemsets to isolate projects' environments completely.
* [ruby-build](https://github.com/sstephenson/ruby-build) - Only installs
  rubies. Use this for finer control over your rubies' installations.
* [rbenv](https://github.com/sstephenson/rbenv) - Only switches between rubies.
  Used with ruby-build.  Use this for finer control over how rubies load.
* [chruby](https://github.com/postmodern/chruby) - Only switches between rubies.
  Similar in spirit to rbenv. Unopinionated about how rubies are installed.

## Ruby Versions

Ruby was created by Yukihiro "Matz" Matsumoto, who remains somewhat of a
[BDFL](https://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life), although
that is changing recently. As a result, the reference implementation of Ruby is
called MRI (Matz' Reference Implementation), and when you hear a Ruby version,
it is referring to the release version of MRI.

The three major version of Ruby in use are:

* 2.0.0 - Released in February 2013. Most major libraries and frameworks support
  2.0.0.
* 1.9.3 - Released in October 2011. This is the version most rubyists use
  currently. Also [retired](https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended/)
* 1.8.7 - Ruby 1.8.7 has been
  [retired](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/).

The change between 1.8.7 to 1.9.x is a much larger change than 1.9.3 to 2.0.0.
For instance, the 1.9 series introduced encodings and a bytecode VM.  There
are projects still on 1.8.7, but they are becoming a small minority, as most of
the community has moved to at least 1.9.2 or 1.9.3.

## Ruby Implementations

The Ruby ecosystem enjoys many different implementations of Ruby, each with
unique strengths and states of compatibility. To be clear, the different
implementations are written in different languages, but *they are all Ruby*.
Each implementation has special hooks and extra features, but they all run
normal Ruby files well. For instance, JRuby is written in Java, but you do
not need to know Java to use it.

Very mature/compatible:

* [MRI](https://github.com/ruby/ruby) - Written in C, this is the reference implementation of Ruby. By
  definition it is 100% compatible (with itself). All other rubies
maintain compatibility with MRI (see [RubySpec](#rubyspec) below).
* [JRuby](http://jruby.org/) - Written in Java and Ruby, this robust implementation is quite fast.
  Most importantly, JRuby's strength is JVM/Java interop, leveraging existing
JVM tools, projects, and languages.
* [Rubinius](http://rubini.us/) - Written primarily in Ruby itself with a C++ bytecode VM. Also
  mature and fast. Because it is implemented in Ruby itself, it exposes many VM
features into rubyland.

Medium mature/compatible:

* [Maglev](http://maglev.github.io/) - Built on top of Gemstone, a Smalltalk VM. Smalltalk has some
  impressive tooling, and this project tries to bring that into Ruby
development.
* [RubyMotion](http://www.rubymotion.com/) - Brings Ruby to iOS development.

Less mature/compatible:

* [Topaz](http://topazruby.com/) - Written in RPython (using the PyPy toolchain), Topaz is fairly young
  and not yet compatible. It shows promise to be a high-performance Ruby
implementation.
* [IronRuby](http://ironruby.net/) - Written in C# targeting the .NET platform, work on IronRuby seems
  to have stopped since Microsoft pulled their support.

Ruby implementations may have their own release version numbers, but they always
target a specific version of MRI for compatibility. Many implementations have
the ability to enter different modes (for example, 1.8 or 1.9 mode) to specify
which MRI version to target.

## RubySpec

Most Ruby implementations rely heavily on [RubySpec](http://rubyspec.org/). Ruby
has no official specification, so the community has written executable specs in
Ruby to test their implementations' compatibility with MRI.

## RubyGems

[RubyGems](http://rubygems.org/) is a community-run package manager for Ruby.
RubyGems ships with Ruby, so there is no need to download it separately.

Ruby packages are called "gems," and they can be hosted by the community at
RubyGems.org. Each gem contains its source code and some metadata, including
things like version, dependencies, author(s), and license(s).

## Bundler

[Bundler](http://bundler.io/) is a gem dependency resolver. It uses a project's
Gemfile to find dependencies, and then fetches those dependencies' dependencies
recursively. It does this until all dependencies are resolved and downloaded, or
it will stop if a conflict has been found.

Bundler will raise an error if it finds conflicting dependencies. For example,
if gem A requires version 3 or greater of gem Z, but gem B requires version 2,
Bundler will notify you of the conflict. This becomes extremely helpful as many
gems refer to other gems (which refer to other gems), which can form a large
dependency graph to resolve.

# Testing

Testing is a large part of Ruby culture. Ruby comes with its own Unit-style
testing framework called minitest (Or TestUnit for Ruby version 1.8.x). There
are many testing libraries with different goals.

* [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html) - Ruby 1.8's built-in "Unit-style" testing framework
* [minitest](http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html) - Ruby 1.9/2.0's built-in testing framework
* [RSpec](http://rspec.info/) - A testing framework that focuses on expressivity
* [Cucumber](http://cukes.info/) - A BDD testing framework that parses Gherkin formatted tests

## Be Nice

The Ruby community takes pride in being an open, diverse, welcoming community.
Matz himself is extremely friendly, and the generosity of rubyists on the whole
is amazing.
---
language: ruby
filename: learnruby.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
  - ["Gabriel Halley", "https://github.com/ghalley"]
  - ["Persa Zula", "http://persazula.com"]
  - ["Jake Faris", "https://github.com/farisj"]
---

```ruby
# This is a comment

=begin
This is a multiline comment
No-one uses them
You shouldn't either
=end

# First and foremost: Everything is an object.

# Numbers are objects

3.class #=> Fixnum

3.to_s #=> "3"


# Some basic arithmetic
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
5 % 3 #=> 2

# Bitwise operators
3 & 5 #=> 1
3 | 5 #=> 7
3 ^ 5 #=> 6

# Arithmetic is just syntactic sugar
# for calling a method on an object
1.+(3) #=> 4
10.* 5 #=> 50

# Special values are objects
nil # equivalent to null in other languages
true # truth
false # falsehood

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Equality
1 == 1 #=> true
2 == 1 #=> false

# Inequality
1 != 1 #=> false
2 != 1 #=> true

# apart from false itself, nil is the only other 'falsey' value

!nil   #=> true
!false #=> true
!0     #=> false

# More comparisons
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Combined comparison operator
1 <=> 10 #=> -1
10 <=> 1 #=> 1
1 <=> 1 #=> 0

# Logical operators
true && false #=> false
true || false #=> true
!true #=> false

# There are alternate versions of the logical operators with much lower
# precedence. These are meant to be used as flow-control constructs to chain
# statements together until one of them returns true or false.

# `do_something_else` only called if `do_something` succeeds.
do_something() and do_something_else()
# `log_error` only called if `do_something` fails.
do_something() or log_error()


# Strings are objects

'I am a string'.class #=> String
"I am a string too".class #=> String

placeholder = 'use string interpolation'
"I can #{placeholder} when using double quoted strings"
#=> "I can use string interpolation when using double quoted strings"

# Prefer single quoted strings to double quoted ones where possible
# Double quoted strings perform additional inner calculations

# Combine strings, but not with numbers
'hello ' + 'world'  #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"

# Combine strings and operators
'hello ' * 3 #=> "hello hello hello "

# Append to string
'hello' << ' world' #=> "hello world"

# print to the output with a newline at the end
puts "I'm printing!"
#=> I'm printing!
#=> nil

# print to the output without a newline
print "I'm printing!"
#=> I'm printing! => nil

# Variables
x = 25 #=> 25
x #=> 25

# Note that assignment returns the value assigned
# This means you can do multiple assignment:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# By convention, use snake_case for variable names
snake_case = true

# Use descriptive variable names
path_to_project_root = '/good/name/'
path = '/bad/name/'

# Symbols (are objects)
# Symbols are immutable, reusable constants represented internally by an
# integer value. They're often used instead of strings to efficiently convey
# specific, meaningful values

:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# Arrays

# This is an array
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Arrays can contain different types of items

[1, 'hello', false] #=> [1, "hello", false]

# Arrays can be indexed
# From the front
array[0] #=> 1
array.first #=> 1
array[12] #=> nil

# Like arithmetic, [var] access
# is just syntactic sugar
# for calling a method [] on an object
array.[] 0 #=> 1
array.[] 12 #=> nil

# From the end
array[-1] #=> 5
array.last #=> 5

# With a start index and length
array[2, 3] #=> [3, 4, 5]

# Reverse an Array
a=[1,2,3]
a.reverse! #=> [3,2,1]

# Or with a range
array[1..3] #=> [2, 3, 4]

# Add to an array like this
array << 6 #=> [1, 2, 3, 4, 5, 6]
# Or like this
array.push(6) #=> [1, 2, 3, 4, 5, 6]

# Check if an item exists in an array
array.include?(1) #=> true

# Hashes are Ruby's primary dictionary with key/value pairs.
# Hashes are denoted with curly braces:
hash = { 'color' => 'green', 'number' => 5 }

hash.keys #=> ['color', 'number']

# Hashes can be quickly looked up by key:
hash['color'] #=> 'green'
hash['number'] #=> 5

# Asking a hash for a key that doesn't exist returns nil:
hash['nothing here'] #=> nil

# Since Ruby 1.9, there's a special syntax when using symbols as keys:

new_hash = { defcon: 3, action: true }

new_hash.keys #=> [:defcon, :action]

# Check existence of keys and values in hash
new_hash.key?(:defcon) #=> true
new_hash.value?(3) #=> true

# Tip: Both Arrays and Hashes are Enumerable
# They share a lot of useful methods such as each, map, count, and more

# Control structures

if true
  'if statement'
elsif false
  'else if, optional'
else
  'else, also optional'
end

for counter in 1..5
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# HOWEVER, No-one uses for loops.
# Instead you should use the "each" method and pass it a block.
# A block is a bunch of code that you can pass to a method like "each".
# It is analogous to lambdas, anonymous functions or closures in other
# programming languages.
#
# The "each" method of a range runs the block once for each element of the range.
# The block is passed a counter as a parameter.
# Calling the "each" method with a block looks like this:

(1..5).each do |counter|
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# You can also surround blocks in curly brackets:
(1..5).each { |counter| puts "iteration #{counter}" }

# The contents of data structures can also be iterated using each.
array.each do |element|
  puts "#{element} is part of the array"
end
hash.each do |key, value|
  puts "#{key} is #{value}"
end

# If you still need an index you can use "each_with_index" and define an index
# variable
array.each_with_index do |element, index|
  puts "#{element} is number #{index} in the array"
end

counter = 1
while counter <= 5 do
  puts "iteration #{counter}"
  counter += 1
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# There are a bunch of other helpful looping functions in Ruby,
# for example "map", "reduce", "inject", the list goes on. Map,
# for instance, takes the array it's looping over, does something
# to it as defined in your block, and returns an entirely new array.
array = [1,2,3,4,5]
doubled = array.map do |element|
  element * 2
end
puts doubled
#=> [2,4,6,8,10]
puts array
#=> [1,2,3,4,5]

grade = 'B'

case grade
when 'A'
  puts 'Way to go kiddo'
when 'B'
  puts 'Better luck next time'
when 'C'
  puts 'You can do better'
when 'D'
  puts 'Scraping through'
when 'F'
  puts 'You failed!'
else
  puts 'Alternative grading system, eh?'
end
#=> "Better luck next time"

# cases can also use ranges
grade = 82
case grade
when 90..100
  puts 'Hooray!'
when 80...90
  puts 'OK job'
else
  puts 'You failed!'
end
#=> "OK job"

# exception handling:
begin
  # code here that might raise an exception
  raise NoMemoryError, 'You ran out of memory.'
rescue NoMemoryError => exception_variable
  puts 'NoMemoryError was raised', exception_variable
rescue RuntimeError => other_exception_variable
  puts 'RuntimeError was raised now'
else
  puts 'This runs if no exceptions were thrown at all'
ensure
  puts 'This code always runs no matter what'
end

# Methods

def double(x)
  x * 2
end

# Methods (and all blocks) implicitly return the value of the last statement
double(2) #=> 4

# Parentheses are optional where the result is unambiguous
double 3 #=> 6

double double 3 #=> 12

def sum(x, y)
  x + y
end

# Method arguments are separated by a comma
sum 3, 4 #=> 7

sum sum(3, 4), 5 #=> 12

# yield
# All methods have an implicit, optional block parameter
# it can be called with the 'yield' keyword

def surround
  puts '{'
  yield
  puts '}'
end

surround { puts 'hello world' }

# {
# hello world
# }


# You can pass a block to a method
# "&" marks a reference to a passed block
def guests(&block)
  block.call 'some_argument'
end

# You can pass a list of arguments, which will be converted into an array
# That's what splat operator ("*") is for
def guests(*array)
  array.each { |guest| puts guest }
end

# If a method returns an array, you can use destructuring assignment
def foods
    ['pancake', 'sandwich', 'quesadilla']
end
breakfast, lunch, dinner = foods
breakfast #=> 'pancake'
dinner #=> 'quesadilla'

# By convention, all methods that return booleans end with a question mark
5.even? # false
5.odd? # true

# And if a method ends with an exclamation mark, it does something destructive
# like mutate the receiver. Many methods have a ! version to make a change, and
# a non-! version to just return a new changed version
company_name = "Dunder Mifflin"
company_name.upcase #=> "DUNDER MIFFLIN"
company_name #=> "Dunder Mifflin"
company_name.upcase! # we're mutating company_name this time!
company_name #=> "DUNDER MIFFLIN"


# Define a class with the class keyword
class Human

  # A class variable. It is shared by all instances of this class.
  @@species = 'H. sapiens'

  # Basic initializer
  def initialize(name, age = 0)
    # Assign the argument to the "name" instance variable for the instance
    @name = name
    # If no age given, we will fall back to the default in the arguments list.
    @age = age
  end

  # Basic setter method
  def name=(name)
    @name = name
  end

  # Basic getter method
  def name
    @name
  end

  # The above functionality can be encapsulated using the attr_accessor method as follows
  attr_accessor :name

  # Getter/setter methods can also be created individually like this
  attr_reader :name
  attr_writer :name

  # A class method uses self to distinguish from instance methods.
  # It can only be called on the class, not an instance.
  def self.say(msg)
    puts msg
  end

  def species
    @@species
  end
end


# Instantiate a class
jim = Human.new('Jim Halpert')

dwight = Human.new('Dwight K. Schrute')

# Let's call a couple of methods
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# Call the class method
Human.say('Hi') #=> "Hi"

# Variable's scopes are defined by the way we name them.
# Variables that start with $ have global scope
$var = "I'm a global var"
defined? $var #=> "global-variable"

# Variables that start with @ have instance scope
@var = "I'm an instance var"
defined? @var #=> "instance-variable"

# Variables that start with @@ have class scope
@@var = "I'm a class var"
defined? @@var #=> "class variable"

# Variables that start with a capital letter are constants
Var = "I'm a constant"
defined? Var #=> "constant"

# Class is also an object in ruby. So class can have instance variables.
# Class variable is shared among the class and all of its descendants.

# base class
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

# derived class
class Worker < Human
end

Human.foo # 0
Worker.foo # 0

Human.foo = 2 # 2
Worker.foo # 2

# Class instance variable is not shared by the class's descendants.

class Human
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doctor < Human
end

Human.bar # 0
Doctor.bar # nil

module ModuleExample
  def foo
    'foo'
  end
end

# Including modules binds their methods to the class instances
# Extending modules binds their methods to the class itself

class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => NoMethodError: undefined method `foo'

# Callbacks are executed when including and extending a module

module ConcernExample
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class Something
  include ConcernExample
end

Something.bar     # => 'bar'
Something.qux     # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
```

## Additional resources

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges.
- [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Learn Ruby through a series of interactive tutorials.
- [Official Documentation](http://ruby-doc.org/core)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.
- [Try Ruby](http://tryruby.org) - Learn the basic of Ruby programming language, interactive in the browser.
---
language: rust
filename: rust-pt.rs
contributors:
    - ["Paulo Henrique Rodrigues Pinheiro", "https://about.me/paulohrpinheiro"]
lang: pt-br

---

Rust é uma linguagem de programação desenvolvida pelo Mozilla Research. Rust
combina controle de baixo nível sobre o desempenho com facilidades de alto
nível e garantias de segurança.

Ele atinge esse objetico sem necessitar de um coletor de lixo ou um processo
*runtime*, permitindo que se use bibliotecas Rust em substituição a bibliotecas 
em C.

A primeira versão de Rust, 0.1, apareceu em janeiro de 2012, e por três anos o
desenvolvimento correu tão rapidamente que que até recentemente o uso de
versões estáveis foi desencorajado e em vez disso a recomendação era usar as
versões empacotadas toda noite.

Em 15 de maio de 2015, a versão 1.0 de Rust foi liberada com a garantia total
de compatibilidade reversa. Melhorias no tempo de compilação e em outros
aspectos do compilador estão disponíveis atualmente nas versões empacotadas à
noite. Rust adotou um modelo de versões *train-based* com novas versões
regularmente liberadas a cada seis semanas. A versão 1.1 beta de Rust foi
disponibilizada ao mesmo tempo que a versão 1.0.

Apesar de Rust ser uma linguagem mais e baixo nível, Rust tem alguns conceitos
funcionais geralmente encontradas em linguagens de alto nível. Isso faz Rust
não apenas rápido, mas também fácil e eficiente para programar.

```rust
// Isso é um comentário. Linhas de comentários são assim...
// e múltiplas linhas se parecem assim.

/// Comentários para documentação são assim e permitem notação em markdown.
/// # Exemplos
///
/// ```
/// let five = 5
/// ```

///////////////
// 1. Básico //
///////////////

// Funções
// `i32` é o tipo para inteiros com sinal de 32 bits
fn add2(x: i32, y: i32) -> i32 {
    // Implicit return (no semicolon)
    x + y
}

// Função main
fn main() {
    // Números //

    // Immutable bindings
    let x: i32 = 1;

    // Inteiros/Sufixos para ponto flutuante
    let y: i32 = 13i32;
    let f: f64 = 1.3f64;

    // Inferência de tipos
    // Em geral, o compilador Rust consegue inferir qual o tipo de uma 
    // variável, então você não tem que escrever uma anotação explícita de tipo.
    // Ao longo desse tutorial, os tipos serão explicitamente anotados em 
    // muitos lugares, mas apenas com propóstico demonstrativo. A inferência de 
    // tipos pode gerenciar isso na maioria das vezes.
    let implicit_x = 1;
    let implicit_f = 1.3;

    // Aritmética
    let sum = x + y + 13;

    // Variáveis mutáveis
    let mut mutable = 1;
    mutable = 4;
    mutable += 2;

    // Strings //

    // String literais
    let x: &str = "hello world!";

    // Imprimindo
    println!("{} {}", f, x); // 1.3 hello world

    // Uma `String` – uma String alocada no heap
    let s: String = "hello world".to_string();

    // Uma String slice - uma visão imutável em outra string.
    // Basicamente, isso é um par imutável de ponteiros para uma string - ele
    // não contém o conteúdo de uma strinf, apenas um ponteiro para o começo e 
    // um ponteiro para o fim da área de memória para a string, estaticamente
    // alocada ou contida em outro objeto (nesse caso, `s`)
    let s_slice: &str = &s;

    println!("{} {}", s, s_slice); // hello world hello world

    // Vetores/arrays //

    // Um array de tamanho fixo
    let four_ints: [i32; 4] = [1, 2, 3, 4];

    // Um array dinâmico (vetor)
    let mut vector: Vec<i32> = vec![1, 2, 3, 4];
    vector.push(5);

    // Uma fatia – uma visão imutável em um vetor ou array
    // Isso é como um string slice, mas para vetores
    let slice: &[i32] = &vector;

    // Use `{:?}` para imprimir alguma coisa no estilo de depuração
    println!("{:?} {:?}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    // Tuplas //

    // Uma tupla é um conjunto de tamanho fixo de valores de tipos
    // possivelmente diferentes
    let x: (i32, &str, f64) = (1, "hello", 3.4);

    // Desestruturando `let`
    let (a, b, c) = x;
    println!("{} {} {}", a, b, c); // 1 hello 3.4

    // Indexando
    println!("{}", x.1); // hello

    //////////////
    // 2. Tipos //
    //////////////

    // Struct
    struct Point {
        x: i32,
        y: i32,
    }

    let origin: Point = Point { x: 0, y: 0 };

    // Uma estrutura com campos sem nome, chamada 'estrutura em tupla'
    struct Point2(i32, i32);

    let origin2 = Point2(0, 0);

    // enum básico com na linguagem C
    enum Direction {
        Left,
        Right,
        Up,
        Down,
    }

    let up = Direction::Up;

    // Enum com campos
    enum OptionalI32 {
        AnI32(i32),
        Nothing,
    }

    let two: OptionalI32 = OptionalI32::AnI32(2);
    let nothing = OptionalI32::Nothing;

    // Generics //

    struct Foo<T> { bar: T }

    // Isso é definido na biblioteca padrão como um `Option`
    enum Optional<T> {
        SomeVal(T),
        NoVal,
    }

    // Methods //

    impl<T> Foo<T> {
        // Métodos recebem um parâmetro `self` explícito
        fn get_bar(self) -> T {
            self.bar
        }
    }

    let a_foo = Foo { bar: 1 };
    println!("{}", a_foo.get_bar()); // 1

    // Traits (conhecidos como interfaces ou typeclasses em outras linguagens)//

    trait Frobnicate<T> {
        fn frobnicate(self) -> Option<T>;
    }

    impl<T> Frobnicate<T> for Foo<T> {
        fn frobnicate(self) -> Option<T> {
            Some(self.bar)
        }
    }

    let another_foo = Foo { bar: 1 };
    println!("{:?}", another_foo.frobnicate()); // Some(1)

    //////////////////////////////////
    // 3. Reconhecimento de padrões //
    //////////////////////////////////

    let foo = OptionalI32::AnI32(1);
    match foo {
        OptionalI32::AnI32(n) => println!("it’s an i32: {}", n),
        OptionalI32::Nothing  => println!("it’s nothing!"),
    }

    // Reconhecimento avançado de padrões
    struct FooBar { x: i32, y: OptionalI32 }
    let bar = FooBar { x: 15, y: OptionalI32::AnI32(32) };

    match bar {
        FooBar { x: 0, y: OptionalI32::AnI32(0) } =>
            println!("The numbers are zero!"),
        FooBar { x: n, y: OptionalI32::AnI32(m) } if n == m =>
            println!("The numbers are the same"),
        FooBar { x: n, y: OptionalI32::AnI32(m) } =>
            println!("Different numbers: {} {}", n, m),
        FooBar { x: _, y: OptionalI32::Nothing } =>
            println!("The second number is Nothing!"),
    }

    //////////////////////////
    // 4. Controle de fluxo //
    //////////////////////////

    // `for` laços de repetição/iteração
    let array = [1, 2, 3];
    for i in array.iter() {
        println!("{}", i);
    }

    // Ranges
    for i in 0u32..10 {
        print!("{} ", i);
    }
    println!("");
    // prints `0 1 2 3 4 5 6 7 8 9 `

    // `if`
    if 1 == 1 {
        println!("Maths is working!");
    } else {
        println!("Oh no...");
    }

    // `if` como expressão
    let value = if true {
        "good"
    } else {
        "bad"
    };

    // laço `while` de repetição
    while 1 == 1 {
        println!("The universe is operating normally.");
    }

    // Repetição infinita
    loop {
        println!("Hello!");
    }

    ////////////////////////////////////////
    // 5. Proteção de memória & ponteiros //
    ////////////////////////////////////////

    // Ponteiro com dono - somente uma coisa pode 'possuir' esse ponteiro por
    // vez.
    // Isso significa que quando `Box` perde seu escopo, ele pode ser
    // automaticamente desalocado com segurança.
    let mut mine: Box<i32> = Box::new(3);
    *mine = 5; // dereference
    // Aqui, `now_its_mine` possui o controle exclusivo de `mine`. Em outras 
    // palavras, `mine` tem o controle transferido.
    let mut now_its_mine = mine;
    *now_its_mine += 2;

    println!("{}", now_its_mine); // 7
    // println!("{}", mine); // não compila porque `now_its_mine` é o dono

    // Referência - um ponteiro imutável que referencia outro dado
    // Quando uma referência é dada a um valor, nós dizemos que o valor foi
    // emprestado 'borrowed'.
    // Quando um valor é emprestado sem ser mutável, ele não pode ser alterado
    // ou ter a sua propriedade transferida.
    // Um empréstimo finaliza quando o escopo em que ele foi criado termina.

    let mut var = 4;
    var = 3;
    let ref_var: &i32 = &var;

    println!("{}", var); // AO contrário de `mine`, `var` ainda pode ser usado
    println!("{}", *ref_var);
    // var = 5; // não compila porque `var` é emprestado
    // *ref_var = 6; // não compila, porque `ref_var` é uma referência imutável

    // Referência mutável
    // Quando um valor mutável é emprestado, ele não pode ser acessado.
    let mut var2 = 4;
    let ref_var2: &mut i32 = &mut var2;
    *ref_var2 += 2;         // '*' aponta para var2, que é mutável e emprestada

    println!("{}", *ref_var2); // 6 , // var2 não compila.
    // ref_var2 é do tipo &mut i32, que guarda uma referência i32, não o valor.
    // var2 = 2; // não compila porque `var2` é empretada.
}
```

## Outras leituras

Existe muita coisa sobre Rust - isto aqui é apenas o básico para que você possa 
entender as coisas mais importantes. Para aprender mais sobre Rust, leia [The 
Rust Programming Language](http://doc.rust-lang.org/book/index.html) e 
acompanhe [/r/rust](http://reddit.com/r/rust). A galera no canal #rust do
irc.mozilla.org também estão sempre dispostos a ajudar os novatos.

Você pode brincar com outras característica de Rust com um compilador online
no portal oficial do projeto [Rust playpen](http://play.rust-lang.org), or ler 
mais na página oficial [Rust website](http://rust-lang.org).

No Brasil acompanhe os encontros do [Meetup Rust São Paulo]
(http://www.meetup.com/pt-BR/Rust-Sao-Paulo-Meetup/).

---
language: rust
contributors:
    - ["P1start", "http://p1start.github.io/"]
filename: learnrust.rs
---

Rust is a programming language developed by Mozilla Research.
Rust combines low-level control over performance with high-level convenience and
safety guarantees.

It achieves these goals without requiring a garbage collector or runtime, making
it possible to use Rust libraries as a "drop-in replacement" for C.

Rust’s first release, 0.1, occurred in January 2012, and for 3 years development
moved so quickly that until recently the use of stable releases was discouraged
and instead the general advice was to use nightly builds.

On May 15th 2015, Rust 1.0 was released with a complete guarantee of backward
compatibility. Improvements to compile times and other aspects of the compiler are
currently available in the nightly builds. Rust has adopted a train-based release
model with regular releases every six weeks. Rust 1.1 beta was made available at
the same time of the release of Rust 1.0.

Although Rust is a relatively low-level language, Rust has some functional
concepts that are generally found in higher-level languages. This makes
Rust not only fast, but also easy and efficient to code in.

```rust
// This is a comment. Line comments look like this...
// and extend multiple lines like this.

/// Documentation comments look like this and support markdown notation.
/// # Examples
///
/// ```
/// let five = 5
/// ```

///////////////
// 1. Basics //
///////////////

// Functions
// `i32` is the type for 32-bit signed integers
fn add2(x: i32, y: i32) -> i32 {
    // Implicit return (no semicolon)
    x + y
}

// Main function
fn main() {
    // Numbers //

    // Immutable bindings
    let x: i32 = 1;

    // Integer/float suffixes
    let y: i32 = 13i32;
    let f: f64 = 1.3f64;

    // Type inference
    // Most of the time, the Rust compiler can infer what type a variable is, so
    // you don’t have to write an explicit type annotation.
    // Throughout this tutorial, types are explicitly annotated in many places,
    // but only for demonstrative purposes. Type inference can handle this for
    // you most of the time.
    let implicit_x = 1;
    let implicit_f = 1.3;

    // Arithmetic
    let sum = x + y + 13;

    // Mutable variable
    let mut mutable = 1;
    mutable = 4;
    mutable += 2;

    // Strings //

    // String literals
    let x: &str = "hello world!";

    // Printing
    println!("{} {}", f, x); // 1.3 hello world

    // A `String` – a heap-allocated string
    let s: String = "hello world".to_string();

    // A string slice – an immutable view into another string
    // This is basically an immutable pair of pointers to a string – it doesn’t
    // actually contain the contents of a string, just a pointer to
    // the begin and a pointer to the end of a string buffer,
    // statically allocated or contained in another object (in this case, `s`)
    let s_slice: &str = &s;

    println!("{} {}", s, s_slice); // hello world hello world

    // Vectors/arrays //

    // A fixed-size array
    let four_ints: [i32; 4] = [1, 2, 3, 4];

    // A dynamic array (vector)
    let mut vector: Vec<i32> = vec![1, 2, 3, 4];
    vector.push(5);

    // A slice – an immutable view into a vector or array
    // This is much like a string slice, but for vectors
    let slice: &[i32] = &vector;

    // Use `{:?}` to print something debug-style
    println!("{:?} {:?}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    // Tuples //

    // A tuple is a fixed-size set of values of possibly different types
    let x: (i32, &str, f64) = (1, "hello", 3.4);

    // Destructuring `let`
    let (a, b, c) = x;
    println!("{} {} {}", a, b, c); // 1 hello 3.4

    // Indexing
    println!("{}", x.1); // hello

    //////////////
    // 2. Types //
    //////////////

    // Struct
    struct Point {
        x: i32,
        y: i32,
    }

    let origin: Point = Point { x: 0, y: 0 };

    // A struct with unnamed fields, called a ‘tuple struct’
    struct Point2(i32, i32);

    let origin2 = Point2(0, 0);

    // Basic C-like enum
    enum Direction {
        Left,
        Right,
        Up,
        Down,
    }

    let up = Direction::Up;

    // Enum with fields
    enum OptionalI32 {
        AnI32(i32),
        Nothing,
    }

    let two: OptionalI32 = OptionalI32::AnI32(2);
    let nothing = OptionalI32::Nothing;

    // Generics //

    struct Foo<T> { bar: T }

    // This is defined in the standard library as `Option`
    enum Optional<T> {
        SomeVal(T),
        NoVal,
    }

    // Methods //

    impl<T> Foo<T> {
        // Methods take an explicit `self` parameter
        fn get_bar(self) -> T {
            self.bar
        }
    }

    let a_foo = Foo { bar: 1 };
    println!("{}", a_foo.get_bar()); // 1

    // Traits (known as interfaces or typeclasses in other languages) //

    trait Frobnicate<T> {
        fn frobnicate(self) -> Option<T>;
    }

    impl<T> Frobnicate<T> for Foo<T> {
        fn frobnicate(self) -> Option<T> {
            Some(self.bar)
        }
    }

    let another_foo = Foo { bar: 1 };
    println!("{:?}", another_foo.frobnicate()); // Some(1)

    /////////////////////////
    // 3. Pattern matching //
    /////////////////////////

    let foo = OptionalI32::AnI32(1);
    match foo {
        OptionalI32::AnI32(n) => println!("it’s an i32: {}", n),
        OptionalI32::Nothing  => println!("it’s nothing!"),
    }

    // Advanced pattern matching
    struct FooBar { x: i32, y: OptionalI32 }
    let bar = FooBar { x: 15, y: OptionalI32::AnI32(32) };

    match bar {
        FooBar { x: 0, y: OptionalI32::AnI32(0) } =>
            println!("The numbers are zero!"),
        FooBar { x: n, y: OptionalI32::AnI32(m) } if n == m =>
            println!("The numbers are the same"),
        FooBar { x: n, y: OptionalI32::AnI32(m) } =>
            println!("Different numbers: {} {}", n, m),
        FooBar { x: _, y: OptionalI32::Nothing } =>
            println!("The second number is Nothing!"),
    }

    /////////////////////
    // 4. Control flow //
    /////////////////////

    // `for` loops/iteration
    let array = [1, 2, 3];
    for i in array.iter() {
        println!("{}", i);
    }

    // Ranges
    for i in 0u32..10 {
        print!("{} ", i);
    }
    println!("");
    // prints `0 1 2 3 4 5 6 7 8 9 `

    // `if`
    if 1 == 1 {
        println!("Maths is working!");
    } else {
        println!("Oh no...");
    }

    // `if` as expression
    let value = if true {
        "good"
    } else {
        "bad"
    };

    // `while` loop
    while 1 == 1 {
        println!("The universe is operating normally.");
    }

    // Infinite loop
    loop {
        println!("Hello!");
    }

    /////////////////////////////////
    // 5. Memory safety & pointers //
    /////////////////////////////////

    // Owned pointer – only one thing can ‘own’ this pointer at a time
    // This means that when the `Box` leaves its scope, it can be automatically deallocated safely.
    let mut mine: Box<i32> = Box::new(3);
    *mine = 5; // dereference
    // Here, `now_its_mine` takes ownership of `mine`. In other words, `mine` is moved.
    let mut now_its_mine = mine;
    *now_its_mine += 2;

    println!("{}", now_its_mine); // 7
    // println!("{}", mine); // this would not compile because `now_its_mine` now owns the pointer

    // Reference – an immutable pointer that refers to other data
    // When a reference is taken to a value, we say that the value has been ‘borrowed’.
    // While a value is borrowed immutably, it cannot be mutated or moved.
    // A borrow lasts until the end of the scope it was created in.
    let mut var = 4;
    var = 3;
    let ref_var: &i32 = &var;

    println!("{}", var); // Unlike `mine`, `var` can still be used
    println!("{}", *ref_var);
    // var = 5; // this would not compile because `var` is borrowed
    // *ref_var = 6; // this would not either, because `ref_var` is an immutable reference

    // Mutable reference
    // While a value is mutably borrowed, it cannot be accessed at all.
    let mut var2 = 4;
    let ref_var2: &mut i32 = &mut var2;
    *ref_var2 += 2;         // '*' is used to point to the mutably borrowed var2

    println!("{}", *ref_var2); // 6 , // var2 would not compile.
    // ref_var2 is of type &mut i32, so stores a reference to an i32, not the value.
    // var2 = 2; // this would not compile because `var2` is borrowed.
}
```

## Further reading

There’s a lot more to Rust—this is just the basics of Rust so you can understand
the most important things. To learn more about Rust, read [The Rust Programming
Language](http://doc.rust-lang.org/book/index.html) and check out the
[/r/rust](http://reddit.com/r/rust) subreddit. The folks on the #rust channel on
irc.mozilla.org are also always keen to help newcomers.

You can also try out features of Rust with an online compiler at the official
[Rust playpen](http://play.rust-lang.org) or on the main
[Rust website](http://rust-lang.org).
---
language: sass
filename: learnsass.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
  - ["Sean Corrales", "https://github.com/droidenator"]
  - ["Kyle Mendes", "https://github.com/pink401k"]
  - ["Keith Miyake", "https://github.com/kaymmm"]
---

Sass is a CSS extension language that adds features such as variables, nesting, mixins and more.
Sass (and other preprocessors, such as [Less](http://lesscss.org/)) help developers write maintainable and DRY (Don't Repeat Yourself) code.

Sass has two different syntax options to choose from. SCSS, which has the same syntax as CSS but with the added features of Sass. Or Sass (the original syntax), which uses indentation rather than curly braces and semicolons.
This tutorial is written using SCSS.

If you're already familiar with CSS3, you'll be able to pick up Sass relatively quickly. It does not provide any new styling properties but rather the tools to write your CSS more efficiently and make maintenance much easier.

```sass


//Single line comments are removed when Sass is compiled to CSS.

/* Multi line comments are preserved. */



/* Variables
============================== */



/* You can store a CSS value (such as a color) in a variable.
Use the '$' symbol to create a variable. */

$primary-color: #A3A4FF;
$secondary-color: #51527F;
$body-font: 'Roboto', sans-serif;

/* You can use the variables throughout your stylesheet.
Now if you want to change a color, you only have to make the change once. */

body {
	background-color: $primary-color;
	color: $secondary-color;
	font-family: $body-font;
}

/* This would compile to: */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}

/* This is much more maintainable than having to change the color
each time it appears throughout your stylesheet. */



/* Control Directives
============================== */

/* Sass lets you use @if, @else, @for, @while, and @each to control the
   compilation of your code to CSS. */

/* @if/@else blocks behave exactly as you might expect */

$debug: true !default;

@mixin debugmode {
	@if $debug {
		@debug "Debug mode enabled";

		display: inline-block;
	}
	@else {
		display: none;
	}
}

.info {
	@include debugmode;
}

/* If $debug is set to true, .info is displayed; if it's set to false then
.info is not displayed.

Note: @debug will output debugging information to the command line.
Useful for checking variables while debugging your SCSS. */

.info {
	display: inline-block;
}

/* @for is a control loop that iterates through a range of values.
Particularly useful for setting styles on a collection of items.
There are two forms, "through" and "to". The former includes the last value,
the latter stops at the last value. */

@for $c from 1 to 4 {
	div:nth-of-type(#{$c}) {
		left: ($c - 1) * 900 / 3;
	}
}

@for $c from 1 through 3 {
	.myclass-#{$c} {
		color: rgb($c * 255 / 3, $c * 255 / 3, $c * 255 / 3);
	}
}

/* Will compile to: */

div:nth-of-type(1) {
	left: 0;
}

div:nth-of-type(2) {
	left: 300;
}

div:nth-of-type(3) {
	left: 600;
}

.myclass-1 {
	color: #555555;
}

.myclass-2 {
	color: #aaaaaa;
}

.myclass-3 {
	color: white;
// SASS automatically converts #FFFFFF to white
}

/* @while is very straightforward: */

$columns: 4;
$column-width: 80px;

@while $columns > 0 {
	.col-#{$columns} {
		width: $column-width;
		left: $column-width * ($columns - 1);
	}

	$columns: $columns - 1;
}

/* Will output the following CSS: */

.col-4 {
	width: 80px;
	left: 240px;
}

.col-3 {
	width: 80px;
	left: 160px;
}

.col-2 {
	width: 80px;
	left: 80px;
}

.col-1 {
	width: 80px;
	left: 0px;
}

/* @each functions like @for, except using a list instead of ordinal values
Note: you specify lists just like other variables, with spaces as
delimiters. */

$social-links: facebook twitter linkedin reddit;

.social-links {
	@each $sm in $social-links {
		.icon-#{$sm} {
			background-image: url("images/#{$sm}.png");
		}
	}
}

/* Which will output: */

.social-links .icon-facebook {
	background-image: url("images/facebook.png");
}

.social-links .icon-twitter {
	background-image: url("images/twitter.png");
}

.social-links .icon-linkedin {
	background-image: url("images/linkedin.png");
}

.social-links .icon-reddit {
	background-image: url("images/reddit.png");
}


/* Mixins
==============================*/

/* If you find you are writing the same code for more than one
element, you might want to store that code in a mixin.

Use the '@mixin' directive, plus a name for your mixin. */

@mixin center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* You can use the mixin with '@include' and the mixin name. */

div {
	@include center;
	background-color: $primary-color;
}

/* Which would compile to: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}

/* You can use mixins to create a shorthand property. */

@mixin size($width, $height) {
	width: $width;
	height: $height;
}

/* Which you can invoke by passing width and height arguments. */

.rectangle {
	@include size(100px, 60px);
}

.square {
	@include size(40px, 40px);
}

/* Compiles to: */
.rectangle {
  width: 100px;
  height: 60px;
}

.square {
  width: 40px;
  height: 40px;
}



/* Functions
============================== */



/* Sass provides functions that can be used to accomplish a variety of
   tasks. Consider the following */

/* Functions can be invoked by using their name and passing in the
   required arguments */
body {
  width: round(10.25px);
}

.footer {
  background-color: fade_out(#000000, 0.25);
}

/* Compiles to: */

body {
  width: 10px;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* You may also define your own functions. Functions are very similar to
   mixins. When trying to choose between a function or a mixin, remember
   that mixins are best for generating CSS while functions are better for
   logic that might be used throughout your Sass code. The examples in
   the 'Math Operators' section are ideal candidates for becoming a reusable
   function. */

/* This function will take a target size and the parent size and calculate
   and return the percentage */

@function calculate-percentage($target-size, $parent-size) {
  @return $target-size / $parent-size * 100%;
}

$main-content: calculate-percentage(600px, 960px);

.main-content {
  width: $main-content;
}

.sidebar {
  width: calculate-percentage(300px, 960px);
}

/* Compiles to: */

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}



/* Extend (Inheritance)
============================== */



/* Extend is a way to share the properties of one selector with another. */

.display {
	@include size(5em, 5em);
	border: 5px solid $secondary-color;
}

.display-success {
	@extend .display;
	border-color: #22df56;
}

/* Compiles to: */
.display, .display-success {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F;
}

.display-success {
  border-color: #22df56;
}

/* Extending a CSS statement is preferable to creating a mixin
   because of the way Sass groups together the classes that all share
   the same base styling. If this was done with a mixin, the width,
   height, and border would be duplicated for each statement that
   called the mixin. While it won't affect your workflow, it will
   add unnecessary bloat to the files created by the Sass compiler. */



/* Nesting
============================== */



/* Sass allows you to nest selectors within selectors */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #FF0000;
	}
}

/* '&' will be replaced by the parent selector. */
/* You can also nest pseudo-classes. */
/* Keep in mind that over-nesting will make your code less maintainable.
Best practices recommend going no more than 3 levels deep when nesting.
For example: */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* Compiles to: */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/* Partials and Imports
============================== */



/* Sass allows you to create partial files. This can help keep your Sass
   code modularized. Partial files should begin with an '_', e.g. _reset.css.
   Partials are not generated into CSS. */

/* Consider the following CSS which we'll put in a file called _reset.css */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Sass offers @import which can be used to import partials into a file.
   This differs from the traditional CSS @import statement which makes
   another HTTP request to fetch the imported file. Sass takes the
   imported file and combines it with the compiled code. */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* Compiles to: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/* Placeholder Selectors
============================== */



/* Placeholders are useful when creating a CSS statement to extend. If you
   wanted to create a CSS statement that was exclusively used with @extend,
   you can do so using a placeholder. Placeholders begin with a '%' instead
   of '.' or '#'. Placeholders will not appear in the compiled CSS. */

%content-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  @extend %content-window;
  background-color: #0000ff;
}

/* Compiles to: */

.message-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  background-color: #0000ff;
}



/* Math Operations
============================== */



/* Sass provides the following operators: +, -, *, /, and %. These can
   be useful for calculating values directly in your Sass files instead
   of using values that you've already calculated by hand. Below is an example
   of a setting up a simple two column design. */

$content-area: 960px;
$main-content: 600px;
$sidebar-content: 300px;

$main-size: $main-content / $content-area * 100%;
$sidebar-size: $sidebar-content / $content-area * 100%;
$gutter: 100% - ($main-size + $sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: $main-size;
}

.sidebar {
  width: $sidebar-size;
}

.gutter {
  width: $gutter;
}

/* Compiles to: */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}

```

## SASS or Sass?
Have you ever wondered whether Sass is an acronym or not? You probably haven't, but I'll tell you anyway. The name of the language is a word, "Sass", and not an acronym.
Because people were constantly writing it as "SASS", the creator of the language jokingly called it "Syntactically Awesome StyleSheets".


## Practice Sass
If you want to play with Sass in your browser, check out [SassMeister](http://sassmeister.com/).
You can use either syntax, just go into the settings and select either Sass or SCSS.


## Compatibility
Sass can be used in any project as long as you have a program to compile it into CSS. You'll want to verify that the CSS you're using is compatible with your target browsers.

[QuirksMode CSS](http://www.quirksmode.org/css/) and [CanIUse](http://caniuse.com) are great resources for checking compatibility.


## Further reading
* [Official Documentation](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way](http://thesassway.com/) provides tutorials (beginner-advanced) and articles.
---
language: Scala
filename: learnscala.scala
contributors:
    - ["George Petrov", "http://github.com/petrovg"]
    - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
    - ["Geoff Liu", "http://geoffliu.me"]
    - ["Ha-Duong Nguyen", "http://reference-error.org"]
---

Scala - the scalable language

```scala

/////////////////////////////////////////////////
// 0. Basics
/////////////////////////////////////////////////
/*
  Setup Scala:

  1) Download Scala - http://www.scala-lang.org/downloads
  2) Unzip/untar to your favorite location and put the bin subdir in your `PATH` environment variable
*/

/*
  Try the REPL

  Scala has a tool called the REPL (Read-Eval-Print Loop) that is analogous to
  commandline interpreters in many other languages. You may type any Scala
  expression, and the result will be evaluated and printed.  

  The REPL is a very handy tool to test and verify code.  Use it as you read
  this tutorial to quickly explore concepts on your own.
*/

// Start a Scala REPL by running `scala`. You should see the prompt:
$ scala
scala>

// By default each expression you type is saved as a new numbered value
scala> 2 + 2
res0: Int = 4

// Default values can be reused.  Note the value type displayed in the result..
scala> res0 + 2
res1: Int = 6

// Scala is a strongly typed language. You can use the REPL to check the type
// without evaluating an expression.
scala> :type (true, 2.0)
(Boolean, Double)

// REPL sessions can be saved
scala> :save /sites/repl-test.scala

// Files can be loaded into the REPL
scala> :load /sites/repl-test.scala
Loading /sites/repl-test.scala...
res2: Int = 4
res3: Int = 6

// You can search your recent history
scala> :h?
1 2 + 2
2 res0 + 2
3 :save /sites/repl-test.scala
4 :load /sites/repl-test.scala
5 :h?

// Now that you know how to play, let's learn a little scala...

/////////////////////////////////////////////////
// 1. Basics
/////////////////////////////////////////////////

// Single-line comments start with two forward slashes

/*
  Multi-line comments, as you can already see from above, look like this.
*/

// Printing, and forcing a new line on the next print
println("Hello world!")
println(10)
// Hello world!
// 10

// Printing, without forcing a new line on next print
print("Hello world")
print(10)
// Hello world10

// Declaring values is done using either var or val.
// val declarations are immutable, whereas vars are mutable. Immutability is
// a good thing.
val x = 10 // x is now 10
x = 20     // error: reassignment to val
var y = 10
y = 20     // y is now 20

/*
  Scala is a statically typed language, yet note that in the above declarations,
  we did not specify a type. This is due to a language feature called type
  inference. In most cases, Scala compiler can guess what the type of a variable
  is, so you don't have to type it every time. We can explicitly declare the
  type of a variable like so:
*/
val z: Int = 10
val a: Double = 1.0

// Notice automatic conversion from Int to Double, result is 10.0, not 10
val b: Double = 10

// Boolean values
true
false

// Boolean operations
!true         // false
!false        // true
true == false // false
10 > 5        // true

// Math is as per usual
1 + 1   // 2
2 - 1   // 1
5 * 3   // 15
6 / 2   // 3
6 / 4   // 1
6.0 / 4 // 1.5
6 / 4.0 // 1.5


// Evaluating an expression in the REPL gives you the type and value of the result

1 + 7

/* The above line results in:

  scala> 1 + 7
  res29: Int = 8

  This means the result of evaluating 1 + 7 is an object of type Int with a
  value of 8

  Note that "res29" is a sequentially generated variable name to store the
  results of the expressions you typed, your output may differ.
*/

"Scala strings are surrounded by double quotes"
'a' // A Scala Char
// 'Single quote strings don't exist' <= This causes an error

// Strings have the usual Java methods defined on them
"hello world".length
"hello world".substring(2, 6)
"hello world".replace("C", "3")

// They also have some extra Scala methods. See also: scala.collection.immutable.StringOps
"hello world".take(5)
"hello world".drop(5)

// String interpolation: notice the prefix "s"
val n = 45
s"We have $n apples" // => "We have 45 apples"

// Expressions inside interpolated strings are also possible
val a = Array(11, 9, 6)
s"My second daughter is ${a(0) - a(2)} years old."    // => "My second daughter is 5 years old."
s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples."
s"Power of 2: ${math.pow(2, 2)}"                      // => "Power of 2: 4"

// Formatting with interpolated strings with the prefix "f"
f"Power of 5: ${math.pow(5, 2)}%1.0f"         // "Power of 5: 25"
f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122: 11.0454"

// Raw strings, ignoring special characters.
raw"New line feed: \n. Carriage return: \r." // => "New line feed: \n. Carriage return: \r."

// Some characters need to be "escaped", e.g. a double quote inside a string:
"They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown""

// Triple double-quotes let strings span multiple rows and contain quotes
val html = """<form id="daform">
                <p>Press belo', Joe</p>
                <input type="submit">
              </form>"""


/////////////////////////////////////////////////
// 2. Functions
/////////////////////////////////////////////////

// Functions are defined like so:
//
//   def functionName(args...): ReturnType = { body... }
//
// If you come from more traditional languages, notice the omission of the
// return keyword. In Scala, the last expression in the function block is the
// return value.
def sumOfSquares(x: Int, y: Int): Int = {
  val x2 = x * x
  val y2 = y * y
  x2 + y2
}

// The { } can be omitted if the function body is a single expression:
def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y

// Syntax for calling functions is familiar:
sumOfSquares(3, 4)  // => 25

// You can use parameters names to specify them in different order
def subtract(x: Int, y: Int): Int = x - y

subtract(10, 3)     // => 7
subtract(y=10, x=3) // => -7

// In most cases (with recursive functions the most notable exception), function
// return type can be omitted, and the same type inference we saw with variables
// will work with function return values:
def sq(x: Int) = x * x  // Compiler can guess return type is Int

// Functions can have default parameters:
def addWithDefault(x: Int, y: Int = 5) = x + y
addWithDefault(1, 2) // => 3
addWithDefault(1)    // => 6


// Anonymous functions look like this:
(x: Int) => x * x

// Unlike defs, even the input type of anonymous functions can be omitted if the
// context makes it clear. Notice the type "Int => Int" which means a function
// that takes Int and returns Int.
val sq: Int => Int = x => x * x

// Anonymous functions can be called as usual:
sq(10)   // => 100

// If each argument in your anonymous function is
// used only once, Scala gives you an even shorter way to define them. These
// anonymous functions turn out to be extremely common, as will be obvious in
// the data structure section.
val addOne: Int => Int = _ + 1
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)

addOne(5)      // => 6
weirdSum(2, 4) // => 16


// The return keyword exists in Scala, but it only returns from the inner-most
// def that surrounds it.
// WARNING: Using return in Scala is error-prone and should be avoided.
// It has no effect on anonymous functions. For example:
def foo(x: Int): Int = {
  val anonFunc: Int => Int = { z =>
    if (z > 5)
      return z // This line makes z the return value of foo!
    else
      z + 2    // This line is the return value of anonFunc
  }
  anonFunc(x)  // This line is the return value of foo
}


/////////////////////////////////////////////////
// 3. Flow Control
/////////////////////////////////////////////////

1 to 5
val r = 1 to 5
r.foreach(println)

r foreach println
// NB: Scala is quite lenient when it comes to dots and brackets - study the
// rules separately. This helps write DSLs and APIs that read like English

(5 to 1 by -1) foreach (println)

// A while loop
var i = 0
while (i < 10) { println("i " + i); i += 1 }

while (i < 10) { println("i " + i); i += 1 }   // Yes, again. What happened? Why?

i    // Show the value of i. Note that while is a loop in the classical sense -
     // it executes sequentially while changing the loop variable. while is very
     // fast, but using the combinators and comprehensions above is easier
     // to understand and parallelize

// A do-while loop
i = 0
do {
  println("i is still less than 10")
  i += 1
} while (i < 10)

// Recursion is the idiomatic way of repeating an action in Scala (as in most
// other functional languages).
// Recursive functions need an explicit return type, the compiler can't infer it.
// Here it's Unit.
def showNumbersInRange(a: Int, b: Int): Unit = {
  print(a)
  if (a < b)
    showNumbersInRange(a + 1, b)
}
showNumbersInRange(1, 14)


// Conditionals

val x = 10

if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println("yeah") else println("nay")

println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"


/////////////////////////////////////////////////
// 4. Data Structures
/////////////////////////////////////////////////

val a = Array(1, 2, 3, 5, 8, 13)
a(0)     // Int = 1
a(3)     // Int = 5
a(21)    // Throws an exception

val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
m("fork")         // java.lang.String = tenedor
m("spoon")        // java.lang.String = cuchara
m("bottle")       // Throws an exception

val safeM = m.withDefaultValue("no lo se")
safeM("bottle")   // java.lang.String = no lo se

val s = Set(1, 3, 7)
s(0)      // Boolean = false
s(1)      // Boolean = true

/* Look up the documentation of map here -
 * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
 * and make sure you can read it
 */


// Tuples

(1, 2)

(4, 3, 2)

(1, 2, "three")

(a, 2, "three")

// Why have this?
val divideInts = (x: Int, y: Int) => (x / y, x % y)

// The function divideInts gives you the result and the remainder
divideInts(10, 3)    // (Int, Int) = (3,1)

// To access the elements of a tuple, use _._n where n is the 1-based index of
// the element
val d = divideInts(10, 3)    // (Int, Int) = (3,1)

d._1    // Int = 3
d._2    // Int = 1

// Alternatively you can do multiple-variable assignment to tuple, which is more
// convenient and readable in many cases
val (div, mod) = divideInts(10, 3)

div     // Int = 3
mod     // Int = 1


/////////////////////////////////////////////////
// 5. Object Oriented Programming
/////////////////////////////////////////////////

/*
  Aside: Everything we've done so far in this tutorial has been simple
  expressions (values, functions, etc). These expressions are fine to type into
  the command-line interpreter for quick tests, but they cannot exist by
  themselves in a Scala file. For example, you cannot have just "val x = 5" in
  a Scala file. Instead, the only top-level constructs allowed in Scala are:

  - objects
  - classes
  - case classes
  - traits

  And now we will explain what these are.
*/

// classes are similar to classes in other languages. Constructor arguments are
// declared after the class name, and initialization is done in the class body.
class Dog(br: String) {
  // Constructor code here
  var breed: String = br

  // Define a method called bark, returning a String
  def bark = "Woof, woof!"

  // Values and methods are assumed public. "protected" and "private" keywords
  // are also available.
  private def sleep(hours: Int) =
    println(s"I'm sleeping for $hours hours")

  // Abstract methods are simply methods with no body. If we uncomment the next
  // line, class Dog would need to be declared abstract
  //   abstract class Dog(...) { ... }
  // def chaseAfter(what: String): String
}

val mydog = new Dog("greyhound")
println(mydog.breed) // => "greyhound"
println(mydog.bark)  // => "Woof, woof!"


// The "object" keyword creates a type AND a singleton instance of it. It is
// common for Scala classes to have a "companion object", where the per-instance
// behavior is captured in the classes themselves, but behavior related to all
// instance of that class go in objects. The difference is similar to class
// methods vs static methods in other languages. Note that objects and classes
// can have the same name.
object Dog {
  def allKnownBreeds = List("pitbull", "shepherd", "retriever")
  def createDog(breed: String) = new Dog(breed)
}


// Case classes are classes that have extra functionality built in. A common
// question for Scala beginners is when to use classes and when to use case
// classes. The line is quite fuzzy, but in general, classes tend to focus on
// encapsulation, polymorphism, and behavior. The values in these classes tend
// to be private, and only methods are exposed. The primary purpose of case
// classes is to hold immutable data. They often have few methods, and the
// methods rarely have side-effects.
case class Person(name: String, phoneNumber: String)

// Create a new instance. Note cases classes don't need "new"
val george = Person("George", "1234")
val kate = Person("Kate", "4567")

// With case classes, you get a few perks for free, like getters:
george.phoneNumber  // => "1234"

// Per field equality (no need to override .equals)
Person("George", "1234") == Person("Kate", "1236")  // => false

// Easy way to copy
// otherGeorge == Person("george", "9876")
val otherGeorge = george.copy(phoneNumber = "9876")

// And many others. Case classes also get pattern matching for free, see below.

// Traits
// Similar to Java interfaces, traits define an object type and method
// signatures. Scala allows partial implementation of those methods.
// Constructor parameters are not allowed. Traits can inherit from other
// traits or classes without parameters.

trait Dog {
	def breed: String
	def color: String
	def bark: Boolean = true
	def bite: Boolean
}
class SaintBernard extends Dog {
	val breed = "Saint Bernard"
	val color = "brown"
	def bite = false
}  

scala> b  
res0: SaintBernard = SaintBernard@3e57cd70  
scala> b.breed  
res1: String = Saint Bernard  
scala> b.bark  
res2: Boolean = true  
scala> b.bite  
res3: Boolean = false  

// A trait can also be used as Mixin. The class "extends" the first trait,
// but the keyword "with" can add additional traits.

trait Bark {
	def bark: String = "Woof"
}
trait Dog {
	def breed: String
	def color: String
}
class SaintBernard extends Dog with Bark {
	val breed = "Saint Bernard"
	val color = "brown"
}

scala> val b = new SaintBernard
b: SaintBernard = SaintBernard@7b69c6ba
scala> b.bark
res0: String = Woof


/////////////////////////////////////////////////
// 6. Pattern Matching
/////////////////////////////////////////////////

// Pattern matching is a powerful and commonly used feature in Scala. Here's how
// you pattern match a case class. NB: Unlike other languages, Scala cases do
// not need breaks, fall-through does not happen.

def matchPerson(person: Person): String = person match {
  // Then you specify the patterns:
  case Person("George", number) => "We found George! His number is " + number
  case Person("Kate", number)   => "We found Kate! Her number is " + number
  case Person(name, number)     => "We matched someone : " + name + ", phone : " + number
}

val email = "(.*)@(.*)".r  // Define a regex for the next example.

// Pattern matching might look familiar to the switch statements in the C family
// of languages, but this is much more powerful. In Scala, you can match much
// more:
def matchEverything(obj: Any): String = obj match {
  // You can match values:
  case "Hello world" => "Got the string Hello world"

  // You can match by type:
  case x: Double => "Got a Double: " + x

  // You can specify conditions:
  case x: Int if x > 10000 => "Got a pretty big number!"

  // You can match case classes as before:
  case Person(name, number) => s"Got contact info for $name!"

  // You can match regular expressions:
  case email(name, domain) => s"Got email address $name@$domain"

  // You can match tuples:
  case (a: Int, b: Double, c: String) => s"Got a tuple: $a, $b, $c"

  // You can match data structures:
  case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c"

  // You can nest patterns:
  case List(List((1, 2, "YAY"))) => "Got a list of list of tuple"

  // Match any case (default) if all previous haven't matched
  case _ => "Got unknown object"
}

// In fact, you can pattern match any object with an "unapply" method. This
// feature is so powerful that Scala lets you define whole functions as
// patterns:
val patternFunc: Person => String = {
  case Person("George", number) => s"George's number: $number"
  case Person(name, number) => s"Random person's number: $number"
}


/////////////////////////////////////////////////
// 7. Functional Programming
/////////////////////////////////////////////////

// Scala allows methods and functions to return, or take as parameters, other
// functions or methods.

val add10: Int => Int = _ + 10 // A function taking an Int and returning an Int
List(1, 2, 3) map add10 // List(11, 12, 13) - add10 is applied to each element

// Anonymous functions can be used instead of named functions:
List(1, 2, 3) map (x => x + 10)

// And the underscore symbol, can be used if there is just one argument to the
// anonymous function. It gets bound as the variable
List(1, 2, 3) map (_ + 10)

// If the anonymous block AND the function you are applying both take one
// argument, you can even omit the underscore
List("Dom", "Bob", "Natalia") foreach println


// Combinators

s.map(sq)

val sSquared = s. map(sq)

sSquared.filter(_ < 10)

sSquared.reduce (_+_)

// The filter function takes a predicate (a function from A -> Boolean) and
// selects all elements which satisfy the predicate
List(1, 2, 3) filter (_ > 2) // List(3)
case class Person(name: String, age: Int)
List(
  Person(name = "Dom", age = 23),
  Person(name = "Bob", age = 30)
).filter(_.age > 25) // List(Person("Bob", 30))


// Scala a foreach method defined on certain collections that takes a type
// returning Unit (a void method)
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println

// For comprehensions

for { n <- s } yield sq(n)

val nSquared2 = for { n <- s } yield sq(n)

for { n <- nSquared2 if n < 10 } yield n

for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared

/* NB Those were not for loops. The semantics of a for loop is 'repeat', whereas
   a for-comprehension defines a relationship between two sets of data. */


/////////////////////////////////////////////////
// 8. Implicits
/////////////////////////////////////////////////

/* WARNING WARNING: Implicits are a set of powerful features of Scala, and
 * therefore it is easy to abuse them. Beginners to Scala should resist the
 * temptation to use them until they understand not only how they work, but also
 * best practices around them. We only include this section in the tutorial
 * because they are so commonplace in Scala libraries that it is impossible to
 * do anything meaningful without using a library that has implicits. This is
 * meant for you to understand and work with implicits, not declare your own.
 */

// Any value (vals, functions, objects, etc) can be declared to be implicit by
// using the, you guessed it, "implicit" keyword. Note we are using the Dog
// class from section 5 in these examples.
implicit val myImplicitInt = 100
implicit def myImplicitFunction(breed: String) = new Dog("Golden " + breed)

// By itself, implicit keyword doesn't change the behavior of the value, so
// above values can be used as usual.
myImplicitInt + 2                   // => 102
myImplicitFunction("Pitbull").breed // => "Golden Pitbull"

// The difference is that these values are now eligible to be used when another
// piece of code "needs" an implicit value. One such situation is implicit
// function arguments:
def sendGreetings(toWhom: String)(implicit howMany: Int) =
  s"Hello $toWhom, $howMany blessings to you and yours!"

// If we supply a value for "howMany", the function behaves as usual
sendGreetings("John")(1000)  // => "Hello John, 1000 blessings to you and yours!"

// But if we omit the implicit parameter, an implicit value of the same type is
// used, in this case, "myImplicitInt":
sendGreetings("Jane")  // => "Hello Jane, 100 blessings to you and yours!"

// Implicit function parameters enable us to simulate type classes in other
// functional languages. It is so often used that it gets its own shorthand. The
// following two lines mean the same thing:
// def foo[T](implicit c: C[T]) = ...
// def foo[T : C] = ...


// Another situation in which the compiler looks for an implicit is if you have
//   obj.method(...)
// but "obj" doesn't have "method" as a method. In this case, if there is an
// implicit conversion of type A => B, where A is the type of obj, and B has a
// method called "method", that conversion is applied. So having
// myImplicitFunction above in scope, we can say:
"Retriever".breed // => "Golden Retriever"
"Sheperd".bark    // => "Woof, woof!"

// Here the String is first converted to Dog using our function above, and then
// the appropriate method is called. This is an extremely powerful feature, but
// again, it is not to be used lightly. In fact, when you defined the implicit
// function above, your compiler should have given you a warning, that you
// shouldn't do this unless you really know what you're doing.


/////////////////////////////////////////////////
// 9. Misc
/////////////////////////////////////////////////

// Importing things
import scala.collection.immutable.List

// Import all "sub packages"
import scala.collection.immutable._

// Import multiple classes in one statement
import scala.collection.immutable.{List, Map}

// Rename an import using '=>'
import scala.collection.immutable.{List => ImmutableList}

// Import all classes, except some. The following excludes Map and Set:
import scala.collection.immutable.{Map => _, Set => _, _}

// Java classes can also be imported. Scala syntax can be used
import java.swing.{JFrame, JWindow}

// Your programs entry point is defined in an scala file using an object, with a
// single method, main:
object Application {
  def main(args: Array[String]): Unit = {
    // stuff goes here.
  }
}

// Files can contain multiple classes and objects. Compile with scalac




// Input and output

// To read a file line by line
import scala.io.Source
for(line <- Source.fromFile("myfile.txt").getLines())
  println(line)

// To write a file use Java's PrintWriter
val writer = new PrintWriter("myfile.txt")
writer.write("Writing line for line" + util.Properties.lineSeparator)
writer.write("Another line here" + util.Properties.lineSeparator)
writer.close()

```

## Further resources

* [Scala for the impatient](http://horstmann.com/scala/)
* [Twitter Scala school](http://twitter.github.io/scala_school/)
* [The scala documentation](http://docs.scala-lang.org/)
* [Try Scala in your browser](http://scalatutorials.com/tour/)
* Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user)
---
language: self
contributors:
    - ["Russell Allen", "http://github.com/russellallen"]
filename: learnself.self
---

Self is a fast prototype based OO language which runs in its own JIT vm. Most development is done through interacting with live objects through a visual development environment called *morphic* with integrated browsers and debugger.

Everything in Self is an object. All computation is done by sending messages to objects. Objects in Self can be understood as sets of key-value slots.

# Constructing objects

The inbuild Self parser can construct objects, including method objects.

```
"This is a comment"

"A string:"
'This is a string with \'escaped\' characters.\n'

"A 30 bit integer"
23

"A 30 bit float"
3.2

"-20"
-14r16

"An object which only understands one message, 'x' which returns 20"
(|
  x = 20.
|)

"An object which also understands 'x:' which sets the x slot"
(|
  x <- 20.
|)

"An object which understands the method 'doubleX' which
doubles the value of x and then returns the object"
(|
  x <- 20.
  doubleX = (x: x * 2. self)
|)

"An object which understands all the messages
that 'traits point' understands". The parser
looks up 'traits point' by sending the messages
'traits' then 'point' to a known object called
the 'lobby'. It looks up the 'true' object by
also sending the message 'true' to the lobby."
(|     parent* = traits point.
       x = 7.
       y <- 5.
       isNice = true.
|)
```

# Sending messages to objects

Messages can either be unary, binary or keyword. Precedence is in that order. Unlike Smalltalk, the precedence of binary messages must be specified, and all keywords after the first must start with a capital letter. Messages are separated from their destination by whitespace.

```
"unary message, sends 'printLine' to the object '23'
which prints the string '23' to stdout and returns the receiving object (ie 23)"
23 printLine

"sends the message '+' with '7' to '23', then the message '*' with '8' to the result"
(23 + 7) * 8

"sends 'power:' to '2' with '8' returns 256"
2 power: 8

"sends 'keyOf:IfAbsent:' to 'hello' with arguments 'e' and '-1'.
Returns 1, the index of 'e' in 'hello'."
'hello' keyOf: 'e' IfAbsent: -1
```

# Blocks

Self defines flow control like Smalltalk and Ruby by way of blocks. Blocks are delayed computations of the form:

```
[|:x. localVar| x doSomething with: localVar]
```

Examples of the use of a block:

```
"returns 'HELLO'"
'hello' copyMutable mapBy: [|:c| c capitalize]

"returns 'Nah'"
'hello' size > 5 ifTrue: ['Yay'] False: ['Nah']

"returns 'HaLLO'"
'hello' copyMutable mapBy: [|:c|
   c = 'e' ifTrue: [c capitalize]
            False: ['a']]
```

Multiple expressions are separated by a period. ^ returns immediately.

```
"returns An 'E'! How icky!"
'hello' copyMutable mapBy: [|:c. tmp <- ''|
   tmp: c capitalize.
   tmp = 'E' ifTrue: [^ 'An \'E\'! How icky!'].
   c capitalize
   ]
```

Blocks are performed by sending them the message 'value' and inherit (delegate to) their contexts:
```
"returns 0"
[|x|
    x: 15.
    "Repeatedly sends 'value' to the first block while the result of sending 'value' to the
     second block is the 'true' object"
    [x > 0] whileTrue: [x: x - 1].
    x
] value
```

# Methods

Methods are like blocks but they are not within a context but instead are stored as values of slots. Unlike Smalltalk, methods by default return their final value not 'self'.

```
"Here is an object with one assignable slot 'x' and a method 'reduceXTo: y'.
Sending the message 'reduceXTo: 10' to this object will put
the object '10' in the 'x' slot and return the original object"
(|
    x <- 50.
    reduceXTo: y = (
        [x > y] whileTrue: [x: x - 1].
        self)
|)
.
```

# Prototypes

Self has no classes. The way to get an object is to find a prototype and copy it.

```
| d |
d: dictionary copy.
d at: 'hello' Put: 23 + 8.
d at: 'goodbye' Put: 'No!.
"Prints No!"
( d at: 'goodbye' IfAbsent: 'Yes! ) printLine.
"Prints 31"
( d at: 'hello' IfAbsent: -1 ) printLine.
```

# Further information

The [Self handbook](http://handbook.selflanguage.org) has much more information, and nothing beats hand-on experience with Self by downloading it from the [homepage](http://www.selflanguage.org).
---
category: tool
tool: ShutIt
contributors:
    - ["Ian Miell", "http://ian.meirionconsulting.tk"]
filename: learnshutit.html
---

## ShutIt

ShutIt is an shell automation framework designed to be easy to use.

It is a wrapper around a Python-based expect clone (pexpect).

You can look at it as 'expect without the pain'.

It is available as a pip install.

## Hello World

Starting with the simplest example. Create a file called example.py:

```python

import shutit
session = shutit.create_session('bash')
session.send('echo Hello World', echo=True)
```

Running this with:

```bash
python example.py
```

outputs:

```bash
$ python example.py
echo "Hello World"
echo "Hello World"
Hello World
Ians-MacBook-Air.local:ORIGIN_ENV:RhuebR2T#
```

The first argument to 'send' is the command you want to run. The 'echo'
argument outputs the terminal interactions. By default ShutIt is silent.

'send' takes care of all the messing around with prompts and 'expects' that
you might be familiar with from expect.


## Log Into a Server

Let's say you want to log into a server and run a command. Change example.py
to:

```python
import shutit
session = shutit.create_session('bash')
session.login('ssh you@example.com', user='you', password='mypassword')
session.send('hostname', echo=True)
session.logout()
```

which will log you into your server (if you replace with your details) and
output the hostname.

```
$ python example.py
hostname
hostname
example.com
example.com:cgoIsdVv:heDa77HB#
```

Obviously that's insecure! Instead you can run:

```python
import shutit
session = shutit.create_session('bash')
password = session.get_input('', ispass=True)
session.login('ssh you@example.com', user='you', password=password)
session.send('hostname', echo=True)
session.logout()
```

which forces you to input the password:

```
$ python example.py
Input Secret:
hostname
hostname
example.com
example.com:cgoIsdVv:heDa77HB#
```

Again, the 'login' method handles the changing prompt from a login. You give
ShutIt the login command, the user you expect to log in as, and a password
(if needed), and ShutIt takes care of the rest.

'logout' handles the ending of a 'login', handling any changes to the prompt
for you.

## Log Into Multiple Servers

Let's say you have a server farm of two servers, and want to log onto both.
Just create two sessions and run similar login and send commands:

```python
import shutit
session1 = shutit.create_session('bash')
session2 = shutit.create_session('bash')
password1 = session1.get_input('Password for server1', ispass=True)
password2 = session2.get_input('Password for server2', ispass=True)
session1.login('ssh you@one.example.com', user='you', password=password1)
session2.login('ssh you@two.example.com', user='you', password=password2)
session1.send('hostname', echo=True)
session2.send('hostname', echo=True)
session1.logout()
session2.logout()
```

would output:

```bash
$ python example.py
Password for server1
Input Secret:

Password for server2
Input Secret:
hostname
hostname
one.example.com
one.example.com:Fnh2pyFj:qkrsmUNs# hostname
hostname
two.example.com
two.example.com:Gl2lldEo:D3FavQjA#
```

## Example: Monitor Multiple Servers

We can turn the above into a simple monitoring tool by adding some logic to
examine the output of a command:

```python
import shutit
capacity_command="""df / | awk '{print $5}' | tail -1 | sed s/[^0-9]//"""
session1 = shutit.create_session('bash')
session2 = shutit.create_session('bash')
password1 = session.get_input('Password for server1', ispass=True)
password2 = session.get_input('Password for server2', ispass=True)
session1.login('ssh you@one.example.com', user='you', password=password1)
session2.login('ssh you@two.example.com', user='you', password=password2)
capacity = session1.send_and_get_output(capacity_command)
if int(capacity) < 10:
	print('RUNNING OUT OF SPACE ON server1!')
capacity = session2.send_and_get_output(capacity_command)
if int(capacity) < 10:
	print('RUNNING OUT OF SPACE ON server2!')
session1.logout()
session2.logout()
```

Here you use the 'send\_and\_get\_output' method to retrieve the output of the
capacity command (df).

There are much more elegant ways to do the above (e.g. have a dictionary of the
servers to iterate over), but it's up to you how clever you need the Python to
be.


## More Intricate IO - Expecting

Let's say you have an interaction with an interactive command line application
you want to automate. Here we will use telnet as a trivial example:

```python
import shutit
session = shutit.create_session('bash')
session.send('telnet', expect='elnet>', echo=True)
session.send('open google.com 80', expect='scape character', echo=True)
session.send('GET /', echo=True, check_exit=False)
session.logout()
```

Note the 'expect' argument. You only need to give a subset of telnet's
prompt to match and continue.

Note also the 'check\_exit' argument in the above, which is new. We'll come back
to that. The output of the above is:

```bash
$ python example.py
telnet
telnet> open google.com 80
Trying 216.58.214.14...
Connected to google.com.
Escape character is '^]'.
GET /
HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Location: http://www.google.co.uk/?gfe_rd=cr&ei=huczWcj3GfTW8gfq0paQDA
Content-Length: 261
Date: Sun, 04 Jun 2017 10:57:10 GMT

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/?gfe_rd=cr&amp;ei=huczWcj3GfTW8gfq0paQDA">
here
</A>.
</BODY></HTML>
Connection closed by foreign host.
```

Now back to 'check\_exit=False'. Since the telnet command returns a failure exit
code (1) and we don't want the script to fail, you set 'check\_exit=False' to
let ShutIt know you don't care about the exit code.

If you didn't pass that argument in, ShutIt gives you an interactive terminal
if there is a terminal to communicate with. This is called a 'pause point'.


## Pause Points

You can trigger a 'pause point' at any point by calling

```python
[...]
session.pause_point('This is a pause point')
[...]
```

within your script, and then continue with the script by hitting CTRL and ']'
at the same time. This is great for debugging: add a pause point, have a look
around, then continue. Try this:

```python
import shutit
session = shutit.create_session('bash')
session.pause_point('Have a look around!')
session.send('echo "Did you enjoy your pause point?"', echo=True)
```

with output like this:

```bash
$ python example.py
Have a look around!

Ians-Air.home:ORIGIN_ENV:I00LA1Mq#  bash
imiell@Ians-Air:/space/git/shutit  ⑂ master +    
CTRL-] caught, continuing with run...
2017-06-05 15:12:33,577 INFO: Sending:  exit
2017-06-05 15:12:33,633 INFO: Output (squashed):  exitexitIans-Air.home:ORIGIN_ENV:I00LA1Mq#  [...]
echo "Did you enjoy your pause point?"
echo "Did you enjoy your pause point?"
Did you enjoy your pause point?
Ians-Air.home:ORIGIN_ENV:I00LA1Mq#
```


## More Intricate IO - Backgrounding

Returning to our 'monitoring multiple servers' example, let's imagine we
have a long-running task that we want to run on each server. By default, ShutIt
works serially which would take a long time. But we can run tasks in the
background to speed things up.

Here you can try an example with the trivial command: 'sleep 60'.


```python
import shutit
import time
long_command="""sleep 60"""
session1 = shutit.create_session('bash')
session2 = shutit.create_session('bash')
password1 = session1.get_input('Password for server1', ispass=True)
password2 = session2.get_input('Password for server2', ispass=True)
session1.login('ssh you@one.example.com', user='you', password=password1)
session2.login('ssh you@two.example.com', user='you', password=password2)
start = time.time()
session1.send(long_command, background=True)
session2.send(long_command, background=True)
print('That took: ' + str(time.time() - start) + ' seconds to fire')
session1.wait()
session2.wait()
print('That took: ' + str(time.time() - start) + ' seconds to complete')
```

My laptop says it took 0.5 seconds to run fire those two commands, and then just
over a minute to complete (using the 'wait' method).

Again, this is trivial, but imagine you have hundreds of servers to manage like
this and you can see the power it can bring in a few lines of code and one
Python import.


## Learn More

There's a lot more that can be done with ShutIt.

To learn more, see:

[ShutIt](https://ianmiell.github.io/shutit/)
[GitHub](https://github.com/ianmiell/shutit/blob/master/README.md)

It's a broader automation framework, and the above is its 'standalone mode'.

Feedback, feature requests, 'how do I?'s highly appreciated! Reach me at
[@ianmiell](https://twitter.com/ianmiell)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
translators:
    - ["Juraj Kostolanský", "http://www.kostolansky.sk"]
lang: sk-sk
filename: LearnBash-sk.sh
---

Bash je pomenovanie pre unix shell (príkazový interpreter), ktorý bol
tiež distribuovaný ako shell pre GNU operačné systémy a ako predvolený
shell pre Linux a Mac OS X.
Takmer všetky príklady uvedené nižšie môžu byť súčasťou shell skriptu alebo
vykonané priamo v shelli.

[Viac informácií tu.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# Prvý riadok skriptu je tzv. shebang, ktorý systému povie ako skript vykonať:
# http://en.wikipedia.org/wiki/Shebang_(Unix)
# Komentáre začínajú znakom #. Shebang je tiež komentár.

# Jednoduchý príklad:
echo Ahoj svet!

# Každý príkaz začína na novom riadku alebo za bodkočiarkou:
echo 'Toto je prvý riadok'; echo 'Toto je druhý riadok'

# Deklarácia premenných vyzerá takto:
Premenna="Nejaky retazec"

# Ale nie takto:
Premenna = "Nejaky retazec"
# Bash si bude myslieť, že Premenna je príkaz, ktorý musí vykonať.
# Výsledkom bude chyba, pretože taký príkaz nenájde.

# Alebo takto:
Premenna= 'Nejaky retazec'
# Bash zistí, že 'Nejaky retazec' je príkaz, ktorý musí vykonať.
# Výsledkom je opäť chyba, lebo taký príkaz neexistuje.

# Používanie premenných:
echo $Premenna
echo "$Premenna"
echo '$Premenna'
# Keď je premenná použitá samostatne - priradenie, exportovanie a pod. - jej
# meno sa píše bez znaku $. Keď sa používa hodnota premennej, pred názov sa
# dáva znak $. Pozor však pri použití ' (apostrof), ktorý nenahradí premennú
# hodnotou!

# Nahradenie reťazca v premennej
echo ${Premenna/Nieco/A}
# Toto nahradí prvý výskyt reťazca "Nieco" za "A"

# Podreťazec z premennej
Dlzka=7
echo ${Premenna:0:Dlzka}
# Toto vráti iba prvých 7 znakov z hodnoty premennej

# Predvolená hodnota premennej
echo ${Foo:-"PredvolenaHodnotaAkFooChybaAleboJePrazdna"}
# Toto funguje pre null (Foo=) a prázdny reťazec (Foo="");
# nula (Foo=0) vráti 0. Všimni si, že toto iba vráti predvolenú hodnotu,
# ale nezmení hodnotu premennej.

# Štandardné premenné:
# Existujú aj užitočné "vstavané" premenné, ako
echo "Hodnota vrátená posledným programom: $?"
echo "PID skriptu: $$"
echo "Počet argumentov: $#"
echo "Argumeny skriptu: $@"
echo "Argumeny skriptu oddelené do rôznych premenných: $1 $2..."

# Čítanie hodnoty zo vstupu:
echo "Ako sa voláš?"
read Meno # Premenná nemusí byť deklarovaná skôr
echo Ahoj, $Meno!

# Klasická if štruktúra:
# použi 'man test' Pre viac informácií o podmienkach
if [ $Meno -ne $USER ]
then
    echo "Meno nie je tvoje používateľské meno"
else
    echo "Meno je tvoje používateľské meno"
fi

# Existuje aj podmienené vykonanie
echo "Vykonané vždy" || echo "Vykonané iba ak prvý príkaz zlyhá"
echo "Vykonané vždy" && echo "Vykonané iba ak prvý príkaz uspeje"

# Pre použitie && a || s if-podmienkou je potrebné použiť zátvorky:
if [ $Meno == "Steve" ] && [ $Vek -eq 15 ]
then
    echo "Toto sa spustí ak $Meno je Steve a $Vek je 15."
fi

if [ $Meno == "Daniya" ] || [ $Meno == "Zach" ]
then
    echo "Toto sa spustí ak $Meno je Daniya alebo Zach."
fi

# Pre výrazy sa používa nasledovný formát:
echo $(( 10 + 5 ))

# Na rozdiel od programovacích jazykov shell pracuje v kontexte aktuálneho
# adresára. Môžeš si prehliadať súbory a adresáre v aktuálnom adresári pomocou
# príkazu ls:
ls

# Tieto príkazy majú aj argumenty pre úpravu ich správania:
ls -l # Vypíše zoznam súborov a priečinkov, každý na samostatnom riadku

# Výsledok predchádzajúceho príkazu môže byť využitý priamo ako vstup pre
# ďalší príkaz.
# Príkaz grep filtruje vstupvyužitím poskytnutého vzoru. Takto môžeme vypísať
# iba .txt súbory:
ls -l | grep "\.txt"

# Vstup a výstup príkazu (stdin, stdout, stderr) môžu byť presmerované.
# Toto číta stdin až po ^EOF$ a prepíše hello.py riadkami medzi "EOF":
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Spustí hello.py s rôznymi presmerovaniami pre stdin, stdout a stderr:
python hello.py < "vstup.in"
python hello.py > "vystup.out"
python hello.py 2> "chyby.err"
python hello.py > "vystup-a-chyby.log" 2>&1
python hello.py > /dev/null 2>&1
# Chybový výstup prepíše uvedený súbor, ak už existuje.
# Ak chceš výstup pridať za existujúci obsah, použi ">>":
python hello.py >> "vystup.out" 2>> "chyby.err"

# Prepíše vystup.out, pripojí k chyby.err a spočíta riadky:
info bash 'Basic Shell Features' 'Redirections' > vystup.out 2>> chyby.err
wc -l vystup.out chyby.err

# Spustí príkaz a vypíše deskriptor súboru (napr. /dev/fd/123)
# pozri: man fd
echo <(echo "#ahojsvet")

# Prepíše vystup.out s "#ahojsvet":
cat > vystup.out <(echo "#ahojsvet")
echo "#ahojsvet" > vystup.out
echo "#ahojsvet" | cat > vystup.out
echo "#ahojsvet" | tee vystup.out >/dev/null

# Potichu odstráni dočasné súbory (pridaj '-i' pre interaktivitu)
rm -v vystup.out chyby.err vystup-a-chyby.log

# Príkazy môžu byť nahradené v iných príkazoch použitím $( ):
# Nasledujúci príkaz vypíše počet súborov a adresárov v aktuálnom adresári
echo "Je tu $(ls | wc -l) súborov a priečinkov."

# To isté sa dá spraviť pomocou spätného apostrofu ``, tie však nemôžu byť
# vhniezdené - preferovaný spôsob je preto $( ).
echo "Je tu `ls | wc -l` súborov a priečinkov."

# Bash používa case, ktorý funguje podobne ako switch v Jave a C++:
case "$Premenna" in
    #Zoznam vzorov pre podmienky
    0) echo "Je to nula.";;
    1) echo "Je to jednotka.";;
    *) echo "Nie je to null.";;
esac

# for-cyklus iteruje cez všetky argumenty:
# Obsah premennej $Premenna sa vypíše trikrát.
for Premenna in {1..3}
do
    echo "$Premenna"
done

# Alebo "tradičným" spôsobom:
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Môžu sa použiť aj na súbory..
# Toto spustí príkaz 'cat' na subor1 a subor2
for Premenna in subor1 subor2
do
    cat "$Premenna"
done

# ..alebo na výstup príkazu.
# Toto použije príkaz cat na výstup z ls.
for Vystup in $(ls)
do
    cat "$Vystup"
done

# while-cykklus:
while [ true ]
do
    echo "telo cyklu..."
    break
done

# Môžeš tiež definovať funkice
# Definícia:
function foo ()
{
    echo "Argumenty fungujú rovnako ako pri skriptoch: $@"
    echo "A: $1 $2..."
    echo "Toto je funkcia"
    return 0
}

# alebo jednoducho
bar ()
{
    echo "Iný spôsob definície funkcií"
    return 0
}

# Volanie funkcie
foo "Moje meno je" $Meno

# Existuje veľa užitočných príkazov, ktoré sa oplatí naučiť:
# vypíše posledných 10 riadkov zo subor.txt
tail -n 10 subor.txt
# vypíše prvých 10 riadkov zo subor.txt
head -n 10 subor.txt
# zotriedi riadky zo subor.txt
sort subor.txt
# vypíše alebo vynechá opakované riadky, použitím -d ich vypíše
uniq -d subor.txt
# vypíše iba prvý stĺpecpred znakom ','
cut -d ',' -f 1 subor.txt
# nahradí každý výskyt 'oukej' za 'super' v subor.txt (možnosť použiť regex)
sed -i 's/oukej/super/g' subor.txt
# vypíše všetky riadky zo subor.txt ktoré vyhovujú regexu
# ukážka vypíše riadky ktoré začínajú s "foo" a končia s "bar"
grep "^foo.*bar$" subor.txt
# pre výpis počtu riadkov vyhovujúcich regexu slúži "-c"
grep -c "^foo.*bar$" subor.txt
# pre vyhľadávanie reťazca bez regexu slúži fgrep (alebo grep -F)
fgrep "^foo.*bar$" subor.txt


# Prečítaj si dokumentáciu k Bash shellu použitím príkazu 'help':
help
help help
help for
help return
help source
help .

# Prečítaj si Bash manpage dokumentáciu príkazom 'man'
apropos bash
man 1 bash
man bash

# Prečítaj si info dokumentáciu pomocou 'info' (? pre help)
apropos info | grep '^info.*('
man info
info info
info 5 info

# Prečítaj si bash info dokumentáciu:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
  - ["Juraj Kostolanský", "http://www.kostolansky.sk"]
lang: sk-sk
filename: coffeescript-fr.coffee
---

CoffeeScript je jazyk, ktorý sa kompiluje do ekvivalentného JavaScriptu,
neexistuje peňho interpretácia počas behu programu (runtime).
CoffeeScript sa snaží vytvárať čitateľný, pekne formátovaný a optimalizovaný
JavaScriptový kód pracujúci pod každým JavaScriptovým prostredím.

Pozri tiež [stránku CoffeeScript](http://coffeescript.org/), ktoré obsahuje kompletný tutoriál o CoffeeScripte.

```coffeescript
# CoffeeScript je jazyk hipsterov.
# Ide s trendom mnohých moderných jazykov.
# Komentáre sú podobné tým v Ruby a Pythone, používajú symbol #.

###
Blokové komentáre vyzerajú takto, prekladajú sa priamo do '/ * ... * /'
pre výsledný kód JavaScriptu.

Predtým, než budeš pokračovať, mal by si rozumieť sémantike JavaScriptu.
###

# Priradenia:
cislo = 42   #=> var cislo = 42;
opak  = true #=> var opak = true;

# Podmienky:
cislo = -42 if opak #=> if(opak) { cislo = -42; }

# Funkcie:
stvorec = (x) -> x * x #=> var stvorec = function(x) { return x * x; }

vypln = (nadoba, tekutina = "káva") ->
  "#{nadoba} sa napĺňa tekutinou #{tekutina}..."
#=>var vypln;
#
#vypln = function(nadoba, tekutina) {
#  if (tekutina == null) {
#    tekutina = "káva";
#  }
#  return nadoba + " sa napĺňa tekutinou " + tekutina + "...";
#};

# Rozsahy:
zoznam = [1..5] #=> var zoznam = [1, 2, 3, 4, 5];

# Objekty:
matika =
  zaklad:  Math.sqrt
  stvorec: square
  kocka:   (x) -> x * square x
#=> var matika = {
#  "zaklad": Math.sqrt,
#  "stvorec": square,
#  "kocka": function(x) { return x * square(x); }
#}

# Splat operátor:
zavod = (vitaz, bezci...) ->
  print vitaz, bezci
#=>zavod = function() {
#  var vitaz, bezci;
#  vitaz = arguments[0],
#  bezci = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#  return print(vitaz, bezci);
#};

# Existencia:
alert "Vedel som to!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null)
# { alert("Vedel som to!"); }

# Pole:
kocky = (matika.kocka cislo for cislo in zoznam)
#=>kocky = (function() {
#	var _i, _len, _results;
#	_results = [];
# 	for (_i = 0, _len = zoznam.length; _i < _len; _i++) {
#		cislo = zoznam[_i];
#		_results.push(matika.kocka(cislo));
#	}
#	return _results;
#  })();

jedla = ['brokolica', 'špenát', 'čokoláda']
zjedz jedlo for jedlo in jedla when jedlo isnt 'čokoláda'
#=>jedla = ['brokolica', 'špenát', 'čokoláda'];
#
#for (_k = 0, _len2 = jedla.length; _k < _len2; _k++) {
#  jedlo = jedla[_k];
#  if (jedlo !== 'čokoláda') {
#    zjedz(jedlo);
#  }
#}
```

## Ďalšie zdroje

- [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/)
- [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read)
---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
    - ["Dzianis Dashkevich", "https://github.com/dskecse"]
    - ["Ryan Plant", "https://github.com/ryanplant-au"]
translators:
  - ["Peter Szatmary", "https://github.com/peterszatmary"]
lang: sk-sk
filename: learnelixir-sk.ex
---

Elixir je moderný funkcionálny jazyk vytvorený nad Erlang VM (virtuálnym
strojom). Je plne kompatibilný s Erlangom, ale ponúka viac štandardnú syntax
a množstvo funkcií.

```Elixir

# Jednoriadkový komentár začína symbolom #

# Neexistuje viacriadkový komentár, avšak je možné vyskladať za sebou viac
# jednoriadkových komentárov.

# Pre spustenie Elixir shellu zadajte príkaz `iex`
# Kompiláciu zdrojových kódov vykonáte príkazom `elixirc`

# Obe príkazy by sa už mali nachádzať v path pokiaľ ste nainštalovali elixir
# správne.

## ---------------------------
## -- Základné typy
## ---------------------------

# Čísla
3    # integer
0x1F # integer
3.0  # float

# Atómy, sú literály, konštanty s rovnakým menom. Začínajú s `:`.
:ahoj # atom

# Tzv. Tuples sú v pamäti uložené súvisle.
{1,2,3} # tuple

# Pristúpiť k tuple elementu vieme pomocou funkcie `elem`:
elem({1, 2, 3}, 0) #=> 1

# Zoznamy sú implementované ako linkované zoznamy.
[1,2,3] # zoznam

# Vieme pristúpiť k hlavičke (head) a chvostu (tail) zoznamu:
[head | tail] = [1,2,3]
head #=> 1
tail #=> [2,3]

# V Elixire, rovnako ako v Erlangu, `=` znamená pattern matching a nie
# klasické priradenie.
#
# To znamená, že ľavá strana (pattern / vzor) je postavená oproti pravej
# strane.
#
# Takto funguje aj príklad vyššie s čítaním hlavičky a chvosta zoznamu.

# Pattern match končí chybou ak sa obe strany nezhodujú, v tomto príklade majú
# tuples rôznu veľkosť.
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# Binárne typy
<<1,2,3>> # binary

# Reťazce a zoznamy znakov
"ahoj" # reťazec
'ahoj' # zoznam znakov

# Viacriadkový reťazec
"""
Ja som viacriadkový
reťazec.
"""
#=> "Ja som viacriadkový\nreťazec.\n"

# Reťazce sú kódované v UTF-8:
"héllò" #=> "héllò"

# Reťazce sú skutočne iba binárne typy, a zoznamy znakov sú iba zoznamy.
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# `?a` v elixire vráti ASCII číselnú reprezentáciu pre znak `a`
?a #=> 97

# Pre spájanie zoznamov sa používa `++`, pre binárne typy `<>`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'ahoj ' ++ 'svet'  #=> 'ahoj svet'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"Ahoj " <> "svet"  #=> "ahoj svet"

# Rozsahy sú reprezentované ako `začiatok..koniec` (obe inkluzívne)
1..10 #=> 1..10
dolna..horna = 1..10 # Na rozsahy možno použiť rovnako pattern matching
[dolna, horna] #=> [1, 10]

# Mapy sú páry kľúč-hodnota
pohlavia = %{"david" => "muž", "gillian" => "žena"}
pohlavia["david"] #=> "muž"

# Mapy s kľúčmi reprezentovanými atómami môžu byť použité takto
pohlavia = %{david: "muž", gillian: "žena"}
pohlavia.gillian #=> "žena"

## ---------------------------
## -- Operátory
## ---------------------------

# Trošku matematiky
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# V elixire operátor `/` vždy vráti float (reálne číslo).

# Pre celočíselné delenie sa používa `div`
div(10, 2) #=> 5

# Pre celočíselné delenie so zvyškom `rem`
rem(10, 3) #=> 1

# Boolean operátory: `or`, `and` a `not`.
# Tieto operátori očakávajú ako svoj prvý argument boolean.
true and true #=> true
false or true #=> true
# 1 and true    #=> ** (ArgumentError) argument error

# Elixir tiež poskytuje `||`, `&&` a `!` , ktoré akceptujú argumenty
# ľubovoľného typu.
# Všetky hodnoty okrem `false` a `nil` budú vyhodnotené ako true.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil
!true #=> false

# Pre porovnávanie máme: `==`, `!=`, `===`, `!==`, `<=`,
`>=`, `<` a `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# `===` a `!==` sú viac striktné pri porovnávaní celých čísel (integer) a
# desatinných čísel (float).
1 == 1.0  #=> true
1 === 1.0 #=> false

# Vieme porovnať dokonca dva rôzne údajové typy:
1 < :ahoj #=> true

# Celkové poradie triedenia:
#
# číslo < atom < referencia < funkcia < port < pid < tuple < zoznam < bitový
# string

# Výrok Joe Armstronga: "Aktuálne poradie nie je dôležité, ale
# dôležité je to, že celkové poradie je dobre definované."

## ---------------------------
## -- Riadenie toku
## ---------------------------

# `if` výraz
if false do
  "Toto nebude nikdy videné"
else
  "Toto bude"
end

# Existuje aj `unless`
unless true do
  "Toto nebude nikdy videné"
else
  "Toto bude"
end

# Pamätáte sa na pattern matching? Mnoho štruktúr pre riadenie toku v
# elixire sa spoliehajú práve na pattern matching.

# `case` dovolí nám porovnať hodnotu oproti mnohým vzorom:
case {:one, :two} do
  {:four, :five} ->
    "Toto nebude zhodné"
  {:one, x} ->
    "Toto bude zhodné a nastaví `x` na hodnotu `:two` "
  _ ->
    "Toto bude zhodné z ľubovoľnou hodnotou."
end

# Je zvyčajné nastaviť hodnotu do `_` ak ju nepotrebujete.
# Napríklad, ak je pre nás potrebná iba hlavička zoznamu (head):
[head | _] = [1,2,3]
head #=> 1

# Pre lepšiu čitateľnosť môžme urobiť nasledovné:
[head | _tail] = [:a, :b, :c]
head #=> :a

# `cond` dovoľuje kontrolovať viac podmienok naraz.
# Použite `cond` namiesto vnorovania mnohých `if` výrazov.
cond do
  1 + 1 == 3 ->
    "Nebudem nikdy videný"
  2 * 5 == 12 ->
    "Ani ja"
  1 + 2 == 3 ->
    "Ja budem"
end

# Je bežné nastaviť poslednú podmienku rovnajúcu sa `true` , ktorá bude vždy
# zodpovedať.
cond do
  1 + 1 == 3 ->
    "Nebudem nikdy videný"
  2 * 5 == 12 ->
    "Ani ja"
  true ->
    "Ale ja budem (je to v podstate vetva else)"
end

# `try/catch` sa používa na zachytenie hodnôt, ktoré boli vyhodené, takisto
# podporuje `after` klauzulu, ktorá je zavolaná vždy, či bola hodnota
# zachytená alebo nie.
try do
  throw(:ahoj)
catch
  message -> "Mám #{message}."
after
  IO.puts("Som after klauzula.")
end
#=> Som after klauzula
# "Mám :ahoj"

## ---------------------------
## -- Moduly a funkcie
## ---------------------------

# Anonymné funkcie (všimnite si bodku)
stvorec = fn(x) -> x * x end
stvorec.(5) #=> 25

# Takisto akceptujú viax klauzúl a tzv. stráže (guards).
# Stráže vám umožnia pattern matching ešte viac zlepšiť, tieto časti sú
# označené kľúčovým slovom `when`:
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir tiež poskytuje množstvo vstavaných funkcií.
# Tie sú dostupné v aktuálnom scope (viditeľnej oblasti).
is_number(10)    #=> true
is_list("hello") #=> false
elem({1,2,3}, 0) #=> 1

# Možno zgrupovať viac funkcií do jedného modulu. V module použite `def`
# na definíciu funkcie.
defmodule Matematika do
  def sucet(a, b) do
    a + b
  end

  def na_druhu(x) do
    x * x
  end
end

Matematika.sucet(1, 2)  #=> 3
Matematika.na_druhu(3) #=> 9

# Na zkompilovanie našeho Matematika modulu ho uložte ako `math.ex`  a použite
# `elixirc` v termináli: elixirc math.ex

# V module môžme definovať funkcie s `def` a privátne funkcie s `defp`.
# Funkcia definovaná s `def` je možné volať z iných modulov, privátne funkcie
# môžu byť volané iba lokálne.
defmodule SukromnaMatematika do
  def sucet(a, b) do
    rob_sucet(a, b)
  end

  defp rob_sucet(a, b) do
    a + b
  end
end

SukromnaMatematika.sucet(1, 2)    #=> 3
# SukromnaMatematika.rob_sucet(1, 2) #=> ** (UndefinedFunctionError)

# Deklarácie funkcií tiež podporujú stráže (guards) a viacnásobné klauzuly:

defmodule Geometria do
  def oblast({:obdlznik, w, h}) do
    w * h
  end

  def oblast({:kruh, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometria.oblast({:obdlznik, 2, 3}) #=> 6
Geometria.oblast({:kruh, 3})       #=> 28.25999999999999801048
# Geometria.oblast({:kruh, "nie_je_cislo"})
#=> ** (FunctionClauseError) no function clause matching in Geometria.oblast/1

# Vďaka nemeniteľnosti (immutability) je rekurzia významnou časťou elixiru
defmodule Rekurzia do
  def sumuj_zoznam([hlavicka | schvost], acc) do
    sumuj_zoznam(chvost, acc + hlavicka)
  end

  def sumuj_zoznam([], acc) do
    acc
  end
end

Rekurzia.sumuj_zoznam([1,2,3], 0) #=> 6

# Elixir moduly podporujú atribúty, existujú vstavané atribúty a takisto
# môžte pridávať vlastné.
defmodule MojModul do
  @moduledoc """
  Toto je vstavaný atribút v príkladovom module.
  """

  @moj_udaj 100 # Toto je vlastný atribút.
  IO.inspect(@moj_udaj) #=> 100
end

# Pipe operátor (rúra) |> umožnuje predať výsledok výrazu ako prvý parameter
# do ďalšej funkcie.

Range.new(1,10)
|> Enum.map(fn x -> x * x end)
|> Enum.filter(fn x -> rem(x, 2) == 0 end)
#=> [4, 16, 36, 64, 100]

## ---------------------------
## -- Štruktúry a výnimky
## ---------------------------

# Štruktúry sú rozšírenia postavené na mapách, ktoré prinášajú defaultné
# hodnoty, garancie v čase kompilácie a polymorfizmus do Elixiru.
defmodule Osoba do
  defstruct meno: nil, vek: 0, vyska: 0
end

joe_info = %Osoba{ meno: "Joe", vek: 30, vyska: 180 }
#=> %Osoba{vek: 30, vyska: 180, meno: "Joe"}

# Prístup k hodnote mena
joe_info.meno #=> "Joe"

# Zmena hodnoty veku
starsi_joe_info = %{ joe_info | vek: 31 }
#=> %Osoba{vek: 31, vyska: 180, meno: "Joe"}

# `try` blok s kľúčovým slovom `rescue` sa používa na riadenie výnimiek
try do
  raise "nejaký error"
rescue
  RuntimeError -> "zachytí runtime error"
  _error -> "zachytí ľubovoľný iný error"
end
#=> "zachytí runtime error"

# Každá výnimka má správu
try do
  raise "nejaký error"
rescue
  x in [RuntimeError] ->
    x.message
end
#=> "nejaký error"

## ---------------------------
## -- Konkurencia
## ---------------------------

# Elixir sa pri konkurencii spolieha na Actor model. Všetko čo je
# potrebné na písanie konkuretných programov v elixire sú tri primitívy:
# spawning procesy, posielanie a prijímanie správ.

# Na spustnenie nového procesu použijeme `spawn` funkciu, ktorá má ako
# parameter funkciu.
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>

# `spawn` vracia pid (identifikátor procesu), tento pid možno použiť na
# posielanie správ procesu. Správu pošleme `send` operatorátorom.
# Aby všetko fungovalo ako má, potrebujeme byť schopný správu prijať. To
# dosiahneme s `receive` mechanizmom:

# `receive do` blok sa používa na počúvanie správ a ich spracúvavanie v čase
# prijatia. `receive do` blok spracuje iba jednu prijatú správu. Pre
# spracovanie viacerých správ, musí funkcia s `receive do` blokom rekurzívne
# volať samu seba, aby sa dostala opäť do `receive do` bloku.

defmodule Geometria do
  def slucka_oblasti do
    receive do
      {:obdlznik, w, h} ->
        IO.puts("Oblast = #{w * h}")
        slucka_oblasti()
      {:kruh, r} ->
        IO.puts("Oblast = #{3.14 * r * r}")
        slucka_oblasti()
    end
  end
end

# Kompiluj modul a vytvor proces, ktorý vyhodnotí `slucka_oblasti` v shelli

pid = spawn(fn -> Geometria.slucka_oblasti() end) #=> #PID<0.40.0>
# Alternatívne
pid = spawn(Geometria, :slucka_oblasti, [])

# Pošli správu ku `pid`, ktorá bude v zhode so vzorom v receive časti
send pid, {:obdlznik, 2, 3}
#=> Oblast = 6
#   {:obdlznik,2,3}

send pid, {:kruh, 2}
#=> Oblast = 12.56000000000000049738
#   {:kruh,2}

# Shell je takisto proces, môžete použiť `self` pre zistenie aktuálneho pid-u
self() #=> #PID<0.27.0>

## ---------------------------
## -- Agenti
## ---------------------------

# Agent je proces, ktorý udržuje informácie o meniacej sa hodnote

# Vytvor agenta s `Agent.start_link` parametrom, ktorého je funkcia
# Iniciálny stav agenta bude čokoľvek, čo daná funkcia vráti
{ok, moj_agent} = Agent.start_link(fn -> ["cervena, zelena"] end)

# `Agent.get` vezme meno agenta a `fn` , ktorej je odovzdaný aktuálny stav
# Čokoľvek čo `fn` vráti je to, čo dostanete späť
Agent.get(moj_agent, fn farby -> farby end) #=> ["cervena, "zelena"]

# Zmena stavu agenta rovnakým spôsobom
Agent.update(moj_agent, fn farby -> ["modra" | farby] end)
```

## Referencie

* [Začíname](http://elixir-lang.org/getting-started/introduction.html) z
[Elixir stránky](http://elixir-lang.org)
* [Elixir dokumentácia](http://elixir-lang.org/docs/master/)
* [Elixir programovanie](https://pragprog.com/book/elixir/programming-elixir)
 od Dave Thomasa
* [Elixir ťahák](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf)
* [Nauč sa kúsok Erlangu pre veľké dobro!](http://learnyousomeerlang.com/) od
Freda Heberta
* [Erlang programovanie: Softvér pre konkurentný svet](https://pragprog
.com/book/jaerlang2/programming-erlang) od Joe Armstronga
---
category: tool
tool: git
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Leo Rudberg" , "http://github.com/LOZORD"]
    - ["Betsy Lorton" , "http://github.com/schbetsy"]
    - ["Bruno Volcov", "http://github.com/volcov"]
    - ["Andrew Taylor", "http://github.com/andrewjt71"]
translators:
    - ["Terka Slanináková", "http://github.com/TerkaSlan"]
lang: sk-sk
filename: LearnGit-sk.txt
---

Git je distribuovaný systém riadenia revízií a správy zdrojového kódu.

Funguje robením "snímkov" tvojho projektu, s ktorými ďalej pracuje na revíziach a správe zdrojových kódov.

## Koncept Revízií

### Čo je riadenie revízií?

Riadenie revízií je systém, ktorý postupom času zaznamenáva zmeny súboru (súborov).

### Centralizované Revízie VS Distribuované revízie

* Centralizované riadenie revízií sa zameriava na synchronizáciu, sledovanie a zálohovanie súborov.
* Distribuované riadenie revízií sa zameriava na zdieľanie zmien. Kaťdá zmena má jedinečný identifikátor (id).
* Distribuované systémy nemajú definovanú štruktúru. S gitom môžeš mať centralizovaný systém v subversion (SVN) štýle.

[Ďalšie informácie](http://git-scm.com/book/en/Getting-Started-About-Version-Control)

### Prečo Používať Git? 

* Môžeš pracovať offline.
* Spolupráca s ostatnými je jednoduchá!
* Vetvenie je jednoduché!
* Zlučovanie je jednoduché!
* Git je rýchly.
* Git je flexibilný.

## Architektúra Gitu


### Repozitár

Skupina súborov, adresárov, minulých záznamov, commitov (konkrétnych revízií) a odkazy na aktuálu vetvu (HEADs). Predstav si ho ako údajovú štruktúru, kde ti každý "prvok" zdrojového kódu poskytne (okrem iného) prístup k minulým revíziam.

Git repozitár sa skladá z .git adresára a pracovného stromu

### .git Adresár (časť repozitára)

.git  adresár obsahuje všetky konfigurácie, logy, vetvy, odkaz na aktuálnu vetvu (HEAD) a ostatné.
[Detailný zoznam.](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### Pracovný Strom (Working Tree - časť repozitára)

Toto sú adresáre a súbory v tvojom repozitári. Tiež sa tomu hovorí pracovný adresár.

### Index (časť .git adresára)

Index je také odpočívadlo Gitu. Je to v podstate vrstva, ktorá oddeľuje pracovný strom od Git repozitára. Toto dáva vývojárom viac možností nad tým, čo do repozitára naozaj pošlú.

### Commit

Commit je "snímka" zmien, či manipulácií s tvojím Pracovným Stromom. Ak si napríklad pridal 5 súborov a odstránil 2 ďalšie, tieto zmeny budú zachytené v commite. Ten môže (ale nemusí) byť zverejnený (pushed) do iných repozitárov.

### Vetva (Branch)

Vetva je ukazateľ na posledný vykonaný commit. Po ďalších commitnutiach sa ukazateľ bude automaticky posúvať na ten najnovší.

### Tag

Tag je označenie špecifického bodu v minulosti. Typicky sa používa na značenie vydaných verzií (v1.0, atď).

### HEAD a head (časť .git adresára)

HEAD je ukazateľ na aktuálnu vetvu. Repozitár má len 1 *aktívny* HEAD.
head je ukazateľ, ktorý môže ukazovať na akýkoľvek commit. Repozitár môže mať niekoľko headov.

### Štádia Gitu
* Modified - Súbor bol zmenený, no nebol ešte commitnutý do Git Databázy.
* Staged - Zmenený súbor, ktorý pôjde do najbližšieho commit snímku.
* Committed - Súbory boli commitnuté do Git Databázy.

### Koncepčné zdroje

* [Git Pre Informatikov](http://eagain.net/articles/git-for-computer-scientists/)
* [Git Pre Designerov](http://hoth.entp.com/output/git_for_designers.html)


## Príkazy


### init

Vytvorí prázdny Git repozitár. Jeho nastavenia, uložené informácie a mnoho iného sú uložené v adresári (zložke) s názvom ".git".

```bash
$ git init
```

### config

Konfiguruj nastavenia. Či už pre repozitár, samotný systém, alebo globálne konfigurácie (súbor pre globálny config je `~/.gitconfig`).


```bash
# Zobraz a Nastav Základné Konfiguračné Premenné (Globálne)
$ git config --global user.email "MôjEmail@Zoho.com"
$ git config --global user.name  "Moje Meno	"
```

[Prečítaj si viac o git configu.](http://git-scm.com/docs/git-config)

### pomoc

Máš tiež prístup k extrémne detailnej dokumentácií pre každý príkaz (po anglicky). Hodí sa, ak potrebuješ pripomenúť semantiku.

```bash
# Rýchlo zobraz všetky dostupné príkazy
$ git help

# Zobraz všetky dostupné príkazy
$ git help -a

# Zobraz konkrétnu pomoc - použivateľský manuál
# git help <príkaz_tu>
$ git help add
$ git help commit
$ git help init
# alebo git <príkaz_tu> --help
$ git add --help
$ git commit --help
$ git init --help
```

### ignoruj súbory

Zámerne prestaneš sledovať súbor(y) a zložky. Typicky sa používa pre súkromné a dočasné súbory, ktoré by boli inak zdieľané v repozitári.
```bash
$ echo "temp/" >> .gitignore
$ echo "private_key" >> .gitignore
```


### status

Na zobrazenie rozdielov medzi indexovými súbormi (tvoj pracovný repozitár) a aktuálnym HEAD commitom.


```bash
# Zobrazí vetvu, nesledované súbory, zmeny a ostatné rozdiely
$ git status

# Zistí iné vychytávky o git statuse
$ git help status
```

### add

Pripraví súbory na commit pridaním do tzv. staging indexu. Ak ich nepridáš pomocou `git add` do staging indexu, nebudú zahrnuté v commitoch!

```bash
# pridá súbor z tvojho pracovného adresára
$ git add HelloWorld.java

# pridá súbor z iného adresára
$ git add /cesta/k/súboru/HelloWorld.c

# Môžeš použiť regulárne výrazy!
$ git add ./*.java
```
Tento príkaz len pridáva súbory do staging indexu, necommituje ich do repozitára.

### branch

Spravuj svoje vetvy. Môžeš ich pomocou tohto príkazu zobraziť, meniť, vytvoriť, či zmazať.

```bash
# zobraz existujúce vetvy a vzdialené repozitáre
$ git branch -a

# vytvor novú vetvu
$ git branch myNewBranch

# vymaž vetvu
$ git branch -d myBranch

# premenuj vetvu
# git branch -m <starémeno> <novémeno>
$ git branch -m mojaStaraVetva mojaNovaVetva

# zmeň opis vetvy
$ git branch myBranchName --edit-description
```

### tag

Spravuj svoje tagy

```bash
# Zobraz tagy
$ git tag
# Vytvor tag so správou
# -m špecifikuje správu, ktorá bude s tagom uložená.
# Ak nešpeficikuješ správu pri tagu so správou,
# Git spustí tvoj editor, aby si ju napísal.
$ git tag -a v2.0 -m 'moja verzia 2.0'
# Ukáž informácie o tagu
# Zobrazí zadané informácie, dátum tagnutia commitu
# a správu pred zobrazením informácií o commite.
$ git show v2.0
# Zverejní (pushne) jediný tag do vzdialeného repozitára
$ git push origin v2.0
# Zverejní viacero tagov do vzdialeného repozitára
$ git push origin --tags
```

### checkout

Aktualizuje všetky súbory v pracovnom strome, aby odpovedali verzií v indexe, alebo v inom strome.

```bash
# Aktualizuj strom, aby odpovedal (predvolene) 
# hlavnej vetve repozitáru (master branch)
$ git checkout
# Aktualizuj strom, aby odpovedal konrkétnej vetve
$ git checkout menoVetvy
# Vytvor novú vetvu & prepni sa na ňu
# ekvivalentný príkaz: "git branch <meno>; git checkout <meno>"
$ git checkout -b nováVetva
```

### clone

"Naklonuje", alebo vytvorí kópiu existujúceho repozitára do nového adresára. Tiež pridá špeciálne ďiaľkovo-monitorujúce vetvy (remote-tracking branches), ktoré ti umožnia zverejňovať do vzdialených vetiev.

```bash
# Naklonuj learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git
# povrchné klonovanie - rýchlejšie, uloží iba najnovšiu snímku
$ git clone --depth 1 https://github.com/adambard/learnxinyminutes-docs.git
# naklonuj iba konkrétnu vetvu
$ git clone -b master-cn https://github.com/adambard/learnxinyminutes-docs.git --single-branch
```

### commit

Uloží aktuálny obsah indexu v novom "commite". Ten obsahuje vytvorené zmeny a s nimi súvisiace správy vytvorené použivateľom.

```bash
# commitni so správou
$ git commit -m "Pridal som multiplyNumbers() funkciu do HelloWorld.c"

# automaticky pridaj zmenené a vymazané súbory do staging indexu,  potom ich commitni.
$ git commit -a -m "Zmenil som foo.php a vymazal bar.php"

# zmeň posledný commit (toto nahradí predchádzajúci commit novým)
$ git commit --amend -m "Správna správa"
```

### diff

Ukáže rozdiel medzi súborom v pracovnom repozitári, indexe a commitoch.

```bash
# Ukáž rozdiel medzi pracovným repozitárom a indexom.
$ git diff

# Ukáž rozdiely medzi indexom a najnovším commitom.
$ git diff --cached

# Ukáž rozdiely medzi pracovným adresárom a najnovším commitom.
$ git diff HEAD
```

### grep

Umožní ti rýchlo prehľadávať repozitár.

Možná konfigurácia:

```bash
# Nastav, aby sa vo výsledkoch vyhľadávania zobrazovalo číslo riadku
$ git config --global grep.lineNumber true

# Urob výsledky vyhľadávania čitateľnejšie, vrátane zoskupovania
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Vďaka Travisovi Jefferymu za túto sekciu
# Hľadaj "názovPremennej" vo všetkých java súboroch
$ git grep 'názovPremennej' -- '*.java'

# Hľadaj riadok, ktorý obsahuje "názovPoľa" a "add" alebo "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
```

Google je tvoj kamarát; pre viac príkladov skoč na
[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Zobral commity do repozitára.

```bash
# Zobraz všetky commity
$ git log

# Zobraz iba správy a referencie commitov
$ git log --oneline

# Zobraz zlúčené (merged) commity
$ git log --merges

# Zobraz všetky commity vo forme ASCII grafu
$ git log --graph
```

### merge

"Zlúč" zmeny externých commitov do aktuálnej vetvy.

```bash
# Zlúč vybranú vetvu do aktuálnej.
$ git merge názovVetvy

# Vždy vytvor zlučovací commit
$ git merge --no-ff názovVetvy
```

### mv

Premenuj, alebo presuň súbor

```bash
# Premenuj súbor
$ git mv HelloWorld.c HelloNewWorld.c

# Presuň súbor
$ git mv HelloWorld.c ./nová/cesta/HelloWorld.c

# "Nasilu" premenuj, alebo presuň
# "existujúciSúbor" už v adresári existuje, bude prepísaný
$ git mv -f môjSúbor existujúciSúbor
```

### pull

Uloží obsah repozitára a zlúči ho s inou vetvou.

```bash
# Aktualizuje tvoj lokálny repozitár zlúčením nových zmien
# zo vzdialených "origin" a "master" vetiev.
# git pull <alias-vzdialeného-repo> <vetva>
$ git pull origin master

# Predvolene, git pull aktualizuje tvoju aktuálnu vetvu
# zlúčením nových zmien zo vzdialenej vetvy 
$ git pull

# Zlúč zmeny zo vzdialenej vetvy a presuň vetvu do nového základného commitu (rebase)
# vetva commitne na tvoj lokálny repozitár, ekvivalentný príkaz: "git pull <alias-vzdialeného-repo> <vrstva>, git rebase <branch>"
$ git pull origin master --rebase
```

### push

Zverejní a zlúči zmeny z lokálnej do vzdialenej vetvy.

```bash
# Zverejni a zlúč zmeny z lokálneho repozitára do
# vzdialených vetiev s názvom "origin" a "master".
# git push <vzdialené> <vetvy>
$ git push origin master

# Predvolene git zverejní a zlúči zmeny z
# aktuálnej vetvy do vzdialenej vetvy s ňou spojenej
$ git push

# Na spojenie lokálnej vetvy so vzdialenou pridaj -u:
$ git push -u origin master
# Kedykoľvek budeš chcieť zverejniť z rovnakej lokálnej vetvy, použi príkaz:
$ git push
```

### stash

Umožní ti opustiť chaotický stav pracovného adresára a uloží ho na zásobník nedokončených zmien, ku ktorým sa môžeš kedykoľvek vrátiť.

Povedzme, že si urobil nejaké zmeny vo svojom git repozitári, ale teraz potrebuješ pullnúť zo vzdialenej repo. Keďže máš necommitnuté zmeny, príkaz `git pull` nebude fungovať. Namiesto toho môžeš použiť `git stash` a uložiť svoje nedokončené zmeny na zásobník!

```bash
$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 added the index file"
  HEAD is now at 049d078 added the index file
  (To restore them type "git stash apply")
```

Teraz môžeš uložiť vzdialenú vetvu!

```bash
$ git pull
```

Over, či je všetko v poriadku

```bash
$ git status
# On branch master
nothing to commit, working directory clean
```

Môžeš si pozrieť, čo za chaos je na zásobníku cez `git stash list`.
Nedokončené zmeny sú uložené ako Last-In-First-Out (Prvý dnu, posledný von) štruktúra, navrchu sa objavia najnovšie zmeny.

```bash
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
```

Keď so zmenami budeš chcieť pracovať, odstráň ich zo stacku.

```bash
$ git stash pop
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   index.html
#      modified:   lib/simplegit.rb
#
```

`git stash apply` urobí presne to isté

Hotovo, môžeš pokračovať v práci!

[Čítaj viac.](http://git-scm.com/book/en/v1/Git-Tools-Stashing)

### rebase (pozor)

Zober všetky zmeny commitnuté do vetvy a aplikuj ich na inú vetvu.
*Tento príkaz nerob na verejných repozitároch*.

```bash
# Aplikuj commity z experimentálnej vetvy na master
# git rebase <základnáVetva> <ináVetva>
$ git rebase master experimentBranch
```

[Čítaj viac.](http://git-scm.com/book/en/Git-Branching-Rebasing)

### reset (pozor)

Resetni HEAD (ukazateľ na aktuálnu vetvu) do konrkétneho stavu. To ti umožní vziať späť zlúčenia, zverejnenia, commity, pridania atď. Je to užitočný, no nebezpečný príkaz, pokiaľ nevieš, čo robíš.

```bash
# Resetni index (vrstvu medzi pracovným stromom a Git repozitárom), aby odpovedal najnovšiemu commitu (adresár ostane nezmenený)
$ git reset

# Resetni index, aby odpovedal najnovšiemu commitu (adresár sa prepíše)
$ git reset --hard

# Presunie vrchol aktuálnuej vetvy do konkrétneho commitu (adresár ostane nezmenený)
# všetky zmeny sú zachované v adresári.
$ git reset 31f2bb1

# Presunie vrchol aktuálnuej vetvy naopak do konkrétneho commitu
# a zosúladí ju s pracovným adresárom (vymaže nekomitnuté zmeny).
$ git reset --hard 31f2bb1
```
### revert

Vezme naspäť ("od-urobí") commit. Nezamieňaj s resetom, ktorý obnoví stav
projektu do predchádzajúceho bodu v čase. Revert pridá nový commit, inverzný tomu, ktorý chceš vymazať, tým ho od-urobí.

```bash
# Vezmi späť konkrétny commit
$ git revert <commit>
```

### rm

Opak od git add, rm odstráni súbory z aktuálneho pracovného stromu.

```bash
# odstráň HelloWorld.c
$ git rm HelloWorld.c

# Odstráň súbor z vnoreného adresára
$ git rm /pather/to/the/file/HelloWorld.c
```

## Ďalšie informácie

* [tryGit - Zábavný interaktívny spôsob, ako sa naučiť Git.](http://try.github.io/levels/1/challenges/1)

* [Udemy Git Tutoriál: Kompletný návod](https://blog.udemy.com/git-tutorial-a-comprehensive-guide/)

* [Git Immersion - Návod, ktorý Ťa prevedie základmi Gitu](http://gitimmersion.com/)

* [git-scm - Video Tutoriály](http://git-scm.com/videos)

* [git-scm - Dokumentácia](http://git-scm.com/docs)

* [Atlassian Git - Tutoriály & Postupy](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)

* [Git - jednoducho](http://rogerdudler.github.io/git-guide/index.html)

* [Pro Git](http://www.git-scm.com/book/en/v2)

* [Úvod do Gitu a GitHubu pre začiatočníkov (Tutoriál)](http://product.hubspot.com/blog/git-and-github-tutorial-for-beginners)
---
language: json
filename: learnjson-sk.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
  - ["Juraj Kostolanský", "http://www.kostolansky.sk"]
lang: sk-sk
---

Nakoľko je JSON extrémne jednoduchý formát na výmenu dát, toto bude
pravdepodobne najjednoduchšie "Learn X in Y Minutes".

JSON v jeho základnej forme nemá komentáre, ale veľa parserov akceptuje
komentáre v štýle C (`//`, `/* */`). V tomto návode však bude všetko
100% valídny JSON.

```json
{
  "kľúč": "hodnota",

  "kľúč": "musí byť vždy uzavretý v dvojitých uvodzovkách",
  "čísla": 0,
  "reťazce": "Ahøj, svet. Unicode je povolený pri použití \"únikovej sekvencie (escaping)\".",
  "boolean?": true,
  "nič": null,

  "veľké číslo": 1.2e+100,

  "objekty": {
    "komentár": "Väčšina štruktúry bude pochádzať z objektov.",

    "pole": [0, 1, 2, 3, "Pole môže obsahovať čokoľvek.", 5],

    "iný objekt": {
      "komentár": "Môžu byť vhniezdené, čo môže byť užitočné."
    }
  },

  "nezmysly": [
    {
      "zdroje draslíka": ["banány"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "alternatívny štýl": {
    "komentár": "sleduj toto!"
  , "pozícia čiarky": "nezáleží na nej - pokiaľ je pred hodnotou, všetko je ok"
  , "iný komentár": "pekné, že?"
  },

  "to bolo rýchle": "A už sme aj na konci. Teraz ovládš všetko, čo ti JSON môže ponúknuť."
}
```
---
language: latex
contributors:
    - ["Chaitanya Krishna Ande", "http://icymist.github.io"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
    - ["Sricharan Chiruvolu", "http://sricharan.xyz"]
translators:
    - ["Terka Slanináková", "http://github.com/TerkaSlan"]
filename: learn-latex-sk.tex
---

```tex
% Všetky komentáre začínajú s %
% Viac-riadkové komentáre sa nedajú urobiť

% LaTeX NIE JE WYSIWY ("What You See Is What You Get") software ako MS Word, alebo OpenOffice Writer

% Každý LaTeX príkaz začína s opačným lomítkom (\)

% LaTeX dokumenty začínajú s definíciou typu  kompilovaného dokumentu
% Ostatné typy sú napríklad kniha (book), správa (report), prezentácia (presentation), atď.
% Možnosti dokumentu sa píšu do [] zátvoriek. V tomto prípade tam upresňujeme veľkosť (12pt) fontu.
\documentclass[12pt]{article}

% Ďalej definujeme balíčky, ktoré dokuemnt používa.
% Ak chceš zahrnúť grafiku, farebný text, či zdrojový kód iného jazyka, musíš rozšíriť možnosti LaTeXu dodatočnými balíčkami.
% Zahŕňam float a caption. Na podporu slovenčiny treba zahrnúť aj utf8 balíček.
\usepackage{caption}
\usepackage{float}
\usepackage[utf8]{inputenc}
% Tu môžme definovať ostatné vlastnosti dokumentu!
% Autori tohto dokumentu, "\\*" znamená "choď na nový riadok"
\author{Chaitanya Krishna Ande, Colton Kohnke \& Sricharan Chiruvolu, \\*Preklad: Terka Slanináková}
% Vygeneruje dnešný dátum
\date{\today}
\title{Nauč sa LaTeX za Y Minút!}
% Teraz môžme začať pracovať na samotnom dokumente.
% Všetko do tohto riadku sa nazýva "Preambula" ("The Preamble")
\begin{document} 
% ak zadáme položky author, date a title, LaTeX vytvorí titulnú stranu.
\maketitle

% Väčšina odborných článkov má abstrakt, na jeho vytvorenie môžeš použiť preddefinované príkazy.
% Ten by sa mal zobraziť v logickom poradí, teda po hlavičke,
% no pred hlavnými sekciami tela.. 
% Tento príkaz je tiež dostupný v triedach article a report.
% Tzv. makro "abstract" je fixnou súčasťou LaTeXu, ak chceme použiť abstract
% a napísať ho po slovensky, musíme ho redefinovať nasledujúcim príkazom
\renewcommand\abstractname{Abstrakt}

\begin{abstract}
LaTeX dokumentácia v LaTeXe! Aké netradičné riešenie cudzieho nápadu!
\end{abstract}

% Príkazy pre sekciu sú intuitívne
% Všetky nadpisy sekcií sú pridané automaticky do obsahu.
\section{Úvod}
Čaute, volám sa Colton a spoločne sa pustíme do skúmania LaTeXu (toho druhého)!

\section{Ďalšia sekcia}
Toto je text ďalšej sekcie. Myslím, že potrebuje podsekciu.

\subsection{Toto je podsekcia} % Podsekcie sú tiež intuitívne.
Zdá sa mi, že treba ďalšiu.

\subsubsection{Pytagoras}
To je ono!
\label{subsec:pytagoras}

% Použitím '*' môžeme potlačiť zabudované číslovanie LaTeXu.
% Toto funguje aj na iné príkazy. 
\section*{Toto je nečíslovaná sekcia} 
Všetky číslované byť nemusia!

\section{Nejaké poznámočky}
Zarovnávať veci tam, kde ich chceš mať, je všeobecne ľahké. Ak 
potrebuješ \\ nový \\ riadok \\ pridaj \textbackslash\textbackslash do 
zdrojového kódu. \\ 

\section{Zoznamy}
Zoznamy sú jednou z najjednoduchších vecí vôbec! Treba mi zajtra nakúpiť, urobím si zoznam.
\begin{enumerate} % "enumerate" spustí číslovanie prvkov.
  % \item povie LaTeXu, ako že treba pripočítať 1 
  \item Vlašský šalát.
  \item 5 rožkov.
  \item 3 Horalky.
  % číslovanie môžeme pozmeniť použitím []
  \item[koľko?] Stredne veľkých guličkoviek.

  Ja už nie som položka zoznamu, no stále som časť "enumerate".

\end{enumerate} % Všetky prostredia končia s "end".

\section{Matika}

Jedným z primárnych použití LaTeXu je písanie akademických, či technických prác. Zvyčajne za použitia matematiky a vedy. Podpora špeciálnych symbolov preto nemôže chýbať!\\

Matematika má veľa symbolov, omnoho viac, než by klávesnica mohla reprezentovať;
Množinové a relačné symboly, šípky, operátory a Grécke písmená sú len malou ukážkou.\\

Množiny a relácie hrajú hlavnú rolu v mnohých matematických článkoch.
Takto napíšeš, že niečo platí pre všetky x patriace do X, $\forall$ x $\in$ X. \\
% Všimni si, že som pridal $ pred a po symboloch. Je to 
% preto, lebo pri písaní sme v textovom móde, 
% no matematické symboly existujú len v matematickom. 
% Vstúpime doňho z textového práve '$' znamienkami.
% Platí to aj opačne. Premenná môže byť zobrazená v matematickom móde takisto.
% Do matematického módu sa dá dostať aj s \[\]

\[a^2 + b^2 = c^2 \]

Moje obľúbené Grécke písmeno je $\xi$. Tiež sa mi pozdávajú $\beta$, $\gamma$ a $\sigma$.
Ešte som neprišiel na Grécke písmeno, ktoré by LaTeX nepoznal!

Operátory sú dôležitou súčasťou matematických dokumentov: 
goniometrické funkcie ($\sin$, $\cos$, $\tan$), 
logaritmy and exponenciálne výrazy ($\log$, $\exp$), 
limity ($\lim$), atď. 
majú pred-definované LaTeXové príkazy. 
Napíšme si rovnicu, nech vidíme, ako to funguje: \\

$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$

Zlomky(Čitateľ-menovateľ) sa píšu v týchto formách:

% 10 / 7
$^{10}/_{7}$ 

% Relatívne komplexné zlomky sa píšu ako
% \frac{čitateľ}{menovateľ}
$\frac{n!}{k!(n - k)!}$ \\

Rovnice tiež môžeme zadať v "rovnicovom prostredí". 

% Takto funguje rovnicové prostredie
\begin{equation} % vstúpi do matematického módu
    c^2 = a^2 + b^2.
    \label{eq:pythagoras} % na odkazovanie
\end{equation} % všetky \begin príkazy musia mať konečný príkaz.

Teraz môžeme odkázať na novovytvorenú rovnicu! 
Rovn.~\ref{eq:pythagoras} je tiež známa ako Pytagorova Veta, ktorá je tiež predmetom Sekc.~\ref{subsec:pytagoras}. Odkazovať môžme na veľa vecí, napr na: čísla, rovnice, či sekcie.

Sumácie a Integrály sa píšu príkazmi sum a int:

% Niektoré prekladače LaTeXu sa môžu sťažovať na prázdne riadky (ak tam sú)
% v rovnicovom prostredí.
\begin{equation} 
  \sum_{i=0}^{5} f_{i}
\end{equation} 
\begin{equation} 
  \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x
\end{equation} 

\section{Obrázky}

Vloženie obrázku môže byť zložitejšie. Ja si vždy možnosti vloženia pozerám pre každý obrázok.
\renewcommand\figurename{Obrázok}
\begin{figure}[H] % H značí možnosť zarovnania. 
    \centering % nacentruje obrázok na stránku
    % Vloží obrázok na 80% šírky stránky.
    %\includegraphics[width=0.8\linewidth]{right-triangle.png} 
    % Zakomentované kvôli kompilácií, použi svoju predstavivosť :).
    \caption{Pravouhlý trojuholník so stranami $a$, $b$, $c$}
    \label{fig:right-triangle}
\end{figure}
% Opäť, fixné makro "table" nahradíme slovenskou tabuľkou. Pokiaľ preferuješ table, nasledujúci riadok nepridávaj
\renewcommand\tablename{Tabuľka}

\subsection{Tabuľky}
Tabuľky sa vkládajú podobne ako obrázky.

\begin{table}[H]
  \caption{Nadpis tabuľky.}
  % zátvorky: {} hovoria ako sa vykreslí každý riadok.
  % Toto si nikdy nepamätám a vždy to musím hľadať. Všetko. Každý. Jeden. Raz!
  \begin{tabular}{c|cc} 
    Číslo & Priezvisko & Krstné meno \\ % Stĺpce sú rozdelené $
    \hline % horizontálna čiara
    1 & Ladislav & Meliško \\
    2 & Eva & Máziková
  \end{tabular}
\end{table}

% \section{Hyperlinks} % Už čoskoro :)

\section{Prikáž LaTeXu nekompilovať (napr. Zdrojový Kód)}
Povedzme, že chceme do dokumentu vložiť zdrojový kód, budeme musieť LaTeXu povedať, nech sa ho nesnaží skompilovať, ale nech s ním pracuje ako s textom.
Toto sa robí vo verbatim prostredí. 

% Tiež sú na to špeciálne balíčky, (napr. minty, lstlisting, atď.)
% ale verbatim je to najzákladnejšie, čo môžeš použiť.
\begin{verbatim} 
  print("Hello World!")
  a%b; pozri! Vo verbatime môžme použiť % znamienka. 
  random = 4; #priradené randomným hodom kockou
\end{verbatim}

\section{Kompilácia} 

Už by bolo načase túto nádheru skompilovať a zhliadnuť jej úžasnú úžasnosť v podobe LaTeX pdfka, čo povieš?
(áno, tento dokument sa musí kompilovať). \\
Cesta k finálnemu dokumentu pomocou LaTeXu pozostáva z nasledujúcich krokov:
  \begin{enumerate}
    \item Napíš dokument v čistom texte (v "zdrojáku").
    \item Skompiluj zdroják na získanie pdfka. 
     Kompilácia by mala vyzerať nasledovne (v Linuxe): \\
     \begin{verbatim} 
        $pdflatex learn-latex.tex learn-latex.pdf 
     \end{verbatim}
  \end{enumerate}

Mnoho LaTeX editorov kombinuje Krok 1 a Krok 2 v jednom prostredí. Krok 1 teda uvidíš, krok 2 už nie.
Ten sa deje za oponou. Kompilácia v 2. kroku sa postará o produkciu dokumentu v tebou zadanom formáte.

\section{Koniec}

To je zatiaľ všetko!

% koniec dokumentu
\end{document}
```

## Viac o LaTeXe (anglicky)

* Úžasná LaTeX wikikniha: [https://en.wikibooks.org/wiki/LaTeX](https://en.wikibooks.org/wiki/LaTeX)
* Naozajstný tutoriál: [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/)
---
language: ruby
filename: learnruby-sk.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
translators:
  - ["Juraj Kostolanský", "http://www.kostolansky.sk"]
lang: sk-sk
---

```ruby
# Toto je komentár

=begin
Toto je viacriadkový komentár
Nikto ho nepoužíva
Ani ty by si nemal
=end

# V prvom rade: Všetko je objekt.

# Čísla sú objekty

3.class #=> Fixnum

3.to_s #=> "3"


# Základná aritmetika
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32

# Aritmetika je iba syntaktickým cukrom
# pre volanie metódy nad objektom
1.+(3) #=> 4
10.* 5 #=> 50

# Špeciálne hodnoty sú objektami
nil # nič
true # pravda
false # lož

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Rovnosť
1 == 1 #=> true
2 == 1 #=> false

# Nerovnosť
1 != 1 #=> false
2 != 1 #=> true

# Okrem samotného false, nil je jedinou ďalšou 'nepravdivou' hodnotou

!nil   #=> true
!false #=> true
!0     #=> false

# Ďalšie porovnania
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Logické operácie
true && false #=> false
true || false #=> true
!true #=> false

# Existujú aj alternatívne verzie logických operátorov s nižšou prioritou.
# Tie sa využívajú ako konštrukcie pre riadenie toku na reťazenie výrazov
# kým jeden z nich nevráti true alebo false.

# `sprav_nieco_ine` sa vykoná ak bude `sprav_nieco` úspešné (vráti true)
sprav_nieco() and sprav_nieco_ine()
# `zaznamenaj_chybu` sa vykoná ak `sprav_nieco` neuspeje (vráti false)
sprav_nieco() or zaznamenaj_chybu()


# Reťazce sú objekty

'Ja som reťazec'.class #=> String
"Ja som tiež reťazec".class #=> String

retazec = 'použiť interpoláciu reťazca'
"Môžem #{retazec} pri použití dvojitých úvodzoviek"
#=> "Môžem použiť interpoláciu reťazca pri použití dvojitých úvodzoviek"

# Preferuj jednoduché úvodzovky pred dvojitými, ak je to možné
# Dvojité úvodzovky totiž vyžadujú ďalšie výpočty

# Kombinuj reťazce, ale nie s číslami
'ahoj ' + 'svet'  #=> "ahoj svet"
'ahoj ' + 3 #=> TypeError: can't convert Fixnum into String
'ahoj ' + 3.to_s #=> "ahoj 3"

# Výpis na štandardný výstup
puts "Píšem!"


# Premenné
x = 25 #=> 25
x #=> 25

# Všimni si, že priradenie vracia priradenú hodnotu
# To umožňuje viacnásobné priradenie:

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Podľa konvencie sa pre mená premenných využíva snake_case
snake_case = true

# Používaj správne (opisné) mená premenných
cesta_ku_korenovemu_adresaru = '/dobre/meno/'
cesta = '/zle/meno/'


# Symboly (sú objektami)
# Symboly sú nemenné znovupoužiteľné konštanty, ktoré sú interne
# reprezentované ako číslo. Často sa používajú namiesto reťazcov
# pre efektívnu reprezentáciu špecifickej hodnoty.

:cakajuci.class #=> Symbol

status = :cakajuci

status == :cakajuci #=> true

status == 'cakajuci' #=> false

status == :schvaleny #=> false


# Polia

# Toto je pole
pole = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Polia môžu obsahovať rôzne typy objektov

[1, 'ahoj', false] #=> [1, "ahoj", false]

# Polia môžu byť indexované
# Od začiatku
pole[0] #=> 1
pole[12] #=> nil

# Podobne ako pri aritmetike, prístup prostredníctvom [var]
# je iba syntaktickým cukrom pre volanie metódy [] nad objektom
pole.[] 0 #=> 1
pole.[] 12 #=> nil

# Od konca
pole[-1] #=> 5

# Pomocou počiatočného indexu a dĺžky
pole[2, 3] #=> [3, 4, 5]

# Alebo rozsahom
pole[1..3] #=> [2, 3, 4]

# Pridanie prvku do pola
pole << 6 #=> [1, 2, 3, 4, 5, 6]
# Alebo takto
pole.push(6) #=> [1, 2, 3, 4, 5, 6]

# Skontroluje, či už je objekt v poli
pole.include?(1) #=> true


# Asociatívne polia (hash) sú slovníkmi s dvojicami kľúč-hodnota.
# Asociatívne polia sú označované kučeravými zátvorkami:
asoc_pole = { 'farba' => 'zelena', 'cislo' => 5 }

asoc_pole.keys #=> ['farba', 'cislo']

# V asociatívnych poliach sa rýchlo vyhľadáva pomocou kľúča
asoc_pole['farba'] #=> 'zelena'
asoc_pole['cislo'] #=> 5

# Asking a hash for a key that doesn't exist returns nil:
asoc_pole['nic tu nie je'] #=> nil

# Od verzie Ruby 1.9 existuje špeciálna syntax,
# pri ktorej sa využíva symbol ako kľúč

nove_asoc_pole = { defcon: 3, akcia: true }
nove_asoc_pole.keys #=> [:defcon, :akcia]

# Skontroluje existenciu kľúča a hodnoty v asociatívnom poli
nove_asoc_pole.has_key?(:defcon) #=> true
nove_asoc_pole.has_value?(3) #=> true

# Tip: Polia aj asociatívne polia sú vypočítateľné (Enumerable)
# Zdieľajú veľa užitočných metód ako each, map, count a ďalšie


# Štruktúry riadenia

if true
  'if podmienka'
elsif false
  'else if, voliteľná vetva'
else
  'else, tiež voliteľná vetva'
end

for pocitadlo in 1..5
  puts "iterácia #{pocitadlo}"
end
#=> iterácia 1
#=> iterácia 2
#=> iterácia 3
#=> iterácia 4
#=> iterácia 5

# NIKTO však nepoužíva for cyklus
# Aj ty by si mal preferovať metódu "each" a podať jej blok
# Blok kus kódu, ktorý môžeš podať metódam ako "each"
# Je podobný lambdám alebo anonymným funkciám v iných jazykoch
#
# Metóda "each" pre rozsah spustí blokpre každý element tohto rozsahu
# Blok získava počítadlo ako parameter
# Volanie metódy "each" s blokomvyzerá takto:

(1..5).each do |pocitadlo|
  puts "iterácia #{pocitadlo}"
end
#=> iterácia 1
#=> iterácia 2
#=> iterácia 3
#=> iterácia 4
#=> iterácia 5

# Blok môže byť uzavretý aj v kučeravých záítvorkách:
(1..5).each { |pocitadlo| puts "iterácia #{pocitadlo}" }

# Obsah dátových štruktúr môže byť tiež prechádzaný pomocou metódy "each"
pole.each do |prvok|
  puts "#{prvok} je súčasťou pola"
end
asoc_pole.each do |kluc, hodnota|
  puts "#{kluc} je #{hodnota}"
end

pocitadlo = 1
while pocitadlo <= 5 do
  puts "iterácia #{pocitadlo}"
  pocitadlo += 1
end
#=> iterácia 1
#=> iterácia 2
#=> iterácia 3
#=> iterácia 4
#=> iterácia 5

znamka = 'B'

case znamka
when 'A'
  puts 'Len tak ďalej, chlapče'
when 'B'
  puts 'Viac šťastia nabudúce'
when 'C'
  puts 'Zvládneš to aj lepšie'
when 'D'
  puts 'S odratými ušami'
when 'F'
  puts 'Zlyhal si!'
else
  puts 'Iný systém známkovania, čo?'
end
#=> "Viac šťastia nabudúce"

# prípady (cases) môžu tiež využívať rozsahy
znamka = 82
case znamka
when 90..100
  puts 'Hurá!'
when 80...90
  puts 'Dobrá práca'
else
  puts 'Zlyhal si!'
end
#=> "Dobrá práca"

# Zaobchádzanie s výnimkami
begin
  # kód, ktorý môže vyhodiť výnimku
  raise NoMemoryError, 'Došla ti pamäť.'
rescue NoMemoryError => premenna_vynimky
  puts 'Nastala vynimka NoMemoryError', premenna_vynimky
rescue RuntimeError => ina_premenna_vynimky
  puts 'Nastala vynimka RuntimeError'
else
  puts 'Toto sa spustí, ak nenastala žiadna výnimka'
ensure
  puts 'Táto časť kódu sa spustí vždy'
end

# Funkcie

def zdvojnasob(x)
  x * 2
end

# Funkcie (a bloky) implicitne vracajú hodnotu posledného výrazu
zdvojnasob(2) #=> 4

# Zátvorky sú voliteľné ak je výsledok jednoznačný
zdvojnasob 3 #=> 6

zdvojnasob zdvojnasob 3 #=> 12

def suma(x, y)
  x + y
end

# Argumenty metódy sa oddeľujú čiarkami
suma 3, 4 #=> 7

suma suma(3, 4), 5 #=> 12

# yield
# Všetky metódy majú implicitný, voliteľný parameter bloku
# môže byť zavolaný ako kľúčové slovo 'yield'

def obal
  puts '{'
  yield
  puts '}'
end

obal { puts 'ahoj svet' }

# {
# ahoj svet
# }


# Funkcii môžeš odovzdať blok
# "&" označuje referenciu na tento blok
def hostia(&blok)
  blok.call 'nejake argumenty'
end

# Tiež môžeš odovzdať zoznam argumentov, ktoré sa prevedú na pole
# Na to sa využíva operátor "*"
def hostia(*pole)
  pole.each { |host| puts host }
end


# Trieda sa definuje kľúčovým slovom class
class Clovek

  # Premenná triedy. Je zdieľaná všetkými inštanciami tejto triedy.
  @@druh = 'H. sapiens'

  # Jednoduchý inicializátor
  def initialize(meno, vek = 0)
    # Priradí argument k premennej inštancie "meno"
    @meno = meno
    # Ak nie je uvedený vek, použije sa špecifikovaná predvolená hodnota
    @vek = vek
  end

  # Jednoduchá metóda pre nastavenie hodnoty premennej
  def meno=(meno)
    @meno = meno
  end

  # Jednoduchá metóda pre získanie hodnoty premennej
  def meno
    @meno
  end

  # Vyššie uvedená funkcionalita môže byť zapúzdrená použitím
  # metódy attr_accessor
  attr_accessor :meno

  # Metódy pre nastavenie a získanie hodnoty premennej môžu byť vytvorené
  # aj individuálne
  attr_reader :meno
  attr_writer :meno

  # Metóda triedy používa kľúčové slovo self pre odlíšenie
  # od metód inštancií. Môže byť volaná iba nad triedou, nie inštanciou.
  def self.povedz(sprava)
    puts sprava
  end

  def druh
    @@druh
  end
end


# Vytvorenie inštancie triedy
jim = Clovek.new('Jim Halpert')

dwight = Clovek.new('Dwight K. Schrute')

# Skúsme zavolať zopár metód
jim.druh #=> "H. sapiens"
jim.meno #=> "Jim Halpert"
jim.meno = "Jim Halpert II" #=> "Jim Halpert II"
jim.meno #=> "Jim Halpert II"
dwight.druh #=> "H. sapiens"
dwight.meno #=> "Dwight K. Schrute"

# Volanie metódy triedy
Clovek.povedz('Ahoj') #=> "Ahoj"

# Rozsah platnosti premennej je definovaná spôsobom, akým ju nazveme.
# Premenné začínajúce znakom $ majú globálnu platnosť.
$premenna = "Ja som globálna premenná"
defined? $premenna #=> "global-variable"

# Premenné začínajúce znakom @ majú platnosť v rámci inštancie
@premenna = "Ja som premenná inštancie"
defined? @premenna #=> "instance-variable"

# Premenné začínajúce znakmi @@ majú platnosť v rámci triedy
@@premenna= "Ja som premenná triedy"
defined? @@premenna #=> "class variable"

# Premenné začínajúce veľkým písmenom sú konštanty
Premenna = "Ja som konštanta"
defined? Premenna #=> "constant"

# Trieda je tiež objektom v ruby, takže aj ona môže mať premenné inštancie.
# Premenná triedy je zdieľaná triedou a jej nasledovníkmi.

# Základná trieda
class Clovek
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(hodnota)
    @@foo = hodnota
  end
end

# Odvodená trieda
class Pracovnik < Clovek
end

Clovek.foo # 0
Pracovnik.foo # 0

Clovek.foo = 2 # 2
Pracovnik.foo # 2

# Premenné inštancie triedy nie sú zdieľané jej nasledovníkmi.

class Clovek
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(hodnota)
    @bar = hodnota
  end
end

class Doktor < Clovek
end

Clovek.bar # 0
Doktor.bar # nil

module PrikladModulu
  def foo
    'foo'
  end
end

# Vloženie modulu (include) previaže jeho metódy s inštanciou triedy.
# Rozšírenie modulu (extend) previaže jeho metódy so samotnou triedou.

class Osoba
  include PrikladModulu
end

class Kniha
  extend PrikladModulu
end

Osoba.foo     # => NoMethodError: undefined method `foo' for Osoba:Class
Osoba.new.foo # => 'foo'
Kniha.foo       # => 'foo'
Kniha.new.foo   # => NoMethodError: undefined method `foo'

# Spätné volania sú vykonané pri vložení alebo rozšírení modulu

module PrikladKoncernu
  def self.included(zaklad)
    zaklad.extend(MetodyTriedy)
    zaklad.send(:include, MetodyInstancie)
  end

  module MetodyTriedy
    def bar
      'bar'
    end
  end

  module MetodyInstancie
    def qux
      'qux'
    end
  end
end

class Nieco
  include PrikladKoncernu
end

Nieco.bar     # => 'bar'
Nieco.qux     # => NoMethodError: undefined method `qux'
Nieco.new.bar # => NoMethodError: undefined method `bar'
Nieco.new.qux # => 'qux'
```

## Ďalšie zdroje

- [Nauč sa ruby v príkladoch s úlohami](http://www.learneroo.com/modules/61/nodes/338) - Variácia tejto referencie s úlohami v prehliadači.
- [Oficiálna dokumentácia](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby z iných jazykov](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - Staršia [bezplatná edícia](http://ruby-doc.com/docs/ProgrammingRuby/) je dostupná online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - Komunitná príručka odporúčaných štýlov programovania v Ruby.
---
language: SmallBASIC
filename: learnsmallbasic.bas
contributors:
    - ["Chris Warren-Smith", "http://smallbasic.sourceforge.net"]
---

## About

SmallBASIC is a fast and easy to learn BASIC language interpreter ideal for everyday calculations, scripts and prototypes. SmallBASIC includes trigonometric, matrices and algebra functions, a built in IDE, a powerful string library, system, sound, and graphic commands along with structured programming syntax.

## Development

SmallBASIC was originally developed by Nicholas Christopoulos in late 1999 for the Palm Pilot. Project development has been continued by Chris Warren-Smith since around 2005.

Versions of SmallBASIC have been made for a number of early hand held devices including Franklin eBookman and the Nokia 770. Also various desktop versions have been released based on a variety of GUI tool-kits, some of which have become defunct. The current supported platforms are Linux and Windows based on SDL2 and Android based on NDK. A desktop command line version is also available, although not typically released in binary form.

In around 2008 a large corporation released a BASIC like programming environment with a similar sounding name. SmallBASIC is not related to this other project.

```
REM This is a comment
' and this is also a comment

REM print text
print "hello"
? "? is short for PRINT"

REM Control structures
FOR index = 0 TO 10 STEP 2
  ? "This is line number "; index
NEXT
J=0
REPEAT
 J++
UNTIL J=10
WHILE J>0
 J--
WEND

REM Select case statement
Select Case "Cool"
 Case "null", 1,2,3,4,5,6,7,8,"Cool","blah"
 Case "Not cool"
   PRINT "Epic fail"
 Case Else
   PRINT "Fail"
End Select

REM catching errors with TRY/CATCH
Try
  fn = Freefile
  Open filename For Input As #fn
Catch err
  Print "failed to open"
End Try

REM User defined subs and functions
func add2(x,y)
  ' variables may be declared as local within the scope of a SUB or FUNC
  local K
  k = "k will cease to exist when this FUNC returns"
  add2=x+y
end
Print add2(5,5)
sub print_it(it)
  print it
end
print_it "IT...."

REM Display lines and pixels
At 0,ymax/2+txth("Q")
Color 1: ? "sin(x)":
Color 8: ? "cos(x)":
Color 12: ? "tan(x)"
Line 0,ymax/2,xmax,ymax/2
For i=0 to xmax
  Pset i,ymax/2-sin(i*2*pi/ymax)*ymax/4 color 1
  Pset i,ymax/2-cos(i*2*pi/ymax)*ymax/4 color 8
  Pset i,ymax/2-tan(i*2*pi/ymax)*ymax/4 color 12
Next
showpage

REM SmallBASIC is great for experimenting with fractals and other interesting effects
Delay 3000
Randomize
ff = 440.03
For j = 0 to 20
  r = rnd * 1000 % 255
  b = rnd * 1000 % 255
  g = rnd * 1000 % 255
  c = rgb(r,b,g)
  ff += 9.444
  for i=0 to 25000
    f += ff
    x = min(xmax, -x + cos(f*i))
    y = min(ymax, -y + sin(f*i))
    pset x, y color c
    if (i%1000==0) then
      showpage
    fi
  next
Next j

REM For computer historians, SmallBASIC can run programs
REM found in early computer books and magazines, for example:
10 LET A=9
20 LET B=7
30 PRINT A*B
40 PRINT A/B

REM SmallBASIC also has support for a few modern concepts such as JSON
aa = array("{\"cat\":{\"name\":\"harry\"},\"pet\":\"true\"}")
If (ismap(aa) == false) Then
  throw "not an map"
End If
Print aa

PAUSE

```

## Articles

* [Getting started](http://smallbasic.sourceforge.net/?q=node/1573)
* [Welcome to SmallBASIC](http://smallbasic.sourceforge.net/?q=node/838)

## GitHub

* [Source code](https://github.com/smallbasic/SmallBASIC)
* [Reference snapshot](http://smallbasic.github.io/)

---
language: smalltalk
filename: smalltalk.st
contributors:
    - ["Jigyasa Grover", "https://github.com/jig08"]
---

- Smalltalk is an object-oriented, dynamically typed, reflective programming language.
- Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis."
- It was designed and created in part for educational use, more so for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and others during the 1970s.

Feedback highly appreciated! Reach me at [@jigyasa_grover](https://twitter.com/jigyasa_grover) or send me an e-mail at `grover.jigyasa1@gmail.com`.


##Allowable characters:
- a-z
- A-Z
- 0-9
- .+/\*~<>@%|&?
- blank, tab, cr, ff, lf

##Variables:
- variables must be declared before use
- shared vars must begin with uppercase
- local vars must begin with lowercase
- reserved names: `nil`, `true`, `false`, `self`, `super`, and `Smalltalk`

##Variable scope:
- Global: defined in Dictionary Smalltalk and accessible by all objects in system                                 - Special: (reserved) `Smalltalk`, `super`, `self`, `true`, `false`, & `nil`
- Method Temporary: local to a method
- Block Temporary: local to a block
- Pool: variables in a Dictionary object
- Method Parameters: automatic local vars created as a result of message call with params
- Block Parameters: automatic local vars created as a result of value: message call
- Class: shared with all instances of one class & its subclasses
- Class Instance: unique to each instance of a class
- Instance Variables: unique to each instance

`"Comments are enclosed in quotes"`

`"Period (.) is the statement separator"`

## Transcript:
```
Transcript clear.                                           "clear to transcript window"
Transcript show: 'Hello World'.                             "output string in transcript window"
Transcript nextPutAll: 'Hello World'.                       "output string in transcript window"
Transcript nextPut: $A.                                     "output character in transcript window"
Transcript space.                                           "output space character in transcript window"
Transcript tab.                                             "output tab character in transcript window"
Transcript cr.                                              "carriage return / linefeed"
'Hello' printOn: Transcript.                                "append print string into the window"
'Hello' storeOn: Transcript.                                "append store string into the window"
Transcript endEntry.                                        "flush the output buffer"
```

##Assignment:
```
| x y |
x _ 4.                                                      "assignment (Squeak) <-"
x := 5.                                                     "assignment"
x := y := z := 6.                                           "compound assignment"
x := (y := 6) + 1.
x := Object new.                                            "bind to allocated instance of a class"
x := 123 class.                                             "discover the object class"
x := Integer superclass.                                    "discover the superclass of a class"
x := Object allInstances.                                   "get an array of all instances of a class"
x := Integer allSuperclasses.                               "get all superclasses of a class"
x := 1.2 hash.                                              "hash value for object"
y := x copy.                                                "copy object"
y := x shallowCopy.                                         "copy object (not overridden)"
y := x deepCopy.                                            "copy object and instance vars"
y := x veryDeepCopy.                                        "complete tree copy using a dictionary"
```

##Constants:
```
| b |
b := true.                                                  "true constant"
b := false.                                                 "false constant"
x := nil.                                                   "nil object constant"
x := 1.                                                     "integer constants"
x := 3.14.                                                  "float constants"
x := 2e-2.                                                  "fractional constants"
x := 16r0F.                                                 "hex constant".
x := -1.                                                    "negative constants"
x := 'Hello'.                                               "string constant"
x := 'I''m here'.                                           "single quote escape"
x := $A.                                                    "character constant"
x := $ .                                                    "character constant (space)"
x := #aSymbol.                                              "symbol constants"
x := #(3 2 1).                                              "array constants"
x := #('abc' 2 $a).                                         "mixing of types allowed"

```

## Booleans:
```
| b x y |
x := 1. y := 2.
b := (x = y).                                               "equals"
b := (x ~= y).                                              "not equals"
b := (x == y).                                              "identical"
b := (x ~~ y).                                              "not identical"
b := (x > y).                                               "greater than"
b := (x < y).                                               "less than"
b := (x >= y).                                              "greater than or equal"
b := (x <= y).                                              "less than or equal"
b := b not.                                                 "boolean not"
b := (x < 5) & (y > 1).                                     "boolean and"
b := (x < 5) | (y > 1).                                     "boolean or"
b := (x < 5) and: [y > 1].                                  "boolean and (short-circuit)"
b := (x < 5) or: [y > 1].                                   "boolean or (short-circuit)"
b := (x < 5) eqv: (y > 1).                                  "test if both true or both false"
b := (x < 5) xor: (y > 1).                                  "test if one true and other false"
b := 5 between: 3 and: 12.                                  "between (inclusive)"
b := 123 isKindOf: Number.                                  "test if object is class or subclass of"
b := 123 isMemberOf: SmallInteger.                          "test if object is type of class"
b := 123 respondsTo: sqrt.                                  "test if object responds to message"
b := x isNil.                                               "test if object is nil"
b := x isZero.                                              "test if number is zero"
b := x positive.                                            "test if number is positive"
b := x strictlyPositive.                                    "test if number is greater than zero"
b := x negative.                                            "test if number is negative"
b := x even.                                                "test if number is even"
b := x odd.                                                 "test if number is odd"
b := x isLiteral.                                           "test if literal constant"
b := x isInteger.                                           "test if object is integer"
b := x isFloat.                                             "test if object is float"
b := x isNumber.                                            "test if object is number"
b := $A isUppercase.                                        "test if upper case character"
b := $A isLowercase.                                        "test if lower case character"

```

## Arithmetic expressions:
```
| x |
x := 6 + 3.                                                 "addition"
x := 6 - 3.                                                 "subtraction"
x := 6 * 3.                                                 "multiplication"
x := 1 + 2 * 3.                                             "evaluation always left to right (1 + 2) * 3"
x := 5 / 3.                                                 "division with fractional result"
x := 5.0 / 3.0.                                             "division with float result"
x := 5.0 // 3.0.                                            "integer divide"
x := 5.0 \\ 3.0.                                            "integer remainder"
x := -5.                                                    "unary minus"
x := 5 sign.                                                "numeric sign (1, -1 or 0)"
x := 5 negated.                                             "negate receiver"
x := 1.2 integerPart.                                       "integer part of number (1.0)"
x := 1.2 fractionPart.                                      "fractional part of number (0.2)"
x := 5 reciprocal.                                          "reciprocal function"
x := 6 * 3.1.                                               "auto convert to float"
x := 5 squared.                                             "square function"
x := 25 sqrt.                                               "square root"
x := 5 raisedTo: 2.                                         "power function"
x := 5 raisedToInteger: 2.                                  "power function with integer"
x := 5 exp.                                                 "exponential"
x := -5 abs.                                                "absolute value"
x := 3.99 rounded.                                          "round"
x := 3.99 truncated.                                        "truncate"
x := 3.99 roundTo: 1.                                       "round to specified decimal places"
x := 3.99 truncateTo: 1.                                    "truncate to specified decimal places"
x := 3.99 floor.                                            "truncate"
x := 3.99 ceiling.                                          "round up"
x := 5 factorial.                                           "factorial"
x := -5 quo: 3.                                             "integer divide rounded toward zero"
x := -5 rem: 3.                                             "integer remainder rounded toward zero"
x := 28 gcd: 12.                                            "greatest common denominator"
x := 28 lcm: 12.                                            "least common multiple"
x := 100 ln.                                                "natural logarithm"
x := 100 log.                                               "base 10 logarithm"
x := 100 log: 10.                                           "logarithm with specified base"
x := 100 floorLog: 10.                                      "floor of the log"
x := 180 degreesToRadians.                                  "convert degrees to radians"
x := 3.14 radiansToDegrees.                                 "convert radians to degrees"
x := 0.7 sin.                                               "sine"
x := 0.7 cos.                                               "cosine"
x := 0.7 tan.                                               "tangent"
x := 0.7 arcSin.                                            "arcsine"
x := 0.7 arcCos.                                            "arccosine"
x := 0.7 arcTan.                                            "arctangent"
x := 10 max: 20.                                            "get maximum of two numbers"
x := 10 min: 20.                                            "get minimum of two numbers"
x := Float pi.                                              "pi"
x := Float e.                                               "exp constant"
x := Float infinity.                                        "infinity"
x := Float nan.                                             "not-a-number"
x := Random new next; yourself. x next.                     "random number stream (0.0 to 1.0)
x := 100 atRandom.                                          "quick random number"

```

##Bitwise Manipulation:
```
| b x |
x := 16rFF bitAnd: 16r0F.                                   "and bits"
x := 16rF0 bitOr: 16r0F.                                    "or bits"
x := 16rFF bitXor: 16r0F.                                   "xor bits"
x := 16rFF bitInvert.                                       "invert bits"
x := 16r0F bitShift: 4.                                     "left shift"
x := 16rF0 bitShift: -4.                                    "right shift"
"x := 16r80 bitAt: 7."                                      "bit at position (0|1) [!Squeak]"
x := 16r80 highbit.                                         "position of highest bit set"
b := 16rFF allMask: 16r0F.                                  "test if all bits set in mask set in receiver"
b := 16rFF anyMask: 16r0F.                                  "test if any bits set in mask set in receiver"
b := 16rFF noMask: 16r0F.                                   "test if all bits set in mask clear in receiver"

```

## Conversion:
```
| x |
x := 3.99 asInteger.                                        "convert number to integer (truncates in Squeak)"
x := 3.99 asFraction.                                       "convert number to fraction"
x := 3 asFloat.                                             "convert number to float"
x := 65 asCharacter.                                        "convert integer to character"
x := $A asciiValue.                                         "convert character to integer"
x := 3.99 printString.                                      "convert object to string via printOn:"
x := 3.99 storeString.                                      "convert object to string via storeOn:"
x := 15 radix: 16.                                          "convert to string in given base"
x := 15 printStringBase: 16.
x := 15 storeStringBase: 16.

```

## Blocks:
- blocks are objects and may be assigned to a variable
- value is last expression evaluated unless explicit return
- blocks may be nested
- specification [ arguments | | localvars | expressions ]
- Squeak does not currently support localvars in blocks
- max of three arguments allowed
- `^`expression terminates block & method (exits all nested blocks)
- blocks intended for long term storage should not contain `^`

```
| x y z |
x := [ y := 1. z := 2. ]. x value.                          "simple block usage"
x := [ :argOne :argTwo |   argOne, ' and ' , argTwo.].      "set up block with argument passing"
Transcript show: (x value: 'First' value: 'Second'); cr.    "use block with argument passing"
"x := [ | z | z := 1.]. *** localvars not available in squeak blocks"
```

## Method calls:
- unary methods are messages with no arguments
- binary methods
- keyword methods are messages with selectors including colons standard categories/protocols:                     - initialize-release    (methods called for new instance)
- accessing             (get/set methods)
- testing               (boolean tests - is)
- comparing             (boolean tests with parameter
- displaying            (gui related methods)
- printing              (methods for printing)
- updating              (receive notification of changes)
- private               (methods private to class)
- instance-creation     (class methods for creating instance)
```
| x |
x := 2 sqrt.                                                "unary message"
x := 2 raisedTo: 10.                                        "keyword message"
x := 194 * 9.                                               "binary message"
Transcript show: (194 * 9) printString; cr.                 "combination (chaining)"
x := 2 perform: #sqrt.                                      "indirect method invocation"
Transcript                                                  "Cascading - send multiple messages to receiver"
   show: 'hello ';
   show: 'world';
   cr.
x := 3 + 2; * 100.                                          "result=300. Sends message to same receiver (3)"
```

##Conditional Statements:
```
| x |
x > 10 ifTrue: [Transcript show: 'ifTrue'; cr].             "if then"
x > 10 ifFalse: [Transcript show: 'ifFalse'; cr].           "if else"
x > 10                                                      "if then else"
   ifTrue: [Transcript show: 'ifTrue'; cr]
   ifFalse: [Transcript show: 'ifFalse'; cr].
x > 10                                                      "if else then"
   ifFalse: [Transcript show: 'ifFalse'; cr]
   ifTrue: [Transcript show: 'ifTrue'; cr].
Transcript
   show:
      (x > 10
         ifTrue: ['ifTrue']
         ifFalse: ['ifFalse']);
   cr.
Transcript                                                  "nested if then else"
   show:
      (x > 10
         ifTrue: [x > 5
            ifTrue: ['A']
            ifFalse: ['B']]
         ifFalse: ['C']);
   cr.
switch := Dictionary new.                                   "switch functionality"
switch at: $A put: [Transcript show: 'Case A'; cr].
switch at: $B put: [Transcript show: 'Case B'; cr].
switch at: $C put: [Transcript show: 'Case C'; cr].
result := (switch at: $B) value.
```

## Iteration statements:
```
| x y |
x := 4. y := 1.
[x > 0] whileTrue: [x := x - 1. y := y * 2].                "while true loop"
[x >= 4] whileFalse: [x := x + 1. y := y * 2].              "while false loop"
x timesRepeat: [y := y * 2].                                "times repeat loop (i := 1 to x)"
1 to: x do: [:a | y := y * 2].                              "for loop"
1 to: x by: 2 do: [:a | y := y / 2].                        "for loop with specified increment"
#(5 4 3) do: [:a | x := x + a].                             "iterate over array elements"
```

## Character:
```
| x y |
x := $A.                                                    "character assignment"
y := x isLowercase.                                         "test if lower case"
y := x isUppercase.                                         "test if upper case"
y := x isLetter.                                            "test if letter"
y := x isDigit.                                             "test if digit"
y := x isAlphaNumeric.                                      "test if alphanumeric"
y := x isSeparator.                                         "test if separator char"
y := x isVowel.                                             "test if vowel"
y := x digitValue.                                          "convert to numeric digit value"
y := x asLowercase.                                         "convert to lower case"
y := x asUppercase.                                         "convert to upper case"
y := x asciiValue.                                          "convert to numeric ascii value"
y := x asString.                                            "convert to string"
b := $A <= $B.                                              "comparison"
y := $A max: $B.

```

## Symbol:
```
| b x y |
x := #Hello.                                                "symbol assignment"
y := 'String', 'Concatenation'.                             "symbol concatenation (result is string)"
b := x isEmpty.                                             "test if symbol is empty"
y := x size.                                                "string size"
y := x at: 2.                                               "char at location"
y := x copyFrom: 2 to: 4.                                   "substring"
y := x indexOf: $e ifAbsent: [0].                           "first position of character within string"
x do: [:a | Transcript show: a printString; cr].            "iterate over the string"
b := x conform: [:a | (a >= $a) & (a <= $z)].               "test if all elements meet condition"
y := x select: [:a | a > $a].                               "return all elements that meet condition"
y := x asString.                                            "convert symbol to string"
y := x asText.                                              "convert symbol to text"
y := x asArray.                                             "convert symbol to array"
y := x asOrderedCollection.                                 "convert symbol to ordered collection"
y := x asSortedCollection.                                  "convert symbol to sorted collection"
y := x asBag.                                               "convert symbol to bag collection"
y := x asSet.                                               "convert symbol to set collection"
```

## String:
```
| b x y |
x := 'This is a string'.                                    "string assignment"
x := 'String', 'Concatenation'.                             "string concatenation"
b := x isEmpty.                                             "test if string is empty"
y := x size.                                                "string size"
y := x at: 2.                                               "char at location"
y := x copyFrom: 2 to: 4.                                   "substring"
y := x indexOf: $a ifAbsent: [0].                           "first position of character within string"
x := String new: 4.                                         "allocate string object"
x                                                           "set string elements"
   at: 1 put: $a;
   at: 2 put: $b;
   at: 3 put: $c;
   at: 4 put: $e.
x := String with: $a with: $b with: $c with: $d.            "set up to 4 elements at a time"
x do: [:a | Transcript show: a printString; cr].            "iterate over the string"
b := x conform: [:a | (a >= $a) & (a <= $z)].               "test if all elements meet condition"
y := x select: [:a | a > $a].                               "return all elements that meet condition"
y := x asSymbol.                                            "convert string to symbol"
y := x asArray.                                             "convert string to array"
x := 'ABCD' asByteArray.                                    "convert string to byte array"
y := x asOrderedCollection.                                 "convert string to ordered collection"
y := x asSortedCollection.                                  "convert string to sorted collection"
y := x asBag.                                               "convert string to bag collection"
y := x asSet.                                               "convert string to set collection"
y := x shuffled.                                            "randomly shuffle string"
```

## Array:         Fixed length collection
- ByteArray:     Array limited to byte elements (0-255)
- WordArray:     Array limited to word elements (0-2^32)

```
| b x y sum max |
x := #(4 3 2 1).                                            "constant array"
x := Array with: 5 with: 4 with: 3 with: 2.                 "create array with up to 4 elements"
x := Array new: 4.                                          "allocate an array with specified size"
x                                                           "set array elements"
   at: 1 put: 5;
   at: 2 put: 4;
   at: 3 put: 3;
   at: 4 put: 2.
b := x isEmpty.                                             "test if array is empty"
y := x size.                                                "array size"
y := x at: 4.                                               "get array element at index"
b := x includes: 3.                                         "test if element is in array"
y := x copyFrom: 2 to: 4.                                   "subarray"
y := x indexOf: 3 ifAbsent: [0].                            "first position of element within array"
y := x occurrencesOf: 3.                                    "number of times object in collection"
x do: [:a | Transcript show: a printString; cr].            "iterate over the array"
b := x conform: [:a | (a >= 1) & (a <= 4)].                 "test if all elements meet condition"
y := x select: [:a | a > 2].                                "return collection of elements that pass test"
y := x reject: [:a | a < 2].                                "return collection of elements that fail test"
y := x collect: [:a | a + a].                               "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: [].                     "find position of first element that passes test"
sum := 0. x do: [:a | sum := sum + a]. sum.                 "sum array elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum array elements"
sum := x inject: 0 into: [:a :c | a + c].                   "sum array elements"
max := x inject: 0 into: [:a :c | (a > c)                   "find max element in array"
   ifTrue: [a]
   ifFalse: [c]].
y := x shuffled.                                            "randomly shuffle collection"
y := x asArray.                                             "convert to array"
"y := x asByteArray."                                       "note: this instruction not available on Squeak"
y := x asWordArray.                                         "convert to word array"
y := x asOrderedCollection.                                 "convert to ordered collection"
y := x asSortedCollection.                                  "convert to sorted collection"
y := x asBag.                                               "convert to bag collection"
y := x asSet.                                               "convert to set collection"
```

##OrderedCollection: acts like an expandable array
```
| b x y sum max |
x := OrderedCollection with: 4 with: 3 with: 2 with: 1.     "create collection with up to 4 elements"
x := OrderedCollection new.                                 "allocate collection"
x add: 3; add: 2; add: 1; add: 4; yourself.                 "add element to collection"
y := x addFirst: 5.                                         "add element at beginning of collection"
y := x removeFirst.                                         "remove first element in collection"
y := x addLast: 6.                                          "add element at end of collection"
y := x removeLast.                                          "remove last element in collection"
y := x addAll: #(7 8 9).                                    "add multiple elements to collection"
y := x removeAll: #(7 8 9).                                 "remove multiple elements from collection"
x at: 2 put: 3.                                             "set element at index"
y := x remove: 5 ifAbsent: [].                              "remove element from collection"
b := x isEmpty.                                             "test if empty"
y := x size.                                                "number of elements"
y := x at: 2.                                               "retrieve element at index"
y := x first.                                               "retrieve first element in collection"
y := x last.                                                "retrieve last element in collection"
b := x includes: 5.                                         "test if element is in collection"
y := x copyFrom: 2 to: 3.                                   "subcollection"
y := x indexOf: 3 ifAbsent: [0].                            "first position of element within collection"
y := x occurrencesOf: 3.                                    "number of times object in collection"
x do: [:a | Transcript show: a printString; cr].            "iterate over the collection"
b := x conform: [:a | (a >= 1) & (a <= 4)].                 "test if all elements meet condition"
y := x select: [:a | a > 2].                                "return collection of elements that pass test"
y := x reject: [:a | a < 2].                                "return collection of elements that fail test"
y := x collect: [:a | a + a].                               "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: [].                     "find position of first element that passes test"
sum := 0. x do: [:a | sum := sum + a]. sum.                 "sum elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements"
sum := x inject: 0 into: [:a :c | a + c].                   "sum elements"
max := x inject: 0 into: [:a :c | (a > c)                   "find max element in collection"
   ifTrue: [a]
   ifFalse: [c]].
y := x shuffled.                                            "randomly shuffle collection"
y := x asArray.                                             "convert to array"
y := x asOrderedCollection.                                 "convert to ordered collection"
y := x asSortedCollection.                                  "convert to sorted collection"
y := x asBag.                                               "convert to bag collection"
y := x asSet.                                               "convert to set collection"
```

## SortedCollection:    like OrderedCollection except order of elements determined by sorting criteria
```
| b x y sum max |
x := SortedCollection with: 4 with: 3 with: 2 with: 1.      "create collection with up to 4 elements"
x := SortedCollection new.                                  "allocate collection"
x := SortedCollection sortBlock: [:a :c | a > c].           "set sort criteria"
x add: 3; add: 2; add: 1; add: 4; yourself.                 "add element to collection"
y := x addFirst: 5.                                         "add element at beginning of collection"
y := x removeFirst.                                         "remove first element in collection"
y := x addLast: 6.                                          "add element at end of collection"
y := x removeLast.                                          "remove last element in collection"
y := x addAll: #(7 8 9).                                    "add multiple elements to collection"
y := x removeAll: #(7 8 9).                                 "remove multiple elements from collection"
y := x remove: 5 ifAbsent: [].                              "remove element from collection"
b := x isEmpty.                                             "test if empty"
y := x size.                                                "number of elements"
y := x at: 2.                                               "retrieve element at index"
y := x first.                                               "retrieve first element in collection"
y := x last.                                                "retrieve last element in collection"
b := x includes: 4.                                         "test if element is in collection"
y := x copyFrom: 2 to: 3.                                   "subcollection"
y := x indexOf: 3 ifAbsent: [0].                            "first position of element within collection"
y := x occurrencesOf: 3.                                    "number of times object in collection"
x do: [:a | Transcript show: a printString; cr].            "iterate over the collection"
b := x conform: [:a | (a >= 1) & (a <= 4)].                 "test if all elements meet condition"
y := x select: [:a | a > 2].                                "return collection of elements that pass test"
y := x reject: [:a | a < 2].                                "return collection of elements that fail test"
y := x collect: [:a | a + a].                               "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: [].                     "find position of first element that passes test"
sum := 0. x do: [:a | sum := sum + a]. sum.                 "sum elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements"
sum := x inject: 0 into: [:a :c | a + c].                   "sum elements"
max := x inject: 0 into: [:a :c | (a > c)                   "find max element in collection"
   ifTrue: [a]
   ifFalse: [c]].
y := x asArray.                                             "convert to array"
y := x asOrderedCollection.                                 "convert to ordered collection"
y := x asSortedCollection.                                  "convert to sorted collection"
y := x asBag.                                               "convert to bag collection"
y := x asSet.                                               "convert to set collection"
```

## Bag:        like OrderedCollection except elements are in no particular order
```
| b x y sum max |
x := Bag with: 4 with: 3 with: 2 with: 1.                   "create collection with up to 4 elements"
x := Bag new.                                               "allocate collection"
x add: 4; add: 3; add: 1; add: 2; yourself.                 "add element to collection"
x add: 3 withOccurrences: 2.                                "add multiple copies to collection"
y := x addAll: #(7 8 9).                                    "add multiple elements to collection"
y := x removeAll: #(7 8 9).                                 "remove multiple elements from collection"
y := x remove: 4 ifAbsent: [].                              "remove element from collection"
b := x isEmpty.                                             "test if empty"
y := x size.                                                "number of elements"
b := x includes: 3.                                         "test if element is in collection"
y := x occurrencesOf: 3.                                    "number of times object in collection"
x do: [:a | Transcript show: a printString; cr].            "iterate over the collection"
b := x conform: [:a | (a >= 1) & (a <= 4)].                 "test if all elements meet condition"
y := x select: [:a | a > 2].                                "return collection of elements that pass test"
y := x reject: [:a | a < 2].                                "return collection of elements that fail test"
y := x collect: [:a | a + a].                               "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: [].                     "find position of first element that passes test"
sum := 0. x do: [:a | sum := sum + a]. sum.                 "sum elements"
sum := x inject: 0 into: [:a :c | a + c].                   "sum elements"
max := x inject: 0 into: [:a :c | (a > c)                   "find max element in collection"
   ifTrue: [a]
   ifFalse: [c]].
y := x asOrderedCollection.                                 "convert to ordered collection"
y := x asSortedCollection.                                  "convert to sorted collection"
y := x asBag.                                               "convert to bag collection"
y := x asSet.                                               "convert to set collection"
```

## Set:           like Bag except duplicates not allowed
## IdentitySet:   uses identity test (== rather than =)
```
| b x y sum max |
x := Set with: 4 with: 3 with: 2 with: 1.                   "create collection with up to 4 elements"
x := Set new.                                               "allocate collection"
x add: 4; add: 3; add: 1; add: 2; yourself.                 "add element to collection"
y := x addAll: #(7 8 9).                                    "add multiple elements to collection"
y := x removeAll: #(7 8 9).                                 "remove multiple elements from collection"
y := x remove: 4 ifAbsent: [].                              "remove element from collection"
b := x isEmpty.                                             "test if empty"
y := x size.                                                "number of elements"
x includes: 4.                                              "test if element is in collection"
x do: [:a | Transcript show: a printString; cr].            "iterate over the collection"
b := x conform: [:a | (a >= 1) & (a <= 4)].                 "test if all elements meet condition"
y := x select: [:a | a > 2].                                "return collection of elements that pass test"
y := x reject: [:a | a < 2].                                "return collection of elements that fail test"
y := x collect: [:a | a + a].                               "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: [].                     "find position of first element that passes test"
sum := 0. x do: [:a | sum := sum + a]. sum.                 "sum elements"
sum := x inject: 0 into: [:a :c | a + c].                   "sum elements"
max := x inject: 0 into: [:a :c | (a > c)                   "find max element in collection"
   ifTrue: [a]
   ifFalse: [c]].
y := x asArray.                                             "convert to array"
y := x asOrderedCollection.                                 "convert to ordered collection"
y := x asSortedCollection.                                  "convert to sorted collection"
y := x asBag.                                               "convert to bag collection"
y := x asSet.                                               "convert to set collection"
```

## Interval:
```
| b x y sum max |
x := Interval from: 5 to: 10.                               "create interval object"
x := 5 to: 10.
x := Interval from: 5 to: 10 by: 2.                         "create interval object with specified increment"
x := 5 to: 10 by: 2.
b := x isEmpty.                                             "test if empty"
y := x size.                                                "number of elements"
x includes: 9.                                              "test if element is in collection"
x do: [:k | Transcript show: k printString; cr].            "iterate over interval"
b := x conform: [:a | (a >= 1) & (a <= 4)].                 "test if all elements meet condition"
y := x select: [:a | a > 7].                                "return collection of elements that pass test"
y := x reject: [:a | a < 2].                                "return collection of elements that fail test"
y := x collect: [:a | a + a].                               "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: [].                     "find position of first element that passes test"
sum := 0. x do: [:a | sum := sum + a]. sum.                 "sum elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements"
sum := x inject: 0 into: [:a :c | a + c].                   "sum elements"
max := x inject: 0 into: [:a :c | (a > c)                   "find max element in collection"
   ifTrue: [a]
   ifFalse: [c]].
y := x asArray.                                             "convert to array"
y := x asOrderedCollection.                                 "convert to ordered collection"
y := x asSortedCollection.                                  "convert to sorted collection"
y := x asBag.                                               "convert to bag collection"
y := x asSet.                                               "convert to set collection"
```

##Associations:
```
| x y |
x := #myVar->'hello'.
y := x key.
y := x value.
```

## Dictionary:
## IdentityDictionary:   uses identity test (== rather than =)
```
| b x y |
x := Dictionary new.                                        "allocate collection"
x add: #a->4; add: #b->3; add: #c->1; add: #d->2; yourself. "add element to collection"
x at: #e put: 3.                                            "set element at index"
b := x isEmpty.                                             "test if empty"
y := x size.                                                "number of elements"
y := x at: #a ifAbsent: [].                                 "retrieve element at index"
y := x keyAtValue: 3 ifAbsent: [].                          "retrieve key for given value with error block"
y := x removeKey: #e ifAbsent: [].                          "remove element from collection"
b := x includes: 3.                                         "test if element is in values collection"
b := x includesKey: #a.                                     "test if element is in keys collection"
y := x occurrencesOf: 3.                                    "number of times object in collection"
y := x keys.                                                "set of keys"
y := x values.                                              "bag of values"
x do: [:a | Transcript show: a printString; cr].            "iterate over the values collection"
x keysDo: [:a | Transcript show: a printString; cr].        "iterate over the keys collection"
x associationsDo: [:a | Transcript show: a printString; cr]."iterate over the associations"
x keysAndValuesDo: [:aKey :aValue | Transcript              "iterate over keys and values"
   show: aKey printString; space;
   show: aValue printString; cr].
b := x conform: [:a | (a >= 1) & (a <= 4)].                 "test if all elements meet condition"
y := x select: [:a | a > 2].                                "return collection of elements that pass test"
y := x reject: [:a | a < 2].                                "return collection of elements that fail test"
y := x collect: [:a | a + a].                               "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: [].                     "find position of first element that passes test"
sum := 0. x do: [:a | sum := sum + a]. sum.                 "sum elements"
sum := x inject: 0 into: [:a :c | a + c].                   "sum elements"
max := x inject: 0 into: [:a :c | (a > c)                   "find max element in collection"
   ifTrue: [a]
   ifFalse: [c]].
y := x asArray.                                             "convert to array"
y := x asOrderedCollection.                                 "convert to ordered collection"
y := x asSortedCollection.                                  "convert to sorted collection"
y := x asBag.                                               "convert to bag collection"
y := x asSet.                                               "convert to set collection"

Smalltalk at: #CMRGlobal put: 'CMR entry'.                  "put global in Smalltalk Dictionary"
x := Smalltalk at: #CMRGlobal.                              "read global from Smalltalk Dictionary"
Transcript show: (CMRGlobal printString).                   "entries are directly accessible by name"
Smalltalk keys do: [ :k |                                   "print out all classes"
   ((Smalltalk at: k) isKindOf: Class)
      ifFalse: [Transcript show: k printString; cr]].
Smalltalk at: #CMRDictionary put: (Dictionary new).         "set up user defined dictionary"
CMRDictionary at: #MyVar1 put: 'hello1'.                    "put entry in dictionary"
CMRDictionary add: #MyVar2->'hello2'.                       "add entry to dictionary use key->value combo"
CMRDictionary size.                                         "dictionary size"
CMRDictionary keys do: [ :k |                               "print out keys in dictionary"
   Transcript show: k printString; cr].
CMRDictionary values do: [ :k |                             "print out values in dictionary"
   Transcript show: k printString; cr].
CMRDictionary keysAndValuesDo: [:aKey :aValue |             "print out keys and values"
   Transcript
      show: aKey printString;
      space;
      show: aValue printString;
      cr].
CMRDictionary associationsDo: [:aKeyValue |                 "another iterator for printing key values"
   Transcript show: aKeyValue printString; cr].
Smalltalk removeKey: #CMRGlobal ifAbsent: [].               "remove entry from Smalltalk dictionary"
Smalltalk removeKey: #CMRDictionary ifAbsent: [].           "remove user dictionary from Smalltalk dictionary"
```

## Internal Stream:
```
| b x ios |
ios := ReadStream on: 'Hello read stream'.
ios := ReadStream on: 'Hello read stream' from: 1 to: 5.
[(x := ios nextLine) notNil]
   whileTrue: [Transcript show: x; cr].
ios position: 3.
ios position.
x := ios next.
x := ios peek.
x := ios contents.
b := ios atEnd.

ios := ReadWriteStream on: 'Hello read stream'.
ios := ReadWriteStream on: 'Hello read stream' from: 1 to: 5.
ios := ReadWriteStream with: 'Hello read stream'.
ios := ReadWriteStream with: 'Hello read stream' from: 1 to: 10.
ios position: 0.
[(x := ios nextLine) notNil]
   whileTrue: [Transcript show: x; cr].
ios position: 6.
ios position.
ios nextPutAll: 'Chris'.
x := ios next.
x := ios peek.
x := ios contents.
b := ios atEnd.
```

## FileStream:
```
| b x ios |
ios := FileStream newFileNamed: 'ios.txt'.
ios nextPut: $H; cr.
ios nextPutAll: 'Hello File'; cr.
'Hello File' printOn: ios.
'Hello File' storeOn: ios.
ios close.

ios := FileStream oldFileNamed: 'ios.txt'.
[(x := ios nextLine) notNil]
   whileTrue: [Transcript show: x; cr].
ios position: 3.
x := ios position.
x := ios next.
x := ios peek.
b := ios atEnd.
ios close.
```

## Date:
```
| x y |
x := Date today.                                            "create date for today"
x := Date dateAndTimeNow.                                   "create date from current time/date"
x := Date readFromString: '01/02/1999'.                     "create date from formatted string"
x := Date newDay: 12 month: #July year: 1999                "create date from parts"
x := Date fromDays: 36000.                                  "create date from elapsed days since 1/1/1901"
y := Date dayOfWeek: #Monday.                               "day of week as int (1-7)"
y := Date indexOfMonth: #January.                           "month of year as int (1-12)"
y := Date daysInMonth: 2 forYear: 1996.                     "day of month as int (1-31)"
y := Date daysInYear: 1996.                                 "days in year (365|366)"
y := Date nameOfDay: 1                                      "weekday name (#Monday,...)"
y := Date nameOfMonth: 1.                                   "month name (#January,...)"
y := Date leapYear: 1996.                                   "1 if leap year; 0 if not leap year"
y := x weekday.                                             "day of week (#Monday,...)"
y := x previous: #Monday.                                   "date for previous day of week"
y := x dayOfMonth.                                          "day of month (1-31)"
y := x day.                                                 "day of year (1-366)"
y := x firstDayOfMonth.                                     "day of year for first day of month"
y := x monthName.                                           "month of year (#January,...)"
y := x monthIndex.                                          "month of year (1-12)"
y := x daysInMonth.                                         "days in month (1-31)"
y := x year.                                                "year (19xx)"
y := x daysInYear.                                          "days in year (365|366)"
y := x daysLeftInYear.                                      "days left in year (364|365)"
y := x asSeconds.                                           "seconds elapsed since 1/1/1901"
y := x addDays: 10.                                         "add days to date object"
y := x subtractDays: 10.                                    "subtract days to date object"
y := x subtractDate: (Date today).                          "subtract date (result in days)"
y := x printFormat: #(2 1 3 $/ 1 1).                        "print formatted date"
b := (x <= Date today).                                     "comparison"
```

## Time:
```
| x y |
x := Time now.                                              "create time from current time"
x := Time dateAndTimeNow.                                   "create time from current time/date"
x := Time readFromString: '3:47:26 pm'.                     "create time from formatted string"
x := Time fromSeconds: (60 * 60 * 4).                       "create time from elapsed time from midnight"
y := Time millisecondClockValue.                            "milliseconds since midnight"
y := Time totalSeconds.                                     "total seconds since 1/1/1901"
y := x seconds.                                             "seconds past minute (0-59)"
y := x minutes.                                             "minutes past hour (0-59)"
y := x hours.                                               "hours past midnight (0-23)"
y := x addTime: (Time now).                                 "add time to time object"
y := x subtractTime: (Time now).                            "subtract time to time object"
y := x asSeconds.                                           "convert time to seconds"
x := Time millisecondsToRun: [                              "timing facility"
   1 to: 1000 do: [:index | y := 3.14 * index]].
b := (x <= Time now).                                       "comparison"
```

## Point:
```
| x y |
x := 200@100.                                               "obtain a new point"
y := x x.                                                   "x coordinate"
y := x y.                                                   "y coordinate"
x := 200@100 negated.                                       "negates x and y"
x := (-200@-100) abs.                                       "absolute value of x and y"
x := (200.5@100.5) rounded.                                 "round x and y"
x := (200.5@100.5) truncated.                               "truncate x and y"
x := 200@100 + 100.                                         "add scale to both x and y"
x := 200@100 - 100.                                         "subtract scale from both x and y"
x := 200@100 * 2.                                           "multiply x and y by scale"
x := 200@100 / 2.                                           "divide x and y by scale"
x := 200@100 // 2.                                          "divide x and y by scale"
x := 200@100 \\ 3.                                          "remainder of x and y by scale"
x := 200@100 + 50@25.                                       "add points"
x := 200@100 - 50@25.                                       "subtract points"
x := 200@100 * 3@4.                                         "multiply points"
x := 200@100 // 3@4.                                        "divide points"
x := 200@100 max: 50@200.                                   "max x and y"
x := 200@100 min: 50@200.                                   "min x and y"
x := 20@5 dotProduct: 10@2.                                 "sum of product (x1*x2 + y1*y2)"
```

## Rectangle:
```
Rectangle fromUser.
```

## Pen:
```
| myPen |
Display restoreAfter: [
   Display fillWhite.

myPen := Pen new.                                           "get graphic pen"
myPen squareNib: 1.
myPen color: (Color blue).                                  "set pen color"
myPen home.                                                 "position pen at center of display"
myPen up.                                                   "makes nib unable to draw"
myPen down.                                                 "enable the nib to draw"
myPen north.                                                "points direction towards top"
myPen turn: -180.                                           "add specified degrees to direction"
myPen direction.                                            "get current angle of pen"
myPen go: 50.                                               "move pen specified number of pixels"
myPen location.                                             "get the pen position"
myPen goto: 200@200.                                        "move to specified point"
myPen place: 250@250.                                       "move to specified point without drawing"
myPen print: 'Hello World' withFont: (TextStyle default fontAt: 1).
Display extent.                                             "get display width@height"
Display width.                                              "get display width"
Display height.                                             "get display height"

].
```

## Dynamic Message Calling/Compiling:
```
| receiver message result argument keyword1 keyword2 argument1 argument2 |
"unary message"
receiver := 5.
message := 'factorial' asSymbol.
result := receiver perform: message.
result := Compiler evaluate: ((receiver storeString), ' ', message).
result := (Message new setSelector: message arguments: #()) sentTo: receiver.

"binary message"
receiver := 1.
message := '+' asSymbol.
argument := 2.
result := receiver perform: message withArguments: (Array with: argument).
result := Compiler evaluate: ((receiver storeString), ' ', message, ' ', (argument storeString)).
result := (Message new setSelector: message arguments: (Array with: argument)) sentTo: receiver.

"keyword messages"
receiver := 12.
keyword1 := 'between:' asSymbol.
keyword2 := 'and:' asSymbol.
argument1 := 10.
argument2 := 20.
result := receiver
   perform: (keyword1, keyword2) asSymbol
   withArguments: (Array with: argument1 with: argument2).
result := Compiler evaluate:
   ((receiver storeString), ' ', keyword1, (argument1 storeString) , ' ', keyword2, (argument2 storeString)).
result := (Message
   new
      setSelector: (keyword1, keyword2) asSymbol
      arguments: (Array with: argument1 with: argument2))
   sentTo: receiver.
```

## Class/Meta-class:
```
| b x |
x := String name.                                           "class name"
x := String category.                                       "organization category"
x := String comment.                                        "class comment"
x := String kindOfSubclass.                                 "subclass type - subclass: variableSubclass, etc"
x := String definition.                                     "class definition"
x := String instVarNames.                                   "immediate instance variable names"
x := String allInstVarNames.                                "accumulated instance variable names"
x := String classVarNames.                                  "immediate class variable names"
x := String allClassVarNames.                               "accumulated class variable names"
x := String sharedPools.                                    "immediate dictionaries used as shared pools"
x := String allSharedPools.                                 "accumulated dictionaries used as shared pools"
x := String selectors.                                      "message selectors for class"
x := String sourceCodeAt: #size.                            "source code for specified method"
x := String allInstances.                                   "collection of all instances of class"
x := String superclass.                                     "immediate superclass"
x := String allSuperclasses.                                "accumulated superclasses"
x := String withAllSuperclasses.                            "receiver class and accumulated superclasses"
x := String subclasses.                                     "immediate subclasses"
x := String allSubclasses.                                  "accumulated subclasses"
x := String withAllSubclasses.                              "receiver class and accumulated subclasses"
b := String instSize.                                       "number of named instance variables"
b := String isFixed.                                        "true if no indexed instance variables"
b := String isVariable.                                     "true if has indexed instance variables"
b := String isPointers.                                     "true if index instance vars contain objects"
b := String isBits.                                         "true if index instance vars contain bytes/words"
b := String isBytes.                                        "true if index instance vars contain bytes"
b := String isWords.                                        true if index instance vars contain words"
Object withAllSubclasses size.                              "get total number of class entries"
```

## Debugging:
```
| a b x |
x yourself.                                                 "returns receiver"
String browse.                                              "browse specified class"
x inspect.                                                  "open object inspector window"
x confirm: 'Is this correct?'.
x halt.                                                     "breakpoint to open debugger window"
x halt: 'Halt message'.
x notify: 'Notify text'.
x error: 'Error string'.                                    "open up error window with title"
x doesNotUnderstand: #cmrMessage.                           "flag message is not handled"
x shouldNotImplement.                                       "flag message should not be implemented"
x subclassResponsibility.                                   "flag message as abstract"
x errorImproperStore.                                       "flag an improper store into indexable object"
x errorNonIntegerIndex.                                     "flag only integers should be used as index"
x errorSubscriptBounds.                                     "flag subscript out of bounds"
x primitiveFailed.                                          "system primitive failed"

a := 'A1'. b := 'B2'. a become: b.                          "switch two objects"
Transcript show: a, b; cr.
```

## Misc
```
| x |
"Smalltalk condenseChanges."                                "compress the change file"
x := FillInTheBlank request: 'Prompt Me'.                   "prompt user for input"
Utilities openCommandKeyHelp
```




## Ready For More?

### Free Online

* [GNU Smalltalk User's Guide](https://www.gnu.org/software/smalltalk/manual/html_node/Tutorial.html)
* [smalltalk dot org](http://www.smalltalk.org/)
* [Computer Programming using GNU Smalltalk](http://www.canol.info/books/computer_programming_using_gnu_smalltalk/)
* [Smalltalk Cheatsheet](http://www.angelfire.com/tx4/cus/notes/smalltalk.html)
* [Smalltalk-72 Manual](http://www.bitsavers.org/pdf/xerox/parc/techReports/Smalltalk-72_Instruction_Manual_Mar76.pdf)
* [BYTE: A Special issue on Smalltalk](https://archive.org/details/byte-magazine-1981-08)
* [Smalltalk, Objects, and Design](https://books.google.co.in/books?id=W8_Une9cbbgC&printsec=frontcover&dq=smalltalk&hl=en&sa=X&ved=0CCIQ6AEwAWoVChMIw63Vo6CpyAIV0HGOCh3S2Alf#v=onepage&q=smalltalk&f=false)
* [Smalltalk: An Introduction to Application Development Using VisualWorks](https://books.google.co.in/books?id=zalQAAAAMAAJ&q=smalltalk&dq=smalltalk&hl=en&sa=X&ved=0CCgQ6AEwAmoVChMIw63Vo6CpyAIV0HGOCh3S2Alf/)
* [Smalltalk Programming Resources](http://www.whoishostingthis.com/resources/smalltalk/)
---
language: Solidity
filename: learnSolidity.sol
contributors:
  - ["Nemil Dalal", "https://www.nemil.com"]
  - ["Joseph Chow", ""]
---

Solidity lets you program on [Ethereum](https://www.ethereum.org/), a
blockchain-based virtual machine that allows the creation and
execution of smart contracts, without requiring centralized or trusted parties.

Solidity is a statically typed, contract programming language that has
similarities to Javascript and C. Like objects in OOP, each contract contains
state variables, functions, and common data types. Contract-specific features
include modifier (guard) clauses, event notifiers for listeners, and custom
global variables.

Some Ethereum contract examples include crowdfunding, voting, and blind auctions.

There is a high risk and high cost of errors in Solidity code, so you must be very careful to test
and slowly rollout. WITH THE RAPID CHANGES IN ETHEREUM, THIS DOCUMENT IS UNLIKELY TO STAY UP TO
DATE, SO YOU SHOULD FOLLOW THE SOLIDITY CHAT ROOM AND ETHEREUM BLOG FOR THE LATEST. ALL CODE HERE IS
PROVIDED AS IS, WITH SUBSTANTIAL RISK OF ERRORS OR DEPRECATED CODE PATTERNS.

Unlike other code, you may also need to add in design patterns like pausing, deprecation, and
throttling usage to reduce risk. This document primarily discusses syntax, and so excludes many
popular design patterns.

As Solidity and Ethereum are under active development, experimental or beta
features are typically marked, and subject to change. Pull requests welcome.

```javascript
// First, a simple Bank contract
// Allows deposits, withdrawals, and balance checks

// simple_bank.sol (note .sol extension)
/* **** START EXAMPLE **** */

// Declare the source file compiler version.
pragma solidity ^0.4.2;

// Start with Natspec comment (the three slashes)
// used for documentation - and as descriptive data for UI elements/actions

/// @title SimpleBank
/// @author nemild

/* 'contract' has similarities to 'class' in other languages (class variables,
inheritance, etc.) */
contract SimpleBank { // CapWords
    // Declare state variables outside function, persist through life of contract

    // dictionary that maps addresses to balances
    // always be careful about overflow attacks with numbers
    mapping (address => uint) private balances;

    // "private" means that other contracts can't directly query balances
    // but data is still viewable to other parties on blockchain

    address public owner;
    // 'public' makes externally readable (not writeable) by users or contracts

    // Events - publicize actions to external listeners
    event LogDepositMade(address accountAddress, uint amount);

    // Constructor, can receive one or many variables here; only one allowed
    function SimpleBank() {
        // msg provides details about the message that's sent to the contract
        // msg.sender is contract caller (address of contract creator)
        owner = msg.sender;
    }

    /// @notice Deposit ether into bank
    /// @return The balance of the user after the deposit is made
    function deposit() public returns (uint) {
        balances[msg.sender] += msg.value;
        // no "this." or "self." required with state variable
        // all values set to data type's initial value by default

        LogDepositMade(msg.sender, msg.value); // fire event

        return balances[msg.sender];
    }

    /// @notice Withdraw ether from bank
    /// @dev This does not return any excess ether sent to it
    /// @param withdrawAmount amount you want to withdraw
    /// @return The balance remaining for the user
    function withdraw(uint withdrawAmount) public returns (uint remainingBal) {
        if(balances[msg.sender] >= withdrawAmount) {
            // Note the way we deduct the balance right away, before sending - due to
            // the risk of a recursive call that allows the caller to request an amount greater
            // than their balance
            balances[msg.sender] -= withdrawAmount;

            if (!msg.sender.send(withdrawAmount)) {
                // increment back only on fail, as may be sending to contract that
                // has overridden 'send' on the receipt end
                balances[msg.sender] += withdrawAmount;
            }
        }

        return balances[msg.sender];
    }

    /// @notice Get balance
    /// @return The balance of the user
    // 'constant' prevents function from editing state variables;
    // allows function to run locally/off blockchain
    function balance() constant returns (uint) {
        return balances[msg.sender];
    }

    // Fallback function - Called if other functions don't match call or
    // sent ether without data
    // Typically, called when invalid data is sent
    // Added so ether sent to this contract is reverted if the contract fails
    // otherwise, the sender's money is transferred to contract
    function () {
        throw; // throw reverts state to before call
    }
}
// ** END EXAMPLE **


// Now, the basics of Solidity

// 1. DATA TYPES AND ASSOCIATED METHODS
// uint used for currency amount (there are no doubles
//  or floats) and for dates (in unix time)
uint x;

// int of 256 bits, cannot be changed after instantiation
int constant a = 8;
int256 constant a = 8; // same effect as line above, here the 256 is explicit
uint constant VERSION_ID = 0x123A1; // A hex constant
// with 'constant', compiler replaces each occurrence with actual value


// For int and uint, can explicitly set space in steps of 8 up to 256
// e.g., int8, int16, int24
uint8 b;
int64 c;
uint248 e;

// Be careful that you don't overflow, and protect against attacks that do

// No random functions built in, use other contracts for randomness

// Type casting
int x = int(b);

bool b = true; // or do 'var b = true;' for inferred typing

// Addresses - holds 20 byte/160 bit Ethereum addresses
// No arithmetic allowed
address public owner;

// Types of accounts:
// Contract account: address set on create (func of creator address, num transactions sent)
// External Account: (person/external entity): address created from public key

// Add 'public' field to indicate publicly/externally accessible
// a getter is automatically created, but NOT a setter

// All addresses can be sent ether
owner.send(SOME_BALANCE); // returns false on failure
if (owner.send) {} // REMEMBER: wrap in 'if', as contract addresses have
// functions executed on send and these can fail
// Also, make sure to deduct balances BEFORE attempting a send, as there is a risk of a recursive
// call that can drain the contract

// can override send by defining your own

// Can check balance
owner.balance; // the balance of the owner (user or contract)


// Bytes available from 1 to 32
byte a; // byte is same as bytes1
bytes2 b;
bytes32 c;

// Dynamically sized bytes
bytes m; // A special array, same as byte[] array (but packed tightly)
// More expensive than byte1-byte32, so use those when possible

// same as bytes, but does not allow length or index access (for now)
string n = "hello"; // stored in UTF8, note double quotes, not single
// string utility functions to be added in future
// prefer bytes32/bytes, as UTF8 uses more storage

// Type inference
// var does inferred typing based on first assignment,
// can't be used in functions parameters
var a = true;
// use carefully, inference may provide wrong type
// e.g., an int8, when a counter needs to be int16

// var can be used to assign function to variable
function a(uint x) returns (uint) {
    return x * 2;
}
var f = a;
f(22); // call

// by default, all values are set to 0 on instantiation

// Delete can be called on most types
// (does NOT destroy value, but sets value to 0, the initial value)
uint x = 5;


// Destructuring/Tuples
(x, y) = (2, 7); // assign/swap multiple value


// 2. DATA STRUCTURES
// Arrays
bytes32[5] nicknames; // static array
bytes32[] names; // dynamic array
uint newLength = names.push("John"); // adding returns new length of the array
// Length
names.length; // get length
names.length = 1; // lengths can be set (for dynamic arrays in storage only)

// multidimensional array
uint x[][5]; // arr with 5 dynamic array elements (opp order of most languages)

// Dictionaries (any type to any other type)
mapping (string => uint) public balances;
balances["charles"] = 1;
console.log(balances["ada"]); // is 0, all non-set key values return zeroes
// 'public' allows following from another contract
contractName.balances("charles"); // returns 1
// 'public' created a getter (but not setter) like the following:
function balances(string _account) returns (uint balance) {
    return balances[_account];
}

// Nested mappings
mapping (address => mapping (address => uint)) public custodians;

// To delete
delete balances["John"];
delete balances; // sets all elements to 0

// Unlike other languages, CANNOT iterate through all elements in
// mapping, without knowing source keys - can build data structure
// on top to do this

// Structs and enums
struct Bank {
    address owner;
    uint balance;
}
Bank b = Bank({
    owner: msg.sender,
    balance: 5
});
// or
Bank c = Bank(msg.sender, 5);

c.amount = 5; // set to new value
delete b;
// sets to initial value, set all variables in struct to 0, except mappings

// Enums
enum State { Created, Locked, Inactive }; // often used for state machine
State public state; // Declare variable from enum
state = State.Created;
// enums can be explicitly converted to ints
uint createdState = uint(State.Created); //  0

// Data locations: Memory vs. storage vs. stack - all complex types (arrays,
// structs) have a data location
// 'memory' does not persist, 'storage' does
// Default is 'storage' for local and state variables; 'memory' for func params
// stack holds small local variables

// for most types, can explicitly set which data location to use


// 3. Simple operators
// Comparisons, bit operators and arithmetic operators are provided
// exponentiation: **
// exclusive or: ^
// bitwise negation: ~


// 4. Global Variables of note
// ** this **
this; // address of contract
// often used at end of contract life to send remaining balance to party
this.balance;
this.someFunction(); // calls func externally via call, not via internal jump

// ** msg - Current message received by the contract ** **
msg.sender; // address of sender
msg.value; // amount of ether provided to this contract in wei
msg.data; // bytes, complete call data
msg.gas; // remaining gas

// ** tx - This transaction **
tx.origin; // address of sender of the transaction
tx.gasprice; // gas price of the transaction

// ** block - Information about current block **
now; // current time (approximately), alias for block.timestamp (uses Unix time)
block.number; // current block number
block.difficulty; // current block difficulty
block.blockhash(1); // returns bytes32, only works for most recent 256 blocks
block.gasLimit();

// ** storage - Persistent storage hash **
storage['abc'] = 'def'; // maps 256 bit words to 256 bit words


// 4. FUNCTIONS AND MORE
// A. Functions
// Simple function
function increment(uint x) returns (uint) {
    x += 1;
    return x;
}

// Functions can return many arguments, and by specifying returned arguments
// name don't need to explicitly return
function increment(uint x, uint y) returns (uint x, uint y) {
    x += 1;
    y += 1;
}
// Call previous functon
uint (a,b) = increment(1,1);

// 'constant' indicates that function does not/cannot change persistent vars
// Constant function execute locally, not on blockchain
uint y;

function increment(uint x) constant returns (uint x) {
    x += 1;
    y += 1; // this line would fail
    // y is a state variable, and can't be changed in a constant function
}

// 'Function Visibility specifiers'
// These can be placed where 'constant' is, including:
// public - visible externally and internally (default)
// external
// private - only visible in the current contract
// internal - only visible in current contract, and those deriving from it

// Functions hoisted - and can assign a function to a variable
function a() {
    var z = b;
    b();
}

function b() {

}


// Prefer loops to recursion (max call stack depth is 1024)

// B. Events
// Events are notify external parties; easy to search and
// access events from outside blockchain (with lightweight clients)
// typically declare after contract parameters

// Typically, capitalized - and add Log in front to be explicit and prevent confusion
// with a function call

// Declare
event LogSent(address indexed from, address indexed to, uint amount); // note capital first letter

// Call
Sent(from, to, amount);

// For an external party (a contract or external entity), to watch:
Coin.Sent().watch({}, '', function(error, result) {
    if (!error) {
        console.log("Coin transfer: " + result.args.amount +
            " coins were sent from " + result.args.from +
            " to " + result.args.to + ".");
        console.log("Balances now:\n" +
            "Sender: " + Coin.balances.call(result.args.from) +
            "Receiver: " + Coin.balances.call(result.args.to));
    }
}
// Common paradigm for one contract to depend on another (e.g., a
// contract that depends on current exchange rate provided by another)

// C. Modifiers
// Modifiers validate inputs to functions such as minimal balance or user auth;
// similar to guard clause in other languages

// '_' (underscore) often included as last line in body, and indicates
// function being called should be placed there
modifier onlyAfter(uint _time) { if (now <= _time) throw; _ }
modifier onlyOwner { if (msg.sender == owner) _ }
// commonly used with state machines
modifier onlyIfState (State currState) { if (currState != State.A) _ }

// Append right after function declaration
function changeOwner(newOwner)
onlyAfter(someTime)
onlyOwner()
onlyIfState(State.A)
{
    owner = newOwner;
}

// underscore can be included before end of body,
// but explicitly returning will skip, so use carefully
modifier checkValue(uint amount) {
    _
    if (msg.value > amount) {
        uint amountToRefund = amount - msg.value;
        if (!msg.sender.send(amountToRefund)) {
            throw;
        }
    }
}


// 6. BRANCHING AND LOOPS

// All basic logic blocks work - including if/else, for, while, break, continue
// return - but no switch

// Syntax same as javascript, but no type conversion from non-boolean
// to boolean (comparison operators must be used to get the boolean val)

// For loops that are determined by user behavior, be careful - as contracts have a maximal
// amount of gas for a block of code - and will fail if that is exceeded
// For example:
for(uint x = 0; x < refundAddressList.length; x++) {
    if (!refundAddressList[x].send(SOME_AMOUNT)) {
       throw;
    }
}

// Two errors above:
// 1. A failure on send stops the loop from completing, tying up money
// 2. This loop could be arbitrarily long (based on the amount of users who need refunds), and
// therefore may always fail as it exceeds the max gas for a block
// Instead, you should let people withdraw individually from their subaccount, and mark withdrawn


// 7. OBJECTS/CONTRACTS

// A. Calling external contract
contract infoFeed {
    function info() returns (uint ret) { return 42; }
}

contract Consumer {
    InfoFeed feed; // points to contract on blockchain

    // Set feed to existing contract instance
    function setFeed(address addr) {
        // automatically cast, be careful; constructor is not called
        feed = InfoFeed(addr);
    }

    // Set feed to new instance of contract
    function createNewFeed() {
        feed = new InfoFeed(); // new instance created; constructor called
    }

    function callFeed() {
        // final parentheses call contract, can optionally add
        // custom ether value or gas
        feed.info.value(10).gas(800)();
    }
}

// B. Inheritance

// Order matters, last inherited contract (i.e., 'def') can override parts of
// previously inherited contracts
contract MyContract is abc, def("a custom argument to def") {

// Override function
    function z() {
        if (msg.sender == owner) {
            def.z(); // call overridden function from def
            super.z(); // call immediate parent overridden function
        }
    }
}

// abstract function
function someAbstractFunction(uint x);
// cannot be compiled, so used in base/abstract contracts
// that are then implemented

// C. Import

import "filename";
import "github.com/ethereum/dapp-bin/library/iterable_mapping.sol";

// Importing under active development
// Cannot currently be done at command line


// 8. OTHER KEYWORDS

// A. Throwing
// Throwing
throw; // reverts unused money to sender, state is reverted
// Can't currently catch

// Common design pattern is:
if (!addr.send(123)) {
    throw;
}

// B. Selfdestruct
// selfdestruct current contract, sending funds to address (often creator)
selfdestruct(SOME_ADDRESS);

// removes storage/code from current/future blocks
// helps thin clients, but previous data persists in blockchain

// Common pattern, lets owner end the contract and receive remaining funds
function remove() {
    if(msg.sender == creator) { // Only let the contract creator do this
        selfdestruct(creator); // Makes contract inactive, returns funds
    }
}

// May want to deactivate contract manually, rather than selfdestruct
// (ether sent to selfdestructed contract is lost)


// 9. CONTRACT DESIGN NOTES

// A. Obfuscation
// All variables are publicly viewable on blockchain, so anything
// that is private needs to be obfuscated (e.g., hashed w/secret)

// Steps: 1. Commit to something, 2. Reveal commitment
sha3("some_bid_amount", "some secret"); // commit

// call contract's reveal function in the future
// showing bid plus secret that hashes to SHA3
reveal(100, "mySecret");

// B. Storage optimization
// Writing to blockchain can be expensive, as data stored forever; encourages
// smart ways to use memory (eventually, compilation will be better, but for now
// benefits to planning data structures - and storing min amount in blockchain)

// Cost can often be high for items like multidimensional arrays
// (cost is for storing data - not declaring unfilled variables)

// C. Data access in blockchain
// Cannot restrict human or computer from reading contents of
// transaction or transaction's state

// While 'private' prevents other *contracts* from reading data
// directly - any other party can still read data in blockchain

// All data to start of time is stored in blockchain, so
// anyone can observe all previous data and changes

// D. Cron Job
// Contracts must be manually called to handle time-based scheduling; can create external
// code to regularly ping, or provide incentives (ether) for others to

// E. Observer Pattern
// An Observer Pattern lets you register as a subscriber and
// register a function which is called by the oracle (note, the oracle pays
// for this action to be run)
// Some similarities to subscription in Pub/sub

// This is an abstract contract, both client and server classes import
// the client should implement
contract SomeOracleCallback {
    function oracleCallback(int _value, uint _time, bytes32 info) external;
}

contract SomeOracle {
    SomeOracleCallback[] callbacks; // array of all subscribers

    // Register subscriber
    function addSubscriber(SomeOracleCallback a) {
        callbacks.push(a);
    }

    function notify(value, time, info) private {
        for(uint i = 0;i < callbacks.length; i++) {
            // all called subscribers must implement the oracleCallback
            callbacks[i].oracleCallback(value, time, info);
        }
    }

    function doSomething() public {
        // Code to do something

        // Notify all subscribers
        notify(_value, _time, _info);
    }
}

// Now, your client contract can addSubscriber by importing SomeOracleCallback
// and registering with Some Oracle

// F. State machines
// see example below for State enum and inState modifier


// *** EXAMPLE: A crowdfunding example (broadly similar to Kickstarter) ***
// ** START EXAMPLE **

// CrowdFunder.sol

/// @title CrowdFunder
/// @author nemild
contract CrowdFunder {
    // Variables set on create by creator
    address public creator;
    address public fundRecipient; // creator may be different than recipient
    uint public minimumToRaise; // required to tip, else everyone gets refund
    string campaignUrl;
    byte constant version = 1;

    // Data structures
    enum State {
        Fundraising,
        ExpiredRefund,
        Successful
    }
    struct Contribution {
        uint amount;
        address contributor;
    }

    // State variables
    State public state = State.Fundraising; // initialize on create
    uint public totalRaised;
    uint public raiseBy;
    uint public completeAt;
    Contribution[] contributions;

    event LogFundingReceived(address addr, uint amount, uint currentTotal);
    event LogWinnerPaid(address winnerAddress);

    modifier inState(State _state) {
        if (state != _state) throw;
        _
    }

    modifier isCreator() {
        if (msg.sender != creator) throw;
        _
    }

    // Wait 6 months after final contract state before allowing contract destruction
    modifier atEndOfLifecycle() {
    if(!((state == State.ExpiredRefund || state == State.Successful) &&
        completeAt + 6 months < now)) {
            throw;
        }
        _
    }

    function CrowdFunder(
        uint timeInHoursForFundraising,
        string _campaignUrl,
        address _fundRecipient,
        uint _minimumToRaise)
    {
        creator = msg.sender;
        fundRecipient = _fundRecipient;
        campaignUrl = _campaignUrl;
        minimumToRaise = _minimumToRaise;
        raiseBy = now + (timeInHoursForFundraising * 1 hours);
    }

    function contribute()
    public
    inState(State.Fundraising)
    {
        contributions.push(
            Contribution({
                amount: msg.value,
                contributor: msg.sender
            }) // use array, so can iterate
        );
        totalRaised += msg.value;

        LogFundingReceived(msg.sender, msg.value, totalRaised);

        checkIfFundingCompleteOrExpired();
        return contributions.length - 1; // return id
    }

    function checkIfFundingCompleteOrExpired() {
        if (totalRaised > minimumToRaise) {
            state = State.Successful;
            payOut();

            // could incentivize sender who initiated state change here
        } else if ( now > raiseBy )  {
            state = State.ExpiredRefund; // backers can now collect refunds by calling getRefund(id)
        }
        completeAt = now;
    }

    function payOut()
    public
    inState(State.Successful)
    {
        if(!fundRecipient.send(this.balance)) {
            throw;
        }


        LogWinnerPaid(fundRecipient);
    }

    function getRefund(id)
    public
    inState(State.ExpiredRefund)
    {
        if (contributions.length <= id || id < 0 || contributions[id].amount == 0 ) {
            throw;
        }

        uint amountToRefund = contributions[id].amount;
        contributions[id].amount = 0;

        if(!contributions[id].contributor.send(amountToSend)) {
            contributions[id].amount = amountToSend;
            return false;
        }

      return true;
    }

    function removeContract()
    public
    isCreator()
    atEndOfLifecycle()
    {
        selfdestruct(msg.sender);
        // creator gets all money that hasn't be claimed
    }

    function () { throw; }
}
// ** END EXAMPLE **

// 10. OTHER NATIVE FUNCTIONS

// Currency units
// Currency is defined using wei, smallest unit of Ether
uint minAmount = 1 wei;
uint a = 1 finney; // 1 ether == 1000 finney
// Other units, see: http://ether.fund/tool/converter

// Time units
1 == 1 second
1 minutes == 60 seconds

// Can multiply a variable times unit, as units are not stored in a variable
uint x = 5;
(x * 1 days); // 5 days

// Careful about leap seconds/years with equality statements for time
// (instead, prefer greater than/less than)

// Cryptography
// All strings passed are concatenated before hash action
sha3("ab", "cd");
ripemd160("abc");
sha256("def");

// 11. SECURITY

// Bugs can be disastrous in Ethereum contracts - and even popular patterns in Solidity,
// may be found to be antipatterns

// See security links at the end of this doc

// 12. LOW LEVEL FUNCTIONS
// call - low level, not often used, does not provide type safety
successBoolean = someContractAddress.call('function_name', 'arg1', 'arg2');

// callcode - Code at target address executed in *context* of calling contract
// provides library functionality
someContractAddress.callcode('function_name');


// 13. STYLE NOTES
// Based on Python's PEP8 style guide

// Quick summary:
// 4 spaces for indentation
// Two lines separate contract declarations (and other top level declarations)
// Avoid extraneous spaces in parentheses
// Can omit curly braces for one line statement (if, for, etc)
// else should be placed on own line


// 14. NATSPEC COMMENTS
// used for documentation, commenting, and external UIs

// Contract natspec - always above contract definition
/// @title Contract title
/// @author Author name

// Function natspec
/// @notice information about what function does; shown when function to execute
/// @dev Function documentation for developer

// Function parameter/return value natspec
/// @param someParam Some description of what the param does
/// @return Description of the return value
```

## Additional resources
- [Solidity Docs](https://solidity.readthedocs.org/en/latest/)
- [Solidity Style Guide](https://ethereum.github.io/solidity//docs/style-guide/): Ethereum's style guide is heavily derived from Python's [pep8](https://www.python.org/dev/peps/pep-0008/) style guide.
- [Browser-based Solidity Editor](http://chriseth.github.io/browser-solidity/)
- [Gitter Solidity Chat room](https://gitter.im/ethereum/solidity)
- [Modular design strategies for Ethereum Contracts](https://docs.erisindustries.com/tutorials/solidity/)

## Sample contracts
- [Dapp Bin](https://github.com/ethereum/dapp-bin)
- [Solidity Baby Step Contracts](https://github.com/fivedogit/solidity-baby-steps/tree/master/contracts)
- [ConsenSys Contracts](https://github.com/ConsenSys/dapp-store-contracts)
- [State of Dapps](http://dapps.ethercasts.com/)

## Security
- [Thinking About Smart Contract Security](https://blog.ethereum.org/2016/06/19/thinking-smart-contract-security/)
- [Smart Contract Security](https://blog.ethereum.org/2016/06/10/smart-contract-security/)
- [Hacking Distributed Blog](http://hackingdistributed.com/)

## Information purposefully excluded
- Libraries

## Style
- Python's [PEP8](https://www.python.org/dev/peps/pep-0008/) is used as the baseline style guide, including its general philosophy

## Editors
- [Vim Solidity](https://github.com/tomlion/vim-solidity)
- Editor Snippets ([Ultisnips format](https://gist.github.com/nemild/98343ce6b16b747788bc))

## Future To Dos
- New keywords: protected, inheritable
- List of common design patterns (throttling, RNG, version upgrade)
- Common security anti patterns

Feel free to send a pull request with any edits - or email nemild -/at-/ gmail
---
language: "Standard ML"
filename: standardml.sml
contributors:
    - ["Simon Shine", "http://shine.eu.org/"]
    - ["David Pedersen", "http://lonelyproton.com/"]
    - ["James Baker", "http://www.jbaker.io/"]
    - ["Leo Zovic", "http://langnostic.inaimathi.ca/"]
---

Standard ML is a functional programming language with type inference and some
side-effects.  Some of the hard parts of learning Standard ML are: Recursion,
pattern matching, type inference (guessing the right types but never allowing
implicit type conversion). Standard ML is distinguished from Haskell by including
references, allowing variables to be updated.

```ocaml
(* Comments in Standard ML begin with (* and end with *).  Comments can be
   nested which means that all (* tags must end with a *) tag.  This comment,
   for example, contains two nested comments. *)

(* A Standard ML program consists of declarations, e.g. value declarations: *)
val rent = 1200
val phone_no = 5551337
val pi = 3.14159
val negative_number = ~15  (* Yeah, unary minus uses the 'tilde' symbol *)

(* Optionally, you can explicitly declare types. This is not necessary as
   ML will automatically figure out the types of your values. *)
val diameter = 7926 : int
val e = 2.718 : real
val name = "Bobby" : string

(* And just as importantly, functions: *)
fun is_large(x : int) = if x > 37 then true else false

(* Floating-point numbers are called "reals". *)
val tau = 2.0 * pi         (* You can multiply two reals *)
val twice_rent = 2 * rent  (* You can multiply two ints *)
(* val meh = 1.25 * 10 *)  (* But you can't multiply an int and a real *)
val yeh = 1.25 * (Real.fromInt 10) (* ...unless you explicitly convert
                                      one or the other *)

(* +, - and * are overloaded so they work for both int and real. *)
(* The same cannot be said for division which has separate operators: *)
val real_division = 14.0 / 4.0  (* gives 3.5 *)
val int_division  = 14 div 4    (* gives 3, rounding down *)
val int_remainder = 14 mod 4    (* gives 2, since 3*4 = 12 *)

(* ~ is actually sometimes a function (e.g. when put in front of variables) *)
val negative_rent = ~(rent)  (* Would also have worked if rent were a "real" *)

(* There are also booleans and boolean operators *)
val got_milk = true
val got_bread = false
val has_breakfast = got_milk andalso got_bread  (* 'andalso' is the operator *)
val has_something = got_milk orelse got_bread   (* 'orelse' is the operator *)
val is_sad = not(has_something)                 (* not is a function *)

(* Many values can be compared using equality operators: = and <> *)
val pays_same_rent = (rent = 1300)  (* false *)
val is_wrong_phone_no = (phone_no <> 5551337)  (* false *)

(* The operator <> is what most other languages call !=. *)
(* 'andalso' and 'orelse' are called && and || in many other languages. *)

(* Actually, most of the parentheses above are unnecessary.  Here are some
   different ways to say some of the things mentioned above: *)
fun is_large x = x > 37  (* The parens above were necessary because of ': int' *)
val is_sad = not has_something
val pays_same_rent = rent = 1300  (* Looks confusing, but works *)
val is_wrong_phone_no = phone_no <> 5551337
val negative_rent = ~rent  (* ~ rent (notice the space) would also work *)

(* Parentheses are mostly necessary when grouping things: *)
val some_answer = is_large (5 + 5)      (* Without parens, this would break! *)
(* val some_answer = is_large 5 + 5 *)  (* Read as: (is_large 5) + 5. Bad! *)


(* Besides booleans, ints and reals, Standard ML also has chars and strings: *)
val foo = "Hello, World!\n"  (* The \n is the escape sequence for linebreaks *)
val one_letter = #"a"        (* That funky syntax is just one character, a *)

val combined = "Hello " ^ "there, " ^ "fellow!\n"  (* Concatenate strings *)

val _ = print foo       (* You can print things. We are not interested in the *)
val _ = print combined  (* result of this computation, so we throw it away. *)
(* val _ = print one_letter *)  (* Only strings can be printed this way *)


val bar = [ #"H", #"e", #"l", #"l", #"o" ]  (* SML also has lists! *)
(* val _ = print bar *)  (* Lists are unfortunately not the same as strings *)

(* Fortunately they can be converted.  String is a library and implode and size
   are functions available in that library that take strings as argument. *)
val bob = String.implode bar          (* gives "Hello" *)
val bob_char_count = String.size bob  (* gives 5 *)
val _ = print (bob ^ "\n")            (* For good measure, add a linebreak *)

(* You can have lists of any kind *)
val numbers = [1, 3, 3, 7, 229, 230, 248]  (* : int list *)
val names = [ "Fred", "Jane", "Alice" ]    (* : string list *)

(* Even lists of lists of things *)
val groups = [ [ "Alice", "Bob" ],
               [ "Huey", "Dewey", "Louie" ],
               [ "Bonnie", "Clyde" ] ]     (* : string list list *)

val number_count = List.length numbers     (* gives 7 *)

(* You can put single values in front of lists of the same kind using
   the :: operator, called "the cons operator" (known from Lisp). *)
val more_numbers = 13 :: numbers  (* gives [13, 1, 3, 3, 7, ...] *)
val more_groups  = ["Batman","Superman"] :: groups

(* Lists of the same kind can be appended using the @ ("append") operator *)
val guest_list = [ "Mom", "Dad" ] @ [ "Aunt", "Uncle" ]

(* This could have been done with the "cons" operator.  It is tricky because the
   left-hand-side must be an element whereas the right-hand-side must be a list
   of those elements. *)
val guest_list = "Mom" :: "Dad" :: [ "Aunt", "Uncle" ]
val guest_list = "Mom" :: ("Dad" :: ("Aunt" :: ("Uncle" :: [])))

(* If you have many lists of the same kind, you can concatenate them all *)
val everyone = List.concat groups  (* [ "Alice", "Bob", "Huey", ... ] *)

(* A list can contain any (finite) number of values *)
val lots = [ 5, 5, 5, 6, 4, 5, 6, 5, 4, 5, 7, 3 ]  (* still just an int list *)

(* Lists can only contain one kind of thing... *)
(* val bad_list = [ 1, "Hello", 3.14159 ] : ??? list *)


(* Tuples, on the other hand, can contain a fixed number of different things *)
val person1 = ("Simon", 28, 3.14159)  (* : string * int * real *)

(* You can even have tuples inside lists and lists inside tuples *)
val likes = [ ("Alice", "ice cream"),
              ("Bob",   "hot dogs"),
              ("Bob",   "Alice") ]     (* : (string * string) list *)

val mixup = [ ("Alice", 39),
              ("Bob",   37),
              ("Eve",   41) ]  (* : (string * int) list *)

val good_bad_stuff =
  (["ice cream", "hot dogs", "chocolate"],
   ["liver", "paying the rent" ])           (* : string list * string list *)


(* Records are tuples with named slots *)

val rgb = { r=0.23, g=0.56, b=0.91 } (* : {b:real, g:real, r:real} *)

(* You don't need to declare their slots ahead of time. Records with
   different slot names are considered different types, even if their
   slot value types match up. For instance... *)

val Hsl = { H=310.3, s=0.51, l=0.23 } (* : {H:real, l:real, s:real} *)
val Hsv = { H=310.3, s=0.51, v=0.23 } (* : {H:real, s:real, v:real} *)

(* ...trying to evaluate `Hsv = Hsl` or `rgb = Hsl` would give a type
   error. While they're all three-slot records composed only of `real`s,
   they each have different names for at least some slots. *)

(* You can use hash notation to get values out of tuples. *)

val H = #H Hsv (* : real *)
val s = #s Hsl (* : real *)

(* Functions! *)
fun add_them (a, b) = a + b    (* A simple function that adds two numbers *)
val test_it = add_them (3, 4)  (* gives 7 *)

(* Larger functions are usually broken into several lines for readability *)
fun thermometer temp =
    if temp < 37
    then "Cold"
    else if temp > 37
         then "Warm"
         else "Normal"

val test_thermo = thermometer 40  (* gives "Warm" *)

(* if-sentences are actually expressions and not statements/declarations.
   A function body can only contain one expression.  There are some tricks
   for making a function do more than just one thing, though. *)

(* A function can call itself as part of its result (recursion!) *)
fun fibonacci n =
    if n = 0 then 0 else                   (* Base case *)
    if n = 1 then 1 else                   (* Base case *)
    fibonacci (n - 1) + fibonacci (n - 2)  (* Recursive case *)

(* Sometimes recursion is best understood by evaluating a function by hand:

 fibonacci 4
   ~> fibonacci (4 - 1) + fibonacci (4 - 2)
   ~> fibonacci 3 + fibonacci 2
   ~> (fibonacci (3 - 1) + fibonacci (3 - 2)) + fibonacci 2
   ~> (fibonacci 2 + fibonacci 1) + fibonacci 2
   ~> ((fibonacci (2 - 1) + fibonacci (2 - 2)) + fibonacci 1) + fibonacci 2
   ~> ((fibonacci 1 + fibonacci 0) + fibonacci 1) + fibonacci 2
   ~> ((1 + fibonacci 0) + fibonacci 1) + fibonacci 2
   ~> ((1 + 0) + fibonacci 1) + fibonacci 2
   ~> (1 + fibonacci 1) + fibonacci 2
   ~> (1 + 1) + fibonacci 2
   ~> 2 + fibonacci 2
   ~> 2 + (fibonacci (2 - 1) + fibonacci (2 - 2))
   ~> 2 + (fibonacci (2 - 1) + fibonacci (2 - 2))
   ~> 2 + (fibonacci 1 + fibonacci 0)
   ~> 2 + (1 + fibonacci 0)
   ~> 2 + (1 + 0)
   ~> 2 + 1
   ~> 3  which is the 4th Fibonacci number, according to this definition

 *)

(* A function cannot change the variables it can refer to.  It can only
   temporarily shadow them with new variables that have the same names.  In this
   sense, variables are really constants and only behave like variables when
   dealing with recursion.  For this reason, variables are also called value
   bindings. An example of this: *)

val x = 42
fun answer(question) =
    if question = "What is the meaning of life, the universe and everything?"
    then x
    else raise Fail "I'm an exception. Also, I don't know what the answer is."
val x = 43
val hmm = answer "What is the meaning of life, the universe and everything?"
(* Now, hmm has the value 42.  This is because the function answer refers to
   the copy of x that was visible before its own function definition. *)


(* Functions can take several arguments by taking one tuples as argument: *)
fun solve2 (a : real, b : real, c : real) =
    ((~b + Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a),
     (~b - Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a))

(* Sometimes, the same computation is carried out several times. It makes sense
   to save and re-use the result the first time. We can use "let-bindings": *)
fun solve2 (a : real, b : real, c : real) =
    let val discr  = b * b - 4.0 * a * c
        val sqr = Math.sqrt discr
        val denom = 2.0 * a
    in ((~b + sqr) / denom,
        (~b - sqr) / denom)
    end


(* Pattern matching is a funky part of functional programming.  It is an
   alternative to if-sentences.  The fibonacci function can be rewritten: *)
fun fibonacci 0 = 0  (* Base case *)
  | fibonacci 1 = 1  (* Base case *)
  | fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)  (* Recursive case *)

(* Pattern matching is also possible on composite types like tuples, lists and
   records. Writing "fun solve2 (a, b, c) = ..." is in fact a pattern match on
   the one three-tuple solve2 takes as argument. Similarly, but less intuitively,
   you can match on a list consisting of elements in it (from the beginning of
   the list only). *)
fun first_elem (x::xs) = x
fun second_elem (x::y::xs) = y
fun evenly_positioned_elems (odd::even::xs) = even::evenly_positioned_elems xs
  | evenly_positioned_elems [odd] = []  (* Base case: throw away *)
  | evenly_positioned_elems []    = []  (* Base case *)

(* When matching on records, you must use their slot names, and you must bind
   every slot in a record. The order of the slots doesn't matter though. *)

fun rgbToTup {r, g, b} = (r, g, b)    (* fn : {b:'a, g:'b, r:'c} -> 'c * 'b * 'a *)
fun mixRgbToTup {g, b, r} = (r, g, b) (* fn : {b:'a, g:'b, r:'c} -> 'c * 'b * 'a *)

(* If called with {r=0.1, g=0.2, b=0.3}, either of the above functions
   would return (0.1, 0.2, 0.3). But it would be a type error to call them
   with {r=0.1, g=0.2, b=0.3, a=0.4} *)

(* Higher order functions: Functions can take other functions as arguments.
   Functions are just other kinds of values, and functions don't need names
   to exist.  Functions without names are called "anonymous functions" or
   lambda expressions or closures (since they also have a lexical scope). *)
val is_large = (fn x => x > 37)
val add_them = fn (a,b) => a + b
val thermometer =
    fn temp => if temp < 37
               then "Cold"
               else if temp > 37
                    then "Warm"
                    else "Normal"

(* The following uses an anonymous function directly and gives "ColdWarm" *)
val some_result = (fn x => thermometer (x - 5) ^ thermometer (x + 5)) 37

(* Here is a higher-order function that works on lists (a list combinator) *)
(* map f l
       applies f to each element of l from left to right, 
       returning the list of results. *)
val readings = [ 34, 39, 37, 38, 35, 36, 37, 37, 37 ]  (* first an int list *)
val opinions = List.map thermometer readings (* gives [ "Cold", "Warm", ... ] *)

(* And here is another one for filtering lists *)
val warm_readings = List.filter is_large readings  (* gives [39, 38] *)

(* You can create your own higher-order functions, too.  Functions can also take
   several arguments by "currying" them. Syntax-wise this means adding spaces
   between function arguments instead of commas and surrounding parentheses. *)
fun map f [] = []
  | map f (x::xs) = f(x) :: map f xs

(* map has type ('a -> 'b) -> 'a list -> 'b list and is called polymorphic. *)
(* 'a is called a type variable. *)


(* We can declare functions as infix *)
val plus = add_them   (* plus is now equal to the same function as add_them *)
infix plus            (* plus is now an infix operator *)
val seven = 2 plus 5  (* seven is now bound to 7 *)

(* Functions can also be made infix before they are declared *)
infix minus
fun x minus y = x - y (* It becomes a little hard to see what's the argument *)
val four = 8 minus 4  (* four is now bound to 4 *)

(* An infix function/operator can be made prefix with 'op' *)
val n = op + (5, 5)   (* n is now 10 *)

(* 'op' is useful when combined with high order functions because they expect
   functions and not operators as arguments. Most operators are really just
   infix functions. *)
(* foldl f init [x1, x2, ..., xn]
       returns
       f(xn, ...f(x2, f(x1, init))...)
       or init if the list is empty. *)
val sum_of_numbers = foldl op+ 0 [1, 2, 3, 4, 5]


(* Datatypes are useful for creating both simple and complex structures *)
datatype color = Red | Green | Blue

(* Here is a function that takes one of these as argument *)
fun say(col) =
    if col = Red then "You are red!" else
    if col = Green then "You are green!" else
    if col = Blue then "You are blue!" else
    raise Fail "Unknown color"

val _ = print (say(Red) ^ "\n")

(* Datatypes are very often used in combination with pattern matching *)
fun say Red   = "You are red!"
  | say Green = "You are green!"
  | say Blue  = "You are blue!"

(* We did not include the match arm `say _ = raise Fail "Unknown color"`
because after specifying all three colors, the pattern is exhaustive
and redundancy is not permitted in pattern matching *)


(* Here is a binary tree datatype *)
datatype 'a btree = Leaf of 'a
                  | Node of 'a btree * 'a * 'a btree (* three-arg constructor *)

(* Here is a binary tree *)
val myTree = Node (Leaf 9, 8, Node (Leaf 3, 5, Leaf 7))

(* Drawing it, it might look something like...

           8
          / \
 leaf -> 9   5
            / \
   leaf -> 3   7 <- leaf
 *)

(* This function counts the sum of all the elements in a tree *)
fun count (Leaf n) = n
  | count (Node (leftTree, n, rightTree)) = count leftTree + n + count rightTree

val myTreeCount = count myTree  (* myTreeCount is now bound to 32 *)


(* Exceptions! *)
(* Exceptions can be raised/thrown using the reserved word 'raise' *)
fun calculate_interest(n) = if n < 0.0
                            then raise Domain
                            else n * 1.04

(* Exceptions can be caught using "handle" *)
val balance = calculate_interest ~180.0
              handle Domain => ~180.0    (* balance now has the value ~180.0 *)

(* Some exceptions carry extra information with them *)
(* Here are some examples of built-in exceptions *)
fun failing_function []    = raise Empty  (* used for empty lists *)
  | failing_function [x]   = raise Fail "This list is too short!"
  | failing_function [x,y] = raise Overflow  (* used for arithmetic *)
  | failing_function xs    = raise Fail "This list is too long!"

(* We can pattern match in 'handle' to make sure
   a specific exception was raised, or grab the message *)
val err_msg = failing_function [1,2] handle Fail _ => "Fail was raised"
                                          | Domain => "Domain was raised"
                                          | Empty  => "Empty was raised"
                                          | _      => "Unknown exception"

(* err_msg now has the value "Unknown exception" because Overflow isn't
   listed as one of the patterns -- thus, the catch-all pattern _ is used. *)

(* We can define our own exceptions like this *)
exception MyException
exception MyExceptionWithMessage of string
exception SyntaxError of string * (int * int)

(* File I/O! *)
(* Write a nice poem to a file *)
fun writePoem(filename) =
    let val file = TextIO.openOut(filename)
        val _ = TextIO.output(file, "Roses are red,\nViolets are blue.\n")
        val _ = TextIO.output(file, "I have a gun.\nGet in the van.\n")
    in TextIO.closeOut(file)
    end

(* Read a nice poem from a file into a list of strings *)
fun readPoem(filename) =
    let val file = TextIO.openIn filename
        val poem = TextIO.inputAll file
        val _ = TextIO.closeIn file
    in String.tokens (fn c => c = #"\n") poem
    end

val _ = writePoem "roses.txt"
val test_poem = readPoem "roses.txt"  (* gives [ "Roses are red,",
                                                 "Violets are blue.",
                                                 "I have a gun.",
                                                 "Get in the van." ] *)

(* We can create references to data which can be updated *)
val counter = ref 0 (* Produce a reference with the ref function *)

(* Assign to a reference with the assignment operator *)
fun set_five reference = reference := 5

(* Read a reference with the dereference operator *)
fun equals_five reference = !reference = 5

(* We can use while loops for when recursion is messy *)
fun decrement_to_zero r = if !r < 0
                          then r := 0
                          else while !r >= 0 do r := !r - 1

(* This returns the unit value (in practical terms, nothing, a 0-tuple) *)

(* To allow returning a value, we can use the semicolon to sequence evaluations *)
fun decrement_ret x y = (x := !x - 1; y)
```

## Further learning

* Install an interactive compiler (REPL), for example
  [Poly/ML](http://www.polyml.org/),
  [Moscow ML](http://mosml.org),
  [SML/NJ](http://smlnj.org/).
* Follow the Coursera course [Programming Languages](https://www.coursera.org/course/proglang).
* Get the book *ML for the Working Programmer* by Larry C. Paulson.
* Use [StackOverflow's sml tag](http://stackoverflow.com/questions/tagged/sml).
---
language: brainfuck
filename: brainfuck-sv.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["Richard Lindberg", "https://github.com/Lidenburg"]
lang: sv-se
---

Brainfuck (ej versaliserat förutom vid ny mening) är ett extremt
minimalistiskt Turing-komplett programmeringsspråk med endast 8 kommandon.

Du kan testa brainfuck i din webbläsare via [brainfuck-visualizer](http://fatiherikli.github.io/brainfuck-visualizer/).

```
Alla karaktärer förutom "><+-.,[]" (inte inkluderat citattecken) ignoreras.

Brainfuck är representerat av ett fält med 30 000 celler initialiserade till
noll och en data pekare som pekar på den valda cellen.

Det finns åtta kommandon:
+ : Ökar värdet av den valda cellen med ett.
- : Minskar värdet av den valda cellen med ett.
> : Flyttar data pekaren till nästa cell (cellen till höger).
< : Flyttar data pekaren till förra cellen (cellen till vänster).
. : Skriver ut ASCII värdet av den valda cellen (t.ex. 65 = 'A').
, : Läser in en karaktär till den valda cellen.
[ : Om värdet vid den valda cellen är noll, hoppa till matchande ].
    Annars fortsätts till nästa instruktion.
] : Om värdet vid den valda cellen är noll, fortsätt till nästa instruktion.
    Annars, gå tillbaka till matchande ].

[ och ] formar en while loop.

Nedan är ett exempel på ett simpelt brainfuck program.

++++++ [ > ++++++++++ < - ] > +++++ .

Programmet skriver ut bokstaven 'A'. Först ökar den värdet av cell #1 till 6.
Cell #1 kommer att användas för att loopa. Sen börjar den loopen (vid '[') och
flyttar till cell #2. Den ökar värdet av cell #2 10 gånger, går tillbaka till
cell #1 och minskar den med 1. Den gör det här 6 gånger (så många iterationer
det tar för cell #1 att bli noll).

Nu är programmet på cell #1, vilket har ett värde av 0 och cell #2 har värdet 60.
Programmet flyttar pekaren till cell #2 och ökar värdet med 5, vilket leder till
att cell #2 har ett värde av 65 (vilket är bokstaven 'A' i ASCII), sedan skriver
den ut cell #2 och bokstaven 'A' skrivs ut till skärmen.


, [ > + < - ] > .

Det här programmet läser en karaktär från användaren och kopierar karaktären
till cell #1. Sedan startas en loop. Pekaren flyttas till cell #2, värder ökas
med ett, pekaren flyttas tillbaka till cell #1 och minskar värdet med ett.
Det här fortsätter tills cell #1 innehåller noll och cell #2 innehåller det
värde som cell #1 innehöll från början. Eftersom att programmet vid slutet av
loopen är på cell #1 flyttas pekaren till cell #2 och sedan skriver den ut
värdet av cell #2 i ASCII.

Värt att komma ihåg är att programmet ovan kan skrivas utan mellanslag också:

,[>+<-]>.


Försök och lista ut vad det här programmet gör:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Programmet tar två nummer som indata, och multiplicerar dem.

Kärnan av det är att den först läser in två tal/bokstäver. Sedan startar
den yttre loopen som beror på cell #1. Sedan går den vidare till cell #2 och
startar den innre loopen som beror på cell #2 och ökar cell #3. Men det uppstår
ett problem: Vid slutet av den innre loopen är cell #2 noll. Vilket betyder att
den inre loopen inte kommer att fungera tills nästa gång. För att lösa det här
problemet ökas också cell #4 som sedan kopieras till cell #2.
Sedan är resultatet i cell #3.
```

Och det är brainfuck. Inte så svårt va? För skojs skull kan du skriva dina egna
brainfuck program, eller skriva en egen brainfuck interpretator i ett annat
språk. interpretatorn är ganska simpel att implementera, men om man är en
masochist, testa att skriva en brainfuck interpretator… i brainfuck.
---
language: json
filename: learnjson-sv.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
  - ["Michael Neth", "https://github.com/infernocloud"]
translators:
  - ["Lari Kovanen", "https://github.com/larkov"]
  - ["Joakim Lahtinen", "https://github.com/VibyJocke"]
lang: sv-se
---

JSON är ett extremt enkelt datautbytesformat. Som [json.org](http://json.org) beskriver så är det lätt för människor att läsa och skriva, och för datorer att tolka och generera.

En bit av JSON måste representera antingen:
* En samling av namn/värde-par (`{ }`). I olika språk kan denna realiseras som ett objekt, struct, dictionary, hash-tabell, nyckellista eller en associativ array.
* En ordnad lista av värden (`[ ]`). I olika språk kan denna realiseras som en array, vektor, lista eller sekvens.

JSON i dess renaste form har inga kommentarer, men de flesta tolkarna accepterar C-stils (`//`, `/* */`) kommentarer. Vissa tolkar tolererar även komman efter sista elementet i en array, eller det sista attributet av ett objekt, men dessa bör undvikas för bättre kompabilitet.

Detta dokument kommer dock att tillämpa 100% giltigt JSON. Lyckligtvis så är resten av dokumentet självförklarande.

Följande datatyper stöds:
* Strängar: `"hello"`, `"\"A quote.\""`, `"\u0abe"`, `"Newline.\n"`
* Nummer: `23`, `0.11`, `12e10`, `3.141e-10`, `1.23e+4`
* Objekt: `{ "key": "value" }`
* Arrayer: `["Values"]`
* Övriga: `true`, `false`, `null`

```json
{
  "nyckel": "värde",

  "nycklar": "måste alltid omslutas med dubbla citationstecken",
  "nummer": 0,
  "strängar": "Alla unicode-tecken (inklusive \"escaping\") är tillåtna.",
  "boolska värden?": true,
  "nullvärden": null,

  "stora tal": 1.2e+100,

  "objekt": {
    "kommentar": "De flesta datastukturerna i JSON kommer i form av objekt.",

    "matris": [0, 1, 2, 3, "Matriser kan innehålla vad som helst.", 5],

    "ytterligare objekt": {
      "kommentar": "Objekten kan vara nästlade."
    }
  },

  "trams": [
    {
      "kaliumkällor": ["bananer"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "alternativ formatering": {
    "kommentar": "kolla på detta!"
  , "kommats position": "spelar ingen roll - så länge det kommer innan värdet"
  , "en kommentar till": "vad fint"
  },



  "blanksteg": "Spelar ingen roll.",



  "det var kort": "Nu är du klar och kan allt vad JSON har att erbjuda."
}
```

## Fortsatt läsning

* [JSON.org](http://json.org/json-sv.html) Allt du kan tänkas vilja veta om JSON, och lite därtill.
---
language: swift
contributors:
  - ["Grant Timmerman", "http://github.com/grant"]
  - ["Christopher Bess", "http://github.com/cbess"]
  - ["Joey Huang", "http://github.com/kamidox"]
  - ["Anthony Nguyen", "http://github.com/anthonyn60"]
  - ["Clayton Walker", "https://github.com/cwalk"]
  - ["Fernando Valverde", "http://visualcosita.xyz"]
  - ["Alexey Nazaroff", "https://github.com/rogaven"]
filename: learnswift.swift
---

Swift is a programming language for iOS and OS X development created by Apple. Designed to coexist with Objective-C and to be more resilient against erroneous code, Swift was introduced in 2014 at Apple's developer conference WWDC. It is built with the LLVM compiler included in Xcode 6+.

The official [Swift Programming Language](https://itunes.apple.com/us/book/swift-programming-language/id881256329) book from Apple is now available via iBooks.

See also Apple's [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/), which has a complete tutorial on Swift.

```swift
// import a module
import UIKit

//
// MARK: Basics
//

// Xcode supports landmarks to annotate your code and lists them in the jump bar
// MARK: Section mark
// MARK: - Section mark with a separator line
// TODO: Do something soon
// FIXME: Fix this code

// In Swift 2, println and print were combined into one print method. Print automatically appends a new line.
print("Hello, world") // println is now print
print("Hello, world", terminator: "") // printing without appending a newline

// variables (var) value can change after being set
// constants (let) value can NOT be changed after being set

var myVariable = 42
let øπΩ = "value" // unicode variable names
let π = 3.1415926
let convenience = "keyword" // contextual variable name
let weak = "keyword"; let override = "another keyword" // statements can be separated by a semi-colon
let `class` = "keyword" // backticks allow keywords to be used as variable names
let explicitDouble: Double = 70
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "some text " + String(myVariable) // String construction
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // String interpolation

// Build Specific values
// uses -D build configuration
#if false
    print("Not printed")
    let buildValue = 3
#else
    let buildValue = 7
#endif
print("Build value: \(buildValue)") // Build value: 7

/*
Optionals are a Swift language feature that either contains a value,
or contains nil (no value) to indicate that a value is missing.
A question mark (?) after the type marks the value as optional.

Because Swift requires every property to have a value, even nil must be
explicitly stored as an Optional value.

Optional<T> is an enum.
*/
var someOptionalString: String? = "optional" // Can be nil
// same as above, but ? is a postfix operator (syntax candy)
var someOptionalString2: Optional<String> = "optional"

if someOptionalString != nil {
    // I am not nil
    if someOptionalString!.hasPrefix("opt") {
        print("has the prefix")
    }

    let empty = someOptionalString?.isEmpty
}
someOptionalString = nil

/*
Trying to use ! to access a non-existent optional value triggers a runtime
error. Always make sure that an optional contains a non-nil value before
using ! to force-unwrap its value.
*/

// implicitly unwrapped optional
var unwrappedString: String! = "Value is expected."
// same as above, but ! is a postfix operator (more syntax candy)
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Value is expected."

// If let structure -
// If let is a special structure in Swift that allows you to check if an Optional rhs holds a value, and in case it does - unwraps and assigns it to the lhs.
if let someOptionalStringConstant = someOptionalString {
    // has `Some` value, non-nil
    if !someOptionalStringConstant.hasPrefix("ok") {
        // does not have the prefix
    }
}

// The nil-coalescing operator ?? unwraps an optional if it contains a non-nil value, or returns a default value.
var someOptionalString: String?
let someString = someOptionalString ?? "abc"
print(someString) // abc

// Swift has support for storing a value of any type.
// For that purposes there is two keywords: `Any` and `AnyObject`
// `AnyObject` == `id` from Objective-C
// `Any` – also works with any scalar values (Class, Int, struct, etc.)
var anyVar: Any = 7
anyVar = "Changed value to a string, not good practice, but possible."
let anyObjectVar: AnyObject = Int(1) as NSNumber

/*
    Comment here

    /*
        Nested comments are also supported
    */
*/

//
// MARK: Collections
//

/*
Array and Dictionary types are structs. So `let` and `var` also indicate
that they are mutable (var) or immutable (let) when declaring these types.
*/

// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
let emptyArray = [String]() // let == immutable
let emptyArray2 = Array<String>() // same as above
var emptyMutableArray = [String]() // var == mutable
var explicitEmptyMutableStringArray: [String] = [] // same as above


// Dictionary
var occupations = [
    "Malcolm": "Captain",
    "kaylee": "Mechanic"
]
occupations["Jayne"] = "Public Relations"
let emptyDictionary = [String: Float]() // let == immutable
let emptyDictionary2 = Dictionary<String, Float>() // same as above
var emptyMutableDictionary = [String: Float]() // var == mutable
var explicitEmptyMutableDictionary: [String: Float] = [:] // same as above


//
// MARK: Control Flow
//

// Condition statements support "," (comma) clauses, which can be used
// to help provide conditions on optional values.
// Both the assignment and the "," clause must pass.
let someNumber = Optional<Int>(7)
if let num = someNumber, num > 3 {
    print("num is greater than 3")
}

// for loop (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
    if value == 1 {
        print("One!")
    } else {
        print("Not one!")
    }
}

// for loop (dictionary)
var dict = ["one": 1, "two": 2]
for (key, value) in dict {
    print("\(key): \(value)")
}

// for loop (range)
for i in -1...shoppingList.count {
    print(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// use ..< to exclude the last number

// while loop
var i = 1
while i < 1000 {
    i *= 2
}

// repeat-while loop
repeat {
    print("hello")
} while 1 == 2

// Switch
// Very powerful, think `if` statements with syntax candy
// They support String, object instances, and primitives (Int, Double, etc)
let vegetable = "red pepper"
switch vegetable {
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let localScopeValue where localScopeValue.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(localScopeValue)?"
default: // required (in order to cover all possible input)
    let vegetableComment = "Everything tastes good in soup."
}

//
// MARK: Functions
//

// Functions are a first-class type, meaning they can be nested
// in functions and can be passed around

// Function with Swift header docs (format as Swift-modified Markdown syntax)

/**
A greet operation

- A bullet in docs
- Another bullet in the docs

- Parameter name	: A name
- Parameter day	: A day
- Returns : A string containing the name and day value.
*/
func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet(name: "Bob", day: "Tuesday")

// similar to above except for the function parameter behaviors
func greet2(name: String, externalParamName localParamName: String) -> String {
    return "Hello \(name), the day is \(localParamName)"
}
greet2(name: "John", externalParamName: "Sunday")

// Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// Ignore Tuple (or other) values by using _ (underscore)
let (_, price1, _) = pricesTuple // price1 == 3.69
print(price1 == pricesTuple.1) // true
print("Gas price: \(price)")

// Labeled/named tuple params
func getGasPrices2() -> (lowestPrice: Double, highestPrice: Double, midPrice: Double) {
    return (1.77, 37.70, 7.37)
}
let pricesTuple2 = getGasPrices2()
let price2 = pricesTuple2.lowestPrice
let (_, price3, _) = pricesTuple2
print(pricesTuple2.highestPrice == pricesTuple2.1) // true
print("Highest gas price: \(pricesTuple2.highestPrice)")

// guard statements
func testGuard() {
    // guards provide early exits or breaks, placing the error handler code near the conditions.
    // it places variables it declares in the same scope as the guard statement.
    guard let aNumber = Optional<Int>(7) else {
        return
    }

    print("number is \(aNumber)")
}
testGuard()

// Variadic Args
func setup(numbers: Int...) {
    // it's an array
    let _ = numbers[0]
    let _ = numbers.count
}

// Passing and returning functions
func makeIncrementer() -> ((Int) -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

// pass by ref
func swapTwoInts(a: inout Int, b: inout Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(a: &someIntA, b: &someIntB)
print(someIntB) // 7


//
// MARK: Closures
//
var numbers = [1, 2, 6]

// Functions are special case closures ({})

// Closure example.
// `->` separates the arguments and return type
// `in` separates the closure header from the closure body
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})

// When the type is known, like above, we can do this
numbers = numbers.map({ number in 3 * number })
// Or even this
//numbers = numbers.map({ $0 * 3 })

print(numbers) // [3, 6, 18]

// Trailing closure
numbers = numbers.sorted { $0 > $1 }

print(numbers) // [18, 6, 3]

//
// MARK: Structures
//

// Structures and classes have very similar capabilities
struct NamesTable {
    let names: [String]

    // Custom subscript
    subscript(index: Int) -> String {
        return names[index]
    }
}

// Structures have an auto-generated (implicit) designated initializer
let namesTable = NamesTable(names: ["Me", "Them"])
let name = namesTable[1]
print("Name is \(name)") // Name is Them

//
// MARK: Error Handling
//

// The `Error` protocol is used when throwing errors to catch
enum MyError: Error {
    case BadValue(msg: String)
    case ReallyBadValue(msg: String)
}

// functions marked with `throws` must be called using `try`
func fakeFetch(value: Int) throws -> String {
    guard 7 == value else {
        throw MyError.ReallyBadValue(msg: "Some really bad value")
    }

    return "test"
}

func testTryStuff() {
    // assumes there will be no error thrown, otherwise a runtime exception is raised
    let _ = try! fakeFetch(value: 7)

    // if an error is thrown, then it proceeds, but if the value is nil
    // it also wraps every return value in an optional, even if its already optional
    let _ = try? fakeFetch(value: 7)

    do {
        // normal try operation that provides error handling via `catch` block
        try fakeFetch(value: 1)
    } catch MyError.BadValue(let msg) {
        print("Error message: \(msg)")
    } catch {
        // must be exhaustive
    }
}
testTryStuff()

//
// MARK: Classes
//

// Classes, structures and its members have three levels of access control
// They are: internal (default), public, private

public class Shape {
    public func getArea() -> Int {
        return 0
    }
}

// All methods and properties of a class are public.
// If you just need to store data in a
// structured object, you should use a `struct`

internal class Rect: Shape {
    var sideLength: Int = 1

    // Custom getter and setter property
    private var perimeter: Int {
        get {
            return 4 * sideLength
        }
        set {
            // `newValue` is an implicit variable available to setters
            sideLength = newValue / 4
        }
    }

    // Computed properties must be declared as `var`, you know, cause' they can change
    var smallestSideLength: Int {
        return self.sideLength - 1
    }

    // Lazily load a property
    // subShape remains nil (uninitialized) until getter called
    lazy var subShape = Rect(sideLength: 4)

    // If you don't need a custom getter and setter,
    // but still want to run code before and after getting or setting
    // a property, you can use `willSet` and `didSet`
    var identifier: String = "defaultID" {
        // the `willSet` arg will be the variable name for the new value
        willSet(someIdentifier) {
            print(someIdentifier)
        }
    }

    init(sideLength: Int) {
        self.sideLength = sideLength
        // always super.init last when init custom properties
        super.init()
    }

    func shrink() {
        if sideLength > 0 {
            sideLength -= 1
        }
    }

    override func getArea() -> Int {
        return sideLength * sideLength
    }
}

// A simple class `Square` extends `Rect`
class Square: Rect {
    convenience init() {
        self.init(sideLength: 5)
    }
}

var mySquare = Square()
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4

// cast instance
let aShape = mySquare as Shape

// compare instances, not the same as == which compares objects (equal to)
if mySquare === mySquare {
    print("Yep, it's mySquare")
}

// Optional init
class Circle: Shape {
    var radius: Int
    override func getArea() -> Int {
        return 3 * radius * radius
    }

    // Place a question mark postfix after `init` is an optional init
    // which can return nil
    init?(radius: Int) {
        self.radius = radius
        super.init()

        if radius <= 0 {
            return nil
        }
    }
}

var myCircle = Circle(radius: 1)
print(myCircle?.getArea())    // Optional(3)
print(myCircle!.getArea())    // 3
var myEmptyCircle = Circle(radius: -1)
print(myEmptyCircle?.getArea())    // "nil"
if let circle = myEmptyCircle {
    // will not execute since myEmptyCircle is nil
    print("circle is not nil")
}


//
// MARK: Enums
//

// Enums can optionally be of a specific type or on their own.
// They can contain methods like classes.

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func getIcon() -> String {
        switch self {
        case .Spades: return "♤"
        case .Hearts: return "♡"
        case .Diamonds: return "♢"
        case .Clubs: return "♧"
        }
    }
}

// Enum values allow short hand syntax, no need to type the enum type
// when the variable is explicitly declared
var suitValue: Suit = .Hearts

// String enums can have direct raw value assignments
// or their raw values will be derived from the Enum field
enum BookName: String {
    case John
    case Luke = "Luke"
}
print("Name: \(BookName.John.rawValue)")

// Enum with associated Values
enum Furniture {
    // Associate with Int
    case Desk(height: Int)
    // Associate with String and Int
    case Chair(String, Int)

    func description() -> String {
        switch self {
        case .Desk(let height):
            return "Desk with \(height) cm"
        case .Chair(let brand, let height):
            return "Chair of \(brand) with \(height) cm"
        }
    }
}

var desk: Furniture = .Desk(height: 80)
print(desk.description())     // "Desk with 80 cm"
var chair = Furniture.Chair("Foo", 40)
print(chair.description())    // "Chair of Foo with 40 cm"


//
// MARK: Protocols
//

// `protocol`s can require that conforming types have specific
// instance properties, instance methods, type methods,
// operators, and subscripts.

protocol ShapeGenerator {
    var enabled: Bool { get set }
    func buildShape() -> Shape
}

// Protocols declared with @objc allow optional functions,
// which allow you to check for conformance. These functions must be
// marked with @objc also.
@objc protocol TransformShape {
    @objc optional func reshape()
    @objc optional func canReshape() -> Bool
}

class MyShape: Rect {
    var delegate: TransformShape?

    func grow() {
        sideLength += 2

        // Place a question mark after an optional property, method, or
        // subscript to gracefully ignore a nil value and return nil
        // instead of throwing a runtime error ("optional chaining").
        if let reshape = self.delegate?.canReshape?(), reshape {
            // test for delegate then for method
            self.delegate?.reshape?()
        }
    }
}


//
// MARK: Other
//

// `extension`s: Add extra functionality to an already existing type

// Square now "conforms" to the `CustomStringConvertible` protocol
extension Square: CustomStringConvertible {
    var description: String {
        return "Area: \(self.getArea()) - ID: \(self.identifier)"
    }
}

print("Square: \(mySquare)")

// You can also extend built-in types
extension Int {
    var customProperty: String {
        return "This is \(self)"
    }

    func multiplyBy(num: Int) -> Int {
        return num * self
    }
}

print(7.customProperty) // "This is 7"
print(14.multiplyBy(num: 3)) // 42

// Generics: Similar to Java and C#. Use the `where` keyword to specify the
//   requirements of the generics.

func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
    for (index, value) in array.enumerated() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let foundAtIndex = findIndex(array: [1, 2, 3, 4], valueToFind: 3)
print(foundAtIndex == 2) // true

// Operators:
// Custom operators can start with the characters:
//      / = - + * % < > ! & | ^ . ~
// or
// Unicode math, symbol, arrow, dingbat, and line/box drawing characters.
prefix operator !!!

// A prefix operator that triples the side length when used
prefix func !!! (shape: inout Square) -> Square {
    shape.sideLength *= 3
    return shape
}

// current value
print(mySquare.sideLength) // 4

// change side length using custom !!! operator, increases size by 3
!!!mySquare
print(mySquare.sideLength) // 12

// Operators can also be generics
infix operator <->
func <-><T: Equatable> (a: inout T, b: inout T) {
    let c = a
    a = b
    b = c
}

var foo: Float = 10
var bar: Float = 20

foo <-> bar
print("foo is \(foo), bar is \(bar)") // "foo is 20.0, bar is 10.0"
```
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
    - ["Geoffrey Liu", "https://github.com/g-liu"]
    - ["Connor Shea", "https://github.com/connorshea"]
    - ["Deepanshu Utkarsh", "https://github.com/duci9y"]
translators:
    - ["Rasendran Kirushan", "https://github.com/kirushanr"]
filename: learncss-ta.css
lang: in-ta
---


இணையத்தின்    ஆரம்ப காலத்தில்  முழுமையாக உரைகளை மட்டுமே கொண்டிருந்தன. 
ஆனால் உலாவிகளில் கொண்டு வரப்பட்ட மாற்றங்களில் முழுமையான காட்சிபடுத்தல்களுடன்
கூடிய இணையதளங்கள் உருவாகின.


CSS ஆனது HTML மற்றும் அதன் அழகுபடுத்கூடிய காரணிகளையும் வேறுபடுத்த உதவியது.

ஒரு html இல் உள்ள உறுப்புகளை(elements) வெவ்வேறு வகையான காட்சி பண்புகளை வழங்க உதவுகிறது.

இந்த வழிகாட்டி CSS2 உக்கு எழுதப்பட்டுள்ளது, இருப்பினும் தற்போது CSS 3 வேகமாக பிரபல்யமாகி வருகிறது.

**குறிப்பு:**
CSS ஆனது முற்று முழுதாக visual(காட்சி)  மாற்றங்களை தருவதால் அதை நீங்கள் முயற்சிக்க
இதை    உபயோகபடுத்தலாம்  [dabblet](http://dabblet.com/).
இந்த வழிகாட்டியின் பிரதான நோக்கம் CSS இன்  syntax மற்றும் மேலும் சில வழிமுறைகளை
உங்களுக்கு கற்று தருவதாகும்

```css
/* css இல் குறிப்புகளை இப்படி இடலாம் */

/* ####################
   ## SELECTORS
   #################### */

/* ஒரு HTML பக்கத்தில் இருக்கும் உறுப்பை நாம் selector மூலம் தெரிவு செய்யலாம்
selector { property: value; /* more properties...*/ }

/*
கிழே ஒரு உதாரணம் காட்டப்பட்டுள்ளது:

<div class='class1 class2' id='anID' attr='value' otherAttr='en-us foo bar' />
*/

/* நீங்கள் அந்த உறுப்பை அதன் CSS class மூலம் தெரியலாம் */
.class1 { }

/* அல்லது இவ்வாறு  இரண்டு  class மூலம் தெரியலாம்! */
.class1.class2 { }

/* அல்லது  அதன்  பெயரை பாவித்து தெரியலாம் */
div { }

/* அல்லது  அதன் id ஐ  பயன்படுத்தி  தெரியலாம்*/
#anID { }

/* அல்லது ஒரு  உறுப்பின்   பண்பு ஒன்றின்  மூலம்! */
[attr] { font-size:smaller; }

/* அல்லது அந்த  பண்பு ஒரு  குறிப்பிட்ட  பெறுமானத்தை கொண்டு இருப்பின் */
[attr='value'] { font-size:smaller; }

/* ஒரு  பெறுமதியுடன் ஆரம்பமாகும் போது (CSS 3) */
[attr^='val'] { font-size:smaller; }

/* அல்லது  ஒரு பெறுமதியுடன் முடிவடையும் போது  (CSS 3) */
[attr$='ue'] { font-size:smaller; }

/*  அல்லது  காற்புள்ளியால் பிரிக்கப்பட்ட  பெறுமானங்களை கொண்டு இருப்பின் */
[otherAttr~='foo'] { }
[otherAttr~='bar'] { }

/* அல்லது  `-` பிரிக்கப்பட்ட  பெறுமானங்களை கொண்டு இருப்பின், உ.ம்:-, "-" (U+002D) */
[otherAttr|='en'] { font-size:smaller; }


/* நாம் இரண்டு selectors ஐ ஒன்றாக உபயோகித்தும் ஒரு உறுப்பை அணுக முடியும்  , 
அவற்றுக்கு இடயே இடைவெளி காணப்படகூடாது
 */
div.some-class[attr$='ue'] { }

/*அல்லது ஒரு உறுப்பினுள் இருக்கும் இன்னொரு உறுப்பை (child element) அணுக */
div.some-parent > .class-name { }

/* ஒரு  ஒரு  பிரதான உறுப்பில் உள்ள உப உறுப்புகளை அணுக*/
div.some-parent .class-name { }

/* மேலே  குறிபிட்ட அணுகுமுறையில் இடைவெளி காணப்படாது விடின் 
	அந்த selector வேலை செய்யாது
 */
div.some-parent.class-name { }

/* அல்லது ஒரு உறுப்புக்கு அடுத்துள்ள  */
.i-am-just-before + .this-element { }

/* or அல்லது அதற்கு முந்தய உறுப்பின்  மூலம் */
.i-am-any-element-before ~ .this-element { }

/* 
	சில selectors ஐ pseudo class மூலம் அணுக முடியும் , எப்போது எனில் அவை
	குறித்த ஒரு நிலையில் இருக்கும் போது ஆகும்
   */

/* உதாரணமாக நாம் ஒரு  உறுப்பின் மீதாக cursor ஐ நகர்த்தும் போது */
selector:hover { }

/* அல்லது ஒரு
பார்வையிட்ட இணைப்பு */
selector:visited { }

/* அல்லது ஒரு  பார்வையிடபடாத இணைப்பு */   
selected:link { }

/* அல்லது  ஒரு element ஐ  focus செய்யும் போது */
selected:focus { }

/* 
	எல்லா elementகளையும் ஒரே நேரத்தில் அணுக `*`
*/
* { } /* all elements */
.parent * { } /* all descendants */
.parent > * { } /* all children */

/* ####################
   ## பண்புகள்
   #################### */

selector {
    
    /*  நீளத்தின் அலகுகள் absolute அல்லது relative ஆக இருக்கலாம். */
    
    /* Relative units */
    width: 50%;       /* percentage of parent element width */
    font-size: 2em;   /* multiples of element's original font-size */
    font-size: 2rem;  /* or the root element's font-size */
    font-size: 2vw;   /* multiples of 1% of the viewport's width (CSS 3) */
    font-size: 2vh;   /* or its height */
    font-size: 2vmin; /* whichever of a vh or a vw is smaller */
    font-size: 2vmax; /* or greater */
    
    /* Absolute units */
    width: 200px;     /* pixels */
    font-size: 20pt;  /* points */
    width: 5cm;       /* centimeters */
    min-width: 50mm;  /* millimeters */
    max-width: 5in;   /* inches */
    
	
    /* Colors */
    color: #F6E;                 /* short hex format */
    color: #FF66EE;              /* long hex format */
    color: tomato;               /* a named color */
    color: rgb(255, 255, 255);   /* as rgb values */
    color: rgb(10%, 20%, 50%);   /* as rgb percentages */
    color: rgba(255, 0, 0, 0.3); /* as rgba values (CSS 3) Note: 0 < a < 1 */
    color: transparent;          /* equivalent to setting the alpha to 0 */
    color: hsl(0, 100%, 50%);    /* as hsl percentages (CSS 3) */
    color: hsla(0, 100%, 50%, 0.3); /* as hsla percentages with alpha */
    
    /* Images as backgrounds of elements */
    background-image: url(/img-path/img.jpg); /* quotes inside url() optional */
    
    /* Fonts */
    font-family: Arial;
    /* if the font family name has a space, it must be quoted */
    font-family: "Courier New";
    /* if the first one is not found, the browser uses the next, and so on */
    font-family: "Courier New", Trebuchet, Arial, sans-serif;
}
```

## Usage

ஒரு css file ஐ  save செய்ய `.css`.

```xml
<!-- உங்கள் css file ஐ  <head>. உள் குறிப்பிட வேண்டும் 
     சரியான முறையை பார்க்க  http://stackoverflow.com/questions/8284365 -->
<link rel='stylesheet' type='text/css' href='path/to/style.css' />

<!-- நீங்கள் css ஐ html உள்ளும் எழுத முடியும் -->
<style>
   a { color: purple; }
</style>

<!-- அல்லது css ஐ நேரடியாக அந்த element இல் எழுத முடியும் -->
<div style="border: 1px solid red;">
</div>
```

## Precedence அல்லது Cascade

ஒரு element ஆனது ஒன்றுக்கு மேற்பட்ட selectors மூலம் அணுகபடலாம் ,இவ்வாறான சந்தர்பங்களில் 
ஒரு குறிபிட்ட விதிமுறையை பின்பற்றுகிறது இது cascading என அழைக்கபடுகிறது, அதனால் தன
இது  Cascading Style Sheets என அழைக்கபடுகிறது.


கிழே தரப்பட்டுள்ள css இன் படி:

```css
/* A */
p.class1[attr='value']

/* B */
p.class1 { }

/* C */
p.class2 { }

/* D */
p { }

/* E */
p { property: value !important; }
```

அத்துடன் கிழே தரப்பட்டுள்ள கட்டமைப்பின்படியும்:

```xml
<p style='/*F*/ property:value;' class='class1 class2' attr='value' />
```


css முன்னுரிமை பின்வருமாறு 
* `E` இதுவே அதிக முக்கியத்துவம் வாய்ந்தது காரணம் இது `!important` பயன்படுத்துகிறது. இதை பயன்படுத்துவதை தவிர்க்கவும்
* `F` இது இரண்டாவது காரணம் இது inline style.
* `A` இது  மூன்றவதாக வருகிறது, காரணம் இது மூன்று காரணிகளை குறிக்கிறது : element(உறுப்பு) பெயர் `p`, அதன் class `class1`, an அதன் பண்பு(attribute) `attr='value'`.
* `C` இது அடுத்த நிலையில் உள்ளது கடைசி.
* `B` இது அடுத்தது.
* `D` இதுவே கடைசி .

## css அம்சங்களின் பொருந்தகூடிய தன்மை

பெரும்பாலான css 2 வின் அம்சங்கள் எல்லா உலாவிகளிலும் , கருவிகளிலும் உள்ளன. ஆனால் முன்கூட்டியே அந்த அம்சங்களை பரிசோதிப்பது நல்லது.

## வளங்கள்

* To run a quick compatibility check, [CanIUse](http://caniuse.com).
* CSS Playground [Dabblet](http://dabblet.com/).
* [Mozilla Developer Network's CSS documentation](https://developer.mozilla.org/en-US/docs/Web/CSS)
* [Codrops' CSS Reference](http://tympanus.net/codrops/css_reference/)

## மேலும் வாசிக்க

* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [Selecting elements using attributes](https://css-tricks.com/almanac/selectors/a/attribute/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
* [SASS](http://sass-lang.com/) and [LESS](http://lesscss.org/) for CSS pre-processing
* [CSS-Tricks](https://css-tricks.com)
---
language: javascript
contributors:
    - ['Adam Brenecki', 'http://adam.brenecki.id.au']
    - ['Ariel Krakowski', 'http://www.learneroo.com']
translators:
  - ["Rasendran Kirushan", "https://github.com/kirushanr"]
filename: javascript-ta.js
lang: in-ta
---

javascript 1995 ஆம்  ஆண்டு Netscape இல் பணிபுரிந்த Brendan Eich
என்பவரால் உருவாக்கபட்டது.ஆரம்பத்தில் மிகவும் எளிமையான
ஸ்க்ரிப்டிங் மொழியாக இணையதளங்களில் பயன்படுத்தபட்டது.
இது ஜாவா (java ) வில் உருவாக்கபட்ட மிகவும் சிக்கலான இணைய செயலிகளுக்கு 
உதவும் முகமாக உருவாக்கபட்டது. எனினும் இணையதள பக்கங்களில் இதன் முழுதான பயன்பாடு 
மற்றும் உலாவிகளில்   பயன்படுத்த கூடிய வகையில் இருந்தமையாலும் Java வை விட 
இணையதளகளின் முகப்பு உருவாக்கத்தில் இன்றளவில் முன்னிலை பெற்றுள்ளது.

உலாவிகளுக்கு மட்டும் மட்டுபடுத்தபடவில்லை , Node.js மூலமாக 
மிகவும் பிரபல்யமடைந்து வருகின்றது , உதாரணமாக கூகிள்க்ரோம் உலாவியின் 
V8 JavaScript engine Node .js உதவியுடன் இயங்குகிறது .

உங்கள் கருத்துக்கள் மிகவும் வரவேற்கபடுகின்றன , என்னுடன் தொடர்புகொள்ள 
[@adambrenecki](https://twitter.com/adambrenecki), or
[adam@brenecki.id.au](mailto:adam@brenecki.id.au).

```js
// குறிப்புக்கள் C நிரலாக்கத்தை ஒத்தது .ஒரு வரி குறிப்புக்கள்  "//" குறியீடுடன் ஆரம்பமாகும் 

/* பலவரி குறிப்புக்கள் "/*" ஆரம்பமாகி "/*" இல் முடிவடையும்  */

// ஒரு கூற்று முற்றுபெற செய்ய ; இடல் வேண்டும் .
doStuff();

// ...ஆனால் அரைபுள்ளி இட வேண்டும் என்று அவசியம் இல்லை ஏன்  எனில் 
// ஒரு வரி புதிதாக இடப்படும் போது அரைபுள்ளிகள் தானாகவே இடப்படும் ஆனால் சில தருணங்களை தவிர .
doStuff()

// ஆனால் அவ்வாறான தருணங்கள் எதிர்பாராத முடிவுகளை தரலாம் 

// எனவே நாம் தொடர்ந்து ஒரு கூற்று நிறைவடையும் போது அரைபுள்ளி ஒன்றை இடுவோம் .

///////////////////////////////////
// 1. எண்கள்(Number) ,சரம் (String),செயற்குறிகள்(Operators) 

// JavaScript ஒரே ஒரு எண்வகை காணப்படுகிறது  தசமி  (which is a 64-bit IEEE 754 double).
// தசமி எண்வகை  (Doubles) 2^ 52 வரை சேமிக்க கூடியது
// முழு எண்வகையின் 9✕10¹⁵ சேமிக்க போதுமானது .
3; // = 3
1.5; // = 1.5

// அடிப்படை கணித பொறிமுறைகள் 
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// வகுத்தல்
5 / 2; // = 2.5


//bitwise பொறிமுறையை உபயோகிக்கும் போது 
//உங்கள் தசம எண்ணின் பெறுமானமானது ஒரு நேர் அல்லது மறை அல்லது பூசியமாகவுள்ள முழு எண்ணாக 
//மாற்றம் பெறுகிறது  இது 32 இருமம்(bit) வரை செல்லலாம் 

1 << 2; // = 4

// நிரலாக்கத்தில் செயலியை அமுல்படுத்தும் வரிசைமுறையில் அடைப்பு குறிக்கு முன்னிலை வழங்கபடுகிறது 
(1 + 3) * 2; // = 8

// மெய் எண்  அல்லாத மூன்றுபெறுமானங்கள் உள்ளன :
Infinity; // result of e.g. 1/0
-Infinity; // result of e.g. -1/0
NaN; // result of e.g. 0/0, இது எண்  அல்ல என்பதை  குறிக்கும் 

// தர்க ரீதியில் ஆன கட்டமைப்பு காணப்படுகிறது .
true;
false;

// சரம் (string) ' அல்லது "  குறியீட்டினால்  உருவாக்கபடுகிறது 
'abc';
"Hello, world";

// ஒரு boolean பெறுமானத்தின் எதிர்மறை பெறுமானத்தை பெற ! குறியீடு பயன்படுத்தபடுகிறது 
!true; // = false
!false; // = true

//  சமமா என பார்க்க  ===
1 === 1; // = true
2 === 1; // = false

// சமனற்றவையா  என பார்க்க  !==
1 !== 1; // = false
2 !== 1; // = true

// மேலும் சில ஒப்பீடுகள் 
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// இரண்டு சரங்களை(Strings) ஒன்றாக இணைப்பதற்கு +
"Hello " + "world!"; // = "Hello world!"

// இரண்டு மாறிகளை/பெறுமானங்களை  ஒப்பிட  < and >
"a" < "b"; // = true

// இரண்டு பெறுமானங்கள் / மாறிகள்  ஒரேவகையை சேர்ந்தவையா என பார்க்க 
"5" == 5; // = true
null == undefined; // = true

// ...இல்லாவிடின்  ===
"5" === 5; // = false
null === undefined; // = false 

// ...கிழே உள்ள கூற்றுகள் எதிர்பாராத 
வெளியீடுகளை தரலாம் ...
13 + !0; // 14
"13" + !0; // '13true'

// ஒரு சரத்தில்(string ) உள்ள எழுத்தை பெற  `charAt`
"This is a string".charAt(0);  // = 'T'


//... ஒரு சரத்தை(string )  சொற்களாக பிரிக்க (substring) `substring
"Hello world".substring(0, 5); // = "Hello"

// `length` ஒரு சரத்தில்(string) உள்ள சொற்களின் எண்ணிக்கை அல்லது நீளத்தை(length)அறிய 
"Hello".length; // = 5

// `null` மற்றும்  `undefined` இரு பெறுமானங்கள் உள்ளன  .
null;      // மதிப்பு அற்ற ஒரு பெறுமானத்தை குறிக்கும் 
undefined; // பெறுமானம் இன்னும் நிர்ணயிக்க படவில்லை என்பதை குறிக்கும்  (
           // `undefined` இருப்பினும் இதுவும் ஒரு பெறுமானமாக கருதபடுகிறது )

// ஆகியன  தர்க்க ரீதியாக பிழையானவை(false) , மற்றவை யாவும் சரியானவை (true).
// 0 மானது பிழையை (false) குறிக்கும்  "0" சரியை (true) குறிக்கும் எனினும் 0 == "0".

///////////////////////////////////
// 2. மாறிகள்  (Variables),அணிகள் (Arrays) மற்றும் பொருட்கள் (Objects)

// மாறிகளை உருவாக்க `var ` என்னும் குறியீட்டு சொல் (keyword ) பயன்படுகிறது .
//உருவாக்கப்படும் மாறிகள் எந்த வகையை சார்ந்தன என்பதை  JavaScript 
//தானாகவே நிர்ணயிக்கும் . மாறிக்கு  ஒரு பெறுமானத்தை வழங்க  `=` பாவிக்க 
var someVar = 5;

// //நீங்கள் மாறிகளை  நிறுவ  'var' குறியீட்டு சொல்லை பயன்படுத்தா விடினும் 
//அது தவறில்லை ...
someOtherVar = 10;

// ...ஆனால் நீங்கள் நிறுவிய மாறி(variable) எல்லா உங்கள் ப்ரோக்ராம் இன் சகல இடங்களிலும் 
//அணுக கூடியதாய் அமையும் , இல்லாவிடின் அது ஒரு குறிபிட்ட இடத்திற்கு மட்டும் 
//மட்டுபடுத்தபடும் .

//பெறுமானம் வழங்கபடாத மாறிகளுக்கு ,இயல்பாக/தானாக undefined என்ற பெறுமானம் 
//வழங்கப்படும் 
var someThirdVar; // = undefined

// மாறிகளில் கணித செயல்பாடுகளை நடத்த சுருக்கெழுத்து முறைகள் காணப்படுகின்றன :
someVar += 5; // இது  someVar = someVar + 5; ஐ  ஒத்தது someVar இன் பெறுமானம் இப்போது  10
someVar *= 10; //  someVar இன் பெறுமானம் இப்போது  100

//மிகவும் சுருக்கமான சுருகேழுத்து முறை கூட்டல்  அல்லது  கழித்தல் செயன்முறையை 
//மேற்கொள்ள    
someVar++; // someVar இன் பெறுமானம் இப்போது is 101
someVar--; // someVar இன் பெறுமானம் இப்போது 100

// அணிகள்(Arrays) எல்லாவகையான  பெறுமானங்களையும் உள்ளடக்க கூடியது 
var myArray = ["Hello", 45, true];

// அணிகள்(Arrays) உறுப்பினர்கள் சதுர அடைப்புக்குறிக்குள் அதன் தான இலக்கத்தை கொண்டு 
//அணுகமுடியும் .
// அணிகளில் உள்ள உறுப்புகள் 0 இருந்து  ஆரம்பமாகும் .
myArray[1]; // = 45

// அணிகள் உள்ள உறுப்புகளை மாற்றமுடியும்  அத்துடன் உறுப்புகளின் எண்ணிக்கையும் மாறலாம் .
myArray.push("World");
myArray.length; // = 4

// அணியில்(Array)  ஒரு குறிப்பிட்ட இடத்தில உள்ள பெறுமானத்தை மாற்ற .
myArray[3] = "Hello";

// JavaScript's பொருள் (objects) அகராதியை ஒத்தன  
// ஒழுங்கு படுத்த படாத சேகரிப்பு (collection) ஆகும் இதில் ஒரு சாவியும்(key) 
//அதுக்குரிய பெறுமானமும்(value) காணப்படும் .
var myObj = {key1: "Hello", key2: "World"};

// விசைகள் சரங்களை, ஆனால் அவர்கள் சரியான என்றால் மேற்கோள் அவசியம் இல்லை
//சாவிகளை உ.ம் : "key" என நிறுவலாம் ஆனால் , மேற்கோள்  ஆனது சாவி முன்பே நிறுவபட்டிருப்பின்
//அவசியம் இல்லை   
// சாவிகளுக்குரிய பெறுமானங்கள் எந்த வகையாகவும் இருக்கலாம் 
var myObj = {myKey: "myValue", "my other key": 4};

//பொருள் பண்புகளை சதுர அடைப்புக்குறிக்குள் அதன் சாவியின் பெயரை (key) கொண்டு 
//அணுகமுடியும் ,
myObj["my other key"]; // = 4

// ... அல்லது புள்ளி குறியீட்டை பயன்படுத்தி ,சாவியின் (key is a valid identifier)
//பெயர் மூலம் அணுக முடியும் 
myObj.myKey; // = "myValue"

// பொருட்கள்(ஒப்ஜெக்ட்ஸ்) மாற்றபடகூடியான சாவிகளின் பெறுமதிகளை மாற்ற முடியும் அத்துடன் புதிய 
//சாவிகளை(keys) இடவும் முடியும் 
myObj.myThirdKey = true;

//பெறுமதி வரையறுக்கபடாத ஒரு சாவியினை அணுகும் போது 
//அது வெளியிடும் பெறுமதி `undefined`.
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. தர்க்கம் மற்றும் கட்டுப்பாட்டு கட்டமைப்பு

// கீழே காட்டப்பட்டுள்ள தொடரியல் ஜாவா வை ஒத்தது 

// The `if` ஒரு குறித்த தர்க்கம் சரியாயின் 
//அல்லது என்ற வடிவமைப்பை 
var count = 1;
if (count == 3){
    // count  இன் பெறுமானம் 3 சமமா என பார்க்கபடுகிறது 
} else if (count == 4){
    // count  இன் பெறுமானம் 4க்கு  சமமா என பார்க்கபடுகிறது 
} else {
    // count ஆனது 3  அல்ல  4 அல்ல  எனின் 
}

// ஒரு குறிப்பிட்ட  ஒப்பீடு உண்மையாக இருக்கும் வரை  `while`.
while (true){
    // இந்த இருக்கும் கூற்றுகள் முடிவிலி தடவை மறுபடி செயற்படுத்தப்படும் !
}

// while போல் அல்லாது do-while ,அவை ஒரு தடவையேனும் அதனுள் உள்ள கூற்றுகள் செயற்படுத்தபடும் 
var input;
do {
    input = getInput();
} while (!isValid(input))

// for (loop /சுற்று ) C , ஜாவாவை ஒத்தது 
//மாறிக்கு பெறுமானத்தை  வழங்கல் , மாறியானது தர்க்கத்தை பூர்த்தி செய்கிறதா என பார்த்தல் ,
//சுற்றுக்குள் இருக்கும் கூற்றை செயற்படுதல்  

for (var i = 0; i < 5; i++){
    // இந்த சுற்று 5 தடவைகள் தொடர்ந்து செயற்படுத்தபடும் 
}

//for /In  சுற்றுகள் prototype சங்கிலியில் உள்ள சகல காரணிகள் ஊடகவும் செல்லும் 
var description = "";
var person = {fname:"Paul", lname:"Ken", age:18}; 
for (var x in person){
    description += person[x] + " ";
}

//ஒரு பொருளில் (Object) இடப்பட்ட பண்புகளை (properties) கருத்தில் கொள்ளும் போது
//குறிப்பிட்ட பண்புகளை அந்த Object கொண்டுள்ளதா என பார்க்க  
var description = "";
var person = {fname:"Paul", lname:"Ken", age:18}; 
for (var x in person){
    if (person.hasOwnProperty(x)){
        description += person[x] + " ";
    }
}

//for /in  ஆனது அணியில் உள்ள பண்புகள் ஒழுங்குபடுத்தப்பட்டவிதம் முக்கியம் 
//ஆயின் பாவிப்பதை தவிர்க்கவும் ஏனெனில் அது சரியான ஒழுங்கில் 
//வெளியீட்டை தரும் என்பது ஐயம் ஆகும் 

// && is logical and, || is logical or
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear";
}
if (colour == "red" || colour == "blue"){
    // colour is either red or blue
}

// && and || "short circuit", which is useful for setting default values.
var name = otherName || "default";



grade = 'B';
switch (grade) {
  case 'A':
    console.log("Great job");
    break;
  case 'B':
    console.log("OK job");
    break;
  case 'C':
    console.log("You can do better");
    break;
  default:
    console.log("Oy vey");
    break;
}


///////////////////////////////////
// 4. Functions, Scope and Closures

// JavaScript இல் functions நிறுவ  `function` keyword.பயன்படும் 
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"

//ஒரு பெறுமானத்தை return செய்ய வேண்டும் எனின் இரண்டும் ஒரே வரியில் 
//இருக்க வேண்டும் இல்லாவிடின் return ஆனது `undefined ` return செய்யும் 
//காற் புள்ளி  தானாகவே இடப்படும் , நீங்கள்  Allman style உபயோகிக்கும் போது 
//அவதானமாக இருக்கவும் 
function myFunction()
{
    return // <- semicolon automatically inserted here
    {
        thisIsAn: 'object literal'
    }
}
myFunction(); // = undefined

// JavaScript functions ஆனது   first class objects ஆகும் ,எனவே அவற்றை மாறிகளுக்கு 
//assign செய்ய முடியும் அதுமட்டும் அல்லது functions களில் arguments ஆக அனுப்பமுடியும் 
// உதாரணமாக ஒரு event handler:
function myFunction(){
    //இந்த code 5 செக்கன்களில்  செயற்படுத்தப்படும் 
}
setTimeout(myFunction, 5000);
// Note: setTimeout ஆனது  ஜாவஸ்க்ரிப்ட்  சேர்ந்தது அன்று , ஆனால் அந்த வசதி 
//உலாவிகளிலும் ,Node .js  காணப்படுகிறது 

// Function objects கட்டாயம் பெயரிடப்பட வீண்டும் என்று அவசியம் இல்லை 
// அவை  anonymous(பெயரிடப்படாமல்) உருவாக்கபடலாம் 
setTimeout(function(){
        //இந்த code 5 செக்கன்களில்  செயற்படுத்தப்படும் 
}, 5000);

// JavaScript  function ஒரு குறிப்பிட்ட  scope(எல்லை) கொண்டுள்ளது ;
//functions தமக்கென  ஒரு scope கொண்டுள்ளன .

if (true){
    var i = 5;
}
i; // = 5 - //இது undefined அல்ல 

// இதன் காரணமாக anonymous functions உடனடியாக செயற்படுத்தபடுகின்றன 
//இதன் மூலம் தற்காலிக மாறிகள்(variable) குளோபல் scope
//இற்கு மாறுவதை தவிர்க்கலாம் .
(function(){
    var temporary = 5;
	//நாங்கள் ஒரு மாறியை எங்கிருந்தும் அணுக (access) அதை "global object"
	//ஒன்றுக்கு வழங்க வேண்டும் உலாவியில் அது எப்போதும் `window` ஆகும் .
	//உலாவி  அல்லாத சூழலில் (Node.js) வேறு பெயருடன் இருக்கும் 
    window.permanent = 10;
})();
temporary; // raises ReferenceError
permanent; // = 10

//JavaScript's மிகவும் சக்தி வாய்ந்த ஒரு வசதி closures ஆகும் 
//ஒரு function இன்னொரு function உள் உருவாக்கபடின்
//அது உருவாகப்பட்ட function  இன் மாறிகளை அணுக முடியும்  
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!";
    // Inner functions ஆனது local scope இல்  காணப்படும் 
	//அது `var ` என்ற குறியீட்டு சொல்லால் நிறுவப்படும் 
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    //setTimeout  ஆனது background இல் இயங்கும்  , எனவே sayHelloInFiveSeconds function,
	//செயற்பாடு முடிவடைய ,setTimeout ஆனது inner function call செய்யும்.

}
sayHelloInFiveSeconds("Adam"); // //இது ஒரு popup  ஐ ஐந்து செக்கன்களில் காட்டும் 

///////////////////////////////////
// 5.  Objects; Constructors and Prototypes  பற்றி மேலும் 

// Objects  functions ஐ கொண்டிருக்கலாம் 
var myObj = {
    myFunc: function(){
        return "Hello world!";
    }
};
myObj.myFunc(); // = "Hello world!"

//functions ஆனது objects உடன் இணைக்கப்பட்டுள போது அவை object ஐ அணுக முடியும் 
//அவை this என்ற  குறியீட்டு  சொல்லை பயன்படுத்தி இணைக்கபடுகின்றன 
myObj = {
    myString: "Hello world!",
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = "Hello world!"

//எங்கள் function ஆனது தொழிற் படாமல் போகலாம் அது context(அமைப்பு ) of the object call செய்யபடவிடின் 
var myFunc = myObj.myFunc;
myFunc(); // = undefined


//function ஆனது ஒரு object உக்கு assign செய்யலாம் பிறகு அதை நாம் அணுகமுடியும் 
//`this` மூலம்  
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"

//ஒரு function ஒரு அமைப்பை நாம் உருவாக்க முடியும் 
//அதை நாம் `call` அல்லது  `apply` மூலம் செயல்படுத்த முடியும்

var anotherFunc = function(s){
    return this.myString + s;
}
anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"

//apply செயற்பாட்டளவில் ஒத்தன ,ஆனால் அது array (அணி) argument 
//ஆக எடுக்கிறது.

anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!"

//இது தொடர்ச்சியான arguments ஐ நாம் function ஒன்றுக்குள் pass பண்ண 
//வேண்டும் எனில் மிகவும் உபயோகமானது 

Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

//ஆனால் `call ` ,`apply ` இரண்டும் தற்காலிகமானவை 
//அவற்றை நிரந்தரமாக்க bind function ஐ பயன்படுத்தவும் 

var boundFunc = anotherFunc.bind(myObj);
boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"

//`bind ` ஐ உபயோகித்து ஒரு function ஐ பகுதியாக apply செய்ய முடியும் 

var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16


//ஒரு function ஐ நாம் new என்ற குறியீட்டு சொல்லை பயன்படுத்தி 
//அழைக்கும் போது  புதிய object உருவாக்கப்படும் .இவ்வாறான  functions 
//constructors என அழைக்கப்படும் 

var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

//ஒவ்வொரு JavaScript object உம் ஒரு `prototype ` கொண்டுள்ளது 
//நீங்கள் object ஒன்றின் ஒரு property ஐ அணுகும் போது 
//அந்த property இல்லாவிடின்  interpreter  ஆனது 
//அதன் prototype உள்ளதா என பார்க்கும் 

//JS இன் சில செயலாக்கங்கள் ஒரு object இன் protoype ஐ 
//இலகுவாக `__proto__` மூலம் access செய்ய முடியும் .
//இது prototype பாவணை யை இலகுவாக்கினாலும் 
//இது சரியான ஒரு முறை அல்ல 
var myObj = {
    myString: "Hello world!"
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// This works for functions, too.
myObj.myFunc(); // = "hello world!"

//உங்கள் property prototype இல் இல்லது இருப்பின் , protype இன் 
//prototype search செய்யப்படும் 
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

//ஒவ்வொரு object உம்  அதன் protype க்கும்  reference (மேற்கோள் ) ஒன்றை வைத்திருக்கும்  
//நாம் ஒரு protype இணை மாற்றினால் அதன் மாற்றங்கள் எல்லா இடத்திலும் (program இல் )
//பிரதிபலிக்கும் 
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

 
//நாம் முன்பு கூறியது போல் `__proto__` பயன்படுத்துவது சரியான முறை அல்ல 
//எனவே நாம் ஒரு protype ஐ object இல் உருவாக்க  இரண்டு வழிமுறைகள்
//உள்ளன

// முதல் முறை  Object.create இது அண்மையில் அறிமுகம் செய்ய பட்ட ஒன்று
//எனவே சில இடங்களில் இந்த முறை இன்னும் அறிமுகம் ஆகவில்லை

var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43


// இரண்டாவது முறை , இது சகல இடங்களிலும் வேலைசெய்யும், இது constructors மூலம்.
//constructors prototype என்னும் ஒரு காரணியை  கொண்டுள்ளது , இது  constructor function
//இன் prototype அன்று. ,இது நாம் new என்ற குறியீட்டு சொல்லையும்  அந்த constructor உபயோகித்து
//உருவாக்கபடுகிறது

MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function(){
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// Built-in types like strings and numbers also have constructors that create
// equivalent wrapper objects.
// JavaScript இல் உள்ள strings மற்றும் numbers வகைகளும் constructors கொண்டுள்ளன
//இவை wrapper objects ஐ ஒத்தன

var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true


//இவை மிக சிறிய அளவில் ஒத்தவை
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // இந்த கூற்றானது செயல்படுத்தபடாது ஏனெனில் ௦ false ஆகும்
}

// However, the wrapper objects and the regular builtins share a prototype, so
// you can actually add functionality to a string, for instance.

//இருப்பினும்  wrapper objects மற்றும் regular builtins ஆகியன  prototype ஒன்றை கொண்டுள்ளன
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// This fact is often used in "polyfilling", which is implementing newer
// features of JavaScript in an older subset of JavaScript, so that they can be
// used in older environments such as outdated browsers.

//இந்த முறையானது  "polyfilling" இல் உபயோகபடுத்தபடுகிறது.
//புதிய சில  வசதிகளை JavaScript பழைய JavaScript பிரதிகளில் இல்  உருவாக்குகிறது.
//இது பழைய சூழல்களில் உபயோகிகப்படும்.


//நாங்கள் முன்பு கூறி இருந்தோம்  Object.create சில இடங்களில் இந்த முறை இன்னும் 
//அறிமுகம் ஆகவில்லை என்று ஆனால் இதை polyfill ஐ  பயன்படுத்தி உருவாக்க
//முடியும்

if (Object.create === undefined){ // don't overwrite it if it exists
    Object.create = function(proto){
        // make a temporary constructor with the right prototype
        var Constructor = function(){};
        Constructor.prototype = proto;
        // then use it to create a new, appropriately-prototyped object
        return new Constructor();
    }
}
```

## மேலும் JavaScript பற்றி கற்க

The [Mozilla Developer
Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript) provides
excellent documentation for JavaScript as it's used in browsers. Plus, it's a
wiki, so as you learn more you can help others out by sharing your own
knowledge.

MDN's [A re-introduction to
JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
covers much of the concepts covered here in more detail. This guide has quite
deliberately only covered the JavaScript language itself; if you want to learn
more about how to use JavaScript in web pages, start by learning about the
[Document Object
Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)

[Learn Javascript by Example and with Challenges](http://www.learneroo.com/modules/64/nodes/350) is a variant of this reference with built-in challenges.

[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) is an in-depth
guide of all the counter-intuitive parts of the language.

[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) is a classic guide / reference book.

In addition to direct contributors to this article, some content is adapted
from Louie Dinh's Python tutorial on this site, and the [JS
Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
on the Mozilla Developer Network.
---
language: json
filename: learnjson-ta.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
translators:
  - ["Rasendran Kirushan", "https://github.com/kirushanr"]
lang: ta-in
---

ஜேசன் ஒரு ஒரு மிக எளிய தரவு உள்மாற்றீட்டு வடிவம் ஆகும்.
Learn X in Y Minutes இதுவே மிகவும் இலகுவான பகுதியாக  அமைய போகிறது.


ஜேசன்  இன் எளிமையான கட்டமைப்பில்  குறிப்புக்கள் (Comments) இல்லை , எனினும் 
பெரும்பாலான  பாகுபடுத்திகளில் C - style  முறையிலான (`//`, `/* */`) குறிப்புகளை இட முடியும்.
சில பாகுபடுத்திகள்(interpreter) குறிப்புகளுக்கு (comments)தொடர்ச்சியாக வரும்
 காற்புள்ளியை  அனுமதிக்கின்றன (உதாரணமாக ஒரு அணியை (array) அடுத்துவரும் காற்புள்ளி
 அல்லது ஒரு பொருளில் (object)உள்ள கடைசி உறுப்பை/சொத்தை(  last property) அடுத்து வரும் காற்புள்ளி )
எனினும் சகல இடங்களிலும் ஜேசன் பயன்படுத்த பட வேண்டும் எனில் மேற்கூறிய குறிப்புகளை தவிர்த்தல் நல்லது .\


ஜேசன் 100% மிக சரியாக அமைவது மட்டும் இன்றி 
இலகுவாக புரியக் கூடிய எளிய தரவு உள்மாற்றீட்டு வடிவம் ஆகும்.


ஜேசன் அனுமதிக்கும் தரவு வகைகள் : சரம் (string),முழு (int),பூலியன் (தர்க ரீதியில் ஆன கட்டமைப்பு),
அணி (array ),கழி (null ),பொருள் (object).

ஜேசன் அனுமதிக்கும் அல்லது பாவனைக்கு உட்படுத்த கூடிய உலாவிகள் (browsers): 
Firefox(Mozilla) 3.5, Internet Explorer 8, Chrome, Opera 10, Safari 4.

ஜேசனின் கோப்புவகை(filetype)  ".json " ஆகும் .

ஜேசன் உரைக்கான MIME வகை   "application/json" ஆகும். 
ஜேசன் இல் காணப்படும் பிரதான பின்னடைவு தரவு இனம் இதுவென்று வரையறுக்க 
படாமை ஆகும் .

ஒரு ஜேசன் இன் எளிய கட்டமைப்பு கீழே காட்டப்பட்டுள்ளது 

```json
{
  "key": "ஒரு சாவிக்கு ஒரு பெறுமதி உள்ளது ",

  "keys": "சாவிகள் , மற்றும் பெறுமானங்கள் மேற்கோள் குறிக்குள் இடல் வேண்டும்",
  "numbers": 0,
  "strings": "Hellø, wørld. எல்லாவகையான  unicode உம் அனுமதிக்கப்படும், அத்துடன் \"escaping\".",
  "has bools?": true,
  "nothingness": null,

  "big number": 1.2e+100,

  "objects": {
    "comment": "பெரும்பாலான கட்டமைப்புகள் objects இல் இருந்தே வருகின்றன",

    "array": [0, 1, 2, 3, "array யானது எல்லாவகையான பெறுமானங்களையும் கொண்டிருக்கும்", 5],

    "another object": {
      "comment": "இவை ஒன்றுக்குள் இன்னொன்றை எழுத முடியும்"
    }
  },

  "silliness": [
    {
      "sources of potassium": ["வாழைபழம்"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "alternative style": {
    "comment": "இதை பார்க்கவும்"
  , "comma position": "doesn't matter - as long as it's before the value, then it's valid"
  , "another comment": "how nice"
  },

  "that was short": "நீங்கள் ஜேசன் பற்றி யாவற்றையும் கற்றுள்ளீர்கள்"
}
```

---
language: xml
filename: learnxml-ta.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Rasendran Kirushan", "https://github.com/kirushanr"]
lang: in-ta
---


XML ஆனது ஒரு கட்டமைப்பு மொழி ஆகும் இது தகவலை சேமிக்கவும்
தகவலை பரிமாறவும் உருவாக்கபட்டுள்ளது


HTML போல் அன்றி , XML ஆனது தகவலை மட்டும் கொண்டு செல்ல்கிறது 
* XML வாக்கிய அமைப்பு


```xml
<!-- இது ஒரு XML குறிப்பு -->

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>


  <!--
  
	மேல காட்டப்பட்டுள்ளது ஒரு xml file இன் உதாரணம் ஆகும்
	அது metadata உடன் ஆரம்பமாகிறது
	XML  ஆனது ஒரு மரத்தை போன்ற கட்டமைப்பை ஒத்தது. 
	இங்கு root node (கொப்பு)  `bookstore`  இது மூன்று கிளைகள்  (child nodes)
	கொண்டுள்ளது. இந்த கிளைகள் மேலும் சில கிளைகளை கொண்டு இருக்கலாம்
	ஒவொரு node கட்டமைப்பும்  ஒரு `<` ஆரம்பாமாகி `>` முடிவடையும்
	கிளைகள் இந்த கட்டமைப்புக்கு இடையில் நிறுவப்படும்
  -->


<!--
XML இரண்டு வகையான தகவல்களை கொண்டு செல்லக்கூடியது
1- Attributes -> ஒரு  கணு(node) பற்றிய metadata 
பொதுவாக   XML Parser இந்த தகவலை பயன்படுத்தியே தகவலை
சரியான முறையில் சேமிக்க.
இது xml கட்டமைப்பின் ஆரம்பத்தில் உள்ள name="value"
தீர்மானிக்கபடுகிறது.

2-Elements ->இவற்றில் முற்றிலும் தகவல்களே சேமிக்கபட்டு இருக்கும்
Elements  ஒரு `<` ஆரம்பாமாகி `>` முடிவடையும் காணப்படும்


-->

<!-- கிழே உள்ள element இரண்டு பெறுமானங்களை கொண்டுள்ளது  -->
<file type="gif" id="4293">computer.gif</file>


```

* சரியான முறையில் ஒழுகுபடுத்தபட்ட X document


ஒரு XML document ஆனது சரியான முறையில் எழுத பட்டிருப்பின் மட்டுமே அது 
சிறந்த வகையில்  வடிவமைக்கபட்டுள்ளது,எனினும் மேலும் பல கட்டுபாடுகளை
நாம் ஒரு xml document உக்கு இட முடியும் உ.ம்:-DTD மற்றும்  XML Schema.


ஒரு xml document ஆனது ஒரு வரையறுக்கபட்டிருப்பின் மட்டுமே 
அது சரி என கொள்ளப்படும்


With this tool, you can check the XML data outside the application logic.
இந்த கருவியை உபயோகித்து xml தகவல்களை சோதிக்க முடியும் 

```xml

<!-- கீழே bookstore html document இன் எளிமையான வடிவம் 
    DTD வரையறைகளுடன்
-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Bookstore.dtd">
<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>

<!-- DTD ஆனது பின்வருமாறு  அமையும் :-->

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>


<!-- DTD ஆனது root node ஐ உருவாக்கிய பின் நிறுவ படுகிறது ,இது ஒன்று அல்லது 
ஒன்றிக்கு மேற்பட்ட child node களை எதிர்பார்க்கிறது.
 ஒவ்வொரு 'book' உம் கட்டாயமாக ஒரு 'title' , 'price','category', with "Literature"
 ஆகிய பெறுமானங்களை கொண்டிருத்தல் அவசியம்.
--> 

<!-- DTD ஆனது xml file ஒன்றினுள் உருவாக்கபடுகிறது-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>

<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>
```
---
language: Tcl
contributors:
    - ["Poor Yorick", "https://pooryorick.com/"]
filename: learntcl.tcl
---

Tcl was created by [John Ousterhout](https://wiki.tcl.tk/John%20Ousterout) as a
reusable scripting language for circuit design tools that he authored.  In 1997 he
was awarded the [ACM Software System
Award](https://en.wikipedia.org/wiki/ACM_Software_System_Award) for Tcl.   Tcl
can be used both as an embeddable scripting language and as a general
programming language.  It can also be used as a portable C library, even in
cases where no scripting capability is needed, as it provides data structures
such as dynamic strings, lists, and hash tables.  The C library also provides
portable functionality for loading dynamic libraries, string formatting and
code conversion, filesystem operations, network operations, and more.  Various
features of Tcl stand out:

* Convenient cross-platform networking API

* Fully virtualized filesystem

* Stackable I/O channels

* Asynchronous to the core

* Full coroutines

* A threading model recognized as robust and easy to use


Tcl has much in common with Lisp, but instead of lists, Tcl uses strings as the
currency of the language.  All values are strings.  A list is a string with a
defined format, and the body of a procedure (a script) is also a string rather
than a block.  To achieve performance, Tcl internally caches structured
representations of these values.  list routines, for example, operate on
the internal cached representation, and Tcl takes care of updating the string
representation if it is ever actually needed in the script.  The copy-on-write
design of Tcl allows script authors to pass around large data values without
actually incurring additional memory overhead.  Procedures are automatically
byte-compiled unless they use the more dynamic routines such as "uplevel",
"upvar", and "trace".

Tcl is a pleasure to program in.  It will appeal to hacker types who find Lisp,
Forth, or Smalltalk interesting, as well as to engineers and scientists who
just want to get down to business with a tool that bends to their will.  Its
discipline of exposing all programmatic functionality as routines, including
things like looping and mathematical operations that are usually baked into the
syntax of other languages, allows it to fade into the background of whatever
domain-specific functionality a project needs. Its syntax, which is even
lighter than that of Lisp, just gets out of the way.



```tcl
#! /bin/env tclsh

###############################################################################
## 1. Guidelines
###############################################################################

# Tcl is not Sh or C!  This needs to be said because standard shell quoting
# habits almost work in Tcl and it is common for people to pick up Tcl and try
# to get by with syntax they know from another language.  It works at first,
# but soon leads to frustration when scripts become more complex.

# Braces are a quoting mechanism, not syntax for the construction of code
# blocks or lists. Tcl doesn't have either of those things.  Braces are used to
# escape special characters, which makes them well-suited for quoting procedure
# bodies and strings that should be interpreted as lists.


###############################################################################
## 2. Syntax
###############################################################################

# A script is made up of commands delimited by newlines or semicolons.  Each
# command is a call to a routine.  The first word is the name of a routine to
# call, and subsequent words are arguments to the routine.  Words are delimited
# by whitespace.  Since each argument is a word in the command it is already a
# string, and may be unquoted:
set part1 Sal
set part2 ut; set part3 ations


# a dollar sign introduces variable substitution:
set greeting $part1$part2$part3


# When "set" is given only the name of a variable, it returns the
# value of that variable:
set part3 ;# Returns the value of the variable.


# Left and right brackets embed a script to be evaluated for a result to
# substitute into the word:
set greeting $part1$part2[set part3]


# An embedded script may be composed of multiple commands, the last of which provides
# the result for the substitution:
set greeting $greeting[
    incr i
    incr i
    incr i
]
puts $greeting ;# The output is "Salutations3"

# Every word in a command is a string, including the name of the routine, so
# substitutions can be used on it as well. Given this variable
# assignment,
set action pu

# , the following three commands are equivalent:
puts $greeting
${action}ts $greeting 
[set action]ts $greeting


# backslash suppresses the special meaning of characters:
set amount \$16.42


# backslash adds special meaning to certain characters:
puts lots\nof\n\n\n\n\n\nnewlines


# A word enclosed in braces is not subject to any special interpretation or
# substitutions, except that a backslash before a brace is not counted when
# looking for the closing brace:
set somevar {
    This is a literal $ sign, and this \} escaped
    brace remains uninterpreted
}


# In a word enclosed in double quotes, whitespace characters lose their special
# meaning:
set name Neo
set greeting "Hello, $name"


# A variable name can be any string:
set {first name} New


# The braced form of variable substitution handles more complex variable names:
set greeting "Hello, ${first name}"


# "set" can always be used instead of variable substitution, and can handle all
# variable names:
set greeting "Hello, [set {first name}]"


# To unpack a list into the command, use the expansion operator, "{*}".  These
# two commands are equivalent:
set name Neo
set {*}{name Neo}


# An array is a special variable that is a container for other variables.
set person(name) Neo
set person(destiny) {The One}
set greeting "Hello, $person(name)"


# "variable" can be used to declare or set variables. In contrast with "set",
# which uses both the global namespace and the current namespace to resolve a
# variable name, "variable" uses only the current namespace:
variable name New


# "namespace eval" creates a new namespace if it doesn't exist.  A namespace
# can contain both routines and variables:
namespace eval people {
    namespace eval person1 {
        variable name Neo
    }
}


# Use two or more colons to delimit namespace components in variable names:
namespace eval people {
    set greeting "Hello $person1::name"
}

# Two or more colons also delimit namespace components in routine names:
proc people::person1::speak {} {
    puts {I am The One.}
}

# Fully-qualified names begin with two colons:
set greeting "Hello $::people::person1::name"



###############################################################################
## 3. No More Syntax
###############################################################################

# All other functionality is implemented via routines.  From this point on,
# there is no new syntax.  Everything else there is to learn about
# Tcl is about the behaviour of individual routines and what meaning they
# assign to their arguments.



###############################################################################
## 4. Variables and Namespaces
###############################################################################

# Each variable and routine is associated with some namespace.

# To end up with an interpreter that can do nothing, delete the global
# namespace.  It's not very useful to do such a thing, but it illustrates the
# nature of Tcl.  The name of the global namespace is actually the empty
# string, but the only way to represent it is as a fully-qualified name. To
# try it out call this routine:
proc delete_global_namespace {} {
    namespace delete ::
}

# Because "set" always keeps its eye on both the global namespace and the
# current namespace, it's safer to use "variable" to declare a variable or
# assign a value to a variable.  If a variable called "name" already exists in
# the global namespace, using "set" here will assign a value to the global
# variable instead of to a variable in the current namespace, whereas
# "variable" operates only on the current namespace.
namespace eval people {
    namespace eval person1 {
        variable name Neo
    }
}

# Once a variable is declared in a namespace, [set] sees it instead of seeing
# an identically-named variable in the global namespace:
namespace eval people {
    namespace eval person1 {
        variable name
        set name Neo
    }
}

# But if "set" has to create a new variable, it always does it relative to the
# current namespace:
unset name
namespace eval people {
    namespace eval person1 {
        set name neo
    }

}
set people::person1::name


# An absolute name always begins with the name of the global namespace (the
# empty string), followed by two colons:
set ::people::person1::name Neo


# Within a procedure, the "variable" links a variable in the current namespace
# into the local scope:
namespace eval people::person1 {
    proc fly {} {
        variable name
        puts "$name is flying!"
    }
}




###############################################################################
## 4. Built-in Routines
###############################################################################

# Math can be done with the "expr":
set a 3
set b 4
set c [expr {$a + $b}]

# Since "expr" performs variable substitution on its own, brace the expression
# to prevent Tcl from performing variable substitution first.  See
# "http://wiki.tcl.tk/Brace%20your%20#%20expr-essions" for details.


# "expr" understands variable and script substitution:
set c [expr {$a + [set b]}]


# "expr" provides a set of mathematical functions:
set c [expr {pow($a,$b)}]


# Mathematical operators are available as routines in the ::tcl::mathop
# namespace:
::tcl::mathop::+ 5 3

# Routines can be imported from other namespaces:
namespace import ::tcl::mathop::+
set result [+ 5 3]


# Non-numeric values must be quoted, and operators like "eq" can be used to
# constrain the operation to string comparison:
set name Neo
expr {{Bob} eq $name}

# The general operators fall back to string string comparison if numeric
# operation isn't feasible:
expr {{Bob} == $name}


# "proc" creates new routines:
proc greet name {
    return "Hello, $name!"
}

#multiple parameters can be specified:
proc greet {greeting name} {
    return "$greeting, $name!"
}


# As noted earlier, braces do not construct a code block.  Every value, even
# the third argument to "proc", is a string.  The previous command
# can be rewritten using no braces:
proc greet greeting\ name return\ \"\$greeting,\ \$name!\"



# When the last parameter is the literal value "args", all extra arguments
# passed to the routine are collected into a list and assigned to "args":
proc fold {cmd first args} {
    foreach arg $args {
        set first [$cmd $first $arg]
    }
    return $first
}
fold ::tcl::mathop::* 5 3 3 ;# ->  45


# Conditional execution is implemented as a routine:
if {3 > 4} {
    puts {This will never happen}
} elseif {4 > 4} {
    puts {This will also never happen}
} else {
    puts {This will always happen}
}


# Loops are implemented as routines.  The first and third arguments to 
# "for" are treated as scripts, while the second argument is treated as
# an expression:
set res 0
for {set i 0} {$i < 10} {incr i} {
    set res [expr {$res + $i}]
}
unset res


# The first argument to "while" is also treated as an expression:
set i 0
while {$i < 10} {
    incr i 2
}


# A list is a string, and items in the list are delimited by whitespace:
set amounts 10\ 33\ 18
set amount [lindex $amounts 1]

# Whitespace in a list item must be quoted:
set inventory {"item 1" item\ 2 {item 3}}


# It's generally a better idea to use list routines when modifying lists:
lappend inventory {item 1} {item 2} {item 3}


# Braces and backslash can be used to format more complex values in a list.  A
# list looks exactly like a script, except that the newline character and the
# semicolon character lose their special meanings, and there is no script or
# variable substitution.  This feature makes Tcl homoiconic.  There are three
# items in the following list:
set values {

    one\ two

    {three four}

    five\{six

}


# Since, like all values, a list is a string, string operations could be
# performed on it, at the risk of corrupting the formatting of the list:
set values {one two three four}
set values [string map {two \{} $values] ;# $values is no-longer a \
    properly-formatted list


# The sure-fire way to get a properly-formatted list is to use "list" routines:
set values [list one \{ three four]
lappend values { } ;# add a single space as an item in the list


# Use "eval" to evaluate a value as a script:
eval {
    set name Neo
    set greeting "Hello, $name"
}


# A list can always be passed to "eval" as a script composed of a single
# command:
eval {set name Neo}
eval [list set greeting "Hello, $name"]


# Therefore, when using "eval", use "list" to build up the desired command:
set command {set name}
lappend command {Archibald Sorbisol}
eval $command


# A common mistake is not to use list functions when building up a command:
set command {set name}
append command { Archibald Sorbisol}
try {
    eval $command ;# The error here is that there are too many arguments \
        to "set" in {set name Archibald Sorbisol}
} on error {result eoptions} {
    puts [list {received an error} $result]
}

# This mistake can easily occur with "subst":

set replacement {Archibald Sorbisol}
set command {set name $replacement}
set command [subst $command] 
try {
    eval $command ;# The same error as before:  too many arguments to "set" in \
        {set name Archibald Sorbisol}
} trap {TCL WRONGARGS} {result options} {
    puts [list {received another error} $result]
}


# "list" correctly formats a value for substitution:
set replacement [list {Archibald Sorbisol}]
set command {set name $replacement}
set command [subst $command]
eval $command


# "list" is commonly used to format values for substitution into scripts: There
# are several examples of this, below.


# "apply" evaluates a two-item list as a routine:
set cmd {{greeting name} {
    return "$greeting, $name!"
}}
apply $cmd Whaddup Neo

# A third item can be used to specify the namespace to apply the routine in:
set cmd [list {greeting name} {
    return "$greeting, $name!"
} [namespace current]]
apply $cmd Whaddup Neo


# "uplevel" evaluates a script at some higher level in the call stack:
proc greet {} {
    uplevel {puts "$greeting, $name"}
}

proc set_double {varname value} {
    if {[string is double $value]} {
        uplevel [list variable $varname $value]
    } else {
        error [list {not a double} $value]
    }
}


# "upvar" links a variable at the current level in the call stack to a variable
# at some higher level:
proc set_double {varname value} {
    if {[string is double $value]} {
        upvar 1 $varname var
        set var $value
    } else {
        error [list {not a double} $value]
    }
}


# Get rid of the built-in "while" routine, and use "proc" to define a new one:
rename ::while {}
# handling is left as an exercise:
proc while {condition script} {
    if {[uplevel 1 [list expr $condition]]} {
        uplevel 1 $script
        tailcall [namespace which while] $condition $script
    }
}


# "coroutine" creates a new call stack, a new routine to enter that call stack,
# and then calls that routine.  "yield" suspends evaluation in that stack and
# returns control to the calling stack:
proc countdown count {
    # send something back to the creator of the coroutine, effectively pausing
    # this call stack for the time being.
    yield [info coroutine]

    while {$count > 1} {
        yield [incr count -1]
    }
    return 0
}
coroutine countdown1 countdown 3
coroutine countdown2 countdown 5
puts [countdown1] ;# -> 2 
puts [countdown2] ;# -> 4 
puts [countdown1] ;# -> 1 
puts [countdown1] ;# -> 0 
catch {
    puts [coundown1] ;# -> invalid command name "countdown1"
} cres copts 
puts $cres
puts [countdown2] ;# -> 3 


# Coroutine stacks can yield control to each other:

proc pass {whom args} {
    return [yieldto $whom {*}$args]
}

coroutine a apply {{} {
        yield
        set result [pass b {please pass the salt}]
        puts [list got the $result]
        set result [pass b {please pass the pepper}]
        puts [list got the $result]
}}

coroutine b apply {{} {
    set request [yield]
    while 1 {
        set response [pass c $request]
        puts [list [info coroutine] is now yielding]
        set request [pass a $response]
    }
}}

coroutine c apply {{} {
    set request [yield]
    while 1 {
        if {[string match *salt* $request]} {
            set request [pass b salt]
        } else {
            set request [pass b huh?]
        }
    }
}}

# get things moving
a


```

## Reference

[Official Tcl Documentation](http://www.tcl.tk/man/tcl/)

[Tcl Wiki](http://wiki.tcl.tk)

[Tcl Subreddit](http://www.reddit.com/r/Tcl)
---
language: tcsh
filename: LearnTCSH.csh
contributors:
       - ["Nicholas Christopoulos", "https://github.com/nereusx"]

---
tcsh ("tee-see-shell") is a Unix shell based on and compatible with the C shell (csh).
It is essentially the C shell with programmable command-line completion, command-line editing,
and a few other features.
It is the native root shell for BSD-based systems such as FreeBSD.

Almost all Linux distros and BSD today use tcsh instead of the original csh. In
most cases csh is a symbolic link that points to tcsh.
This is because tcsh is backward compatible with csh, and the last
is not maintained anymore.

- [TCSH Home](http://www.tcsh.org/)
- [TCSH Wikipedia](https://en.wikipedia.org/wiki/Tcsh)
- [TCSH manual page](http://www.tcsh.org/tcsh.html/top.html)
- [“An Introduction to the C shell”, William Joy](https://docs.freebsd.org/44doc/usd/04.csh/paper.html)
- [TCSH Bug reports and/or features requests](https://bugs.gw.com/)

Some more files:
[tcsh help command (for 132x35 terminal size)](https://github.com/nereusx/dotfiles/blob/master/csh-help),
[my ~/.tcshrc](https://github.com/nereusx/dotfiles/blob/master/.tcshrc)

```tcsh
#!/bin/tcsh
# First line of the script is shebang which tells the system how to execute the
# script: http://en.wikipedia.org/wiki/Shebang_(Unix)
# TCSH emulates the shebang on systems which don't understand it.

# In most cases you'll use `#!/bin/tcsh -f', because `-f' option does not load
# any resource or start-up files, or perform any command hashing, and thus
# starts faster.

# --- the echo command --------------------------------------------------------
# The `echo' writes each word to the shell's standard output, separated by
# spaces and terminated with a newline. The echo_style shell variable may be
# set to emulate (or not) the flags and escape sequences.

# Display the value of echo_style
echo $echo_style

# Enable `echo' to support backslashed characters and `-n' option (no new line)
# This is the default for tcsh, but your distro may change it. Slackware has
# done so.
set echo_style = both

# Prints "Hello world"
echo Hello world
echo "Hello world"
echo 'Hello world'
echo `echo Hello world`

# This prints "twonlines" in one line
echo two\nlines

# Prints the two lines
echo "two\nlines"
echo 'two\nlines'

# --- Basic Syntax ------------------------------------------------------------

# A special character (including a blank or tab) may be prevented from having
# its special meaning by preceding it with a backslash `\'.
# this will display the last history commands
echo !!
# this will not
echo \!\!

# Single quotes prevents expanding special characters too, but some
# characters like `!' and backslash have higher priority
# `$' (variable value) will not expands
echo '$1 tip'
# `!' (history) will expands
echo '!!'

# Strings enclosed by back-quotes will be executed and replaced by the result.
echo `ls`

# Semi-colon separate commands
echo 'first line'; echo 'second line'

# There is also conditional execution
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# Parenthesised commands are always executed in a subshell,

# example: create a project and then informs you that it finished while
# it does the installation.
make && ( espeak "BOSS, compilation finished"; make install )

# prints the home directory but leaving you where you were
(cd; pwd); pwd

# Read tcsh man-page documentation
man tcsh

# --- Variables ---------------------------------------------------------------
# The shell maintains a list of variables, each of which has as value a list of
# zero or more words. The values of shell variables can be displayed and
# changed with the `set' and `unset' commands.
# The system maintains its own list of ``environment'' variables.
# These can be displayed and changed with `printenv', `setenv' and `unsetenv'.
# The syntax of setenv is similar to POSIX sh.

# Assign a value or nothing will create a variable
# Assign nothing
set var
# Assign a numeric value
# the '@' denotes the expression is arithmetic; it works similar to 'set' but
# the right value can be a numeric expression.
@ var = 1 + 2
# Assign a string value
set var = "Hello, I am the contents of 'var' variable"
# Assign the output of a program
set var = `ls`

# Remove a variable
unset var
# Prints 1 (true) if the variable `var' exists otherwise prints 0 (false)
echo $?var
# Print all variables and their values
set

# Prints the contents of 'var'
echo $var;
echo "$var";
# Prints the string `$var'
echo \$var
echo '$var'
# braces can be used to separate variable from the rest when its needed
set num = 12; echo "There ${num}th element"

# Prints the number of characters of the value: 6
set var = '123456'; echo $%var

### LISTs
# Assign a list of values
set var = ( one two three four five )
# Print all the elements: one two three four five
echo $var
echo $var[*]
# Print the count of elements: 5
echo $#var
# Print indexed element; prints the second element: two
echo $var[2]
# Print range of elements; prints 2nd up to 3rd: two, three
echo $var[2-3]
# Prints all elements starting from the 3rd: three four five
echo $var[3-]
# Prints print all up to 3rd element: one two three
echo $var[-3]

### Special Variables
# $argv         list of command-line arguments
# $argv[0]      this file-name (the file of the script file)
# $# $0, $n, $* are the same as $#argv, $argv[0], $argv[n], $argv[*]
# $status, $?   the exit code of the last command that executed
# $_            the previous command line
# $!            the PID of the last background process started by this shell
# $$            script's PID

# $path, $PATH  the list of directories that will search for executable to run
# $home, $HOME  user's home directory, also the `~' can be used instead
# $uid          user's login ID
# $user         user's login name
# $gid          the user's group ID
# $group        the user's group-name
# $cwd, $PWD    the Current/Print Working Directory
# $owd          the previous working directory
# $tcsh         tcsh version
# $tty          the current tty; ttyN for linux console, pts/N for terminal
#               emulators under X
# $term         the terminal type
# $verbose      if set, causes the words of each command to be printed.
#               can be set by the `-v' command line option too.
# $loginsh      if set, it is a login shell

# TIP: $?0 is always false in interactive shells
# TIP: $?prompt is always false in non-interactive shells
# TIP: if `$?tcsh' is unset; you run the original `csh' or something else;
#      try `echo $shell'
# TIP: $verbose this is useful to debugging scripts
# NOTE: $PWD and $PATH are synchronised with $cwd and $pwd automatically.

# --- Variable modifiers ------------------------------------------------------
# Syntax: ${var}:m[:mN]
# Where <m> is:
# h : the directory  t : the filenane  r : remove extension   e : the extension
# u : uppercase the first lowercase letter
# l : lowercase the first uppercase letter
# p : print but do not execute it (hist)
# q : quote the substituted words, preventing further substitutions
# x : like q, but break into words at white spaces
# g : apply the following modifier once to each word
# a  : apply the following modifier as many times as possible to single word
# s/l/r/ : search for `l' and replace with `r', not regex; the `&' in the r is
# replaced by l
# & : Repeat the previous substitution

# start with this file
set f = ~/Documents/Alpha/beta.txt
# prints ~/Documents/Alpha/beta
echo $f:r
# prints ~/Documents/Alpha
echo $f:h
# prints beta.txt
echo $f:t
# prints txt
echo $f:e
# prints beta
echo $f:t:r
# prints Beta
echo $f:t:r:u
# prints Biota
echo $f:t:r:u:s/eta/iota/

# --- Redirection -------------------------------------------------------------

# Create file.txt and write the standard output to it
echo 'this string' > file.txt
# Create file.txt and write the standard output and standard error to it
echo 'this string' >& file.txt
# Append the standard output to file.txt
echo 'this string' >> file.txt
# Append the standard output and standard error to file.txt
echo 'this string' >>& file.txt
# Redirect the standard input from file.txt
cat < file.txt
# Input from keyboard; this stores the input line to variable `x'
set x = $<
# Document here;
cat << LABEL
...text here...
LABEL

# TIP: this is how to get standard error separated:
(grep 'AGP' /usr/src/linux/Documentation/* > output-file.txt) >& error-file.txt

# example: read a name from standard input and display a greetings message
echo -n "Enter your name? "
set name = $<
echo "Greetings $name"

# --- Expressions ------------------------------------------------------------

# Operators:
# ==  equal         !=  not equal    !  not
#  >  greater than   <  less than   >=  greater or equal  <= less or equal
# &&  logical AND   ||  logical OR

if ( $name != $user ) then
    echo "Your name isn't your username"
else
    echo "Your name is your username"
endif

# single-line form
if ( $name != $user ) echo "Your name isn't your username"

# NOTE: if $name is empty, tcsh sees the above condition as:
# if ( != $user ) ...
# which is invalid syntax
# so the "safe" way to use potentially empty variables in tcsh is:
# if ( "$name" != $user ) ...
# which, when $name is empty, is seen by tcsh as:
# if ( "" != $user ) ...
# which works as expected

# There is also conditional execution
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# To use && and || with if statements, you don't need multiple pairs of
# square brackets:
if ( "$name" == "Steve" && "$age" == 15 ) then
    echo "This will run if $name is Steve AND $age is 15."
endif

if ( "$name" == "Daniya" || "$name" == "Zach" ) then
    echo "This will run if $name is Daniya OR Zach."
endif

# String matching operators ( `=~' and `!~' )
# The ‘==’ ‘!=’ ‘=~’ and ‘!~’ operators compare their arguments as strings;
# all others operate on numbers. The operators ‘=~’ and ‘!~’ are like ‘!=’
# and ‘==’ except that the right hand side is a glob-pattern against which
# the left hand operand is matched.

if ( $user =~ ni[ck]* ) echo "Greetings Mr. Nicholas."
if ( $user !~ ni[ck]* ) echo "Hey, get out of Nicholas PC."

# Arithmetic expressions are denoted with the following format:
@ result = 10 + 5
echo $result

# Arithmetic Operators
# +, -, *, /, %
#
# Arithmetic Operators which must be parenthesised
# !, ~, |, &, ^, ~, <<, >>,
# Compare and logical operators
#
# All operators are same as in C.

# It is non so well documented that numeric expressions require spaces
# in-between; Also, `@' has its own parser, it seems that work well when the
# expression is parenthesised otherwise the primary parser seems it is active.
# Parenthesis require spaces around, this is documented.

# wrong
@ x = $y+1
@ x = 0644 & 022;      echo $x
@ x = (0644 & 022) +1; echo $x
@ x = (0644 & 022)+ 1; echo $x
@ x = ( ~077 );        echo $x

# correct
@ x = $y + 1
@ x = ( 0644 & 022 ) + 1; echo $x
@ x = ( ~ 077 );          echo $x
@ x = ( ~ 077 | 022 );    echo $x
@ x = ( ! 0 );            echo $x

# C's operators ++ and -- are supported if there is not assignment
@ result ++

# None shell created to do mathematics;
# Except for the basic operations, use an external command with backslashes.
#
# I suggest the calc as the best option.
# (http://www.isthe.com/chongo/tech/comp/calc/)
#
# The standard Unix's bc as second option
# (https://www.gnu.org/software/bc/manual/html_mono/bc.html)
#
# The standard Unix's AWK as third option
# (https://www.gnu.org/software/gawk/manual/gawk.html)

# You can also use `perl', `php' or even several BASICs, but prefer the
# above utilities for faster load-and-run results.

# real example: (that I answer in StackExchange)
# REQ: x := 1001b OR 0110b

# in `tcsh' expression (by using octal)
@ x = ( 011 | 06 ); echo $x

# the same by using `calc' (and using binary as the original req)
set x = `calc '0b1001 | 0b110'`; echo $x

# --- File Inquiry Operators --------------------------------------------------
# NOTE: The builtin `filetest' command do the same thing.

#### Boolean operators
# -r  read access    -w  write access    -x  execute access    -e  existence
# -f  plain file     -d  directory       -l  symbolic link     -p  named pipe
# -S  socket file
# -o  ownership      -z  zero size       -s  non-zero size
# -u  SUID is set    -g  SGID is set     -k  sticky is set
# -b  block device   -c  char device
# -t  file (digit) is an open file descriptor for a terminal device

# if the file `README' exists, displays a message
if ( -e README ) echo "I have already README file"

# if the `less' program is installed, use this instead of `more'
if ( -e `where less` ) then
	alias more 'less'
endif

#### Non-boolean operators
# -Z  returns the file size in bytes
# -M  returns the modification time (mtime)    -M: returns mtime string
# -A  returns the lass access time (atime)     -A: returns atime string
# -U  returns the owners user ID               -U: returns the owners user-name
# -G  returns the group ID                     -G: returns the group-name
# -P  returns the permissions as octal number  -Pmode returns perm. AND mode

# this will display the date as Unix-time integer: 1498511486
filetest -M README.md

# This will display "Tue Jun 27 00:11:26 2017"
filetest -M: README.md

# --- Basic Commands ----------------------------------------------------------

# Navigate though file system with `chdir' (cd)
cd path # change working directory
cd      # change to home directory
cd -    # change to previous directory
cd ..   # go up one directory

# Examples:
cd ~/Downloads # go to my `Downloads' directory

# Use `mkdir` to create new directories.
mkdir newdir
# The `-p` flag causes new intermediate directories to be created as necessary.
mkdir -p ~/.backup/saves

# which & where
# find if csh points to tcsh
ls -lha `which csh`
# find if csh is installed on more than one directory
where csh

# --- Pipe-lines --------------------------------------------------------------
# A pipeline is a sequence of processes chained together by their standard
# streams, so that the output of each process (stdout) feeds directly as input
# (stdin) to the next one. This `pipes' are created with the `|' special
# character and it is one of the most powerful characteristics of Unix.

# example:
ls -l | grep key | less
# "ls -l" produces a process, the output (stdout) of which is piped to the
# input (stdin) of the process for "grep key"; and likewise for the process
# for "less".

# the `ls', the `grep' and the `less' are programs of Unix and they have their
# own man-page. The `pipe' mechanism is part of the kernel but the syntax
# and the control is job of the shell, the tcsh in our case.

# NOTE: `pipe' mechanism has Windows too, but it is buggy and I sign it for all
# versions until Windows XP SP3 API32 which was the last one that I worked on.
# Microsoft still denied it but is well known bug since it is a common method
# for inter-process communication. For small I/O it will work well.
# tcsh, along with grep, gcc and perl is one of the first Unix programs that
# ported to DOS (with EMX DOS extender) and later to Windows (1998).

# example: this will convert tcsh to PostScript and will show it with okular
zcat /usr/man/man1/tcsh.1.gz | groff -Tps -man | okular -

# a better version
zcat `locate -b -n 1 '\tcsh.1.gz'` | groff -Tps -man | okular -

# even better
set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
 zcat `eval $loc` | groff -Tps -man | okular -

# the same, modified to create man page pdf
set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
 zcat `eval $loc` | groff -Tps -man | ps2pdf - ${page}.pdf

# the same, but now shows the ${page}.pdf too
set page = tcsh; set loc = (locate -b -n 1 "\\\\"${page}".1.gz");
 zcat `eval $loc` | groff -Tps -man | ps2pdf - ${page}.pdf && okular tcsh.pdf

# NOTE: `okular' is the default application of KDE environment and it shows
# postcript and pdf files. You can replace it with your lovely pdf viewer.
# zcat, locate, groff, are common programs in all Unices. `ps2pdf' program
# is part of `ghostscript' package that is widely used.

# --- Control Flow ------------------------------------------------------------

#### IF-THEN-ELSE-ENDIF
# Syntax:
# if ( expr ) then
#    ...
# [else if ( expr2 ) then
#    ...]
# [else
#    ...]
# endif
#
# If the specified expr is true then the commands to the first else are
# executed; otherwise if expr2 is true then the commands to the second else
# are executed, etc.
# Any number of else-if pairs are possible; only one endif is needed.
#
# Single-line form:
#
# if ( expr ) command
#
# If `expr' evaluates true, then command is executed.
# `command' must be a simple command, not an alias, a pipeline, a command list
# or a parenthesized command list. With few words, avoid to use it.
#
# BUG: Input/output redirection occurs even if expr is false and command is
# thus not executed.
#

# check if we are in non-interactive shell and quit if true
if ( $?USER == 0 || $?prompt == 0 ) exit

# check if we are a login shell
if ( $?loginsh ) then
	# check if you are on linux console (not X's terminal)
	if ( $tty =~ tty* ) then
		# enable keypad application keys (man console_codes)
		echo '\033='
	endif
endif

#### SWITCH-ENDSW
# Syntax:
# switch ( expr )
# case pattern:
#     ...
#     [breaksw]
# [default:
#     ...]
# endsw
#
# tcsh uses a case statement that works similarly to switch in C.
# Each case label is successively matched, against the specified string which
# is first command and filename  expanded. The file  metacharacters `*', `?'
# and `[...]' may be used in the case labels. If none of the labels match the
# execution begins after the default label if its defined.
# The command `breaksw' causes execution to continue after the endsw. Otherwise
# control may fall through case labels and default labels as in C.

switch ( $var )
case *.[1-9]:
case *.[1-9].gz:
	echo "$var is a man-page."
	breaksw
case *gz:
	echo "$var is gzipped"
	breaksw
default:
	file $var
endsw

#### FOREACH-END
# Syntax:
# foreach name ( wordlist )
#	...
#   [break | continue]
# end
#
# Successively sets the variable `name' to each member of `wordlist' and
# executes the sequence of commands between this command and the matching
# `end' keyword. The `continue' keyword jump to the next element back to
# top; and the `break' keyword terminates the loop.
#
# BUG: `foreach' doesn't ignore here documents when looking for its end.

# example: counting 1 to 10
foreach i ( `seq 1 10` )
    echo $i
end

# example: type all files in the list
foreach f ( a.txt b.txt c.txt )
	cat $f
end

# example: convert wma to ogg
foreach f ( *.wma )
	ffmpeg -i "$f" "$f:r".ogg
end

#### WHILE-END
# while ( expr )
#     ...
#     [break | continue]
# end
#
# Executes the commands between the `while' and the matching `end' while `expr'
# evaluates non-zero. `break' and `continue' may be used to terminate or
# continue the loop prematurely.

# count from 1 to 10
set num = 1
while ( $num <= 10 )
	echo $num
	@ num ++
end

# print all directories of CWD
set lst = ( * )
while ( $#lst )
	if ( -d $lst[1] ) echo $lst[1] is directory
	shift lst
end

# separate command-line arguments to options or parameters
set options
set params
set lst = ( $* )
while ( $#lst )
	if ( "$lst[1]" =~ '-*' ) then
		set options = ( $options $lst[1] )
	else
		set params = ( $params $lst[1] )
	endif
	shift lst
end
echo 'options =' $options
echo 'parameters =' $params

#### REPEAT
# Syntax: repeat count command
#
# The specified command, which is subject to the same restrictions as the
# command in the one line if statement above, is executed count times.
# I/O redirections occur exactly once, even if count is 0.
#
# TIP: in most cases prefer `while'

repeat 3 echo "ding dong"

# --- Functions ---------------------------------------------------------------
# tcsh has no functions but its expression syntax is advanced enough to use
# `alias' as functions. Another method is recursion

# Alias argument selectors; the ability to define an alias to take arguments
# supplied to it and apply them to the commands that it refers to.
# Tcsh is the only shell that provides this feature.
#
# \!#   argument selector for all arguments, including the alias/command
#       itself; arguments need not be supplied.
# \!*   argument selector for all arguments, excluding the alias/command;
#       arguments need not be supplied.
# \!$   argument selector for the last argument; argument need not be supplied,
#       but if none is supplied, the alias name is considered to be the
#       last argument.
# \!^   argument selector for first argument; argument MUST be supplied.
# \!:n  argument selector for the nth argument; argument MUST be supplied;
#       n=0 refers to the alias/command name.
# \!:m-n   argument selector for the arguments from the mth to the nth;
#       arguments MUST be supplied.
# \!:n-$   argument selector for the arguments from the nth to the last;
#       at least argument n MUST be supplied.

# Alias the cd command so that when you change directories, the contents
# are immediately displayed.
alias cd 'cd \!* && ls'

# --- Recursion method --- begin ---
#!/bin/tcsh -f
set todo = option1
if ( $#argv > 0 ) then
	set todo = $argv[1]
endif

switch ( $todo )
case option1:
#	...
	$0 results
	breaksw
case option2:
#	...
	$0 results
	breaksw
case results:
	echo "print the results here"
#	...
	breaksw
default:
	echo "Unknown option: $todo"
#	exit 0
endsw
# --- Recursion method --- end ---

# --- examples ----------------------------------------------------------------

# this script prints available power-states if no argument is set;
# otherwise it set the state of the $argv[1]
# --- power-state script --- begin --------------------------------------------
#!/bin/tcsh -f
# get parameter ("help" for none)
set todo = help
if ( $#argv > 0 ) then
	set todo = $argv[1]
endif
# available options
set opts = `cat /sys/power/state`
# is known?
foreach o ( $opts )
	if ( $todo == $o ) then
		# found; execute it
		echo -n $todo > /sys/power/state
		break
	endif
end
# print help and exit
echo "usage: $0 [option]"
echo "available options on kernel: $opts"
# --- power-state script --- end ----------------------------------------------

# Guess the secret number game
# --- secretnum.csh --- begin -------------------------------------------------
#!/bin/tcsh -f
set secret=`shuf -i1-100 -n1`
echo "I have a secret number from 1 up to 100"
while ( 1 )
	echo -n "Guess: "
	set guess = $<
	if ( $secret == $guess ) then
		echo "You found it"
		exit 1
	else
		if ( $secret > $guess ) then
			echo "its greater"
		else if ( $secret < $guess ) then
				echo "its lesser"
			endif
		endif
	endif
end
# --- secretnum.csh --- end ---------------------------------------------------

# -----------------------------------------------------------------------------
# Appendices

#### About [T]CSH:
# * CSH is notorious about its bugs;
# * It was also famous about its advanced interactive mode.
# * TCSH is famous that have the most advanced completition subsystem.
# * TCSH is famous that have the most advanced aliases subsystem; aliases
#   can take parameters and often used as functions!
# * TCSH is well known that preferred by people  (me too) because of better
#   syntax. All shells are using Thomson's syntax with exception of [t]csh,
#   fish and plan9's shells (rc, ex).
# * It is smaller and consume far less memory than bash, zsh even mksh!
#   (memusage reports)
# * TCSH still has bugs; less but have; if you write readable clean code you'll
#   find none; well almost none... This has to do with the implementation of
#   csh; that no means the other shells has good implementation.
# * no one well known shell is capable for regular programming; if your script
#   getting big, use a programming language, or at least PHP or Perl (good
#   script languages).
#
# Advises:
# 1. Do not use redirection in single-line if (it is well documented bug)
#    In most cases avoid to use single-line IFs.
# 2. Do not mess up with other shells code, c-shell is not compatible with
#    other shells and has different abilities and priorities.
# 3. Use spaces as you'll use them to write readable code in any language.
#    A bug of csh was `set x=1' worked, `set x = 1' worked, `set x =1' did not!
# 4. It is well documented that numeric expressions require spaces in-between;
#    also parenthesise all bit-wise and unary operators.
# 5. Do not write a huge weird expression with several quotes, backslashes etc
#    It is bad practice for generic programming, it is dangerous in any shell.
# 6. Help tcsh, report the bug here <https://bugs.gw.com/>
# 7. Read the man page, `tcsh' has huge number of options, and variables.
#
#    I suggest the following options enabled by default
#    --------------------------------------------------
# Even in non-interactive shells
#    set echo_style=both
#    set backslash_quote
#    set parseoctal
#    unset noclobber
#
# Whatever...
#    set inputmode=insert
#    set autolist
#    set listjobs
#    set padhour
#    set color
#    set colorcat
#    set nobeep
#    set cdtohome
#
#    set histdup
#    set histlit
#    set nohistclop
#
#    unset compat_expr
#    unset noglob
#    unset autologout
#    unset time
#    unset tperiod
#
# NOTE: If the `backslash_quote' is set, it may create compatibility issues
# with other tcsh scripts which was written without it.
#
# NOTE: The same for `parseoctal', but it is better to fix the problematic
# scripts.
#
# NOTE: **for beginners only**
# This enable automatically rescan `path' directories if need to. (like bash)
#    set autorehash

#### common aliases
#    alias hist  'history 20'
#    alias ll    'ls --color -lha'
#    alias today "date '+%d%h%y'
#    alias ff    'find . -name '

#### a nice prompt
#    set prompt = "%B%{\033[35m%}%t %{\033[32m%}%n@%m%b %C4 %# "
```
---
category: tool
tool: tmux
contributors:
    - ["mdln", "https://github.com/mdln"]
filename: LearnTmux.txt
---


[tmux](http://tmux.github.io)
is a terminal multiplexer: it enables a number of terminals
to be created, accessed, and controlled from a single screen. tmux
may be detached from a screen and continue running in the background
then later reattached.


```

  tmux [command]     # Run a command
                     # 'tmux' with no commands will create a new session

    new              # Create a new session
     -s "Session"    # Create named session
     -n "Window"     # Create named Window
     -c "/dir"       # Start in target directory

    attach           # Attach last/available session
     -t "#"          # Attach target session
     -d              # Detach the session from other instances

    ls               # List open sessions
     -a              # List all open sessions

    lsw              # List windows
     -a              # List all windows
     -s              # List all windows in session

    lsp              # List panes
     -a              # List all panes
     -s              # List all panes in session
     -t              # List all panes in target

    kill-window      # Kill current window
     -t "#"          # Kill target window
     -a              # Kill all windows
     -a -t "#"       # Kill all windows but the target

    kill-session     # Kill current session
     -t "#"          # Kill target session
     -a              # Kill all sessions
     -a -t "#"       # Kill all sessions but the target

```


### Key Bindings

The method of controlling an attached tmux session is via key
combinations called 'Prefix' keys.

```
----------------------------------------------------------------------
  (C-b) = Ctrl + b    # 'Prefix' combination required to use keybinds

  (M-1) = Meta + 1 -or- Alt + 1
----------------------------------------------------------------------

  ?                  # List all key bindings
  :                  # Enter the tmux command prompt
  r                  # Force redraw of the attached client
  c                  # Create a new window

  !                  # Break the current pane out of the window.
  %                  # Split the current pane into two, left and right
  "                  # Split the current pane into two, top and bottom

  n                  # Change to the next window
  p                  # Change to the previous window
  {                  # Swap the current pane with the previous pane
  }                  # Swap the current pane with the next pane

  s                  # Select a new session for the attached client
                     interactively
  w                  # Choose the current window interactively
  0 to 9             # Select windows 0 to 9

  d                  # Detach the current client
  D                  # Choose a client to detach

  &                  # Kill the current window
  x                  # Kill the current pane

  Up, Down           # Change to the pane above, below, left, or right
  Left, Right

  M-1 to M-5         # Arrange panes:
                       # 1) even-horizontal
                       # 2) even-vertical
                       # 3) main-horizontal
                       # 4) main-vertical
                       # 5) tiled

  C-Up, C-Down       # Resize the current pane in steps of one cell
  C-Left, C-Right

  M-Up, M-Down       # Resize the current pane in steps of five cells
  M-Left, M-Right

```


### Configuring ~/.tmux.conf

tmux.conf can be used to set options automatically on start up, much
like how .vimrc or init.el are used.

```
# Example tmux.conf
# 2015.12


### General
###########################################################################

# Scrollback/History limit
set -g history-limit 2048

# Index Start
set -g base-index 1

# Mouse
set-option -g -q mouse on

# Force reload of config file
unbind r
bind r source-file ~/.tmux.conf


### Keybinds
###########################################################################

# Unbind C-b as the default prefix
unbind C-b

# Set new default prefix
set-option -g prefix `

# Return to previous window when prefix is pressed twice
bind C-a last-window
bind ` last-window

# Allow swapping C-a and ` using F11/F12
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Keybind preference
setw -g mode-keys vi
set-option -g status-keys vi

# Moving between panes with vim movement keys
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Window Cycle/Swap
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# Easy split pane commands
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# Activate inner-most session (when nesting tmux) to send commands
bind a send-prefix


### Theme
###########################################################################

# Statusbar Color Palatte
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# Pane Border Color Palette
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Message Color Palette
set-option -g message-fg black
set-option -g message-bg green

# Window Status Color Palette
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### UI
###########################################################################

# Notification
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# Automatically set window titles
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # window number,program name,active (or not)

# Statusbar Adjustments
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# Show performance counters in statusbar
# Requires https://github.com/thewtex/tmux-mem-cpu-load/
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```


### References

[Tmux | Home](http://tmux.github.io)

[Tmux Manual page](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)

[Display CPU/MEM % in statusbar](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)

[tmuxinator - Manage complex tmux sessions](https://github.com/tmuxinator/tmuxinator)
---
language: toml
filename: learntoml.toml
contributors:
  - ["Alois de Gouvello", "https://github.com/aloisdg"]
---

TOML stands for Tom's Obvious, Minimal Language. It is a data serialisation language designed to be a minimal configuration file format that's easy to read due to obvious semantics.

It is an alternative to YAML and JSON. It aims to be more human friendly than JSON and simpler that YAML. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.

Be warned, TOML's spec is still changing a lot. Until it's marked as 1.0, you
should assume that it is unstable and act accordingly. This document follows TOML v0.4.0. 

```toml
# Comments in TOML look like this.

################
# SCALAR TYPES #
################

# Our root object (which continues for the entire document) will be a map,
# which is equivalent to a dictionary, hash or object in other languages.

# The key, equals sign, and value must be on the same line
# (though some values can be broken over multiple lines).
key = "value"
string = "hello"
number = 42
float = 3.14
boolean = true
dateTime = 1979-05-27T07:32:00-08:00
scientificNotation = 1e+12
"key can be quoted" = true # Both " and ' are fine
"key may contains" = "letters, numbers, underscores, and dashes"

# A bare key must be non-empty, but an empty quoted key is allowed
"" = "blank"     # VALID but discouraged
'' = 'blank'     # VALID but discouraged

##########
# String #
##########

# All strings must contain only valid UTF-8 characters.
# We can escape characters and some of them have a compact escape sequence.
# For example, \t add a tabulation. Refers to the spec to get all of them.
basicString = "are surrounded by quotation marks. \"I'm quotable\". Name\tJos"

multiLineString = """
are surrounded by three quotation marks
on each side and allow newlines."""

literalString = 'are surrounded by single quotes. Escaping are not allowed.'

multiLineLiteralString = '''
are surrounded by three single quotes on each side
and allow newlines. Still no escaping.
The first newline is trimmed in raw strings.
   All other whitespace
   is preserved. #! are preserved?
'''

# For binary data it is recommended that you use Base64, another ASCII or UTF8
# encoding. The handling of that encoding will be application specific.

###########
# Integer #
###########

## Integers can start with a +, a - or nothing.
## Leading zeros are not allowed. Hex, octal, and binary forms are not allowed.
## Values that cannot be expressed as a series of digits are not allowed.
int1 = +42
int2 = 0
int3 = -21
integerRange = 64

## You can use underscores to enhance readability. Each
## underscore must be surrounded by at least one digit.
int4 = 5_349_221
int5 = 1_2_3_4_5     # VALID but discouraged

#########
# Float #
#########

# Floats are an integer followed by a fractional and/or an exponent part.
flt1 = 3.1415
flt2 = -5e6
flt3 = 6.626E-34

###########
# Boolean #
###########

bool1 = true
bool2 = false
boolMustBeLowercase = true

############
# Datetime #
############

date1 = 1979-05-27T07:32:00Z # follows the RFC 3339 spec
date2 = 1979-05-27T07:32:00 # without offset
date3 = 1979-05-27 # without offset nor time

####################
# COLLECTION TYPES #
####################

#########
# Array #
#########

array1 = [ 1, 2, 3 ]
array2 = [ "Commas", "are", "delimiters" ]
array3 = [ "Don't mixed", "different", "types" ]
array4 = [ [ 1.2, 2.4 ], ["all", 'strings', """are the same""", '''type'''] ]
array5 = [
  "Whitespace", "is", "ignored"
]

#########
# Table #
#########

# Tables (or hash tables or dictionaries) are collections of key/value
# pairs. They appear in square brackets on a line by themselves.
# Empty tables are allowed and simply have no key/value pairs within them.
[table]

# Under that, and until the next table or EOF are the key/values of that table.
# Key/value pairs within tables are not guaranteed to be in any specific order.
[table-1]
key1 = "some string"
key2 = 123

[table-2]
key1 = "another string"
key2 = 456

# Dots are prohibited in bare keys because dots are used to signify nested tables.
# Naming rules for each dot separated part are the same as for keys.
[dog."tater.man"]
type = "pug"

# In JSON land, that would give you the following structure:
# { "dog": { "tater.man": { "type": "pug" } } }

# Whitespace around dot-separated parts is ignored, however, best practice is to
# not use any extraneous whitespace.
[a.b.c]            # this is best practice
[ d.e.f ]          # same as [d.e.f]
[ j . "ʞ" . 'l' ]  # same as [j."ʞ".'l']

# You don't need to specify all the super-tables if you don't want to. TOML knows
# how to do it for you.
# [x] you
# [x.y] don't
# [x.y.z] need these
[x.y.z.w] # for this to work

# As long as a super-table hasn't been directly defined and hasn't defined a
# specific key, you may still write to it.
[a.b]
c = 1

[a]
d = 2

# You cannot define any key or table more than once. Doing so is invalid.

# DO NOT DO THIS
[a]
b = 1

[a]
c = 2

# DO NOT DO THIS EITHER
[a]
b = 1

[a.b]
c = 2

# All table names must be non-empty.
[]     # INVALID
[a.]   # INVALID
[a..b] # INVALID
[.b]   # INVALID
[.]    # INVALID

################
# Inline table #
################

inlineTables = { areEnclosedWith = "{ and }", mustBeInline = true }
point = { x = 1, y = 2 }

###################
# Array of Tables #
###################

# An array of tables can be expressed by using a table name in double brackets.
# Each table with the same double bracketed name will be an item in the array.
# The tables are inserted in the order encountered.

[[products]]
name = "array of table"
sku = 738594937
emptyTableAreAllowed = true

[[products]]

[[products]]
name = "Nail"
sku = 284758393
color = "gray"

# You can create nested arrays of tables as well. Each double-bracketed
# sub-table will belong to the nearest table element above it.

[[fruit]]
  name = "apple"

  [fruit.Geometry]
    shape = "round"
    note = "I am an fruit's property"

  [[fruit.color]]
    name = "red"
    note = "I am an array's item in apple"

  [[fruit.color]]
    name = "green"
    note = "I am in the same array than red"

[[fruit]]
  name = "banana"

  [[fruit.color]]
    name = "yellow"
    note = "I am an array's item too but banana's one"
```

In JSON land, this code will be:

```json
{
  "fruit": [
    {
      "name": "apple",
      "geometry": { "shape": "round", "note": "..."},
      "color": [
        { "name": "red", "note": "..." },
        { "name": "green", "note": "..." }
      ]
    },
    {
      "name": "banana",
      "color": [
        { "name": "yellow", "note": "..." }
      ]
    }
  ]
}
```

### More Resources

+ [TOML official repository](https://github.com/toml-lang/toml)
---
language: bf
filename: brainfuck-tr
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io"]
translators:
    - ["Haydar KULEKCI", "http://scanf.info/"]
lang: tr-tr
---

Brainfuck (normalde brainfuck olarak bütün harfleri küçük olarak yazılır.) 
son derece minimal bir programlama dilidir. (Sadece 8 komut) ve tamamen 
Turing'dir.

```
"><+-.,[]" (tırnak işaretleri hariç) karakterleri dışındaki her karakter
gözardı edilir.

Brainfuck 30,000 hücresi olan ve ilk değerleri sıfır olarak atanmış bir
dizidir. İşaretçi ilk hücreyi işaret eder.

Sekiz komut vardır:
+ : Geçerli hücrenin değerini bir artırır.
- : Geçerli hücrenin değerini bir azaltır.
> : Veri işaretçisini bir sonraki hücreye hareket ettirir(sağdaki hücreye).
< : Veri işaretçisini bir önceki hücreye hareket ettirir(soldaki hücreye).
. : Geçerli hücrenin ASCII değerini yazdırır (örn: 65 = 'A').
, : Bir girdilik karakteri aktif hücre için okur.
[ : Eğer geçerli hücredeki değer sıfır ise, ]ifadesine atlar.
    Diğer durumlarda bir sonraki yönergeye geçer.
] : Eğer geçerli hücredeki değer sıfır ise, bir sonraki yönergeye geçer.
    Diğer durumlarda, [ ifadesine karşılık gelen yönergelere döner.

[ ve ] bir while döngüsü oluşturur. Açıkça, dengeli olmalıdırlar.

Basit bir brainfuck programına göz atalım.

++++++ [ > ++++++++++ < - ] > +++++ .

Bu program 'A' karaterini ekrana basar. İlk olarak, #1'inci hücre 6'ya artırılır.
#1'inci hücre döngü için kullanılacaktır. Sonra, ([) döngüsüne girilir ve
#2'inci hücreye hareket edilir. #2'inci hücre 10 kez artırılır, #1'inci hücreye
geri dönülür. #1 hücresini bir azaltır. Bu döngü 6 kez gerçekleşir. (Bu 6 kez
azaltmak demektir, #1 hücresi 0 değerini alır ve bu noktada ] ifadesini atlar).

Bu noktada, biz #1 hücresindeyiz, değeri şu anda 0 ve #2 hücresinin değeri
60'tır. Biz #2 hücresine hareket diyoruz ve bu hücreyi 5 defa artırıyoruz.
#2'nin şu anki değeri 65 olur. Sonra #2 hücresinin ASCII karşılığını
yazdırıyoruz. 65 değerinin ASCII karşılığı 'A'dır. Ekrana 'A' yazılacaktır.


, [ > + < - ] > .

Bu program kullanıcıdan bir girdi okur, ve karakteri bir diğer hücreye yazdırır,
ve daha sonra aynı karakteri ekrana yazdırır.

, ifadesi kullanıcıdan karakteri #1 hücresine okur. Sonra bir döngü
başlar. #2 hücresine hareket edilir, #2 hücresinin değeri bir artırılır, #1
hücresine geri dönülür, ve #1 hücresinin değer bir azaltılır. Bu #1 hücresinin
değeri 0 olana kadar devam eder ve #2 hücresi #1'in eski değerini tutar. Çünkü
biz #1 hücresindeki verileri döngü süresince #2 hücresine taşıyoruz, ve sonunda
#2 hücresinin ASCII değerini yazdırıyoruz.

Boşluk karakteri sadece okunabilirliği artırmak içindir. Aşağıdaki gibi de
yazabilirsiniz.

,[>+<-]>.


Bu uygulamanın ne yaptığına bakalım:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

Bu program 2 sayı alır, ve birbiri ile çarpar.

Özetle, ilk olarak iki girdi alır. Sonra, #1 hücresinde şarta bağlı harici bir
döngü başlar. Sonra #2 ye hareket edilir, ve içerde #2 hücresine bağlı bir döngü
daha başlar ve #3 hücresinin değerini artırır. Ama, Bir problem vardır: iç
döngünün sonunda #2'inci hücrenin değeri 0 olacaktır. Bunu çözmek için #4
hücresinin de değerini yükseltiyoruz, ve sonra #4 hücresinin değerini #2'ye
kopyalıyoruz.
```

İşte brainfuck. Zor değil değil mi? Eğlenmek için kendi programınızı
yazabilirsiniz, veya farklı bir dilde brainfuck yorumlayıcısı yazabilirsiniz.
Yorumlayıcı oldukça basittir, ama mazoşist iseniz, brainfuck içerisinde bir
brainfuck yorumlayıcısı yazmayı deneyebilirsiniz.
---
name: c
category: language
language: c
filename: learnc-tr.c
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Haydar KULEKCI", "http://scanf.info/"]
lang: tr-tr

---
/*
C halen modern yüksek performans bilgisayarların dili.

C bir çok programcının kullandığı en düşük seviye dillerdendir, ama
salt hız ile daha fazlasını karşılar. C'nin bellek yönetiminden iyi
anlarsanız sizi isteğiniz yere götürecektir.

```c
// Tek satır yorum // karakterleri ile başlar

/*
Çoklu satırlı yorumlar bu şekilde görünür.
*/

// C Standart kütüphanelerini uygulamanıza #include<ornek.h> ile 
// dahil edebilirsiniz.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Kendi başlık(header) dosyalarınız dahil etmek için "çift tırnak" 
// kullanmalısınız.
#include "my_header.h"

// Fonksiyonlarınızı bir .h dosyasında ya da c dosyanızın üst tarafta 
// tanımlayın.

void function_1();
void function_2();

// Programınızın giriş noktası main isimli bir fonksiyondur ve 
// integer değer döner
int main() {

    // çıktıları yazdırmak için printf kullanılır, "print formatted"
    // %d bir sayı tipidir, \n yeni satır karakteridir
    printf("%d\n", 0); // => 0 karakteri yazdırılır.
    // Tüm ifadeler noktalı virgül ile bitmelidir.

    ///////////////////////////////////////
    // Tipler
    ///////////////////////////////////////

    // Değişkenleri kullanmadan önce tanımlamalısınız. Bir değişken tanımlarken
    // tipini belirtmelisiniz; bu tip onun byte olarak boyutunu belirler.

    // int değişken tipi 4 byte boyutundadır.
    int x_int = 0;

    // short değişken tipi genellikle 2 byte boyutundadır.
    short x_short = 0;

    // char tipi 1 byte boyutunu garanti eder. 
    char x_char = 0;
    char y_char = 'y'; // Karakterler '' işaretleri arasına yazılır.

    // long tipi 4-8 byte olur; long long tipi en azından 64 bit garantiler.
    long x_long = 0;
    long long x_long_long = 0; 

    // float tipi 32-bit kayan noktalı sayı boyutundadır.
    float x_float = 0.0;

    // double değişken tipi 64-bit kayan noktalı yazı tipindedir. 
    double x_double = 0.0;

    // Integral türleri işaretsiz olabilir. Bunun anlamı, onlar eksi değer 
    // olamaz demektir, ama aynı boyuttaki işaretsiz bir sayının maksimum 
    // değeri işaretli bir sayının maksimum değeriden büyük olur.
    unsigned char ux_char;
    unsigned short ux_short;
    unsigned int ux_int;
    unsigned long long ux_long_long;

    // Diğer taraftan char, ki her zaman bir byte boyutundadır, bu tipler 
    // makinenize göre boyut değiştirir. sizeof(T) size bir değişkenin byte 
    // cinsinden boyutunu verir öyle ki bu tipin boyutunu taşınabilir bir 
    // şekilde ifade edilebilir
    // Örneğin,
    printf("%lu\n", sizeof(int)); // => 4 (bir çok makinede 4-byte words)

    // Eger arguman düzenli ifae olan sizeof operatoru ise degerlendirilmez.
    // VLAs hariç asagiya bakiniz).
    // Bu durumda verimliligin degeri derleme-zamani sabitidir.
    int a = 1;

    // size_t bir objeyi temsil etmek için kullanılan 2 byte uzunluğundaki bir 
    // işaretsiz tam sayı tipidir

    size_t size = sizeof(a++); // a++ is not evaluated
    printf("sizeof(a++) = %zu where a = %d\n", size, a);
    // yazdirilan "sizeof(a++) = 4 where a = 1" (32-bit mimaride)

    // Diziler somut bir boyut ile oluşturulmalıdır.
    char my_char_array[20]; // Bu dizi 1 * 20 = 20 byte alan kaplar
    int my_int_array[20]; // Bu dizi 4 * 20 = 80 byte alan kaplar
                          // (4-byte bir word varsayılır)

    // Şu şekilde bir diziyi 0 ile oluşturabilirsiniz:
    char my_array[20] = {0};

    // Dizinin elemanlarını indexlemek diğer diller gibidir, veya 
    // diğer diller C gibi.
    my_array[0]; // => 0

    // Diziler değişebilirdir (mutable); O sadece memory!
    my_array[1] = 2;
    printf("%d\n", my_array[1]); // => 2

    // C99'da (ve C11 istege bagli bir ozellik olarak), değidken-uzunluklu diziler (VLAs) bildirilebilirler.
    // Böyle bir dizinin boyuunu derlenmesi gerekmez
    // zaman sabiti:
    printf("Enter the array size: "); // dizi boyutu kullaniciya soruluyor
    char buf[0x100];
    fgets(buf, sizeof buf, stdin);

    // strtoul isaretsiz integerlar icin string ayiricisidir.
    size_t size = strtoul(buf, NULL, 10);
    int var_length_array[size]; // declare the VLA
    printf("sizeof array = %zu\n", sizeof var_length_array);

    // Bu programın olası bir sonucu olabilir:
    // > Enter the array size: 10
    // > sizeof array = 40

    // String'ler bir NUL (0x00) byte ile sonlandırılmış karakter dizileridir,
    // bu string içerisinde özel bir karakter olan '\0' ile gösterilir.
    // (Biz Nul byte'i string karakterleri arasında bulundurmamıza gerek 
    // yoktur; derleyici onu bizim için dizinin sonuna ekler.)
    char a_string[20] = "This is a string";
    printf("%s\n", a_string); // %s bir string formatıdır.

    /*
    a_string 16 karakter uzunluğundadır. 
    17. karakter NUL karakteridir.
    18., 19. ve 20. karakterler tanımsızdır.(undefined)
    */

    printf("%d\n", a_string[16]); // => 0
    // i.e., byte #17 is 0 (as are 18, 19, and 20)

    // Tek tirnak arasinda karakterlere sahipsek, bu karakterler degismezdir.
    // Tip `int` ise, `char` *degildir* (tarihsel sebeplerle).
    int cha = 'a'; // fine
    char chb = 'a'; // fine too (implicit conversion from int to char)

    ///////////////////////////////////////
    // Operatörler
    ///////////////////////////////////////

    int i1 = 1, i2 = 2; // Çoklu tanımlama için kısayol.
    float f1 = 1.0, f2 = 2.0;

    // Aritmatik basittir.
    i1 + i2; // => 3
    i2 - i1; // => 1
    i2 * i1; // => 2
    i1 / i2; // => 0 (0.5'dir ama 0 a yuvarlanmıştır.)

    f1 / f2; // => 0.5, artı veya eksi epsilon

    // Modüler aritmetikte vardır.
    11 % 3; // => 2

    // Karşılaştırma operatörleri muhtemelen tanıdıktır, ama
    // C'de boolean tipi yoktur. Bunun yerine sayı(int) kullanırız.
    // 0 false yerine ve diğer herşey true yerine geçmektedir. 
    // (Karşılaştırma operatörleri her zaman 0 veya 1 dönmektedir.)
    3 == 2; // => 0 (false)
    3 != 2; // => 1 (true)
    3 > 2; // => 1
    3 < 2; // => 0
    2 <= 2; // => 1
    2 >= 2; // => 1

    // Sayılar üzerinde mantık işlemleri
    !3; // => 0 (Logical not)
    !0; // => 1
    1 && 1; // => 1 (Logical and)
    0 && 1; // => 0
    0 || 1; // => 1 (Logical or)
    0 || 0; // => 0

    // Bit boyutunda işlem yapmak için operatörler
    ~0x0F; // => 0xF0 (bitwise negation)
    0x0F & 0xF0; // => 0x00 (bitwise AND)
    0x0F | 0xF0; // => 0xFF (bitwise OR)
    0x04 ^ 0x0F; // => 0x0B (bitwise XOR)
    0x01 << 1; // => 0x02 (bitwise left shift (by 1))
    0x02 >> 1; // => 0x01 (bitwise right shift (by 1))

    // Isaretli sayilari kaydirirken dikkatli olun - tanimsizlar sunlardir:
    // - isaretli sayinin isaret bitinde yap?ilan kaydirma (int a = 1 << 32)
    // - negatif sayilarda sol kaydirma (int a = -1 << 2)
    // - LHS tipinde >= ile olan ofset genisletmelerde yapilan kaydirma:
    //   int a = 1 << 32; // UB if int is 32 bits wide

    ///////////////////////////////////////
    // Kontrol Yapıları
    ///////////////////////////////////////

    if (0) {
      printf("I am never run\n");
    } else if (0) {
      printf("I am also never run\n");
    } else {
      printf("I print\n");
    }

    // While Döngüsü
    int ii = 0;
    while (ii < 10) {
        printf("%d, ", ii++); // ii++, ii değişkenini değerini kullandıktan sonra artırır.
    } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    int kk = 0;
    do {
        printf("%d, ", kk);
    } while (++kk < 10); // ++kk, kk değişkeninin değerini kullanmadan önce artırır.
    // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    // For Döngüsü
    int jj;
    for (jj=0; jj < 10; jj++) {
        printf("%d, ", jj);
    } // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

    printf("\n");

    ///////////////////////////////////////
    // Tip Dönüşümleri
    ///////////////////////////////////////

    // C'de her değer bir tipe sahiptir, ama siz bir değeri bir başka tipe 
    // dönüştürebilirsiniz.

    int x_hex = 0x01; // Hex literatüründe değer atayabilirsiniz.

    // Türler arasındaki dönüşümde kendi değerini korumak için çalışacaktır.
    printf("%d\n", x_hex); // => Prints 1
    printf("%d\n", (short) x_hex); // => Prints 1
    printf("%d\n", (char) x_hex); // => Prints 1

    // Tip hiçbir hata vermeden taşacaktır(overflow).
    printf("%d\n", (unsigned char) 257); // => 1 (Max char = 255 eğer karakter 8 bit uzunluğunda ise)

    // `char`, `signed char` ve `unsigned char` karakter tiplerinin maksimum uzunluğunu 
    // belirlemek için <limits.h> kütüphanesindeki CHAR_MAX, SCHAR_MAX ve UCHAR_MAX 
    // macrolarını kullanınız.

    // Integral tipi kayan noktalı yazı tipine dönüştürülecektir ve tam tersi. 
    printf("%f\n", (float)100); // %f formats a float
    printf("%lf\n", (double)100); // %lf formats a double
    printf("%d\n", (char)100.0);

    ///////////////////////////////////////
    // İşaretçiler (Pointers)
    ///////////////////////////////////////

    // Bir işaretci bellek adresini barındıran bir değişkendir. Tanımlaması ile işaret 
    // edeceği verinin tipi de belirtilecektir. Değişkenlerininzi bellek adreslerini 
    // getirerek bellek ile ilgili karışıklığı ortadan kaldırabilirsiniz. 

    int x = 0;
    printf("%p\n", &x); // & işareti bir değişkenin bellek adresini getirmek için kullanılır. 
    // (%p işaretçilerin formatıdır)
    // => Bazı bellek adresleri yazdırılacaktır.


    // İşaretçiler tanımlanırken * ile başlar
    int *px, not_a_pointer; // px sayı tipinde bir işaretçidir. 
    px = &x; // X değişkeninin bellek adresi px değişkeninde tutulmaktadır.
    printf("%p\n", px); // => x değişkeninin bellek adresi yazdırılacaktır. 
    printf("%d, %d\n", (int)sizeof(px), (int)sizeof(not_a_pointer));
    // => 64-bit sistemde "8, 4" yazdırılacaktır.

    // İşaretçinin adres olarak gösterdiği yerdeki değeri almak için 
    // değişkenin önüne * işareti ekleyiniz.
    printf("%d\n", *px); // => 0 bastıracaktır, x in değeridir, 
                         // çünkü px değişkeni x in adresini göstermektedir.

    // Ayrıca siz işaretçinin gösterdiği yerin değerini 
    // değiştirebilirsiniz. Burada referansı parantez içerisinde göstereceğiz 
    // çünkü ++ işleminin önceliği * işleminden yüksektir.
    (*px)++; // px'in işaret ettiği değeri 1 artır. 
    printf("%d\n", *px); // => 1 yazdırılır.
    printf("%d\n", x); // => 1 yazdırılır.

    int x_array[20]; // Diziler(arrays) bellekten yan yana bellek bloklarını 
                     // tahsis etmek için iyi bir yöntemdir.
    int xx;
    for (xx=0; xx<20; xx++) {
        x_array[xx] = 20 - xx;
    } // x_array dizisi 20, 19, 18,... 2, 1 değerleri ile oluşturuluyor.

    // Bir sayı tipinde işaretçi tanımlanıyor ve x_array'i işaret ediyor.
    int* x_ptr = x_array;
    // x_ptr artık dizinin ilk elemanını işaret etmektedir (the integer 20).
    // Bu çalışacaktır çünkü diziler(arrays) aslında sadece onların ilk 
    // elemanlarını gösteren birer işaretçidir.
    // For example, when an array is passed to a function or is assigned to a pointer,
    // it decays into (implicitly converted to) a pointer.
    // Exceptions: when the array is the argument of the `&` (address-od) operator:
    int arr[10];
    int (*ptr_to_arr)[10] = &arr; // &arr is NOT of type `int *`!
                                  // It's of type "pointer to array" (of ten `int`s).
    // or when the array is a string literal used for initializing a char array:
    char arr[] = "foobarbazquirk";
    // or when it's the argument of the `sizeof` or `alignof` operator:
    int arr[10];
    int *ptr = arr; // equivalent with int *ptr = &arr[0];
    printf("%zu %zu\n", sizeof arr, sizeof ptr); // probably prints "40, 4" or "40, 8"

    // Diziler ilk elemanlarını gösteren birer işaretçidirler.
    printf("%d\n", *(x_ptr)); // => 20 yazılacaktır.
    printf("%d\n", x_array[0]); // => 20 yazılacaktır.

    // İşaretçiler kendi tiplerinde artırılır ve azaltılır. 
    printf("%d\n", *(x_ptr + 1)); // => 19 yazılacaktır.
    printf("%d\n", x_array[1]); // => 19 yazılacaktır.

    // Ayrıca dinamik olarak bir bellek bloğunu standart kütüphanede bulunan 
    // malloc fonksiyonu ile uygulamanız için ayırabilirsiniz. Bu fonksiyon 
    // byte türünden ayırmak istediğiniz bloğun boyutunu parametre olarak alır. 
    int* my_ptr = (int*) malloc(sizeof(int) * 20);
    for (xx=0; xx<20; xx++) {
        *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx 'de aynı zamanda çalışabilir
    } // Bellekte 20, 19, 18, 17... 2, 1 (as ints) şeklinde oluşturulmuş olacaktır.

    // Eğer ayrımadığınız bir bellek adresini çağırırsanız
    // öngörülmeyen bir değer dönecektir. 
    printf("%d\n", *(my_ptr + 21)); // => kim-bilir-ne-yazacak?

    // Malloc fonksiyonu ile ayrıdığınız bellek kısmı ile işiniz bittiğinde 
    // onu free fonksiyonu ile boşaltmalısınız, aksi durumda uygulamanız 
    // kapatılana kadar belleğin o kısmını kimse kullanamaz. 
    free(my_ptr);

    // Metin Dizileri(String) birer karakter dizisidir(char array), ama
    // genelde karakter işaretçisi olarak kullanılır.
    char* my_str = "This is my very own string";

    printf("%c\n", *my_str); // => 'T'

    function_1();
} // main fonksiyon sonu

///////////////////////////////////////
// Fonksiyonlar
///////////////////////////////////////

// Fonksiyon tanımlama sözdizimi:
// <return type> <fonksiyon adi>(<args>)

int add_two_ints(int x1, int x2){
    return x1 + x2; // bir değer geri döndürmek için return kullanılır.
}

/*
Fonksiyonlar pass-by-value'dür, ama isterseniz işaretçi referanslarını 
kullanarak fonksiyona gönderilen parametrenin değerini değiştirebilirsiniz. 

Example: Bir metni tersine çevirme
*/

// Bir void konksiyonu hiç bir değer dönmez
void str_reverse(char* str_in){
    char tmp;
    int ii=0, len = strlen(str_in); // Strlen C'nin standart kütüphanesinin bir fonksiyonu
    for(ii=0; ii<len/2; ii++){
        tmp = str_in[ii];
        str_in[ii] = str_in[len - ii - 1]; // sondan ii'inci elemanı
        str_in[len - ii - 1] = tmp;
    }
}

/*
char c[] = "This is a test.";
str_reverse(c);
printf("%s\n", c); // => ".tset a si sihT"
*/

///////////////////////////////////////
// Kullanıcı Tanımlı Tipler ve Yapılar
///////////////////////////////////////

// Typedef'ler bir tip takma adı oluşturur.
typedef int my_type;
my_type my_type_var = 0;

// Struct'lar bir veri koleksiyonudur. 
struct rectangle {
    int width;
    int height;
};

// It's not generally true that
// sizeof(struct rectangle) == sizeof(int) + sizeof(int)
// due to potential padding between the structure members (this is for alignment
// reasons). [1]

void function_1(){

    struct rectangle my_rec;

    // "." ile yapı üyelerine ulaşılabilir.
    my_rec.width = 10;
    my_rec.height = 20;

    // Bir yapının adresini işaretçiye atayabilirsiniz.
    struct rectangle* my_rec_ptr = &my_rec;

    // İşaretçi üzerinden bir yapıya ulaşabilirsiniz. 
    (*my_rec_ptr).width = 30;

    // ya da -> işareti ile yapının elemanlarına ulaşabilirsiniz. 
    my_rec_ptr->height = 10; // (*my_rec_ptr).height = 10; ile aynıdır.
}

// Kolaylık sağlamak için bir yapıya typedef tanımlayabilirsiniz. 
typedef struct rectangle rect;

int area(rect r){
    return r.width * r.height;
}

///////////////////////////////////////
// Fonksiyon İşaretçiler
///////////////////////////////////////

/*
Çalışma zamanında, fonksiyonların bilinen bir bellek adresleri vardır. Fonksiyon
işaretçileri fonksiyonları direk olarak çağırmayı sağlayan veya geri bildirim(callback)
için parametre gönderirken kullanılan başka birer işaretçidirler. Ama, syntax tanımı 
başlangıçta biraz karışık gelebilir. 

Örnek: bir işaretçiden str_reverse kullanımı
*/
void str_reverse_through_pointer(char * str_in) {
    // f adında bir fonksiyon işaretçisi tanımlanır.
    void (*f)(char *); // Signature should exactly match the target function.
    f = &str_reverse; // Assign the address for the actual function (determined at runtime)
    (*f)(str_in); // Just calling the function through the pointer
    // f(str_in); // That's an alternative but equally valid syntax for calling it.
}

/*
As long as function signatures match, you can assign any function to the same pointer.
Function pointers are usually typedef'd for simplicity and readability, as follows:
*/

typedef void (*my_fnp_type)(char *);

// Gerçek bir işaretçi tanımlandığı zaman ki kullanımı:
// ...
// my_fnp_type f; 

```

## Daha Fazla Okuma Listesi

[K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language)'in bir kopyasını bulundurmak mükemmel olabilir

Diğer bir iyi kaynak ise [Learn C the hard way](http://c.learncodethehardway.org/book/)

It's very important to use proper spacing, indentation and to be consistent with your coding style in general.
Readable code is better than clever code and fast code. For a good, sane coding style to adopt, see the
[Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle).

Diğer taraftan google sizin için bir arkadaş olabilir.

[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member
---
language: c#
contributors:
    - ["Irfan Charania", "https://github.com/irfancharania"]
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Melvyn Laïly", "http://x2a.yt"]
    - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
translators:
    - ["Melih Mucuk", "http://melihmucuk.com"]
lang: tr-tr
filename: LearnCSharp-tr.cs

---

C# zarif ve tip güvenli nesne yönelimli bir dil olup geliştiricilerin .NET framework üzerinde çalışan güçlü ve güvenli uygulamalar geliştirmesini sağlar.

[Yazım yanlışları ve öneriler için bana ulaşabilirsiniz](mailto:melihmucuk@gmail.com)

[Daha fazlasını okuyun.](http://msdn.microsoft.com/en-us/library/vstudio/z1zx9t92.aspx)

```c#
// Tek satırlık yorumlar // ile başlar 
/*
Birden fazla satırlı yorumlar buna benzer
*/
/// <summary>
/// Bu bir XML dokümantasyon yorumu
/// </summary>

// Uygulamanın kullanacağı ad alanlarını belirtin
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;
using System.IO;

// Kodu düzenlemek için paketler içinde alan tanımlayın
namespace Learning
{
    // Her .cs dosyası, dosya ile aynı isimde en az bir sınıf içermeli
    // bu kurala uymak zorunda değilsiniz ancak mantıklı olan yol budur.
    public class LearnCSharp
    {
        // TEMEL SÖZ DİZİMİ - daha önce Java ya da C++ kullandıysanız İLGİNÇ ÖZELLİKLER'e geçin
        public static void Syntax() 
        {
            // Satırları yazdırmak için Console.WriteLine kullanın
            Console.WriteLine("Merhaba Dünya");
            Console.WriteLine(
                "Integer: " + 10 +
                " Double: " + 3.14 +
                " Boolean: " + true);

            // Yeni satıra geçmeden yazdırmak için Console.Write kullanın
            Console.Write("Merhaba ");
            Console.Write("Dünya");

            ///////////////////////////////////////////////////
            // Tipler & Değişkenler
            //
            // Bir değişken tanımlamak için <tip> <isim> kullanın
            ///////////////////////////////////////////////////

            // Sbyte - Signed 8-bit integer
            // (-128 <= sbyte <= 127)
            sbyte fooSbyte = 100;

            // Byte - Unsigned 8-bit integer
            // (0 <= byte <= 255)
            byte fooByte = 100;

            // Short - 16-bit integer
            // Signed - (-32,768 <= short <= 32,767)
            // Unsigned - (0 <= ushort <= 65,535)
            short fooShort = 10000;
            ushort fooUshort = 10000;

            // Integer - 32-bit integer
            int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647)
            uint fooUint = 1; // (0 <= uint <= 4,294,967,295)

            // Long - 64-bit integer
            long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
            ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
            // Sayılar boyutlarına göre ön tanımlı olarak int ya da uint olabilir.
            // L, değişken değerinin long ya da ulong tipinde olduğunu belirtmek için kullanılır.

            // Double - Çift hassasiyetli 64-bit IEEE 754 kayan sayı
            double fooDouble = 123.4; // Hassasiyet: 15-16 basamak

            // Float - Tek hassasiyetli 32-bit IEEE 754 kayan sayı
            float fooFloat = 234.5f; // Hassasiyet: 7 basamak
            // f, değişken değerinin float tipinde olduğunu belirtmek için kullanılır.

            // Decimal - 128-bit veri tiğinde ve diğer kayan sayı veri tiplerinden daha hassastır,
            // finansal ve mali hesaplamalar için uygundur.
            decimal fooDecimal = 150.3m;

            // Boolean - true & false
            bool fooBoolean = true; // veya false

            // Char - 16-bitlik tek bir unicode karakter
            char fooChar = 'A';

            // Strings -- Önceki baz tiplerinin hepsi değer tipiyken,
            // string bir referans tipidir. Null değer atayabilirsiniz
            string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs)";
            Console.WriteLine(fooString);

            // İndeks numarası kullanarak bir string'in bütün karakterlerine erişilebilirsiniz: 
            char charFromString = fooString[1]; // => 'e'
            // String'ler değiştirilemez: fooString[1] = 'X' işlemini yapamazsınız;

            // String'leri geçerli kültür değeri ve büyük küçük harf duyarlılığı olmadan karşılaştırma
            string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);

            // sprintf baz alınarak formatlama
            string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);

            // Tarihler & Formatlama
            DateTime fooDate = DateTime.Now;
            Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));

            // Bir string'i iki satıra bölmek için @ sembolü kullanabilirsiniz. " işaretinden kaçmak için "" kullanın
            string bazString = @"Here's some stuff
on a new line! ""Wow!"", the masses cried";

            // Bir değişkeni değiştirilemez yapmak için const ya da read-only kullanın. 
            // const değerleri derleme sırasında hesaplanır
            const int HOURS_I_WORK_PER_WEEK = 9001;

            ///////////////////////////////////////////////////
            // Veri Yapıları
            ///////////////////////////////////////////////////

            // Diziler - Sıfır indeksli
            // Dizi boyutuna tanımlama sırasında karar verilmelidir.
            // Dizi tanımlama formatı şöyledir:
            // <veri tipi>[] <değişken ismi> = new <veri tipi>[<dizi boyutu>];
            int[] intArray = new int[10];

            // Bir diğer dizi tanımlama formatı şöyledir:
            int[] y = { 9000, 1000, 1337 };

            // Bir diziyi indeksleme - Bir elemente erişme
            Console.WriteLine("intArray @ 0: " + intArray[0]);
            // Diziler değiştirilebilir.
            intArray[1] = 1;

            // Listeler
            // Listeler daha esnek oldukları için dizilerden daha sık kullanılırlar.
            // Bir liste tanımlama formatı şöyledir:
            // List<veri tipi> <değişken ismi> = new List<veri tipi>();
            List<int> intList = new List<int>();
            List<string> stringList = new List<string>();
            List<int> z = new List<int> { 9000, 1000, 1337 }; // tanımlama
            // <> işareti generic ifadeler içindir - Güzel özellikler sekmesini inceleyin

            // Listelerin varsayılan bir değeri yoktur;
            // İndekse erişmeden önce değer eklenmiş olmalıdır
            intList.Add(1);
            Console.WriteLine("intList @ 0: " + intList[0]);

            // Diğer veri yapıları için şunlara bakın:
            // Stack/Queue (Yığın/Kuyruk)
            // Dictionary (hash map'in uygulanması) (Sözlük)
            // HashSet (karma seti)
            // Read-only Collections (Değiştirilemez koleksiyonlar)
            // Tuple (.Net 4+) (tüp)

            ///////////////////////////////////////
            // Operatörler
            ///////////////////////////////////////
            Console.WriteLine("\n->Operators");

            int i1 = 1, i2 = 2; // Birden çok tanımlamanın kısa yolu

            // Aritmetik basittir
            Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3

            // Mod
            Console.WriteLine("11%3 = " + (11 % 3)); // => 2

            // Karşılaştırma operatörleri
            Console.WriteLine("3 == 2? " + (3 == 2)); // => false
            Console.WriteLine("3 != 2? " + (3 != 2)); // => true
            Console.WriteLine("3 > 2? " + (3 > 2)); // => true
            Console.WriteLine("3 < 2? " + (3 < 2)); // => false
            Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true
            Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true

            // Bit düzeyi operatörleri!
            /*
            ~       Tekli bit tamamlayıcısı
            <<      Sola kaydırma Signed left shift
            >>      Sağa kaydırma Signed right shift
            &       Bit düzeyi AND
            ^       Bit düzeyi harici OR
            |       Bit düzeyi kapsayan OR
            */

            // Arttırma
            int i = 0;
            Console.WriteLine("\n->Inc/Dec-rementation");
            Console.WriteLine(i++); //i = 1. Post-Incrementation
            Console.WriteLine(++i); //i = 2. Pre-Incrementation
            Console.WriteLine(i--); //i = 1. Post-Decrementation
            Console.WriteLine(--i); //i = 0. Pre-Decrementation

            ///////////////////////////////////////
            // Kontrol Yapıları
            ///////////////////////////////////////
            Console.WriteLine("\n->Control Structures");

            // If ifadesi c benzeridir
            int j = 10;
            if (j == 10)
            {
                Console.WriteLine("I get printed");
            }
            else if (j > 10)
            {
                Console.WriteLine("I don't");
            }
            else
            {
                Console.WriteLine("I also don't");
            }

            // Üçlü operatörler
            // Basit bir if/else ifadesi şöyle yazılabilir
            // <koşul> ? <true> : <false>
            int toCompare = 17;
            string isTrue = toCompare == 17 ? "True" : "False";

            // While döngüsü
            int fooWhile = 0;
            while (fooWhile < 100)
            {
                //100 kere tekrarlanır, fooWhile 0->99
                fooWhile++;
            }

            // Do While Döngüsü
            int fooDoWhile = 0;
            do
            {
                //100 kere tekrarlanır, fooDoWhile 0->99
                fooDoWhile++;
            } while (fooDoWhile < 100);

            //for döngüsü yapısı => for(<başlangıç ifadesi>; <koşul>; <adım>)
            for (int fooFor = 0; fooFor < 10; fooFor++)
            {
                //10 kere tekrarlanır, fooFor 0->9
            }

            // For Each Döngüsü
            // foreach döngüsü yapısı => foreach(<yineleyici tipi> <yineleyici ismi> in <enumerable>)
            // foreach döngüsü, IEnumerable ya da IEnumerable<T> e dönüştürülmüş herhangi bir obje üzerinde döngü yapabilir
            // .Net framework üzerindeki bütün koleksiyon tiplerinden (Dizi, Liste, Sözlük...)
            // biri ya da hepsi uygulanarak gerçekleştirilebilir.
            // (ToCharArray() silindi, çünkü string'ler aynı zamanda IEnumerable'dır.)
            foreach (char character in "Hello World".ToCharArray())
            {
                //String içindeki bütün karakterler üzerinde döner
            }

            // Switch Case
            // Bir switch byte, short, char ve int veri tipleri ile çalışır.
            // Aynı zamanda sıralı tipler ile de çalışabilir.(Enum Tipleri bölümünde tartışıldı),
            // String sınıfı, ve bir kaç özel sınıf kaydırılır
            // basit tipler: Character, Byte, Short, and Integer.
            int month = 3;
            string monthString;
            switch (month)
            {
                case 1:
                    monthString = "January";
                    break;
                case 2:
                    monthString = "February";
                    break;
                case 3:
                    monthString = "March";
                    break;
                // Bir aksiyon için birden fazla durum atayabilirsiniz
                // Ancak, break olmadan yeni bir durum ekleyemezsiniz
                // (Eğer bunu yapmak istiyorsanız, goto komutu eklemek zorundasınız)
                case 6:
                case 7:
                case 8:
                    monthString = "Summer time!!";
                    break;
                default:
                    monthString = "Some other month";
                    break;
            }

            ///////////////////////////////////////
            // Veri Tipleri Dönüştürme ve Typecasting
            ///////////////////////////////////////

            // Veri Dönüştürme

            // String'i Integer'a Dönüştürme
            // bu başarısız olursa hata fırlatacaktır
            int.Parse("123");// "123" 'in Integer değerini döndürür

            // try parse hata durumunda değişkene varsayılan bir değer atamak için kullanılır
            // bu durumda: 0
            int tryInt;
            if (int.TryParse("123", out tryInt)) // Fonksiyon boolean'dır
                Console.WriteLine(tryInt);       // 123

            // Integer'ı String'e Dönüştürme
            // Convert sınıfı dönüştürme işlemini kolaylaştırmak için bir dizi metoda sahiptir
            Convert.ToString(123);
            // veya
            tryInt.ToString();
        }

        ///////////////////////////////////////
        // SINIFLAR - dosyanın sonunda tanımları görebilirsiniz
        ///////////////////////////////////////
        public static void Classes()
        {
            // Obje tanımlamalarını dosyanın sonunda görebilirsiniz

            // Bir sınıfı türetmek için new kullanın
            Bicycle trek = new Bicycle();

            // Obje metodlarını çağırma
            trek.SpeedUp(3); // Her zaman setter ve getter metodları kullanmalısınız
            trek.Cadence = 100;

            // ToString objenin değerini göstermek için kullanılır.
            Console.WriteLine("trek info: " + trek.Info());

            // Yeni bir Penny Farthing sınıfı türetmek
            PennyFarthing funbike = new PennyFarthing(1, 10);
            Console.WriteLine("funbike info: " + funbike.Info());

            Console.Read();
        } // Ana metodun sonu

        // KONSOLE BAŞLANGICI Bir konsol uygulaması başlangıç olarak mutlaka ana metod'a sahip olmalı
        public static void Main(string[] args)
        {
            OtherInterestingFeatures();
        }

        //
        // İLGİNÇ ÖZELLİKLER
        //
        
        // VARSAYILAN METOD TANIMLAMALARI

        public // Görünebilir
        static // Sınıf üzerinden obje türetmeden çağırılabilir
        int // Dönüş Tipi,
        MethodSignatures(
            int maxCount, // İlk değişken, int değer bekler
            int count = 0, // Eğer değer gönderilmezse varsayılan olarak 0 değerini alır
            int another = 3,
            params string[] otherParams // Metoda gönderilen diğer bütün parametreleri alır
        )
        { 
            return -1;
        }

        // Metodlar tanımlamalar benzersiz ise aynı isimleri alabilirler
        public static void MethodSignatures(string maxCount)
        {
        }

        // GENERIC'LER
        // TKey ve TValue değerleri kullanıcı tarafından bu fonksiyon çağırılırken belirtilir.
        // Bu metod Python'daki SetDefault'a benzer
        public static TValue SetDefault<TKey, TValue>(
            IDictionary<TKey, TValue> dictionary, 
            TKey key, 
            TValue defaultItem)
        {
            TValue result;
            if (!dictionary.TryGetValue(key, out result))
                return dictionary[key] = defaultItem;
            return result;
        }

        // Gönderilen objeleri daraltabilirsiniz
        public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
        {
            // Eğer T IEnumerable ise tekrarlayabiliriz
            foreach (var item in toPrint)
                // Item bir int
                Console.WriteLine(item.ToString());
        }

        public static void OtherInterestingFeatures()
        {
            // İSTEĞE BAĞLI PARAMETRELER
            MethodSignatures(3, 1, 3, "Some", "Extra", "Strings");
            MethodSignatures(3, another: 3); // isteğe bağlı olanlar gönderilmedi

            // UZANTI METODLARI
            int i = 3;
            i.Print(); // Aşağıda tanımlandı

            // NULLABLE TYPES - veri tabanı işlemleri için uygun / return values
            // Herhangi bir değer tipi sonuna ? eklenerek nullable yapılabilir (sınıflar hariç)
            // <tip>? <değiken ismi> = <değer>
            int? nullable = null; // Nullable<int> için kısa yol
            Console.WriteLine("Nullable variable: " + nullable);
            bool hasValue = nullable.HasValue; // eğer null değilse true döner

            // ?? varsayılan değer belirlemek için söz dizimsel güzel bir özellik
            // bu durumda değişken null'dır
            int notNullable = nullable ?? 0; // 0

            // TİPİ BELİRTİLMEMİŞ DEĞİŞKENLER - compiler değişkenin tipini bilmeden çalışabilir:
            var magic = "magic is a string, at compile time, so you still get type safety";
            // magic = 9; string gibi çalışmayacaktır, bu bir int değil

            // GENERIC'LER
            //
            var phonebook = new Dictionary<string, string>() { 
                {"Sarah", "212 555 5555"} // Telefon rehberine bir kaç numara ekleyelim.
            };

            // Yukarıda generic olarak tanımlanan SETDEFAULT'u çağırma
            Console.WriteLine(SetDefault<string,string>(phonebook, "Shaun", "No Phone")); // Telefonu yok
            // TKey ve TValue tipini belirtmek zorunda değilsiniz
            Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555

            // LAMBDA IFADELERİ - satır içinde kod yazmanıza olanak sağlar
            Func<int, int> square = (x) => x * x; // Son T nesnesi dönüş değeridir
            Console.WriteLine(square(3)); // 9

            // TEK KULLANIMLIK KAYNAK YÖNETİMİ -  Yönetilemeyen kaynakların üstesinden kolayca gelebilirsiniz.
            // Bir çok obje yönetilemeyen kaynaklara (dosya yakalama, cihaz içeriği, vb.)
            // IDisposable arabirimi ile erişebilir. Using ifadesi sizin için IDisposable objeleri temizler.
            using (StreamWriter writer = new StreamWriter("log.txt"))
            {
                writer.WriteLine("Nothing suspicious here");
                // Bu bölümün sonunda kaynaklar temilenir.
                // Hata fırlatılmış olsa bile.
            } 

            // PARALEL FRAMEWORK
            // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
            var websites = new string[] { 
                "http://www.google.com", "http://www.reddit.com", 
                "http://www.shaunmccarthy.com"
            };
            var responses = new Dictionary<string, string>();
            
            // Her istek farklı bir thread de işlem görecek 
            // bir sonraki işleme geçmeden birleştirilecek.
            Parallel.ForEach(websites, 
                new ParallelOptions() {MaxDegreeOfParallelism = 3}, // en fazla 3 thread kullanmak için
                website =>
            {
                // Uzun sürecek bir işlem yapın
                using (var r = WebRequest.Create(new Uri(website)).GetResponse())
                {
                    responses[website] = r.ContentType;
                }
            });

            // Bütün istekler tamamlanmadan bu döndü çalışmayacaktır.
            foreach (var key in responses.Keys)
                Console.WriteLine("{0}:{1}", key, responses[key]);

            // DİNAMİK OBJELER (diğer dillerle çalışırken kullanmak için uygun)
            dynamic student = new ExpandoObject();
            student.FirstName = "First Name"; // Önce yeni bir sınıf tanımlamanız gerekmez!

            // Hatta metod bile ekleyebilirsiniz (bir string döner, ve bir string alır)
            student.Introduce = new Func<string, string>(
                (introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo));
            Console.WriteLine(student.Introduce("Beth"));

            // IQUERYABLE<T> - neredeyse bütün koleksiyonlar bundan türer, bu size bir çok
            // kullanışlı Map / Filter / Reduce stili metod sağlar.
            var bikes = new List<Bicycle>();
            bikes.Sort(); // Dizi sıralama
            bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels)); // Wheels baz alınarak sıralama
            var result = bikes
                .Where(b => b.Wheels > 3) // Filters- chainable (bir önceki tipin IQueryable'ını döner)
                .Where(b => b.IsBroken && b.HasTassles)
                .Select(b => b.ToString()); // Map - sadece bunu seçiyoruz, yani sonuç bir IQueryable<string> olacak

            var sum = bikes.Sum(b => b.Wheels); // Reduce - koleksiyonda bulunan bütün wheel değerlerinin toplamı

            // Bike içindeki bazı parametreleri baz alarak bir liste oluşturmak
            var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
            // Burada göstermek zor ama, compiler yukaridaki tipleri çözümleyebilirse derlenmeden önce tipi verebilir.
            foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome))
                Console.WriteLine(bikeSummary.Name);

            // ASPARALLEL
            // Linq ve paralel işlemlerini birleştirme
            var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
            // bu paralel bir şekilde gerçekleşecek! Threadler otomatik ve sihirli bir şekilde işleri paylaşacak! 
            // Birden fazla çekirdeğiniz varsa büyük veri setleri ile kullanmak için oldukça uygun bir yapı.

            // LINQ - IQueryable<T> objelerini mapler ve saklar, gecikmeli bir işlemdir
            // e.g. LinqToSql - veri tabanını mapler, LinqToXml xml dökümanlarını mapler.
            var db = new BikeRepository();

            // işlem gecikmelidir, bir veri tabanı üzerinde sorgulama yaparken harikadır.
            var filter = db.Bikes.Where(b => b.HasTassles); // sorgu henüz çalışmadı
            if (42 > 6) // Filtreler eklemeye devam edebilirsiniz - ileri düzey arama fonksiyonları için harikadır
                filter = filter.Where(b => b.IsBroken); // sorgu henüz çalışmadı

            var query = filter
                .OrderBy(b => b.Wheels)
                .ThenBy(b => b.Name)
                .Select(b => b.Name); // hala sorgu çalışmadı

            // Şimdi sorgu çalışıyor, reader'ı açar ama sadece sizin sorgunuza uyanlar foreach döngüsüne girer.
            foreach (string bike in query) 
                Console.WriteLine(result);
            


        }

    } // LearnCSharp sınıfının sonu

    // Bir .cs dosyasına diğer sınıflarıda dahil edebilirsiniz

    public static class Extensions
    {
        // UZANTI FONKSİYONLARI
        public static void Print(this object obj)
        {
            Console.WriteLine(obj.ToString());
        }
    }

    // Sınıf Tanımlama Sözdizimi:
    // <public/private/protected/internal> class <sınıf ismi>{
    //    //veri alanları, kurucular , fonksiyonlar hepsi içindedir.
    //    //Fonksiyonlar Java'daki gibi metod olarak çağırılır.
    // }

    public class Bicycle
    {
        // Bicycle'ın Alanları/Değişkenleri
        public int Cadence // Public: herhangi bir yerden erişilebilir
        {
            get // get - değeri almak için tanımlanan metod
            {
                return _cadence;
            }
            set // set - değer atamak için tanımlanan metod
            {
                _cadence = value; // Değer setter'a gönderilen value değeridir
            }
        }
        private int _cadence;

        protected virtual int Gear // Protected: Sınıf ve alt sınıflar tarafından erişilebilir
        {
            get; // bir üye alanına ihtiyacınız yok, bu otomatik olarak bir değer oluşturacaktır
            set;
        }

        internal int Wheels // Internal: Assembly tarafından erişilebilir
        {
            get;
            private set; // Nitelik belirleyicileri get/set metodlarında atayabilirsiniz
        }

        int _speed; // Her şey varsayılan olarak private'dır : Sadece sınıf içinden erişilebilir.
                    // İsterseniz yinede private kelimesini kullanabilirsiniz.
        public string Name { get; set; }

        // Enum sabitler kümesinden oluşan bir değer tipidir.
        // Gerçekten sadece bir isim ile bir değeri tutmak için kullanılır. (aksi belirtilmedikçe bir int'dir).
        // İzin verilen enum tipleri şunlardır byte, sbyte, short, ushort, int, uint, long, veya ulong.
        // Bir enum aynı değeri birden fazla sayıda barındıramaz.
        public enum BikeBrand
        {
            AIST,
            BMC,
            Electra = 42, // bir isme tam bir değer verebilirsiniz
            Gitane // 43
        }
        // Bu tipi Bicycle sınıfı içinde tanımladığımız için bu bir bağımlı tipdir.
        // Bu sınıf dışında kullanmak için tipi Bicycle.Brand olarak kullanmamız gerekir

        public BikeBrand Brand; // Enum tipini tanımladıktan sonra alan tipini tanımlayabiliriz

        // Static üyeler belirli bir obje yerine kendi tipine aittir
        // Onlara bir obje referans göstermeden erişebilirsiniz:
        // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated);
        static public int BicyclesCreated = 0;

        // readonly değerleri çalışma zamanında atanır
        // onlara sadece tanımlama yapılarak ya da kurucular içinden atama yapılabilir
        readonly bool _hasCardsInSpokes = false; // read-only private

        // Kurucular sınıf oluşturmanın bir yoludur
        // Bu bir varsayılan kurucudur.
        public Bicycle() 
        {
            this.Gear = 1; // bu objenin üyelerine this anahtar kelimesi ile ulaşılır
            Cadence = 50;  // ama her zaman buna ihtiyaç duyulmaz
            _speed = 5;
            Name = "Bontrager";
            Brand = BikeBrand.AIST;
            BicyclesCreated++;
        }

        // Bu belirlenmiş bir kurucudur. (argümanlar içerir)
        public Bicycle(int startCadence, int startSpeed, int startGear,
                       string name, bool hasCardsInSpokes, BikeBrand brand) 
            : base() // önce base'i çağırın
        {
            Gear = startGear; 
            Cadence = startCadence;
            _speed = startSpeed;
            Name = name; 
            _hasCardsInSpokes = hasCardsInSpokes;
            Brand = brand;
        }

        // Kurucular zincirleme olabilir
        public Bicycle(int startCadence, int startSpeed, BikeBrand brand) :
            this(startCadence, startSpeed, 0, "big wheels", true, brand)
        {
        }

        // Fonksiyon Sözdizimi:
        // <public/private/protected> <dönüş tipi> <fonksiyon ismi>(<argümanlar>)

        // sınıflar getter ve setter'ları alanları için kendisi uygular
        // veya property'ler eklenebilir (C# da tercih edilen yol budur)

        // Metod parametreleri varsayılan değerlere sahip olabilir.
        // Bu durumda, metodlar bu parametreler olmadan çağırılabilir.
        public void SpeedUp(int increment = 1)
        {
            _speed += increment;
        }

        public void SlowDown(int decrement = 1)
        {
            _speed -= decrement;
        }

        // property'lerin get/set değerleri
        // sadece veri gerektiği zaman erişilebilir, kullanmak için bunu göz önünde bulundurun.
        // property'ler sadece get ya da set'e sahip olabilir veya ikisine birden
        private bool _hasTassles; // private değişken
        public bool HasTassles // public accessor
        {
            get { return _hasTassles; }
            set { _hasTassles = value; }
        }

        // Ayrıca tek bir satırda otomatik property tanımlayabilirsiniz. 
        // bu söz dizimi otomatik olarak alan oluşturacaktır.
        // Erişimi kısıtlamak için nitelik belirleyiciler getter veya setter'a ya da ikisine birden atanabilir:
        public bool IsBroken { get; private set; }

        // Property'ler otomatik eklenmiş olabilir
        public int FrameSize
        {
            get;
            // nitelik beliryecileri get veya set için tanımlayabilirsiniz
            // bu sadece Bicycle sınıfı Framesize değerine atama yapabilir demektir
            private set;
        }

        // Ayrıca obje üzerinde özel indeksleyici belirlemek mümkündür.
        // Tüm bunlar bu örnek için çok kullanışlı değil,
        // bicycle[0] ile ilk yolcu olan "chris" i almak mümkün veya
        // bicycle[1] = "lisa" ile yolcuyu atayabilirsiniz. (bariz quattrocycle)
        private string[] passengers = { "chris", "phil", "darren", "regina" }

        public string this[int i]
        {
            get {
                return passengers[i];
            }

            set {
                return passengers[i] = value;
            }
        }

        //Bu objenin nitelik değerlerini göstermek için bir metod.
        public virtual string Info()
        {
            return "Gear: " + Gear +
                    " Cadence: " + Cadence +
                    " Speed: " + _speed +
                    " Name: " + Name +
                    " Cards in Spokes: " + (_hasCardsInSpokes ? "yes" : "no") +
                    "\n------------------------------\n"
                    ;
        }

        // Metodlar static olabilir. Yardımcı metodlar için kullanışlı olabilir.
        public static bool DidWeCreateEnoughBycles()
        {
            // Bir static metod içinde sadece static sınıf üyeleri referans gösterilebilir
            return BicyclesCreated > 9000;
        } // Eğer sınıfınızın sadece static üyelere ihtiyacı varsa, sınıfın kendisini static yapmayı düşünebilirsiniz.


    } // Bicycle sınıfı sonu

    // PennyFarthing , Bicycle sınıfının alt sınıfıdır.
    class PennyFarthing : Bicycle
    {
        // (Penny Farthing'ler ön jantı büyük bisikletlerdir.
        // Vitesleri yoktur.)

        // Ana kurucuyu çağırmak
        public PennyFarthing(int startCadence, int startSpeed) :
            base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra)
        {
        }

        protected override int Gear
        {
            get
            {
                return 0;
            }
            set
            {
                throw new ArgumentException("You can't change gears on a PennyFarthing");
            }
        }

        public override string Info()
        {
            string result = "PennyFarthing bicycle ";
            result += base.ToString(); // Metodun temel versiyonunu çağırmak
            return result;
        }
    }

    // Arabirimler sadece üyelerin izlerini içerir, değerlerini değil.
    interface IJumpable
    {
        void Jump(int meters); // bütün arbirim üyeleri public'tir
    }

    interface IBreakable
    {
        bool Broken { get; } // arabirimler property'leri, metodları ve olayları içerebilir
    }

    // Sınıflar sadece tek bir sınıftan miras alabilir ama sınırsız sayıda arabirime sahip olabilir
    class MountainBike : Bicycle, IJumpable, IBreakable
    {
        int damage = 0;

        public void Jump(int meters)
        {
            damage += meters;
        }

        public bool Broken
        {
            get
            {
                return damage > 100;
            }
        }
    }

    /// <summary>
    /// LinqToSql örneği veri tabanına bağlanmak için kullanılır.
    /// EntityFramework Code First harika! (Ruby'deki ActiveRecord'a benzer, ama iki yönlü)
    /// http://msdn.microsoft.com/en-us/data/jj193542.aspx
    /// </summary>
    public class BikeRepository : DbSet
    {
        public BikeRepository()
            : base()
        {
        }

        public DbSet<Bicycle> Bikes { get; set; }
    }
} // namespace sonu
```

## İşlenmeyen Konular

 * Flags
 * Attributes
 * Static properties
 * Exceptions, Abstraction
 * ASP.NET (Web Forms/MVC/WebMatrix)
 * Winforms
 * Windows Presentation Foundation (WPF)

## Daha Fazlasını Okuyun

 * [DotNetPerls](http://www.dotnetperls.com)
 * [C# in Depth](http://manning.com/skeet2)
 * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
 * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
 * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx)
 * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
 * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials)
 * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
 * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)



[C# Kodlama Adetleri](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx)
---
language: Dynamic Programming
filename: dynamic-tr.txt
contributors:
    - ["Akashdeep Goel", "https://github.com/akashdeepgoel"]
translators:
    - ["Mehmet Cem Yaraş", "https://www.linkedin.com/in/yarascem/"]
lang: tr-tr
---

Dinamik Programlama
Giriş
Dinamik Programlama, göreceğimiz gibi belirli bir problem sınıfını çözmek için kullanılan güçlü bir tekniktir. Fikir çok basittir, verilen girdiyle ilgili bir sorunu çözdüyseniz, aynı sorunun tekrar çözülmesini önlemek için sonucunu gelecekte referans olarak kaydedilmesine dayanır.

Her zaman hatırla! "Geçmiş hatırlayamayanlar, aynı şeyleri tekrar yaşamaya mahkumlardır!"

Bu tür sorunların çözüm yolları

1-Yukarıdan aşağıya:
Verilen problemi çözerek çözmeye başlayın. Sorunun zaten çözüldüğünü görürseniz, kaydedilen cevabı döndürmeniz yeterlidir. Çözülmemişse, çözünüz ve cevabı saklayınız. Bu genellikle düşünmek kolaydır ve çok sezgiseldir. Buna Ezberleştirme denir.

2-Aşağıdan yukarıya:
Sorunu analiz edin ve alt problemlerin çözülme sırasını görün ve önemsiz alt sorundan verilen soruna doğru başlayın. Bu süreçte, problemi çözmeden önce alt problemlerin çözülmesi gerekmektedir. Buna Dinamik Programlama denir.

Örnek
En Uzun Artan Subsequence problemi belirli bir dizinin en uzun artan alt dizini bulmaktır. S = {a1, a2, a3, a4, ............., an-1} dizisi göz önüne alındığında, en uzun bir alt kümeyi bulmak zorundayız, böylece tüm j ve i, j için <I, aj <ai alt kümesinde. Her şeyden önce, en son alt dizgenin (LSi) değerini dizinin son elemanı olan ai'nin her indeksinde bulmalıyız. Daha sonra en büyük LSi, verilen dizideki en uzun alt dizin olacaktır. Başlamak için, ai, dizinin elemanı olduğundan (Son öğe) LSi atanır. Sonra tüm j için j <i ve aj <ai gibi, En Büyük LSj'yi buluruz ve LSi'ye ekleriz. Sonra algoritma O (n2) zaman alır.

En uzun artan alt dizinin uzunluğunu bulmak için sözde kod: Bu algoritmaların karmaşıklığı dizi yerine daha iyi veri yapısı kullanılarak azaltılabilir. Büyük dizin ve dizin gibi selefi dizi ve değişkeni saklama çok zaman kazandıracaktır.

Yönlendirilmiş asiklik grafiğinde en uzun yolu bulmak için benzer bir kavram uygulanabilir.

```python
for i=0 to n-1
    LS[i]=1
    for j=0 to i-1
        if (a[i] >  a[j] and LS[i]<LS[j])
            LS[i] = LS[j]+1
for i=0 to n-1
    if (largest < LS[i])
    
```  

Bazı Ünlü Dinamik Programlama Problemleri
-Floyd Warshall Algorithm - Tutorial and C Program source code:http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs—floyd-warshall-algorithm-with-c-program-source-code
-Integer Knapsack Problem - Tutorial and C Program source code: http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming—the-integer-knapsack-problem
-Longest Common Subsequence - Tutorial and C Program source code : http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming—longest-common-subsequence

Online Kaynaklar
https://www.codechef.com/wiki/tutorial-dynamic-programming
---
language: F#
contributors:
    - ["Scott Wlaschin", "http://fsharpforfunandprofit.com/"]
translators:
    - ["Mustafa Zengin", "http://zengin.github.com/"]
filename: learnfsharp-tr.fs
lang: tr-tr
---

F# fonksiyonel ve nesne yönelimli, genel amaçlı bir programlama dilidir. Bedava ve açık kaynaklıdır ve Linux, Mac, Windows ve dahasında çalışır.

Hataları derleme zamanında yakalayan çok güçlü bir tip sistemine sahiptir, ancak tip çıkarımı yaptığından dinamik bir dil gibi görünür.

F#'ın söz dizimi C-stili dillerden farklıdır:

* Küme parantezi kod bloklarını ayırmak için kullanılmaz. Bunun yerine Python'da olduğu gibi girinti kullanılır.
* Parametreleri birbirinden ayırmak için virgül yerine boşluk karakteri kullanılır.

Aşağıdaki kodu denemek istiyorsanız, [tryfsharp.org](http://www.tryfsharp.org/Create)'a gidin be interaktif REPL'e kodu yapıştırın.

```csharp

// tek satır yorumlar ikili bölme işareti ile başlar
(* çok satırlı yorumlar ( * . . . * ) ikilisini kullanır

-çok satırlı yorumun sonu- *)

// ================================================
// Temel Söz Dizimi
// ================================================

// ------ "Değişkenler" (tam da değil) ------
// "let" anahtar kelimesi (değişmez) değer tanımlar
let tamsayım = 5
let ondalığım = 3.14
let stringim = "merhaba"           // tip bilgisi olmamasına dikkat

// ------ Listeler ------
let ikidenBeşe = [2; 3; 4; 5]      // Köşeli parantezler listeleri oluşturur,
                                   // değerler ise noktalı virgülle ayrılır.
let birdenBeşe = 1 :: ikidenBeşe   // :: yeni birinci elemanı olan bir liste oluşturur.
// Sonuç: [1; 2; 3; 4; 5]
let sıfırdanBeşe = [0; 1] @ ikidenBeşe  // @ iki listeyi birbirine ekler.

// ÖNEMLİ: virgüller hiçbir zaman ayraç olarak kullanılmaz, sadece noktalı virgüller!

// ------ Fonksiyonlar ------
// "let" anahtar kelimesi isimlendirilmiş fonksiyonları da tanımlar.
let kare x = x * x          // Parantez kullanılmadığına dikkat.
kare 3                      // Şimdi fonksiyonu uygulayın. Yine parantez yok.

let topla x y = x + y       // topla (x,y) kullanmayın! Bu tamamen başka bir anlama geliyor.
topla 2 3                   // Şimdi fonksiyonu uygulayın.

// çok satırlı bir fonksiyon tanımlamak için sadece girinti kullanın. Noktalı virgül gerekmez.
let çiftler liste =
   let çiftMi x = x % 2 = 0     // "çiftMi"yi alt fonksiyon olarak tanımlayın
   List.filter çiftMi liste     // List.filter 'boolean bir fonksiyon' ve
                                // 'üzerinde çalışılacak bir liste' parametrelerinden oluşan
                                // bir kütüphane fonksiyonu
                              
çiftler birdenBeşe              // Şimdi fonksiyonu uygula.

// Parantezleri önceliği netleştirmek için kullanabilirsiniz. Bu örnek
// "map"i önce iki argümana, sonra sonuç üzerinde "ekle" uyguluyor.
// Parantezler olmasaydı, "List.map" List.sum'ın ilk argümanı olurdu.
let yüzeKadarKarelerinToplamı =
   List.sum ( List.map kare [1..100] )

// Bir operasyonun sonucunu bir sonrakine "|>" kullanarak besleyebilirsin.
// Veri beslemek F#'ta UNIX'te olduğu gibi yaygındır..

// Burada yüzeKadarKarelerinToplamı fonksiyonunun veri beslemeyle yazılmış hali var:
let veriBeslemeyleYüzeKadarKarelerinToplamı =
   [1..100] |> List.map kare |> List.sum  // "kare" önceden tanımlanmıştı

// Lambda'ları (anonim fonksiyonları) "fun" anahtar kelimesiyle tanımlayabilirsin
let funlaYüzeKadarKarelerinToplamı =
   [1..100] |> List.map (fun x -> x * x) |> List.sum

// F#'ta "return" anahtar kelimesi yoktur. Bir fonksiyon
// her zaman son kullanılan ifadeyi döndürür.

// ------ Kalıp eşleştirme ------
// Match..with.. çok güçlü bir case/switch türevidir.
let basitKalıpEşleştirme =
   let x = "a"
   match x with
    | "a" -> printfn "x a'dır"
    | "b" -> printfn "x b'dir"
    | _ -> printfn "x başka bir şeydir"   // alt çizgi bütün kalıplarla eşleşir

// F# varsayılan olarak null'lara izin vermez -- Option tipini kullanıp
// kalıp eşleştirme yapmalısın.
// Some(..) ve None, Nullable tipler gibidir.
let geçerliDeğer = Some(99)
let geçersizDeğer = None

// Bu örnekte, match..with "Some" ve "None"la eşleştirme yapıyor,
// ve ayrıca "Some" içerisindeki değeri de çıkarıyor.
let optionKalıpEşleştirme input =
   match input with
    | Some i -> printfn "input is an int=%d" i
    | None -> printfn "input is missing"

optionKalıpEşleştirme geçerliDeğer
optionKalıpEşleştirme geçersizDeğer

// ------ Yazdırma ------
// printf/printfn fonksiyonları C#'taki 
// Console.Write/WriteLine fonksiyonlarına benzer.
printfn "Bir tamsayı %i, bir ondalık %f, bir boolean %b yazdırma" 1 2.0 true
printfn "Bir string %s, ve jenerik bir tip %A" "merhaba" [1; 2; 3; 4]

// sprintf/sprintfn fonksiyonları ise veriyi string'e
// çevirmek içindir, C#'taki String.Format gibi.

// ================================================
// Fonksiyonlar hakkında dahası
// ================================================

// F# gerçek bir fonksiyonel dildir. Fonksiyonlar birinci
// sınıf varlıklardır ve güçlü yapılar oluşturmak için
// birleştirilebilirler.

// Modüller fonksiyonları gruplamak için kullanılır.
// Her bir modül için girinti gerekir.
module FonksiyonOrnekleri =

    // Temel bir ekleme fonksiyonu tanımla
    let topla x y = x + y

    // Bir fonksiyonun temel kullanımı
    let a = topla 1 2
    printfn "1 + 2 = %i" a

    // Parametreleri kaynaklamak için parçalı uygulama
    let kırkİkiEkle = topla 42
    let b = kırkİkiEkle 1
    printfn "42 + 1 = %i" b

    // Fonksiyonları birleştirmek için kompozisyon
    let birEkle = topla 1
    let ikiEkle = topla 2
    let üçEkle = birEkle >> ikiEkle
    let c = üçEkle 7
    printfn "3 + 7 = %i" c

    // Yüksek dereceli fonksiyonlar
    [1..10] |> List.map üçEkle |> printfn "yeni liste: %A"

    // Fonksiyonlar listesi ve dahası
    let altıEkle = [birEkle; ikiEkle; üçEkle] |> List.reduce (>>)
    let d = altıEkle 7
    printfn "1 + 2 + 3 + 7 = %i" d

// ================================================
// Listeler ve kolleksiyonlar
// ================================================

// Üç çesit sıralı fonksiyon vardır:
// * Listeler en temel değiştirilemez kolleksiyonlardır.
// * Diziler değiştirilebilir ve gerektiğinde daha verimlidirler.
// * Seriler tembel (lazy evaluation) ve sonsuzdurlar (Enumeratörler gibi). 
// 
// Değiştirilmez map'ler ve kümeler ve bütün .NET kolleksiyonları
// diğer kolleksiyon türleridir.

module ListeÖrnekleri =

    // listeler köşeli parantez kullanır
    let liste1 = ["a"; "b"]
    let liste2 = "c" :: liste1    // :: başa eleman ekler
    let liste3 = liste1 @ liste2   // @ listeleri birbirine ekler

    // Liste comprehension'ları (jeneratörler olarak da bilinir)
    let kareler = [for i in 1..10 do yield i * i]

    // asal sayı jeneratörü
    let rec elek = function
        | (p::xler) -> p :: elek [ for x in xler do if x % p > 0 then yield x ]
        | []      -> []
    let asallar = elek [2..50]
    printfn "%A" asallar

    // Listelerle kalıp eşleştirme
    let listeEşleyici liste =
        match liste with
        | [] -> printfn "liste boş"
        | [birinci] -> printfn "listede sadece bir eleman var: %A " birinci
        | [birinci; ikinci] -> printfn "liste: %A ve %A" birinci ikinci
        | _ -> printfn "listede ikiden fazla eleman var"

    listeEşleyici [1; 2; 3; 4]
    listeEşleyici [1; 2]
    listeEşleyici [1]
    listeEşleyici []

    // Listeleri kullanarak recursion
    let rec ekle liste =
        match liste with
        | [] -> 0
        | x::xler -> x + ekle xler
    ekle [1..10]

    // -----------------------------------------
    // Standart kütüphane fonksiyonları
    // -----------------------------------------

    // map
    let üçEkle x = x + 3
    [1..10] |> List.map üçEkle

    // filter
    let çift x = x % 2 = 0
    [1..10] |> List.filter çift

    // ve dahası -- dökümantasyona bakınız

module DiziÖrnekleri =

    // Diziler köşeli parantezle birlikte çubuk karakterini kullanır
    let dizi1 = [| "a"; "b" |]
    let birinci = dizi1.[0]        // nokta kullanarak indeks erişimi

    // Diziler için kalıp eşleştirme listlerle aynıdır
    let diziEşleştirici liste =
        match liste with
        | [| |] -> printfn "dizi boş"
        | [| birinci |] -> printfn "dizide sadece bir eleman var: %A " birinci
        | [| birinci; ikinci |] -> printfn "dizi: %A ve %A" birinci ikinci
        | _ -> printfn "dizide ikiden fazla eleman var"

    diziEşleştirici [| 1; 2; 3; 4 |]

    // Listede olduğu gibi kütüphane fonksiyonları

    [| 1..10 |]
    |> Array.map (fun i -> i + 3)
    |> Array.filter (fun i -> i % 2 = 0)
    |> Array.iter (printfn "değer: %i. ")


module SeriÖrnekleri =

    // seriler kıvrık parantez kullanır
    let seri1 = seq { yield "a"; yield "b" }

    // seriler yield'ı kullanabilir
    // ve alt seriler barındırabilir
    let garip = seq {
        // "yield" bir eleman ekliyor
        yield 1; yield 2;

        // "yield!" bütün bir alt seriyi ekliyor
        yield! [5..10]
        yield! seq {
            for i in 1..10 do
              if i % 2 = 0 then yield i }}
    // test
    garip |> Seq.toList


    // Seriler "unfold" kullanılarak oluşturulabilir
    // Fibonacci serisi örneği
    let fib = Seq.unfold (fun (birinci,ikinci) ->
        Some(birinci + ikinci, (ikinci, birinci + ikinci))) (0,1)

    // test
    let fib10 = fib |> Seq.take 10 |> Seq.toList
    printf "ilk 10 fibonacci sayısı: %A" fib10


// ================================================
// Veri Tipleri
// ================================================

module VeriTipiÖrnekleri =

    // Bütün veriler varsayılan olarak değiştirilemezdir.

    // -- Tuple oluşturmak için virgül kullan
    let ikiliTuple = 1, 2
    let üçlüTuple = "a", 2, true
    
    // Tuple'lar çabuk ve kolay anonim tiplerdir.
    // paketi açmak için kalıp eşleştirme kullan
    let x, y = ikiliTuple  // x = 1, y = 2

    // ------------------------------------
    // Record tipi isimlendirilmiş alanlara sahiptir
    // ------------------------------------

    // "type" ile kıvrık parantezleri record tipi oluşturmak için kullan
    type Kişi = {Ad:string; Soyad:string}

    // "let" ile kıvrık parantezi record tipi oluşturmak için kullan
    let kişi1 = {Ad="Falanca"; Soyad="Kişi"}

    // paketi açmak için kalıp eşleştirme kullan
    let {Ad = Ad} = kişi1    // birinci="John"

    // ------------------------------------
    // Union tipleri (değişkenler olarak da bilinir) birden fazla
    // seçeneğe sahiptir. Belli bir zamanda sadece bir tanesi geçerlidir.
    // ------------------------------------

    // "type" ile çubuk karakterini union tipi tanımlamak için kullan
    type Sıcaklık =
        | Santigrat of float
        | Fahrenhayt of float

    // Seçeneklerden birini kullan
    let derece1 = Fahrenhayt 98.6
    let derece2 = Santigrat 37.0

    // Paketi açmak için bütün seçenekler üzerinde kalıp eşleştirme kullan
    let dereceYazdır = function
       | Santigrat t -> printfn "%f C" t
       | Fahrenhayt t -> printfn "%f F" t

    dereceYazdır derece1
    dereceYazdır derece2

    // ------------------------------------
    // Yinelgen (Recursive) tipler
    // ------------------------------------

    // Tipler alt sınıflar oluşturmadan karmaşık şekillerde
    // yinelgen olarak birleştirilebilirler.
    type Çalışan =
      | İşçi of Kişi
      | Yönetici of Çalışan list

    let falancaKişi = {Ad="Falanca"; Soyad="Kişi"}
    let işçi = İşçi falancaKişi

    // ------------------------------------
    // Tipleri Kullanarak Modelleme
    // ------------------------------------

    // Union tipleri bayrak kullanmadan durum modelleme için harikadır.
    type EpostaAdresi =
        | GeçerliEpostaAdresi of string
        | GeçersizEpostaAdresi of string

    let epostaGöndermeyiDene eposta =
        match eposta with                     // kalıp eşleştirme kullan
        | GeçerliEpostaAdresi adres -> ()     // gönder
        | GeçersizEpostaAdresi adres -> ()    // gönderme

    // Union tiplerin record tiplerle birleşimi
    // domain driven design için iyi bir temel oluşturur.
    // Domain'i yansıtan yüzlerce ufak tip oluşturabilirsiniz.

    type Ürün = { ÜrünKodu: string; Miktar: int }
    type Ödeme = Ödeme of float
    type AktifSepetVerisi = { ÖdenmemişÜrünler: Ürün list }
    type ÖdenmişSepetVerisi = { ÖdenmişÜrünler: Ürün list; Ödeme: Ödeme}

    type AlışverişSepeti =
        | BosSepet  // veri yok
        | AktifSepet of AktifSepetVerisi
        | ÖdenmişSepet of ÖdenmişSepetVerisi

    // ------------------------------------
    // Tipler için içgüdüsel davranış
    // ------------------------------------

    // Çekirdek tipler kendinden çok kullanışlı özelliklere sahiptir
    // Ek kodlama gerektirmez
    // * Değişmezlik
    // * Debug ederken yazdırma
    // * Eşitlik ve kıyaslama
    // * Serialization

    // %A kullanarak yazdırma
    printfn "ikiliTuple=%A,\nKişi=%A,\Sıcaklık=%A,\nÇalışan=%A"
             ikiliTuple kişi1 derece1 işçi

    // Eşitlik ve kıyaslama içgüdüseldir.
    // İskambil kartlarıyla bir örnek
    type Simge = Sinek | Karo | Maça | Kupa
    type Sıra = İki | Üç | Dört | Beş | Altı | Yedi | Sekiz
                | Dokuz | On | Bacak | Kız | Papaz | As

    let el = [ Sinek, As; Kupa, Üç; Kupa, As;
                 Maça, Bacak; Karo, İki; Karo, As ]

    // Sıralama
    List.sort el |> printfn "artarak dizilen el: %A"
    List.max el |> printfn "en yüksek kart: %A"
    List.min el |> printfn "en düşük kart: %A"


// ================================================
// Aktif Kalıplar
// ================================================

module AktifKalıpÖrnekleri =

    // F# "aktif kalıplar" denen bir kalıp eşleştirmeye sahiptir.
    // Kalıplar dinamik bir şekilde tespit edilip eşleştirilebilir.

    // Aktif kalıplar için söz dizimi (| ... |) şeklindedir

    // Örneğin, karakter tiplerini eşleyen bir "aktif" kalıp tanımlayın...
    let (|Rakam|Harf|Boşluk|Diğer|) karakter =
       if System.Char.IsDigit(karakter) then Rakam
       else if System.Char.IsLetter(karakter) then Harf
       else if System.Char.IsWhiteSpace(karakter) then Boşluk
       else Diğer

    // ... daha sonra eşleme mantığı çok daha net yapmak için bunu kullanın
    let karakterYazdır karakter =
      match karakter with
      | Rakam -> printfn "%c bir rakamdır" karakter
      | Harf -> printfn "%c bir harftir" karakter
      | Boşluk -> printfn "%c bir boşluktur" karakter
      | _ -> printfn "%c başka bir şeydir" karakter

    // Bir liste yazdırma
    ['a'; 'b'; '1'; ' '; '-'; 'c'] |> List.iter karakterYazdır

    // -----------------------------------
    // Aktif Kalıpları Kullanarak FizzBuzz
    // -----------------------------------

    // Parçalı eşleşen kalıplar da oluşturabilirsiniz
    // Tanımda alt çizgi karakterini kullanın ve eşleşince Some döndürün.
    let (|ÜçünKatı|_|) i = if i % 3 = 0 then Some ÜçünKatı else None
    let (|BeşinKatı|_|) i = if i % 5 = 0 then Some BeşinKatı else None

    // Ana fonksiyon
    let fizzBuzz i =
      match i with
      | ÜçünKatı & BeşinKatı -> printf "FizzBuzz, "
      | ÜçünKatı -> printf "Fizz, "
      | BeşinKatı -> printf "Buzz, "
      | _ -> printf "%i, " i

    // test
    [1..20] |> List.iter fizzBuzz

// ================================================
// Sadelik
// ================================================

module AlgoritmaÖrnekleri =

    // F#'ın sinyal/gürültü oranı yüksektir, dolayısıyla
    // kod algoritmayla hemen hemen aynı görünür.

    // ------ Örnek: karelerToplami fonksiyonunu tanımla ------
    let karelerToplamı n =
       [1..n]                 // 1) 1'den n'e kadar bütün sayıları al
       |> List.map kare       // 2) hepsinin karesini al
       |> List.sum            // 3) sonuçları topla

    // test
    karelerToplamı 100 |> printfn "kareler toplamı = %A"

    // ------ Örnek: bir sıralama fonksiyonu tanımla ------
    let rec sırala liste =
       match liste with
       // Liste boşsa
       | [] ->
            []                              // boş listeyi döndür
       // Liste boş değilse
       | ilkEleman::diğerElemanlar ->       // İlk elemanı al
            let küçükElemanlar =            // Daha küçük elemanları 
                diğerElemanlar              // diğerlerinden ayır
                |> List.filter (fun e -> e < ilkEleman)
                |> sırala                   // ve sırala
            let büyükElemanlar =            // Daha büyük elemanları
                diğerElemanlar              // diğerlerinden ayır
                |> List.filter (fun e -> e >= ilkEleman)
                |> sırala                   // ve sırala
            // 3 parçayı birbirine ekle ve listeyi döndür
            List.concat [küçükElemanlar; [ilkEleman]; büyükElemanlar]

    // test
    sırala [1; 5; 23; 18; 9; 1; 3] |> printfn "Sırala = %A"

// ================================================
// Eşzamansız kod
// ================================================

module EşzamansızÖrneği =

    // F# "pyramid of doom" durumuyla karşılaştırmayacak şekilde
    // içgüdüsel eşzamansız özelliklere sahiptir.
    //
    // Bir sonraki örnek bir web sayfasını paralel bir şekilde indirir.

    open System.Net
    open System
    open System.IO
    open Microsoft.FSharp.Control.CommonExtensions

    // İçeriği eşzamansız bir şekilde getir
    let eşzamansızUrlGetir url =
        async {   // "async" anahtar kelimesi ve kıvrık parantez
                  // "async (eşzamansız)" nesneyi oluşturur
            let istek = WebRequest.Create(Uri(url))
            use! cevap = istek.AsyncGetResponse()
                // use! eşzamansız atamadır
            use akış = cevap.GetResponseStream()
                // "use" kullanılan bloğun dışına çıkınca
                // close()'u otomatik olarak tetikler
            use okuyucu = new IO.StreamReader(akış)
            let html = okuyucu.ReadToEnd()
            printfn "İndirme tamamlandı: %s" url
            }

    // İndirmek için bir web sitesi listesi
    let siteler = ["http://www.bing.com";
                 "http://www.google.com";
                 "http://www.microsoft.com";
                 "http://www.amazon.com";
                 "http://www.yahoo.com"]

    // İndir
    siteler
    |> List.map eşzamansızUrlGetir  // eşzamansız görevlerden oluşan bir liste yap
    |> Async.Parallel               // bu görevleri paralel çalışacak şekilde ayarla
    |> Async.RunSynchronously       // başlat

// ================================================
// .NET uyumluluğu
// ================================================

module NetUyumlulukÖrnekleri =

    // F#, C#'ın yapabildiği hemen herşeyi yapabilir,
    // ve .NET ve Mono kütüphaneleriyle tereyağından kıl çeker gibi çalışır.

    // ------- var olan kütüphane fonksiyonları ile çalışma -------

    let (i1başarılı, i1) = System.Int32.TryParse("123");
    if i1başarılı then printfn "%i olarak dönüştürüldü" i1 else printfn "dönüştürme başarısız"

    // ------- Arayüzleri yol üstünde tanımlayın! -------

    // IDisposable'ı sağlayan yeni bir nesne oluştur
    let kaynakOluştur isim =
       { new System.IDisposable
         with member this.Dispose() = printfn "%s atıldı" isim }

    let kaynakKullanVeAt =
        use r1 = kaynakOluştur "birinci kaynak"
        printfn "birinci kaynağı kullanıyor"
        for i in [1..3] do
            let kaynakİsmi = sprintf "\tiç kaynak %d" i
            use geçici = kaynakOluştur kaynakİsmi
            printfn "\t%s ile bir şey yap" kaynakİsmi
        use r2 = kaynakOluştur "ikinci kaynak"
        printfn "ikinci kaynağı kullanıyor"
        printfn "bitti."

    // ------- Nesne yönelimli kod -------

    // F# aynı zamanda tam bir nesne yönelimli dildir.
    // Sınıfları, kalıtımı ve sanal metotları destekler.

    // Genel tipli bir arayüz
    type IEnumerator<'a> =
        abstract member Şimdiki : 'a
        abstract SonrakineGeç : unit -> bool

    // Sanal metotları olan soyut temel sınıflar
    [<AbstractClass>]
    type Şekil() =
        // sadece okunabilir özellikler
        abstract member Genişlik : int with get
        abstract member Yükseklik : int with get
        // sanal olmayan metot
        member this.ÇevreleyenAlan = this.Yükseklik * this.Genişlik
        // temel uygulamasıyla bir sanal metot
        abstract member Yazdır : unit -> unit
        default this.Yazdır () = printfn "Ben bir şekil (önümden çekil!)"

    // Somut bir sınıfın soyut sınıftan kalıtımı
    type Dikdörtgen(x:int, y:int) =
        inherit Şekil()
        override this.Genişlik = x
        override this.Yükseklik = y
        override this.Yazdır ()  = printfn "Ben bir dikdörtgenim"

    // test
    let r = Dikdörtgen(2, 3)
    printfn "Genişlik: %i" r.Genişlik
    printfn "Çevreleyen Alan: %i" r.ÇevreleyenAlan
    r.Yazdır()

    // ------- ekleme metotları -------

    // C#'ta olduğu gibi F# da var olan sınıfları ekleme metotları ile genişletebilir.
    type System.String with
       member this.StartsWithA = this.StartsWith "A"

    // test
    let s = "Ahmet"
    printfn "'%s' 'A' ile başlar = %A" s s.StartsWithA

    // ------- olaylar -------

    type Butonum() =
        let tıklamaOlayı = new Event<_>()

        [<CLIEvent>]
        member this.OnClick = tıklamaOlayı.Publish

        member this.DenemeOlayı(arg) =
            tıklamaOlayı.Trigger(this, arg)

    // test
    let butonum = new Butonum()
    butonum.OnClick.Add(fun (sender, arg) ->
            printfn "arg=%O ile beraber bir tıklama olayı" arg)

    butonum.DenemeOlayı("Merhaba Dünya!")

```

## Daha fazla bilgi

F# hakkında daha fazla demo için [Try F#](http://www.tryfsharp.org/Learn) sitesine gidin, veya benim (yazarın) [why use F#](http://fsharpforfunandprofit.com/why-use-fsharp/) serimi okuyun.

F# hakkında daha fazla bilgi için: [fsharp.org](http://fsharp.org/).
---
language: kotlin
filename: kotlin-tr.kt
contributors:
        - ["Baha Can Aydın", "https://github.com/bahacan19"]
lang: tr-tr
---
Kotlin, JVM, Android ve tarayıcı için statik olarak yazılmış bir programlama dilidir.
Java %100 birlikte çalışabilir.
[Daha:](https://kotlinlang.org/)

```kotlin

// Tek satır yoruma almak için : //
/*
    Birkaç satırı yoruma almak için
*/

//  "package" anahtar kelimesi tıpkı Java'da olduğu gibidir.
package com.learnxinyminutes.kotlin

/*
Bir Kotlin programının başlama noktası (Java'da olduğu gibi) "com.learnxinyminutes.kotlin.main" metodudur.
Bu metoda komut satırından bir 'Array' gönderilebilir.
*/
fun main(args: Array<String>) {
    /*
    Bir değer tanımlamak için "var" ya da "val" anahtar kelimeleri kullanılıyor.
    "val" tanımlananlar tekrar atanamazken "var" tanımlananlar atanabilir.
    */
    val fooVal = 10 // fooVal değerini daha sonra tekrar atayamıyoruz
    var fooVar = 10
    fooVar = 20 // fooVar tekrar atanabilir.

    /*
    Çoğu zaman, Kotlin bir değişkenin tipini anlayabilir,
    bu yüzden her zaman belirtmeye gerek yoktur.
    Bir değişkenin tipini şöyle belirtebiliriz:
    */
    val foo: Int = 7

    /*
    String değerler Java'da olduğu gibi tanımlanır.
    */
    val fooString = "İşte String bu!"
    val barString = "Yeni satıra geçiyorum...?\nGeçtim!"
    val bazString = "Tab mı istedin?\tAl bakalım!"
    println(fooString)
    println(barString)
    println(bazString)

    /*
    Raw string, üçlü çift-tırnak sınırlandırılan String bloklarıdır.
    Tıpkı bir text editör gibi String tanımlamaya izin verir.
    */
    val fooRawString = """
fun helloWorld(val name : String) {
   println("Merhaba, dünya!")
}
"""
    println(fooRawString)

    /*
    String değerler, ($) işareti ile birtakım deyimler ve değerler içererbilir
    */
    val fooTemplateString = "$fooString değerinin ${fooString.length} adet karakteri vardır."
    println(fooTemplateString)

    /*
    Null atanabilen bir değişken nullable olarak tanımlanmalıdır.
    Bu, deişken tipinin sonuna ? eklenerek yapılabilir.
    Erişim ise '?.' operatörü ile yapılır.
    Bir değişken null ise, yerine kullaılacak alternatif bir değer belirtmek için
    '?:' operatörünü kullanırız.
    */
    var fooNullable: String? = "abc"
    println(fooNullable?.length) // => 3
    println(fooNullable?.length ?: -1) // => 3
    fooNullable = null
    println(fooNullable?.length) // => null
    println(fooNullable?.length ?: -1) // => -1

    /*
    Metodlar "fun" anahtar kelimesi ile tanımlanır.
    Metod argümanları, Metod adından sonra  parantez içinde belirtilir.
    Metod argümanlarının opsiyonel olarak default (varsayılan) değerleri olabilir.
    Metodun dönüş tipi, gerekirse, metod parentezinden sonra ':' operatörü ile belirtilir.
    */
    fun hello(name: String = "dünya"): String {
        return "Merhaba, $name!"
    }
    println(hello("foo")) // => Merhaba, foo!
    println(hello(name = "bar")) // => Merhaba, bar!
    println(hello()) // => Merhaba, dünya!

    /*
    Bir metoda çokca argüman göndermek için 'vararg' anahtar kelimesi
    kullanılır.
    */
    fun varargExample(vararg names: Int) {
        println("${names.size}  adet arguman paslanmıştır")
    }
    varargExample() // => 0 adet arguman paslanmıştır
    varargExample(1) // => 1 adet arguman paslanmıştır
    varargExample(1, 2, 3) // => 3 adet arguman paslanmıştır

    /*
    Bir metod tek bir ifadeden oluşuyorsa
    süslü parantezler yerine '=' kullanılabilir.
    */
    fun odd(x: Int): Boolean = x % 2 == 1
    println(odd(6)) // => false
    println(odd(7)) // => true

    // Eğer dönüş tipi anlaşılabiliyorsa ayrıca belirtmemize gerek yoktur.
    fun even(x: Int) = x % 2 == 0
    println(even(6)) // => true
    println(even(7)) // => false

    // Metodlar, metodları arguman ve dönüş tipi olarak alabilir
    fun not(f: (Int) -> Boolean): (Int) -> Boolean {
        return {n -> !f.invoke(n)} // bu satırdaki !f.invoke(n) metodu !f(n) şeklinde sadeleştirilebilir.
    }


    // Bir metodu sadece '::' ön eki ile de arguman olarak çağırabiliriz
    println(not(::odd)(4)) // ==> true

    // Metodlar değişken gibi atanabilir.
    val notOdd = not(::odd)
    val notEven = not(::even)

    // Lambda ifadeleri arguman olarak paslanabilir.
    val notZero = not {n -> n == 0}
    /*
    Eğer bir lambda fonksiyonu sadece bir arguman alıyorsa,
    '->' ifadesi atlanabilir, 'it' ifadesi ile belirtilebilir.
    */
    val notPositive = not { it > 0} // not(n -> n > 0) ifadesi ile aynı

    for (i in 0..4) {
        println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}")
    }

    /*
    * Diğer for döngüleri
    * */
    val myInt = 3
    for (i in 1..100) {  }  // kapalı aralık. 100 dahil.
    for (i in 1 until 100) {  } // 100 dahil değil
    for (x in 2..10 step 2) {  } // ikişer adımlı
    for (x in 10 downTo 1) {  } // Ondan geriye doğru. 1 dahil.
    if (myInt in 1..10) {  }



    /*
    Bir sınıf tanımlamak için 'class' anahtar kelimesi kullanılır.
    Kotlin'de bütün sınıflar varsayılan olarak 'final' tanımlanırlar.
    * */
    class ExampleClass(val x: Int) {

        fun memberFunction(y: Int): Int {
            return x + y
        }

        infix fun yTimes(y: Int): Int {
            return x * y
        }
    }
    /*
    * Bir sınıfı türetilebilir yapmak için 'open' anahtar kelimesi kullanılır.
    * */
    open class A

    class B : A()


    /*
    Yeni bir instance oluşturmak için doğrudan constructor çağırılır.
    Kotlinde 'new' anahtar kelimesi yoktur.
    */
    val fooExampleClass = ExampleClass(7)
    // Bir sınıfa üye metodları . (nokta) ile çağırabiliriz.
    println(fooExampleClass.memberFunction(4)) // => 11
    /*
    'infix' ön eki ile tanımlanan metodlar
    alışılan metod çağrısını daha kolay bir söz dizimine dönüştürür.
    */
    println(fooExampleClass yTimes 4) // => 28

    /*
    Data class lar sadece veri tutan sınıflar için uygun bir çözümdür.
    Bu şekilde tanımlanan sınıfların "hashCode"/"equals" ve "toString" metodları
    otomatik olarak oluşur.
    */
    data class DataClassExample (val x: Int, val y: Int, val z: Int)
    val fooData = DataClassExample(1, 2, 4)
    println(fooData) // => DataClassExample(x=1, y=2, z=4)

    // Data class ların copy metodları olur.
    val fooCopy = fooData.copy(y = 100)
    println(fooCopy) // => DataClassExample(x=1, y=100, z=4)

    // Destructuring Declarations, bir objeyi çoklu değişkenler ile ifade etme yöntemidir.
    val (a, b, c) = fooCopy
    println("$a $b $c") // => 1 100 4

    // bir 'for' döngüsü içinde 'Destructuring' :
    for ((a, b, c) in listOf(fooData)) {
        println("$a $b $c") // => 1 100 4
    }

    val mapData = mapOf("a" to 1, "b" to 2)
    // Map.Entry de destructurable gösterilebilir.
    for ((key, value) in mapData) {
        println("$key -> $value")
    }

    // 'with' metodu ile bir objeye bir lamda metodu uygulayabiliriz.
    data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
    val fooMutableData = MutableDataClassExample(7, 4, 9)
    with (fooMutableData) {
        x -= 2
        y += 2
        z--
    }

    println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8)

    /*
    'listOf' metodu ile bir liste oluşturulabilir.
    Oluşan liste immutable olacaktır, yani elaman eklenemez ve çıkarılamaz.
    */
    val fooList = listOf("a", "b", "c")
    println(fooList.size) // => 3
    println(fooList.first()) // => a
    println(fooList.last()) // => c
    // Elemanlara indexleri ile erişilebilir.
    println(fooList[1]) // => b

    // Mutable bir liste ise 'mutableListOf' metodu ile oluşturabilir.
    val fooMutableList = mutableListOf("a", "b", "c")
    fooMutableList.add("d")
    println(fooMutableList.last()) // => d
    println(fooMutableList.size) // => 4

    // Bir 'set' oluşturmak için 'setOf' metodunu kullanabiliriz.
    val fooSet = setOf("a", "b", "c")
    println(fooSet.contains("a")) // => true
    println(fooSet.contains("z")) // => false

    // 'mapOf' metodu ile 'map' oluşturabiliriz.
    val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9)
    // Map değerlerine ulaşmak için :
    println(fooMap["a"]) // => 8

    /*
    Sequence, Kotlin dilinde lazy-hesaplanan collection ları temsil eder.
    Bunun için 'generateSequence' metodunu kullanabiliriz. Bu metod bir önceki değerden
    bir sonraki değeri hesaplamak için gerekli bir lamda metodunu arguman olarak alır.
    */
    val fooSequence = generateSequence(1, { it + 1 })

    val x = fooSequence.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // Örneğin fibonacci serisi oluşturabilen bir 'Sequence' oluşturmak için:
    fun fibonacciSequence(): Sequence<Long> {
        var a = 0L
        var b = 1L

        fun next(): Long {
            val result = a + b
            a = b
            b = result
            return a
        }

        return generateSequence(::next)
    }
    val y = fibonacciSequence().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]


    // Kotlin Collection lar ile çalışmak için higher-order metodlar sağlar.
    val z = (1..9)
            .map {it * 3} // her bir elamanı 3 ile çarp
            .filter {it < 20} // 20 den küçük değerleri ele
            .groupBy {it % 2 == 0} // ikiye tam bölünen ve bölünmeyen şeklinde grupla (Map)
            .mapKeys {if (it.key) "even" else "odd"} // oluşan map in boolen 'key' lerini String bir değere dönüştür.
    println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]}

    // Bir 'for' döngüsü 'itearator' sağlayan her objeye uygulanabilir.
    for (c in "merhaba") {
        println(c)
    }

    // 'while' döngüsü diğer dillere benzer şekilde çalışır.
    var ctr = 0
    while (ctr < 5) {
        println(ctr)
        ctr++
    }
    do {
        println(ctr)
        ctr++
    } while (ctr < 10)

    /*
    'if' bir dönüş değeri olan deyim gibi de kullanılabilir.
    Bu sebepten Kotlin, Java'da bulunan '?:' ifadesi içermez.
    */
    val num = 5
    val message = if (num % 2 == 0) "even" else "odd"
    println("$num is $message") // => 5 is odd

    // 'if-else if' yapıları için 'when' kullanılabilir.
    val i = 10
    when {
        i < 7                         ->    println("first block")
        fooString.startsWith("hello") ->    println("second block")
        else                          ->    println("else block")
    }

    // 'when' bir parametre ile de kullanılabilir.
    when (i) {
        0, 21 -> println("0 or 21")
        in 1..20 -> println("in the range 1 to 20")
        else -> println("none of the above")
    }

    // 'when' dönüş değeri olan bir metod gibi de davranabilir.
    var result = when (i) {
        0, 21 -> "0 or 21"
        in 1..20 -> "in the range 1 to 20"
        else -> "none of the above"
    }
    println(result)


    /*
    Bir objenin tipini 'is' operatörü ile tayin edebiliriz.
    Eğer obje tip kontrolünü geçerse, cast etmeden doğrudan
    o tipteymiş gibi kullanılabilir.
    */
    fun smartCastExample(x: Any) : Boolean {
        if (x is Boolean) {
            // x otomatik olarak Boolean'a cast edilir.
            return x
        } else if (x is Int) {
            // x otomatik olarak Int tipine cast edilir.
            return x > 0
        } else if (x is String) {
            // x otomatik olarak String tipine cast edilir.
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(smartCastExample("Merhaba, dünya!")) // => true
    println(smartCastExample("")) // => false
    println(smartCastExample(5)) // => true
    println(smartCastExample(0)) // => false
    println(smartCastExample(true)) // => true

    // Smartcast 'when' bloğu ile de çalışır.
    fun smartCastWhenExample(x: Any) = when (x) {
        is Boolean -> x
        is Int -> x > 0
        is String -> x.isNotEmpty()
        else -> false
    }

    /*
    Extension lar, bir sınıfa fonksinolalite eklemenin bir yoludur.
    */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("Merhaba, dünya!".remove('a')) // => Merhb, düny!



    //Biraz detaylı Kotlin


    /*
     * Delegated Properties, bir değişken tanımlarken kullanılan birkaç standart yöntemler içerir.
     * https://kotlinlang.org/docs/reference/delegated-properties.html
     * En bilinen delegate property metodları: lazy(), observable()
     * */

    /*
     * Lazy, bir değişkeni ilk erişimde çalıştırılacak olan bir lambda ile tanımlama metodudur.
     * Sonraki erişimlerde değişkene atanan değer hatırlanır.
     * Lazy, synchronized bir delegation yöntemidir; değer sadece bir thread içinde hesaplanır,
     * tüm thread ler aynı değere erişir. Eğer senkronizasyon gerekli değilse, lazy metodu içine
     * LazyThreadSafetyMode.PUBLICATION paslanabilir.
     * */

    val lazyValue: String by lazy( {
        println("bi sn... hesaplıyorum....")
        "Selam!"
    })

    println(lazyValue)// bi sn... hesaplıyorum.... Selam!
    println(lazyValue) // Selam!
    /*
     * Observable, bir değişkende olabilecek yeniden atama değişikliklerini dinleme yöntemidir.
     * İki arguman alır; değişkenin ilk değeri, değiştiğinde çağrılan bir handler metodu. Handler
     * metodu değişken her değiştiğinde çağırılır.
     * */
    var myObservableName: String by Delegates.observable("<isim yok>") {
        prop, old, new ->
        println("$old -> $new")
    }
    myObservableName = "Baha" //<isim yok> -> Baha
    myObservableName = "Can"  //Baha -> Can


    /*
     * Eğer değişkenin yeniden atanmasını denetlemek isterek vetoable()
     * metodunu kullanabiliriz.
     * */

    var myVetoableName : String by Delegates.vetoable("<isim yok>"){
        property, oldValue, newValue ->
        if (newValue.length < 2) {
            println("Tek harfli isim kabul etmiyoruz!")
            false
        } else {
            println("$oldValue -> $newValue")
            true
        }
    }

    myVetoableName = "Baha" //<isim yok> -> Baha
    myVetoableName = "C"    //Tek harfli isim kabul etmiyoruz!
    println(myVetoableName) //Baha


    //singleton değişkene ulaşmak:
    println(ObjectExample.hello()) // => Merhaba
}

// Enum class lar Java'daki enum lara benzerdir.
enum class EnumExample {
    A, B, C
}

/*
'object' anahtar kelimesi ile singleton nesneler oluşturulabilir.
Bu şekilde tanımlanan sınıflardan yeni nesneler oluşturulamaz, sadece adı ile refere edilebilir.
*/
object ObjectExample {
    fun hello(): String {
        return "Merhaba"
    }
}

fun useObject() {
    ObjectExample.hello()
    val someRef: Any = ObjectExample
}

```

### İlerisi için:

* [Kotlin tutorials](https://kotlinlang.org/docs/tutorials/)
* [Try Kotlin in your browser](http://try.kotlinlang.org/)
* [A list of Kotlin resources](http://kotlin.link/)
* [Kotlin Koans in your IDE](https://kotlinlang.org/docs/tutorials/koans.html/)
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Eray AYDIN", "http://erayaydin.me/"]
lang: tr-tr
filename: markdown-tr.md
---

Markdown, 2004 yılında John Gruber tarafından oluşturuldu. Asıl amacı kolay okuma ve yazmayı sağlamakla beraber kolayca HTML (artık bir çok diğer formatlara) dönüşüm sağlamaktır.


```markdown
<!-- Markdown, HTML'i kapsar, yani her HTML dosyası geçerli bir Markdown dosyasıdır, bu demektir
ki Markdown içerisinde HTML etiketleri kullanabiliriz, örneğin bu yorum elementi, ve
markdown işleyicisinde etki etmezler. Fakat, markdown dosyası içerisinde HTML elementi oluşturursanız,
bu elementin içeriğinde markdown söz dizimlerini kullanamazsınız. -->

<!-- Markdown ayrıca işleyiciden işleyiciye farklılık gösterebilir. Bu rehberde
evrensel özelliklere uygun anlatımlar olacaktır. Bir çok işleyici bu rehberdeki
anlatımları destekler -->

<!-- Başlıklar -->
<!-- Kolayca <h1>'den <h6>'ya HTML etiketleri oluşturabilirsiniz.
Kare (#) sayısı bu elementin numarasını belirleyecek ve devamında getirdiğiniz
yazı bu elementin içeriği olacaktır
-->
# Bu bir <h1>
## Bu bir <h2>
### Bu bir <h3>
#### Bu bir <h4>
##### Bu bir <h5>
###### Bu bir <h6>

<!-- Markdown ayrıca h1 ve h2 için 2 alternatif yol daha taşır -->
Bu bir h1
=========

Bu bir h2
---------

<!-- Basit yazı stilleri -->
<!-- Markdown ile yazılar kolayca italik ve kalın belirtilebilir -->
*Bu yazı italik.*
_Bu yazı da italik._

**Bu yazı kalın.**
__Bu yazı da kalın.__

***Bu yazı hem kalın hem italik.***
**_Bu da öyle!_**
*__Hatta bu bile!__*

<!-- GitHub Flavored Markdown'da ayrıca üstü çizgili karakter de desteklenir: -->
~~Bu yazı üstü çizili olarak gözükecek.~~

<!-- Paragraflar bir veya daha fazla boş satırla ayrılır. -->

Bu bir paragraf. Paragrafın içeriğine devam ediyorum, eğlenceli değil mi?

Şimdi 2. paragrafıma geçtim.
Hala 2. paragraftayım, çünkü boş bir satır bırakmadım.

Bu da 3. paragrafım!

<!-- HTML'de her satır için <br /> etiketi kullanmak ister misiniz, Bir
paragrafı bitirdikten sonra 2 veya daha fazla boşluk bırakın ve yeni paragrafa
başlayın, bu bir <br /> etiketi sayılacaktır  -->

Bu yazının sonunda 2 boşluk var (bu satırı seçerek kontrol edebilirsiniz).  

Bir üst satırda <br /> etiketi var!

<!-- Blok yazılarının yapımı oldukça kolay, (>) karakteri ile yapabilirsiniz  -->

> Bu bir blok etiketi. Satırlara ayırmak için
> her satırın başında `>` karakter yerleştirmeli veya tek satırda bütün içeriği yazabilirsiniz.
> Satır `>` karakteri ile başladığı sürece sorun yok. 

> Ayrıca alt alta da blok elementi açabilirsiniz
>> iç içe yani
> düzgün değil mi ?

<!-- Listeler -->
<!-- Numarasız listeler için yıldız, artı, veya tire kullanabilirsiniz -->

* Nesne
* Nesne
* Bir başka nesne

veya

+ Nesne
+ Nesne
+ Bir başka nesne

veya

- Nesne
- Nesne
- Son bir nesne

<!-- Numaralı liste için başına sıralı bir şekilde sayı eklemeniz yeterli -->

1. İlk nesne
2. İkinci nesne
3. Üçüncü nesne

<!-- İsterseniz sıralı bir şekilde yazmak zorunda değilsiniz, markdown
biçimlendirirken sizin için sıralayacaktır, fakat bunu önermiyorum. Markdown dosyasının
düzgün gözükmesi için önerilen metodu uygulamanızı tavsiye ederim -->

1. İlk nesne
1. İkinci nesne
1. Üçüncü nesne

<!-- (Bunun çıktısı ile, sıralı olarak yazdığımız örneğin çıktısı aynı olacaktır) -->

<!-- Ayrıca alt alta liste oluşturabilirsiniz -->

1. İlk nesne
2. İkinci nesne
3. Üçüncü nesne
    * Alt nesne
    * Alt nesne
4. Dördüncü nesne

<!-- Ayrıca görev listeleri de bulunmakta. HTML seçim kutusu(checkbox) oluşturacaktır. -->
Kutunun içerisinde `x` yoksa eğer seçim kutusu boş olacaktır.
- [ ] Yapılacak ilk görev.
- [ ] Yapılması gereken bir başka görev
Aşağıdaki seçim kutusu ise içi dolu olacaktır.
- [x] Bu görev başarıyla yapıldı

<!-- Kod blokları -->
<!-- Kod bloklarını(<code> elementi) belirtmek için 4 adet boşluk veya bir
tab karakterini kullanabilirsiniz -->

    Bu bir kod
    öyle mi?

<!-- Ayrıca kod içerisinde girinti kullanmak istiyorsanız tekrar `tab` veya `4 boşluk`
kullanabilirsiniz -->

    my_array.each do |item|
        puts item
    end

<!-- Yazı içerisinde kod belirtmek için sorgu tırnağı (`) kullanabilirsiniz -->

Ahmet `go_to()` fonksiyonun ne yaptığını bilmiyor!

<!-- GitHub Flavored Markdown'da, kod içerisinde aydınlatma kullanabilirsiniz -->

\`\`\`ruby <!-- buradaki ters slaş (\) işaretlerini kullanmayın, sadece ```ruby ! -->
def foobar
    puts "Hello world!"
end
\`\`\` <!-- burada da (\) işaretlerini kullanmayın, sadece ``` -->

<!-- Yukarıdaki örnekte girinti kullanmanıza gerek yok, GitHub da 
``` işaretinden sonra belirttiğiniz yazılım diline göre gerekli
syntax aydınlatmaları uygulanacaktır -->

<!-- Düz çizgi (<hr />) -->
<!-- Düz çizgiler 3 veya daha fazla yıldız/çizgi ile yapılabilir. Boşluklar önemsiz. -->

***
---
- - -
****************

<!-- Linkler -->
<!-- Markdown'daki en güzel şeylerden biri kolayca link oluşturmaktır. 
Linkte göstermek istediğiniz yazıyı [] içerisine yerleştirin ve sonuna parantezler içerisinde ()
gideceği adresi belirtin -->

[Bana tıkla!](http://test.com)

<!-- Ayrıca linke `title` özelliği eklemek için tırnakları kullanabilirsiniz -->

[Bana tıkla!](http://test.com "Test.com'a gider")

<!-- Bağıl yollar da çalışıyor. -->
[Müzik dinle](/muzik/).

<!-- Markdown ayrıca referans linklerini de destekler -->

[Bu linke tıklayarak][link1] daha detaylı bilgi alabilirsiniz!
[Ayrıca bu linki de inceleyin][foobar] tabi istiyorsanız.

[link1]: http://test.com/ "harika!"
[foobar]: http://foobar.biz/ "okey!"

<!--Başlık ayrıca tek tırnak veya parantez içinde olabilir, veya direk yazılabilir.
Referans döküman içerisindeki herhangi bir yer olabilir ve referans IDsi 
benzersiz olduğu sürece sorunsuz çalışacaktır. -->

<!-- Ayrıca "dolaylı adlandırma" bulunmaktadır, "dolaylı adlandırma", linkin yazısının
aynı zamanda onun idsi olmasıdır -->

[Bu][] bir link.
[bu]: http://bubirlink.com

<!-- Fakat bu çok tercih edilen bir yöntem değil. -->

<!-- Resimler -->
<!-- Resimler aslında linklere çok benziyor fakat başında ünlem bulunuyor! -->
![Bu alt etiketine gelecek içerik](http://imgur.com/resmim.jpg "Bu da isteğe bağlı olan bir başlık")

<!-- Referanslar resimler için de geçerli -->
![Bu alt etiketi.][resmim]

[resmim]: bagil/linkler/de/calisiyor.jpg "Başlık isterseniz buraya girebilirsiniz"

<!-- Çeşitli -->
<!-- Oto-linkler -->

<http://testwebsite.com/> ile
[http://testwebsite.com/](http://testwebsite.com) aynı şeyler

<!-- Oto-linkler epostaları da destekler -->

<foo@bar.com>

<!-- Kaçış karakterleri -->

Bu yazının *yıldızlar arasında gözükmesini* istiyorum fakat italik olmamasını istiyorum,
bunun için, şu şekilde: \*bu yazı italik değil, yıldızlar arasında\*.

<!-- Tablolar -->
<!-- Tablolar sadece GitHub Flavored Markdown'da destekleniyor ve açıkçası
performansı çok yoruyorlar, fakat illa ki kullanmak isterseniz: -->

| Sütun1       | Sütun 2  | Sütün 3       |
| :----------- | :------: | ------------: |
| Sola dayalı  | Ortalı   | Sağa dayalı   |
| test         | test     | test          |

<!-- ayrıca, bunun aynısı -->

Sütun 1 | Sütun 2 | Sütun 3
:-- | :-: | --:
Çok çirkin göözüküyor | değil | mi?

<!-- Bitiş! -->

```

Daha detaylı bilgi için, John Gruber'in resmi söz dizimi yazısını [buradan](http://daringfireball.net/projects/markdown/syntax) veya Adam Pritchard'ın mükemmel hatırlatma kağıdını [buradan](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) inceleyebilirsiniz.
---
language: Objective-C
contributors:
    - ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
    - ["Yannick Loriot", "https://github.com/YannickL"]
filename: LearnObjectiveC-tr.m
translators:
    - ["Haydar KULEKCI", "http://scanf.info/"]
lang: tr-tr
---

Objective-C Apple tarafından, OSX ve iOS işletim sistemleri ve onların
kendi çatıları olan Cocoa ve Cocoa Touch için kullanılan bir programlama dilidir.
Genel açamlı, object-oriented bir yapıya sahip programlama dilidir. C 
programlama diline Smalltalk stilinde mesajlaşma ekler.  

```objective-c
// Tek satır yorum // işaretleri ile başlar

/*
Çoklu satır yorum bu şekilde görünür.
*/

// #import ile Foundation başlıklarını projeye import edebiliriz. 
#import <Foundation/Foundation.h>
#import "MyClass.h"

// Progarmınızı girişi bir main fonksiyonudur ve bir integer değer döner.
int main (int argc, const char * argv[])
{
    // Programdaki bellek kullanımını kontrol etmek için autorelease bir 
    // oluşturuyoruz. Autorelease bellekte kullanılmayan değerlerin kendi 
    // kendini silmesi demektir.
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    // NSLog konsola bir satırlık bilgi yazdırmak için kullanılır.
    NSLog(@"Hello World!"); // "Hello World!" değeri yazdırılır.
 
    ///////////////////////////////////////
    // Tipler & Değişkenler
    ///////////////////////////////////////
    
    // Basit Tanımlamalar
    int myPrimitive1  = 1;
    long myPrimitive2 = 234554664565;
    
    // Nesne Tanımlamaları
    // strongly-typed nesne tanımlaması için karakter değişken isminin önüne 
    // * karakteri konulur.
    MyClass *myObject1 = nil;  // Strong typing
    id       myObject2 = nil;  // Weak typing
    // %@ bir nesnedir.
    // 'description' objelerin değerlerinin gösterilmesi için bir düzendir.
    NSLog(@"%@ and %@", myObject1, [myObject2 description]); 
            // "(null) and (null)" yazdırılacaktır.
    
    // Karakter Dizisi (String)
    NSString *worldString = @"World";
    NSLog(@"Hello %@!", worldString); // "Hello World!" yazdırılacaktır.
    
    // Karakterler
    NSNumber *theLetterZNumber = @'Z';
    char theLetterZ            = [theLetterZNumber charValue];
    NSLog(@"%c", theLetterZ);

    // Tamsayılar
    NSNumber *fortyTwoNumber = @42;
    int fortyTwo             = [fortyTwoNumber intValue];
    NSLog(@"%i", fortyTwo);
    
    NSNumber *fortyTwoUnsignedNumber = @42U;
    unsigned int fortyTwoUnsigned    = [fortyTwoUnsignedNumber unsignedIntValue];
    NSLog(@"%u", fortyTwoUnsigned);
    
    NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42];
    short fortyTwoShort           = [fortyTwoShortNumber shortValue];
    NSLog(@"%hi", fortyTwoShort);
    
    NSNumber *fortyTwoLongNumber = @42L;
    long fortyTwoLong            = [fortyTwoLongNumber longValue];
    NSLog(@"%li", fortyTwoLong);

    // Kayan Noktalı Sayılar (Floats)
    NSNumber *piFloatNumber = @3.141592654F;
    float piFloat           = [piFloatNumber floatValue];
    NSLog(@"%f", piFloat);
    
    NSNumber *piDoubleNumber = @3.1415926535;
    piDouble                 = [piDoubleNumber doubleValue];
    NSLog(@"%f", piDouble);

    // BOOL Değerler
    NSNumber *yesNumber = @YES;
    NSNumber *noNumber  = @NO;

    // Dizi objeleri
    NSArray *anArray      = @[@1, @2, @3, @4];
    NSNumber *thirdNumber = anArray[2];
    NSLog(@"Third number = %@", thirdNumber); // "Third number = 3" yazdırılır

    // Dictionary objeleri
    NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
    NSObject *valueObject     = aDictionary[@"A Key"];
    NSLog(@"Object = %@", valueObject); // "Object = (null)" yazıdılır

    ///////////////////////////////////////
    // Operatörler
    ///////////////////////////////////////
    
    // Operatörler C dilindeki gibi çalışır.
    // Örneğin:
    2 + 5; // => 7
    4.2f + 5.1f; // => 9.3f
    3 == 2; // => 0 (NO)
    3 != 2; // => 1 (YES)
    1 && 1; // => 1 (Logical and)
    0 || 1; // => 1 (Logical or)
    ~0x0F; // => 0xF0 (bitwise negation)
    0x0F & 0xF0; // => 0x00 (bitwise AND)
    0x01 << 1; // => 0x02 (bitwise left shift (by 1))

    ///////////////////////////////////////
    // Kontrol Yapıları
    ///////////////////////////////////////

    // If-Else ifadesi
    if (NO)
    {
        NSLog(@"I am never run");
    } else if (0)
    {
        NSLog(@"I am also never run");
    } else
    {
        NSLog(@"I print");
    }

    // Switch ifadesi
    switch (2)
    {
        case 0:
        {
            NSLog(@"I am never run");
        } break;
        case 1:
        {
            NSLog(@"I am also never run");
        } break;
        default:
        {
            NSLog(@"I print");
        } break;
    }
    
    // While döngü ifadesi
    int ii = 0;
    while (ii < 4)
    {
        NSLog(@"%d,", ii++); // ii++, ii değişkenini kullanıldıktan 
                             //sonra yerinde artırır.
    } // =>        "0," 
      //           "1,"
      //           "2,"
      //           "3," yazdırılır

    // For döngü ifadesi
    int jj;
    for (jj=0; jj < 4; jj++)
    {
        NSLog(@"%d,", jj++);
    } // =>        "0," 
      //           "1,"
      //           "2,"
      //           "3," yazdırılır
     
    // Foreach ifadesi             
    NSArray *values = @[@0, @1, @2, @3];
    for (NSNumber *value in values)
    {
        NSLog(@"%@,", value);
    } // =>        "0," 
      //           "1,"
      //           "2,"
      //           "3," yazdırılır

    // Try-Catch-Finally ifadesi
    @try
    {
        // İfadelerinizi buraya yazın
        @throw [NSException exceptionWithName:@"FileNotFoundException"
                            reason:@"Sistemde Dosya Bulunamadı" userInfo:nil];
    } @catch (NSException * e)
    {
        NSLog(@"Exception: %@", e);
    } @finally
    {
        NSLog(@"Finally");
    } // =>        "Exception: Sistemde Dosya Bulunamadı"
      //           "Finally"
      // yazdırılacaktır
 
    ///////////////////////////////////////
    // Objeler
    ///////////////////////////////////////
    
    // Bellekten bir alan ayırmak ve objeyi burada oluşturmak bir obje örneği 
    // oluşturalım. Bir obje allocate ve init aşamalarını bitirmeden tam olarak
    // işlevsel değildir.
    MyClass *myObject = [[MyClass alloc] init];
    
    // Objective-C nesne yönelimli programlama modelinin temelinde objelere 
    // mesaj gönderme vardır.
    // Objective-C'de bir method çağırılmaz, ona bir mesaj gönderilir.
    [myObject instanceMethodWithParameter:@"Steve Jobs"];

    // Programda kullanılan bellek temizlenir
    [pool drain];
    
    // Program Sonu
    return 0;
}

///////////////////////////////////////
// Sınıflar ve Fonksiyonlar
///////////////////////////////////////

// Sınıfınızı (MyClass.h) header dosyasında tanımlayın:

// Sınıf tanımlama yapısı:
// @interface ClassName : ParentClassName <ImplementedProtocols>
// {
//    Üye değişken (member variable) tanımlaması;
// }
// -/+ (type) Method tanımlaması;
// @end
@interface MyClass : NSObject <MyCustomProtocol>
{
    int count;
    id data;
    NSString *name;
}
// getter ve setter için otomatik oluşturulmuş gösterim.
@property int count;
@property (copy) NSString *name; // Copy the object during assignment.
@property (readonly) id data;    // Declare only a getter method.

// Metodlar
+/- (return type)methodSignature:(Parameter Type *)parameterName;

// "+" class metodları içindir
+ (NSString *)classMethod;

// "-" instance metodu içindir
- (NSString *)instanceMethodWithParmeter:(NSString *)string;
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;

@end

// Metodların implementasyonlarını (MyClass.m) dosyasında yapıyoruz:

@implementation UserObject

// Obje bellekten silineceği (release) zaman çağırılır
- (void)dealloc
{
}

// Constructor'lar sınıf oluşturmanın bir yoludur
// Bu varsayılan bir constructor'dur ve bir obje oluşturulurken çağrılır.
- (id)init
{
    if ((self = [super init]))
    {
        self.count = 1;
    }
    return self;
}

+ (NSString *)classMethod
{
    return [[self alloc] init];
}

- (NSString *)instanceMethodWithParmeter:(NSString *)string
{
    return @"New string";
}

- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number
{
    return @42;
}

// MyProtocol içerisinde metod tanımlamaları
- (void)myProtocolMethod
{
    // ifadeler
}

@end

/*
 * Bir `protocol` herhangi bir sınıf tarafından implement edilen metodları tanımlar 
 * `Protocol`ler sınıfların kendileri değildir. Onlar basitçe diğer objelerin 
 * implementasyon için sorumlu oldukları bir arayüz (interface) tanımlarlar. 
 */
@protocol MyProtocol
    - (void)myProtocolMethod;
@end



```
## Daha Fazla Okuma

[Vikipedi Objective-C](http://tr.wikipedia.org/wiki/Objective-C)

[Objective-C Öğrenme](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/)

[Lise Öğrencileri için iOS: Başlangıç](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started)
---
language: PHP
filename: learnphp-tr.php
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
translators:
    - ["Haydar KULEKCI", "http://scanf.info/"]
lang: tr-tr
---

PHP 5+ versiyonu için geçerlidir.

```php
<?php // PHP kodları <?php etiketleri içerisinde bulunmalıdır.

// Eğer php dosyanız sadece PHP kodu içeriyorsa, php'nin kapatma 
// etiketini kapatmayabilirsiniz. 

// // işareti ile tek satırlık yorum satırı başlar.

# # işareti de aynı görevi görür ancak // daha genel kullanımdadır.



/*
    Çoklu satır kodu bu şekilde yazıyoruz. slash yıldız ile başlar 
    ve yıldız slash ile biter.
*/

// "echo" ya da "print" metodları çıktı almak için kullanılır.
print('Hello '); // Ekrana Yeni satır karakteri olmadan "Hello " 
                 // yazdırılır.

// () parantezler "echo" ve "print" metodları için isteğe bağlıdır. 
echo "World\n"; // Ekrana yeni satır karakteri olmadan "World"
                // yazdırılır.
// (Bütün ifadeler noktalı virgül ile bitmelidir.)

// <?php tagı dışarısındaki herşey otomatik olarak yazdırılacaktır.
?>
Hello World Again!
<?php


/************************************
*   Tipler ve Değişkenler
*************************************/

// Değişkenler $ sembolü ile başlar.
// Geçerli bir değişken bir harf veya alt çizgi ile başlar,
// devamında da bir sayı, harf veya alt çizgi ile devam eder. 

// Mantıksal değerler 
// Boolean values are harf büyüklüğüne duyarsızdır.
$boolean = true;  // veya TRUE veya True
$boolean = false; // veya FALSE veya False

// Tam Sayılar
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (öneki 0 olan sekizlik(octal) bir sayıyı gösterir)
$int4 = 0x0F; // => 15 (öneki 0x olanlar hex sayıları gösterir.)

// Kayan Noktalı Sayılar
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// Değişken Silmek
unset($int1)

// Aritmetik
$sum        = 1 + 1; // 2
$difference = 2 - 1; // 1
$product    = 2 * 2; // 4
$quotient   = 2 / 1; // 2

// Aritmetik Kısayolları
$number = 0;
$number += 1;      // $number değişkeninin değerini 1 artırır.
echo $number++;    // 1 yazdırılır. (Yazdırıldıktan sonra artırılır.)
echo ++$number;    // 3 yazdırılır. (yazdırılmadan önce artırılır.)
$number /= $float; // Bölünür ve bölüm $number değerine eşitlenir.

// Karakter dizileri(strings) tek tırnak ile kullanılmalıdır.
$sgl_quotes = '$String'; // => '$String'

// Bir değişken içermediği sürece çift tırnak kullanmaktan kaçının
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// Özel karakterler sadece çift tırnak ile kullanılabilir.
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// Gerekirse bir değişkeni küme ayracı içine alın.
$money = "I have $${number} in the bank.";

// Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners
$nowdoc = <<<'END'
Multi line
string
END;

// Heredocs will do string interpolation
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// . işareti ile karakter dizileri birbirine birleştirilebilir.
echo 'This string ' . 'is concatenated';


/********************************
 * Sabitler
 */

// Bir sabit define() metodu kullanılarak tanımlanır.
// ve çalışma zamanından hiçbir zaman değişmez!

// geçerli bir sabit bir harf veya altçizgi ile başlar,
// ve bir sayı, harf ya da altçizgi ile devam eder.
define("FOO",     "something");

// sabite ulaşmak için direk olarak seçilen ismi kullanabilirsiniz. 
echo 'This outputs '.FOO;


/********************************
 * Diziler
 */

// PHP'de bütün diziler ilişikilendirilebilirdir. (hashmaps),
// İlişkilendirilebilir(associative) diziler, hashmap olarak bilinir. 

// PHP'nin bütün versiyonları için çalışır
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);

// PHP 5.4 ile yeni bir söz dizimi kullanılmaya başlandı
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];

echo $associative['One']; // 1 yazdıracaktır.

// Liste kullanımında index'ler tam sayıdır.
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"

// Dizinin sonuna bir eleman ekleme
$array[] = 'Four';

// Diziden eleman silme
unset($array[3]);

/********************************
 * Çıktı
 */

echo('Hello World!');
// Hello World! çıktısı stdout'a yazdırılır.
// Eğer bir web browser ile çalışıyorsanır Stdout bir web sayfasıdır.

print('Hello World!'); // echo ile aynıdır.

// Aslında echo bir dil sabitidir, parantezleri kullanmayabilirsiniz. 
echo 'Hello World!';
print 'Hello World!'; // Bu yazdırılacaktır. 

$paragraph = 'paragraph';

echo 100;        // Echo ile doğrudan sayısal değer kullanımı
echo $paragraph; // veya değişken

// PHP 5.4.0 veya daha üstü sürümlerde kısa açılış etiketi 
// konfigürasyonları yapıldı ise, kısa açılış etiketini kullanabilirsiniz.
?>
<p><?= $paragraph ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // Şu anda $x değişkeni $y ile aynı değere sahiptir.
$z = &$y;
// $z, $y'nin referansını içermektedir. 
// $z'nin değerinin değişmesi $y'nin değerinide değiştireceltir veya
// tam tersi. Ancak $x özgün değeri olarak kalacaktır.

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0

// Dump'lar değişkenin tipi ve değerini yazdırır
var_dump($z); // int(0) yazdırılacaktır

// Print'ler ise değişkeni okunabilir bir formatta yazdıracaktır. 
print_r($array); // Çıktı: Array ( [0] => One [1] => Two [2] => Three )


/********************************
 * Mantık
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// Argüman doğru değilse bir hata fırlatılacaktır.

// Bu karşılaştırmalar tipler aynı olmasa bile her zaman true olacaktır.
assert($a == $b); // equality
assert($c != $a); // inequality
assert($c <> $a); // alternative inequality
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// Aşağıdakiler yanlızca değer ve tip aynı olduğunda true olacaktır.
assert($c === $d);
assert($a !== $d);
assert(1 == '1');
assert(1 !== '1');

// Değişkenler kullanıma bağlı olarak farklı tiplere çevrilebilir.

$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2 (Karakter dizisi tam sayıya çevrilmeye zorlanır)

$string = 'one';
echo $string + $string; // => 0
// Çıktı 0 olacaktır, çünkü + operatörü karakter dizisi olan 'one' değerini
// bir sayıya çeviremez.

// Veri tipi çevirileri bir değişkeni farklı bir türde 
// düzenlemek için kullanılabilir.

$boolean = (boolean) 1; // => true

$zero = 0;
$boolean = (boolean) $zero; // => false

// Veri tiplerini çevirmek için bazı fonksiyonlar vardır.
$integer = 5;
$string = strval($integer);

$var = null; // Null değeri.


/********************************
 * Kontrol Yapıları
 */

if (true) {
    print 'I get printed';
}

if (false) {
    print 'I don\'t';
} else {
    print 'I get printed';
}

if (false) {
    print 'Does not get printed';
} elseif(true) {
    print 'Does';
}

// Üçlü operatör
print (false ? 'Does not get printed' : 'Does');

$x = 0;
if ($x === '0') {
    print 'Does not print';
} elseif($x == '1') {
    print 'Does not print';
} else {
    print 'Does print';
}



// Bu alternatif sözdizimi template'ler için kullanışlıdır.
?>

<?php if ($x): ?>
This is displayed if the test is truthy.
<?php else: ?>
This is displayed otherwise.
<?php endif; ?>

<?php

// Use switch to save some logic.
switch ($x) {
    case '0':
        print 'Switch does type coercion';
        break; // Bir breake yazmalısınız ya da 'two' ve 'three'
               // durumunuda kapsarsınız.
    case 'two':
    case 'three':
        // $variable değişkeni 'two' ya da 'three' ise 
        break;
    default:
        // Varsayılan olarak bir şey yap
}

// While, do...while ve for döngüleri tanıdıktır.
$i = 0;
while ($i < 5) {
    echo $i++;
}; // "01234" yazdırılacaktır

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // "01234" yazdırılacaktır.

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // "0123456789" yazdırılacaktır.

echo "\n";

$wheels = ['bicycle' => 2, 'car' => 4];

// Foreach döngüsü diziler üzerinde çalışır
foreach ($wheels as $wheel_count) {
    echo $wheel_count;
} // "24" yazdırılacaktır.

echo "\n";

// Key-Value değerlere ulaşabilirsiniz.
foreach ($wheels as $vehicle => $wheel_count) {
    echo "A $vehicle has $wheel_count wheels";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // while döngüsünden çıkar
    }
    echo $i++;
} // Prints "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // Aktif döngüyü atlar 
    }
    echo $i;
} // "0124" yazdırılacaktır.



/********************************
 * Fonksiyonlar
 */

// Bir fonksiyon tanımlamak için "function" kullanılır:
function my_function () {
  return 'Hello';
}

echo my_function(); // => "Hello"

// Geçerli bir fonksiyon ismi bir harf veya altçizgi ile başlar ve
// bir sayı, harf ya da alt çizgi ile devam eder.

function add ($x, $y = 1) { // $y değeri isteğe bağlıdır ve 
                            // varsayılan değeri 1'dir
  $result = $x + $y;
  return $result;
}

echo add(4); // => 5
echo add(4, 2); // => 6

// $result fonksiyon dışında ulaşılabilir değildir. 
// print $result; // Bir uyarı verecektir.

// PHP 5.3'den beri bir anonymous fonksiyon tanımlayabilirsiniz;
$inc = function ($x) {
  return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
  echo "$x - $y - $z";
}

// Fonksiyonlar bir fonksiyon dönebilir.
function bar ($x, $y) {
  // Fonksiyona dışardan değişken gönderebilmek için 'use' komutunu kullanın.
  return function ($z) use ($x, $y) {
    foo($x, $y, $z);
  };
}

$bar = bar('A', 'B');
$bar('C'); // "A - B - C" yazdırılacaktır.

// Fonksiyonun ismini karakter dizinden çağırabilirsiniz. 
$function_name = 'add';
echo $function_name(1, 2); // => 3
// Programatik olarak fonksiyonları çalıştırmak için yararlı olabilir
// veya, call_user_func(callable $callback [, $parameter [, ... ]]); kulanın.


/********************************
 * Includes
 */

<?php
// PHP'de include edilecek dosyalar PHP açma etiketi ile başlamalı. (!)

include 'my-file.php';
// my-file.php dosyasındaki kodlar artık mevcut scope'da kullanılabilir.
// Eğer dosya include edilemezse bir uyarı (örneğin: file not found) 
// fırlatılacaktır.

include_once 'my-file.php';
// Eğer bir dosya include edildi ise tekrar include edilmeyecektir.
// Bu çoklu class tanımlama hatalarını önler.

require 'my-file.php';
require_once 'my-file.php';
// Dosya include edilemediğinde fatal error veren require() bu konu 
// dışında include() ile aynıdır.

// my-include.php dosyasının içeriği:
<?php

return 'Anything you like.';
// Dosya sonu

// Include'lar ver require'lar aynı zamanda bir return dönebilir.
$value = include 'my-include.php';

// Dosyalar verilen dosya yoluna göre include edilir veya, hiçbirşey 
// verilmezse include_path konfigürasyonuna göre include edecektir. 
// Eğer dosya include_path'da da bulunamazsa include hata vermeden 
// önce içinde bulunulan dizini kontrol ecektir.
/* */


/********************************
 * Sınıflar
 */

// Sınıflar class kelimesi ile tanımlanır

class MyClass
{
    const MY_CONST      = 'value'; // Bir sabit

    static $staticVar   = 'static';

    // Static değişkenler ve onların görünürlüğü
    public static $publicStaticVar = 'publicStatic';
    private static $privateStaticVar = 'privateStatic'; 
        // Sadece bu sınıf içerisinde erişilebilir
    protected static $protectedStaticVar = 'protectedStatic'; 
        // Bu sınıf ve alt sınıflarından erişilebilir

    // Özellikler görünürlüğü ile birlikte tanımlanmalıdır.
    public $property    = 'public';
    public $instanceProp;
    protected $prot = 'protected'; // Sınıf ve alt sınıflardan erişilebilir
    private $priv   = 'private';   // Sadece bu sınıftan erişilebilir

    // __construct ile bir constructor oluşturulabilir.
    public function __construct($instanceProp) {
        // $this ile instance değişkenine erişilir.
        $this->instanceProp = $instanceProp;
    }

    // Sınıfın içerisinde metodlar fonksiyonlar gibi tanımlanır.
    public function myMethod()
    {
        print 'MyClass';
    }

    //final anahtar kelimesi bu metodu override edilemez yapacaktır.
    final function youCannotOverrideMe()
    {
    }

/*
Bir sınıfın özelliğini ya da metodunu statik yaptığınız takdirde sınıfın bir 
objesini oluşturmadan bu elemana erişebilirsiniz. Bir özellik statik tanımlanmış 
ise obje üzerinden bu elemana erişilemez. (Statik metodlar öyle değildir.)
*/


    public static function myStaticMethod()
    {
        print 'I am static';
    }
}

echo MyClass::MY_CONST;    // 'value' şeklinde çıktı verir;
echo MyClass::$staticVar;  // 'static' şeklinde çıktı verir;
MyClass::myStaticMethod(); // 'I am static' şeklinde çıktı verir;

// Sınıfların new ile kullanımı
$my_class = new MyClass('An instance property');
// Eğer argüman göndermeyeceksek parantezler isteğe bağlıdır.

// Sınıfın üyelerine erişim ->
echo $my_class->property;     // => "public"
echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod();        // => "MyClass"


// "extends" ile sınıfı extend etmek
class MyOtherClass extends MyClass
{
    function printProtectedProperty()
    {
        echo $this->prot;
    }

    // Bir methodu ezmek
    function myMethod()
    {
        parent::myMethod();
        print ' > MyOtherClass';
    }
}

$my_other_class = new MyOtherClass('Instance prop');
$my_other_class->printProtectedProperty(); // "protected" şeklinde çıktı verir.
$my_other_class->myMethod();    // "MyClass > MyOtherClass" şeklinde çıktı verir

final class YouCannotExtendMe
{
}

// getter ve setter'ları oluşturmak için "magic method"ları kullanabilirsiniz.
class MyMapClass
{
    private $property;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MyMapClass();
echo $x->property; // __get() metodunu kullanacaktır.
$x->property = 'Something'; // __set() metodunu kullanacaktır. 

// Sınıflar abstract olabilir(abstract kelimesini kullanarak) veya
// interface'ler uygulanabilir (implements kelimesi kullanılarak).
// Bir interface "interface" kelimesi kullanılarak oluşturulur.

interface InterfaceOne
{
    public function doSomething();
}

interface InterfaceTwo
{
    public function doSomethingElse();
}

// interfaces can be extended
interface InterfaceThree extends InterfaceTwo
{
    public function doAnotherContract();
}

abstract class MyAbstractClass implements InterfaceOne
{
    public $x = 'doSomething';
}

class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
{
    public function doSomething()
    {
        echo $x;
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


// Sınıflar birden fazla interface kullanabilir.
class SomeOtherClass implements InterfaceOne, InterfaceTwo
{
    public function doSomething()
    {
        echo 'doSomething';
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}



/********************************
 * Traits
 */
// Trait'ler PHP 5.4.0'dan beri kullanılabilir ve "trait" kullanılarak 
// tanımlanır.

trait MyTrait
{
    public function myTraitMethod()
    {
        print 'I have MyTrait';
    }
}

class MyTraitfulClass
{
    use MyTrait;
}

$cls = new MyTraitfulClass();
$cls->myTraitMethod(); // "I have MyTrait" çıktısını verir.



/********************************
 * İsim Uzayları
 */

// Bu alan ayrılmıştır, çünkü isim uzayı tanımı bir dosyada en başta 
// yapılmalıdır. Bu örnekte böyle olmadığını varsayalım.

<?php

// Varsayılan olarak, sınıflar global isim uzayındadır, ve açıkça bir 
// ters slash ile çağrılabilir.

$cls = new \MyClass();



// Bir dosya için isim uzayı atama
namespace My\Namespace;

class MyClass
{
}

// (diğer bir dosya)
$cls = new My\Namespace\MyClass;

//veya diğer bir isim uzayında.
namespace My\Other\Namespace;

use My\Namespace\MyClass;

$cls = new MyClass();

// veya isim uzayına bir takma isim koyabilirsiniz.

namespace My\Other\Namespace;

use My\Namespace as SomeOtherNamespace;

$cls = new SomeOtherNamespace\MyClass();

*/

```

## Daha fazla bilgi

Referans ve topluluk yazıları için [official PHP documentation](http://www.php.net/manual/) adresini ziyaret edin.

Güncel en yi örnekler için [PHP Usulüne Uygun](http://kulekci.net/php-the-right-way/) adresini ziyaret edin.

Eğer bir paket yöneticisi olan dil kullandıysanız, [Composer](http://getcomposer.org/)'a bir göz atın.

Genel standartlar için PHP Framework Interoperability Group'unun [PSR standards](https://github.com/php-fig/fig-standards) ziyaret edebilirsiniz. 

---
language: python
filename: learnpython-tr.py
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["Haydar KULEKCI", "http://scanf.info/"]
lang: tr-tr
---
Python Guido Van Rossum tarafından 90'ların başında yaratılmıştır. Şu anda 
varolanlar arasında en iyi dillerden birisidir. Ben (Louie Dinh) Python
dilinin syntax'ının belirginliğine aşığım. O basit olarak çalıştırılabilir 
pseudocode'dur.

Geri bildirimlerden son derece mutluluk duyarım! Bana [@louiedinh](http://twitter.com/louiedinh)
adresinden ya da louiedinh [at] [google's email service] adresinden ulaşabilirsiniz.

Çeviri için geri bildirimleri de [@kulekci](http://twitter.com/kulekci) 
adresine yapabilirsiniz. 

Not: Bu yazıdaki özellikler Python 2.7 için geçerlidir, ama Python 2.x için de 
uygulanabilir. Python 3 için başka bir zaman tekrar bakınız. 


```python
# Tek satır yorum hash işareti ile başlar.
""" Çoklu satır diziler üç tane çift tırnak 
    arasında yazılır. Ve yorum olarak da 
    kullanılabilir
"""


####################################################
## 1. İlkel Veri Tipleri ve Operatörler
####################################################

# Sayılar
3 #=> 3

# Matematik beklediğiniz gibi
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7

# Bölünme biraz ilginç. EĞer tam sayılar üzerinde bölünme işlemi yapıyorsanız
# sonuç otomatik olarak kırpılır. 
5 / 2 #=> 2

# Bölünme işlemini düzenlemek için kayan noktalı sayıları bilmeniz gerekir.
2.0     # Bu bir kayan noktalı sayı
11.0 / 4.0 #=> 2.75 ahhh...daha iyi

# İşlem önceliğini parantezler ile sağlayabilirsiniz.
(1 + 3) * 2 #=> 8

# Boolean değerleri bilindiği gibi
True
False

# not ile nagatif(mantıksal) değerini alma
not True #=> False
not False #=> True

# Eşitlik ==
1 == 1 #=> True
2 == 1 #=> False

# Eşitsizlik !=
1 != 1 #=> False
2 != 1 #=> True

# Daha fazla karşılaştırma
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

# Karşılaştırma zincirleme yapılabilir!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False

# Karakter dizisi " veya ' ile oluşturulabilir
"This is a string."
'This is also a string.'

# Karakter dizileri birbirleri ile eklenebilir
"Hello " + "world!" #=> "Hello world!"

# A string can be treated like a list of characters
# Bir string'e karakter listesi gibi davranabilirsiniz. 
"This is a string"[0] #=> 'T'

# % karakter dizisini(string) formatlamak için kullanılır, bunun gibi:
"%s can be %s" % ("strings", "interpolated")

# String'leri formatlamanın yeni bir yöntem ise format metodudur. 
# Bu metod tercih edilen yöntemdir.
"{0} can be {1}".format("strings", "formatted")
# Eğer saymak istemiyorsanız anahtar kelime kullanabilirsiniz.
"{name} wants to eat {food}".format(name="Bob", food="lasagna")

# None bir objedir
None #=> None

# "==" eşitliğini non objesi ile karşılaştırmak için kullanmayın.
# Onun yerine "is" kullanın.
"etc" is None #=> False
None is None  #=> True

# 'is' operatörü obje kimliği için test etmektedir. Bu ilkel değerler
# için kullanışlı değildir, ama objeleri karşılaştırmak için kullanışlıdır.

# None, 0 ve boş string/list'ler False olarak değerlendirilir.
# Tüm eşitlikler True döner
0 == False  #=> True
"" == False #=> True


####################################################
## 2. Değişkenler ve Kolleksiyonlar
####################################################

# Ekrana yazdırma oldukça kolaydır.
print "I'm Python. Nice to meet you!"


# Değişkenlere bir değer atamadan önce tanımlamaya gerek yoktur.
some_var = 5    # Değişken isimlerinde gelenek küçük karakter ve alt çizgi 
                # kullanmaktır.
some_var #=> 5

# Daha önceden tanımlanmamış ya da assign edilmemeiş bir değişkene erişmeye 
# çalıştığınızda bir hata fırlatılacaktır. Hata ayıklama hakkında daha fazla 
# bilgi için kontrol akışı kısmına göz atınız.
some_other_var  # isim hatası fırlatılır

# isterseniz "if"i bir ifade gibi kullanabilirsiniz.
"yahoo!" if 3 > 2 else 2 #=> "yahoo!"

# Listeler
li = []
# Önceden değerleri tanımlanmış listeler
other_li = [4, 5, 6]

# Bir listenin sonuna birşeyler eklemek
li.append(1)    #li şu anda [1]
li.append(2)    #li şu anda [1, 2]
li.append(4)    #li şu anda [1, 2, 4]
li.append(3)    #li şu anda [1, 2, 4, 3]
# pop ile sondan birşeyler silmek
li.pop()        #=> 3 and li is now [1, 2, 4]
# Tekrar sonuna eklemek
li.append(3)    # li is now [1, 2, 4, 3] again.

# Dizi gibi listenin elemanlarına erişmek
li[0] #=> 1
# Son elemanın değerine ulaşmak
li[-1] #=> 3

# Listede bulunmayan bir index'teki elemana erişirken "IndexError" hatası 
# fırlatılır 
li[4] # IndexError fırlatılır

# slice syntax'ı ile belli aralıktakı değerlere bakabilirsiniz.
# (Açık ve kapalı aralıklıdır.)
li[1:3] #=> [2, 4]
# Başlangıcı ihmal etme
li[2:] #=> [4, 3]
# Sonu ihmal etme
li[:3] #=> [1, 2, 4]

# "del" ile istenilen bir elemanı listeden silmek
del li[2] # li is now [1, 2, 3]

# Listeleri birbiri ile birleştirebilirsiniz.
li + other_li #=> [1, 2, 3, 4, 5, 6] - Not: li ve other_li yanlız bırakılır

# extend ile listeleri birleştirmek
li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6]

# bir değerin liste içerisinde varlığını "in" ile kontrol etmek
1 in li #=> True

# "len" ile listenin uzunluğunu bulmak
len(li) #=> 6

# Tüpler listeler gibidir sadece değişmezler(immutable)
tup = (1, 2, 3)
tup[0] #=> 1
tup[0] = 3  # TypeError fırlatılır.

# Litelerde yapılanların hepsini tüplerde de yapılabilir
len(tup) #=> 3
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
tup[:2] #=> (1, 2)
2 in tup #=> True

# Tüplerin(veya listelerin) içerisindeki değerleri değişkenelere
# atanabilir
a, b, c = (1, 2, 3)     # a şu anda 1, b şu anda 2 ve c şu anda 3
# Eğer parantez kullanmaz iseniz tüpler varsayılan olarak oluşturulur
d, e, f = 4, 5, 6
# şimdi iki değeri değiş tokuş etmek çok kolaydır.
e, d = d, e     # d şimdi 5 ve e şimdi 4


# Sözlükler (Dictionaries) key-value saklanır.
empty_dict = {}
# Sözlüklere önceden değer atama örneği
filled_dict = {"one": 1, "two": 2, "three": 3}

# Değere ulaşmak için [] kullanılır
filled_dict["one"] #=> 1

# Tüm anahtarlara(key) "keys()" metodu ile ulaşılır
filled_dict.keys() #=> ["three", "two", "one"]
# Not - Sözlüklerin anahtarlarının sıralı geleceği garanti değildir
# Sonuçlarınız değer listesini aldığınızda tamamen eşleşmeyebilir 

# Tüm değerleri almak için "values()" kullanabilirsiniz.
filled_dict.values() #=> [3, 2, 1]
# Not - Sıralama ile ilgili anahtarlar ile aynı durum geçerlidir.

# Bir anahtarın sözlükte oluş olmadığını "in" ile kontrol edilebilir
"one" in filled_dict #=> True
1 in filled_dict #=> False

# Olmayan bir anahtar çağrıldığında KeyError fırlatılır.
filled_dict["four"] # KeyError

# "get()" metodu KeyError fırlatılmasını önler
filled_dict.get("one") #=> 1
filled_dict.get("four") #=> None
# get() metodu eğer anahtar mevcut değilse varsayılan bir değer atama
# imknaı sağlar.
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4

# "setdefault()" metodu sözlüğe yeni bir key-value eşleşmesi eklemenin 
# güvenli bir yoludur.
filled_dict.setdefault("five", 5) #filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6) #filled_dict["five"] is still 5


# Sets store ... well sets
empty_set = set()
# Bir demek değer ile bir "set" oluşturmak
some_set = set([1,2,2,3,4]) # some_set is now set([1, 2, 3, 4])

# Python 2.7'den beri {}'ler bir "set" tanımlaman için kullanılabilir
filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}

# Bir set'e daha fazla eleman eklemek
filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}

# "&" işareti ile iki set'in kesişimlerini alınabilir
other_set = {3, 4, 5, 6}
filled_set & other_set #=> {3, 4, 5}

# | işareti ile 
filled_set | other_set #=> {1, 2, 3, 4, 5, 6}

# "-" işareti ile iki set'in farkları alınabilir
{1,2,3,4} - {2,3,5} #=> {1, 4}

# "in" ile değerin set içerisinde olup olmadığını kontrol edebilirsiniz
2 in filled_set #=> True
10 in filled_set #=> False


####################################################
## 3. Akış Denetimi
####################################################

# Bir değişken oluşturmak
some_var = 5

# Buradaki bir if ifadesi. Girintiler(Intentation) Python'da önemlidir!
# "some_var is smaller than 10" yazdırılır.
if some_var > 10:
    print "some_var is totally bigger than 10."
elif some_var < 10:    # elif ifadesi isteğe bağlıdır
    print "some_var is smaller than 10."
else:           # Bu da isteğe bağlıdır.
    print "some_var is indeed 10."


"""
For döngüleri listeler üzerinde iterasyon yapar
Ekrana yazdırılan:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # Biçimlendirmeleri string'e katmak için % kullanabilirsiniz
    print "%s is a mammal" % animal
    
"""
"range(number)" ifadesi sıfırdan verilen sayıya kadar bir sayı listesi döner
Ekrana yazdırılan:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
While döngüsü koşul sağlanmayana kadar devam eder
Ekrana yazdırılan:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # Shorthand for x = x + 1

# try/except bloğu ile hatalar ayıklanabilir

# Python 2.6 ve üstü için çalışacaktır:
try:
    # "raise" bir hata fırlatmak için kullanılabilir
    raise IndexError("This is an index error")
except IndexError as e:
    pass    # Pass is just a no-op. Usually you would do recovery here.


####################################################
## 4. Fonksiyonlar
####################################################


# Yeni bir fonksiyon oluşturmak için "def" kullanılır
def add(x, y):
    print "x is %s and y is %s" % (x, y)
    return x + y    # Return values with a return statement

# Fonksiyonu parametre ile çağırmak
add(5, 6) #=> prints out "x is 5 and y is 6" and returns 11

# Diğer bir yol fonksiyonları anahtar argümanları ile çağırmak
add(y=6, x=5)   # Anahtar argümanlarının sırası farklı da olabilir

# Değişken sayıda parametresi olan bir fonksiyon tanımlayabilirsiniz
def varargs(*args):
    return args

varargs(1, 2, 3) #=> (1,2,3)

# Değişken sayıda anahtar argümanlı parametre alan fonksiyonlar da 
# tanımlayabilirsiniz.
def keyword_args(**kwargs):
    return kwargs

# Şu şekilde kullanılacaktır
keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}

# Eğer isterseniz ikisini aynı anda da yapabilirsiniz
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) prints:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Fonksiyonu çağırırken, args/kwargs'ın tam tersini de yapabilirsiniz!
# Tüpü yaymak için * ve kwargs'ı yaymak için ** kullanın.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # foo(1, 2, 3, 4) ile eşit
all_the_args(**kwargs) # foo(a=3, b=4) ile eşit
all_the_args(*args, **kwargs) # foo(1, 2, 3, 4, a=3, b=4) ile eşit

# Python first-class fonksiyonlara sahiptir
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3) #=> 13

# Anonymous fonksiyonlar da vardır
(lambda x: x > 2)(3) #=> True

# Dahili yüksek seviye fonksiyonlar vardır
map(add_10, [1,2,3]) #=> [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]

# Map etme(maps) ve filtreleme(filtres) için liste kullanabiliriz. 
[add_10(i) for i in [1, 2, 3]]  #=> [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]


####################################################
## 5. Sınıflar
####################################################

# We subclass from object to get a class.
class Human(object):
    
    # Bir sınıf özelliği. Bu sınıfın tüm "instance"larına paylaşılmıştır.
    species = "H. sapiens"

    # Basic initializer
    def __init__(self, name):
        # Metoda gelen argümanın değerini sınıfın elemanı olan "name" 
        # değişkenine atama
        self.name = name

    # Bir instance metodu. Tüm metodlar ilk argüman olarak "self" 
    # parametresini alır
    def say(self, msg):
       return "%s: %s" % (self.name, msg)

    # Bir sınıf metodu tüm "instance"lar arasında paylaşılır
    # İlk argüman olarak sınıfı çağırarak çağrılırlar
    @classmethod
    def get_species(cls):
        return cls.species

    # Bir statik metod bir sınıf ya da instance referansı olmadan çağrılır
    @staticmethod
    def grunt():
        return "*grunt*"


# Bir sınıf örneği oluşturmak
i = Human(name="Ian")
print i.say("hi")     # "Ian: hi" çıktısı verir

j = Human("Joel")
print j.say("hello")  # "Joel: hello" çıktısı verir

# Sınıf metodunu çağıralım
i.get_species() #=> "H. sapiens"

# Paylaşılan sınıf özellik değiştirelim.
Human.species = "H. neanderthalensis"
i.get_species() #=> "H. neanderthalensis"
j.get_species() #=> "H. neanderthalensis"

# Statik metodu çağırma
Human.grunt() #=> "*grunt*"


####################################################
## 6. Modüller
####################################################

# Modülleri sayfaya dahil edebilirsiniz
import math
print math.sqrt(16) #=> 4

# Modül içerisinden spesifik bir fonksiyonu getirebilirsiniz
from math import ceil, floor
print ceil(3.7)  #=> 4.0
print floor(3.7) #=> 3.0

# Modüldeki tüm fonksiyonları dahil edebilirsiniz
# Uyarı: bu önerilmez
from math import *

# Modülün adını kısaltabilirsiniz
import math as m
math.sqrt(16) == m.sqrt(16) #=> True

# Python modülleri sıradan python dosyalarıdır. Kendinize bir modül 
# yazabilirsiniz, ve dahil edebilirsiniz. Modülün adı ile dosya adı
# aynı olmalıdır.

# Modüllerde tanımlanmış fonksiyon ve metodları öğrenebilirsiniz.
import math
dir(math)



```

## Daha fazlası için hazır mısınız?

### Ücretsiz Dökümanlar

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)

### Dead Tree

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["Andre Polykanine", "https://github.com/Oire"]
translators:
    - ["Eray AYDIN", "http://erayaydin.me/"]
lang: tr-tr
filename: learnpython3-tr.py
---

Python,90ların başlarında Guido Van Rossum tarafından oluşturulmuştur. En popüler olan dillerden biridir. Beni Python'a aşık eden sebep onun syntax beraklığı. Çok basit bir çalıştırılabilir söz koddur.

Not: Bu makale Python 3 içindir. Eğer Python 2.7 öğrenmek istiyorsanız [burayı](http://learnxinyminutes.com/docs/python/) kontrol edebilirsiniz.

```python

# Tek satırlık yorum satırı kare(#) işareti ile başlamaktadır.

""" Çok satırlı olmasını istediğiniz yorumlar
    üç adet tırnak(") işareti ile
    yapılmaktadır
"""

####################################################
## 1. Temel Veri Türleri ve Operatörler
####################################################

# Sayılar
3  # => 3

# Tahmin edebileceğiniz gibi matematik
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20

# Bölme işlemi varsayılan olarak onluk döndürür
35 / 5  # => 7.0

# Tam sayı bölmeleri, pozitif ve negatif sayılar için aşağıya yuvarlar
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # onluklar için de bu böyledir
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# Onluk kullanırsanız, sonuç da onluk olur
3 * 2.0 # => 6.0

# Kalan operatörü
7 % 3 # => 1

# Üs (2 üzeri 4)
2**4 # => 16

# Parantez ile önceliği değiştirebilirsiniz
(1 + 3) * 2  # => 8

# Boolean(Doğru-Yanlış) değerleri standart
True
False

# 'değil' ile terse çevirme
not True  # => False
not False  # => True

# Boolean Operatörleri
# "and" ve "or" büyük küçük harf duyarlıdır
True and False #=> False
False or True #=> True

# Bool operatörleri ile sayı kullanımı
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True 
2 == True #=> False 
1 == True #=> True

# Eşitlik kontrolü ==
1 == 1  # => True
2 == 1  # => False

# Eşitsizlik Kontrolü !=
1 != 1  # => False
2 != 1  # => True

# Diğer karşılaştırmalar
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# Zincirleme şeklinde karşılaştırma da yapabilirsiniz!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# Yazı(Strings) " veya ' işaretleri ile oluşturulabilir
"Bu bir yazı."
'Bu da bir yazı.'

# Yazılar da eklenebilir! Fakat bunu yapmanızı önermem.
"Merhaba " + "dünya!"  # => "Merhaba dünya!"

# Bir yazı(string) karakter listesi gibi işlenebilir
"Bu bir yazı"[0]  # => 'B'

# .format ile yazıyı biçimlendirebilirsiniz, şu şekilde:
"{} da ayrıca {}".format("yazılar", "işlenebilir")

# Biçimlendirme işleminde aynı argümanı da birden fazla kullanabilirsiniz.
"{0} çeviktir, {0} hızlıdır, {0} , {1} üzerinden atlayabilir".format("Ahmet", "şeker çubuğu")
#=> "Ahmet çeviktir, Ahmet hızlıdır, Ahmet , şeker çubuğu üzerinden atlayabilir"

# Argümanın sırasını saymak istemiyorsanız, anahtar kelime kullanabilirsiniz.
"{isim} yemek olarak {yemek} istiyor".format(isim="Ahmet", yemek="patates") #=> "Ahmet yemek olarak patates istiyor"

# Eğer Python 3 kodunuz ayrıca Python 2.5 ve üstünde çalışmasını istiyorsanız, 
# eski stil formatlamayı kullanabilirsiniz:
"%s bu %s yolla da %s" % ("yazılar", "eski", "biçimlendirilebilir")


# Hiçbir şey(none) da bir objedir
None  # => None

# Bir değerin none ile eşitlik kontrolü için "==" sembolünü kullanmayın
# Bunun yerine "is" kullanın. Obje türünün eşitliğini kontrol edecektir.
"vb" is None  # => False
None is None  # => True

# None, 0, ve boş yazılar/listeler/sözlükler hepsi False değeri döndürü.
# Diğer veriler ise True değeri döndürür
bool(0)  # => False
bool("")  # => False
bool([]) #=> False
bool({}) #=> False


####################################################
## 2. Değişkenler ve Koleksiyonlar
####################################################

# Python bir yazdırma fonksiyonuna sahip
print("Ben Python. Tanıştığıma memnun oldum!")

# Değişkenlere veri atamak için önce değişkeni oluşturmanıza gerek yok. 
# Düzenli bir değişken için hepsi_kucuk_ve_alt_cizgi_ile_ayirin
bir_degisken = 5
bir_degisken  # => 5

# Önceden tanımlanmamış değişkene erişmek hata oluşturacaktır.
# Kontrol akışları başlığından hata kontrolünü öğrenebilirsiniz.
bir_bilinmeyen_degisken  # NameError hatası oluşturur

# Listeler ile sıralamaları tutabilirsiniz
li = []
# Önceden doldurulmuş listeler ile başlayabilirsiniz
diger_li = [4, 5, 6]

# 'append' ile listenin sonuna ekleme yapabilirsiniz
li.append(1)    # li artık [1] oldu
li.append(2)    # li artık [1, 2] oldu
li.append(4)    # li artık [1, 2, 4] oldu
li.append(3)    # li artık [1, 2, 4, 3] oldu
# 'pop' ile listenin son elementini kaldırabilirsiniz
li.pop()        # => 3 ve li artık [1, 2, 4]
# Çıkarttığımız tekrardan ekleyelim
li.append(3)    # li yeniden [1, 2, 4, 3] oldu.

# Dizi gibi listeye erişim sağlayın
li[0]  # => 1
# Son elemente bakın
li[-1]  # => 3

# Listede olmayan bir elemente erişim sağlamaya çalışmak IndexError hatası oluşturur
li[4]  # IndexError hatası oluşturur

# Bir kısmını almak isterseniz.
li[1:3]  # => [2, 4]
# Başlangıç belirtmezseniz
li[2:]  # => [4, 3]
# Sonu belirtmesseniz
li[:3]  # => [1, 2, 4]
# Her ikişer objeyi seçme
li[::2]   # =>[1, 4]
# Listeyi tersten almak
li[::-1]   # => [3, 4, 2, 1]
# Kombinasyonları kullanarak gelişmiş bir şekilde listenin bir kısmını alabilirsiniz
# li[baslangic:son:adim]

# "del" ile isteğe bağlı, elementleri listeden kaldırabilirsiniz
del li[2]   # li artık [1, 2, 3] oldu

# Listelerde de ekleme yapabilirsiniz
# Not: değerler üzerinde değişiklik yapılmaz.
li + diger_li   # => [1, 2, 3, 4, 5, 6] 

# Listeleri birbirine bağlamak için "extend()" kullanılabilir
li.extend(diger_li)   #  li artık [1, 2, 3, 4, 5, 6] oldu

# Listedeki bir elementin olup olmadığı kontrolü "in" ile yapılabilir
1 in li   # => True

# Uzunluğu öğrenmek için "len()" kullanılabilir
len(li)   # => 6


# Tüpler listeler gibidir fakat değiştirilemez.
tup = (1, 2, 3)
tup[0]   # => 1
tup[0] = 3  # TypeError hatası oluşturur

# Diğer liste işlemlerini tüplerde de uygulayabilirsiniz
len(tup)   # => 3
tup + (4, 5, 6)   # => (1, 2, 3, 4, 5, 6)
tup[:2]   # => (1, 2)
2 in tup   # => True

# Tüpleri(veya listeleri) değişkenlere açabilirsiniz
a, b, c = (1, 2, 3)     # 'a' artık 1, 'b' artık 2 ve 'c' artık 3
# Eğer parantez kullanmazsanız varsayılan oalrak tüpler oluşturulur
d, e, f = 4, 5, 6
# 2 değeri birbirine değiştirmek bu kadar kolay
e, d = d, e     # 'd' artık 5 ve 'e' artık 4


# Sözlükler anahtar kodlarla verileri tutar
bos_sozl = {}
# Önceden doldurulmuş sözlük oluşturma
dolu_sozl = {"bir": 1, "iki": 2, "uc": 3}

# Değere bakmak için [] kullanalım
dolu_sozl["bir"]   # => 1

# Bütün anahtarları almak için  "keys()" kullanılabilir. 
# Listelemek için list() kullanacağınız çünkü dönen değerin işlenmesi gerekiyor. Bu konuya daha sonra değineceğiz.
# Not - Sözlük anahtarlarının sıralaması kesin değildir.
# Beklediğiniz çıktı sizinkiyle tam uyuşmuyor olabilir.
list(dolu_sozl.keys())   # => ["uc", "iki", "bir"]


# Tüm değerleri almak için "values()" kullanacağız. Dönen değeri biçimlendirmek için de list() kullanmamız gerekiyor
# Not - Sıralama değişebilir.
list(dolu_sozl.values())   # => [3, 2, 1]


# Bir anahtarın sözlükte olup olmadığını "in" ile kontrol edebilirsiniz
"bir" in dolu_sozl   # => True
1 in dolu_sozl   # => False

# Olmayan bir anahtardan değer elde etmek isterseniz KeyError sorunu oluşacaktır.
dolu_sozl["dort"]   # KeyError hatası oluşturur

# "get()" metodu ile değeri almaya çalışırsanız KeyError sorunundan kurtulursunuz
dolu_sozl.get("bir")   # => 1
dolu_sozl.get("dort")   # => None
# "get" metoduna parametre belirterek değerin olmaması durumunda varsayılan bir değer döndürebilirsiniz.
dolu_sozl.get("bir", 4)   # => 1
dolu_sozl.get("dort", 4)   # => 4

# "setdefault()" metodu sözlükte, belirttiğiniz anahtarın [olmaması] durumunda varsayılan bir değer atayacaktır
dolu_sozl.setdefault("bes", 5)  # dolu_sozl["bes"] artık 5 değerine sahip
dolu_sozl.setdefault("bes", 6)  # dolu_sozl["bes"] değişmedi, hala 5 değerine sahip

# Sözlüğe ekleme
dolu_sozl.update({"dort":4}) #=> {"bir": 1, "iki": 2, "uc": 3, "dort": 4}
#dolu_sozl["dort"] = 4  #sözlüğe eklemenin bir diğer yolu

# Sözlükten anahtar silmek için 'del' kullanılabilir
del dolu_sozl["bir"]  # "bir" anahtarını dolu sözlükten silecektir


# Setler ... set işte :D 
bos_set = set()
# Seti bir veri listesi ile de oluşturabilirsiniz. Evet, biraz sözlük gibi duruyor. Üzgünüm.
bir_set = {1, 1, 2, 2, 3, 4}   # bir_set artık {1, 2, 3, 4}

# Sete yeni setler ekleyebilirsiniz
dolu_set = bir_set

# Sete bir diğer öğe ekleme 
dolu_set.add(5)   # dolu_set artık {1, 2, 3, 4, 5} oldu

# Setlerin çakışan kısımlarını almak için '&' kullanabilirsiniz
diger_set = {3, 4, 5, 6}
dolu_set & diger_set   # => {3, 4, 5}

# '|' ile aynı olan elementleri almayacak şekilde setleri birleştirebilirsiniz
dolu_set | diger_set   # => {1, 2, 3, 4, 5, 6}

# Farklılıkları almak için "-" kullanabilirsiniz
{1, 2, 3, 4} - {2, 3, 5}   # => {1, 4}

# Bir değerin olup olmadığının kontrolü için "in" kullanılabilir
2 in dolu_set   # => True
10 in dolu_set   # => False


####################################################
## 3. Kontrol Akışları ve Temel Soyutlandırma
####################################################

# Bir değişken oluşturalım
bir_degisken = 5

# Burada bir "if" ifadesi var. Girinti(boşluk,tab) python için önemlidir!
# çıktı olarak "bir_degisken 10 dan küçük" yazar
if bir_degisken > 10:
    print("bir_degisken 10 dan büyük")
elif bir_degisken < 10:    # Bu 'elif' ifadesi zorunlu değildir.
    print("bir_degisken 10 dan küçük")
else:                  # Bu ifade de zorunlu değil.
    print("bir_degisken değeri 10")


"""
Döngülerle lsiteleri döngüye alabilirsiniz
çıktı:
    köpek bir memeli hayvandır
    kedi bir memeli hayvandır
    fare bir memeli hayvandır
"""
for hayvan in ["köpek", "kedi, "fare"]:
    # format ile kolayca yazıyı biçimlendirelim
    print("{} bir memeli hayvandır".format(hayvan))

"""
"range(sayi)" bir sayı listesi döndür
0'dan belirttiğiniz sayıyıa kadar
çıktı:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
'While' döngüleri koşul çalıştıkça işlemleri gerçekleştirir.
çıktı:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # Uzun hali x = x + 1

# Hataları kontrol altına almak için try/except bloklarını kullanabilirsiniz
try:
    # Bir hata oluşturmak için "raise" kullanabilirsiniz
    raise IndexError("Bu bir index hatası")
except IndexError as e:
    pass    # Önemsiz, devam et.
except (TypeError, NameError):
    pass    # Çoklu bir şekilde hataları kontrol edebilirsiniz, tabi gerekirse.
else:   # İsteğe bağlı bir kısım. Eğer hiçbir hata kontrol mekanizması desteklemiyorsa bu blok çalışacaktır
    print("Her şey iyi!")   # IndexError, TypeError ve NameError harici bir hatada bu blok çalıştı

# Temel Soyutlandırma, bir objenin işlenmiş halidir.
# Aşağıdaki örnekte; Obje, range fonksiyonuna temel soyutlandırma gönderdi.

dolu_sozl = {"bir": 1, "iki": 2, "uc": 3}
temel_soyut = dolu_sozl.keys()
print(temel_soyut) #=> range(1,10). Bu obje temel soyutlandırma arayüzü ile oluşturuldu

# Temel Soyutlandırılmış objeyi döngüye sokabiliriz.
for i in temel_soyut:
    print(i)    # Çıktısı: bir, iki, uc

# Fakat, elementin anahtarına değerine.
temel_soyut[1]  # TypeError hatası!

# 'iterable' bir objenin nasıl temel soyutlandırıldığıdır.
iterator = iter(temel_soyut)

# 'iterator' o obje üzerinde yaptığımız değişiklikleri hatırlayacaktır
# Bir sonraki objeyi almak için __next__ fonksiyonunu kullanabilirsiniz.
iterator.__next__()  #=> "bir"

# Bir önceki __next__ fonksiyonumuzu hatırlayıp bir sonraki kullanımda bu sefer ondan bir sonraki objeyi döndürecektir
iterator.__next__()  #=> "iki"
iterator.__next__()  #=> "uc"

# Bütün nesneleri aldıktan sonra bir daha __next__ kullanımınızda, StopIterator hatası oluşturacaktır.
iterator.__next__() # StopIteration hatası

# iterator'deki tüm nesneleri almak için list() kullanabilirsiniz.
list(dolu_sozl.keys())  #=> Returns ["bir", "iki", "uc"]


####################################################
## 4. Fonksiyonlar
####################################################

# "def" ile yeni fonksiyonlar oluşturabilirsiniz
def topla(x, y):
    print("x = {} ve y = {}".format(x, y))
    return x + y    # Değer döndürmek için 'return' kullanmalısınız

# Fonksiyonu parametleri ile çağırıyoruz
topla(5, 6)   # => çıktı "x = 5 ve y = 6" ve değer olarak 11 döndürür

# Bir diğer fonksiyon çağırma yöntemi de anahtar değerleri ile belirtmek
topla(y=6, x=5)   # Anahtar değeri belirttiğiniz için parametre sıralaması önemsiz.

# Sınırsız sayıda argüman da alabilirsiniz
def argumanlar(*argumanlar):
    return argumanlar

argumanlar(1, 2, 3)   # => (1, 2, 3)

# Parametrelerin anahtar değerlerini almak isterseniz
def anahtar_par(**anahtarlar):
    return anahtar

# Çalıştırdığımızda
anahtar_par(anah1="deg1", anah2="deg2")   # => {"anah1": "deg1", "anah2": "deg2"}


# İsterseniz, bu ikisini birden kullanabilirsiniz
def tum_argumanlar(*argumanlar, **anahtarla):
    print(argumanlar)
    print(anahtarla)
"""
tum_argumanlar(1, 2, a=3, b=4) çıktı:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Fonksiyonu çağırırken de aynısını kullanabilirsiniz
argumanlar = (1, 2, 3, 4)
anahtarla = {"a": 3, "b": 4}
tum_argumanlar(*argumanlar)   # = foo(1, 2, 3, 4)
tum_argumanlar(**anahtarla)   # = foo(a=3, b=4)
tum_argumanlar(*argumanlar, **anahtarla)   # = foo(1, 2, 3, 4, a=3, b=4)


# Fonksiyonlarda kullanacağımız bir değişken oluşturalım                                  
x = 5

def belirleX(sayi):
    # Fonksiyon içerisindeki x ile global tanımladığımız x aynı değil
    x = sayi # => 43
    print (x) # => 43
    
def globalBelirleX(sayi):
    global x
    print (x) # => 5
    x = sayi # global olan x değişkeni artık 6
    print (x) # => 6

belirleX(43)
globalBelirleX(6)


# Sınıf fonksiyonları oluşturma
def toplama_olustur(x):
    def topla(y):
        return x + y
    return topla

ekle_10 = toplama_olustur(10)
ekle_10(3)   # => 13

# Bilinmeyen fonksiyon
(lambda x: x > 2)(3)   # => True

# TODO - Fix for iterables
# Belirli sayıdan yükseğini alma fonksiyonu
map(ekle_10, [1, 2, 3])   # => [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]

# Filtreleme işlemi için liste comprehensions da kullanabiliriz
[ekle_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]   # => [6, 7]

####################################################
## 5. Sınıflar
####################################################


# Sınıf oluşturmak için objeden alt sınıf oluşturacağız.
class Insan(obje):

    # Sınıf değeri. Sınıfın tüm nesneleri tarafından kullanılabilir
    tur = "H. sapiens"

    # Basit başlatıcı, Sınıf çağrıldığında tetiklenecektir.
    # Dikkat edin, iki adet alt çizgi(_) bulunmakta. Bunlar
    # python tarafından tanımlanan isimlerdir. 
    # Kendinize ait bir fonksiyon oluştururken __fonksiyon__ kullanmayınız!
    def __init__(self, isim):
        # Parametreyi sınıfın değerine atayalım
        self.isim = isim

    # Bir metot. Bütün metotlar ilk parametre olarak "self "alır.
    def soyle(self, mesaj):
        return "{isim}: {mesaj}".format(isim=self.name, mesaj=mesaj)

    # Bir sınıf metotu bütün nesnelere paylaştırılır
    # İlk parametre olarak sınıf alırlar
    @classmethod
    def getir_tur(snf):
        return snf.tur

    # Bir statik metot, sınıf ve nesnesiz çağrılır
    @staticmethod
    def grunt():
        return "*grunt*"


# Sınıfı çağıralım
i = Insan(isim="Ahmet")
print(i.soyle("merhaba"))     # çıktı "Ahmet: merhaba"

j = Insan("Ali")
print(j.soyle("selam"))  # çıktı "Ali: selam"

# Sınıf metodumuzu çağıraim
i.getir_tur()   # => "H. sapiens"

# Paylaşılan değeri değiştirelim
Insan.tur = "H. neanderthalensis"
i.getir_tur()   # => "H. neanderthalensis"
j.getir_tur()   # => "H. neanderthalensis"

# Statik metodumuzu çağıralım
Insan.grunt()   # => "*grunt*"


####################################################
## 6. Moduller
####################################################

# Modülleri içe aktarabilirsiniz
import math
print(math.sqrt(16))  # => 4.0

# Modülden belirli bir fonksiyonları alabilirsiniz
from math import ceil, floor
print(ceil(3.7))  # => 4.0
print(floor(3.7))   # => 3.0

# Modüldeki tüm fonksiyonları içe aktarabilirsiniz
# Dikkat: bunu yapmanızı önermem.
from math import *

# Modül isimlerini değiştirebilirsiniz.
# Not: Modül ismini kısaltmanız çok daha iyi olacaktır
import math as m
math.sqrt(16) == m.sqrt(16)   # => True

# Python modulleri aslında birer python dosyalarıdır.
# İsterseniz siz de yazabilir ve içe aktarabilirsiniz Modulün
# ismi ile dosyanın ismi aynı olacaktır.

# Moduldeki fonksiyon ve değerleri öğrenebilirsiniz.
import math
dir(math)


####################################################
## 7. Gelişmiş
####################################################

# Oluşturucular uzun uzun kod yazmamanızı sağlayacak ve yardımcı olacaktır
def kare_sayilar(nesne):
    for i in nesne:
        yield i + i

# Bir oluşturucu(generator) değerleri anında oluşturur.
# Bir seferde tüm değerleri oluşturup göndermek yerine teker teker her oluşumdan
# sonra geri döndürür.  Bu demektir ki, kare_sayilar fonksiyonumuzda 15'ten büyük
# değerler işlenmeyecektir.
# Not: range() da bir oluşturucu(generator)dur. 1-900000000 arası bir liste yapmaya çalıştığınızda
# çok fazla vakit alacaktır.
# Python tarafından belirlenen anahtar kelimelerden kaçınmak için basitçe alt çizgi(_) kullanılabilir. 
range_ = range(1, 900000000)
# kare_sayilar'dan dönen değer 30'a ulaştığında durduralım
for i in kare_sayilar(range_):
    print(i)
    if i >= 30:
        break


# Dekoratörler
# Bu örnekte,
# Eğer lutfen_soyle True ise dönen değer değişecektir.
from functools import wraps


def yalvar(hedef_fonksiyon):
    @wraps(hedef_fonksiyon)
    def metot(*args, **kwargs):
        msj, lutfen_soyle = hedef_fonksiyon(*args, **kwargs)
        if lutfen_soyle:
            return "{} {}".format(msj, "Lütfen! Artık dayanamıyorum :(")
        return msj

    return metot


@yalvar
def soyle(lutfen_soyle=False):
    msj = "Bana soda alır mısın?"
    return msj, lutfen_soyle


print(soyle())  # Bana soda alır mısın?
print(soyle(lutfen_soyle=True))  # Ban soda alır mısın? Lutfen! Artık dayanamıyorum :(
```

## Daha Fazlasına Hazır Mısınız?

### Ücretsiz Online

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)
* [Python Course](http://www.python-course.eu/index.php)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)
* [A curated list of awesome Python frameworks, libraries and software](https://github.com/vinta/awesome-python)
* [30 Python Language Features and Tricks You May Not Know About](http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html)
* [Official Style Guide for Python](https://www.python.org/dev/peps/pep-0008/)
* [Python 3 Computer Science Circles](http://cscircles.cemc.uwaterloo.ca/)

### Kitaplar

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: swift
contributors:
  - ["Özgür Şahin", "https://github.com/ozgurshn/"]
filename: learnswift-tr.swift
lang: tr-tr
---

Swift iOS ve OSX platformlarında geliştirme yapmak için Apple tarafından oluşturulan yeni bir programlama dilidir.  Objective - C ile beraber kullanılabilecek ve de hatalı kodlara karşı daha esnek bir yapı sunacak bir şekilde tasarlanmıştır. Swift 2014 yılında Apple'ın geliştirici konferansı WWDC de tanıtıldı. Xcode 6+'a dahil edilen LLVM derleyici ile geliştirildi.
 

Apple'ın resmi [Swift Programlama Dili](https://itunes.apple.com/us/book/swift-programming-language/id881256329) kitabı iBooks'ta yerini aldı.

Ayrıca Swift ile gelen tüm özellikleri görmek için Apple'ın [başlangıç kılavuzu](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/RoadMapiOS/index.html)na bakmanızda yarar var.



```swift
// modülü import etme
import UIKit

//
// MARK: Temeller
//


//XCode işaretlemelerle kodunuzu bölümlere ayırmanızı ve sağ üstteki metot
//listesinde gruplama yapmanıza olanak sağlıyor
// MARK: Bölüm işareti
// TODO: Daha sonra yapılacak
// FIXME: Bu kodu düzelt

 
//Swift 2 de, println ve print metotları print komutunda birleştirildi.
//Print otomatik olarak yeni satır ekliyor.
print("Merhaba dünya") // println print olarak kullanılıyor.
print("Merhaba dünya", appendNewLine: false) // yeni bir satır eklemeden yazar.

// variables (var) değer atandıktan sonra değiştirilebilir.
// constants (let) değer atndıktan sonra değiştirilemez.

var degiskenim = 42
let øπΩ = "deger" // unicode degişken adları
let π = 3.1415926
let convenience = "keyword" // bağlamsal değişken adı
let isim = "ahmet"; let soyad = "un" // farklı ifadeler noktalı virgül 
kullanılarak ayrılabilir.
let `class` = "keyword" // rezerve edilmiş keywordler tek tırnak içerisine 
alınarak değişken adı olarak kullanılabilir
let doubleOlduguBelli: Double = 70
let intDegisken = 0007 // 7
let largeIntDegisken = 77_000 // 77000
let etiket = "birseyler " + String(degiskenim) // Cast etme
let piYazi = "Pi = \(π), Pi 2 = \(π * 2)" // String içerisine değiken yazdırma


// Builde özel değişkenler
// -D build ayarını kullanır.
#if false
    print("yazılmadı")
    let buildDegiskeni= 3
#else
    let buildDegiskeni = 7
#endif
print("Build degiskeni: \(buildDegiskeni)") // Build degeri: 7

/*  
    Optionals Swift dilinde bazı değerleri veya yokluğu (None) bir değişkende 
    	tutmanıza olanak sağlar.  
    
    Swift'te her bir degişkeninin bir değeri olması gerektiğinden, nil değeri 
    	 bile Optional değer olarak saklanır.

    Optional<T> bir enum'dır.
*/
var baziOptionalString: String? = "optional" // nil olabilir.
// yukarıdakiyle aynı ama ? bir postfix (sona eklenir) operatördür. (kolay 
//okunabilir)
var someOptionalString2: Optional<String> = "optional"  


if baziOptionalString != nil {
    // ben nil değilim
    if baziOptionalString!.hasPrefix("opt") {
        print("ön eki var")
    }
    
    let bos = baziOptionalString?.isEmpty
}
baziOptionalString = nil

// belirgin olarak acilan(unwrap) opsiyonel (optional) değer
var acilanString: String! = "Değer bekleniliyor"
//yukarıdakiyle aynı ama ! bir postfix operatördür (kolay okunabilir)
var acilanString2: ImplicitlyUnwrappedOptional<String> = "Değer bekleniliyor."

if let baziOpsiyonelSabitString = baziOptionalString {
    // eğer bir değeri varsa, nil değilse
    if ! baziOpsiyonelSabitString("tamam") {
        // ön eke sahip değil
    }
}

// Swift değişkenlerde herhangi bir tip saklanabilir.
// AnyObject == id
// Objective-C deki `id` den farklı olarak, AnyObject tüm değişkenlerle
//çalışabilir
(Class, Int, struct, etc)
var herhangiBirObject: AnyObject = 7
herhangiBirObject = "Değer string olarak değişti, iyi bir yöntem değil ama mümkün"

/*
    Yorumlar buraya
    
    /*
        İç içe yorum yazılması da mümkün
    */
*/

//
// MARK: Koleksiyonlar
//

/*
    	Array ve Dictionary tipleri aslında structdırlar. Bu yüzden `let` ve `var` 
    	ayrıca bu tipleri tanımlarken değişebilir(var) veya değişemez(let) 
    	olduğunu 	belirtir.
    	
*/

// Diziler
var liste = ["balik", "su", "limon"]
liste[1] = "şişe su"
let bosDizi = [String]() // let == değiştirilemez
let bosDizi2 = Array<String>() // yukarıdakiyle aynı
var bosDegistirilebilirDizi = [String]() // var == değişebilir


// Dictionary
var meslekler = [
    "Kamil": "Kaptan",
    "Ayse": "Analist"
]
meslekler["Cansu"] = "Halkla İlişkiler"
let bosDictionary = [String: Float]() // let == değiştirilemez
let bosDictionary2 = Dictionary<String, Float>() // yukarıdakiyle aynı
var bosDegistirilebirDictionary = [String: Float]() // var == değiştirilebilir


//
// MARK: Kontroller
//

// for döngüsü (dizi)
let dizi = [1, 1, 2, 3, 5]
for deger in dizi {
    if deger == 1 {
        print("Bir!")
    } else {
        print("Bir degil!")
    }
}

// for döngüsü (dictionary)
var dict = ["one": 1, "two": 2]
for (key, value) in dict {
    print("\(key): \(value)")
}

// for döngüsü (aralık)
for i in -1...liste.count {
    print(i)
}
liste[1...2] = ["et", "yogurt"]
// ..<  kullanarak son elemanı çıkartabilirsiniz

// while döngüsü
var i = 1
while i < 1000 {
    i *= 2
}

// do-while döngüsü
do {
    print("merhaba")
} while 1 == 2

// Switch
// Çok güçlü, `if` ifadesenin daha kolay okunabilir hali olarak düşünün
// String, object örnekleri, ve primitif tipleri (Int, Double, vs) destekler.
let sebze = "kırmızı biber"
switch sebze {
case "sogan":
    let sebzeYorumu = "Biraz da domates ekle"
case "domates", "salata":
    let sebzeYorumu = "İyi bir sandviç olur"
case let lokalScopeDegeri where lokalScopeDegeri.hasSuffix("biber"):
    let sebzeYorumu = "Acı bir \(lokalScopeDegeri)?"
default: // zorunludur (tüm olasılıkları yakalamak icin)
    let sebzeYorumu = "Corbadaki herseyin tadı güzel"
}


//
// MARK: Fonksiyonlar
//

// Fonksiyonlar first-class tiplerdir, yani başka fonksiyon içine konabilir
// ve parametre olarak geçirilebilirler.

// Swift dökümanlarıylaa birlikte Fonksiyonlar (format as reStructedText)

/**
    selamlama işlemi

    :param: isim e isim
    :param: gun  e A gun
    :returns: isim ve gunu iceren bir String
*/
func selam(isim: String, gun: String) -> String {
    return "Merhaba \(isim), bugün \(gun)."
}
selam("Can", "Salı")

// fonksiyon parametre davranışı hariç yukarıdakine benzer
func selam2(#gerekliIsim: String, disParametreIsmi lokalParamtreIsmi: String) -> String {
    return "Merhaba \(gerekliIsim), bugün \(lokalParamtreIsmi)"
}
selam2(gerekliIsim:"Can", disParametreIsmi: "Salı")

// Bir tuple ile birden fazla deger dönen fonksiyon
func fiyatlariGetir() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let fiyatTuple = fiyatlariGetir()
let fiyat = fiyatTuple.2 // 3.79
// _ (alt çizgi) kullanımı Tuple degerlerini veya diğer değerleri görmezden
//gelir
let (_, fiyat1, _) = fiyatTuple // fiyat1 == 3.69
print(fiyat1 == fiyatTuple.1) // true
print("Benzin fiyatı: \(fiyat)")

// Çeşitli Argümanlar
func ayarla(sayilar: Int...) {
    // bu bir dizidir
    let sayi = sayilar[0]
    let argumanSAyisi = sayilar.count
}

// fonksiyonu parametre olarak geçirme veya döndürme
func arttirmaIslemi() -> (Int -> Int) {
    func birEkle(sayi: Int) -> Int {
        return 1 + sayi
    }
    return birEkle
}
var arttir = arttirmaIslemi()
arttir(7)

// referans geçirme
func yerDegistir(inout a: Int, inout b: Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
yerDegistir(&someIntA, &someIntB)
print(someIntB) // 7


//
// MARK: Closurelar
//
var sayilar = [1, 2, 6]

// Fonksiyonlar özelleştirilmiş closurelardır. ({})

// Closure örneği.
// `->` parametrelerle dönüş tipini birbirinden ayırır
// `in` closure başlığını closure bodysinden ayırır.
sayilar.map({
    (sayi: Int) -> Int in
    let sonuc = 3 * sayi
    return sonuc
})

// eger tip biliniyorsa, yukarıdaki gibi, şöyle yapabiliriz
sayilar = sayilar.map({ sayi in 3 * sayi })
// Hatta bunu
//sayilar = sayilar.map({ $0 * 3 })

print(sayilar) // [3, 6, 18]

// Trailing closure
sayilar = sorted(sayilar) { $0 > $1 }

print(sayilar) // [18, 6, 3]

// Super kısa hali ise, < operatörü tipleri çıkartabildiği için

sayilar = sorted(sayilar, < )

print(sayilar) // [3, 6, 18]

//
// MARK: Yapılar
//

// Structurelar ve sınıflar birçok aynı özelliğe sahiptir.
struct IsimTablosu {
    let isimler = [String]()
    
    // Özelleştirilmiş dizi erişimi
    subscript(index: Int) -> String {
        return isimler[index]
    }
}

// Structurelar otomatik oluşturulmuş kurucu metoda sahiptir.
let isimTablosu = IsimTablosu(isimler: ["Ben", "Onlar"])
let isim = isimTablosu[1]
print("İsim \(name)") // İsim Onlar

//
// MARK: Sınıflar
//

// Sınıflar, structurelar ve üyeleri 3 seviye erişime sahiptir.
// Bunlar: internal (default), public, private

public class Sekil {
    public func alaniGetir() -> Int {
        return 0;
    }
}

// Sınıfın tüm değişkenleri ve metotları publictir.
// Eğer sadece veriyi yapılandırılmış bir objede
// saklamak istiyorsanız, `struct` kullanmalısınız.

internal class Rect: Sekil {
    var yanUzunluk: Int = 1
    
    // Özelleştirilmiş getter ve setter propertyleri
    private var cevre: Int {
        get {
            return 4 * yanUzunluk
        }
        set {
            // `newValue ` setterlarda yeni değere erişimi sağlar
            yanUzunluk = newValue / 4
        }
    }
    
    // Bir değişkene geç atama(lazy load) yapmak
    // altSekil getter cağrılana dek nil (oluşturulmamış) olarak kalır
    lazy var altSekil = Rect(yanUzunluk: 4)
    
    // Eğer özelleştirilmiş getter ve setter a ihtiyacınız yoksa,
    // ama bir değişkene get veya set yapıldıktan sonra bir işlem yapmak 
    // istiyorsanız, `willSet` ve `didSet` metotlarını kullanabilirsiniz
    var identifier: String = "defaultID" {
        // `willSet` argümanı yeni değer için değişkenin adı olacaktır.
        willSet(someIdentifier) {
            print(someIdentifier)
        }
    }
    
    init(yanUzunluk: Int) {
        self. yanUzunluk = yanUzunluk
        // super.init i her zaman özelleştirilmiş değerleri oluşturduktan sonra
         çağırın
        super.init()
    }
    
    func kisalt() {
        if yanUzunluk > 0 {
            --yanUzunluk
        }
    }
    
    override func alaniGetir() -> Int {
        return yanUzunluk * yanUzunluk
    }
}

// Basit `Kare` sınıfI `Rect` sınıfını extend ediyor.
class Kare: Rect {
    convenience init() {
        self.init(yanUzunluk: 5)
    }
}

var benimKarem = Kare()
print(m benimKarem.alaniGetir()) // 25
benimKarem.kisalt()
print(benimKarem.yanUzunluk) // 4

// sınıf örneğini cast etme
let birSekil = benimKarem as Sekil

// örnekleri karşılaştır, objeleri karşılaştıran == (equal to) ile aynı değil  
if benimKarem === benimKarem {
    print("Evet, bu benimKarem")
}

// Opsiyonel init
class Daire: Sekil {
    var yaricap: Int
    override func alaniGetir() -> Int {
        return 3 * yaricap * yaricap
    }
    
    // Eğer init opsiyonelse (nil dönebilir) `init` den sonra soru işareti
    // son eki ekle.
    init?(yaricap: Int) {
        self.yaricap = yaricap
        super.init()
        
        if yaricap <= 0 {
            return nil
        }
    }
}

var benimDairem = Daire(radius: 1)
print(benimDairem?.alaniGetir())    // Optional(3)
print(benimDairem!. alaniGetir())    // 3
var benimBosDairem = Daire(yaricap: -1)
print(benimBosDairem?. alaniGetir())    // "nil"
if let daire = benimBosDairem {
    // benimBosDairem nil olduğu için çalışmayacak
    print("circle is not nil")
}


//
// MARK: Enumlar
//

// Enumlar opsiyonel olarak özel bir tip veya kendi tiplerinde olabilirler.
// Sınıflar gibi metotlar içerebilirler.

enum Kart {
    case Kupa, Maca, Sinek, Karo
    func getIcon() -> String {
        switch self {
        case .Maca: return "♤"
        case .Kupa: return "♡"
        case .Karo: return "♢"
        case .Sinek: return "♧"
        }
    }
}

// Enum değerleri kısayol syntaxa izin verir. Eğer değişken tipi açık olarak belirtildiyse enum tipini yazmaya gerek kalmaz.
var kartTipi: Kart = .Kupa

// Integer olmayan enumlar direk değer (rawValue) atama gerektirir.
enum KitapAdi: String {
    case John = "John"
    case Luke = "Luke"
}
print("Name: \(KitapAdi.John.rawValue)")

// Değerlerle ilişkilendirilmiş Enum
enum Mobilya {
    // Int ile ilişkilendirilmiş
    case Masa(yukseklik: Int)
    // String ve Int ile ilişkilendirilmiş
    case Sandalye(String, Int)
    
    func aciklama() -> String {
        switch self {
        case .Masa(let yukseklik):
            return "Masa boyu \(yukseklik) cm"
        case .Sandalye(let marka, let yukseklik):
            return "\(brand) marka sandalyenin boyu \(yukseklik) cm"
        }
    }
}

var masa: Mobilya = .Masa(yukseklik: 80)
print(masa.aciklama())     // "Masa boyu 80 cm"
var sandalye = Mobilya.Sandalye("Foo", 40)
print(sandalye.aciklama())    // "Foo marka sandalyenin boyu 40 cm"


//
// MARK: Protokoller
//

// `protocol` onu kullanan tiplerin bazı özel değişkenleri, metotları,
// tip metotlarını,opertörleri ve alt indisleri (subscripts) içermesini
// zorunlu hale getirebilir.

protocol SekilUretici {
    var aktif: Bool { get set }
    func sekilOlustur() -> Sekil
}

// @objc ile tanımlanan protokoller, uygunluğu kontrol edebilmenizi sağlayacak
// şekilde opsiyonel fonksiyonlara izin verir
@objc protocol SekliDondur {
    optional func sekillendirilmis()
    optional func sekillendirilebilir() -> Bool
}

class BenimSeklim: Rect {
    var delegate: SekliDondur?
    
    func buyut() {
        yanUzlunluk += 2

	// Bir çalışma zamanı hatası("optional chaining") fırlatmak yerine nil 
	//değeri görmezden gelerek nil dönmek için opsiyonel değişken, metot veya
	// altindisten sonra soru işareti koyabilirsiniz.
        if let izinVeriyormu = self.delegate?.sekillendirilebilir?() {
            // önce delegate i sonra metodu test edin
            self.delegate?.sekillendirilmis?()
        }
    }
}


//
// MARK: Diğerleri
//

// `extension`lar: Var olan tiplere ekstra özellikler ekleyin

// Kare artık `Printable` protokolüne uyuyor.
extension Kare: Printable {
    var description: String {
        return "Alan: \(alaniGetir()) - ID: \(self.identifier)"
    }
}

print("Kare: \(benimKarem)")

// Dahili tipleri de yeni özellikler ekleyebilirsiniz
extension Int {
    var customProperty: String {
        return "Bu sayı \(self)"
    }
    
    func carp(num: Int) -> Int {
        return num * self
    }
}

print(7.customProperty) // "Bu sayı 7"
print(14.carp(3)) // 42

// Genericler: Java ve C#'a benzer şekilde. `where` anahtar kelimesini
// kullanarak genericlerin özelliklerini belirleyin

func indexiBul<T: Equatable>(dizi: [T], bulunacakDeger: T) -> Int? {
    for (index, deger) in enumerate(dizi) {
        if deger == bulunacakDeger {
            return index
        }
    }
    return nil
}
let bulunanIndex = indexiBul([1, 2, 3, 4], 3)
print(bulunanIndex == 2) // true

// Operatorler:
// Özel operatorler şu karakterlerle başlayabilir:
//      / = - + * % < > ! & | ^ . ~
// veya
// Unicode math, symbol, arrow, dingbat, ve line/box karakterleri.
prefix operator !!! {}

// Yan uzunluğu 3 katına çıkartan prefix operatörü
prefix func !!! (inout sekil: Kare) -> Kare {
    sekil.YanUzunluk *= 3
    return sekil
}

// güncel deger
print(benimKarem.YanUzunluk)  // 4

// yan uzunluğu !!! operatorü kullanarak 3 katına çıkar
!!!benimKarem
print(benimKarem.YanUzunluk) // 12
```
---
language: TypeScript
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
translators:
    - ["Mustafa Zengin", "http://zengin.github.com"]
filename: learntypescript-tr.ts
lang: tr-tr
---

TypeScript, JavaScript'le yazılmış büyük ölçekli uygulamaların geliştirilmesini kolaylaştırmayı hedefleyen bir dildir.
TypeScript, JavaScript'e sınıflar, modüller, arayüzler, jenerik tipler ve (isteğe bağlı) static tipleme gibi genel konseptler ekler.
JavaScript, TypeScript'in bir alt kümesidir: Bütün JavaScript kodları geçerli birer TypeScript kodudur ve sorunsuz herhangi bir projeye eklenebilirler. TypeScript derleyici JavaScript kodu üretir.

Bu makale sadece TypeScript'e ait ekstra söz dizimini konu alır, JavaScript için bkz: [JavaScript] (../javascript/).

TypeScript derleyiciyi test etmek için [Playground] (http://www.typescriptlang.org/Playground)'a gidin. Orada otomatik tamamlama ile kod yazabilecek ve üretilen JavaScript'i görebileceksiniz.

```js
// TypeScript'te üç ana tip vardır.
var bittiMi: boolean = false;
var satırlar: number = 42;
var isim: string = "Anders";

// Tipin bilinmediği zamanlar için "Any" tipi
var bilinmiyor: any = 4;
bilinmiyor = "belki de bir string'dir";
bilinmiyor = false; // tamam, boolean olsun

// Kolleksiyon olarak, tipli ve jenerik diziler
var liste: number[] = [1, 2, 3];
// Alternatif olarak jenerik Array tipi
var liste: Array<number> = [1, 2, 3];

// 'enum' tipleri:
enum Renk {Kırmızı, Yeşil, Mavi};
var r: Renk = Renk.Yeşil;

// Son olarak, "void" hiç bir şey döndürmeyen fonksiyonlarda kullanılan tiptir.
function çokFeciBirUyarı(): void {
  alert("Ben biraz sinir bozucuyum!");
}

// Fonksiyonlar birinci sınıf vatandaşlardır ve "kalın ok" lambda söz dizimi "=>"
// ve tip çıkarımı kullanırlar.
// Aşağıda listelenenler birbirinin aynısı ve derleyici aynı fonksiyon yapısını
// çıkaracak ve aynı JavaScript kodunu üretecektir
var f1 = function(i: number): number { return i * i; }
// Döndürülen tip tip çıkarımıyla belirlendi
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
// Döndürülen tip tip çıkarımıyla belirlendi
var f4 = (i: number) => { return i * i; }
// Döndürülen tip tip çıkarımıyla belirlendi
// Tek satırlık yazımda "return" anahtar kelimesine ihtiyaç yok.
var f5 = (i: number) =>  i * i;

// Arayüzler yapısaldır, listelenen özelliklere sahip her şey arayüzle uyumludur.
interface Kişi {
  isim: string;
  // İsteğe bağlı özellikler "?" ile işaretlenir
  yaş?: number;
  // Ve fonksiyonlar...
  hareketEt(): void;
}

// "Kişi" arayüzünü kullanan bir nesne
// isim ve hareketEt özelliklerine sahip olduğu için Kişi olarak kullanılabilir.
var p: Kişi = { isim: "Anders", hareketEt: () => {} };
// İsteğe bağlı özelliğe sahip bir Kişi
var geçerliKişi: Kişi = { isim: "Anders", yaş: 42, hareketEt: () => {} };
// Geçersiz bir kişi, çünkü yaş bir sayı (number) tipi değil
var geçersizKişi: Kişi = { isim: "Anders", yaş: true };

// Arayüzler bir fonksiyon tipi de ifade edebilirler
interface aramaFonksiyonu {
  (kaynak: string, altString: string): boolean;
}

// Parametrelerin sadece tipleri önemli, isimleri önemli değil
var benimAramam: aramaFonksiyonu;
benimAramam = function(kynk: string, alt: string) {
  return kynk.search(alt) != -1;
}

// Sınıflar - üyeler (members) varsayılan olarak public'tir.
class Nokta {
  // Özellikler
  x: number;

  // Yapıcı (constructor) - bu bağlamdaki public/private anahtar kelimeleri
  // özellikler için gerekli demirbaş kodu oluşturur ve ilk değerlerin
  // atanmasını sağlar.
  // Bu örnekte, "y" de "x" gibi tanımlanacak is, but with less code
  // Default values are also supported

  constructor(x: number, public y: number = 0) {
    this.x = x;
  }

  // Fonksiyonlar
  mesafe() { return Math.sqrt(this.x * this.x + this.y * this.y); }

  // Statik üyeler
  static orijin = new Nokta(0, 0);
}

var p1 = new Nokta(10 ,20);
var p2 = new Nokta(25); //y = 0

// Kalıtım
class Nokta3Boyutlu extends Nokta {
  constructor(x: number, y: number, public z: number = 0) {
    super(x, y); // süper sınıfın yapıcısını çağırmak zorunlu
  }

  // yeniden tanımlama
  mesafe() {
    var d = super.mesafe();
    return Math.sqrt(d * d + this.z * this.z);
  }
}

// Modüller, "." alt modülleri ayırmak için kullanılabilir
module Geometri {
  export class Kare {
    constructor(public kenarUzunluğu: number = 0) {
    }
    alan() {
      return Math.pow(this.kenarUzunluğu, 2);
    }
  }
}

var s1 = new Geometri.Kare(5);

// Modüle atıfta bulunmak için yerel takma ad
import G = Geometri;

var s2 = new G.Kare(10);

// Jenerik Tipler
// Sınıflar
class Tuple<T1, T2> {
  constructor(public item1: T1, public item2: T2) {
  }
}

// Arayüzler
interface Çift<T> {
  item1: T;
  item2: T;
}

// Ve fonksiyonlar
var çifttenTupleÜret = function<T>(p: Çift<T>) {
  return new Tuple(p.item1, p.item2);
};

var tuple = çifttenTupleÜret({ item1:"merhaba", item2:"dünya"});

// Tanım dosyasına atıfta bulunma:
/// <reference path="jquery.d.ts" />

// Şablon Stringleri (ters apostrof kullanan stringler)
// Şablon Stringlerinin kullanımı
var isim = 'Anders';
var selamlama = `Merhaba ${isim}, nasılsın?`
// Şablon Stringleri ile çok satırlı stringler
var çokSatırlıString = `Bu çok satırlı
bir string örneği`;

```

## Daha fazlası
 * [TypeScript Resmi Sitesi] (http://www.typescriptlang.org/)
 * [TypeScript dil spesifikasyonu (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg - Channel 9'da TypeScript'e Giriş] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [GitHub'ta Açık Kaynak Kodu] (https://github.com/Microsoft/TypeScript)
 * [Definitely Typed - tip tanımları için kaynak] (http://definitelytyped.org/)
---
language: TypeScript
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
filename: learntypescript.ts
---

TypeScript is a language that aims at easing development of large scale applications written in JavaScript.
TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript.
It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. The TypeScript compiler emits JavaScript.

This article will focus only on TypeScript extra syntax, as opposed to [JavaScript](javascript.html.markdown).

To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript.

```ts
// There are 3 basic types in TypeScript
let isDone: boolean = false;
let lines: number = 42;
let name: string = "Anders";

// But you can omit the type annotation if the variables are derived from explicit literals
let isDone = false;
let lines = 42;
let name = "Anders";

// When it's impossible to know, there is the "Any" type
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

// Use const keyword for constant variables
const numLivesForCat = 9;
numLivesForCat = 1; // Error

// For collections, there are typed arrays and generic arrays
let list: number[] = [1, 2, 3];
// Alternatively, using the generic array type
let list: Array<number> = [1, 2, 3];

// For enumerations:
enum Color { Red, Green, Blue };
let c: Color = Color.Green;

// Lastly, "void" is used in the special case of a function returning nothing
function bigHorribleAlert(): void {
  alert("I'm a little annoying box!");
}

// Functions are first class citizens, support the lambda "fat arrow" syntax and
// use type inference

// The following are equivalent, the same signature will be inferred by the
// compiler, and same JavaScript will be emitted
let f1 = function (i: number): number { return i * i; }
// Return type inferred
let f2 = function (i: number) { return i * i; }
// "Fat arrow" syntax
let f3 = (i: number): number => { return i * i; }
// "Fat arrow" syntax with return type inferred
let f4 = (i: number) => { return i * i; }
// "Fat arrow" syntax with return type inferred, braceless means no return
// keyword needed
let f5 = (i: number) => i * i;

// Interfaces are structural, anything that has the properties is compliant with
// the interface
interface Person {
  name: string;
  // Optional properties, marked with a "?"
  age?: number;
  // And of course functions
  move(): void;
}

// Object that implements the "Person" interface
// Can be treated as a Person since it has the name and move properties
let p: Person = { name: "Bobby", move: () => { } };
// Objects that have the optional property:
let validPerson: Person = { name: "Bobby", age: 42, move: () => { } };
// Is not a person because age is not a number
let invalidPerson: Person = { name: "Bobby", age: true };

// Interfaces can also describe a function type
interface SearchFunc {
  (source: string, subString: string): boolean;
}
// Only the parameters' types are important, names are not important.
let mySearch: SearchFunc;
mySearch = function (src: string, sub: string) {
  return src.search(sub) != -1;
}

// Classes - members are public by default
class Point {
  // Properties
  x: number;

  // Constructor - the public/private keywords in this context will generate
  // the boiler plate code for the property and the initialization in the
  // constructor.
  // In this example, "y" will be defined just like "x" is, but with less code
  // Default values are also supported

  constructor(x: number, public y: number = 0) {
    this.x = x;
  }

  // Functions
  dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

  // Static members
  static origin = new Point(0, 0);
}

let p1 = new Point(10, 20);
let p2 = new Point(25); //y will be 0

// Inheritance
class Point3D extends Point {
  constructor(x: number, y: number, public z: number = 0) {
    super(x, y); // Explicit call to the super class constructor is mandatory
  }

  // Overwrite
  dist() {
    let d = super.dist();
    return Math.sqrt(d * d + this.z * this.z);
  }
}

// Modules, "." can be used as separator for sub modules
module Geometry {
  export class Square {
    constructor(public sideLength: number = 0) {
    }
    area() {
      return Math.pow(this.sideLength, 2);
    }
  }
}

let s1 = new Geometry.Square(5);

// Local alias for referencing a module
import G = Geometry;

let s2 = new G.Square(10);

// Generics
// Classes
class Tuple<T1, T2> {
  constructor(public item1: T1, public item2: T2) {
  }
}

// Interfaces
interface Pair<T> {
  item1: T;
  item2: T;
}

// And functions
let pairToTuple = function <T>(p: Pair<T>) {
  return new Tuple(p.item1, p.item2);
};

let tuple = pairToTuple({ item1: "hello", item2: "world" });

// Including references to a definition file:
/// <reference path="jquery.d.ts" />

// Template Strings (strings that use backticks)
// String Interpolation with Template Strings
let name = 'Tyrone';
let greeting = `Hi ${name}, how are you?`
// Multiline Strings with Template Strings
let multiline = `This is an example
of a multiline string`;

```

## Further Reading
 * [TypeScript Official website] (http://www.typescriptlang.org/)
 * [TypeScript language specifications] (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md)
 * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [Source Code on GitHub] (https://github.com/Microsoft/TypeScript)
 * [Definitely Typed - repository for type definitions] (http://definitelytyped.org/)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
translators:
    - ["Ehreshi Ivan", "https://github.com/IvanEh"]
    - ["Serhii Maksymchuk", "https://github.com/Serg-Maximchuk"]
lang: uk-ua
---

Bash - командна оболонка unix (unix shell), що також розповсюджувалась як оболонка для
операційної системи GNU і зараз використовується як командна оболонка за замовчуванням
для Linux i Max OS X.
Майже всі приклади, що наведені нижче можуть бути частиною shell-скриптів або
виконані в оболонці

[Більш детально тут.](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# Перший рядок скрипта - це shebang, який вказує системі, як потрібно виконувати
# скрипт. Як ви вже зрозуміли, коментарі починаються з #. Shebang - також коментар

# Простий приклад hello world:
echo Hello world!

# Окремі команди починаються з нового рядка або розділяються крапкою з комкою:
echo 'Перший рядок'; echo 'Другий рядок'

# Оголошення змінної
VARIABLE="Просто рядок"

# Але не так!
VARIABLE = "Просто рядок"
# Bash вирішить, що VARIABLE - це команда, яку він може виконати,
# і видасть помилку, тому що не зможе знайти її

# І так також не можна писати:
VARIABLE= 'Просто рядок'
# Bash сприйме рядок 'Просто рядок' як команду. Але такої команди не має, тому
# видасть помилку. 
# (тут 'VARIABLE=' інтерпретується як присвоєння тільки в контексті
# виконання команди 'Просто рядок')

# Використання змінних:
echo $VARIABLE
echo "$VARIABLE"
echo '$VARIABLE'
# Коли ви використовуєте змінну - присвоюєте значення, експортуєте і т.д. -
# пишіть її імя без $. А для отримання значення змінної використовуйте $.
# Одинарні лапки ' не розкривають значення змінних

# Підстановка рядків в змінні
echo ${VARIABLE/Просто/A}
# Цей вираз замінить перше входження підрядка "Просто" на "А"

# Отримання підрядка із рядка
LENGTH=7
echo ${VARIABLE:0:LENGTH}
# Цей вираз поверне тільки перші 7 символів змінної VARIABLE

# Значення за замовчуванням
echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"}
# Це спрацює при відсутності значення (FOO=) і при пустому рядку (FOO="")
# Нуль (FOO=0) поверне 0.
# Зауважте, що у всіх випадках значення самої змінної FOO не зміниться

# Вбудовані змінні:
# В bash є корисні вбудовані змінні, наприклад
echo "Значення, яке було повернуте в останній раз: $?"
echo "PID скрипта: $$"
echo "Кількість аргументів: $#"
echo "Аргументи скрипта: $@"
echo "Аргументи скрипта, розподілені по різним змінним: $1 $2..."

# Зчитування змінних з пристроїв  введення
echo "Як вас звати?"
read NAME # Зверніть увагу, що вам не потрібно оголошувати нову змінну
echo Привіт, $NAME!

# В bash є звичайна умовна конструкція if:
# наберіть 'man test', щоб переглянути детальну інформацію про формати умов
if [ $NAME -ne $USER ]
then
    echo "Ім’я користувача не збігається з введеним"
else
    echo "Ім’я збігаєтьяс з іменем користувача"
fi

# Зауважте! якщо $Name пуста, bash інтерпретує код вище як:
if [ -ne $USER ]
# що є неправильним синтаксисом
# тому безпечний спосіб використання потенційно пустих змінних має вигляд:
if [ "$Name" -ne $USER ] ...
# коли $Name пуста, інтерпретується наступним чином:
if [ "" -ne $USER ] ...
# що працює як і очікувалося

# Умовне виконання (conditional execution)
echo "Виконується завжди" || echo "Виконається, якщо перша команда завершиться з помилкою"
echo "Виконується завжди" && echo "Виконається, якщо перша команда завершиться успішно"

# Щоб використати && і || у конструкції if, потрібно декілька пар дужок:
if [ $NAME == "Steve" ] && [ $AGE -eq 15 ]
then
    echo "Виконається, якщо $NAME="Steve" i AGE=15."
fi

if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ]
then
    echo "Виконається, якщо NAME="Steve" або NAME="Zach"."
fi

# Вирази позначаються наступним форматом:
echo $(( 10 + 5 ))

# На відміну від інших мов програмування, Bash - це командна оболонка, а
# отже, працює в контексті поточної директорії
ls

# Ця команда може використовуватися з опціями
ls -l # Показати кожен файл і директорію на окремому рядку

# Результат попередньої команди можна перенаправити на вхід наступної.
# Команда grep фільтрує вхід по шаблону.
# Таким чином ми можемо переглянути тільки *.txt файли в поточній директорії:
ls -l | grep "\.txt"

# Ви можете перенаправити вхід і вихід команди (stdin, stdout, stderr).
# Наступна команда означає: читати із stdin, поки не зустрінеться ^EOF$, і
# перезаписати hello.py наступними рядками (до рядка "EOF"):
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# Запуск hello.py з різними варіантами перенаправлення stdin, 
# stdout, stderr (стандартні потоки введення, виведення і помилок):
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# Потік помилок перезапише файл, якщо цей файл існує
# тому, якщо ви хочете дописувати до файлу, використовуйте ">>":
python hello.py >> "output.out" 2>> "error.err"

# Перезаписати output.txt, дописати error.err і порахувати кількість рядків:
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# Запустити команду і вивести її файловий дескриптор (див.: man fd; наприклад /dev/fd/123)
echo <(echo "#helloworld")

# Перезаписати output.txt рядком "#helloworld":
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# Очистити тимчасові файли з детальним виводом (додайте '-i'
# для інтерактивного режиму)
rm -v output.out error.err output-and-error.log

# Команди можуть бути підставлені в інші команди використовуючи $():
# наступна команда виводить кількість файлів і директорій в поточній директорії
echo "Тут $(ls | wc -l) елементів."

# Те саме можна зробити використовуючи зворотні лапки
# Але вони не можуть бути вкладеними, тому перший варіант бажаніший
echo "Тут `ls | wc -l` елементів."

# В Bash є структура case, яка схожа на switch в Java и C++:
case "$VARIABLE" in 
    # перерахуйте шаблони, які будуть використовуватися в якості умов
    0) echo "Тут нуль.";;
    1) echo "Тут один.";;
    *) echo "Не пусте значення.";;
esac

# Цикл for перебирає елементи передані в аргумент:
# Значення $VARIABLE буде надруковано тричі.
for VARIABLE in {1..3}
do
    echo "$VARIABLE"
done

# Aбо можна використати звичний синтаксис for:
for ((a=1; a <= 3; a++))
do
    echo $a
done

# Цикл for можно використати, щоб виконувати дії над файлами.
# Цей код запустить команду 'cat' для файлів file1 и file2
for VARIABLE in file1 file2
do
    cat "$VARIABLE"
done

# ... або дії над виводом команд
# Запустимо cat для виведення із ls.
for OUTPUT in $(ls)
do
    cat "$OUTPUT"
done

# Цикл while:
while [ true ]
do
    echo "Тіло циклу..."
    break
done

# Ви також можете оголосити функцію
# Оголошення:
function foo ()
{
    echo "Аргументи функції доступні так само, як і аргументи скрипта: $@"
    echo "$1 $2..."
    echo "Це функція"
    return 0
}

# Або просто
bar ()
{
    echo "Інший спосіб оголошення функцій!"
    return 0
}

# Виклик функцій
foo "Мое имя" $NAME

# Є багато корисних команд:
# вивести останні 10 рядків файла file.txt
tail -n 10 file.txt
# вивести перші 10 рядків файла file.txt
head -n 10 file.txt
# відсортувати рядки file.txt
sort file.txt
# відібрати або пропустити рядки, що дублюються (з опцією -d відбирає)
uniq -d file.txt
# вивести тільки першу колонку перед символом ','
cut -d ',' -f 1 file.txt
# замінити кожне 'okay' на 'great' у файлі file.txt (підтримується regex)
sed -i 's/okay/great/g' file.txt
# вивести в stdout всі рядки з file.txt, що задовольняють шаблону regex;
# цей приклад виводить рядки, що починаються на foo і закінчуються на bar:
grep "^foo.*bar$" file.txt
# використайте опцію -c, щоб вивести кількість входжень
grep -c "^foo.*bar$" file.txt
# щоб здійснити пошук по рядку, а не по шаблону regex, використовуйте fgrea (або grep -F)
fgrep "^foo.*bar$" file.txt 

# Читайте вбудовану документацію Bash командою 'help':
help
help help
help for
help return
help source
help .

# Читайте Bash man-документацію
apropos bash
man 1 bash
man bash

# Читайте документацію info (? для допомоги)
apropos info | grep '^info.*('
man info
info info
info 5 info

# Читайте bash info документацію:
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: java
filename: LearnJava-ua.java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Jakukyo Friel", "http://weakish.github.io"]
    - ["Madison Dickson", "http://github.com/mix3d"]
    - ["Simon Morgan", "http://sjm.io/"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
    - ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
    - ["Rachel Stiyer", "https://github.com/rstiyer"]
translators:
    - ["Oleksandr Tatarchuk", "https://github.com/tatarchuk"]
    - ["Andre Polykanine", "https://github.com/Oire"]
lang: uk-ua

---

Java є об’єктно-орієнтованою мовою програмування загального призначення з підтримкою паралельного програмування, яка базується на класах.
[Детальніше читайте тут, англ.](http://docs.oracle.com/javase/tutorial/java/)

```java
// Однорядковий коментар починається з //
/*
Багаторядковий коментар виглядає так.
*/
/**
JavaDoc-коментар виглядає так. Використовується для опису класу та членів класу.
*/

// Імпорт класу ArrayList з пакета java.util
import java.util.ArrayList;
// Імпорт усіх класів з пакета java.security 
import java.security.*;

// Кожний .java файл містить один зовнішній публічний клас, ім’я якого співпадає
// з іменем файлу.
public class LearnJava {

    // Для запуску програма, написана на java, повинна мати точку входу у вигляді методу main.
    public static void main (String[] args) {

        // Використання System.out.println() для виводу на друк рядків.
        System.out.println("Привіт, світе!");
        System.out.println(
            " Ціле число: " + 10 +
            " Число з рухомою комою подвійної точности: " + 3.14 +
            " Булеве значення: " + true);

        // Для друку без переходу на новий рядок використовується System.out.print().
        System.out.print("Привіт, ");
        System.out.print("світе");

        // Використання System.out.printf() для простого форматованого виводу на друк.
        System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159

        ///////////////////////////////////////
        // Змінні
        ///////////////////////////////////////

        /*
        *  Оголошення змінних
        */
        // Для оголошення змінних використовується формат <тип> <змінна>
        int fooInt;
        // Оголошення декількох змінних одного типу <тип> <ім’я1>, <ім’я2>, <ім’я3>
        int fooInt1, fooInt2, fooInt3;

        /*
        *  Ініціалізація змінних
        */

        // Ініціалізація змінної з використанням формату <тип> <ім’я> = <значення>
        int fooInt = 1;
        // Ініціалізація декількох змінних одного типу з одним значенням <тип> <ім’я1>, <ім’я2>, <ім’я3> = <значення>
        int fooInt1, fooInt2, fooInt3;
        fooInt1 = fooInt2 = fooInt3 = 1;

        /*
        *  Типи змінних
        */
        // Байт — 8-бітне ціле число зі знаком
        // (-128 <= byte <= 127)
        byte fooByte = 100;

        // Short — 16-бітне ціле число зі знаком
        // (-32 768 <= short <= 32 767)
        short fooShort = 10000;

        // Integer — 32-бітне ціле число зі знаком
        // (-2 147 483 648 <= int <= 2 147 483 647)
        int fooInt = 1;

        // Long — 64-бітне ціле число зі знаком
        // (-9 223 372 036 854 775 808 <= long <= 9 223 372 036 854 775 807)
        long fooLong = 100000L;
        // L використовується для позначення того, що число має тип Long;
        // інакше число буде трактуватись як integer.

        // Примітка: Java не має беззнакових типів.

        // Float — 32-бітне число з рухомою комою одиничної точності за стандартом IEEE 754 
        // 2^-149 <= float <= (2-2^-23) * 2^127
        float fooFloat = 234.5f;
        // f або F використовується для позначення того, що змінна має тип float;
        // інакше трактується як double.

        // Double — 64-бітне число з рухомою комою подвійної точності за стандартом IEEE 754 
        // 2^-1074 <= x <= (2-2^-52) * 2^1023
        double fooDouble = 123.4;

        // Boolean — true & false (істина чи хиба)
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // Char — 16-бітний символ Unicode
        char fooChar = 'A';

        // final - посилання на такі змінні не можуть бути присвоєні іншим об’єктам,
        final int HOURS_I_WORK_PER_WEEK = 9001;
        // але вони можуть мати відкладену ініціалізацію.
        final double E;
        E = 2.71828;


        // BigInteger -Незмінні знакові цілі числа довільної точності
        //
        // BigInteger є типом даних, який дає можливість розробнику виконувати операції
        // з цілими числами, розрядність яких більша за 64 біти. Числа зберігаються у масиві
        // байтів, операції над ними виконуються функціями, які мають клас BigInteger
        //
        // BigInteger можна ініціалізувати, використовуючи масив байтів чи рядок.
        
        BigInteger fooBigInteger = new BigInteger(fooByteArray);


        // BigDecimal — Незмінні знакові дробові числа довільної точності
        //
        // BigDecimal складається з двох частин: цілого числа довільної точності 
        // з немасштабованим значенням та 32-бітного масштабованого цілого числа
        //
        // BigDecimal дозволяє розробникам контролювати десяткове округлення.
        // Рекомендовано використовувати BigDecimal зі значеннями валют
        // і там, де необхідна точність дробових обчислень.
        //
        // BigDecimal може бути ініціалізований типами даних int, long, double або String
        // чи немасштабованим значенням (BigInteger) і масштабованим значенням (int).

        BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt);
        
        // Для дотримання заданої точності рекомендується використовувати
        // конструктор, який приймає String 
        
        BigDecimal tenCents = new BigDecimal("0.1");


        // Рядки
        String fooString = "Це мій рядок!";

        // \n є символом переходу на новий рядок
        String barString = "Друк з нового рядка?\nНема питань!";
        // \t — це символ табуляції
        String bazString = "Хочете додати табуляцію?\tТримайте!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // Масиви
        // Розмір масиву має бути визначений перед ініціалізацією
        // Наведений формат ілюструє ініціалізацію масивів
        // <тип даних>[] <ім’я змінної> = new <тип даних>[<розмір масиву>];
        // <тип даних> <ім’я змінної>[] = new <тип даних>[<розмір масиву>];
        int[] intArray = new int[10];
        String[] stringArray = new String[1];
        boolean boolArray[] = new boolean[100];

        // Інший шлях оголошення та ініціалізації масиву
        int[] y = {9000, 1000, 1337};
        String names[] = {"Bob", "John", "Fred", "Juan Pedro"};
        boolean bools[] = new boolean[] {true, false, false};

        // Індексація масиву — доступ за елементами
        System.out.println("intArray @ 0: " + intArray[0]);

        // Масиви є змінними та мають нульовий елемент.
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // Додатково
        // ArrayLists — Схожі на масив, але мають більший функціонал та змінний розмір.
        // LinkedLists — Реалізація двозв’язного списку. Всі операції
        //               виконуються так, як очікується від
        //               двозв’язного списку.
        // Maps — Множина об’єктів, які пов’язують ключ зі значенням. Map є
        //        інтерфейсом, тому не може бути успадкований.
        //        Типи ключів і значень, які зберігаються в Map, мають
        //        вказуватись у класі, який його реалізує.
		//		  Ключ не може повторюватись і пов’язаний лише з одним значенням        
        // HashMaps — Цей клас використовує хеш-таблицю для реалізації інтерфейсу Map.
        //            Це дозволяє виконувати певні операції,
        //            такі, як отримання та вставка елемента,
        //            залишаючись постійними навіть для великої кількості елементів.

        ///////////////////////////////////////
        // Оператори
        ///////////////////////////////////////
        System.out.println("\n->Оператори");

        int i1 = 1, i2 = 2; // Коротка форма присвоєння

        // Арифметичні операції виконуються очевидним способом
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (int/int повертається як int)
        System.out.println("1/2 = " + (i1 / (double)i2)); // => 0.5

        // Ділення з остачею
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // Оператори порівняння
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // Логічні оператори
        System.out.println("3 > 2 && 2 > 3? " + ((3 > 2) && (2 > 3))); // => false
        System.out.println("3 > 2 || 2 > 3? " + ((3 > 2) || (2 > 3))); // => true
        System.out.println("!(3 == 2)? " + (!(3 == 2))); // => true

        // Бітові оператори!
        /*
        ~      Унарне бітове доповнення
        <<     Знаковий зсув уліво
        >>     Знаковий/Арифметичний зсув управо
        >>>    Беззнаковий/Логічний зсув управо
        &      Бітове І
        ^      Бітови виключне АБО
        |      Бітове АБО
        */

        // Інкремент
        int i = 0;
        System.out.println("\n->Інкремент/Декремент");
        // Оператори ++ і -- здійснюють інкремент та декремент ретроспективно.
        // Якщо вони розташовані перед змінною, операція виконається перед поверненням;
        // якщо після неї — повернеться інкремент або декремент.
        System.out.println(i++); // i = 1, друкує 0 (постінкремент)
        System.out.println(++i); // i = 2, друкує 2 (преінкремент)
        System.out.println(i--); // i = 1, друкує 2 (постдекремент)
        System.out.println(--i); // i = 0, друкує 0 (предекремент)

        ///////////////////////////////////////
        // Керуючі конструкції
        ///////////////////////////////////////
        System.out.println("\n->Керуючі конструкції");

        // Оператор if використовується так само, як у мові C
        int j = 10;
        if (j == 10) {
            System.out.println("Це надрукується");
        } else if (j > 10) {
            System.out.println("А це — ні");
        } else {
            System.out.println("Це — також ні");
        }

        // Цикл з передумовою While
        int fooWhile = 0;
        while(fooWhile < 100) {
            System.out.println(fooWhile);
            // Інкремент лічильника
            // Виконається 100 разів, fooWhile 0,1,2...99
            fooWhile++;
        }
        System.out.println("fooWhile Value: " + fooWhile);

        // Цикл з післяумовою Do While
        int fooDoWhile = 0;
        do {
            System.out.println(fooDoWhile);
            // Інкремент лічильника
            // Виконається 99 разів, fooDoWhile 0->99
            fooDoWhile++;
        } while(fooDoWhile < 100);
        System.out.println("Значення fooDoWhile: " + fooDoWhile);

        // Цикл з параметром For
        // структура циклу => for(<початковий стан>; <умова завершення>; <крок>)
        for (int fooFor = 0; fooFor < 10; fooFor++) {
            System.out.println(fooFor);
            // Виконається 10 разів, fooFor 0->9
        }
        System.out.println("Значення fooFor: " + fooFor);
        
        // Вихід із вкладеного циклу через мітку
        outer:
        for (int i = 0; i < 10; i++) {
          for (int j = 0; j < 10; j++) {
            if (i == 5 && j ==5) {
              break outer;
              // вихід із зовнішнього циклу, а не лише внутрішнього
            }
          }
        }
        
        // Цикл For Each
        // Призначений для перебору масивів та колекцій       
        int[] fooList = {1, 2, 3, 4, 5, 6, 7, 8, 9};       

        for (int bar : fooList) {
            System.out.println(bar);
            // Повторюється 9 разів та друкує числа від 1 до 9 на нових рядках
        }

        // Оператор вибору Switch Case
        // Оператор вибору працює з типами даних byte, short, char, int.
        // Також працює з переліками Enum, 
        // класом String та класами-обгортками примітивних типів:
        // Character, Byte, Short та Integer.
        int month = 3;
        String monthString;
        switch (month) {
            case 1: monthString = "Січень";
                    break;
            case 2: monthString = "Лютий";
                    break;
            case 3: monthString = "Березень";
                    break;
            default: monthString = "Інший місяць";
                     break;
        }
        System.out.println("Результат Switch Case: " + monthString);
        
        // Починаючи з Java 7 і далі, вибір рядкових змінних здійснюється так:
        String myAnswer = "можливо";
        switch(myAnswer) {
            case "так":
                System.out.println("Ви відповіли «Так».");
                break;
            case "ні":
                System.out.println("Ви відповіли «ні».");
                break;
            case "можливо":
                System.out.println("Ви відповіли «Можливо».");
                break;
            default:
                System.out.println("Ви відповіли «" + myAnswer + "»");
                break;
        }

        // Тернарний оператор вибору
        // Можна використовувати оператор «?» (знак питання) для визначення умови.
        // Читається так: «Якщо (умова) вірна, то <перше значення>, інакше
        // <друге значення>»
        int foo = 5;
        String bar = (foo < 10) ? "A" : "B";
        System.out.println(bar); // Надрукується А, бо умова вірна


        ////////////////////////////////////////
        // Перетворення типів
        ////////////////////////////////////////

        // Перетворення String на Integer
        Integer.parseInt("123");//поверне числову версію рядка "123"

        // Перетворення Integer на String
        Integer.toString(123);//повертає рядкову версію 123

        // Для інших перетворень є наступні класи:
        // Double
        // Long
        // String

        // Приведення типів
        // Тут можна прочитати про приведення об’єктів (англ.):
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // Класи та функції
        ///////////////////////////////////////

        System.out.println("\n->Класи та функції");

        // (Клас Bicycle наведений нижче)

        // Новий об’єкт класу
        Bicycle trek = new Bicycle();

        // Виклик методу об’єкта
        trek.speedUp(3); // Постійно використовуються методи з назвами set і get
        trek.setCadence(100);

        // toString повертає рядкове представлення об’єкту.
        System.out.println("Інформація про об’єкт trek: " + trek.toString());
        
        // У Java немає синтаксису для явного створення статичних колекцій.
        // Це можна зробити так:

        private static final Set<String> COUNTRIES = new HashSet<String>();
        static {
           validCodes.add("DENMARK");
           validCodes.add("SWEDEN");
           validCodes.add("FINLAND");
        }

        // Але є інший спосіб — ініціалізація з подвійними фігурними дужками.

        private static final Set<String> COUNTRIES = new HashSet<String>() {{
            add("DENMARK");
            add("SWEDEN");
            add("FINLAND");
        }}

        // Використовується анонімний внутрішній клас

    } // Кінець методу main
} // Кінець класу LearnJava


// У .java-файл можна додавати інші, не public класи зовнішнього рівня,
// але це не є хорошою практикою. Розміщуйте класи в окремих файлах.


// Синтаксис оголошення класу:
// <public/private/protected> class <ім’я класу> {
//    // поля, конструктори, функції та ін.
//    // у Java функції називаються методами.
// }

class Bicycle {

    // Поля (змінні) класу Bicycle
    public int cadence; // Public: доступно звідусіль
    private int speed;  // Private: доступно лише у межах класу
    protected int gear; // Protected: доступно лише класові та його нащадкам
    String name; // за замовчанням: доступно у даному пакеті

    static String className; // статична змінна класу

    // статичний блок
    // Java не має статичних конструкторів, але
    // має статичний блок ініціалізації змінних класу    
    // Цей блок виконується при завантаженні класу.
    static {
        className = "Bicycle";
    }

    // Конструктори є способом створення класу
    // Оце — конструктор
    public Bicycle() {
        // Можна викликати інший конструктор:
        // this(1, 50, 5, "Bontrager");
        gear = 1;
        cadence = 50;
        speed = 5;
        name = "Bontrager";
    }

    // Цей конструктор приймає аргументи
    public Bicycle(int startCadence, int startSpeed, int startGear,
        String name) {
        this.gear = startGear;
        this.cadence = startCadence;
        this.speed = startSpeed;
        this.name = name;
    }

    // Синтаксис методу:
    // <public/private/protected> <тип повернутого значення> <ім’я методу>(<аргументи>)

    // Java-класи часто мають методи для отримання та встановлення змінних

    // Синтаксис оголошення методу:
    // <модифікатор доступу> <тип повернутого значення> <ім’я методу>(<аргументи>)
    public int getCadence() {
        return cadence;
    }

    // void-методи не повертають значень
    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void speedUp(int increment) {
        speed += increment;
    }

    public void slowDown(int decrement) {
        speed -= decrement;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    //Метод показує значення змінних об’єкту.
    @Override // Успадковано від класу Object.
    public String toString() {
        return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
            " name: " + name;
    }
} // кінець класу Bicycle

// PennyFarthing є розширенням (нащадком) класу Bicycle
class PennyFarthing extends Bicycle {
    // (Penny Farthings мають велике переднє колесо.
    // Вони не мають передач.)

    public PennyFarthing(int startCadence, int startSpeed){
        // Виклик батьківського конструктора через super
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // Перевизначений метод має бути відмічений аннотацією, яка починається зі знака @.
    // Для ознайомлення з аннотаціями перейдіть за посиланням
    // http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGear(int gear) {
        gear = 0;
    }
}

// Інтерфейси
// Синтаксис оголошення інтерфейсів
// <рівень доступу> interface <ім’я інтерфейсу> extends <батьківський інтерфейс> {
//     // Константи
//     // Оголошення методів
// }

//Приклад — їжа (Food):
public interface Edible {
    public void eat(); // Будь-які класи, що реалізують цей інтерфейс,
                       // повинні реалізувати цей метод.
}

public interface Digestible {
    public void digest();
}


// Можна створити клас, що реалізує обидва інтерфейси.
public class Fruit implements Edible, Digestible {
  
    @Override
    public void eat() {
        // ...
    }

    @Override
    public void digest() {
        // ...
    }
}

// В Java можна успадковувати лише один клас, але реалізовувати багато
// інтерфейсів. Наприклад:
public class ExampleClass extends ExampleClassParent implements InterfaceOne,
    InterfaceTwo {

    @Override
    public void InterfaceOneMethod() {
    }

    @Override
    public void InterfaceTwoMethod() {
    }

}

// Абстрактні класи

// Синтаксис оголошення абстрактних класів:
// <рівень доступу> abstract <ім’я класу> extends <батьківський абстрактний клас> {
//     // Константи і змінні
//     // Оголошення методів
// }

// Позначення класу як абстрактного означає, що оголошені у ньому методи мають
// бути реалізовані у дочірніх класах. Подібно до інтерфейсів, не можна створити екземпляри
// абстракних класів, але їх можна успадковувати. Нащадок зобов’язаний реалізувати всі абстрактні
// методи. на відміну від інтерфейсів, абстрактні класи можуть мати як визначені,
// так і абстрактні методи. Методи в інтерфейсах не мають тіла,
// за винятком статичних методів, а змінні неявно мають модифікатор final, на відміну від
// абстрактного класу. Абстрактні класи МОЖУТЬ мати метод «main».

public abstract class Animal
{
    public abstract void makeSound();

    // Метод може мати тіло
    public void eat()
    {
        System.out.println("Я тварина, і я їм.");  
        // Зауваження: є доступ до приватних змінних.
        age = 30;
    }

    // Ініціалізація не потрібна
    protected int age;

    public void printAge()
    {
        System.out.println(age);  
    }

    // Абстрактні класи МОЖУТЬ мати метод «main».
    public static void main(String[] args)
    {
        System.out.println("Я абстрактний");
    }
}

class Dog extends Animal
{
    // Слід помічати перевизначення абстрактних методів
    @Override
    public void makeSound()
    {
        System.out.println("Гав!");
        // age = 30;	==> ПОМИЛКА! age є private для Animal
    }

    // Зауваження: Буде помилка, якщо використати аннотацію
    // @Override тут, так як у java не можна
    // перевизначати статичні методи.
    // Те, що тут відбувається, називається приховування методів.
    // Більш детально: http://stackoverflow.com/questions/16313649/
    public static void main(String[] args)
    {
        Dog pluto = new Dog();
        pluto.makeSound();
        pluto.eat();
        pluto.printAge();
    }
}

// Фінальні класи

// Синтаксис оголошення фінальних класів
// <рівень доступу> final <ім’я класу> {
//     // Константи і змінні
//     // Оголошення методів
// }

// Фінальні класи не можуть мати нащадків, також самі вони є останніми нащадками.
// Фінальні класи є протилежністю абстрактних у цьому плані.

public final class SaberToothedCat extends Animal
{
    // Перевизначення методу
    @Override
    public void makeSound()
    {
        System.out.println("Гррр!");
    }
}

// Фінальні методи
public abstract class Mammal()
{
    // Синтаксис фінальних методів:
    // <модифікатор доступу> final <тип повернутого значення> <ім’я функції>(<аргументи>)

    // Фінальні методи не можуть бути перевизначені класом-нащадком,
    // вони є остаточною реалізацією методу.
    public final boolean isWarmBlooded()
    {
        return true;
    }
}


// Тип Enum (перелік)
//
// Enum є спеціальним типом даних, який дозволяє змінним бути певною множиною
// визначених констант. Змінна має відповідати одному зі значень, що
// заздалегідь визначені для неї. Оскільки це константи, імена типів полів у enum
// задаються у верхньому регістрі. Тип «перелік» у Java задається за допомогою
// ключового слова enum. Наприклад, перелік днів тижня можна задати так:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

// Перелік Day можна використовувати так:

public class EnumTest {
    
    // Змінна того же типу, що й перелік
    Day day;
    
    public EnumTest(Day day) {
        this.day = day;
    }
    
    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Понеділкі важкі.");
                break;
                    
            case FRIDAY:
                System.out.println("П’ятниці краще.");
                break;
                         
            case SATURDAY: 
            case SUNDAY:
                System.out.println("Вихідні найліпші.");
                break;
                        
            default:
                System.out.println("Середина тижня так собі.");
                break;
        }
    }
    
    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs(); // => Понеділки важкі.
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs(); // => Середина тижня так собі.
    }
}

// Переліки набагато потужніші, ніж тут показано. 
// Тіло переліків може містити методи та інші змінні.
// Дивіться більше тут: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

```

## Додатково для читання

Посилання, наведені нижче, дозволяють тільки зрозуміти тему. Щоб знайти конкретні приклади, використовуйте Ґуґл.

**Офіційні посібники Oracle**:

* [Посібник Java від Sun / Oracle](http://docs.oracle.com/javase/tutorial/index.html)

* [Java — модифікатори доступу](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [ООП-концепції](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [Наслідування](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [Поліморфізм](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [Абстракція](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [Виключення](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [Інтерфейси](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [параметризація](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Стиль коду у Java](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html)

**Online-практика та посібники**

* [Learneroo.com — Вивчаємо Java](http://www.learneroo.com)

* [Codingbat.com](http://codingbat.com/java)


**Книжки**:

* [Head First Java](http://www.headfirstlabs.com/books/hfjava/)

* [Thinking in Java](http://www.mindview.net/Books/TIJ/)

* [Objects First with Java](http://www.amazon.com/Objects-First-Java-Practical-Introduction/dp/0132492660)

* [Java The Complete Reference](http://www.amazon.com/gp/product/0071606300)
---
language: javascript
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
    - ["clearsense", "https://github.com/clearsense"]
filename: javascript-uk.js
translators:
  - ["Ivan", "https://github.com/IvanEh"]
  - ["Serhii Maksymchuk", "https://github.com/Serg-Maximchuk"]
lang: uk-ua
---

JavaScript було створено в 1995 році Бренданом Айком, який працював у компанії Netscape.
Він був задуманий як проста мова сценаріїв для веб-сайтів, який би доповнював Java
для більш складних веб-застосунків. Але тісна інтеграція з веб-сторінками і
вбудована підтримка браузерами призвела до того, що JavaScript став популярніший
за власне Java.

Зараз JavaScript не обмежується тільки веб-браузеорм. Наприклад, Node.js,
програмна платформа, що дозволяє виконувати JavaScript код з використанням
рушія V8 від браузера Google Chrome, стає все більш і більш популярною.

```js
// С-подібні коментарі. Однорядкові коментарі починаються з двох символів /(слеш)
/* а багаторядкові коментарі починаються з послідовності слеша та зірочки і
   закінчуються символами зірочка-слеш */

//Інструкції можуть закінчуватися крапкою з комою ;
doStuff();

// ... але не обов’язково, тому що крапка з комою автоматично вставляється на
// місці символу нового рядка, крім деяких випадків.
doStuff()

// Ми завжди будемо використовувати крапку з комою в цьому посібнику, тому що ці
// винятки можуть призвести до неочікуваних результатів

///////////////////////////////////
// 1. Числа, Рядки і Оператори

// В JavaScript числа зберігаються тільки в одному форматі (64-bit IEEE 754 double)
// Цей тип має 52-бітну мантису, якої достатньо для збереження чисел з
// точністю до 9✕10¹⁵.
3; // = 3
1.5; // = 1.5

// Деякі прості арифметичні операції працють так, як ми очікуємо.
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004 (а деякі - ні)
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// В тому числі ділення з остачею
5 / 2; // = 2.5

// В JavaScript є побітові операції; коли ви виконуєте таку операцію,
// число з плаваючою точкою переводиться в ціле зі знаком
// довжиною *до* 32 розрядів.
1 << 2; // = 4

// Пріоритет у виразах можна задати явно круглими дужками
(1 + 3) * 2; // = 8

// Є три спеціальні значення, які не є реальними числами:
Infinity; // "нескінченність", наприклад, як результат ділення на 0
-Infinity; // "мінус нескінченність", як результат ділення від’ємного числа на 0
NaN; // "не число", наприклад, ділення 0/0

// Логічні типи
true;
false;

// Рядки створюються за допомогою подвійних та одинарних лапок
'абв';
"Привіт, світе!";

// Для логічного заперечення використовується знак оклику.
!true; // = false
!false; // = true

// Строга рівність ===
1 === 1; // = true
2 === 1; // = false

// Строга нерівність !==
1 !== 1; // = false
2 !== 1; // = true

// Інші оператори порівняння
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// Рядки об’єднуються за допомогою оператора +
"hello, " + "world!"; // = "hello, world!"

// І порівнюються за допомогою > та <
"a" < "b"; // = true

// Перевірка на рівність з приведнням типів здійснюється оператором ==
"5" == 5; // = true
null == undefined; // = true

// ... але приведення не виконується при ===
"5" === 5; // = false
null === undefined; // = false 

// ... приведення типів може призвести до дивних результатів
13 + !0; // 14
"13" + !0; // '13true'

// Можна отримати доступ до будь-якого символа рядка за допомгою charAt
"Це рядок".charAt(0);  // = 'Ц'

// ... або використати метод substring, щоб отримати більший кусок
"Hello, world".substring(0, 5); // = "Hello"

// length - це не метод, а поле
"Hello".length; // = 5

// Типи null и undefined
null; // навмисна відсутність результату
undefined; // використовується для позначення відсутності присвоєного значення

// false, null, undefined, NaN, 0 та "" — хиба; все інше - істина.
// Потрібно відмітити, що 0 — це хиба, а "0" — істина, не зважаючи на те що:
// 0 == "0".

///////////////////////////////////
// 2. Змінні, Масиви, Об’єкти

// Змінні оголошуються за допомогою ключового слова var. JavaScript — мова з
// динамічною типізацією, тому не потрібно явно вказувати тип. Для присвоєння
// значення змінної використовується символ =
var someVar = 5;

// якщо пропустити слово var, ви не отримаєте повідомлення про помилку, ...
someOtherVar = 10;

// ... але вашу змінну буде створено в глобальному контексті, а не там, де
// ви її оголосили

// Змінні, які оголошені без присвоєння, автоматично приймають значення undefined
var someThirdVar; // = undefined

// У математичних операцій є скорочені форми:
someVar += 5; // як someVar = someVar + 5;
someVar *= 10; // тепер someVar = 100

// Інкремент і декремент
someVar++; // тепер someVar дорівнює 101
someVar--; // а зараз 100

// Масиви — це нумеровані списки, які зберігають значення будь-якого типу.
var myArray = ["Привіт", 45, true];

// Доступ до елементів можна отримати за допомогою синтаксиса з квадратними дужками
// Індексація починається з нуля
myArray[1]; // = 45

// Масиви в JavaScript змінюють свою довжину при додаванні нових елементів
myArray.push("Привіт");
myArray.length; // = 4

// Додавання і редагування елементів
myArray[3] = "світ";

// Об’єкти в JavaScript схожі на словники або асоціативні масиви в інших мовах
var myObj = {key1: "Hello", key2: "World"};

// Ключі - це рядки, але лапки не обов’язкі, якщо ключ задовольняє
// правилам формування назв змінних. Значення можуть бути будь-яких типів.
var myObj = {myKey: "myValue", "my other key": 4};

// Атрибути можна отримати використовуючи квадратні дужки
myObj["my other key"]; // = 4

// Або через точку, якщо ключ є правильним ідентифікатором
myObj.myKey; // = "myValue"

// Об’єкти можна динамічно змінювати й додавати нові поля
myObj.myThirdKey = true;

// Коли ви звертаєтесь до поля, що не існує, ви отримуєте значення undefined
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. Керуючі конструкції

// Синтаксис для цього розділу майже такий самий, як у Java

// Умовна конструкція
var count = 1;
if (count == 3) {
    // виконується, якщо count дорівнює 3
} else if (count == 4) {
    // ..
} else {
    // ...
}

// ...  цикл while.
while (true){
    // Нескінченний цикл!
}

// Цикл do-while такий самий, як while, але завжди виконується принаймні один раз.
var input
do {
    input = getInput();
} while (!isValid(input))

// цикл for такий самий, як в C і Java:
// ініціалізація; умова; крок.
for (var i = 0; i < 5; i++) {
    // виконається 5 разів
}

// && — логічне І, || — логічне АБО
if (house.size == "big" && house.color == "blue") {
    house.contains = "bear";
}
if (color == "red" || color == "blue") {
    // колір червоний або синій
}

// && та || використовують скорочене обчислення
// тому їх можна використовувати для задання значень за замовчуванням.
var name = otherName || "default";

// Оператор switch виконує перевірку на рівність за допомогою ===
// використовуйте break, щоб призупити виконання наступного case,  
grade = 4;
switch (grade) {
  case 5:
    console.log("Відмінно");
    break;
  case 4:
    console.log("Добре");
    break;
  case 3:
    console.log("Можна краще");
    break;
  default:
    console.log("Погано!");
    break;
}


///////////////////////////////////
// 4. Функції, область видимості і замикання

// Функції в  JavaScript оголошуються за допомогою ключового слова function.
function myFunction(thing) {
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"
 
// Зверніть увагу, що значення яке буде повернено, повинно починатися на тому ж
// рядку, що і ключове слово return, інакше завжди буде повертатися значення undefined
// через автоматичну вставку крапки з комою
function myFunction()
{
    return // <- крапка з комою вставляється автоматично
    {
        thisIsAn: 'object literal'
    }
}
myFunction(); // = undefined

// В JavaScript функції - це об`єкти першого класу, тому вони можуть присвоюватися
// іншим змінним і передаватися іншим функціям, наприклад, щоб визначити обробник
// події.
function myFunction() {
    // код буде виконано через 5 сек.
}
setTimeout(myFunction, 5000);
// setTimeout не є частиною мови, але реалізований в браузерах і Node.js

// Функції не обов’язково мають мати ім’я при оголошенні — ви можете написати
// анонімну функцію як аргумент іншої функції
setTimeout(function() {
    // Цей код буде виконано через п’ять секунд
}, 5000);

// В JavaScript реалізована концепція області видимості; функції мають свою
// область видимості, а інші блоки не мають
if (true) {
    var i = 5;
}
i; // = 5, а не undefined, як це звичайно буває в інших мовах

// Така особливість призвела до шаблону "анонімних функцій, які викликають самих себе"
// що дозволяє уникнути проникнення змінних в глобальну область видимості
(function() {
    var temporary = 5;
    // об’єкт window зберігає глобальний контекст; таким чином ми можемо також додавати
    // змінні до глобальної області
    window.permanent = 10;
})();
temporary; // повідомлення про помилку ReferenceError
permanent; // = 10

// Замикання - один з найпотужніших інструментів JavaScript. Якщо функція визначена
// всередині іншої функції, то внутрішня функція має доступ до змінних зовнішньої
// функції навіть після того, як код буде виконуватися поза контекстом зовнішньої функції
function sayHelloInFiveSeconds(name) {
    var prompt = "Привіт, " + name + "!";
    // Внутрішня функція зберігається в локальній області так,
    // ніби функція була оголошена за допомогою ключового слова var
    function inner() {
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout асинхронна, тому функція sayHelloInFiveSeconds одразу завершиться,
    // після чого setTimeout викличе функцію inner. Але функція inner
    // «замкнута» кругом sayHelloInFiveSeconds, вона все рівно має доступ до змінної prompt
}
sayHelloInFiveSeconds("Адам"); // Через 5 с відкриється вікно «Привіт, Адам!»

///////////////////////////////////
// 5. Об’єкти: конструктори і прототипи

// Об’єкти можуть містити функції
var myObj = {
    myFunc: function() {
        return "Hello, world!";
    }
};
myObj.myFunc(); // = "Hello, world!"

// Функції, що прикріплені до об’єктів мають доступ до поточного об’єкта за 
// допомогою ключового слова this.
myObj = {
    myString: "Hello, world!",
    myFunc: function() {
        return this.myString;
    }
};
myObj.myFunc(); // = "Hello, world!"

// Значення this залежить від того, як функція викликається
// а не від того, де вона визначена. Таким чином наша функція не працює, якщо
// вона викликана не в контексті об’єкта
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// Функція може бути присвоєна іншому об’єкту. Тоді вона матиме доступ до 
// цього об’єкта через this
var myOtherFunc = function() {
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO, WORLD!"

// Контекст виконання функції можна задати за допомогою сall або apply
var anotherFunc = function(s) {
    return this.myString + s;
}
anotherFunc.call(myObj, " Hello!"); // = "Hello, world! Hello!"

// Функцiя apply приймає в якості аргументу масив
anotherFunc.apply(myObj, [" Hello!"]); // = "Hello, world! Hello!"

// apply можна використати, коли функція працює послідовністю аргументів, а
// ви хочете передати масив
Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (Ой-ой!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// Але call і apply — тимчасові. Коли ми хочемо зв’язати функцію і об’єкт 
// використовують bind
var boundFunc = anotherFunc.bind(myObj);
boundFunc(" Hello!"); // = "Hello world, Hello!"

// Bind можна використати для задання аргументів
var product = function(a, b) { return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// Коли ви викликаєте функцію за допомогою ключового слова new, створюється новий об’єкт,
// доступний функції за допомогою this. Такі функції називають конструкторами.
var MyConstructor = function() {
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// У кожного об’єкта є прототип. Коли ви звертаєтесь до поля, яке не існує в цьому
// об’єкті, інтерпретатор буде шукати поле в прототипі

// Деякі реалізації мови дозволяють отримати доступ до прототипа об’єкта через
// "магічну" властивість __proto__. Це поле не є частиною стандарта, але існують
// стандартні способи використання прототипів, які ми побачимо пізніше
var myObj = {
    myString: "Hello, world!"
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function() {
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// Аналогічно для функцій
myObj.myFunc(); // = "hello, world!"

// Якщо інтерпретатор не знайде властивості в прототипі, то він продовжить пошук
// в прототипі прототипа і так далі
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

// Кожен об’єкт зберігає посилання на свій прототип. Це значить, що ми можемо змінити
// наш прототип, і наші зміни будуть всюди відображені.
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

// Ми сказали, що властивість __proto__ нестандартна, і нема ніякого стандартного способу
// змінити прототип об’єкта, що вже існує. Але є два способи створити новий об’єкт із заданим
// прототипом

// Перший спосіб — це Object.create, який з’явився в JavaScript недавно,
// а тому в деяких реалізаціях може бути недоступним.
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// Другий спосіб: у конструкторів є властивість з іменем prototype. Це *не*
// прототип функції-конструктора, це прототип для нових об’єктів, які будуть створені
// цим конструктором і ключовим словом new.
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function() {
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// У вбудованих типів(рядок, число) теж є конструктори, які створють еквівалентні
// об’єкти-обгортки
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// Але вони не ідентичні
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false

// Об’єкти-обгортки і вбудовані типи мають спільні прототипи, тому
// ви можете розширити функціонал рядків:
String.prototype.firstCharacter = function() {
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// Такий прийом часто використовуються в поліфілах, які реалізують нові можливості
// JavaScript в старій реалізації мови, так що вони можуть бути використані в старих
// середовищах

// Наприклад, Object.create доступний не у всіх реалізаціях, але ми можемо
// використати функції за допомогою наступного поліфіла:
if (Object.create === undefined) { // не перезаписуємо метод, якщо він існує
    Object.create = function(proto) {
        // Створюємо правильний конструктор з правильним прототипом
        var Constructor = function(){};
        Constructor.prototype = proto;
    
        return new Constructor();
    }
}
```

## Що почитати

* [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript
* [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
* [3]: https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core
* [4]: http://www.learneroo.com/modules/64/nodes/350
* [5]: http://bonsaiden.github.io/JavaScript-Garden/
* [6]: http://www.amazon.com/gp/product/0596805527/
* [7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
* [8]: http://eloquentjavascript.net/
* [9]: http://jstherightway.org/
---
language: json
filename: learnjson-ua.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
  - ["Ehreshi Ivan", "https://github.com/IvanEh"]
  - ["Serhii Maksymchuk", "https://github.com/Serg-Maximchuk"]
lang: uk-ua
---

JSON - це надзвичайно простий формат обміну даними. Згідно з [json.org](http://json.org), для людей він легкий в написанні і читанні, а для комп’ютерів в розборі та генерації.

JSON підтримує наступні структури даних:

* Колекція пар ключ/значення (`{ "ключ": "значення" }`). У різних мовах програмування реалізується як об’єкт, запис, структура, словник, хеш-таблиця, іменований список або асоціативний масив.
* Впорядкований список значень (`[ "елемент0", "елемент1" ]`). У різних мовах програмування реалізується як масив, вектор, список або послідовність.
* Рядки: `"привіт"`, `"\"Лапки\""`, `"\u0abe"`, `"Новий рядок.\n"`
* Числа: `23`, `0.11`, `12e10`, `3.141e-10`, `1.23e+4`
* Інші: `true`, `false`, `null`

JSON в чистій формі не містить коментарів, але більшість парсерів підтримують коментарі в C-стилі (`//`, `/* */`). Деякі парсери також не звертають уваги на кому після останнього поля, але їх варто уникати для кращої сумісності.

```json
{
  "ключ": "значення",
  
  "ключі": "завжди мають бути обгорнуті в подвійні лапки",
  "числа": 0,
  "рядки": "Пρивіт, світe. Допускаються всі unicode-символи разом із \"екрануванням\".",
  "логічний тип": true,
  "нічого": null,

  "велике число": 1.2e+100,

  "об’єкти": {
    "коментар": "Більшість ваших структур будуть складатися з об’єктів",

    "масив": [0, 1, 2, 3, "масиви можуть містити будь-які типи", 5],

    "інший об’єкт": {
      "коментар": "Об’єкти можуть містити інші об’єкти. Це дуже корисно."
    }
  },

  "безглуздя": [
    {
      "джерело калію": ["банани"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "нео"],
      [0, 0, 0, 1]
    ]
  ],
  
  "альтернативний стиль": {
    "коментар": "Гляньте!"
  , "позиція коми": "неважлива, коректно якщо вона знаходиться перед наступним полем"
  , "інший коментар": "класно"
  },

  "Це було не довго": "І ви впорались! Тепер ви знаєте все про JSON."
}

Одиничний масив значень теж є правильним JSON

[1, 2, 3, "text", true]


```
---
language: ruby
filename: learnruby-ua.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
  - ["Gabriel Halley", "https://github.com/ghalley"]
  - ["Persa Zula", "http://persazula.com"]
  - ["Jake Faris", "https://github.com/farisj"]
translators:
  - ["Serhii Maksymchuk", "https://github.com/Serg-Maximchuk"]
lang: uk-ua
---

Rubi — це інтерпретована, повністю об'єктно-орієнтована мова програмування з чіткою динамічною типізацією.

```ruby
# Це коментар

=begin
Це багаторядковий коментар
Ніхто їх не використовує
Тобі теж не варто
=end

# В першу чергу: все являється об’єктом.

# Числа це об’єкти

3.class #=> Fixnum

3.to_s #=> "3"


# Базова арифметика
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
5 % 3 #=> 2

# Побітові оператори
3 & 5 #=> 1
3 | 5 #=> 7
3 ^ 5 #=> 6

# Арифметика це просто синтаксичний цукор
# для виклику методу об’єкта
1.+(3) #=> 4
10.* 5 #=> 50

# Спеціальні значення теж об’єкти
nil # еквівалентно null в інших мовах
true # істина
false # хиба

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# Рівність
1 == 1 #=> true
2 == 1 #=> false

# Нерівність
1 != 1 #=> false
2 != 1 #=> true

# Окрім власне false, nil це ще одне "хибне" значення

!nil   #=> true
!false #=> true
!0     #=> false

# Інші порівняння
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Комбінований оператор порівняння
1 <=> 10 #=> -1
10 <=> 1 #=> 1
1 <=> 1 #=> 0

# Логічні оператори
true && false #=> false
true || false #=> true
!true #=> false

# Є альтернативні версії логічних операторів з набагато меншим
# прецедентом. Вони використовуються в конструкціях з контролем
# виконання ланцюга виразів які повертають булевий результат.

# `do_something_else` викликається лише якщо `do_something` повертає true.
do_something() and do_something_else()
# `log_error` викликається лише якщо `do_something` повертає false.
do_something() or log_error()


# Strings це об’єкти

'Я — рядок'.class #=> String
"Я теж рядок".class #=> String

placeholder = 'використовувати інтерполяцію рядків'
"Я можу #{placeholder} коли користуюсь рядками в подвійних лапках"
#=> "Я можу використовувати інтерполяцію рядків коли користуюсь рядками в подвійних лапках"

# Варто надавати перевагу рядкам в одинарних лапках де це можливо
# Рядки в подвійних лапках викликають додаткові внутрішні обчислення

# Об’єднуйте рядки, але не з числами
'hello ' + 'world'  #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"

# Об’єднуйте рядки з операторами
'hello ' * 3 #=> "hello hello hello "

# Додавання до рядка
'hello' << ' world' #=> "hello world"

# Вивести рядок з переходом на новий рядок вкінці
puts "Я надрукований!"
#=> Я надрукований!
#=> nil

# Вивести рядок без переходу на новий
print "Я надрукований!"
#=> Я надрукований! => nil

# Змінні
x = 25 #=> 25
x #=> 25

# Зверніть увагу, оператор присвоєння повертає присвоєне значення
# Отже можна робити одночасне присвоєння кількох змінних:
x = y = 10 #=> 10
x #=> 10
y #=> 10

# Для назв змінних використовується зміїний_регістр
snake_case = true

# Використовуйте назви змінних які їх характеризують 
path_to_project_root = '/good/name/'
path = '/bad/name/'

# Символи (теж об’єкти)
# Символи є незмінними константами багаторазового використання, внутрішньо
# представлені цілочисельним значенням. Вони часто використовуються замість
# рядків щоб ефективно передати конкретні, значущі значення.

:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# Масиви

# Ось масив
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Масиви можуть містити елементи різних типів
[1, 'hello', false] #=> [1, "hello", false]

# Масиви можуть бути проіндексовані
# З початку
array[0] #=> 1
array.first #=> 1
array[12] #=> nil

# Як і арифметика, доступ до елемента масиву у вигляді [індекс] — це лише
# синтаксичний цукор виклику методу [] об’єкта
array.[] 0 #=> 1
array.[] 12 #=> nil

# З кінця
array[-1] #=> 5
array.last #=> 5

# З початковим індексом та довжиною
array[2, 3] #=> [3, 4, 5]

# Реверс масиву
a=[1,2,3]
a.reverse! #=> [3,2,1]

# Елементи масиву за діапазоном індексів
array[1..3] #=> [2, 3, 4]

# Додавати елементи в масив можна так:
array << 6 #=> [1, 2, 3, 4, 5, 6]
# Або так:
array.push(6) #=> [1, 2, 3, 4, 5, 6]

# Перевірити чи масив містить елемент
array.include?(1) #=> true

# Хеш — це масив пар ключ/значення.
# Хеш оголошується з використанням фігурних дужок:
hash = { 'color' => 'green', 'number' => 5 }

hash.keys #=> ['color', 'number']

# Значення в хеші може бути швидко знайдене за ключем:
hash['color'] #=> 'green'
hash['number'] #=> 5

# Запит значення за неіснуючим ключем повертає nil:
hash['nothing here'] #=> nil

# Починаючи з Ruby 1.9 з’явився спеціальний синтаксис при використанні
# символів в якості ключів:
new_hash = { defcon: 3, action: true }

new_hash.keys #=> [:defcon, :action]

# Перевірка наявності ключів та значень в хеші
new_hash.key?(:defcon) #=> true
new_hash.value?(3) #=> true

# Хеші та масиви — перелічувальні типи даних
# Вони мають багато корисних методів, такі як each, map, count, та інші.

# Оператор вибору "if"
if true
  'якщо'
elsif false
  'інакше якщо, опціонально'
else
  'інакше, також опціонально'
end

# Оператор циклу "for"
for counter in 1..5
  puts "ітерація #{counter}"
end
#=> ітерація 1
#=> ітерація 2
#=> ітерація 3
#=> ітерація 4
#=> ітерація 5

# Проте, ніхто не використовує "for" в циклах.
# Замість цього варто використовувати метод "each" і передати йому блок.
# Блок — це відокремлений код який можна передати в метод, наприклад в "each".
# Це аналог лямбда-виразів, анонімних функцій або замикань в інших мовах програмування.

# Метод "each" для діапазону запускає блок один раз для кожного елементу діапазону.
# Лічильник передається блоку в якості аргументу.

# Виклик методу "each" з блоком виглядає наступним чином:
(1..5).each do |counter|
  puts "ітерація #{counter}"
end
#=> ітерація 1
#=> ітерація 2
#=> ітерація 3
#=> ітерація 4
#=> ітерація 5

# Також можна загорнути блок в фігурні дужки:
(1..5).each { |counter| puts "ітерація #{counter}" }

# Вміст структур даних також може бути ітерований використовуючи метод "each":
array.each do |element|
  puts "#{element} є елементом масиву"
end
hash.each do |key, value|
  puts "#{key} є #{value}"
end

# Якщо є необхідність індексувати ітерацію, можна використати метод "each_with_index":
array.each_with_index do |element, index|
  puts "#{element} під номером #{index} в масиві"
end

# Оператор циклу "while"
counter = 1
while counter <= 5 do
  puts "ітерація #{counter}"
  counter += 1
end
#=> ітерація 1
#=> ітерація 2
#=> ітерація 3
#=> ітерація 4
#=> ітерація 5

# Є й інші корисні функції для циклів, такі як "map", "reduce",
# "inject" та інші. Наприклад "map" в циклі проходить по масиву,
# виконує над елементами операцію(-ї) в блоці і повертає абсолютно
# новий масив.
array = [1,2,3,4,5]
doubled = array.map do |element|
  element * 2
end
puts doubled
#=> [2,4,6,8,10]
puts array
#=> [1,2,3,4,5]

# Оператор множинного вибору
grade = 'B'

case grade
when 'A'
  puts 'Відмінно!'
when 'B'
  puts 'Пощастить наступного разу'
when 'C'
  puts 'Ти можеш краще'
when 'D'
  puts 'Майже четвірка'
when 'E'
  puts 'Випросив'
when 'F'
  puts 'Не здав!'
else
  puts 'Інша система оцінювання, так?'
end
#=> "Пощастить наступного разу"

# Оператор "case" також може використовувати діапазон:
grade = 82
case grade
when 90..100
  puts 'Ура!'
when 80...90
  puts 'Хороша робота'
when 60...80
  puts 'Ну, хоч щось'
else
  puts 'Не здав!'
end
#=> "Хороша робота"

# Обробка вийнятків:
begin
  # код з можливим вийнятком
  raise NoMemoryError, 'Ви використали всю пам’ять.'
rescue NoMemoryError => exception_variable
  puts 'Помилка NoMemoryError', exception_variable
rescue RuntimeError => other_exception_variable
  puts 'Помилка RuntimeError'
else
  puts 'Цей код запуститься якщо вийнятків не було взагалі'
ensure
  puts 'Цей код запуститься в будь-якому випадку'
end

# Функції

def double(x)
  x * 2
end

# Функції (і всі блоки) неявно повертають значення останнього виразу
double(2) #=> 4

# Дужки не є обов’язковими якщо результат недвозначний
double 3 #=> 6

double double 3 #=> 12

def sum(x, y)
  x + y
end

# Аргументи методів розділяються комою
sum 3, 4 #=> 7

sum sum(3, 4), 5 #=> 12

# yield
# Всі методи мають неявний, опціональний параметр, який
# можна викликат за допомогою ключового слова 'yield'
def surround
  puts '{'
  yield
  puts '}'
end

surround { puts 'привіт світ' }

# {
# привіт світ
# }


# Ви можете передати блок у функцію
# "&" позначає посилання на блок
def guests(&block)
  block.call 'some_argument'
end

# Ви можете передати список аргументів, вони будуть перетворені в масив
# Для цього існує оператор ("*")
def guests(*array)
  array.each { |guest| puts guest }
end

# Якщо метод повертає масив, ви можете використати деструктуризацію
def foods
    ['млинці', 'бутерброд', 'лазанья']
end
breakfast, lunch, dinner = foods
breakfast #=> 'млинці'
dinner #=> 'лазанья'

# Зазвичай методи які повертають булевий результат закінчуються знаком питання
5.even? # false
5.odd? # true

# І якщо метод закінчується знаком оклику, то він робить щось деструктивне 
# типу зміни отриманого аргументу. Багато методів мають версію з "!" які
# змінюють аргумент, та без "!" що повертають новий об’єкт
company_name = "Дандер Міфлін"
company_name.upcase #=> "ДАНДЕР МІФЛІН"
company_name #=> "Дандер Міфлін"
company_name.upcase! # змінна company_name зміниться цього разу!
company_name #=> "ДАНДЕР МІФЛІН"


# Клас оголошується ключовим словом class
class Human

  # Змінна класу. Вона поширюється на всі екземпляри цього класу.
  @@species = 'Homo sapiens'

  # Основний ініціалізатор
  def initialize(name, age = 0)
    # Призначення аргументу "name" до однойменної змінної екземпляру
    @name = name
    # Якщо аргумент "age" не заданий, то йому буде присвоєно дефолтне значення
    # зі списку аргументів
    @age = age
  end

  # Метод-сетер
  def name=(name)
    @name = name
  end

  # Метод-ґетер
  def name
    @name
  end

  # Функціональність вище може бути інкапсульована використовуючи метод attr_accessor:
  attr_accessor :name

  # Ґетери і сетери можуть бути створені індивідуально, наприклад:
  attr_reader :name
  attr_writer :name

  # Метод класу позначається ключовим словом "self", щоб відрізнити від
  # методів екземпляра класу.
  # Він може бути викликаний лише в класі, але не в екземплярі.
  def self.say(msg)
    puts msg
  end

  def species
    @@species
  end
end


# Ініціалізуємо клас
jim = Human.new('Джим Галперт')

dwight = Human.new('Дуайт Шрут')

# Викличемо кілька методів
jim.species #=> "Homo sapiens"
jim.name #=> "Джим Галперт"
jim.name = "Джим Галперт II" #=> "Джим Галперт II"
jim.name #=> "Джим Галперт II"
dwight.species #=> "Homo sapiens"
dwight.name #=> "Дуайт Шрут"

# Викликати метод класу
Human.say('Привіт') #=> "Привіт"

# Область видимості змінних визначається способом оголошення імені змінної.
# Змінні, що починаються на "$" мають глобальну область видимості.
$var = "Я глобальна змінна"
defined? $var #=> "global-variable"

# Зміннні, що опчинають на "@" мають область видимості екзкмпляра
@var = "Я змінна екземпляра"
defined? @var #=> "instance-variable"

# Змінні, що починаються на "@@" мають область видимості класу
@@var = "Я змінна класу"
defined? @@var #=> "class variable"

# Змінні, що починаються з великої букви, є константами
Var = "Я константа"
defined? Var #=> "constant"

# Клас теж об’єкт. Тому клас може мати змінні екземпляра.
# Змінна класу поширюється між класом і всіма його нащадками.

# Базовий клас
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

# Похідний клас (нащадок)
class Worker < Human
end

Human.foo # 0
Worker.foo # 0

Human.foo = 2 # 2
Worker.foo # 2

# Змінна екземпляра класу не поширюється між класами-нащадками.
class Human
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doctor < Human
end

Human.bar # 0
Doctor.bar # nil

module ModuleExample
  def foo
    'foo'
  end
end

# Включення модулів додає їхні методи до екземплярів класу
# Розширення модулів додає їхні методи в сам клас

class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => NoMethodError: undefined method `foo'

# Колбек виконується при включенні і розширенні модуля

module ConcernExample
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class Something
  include ConcernExample
end

Something.bar     # => 'bar'
Something.qux     # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
```

## Додаткові ресурси

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges.
- [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Learn Ruby through a series of interactive tutorials.
- [Official Documentation](http://ruby-doc.org/core)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.
- [Try Ruby](http://tryruby.org) - Learn the basic of Ruby programming language, interactive in the browser.
---
language: vala
contributors:
    - ["Milo Gilad", "https://github.com/Myl0g"]
filename: LearnVala.vala
---

In GNOME's own words, "Vala is a programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C."

Vala has aspects of Java and C#, so it'll be natural to those who know either.

[Read more here.](https://wiki.gnome.org/Projects/Vala)

```vala

// Single line comment

/* Multiline
Comment */

/**
* Documentation comment
*/

/* Data Types */

char character = 'a'
unichar unicode_character = 'u' // 32-bit unicode character

int i = 2; // ints can also have guaranteed sizes (e.g. int64, uint64)
uint j = -6; // Won't compile; unsigned ints can only be positive

long k;

short l;
ushort m;

string text = "Hello,"; // Note that the == operator will check string content

string verbatim = """This is a verbatim (a.k.a. raw) string. Special characters
(e.g. \n and "") are not interpreted. They may also be multiple lines long.""";

// String Templates allow for easy string formatting
string string_template = @"$text world"; // "$text" evaluates to "Hello,"

int test = 5;
int test2 = 10;
string template2 = @"$(test * test2) is a number."; // Expression evaluation

string template_slice = string_template[7:12]; // => "world"

// Most data types have methods for parsing.

bool parse_bool = bool.parse("false"); // => false
int parse_int = int.parse("-52"); // => -52
string parse_string = parse_int.to_string(); // => "-52"

/* Basic I/O */

stdout.printf(parse_string); // Prints to console
string input = stdin.read_line(); // Gets input from console

stderr.printf("Error message"); // Error printing

/* Arrays */

int[] int_array = new int[10]; // Array of ints with 10 slots
int better_int_array[10]; // Above expression, shortened
int_array.length; // => 10;

int[] int_array2 = {5, 10, 15, 20}; // Can be created on-the-fly

int[] array_slice = int_array2[1:3]; // Slice (copy of data)
unowned int[] array_slice_ref = int_array2[1:3]; // Reference to data

// Multi-dimensional Arrays (defined with a number of commas in the brackets)

int[,] multi_array = new int[6,4]; // 6 is the number of arrays, 4 is their size
int[,] multi_array2 = {{7, 4, 6, 4},
                       {3, 2, 4, 6},
                       {5, 9, 5, 1}}; // new int[3,4]
multi_array2[2,3] = 12; // 2 is the array, 3 is the index in the array
int first_d = multi_array2.length[0] // => 3
int second_d = multi_array2.length[1] // => 4

// Stacked arrays (e.g. int[][]) where array lengths vary are not supported.

// Multi-dimensional arrays cannot be sliced, nor can they be converted to one-
// dimensional.

int[] add_to_array = {};
add_to_array += 12; // Arrays can be dynamically added to

add_to_array.resize(20); // Array now has 20 slots

uint8[] chars = "test message".data;
chars.move(5, 0, 7);
stdout.printf((string) chars); // Casts the array to a string and prints it

/* Control Flow */

int a = 1;
int b = 2;
int[] foreach_demo = {2, 4, 6, 8};

while (b > a) { // While loop; checks if expression is true before executing
  b--;
}

do {
  b--;
}
while (b > a); // Do While loop; executes the code in "do" before while (b > a)

for (a = 0; a < 10; a++) { stdout.printf("%d\n", a); } // for loop

foreach (int foreach_demo_var in foreach_demo) {
  stdout.printf("%d\n", foreach_demo_var);
} // foreach works on any iterable collection

if (a == 0) {
  stdout.printf("%d\n", a);
} else if (a > 1) {
  stdout.printf("%d\n", a);
} else {
  stdout.printf("A is less than 0");
} // if-then-else

switch (a) {
  case 1:
    stdout.printf("A is 1\n");
    break;
  case 5:
  case 10:
    stdout.printf("A is 5 or 10\n");
    break;
  default:
    stdout.printf("???\n")
    break;
} // switch statement

/* Type Casting and Inference */

int cast_to_float = 10;
float casted_float = (float) cast_to_float; // static casting; no runtime checks

// For runtime checks, use dynamic casting.
// Dynamically casted objects must be the following:
// - Object's class is the same class as the desired type
// - Object's class is a subclass of the desired type
// - Desired class is an interface implemented by the object's class

float dyna_casted_float = cast_to_float as float // Won't compile

var inferred_string = "hello"; // Type inference

/* Methods (a.k.a. functions) */

int method_demo(string arg1, Object arg2) { // Returns int and takes args
    return 1;
}

// Vala methods cannot be overloaded.

void some_method(string text) { }
void some_method(int number) { }  // Won't compile

// To achieve similar functionality, use default argument values.

void some_better_method(string text, int number = 0) { }

some_better_method("text");
some_better_method("text", 12);

// varargs (variable-length argument lists) are also supported.

void method_with_varargs(int arg1, ...) {
    var varargs_list = va_list(); // gets the varargs list

    string arg_string = varargs_list.arg(); // gets arguments, one after another
    int int_vararg = varargs_list.arg();

    stdout.printf("%s, %d\n", arg_string, int_vararg)
}

string? ok_to_be_null(int? test_int) { } // "?" denotes possible null value

// Delegates

delegate void DelegateDemo(char char_a);

void delegate_match(char char_a) { // Matches DelegateDemo's signature
  stdout.printf("%d\n");
}

void call_delegate(DelegateDemo d, char char_b) { // Takes a delegate arg
  d(char_b) // calls delegate
}

void final_delegate_demo() {
  call_delegate(delegate_match); // Passes matching method as argument
}

// Lambdas (a.k.a. Anonymous Methods) are defined with "=>"

(a) => { stdout.printf("%d\n", a); } // Prints "a"

/* Namespaces */

namespace NamespaceDemo {
  // Allows you to organize variable names
  int namespace_int = 12;
}
namespace_int += 5; // Won't compile

using NamespaceDemo;
namespace_int += 5; // Valid

/* Structs and Enums */

struct Closet {
  public uint shirts; // Default access modifier is private
  public uint jackets;
}

Closet struct_init_1 = Closet(); // or Closet struct_init_1 = {};
Closet struct_init_2 = {15, 3};
var struct_init_3 = Closet() { // Type inference also works
  shirts = 15;
  jackets = 3;
}

enum HouseSize { // An example of an enum
  SMALL,
  MODERATE,
  BIG
}

/* Classes and Object-Oriented Programming */

class Message : GLib.Object { // Class Message extends GLib's Object
  private string sender; // a private field
  public string text {get; set;} // a public property (more on that later)
  protected bool is_digital = true; // protected (this class and subclasses)
  internal bool sent = false; // internal (classes in same package)

  public void send(string sender) { // public method
    this.sender = sender;
    sent = true;
  }

  public Message() { // Constructor
    // ...
  }

}

// Since method overloading isn't possible, you can't overload constructors.
// However, you can use named constructors to achieve the same functionality.

public class Calculator : GLib.Object {

    public Calculator() {
    }

    public Calculator.with_name(string name) {
    }

    public Calculator.model(string model_id, string name = "") {
      this.with_name(@"$model_id $name"); // Chained constructors with "this"
    }
    ~Calculator() { } // Only needed if you're using manual memory management
}

var calc1 = new Calculator.with_name("Temp");
var calc2 = new Calculator.model("TI-84");

// Signals (a.k.a. events or event listeners) are a way to execute multiple
// methods with the same signature at the same time.

public class SignalDemo : GLib.Object {
  public signal void sig_demo(int sig_demo_int); // Must be public

  public static int main(string[] args) {
    // main method; program does not compile without it

    var sig_demo_class = new SignalDemo(); // New instance of class

    sig_demo_class.sig_demo.connect((ob, sig_int) => { // Lambda used as handler
        stdout.printf("%d\n", sig_int); // "ob" is object on which it is emitted
      });

    sig_demo_class.sig_demo(27); // Signal is emitted

    return 0;
  }
}

// You may use the connect() method and attach as many handlers as you'd like.
// They'll all run at around the same time when the signal is emitted.

// Properties (getters and setters)

class Animal : GLib.Object {
  private int _legs; // prefixed with underscore to prevent name clashes

  public int legs {
    get { return _legs; }
    set { _legs = value; }
  }

  public int eyes { get; set; default = 5; } // Shorter way
  public int kingdom { get; private set; default = "Animalia"} // Read-only

  public static void main(string args[]) {
    rabbit = new Animal();

    // All GLib.Objects have a signal "notify" emitted when a property changes.

    // If you specify a specific property, replace all underscores with dashes
    // to conform to the GObject naming convention.

    rabbit.notify["eyes"].connect((s, p) => { // Remove the ["eyes"] for all
      stdout.printf("Property '%s' has changed!\n", p.name);
    });

    rabbit.legs = 2;
    rabbit.legs += 2;
    rabbit.eyes = 2;

  }
}

// Inheritance: Vala classes may inherit 1 class. Inheritance is not implicit.

class SuperDemo : GLib.Object {
  public int data1;
  protected int data2;
  internal int data3;
  private int data4;

  public static void test_method {  } // Statics can be called w/out an object
}
class SubDemo : SuperDemo {
  public static void main(string args[]) {
    stdout.printf((string) data1); // Will compile
    stdout.printf((string) data2); // Protected can be accessed by subclasses
    stdout.printf((string) data3); // Internal is accessible to package
    stdout.printf((string) data4); // Won't compile
  }
}

// Abstract Classes and Methods

public abstract class OperatingSystem : GLib.Object {
  public void turn_on() {
    stdout.printf("Booted successfully.\n");
  }
  public abstract void use_computer();
}

public class Linux : OperatingSystem {
  public override void use_computer() { // Abstract methods must be overridden
    stdout.printf("Beep boop\n");
  }
}

// Add default behavior to an abstract method by making it "virtual".

public abstract class HardDrive : GLib.Object {
  public virtual void die() {
    stdout.printf("CLICK-CLICK-CLICK\n");
  }
}
public class MyHD : HardDrive {
  public override void die() {
    return;
  }
}

// Interfaces: classes can implement any number of these.

interface Laptop { // May only contain abstracts or virtuals
  public abstract void turn_on();
  public abstract void turn_off();

  public abstract int cores; // Won't compile; fields cannot be abstract
  public abstract int cores {get; set;} // Will compile

  public virtual void keyboard() { // Virtuals are allowed (unlike Java/C#)
    stdout.printf("Clickity-clack\n");
  }
}

// The ability to use virtuals in Vala means that multiple inheritance is
// possible (albeit somewhat confined)

// Interfaces cannot implement interfaces, but they may specify that certain
// interfaces or classes must be also implemented (pre-requisites).

public interface CellPhone : Collection, GLib.Object {}

// You can get the type info of a class at runtime dynamically.

bool type_info = object is TypeName; // uses "is" to get a bool

Type type_info2 = object.get_type();
var type_name = type_info2.name();

Type type_info3 = typeof(Linux);
Linux type_demo = (Linux) Object.new(type_info3);

// Generics

class Computer<OperatingSystem> : GLib.Object {
  private OperatingSystem os;

  public void install_os(OperatingSystem os) {
    this.os = os;
  }
  public OperatingSystem retrieve_os() {
    return this.os;
  }
}

var new_computer = new Computer<Linux>();

/* Other Features */

// Assertions: crash if a statement is not true (at runtime)

bool is_true = true;
assert(is_true);

// Contract Programming

int contract_demo(int arg1, int arg2) {
  requires(arg1 > 0 && arg1 < 10) // Notice the lack of semicolon
  requires(arg2 >= 12)
  ensures(result >= 0)
}

// Error Handling

void error_demo(int int_ex) throws GError {
  if (int_ex != 1) {
    throw new GError("TEST MESSAGE");
  }
}
void error_demo2() {
  try {
    error_demo(0);
  } catch (GError ge) {
    stdout.printf("%s\n", ge.message);
  }
}

// Main Loop

void main() {

  var main_loop = new MainLoop();
  var time = new TimeoutSource(2000);

  time.set_callback(() => { // Executes the following lambda after 2000ms
      stdout.printf("2000ms have passed\n");
      main_loop.quit();
      return false;
  });

  time.attach(main_loop.get_context());

  loop.run();
}

// Pointers (manual memory management)

Object* pointer_obj = new Object(); // Creates Object instance and gives pointer

pointer_obj->some_method(); // Executes some_method
pointer_obj->some_data; // Returns some_data

delete pointer_obj;

int more = 57;
int* more_pointer = &i; // & = address-of
int indirection_demo = more_pointer*; // indirection

// Profiles: affect which Vala features are avaliable and which libraries the
// C-code will use.
// - gobject (default)
// posix
// dova
// Use "--profile=whatever" when compiling.

```
* More Vala documentation can be found [here](https://valadoc.org/).
* [Alternate construction syntax](https://wiki.gnome.org/Projects/Vala/Tutorial#GObject-Style_Construction) similar to GObject
* More on contract programming [here](http://en.wikipedia.org/wiki/Contract_programming)
* Collections library can be found [here](https://wiki.gnome.org/Projects/Vala/Tutorial#Collections)
* [Multithreading](https://wiki.gnome.org/Projects/Vala/Tutorial#Multi-Threading)
* Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw).
* D-Bus [integration](https://wiki.gnome.org/Projects/Vala/Tutorial#D-Bus_Integration)
---
category: tool
tool: git
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
    - ["Vinh Nguyen", "https://twitter.com/vinhnx"]
filename: LearnGit-vi.txt
lang: vi-vn
---

Git là một hệ quản lý mã nguồn và phiên bản phân tán (distributed version control and source code management system).

Nó làm được điều này là do một loạt các snapshot từ đề án của bạn, và nó hoạt động
với các snapshot đó để cung cấp cho bạn với chức năng đến phiên bản và
quản lý mã nguồn của bạn.

## Khái Niệm Versioning

### Version Control là gì?

Version Control là một hệ thống ghi lại những thay đổi ở một tập tin, hay một nhóm các tập tin, theo thời gian.

### So sánh giữa Centralized Versioning và Distributed Versioning

* Quản lý phiên bản tập trung (Centralized Versioning) tập trung vào việc đồng bộ hóa, theo dõi, và lưu trữ tập tin.
* Quản lý phiên bản phân tán (Distributed Versioning) tập trung vào việc chia sẻ các thay đổi. Mỗi sự thay đổi có một mã định dạng (id) duy nhất.
* Các hệ phân tán không có cấu trúc định sẵn. Bạn có thể thay đổi một kiểu SVN, hệ phân tán, với git.

[Thông tin thêm](http://git-scm.com/book/en/Getting-Started-About-Version-Control)

### Tại Sao Dùng Git?

* Có thể hoạt động offline.
* Cộng tác với nhau rất dễ dàng!
* Phân nhánh dễ dàng!
* Trộn (Merging)
* Git nhanh.
* Git linh hoạt.

## Kiến Trúc Git


### Repository

Một nhóm các tập tin, thư mục, các ghi chép trong quá khứ, commit, và heads. Tưởng tượng nó như là một cấu trúc dữ liệu mã nguồn,
với thuộc tính mà một "nhân tố" mã nguồn cho bạn quyền truy cập đến lịch sử sửa đổi, và một số thứ khác.

Một git repository bao gồm thư mục .git & tree đang làm việc.

### Thư mục .git (thành phần của một repository)

Thư mục .git chứa tất cả các cấu hình, log, nhánh, HEAD, và hơn nữa.
[Danh Sách Chi Tiết.](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### Tree Đang Làm (thành phần của một repository)

Đây cơ bản là các thư mục và tập tin trong repository của bạn. Nó thường được tham chiếu
thư mục đang làm việc của bạn

### Chỉ mục (thành phần của một thư mục .git)

Chỉ mục của là một staging area trong git. Nó đơn giản là một lớp riêng biệt với tree đang làm việc của bạn
từ Git repository. Điều này cho nhà phát triền nhiều lựa chọn hơn trong việc xem xét những gì được gửi đến Git
repository.

### Commit

Một git commit là một snapshot của một nhóm các thay đổi, hoặc các thao tác Working Tree của bạn.
Ví dụ, nếu bạn thêm 5 tập tin, và xóa 2 tập tin khác, những thay đổi này sẽ được chứa trong
một commit (hoặc snapshot). Commit này có thể được đẩy đến các repo khác, hoặc không!

### Nhánh

Nhánh thực chất là một con trỏ đến commit mới nhất mà bạn vừa thực hiện. Khi bạn commit,
con trỏ này sẽ cập nhật tự động và trỏ đến commit mới nhất.

### HEAD và head (thành phần của thư mục .git)

HEAD là một con trỏ đến branch hiện tại. Một repo chỉ có một HEAD *đang hoạt động*.
head là một con trỏ đến bất kỳ commit nào. Một repo có thể có nhiều head.

### Các Tài Nguyên Mang Tính Khái Niệm

* [Git For Computer Scientists](http://eagain.net/articles/git-for-computer-scientists/)
* [Git For Designers](http://hoth.entp.com/output/git_for_designers.html)


## Các Lệnh


### init

Tạo một repo Git rỗng. Các cài đặt, thông tin lưu trữ... của Git
được lưu ở một thư mục tên là ".git".

```bash
$ git init
```

### config

Để chỉnh tùy chọn. Bất kể là cho repo, hay cho hệ thống, hay điều chỉnh
toàn cục (global)



```bash
# In Ra & Và Gán Một Số Biến Tùy Chỉnh Cơ Bản (Toàn cục - Global)
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "MyEmail@Zoho.com"
$ git config --global user.name "My Name"
```

[Tìm hiểu thêm về git config.](http://git-scm.com/docs/git-config)

### help

Để cho bạn lối truy cập nhanh đến một chỉ dẫn cực kỳ chi tiết của từng lệnh. Hoặc chỉ để
nhắc bạn một số cú pháp.

```bash
# Xem nhanh các lệnh có sẵn
$ git help

# Xem tất các các lệnh
$ git help -a

# Lệnh help riêng biệt - tài liệu người dùng
# git help <command_here>
$ git help add
$ git help commit
$ git help init
```

### status

Để hiển thị sự khác nhau giữa tập tin index (cơ bản là repo đang làm việc) và HEAD commit
hiện tại.


```bash
# Sẽ hiển thị nhánh, các tập tin chưa track (chưa commit), các thay đổi và những khác biệt khác
$ git status

# Để xem các "tid bits" về git status
$ git help status
```

### add

Để thêm các tập vào tree/thư mục/repo hiện tại. Nếu bạn không `git add` các tập tin mới đến
tree/thư mục hiện tại, chúng sẽ không được kèm theo trong các commit!

```bash
# thêm một file vào thư mục hiện tại
$ git add HelloWorld.java

# thêm một file vào một thư mục khác
$ git add /path/to/file/HelloWorld.c

# Hỗ trợ Regular Expression!
$ git add ./*.java
```

### branch

Quản lý nhánh (branch). Bạn có thể xem, sửa, tạo, xóa các nhánh bằng cách dùng lệnh này.

```bash
# liệt kê các branch đang có và ở remote
$ git branch -a

# tạo branch mới
$ git branch myNewBranch

# xóa một branch
$ git branch -d myBranch

# đặt tên lại một branch
# git branch -m <oldname> <newname>
$ git branch -m myBranchName myNewBranchName

# chỉnh sửa diễn giải của một branch
$ git branch myBranchName --edit-description
```

### checkout

Cập nhật tất cả các file trong tree hiện tại để cho trùng khớp với phiên bản của index, hoặc tree cụ thể.

```bash
# Checkout (chuyển) một repo - mặc định là nhánh master
$ git checkout
# Checkout một nhánh cụ thể
$ git checkout branchName
# Tạo một nhánh mới và chuyển đến nó, tương tự: "git branch <name>; git checkout <name>"
$ git checkout -b newBranch
```

### clone

Nhân bản, hoặc sao chép, một repo hiện có thành một thư mục mới. Nó cũng thêm
các branch có remote-tracking cho mỗi branch trong một repo được nhân bản, mà
cho phép bạn push đến một remote branch.

```bash
# Nhân bản learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git
```

### commit

Lưu trữ nội dung hiện tại của index trong một "commit" mới. Điều này cho phép tạo ra thay đổi và một ghi chú tạo ra bởi người dùng.

```bash
# commit với một ghi chú
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"
```

### diff

Hiển thị sự khác biệt giữa một file trong thư mục hiện tại, index và commits.

```bash
# Hiển thị sự khác biệt giữa thư mục hiện tại và index
$ git diff

# Hiển thị khác biệt giữa index và commit mới nhất.
$ git diff --cached

# Hiển thị khác biệt giữa thư mục đang làm việc và commit mới nhất
$ git diff HEAD
```

### grep

Cho phép bạn tìm kiếm nhanh một repo.

Các tinh chỉnh tùy chọn:

```bash
# Cảm ơn Travis Jeffery vì những lệnh này
# Đặt số của dòng được hiển thị trong kết quả tìm kiếm grep
$ git config --global grep.lineNumber true

# Làm cho kết quả tìm kiếm dễ đọc hơn, bao gồm cả gom nhóm
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# Tìm "variableName" trong tất cả các file Java
$ git grep 'variableName' -- '*.java'

# Tìm một dòng mà có chứa "arrayListName" và, "add" hoặc "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)
```

Google để xem thêm các ví dụ
[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

Hiển thị các commit đến repo.

```bash
# Hiện tất cả các commit
$ git log

# Hiện X commit
$ git log -n 10

# Chỉ hiện các commit đã merge merge commits
$ git log --merges
```

### merge

"Trộn" các thay đổi từ commit bên ngoài vào trong nhánh hiện tại.

```bash
# Merge branch cụ thể vào branch hiện tại.
$ git merge branchName

# Luôn khởi tạo một merge commit khi trộn (merge)
$ git merge --no-ff branchName
```

### mv

Đặt lại tên hoặc di chuyển một file

```bash
# Đặt lại tên một file
$ git mv HelloWorld.c HelloNewWorld.c

# Di chuyển một file
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# Buộc đặt lại tên hoặc di chuyển
# "existingFile" đã tồn tại trong thự mục, sẽ bị ghi đè
$ git mv -f myFile existingFile
```

### pull

Pull về từ một repo và merge nó vào branch khác.

```bash
# Cập nhật repo local của bạn, bằng cách merge các thay đổi mới
# từ remote "origin" và nhánh "master".
# git pull <remote> <branch>
# git pull => hoàn toàn mặc định như => git pull origin master
$ git pull origin master

# Merge các thay đổi từ remote branch và rebase
# các commit trong branch lên trên local repo, như sau: "git pull <remote> <branch>, git rebase <branch>"
$ git pull origin master --rebase
```

### push

push và merge các thay đổi từ một branch đến một remote & branch.

```bash
# Push và merge các thay đổi từ một repo local đến một
# remote có tên là "origin" và nhánh "master".
# git push <remote> <branch>
# git push => mặc định ẩn đến => git push origin master
$ git push origin master

# Để liên kết đến một branch local với một branch remote, thêm vào cờ -u:
$ git push -u origin master
# Từ lúc này, bất cứ khi nào bạn muốn push từ cùng một nhánh local đó, sử dụng lối tắt:
$ git push 
```

### rebase (thận trọng)

Lấy tất cả các thay đổi mà đã được commit trên một nhánh, và replay (?) chúng trên một nhánh khác.
*Không rebase các commit mà bạn đã push đến một repo công khai*.

```bash
# Rebase experimentBranch lên master
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch
```

[Đọc Thêm.](http://git-scm.com/book/en/Git-Branching-Rebasing)

### reset (thận trọng)

Thiết lập lạo HEAD hiện tại đến một trạng thái cụ thể. Điều này cho phép bạn làm lại các merges,
pulls, commits, thêm, and hơn nữa. Nó là một lệnh hay nhưng cũng nguy hiểm nếu bạn không
biết mình đang làm gì.

```bash
# Thiết lập lại staging area, để trùng với commit mới nhất (để thư mục không thay đổi)
$ git reset

#  Thiết lập lại staging area, để trùng với commit mới nhất, và ghi đè lên thư mục hiện tại
$ git reset --hard

# Di chuyển nhánh hiện tại đến một commit cụ thể (để thư mục không thay đổi)
# tất cả thay đổi vẫn duy trì trong thư mục.
$ git reset 31f2bb1

# Di chuyển nhánh hiện tại lùi về một commit cụ thể
# và làm cho thư mục hiện tại trùng (xóa các thay đổi chưa được commit và tất cả các commit
# sau một commit cụ thể).
$ git reset --hard 31f2bb1
```

### rm

Ngược lại với git add, git rm xóa file từ tree đang làm việc.

```bash
# xóa HelloWorld.c
$ git rm HelloWorld.c

# Xóa file từ thư mục khác
$ git rm /pather/to/the/file/HelloWorld.c
```

## Thông tin thêm

* [tryGit - A fun interactive way to learn Git.](http://try.github.io/levels/1/challenges/1)

* [git-scm - Video Tutorials](http://git-scm.com/videos)

* [git-scm - Documentation](http://git-scm.com/docs)

* [Atlassian Git - Tutorials & Workflows](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)

* [Git - the simple guide](http://rogerdudler.github.io/git-guide/index.html)


---
language: html
filename: learnhtml-vi.html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
translators:
    - ["Robert Steed", "https://github.com/robochat"]
    - ["Xuan (Sean) Luong", "https://github.com/xuanluong"]
lang: vi-vn
---

HTML là viết tắt của HyperText Markup Language (Ngôn ngữ đánh dấu siêu văn bản).
Nó là một ngôn ngữ cho phép chúng ta viết nên những trang web.
Nó là một ngôn ngữ đánh dấu, cho phép chúng ta viết trang web bằng code để chỉ định cách thức mà văn bản và dữ liệu nên được trình bày.
Tập tin html thực chất chỉ là một tập tin văn bản đơn giản.
Đánh dấu có nghĩa là gì? Nó là một phương pháp tổ chức dữ liệu của một trang web bằng cách bao quanh dữ liệu bởi các thẻ (tags) mở và đóng.
Việc đánh dấu phục vụ mục đích cung cấp tầm quan trọng của phần văn bản mà nó bao quanh.
Cũng như các ngôn ngữ máy tính khác, HTML có nhiều phiên bản. Ở đây chúng ta nói về HTML5.

**Lưu ý :**  Bạn có thể thử nghiệm những thẻ và phần tử HTML khác nhau trong quá trình đọc bài viết bằng cách truy cập những trang web như [codepen](http://codepen.io/pen/) để có thể thấy được tác dụng của những thẻ hay phần tử HTML đó,
nhằm hiểu cách chúng hoạt động và làm quen với ngôn ngữ HTML.
Bài viết này chủ yếu bàn về cú pháp của HTML và một vài mẹo hữu dụng.


```html
<!-- Bình luận được bao quanh bởi các ký tự giống như trong ví dụ này -->

<!-- #################### Các thẻ #################### -->
   
<!-- Dưới đây là tập tin HTML ví dụ mà chúng ta sẽ phân tích. -->

<!doctype html>
	<html>
		<head>
			<title>Trang web của tôi</title>
		</head>
		<body>
			<h1>Xin chào!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">Truy cập để biết cái gì sẽ được hiển thị</a>
			<p>Đây là một văn bản.</p>
			<p>Một văn bản khác.</p>
			<ul>
				<li>Đây là một danh sách không liệt kê</li>
				<li>Đây là một danh sách không liệt kê khác</li>
				<li>Danh sách không liệt kê cuối cùng của danh sách cha</li>
			</ul>
		</body>
	</html>

<!-- Một tập tin HTML luôn bắt đầu bằng việc thể hiện cho trình duyệt rằng nó là một trang HTML -->
<!doctype html>

<!-- Sau đó nó bắt đầu với một thẻ <html> mở -->
<html>

<!-- thẻ đó sẽ được đóng vào cuối tập tin bằng một thẻ đóng </html>. -->
</html>

<!-- Không nên viết gì sau thể đóng cuối cùng này. -->

<!-- Ở bên trong (giữa thẻ đóng và mở <html></html>), ta tìm thấy: -->

<!-- Phần đầu được định nghĩa bằng <head> (và phải được đóng lại bằng </head>). -->
<!-- Phần đầu chứa một vài định nghĩa và những thông tin khác không được dùng để hiển thị; đây gọi là siêu dữ liệu (metadata) -->

<head>
	<title>Trang web của tôi</title><!-- Thẻ <title> cho trình duyệt biết dòng chữ để hiển thị trên thanh tựa đề của trình duyệt vả tên tab. -->
</head>

<!-- Sau phần <head> ta sẽ gặp thẻ <body> -->
<!-- Cho tới đây, chưa có gì được hiển thị trên cửa sổ trình duyệt.  -->
<!-- Chúng ta phải đưa nội dùng vào phần <body> để hiển thị. -->

<body>
	<h1>Xin chào!</h1> <!-- Thẻ h1 tạo ra một đề mục. -->
	<!-- Ngoài <h1> ra ta còn có những đề mục cấp thấp hơn, từ h2 đến h6 -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">Truy cập để biết cái gì sẽ được hiển thị</a> <!-- một liên kết đền một url được cung cấp bởi thuộc tính href="" -->
	<p>Đây là một văn bản.</p> <!-- Thẻ <p> cho phép đưa văn bản vào trang html. -->
	<p>Một văn bản khác.</p>
	<ul> <!-- Thẻ <ul> tạo ra danh sách không đánh s. -->
	<!-- Để có một danh sách có đánh số ta dùng thể <ol> thay vì <ul> từ đó sẽ có số thứ tự  1. cho phần tử đầu tiên, 2. cho phần tử thứ hai, v.v... -->
		<li>Đây là một danh sách không liệt kê</li>
		<li>Đây là một danh sách không liệt kê khác</li>
		<li>Danh sách không liệt kê cuối cùng của danh sách cha</li>
	</ul>
</body>

<!-- Và ta đã có một tập tim HTML. Việc tạo ra tập tin HTML có thể được thực hiện một cách đơn giản. -->

<!-- Những ta cũng có thể thêm vào những loại thẻ HTML khác. -->

<!-- Chèn vào một ảnh. -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- Nguồn của ảnh sẽ được khai báo qua thuộc tính src="" -->
<!-- Nguồn ảnh có thể là một URL hoặc đường dẫn tới một tập tin trong máy. -->

<!-- Ta cũng có thể tạo ra một table. -->

<table> <!-- Tạo bảng với thẻ <table>. -->
	<tr> <!-- <tr> dùng để tạo ra một hàng. -->
		<th>Cột một</th> <!-- <th> dùng để khai báo tên cột. -->
		<th>Cột hai</th>
	</tr>
	<tr>
		<td>hàng một, cột một</td> <!-- <td> dùng để tạo ra một ô trong table. -->
		<td>hàng một, cột hai</td>
	</tr>
	<tr>
		<td>hàng hai, cột một</td>
		<td>hàng hai, cột hai</td>
	</tr>
</table>

```

## Cách sử dụng

HTML được viết trong tập tin có phần mở rộng `.html`.

## Thông tin mở rộng

* [wikipedia](https://vi.wikipedia.org/wiki/HTML)
* [HTML tutorial](https://developer.mozilla.org/en-US/docs/Web/HTML)
* [W3School](http://www.w3schools.com/html/html_intro.asp)
---
language: json
filename: learnjson-vi.json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
  - ["Marco Scannadinari", "https://github.com/marcoms"]
  - ["himanshu", "https://github.com/himanshu81494"]
translators:
  - ["Thanh Phan", "https://github.com/thanhpd"]
lang: vi-vn
---

Do JSON là một ngôn ngữ trao đổi dữ liệu hết sức đơn giản, đây có thể sẽ là bài
đơn giản nhất của Học X trong Y phút (Learn X in Y Minutes) từ trước tới nay.

JSON ở dạng thuần túy nhất không có chú thích cho câu lệnh (comment) nào, nhưng
hầu hết các trình phân tích cú pháp (parser) đều chấp nhận chú thích theo phong
cách của ngôn ngữ C (`//`, `/* */`). Một số trình phân tích cú pháp còn chấp
nhận dấu phẩy cuối cùng (vd: một dấu phẩy sau phần tử cuối cùng của một mảng
hoặc sau thuộc tính cuối cùng của một object), nhưng những trường hợp này nên
tránh để có sự tương thích tốt hơn.

Để phục vụ cho mục đích bài học này, tất cả cú pháp JSON ở đây sẽ đều là 100% hợp lệ.
May mắn thay, chúng cũng tự trình bày cho chính mình mà không cần thêm giải thích.

Các kiểu dữ liệu được JSON hỗ trợ bao gồm: số (*numbers*), chuỗi kí tự
(*string*), toán tử đúng/sai (*boolean*), mảng (*array*), *object* và *null*.
Các trình duyệt hỗ trợ bao gồm: Mozilla Firefox phiên bản 3.5 trở lên,
Internet Explorer 8 trở lên, Google Chrome, Opera 10 trở lên, Safari 4 trở lên.
Kiểu tệp JSON có dạng ".json". Kiểu MIME (Multipurpose Internet Mail Extensions)
cho JSON là "application/json". Điểm yếu của JSON đó là thiếu các định dạng cho
kiểu dữ liệu cũng như quy chuẩn cú pháp chặt chẽ sử dụng DTD.

```json
{
  "khóa": "dữ liệu",

  "các khóa": "phải luôn được đặt trong dấu ngoặc kép",
  "số": 0,
  "chuỗi kí tự": "Xin chàø. Tất cả kí tự unicode đều được chấp nhận, sử dụng với dạng \"kí tự\"."
  "có đúng không?": true,
  "không có gì": null,

  "số rất lớn": 1.2e+100,

  "objects": {
    "chú thích": "Hầu hết các cấu trúc dữ liệu bạn sẽ dùng sẽ sử dụng object.",

    "mảng": [0, 1, 2, 3, "Mảng có thể chứa bất kì thứ gì bên trong.", 5],

    "một object khác": {
      "chú thích": "Những thứ này có thể lồng vào nhau, rất tiện."
    }
  },

  "ngớ ngẩn": [
    {
      "nguồn cung cấp kali": ["chuối"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "phong cách khác": {
    "chú thích": "kiểm tra cái này xem!"
  , "vị trí dấu phẩy": "không quan trọng - chỉ cần nó ở trước khóa tiếp theo là được"
  , "chú thích khác": "tiện phải không"
  },

  "nó rất ngắn": "Và bạn đã xong rồi đấy. Bạn đã biết tất cả những thứ mà JSON có thể cung cấp."
}
```
---
language: Objective-C
contributors:
    - ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
    - ["Yannick Loriot", "https://github.com/YannickL"]
lang: vi-vn
filename: LearnObjectiveC-vi.m
---

Objective-C là ngôn ngữ lập trình chính được sử dụng bởi Apple cho các hệ điều hành OS X, iOS và các framework tương ứng của họ, Cocoa và Cocoa Touch.
Nó là một ngôn ngữ lập trình mục đích tổng quát, hướng đối tượng có bổ sung thêm kiểu truyền thông điệp giống Smalltalk vào ngôn ngữ lập trình C.

```objective-c
// Chú thích dòng đơn bắt đầu với //

/*
Chú thích đa dòng trông như thế này.
*/

// Nhập các headers của framework Foundation với cú pháp #import
#import <Foundation/Foundation.h>
#import "MyClass.h"

// Đầu vào chương trình của bạn là một hàm gọi là
// main với một kiểu trả về kiểu integer.
int main (int argc, const char * argv[])
{
    // Tạo một autorelease pool để quản lý bộ nhớ vào chương trình
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Sử dụng hàm NSLog() để in ra các dòng lệnh vào console
    NSLog(@"Hello World!"); // Print the string "Hello World!"

    ///////////////////////////////////////
    // Kiểu & Biến (Types & Variables)
    ///////////////////////////////////////

    // Khai báo số nguyên
    int myPrimitive1  = 1;
    long myPrimitive2 = 234554664565;

    // Khai báo đối tượng
    // Đặt dấu nháy * vào trước tên biến cho khai báo đối tượng strong
    MyClass *myObject1 = nil;  // Strong
    id       myObject2 = nil;  // Weak
    // %@ là một đối tượng
    // 'miêu tả' ('desciption') là thông lệ để trình bày giá trị của các Đối tượng
    NSLog(@"%@ và %@", myObject1, [myObject2 description]); // In ra "(null) và (null)"

    // Chuỗi
    NSString *worldString = @"World";
    NSLog(@"Hello %@!", worldString); // In ra "Hello World!"

    // Ký tự literals
    NSNumber *theLetterZNumber = @'Z';
    char theLetterZ            = [theLetterZNumber charValue];
    NSLog(@"%c", theLetterZ);

    // Số nguyên literals
    NSNumber *fortyTwoNumber = @42;
    int fortyTwo             = [fortyTwoNumber intValue];
    NSLog(@"%i", fortyTwo);

    NSNumber *fortyTwoUnsignedNumber = @42U;
    unsigned int fortyTwoUnsigned    = [fortyTwoUnsignedNumber unsignedIntValue];
    NSLog(@"%u", fortyTwoUnsigned);

    NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42];
    short fortyTwoShort           = [fortyTwoShortNumber shortValue];
    NSLog(@"%hi", fortyTwoShort);

    NSNumber *fortyTwoLongNumber = @42L;
    long fortyTwoLong            = [fortyTwoLongNumber longValue];
    NSLog(@"%li", fortyTwoLong);

    // Dấu phẩy động (floating point) literals
    NSNumber *piFloatNumber = @3.141592654F;
    float piFloat           = [piFloatNumber floatValue];
    NSLog(@"%f", piFloat);

    NSNumber *piDoubleNumber = @3.1415926535;
    double piDouble                 = [piDoubleNumber doubleValue];
    NSLog(@"%f", piDouble);

    // BOOL literals
    NSNumber *yesNumber = @YES;
    NSNumber *noNumber  = @NO;

    // Đối tượng Mảng
    NSArray *anArray      = @[@1, @2, @3, @4];
    NSNumber *thirdNumber = anArray[2];
    NSLog(@"Third number = %@", thirdNumber); // In ra "Third number = 3"

    // Đối tượng Từ điển
    NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
    NSObject *valueObject     = aDictionary[@"A Key"];
    NSLog(@"Đối tượng = %@", valueObject); // In ra "Object = (null)"

    ///////////////////////////////////////
    // Toán Tử (Operators)
    ///////////////////////////////////////

    // Các toán tử cũng hoạt động giống như ngôn ngữ C
    // Ví dụ:
    2 + 5; // => 7
    4.2f + 5.1f; // => 9.3f
    3 == 2; // => 0 (NO)
    3 != 2; // => 1 (YES)
    1 && 1; // => 1 (Logical and)
    0 || 1; // => 1 (Logical or)
    ~0x0F; // => 0xF0 (bitwise negation)
    0x0F & 0xF0; // => 0x00 (bitwise AND)
    0x01 << 1; // => 0x02 (bitwise dịch trái (bởi 1))

    /////////////////////////////////////////////
    // Cấu Trúc Điều Khiển (Controls Structures)
    /////////////////////////////////////////////

    // Câu lệnh If-Else
    if (NO)
    {
        NSLog(@"I am never run");
    } else if (0)
    {
        NSLog(@"I am also never run");
    } else
    {
        NSLog(@"I print");
    }

    // Câu lệnh Switch
    switch (2)
    {
        case 0:
        {
            NSLog(@"I am never run");
        } break;
        case 1:
        {
            NSLog(@"I am also never run");
        } break;
        default:
        {
            NSLog(@"I print");
        } break;
    }

    // Câu lệnh vòng lặp While
    int ii = 0;
    while (ii < 4)
    {
        NSLog(@"%d,", ii++); // ii++ tăng dần, sau khi sử dụng giá trị của nó.
    } // => in ra  "0,"
      //           "1,"
      //           "2,"
      //           "3,"

    // Câu lệnh vòng lặp For
    int jj;
    for (jj=0; jj < 4; jj++)
    {
        NSLog(@"%d,", jj);
    } // => in ra  "0,"
      //           "1,"
      //           "2,"
      //           "3,"

    // Câu lệnh Foreach
    NSArray *values = @[@0, @1, @2, @3];
    for (NSNumber *value in values)
    {
        NSLog(@"%@,", value);
    } // => in ra  "0,"
      //           "1,"
      //           "2,"
      //           "3,"

    // Câu lệnh Try-Catch-Finally
    @try
    {
        // Your statements here
        @throw [NSException exceptionWithName:@"FileNotFoundException"
                            reason:@"Không Tìm Thấy Tập Tin trên Hệ Thống" userInfo:nil];
    } @catch (NSException * e)
    {
        NSLog(@"Exception: %@", e);
    } @finally
    {
        NSLog(@"Finally");
    } // => in ra  "Exception: Không Tìm Thấy Tập Tin trên Hệ Thống"
      //           "Finally"

    ///////////////////////////////////////
    // Đối Tượng (Objects)
    ///////////////////////////////////////

    // Tạo một thực thể đối tượng bằng cách phân vùng nhớ và khởi tạo đối tượng đó.
    // Một đối tượng sẽ không thật sự hoạt động cho đến khi cả 2 bước alloc] init] được hoàn thành
    MyClass *myObject = [[MyClass alloc] init];

    // Mô hình lập trình hướng đối tượng của Objective-C dựa trên việc truyền thông điệp (message)
    // và các thực thể đối tượng với nhau.
    // Trong Objective-C một đối tượng không đơn thuần gọi phương thức; nó truyền thông điệp.
    [myObject instanceMethodWithParameter:@"Steve Jobs"];

    // Dọn dẹp vùng nhớ mà bạn đã dùng ở chương trình
    [pool drain];

    // Kết thúc chương trình
    return 0;
}

///////////////////////////////////////
// Lớp và Hàm (Classes & Functions)
///////////////////////////////////////

// Khai báo lớp của bạn ở một tập tin header (MyClass.h):
// Cú pháp Khai Báo Lớp:
// @interface ClassName : ParentClassName <ImplementedProtocols>
// {
//    Khai báo biến thành viên;
// }
// -/+ (type) Khai báo method;
// @end
@interface MyClass : NSObject <MyProtocol>
{
    int count;
    id data;
    NSString *name;
}
// Ký hiệu (notation) tiện ích để tự động khởi tạo public getter và setter
@property int count;
@property (copy) NSString *name; // Sao chép đối tượng trong quá trình gán.
@property (readonly) id data;    // Chỉ khai báo phương thức getter.

// Phương thức
+/- (return type)methodSignature:(Parameter Type *)parameterName;

// dấu '+' cho phương thức lớp
+ (NSString *)classMethod;

// dấu '-' cho phương thức thực thể
- (NSString *)instanceMethodWithParameter:(NSString *)string;
- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number;

@end

// Thực thi các phương thức trong một tập tin thực thi (MyClass.m):

@implementation MyClass

// Gọi khi đối tượng được release
- (void)dealloc
{
}

// Phương thức khởi tạo (Constructors) là một cách để tạo các lớp
// Đây là phương thức khởi tạo mặc định được gọi khi đối tượng được khởi tạo
- (id)init
{
    if ((self = [super init]))
    {
        self.count = 1;
    }
    return self;
}

+ (NSString *)classMethod
{
    return [[self alloc] init];
}

- (NSString *)instanceMethodWithParameter:(NSString *)string
{
    return @"New string";
}

- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number
{
    return @42;
}

// Các phương thức được khai báo vào MyProtocol
- (void)myProtocolMethod
{
    // câu lệnh
}

@end

/*
 * Một protocol khai báo các phương thức mà có thể thực thi bởi bất kỳ lớp nào.
 * Các protocol chính chúng không phải là các lớp. Chúng chỉ đơn giản là định ra giao diện (interface)
 * mà các đối tượng khác có trách nhiệm sẽ thực thi.
 */
@protocol MyProtocol
    - (void)myProtocolMethod;
@end



```
## Xem Thêm

+ [Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C)

+ Apple Docs':
    + [Learning Objective-C](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/)

    + [Programming With Objective-C](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html)

    + [Object-Oriented Programming with Objective-C](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005149)

    + [Coding Guidelines for Cocoa](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html)

+ [iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started)
---
language: python3
filename: learnpython3-vi.py
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["Zachary Ferguson", "http://github.com/zfergus2"]
    - ["evuez", "http://github.com/evuez"]
translators:
    - ["Xuan (Sean) Luong, https://github.com/xuanluong"]
lang: vi-vn

---

Python được tạo ra bởi Guido van Rossum vào đầu những năm 90s. Ngày nay nó là một trong những ngôn ngữ phổ biến
nhất còn tồn tại. Tôi thích Python vì sự rõ ràng, trong sáng về mặt cú pháp. Về cơ bản, Python có thể coi
như một loại mã giả (pseudocode) có thể thực thi được.

Mọi phản hồi đều sẽ được tích cực ghi nhận! Bạn có thể liên lạc với tôi qua Twitter [@louiedinh](http://twitter.com/louiedinh) hoặc louiedinh [at] [google's email service]

Lưu ý: Bài viết này áp dụng riêng cho Python 3. Truy cập [vào đây](http://learnxinyminutes.com/docs/python/) nếu bạn muốn học phiên bản cũ Python 2.7

```python

# Dòng bình luận (comment) bắt đầu bằng dấu thăng (#)

""" Những chuỗi ký tự (string) nằm trên nhiều dòng
    có thể được viết bằng cách dùng 3 dấu nháy " và thường
    được dùng trong quá trình viết tài liệu (documentation).
"""

####################################################
## 1. Các kiểu dữ liệu cơ bản và Các phép toán
####################################################

# Bạn có những con số
3  # => 3

# Tính toán với những con số là những điều có thể bạn sẽ làm
1 + 1   # => 2
8 - 1   # => 7
10 * 2  # => 20
35 / 5  # => 7.0

# Kết quả của phép chia số nguyên sẽ được làm tròn xuống cho cả số dương và số âm
5 // 3       # => 1
5.0 // 3.0   # => 1.0 # phép chia số nguyên cũng áp dụng được cho kiểu dữ liệu float biểu diễn số thực
-5 // 3      # => -2
-5.0 // 3.0  # => -2.0

# Kết quả của phép chia luôn là số thực
10.0 / 3  # => 3.3333333333333335

# Phép toán lấy phần dư (modulo)
7 % 3  # => 1

# Phép lũy thừa (x**y, x lũy thừa y)
2**3  # => 8

# Áp đặt thứ tự tính toán bằng dấu ngoặc
(1 + 3) * 2  # => 8

# Kiểu Boolean cũng là một kiểu dữ liệu cơ bản (Lưu ý: ký tự đầu tiên viết hoa)
True
False

# Phủ định bằng từ khóa 'not'
not True   # => False
not False  # => True

# Các phép toán với kiểu Boolean
# Lưu ý từ khóa "and" và "or" là case-sensitive
True and False  # => False
False or True   # => True

# Lưu ý khi sử dụng các phép toán của kiểu Boolean với số nguyên 'int'
# False là 0 và True là 1
# Đừng nhầm lẫn các phép toán Boolean cho số nguyên và các phép toán and/or trên bit (& và |)
0 and 2     # => 0
-5 or 0     # => -5
0 == False  # => True
2 == True   # => False
1 == True   # => True
-5 != False != True #=> True

# So sánh bằng với ==
1 == 1  # => True
2 == 1  # => False

# So sánh không bằng với !=
1 != 1  # => False
2 != 1  # => True

# Các phép so sánh khác
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# Các phép so sánh có thể xâu chuỗi với nhau!
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# (is vs. ==) từ khóa is kiểm tra xem 2 biến có cùng tham chiếu một đối tượng, còn == kiếm tra
# xem hai đối tượng có cùng giá trị hay không.
a = [1, 2, 3, 4]  # a trỏ tới một danh sách (list) mới, [1, 2, 3, 4]
b = a             # b trỏ tới nơi mà a cũng đang trỏ tới
b is a            # => True, a và b cùng trỏ tới một đối tượng
b == a            # => True, đối tượng mà a và b trỏ tới có cùng giá trị
b = [1, 2, 3, 4]  # b trỏ tới một danh sách mới, [1, 2, 3, 4]
b is a            # => False, a và b không cùng trỏ tới một đối tượng
b == a            # => True, đối tượng mà a và b trỏ tới không có cùng giá trị

# Chuỗi ký tự được tạo ra bằng dấu nháy kép " hoặc nháy đơn '
"Đây là một chuỗi ký tự."
'Đây cũng là một chuỗi ký tự.'

# Chuỗi ký tự có thể được cộng với nhau can be added too! Tuy nhiên nên tránh làm như vậy
"Xin " + "chào!"  # => "Xin chào!"
# Các chuỗi ký tự không phải là biến (literals) có thể được nối với nhau mà không cần dùng phép cộng '+'
"Xin " "chào!"    # => "Xin chào!"

# Một chuỗi ký tự có thể xem như một danh sách (list) các ký tự
"Đây là một chuỗi ký tự"[0]  # => 'Đ'

# Bạn có thể tìm chiều dài một chuỗi
len("Đây là một chuỗi")  # => 16

# .format có thể được dùng để định dạng chuỗi, ví dụ như:
"{} có thể được {}".format("Chuỗi ký tự", "định dạng")  # => "Chuỗi ký tự có thể được định dạng"

# Bạn có thể lặp lại đối số (arguments) khi định dạnh để không phải gõ nhiều lần
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
# => "Jack be nimble, Jack be quick, Jack jump over the candle stick"

# Bạn có thể dùng từ khóa nếu bạn không muốn đếm
"{name} wants to eat {food}".format(name="Bob", food="lasagna")  # => "Bob wants to eat lasagna"

# Nếu code Python 3 của bạn cần phải chạy với Python 2.5 hoặc các bản cũ hơn, bạn cũng có thể
# dùng cách định dạng cũ:
"%s can be %s the %s way" % ("Strings", "interpolated", "old")  # => "Strings can be interpolated the old way"


# None là một đối tượng
None  # => None

# Đừng dùng so sánh bằng "==" để so sánh đối tượng với None
# Thay vào đó dùng is. Nó sẽ kiểm tra xem một đối tượng có đồng nhất với None hay không.
"etc" is None  # => False
None is None   # => True

# None, 0, và chuỗi/danh sách (list)/từ điển (dict)/tuple rỗng khi chuyển về kiểu Boolean đều có giá trị là False.
# Tất cả những giá trị khác đều là True
bool(0)   # => False
bool("")  # => False
bool([])  # => False
bool({})  # => False
bool(())  # => False

####################################################
## 2. Biến và Các kiểu dữ liệu gộp (Collections)
####################################################

# Hàm print trong Python
print("Tôi là Python. Rất hân hạnh được làm quen!")  # => Tôi là Python. Rất hân hạnh được làm quen!

# Hàm print mặc định in thêm ký tự xuống dòng
# Dùng đối số tùy chọn (optional argument) để thay đổi cách kết thúc chuỗi.
print("Hello, World", end="!")  # => Hello, World!

# Một cách đơn giản để lấy dữ liệu vào từ bàn phím
input_string_var = input("Nhập dữ liệu: ") # Trả về dữ liệu vào là một chuỗi
# Lưu ý: Trong những phiên bản cũ của Python input() có tên là raw_input()

# Không cần phải khai báo biến mà chỉ có gán giá trị cho biến.
# Quy ước là sử dụng chữ_viết_thường_có_dấu_gạch_dưới
some_var = 5
some_var  # => 5

# Truy cập một biến chưa được gán trước đó sẽ tạo ra biệt lệ (exception).
# Đọc mục Luồng điều khiển để hiểu thêm về việc giải quyết các biệt lệ (exception handling)
some_unknown_var  # Sinh ra một biệt lệ kiểu NameError

# if có thể dùng như một biểu thức
# Tương đương với phép toán ba ngôi trong C: '?:'
"yahoo!" if 3 > 2 else 2  # => "yahoo!"

# Kiểu danh sách (list) lưu trữ chuỗi đối tượng tuần tự
li = []
# Bạn có thể bắt đầu với một danh sách đã có sãn các phần tử
other_li = [4, 5, 6]

# Thêm phần tử vào cuối danh sách bằng phương thức append
li.append(1)    # li bây giờ là [1]
li.append(2)    # li bây giờ là [1, 2]
li.append(4)    # li bây giờ là [1, 2, 4]
li.append(3)    # li bây giờ là [1, 2, 4, 3]
# Xóa phần tử cuối cùng bằng phương thức pop
li.pop()        # => 3 and li is now [1, 2, 4]
# Sau đó ta có thể đưa đối tượng trở lại danh sách
li.append(3)    # li trở lại là [1, 2, 4, 3].

# Truy cập một danh sách như bạn làm với một mảng (array)
li[0]   # => 1
# Truy cập phần tử cuối cùng
li[-1]  # => 3

# Truy cập ngoài giới hạn sẽ tạo ra biệt lệ IndexError
li[4]  # Sinh ra một biệt lệ kiểu IndexError

# Bạn có thể truy cập một đoạn bằng phép cắt (slice).
# Chỉ mục bắt đầu được tính làm điểm bắt đầu còn chỉ mục kết thúc thì không, mà là chỉ mục của phần tử tiếp theo phần tử kết thúc
# (Về mặt toán học thì đây là một đoạn đóng/mở, hay nửa đoạn)
li[1:3]   # => [2, 4]
# Lấy từ vị trí thứ 3 đến hết
li[2:]    # => [4, 3]
# Lấy từ đầu đến vị trí thứ 3
li[:3]    # => [1, 2, 4]
# Lấy những phần tử có chỉ mục chẵn
li[::2]   # =>[1, 4]
# Trả về bản sao của danh sách bị đảo ngược
li[::-1]  # => [3, 4, 2, 1]
# Kết hợp 3 tham số để làm những phép cắt phức tạp hơn
# li[start:end:step]

# Tạo ra một bản sao sâu (deep copy) bằng phép cắt
li2 = li[:]  # => li2 = [1, 2, 4, 3] but (li2 is li) will result in false.

# Xóa phần tử nào đó của danh sách bằng "del"
del li[2]  # li is now [1, 2, 3]

# Xóa đi phần tử đầu tiên mang một giá trị nhất định
li.remove(2)  # li bây giờ là [1, 3]
li.remove(2)  # Sinh ra biệt lệ kiểu ValueError vì 2 không tồn tại trong danh sách

# Chèn một phần tử vào một vị trí cụ thể
li.insert(1, 2)  # li bây giờ lại là [1, 2, 3]

# Tìm chỉ mục của của phần tử đầu tiên mang một giá trị nhất định
li.index(2)  # => 1
li.index(4)  # Sinh ra biệt lệ a ValueError as 4 is not in the list

# Bạn có thể cộng dồn các danh sách
# Lưu ý: giá trị của li và other_li không đổi
li + other_li  # => [1, 2, 3, 4, 5, 6]

# Nối danh sách bằng "extend()"
li.extend(other_li)  # Now li is [1, 2, 3, 4, 5, 6]

# Kiểm tra sự tồn tại của một phần tử trong danh sách bằng "in"
1 in li  # => True

# Xác định độ dài bằng "len()"
len(li)  # => 6


# Tuple cũng giống như danh sách nhưng không thể thay đổi giá trị được (immutable)
tup = (1, 2, 3)
tup[0]      # => 1
tup[0] = 3  # Sinh ra biệt lệ kiểu TypeError

# Lưu ý rằng tuple có độ dài là 1 phải có dấu phẩy theo sau phần tử cuối
# nhưng tuples có độ dài khác, ngay cả tuple rỗng, thì không cần như vậy
type((1))   # => <class 'int'>
type((1,))  # => <class 'tuple'>
type(())    # => <class 'tuple'>

# Hầu hết các phép toán của danh sách đều áp dụng được cho tuples
len(tup)         # => 3
tup + (4, 5, 6)  # => (1, 2, 3, 4, 5, 6)
tup[:2]          # => (1, 2)
2 in tup         # => True

# Bạn có thể gán giá trị cho nhiều biến một lúc bằng tuple (tuple unpacking)
a, b, c = (1, 2, 3)  # a is now 1, b is now 2 and c is now 3
# Sau đây là unpacking kiểu mở rộng
a, *b, c = (1, 2, 3, 4)  # a bây giờ là 1, b là [2, 3] và c là 4
# Tuple được tự động tạo ra nếu bạn không để dấu ngoặc đơn
d, e, f = 4, 5, 6
# Hoán đổi hai biến trở nên dễ dàng
e, d = d, e  # d bây giờ là 5 và e là 4


# Kiểu dữ liệu từ điển (dictionaries) lưu trữ ánh xạ từ các khóa (keys) đến các giá trị (values)
empty_dict = {}
# Sau đây là một từ điển có sẵn phần tử
filled_dict = {"one": 1, "two": 2, "three": 3}

# Lưu ý rằng khóa của từ điển phải có kiểu dữ liệu thuộc loại immutable. Điều này để bảo đảm rằng
# khóa đó luôn được chuyển hóa thành một giá trị băm (hash value) duy nhất khi tìm kiếm trong từ điển
# Những kiểu immutable bao gồm số nguyên (int), số thực (float), chuỗi ký tự (string), hay tuple
invalid_dict = {[1,2,3]: "123"}  # => Sinh ra biệt lệ kiểu TypeError: unhashable type: 'list'
valid_dict = {(1,2,3):[1,2,3]}   # Tuy nhiên, giá trị có thể thuộc bất kỳ kiểu gì

# Truy cập giá trị của một từ khóa bằng dấu []
filled_dict["one"]  # => 1

# Tất cả khóa trong một từ điển có thể được chuyển thành một đối tượng khả lặp (iterable).
# Chúng ta cần phải gọi hàm list() để chuyển một iterable thành một danh sách.
# Chúng ta sẽ bàn về vấn đề này sau. Lưu ý - Thứ tự của khóa trong từ điển sẽ không được đảm bảo.
# Những gì bạn thấy khi chạy dòng code dưới đây có thể sẽ không hoàn toàn giống như vậy.
list(filled_dict.keys())  # => ["three", "two", "one"]


# Tất cả các giá trị có thể chuyển thành một đối tượng khả lặp bằng cách gọi hàm "values()".
# Chúng ta cũng vẫn phải gọi hàm list() nếu muốn chuyển nó thành một danh sách. Lưu ý - thứ
# tự của giá trị cũng không được đảm bảo
list(filled_dict.values())  # => [3, 2, 1]


# Sự tồn tại của khóa trong từ điển có thể kiểm tra được thông qua từ khóa "in"
"one" in filled_dict  # => True
1 in filled_dict      # => False

# Truy xuất giá trị của một khóa không tồn tại trong từ điển sẽ tạo ra biệt lệ KeyError
filled_dict["four"]  # KeyError

# Dừng phương thức "get()" để tránh tạo ra biệt lệ KeyError
filled_dict.get("one")      # => 1
filled_dict.get("four")     # => None
# Phương thức get hỗ trợ một đối số mặt định khi không thể tìm thấy giá trị ứng với từ khóa
filled_dict.get("one", 4)   # => 1
filled_dict.get("four", 4)  # => 4

# "setdefault()" chèn một giá trị ứng với khóa nếu khóa đó không có sẵn trong từ điển
filled_dict.setdefault("five", 5)  # filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] is still 5

# Thêm khóa và giá trị vào từ điển
filled_dict.update({"four":4})  # => {"one": 1, "two": 2, "three": 3, "four": 4}
filled_dict["four"] = 4         # another way to add to dict

# Xóa một khóa ra khỏi từ điển bằng từ khóa del
del filled_dict["one"]  # Removes the key "one" from filled dict

# Bắt đầu từ Python 3.5 bạn có thể unpack từ điển trong một từ điển khác
{'a': 1, **{'b': 2}}  # => {'a': 1, 'b': 2}
{'a': 1, **{'a': 2}}  # => {'a': 2}



# Kiểu tập hợp (set) lưu trữ ... tập hợp
empty_set = set()
# Khởi tạo giá trị một tập hợp với nhiều giá tri. Vâng, nhìn nó khá giống từ điển.
some_set = {1, 1, 2, 2, 3, 4}  # some_set is now {1, 2, 3, 4}

# Tương tự như khóa của từ điển, phần tử của một tập hợp cũng phải là immutable
invalid_set = {[1], 1}  # => Sinh ra biệt lệ TypeError: unhashable type: 'list'
valid_set = {(1,), 1}

# Thêm một phần tử vào tập hợp
filled_set.add(5)  # filled_set is now {1, 2, 3, 4, 5}

# Thực hiện phép giao hai tập hợp bằng phép toán &
other_set = {3, 4, 5, 6}
filled_set & other_set  # => {3, 4, 5}

# Thực hiện phép hợp bằng phép toán |
filled_set | other_set  # => {1, 2, 3, 4, 5, 6}

# Lấy hiệu của hai tập hơp bằng phép toán -
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# Lấy hiệu đối xứng bằng phép toán ^
{1, 2, 3, 4} ^ {2, 3, 5}  # => {1, 4, 5}

# Kiểm tra tập hợp bên trái là tập cha của bên phải
{1, 2} >= {1, 2, 3} # => False

# Kiểm tra xem tập hợp bên trái có phải là tập con của tập hợp bên phải
{1, 2} <= {1, 2, 3} # => True

# Kiểm tra sự tồn tại của một phần tử trong tập hợp bằng từ khóa in
2 in filled_set   # => True
10 in filled_set  # => False



####################################################
## 3. Luồng điều khiển và kiểu khả lặp
####################################################

# Đầu tiên hãy tạo ra một biến
some_var = 5

# Sau đây là một câu lệnh if. Khoảng cách lề rất quan trọng trong Python
# Quy ước chung là dùng khoảng trắng chứ không phải ký tự tab
# Chuỗi sau sẽ được in ra "some_var is smaller than 10"
if some_var > 10:
    print("some_var is totally bigger than 10.")
elif some_var < 10:    # Phần elif là tùy chọn.
    print("some_var is smaller than 10.")
else:                  # else cũng là tùy chọn.
    print("some_var is indeed 10.")


"""
Lặp qua một danh sách bằng for
in ra:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # Bạn có thể dùng format() để gán một giá trị vào giữa chuỗi (string interpolation)
    print("{} is a mammal".format(animal))

"""
"range(number)" trả về một đối tượng khả lặp kiểu số
từ 0 đến giá trị của number
in ra:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
"range(lower, upper)" trả về một đối tượng khả lặp kiểu số
từ giá trị lower đến giá trị upper
in ra:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print(i)

"""
"range(lower, upper, step)" trả về một đối tượng khả lặp kiểu số
từ giá trị lower đến giá trị upper, tăng dần theo giá trị
của step. Nếu không có giá trị của step thì mặc định là 1.
in ra:
    4
    6
"""
for i in range(4, 8, 2):
    print(i)
"""

Vòng lặp while tiếp tục lặp khi điều kiện còn được thỏa mãn
in ra:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # cách viết ngán cho x = x + 1

# Handle exceptions with a try/except block
# Đối phó với biệt lệ bằng khối lệnh try/except
try:
    # Dùng "raise" để ném ra một biệt lệ
    raise IndexError("This is an index error")
except IndexError as e:
    pass                 # pass có nghĩa là không làm gì cả. Thông thường đây là nơi để khắc phụ vấn đề làm xảy ra biệt lệ
except (TypeError, NameError):
    pass                 # Nhiều biệt lệ có thể được đối phó cùng một lúc nếu cần
else:                    # Không bắt buộc phải sử dụng else nhưng nếu dùng thì nó phải sau tất cả các khối except
    print("All good!")   # Chỉ thực thi nếu không có biệt lệ phát sinh
finally:                 # Luôn thực thi trong mọi hoàn cảnh
    print("We can clean up resources here")

# Thay vì dùng try/finally để thu hồi tài nguyên (resources) ta có thể dùng with
with open("myfile.txt") as f:
    for line in f:
        print(line)

# Python hỗ trợ kiểu dữ liệu khả lặp (iterable).
# Một đối tượng khả lặp có thể được xem như là một chuỗi các đối tượng tuần tự (sequence)
# Đối tượng trả về bởi hàm range là một khả lặp.

filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable)  # => dict_keys(['one', 'two', 'three']). Đây là một đối tượng khả lặp

# Ta có thể lặp qua đối tượng
for i in our_iterable:
    print(i)  # In ra một, hai, ba

# Tuy nhiên chúng ta không thể truy cập phần tử bằng chỉ mục
our_iterable[1]  # Sinh ra biệt lệ TypeError

# Một đối tượng khả lặp là đối tượng có thể tạo ra một iterator
our_iterator = iter(our_iterable)

# iterator là một đối tượng ghi nhớ được trạng thái trong quá trình nó được duyệt qua
# đối tượng kế tiếp có thể truy cập được bằng hàm next
next(our_iterator)  # => "one"

# Nó ghi nhớ trạng thái trong quá trình lặp
next(our_iterator)  # => "two"
next(our_iterator)  # => "three"

# Sau khi iterator đã trả về tất cả dữ liệu, nó sẽ sinh ra biệt lệ kiểu StopIteration
next(our_iterator)  # Sinh ra biệt lệ StopIteration

# Ta có thể lấy tất cả phần tử của một iterator bằng cách gọi hàm list với nó
list(filled_dict.keys())  # => Returns ["one", "two", "three"]


####################################################
## 4. Hàm
####################################################

# Dùng từ khóa def để định nghĩa hàm
def add(x, y):
    print("x is {} and y is {}".format(x, y))
    return x + y  # từ khóa return để trả về một giá trị

# Gọi một hàm với đối số
add(5, 6)  # => In ra "x is 5 and y is 6" và trả về 11

# Một cách khác để gọi hàm là dùng đối số có từ khóa (keyword arguments)
add(y=6, x=5)  # Đối số có từ khóa có thể xuất hiện với thứ tự bất kỳ

# Bạn có thể định nghĩa hàm có số lượng đối số vị trí (positional arguments) không biết trước
def varargs(*args):
    return args

varargs(1, 2, 3)  # => (1, 2, 3)

# Số lượng tham số từ khóa cũng có thể không cần biết trước
def keyword_args(**kwargs):
    return kwargs

# Thử gọi hàm để xem điều gì xảy ra
keyword_args(big="foot", loch="ness")  # => {"big": "foot", "loch": "ness"}


# Có thể định nghĩa hàm dùng cả hai loại đối số
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) in ra:
    (1, 2)
    {"a": 3, "b": 4}
"""

# Khi gọi hàm, bạn có thể làm ngược với khi định nghĩa
# Dùng dấu * để lấy giá trị từ args và ** với giá trị từ kwargs
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)            # tương đương với foo(1, 2, 3, 4)
all_the_args(**kwargs)         # tương đương với foo(a=3, b=4)
all_the_args(*args, **kwargs)  # tương đương với foo(1, 2, 3, 4, a=3, b=4)

# Trả về nhiều giá trị (gán vào một tuple)
def swap(x, y):
    return y, x  # Trả về nhiều giá trị dưới dạng một tuple mà không cần dấu ngoặc.
                 # (Lưu ý là dấu ngoặc đơn đã được bỏ đi những vẫn có thể được thêm vào)

x = 1
y = 2
x, y = swap(x, y)     # => x = 2, y = 1
# (x, y) = swap(x,y)  # dấu ngoặc đơn đã được bỏ đi những vẫn có thể được thêm vào

# Tầm vực của hàm
x = 5

def set_x(num):
    # Biến cục bộ x không đồng nhất với biến toàn cục x
    x = num    # => 43
    print(x)   # => 43

def set_global_x(num):
    global x
    print(x)   # => 5
    x = num    # biến toàn cục x được gán giá trị là 6
    print(x)   # => 6

set_x(43)
set_global_x(6)


# Hàm trong Python cũng là đối tượng
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)   # => 13

# Có những hàm không tên
(lambda x: x > 2)(3)                  # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1)  # => 5

# Có những hàm cấp cao được hỗ trọ sẵn
list(map(add_10, [1, 2, 3]))          # => [11, 12, 13]
list(map(max, [1, 2, 3], [4, 2, 1]))  # => [4, 2, 3]

list(filter(lambda x: x > 5, [3, 4, 5, 6, 7]))  # => [6, 7]

# list comprehension có thể dùng để hay thế map và filter
# list comprehension lưu giá trị xuất vào một danh sách mà bản thân nó có thể lồng trong danh sách khác
[add_10(i) for i in [1, 2, 3]]         # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]  # => [6, 7]

# Tập hơp và từ điển cũng có thể được tao ra thông qua set comprehension và dict comprehension
{x for x in 'abcddeef' if x not in 'abc'}  # => {'d', 'e', 'f'}
{x: x**2 for x in range(5)}  # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


####################################################
## 5. Mô đun
####################################################

# Bạn có thể import một mô đun
import math
print(math.sqrt(16))  # => 4.0

# Bạn có thể lấy một hàm cụ thể từ một mô đun
from math import ceil, floor
print(ceil(3.7))   # => 4.0
print(floor(3.7))  # => 3.0

# Hoặc import tất cả hàm từ một mô đun
# Cảnh báo: đây không phải là một cách hay
from math import *

# Có thể làm tên của module ngắn lại
import math as m
math.sqrt(16) == m.sqrt(16)  # => True

# Mô đun trong Python chỉ là những tập tin Python bình thường. Bạn
# có thể viết mô đun của mình và import chúng. Tên của mô đun
# cũng là tên của tập tin.

# You can find out which functions and attributes
# are defined in a module.
# Bạn có thể liệt kê những hàm và thuộc tính
# được định nghĩa trong một mô đun
import math
dir(math)

# Nếu bạn có một tập tin code Python gọi là math.py ở cùng
# thư mục với tập tin hiện tai, tập tin math.py sẽ
# được nạp vào thay vì mô đun được cung cấp sẵn (built-in) trong Python.
# Điều này xảy ra vì thư mục hiện tại có ưu tiên
# hơn những thư viện cung cấp sẵn.


####################################################
## 6. Lớp (classes)
####################################################

# Ta dùng từ khóa "class" đề định nghĩa một lớp
class Human:

    # Một thuộc tính của lớp được chia sẽ bởi tất cả đối tượng của lớp này
    species = "H. sapiens"

    # Hàm khởi tạo cơ bản sẽ được goi khi một đối tượng được tạo ra.
    # Lưu ý 2 dấu gạch dưới ở đầu và cuối ám chỉ đối tượng
    # hoặc thuộc tính dùng bở Python những tồn tại trong không gian tên
    # do người dùng kiểm soát. Phương thức (hoặc thuộc tính) như: __init__, __str__,
    # __repr__ v.v.. là những phương thức đặc biệt.
    # Bạn không nên tự đặt những tên như vậy.
    def __init__(self, name):
        # Gán đối số vào thuộc tính name của đối tượng
        self.name = name

        # Khởi tạo thuộc tính
        self._age = 0

    # Một phương thức trên đối tượng. Tất cả đều có đối số đầu tiên là "self"
    def say(self, msg):
        print ("{name}: {message}".format(name=self.name, message=msg))

    # Một phương thức trên đối tượng khác
    def sing(self):
        return 'yo... yo... microphone check... one two... one two...'

    # Một phương thức trên lớp được chia sẻ với mọi đối tượng
    # Lớp đó cũng là đối số thứ nhất của phương thức đó
    @classmethod
    def get_species(cls):
        return cls.species

    # Một phương thức tĩnh được gọi mà không có lớp hay đối tượng đi kèm
    @staticmethod
    def grunt():
        return "*grunt*"

    # Một thuộc tính chỉ giống như một hàm truy xuất.
    # Nó biến phương thức age() thành một thuộc tính chỉ đọc cùng tên.
    # Tuy nhiên trong Python không nhất thiết phải viết những hàm đọc và ghi quá đơn giản
    @property
    def age(self):
        return self._age

    # Đây là hàm để ghi giá trị cho thuộc tính
    @age.setter
    def age(self, age):
        self._age = age

    # Đây là hàm để xóa thuộc tính
    @age.deleter
    def age(self):
        del self._age


# Khi trình thông dịch Python đọc một tập tin mã nguồn, nó thực thi tất cả code trong đó.
# Kiểm tra giá trị của __name__ bảo đảm rằng đoạn mã bên dưới chỉ thực thi khi
# mô đun này là chương trình chính
if __name__ == '__main__':
    # Khởi tạo một đối tượng
    i = Human(name="Ian")
    i.say("hi")                     # "Ian: hi"
    j = Human("Joel")
    j.say("hello")                  # "Joel: hello"
    # i và j là thực thể của kiểu Human, nói cách khác: chúng là những đối tượng Human

    # Gọi những phương thức trên lớp
    i.say(i.get_species())          # "Ian: H. sapiens"
    # Thay đổi thuộc tính chung
    Human.species = "H. neanderthalensis"
    i.say(i.get_species())          # => "Ian: H. neanderthalensis"
    j.say(j.get_species())          # => "Joel: H. neanderthalensis"

    # Gọi phương thức tĩnh
    print(Human.grunt())            # => "*grunt*"
    
    # Không thể gọi phương thức tĩnh với một thực thể/đối tượng
    # bởi vì i.grunt() sẽ tự động đặt "self" (tức là đối tượng i) làm đối số thứ nhất
    print(i.grunt())                # => TypeError: grunt() takes 0 positional arguments but 1 was given
                                    
    # Thay đổi thuộc tính của đối tượng
    i.age = 42
    # Truy cập thuộc tính
    i.say(i.age)                    # => "Ian: 42"
    j.say(j.age)                    # => "Joel: 0"
    # Xóa thuộc tính
    del i.age
    # i.age                         # => dòng nãy sẽ tạo ra biệt lệ AttributeError


####################################################
## 6.1 Đa thừa kế
####################################################

# Một định nghĩa lớp khác
class Bat:

    species = 'Baty'

    def __init__(self, can_fly=True):
        self.fly = can_fly

    # Lớp này có phương thức say
    def say(self, msg):
        msg = '... ... ...'
        return msg

    # Và một phương thức khác
    def sonar(self):
        return '))) ... ((('

if __name__ == '__main__':
    b = Bat()
    print(b.say('hello'))
    print(b.fly)

# Để tận dụng việc mô đun hóa thành từng tập tin, bạn có thể đặt những lớp định nghĩa ở trên vào các tập tin riêng,
# ví dụ như human.py và bat.py

# Để import hàm từ tập tin khác dừng cấu trúc sau
# from "filename-without-extension" import "function-or-class"

# superhero.py
from human import Human
from bat import Bat

# Batman thừa kế từ lớp Human và Bat
class Batman(Human, Bat):

    # Batman có giá trị riêng cho thuộc tính trên lớp species
    species = 'Superhero'

    def __init__(self, *args, **kwargs):
	# Cách điển hình để thừa kế thuộc tính là gọi super
	# super(Batman, self).__init__(*args, **kwargs)
	# Tuy nhiên với đa thừa kế, super() sẽ chỉ gọi lớp cơ sở tiếp theo trong danh sách MRO.
	# Vì thế, ta sẽ gọi cụ thể hàm __init__ của các lớp chả.
	# Sử dụng *args và **kwargs cho phép việc truyền đối số gọn gàng hơn,
	# trong đó mỗi lớp cha sẽ chịu trách nhiệm cho những phần thuộc về nó
        Human.__init__(self, 'anonymous', *args, **kwargs)
        Bat.__init__(self, *args, can_fly=False, **kwargs)
	# ghi đè giá trị của thuộc tính name
        self.name = 'Sad Affleck'

    def sing(self):
        return 'nan nan nan nan nan batman!'


if __name__ == '__main__':
    sup = Batman()

    # Kiểm tra kiểu đối tượng
    if isinstance(sup, Human):
        print('I am human')
    if isinstance(sup, Bat):
        print('I am bat')
    if type(sup) is Batman:
        print('I am Batman')

    # Truy xuất thứ tự phương thức của các lớp cha (Method Resolution search Order), vốn được dùng bởi cả getattr() và super9)
    # Thuộc tính này động và có thể được cập nhật
    print(Batman.__mro__)       # => (<class '__main__.Batman'>, <class 'human.Human'>, <class 'bat.Bat'>, <class 'object'>)

    # Gọi phương thức của lớp cha nhưng dùng thuộc tính trên chính lớp hiện tại
    print(sup.get_species())    # => Superhero

    # Gọi phương thức được nạp chồng
    print(sup.sing())           # => nan nan nan nan nan batman!

    # Gọi phương thức của Human, bởi vì thứ tự thừa kế ảnh hưởng đến phương thức được gọi
    sup.say('I agree')          # => Sad Affleck: I agree

    # Gọi phương thức chỉ tồn tại ở lớp cha thứ 2
    print(sup.sonar())          # => ))) ... (((

    # Thuộc tính cấp lớp được thừa kế
    sup.age = 100
    print(sup.age)

    # Thuộc tính thừa kế từ lớp cha thứ 2 có giá trị mặc định đã bị ghi đè
    print('Can I fly? ' + str(sup.fly))



####################################################
## 7. Phần nâng cao
####################################################

# Generator giúp ta viết những đoạn code lười biếng (áp dụng nguyên tắc lazy evaluation)
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# Generators tiết kiệm bộ nhớ vì nó chỉ tải dữ liệu khi cần
# xử lý giá trị kế tiếp của một đối tượng khả lặp. Điều này cho phép generator thực hiện
# những thao tác mà bình thường không làm được trên những khoảng giá trị lớn
# Lưu ý: `range` thay thế `xrange` trong Python3.

for i in double_numbers(range(1, 900000000)):  # `range` là một generator.
    print(i)
    if i >= 30:
        break

# Cũng như danh sách có list comprehension, generator cũng có generator
# comprehension
values = (-x for x in [1,2,3,4,5])
for x in values:
    print(x)  # in -1 -2 -3 -4 -5 ra màn hình dòng lệnh

# Một generator cũng có thể bị ép kiểu thành danh sách
values = (-x for x in [1,2,3,4,5])
gen_to_list = list(values)
print(gen_to_list)  # => [-1, -2, -3, -4, -5]


# Decorators
# Trong ví dụ này hàm `beg` 'phủ lên' hàm `say`. Nếu say_please là True thì nó
# sẽ thay đội giá trị trả về
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Làm ơn! Tui rất nghèo :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Mua bia cho tui nhé?"
    return msg, say_please


print(say())                 # Mua bia cho tui nhé?
print(say(say_please=True))  # Mua bia cho tui nhé? Làm ơn! Tui rất nghèo :(
```

## Sẵn sàng để học nhiều hơn?

### Miễn phí trên mạng

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)
* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Course](http://www.python-course.eu/index.php)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)
* [A curated list of awesome Python frameworks, libraries and software](https://github.com/vinta/awesome-python)
* [30 Python Language Features and Tricks You May Not Know About](http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html)
* [Official Style Guide for Python](https://www.python.org/dev/peps/pep-0008/)
* [Python 3 Computer Science Circles](http://cscircles.cemc.uwaterloo.ca/)
* [Dive Into Python 3](http://www.diveintopython3.net/index.html)
* [A Crash Course in Python for Scientists](http://nbviewer.jupyter.org/gist/anonymous/5924718)
---
category: tool
tool: ruby ecosystem
contributors:
    - ["Jon Smock", "http://github.com/jonsmock"]
    - ["Rafal Chmiel", "http://github.com/rafalchmiel"]
    - ["Vinh Nguyen", "http://rubydaily.net"]
lang: vi-vn
---

Nhìn chung các lập trình viên Ruby luôn có cách để cài đặt các phiên bản
Ruby khác nhau, quản lý các gói (hoặc gems), và quản lý các thư viện.

## Trình quản lý Ruby

Một vài nền tảng phải có Ruby đã được cài đặt trước hoặc có sẵn như một gói.
Số đông lập trình viên Ruby không sử dụng cái này, hoặc nếu có, họ chỉ sử
dụng chúng để bootstrap cài đặt Ruby. Thay vào đó, các lập trình viên Ruby
có xu hướng cài đặt trình quản lý Ruby để cài đặt và chuyển đổi các phiên
bản của Ruby và môi trường Ruby cho dự án của họ.

Dưới đây là các trình quản lý môi trường Ruby nổi tiếng:

* [RVM](https://rvm.io/) - Cài đặt và chuyển đổi các phiên bản Ruby. RVM cũng
  có các khái niệm về tập các gems để quản lý môi trường dự án một
  cách tốt nhất.
* [ruby-build](https://github.com/sstephenson/ruby-build) - Chỉ cài đặt các
  phiên bản Ruby. Sử dụng cái này giúp cho việc cài đặt Ruby tốt hơn.
* [rbenv](https://github.com/sstephenson/rbenv) - Chỉ dùng để chuyển đổi các
  phiên bản Ruby. Được sử dụng đi kèm với ruby-build. Tiện ích này sẽ giúp
  cho việc dùng Ruby tốt hơn.
* [chruby](https://github.com/postmodern/chruby) - Chỉ dùng để chuyển đổi các
  phiên bản Ruby. Tương tự như rbenv. Không quan tâm làm thế nào Ruby được
  cài đặt.

## Các phiên bản Ruby

Ruby được tạo ra bởi Yukihiro "Matz" Matsumoto, người được xem như là một
[BDFL](https://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life), mặc dầu gần
đây luôn thay đổi. Kết quả là, tham chiếu của Ruby được gọi là MRI(Matz'
Reference Implementation), và khi bạn biết về một phiên bản Ruby, nó đang
được tham chiếu để phát hành một phiên bản của MRI.

Có ba phiên bản Ruby chính thức được dùng là:

* 2.0.0 - Được phát hành vào tháng 2 năm 2013. Hầu hết các thư viện lớn, và
nền tảng đều hỗ trợ 2.0.0.
* 1.9.3 - Được phát hành vào tháng 10 năm 2011. Đây là phiên bản hầu hết các
lập trình viên Ruby đang dùng. [Nhưng đã không còn hỗ trợ](
  https://www.ruby-lang.org/en/news/2015/02/23/support-for-ruby-1-9-3-has-ended
  /)
* 1.8.7 - [Ruby 1.8.7 đã không còn được sử dụng](
  http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/).

Sự thay đổi giữa phiên bản 1.8.7 đến 1.9.x lớn hơn nhiều so với thay đổi từ
1.9.3 đến 2.0.0. Ví dụ, các phiên bản 1.9 giới thiệu các bảng mã và một
byecote VM. Có các dự án vẫn đang ở 1.8.7, nhưng chúng chiếm một số lượng ít
, phần lớn cộng đồng đã chuyển sang ít nhất là 1.9.2 hoặc 1.9.3

## Các ứng dụng Ruby

Hệ sinh thái Ruby có rất nhiều ứng dụng, với mỗi thế mạnh độc đáo và khả
năng tương thích. Để rõ ràng hơn, sự khác nhau giữa các ứng dụng được viết
bằng các ngôn ngữ khác nhau, nhưng *chúng vẫn là Ruby*.
Mỗi ứng dụng có các hook đặc trưng và những tính năng đặc biệt, nhưng tất cả
đều chạy Ruby rất tốt. Ví dụ, JRuby được viết bằng Java, nhưng bạn không
cần biết Java để sử dụng.

Một số ứng dụng nổi tiếng/tương thích cao:

* [MRI](https://github.com/ruby/ruby) - Được viết bằng C, đây là ứng dụng
  tham chiếu của Ruby. Nó tương thích 100%. Tất cả các phiên bản Ruby có khả
  năng duy trì với MRI(xem [RubySpec](#rubyspec) bên dưới).
* [JRuby](http://jruby.org/) - Được viết bằng Java và Ruby, ứng dụng này khá
  nhanh. Điểm mạnh quan trọng nhất của JRuby là JVM/Java interop, tận dụng
  các công cụ, dự án và ngôn ngữ hiện có của JVM.
* [Rubinius](http://rubini.us/) - Được viết bằng ngôn ngữ chính là Ruby với
  một C++ bytecode VM. Rất nhanh. Bởi vì nó được phát triển bằng chính Ruby.

Một số ứng dụng khá nổi tiếng/tương thích:

* [Maglev](http://maglev.github.io/) - Đứng đầu Gemstone, một Smalltalk VM.
  SmallTalk có một vài tiện ích hấp dẫn, và trong dự án này đã mang nó vào
  môi trường Ruby.
* [RubyMotion](http://www.rubymotion.com/) - Mang Ruby đến việc phát triển iOS.

Một số ứng dụng tốt/tương thích:

* [Topaz](http://topazruby.com/) - Được biết bằng RPython (sử dụng Pypy),
  Topaz vẫn còn rất trẻ và chưa hoàn toàn tương thích. Nó hứa hẹn khả năng
  trở thành một ứng dụng Ruby tương thích cao.
* [IronRuby](http://ironruby.net/) - Được viết bằng C# hướng đến nền tảng .NET
  , IronRuby dường như đã dừng hoạt động kể từ khi Microsoft rút hỗ trợ.

Các ứng dụng Ruby có các phiên bản riêng của mình, nhưng chúng luôn luôn
hướng đến sự một phiên bản đặc biệt của MRI cho sự tương thích. Nhiều ứng
dụng có khả năng đến các chế độ khác nhau (ví dụ, 1.8 hoặc 1.9) để hướng đến
phiên bản MRI.

## RubySpec

Hầu hết các ứng dụng Ruby dựa vào [RubySpec](http://rubyspec.org/). Ruby không
có thông báo chính thức, nhưng cộng đồng đã viết những specs thực thi trong
Ruby để kiểm tra sự tương thích với MRI.

## RubyGems

[RubyGems](http://rubygems.org/) là một cộng đồng quản lý các gói cho Ruby.
RubyGems đi kèm với Ruby, bởi vậy không cần cài đặt riêng lẻ.

Các gói Ruby được gọi là "gems", và chúng được host bởi cộng đồng tại
RubyGems.org. Một gem chứa mã nguồn của nó và một vài mô tả, bao gồm những
thứ như phiên bản, các thư viện độc lập, các tác giả và các loại giấy phép.

## Bundler

[Bundler](http://bundler.io/) là một gem giải quyết độc lập. Nó sử dụng một
Gemfile để tìm kiếm các thư viện độc lập trong dự án, và sau đó sẽ lấy về
các thư viện của các thư viện độc lập này. Nó thực hiện cho đến khi việc
tải các thư viện hoàn tất, hoặc nó sẽ dừng nếu xuất hiện bất kỳ xung đột nào.

Bundler sẽ hiển thị lỗi nếu tìm thấy bất kỳ xung đột giữa các thư viện. Ví
dụ, nếu như gem A yêu cầu gem Z có phiên bản 3 hoặc cao hơn, nhưng gem B lại
yêu cầu gem Z phiên bản 2. Bundler sẽ thông báo cho bạn sự xung đột này.
Điều này đã rất hữu ích khi nhiều gem tham chiếu các các gem khác (trong
gem này lại tham chiếu đến các gem khác nữa), có thể hình thành một đồ thị
lớn để nói.

# Kiểm thử

Kiểm thử là một phần lớn của Ruby. Ruby mang đến một nền tảng kiểm thử theo
kiểu Unit được gọi là minitest (hoặc TestUnit for phiên bản Ruby 1.8.x).
Có nhiều thư viện kiểm thử với các mục đích khác nhau.

* [TestUnit](http://ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/
  Unit.html) - Nền tảng kiểm thử theo kiểu Unit của Ruby 1.8.
* [minitest](http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest
  /rdoc/MiniTest.html) -Nền tảng kiểm thử được xây dựng cho Ruby 1.9/2.0
* [RSpec](http://rspec.info/) - Một nền tảng kiểm thử tập trung vào sự
  hoạt động.
* [Cucumber](http://cukes.info/) - Một nền tảng kiểm thử theo kiểu BDD dưới
  định dạng Gherkin.

## Be Nice

Cộng đồng Ruby tự hào là một cộng đồng mở, đa dạng và chào đón tất cả mọi
người. Bản thân Matz là một người cực kỳ thân thiện, và các lập trình viên
Ruby rất tuyệt vời.
---
language: ruby
filename: learnruby-vi.rb
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["Tristan Hume", "http://thume.ca/"]
  - ["Nick LaMuro", "https://github.com/NickLaMuro"]
  - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
  - ["Ariel Krakowski", "http://www.learneroo.com"]
  - ["Dzianis Dashkevich", "https://github.com/dskecse"]
  - ["Levi Bostian", "https://github.com/levibostian"]
  - ["Rahil Momin", "https://github.com/iamrahil"]
  - ["Vinh Nguyen", "http://rubydaily.net"]
lang: vi-vn

---

```ruby
# Đây là một comment

=begin
Đây là một comment nhiều dòng
Không ai dùng cách này
Bạn không nên dùng
=end

# Đầu tiên và quan trọng nhất: Mọi thứ là đối tượng.

# Các con số là các đối tượng.

3.class #=> Fixnum

3.to_s #=> "3"


# Một vài bài toán số học căn bản
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32

# Số học vừa là các cú pháp  thân thiện cho việc gọi
# một hàm trên một đối tượng
1.+(3) #=> 4
10.* 5 #=> 50

# Các giá trị đặc biệt là các đối tượng
nil # Ở đây không có gì để xem
true # luôn đúng
false # luôn sai

nil.class #=> Lớp Nil
true.class #=> Lớp True
false.class #=> Lớp False

# So sánh bằng
1 == 1 #=> true
2 == 1 #=> false

# So sánh không bằng
1 != 1 #=> false
2 != 1 #=> true

# Ngoài chính false, thì nil là một giá trị khác của false

!nil   #=> true
!false #=> true
!0     #=> false

# Các loại so sánh khác
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true

# Các toán tử logic
true && false #=> false
true || false #=> true
!true #=> false


# Có các cách khác của các toán tử logic với mức thấp hơn
# Chúng được sử dụng như các cấu trúc điều khiển luồng nối các mệnh đề
# với nhau cho đến khi một trong số chúng trả về đúng hoặc sai.

# `do_something_else` chỉ được gọi nếu như hàm  `do_something` thành công.
do_something() and do_something_else()
# `log_error` chỉ được gọi nếu hàm `do_something` không thành công.
do_something() or log_error()


# Các chuỗi là các đối tượng

'I am a string'.class #=> String
"I am a string too".class #=> String

placeholder = 'use string interpolation'
"I can #{placeholder} when using double quoted strings"
#=> "I can use string interpolation when using double quoted strings"

# Nên đưa các chuỗi vào trong dấu nháy đơn
# Ngoài ra dấu nháy kép được sử dụng trong tính toán.

# Nối các chuỗi, nhưng không nối với các số.
'hello ' + 'world'  #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"

# Xuất ra ngoài màn hình
puts "I'm printing!"

# Các biến
x = 25 #=> 25
x #=> 25

# Chú ý về việc gán các giá trị được trả về vào biến.
# Điều này có nghĩa là bạn có thể gán nhiều biến.

x = y = 10 #=> 10
x #=> 10
y #=> 10

# Theo quy ước, dùng snake_case cho các tên của biến.
snake_case = true

# Dùng để mô tả tên các biến
path_to_project_root = '/good/name/'
path = '/bad/name/'

# Ký tự (là các đối tượng)
# Các ký tự là bất biến, như các biến hằng số chỉ đến các số nguyên.
# Chúng thường xuyên được sử dụng thay cho các chuỗi để chuyển đổi các giá
# trị hiệu quả.

:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# Các mảng

# Đây là một mảng
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# Các mảng có thể chứa nhiều phần tử khác nhau

[1, 'hello', false] #=> [1, "hello", false]

# Có thể truy cập các giá trị của mảng thông qua các chỉ mục
array[0] #=> 1
array[12] #=> nil

# Giống như số học, sử dụng [biến] là một cú pháp thông dụng
array.[] 0 #=> 1
array.[] 12 #=> nil

# Lấy phần tử cuối cùng
array[-1] #=> 5

# Bắt đầu từ chỉ mục và số phần tử cần lấy
array[2, 3] #=> [3, 4, 5]

# Đảo ngược một mảng
a=[1,2,3]
a.reverse! #=> [3,2,1]

# Lấy một khoảng
array[1..3] #=> [2, 3, 4]

# Thêm phần tử vào mảng bằng cách này
array << 6 #=> [1, 2, 3, 4, 5, 6]
# Hoặc cách này
array.push(6) #=> [1, 2, 3, 4, 5, 6]

# Kiểm tra phần tử có tồn tại trong mảng
array.include?(1) #=> true

# Băm là phần chính của Ruby với các cặp khoá/giá trị
# Băm được biểu thị bằng dấu ngoặc nhọn:
hash = { 'color' => 'green', 'number' => 5 }

hash.keys #=> ['color', 'number']

# Băm có thể được truy cập nhanh chóng thông qua khoá
hash['color'] #=> 'green'
hash['number'] #=> 5

# Khoá không tồn tại sẽ trả về nil
hash['nothing here'] #=> nil

# Kể từ Ruby bản 1.9, đây là một cú pháp đặc biệt, sử dụng symbol như khoá

new_hash = { defcon: 3, action: true }

new_hash.keys #=> [:defcon, :action]

# Kiểm tra khoá hoặc giá trị có tồn tại hay không
new_hash.has_key?(:defcon) #=> true
new_hash.has_value?(3) #=> true

# Mẹo: Cả Mảng và Băm đều là Enumberable
# Chúng cùng chia sẻ rất nhiều phương thức hữu ích như each, map, count...

# Cấu trúc điều khiển

if true
  'if statement'
elsif false
  'else if, optional'
else
  'else, also optional'
end

for counter in 1..5
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# TUY NHIÊN, không ai sử dụng vòng lặp  for.
# Thay vào đó, ban nên dùng phương thức "each" và truyền vào đó một khối.
# Một khối là một loạt các mã mà bạn có thể truyền
# cho một phương thức giống như each.
# Nó tương tự với lambda,  các hàm ẩn danh hoặc closures trong các ngôn ngữ
# lập trình khác.
#
# Phương thức "each" cho một khoản sẽ chạy qua từng phần tử của khoảng đó.
# Khối được truyền vào là một số đếm như là tham số.
# Gọi một method "each" với một khối sẽ trông như thế này:

(1..5).each do |counter|
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# Bạn cũng có thể bao khối trong các dấu ngoặc nhọn.
(1..5).each { |counter| puts "iteration #{counter}" }

# Các nội dung của cấu trúc dữ liệu cũng có thể được lặp bằng each.
array.each do |element|
  puts "#{element} is part of the array"
end
hash.each do |key, value|
  puts "#{key} is #{value}"
end

counter = 1
while counter <= 5 do
  puts "iteration #{counter}"
  counter += 1
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

grade = 'B'

case grade
when 'A'
  puts 'Way to go kiddo'
when 'B'
  puts 'Better luck next time'
when 'C'
  puts 'You can do better'
when 'D'
  puts 'Scraping through'
when 'F'
  puts 'You failed!'
else
  puts 'Alternative grading system, eh?'
end
#=> "Better luck next time"

# Cases cũng được dùng cho các dãy
grade = 82
case grade
when 90..100
  puts 'Hooray!'
when 80...90
  puts 'OK job'
else
  puts 'You failed!'
end
#=> "OK job"

# Xử lý ngoại lệ:
begin
  # Code ở đây có thể sẽ đưa ra một ngoại lệ.
  raise NoMemoryError, 'You ran out of memory.'
rescue NoMemoryError => exception_variable
  puts 'NoMemoryError was raised', exception_variable
rescue RuntimeError => other_exception_variable
  puts 'RuntimeError was raised now'
else
  puts 'This runs if no exceptions were thrown at all'
ensure
  puts 'This code always runs no matter what'
end

# Hàm

def double(x)
  x * 2
end

# Hàm (và tất cả các khối) được mặc định giá trị trả về ở mệnh đề cuối.
double(2) #=> 4

# Dấu ngoặc là một tuỳ chọn cho một kết quả rõ ràng.
double 3 #=> 6

double double 3 #=> 12

def sum(x, y)
  x + y
end

# Các đối số được chia cắt bởi dấu phẩy.
sum 3, 4 #=> 7

sum sum(3, 4), 5 #=> 12

# yield
# Tất cả các hàm có thể có một tham số tuỳ chọn.
# Nó có thể được gọi với từ khóa "yield".
def surround
  puts '{'
  yield
  puts '}'
end

surround { puts 'hello world' }

# {
# hello world
# }


# Bạn có thể truyền một khối đến một hàm
# Dấu "&" được đánh dấu đến một khối
def guests(&block)
  block.call 'some_argument'
end

# Bạn có thể truyền một danh sách các tham số, nó sẽ được chuyển thành mảng.
# Thông qua việc sử dụng dấu *.
def guests(*array)
  array.each { |guest| puts guest }
end

# Định nghĩ một lớp thông qua từ khoá class.
class Human

  # Một biến class. Nó được chia sẽ cho tất cả các instance của lớp này.
  @@species = 'H. sapiens'

  # Các khởi tạo căn bản
  def initialize(name, age = 0)
    # Gán đối số đến biến instance "name"
    @name = name
    # Nếu không có age, sẽ lấy giá trị mặc định trong danh sách đối số.
    @age = age
  end

  # Hàm nhập giá trị căn bản
  def name=(name)
    @name = name
  end

  # Hàm lấy giá trị căn bản
  def name
    @name
  end

  # Các hàm trên có thể được gọn lại bằng cách dùng hàm attr_accessor
  attr_accessor :name

  # Các hàm nhận/lấy cũng có thể được tạo riêng như sau:
  attr_reader :name
  attr_writer :name

  # Một hàm lớp dùng self để phân biệt với hàm instance.
  # Nó chỉ có thể được gọi trên lớp.
  def self.say(msg)
    puts msg
  end

  def species
    @@species
  end
end


# Khởi tạo một lớp
jim = Human.new('Jim Halpert')

dwight = Human.new('Dwight K. Schrute')

# Hãy gọi một cặp các hàm.
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# Gọi một hàm lớp
Human.say('Hi') #=> "Hi"

# Phạm vi của biến được định nghĩa bởi cách chúng ta đặt tên cho chúng.
# Các biến bắt đầu với dấu $ là biến toàn cục.
$var = "I'm a global var"
defined? $var #=> "global-variable"

# Các biến bắt đầu với dấu @ là biến phạm vi.
@var = "I'm an instance var"
defined? @var #=> "instance-variable"

# Các biến bắt đầu với dấu @@ có pham vi là trong một lớp.
@@var = "I'm a class var"
defined? @@var #=> "class variable"

# Các biến bắt đầu với ký tự viết hoa là biến hằng.
Var = "I'm a constant"
defined? Var #=> "constant"

# Lớp cũng là một đối tượng trong Ruby. Bởi vậy lớp có các biến instance.
# Biến lớp được chia sẽ trong lớp và các lớp kế thừa nó.

# Lớp cơ sở
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

# Lớp kế thừa
class Worker < Human
end

Human.foo # 0
Worker.foo # 0

Human.foo = 2 # 2
Worker.foo # 2

# Các biến lớp instance không được chia sẽ trong lớp kế thừa.

class Human
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doctor < Human
end

Human.bar # 0
Doctor.bar # nil

module ModuleExample
  def foo
    'foo'
  end
end

# Include một module sẽ đưa các hàm của module thành instances của lớp.
# Extend một module sẽ đưa các hàm của module thành các biến của lớp.

class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => NoMethodError: undefined method `foo'

# Hàm hồi quy được thực hiện khi include và extend một module.

module ConcernExample
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class Something
  include ConcernExample
end

Something.bar     # => 'bar'
Something.qux     # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
```

## Các nguồn tham khảo thêm.

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges.
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.
---
category: tool
tool: vim
contributors:
    - ["RadhikaG", "https://github.com/RadhikaG"]
filename: LearnVim.txt
---


[Vim](http://www.vim.org)
(Vi IMproved) is a clone of the popular vi editor for Unix. It is a text 
editor designed for speed and increased productivity, and is ubiquitous in most 
unix-based systems. It has numerous keybindings for speedy navigation to 
specific points in the file, and for fast editing.

## Basics of navigating Vim

```
    vim <filename>   # Open <filename> in vim
    :q               # Quit vim
    :w               # Save current file
    :wq              # Save file and quit vim
    :q!              # Quit vim without saving file
                     # ! *forces* :q to execute, hence quiting vim without saving
    :x               # Save file and quit vim, shorter version of :wq

    u                # Undo
    CTRL+R           # Redo

    h                # Move left one character
    j                # Move down one line
    k                # Move up one line
    l                # Move right one character

    # Moving within the line

    0                # Move to beginning of line
    $                # Move to end of line
    ^                # Move to first non-blank character in line

    # Searching in the text

    /word            # Highlights all occurrences of word after cursor
    ?word            # Highlights all occurrences of word before cursor
    n                # Moves cursor to next occurrence of word after search
    N                # Moves cursor to previous occerence of word

    :%s/foo/bar/g    # Change 'foo' to 'bar' on every line in the file
    :s/foo/bar/g     # Change 'foo' to 'bar' on the current line

    # Jumping to characters

    f<character>     # Jump forward and land on <character>
    t<character>     # Jump forward and land right before <character> 

    # For example,    
    f<               # Jump forward and land on <
    t<               # Jump forward and land right before <
    
    # Moving by word

    w                # Move forward by one word
    b                # Move back by one word
    e                # Move to end of current word

    # Other characters for moving around

    gg               # Go to the top of the file
    G                # Go to the bottom of the file
    :NUM             # Go to line number NUM (NUM is any number)
    H                # Move to the top of the screen
    M                # Move to the middle of the screen
    L                # Move to the bottom of the screen
```

## Modes:

Vim is based on the concept on **modes**.

Command Mode - vim starts up in this mode, used to navigate and write commands 
Insert Mode  - used to make changes in your file 
Visual Mode  - used to highlight text and do operations to them 
Ex Mode      - used to drop down to the bottom with the ':' prompt to enter commands

```
    i                # Puts vim into insert mode, before the cursor position
    a                # Puts vim into insert mode, after the cursor position
    v                # Puts vim into visual mode    
    :                # Puts vim into ex mode
    <esc>            # 'Escapes' from whichever mode you're in, into Command mode

    # Copying and pasting text

    y                # Yank whatever is selected
    yy               # Yank the current line
    d                # Delete whatever is selected
    dd               # Delete the current line
    p                # Paste the copied text after the current cursor position
    P                # Paste the copied text before the current cursor position
    x                # Deleting character under current cursor position
```

## The 'Grammar' of vim

Vim can be thought of as a set of commands in a 
'Verb-Modifier-Noun' format, where:

Verb     - your action 
Modifier - how you're doing your action 
Noun     - the object on which your action acts on

A few important examples of 'Verbs', 'Modifiers', and 'Nouns':

```
    # 'Verbs'
 
    d                # Delete
    c                # Change
    y                # Yank (copy)
    v                # Visually select

    # 'Modifiers'

    i                # Inside
    a                # Around
    NUM              # Number (NUM is any number)
    f                # Searches for something and lands on it
    t                # Searches for something and stops before it
    /                # Finds a string from cursor onwards
    ?                # Finds a string before cursor

    # 'Nouns'

    w                # Word
    s                # Sentence
    p                # Paragraph
    b                # Block
    
    # Sample 'sentences' or commands

    d2w              # Delete 2 words
    cis              # Change inside sentence
    yip              # Yank inside paragraph (copy the para you're in)
    ct<              # Change to open bracket
                     # Change the text from where you are to the next open bracket
    d$               # Delete till end of line
```

## Some shortcuts and tricks

        <!--TODO: Add more!-->
```
    >                # Indent selection by one block
    <                # Dedent selection by one block
    :earlier 15m     # Reverts the document back to how it was 15 minutes ago
    :later 15m       # Reverse above command
    ddp              # Swap position of consecutive lines, dd then p
    .                # Repeat previous action
    :w !sudo tee %   # Save the current file as root
```

## Macros

Macros are basically recordable actions.
When you start recording a macro, it records **every** action and command
you use, until you stop recording. On invoking a macro, it applies the exact
same sequence of actions and commands again on the text selection.

```
    qa               # Start recording a macro named 'a'
    q                # Stop recording
    @a               # Play back the macro
```

### Configuring ~/.vimrc

The .vimrc file can be used to configure Vim on startup.

Here's a sample ~/.vimrc file:

```
" Example ~/.vimrc
" 2015.10 

" Required for vim to be iMproved
set nocompatible

" Determines filetype from name to allow intelligent auto-indenting, etc.
filetype indent plugin on

" Enable syntax highlighting
syntax on

" Better command-line completion
set wildmenu

" Use case insensitive search except when using capital letters
set ignorecase
set smartcase

" When opening a new line and no file-specific indenting is enabled,
" keep same indent as the line you're currently on
set autoindent

" Display line numbers on the left
set number

" Indentation options, change according to personal preference

" Number of visual spaces per TAB
set tabstop=4

" Number of spaces in TAB when editing
set softtabstop=4

" Number of spaces indented when reindent operations (>> and <<) are used
set shiftwidth=4

" Convert TABs to spaces
set expandtab

" Enable intelligent tabbing and spacing for indentation and alignment
set smarttab
```

### References

[Vim | Home](http://www.vim.org/index.php)

`$ vimtutor`

[A vim Tutorial and Primer](https://danielmiessler.com/study/vim/)

[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about)

[Arch Linux Wiki](https://wiki.archlinux.org/index.php/Vim)
---
language: Visual Basic
contributors:
    - ["Brian Martin", "http://brianmartin.biz"]
filename: learnvisualbasic.vb
---

```vb
Module Module1

    Sub Main()
        'A Quick Overview of Visual Basic Console Applications before we dive
        'in to the deep end.
        'Apostrophe starts comments.
        'To Navigate this tutorial within the Visual Basic Compiler, I've put
        'together a navigation system.
        'This navigation system is explained however as we go deeper into this
        'tutorial, you'll understand what it all means.
        Console.Title = ("Learn X in Y Minutes")
        Console.WriteLine("NAVIGATION") 'Display
        Console.WriteLine("")
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine("1. Hello World Output")
        Console.WriteLine("2. Hello World Input")
        Console.WriteLine("3. Calculating Whole Numbers")
        Console.WriteLine("4. Calculating Decimal Numbers")
        Console.WriteLine("5. Working Calculator")
        Console.WriteLine("6. Using Do While Loops")
        Console.WriteLine("7. Using For While Loops")
        Console.WriteLine("8. Conditional Statements")
        Console.WriteLine("9. Select A Drink")
        Console.WriteLine("50. About")
        Console.WriteLine("Please Choose A Number From The Above List")
        Dim selection As String = Console.ReadLine
        'The "Case" in the Select statement is optional.
        'For example, "Select selection" instead of "Select Case selection"
        'will also work.
        Select Case selection
            Case "1" 'HelloWorld Output
                Console.Clear() 'Clears the application and opens the private sub
                HelloWorldOutput() 'Name Private Sub, Opens Private Sub
            Case "2" 'Hello Input
                Console.Clear()
                HelloWorldInput()
            Case "3" 'Calculating Whole Numbers
                Console.Clear()
                CalculatingWholeNumbers()
            Case "4" 'Calculating Decimal Numbers
                Console.Clear()
                CalculatingDecimalNumbers()
            Case "5" 'Working Calculator
                Console.Clear()
                WorkingCalculator()
            Case "6" 'Using Do While Loops
                Console.Clear()
                UsingDoWhileLoops()
            Case "7" 'Using For While Loops
                Console.Clear()
                UsingForLoops()
            Case "8" 'Conditional Statements
                Console.Clear()
                ConditionalStatement()
            Case "9" 'If/Else Statement
                Console.Clear()
                IfElseStatement() 'Select a drink
            Case "50" 'About msg box
                Console.Clear()
                Console.Title = ("Learn X in Y Minutes :: About")
                MsgBox("This tutorial is by Brian Martin (@BrianMartinn")
                Console.Clear()
                Main()
                Console.ReadLine()

        End Select
    End Sub

    'One - I'm using numbers to help with the above navigation when I come back
    'later to build it.

    'We use private subs to separate different sections of the program.
    Private Sub HelloWorldOutput()
        'Title of Console Application
        Console.Title = "Hello World Output | Learn X in Y Minutes"
        'Use Console.Write("") or Console.WriteLine("") to print outputs.
        'Followed by Console.Read() alternatively Console.Readline()
        'Console.ReadLine() prints the output to the console.
        Console.WriteLine("Hello World")
        Console.ReadLine()
    End Sub

    'Two
    Private Sub HelloWorldInput()
        Console.Title = "Hello World YourName | Learn X in Y Minutes"
        'Variables
        'Data entered by a user needs to be stored.
        'Variables also start with a Dim and end with an As VariableType.

        'In this tutorial, we want to know what your name, and make the program
        'respond to what is said.
        Dim username As String
        'We use string as string is a text based variable.
        Console.WriteLine("Hello, What is your name? ") 'Ask the user their name.
        username = Console.ReadLine() 'Stores the users name.
        Console.WriteLine("Hello " + username) 'Output is Hello 'Their name'
        Console.ReadLine() 'Outputs the above.
        'The above will ask you a question followed by printing your answer.
        'Other variables include Integer and we use Integer for whole numbers.
    End Sub

    'Three
    Private Sub CalculatingWholeNumbers()
        Console.Title = "Calculating Whole Numbers | Learn X in Y Minutes"
        Console.Write("First number: ") 'Enter a whole number, 1, 2, 50, 104, etc
        Dim a As Integer = Console.ReadLine()
        Console.Write("Second number: ") 'Enter second whole number.
        Dim b As Integer = Console.ReadLine()
        Dim c As Integer = a + b
        Console.WriteLine(c)
        Console.ReadLine()
        'The above is a simple calculator
    End Sub

    'Four
    Private Sub CalculatingDecimalNumbers()
        Console.Title = "Calculating with Double | Learn X in Y Minutes"
        'Of course we would like to be able to add up decimals.
        'Therefore we could change the above from Integer to Double.

        'Enter a floating-point number, 1.2, 2.4, 50.1, 104.9, etc
        Console.Write("First number: ")
        Dim a As Double = Console.ReadLine
        Console.Write("Second number: ") 'Enter second floating-point number.
        Dim b As Double = Console.ReadLine
        Dim c As Double = a + b
        Console.WriteLine(c)
        Console.ReadLine()
        'Therefore the above program can add up 1.1 - 2.2
    End Sub

    'Five
    Private Sub WorkingCalculator()
        Console.Title = "The Working Calculator| Learn X in Y Minutes"
        'However if you'd like the calculator to subtract, divide, multiple and
        'add up.
        'Copy and paste the above again.
        Console.Write("First number: ")
        Dim a As Double = Console.ReadLine
        Console.Write("Second number: ") 'Enter second floating-point number.
        Dim b As Double = Console.ReadLine
        Dim c As Double = a + b
        Dim d As Double = a * b
        Dim e As Double = a - b
        Dim f As Double = a / b

        'By adding the below lines we are able to calculate the subtract,
        'multiply as well as divide the a and b values
        Console.Write(a.ToString() + " + " + b.ToString())
        'We want to pad the answers to the left by 3 spaces.
        Console.WriteLine(" = " + c.ToString.PadLeft(3))
        Console.Write(a.ToString() + " * " + b.ToString())
        Console.WriteLine(" = " + d.ToString.PadLeft(3))
        Console.Write(a.ToString() + " - " + b.ToString())
        Console.WriteLine(" = " + e.ToString.PadLeft(3))
        Console.Write(a.ToString() + " / " + b.ToString())
        Console.WriteLine(" = " + f.ToString.PadLeft(3))
        Console.ReadLine()

    End Sub

    'Six
    Private Sub UsingDoWhileLoops()
        'Just as the previous private sub
        'This Time We Ask If The User Wishes To Continue (Yes or No?)
        'We're using Do While Loop as we're unsure if the user wants to use the
        'program more than once.
        Console.Title = "UsingDoWhileLoops | Learn X in Y Minutes"
        Dim answer As String 'We use the variable "String" as the answer is text
        Do 'We start the program with
            Console.Write("First number: ")
            Dim a As Double = Console.ReadLine
            Console.Write("Second number: ")
            Dim b As Double = Console.ReadLine
            Dim c As Double = a + b
            Dim d As Double = a * b
            Dim e As Double = a - b
            Dim f As Double = a / b

            Console.Write(a.ToString() + " + " + b.ToString())
            Console.WriteLine(" = " + c.ToString.PadLeft(3))
            Console.Write(a.ToString() + " * " + b.ToString())
            Console.WriteLine(" = " + d.ToString.PadLeft(3))
            Console.Write(a.ToString() + " - " + b.ToString())
            Console.WriteLine(" = " + e.ToString.PadLeft(3))
            Console.Write(a.ToString() + " / " + b.ToString())
            Console.WriteLine(" = " + f.ToString.PadLeft(3))
            Console.ReadLine()
            'Ask the question, does the user wish to continue? Unfortunately it
            'is case sensitive.
            Console.Write("Would you like to continue? (yes / no) ")
            'The program grabs the variable and prints and starts again.
            answer = Console.ReadLine
            'The command for the variable to work would be in this case "yes"
        Loop While answer = "yes"

    End Sub

    'Seven
    Private Sub UsingForLoops()
        'Sometimes the program only needs to run once.
        'In this program we'll be counting down from 10.

        Console.Title = "Using For Loops | Learn X in Y Minutes"
        'Declare Variable and what number it should count down in Step -1,
        'Step -2, Step -3, etc.
        For i As Integer = 10 To 0 Step -1
            Console.WriteLine(i.ToString) 'Print the value of the counter
        Next i 'Calculate new value
        Console.WriteLine("Start") 'Lets start the program baby!!
        Console.ReadLine() 'POW!! - Perhaps I got a little excited then :)
    End Sub

    'Eight
    Private Sub ConditionalStatement()
        Console.Title = "Conditional Statements | Learn X in Y Minutes"
        Dim userName As String
        Console.WriteLine("Hello, What is your name? ") 'Ask the user their name.
        userName = Console.ReadLine() 'Stores the users name.
        If userName = "Adam" Then
            Console.WriteLine("Hello Adam")
            Console.WriteLine("Thanks for creating this useful site")
            Console.ReadLine()
        Else
            Console.WriteLine("Hello " + userName)
            Console.WriteLine("Have you checked out www.learnxinyminutes.com")
            Console.ReadLine() 'Ends and prints the above statement.
        End If
    End Sub

    'Nine
    Private Sub IfElseStatement()
        Console.Title = "If / Else Statement | Learn X in Y Minutes"
        'Sometimes it is important to consider more than two alternatives.
        'Sometimes there are a good few others.
        'When this is the case, more than one if statement would be required.
        'An if statement is great for vending machines. Where the user enters a code.
        'A1, A2, A3, etc to select an item.
        'All choices can be combined into a single if block.

        Dim selection As String 'Declare a variable for selection
        Console.WriteLine("Please select a product form our lovely vending machine.")
        Console.WriteLine("A1. for 7Up")
        Console.WriteLine("A2. for Fanta")
        Console.WriteLine("A3. for Dr. Pepper")
        Console.WriteLine("A4. for Diet Coke")

        selection = Console.ReadLine() 'Store a selection from the user
        If selection = "A1" Then
            Console.WriteLine("7up")
        ElseIf selection = "A2" Then
            Console.WriteLine("fanta")
        ElseIf selection = "A3" Then
            Console.WriteLine("dr. pepper")
        ElseIf selection = "A4" Then
            Console.WriteLine("diet coke")
        Else
            Console.WriteLine("Sorry, I don't have any " + selection)
        End If
        Console.ReadLine()

    End Sub

End Module

```
---
language: whip
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Saurabh Sandav", "http://github.com/SaurabhSandav"]
author: Tenor Biel
author_url: http://github.com/L8D
filename: whip.lisp
---

Whip is a LISP-dialect made for scripting and simplified concepts.
It has also borrowed a lot of functions and syntax from Haskell (a non-related language).

These docs were written by the creator of the language himself. So is this line.

```scheme
; Comments are like LISP. Semi-colons...

; Majority of first-level statements are inside "forms"
; which are just things inside parens separated by whitespace
not_in_form
(in_form)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 1. Numbers, Strings, and Operators

; Whip has one number type (which is a 64-bit IEEE 754 double, from JavaScript).
3 ; => 3
1.5 ; => 1.5

; Functions are called if they are the first element in a form
(called_function args)

; Majority of operations are done with functions
; All the basic arithmetic is pretty straight forward
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2
; even modulo
(% 9 4) ; => 1
; JavaScript-style uneven division.
(/ 5 2) ; => 2.5

; Nesting forms works as you expect.
(* 2 (+ 1 3)) ; => 8

; There's a boolean type.
true
false

; Strings are created with ".
"Hello, world"

; Single chars are created with '.
'a'

; Negation uses the 'not' function.
(not true) ; => false
(not false) ; => true

; But the majority of non-haskell functions have shortcuts
; not's shortcut is a '!'.
(! (! true)) ; => true

; Equality is `equal` or `=`.
(= 1 1) ; => true
(equal 2 1) ; => false

; For example, inequality would be combining the not and equal functions.
(! (= 2 1)) ; => true

; More comparisons
(< 1 10) ; => true
(> 1 10) ; => false
; and their word counterpart.
(lesser 1 10) ; => true
(greater 1 10) ; => false

; Strings can be concatenated with +.
(+ "Hello " "world!") ; => "Hello world!"

; You can use JavaScript's comparative abilities.
(< 'a' 'b') ; => true
; ...and type coercion
(= '5' 5)

; The `at` or @ function will access characters in strings, starting at 0.
(at 0 'a') ; => 'a'
(@ 3 "foobar") ; => 'b'

; There is also the `null` and `undefined` variables.
null ; used to indicate a deliberate non-value
undefined ; user to indicate a value that hasn't been set

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 2. Variables, Lists, and Dicts

; Variables are declared with the `def` or `let` functions.
; Variables that haven't been set will be `undefined`.
(def some_var 5)
; `def` will keep the variable in the global context.
; `let` will only have the variable inside its context, and has a weirder syntax.
(let ((a_var 5)) (+ a_var 5)) ; => 10
(+ a_var 5) ; = undefined + 5 => undefined

; Lists are arrays of values of any type.
; They basically are just forms without functions at the beginning.
(1 2 3) ; => [1, 2, 3] (JavaScript syntax)

; Dictionaries are Whip's equivalent to JavaScript 'objects' or Python 'dicts'
; or Ruby 'hashes': an unordered collection of key-value pairs.
{"key1" "value1" "key2" 2 3 3}

; Keys are just values, either identifier, number, or string.
(def my_dict {my_key "my_value" "my other key" 4})
; But in Whip, dictionaries get parsed like: value, whitespace, value;
; with more whitespace between each. So that means
{"key" "value"
"another key"
1234
}
; is evaluated to the same as
{"key" "value" "another key" 1234}

; Dictionary definitions can be accessed used the `at` function
; (like strings and lists.)
(@ "my other key" my_dict) ; => 4

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 3. Logic and Control sequences

; The `if` function is pretty simple, though different than most imperative langs.
(if true "returned if first arg is true" "returned if first arg is false")
; => "returned if first arg is true"

; And for the sake of ternary operator legacy
; `?` is if's unused shortcut.
(? false true false) ; => false

; `both` is a logical 'and' statement, and `either` is a logical 'or'.
(both true true) ; => true
(both true false) ; => false
(either true false) ; => true
(either false false) ; => false
; And their shortcuts are
; & => both
; ^ => either
(& true true) ; => true
(^ false true) ; => true

;;;;;;;;;
; Lambdas

; Lambdas in Whip are declared with the `lambda` or `->` function.
; And functions are really just lambdas with names.
(def my_function (-> (x y) (+ (+ x y) 10)))
;         |       |    |          |
;         |       |    |    returned value(with scope containing argument vars)
;         |       | arguments
;         | lambda declaration function
;         |
;   name of the to-be-declared lambda

(my_function 10 10) ; = (+ (+ 10 10) 10) => 30

; Obviously, all lambdas by definition are anonymous and
; technically always used anonymously. Redundancy.
((lambda (x) x) 10) ; => 10

;;;;;;;;;;;;;;;;
; Comprehensions

; `range` or `..` generates a list of numbers for
; each number between its two args.
(range 1 5) ; => (1 2 3 4 5)
(.. 0 2)    ; => (0 1 2)

; `map` applies its first arg (which should be a lambda/function)
; to each item in the following arg (which should be a list)
(map (-> (x) (+ x 1)) (1 2 3)) ; => (2 3 4)

; Reduce
(reduce + (.. 1 5))
; equivalent to
((+ (+ (+ 1 2) 3) 4) 5)

; Note: map and reduce don't have shortcuts

; `slice` or `\` is just like JavaScript's .slice()
; But do note, it takes the list as the first argument, not the last.
(slice (.. 1 5) 2) ; => (3 4 5)
(\ (.. 0 100) -5) ; => (96 97 98 99 100)

; `append` or `<<` is self explanatory
(append 4 (1 2 3)) ; => (1 2 3 4)
(<< "bar" ("foo")) ; => ("foo" "bar")

; Length is self explanatory.
(length (1 2 3)) ; => 3
(_ "foobar") ; => 6

;;;;;;;;;;;;;;;
; Haskell fluff

; First item in list
(head (1 2 3)) ; => 1
; List from second to last elements in list
(tail (1 2 3)) ; => (2 3)
; Last item in list
(last (1 2 3)) ; => 3
; Reverse of `tail`
(init (1 2 3)) ; => (1 2)
; List from first to specified elements in list
(take 1 (1 2 3 4)) ; (1 2)
; Reverse of `take`
(drop 1 (1 2 3 4)) ; (3 4)
; Lowest value in list
(min (1 2 3 4)) ; 1
; Highest value in list
(max (1 2 3 4)) ; 4
; If value is in list or object
(elem 1 (1 2 3)) ; true
(elem "foo" {"foo" "bar"}) ; true
(elem "bar" {"foo" "bar"}) ; false
; Reverse list order
(reverse (1 2 3 4)) ; => (4 3 2 1)
; If value is even or odd
(even 1) ; => false
(odd 1) ; => true
; Split string into list of strings by whitespace
(words "foobar nachos cheese") ; => ("foobar" "nachos" "cheese")
; Join list of strings together.
(unwords ("foo" "bar")) ; => "foobar"
(pred 21) ; => 20
(succ 20) ; => 21
```

For more info, check out the [repo](http://github.com/L8D/whip)
---
language: wolfram
contributors:
    - ["hyphz", "http://github.com/hyphz/"]
filename: learnwolfram.nb
---

The Wolfram Language is the underlying language originally used in Mathematica, but now available for use in multiple contexts.

Wolfram Language has several interfaces:
* The command line kernel interface on Raspberry Pi (just called _The Wolfram Language_) which runs interactively and can't produce graphical input.
* _Mathematica_ which is a rich text/maths editor with interactive Wolfram built in: pressing shift+Return on a "code cell" creates an output cell with the result, which is not dynamic
* _Wolfram Workbench_ which is Eclipse interfaced to the Wolfram Language backend

The code in this example can be typed in to any interface and edited with Wolfram Workbench. Loading directly into Mathematica may be awkward because the file contains no cell formatting information (which would make the file a huge mess to read as text) - it can be viewed/edited but may require some setting up.

```
(* This is a comment *)

(* In Mathematica instead of using these comments you can create a text cell
   and annotate your code with nicely typeset text and images *)

(* Typing an expression returns the result *)
2*2              (* 4 *)
5+8              (* 13 *)

(* Function Call *)
(* Note, function names (and everything else) are case sensitive *)
Sin[Pi/2]        (* 1 *)

(* Alternate Syntaxes for Function Call with one parameter *)
Sin@(Pi/2)       (* 1 *)
(Pi/2) // Sin    (* 1 *)

(* Every syntax in WL has some equivalent as a function call *)
Times[2, 2]      (* 4 *)
Plus[5, 8]       (* 13 *)

(* Using a variable for the first time defines it and makes it global *)
x = 5            (* 5 *)
x == 5           (* True, C-style assignment and equality testing *)
x                (* 5 *)
x = x + 5        (* 10 *)
x                (* 10 *)
Set[x, 20]       (* I wasn't kidding when I said EVERYTHING has a function equivalent *)
x                (* 20 *)

(* Because WL is based on a computer algebra system, *)
(* using undefined variables is fine, they just obstruct evaluation *)
cow + 5          (* 5 + cow, cow is undefined so can't evaluate further *)
cow + 5 + 10     (* 15 + cow, it'll evaluate what it can *)
%                (* 15 + cow, % fetches the last return *)
% - cow          (* 15, undefined variable cow cancelled out *)
moo = cow + 5    (* Beware, moo now holds an expression, not a number! *)

(* Defining a function *)
Double[x_] := x * 2    (* Note := to prevent immediate evaluation of the RHS
                          And _ after x to indicate no pattern matching constraints *)
Double[10]             (* 20 *)
Double[Sin[Pi/2]]      (* 2 *)
Double @ Sin @ (Pi/2)  (* 2, @-syntax avoids queues of close brackets *)
(Pi/2) // Sin // Double(* 2, //-syntax lists functions in execution order *)

(* For imperative-style programming use ; to separate statements *)
(* Discards any output from LHS and runs RHS *)
MyFirst[] := (Print@"Hello"; Print@"World")  (* Note outer parens are critical
                                                ;'s precedence is lower than := *)
MyFirst[]                                    (* Hello World *)

(* C-Style For Loop *)
PrintTo[x_] := For[y=0, y<x, y++, (Print[y])]  (* Start, test, incr, body *)
PrintTo[5]                                     (* 0 1 2 3 4 *)

(* While Loop *)
x = 0; While[x < 2, (Print@x; x++)]     (* While loop with test and body *)

(* If and conditionals *)
x = 8; If[x==8, Print@"Yes", Print@"No"]   (* Condition, true case, else case *)
Switch[x, 2, Print@"Two", 8, Print@"Yes"]  (* Value match style switch *)
Which[x==2, Print@"No", x==8, Print@"Yes"] (* Elif style switch *)

(* Variables other than parameters are global by default, even inside functions *)
y = 10             (* 10, global variable y *)
PrintTo[5]         (* 0 1 2 3 4 *)
y                  (* 5, global y clobbered by loop counter inside PrintTo *)
x = 20             (* 20, global variable x *)
PrintTo[5]         (* 0 1 2 3 4 *)
x                  (* 20, x in PrintTo is a parameter and automatically local *)

(* Local variables are declared using the Module metafunction *)
(* Version with local variable *)
BetterPrintTo[x_] := Module[{y}, (For[y=0, y<x, y++, (Print@y)])]
y = 20             (* Global variable y *)
BetterPrintTo[5]   (* 0 1 2 3 4 *)
y                  (* 20, that's better *)

(* Module actually lets us declare any scope we like *)
Module[{count}, count=0;        (* Declare scope of this variable count *)
  (IncCount[] := ++count);      (* These functions are inside that scope *)
  (DecCount[] := --count)]
count              (* count - global variable count is not defined *)
IncCount[]         (* 1, using the count variable inside the scope *)
IncCount[]         (* 2, incCount updates it *)
DecCount[]         (* 1, so does decCount *)
count              (* count - still no global variable by that name *)

(* Lists *)
myList = {1, 2, 3, 4}     (* {1, 2, 3, 4} *)
myList[[1]]               (* 1 - note list indexes start at 1, not 0 *)
Map[Double, myList]       (* {2, 4, 6, 8} - functional style list map function *)
Double /@ myList          (* {2, 4, 6, 8} - Abbreviated syntax for above *)
Scan[Print, myList]       (* 1 2 3 4 - imperative style loop over list *)
Fold[Plus, 0, myList]     (* 10 (0+1+2+3+4) *)
FoldList[Plus, 0, myList] (* {0, 1, 3, 6, 10} - fold storing intermediate results *)
Append[myList, 5]         (* {1, 2, 3, 4, 5} - note myList is not updated *)
Prepend[myList, 5]        (* {5, 1, 2, 3, 4} - add "myList = " if you want it to be *)
Join[myList, {3, 4}]      (* {1, 2, 3, 4, 3, 4} *)
myList[[2]] = 5          (* {1, 5, 3, 4} - this does update myList *)

(* Associations, aka Dictionaries/Hashes *)
myHash = <|"Green" -> 2, "Red" -> 1|>   (* Create an association *)
myHash[["Green"]]                       (* 2, use it *)
myHash[["Green"]] := 5                  (* 5, update it *)
myHash[["Puce"]] := 3.5                 (* 3.5, extend it *)
KeyDropFrom[myHash, "Green"]            (* Wipes out key Green *)
Keys[myHash]                            (* {Red} *)
Values[myHash]                          (* {1} *)

(* And you can't do any demo of Wolfram without showing this off *)
Manipulate[y^2, {y, 0, 20}] (* Return a reactive user interface that displays y^2
                               and allows y to be adjusted between 0-20 with a slider.
                               Only works on graphical frontends *)
```

##Ready For More?

* [Wolfram Language Documentation Center](http://reference.wolfram.com/language/)
---
language: xml
filename: learnxml.xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
  - ["Rachel Stiyer", "https://github.com/rstiyer"]
  - ["Deepanshu Utkarsh", "https://github.com/duci9y"]
---

XML is a markup language designed to store and transport data. It is supposed to be both human readable and machine readable.

Unlike HTML, XML does not specify how to display or to format data, it just carries it.

Distinctions are made between the **content** and the **markup**. In short, content could be anything, markup is defined.

## Some definitions and introductions

XML Documents are basically made up of *elements* which can have *attributes* describing them and may contain some textual content or more elements as its children. All XML documents must have a root element, which is the ancestor of all the other elements in the document.

XML Parsers are designed to be very strict, and will stop parsing malformed documents. Therefore it must be ensured that all XML documents follow the [XML Syntax Rules](http://www.w3schools.com/xml/xml_syntax.asp).

```xml
<!-- This is a comment. It must not contain two consecutive hyphens (-). -->
<!-- Comments can span
  multiple lines -->

<!-- Elements -->
<!-- An element is a basic XML component. There are two types, empty: -->
<element1 attribute="value" /> <!-- Empty elements do not hold any content -->
<!-- and non-empty: -->
<element2 attribute="value">Content</element2>
<!-- Element names may only contain alphabets and numbers. -->

<empty /> <!-- An element either consists an empty element tag… -->
<!-- …which does not hold any content and is pure markup. -->

<notempty> <!-- Or, it consists of a start tag… -->
  <!-- …some content… -->
</notempty> <!-- and an end tag. -->

<!-- Element names are case sensitive. -->
<element />
<!-- is not the same as -->
<eLEMENT />

<!-- Attributes -->
<!-- An attribute is a key-value pair and exists within an element. -->
<element attribute="value" another="anotherValue" many="space-separated list" />
<!-- An attribute may appear only once in an element. It holds just one value.
  Common workarounds to this involve the use of space-separated lists. -->

<!-- Nesting elements -->
<!-- An element's content may include other elements: -->
<parent>
  <child>Text</child>
  <emptysibling />
</parent>
<!-- Standard tree nomenclature is followed. Each element being called a node.
  An ancestor a level up is the parent, descendants a level down are children.
  Elements within the same parent element are siblings. -->

<!-- XML preserves whitespace. -->
<child>
  Text
</child>
<!-- is not the same as -->
<child>Text</child>
```

## An XML document

This is what makes XML versatile. It is human readable too. The following document tells us that it defines a bookstore which sells three books, one of which is Learning XML by Erik T. Ray. All this without having used an XML Parser yet.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is called an XML prolog. Optional, but recommended. -->
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
```

## Well-formedness and Validation

A XML document is *well-formed* if it is syntactically correct. However, it is possible to add more constraints to the document, using Document Type Definitions (DTDs). A document whose elements are attributes are declared in a DTD and which follows the grammar specified in that DTD is called *valid* with respect to that DTD, in addition to being well-formed.

```xml
<!-- Declaring a DTD externally: -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore SYSTEM "Bookstore.dtd">
<!-- Declares that bookstore is our root element and 'Bookstore.dtd' is the path
  to our DTD file. -->
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
</bookstore>

<!-- The DTD file: -->
<!ELEMENT bookstore (book+)>
<!-- The bookstore element may contain one or more child book elements. -->
<!ELEMENT book (title, price)>
<!-- Each book must have a title and a price as its children. -->
<!ATTLIST book category CDATA "Literature">
<!-- A book should have a category attribute. If it doesn't, its default value
  will be 'Literature'. -->
<!ELEMENT title (#PCDATA)>
<!-- The element title must only contain parsed character data. That is, it may
  only contain text which is read by the parser and must not contain children.
  Compare with CDATA, or character data. -->
<!ELEMENT price (#PCDATA)>
]>

<!-- The DTD could be declared inside the XML file itself.-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE bookstore [
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>

<bookstore>
  <book category="COOKING">
    <title>Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>
```

## DTD Compatibility and XML Schema Definitions

Support for DTDs is ubiquitous because they are so old. Unfortunately, modern XML features like namespaces are not supported by DTDs. XML Schema Definitions (XSDs) are meant to replace DTDs for defining XML document grammar. 

## Resources

* [Validate your XML](http://www.xmlvalidation.com)

## Further Reading

* [XML Schema Definitions Tutorial](http://www.w3schools.com/schema/)
* [DTD Tutorial](http://www.w3schools.com/xml/xml_dtd_intro.asp)
* [XML Tutorial](http://www.w3schools.com/xml/default.asp)
* [Using XPath queries to parse XML](http://www.w3schools.com/xml/xml_xpath.asp)
---
language: yaml
filename: learnyaml.yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
  - ["Suhas SG", "https://github.com/jargnar"]
---

YAML is a data serialisation language designed to be directly writable and
readable by humans.

It's a strict superset of JSON, with the addition of syntactically
significant newlines and indentation, like Python. Unlike Python, however,
YAML doesn't allow literal tab characters at all.

```yaml
# Comments in YAML look like this.

################
# SCALAR TYPES #
################

# Our root object (which continues for the entire document) will be a map,
# which is equivalent to a dictionary, hash or object in other languages.
key: value
another_key: Another value goes here.
a_number_value: 100
scientific_notation: 1e+12
# The number 1 will be interpreted as a number, not a boolean. if you want
# it to be interpreted as a boolean, use true
boolean: true
null_value: null
key with spaces: value
# Notice that strings don't need to be quoted. However, they can be.
however: "A string, enclosed in quotes."
"Keys can be quoted too.": "Useful if you want to put a ':' in your key."

# Multiple-line strings can be written either as a 'literal block' (using |),
# or a 'folded block' (using '>').
literal_block: |
    This entire block of text will be the value of the 'literal_block' key,
    with line breaks being preserved.

    The literal continues until de-dented, and the leading indentation is
    stripped.

        Any lines that are 'more-indented' keep the rest of their indentation -
        these lines will be indented by 4 spaces.
folded_style: >
    This entire block of text will be the value of 'folded_style', but this
    time, all newlines will be replaced with a single space.

    Blank lines, like above, are converted to a newline character.

        'More-indented' lines keep their newlines, too -
        this text will appear over two lines.

####################
# COLLECTION TYPES #
####################

# Nesting is achieved by indentation.
a_nested_map:
    key: value
    another_key: Another Value
    another_nested_map:
        hello: hello

# Maps don't have to have string keys.
0.25: a float key

# Keys can also be complex, like multi-line objects
# We use ? followed by a space to indicate the start of a complex key.
? |
    This is a key
    that has multiple lines
: and this is its value

# YAML also allows mapping between sequences with the complex key syntax
# Some language parsers might complain
# An example
? - Manchester United
  - Real Madrid
: [ 2001-01-01, 2002-02-02 ]

# Sequences (equivalent to lists or arrays) look like this:
a_sequence:
    - Item 1
    - Item 2
    - 0.5 # sequences can contain disparate types.
    - Item 4
    - key: value
      another_key: another_value
    -
        - This is a sequence
        - inside another sequence

# Since YAML is a superset of JSON, you can also write JSON-style maps and
# sequences:
json_map: {"key": "value"}
json_seq: [3, 2, 1, "takeoff"]

#######################
# EXTRA YAML FEATURES #
#######################

# YAML also has a handy feature called 'anchors', which let you easily duplicate
# content across your document. Both of these keys will have the same value:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name

# Anchors can be used to duplicate/inherit properties
base: &base
    name: Everyone has same name

foo: &foo
    <<: *base
    age: 10

bar: &bar
    <<: *base
    age: 20

# foo and bar would also have name: Everyone has same name

# YAML also has tags, which you can use to explicitly declare types.
explicit_string: !!str 0.5
# Some parsers implement language specific tags, like this one for Python's
# complex number type.
python_complex_number: !!python/complex 1+2j

# We can also use yaml complex keys with language specific tags
? !!python/tuple [5, 7]
: Fifty Seven
# Would be {(5, 7): 'Fifty Seven'} in Python

####################
# EXTRA YAML TYPES #
####################

# Strings and numbers aren't the only scalars that YAML can understand.
# ISO-formatted date and datetime literals are also parsed.
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14

# The !!binary tag indicates that a string is actually a base64-encoded
# representation of a binary blob.
gif_file: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML also has a set type, which looks like this:
set:
    ? item1
    ? item2
    ? item3

# Like Python, sets are just maps with null values; the above is equivalent to:
set2:
    item1: null
    item2: null
    item3: null
```

### More Resources

+ [YAML official website](http://yaml.org/)
+ [Online YAML Validator](http://codebeautify.org/yaml-validator)
---
category: tool
tool: zfs
contributors:
    - ["sarlalian", "http://github.com/sarlalian"]
filename: LearnZfs.txt
---


[ZFS](http://open-zfs.org/wiki/Main_Page)
is a rethinking of the storage stack, combining traditional file systems as well as volume
managers into one cohesive tool.  ZFS has some specific terminology that sets it apart from
more traditional storage systems, however it has a great set of features with a focus on
usability for systems administrators.


## ZFS Concepts

### Virtual Devices

A VDEV is similar to a raid device presented by a RAID card, there are several different
types of VDEV's that offer various advantages, including redundancy and speed.  In general
VDEV's offer better reliability and safety than a RAID card.  It is discouraged to use a
RAID setup with ZFS, as ZFS expects to directly manage the underlying disks.

Types of VDEV's

* stripe (a single disk, no redundancy)
* mirror (n-way mirrors supported)
* raidz
	* raidz1 (1-disk parity, similar to RAID 5)
	* raidz2 (2-disk parity, similar to RAID 6)
	* raidz3 (3-disk parity, no RAID analog)
* disk
* file (not recommended for production due to another filesystem adding unnecessary layering)

Your data is striped across all the VDEV's present in your Storage Pool, so more VDEV's will
increase your IOPS.

### Storage Pools

ZFS uses Storage Pools as an abstraction over the lower level storage provider (VDEV), allow
you to separate the user visible file system from the physical layout.

### ZFS Dataset

ZFS datasets are analogous to traditional filesystems but with many more features.  They
provide many of ZFS's advantages.  Datasets support [Copy on Write](https://en.wikipedia.org/wiki/Copy-on-write)
snapshots, quota's, compression and de-duplication.


### Limits

One directory may contain up to 2^48 files, up to 16 exabytes each.  A single storage pool
can contain up to 256 zettabytes (2^78) of space, and can be striped across 2^64 devices.  A
single host can have 2^64 storage pools.  The limits are huge.


## Commands

### Storage Pools

Actions:

* List
* Status
* Destroy
* Get/Set properties

List zpools

```bash
# Create a raidz zpool
$ zpool create bucket raidz1 gpt/zfs0 gpt/zfs1 gpt/zfs2

# List ZPools
$ zpool list
NAME    SIZE  ALLOC   FREE  EXPANDSZ   FRAG    CAP  DEDUP  HEALTH  ALTROOT
zroot   141G   106G  35.2G         -    43%    75%  1.00x  ONLINE  -

# List detailed information about a specific zpool
$ zpool list -v zroot
NAME                                     SIZE  ALLOC   FREE  EXPANDSZ   FRAG    CAP  DEDUP HEALTH  ALTROOT
zroot                                    141G   106G  35.2G         -    43%    75%  1.00x ONLINE  -
  gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655   141G   106G  35.2G         -    43%    75%
```

Status of zpools

```bash
# Get status information about zpools
$ zpool status
  pool: zroot
 state: ONLINE
  scan: scrub repaired 0 in 2h51m with 0 errors on Thu Oct  1 07:08:31 2015
config:

        NAME                                          STATE     READ WRITE CKSUM
        zroot                                         ONLINE       0     0     0
          gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655  ONLINE       0     0     0

errors: No known data errors

# Scrubbing a zpool to correct any errors
$ zpool scrub zroot
$ zpool status -v zroot
  pool: zroot
 state: ONLINE
  scan: scrub in progress since Thu Oct 15 16:59:14 2015
        39.1M scanned out of 106G at 1.45M/s, 20h47m to go
        0 repaired, 0.04% done
config:

        NAME                                          STATE     READ WRITE CKSUM
        zroot                                         ONLINE       0     0     0
          gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655  ONLINE       0     0     0

errors: No known data errors
```

Properties of zpools

```bash

# Getting properties from the pool properties can be user set or system provided.
$ zpool get all zroot
NAME   PROPERTY                       VALUE                          SOURCE
zroot  size                           141G                           -
zroot  capacity                       75%                            -
zroot  altroot                        -                              default
zroot  health                         ONLINE                         -
...

# Setting a zpool property
$ zpool set comment="Storage of mah stuff" zroot
$ zpool get comment
NAME   PROPERTY  VALUE                 SOURCE
tank   comment   -                     default
zroot  comment   Storage of mah stuff  local
```

Remove zpool

```bash
$ zpool destroy test
```


### Datasets

Actions:
* Create
* List
* Rename
* Delete
* Get/Set properties

Create datasets

```bash
# Create dataset
$ zfs create tank/root/data
$ mount | grep data
tank/root/data on /data (zfs, local, nfsv4acls)

# Create child dataset
$ zfs create tank/root/data/stuff
$ mount | grep data
tank/root/data on /data (zfs, local, nfsv4acls)
tank/root/data/stuff on /data/stuff (zfs, local, nfsv4acls)


# Create Volume
$ zfs create -V zroot/win_vm
$ zfs list zroot/win_vm
NAME                 USED  AVAIL  REFER  MOUNTPOINT
tank/win_vm         4.13G  17.9G    64K  -
```

List datasets

```bash
# List all datasets
$ zfs list
NAME                                                                       USED  AVAIL  REFER  MOUNTPOINT
zroot                                                                      106G  30.8G   144K  none
zroot/ROOT                                                                18.5G  30.8G   144K  none
zroot/ROOT/10.1                                                              8K  30.8G  9.63G  /
zroot/ROOT/default                                                        18.5G  30.8G  11.2G  /
zroot/backup                                                              5.23G  30.8G   144K  none
zroot/home                                                                 288K  30.8G   144K  none
...

# List a specific dataset
$ zfs list zroot/home
NAME         USED  AVAIL  REFER  MOUNTPOINT
zroot/home   288K  30.8G   144K  none

# List snapshots
$ zfs list -t snapshot
zroot@daily-2015-10-15                                                                  0      -   144K  -
zroot/ROOT@daily-2015-10-15                                                             0      -   144K  -
zroot/ROOT/default@daily-2015-10-15                                                     0      -  24.2G  -
zroot/tmp@daily-2015-10-15                                                           124K      -   708M  -
zroot/usr@daily-2015-10-15                                                              0      -   144K  -
zroot/home@daily-2015-10-15                                                             0      -  11.9G  -
zroot/var@daily-2015-10-15                                                           704K      -  1.42G  -
zroot/var/log@daily-2015-10-15                                                       192K      -   828K  -
zroot/var/tmp@daily-2015-10-15                                                          0      -   152K  -
```

Rename datasets

```bash
$ zfs rename tank/root/home tank/root/old_home
$ zfs rename tank/root/new_home tank/root/home
```

Delete dataset

```bash
# Datasets cannot be deleted if they have any snapshots
zfs destroy tank/root/home
```

Get / set properties of a dataset

```bash
# Get all properties
$ zfs get all  zroot/usr/home                                                                                              │157 # Create Volume
NAME            PROPERTY              VALUE                  SOURCE                                                                          │158 $ zfs create -V zroot/win_vm
zroot/home      type                  filesystem             -                                                                               │159 $ zfs list zroot/win_vm
zroot/home      creation              Mon Oct 20 14:44 2014  -                                                                               │160 NAME                 USED  AVAIL  REFER  MOUNTPOINT
zroot/home      used                  11.9G                  -                                                                               │161 tank/win_vm         4.13G  17.9G    64K  -
zroot/home      available             94.1G                  -                                                                               │162 ```
zroot/home      referenced            11.9G                  -                                                                               │163
zroot/home      mounted               yes                    -
...

# Get property from dataset
$ zfs get compression zroot/usr/home
NAME            PROPERTY     VALUE     SOURCE
zroot/home      compression  off       default

# Set property on dataset
$ zfs set compression=gzip-9 mypool/lamb

# Get a set of properties from all datasets
$ zfs list -o name,quota,reservation
NAME                                                               QUOTA  RESERV
zroot                                                               none    none
zroot/ROOT                                                          none    none
zroot/ROOT/default                                                  none    none
zroot/tmp                                                           none    none
zroot/usr                                                           none    none
zroot/home                                                          none    none
zroot/var                                                           none    none
...
```


### Snapshots

ZFS snapshots are one of the things about zfs that are a really big deal

* The space they take up is equal to the difference in data between the filesystem and its snapshot
* Creation time is only seconds
* Recovery is as fast as you can write data.
* They are easy to automate.

Actions:
* Create
* Delete
* Rename
* Access snapshots
* Send / Receive
* Clone


Create snapshots

```bash
# Create a snapshot of a single dataset
zfs snapshot tank/home/sarlalian@now

# Create a snapshot of a dataset and its children
$ zfs snapshot -r tank/home@now
$ zfs list -t snapshot
NAME                       USED  AVAIL  REFER  MOUNTPOINT
tank/home@now                 0      -    26K  -
tank/home/sarlalian@now       0      -   259M  -
tank/home/alice@now           0      -   156M  -
tank/home/bob@now             0      -   156M  -
...

Destroy snapshots

```bash
# How to destroy a snapshot
$ zfs destroy tank/home/sarlalian@now

# Delete a snapshot on a parent dataset and its children
$ zfs destroy -r tank/home/sarlalian@now

```

Renaming Snapshots

```bash
# Rename a snapshot
$ zfs rename tank/home/sarlalian@now tank/home/sarlalian@today
$ zfs rename tank/home/sarlalian@now today

# zfs rename -r tank/home@now @yesterday
```

Accessing snapshots

```bash
# CD Into a snapshot directory
$ cd /home/.zfs/snapshot/
```

Sending and Receiving

```bash
# Backup a snapshot to a file
$ zfs send tank/home/sarlalian@now | gzip > backup_file.gz

# Send a snapshot to another dataset
$ zfs send tank/home/sarlalian@now | zfs recv backups/home/sarlalian

# Send a snapshot to a remote host
$ zfs send tank/home/sarlalian@now | ssh root@backup_server 'zfs recv tank/home/sarlalian'

# Send full dataset with snapshos to new host
$ zfs send -v -R tank/home@now | ssh root@backup_server 'zfs recv tank/home'
```

Cloneing Snapshots

```bash
# Clone a snapshot
$ zfs clone tank/home/sarlalian@now tank/home/sarlalian_new

# Promoting the clone so it is no longer dependent on the snapshot
$ zfs promote tank/home/sarlalian_new
```

### Putting it all together

This following a script utilizing FreeBSD, jails and ZFS to automate
provisioning a clean copy of a mysql staging database from a live replication
slave.

```bash
#!/bin/sh

echo "==== Stopping the staging database server ===="
jail -r staging

echo "==== Cleaning up existing staging server and snapshot ===="
zfs destroy -r zroot/jails/staging
zfs destroy zroot/jails/slave@staging

echo "==== Quiescing the slave database ===="
echo "FLUSH TABLES WITH READ LOCK;" | /usr/local/bin/mysql -u root -pmyrootpassword -h slave

echo "==== Snapshotting the slave db filesystem as zroot/jails/slave@staging ===="
zfs snapshot zroot/jails/slave@staging

echo "==== Starting the slave database server ===="
jail -c slave

echo "==== Cloning the slave snapshot to the staging server ===="
zfs clone zroot/jails/slave@staging zroot/jails/staging

echo "==== Installing the staging mysql config ===="
mv /jails/staging/usr/local/etc/my.cnf /jails/staging/usr/local/etc/my.cnf.slave
cp /jails/staging/usr/local/etc/my.cnf.staging /jails/staging/usr/local/etc/my.cnf

echo "==== Setting up the staging rc.conf file ===="
mv /jails/staging/etc/rc.conf.local /jails/staging/etc/rc.conf.slave
mv /jails/staging/etc/rc.conf.staging /jails/staging/etc/rc.conf.local

echo "==== Starting the staging db server ===="
jail -c staging

echo "==== Makes the staging database not pull from the master ===="
echo "STOP SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
echo "RESET SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
```


### Additional Reading

* [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs)
* [FreeBSD Handbook on ZFS](https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/zfs.html)
* [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs)
* [Oracle's Tuning Guide](http://www.oracle.com/technetwork/articles/servers-storage-admin/sto-recommended-zfs-settings-1951715.html)
* [OpenZFS Tuning Guide](http://open-zfs.org/wiki/Performance_tuning)
* [FreeBSD ZFS Tuning Guide](https://wiki.freebsd.org/ZFSTuningGuide)
---
category: tool
tool: AngularJS
contributors:
    - ["Walter Cordero", "http://waltercordero.com"]
filename: learnangular-cn.html
translators:
   - ["Jiang Haiyun", "http://www.atjiang.com"]
lang: zh-cn
---

## AngularJS 教程。

AngularJS 1.0 版在 2012 年发布。
Miško Hevery, 一位 Google 员工， 从 2009 年开始开发 AngularJS。
结果发现这个想法很好，从而该项目现在也被 Google 官方所支持了。

AngularJS 是一个 JavaScript 框架。它可以通过一个 "script" 标签添加到一个 HTML 页面中。
AngularJS 通过指令扩展了 HTML 属性，并且通过表达式将数据绑定到 HTML。

## 你应该已经了解了的知识

在学习 AngularJS 之前， 你应该对以下知识有了基本的了解：

- HTML
- CSS
- JavaScript

```html
// AngularJS 是一个 JavaScript 框架。它是一个用 JavaScript 写的库。
// AngularJS 以一个 JavaScript 文件的形式发布，并且能通过一个 script 标签添加到一个网页中：
// <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

///////////////////////////////////
// AngularJS 扩展 HTML

//AngularJS 通过 ng-directives 扩展 HTML。
//ng-app 指令定义一个 AngularJS 应用。
//ng-model 指令将 HTML 控件 (input, select, textarea) 的值绑定到应用的数据上。
//ng-bind 指令将应用的数据绑定到 HTML 视图上。
<!DOCTYPE html>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div ng-app="">
      <p>Name: <input type="text" ng-model="name"></p>
      <p ng-bind="name"></p>
    </div>
  </body>
</html>

/*
  * 例子解析：
  * AngularJS 在网页加载后自动开启。
  * ng-app 指令告诉 AngularJS： <div> 元素是 AngularJS 应用的 "所有者"。
  * ng-model 指令将 input 输入框的值绑定到应用的 name 变量上。
  * ng-bind 指令将 <p> 元素的 innerHTML 绑定到应用的 name 变量上。
*/
<tag> 这里是要解析的内容 </tag>

///////////////////////////////////
// AngularJS 表达式

// AngularJS 表达式写在双括号内： {{ 表达式 }}。
// AngularJS 表达式采用和 ng-bind 指令一样的方式将数据绑定到 HTML。
// AngularJS 将在编写表达式的原样位置上 "输出" 数据。
// AngularJS 表达式非常像 JavaScript 表达式：它们能包含文本，运算符和变量。
// 例如 {{ 5 + 5 }} 或 {{ firstName + " " + lastName }}
<!DOCTYPE html>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div ng-app="">
      <p>My first expression: {{ 5 + 5 }}</p>
    </div>
  </body>
</html>

//如果你删除了 ng-app 指令， HTML 将原样显示表达式，不对它进行解析：
<!DOCTYPE html>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div>
      <p>My first expression: {{ 5 + 5 }}</p>
    </div>
  </body>
</html>

// AngularJS 表达式采用和 ng-bind 指令一样的方式将 AngularJS 数据绑定到 HTML。
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <body>
    <div ng-app="">
      <p>Name: <input type="text" ng-model="name"></p>
      <p>{{name}}</p>
    </div>
  </body>
</html>

// AngularJS 的数字类似 JavaScript 的数字：
<div ng-app="" ng-init="quantity=1;cost=5">
  <p>Total in dollar: {{ quantity * cost }}</p>
</div>

//AngularJS 的字符串类似 JavaScript 的字符串：
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
  <p>The name is <span ng-bind="firstName + ' ' + lastName"></span></p>
</div>

//AngularJS 的对象类似 JavaScript 的对象：
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
  <p>The name is {{ person.lastName }}</p>
</div>

//AngularJS 的数组类似 JavaScript 的数组：
<div ng-app="" ng-init="points=[1,15,19,2,40]">
  <p>The third result is {{ points[2] }}</p>
</div>

// 和 JavaScript 表达式一样， AngularJS 表达式能包含文本，运算符和变量。
// 和 JavaScript 表达式不同， AngularJS 表达式能写在 HTML 内。
// AngularJS 表达式不支持条件，循环和异常，而 JavaScript 表达式却支持。
// AngularJS 表达式支持过滤器，而 JavaScript 表达式不支持。

///////////////////////////////////
// AngularJS 指令


//AngularJS 指令使用前缀 ng- 扩展 HTML 属性。
//ng-app 指令初始化一个 AngularJS 应用。
//ng-init 指令初始化应用的数据。
//ng-model 指令将 HTML 控件 (input, select, textarea) 的值绑定到应用的数据上。
<div ng-app="" ng-init="firstName='John'">
  <p>Name: <input type="text" ng-model="firstName"></p>
  <p>You wrote: {{ firstName }}</p>
</div>

//使用 ng-init 并不常见。你将在有关控制器的章节中学习如何初始化数据。

//ng-repeat 指令会重复一个 HTML 元素：
<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

//ng-repeat 指令用在一个对象数组上：
<div ng-app="" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
  <ul>
    <li ng-repeat="x  in names">
      {{ x.name + ', ' + x.country }}
    </li>
  </ul>
</div>

// AngularJS 最适合用于数据库 CRUD (Create Read Update Delete) 的应用。
// 只需设想这些对象都是来自一个数据库的记录。

// ng-app 指令定义一个 AngularJS 应用的根元素。
// ng-app 指令将在页面加载后自动启动（自动初始化）应用。
// 稍后你将学习如何为 ng-app 设置一个值（如 ng-app="myModule"）， 来连接代码模块。

// ng-init 指令为一个 AngularJS 应用定义初始值。
// 通常，你不太使用 ng-init。你会转而使用一个控制器或模块。
// 你将在稍后学到更多有关控制器和模块的内容。

//ng-model 指令将 HTML 控件 (input, select, textarea) 的值绑定到应用的数据上。
//ng-model 指令还能：
//为应用的数据提供类型验证 (number, email, required)。
//为应用的数据提供状态信息 (invalid, dirty, touched, error)。
//为 HTML 元素提供 CSS 类。
//将 HTML 元素绑定到 HTML 表单。

//ng-repeat 指令为集合（一个数组）中的每个元素克隆出 HTML 元素。

///////////////////////////////////
// AngularJS 控制器

// AngularJS 控制器控制 AngularJS 应用中的数据。
// AngularJS 控制器就是常规的 JavaScript 对象。

// AngularJS 应用由控制器控制。
// ng-controller 指令定义应用的控制器。
// 一个控制器就是一个 JavaScript 对象， 通过标准的 JavaScript 对象构建器创建。

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

//应用的解析：

//AngularJS 应用通过 ng-app="myApp" 定义。该应用运行在 <div> 内。
//ng-controller="myCtrl" 属性是一个 AngularJS 指令。它定义了一个控制器。
//myCtrl 函数是一个 JavaScript 函数。
//AngularJS 将使用一个 $scope 对象来调用控制器。
//AngularJS 中， $scope 就是该应用对象（应用的变量和函数的所有者）。
//该控制器在 $scope 内创建了两个属性（即变量 firstName 和 lastName）。
//ng-model 指令将输入表单项绑定到控制器的属性上（firstName 和 lastName）。

//以上的例子演示了一个包含有两个属性 lastName 和 firstName 的控制器。
//一个控制器也可以有方法（函数的变量）：
<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    $scope.fullName = function() {
        return $scope.firstName + " " + $scope.lastName;
    }
});
</script>

//在较大型的应用中， 通常是将控制器代码保存在外部文件中。
//只需将 <script> </script> 标签之间的代码复制到一个名为 personController.js 的外部文件中：

<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

<script src="personController.js"></script>

// 为方便下个例子使用，我们将创建一个新的控制器文件：
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        {name:'Jani',country:'Norway'},
        {name:'Hege',country:'Sweden'},
        {name:'Kai',country:'Denmark'}
    ];
});

//将文件保存为 namesController.js：
//然后在一个应用中使用该控制器：

<div ng-app="myApp" ng-controller="namesCtrl">

<ul>
  <li ng-repeat="x in names">
    {{ x.name + ', ' + x.country }}
  </li>
</ul>

</div>

<script src="namesController.js"></script>

///////////////////////////////////
// AngularJS 过滤器

// 过滤器可以通过一个管道符添加到表达式和指令上。
// AngularJS 过滤器能用来转换数据：

- **currency**:  将一个数字格式化成货币格式。
- **filter**:  从一个数组中选择一组子集元素。
- **lowercase**: 将一个字符串格式化成小写形式。
- **orderBy**: 依据一个表达式排序一个数组。
- **upper**: 将一个字符串格式化成大写形式。

//一个过滤器可以通过一个管道符 (|) 及一个过滤器表达式添加到一个表达式上。
//（在下面的两个例子中，我们将使用前一章中的 person 控制器）
//uppercase 过滤器将字符串格式化成大写格式：
<div ng-app="myApp" ng-controller="personCtrl">

<p>The name is {{ lastName | uppercase }}</p>

</div>

//lowercase 过滤器将字符串格式化成小写格式：
<div ng-app="myApp" ng-controller="personCtrl">

<p>The name is {{ lastName | lowercase }}</p>

</div>

//currency 过滤器将一个数字格式化成货币格式：
<div ng-app="myApp" ng-controller="costCtrl">

<input type="number" ng-model="quantity">
<input type="number" ng-model="price">

<p>Total = {{ (quantity * price) | currency }}</p>

</div> 

//一个过滤器可以通过一个管道符 (|) 及一个过滤器表达式添加到一个指令上。
//orderBy 过滤器根据一个表达式排序一个数组：
<div ng-app="myApp" ng-controller="namesCtrl">

<ul>
  <li ng-repeat="x in names | orderBy:'country'">
    {{ x.name + ', ' + x.country }}
  </li>
</ul>

<div>

//一个输入框过滤器可以通过一个管道符 (|) 
//以及后跟一个冒号和模式名的 filter 添加到一个指令上。
//该过滤器从一个数组中选择一个子集：

<div ng-app="myApp" ng-controller="namesCtrl">

<p><input type="text" ng-model="test"></p>

<ul>
  <li ng-repeat="x in names | filter:test | orderBy:'country'">
    {{ (x.name | uppercase) + ', ' + x.country }}
  </li>
</ul>

</div>

///////////////////////////////////
// AngularJS AJAX - $http

//$http 是一个从远程服务器读取数据的 AngularJS 服务。

// 以下数据可由一个 web 服务器提供：
// http://www.w3schools.com/angular/customers.php
// **访问 URL 来查看数据格式**

// AngularJS $http 是一个从 web 服务器上读取数据的核心服务。
// $http.get(url) 这个函数用来读取服务器数据。
<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul>
  <li ng-repeat="x in names">
    {{ x.Name + ', ' + x.Country }}
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function(response) {$scope.names = response.records;});
});
</script>

// 应用解析：

// AngularJS 应用由 ng-app 定义。该应用运行在一个 <div> 中。
// ng-controller 指令命名控制器对象。
// customersCtrl 函数是一个标准的 JavaScript 对象构造器。
// AngularJS 会使用一个 $scope 和 $http 对象来调用 customersCtrl。
// $scope 就是该应用对象（应用的变量和函数的所有者）。
// $http 是一个用于请求外部数据的 XMLHttpRequest 对象。
// $http.get() 从 http://www.w3schools.com/angular/customers.php 读取 JSON 数据。
// 如果成功， 该控制器会根据来自服务器的 JSON 数据，在 $scope 中创建一个属性 (names)。


// 向不同的服务器（不同于请求页）请求数据，称作跨站 HTTP 请求。
// 跨站请求在网站上很普遍。许多网页会从不同的服务器加载 CSS，图片和脚本。
// 在现代浏览器中，基于安全原因，从脚本内进行跨站 HTTP 请求是被禁止的。
// 下面的这行代码，已被加入到我们的 PHP 例子中，以便允许跨站访问。
header("Access-Control-Allow-Origin: *");


///////////////////////////////////
// AngularJS 表格

// 使用 angular 显示表格非常简单：
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function (response) {$scope.names = response.records;});
});
</script>

// 要排序表格，添加一个 orderBy 过滤器：
<table>
  <tr ng-repeat="x in names | orderBy : 'Country'">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

// 要显示表格索引值，添加一个带有 $index 的 <td>：
<table>
  <tr ng-repeat="x in names">
    <td>{{ $index + 1 }}</td>
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

// 使用 $even 和 $odd
<table>
  <tr ng-repeat="x in names">
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td>
    <td ng-if="$even">{{ x.Name }}</td>
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td>
    <td ng-if="$even">{{ x.Country }}</td>
  </tr>
</table>

///////////////////////////////////
// AngularJS HTML DOM

//AngularJS 有用于将应用的数据绑定到 HTML DOM 元素属性的指令。

// ng-disabled 指令将 AngularJS 应用的数据绑定到 HTML 元素的 disabled 属性上。

<div ng-app="" ng-init="mySwitch=true">

<p>
<button ng-disabled="mySwitch">Click Me!</button>
</p>

<p>
<input type="checkbox" ng-model="mySwitch">Button
</p>

</div>

//应用解析：

// ng-disabled 指令将应用的 mySwitch 数据绑定到 HTML 按钮的 disabled 属性上。
// ng-model 指令将 HTML checkbox 元素的值绑定到 mySwitch 的值上。
// 如果 mySwitch 的值求值为 true，则该按钮将被禁用：
<p>
<button disabled>Click Me!</button>
</p>

// 如果 mySwitch 的值求值为 false，则该按钮将不会被禁用：
<p>
  <button>Click Me!</button>
</p>

// ng-show 指令显示或隐藏一个 HTML 元素。

<div ng-app="">

<p ng-show="true">I am visible.</p>

<p ng-show="false">I am not visible.</p>

</div>

// ng-show 指令基于 ng-show 的值显示（或隐藏）一个 HTML 元素。
// 你可以使用任何能求值成 true 或 false 的表达式：
<div ng-app="">
<p ng-show="hour > 12">I am visible.</p>
</div>

///////////////////////////////////
// AngularJS 事件

// AngularJS 有它自己的 HTML 事件指令。

// ng-click 指令定义一个 AngularJS 点击事件。
<div ng-app="myApp" ng-controller="myCtrl">

<button ng-click="count = count + 1">Click me!</button>

<p>{{ count }}</p>

</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.count = 0;
});
</script>

// ng-hide 指令可用于设置一个应用的部分区域的可见性。
// 值 ng-hide="true" 使得一个 HTML 元素不可见。
// 值 ng-hide="false" 使得一个 HTML 元素可见。
<div ng-app="myApp" ng-controller="personCtrl">

<button ng-click="toggle()">Toggle</button>

<p ng-hide="myVar">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John",
    $scope.lastName = "Doe"
    $scope.myVar = false;
    $scope.toggle = function() {
        $scope.myVar = !$scope.myVar;
    };
});
</script>

//应用解析：

// personController 的第一部分和讲述控制器章节中的一样。
// 该应用有一个默认属性（一个变量）：$scope.myVar = false：
// ng-hide 指令依据 myVar 的值（true 或 false），
// 设置 <p> 元素的可见性，该元素含有两个输入框。
// 函数 toggle() 将 myVar 在 true 和 false 间进行切换。
// 值 ng-hide="true" 使得该元素不可见。


// ng-show 指令也能用来设置一个应用的某部分的可见性。
// 值 ng-show="false" 使得一个 HTML 元素不可见。
// 值 ng-show="true" 使得一个 HTML 元素可见。
// 这个例子与上面的一样，但用 ng-show 替代了 ng-hide：
<div ng-app="myApp" ng-controller="personCtrl">

<button ng-click="toggle()">Toggle</button>

<p ng-show="myVar">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John",
    $scope.lastName = "Doe"
    $scope.myVar = true;
    $scope.toggle = function() {
        $scope.myVar = !$scope.myVar;
    }
});
</script>

///////////////////////////////////
// AngularJS 模块

// 一个 AngularJS 模块定义一个应用。
// 模块是一个应用的不同部分所在的一个容器。
// 模块是应用控制器的一个容器。
// 控制器总是隶属于一个模块。

// 这个应用 ("myApp") 有一个控制器 ("myCtrl")：

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

</body>
</html>

// 在 AngularJS 应用中通常将模块和控制器放置在 JavaScript 文件中。
// 在本例中，"myApp.js" 包含了一个应用模块的定义，而 "myCtrl.js" 包含了控制器：

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script src="myApp.js"></script>
<script src="myCtrl.js"></script>

</body>
</html>

//myApp.js
var app = angular.module("myApp", []); 

// 模块定义中的 [] 参数可用来定义依赖的模块。

// myCtrl.js
app.controller("myCtrl", function($scope) {
    $scope.firstName  = "John";
    $scope.lastName= "Doe";
});

// JavaScript 中应该避免使用全局函数。它们会非常容易地被覆盖
// 或被其它脚本破坏。

// AngularJS 脚本通过将所有函数保存在模块内，缓解了这种问题。

// 虽然 HTML 应用中通常是将脚本放置在
// <body> 元素的末尾，但还是推荐你要么在
// <head> 中要么在 <body> 的开头处加载 AngularJS 库。

// 这是因为对 angular.module 的调用只有在库被加载后才能被编译。

<!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

</body>
</html>


///////////////////////////////////
// AngularJS 应用

// AngularJS 模块定义 AngularJS 应用。
// AngularJS 控制器控制 AngularJS 应用。
// ng-app 指令定义该应用，ng-controller 定义该控制器。
<div ng-app="myApp" ng-controller="myCtrl">
  First Name: <input type="text" ng-model="firstName"><br>
  Last Name: <input type="text" ng-model="lastName"><br>
  <br>
  Full Name: {{firstName + " " + lastName}}
</div>
<script>
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope) {
      $scope.firstName= "John";
      $scope.lastName= "Doe";
  });
</script>

// AngularJS 模块定义应用：
var app = angular.module('myApp', []);

// AngularJS 控制器控制应用：
app.controller('myCtrl', function($scope) {
    $scope.firstName= "John";
    $scope.lastName= "Doe";
});
```

## 来源 & 参考

**例子**

- http://www.w3schools.com/angular/angular_examples.asp

**参考**

- http://www.w3schools.com/angular/angular_ref_directives.asp
- http://www.w3schools.com/angular/default.asp
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
translators:
    - ["Jinchang Ye", "https://github.com/Alwayswithme"]
    - ["Chunyang Xu", "https://github.com/XuChunyang"]
filename: LearnBash-cn.sh
lang: zh-cn
---

Bash 是一个为 GNU 计划编写的 Unix shell，是 Linux 和 Mac OS X 下的默认 shell。
以下大多数例子可以作为脚本的一部分运行，也可直接在 shell 下交互执行。

[更多信息](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# 脚本的第一行叫 shebang，用来告知系统如何执行该脚本:
# 参见： http://en.wikipedia.org/wiki/Shebang_(Unix)
# 如你所见，注释以 # 开头，shebang 也是注释。

# 显示 “Hello world!”
echo Hello world!

# 每一句指令以换行或分号隔开：
echo 'This is the first line'; echo 'This is the second line'

# 声明一个变量：
Variable="Some string"

# 下面是错误的做法：
Variable = "Some string"
# Bash 会把 Variable 当做一个指令，由于找不到该指令，因此这里会报错。

# 也不可以这样：
Variable= 'Some string'
# Bash 会认为 'Some string' 是一条指令，由于找不到该指令，这里再次报错。
# （这个例子中 'Variable=' 这部分会被当作仅对 'Some string' 起作用的赋值。）

# 使用变量：
echo $Variable
echo "$Variable"
echo '$Variable'
# 当你赋值 (assign) 、导出 (export)，或者以其他方式使用变量时，变量名前不加 $。
# 如果要使用变量的值， 则要加 $。
# 注意： ' (单引号) 不会展开变量（即会屏蔽掉变量）。


# 在变量内部进行字符串代换
echo ${Variable/Some/A}
# 会把 Variable 中首次出现的 "some" 替换成 “A”。

# 变量的截取
Length=7
echo ${Variable:0:Length}
# 这样会仅返回变量值的前7个字符

# 变量的默认值
echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
# 对 null (Foo=) 和空串 (Foo="") 起作用； 零（Foo=0）时返回0
# 注意这仅返回默认值而不是改变变量的值

# 内置变量：
# 下面的内置变量很有用
echo "Last program return value: $?"
echo "Script's PID: $$"
echo "Number of arguments: $#"
echo "Scripts arguments: $@"
echo "Scripts arguments separated in different variables: $1 $2..."

# 读取输入：
echo "What's your name?"
read Name # 这里不需要声明新变量
echo Hello, $Name!

# 通常的 if 结构看起来像这样：
# 'man test' 可查看更多的信息
if [ $Name -ne $USER ]
then
    echo "Your name isn't your username"
else
    echo "Your name is your username"
fi

# 根据上一个指令执行结果决定是否执行下一个指令
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# 在 if 语句中使用 && 和 || 需要多对方括号
if [ $Name == "Steve" ] && [ $Age -eq 15 ]
then
    echo "This will run if $Name is Steve AND $Age is 15."
fi

if [ $Name == "Daniya" ] || [ $Name == "Zach" ]
then
    echo "This will run if $Name is Daniya OR Zach."
fi

# 表达式的格式如下:
echo $(( 10 + 5 ))

# 与其他编程语言不同的是，bash 运行时依赖上下文。比如，使用 ls 时，列出当前目录。
ls

# 指令可以带有选项：
ls -l # 列出文件和目录的详细信息

# 前一个指令的输出可以当作后一个指令的输入。grep 用来匹配字符串。
# 用下面的指令列出当前目录下所有的 txt 文件：
ls -l | grep "\.txt"

# 重定向输入和输出（标准输入，标准输出，标准错误）。
# 以 ^EOF$ 作为结束标记从标准输入读取数据并覆盖 hello.py :
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# 重定向可以到输出，输入和错误输出。
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# > 会覆盖已存在的文件， >> 会以累加的方式输出文件中。
python hello.py >> "output.out" 2>> "error.err"

# 覆盖 output.out , 追加 error.err 并统计行数
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# 运行指令并打印文件描述符 （比如 /dev/fd/123）
# 具体可查看： man fd
echo <(echo "#helloworld")

# 以 "#helloworld" 覆盖 output.out:
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# 清理临时文件并显示详情（增加 '-i' 选项启用交互模式）
rm -v output.out error.err output-and-error.log

# 一个指令可用 $( ) 嵌套在另一个指令内部：
# 以下的指令会打印当前目录下的目录和文件总数
echo "There are $(ls | wc -l) items here."

# 反引号 `` 起相同作用，但不允许嵌套
# 优先使用 $(  ).
echo "There are `ls | wc -l` items here."

# Bash 的 case 语句与 Java 和 C++ 中的 switch 语句类似:
case "$Variable" in
    # 列出需要匹配的字符串
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# 循环遍历给定的参数序列:
# 变量$Variable 的值会被打印 3 次。
for Variable in {1..3}
do
    echo "$Variable"
done

# 或传统的 “for循环” ：
for ((a=1; a <= 3; a++))
do
    echo $a
done

# 也可以用于文件
# 用 cat 输出 file1 和 file2 内容
for Variable in file1 file2
do
    cat "$Variable"
done

# 或作用于其他命令的输出
# 对 ls 输出的文件执行 cat 指令。
for Output in $(ls)
do
    cat "$Output"
done

# while 循环：
while [ true ]
do
    echo "loop body here..."
    break
done

# 你也可以使用函数
# 定义函数：
function foo ()
{
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
}

# 更简单的方法
bar ()
{
    echo "Another way to declare functions!"
    return 0
}

# 调用函数
foo "My name is" $Name

# 有很多有用的指令需要学习:
# 打印 file.txt 的最后 10 行
tail -n 10 file.txt
# 打印 file.txt 的前 10 行
head -n 10 file.txt
# 将 file.txt 按行排序
sort file.txt
# 报告或忽略重复的行，用选项 -d 打印重复的行
uniq -d file.txt
# 打印每行中 ',' 之前内容
cut -d ',' -f 1 file.txt
# 将 file.txt 文件所有 'okay' 替换为 'great', （兼容正则表达式）
sed -i 's/okay/great/g' file.txt
# 将 file.txt 中匹配正则的行打印到标准输出
# 这里打印以 "foo" 开头, "bar" 结尾的行
grep "^foo.*bar$" file.txt
# 使用选项 "-c" 统计行数
grep -c "^foo.*bar$" file.txt
# 如果只是要按字面形式搜索字符串而不是按正则表达式，使用 fgrep (或 grep -F)
fgrep "^foo.*bar$" file.txt 


# 以 bash 内建的 'help' 指令阅读 Bash 自带文档：
help
help help
help for
help return
help source
help .

# 用 man 指令阅读相关的 Bash 手册
apropos bash
man 1 bash
man bash

# 用 info 指令查阅命令的 info 文档 （info 中按 ? 显示帮助信息）
apropos info | grep '^info.*('
man info
info info
info 5 info

# 阅读 Bash 的 info 文档：
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: bf
filename: brainfuck-cn.bf
contributors:
    - ["Prajit Ramachandran", "http://prajitr.github.io/"]
    - ["Mathias Bynens", "http://mathiasbynens.be/"]
translators:
    - ["lyuehh", "https://github.com/lyuehh"]
lang: zh-cn

---

Brainfuck 是一个极小的只有8个指令的图灵完全的编程语言。

```
除"><+-.,[]"之外的的任何字符都会被忽略 (不包含双引号)。

Brainfuck 包含一个有30,000个单元为0的数组，和
一个数据指针指向当前的单元。

8个指令如下:
+ : 指针指向的单元的值加1
- : 指针指向的单元的值减1
> : 将指针移动到下一个单元(右边的元素)
< : 将指针移动到上一个单元(左边的元素)
. : 打印当前单元的内容的ASCII值 (比如 65 = 'A').
, : 读取一个字符到当前的单元
[ : 如果当前单元的值是0，则向后调转到对应的]处
] : 如果当前单元的值不是0，则向前跳转到对应的[处

[ 和 ] 组成了一个while循环。很明显，它们必须配对。

让我们看一些基本的brainfuck 程序。

++++++ [ > ++++++++++ < - ] > +++++ .

这个程序打印字母'A'。首先，它把 #1 增加到6，使用它来作为循环条件，
然后，进入循环，将指针移动到 #2 ，将 #2 的值增加到10，然后
移动回 #1，将单元 #1 的值减1，然后继续。循环共进行了6次。

这时，我们在 #1，它的值为0，#2 的值为60，我们移动到
#2，将 #2 的内容加上5，然后将 #2 的内容打印出来，65在
ASCII中表示'A', 所以'A'就会被打印出来。


, [ > + < - ] > .

这个程序从用户的输入中读取一个字符，然后把它复制到 #1。
然后我们开始一个循环，移动到 #2，将 #2 的值加1，再移动回 #1，将 #1
的值减1，直到 #1的值为0，这样 #2 里就保存了 #1 的旧值，循环结束时我们
在 #1，这时我们移动到 #2，然后把字符以ASCII打印出来。

而且要记住的一点就是，空格在这里只是为了可读性，你可以将他们写成这样:

,[>+<-]>.

试着思考一下这段程序是干什么的:

,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>

这段程序从输入接收2个参数，然后将他们相乘。

先读取2个输入，然后开始外层循环，以 #1 作为终止条件，然后将指针移动到
#2，然后开始 #2 的内层循环，将 #3 加1。但是这里有一个小问题，在内层
循环结束的时候，#2 的值是0了，那么下次执行外层循环的时候，就有问题了。
为了解决这个问题，我们可以增加 #4 的值，然后把 #4 的值复制到 #2，
最后结果就保存在 #3 中了。
```
好了这就是brainfuck了。也没那么难，是吧？为了好玩，你可以写你自己的
brainfuck程序，或者用其他语言写一个brainfuck的解释器，解释器非常容易
实现，但是如果你是一个自虐狂的话，你可以尝试用brainfuck写一个brainfuk的
解释器。
---
language: c++
filename: learncpp-cn.cpp
contributors:
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Matt Kline", "https://github.com/mrkline"]
translators:
    - ["Arnie97", "https://github.com/Arnie97"]
lang: zh-cn
---

C++是一种系统编程语言。用它的发明者，
[Bjarne Stroustrup的话](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote)来说，C++的设计目标是：

- 成为“更好的C语言”
- 支持数据的抽象与封装
- 支持面向对象编程
- 支持泛型编程

C++提供了对硬件的紧密控制（正如C语言一样），
能够编译为机器语言，由处理器直接执行。
与此同时，它也提供了泛型、异常和类等高层功能。
虽然C++的语法可能比某些出现较晚的语言更复杂，它仍然得到了人们的青睞——
功能与速度的平衡使C++成为了目前应用最广泛的系统编程语言之一。

```c++
////////////////
// 与C语言的比较
////////////////

// C++_几乎_是C语言的一个超集，它与C语言的基本语法有许多相同之处，
// 例如变量和函数的声明，原生数据类型等等。

// 和C语言一样，在C++中，你的程序会从main()开始执行，
// 该函数的返回值应当为int型，这个返回值会作为程序的退出状态值。
// 不过，大多数的编译器（gcc，clang等）也接受 void main() 的函数原型。
// （参见 http://en.wikipedia.org/wiki/Exit_status 来获取更多信息）
int main(int argc, char** argv)
{
    // 和C语言一样，命令行参数通过argc和argv传递。
    // argc代表命令行参数的数量，
    // 而argv是一个包含“C语言风格字符串”（char *）的数组，
    // 其中每个字符串代表一个命令行参数的内容，
    // 首个命令行参数是调用该程序时所使用的名称。
    // 如果你不关心命令行参数的值，argc和argv可以被忽略。
    // 此时，你可以用int main()作为函数原型。

    // 退出状态值为0时，表示程序执行成功
    return 0;
}

// 然而，C++和C语言也有一些区别：

// 在C++中，字符字面量的大小是一个字节。
sizeof('c') == 1

// 在C语言中，字符字面量的大小与int相同。
sizeof('c') == sizeof(10)


// C++的函数原型与函数定义是严格匹配的
void func(); // 这个函数不能接受任何参数

// 而在C语言中
void func(); // 这个函数能接受任意数量的参数

// 在C++中，用nullptr代替C语言中的NULL
int* ip = nullptr;

// C++也可以使用C语言的标准头文件，
// 但是需要加上前缀“c”并去掉末尾的“.h”。
#include <cstdio>

int main()
{
    printf("Hello, world!\n");
    return 0;
}

///////////
// 函数重载
///////////

// C++支持函数重载，你可以定义一组名称相同而参数不同的函数。

void print(char const* myString)
{
    printf("String %s\n", myString);
}

void print(int myInt)
{
    printf("My int is %d", myInt);
}

int main()
{
    print("Hello"); // 解析为 void print(const char*)
    print(15); // 解析为 void print(int)
}

///////////////////
// 函数参数的默认值
///////////////////

// 你可以为函数的参数指定默认值，
// 它们将会在调用者没有提供相应参数时被使用。

void doSomethingWithInts(int a = 1, int b = 4)
{
    // 对两个参数进行一些操作
}

int main()
{
    doSomethingWithInts();      // a = 1,  b = 4
    doSomethingWithInts(20);    // a = 20, b = 4
    doSomethingWithInts(20, 5); // a = 20, b = 5
}

// 默认参数必须放在所有的常规参数之后。

void invalidDeclaration(int a = 1, int b) // 这是错误的！
{
}


///////////
// 命名空间
///////////

// 命名空间为变量、函数和其他声明提供了分离的的作用域。
// 命名空间可以嵌套使用。

namespace First {
    namespace Nested {
        void foo()
        {
            printf("This is First::Nested::foo\n");
        }
    } // 结束嵌套的命名空间Nested
} // 结束命名空间First

namespace Second {
    void foo()
    {
        printf("This is Second::foo\n")
    }
}

void foo()
{
    printf("This is global foo\n");
}

int main()
{
    // 如果没有特别指定，就从“Second”中取得所需的内容。
    using namespace Second;

    foo(); // 显示“This is Second::foo”
    First::Nested::foo(); // 显示“This is First::Nested::foo”
    ::foo(); // 显示“This is global foo”
}

////////////
// 输入/输出
////////////

// C++使用“流”来输入输出。<<是流的插入运算符，>>是流提取运算符。
// cin、cout、和cerr分别代表
// stdin（标准输入）、stdout（标准输出）和stderr（标准错误）。

#include <iostream> // 引入包含输入/输出流的头文件

using namespace std; // 输入输出流在std命名空间（也就是标准库）中。

int main()
{
   int myInt;

   // 在标准输出（终端/显示器）中显示
   cout << "Enter your favorite number:\n";
   // 从标准输入（键盘）获得一个值
   cin >> myInt;

   // cout也提供了格式化功能
   cout << "Your favorite number is " << myInt << "\n";
   // 显示“Your favorite number is <myInt>”

   cerr << "Used for error messages";
}

/////////
// 字符串
/////////

// C++中的字符串是对象，它们有很多成员函数
#include <string>

using namespace std; // 字符串也在std命名空间（标准库）中。

string myString = "Hello";
string myOtherString = " World";

// + 可以用于连接字符串。
cout << myString + myOtherString; // "Hello World"

cout << myString + " You"; // "Hello You"

// C++中的字符串是可变的，具有“值语义”。
myString.append(" Dog");
cout << myString; // "Hello Dog"


/////////////
// 引用
/////////////

// 除了支持C语言中的指针类型以外，C++还提供了_引用_。
// 引用是一种特殊的指针类型，一旦被定义就不能重新赋值，并且不能被设置为空值。
// 使用引用时的语法与原变量相同：
// 也就是说，对引用类型进行解引用时，不需要使用*；
// 赋值时也不需要用&来取地址。

using namespace std;

string foo = "I am foo";
string bar = "I am bar";


string& fooRef = foo; // 建立了一个对foo的引用。
fooRef += ". Hi!"; // 通过引用来修改foo的值
cout << fooRef; // "I am foo. Hi!"

// 这句话的并不会改变fooRef的指向，其效果与“foo = bar”相同。
// 也就是说，在执行这条语句之后，foo == "I am bar"。
fooRef = bar;

const string& barRef = bar; // 建立指向bar的常量引用。
// 和C语言中一样，（指针和引用）声明为常量时，对应的值不能被修改。
barRef += ". Hi!"; // 这是错误的，不能修改一个常量引用的值。

///////////////////
// 类与面向对象编程
///////////////////

// 有关类的第一个示例
#include <iostream>

// 声明一个类。
// 类通常在头文件（.h或.hpp）中声明。
class Dog {
    // 成员变量和成员函数默认情况下是私有（private）的。
    std::string name;
    int weight;

// 在这个标签之后，所有声明都是公有（public）的，
// 直到重新指定“private:”（私有继承）或“protected:”（保护继承）为止
public:

    // 默认的构造器
    Dog();

    // 这里是成员函数声明的一个例子。
    // 可以注意到，我们在此处使用了std::string，而不是using namespace std
    // 语句using namespace绝不应当出现在头文件当中。
    void setName(const std::string& dogsName);

    void setWeight(int dogsWeight);

    // 如果一个函数不对对象的状态进行修改，
    // 应当在声明中加上const。
    // 这样，你就可以对一个以常量方式引用的对象执行该操作。
    // 同时可以注意到，当父类的成员函数需要被子类重写时，
    // 父类中的函数必须被显式声明为_虚函数（virtual）_。
    // 考虑到性能方面的因素，函数默认情况下不会被声明为虚函数。
    virtual void print() const;

    // 函数也可以在class body内部定义。
    // 这样定义的函数会自动成为内联函数。
    void bark() const { std::cout << name << " barks!\n" }

    // 除了构造器以外，C++还提供了析构器。
    // 当一个对象被删除或者脱离其定义域时，它的析构函数会被调用。
    // 这使得RAII这样的强大范式（参见下文）成为可能。
    // 为了衍生出子类来，基类的析构函数必须定义为虚函数。
    virtual ~Dog();

}; // 在类的定义之后，要加一个分号

// 类的成员函数通常在.cpp文件中实现。
void Dog::Dog()
{
    std::cout << "A dog has been constructed\n";
}

// 对象（例如字符串）应当以引用的形式传递，
// 对于不需要修改的对象，最好使用常量引用。
void Dog::setName(const std::string& dogsName)
{
    name = dogsName;
}

void Dog::setWeight(int dogsWeight)
{
    weight = dogsWeight;
}

// 虚函数的virtual关键字只需要在声明时使用，不需要在定义时重复
void Dog::print() const
{
    std::cout << "Dog is " << name << " and weighs " << weight << "kg\n";
}

void Dog::~Dog()
{
    std::cout << "Goodbye " << name << "\n";
}

int main() {
    Dog myDog; // 此时显示“A dog has been constructed”
    myDog.setName("Barkley");
    myDog.setWeight(10);
    myDog.print(); // 显示“Dog is Barkley and weighs 10 kg”
    return 0;
} // 显示“Goodbye Barkley”

// 继承：

// 这个类继承了Dog类中的公有（public）和保护（protected）对象
class OwnedDog : public Dog {

    void setOwner(const std::string& dogsOwner)

    // 重写OwnedDogs类的print方法。
    // 如果你不熟悉子类多态的话，可以参考这个页面中的概述：
    // http://zh.wikipedia.org/wiki/%E5%AD%90%E7%B1%BB%E5%9E%8B

    // override关键字是可选的，它确保你所重写的是基类中的方法。
    void print() const override;

private:
    std::string owner;
};

// 与此同时，在对应的.cpp文件里：

void OwnedDog::setOwner(const std::string& dogsOwner)
{
    owner = dogsOwner;
}

void OwnedDog::print() const
{
    Dog::print(); // 调用基类Dog中的print方法
    // "Dog is <name> and weights <weight>"

    std::cout << "Dog is owned by " << owner << "\n";
    // "Dog is owned by <owner>"
}

/////////////////////
// 初始化与运算符重载
/////////////////////

// 在C++中，通过定义一些特殊名称的函数，
// 你可以重载+、-、*、/等运算符的行为。
// 当运算符被使用时，这些特殊函数会被调用，从而实现运算符重载。

#include <iostream>
using namespace std;

class Point {
public:
    // 可以以这样的方式为成员变量设置默认值。
    double x = 0;
    double y = 0;

    // 定义一个默认的构造器。
    // 除了将Point初始化为(0, 0)以外，这个函数什么都不做。
    Point() { };

    // 下面使用的语法称为初始化列表，
    // 这是初始化类中成员变量的正确方式。
    Point (double a, double b) :
        x(a),
        y(b)
    { /* 除了初始化成员变量外，什么都不做 */ }

    // 重载 + 运算符
    Point operator+(const Point& rhs) const;

    // 重载 += 运算符
    Point& operator+=(const Point& rhs);

    // 增加 - 和 -= 运算符也是有意义的，但这里不再赘述。
};

Point Point::operator+(const Point& rhs) const
{
    // 创建一个新的点，
    // 其横纵坐标分别为这个点与另一点在对应方向上的坐标之和。
    return Point(x + rhs.x, y + rhs.y);
}

Point& Point::operator+=(const Point& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

int main () {
    Point up (0,1);
    Point right (1,0);
    // 这里使用了Point类型的运算符“+”
    // 调用up（Point类型）的“+”方法，并以right作为函数的参数
    Point result = up + right;
    // 显示“Result is upright (1,1)”
    cout << "Result is upright (" << result.x << ',' << result.y << ")\n";
    return 0;
}

///////////
// 异常处理
///////////

// 标准库中提供了一些基本的异常类型
// （参见http://en.cppreference.com/w/cpp/error/exception）
// 但是，其他任何类型也可以作为一个异常被拋出
#include <exception>

// 在_try_代码块中拋出的异常可以被随后的_catch_捕获。
try {
    // 不要用 _new_关键字在堆上为异常分配空间。
    throw std::exception("A problem occurred");
}
// 如果拋出的异常是一个对象，可以用常量引用来捕获它
catch (const std::exception& ex)
{
  std::cout << ex.what();
// 捕获尚未被_catch_处理的所有错误
} catch (...)
{
    std::cout << "Unknown exception caught";
    throw; // 重新拋出异常
}

///////
// RAII
///////

// RAII指的是“资源获取就是初始化”（Resource Allocation Is Initialization），
// 它被视作C++中最强大的编程范式之一。
// 简单说来，它指的是，用构造函数来获取一个对象的资源，
// 相应的，借助析构函数来释放对象的资源。

// 为了理解这一范式的用处，让我们考虑某个函数使用文件句柄时的情况：
void doSomethingWithAFile(const char* filename)
{
    // 首先，让我们假设一切都会顺利进行。

    FILE* fh = fopen(filename, "r"); // 以只读模式打开文件

    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

    fclose(fh); // 关闭文件句柄
}

// 不幸的是，随着错误处理机制的引入，事情会变得复杂。
// 假设fopen函数有可能执行失败，
// 而doSomethingWithTheFile和doSomethingElseWithIt会在失败时返回错误代码。
// （虽然异常是C++中处理错误的推荐方式，
// 但是某些程序员，尤其是有C语言背景的，并不认可异常捕获机制的作用）。
// 现在，我们必须检查每个函数调用是否成功执行，并在问题发生的时候关闭文件句柄。
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // 以只读模式打开文件
    if (fh == nullptr) // 当执行失败是，返回的指针是nullptr
        return false; // 向调用者汇报错误

    // 假设每个函数会在执行失败时返回false
    if (!doSomethingWithTheFile(fh)) {
        fclose(fh); // 关闭文件句柄，避免造成内存泄漏。
        return false; // 反馈错误
    }
    if (!doSomethingElseWithIt(fh)) {
        fclose(fh); // 关闭文件句柄
        return false; // 反馈错误
    }

    fclose(fh); // 关闭文件句柄
    return true; // 指示函数已成功执行
}

// C语言的程序员通常会借助goto语句简化上面的代码：
bool doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r");
    if (fh == nullptr)
        return false;

    if (!doSomethingWithTheFile(fh))
        goto failure;

    if (!doSomethingElseWithIt(fh))
        goto failure;

    fclose(fh); // 关闭文件
    return true; // 执行成功

failure:
    fclose(fh);
    return false; // 反馈错误
}

// 如果用异常捕获机制来指示错误的话，
// 代码会变得清晰一些，但是仍然有优化的余地。
void doSomethingWithAFile(const char* filename)
{
    FILE* fh = fopen(filename, "r"); // 以只读模式打开文件
    if (fh == nullptr)
        throw std::exception("Could not open the file.");

    try {
        doSomethingWithTheFile(fh);
        doSomethingElseWithIt(fh);
    }
    catch (...) {
        fclose(fh); // 保证出错的时候文件被正确关闭
        throw; // 之后，重新抛出这个异常
    }

    fclose(fh); // 关闭文件
    // 所有工作顺利完成
}

// 相比之下，使用C++中的文件流类（fstream）时，
// fstream会利用自己的析构器来关闭文件句柄。
// 只要离开了某一对象的定义域，它的析构函数就会被自动调用。
void doSomethingWithAFile(const std::string& filename)
{
    // ifstream是输入文件流（input file stream）的简称
    std::ifstream fh(filename); // 打开一个文件

    // 对文件进行一些操作
    doSomethingWithTheFile(fh);
    doSomethingElseWithIt(fh);

} // 文件已经被析构器自动关闭

// 与上面几种方式相比，这种方式有着_明显_的优势：
// 1. 无论发生了什么情况，资源（此例当中是文件句柄）都会被正确关闭。
//    只要你正确使用了析构器，就_不会_因为忘记关闭句柄，造成资源的泄漏。
// 2. 可以注意到，通过这种方式写出来的代码十分简洁。
//    析构器会在后台关闭文件句柄，不再需要你来操心这些琐事。
// 3. 这种方式的代码具有异常安全性。
//    无论在函数中的何处拋出异常，都不会阻碍对文件资源的释放。

// 地道的C++代码应当把RAII的使用扩展到各种类型的资源上，包括：
// - 用unique_ptr和shared_ptr管理的内存
// - 各种数据容器，例如标准库中的链表、向量（容量自动扩展的数组）、散列表等；
//   当它们脱离作用域时，析构器会自动释放其中储存的内容。
// - 用lock_guard和unique_lock实现的互斥
```
扩展阅读：

<http://cppreference.com/w/cpp> 提供了最新的语法参考。

可以在 <http://cplusplus.com> 找到一些补充资料。
---
language: c
filename: learnc-cn.c
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Chenbo Li", "http://binarythink.net/"]
    - ["Jakukyo Friel", "http://weakish.github.io"]
lang: zh-cn
---

C语言在今天仍然是高性能计算的主要选择。

C大概是大多数程序员用到的最接近底层的语言了，C语言原生的速度就很高了，但是别忘了C的手动内存管理，它会让你将性能发挥到极致。

```c
// 单行注释以//开始。（仅适用于C99或更新的版本。）

/*
多行注释是这个样子的。（C89也适用。）
*/

// 常数： #define 关键词
#define DAYS_IN_YEAR 365

// 以枚举的方式定义常数
enum days {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
// MON自动被定义为2，TUE被定义为3，以此类推。

// 用#include来导入头文件
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// <尖括号>间的文件名是C标准库的头文件。
// 标准库以外的头文件，使用双引号代替尖括号。
#include "my_header.h"

// 函数的签名可以事先在.h文件中定义，
// 也可以直接在.c文件的头部定义。
void function_1(char c);
void function_2(void);

// 如果函数出现在main()之后，那么必须在main()之前
// 先声明一个函数原型
int add_two_ints(int x1, int x2); // 函数原型

// 你的程序的入口是一个返回值为整型的main函数
int main() {

// 用printf打印到标准输出，可以设定格式，
// %d 代表整数, \n 代表换行
printf("%d\n", 0); // => 打印 0
// 所有的语句都要以分号结束

///////////////////////////////////////
// 类型
///////////////////////////////////////

// 在使用变量之前我们必须先声明它们。
// 变量在声明时需要指明其类型，而类型能够告诉系统这个变量所占用的空间

// int型（整型）变量一般占用4个字节
int x_int = 0;

// short型（短整型）变量一般占用2个字节
short x_short = 0;

// char型（字符型）变量会占用1个字节
char x_char = 0;
char y_char = 'y'; // 字符变量的字面值需要用单引号包住

// long型（长整型）一般需要4个字节到8个字节; 而long long型则至少需要8个字节（64位）

long x_long = 0;
long long x_long_long = 0; 

// float一般是用32位表示的浮点数字
float x_float = 0.0;

// double一般是用64位表示的浮点数字
double x_double = 0.0;

// 整数类型也可以有无符号的类型表示。这样这些变量就无法表示负数
// 但是无符号整数所能表示的范围就可以比原来的整数大一些

unsigned short ux_short;
unsigned int ux_int;
unsigned long long ux_long_long;

// 单引号内的字符是机器的字符集中的整数。
'0' // => 在ASCII字符集中是48
'A' // => 在ASCII字符集中是65

// char类型一定会占用1个字节，但是其他的类型却会因具体机器的不同而各异
// sizeof(T) 可以返回T类型在运行的机器上占用多少个字节 
// 这样你的代码就可以在各处正确运行了
// sizeof(obj)返回表达式（变量、字面量等）的尺寸
printf("%zu\n", sizeof(int)); // => 4 (大多数的机器字长为4)

// 如果`sizeof`的参数是一个表达式，那么这个参数不会被演算（VLA例外，见下）
// 它产生的值是编译期的常数
int a = 1;
// size_t是一个无符号整型，表示对象的尺寸，至少2个字节
size_t size = sizeof(a++); // a++ 不会被演算
printf("sizeof(a++) = %zu where a = %d\n", size, a);
// 打印 "sizeof(a++) = 4 where a = 1" （在32位架构上）

// 数组必须要被初始化为具体的长度
char my_char_array[20]; // 这个数组占据 1 * 20 = 20 个字节
int my_int_array[20]; // 这个数组占据 4 * 20 = 80 个字节
                      // (这里我们假设字长为4)


// 可以用下面的方法把数组初始化为0:
char my_array[20] = {0};

// 索引数组和其他语言类似 -- 好吧，其实是其他的语言像C
my_array[0]; // => 0

// 数组是可变的，其实就是内存的映射！
my_array[1] = 2;
printf("%d\n", my_array[1]); // => 2

// 在C99 （C11中是可选特性），变长数组（VLA）也可以声明长度。
// 其长度不用是编译期常量。
printf("Enter the array size: "); // 询问用户数组长度
char buf[0x100];
fgets(buf, sizeof buf, stdin);

// stroul 将字符串解析为无符号整数
size_t size = strtoul(buf, NULL, 10);
int var_length_array[size]; // 声明VLA
printf("sizeof array = %zu\n", sizeof var_length_array);

// 上述程序可能的输出为：
// > Enter the array size: 10
// > sizeof array = 40

// 字符串就是以 NUL (0x00) 这个字符结尾的字符数组,
// NUL可以用'\0'来表示.
// (在字符串字面量中我们不必输入这个字符，编译器会自动添加的)
char a_string[20] = "This is a string";
printf("%s\n", a_string); // %s 可以对字符串进行格式化
/*
也许你会注意到 a_string 实际上只有16个字节长.
第17个字节是一个空字符(NUL) 
而第18, 19 和 20 个字符的值是未定义。
*/

printf("%d\n", a_string[16]); // => 0
//  byte #17值为0（18，19，20同样为0）

// 单引号间的字符是字符字面量
// 它的类型是`int`，而 *不是* `char`
// （由于历史原因）
int cha = 'a'; // 合法
char chb = 'a'; // 同样合法 (隐式类型转换

// 多维数组
int multi_array[2][5] = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 0}
    }
// 获取元素
int array_int = multi_array[0][2]; // => 3

///////////////////////////////////////
// 操作符
///////////////////////////////////////

// 多个变量声明的简写
int i1 = 1, i2 = 2;
float f1 = 1.0, f2 = 2.0;

int a, b, c;
a = b = c = 0;

// 算数运算直截了当
i1 + i2; // => 3
i2 - i1; // => 1
i2 * i1; // => 2
i1 / i2; // => 0 (0.5，但会被化整为 0)

f1 / f2; // => 0.5, 也许会有很小的误差
// 浮点数和浮点数运算都是近似值

// 取余运算
11 % 3; // => 2

// 你多半会觉得比较操作符很熟悉, 不过C中没有布尔类型
// 而是用整形替代
// (C99中有_Bool或bool。)
// 0为假, 其他均为真. (比较操作符的返回值总是返回0或1)
3 == 2; // => 0 (false)
3 != 2; // => 1 (true)
3 > 2; // => 1
3 < 2; // => 0
2 <= 2; // => 1
2 >= 2; // => 1

// C不是Python —— 连续比较不合法
int a = 1;
// 错误
int between_0_and_2 = 0 < a < 2;
// 正确
int between_0_and_2 = 0 < a && a < 2;

// 逻辑运算符适用于整数
!3; // => 0 (非)
!0; // => 1
1 && 1; // => 1 (且)
0 && 1; // => 0
0 || 1; // => 1 (或)
0 || 0; // => 0

// 条件表达式 （ ? : ）
int a = 5;
int b = 10;
int z;
z = (a > b) ? a : b; //  10 “若a > b返回a，否则返回b。”

// 增、减
char *s = "iLoveC"
int j = 0;
s[j++]; // "i" 返回s的第j项，然后增加j的值。
j = 0;
s[++j]; // => "L"  增加j的值，然后返回s的第j项。
// j-- 和 --j 同理

// 位运算
~0x0F; // => 0xF0 (取反)
0x0F & 0xF0; // => 0x00 (和)
0x0F | 0xF0; // => 0xFF (或)
0x04 ^ 0x0F; // => 0x0B (异或)
0x01 << 1; // => 0x02 (左移1位)
0x02 >> 1; // => 0x01 (右移1位)

// 对有符号整数进行移位操作要小心 —— 以下未定义：
// 有符号整数位移至符号位 int a = 1 << 32
// 左移位一个负数 int a = -1 << 2
// 移位超过或等于该类型数值的长度
// int a = 1 << 32; // 假定int32位


///////////////////////////////////////
// 控制结构
///////////////////////////////////////

if (0) {
  printf("I am never run\n");
} else if (0) {
  printf("I am also never run\n");
} else {
  printf("I print\n");
}

// While循环
int ii = 0;
while (ii < 10) { // 任何非0的值均为真
    printf("%d, ", ii++); // ii++ 在取值过后自增
} // =>  打印 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

printf("\n");

int kk = 0;
do {
    printf("%d, ", kk);
} while (++kk < 10); // ++kk 先自增，再被取值
// => 打印 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

printf("\n");

// For 循环
int jj;
for (jj=0; jj < 10; jj++) {
    printf("%d, ", jj);
} // => 打印 "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "

printf("\n");

// *****注意*****:
// 循环和函数必须有主体部分，如果不需要主体部分：
int i;
    for (i = 0; i <= 5; i++) {
    ; // 使用分号表达主体（null语句）
}

// 多重分支：switch()
switch (some_integral_expression) {
case 0: // 标签必须是整数常量表达式
    do_stuff();
    break; // 如果不使用break，控制结构会继续执行下面的标签
case 1:
    do_something_else();
    break;
default:
    // 假设 `some_integral_expression` 不匹配任何标签
    fputs("error!\n", stderr);
    exit(-1);
    break;
    }

///////////////////////////////////////
// 类型转换
///////////////////////////////////////

// 在C中每个变量都有类型，你可以将变量的类型进行转换
// (有一定限制)

int x_hex = 0x01; // 可以用16进制字面量赋值

// 在类型转换时，数字本身的值会被保留下来
printf("%d\n", x_hex); // => 打印 1
printf("%d\n", (short) x_hex); // => 打印 1
printf("%d\n", (char) x_hex); // => 打印 1

// 类型转换时可能会造成溢出，而且不会抛出警告
printf("%d\n", (char) 257); // => 1 (char的最大值为255，假定char为8位长)

// 使用<limits.h>提供的CHAR_MAX、SCHAR_MAX和UCHAR_MAX宏可以确定`char`、`signed_char`和`unisigned char`的最大值。


// 整数型和浮点型可以互相转换
printf("%f\n", (float)100); // %f 格式化单精度浮点
printf("%lf\n", (double)100); // %lf 格式化双精度浮点
printf("%d\n", (char)100.0);

///////////////////////////////////////
// 指针
///////////////////////////////////////

// 指针变量是用来储存内存地址的变量
// 指针变量的声明也会告诉它所指向的数据的类型
// 你可以使用得到你的变量的地址，并把它们搞乱，;-)

int x = 0;
printf("%p\n", &x); // 用 & 来获取变量的地址
// (%p 格式化一个类型为 void *的指针)
// => 打印某个内存地址

// 指针类型在声明中以*开头
int* px, not_a_pointer; // px是一个指向int型的指针
px = &x; // 把x的地址保存到px中
printf("%p\n", (void *)px); // => 输出内存中的某个地址
printf("%zu, %zu\n", sizeof(px), sizeof(not_a_pointer));
// => 在64位系统上打印“8， 4”。

// 要得到某个指针指向的内容的值，可以在指针前加一个*来取得（取消引用）
// 注意： 是的，这可能让人困惑，'*'在用来声明一个指针的同时取消引用它。
printf("%d\n", *px); // => 输出 0, 即x的值

// 你也可以改变指针所指向的值
// 此时你需要取消引用上添加括号，因为++比*的优先级更高
(*px)++; // 把px所指向的值增加1
printf("%d\n", *px); // => 输出 1
printf("%d\n", x); // => 输出 1

// 数组是分配一系列连续空间的常用方式
int x_array[20];
int xx;
for (xx=0; xx<20; xx++) {
    x_array[xx] = 20 - xx;
} // 初始化 x_array 为 20, 19, 18,... 2, 1

// 声明一个整型的指针，并初始化为指向x_array
int* x_ptr = x_array;
// x_ptr现在指向了数组的第一个元素(即整数20). 
// 这是因为数组通常衰减为指向它们的第一个元素的指针。
// 例如，当一个数组被传递给一个函数或者绑定到一个指针时，
//它衰减为(隐式转化为）一个指针。
// 例外： 当数组是`&`操作符的参数：
int arr[10];
int (*ptr_to_arr)[10] = &arr; // &arr的类型不是`int *`！
                              // 它的类型是指向数组的指针（数组由10个int组成）
// 或者当数组是字符串字面量（初始化字符数组）
char arr[] = "foobarbazquirk";
// 或者当它是`sizeof`或`alignof`操作符的参数时：
int arr[10];
int *ptr = arr; // 等价于 int *ptr = &arr[0];
printf("%zu, %zu\n", sizeof arr, sizeof ptr); // 应该会输出"40, 4"或"40, 8"

// 指针的增减多少是依据它本身的类型而定的
// （这被称为指针算术）
printf("%d\n", *(x_ptr + 1)); // => 打印 19
printf("%d\n", x_array[1]); // => 打印 19

// 你也可以通过标准库函数malloc来实现动态分配
// 这个函数接受一个代表容量的参数，参数类型为`size_t`
// 系统一般会从堆区分配指定容量字节大小的空间
// （在一些系统，例如嵌入式系统中这点不一定成立
// C标准对此未置一词。）
int *my_ptr = malloc(sizeof(*my_ptr) * 20);
for (xx=0; xx<20; xx++) {
    *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx
} // 初始化内存为 20, 19, 18, 17... 2, 1 (类型为int）

// 对未分配的内存进行取消引用会产生未定义的结果
printf("%d\n", *(my_ptr + 21)); // => 谁知道会输出什么

// malloc分配的区域需要手动释放
// 否则没人能够再次使用这块内存，直到程序结束为止
free(my_ptr);

// 字符串通常是字符数组，但是经常用字符指针表示
// (它是指向数组的第一个元素的指针)
// 一个优良的实践是使用`const char *`来引用一个字符串字面量，
// 因为字符串字面量不应当被修改（即"foo"[0] = 'a'犯了大忌）
const char* my_str = "This is my very own string";
printf("%c\n", *my_str); // => 'T'

// 如果字符串是数组，（多半是用字符串字面量初始化的）
// 情况就不一样了，字符串位于可写的内存中
char foo[] = "foo";
foo[0] = 'a'; // 这是合法的，foo现在包含"aoo"

function_1();
} // main函数结束

///////////////////////////////////////
// 函数
///////////////////////////////////////

// 函数声明语法:
// <返回值类型> <函数名称>(<参数>)

int add_two_ints(int x1, int x2){
    return x1 + x2; // 用return来返回一个值
}

/*
函数是按值传递的。当调用一个函数的时候，传递给函数的参数
是原有值的拷贝（数组除外）。你在函数内对参数所进行的操作
不会改变该参数原有的值。

但是你可以通过指针来传递引用，这样函数就可以更改值

例子：字符串本身翻转
*/

// 类型为void的函数没有返回值
void str_reverse(char *str_in){
    char tmp;
    int ii = 0;
    size_t len = strlen(str_in); // `strlen()`` 是C标准库函数
    for(ii = 0; ii < len / 2; ii++){
        tmp = str_in[ii];
        str_in[ii] = str_in[len - ii - 1]; // 从倒数第ii个开始
        str_in[len - ii - 1] = tmp;
    }
}

/*
char c[] = "This is a test.";
str_reverse(c);
printf("%s\n", c); // => ".tset a si sihT"
*/

// 如果引用函数之外的变量，必须使用extern关键字
int i = 0;
void testFunc() {
    extern int i; // 使用外部变量 i
}

// 使用static确保external变量为源文件私有
static int i = 0; // 其他使用 testFunc()的文件无法访问变量i
void testFunc() {
    extern int i;
}
//**你同样可以声明函数为static**


///////////////////////////////////////
// 用户自定义类型和结构
///////////////////////////////////////

// Typedefs可以创建类型别名
typedef int my_type;
my_type my_type_var = 0;

// struct是数据的集合，成员依序分配，按照
// 编写的顺序
struct rectangle {
    int width;
    int height;
};

// 一般而言，以下断言不成立：
// sizeof(struct rectangle) == sizeof(int) + sizeof(int)
//这是因为structure成员之间可能存在潜在的间隙（为了对齐）[1]

void function_1(){

    struct rectangle my_rec;

    // 通过 . 来访问结构中的数据
    my_rec.width = 10;
    my_rec.height = 20;

    // 你也可以声明指向结构体的指针
    struct rectangle *my_rec_ptr = &my_rec;

    // 通过取消引用来改变结构体的成员...
    (*my_rec_ptr).width = 30;

    // ... 或者用 -> 操作符作为简写提高可读性
    my_rec_ptr->height = 10; // Same as (*my_rec_ptr).height = 10;
}

// 你也可以用typedef来给一个结构体起一个别名
typedef struct rectangle rect;

int area(rect r){
    return r.width * r.height;
}

// 如果struct较大，你可以通过指针传递，避免
// 复制整个struct。
int area(const rect *r)
{
    return r->width * r->height;
}

///////////////////////////////////////
// 函数指针
///////////////////////////////////////
/*
在运行时，函数本身也被存放到某块内存区域当中
函数指针就像其他指针一样（不过是存储一个内存地址） 但却可以被用来直接调用函数,
并且可以四处传递回调函数
但是，定义的语法初看令人有些迷惑

例子：通过指针调用str_reverse
*/
void str_reverse_through_pointer(char *str_in) {
    // 定义一个函数指针 f. 
    void (*f)(char *); // 签名一定要与目标函数相同
    f = &str_reverse; // 将函数的地址在运行时赋给指针
    (*f)(str_in); // 通过指针调用函数
    // f(str_in); // 等价于这种调用方式
}

/*
只要函数签名是正确的，任何时候都能将任何函数赋给某个函数指针
为了可读性和简洁性，函数指针经常和typedef搭配使用：
*/

typedef void (*my_fnp_type)(char *);

// 实际声明函数指针会这么用:
// ...
// my_fnp_type f; 

// 特殊字符
'\a' // bell
'\n' // 换行
'\t' // tab
'\v' // vertical tab
'\f' // formfeed
'\r' // 回车
'\b' // 退格
'\0' // null，通常置于字符串的最后。
     //   hello\n\0. 按照惯例，\0用于标记字符串的末尾。
'\\' // 反斜杠
'\?' // 问号
'\'' // 单引号
'\"' // 双引号
'\xhh' // 十六进制数字. 例子: '\xb' = vertical tab
'\ooo' // 八进制数字. 例子: '\013' = vertical tab

// 打印格式：
"%d"    // 整数
"%3d"   // 3位以上整数 （右对齐文本）
"%s"    // 字符串
"%f"    // float
"%ld"   // long
"%3.2f" // 左3位以上、右2位以上十进制浮
"%7.4s" // (字符串同样适用)
"%c"    // 字母
"%p"    // 指针
"%x"    // 十六进制
"%o"    // 八进制
"%%"    // 打印 %

///////////////////////////////////////
// 演算优先级
///////////////////////////////////////
//---------------------------------------------------//
//        操作符                     | 组合          //
//---------------------------------------------------//
// () [] -> .                        | 从左到右      //
// ! ~ ++ -- + = *(type)sizeof       | 从右到左      //
// * / %                             | 从左到右      //
// + -                               | 从左到右      //
// << >>                             | 从左到右      //
// < <= > >=                         | 从左到右      //
// == !=                             | 从左到右      //
// &                                 | 从左到右      //
// ^                                 | 从左到右      //
// |                                 | 从左到右      //
// &&                                | 从左到右      //
// ||                                | 从左到右      //
// ?:                                | 从右到左      //
// = += -= *= /= %= &= ^= |= <<= >>= | 从右到左      //
// ,                                 | 从左到右      //
//---------------------------------------------------//

```

## 更多阅读

最好找一本 [K&R, aka "The C Programming Language", “C程序设计语言”](https://en.wikipedia.org/wiki/The_C_Programming_Language)。它是关于C最重要的一本书，由C的创作者撰写。不过需要留意的是它比较古老了，因此有些不准确的地方。


另一个比较好的资源是 [Learn C the hard way](http://c.learncodethehardway.org/book/)

如果你有问题，请阅读[compl.lang.c Frequently Asked Questions](http://c-faq.com/)。

使用合适的空格、缩进，保持一致的代码风格非常重要。可读性强的代码比聪明的代码、高速的代码更重要。可以参考下[Linux内核编码风格](https://www.kernel.org/doc/Documentation/CodingStyle)
。
除了这些，多多Google吧

[1] http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member
---
language: clojure
filename: learnclojure-cn.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Bill Zhang", "http://jingege.github.io/"]
lang: zh-cn
---

Clojure是运行在JVM上的Lisp家族中的一员。她比Common Lisp更强调纯[函数式编程](https://en.wikipedia.org/wiki/Functional_programming)，且自发布时便包含了一组工具来处理状态。

这种组合让她能十分简单且自动地处理并发问题。

(你需要使用Clojure 1.2或更新的发行版)

```clojure
; 注释以分号开始。

; Clojure代码由一个个form组成， 即写在小括号里的由空格分开的一组语句。
; Clojure解释器会把第一个元素当做一个函数或者宏来调用，其余的被认为是参数。

; Clojure代码的第一条语句一般是用ns来指定当前的命名空间。
(ns learnclojure)

; 更基本的例子:

; str会使用所有参数来创建一个字符串
(str "Hello" " " "World") ; => "Hello World"

; 数学计算比较直观
(+ 1 1) ; => 2
(- 2 1) ; => 1
(* 1 2) ; => 2
(/ 2 1) ; => 2

; 等号是 =
(= 1 1) ; => true
(= 2 1) ; => false

; 逻辑非
(not true) ; => false

; 嵌套的form工作起来应该和你预想的一样
(+ 1 (- 3 2)) ; = 1 + (3 - 2) => 2

; 类型
;;;;;;;;;;;;;

; Clojure使用Java的Object来描述布尔值、字符串和数字
; 用函数 `class` 来查看具体的类型
(class 1) ; 整形默认是java.lang.Long类型
(class 1.); 浮点默认是java.lang.Double类型的
(class ""); String是java.lang.String类型的，要用双引号引起来
(class false) ; 布尔值是java.lang.Boolean类型的
(class nil); "null"被称作nil

; 如果你想创建一组数据字面量，用单引号(')来阻止form被解析和求值
'(+ 1 2) ; => (+ 1 2)
; (单引号是quote的简写形式，故上式等价于(quote (+ 1 2)))

; 可以对一个引用列表求值
(eval '(+ 1 2)) ; => 3

; 集合（Collection）和序列
;;;;;;;;;;;;;;;;;;;

; List的底层实现是链表，Vector的底层实现是数组
; 二者也都是java类
(class [1 2 3]); => clojure.lang.PersistentVector
(class '(1 2 3)); => clojure.lang.PersistentList

; list本可以写成(1 2 3), 但必须用引用来避免被解释器当做函数来求值。
; (list 1 2 3)等价于'(1 2 3)

; 集合其实就是一组数据
; List和Vector都是集合:
(coll? '(1 2 3)) ; => true
(coll? [1 2 3]) ; => true

; 序列 (seqs) 是数据列表的抽象描述
; 只有列表才可称作序列。
(seq? '(1 2 3)) ; => true
(seq? [1 2 3]) ; => false

; 序列被访问时只需要提供一个值，所以序列可以被懒加载——也就意味着可以定义一个无限序列：
(range 4) ; => (0 1 2 3)
(range) ; => (0 1 2 3 4 ...) (无限序列)
(take 4 (range)) ;  (0 1 2 3)

; cons用以向列表或向量的起始位置添加元素
(cons 4 [1 2 3]) ; => (4 1 2 3)
(cons 4 '(1 2 3)) ; => (4 1 2 3)

; conj将以最高效的方式向集合中添加元素。
; 对于列表，数据会在起始位置插入，而对于向量，则在末尾位置插入。
(conj [1 2 3] 4) ; => [1 2 3 4]
(conj '(1 2 3) 4) ; => (4 1 2 3)

; 用concat来合并列表或向量
(concat [1 2] '(3 4)) ; => (1 2 3 4)

; 用filter来过滤集合中的元素，用map来根据指定的函数来映射得到一个新的集合
(map inc [1 2 3]) ; => (2 3 4)
(filter even? [1 2 3]) ; => (2)

; recuce使用函数来规约集合
(reduce + [1 2 3 4])
; = (+ (+ (+ 1 2) 3) 4)
; => 10

; reduce还能指定一个初始参数
(reduce conj [] '(3 2 1))
; = (conj (conj (conj [] 3) 2) 1)
; => [3 2 1]

; 函数
;;;;;;;;;;;;;;;;;;;;;

; 用fn来创建函数。函数的返回值是最后一个表达式的值
(fn [] "Hello World") ; => fn

; (你需要再嵌套一组小括号来调用它)
((fn [] "Hello World")) ; => "Hello World"

; 你可以用def来创建一个变量（var）
(def x 1)
x ; => 1

; 将函数定义为一个变量（var）
(def hello-world (fn [] "Hello World"))
(hello-world) ; => "Hello World"

; 你可用defn来简化函数的定义
(defn hello-world [] "Hello World")

; 中括号内的内容是函数的参数。
(defn hello [name]
  (str "Hello " name))
(hello "Steve") ; => "Hello Steve"

; 你还可以用这种简写的方式来创建函数：
(def hello2 #(str "Hello " %1))
(hello2 "Fanny") ; => "Hello Fanny"

; 函数也可以有多个参数列表。
(defn hello3
  ([] "Hello World")
  ([name] (str "Hello " name)))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"

; 可以定义变参函数，即把&后面的参数全部放入一个序列
(defn count-args [& args]
  (str "You passed " (count args) " args: " args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"

; 可以混用定参和变参（用&来界定）
(defn hello-count [name & args]
  (str "Hello " name ", you passed " (count args) " extra args"))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"


; 哈希表
;;;;;;;;;;

; 基于hash的map和基于数组的map（即arraymap）实现了相同的接口，hashmap查询起来比较快，
; 但不保证元素的顺序。
(class {:a 1 :b 2 :c 3}) ; => clojure.lang.PersistentArrayMap
(class (hash-map :a 1 :b 2 :c 3)) ; => clojure.lang.PersistentHashMap

; arraymap在足够大的时候，大多数操作会将其自动转换成hashmap，
; 所以不用担心(对大的arraymap的查询性能)。

; map支持很多类型的key，但推荐使用keyword类型
; keyword类型和字符串类似，但做了一些优化。
(class :a) ; => clojure.lang.Keyword

(def stringmap {"a" 1, "b" 2, "c" 3})
stringmap  ; => {"a" 1, "b" 2, "c" 3}

(def keymap {:a 1, :b 2, :c 3})
keymap ; => {:a 1, :c 3, :b 2}

; 顺便说一下，map里的逗号是可有可无的，作用只是提高map的可读性。

; 从map中查找元素就像把map名作为函数调用一样。
(stringmap "a") ; => 1
(keymap :a) ; => 1

; 可以把keyword写在前面来从map中查找元素。
(:b keymap) ; => 2

; 但不要试图用字符串类型的key来这么做。
;("a" stringmap)
; => Exception: java.lang.String cannot be cast to clojure.lang.IFn

; 查找不存在的key会返回nil。
(stringmap "d") ; => nil

; 用assoc函数来向hashmap里添加元素
(def newkeymap (assoc keymap :d 4))
newkeymap ; => {:a 1, :b 2, :c 3, :d 4}

; 但是要记住的是clojure的数据类型是不可变的！
keymap ; => {:a 1, :b 2, :c 3}

; 用dissoc来移除元素
(dissoc keymap :a :b) ; => {:c 3}

; 集合（Set）
;;;;;;

(class #{1 2 3}) ; => clojure.lang.PersistentHashSet
(set [1 2 3 1 2 3 3 2 1 3 2 1]) ; => #{1 2 3}

; 用conj新增元素
(conj #{1 2 3} 4) ; => #{1 2 3 4}

; 用disj移除元素
(disj #{1 2 3} 1) ; => #{2 3}

; 把集合当做函数调用来检查元素是否存在:
(#{1 2 3} 1) ; => 1
(#{1 2 3} 4) ; => nil

; 在clojure.sets模块下有很多相关函数。

; 常用的form
;;;;;;;;;;;;;;;;;

; clojure里的逻辑控制结构都是用宏（macro）实现的，这在语法上看起来没什么不同。
(if false "a" "b") ; => "b"
(if false "a") ; => nil

; 用let来创建临时的绑定变量。
(let [a 1 b 2]
  (> a b)) ; => false

; 用do将多个语句组合在一起依次执行
(do
  (print "Hello")
  "World") ; => "World" (prints "Hello")

; 函数定义里有一个隐式的do
(defn print-and-say-hello [name]
  (print "Saying hello to " name)
  (str "Hello " name))
(print-and-say-hello "Jeff") ;=> "Hello Jeff" (prints "Saying hello to Jeff")

; let也是如此
(let [name "Urkel"]
  (print "Saying hello to " name)
  (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")

; 模块
;;;;;;;;;;;;;;;

; 用use来导入模块里的所有函数
(use 'clojure.set)

; 然后就可以使用set相关的函数了
(intersection #{1 2 3} #{2 3 4}) ; => #{2 3}
(difference #{1 2 3} #{2 3 4}) ; => #{1}

; 你也可以从一个模块里导入一部分函数。
(use '[clojure.set :only [intersection]])

; 用require来导入一个模块
(require 'clojure.string)

; 用/来调用模块里的函数
; 下面是从模块`clojure.string`里调用`blank?`函数。
(clojure.string/blank? "") ; => true

; 在`import`里你可以给模块名指定一个较短的别名。
(require '[clojure.string :as str])
(str/replace "This is a test." #"[a-o]" str/upper-case) ; => "THIs Is A tEst."
; (#""用来表示一个正则表达式)

; 你可以在一个namespace定义里用:require的方式来require（或use，但最好不要用）模块。
; 这样的话你无需引用模块列表。
(ns test
  (:require
    [clojure.string :as str]
    [clojure.set :as set]))

; Java
;;;;;;;;;;;;;;;;;

; Java有大量的优秀的库，你肯定想学会如何用clojure来使用这些Java库。

; 用import来导入java类
(import java.util.Date)

; 也可以在ns定义里导入
(ns test
  (:import java.util.Date
           java.util.Calendar))

; 用类名末尾加`.`的方式来new一个Java对象
(Date.) ; <a date object>

; 用`.`操作符来调用方法，或者用`.method`的简化方式。
(. (Date.) getTime) ; <a timestamp>
(.getTime (Date.)) ; 和上例一样。

; 用`/`调用静态方法
(System/currentTimeMillis) ; <a timestamp> (system is always present)

; 用`doto`来更方便的使用（可变）类。
(import java.util.Calendar)
(doto (Calendar/getInstance)
  (.set 2000 1 1 0 0 0)
  .getTime) ; => A Date. set to 2000-01-01 00:00:00

; STM
;;;;;;;;;;;;;;;;;

; 软件内存事务（Software Transactional Memory）被clojure用来处理持久化的状态。
; clojure里内置了一些结构来使用STM。
; atom是最简单的。给它传一个初始值
(def my-atom (atom {}))

; 用`swap!`更新atom。
; `swap!`会以atom的当前值为第一个参数来调用一个指定的函数，
; `swap`其余的参数作为该函数的第二个参数。
(swap! my-atom assoc :a 1) ; Sets my-atom to the result of (assoc {} :a 1)
(swap! my-atom assoc :b 2) ; Sets my-atom to the result of (assoc {:a 1} :b 2)

; 用`@`读取atom的值
my-atom  ;=> Atom<#...> (返回Atom对象)
@my-atom ; => {:a 1 :b 2}

; 下例是一个使用atom实现的简单计数器
(def counter (atom 0))
(defn inc-counter []
  (swap! counter inc))

(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)
(inc-counter)

@counter ; => 5

; 其他STM相关的结构是ref和agent.
; Refs: http://clojure.org/refs
; Agents: http://clojure.org/agents
```

### 进阶读物

本文肯定不足以讲述关于clojure的一切，但是希望足以让你迈出第一步。

Clojure.org官网有很多文章:
[http://clojure.org/](http://clojure.org/)

Clojuredocs.org有大多数核心函数的文档，还带了示例哦:
[http://clojuredocs.org/quickref/Clojure%20Core](http://clojuredocs.org/quickref/Clojure%20Core)

4Clojure是个很赞的用来练习clojure/FP技能的地方:
[http://www.4clojure.com/](http://www.4clojure.com/)

Clojure-doc.org (你没看错)有很多入门级的文章:
[http://clojure-doc.org/](http://clojure-doc.org/)
---
language: "clojure macros"
filename: learnclojuremacros-zh.clj
contributors:
    - ["Adam Bard", "http://adambard.com/"]
translators:
    - ["Jakukyo Friel", "http://weakish.github.io"]
lang: zh-cn
---

和所有Lisp一样，Clojure内在的[同构性](https://en.wikipedia.org/wiki/Homoiconic)使得你可以穷尽语言的特性，编写生成代码的子过程——“宏”。宏是一种按需调制语言的强大方式。

小心！可以用函数完成的事用宏去实现可不是什么好事。你应该仅在需要控制参数是否或者何时eval的时候使用宏。

你应该熟悉Clojure.确保你了解[Y分钟学Clojure](http://learnxinyminutes.com/docs/zh-cn/clojure-cn/)中的所有内容。

```clojure
;; 使用defmacro定义宏。宏应该输出一个可以作为clojure代码演算的列表。
;;
;; 以下宏的效果和直接写(reverse "Hello World")一致。

(defmacro my-first-macro []
  (list reverse "Hello World"))

;; 使用macroexpand或macroexpand-1查看宏的结果。
;;
;; 注意，调用需要引用。
(macroexpand '(my-first-macro))
;; -> (#<core$reverse clojure.core$reverse@xxxxxxxx> "Hello World")

;; 你可以直接eval macroexpand的结果
(eval (macroexpand '(my-first-macro)))
; -> (\d \l \o \r \W \space \o \l \l \e \H)

;; 不过一般使用以下形式，更简短，更像函数：
(my-first-macro)  ; -> (\d \l \o \r \W \space \o \l \l \e \H)

;; 创建宏的时候可以使用更简短的引用形式来创建列表
(defmacro my-first-quoted-macro []
  '(reverse "Hello World"))

(macroexpand '(my-first-quoted-macro))
;; -> (reverse "Hello World")
;; 注意reverse不再是一个函数对象，而是一个符号。

;; 宏可以传入参数。
(defmacro inc2 [arg]
  (list + 2 arg))

(inc2 2) ; -> 4

;; 不过，如果你尝试配合使用引用列表，会导致错误，
;; 因为参数也会被引用。
;; 为了避免这个问题，clojure提供了引用宏的另一种方式：`
;; 在`之内，你可以使用~获得外圈作用域的变量。
(defmacro inc2-quoted [arg]
  `(+ 2 ~arg))

(inc2-quoted 2)

;; 你可以使用通常的析构参数。用~@展开列表中的变量。
(defmacro unless [arg & body]
  `(if (not ~arg)
     (do ~@body))) ; 别忘了 do!

(macroexpand '(unless true (reverse "Hello World")))

;; ->
;; (if (clojure.core/not true) (do (reverse "Hello World")))

;; 当第一个参数为假时，(unless)会演算、返回主体。 
;; 否则返回nil。

(unless true "Hello") ; -> nil
(unless false "Hello") ; -> "Hello"

;; 需要小心，宏会搞乱你的变量
(defmacro define-x []
  '(do
     (def x 2)
     (list x)))

(def x 4)
(define-x) ; -> (2)
(list x) ; -> (2)

;; 使用gensym来获得独有的标识符
(gensym 'x) ; -> x1281 (or some such thing)

(defmacro define-x-safely []
  (let [sym (gensym 'x)]
    `(do
       (def ~sym 2)
       (list ~sym))))

(def x 4)
(define-x-safely) ; -> (2)
(list x) ; -> (4)

;; 你可以在 ` 中使用 # 为每个符号自动生成gensym
(defmacro define-x-hygenically []
  `(do
     (def x# 2)
     (list x#)))

(def x 4)
(define-x-hygenically) ; -> (2)
(list x) ; -> (4)

;; 通常会配合宏使用帮助函数。
;; 让我们创建一些帮助函数来支持（无聊的）算术语法：

(declare inline-2-helper)
(defn clean-arg [arg]
  (if (seq? arg)
    (inline-2-helper arg)
    arg))

(defn apply-arg
  "Given args [x (+ y)], return (+ x y)"
  [val [op arg]]
  (list op val (clean-arg arg)))

(defn inline-2-helper
  [[arg1 & ops-and-args]]
  (let [ops (partition 2 ops-and-args)]
    (reduce apply-arg (clean-arg arg1) ops)))

;; 在创建宏前，我们可以先测试
(inline-2-helper '(a + (b - 2) - (c * 5))) ; -> (- (+ a (- b 2)) (* c 5))

; 然而，如果我们希望它在编译期执行，就需要创建宏
(defmacro inline-2 [form]
  (inline-2-helper form)))

(macroexpand '(inline-2 (1 + (3 / 2) - (1 / 2) + 1)))
; -> (+ (- (+ 1 (/ 3 2)) (/ 1 2)) 1)

(inline-2 (1 + (3 / 2) - (1 / 2) + 1))
; -> 3 (事实上，结果是3N, 因为数字被转化为带/的有理分数）
```

## 扩展阅读

[Clojure for the Brave and True](http://www.braveclojure.com/)系列的编写宏
http://www.braveclojure.com/writing-macros/

官方文档
http://clojure.org/macros

何时使用宏？
http://dunsmor.com/lisp/onlisp/onlisp_12.html
---
language: coffeescript
contributors:
  - ["Tenor Biel", "http://github.com/L8D"]
  - ["Xavier Yao", "http://github.com/xavieryao"]
translators:
  - ["Xavier Yao", "http://github.com/xavieryao"]
filename: coffeescript-cn.coffee
lang: zh-cn
---

CoffeeScript是逐句编译为JavaScript的一种小型语言，且没有运行时的解释器。
作为JavaScript的替代品之一，CoffeeScript旨在编译人类可读、美观优雅且速度不输原生的代码，
且编译后的代码可以在任何JavaScript运行时正确运行。

参阅 [CoffeeScript官方网站](http://coffeescript.org/)以获取CoffeeScript的完整教程。

``` coffeescript
# CoffeeScript是一种很潮的编程语言，
# 它紧随众多现代编程语言的趋势。
# 因此正如Ruby和Python，CoffeeScript使用井号标记注释。

###
大段落注释以此为例，可以被直接编译为 '/ *' 和 '* /' 包裹的JavaScript代码。

在继续之前你需要了解JavaScript的基本概念。

示例中 => 后为编译后的JavaScript代码
###

# 赋值:
number   = 42 #=> var number = 42;
opposite = true #=> var opposite = true;

# 条件:
number = -42 if opposite #=> if(opposite) { number = -42; }

# 函数:
square = (x) -> x * x #=> var square = function(x) { return x * x; }

fill = (container, liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."
#=>var fill;
#
#fill = function(container, liquid) {
#  if (liquid == null) {
#    liquid = "coffee";
#  }
#  return "Filling the " + container + " with " + liquid + "...";
#};

# 区间:
list = [1..5] #=> var list = [1, 2, 3, 4, 5];

# 对象:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
#=> var math = {
#  "root": Math.sqrt,
#  "square": square,
#  "cube": function(x) { return x * square(x); }
#}

# Splats:
race = (winner, runners...) ->
  print winner, runners
#=>race = function() {
#  var runners, winner;
#  winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
#  return print(winner, runners);
#};

# 存在判断:
alert "I knew it!" if elvis?
#=> if(typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }

# 数组推导:
cubes = (math.cube num for num in list) 
#=>cubes = (function() {
#	var _i, _len, _results;
#	_results = [];
# 	for (_i = 0, _len = list.length; _i < _len; _i++) {
#		num = list[_i];
#		_results.push(math.cube(num));
#	}
#	return _results;
#  })();

foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
#=>foods = ['broccoli', 'spinach', 'chocolate'];
#
#for (_k = 0, _len2 = foods.length; _k < _len2; _k++) {
#  food = foods[_k];
#  if (food !== 'chocolate') {
#    eat(food);
#  }
#}
```
---
language: "Common Lisp"
filename: commonlisp-cn.lisp
contributors:
  - ["Paul Nathan", "https://github.com/pnathan"]
translators:
  - ["Mac David", "http://macdavid313.com"]
  - ["mut0u", "http://github.com/mut0u"]
lang: zh-cn
---

ANSI Common Lisp 是一个广泛通用于各个工业领域的、支持多种范式的编程语言。
这门语言也经常被引用作“可编程的编程语言”（可以写代码的代码）。

免费的经典的入门书籍[《实用 Common Lisp 编程》](http://www.gigamonkeys.com/book/)

许多人都抱怨上面这本书的翻译。[《ANSI Common Lisp》](http://acl.readthedocs.org/en/latest/)也许对中文读者更友好一些。

另外还有一本热门的近期出版的
[Land of Lisp](http://landoflisp.com/).

```common-lisp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 0. 语法
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 一般形式

;; Lisp有两个基本的语法单元：原子（atom），以及S-表达式。
;; 一般的，一组S-表达式被称为“组合式”。

10  ; 一个原子; 它对自身进行求值

:THING ;同样是一个原子；它被求值为一个符号 :thing

t  ;还是一个原子，代表逻辑真值。

(+ 1 2 3 4) ; 一个S-表达式。

'(4 :foo  t)  ;同样是一个S-表达式。


;;; 注释

;; 一个分号开头的注释表示仅用于此行（单行）；两个分号开头的则表示一个所谓标准注释；
;; 三个分号开头的意味着段落注释；
;; 而四个分号开头的注释用于文件头注释（译者注：即对该文件的说明）。

#| 块注释
   可以涵盖多行，而且...
    #|
       他们可以被嵌套！
    |#
|#

;;; 运行环境

;; 有很多不同的Common Lisp的实现；并且大部分的实现是一致（可移植）的。
;; 对于入门学习来说，CLISP是个不错的选择。

;; 可以通过QuickLisp.org的Quicklisp系统管理你的库。

;; 通常，使用文本编辑器和“REPL”来开发Common Lisp；
;; （译者注：“REPL”指读取-求值-打印循环）。
;; “REPL”允许对程序进行交互式的运行、调试，就好像在系统“现场”操作。


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 1. 基本数据类型以及运算符
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 符号

'foo ; => FOO  注意到这个符号被自动转换成大写了。

;; `intern`由一个给定的字符串而创建相应的符号

(intern "AAAA") ; => AAAA

(intern "aaa") ; => |aaa|

;;; 数字
9999999999999999999999 ; 整型数
#b111                  ; 二进制 => 7
#o111                  ; 八进制 => 73
#x111                  ; 十六进制 => 273
3.14159s0              ; 单精度
3.14159d0              ; 双精度
1/2                    ; 分数
#C(1 2)                ; 复数


;; 使用函数时，应当写成这样的形式：(f x y z ...)；
;; 其中，f是一个函数（名），x, y, z为参数；
;; 如果你想创建一个“字面”意义上（即不求值）的列表， 只需使用单引号 ' ，
;; 从而避免接下来的表达式被求值。即，只“引用”这个数据（而不求值）。
'(+ 1 2) ; => (+ 1 2)
;; 你同样也可以手动地调用一个函数（译者注：即使用函数对象来调用函数）：
(funcall #'+ 1 2 3) ; => 6
;; 一些算术运算符
(+ 1 1)              ; => 2
(- 8 1)              ; => 7
(* 10 2)             ; => 20
(expt 2 3)           ; => 8
(mod 5 2)            ; => 1
(/ 35 5)             ; => 7
(/ 1 3)              ; => 1/3
(+ #C(1 2) #C(6 -4)) ; => #C(7 -2)

                     ;;; 布尔运算
t                    ; 逻辑真（任何不是nil的值都被视为真值）
nil                  ; 逻辑假，或者空列表
(not nil)            ; => t
(and 0 t)            ; => t
(or 0 nil)           ; => 0

                     ;;; 字符
#\A                  ; => #\A
#\λ                  ; => #\GREEK_SMALL_LETTER_LAMDA（希腊字母Lambda的小写）
#\u03BB              ; => #\GREEK_SMALL_LETTER_LAMDA（Unicode形式的小写希腊字母Lambda）

;;; 字符串被视为一个定长字符数组
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ;反斜杠用作转义字符

;; 可以拼接字符串
(concatenate 'string "Hello " "world!") ; => "Hello world!"

;; 一个字符串也可被视作一个字符序列
(elt "Apple" 0) ; => #\A

;; `format`被用于格式化字符串
(format nil "~a can be ~a" "strings" "formatted")

;; 利用`format`打印到屏幕上是非常简单的
;;（译者注：注意到第二个参数是t，不同于刚刚的nil）；~% 代表换行符
(format t "Common Lisp is groovy. Dude.~%")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. 变量
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 你可以通过`defparameter`创建一个全局（动态）变量
;; 变量名可以是除了：()[]{}",'`;#|\ 这些字符之外的其他任何字符

;; 动态变量名应该由*号开头与结尾！
;; (译者注：这个只是一个习惯)

(defparameter *some-var* 5)
*some-var* ; => 5

;; 你也可以使用Unicode字符：
(defparameter *AΛB* nil)


;; 访问一个在之前从未被绑定的变量是一种不规范的行为（即使依然是可能发生的）；
;; 不要尝试那样做。


;; 局部绑定：在(let ...)语句内，'me'被绑定到"dance with you"上。
;; `let`总是返回在其作用域内最后一个表达式的值

(let ((me "dance with you"))
  me)
;; => "dance with you"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. 结构体和集合
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 结构体
(defstruct dog name breed age)
(defparameter *rover*
    (make-dog :name "rover"
              :breed "collie"
              :age 5))
*rover* ; => #S(DOG :NAME "rover" :BREED "collie" :AGE 5)

(dog-p *rover*) ; => t  ;; ewww)
(dog-name *rover*) ; => "rover"

;; Dog-p，make-dog，以及 dog-name都是由defstruct创建的！

;;; 点对单元(Pairs)
;; `cons`可用于生成一个点对单元， 利用`car`以及`cdr`将分别得到第一个和第二个元素
(cons 'SUBJECT 'VERB) ; => '(SUBJECT . VERB)
(car (cons 'SUBJECT 'VERB)) ; => SUBJECT
(cdr (cons 'SUBJECT 'VERB)) ; => VERB

;;; 列表

;; 所有列表都是由点对单元构成的“链表”。它以'nil'（或者'()）作为列表的最后一个元素。
(cons 1 (cons 2 (cons 3 nil))) ; => '(1 2 3)
;; `list`是一个生成列表的便利途径
(list 1 2 3) ; => '(1 2 3)
;; 并且，一个引用也可被用做字面意义上的列表值
'(1 2 3) ; => '(1 2 3)

;; 同样的，依然可以用`cons`来添加一项到列表的起始位置
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; 而`append`也可用于连接两个列表
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; 或者使用`concatenate`

(concatenate 'list '(1 2) '(3 4))

;; 列表是一种非常核心的数据类型，所以有非常多的处理列表的函数
;; 例如：
(mapcar #'1+ '(1 2 3))             ; => '(2 3 4)
(mapcar #'+ '(1 2 3) '(10 20 30))  ; => '(11 22 33)
(remove-if-not #'evenp '(1 2 3 4)) ; => '(2 4)
(every #'evenp '(1 2 3 4))         ; => nil
(some #'oddp '(1 2 3 4))           ; => T
(butlast '(subject verb object))   ; => (SUBJECT VERB)


;;; 向量

;; 向量的字面意义是一个定长数组
;;（译者注：此处所谓“字面意义”，即指#(......)的形式，下文还会出现）
#(1 2 3) ; => #(1 2 3)

;; 使用`concatenate`来将两个向量首尾连接在一起
(concatenate 'vector #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; 数组

;; 向量和字符串只不过是数组的特例

;; 二维数组

(make-array (list 2 2))

;; (make-array '(2 2)) 也是可以的

; => #2A((0 0) (0 0))

(make-array (list 2 2 2))

; => #3A(((0 0) (0 0)) ((0 0) (0 0)))

;; 注意：数组的默认初始值是可以指定的
;; 下面是如何指定的示例：

(make-array '(2) :initial-element 'unset)

; => #(UNSET UNSET)

;; 若想获取数组[1][1][1]上的元素：
(aref (make-array (list 2 2 2)) 1 1 1)

; => 0

;;; 变长向量

;; 若将变长向量打印出来，那么它的字面意义上的值和定长向量的是一样的

(defparameter *adjvec* (make-array '(3) :initial-contents '(1 2 3)
      :adjustable t :fill-pointer t))
      
*adjvec* ; => #(1 2 3)

;; 添加新的元素:
(vector-push-extend 4 *adjvec*) ; => 3

*adjvec* ; => #(1 2 3 4)



;;; 不怎么严谨地说，集合也可被视为列表

(set-difference '(1 2 3 4) '(4 5 6 7)) ; => (3 2 1)
(intersection '(1 2 3 4) '(4 5 6 7)) ; => 4
(union '(1 2 3 4) '(4 5 6 7))        ; => (3 2 1 4 5 6 7)
(adjoin 4 '(1 2 3 4))     ; => (1 2 3 4)

;; 然而，你可能想使用一个更好的数据结构，而并非一个链表

;;; 在Common Lisp中，“字典”和哈希表的实现是一样的。

;; 创建一个哈希表
(defparameter *m* (make-hash-table))

;; 给定键，设置对应的值
(setf (gethash 'a *m*) 1)

;; （通过键）检索对应的值
(gethash 'a *m*) ; => 1, t

;; 注意此处有一细节：Common Lisp往往返回多个值。`gethash`返回的第二个值是t，代表找到了这个元素；返回nil表示没有找到这个元素。
;;（译者注：返回的第一个值表示给定的键所对应的值或者nil；）
;;（第二个是一个布尔值，表示在哈希表中是否存在这个给定的键）
;; 例如，如果可以找到给定的键所对应的值，则返回一个t，否则返回nil

;; 由给定的键检索一个不存在的值，则返回nil
;;（译者注：这个nil是第一个nil，第二个nil其实是指该键在哈希表中也不存在）
 (gethash 'd *m*) ;=> nil, nil

;; 给定一个键，你可以指定其对应的默认值：
(gethash 'd *m* :not-found) ; => :NOT-FOUND

;; 在此，让我们看一看怎样处理`gethash`的多个返回值。

(multiple-value-bind
      (a b)
    (gethash 'd *m*)
  (list a b))
; => (NIL NIL)

(multiple-value-bind
      (a b)
    (gethash 'a *m*)
  (list a b))
; => (1 T)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. 函数
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 使用`lambda`来创建一个匿名函数。
;; 一个函数总是返回其形式体内最后一个表达式的值。
;; 将一个函数对象打印出来后的形式是多种多样的...

(lambda () "Hello World") ; => #<FUNCTION (LAMBDA ()) {1004E7818B}>

;; 使用`funcall`来调用lambda函数
(funcall (lambda () "Hello World")) ; => "Hello World"

;; 或者使用`apply`
(apply (lambda () "Hello World") nil) ; => "Hello World"

;; 显式地定义一个函数（译者注：即非匿名的）
(defun hello-world ()
   "Hello World")
(hello-world) ; => "Hello World"

;; 刚刚上面函数名"hello-world"后的()其实是函数的参数列表
(defun hello (name)
   (format nil "Hello, ~a " name))

(hello "Steve") ; => "Hello, Steve"

;; 函数可以有可选形参并且其默认值都为nil

(defun hello (name &optional from)
    (if from
        (format t "Hello, ~a, from ~a" name from)
        (format t "Hello, ~a" name)))

 (hello "Jim" "Alpacas") ;; => Hello, Jim, from Alpacas

;; 你也可以指定那些可选形参的默认值
(defun hello (name &optional (from "The world"))
   (format t "Hello, ~a, from ~a" name from))

(hello "Steve")
; => Hello, Steve, from The world

(hello "Steve" "the alpacas")
; => Hello, Steve, from the alpacas


;; 当然，你也可以设置所谓关键字形参；
;; 关键字形参往往比可选形参更具灵活性。

(defun generalized-greeter (name &key (from "the world") (honorific "Mx"))
    (format t "Hello, ~a ~a, from ~a" honorific name from))

(generalized-greeter "Jim")   ; => Hello, Mx Jim, from the world

(generalized-greeter "Jim" :from "the alpacas you met last summer" :honorific "Mr")
; => Hello, Mr Jim, from the alpacas you met last summer

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. 等式
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Common Lisp具有一个十分复杂的用于判断等价的系统，下面只是其中一部分的例子

;; 若要比较数值是否等价，使用`=`
(= 3 3.0) ; => t
(= 2 1) ; => nil

;; 若要比较对象的类型，则使用`eql`
;;（译者注：抱歉，翻译水平实在有限，下面是我个人的补充说明）
;;（`eq` 返回真，如果对象的内存地址相等）
;;（`eql` 返回真，如果两个对象内存地址相等，或者对象的类型相同，并且值相等）
;;（例如同为整形数或浮点数，并且他们的值相等时，二者`eql`等价）
;;（想要弄清`eql`，其实有必要先了解`eq`)
;;（[可以参考](http://stackoverflow.com/questions/547436/whats-the-difference-between-eq-eql-equal-and-equalp-in-common-lisp)）
;;（可以去CLHS上分别查看两者的文档）
;;（另外，《实用Common Lisp编程》的4.8节也提到了两者的区别）
(eql 3 3) ; => t
(eql 3 3.0) ; => nil
(eql (list 3) (list 3)) ; => nil

;; 对于列表、字符串、以及位向量，使用`equal`
(equal (list 'a 'b) (list 'a 'b)) ; => t
(equal (list 'a 'b) (list 'b 'a)) ; => nil

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. 控制流
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 条件判断语句

(if t                ; “test”，即判断语句
    "this is true"   ; “then”，即判断条件为真时求值的表达式
    "this is false") ; “else”，即判断条件为假时求值的表达式
; => "this is true"

;; 在“test”（判断）语句中，所有非nil或者非()的值都被视为真值
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(GROUCHO ZEPPO)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'YEP

;; `cond`将一系列测试语句串联起来，并对相应的表达式求值
(cond ((> 2 2) (error "wrong!"))
      ((< 2 2) (error "wrong again!"))
      (t 'ok)) ; => 'OK

;; 对于给定值的数据类型，`typecase`会做出相应地判断
(typecase 1
  (string :string)
  (integer :int))

; => :int

;;; 迭代

;; 当然，递归是肯定被支持的：

(defun walker (n)
  (if (zerop n)
      :walked
      (walker (1- n))))

(walker) ; => :walked

;; 而大部分场合下，我们使用`DOLIST`或者`LOOP`来进行迭代


(dolist (i '(1 2 3 4))
  (format t "~a" i))

; => 1234

(loop for i from 0 below 10
      collect i)

; => (0 1 2 3 4 5 6 7 8 9)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. 可变性
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 使用`setf`可以对一个已经存在的变量进行赋值；
;; 事实上，刚刚在哈希表的例子中我们已经示范过了。

(let ((variable 10))
    (setf variable 2))
 ; => 2


;; 所谓好的Lisp编码风格就是为了减少使用破坏性函数，防止发生副作用。

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. 类与对象
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 我们就不写什么有关动物的类了，下面给出的人力车的类

(defclass human-powered-conveyance ()
  ((velocity
    :accessor velocity
    :initarg :velocity)
   (average-efficiency
    :accessor average-efficiency
   :initarg :average-efficiency))
  (:documentation "A human powered conveyance"))

;; `defclass`，后面接类名，以及超类列表
;; 再接着是槽的列表（槽有点像Java里的成员变量），最后是一些可选的特性
;; 例如文档说明“:documentation”

;; 如果超类列表为空，则默认该类继承于“standard-object”类（standard-object又是T的子类）
;; 这种默认行为是可以改变的，但你最好有一定的基础并且知道自己到底在干什么；
;; 参阅《The Art of the Metaobject Protocol》来了解更多信息。

(defclass bicycle (human-powered-conveyance)
  ((wheel-size
    :accessor wheel-size
    :initarg :wheel-size
    :documentation "Diameter of the wheel.")
   (height
    :accessor height
    :initarg :height)))

(defclass recumbent (bicycle)
  ((chain-type
    :accessor chain-type
    :initarg  :chain-type)))

(defclass unicycle (human-powered-conveyance) nil)

(defclass canoe (human-powered-conveyance)
  ((number-of-rowers
    :accessor number-of-rowers
    :initarg :number-of-rowers)))


;; 在REPL中对human-powered-conveyance类调用`DESCRIBE`后结果如下：

(describe 'human-powered-conveyance)

; COMMON-LISP-USER::HUMAN-POWERED-CONVEYANCE
;  [symbol]
;
; HUMAN-POWERED-CONVEYANCE names the standard-class #<STANDARD-CLASS
;                                                    HUMAN-POWERED-CONVEYANCE>:
;  Documentation:
;    A human powered conveyance
;  Direct superclasses: STANDARD-OBJECT
;  Direct subclasses: UNICYCLE, BICYCLE, CANOE
;  Not yet finalized.
;  Direct slots:
;    VELOCITY
;      Readers: VELOCITY
;      Writers: (SETF VELOCITY)
;    AVERAGE-EFFICIENCY
;      Readers: AVERAGE-EFFICIENCY
;      Writers: (SETF AVERAGE-EFFICIENCY)

;; 注意到这些有用的返回信息——Common Lisp一直是一个交互式的系统。

;; 若要定义一个方法；
;; 注意，我们计算自行车轮子周长时使用了这样一个公式：C = d * pi

(defmethod circumference ((object bicycle))
  (* pi (wheel-size object)))

;; pi在Common Lisp中已经是一个内置的常量。

;; 假设我们已经知道了效率值（“efficiency value”）和船桨数大概呈对数关系；
;; 那么效率值的定义应当在构造器/初始化过程中就被完成。

;; 下面是一个Common Lisp构造实例时初始化实例的例子：

(defmethod initialize-instance :after ((object canoe) &rest args)
  (setf (average-efficiency object)  (log (1+ (number-of-rowers object)))))

;; 接着初构造一个实例并检查平均效率...

(average-efficiency (make-instance 'canoe :number-of-rowers 15))
; => 2.7725887


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. 宏
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 宏可以让你扩展语法

;; 例如，Common Lisp并没有自带WHILE循环——所以让我们自己来为他添加一个；
;; 如果按照汇编程序的直觉来看，我们会这样写：

(defmacro while (condition &body body)
    "While `condition` is true, `body` is executed.

`condition` is tested prior to each execution of `body`"
    (let ((block-name (gensym)))
        `(tagbody
           (unless ,condition
               (go ,block-name))
           (progn
           ,@body)
           ,block-name)))

;; 让我们来看看它的高级版本：

(defmacro while (condition &body body)
    "While `condition` is true, `body` is executed.

`condition` is tested prior to each execution of `body`"
  `(loop while ,condition
         do
         (progn
            ,@body)))

;; 然而，在一个比较现代化的编译环境下，这样的WHILE是没有必要的；
;; LOOP形式的循环和这个WHILE同样的好，并且更易于阅读。

;; 注意反引号'`'，逗号','以及'@'这三个符号； 
;; 反引号'`'是一种所谓“quasiquote”的引用类型的运算符，有了它，之后的逗号“,”才有意义。
;; 逗号“,”意味着解除引用（unquote，即开始求值）；
;; “@”符号则表示将当前的参数插入到当前整个列表中。
;;（译者注：要想真正用好、用对这三个符号，需要下一番功夫）
;;（甚至光看《实用 Common Lisp 编程》中关于宏的介绍都是不够的）
;;（建议再去读一读Paul Graham的两本著作《ANSI Common Lisp》和《On Lisp》）

;; 函数`gensym`创建一个唯一的符号——这个符号确保不会出现在其他任何地方。
;; 这样做是因为，宏是在编译期展开的
;; 而在宏中声明的变量名极有可能和常规代码中使用的变量名发生冲突。

;; 可以去《实用 Common Lisp 编程》中阅读更多有关宏的内容。
```


## 拓展阅读

[继续阅读《实用 Common Lisp 编程》一书](http://www.gigamonkeys.com/book/)


## 致谢

非常感谢Scheme社区的人们，我基于他们的成果得以迅速的写出这篇有关Common Lisp的快速入门
同时也感谢
- [Paul Khuong](https://github.com/pkhuong) ，他提出了很多有用的点评。

##译者寄语
“祝福那些将思想镶嵌在重重括号之内的人们。”
---
language: c#
contributors:
    - ["Irfan Charania", "https://github.com/irfancharania"]
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Melvyn Laïly", "http://x2a.yt"]
    - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
translators:
    - ["Jakukyo Friel", "http://weakish.github.io"]
filename: LearnCSharp-cn.cs
lang: zh-cn
---


C#是一个优雅的、类型安全的面向对象语言。使用C#，开发者可以在.NET框架下构建安全、健壮的应用程序。

[更多关于C#的介绍](http://msdn.microsoft.com/en-us/library/vstudio/z1zx9t92.aspx)

```c#
// 单行注释以 // 开始
/*
多行注释是这样的
*/
/// <summary>
/// XML文档注释
/// </summary>

// 声明应用用到的命名空间
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;
using System.IO;

// 定义作用域，将代码组织成包
namespace Learning
{
    // 每个 .cs 文件至少需要包含一个和文件名相同的类
    // 你可以不这么干，但是这样不好。
    public class LearnCSharp
    {
        // 基本语法 -  如果你以前用过 Java 或 C++ 的话，可以直接跳到后文「有趣的特性」
        public static void Syntax() 
        {
            // 使用 Console.WriteLine 打印信息
            Console.WriteLine("Hello World");
            Console.WriteLine(
                "Integer: " + 10 +
                " Double: " + 3.14 +
                " Boolean: " + true);

            // 使用 Console.Write 打印，不带换行符号
            Console.Write("Hello ");
            Console.Write("World");

            ///////////////////////////////////////////////////
            // 类型和变量
            //
            // 使用 <type> <name> 定义变量
            ///////////////////////////////////////////////////

            // Sbyte - 有符号 8-bit 整数
            // (-128 <= sbyte <= 127)
            sbyte fooSbyte = 100;

            // Byte - 无符号 8-bit 整数
            // (0 <= byte <= 255)
            byte fooByte = 100;

            // Short - 16-bit 整数
            // 有符号 - (-32,768 <= short <= 32,767)
            // 无符号 - (0 <= ushort <= 65,535)
            short fooShort = 10000;
            ushort fooUshort = 10000;

            // Integer - 32-bit 整数
            int fooInt = 1; // (-2,147,483,648 <= int <= 2,147,483,647)
            uint fooUint = 1; // (0 <= uint <= 4,294,967,295)

            // Long - 64-bit 整数
            long fooLong = 100000L; // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
            ulong fooUlong = 100000L; // (0 <= ulong <= 18,446,744,073,709,551,615)
            // 数字默认为 int 或 uint （取决于尺寸）
            // 使用 L 标明变量值类型为long 或 ulong

            // Double - 双精度 64-bit IEEE 754 浮点数
            double fooDouble = 123.4; // 精度: 15-16 位

            // Float - 单精度 32-bit IEEE 754 浮点数
            float fooFloat = 234.5f; // 精度: 7 位
            // 使用 f 标明变量值类型为float

            // Decimal - 128-bits 数据类型，比其他浮点类型精度更高
            // 适合财务、金融
            decimal fooDecimal = 150.3m;

            // 布尔值 - true & false
            bool fooBoolean = true; // 或 false

            // Char - 单个 16-bit Unicode 字符
            char fooChar = 'A';

            // 字符串 -- 和前面的基本类型不同，字符串不是值，而是引用。
            // 这意味着你可以将字符串设为null。
            string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs)";
            Console.WriteLine(fooString);

            // 你可以通过索引访问字符串的每个字符：
            char charFromString = fooString[1]; // => 'e'
            // 字符串不可修改:  fooString[1] = 'X' 是行不通的;

            // 根据当前的locale设定比较字符串，大小写不敏感
            string.Compare(fooString, "x", StringComparison.CurrentCultureIgnoreCase);

            // 基于sprintf的字符串格式化
            string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2);

            // 日期和格式
            DateTime fooDate = DateTime.Now;
            Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy"));

            // 使用 @  符号可以创建跨行的字符串。使用 "" 来表示 "
            string bazString = @"Here's some stuff
on a new line! ""Wow!"", the masses cried";

            // 使用const或read-only定义常量
            // 常量在编译期演算
            const int HOURS_I_WORK_PER_WEEK = 9001;

            ///////////////////////////////////////////////////
            // 数据结构
            ///////////////////////////////////////////////////

            // 数组 - 从0开始计数
            // 声明数组时需要确定数组长度
            // 声明数组的格式如下：
            // <datatype>[] <var name> = new <datatype>[<array size>];
            int[] intArray = new int[10];

            // 声明并初始化数组的其他方式：
            int[] y = { 9000, 1000, 1337 };

            // 访问数组的元素
            Console.WriteLine("intArray @ 0: " + intArray[0]);
            // 数组可以修改
            intArray[1] = 1;

            // 列表
            // 列表比数组更常用，因为列表更灵活。
            // 声明列表的格式如下：
            // List<datatype> <var name> = new List<datatype>();
            List<int> intList = new List<int>();
            List<string> stringList = new List<string>();
            List<int> z = new List<int> { 9000, 1000, 1337 }; // i
            // <>用于泛型 - 参考下文

            // 列表无默认值
            // 访问列表元素时必须首先添加元素
            intList.Add(1);
            Console.WriteLine("intList @ 0: " + intList[0]);

            // 其他数据结构：
            // 堆栈/队列
            // 字典 (哈希表的实现)
            // 哈希集合
            // 只读集合
            // 元组 (.Net 4+)

            ///////////////////////////////////////
            // 操作符
            ///////////////////////////////////////
            Console.WriteLine("\n->Operators");

            int i1 = 1, i2 = 2; // 多重声明的简写形式

            // 算术直截了当
            Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3

            // 取余
            Console.WriteLine("11%3 = " + (11 % 3)); // => 2

            // 比较操作符
            Console.WriteLine("3 == 2? " + (3 == 2)); // => false
            Console.WriteLine("3 != 2? " + (3 != 2)); // => true
            Console.WriteLine("3 > 2? " + (3 > 2)); // => true
            Console.WriteLine("3 < 2? " + (3 < 2)); // => false
            Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true
            Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true

            // 位操作符
            /*
            ~       取反
            <<      左移（有符号）
            >>      右移（有符号）
            &       与
            ^       异或
            |       或
            */

            // 自增、自减
            int i = 0;
            Console.WriteLine("\n->Inc/Dec-rementation");
            Console.WriteLine(i++); //i = 1. 事后自增
            Console.WriteLine(++i); //i = 2. 事先自增
            Console.WriteLine(i--); //i = 1. 事后自减
            Console.WriteLine(--i); //i = 0. 事先自减

            ///////////////////////////////////////
            // 控制结构
            ///////////////////////////////////////
            Console.WriteLine("\n->Control Structures");

            // 类似C的if语句
            int j = 10;
            if (j == 10)
            {
                Console.WriteLine("I get printed");
            }
            else if (j > 10)
            {
                Console.WriteLine("I don't");
            }
            else
            {
                Console.WriteLine("I also don't");
            }

            // 三元表达式
            // 简单的 if/else 语句可以写成：
            // <条件> ? <真> : <假>
            int toCompare = 17;
            string isTrue = toCompare == 17 ? "True" : "False";

            // While 循环
            int fooWhile = 0;
            while (fooWhile < 100)
            {
                //迭代 100 次, fooWhile 0->99
                fooWhile++;
            }

            // Do While 循环
            int fooDoWhile = 0;
            do
            {
                //迭代 100 次, fooDoWhile 0->99
                fooDoWhile++;
            } while (fooDoWhile < 100);

            //for 循环结构 => for(<初始条件>; <条件>; <步>)
            for (int fooFor = 0; fooFor < 10; fooFor++)
            {
                //迭代10次, fooFor 0->9
            }

            // foreach循环
            // foreach 循环结构 => foreach(<迭代器类型> <迭代器> in <可枚举结构>)
            // foreach 循环适用于任何实现了 IEnumerable 或 IEnumerable<T> 的对象。
            // .Net 框架下的集合类型(数组, 列表, 字典...)
            // 都实现了这些接口
            // (下面的代码中，ToCharArray()可以删除，因为字符串同样实现了IEnumerable)
            foreach (char character in "Hello World".ToCharArray())
            {
                //迭代字符串中的所有字符
            }

            // Switch 语句
            // switch 适用于 byte、short、char和int 数据类型。
            // 同样适用于可枚举的类型
            // 包括字符串类, 以及一些封装了原始值的类：
            // Character、Byte、Short和Integer。
            int month = 3;
            string monthString;
            switch (month)
            {
                case 1:
                    monthString = "January";
                    break;
                case 2:
                    monthString = "February";
                    break;
                case 3:
                    monthString = "March";
                    break;
                // 你可以一次匹配多个case语句
                // 但是你在添加case语句后需要使用break
                // （否则你需要显式地使用goto case x语句）
                case 6:
                case 7:
                case 8:
                    monthString = "Summer time!!";
                    break;
                default:
                    monthString = "Some other month";
                    break;
            }

            ///////////////////////////////////////
            // 转换、指定数据类型
            ///////////////////////////////////////

            // 转换类型

            // 转换字符串为整数
            // 转换失败会抛出异常
            int.Parse("123");//返回整数类型的"123"

            // TryParse会尝试转换类型，失败时会返回缺省类型
            // 例如 0
            int tryInt;
            if (int.TryParse("123", out tryInt)) // Funciton is boolean
                Console.WriteLine(tryInt);       // 123

            // 转换整数为字符串
            // Convert类提供了一系列便利转换的方法
            Convert.ToString(123);
            // or
            tryInt.ToString();
        }

        ///////////////////////////////////////
        // 类
        ///////////////////////////////////////
        public static void Classes()
        {
            // 参看文件尾部的对象声明

            // 使用new初始化对象
            Bicycle trek = new Bicycle();

            // 调用对象的方法
            trek.SpeedUp(3); // 你应该一直使用setter和getter方法
            trek.Cadence = 100;

            // 查看对象的信息.
            Console.WriteLine("trek info: " + trek.Info());

            // 实例化一个新的Penny Farthing
            PennyFarthing funbike = new PennyFarthing(1, 10);
            Console.WriteLine("funbike info: " + funbike.Info());

            Console.Read();
        } // 结束main方法

        // 终端程序 终端程序必须有一个main方法作为入口
        public static void Main(string[] args)
        {
            OtherInterestingFeatures();
        }

        //
        // 有趣的特性
        //
        
        // 默认方法签名

        public // 可见性
        static // 允许直接调用类，无需先创建实例
        int, //返回值
        MethodSignatures(
            int maxCount, // 第一个变量，类型为整型
            int count = 0, // 如果没有传入值，则缺省值为0
            int another = 3,
            params string[] otherParams // 捕获其他参数
        )
        { 
            return -1;
        }

        // 方法可以重名，只要签名不一样
        public static void MethodSignature(string maxCount)
        {
        }

        //泛型
        // TKey和TValue类由用用户调用函数时指定。
        // 以下函数模拟了Python的SetDefault
        public static TValue SetDefault<TKey, TValue>(
            IDictionary<TKey, TValue> dictionary, 
            TKey key, 
            TValue defaultItem)
        {
            TValue result;
            if (!dictionary.TryGetValue(key, out result))
                return dictionary[key] = defaultItem;
            return result;
        }

        // 你可以限定传入值的范围
        public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
        {
            // 我们可以进行迭代，因为T是可枚举的
            foreach (var item in toPrint)
                // ittm为整数
                Console.WriteLine(item.ToString());
        }

        public static void OtherInterestingFeatures()
        {
            // 可选参数  
            MethodSignatures(3, 1, 3, "Some", "Extra", "Strings");
            MethodSignatures(3, another: 3); // 显式指定参数，忽略可选参数

            // 扩展方法
            int i = 3;
            i.Print(); // 参见下面的定义 

            // 可为null的类型 对数据库交互、返回值很有用
            // 任何值类型 (i.e. 不为类) 添加后缀 ? 后会变为可为null的值
            // <类型>? <变量名> = <值>
            int? nullable = null; // Nullable<int> 的简写形式
            Console.WriteLine("Nullable variable: " + nullable);
            bool hasValue = nullable.HasValue; // 不为null时返回真
            // ?? 是用于指定默认值的语法糖
            // 以防变量为null的情况
            int notNullable = nullable ?? 0; // 0

            // 变量类型推断 - 你可以让编译器推断变量类型:
            var magic = "编译器确定magic是一个字符串，所以仍然是类型安全的";
            // magic = 9; // 不工作，因为magic是字符串，而不是整数。 

            // 泛型
            //
            var phonebook = new Dictionary<string, string>() { 
                {"Sarah", "212 555 5555"} // 在电话簿中加入新条目
            };

            // 调用上面定义为泛型的SETDEFAULT
            Console.WriteLine(SetDefault<string,string>(phonebook, "Shaun", "No Phone")); // 没有电话
            // 你不用指定TKey、TValue，因为它们会被隐式地推导出来
            Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555

            // lambda表达式 - 允许你用一行代码搞定函数
            Func<int, int> square = (x) => x * x; // 最后一项为返回值
            Console.WriteLine(square(3)); // 9

            // 可抛弃的资源管理 - 让你很容易地处理未管理的资源
            // 大多数访问未管理资源 (文件操作符、设备上下文, etc.)的对象
            // 都实现了IDisposable接口。 
            // using语句会为你清理IDisposable对象。
            using (StreamWriter writer = new StreamWriter("log.txt"))
            {
                writer.WriteLine("这里没有什么可疑的东西");
                // 在作用域的结尾，资源会被回收
                // （即使有异常抛出，也一样会回收）
            } 

            // 并行框架
            // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
            var websites = new string[] { 
                "http://www.google.com", "http://www.reddit.com", 
                "http://www.shaunmccarthy.com"
            };
            var responses = new Dictionary<string, string>();
            
            // 为每个请求新开一个线程
            // 在运行下一步前合并结果
            Parallel.ForEach(websites, 
                new ParallelOptions() {MaxDegreeOfParallelism = 3}, // max of 3 threads
                website =>
            {
                // Do something that takes a long time on the file
                using (var r = WebRequest.Create(new Uri(website)).GetResponse())
                {
                    responses[website] = r.ContentType;
                }
            });

            // 直到所有的请求完成后才会运行下面的代码
            foreach (var key in responses.Keys)
                Console.WriteLine("{0}:{1}", key, responses[key]);

            // 动态对象（配合其他语言使用很方便）
            dynamic student = new ExpandoObject();
            student.FirstName = "First Name"; // 不需要先定义类！

            // 你甚至可以添加方法（接受一个字符串，输出一个字符串）
            student.Introduce = new Func<string, string>(
                (introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo));
            Console.WriteLine(student.Introduce("Beth"));

            // IQUERYABLE<T> - 几乎所有的集合都实现了它，
            // 带给你 Map / Filter / Reduce 风格的方法
            var bikes = new List<Bicycle>();
            bikes.Sort(); // Sorts the array
            bikes.Sort((b1, b2) => b1.Wheels.CompareTo(b2.Wheels)); // 根据车轮数排序
            var result = bikes
                .Where(b => b.Wheels > 3) // 筛选 - 可以连锁使用 （返回IQueryable)
                .Where(b => b.IsBroken && b.HasTassles)
                .Select(b => b.ToString()); // Map - 这里我们使用了select，所以结果是IQueryable<string>

            var sum = bikes.Sum(b => b.Wheels); // Reduce - 计算集合中的轮子总数

            // 创建一个包含基于自行车的一些参数生成的隐式对象的列表
            var bikeSummaries = bikes.Select(b=>new { Name = b.Name, IsAwesome = !b.IsBroken && b.HasTassles });
            // 很难演示，但是编译器在代码编译完成前就能推导出以上对象的类型
            foreach (var bikeSummary in bikeSummaries.Where(b => b.IsAwesome))
                Console.WriteLine(bikeSummary.Name);

            // ASPARALLEL
            // 邪恶的特性 —— 组合了linq和并行操作
            var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
            // 以上代码会并发地运行。会自动新开线程，分别计算结果。
            // 适用于多核、大数据量的场景。

            // LINQ - 将IQueryable<T>映射到存储，延缓执行
            // 例如 LinqToSql 映射数据库, LinqToXml 映射XML文档
            var db = new BikeRespository();

            // 执行被延迟了，这对于查询数据库来说很好
            var filter = db.Bikes.Where(b => b.HasTassles); // 不运行查询
            if (42 > 6) // 你可以不断地增加筛选，包括有条件的筛选，例如用于“高级搜索”功能
                filter = filter.Where(b => b.IsBroken); // 不运行查询 

            var query = filter
                .OrderBy(b => b.Wheels)
                .ThenBy(b => b.Name)
                .Select(b => b.Name); // 仍然不运行查询

            // 现在运行查询，运行查询的时候会打开一个读取器，所以你迭代的是一个副本
            foreach (string bike in query) 
                Console.WriteLine(result);
            


        }

    } // 结束LearnCSharp类

    // 你可以在同一个 .cs 文件中包含其他类

    public static class Extensions
    {
        // 扩展函数
        public static void Print(this object obj)
        {
            Console.WriteLine(obj.ToString());
        }
    }
    // 声明类的语法：
    // <public/private/protected/internal> class <类名>{
    //    //数据字段, 构造器, 内部函数.
    /     // 在Java中函数被称为方法。
    // }

    public class Bicycle
    {
        // 自行车的字段、变量
        public int Cadence // Public: 任何地方都可以访问
        {
            get // get - 定义获取属性的方法
            {
                return _cadence;
            }
            set // set - 定义设置属性的方法
            {
                _cadence = value; // value是被传递给setter的值
            }
        }
        private int _cadence;

        protected virtual int Gear // 类和子类可以访问
        {
            get; // 创建一个自动属性，无需成员字段
            set;
        }

        internal int Wheels // Internal:在同一程序集内可以访问
        {
            get;
            private set; // 可以给get/set方法添加修饰符
        }

        int _speed; // 默认为private: 只可以在这个类内访问，你也可以使用`private`关键词
        public string Name { get; set; }

        // enum类型包含一组常量
        // 它将名称映射到值（除非特别说明，是一个整型）
        // enmu元素的类型可以是byte、sbyte、short、ushort、int、uint、long、ulong。
        // enum不能包含相同的值。
        public enum BikeBrand
        {
            AIST,
            BMC,
            Electra = 42, //你可以显式地赋值
            Gitane // 43
        }
        // 我们在Bicycle类中定义的这个类型，所以它是一个内嵌类型。
        // 这个类以外的代码应当使用`Bicycle.Brand`来引用。

        public BikeBrand Brand; // 声明一个enum类型之后，我们可以声明这个类型的字段

        // 静态方法的类型为自身，不属于特定的对象。
        // 你无需引用对象就可以访问他们。
        // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated);
        static public int BicyclesCreated = 0;
        
        // 只读值在运行时确定
        // 它们只能在声明或构造器内被赋值
        readonly bool _hasCardsInSpokes = false; // read-only private

        // 构造器是创建类的一种方式
        // 下面是一个默认的构造器
        public Bicycle() 
        {
            this.Gear = 1; // 你可以使用关键词this访问对象的成员
            Cadence = 50;  // 不过你并不总是需要它
            _speed = 5;
            Name = "Bontrager";
            Brand = BikeBrand.AIST;
            BicyclesCreated++;
        }

        // 另一个构造器的例子（包含参数）
        public Bicycle(int startCadence, int startSpeed, int startGear,
                       string name, bool hasCardsInSpokes, BikeBrand brand) 
            : base() // 首先调用base
        {
            Gear = startGear; 
            Cadence = startCadence;
            _speed = startSpeed;
            Name = name; 
            _hasCardsInSpokes = hasCardsInSpokes;
            Brand = brand;
        }

        // 构造器可以连锁使用
        public Bicycle(int startCadence, int startSpeed, BikeBrand brand) :
            this(startCadence, startSpeed, 0, "big wheels", true, brand)
        {
        }

        // 函数语法
        // <public/private/protected> <返回值> <函数名称>(<参数>)

        // 类可以为字段实现 getters 和 setters 方法 for their fields
        // 或者可以实现属性（C#推荐使用这个）
        // 方法的参数可以有默认值
        // 在有默认值的情况下，调用方法的时候可以省略相应的参数
        public void SpeedUp(int increment = 1)
        {
            _speed += increment;
        }

        public void SlowDown(int decrement = 1)
        {
            _speed -= decrement;
        }

        // 属性可以访问和设置值
        // 当只需要访问数据的时候，考虑使用属性。
        // 属性可以定义get和set，或者是同时定义两者
        private bool _hasTassles; // private variable
        public bool HasTassles // public accessor
        {
            get { return _hasTassles; }
            set { _hasTassles = value; }
        }
        
        // 你可以在一行之内定义自动属性
        // 这个语法会自动创建后备字段
        // 你可以给getter或setter设置访问修饰符
        // 以便限制它们的访问
        public bool IsBroken { get; private set; }

        // 属性的实现可以是自动的
        public int FrameSize
        {
            get;
            // 你可以给get或set指定访问修饰符
            // 以下代码意味着只有Bicycle类可以调用Framesize的set
            private set;
        }

        //显示对象属性的方法
        public virtual string Info()
        {
            return "Gear: " + Gear +
                    " Cadence: " + Cadence +
                    " Speed: " + _speed +
                    " Name: " + Name +
                    " Cards in Spokes: " + (_hasCardsInSpokes ? "yes" : "no") +
                    "\n------------------------------\n"
                    ;
        }

        // 方法可以是静态的。通常用于辅助方法。
        public static bool DidWeCreateEnoughBycles()
        {
            // 在静态方法中，你只能引用类的静态成员
            return BicyclesCreated > 9000;
        } // 如果你的类只需要静态成员，考虑将整个类作为静态类。


    } //  Bicycle类结束

    // PennyFarthing是Bicycle的一个子类
    class PennyFarthing : Bicycle
    {
        // (Penny Farthings是一种前轮很大的自行车。没有齿轮。）

        // 调用父构造器
        public PennyFarthing(int startCadence, int startSpeed) :
            base(startCadence, startSpeed, 0, "PennyFarthing", true, BikeBrand.Electra)
        {
        }

        protected override int Gear
        {
            get
            {
                return 0;
            }
            set
            {
                throw new ArgumentException("你不可能在PennyFarthing上切换齿轮");
            }
        }

        public override string Info()
        {
            string result = "PennyFarthing bicycle ";
            result += base.ToString(); // 调用父方法
            return result;
        }
    }

    // 接口只包含成员的签名，而没有实现。
    interface IJumpable
    {
        void Jump(int meters); // 所有接口成员是隐式地公开的
    }

    interface IBreakable
    {
        bool Broken { get; } // 接口可以包含属性、方法和事件
    }

    // 类只能继承一个类，但是可以实现任意数量的接口
    {
        int damage = 0;

        public void Jump(int meters)
        {
            damage += meters;
        }

        public bool Broken
        {
            get
            {
                return damage > 100;
            }
        }
    }

    /// <summary>
    /// 连接数据库，一个 LinqToSql的示例。
    /// EntityFramework Code First 很棒 (类似 Ruby的 ActiveRecord, 不过是双向的)
    /// http://msdn.microsoft.com/en-us/data/jj193542.aspx
    /// </summary>
    public class BikeRespository : DbSet
    {
        public BikeRespository()
            : base()
        {
        }

        public DbSet<Bicycle> Bikes { get; set; }
    }
} // 结束 Namespace
```

## 没有涉及到的主题

 * Flags
 * Attributes
 * 静态属性
 * Exceptions, Abstraction
 * ASP.NET (Web Forms/MVC/WebMatrix)
 * Winforms
 * Windows Presentation Foundation (WPF)

## 扩展阅读

 * [DotNetPerls](http://www.dotnetperls.com)
 * [C# in Depth](http://manning.com/skeet2)
 * [Programming C#](http://shop.oreilly.com/product/0636920024064.do)
 * [LINQ](http://shop.oreilly.com/product/9780596519254.do)
 * [MSDN Library](http://msdn.microsoft.com/en-us/library/618ayhy6.aspx)
 * [ASP.NET MVC Tutorials](http://www.asp.net/mvc/tutorials)
 * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials)
 * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials)
 * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208)
 * [C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx)
---
language: css
contributors:
    - ["Mohammad Valipour", "https://github.com/mvalipour"]
    - ["Marco Scannadinari", "https://github.com/marcoms"]
translators:
    - ["Jakukyo Friel", "https://weakish.github.io"]
lang: zh-cn
filename: learncss-cn.css
---

早期的web没有样式，只是单纯的文本。通过CSS，可以实现网页样式和内容的分离。

简单来说，CSS可以指定HTML页面上的元素所使用的样式。

和其他语言一样，CSS有很多版本。最新的版本是CSS 3. CSS 2.0兼容性最好。

你可以使用[dabblet](http://dabblet.com/)来在线测试CSS的效果。

```css
/* 注释 */

/* ####################
   ## 选择器
   ####################*/

/* 一般而言，CSS的声明语句非常简单。 */
选择器 { 属性: 值; /* 更多属性...*/ }

/* 选择器用于指定页面上的元素。

针对页面上的所有元素。 */
* { color:red; }

/*
假定页面上有这样一个元素

<div class='some-class class2' id='someId' attr='value' />
*/

/* 你可以通过类名来指定它 */
.some-class { }

/* 给出所有类名 */
.some-class.class2 { }

/* 标签名 */
div { }

/* id */
#someId { }

/* 由于元素包含attr属性，因此也可以通过这个来指定 */
[attr] { font-size:smaller; }

/* 以及有特定值的属性 */
[attr='value'] { font-size:smaller; }

/* 通过属性的值的开头指定 */
[attr^='val'] { font-size:smaller; }

/* 通过属性的值的结尾来指定 */
[attr$='ue'] { font-size:smaller; }

/* 通过属性的值的部分来指定 */
[attr~='lu'] { font-size:smaller; }


/* 你可以把这些全部结合起来，注意不同部分间不应该有空格，否则会改变语义 */
div.some-class[attr$='ue'] { }

/* 你也可以通过父元素来指定。*/

/* 某个元素是另一个元素的直接子元素 */
div.some-parent > .class-name {}

/* 或者通过该元素的祖先元素 */
div.some-parent .class-name {}

/* 注意，去掉空格后语义就不同了。
你能说出哪里不同么？ */
div.some-parent.class-name {}

/* 你可以选择某元素前的相邻元素 */
.i-am-before + .this-element { }

/* 某元素之前的同级元素（相邻或不相邻） */
.i-am-any-before ~ .this-element {}

/* 伪类允许你基于页面的行为指定元素（而不是基于页面结构） */

/* 例如，当鼠标悬停在某个元素上时 */
:hover {}

/* 已访问过的链接*/
:visited {}

/* 未访问过的链接*/
:link {}

/* 当前焦点的input元素 */
:focus {}


/* ####################
   ## 属性
   ####################*/

选择器 {
    
    /* 单位 */
    width: 50%; /* 百分比 */
    font-size: 2em; /* 当前字体大小的两倍 */
    width: 200px; /* 像素 */
    font-size: 20pt; /* 点 */
    width: 5cm; /* 厘米 */
    width: 50mm; /* 毫米 */
    width: 5in; /* 英尺 */
    
    /* 颜色 */
    background-color: #F6E;  /* 短16位 */
    background-color: #F262E2; /* 长16位 */
    background-color: tomato; /* 颜色名称 */
    background-color: rgb(255, 255, 255); /* rgb */
    background-color: rgb(10%, 20%, 50%); /*  rgb 百分比 */
    background-color: rgba(255, 0, 0, 0.3); /*  rgb 加透明度 */
    
    /* 图片 */
    background-image: url(/path-to-image/image.jpg);
    
    /* 字体 */
    font-family: Arial;
    font-family: "Courier New"; /* 使用双引号包裹含空格的字体名称 */
    font-family: "Courier New", Trebuchet, Arial; /* 如果第一个
    						 字体没找到，浏览器会使用第二个字体，一次类推 */
}

```

## 使用

CSS文件使用 `.css` 后缀。

```xml
<!-- 你需要在文件的 <head> 引用CSS文件 -->
<link rel='stylesheet' type='text/css' href='filepath/filename.css' />

<!-- 你也可以在标记中内嵌CSS。不过强烈建议不要这么干。 -->
<style>
   选择器 { 属性:值; }
</style>

<!-- 也可以直接使用元素的style属性。
这是你最不该干的事情。 -->
<div style='property:value;'>
</div>

```

## 优先级

同一个元素可能被多个不同的选择器指定，因此可能会有冲突。

假定CSS是这样的：

```css
/*A*/
p.class1[attr='value']

/*B*/
p.class1 {}

/*C*/
p.class2 {}

/*D*/
p {}

/*E*/
p { property: value !important; }

```

然后标记语言为：

```xml
<p style='/*F*/ property:value;' class='class1 class2' attr='value'>
</p>
```

那么将会按照下面的顺序应用风格：


* `E` 优先级最高，因为它使用了 `!important`，除非很有必要，尽量避免使用这个。
* `F` 其次，因为它是嵌入的风格。
* `A` 其次，因为它比其他指令更具体。
* `C` 其次，虽然它的具体程度和`B`一样，但是它在`B`之后。
* 接下来是 `B`。
* 最后是 `D`。

## 兼容性

CSS2 的绝大部分特性兼容各种浏览器和设备。现在 CSS3 的兼容性也越来越好了。
但是兼容性问题仍然是需要留意的一个问题。

[QuirksMode CSS](http://www.quirksmode.org/css/)是关于这方面最好的资源。

## 扩展阅读

* [理解CSS的风格优先级: 特定性, 继承和层叠](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
---
language: dart
lang: zh-cn
filename: learndart-cn.dart
contributors:
    - ["Joao Pedrosa", "https://github.com/jpedrosa/"]
translators:
    - ["Guokai Han", "https://github.com/hanguokai/"]
---

Dart 是编程语言王国的新人。
它借鉴了许多其他主流语言，并且不会偏离它的兄弟语言 JavaScript 太多。
就像 JavaScript 一样，Dart 的目标是提供良好的浏览器集成。

Dart 最有争议的特性必然是它的可选类型。

```javascript
import "dart:collection";
import "dart:math" as DM;

// 欢迎进入15分钟的 Dart 学习。 http://www.dartlang.org/
// 这是一个可实际执行的向导。你可以用 Dart 运行它
// 或者在线执行! 可以把代码复制/粘贴到这个网站。 http://try.dartlang.org/

// 函数声明和方法声明看起来一样。
// 函数声明可以嵌套。声明使用这种 name() {} 的形式，
// 或者 name() => 单行表达式; 的形式。
// 右箭头的声明形式会隐式地返回表达式的结果。
example1() {
  example1nested1() {
    example1nested2() => print("Example1 nested 1 nested 2");
    example1nested2();
  }
  example1nested1();
}

// 匿名函数没有函数名。
example2() {
  example2nested1(fn) {
    fn();
  }
  example2nested1(() => print("Example2 nested 1"));
}

// 当声明函数类型的参数的时候，声明中可以包含
// 函数参数需要的参数，指定所需的参数名即可。
example3() {
  example3nested1(fn(informSomething)) {
    fn("Example3 nested 1");
  }
  example3planB(fn) { // 或者不声明函数参数的参数
    fn("Example3 plan B");
  }
  example3nested1((s) => print(s));
  example3planB((s) => print(s));
}

// 函数有可以访问到外层变量的闭包。
var example4Something = "Example4 nested 1";
example4() {
  example4nested1(fn(informSomething)) {
    fn(example4Something);
  }
  example4nested1((s) => print(s));
}

// 下面这个包含 sayIt 方法的类声明，同样有一个可以访问外层变量的闭包，
// 就像前面的函数一样。
var example5method = "Example5 sayIt";
class Example5Class {
  sayIt() {
    print(example5method);
  }
}
example5() {
  // 创建一个 Example5Class 类的匿名实例，
  // 并调用它的 sayIt 方法。
  new Example5Class().sayIt();
}

// 类的声明使用这种形式 class name { [classBody] }.
// classBody 中可以包含实例方法和变量，
// 还可以包含类方法和变量。
class Example6Class {
  var example6InstanceVariable = "Example6 instance variable"; 
  sayIt() {
    print(example6InstanceVariable);
  }
}
example6() {
  new Example6Class().sayIt();
}

// 类方法和变量使用 static 关键词声明。
class Example7Class {
  static var example7ClassVariable = "Example7 class variable"; 
  static sayItFromClass() {
    print(example7ClassVariable);
  }
  sayItFromInstance() {
    print(example7ClassVariable);
  }
}
example7() {
  Example7Class.sayItFromClass();
  new Example7Class().sayItFromInstance();
}

// 字面量非常方便，但是对于在函数/方法的外层的字面量有一个限制，
// 类的外层或外面的字面量必需是常量。
// 字符串和数字默认是常量。
// 但是 array 和 map 不是。他们需要用 "const" 声明为常量。
var example8A = const ["Example8 const array"],
  example8M = const {"someKey": "Example8 const map"}; 
example8() {
  print(example8A[0]);
  print(example8M["someKey"]);
}

// Dart 中的循环使用标准的 for () {} 或 while () {} 的形式，
// 以及更加现代的 for (.. in ..) {} 的形式, 或者
// 以 forEach 开头并具有许多特性支持的函数回调的形式。
var example9A = const ["a", "b"];
example9() {
  for (var i = 0; i < example9A.length; i++) {
    print("Example9 for loop '${example9A[i]}'");
  }
  var i = 0;
  while (i < example9A.length) {
    print("Example9 while loop '${example9A[i]}'");
    i++;
  }
  for (var e in example9A) {
    print("Example9 for-in loop '${e}'");
  }
  example9A.forEach((e) => print("Example9 forEach loop '${e}'"));
}

// 遍历字符串中的每个字符或者提取其子串。
var example10S = "ab";
example10() {
  for (var i = 0; i < example10S.length; i++) {
    print("Example10 String character loop '${example10S[i]}'");
  }
  for (var i = 0; i < example10S.length; i++) {
    print("Example10 substring loop '${example10S.substring(i, i + 1)}'");
  }
}

// 支持两种数字格式 int 和 double 。
example11() {
  var i = 1 + 320, d = 3.2 + 0.01;
  print("Example11 int ${i}");
  print("Example11 double ${d}");
}

// DateTime 提供了日期/时间的算法。
example12() {
  var now = new DateTime.now();
  print("Example12 now '${now}'");
  now = now.add(new Duration(days: 1));
  print("Example12 tomorrow '${now}'");
}

// 支持正则表达式。
example13() {
  var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$");
  match(s) {
    if (re.hasMatch(s)) {
      print("Example13 regexp matches '${s}'");
    } else {
      print("Example13 regexp doesn't match '${s}'");
    }
  }
  match(s1);
  match(s2);
}

// 布尔表达式必需被解析为 true 或 false，
// 因为不支持隐式转换。
example14() {
  var v = true;
  if (v) {
    print("Example14 value is true");
  }
  v = null;
  try {
    if (v) {
      // 不会执行
    } else {
      // 不会执行
    }
  } catch (e) {
    print("Example14 null value causes an exception: '${e}'");
  }
}

// try/catch/finally 和 throw 语句用于异常处理。
// throw 语句可以使用任何对象作为参数。
example15() {
  try {
    try {
      throw "Some unexpected error.";
    } catch (e) {
      print("Example15 an exception: '${e}'");
      throw e; // Re-throw
    }
  } catch (e) {
    print("Example15 catch exception being re-thrown: '${e}'");
  } finally {
    print("Example15 Still run finally");
  }
}

// 要想有效地动态创建长字符串，
// 应该使用 StringBuffer。 或者 join 一个字符串的数组。
example16() {
  var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e;
  for (e in a) { sb.write(e); }
  print("Example16 dynamic string created with "
    "StringBuffer '${sb.toString()}'");
  print("Example16 join string array '${a.join()}'");
}

// 字符串连接只需让相邻的字符串字面量挨着，
// 不需要额外的操作符。
example17() {
  print("Example17 "
      "concatenate "
      "strings "
      "just like that");
}

// 字符串使用单引号或双引号做分隔符，二者并没有实际的差异。
// 这种灵活性可以很好地避免内容中需要转义分隔符的情况。
// 例如，字符串内容里的 HTML 属性使用了双引号。
example18() {
  print('Example18 <a href="etc">'
      "Don't can't I'm Etc"
      '</a>');
}

// 用三个单引号或三个双引号表示的字符串
// 可以跨越多行，并且包含行分隔符。
example19() {
  print('''Example19 <a href="etc"> 
Example19 Don't can't I'm Etc
Example19 </a>''');
}

// 字符串可以使用 $ 字符插入内容。
// 使用 $ { [expression] } 的形式，表达式的值会被插入到字符串中。
// $ 跟着一个变量名会插入变量的值。
// 如果要在字符串中插入 $ ，可以使用 \$ 的转义形式代替。
example20() {
  var s1 = "'\${s}'", s2 = "'\$s'";
  print("Example20 \$ interpolation ${s1} or $s2 works.");
}

// 可选类型允许作为 API 的标注，并且可以辅助 IDE，
// 这样 IDE 可以更好地提供重构、自动完成和错误检测功能。
// 目前为止我们还没有声明任何类型，并且程序运行地很好。
// 事实上，类型在运行时会被忽略。
// 类型甚至可以是错的，并且程序依然可以执行，
// 好像和类型完全无关一样。
// 有一个运行时参数可以让程序进入检查模式，它会在运行时检查类型错误。
// 这在开发时很有用，但是由于增加了额外的检查会使程序变慢，
// 因此应该避免在部署时使用。
class Example21 {
  List<String> _names;
  Example21() {
    _names = ["a", "b"]; 
  }
  List<String> get names => _names;
  set names(List<String> list) {
    _names = list;
  }
  int get length => _names.length;
  void add(String name) {
    _names.add(name);
  }
}
void example21() {
  Example21 o = new Example21();
  o.add("c");
  print("Example21 names '${o.names}' and length '${o.length}'");
  o.names = ["d", "e"];
  print("Example21 names '${o.names}' and length '${o.length}'");
}

// 类的继承形式是 class name extends AnotherClassName {} 。
class Example22A {
  var _name = "Some Name!";
  get name => _name;
}
class Example22B extends Example22A {}
example22() {
  var o = new Example22B();
  print("Example22 class inheritance '${o.name}'");
}

// 类也可以使用 mixin 的形式 ：
// class name extends SomeClass with AnotherClassName {}.
// 必需继承某个类才能 mixin 另一个类。
// 当前 mixin 的模板类不能有构造函数。
// Mixin 主要是用来和辅助的类共享方法的，
// 这样单一继承就不会影响代码复用。
// Mixin 声明在类定义的 "with" 关键词后面。
class Example23A {}
class Example23Utils {
  addTwo(n1, n2) {
    return n1 + n2;
  }
}
class Example23B extends Example23A with Example23Utils {
  addThree(n1, n2, n3) {
    return addTwo(n1, n2) + n3;
  }
}
example23() {
  var o = new Example23B(), r1 = o.addThree(1, 2, 3),
    r2 = o.addTwo(1, 2);
  print("Example23 addThree(1, 2, 3) results in '${r1}'");
  print("Example23 addTwo(1, 2) results in '${r2}'");
}

// 类的构造函数名和类名相同，形式为
// SomeClass() : super() {},  其中 ": super()" 的部分是可选的，
// 它用来传递参数给父类的构造函数。
class Example24A {
  var _value;
  Example24A({value: "someValue"}) {
    _value = value;
  }
  get value => _value;
}
class Example24B extends Example24A {
  Example24B({value: "someOtherValue"}) : super(value: value);
}
example24() {
  var o1 = new Example24B(),
    o2 = new Example24B(value: "evenMore");
  print("Example24 calling super during constructor '${o1.value}'");
  print("Example24 calling super during constructor '${o2.value}'");
}

// 对于简单的类，有一种设置构造函数参数的快捷方式。
// 只需要使用 this.parameterName 的前缀，
// 它就会把参数设置为同名的实例变量。
class Example25 {
  var value, anotherValue;
  Example25({this.value, this.anotherValue});
}
example25() {
  var o = new Example25(value: "a", anotherValue: "b");
  print("Example25 shortcut for constructor '${o.value}' and "
    "'${o.anotherValue}'");
}

// 可以在大括号 {} 中声明命名参数。
// 大括号 {} 中声明的参数的顺序是随意的。
// 在中括号 [] 中声明的参数也是可选的。 
example26() {
  var _name, _surname, _email;
  setConfig1({name, surname}) {
    _name = name;
    _surname = surname;
  }
  setConfig2(name, [surname, email]) {
    _name = name;
    _surname = surname;
    _email = email;
  }
  setConfig1(surname: "Doe", name: "John");
  print("Example26 name '${_name}', surname '${_surname}', "
    "email '${_email}'");
  setConfig2("Mary", "Jane");
  print("Example26 name '${_name}', surname '${_surname}', "
  "email '${_email}'");
}

// 使用 final 声明的变量只能被设置一次。
// 在类里面，final 实例变量可以通过常量的构造函数参数设置。
class Example27 {
  final color1, color2;
  // 更灵活一点的方法是在冒号 : 后面设置 final 实例变量。
  Example27({this.color1, color2}) : color2 = color2;
}
example27() {
  final color = "orange", o = new Example27(color1: "lilac", color2: "white");
  print("Example27 color is '${color}'");
  print("Example27 color is '${o.color1}' and '${o.color2}'");
}

// 要导入一个库，使用 import "libraryPath" 的形式，或者如果要导入的是
// 核心库使用 import "dart:libraryName" 。还有一个称为 "pub" 的包管理工具，
// 它使用 import "package:packageName" 的约定形式。
// 看下这个文件顶部的 import "dart:collection"; 语句。 
// 导入语句必需在其它代码声明之前出现。IterableBase 来自于 dart:collection 。
class Example28 extends IterableBase {
  var names;
  Example28() {
    names = ["a", "b"];
  }
  get iterator => names.iterator;
}
example28() {
  var o = new Example28();
  o.forEach((name) => print("Example28 '${name}'"));
}

// 对于控制流语句，我们有：
// * 必需带 break 的标准 switch 语句
// * if-else 和三元操作符 ..?..:.. 
// * 闭包和匿名函数
// * break, continue 和 return 语句
example29() {
  var v = true ? 30 : 60;
  switch (v) {
    case 30:
      print("Example29 switch statement");
      break;
  }
  if (v < 30) {
  } else if (v > 30) {
  } else {
    print("Example29 if-else statement");
  }
  callItForMe(fn()) {
    return fn();
  }
  rand() {
    v = new DM.Random().nextInt(50);
    return v;
  }
  while (true) {
    print("Example29 callItForMe(rand) '${callItForMe(rand)}'");
    if (v != 30) {
      break;
    } else {
      continue;
    }
    // 不会到这里。
  }
}

// 解析 int，把 double 转成 int，或者使用 ~/ 操作符在除法计算时仅保留整数位。
// 让我们也来场猜数游戏吧。
example30() {
  var gn, tooHigh = false,
    n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0;
  top = top ~/ 6;
  gn = new DM.Random().nextInt(top + 1); // +1 because nextInt top is exclusive
  print("Example30 Guess a number between 0 and ${top}");
  guessNumber(i) {
    if (n == gn) {
      print("Example30 Guessed right! The number is ${gn}");
    } else {
      tooHigh = n > gn;
      print("Example30 Number ${n} is too "
        "${tooHigh ? 'high' : 'low'}. Try again");
    }
    return n == gn;
  }
  n = (top - bottom) ~/ 2;
  while (!guessNumber(n)) {
    if (tooHigh) {
      top = n - 1;
    } else {
      bottom = n + 1;
    }
    n = bottom + ((top - bottom) ~/ 2);
  }
}

// 程序的唯一入口点是 main 函数。
// 在程序开始执行 main 函数之前，不期望执行任何外层代码。
// 这样可以帮助程序更快地加载，甚至仅惰性加载程序启动时需要的部分。
main() {
  print("Learn Dart in 15 minutes!");
  [example1, example2, example3, example4, example5, example6, example7,
    example8, example9, example10, example11, example12, example13, example14,
    example15, example16, example17, example18, example19, example20,
    example21, example22, example23, example24, example25, example26,
    example27, example28, example29, example30
    ].forEach((ef) => ef());
}

```

## 延伸阅读

Dart 有一个综合性网站。它涵盖了 API 参考、入门向导、文章以及更多，
还包括一个有用的在线试用 Dart 页面。
http://www.dartlang.org/
http://try.dartlang.org/



---
category: Algorithms & Data Structures
name: Dynamic Programming
contributors:
    - ["Akashdeep Goel", "http://github.com/akashdeepgoel"]
filename: dynamic-programming-cn.html.markdown
lang: zh-cn
translators:
   - ["EtaoinWu", "https://github.com/EtaoinWu"]
---

# 动态规划

## 简介

动态规划是一种实用的技巧，它可以用来解决一系列特定问题。它的思路很简单，如果你对某个给定的输入解决了一个问题，那么你可以保存已有信息，以避免重复计算，节约计算时间。

记住，只有那些没有办法记住历史的才被迫做更多的苦力。(Fibonacci就是一个显然的例子)

## 解决问题的方式

1. *自顶向下* : 利用分支策略分解问题。如果你已经解决过当前子问题了，那么就返回已有信息。如果当前子问题没有计算过，那么就对它进行计算。这样的方法很易于思考、很直观。这被称作“记忆化”。

2. *自底向上* : 首先分析问题，将问题分解为不同规模的问题，并决定它们的顺序，按顺序计算，直到解决给定规模的问题。这样的流程可以保证在解决较大的问题之前解决（它所依赖的）较小的问题。这种流程被称作“动态规划”。

## 动态规划的例子

最长上升子序列问题。给定`S= {a[1] , a[2] , a[3], a[4], ............., a[n-1], a[n] }`，求出一个子序列，使得对于所有在这个子序列中所有满足`j<i`的`j`与`i`，满足`aj<ai`。首先我们要讨论以原序列的第`i`个元素结尾的最长上升子序列`dp[i]`。那么答案是整个dp序列的最大值。考虑`dp[i]`，它的最后一个元素为`a[i]`。枚举它的倒数第二个元素`a[j]`，则`a[j]<a[i]`成立。则`dp[i]`就是所有这样的`dp[j]`的最大值加上1(最后一个元素)。这个算法具有*O(n^2)*的时间复杂度。

此算法的伪代码：

```python
for i=0 to n-1
    dp[i]=0
    for j=0 to i-1
        if (a[i] >  a[j] and dp[i]<dp[j])
            LS[i] = LS[j]
	dp[i]=dp[i]+1
for i=0 to n-1
    if (largest < dp[i])
		largest = dp[i]
```

这个算法的复杂度可以通过将数组换为其他数据结构来优化，来获得*O(n * log n)*的时间复杂度。

同样的思路可以求出有向无环图上的最大路径。

### 一些著名的动态规划问题及其实现

- Floyd Warshall 算法 - [教程与C实现源码](http://www.thelearningpoint.net/computer-science/algorithms-all-to-all-shortest-paths-in-graphs---floyd-warshall-algorithm-with-c-program-source-code)
- 整数背包问题 - [教程与C实现源码](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---the-integer-knapsack-problem)
- 最长公共子序列问题 - [教程与C实现源码](http://www.thelearningpoint.net/computer-science/algorithms-dynamic-programming---longest-common-subsequence)

## 在线资源

* [codechef](https://www.codechef.com/wiki/tutorial-dynamic-programming)
* [洛谷](https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=3)
---
language: elisp
contributors:
    - ["Bastien Guerry", "http://bzg.fr"]
translators:
    - ["Chenbo Li", "http://binarythink.net"]
filename: learn-emacs-lisp-zh.el
lang: zh-cn
---

```scheme
;; 15分钟学会Emacs Lisp (v0.2a)
;;（作者：bzg，https://github.com/bzg 
;;  译者：lichenbo，http://douban.com/people/lichenbo）
;;
;; 请先阅读Peter Norvig的一篇好文:
;; http://norvig.com/21-days.html
;; （译者注：中文版请见http://blog.youxu.info/21-days/）
;;
;; 之后安装GNU Emacs 24.3:
;;
;; Debian: apt-get install emacs (视具体发行版而定)
;; MacOSX: http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg
;; Windows: http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip
;;
;; 更多信息可以在这里找到:
;; http://www.gnu.org/software/emacs/#Obtaining
 
;; 很重要的警告:
;;
;; 按照这个教程来学习并不会对你的电脑有任何损坏
;; 除非你自己在学习的过程中愤怒地把它砸了
;; 如果出现了这种情况，我不会承担任何责任
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; 打开emacs
;;
;; 按'q'消除欢迎界面
;;
;; 现在请注意窗口底部的那一个灰色长条
;;
;; "*scratch*" 是你现在编辑界面的名字。
;; 这个编辑界面叫做一个"buffer"。
;;
;; 每当你打开Emacs时，都会默认打开这个scratch buffer
;; 此时你并没有在编辑任何文件，而是在编辑一个buffer
;; 之后你可以将这个buffer保存到一个文件中。
;; 
;; 之后的"Lisp interaction" 则是表明我们可以用的某组命令
;; 
;; Emacs在每个buffer中都有一组内置的命令
;; 而当你激活某种特定的模式时，就可以使用相应的命令
;; 这里我们使用`lisp-interaction-mode'，
;; 这样我们就可以使用内置的Emacs Lisp（以下简称Elisp）命令了。
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 分号是注释开始的标志
;;
;; Elisp 是由符号表达式构成的 (即"s-表达式"或"s式"):
(+ 2 2)
 
;; 这个s式的意思是 "对2进行加2操作".
 
;; s式周围有括号，而且也可以嵌套:
(+ 2 (+ 1 1))
 
;; 一个s式可以包含原子符号或者其他s式
;; 在上面的例子中，1和2是原子符号
;; (+ 2 (+ 1 1)) 和 (+ 1 1) 是s式.
 
;; 在 `lisp-interaction-mode' 中你可以计算s式.
;; 把光标移到闭括号后，之后按下ctrl+j（以后简写为'C-j'）
 
(+ 3 (+ 1 2))
;;           ^ 光标放到这里
;; 按下`C-j' 就会输出 6
 
;; `C-j' 会在buffer中插入当前运算的结果
 
;; 而`C-xC-e' 则会在emacs最底部显示结果，也就是被称作"minibuffer"的区域
;; 为了避免把我们的buffer填满无用的结果，我们以后会一直用`C-xC-e'
 
;; `setq' 可以将一个值赋给一个变量
(setq my-name "Bastien")
;; `C-xC-e' 输出 "Bastien" (在 mini-buffer 中显示)
 
;; `insert' 会在光标处插入字符串:
(insert "Hello!")
;; `C-xC-e' 输出 "Hello!"
 
;; 在这里我们只传给了insert一个参数"Hello!", 但是
;; 我们也可以传给它更多的参数，比如2个：
 
(insert "Hello" " world!")
;; `C-xC-e' 输出 "Hello world!"
 
;; 你也可以用变量名来代替字符串
(insert "Hello, I am " my-name)
;; `C-xC-e' 输出 "Hello, I am Bastien"
 
;; 你可以把s式嵌入函数中
(defun hello () (insert "Hello, I am " my-name))
;; `C-xC-e' 输出 hello
 
;; 现在执行这个函数
(hello)
;; `C-xC-e' 输出 Hello, I am Bastien
 
;; 函数中空括号的意思是我们不需要接受任何参数
;; 但是我们不能一直总是用my-name这个变量
;; 所以我们现在使我们的函数接受一个叫做"name"的参数 
 
(defun hello (name) (insert "Hello " name))
;; `C-xC-e' 输出 hello
 
;; 现在我们调用这个函数，并且将"you"作为参数传递
 
(hello "you")
;; `C-xC-e' 输出 "Hello you"
 
;; 成功！
 
;; 现在我们可以休息一下
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 下面我们在新的窗口中新建一个名为 "*test*" 的buffer:
 
(switch-to-buffer-other-window "*test*")
;; `C-xC-e' 这时屏幕上会显示两个窗口，而光标此时位于*test* buffer内
 
;; 用鼠标单击上面的buffer就会使光标移回。
;; 或者你可以使用 `C-xo' 使得光标跳到另一个窗口中
 
;; 你可以用 `progn'命令将s式结合起来:
(progn
  (switch-to-buffer-other-window "*test*")
  (hello "you"))
;; `C-xC-e' 此时屏幕分为两个窗口，并且在*test* buffer中显示"Hello you"
 
;; 现在为了简洁，我们需要在每个s式后面都使用`C-xC-e'来执行，后面就不再说明了
 
;; 记得可以用过鼠标或者`C-xo'回到*scratch*这个buffer。
 
;; 清除当前buffer也是常用操作之一：
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "there"))
 
;; 也可以回到其他的窗口中
(progn
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello "you")
  (other-window 1))
 
;; 你可以用 `let' 将一个值和一个局部变量绑定:
(let ((local-name "you"))
  (switch-to-buffer-other-window "*test*")
  (erase-buffer)
  (hello local-name)
  (other-window 1))
 
;; 这里我们就不需要使用 `progn' 了， 因为 `let' 也可以将很多s式组合起来。
 
;; 格式化字符串的方法：
(format "Hello %s!\n" "visitor")
 
;; %s 是字符串占位符，这里被"visitor"替代.
;; \n 是换行符。
 
;; 现在我们用格式化的方法再重写一下我们的函数:
(defun hello (name)
  (insert (format "Hello %s!\n" name)))
 
(hello "you")
 
;; 我们再用`let'新建另一个函数:
(defun greeting (name)
  (let ((your-name "Bastien"))
    (insert (format "Hello %s!\n\nI am %s."
                    name       ; the argument of the function
                    your-name  ; the let-bound variable "Bastien"
                    ))))
 
;; 之后执行:
(greeting "you")
 
;; 有些函数可以和用户交互:
(read-from-minibuffer "Enter your name: ")
 
;; 这个函数会返回在执行时用户输入的信息
 
;; 现在我们让`greeting'函数显示你的名字:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (insert (format "Hello!\n\nI am %s and you are %s."
                    from-name ; the argument of the function
                    your-name ; the let-bound var, entered at prompt
                    ))))
 
(greeting "Bastien")
 
;; 我们让结果在另一个窗口中显示:
(defun greeting (from-name)
  (let ((your-name (read-from-minibuffer "Enter your name: ")))
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (insert (format "Hello %s!\n\nI am %s." your-name from-name))
    (other-window 1)))
 
;; 测试一下：
(greeting "Bastien")
 
;; 第二节结束，休息一下吧。
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 我们将一些名字存到列表中：
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))
 
;; 用 `car'来取得第一个名字：
(car list-of-names)
 
;; 用 `cdr'取得剩下的名字:
(cdr list-of-names)
 
;; 用 `push'把名字添加到列表的开头:
(push "Stephanie" list-of-names)
 
;; 注意: `car' 和 `cdr' 并不修改列表本身, 但是 `push' 却会对列表本身进行操作.
;; 这个区别是很重要的: 有些函数没有任何副作用（比如`car'）
;; 但还有一些却是有的 (比如 `push').
 
;; 我们来对`list-of-names'列表中的每一个元素都使用hello函数:
(mapcar 'hello list-of-names)
 
;; 将 `greeting' 改进，使的我们能够对`list-of-names'中的所有名字执行:
(defun greeting ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (mapcar 'hello list-of-names)
    (other-window 1))
 
(greeting)
 
;; 记得我们之前定义的 `hello' 函数吗？ 这个函数接受一个参数，名字。
;; `mapcar' 调用 `hello', 并将`list-of-names'作为参数先后传给`hello'
 
;; 现在我们对显示的buffer中的内容进行一些更改：
 
(defun replace-hello-by-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (search-forward "Hello")
      (replace-match "Bonjour"))
    (other-window 1))
 
;; (goto-char (point-min)) 将光标移到buffer的开始
;; (search-forward "Hello") 查找字符串"Hello"
;; (while x y) 当x返回某个值时执行y这个s式
;; 当x返回`nil' (空), 退出循环
 
(replace-hello-by-bonjour)
 
;; 你会看到所有在*test* buffer中出现的"Hello"字样都被换成了"Bonjour"
 
;; 你也会得到以下错误提示: "Search failed: Hello".
;;
;; 如果要避免这个错误, 你需要告诉 `search-forward' 这个命令是否在
;; buffer的某个地方停止查找, 并且在什么都没找到时是否应该不给出错误提示
 
;; (search-forward "Hello" nil t) 可以达到这个要求:
 
;; `nil' 参数的意思是 : 查找并不限于某个范围内
;; `t' 参数的意思是: 当什么都没找到时，不给出错误提示
 
;; 在下面的函数中，我们用到了s式，并且不给出任何错误提示:
 
(defun hello-to-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    ;; 为`list-of-names'中的每个名字调用hello
    (mapcar 'hello list-of-names)
    (goto-char (point-min))
    ;; 将"Hello" 替换为"Bonjour"
    (while (search-forward "Hello" nil t)
      (replace-match "Bonjour"))
    (other-window 1))
 
(hello-to-bonjour)
 
;; 给这些名字加粗:
 
(defun boldify-names ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))
    (while (re-search-forward "Bonjour \\(.+\\)!" nil t)
      (add-text-properties (match-beginning 1)
                           (match-end 1)
                           (list 'face 'bold)))
    (other-window 1))
 
;; 这个函数使用了 `re-search-forward':
;; 和查找一个字符串不同，你用这个命令可以查找一个模式，即正则表达式
 
;; 正则表达式 "Bonjour \\(.+\\)!" 的意思是:
;; 字符串 "Bonjour ", 之后跟着
;; 一组           |  \\( ... \\) 结构
;; 任意字符       |  . 的含义
;; 有可能重复的   |  + 的含义
;; 之后跟着 "!" 这个字符串
 
;; 准备好了？试试看。
 
(boldify-names)
 
;; `add-text-properties' 可以添加文字属性, 比如文字样式
 
;; 好的，我们成功了！
 
;; 如果你想对一个变量或者函数有更多的了解：
;;
;; C-h v 变量 回车
;; C-h f 函数 回车
;;
;; 阅读Emacs Lisp官方文档:
;;
;; C-h i m elisp 回车
;;
;; 在线阅读Emacs Lisp文档:
;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html
 
;; 感谢以下同学的建议和反馈:
;; - Wes Hardaker
;; - notbob
;; - Kevin Montuori
;; - Arne Babenhauserheide
;; - Alan Schmitt
;; - spacegoing
```

---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
translators:
    - ["lidashuang", "http://github.com/lidashuang"]
filename: learnelixir-cn.ex
lang: zh-cn
---

Elixir 是一门构建在Erlang VM 之上的函数式编程语言。Elixir 完全兼容 Erlang, 
另外还提供了更标准的语法，特性。

```elixir

# 这是单行注释, 注释以井号开头

# 没有多行注释
# 但你可以堆叠多个注释。

# elixir shell 使用命令 `iex` 进入。
# 编译模块使用 `elixirc` 命令。

# 如果安装正确，这些命令都会在环境变量里

## ---------------------------
## -- 基本类型
## ---------------------------

# 数字
3    # 整型
0x1F # 整型
3.0  # 浮点类型

# 原子(Atoms)，以 `:`开头
:hello # atom

# 元组(Tuple) 在内存中的存储是连续的
{1,2,3} # tuple

# 使用`elem`函数访问元组(tuple)里的元素:
elem({1, 2, 3}, 0) #=> 1

# 列表(list)
[1,2,3] # list

# 可以用下面的方法访问列表的头尾元素:
[head | tail] = [1,2,3]
head #=> 1
tail #=> [2,3]

# 在elixir,就像在Erlang, `=` 表示模式匹配 (pattern matching) 
# 不是赋值。
#
# 这表示会用左边的模式(pattern)匹配右侧
# 
# 上面的例子中访问列表的头部和尾部就是这样工作的。

# 当左右两边不匹配时，会返回error, 在这个
# 例子中，元组大小不一样。
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# 还有二进制类型 (binaries)
<<1,2,3>> # binary

# 字符串(Strings) 和 字符列表(char lists)
"hello" # string
'hello' # char list

# 多行字符串
"""
I'm a multi-line
string.
"""
#=> "I'm a multi-line\nstring.\n"

# 所有的字符串(Strings)以UTF-8编码：
"héllò" #=> "héllò"

# 字符串(Strings)本质就是二进制类型(binaries), 字符列表(char lists)本质是列表(lists)
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# 在 elixir中，`?a`返回 `a` 的 ASCII 整型值  
?a #=> 97

# 合并列表使用 `++`, 对于二进制类型则使用 `<>`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'hello ' ++ 'world'  #=> 'hello world'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"hello " <> "world"  #=> "hello world"

## ---------------------------
## -- 操作符(Operators)
## ---------------------------

#  一些数学运算
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# 在 elixir 中，操作符 `/` 返回值总是浮点数。

# 做整数除法使用 `div`
div(10, 2) #=> 5

# 为了得到余数使用 `rem`
rem(10, 3) #=> 1

# 还有 boolean 操作符: `or`, `and` and `not`.
# 第一个参数必须是boolean 类型
true and true #=> true
false or true #=> true
# 1 and true    #=> ** (ArgumentError) argument error

# Elixir 也提供了 `||`, `&&` 和  `!` 可以接受任意的类型
# 除了`false` 和 `nil` 其它都会被当作true.
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil

!true #=> false

# 比较有: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` 和 `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# `===` 和 `!==` 在比较整型和浮点类型时更为严格:
1 == 1.0  #=> true
1 === 1.0 #=> false

# 我们也可以比较两种不同的类型:
1 < :hello #=> true

# 总的排序顺序定义如下:
# number < atom < reference < functions < port < pid < tuple < list < bit string

# 引用Joe Armstrong ：“实际的顺序并不重要，
# 但是，一个整体排序是否经明确界定是非常重要的。”

## ---------------------------
## -- 控制结构(Control Flow)
## ---------------------------

# `if` 表达式
if false do
  "This will never be seen"
else
  "This will"
end

# 还有 `unless`
unless true do
  "This will never be seen"
else
  "This will"
end

# 在Elixir中，很多控制结构都依赖于模式匹配

# `case` 允许我们把一个值与多种模式进行比较:
case {:one, :two} do
  {:four, :five} ->
    "This won't match"
  {:one, x} ->
    "This will match and assign `x` to `:two`"
  _ ->
    "This will match any value"
end

# 模式匹配时，如果不需要某个值，通用的做法是把值 匹配到 `_` 
# 例如，我们只需要要列表的头元素:
[head | _] = [1,2,3]
head #=> 1

# 下面的方式效果一样，但可读性更好
[head | _tail] = [:a, :b, :c]
head #=> :a

# `cond` 可以检测多种不同的分支
# 使用 `cond` 代替多个`if` 表达式嵌套
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  1 + 2 == 3 ->
    "But I will"
end

# 经常可以看到最后一个条件等于'true'，这将总是匹配。
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  true ->
    "But I will (this is essentially an else)"
end

# `try/catch` 用于捕获被抛出的值, 它也支持 `after` 子句，
# 无论是否值被捕获，after 子句都会被调用
# `try/catch` 
try do
  throw(:hello)
catch
  message -> "Got #{message}."
after
  IO.puts("I'm the after clause.")
end
#=> I'm the after clause
# "Got :hello"

## ---------------------------
## -- 模块和函数(Modules and Functions)
## ---------------------------

# 匿名函数 (注意点)
square = fn(x) -> x * x end
square.(5) #=> 25


# 也支持接收多个子句和卫士(guards).
# Guards 可以进行模式匹配
# Guards 使用 `when` 关键字指明:
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir 提供了很多内建函数
# 在默认作用域都是可用的
is_number(10)    #=> true
is_list("hello") #=> false
elem({1,2,3}, 0) #=> 1

# 你可以在一个模块里定义多个函数，定义函数使用 `def`
defmodule Math do
  def sum(a, b) do
    a + b
  end

  def square(x) do
    x * x
  end
end

Math.sum(1, 2)  #=> 3
Math.square(3) #=> 9

# 保存到 `math.ex`，使用 `elixirc` 编译你的 Math 模块
# 在终端里: elixirc math.ex

# 在模块中可以使用`def`定义函数，使用 `defp` 定义私有函数
# 使用`def` 定义的函数可以被其它模块调用
# 私有函数只能在本模块内调用
defmodule PrivateMath do
  def sum(a, b) do
    do_sum(a, b)
  end

  defp do_sum(a, b) do
    a + b
  end
end

PrivateMath.sum(1, 2)    #=> 3
# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)


# 函数定义同样支持 guards 和 多重子句：
defmodule Geometry do
  def area({:rectangle, w, h}) do
    w * h
  end

  def area({:circle, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometry.area({:rectangle, 2, 3}) #=> 6
Geometry.area({:circle, 3})       #=> 28.25999999999999801048
# Geometry.area({:circle, "not_a_number"})
#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1

#由于不变性，递归是Elixir的重要组成部分
defmodule Recursion do
  def sum_list([head | tail], acc) do
    sum_list(tail, acc + head)
  end

  def sum_list([], acc) do
    acc
  end
end

Recursion.sum_list([1,2,3], 0) #=> 6

# Elixir 模块支持属性，模块内建了一些属性，你也可以自定义属性
defmodule MyMod do
  @moduledoc """
  内置的属性，模块文档
  """

  @my_data 100 # 自定义属性
  IO.inspect(@my_data) #=> 100
end

## ---------------------------
## -- 记录和异常(Records and Exceptions)
## ---------------------------

# 记录就是把特定值关联到某个名字的结构体
defrecord Person, name: nil, age: 0, height: 0

joe_info = Person.new(name: "Joe", age: 30, height: 180)
#=> Person[name: "Joe", age: 30, height: 180]

# 访问name的值
joe_info.name #=> "Joe"

# 更新age的值
joe_info = joe_info.age(31) #=> Person[name: "Joe", age: 31, height: 180]

# 使用 `try` `rescue` 进行异常处理
try do
  raise "some error"
rescue
  RuntimeError -> "rescued a runtime error"
  _error -> "this will rescue any error"
end

# 所有的异常都有一个message
try do
  raise "some error"
rescue
  x in [RuntimeError] ->
    x.message
end

## ---------------------------
## -- 并发(Concurrency)
## ---------------------------

# Elixir 依赖于 actor并发模型。在Elixir编写并发程序的三要素：
# 创建进程，发送消息，接收消息

# 启动一个新的进程使用`spawn`函数，接收一个函数作为参数

f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>


# `spawn` 函数返回一个pid(进程标识符)，你可以使用pid向进程发送消息。
# 使用 `<-` 操作符发送消息。
#  我们需要在进程内接收消息，要用到 `receive` 机制。

defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Area = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        area_loop()
    end
  end
end

# 编译这个模块，在shell中创建一个进程，并执行 `area_looop` 函数。
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>

# 发送一个消息给 `pid`， 会在receive语句进行模式匹配
pid <- {:rectangle, 2, 3}
#=> Area = 6
#   {:rectangle,2,3}

pid <- {:circle, 2}
#=> Area = 12.56000000000000049738
#   {:circle,2}

# shell也是一个进程(process), 你可以使用`self`获取当前 pid 
self() #=> #PID<0.27.0>
```

## 参考文献

* [Getting started guide](http://elixir-lang.org/getting_started/1.html) from [elixir webpage](http://elixir-lang.org)
* [Elixir Documentation](http://elixir-lang.org/docs/master/)
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) by Fred Hebert
* "Programming Erlang: Software for a Concurrent World" by Joe Armstrong
---
language: erlang
lang: zh-cn
contributors:
    - ["Giovanni Cappellotto", "http://www.focustheweb.com/"]
translators:
    - ["Jakukyo Friel", "http://weakish.github.io"]
filename: erlang-cn.erl
---

```erlang
% 百分比符号标明注释的开始。

%% 两个符号通常用于注释函数。

%%% 三个符号通常用于注释模块。

% Erlang 里使用三种标点符号：
% 逗号 (`,`) 分隔函数调用中的参数、数据构建和模式。
% 句号 (`.`) （后跟空格）分隔函数和 shell 中的表达式。
% 分号 (`;`) 分隔语句。以下环境中使用语句：
% 函数定义和`case`、`if`、`try..catch`、`receive`表达式。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1. 变量和模式匹配
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Num = 42.  % 变量必须以大写字母开头。

% Erlang 的变量只能赋值一次。如果给变量赋不同的值，会导致错误：
Num = 43. % ** exception error: no match of right hand side value 43

% 大多数语言中`=`表示赋值语句，在Erlang中，则表示模式匹配。
% `Lhs = Rhs`实际上意味着：
% 演算右边(Rhs), 将结果与左边的模式匹配。
Num = 7 * 6.

% 浮点数
Pi = 3.14159.

% Atoms 用于表示非数字的常量。
% Atom 以小写字母开始，包含字母、数字、`_`和`@`。
Hello = hello.
OtherNode = example@node.

% Atom 中如果包含特殊字符，可以用单引号括起。
AtomWithSpace = 'some atom with space'.

% Erlang 的元组类似 C 的 struct.
Point = {point, 10, 45}.

% 使用模式匹配操作符`=`获取元组的值。
{point, X, Y} = Point.  % X = 10, Y = 45

% 我们可以使用`_`存放我们不感兴趣的变量。
% `_`被称为匿名变量。和其他变量不同，
% 同一个模式中的多个`_`变量不必绑定到相同的值。
Person = {person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}.
{_, {_, {_, Who}, _}, _} = Person.  % Who = joe

% 列表使用方括号，元素间使用逗号分隔。
% 列表的元素可以是任意类型。
% 列表的第一个元素称为列表的 head，其余元素称为列表的 tail。
ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].

% 若`T`是一个列表，那么`[H|T]`同样是一个列表，head为`H`，tail为`T`.
% `|`分隔列表的 head 和 tail.
% `[]`是空列表。
% 我们可以使用模式匹配操作来抽取列表中的元素。
% 如果我们有一个非空的列表`L`，那么`[X|Y] = L`则
% 抽取 L 的 head 至 X，tail 至 Y （X、Y需为未定义的变量）。
[FirstThing|OtherThingsToBuy] = ThingsToBuy.
% FirstThing = {apples, 10}
% OtherThingsToBuy = {pears, 6}, {milk, 3}

% Erlang 中的字符串其实是由整数组成的数组。字符串使用双引号。
Name = "Hello".
[72, 101, 108, 108, 111] = "Hello".


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2. 循序编程
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Module 是 Erlang 代码的基本单位。我们编写的所有函数都储存在 module 中。
% Module 存储在后缀为 `.erl` 的文件中。
% Module 必须事先编译。编译好的 module 以 `.beam` 结尾。
-module(geometry).
-export([area/1]). % module 对外暴露的函数列表

% `area`函数包含两个分句，分句间以分号相隔。
% 最后一个分句以句号加换行结尾。
% 每个分句由头、体两部门组成。
% 头部包含函数名称和用括号括起的模式，
% 体部包含一系列表达式，如果头部的模式和调用时的参数匹配，这些表达式会被演算。
% 模式匹配依照定义时的顺序依次进行。
area({rectangle, Width, Ht}) -> Width * Ht;
area({circle, R})            -> 3.14159 * R * R.

% 编译文件为 geometry.erl.
c(geometry).  % {ok,geometry}

% 调用函数时必须使用 module 名和函数名。
geometry:area({rectangle, 10, 5}).  % 50
geometry:area({circle, 1.4}).  % 6.15752

% 在 Erlang 中，同一模块中，参数数目不同，名字相同的函数是完全不同的函数。
-module(lib_misc).
-export([sum/1]). % 对外暴露的`sum`函数接受一个参数：由整数组成的列表。
sum(L) -> sum(L, 0).
sum([], N)    -> N;
sum([H|T], N) -> sum(T, H+N).

% fun 是匿名函数。它们没有名字，不过可以赋值给变量。
Double = fun(X) -> 2*X end. % `Double` 指向匿名函数 #Fun<erl_eval.6.17052888>
Double(2).  % 4

% fun 可以作为函数的参数和返回值。
Mult = fun(Times) -> ( fun(X) -> X * Times end ) end.
Triple = Mult(3).
Triple(5).  % 15

% 列表解析是创建列表的表达式（不使用fun、map 或 filter）。
% `[F(X) || X <- L]` 表示 "由 `F(X)` 组成的列表，其中 `X` 取自列表 `L`。
L = [1,2,3,4,5].
[2*X || X <- L].  % [2,4,6,8,10]
% 列表解析可以使用生成器，也可以使用过滤器，过滤器用于筛选列表的一部分。
EvenNumbers = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4]

% Guard 是用于增强模式匹配的结构。
% Guard 可用于简单的测试和比较。
% Guard 可用于函数定义的头部，以`when`关键字开头，或者其他可以使用表达式的地方。
max(X, Y) when X > Y -> X;
max(X, Y) -> Y.

% guard 可以由一系列 guard 表达式组成，这些表达式以逗号分隔。
% `GuardExpr1, GuardExpr2, ..., GuardExprN` 为真，当且仅当每个 guard 表达式均为真。
is_cat(A) when is_atom(A), A =:= cat -> true;
is_cat(A) -> false.
is_dog(A) when is_atom(A), A =:= dog -> true;
is_dog(A) -> false.

% guard 序列 `G1; G2; ...; Gn` 为真，当且仅当其中任意一个 guard 表达式为真。
is_pet(A) when is_dog(A); is_cat(A) -> true;
is_pet(A) -> false.

% Record 可以将元组中的元素绑定到特定的名称。
% Record 定义可以包含在 Erlang 源代码中，也可以放在后缀为`.hrl`的文件中（Erlang 源代码中 include 这些文件）。
-record(todo, {
  status = reminder,  % Default value
  who = joe,
  text
}).

% 在定义某个 record 之前，我们需要在 shell 中导入 record 的定义。
% 我们可以使用 shell 函数`rr` (read records 的简称）。
rr("records.hrl").  % [todo]

% 创建和更新 record。
X = #todo{}.
% #todo{status = reminder, who = joe, text = undefined}
X1 = #todo{status = urgent, text = "Fix errata in book"}.
% #todo{status = urgent, who = joe, text = "Fix errata in book"}
X2 = X1#todo{status = done}.
% #todo{status = done,who = joe,text = "Fix errata in book"}

% `case` 表达式。
% `filter` 返回由列表`L`中所有满足`P(x)`为真的元素`X`组成的列表。
filter(P, [H|T]) ->
  case P(H) of
    true -> [H|filter(P, T)];
    false -> filter(P, T)
  end;
filter(P, []) -> [].
filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]). % [2, 4]

% `if` 表达式。
max(X, Y) ->
  if
    X > Y -> X;
    X < Y -> Y;
    true -> nil;
  end.

% 警告: `if` 表达式里至少有一个 guard 为真，否则会触发异常。


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3. 异常
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 当遇到内部错误或显式调用时，会触发异常。
% 显式调用包括 `throw(Exception)`, `exit(Exception)` 和
% `erlang:error(Exception)`.
generate_exception(1) -> a;
generate_exception(2) -> throw(a);
generate_exception(3) -> exit(a);
generate_exception(4) -> {'EXIT', a};
generate_exception(5) -> erlang:error(a).

% Erlang 有两种捕获异常的方法。其一是将调用包裹在`try...catch`表达式中。
catcher(N) ->
  try generate_exception(N) of
    Val -> {N, normal, Val}
  catch
    throw:X -> {N, caught, thrown, X};
    exit:X -> {N, caught, exited, X};
    error:X -> {N, caught, error, X}
  end.

% 另一种方式是将调用包裹在`catch`表达式中。
% 此时异常会被转化为一个描述错误的元组。
catcher(N) -> catch generate_exception(N).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 4. 并发
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Erlang 依赖于 actor并发模型。在 Erlang 编写并发程序的三要素：
% 创建进程，发送消息，接收消息

% 启动一个新的进程使用`spawn`函数，接收一个函数作为参数

F = fun() -> 2 + 2 end. % #Fun<erl_eval.20.67289768>
spawn(F). % <0.44.0>

% `spawn` 函数返回一个pid(进程标识符)，你可以使用pid向进程发送消息。
% 使用 `!` 操作符发送消息。
%  我们需要在进程内接收消息，要用到 `receive` 机制。

-module(caculateGeometry).
-compile(export_all).
caculateAera() ->
    receive
      {rectangle, W, H} ->
        W * H;
      {circle, R} ->
        3.14 * R * R;
      _ ->
        io:format("We can only caculate area of rectangles or circles.")
    end.

% 编译这个模块，在 shell 中创建一个进程，并执行 `caculateArea` 函数。
c(caculateGeometry).
CaculateAera = spawn(caculateGeometry, caculateAera, []).
CaculateAera ! {circle, 2}. % 12.56000000000000049738

% shell也是一个进程(process), 你可以使用`self`获取当前 pid

self(). % <0.41.0>

```

## References

* ["Learn You Some Erlang for great good!"](http://learnyousomeerlang.com/)
* ["Programming Erlang: Software for a Concurrent World" by Joe Armstrong](http://pragprog.com/book/jaerlang/programming-erlang)
* [Erlang/OTP Reference Documentation](http://www.erlang.org/doc/)
* [Erlang - Programming Rules and Conventions](http://www.erlang.se/doc/programming_rules.shtml)
---
category: tool
tool: git
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
    - ["Chenbo Li", "http://binarythink.net"]
lang: zh-cn
---

Git是一个分布式版本控制及源代码管理工具 

Git可以为你的项目保存若干快照，以此来对整个项目进行版本管理

## 版本

### 什么是版本控制

版本控制系统就是根据时间来记录一个或多个文件的更改情况的系统。

### 集中式版本控制 VS 分布式版本控制

* 集中式版本控制的主要功能为同步，跟踪以及备份文件
* 分布式版本控制则更注重共享更改。每一次更改都有唯一的标识
* 分布式系统没有预定的结构。你也可以用git很轻松的实现SVN风格的集中式系统控制

[更多信息](http://git-scm.com/book/en/Getting-Started-About-Version-Control)

### 为什么要使用Git

* 可以离线工作
* 和他人协同工作变得简单
* 分支很轻松
* 合并很容易
* Git系统速度快，也很灵活

## Git 架构


### 版本库

一系列文件，目录，历史记录，提交记录和头指针。
可以把它视作每个源代码文件都带有历史记录属性数据结构

一个Git版本库包括一个 .git 目录和其工作目录

### .git 目录(版本库的一部分)

.git 目录包含所有的配置、日志、分支信息、头指针等
[详细列表](http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html)

### 工作目录 (版本库的一部分)

版本库中的目录和文件，可以看做就是你工作时的目录

### 索引(.git 目录)

索引就是git中的 staging 区. 可以算作是把你的工作目录与Git版本库分割开的一层
这使得开发者能够更灵活的决定要将要在版本库中添加什么内容

### 提交

一个 git 提交就是一组更改或者对工作目录操作的快照
比如你添加了5个文件，删除了2个文件，那么这些变化就会被写入一个提交比如你添加了5个文件，删除了2个文件，那么这些变化就会被写入一个提交中
而这个提交之后也可以被决定是否推送到另一个版本库中

### 分支

分支其实就是一个指向你最后一次的提交的指针
当你提交时，这个指针就会自动指向最新的提交

### 头指针 与 头(.git 文件夹的作用)

头指针是一个指向当前分支的指针，一个版本库只有一个当前活动的头指针
而头则可以指向版本库中任意一个提交，每个版本库也可以有多个头

### 其他形象化解释

* [给计算机科学家的解释](http://eagain.net/articles/git-for-computer-scientists/)
* [给设计师的解释](http://hoth.entp.com/output/git_for_designers.html)


## 命令


### 初始化

创建一个新的git版本库。这个版本库的配置、存储等信息会被保存到.git文件夹中

```bash
$ git init
```

### 配置

更改设置。可以是版本库的设置，也可以是系统的或全局的


```bash
# 输出、设置基本的全局变量
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "MyEmail@Zoho.com"
$ git config --global user.name "My Name"
```

[关于git的更多设置](http://git-scm.com/docs/git-config)

### 帮助

git内置了对命令非常详细的解释，可以供我们快速查阅

```bash
# 查找可用命令
$ git help

# 查找所有可用命令
$ git help -a

# 在文档当中查找特定的命令
# git help <命令>
$ git help add
$ git help commit
$ git help init
```

### 状态

显示索引文件（也就是当前工作空间）和当前的头指针指向的提交的不同


```bash
# 显示分支，为跟踪文件，更改和其他不同
$ git status

# 查看其他的git status的用法
$ git help status
```

### 添加

添加文件到当前工作空间中。如果你不使用 `git add` 将文件添加进去，
那么这些文件也不会添加到之后的提交之中

```bash
# 添加一个文件
$ git add HelloWorld.java

# 添加一个子目录中的文件
$ git add /path/to/file/HelloWorld.c

# 支持正则表达式
$ git add ./*.java
```

### 分支

管理分支，可以通过下列命令对分支进行增删改查

```bash
# 查看所有的分支和远程分支
$ git branch -a

# 创建一个新的分支
$ git branch myNewBranch

# 删除一个分支
$ git branch -d myBranch

# 重命名分支
# git branch -m <旧名称> <新名称>
$ git branch -m myBranchName myNewBranchName

# 编辑分支的介绍
$ git branch myBranchName --edit-description
```

### 检出

将当前工作空间更新到索引所标识的或者某一特定的工作空间

```bash
# 检出一个版本库，默认将更新到master分支
$ git checkout
# 检出到一个特定的分支
$ git checkout branchName
# 新建一个分支，并且切换过去，相当于"git branch <名字>; git checkout <名字>"
$ git checkout -b newBranch
```

### clone

这个命令就是将一个版本库拷贝到另一个目录中，同时也将
分支都拷贝到新的版本库中。这样就可以在新的版本库中提交到远程分支

```bash
# clone learnxinyminutes-docs
$ git clone https://github.com/adambard/learnxinyminutes-docs.git
```

### commit

将当前索引的更改保存为一个新的提交，这个提交包括用户做出的更改与信息

```bash
# 提交时附带提交信息
$ git commit -m "Added multiplyNumbers() function to HelloWorld.c"
```

### diff

显示当前工作空间和提交的不同

```bash
# 显示工作目录和索引的不同
$ git diff

# 显示索引和最近一次提交的不同
$ git diff --cached

# 显示工作目录和最近一次提交的不同
$ git diff HEAD
```

### grep

可以在版本库中快速查找

可选配置：

```bash
# 感谢Travis Jeffery提供的以下用法：
# 在搜索结果中显示行号
$ git config --global grep.lineNumber true

# 是搜索结果可读性更好
$ git config --global alias.g "grep --break --heading --line-number"
```

```bash
# 在所有的java中查找variableName
$ git grep 'variableName' -- '*.java'

# 搜索包含 "arrayListName" 和, "add" 或 "remove" 的所有行
$ git grep -e 'arrayListName' --and \( -e add -e remove \) 
```

更多的例子可以查看：
[Git Grep Ninja](http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja)

### log

显示这个版本库的所有提交

```bash
# 显示所有提交
$ git log

# 显示某几条提交信息
$ git log -n 10

# 仅显示合并提交
$ git log --merges
```

### merge

合并就是将外部的提交合并到自己的分支中

```bash
# 将其他分支合并到当前分支
$ git merge branchName

# 在合并时创建一个新的合并后的提交
$ git merge --no-ff branchName
```

### mv

重命名或移动一个文件

```bash
# 重命名
$ git mv HelloWorld.c HelloNewWorld.c

# 移动
$ git mv HelloWorld.c ./new/path/HelloWorld.c

# 强制重命名或移动
# 这个文件已经存在，将要覆盖掉
$ git mv -f myFile existingFile
```

### pull

从远端版本库合并到当前分支

```bash
# 从远端origin的master分支更新版本库
# git pull <远端> <分支>
$ git pull origin master
```

### push

把远端的版本库更新

```bash
# 把本地的分支更新到远端origin的master分支上
# git push <远端> <分支>
# git push 相当于 git push origin master
$ git push origin master
```

### rebase (谨慎使用) 

将一个分支上所有的提交历史都应用到另一个分支上
*不要在一个已经公开的远端分支上使用rebase*.

```bash
# 将experimentBranch应用到master上面
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch
```

[更多阅读](http://git-scm.com/book/en/Git-Branching-Rebasing)

### reset (谨慎使用)

将当前的头指针复位到一个特定的状态。这样可以使你撤销merge、pull、commits、add等
这是个很强大的命令，但是在使用时一定要清楚其所产生的后果

```bash
# 使 staging 区域恢复到上次提交时的状态，不改变现在的工作目录
$ git reset

# 使 staging 区域恢复到上次提交时的状态，覆盖现在的工作目录
$ git reset --hard

# 将当前分支恢复到某次提交，不改变现在的工作目录
# 在工作目录中所有的改变仍然存在
$ git reset 31f2bb1

# 将当前分支恢复到某次提交，覆盖现在的工作目录
# 并且删除所有未提交的改变和指定提交之后的所有提交
$ git reset --hard 31f2bb1
```

### rm

和add相反，从工作空间中去掉某个文件

```bash
# 移除 HelloWorld.c
$ git rm HelloWorld.c

# 移除子目录中的文件
$ git rm /pather/to/the/file/HelloWorld.c
```

## 更多阅读

* [tryGit - 学习Git的有趣方式](http://try.github.io/levels/1/challenges/1)

* [git-scm - 视频教程](http://git-scm.com/videos)

* [git-scm - 文档](http://git-scm.com/docs)

* [Atlassian Git - 教程与工作流程](https://www.atlassian.com/git/)

* [SalesForce Cheat Sheet](https://na1.salesforce.com/help/doc/en/salesforce_git_developer_cheatsheet.pdf)

* [GitGuys](http://www.gitguys.com/)
---
language: Go
lang: zh-cn
filename: learngo-cn.go
contributors:
    - ["Sonia Keys", "https://github.com/soniakeys"]
    - ["pantaovay", "https://github.com/pantaovay"]
    - ["lidashuang", "https://github.com/lidashuang"]
    - ["Tim Zhang", "https://github.com/ttimasdf"]

---

发明Go语言是出于更好地完成工作的需要。Go不是计算机科学的最新发展潮流，但它却提供了解决现实问题的最新最快的方法。

Go拥有命令式语言的静态类型，编译很快，执行也很快，同时加入了对于目前多核CPU的并发计算支持，也有相应的特性来实现大规模编程。

Go语言有非常棒的标准库，还有一个充满热情的社区。

```go
// 单行注释
/* 多行
    注释 */

// 导入包的子句在每个源文件的开头。
// Main比较特殊，它用来声明可执行文件，而不是一个库。
package main

// Import语句声明了当前文件引用的包。
import (
    "fmt"       // Go语言标准库中的包
    "io/ioutil" // 包含一些输入输出函数
    m "math"    // 数学标准库，在此文件中别名为m
    "net/http"  // 一个web服务器包
    "os"        // 系统底层函数，如文件读写
    "strconv"   // 字符串转换
)

// 函数声明：Main是程序执行的入口。
// 不管你喜欢还是不喜欢，反正Go就用了花括号来包住函数体。
func main() {
    // 往标准输出打印一行。
    // 用包名fmt限制打印函数。
    fmt.Println("天坑欢迎你!")

    // 调用当前包的另一个函数。
    beyondHello()
}

// 函数可以在括号里加参数。
// 如果没有参数的话，也需要一个空括号。
func beyondHello() {
    var x int   // 变量声明，变量必须在使用之前声明。
    x = 3       // 变量赋值。
    // 可以用:=来偷懒，它自动把变量类型、声明和赋值都搞定了。
    y := 4
    sum, prod := learnMultiple(x, y)        // 返回多个变量的函数
    fmt.Println("sum:", sum, "prod:", prod) // 简单输出
    learnTypes()                            // 少于y分钟，学的更多！
}

/* <- 快看快看我是跨行注释_(:з」∠)_
Go语言的函数可以有多个参数和 *多个* 返回值。
在这个函数中， `x`、`y` 是参数，
`sum`、`prod` 是返回值的标识符（可以理解为名字）且类型为int
*/
func learnMultiple(x, y int) (sum, prod int) {
    return x + y, x * y // 返回两个值
}

// 内置变量类型和关键词
func learnTypes() {
    // 短声明给你所想。
    str := "少说话多读书!" // String类型

    s2 := `这是一个
可以换行的字符串` // 同样是String类型

    // 非ascii字符。Go使用UTF-8编码。
    g := 'Σ' // rune类型，int32的别名，使用UTF-8编码

    f := 3.14195 // float64类型，IEEE-754 64位浮点数
    c := 3 + 4i  // complex128类型，内部使用两个float64表示

    // Var变量可以直接初始化。
    var u uint = 7  // unsigned 无符号变量，但是实现依赖int型变量的长度
    var pi float32 = 22. / 7

    // 字符转换
    n := byte('\n') // byte是uint8的别名

    // 数组（Array）类型的大小在编译时即确定
    var a4 [4] int              // 有4个int变量的数组，初始为0
    a3 := [...]int{3, 1, 5}     // 有3个int变量的数组，同时进行了初始化

    // Array和slice各有所长，但是slice可以动态的增删，所以更多时候还是使用slice。
    s3 := []int{4, 5, 9}    // 回去看看 a3 ，是不是这里没有省略号？
    s4 := make([]int, 4)    // 分配4个int大小的内存并初始化为0
    var d2 [][]float64      // 这里只是声明，并未分配内存空间
    bs := []byte("a slice") // 进行类型转换

    // 切片（Slice）的大小是动态的，它的长度可以按需增长
    // 用内置函数 append() 向切片末尾添加元素
    // 要增添到的目标是 append 函数第一个参数，
    // 多数时候数组在原内存处顺次增长，如
    s := []int{1, 2, 3}     // 这是个长度3的slice
    s = append(s, 4, 5, 6)  // 再加仨元素，长度变为6了
    fmt.Println(s) // 更新后的数组是 [1 2 3 4 5 6]

    // 除了向append()提供一组原子元素（写死在代码里的）以外，我们
    // 还可以用如下方法传递一个slice常量或变量，并在后面加上省略号，
    // 用以表示我们将引用一个slice、解包其中的元素并将其添加到s数组末尾。
    s = append(s, []int{7, 8, 9}...) // 第二个参数是一个slice常量
    fmt.Println(s)  // 更新后的数组是 [1 2 3 4 5 6 7 8 9]

    p, q := learnMemory()       // 声明p,q为int型变量的指针
    fmt.Println(*p, *q)         // * 取值

    // Map是动态可增长关联数组，和其他语言中的hash或者字典相似。
    m := map[string]int{"three": 3, "four": 4}
    m["one"] = 1

    // 在Go语言中未使用的变量在编译的时候会报错，而不是warning。
    // 下划线 _ 可以使你“使用”一个变量，但是丢弃它的值。
    _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, u, pi, n, a3, s4, bs
    // 通常的用法是，在调用拥有多个返回值的函数时，
    // 用下划线抛弃其中的一个参数。下面的例子就是一个脏套路，
    // 调用os.Create并用下划线变量扔掉它的错误代码。
    // 因为我们觉得这个文件一定会成功创建。
    file, _ := os.Create("output.txt")
    fmt.Fprint(file, "这句代码还示范了如何写入文件呢")
    file.Close()

    // 输出变量
    fmt.Println(s, c, a4, s3, d2, m)

    learnFlowControl() // 回到流程控制
}

// 和其他编程语言不同的是，go支持有名称的变量返回值。
// 声明返回值时带上一个名字允许我们在函数内的不同位置
// 只用写return一个词就能将函数内指定名称的变量返回
func learnNamedReturns(x, y int) (z int) {
    z = x * y
    return // z is implicit here, because we named it earlier.

// Go全面支持垃圾回收。Go有指针，但是不支持指针运算。
// 你会因为空指针而犯错，但是不会因为增加指针而犯错。
func learnMemory() (p, q *int) {
    // 返回int型变量指针p和q
    p = new(int)    // 内置函数new分配内存
    // 自动将分配的int赋值0，p不再是空的了。
    s := make([]int, 20)    // 给20个int变量分配一块内存
    s[3] = 7                // 赋值
    r := -2                 // 声明另一个局部变量
    return &s[3], &r        // & 取地址
}

func expensiveComputation() int {
    return 1e6
}

func learnFlowControl() {
    // If需要花括号，括号就免了
    if true {
        fmt.Println("这句话肯定被执行")
    }
    // 用go fmt 命令可以帮你格式化代码，所以不用怕被人吐槽代码风格了，
    // 也不用容忍别人的代码风格。
    if false {
        // pout
    } else {
        // gloat
    }
    // 如果太多嵌套的if语句，推荐使用switch
    x := 1
    switch x {
    case 0:
    case 1:
        // 隐式调用break语句，匹配上一个即停止
    case 2:
        // 不会运行
    }
    // 和if一样，for也不用括号
    for x := 0; x < 3; x++ { // ++ 自增
        fmt.Println("遍历", x)
    }
    // x在这里还是1。为什么？

    // for 是go里唯一的循环关键字，不过它有很多变种
    for { // 死循环
        break    // 骗你的
        continue // 不会运行的
    }

    // 用range可以枚举 array、slice、string、map、channel等不同类型
    // 对于channel，range返回一个值，
    // array、slice、string、map等其他类型返回一对儿
    for key, value := range map[string]int{"one": 1, "two": 2, "three": 3} {
        // 打印map中的每一个键值对
        fmt.Printf("索引：%s, 值为：%d\n", key, value)
    }
    // 如果你只想要值，那就用前面讲的下划线扔掉没用的
    for _, name := range []string{"Bob", "Bill", "Joe"} {
        fmt.Printf("你是。。 %s\n", name)
    }

    // 和for一样，if中的:=先给y赋值，然后再和x作比较。
    if y := expensiveComputation(); y > x {
        x = y
    }
    // 闭包函数
    xBig := func() bool {
        return x > 100 // x是上面声明的变量引用
    }
    fmt.Println("xBig:", xBig()) // true （上面把y赋给x了）
    x /= 1e5                     // x变成10
    fmt.Println("xBig:", xBig()) // 现在是false

    // 除此之外，函数体可以在其他函数中定义并调用，
    // 满足下列条件时，也可以作为参数传递给其他函数：
    //   a) 定义的函数被立即调用
    //   b) 函数返回值符合调用者对类型的要求
    fmt.Println("两数相加乘二: ",
        func(a, b int) int {
            return (a + b) * 2
        }(10, 2)) // Called with args 10 and 2
    // => Add + double two numbers: 24

    // 当你需要goto的时候，你会爱死它的！
    goto love
love:

    learnFunctionFactory() // 返回函数的函数多棒啊
    learnDefer()      // 对defer关键字的简单介绍
    learnInterfaces() // 好东西来了！
}

func learnFunctionFactory() {
    // 空行分割的两个写法是相同的，不过第二个写法比较实用
    fmt.Println(sentenceFactory("原谅")("当然选择", "她！"))

    d := sentenceFactory("原谅")
    fmt.Println(d("当然选择", "她！"))
    fmt.Println(d("你怎么可以", "她？"))
}

// Decorator在一些语言中很常见，在go语言中，
// 接受参数作为其定义的一部分的函数是修饰符的替代品
func sentenceFactory(mystring string) func(before, after string) string {
    return func(before, after string) string {
        return fmt.Sprintf("%s %s %s", before, mystring, after) // new string
    }
}

func learnDefer() (ok bool) {
    // defer表达式在函数返回的前一刻执行
    defer fmt.Println("defer表达式执行顺序为后进先出（LIFO）")
    defer fmt.Println("\n这句话比上句话先输出，因为")
    // 关于defer的用法，例如用defer关闭一个文件，
    // 就可以让关闭操作与打开操作的代码更近一些
    return true
}

// 定义Stringer为一个接口类型，有一个方法String
type Stringer interface {
    String() string
}

// 定义pair为一个结构体，有x和y两个int型变量。
type pair struct {
    x, y int
}

// 定义pair类型的方法，实现Stringer接口。
func (p pair) String() string { // p被叫做“接收器”
    // Sprintf是fmt包中的另一个公有函数。
    // 用 . 调用p中的元素。
    return fmt.Sprintf("(%d, %d)", p.x, p.y)
}

func learnInterfaces() {
    // 花括号用来定义结构体变量，:=在这里将一个结构体变量赋值给p。
    p := pair{3, 4}
    fmt.Println(p.String()) // 调用pair类型p的String方法
    var i Stringer          // 声明i为Stringer接口类型
    i = p                   // 有效！因为p实现了Stringer接口（类似java中的塑型）
    // 调用i的String方法，输出和上面一样
    fmt.Println(i.String())

    // fmt包中的Println函数向对象要它们的string输出，实现了String方法就可以这样使用了。
    // （类似java中的序列化）
    fmt.Println(p) // 输出和上面一样，自动调用String函数。
    fmt.Println(i) // 输出和上面一样。

    learnVariadicParams("great", "learning", "here!")
}

// 有变长参数列表的函数
func learnVariadicParams(myStrings ...interface{}) {
    // 枚举变长参数列表的每个参数值
    // 下划线在这里用来抛弃枚举时返回的数组索引值
    for _, param := range myStrings {
        fmt.Println("param:", param)
    }

    // 将可变参数列表作为其他函数的参数列表
    fmt.Println("params:", fmt.Sprintln(myStrings...))

    learnErrorHandling()
}

func learnErrorHandling() {
    // ", ok"用来判断有没有正常工作
    m := map[int]string{3: "three", 4: "four"}
    if x, ok := m[1]; !ok { // ok 为false，因为m中没有1
        fmt.Println("别找了真没有")
    } else {
        fmt.Print(x) // 如果x在map中的话，x就是那个值喽。
    }
    // 错误可不只是ok，它还可以给出关于问题的更多细节。
    if _, err := strconv.Atoi("non-int"); err != nil { // _ discards value
        // 输出"strconv.ParseInt: parsing "non-int": invalid syntax"
        fmt.Println(err)
    }
    // 待会再说接口吧。同时，
    learnConcurrency()
}

// c是channel类型，一个并发安全的通信对象。
func inc(i int, c chan int) {
    c <- i + 1 // <-把右边的发送到左边的channel。
}

// 我们将用inc函数来并发地增加一些数字。
func learnConcurrency() {
    // 用make来声明一个slice，make会分配和初始化slice，map和channel。
    c := make(chan int)
    // 用go关键字开始三个并发的goroutine，如果机器支持的话，还可能是并行执行。
    // 三个都被发送到同一个channel。
    go inc(0, c) // go is a statement that starts a new goroutine.
    go inc(10, c)
    go inc(-805, c)
    // 从channel中读取结果并打印。
    // 打印出什么东西是不可预知的。
    fmt.Println(<-c, <-c, <-c) // channel在右边的时候，<-是读操作。

    cs := make(chan string)       // 操作string的channel
    cc := make(chan chan string)  // 操作channel的channel
    go func() { c <- 84 }()       // 开始一个goroutine来发送一个新的数字
    go func() { cs <- "wordy" }() // 发送给cs
    // Select类似于switch，但是每个case包括一个channel操作。
    // 它随机选择一个准备好通讯的case。
    select {
    case i := <-c: // 从channel接收的值可以赋给其他变量
        fmt.Println("这是……", i)
    case <-cs: // 或者直接丢弃
        fmt.Println("这是个字符串！")
    case <-cc: // 空的，还没作好通讯的准备
        fmt.Println("别瞎想")
    }
    // 上面c或者cs的值被取到，其中一个goroutine结束，另外一个一直阻塞。

    learnWebProgramming() // Go很适合web编程，我知道你也想学！
}

// http包中的一个简单的函数就可以开启web服务器。
func learnWebProgramming() {
    // ListenAndServe第一个参数指定了监听端口，第二个参数是一个接口，特定是http.Handler。
    go func() {
        err := http.ListenAndServe(":8080", pair{})
        fmt.Println(err) // 不要无视错误。
    }()

    requestServer()
}

// 使pair实现http.Handler接口的ServeHTTP方法。
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // 使用http.ResponseWriter返回数据
    w.Write([]byte("Y分钟golang速成!"))
}

func requestServer() {
    resp, err := http.Get("http://localhost:8080")
    fmt.Println(err)
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Printf("\n服务器消息： `%s`", string(body))
}
```

## 更进一步

关于Go的一切你都可以在[Go官方网站](http://golang.org/)找到。
在那里你可以获得教程参考，在线试用，和更多的资料。
在简单的尝试过后，在[官方文档](https://golang.org/doc/)那里你会得到你所需要的所有资料、关于编写代码的规范、库和命令行工具的文档与Go的版本历史。

强烈推荐阅读语言定义部分，很简单而且很简洁！（赶时髦！）

你还可以前往[Go在线体验中心](https://play.golang.org/p/tnWMjr16Mm)进，在浏览器里修改并运行这些代码，一定要试一试哦！你可以将[https://play.golang.org](https://play.golang.org)当作一个[REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop)，在那里体验语言特性或运行自己的代码，连环境都不用配！

学习Go还要阅读Go[标准库的源代码](http://golang.org/src/)，全部文档化了，可读性非常好，可以学到go，go style和go idioms。在[文档](http://golang.org/pkg/)中点击函数名，源代码就出来了！

[Go by example](https://gobyexample.com/)也是一个学习的好地方。



Go Mobile添加了对移动平台的支持（Android and iOS）。你可以完全用go语言来创造一个app或编写一个可以从Java或Obj-C调用的函数库，敬请参考[Go Mobile page](https://github.com/golang/go/wiki/Mobile)。
---
language: Groovy
filename: learngroovy-cn.groovy
contributors:
    - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"]
translators:
    - ["Todd Gao", "http://github.com/7c00"]
lang: zh-cn
---

Groovy - Java平台的动态语言。[了解更多。](http://www.groovy-lang.org/)

```groovy

/*
  安装：

  1) 安装 GVM - http://gvmtool.net/
  2) 安装 Groovy： gvm install groovy
  3) 启动 groovy 控制台，键入： groovyConsole

*/

//  双斜线开始的是单行注释
/*
像这样的是多行注释
*/

// Hello World
println "Hello world!"

/*
  变量：

  可以给变量赋值，以便稍后使用
*/

def x = 1
println x

x = new java.util.Date()
println x

x = -3.1499392
println x

x = false
println x

x = "Groovy!"
println x

/*
  集合和映射
*/

//创建一个空的列表
def technologies = []

/*** 往列表中增加一个元素 ***/

// 和Java一样
technologies.add("Grails")

// 左移添加，返回该列表
technologies << "Groovy"

// 增加多个元素
technologies.addAll(["Gradle","Griffon"])

/*** 从列表中删除元素 ***/

// 和Java一样
technologies.remove("Griffon")

// 减号也行
technologies = technologies - 'Grails'

/*** 遍历列表 ***/

// 遍历列表中的元素
technologies.each { println "Technology: $it"}
technologies.eachWithIndex { it, i -> println "$i: $it"}

/*** 检查列表内容 ***/

//判断列表是否包含某元素，返回boolean
contained = technologies.contains( 'Groovy' )

// 或
contained = 'Groovy' in technologies

// 检查多个元素
technologies.containsAll(['Groovy','Grails'])

/*** 列表排序 ***/

// 排序列表（修改原列表）
technologies.sort()

// 要想不修改原列表，可以这样：
sortedTechnologies = technologies.sort( false )

/*** 列表操作 ***/

//替换列表元素
Collections.replaceAll(technologies, 'Gradle', 'gradle')

//打乱列表
Collections.shuffle(technologies, new Random())

//清空列表
technologies.clear()

//创建空的映射
def devMap = [:]

//增加值
devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
devMap.put('lastName','Perez')

//遍历映射元素
devMap.each { println "$it.key: $it.value" }
devMap.eachWithIndex { it, i -> println "$i: $it"}

//判断映射是否包含某键
assert devMap.containsKey('name')

//判断映射是否包含某值
assert devMap.containsValue('Roberto')

//取得映射所有的键
println devMap.keySet()

//取得映射所有的值
println devMap.values()

/*
  Groovy Beans

  GroovyBeans 是 JavaBeans，但使用了更简单的语法

  Groovy 被编译为字节码时，遵循下列规则。

    * 如果一个名字声明时带有访问修饰符（public, private, 或者 protected），
      则会生成一个字段（field）。

    * 名字声明时没有访问修饰符，则会生成一个带有public getter和setter的
      private字段，即属性（property）。

    * 如果一个属性声明为final，则会创建一个final的private字段，但不会生成setter。

    * 可以声明一个属性的同时定义自己的getter和setter。

    * 可以声明具有相同名字的属性和字段，该属性会使用该字段。

    * 如果要定义private或protected属性，必须提供声明为private或protected的getter
      和setter。

    * 如果使用显式或隐式的 this（例如 this.foo, 或者 foo）访问类的在编译时定义的属性，
      Groovy会直接访问对应字段，而不是使用getter或者setter

    * 如果使用显式或隐式的 foo 访问一个不存在的属性，Groovy会通过元类（meta class）
      访问它，这可能导致运行时错误。

*/

class Foo {
    // 只读属性
    final String name = "Roberto"

    // 只读属性，有public getter和protected setter
    String language
    protected void setLanguage(String language) { this.language = language }

    // 动态类型属性
    def lastName
}

/*
  逻辑分支和循环
*/

//Groovy支持常见的if - else语法
def x = 3

if(x==1) {
    println "One"
} else if(x==2) {
    println "Two"
} else {
    println "X greater than Two"
}

//Groovy也支持三元运算符
def y = 10
def x = (y > 1) ? "worked" : "failed"
assert x == "worked"

//for循环
//使用区间（range）遍历
def x = 0
for (i in 0 .. 30) {
    x += i
}

//遍历列表
x = 0
for( i in [5,3,2,1] ) {
    x += i
}

//遍历数组
array = (0..20).toArray()
x = 0
for (i in array) {
    x += i
}

//遍历映射
def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
x = 0
for ( e in map ) {
    x += e.value
}

/*
  运算符

  在Groovy中以下常用运算符支持重载：
  http://www.groovy-lang.org/operators.html#Operator-Overloading

  实用的groovy运算符
*/
//展开（spread）运算符：对聚合对象的所有元素施加操作
def technologies = ['Groovy','Grails','Gradle']
technologies*.toUpperCase() // 相当于 technologies.collect { it?.toUpperCase() }

//安全导航（safe navigation）运算符：用来避免NullPointerException
def user = User.get(1)
def username = user?.username


/*
  闭包
  Groovy闭包好比代码块或者方法指针，它是一段代码定义，可以以后执行。

  更多信息见：http://www.groovy-lang.org/closures.html
*/
//例子：
def clos = { println "Hello World!" }

println "Executing the Closure:"
clos()

//传参数给闭包
def sum = { a, b -> println a+b }
sum(2,4)

//闭包可以引用参数列表以外的变量
def x = 5
def multiplyBy = { num -> num * x }
println multiplyBy(10)

// 只有一个参数的闭包可以省略参数的定义
def clos = { print it }
clos( "hi" )

/*
  Groovy可以记忆闭包结果 [1][2][3]
*/
def cl = {a, b ->
    sleep(3000) // 模拟费时操作
    a + b
}

mem = cl.memoize()

def callClosure(a, b) {
    def start = System.currentTimeMillis()
    mem(a, b)
    println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs."
}

callClosure(1, 2)
callClosure(1, 2)
callClosure(2, 3)
callClosure(2, 3)
callClosure(3, 4)
callClosure(3, 4)
callClosure(1, 2)
callClosure(2, 3)
callClosure(3, 4)

/*
  Expando

  Expando类是一种动态bean类，可以给它的实例添加属性和添加闭包作为方法

  http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/
  def user = new Expando(name:"Roberto")
  assert 'Roberto' == user.name

  user.lastName = 'Pérez'
  assert 'Pérez' == user.lastName

  user.showInfo = { out ->
      out << "Name: $name"
      out << ", Last name: $lastName"
  }

  def sw = new StringWriter()
  println user.showInfo(sw)


/*
  元编程(MOP)
*/

//使用ExpandoMetaClass增加行为
String.metaClass.testAdd = {
    println "we added this"
}

String x = "test"
x?.testAdd()

//拦截方法调用
class Test implements GroovyInterceptable {
    def sum(Integer x, Integer y) { x + y }

    def invokeMethod(String name, args) {
        System.out.println "Invoke method $name with args: $args"
    }
}

def test = new Test()
test?.sum(2,3)
test?.multiply(2,3)

//Groovy支持propertyMissing，来处理属性解析尝试
class Foo {
   def propertyMissing(String name) { name }
}
def f = new Foo()

assertEquals "boo", f.boo

/*
  类型检查和静态编译
  Groovy天生是并将永远是一门动态语言，但也支持类型检查和静态编译

  更多： http://www.infoq.com/articles/new-groovy-20
*/
//类型检查
import groovy.transform.TypeChecked

void testMethod() {}

@TypeChecked
void test() {
    testMeethod()

    def name = "Roberto"

    println naameee

}

//另一例子
import groovy.transform.TypeChecked

@TypeChecked
Integer test() {
    Integer num = "1"

    Integer[] numbers = [1,2,3,4]

    Date date = numbers[1]

    return "Test"

}

//静态编译例子
import groovy.transform.CompileStatic

@CompileStatic
int sum(int x, int y) {
    x + y
}

assert sum(2,5) == 7


```

## 进阶资源

[Groovy文档](http://www.groovy-lang.org/documentation.html)

[Groovy web console](http://groovyconsole.appspot.com/)

加入[Groovy用户组](http://www.groovy-lang.org/usergroups.html)

## 图书

* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook)

* [Groovy in Action] (http://manning.com/koenig2/)

* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)

[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html



---
language: Haskell 
filename: learn-haskell-zh.hs
contributors:
    - ["Adit Bhargava", "http://adit.io"]
translators:
    - ["Peiyong Lin", ""]
    - ["chad luo", "http://yuki.rocks"]
lang: zh-cn
---

Haskell 是一门实用的函数式编程语言，因其 Monads 与类型系统而闻名。而我使用它则是因为它异常优雅。用 Haskell 编程令我感到非常快乐。

```haskell
-- 单行注释以两个减号开头
{- 多行注释像这样
    被一个闭合的块包围
-}

----------------------------------------------------
-- 1. 简单的数据类型和操作符
----------------------------------------------------

-- 数字
3 -- 3
-- 数学计算
1 + 1 -- 2
8 - 1 -- 7
10 * 2 -- 20
35 / 5 -- 7.0

-- 默认除法不是整除
35 / 4 -- 8.75

-- 整除
35 `div` 4 -- 8

-- 布尔值
True
False

-- 布尔操作
not True -- False
not False -- True
1 == 1 -- True
1 /= 1 -- False
1 < 10 -- True

-- 在上面的例子中，`not` 是一个接受一个参数的函数。
-- Haskell 不需要括号来调用函数，所有的参数都只是在函数名之后列出来
-- 因此，通常的函数调用模式是：
--   func arg1 arg2 arg3...
-- 你可以查看函数部分了解如何自行编写。

-- 字符串和字符
"This is a string." -- 字符串
'a' -- 字符
'对于字符串你不能使用单引号。' -- 错误！

-- 连接字符串
"Hello " ++ "world!" -- "Hello world!"

-- 一个字符串是一系列字符
['H', 'e', 'l', 'l', 'o'] -- "Hello"
"This is a string" !! 0 -- 'T'


----------------------------------------------------
-- 2. 列表和元组
----------------------------------------------------

-- 一个列表中的每一个元素都必须是相同的类型。
-- 下面两个列表等价
[1, 2, 3, 4, 5]
[1..5]

-- 区间也可以这样
['A'..'F'] -- "ABCDEF"

-- 你可以在区间中指定步进
[0,2..10] -- [0, 2, 4, 6, 8, 10]
[5..1] -- 这样不行，因为 Haskell 默认递增
[5,4..1] -- [5, 4, 3, 2, 1]

-- 列表下标
[0..] !! 5 -- 5

-- 在 Haskell 你可以使用无限列表
[1..] -- 一个含有所有自然数的列表

-- 无限列表的原理是，Haskell 有“惰性求值”。
-- 这意味着 Haskell 只在需要时才会计算。
-- 所以当你获取列表的第 1000 项元素时，Haskell 会返回给你：
[1..] !! 999 -- 1000
-- Haskell 计算了列表中第 1 至 1000 项元素，但这个无限列表中剩下的元素还不存在。
-- Haskell 只有在需要时才会计算它们。

-- 连接两个列表
[1..5] ++ [6..10]

-- 往列表头增加元素
0:[1..5] -- [0, 1, 2, 3, 4, 5]

-- 其它列表操作
head [1..5] -- 1
tail [1..5] -- [2, 3, 4, 5]
init [1..5] -- [1, 2, 3, 4]
last [1..5] -- 5

-- 列表推导 (list comprehension)
[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]

-- 附带条件
[x*2 | x <-[1..5], x*2 > 4] -- [6, 8, 10]

-- 元组中的每一个元素可以是不同类型，但是一个元组的长度是固定的
-- 一个元组
("haskell", 1)

-- 获取元组中的元素（例如，一个含有 2 个元素的元祖）
fst ("haskell", 1) -- "haskell"
snd ("haskell", 1) -- 1

----------------------------------------------------
-- 3. 函数
----------------------------------------------------

-- 一个接受两个变量的简单函数
add a b = a + b

-- 注意，如果你使用 ghci (Hakell 解释器)，你需要使用 `let`，也就是
-- let add a b = a + b

-- 调用函数
add 1 2 -- 3

-- 你也可以使用反引号中置函数名：
1 `add` 2 -- 3

-- 你也可以定义不带字母的函数名，这样你可以定义自己的操作符。
-- 这里有一个做整除的操作符
(//) a b = a `div` b
35 // 4 -- 8

-- Guard：一个在函数中做条件判断的简单方法
fib x
  | x < 2 = x
  | otherwise = fib (x - 1) + fib (x - 2)

-- 模式匹配与 Guard 类似。
-- 这里给出了三个不同的 fib 定义。
-- Haskell 会自动调用第一个符合参数模式的声明
fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)

-- 元组的模式匹配
foo (x, y) = (x + 1, y + 2)

-- 列表的模式匹配
-- 这里 `x` 是列表中第一个元素，`xs` 是列表剩余的部分。
-- 我们可以实现自己的 map 函数：
myMap func [] = []
myMap func (x:xs) = func x:(myMap func xs)

-- 匿名函数带有一个反斜杠，后面跟着所有的参数
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]

-- 在 fold（在一些语言称 为`inject`）中使用匿名函数
-- foldl1 意味着左折叠 (fold left), 并且使用列表中第一个值作为累加器的初始值。
foldl1 (\acc x -> acc + x) [1..5] -- 15

----------------------------------------------------
-- 4. 其它函数
----------------------------------------------------

-- 部分调用
-- 如果你调用函数时没有给出所有参数，它就被“部分调用”。
-- 它将返回一个接受余下参数的函数。
add a b = a + b
foo = add 10 -- foo 现在是一个接受一个数并对其加 10 的函数
foo 5 -- 15

-- 另一种等价写法
foo = (+10)
foo 5 -- 15

-- 函列表合
-- (.) 函数把其它函数链接到一起。
-- 例如，这里 foo 是一个接受一个值的函数。
-- 它对接受的值加 10，并对结果乘以 5，之后返回最后的值。
foo = (*5) . (+10)

-- (5 + 10) * 5 = 75
foo 5 -- 75

-- 修正优先级
-- Haskell 有另外一个函数 `$` 可以改变优先级。
-- `$` 使得 Haskell 先计算其右边的部分，然后调用左边的部分。
-- 你可以使用 `$` 来移除多余的括号。

-- 修改前
(even (fib 7)) -- False

-- 修改后
even . fib $ 7 -- False

-- 等价地
even $ fib 7 -- False

----------------------------------------------------
-- 5. 类型声明
----------------------------------------------------

-- Haskell 有一个非常强大的类型系统，一切都有一个类型声明。

-- 一些基本的类型：
5 :: Integer
"hello" :: String
True :: Bool

-- 函数也有类型
-- `not` 接受一个布尔型返回一个布尔型
-- not :: Bool -> Bool

-- 这是接受两个参数的函数
-- add :: Integer -> Integer -> Integer

-- 当你定义一个值，声明其类型是一个好做法
double :: Integer -> Integer
double x = x * 2

----------------------------------------------------
-- 6. 控制流和 If 语句
----------------------------------------------------

-- if 语句：
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"

-- if 语句也可以有多行，注意缩进：
haskell = if 1 == 1
            then "awesome"
            else "awful"

-- case 语句
-- 解析命令行参数：
case args of
  "help" -> printHelp
  "start" -> startProgram
  _ -> putStrLn "bad args"

-- Haskell 没有循环，它使用递归
-- map 对一个列表中的每一个元素调用一个函数
map (*2) [1..5] -- [2, 4, 6, 8, 10]

-- 你可以使用 map 来编写 for 函数
for array func = map func array

-- 调用
for [0..5] $ \i -> show i

-- 我们也可以像这样写
for [0..5] show

-- 你可以使用 foldl 或者 foldr 来分解列表
-- foldl <fn> <initial value> <list>
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43

-- 等价于
(2 * (2 * (2 * 4 + 1) + 2) + 3)

-- foldl 从左开始，foldr 从右
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16

-- 现在它等价于
(2 * 3 + (2 * 2 + (2 * 1 + 4)))

----------------------------------------------------
-- 7. 数据类型
----------------------------------------------------

-- 在 Haskell 中声明你自己的数据类型：
data Color = Red | Blue | Green

-- 现在你可以在函数中使用它：
say :: Color -> String
say Red = "You are Red!"
say Blue = "You are Blue!"
say Green =  "You are Green!"

-- 你的数据类型也可以有参数：
data Maybe a = Nothing | Just a

-- 这些都是 Maybe 类型：
Just "hello"    -- `Maybe String` 类型
Just 1          -- `Maybe Int` 类型
Nothing         -- 对任意 `a` 为 `Maybe a` 类型

----------------------------------------------------
-- 8. Haskell IO
----------------------------------------------------

-- 虽然不解释 Monads 就无法完全解释 IO，但大致了解并不难。

-- 当执行一个 Haskell 程序时，函数 `main` 就被调用。
-- 它必须返回一个类型 `IO ()` 的值。例如：
main :: IO ()
main = putStrLn $ "Hello, sky! " ++ (say Blue) 
-- putStrLn 的类型是 String -> IO ()

-- 如果你的程序输入 String 返回 String，那样编写 IO 是最简单的。
-- 函数
--    interact :: (String -> String) -> IO ()
-- 输入一些文本，对其调用一个函数，并打印输出。

countLines :: String -> String
countLines = show . length . lines

main' = interact countLines

-- 你可以认为一个 `IO ()` 类型的值是表示计算机做的一系列操作，类似命令式语言。
-- 我们可以使用 `do` 声明来把动作连接到一起。
-- 举个列子
sayHello :: IO ()
sayHello = do 
   putStrLn "What is your name?"
   name <- getLine -- 这里接受一行输入并绑定至 "name"
   putStrLn $ "Hello, " ++ name
   
-- 练习：编写只读取一行输入的 `interact`
   
-- 然而，`sayHello` 中的代码将不会被执行。唯一被执行的动作是 `main` 的值。
-- 为了运行 `sayHello`，注释上面 `main` 的定义，替换为：
--   main = sayHello

-- 让我们来更进一步理解刚才所使用的函数 `getLine` 是怎样工作的。它的类型是：
--    getLine :: IO String
-- 你可以认为一个 `IO a` 类型的值代表了一个运行时会生成一个 `a` 类型值的程序。
-- （可能伴随其它行为）
-- 我们可以通过 `<-` 保存和重用这个值。
-- 我们也可以实现自己的 `IO String` 类型函数：
action :: IO String
action = do
   putStrLn "This is a line. Duh"
   input1 <- getLine 
   input2 <- getLine
   -- `do` 语句的类型是它的最后一行
   -- `return` 不是关键字，只是一个普通函数
   return (input1 ++ "\n" ++ input2) -- return :: String -> IO String

-- 我们可以像调用 `getLine` 一样调用它
main'' = do
    putStrLn "I will echo two lines!"
    result <- action 
    putStrLn result
    putStrLn "This was all, folks!"

-- `IO` 类型是一个 "Monad" 的例子。
-- Haskell 通过使用 Monad 使得其本身为纯函数式语言。
-- 任何与外界交互的函数（即 IO）都在它的类型声明中标记为 `IO`。
-- 这告诉我们什么样的函数是“纯洁的”(不与外界交互，不修改状态) ，
-- 什么样的函数不是 “纯洁的”。
-- 这个功能非常强大，因为纯函数并发非常容易，由此在 Haskell 中做并发非常容易。

----------------------------------------------------
-- 9. Haskell REPL
----------------------------------------------------

-- 键入 `ghci` 开始 REPL。
-- 现在你可以键入 Haskell 代码。
-- 任何新值都需要通过 `let` 来创建
let foo = 5

-- 你可以通过命令 `:t` 查看任何值的类型
>:t foo
foo :: Integer

-- 你也可以运行任何 `IO ()`类型的动作
> sayHello
What is your name?
Friend!
Hello, Friend!

```

Haskell 还有许多内容，包括类型类 (typeclasses) 与 Monads。这些都是令 Haskell 编程非常有趣的好东西。我们最后给出 Haskell 的一个例子，一个快速排序的实现：

```haskell
qsort [] = []
qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
    where lesser  = filter (< p) xs
          greater = filter (>= p) xs
```

安装 Haskell 很简单。你可以[从这里获得](http://www.haskell.org/platform/)。

你可以从优秀的
[Learn you a Haskell](http://learnyouahaskell.com/) 或者
[Real World Haskell](http://book.realworldhaskell.org/)
找到更平缓的入门介绍。
---
language: html
filename: learnhtml-cn.html
contributors:
    - ["Christophe THOMAS", "https://github.com/WinChris"]
translators:
    - ["zxyqwe", "https://github.com/zxyqwe"]
lang: zh-cn
---

HTML是超文本标记语言的缩写。
这门语言可以让我们为万维网创造页面。
这是一门标记语言，它允许我们用代码来指示网页上文字和数据应该如何显示。
实际上html文件是简单的文本文件。
什么是标记？标记是通过使用开始和结束标签包围数据的方法，来组织管理页面上的数据。
这些标记对它们环绕的文本有重要的意义。
和其它计算机语言意义，HTML有很多版本。这里我们将讨论HTML5。

**注意：**  你可以在类似[codepen](http://codepen.io/pen/)的网站上的教程中，尝试不同的标签和元素带来的效果，理解它们如何起效，并且逐渐熟悉这门语言。
本文主要关注HTML的语法和一些有用的小窍门。


```html
<!-- 注释要像本行一样被包围起来！ -->

<!-- #################### 标签 #################### -->
   
<!-- 下面是一个我们将要分析的HTML文件的例子。 -->

<!doctype html>
	<html>
		<head>
			<title>我的网站</title>
		</head>
		<body>
			<h1>Hello, world!</h1>
			<a href = "http://codepen.io/anon/pen/xwjLbZ">来看看这里有什么</a>
			<p>这是一个段落。</p>
			<p>这是另外一个段落。</p>
			<ul>
				<li>这是一个非计数列表的一项（项目符合列表）</li>
				<li>这是另一项</li>
				<li>这是列表中的最后一项</li>
			</ul>
		</body>
	</html>

<!-- 一个HTML文件通常开始于向浏览器表明本页面是HTML。 -->
<!doctype html>

<!-- 在这之后，由<html>开始标签作为起始。 -->
<html>

<!-- 在文件的最后会由</html>标签结束。 -->
</html>

<!-- 在最终的标签后面应该没有任何东西。 -->

<!-- 在其中（在开始标签<html>和结束标签</html>中间）我们可以看到： -->

<!-- 由标签<head>定义的头部 （头部必须被</head>标签关闭）。 -->
<!-- 头部包含一些不显示的描述和额外信息；这些是元数据。 -->

<head>
	<title>我的网站</title><!-- <title>标签告诉浏览器在浏览器窗口的标题区和标签栏应该显示什么标题。 -->
</head>

<!-- 在<head>区域之后，我们可以看到<body>标签 -->
<!-- 在这点之前的内容都不会显示在浏览器的窗口中。 -->
<!-- 我们必须在正文区填上需要显示的内容。 -->

<body>
	<h1>Hello, world!</h1> <!-- h1标签创建了一个标题 -->
	<!-- <h1>标签可以有一些副标题，从最重要的（h2）到最细微的（h6）。 -->
	<a href = "http://codepen.io/anon/pen/xwjLbZ">来看看这里有什么</a> <!-- 一个指向href=""属性中URL的超链接 -->
	<p>这是一个段落。</p> <!-- <p>标签让我们在html页面中显示文字 -->
	<p>这是另外一个段落。</p>
	<ul> <!-- <ul>标签创建了一个项目符合列表。 -->
	<!-- 如果需要一个编号列表，我们可以使用<ol>标签。这样会在在第一项前显示1.，第二项前显示2.，以此类推。 -->
		<li>这是一个非计数列表的一项（项目符合列表）</li>
		<li>这是另一项</li>
		<li>这是列表中的最后一项</li>
	</ul>
</body>

<!-- 好了，创建一个HTML文件就是这么简单。 -->

<!-- 当然我们还可以加入很多额外的HTML标签类型。 -->

<!-- 插入图片。 -->
<img src="http://i.imgur.com/XWG0O.gif"/> <!-- 图片源是由src=""属性指明的 -->
<!-- 图片源可以是一个URL或者你电脑上一个文件的路径。 -->

<!-- 创建表格也没问题。 -->

<table> <!-- 我们开始一个<table>元素 -->
	<tr> <!-- <tr>让我们创建一行 -->
		<th>第一个表头</th> <!-- <th>让我们给表格列一个标题 -->
		<th>第二个表头</th>
	</tr>
	<tr>
		<td>第一行第一列</td> <!-- <td>让我们创建一个单元格 -->
		<td>第一行第二列</td>
	</tr>
	<tr>
		<td>第二行第一列</td>
		<td>第二行第二列</td>
	</tr>
</table>

```

## 使用

HTML文件使用`.html`后缀。

## 扩展阅读

* [维基百科](https://en.wikipedia.org/wiki/HTML)
* [HTML tutorial](https://developer.mozilla.org/en-US/docs/Web/HTML)
* [W3School](http://www.w3schools.com/html/html_intro.asp)
---
name: java
category: language
language: java
lang: zh-cn
filename: LearnJava-zh.java
contributors:
    - ["Jake Prather", "http://github.com/JakeHP"]
translators:
    - ["Chenbo Li", "http://binarythink.net"]
---

Java是一个通用的程序语言, 包含并发, 基于类的面向对象等特性
[阅读更多](http://docs.oracle.com/javase/tutorial/java/index.html)

```java
// 单行注释
/*
多行注释
*/
/**
JavaDoc（Java文档）注释是这样的。可以用来描述类和类的属性。
*/

// 导入 java.util中的 ArrayList 类
import java.util.ArrayList;
// 导入 java.security 包中的所有类
import java.security.*;

// 每个 .java 文件都包含一个public类，这个类的名字必须和这个文件名一致。
public class LearnJava {

    // 每个程序都需要有一个main函数作为入口
    public static void main (String[] args) {

        // 使用 System.out.println 来输出到标准输出
        System.out.println("Hello World!");
        System.out.println(
            "Integer: " + 10 +
            " Double: " + 3.14 +
            " Boolean: " + true);

        // 如果要在输出后不自动换行，可以使用System.out.print方法。
        System.out.print("Hello ");
        System.out.print("World");


        ///////////////////////////////////////
        // 类型与变量
        ///////////////////////////////////////

        // 用 <type> <name> 来声明变量
        // 字节类型 - 8位补码表示
        // (-128 <= 字节 <= 127)
        byte fooByte = 100;

        // 短整型 - 16位补码表示
        // (-32,768 <= 短整型 <= 32,767)
        short fooShort = 10000;

        // 整型 - 32位补码表示
        // (-2,147,483,648 <= 整型 <= 2,147,483,647)
        int fooInt = 1;

        // 长整型 - 64位补码表示
        // (-9,223,372,036,854,775,808 <= 长整型 <= 9,223,372,036,854,775,807)
        long fooLong = 100000L;
        // L可以用来表示一个数字是长整型的。
        // 其他的数字都默认为整型。

        // 注意：Java中没有无符号类型

        // 浮点型 - 即 IEEE 754 规定的32位单精度浮点类型
        float fooFloat = 234.5f;
        // f用来表示一个数字是浮点型的。
        // 否则会被默认当做是双精度浮点型。

        // 双精度浮点型 - 即 IEEE 754 规定的64位双精度浮点类型
        double fooDouble = 123.4;

        // 布尔类型 - true 与 false
        boolean fooBoolean = true;
        boolean barBoolean = false;

        // 字符类型 - 16位 Unicode编码字符
        char fooChar = 'A';

        // 用 final 可以使一个常量不可更改
        final int HOURS_I_WORK_PER_WEEK = 9001;

        // 字符串
        String fooString = "My String Is Here!";

        // \n 代表一个新的换行
        String barString = "Printing on a new line?\nNo Problem!";
        // \t 代表一个新的制表符
        String bazString = "Do you want to add a tab?\tNo Problem!";
        System.out.println(fooString);
        System.out.println(barString);
        System.out.println(bazString);

        // 数组
        // 数组在声明时大小必须已经确定
        // 数组的声明格式:
        //<数据类型> [] <变量名> = new <数据类型>[<数组大小>];
        int [] intArray = new int[10];
        String [] stringArray = new String[1];
        boolean [] booleanArray = new boolean[100];

        // 声明并初始化数组也可以这样:
        int [] intArray = {9000, 1000, 1337};

        // 随机访问数组中的元素
        System.out.println("intArray @ 0: " + intArray[0]);

        // 数组下标从0开始并且可以被更改
        intArray[1] = 1;
        System.out.println("intArray @ 1: " + intArray[1]); // => 1

        // 其他数据类型
        // ArrayLists - 类似于数组，但是功能更多，并且大小也可以改变
        // LinkedLists
        // Maps
        // HashMaps

        ///////////////////////////////////////
        // 操作符
        ///////////////////////////////////////
        System.out.println("\n->Operators");

        int i1 = 1, i2 = 2; // 多重声明可以简化

        // 算数运算
        System.out.println("1+2 = " + (i1 + i2)); // => 3
        System.out.println("2-1 = " + (i2 - i1)); // => 1
        System.out.println("2*1 = " + (i2 * i1)); // => 2
        System.out.println("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down)

        // 取余
        System.out.println("11%3 = "+(11 % 3)); // => 2

        // 比较操作符
        System.out.println("3 == 2? " + (3 == 2)); // => false
        System.out.println("3 != 2? " + (3 != 2)); // => true
        System.out.println("3 > 2? " + (3 > 2)); // => true
        System.out.println("3 < 2? " + (3 < 2)); // => false
        System.out.println("2 <= 2? " + (2 <= 2)); // => true
        System.out.println("2 >= 2? " + (2 >= 2)); // => true

        // 位运算操作符
        /*
        ~       取反，求反码
        <<      带符号左移
        >>      带符号右移
        >>>     无符号右移
        &       和
        ^       异或
        |       相容或
        */

        // 自增
        int i = 0;
        System.out.println("\n->Inc/Dec-rementation");
        // ++ 和 -- 操作符使变量加或减1。放在变量前面或者后面的区别是整个表达
        // 式的返回值。操作符在前面时，先加减，后取值。操作符在后面时，先取值
        // 后加减。
        System.out.println(i++); // 后自增 i = 1, 输出0
        System.out.println(++i); // 前自增 i = 2, 输出2
        System.out.println(i--); // 后自减 i = 1, 输出2
        System.out.println(--i); // 前自减 i = 0, 输出0

        ///////////////////////////////////////
        // 控制结构
        ///////////////////////////////////////
        System.out.println("\n->Control Structures");

        // If语句和C的类似
        int j = 10;
        if (j == 10){
            System.out.println("I get printed");
        } else if (j > 10) {
            System.out.println("I don't");
        } else {
            System.out.println("I also don't");
        }

        // While循环
        int fooWhile = 0;
        while(fooWhile < 100)
        {
            //System.out.println(fooWhile);
            //增加计数器
            //遍历99次， fooWhile 0->99
            fooWhile++;
        }
        System.out.println("fooWhile Value: " + fooWhile);

        // Do While循环
        int fooDoWhile = 0;
        do
        {
            //System.out.println(fooDoWhile);
            //增加计数器
            //遍历99次, fooDoWhile 0->99
            fooDoWhile++;
        }while(fooDoWhile < 100);
        System.out.println("fooDoWhile Value: " + fooDoWhile);

        // For 循环
        int fooFor;
        //for 循环结构 => for(<起始语句>; <循环进行的条件>; <步长>)
        for(fooFor=0; fooFor<10; fooFor++){
            //System.out.println(fooFor);
            //遍历 10 次, fooFor 0->9
        }
        System.out.println("fooFor Value: " + fooFor);

        // Switch Case 语句
        // switch可以用来处理 byte, short, char, 和 int 数据类型
        // 也可以用来处理枚举类型,字符串类,和原始数据类型的包装类：
        // Character, Byte, Short, 和 Integer
        int month = 3;
        String monthString;
        switch (month){
            case 1:
                    monthString = "January";
                    break;
            case 2:
                    monthString = "February";
                    break;
            case 3:
                    monthString = "March";
                    break;
            default:
                    monthString = "Some other month";
                    break;
        }
        System.out.println("Switch Case Result: " + monthString);


        ///////////////////////////////////////
        // 类型转换
        ///////////////////////////////////////

        // 数据转换

        // 将字符串转换为整型
        Integer.parseInt("123");//返回整数123

        // 将整型转换为字符串
        Integer.toString(123);//返回字符串"123"

        // 其他的数据也可以进行互相转换:
        // Double
        // Long
        // String

        // 类型转换
        // 你也可以对java对象进行类型转换, 但其中会牵扯到很多概念
        // 在这里可以查看更详细的信息:
        // http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


        ///////////////////////////////////////
        // 类与函数
        ///////////////////////////////////////

        System.out.println("\n->Classes & Functions");

        // (Bicycle类定义如下)

        // 用new来实例化一个类
        Bicycle trek = new Bicycle();

        // 调用对象的方法
        trek.speedUp(3); // 需用getter和setter方法
        trek.setCadence(100);

        // toString 可以把对象转换为字符串
        System.out.println("trek info: " + trek.toString());

    } // main 方法结束
} // LearnJava 类结束


// 你也可以把其他的非public类放入到.java文件中


// 类定义的语法:
// <public/private/protected> class <类名>{
//    //成员变量, 构造函数, 函数
//    //Java中函数被称作方法
// }

class Bicycle {

    // Bicycle 类的成员变量和方法
    public int cadence; // Public: 任意位置均可访问
    private int speed;  // Private: 只在同类中可以访问
    protected int gear; // Protected: 可以在同类与子类中可以访问
    String name; // default: 可以在包内中可以访问

    // 构造函数是初始化一个对象的方式
    // 以下是一个默认构造函数
    public Bicycle() {
        gear = 1;
        cadence = 50;
        speed = 5;
        name = "Bontrager";
    }

    // 以下是一个含有参数的构造函数
    public Bicycle(int startCadence, int startSpeed, int startGear, String name) {
        this.gear = startGear;
        this.cadence = startCadence;
        this.speed = startSpeed;
        this.name = name;
    }

    // 函数语法:
    // <public/private/protected> <返回值类型> <函数名称>(<参数列表>)

    // Java类中经常会用getter和setter来对成员变量进行操作

    // 方法声明的语法:
    // <作用域> <返回值类型> <方法名>(<参数列表>)
    public int getCadence() {
        return cadence;
    }

    // void返回值函数没有返回值
    public void setCadence(int newValue) {
        cadence = newValue;
    }

    public void setGear(int newValue) {
        gear = newValue;
    }

    public void speedUp(int increment) {
        speed += increment;
    }

    public void slowDown(int decrement) {
        speed -= decrement;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    //返回对象属性的方法
    @Override
    public String toString() {
        return "gear: " + gear +
                " cadence: " + cadence +
                " speed: " + speed +
                " name: " + name;
    }
} // Bicycle 类结束

// PennyFarthing 是 Bicycle 的子类
class PennyFarthing extends Bicycle {
    // (Penny Farthings 是前轮很大的 Bicycle， 并且没有齿轮)

    public PennyFarthing(int startCadence, int startSpeed){
        // 通过super调用父类的构造函数
        super(startCadence, startSpeed, 0, "PennyFarthing");
    }

    // 你可以用@注释来表示需要重载的方法
    // 了解更多的注释使用方法，可以访问下面的地址：
    // http://docs.oracle.com/javase/tutorial/java/annotations/
    @Override
    public void setGear(int gear) {
        gear = 0;
    }

}

```

## 更多阅读

下面的链接只是为了便于大家理解这些主题而给出的，对于具体的例子请大家自行Google

其他主题：

* [Java 官方教程](http://docs.oracle.com/javase/tutorial/index.html)

* [Java 访问修饰符](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)

* [面向对象程序设计概念](http://docs.oracle.com/javase/tutorial/java/concepts/index.html):
    * [继承](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
    * [多态](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html)
    * [抽象](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)

* [异常](http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html)

* [接口](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

* [泛型](http://docs.oracle.com/javase/tutorial/java/generics/index.html)

* [Java代码规范](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html)
---
language: javascript
category: language
name: javascript
filename: javascript-zh.js
contributors:
    - ["Adam Brenecki", "http://adam.brenecki.id.au"]
    - ["Ariel Krakowski", "http://www.learneroo.com"]
translators:
    - ["Chenbo Li", "http://binarythink.net"]
    - ["Guodong Qu", "https://github.com/jasonqu"]
lang: zh-cn
---

Javascript于1995年由网景公司的Brendan Eich发明。
最初发明的目的是作为一个简单的网站脚本语言，来作为
复杂网站应用java的补充。但由于它与网页结合度很高并且由浏览器内置支持，
所以javascript变得比java在前端更为流行了。

不过 JavaScript 可不仅仅只用于浏览器： Node.js，一个基于Google Chrome V8引擎的独立运行时环境，也越来越流行。

很欢迎来自您的反馈，您可以通过下列方式联系到我：
[@adambrenecki](https://twitter.com/adambrenecki), 或者
[adam@brenecki.id.au](mailto:adam@brenecki.id.au).

```js
// 注释方式和C很像，这是单行注释
/* 这是多行
   注释 */

// 语句可以以分号结束
doStuff();

// ... 但是分号也可以省略，每当遇到一个新行时，分号会自动插入（除了一些特殊情况）。
doStuff()

// 因为这些特殊情况会导致意外的结果，所以我们在这里保留分号。

///////////////////////////////////
// 1. 数字、字符串与操作符

// Javascript 只有一种数字类型(即 64位 IEEE 754 双精度浮点 double)。
// double 有 52 位表示尾数，足以精确存储大到 9✕10¹⁵ 的整数。
3; // = 3
1.5; // = 1.5

// 所有基本的算数运算都如你预期。
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7

// 包括无法整除的除法。
5 / 2; // = 2.5

// 位运算也和其他语言一样；当你对浮点数进行位运算时，
// 浮点数会转换为*至多* 32 位的无符号整数。
1 << 2; // = 4

// 括号可以决定优先级。
(1 + 3) * 2; // = 8

// 有三种非数字的数字类型
Infinity; // 1/0 的结果
-Infinity; // -1/0 的结果
NaN; // 0/0 的结果

// 也有布尔值。
true;
false;

// 可以通过单引号或双引号来构造字符串。
'abc';
"Hello, world";

// 用！来取非
!true; // = false
!false; // = true

// 相等 ===
1 === 1; // = true
2 === 1; // = false

// 不等 !=
1 !== 1; // = false
2 !== 1; // = true

// 更多的比较操作符 
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

// 字符串用+连接
"Hello " + "world!"; // = "Hello world!"

// 字符串也可以用 < 、> 来比较
"a" < "b"; // = true

// 使用“==”比较时会进行类型转换...
"5" == 5; // = true
null == undefined; // = true

// ...除非你是用 ===
"5" === 5; // = false
null === undefined; // = false 

// ...但会导致奇怪的行为
13 + !0; // 14
"13" + !0; // '13true'

// 你可以用`charAt`来得到字符串中的字符
"This is a string".charAt(0);  // = 'T'

// ...或使用 `substring` 来获取更大的部分。
"Hello world".substring(0, 5); // = "Hello"

// `length` 是一个属性，所以不要使用 ().
"Hello".length; // = 5

// 还有两个特殊的值：`null`和`undefined`
null;      // 用来表示刻意设置的空值
undefined; // 用来表示还没有设置的值(尽管`undefined`自身实际是一个值)

// false, null, undefined, NaN, 0 和 "" 都是假的；其他的都视作逻辑真
// 注意 0 是逻辑假而  "0"是逻辑真，尽管 0 == "0"。

///////////////////////////////////
// 2. 变量、数组和对象

// 变量需要用`var`关键字声明。Javascript是动态类型语言，
// 所以你无需指定类型。 赋值需要用 `=` 
var someVar = 5;

// 如果你在声明时没有加var关键字，你也不会得到错误...
someOtherVar = 10;

// ...但是此时这个变量就会在全局作用域被创建，而非你定义的当前作用域

// 没有被赋值的变量都会被设置为undefined
var someThirdVar; // = undefined

// 对变量进行数学运算有一些简写法：
someVar += 5; // 等价于 someVar = someVar + 5; someVar 现在是 10 
someVar *= 10; // 现在 someVar 是 100

// 自增和自减也有简写
someVar++; // someVar 是 101
someVar--; // 回到 100

// 数组是任意类型组成的有序列表
var myArray = ["Hello", 45, true];

// 数组的元素可以用方括号下标来访问。
// 数组的索引从0开始。
myArray[1]; // = 45

// 数组是可变的，并拥有变量 length。
myArray.push("World");
myArray.length; // = 4

// 在指定下标添加/修改
myArray[3] = "Hello";

// javascript中的对象相当于其他语言中的“字典”或“映射”：是键-值对的无序集合。
var myObj = {key1: "Hello", key2: "World"};

// 键是字符串，但如果键本身是合法的js标识符，则引号并非是必须的。
// 值可以是任意类型。
var myObj = {myKey: "myValue", "my other key": 4};

// 对象属性的访问可以通过下标
myObj["my other key"]; // = 4

// ... 或者也可以用 . ，如果属性是合法的标识符
myObj.myKey; // = "myValue"

// 对象是可变的；值也可以被更改或增加新的键
myObj.myThirdKey = true;

// 如果你想要获取一个还没有被定义的值，那么会返回undefined
myObj.myFourthKey; // = undefined

///////////////////////////////////
// 3. 逻辑与控制结构

// 本节介绍的语法与Java的语法几乎完全相同

// `if`语句和其他语言中一样。
var count = 1;
if (count == 3){
    // count 是 3 时执行
} else if (count == 4){
    // count 是 4 时执行
} else {
    // 其他情况下执行 
}

// while循环
while (true) {
    // 无限循环
}

// Do-while 和 While 循环很像 ，但前者会至少执行一次
var input;
do {
    input = getInput();
} while (!isValid(input))

// `for`循环和C、Java中的一样：
// 初始化; 继续执行的条件; 迭代。
for (var i = 0; i < 5; i++){
    // 遍历5次
}

// && 是逻辑与, || 是逻辑或
if (house.size == "big" && house.colour == "blue"){
    house.contains = "bear";
}
if (colour == "red" || colour == "blue"){
    // colour是red或者blue时执行
}

// && 和 || 是“短路”语句，它在设定初始化值时特别有用 
var name = otherName || "default";

// `switch`语句使用`===`检查相等性。
// 在每一个case结束时使用 'break'
// 否则其后的case语句也将被执行。 
grade = 'B';
switch (grade) {
  case 'A':
    console.log("Great job");
    break;
  case 'B':
    console.log("OK job");
    break;
  case 'C':
    console.log("You can do better");
    break;
  default:
    console.log("Oy vey");
    break;
}

///////////////////////////////////
// 4. 函数、作用域、闭包

// JavaScript 函数由`function`关键字定义
function myFunction(thing){
    return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"

// 注意被返回的值必须开始于`return`关键字的那一行，
// 否则由于自动的分号补齐，你将返回`undefined`。
// 在使用Allman风格的时候要注意.
function myFunction()
{
    return // <- 分号自动插在这里
    {
        thisIsAn: 'object literal'
    }
}
myFunction(); // = undefined

// javascript中函数是一等对象，所以函数也能够赋给一个变量，
// 并且被作为参数传递 —— 比如一个事件处理函数：
function myFunction(){
    // 这段代码将在5秒钟后被调用
}
setTimeout(myFunction, 5000);
// 注意：setTimeout不是js语言的一部分，而是由浏览器和Node.js提供的。

// 函数对象甚至不需要声明名称 —— 你可以直接把一个函数定义写到另一个函数的参数中
setTimeout(function(){
    // 这段代码将在5秒钟后被调用
}, 5000);

// JavaScript 有函数作用域；函数有其自己的作用域而其他的代码块则没有。
if (true){
    var i = 5;
}
i; // = 5 - 并非我们在其他语言中所期望得到的undefined

// 这就导致了人们经常使用的“立即执行匿名函数”的模式，
// 这样可以避免一些临时变量扩散到全局作用域去。
(function(){
    var temporary = 5;
    // 我们可以访问修改全局对象（"global object"）来访问全局作用域，
    // 在web浏览器中是`window`这个对象。 
    // 在其他环境如Node.js中这个对象的名字可能会不同。
    window.permanent = 10;
})();
temporary; // 抛出引用异常ReferenceError
permanent; // = 10

// javascript最强大的功能之一就是闭包。
// 如果一个函数在另一个函数中定义，那么这个内部函数就拥有外部函数的所有变量的访问权，
// 即使在外部函数结束之后。
function sayHelloInFiveSeconds(name){
    var prompt = "Hello, " + name + "!";
    // 内部函数默认是放在局部作用域的，
    // 就像是用`var`声明的。
    function inner(){
        alert(prompt);
    }
    setTimeout(inner, 5000);
    // setTimeout是异步的，所以 sayHelloInFiveSeconds 函数会立即退出，
    // 而 setTimeout 会在后面调用inner
    // 然而，由于inner是由sayHelloInFiveSeconds“闭合包含”的，
    // 所以inner在其最终被调用时仍然能够访问`prompt`变量。
}
sayHelloInFiveSeconds("Adam"); // 会在5秒后弹出 "Hello, Adam!"


///////////////////////////////////
// 5. 对象、构造函数与原型

//  对象可以包含方法。
var myObj = {
    myFunc: function(){
        return "Hello world!";
    }
};
myObj.myFunc(); // = "Hello world!"

// 当对象中的函数被调用时，这个函数可以通过`this`关键字访问其依附的这个对象。
myObj = {
    myString: "Hello world!",
    myFunc: function(){
        return this.myString;
    }
};
myObj.myFunc(); // = "Hello world!"

// 但这个函数访问的其实是其运行时环境，而非定义时环境，即取决于函数是如何调用的。
// 所以如果函数被调用时不在这个对象的上下文中，就不会运行成功了。
var myFunc = myObj.myFunc;
myFunc(); // = undefined

// 相应的，一个函数也可以被指定为一个对象的方法，并且可以通过`this`访问
// 这个对象的成员，即使在函数被定义时并没有依附在对象上。
var myOtherFunc = function(){
    return this.myString.toUpperCase();
}
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"

// 当我们通过`call`或者`apply`调用函数的时候，也可以为其指定一个执行上下文。
var anotherFunc = function(s){
    return this.myString + s;
}
anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"

// `apply`函数几乎完全一样，只是要求一个array来传递参数列表。
anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!"

// 当一个函数接受一系列参数，而你想传入一个array时特别有用。
Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6

// 但是`call`和`apply`只是临时的。如果我们希望函数附着在对象上，可以使用`bind`。
var boundFunc = anotherFunc.bind(myObj);
boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"

// `bind` 也可以用来部分应用一个函数（柯里化）。
var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16

// 当你通过`new`关键字调用一个函数时，就会创建一个对象，
// 而且可以通过this关键字访问该函数。
// 设计为这样调用的函数就叫做构造函数。
var MyConstructor = function(){
    this.myNumber = 5;
}
myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.myNumber; // = 5

// 每一个js对象都有一个‘原型’。当你要访问一个实际对象中没有定义的一个属性时，
// 解释器就回去找这个对象的原型。

// 一些JS实现会让你通过`__proto__`属性访问一个对象的原型。
// 这虽然对理解原型很有用，但是它并不是标准的一部分；
// 我们后面会介绍使用原型的标准方式。
var myObj = {
    myString: "Hello world!"
};
var myPrototype = {
    meaningOfLife: 42,
    myFunc: function(){
        return this.myString.toLowerCase()
    }
};

myObj.__proto__ = myPrototype;
myObj.meaningOfLife; // = 42

// 函数也可以工作。
myObj.myFunc() // = "hello world!"

// 当然，如果你要访问的成员在原型当中也没有定义的话，解释器就会去找原型的原型，以此类推。
myPrototype.__proto__ = {
    myBoolean: true
};
myObj.myBoolean; // = true

// 这其中并没有对象的拷贝；每个对象实际上是持有原型对象的引用。
// 这意味着当我们改变对象的原型时，会影响到其他以这个原型为原型的对象。
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43

// 我们知道 `__proto__` 并非标准规定，实际上也没有标准办法来修改一个已存在对象的原型。
// 然而，我们有两种方式为指定原型创建一个新的对象。

// 第一种方式是 Object.create，这个方法是在最近才被添加到Js中的，
// 因此并不是所有的JS实现都有这个方法
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 43

// 第二种方式可以在任意版本中使用，不过必须通过构造函数。
// 构造函数有一个属性prototype。但是它 *不是* 构造函数本身的原型；相反，
// 是通过构造函数和new关键字创建的新对象的原型。
MyConstructor.prototype = {
    myNumber: 5,
    getMyNumber: function(){
        return this.myNumber;
    }
};
var myNewObj2 = new MyConstructor();
myNewObj2.getMyNumber(); // = 5
myNewObj2.myNumber = 6
myNewObj2.getMyNumber(); // = 6

// 字符串和数字等内置类型也有通过构造函数来创建的包装类型
var myNumber = 12;
var myNumberObj = new Number(12);
myNumber == myNumberObj; // = true

// 但是它们并非严格等价
typeof myNumber; // = 'number'
typeof myNumberObj; // = 'object'
myNumber === myNumberObj; // = false
if (0){
    // 这段代码不会执行，因为0代表假
}

// 不过，包装类型和内置类型共享一个原型，
// 所以你实际可以给内置类型也增加一些功能，例如对string：
String.prototype.firstCharacter = function(){
    return this.charAt(0);
}
"abc".firstCharacter(); // = "a"

// 这个技巧经常用在“代码填充”中，来为老版本的javascript子集增加新版本js的特性，
// 这样就可以在老的浏览器中使用新功能了。

// 比如，我们知道Object.create并没有在所有的版本中都实现，
// 但是我们仍然可以通过“代码填充”来实现兼容：
if (Object.create === undefined){ // 如果存在则不覆盖
    Object.create = function(proto){
        // 用正确的原型来创建一个临时构造函数
        var Constructor = function(){};
        Constructor.prototype = proto;
        // 之后用它来创建一个新的对象
        return new Constructor();
    }
}
```

## 更多阅读

[Mozilla 开发者
网络](https://developer.mozilla.org/en-US/docs/Web/JavaScript) 提供了优秀的介绍
Javascript如何在浏览器中使用的文档。而且它是wiki，所以你也可以自行编辑来分享你的知识。

MDN的 [A re-introduction to
JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)
覆盖了这里提到的绝大多数话题的细节。该导引的大多数内容被限定在只是Javascript这个语言本身；
如果你想了解Javascript是如何在网页中被应用的，那么可以查看
[Document Object
Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)

[Learn Javascript by Example and with Challenges](http://www.learneroo.com/modules/64/nodes/350) 是本参考的另一个版本，并包含了挑战习题。

[Javascript Garden](http://bonsaiden.github.io/JavaScript-Garden/) 是一个深入
讲解所有Javascript反直觉部分的导引。

[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) 是一个经典的指导参考书。

除了这篇文章的直接贡献者之外，这篇文章也参考了这个网站上
Louie Dinh 的 Python 教程，以及 Mozilla开发者网络上的[JS
Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)。
---
category: tool
tool: jquery
contributors:
    - ["Sawyer Charles", "https://github.com/xssc"]
translators:
    - ["zxyqwe", "https://github.com/zxyqwe"]
lang: zh-cn
filename: jquery-cn.js
---

jQuery是JavaScript的一个函数库，它可以帮你“写更少，做更多”。它集成了很多常见的JavaScript任务并且很容易调用。jQuery被世界各地的很多的大公司和开发者使用。它包括了AJAX，事件处理，文档操作以及很多其它功能，并且更加简单和快速。

正因为jQuery是JavaScript的一个函数库，所以你需要[首先学习JavaScript](https://learnxinyminutes.com/docs/javascript/)

```js


///////////////////////////////////
// 1. 选择器

// jQuery中的选择器被用来选择一个元素
var page = $(window); // 选择整个视窗

// 选择器可以作为CSS选择器使用
var paragraph = $('p'); // 选择所有段落元素
var table1 = $('#table1'); // 选择id为table1的元素
var squares = $('.square'); // 选择所有类是square的元素
var square_p = $('p.square') // 选择具有square类的所有段落


///////////////////////////////////
// 2. 事件和效果
// jQuery非常善于处理当事件触发的时候应该做什么
// 一个非常常见的事件就是文档的就绪事件
// 你可以用ready方法，在所有元素完成加载的时候执行
$(document).ready(function(){
  // 只有文档加载完成以后代码才会执行
});
// 你也可以用定义了的函数
function onAction() {
  // 本函数在事件触发的时候被执行
}
$('#btn').click(onAction); // 当点击的时候调用onAction函数

// 其它常见的事件：
$('#btn').dblclick(onAction); // 双击
$('#btn').hover(onAction); // 划过
$('#btn').focus(onAction); // 聚焦
$('#btn').blur(onAction); // 失焦
$('#btn').submit(onAction); // 提交
$('#btn').select(onAction); // 当元素被选中
$('#btn').keydown(onAction); // 当一个按键被按下
$('#btn').keyup(onAction); // 当一个按键被抬起
$('#btn').keypress(onAction); // 当一个按键被按住
$('#btn').mousemove(onAction); // 当鼠标在移动
$('#btn').mouseenter(onAction); // 鼠标移入元素
$('#btn').mouseleave(onAction); // 鼠标离开元素


// 如果不提供任何参数的话，那么这些方法可以触发事件
// 而不是定义处理事件的方法
$('#btn').dblclick(); // 触发元素上的双击

// 你可以只用选择器一次而处理多个事件
$('#btn').on(
  {dblclick: myFunction1} // 双击的时候触发
  {blur: myFunction1} // 失焦的时候触发
);

// 你可以用一些效果函数来移动或隐藏元素
$('.table').hide(); // 隐藏元素

// 注意：在这些方法中调用函数会仍然隐藏元素
$('.table').hide(function(){
    // 元素先隐藏然后函数被执行
});

// 你可以在变量中储存选择器
var tables = $('.table');

// 一些基本的文档操作方法有：
tables.hide(); // 隐藏元素
tables.show(); // 显示元素
tables.toggle(); // 对被选元素进行隐藏和显示的切换
tables.fadeOut(); // 淡出
tables.fadeIn(); // 淡入
tables.fadeToggle(); // 对被选元素进行淡入和淡出显示的切换
tables.fadeTo(0.5); // 把被选元素逐渐改变至给定的不透明度（0和1之间）
tables.slideUp(); // 通过调整高度来滑动隐藏被选元素
tables.slideDown(); // 对被选元素进行滑动隐藏和滑动显示的切换
tables.slideToggle(); // 对被选元素进行滑动隐藏和滑动显示的切换

// 上面所有的方法接受速度参数（毫秒）和一个回调函数
tables.hide(1000, myFunction); // 持续一秒的隐藏动画然后执行函数

// fadeTo要求提供透明度参数作为第二个参数
tables.fadeTo(2000, 0.1, myFunction); // 通过2秒钟将透明度变为0.1然后执行函数

// 你可以用animate方法实现一些略微高级的效果
tables.animate({margin-top:"+=50", height: "100px"}, 500, myFunction);
// animate方法接受一个包含CSS和值的对象作为目标，
// 其次是可选的速度参数，
// 以及最后的回调函数

///////////////////////////////////
// 3. 操作

// 这些类似效果函数但是可以做更多
$('div').addClass('taming-slim-20'); // 给所有div添加类taming-slim-20

// 常见操作方法
$('p').append('Hello world'); // 添加到元素末尾
$('p').attr('class'); // 获取属性
$('p').attr('class', 'content'); // 设置属性
$('p').hasClass('taming-slim-20'); // 如果有类则为真
$('p').height(); // 获取和设置元素的高度


// 对于很多的操作函数来说，获取元素的信息
// 仅仅是第一个符合元素的
$('p').height(); // 仅仅获取第一个p标签的高度

// 你可以用each来迭代所有元素
var heights = [];
$('p').each(function() {
  heights.push($(this).height()); // 把所有p标签的高度加入数组
});


```
---
language: json
contributors:
  - ["Anna Harren", "https://github.com/iirelu"]
translators:
  - ["Zach Zhang", "https://github.com/checkcheckzz"]
filename: learnjson-cn.json
lang: zh-cn
---

因为JSON是一个极其简单的数据交换格式，本教程最有可能成为有史以来最简单的
Learn X in Y Minutes。

纯正的JSON实际上没有注释，但是大多数解析器都
接受C-风格(//, /\* \*/)的注释。为了兼容性，最好不要在其中写这样形式的注释。

因此，本教程的一切都会是100%有效的JSON。幸亏，它的表达能力很丰富。

支持的数据类型：

- 字符串: "hello", "\"A quote.\"", "\u0abe", "Newline.\n"
- 数字: 23, 0.11, 12e10, 3.141e-10, 1.23e+4
- 对象: { "key": "value" }
- 数组: ["Values"]
- 其他: true, false, null

```json
{
  "numbers": 0,
  "strings": "Hellø, wørld. All unicode is allowed, along with \"escaping\".",
  "has bools?": true,
  "nothingness": null,

  "big number": 1.2e+100,

  "objects": {
    "comment": "Most of your structure will come from objects.",

    "array": [0, 1, 2, 3, "Arrays can have anything in them.", 5],

    "another object": {
      "comment": "These things can be nested, very useful."
    }
  },

  "silliness": [
    {
      "sources of potassium": ["bananas"]
    },
    [
      [1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, "neo"],
      [0, 0, 0, 1]
    ]
  ],

  "that was short": "And, you're done. You now know everything JSON has to offer."
}
```
---
language: Julia
filename: learn-julia-zh.jl
contributors:
    - ["Jichao Ouyang", "http://oyanglul.us"]
translators:
    - ["Jichao Ouyang", "http://oyanglul.us"]
lang: zh-cn
---

```ruby
# 单行注释只需要一个井号
#= 多行注释
   只需要以 '#=' 开始 '=#' 结束
   还可以嵌套.
=#

####################################################
## 1. 原始类型与操作符
####################################################

# Julia 中一切皆是表达式。

# 这是一些基本数字类型.
3 # => 3 (Int64)
3.2 # => 3.2 (Float64)
2 + 1im # => 2 + 1im (Complex{Int64})
2//3 # => 2//3 (Rational{Int64})

# 支持所有的普通中缀操作符。
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
5 / 2 # => 2.5 # 用 Int 除 Int 永远返回 Float
div(5, 2) # => 2 # 使用 div 截断小数点
5 \ 35 # => 7.0
2 ^ 2 # => 4 # 次方, 不是二进制 xor
12 % 10 # => 2

# 用括号提高优先级
(1 + 3) * 2 # => 8

# 二进制操作符
~2 # => -3   # 非
3 & 5 # => 1 # 与
2 | 4 # => 6 # 或
2 $ 4 # => 6 # 异或
2 >>> 1 # => 1 # 逻辑右移
2 >> 1  # => 1 # 算术右移
2 << 1  # => 4 # 逻辑/算术 右移

# 可以用函数 bits 查看二进制数。
bits(12345)
# => "0000000000000000000000000000000000000000000000000011000000111001"
bits(12345.0)
# => "0100000011001000000111001000000000000000000000000000000000000000"

# 布尔值是原始类型
true
false

# 布尔操作符
!true # => false
!false # => true
1 == 1 # => true
2 == 1 # => false
1 != 1 # => false
2 != 1 # => true
1 < 10 # => true
1 > 10 # => false
2 <= 2 # => true
2 >= 2 # => true
# 比较可以串联
1 < 2 < 3 # => true
2 < 3 < 2 # => false

# 字符串可以由 " 创建
"This is a string."

# 字符字面量可用 ' 创建
'a'

# 可以像取数组取值一样用 index 取出对应字符
"This is a string"[1] # => 'T' # Julia 的 index 从 1 开始 :(
# 但是对 UTF-8 无效,
# 因此建议使用遍历器 (map, for loops, 等).

# $ 可用于字符插值:
"2 + 2 = $(2 + 2)" # => "2 + 2 = 4"
# 可以将任何 Julia 表达式放入括号。

# 另一种格式化字符串的方式是 printf 宏.
@printf "%d is less than %f" 4.5 5.3 # 5 is less than 5.300000

# 打印字符串很容易
println("I'm Julia. Nice to meet you!")

####################################################
## 2. 变量与集合
####################################################

# 给变量赋值就是声明变量
some_var = 5 # => 5
some_var # => 5

# 访问未声明变量会抛出异常
try
    some_other_var # => ERROR: some_other_var not defined
catch e
    println(e)
end

# 变量名需要以字母开头.
# 之后任何字母，数字，下划线，叹号都是合法的。
SomeOtherVar123! = 6 # => 6

# 甚至可以用 unicode 字符
☃ = 8 # => 8
# 用数学符号非常方便
2 * π # => 6.283185307179586

# 注意 Julia 的命名规约:
#
# * 变量名为小写，单词之间以下划线连接('\_')。
#
# * 类型名以大写字母开头，单词以 CamelCase 方式连接。
#
# * 函数与宏的名字小写，无下划线。
#
# * 会改变输入的函数名末位为 !。
#   这类函数有时被称为 mutating functions 或 in-place functions.

# 数组存储一列值，index 从 1 开始。
a = Int64[] # => 0-element Int64 Array

# 一维数组可以以逗号分隔值的方式声明。
b = [4, 5, 6] # => 包含 3 个 Int64 类型元素的数组: [4, 5, 6]
b[1] # => 4
b[end] # => 6

# 二维数组以分号分隔维度。
matrix = [1 2; 3 4] # => 2x2 Int64 数组: [1 2; 3 4]

# 使用 push! 和 append! 往数组末尾添加元素
push!(a,1)     # => [1]
push!(a,2)     # => [1,2]
push!(a,4)     # => [1,2,4]
push!(a,3)     # => [1,2,4,3]
append!(a,b) # => [1,2,4,3,4,5,6]

# 用 pop 弹出末尾元素
pop!(b)        # => 6 and b is now [4,5]

# 可以再放回去
push!(b,6)   # b 又变成了 [4,5,6].

a[1] # => 1 #  永远记住 Julia 的 index 从 1 开始!

# 用 end 可以直接取到最后索引. 可用作任何索引表达式
a[end] # => 6

# 还支持 shift 和 unshift
shift!(a) # => 返回 1，而 a 现在时 [2,4,3,4,5,6]
unshift!(a,7) # => [7,2,4,3,4,5,6]

# 以叹号结尾的函数名表示它会改变参数的值
arr = [5,4,6] # => 包含三个 Int64 元素的数组: [5,4,6]
sort(arr) # => [4,5,6]; arr 还是 [5,4,6]
sort!(arr) # => [4,5,6]; arr 现在是 [4,5,6]

# 越界会抛出 BoundsError 异常
try
    a[0] # => ERROR: BoundsError() in getindex at array.jl:270
    a[end+1] # => ERROR: BoundsError() in getindex at array.jl:270
catch e
    println(e)
end

# 错误会指出发生的行号，包括标准库
# 如果你有 Julia 源代码，你可以找到这些地方

# 可以用 range 初始化数组
a = [1:5] # => 5-element Int64 Array: [1,2,3,4,5]

# 可以切割数组
a[1:3] # => [1, 2, 3]
a[2:end] # => [2, 3, 4, 5]

# 用 splice! 切割原数组
arr = [3,4,5]
splice!(arr,2) # => 4 ; arr 变成了 [3,5]

# 用 append! 连接数组
b = [1,2,3]
append!(a,b) # a 变成了 [1, 2, 3, 4, 5, 1, 2, 3]

# 检查元素是否在数组中
in(1, a) # => true

# 用 length 获得数组长度
length(a) # => 8

# Tuples 是 immutable 的
tup = (1, 2, 3) # => (1,2,3) # an (Int64,Int64,Int64) tuple.
tup[1] # => 1
try:
    tup[1] = 3 # => ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
catch e
    println(e)
end

# 大多数组的函数同样支持 tuples
length(tup) # => 3
tup[1:2] # => (1,2)
in(2, tup) # => true

# 可以将 tuples 元素分别赋给变量
a, b, c = (1, 2, 3) # => (1,2,3)  # a is now 1, b is now 2 and c is now 3

# 不用括号也可以
d, e, f = 4, 5, 6 # => (4,5,6)

# 单元素 tuple 不等于其元素值
(1,) == 1 # => false
(1) == 1 # => true

# 交换值
e, d = d, e  # => (5,4) # d is now 5 and e is now 4


# 字典Dictionaries store mappings
empty_dict = Dict() # => Dict{Any,Any}()

# 也可以用字面量创建字典
filled_dict = ["one"=> 1, "two"=> 2, "three"=> 3]
# => Dict{ASCIIString,Int64}

# 用 [] 获得键值
filled_dict["one"] # => 1

# 获得所有键
keys(filled_dict)
# => KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# 注意，键的顺序不是插入时的顺序

# 获得所有值
values(filled_dict)
# => ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
# 注意，值的顺序也一样

# 用 in 检查键值是否已存在，用 haskey 检查键是否存在
in(("one", 1), filled_dict) # => true
in(("two", 3), filled_dict) # => false
haskey(filled_dict, "one") # => true
haskey(filled_dict, 1) # => false

# 获取不存在的键的值会抛出异常
try
    filled_dict["four"] # => ERROR: key not found: four in getindex at dict.jl:489
catch e
    println(e)
end

# 使用 get 可以提供默认值来避免异常
# get(dictionary,key,default_value)
get(filled_dict,"one",4) # => 1
get(filled_dict,"four",4) # => 4

# 用 Sets 表示无序不可重复的值的集合
empty_set = Set() # => Set{Any}()
# 初始化一个 Set 并定义其值
filled_set = Set(1,2,2,3,4) # => Set{Int64}(1,2,3,4)

# 添加值
push!(filled_set,5) # => Set{Int64}(5,4,2,3,1)

# 检查是否存在某值
in(2, filled_set) # => true
in(10, filled_set) # => false

# 交集，并集，差集
other_set = Set(3, 4, 5, 6) # => Set{Int64}(6,4,5,3)
intersect(filled_set, other_set) # => Set{Int64}(3,4,5)
union(filled_set, other_set) # => Set{Int64}(1,2,3,4,5,6)
setdiff(Set(1,2,3,4),Set(2,3,5)) # => Set{Int64}(1,4)


####################################################
## 3. 控制流
####################################################

# 声明一个变量
some_var = 5

# 这是一个 if 语句，缩进不是必要的
if some_var > 10
    println("some_var is totally bigger than 10.")
elseif some_var < 10    # elseif 是可选的.
    println("some_var is smaller than 10.")
else                    # else 也是可选的.
    println("some_var is indeed 10.")
end
# => prints "some var is smaller than 10"


# For 循环遍历
# Iterable 类型包括 Range, Array, Set, Dict, 以及 String.
for animal=["dog", "cat", "mouse"]
    println("$animal is a mammal")
    # 可用 $ 将 variables 或 expression 转换为字符串into strings
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# You can use 'in' instead of '='.
for animal in ["dog", "cat", "mouse"]
    println("$animal is a mammal")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$(a[1]) is a $(a[2])")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

for (k,v) in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
    println("$k is a $v")
end
# prints:
#    dog is a mammal
#    cat is a mammal
#    mouse is a mammal

# While 循环
x = 0
while x < 4
    println(x)
    x += 1  # x = x + 1
end
# prints:
#   0
#   1
#   2
#   3

# 用 try/catch 处理异常
try
   error("help")
catch e
   println("caught it $e")
end
# => caught it ErrorException("help")


####################################################
## 4. 函数
####################################################

# 用关键字 'function' 可创建一个新函数
#function name(arglist)
#  body...
#end
function add(x, y)
    println("x is $x and y is $y")

    # 最后一行语句的值为返回
    x + y
end

add(5, 6) # => 在 "x is 5 and y is 6" 后会打印 11

# 还可以定义接收可变长参数的函数
function varargs(args...)
    return args
    # 关键字 return 可在函数内部任何地方返回
end
# => varargs (generic function with 1 method)

varargs(1,2,3) # => (1,2,3)

# 省略号 ... 被称为 splat.
# 刚刚用在了函数定义中
# 还可以用在函数的调用
# Array 或者 Tuple 的内容会变成参数列表
Set([1,2,3])    # => Set{Array{Int64,1}}([1,2,3]) # 获得一个 Array 的 Set
Set([1,2,3]...) # => Set{Int64}(1,2,3) # 相当于 Set(1,2,3)

x = (1,2,3)     # => (1,2,3)
Set(x)          # => Set{(Int64,Int64,Int64)}((1,2,3)) # 一个 Tuple 的 Set
Set(x...)       # => Set{Int64}(2,3,1)


# 可定义可选参数的函数
function defaults(a,b,x=5,y=6)
    return "$a $b and $x $y"
end

defaults('h','g') # => "h g and 5 6"
defaults('h','g','j') # => "h g and j 6"
defaults('h','g','j','k') # => "h g and j k"
try
    defaults('h') # => ERROR: no method defaults(Char,)
    defaults() # => ERROR: no methods defaults()
catch e
    println(e)
end

# 还可以定义键值对的参数
function keyword_args(;k1=4,name2="hello") # note the ;
    return ["k1"=>k1,"name2"=>name2]
end

keyword_args(name2="ness") # => ["name2"=>"ness","k1"=>4]
keyword_args(k1="mine") # => ["k1"=>"mine","name2"=>"hello"]
keyword_args() # => ["name2"=>"hello","k1"=>4]

# 可以组合各种类型的参数在同一个函数的参数列表中
function all_the_args(normal_arg, optional_positional_arg=2; keyword_arg="foo")
    println("normal arg: $normal_arg")
    println("optional arg: $optional_positional_arg")
    println("keyword arg: $keyword_arg")
end

all_the_args(1, 3, keyword_arg=4)
# prints:
#   normal arg: 1
#   optional arg: 3
#   keyword arg: 4

# Julia 有一等函数
function create_adder(x)
    adder = function (y)
        return x + y
    end
    return adder
end

# 这是用 "stabby lambda syntax" 创建的匿名函数
(x -> x > 2)(3) # => true

# 这个函数和上面的 create_adder 一模一样
function create_adder(x)
    y -> x + y
end

# 你也可以给内部函数起个名字
function create_adder(x)
    function adder(y)
        x + y
    end
    adder
end

add_10 = create_adder(10)
add_10(3) # => 13


# 内置的高阶函数有
map(add_10, [1,2,3]) # => [11, 12, 13]
filter(x -> x > 5, [3, 4, 5, 6, 7]) # => [6, 7]

# 还可以使用 list comprehensions 替代 map
[add_10(i) for i=[1, 2, 3]] # => [11, 12, 13]
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]

####################################################
## 5.  类型
####################################################

# Julia 有类型系统
# 所有的值都有类型；但变量本身没有类型
# 你可以用 `typeof` 函数获得值的类型
typeof(5) # => Int64

# 类型是一等值
typeof(Int64) # => DataType
typeof(DataType) # => DataType
# DataType 是代表类型的类型，也代表他自己的类型

# 类型可用作文档化，优化，以及调度
# 并不是静态检查类型

# 用户还可以自定义类型
# 跟其他语言的 records 或 structs 一样
# 用 `type` 关键字定义新的类型

# type Name
#   field::OptionalType
#   ...
# end
type Tiger
  taillength::Float64
  coatcolor # 不附带类型标注的相当于 `::Any`
end

# 构造函数参数是类型的属性
tigger = Tiger(3.5,"orange") # => Tiger(3.5,"orange")

# 用新类型作为构造函数还会创建一个类型
sherekhan = typeof(tigger)(5.6,"fire") # => Tiger(5.6,"fire")

# struct 类似的类型被称为具体类型
# 他们可被实例化但不能有子类型
# 另一种类型是抽象类型

# abstract Name
abstract Cat # just a name and point in the type hierarchy

# 抽象类型不能被实例化，但是可以有子类型
# 例如，Number 就是抽象类型
subtypes(Number) # => 6-element Array{Any,1}:
                 #     Complex{Float16}
                 #     Complex{Float32}
                 #     Complex{Float64}
                 #     Complex{T<:Real}
                 #     ImaginaryUnit
                 #     Real
subtypes(Cat) # => 0-element Array{Any,1}

# 所有的类型都有父类型; 可以用函数 `super` 得到父类型.
typeof(5) # => Int64
super(Int64) # => Signed
super(Signed) # => Real
super(Real) # => Number
super(Number) # => Any
super(super(Signed)) # => Number
super(Any) # => Any
# 所有这些类型，除了 Int64, 都是抽象类型.

# <: 是类型集成操作符
type Lion <: Cat # Lion 是 Cat 的子类型
  mane_color
  roar::String
end

# 可以继续为你的类型定义构造函数
# 只需要定义一个同名的函数
# 并调用已有的构造函数设置一个固定参数
Lion(roar::String) = Lion("green",roar)
# 这是一个外部构造函数，因为他再类型定义之外

type Panther <: Cat # Panther 也是 Cat 的子类型
  eye_color
  Panther() = new("green")
  # Panthers 只有这个构造函数，没有默认构造函数
end
# 使用内置构造函数，如 Panther，可以让你控制
# 如何构造类型的值
# 应该尽可能使用外部构造函数而不是内部构造函数

####################################################
## 6. 多分派
####################################################

# 在Julia中, 所有的具名函数都是类属函数
# 这意味着他们都是有很大小方法组成的
# 每个 Lion 的构造函数都是类属函数 Lion 的方法

# 我们来看一个非构造函数的例子

# Lion, Panther, Tiger 的 meow 定义为
function meow(animal::Lion)
  animal.roar # 使用点符号访问属性
end

function meow(animal::Panther)
  "grrr"
end

function meow(animal::Tiger)
  "rawwwr"
end

# 试试 meow 函数
meow(tigger) # => "rawwr"
meow(Lion("brown","ROAAR")) # => "ROAAR"
meow(Panther()) # => "grrr"

# 再看看层次结构
issubtype(Tiger,Cat) # => false
issubtype(Lion,Cat) # => true
issubtype(Panther,Cat) # => true

# 定义一个接收 Cats 的函数
function pet_cat(cat::Cat)
  println("The cat says $(meow(cat))")
end

pet_cat(Lion("42")) # => prints "The cat says 42"
try
    pet_cat(tigger) # => ERROR: no method pet_cat(Tiger,)
catch e
    println(e)
end

# 在面向对象语言中，通常都是单分派
# 这意味着分派方法是通过第一个参数的类型决定的
# 在Julia中, 所有参数类型都会被考虑到

# 让我们定义有多个参数的函数，好看看区别
function fight(t::Tiger,c::Cat)
  println("The $(t.coatcolor) tiger wins!")
end
# => fight (generic function with 1 method)

fight(tigger,Panther()) # => prints The orange tiger wins!
fight(tigger,Lion("ROAR")) # => prints The orange tiger wins!

# 让我们修改一下传入具体为 Lion 类型时的行为
fight(t::Tiger,l::Lion) = println("The $(l.mane_color)-maned lion wins!")
# => fight (generic function with 2 methods)

fight(tigger,Panther()) # => prints The orange tiger wins!
fight(tigger,Lion("ROAR")) # => prints The green-maned lion wins!

# 把 Tiger 去掉
fight(l::Lion,c::Cat) = println("The victorious cat says $(meow(c))")
# => fight (generic function with 3 methods)

fight(Lion("balooga!"),Panther()) # => prints The victorious cat says grrr
try
  fight(Panther(),Lion("RAWR")) # => ERROR: no method fight(Panther,Lion)
catch
end

# 在试试让 Cat 在前面
fight(c::Cat,l::Lion) = println("The cat beats the Lion")
# => Warning: New definition
#    fight(Cat,Lion) at none:1
# is ambiguous with
#    fight(Lion,Cat) at none:2.
# Make sure
#    fight(Lion,Lion)
# is defined first.
#fight (generic function with 4 methods)

# 警告说明了无法判断使用哪个 fight 方法
fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The victorious cat says rarrr
# 结果在老版本 Julia 中可能会不一样

fight(l::Lion,l2::Lion) = println("The lions come to a tie")
fight(Lion("RAR"),Lion("brown","rarrr")) # => prints The lions come to a tie


# Under the hood
# 你还可以看看 llvm 以及生成的汇编代码

square_area(l) = l * l      # square_area (generic function with 1 method)

square_area(5) #25

# 给 square_area 一个整形时发生什么
code_native(square_area, (Int32,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1              # Prologue
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       movsxd  RAX, EDI        # Fetch l from memory?
    #       imul    RAX, RAX        # Square l and store the result in RAX
    #       pop RBP                 # Restore old base pointer
    #       ret                     # Result will still be in RAX

code_native(square_area, (Float32,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       vmulss  XMM0, XMM0, XMM0  # Scalar single precision multiply (AVX)
    #       pop RBP
    #       ret

code_native(square_area, (Float64,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       vmulsd  XMM0, XMM0, XMM0 # Scalar double precision multiply (AVX)
    #       pop RBP
    #       ret
    #
# 注意 只要参数中又浮点类型，Julia 就使用浮点指令
# 让我们计算一下圆的面积
circle_area(r) = pi * r * r     # circle_area (generic function with 1 method)
circle_area(5)                  # 78.53981633974483

code_native(circle_area, (Int32,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #   Source line: 1
    #       vcvtsi2sd   XMM0, XMM0, EDI          # Load integer (r) from memory
    #       movabs  RAX, 4593140240              # Load pi
    #       vmulsd  XMM1, XMM0, QWORD PTR [RAX]  # pi * r
    #       vmulsd  XMM0, XMM0, XMM1             # (pi * r) * r
    #       pop RBP
    #       ret
    #

code_native(circle_area, (Float64,))
    #       .section    __TEXT,__text,regular,pure_instructions
    #   Filename: none
    #   Source line: 1
    #       push    RBP
    #       mov RBP, RSP
    #       movabs  RAX, 4593140496
    #   Source line: 1
    #       vmulsd  XMM1, XMM0, QWORD PTR [RAX]
    #       vmulsd  XMM0, XMM1, XMM0
    #       pop RBP
    #       ret
    #
```
---
language: kotlin
contributors:
    - ["S Webber", "https://github.com/s-webber"]
translators:
    - ["Jimin Lu", "https://github.com/lujimin"]
filename: LearnKotlin-cn.kt
lang: zh-cn
---

Kotlin是一门适用于JVM、Android和浏览器的静态类型编程语言。它100%兼容Java。
[了解更多。](https://kotlinlang.org/)

```java
// 单行注释从 // 开始
/*
多行注释看起来像这样。
*/

// "package" 关键字的工作方式与Java相同。
package com.learnxinyminutes.kotlin

/*
Kotlin程序的入口点是一个"main"函数
该函数传递一个包含任何命令行参数的数组。
*/
fun main(args: Array<String>) {
    /*
    使用"var"或"val"来声明一个值。
    "val"声明的值不能被重新赋值，而"var"声明的值可以。
    */
    val fooVal = 10 // 以后我们不能再次给fooVal赋值
    var fooVar = 10
    fooVar = 20 // fooVar可以被再次赋值

    /*
    在大多数情况下，Kotlin可以确定变量的类型是什么，
    所以我们不必要每次都去明确指定它。
    我们可以像这样明确地声明一个变量的类型：
    */
    val foo: Int = 7

    /*
    可以采取和Java类似的方法来表示一个字符串。
    用反斜杠来转义字符。
    */
    val fooString = "My String Is Here!";
    val barString = "Printing on a new line?\nNo Problem!";
    val bazString = "Do you want to add a tab?\tNo Problem!";
    println(fooString);
    println(barString);
    println(bazString);

    /*
    原始字符串用三重引号(""")来定义。
    原始字符串可以包含换行符以及其他任何字符。
    */
    val fooRawString = """
fun helloWorld(val name : String) {
   println("Hello, world!")
}
"""
    println(fooRawString)

    /*
    字符串可以包含模板表达式。
    模板表达式从一个美元符号($)开始。
    */
    val fooTemplateString = "$fooString has ${fooString.length} characters"
    println(fooTemplateString)

    /*
    当某个变量的值可以为 null 的时候，我们必须被明确指定它是可为空的。
    在变量声明处的类型后面加上?来标识它是可为空的。
    我们可以用?.操作符来访问可为空的变量。
    我们可以用?:操作符来指定一个在变量为空时使用的替代值。
    */
    var fooNullable: String? = "abc"
    println(fooNullable?.length) // => 3
    println(fooNullable?.length ?: -1) // => 3
    fooNullable = null
    println(fooNullable?.length) // => null
    println(fooNullable?.length ?: -1) // => -1

    /*
    使用"fun"关键字来声明一个函数。
    函数的参数在函数名后面的括号内指定。
    函数的参数可以设定一个默认值。
    如果需要的话，函数的返回值类型可以在参数后面指定。
    */
    fun hello(name: String = "world") : String {
        return "Hello, $name!"
    }
    println(hello("foo")) // => Hello, foo!
    println(hello(name = "bar")) // => Hello, bar!
    println(hello()) // => Hello, world!

    /*
    用"vararg"关键字来修饰一个函数的参数来允许可变参数传递给该函数
    */
    fun varargExample(vararg names: Int) {
        println("Argument has ${names.size} elements")
    }
    varargExample() // => Argument has 0 elements
    varargExample(1) // => Argument has 1 elements
    varargExample(1, 2, 3) // => Argument has 3 elements

    /*
    当函数只包含一个单独的表达式时，大括号可以被省略。
    函数体可以被指定在一个=符号后面。
    */
    fun odd(x: Int): Boolean = x % 2 == 1
    println(odd(6)) // => false
    println(odd(7)) // => true

    // 如果返回值类型可以被推断，那么我们不需要指定它。
    fun even(x: Int) = x % 2 == 0
    println(even(6)) // => true
    println(even(7)) // => false

    // 函数可以用函数作为参数并且可以返回函数。
    fun not(f: (Int) -> Boolean) : (Int) -> Boolean {
        return {n -> !f.invoke(n)}
    }
    // 命名函数可以用::运算符被指定为参数。
    val notOdd = not(::odd)
    val notEven = not(::even)
    // 匿名函数可以被指定为参数。
    val notZero = not {n -> n == 0}
    /*
    如果一个匿名函数只有一个参数
    那么它的声明可以被省略（连同->）。
    这个参数的名字是"it"。
    */
    val notPositive = not {it > 0}
    for (i in 0..4) {
        println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}")
    }

    // "class"关键字用来声明类。
    class ExampleClass(val x: Int) {
        fun memberFunction(y: Int) : Int {
            return x + y
        }

        infix fun infixMemberFunction(y: Int) : Int {
            return x * y
        }
    }
    /*
    我们调用构造方法来创建一个新的实例。
    注意，Kotlin没有"new"关键字。
    */
    val fooExampleClass = ExampleClass(7)
    // 可以使用一个点号来调用成员函数。
    println(fooExampleClass.memberFunction(4)) // => 11
    /*
    如果使用"infix"关键字来标记一个函数
    那么可以使用中缀表示法来调用该函数。
    */
    println(fooExampleClass infixMemberFunction 4) // => 28

    /*
    数据类是创建只包含数据的类的一个简洁的方法。
    "hashCode"、"equals"和"toString"方法将被自动生成。
    */
    data class DataClassExample (val x: Int, val y: Int, val z: Int)
    val fooData = DataClassExample(1, 2, 4)
    println(fooData) // => DataClassExample(x=1, y=2, z=4)

    // 数据类有一个"copy"函数
    val fooCopy = fooData.copy(y = 100)
    println(fooCopy) // => DataClassExample(x=1, y=100, z=4)

    // 对象可以被解构成为多个变量
    val (a, b, c) = fooCopy
    println("$a $b $c") // => 1 100 4

    // "with"函数类似于JavaScript中的"with"用法。
    data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
    val fooMutableData = MutableDataClassExample(7, 4, 9)
    with (fooMutableData) {
        x -= 2
        y += 2
        z--
    }
    println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8)

    /*
    我们可以使用"listOf"函数来创建一个list。
    这个list是不可变的 - 元素不可以被添加或删除。
    */
    val fooList = listOf("a", "b", "c")
    println(fooList.size) // => 3
    println(fooList.first()) // => a
    println(fooList.last()) // => c
    // 可以通过索引来访问list中的元素。
    println(fooList[1]) // => b

    // 可以使用"mutableListOf"函数来创建一个可变的list。
    val fooMutableList = mutableListOf("a", "b", "c")
    fooMutableList.add("d")
    println(fooMutableList.last()) // => d
    println(fooMutableList.size) // => 4

    // 我们可以使用"setOf"函数来创建一个set。
    val fooSet = setOf("a", "b", "c")
    println(fooSet.contains("a")) // => true
    println(fooSet.contains("z")) // => false

    // 我们可以使用"mapOf"函数来创建一个map。
    val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9)
    // 可以通过键来访问map中的值。
    println(fooMap["a"]) // => 8

    /*
    序列表示惰性求值集合。
    我们可以使用"generateSequence"函数来创建一个序列。
    */
    val fooSequence = generateSequence(1, {it + 1})
    val x = fooSequence.take(10).toList()
    println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    // 一个用序列来生成斐波那契数列的例子。
    fun fibonacciSequence() : Sequence<Long> {
        var a = 0L
        var b = 1L

        fun next() : Long {
            val result = a + b
            a = b
            b = result
            return a
        }

        return generateSequence(::next)
    }
    val y = fibonacciSequence().take(10).toList()
    println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

    // Kotlin为集合提供高阶函数。
    val z = (1..9).map {it * 3}
                  .filter {it < 20}
                  .groupBy {it % 2 == 0}
                  .mapKeys {if (it.key) "even" else "odd"}
    println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]}

    // 任何提供迭代器的都可以使用"for"循环。
    for (c in "hello") {
        println(c)
    }

    // "while"循环的用法和其他语言一样。
    var ctr = 0
    while (ctr < 5) {
        println(ctr)
        ctr++
    }
    do {
        println(ctr)
        ctr++
    } while (ctr < 10)

    // "when"可以用来替代"if-else if"链。
    val i = 10
    when {
        i < 7 -> println("first block")
        fooString.startsWith("hello") -> println("second block")
        else -> println("else block")
    }

    // "when"可以带参数。
    when (i) {
        0, 21 -> println("0 or 21")
        in 1..20 -> println("in the range 1 to 20")
        else -> println("none of the above")
    }

    // "when"可以作为一个函数，提供返回值。
    var result = when (i) {
        0, 21 -> "0 or 21"
        in 1..20 -> "in the range 1 to 20"
        else -> "none of the above"
    }
    println(result)

    /*
    我们可以通过使用"is"操作符来检查一个对象是否是某个类型的。
    如果对象通过了类型检查那么它可以作为该类型使用而不需要强制转换它。
    */
    fun smartCastExample(x: Any) : Boolean {
        if (x is Boolean) {
            // x自动转换为Boolean
            return x
        } else if (x is Int) {
            // x自动转换为Int
            return x > 0
        } else if (x is String) {
            // x自动转换为String
            return x.isNotEmpty()
        } else {
            return false
        }
    }
    println(smartCastExample("Hello, world!")) // => true
    println(smartCastExample("")) // => false
    println(smartCastExample(5)) // => true
    println(smartCastExample(0)) // => false
    println(smartCastExample(true)) // => true

    /*
    扩展是用来给一个类添加新的功能的。
    它类似于C#的扩展方法。
    */
    fun String.remove(c: Char): String {
        return this.filter {it != c}
    }
    println("Hello, world!".remove('l')) // => Heo, word!

    println(EnumExample.A) // => A
    println(ObjectExample.hello()) // => hello
}

// 枚举类和Java的枚举类型类似。
enum class EnumExample {
    A, B, C
}

/*
"object"关键字用来创建单例对象。
我们不能把它赋给一个变量，但我们可以通过它的名字引用它。
这类似于Scala的单例对象。
*/
object ObjectExample {
    fun hello() : String {
        return "hello"
    }
}

```

### 进一步阅读

* [Kotlin教程](https://kotlinlang.org/docs/tutorials/)
* [在您的浏览器中使用Kotlin](http://try.kotlinlang.org/)
* [Kotlin资源列表](http://kotlin.link/)
---
language: latex
contributors:
    - ["Chaitanya Krishna Ande", "http://icymist.github.io"]
    - ["Colton Kohnke", "http://github.com/voltnor"]
    - ["Sricharan Chiruvolu", "http://sricharan.xyz"]
    - ["Ramanan Balakrishnan", "https://github.com/ramananbalakrishnan"]
    - ["Svetlana Golubeva", "https://attillax.github.io/"]
translators:
    - ["Dp Leo", "https://github.com/minoriwww"]
filename: learn-latex-cn.tex
lang: zh-cn
---

```tex
% 所有的注释行以 % 开头
% 没有多行注释语法

% LaTeX 不是一个“所见即所得” 的文字处理软件
% 这与 MS Word，和 OpenOffice Writer 不同

% 每一个LaTeX命令由反斜线 (\) 开始

% LaTeX 文档以对编译对象文档的定义开始
% 这些文档包括书籍，报告，演示等
% 文档的选项出现在中括号里
% 下例中，我们设定文章字体为12pt
\documentclass[12pt]{article}

% 之后我们定义该文档所用的库
% 如果想要引入图片，彩色字，或是其他语言的源码在您的文档中
% 您需要增强 LaTeX 的功能。这将通过添加库来实现
% 下例中将要为展示数据引入 float 和 caption 库
% 为超链接引入 hyperref 库
\usepackage{caption}
\usepackage{float}
\usepackage{hyperref}

% 我们还可以定义其他文档属性！
\author{Chaitanya Krishna Ande, Colton Kohnke, Sricharan Chiruvolu \& \\
Svetlana Golubeva}
\date{\today}
\title{Learn \LaTeX \hspace{1pt} in Y Minutes!}

% 现在我们开始正文
% 这一行之前都是“序章”
\begin{document} 
% 如果想设定作者，时间，标题字段我们可使用 LaTeX 来建立标题页
\maketitle

% 分章节时，可以建立目录
% 我们需要编译文档两次来保证他们顺序正确
% 使用目录来分开文档是很好的做法
% 这里我们使用 \newpage 操作符
\newpage
\tableofcontents

\newpage

% 许多研究论文有摘要部分。这可以使用预定义的指令来实现
% 它应被放在逻辑上正确的位置，即顶部标题等的下面和文章主体的上面
% 该指令可以再报告和文章中使用
\begin{abstract}
 \LaTeX \hspace{1pt} documentation written as \LaTeX! How novel and totally not
 my idea!
\end{abstract}

% 章节指令非常直观
% 所有章节标题会自动地添加到目录中
\section{Introduction}
Hello, my name is Colton and together we're going to explore \LaTeX!

\section{Another section}
This is the text for another section. I think it needs a subsection.

\subsection{This is a subsection} % 子章节同样非常直观
I think we need another one

\subsubsection{Pythagoras}
Much better now.
\label{subsec:pythagoras}

% 使用型号我们可以借助 LaTeX 内置的编号功能
% 这一技巧也在其他指令中有效
\section*{This is an unnumbered section} 
然而并不是所有章节都要被标序号

\section{Some Text notes}
%\section{Spacing} % 需要增加有关空白间隔的信息
\LaTeX \hspace{1pt} is generally pretty good about placing text where it should
go. If 
a line \\ needs \\ to \\ break \\ you add \textbackslash\textbackslash 
\hspace{1pt} to the source code. \\ 

\section{Lists}
Lists are one of the easiest things to create in \LaTeX! I need to go shopping
tomorrow, so let's make a grocery list.
\begin{enumerate} % 此处创建了一个“枚举”环境
  % \item 使枚举增加一个单位
  \item Salad.
  \item 27 watermelon.
  \item A single jackrabbit.
  % 我们甚至可以通过使用 [] 覆盖美剧的数量
  \item[how many?] Medium sized squirt guns.

  Not a list item, but still part of the enumerate.

\end{enumerate} % 所有环境都有终止符

\section{Math}

使用 \LaTeX \hspace{1pt} 的一个最主要的方面是学术论文和技术文章
通常在数学和科学的领域 
因此我们需要在文章中插入特殊符号！ \\

数学符号极多，远超出你能在键盘上找到的那些；
集合关系符，箭头，操作符，希腊字符等等 \\

集合与关系在数学文章中很重要
如声明所有 x 属于 X $\forall$ x $\in$ X. \\
% 注意我们需要在这些符号之前和之后增加 $ 符号
% 因为在编写时我们处于 text-mode，然而数学符号只在 math-mode 中存在
% text mode 进入 math-mode 使用 $ 操作符
% 反之亦然，变量同时会在 math-mode 中被渲染。
% 我们也可以使用 \[\] 来进入 math mode

\[a^2 + b^2 = c^2 \]

My favorite Greek letter is $\xi$. I also like $\beta$, $\gamma$ and $\sigma$.
I haven't found a Greek letter yet that \LaTeX \hspace{1pt} doesn't know
about! \\

常用函数操作符同样很重要： 
trigonometric functions ($\sin$, $\cos$, $\tan$), 
logarithms 和 exponentials ($\log$, $\exp$), 
limits ($\lim$), etc. 
在 LaTeX 指令中预定义 
让我们写一个等式看看发生了什么：
$\cos(2\theta) = \cos^{2}(\theta) - \sin^{2}(\theta)$ \\

分数可以写成以下形式：

% 10 / 7
$$ ^{10}/_{7} $$

% 相对比较复杂的分数可以写成
% \frac{numerator}{denominator}
$$ \frac{n!}{k!(n - k)!} $$ \\

我们同样可以插入公式（equations）在环境 ``equation environment'' 下。

% 展示数学相关时，使用方程式环境
\begin{equation} % 进入 math-mode
    c^2 = a^2 + b^2.
    \label{eq:pythagoras} % 为了下一步引用
\end{equation} % 所有 \begin 语句必须有end语句对应

引用我们的新等式！
Eqn.~\ref{eq:pythagoras} is also known as the Pythagoras Theorem which is also
the subject of Sec.~\ref{subsec:pythagoras}. A lot of things can be labeled: 
figures, equations, sections, etc.

求和（Summations）与整合（Integrals）写作 sum 和 int ：

% 一些编译器会提醒在等式环境中的空行

\begin{equation} 
  \sum_{i=0}^{5} f_{i}
\end{equation} 
\begin{equation} 
  \int_{0}^{\infty} \mathrm{e}^{-x} \mathrm{d}x
\end{equation} 

\section{Figures}

让我们插入图片。图片的放置非常微妙。
我在每次使用时都会查找可用选项。

\begin{figure}[H] % H 是放置选项的符号
    \centering % 图片在本页居中
    % 宽度放缩为页面的0.8倍
    %\includegraphics[width=0.8\linewidth]{right-triangle.png} 
    % 需要使用想象力决定是否语句超出编译预期
    \caption{Right triangle with sides $a$, $b$, $c$}
    \label{fig:right-triangle}
\end{figure}

\subsection{Table}
插入表格与插入图片方式相同

\begin{table}[H]
  \caption{Caption for the Table.}
  % 下方的 {} 描述了表格中每一行的绘制方式
  % 同样，我在每次使用时都会查找可用选项。
  \begin{tabular}{c|cc} 
    Number &  Last Name & First Name \\ % 每一列被 & 分开
    \hline % 水平线
    1 & Biggus & Dickus \\
    2 & Monty & Python
  \end{tabular}
\end{table}

\section{Getting \LaTeX \hspace{1pt} to not compile something (i.e. Source Code)}
现在增加一些源代码在 \LaTeX \hspace{1pt} 文档中，
我们之后需要 \LaTeX \hspace{1pt} 不翻译这些内容而仅仅是把他们打印出来
这里使用 verbatim environment。 

% 也有其他库存在 (如. minty, lstlisting, 等)
% 但是 verbatim 是最基础和简单的一个
\begin{verbatim} 
  print("Hello World!")
  a%b; % 在这一环境下我们可以使用 %
  random = 4; #decided by fair random dice roll
\end{verbatim}

\section{Compiling} 

现在你大概想了解如何编译这个美妙的文档
然后得到饱受称赞的 \LaTeX \hspace{1pt} pdf文档
(这个文档确实被编译了)。 \\
得到最终文档，使用 \LaTeX \hspace{1pt} 组合步骤：
  \begin{enumerate}
    \item Write the document in plain text (the ``source code'').
    \item Compile source code to produce a pdf. 
     The compilation step looks like this (in Linux): \\
     \begin{verbatim} 
        > pdflatex learn-latex.tex
     \end{verbatim}
  \end{enumerate}

许多 \LaTeX \hspace{1pt}编译器把步骤1和2在同一个软件中进行了整合
所以你可以只看步骤1完全不看步骤2
步骤2同样在以下情境中使用情景 \footnote{以防万一，当你使用引用时
 (如 Eqn.~\ref{eq:pythagoras})，你将需要多次运行步骤2
来生成一个媒介文件 *.aux 。}.
% 同时这也是在文档中增加脚标的方式

在步骤1中，用普通文本写入格式化信息
步骤2的编译阶段则注意在步骤1 中定义的格式信息。

\section{Hyperlinks}
同样可以在文档中加入超链接
使用如下命令在序言中引入库：
\begin{verbatim} 
    \usepackage{hyperref}
\end{verbatim}

有两种主要的超链接方式 \\
\url{https://learnxinyminutes.com/docs/latex/}， 或  
\href{https://learnxinyminutes.com/docs/latex/}{shadowed by text}
% 你不可以增加特殊空格和符号，因为这将会造成编译错误

这个库同样在输出PDF文档时制造略缩的列表，或在目录中激活链接


\section{End}

这就是全部内容了！

% 通常，你会希望文章中有个引用部分
% 最简单的建立方式是使用书目提要章节
\begin{thebibliography}{1}
  % 与其他列表相同， \bibitem 命令被用来列出条目
  % 每个记录可以直接被文章主体引用
  \bibitem{latexwiki} The amazing \LaTeX \hspace{1pt} wikibook: {\em 
https://en.wikibooks.org/wiki/LaTeX}
  \bibitem{latextutorial} An actual tutorial: {\em http://www.latex-tutorial.com}
\end{thebibliography}

% 结束文档
\end{document}
```

## LaTeX 进阶

* The amazing LaTeX wikibook: [https://en.wikibooks.org/wiki/LaTeX](https://en.wikibooks.org/wiki/LaTeX)
* An actual tutorial: [http://www.latex-tutorial.com/](http://www.latex-tutorial.com/)
---
language: less
filename: learnless-cn.less
contributors:
  - ["Saravanan Ganesh", "http://srrvnn.me"]
translators:
   - ["Jiang Haiyun", "http://www.atjiang.com"]
lang: zh-cn
---


Less是一种CSS预处理器，它增加了诸如变量、嵌套、mixin等功能。
Less(以及其它预处理器，如[Sass](http://sass-lang.com/))能帮助开发人员编写易维护，DRY (Don't Repeat Yourself) 的代码。

```css


//单行注释在编译成CSS后会被删除。

/* 多行注释将保留. */



/* 变量
==============================*/


/* 你可以将一个CSS值（如一个颜色值）保存到变量中。
   使用'@'符号来创建一个变量。*/

@primary-color: #a3a4ff;
@secondary-color: #51527f;
@body-font: 'Roboto', sans-serif;

/* 你可以在你的样式文件中使用这些变量。
   现在假如你想修改颜色，你只需修改一次即可。*/

body {
	background-color: @primary-color;
	color: @secondary-color;
	font-family: @body-font;
}

/* 以上将编译成： */

body {
	background-color: #a3a4ff;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}


/* 相比于在你的样式文件中逐个修改，这种方式维护性更好。 */



/* Mixins
==============================*/


/* 如果你要为多个元素编写同样的代码，
   你可能想实现轻松地重用。*/

.center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* 你只需简单地将选择子作为样式添加进来就能使用mixin了 */

div {
	.center;
	background-color: @primary-color;
}

/* 它将编译成: */

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
}
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #a3a4ff;
}

/* 通过在选择子后添加括号，可以使这些mixin代码不被编译 */

.center() {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
}

div {
  .center;
  background-color: @primary-color;
}

/* 将编译成: */
div {
  display: block;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
  background-color: #a3a4ff;
}



/* 嵌套
==============================*/


/* Less允许你在选择子中嵌套选择子 */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #f00;
	}
}

/* '&'将被替换成父选择子。*/
/* 你也可以嵌套伪类。 */
/* 注意过度嵌套将会导致代码难以维护。
   最佳实践推荐在嵌套时不超过3层。
   例如： */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* 编译成： */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/* 函数
==============================*/


/* Less提供的函数可以用来完成多种任务。
   考虑以下情况： */

/* 函数可以通过其名称及传入其所需的参数来调用。 */

body {
  width: round(10.25px);
}

.header {
	background-color: lighten(#000, 0.5);
}

.footer {
  background-color: fadeout(#000, 0.25)
}

/* 编译成: */

body {
  width: 10px;
}

.header {
  background-color: #010101;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* 你也可以定义自己的函数。函数非常类似于mixin。
   当你在函数和mixin之间抉择时，
   记住mixin最适合用来创建CSS而函数更适合于
   处理那些可能在你的Less代码中使用的逻辑。
   '数学运算符'部分的例子是转成可重用函数的最佳选择。*/

/* 该函数计算两数的平均值： */

.average(@x, @y) {
  @average-result: ((@x + @y) / 2);
}

div {
  .average(16px, 50px); // "调用"mixin
  padding: @average-result;    // 使用它的"返回"值
}

/* 编译成: */

div {
  padding: 33px;
}



/* 扩展 (继承)
==============================*/


/* 扩展是在选择子间共享属性的一种方法。 */

.display {
  height: 50px;
}

.display-success {
  &:extend(.display);
	border-color: #22df56;
}

/* 编译成: */
.display,
.display-success {
  height: 50px;
}
.display-success {
  border-color: #22df56;
}

/* 扩展一条CSS语句优于创建一个mixin，
   这是由其组合所有共享相同基样式的类的方式决定的。
   如果使用mixin完成，其属性将会在调用了该mixin的每条语句中重复。
   虽然它不至会影响你的工作流，但它会在由Less编译器
   生成的的文件中添加不必要的代码。*/



/* 片段与导入
==============================*/


/* Less允许你创建片段文件。它有助于你的Less代码保持模块化。
   片段文件习惯上以'_'开头，例如 _reset.css，并被导入到
   一个将会被编译成CSS的主less文件中。*/

/* 考虑以下的CSS，我们将把它们放入一个叫_reset.css的文件中 */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Less提供的@import能用来将片段导入到文件中。
   它与传统的CSS @import语句不同，无需通过
   HTTP请求获取导入文件。Less提取导入文件
   并将它们与编译后的代码结合起来。 */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* 编译成: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/* 数学运算符
==============================*/


/* Less提供以下的运算符: +, -, *, /, 和 %。
   相比于使用你事先手工计算好了的数值，它们
   对于直接在你的Less文件中计算数值很有用。
   以下是设置一个两列设计的例子。*/

@content-area: 960px;
@main-content: 600px;
@sidebar-content: 300px;

@main-size: @main-content / @content-area * 100%;
@sidebar-size: @sidebar-content / @content-area * 100%;
@gutter: 100% - (@main-size + @sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: @main-size;
}

.sidebar {
  width: @sidebar-size;
}

.gutter {
  width: @gutter;
}

/* 编译成： */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}


```

## 实践Less

如果你想在你的浏览器中尝试LESS，参阅：
* [Codepen](http://codepen.io/)
* [LESS2CSS](http://lesscss.org/less-preview/)

## 兼容性

Less可以用于任何项目中，只要你有程序能将它编译成CSS即可。你还需要验证你所使用的CSS是否与你的目标浏览器兼容。

[QuirksMode CSS](http://www.quirksmode.org/css/)和[CanIUse](http://caniuse.com) 对于检查兼容性来说都是不错的资源。

## 延伸阅读资料
* [Official Documentation](http://lesscss.org/features/)
* [Less CSS - Beginner's Guide](http://www.hongkiat.com/blog/less-basic/)
---
language: LiveScript
filename: learnLivescript-cn.ls
contributors:
    - ["Christina Whyte", "http://github.com/kurisuwhyte/"]
translators:
    - ["ShengDa Lyu", "http://github.com/SDLyu/"]
lang: zh-cn    
---

LiveScript 是一种具有函数式特性且编译成 JavaScript 的语言，能对应 JavaScript 的基本语法。
还有些额外的特性如：柯里化，组合函数，模式匹配，还有借镜于 Haskell，F# 和 Scala 的许多特点。 

LiveScript 诞生于 [Coco][]，而 Coco 诞生于 [CoffeeScript][]。
LiveScript 目前已释出稳定版本，开发中的新版本将会加入更多特性。

[Coco]: http://satyr.github.io/coco/
[CoffeeScript]: http://coffeescript.org/

非常期待您的反馈，你可以通过
[@kurisuwhyte](https://twitter.com/kurisuwhyte) 与我连系 :)


```coffeescript
# 与 CoffeeScript 一样，LiveScript 使用 # 单行注解。

/*
 多行注解与 C 相同。使用注解可以避免被当成 JavaScript 输出。
*/
```
```coffeescript
# 语法的部份，LiveScript 使用缩进取代 {} 来定义区块，
# 使用空白取代 () 来执行函数。


########################################################################
## 1. 值类型
########################################################################

# `void` 取代 `undefined` 表示未定义的值
void            # 与 `undefined` 等价但更安全（不会被覆写）

# 空值则表示成 Null。
null


# 最基本的值类型数据是罗辑类型：
true
false

# 罗辑类型的一些别名，等价于前者：
on; off
yes; no


# 数字与 JS 一样，使用倍精度浮点数表示。
10
0.4     # 开头的 0 是必要的


# 可以使用底线及单位后缀提高可读性，编译器会自动略过底线及单位后缀。
12_344km


# 字串与 JS 一样，是一种不可变的字元序列：
"Christina"             # 单引号也可以！
"""Multi-line
   strings
   are
   okay
   too."""

# 在前面加上 \ 符号也可以表示字串：
\keyword                # => 'keyword'


# 数组是值的有序集合。
fruits =
  * \apple
  * \orange
  * \pear

# 可以用 [] 简洁地表示数组：
fruits = [ \apple, \orange, \pear ]


# 你可以更方便地建立字串数组，并使用空白区隔元素。
fruits = <[ apple orange pear ]>

# 以 0 为起始值的数组下标获取元素：
fruits[0]       # => "apple"


# 对象是无序键值对集合（更多给节将在下面章节讨论）。
person =
  name: "Christina"
  likes:
    * "kittens"
    * "and other cute stuff"

# 你也可以用更简洁的方式表示对象：
person = {name: "Christina", likes: ["kittens", "and other cute stuff"]}

# 可以通过键值获取值：
person.name     # => "Christina"
person["name"]  # => "Christina"


# 正则表达式的使用跟 JavaScript 一样：
trailing-space = /\s$/          # dashed-words 变成 dashedWords

# 你也可以用多行描述表达式!（注解和空白会被忽略）
funRE = //
        function\s+(.+)         # name
        \s* \((.*)\) \s*        # arguments
        { (.*) }                # body
        //


########################################################################
## 2. 基本运算
########################################################################

# 数值操作符与 JavaScript 一样：
1 + 2   # => 3
2 - 1   # => 1
2 * 3   # => 6
4 / 2   # => 2
3 % 2   # => 1


# 比较操作符大部份也一样，除了 `==` 等价于 JS 中的 `===`，
# JS 中的 `==` 在 LiveScript 里等价于 `~=`，
# `===` 能进行对象、数组和严格比较。
2 == 2          # => true
2 == "2"        # => false
2 ~= "2"        # => true
2 === "2"       # => false

[1,2,3] == [1,2,3]        # => false
[1,2,3] === [1,2,3]       # => true

+0 == -0     # => true
+0 === -0    # => false

# 其它关系操作符包括 <、<=、> 和 >=

# 罗辑值可以通过 `or`、`and` 和 `not` 结合：
true and false  # => false
false or true   # => true
not false       # => true


# 集合也有一些便利的操作符
[1, 2] ++ [3, 4]                # => [1, 2, 3, 4]
'a' in <[ a b c ]>              # => true
'name' of { name: 'Chris' }     # => true


########################################################################
## 3. 函数
########################################################################        

# 因为 LiveScript 是函数式特性的语言，你可以期待函数在语言里被高规格的对待。
add = (left, right) -> left + right
add 1, 2        # => 3

# 加上 ! 防止函数执行后的返回值
two = -> 2
two!

# LiveScript 与 JavaScript 一样使用函式作用域，且一样拥有闭包的特性。
# 与 JavaScript 不同的地方在于，`=` 变量赋值时，左边的对象永远不用变量宣告。

# `:=` 操作符允许*重新賦值*父作用域里的变量。


# 你可以解构函数的参数，从不定长度的参数结构里获取感兴趣的值。
tail = ([head, ...rest]) -> rest
tail [1, 2, 3]  # => [2, 3]

# 你也可以使用一元或二元操作符转换参数。当然也可以预设传入的参数值。
foo = (a = 1, b = 2) -> a + b
foo!    # => 3

# 你可以以拷贝的方式传入参数来避免副作用，例如：
copy = (^^target, source) ->
  for k,v of source => target[k] = v
  target
a = { a: 1 }
copy a, { b: 2 }        # => { a: 1, b: 2 }
a                       # => { a: 1 }


# 使用长箭号取代短箭号来柯里化一个函数：
add = (left, right) --> left + right
add1 = add 1
add1 2          # => 3

# 函式里有一个隐式的 `it` 变量，意谓着你不用宣告它。
identity = -> it
identity 1      # => 1

# 操作符在 LiveScript 里不是一個函数，但你可以简单地将它们转换成函数！
# Enter the operator sectioning：
divide-by-2 = (/ 2)
[2, 4, 8, 16].map(divide-by-2) .reduce (+)


# LiveScript 里不只有应用函数，如同其它良好的函数式语言，你可以合并函数获得更多发挥：
double-minus-one = (- 1) . (* 2)

# 除了普通的数学公式合并 `f . g` 之外，还有 `>>` 和 `<<` 操作符定义函数的合并顺序。
double-minus-one = (* 2) >> (- 1)
double-minus-one = (- 1) << (* 2)


# 说到合并函数的参数, LiveScript 使用 `|>` 和 `<|` 操作符将参数传入：
map = (f, xs) --> xs.map f
[1 2 3] |> map (* 2)            # => [2 4 6]

# 你也可以选择填入值的位置，只需要使用底线 _ 标记：
reduce = (f, xs, initial) --> xs.reduce f, initial
[1 2 3] |> reduce (+), _, 0     # => 6


# 你也能使 _ 让任何函数变成偏函数应用：
div = (left, right) -> left / right
div-by-2 = div _, 2
div-by-2 4      # => 2


# 最后，也很重要的，LiveScript 拥有後呼叫特性， 可以是基於回调的代码
# （你可以试试其它函数式特性的解法，比如 Promises）：
readFile = (name, f) -> f name
a <- readFile 'foo'
b <- readFile 'bar'
console.log a + b

# 等同於：
readFile 'foo', (a) -> readFile 'bar', (b) -> console.log a + b


########################################################################
## 4. 模式、判断和流程控制
########################################################################

# 流程控制可以使用 `if...else` 表达式：
x = if n > 0 then \positive else \negative

# 除了 `then` 你也可以使用 `=>`
x = if n > 0 => \positive
    else        \negative

# 过於复杂的流程可以用 `switch` 表达式代替：
y = {}
x = switch
  | (typeof y) is \number => \number
  | (typeof y) is \string => \string
  | 'length' of y         => \array
  | otherwise             => \object      # `otherwise` 和 `_` 是等价的。

# 函数主体、宣告式和赋值式可以表式成 `switch`，这可以省去一些代码：
take = (n, [x, ...xs]) -->
                        | n == 0 => []
                        | _      => [x] ++ take (n - 1), xs


########################################################################
## 5. 推导式
########################################################################

# 在 JavaScript 的标准函式库里有一些辅助函数能帮助处理列表及对象
#（LiveScript 则带有一个 prelude.ls ，作为标准函式库的补充 ）， 
# 推导式能让你使用优雅的语法且快速地处理这些事：
oneToTwenty = [1 to 20]
evens       = [x for x in oneToTwenty when x % 2 == 0]

# 在推导式里 `when` 和 `unless` 可以当成过滤器使用。

# 对象推导式在使用上也是同样的方式，差别在于你使用的是对象而不是数组：
copy = { [k, v] for k, v of source }


########################################################################
## 6. OOP
########################################################################

# 虽然 LiveScript 是一门函数式语言，但具有一些命令式及面向对象的特性。
# 像是 class 语法和一些借镜於 CoffeeScript 的类别继承语法糖：
class Animal
  (@name, kind) ->
    @kind = kind
  action: (what) -> "*#{@name} (a #{@kind}) #{what}*"

class Cat extends Animal
  (@name) -> super @name, 'cat'
  purr: -> @action 'purrs'

kitten = new Cat 'Mei'
kitten.purr!      # => "*Mei (a cat) purrs*"

# 除了类别的单一继承模式之外，还提供了像混入 (Mixins) 这种特性。
# Mixins 在语言里被当成普通对象：
Huggable =
  hug: -> @action 'is hugged'

class SnugglyCat extends Cat implements Huggable

kitten = new SnugglyCat 'Purr'
kitten.hug!     # => "*Mei (a cat) is hugged*"
```

## 延伸阅读

LiveScript 还有许多强大之处，但这些应该足够启发你写些小型函数式程式了。 
[LiveScript](http://livescript.net/)有更多关于 LiveScript 的资讯
和线上编译器等着你来试！

你也可以参考
[prelude.ls](http://gkz.github.io/prelude-ls/)，和一些 `#livescript` 
的网络聊天室频道。
---
language: Lua
lang: zh-cn
contributors: 
    - ["Tyler Neylon", "http://tylerneylon.com/"]
    - ["Rob Hoelz", "http://hoelz.ro"]
    - ["Jakukyo Friel", "http://weakish.github.io"]
    - ["Craig Roddin", "craig.roddin@gmail.com"]
    - ["Amr Tamimi", "https://amrtamimi.com"]
translators:
    - ["Jakukyo Friel", "http://weakish.github.io"]
filename: lua-cn.lua
---

```lua
-- 单行注释以两个连字符开头 

--[[ 
     多行注释
--]]

---------------------------------------------------- 
-- 1. 变量和流程控制
---------------------------------------------------- 

num = 42  -- 所有的数字都是双精度浮点型。
-- 别害怕，64位的双精度浮点型数字中有52位用于 
-- 保存精确的整型值; 对于52位以内的整型值， 
-- 不用担心精度问题。

s = 'walternate'  -- 和Python一样，字符串不可变。 
t = "也可以用双引号" 
u = [[ 多行的字符串
       以两个方括号
       开始和结尾。]] 
t = nil  -- 撤销t的定义; Lua 支持垃圾回收。 

-- 块使用do/end之类的关键字标识： 
while num < 50 do 
  num = num + 1  -- 不支持 ++ 或 += 运算符。 
end 

-- If语句： 
if num > 40 then 
  print('over 40') 
elseif s ~= 'walternate' then  -- ~= 表示不等于。 
  -- 像Python一样，用 == 检查是否相等 ；字符串同样适用。 
  io.write('not over 40\n')  -- 默认标准输出。
else 
  -- 默认全局变量。 
  thisIsGlobal = 5  -- 通常使用驼峰。

  -- 如何定义局部变量： 
  local line = io.read()  -- 读取标准输入的下一行。 

  -- ..操作符用于连接字符串： 
  print('Winter is coming, ' .. line) 
end 

-- 未定义的变量返回nil。 
-- 这不是错误： 
foo = anUnknownVariable  -- 现在 foo = nil. 

aBoolValue = false 

--只有nil和false为假; 0和 ''均为真！ 
if not aBoolValue then print('false') end 

-- 'or'和 'and'短路 
-- 类似于C/js里的 a?b:c 操作符： 
ans = aBoolValue and 'yes' or 'no'  --> 'no' 

karlSum = 0 
for i = 1, 100 do  -- 范围包含两端 
  karlSum = karlSum + i 
end 

-- 使用 "100, 1, -1" 表示递减的范围： 
fredSum = 0 
for j = 100, 1, -1 do fredSum = fredSum + j end 

-- 通常，范围表达式为begin, end[, step]. 

-- 循环的另一种结构： 
repeat 
  print('the way of the future') 
  num = num - 1 
until num == 0 

---------------------------------------------------- 
-- 2. 函数。 
---------------------------------------------------- 

function fib(n)
  if n < 2 then return n end
  return fib(n - 2) + fib(n - 1)
end

-- 支持闭包及匿名函数： 
function adder(x) 
  -- 调用adder时，会创建返回的函数，
  -- 并且会记住x的值： 
  return function (y) return x + y end 
end 
a1 = adder(9) 
a2 = adder(36) 
print(a1(16))  --> 25 
print(a2(64))  --> 100 

-- 返回值、函数调用和赋值都可以
-- 使用长度不匹配的list。 
-- 不匹配的接收方会被赋值nil； 
-- 不匹配的发送方会被丢弃。 

x, y, z = 1, 2, 3, 4 
-- x = 1、y = 2、z = 3, 而 4 会被丢弃。 

function bar(a, b, c) 
  print(a, b, c) 
  return 4, 8, 15, 16, 23, 42 
end 

x, y = bar('zaphod')  --> 打印 "zaphod  nil nil" 
-- 现在 x = 4, y = 8, 而值15..42被丢弃。 

-- 函数是一等公民，可以是局部的，也可以是全局的。 
-- 以下表达式等价： 
function f(x) return x * x end 
f = function (x) return x * x end 

-- 这些也是等价的： 
local function g(x) return math.sin(x) end
local g; g = function (x) return math.sin(x) end
-- 以上均因'local g'，使得g可以自引用。
local g = function(x) return math.sin(x) end
-- 等价于 local function g(x)..., 但函数体中g不可自引用

-- 顺便提下，三角函数以弧度为单位。 

-- 用一个字符串参数调用函数，可以省略括号： 
print 'hello'  --可以工作。 

-- 调用函数时，如果只有一个table参数，
-- 同样可以省略括号（table详情见下）：
print {} -- 一样可以工作。

---------------------------------------------------- 
-- 3. Table。 
---------------------------------------------------- 

-- Table = Lua唯一的组合数据结构; 
--         它们是关联数组。 
-- 类似于PHP的数组或者js的对象， 
-- 它们是哈希表或者字典，也可以当列表使用。 

-- 按字典/map的方式使用Table： 

-- Dict字面量默认使用字符串类型的key： 
t = {key1 = 'value1', key2 = false} 

-- 字符串key可以使用类似js的点标记： 
print(t.key1)  -- 打印 'value1'. 
t.newKey = {}  -- 添加新的键值对。 
t.key2 = nil   -- 从table删除 key2。 

-- 使用任何非nil的值作为key： 
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'} 
print(u[6.28])  -- 打印 "tau" 

-- 数字和字符串的key按值匹配的
-- table按id匹配。 
a = u['@!#']  -- 现在 a = 'qbert'. 
b = u[{}]     -- 我们或许期待的是 1729,  但是得到的是nil: 
-- b = nil ，因为没有找到。 
-- 之所以没找到，是因为我们用的key与保存数据时用的不是同
-- 一个对象。 
-- 所以字符串和数字是移植性更好的key。 

-- 只需要一个table参数的函数调用不需要括号： 
function h(x) print(x.key1) end 
h{key1 = 'Sonmi~451'}  -- 打印'Sonmi~451'. 

for key, val in pairs(u) do  -- 遍历Table
  print(key, val) 
end 

-- _G 是一个特殊的table，用于保存所有的全局变量 
print(_G['_G'] == _G)  -- 打印'true'. 

-- 按列表/数组的方式使用： 

-- 列表字面量隐式添加整数键： 
v = {'value1', 'value2', 1.21, 'gigawatts'} 
for i = 1, #v do  -- #v 是列表的大小
  print(v[i])  -- 索引从 1 开始!! 太疯狂了！ 
end
-- 'list'并非真正的类型，v 其实是一个table， 
-- 只不过它用连续的整数作为key，可以像list那样去使用。 

---------------------------------------------------- 
-- 3.1 元表（metatable） 和元方法（metamethod）。 
---------------------------------------------------- 

-- table的元表提供了一种机制，支持类似操作符重载的行为。
-- 稍后我们会看到元表如何支持类似js prototype的行为。 

f1 = {a = 1, b = 2}  -- 表示一个分数 a/b. 
f2 = {a = 2, b = 3} 

-- 这会失败：
-- s = f1 + f2 

metafraction = {} 
function metafraction.__add(f1, f2) 
  local sum = {} 
  sum.b = f1.b * f2.b 
  sum.a = f1.a * f2.b + f2.a * f1.b 
  return sum
end

setmetatable(f1, metafraction) 
setmetatable(f2, metafraction) 

s = f1 + f2  -- 调用在f1的元表上的__add(f1, f2) 方法 

-- f1, f2 没有关于元表的key，这点和js的prototype不一样。 
-- 因此你必须用getmetatable(f1)获取元表。
-- 元表是一个普通的table， 
-- 元表的key是普通的Lua中的key，例如__add。 

-- 但是下面一行代码会失败，因为s没有元表： 
-- t = s + s 
-- 下面提供的与类相似的模式可以解决这个问题： 

-- 元表的__index 可以重载用于查找的点操作符： 
defaultFavs = {animal = 'gru', food = 'donuts'} 
myFavs = {food = 'pizza'} 
setmetatable(myFavs, {__index = defaultFavs}) 
eatenBy = myFavs.animal  -- 可以工作！感谢元表 

-- 如果在table中直接查找key失败，会使用
-- 元表的__index 递归地重试。

-- __index的值也可以是function(tbl, key)
-- 这样可以支持自定义查找。 

-- __index、__add等的值，被称为元方法。 
-- 这里是一个table元方法的清单： 

-- __add(a, b)                     for a + b 
-- __sub(a, b)                     for a - b 
-- __mul(a, b)                     for a * b 
-- __div(a, b)                     for a / b 
-- __mod(a, b)                     for a % b 
-- __pow(a, b)                     for a ^ b 
-- __unm(a)                        for -a 
-- __concat(a, b)                  for a .. b 
-- __len(a)                        for #a 
-- __eq(a, b)                      for a == b 
-- __lt(a, b)                      for a < b 
-- __le(a, b)                      for a <= b 
-- __index(a, b)  <fn or a table>  for a.b 
-- __newindex(a, b, c)             for a.b = c 
-- __call(a, ...)                  for a(...) 

---------------------------------------------------- 
-- 3.2 与类相似的table和继承。 
---------------------------------------------------- 

-- Lua没有内建的类；可以通过不同的方法，利用表和元表
-- 来实现类。 

-- 下面是一个例子，解释在后面： 

Dog = {}                                   -- 1. 

function Dog:new()                         -- 2. 
  local newObj = {sound = 'woof'}                -- 3. 
  self.__index = self                      -- 4. 
  return setmetatable(newObj, self)        -- 5. 
end 

function Dog:makeSound()                   -- 6. 
  print('I say ' .. self.sound) 
end 

mrDog = Dog:new()                          -- 7. 
mrDog:makeSound()  -- 'I say woof'         -- 8. 

-- 1. Dog看上去像一个类；其实它是一个table。 
-- 2. 函数tablename:fn(...) 等价于
--    函数tablename.fn(self, ...)
--    冒号（:）只是添加了self作为第一个参数。 
--    阅读7 & 8条 了解self变量是如何得到其值的。 
-- 3. newObj是类Dog的一个实例。 
-- 4. self = 被继承的类。通常self = Dog，不过继承可以改变它。 
--    如果把newObj的元表和__index都设置为self， 
--    newObj就可以得到self的函数。 
-- 5. 备忘：setmetatable返回其第一个参数。 
-- 6. 冒号（：）的作用和第2条一样，不过这里 
--    self是一个实例，而不是类 
-- 7. 等价于Dog.new(Dog)，所以在new()中，self = Dog。 
-- 8. 等价于mrDog.makeSound(mrDog); self = mrDog。 

---------------------------------------------------- 

-- 继承的例子： 

LoudDog = Dog:new()                           -- 1. 

function LoudDog:makeSound() 
  local s = self.sound .. ' '                       -- 2. 
  print(s .. s .. s) 
end 

seymour = LoudDog:new()                       -- 3. 
seymour:makeSound()  -- 'woof woof woof'      -- 4. 

-- 1. LoudDog获得Dog的方法和变量列表。 
-- 2. 因为new()的缘故，self拥有了一个'sound' key，参见第3条。 
-- 3. 等价于LoudDog.new(LoudDog)，转换一下就是 
--    Dog.new(LoudDog)，这是因为LoudDog没有'new' key， 
--    但是它的元表中有 __index = Dog。 
--    结果: seymour的元表是LoudDog，并且 
--    LoudDog.__index = Dog。所以有seymour.key 
--    = seymour.key, LoudDog.key, Dog.key 
--    从其中第一个有指定key的table获取。 
-- 4. 在LoudDog可以找到'makeSound'的key； 
--    等价于LoudDog.makeSound(seymour)。 

-- 如果有必要，子类也可以有new()，与基类相似： 
function LoudDog:new() 
  local newObj = {} 
  -- 初始化newObj 
  self.__index = self 
  return setmetatable(newObj, self) 
end 

---------------------------------------------------- 
-- 4. 模块 
---------------------------------------------------- 


--[[ 我把这部分给注释了，这样脚本剩下的部分可以运行 
```

```lua
-- 假设文件mod.lua的内容类似这样： 
local M = {} 

local function sayMyName() 
  print('Hrunkner') 
end 

function M.sayHello() 
  print('Why hello there') 
  sayMyName() 
end 

return M 

-- 另一个文件可以使用mod.lua的功能： 
local mod = require('mod')  -- 运行文件mod.lua. 

-- require是包含模块的标准做法。 
-- require等价于:     (针对没有被缓存的情况；参见后面的内容) 
local mod = (function () 
  <contents of mod.lua> 
end)() 
-- mod.lua被包在一个函数体中，因此mod.lua的局部变量
-- 对外不可见。 

-- 下面的代码可以工作，因为在这里mod = mod.lua 中的 M： 
mod.sayHello()  -- Says hello to Hrunkner. 

-- 这是错误的；sayMyName只在mod.lua中存在： 
mod.sayMyName()  -- 错误 

-- require返回的值会被缓存，所以一个文件只会被运行一次， 
-- 即使它被require了多次。 

-- 假设mod2.lua包含代码"print('Hi!')"。 
local a = require('mod2')  -- 打印Hi! 
local b = require('mod2')  -- 不再打印; a=b. 

-- dofile与require类似，但是不缓存： 
dofile('mod2')  --> Hi! 
dofile('mod2')  --> Hi! (再次运行，与require不同) 

-- loadfile加载一个lua文件，但是并不运行它。 
f = loadfile('mod2')  -- Calling f() runs mod2.lua. 

-- loadstring是loadfile的字符串版本。 
g = loadstring('print(343)')  --返回一个函数。 
g()  -- 打印343; 在此之前什么也不打印。 

--]] 
```

## 参考



为什么？我非常兴奋地学习lua， 这样我就可以使用[Löve 2D游戏引擎](http://love2d.org/)来编游戏。

怎么做？我从[BlackBulletIV的面向程序员的Lua指南](http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/)入门。接着我阅读了官方的[Lua编程](http://www.lua.org/pil/contents.html)一书。

lua-users.org上的[Lua简明参考](http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf)应该值得一看。

本文没有涉及标准库的内容：
   
* <a href="http://lua-users.org/wiki/StringLibraryTutorial">string library</a>
* <a href="http://lua-users.org/wiki/TableLibraryTutorial">table library</a>
* <a href="http://lua-users.org/wiki/MathLibraryTutorial">math library</a>
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a>
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a>

顺便说一下，整个文件是可运行的Lua; 
保存为 learn-cn.lua 用命令 `lua learn-cn.lua` 启动吧！

本文首次撰写于 [tylerneylon.com](http://tylerneylon.com) 同时也有 [github gist](https://gist.github.com/tylerneylon/5853042) 版.

使用Lua，欢乐常在！
---
language: markdown
contributors:
    - ["Dan Turkel", "http://danturkel.com/"]
translators:
    - ["Fangzhou Chen","https://github.com/FZSS"]
filename: learnmarkdown-cn.md
lang: zh-cn
---

Markdown 由 John Gruber 于 2004年创立. 它旨在成为一门容易读写的语法结构，并可以便利地转换成 HTML（以及其他很多）格式。

欢迎您多多反馈以及分支和请求合并。


```markdown
<!-- Markdown 是 HTML 的父集，所以任何 HTML 文件都是有效的 Markdown。
这意味着我们可以在 Markdown 里使用任何 HTML 元素，比如注释元素，
且不会被 Markdown 解析器所影响。不过如果你在 Markdown 文件内创建了 HTML 元素，
你将无法在 HTML 元素的内容中使用 Markdown 语法。-->

<!-- 在不同的解析器中，Markdown 的实现方法有所不同。
此教程会指出当某功能是否通用及是否只对某一解析器有效。 -->

<!-- 标头 -->
<!-- 通过在文本前加上不同数量的hash(#), 你可以创建相对应的 <h1> 
到 <h6> HTML元素。-->

# 这是一个 <h1>
## 这是一个 <h2>
### 这是一个 <h3>
#### 这是一个 <h4>
##### 这是一个 <h5>
###### 这是一个 <h6>

<!-- 对于 <h1> 和 <h2> 元素，Markdown 额外提供了两种添加方式。 -->
这是一个 h1
=============

这是一个 h2
-------------

<!-- 简易文本样式 -->
<!-- 文本的斜体，粗体，和删除线在 Markdown 中可以轻易地被实现。-->

*此文本为斜体。*
_此文本也是。_

**此文本为粗体。**
__此文本也是__

***此文本是斜体加粗体。***
**_或者这样。_**
*__这个也是！__*

<!-- 在 GitHub 采用的 Markdown 中 -->

~~此文本为删除线效果。~~

<!-- 单个段落由一句或多句邻近的句子组成，这些句子由一个或多个空格分隔。-->

这是第一段落. 这句话在同一个段落里，好玩么？

现在我是第二段落。
这句话也在第二段落！

这句话在第三段落！

<!-- 如果你插入一个 HTML中的<br />标签，你可以在段末加入两个以上的空格，
然后另起一段。-->

此段落结尾有两个空格（选中以显示）。  

上文有一个 <br /> ！

<!-- 段落引用可由 > 字符轻松实现。-->

> 这是一个段落引用. 你可以
> 手动断开你的句子，然后在每句句子前面添加 “>” 字符。或者让你的句子变得很长，以至于他们自动得断开。
> 只要你的文字以“>” 字符开头，两种方式无异。

> 你也对文本进行
>> 多层引用
> 这多机智啊！

<!-- 序列 -->
<!-- 无序序列可由星号，加号或者减号来建立 -->

* 项目
* 项目
* 另一个项目

或者

+ 项目
+ 项目
+ 另一个项目

或者 

- 项目
- 项目
- 最后一个项目

<!-- 有序序列可由数字加点来实现 -->

1. 项目一
2. 项目二
3. 项目三

<!-- 即使你的标签数字有误，Markdown 依旧会呈现出正确的序号，
不过这并不是一个好主意-->

1. 项目一
1. 项目二
1. 项目三
<!-- (此段与前例一模一样) -->

<!-- 你也可以使用子序列 -->

1. 项目一
2. 项目二
3. 项目三
    * 子项目
    * 子项目
4. 项目四

<!-- 代码段落 -->
<!-- 代码段落（HTML中 <code>标签）可以由缩进四格（spaces）
或者一个制表符（tab）实现-->

    This is code
    So is this

<!-- 在你的代码中，你仍然使用tab可以进行缩进操作 -->

    my_array.each do |item|
        puts item
    end

<!-- 内联代码可由反引号 ` 实现 -->

John 甚至不知道 `go_to()` 方程是干嘛的!

<!-- 在GitHub的 Markdown中，对于代码你可以使用特殊的语法 -->

\`\`\`ruby <!-- 插入时记得移除反斜线， 仅留```ruby ！ -->
def foobar
    puts "Hello world!"
end
\`\`\` <!-- 这里也是，移除反斜线，仅留 ``` -->

<!-- 以上代码不需要缩进，而且 GitHub 会根据```后表明的语言来进行语法高亮 -->

<!-- 水平线 （<hr />） -->
<!-- 水平线可由三个或以上的星号或者减号创建，可带可不带空格。 -->

***
---
- - - 
****************

<!-- 链接 -->
<!-- Markdown 最棒的地方就是简易的链接制作。链接文字放在中括号[]内，
在随后的括弧()内加入url。-->

[点我点我!](http://test.com/)

<!-- 你也可以为链接加入一个标题：在括弧内使用引号 -->

[点我点我!](http://test.com/ "连接到Test.com")

<!-- 相对路径也可以有 -->

[去 music](/music/).

<!-- Markdown同样支持引用样式的链接 -->

[点此链接][link1]以获取更多信息！
[看一看这个链接][foobar] 如果你愿意的话.

[link1]: http://test.com/ "Cool!"
[foobar]: http://foobar.biz/ "Alright!"

<!-- 链接的标题可以处于单引号中，括弧中或是被忽略。引用名可以在文档的任意何处，
并且可以随意命名，只要名称不重复。-->

<!-- “隐含式命名” 的功能可以让链接文字作为引用名 -->

[This][] is a link.

[this]: http://thisisalink.com/

<!-- 但这并不常用 -->

<!-- 图像 -->
<!-- 图像与链接相似，只需在前添加一个感叹号 -->

![这是我图像的悬停文本(alt text)](http://imgur.com/myimage.jpg "可选命名")

<!-- 引用样式也同样起作用 -->

![这是我的悬停文本.][myimage]

[myimage]: relative/urls/cool/image.jpg "在此输入标题"

<!-- 杂项 -->
<!-- 自动链接 -->

<http://testwebsite.com/> 与
[http://testwebsite.com/](http://testwebsite.com/) 等同

<!-- 电子邮件的自动链接 -->

<foo@bar.com>

<!-- 转义字符 -->

我希望 *将这段文字置于星号之间* 但是我不希望它被
斜体化, 所以我就: \*这段置文字于星号之间\*。

<!-- 表格 -->
<!-- 表格只被 GitHub 的 Markdown 支持，并且有一点笨重，但如果你真的要用的话: -->

| 第一列        | 第二列    | 第三列       |
| :----------   | :------:  | ----------:  |
| 左对齐        | 居个中    | 右对齐       |
| 某某某        | 某某某    | 某某某       |

<!-- 或者, 同样的 -->

第一列 | 第二列 | 第三列
:-- | :-: | --:
这太丑了 | 药不能 | 停

<!-- 结束! -->

```

更多信息, 请于[此处](http://daringfireball.net/projects/Markdown/syntax)参见 John Gruber 关于语法的官方帖子，及于[此处](https://github.com/adam-p/Markdown-here/wiki/Markdown-Cheatsheet) 参见 Adam Pritchard 的摘要笔记。
---
language: Matlab
filename: matlab-cn.m
contributors:
    - ["mendozao", "http://github.com/mendozao"]
    - ["jamesscottbrown", "http://jamesscottbrown.com"]
translators:
    - ["sunxb10", "https://github.com/sunxb10"]
lang: zh-cn

---

MATLAB 是 MATrix LABoratory （矩阵实验室）的缩写，它是一种功能强大的数值计算语言，在工程和数学领域中应用广泛。

如果您有任何需要反馈或交流的内容，请联系本教程作者[@the_ozzinator](https://twitter.com/the_ozzinator)、[osvaldo.t.mendoza@gmail.com](mailto:osvaldo.t.mendoza@gmail.com)。

```matlab
% 以百分号作为注释符

%{
多行注释
可以
这样
表示
%}

% 指令可以随意跨行，但需要在跨行处用 '...' 标明：
 a = 1 + 2 + ...
 + 4

% 可以在MATLAB中直接向操作系统发出指令
!ping google.com

who  % 显示内存中的所有变量
whos  % 显示内存中的所有变量以及它们的类型
clear  % 清除内存中的所有变量
clear('A')  % 清除指定的变量
openvar('A')  % 在变量编辑器中编辑指定变量

clc  % 清除命令窗口中显示的所有指令
diary  % 将命令窗口中的内容写入本地文件
ctrl-c  % 终止当前计算

edit('myfunction.m')  % 在编辑器中打开指定函数或脚本
type('myfunction.m')  % 在命令窗口中打印指定函数或脚本的源码

profile on  % 打开 profile 代码分析工具
profile of  % 关闭 profile 代码分析工具
profile viewer  % 查看 profile 代码分析工具的分析结果

help command    % 在命令窗口中显示指定命令的帮助文档
doc command     % 在帮助窗口中显示指定命令的帮助文档
lookfor command  % 在所有 MATLAB 内置函数的头部注释块的第一行中搜索指定命令
lookfor command -all  % 在所有 MATLAB 内置函数的整个头部注释块中搜索指定命令


% 输出格式
format short    % 浮点数保留 4 位小数
format long     % 浮点数保留 15 位小数
format bank     % 金融格式，浮点数只保留 2 位小数
fprintf('text') % 在命令窗口中显示 "text"
disp('text')    % 在命令窗口中显示 "text"


% 变量与表达式
myVariable = 4  % 命令窗口中将新创建的变量
myVariable = 4; % 加上分号可使命令窗口中不显示当前语句执行结果
4 + 6       % ans = 10 
8 * myVariable  % ans = 32 
2 ^ 3       % ans = 8 
a = 2; b = 3; 
c = exp(a)*sin(pi/2) % c = 7.3891


% 调用函数有两种方式：
% 标准函数语法：
load('myFile.mat', 'y') % 参数放在括号内，以英文逗号分隔
% 指令语法:
load myFile.mat y   % 不加括号，以空格分隔参数
% 注意在指令语法中参数不需要加引号：在这种语法下，所有输入参数都只能是文本文字，
% 不能是变量的具体值，同样也不能是输出变量
[V,D] = eig(A);  % 这条函数调用无法转换成等价的指令语法
[~,D] = eig(A);  % 如果结果中只需要 D 而不需要 V 则可以这样写



% 逻辑运算
1 > 5  % 假，ans = 0
10 >= 10  % 真，ans = 1
3 ~= 4  % 不等于 -> ans = 1
3 == 3  % 等于 -> ans = 1
3 > 1 && 4 > 1  % 与 -> ans = 1
3 > 1 || 4 > 1  % 或 -> ans = 1
~1  % 非 -> ans = 0

% 逻辑运算可直接应用于矩阵，运算结果也是矩阵
A > 5
% 对矩阵中每个元素做逻辑运算，若为真，则在运算结果的矩阵中对应位置的元素就是 1
A( A > 5 )
% 如此返回的向量，其元素就是 A 矩阵中所有逻辑运算为真的元素

% 字符串
a = 'MyString'
length(a)  % ans = 8
a(2)  % ans = y
[a,a]  % ans = MyStringMyString
b = '字符串'  % MATLAB目前已经可以支持包括中文在内的多种文字
length(b)  % ans = 3
b(2)  % ans = 符
[b,b]  % ans = 字符串字符串


% 元组（cell 数组）
a = {'one', 'two', 'three'} 
a(1)  % ans = 'one' - 返回一个元组
char(a(1))  % ans = one - 返回一个字符串


% 结构体
A.b = {'one','two'};
A.c = [1 2];
A.d.e = false;


% 向量
x = [4 32 53 7 1] 
x(2)  % ans = 32，MATLAB中向量的下标索引从1开始，不是0
x(2:3)  % ans = 32 53
x(2:end)  % ans = 32 53 7 1

x = [4; 32; 53; 7; 1]  % 列向量

x = [1:10]  % x = 1 2 3 4 5 6 7 8 9 10


% 矩阵
A = [1 2 3; 4 5 6; 7 8 9] 
% 以分号分隔不同的行，以空格或逗号分隔同一行中的不同元素
% A =

%     1     2     3
%     4     5     6
%     7     8     9

A(2,3) % ans = 6，A(row, column)
A(6) % ans = 8 
% （隐式地将 A 的三列首尾相接组成一个列向量，然后取其下标为 6 的元素）


A(2,3) = 42  % 将第 2 行第 3 列的元素设为 42
% A =

%     1     2     3
%     4     5     42
%     7     8     9

A(2:3,2:3)  % 取原矩阵中的一块作为新矩阵
%ans =

%     5     42
%     8     9

A(:,1)  % 第 1 列的所有元素
%ans =

%     1
%     4
%     7

A(1,:)  % 第 1 行的所有元素
%ans =

%     1     2     3

[A ; A]  % 将两个矩阵上下相接构成新矩阵
%ans =

%     1     2     3
%     4     5    42
%     7     8     9
%     1     2     3
%     4     5    42
%     7     8     9

% 等价于 
vertcat(A, A);


[A , A]  % 将两个矩阵左右相接构成新矩阵

%ans =

%     1     2     3     1     2     3
%     4     5    42     4     5    42
%     7     8     9     7     8     9

% 等价于
horzcat(A, A);


A(:, [3 1 2])  % 重新排布原矩阵的各列
%ans =

%     3     1     2
%    42     4     5
%     9     7     8

size(A)  % 返回矩阵的行数和列数，ans = 3 3

A(1, :) =[]  % 删除矩阵的第 1 行
A(:, 1) =[]  % 删除矩阵的第 1 列

transpose(A)  % 矩阵转置，等价于 A'
ctranspose(A)  % 矩阵的共轭转置（对矩阵中的每个元素取共轭复数）


% 元素运算 vs. 矩阵运算
% 单独运算符就是对矩阵整体进行矩阵运算
% 在运算符加上英文句点就是对矩阵中的元素进行元素计算
% 示例如下：
A * B  % 矩阵乘法，要求 A 的列数等于 B 的行数
A .* B  % 元素乘法，要求 A 和 B 形状一致（A 的行数等于 B 的行数， A 的列数等于 B 的列数）
% 元素乘法的结果是与 A 和 B 形状一致的矩阵，其每个元素等于 A 对应位置的元素乘 B 对应位置的元素

% 以下函数中，函数名以 m 结尾的执行矩阵运算，其余执行元素运算： 
exp(A)  % 对矩阵中每个元素做指数运算
expm(A)  % 对矩阵整体做指数运算
sqrt(A)  % 对矩阵中每个元素做开方运算
sqrtm(A)  % 对矩阵整体做开放运算（即试图求出一个矩阵，该矩阵与自身的乘积等于 A 矩阵）


% 绘图
x = 0:.10:2*pi;  % 生成一向量，其元素从 0 开始，以 0.1 的间隔一直递增到 2*pi（pi 就是圆周率）
y = sin(x);
plot(x,y)
xlabel('x axis')
ylabel('y axis')
title('Plot of y = sin(x)')
axis([0 2*pi -1 1])  % x 轴范围是从 0 到 2*pi，y 轴范围是从 -1 到 1

plot(x,y1,'-',x,y2,'--',x,y3,':')  % 在同一张图中绘制多条曲线
legend('Line 1 label', 'Line 2 label')  % 为图片加注图例
% 图例数量应当小于或等于实际绘制的曲线数目，从 plot 绘制的第一条曲线开始对应

% 在同一张图上绘制多条曲线的另一种方法：
% 使用 hold on，令系统保留前次绘图结果并在其上直接叠加新的曲线，
% 如果没有 hold on，则每个 plot 都会首先清除之前的绘图结果再进行绘制。
% 在 hold on 和 hold off 中可以放置任意多的 plot 指令，
% 它们和 hold on 前最后一个 plot 指令的结果都将显示在同一张图中。
plot(x, y1)
hold on
plot(x, y2)
plot(x, y3)
plot(x, y4)
hold off

loglog(x, y)  % 对数—对数绘图
semilogx(x, y)  % 半对数（x 轴对数）绘图
semilogy(x, y)  % 半对数（y 轴对数）绘图

fplot (@(x) x^2, [2,5])  % 绘制函数 x^2 在 [2, 5] 区间的曲线

grid on  % 在绘制的图中显示网格，使用 grid off 可取消网格显示
axis square  % 将当前坐标系设定为正方形（保证在图形显示上各轴等长）
axis equal  % 将当前坐标系设定为相等（保证在实际数值上各轴等长）

scatter(x, y);  % 散点图
hist(x);  % 直方图

z = sin(x);
plot3(x,y,z);  % 绘制三维曲线

pcolor(A)  % 伪彩色图（热图）
contour(A)  % 等高线图
mesh(A)  % 网格曲面图

h = figure  % 创建新的图片对象并返回其句柄 h
figure(h)  % 将句柄 h 对应的图片作为当前图片
close(h)  % 关闭句柄 h 对应的图片
close all  % 关闭 MATLAB 中所用打开的图片
close  % 关闭当前图片

shg  % 显示图形窗口
clf clear  % 清除图形窗口中的图像，并重置图像属性

% 图像属性可以通过图像句柄进行设定
% 在创建图像时可以保存图像句柄以便于设置
% 也可以用 gcf 函数返回当前图像的句柄 
h = plot(x, y);  % 在创建图像时显式地保存图像句柄
set(h, 'Color', 'r')
% 颜色代码：'y' 黄色，'m' 洋红色，'c' 青色，'r' 红色，'g' 绿色，'b' 蓝色，'w' 白色，'k' 黑色
set(h, 'Color', [0.5, 0.5, 0.4])
% 也可以使用 RGB 值指定颜色
set(h, 'LineStyle', '--')
% 线型代码：'--' 实线，'---' 虚线，':' 点线，'-.' 点划线，'none' 不划线
get(h, 'LineStyle')
% 获取当前句柄的线型


% 用 gca 函数返回当前图像的坐标轴句柄
set(gca, 'XDir', 'reverse');  % 令 x 轴反向

% 用 subplot 指令创建平铺排列的多张子图
subplot(2,3,1);  % 选择 2 x 3 排列的子图中的第 1 张图
plot(x1); title('First Plot')  % 在选中的图中绘图
subplot(2,3,2);  % 选择 2 x 3 排列的子图中的第 2 张图
plot(x2); title('Second Plot')  % 在选中的图中绘图


% 要调用函数或脚本，必须保证它们在你的当前工作目录中
path  % 显示当前工作目录
addpath /path/to/dir  % 将指定路径加入到当前工作目录中
rmpath /path/to/dir  % 将指定路径从当前工作目录中删除
cd /path/to/move/into  % 以制定路径作为当前工作目录


% 变量可保存到 .mat 格式的本地文件
save('myFileName.mat')  % 保存当前工作空间中的所有变量 
load('myFileName.mat')  % 将指定文件中的变量载入到当前工作空间 


% .m 脚本文件
% 脚本文件是一个包含多条 MATLAB 指令的外部文件，以 .m 为后缀名
% 使用脚本文件可以避免在命令窗口中重复输入冗长的指令


% .m 函数文件
% 与脚本文件类似，同样以 .m 作为后缀名
% 但函数文件可以接受用户输入的参数并返回运算结果
% 并且函数拥有自己的工作空间（变量域），不必担心变量名称冲突
% 函数文件的名称应当与其所定义的函数的名称一致（比如下面例子中函数文件就应命名为 double_input.m）
% 使用 'help double_input.m' 可返回函数定义中第一行注释信息
function output = double_input(x) 
    % double_input(x) 返回 x 的 2 倍
    output = 2*x;
end
double_input(6)  % ans = 12 


% 同样还可以定义子函数和内嵌函数
% 子函数与主函数放在同一个函数文件中，且只能被这个主函数调用
% 内嵌函数放在另一个函数体内，可以直接访问被嵌套函数的各个变量


% 使用匿名函数可以不必创建 .m 函数文件
% 匿名函数适用于快速定义某函数以便传递给另一指令或函数（如绘图、积分、求根、求极值等）
% 下面示例的匿名函数返回输入参数的平方根，可以使用句柄 sqr 进行调用：
sqr = @(x) x.^2;
sqr(10) % ans = 100
doc function_handle % find out more


% 接受用户输入
a = input('Enter the value: ')


% 从文件中读取数据
fopen(filename)
% 类似函数还有 xlsread（excel 文件）、importdata（CSV 文件）、imread（图像文件）


% 输出
disp(a)  % 在命令窗口中打印变量 a 的值
disp('Hello World')  % 在命令窗口中打印字符串
fprintf  % 按照指定格式在命令窗口中打印内容

% 条件语句（if 和 elseif 语句中的括号并非必需，但推荐加括号避免混淆）
if (a > 15)
    disp('Greater than 15')
elseif (a == 23)
    disp('a is 23')
else
    disp('neither condition met')
end

% 循环语句
% 注意：对向量或矩阵使用循环语句进行元素遍历的效率很低！！
% 注意：只要有可能，就尽量使用向量或矩阵的整体运算取代逐元素循环遍历！！
% MATLAB 在开发时对向量和矩阵运算做了专门优化，做向量和矩阵整体运算的效率高于循环语句
for k = 1:5
    disp(k)
end
    
k = 0;  
while (k < 5)
    k = k + 1;
end


% 程序运行计时：'tic' 是计时开始，'toc' 是计时结束并打印结果
tic
A = rand(1000);
A*A*A*A*A*A*A;
toc


% 链接 MySQL 数据库
dbname = 'database_name';
username = 'root';
password = 'root';
driver = 'com.mysql.jdbc.Driver';
dburl = ['jdbc:mysql://localhost:8889/' dbname];
javaclasspath('mysql-connector-java-5.1.xx-bin.jar');  % 此处 xx 代表具体版本号
% 这里的 mysql-connector-java-5.1.xx-bin.jar 可从 http://dev.mysql.com/downloads/connector/j/ 下载
conn = database(dbname, username, password, driver, dburl); 
sql = ['SELECT * from table_name where id = 22']  % SQL 语句
a = fetch(conn, sql)  % a 即包含所需数据


% 常用数学函数
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
exp(x) 
sqrt(x)
log(x)
log10(x)
abs(x)
min(x)
max(x)
ceil(x)
floor(x)
round(x)
rem(x)
rand  % 均匀分布的伪随机浮点数
randi  % 均匀分布的伪随机整数
randn  % 正态分布的伪随机浮点数

% 常用常数
pi
NaN
inf

% 求解矩阵方程（如果方程无解，则返回最小二乘近似解）
% \ 操作符等价于 mldivide 函数，/ 操作符等价于 mrdivide 函数
x=A\b  % 求解 Ax=b，比先求逆再左乘 inv(A)*b 更加高效、准确
x=b/A  % 求解 xA=b

inv(A)  % 逆矩阵
pinv(A)  % 伪逆矩阵


% 常用矩阵函数
zeros(m, n)  % m x n 阶矩阵，元素全为 0
ones(m, n)  % m x n 阶矩阵，元素全为 1
diag(A)  % 返回矩阵 A 的对角线元素
diag(x)  % 构造一个对角阵，对角线元素就是向量 x 的各元素 
eye(m, n)  % m x n 阶单位矩阵
linspace(x1, x2, n)  % 返回介于 x1 和 x2 之间的 n 个等距节点
inv(A)  % 矩阵 A 的逆矩阵
det(A)  % 矩阵 A 的行列式
eig(A)  % 矩阵 A 的特征值和特征向量
trace(A)  % 矩阵 A 的迹（即对角线元素之和），等价于 sum(diag(A))
isempty(A)  % 测试 A 是否为空
all(A)  % 测试 A 中所有元素是否都非 0 或都为真（逻辑值）
any(A)  % 测试 A 中是否有元素非 0 或为真（逻辑值）
isequal(A, B)  % 测试 A 和 B是否相等
numel(A)  % 矩阵 A 的元素个数
triu(x)  % 返回 x 的上三角这部分
tril(x)  % 返回 x 的下三角这部分
cross(A, B)  % 返回 A 和 B 的叉积（矢量积、外积）
dot(A, B)  % 返回 A 和 B 的点积（数量积、内积），要求 A 和 B 必须等长
transpose(A)  % A 的转置，等价于 A'
fliplr(A)  % 将一个矩阵左右翻转
flipud(A)  % 将一个矩阵上下翻转

% 矩阵分解
[L, U, P] = lu(A)  % LU 分解：PA = LU，L 是下三角阵，U 是上三角阵，P 是置换阵
[P, D] = eig(A)  % 特征值分解：AP = PD，D 是由特征值构成的对角阵，P 的各列就是对应的特征向量
[U, S, V] = svd(X)  % 奇异值分解：XV = US，U 和 V 是酉矩阵，S 是由奇异值构成的半正定实数对角阵

% 常用向量函数
max     % 最大值
min     % 最小值 
length  % 元素个数
sort    % 按升序排列 
sum     % 各元素之和 
prod    % 各元素之积
mode    % 众数
median  % 中位数 
mean    % 平均值 
std     % 标准差
perms(x) % x 元素的全排列

```

## 相关资料

* 官方网页：[http://http://www.mathworks.com/products/matlab/](http://www.mathworks.com/products/matlab/)
* 官方论坛：[http://www.mathworks.com/matlabcentral/answers/](http://www.mathworks.com/matlabcentral/answers/)
---
name: perl
category: language
language: perl
filename: learnperl-cn.pl
contributors:
    - ["Korjavin Ivan", "http://github.com/korjavin"]
translators:
    - ["Yadong Wen", "https://github.com/yadongwen"]
lang: zh-cn
---

Perl 5是一个功能强大、特性齐全的编程语言，有25年的历史。

Perl 5可以在包括便携式设备和大型机的超过100个平台上运行，既适用于快速原型构建，也适用于大型项目开发。

```perl
# 单行注释以#号开头


#### Perl的变量类型

#  变量以$号开头。
#  合法变量名以英文字母或者下划线起始，
#  后接任意数目的字母、数字或下划线。

### Perl有三种主要的变量类型：标量、数组和哈希。

## 标量
#  标量类型代表单个值：
my $animal = "camel";
my $answer = 42;

# 标量类型值可以是字符串、整型或浮点类型，Perl会根据需要自动进行类型转换。

## 数组
#  数组类型代表一列值：
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed   = ("camel", 42, 1.23);



## 哈希
#  哈希类型代表一个键/值对的集合：

my %fruit_color = ("apple", "red", "banana", "yellow");

#  可以使用空格和“=>”操作符更清晰的定义哈希：

my %fruit_color = (
        apple  => "red",
        banana => "yellow",
        );
# perldata中有标量、数组和哈希更详细的介绍。 (perldoc perldata).

# 可以用引用构建更复杂的数据类型，比如嵌套的列表和哈希。

#### 逻辑和循环结构

# Perl有大多数常见的逻辑和循环控制结构

if ( $var ) {
    ...
} elsif ( $var eq 'bar' ) {
    ...
} else {
    ...
}

unless ( condition ) {
                   ...
               }
# 上面这个比"if (!condition)"更可读。

# 有Perl特色的后置逻辑结构
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;

#  while
  while ( condition ) {
                   ...
               }


# for和foreach
for ($i = 0; $i <= $max; $i++) {
                   ...
               }

foreach (@array) {
                   print "This element is $_\n";
               }


#### 正则表达式

# Perl对正则表达式有深入广泛的支持，perlrequick和perlretut等文档有详细介绍。简单来说：

# 简单匹配
if (/foo/)       { ... }  # 如果 $_ 包含"foo"逻辑为真
if ($a =~ /foo/) { ... }  # 如果 $a 包含"foo"逻辑为真

# 简单替换

$a =~ s/foo/bar/;         # 将$a中的foo替换为bar
$a =~ s/foo/bar/g;        # 将$a中所有的foo替换为bar


#### 文件和输入输出

# 可以使用“open()”函数打开文件用于输入输出。

open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";
open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";

# 可以用"<>"操作符读取一个打开的文件句柄。 在标量语境下会读取一行，
# 在列表环境下会将整个文件读入并将每一行赋给列表的一个元素：

my $line  = <$in>;
my @lines = <$in>;

#### 子程序

# 写子程序很简单：

sub logger {
    my $logmessage = shift;
    open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
    print $logfile $logmessage;
}

# 现在可以像内置函数一样调用子程序：

logger("We have a logger subroutine!");


```

#### 使用Perl模块

Perl模块提供一系列特性来帮助你避免重新发明轮子，CPAN是下载模块的好地方( http://www.cpan.org/ )。Perl发行版本身也包含很多流行的模块。

perlfaq有很多常见问题和相应回答，也经常有对优秀CPAN模块的推荐介绍。

#### 深入阅读

    - [perl-tutorial](http://perl-tutorial.org/)
    - [www.perl.com的learn站点](http://www.perl.org/learn.html)
    - [perldoc](http://perldoc.perl.org/)
    - 以及 perl 内置的： `perldoc perlintro`
---
language: PHP
contributors:
    - ["Malcolm Fell", "http://emarref.net/"]
    - ["Trismegiste", "https://github.com/Trismegiste"]
translators:
    - ["Chenbo Li", "http://binarythink.net"]
filename: learnphp-zh.php
lang: zh-cn
---

这份教程所使用的版本是 PHP 5+.

```php
<?php // PHP必须被包围于 <?php ? > 之中

// 如果你的文件中只有php代码，那么最好省略结束括号标记

// 这是单行注释的标志

# 井号也可以，但是//更常见

/*
     这是多行注释
*/

// 使用 "echo" 或者 "print" 来输出信息到标准输出
print('Hello '); // 输出 "Hello " 并且没有换行符

// () 对于echo和print是可选的
echo "World\n"; // 输出 "World" 并且换行
// (每个语句必须以分号结尾)

// 在 <?php 标签之外的语句都被自动输出到标准输出
?>Hello World Again!
<?php


/************************************
 * 类型与变量
 */

// 变量以$开始
// 变量可以以字母或者下划线开头，后面可以跟着数字、字母和下划线

// 布尔值是大小写无关的
$boolean = true;  // 或 TRUE 或 True
$boolean = false; // 或 FALSE 或 False

// 整型
$int1 = 12;   // => 12
$int2 = -12;  // => -12
$int3 = 012;  // => 10 (0开头代表八进制数)
$int4 = 0x0F; // => 15 (0x开头代表十六进制数)

// 浮点型 (即双精度浮点型)
$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

// 算数运算
$sum        = 1 + 1; // 2
$difference = 2 - 1; // 1
$product    = 2 * 2; // 4
$quotient   = 2 / 1; // 2

// 算数运算的简写
$number = 0;
$number += 1;      // $number 自增1
echo $number++;    // 输出1 (运算后自增)
echo ++$number;    // 输出3 (自增后运算)
$number /= $float; // 先除后赋值给 $number

// 字符串需要被包含在单引号之中
$sgl_quotes = '$String'; // => '$String'

// 如果需要在字符串中引用变量，就需要使用双引号
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// 特殊字符只有在双引号中有用
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// 可以把变量包含在一对大括号中
$money = "I have $${number} in the bank.";

// 自 PHP 5.3 开始, nowdocs 可以被用作多行非计算型字符串
$nowdoc = <<<'END'
Multi line
string
END;

// 而Heredocs则可以用作多行计算型字符串
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// 字符串需要用 . 来连接
echo 'This string ' . 'is concatenated';


/********************************
 * 数组
 */

// PHP 中的数组都是关联型数组，也就是某些语言中的哈希表或字典

// 在所有PHP版本中均适用：
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);

// PHP 5.4 中引入了新的语法
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];

echo $associative['One']; // 输出 1

// 声明为列表实际上是给每个值都分配了一个整数键（key）
$array = ['One', 'Two', 'Three'];
echo $array[0]; // => "One"


/********************************
 * 输出
 */

echo('Hello World!');
// 输出到标准输出
// 此时标准输出就是浏览器中的网页

print('Hello World!'); // 和echo相同

// echo和print实际上也属于这个语言本身，所以我们省略括号
echo 'Hello World!';
print 'Hello World!'; 

$paragraph = 'paragraph';

echo 100;        // 直接输出标量
echo $paragraph; // 或者输出变量

// 如果你配置了短标签，或者使用5.4.0及以上的版本
// 你就可以使用简写的echo语法
?>
<p><?= $paragraph ?></p>
<?php

$x = 1;
$y = 2;
$x = $y; // $x 现在和 $y 的值相同
$z = &$y;
// $z 现在持有 $y 的引用. 现在更改 $z 的值也会更改 $y 的值，反之亦然
// 但是改变 $y 的值不会改变 $x 的值

echo $x; // => 2
echo $z; // => 2
$y = 0;
echo $x; // => 2
echo $z; // => 0


/********************************
 * 逻辑
 */
$a = 0;
$b = '0';
$c = '1';
$d = '1';

// 如果assert的参数为假，就会抛出警告

// 下面的比较都为真，不管它们的类型是否匹配
assert($a == $b); // 相等
assert($c != $a); // 不等
assert($c <> $a); // 另一种不等的表示
assert($a < $c);
assert($c > $b);
assert($a <= $b);
assert($c >= $d);

// 下面的比较只有在类型相同、值相同的情况下才为真
assert($c === $d);
assert($a !== $d);
assert(1 === '1');
assert(1 !== '1');

// 变量可以根据其使用来进行类型转换

$integer = 1;
echo $integer + $integer; // => 2

$string = '1';
echo $string + $string; // => 2 (字符串在此时被转化为整数)

$string = 'one';
echo $string + $string; // => 0
// 输出0，因为'one'这个字符串无法被转换为整数

// 类型转换可以将一个类型视作另一种类型

$boolean = (boolean) 1; // => true

$zero = 0;
$boolean = (boolean) $zero; // => false

// 还有一些专用的函数来进行类型转换
$integer = 5;
$string = strval($integer);

$var = null; // 空值


/********************************
 * 控制结构
 */

if (true) {
    print 'I get printed';
}

if (false) {
    print 'I don\'t';
} else {
    print 'I get printed';
}

if (false) {
    print 'Does not get printed';
} elseif(true) {
    print 'Does';
}

// 三目运算符
print (false ? 'Does not get printed' : 'Does');

$x = 0;
if ($x === '0') {
    print 'Does not print';
} elseif($x == '1') {
    print 'Does not print';
} else {
    print 'Does print';
}



// 下面的语法常用于模板中:
?>

<?php if ($x): ?>
This is displayed if the test is truthy.
<?php else: ?>
This is displayed otherwise.
<?php endif; ?>

<?php

// 用switch来实现相同的逻辑
switch ($x) {
    case '0':
        print 'Switch does type coercion';
        break; // 在case中必须使用一个break语句，
		       // 否则在执行完这个语句后会直接执行后面的语句
    case 'two':
    case 'three':
        // 如果$variable是 'two' 或 'three'，执行这里的语句
        break;
    default:
        // 其他情况
}

// While, do...while 和 for 循环
$i = 0;
while ($i < 5) {
    echo $i++;
}; // 输出 "01234"

echo "\n";

$i = 0;
do {
    echo $i++;
} while ($i < 5); // 输出 "01234"

echo "\n";

for ($x = 0; $x < 10; $x++) {
    echo $x;
} // 输出 "0123456789"

echo "\n";

$wheels = ['bicycle' => 2, 'car' => 4];

// Foreach 循环可以遍历数组
foreach ($wheels as $wheel_count) {
    echo $wheel_count;
} // 输出 "24"

echo "\n";

// 也可以同时遍历键和值
foreach ($wheels as $vehicle => $wheel_count) {
    echo "A $vehicle has $wheel_count wheels";
}

echo "\n";

$i = 0;
while ($i < 5) {
    if ($i === 3) {
        break; // 退出循环
    }
    echo $i++;
} // 输出 "012"

for ($i = 0; $i < 5; $i++) {
    if ($i === 3) {
        continue; // 跳过此次遍历
    }
    echo $i;
} // 输出 "0124"


/********************************
 * 函数
 */

// 通过"function"定义函数:
function my_function () {
  return 'Hello';
}

echo my_function(); // => "Hello"

// 函数名需要以字母或者下划线开头, 
// 后面可以跟着任意的字母、下划线、数字.

function add ($x, $y = 1) { // $y 是可选参数，默认值为 1
  $result = $x + $y;
  return $result;
}

echo add(4); // => 5
echo add(4, 2); // => 6

// $result 在函数外部不可访问
// print $result; // 抛出警告

// 从 PHP 5.3 起我们可以定义匿名函数
$inc = function ($x) {
  return $x + 1;
};

echo $inc(2); // => 3

function foo ($x, $y, $z) {
  echo "$x - $y - $z";
}

// 函数也可以返回一个函数
function bar ($x, $y) {
  // 用 'use' 将外部的参数引入到里面
  return function ($z) use ($x, $y) {
    foo($x, $y, $z);
  };
}

$bar = bar('A', 'B');
$bar('C'); // 输出 "A - B - C"

// 你也可以通过字符串调用函数
$function_name = 'add';
echo $function_name(1, 2); // => 3
// 在通过程序来决定调用哪个函数时很有用
// 或者，使用 call_user_func(callable $callback [, $parameter [, ... ]]);

/********************************
 * 导入
 */

<?php
// 被导入的php文件也必须以php开标签开始

include 'my-file.php';
// 现在my-file.php就在当前作用域中可见了
// 如果这个文件无法被导入（比如文件不存在），会抛出警告

include_once 'my-file.php';
// my-file.php中的代码在其他地方被导入了，那么就不会被再次导入
// 这会避免类的多重定义错误

require 'my-file.php';
require_once 'my-file.php';
// 和include功能相同，只不过如果不能被导入时，会抛出错误

// my-include.php的内容:
<?php

return 'Anything you like.';
// 文件结束

// Include和Require函数也有返回值
$value = include 'my-include.php';

// 被引入的文件是根据文件路径或者include_path配置来查找到的
// 如果文件最终没有被找到，那么就会查找当前文件夹。之后才会报错
/* */

/********************************
 * 类
 */

// 类是由class关键字定义的

class MyClass
{
    const MY_CONST      = 'value'; // 常量

    static $staticVar   = 'static';

    // 属性必须声明其作用域
    public $property    = 'public';
    public $instanceProp;
    protected $prot = 'protected'; // 当前类和子类可访问
    private $priv   = 'private';   // 仅当前类可访问

    // 通过 __construct 来定义构造函数
    public function __construct($instanceProp) {
        // 通过 $this 访问当前对象
        $this->instanceProp = $instanceProp;
    }

    // 方法就是类中定义的函数
    public function myMethod()
    {
        print 'MyClass';
    }

    final function youCannotOverrideMe()
    {
    }

    public static function myStaticMethod()
    {
        print 'I am static';
    }
}

echo MyClass::MY_CONST;    // 输出 'value';
echo MyClass::$staticVar;  // 输出 'static';
MyClass::myStaticMethod(); // 输出 'I am static';

// 通过new来新建实例
$my_class = new MyClass('An instance property');
// 如果不传递参数，那么括号可以省略

// 用 -> 来访问成员
echo $my_class->property;     // => "public"
echo $my_class->instanceProp; // => "An instance property"
$my_class->myMethod();        // => "MyClass"


// 使用extends来生成子类
class MyOtherClass extends MyClass
{
    function printProtectedProperty()
    {
        echo $this->prot;
    }

    // 方法覆盖
    function myMethod()
    {
        parent::myMethod();
        print ' > MyOtherClass';
    }
}

$my_other_class = new MyOtherClass('Instance prop');
$my_other_class->printProtectedProperty(); // => 输出 "protected"
$my_other_class->myMethod();               // 输出 "MyClass > MyOtherClass"

final class YouCannotExtendMe
{
}

// 你可以使用“魔法方法”来生成getter和setter方法
class MyMapClass
{
    private $property;

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }
}

$x = new MyMapClass();
echo $x->property; // 会使用 __get() 方法
$x->property = 'Something'; // 会使用 __set() 方法

// 类可以是被定义成抽象类 (使用 abstract 关键字) 或者
// 去实现接口 (使用 implements 关键字).
// 接口需要通过interface关键字来定义

interface InterfaceOne
{
    public function doSomething();
}

interface InterfaceTwo
{
    public function doSomethingElse();
}

// 接口可以被扩展
interface InterfaceThree extends InterfaceTwo
{
    public function doAnotherContract();
}

abstract class MyAbstractClass implements InterfaceOne
{
    public $x = 'doSomething';
}

class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
{
    public function doSomething()
    {
        echo $x;
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


// 一个类可以实现多个接口
class SomeOtherClass implements InterfaceOne, InterfaceTwo
{
    public function doSomething()
    {
        echo 'doSomething';
    }

    public function doSomethingElse()
    {
        echo 'doSomethingElse';
    }
}


/********************************
 * 特征
 */

// 特征 从 PHP 5.4.0 开始包括，需要用 "trait" 这个关键字声明

trait MyTrait
{
    public function myTraitMethod()
    {
        print 'I have MyTrait';
    }
}

class MyTraitfulClass
{
    use MyTrait;
}

$cls = new MyTraitfulClass();
$cls->myTraitMethod(); // 输出 "I have MyTrait"


/********************************
 * 命名空间
 */

// 这部分是独立于这个文件的
// 因为命名空间必须在一个文件的开始处。

<?php

// 类会被默认的放在全局命名空间中，可以被一个\来显式调用

$cls = new \MyClass();



// 为一个文件设置一个命名空间
namespace My\Namespace;

class MyClass
{
}

// (或者从其他文件中)
$cls = new My\Namespace\MyClass;

//或者从其他命名空间中
namespace My\Other\Namespace;

use My\Namespace\MyClass;

$cls = new MyClass();

// 你也可以为命名空间起一个别名

namespace My\Other\Namespace;

use My\Namespace as SomeOtherNamespace;

$cls = new SomeOtherNamespace\MyClass();

*/

```

## 更多阅读

访问 [PHP 官方文档](http://www.php.net/manual/) 

如果你对最佳实践感兴趣（实时更新） [PHP The Right Way](http://www.phptherightway.com/).

如果你很熟悉善于包管理的语言 [Composer](http://getcomposer.org/).

如要了解通用标准，请访问PHP Framework Interoperability Group's [PSR standards](https://github.com/php-fig/fig-standards).
---
language: python
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
translators:
    - ["Chenbo Li", "http://binarythink.net"]
filename: learnpython-zh.py
lang: zh-cn
---

Python 由 Guido Van Rossum 在90年代初创建。 它现在是最流行的语言之一
我喜爱python是因为它有极为清晰的语法，甚至可以说，它就是可以执行的伪代码

很欢迎来自您的反馈，你可以在[@louiedinh](http://twitter.com/louiedinh) 和 louiedinh [at] [google's email service] 这里找到我

注意: 这篇文章针对的版本是Python 2.7，但大多也可使用于其他Python 2的版本
如果是Python 3，请在网络上寻找其他教程

```python

# 单行注释
""" 多行字符串可以用
    三个引号包裹，不过这也可以被当做
    多行注释
"""

####################################################
## 1. 原始数据类型和操作符
####################################################

# 数字类型
3  # => 3

# 简单的算数
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20
35 / 5  # => 7

# 整数的除法会自动取整
5 / 2  # => 2

# 要做精确的除法，我们需要引入浮点数
2.0     # 浮点数
11.0 / 4.0  # => 2.75 精确多了

# 括号具有最高优先级
(1 + 3) * 2  # => 8

# 布尔值也是基本的数据类型
True
False

# 用 not 来取非
not True  # => False
not False  # => True

# 相等
1 == 1  # => True
2 == 1  # => False

# 不等
1 != 1  # => False
2 != 1  # => True

# 更多的比较操作符
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# 比较运算可以连起来写！
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# 字符串通过 " 或 ' 括起来
"This is a string."
'This is also a string.'

# 字符串通过加号拼接
"Hello " + "world!"  # => "Hello world!"

# 字符串可以被视为字符的列表
"This is a string"[0]  # => 'T'

# % 可以用来格式化字符串
"%s can be %s" % ("strings", "interpolated")

# 也可以用 format 方法来格式化字符串
# 推荐使用这个方法
"{0} can be {1}".format("strings", "formatted")
# 也可以用变量名代替数字
"{name} wants to eat {food}".format(name="Bob", food="lasagna")

# None 是对象
None  # => None

# 不要用相等 `==` 符号来和None进行比较
# 要用 `is`
"etc" is None  # => False
None is None  # => True

# 'is' 可以用来比较对象的相等性
# 这个操作符在比较原始数据时没多少用，但是比较对象时必不可少

# None, 0, 和空字符串都被算作 False
# 其他的均为 True
0 == False  # => True
"" == False  # => True


####################################################
## 2. 变量和集合
####################################################

# 很方便的输出
print "I'm Python. Nice to meet you!"


# 给变量赋值前不需要事先声明
some_var = 5    # 一般建议使用小写字母和下划线组合来做为变量名
some_var  # => 5

# 访问未赋值的变量会抛出异常
# 可以查看控制流程一节来了解如何异常处理
some_other_var  # 抛出 NameError

# if 语句可以作为表达式来使用
"yahoo!" if 3 > 2 else 2  # => "yahoo!"

# 列表用来保存序列
li = []
# 可以直接初始化列表
other_li = [4, 5, 6]

# 在列表末尾添加元素
li.append(1)    # li 现在是 [1]
li.append(2)    # li 现在是 [1, 2]
li.append(4)    # li 现在是 [1, 2, 4]
li.append(3)    # li 现在是 [1, 2, 4, 3]
# 移除列表末尾元素
li.pop()        # => 3 li 现在是 [1, 2, 4]
# 重新加进去
li.append(3)    # li is now [1, 2, 4, 3] again.

# 像其他语言访问数组一样访问列表
li[0]  # => 1
# 访问最后一个元素
li[-1]  # => 3

# 越界会抛出异常
li[4]  # 抛出越界异常

# 切片语法需要用到列表的索引访问
# 可以看做数学之中左闭右开区间
li[1:3]  # => [2, 4]
# 省略开头的元素
li[2:]  # => [4, 3]
# 省略末尾的元素
li[:3]  # => [1, 2, 4]

# 删除特定元素
del li[2]  # li 现在是 [1, 2, 3]

# 合并列表
li + other_li  # => [1, 2, 3, 4, 5, 6] - 并不会不改变这两个列表

# 通过拼接来合并列表
li.extend(other_li)  # li 是 [1, 2, 3, 4, 5, 6]

# 用 in 来返回元素是否在列表中
1 in li  # => True

# 返回列表长度
len(li)  # => 6


# 元组类似于列表，但它是不可改变的
tup = (1, 2, 3)
tup[0]  # => 1
tup[0] = 3  # 类型错误

# 对于大多数的列表操作，也适用于元组
len(tup)  # => 3
tup + (4, 5, 6)  # => (1, 2, 3, 4, 5, 6)
tup[:2]  # => (1, 2)
2 in tup  # => True

# 你可以将元组解包赋给多个变量
a, b, c = (1, 2, 3)     # a 是 1，b 是 2，c 是 3
# 如果不加括号，将会被自动视为元组
d, e, f = 4, 5, 6
# 现在我们可以看看交换两个数字是多么容易的事
e, d = d, e     # d 是 5，e 是 4


# 字典用来储存映射关系
empty_dict = {}
# 字典初始化
filled_dict = {"one": 1, "two": 2, "three": 3}

# 字典也用中括号访问元素
filled_dict["one"]  # => 1

# 把所有的键保存在列表中
filled_dict.keys()  # => ["three", "two", "one"]
# 键的顺序并不是唯一的，得到的不一定是这个顺序

# 把所有的值保存在列表中
filled_dict.values()  # => [3, 2, 1]
# 和键的顺序相同

# 判断一个键是否存在
"one" in filled_dict  # => True
1 in filled_dict  # => False

# 查询一个不存在的键会抛出 KeyError
filled_dict["four"]  # KeyError

# 用 get 方法来避免 KeyError
filled_dict.get("one")  # => 1
filled_dict.get("four")  # => None
# get 方法支持在不存在的时候返回一个默认值
filled_dict.get("one", 4)  # => 1
filled_dict.get("four", 4)  # => 4

# setdefault 是一个更安全的添加字典元素的方法
filled_dict.setdefault("five", 5)  # filled_dict["five"] 的值为 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] 的值仍然是 5


# 集合储存无顺序的元素
empty_set = set()
# 初始化一个集合
some_set = set([1, 2, 2, 3, 4])  # some_set 现在是 set([1, 2, 3, 4])

# Python 2.7 之后，大括号可以用来表示集合
filled_set = {1, 2, 2, 3, 4}  # => {1 2 3 4}

# 向集合添加元素
filled_set.add(5)  # filled_set 现在是 {1, 2, 3, 4, 5}

# 用 & 来计算集合的交
other_set = {3, 4, 5, 6}
filled_set & other_set  # => {3, 4, 5}

# 用 | 来计算集合的并
filled_set | other_set  # => {1, 2, 3, 4, 5, 6}

# 用 - 来计算集合的差
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# 用 in 来判断元素是否存在于集合中
2 in filled_set  # => True
10 in filled_set  # => False


####################################################
## 3. 控制流程
####################################################

# 新建一个变量
some_var = 5

# 这是个 if 语句，在 python 中缩进是很重要的。
# 下面的代码片段将会输出 "some var is smaller than 10"
if some_var > 10:
    print "some_var is totally bigger than 10."
elif some_var < 10:    # 这个 elif 语句是不必须的
    print "some_var is smaller than 10."
else:           # 这个 else 也不是必须的
    print "some_var is indeed 10."


"""
用for循环遍历列表
输出:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # 你可以用 % 来格式化字符串
    print "%s is a mammal" % animal

"""
`range(number)` 返回从0到给定数字的列表
输出:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
while 循环
输出:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  #  x = x + 1 的简写

# 用 try/except 块来处理异常

# Python 2.6 及以上适用:
try:
    # 用 raise 来抛出异常
    raise IndexError("This is an index error")
except IndexError as e:
    pass    # pass 就是什么都不做，不过通常这里会做一些恢复工作


####################################################
## 4. 函数
####################################################

# 用 def 来新建函数
def add(x, y):
    print "x is %s and y is %s" % (x, y)
    return x + y    # 通过 return 来返回值

# 调用带参数的函数
add(5, 6)  # => 输出 "x is 5 and y is 6" 返回 11

# 通过关键字赋值来调用函数
add(y=6, x=5)   # 顺序是无所谓的

# 我们也可以定义接受多个变量的函数，这些变量是按照顺序排列的
def varargs(*args):
    return args

varargs(1, 2, 3)  # => (1,2,3)


# 我们也可以定义接受多个变量的函数，这些变量是按照关键字排列的
def keyword_args(**kwargs):
    return kwargs

# 实际效果：
keyword_args(big="foot", loch="ness")  # => {"big": "foot", "loch": "ness"}

# 你也可以同时将一个函数定义成两种形式
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) prints:
    (1, 2)
    {"a": 3, "b": 4}
"""

# 当调用函数的时候，我们也可以进行相反的操作，把元组和字典展开为参数
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)  # 等价于 foo(1, 2, 3, 4)
all_the_args(**kwargs)  # 等价于 foo(a=3, b=4)
all_the_args(*args, **kwargs)  # 等价于 foo(1, 2, 3, 4, a=3, b=4)

# 函数在 python 中是一等公民
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)  # => 13

# 匿名函数
(lambda x: x > 2)(3)  # => True

# 内置高阶函数
map(add_10, [1, 2, 3])  # => [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7])  # => [6, 7]

# 可以用列表方法来对高阶函数进行更巧妙的引用
[add_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]  # => [6, 7]

####################################################
## 5. 类
####################################################

# 我们新建的类是从 object 类中继承的
class Human(object):

     # 类属性，由所有类的对象共享
    species = "H. sapiens"

    # 基本构造函数
    def __init__(self, name):
        # 将参数赋给对象成员属性
        self.name = name

    # 成员方法，参数要有 self
    def say(self, msg):
        return "%s: %s" % (self.name, msg)

    # 类方法由所有类的对象共享
    # 这类方法在调用时，会把类本身传给第一个参数
    @classmethod
    def get_species(cls):
        return cls.species

    # 静态方法是不需要类和对象的引用就可以调用的方法
    @staticmethod
    def grunt():
        return "*grunt*"


# 实例化一个类
i = Human(name="Ian")
print i.say("hi")     # 输出 "Ian: hi"

j = Human("Joel")
print j.say("hello")  # 输出 "Joel: hello"

# 访问类的方法
i.get_species()  # => "H. sapiens"

# 改变共享属性
Human.species = "H. neanderthalensis"
i.get_species()  # => "H. neanderthalensis"
j.get_species()  # => "H. neanderthalensis"

# 访问静态变量
Human.grunt()  # => "*grunt*"


####################################################
## 6. 模块
####################################################

# 我们可以导入其他模块
import math
print math.sqrt(16)  # => 4

# 我们也可以从一个模块中导入特定的函数
from math import ceil, floor
print ceil(3.7)   # => 4.0
print floor(3.7)  # => 3.0

# 从模块中导入所有的函数
# 警告：不推荐使用
from math import *

# 简写模块名
import math as m
math.sqrt(16) == m.sqrt(16)  # => True

# Python的模块其实只是普通的python文件
# 你也可以创建自己的模块，并且导入它们
# 模块的名字就和文件的名字相同

# 也可以通过下面的方法查看模块中有什么属性和方法
import math
dir(math)


```

## 更多阅读

希望学到更多？试试下面的链接：

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2.6/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)
---
language: python3
contributors:
    - ["Louie Dinh", "http://pythonpracticeprojects.com"]
    - ["Steven Basart", "http://github.com/xksteven"]
    - ["Andre Polykanine", "https://github.com/Oire"]
translators:
    - ["Geoff Liu", "http://geoffliu.me"]
filename: learnpython3-cn.py
lang: zh-cn
---

Python是由吉多·范罗苏姆(Guido Van Rossum)在90年代早期设计。它是如今最常用的编程
语言之一。它的语法简洁且优美，几乎就是可执行的伪代码。

欢迎大家斧正。英文版原作Louie Dinh [@louiedinh](http://twitter.com/louiedinh)
或着Email louiedinh [at] [谷歌的信箱服务]。中文翻译Geoff Liu。

注意：这篇教程是特别为Python3写的。如果你想学旧版Python2，我们特别有另一篇教程。

```python

# 用井字符开头的是单行注释

""" 多行字符串用三个引号
    包裹，也常被用来做多
    行注释
"""

####################################################
## 1. 原始数据类型和运算符
####################################################

# 整数
3  # => 3

# 算术没有什么出乎意料的
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20

# 但是除法例外，会自动转换成浮点数
35 / 5  # => 7.0
5 / 3  # => 1.6666666666666667

# 整数除法的结果都是向下取整
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # 浮点数也可以
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# 浮点数的运算结果也是浮点数
3 * 2.0 # => 6.0

# 模除
7 % 3 # => 1

# x的y次方
2**4 # => 16

# 用括号决定优先级
(1 + 3) * 2  # => 8

# 布尔值
True
False

# 用not取非
not True  # => False
not False  # => True

# 逻辑运算符，注意and和or都是小写
True and False #=> False
False or True #=> True

# 整数也可以当作布尔值
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# 用==判断相等
1 == 1  # => True
2 == 1  # => False

# 用!=判断不等
1 != 1  # => False
2 != 1  # => True

# 比较大小
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# 大小比较可以连起来！
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# 字符串用单引双引都可以
"这是个字符串"
'这也是个字符串'

# 用加号连接字符串
"Hello " + "world!"  # => "Hello world!"

# 字符串可以被当作字符列表
"This is a string"[0]  # => 'T'

# 用.format来格式化字符串
"{} can be {}".format("strings", "interpolated")

# 可以重复参数以节省时间
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
#=> "Jack be nimble, Jack be quick, Jack jump over the candle stick"

# 如果不想数参数，可以用关键字
"{name} wants to eat {food}".format(name="Bob", food="lasagna") #=> "Bob wants to eat lasagna"

# 如果你的Python3程序也要在Python2.5以下环境运行，也可以用老式的格式化语法
"%s can be %s the %s way" % ("strings", "interpolated", "old")

# None是一个对象
None  # => None

# 当与None进行比较时不要用 ==，要用is。is是用来比较两个变量是否指向同一个对象。
"etc" is None  # => False
None is None  # => True

# None，0，空字符串，空列表，空字典都算是False
# 所有其他值都是True
bool(0)  # => False
bool("")  # => False
bool([]) #=> False
bool({}) #=> False


####################################################
## 2. 变量和集合
####################################################

# print是内置的打印函数
print("I'm Python. Nice to meet you!")

# 在给变量赋值前不用提前声明
# 传统的变量命名是小写，用下划线分隔单词
some_var = 5
some_var  # => 5

# 访问未赋值的变量会抛出异常
# 参考流程控制一段来学习异常处理
some_unknown_var  # 抛出NameError

# 用列表(list)储存序列
li = []
# 创建列表时也可以同时赋给元素
other_li = [4, 5, 6]

# 用append在列表最后追加元素
li.append(1)    # li现在是[1]
li.append(2)    # li现在是[1, 2]
li.append(4)    # li现在是[1, 2, 4]
li.append(3)    # li现在是[1, 2, 4, 3]
# 用pop从列表尾部删除
li.pop()        # => 3 且li现在是[1, 2, 4]
# 把3再放回去
li.append(3)    # li变回[1, 2, 4, 3]

# 列表存取跟数组一样
li[0]  # => 1
# 取出最后一个元素
li[-1]  # => 3

# 越界存取会造成IndexError
li[4]  # 抛出IndexError

# 列表有切割语法
li[1:3]  # => [2, 4]
# 取尾
li[2:]  # => [4, 3]
# 取头
li[:3]  # => [1, 2, 4]
# 隔一个取一个
li[::2]   # =>[1, 4]
# 倒排列表
li[::-1]   # => [3, 4, 2, 1]
# 可以用三个参数的任何组合来构建切割
# li[始:终:步伐]

# 用del删除任何一个元素
del li[2]   # li is now [1, 2, 3]

# 列表可以相加
# 注意：li和other_li的值都不变
li + other_li   # => [1, 2, 3, 4, 5, 6]

# 用extend拼接列表
li.extend(other_li)   # li现在是[1, 2, 3, 4, 5, 6]

# 用in测试列表是否包含值
1 in li   # => True

# 用len取列表长度
len(li)   # => 6


# 元组是不可改变的序列
tup = (1, 2, 3)
tup[0]   # => 1
tup[0] = 3  # 抛出TypeError

# 列表允许的操作元组大都可以
len(tup)   # => 3
tup + (4, 5, 6)   # => (1, 2, 3, 4, 5, 6)
tup[:2]   # => (1, 2)
2 in tup   # => True

# 可以把元组合列表解包，赋值给变量
a, b, c = (1, 2, 3)     # 现在a是1，b是2，c是3
# 元组周围的括号是可以省略的
d, e, f = 4, 5, 6
# 交换两个变量的值就这么简单
e, d = d, e     # 现在d是5，e是4


# 用字典表达映射关系
empty_dict = {}
# 初始化的字典
filled_dict = {"one": 1, "two": 2, "three": 3}

# 用[]取值
filled_dict["one"]   # => 1


# 用keys获得所有的键。因为keys返回一个可迭代对象，所以在这里把结果包在list里。我们下面会详细介绍可迭代。
# 注意：字典键的顺序是不定的，你得到的结果可能和以下不同。
list(filled_dict.keys())   # => ["three", "two", "one"]


# 用values获得所有的值。跟keys一样，要用list包起来，顺序也可能不同。
list(filled_dict.values())   # => [3, 2, 1]


# 用in测试一个字典是否包含一个键
"one" in filled_dict   # => True
1 in filled_dict   # => False

# 访问不存在的键会导致KeyError
filled_dict["four"]   # KeyError

# 用get来避免KeyError
filled_dict.get("one")   # => 1
filled_dict.get("four")   # => None
# 当键不存在的时候get方法可以返回默认值
filled_dict.get("one", 4)   # => 1
filled_dict.get("four", 4)   # => 4

# setdefault方法只有当键不存在的时候插入新值
filled_dict.setdefault("five", 5)  # filled_dict["five"]设为5
filled_dict.setdefault("five", 6)  # filled_dict["five"]还是5

# 字典赋值
filled_dict.update({"four":4}) #=> {"one": 1, "two": 2, "three": 3, "four": 4}
filled_dict["four"] = 4  # 另一种赋值方法

# 用del删除
del filled_dict["one"]  # 从filled_dict中把one删除


# 用set表达集合
empty_set = set()
# 初始化一个集合，语法跟字典相似。
some_set = {1, 1, 2, 2, 3, 4}   # some_set现在是{1, 2, 3, 4}

# 可以把集合赋值于变量
filled_set = some_set

# 为集合添加元素
filled_set.add(5)   # filled_set现在是{1, 2, 3, 4, 5}

# & 取交集
other_set = {3, 4, 5, 6}
filled_set & other_set   # => {3, 4, 5}

# | 取并集
filled_set | other_set   # => {1, 2, 3, 4, 5, 6}

# - 取补集
{1, 2, 3, 4} - {2, 3, 5}   # => {1, 4}

# in 测试集合是否包含元素
2 in filled_set   # => True
10 in filled_set   # => False


####################################################
## 3. 流程控制和迭代器
####################################################

# 先随便定义一个变量
some_var = 5

# 这是个if语句。注意缩进在Python里是有意义的
# 印出"some_var比10小"
if some_var > 10:
    print("some_var比10大")
elif some_var < 10:    # elif句是可选的
    print("some_var比10小")
else:                  # else也是可选的
    print("some_var就是10")


"""
用for循环语句遍历列表
打印:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    print("{} is a mammal".format(animal))

"""
"range(number)"返回数字列表从0到给的数字
打印:
    0
    1
    2
    3
"""
for i in range(4):
    print(i)

"""
while循环直到条件不满足
打印:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print(x)
    x += 1  # x = x + 1 的简写

# 用try/except块处理异常状况
try:
    # 用raise抛出异常
    raise IndexError("This is an index error")
except IndexError as e:
    pass    # pass是无操作，但是应该在这里处理错误
except (TypeError, NameError):
    pass    # 可以同时处理不同类的错误
else:   # else语句是可选的，必须在所有的except之后
    print("All good!")   # 只有当try运行完没有错误的时候这句才会运行


# Python提供一个叫做可迭代(iterable)的基本抽象。一个可迭代对象是可以被当作序列
# 的对象。比如说上面range返回的对象就是可迭代的。

filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable) # => range(1,10) 是一个实现可迭代接口的对象

# 可迭代对象可以遍历
for i in our_iterable:
    print(i)    # 打印 one, two, three

# 但是不可以随机访问
our_iterable[1]  # 抛出TypeError

# 可迭代对象知道怎么生成迭代器
our_iterator = iter(our_iterable)

# 迭代器是一个可以记住遍历的位置的对象
# 用__next__可以取得下一个元素
our_iterator.__next__()  #=> "one"

# 再一次调取__next__时会记得位置
our_iterator.__next__()  #=> "two"
our_iterator.__next__()  #=> "three"

# 当迭代器所有元素都取出后，会抛出StopIteration
our_iterator.__next__() # 抛出StopIteration

# 可以用list一次取出迭代器所有的元素
list(filled_dict.keys())  #=> Returns ["one", "two", "three"]



####################################################
## 4. 函数
####################################################

# 用def定义新函数
def add(x, y):
    print("x is {} and y is {}".format(x, y))
    return x + y    # 用return语句返回

# 调用函数
add(5, 6)   # => 印出"x is 5 and y is 6"并且返回11

# 也可以用关键字参数来调用函数
add(y=6, x=5)   # 关键字参数可以用任何顺序


# 我们可以定义一个可变参数函数
def varargs(*args):
    return args

varargs(1, 2, 3)   # => (1, 2, 3)


# 我们也可以定义一个关键字可变参数函数
def keyword_args(**kwargs):
    return kwargs

# 我们来看看结果是什么：
keyword_args(big="foot", loch="ness")   # => {"big": "foot", "loch": "ness"}


# 这两种可变参数可以混着用
def all_the_args(*args, **kwargs):
    print(args)
    print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) prints:
    (1, 2)
    {"a": 3, "b": 4}
"""

# 调用可变参数函数时可以做跟上面相反的，用*展开序列，用**展开字典。
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)   # 相当于 foo(1, 2, 3, 4)
all_the_args(**kwargs)   # 相当于 foo(a=3, b=4)
all_the_args(*args, **kwargs)   # 相当于 foo(1, 2, 3, 4, a=3, b=4)


# 函数作用域
x = 5

def setX(num):
    # 局部作用域的x和全局域的x是不同的
    x = num # => 43
    print (x) # => 43

def setGlobalX(num):
    global x
    print (x) # => 5
    x = num # 现在全局域的x被赋值
    print (x) # => 6

setX(43)
setGlobalX(6)


# 函数在Python是一等公民
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)   # => 13

# 也有匿名函数
(lambda x: x > 2)(3)   # => True

# 内置的高阶函数
map(add_10, [1, 2, 3])   # => [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]

# 用列表推导式可以简化映射和过滤。列表推导式的返回值是另一个列表。
[add_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]   # => [6, 7]

####################################################
## 5. 类
####################################################


# 定义一个继承object的类
class Human(object):

    # 类属性，被所有此类的实例共用。
    species = "H. sapiens"

    # 构造方法，当实例被初始化时被调用。注意名字前后的双下划线，这是表明这个属
    # 性或方法对Python有特殊意义，但是允许用户自行定义。你自己取名时不应该用这
    # 种格式。
    def __init__(self, name):
        # Assign the argument to the instance's name attribute
        self.name = name

    # 实例方法，第一个参数总是self，就是这个实例对象
    def say(self, msg):
        return "{name}: {message}".format(name=self.name, message=msg)

    # 类方法，被所有此类的实例共用。第一个参数是这个类对象。
    @classmethod
    def get_species(cls):
        return cls.species

    # 静态方法。调用时没有实例或类的绑定。
    @staticmethod
    def grunt():
        return "*grunt*"


# 构造一个实例
i = Human(name="Ian")
print(i.say("hi"))     # 印出 "Ian: hi"

j = Human("Joel")
print(j.say("hello"))  # 印出 "Joel: hello"

# 调用一个类方法
i.get_species()   # => "H. sapiens"

# 改一个共用的类属性
Human.species = "H. neanderthalensis"
i.get_species()   # => "H. neanderthalensis"
j.get_species()   # => "H. neanderthalensis"

# 调用静态方法
Human.grunt()   # => "*grunt*"


####################################################
## 6. 模块
####################################################

# 用import导入模块
import math
print(math.sqrt(16))  # => 4.0

# 也可以从模块中导入个别值
from math import ceil, floor
print(ceil(3.7))  # => 4.0
print(floor(3.7))   # => 3.0

# 可以导入一个模块中所有值
# 警告：不建议这么做
from math import *

# 如此缩写模块名字
import math as m
math.sqrt(16) == m.sqrt(16)   # => True

# Python模块其实就是普通的Python文件。你可以自己写，然后导入，
# 模块的名字就是文件的名字。

# 你可以这样列出一个模块里所有的值
import math
dir(math)


####################################################
## 7. 高级用法
####################################################

# 用生成器(generators)方便地写惰性运算
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# 生成器只有在需要时才计算下一个值。它们每一次循环只生成一个值，而不是把所有的
# 值全部算好。这意味着double_numbers不会生成大于15的数字。
#
# range的返回值也是一个生成器，不然一个1到900000000的列表会花很多时间和内存。
#
# 如果你想用一个Python的关键字当作变量名，可以加一个下划线来区分。
range_ = range(1, 900000000)
# 当找到一个 >=30 的结果就会停
for i in double_numbers(range_):
    print(i)
    if i >= 30:
        break


# 装饰器(decorators)
# 这个例子中，beg装饰say
# beg会先调用say。如果返回的say_please为真，beg会改变返回的字符串。
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Please! I am poor :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Can you buy me a beer?"
    return msg, say_please


print(say())  # Can you buy me a beer?
print(say(say_please=True))  # Can you buy me a beer? Please! I am poor :(
```

## 想继续学吗？

### 线上免费材料（英文）

* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [Ideas for Python Projects](http://pythonpracticeprojects.com)

* [The Official Docs](http://docs.python.org/3/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/3/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)

### 书籍（也是英文）

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)

---
language: R
contributors:
    - ["e99n09", "http://github.com/e99n09"]
    - ["isomorphismes", "http://twitter.com/isomorphisms"]
translators:
    - ["小柒", "http://weibo.com/u/2328126220"]
    - ["alswl", "https://github.com/alswl"]
filename: learnr-zh.r
lang: zh-cn
---

R 是一门统计语言。它有很多数据分析和挖掘程序包。可以用来统计、分析和制图。
你也可以在 LaTeX 文档中运行 `R` 命令。

```r
# 评论以 # 开始

# R 语言原生不支持 多行注释
# 但是你可以像这样来多行注释

# 在窗口里按回车键可以执行一条命令


###################################################################
# 不用懂编程就可以开始动手了
###################################################################

data()	# 浏览内建的数据集
data(rivers)	# 北美主要河流的长度（数据集）
ls()	# 在工作空间中查看「河流」是否出现
head(rivers)	# 撇一眼数据集
# 735 320 325 392 524 450
length(rivers)	# 我们测量了多少条河流？
# 141
summary(rivers)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#  135.0   310.0   425.0   591.2   680.0  3710.0
stem(rivers)	# 茎叶图（一种类似于直方图的展现形式）
#
#  The decimal point is 2 digit(s) to the right of the |
#
#   0 | 4
#   2 | 011223334555566667778888899900001111223333344455555666688888999
#   4 | 111222333445566779001233344567
#   6 | 000112233578012234468
#   8 | 045790018
#  10 | 04507
#  12 | 1471
#  14 | 56
#  16 | 7
#  18 | 9
#  20 |
#  22 | 25
#  24 | 3
#  26 |
#  28 |
#  30 |
#  32 |
#  34 |
#  36 | 1


stem(log(rivers))	# 查看数据集的方式既不是标准形式，也不是取log后的结果! 看起来，是钟形曲线形式的基本数据集

#  The decimal point is 1 digit(s) to the left of the |
#
#  48 | 1
#  50 |
#  52 | 15578
#  54 | 44571222466689
#  56 | 023334677000124455789
#  58 | 00122366666999933445777
#  60 | 122445567800133459
#  62 | 112666799035
#  64 | 00011334581257889
#  66 | 003683579
#  68 | 0019156
#  70 | 079357
#  72 | 89
#  74 | 84
#  76 | 56
#  78 | 4
#  80 |
#  82 | 2


hist(rivers, col="#333333", border="white", breaks=25)	# 试试用这些参数画画 （译者注：给 river 做统计频数直方图，包含了这些参数：数据源，颜色，边框，空格）
hist(log(rivers), col="#333333", border="white", breaks=25)	#你还可以做更多式样的绘图

# 还有其他一些简单的数据集可以被用来加载。R 语言包括了大量这种 data()
data(discoveries)
plot(discoveries, col="#333333", lwd=3, xlab="Year", main="Number of important discoveries per year")
# 译者注：参数为（数据源，颜色，线条宽度，X 轴名称，标题）
plot(discoveries, col="#333333", lwd=3, type = "h", xlab="Year", main="Number of important discoveries per year")


# 除了按照默认的年份排序，我们还可以排序来发现特征
sort(discoveries)
#  [1]  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2
# [26]  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3
# [51]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  4  4  4  4  4  4  4
# [76]  4  4  4  4  5  5  5  5  5  5  5  6  6  6  6  6  6  7  7  7  7  8  9 10 12

stem(discoveries, scale=2) # 译者注：茎叶图（数据，放大系数）
#
#  The decimal point is at the |
#
#   0 | 000000000
#   1 | 000000000000
#   2 | 00000000000000000000000000
#   3 | 00000000000000000000
#   4 | 000000000000
#   5 | 0000000
#   6 | 000000
#   7 | 0000
#   8 | 0
#   9 | 0
#  10 | 0
#  11 |
#  12 | 0

max(discoveries)
# 12

summary(discoveries)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#    0.0     2.0     3.0     3.1     4.0    12.0




#基本的统计学操作也不需要任何编程知识

#随机生成数据
round(runif(7, min=.5, max=6.5))
# 译者注：runif 产生随机数，round 四舍五入
# 1 4 6 1 4 6 4

# 你输出的结果会和我们给出的不同，除非我们设置了相同的随机种子 random.seed(31337)


#从标准高斯函数中随机生成 9 次
rnorm(9)
# [1]  0.07528471  1.03499859  1.34809556 -0.82356087  0.61638975 -1.88757271
# [7] -0.59975593  0.57629164  1.08455362









#########################
# 基础编程
#########################

# 数值

#“数值”指的是双精度的浮点数
5	# 5
class(5)	# "numeric"
5e4	# 50000				# 用科学技术法方便的处理极大值、极小值或者可变的量级
6.02e23	# 阿伏伽德罗常数#
1.6e-35	# 布朗克长度

# 长整数并用 L 结尾
5L	# 5
#输出5L
class(5L)	# "integer"

# 可以自己试一试？用 class() 函数获取更多信息
# 事实上，你可以找一些文件查阅 `xyz` 以及xyz的差别
# `xyz` 用来查看源码实现，?xyz 用来看帮助

# 算法
10 + 66	# 76
53.2 - 4	# 49.2
2 * 2.0	# 4
3L / 4	# 0.75
3 %% 2	# 1

# 特殊数值类型
class(NaN)	# "numeric"
class(Inf)	# "numeric"
class(-Inf)	# "numeric"		# 在以下场景中会用到 integrate( dnorm(x), 3, Inf ) -- 消除 Z 轴数据

# 但要注意，NaN 并不是唯一的特殊数值类型……
class(NA)	# 看上面
class(NULL)	# NULL


# 简单列表
c(6, 8, 7, 5, 3, 0, 9)	# 6 8 7 5 3 0 9
c('alef', 'bet', 'gimmel', 'dalet', 'he')
c('Z', 'o', 'r', 'o') == "Zoro"	# FALSE FALSE FALSE FALSE

# 一些优雅的内置功能
5:15	# 5  6  7  8  9 10 11 12 13 14 15

seq(from=0, to=31337, by=1337)
#  [1]     0  1337  2674  4011  5348  6685  8022  9359 10696 12033 13370 14707
# [13] 16044 17381 18718 20055 21392 22729 24066 25403 26740 28077 29414 30751

letters
#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [20] "t" "u" "v" "w" "x" "y" "z"

month.abb	# "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"


# Access the n'th element of a list with list.name[n] or sometimes list.name[[n]]
# 使用 list.name[n] 来访问第 n 个列表元素，有时候需要使用 list.name[[n]]
letters[18]	# "r"
LETTERS[13]	# "M"
month.name[9]	# "September"
c(6, 8, 7, 5, 3, 0, 9)[3]	# 7



# 字符串

# 字符串和字符在 R 语言中没有区别
"Horatio"	# "Horatio"
class("Horatio") # "character"
substr("Fortuna multis dat nimis, nulli satis.", 9, 15)	# "multis "
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.")	# "Fortøna møltis dat nimis, nølli satis."



# 逻辑值

# 布尔值
class(TRUE)	# "logical"
class(FALSE)	# "logical"
# 和我们预想的一样
TRUE == TRUE	# TRUE
TRUE == FALSE	# FALSE
FALSE != FALSE	# FALSE
FALSE != TRUE	# TRUE
# 缺失数据（NA）也是逻辑值
class(NA)	# "logical"
#定义NA为逻辑型



# 因子
# 因子是为数据分类排序设计的（像是排序小朋友们的年级或性别）
levels(factor(c("female", "male", "male", "female", "NA", "female")))	# "female" "male"   "NA"

factor(c("female", "female", "male", "NA", "female"))
#  female female male   NA     female
# Levels: female male NA

data(infert)	# 自然以及引产导致的不育症
levels(infert$education)	# "0-5yrs"  "6-11yrs" "12+ yrs"



# 变量

# 有许多种方式用来赋值
x = 5 # 这样可以
y <- "1" # 更推荐这样
TRUE -> z # 这样可行，但是很怪

#我们还可以使用强制转型
as.numeric(y)	# 1
as.character(x)	# "5"

# 循环

# for 循环语句
for (i in 1:4) {
  print(i)
}

# while 循环
a <- 10
while (a > 4) {
	cat(a, "...", sep = "")
	a <- a - 1
}

# 记住，在 R 语言中 for / while 循环都很慢
# 建议使用 apply()（我们一会介绍）来操作一串数据（比如一列或者一行数据）

# IF/ELSE

# 再来看这些优雅的标准
if (4 > 3) {
	print("Huzzah! It worked!")
} else {
	print("Noooo! This is blatantly illogical!")
}

# =>
# [1] "Huzzah! It worked!"

# 函数

# 定义如下
jiggle <- function(x) {
	x = x + rnorm(1, sd=.1)	# 添加一点（正态）波动	
	return(x)
}

# 和其他 R 语言函数一样调用
jiggle(5)	# 5±ε. 使用 set.seed(2716057) 后， jiggle(5)==5.005043

#########################
# 数据容器：vectors, matrices, data frames, and arrays
#########################

# 单维度
# 你可以将目前我们学习到的任何类型矢量化，只要它们拥有相同的类型
vec <- c(8, 9, 10, 11)
vec	#  8  9 10 11
# 矢量的类型是这一组数据元素的类型
class(vec)	# "numeric"
# If you vectorize items of different classes, weird coercions happen
#如果你强制的将不同类型数值矢量化，会出现特殊值
c(TRUE, 4)	# 1 4
c("dog", TRUE, 4)	# "dog"  "TRUE" "4"

#我们这样来取内部数据，（R 的下标索引顺序 1 开始）
vec[1]	# 8
# 我们可以根据条件查找特定数据
which(vec %% 2 == 0)	# 1 3
# 抓取矢量中第一个和最后一个字符
head(vec, 1)	# 8
tail(vec, 1)	# 11
#如果下标溢出或不存会得到 NA
vec[6]	# NA
# 你可以使用 length() 获取矢量的长度
length(vec)	# 4

# 你可以直接操作矢量或者矢量的子集
vec * 4	# 16 20 24 28
vec[2:3] * 5	# 25 30
# 这里有许多内置的函数，来表现向量
mean(vec)	# 9.5
var(vec)	# 1.666667
sd(vec)	# 1.290994
max(vec)	# 11
min(vec)	# 8
sum(vec)	# 38

# 二维（相同元素类型）

#你可以为同样类型的变量建立矩阵
mat <- matrix(nrow = 3, ncol = 2, c(1,2,3,4,5,6))
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# 和 vector 不一样的是，一个矩阵的类型真的是 「matrix」，而不是内部元素的类型
class(mat) # => "matrix"
# 访问第一行的字符
mat[1,]	# 1 4
# 操作第一行数据
3 * mat[,1]	# 3 6 9
# 访问一个特定数据
mat[3,2]	# 6
# 转置整个矩阵（译者注：变成 2 行 3 列）
t(mat)
# =>
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6

# 使用 cbind() 函数把两个矩阵按列合并，形成新的矩阵
mat2 <- cbind(1:4, c("dog", "cat", "bird", "dog"))
mat2
# =>
#      [,1] [,2]
# [1,] "1"  "dog"
# [2,] "2"  "cat"
# [3,] "3"  "bird"
# [4,] "4"  "dog"
class(mat2)	# matrix
# Again, note what happened!
# 注意
# 因为矩阵内部元素必须包含同样的类型
# 所以现在每一个元素都转化成字符串
c(class(mat2[,1]), class(mat2[,2]))

# 按行合并两个向量，建立新的矩阵
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    4    5
# [2,]    6    7    0    4
# 哈哈，数据类型都一样的，没有发生强制转换，生活真美好

# 二维(不同的元素类型)

# 利用 data frame 可以将不同类型数据放在一起
dat <- data.frame(c(5,2,1,4), c("dog", "cat", "bird", "dog"))
names(dat) <- c("number", "species") # 给数据列命名
class(dat)	# "data.frame"
dat
# =>
#   number species
# 1      5     dog
# 2      2     cat
# 3      1    bird
# 4      4     dog
class(dat$number)	# "numeric"
class(dat[,2])	# "factor"
# data.frame() 会将字符向量转换为 factor 向量

# 有很多精妙的方法来获取 data frame 的子数据集
dat$number	# 5 2 1 4
dat[,1]	# 5 2 1 4
dat[,"number"]	# 5 2 1 4

# 多维（相同元素类型）

# 使用 arry 创造一个 n 维的表格
# You can make a two-dimensional table (sort of like a matrix)
# 你可以建立一个 2 维表格（有点像矩阵）
array(c(c(1,2,4,5),c(8,9,3,6)), dim=c(2,4))
# =>
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    8    3
# [2,]    2    5    9    6
#你也可以利用数组建立一个三维的矩阵
array(c(c(c(2,300,4),c(8,9,0)),c(c(5,60,0),c(66,7,847))), dim=c(3,2,2))
# =>
# , , 1
#
#      [,1] [,2]
# [1,]    2    8
# [2,]  300    9
# [3,]    4    0
#
# , , 2
#
#      [,1] [,2]
# [1,]    5   66
# [2,]   60    7
# [3,]    0  847

#列表（多维的，不同类型的）

# R语言有列表的形式
list1 <- list(time = 1:40)
list1$price = c(rnorm(40,.5*list1$time,4)) # 随机
list1

# You can get items in the list like so
# 你可以这样获得列表的元素
list1$time
# You can subset list items like vectors
# 你也可以和矢量一样获取他们的子集
list1$price[4]

#########################
# apply()函数家族
#########################

# 还记得 mat 么？
mat
# =>
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
# Use apply(X, MARGIN, FUN) to apply function FUN to a matrix X
# 使用(X, MARGIN, FUN)将函数 FUN 应用到矩阵 X 的行 (MAR = 1) 或者 列 (MAR = 2)
# That is, R does FUN to each row (or column) of X, much faster than a
# R 在 X 的每一行/列使用 FUN，比循环要快很多
apply(mat, MAR = 2, myFunc)
# =>
#      [,1] [,2]
# [1,]    3   15
# [2,]    7   19
# [3,]   11   23
# 还有其他家族函数 ?lapply, ?sapply

# 不要被吓到，虽然许多人在此都被搞混
# plyr 程序包的作用是用来改进 apply() 函数家族

install.packages("plyr")
require(plyr)
?plyr

#########################
# 载入数据
#########################

# "pets.csv" 是网上的一个文本
pets <- read.csv("http://learnxinyminutes.com/docs/pets.csv")
pets
head(pets, 2) # 前两行
tail(pets, 1) # 最后一行

# 以 .csv 格式来保存数据集或者矩阵
write.csv(pets, "pets2.csv") # 保存到新的文件 pets2.csv
# set working directory with setwd(), look it up with getwd()
# 使用 setwd() 改变工作目录，使用 getwd() 查看当前工作目录

# 尝试使用 ?read.csv 和 ?write.csv 来查看更多信息

#########################
# 画图
#########################

# 散点图
plot(list1$time, list1$price, main = "fake data") # 译者注：横轴 list1$time，纵轴 wlist1$price，标题 fake data
# 回归图
linearModel <- lm(price  ~ time, data = list1) # 译者注：线性模型，数据集为list1，以价格对时间做相关分析模型
linearModel # 拟合结果
# 将拟合结果展示在图上，颜色设为红色
abline(linearModel, col = "red")
# 也可以获取各种各样漂亮的分析图
plot(linearModel)

# 直方图
hist(rpois(n = 10000, lambda = 5), col = "thistle") # 译者注：统计频数直方图

# 柱状图
barplot(c(1,4,5,1,2), names.arg = c("red","blue","purple","green","yellow"))

# 可以尝试着使用 ggplot2 程序包来美化图片
install.packages("ggplot2")
require(ggplot2)
?ggplot2

```

## 获得 R

* 从 [http://www.r-project.org/](http://www.r-project.org/) 获得安装包和图形化界面
* [RStudio](http://www.rstudio.com/ide/) 是另一个图形化界面
---

language: racket
lang: zh-cn
filename: learnracket-zh.rkt
contributors:
  - ["th3rac25", "https://github.com/voila"]
  - ["Eli Barzilay", "https://github.com/elibarzilay"]
  - ["Gustavo Schmidt", "https://github.com/gustavoschmidt"]
translators:
    - ["lyuehh", "https://github.com/lyuehh"]
---

Racket是Lisp/Scheme家族中的一个通用的，多范式的编程语言。
非常期待您的反馈！你可以通过[@th3rac25](http://twitter.com/th3rac25)或以用户名为 th3rac25 的Google邮箱服务和我取得联系

```racket
#lang racket ; 声明我们使用的语言

;;; 注释

;; 单行注释以分号开始

#| 块注释
   可以横跨很多行而且...
    #|
       可以嵌套
    |#
|#

;; S表达式注释忽略剩下的表达式
;; 在调试的时候会非常有用
#; (被忽略的表达式)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. 原始数据类型和操作符
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 数字
9999999999999999999999 ; 整数
#b111                  ; 二进制数字 => 7
#o111                  ; 八进制数字 => 73
#x111                  ; 十六进制数字 => 273
3.14                   ; 实数
6.02e+23
1/2                    ; 有理数
1+2i                   ; 复数

;; 函数调用写作(f x y z ...)
;; 在这里 f 是一个函数， x, y, z, ... 是参数
;; 如果你想创建一个列表数据的字面量, 使用 ' 来阻止它们
;; 被求值
'(+ 1 2) ; => (+ 1 2)
;; 接下来，是一些数学运算
(+ 1 1)  ; => 2
(- 8 1)  ; => 7
(* 10 2) ; => 20
(expt 2 3) ; => 8
(quotient 5 2) ; => 2
(remainder 5 2) ; => 1
(/ 35 5) ; => 7
(/ 1 3) ; => 1/3
(exact->inexact 1/3) ; => 0.3333333333333333
(+ 1+2i  2-3i) ; => 3-1i

;;; 布尔类型
#t ; 为真
#f ; 为假，#f 之外的任何值都是真
(not #t) ; => #f
(and 0 #f (error "doesn't get here")) ; => #f
(or #f 0 (error "doesn't get here"))  ; => 0

;;; 字符
#\A ; => #\A
#\λ ; => #\λ
#\u03BB ; => #\λ

;;; 字符串是字符组成的定长数组
"Hello, world!"
"Benjamin \"Bugsy\" Siegel"   ; \是转义字符
"Foo\tbar\41\x21\u0021\a\r\n" ; 包含C语言的转义字符，和Unicode
"λx:(μα.α→α).xx"              ; 字符串可以包含Unicode字符

;; 字符串可以相加
(string-append "Hello " "world!") ; => "Hello world!"

;; 一个字符串可以看做是一个包含字符的列表
(string-ref "Apple" 0) ; => #\A

;; format 可以用来格式化字符串
(format "~a can be ~a" "strings" "formatted")

;; 打印字符串非常简单
(printf "I'm Racket. Nice to meet you!\n")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 2. 变量
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 你可以使用 define 定义一个变量
;; 变量的名字可以使用任何字符除了: ()[]{}",'`;#|\
(define some-var 5)
some-var ; => 5

;; 你也可以使用Unicode字符
(define ⊆ subset?)
(⊆ (set 3 2) (set 1 2 3)) ; => #t

;; 访问未赋值的变量会引发一个异常
; x ; => x: undefined ...

;; 本地绑定: `me' 被绑定到 "Bob"，并且只在 let 中生效
(let ([me "Bob"])
  "Alice"
  me) ; => "Bob"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. 结构和集合
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 结构体
(struct dog (name breed age))
(define my-pet
  (dog "lassie" "collie" 5))
my-pet ; => #<dog>
(dog? my-pet) ; => #t
(dog-name my-pet) ; => "lassie"

;;; 对 (不可变的)
;; `cons' 返回对, `car' 和 `cdr' 从对中提取第1个
;; 和第2个元素
(cons 1 2) ; => '(1 . 2)
(car (cons 1 2)) ; => 1
(cdr (cons 1 2)) ; => 2

;;; 列表

;; 列表由链表构成, 由 `cons' 的结果
;; 和一个 `null' (或者 '()) 构成，后者标记了这个列表的结束
(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3)
;; `list' 给列表提供了一个非常方便的可变参数的生成器
(list 1 2 3) ; => '(1 2 3)
;; 一个单引号也可以用来表示一个列表字面量
'(1 2 3) ; => '(1 2 3)

;; 仍然可以使用 `cons' 在列表的开始处添加一项
(cons 4 '(1 2 3)) ; => '(4 1 2 3)

;; `append' 函数可以将两个列表合并
(append '(1 2) '(3 4)) ; => '(1 2 3 4)

;; 列表是非常基础的类型，所以有*很多*操作列表的方法
;; 下面是一些例子:
(map add1 '(1 2 3))          ; => '(2 3 4)
(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33)
(filter even? '(1 2 3 4))    ; => '(2 4)
(count even? '(1 2 3 4))     ; => 2
(take '(1 2 3 4) 2)          ; => '(1 2)
(drop '(1 2 3 4) 2)          ; => '(3 4)

;;; 向量

;; 向量是定长的数组
#(1 2 3) ; => '#(1 2 3)

;; 使用 `vector-append' 方法将2个向量合并
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)

;;; Set(翻译成集合也不太合适，所以不翻译了..)

;; 从一个列表创建一个Set
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)

;; 使用 `set-add' 增加一个成员
;; (函数式特性: 这里会返回一个扩展后的Set，而不是修改输入的值)
(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)

;; 使用 `set-remove' 移除一个成员
(set-remove (set 1 2 3) 1) ; => (set 2 3)

;; 使用 `set-member?' 测试成员是否存在
(set-member? (set 1 2 3) 1) ; => #t
(set-member? (set 1 2 3) 4) ; => #f

;;; 散列表

;; 创建一个不变的散列表 (可变散列表的例子在下面)
(define m (hash 'a 1 'b 2 'c 3))

;; 根据键取得值
(hash-ref m 'a) ; => 1

;; 获取一个不存在的键是一个异常
; (hash-ref m 'd) => 没有找到元素

;; 你可以给不存在的键提供一个默认值
(hash-ref m 'd 0) ; => 0

;; 使用 `hash-set' 来扩展一个不可变的散列表
;; (返回的是扩展后的散列表而不是修改它)
(define m2 (hash-set m 'd 4))
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))

;; 记住，使用 `hash` 创建的散列表是不可变的
m ; => '#hash((b . 2) (a . 1) (c . 3))  <-- no `d'

;; 使用 `hash-remove' 移除一个键值对 (函数式特性，m并不变)
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. 函数
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 使用 `lambda' 创建函数
;; 函数总是返回它最后一个表达式的值
(lambda () "Hello World") ; => #<procedure>
;; 也可以使用 Unicode 字符 `λ'
(λ () "Hello World")     ; => 同样的函数

;; 使用括号调用一个函数，也可以直接调用一个 lambda 表达式
((lambda () "Hello World")) ; => "Hello World"
((λ () "Hello World"))      ; => "Hello World"

;; 将函数赋值为一个变量
(define hello-world (lambda () "Hello World"))
(hello-world) ; => "Hello World"

;; 你可以使用函数定义的语法糖来简化代码
(define (hello-world2) "Hello World")

;; `()`是函数的参数列表
(define hello
  (lambda (name)
    (string-append "Hello " name)))
(hello "Steve") ; => "Hello Steve"
;; 同样的，可以使用语法糖来定义:
(define (hello2 name)
  (string-append "Hello " name))

;; 你也可以使用可变参数， `case-lambda'
(define hello3
  (case-lambda
    [() "Hello World"]
    [(name) (string-append "Hello " name)]))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"
;; ... 或者给参数指定一个可选的默认值
(define (hello4 [name "World"])
  (string-append "Hello " name))

;; 函数可以将多余的参数放到一个列表里
(define (count-args . args)
  (format "You passed ~a args: ~a" (length args) args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
;; ... 也可以使用不带语法糖的 `lambda' 形式:
(define count-args2
  (lambda args
    (format "You passed ~a args: ~a" (length args) args)))

;; 你可以混用两种用法
(define (hello-count name . args)
  (format "Hello ~a, you passed ~a extra args" name (length args)))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"
;; ... 不带语法糖的形式:
(define hello-count2
  (lambda (name . args)
    (format "Hello ~a, you passed ~a extra args" name (length args))))

;; 使用关键字
(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
  (format "~a ~a, ~a extra args" g name (length args)))
(hello-k)                 ; => "Hello World, 0 extra args"
(hello-k 1 2 3)           ; => "Hello World, 3 extra args"
(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
                                         ; => "Hi Finn, 6 extra args"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. 判断是否相等
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 判断数字使用 `='
(= 3 3.0) ; => #t
(= 2 1) ; => #f

;; 判断对象使用 `eq?'
(eq? 3 3) ; => #t
(eq? 3 3.0) ; => #f
(eq? (list 3) (list 3)) ; => #f

;; 判断集合使用 `equal?'
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
(equal? (list 'a 'b) (list 'b 'a)) ; => #f

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 5. 控制结构
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 条件判断

(if #t               ; 测试表达式
    "this is true"   ; 为真的表达式
    "this is false") ; 为假的表达式
; => "this is true"

;; 注意, 除 `#f` 之外的所有值都认为是真
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
(if (member 'Groucho '(Harpo Groucho Zeppo))
    'yep
    'nope)
; => 'yep

;; `cond' 会进行一系列的判断来选择一个结果
(cond [(> 2 2) (error "wrong!")]
      [(< 2 2) (error "wrong again!")]
      [else 'ok]) ; => 'ok

;;; 模式匹配

(define (fizzbuzz? n)
  (match (list (remainder n 3) (remainder n 5))
    [(list 0 0) 'fizzbuzz]
    [(list 0 _) 'fizz]
    [(list _ 0) 'buzz]
    [_          #f]))

(fizzbuzz? 15) ; => 'fizzbuzz
(fizzbuzz? 37) ; => #f

;;; 循环

;; 循环可以使用递归(尾递归)
(define (loop i)
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i))))
(loop 5) ; => i=5, i=6, ...

;; 类似的，可以使用 `let` 定义
(let loop ((i 0))
  (when (< i 10)
    (printf "i=~a\n" i)
    (loop (add1 i)))) ; => i=0, i=1, ...

;; 看上面的例子怎么增加一个新的 `loop' 形式, 但是 Racket 已经有了一个非常
;; 灵活的 `for' 了:
(for ([i 10])
  (printf "i=~a\n" i)) ; => i=0, i=1, ...
(for ([i (in-range 5 10)])
  (printf "i=~a\n" i)) ; => i=5, i=6, ...

;;; 其他形式的迭代
;; `for' 允许在很多数据结构中迭代:
;; 列表, 向量, 字符串, Set, 散列表, 等...

(for ([i (in-list '(l i s t))])
  (displayln i))

(for ([i (in-vector #(v e c t o r))])
  (displayln i))

(for ([i (in-string "string")])
  (displayln i))

(for ([i (in-set (set 'x 'y 'z))])
  (displayln i))

(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
  (printf "key:~a value:~a\n" k v))

;;; 更多复杂的迭代

;; 并行扫描多个序列 (遇到长度小的就停止)
(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x 1:y 2:z

;; 嵌套循环
(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j))
; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z

;; 带有条件判断的 `for`
(for ([i 1000]
      #:when (> i 5)
      #:unless (odd? i)
      #:break (> i 10))
  (printf "i=~a\n" i))
; => i=6, i=8, i=10

;;; 更多的例子帮助你加深理解..
;; 和 `for' 循环非常像 -- 收集结果

(for/list ([i '(1 2 3)])
  (add1 i)) ; => '(2 3 4)

(for/list ([i '(1 2 3)] #:when (even? i))
  i) ; => '(2)

(for/list ([i 10] [j '(x y z)])
  (list i j)) ; => '((0 x) (1 y) (2 z))

(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10))
  i) ; => '(6 8 10)

(for/hash ([i '(1 2 3)])
  (values i (number->string i)))
; => '#hash((1 . "1") (2 . "2") (3 . "3"))

;; 也有很多其他的内置方法来收集循环中的值:
(for/sum ([i 10]) (* i i)) ; => 285
(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000
(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t
(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t
;; 如果需要合并计算结果, 使用 `for/fold'
(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10
;; (这个函数可以在大部分情况下替代普通的命令式循环)

;;; 异常

;; 要捕获一个异常，使用 `with-handlers' 形式
(with-handlers ([exn:fail? (lambda (exn) 999)])
  (+ 1 "2")) ; => 999
(with-handlers ([exn:break? (lambda (exn) "no time")])
  (sleep 3)
  "phew") ; => "phew", 如果你打断了它，那么结果 => "no time"

;; 使用 `raise' 抛出一个异常后者其他任何值
(with-handlers ([number?    ; 捕获抛出的数字类型的值
                 identity]) ; 将它们作为普通值
  (+ 1 (raise 2))) ; => 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 6. 可变的值
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 使用 `set!' 给一个已经存在的变量赋一个新值
(define n 5)
(set! n (add1 n))
n ; => 6

;; 给那些明确地需要变化的值使用 `boxes` (在其他语言里类似指针
;; 或者引用)
(define n* (box 5))
(set-box! n* (add1 (unbox n*)))
(unbox n*) ; => 6

;; 很多 Racket 诗句类型是不可变的 (对，列表，等)，有一些既是可变的
;; 又是不可变的 (字符串，向量，散列表
;; 等...)

;; 使用 `vector' 或者 `make-vector' 创建一个可变的向量
(define vec (vector 2 2 3 4))
(define wall (make-vector 100 'bottle-of-beer))
;; 使用 `vector-set!` 更新一项
(vector-set! vec 0 1)
(vector-set! wall 99 'down)
vec ; => #(1 2 3 4)

;; 创建一个空的可变散列表，然后操作它
(define m3 (make-hash))
(hash-set! m3 'a 1)
(hash-set! m3 'b 2)
(hash-set! m3 'c 3)
(hash-ref m3 'a)   ; => 1
(hash-ref m3 'd 0) ; => 0
(hash-remove! m3 'a)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 7. 模块
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 模块让你将你的代码组织为多个文件，成为可重用的模块，
;; 在这里，我们使用嵌套在本文的整个大模块
;; 里的子模块(从 "#lang" 这一行开始)

(module cake racket/base ; 基于 racket/base 定义一个 `cake` 模块

  (provide print-cake) ; 这个模块导出的函数

  (define (print-cake n)
    (show "   ~a   " n #\.)
    (show " .-~a-. " n #\|)
    (show " | ~a | " n #\space)
    (show "---~a---" n #\-))

  (define (show fmt n ch) ; 内部函数
    (printf fmt (make-string n ch))
    (newline)))

;; 使用 `require` 从模块中得到所有 `provide` 的函数
(require 'cake) ; 这里的 `'`表示是本地的子模块
(print-cake 3)
; (show "~a" 1 #\A) ; => 报错, `show' 没有被导出，不存在

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 8. 类和对象
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 创建一个 fish% 类(%是给类绑定用的)
(define fish%
  (class object%
    (init size) ; 初始化的参数
    (super-new) ; 父类的初始化
    ;; 域
    (define current-size size)
    ;; 公共方法
    (define/public (get-size)
      current-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

;; 创建一个 fish% 类的示例
(define charlie
  (new fish% [size 10]))

;; 使用 `send' 调用一个对象的方法
(send charlie get-size) ; => 10
(send charlie grow 6)
(send charlie get-size) ; => 16

;; `fish%' 是一个普通的值，我们可以用它来混入
(define (add-color c%)
  (class c%
    (init color)
    (super-new)
    (define my-color color)
    (define/public (get-color) my-color)))
(define colored-fish% (add-color fish%))
(define charlie2 (new colored-fish% [size 10] [color 'red]))
(send charlie2 get-color)
;; 或者，不带名字
(send (new (add-color fish%) [size 10] [color 'red]) get-color)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 9. 宏
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 宏让你扩展这门语言的语法

;; 让我们定义一个while循环
(define-syntax-rule (while condition body ...)
  (let loop ()
    (when condition
      body ...
      (loop))))

(let ([i 0])
  (while (< i  10)
    (displayln i)
    (set! i (add1 i))))

;; 宏是安全的，你不能修改现有的变量
(define-syntax-rule (swap! x y) ; !表示会修改
  (let ([tmp x])
    (set! x y)
    (set! y tmp)))

(define tmp 2)
(define other 3)
(swap! tmp other)
(printf "tmp = ~a; other = ~a\n" tmp other)
;; 变量 `tmp` 被重命名为 `tmp_1`
;; 避免名字冲突
;; (let ([tmp_1 tmp])
;;   (set! tmp other)
;;   (set! other tmp_1))

;; 但它们仍然会导致错误代码，比如:
(define-syntax-rule (bad-while condition body ...)
  (when condition
    body ...
    (bad-while condition body ...)))
;; 这个宏会挂掉，它产生了一个无限循环，如果你试图去使用它
;; 编译器会进入死循环

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 10. 契约
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 契约限制变量从模块中导入

(module bank-account racket
  (provide (contract-out
            [deposit (-> positive? any)] ; 数量一直是正值
            [balance (-> positive?)]))

  (define amount 0)
  (define (deposit a) (set! amount (+ amount a)))
  (define (balance) amount)
  )

(require 'bank-account)
(deposit 5)

(balance) ; => 5

;; 客户端尝试存储一个负值时会出错
;; (deposit -5) ; => deposit: contract violation
;; expected: positive?
;; given: -5
;; more details....
```

## 进一步阅读

想知道更多吗？ 尝试 [Getting Started with Racket](http://docs.racket-lang.org/getting-started/)
---
name: Red
category: language
language: Red
filename: LearnRed-zh.red
contributors:
    - ["Arnold van Hofwegen", "https://github.com/iArnold"]
translators:
    - ["Limo Saplf", "https://github.com/saplf"]
lang: zh-cn
---

Red 的编写是出于工作需要，该语言的作者想要使用 REBOL，但它有许多缺陷。
当时 REBOL 还没有开源，由于它是一门解释型语言，这就意味着它比编译型语言效率低。

Red 使用 C 语言级别的 Red/System，是一门涉及所有编程领域的语言。
Red 基于 REBOL 编写，它继承了 REBOL 的灵活性，同时也包含了许多 C 语言能做的底层实现。

Red 将会成为世界上第一门全栈式编程语言，这意味着它可以完成几乎所有的编程任务，从底层到抽象，无需其他工具的参与。
而且，Red 支持交叉编译，任意两个平台之间，不需要任何 GCC 之类的工具链的支持。
所有的工作，仅仅需要一个不到 1 MB 的二进制可执行文件。

准备好你的 Red 第一课了吗？

```red
所有 header 之前的文字都是注释，只要你不使用 "red" 关键字，其中的 "r" 大写。
这是词法分析器的一个缺陷，所以大多数时候，你都应该直接以 header 开始程序或者脚本的编写。

red 脚本的 header 由关键字，首字母大写的 "red" 开始，后跟一个空格，再跟一对方括号 []。
方括号里可以写上一些关于这段脚本或者程序的相关信息：
作者，文件名，版本号，license，程序功能的简介，它依赖的其他文件。
red/System 的 header 和 red header 类似，仅仅是说明 "red/System" 而非 "red"。


Red []

; 这是一条行注释

print "Hello Red World"    ; 另一条注释

comment {
    这是多行注释。
    你刚刚看到的就是 Red 版的 Hello World。
}

; 程序的入口就是第一句可执行的代码
; 不需要把它放在 'main' 函数里

; 变量名以一个字母开始，可以包含数字，
; 只包含 A ~ F 字符和数字的变量名不能以 'h' 结尾，
; 因为这是 Red 和 Red/System 中十六进制数字的表达方式。

; 给变量赋值使用冒号 ":"
my-name: "Red"
reason-for-using-the-colon: {使用冒号作为赋值符号
 是为了能够让 "=" 能够用来作为比较符号，这本来就是 "="
 存在的意义！还记得上学时学的，y = x + 1 、 x = 1，
 以及推导出的 y = 2 吗？
}
is-this-name-valid?: true

; 用 print 打印输出，prin 打印不带换行的输出

prin "我的名字是 " print my-name
; 我的名字是 Red

print ["我的名字是 " my-name lf]
; 我的名字是 Red

; 注意到了吗：语句没有以分号结尾 ;-)

;
; 数据类型
;
; 如果你了解 Rebol，你可能就会注意到它有许多数据类型。
; Red 并没有囊括它所有的类型，但由于 Red 想要尽可能的
; 接近 Rebol，所以它也会有很多数据类型。
; 类型以叹号结尾，但要注意，变量名也是可以以叹号结尾的。
; 一些内置类型有 integer! string! block!

; 使用变量前需要声明吗？
; Red 能够分辨什么时候使用什么变量，变量声明并非必要的。
; 一般认为，声明变量是较好的编码实践，但 Red 并不会强制这点。
; 你可以声明一个变量然后指定它的类型，而一个变量的类型就
; 指定了它的字节大小。

; integer! 类型的变量通常是 4 字节，32位
my-integer: 0
; Red 的整型包含符号，暂时不支持无符号类型，但以后会支持的。

; 怎样判断一个变量的类型？
type? my-integer
; integer!

; 一个变量的初始化可以使用另一个同样刚刚初始化的变量：
i2: 1 + i1: 1

; 算数运算符
i1 + i2 ; 3
i2 - i1 ; 1
i2 * i1 ; 2
i1 / i2 ; 0 (0.5，但截取为 0)

; 比较运算符都差不多，但和其他语言不一样的是相等的比较，
; Red 使用单个的 '='。
; Red 有一个类似 boolean 的类型，它的值是 true 和 false，
; 但也可以使用 on/off 或者 yes/on

3 = 2  ; false
3 != 2 ; true
3 > 2  ; true
3 < 2  ; false
2 <= 2 ; true
2 >= 2 ; true

;
; 控制流
;
; if
; 如果给定的条件为 true 则执行一段代码块。
; if 没有返回值，所以不能用作表达式。
if a < 0 [print "a 是负值"]

; either
; 如果给定的条件为 true 则执行一段代码块，否则就
; 执行另一段可选的代码块。如果两个代码块中最后一个表达式
; 的类型相同， either 就可以用作表达式。
either a > 0 [
    msg: "正值"
][
    either a = 0 [
        msg: "零"
    ][
        msg: "负值"
    ]
]
print ["a 是 " msg lf]

; 还可以有另一种写法
; （因为两条路径的返回值相同，所以可以这么写）：

msg: either a > 0 [
    "正值"
][
    either a = 0 [
        "零"
    ][
        "负值"
    ]
]
print ["a 是 " msg lf]

; util
; 循环执行一段代码块，直到满足给定的条件为止。
; util 没有返回值，所以它不能用在表示式中。
c: 5
util [
    prin "o"
    c: c - 1
    c = 0    ; 终止循环的条件
]
; 输出：ooooo
; 需要注意的是，即使条件从一开始就不满足，
; 这个循环也至少会执行一次。

; while
; 当满足给定的条件，就执行一段代码。
; while 没有返回值，不能用在表达式中。
c: 5
while [c > 0][
    prin "o"
    c: c - 1
]
; 输出：ooooo

;
; 函数
;
; 函数示例
twice: function [a [integer!] /one return: [integer!]][
        c: 2
        a: a * c
        either one [a + 1][a]
]
b: 3
print twice b      ; 输出 6

; 使用 #include 和 %文件名 来导入外部文件
#include %includefile.red
; 现在就可以使用 includefile.red 中的函数了。

```

## 更进一步

Red 相关的源码信息在 [Red 语言主页](http://www.red-lang.org)。

源代码的 [github 库](https://github.com/red/red)。

Red/System 特性在 [这里](http://static.red-lang.org/red-system-specs-light.html)。

想要了解更多关于 Rebol 和 Red 的信息，加入 [Gitter 聊天室](https://gitter.im/red/red)。如果你无法加入，也可以给我们发[邮件](mailto:red-langNO_SPAM@googlegroups.com)。

也可以在 [Stack Overflow](stackoverflow.com/questions/tagged/red) 上查阅、提交问题。

也许你现在就要试一试 Red ？可以在线尝试 [try Rebol and Red site](http://tryrebol.esperconsultancy.nl)。

你也可以通过学习一些 [Rebol](http://www.rebol.com/docs.html) 来学习 Red。
---
language: ruby
filename: learnruby-zh.rb
lang: zh-cn
contributors:
  - ["David Underwood", "http://theflyingdeveloper.com"]
  - ["Joel Walden", "http://joelwalden.net"]
  - ["Luke Holder", "http://twitter.com/lukeholder"]
  - ["lidashuang", "https://github.com/lidashuang"]
  - ["ftwbzhao", "https://github.com/ftwbzhao"]
translators:
  - ["Lin Xiangyu", "https://github.com/oa414"]
  - ["Jiang Haiyun", "https://github.com/haiiiiiyun"]
---

```ruby
# 这是单行注释

=begin
这是多行注释
没人用这个
你也不该用
=end

# 首先，也是最重要的，所有东西都是对象

# 数字是对象

3.class #=> Fixnum

3.to_s #=> "3"


# 一些基本的算术符号
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2**5 #=> 32
5 % 3 #=> 2

# 位运算符
3 & 5 #=> 1
3 | 5 #=> 7
3 ^ 5 #=> 6

# 算术符号只是语法糖而已
# 实际上是调用对象的方法
1.+(3) #=> 4
10.* 5 #=> 50 

# 特殊的值也是对象
nil # 相当于其它语言中的 null
true # 真
false # 假

nil.class #=> NilClass
true.class #=> TrueClass
false.class #=> FalseClass

# 相等运算符
1 == 1 #=> true
2 == 1 #=> false

# 不相等运算符
1 != 1 #=> false
2 != 1 #=> true

# 除了false自己，nil是唯一的另一个值为false的对象

!nil   #=> true
!false #=> true
!0     #=> false

# 更多比较
1 < 10 #=> true
1 > 10 #=> false
2 <= 2 #=> true
2 >= 2 #=> true


# 组合比较运算符
1 <=> 10 #=> -1
10 <=> 1 #=> 1
1 <=> 1 #=> 0

# 逻辑运算符
true && false #=> false
true || false #=> true
!true #=> false

# 也有优先级更低的逻辑运算符
# 它们用于控制流结构中，用来串接语句，直到返回true或false。

# `do_something_else` 只当 `do_something` 返回true时才会被调用
do_something() and do_something_else()
# `log_error` 只当 `do_something` 返回false时才会被调用
do_something() or log_error()


# 字符串是对象

'I am a string'.class #=> String
"I am a string too".class #=> String

placeholder = "use string interpolation"
"I can #{placeholder} when using double quoted strings"
#=> "I can use string interpolation when using double quoted strings"

# 尽可能优先使用单引号的字符串
# 双引号的字符串会进行一些额外的内部处理

# 合并字符串，但不能和数字合并
'hello ' + 'world'  #=> "hello world"
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
'hello ' + 3.to_s #=> "hello 3"

# 合并字符串及其运算符
'hello ' * 3 #=> "hello hello hello "

# 字符串追加
'hello' << ' world' #=> "hello world"

# 打印输出，并在末尾加换行符
puts "I'm printing!"
#=> I'm printing!
#=> nil

# 打印输出，不加换行符
print "I'm printing!"
#=> I'm printing! => nil

# 变量
x = 25 #=> 25
x #=> 25

# 注意赋值语句返回了赋的值
# 这意味着你可以用多重赋值语句

x = y = 10 #=> 10
x #=> 10
y #=> 10

# 按照惯例，使用类似snake_case风格的变量名
snake_case = true

# 使用有意义的变量名
path_to_project_root = '/good/name/'
path = '/bad/name/'

# 符号（Symbols，也是对象)
# 符号是不可变的，内部用整数值表示的可重用的常数
# 通常用它代替字符串来有效地表示有意义的值

:pending.class #=> Symbol

status = :pending

status == :pending #=> true

status == 'pending' #=> false

status == :approved #=> false

# 数组

# 这是一个数组
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]

# 数组可以包含不同类型的元素

[1, "hello", false] #=> [1, "hello", false]

# 数组可以被索引
# 从前面开始
array[0] #=> 1
array[12] #=> nil

# 像运算符一样，[var] 形式的访问
# 也只是语法糖
# 实际上是调用对象的 [] 方法
array.[] 0 #=> 1
array.[] 12 #=> nil

# 从尾部开始
array[-1] #=> 5
array.last #=> 5

# 同时指定开始的位置和长度
array[2, 3] #=> [3, 4, 5]

# 将数组逆序
a=[1,2,3]
a.reverse! #=> [3,2,1]

# 或者指定一个区间
array[1..3] #=> [2, 3, 4]

# 像这样往数组增加一个元素
array << 6 #=> [1, 2, 3, 4, 5, 6]
# 或者像这样
array.push(6) #=> [1, 2, 3, 4, 5, 6]

# 检查元素是否包含在数组中
array.include?(1) #=> true

# 哈希表是 Ruby 的主要键/值对表示法
# 哈希表由大括号表示
hash = {'color' => 'green', 'number' => 5}

hash.keys #=> ['color', 'number']

# 哈希表可以通过键快速地查询
hash['color'] #=> 'green'
hash['number'] #=> 5

# 查询一个不存在的键将会返回nil
hash['nothing here'] #=> nil

# 从Ruby 1.9开始，用符号作为键的时候有特别的记号表示:

new_hash = { defcon: 3, action: true }

new_hash.keys #=> [:defcon, :action]

# 小贴士：数组和哈希表都是可枚举的
# 它们共享一些有用的方法，比如each,map,count等等

# 控制流

if true
  "if statement"
elsif false
 "else if, optional"
else
 "else, also optional"
end

for counter in 1..5
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5


# 但是，没有人用for循环。
# 你应该使用"each"方法，然后再传给它一个块。
# 所谓块就是可以传给像"each"这样的方法的代码段。
# 它类似于其它语言中的lambdas, 匿名函数或闭包。
#
# 区间上的"each"方法会对区间中的每个元素运行一次块代码。
# 我们将counter作为一个参数传给了块。
# 调用带有块的"each"方法看起来如下：

(1..5).each do |counter|
  puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# 你也可以将块包含在一个大括号中：
(1..5).each { |counter| puts "iteration #{counter}" }

# 数据结构中的内容也可以使用each来遍历。
array.each do |element|
  puts "#{element} is part of the array"
end
hash.each do |key, value|
  puts "#{key} is #{value}"
end

# 如果你还需要索引值，可以使用"each_with_index"，并且定义
# 一个索引变量
array.each_with_index do |element, index|
  puts "#{element} is number #{index} in the array"
end

counter = 1
while counter <= 5 do
  puts "iteration #{counter}"
  counter += 1
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5

# Ruby 中还有很多有用的循环遍历函数，
# 如"map","reduce","inject"等等。
# 以map为例，它会遍历数组，并根据你在
# 块中定义的逻辑对它进行处理，然后返回
# 一个全新的数组。
array = [1,2,3,4,5]
doubled = array.map do |element|
  element * 2
end
puts doubled
#=> [2,4,6,8,10]
puts array
#=> [1,2,3,4,5]

grade = 'B'

case grade
when 'A'
  puts "Way to go kiddo"
when 'B'
  puts "Better luck next time"
when 'C'
  puts "You can do better"
when 'D'
  puts "Scraping through"
when 'F'
  puts "You failed!"
else 
  puts "Alternative grading system, eh?"
end
#=> "Better luck next time"

# case也可以用区间
grade = 82
case grade
when 90..100
  puts 'Hooray!'
when 80...90
  puts 'OK job'
else
  puts 'You failed!'
end
#=> "OK job"

# 异常处理：
begin
  # 这里的代码可能会抛出异常
  raise NoMemoryError, 'You ran out of memory.'
rescue NoMemoryError => exception_variable
  puts 'NoMemoryError was raised', exception_variable
rescue RuntimeError => other_exception_variable
  puts 'RuntimeError was raised now'
else
  puts 'This runs if no exceptions were thrown at all'
ensure
  puts 'This code always runs no matter what'
end

# 函数

def double(x)
  x * 2
end

# 函数 (以及所有的块) 隐式地返回最后语句的值
double(2) #=> 4

# 当不存在歧义的时候括号是可有可无的
double 3 #=> 6

double double 3 #=> 12

def sum(x,y)
  x + y
end

# 方法的参数通过逗号分隔
sum 3, 4 #=> 7

sum sum(3,4), 5 #=> 12

# yield
# 所有的方法都有一个隐式的，可选的块参数
# 可以用 'yield' 关键字调用

def surround
  puts "{"
  yield
  puts "}"
end

surround { puts 'hello world' }

# {
# hello world
# }

# 可以向函数传递一个块
# "&"标记传递的块是一个引用
def guests(&block)
  block.call 'some_argument'
end

# 可以传递多个参数，这些参数会转成一个数组，
# 这也是使用星号符 ("*") 的原因：
def guests(*array)
  array.each { |guest| puts guest }
end

# 如果函数返回一个数组，在赋值时可以进行拆分：
def foods
    ['pancake', 'sandwich', 'quesadilla']
end
breakfast, lunch, dinner = foods
breakfast #=> 'pancake'
dinner #=> 'quesadilla'

# 按照惯例，所有返回布尔值的方法都以?结尾
5.even? # false
5.odd? # true

# 如果方法名末尾有!，表示会做一些破坏性的操作，比如修改调用者自身。
# 很多方法都会有一个!的版本来进行修改，和一个非!的版本
# 只用来返回更新了的结果
company_name = "Dunder Mifflin"
company_name.upcase #=> "DUNDER MIFFLIN"
company_name #=> "Dunder Mifflin"
company_name.upcase! # we're mutating company_name this time!
company_name #=> "DUNDER MIFFLIN"


# 用class关键字定义一个类
class Human

  # 一个类变量，它被这个类的所有实例变量共享
  @@species = "H. sapiens"
  
  # 基本构造函数
  def initialize(name, age = 0)
    # 将参数值赋给实例变量"name"
    @name = name
    # 如果没有给出age,那么会采用参数列表中的默认值
    @age = age
  end
  
  # 基本的setter方法
  def name=(name)
    @name = name
  end
  
  # 基本地getter方法
  def name
    @name
  end
  
  # 以上的功能也可以用下面的attr_accessor来封装
  attr_accessor :name
  
  # Getter/setter方法也可以像这样单独创建
  attr_reader :name
  attr_writer :name
  
  # 类方法通过使用self与实例方法区别开来。
  # 它只能通过类来调用，不能通过实例调用。
  def self.say(msg)
    puts "#{msg}"
  end
  
  def species
    @@species
  end
end


# 初始化一个类
jim = Human.new("Jim Halpert")

dwight = Human.new("Dwight K. Schrute")

# 让我们来调用一些方法
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# 调用类方法
Human.say('Hi') #=> "Hi"

# 变量的作用域由它们的名字格式定义
# 以$开头的变量具有全局域
$var = "I'm a global var"
defined? $var #=> "global-variable"

# 以@开头的变量具有实例作用域
@var = "I'm an instance var"
defined? @var #=> "instance-variable"

# 以@@开头的变量具有类作用域
@@var = "I'm a class var"
defined? @@var #=> "class variable"

# 以大写字母开头的变量是常数
Var = "I'm a constant"
defined? Var #=> "constant"

# 类也是对象。因此类也可以有实例变量。
# 类变量在类以及其继承者之间共享。

# 基类
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

# 派生类
class Worker < Human
end

Human.foo # 0
Worker.foo # 0

Human.foo = 2 # 2
Worker.foo # 2

# 类实例变量不能在继承类间共享。

class Human
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doctor < Human
end

Human.bar # 0
Doctor.bar # nil

module ModuleExample
  def foo
    'foo'
  end
end

# '包含'模块后，模块的方法会绑定为类的实例方法
# '扩展'模块后，模块的方法会绑定为类方法

class Person
  include ModuleExample
end

class Book
  extend ModuleExample
end

Person.foo     # => NoMethodError: undefined method `foo' for Person:Class
Person.new.foo # => 'foo'
Book.foo       # => 'foo'
Book.new.foo   # => NoMethodError: undefined method `foo'

# 当包含或扩展一个模块时，相应的回调代码会被执行。

module ConcernExample
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end

  module ClassMethods
    def bar
      'bar'
    end
  end

  module InstanceMethods
    def qux
      'qux'
    end
  end
end

class Something
  include ConcernExample
end

Something.bar     # => 'bar'
Something.qux     # => NoMethodError: undefined method `qux'
Something.new.bar # => NoMethodError: undefined method `bar'
Something.new.qux # => 'qux'
```


## 其它资源

- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges.
- [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Learn Ruby through a series of interactive tutorials.
- [Official Documentation](http://ruby-doc.org/core)
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.
- [Try Ruby](http://tryruby.org) - Learn the basic of Ruby programming language, interactive in the browser.
---
language: rust
contributors:
    - ["P1start", "http://p1start.github.io/"]
translators:
    - ["Guangming Mao", "http://maogm.com"]
filename: learnrust-cn.rs
lang: zh-cn
---

Rust 是由 Mozilla 研究院开发的编程语言。Rust 将底层的性能控制与高级语言的便利性和安全保障结合在了一起。

而 Rust 并不需要一个垃圾回收器或者运行时即可实现这个目的，这使得 Rust 库可以成为一种 C 语言的替代品。

Rust 第一版（0.1 版）发布于 2012 年 1 月，3 年以来一直在紧锣密鼓地迭代。
因为更新太频繁，一般建议使用每夜构建版而不是稳定版，直到最近 1.0 版本的发布。

2015 年 3 月 15 日，Rust 1.0 发布，完美向后兼容，最新的每夜构建版提供了缩短编译时间等新特性。
Rust 采用了持续迭代模型，每 6 周一个发布版。Rust 1.1 beta 版在 1.0 发布时同时发布。

尽管 Rust 相对来说是一门底层语言，它提供了一些常见于高级语言的函数式编程的特性。这让 Rust 不仅高效，并且易用。

```rust
// 这是注释，单行注释...
/* ...这是多行注释 */

///////////////
// 1. 基础   //
///////////////

// 函数 (Functions)
// `i32` 是有符号 32 位整数类型(32-bit signed integers)
fn add2(x: i32, y: i32) -> i32 {
    // 隐式返回 (不要分号)
    x + y
}

// 主函数(Main function)
fn main() {
    // 数字 (Numbers) //

    // 不可变绑定
    let x: i32 = 1;

    // 整形/浮点型数 后缀
    let y: i32 = 13i32;
    let f: f64 = 1.3f64;

    // 类型推导
    // 大部分时间，Rust 编译器会推导变量类型，所以不必把类型显式写出来。
    // 这个教程里面很多地方都显式写了类型，但是只是为了示范。
    // 绝大部分时间可以交给类型推导。
    let implicit_x = 1;
    let implicit_f = 1.3;

    // 算术运算
    let sum = x + y + 13;

    // 可变变量
    let mut mutable = 1;
    mutable = 4;
    mutable += 2;

    // 字符串 (Strings) //

    // 字符串字面量
    let x: &str = "hello world!";

    // 输出
    println!("{} {}", f, x); // 1.3 hello world

    // 一个 `String` – 在堆上分配空间的字符串
    let s: String = "hello world".to_string();

    // 字符串分片(slice) - 另一个字符串的不可变视图
    // 基本上就是指向一个字符串的不可变指针，它不包含字符串里任何内容，只是一个指向某个东西的指针
    // 比如这里就是 `s`
    let s_slice: &str = &s;

    println!("{} {}", s, s_slice); // hello world hello world

    // 数组 (Vectors/arrays) //

    // 长度固定的数组 (array)
    let four_ints: [i32; 4] = [1, 2, 3, 4];

    // 变长数组 (vector)
    let mut vector: Vec<i32> = vec![1, 2, 3, 4];
    vector.push(5);

    // 分片 - 某个数组(vector/array)的不可变视图
    // 和字符串分片基本一样，只不过是针对数组的
    let slice: &[i32] = &vector;

    // 使用 `{:?}` 按调试样式输出
    println!("{:?} {:?}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

    // 元组 (Tuples) //

    // 元组是固定大小的一组值，可以是不同类型
    let x: (i32, &str, f64) = (1, "hello", 3.4);

    // 解构 `let`
    let (a, b, c) = x;
    println!("{} {} {}", a, b, c); // 1 hello 3.4

    // 索引
    println!("{}", x.1); // hello

    //////////////
    // 2. 类型 (Type)  //
    //////////////

    // 结构体（Sturct)
    struct Point {
        x: i32,
        y: i32,
    }

    let origin: Point = Point { x: 0, y: 0 };

    // 匿名成员结构体，又叫“元组结构体”（‘tuple struct’）
    struct Point2(i32, i32);

    let origin2 = Point2(0, 0);

    // 基础的 C 风格枚举类型（enum）
    enum Direction {
        Left,
        Right,
        Up,
        Down,
    }

    let up = Direction::Up;

    // 有成员的枚举类型
    enum OptionalI32 {
        AnI32(i32),
        Nothing,
    }

    let two: OptionalI32 = OptionalI32::AnI32(2);
    let nothing = OptionalI32::Nothing;

    // 泛型 (Generics) //

    struct Foo<T> { bar: T }

    // 这个在标准库里面有实现，叫 `Option`
    enum Optional<T> {
        SomeVal(T),
        NoVal,
    }

    // 方法 (Methods) //

    impl<T> Foo<T> {
        // 方法需要一个显式的 `self` 参数
        fn get_bar(self) -> T {
            self.bar
        }
    }

    let a_foo = Foo { bar: 1 };
    println!("{}", a_foo.get_bar()); // 1

    // 接口（Traits） （其他语言里叫 interfaces 或 typeclasses） //

    trait Frobnicate<T> {
        fn frobnicate(self) -> Option<T>;
    }

    impl<T> Frobnicate<T> for Foo<T> {
        fn frobnicate(self) -> Option<T> {
            Some(self.bar)
        }
    }

    let another_foo = Foo { bar: 1 };
    println!("{:?}", another_foo.frobnicate()); // Some(1)

    ///////////////////////////////////
    // 3. 模式匹配 (Pattern matching) //
    ///////////////////////////////////

    let foo = OptionalI32::AnI32(1);
    match foo {
        OptionalI32::AnI32(n) => println!("it’s an i32: {}", n),
        OptionalI32::Nothing  => println!("it’s nothing!"),
    }

    // 高级模式匹配
    struct FooBar { x: i32, y: OptionalI32 }
    let bar = FooBar { x: 15, y: OptionalI32::AnI32(32) };

    match bar {
        FooBar { x: 0, y: OptionalI32::AnI32(0) } =>
            println!("The numbers are zero!"),
        FooBar { x: n, y: OptionalI32::AnI32(m) } if n == m =>
            println!("The numbers are the same"),
        FooBar { x: n, y: OptionalI32::AnI32(m) } =>
            println!("Different numbers: {} {}", n, m),
        FooBar { x: _, y: OptionalI32::Nothing } =>
            println!("The second number is Nothing!"),
    }

    ///////////////////////////////
    // 4. 流程控制 (Control flow) //
    ///////////////////////////////

    // `for` 循环
    let array = [1, 2, 3];
    for i in array.iter() {
        println!("{}", i);
    }

    // 区间 (Ranges)
    for i in 0u32..10 {
        print!("{} ", i);
    }
    println!("");
    // 输出 `0 1 2 3 4 5 6 7 8 9 `

    // `if`
    if 1 == 1 {
        println!("Maths is working!");
    } else {
        println!("Oh no...");
    }

    // `if` 可以当表达式
    let value = if true {
        "good"
    } else {
        "bad"
    };

    // `while` 循环
    while 1 == 1 {
        println!("The universe is operating normally.");
    }

    // 无限循环
    loop {
        println!("Hello!");
    }

    ////////////////////////////////////////////////
    // 5. 内存安全和指针 (Memory safety & pointers) //
    ////////////////////////////////////////////////

    // 独占指针 (Owned pointer) - 同一时刻只能有一个对象能“拥有”这个指针
    // 意味着 `Box` 离开他的作用域后，会被安全地释放
    let mut mine: Box<i32> = Box::new(3);
    *mine = 5; // 解引用
    // `now_its_mine` 获取了 `mine` 的所有权。换句话说，`mine` 移动 (move) 了
    let mut now_its_mine = mine;
    *now_its_mine += 2;

    println!("{}", now_its_mine); // 7
    // println!("{}", mine); // 编译报错，因为现在 `now_its_mine` 独占那个指针

    // 引用 (Reference) – 引用其他数据的不可变指针
    // 当引用指向某个值，我们称为“借用”这个值，因为是被不可变的借用，所以不能被修改，也不能移动
    // 借用一直持续到生命周期结束，即离开作用域
    let mut var = 4;
    var = 3;
    let ref_var: &i32 = &var;

    println!("{}", var); //不像 `mine`, `var` 还可以继续使用
    println!("{}", *ref_var);
    // var = 5; // 编译报错，因为 `var` 被借用了
    // *ref_var = 6; // 编译报错，因为 `ref_var` 是不可变引用

    // 可变引用 (Mutable reference)
    // 当一个变量被可变地借用时，也不可使用
    let mut var2 = 4;
    let ref_var2: &mut i32 = &mut var2;
    *ref_var2 += 2;

    println!("{}", *ref_var2); // 6
    // var2 = 2; // 编译报错，因为 `var2` 被借用了
}
```

## 更深入的资料

Rust 还有很多很多其他内容 - 这只是 Rust 最基本的功能，帮助你了解 Rust 里面最重要的东西。
如果想深入学习 Rust，可以去读
[The Rust Programming Language](http://doc.rust-lang.org/book/index.html)
或者上 reddit [/r/rust](http://reddit.com/r/rust) 订阅。
同时 irc.mozilla.org 的 #rust 频道上的小伙伴们也非常欢迎新来的朋友。

你可以在这个在线编译器 [Rust playpen](http://play.rust-lang.org) 上尝试 Rust 的一些特性
或者上[官方网站](http://rust-lang.org).
---
language: sass
filename: learnsass-cn.scss
contributors:
  - ["Laura Kyle", "https://github.com/LauraNK"]
  - ["Sean Corrales", "https://github.com/droidenator"]
  - ["Kyle Mendes", "https://github.com/pink401k"]
  - ["Keith Miyake", "https://github.com/kaymmm"]
translators:
   - ["Jiang Haiyun", "http://www.atjiang.com"]
lang: zh-cn
---

Sass是一种CSS扩展语言，它增加了诸如变量、嵌套、mixin等功能。
Sass(以及其它预处理器，如[Less](http://lesscss.org/)等) 能帮助开发人员编写易维护和 DRY (Don't Repeat Yourself)的代码。

Sass有两种不同的语法可选用。SCSS的语法和CSS的相同，但增加了Sass的额外功能。或者Sass（原来的语法），它使用缩进而非大括号和分号。

本教程使用SCSS编写。

如果你已熟悉CSS3，你可能相对能较快地掌握Sass。它并没有提供任何新的类型属性，而只是提供了一些工具使你能更高效的编写CSS，并且使维护更加容易。

```scss


// 单行注释当Sass被编译成CSS后会被删除。

/* 多行注释将保留. */

/* 变量
============================== */



/* 你可以将一个CSS值（如一个颜色值）保存到变量中。
使用'$'符号来创建一个变量。*/

$primary-color: #A3A4FF;
$secondary-color: #51527F;
$body-font: 'Roboto', sans-serif;

/* 你可以在你的样式文件中使用变量。
   现在假如你想修改颜色，你只需修改一次即可。*/

body {
	background-color: $primary-color;
	color: $secondary-color;
	font-family: $body-font;
}

/* 以上将编译成： */
body {
	background-color: #A3A4FF;
	color: #51527F;
	font-family: 'Roboto', sans-serif;
}

/* 相比于在你的样式文件中逐个进行修改，这种方式维护性更好。 */



/* 控制指令
============================== */

/* Sass允许你使用@if, @else, @for, @while, 和 @each 来控制
   你的代码如何编译成CSS */

/* @if/@else块的行为和你可能预想的会完全相同 */

$debug: true !default;

@mixin debugmode {
	@if $debug {
		@debug "Debug mode enabled";

		display: inline-block;
	}
	@else {
		display: none;
	}
}

.info {
	@include debugmode;
}

/* 如果$debug设置为了true, .info 将会显示; 如果设置为false那么
   .info 将不显示。

注意： @debug将在命令行中输出调试信息。
在调试你的SCSS时它对于检查变量很有用。*/

.info {
	display: inline-block;
}

/* @for是控制循环，它能遍历区间值。
它对于设置一组元素的类型特别有用。
有两种形式，"through"和"to"。前者包括最末那个值，
而后者止于最末那个值。 
*/

@for $c from 1 to 4 {
	div:nth-of-type(#{$c}) {
		left: ($c - 1) * 900 / 3;
	}
}

@for $c from 1 through 3 {
	.myclass-#{$c} {
		color: rgb($c * 255 / 3, $c * 255 / 3, $c * 255 / 3);
	}
}

/* 将编译成: */

div:nth-of-type(1) {
	left: 0;
}

div:nth-of-type(2) {
	left: 300;
}

div:nth-of-type(3) {
	left: 600;
}

.myclass-1 {
	color: #555555;
}

.myclass-2 {
	color: #aaaaaa;
}

.myclass-3 {
	color: white;
// SASS automatically converts #FFFFFF to white
}

/* @while也非常直白： */

$columns: 4;
$column-width: 80px;

@while $columns > 0 {
	.col-#{$columns} {
		width: $column-width;
		left: $column-width * ($columns - 1);
	}

	$columns: $columns - 1;
}

/* 将输出以下CSS: */

.col-4 {
	width: 80px;
	left: 240px;
}

.col-3 {
	width: 80px;
	left: 160px;
}

.col-2 {
	width: 80px;
	left: 80px;
}

.col-1 {
	width: 80px;
	left: 0px;
}

/* @each函数类似@for, 除了它使用一个列表而不是序列值
注意: 你指定列表的方式和指定其它变量一样，
用空格作为分隔符。 */

$social-links: facebook twitter linkedin reddit;

.social-links {
	@each $sm in $social-links {
		.icon-#{$sm} {
			background-image: url("images/#{$sm}.png");
		}
	}
}

/* 将输出: */

.social-links .icon-facebook {
	background-image: url("images/facebook.png");
}

.social-links .icon-twitter {
	background-image: url("images/twitter.png");
}

.social-links .icon-linkedin {
	background-image: url("images/linkedin.png");
}

.social-links .icon-reddit {
	background-image: url("images/reddit.png");
}


/* Mixins
==============================*/

/* 如果你发现你要为多个元素编写相同的代码，
你可能想将那些代码保存到一个mixin中。

使用'@mixin'指令，再为你的mixin加上一个名称。*/

@mixin center {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
}

/* 你可以通过'@include'及mixin名来调用mixin。 */

div {
	@include center;
	background-color: $primary-color;
}

/* 将编译成: */
div {
	display: block;
	margin-left: auto;
	margin-right: auto;
	left: 0;
	right: 0;
	background-color: #A3A4FF;
}

/* 你可以使用mixin来创建一个快捷属性。*/

@mixin size($width, $height) {
	width: $width;
	height: $height;
}

/* 你可以通过传入width和height参数来调用它。*/

.rectangle {
	@include size(100px, 60px);
}

.square {
	@include size(40px, 40px);
}

/* 编译成: */
.rectangle {
  width: 100px;
  height: 60px;
}

.square {
  width: 40px;
  height: 40px;
}



/* 函数
============================== */



/* Sass提供的函数可以用来完成各种各样的任务。
   考虑以下情况 */

/* 函数可以通过其名称及传入其所需的参数来调用 */
body {
  width: round(10.25px);
}

.footer {
  background-color: fade_out(#000000, 0.25);
}

/* 编译成: */

body {
  width: 10px;
}

.footer {
  background-color: rgba(0, 0, 0, 0.75);
}

/* 你也可以定义你自己的函数。函数非常类似于mixin。
   当你在函数和mixin之间抉择时，记住mixin最适合于创建CSS而函数更适合于
   处理那些可能在你的Sass代码中使用的逻辑。'数学运算符'部分的例子
   是转成可重用函数的最理想选择。 */

/* 该函数将接收一个目标尺寸大小和父结点尺寸大小，然后计算并
    返回百分数 */

@function calculate-percentage($target-size, $parent-size) {
  @return $target-size / $parent-size * 100%;
}

$main-content: calculate-percentage(600px, 960px);

.main-content {
  width: $main-content;
}

.sidebar {
  width: calculate-percentage(300px, 960px);
}

/* 编译成: */

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}



/* 扩展 (继承)
============================== */



/* 扩展是在选择子间共享属性的一种方法。 */

.display {
	@include size(5em, 5em);
	border: 5px solid $secondary-color;
}

.display-success {
	@extend .display;
	border-color: #22df56;
}

/* 编译成: */
.display, .display-success {
  width: 5em;
  height: 5em;
  border: 5px solid #51527F;
}

.display-success {
  border-color: #22df56;
}

/* 扩展一条CSS语句优于创建一个mixin，
   这是由Sass组合所有共享相同基样式的类的方式决定的。
   如果使用mixin完成，width, height, 和border将会在
   调用了该mixin的每条语句中重复。虽然它不至于会影响你的工作流，
   但它会在由Sass编译器生成的的文件中添加不必要的代码。*/


/* 嵌套
============================== */



/* Sass允许在选择子中嵌套选择子 */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: #FF0000;
	}
}

/* '&'将被父选择子替换。*/
/* 你也可以嵌套伪类。 */
/* 注意过度嵌套将导致你的代码难以维护。
最佳实践推荐在嵌套时不超过3层。
例如： */

ul {
	list-style-type: none;
	margin-top: 2em;

	li {
		background-color: red;

		&:hover {
		  background-color: blue;
		}

		a {
		  color: white;
		}
	}
}

/* 编译成： */

ul {
  list-style-type: none;
  margin-top: 2em;
}

ul li {
  background-color: red;
}

ul li:hover {
  background-color: blue;
}

ul li a {
  color: white;
}



/* 片段与导入
============================== */



/* Sass允许你创建片段文件。它有助于你的Sass代码保持模块化。
   片段文件应该以 '_' 开头，例如 _reset.css。
   片段不会输出到CSS中。*/

/* 考虑以下的CSS，我们会将它们放入一个叫作_reset.css的文件中 */

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

/* Sass提供的@import能用来将片段导入到文件中。
   它与传统的CSS @import语句不同，不需要通过
   另外的HTTP请求来获取导入的文件。
   Sass提取导入文件并将它与编译后的代码结合起来。 */

@import 'reset';

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}

/* 编译成: */

html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: Helvetica, Arial, Sans-serif;
}



/* 占位符选择子
============================== */



/* 占位符在创建用于扩展的CSS语句时非常有用。
   如果你想创建一条只通过@extend使用的CSS语句，你可以利用占位符来实现。
   占位符以'%'而非'.'或'#'开头。占位符不会出现在编译后的CSS中 */

%content-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  @extend %content-window;
  background-color: #0000ff;
}

/* 编译成: */

.message-window {
  font-size: 14px;
  padding: 10px;
  color: #000;
  border-radius: 4px;
}

.message-window {
  background-color: #0000ff;
}



/* 数学运算
============================== */



/* Sass提供以下的运算符: +, -, *, /, 和 %。它们
   相比于使用你事先手工计算好了的数值，它们
   对于直接在你的Sass文件中计算数值很有用。
   以下是设置一个简单的两列设计的例子。*/

$content-area: 960px;
$main-content: 600px;
$sidebar-content: 300px;

$main-size: $main-content / $content-area * 100%;
$sidebar-size: $sidebar-content / $content-area * 100%;
$gutter: 100% - ($main-size + $sidebar-size);

body {
  width: 100%;
}

.main-content {
  width: $main-size;
}

.sidebar {
  width: $sidebar-size;
}

.gutter {
  width: $gutter;
}

/* 编译成： */

body {
  width: 100%;
}

.main-content {
  width: 62.5%;
}

.sidebar {
  width: 31.25%;
}

.gutter {
  width: 6.25%;
}

```

## SASS还是Sass?
该语言的名字，“Sass”，是一个词，不是一个缩写。
你有没想过Sass是否是一个缩写词？你可能没有，但我反正会告诉你。
该语言的名字是一个单词，不是一个缩写词。
由于人们老是将它写成"SASS"，语言的作者开玩笑地称它为"Syntactically Awesome StyleSheets"。


## 实践Sass
如果你想在你的浏览器中尝试Sass，参阅[SassMeister](http://sassmeister.com/)。
你可以选用任一种语法，只需进到设置页然后选择Sass或SCSS。


## 兼容性
Sass可以用于任何项目中，只要你有程序能将它编译成CSS即可。你还需要验证你所使用的CSS是否与你的目标浏览器兼容。

[QuirksMode CSS](http://www.quirksmode.org/css/)和[CanIUse](http://caniuse.com)对于检查兼容性来说都是不错的资源。


## 延伸阅读资料
* [Official Documentation](http://sass-lang.com/documentation/file.SASS_REFERENCE.html)
* [The Sass Way](http://thesassway.com/) 上提供了教程(初学者-高级)和文章。
---
language: Scala
filename: learnscala-zh.scala
contributors:
    - ["George Petrov", "http://github.com/petrovg"]
    - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
    - ["Geoff Liu", "http://geoffliu.me"]
translators:
    - ["Peiyong Lin", ""]
    - ["Jinchang Ye", "http://github.com/alwayswithme"]
    - ["Guodong Qu", "https://github.com/jasonqu"]
lang: zh-cn
---

Scala - 一门可拓展的语言

```scala

/*
  自行设置:

  1) 下载 Scala - http://www.scala-lang.org/downloads
  2) unzip/untar 到您喜欢的地方，并把 bin 子目录添加到 path 环境变量
  3) 在终端输入 scala，启动 Scala 的 REPL，您会看到提示符：

  scala>

  这就是所谓的 REPL (读取-求值-输出循环，英语： Read-Eval-Print Loop)，
  您可以在其中输入合法的表达式，结果会被打印。
  在教程中我们会进一步解释 Scala 文件是怎样的，但现在先了解一点基础。
*/


/////////////////////////////////////////////////
// 1. 基础
/////////////////////////////////////////////////

//  单行注释开始于两个斜杠

/*
  多行注释，如您之前所见，看起来像这样
*/

// 打印并强制换行
println("Hello world!")
println(10)

// 没有强制换行的打印
print("Hello world")

// 通过 var 或者 val 来声明变量
// val 声明是不可变的，var 声明是可修改的。不可变性是好事。
val x = 10 // x 现在是 10
x = 20 // 错误: 对 val 声明的变量重新赋值
var y = 10 
y = 20  // y 现在是 20

/*
  Scala 是静态语言，但注意上面的声明方式，我们没有指定类型。
  这是因为类型推导的语言特性。大多数情况， Scala 编译器可以推测变量的类型，
  所以您不需要每次都输入。可以像这样明确声明变量类型：
*/
val z: Int = 10
val a: Double = 1.0

// 注意从 Int 到 Double 的自动转型，结果是 10.0, 不是 10
val b: Double = 10

// 布尔值
true
false

// 布尔操作
!true // false
!false // true
true == false // false
10 > 5 // true

// 数学运算像平常一样
1 + 1 // 2
2 - 1 // 1
5 * 3 // 15
6 / 2 // 3
6 / 4 // 1
6.0 / 4 // 1.5


// 在 REPL 计算一个表达式会返回给您结果的类型和值

1 + 7

/* 上行的结果是：

  scala> 1 + 7
  res29: Int = 8

  这意味着计算 1 + 7 的结果是一个 Int 类型的对象，其值为 8

  注意 "res29" 是一个连续生成的变量名，用以存储您输入的表达式结果，
  您看到的输出可能不一样。
*/

"Scala strings are surrounded by double quotes"
'a' // Scala 的字符
// '不存在单引号字符串' <= 这会导致错误

// String 有常见的 Java 字符串方法
"hello world".length
"hello world".substring(2, 6)
"hello world".replace("C", "3")

// 也有一些额外的 Scala 方法，另请参见：scala.collection.immutable.StringOps
"hello world".take(5)
"hello world".drop(5)

// 字符串改写：留意前缀 "s"
val n = 45
s"We have $n apples" // => "We have 45 apples"

// 在要改写的字符串中使用表达式也是可以的
val a = Array(11, 9, 6)
s"My second daughter is ${a(0) - a(2)} years old." // => "My second daughter is 5 years old."
s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples."
s"Power of 2: ${math.pow(2, 2)}" // => "Power of 2: 4"

// 添加 "f" 前缀对要改写的字符串进行格式化
f"Power of 5: ${math.pow(5, 2)}%1.0f" // "Power of 5: 25"
f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122: 11.0454"

// 未处理的字符串，忽略特殊字符。
raw"New line feed: \n. Carriage return: \r." // => "New line feed: \n. Carriage return: \r."

// 一些字符需要转义，比如字符串中的双引号
"They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown""

// 三个双引号可以使字符串跨越多行，并包含引号
val html = """<form id="daform">
                <p>Press belo', Joe</p>
                <input type="submit">
              </form>"""


/////////////////////////////////////////////////
// 2. 函数
/////////////////////////////////////////////////

// 函数可以这样定义:
//
//   def functionName(args...): ReturnType = { body... }
//
// 如果您以前学习过传统的编程语言，注意 return 关键字的省略。
// 在 Scala 中， 函数代码块最后一条表达式就是返回值。
def sumOfSquares(x: Int, y: Int): Int = {
  val x2 = x * x
  val y2 = y * y
  x2 + y2
}

// 如果函数体是单行表达式，{ } 可以省略：
def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y

// 函数调用的语法是熟知的：
sumOfSquares(3, 4)  // => 25

// 在多数情况下 (递归函数是需要注意的例外), 函数返回值可以省略，
// 变量所用的类型推导一样会应用到函数返回值中：
def sq(x: Int) = x * x  // 编译器会推断得知返回值是 Int

// 函数可以有默认参数
def addWithDefault(x: Int, y: Int = 5) = x + y
addWithDefault(1, 2)  // => 3
addWithDefault(1)  // => 6


// 匿名函数是这样的：
(x:Int) => x * x

// 和 def 不同，如果语义清晰，匿名函数的参数类型也可以省略。
// 类型 "Int => Int" 意味着这个函数接收一个 Int 并返回一个 Int。
val sq: Int => Int = x => x * x

// 匿名函数的调用也是类似的：
sq(10)   // => 100

// 如果您的匿名函数中每个参数仅使用一次，
// Scala 提供一个更简洁的方式来定义他们。这样的匿名函数极为常见，
// 在数据结构部分会明显可见。
val addOne: Int => Int = _ + 1
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)

addOne(5)  // => 6
weirdSum(2, 4)  // => 16


// return 关键字是存在的，但它只从最里面包裹了 return 的 def 函数中返回。
// 警告： 在 Scala 中使用 return 容易出错，应该避免使用。
// 在匿名函数中没有效果，例如：
def foo(x: Int): Int = {
  val anonFunc: Int => Int = { z =>
    if (z > 5)
      return z  // 这一行令 z 成为 foo 函数的返回值！
    else
      z + 2  // 这一行是 anonFunc 函数的返回值
  }
  anonFunc(x)  // 这一行是 foo 函数的返回值
}

/*
 * 译者注：此处是指匿名函数中的 return z 成为最后执行的语句，
 *    在 anonFunc(x) 下面的表达式（假设存在）不再执行。如果 anonFunc
 *    是用 def 定义的函数， return z 仅返回到 anonFunc(x) ，
 *    在 anonFunc(x) 下面的表达式（假设存在）会继续执行。
 */


/////////////////////////////////////////////////
// 3. 控制语句
/////////////////////////////////////////////////

1 to 5
val r = 1 to 5
r.foreach( println )

r foreach println
// 附注： Scala 对点和括号的要求想当宽松，注意其规则是不同的。
// 这有助于写出读起来像英语的 DSL(领域特定语言) 和 API(应用编程接口)。

(5 to 1 by -1) foreach ( println )

// while 循环
var i = 0
while (i < 10) {  println("i " + i); i+=1  }

while (i < 10) {  println("i " + i); i+=1  }   // 没错，再执行一次，发生了什么？为什么？

i    // 显示 i 的值。注意 while 是经典的循环方式，它连续执行并改变循环中的变量。
     // while 执行很快，比 Java 的循环快，但像上面所看到的那样用组合子和推导式
     // 更易于理解和并行化。

// do while 循环
do {
  println("x is still less than 10");
  x += 1
} while (x < 10)

// Scala 中尾递归是一种符合语言习惯的递归方式。
// 递归函数需要清晰的返回类型，编译器不能推断得知。
// 这是一个 Unit。
def showNumbersInRange(a:Int, b:Int):Unit = {
  print(a)
  if (a < b)
    showNumbersInRange(a + 1, b)
}
showNumbersInRange(1,14)


// 条件语句

val x = 10

if (x == 1) println("yeah")
if (x == 10) println("yeah")
if (x == 11) println("yeah")
if (x == 11) println ("yeah") else println("nay")

println(if (x == 10) "yeah" else "nope")
val text = if (x == 10) "yeah" else "nope"


/////////////////////////////////////////////////
// 4. 数据结构
/////////////////////////////////////////////////

val a = Array(1, 2, 3, 5, 8, 13)
a(0)
a(3)
a(21)    // 抛出异常

val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
m("fork")
m("spoon")
m("bottle")       // 抛出异常

val safeM = m.withDefaultValue("no lo se")
safeM("bottle")

val s = Set(1, 3, 7)
s(0)
s(1)

/* 这里查看 map 的文档 -
 * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
 * 并确保你会阅读
 */


// 元组

(1, 2)

(4, 3, 2)

(1, 2, "three")

(a, 2, "three")

// 为什么有这个？
val divideInts = (x:Int, y:Int) => (x / y, x % y)

divideInts(10,3) // 函数 divideInts 同时返回结果和余数

// 要读取元组的元素，使用 _._n，n是从1开始的元素索引
val d = divideInts(10,3)

d._1

d._2


/////////////////////////////////////////////////
// 5. 面向对象编程
/////////////////////////////////////////////////

/*
  旁白: 教程中到现在为止我们所做的一切只是简单的表达式（值，函数等）。
  这些表达式可以输入到命令行解释器中作为快速测试，但它们不能独立存在于 Scala 
  文件。举个例子，您不能在 Scala 文件上简单的写上 "val x = 5"。相反 Scala 文件
  允许的顶级结构是：

  - objects
  - classes
  - case classes
  - traits

  现在来解释这些是什么。
*/

// 类和其他语言的类相似，构造器参数在类名后声明，初始化在类结构体中完成。
class Dog(br: String) {
  // 构造器代码在此
  var breed: String = br

  // 定义名为 bark 的方法，返回字符串
  def bark = "Woof, woof!"

  // 值和方法作用域假定为 public。"protected" 和 "private" 关键字也是可用的。
  private def sleep(hours: Int) =
    println(s"I'm sleeping for $hours hours")

  // 抽象方法是没有方法体的方法。如果取消下面那行注释，Dog 类必须被声明为 abstract
  //   abstract class Dog(...) { ... }
  // def chaseAfter(what: String): String
}

val mydog = new Dog("greyhound")
println(mydog.breed) // => "greyhound"
println(mydog.bark) // => "Woof, woof!"


// "object" 关键字创造一种类型和该类型的单例。
// Scala 的 class 常常也含有一个 “伴生对象”，class 中包含每个实例的行为，所有实例
// 共用的行为则放入 object 中。两者的区别和其他语言中类方法和静态方法类似。
// 请注意 object 和 class 可以同名。
object Dog {
  def allKnownBreeds = List("pitbull", "shepherd", "retriever")
  def createDog(breed: String) = new Dog(breed)
}


// Case 类是有额外内建功能的类。Scala 初学者常遇到的问题之一便是何时用类
// 和何时用 case 类。界线比较模糊，但通常类倾向于封装，多态和行为。类中的值
// 的作用域一般为 private ， 只有方法是暴露的。case 类的主要目的是放置不可变
// 数据。它们通常只有几个方法，且方法几乎没有副作用。
case class Person(name: String, phoneNumber: String)

// 创造新实例，注意 case 类不需要使用 "new" 关键字
val george = Person("George", "1234")
val kate = Person("Kate", "4567")

// 使用 case 类，您可以轻松得到一些功能，像 getters:
george.phoneNumber  // => "1234"

// 每个字段的相等性比较（无需覆盖 .equals）
Person("George", "1234") == Person("Kate", "1236")  // => false

// 简单的拷贝方式
// otherGeorge == Person("george", "9876")
val otherGeorge = george.copy(phoneNumber = "9876")

// 还有很多。case 类同时可以用于模式匹配，接下来会看到。


// 敬请期待 Traits ！


/////////////////////////////////////////////////
// 6. 模式匹配
/////////////////////////////////////////////////

// 模式匹配是一个强大和常用的 Scala 特性。这是用模式匹配一个 case 类的例子。
// 附注：不像其他语言， Scala 的 case 不需要 break， 其他语言中 switch 语句的
// fall-through 现象不会发生。

def matchPerson(person: Person): String = person match {
  // Then you specify the patterns:
  case Person("George", number) => "We found George! His number is " + number
  case Person("Kate", number) => "We found Kate! Her number is " + number
  case Person(name, number) => "We matched someone : " + name + ", phone : " + number
}

val email = "(.*)@(.*)".r  // 定义下一个例子会用到的正则

// 模式匹配看起来和 C语言家族的 switch 语句相似，但更为强大。
// Scala 中您可以匹配很多东西：
def matchEverything(obj: Any): String = obj match {
  // 匹配值：
  case "Hello world" => "Got the string Hello world"

  // 匹配类型：
  case x: Double => "Got a Double: " + x

  // 匹配时指定条件
  case x: Int if x > 10000 => "Got a pretty big number!"

  // 像之前一样匹配 case 类：
  case Person(name, number) => s"Got contact info for $name!"

  // 匹配正则表达式：
  case email(name, domain) => s"Got email address $name@$domain"

  // 匹配元组：
  case (a: Int, b: Double, c: String) => s"Got a tuple: $a, $b, $c"

  // 匹配数据结构：
  case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c"

  // 模式可以嵌套
  case List(List((1, 2,"YAY"))) => "Got a list of list of tuple"
}

// 事实上，你可以对任何有 "unapply" 方法的对象进行模式匹配。
// 这个特性如此强大以致于 Scala 允许定义一个函数作为模式匹配：
val patternFunc: Person => String = {
  case Person("George", number) => s"George's number: $number"
  case Person(name, number) => s"Random person's number: $number"
}


/////////////////////////////////////////////////
// 7. 函数式编程
/////////////////////////////////////////////////

// Scala 允许方法和函数作为其他方法和函数的参数和返回值。

val add10: Int => Int = _ + 10 // 一个接受一个 Int 类型参数并返回一个 Int 类型值的函数
List(1, 2, 3) map add10 // List(11, 12, 13) - add10 被应用到每一个元素

// 匿名函数可以被使用来代替有命名的函数：
List(1, 2, 3) map (x => x + 10)

// 如果匿名函数只有一个参数可以用下划线作为变量
List(1, 2, 3) map (_ + 10)

// 如果您所应用的匿名块和匿名函数都接受一个参数，那么你甚至可以省略下划线
List("Dom", "Bob", "Natalia") foreach println


// 组合子

// 译注: val sq: Int => Int = x => x * x
s.map(sq)

val sSquared = s. map(sq)

sSquared.filter(_ < 10)

sSquared.reduce (_+_)

// filter 函数接受一个 predicate （函数根据条件 A 返回 Boolean）并选择
// 所有满足 predicate 的元素
List(1, 2, 3) filter (_ > 2) // List(3)
case class Person(name:String, age:Int)
List(
  Person(name = "Dom", age = 23),
  Person(name = "Bob", age = 30)
).filter(_.age > 25) // List(Person("Bob", 30))


// Scala 的 foreach 方法定义在某些集合中，接受一个函数并返回 Unit （void 方法）
// 另请参见：
// http://www.scala-lang.org/api/current/index.html#scala.collection.IterableLike@foreach(f:A=>Unit):Unit
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println

// For 推导式

for { n <- s } yield sq(n)

val nSquared2 = for { n <- s } yield sq(n)

for { n <- nSquared2 if n < 10 } yield n

for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared

/* 注意，这些不是 for 循环，for 循环的语义是‘重复’，然而 for 推导式定义
   两个数据集合的关系。 */


/////////////////////////////////////////////////
// 8. 隐式转换
/////////////////////////////////////////////////

/* 警告 警告: 隐式转换是 Scala 中一套强大的特性，因此容易被滥用。
 * Scala 初学者在理解它们的工作原理和最佳实践之前，应抵制使用它的诱惑。
 * 我们加入这一章节仅因为它们在 Scala 的库中太过常见，导致没有用隐式转换的库
 * 就不可能做有意义的事情。这章节主要让你理解和使用隐式转换，而不是自己声明。
 */

// 可以通过 "implicit" 声明任何值（val, 函数，对象等）为隐式值，
// 请注意这些例子中，我们用到第5部分的 Dog 类。
implicit val myImplicitInt = 100
implicit def myImplicitFunction(breed: String) = new Dog("Golden " + breed)

// implicit 关键字本身不改变值的行为，所以上面的值可以照常使用。
myImplicitInt + 2  // => 102
myImplicitFunction("Pitbull").breed  // => "Golden Pitbull"

// 区别在于，当另一段代码“需要”隐式值时，这些值现在有资格作为隐式值。
// 一种情况是隐式函数参数。
def sendGreetings(toWhom: String)(implicit howMany: Int) =
  s"Hello $toWhom, $howMany blessings to you and yours!"

// 如果提供值给 “howMany”，函数正常运行
sendGreetings("John")(1000)  // => "Hello John, 1000 blessings to you and yours!"

// 如果省略隐式参数，会传一个和参数类型相同的隐式值，
// 在这个例子中， 是 “myImplicitInt":
sendGreetings("Jane")  // => "Hello Jane, 100 blessings to you and yours!"

// 隐式的函数参数使我们可以模拟其他函数式语言的 type 类（type classes）。
// 它经常被用到所以有特定的简写。这两行代码是一样的：
def foo[T](implicit c: C[T]) = ...
def foo[T : C] = ...

// 编译器寻找隐式值另一种情况是你调用方法时
//   obj.method(...)
// 但 "obj" 没有一个名为 "method" 的方法。这样的话，如果有一个参数类型为 A
// 返回值类型为 B 的隐式转换，obj 的类型是 A，B 有一个方法叫 "method" ，这样
// 转换就会被应用。所以作用域里有上面的 myImplicitFunction, 我们可以这样做：
"Retriever".breed  // => "Golden Retriever"
"Sheperd".bark  // => "Woof, woof!"

// 这里字符串先被上面的函数转换为 Dog 对象，然后调用相应的方法。
// 这是相当强大的特性，但再次提醒，请勿轻率使用。
// 事实上，当你定义上面的隐式函数时，编译器会作出警告，除非你真的了解
// 你正在做什么否则不要使用。


/////////////////////////////////////////////////
// 9. 杂项
/////////////////////////////////////////////////

// 导入类
import scala.collection.immutable.List

// 导入所有子包
import scala.collection.immutable._

// 一条语句导入多个类
import scala.collection.immutable.{List, Map}

// 使用 ‘=>’ 对导入进行重命名
import scala.collection.immutable.{ List => ImmutableList }

// 导入所有类，排除其中一些。下面的语句排除了 Map 和 Set：
import scala.collection.immutable.{Map => _, Set => _, _}

// 在 Scala 文件用 object 和单一的 main 方法定义程序入口：
object Application {
  def main(args: Array[String]): Unit = {
    // stuff goes here.
  }
}

// 文件可以包含多个 class 和 object，用 scalac 编译源文件




// 输入和输出

// 按行读文件
import scala.io.Source
for(line <- Source.fromFile("myfile.txt").getLines())
  println(line)

// 用 Java 的 PrintWriter 写文件
val writer = new PrintWriter("myfile.txt")
writer.write("Writing line for line" + util.Properties.lineSeparator)
writer.write("Another line here" + util.Properties.lineSeparator)
writer.close()

```

## 更多的资源

[为没耐心的人准备的 Scala](http://horstmann.com/scala/)

[Twitter Scala school](http://twitter.github.io/scala_school/)

[The Scala documentation](http://www.scala-lang.org/documentation/)

[在浏览器尝试 Scala](http://scalatutorials.com/tour/)

加入 [Scala 用户组](https://groups.google.com/forum/#!forum/scala-user)
---
language: "Standard ML"
contributors:
    - ["Simon Shine", "http://shine.eu.org/"]
    - ["David Pedersen", "http://lonelyproton.com/"]
    - ["James Baker", "http://www.jbaker.io/"]
    - ["Leo Zovic", "http://langnostic.inaimathi.ca/"]
filename: standard-ml-cn.html
translators:
   - ["Buqian Zheng", "https://github.com/zhengbuqian"]
lang: zh-cn
---

Standard ML是一门拥有类型推断和一些副作用的函数式编程语言。学习Standard ML的一些
难点在于：递归、模式匹配和类型推断（猜测正确的类型但是决不允许隐式类型转换）。与Haskell的
不同之处在于Standard ML拥有引用，允许对变量进行更新。

```ocaml
(* Standard ML的注释以 (* 开头，以 *) 结束。注释可以嵌套，也就意味着所有的 (* 标签都
   需要由一个 *) 结束。这条注释就是两个嵌套的注释的例子。*)

(* 一个Standard ML程序包括声明，例如值声明： *)
val rent = 1200
val phone_no = 5551337
val pi = 3.14159
val negative_number = ~15  (* 是的，一元运算符用波浪符号`~`表示 *)

(* 你当然也可以显示的声明类型，但这并不是必须的，因为ML会自动推断出值的类型。*)
val diameter = 7926 : int
val e = 2.718 : real
val name = "Bobby" : string

(* 同样重要的还有函数： *)
fun is_large(x : int) = if x > 37 then true else false

(* 浮点数被叫做实数： "real". *)
val tau = 2.0 * pi         (* 两个real可以相乘 *)
val twice_rent = 2 * rent  (* 两个int也可以相乘 *)
(* val meh = 1.25 * 10 *)  (* 但是你不能让int和real相乘。 *)
val yeh = 1.25 * (Real.fromInt 10) (* ...除非你显示的把一个转换为另一个*)

(* +, - 和 * 被重载过，所以可以作用于int和real。*)
(* 但是除法有单独的运算符： *)
val real_division = 14.0 / 4.0  (* 结果是 3.5 *)
val int_division  = 14 div 4    (* 结果是 3， 向下取整 *)
val int_remainder = 14 mod 4    (* 结果是 2， 因为 3*4 = 12 *)

(* ~ 有时其实是函数 (比如被放在变量前面的时候) *)
val negative_rent = ~(rent)  (* 即使rent是"real"也正确 *)

(* 当然也有布尔值和相关的运算符 *)
val got_milk = true
val got_bread = false
val has_breakfast = got_milk andalso got_bread  (* 'andalso' 是运算符 *)
val has_something = got_milk orelse got_bread   (* 'orelse' 是运算符 *)
val is_sad = not(has_something)                 (* not 是一个函数 *)

(* 很多值都可以用判等性运算符进行比较： = 和 <> *)
val pays_same_rent = (rent = 1300)  (* false *)
val is_wrong_phone_no = (phone_no <> 5551337)  (* false *)

(* <> 运算符就是其他大部分语言里的 != 。 *)
(* 'andalso' 和 'orelse' 在很多其他语言里被叫做 && 和 || 。 *)

(* 实际上，上面大部分的圆括号都是不需要的。比如表达上面内容的一些不同的方式： *)
fun is_large x = x > 37  
val is_sad = not has_something
val pays_same_rent = rent = 1300  (* 看起来很奇怪，但是就是这样的。 *)
val is_wrong_phone_no = phone_no <> 5551337
val negative_rent = ~rent  (* ~ rent (注意空格) 也正确 *)

(* 圆括号大部分时候用来把东西们组合在一起 *)
val some_answer = is_large (5 + 5)      (* 没有圆括号的话会出错！ *)
(* val some_answer = is_large 5 + 5 *)  (* 会被理解为： (is_large 5) + 5. 错了！ *)

(* 除了boolean, int, real，Standard ML也有char和string *)
val foo = "Hello, World!\n"  (* \n是换行的转移字符 *)
val one_letter = #"a"        (* 这种酷炫的语法表示一个字符a *)

val combined = "Hello " ^ "there, " ^ "fellow!\n"  (* 拼接字符串 *)

val _ = print foo       (* 你可以打印一些东西，这儿我们队打印的结果并不感兴趣，因此 *)
val _ = print combined  (* 用 _ 把结果丢掉了 *)
(* val _ = print one_letter *)  (* 只有字符串才能被这样打印 *)


val bar = [ #"H", #"e", #"l", #"l", #"o" ]  (* SML 也有列表！ *)
(* val _ = print bar *)  (* 然而列表和string是不同的 *)

(* 当然这二者可以相互转换。String是一个库，implode和size是这个库里接受string作为
   参数的函数。*)
val bob = String.implode bar          (* 结果是 "Hello" *)
val bob_char_count = String.size bob  (* 结果是 5 *)
val _ = print (bob ^ "\n")            (* 为了好看加了个换行符 *)

(* 列表可以包含任意类型的元素 *)
val numbers = [1, 3, 3, 7, 229, 230, 248]  (* : int list *)
val names = [ "Fred", "Jane", "Alice" ]    (* : string list *)

(* 列表甚至可以包含列表！ *)
val groups = [ [ "Alice", "Bob" ],
               [ "Huey", "Dewey", "Louie" ],
               [ "Bonnie", "Clyde" ] ]     (* : string list list *)

val number_count = List.length numbers     (* 结果是 7 *)

(* 你可以使用 :: 操作符把单个值放到同样类型列表的最前面。
   :: 叫做con操作符（名字来自Lisp） *)
val more_numbers = 13 :: numbers  (* 结果是 [13, 1, 3, 3, 7, ...] *)
val more_groups  = ["Batman","Superman"] :: groups

(* 拥有同样类型元素的列表可以使用 @ 操作符连接起来 *)
val guest_list = [ "Mom", "Dad" ] @ [ "Aunt", "Uncle" ]

(* 使用 :: 操作符也能完成这项工作。但是这有点绕，因为左手边必须是单个元素
   而右边必须是这种元素的列表 *)
val guest_list = "Mom" :: "Dad" :: [ "Aunt", "Uncle" ]
val guest_list = "Mom" :: ("Dad" :: ("Aunt" :: ("Uncle" :: [])))

(* 如果你有很多同样类型的列表，也可以整个拼接成一个。 *)
val everyone = List.concat groups  (* [ "Alice", "Bob", "Huey", ... ] *)

(* 列表可以包含任意（无限）数量的元素 *)
val lots = [ 5, 5, 5, 6, 4, 5, 6, 5, 4, 5, 7, 3 ]  (* still just an int list *)

(* 但是列表只能包含一种类型的元素 *)
(* val bad_list = [ 1, "Hello", 3.14159 ] : ??? list *)

(* 而元组Tuples则可以包含有限固定数量的不同类型的元素 *)
val person1 = ("Simon", 28, 3.14159)  (* : string * int * real *)

(* 你甚至可以让列表和元组相互嵌套 *)
val likes = [ ("Alice", "ice cream"),
              ("Bob",   "hot dogs"),
              ("Bob",   "Alice") ]     (* : (string * string) list *)

val mixup = [ ("Alice", 39),
              ("Bob",   37),
              ("Eve",   41) ]  (* : (string * int) list *)

val good_bad_stuff =
  (["ice cream", "hot dogs", "chocolate"],
   ["liver", "paying the rent" ])           (* : string list * string list *)

(* 记录Record是每个位置带名字的元组 *)

val rgb = { r=0.23, g=0.56, b=0.91 } (* : {b:real, g:real, r:real} *)

(* 使用Record时不需要提前声明每个位置的名字。 有不同名字的Record属于不同的类型
   即使他们的值的类型是相同的。比如说：*)
val Hsl = { H=310.3, s=0.51, l=0.23 } (* : {H:real, l:real, s:real} *)
val Hsv = { H=310.3, s=0.51, v=0.23 } (* : {H:real, s:real, v:real} *)

(* ...如果你想判断 `Hsv = Hsl` 或者 `rgb = Hsl` 的话，会得到一个类型错误。虽然他们都包含3个
   real，但是由于名字不同，其类型也不同。 *)

(* 可以使用 # 符号取出元组的值 *)

val H = #H Hsv (* : real *)
val s = #s Hsl (* : real *)

(* 函数！ *)
fun add_them (a, b) = a + b    (* 一个简单的加法函数 *)
val test_it = add_them (3, 4)  (* 结果是 7 *)

(* 复杂函数通常会为了可读性写成多行 *)
fun thermometer temp =
    if temp < 37
    then "Cold"
    else if temp > 37
         then "Warm"
         else "Normal"

val test_thermo = thermometer 40  (* 结果是 "Warm" *)

(* if 实际上是表达式而不是声明。一个函数体只可以包含一个表达式。但是还是有一些小技巧
   让一个函数做更多的事。 *)

(* 函数也可以使用调用自己的结果 (递归！) *)
fun fibonacci n =
    if n = 0 then 0 else                   (* 终止条件 *)
    if n = 1 then 1 else                   (* 终止条件 *)
    fibonacci (n - 1) + fibonacci (n - 2)  (* 递归 *)

(* 有的时候，手写出递归函数的执行过程能帮助理解递归概念：

 fibonacci 4
   ~> fibonacci (4 - 1) + fibonacci (4 - 2)
   ~> fibonacci 3 + fibonacci 2
   ~> (fibonacci (3 - 1) + fibonacci (3 - 2)) + fibonacci 2
   ~> (fibonacci 2 + fibonacci 1) + fibonacci 2
   ~> ((fibonacci (2 - 1) + fibonacci (2 - 2)) + fibonacci 1) + fibonacci 2
   ~> ((fibonacci 1 + fibonacci 0) + fibonacci 1) + fibonacci 2
   ~> ((1 + fibonacci 0) + fibonacci 1) + fibonacci 2
   ~> ((1 + 0) + fibonacci 1) + fibonacci 2
   ~> (1 + fibonacci 1) + fibonacci 2
   ~> (1 + 1) + fibonacci 2
   ~> 2 + fibonacci 2
   ~> 2 + (fibonacci (2 - 1) + fibonacci (2 - 2))
   ~> 2 + (fibonacci (2 - 1) + fibonacci (2 - 2))
   ~> 2 + (fibonacci 1 + fibonacci 0)
   ~> 2 + (1 + fibonacci 0)
   ~> 2 + (1 + 0)
   ~> 2 + 1
   ~> 3  第四个斐波那契数

 *)

(* 函数不能改变它引用的值。它只能暂时的使用同名的新变量来覆盖这个值。也就是说，变量其实是
   常数，只有在递归的时候才表现的比较像变量。因此，变量也被叫做值绑定。举个例子： *)

val x = 42
fun answer(question) =
    if question = "What is the meaning of life, the universe and everything?"
    then x
    else raise Fail "I'm an exception. Also, I don't know what the answer is."
val x = 43
val hmm = answer "What is the meaning of life, the universe and everything?"
(* 现在 hmm 的值是 42。  这是因为函数 answer 引用的x是函数定义之前的x。 *)

(* 函数通过接受一个元组来接受多个参数。 *)
fun solve2 (a : real, b : real, c : real) =
    ((~b + Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a),
     (~b - Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a))

(* 有时候同样的计算会被计算多次，因此把结果保存下来以重复使用是很有必要的。
   这时可以使用 let 绑定。 *)
fun solve2 (a : real, b : real, c : real) =
    let val discr  = b * b - 4.0 * a * c
        val sqr = Math.sqrt discr
        val denom = 2.0 * a
    in ((~b + sqr) / denom,
        (~b - sqr) / denom)
    end

(* 模式匹配是函数式编程的一个精巧的部分，它是实现 if 的另一种方式。  
   斐波那契函数可以被重写为如下方式： *)
fun fibonacci 0 = 0  (* 终止条件 *)
  | fibonacci 1 = 1  (* 终止条件 *)
  | fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)  (* 递归 *)

(* 模式匹配也可以用于比如元组、列表和记录的复合类型。"fun solve2 (a, b, c) = ..."
   的写法实际上也是对于一个三元素元组的模式匹配。类似但是比较不直观的是你也可以从列表的开头
   对列表元素进行匹配。 *)
fun first_elem (x::xs) = x
fun second_elem (x::y::xs) = y
fun evenly_positioned_elems (odd::even::xs) = even::evenly_positioned_elems xs
  | evenly_positioned_elems [odd] = []  (* 终止条件：丢弃结果 *)
  | evenly_positioned_elems []    = []  (* 终止条件 *)

(* 匹配记录的时候，比如使用每个位置的名字，每个位置的值都需要绑定，但是顺序并不重要。 *)

fun rgbToTup {r, g, b} = (r, g, b)    (* fn : {b:'a, g:'b, r:'c} -> 'c * 'b * 'a *)
fun mixRgbToTup {g, b, r} = (r, g, b) (* fn : {b:'a, g:'b, r:'c} -> 'c * 'b * 'a *)

(* 如果传入参数 {r=0.1, g=0.2, b=0.3}，上面的两个函数都会返回 (0.1, 0.2, 0.3)。
   但是传入参数 {r=0.1, g=0.2, b=0.3, a=0.4} 的话则会得到类型错误 *)

(* 高阶函数： 可以接受其他函数作为参数的函数
   函数只不过是另一种类型的值，不需要依附与一个名字而存在。
   没有名字的函数被叫做匿名函数或者lambda表达式或者闭包（因为匿名函数也依赖于词法作用域）*)
val is_large = (fn x => x > 37)
val add_them = fn (a,b) => a + b
val thermometer =
    fn temp => if temp < 37
               then "Cold"
               else if temp > 37
                    then "Warm"
                    else "Normal"

(* 下面的代码就是用了匿名函数，结果是 "ColdWarm" *)
val some_result = (fn x => thermometer (x - 5) ^ thermometer (x + 5)) 37

(* 这是一个作用于列表的高阶函数 *)
(* map f l
       把f从左至右作用于l的每一个元素，并返回结果组成的列表。 *)
val readings = [ 34, 39, 37, 38, 35, 36, 37, 37, 37 ]  (* 先定义一个列表 *)
val opinions = List.map thermometer readings (* 结果是 [ "Cold", "Warm", ... ] *)

(* filter 函数用于筛选列表 *)
val warm_readings = List.filter is_large readings  (* 结果是 [39, 38] *)

(* 你也可以创建自己的高阶函数。函数也可以通过 curry 来接受多个参数。
   从语法上来说，curry就是使用空格来分隔参数，而不是逗号和括号。 *)
fun map f [] = []
  | map f (x::xs) = f(x) :: map f xs

(* map 的类型是 ('a -> 'b) -> 'a list -> 'b list ，这就是多态。 *)
(* 'a 被叫做类型变量 *)


(* 函数可以被声明为中缀的。 *)
val plus = add_them   (* plus 现在和 add_them 是同一个函数。 *)
infix plus            (* plus 现在是一个中缀操作符。 *)
val seven = 2 plus 5  (* seven 现在被绑定上了 7 *)

(* 函数也可以在声明之前就声明为中缀 *)
infix minus
fun x minus y = x - y (* 这样有点不容易判断哪个是参数。 *)
val four = 8 minus 4  (* four 现在被绑定上了 4 *)

(* 中缀函数/操作符也可以使用 'op' 函数变回前缀函数。 *)
val n = op + (5, 5)   (* n is now 10 *)

(* 'op' 在结合高阶函数的时候非常有用，因为高阶函数接受的是函数而不是操作符作为参数。
   大部分的操作符其实都是中缀函数。 *)
(* foldl f init [x1, x2, ..., xn]
       返回
       f(xn, ...f(x2, f(x1, init))...)
       或者如果列表为空时返回 init *)
val sum_of_numbers = foldl op+ 0 [1, 2, 3, 4, 5]


(* 可以很方便的使用 datatype 定义或简单或复杂的数据结构。 *)
datatype color = Red | Green | Blue

(* 这个函数接受 color 之一作为参数。 *)
fun say(col) =
    if col = Red then "You are red!" else
    if col = Green then "You are green!" else
    if col = Blue then "You are blue!" else
    raise Fail "Unknown color"

val _ = print (say(Red) ^ "\n")

(* datatype 经常和模式匹配一起使用。 *)
fun say Red   = "You are red!"
  | say Green = "You are green!"
  | say Blue  = "You are blue!"
  | say _     = raise Fail "Unknown color"


(* 一个二叉树 datatype *)
datatype 'a btree = Leaf of 'a
                  | Node of 'a btree * 'a * 'a btree (* 三个参数的构造器 *)

(* 一颗二叉树： *)
val myTree = Node (Leaf 9, 8, Node (Leaf 3, 5, Leaf 7))

(* 画出来应该是这个样子：

           8
          / \
 leaf -> 9   5
            / \
   leaf -> 3   7 <- leaf
 *)

(* 这个函数计算所有节点值的和。 *)
fun count (Leaf n) = n
  | count (Node (leftTree, n, rightTree)) = count leftTree + n + count rightTree

val myTreeCount = count myTree  (* myTreeCount is now bound to 32 *)


(* 异常！ *)
(* 使用关键字 'raise' 来抛出异常： *)
fun calculate_interest(n) = if n < 0.0
                            then raise Domain
                            else n * 1.04

(* 使用 "handle" 关键字来处理异常 *)
val balance = calculate_interest ~180.0
              handle Domain => ~180.0    (* x 现在的值是 ~180.0 *)

(* 某些异常还包含额外信息 *)
(* 一些内建异常的例子： *)
fun failing_function []    = raise Empty  (* 空列表异常 *)
  | failing_function [x]   = raise Fail "This list is too short!"
  | failing_function [x,y] = raise Overflow  (* 用作计算 *)
  | failing_function xs    = raise Fail "This list is too long!"

(* 使用 'handle' 时也可以使用模式匹配来保证异常都被处理。 *)
val err_msg = failing_function [1,2] handle Fail _ => "Fail was raised"
                                          | Domain => "Domain was raised"
                                          | Empty  => "Empty was raised"
                                          | _      => "Unknown exception"

(* err_msg 的值会是 "Unknown exception" 
   因为 Overflow 没有在模式中列出，因此匹配到了通配符_。 *)

(* 我们也可以定义自己的异常 *)
exception MyException
exception MyExceptionWithMessage of string
exception SyntaxError of string * (int * int)

(* 文件读写！ *)
(* 把一首诗写进文件： *)
fun writePoem(filename) =
    let val file = TextIO.openOut(filename)
        val _ = TextIO.output(file, "Roses are red,\nViolets are blue.\n")
        val _ = TextIO.output(file, "I have a gun.\nGet in the van.\n")
    in TextIO.closeOut(file)
    end

(* 把一首诗读进一个字符串列表： *)
fun readPoem(filename) =
    let val file = TextIO.openIn filename
        val poem = TextIO.inputAll file
        val _ = TextIO.closeIn file
    in String.tokens (fn c => c = #"\n") poem
    end

val _ = writePoem "roses.txt"
val test_poem = readPoem "roses.txt"  (* gives [ "Roses are red,",
                                                 "Violets are blue.",
                                                 "I have a gun.",
                                                 "Get in the van." ] *)

(* 我们还可以创建指向值的引用，引用可以被更新。 *)
val counter = ref 0 (* 使用 ref 函数创建一个引用。 *)

(* 使用赋值运算符给引用复制 *)
fun set_five reference = reference := 5

(* 使用解引用运算符得到引用的值 *)
fun equals_five reference = !reference = 5

(* 递归很复杂的时候，也可以使用 while 循环 *)
fun decrement_to_zero r = if !r < 0
                          then r := 0
                          else while !r >= 0 do r := !r - 1

(* 这将会返回 unit （也就是什么都没有，一个0元素的元组） *)

(* 要返回值，可以使用分号来分开表达式。 *)
fun decrement_ret x y = (x := !x - 1; y)
```

## 阅读更多

* 安装交互式编译器 (REPL)，如：
  [Poly/ML](http://www.polyml.org/),
  [Moscow ML](http://mosml.org),
  [SML/NJ](http://smlnj.org/).
* 上Coursera上的课程 [Programming Languages](https://www.coursera.org/course/proglang).
* 购买 Larry C. Paulson 写的 *ML for the Working Programmer* 书。
* 使用 [StackOverflow's sml 标签](http://stackoverflow.com/questions/tagged/sml).
---
language: swift
filename: learnswift-cn.swift
contributors:
  - ["Grant Timmerman", "http://github.com/grant"]
translators:
  - ["Xavier Yao", "http://github.com/xavieryao"]
  - ["Joey Huang", "http://github.com/kamidox"]
  - ["CY Lim", "http://github.com/cylim"]
lang: zh-cn
---

Swift 是 Apple 开发的用于 iOS 和 OS X 开发的编程语言。Swift 于2014年 Apple WWDC （全球开发者大会）中被引入，用以与 Objective-C 共存，同时对错误代码更具弹性。Swift 由 Xcode 6 beta 中包含的 LLVM 编译器编译。

Swift 的官方语言教程 [Swift Programming Language](https://itunes.apple.com/us/book/swift-programming-language/id881256329) 可以从 iBooks 免费下载.

亦可参阅：Apple's [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/) ——一个完整的Swift 教程

```swift
// 导入外部模块
import UIKit

//
// MARK: 基础
//

// XCODE 支持给注释代码作标记，这些标记会列在 XCODE 的跳转栏里，支持的标记为
// MARK: 普通标记
// TODO: TODO 标记
// FIXME: FIXME 标记

// Swift2.0 println() 及 print() 已经整合成 print()。
print("Hello, world") // 这是原本的 println()，会自动进入下一行
print("Hello, world", terminator: "") // 如果不要自动进入下一行，需设定结束符为空串

// 变量 (var) 的值设置后可以随意改变
// 常量 (let) 的值设置后不能改变
var myVariable = 42
let øπΩ = "value" // 可以支持 unicode 变量名
let π = 3.1415926
let myConstant = 3.1415926
let explicitDouble: Double = 70   // 明确指定变量类型为 Double ，否则编译器将自动推断变量类型
let weak = "keyword"; let override = "another keyword" // 语句之间可以用分号隔开，语句未尾不需要分号
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "some text " + String(myVariable) // 类型转换
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // 格式化字符串

// 条件编译
// 使用 -D 定义编译开关
#if false
    print("Not printed")
    let buildValue = 3
#else
    let buildValue = 7
#endif
print("Build value: \(buildValue)") // Build value: 7

/*
    Optionals 是 Swift 的新特性，它允许你存储两种状态的值给 Optional 变量：有效值或 None 。
    可在值名称后加个问号 （？） 来表示这个值是 Optional。

    Swift 要求所有的 Optinal 属性都必须有明确的值，如果为空，则必须明确设定为 nil

    Optional<T> 是个枚举类型
*/
var someOptionalString: String? = "optional" // 可以是 nil
// 下面的语句和上面完全等价，上面的写法更推荐，因为它更简洁，问号 (?) 是 Swift 提供的语法糖
var someOptionalString2: Optional<String> = "optional"

if someOptionalString != nil {
    // 变量不为空
    if someOptionalString!.hasPrefix("opt") {
        print("has the prefix")
    }

    let empty = someOptionalString?.isEmpty
}
someOptionalString = nil

/*
    使用 （！） 可以解决无法访问optional值的运行错误。若要使用 （！）来强制解析，一定要确保 Optional 里不是 nil参数。
*/

// 显式解包 optional 变量
var unwrappedString: String! = "Value is expected."
// 下面语句和上面完全等价，感叹号 (!) 是个后缀运算符，这也是个语法糖
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Value is expected."

if let someOptionalStringConstant = someOptionalString {
    // 由于变量 someOptinalString 有值，不为空，所以 if 条件为真
    if !someOptionalStringConstant.hasPrefix("ok") {
        // does not have the prefix
    }
}

// Swift 支持可保存任何数据类型的变量
// AnyObject == id
// 和 Objective-C `id` 不一样, AnyObject 可以保存任何类型的值 (Class, Int, struct, 等)
var anyObjectVar: AnyObject = 7
anyObjectVar = "Changed value to a string, not good practice, but possible."

/*
    这里是注释

    /*
        支持嵌套的注释
    */
*/


//
// Mark: 数组与字典（关联数组）
//

/*
    Array 和 Dictionary 是结构体，不是类，他们作为函数参数时，是用值传递而不是指针传递。
    可以用 `var` 和 `let` 来定义变量和常量。
*/

// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
let emptyArray = [String]() // 使用 let 定义常量，此时 emptyArray 数组不能添加或删除内容
let emptyArray2 = Array<String>() // 与上一语句等价，上一语句更常用
var emptyMutableArray = [String]() // 使用 var 定义变量，可以向 emptyMutableArray 添加数组元素
var explicitEmptyMutableStringArray: [String] = [] // 与上一语句等价

// 字典
var occupations = [
    "Malcolm": "Captain",
    "kaylee": "Mechanic"
]
occupations["Jayne"] = "Public Relations"   // 修改字典，如果 key 不存在，自动添加一个字典元素
let emptyDictionary = [String: Float]() // 使用 let 定义字典常量，字典常量不能修改里面的值
let emptyDictionary2 = Dictionary<String, Float>() // 与上一语句类型等价，上一语句更常用
var emptyMutableDictionary = [String: Float]() // 使用 var 定义字典变量
var explicitEmptyMutableDictionary: [String: Float] = [:] // 与上一语句类型等价


//
// MARK: 控制流
//

// 数组的 for 循环
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
    if value == 1 {
        print("One!")
    } else {
        print("Not one!")
    }
}

// 字典的 for 循环
var dict = ["one": 1, "two": 2]
for (key, value) in dict {
    print("\(key): \(value)")
}

// 区间的 loop 循环：其中 `...` 表示闭环区间，即[-1, 3]；`..<` 表示半开闭区间，即[-1,3)
for i in -1...shoppingList.count {
    print(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// 可以使用 `..<` 来去掉最后一个元素

// while 循环
var i = 1
while i < 1000 {
    i *= 2
}

// repeat-while 循环
repeat {
    print("hello")
} while 1 == 2

// Switch 语句
// Swift 里的 Switch 语句功能异常强大，结合枚举类型，可以实现非常简洁的代码，可以把 switch 语句想象成 `if` 的语法糖
// 它支持字符串，类实例或原生数据类型 (Int, Double, etc)
let vegetable = "red pepper"
switch vegetable {
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
case let localScopeValue where localScopeValue.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(localScopeValue)?"
default: // 在 Swift 里，switch 语句的 case 必须处理所有可能的情况，如果 case 无法全部处理，则必须包含 default语句
    let vegetableComment = "Everything tastes good in soup."
}


//
// MARK: 函数
//

// 函数是一个 first-class 类型，他们可以嵌套，可以作为函数参数传递

// 函数文档可使用 reStructedText 格式直接写在函数的头部
/**
    A greet operation

    - A bullet in docs
    - Another bullet in the docs

    :param: name A name
    :param: day A day
    :returns: A string containing the name and day value.
*/
func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", day: "Tuesday")

// 第一个参数表示外部参数名和内部参数名使用同一个名称。
// 第二个参数表示外部参数名使用 `externalParamName` ，内部参数名使用 `localParamName`
func greet2(requiredName requiredName: String, externalParamName localParamName: String) -> String {
    return "Hello \(requiredName), the day is \(localParamName)"
}
greet2(requiredName:"John", externalParamName: "Sunday")    // 调用时，使用命名参数来指定参数的值

// 函数可以通过元组 (tuple) 返回多个值
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// 通过下划线 (_) 来忽略不关心的值
let (_, price1, _) = pricesTuple // price1 == 3.69
print(price1 == pricesTuple.1) // true
print("Gas price: \(price)")

// 可变参数
func setup(numbers: Int...) {
    // 可变参数是个数组
    let _ = numbers[0]
    let _ = numbers.count
}

// 函数变量以及函数作为返回值返回
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

// 强制进行指针传递 (引用传递)，使用 `inout` 关键字修饰函数参数
func swapTwoInts(inout a: Int, inout b: Int) {
    let tempA = a
    a = b
    b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(&someIntA, b: &someIntB)
print(someIntB) // 7


//
// MARK: 闭包
//
var numbers = [1, 2, 6]

// 函数是闭包的一个特例 ({})

// 闭包实例
// `->` 分隔了闭包的参数和返回值
// `in` 分隔了闭包头 (包括参数及返回值) 和闭包体
// 下面例子中，`map` 的参数是一个函数类型，它的功能是把数组里的元素作为参数，逐个调用 `map` 参数传递进来的函数。
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})

// 当闭包的参数类型和返回值都是己知的情况下，且只有一个语句作为其返回值时，我们可以简化闭包的写法
numbers = numbers.map({ number in 3 * number })
// 我们也可以使用 $0, $1 来指代第 1 个，第 2 个参数，上面的语句最终可简写为如下形式
// numbers = numbers.map({ $0 * 3 })

print(numbers) // [3, 6, 18]

// 简洁的闭包
numbers = numbers.sort { $0 > $1 }

print(numbers) // [18, 6, 3]


//
// MARK: 结构体
//

// 结构体和类非常类似，可以有属性和方法

struct NamesTable {
    let names: [String]

    // 自定义下标运算符
    subscript(index: Int) -> String {
        return names[index]
    }
}

// 结构体有一个自动生成的隐含的命名构造函数
let namesTable = NamesTable(names: ["Me", "Them"])
let name = namesTable[1]
print("Name is \(name)") // Name is Them

//
// MARK: 类
//

// 类和结构体的有三个访问控制级别，他们分别是 internal (默认), public, private
// internal: 模块内部可以访问
// public: 其他模块可以访问
// private: 只有定义这个类或结构体的源文件才能访问

public class Shape {
    public func getArea() -> Int {
        return 0;
    }
}

// 类的所有方法和属性都是 public 的
// 如果你只是需要把数据保存在一个结构化的实例里面，应该用结构体

internal class Rect: Shape {
    // 值属性 (Stored properties)
    var sideLength: Int = 1

    // 计算属性 (Computed properties)
    private var perimeter: Int {
        get {
            return 4 * sideLength
        }
        set {
            // `newValue` 是个隐含的变量，它表示将要设置进来的新值
            sideLength = newValue / 4
        }
    }

    // 延时加载的属性，只有这个属性第一次被引用时才进行初始化，而不是定义时就初始化
    // subShape 值为 nil ，直到 subShape 第一次被引用时才初始化为一个 Rect 实例
    lazy var subShape = Rect(sideLength: 4)

    // 监控属性值的变化。
    // 当我们需要在属性值改变时做一些事情，可以使用 `willSet` 和 `didSet` 来设置监控函数
    // `willSet`: 值改变之前被调用
    // `didSet`: 值改变之后被调用
    var identifier: String = "defaultID" {
        // `willSet` 的参数是即将设置的新值，参数名可以指定，如果没有指定，就是 `newValue`
        willSet(someIdentifier) {
            print(someIdentifier)
        }
        // `didSet` 的参数是已经被覆盖掉的旧的值，参数名也可以指定，如果没有指定，就是 `oldValue`
        didSet {
            print(oldValue)
        }
    }

    // 命名构造函数 (designated inits)，它必须初始化所有的成员变量，
    // 然后调用父类的命名构造函数继续初始化父类的所有变量。
    init(sideLength: Int) {
        self.sideLength = sideLength
        // 必须显式地在构造函数最后调用父类的构造函数 super.init
        super.init()
    }

    func shrink() {
        if sideLength > 0 {
            sideLength -= 1
        }
    }

    // 函数重载使用 override 关键字
    override func getArea() -> Int {
        return sideLength * sideLength
    }
}

// 类 `Square` 从 `Rect` 继承
class Square: Rect {
    // 便捷构造函数 (convenience inits) 是调用自己的命名构造函数 (designated inits) 的构造函数
    // Square 自动继承了父类的命名构造函数
    convenience init() {
        self.init(sideLength: 5)
    }
    // 关于构造函数的继承，有以下几个规则：
    // 1. 如果你没有实现任何命名构造函数，那么你就继承了父类的所有命名构造函数
    // 2. 如果你重载了父类的所有命名构造函数，那么你就自动继承了所有的父类快捷构造函数
    // 3. 如果你没有实现任何构造函数，那么你继承了父类的所有构造函数，包括命名构造函数和便捷构造函数
}

var mySquare = Square()
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4

// 类型转换
let aShape = mySquare as Shape

// 使用三个等号来比较是不是同一个实例
if mySquare === aShape {
    print("Yep, it's mySquare")
}

class Circle: Shape {
    var radius: Int
    override func getArea() -> Int {
        return 3 * radius * radius
    }

    // optional 构造函数，可能会返回 nil
    init?(radius: Int) {
        self.radius = radius
        super.init()

        if radius <= 0 {
            return nil
        }
    }
}

// 根据 Swift 类型推断，myCircle 是 Optional<Circle> 类型的变量
var myCircle = Circle(radius: 1)
print(myCircle?.getArea())    // Optional(3)
print(myCircle!.getArea())    // 3
var myEmptyCircle = Circle(radius: -1)
print(myEmptyCircle?.getArea())    // "nil"
if let circle = myEmptyCircle {
    // 此语句不会输出，因为 myEmptyCircle 变量值为 nil
    print("circle is not nil")
}


//
// MARK: 枚举
//

// 枚举可以像类一样，拥有方法

enum Suit {
    case Spades, Hearts, Diamonds, Clubs
    func getIcon() -> String {
        switch self {
        case .Spades: return "♤"
        case .Hearts: return "♡"
        case .Diamonds: return "♢"
        case .Clubs: return "♧"
        }
    }
}

// 当变量类型明确指定为某个枚举类型时，赋值时可以省略枚举类型
var suitValue: Suit = .Hearts

// 非整型的枚举类型需要在定义时赋值
enum BookName: String {
    case John = "John"
    case Luke = "Luke"
}
print("Name: \(BookName.John.rawValue)")

// 与特定数据类型关联的枚举
enum Furniture {
    // 和 Int 型数据关联的枚举记录
    case Desk(height: Int)
    // 和 String, Int 关联的枚举记录
    case Chair(brand: String, height: Int)

    func description() -> String {
        switch self {
        case .Desk(let height):
            return "Desk with \(height) cm"
        case .Chair(let brand, let height):
            return "Chair of \(brand) with \(height) cm"
        }
    }
}

var desk: Furniture = .Desk(height: 80)
print(desk.description())     // "Desk with 80 cm"
var chair = Furniture.Chair(brand: "Foo", height: 40)
print(chair.description())    // "Chair of Foo with 40 cm"


//
// MARK: 协议
// 与 Java 的 interface 类似
//

// 协议可以让遵循同一协议的类型实例拥有相同的属性，方法，类方法，操作符或下标运算符等
// 下面代码定义一个协议，这个协议包含一个名为 enabled 的计算属性且包含 buildShape 方法
protocol ShapeGenerator {
    var enabled: Bool { get set }
    func buildShape() -> Shape
}

// 协议声明时可以添加 @objc 前缀，添加 @objc 前缀后，
// 可以使用 is, as, as? 等来检查协议兼容性
// 需要注意，添加 @objc 前缀后，协议就只能被类来实现，
// 结构体和枚举不能实现加了 @objc 的前缀
// 只有添加了 @objc 前缀的协议才能声明 optional 方法
// 一个类实现一个带 optional 方法的协议时，可以实现或不实现这个方法
// optional 方法可以使用 optional 规则来调用
@objc protocol TransformShape {
    optional func reshape()
    optional func canReshape() -> Bool
}

class MyShape: Rect {
    var delegate: TransformShape?

    func grow() {
        sideLength += 2

        // 在 optional 属性，方法或下标运算符后面加一个问号，可以优雅地忽略 nil 值，返回 nil。
        // 这样就不会引起运行时错误 (runtime error)
        if let reshape = self.delegate?.canReshape?() where reshape {
            // 注意语句中的问号
            self.delegate?.reshape?()
        }
    }
}


//
// MARK: 其它
//

// 扩展: 给一个已经存在的数据类型添加功能

// 给 Square 类添加 `CustomStringConvertible` 协议的实现，现在其支持 `CustomStringConvertible` 协议
extension Square: CustomStringConvertible {
    var description: String {
        return "Area: \(self.getArea()) - ID: \(self.identifier)"
    }
}

print("Square: \(mySquare)")  // Area: 16 - ID: defaultID

// 也可以给系统内置类型添加功能支持
extension Int {
    var customProperty: String {
        return "This is \(self)"
    }

    func multiplyBy(num: Int) -> Int {
        return num * self
    }
}

print(7.customProperty) // "This is 7"
print(14.multiplyBy(3)) // 42

// 泛型: 和 Java 及 C# 的泛型类似，使用 `where` 关键字来限制类型。
// 如果只有一个类型限制，可以省略 `where` 关键字
func findIndex<T: Equatable>(array: [T], _ valueToFind: T) -> Int? {
    for (index, value) in array.enumerate() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let foundAtIndex = findIndex([1, 2, 3, 4], 3)
print(foundAtIndex == 2) // true

// 自定义运算符:
// 自定义运算符可以以下面的字符打头:
//      / = - + * % < > ! & | ^ . ~
// 甚至是 Unicode 的数学运算符等
prefix operator !!! {}

// 定义一个前缀运算符，使矩形的边长放大三倍
prefix func !!! (inout shape: Square) -> Square {
    shape.sideLength *= 3
    return shape
}

// 当前值
print(mySquare.sideLength) // 4

// 使用自定义的 !!! 运算符来把矩形边长放大三倍
!!!mySquare
print(mySquare.sideLength) // 12

// 运算符也可以是泛型
infix operator <-> {}
func <-><T: Equatable> (inout a: T, inout b: T) {
    let c = a
    a = b
    b = c
}

var foo: Float = 10
var bar: Float = 20

foo <-> bar
print("foo is \(foo), bar is \(bar)") // "foo is 20.0, bar is 10.0"

```
---
category: tool
tool: tmux
filename: LearnTmux-cn.txt
contributors:
    - ["mdln", "https://github.com/mdln"]
translators:
    - ["Arnie97", "https://github.com/Arnie97"]
lang: zh-cn
---


[tmux](http://tmux.github.io)是一款终端复用工具。
在它的帮助下，你可以在同一个控制台上建立、访问并控制多个终端。
你可以断开与一个 tmux 终端的连接，此时程序将在后台运行，
当你需要时，可以随时重新连接到这个终端。

```

  tmux [command]     # 运行一条命令
                     # 如果单独使用 'tmux' 而不指定某个命令，将会建立一个新的会话

    new              # 创建一个新的会话
     -s "Session"    # 创建一个会话，并命名为“Session”
     -n "Window"     # 创建一个窗口，并命名为“Window”
     -c "/dir"       # 在指定的工作目录中启动会话

    attach           # 连接到上一次的会话（如果可用）
     -t "#"          # 连接到指定的会话
     -d              # 断开其他客户端的会话

    ls               # 列出打开的会话
     -a              # 列出所有打开的会话

    lsw              # 列出窗口
     -a              # 列出所有窗口
     -s              # 列出会话中的所有窗口

    lsp              # 列出窗格
     -a              # 列出所有窗格
     -s              # 列出会话中的所有窗格
     -t "#"          # 列出指定窗口中的所有窗格

    kill-window      # 关闭当前窗口
     -t "#"          # 关闭指定的窗口
     -a              # 关闭所有窗口
     -a -t "#"       # 关闭除指定窗口以外的所有窗口

    kill-session     # 关闭当前会话
     -t "#"          # 关闭指定的会话
     -a              # 关闭所有会话
     -a -t "#"       # 关闭除指定会话以外的所有会话

```


### 快捷键

通过“前缀”快捷键，可以控制一个已经连入的 tmux 会话。

```
----------------------------------------------------------------------
  (C-b) = Ctrl + b    # 在使用下列快捷键之前，需要按这个“前缀”快捷键

  (M-1) = Meta + 1 或 Alt + 1
----------------------------------------------------------------------

  ?                  # 列出所有快捷键
  :                  # 进入 tmux 的命令提示符
  r                  # 强制重绘当前客户端
  c                  # 创建一个新窗口

  !                  # 将当前窗格从窗口中移出，成为为一个新的窗口
  %                  # 将当前窗格分为左右两半
  "                  # 将当前窗格分为上下两半

  n                  # 切换到下一个窗口
  p                  # 切换到上一个窗口
  {                  # 将当前窗格与上一个窗格交换
  }                  # 将当前窗格与下一个窗格交换

  s                  # 在交互式界面中，选择并连接至另一个会话
  w                  # 在交互式界面中，选择并激活一个窗口
  0 至 9             # 选择 0 到 9 号窗口

  d                  # 断开当前客户端
  D                  # 选择并断开一个客户端

  &                  # 关闭当前窗口
  x                  # 关闭当前窗格

  Up, Down           # 将焦点移动至相邻的窗格
  Left, Right

  M-1 到 M-5         # 排列窗格：
                       # 1) 水平等分
                       # 2) 垂直等分
                       # 3) 将一个窗格作为主要窗格，其他窗格水平等分
                       # 4) 将一个窗格作为主要窗格，其他窗格垂直等分
                       # 5) 平铺

  C-Up, C-Down       # 改变当前窗格的大小，每按一次增减一个单位
  C-Left, C-Right

  M-Up, M-Down       # 改变当前窗格的大小，每按一次增减五个单位
  M-Left, M-Right

```


### 配置 ~/.tmux.conf

tmux.conf 可以在 tmux 启动时自动设置选项，类似于 .vimrc 或 init.el 的用法。

```
# tmux.conf 示例
# 2014.10


### 通用设置
###########################################################################

# 启用 UTF-8 编码
setw -g utf8 on
set-option -g status-utf8 on

# 命令回滚/历史数量限制
set -g history-limit 2048

# 从 1 开始编号，而不是从 0 开始
set -g base-index 1

# 启用鼠标
set-option -g mouse-select-pane on

# 重新加载配置文件
unbind r
bind r source-file ~/.tmux.conf


### 快捷键设置
###########################################################################

# 取消默认的前缀键 C-b
unbind C-b

# 设置新的前缀键 `
set-option -g prefix `

# 多次按下前缀键时，切换到上一个窗口
bind C-a last-window
bind ` last-window

# 按下F11/F12，可以选择不同的前缀键
bind F11 set-option -g prefix C-a
bind F12 set-option -g prefix `

# Vim 风格的快捷键绑定
setw -g mode-keys vi
set-option -g status-keys vi

# 使用 Vim 风格的按键在窗格间移动
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# 循环切换不同的窗口
bind e previous-window
bind f next-window
bind E swap-window -t -1
bind F swap-window -t +1

# 较易于使用的窗格分割快捷键
bind = split-window -h
bind - split-window -v
unbind '"'
unbind %

# 在嵌套使用 tmux 的情况下，激活最内层的会话，以便向其发送命令
bind a send-prefix


### 外观主题
###########################################################################

# 状态栏颜色
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-fg white
set-option -g status-left-length 40
set-option -g status-right-length 80

# 窗格边框颜色
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# 消息框颜色
set-option -g message-fg black
set-option -g message-bg green

# 窗口状态栏颜色
setw -g window-status-bg black
setw -g window-status-current-fg green
setw -g window-status-bell-attr default
setw -g window-status-bell-fg red
setw -g window-status-content-attr default
setw -g window-status-content-fg yellow
setw -g window-status-activity-attr default
setw -g window-status-activity-fg yellow


### 用户界面
###########################################################################

# 通知方式
setw -g monitor-activity on
set -g visual-activity on
set-option -g bell-action any
set-option -g visual-bell off

# 自动设置窗口标题
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # 窗口编号,程序名称,是否活动

# 调整状态栏
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"

# 在状态栏中显示性能计数器
# 需要用到 https://github.com/thewtex/tmux-mem-cpu-load
set -g status-interval 4
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"

```


### 参考资料

[Tmux 主页](http://tmux.github.io)

[Tmux 手册](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)

[FreeBSDChina Wiki](https://wiki.freebsdchina.org/software/t/tmux)

[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux_(简体中文))

[Tmux 快速教程](http://blog.jeswang.org/blog/2013/06/24/tmux-kuai-su-jiao-cheng)

[如何在 tmux 状态栏中显示 CPU / 内存占用的百分比](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)

[管理复杂 tmux 会话的工具 - tmuxinator](https://github.com/tmuxinator/tmuxinator)
---
language: TypeScript
category: language
contributors:
    - ["Philippe Vlérick", "https://github.com/pvlerick"]
translators:
    - ["Shawn Zhang", "https://github.com/shawnzhang009"]
filename: learntypescript-cn.ts
lang: zh-cn
---

TypeScript是一门为开发大型JavaScript应用而设计的语言。TypeScript在JavaScript的基础上增加了类、模块、接口、泛型和静态类型（可选）等常见的概念。它是JavaScript的一个超集：所有JavaScript代码都是有效的TypeScript代码，所以任何JavaScript项目都可以无缝引入TypeScript. TypeScript编译器会把TypeScript代码编译成JavaScript代码。

本文只关注TypeScript额外增加的区别于[JavaScript](../javascript-cn/)的语法，.

如需测试TypeScript编译器，你可以在[Playground](http://www.typescriptlang.org/Playground)码代码，它会自动编译成JavaScript代码然后直接显示出来。

```js
// TypeScript有三种基本类型
var isDone: boolean = false;
var lines: number = 42;
var name: string = "Anders";

// 如果不知道是什么类型，可以使用"any"(任意)类型
var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // 亦可，定义为布尔型

// 对于集合的声明, 有类型化数组和泛型数组
var list: number[] = [1, 2, 3];
// 另外一种，使用泛型数组
var list: Array<number> = [1, 2, 3];

// 枚举：
enum Color {Red, Green, Blue};
var c: Color = Color.Green;

// 最后，"void"用于函数没有任何返回的特殊情况下
function bigHorribleAlert(): void {
  alert("I'm a little annoying box!");
}

// 函数是"第一等公民"(first class citizens), 支持使用箭头表达式和类型推断

// 以下是相等的，TypeScript编译器会把它们编译成相同的JavaScript代码
var f1 = function(i: number): number { return i * i; }
// 返回推断类型的值
var f2 = function(i: number) { return i * i; }
var f3 = (i: number): number => { return i * i; }
// 返回推断类型的值
var f4 = (i: number) => { return i * i; }
// 返回推断类型的值, 单行程式可以不需要return关键字和大括号
var f5 = (i: number) =>  i * i;

// 接口是结构化的，任何具有这些属性的对象都与该接口兼容
interface Person {
  name: string;
  // 可选属性，使用"?"标识
  age?: number;
  // 函数
  move(): void;
}

// 实现"Person"接口的对象，当它有了"name"和"move"方法之后可被视为一个"Person"
var p: Person = { name: "Bobby", move: () => {} };
// 带了可选参数的对象
var validPerson: Person = { name: "Bobby", age: 42, move: () => {} };
// 因为"age"不是"number"类型所以这不是一个"Person"
var invalidPerson: Person = { name: "Bobby", age: true };

// 接口同样可以描述一个函数的类型
interface SearchFunc {
  (source: string, subString: string): boolean;
}
// 参数名并不重要，参数类型才是重要的
var mySearch: SearchFunc;
mySearch = function(src: string, sub: string) {
  return src.search(sub) != -1;
}

// 类 - 成员默认为公共的(public)
class Point {
  // 属性
  x: number;

  // 构造器 - 这里面的public/private关键字会为属性生成样板代码和初始化值
  // 这个例子中，y会被同x一样定义，不需要额外代码
  // 同样支持默认值

  constructor(x: number, public y: number = 0) {
    this.x = x;
  }

  // 函数
  dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

  // 静态成员
  static origin = new Point(0, 0);
}

var p1 = new Point(10 ,20);
var p2 = new Point(25); //y为0

// 继承
class Point3D extends Point {
  constructor(x: number, y: number, public z: number = 0) {
    super(x, y); // 必须显式调用父类的构造器
  }

  // 重写
  dist() {
    var d = super.dist();
    return Math.sqrt(d * d + this.z * this.z);
  }
}

// 模块, "."可以作为子模块的分隔符
module Geometry {
  export class Square {
    constructor(public sideLength: number = 0) {
    }
    area() {
      return Math.pow(this.sideLength, 2);
    }
  }
}

var s1 = new Geometry.Square(5);

// 引入模块并定义本地别名
import G = Geometry;

var s2 = new G.Square(10);

// 泛型
// 类
class Tuple<T1, T2> {
  constructor(public item1: T1, public item2: T2) {
  }
}

// 接口
interface Pair<T> {
  item1: T;
  item2: T;
}

// 以及函数
var pairToTuple = function<T>(p: Pair<T>) {
  return new Tuple(p.item1, p.item2);
};

var tuple = pairToTuple({ item1:"hello", item2:"world"});

// 引用定义文件
// <reference path="jquery.d.ts" />

// 模板字符串(使用反引号的字符串)
// 嵌入变量的模板字符串
var name = 'Tyrone';
var greeting = `Hi ${name}, how are you?`
// 有多行内容的模板字符串
var multiline = `This is an example
of a multiline string`;

```

## 参考资料
 * [TypeScript官网](http://www.typescriptlang.org/)
 * [TypeScript语言规范说明书(pdf)](http://go.microsoft.com/fwlink/?LinkId=267238)
 * [Anders Hejlsberg - TypeScript介绍](http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
 * [GitHub源码](https://github.com/Microsoft/TypeScript)
 * [Definitely Typed - 类型定义仓库](http://definitelytyped.org/)
---
category: tool
tool: vim
filename: LearnVim-cn.txt
contributors:
   - ["RadhikaG", "https://github.com/RadhikaG"]
translators:
   - ["Jiang Haiyun", "https://github.com/haiiiiiyun"]
lang: zh-cn
---


[Vim](http://www.vim.org)
(Vi IMproved) 是 Unix 上的流行编辑器 vi 的克隆版本。这个文本编辑器
是为性能和提升效率而设计的，并且在大多数基于 unix 的系统上普遍存在。
它有大量的快捷键可用来快速导航到文件的特定位置，以便进行快速编辑。

## Vim 导航基础

```
    vim <filename>   # 在 Vim 中打开 <filename>
    :q               # 退出 Vim
    :w               # 保存当前文件
    :wq              # 保存文件并退出 Vim 
    :q!              # 退出 Vim 并且不保存文件
                     # ! *强制* 执行 :q， 因此没有保存就退出 Vim
    :x               # 保存文件并且退出 Vim， 是 :wq 的简写版本

    u                # 撤销
    CTRL+R           # 重做

    h                # 左移一个字符
    j                # 下移一行
    k                # 上移一行
    l                # 右移一个字符

    # 在行内移动

    0                # 移到行首
    $                # 移到行尾
    ^                # 移到行内的第一个非空白字符处

    # 在文本中查找

    /word            # 光标之后的所有该词都高亮显示
    ?word            # 光标之前的所有该词都高亮显示
    n                # 查找后将光标移到该词的下一个出现位置
    N                # 光标移到该词的上一个出现位置

    :%s/foo/bar/g    # 将文件每一行上的所有 'foo' 都改成 'bar'
    :s/foo/bar/g     # 将当前行上的所有 'foo' 都改成 'bar'

    # 跳到字符处

    f<字符>         # 向前跳移到 <字符> 上
    t<字符>         # 向前跳移到 <字符> 的左侧

    # 例如，    
    f<               # 向前跳移到 < 上
    t<               # 向前跳移到 < 的左侧
    
    # 按词移动
    # 默认一个单词由字母，数字和下划线组成

    w                # 移动到下一个词首
    b                # 移动到前一个词首
    e                # 移动到下一个词尾
    

    # 移动的其它命令

    gg               # 移到文件顶部
    G                # 移到文件末尾
    :NUM             # 移到第 NUM 行 (NUM 是任意数字)
    H                # 移到屏幕顶部
    M                # 移到屏幕中间位置
    L                # 移到屏幕末尾
```

## 模式:

Vim 基于 **模式** 这个概念。

命令模式 - Vim 启动后就处于这个模式，用于导航和操作命令
插入模式 - 用于在你的文件中进行修改
可视模式 - 用于高亮文本并对它们进行操作
Ex 模式  - 用于跳到底部的 ':' 提示行上输入命令

```
    i                # 在光标位置前，将 Vim 切换到插入模式
    a                # 在光标位置后，将 Vim 切换到插入模式
    v                # 将 Vim 切换到可视模式
    :                # 将 Vim 切换到 ex 模式
    <esc>            # 无论你当前处于什么模式，都返回到命令模式

    # 复制和粘贴文本

    y                # 复制所选的内容
    yy               # 复制当前行
    d                # 删除所选的内容
    dd               # 删除当前行
    p                # 在当前光标位置后粘贴复制的文本
    P                # 在当前光标位置前粘贴复制的文本
    x                # 删除当前光标位置处的字符
```

## Vim 的 '语法'

Vim 可以被认为是按 '动词-修饰词-名词' 格式编排的一组命令：

动词     - 你的动作
修饰词   - 你如何执行你的动作
名词     - 你的动作所作用于的对象

关于 '动词'，'修饰词'，和 '名词' 的几个重要例子：

```
    # '动词'
    
    d                # 删除
    c                # 修改
    y                # 复制
    v                # 可视化选择

    # '修饰词'

    i                # 内部的
    a                # 周围的
    NUM              # 数字 (NUM 是任意数字)
    f                # 查找文本并位于其上
    t                # 查找文本并停于其前面
    /                # 从光标处开始查找字符串
    ?                # 在光标前查找字符串

    # '名词'

    w                # 词
    s                # 句子
    p                # 段落
    b                # 块
    
    # 示例 '语句' 或命令

    d2w              # 删除 2 个词
    cis              # 修改段落内的内容
    yip              # 复制段落内的内容 (复制你所在的段落)
    ct<              # 修改直到括号开启处
                     # 对你的当前位置直到下个括号开启处的内容进行修改
    d$               # 删除直到行尾
```

## 一些快捷键和技巧

        <!--TODO: Add more!-->
```
    >                # 将所选内容缩进一级
    <                # 将所选内容取消缩进一级
    :earlier 15m     # 将文档还原到 15 分钟前的状态
    :later 15m       # 逆转上述命令
    ddp              # 相邻行交换位置，先 dd 再 p
    .                # 重复之前动作
```

## 宏

宏基本上来说就是可录制的动作。
当你开始录制宏时，它会记录你使用的 **每个** 动作和命令，
直到你停止录制。当调用宏时，它会将这个完全相同的动作和命令序列
再次应用于所选文本之上。

```
    qa               # 开始录制一个叫 'a' 的宏
    q                # 停止录制
    @a               # 重播宏
```

### 配置 ~/.vimrc

.vimrc 可用于在启动时对 Vim 进行配置。

这里是一个示例 ~/.vimrc 文件：

```
" 示例 ~/.vimrc
" 2015.10 

" 需要 Vim iMproved 版本
set nocompatible

" 根据文件名检测文件类型，以便能进行智能自动缩进等操作。
filetype indent plugin on

" 开启语法高亮
syntax on

" 更好的命令行补全
set wildmenu

" 除了当使用大写字母时使用大小写无关查找
set ignorecase
set smartcase

" 当新开一行时，如果没有开启文件特定的缩进规则，
" 则缩进保持与你当前行一致
set autoindent

" 在左侧显示行号
set number

" 缩进选项，根据个人偏好进行修改

" 每个 TAB 的可视空格数
set tabstop=4

" 编辑时 TAB 对应的空格数
set softtabstop=4

" 当使用缩进操作 (>> 和 <<) 时缩进的空格数
set shiftwidth=4

" 将 TAB 转换成空格
set expandtab

" 为缩进和对齐开启智能化的 TAB 和空格切换功能
set smarttab
```

### 参考

[Vim | Home](http://www.vim.org/index.php)

`$ vimtutor`

[A vim Tutorial and Primer](https://danielmiessler.com/study/vim/)

[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread)](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about)

[Arch Linux Wiki](https://wiki.archlinux.org/index.php/Vim)
---
language: Visual Basic
contributors:
    - ["Brian Martin", "http://brianmartin.biz"]
translators:
    - ["Abner Chou", "http://cn.abnerchou.me"]
lang: zh-cn
filename: learnvisualbasic.vb-cn
---

```vb
Module Module1

    Sub Main()
        ' 让我们先从简单的终端程序学起。
        ' 单引号用来生成注释（注意是半角单引号，非全角单引号’）
        ' 为了方便运行此示例代码，我写了个目录索引。
        ' 可能你还不了解以下代码的意义，但随着教程的深入，
        ' 你会渐渐理解其用法。
        Console.Title = ("Learn X in Y Minutes")
        Console.WriteLine("NAVIGATION") ' 显示目录
        Console.WriteLine("")
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine("1. Hello World Output") ' Hello world 输出示例
        Console.WriteLine("2. Hello World Input") ' Hello world 输入示例
        Console.WriteLine("3. Calculating Whole Numbers") ' 求整数之和
        Console.WriteLine("4. Calculating Decimal Numbers") ' 求小数之和
        Console.WriteLine("5. Working Calculator") ' 计算器
        Console.WriteLine("6. Using Do While Loops") ' 使用 Do While 循环
        Console.WriteLine("7. Using For While Loops") ' 使用 For While 循环
        Console.WriteLine("8. Conditional Statements") ' 条件语句
        Console.WriteLine("9. Select A Drink") ' 选饮料
        Console.WriteLine("50. About") ' 关于
        Console.WriteLine("Please Choose A Number From The Above List")
        Dim selection As String = Console.ReadLine
        Select Case selection
            Case "1" ' Hello world 输出示例
                Console.Clear() ' 清空屏幕
                HelloWorldOutput() ' 调用程序块
            Case "2" ' Hello world 输入示例
                Console.Clear()
                HelloWorldInput()
            Case "3" ' 求整数之和
                Console.Clear()
                CalculatingWholeNumbers()
            Case "4" ' 求小数之和
                Console.Clear()
                CalculatingDecimalNumbers()
            Case "5" ' 计算器
                Console.Clear()
                WorkingCalculator()
            Case "6" ' 使用 do while 循环
                Console.Clear()
                UsingDoWhileLoops()
            Case "7" ' 使用 for while 循环
                Console.Clear()
                UsingForLoops()
            Case "8" ' 条件语句
                Console.Clear()
                ConditionalStatement()
            Case "9" ' If/Else 条件语句
                Console.Clear()
                IfElseStatement() ' 选饮料
            Case "50" ' 关于本程序和作者
                Console.Clear()
                Console.Title = ("Learn X in Y Minutes :: About")
                MsgBox("This tutorial is by Brian Martin (@BrianMartinn")
                Console.Clear()
                Main()
                Console.ReadLine()

        End Select
    End Sub

    ' 一、对应程序目录1，下同

    ' 使用 private subs 声明函数。 
    Private Sub HelloWorldOutput()
        ' 程序名
        Console.Title = "Hello World Output | Learn X in Y Minutes"
        ' 使用 Console.Write("") 或者 Console.WriteLine("") 来输出文本到屏幕上
        ' 对应的 Console.Read() 或 Console.Readline() 用来读取键盘输入
        Console.WriteLine("Hello World")
        Console.ReadLine() 
        ' Console.WriteLine()后加Console.ReadLine()是为了防止屏幕输出信息一闪而过
        ' 类似平时常见的“单击任意键继续”的意思。
    End Sub

    ' 二
    Private Sub HelloWorldInput()
        Console.Title = "Hello World YourName | Learn X in Y Minutes"
        ' 变量
        ' 用来存储用户输入的数据
        ' 变量声明以 Dim 开始，结尾为 As VariableType （变量类型）.

        ' 此教程中，我们希望知道你的姓名，并让程序记录并输出。
        Dim username As String
        ' 我们定义username使用字符串类型（String）来记录用户姓名。
        Console.WriteLine("Hello, What is your name? ") ' 询问用户输入姓名
        username = Console.ReadLine() ' 存储用户名到变量 username
        Console.WriteLine("Hello " + username) ' 输出将是 Hello + username
        Console.ReadLine() ' 暂停屏幕并显示以上输出
        ' 以上程序将询问你的姓名，并和你打招呼。
        ' 其它变量如整型（Integer）我们用整型来处理整数。
    End Sub

    ' 三
    Private Sub CalculatingWholeNumbers()
        Console.Title = "Calculating Whole Numbers | Learn X in Y Minutes"
        Console.Write("First number: ") ' 输入一个整数：1，2，50，104，等等
        Dim a As Integer = Console.ReadLine()
        Console.Write("Second number: ") ' 输入第二个整数
        Dim b As Integer = Console.ReadLine()
        Dim c As Integer = a + b
        Console.WriteLine(c)
        Console.ReadLine()
        ' 以上程序将两个整数相加
    End Sub

    ' 四
    Private Sub CalculatingDecimalNumbers()
        Console.Title = "Calculating with Double | Learn X in Y Minutes"
        ' 当然，我们还需要能够处理小数。
        ' 只需要要将整型（Integer）改为小数（Double）类型即可。

        ' 输入一个小数： 1.2， 2.4， 50.1， 104.9，等等
        Console.Write("First number: ")
        Dim a As Double = Console.ReadLine
        Console.Write("Second number: ") ' 输入第二个数
        Dim b As Double = Console.ReadLine
        Dim c As Double = a + b
        Console.WriteLine(c)
        Console.ReadLine()
        ' 以上代码能实现两个小数相加
    End Sub

    ' 五
    Private Sub WorkingCalculator()
        Console.Title = "The Working Calculator| Learn X in Y Minutes"
        ' 但是如果你希望有个能够处理加减乘除的计算器呢？
        ' 只需将上面代码复制粘帖即可。
        Console.Write("First number: ") ' 输入第一个数
        Dim a As Double = Console.ReadLine
        Console.Write("Second number: ") ' 输入第二个数
        Dim b As Integer = Console.ReadLine
        Dim c As Integer = a + b
        Dim d As Integer = a * b
        Dim e As Integer = a - b
        Dim f As Integer = a / b

        ' 通过以下代码我们可以将以上所算的加减乘除结果输出到屏幕上。
        Console.Write(a.ToString() + " + " + b.ToString())
        ' 我们希望答案开头能有3个空格，可以使用String.PadLeft(3)方法。
        Console.WriteLine(" = " + c.ToString.PadLeft(3))
        Console.Write(a.ToString() + " * " + b.ToString())
        Console.WriteLine(" = " + d.ToString.PadLeft(3))
        Console.Write(a.ToString() + " - " + b.ToString())
        Console.WriteLine(" = " + e.ToString.PadLeft(3))
        Console.Write(a.ToString() + " / " + b.ToString())
        Console.WriteLine(" = " + e.ToString.PadLeft(3))
        Console.ReadLine()

    End Sub

    ' 六
    Private Sub UsingDoWhileLoops()
        ' 如同以上的代码一样
        ' 这次我们将询问用户是否继续 （Yes or No?)
        ' 我们将使用Do While循环，因为我们不知到用户是否需要使用一次以上。
        Console.Title = "UsingDoWhileLoops | Learn X in Y Minutes"
        Dim answer As String ' 我们使用字符串变量来存储answer（答案）
        Do ' 循环开始
            Console.Write("First number: ")
            Dim a As Double = Console.ReadLine
            Console.Write("Second number: ")
            Dim b As Integer = Console.ReadLine
            Dim c As Integer = a + b
            Dim d As Integer = a * b
            Dim e As Integer = a - b
            Dim f As Integer = a / b

            Console.Write(a.ToString() + " + " + b.ToString())
            Console.WriteLine(" = " + c.ToString.PadLeft(3))
            Console.Write(a.ToString() + " * " + b.ToString())
            Console.WriteLine(" = " + d.ToString.PadLeft(3))
            Console.Write(a.ToString() + " - " + b.ToString())
            Console.WriteLine(" = " + e.ToString.PadLeft(3))
            Console.Write(a.ToString() + " / " + b.ToString())
            Console.WriteLine(" = " + e.ToString.PadLeft(3))
            Console.ReadLine()
            ' 询问用户是否继续，注意大小写。 
            Console.Write("Would you like to continue? (yes / no)")
            ' 程序读入用户输入
            answer = Console.ReadLine() ' added a bracket here
        ' 当用户输入"yes"时，程序将跳转到Do，并再次执行
        Loop While answer = "yes"

    End Sub

    ' 七
    Private Sub UsingForLoops()
        ' 有一些程序只需要运行一次。
        ' 这个程序我们将实现从10倒数计数.

        Console.Title = "Using For Loops | Learn X in Y Minutes"
        ' 声明变量和Step (步长,即递减的速度，如-1，-2，-3等）。 
        For i As Integer = 10 To 0 Step -1 
            Console.WriteLine(i.ToString) ' 将计数结果输出的屏幕
        Next i ' 计算新的i值
        Console.WriteLine("Start") 
        Console.ReadLine() 
    End Sub

    ' 八
    Private Sub ConditionalStatement()
        Console.Title = "Conditional Statements | Learn X in Y Minutes"
        Dim userName As String = Console.ReadLine
        Console.WriteLine("Hello, What is your name? ") ' 询问用户姓名
        userName = Console.ReadLine() ' 存储用户姓名
        If userName = "Adam" Then
            Console.WriteLine("Hello Adam")
            Console.WriteLine("Thanks for creating this useful site")
            Console.ReadLine()
        Else
            Console.WriteLine("Hello " + userName)
            Console.WriteLine("Have you checked out www.learnxinyminutes.com")
            Console.ReadLine() ' 程序停止，并输出以上文本
        End If
    End Sub

    ' 九
    Private Sub IfElseStatement()
    Console.Title = "If / Else Statement | Learn X in Y Minutes"
        ' 有时候我们需要考虑多于两种情况。
        ' 这时我们就需要使用If/ElesIf条件语句。
        ' If语句就好似个自动售货机，当用户输入A1，A2，A3，等去选择物品时，
        ' 所有的选择可以合并到一个If语句中

        Dim selection As String = Console.ReadLine() ' 读入用户选择
            Console.WriteLine("A1. for 7Up") ' A1 七喜
            Console.WriteLine("A2. for Fanta") ' A2 芬达
            Console.WriteLine("A3. for Dr. Pepper") ' A3 胡椒医生
            Console.WriteLine("A4. for Diet Coke") ' A4 无糖可乐
            Console.ReadLine()
            If selection = "A1" Then
                Console.WriteLine("7up")
                Console.ReadLine()
            ElseIf selection = "A2" Then
                Console.WriteLine("fanta")
                Console.ReadLine()
            ElseIf selection = "A3" Then
                Console.WriteLine("dr. pepper")
                Console.ReadLine()
            ElseIf selection = "A4" Then
                Console.WriteLine("diet coke")
                Console.ReadLine()
            Else
                Console.WriteLine("Please select a product") ' 请选择你需要的产品
                Console.ReadLine()
            End If

    End Sub

End Module

```

## 参考

我（译注：原作者）在命令行下学习的VB。命令行编程使我能够更好的了解程序编译运行机制，并使学习其它语言变得容易。 

如果希望进一步学习VB，这里还有更深层次的 <a href="http://www.vbbootcamp.co.uk/" Title="VB教学">VB教学（英文）</a>。 

所有代码均通过测试。只需复制粘帖到Visual Basic中，并按F5运行即可。 
---
language: xml
contributors:
  - ["João Farias", "https://github.com/JoaoGFarias"]
translators:
  - ["Zach Zhang", "https://github.com/checkcheckzz"]
filename: learnxml-cn.xml
lang: zh-cn
---

XML是一种标记语言，被设计用来存储数据和传输数据。

不像HTML, XML不指定怎样显示或格式化数据，只是携带它。


* XML 语法

```xml
<!-- XML中的注解像这样 -->

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

<!-- 上面是一个典型的XML文件。
  它以一个声明开始，通知一些元数据（自选的）
  
  XML使用一个树的结构。上面的文件中，根节点是'bookstore'，它有三个孩子节点，
  所有的'books'。那些节点有更多的孩子节点，等等。。。
  
  节点用开放/关闭标签创建， 并且孩子就是在开发和关闭标签之间的节点。-->



<!-- XML 携带两类信息:
  1 - 属性 -> 那是关于一个元素的元数据。
      通常，XML解析器使用这些信息去正确地存储数据。
	  它通过在开放标签里出现在插入语中来表示。
  2 - 元素 -> 那是纯数据。
      那就是解析器将从XML文件提取的东西。
	  元素出现在开放和关闭标签之间，没插入语。-->
      
  
<!-- 下面, 一个有两个属性的元素-->
<file type="gif" id="4293">computer.gif</file>


```

* 良好格式的文件 x 验证

一个XML文件是良好格式的如果它是语法正确的。
但是， 使用文件定义，比如DTD和XML概要，在文件中插入更多的限制是可能的。

一个遵守一个文件定义的XML文件被叫做有效的，对于那个文件来说。

有了这个工具，你能够在应用逻辑之外检查XML数据。

```xml

<!-- 下面, 你能够看到一个简化版本的增加了DTD定义的bookstore文件。-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Bookstore.dtd">
<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>

<!-- 这个DTD可能是像这样的:-->

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>


<!-- 这个DTD以一个声明开始。
  接下来, 根节点被声明， 它需要一个或多个孩子节点'book'。 
  每个 'book' 应该准确包含一个 'title' 和 'price' 和
  一个被叫做'category'的缺省值为"Literature"的属性。
  这个'title' 和 'price'节点包含一个解析过的字符数据。-->

<!-- 这个DTD可以在XML文件中本身被声明。-->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE note
[
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title,price)>
<!ATTLIST book category CDATA "Literature">
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>

<bookstore>
  <book category="COOKING">
    <title >Everyday Italian</title>
    <price>30.00</price>
  </book>
</bookstore>
```---
language: yaml
contributors:
  - ["Adam Brenecki", "https://github.com/adambrenecki"]
translators:
  - ["Zach Zhang", "https://github.com/checkcheckzz"]
  - ["Jiang Haiyun", "https://github.com/haiiiiiyun"]
filename: learnyaml-cn.yaml
lang: zh-cn
---

YAML 是一个数据序列化语言，被设计成人类直接可写可读的。

它是 JSON 的严格超集，增加了语法显著换行符和缩进，就像 Python。但和 Python 不一样，
YAML 根本不容许文字制表符。


```yaml
# YAML 中的注解看起来像这样。

################
# 标量类型     #
################

# 我们的根对象 (它们在整个文件里延续) 将会是一个映射，
# 它等价于在别的语言里的一个字典，哈西表或对象。
key: value
another_key: Another value goes here.
a_number_value: 100
# 如果你想将数字 1 作为值，你必须要将它括在引号中。
# 不然 YAML 解析器会假定它是一个布尔值 true。
scientific_notation: 1e+12
boolean: true
null_value: null
key with spaces: value
# 注意到字符串不需要被括在引号中。但是，它们可以被括起来。
"Keys can be quoted too.": "Useful if you want to put a ':' in your key."

# 多行字符串既可以写成像一个'文字块'(使用 |)，
# 或像一个'折叠块'(使用 '>')。
literal_block: |
    This entire block of text will be the value of the 'literal_block' key,
    with line breaks being preserved.

    The literal continues until de-dented, and the leading indentation is
    stripped.

        Any lines that are 'more-indented' keep the rest of their indentation -
        these lines will be indented by 4 spaces.
folded_style: >
    This entire block of text will be the value of 'folded_style', but this
    time, all newlines will be replaced with a single space.

    Blank lines, like above, are converted to a newline character.

        'More-indented' lines keep their newlines, too -
        this text will appear over two lines.

####################
# 集合类型         #
####################

# 嵌套是通过缩进完成的。
a_nested_map:
    key: value
    another_key: Another Value
    another_nested_map:
        hello: hello

# 映射的键值不必是字符串。
0.25: a float key

# 键值也可以是复合型的，比如多行对象
# 我们用 ? 后跟一个空格来表示一个复合键的开始。
? |
    This is a key
    that has multiple lines
: and this is its value

# YAML 也允许使用复杂键语法表示序列间的映射关系。
# 但有些语言的解析器可能会不支持。
# 一个例子：
? - Manchester United
  - Real Madrid
: [ 2001-01-01, 2002-02-02 ]

# 序列 (等价于列表或数组) 看起来像这样：
a_sequence:
    - Item 1
    - Item 2
    - 0.5 # 序列可以包含不同类型。
    - Item 4
    - key: value
      another_key: another_value
    -
        - This is a sequence
        - inside another sequence

# 因为 YAML 是 JSON 的超集，你也可以写 JSON 风格的映射和序列：
json_map: {"key": "value"}
json_seq: [3, 2, 1, "takeoff"]

#######################
# 其余的 YAML 特性    #
#######################

# YAML 还有一个方便的特性叫 '锚'，它能让你很容易在文档中进行文本复用。
# 如下两个键会有相同的值：
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name

# 锚也可被用来复制/继承属性
base: &base
    name: Everyone has same name

foo: &foo
    <<: *base
    age: 10

bar: &bar
    <<: *base
    age: 20

# foo 和 bar 将都含有 name: Everyone has same name

# YAML 还有标签，你可以用它显示地声明类型。
explicit_string: !!str 0.5
# 一些解析器实现特定语言的标签，就像这个针对 Python 的复数类型。
python_complex_number: !!python/complex 1+2j

# 我们也可以在 YAML 的复合键中使用特定语言的标签
? !!python/tuple [5, 7]
: Fifty Seven
# 将会是 Python 中的  {(5, 7): 'Fifty Seven'}

####################
# 其余的 YAML 类型 #
####################

# 除了字符串和数字，YAML 还能理解其它标量。
# ISO 格式的日期和日期时间文本也可以被解析。
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14

# 这个 !!binary 标签表明这个字符串实际上
# 是一个用 base64 编码表示的二进制 blob。
gif_file: !!binary |
    R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
    OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
    +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
    AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# YAML 还有一个集合类型，它看起来像这样：
set:
    ? item1
    ? item2
    ? item3

# 像 Python 一样，集合仅是值为 null 的映射；上面的集合等价于：
set2:
    item1: null
    item2: null
    item3: null
```

### 更多资源

+ [YAML official website](http://yaml.org/)
+ [Online YAML Validator](http://codebeautify.org/yaml-validator)
---
category: tool
tool: zfs
contributors:
    - ["sarlalian", "http://github.com/sarlalian"]
translators:
    - ["Alan Cheng", "https://github.com/kedaio"]
filename: LearnZfs-cn.txt
lang: zh-cn
---

[ZFS](http://open-zfs.org/wiki/Main_Page)
是重新思考与储存相关技术的结果，它把传统的文件系统和卷管理器集成到一个工具当中.
ZFS不但有把它和传统存储系统分开来的特有术语，也有很多聚焦于可用性的功能。


## ZFS概念

### 虚拟设备（Virtual Devices，VDEV）

对于操作系统来说，VDEV和传统的RAID阵列卡所呈现的raid设备类似。VDEV有几种不同的类型，每种类型
都有自己的优势，包括冗余和速度。一般来说，VDEV的可靠性和安全性比阵列卡要好。因此使用ZFS时不
建议使用阵列卡。让ZFS直接管理磁盘。

VDEV的类型
* stripe (条带。单个磁盘，没有冗余)
* mirror (镜像。支持n-way镜像)
* raidz
	* raidz1 (一个奇偶校验磁盘, 类似于RAID 5)
	* raidz2 (两个奇偶校验磁盘, 类似于RAID 6)
	* raidz3 (三个奇偶校验磁盘, 没有类似RAID等级)
* disk  （磁盘）
* file (文件。不推荐在生产环境中使用，因为中间又多了一层不必要的文件系统)

数据会以条带方式存储于存储池中的所有VDEV上。因此一个存储池中的VDEV越多，IOPS就越高。

###  storage pool （存储池） 

ZFS 使用存储池来作为底层存储提供者（VDEV）的抽象。这样可以把用户可见的文件系统和底层的物理磁盘
布局分离开来。

### ZFS 数据集（Dataset）

ZFS 数据集类似于传统的文件系统（译者注：或者说是目录），但是提供了更多的功能。ZFS的很多优势也是
在这一层体现出来的。数据集支持 [Copy on Write](https://en.wikipedia.org/wiki/Copy-on-write)
快照, 配额, 压缩和重复消除（de-duplication）.


### 限制

一个目录最多可包含 2^48个文件, 每个文件最大可以是16 exabytes.  一个存储池最大可包含256 zettabytes 、
(2^78) 的空间, 可以条带化地分布于2^64 设备上. 单一主机最多可以创建2^64个存储池。这些限制可以说是相
当大。


## 命令

### 存储池

Actions: （存储池操作） 
* List   （列举）
* Status （查看状态）
* Destroy （删除）
* Get/Set properties （获取/设置属性）

List zpools （列举存储池（也叫zpool））

```bash
# 创建一个raidz类型的存储池(名称为bucket）
$ zpool create bucket raidz1 gpt/zfs0 gpt/zfs1 gpt/zfs2

# 列出所有存储池
$ zpool list
NAME    SIZE  ALLOC   FREE  EXPANDSZ   FRAG    CAP  DEDUP  HEALTH  ALTROOT
zroot   141G   106G  35.2G         -    43%    75%  1.00x  ONLINE  -

# 列出某一存储池的详细信息
$ zpool list -v zroot
NAME                                     SIZE  ALLOC   FREE  EXPANDSZ   FRAG    CAP  DEDUP HEALTH  ALTROOT
zroot                                    141G   106G  35.2G         -    43%    75%  1.00x ONLINE  -
  gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655   141G   106G  35.2G         -    43%    75%
```

Status of zpools （存储池状态）

```bash
# 获取全部zpool状态信息
$ zpool status
  pool: zroot
 state: ONLINE
  scan: scrub repaired 0 in 2h51m with 0 errors on Thu Oct  1 07:08:31 2015
config:

        NAME                                          STATE     READ WRITE CKSUM
        zroot                                         ONLINE       0     0     0
          gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655  ONLINE       0     0     0

errors: No known data errors

# 用scrub来更正存储池错误信息
$ zpool scrub zroot
$ zpool status -v zroot
  pool: zroot
 state: ONLINE
  scan: scrub in progress since Thu Oct 15 16:59:14 2015
        39.1M scanned out of 106G at 1.45M/s, 20h47m to go
        0 repaired, 0.04% done
config:

        NAME                                          STATE     READ WRITE CKSUM
        zroot                                         ONLINE       0     0     0
          gptid/c92a5ccf-a5bb-11e4-a77d-001b2172c655  ONLINE       0     0     0

errors: No known data errors
```

Properties of zpools （存储池属性）

```bash

# 获取某一存储池的全部属性。属性可能是系统提供，也可能是用户设置
$ zpool get all zroot
NAME   PROPERTY                       VALUE                          SOURCE
zroot  size                           141G                           -
zroot  capacity                       75%                            -
zroot  altroot                        -                              default
zroot  health                         ONLINE                         -
...

# 设置存储池属性，下例这是设置comment(备注)属性
$ zpool set comment="Storage of mah stuff" zroot
$ zpool get comment
NAME   PROPERTY  VALUE                 SOURCE
tank   comment   -                     default
zroot  comment   Storage of mah stuff  local
```

Remove zpool （删除存储池）

```bash
$ zpool destroy test
```


### Datasets （数据集）

Actions:   （数据集相关操作）
* Create   （创建）
* List     （列举）
* Rename   （重命名）
* Delete   （删除）
* Get/Set properties   （获取/设置属性）

Create datasets

```bash
# 创建数据集
$ zfs create tank/root/data
$ mount | grep data
tank/root/data on /data (zfs, local, nfsv4acls)

# 创建子数据集
$ zfs create tank/root/data/stuff
$ mount | grep data
tank/root/data on /data (zfs, local, nfsv4acls)
tank/root/data/stuff on /data/stuff (zfs, local, nfsv4acls)


# 创建卷
$ zfs create -V zroot/win_vm
$ zfs list zroot/win_vm
NAME                 USED  AVAIL  REFER  MOUNTPOINT
tank/win_vm         4.13G  17.9G    64K  -
```

List datasets （列举数据集）

```bash
# 列出所有数据集
$ zfs list
NAME                                                                       USED  AVAIL  REFER  MOUNTPOINT
zroot                                                                      106G  30.8G   144K  none
zroot/ROOT                                                                18.5G  30.8G   144K  none
zroot/ROOT/10.1                                                              8K  30.8G  9.63G  /
zroot/ROOT/default                                                        18.5G  30.8G  11.2G  /
zroot/backup                                                              5.23G  30.8G   144K  none
zroot/home                                                                 288K  30.8G   144K  none
...

# 列举某一数据集的信息
$ zfs list zroot/home
NAME         USED  AVAIL  REFER  MOUNTPOINT
zroot/home   288K  30.8G   144K  none

# 列出快照
$ zfs list -t snapshot
zroot@daily-2015-10-15                                                                  0      -   144K  -
zroot/ROOT@daily-2015-10-15                                                             0      -   144K  -
zroot/ROOT/default@daily-2015-10-15                                                     0      -  24.2G  -
zroot/tmp@daily-2015-10-15                                                           124K      -   708M  -
zroot/usr@daily-2015-10-15                                                              0      -   144K  -
zroot/home@daily-2015-10-15                                                             0      -  11.9G  -
zroot/var@daily-2015-10-15                                                           704K      -  1.42G  -
zroot/var/log@daily-2015-10-15                                                       192K      -   828K  -
zroot/var/tmp@daily-2015-10-15                                                          0      -   152K  -
```

Rename datasets （重命名数据集）

```bash
$ zfs rename tank/root/home tank/root/old_home
$ zfs rename tank/root/new_home tank/root/home
```

Delete dataset （删除数据集）

```bash
# 数据集如果有快照则无法删除
zfs destroy tank/root/home
```

Get / set properties of a dataset （获取/设置数据集属性）

```bash
# 获取数据集全部属性
$ zfs get all  zroot/usr/home                                                                                              │157 # Create Volume
NAME            PROPERTY              VALUE                  SOURCE                                                                          │158 $ zfs create -V zroot/win_vm
zroot/home      type                  filesystem             -                                                                               │159 $ zfs list zroot/win_vm
zroot/home      creation              Mon Oct 20 14:44 2014  -                                                                               │160 NAME                 USED  AVAIL  REFER  MOUNTPOINT
zroot/home      used                  11.9G                  -                                                                               │161 tank/win_vm         4.13G  17.9G    64K  -
zroot/home      available             94.1G                  -                                                                               │162 ```
zroot/home      referenced            11.9G                  -                                                                               │163
zroot/home      mounted               yes                    -
...

# 获取数据集属性
$ zfs get compression zroot/usr/home
NAME            PROPERTY     VALUE     SOURCE
zroot/home      compression  off       default

# 设置数据集属性（下例为设置压缩属性compression）
$ zfs set compression=gzip-9 mypool/lamb

# 列举所有数据集的名称、配额和预留属性
$ zfs list -o name,quota,reservation
NAME                                                               QUOTA  RESERV
zroot                                                               none    none
zroot/ROOT                                                          none    none
zroot/ROOT/default                                                  none    none
zroot/tmp                                                           none    none
zroot/usr                                                           none    none
zroot/home                                                          none    none
zroot/var                                                           none    none
...
```


### Snapshots （快照）

快照是ZFS 的一个非常重要的功能

* 快照占用的空间等于它和原始数据的差异量
* 创建时间以秒计
* 恢复时间和写入速度相同
* 易于自动化

Actions:  （快照相关操作）
* Create  （创建）
* Delete  （删除）
* Rename  （重命名）
* Access snapshots  （访问）
* Send / Receive    （发送/接收）
* Clone             （克隆。译者注：关于clone和快照的区别可参看[这里](http://docs.oracle.com/cd/E19253-01/819-5461/gbcxz/index.html)）


Create snapshots （创建快照）

```bash
# 为单一数据集创建快照
zfs snapshot tank/home/sarlalian@now

# 为数据集及其子集创建快照
$ zfs snapshot -r tank/home@now
$ zfs list -t snapshot
NAME                       USED  AVAIL  REFER  MOUNTPOINT
tank/home@now                 0      -    26K  -
tank/home/sarlalian@now       0      -   259M  -
tank/home/alice@now           0      -   156M  -
tank/home/bob@now             0      -   156M  -
...

Destroy snapshots （删除快照）

```bash
# 如何删除快照
$ zfs destroy tank/home/sarlalian@now

# 删除某一数据集及其子集的快照
$ zfs destroy -r tank/home/sarlalian@now

```

Renaming Snapshots （重命名）

```bash
# 重命名快照
$ zfs rename tank/home/sarlalian@now tank/home/sarlalian@today
$ zfs rename tank/home/sarlalian@now today

# zfs rename -r tank/home@now @yesterday
```

Accessing snapshots  （访问快照）

```bash
# cd进入一个快照目录
$ cd /home/.zfs/snapshot/
```

Sending and Receiving

```bash
# 备份快照到一个文件
$ zfs send tank/home/sarlalian@now | gzip > backup_file.gz

# 发送快照到另一个数据集
$ zfs send tank/home/sarlalian@now | zfs recv backups/home/sarlalian

# 发送快照到一个远程主机
$ zfs send tank/home/sarlalian@now | ssh root@backup_server 'zfs recv tank/home/sarlalian'

# 发送完整数据集及其快照到一个新主机
$ zfs send -v -R tank/home@now | ssh root@backup_server 'zfs recv tank/home'
```

Cloneing Snapshots  （克隆快照）

```bash
# 克隆一个快照
$ zfs clone tank/home/sarlalian@now tank/home/sarlalian_new

# 提升克隆，让它不再依赖原始快照
$ zfs promote tank/home/sarlalian_new
```

### 汇总

下面这个脚本使用了FreeBSD, jails和ZFS，来自动在一个mysql群集的热备主机上为一个mysq staging数据库
创建一份纯净的拷贝。

```bash
#!/bin/sh

echo "==== Stopping the staging database server ===="
jail -r staging

echo "==== Cleaning up existing staging server and snapshot ===="
zfs destroy -r zroot/jails/staging
zfs destroy zroot/jails/slave@staging

echo "==== Quiescing the slave database ===="
echo "FLUSH TABLES WITH READ LOCK;" | /usr/local/bin/mysql -u root -pmyrootpassword -h slave

echo "==== Snapshotting the slave db filesystem as zroot/jails/slave@staging ===="
zfs snapshot zroot/jails/slave@staging

echo "==== Starting the slave database server ===="
jail -c slave

echo "==== Cloning the slave snapshot to the staging server ===="
zfs clone zroot/jails/slave@staging zroot/jails/staging

echo "==== Installing the staging mysql config ===="
mv /jails/staging/usr/local/etc/my.cnf /jails/staging/usr/local/etc/my.cnf.slave
cp /jails/staging/usr/local/etc/my.cnf.staging /jails/staging/usr/local/etc/my.cnf

echo "==== Setting up the staging rc.conf file ===="
mv /jails/staging/etc/rc.conf.local /jails/staging/etc/rc.conf.slave
mv /jails/staging/etc/rc.conf.staging /jails/staging/etc/rc.conf.local

echo "==== Starting the staging db server ===="
jail -c staging

echo "==== Makes the staging database not pull from the master ===="
echo "STOP SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
echo "RESET SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
```


### 延伸阅读

* [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs)
* [FreeBSD Handbook on ZFS](https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/zfs.html)
* [BSDNow's Crash Course on ZFS](http://www.bsdnow.tv/tutorials/zfs)
* [Oracle's Tuning Guide](http://www.oracle.com/technetwork/articles/servers-storage-admin/sto-recommended-zfs-settings-1951715.html)
* [OpenZFS Tuning Guide](http://open-zfs.org/wiki/Performance_tuning)
* [FreeBSD ZFS Tuning Guide](https://wiki.freebsd.org/ZFSTuningGuide)
---
category: tool
tool: bash
contributors:
    - ["Max Yankov", "https://github.com/golergka"]
    - ["Darren Lin", "https://github.com/CogBear"]
    - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
    - ["Denis Arh", "https://github.com/darh"]
    - ["akirahirose", "https://twitter.com/akirahirose"]
    - ["Anton Strömkvist", "http://lutic.org/"]
    - ["Rahil Momin", "https://github.com/iamrahil"]
    - ["Gregrory Kielian", "https://github.com/gskielian"]
    - ["Etan Reisner", "https://github.com/deryni"]
    - ["Jonathan Wang", "https://github.com/Jonathansw"]   
    - ["Leo Rudberg", "https://github.com/LOZORD"]
    - ["Betsy Lorton", "https://github.com/schbetsy"]
    - ["John Detter", "https://github.com/jdetter"]
translators:
    - ["Jinchang Ye", "https://github.com/Alwayswithme"]
    - ["Chunyang Xu", "https://github.com/XuChunyang"]
    - ["Weihang Lo", "https://github.com/weihanglo"]
filename: LearnBash-tw.sh
lang: zh-tw
---

Bash 是一個爲 GNU 計劃編寫的 Unix shell，是 Linux 和 Mac OS X 下預設的 shell。
以下大多數例子可以作爲腳本的一部分運行，也可直接在 shell 下互動執行。

[更多資訊](http://www.gnu.org/software/bash/manual/bashref.html)

```bash
#!/bin/bash
# 腳本的第一行叫 shebang，用來告知系統如何執行該腳本:
# 參見： http://en.wikipedia.org/wiki/Shebang_(Unix)
# 如你所見，註釋以 # 開頭，shebang 也是註釋。

# 顯示 “Hello world!”
echo Hello world!

# 每一句指令以換行或分號隔開：
echo 'This is the first line'; echo 'This is the second line'

# 宣告一個變數：
Variable="Some string"

# 下面是錯誤的做法：
Variable = "Some string"
# Bash 會把 Variable 當做一個指令，由於找不到該指令，因此這裡會報錯。

# 也不可以這樣：
Variable= 'Some string'
# Bash 會認爲 'Some string' 是一條指令，由於找不到該指令，這裡會再次報錯。
# （這個例子中 'Variable=' 這部分會被當作僅對 'Some string' 起作用的賦值。）

# 使用變數：
echo $Variable
echo "$Variable"
echo '$Variable'
# 當你賦值 (assign) 、匯出 (export)，或者以其他方式使用變數時，變數名前不加 $。
# 如果要使用變數的值， 則要加 $。
# 注意： ' (單引號) 不會展開變數。

# 參數展開式 ${}:
echo ${Variable}
# 這是一個參數展開的簡單用法
# 使用參數展開會得到該變數的值，也就是會「展開」或印出該值。
# 在展開期間，可以修改該值或該參數。
# 以下是修改參數展開式的範例：

# 在變數內部進行字串代換
echo ${Variable/Some/A}
# 會把 Variable 中首次出現的 "some" 替換成 “A”。

# 變數的截取
Length=7
echo ${Variable:0:Length}
# 這樣僅會返回變數值的前7個字元

# 變數的預設值
echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
# 對 null (Foo=) 和空字串 (Foo="") 起作用； 零（Foo=0）時返回0
# 注意這僅返回預設值而不是改變變數的值

# 括號展開 { }
# 用以產生任意的字串
echo {1..10}
echo {a..z}
# 這將會輸出該範圍內的起始值到最終值。

# 內建變數：
# 下面的內建變數很有用
echo "Last program's return value: $?"
echo "Script's PID: $$"
echo "Number of arguments: $#"
echo "Scripts arguments: $@"
echo "Scripts arguments separated in different variables: $1 $2..."

# 現在，我們知道變數如何使用與印出
# 讓我們開始學習其他Bash基礎吧！

# 使用 `pwd` 指令，可以得知當前工作目錄
# `pwd` 意指 「印出工作目錄」(print working directory)。
# 我們也可使用內建變數 `$PWD`。
# 下列兩行指令等價：
echo "I'm in $(pwd)" # 執行 `pwd` 且將該值內插至輸出中
echo "I'm in $PWD" # 直接內插 `$PWD` 變數

# 如果終端機上有太多輸出，`clear` 指令可以清除螢幕先前的輸出
clear
# Ctrl-L 也有相同的效果

# 讀取輸入：
echo "What's your name?"
read Name # 這裡不需要宣告新變數
echo Hello, $Name!

# 一般 if 結構看起來像這樣：
# 'man test' 可查看更多的信息
if [ $Name != $USER ]
then
    echo "Your name isn't your username"
else
    echo "Your name is your username"
fi

# 注意： 如果 $Name 為空，bash會將該條件式解讀成：
if [ != USER ]
# 這是一個錯誤的語法
# 所以，安全避免空變數的方法如下：
if [ "$Name" != $USER ] ...
# 如果 $Name 為空，該條件式將被視為：
if [ "" != $USER]
# 此條件式可正常運作


# 根據上一個指令執行結果決定是否執行下一個指令
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"

# 在 if 語句中使用 && 和 || 需要多對方括號
if [ $Name == "Steve" ] && [ $Age -eq 15 ]
then
    echo "This will run if $Name is Steve AND $Age is 15."
fi

if [ $Name == "Daniya" ] || [ $Name == "Zach" ]
then
    echo "This will run if $Name is Daniya OR Zach."
fi

# 表達式的格式如下:
echo $(( 10 + 5 ))

# 與其他程式語言不同的是，bash 運行時依賴上下文。比如，使用 ls 時，列出當前目錄。
ls

# 指令可以帶有選項：
ls -l # 列出文件和目錄的詳細信息
ls -t # 以最後修改時間，對文件與目錄排序
ls -R # 遞迴列出目錄與次目錄的內容

# 前一個指令的輸出可以當作後一個指令的輸入。grep 用來匹配字串。
# 用下面的指令列出當前目錄下所有的 txt 文件：
ls -l | grep "\.txt"

# 使用 `cat` 將檔案印出在標準輸出中：
cat file.txt

# 使用 `cat` 讀取檔案
Contents=$(cat file.txt)
echo "START OF FILE\n$Contents\nEND OF FILE"

# 使用 `cp` 複製檔案或目錄，`cp` 會創建新版本的來源檔案／目錄
# 所以，編輯副本不會影響到初始來源（反之亦然）。
# 注意，如果目的地已存在該檔案／目錄，該檔案／目錄將會被覆寫
cp srcFile.txt clone.txt
cp -r srcDirectory/ dst/ # 遞迴複製

# 如需在兩台電腦間交換檔案，請查看 `scp` 或 `sftp`。
# `scp` 與 `cp` 相似。
# `sftp` 則有更高的互動性（與 `ftp` 相似）。

# 使用 `mv` 來移動目錄與檔案。
# `mv` 與 `cp` 相似，但會刪除來源。
# `mv` 也可以用來重新命名檔案／目錄！
mv s0urc3.txt dst.txt

# 由於 bash 運行時依賴當前目錄的上下文，
# 需要在其他目錄執行指令時，可使用 `cd` 改變當前目錄：
cd ~    # 到家目錄
cd ..   # 到上一層目錄
        # (^^例如, 從 /home/username/Downloads 到 /home/username)
cd /home/username/Documents   # 到指定目錄
cd ~/Documents/..    # 仍位於家目錄，不是嗎？

# 使用子殼程式 (subshells) 在不同目錄間工作
(echo "First, I'm here: $PWD") && (cd someDir; echo "Then, I'm here: $PWD")
pwd # 仍在第一個目錄

# 使用 `mkdir` 來建立新的目錄
mkdir myNewDir
# 使用 `-p` 選項參數，將會自動創建路徑中不存在的目錄
mkdir -p myNewDir/with/intermediate/directories

# 將指令的輸出輸入重新導向（標準輸入、標準輸出、標準錯誤輸出）。
# 從標準輸入讀取資料，直到 ^EOF$ （End-of-file)，且將讀取的資料覆寫至hello.py
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# 重新導向可以到標準輸出（stdout），標準輸入（stdin）和標準錯誤輸出（stderr）。
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# `>` 會覆蓋已存在的文件， `>>` 會以累加的方式輸出文件中。
python hello.py >> "output.out" 2>> "error.err"

# 覆蓋 output.out , 追加 error.err 並統計行數
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# 運行指令並印出文件描述 （比如 /dev/fd/123）
# 具體可查看： man fd
echo <(echo "#helloworld")

# 以 "#helloworld" 覆蓋 output.out:
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# 清理臨時文件並顯示詳情（增加 '-i' 選項啓用互動模式）
# 警告： `rm` 指令無法復原
rm -v output.out error.err output-and-error.log
rm -r tempDir/ # 遞迴刪除

# 一個指令可用 $( ) 嵌套在另一個指令內部：
# 以下的指令會印出當前目錄下的目錄和文件總數
echo "There are $(ls | wc -l) items here."

# 反引號 `` 起相同作用，但不允許嵌套
# 優先使用 $(  ).
echo "There are `ls | wc -l` items here."

# Bash 的 case 語句與 Java 和 C++ 中的 switch 語句類似:
case "$Variable" in
    # 列出需要匹配的字串
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# 循環遍歷給定的參數序列:
# 變數$Variable 的值會被印出 3 次。
for Variable in {1..3}
do
    echo "$Variable"
done

# 或傳統的 “for循環” ：
for ((a=1; a <= 3; a++))
do
    echo $a
done

# 也可以用於文件
# 用 cat 輸出 file1 和 file2 內容
for Variable in file1 file2
do
    cat "$Variable"
done

# 或作用於其他命令的輸出
# 對 ls 輸出的文件執行 cat 指令。
for Output in $(ls)
do
    cat "$Output"
done

# while 循環：
while [ true ]
do
    echo "loop body here..."
    break
done

# 你也可以使用函數
# 定義函數：
function foo ()
{
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
}

# 更簡單的方法
bar ()
{
    echo "Another way to declare functions!"
    return 0
}

# 呼叫函數
foo "My name is" $Name

# 有很多有用的指令需要學習:
# 打印 file.txt 的最後 10 行
tail -n 10 file.txt
# 印出 file.txt 的前 10 行
head -n 10 file.txt
# 將 file.txt 按行排序
sort file.txt
# 報告或忽略重複的行，用選項 -d 印出重複的行
uniq -d file.txt
# 打印每行中 ',' 之前內容
cut -d ',' -f 1 file.txt
# 將 file.txt 文件所有 'okay' 替換爲 'great', （兼容正規表達式）
sed -i 's/okay/great/g' file.txt
# 將 file.txt 中匹配正則的行打印到標準輸出
# 這裡印出以 "foo" 開頭, "bar" 結尾的行
grep "^foo.*bar$" file.txt
# 使用選項 "-c" 統計行數
grep -c "^foo.*bar$" file.txt
# 其他實用的選項參數
grep -r "^foo.*bar$" someDir/ # 遞迴的 `grep`
grep -n "^foo.*bar$" file.txt # 顯示行數
grep -rI "^foo.*bar$" someDir/ # 遞迴的 `grep`, 但忽略二進位檔案
# 同樣的搜尋，再過濾包含「baz」的行
grep "^foo.*bar$" file.txt | grep -v "baz"

# 如果要搜尋字面上的字串而不是用正規表達式，使用 `fgrep` 或 `grep -F`
fgrep "foobar" file.txt

# trap command allows you to execute a command when a signal is received by your script.
# `trap` 可以在一個script運行，接收到特定信號時，執行對應的指令
# `trap` 接收到 `SIGHUP`、`SIGINT`、`SIGTERM` 信號時，會移除 $TEMP_FILE
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM

# `sudo` 可用於以superuser的身分執行指令
$NAME1=$(whoami)
$NAME2=$(sudo whoami)
echo "Was $NAME1, then became more powerful $NAME2"

# 以 bash 內建的 'help' 指令閱讀 Bash 內建文件：
help
help help
help for
help return
help source
help .

# 用 man 指令閱讀相關的 Bash 手冊
apropos bash
man 1 bash
man bash

# 用 info 指令查閱命令的 info 文件 （info 中按 ? 顯示幫助信息）
apropos info | grep '^info.*('
man info
info info
info 5 info

# 閱讀 Bash 的 info 文件：
info bash
info bash 'Bash Features'
info bash 6
info --apropos bash
```
---
language: elixir
contributors:
    - ["Joao Marques", "http://github.com/mrshankly"]
    - ["Dzianis Dashkevich", "https://github.com/dskecse"]
translators:
    - ["Tai An Su", "https://github.com/taiansu"]
filename: learnelixir-tw.ex
lang: zh-tw
---

Elixir 是一門建構在 Erlang 虛擬機上的現代函數式語言。它完全與 Erlang 相容，但
採行了比較常見的語法，並提供更多的功能。

```elixir

# 單行註解以井字號開頭

# 沒有多行註解的功能
# 但你可以連續使用多個單行

# 用 `iex` 來進入 elixir shell
# 用 `elixirc` 來編譯你的模組

# 如果你已成功安裝 elixir 的話，這兩個命令應已在你的 path 下。

## ---------------------------
## -- 基本型別
## ---------------------------

# 數字
3    # 整數
0x1F # 整數
3.0  # 浮點數

# 原子 (Atoms) 是不可變的字面常數，以 `:` 開頭。
:hello # atom

# 元組(Tuples) 會存在記憶體連續的區段裡。
{1,2,3} # tuple

# 我們可以用 `elem` 函式來取得 tuple 中的元素。
elem({1, 2, 3}, 0) #=> 1

# 串列 (List) 是用連結串列實作的。
[1,2,3] # list

# 我們可以這樣取得串列的頭尾元素：
[head | tail] = [1,2,3]
head #=> 1
tail #=> [2,3]

# 在 elixir 中，就如同 Erlang 裡一樣，`=` 代表的是模式比對，而非指派。
#
# 這代表將使用左手邊的模式 (pattern) 去與右手邊的值進行比對。
#
# 這也是先前取得串列的頭尾元素的運作原理

# 當模式比對無法找到合適的配對時，將會回報錯誤，如下例中兩個 tuple 的大小不一致。
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}

# 還有二進位的型別
<<1,2,3>> # binary

# 字串與字母串列
"hello" # string
'hello' # char list

# 多行字串
"""
I'm a multi-line
string.
"""
#=> "I'm a multi-line\nstring.\n"

# 字串皆使用 UTF-8 編碼
"héllò" #=> "héllò"

# 字串其實是以二進位實作，而字母串列就只是單純的串列。
<<?a, ?b, ?c>> #=> "abc"
[?a, ?b, ?c]   #=> 'abc'

# `?a` 在 elixir 中會回傳字母 `a` 的 ASCII 整數
?a #=> 97

# 用 `++` 來合併串列，而合併二進位則要用 `<>`
[1,2,3] ++ [4,5]     #=> [1,2,3,4,5]
'hello ' ++ 'world'  #=> 'hello world'

<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
"hello " <> "world"  #=> "hello world"

# 範圍 (Ranges) 則是以 `開頭..結尾`來宣告 (頭尾都包含在內)
1..10 #=> 1..10
lower..upper = 1..10 # 可以對 range 進行模式比對
[lower, upper] #=> [1, 10]

## ---------------------------
## -- 運算元
## ---------------------------

# 簡單算數
1 + 1  #=> 2
10 - 5 #=> 5
5 * 2  #=> 10
10 / 2 #=> 5.0

# 在 elixir 中， `/` 運算元永遠回傳浮點數。

# 若需要回傳整數的除法，用 `div`
div(10, 2) #=> 5

# 要得到除法的餘數時，用 `rem`
rem(10, 3) #=> 1

# 還有布林運算元: `or`, `and` and `not`.
# 這些運算元要求第一個參數必需為布林值。
true and true #=> true
false or true #=> true
# 1 and true    #=> ** (ArgumentError) argument error

# Elixir 也提供了 `||`, `&&` 及 `!`，它們接受任何型別的參數。
# 除了 `false` 與 `nil` 之外的值都會被當做 true。
1 || true  #=> 1
false && 1 #=> false
nil && 20  #=> nil
!true #=> false

# 用來比較的運算元有：`==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` and `>`
1 == 1 #=> true
1 != 1 #=> false
1 < 2  #=> true

# `===` 和 `!==` 會嚴格比較整數與浮點數
1 == 1.0  #=> true
1 === 1.0 #=> false

# 兩個不同的型別也可以比較
1 < :hello #=> true

# 所有型別的排序如下：
# number < atom < reference < functions < port < pid < tuple < list < bit string

# 引用 Joe Armstrong 的話： "實際排序的先後並不重要， 但有明確排出全體順序的定
# 義才是重要的。"

## ---------------------------
## -- 控制流程
## ---------------------------

# `if` 表達式
if false do
  "This will never be seen"
else
  "This will"
end

# 也有 `unless`
unless true do
  "This will never be seen"
else
  "This will"
end

# 還記得模式比對嗎？Elixir 中許多控制流程的結構都依賴模式比對來運作。

# `case` 讓我們可以將一個值與許多模式進行比對：
case {:one, :two} do
  {:four, :five} ->
    "This won't match"
  {:one, x} ->
    "This will match and bind `x` to `:two` in this clause"
  _ ->
    "This will match any value"
end

# 當我們不需要某個值的時候，通常會將它比對成 `_`。
# 例如我們只關心串列的第一個值的情況時：
[head | _] = [1,2,3]
head #=> 1

# 若希望程式更好懂時，我們會這樣處理：
[head | _tail] = [:a, :b, :c]
head #=> :a

# `cond` 讓我們可以同時檢測多個不同的值。
# 用 `cond` 來代替巢狀的 `if` 表達式
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  1 + 2 == 3 ->
    "But I will"
end

# 把最後一個條件設為 `true` 來捕捉剩下的所有情況是很常見的作法。
cond do
  1 + 1 == 3 ->
    "I will never be seen"
  2 * 5 == 12 ->
    "Me neither"
  true ->
    "But I will (this is essentially an else)"
end

# `try/catch` 用來捕捉拋出的值，它也提供 `after` 子句，無論是否有接到拋出的值，
# 最後都會調用其下的程式。
try do
  throw(:hello)
catch
  message -> "Got #{message}."
after
  IO.puts("I'm the after clause.")
end
#=> I'm the after clause
# "Got :hello"

## ---------------------------
## -- 模組與函式
## ---------------------------

# 匿名函式 (注意那個句點)
square = fn(x) -> x * x end
square.(5) #=> 25

# 匿名函式也接受多個子句及防衛(guards)
# Guards 可以進行模式比對
# 用 `when` 來描述 guards
f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end

f.(1, 3)  #=> 4
f.(-1, 3) #=> -3

# Elixir 也提供許多內建的函式
# 這些在預設的作用域下都可以使用
is_number(10)    #=> true
is_list("hello") #=> false
elem({1,2,3}, 0) #=> 1

# 你可以用模組將多個的函式集合在一起。在模組裡，用 `def` 來定義函式。
defmodule Math do
  def sum(a, b) do
    a + b
  end

  def square(x) do
    x * x
  end
end

Math.sum(1, 2)  #=> 3
Math.square(3) #=> 9

# 要編譯我們的 Math 模組時，先將它存成 `math.ex`，再用 `elixirc` 進行編譯。
# 在終端機輸入： elixirc math.ex

# 在模組中我們可以用 `def` 宣告函式，及用 `defp` 宣告私有 (private) 函式。
# 使用 `def` 定義的函式可以在其它的模組中被調用。
# 私有的函式只能在這個模組內部調用。
defmodule PrivateMath do
  def sum(a, b) do
    do_sum(a, b)
  end

  defp do_sum(a, b) do
    a + b
  end
end

PrivateMath.sum(1, 2)    #=> 3
# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)

# 函式宣告也支援用防衛條件及多個條件子句
defmodule Geometry do
  def area({:rectangle, w, h}) do
    w * h
  end

  def area({:circle, r}) when is_number(r) do
    3.14 * r * r
  end
end

Geometry.area({:rectangle, 2, 3}) #=> 6
Geometry.area({:circle, 3})       #=> 28.25999999999999801048
# Geometry.area({:circle, "not_a_number"})
#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1

# 由於不可變特性 (immutability)，遞迴在 elixir 中扮演重要的角色。
defmodule Recursion do
  def sum_list([head | tail], acc) do
    sum_list(tail, acc + head)
  end

  def sum_list([], acc) do
    acc
  end
end

Recursion.sum_list([1,2,3], 0) #=> 6

# Elixir 模組也支援屬性，模組有內建一些屬性，而你也可以定義自己的屬性。
defmodule MyMod do
  @moduledoc """
  這是內建的屬性，模組文件
  """

  @my_data 100 # 這是自訂的屬性
  IO.inspect(@my_data) #=> 100
end

## ---------------------------
## -- 結構與例外 (Structs and Exceptions)
## ---------------------------

# 結構 (structs) 是 maps 的擴展。是 Elixir 裡可以有預設值，編譯期檢查及
# 多形 (polymorphism) 的資料結構。
defmodule Person do
  defstruct name: nil, age: 0, height: 0
end

joe_info = %Person{ name: "Joe", age: 30, height: 180 }
#=> %Person{age: 30, height: 180, name: "Joe"}

# 取得 name 的值
joe_info.name #=> "Joe"

# 更新 age 的值
older_joe_info = %{ joe_info | age: 31 }
#=> %Person{age: 31, height: 180, name: "Joe"}

# The `try` block with the `rescue` keyword is used to handle exceptions
# 帶有 `rescue` 關鍵字的 `try` 區塊是用來進行例外處理的。
try do
  raise "some error"
rescue
  RuntimeError -> "rescued a runtime error"
  _error -> "this will rescue any error"
end
#=> "rescued a runtime error"

# 所有的異常都有帶著一個訊息
try do
  raise "some error"
rescue
  x in [RuntimeError] ->
    x.message
end
#=> "some error"

## ---------------------------
## -- 平行處理
## ---------------------------

# Elixir 依靠 actor 模式來進行平行處理。在 elixir 中要寫出平行處理程式，
# 只需要三個基本要素：建立行程，發送訊息及接收訊息。

# 我們用 `spawn` 函式來建立行程，它接收一個函式當參數。
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
spawn(f) #=> #PID<0.40.0>

# `spawn` 會回傳一個 pid (行程識別碼)，你可以利用這個 pid 來對該行程傳送訊息。
# 我們會使用 `send` 運算元來傳送訊息。但首先我們要讓該行程可以接收訊息。這要用
# 到 `receive` 機制來達成。

# `receive` 區塊能讓行程監聽接收到的訊息。每個 `receive do` 區塊只能接收一條
# 訊息。若要接收多條訊息時，含有 `receive do` 的函式必須要在接到訊息後，遞迴呼
# 叫自己以再次進入 `receive do` 區塊。

defmodule Geometry do
  def area_loop do
    receive do
      {:rectangle, w, h} ->
        IO.puts("Area = #{w * h}")
        area_loop()
      {:circle, r} ->
        IO.puts("Area = #{3.14 * r * r}")
        area_loop()
    end
  end
end

# 編譯模組，並在 shell 中創造一個行程來執行 `area_loop`。
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>
# 更簡潔的替代寫法
pid = spawn(Geometry, :area_loop, [])

# 對 `pid` 傳送訊息，則會與接收區塊進行樣式比對。
send pid, {:rectangle, 2, 3}
#=> Area = 6
#   {:rectangle,2,3}

send pid, {:circle, 2}
#=> Area = 12.56000000000000049738
#   {:circle,2}

# The shell is also a process, you can use `self` to get the current pid
# shell 也是一個行程 (process)，你可以用 `self` 拿到目前的 pid
self() #=> #PID<0.27.0>
```

## 參考資料

* [Getting started guide](http://elixir-lang.org/getting-started/introduction.html) from the [Elixir website](http://elixir-lang.org)
* [Elixir Documentation](http://elixir-lang.org/docs/master/)
* ["Programming Elixir"](https://pragprog.com/book/elixir/programming-elixir) by Dave Thomas
* [Elixir Cheat Sheet](http://media.pragprog.com/titles/elixir/ElixirCheat.pdf)
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) by Fred Hebert
* ["Programming Erlang: Software for a Concurrent World"](https://pragprog.com/book/jaerlang2/programming-erlang) by Joe Armstrong
---
language: python
contributors:
    - ["Louie Dinh", "http://ldinh.ca"]
    - ["Amin Bandali", "http://aminbandali.com"]
    - ["Andre Polykanine", "https://github.com/Oire"]
    - ["evuez", "http://github.com/evuez"]
translators:
    - ["Michael Yeh", "https://hinet60613.github.io/"]
filename: learnpython-tw.py
lang: zh-tw
---

Python是在1990年代早期由Guido Van Rossum創建的。它是現在最流行的程式語言之一。我愛上Python是因為他極為清晰的語法，甚至可以說它就是可執行的虛擬碼。

非常歡迎各位給我們任何回饋！ 你可以在[@louiedinh](http://twitter.com/louiedinh) 或 louiedinh [at] [google's email service]聯絡到我。

註： 本篇文章適用的版本為Python 2.7，但大部分的Python 2.X版本應該都適用。 Python 2.7將會在2020年停止維護，因此建議您可以從Python 3開始學Python。
Python 3.X可以看這篇[Python 3 教學 (英文)](http://learnxinyminutes.com/docs/python3/).

讓程式碼同時支援Python 2.7和3.X是可以做到的，只要引入
 [`__future__` imports](https://docs.python.org/2/library/__future__.html) 模組.
 `__future__` 模組允許你撰寫可以在Python 2上執行的Python 3程式碼，詳細訊息請參考Python 3 教學。

```python

# 單行註解從井字號開始

""" 多行字串可以用三個雙引號
    包住，不過通常這種寫法會
    被拿來當作多行註解
"""

####################################################
## 1. 原始型別與運算元
####################################################

# 你可以使用數字
3  # => 3

# 還有四則運算
1 + 1  # => 2
8 - 1  # => 7
10 * 2  # => 20
35 / 5  # => 7

# 除法比較麻煩，除以整數時會自動捨去小數位。
5 / 2  # => 2

# 要做精確的除法，我們需要浮點數
2.0     # 浮點數
11.0 / 4.0  # => 2.75 精確多了！

# 整數除法的無條件捨去對正數或負數都適用
5 // 3     # => 1
5.0 // 3.0 # => 1.0 # 浮點數的整數也適用
-5 // 3  # => -2
-5.0 // 3.0 # => -2.0

# 我們可以用除法模組(參考第六節:模組)，讓
# 單一斜線代表普通除法，而非無條件捨去
from __future__ import division
11/4    # => 2.75  ...普通除法
11//4   # => 2 ...無條件捨去

# 取餘數
7 % 3 # => 1

# 指數 (x的y次方)
2**4 # => 16

# 用括號改變運算順序
(1 + 3) * 2  # => 8

# 布林運算
# 注意 "and" 和 "or" 要用小寫
True and False #=> False
False or True #=> True

# 用整數與布林值做運算
0 and 2 #=> 0
-5 or 0 #=> -5
0 == False #=> True
2 == True #=> False
1 == True #=> True

# 用not取反向
not True  # => False
not False  # => True

# 等於判斷是用 ==
1 == 1  # => True
2 == 1  # => False

# 不等於判斷是用 !=
1 != 1  # => False
2 != 1  # => True

# 更多比較
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True

# 比較是可以串接的
1 < 2 < 3  # => True
2 < 3 < 2  # => False

# 字串用單引號 ' 或雙引號 " 建立
"This is a string."
'This is also a string.'

# 字串相加會被串接再一起
"Hello " + "world!"  # => "Hello world!"
# 不用加號也可以做字串相加
"Hello " "world!"  # => "Hello world!"

# ... 也可以做相乘
"Hello" * 3  # => "HelloHelloHello"

# 字串可以被視為字元的陣列
"This is a string"[0]  # => 'T'

# 字串的格式化可以用百分之符號 %
# 儘管在Python 3.1後這個功能被廢棄了，並且在
# 之後的版本會被移除，但還是可以了解一下
x = 'apple'
y = 'lemon'
z = "The items in the basket are %s and %s" % (x,y)

# 新的格式化方式是使用format函式
# 這個方式也是較為推薦的
"{} is a {}".format("This", "placeholder")
"{0} can be {1}".format("strings", "formatted")
# 你也可以用關鍵字，如果你不想數你是要用第幾個變數的話
"{name} wants to eat {food}".format(name="Bob", food="lasagna")

# 無(None) 是一個物件
None  # => None

# 不要用等於符號 "==" 對 無(None)做比較
# 用 "is" 
"etc" is None  # => False
None is None  # => True

# 'is' 運算元是用來識別物件的。對原始型別來說或許沒什麼用，
# 但對物件來說是很有用的。

# 任何物件都可以被當作布林值使用
# 以下的值會被視為是False :
#    - 無(None)
#    - 任何型別的零 (例如: 0, 0L, 0.0, 0j)
#    - 空序列 (例如: '', (), [])
#    - 空容器 (例如: {}, set())
#    - 自定義型別的實體，且滿足某些條件
#      請參考文件: https://docs.python.org/2/reference/datamodel.html#object.__nonzero__
#
# 其餘的值都會被視為True (用bool()函式讓他們回傳布林值).
bool(0)  # => False
bool("")  # => False


####################################################
## 2. 變數與集合
####################################################

# Python的輸出很方便
print "I'm Python. Nice to meet you!" # => I'm Python. Nice to meet you!

# 從命令列獲得值也很方便
input_string_var = raw_input("Enter some data: ") # 資料會被視為字串存進變數
input_var = input("Enter some data: ") # 輸入的資料會被當作Python程式碼執行
# 注意: 請謹慎使用input()函式
# 註: 在Python 3中，input()已被棄用，raw_input()已被更名為input()

# 使用變數前不需要先宣告
some_var = 5    # 方便好用
lower_case_with_underscores
some_var  # => 5

# 對沒有被賦值的變數取值會造成例外
# 請參考錯誤流程部分做例外處理
some_other_var  # 造成 NameError

# if可以當判斷式使用
# 相當於C語言中的二元判斷式
"yahoo!" if 3 > 2 else 2  # => "yahoo!"

# 串列型態可以儲存集合
li = []
# 你可以預先填好串列內容
other_li = [4, 5, 6]

# 用append()在串列後新增東西
li.append(1)    # 此時 li 內容為 [1]
li.append(2)    # 此時 li 內容為 [1, 2]
li.append(4)    # 此時 li 內容為 [1, 2, 4]
li.append(3)    # 此時 li 內容為 [1, 2, 4, 3]
# 用pop()移除串列尾端的元素
li.pop()        # => 3 ，此時 li 內容為 [1, 2, 4]
# 然後再塞回去
li.append(3)    # 此時 li 內容再次為 [1, 2, 4, 3]

# 你可以像存取陣列一樣的存取串列
li[0]  # => 1
# 用等號 = 給串列中特定索引的元素賦值
li[0] = 42
li[0]  # => 42
li[0] = 1  # 註: 將其設定回原本的值
# 用 -1 索引值查看串列最後一個元素
li[-1]  # => 3

# 存取超過範圍會產生IndexError
li[4]  # Raises an IndexError

# 你可以用切片語法來存取特定範圍的值
# (相當於數學中的左閉右開區間，即包含最左邊界，但不包含右邊界)
li[1:3]  # => [2, 4]
# 略過開頭元素
li[2:]  # => [4, 3]
# 略過結尾元素
li[:3]  # => [1, 2, 4]
# 每隔兩個元素取值
li[::2]   # =>[1, 4]
# 串列反轉
li[::-1]   # => [3, 4, 2, 1]
# 你可以任意組合來達到你想要的效果
# li[開始索引:結束索引:間隔]

# 用 "del" 從串列中移除任意元素
del li[2]   # 現在 li 內容為 [1, 2, 3]

# 你可以做串列相加
li + other_li   # => [1, 2, 3, 4, 5, 6]
# 註: li 及 other_li 沒有被更動

# 用 "extend()" 做串列串接
li.extend(other_li)   # 現在 li 內容為 [1, 2, 3, 4, 5, 6]

# 移除特定值的第一次出現
li.remove(2)  # 現在 li 內容為 [1, 3, 4, 5, 6]
li.remove(2)  # 2 不在串列中，造成 ValueError

# 在特定位置插入值
li.insert(1, 2)  # 現在 li 內容再次回復為 [1, 2, 3, 4, 5, 6]

# 取得特定值在串列中第一次出現的位置
li.index(2)  # => 1
li.index(7)  # 7 不在串列中，造成 ValueError

# 用 "in" 檢查特定值是否出現在串列中
1 in li   # => True

# 用 "len()" 取得串列長度
len(li)   # => 6


# 元組(Tuple，以下仍用原文)類似於串列，但是它是不可改變的
tup = (1, 2, 3)
tup[0]   # => 1
tup[0] = 3  # 產生TypeError

# 能對串列做的東西都可以對tuple做
len(tup)   # => 3
tup + (4, 5, 6)   # => (1, 2, 3, 4, 5, 6)
tup[:2]   # => (1, 2)
2 in tup   # => True

# 你可以把tuple拆開並分別將值存入不同變數
a, b, c = (1, 2, 3)     # a 現在是 1, b 現在是 2, c 現在是 3
d, e, f = 4, 5, 6       # 也可以不寫括號
# 如果不加括號，預設會產生tuple
g = 4, 5, 6             # => (4, 5, 6)
# 你看，交換兩個值很簡單吧
e, d = d, e     # 此時 d 的值為 5 且 e 的值為 4


# 字典(Dictionary)用來儲存映射關係
empty_dict = {}
# 你可以對字典做初始化
filled_dict = {"one": 1, "two": 2, "three": 3}

# 用 [] 取值
filled_dict["one"]   # => 1

# 用 "keys()" 將所有的Key輸出到一個List中
filled_dict.keys()   # => ["three", "two", "one"]
# 註: 字典裡key的排序是不固定的
# 你的執行結果可能與上面不同
# 譯註: 只能保證所有的key都有出現，但不保證順序

# 用 "values()" 將所有的Value輸出到一個List中
filled_dict.values()   # => [3, 2, 1]
# 註: 同上，不保證順序

# 用 "in" 來檢查指定的Key是否在字典中
"one" in filled_dict   # => True
1 in filled_dict   # => False

# 查詢不存在的Key會造成KeyError
filled_dict["four"]   # KeyError

# 用 "get()" 來避免KeyError
# 若指定的Key不存在的話會得到None
filled_dict.get("one")   # => 1
filled_dict.get("four")   # => None
# "get()" 函式支援預設值，當找不到指定的值時，會回傳指定的預設值
filled_dict.get("one", 4)   # => 1
filled_dict.get("four", 4)   # => 4
# 注意此時 filled_dict.get("four") 仍然為 None
# (get()此時並沒有產生出任何的值)

# 像操作list一樣，對指定的Key賦值
filled_dict["four"] = 4  # 此時 filled_dict["four"] => 4

# "setdefault()" 只在指定的Key不存在時才會將值插入dictionary
filled_dict.setdefault("five", 5)  # filled_dict["five"] 被指定為 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] 仍保持 5


# 集合(Set)被用來儲存...集合。
# 跟串列(List)有點像，但集合內不會有重複的元素
empty_set = set()
# 初始化 "set()" 並給定一些值
some_set = set([1, 2, 2, 3, 4])   # 現在 some_set 為 set([1, 2, 3, 4])，注意重複的元素只有一個會被存入

# 一樣，不保證順序，就算真的有照順序排也只是你運氣好
another_set = set([4, 3, 2, 2, 1])  # another_set 現在為 set([1, 2, 3, 4])

# 從 Python 2.7 開始，可以使用大括號 {} 來宣告Set
filled_set = {1, 2, 2, 3, 4}   # => {1, 2, 3, 4}

# 加入更多元素進入Set
filled_set.add(5)   # filled_set is now {1, 2, 3, 4, 5}

# 用 & 來對兩個集合取交集
other_set = {3, 4, 5, 6}
filled_set & other_set   # => {3, 4, 5}

# 用 | 來對兩個集合取聯集
filled_set | other_set   # => {1, 2, 3, 4, 5, 6}

# 用 - 來將第二個集合內有的元素移出第一個集合
{1, 2, 3, 4} - {2, 3, 5}   # => {1, 4}

# 用 ^ 來對兩個集合取差集
{1, 2, 3, 4} ^ {2, 3, 5}  # => {1, 4, 5}

# 檢查左邊是否為右邊的母集
{1, 2} >= {1, 2, 3} # => False

# 檢查左邊是否為右邊的子集
{1, 2} <= {1, 2, 3} # => True

# 用 in 來檢查某元素是否存在於集合內
2 in filled_set   # => True
10 in filled_set   # => False


####################################################
## 3. 控制流程
####################################################

# 首先，先宣告一個變數
some_var = 5

# 這邊是 if 判斷式。注意，縮排對Python是很重要的。
# 下面應該會印出 "some_var is smaller than 10"
if some_var > 10:
    print "some_var is totally bigger than 10."
elif some_var < 10:    # elif 可有可無
    print "some_var is smaller than 10."
else:           # else 也可有可無
    print "some_var is indeed 10."


"""
For 迴圈會遞迴整的List
下面的程式碼會輸出:
    dog is a mammal
    cat is a mammal
    mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
    # 你可以用{0}來組合0出格式化字串 (見上面.)
    print "{0} is a mammal".format(animal)

"""
"range(number)" 回傳一個包含從0到給定值的數字List，
下面的程式碼會輸出:
    0
    1
    2
    3
"""
for i in range(4):
    print i

"""
"range(lower, upper)" 回傳一個包含從給定的下限
到給定的上限的數字List
下面的程式碼會輸出:
    4
    5
    6
    7
"""
for i in range(4, 8):
    print i

"""
While迴圈會執行到條件不成立為止
下面的程式碼會輸出:
    0
    1
    2
    3
"""
x = 0
while x < 4:
    print x
    x += 1  # x = x + 1 的簡寫

# 用try/except處理例外

# 適用Python 2.6及以上版本
try:
    # 用 "raise" 來發起例外
    raise IndexError("This is an index error")
except IndexError as e:
    pass    # 毫無反應，就只是個什麼都沒做的pass。通常這邊會讓你做對例外的處理
except (TypeError, NameError):
    pass    # 有需要的話，多種例外可以一起處理
else:   # else 可有可無，但必須寫在所有的except後
    print "All good!"   # 只有在try的時候沒有產生任何except才會被執行
finally: # 不管什麼情況下一定會被執行
    print "We can clean up resources here"

# 除了try/finally以外，你可以用 with 來簡單的處理清理動作
with open("myfile.txt") as f:
    for line in f:
        print line

####################################################
## 4. 函式
####################################################

# 用 "def" 來建立新函式
def add(x, y):
    print "x is {0} and y is {1}".format(x, y)
    return x + y    # 用 "return" 來回傳值

# 用參數來呼叫函式
add(5, 6)   # => 輸出 "x is 5 and y is 6" 並回傳 11

# 你也可以寫上參數名稱來呼叫函式
add(y=6, x=5)   # 這種狀況下，兩個參數的順序並不影響執行


# 你可以定義接受多個變數的函式，用*來表示參數tuple
def varargs(*args):
    return args

varargs(1, 2, 3)   # => (1, 2, 3)


# 你可以定義接受多個變數的函式，用**來表示參數dictionary
def keyword_args(**kwargs):
    return kwargs

# 呼叫看看會發生什麼事吧
keyword_args(big="foot", loch="ness")   # => {"big": "foot", "loch": "ness"}


# 如果你想要，你也可以兩個同時用
def all_the_args(*args, **kwargs):
    print args
    print kwargs
"""
all_the_args(1, 2, a=3, b=4) prints:
    (1, 2)
    {"a": 3, "b": 4}
"""

# 呼叫函式時，你可以做反向的操作
# 用 * 將變數展開為順序排序的變數
# 用 ** 將變數展開為Keyword排序的變數
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)   # 等同於 foo(1, 2, 3, 4)
all_the_args(**kwargs)   # 等同於 foo(a=3, b=4)
all_the_args(*args, **kwargs)   # 等同於 foo(1, 2, 3, 4, a=3, b=4)

# 你可以把args跟kwargs傳到下一個函式內
# 分別用 * 跟 ** 將它展開就可以了
def pass_all_the_args(*args, **kwargs):
    all_the_args(*args, **kwargs)
    print varargs(*args)
    print keyword_args(**kwargs)

# 函式範圍
x = 5

def set_x(num):
    # 區域變數 x 和全域變數 x 不是同一個東西
    x = num # => 43
    print x # => 43

def set_global_x(num):
    global x
    print x # => 5
    x = num # 全域變數 x 在set_global_x(6)被設定為 6 
    print x # => 6

set_x(43)
set_global_x(6)

# Python有一級函式
def create_adder(x):
    def adder(y):
        return x + y
    return adder

add_10 = create_adder(10)
add_10(3)   # => 13

# 也有匿名函式
(lambda x: x > 2)(3)   # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5

# 還有內建的高階函式
map(add_10, [1, 2, 3])   # => [11, 12, 13]
map(max, [1, 2, 3], [4, 2, 1])   # => [4, 2, 3]

filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]

# 我們可以用List列表的方式對map和filter等高階函式做更有趣的應用
[add_10(i) for i in [1, 2, 3]]  # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5]   # => [6, 7]


####################################################
## 5. 類別
####################################################

# 我們可以由object繼承出一個新的類別
class Human(object):

    # 類別的參數，被所有這個類別的實體所共用
    species = "H. sapiens"

    # 基礎建構函式，當class被實體化的時候會被呼叫
    # 注意前後的雙底線
    # 代表此物件或屬性雖然在使用者控制的命名空間內，但是被python使用
    def __init__(self, name):
        # 將函式引入的參數 name 指定給實體的 name 參數
        self.name = name

        # 初始化屬性
        self.age = 0


    # 一個實體的方法(method)。 所有的method都以self為第一個參數
    def say(self, msg):
        return "{0}: {1}".format(self.name, msg)

    # 一個類別方法會被所有的實體所共用
    # 他們會以類別為第一參數的方式被呼叫
    @classmethod
    def get_species(cls):
        return cls.species

    # 靜態方法
    @staticmethod
    def grunt():
        return "*grunt*"

    # 屬性就像是用getter取值一樣
    # 它將方法 age() 轉為同名的、只能讀取的屬性
    @property
    def age(self):
        return self._age

    # 這樣寫的話可以讓屬性被寫入新的值
    @age.setter
    def age(self, age):
        self._age = age

    # 這樣寫的話允許屬性被刪除
    @age.deleter
    def age(self):
        del self._age


# 將類別實體化
i = Human(name="Ian")
print i.say("hi")     # prints out "Ian: hi"

j = Human("Joel")
print j.say("hello")  # prints out "Joel: hello"

# 呼叫類別方法
i.get_species()   # => "H. sapiens"

# 更改共用的屬性
Human.species = "H. neanderthalensis"
i.get_species()   # => "H. neanderthalensis"
j.get_species()   # => "H. neanderthalensis"

# 呼叫靜態方法
Human.grunt()   # => "*grunt*"

# 更新屬性
i.age = 42

# 取得屬性
i.age # => 42

# 移除屬性
del i.age
i.age  # => raises an AttributeError


####################################################
## 6. 模組
####################################################

# 你可以引入模組來做使用
import math
print math.sqrt(16)  # => 4
                     # math.sqrt()為取根號

# 你可以只從模組取出特定幾個函式
from math import ceil, floor
print ceil(3.7)  # => 4.0
print floor(3.7)   # => 3.0

# 你可以將所有的函式從模組中引入
# 注意：不建議這麼做
from math import *

# 你可以用 as 簡寫模組名稱
import math as m
math.sqrt(16) == m.sqrt(16)   # => True
# 你也可以測試函示是否相等
from math import sqrt
math.sqrt == m.sqrt == sqrt  # => True

# Python的模組就只是一般的Python檔。
# 你可以自己的模組自己寫、自己的模組自己引入
# 模組的名稱和檔案名稱一樣

# 你可以用dir()來查看有哪些可用函式和屬性
import math
dir(math)


####################################################
## 7. 進階
####################################################

# 產生器(Generator)可以讓你寫更懶惰的程式碼
def double_numbers(iterable):
    for i in iterable:
        yield i + i

# 產生器可以讓你即時的產生值
# 不是全部產生完之後再一次回傳，產生器會在每一個遞迴時
# 產生值。 這也意味著大於15的值不會在double_numbers中產生。
# 這邊，xrange()做的事情和range()一樣
# 建立一個 1-900000000 的List會消耗很多時間和記憶體空間
# xrange() 建立一個產生器物件，而不是如range()建立整個List
# 我們用底線來避免可能和python的關鍵字重複的名稱
xrange_ = xrange(1, 900000000)

# 下面的程式碼會把所有的值乘以兩倍，直到出現大於30的值
for i in double_numbers(xrange_):
    print i
    if i >= 30:
        break


# 裝飾子
# 在這個範例中，beg會綁在say上
# Beg會呼叫say。 如果say_please為True的話，它會更改回傳的訊息
from functools import wraps


def beg(target_function):
    @wraps(target_function)
    def wrapper(*args, **kwargs):
        msg, say_please = target_function(*args, **kwargs)
        if say_please:
            return "{} {}".format(msg, "Please! I am poor :(")
        return msg

    return wrapper


@beg
def say(say_please=False):
    msg = "Can you buy me a beer?"
    return msg, say_please


print say()  # Can you buy me a beer?
print say(say_please=True)  # Can you buy me a beer? Please! I am poor :(
```

## 準備好學更多了嗎?

### 線上免費資源

* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com)
* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
* [Dive Into Python](http://www.diveintopython.net/)
* [The Official Docs](http://docs.python.org/2/)
* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
* [Python Module of the Week](http://pymotw.com/2/)
* [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182)
* [First Steps With Python](https://realpython.com/learn/python-first-steps/)

### 或買本書?

* [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20)
* [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20)
* [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20)
