Zulip Chat Archive

Stream: lean4

Topic: chained dot notation


Juan Pablo Romero (Jul 01 2022 at 20:21):

Hi, curious if there's a way to write this expression:

([1, 2].map (. + 1)).map toString

in this style:

[1, 2]
  .map (. + 1)
  .map toString

(to mimic chained dot notation in OO languages)

Henrik Böving (Jul 01 2022 at 20:25):

Not exactly but very close:

#eval #[1, 2]
        |>.map (. + 1)
        |>.map toString

Henrik Böving (Jul 01 2022 at 20:27):

In general after a some expression |> you can always use .foo (note that there must not be a blank before the dot here, if there is it has a completely different semantic) to mimick (some epxression).foo

Juan Pablo Romero (Jul 01 2022 at 20:28):

ah, pretty cool!

Henrik Böving (Jul 01 2022 at 20:31):

If you want to know what an alone standing .foo would mean, basically if Lean expects a function that results in a type a at this point it will look up the name foo in namespace a so writing e.g.

def foo : Array Nat := #[1,2,3].map (.add 12)

will make Lean use Nat.add here, you can also use this in pattern matching:

def test : Nat  Nat
  | .zero => 0
  | .succ n => n

Juan Pablo Romero (Jul 01 2022 at 20:33):

yes, I love that feature


Last updated: Dec 20 2023 at 11:08 UTC