In std.functional there is a curry(), that looks more like a
partial application.
In the Python land I've recently seen a library for a more
functional style of coding:
https://pypi.python.org/pypi/PyMonad/
I don't like for Python most of the stuff in that PyMonad
library, but two bits look
Matt Soucy:
So, in the next release std.functional.curry has been renamed
to std.functional.partial:
https://github.com/D-Programming-Language/phobos/pull/1979
Oh, good. (And the sigh by Andrei is cute).
I was starting to work on a proper curry replacement,
Is its syntax usage similar
Walter Bright:
Instead of adding more language features, purpose existing ones:
assert(exp = min exp = max);
I don't see how this can solve the problem I have discussed.
Please explain better.
Bye,
bearophile
Matt Soucy:
Is it a good idea to also mix in the function composition
operator overloading?
I'm not so sure about that one - mainly because then it's
possible with some functions (the curried ones) but not all
(including regular and delegates).
If you compose (multiply) a curried with a
Here D detectes a mismatch in the array lengths of a slice copy
at compile-time (dmd 2.066beta5):
void main() {
import std.algorithm: copy;
int[100] a;
int[8] b;
const int i = 20;
b[] = a[i .. i + 9]; // Detected at compile-time
copy(a[i .. i + 9], b[]); //
Walter Bright:
Because it breaks the least amount of code.
This is not a good idea. What about the future code that will be
buggy/wrong?
Bye,
bearophile
Walter Bright:
We need to stop breaking code.
We also have a responsibility for the bad D code yet to be
written :-)
Bye,
bearophile
BlackEdder:
Is this possible at all?
I think it's possible, as long as your associative array contents
are known at compile-time. You can write a function that given
the associative array returns a string that mixed-in defines the
struct. And then you can define a toStruct template.
Bye,
Márcio Martins:
float[10] a;
a[] = uniform(0.0f, 1.0f);
This is going to set all values to the result of a single call
to uniform();
Is there a way to express my intent that I want one result per
array element?
Currently D array operations can't be used for that kind of
usage. So you
francesco cattoglio:
should this code compile? I understand that the literal 1 is
int therefore it can screw type deduction, but I wonder if
the compiler should be smart enough to deduce it correctly.
To keep both the compiler and programmers sane, D templates don't
perform implicit type
Adam D. Ruppe:
Check for null on a key before trying to get a value out.
Is something like the get() function usable here?
Bye,
bearophile
Pavel:
writeln(cast(bool)(fail in parsed));
Produces false... but why on earth boolean expression would
output null?
In D if you perform an associative array in, the return isn't a
boolean but a pointer. It's zero if the item is not present. And
it's a valid pointer to the value if the
Dmitry Olshansky:
(unlike compute-goto which is very niche and dangerous).
It's a niche that system languages are the few fit for. Even
CPython (and other interpreters) are using that feature.
Bye,
bearophile
Don:
I think that approach is more convincing for functional
languages than for D, especially if you are limited to a single
return type.
Hopefully someday we'll have good enough tuples in D (including
their destructuring), this is similar to having multiple return
values.
Bye,
Daniel Murphy:
Giving an error and forcing you to be explicit about what
exactly you wanted is the best that's possible.
OK. This is far better than silent bugs :-)
Bye,
bearophile
Timon Gehr:
It is easy to understand and
It's easy to understand because you are very intelligent Timon :-)
Bye,
bearophile
Walter Bright:
Would you rather write that or:
How much experience do you have in writing Haskell programs
longer than 50 lines of code? I have a large respect for your
experience and your intelligence, but be careful with comments
like that, to not show just your ignorance...
If you are
Timon Gehr:
What do you think would be the corresponding idiomatic/fast D
code? Does it outperform the Haskell code?
Look in RosettaCode for various implementations in D:
http://rosettacode.org/wiki/Hamming_numbers#D
A good part of the performance difference is caused by the D GC
and the
Eric:
while (!buf.empty())
{
p++;
buf.popFront();
Those () can be omitted, if you mind the noise (but you can also
keep them).
if (buf.front() = '0' || buf.front() = '9') break;
std.ascii.isDigit helps.
curTok.image = cast(string) cbuffer[0 .. (p -
Daniel Gibson:
For normal functions http://dlang.org/interfaceToC.html tells
me to add a ref in the function signature, to tell D to pass
it by reference (couldn't this be implicit for extern (C)
functions?)
I don't know why D isn't adapting such things to the needs of C.
Bye,
bearophile
Walter Bright:
https://issues.dlang.org/show_bug.cgi?id=13169
https://issues.dlang.org/show_bug.cgi?id=13170
Are such optimizations portable and guaranteed on all D
compilers? If the answer is negative, then they can't replace a
_standard_ D syntax for computed gotos.
Bye,
bearophile
Jacob Carlborg:
Error: AA key type TagIndex now requires equality rather than
comparison
Hashing protocol has being finally fixed.
What exactly do I need to define to have a struct with two ints
working as a key in an associative array?
opEquals and opHash. But if your struct has only
Daniel Gibson:
That would be too much magic = special cases one has to know
about.
What special cases? It works for POD structs. If you want a
different hashing behavior (example: you want to ignore some
fields), you define the two methods.
struct Foo {
double x;
int y;
Ary Borenszweig:
Wat?
Hash methods should be automatic for simple structs.
Bye,
bearophile
Basile Burg:
If you still feel ok today then dont read this:
-
module meh;
import std.stdio;
//https://stackoverflow.com/questions/24676375/why-does-int-i-1024-1024-1024-1024-compile-without-error
static shared immutable int o = 1024 * 1024 * 1024 * 1024;
void main(string
Jacob Carlborg:
If the struct already has opCmp I need to defined opEquals and
toHash?
From what I know, the new hash protocol requires toHash and
opEquals. I think that now opCmp is not used by hashing. I don't
know if you define just toHash and opCmp it uses opCmp as
fallback.
Bye,
Tobias Müller:
Wouldn't it be more useful to have a modified/annotated return
statement for that?
I don't know.
Tail-recursiveness is an implementation detail, for the user of
the function it's not really interesting.
Yes, in theory a @tailrec annotation doesn't change the mangling
of
Andrei Alexandrescu:
And that doesn't look awkward at all :o). -- Andrei
A related thread:
http://stackoverflow.com/questions/6622524/why-is-haskell-sometimes-referred-to-as-best-imperative-language
Bye,
bearophile
sigod:
, but I see few mistakes in your code:
What's unfortunate is the silence of the compiler about that
programmer mistake :-)
Bye,
bearophile
enum Vala(uint count, alias arr) = format(
asm {
sub ESP, %d; // Reserve 'count' bytes
mov %s, %d; // Set a.length = 'count'
mov %s + 4, ESP; // Set a[0] to reserved bytes
}, count, arr.stringof, count, arr.stringof);
I'd like to write that more like
Foo:
Hey guys. Can someone explain me, why this code does only works
with the inline assembler version but not with the mixin?
You need mixin(), it's unfortunate this gives no error messages:
import std.string: format;
enum Vala(uint count, alias arr) = format(
asm {
sub ESP,
Foo:
It seems that your code doesn't work with 2.065.
Right. For 2.065 you have to replace the enum expression with a
template.
If you can I suggest to update your compiler to the latest beta.
There are tons of bugfixes and changes.
Bye,
bearophile
mixin template Vala2(uint count, alias arr) {
What about disallowing mixin templatename unless you add
mixin before the template keyword?
Bye,
bearophile
Foo:
I do not understand?
Sorry, mine was a language design question (to catch your bug).
Bye,
bearophile
Uranuz:
ubyte a = 15;
ubyte b = 10;
ubyte c = a + b; //What is happening there?! ARGH! Are you
joking?!
In C/C++/D if you sum a types that are smaller than int, you
obtain an int. D has copied C for backwards compatibility with C
code.
Bye,
bearophile
Matthieu:
Can someone explain to me how to do it well, please?
Put related stuff in a module, and unrelated stuff in other
modules. If the module grows too much, split it up in two or
more. It's about the same as in Python. Just remember that
classes can see each other private members only
Matthieu:
Thanks for your answers. I'll continue to code as I ussed to do,
and try to improve my style to have a better usage of the
modules
(today i'm most one class per file, so I don't use the modules
at all).
Also, try to use less classes and more free (pure) functions :-)
This means a
Dicebot:
Probably most idiomatic D way is to use files _instead_ of
classes :)
It is a bit idealistic though and is not yet 100% feasible in
practice.
What's stopping it from being feasible?
Bye,
bearophile
H. S. Teoh:
It's already in std.typecons.
But it is not online yet?
Bye,
bearophile
Andrei Alexandrescu:
Just read the slides, very interesting.
There are many papers, books and articles around that explain the
same things, but that explanation is easy to understand even for
people not used to functional programming (as I still partially
am).
I think it would be
Andrew Godfrey:
1) A function annotation that means I will call myself
recursively, and when I do, I expect the tail recursion
optimization. I have seen code which allocates something big on
the stack and depends on the optimization. So this intent should
be expressible.
A @tailrec annotation
Andrew Godfrey:
1) A function annotation that means I will call myself
recursively, and when I do, I expect the tail recursion
optimization. I have seen code which allocates something big
on
the stack and depends on the optimization. So this intent
should
be expressible.
A @tailrec
Walter Bright:
If you want to guarantee replacement of a recursive call with a
loop, just write a loop.
There are cases where a recursive algorithm is nicer. And people
that want to use D functionally, may also be used to writing code
recursively.
What about the @continuation
Tove:
IIRC:
Walter's stance was that he needs compelling examples, which
proves the utility of this new feature.
Recently I have had a desire for that feature, to write a
function like this:
int[][] generateTable(in uint nx, in uint ny=nx) {...}
If you give just one argument to this
Walter Bright:
I doubt they'll want to use an @tailrec attribute.
In Scala there is @tailrec:
http://www.scala-lang.org/api/current/scala/annotation/tailrec.html
In both F# and OcaML there is the rec keyword:
http://msdn.microsoft.com/en-us/library/dd233232.aspx
1. So I want to know if it is expected behaviour?
Yes, this was the chosen behaviour.
2. What are the reasons for it?
It's one of the two main behaviours you may want, even if your
specific case it is not. It can be used to unserialize enums from
a text file. This is the principle behind
Uranuz:
This is why I disagree with current implementation,
The best that you can hope is having another different little
function in Phobos that does what you ask for.
Bye,
bearophile
Uranuz:
So as I think there are 2 options.
There is a third option, that is to add a little extra function
that does what you want.
First is keep it al *as is*. In this case I propose to improve
documentation about it that in case of conversion of enums into
string types and vice versa
Marc Schütz:
import std.stdio, std.zip, std.file, std.mmfile;
int main()
{
auto mmfile = new MmFile(File(c:/test.zip, rb));
auto zip = new ZipArchive(mmfile[]);
foreach (item; zip.directory) {
writeln(processing , item.name, ...);
// processing item...
}
return 0;
}
This
Dominikus Dittes Scherkl:
The very little rest is things like exceptions, delegates and
closures - because I have no idea how to use them with manual
memory management. So unfortunately I have to avoid them in RT
code.
Some closures can be avoided with scope, and some exception
allocations
Archibald:
Any rational explaination to this? ( Seems unrelated to garbage
collection )
Not nearly enough info to answer. It looks fishy.
Bye,
bearophile
Walter Bright:
Unless a convincing counter argument emerges, yes.
Please Walter, list your convincing arguments to not fix the
situation.
Bye,
bearophile
Walter Bright:
Breaks existing, working code for little gain. I suggested a
fix that deals with the issue and does not break existing code.
This is not yet convincing. Let's talk about the Pokemon
Exception Handling, for when you just Gotta Catch 'Em All :-) I
think the D/Python code that
Artur Skawina:
For GDC, it's the '-fwrapv' switch, but it's not enabled by
default for D.
I think it has to be enabled by default, plus you can add a
switch to disable that standard D semantics.
Bye,
bearophile
Uranuz:
Actualy I found this bug report, but I don't know if there
suggested something around associative arrays
https://issues.dlang.org/show_bug.cgi?id=5498
I think it's a bug. Issue 5498 it's better left closed. Search if
another more specific bug is open on this, or open a new one
Tero:
Allocating in the stack seems ideal so I'd encourage that by a
clean syntax. I keep missing
this feature.
See:
https://issues.dlang.org/show_bug.cgi?id=9832
http://en.cppreference.com/w/cpp/container/dynarray
Bye,
bearophile
Kapps:
In theory there would probably be an allocator that uses alloca
when Andrei's std.allocator makes it in.
How is it going to work?
Also, there used to be a built in syntax, 'scope foo = new
Foo()' that would allocate on the stack, but that's deprecated
now
Perhaps once the scope
Reduced:
interface IBase {}
class Impl(T): IBase {
T value;
}
void main() {
IBase a = true ? (new Impl!uint) : (new Impl!string);
}
test.d(6,23): Error: cannot implicitly convert expression (new
Impl!uint) of type object.Object to test.IBase
Bye,
bearophile
interface IBase {}
class Impl(T): IBase {
T value;
}
void main() {
IBase a = true ? (new Impl!uint) : (new Impl!string);
}
I have added a note:
https://issues.dlang.org/show_bug.cgi?id=3543
Bye,
bearophile
Uranuz:
OK. Thanks. Using casts everywhere for such case us quite
annoying. I've tested it for plain array it doesn't work too.
However issue is marked as solved for it.
It's not exactly the same issue. Issue 3543 seems more fitting.
In D interfaces and classes are not the same thing.
Vic:
If D came without GC, it would have replaced C++ a long time
ago!
Agree +1000.
I see no proof of this. And not everybody hates GCs.
Bye,
bearophile
H. S. Teoh:
I don't think it will affect existing code (esp. given Walter's
stance on breaking changes!).
Making various parts of Phobos GC-free doesn't mean that nothing
GC-allocates, it means that Phobos will offer means to use memory
provided by the user. There are many situations where
Walter Bright:
We need to STOP breaking existing code.
This is a small example case where I suggest to break
hypothetical existing code:
https://github.com/D-Programming-Language/phobos/pull/2077#issuecomment-49360265
The fear of breaking code should NOT freeze our brains in terror.
For
Dicebot:
Your proposal there is much much worse than one to deprecate
`catch()` - it may result in silent change of behavior
Yes, sorry.
Bye,
bearophile
Walter Bright:
BTW, it's not perfect because of token string literals and
string mixins, but it's good enough.
In general the creation of a tool like Go fix
(http://golang.org/cmd/fix/ ) for D could change a little the
trade-offs regarding the tiny Phobos/D breaking changes.
Bye,
Dean:
I need a little helping hand with dmd on a 32 bit Debian box.
I
installed dmd from http://d-apt.sourceforge.net/
i) First trial:
$cat test.d
import std.stdio;
void main() {
writeln(hello);
}
$ time dmd test.d
real0m2.355s
user0m1.652s
sys
Dean:
Mine is an Athlon 1045.456 MHz. The minimum of 3 consecutive
compilation runs that I get is 2.3 seconds.
Then I think your timings could be OK, I am using an old 2.3 GHz
CPU.
On my box this compiles in 1.2 seconds. So it seems somewhat
consistent (as in 3 times slower for both).
Dean:
dmd compiles very quickly, but to compile writeln D has to
digest a good amount of Phobos code.
Are the reasons for this similar to why C++ STL is not an object
code library ?
The reasons for the large amount of code compiled for a writeln
are that: writeln is more powerful, Phobos
ddos:
auto twice = function (int x) = x * 2;
function is not necessary. And generally it's better to assign
to immutables, unless you need to mutate the variable twice
later.
how can i apply the function to each element in a without using
a forloop? - is there a function to do this?
ddos:
how do i add the elements in a and b elementwise
Several ways to do it:
void main() {
import std.range, std.algorithm, std.array;
immutable a = [1, 2, 3, 4];
immutable b = [2, 3, 4, 5];
int[] c1;
c1.reserve(a.length); // Optional.
foreach (immutable x,
Justin Whear:
By this do mean replacing the template constraint `if
(isInputRange!R)`
syntax? If so, we need concept definition syntax, but we do not
necessarily need a struct realizes concept syntax. And, in
fact, I
would argue against it as a static assert would continue to be
Andrei Alexandrescu:
http://www.reddit.com/r/programming/comments/2aruaf/dconf_2014_keynote_high_performance_code_using_d/
Despite Walter is used to pipeline programming, so the next
step is to also handle failures and off-band messages in a
functional way (without exceptions and global
Despite Walter is
Sorry, I meant to write, Now Walter is...
Bye,
bearophile
Kagamin:
I'd recommend @fantom name, because not only type wrappers can
benefit from these optimizations. Should be also applicable to
functions and maybe modules.
This sounds like something fit for D too, not just gdc. You can
use it for std.typecons.Typedef too.
Bye,
bearophile
Walter Bright:
struct VolatilePointerToUint {
private:
size_t ptr;
public:
this(size_t ptr) { this.ptr = ptr; }
uint read() { return peek(cast(uint*)ptr); }
void write(uint value) { poke(cast(uint*)ptr, value); }
}
You'd probably wish to flesh this out a bit more, but it's
Sean Campbell:
i have the following code
char[] Etag(string file){
auto info = DirEntry(file);
ubyte[16] hash = md5Of(to!string(info.timeLastAccessed.day)~
to!string(info.timeLastAccessed.month)~
Kagamin:
Seems like a popular issue.
Y
es, it is.
Is there a bug report for it?
Bug report for what? To ask for [] to be used when you slice a
fixed size array? This is in Bugzilla. Or perhaps you are asking
for lifetime management of the data? This is currently discussed
in the main D
Kagamin:
Report for the problem when a temporary fixed-size array is
assigned to a slice, which is escaped.
I think this is already in Bugzilla. But the point is: you can't
solve this problem locally and with small means. You need a
principled solution (or no solution at all, as now) of
Klb:
Hello, I'd like to know if it's possible, using CTFE, mixin etc
to convert a POD struct to a class at compile time.
I remember Andrei planned to add this small thingie to Phobos,
but it's still missing.
Bye,
bearophile
Ary Borenszweig:
When assigning a fixed size array to a slice, can't you just
allocate memory and copy the data there (I mean, let the
compiler do that in that case)? That would be safe, right?
Yes, using a .dup:
char[] hashstring = toHexString(hash).dup;
But the compiler should not do
The talk was nice, and it's the chance I was waiting to ask a
question to the speaker.
I've read a very nice paper (+ slides) about using some
specialized but simple type system rules to make less bug-prone
the bit-twiddling kind of code, Bit-Level Types for High-Level
Reasoning by Ranjit
So are those things a good addition to Phobos for your kind of
programming? (additions to the language can be discussed later).
You can look at the slides for a quicker overview, or you can ask
me here for a summary, if necessary.
Bye,
bearophile
Meta:
Spot the bug:
We can start killing those commas in D 2.067 :-)
Bye,
bearophile
Jane Doe:
So, you wanna nerf everything that could produce the wrong
behavior?
A smarter question is: How to reduce the number of bugs in D code
decreasing the language functionality only very little, and
keeping the language handy (or making it even more handy)?
There is nothing in this
Artur Skawina:
You can already express
all the described volatile semantics in GDC's D dialect, in a
completely portable way and without using a single asm
instruction.
What's GDC syntax?
Bye,
bearophile
Martin Krejcirik:
I find the comma op useful somemtimes.
Please shows us all the cases you can think of (or coming from
your own code, or coming from the Web) where you think it's
useful.
This example shows absolutely nothing of comma wrongdoing.
See my precedent answer.
If
Martin Krejcirik:
Also an assignment is not allowed in a condition and without
the comma operator, it wouldn't be possible at all. That's way
too restrictive.
If I find code like yours, I refactor it ASAP :-)
Bye,
bearophile
Johannes Pfau:
Well as described in the DIP it works just like shared from an
implementation point of view, so I doesn't add much complexity
in the compiler / type system.
I am reading blogs about compiler bugs, and I see that the
implementation of volatile is the buggiest part of GCC/Clang
Johannes Pfau:
//In runtime:
enum int* SOME_REG = 0x;
//In user code
peek(SOME_REG);
poke(SOME_REG);
*SOME_REG = 1; //Oops, forgot poke! This is not acceptable
Perhaps this syntax:
volatile enum int* SOME_REG = 0x;
Could turn this in a syntax error (only peek/poke are allowed to
Iain Buclaw:
The use of volatile can be buggy in C because there is no other
safe way to do inter-thread communication.
C11 offers means much better than volatile for multi thread
programming:
http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29#Changes_from_C99
So your saying that
Johannes Pfau:
And how do you think peek/poke are easier to implement in this
regard?
Let's ask Walter.
Well as long as those priorities are clearly communicated.
Most of the design of D is not geared toward embedded computing.
It could be adapted and improved for those purposes, but
pgtkda:
How can i read the whole file if i use this:
File(C:\\Users\\text\\Desktop\\test.csv, r);
In std.file there are two functions to read a file or read a text
file, named read and readText.
Bye,
bearophile
Alexandre:
return (retVal 0x00FF) 24 |
(retVal 0xFF00) 8 |
(retVal 0x00FF) 8 |
(retVal 0xFF00) 24;
See also core.bitop.bswap.
Bye,
bearophile
Alexandre:
Thanks, but, when I convert I recive a 'c' in the front of my
number...
This shows it inverts all bits, not just the four byte positions.
I don't understand:
import core.bitop: bitswap;
uint reverseBytes(in uint val) pure nothrow @safe @nogc {
return val.bitswap;
}
void
Alexandre:
RefCounted!(DWORD) addr;
I think RefCounted is for advanced usages in D :-)
template Wrap(T)
{
struct Wrap
{
T val;
this(T val){val = val;}
}
}
Simpler:
struct Wrap(T) {
T val;
this(T val_) { this.val =
Alexandre:
as rc is better for managing scarce resources like file handles.
File instances are ref counted in D. Is this useful for you?
Bye,
bearophile
Alexandre:
mapstring, Address syms;
If you don't need the key ordering then use a built-in
associative array:
Address[string] syms;
Otherwise use a RedBlackTree from std.container.
vectorpairDWORD, Address values;
vectorpairDWORD, shared_ptrDWORD addrs;
Tuple!(DWORD, Address)[]
Nordlöw:
Could someone elaborate shortly which cases this means?
All cases where you really can't live without it :-) It's like a
cast(.
Bye,
bearophile
Klb:
...and any example where this switch will be usefull ?
I guess it was added to D in the spirit of If you have to ask
what it is, then you don't need to use it.
More seriously, I too would like more documentation about its use
cases.
Bye,
bearophile
Vic:
- Confusing forum. First listed forum on 'D' is not for D
users, but it's called D! It is in fact for D commiters. This
causes frustration for both users and commiters. (yes there is
'learn D' but it's 4th down. Commiters forum should be slightly
hidden and user questions should not be
801 - 900 of 1477 matches
Mail list logo