Jesse, I see where you are going, but Elixir doesn't have currying. For instance, the last example you gave will return an error.
You could make it work by changing it a bit. `@spec const(a) :: fn(any() -> a) when a: var` So, const would return an fn instead of taking two arguments. I don't know how useful that would be in the long run. I know it is common in function programming, but I don't see it being extremely helpful in Elixir. I'm ready to be convinced. Cheers, Amos King CEO Binary Noggin http://binarynoggin.com #business https://elixiroutlaws.com #elixir podcast http://thisagilelife.com #podcast ======================================================= I welcome VSRE emails. Learn more at http://vsre.info/ ======================================================= On Wed, Jan 29, 2020 at 4:01 PM Jesse Claven <[email protected]> wrote: > Hey everyone! > > I propose adding `Function.const/2` as a function which for argument `x`, > would always return `x`. > > ``` > @spec const(any(), any()) :: any() > def const(_original_value, new_value), do: new_value > ``` > > This is somewhat similar to the new `Function.identity/1` in that it may > seem like we've easily lived without it, but it does afford some more > concise/semantic ways to represent a common pattern. > > ``` > fn _ -> something_else() end > ``` > > This would become: > > ``` > const(something_else()) > ``` > > ``` > Enum.map([0,1,2,3], &Function.const/2) > ``` > > I'm new to Elixir, so I'm not sure of the weight that the name `const` > would carry, so there could be a more acceptable name. > > If accepted, I'd be happy to create a PR! > > Equivalents in other languages: > > - Haskell: > https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Function.html#v:const > - PureScript: > https://pursuit.purescript.org/packages/purescript-const/4.1.0/docs/Data.Const > - Elm: https://package.elm-lang.org/packages/elm/core/latest/Basics#always > - Scala: > https://www.scala-lang.org/api/current/scala/Function$.html#const[T,U](x:T)(y:U):T > - Idris: > https://www.idris-lang.org/docs/current/prelude_doc/docs/Prelude.Basics.html#Prelude.Basics.const > > Thanks for your time. > > -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/576949a3-93e2-4117-b1f1-ab4621f10e88%40googlegroups.com > <https://groups.google.com/d/msgid/elixir-lang-core/576949a3-93e2-4117-b1f1-ab4621f10e88%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAJr6D4S%2BZF%2BM2SiP0WrjCdffGop61qv1awAEU7iNWL%3D3csoLZw%40mail.gmail.com.
