{"action":"created","comment":{"url":"https://api.github.com/repos/doomspork/elixir-school/pulls/comments/57529210","id":57529210,"diff_hunk":"@@ -0,0 +1,223 @@\n+---\n+layout: page\n+title: Fonctions\n+category: basics\n+order: 7\n+lang: fr\n+---\n+\n+En Elixir et dans d'autres langages fonctionnels, les fonctions sont des citoyens de premier ordre. Nous verrons les différents types de fonctions en\n+Elixir, ce qui les rend différentes et comment les utiliser.\n+\n+## Table des matières\n+\n+- [Fonctions anonymes](#fonctions-anonymes)\n+  - [Le raccourcis &](#le-raccourcis--)\n+- [Pattern matching](#pattern-matching)\n+- [Fonctions nommées](#fonctions-nommees)\n+  - [Fonctions Privées](#fonctions-privee)\n+  - [Guards](#guards)\n+  - [Arguments par défaut](#arguments-par-defauts)\n+\n+## Fonctions anonymes\n+\n+Tout comme leur nom le sous-entend, les fonctions anonymes n'ont pas de nom. Tel que nous l'avons vu dans la leçon `Enum`, elles sont fréquemment passées à\n+d'autres fonctions. Pour définir une fonctions anonyme en Elixir, nous avons besoin des mot-clés `fn` et `end`, à l'intérieur desquels nous pouvons définir \n+n'importe quel nombre de paramètres et de corps de fonction, séparés par `->`.\n+Jetons un coup d'œil à cet exemple : \n+\n+```elixir\n+iex> sum = fn (a, b) -> a + b end\n+iex> sum.(2, 3)\n+5\n+```\n+### Le raccourci '&'\n+\n+Utiliser des fonctions anonymes est quelque chose de tellement courant en Elixir qu'il y a un raccourci pour le faire : \n+\n+```elixir\n+iex> sum = &(&1 + &2)\n+iex> sum.(2, 3)\n+5\n+```\n+\n+Comme vous avez peut-être déjà deviné, dans la version raccourcie nos paramètres sont disponibles en tant que `&1`, `&2`, `&3`, etc.\n+\n+## Pattern matching\n+\n+Le Pattern matching (ou « Filtrage par motif » en Français) en Elixir ne se limite pas juste aux variables. Il peut être appliqué aux signatures de\n+fonctions, comme nous allons le voir dans cette section : \n+\n+Elixir utilise le pattern matching pour identifier le premier groupe de paramètres correspondant, et appelle le corps de fonction correspondant : \n+\n+```elixir\n+iex> handle_result = fn\n+...>   {:ok, result} -> IO.puts \"Handling result...\"\n+...>   {:error} -> IO.puts \"An error has occurred!\"\n+...> end\n+\n+iex> some_result = 1\n+iex> handle_result.({:ok, some_result})\n+Handling result...\n+\n+iex> handle_result.({:error})\n+An error has occurred!\n+```\n+## Fonctions nommées\n+\n+Nous pouvons définir des fonctions nommées auxquelles nous pouvons nous référer plus tard. Ces fonctions sont définies avec le mot-clé `def` au sein d'un\n+module. Nous en apprendrons plus au sujet des Modules dans les prochaines leçons. Maintenant, concentrons-nous seulement sur les fonctions nommées.\n+\n+Les fonctions définies au sein d'un module sont utilisables par les autres modules, et c'est un élément de langage particulièrement utile en Elixir.\n+\n+```elixir\n+defmodule Greeter do\n+  def hello(name) do\n+    \"Hello, \" <> name\n+  end\n+end\n+\n+iex> Greeter.hello(\"Sean\")\n+\"Hello, Sean\"\n+```\n+\n+Si nos corps de fonction ne s'étendent que sur une ligne, nous pouvons les raccourcir encore plus avec le mot-clé `do:` : \n+\n+```elixir\n+defmodule Greeter do\n+  def hello(name), do: \"Hello, \" <> name\n+end\n+```\n+\n+Et armés de notre connaissance du pattern matching, explorons dorénavant la récursion en utilisant les fonctions : \n+\n+```elixir\n+defmodule Length do\n+  def of([]), do: 0\n+  def of([_|t]), do: 1 + of(t)\n+end\n+\n+iex> Length.of []\n+0\n+iex> Length.of [1, 2, 3]\n+3\n+```\n+\n+### Fonctions privées\n+Quand nous ne voulons pas que d'autres modules aient accès à nos fonctions, nous pouvons utiliser des fonctions privées, qui sont seulement disponibles\n+au sein de leur propre module. Elles sont définies avec le mot-clé `defp` : ","path":"fr/lessons/basics/functions.md","position":108,"original_position":108,"commit_id":"f3c1059a0f4aa62904b176d27cfb1cadc5167a86","original_commit_id":"f3c1059a0f4aa62904b176d27cfb1cadc5167a86","user":{"login":"julienXX","id":53949,"avatar_url":"https://avatars.githubusercontent.com/u/53949?v=3","gravatar_id":"","url":"https://api.github.com/users/julienXX","html_url":"https://github.com/julienXX","followers_url":"https://api.github.com/users/julienXX/followers","following_url":"https://api.github.com/users/julienXX/following{/other_user}","gists_url":"https://api.github.com/users/julienXX/gists{/gist_id}","starred_url":"https://api.github.com/users/julienXX/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/julienXX/subscriptions","organizations_url":"https://api.github.com/users/julienXX/orgs","repos_url":"https://api.github.com/users/julienXX/repos","events_url":"https://api.github.com/users/julienXX/events{/privacy}","received_events_url":"https://api.github.com/users/julienXX/received_events","type":"User","site_admin":false},"body":"pas d'espace avant ':'","created_at":"2016-03-27T14:18:21Z","updated_at":"2016-03-27T14:18:21Z","html_url":"https://github.com/doomspork/elixir-school/pull/229#discussion_r57529210","pull_request_url":"https://api.github.com/repos/doomspork/elixir-school/pulls/229","_links":{"self":{"href":"https://api.github.com/repos/doomspork/elixir-school/pulls/comments/57529210"},"html":{"href":"https://github.com/doomspork/elixir-school/pull/229#discussion_r57529210"},"pull_request":{"href":"https://api.github.com/repos/doomspork/elixir-school/pulls/229"}}},"pull_request":{"url":"https://api.github.com/repos/doomspork/elixir-school/pulls/229","id":64265759,"html_url":"https://github.com/doomspork/elixir-school/pull/229","diff_url":"https://github.com/doomspork/elixir-school/pull/229.diff","patch_url":"https://github.com/doomspork/elixir-school/pull/229.patch","issue_url":"https://api.github.com/repos/doomspork/elixir-school/issues/229","number":229,"state":"open","locked":false,"title":"[FR] Functions","user":{"login":"tchoutri","id":15848443,"avatar_url":"https://avatars.githubusercontent.com/u/15848443?v=3","gravatar_id":"","url":"https://api.github.com/users/tchoutri","html_url":"https://github.com/tchoutri","followers_url":"https://api.github.com/users/tchoutri/followers","following_url":"https://api.github.com/users/tchoutri/following{/other_user}","gists_url":"https://api.github.com/users/tchoutri/gists{/gist_id}","starred_url":"https://api.github.com/users/tchoutri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tchoutri/subscriptions","organizations_url":"https://api.github.com/users/tchoutri/orgs","repos_url":"https://api.github.com/users/tchoutri/repos","events_url":"https://api.github.com/users/tchoutri/events{/privacy}","received_events_url":"https://api.github.com/users/tchoutri/received_events","type":"User","site_admin":false},"body":"@julienXX Could you review this, please?","created_at":"2016-03-27T13:38:14Z","updated_at":"2016-03-27T14:18:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"71f06654b4470345530dafcf8e6c2373ce7f852b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/doomspork/elixir-school/pulls/229/commits","review_comments_url":"https://api.github.com/repos/doomspork/elixir-school/pulls/229/comments","review_comment_url":"https://api.github.com/repos/doomspork/elixir-school/pulls/comments{/number}","comments_url":"https://api.github.com/repos/doomspork/elixir-school/issues/229/comments","statuses_url":"https://api.github.com/repos/doomspork/elixir-school/statuses/f3c1059a0f4aa62904b176d27cfb1cadc5167a86","head":{"label":"tchoutri:fr-functions","ref":"fr-functions","sha":"f3c1059a0f4aa62904b176d27cfb1cadc5167a86","user":{"login":"tchoutri","id":15848443,"avatar_url":"https://avatars.githubusercontent.com/u/15848443?v=3","gravatar_id":"","url":"https://api.github.com/users/tchoutri","html_url":"https://github.com/tchoutri","followers_url":"https://api.github.com/users/tchoutri/followers","following_url":"https://api.github.com/users/tchoutri/following{/other_user}","gists_url":"https://api.github.com/users/tchoutri/gists{/gist_id}","starred_url":"https://api.github.com/users/tchoutri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tchoutri/subscriptions","organizations_url":"https://api.github.com/users/tchoutri/orgs","repos_url":"https://api.github.com/users/tchoutri/repos","events_url":"https://api.github.com/users/tchoutri/events{/privacy}","received_events_url":"https://api.github.com/users/tchoutri/received_events","type":"User","site_admin":false},"repo":{"id":54599451,"name":"elixir-school","full_name":"tchoutri/elixir-school","owner":{"login":"tchoutri","id":15848443,"avatar_url":"https://avatars.githubusercontent.com/u/15848443?v=3","gravatar_id":"","url":"https://api.github.com/users/tchoutri","html_url":"https://github.com/tchoutri","followers_url":"https://api.github.com/users/tchoutri/followers","following_url":"https://api.github.com/users/tchoutri/following{/other_user}","gists_url":"https://api.github.com/users/tchoutri/gists{/gist_id}","starred_url":"https://api.github.com/users/tchoutri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tchoutri/subscriptions","organizations_url":"https://api.github.com/users/tchoutri/orgs","repos_url":"https://api.github.com/users/tchoutri/repos","events_url":"https://api.github.com/users/tchoutri/events{/privacy}","received_events_url":"https://api.github.com/users/tchoutri/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tchoutri/elixir-school","description":"Lessons about the Elixir programming language","fork":true,"url":"https://api.github.com/repos/tchoutri/elixir-school","forks_url":"https://api.github.com/repos/tchoutri/elixir-school/forks","keys_url":"https://api.github.com/repos/tchoutri/elixir-school/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tchoutri/elixir-school/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tchoutri/elixir-school/teams","hooks_url":"https://api.github.com/repos/tchoutri/elixir-school/hooks","issue_events_url":"https://api.github.com/repos/tchoutri/elixir-school/issues/events{/number}","events_url":"https://api.github.com/repos/tchoutri/elixir-school/events","assignees_url":"https://api.github.com/repos/tchoutri/elixir-school/assignees{/user}","branches_url":"https://api.github.com/repos/tchoutri/elixir-school/branches{/branch}","tags_url":"https://api.github.com/repos/tchoutri/elixir-school/tags","blobs_url":"https://api.github.com/repos/tchoutri/elixir-school/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tchoutri/elixir-school/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tchoutri/elixir-school/git/refs{/sha}","trees_url":"https://api.github.com/repos/tchoutri/elixir-school/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tchoutri/elixir-school/statuses/{sha}","languages_url":"https://api.github.com/repos/tchoutri/elixir-school/languages","stargazers_url":"https://api.github.com/repos/tchoutri/elixir-school/stargazers","contributors_url":"https://api.github.com/repos/tchoutri/elixir-school/contributors","subscribers_url":"https://api.github.com/repos/tchoutri/elixir-school/subscribers","subscription_url":"https://api.github.com/repos/tchoutri/elixir-school/subscription","commits_url":"https://api.github.com/repos/tchoutri/elixir-school/commits{/sha}","git_commits_url":"https://api.github.com/repos/tchoutri/elixir-school/git/commits{/sha}","comments_url":"https://api.github.com/repos/tchoutri/elixir-school/comments{/number}","issue_comment_url":"https://api.github.com/repos/tchoutri/elixir-school/issues/comments{/number}","contents_url":"https://api.github.com/repos/tchoutri/elixir-school/contents/{+path}","compare_url":"https://api.github.com/repos/tchoutri/elixir-school/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tchoutri/elixir-school/merges","archive_url":"https://api.github.com/repos/tchoutri/elixir-school/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tchoutri/elixir-school/downloads","issues_url":"https://api.github.com/repos/tchoutri/elixir-school/issues{/number}","pulls_url":"https://api.github.com/repos/tchoutri/elixir-school/pulls{/number}","milestones_url":"https://api.github.com/repos/tchoutri/elixir-school/milestones{/number}","notifications_url":"https://api.github.com/repos/tchoutri/elixir-school/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tchoutri/elixir-school/labels{/name}","releases_url":"https://api.github.com/repos/tchoutri/elixir-school/releases{/id}","deployments_url":"https://api.github.com/repos/tchoutri/elixir-school/deployments","created_at":"2016-03-23T23:13:50Z","updated_at":"2016-03-23T23:13:51Z","pushed_at":"2016-03-27T13:38:45Z","git_url":"git://github.com/tchoutri/elixir-school.git","ssh_url":"git@github.com:tchoutri/elixir-school.git","clone_url":"https://github.com/tchoutri/elixir-school.git","svn_url":"https://github.com/tchoutri/elixir-school","homepage":"https://elixirschool.com","size":823,"stargazers_count":0,"watchers_count":0,"language":"CSS","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"doomspork:master","ref":"master","sha":"e73d66aa939f038603da753464948824834d945b","user":{"login":"doomspork","id":73386,"avatar_url":"https://avatars.githubusercontent.com/u/73386?v=3","gravatar_id":"","url":"https://api.github.com/users/doomspork","html_url":"https://github.com/doomspork","followers_url":"https://api.github.com/users/doomspork/followers","following_url":"https://api.github.com/users/doomspork/following{/other_user}","gists_url":"https://api.github.com/users/doomspork/gists{/gist_id}","starred_url":"https://api.github.com/users/doomspork/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/doomspork/subscriptions","organizations_url":"https://api.github.com/users/doomspork/orgs","repos_url":"https://api.github.com/users/doomspork/repos","events_url":"https://api.github.com/users/doomspork/events{/privacy}","received_events_url":"https://api.github.com/users/doomspork/received_events","type":"User","site_admin":false},"repo":{"id":36616176,"name":"elixir-school","full_name":"doomspork/elixir-school","owner":{"login":"doomspork","id":73386,"avatar_url":"https://avatars.githubusercontent.com/u/73386?v=3","gravatar_id":"","url":"https://api.github.com/users/doomspork","html_url":"https://github.com/doomspork","followers_url":"https://api.github.com/users/doomspork/followers","following_url":"https://api.github.com/users/doomspork/following{/other_user}","gists_url":"https://api.github.com/users/doomspork/gists{/gist_id}","starred_url":"https://api.github.com/users/doomspork/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/doomspork/subscriptions","organizations_url":"https://api.github.com/users/doomspork/orgs","repos_url":"https://api.github.com/users/doomspork/repos","events_url":"https://api.github.com/users/doomspork/events{/privacy}","received_events_url":"https://api.github.com/users/doomspork/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/doomspork/elixir-school","description":"Lessons about the Elixir programming language","fork":false,"url":"https://api.github.com/repos/doomspork/elixir-school","forks_url":"https://api.github.com/repos/doomspork/elixir-school/forks","keys_url":"https://api.github.com/repos/doomspork/elixir-school/keys{/key_id}","collaborators_url":"https://api.github.com/repos/doomspork/elixir-school/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/doomspork/elixir-school/teams","hooks_url":"https://api.github.com/repos/doomspork/elixir-school/hooks","issue_events_url":"https://api.github.com/repos/doomspork/elixir-school/issues/events{/number}","events_url":"https://api.github.com/repos/doomspork/elixir-school/events","assignees_url":"https://api.github.com/repos/doomspork/elixir-school/assignees{/user}","branches_url":"https://api.github.com/repos/doomspork/elixir-school/branches{/branch}","tags_url":"https://api.github.com/repos/doomspork/elixir-school/tags","blobs_url":"https://api.github.com/repos/doomspork/elixir-school/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/doomspork/elixir-school/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/doomspork/elixir-school/git/refs{/sha}","trees_url":"https://api.github.com/repos/doomspork/elixir-school/git/trees{/sha}","statuses_url":"https://api.github.com/repos/doomspork/elixir-school/statuses/{sha}","languages_url":"https://api.github.com/repos/doomspork/elixir-school/languages","stargazers_url":"https://api.github.com/repos/doomspork/elixir-school/stargazers","contributors_url":"https://api.github.com/repos/doomspork/elixir-school/contributors","subscribers_url":"https://api.github.com/repos/doomspork/elixir-school/subscribers","subscription_url":"https://api.github.com/repos/doomspork/elixir-school/subscription","commits_url":"https://api.github.com/repos/doomspork/elixir-school/commits{/sha}","git_commits_url":"https://api.github.com/repos/doomspork/elixir-school/git/commits{/sha}","comments_url":"https://api.github.com/repos/doomspork/elixir-school/comments{/number}","issue_comment_url":"https://api.github.com/repos/doomspork/elixir-school/issues/comments{/number}","contents_url":"https://api.github.com/repos/doomspork/elixir-school/contents/{+path}","compare_url":"https://api.github.com/repos/doomspork/elixir-school/compare/{base}...{head}","merges_url":"https://api.github.com/repos/doomspork/elixir-school/merges","archive_url":"https://api.github.com/repos/doomspork/elixir-school/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/doomspork/elixir-school/downloads","issues_url":"https://api.github.com/repos/doomspork/elixir-school/issues{/number}","pulls_url":"https://api.github.com/repos/doomspork/elixir-school/pulls{/number}","milestones_url":"https://api.github.com/repos/doomspork/elixir-school/milestones{/number}","notifications_url":"https://api.github.com/repos/doomspork/elixir-school/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/doomspork/elixir-school/labels{/name}","releases_url":"https://api.github.com/repos/doomspork/elixir-school/releases{/id}","deployments_url":"https://api.github.com/repos/doomspork/elixir-school/deployments","created_at":"2015-05-31T18:27:42Z","updated_at":"2016-03-27T13:35:10Z","pushed_at":"2016-03-27T14:00:29Z","git_url":"git://github.com/doomspork/elixir-school.git","ssh_url":"git@github.com:doomspork/elixir-school.git","clone_url":"https://github.com/doomspork/elixir-school.git","svn_url":"https://github.com/doomspork/elixir-school","homepage":"https://elixirschool.com","size":868,"stargazers_count":336,"watchers_count":336,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":91,"mirror_url":null,"open_issues_count":36,"forks":91,"open_issues":36,"watchers":336,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/doomspork/elixir-school/pulls/229"},"html":{"href":"https://github.com/doomspork/elixir-school/pull/229"},"issue":{"href":"https://api.github.com/repos/doomspork/elixir-school/issues/229"},"comments":{"href":"https://api.github.com/repos/doomspork/elixir-school/issues/229/comments"},"review_comments":{"href":"https://api.github.com/repos/doomspork/elixir-school/pulls/229/comments"},"review_comment":{"href":"https://api.github.com/repos/doomspork/elixir-school/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/doomspork/elixir-school/pulls/229/commits"},"statuses":{"href":"https://api.github.com/repos/doomspork/elixir-school/statuses/f3c1059a0f4aa62904b176d27cfb1cadc5167a86"}}}}