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
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
>
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
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
On Thursday, 12 October 2023 at 21:20:44 UTC, Jonathan M Davis
wrote:
Thanks Jonathan
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
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
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
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
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
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
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()
{
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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.
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
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
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?
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'
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
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
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
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
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
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
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?
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
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
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
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
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
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()
{
Can pure functions throw exceptions on its arguments? Also, how
can it perform impure operations?
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
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,
.
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 {
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
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
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
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(
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
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
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
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
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
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
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
> 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
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
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
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
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
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
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
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
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()
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
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
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.
71 matches
Mail list logo