Skip to content

Comments

Add hashmap + D arrays example#1756

Merged
andralex merged 1 commit intodlang:masterfrom
wilzbach:add-hashmap-example
Jul 25, 2017
Merged

Add hashmap + D arrays example#1756
andralex merged 1 commit intodlang:masterfrom
wilzbach:add-hashmap-example

Conversation

@wilzbach
Copy link
Contributor

From @CyberShadow's comment:

So, not sure about this example's illustrative powers. D arrays are awesome too!

I really liked the example and imho it makes a good fit for the frontpage.

(this temporarily includes the first commit from #1755 to fix the roulette picker, s.t. it can be previewed)

Copy link
Member

@CyberShadow CyberShadow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this one; it doesn't seem particularly exciting on its own. It's not really something you can't do in almost any language.

index.dd Outdated
)
$(EXTRA_EXAMPLE
----
void main()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a comment at the top explaining what the program does.
"Counts frequencies of adjacent pairs of DNA bases" (though I'm fairly sure I got the terminology wrong).

index.dd Outdated
int[char[2]] aa;

// The string `arr` has a limited alphabet: {A, C, G, T}
// Thus, iteration can be done _without_ decoding
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should bring up auto-decoding in a front page example.

index.dd Outdated
// iterate over all pairs in the string and observe each pair
// ('A', 'G'), ('G', 'A'), ('A', 'T'), ...
foreach (i; 0 .. arr.length - 1)
aa[arr[i .. $][0 .. 2]]++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth mentioning/implying that slicing a string does not allocate a copy

index.dd Outdated
foreach (i; 0 .. arr.length - 1)
aa[arr[i .. $][0 .. 2]]++;

// iterate over all key/value pairs of the Associative Array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems redundant.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@wilzbach wilzbach force-pushed the add-hashmap-example branch from 9ef887e to d6bb71a Compare July 19, 2017 19:18
@wilzbach wilzbach requested a review from andralex July 19, 2017 19:18
@wilzbach
Copy link
Contributor Author

@ others: what do you feel about this one? Is showing cool slicing with nice iteration a good example?

image

@wilzbach
Copy link
Contributor Author

@ others: what do you feel about this one? Is showing cool slicing with nice iteration a good example?

There was a long discussion on the NG one months ago with the gist that the current examples are too advanced and that Python.org shows simple things like list comprehensions on their front page.

Copy link
Member

@PetarKirov PetarKirov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the example.

// ('A', 'B'), ('B', 'B'), ('B', 'A'), ...
// String slicing doesn't allocate a copy
foreach (i; 0 .. arr.length - 1)
aa[arr[i .. $][0 .. 2]]++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above looks cool, but can be simplified to:

aa[arr[i .. i + 2]]++;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case it's necessary:

onlineapp.d(11): Error: cannot implicitly convert expression `arr[i..i + 2LU]` of type `string` to `char[2]`

@wilzbach wilzbach force-pushed the add-hashmap-example branch 2 times, most recently from a707ff9 to aa4af2f Compare July 20, 2017 18:30
@andralex andralex merged commit 7c4b727 into dlang:master Jul 25, 2017
@wilzbach wilzbach deleted the add-hashmap-example branch December 18, 2017 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants