Skip to content

Clarify lambda positioning #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 19, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ let printVolumes x =
(convertVolumeImperialPint x)
```

The same guidelines apply for lambda expressions as function arguments. If the body of a lambda expression, the body can have another line, indented by one scope
The same guidelines apply for lambda expressions as function arguments. If the body of a lambda expression, the body can have another line, indented by one scope.

```fsharp
let printListWithOffset a list1 =
Expand All @@ -782,6 +782,21 @@ let printListWithOffset a list1 =
printfn "%d" (a + elem)
)
list1

// OK
let printListWithOffset a list1 =
list1
|> List.iter (fun elem ->
printfn "%d" (a + elem)
)

// OK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Smaug123, what if I have something like

    innerFunc<'a, 'b>
        path'
        elementNameThatHasThisRatherLongVariableNameToForceTheWholeThingOnMultipleLines
        (foo >> bar value >> List.item a)
        shape

should this be

    innerFunc<'a, 'b> path' elementNameThatHasThisRatherLongVariableNameToForceTheWholeThingOnMultipleLines (
        foo >> bar value >> List.item a
    ) shape

Or is there a stricter rule in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the whole thing wants to split then, i.e. your first example:

    innerFunc<'a, 'b>
        path'
        elementNameThatHasThisRatherLongVariableNameToForceTheWholeThingOnMultipleLines
        (foo >> bar value >> List.item a)
        shape

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So when is it

A:

let printListWithOffset a list1 =
    list1
    |> List.iter (
        ((+) a)
        >> printfn "%d"
    )

and when is it

B:

 innerFunc<'a, 'b>
        path'
        elementNameThatHasThisRatherLongVariableNameToForceTheWholeThingOnMultipleLines
        (foo >> bar value >> List.item a)
        shape

I'm still sort of looking for a technical rule so to speak.
A could be because:

  • No additional arguments are passed to List.iter
  • A pipe operator is preceding List.iter

B could be because:

  • Everything before (foo >> bar value >> List.item a) already makes the whole thing too long.
  • There are additional arguments next to (foo >> bar value >> List.item a), in A there are not.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's due to the fact that the lambda is the only argument (ignoring the one we are getting piped in).

For instance:

let foo =
    List.iter (
        ((+) a)
        >> printfn "%d"
    )

Would be allowed whereas:

let foo =
    someOtherThing a b c d (
       ((+) a)
       >> printfn "%d"
    ) f g h

Would be reformatted like B

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree: if there is just one argument that is too long, the whole thing gets split over many lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, the A example is a bit misleading. These are both right:

let printListWithOffset a list1 =
    list1
    |> List.iter (
        ((+) veryVeryVeryVeryVeryVeryVeryVeryVeryLongThing)
        >> printfn "%d"
    )

let printListWithOffset' a list1 =
    list1
    |> List.iter (((+) a) >> printfn "%d")

let printListWithOffset a list1 =
list1
|> List.iter (
((+) a)
>> printfn "%d"
)
```

However, if the body of a lambda expression is more than one line, consider factoring it out into a separate function rather than have a multi-line construct applied as a single argument to a function.
Expand Down