Skip to content

Commit

Permalink
Merge pull request #1060 from MartinNowak/yourCodeHere
Browse files Browse the repository at this point in the history
more examples for YourCodeHere
  • Loading branch information
DmitryOlshansky committed Aug 15, 2015
2 parents 50e6279 + ab656c0 commit 680c604
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions index.dd
Expand Up @@ -89,6 +89,109 @@ void main()
}
----
)
$(EXTRA_EXAMPLE
----
// Get your local weather report
pragma(lib, "curl");
import std.functional, std.json, std.net.curl,
std.stdio, std.string;

alias getJSON = pipe!(get, parseJSON);
auto K2C = (float f) => f - 273.15;
auto K2F = (float f) => f / 5 * 9 - 459.67;

void main()
{
auto loc = getJSON("ipinfo.io/")["loc"]
.str.split(",");
auto resp = getJSON(
"api.openweathermap.org/data/2.5/weather" ~
"?lat=" ~ loc[0] ~ "&lon=" ~ loc[1]);

auto city = resp["name"].str;
auto country = resp["sys"]["country"].str;
auto desc = resp["weather"][0]["description"].str;
auto temp = resp["main"]["temp"].floating;

writefln(`
+-----------------------------------------+
|%s|
+-----------------------------------------+
| weather | %-23s|
+-----------------------------------------+
| temperature | %.2f°C (%.2f°F) |
+-----------------------------------------+
`.outdent,
centerJustifier(city ~ ", " ~ country, 41),
desc, temp.K2C, temp.K2F);
}
----
)
$(EXTRA_EXAMPLE
----
// D is like...
pragma(lib, "curl");
import std.functional, std.json, std.net.curl,
std.stdio;

alias getJSON = pipe!(get, parseJSON);

void main()
{
auto json = getJSON(
"itsthisforthat.com/api.php?json");
writefln("So, basically D is like a %s for %s",
json["this"].str, json["that"].str);
}
----
)
$(EXTRA_EXAMPLE
----
// RPN calculator
import std.algorithm, std.container.array,
std.conv, std.stdio, std.meta;

void main()
{
Array!int stack;

void binop(string op)()
{
stack[$ - 2] = mixin("stack[$ - 2] " ~
op ~ " stack[$ - 1]");
stack.removeBack();
writeln(stack[$ - 1]);
}

void process(in char[] token)
{
alias Ops = AliasSeq!("+", "-", "*", "/", "%");
Lswitch:
switch (token)
{
foreach (op; Ops)
{
case op:
binop!op();
break Lswitch;
}

case "=":
writeln(stack[$ - 1]);
stack.removeBack();
break;

default:
stack.insertBack(token.to!int);
break;
}
}

stdin.byLine.map!splitter.joiner.each!process;
}
----
)

)

D is a language with C-like syntax and static typing. It pragmatically combines
Expand Down

0 comments on commit 680c604

Please sign in to comment.