Giter Club home page Giter Club logo

d-tips's Introduction

d-tips

D is a language, where code often just works. You don't need to read docs thoroughly, rather work with it by pure intuition. But sometimes there is a shorter way, which you'll never guess if you don't know. So there is a humble collection of such places.

Note

Know something interesting? PRs are welcome.d :)

Attributes can be inferred

Let's imagine, you have a good old top-level function:

void foo() {
  // ...
}

It has a type void(). But you know that it's nothrow and @nogc. Instead of typing them by hand, you can use function attribute interference. The simplest way to trigger it is to make foo auto function:

auto foo() {
  // ...
}

Its type is pure nothrow @nogc @safe void() as expected. The other cases are function template, nested function and function literals.

Lazy and NOT cached

Suppose, you have simple code:

auto arr = [1];
auto i = 0;
auto newArr = arr.map!((a) => i++);
writeln(newArr[0], newArr[0], newArr[0]);

You can expect it to output 000, but the actual output is 012. Each time you need a result, the function will recalculate it. This isn't a problem, but some functions (e.g., filter and tee) can trigger recalculation even if they don't need to. So, if you want to avoid recalculation there are two ways: either place .cache/cacheBidirectional after map and potentially lose random access, either convert it to array and store completely in memory.

d-tips's People

Contributors

darkhole1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.