Skip to content

Commit

Permalink
hotfix: Remove return mention
Browse files Browse the repository at this point in the history
  • Loading branch information
NickSeagull committed Oct 22, 2023
1 parent 9fe6b1b commit 24df612
Showing 1 changed file with 5 additions and 26 deletions.
31 changes: 5 additions & 26 deletions docs/essentials/functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

</TabItem>
Expand All @@ -118,7 +118,9 @@ function estimateShipping(weight, distance, costPerKm) {
</TabItem>
</Tabs>

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:
Expand All @@ -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
```

</TabItem>
Expand All @@ -144,29 +146,6 @@ function estimateShipping(weight, distance, costPerKm) {
</TabItem>
</Tabs>

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:

<Tabs>
<TabItem value="neohaskell" label="NeoHaskell">

```haskell
estimateShipping weight distance costPerKm =
distance * costPerKm + weight * 2
```

</TabItem>

<TabItem value="js" label="JavaScript">

```js
const estimateShipping = (weight, distance, costPerKm) =>
distance * costPerKm + weight * 2;
```

</TabItem>
</Tabs>

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.**

Expand Down

0 comments on commit 24df612

Please sign in to comment.