-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docs): added wing by example (#986)
New pages for "Wing by Example" this is inspired by other popular langugages. - Haskell - https://lotz84.github.io/haskellbyexample/ex/arrays - Go - https://gobyexample.com/arrays - Dart - https://www.jpryan.me/dartbyexample/ Currently WIP. https://wing-docs-git-wing-learn-by-monada.vercel.app/docs/learn/
- Loading branch information
Showing
40 changed files
with
1,604 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
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] | ||
--- | ||
|
||
|
||
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)! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: Wing by example | ||
id: hello-world | ||
slug: /hello-world | ||
sidebar_label: 1. Hello world | ||
description: Hello world wing example | ||
keywords: [Wing language, example] | ||
--- | ||
|
||
# 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! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: Primitive values | ||
id: primitives | ||
slug: /primitive-values | ||
sidebar_label: 2. Primitive values | ||
description: Hello world wing example | ||
keywords: [Wing language, example, primitives, values] | ||
--- | ||
|
||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Variables | ||
id: Variables | ||
slug: /Variables | ||
sidebar_label: 4. Variables | ||
description: Using variables with Wing | ||
keywords: [Wing language, example] | ||
--- | ||
|
||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: For | ||
id: for | ||
slug: /for | ||
sidebar_label: 5. For | ||
description: Using for loops with Wing | ||
keywords: [Wing language, example] | ||
--- | ||
|
||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: If/Else | ||
id: if-else | ||
slug: /if-else | ||
sidebar_label: 6. If/Else | ||
description: Using if else with Wing | ||
keywords: [Wing language, example] | ||
--- | ||
|
||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: While | ||
id: while | ||
slug: /while | ||
sidebar_label: 7. While | ||
description: Using while statements with Wing | ||
keywords: [Wing language, example] | ||
--- | ||
|
||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: Optionality | ||
id: optionality | ||
slug: /optionality | ||
sidebar_label: 8. Optionality | ||
description: Using while statements with Wing | ||
keywords: [Wing language, example] | ||
--- | ||
|
||
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Arrays | ||
id: arrays | ||
slug: /arrays | ||
sidebar_label: 9. Arrays | ||
description: Using arrays with Wing | ||
keywords: [Wing language, example] | ||
--- | ||
|
||
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 | ||
``` |
Oops, something went wrong.