Skip to content

Commit

Permalink
chore(docs): moved wing by example docs to the main wing repo
Browse files Browse the repository at this point in the history
  • Loading branch information
David Boyne authored and David Boyne committed Sep 24, 2024
1 parent 62d0fab commit d72a4ff
Show file tree
Hide file tree
Showing 37 changed files with 1,652 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/by-example/01-introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Wing by example
id: wing-by-example
slug: /
sidebar_label: Introduction
description: Hands-on introduction to Wing using annotated code
keywords: [Wing language, api]
image: /img/wing-by-example.png
---


Wing is an open source programming language for the cloud. Wing combines infrastructure and runtime code in one language, enabling developers to stay in their creative flow, and to deliver better software, faster and more securely.

**Wing by Example** is a hands-on introduction to Wing using annotated example programs. Check out the first example or browse the list using the navigation.

Unless stated otherwise, examples here assume the latest major release of Wing and may use new language features. If something isn't working and upgrading to the latest version doesn't fix it, please [open an issue on our GitHub](https://github.com/winglang/wing/issues/new/choose) or [send us a message on Discord](https://t.winglang.io/discord)!
24 changes: 24 additions & 0 deletions docs/by-example/02-hello-world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Wing by example
id: hello-world
slug: /hello-world
sidebar_label: Hello world
description: Hello world wing example
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

# Hello world

By default, Wing code is executed when it's compiled.

```js playground title="main.w"
log("Hello world!");
```

```bash title="Wing console output"
# Run locally with wing console
wing it

Hello world!
```
38 changes: 38 additions & 0 deletions docs/by-example/03-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Primitive values
id: primitives
slug: /primitive-values
sidebar_label: Primitive values
description: Hello world wing example
keywords: [Wing language, example, primitives, values]
image: /img/wing-by-example.png
---

Wing has primitive types including strings, integers, floats, booleans, etc. Here are a few basic examples.

- Strings, which can be added together with +
- Integers and floats
- Booleans, with boolean operators as you'd expect

```js playground title="main.w"
log("Hello " + "Wing");

log("1+1 = {1+1}");
log("7.0/3.0 = {7.0/3.0}");

log(true && true);
log(true || false);
log(!true);
```

```bash title="Wing console output"
# Run locally with wing console
wing it

Hello Wing
1+1 = 2
7.0/3.0 = 2.3333333333333335
true
true
false
```
45 changes: 45 additions & 0 deletions docs/by-example/04-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: Variables
id: Variables
slug: /Variables
sidebar_label: Variables
description: Using variables with Wing
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

Variables are declared with the `let` keyword. The type of most variables can be inferred automatically, but you can also add an explicit type annotation if needed.

By default, Wing doesn't let you reassign to variables, unless you add the `var` modifier. See [this blog post](https://www.winglang.io/blog/2023/02/02/good-cognitive-friction) for more details.

```js title="main.w"

// var delcares a varaible. Wing infers the type
let a = "initial";
log(a);


// type can also be declared
let b: num = 1;
let c: num = 2;
log("{b}, {c}");

// variables cannot be changed using let without var
let d: str = "Hello";
// d = "Test"; // error: Variable is not reassignable

// makes variable reassignable
let var s = "hello";
s = "hello world"; // compiles
log(s);

```

```bash title="Wing console output"
# Run locally with wing console
wing it

initial
1, 2
hello world
```
60 changes: 60 additions & 0 deletions docs/by-example/05-for.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: For
id: for
slug: /for
sidebar_label: For
description: Using for loops with Wing
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

Wing supports looping over collections with `for..in` statements.

[for..in](/docs/api/language-reference#26-for) is used to iterate over an array, a set or a range.

```js playground title="main.w"
// a standard for loop
for item in 1..3 {
log(item);
}

// for-in with arrays
let arr = [1, 2, 3];
for item in arr {
log(item);
}

// break a loop
let items = Set<num>[1, 2, 3];
for item in items {
if item == 1 {
break;
}
log(item);
}

// continue the next iteration of the loop
for item in 1..10 {
if item % 2 == 0 {
continue;
}
log(item);
}

```

```bash title="Wing console output"
# Run locally with wing console
wing it

1
2
1
2
3
1
3
5
7
9
```
47 changes: 47 additions & 0 deletions docs/by-example/06-ifelse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: If/Else
id: if-else
slug: /if-else
sidebar_label: If/Else
description: Using if else with Wing
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

Flow control can be done with if/else statements. The `if` statement is optionally followed by any number of `else if` clauses and a final `else` clause.

```js playground title="main.w"
if 7 % 2 == 0 {
log("7 is even");
} else {
log("7 is odd");
}

if 8 % 4 == 0 {
log("8 is divisble by 4");
}

if 8 % 2 == 0 || 7 % 2 == 0 {
log("either 8 or 7 are even");
}

let value = 9;
if value < 0 {
log("${value} is negative");
} else if value < 10 {
log("${value} has 1 digit");
} else {
log("{value} has multiple digits");
}

```

```bash title="Wing console output"
# Run locally with wing console
wing it

7 is odd
8 is divisble by 4
either 8 or 7 are even
9 has 1 digit
```
28 changes: 28 additions & 0 deletions docs/by-example/07-while.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: While
id: while
slug: /while
sidebar_label: While
description: Using while statements with Wing
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

While loops repeatedly check a condition and run a block of code until the condition is `false`.

```js playground title="main.w"
let var i = 0;

while i < 2 {
log("while {i}");
i = i + 1;
}
```

```bash title="Wing console output"
# Run locally with wing console
wing it

while 0
while 1
```
31 changes: 31 additions & 0 deletions docs/by-example/08-optionality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Optionality
id: optionality
slug: /optionality
sidebar_label: Optionality
description: Using while statements with Wing
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

Nullity is a primary source of bugs in software. Being able to guarantee that a value will never be null makes it easier to write safe code without constantly having to take nullity into account.

An optional value can be either "nil" or a non-nil value. The type of an optional variable is represented by adding a question mark (`?`) to its end.

```js playground title="main.w"
let monday: str = "doctor";
let tuesday: str? = nil;

// Set next to tuesday if there is a value otherwise use monday value
let next = tuesday ?? monday;

log(next);

```
```bash title="Wing console output"
# Run locally with wing console
wing it

doctor
```
45 changes: 45 additions & 0 deletions docs/by-example/09-arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: Arrays
id: arrays
slug: /arrays
sidebar_label: Arrays
description: Using arrays with Wing
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

Arrays are dynamically sized in Wing and are created with the [] syntax.
Individual array items can be accessed using index operation, `array[index]`, or with the `.at(index: num)` method.
Arrays are similar to dynamically sized arrays or vectors in other languages.

```js playground example title="main.w"
let a = MutArray<num>[1, 2, 3];

log("{a[0]}, {a[1]}, {a[2]}");

a[2] = 4;

log("mutated value: {a[2]}");
log("len: {a.length}");

let data = MutArray<num>[1, 2, 3];
let twoD = MutArray<MutArray<num>>[data];

for array in twoD {
for item in array {
log(item * 10);
}
}
```

```bash title="Wing console output"
# Run locally with wing console
wing it

1, 2, 3
mutated value: 4
len: 3
10
20
30
```
44 changes: 44 additions & 0 deletions docs/by-example/10-maps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: Maps
id: maps
slug: /maps
sidebar_label: Maps
description: Using maps with Wing
keywords: [Wing language, example]
image: /img/wing-by-example.png
---

Maps are key-value data structures that let you associate strings with any kinds of other values.

```js playground example title="main.w"
// immutable map
let configration = Map<str>{
"URL" => "https://winglang.io"
};

// mutable map
let listOfPrices = MutMap<num>{
"PRODUCT_1" => 100.00,
"PRODUCT_2" => 200.00,
"PRODUCT_3" => 300.00
};

// Map<num> is inferred
let values = {"a" => 1, "b" => 2}; // immutable map, Map<num> is inferred

// Change the values of the mutable map
listOfPrices.set("PRODUCT_1", 500);

log(configration.get("URL"));
log(Json.stringify(values.keys()));
log(Json.stringify(listOfPrices.get("PRODUCT_1")));
```

```bash title="Wing console output"
# Run locally with wing console
wing it

https://winglang.io
["a","b"]
500
```
Loading

0 comments on commit d72a4ff

Please sign in to comment.