Re: dlang.org/spec/function.html#pure-functions example

2023-10-28 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 16 October 2023 at 18:05:04 UTC, Paul wrote: On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis wrote: look like? Types can have static members. Basically what it comes down to is that outside of immutable data, pure functions only have access to their arguments and

Re: dlang.org/spec/function.html#pure-functions example

2023-10-21 Thread Jonathan M Davis via Digitalmars-d-learn
hat outside of immutable > > data, pure functions only have access to their arguments and to > > what they can access via their arguments (be it by getting > > pointers from those arguments or calling other pure functions > > on them). > > > > - Jonathan M Davis >

Re: dlang.org/spec/function.html#pure-functions example

2023-10-21 Thread Jonathan M Davis via Digitalmars-d-learn
hat outside of immutable > > data, pure functions only have access to their arguments and to > > what they can access via their arguments (be it by getting > > pointers from those arguments or calling other pure functions > > on them). > > > > - Jonathan M Davis

Re: dlang.org/spec/function.html#pure-functions example

2023-10-16 Thread Paul via Digitalmars-d-learn
On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis wrote: look like? Types can have static members. Basically what it comes down to is that outside of immutable data, pure functions only have access to their arguments and to what they can access via their arguments (be it by

Re: dlang.org/spec/function.html#pure-functions example

2023-10-16 Thread Paul via Digitalmars-d-learn
On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis wrote: Thanks Jonathan

Re: dlang.org/spec/function.html#pure-functions example

2023-10-13 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 12 October 2023 at 19:33:32 UTC, Paul wrote: If **int x** is global mutable state, what does static mutable state look like? In addition to Jonathan's reply, see: https://dlang.org/spec/function.html#local-static-variables

Re: dlang.org/spec/function.html#pure-functions example

2023-10-12 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, October 12, 2023 1:33:32 PM MDT Paul via Digitalmars-d-learn wrote: > The spec doc has the following statement and corresponding > example: > ***"Pure functions cannot directly access global or static > mutable state."*** > > ```d > int x; > immut

Re: how to benchmark pure functions?

2022-10-29 Thread max haughton via Digitalmars-d-learn
On Thursday, 27 October 2022 at 18:41:36 UTC, Dennis wrote: On Thursday, 27 October 2022 at 17:17:01 UTC, ab wrote: How can I prevent the compiler from removing the code I want to measure? With many C compilers, you can use volatile assembly blocks for that. With LDC -O3, a regular assembly b

Re: how to benchmark pure functions?

2022-10-28 Thread Siarhei Siamashka via Digitalmars-d-learn
On Friday, 28 October 2022 at 09:48:14 UTC, ab wrote: Thanks to H.S. Teoh and Dennis for the suggestions, they both work. I like the empty asm block a bit more because it is less invasive, but it only works with ldc. I used the volatileLoad/volatileStore functions to ensure that the compiler

Re: how to benchmark pure functions?

2022-10-28 Thread Imperatorn via Digitalmars-d-learn
On Friday, 28 October 2022 at 09:48:14 UTC, ab wrote: On Thursday, 27 October 2022 at 17:17:01 UTC, ab wrote: [...] Thanks to H.S. Teoh and Dennis for the suggestions, they both work. I like the empty asm block a bit more because it is less invasive, but it only works with ldc. @Imperatorn

Re: how to benchmark pure functions?

2022-10-28 Thread ab via Digitalmars-d-learn
On Thursday, 27 October 2022 at 17:17:01 UTC, ab wrote: Hi, when trying to compare different implementations of the optimized builds of a pure function using benchmark from std.datetime.stopwatch, I get times equal to zero, I suppose because the functions are not executed as they do not have

Re: how to benchmark pure functions?

2022-10-27 Thread Dennis via Digitalmars-d-learn
On Thursday, 27 October 2022 at 17:17:01 UTC, ab wrote: How can I prevent the compiler from removing the code I want to measure? With many C compilers, you can use volatile assembly blocks for that. With LDC -O3, a regular assembly block also does the trick currently: ```D void main() {

Re: how to benchmark pure functions?

2022-10-27 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 27, 2022 at 06:20:10PM +, Imperatorn via Digitalmars-d-learn wrote: > On Thursday, 27 October 2022 at 17:17:01 UTC, ab wrote: > > Hi, > > > > when trying to compare different implementations of the optimized > > builds of a pure function using benchmark from > > std.datetime.stopw

Re: how to benchmark pure functions?

2022-10-27 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 27 October 2022 at 17:17:01 UTC, ab wrote: Hi, when trying to compare different implementations of the optimized builds of a pure function using benchmark from std.datetime.stopwatch, I get times equal to zero, I suppose because the functions are not executed as they do not have

how to benchmark pure functions?

2022-10-27 Thread ab via Digitalmars-d-learn
Hi, when trying to compare different implementations of the optimized builds of a pure function using benchmark from std.datetime.stopwatch, I get times equal to zero, I suppose because the functions are not executed as they do not have side effects. The same happens with the example from t

Re: Map, filter and pure functions

2019-11-29 Thread Paul Backus via Digitalmars-d-learn
On Friday, 29 November 2019 at 15:54:13 UTC, realhet wrote: On Friday, 29 November 2019 at 15:49:24 UTC, Paul Backus wrote: It's actually a much simpler reason: filter calls .front twice for each element in its input (once to check if the value satisfies the predicate, and then again to return

Re: Map, filter and pure functions

2019-11-29 Thread realhet via Digitalmars-d-learn
On Friday, 29 November 2019 at 15:49:24 UTC, Paul Backus wrote: It's actually a much simpler reason: filter calls .front twice for each element in its input (once to check if the value satisfies the predicate, and then again to return the value if it does), and the range returned by map doesn't

Re: Map, filter and pure functions

2019-11-29 Thread realhet via Digitalmars-d-learn
On Friday, 29 November 2019 at 15:30:22 UTC, realhet wrote: ... Unfortunately function purity is not the answer. I put a very long calculation into the transform function which is called from "map!". And the "filter!" is making the "map!" call my function 2 times: First for the "filter!" to

Re: Map, filter and pure functions

2019-11-29 Thread Paul Backus via Digitalmars-d-learn
On Friday, 29 November 2019 at 15:30:22 UTC, realhet wrote: Hi, I have an input range. I use the map! on it to transform using a function. Finally I use filter! on the transformed data. When I do a foreach on this, i noticed, that the transform function is called twice for each element. If I r

Map, filter and pure functions

2019-11-29 Thread realhet via Digitalmars-d-learn
Hi, I have an input range. I use the map! on it to transform using a function. Finally I use filter! on the transformed data. When I do a foreach on this, i noticed, that the transform function is called twice for each element. If I remove the filter! it does only one transform function call p

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Nordlöw via Digitalmars-d-learn
On Saturday, 17 February 2018 at 14:54:37 UTC, ag0aep6g wrote: Nordlöw's methods are only weakly pure. They have mutable indirections either in the return type or in a parameter type. So calls to them should not be optimized away. I found a solution at https://github.com/nordlow/phobos-next/b

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/17/18 9:54 AM, ag0aep6g wrote: On 02/17/2018 03:04 PM, Steven Schveighoffer wrote: You have to be a bit careful here. pure functions can assume nothing is happening and simply not call the function. That's only a problem when the called function is strongly pure, right? Nord

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Eduard Staniloiu via Digitalmars-d-learn
On Saturday, 17 February 2018 at 12:33:25 UTC, Nordlöw wrote: I'm struggling with making https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d callable in pure functions such as here https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84 Should

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread ag0aep6g via Digitalmars-d-learn
On 02/17/2018 03:04 PM, Steven Schveighoffer wrote: You have to be a bit careful here. pure functions can assume nothing is happening and simply not call the function. That's only a problem when the called function is strongly pure, right? Nordlöw's methods are only weakly pure.

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Steven Schveighoffer via Digitalmars-d-learn
used in `pure` code. You have to be a bit careful here. pure functions can assume nothing is happening and simply not call the function. -Steve

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/17/18 7:33 AM, Nordlöw wrote: I'm struggling with making https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d callable in pure functions such as here https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84 Shouldn't a shared

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread ag0aep6g via Digitalmars-d-learn
On 02/17/2018 01:35 PM, rikki cattermole wrote: pure means no globals. As in none :) ... except immutable ones. And since PureMallocator has no fields, `instance` can be made immutable, and all the methods can be made static or const. Then they can be used in `pure` code.

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Nordlöw via Digitalmars-d-learn
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote: in pure functions? pure means no globals. As in none :) I guess one solution is to make the member functions in PureMallocator static and change how the template argument `Allocator` for a container is used to call

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread rikki cattermole via Digitalmars-d-learn
On 17/02/2018 12:48 PM, Nordlöw wrote: On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote: in pure functions? pure means no globals. As in none :) I don't understand. I thought std.experimental.allocators API was designed to be able express these needs, @and

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Nordlöw via Digitalmars-d-learn
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote: in pure functions? pure means no globals. As in none :) I don't understand. I thought std.experimental.allocators API was designed to be able express these needs, @andralex?

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Nordlöw via Digitalmars-d-learn
On Saturday, 17 February 2018 at 12:33:25 UTC, Nordlöw wrote: PureMallocator.instance.allocate(16); currently errors as pure_mallocator.d(84,16): Error: pure function 'pure_mallocator.__unittest_pure_mallocator_82_0' cannot access mutable static data 'instance'

Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread Nordlöw via Digitalmars-d-learn
I'm struggling with making https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d callable in pure functions such as here https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84 Shouldn't a shared static shared PureMallocator instance

Re: Cannot make my shared PureMallocator callable in pure functions

2018-02-17 Thread rikki cattermole via Digitalmars-d-learn
On 17/02/2018 12:33 PM, Nordlöw wrote: I'm struggling with making https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d callable in pure functions such as here https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84 Shouldn't a shared

Re: std.algorithm can not be used inside pure functions?

2017-05-06 Thread Szabo Bogdan via Digitalmars-d-learn
On Saturday, 6 May 2017 at 15:01:16 UTC, Adam D. Ruppe wrote: On Saturday, 6 May 2017 at 14:14:41 UTC, Szabo Bogdan wrote: oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this? Don't use pure? I don't think any of the SysTime conversion methods are pure since al

Re: std.algorithm can not be used inside pure functions?

2017-05-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, May 6, 2017 2:14:41 PM CEST Szabo Bogdan via Digitalmars-d- learn wrote: > On Saturday, 6 May 2017 at 13:21:10 UTC, Adam D. Ruppe wrote: > > On Saturday, 6 May 2017 at 13:19:17 UTC, Szabo Bogdan wrote: > >> a.begin.toISOExtString, > > > > I believe that function is not marked pure if i

Re: std.algorithm can not be used inside pure functions?

2017-05-06 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 6 May 2017 at 14:14:41 UTC, Szabo Bogdan wrote: oh yes, I get it... begin and end are `SysTime`.. there is any workaround for this? Don't use pure? I don't think any of the SysTime conversion methods are pure since all of them call C functions which pull from the time zone... ev

Re: std.algorithm can not be used inside pure functions?

2017-05-06 Thread Szabo Bogdan via Digitalmars-d-learn
On Saturday, 6 May 2017 at 13:21:10 UTC, Adam D. Ruppe wrote: On Saturday, 6 May 2017 at 13:19:17 UTC, Szabo Bogdan wrote: a.begin.toISOExtString, I believe that function is not marked pure if it is a SysTime because it needs to pull global timezone info. What is the type of a.begin? oh y

Re: std.algorithm can not be used inside pure functions?

2017-05-06 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 6 May 2017 at 13:19:17 UTC, Szabo Bogdan wrote: a.begin.toISOExtString, I believe that function is not marked pure if it is a SysTime because it needs to pull global timezone info. What is the type of a.begin?

std.algorithm can not be used inside pure functions?

2017-05-06 Thread Szabo Bogdan via Digitalmars-d-learn
Hi, I'm trying to write a function that saves some structs as csv file: ``` string toCsv(const(StatStorage) storage) { return storage.values .map!(a => [ a.name, a.begin.toISOExtString, a.end.toISOExtString, a.status.to!string ]) .map!(a => a.join(',')) .join('\n'); } ``` I th

Re: pure functions

2016-09-13 Thread Jonathan M Davis via Digitalmars-d-learn
actually something different. It would be far more accurate at this point if it were called something like @noglobal. Actual, functional purity really only comes into play when a pure function's parameters are immutable or implicitly convertible to immutable, at which point the compiler can guar

Re: pure functions

2016-09-13 Thread Steven Schveighoffer via Digitalmars-d-learn
scope. D defines pure differently. You are allowed to declare a function is pure if it takes (and possibly changes) mutable references. It can be called by pure functions, but will not be optimized in the same way one would expect traditional pure functions to be optimized. We call it "we

Re: pure functions

2016-09-13 Thread Patrick Schluter via Digitalmars-d-learn
On Tuesday, 13 September 2016 at 06:59:10 UTC, Jonathan M Davis wrote: On Tuesday, September 13, 2016 03:33:04 Ivy Encarnacion via Digitalmars-d- learn wrote: A pure function cannot call any function that is not pure [...] I've read that a lot but it's not true. A pure function can call imp

Re: pure functions

2016-09-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, September 13, 2016 03:33:04 Ivy Encarnacion via Digitalmars-d- learn wrote: > Can pure functions throw exceptions on its arguments? Also, how > can it perform impure operations? Yes, as long as the exception's constructor is pure, a pure function can throw an excepti

Re: pure functions

2016-09-12 Thread sarn via Digitalmars-d-learn
On Tuesday, 13 September 2016 at 03:33:04 UTC, Ivy Encarnacion wrote: Can pure functions throw exceptions on its arguments? You can throw exceptions for whatever reasons from a function marked pure: void foo() pure { throw new Exception("nope"); } void main() {

pure functions

2016-09-12 Thread Ivy Encarnacion via Digitalmars-d-learn
Can pure functions throw exceptions on its arguments? Also, how can it perform impure operations?

Re: Question about pure functions

2013-09-16 Thread Jonathan M Davis
or make it so that your class can only be constructed as immutable. pure member functions are useful, because they guarantee that the function is not accessing global mutable state, and because it makes it so that they can be called from strongly pure functions, but in general, pure member fu

Re: Question about pure functions

2013-09-16 Thread Bienlein
Mark all parameters const to get a strong pure function. For "this" const goes on the method: class Foo { int i = 0; void bar() const pure { // can't mutate i here } } See also: http://dlang.org/function.html#pure-functions I see,

Re: Question about pure functions

2013-09-16 Thread anonymous
. Regards, Bienlein (Weak) pure functions are allowed to mutate their arguments. Methods take the object via a hidden parameter, so that's an argument, too. Mark all parameters const to get a strong pure function. For "this" const goes on the method: class Foo {

Question about pure functions

2013-09-16 Thread Bienlein
Hello, ich habe a pure method bar() in a class Foo: class Foo { int i = 0; void bar() pure { i++; } } main() { auto foo = new Foo(); foo.bar() } The pure method bar changes the inst var i. Nevertheless, the code above c

Re: pure functions calling impure functions at compile-time

2012-05-24 Thread Don Clugston
On 23/05/12 11:41, bearophile wrote: Simen Kjaeraas: Should this be filed as a bug, or is the plan that only pure functions be ctfe-able? (or has someone already filed it, perhaps) It's already in Bugzilla, see issue 7994 and 6169. It's just happening because the purity c

Re: pure functions calling impure functions at compile-time

2012-05-23 Thread bearophile
Simen Kjaeraas: Should this be filed as a bug, or is the plan that only pure functions be ctfe-able? (or has someone already filed it, perhaps) It's already in Bugzilla, see issue 7994 and 6169. But I think there is a semantic hole in some of the discussions about this problem. Is a f

Re: pure functions/methods

2012-04-20 Thread bearophile
Namespace: So only GDC optimized "pure" functions at all? I've seen DMD performs some optimizations with "strongly pure" functions that return integral values. If you have code like: int sqr(in int x) pure nothrow { return x * x; } int y = ... auto r = sqr(

Re: pure functions/methods

2012-04-20 Thread Namespace
On Friday, 20 April 2012 at 09:55:28 UTC, Timon Gehr wrote: On 04/20/2012 10:06 AM, Namespace wrote: The sense of pure functions isn't clear to me. What is the advantage of pure functions / methods? 1. It enables stateless reasoning about program parts. 2. It enables certain com

Re: pure functions/methods

2012-04-20 Thread Timon Gehr
On 04/20/2012 10:06 AM, Namespace wrote: The sense of pure functions isn't clear to me. What is the advantage of pure functions / methods? 1. It enables stateless reasoning about program parts. 2. It enables certain compiler optimizations. I inform the compiler with "const" t

Re: pure functions/methods

2012-04-20 Thread Sean Cavanaugh
On 4/20/2012 3:06 AM, Namespace wrote: The sense of pure functions isn't clear to me. What is the advantage of pure functions / methods? I inform the compiler with "const" that this method does not change the current object, and therefore he can optimize (at least in C++) this

Re: pure functions/methods

2012-04-20 Thread Ary Manzana
On 4/20/12 4:06 PM, Namespace wrote: The sense of pure functions isn't clear to me. What is the advantage of pure functions / methods? I inform the compiler with "const" that this method does not change the current object, and therefore he can optimize (at least in C++) this metho

pure functions/methods

2012-04-20 Thread Namespace
The sense of pure functions isn't clear to me. What is the advantage of pure functions / methods? I inform the compiler with "const" that this method does not change the current object, and therefore he can optimize (at least in C++) this method. How and what optimized the comp

Re: Pure functions and delegates

2012-01-18 Thread Timon Gehr
On 01/18/2012 04:40 AM, H. S. Teoh wrote: So, I was quite impressed with D's pureness system, and was experimenting a bit with it. Then I discovered that delegates are impure, which seems reasonable since there's no way to know what a delegate might do. But *if* the compiler verifies that a parti

Pure functions and delegates

2012-01-17 Thread H. S. Teoh
So, I was quite impressed with D's pureness system, and was experimenting a bit with it. Then I discovered that delegates are impure, which seems reasonable since there's no way to know what a delegate might do. But *if* the compiler verifies that a particular delegate is (weakly) pure in the conte

Re: Forcing compile time evaluation of pure functions

2011-07-14 Thread bearophile
> In GCC there is a way to (sometimes) do it, see the __builtin_constant_p here: > http://www.delorie.com/gnu/docs/gcc/gcc_81.html Google code search gives about 8,100 answers: http://www.google.com/codesearch#search/&q=%22__builtin_constant_p%22&type=cs Bye, bearophile

Re: Forcing compile time evaluation of pure functions

2011-07-14 Thread bearophile
scarrow: > I'd really like to figure out how to have Hash("foo") be static and > Hash(variable) be dynamic. In GCC there is a way to (sometimes) do it, see the __builtin_constant_p here: http://www.delorie.com/gnu/docs/gcc/gcc_81.html Time ago I have asked for something similar in D too, becaus

Re: Forcing compile time evaluation of pure functions

2011-07-14 Thread scarrow
I think invoking a template to call the pure function (StaticEval!(Hash("foo")) isn't much different from StaticHash!("foo"). You still have to explicitly know whether you're dealing with a compile time constant or not. I'd really like to figure out how to have Hash("foo") be static and Hash(vari

Re: Forcing compile time evaluation of pure functions

2011-06-30 Thread Simen Kjaeraas
ishing between these two cases. If it is a properly pure function there should be no harm in doing a compile time evaluation. Actually, there might. Pure functions are allowed to depend upon the immutable global variables, whose values may be calculated at startup (see static this) -- Simen

Re: Forcing compile time evaluation of pure functions

2011-06-30 Thread bearophile
scarrow: > Annoyingly, however, I can't do the following at compile time: > > f(Hash("foo")); > > I'm not sure what the point is in distinguishing between these two cases. Walter has decided that he doesn't like the D compiler to arbitrary run at compile time arbitrary long to run code. Th

Re: Forcing compile time evaluation of pure functions

2011-06-29 Thread scarrow
Oh, I also wanted to mention that although ideally I'd be able to invoke the compile time and runtime versions identically and have it automatically select, I'm also worried about accidentally invoking the runtime version when I should have been using the compile time version. I can't think of a w

Forcing compile time evaluation of pure functions

2011-06-29 Thread scarrow
Hey all, I'd like to embed hashed strings into my code. The C++ version of this engine ran an external tool to preprocess the files. In D my strongly pure function is only evaluated if I assign it to something like an enum or invoke it from a template. So the following generate compile time has

Re: running pure functions in parallel

2010-08-03 Thread Simen kjaeraas
Justin wrote: I'm designing an application which needs to process a lot of data as quickly as possible. I've found that I can engineer the processing algorithms into pure functions which can operate on different segments of my data. I would like to run these functions in pa

running pure functions in parallel

2010-08-03 Thread Justin
I'm designing an application which needs to process a lot of data as quickly as possible. I've found that I can engineer the processing algorithms into pure functions which can operate on different segments of my data. I would like to run these functions in parallel but: spawn()

Re: Making pure functions get dirty (D 2.0)

2009-02-20 Thread Burton Radons
Oh I see what's going on. pure functions get funky processing; if you don't actually use their return values they're not even compiled. Once you actually take the return value it'll complain about it whether it's a pure inner function in a pure outer function or anyth

Making pure functions get dirty (D 2.0)

2009-02-20 Thread Burton Radons
I'm writing a general conversion template function a la: pure T convert (T, U) (const (U) value); Sweet, and really handy for template errors because you can tell the user which number input it is that angered it. The problem is that if you're converting int to string there's allocations ther

Re: Making pure functions get dirty (D 2.0)

2009-02-20 Thread Burton Radons
Burton Radons Wrote: > void convertInto (T, U, alias write) (const (T) value) This should read "pure void". Everything I said about its behaviour is correct for my experiences.