Skip to content
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

workspace command: extract an anonymous function to a private one #436

Open
NJichev opened this issue Apr 24, 2024 · 1 comment
Open

workspace command: extract an anonymous function to a private one #436

NJichev opened this issue Apr 24, 2024 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers workspace-command

Comments

@NJichev
Copy link
Collaborator

NJichev commented Apr 24, 2024

A workspace command that extracts an anonymous function to a private one.

Example:

defmodule Example do
  def run(list) do
    square = fn x -> x * x end
    Enum.map(list, square)
  end
end

Hovering the variable of the anonymous function or the anonymous function definition and using the workspace command should result in:

defmodule Example do
  def run(list) do
    Enum.map(list, &square/1)
  end

  defp square(x) do
    x * x
  end
end

Consider the following scenarios:

  • The anonymous function binds variables from out of their scope
square_sum = fn x -> x * x + constant end
# should result in:
defp square_sum(x, constant), do: ...
  • Any function calls should be converted as well: square.(3) -> square(3)
  • An inlined anonymous function that is not bound to a variable, will need a default name to use
@NJichev NJichev added enhancement New feature or request workspace-command good first issue Good for newcomers labels Apr 24, 2024
@mhanberg
Copy link
Collaborator

similar to #437 , i think i would expect this work work as follows

defmodule Example do
  def run(list) do
    square = fn x -> x * x end
    Enum.map(list, square)
  end
end

refactors to

defmodule Example do
  def run(list) do
    square = &square/1
    Enum.map(list, square)
  end

  defp square(x) do
    x * x
  end
end

this reduces complexity, and fits with the more intuitive (IMO) behavior if you were to transform an inline anonymous function, like this

defmodule Example do
  def run(list) do
    Enum.map(list, fn x -> x * x end)
  end
end

refactors to

defmodule Example do
  def run(list) do
    Enum.map(list, &square/1)
  end

  defp square(x) do
    x * x
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers workspace-command
Projects
None yet
Development

No branches or pull requests

2 participants