Jump to page: 1 2 3
Thread overview
Python's features, which requires D
May 22, 2015
Dennis Ritchie
May 22, 2015
weaselcat
May 22, 2015
Dennis Ritchie
May 22, 2015
weaselcat
May 22, 2015
Dennis Ritchie
May 22, 2015
Ali Çehreli
May 23, 2015
Dennis Ritchie
May 23, 2015
Dennis Ritchie
May 23, 2015
Kagamin
May 23, 2015
Dennis Ritchie
May 23, 2015
Russel Winder
Jun 11, 2015
Dennis Ritchie
May 23, 2015
Alex Parrill
May 23, 2015
Dennis Ritchie
May 23, 2015
cym13
May 23, 2015
cym13
May 23, 2015
Dennis Ritchie
May 24, 2015
Idan Arye
May 24, 2015
Dennis Ritchie
May 24, 2015
Idan Arye
May 24, 2015
Dennis Ritchie
May 24, 2015
Idan Arye
May 24, 2015
Dennis Ritchie
May 23, 2015
anonymous
May 23, 2015
Dennis Ritchie
May 23, 2015
anonymous
May 23, 2015
Dennis Ritchie
May 23, 2015
Dennis Ritchie
May 23, 2015
weaselcat
May 23, 2015
Dennis Ritchie
May 22, 2015
Hi,
I've collected some of Python's features. It seems to me that they are not in the D!

Surely all this is in the D? :)
http://rextester.com/CNQQR3333
May 22, 2015
On Friday, 22 May 2015 at 00:23:30 UTC, Dennis Ritchie wrote:
> Hi,
> I've collected some of Python's features. It seems to me that they are not in the D!
>
> Surely all this is in the D? :)
> http://rextester.com/CNQQR3333

D doesn't have list comprehensions, so it's difficult to directly port these.
off the top of my head, the last one can easily be done with std.range.stride

>P.S. I think that all of this is written in D is much more lines of code!
and they can be done in less with APL and J.
May 22, 2015
On Friday, 22 May 2015 at 01:17:17 UTC, weaselcat wrote:
> D doesn't have list comprehensions, so it's difficult to directly port these.

I can not imagine how difficult it is to implement it in D, but I'm pretty sure that nested for loops to fill arrays (in D, you can call them differently, for example, force :)) will be very useful thing, because Python is a veryIt is often used.
Besides, I do not understand what could be the problem with nested loops in arrays, because std.algorithm.map works on the principle of nested loops. I think that the biggest problem in the implementation of this should not be. Excuse me if I'm wrong.

> off the top of my head, the last one can easily be done with std.range.stride

import std.stdio, std.range;

void main()
{
    int[] a = [ 1, 2, 3, 4, 5, 6 ];

    writeln(stride(a, 2)); // [1, 3, 5] #odd #print(x[::2]) #OK
    // [2, 4, 6] #even #print(x[1::2]) #no equivalent in D

    auto x = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
    // [2, 6, 10] #print(x[1::4]) #no equivalent in D
}
May 22, 2015
On Friday, 22 May 2015 at 01:52:30 UTC, Dennis Ritchie wrote:
> On Friday, 22 May 2015 at 01:17:17 UTC, weaselcat wrote:
>> D doesn't have list comprehensions, so it's difficult to directly port these.
>
> I can not imagine how difficult it is to implement it in D, but I'm pretty sure that nested for loops to fill arrays (in D, you can call them differently, for example, force :)) will be very useful thing, because Python is a veryIt is often used.
> Besides, I do not understand what could be the problem with nested loops in arrays, because std.algorithm.map works on the principle of nested loops. I think that the biggest problem in the implementation of this should not be. Excuse me if I'm wrong.
>
>> off the top of my head, the last one can easily be done with std.range.stride
>
> import std.stdio, std.range;
>
> void main()
> {
>     int[] a = [ 1, 2, 3, 4, 5, 6 ];
>
>     writeln(stride(a, 2)); // [1, 3, 5] #odd #print(x[::2]) #OK
>     // [2, 4, 6] #even #print(x[1::2]) #no equivalent in D
writeln(stride(a[1..$], 2));
>
>     auto x = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
>     // [2, 6, 10] #print(x[1::4]) #no equivalent in D
writeln(stride(a[1..$], 4));
> }

May 22, 2015
On Friday, 22 May 2015 at 02:18:23 UTC, weaselcat wrote:
> On Friday, 22 May 2015 at 01:52:30 UTC, Dennis Ritchie wrote:
>>> off the top of my head, the last one can easily be done with std.range.stride
>>
>> import std.stdio, std.range;
>>
>> void main()
>> {
>>    int[] a = [ 1, 2, 3, 4, 5, 6 ];
>>
>>    writeln(stride(a, 2)); // [1, 3, 5] #odd #print(x[::2]) #OK
>>    // [2, 4, 6] #even #print(x[1::2]) #no equivalent in D
> writeln(stride(a[1..$], 2));
>>
>>    auto x = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
>>    // [2, 6, 10] #print(x[1::4]) #no equivalent in D
> writeln(stride(a[1..$], 4));
>> }

Yes, this is what you need (I often forget that the functions can take ranges in D).

Maybe somewhere and nested loops "for" to fill the arrays lying around :)
May 22, 2015
On 05/21/2015 05:23 PM, Dennis Ritchie wrote:
> Hi,
> I've collected some of Python's features. It seems to me that they are
> not in the D!
>
> Surely all this is in the D? :)
> http://rextester.com/CNQQR3333

Here is my attempt:

import std.stdio;
import std.algorithm;
import std.conv;
import std.range;

void main()
{
    // Replace 'none' with 'all' to activate.
    version (none) {
        const n = 5;

        auto a = stdin
                 .byLine
                 .map!(l => l.splitter.map!(to!int).array)
                 .take(n);

        writeln(a);
        writeln("-----");
    }

    {
        const n = 6;

        auto a = iota(n)
                 .map!(i => chain([2].replicate(i),
                                  [1],
                                  [0].replicate(n - i - 1)));

        writefln("%(%(%s %)\n%)", a);
        writeln("-----");
    }

    {
        const x = [ 1, 2, 3, 4, 5, 6 ];

        writeln(x.stride(2));
        writeln(x.dropOne.stride(2));
        writeln("-----");
    }

    {
        // The internet does not need another fizz buzz. :p
    }
}

Ali

May 23, 2015
On Friday, 22 May 2015 at 05:31:38 UTC, Ali Çehreli wrote:
> Here is my attempt:
>
> import std.stdio;
> import std.algorithm;
> import std.conv;
> import std.range;
>
> void main()
> {
>     // Replace 'none' with 'all' to activate.
>     version (none) {
>         const n = 5;
>
>         auto a = stdin
>                  .byLine
>                  .map!(l => l.splitter.map!(to!int).array)
>                  .take(n);
>
>         writeln(a);
>         writeln("-----");
>     }
>
>     {
>         const n = 6;
>
>         auto a = iota(n)
>                  .map!(i => chain([2].replicate(i),
>                                   [1],
>                                   [0].replicate(n - i - 1)));
>
>         writefln("%(%(%s %)\n%)", a);
>         writeln("-----");
>     }
>
>     {
>         const x = [ 1, 2, 3, 4, 5, 6 ];
>
>         writeln(x.stride(2));
>         writeln(x.dropOne.stride(2));
>         writeln("-----");
>     }
>
>     {
>         // The internet does not need another fizz buzz. :p
>     }
> }
>
> Ali

Yes, it looks pretty good :) Thanks. But...

It seems to me that D lacks features that allow you to write function readln/readln.strip/readln.split just inside the lambda. stdin.byLine is a good feature, but it captures the entire input stream, which I should handle all or take lambdas function take(n) n lines and then still handle all of these lines right away. Ie stdin.byline used everywhere is not always convenient!

For example, the code in Python looks quite natural:

a = [[int(j) for j in input().split()] for i in range(n)]

About D-code, I can not say:

>auto a = stdin
>          .byLine
>          .map!(l => l.splitter.map!(to!int).array)
>          .take(n);

I can call the map with the existing array:

import std.stdio, std.algorithm, std.conv, std.array;

void main()
{
    auto a = [1, 2, 3];

    auto b = a.map!(c => c ~ readln.split.map!(to!int).array).array;
	
    writeln(b);
}
-----
http://rextester.com/MBUMHI13858

But I can not call the readln n times without a map:

import std.stdio, std.conv, std.algorithm, std.array, std.range;

void main() {

	auto a = [1, 2, 3];

	auto b = [readln.split.map!(to!int).array].take(3);
	
	writeln(b);
}
-----
http://rextester.com/KCJ9346

Ie readln function cycle is needed for, another lambda or revised map! Is there a function in Phobos?
May 23, 2015
By the way, Python has deepDup :)

http://rextester.com/KBFA82886
May 23, 2015
On Saturday, 23 May 2015 at 02:36:14 UTC, Dennis Ritchie wrote:
> For example, the code in Python looks quite natural:
>
> a = [[int(j) for j in input().split()] for i in range(n)]
>
> About D-code, I can not say:
>
>>auto a = stdin
>>         .byLine
>>         .map!(l => l.splitter.map!(to!int).array)
>>         .take(n);

Well, list comprehension is built into language in python (and not in D), such level of support is definitely more streamlined.
May 23, 2015
On Saturday, 23 May 2015 at 10:58:33 UTC, Kagamin wrote:
> Well, list comprehension is built into language in python (and not in D), such level of support is definitely more streamlined.

Well, what's to keep D more functions to work with slist and dlist ?
In my opinion, lists in D completely bald :)

After all, there is a module in Phobos std.array, so why not make the module std.list.
Do lists in D can be done as powerful as arrays?
« First   ‹ Prev
1 2 3