Re: Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread steve via Digitalmars-d-learn
thanks a lot both! Yes I'm aware that map exists already. This was didactic. I had tried to find out whether lambdas generate function pointers but also couldn't figure that one out :D

Re: Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 21 February 2022 at 10:04:16 UTC, steve wrote: I am trying to implement a simple map function. I found code to do this in another post but it only seems to work with lambda functions and I do not understand why. Any help would be greatly appreciated ``` import std.stdio; T[]

Re: Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread partypooper via Digitalmars-d-learn
On Monday, 21 February 2022 at 10:04:16 UTC, steve wrote: I am trying to implement a simple map function. I found code to do this in another post but it only seems to work with lambda functions and I do not understand why. Any help would be greatly appreciated ``` import std.stdio; T[]

Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread steve via Digitalmars-d-learn
I am trying to implement a simple map function. I found code to do this in another post but it only seems to work with lambda functions and I do not understand why. Any help would be greatly appreciated ``` import std.stdio; T[] map_vals(T,S)(scope T function(S) f, S[] a){ auto b = new

Re: Higher-order functions?

2012-04-11 Thread Xan
full runtime support for higher-order functions and closures. import std.stdio; int[] map(scope int delegate(int) f, int[] a){ auto b = new int[a.length]; foreach(i,x;a) b[i] = f(x); return b; } void main(){ int a = 2; writeln(map(x=a*x, [1,2,3])); } (I'm not talking about

Re: Higher-order functions?

2012-04-11 Thread Jacob Carlborg
On 2012-04-11 10:45, Xan wrote: Good answer. For the other hand, what is the simplest method for implementing this (in pseucode) in D: Sure: FUNC someprocedure(int a, int b, funcint, int: int f) int RETURN f(a, b) } And call it with: IO.writeLine(add: .. someprocedure(2, 3, { a, b = a + b

Re: Higher-order functions?

2012-04-11 Thread Xan
On Wednesday, 11 April 2012 at 09:17:12 UTC, Jacob Carlborg wrote: On 2012-04-11 10:45, Xan wrote: Good answer. For the other hand, what is the simplest method for implementing this (in pseucode) in D: Sure: FUNC someprocedure(int a, int b, funcint, int: int f) int RETURN f(a, b) } And

Re: Higher-order functions?

2012-04-11 Thread Timon Gehr
On 04/11/2012 11:37 AM, Xan wrote: On Wednesday, 11 April 2012 at 09:17:12 UTC, Jacob Carlborg wrote: On 2012-04-11 10:45, Xan wrote: Good answer. For the other hand, what is the simplest method for implementing this (in pseucode) in D: Sure: FUNC someprocedure(int a, int b, funcint, int:

Re: Higher-order functions?

2012-04-11 Thread Xan
On Wednesday, 11 April 2012 at 09:43:27 UTC, Timon Gehr wrote: On 04/11/2012 11:37 AM, Xan wrote: On Wednesday, 11 April 2012 at 09:17:12 UTC, Jacob Carlborg wrote: On 2012-04-11 10:45, Xan wrote: Good answer. For the other hand, what is the simplest method for implementing this (in

Re: Higher-order functions?

2012-04-11 Thread Mirko Pilger
What is the error? e.g. try this: auto someprocedure (int a, int b, int delegate (int, int) f)

Re: Higher-order functions?

2012-04-11 Thread Xan
On Wednesday, 11 April 2012 at 10:14:21 UTC, Mirko Pilger wrote: What is the error? e.g. try this: auto someprocedure (int a, int b, int delegate (int, int) f) I receive the same error

Re: Higher-order functions?

2012-04-11 Thread Timon Gehr
On 04/11/2012 11:51 AM, Xan wrote: On Wednesday, 11 April 2012 at 09:43:27 UTC, Timon Gehr wrote: On 04/11/2012 11:37 AM, Xan wrote: On Wednesday, 11 April 2012 at 09:17:12 UTC, Jacob Carlborg wrote: On 2012-04-11 10:45, Xan wrote: Good answer. For the other hand, what is the simplest

Re: Higher-order functions?

2012-04-11 Thread Xan
Apparently your compiler does not support parameter type deduction yet. void main () { writeln(add: , someprocedure(2, 3, (int a, int b) { return a + b; })); writeln(multiply: , someprocedure(2, 3, (int a, int b) { return a * b; })); } Yes, now it works! Thanks,

Re: Higher-order functions?

2012-04-11 Thread Jonas H.
Wow, thanks for all the answers! Seems to be a great community here. What do you guys think about adding the term anonymous functions to the frontpage and features page? I think that one's a lot more comman than delegates (even if it's not exactly the same thing).

Re: Higher-order functions?

2012-04-11 Thread H. S. Teoh
On Wed, Apr 11, 2012 at 07:29:20PM +0200, Jonas H. wrote: Wow, thanks for all the answers! Seems to be a great community here. Welcome to the community! :-) What do you guys think about adding the term anonymous functions to the frontpage and features page? I think that one's a lot more

Higher-order functions?

2012-04-10 Thread Jonas H.
Hi everyone, does D have any runtime higher-order function facilities? (I'm not talking about templates.) More specifically, is something like this possible? (That's how I'd do it in Python) car_prices = map(Car.get_price, list_of_cars) car = new Car foobar(car.get_price) Thanks Jonas

Re: Higher-order functions?

2012-04-10 Thread H. S. Teoh
On Wed, Apr 11, 2012 at 01:13:17AM +0200, Jonas H. wrote: Hi everyone, does D have any runtime higher-order function facilities? (I'm not talking about templates.) More specifically, is something like this possible? (That's how I'd do it in Python) car_prices = map(Car.get_price,

Re: Higher-order functions?

2012-04-10 Thread Timon Gehr
On 04/11/2012 01:13 AM, Jonas H. wrote: Hi everyone, does D have any runtime higher-order function facilities? D has full runtime support for higher-order functions and closures. import std.stdio; int[] map(scope int delegate(int) f, int[] a){ auto b = new int[a.length]; foreach(i,x

Re: Higher-order functions?

2012-04-10 Thread Nick Sabalausky
Jonas H. jo...@lophus.org wrote in message news:mailman.1600.1334099651.4860.digitalmars-d-le...@puremagic.com... Hi everyone, does D have any runtime higher-order function facilities? (I'm not talking about templates.) Yes. Fully. Many are already in the std lib:

Re: Higher-order functions?

2012-04-10 Thread Nick Sabalausky
Timon Gehr timon.g...@gmx.ch wrote in message news:jm2hnp$s0s$1...@digitalmars.com... (Well, the standard way to do what that python code does is using templates. auto car_prices = map!(car = car.get_price)(list_of_cars);// lazy range auto car_prices = array(map!(car =

Re: Pure and higher-order functions

2012-02-24 Thread mist
Ok, finally understood. I was trying to declare hof like this: void f2( pure int function() param ) , similar to the way I declare usual pure functions. Looks like it is syntax error and only void f2( int function() pure param ) is allowed. That led me to false conclusion, that such

Re: Pure and higher-order functions

2012-02-24 Thread bearophile
mist: Are there any reasons for this inconsistency? I don't know. Maybe it's just a parser bug. There are some of those in Bugzilla. If you don't like it, then I suggest you to add it to D Bugzilla. Bye, bearophile

Re: Pure and higher-order functions

2012-02-24 Thread mist
Actually, looks like you have done it already 2 years ago :) http://d.puremagic.com/issues/show_bug.cgi?id=4505

Pure and higher-order functions

2012-02-23 Thread mist
Hello! I have been asked few question recently from a Haskell programmer about D2 and, after experimenting a bit, have found that I really can't provide a good answe myself, as I am not getting a design limititations (if any). Here is the snippet, it is pretty self-descriptive:

Re: Pure and higher-order functions

2012-02-23 Thread deadalnix
Le 23/02/2012 21:00, mist a écrit : Hello! I have been asked few question recently from a Haskell programmer about D2 and, after experimenting a bit, have found that I really can't provide a good answe myself, as I am not getting a design limititations (if any). Here is the snippet, it is

Re: Pure and higher-order functions

2012-02-23 Thread mist
But is there any way to actually say D compiler that I want this function to accept only pure delegates?

Re: Pure and higher-order functions

2012-02-23 Thread Jonathan M Davis
On Thursday, February 23, 2012 21:17:46 mist wrote: But is there any way to actually say D compiler that I want this function to accept only pure delegates? Mark the delegate type that it accepts as pure. - Jonathan M Davis

tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT
Hello List: In tDlp book in section 5.6 entitled `Higher-Order Functions. Function Literals, the first code example is: - T[] find(alias pred, T)(T[] input) if (is(typeof(pred(input[0])) == bool

Re: tdlp: higher-order functions

2012-01-20 Thread Alex Rønne Petersen
On 20-01-2012 15:32, Jerome BENOIT wrote: Hello List: In tDlp book in section 5.6 entitled `Higher-Order Functions. Function Literals, the first code example is: - T[] find(alias pred, T)(T[] input) if (is(typeof(pred(input[0

Re: tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT
Thanks. Let go further. On 20/01/12 15:58, Alex Rønne Petersen wrote: On 20-01-2012 15:32, Jerome BENOIT wrote: Hello List: In tDlp book in section 5.6 entitled `Higher-Order Functions. Function Literals, the first code example

Re: tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT
Hello Again: On 20/01/12 15:58, Alex Rønne Petersen wrote: On 20-01-2012 15:32, Jerome BENOIT wrote: Hello List: In tDlp book in section 5.6 entitled `Higher-Order Functions. Function Literals, the first code example is: - T

Re: tdlp: higher-order functions

2012-01-20 Thread Alex Rønne Petersen
On 20-01-2012 17:14, Jerome BENOIT wrote: Hello Again: On 20/01/12 15:58, Alex Rønne Petersen wrote: On 20-01-2012 15:32, Jerome BENOIT wrote: Hello List: In tDlp book in section 5.6 entitled `Higher-Order Functions. Function Literals, the first code example

Re: tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT
On 20/01/12 17:23, Alex Rønne Petersen wrote: On 20-01-2012 17:14, Jerome BENOIT wrote: Hello Again: On 20/01/12 15:58, Alex Rønne Petersen wrote: On 20-01-2012 15:32, Jerome BENOIT wrote: Hello List: In tDlp book in section 5.6 entitled `Higher-Order Functions. Function Literals

Re: tdlp: higher-order functions

2012-01-20 Thread Ali Çehreli
On 01/20/2012 08:43 AM, Jerome BENOIT wrote: - T[] find(alias pred, T)(T[] input) if (is(typeof(pred(input[0])) == bool)) { for(; input.length 0; input = input[1 .. $]) { if (pred(input[0])) break; } return input; }

Re: tdlp: higher-order functions

2012-01-20 Thread Tobias Pankrath
Philippe Sigaud's template document covers everything about templates: https://github.com/PhilippeSigaud/D-templates-tutorial (Just download the pdf there.) This should be on the website.

Re: higher-order functions

2010-11-01 Thread spir
On Sun, 31 Oct 2010 20:24:59 -0600 Rainer Deyke rain...@eldwood.com wrote: On 10/31/2010 16:57, Simen kjaeraas wrote: For very short functions, strings are better, because of the length of the 'return' keyword. Had D instead always returned the result of the last line of a function (unless

Re: higher-order functions

2010-11-01 Thread bearophile
Jesse Phillips: Since when? You are right, what I have said doesn't apply to map/filter. Sorry for my silly mistake. Bye, bearophile

Re: higher-order functions

2010-10-31 Thread spir
On Sun, 31 Oct 2010 17:31:54 -0400 Nick Sabalausky a...@a.a wrote: spir denis.s...@gmail.com wrote in message news:mailman.45.1288523296.21107.digitalmars-d-le...@puremagic.com... Also, I could not find functional methods like map, filter, reduce in std.functional. Where else? Also not in

Re: higher-order functions

2010-10-31 Thread Simen kjaeraas
spir denis.s...@gmail.com wrote: On Sun, 31 Oct 2010 17:31:54 -0400 Nick Sabalausky a...@a.a wrote: spir denis.s...@gmail.com wrote in message news:mailman.45.1288523296.21107.digitalmars-d-le...@puremagic.com... Also, I could not find functional methods like map, filter, reduce in