From 24df612ccdc94845d4bfa75497b22759a7b4ad89 Mon Sep 17 00:00:00 2001 From: Nikita Tchayka Date: Sun, 22 Oct 2023 20:58:28 +0100 Subject: [PATCH] hotfix: Remove return mention --- docs/essentials/functions.mdx | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/docs/essentials/functions.mdx b/docs/essentials/functions.mdx index 3b27dee..bedff32 100644 --- a/docs/essentials/functions.mdx +++ b/docs/essentials/functions.mdx @@ -100,7 +100,7 @@ Let's write a function that calculates the shipping cost, given the weight, dist estimateShipping weight distance costPerKm = do let distanceCost = distance * costPerKm let weightCost = weight * 2 - return (distanceCost + weightCost) + distanceCost + weightCost ``` @@ -118,7 +118,9 @@ function estimateShipping(weight, distance, costPerKm) { -In the example above, we're using a **block** to define a function, so it allows us to define constants with the `let` keyword, and to return a value with the `return` keyword. Note how that the `return` keyword requires you to wrap the return value in parentheses. +In the example above, we're using a **block** to define a function, so it allows us to define constants with the `let` keyword. Note how that the last line in a block is +returned, so there's no need to write `return` like in +other languages. **Blocks are optional**, and if we wanted to write the same function without it, removing all the constants, and writing the calculation inline, we could do it like this: @@ -128,7 +130,7 @@ and writing the calculation inline, we could do it like this: ```haskell estimateShipping weight distance costPerKm = - return (distance * costPerKm + weight * 2) + distance * costPerKm + weight * 2 ``` @@ -144,29 +146,6 @@ function estimateShipping(weight, distance, costPerKm) { -In fact, the last line of a NeoHaskell function will be always returned, so we can remove the `return` keyword. -In JavaScript, this is not possible with a regular function, so we have to change the JavaScript code so it is an arrow function: - - - - -```haskell -estimateShipping weight distance costPerKm = - distance * costPerKm + weight * 2 -``` - - - - - -```js -const estimateShipping = (weight, distance, costPerKm) => - distance * costPerKm + weight * 2; -``` - - - - This way of writing code helps writing data processing code and validation rules in a very readable way. **But be careful**, because one-liners can easily become hard to read, so **it is advised to use blocks for more complex functions.**