Jump to page: 1 2
Thread overview
Key and value with ranges
Oct 02, 2023
Joel
Oct 02, 2023
Imperatorn
Oct 02, 2023
Imperatorn
Oct 02, 2023
Joel
Oct 03, 2023
Salih Dincer
Oct 02, 2023
christian.koestlin
Oct 03, 2023
Andrey Zherikov
Oct 03, 2023
christian.koestlin
Oct 03, 2023
Andrey Zherikov
Oct 03, 2023
christian.koestlin
Oct 02, 2023
Joel
Oct 03, 2023
Joel
October 02, 2023
import std;

auto data=“I went for a walk, and fell down a hole.”;

void main(string[] args) {
    int[string] dic;
    struct WordCnt {
        string word;
        ulong count;
        string toString() const {
            return text("Word: ", word, " - number of instances: ", count);
        }
    }
    WordCnt[] wc;
    data
        .map!(c => lowercase.canFind(std.uni.toLower(c)) ? c : ' ')
        .to!string
        .splitter
        .each!(d => dic[d]+=1);
    foreach(key, value; dic)
        wc~=WordCnt(key, value);
    wc.sort!"a.count>b.count".each!writeln;
}

How can I improve this code? Like avoiding using foreach.

October 02, 2023

On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:

>
import std;

auto data=“I went for a walk, and fell down a hole.”;

void main(string[] args) {
    int[string] dic;
    struct WordCnt {
        string word;
        ulong count;
        string toString() const {
            return text("Word: ", word, " - number of instances: ", count);
        }
    }
    WordCnt[] wc;
    data
        .map!(c => lowercase.canFind(std.uni.toLower(c)) ? c : ' ')
        .to!string
        .splitter
        .each!(d => dic[d]+=1);
    foreach(key, value; dic)
        wc~=WordCnt(key, value);
    wc.sort!"a.count>b.count".each!writeln;
}

How can I improve this code? Like avoiding using foreach.

You don't need a struct at all, you can just have an int[string] aa

October 02, 2023

On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:

>
import std;

auto data=“I went for a walk, and fell down a hole.”;

You can improve it further by inlining

import std;

auto data = "I went for a walk, and fell down a hole.";

void main(string[] args)
{
    int[string] dic;

    data.split(' ').each!(w => dic[w]++);
    sort(dic.keys).each!(w => writeln(dic[w], " ",w));
}
October 02, 2023

On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:

>

How can I improve this code? Like avoiding using foreach.

You could fold into a hash that counts the occurrences like that:

import std.uni : toLower;
import std.array : split, array;
import std.stdio : writeln;
import std.algorithm : fold, sort, map;


auto data="I went for a walk, and fell down a hole. a went";

int main(string[] args)
{
    int[string] wc;
    data
        .toLower
        .split
        .fold!((result, element) { result[element] += 1; return result; })(wc)
        .byKeyValue
        .array
        .sort!((pair1, pair2) => pair1.value > pair2.value)
        .map!(pair => pair.key)
        .writeln
        ;
    return 0;
}

Not sure how to get rid of the declaration of the empty wc hash though.

Kind regards,
Christian

October 02, 2023

On Monday, 2 October 2023 at 06:19:29 UTC, Imperatorn wrote:

>

On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:

>
import std;

auto data=“I went for a walk, and fell down a hole.”;

You can improve it further by inlining

import std;

auto data = "I went for a walk, and fell down a hole.";

void main(string[] args)
{
    int[string] dic;

    data.split(' ').each!(w => dic[w]++);
    sort(dic.keys).each!(w => writeln(dic[w], " ",w));
}

I want the output sorted by value.

October 02, 2023

On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:

>
import std;

auto data=“I went for a walk, and fell down a hole.”;

void main(string[] args) {
    int[string] dic;
    struct WordCnt {
        string word;
        ulong count;
        string toString() const {
            return text("Word: ", word, " - number of instances: ", count);
        }
    }
    WordCnt[] wc;
    data
        .map!(c => lowercase.canFind(std.uni.toLower(c)) ? c : ' ')
        .to!string
        .splitter
        .each!(d => dic[d]+=1);
    foreach(key, value; dic)
        wc~=WordCnt(key, value);
    wc.sort!"a.count>b.count".each!writeln;
}

How can I improve this code? Like avoiding using foreach.

This is what I've got so far. Is there a way to do it any better?

import std;

auto data="I went for a walk, and fell down a hole.";

void main() {
    int[string] aa;
    struct WordCnt {
        string word;
        ulong count;
        string toString() const {
            return text("Word: ", word, " - number of instances: ", count);
        }
    }
    WordCnt[] wc;
    data
        .map!(c => lowercase.canFind(std.uni.toLower(c)) ? c : ' ')
        .to!string
        .splitter
        .each!(d => aa[d]+=1);
    aa.each!((key, value) {
            wc~=WordCnt(key, value);
    });
    wc.sort!"a.count>b.count"
        .each!writeln;
}
October 03, 2023

On Monday, 2 October 2023 at 20:20:44 UTC, Joel wrote:

>

I want the output sorted by value.

Look: https://forum.dlang.org/post/qjlmiohaoeolmoavwtif@forum.dlang.org

struct SIRALA(T) { }

You can use SIRALA(T). Okay, his language is Turkish but our common language is D. His feature is that he uses double AA. You will like it. Here is the source:

https://gist.github.com/run-dlang/808633909615dbda431baa01f795484f

SDB@79

October 03, 2023

On Monday, 2 October 2023 at 18:46:14 UTC, christian.koestlin wrote:

>

On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:

>

How can I improve this code? Like avoiding using foreach.

You could fold into a hash that counts the occurrences like that:

import std.uni : toLower;
import std.array : split, array;
import std.stdio : writeln;
import std.algorithm : fold, sort, map;


auto data="I went for a walk, and fell down a hole. a went";

int main(string[] args)
{
    int[string] wc;
    data
        .toLower
        .split
        .fold!((result, element) { result[element] += 1; return result; })(wc)
        .byKeyValue
        .array
        .sort!((pair1, pair2) => pair1.value > pair2.value)
        .map!(pair => pair.key)
        .writeln
        ;
    return 0;
}

Not sure how to get rid of the declaration of the empty wc hash though.

Kind regards,
Christian

Slightly improved:

import std;

auto data="I went for a walk, and fell down a hole. a went";

int main(string[] args)
{
    data
        .toLower
        .split
        .fold!((ref result, element) { ++result[element]; return result; })(uint[string].init)
        .byKeyValue
        .array
        .sort!((pair1, pair2) => pair1.value > pair2.value)
        .each!(pair => writeln("Word: ", pair.key, " - number of instances: ", pair.value))
        ;
    return 0;
}

Output:

Word: a - number of instances: 3
Word: went - number of instances: 2
Word: and - number of instances: 1
Word: i - number of instances: 1
Word: hole. - number of instances: 1
Word: for - number of instances: 1
Word: down - number of instances: 1
Word: fell - number of instances: 1
Word: walk, - number of instances: 1
October 03, 2023

On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote:

>
import std;

auto data=“I went for a walk, and fell down a hole.”;

[snip]

>

How can I improve this code? Like avoiding using foreach.

This works for me:

import std;

auto data="I went for a walk, and fell down a hole.";

void main(string[] args)
{
    if (args.length>1 && args[1].exists)
        data=readText(args[1]);
    data
        .toLower
        .map!(c => lowercase.canFind(std.uni.toLower(c)) ? c : ' ')
        .to!string
        .split
        .fold!((ref result, element) { result[element]+=1; return result; })(uint[string].init)
        .byKeyValue
        .array
        .sort!"a.value>b.value"
        .each!(pair => writeln("Word: ", pair.key, " - number of instances: ", pair.value));
}
October 03, 2023

On Tuesday, 3 October 2023 at 01:55:43 UTC, Andrey Zherikov wrote:

>

On Monday, 2 October 2023 at 18:46:14 UTC, christian.koestlin wrote:

>

[...]

Slightly improved:

import std;

[...]

Thanks .. the thing with ref result is very clever!
Should ref result be return result?

Kind regards,
Christian

« First   ‹ Prev
1 2