January 29, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Friday, 29 January 2016 at 23:20:38 UTC, Walter Bright wrote:
> On 1/29/2016 5:11 AM, Luís Marques wrote:
>> Just to bikeshed a little, I remember that when I first started using
>> std.algorithm I was ctrl-F'ing for "accumulate" and not finding it quite often.
>> D competes with C++ directly, so do consider that name :-)
>
> For algorithms and FP in general, we may be better off drawing inspiration from Haskell, as C++ does not have FP in its DNA.
So D is adding currying and builtin tuples? :^)
|
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luís Marques | On Friday, 29 January 2016 at 13:11:34 UTC, Luís Marques wrote: > On Friday, 29 January 2016 at 12:08:01 UTC, Andrei Alexandrescu wrote: >> So the next best solution is to introduce a new name such as the popular "fold", and put them together in the documentation. > > Just to bikeshed a little, I remember that when I first started using std.algorithm I was ctrl-F'ing for "accumulate" and not finding it quite often. D competes with C++ directly, so do consider that name :-) I'm not sure what the problem was, but the documentation for "reduce" already mentions "accumulate": https://dlang.org/library/std/algorithm/iteration/reduce.html |
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | On Friday, 29 January 2016 at 23:45:04 UTC, Ola Fosheim Grøstad wrote:
> So D is adding currying and builtin tuples? :^)
Yes. Come back in 10 years it'll be ready for you.
|
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Saturday, 30 January 2016 at 01:34:48 UTC, deadalnix wrote:
> On Friday, 29 January 2016 at 23:45:04 UTC, Ola Fosheim Grøstad wrote:
>> So D is adding currying and builtin tuples? :^)
>
> Yes. Come back in 10 years it'll be ready for you.
Currying is pointless, I believe Swift is removing it. I was joking, Haskell's naming scheme isn't particularly suited for an imperative language.
Look:
nub
fst
snd
chr
intercalate
unwords
inits
??
|
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | On Saturday, 30 January 2016 at 12:11:37 UTC, Ola Fosheim Grøstad wrote: > Currying is pointless, I believe Swift is removing it. ... It might be fairly useless in D but it's definitely not useless in general. It' a different design pattern and functional languages make great use of it. OTOH, D substitutes it with other design patterns like UFCS and template arguments, e.g. arr.sort!"a > b". Phobos even has a function which mimics currying via std.functional.partial: https://dlang.org/phobos/std_functional.html#partial |
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | On 1/30/16 12:25 PM, Xinok wrote:
> On Saturday, 30 January 2016 at 12:11:37 UTC, Ola Fosheim Grøstad wrote:
>> Currying is pointless, I believe Swift is removing it. ...
>
> It might be fairly useless in D but it's definitely not useless in
> general. It' a different design pattern and functional languages make
> great use of it. OTOH, D substitutes it with other design patterns like
> UFCS and template arguments, e.g. arr.sort!"a > b".
>
> Phobos even has a function which mimics currying via
> std.functional.partial:
>
> https://dlang.org/phobos/std_functional.html#partial
I forgot the distinction between currying and partial application. Can we also define currying in current D? -- Andrei
|
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Saturday, 30 January 2016 at 17:40:38 UTC, Andrei Alexandrescu wrote:
> I forgot the distinction between currying and partial application. Can we also define currying in current D? -- Andrei
Currying is turning (A, B, C) -> D into A -> (B -> (C -> D)), i.e. a function with multiple arguments into a sequence of functions that each take a single argument to apply each.
I think I've implemented something like that for fun once, but never really found much use for it. In the few places where I could have used it (mostly binary functions), just using a lambda and partial application seemed to be much more idiomatic. I guess D lacks any idioms that would make its use come naturally.
- David
|
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Saturday, 30 January 2016 at 17:40:38 UTC, Andrei Alexandrescu wrote: > On 1/30/16 12:25 PM, Xinok wrote: > > I forgot the distinction between currying and partial application. Can we also define currying in current D? -- Andrei I'm sure others can give an informed answer on the distinction, but a curried function takes only one argument, and I am unaware of a practical reason to add currying to D if we already have partial application. |
January 30, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dragos Carp | On 01/29/2016 02:56 PM, Dragos Carp wrote: > On Friday, 29 January 2016 at 13:11:34 UTC, Luís Marques wrote: >> Just to bikeshed a little, I remember that when I first started using >> std.algorithm I was ctrl-F'ing for "accumulate" and not finding it >> quite often. D competes with C++ directly, so do consider that name :-) > > But not in python, where "accumulate"[1] is the generic equivalent of > C++ "partial_sum"[2]. I like "fold" more. > > BTW this week, a colleague of mine implemented a python "accumulate" in > D. Is there any interest to contribute it to Phobos? How should this be > named? > > [1] https://docs.python.org/3/library/itertools.html#itertools.accumulate > [2] http://en.cppreference.com/w/cpp/algorithm/partial_sum "scan" http://hackage.haskell.org/package/base-4.8.2.0/docs/Prelude.html#v:scanl |
February 01, 2016 Re: reduce -> fold? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Friday, 29 January 2016 at 20:40:18 UTC, Andrei Alexandrescu wrote:
> That'd be interesting if (a) lazy and (b) general a la https://dlang.org/library/std/range/recurrence.html. -- Andrei
To be clear, by general you mean to allow functions with more than 2 arguments?
For example if you have:
foo(int i, int j, int k) { return i + j + k; }
then:
scan!foo([1, 2, 3, 4]).array returns [1, 2, 6, 12]
Is "scan" (thanks Timon) telling enough? The python "accumulate" conflicts with the C++ meaning.
|
Copyright © 1999-2021 by the D Language Foundation