Re: [elixir-core:9360] [Proposal] Add Function.const/2

2020-02-04 Thread Austin Ziegler
On Tue, Feb 4, 2020 at 10:04 AM Ben Wilson  wrote:

> > The code Function.constant(c) is much more expressive and descriptive
> than fn _ -> c end.
>
> To be clear, if this could work without macros, I'd be inclined to agree,
> although I recognize that there may be some who do not. My issue personally
> is that there simply isn't any function you can write that will behave this
> way, for all values of `c`. You could build `constant` as a macro, but I
> think that ends up killing the whole "name a concept" thing, because really
> the dominant concept at hand ends up being how macros work.
>

I don’t really have a strong opinion on this proposal, but I do think that
this could be done without macros and if naming a concept is important then
it’s worth considering. I do see one problem with `Function.constant/1` in
how it would be applied, because there would potentially need to be some
magic involved (which brings it back to possible macros).

The simplest implementation would be:

def constant(const), do: fn -> const end

That will work well for `Stream.repeatedly/1` as the function parameter is
`/0`, but not so well for `Enum.map/2` as the function parameter is `/1`.
This could be addressed with multiple functions (constant1/1, etc.) or with
an arity parameter (both examples shown below):

def constant(const), do: fn -> const end
def constant1(const), do: fn _ -> const end # OR
def constant(const, arity \\ 0)
def constant(const, 0), do: fn -> const end
def constant(const, 1), do: fn _ -> const end

I’m not sure it would be possible to know, at the call-site, whether you
needed a `/0` or `/1` anonymous function through macros, and I’m not sure
that it would be meaningful to have constant for _larger_ than `/{0,1}`
functions. But I do know that I have occasionally wanted to write:

Enum.map([0, 1, 2], &:padding) # or any other non-function value

and have it act as the correct type of function.

-a


> On Tuesday, February 4, 2020 at 9:39:48 AM UTC-5, Bruce Tate wrote:
>>
>> Thanks for the discussion, all. I enjoy being here.
>>
>> *Summary: I will bow out of this conversation after this one last
>> attempt. Feel free to skip it. *
>>
>> I will say that I would use this function frequently in my teaching and
>> in my day-to-day code if I had it. I think it describes a concept that is
>> well known in math, and useful in any functional language. For example,
>> resetting a counter in an agent or genserver with a similar API. Creating a
>> stream of constants as padding/initialization data of arbitrary length
>> (with take later).
>>
>> > we'll bloat the language.
>>
>> I don't think this is a good argument for foundational concepts. A
>> library that provides names for significant functions in math and
>> programming is important enough.
>>
>> > I’d argue back that this particular pattern, where you want a list of
>> fixed length with the same value, is much better served by
>> `List.duplicate/2`.
>>
>> Maybe. But List.duplicate/2 doesn't really solve the same problem.
>> Stream.repeatedly with Function.constant gives me  padding of an arbitrary
>> length, that I can then later instantiate based on the number of columns
>> that I need. It's a great pattern. It's basically fixed vs variable.
>>
>> > if I know anonymous functions, I know what’s going on.
>>
>> Variables, anonymous functions, and the like are not equivalent. A
>> library function has weight and descriptive ability that writing code just
>> doesn't. I can tell you that `fn(x) -> x end` and `Function.identity` are
>> not remotely equivalent when it comes to scanning code. There's a cost to
>> that much syntax.
>>
>> I'll say one more thing before moving on from this conversation.
>>
>> Naming concepts in code is important. The code Function.constant(c) is
>> much more expressive and descriptive than fn _ -> c end. It gives us a
>> common language across programming languages and theory plus math to
>> express ideas. We in the Elixir community often undersell the idea, and we
>> pay in the idioms that new coders must learn to be successful with our
>> language.
>>
>> *So thanks to all for a great discussion. I do recognize there's no
>> appetite for these ideas in Elixir, so I'll gracefully bow out. *
>>
>> -bt
>>
>>
>>
>> On Tue, Feb 4, 2020 at 8:58 AM Amos King  wrote:
>>
>>> Ben,
>>>
>>> That is how const is used in Haskell. Although without currying I don’t
>>> see how it is useful. I’m waiting to see an example that drives it home. I
>>> agree with Bruce about naming concepts but I don’t see the concept as
>>> useful in Elixir.
>>>
>>> Bruce, do you have a code sample using the idea?
>>>
>>> Amos King
>>> CEO
>>> Binary Noggin
>>>
>>> On Feb 4, 2020, at 07:35, Ben Wilson  wrote:
>>>
>>> 
>>> Addendum: I re-read the proposal because the const/1 vs const/2 thing
>>> confused me, and I'm seeing both in play there. The spec is arity 2, the
>>> example right after though is arity 1, and the Enum example is arity 2 but
>>> without a constant 

Re: [elixir-core:9360] [Proposal] Add Function.const/2

2020-02-04 Thread Ben Wilson
> The code Function.constant(c) is much more expressive and descriptive 
than fn _ -> c end.

To be clear, if this could work without macros, I'd be inclined to agree, 
although I recognize that there may be some who do not. My issue personally 
is that there simply isn't any function you can write that will behave this 
way, for all values of `c`. You could build `constant` as a macro, but I 
think that ends up killing the whole "name a concept" thing, because really 
the dominant concept at hand ends up being how macros work.

On Tuesday, February 4, 2020 at 9:39:48 AM UTC-5, Bruce Tate wrote:
>
> Thanks for the discussion, all. I enjoy being here. 
>
> *Summary: I will bow out of this conversation after this one last attempt. 
> Feel free to skip it. *
>
> I will say that I would use this function frequently in my teaching and in 
> my day-to-day code if I had it. I think it describes a concept that is well 
> known in math, and useful in any functional language. For example, 
> resetting a counter in an agent or genserver with a similar API. Creating a 
> stream of constants as padding/initialization data of arbitrary length 
> (with take later). 
>
> > we'll bloat the language. 
>
> I don't think this is a good argument for foundational concepts. A library 
> that provides names for significant functions in math and programming is 
> important enough.
>
> > I’d argue back that this particular pattern, where you want a list of 
> fixed length with the same value, is much better served by 
> `List.duplicate/2`.
>
> Maybe. But List.duplicate/2 doesn't really solve the same problem. 
> Stream.repeatedly with Function.constant gives me  padding of an arbitrary 
> length, that I can then later instantiate based on the number of columns 
> that I need. It's a great pattern. It's basically fixed vs variable. 
>
> > if I know anonymous functions, I know what’s going on.
>
> Variables, anonymous functions, and the like are not equivalent. A library 
> function has weight and descriptive ability that writing code just doesn't. 
> I can tell you that `fn(x) -> x end` and `Function.identity` are not 
> remotely equivalent when it comes to scanning code. There's a cost to that 
> much syntax. 
>
> I'll say one more thing before moving on from this conversation. 
>
> Naming concepts in code is important. The code Function.constant(c) is 
> much more expressive and descriptive than fn _ -> c end. It gives us a 
> common language across programming languages and theory plus math to 
> express ideas. We in the Elixir community often undersell the idea, and we 
> pay in the idioms that new coders must learn to be successful with our 
> language.
>
> *So thanks to all for a great discussion. I do recognize there's no 
> appetite for these ideas in Elixir, so I'll gracefully bow out. *
>
> -bt
>
>
>
> On Tue, Feb 4, 2020 at 8:58 AM Amos King  > wrote:
>
>> Ben,
>>
>> That is how const is used in Haskell. Although without currying I don’t 
>> see how it is useful. I’m waiting to see an example that drives it home. I 
>> agree with Bruce about naming concepts but I don’t see the concept as 
>> useful in Elixir.
>>
>> Bruce, do you have a code sample using the idea?
>>
>> Amos King
>> CEO
>> Binary Noggin
>>
>> On Feb 4, 2020, at 07:35, Ben Wilson > 
>> wrote:
>>
>> 
>> Addendum: I re-read the proposal because the const/1 vs const/2 thing 
>> confused me, and I'm seeing both in play there. The spec is arity 2, the 
>> example right after though is arity 1, and the Enum example is arity 2 but 
>> without a constant value. The Enum example perhaps makes the most sense, 
>> because you could do:
>>
>> ```
>> Enum.map([1,2,3,4], (&1, :foo))
>> ```
>> which would return `[:foo, :foo, :foo, :foo]` effectively replacing the 
>> contents of the list with all `:foo`. Is that the idea?
>>
>> On Tuesday, February 4, 2020 at 8:16:53 AM UTC-5, Ben Wilson wrote:
>>>
>>> I agree with Michal. Additionally, I'm not clear how `const/1` could be 
>>> used in Bruce's example at all.
>>>
>>> To elaborate,  `fn -> foo() end` and `const(foo())` cannot be equivalent 
>>> when `const/1` is merely a function. This becomes readily apparent when 
>>> `foo()` is side effects or side causes. In the first case, `foo()` is never 
>>> evaluated until the wrapping function is called, in the case of `const/1` 
>>> however the function is evaluated and then its return value bound over by a 
>>> function. Eg: `fn -> DateTime.utc_now() end` will always return the current 
>>> time when evaluated, where as `const(DateTime.utc_now())` will evaluate the 
>>> current time once and then always return that same time.
>>>
>>> That might sound useful, except that we already can do that by simply 
>>> binding the return value of `DateTime.utc_now()` to a variable and passing 
>>> that variable around. I'm having difficulty coming up with a scenario 
>>> where, instead of simply having the value, I have the value wrapped in an 
>>> anonymous function that I need to