Filter a Dictionary in D

github logo ・1 min read

Python (14 Part Series)

1) Boolean in D 2) Comments in D 3 ... 12 3) Raise Exceptions in D 4) Add to a List in D 5) Count of a List in D 6) String Contained in a String in D 7) Where did my string go in D 8) Case Insensitive in D 9) Add to a Dictionary in D 10) Check Dictionary has key in D 11) Filter a Dictionary in D 12) Split, Apply, Merge in D 13) Map Reduce in D 14) Skip the first line of a file in D

This wasn't so much about filtering the content, but instead having a list of dictionaries and filtering to ones with specific data.

auto data = [["key": 5], ["key": 2]];

auto foo = data.filter!(x => x["key"] == 5);

Well I showed this with my count example, it is one of the reasons I like D. I just keep with the same tools and there is similarities in how they all work. One of the challenges can be producing the ability to traverse, let me dive into this dictionary filtering.

import std.array;

auto dict = ["one" : 1
           , "two" : 2
           , "three" : 3];

dict.byPair
    .filter!(x => x[0] == "one")
    .assocArray;

In this case I'm asking to traverse the dictionary by each key/value pair. After the filtering we build out a new dictionary.

As an aside, while I find consistency in these, I'm also aware D has inconsistency.

twitter logo DISCUSS
Classic DEV Post from Apr 25

Terrible interview question: Swap variables without a temporary

Examination of interview questions that don't evaluate candidates well

Jesse Phillips profile image
Senior Quality Assurance (SDET) starting to establish a larger presence in communities.