Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): added wing by example #986

Merged
merged 60 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f57f78a
feat(docs): adding learn by for Wing
Aug 27, 2024
d41202d
feat(docs): adding learn by for Wing
Aug 27, 2024
10a8a8b
adding more examples to learn by
Sep 2, 2024
fad5a72
adding more examples to learn by
Sep 2, 2024
e1c3537
adding more examples
Sep 2, 2024
ebfdde2
adding more examples
Sep 3, 2024
179bcd5
Merge branch 'main' into wing-learn-by
boyney123 Sep 4, 2024
4f2b115
Update example_versioned_docs/version-latest/01-introduction.md
boyney123 Sep 9, 2024
3618a07
Update example_versioned_docs/version-latest/01-introduction.md
boyney123 Sep 9, 2024
a4d4727
Update example_versioned_docs/version-latest/02-hello-world.md
boyney123 Sep 9, 2024
5e1e08b
Update example_versioned_docs/version-latest/03-values.md
boyney123 Sep 9, 2024
125a2e9
Update example_versioned_docs/version-latest/11-sets.md
boyney123 Sep 9, 2024
da21880
Update example_versioned_docs/version-latest/10-maps.md
boyney123 Sep 9, 2024
6fdc7db
Update example_versioned_docs/version-latest/12-structs.md
boyney123 Sep 9, 2024
3c28bed
Merge branch 'main' into wing-learn-by
boyney123 Sep 9, 2024
631ace6
updates docs
Sep 9, 2024
12d20a7
update to examples
Sep 9, 2024
c999175
Update example_versioned_docs/version-latest/11-sets.md
boyney123 Sep 9, 2024
8508a7d
Update example_versioned_docs/version-latest/12-structs.md
boyney123 Sep 9, 2024
93e57a3
Update example_versioned_docs/version-latest/14-async-functions.md
boyney123 Sep 9, 2024
91c1e35
Update example_versioned_docs/version-latest/14-async-functions.md
boyney123 Sep 9, 2024
ce79fe6
Update example_versioned_docs/version-latest/14-async-functions.md
boyney123 Sep 9, 2024
22b0fbc
Update example_versioned_docs/version-latest/17-recursion.md
boyney123 Sep 9, 2024
be174e6
Update example_versioned_docs/version-latest/18-methods.md
boyney123 Sep 9, 2024
7788d15
Update example_versioned_docs/version-latest/19-interfaces.md
boyney123 Sep 9, 2024
5810ace
Update example_versioned_docs/version-latest/19-interfaces.md
boyney123 Sep 9, 2024
ba4ba10
Update example_versioned_docs/version-latest/18-methods.md
boyney123 Sep 10, 2024
ecf53d7
Update example_versioned_docs/version-latest/23-Json.md
boyney123 Sep 10, 2024
be28f1e
Update example_versioned_docs/version-latest/26-number-parsing.md
boyney123 Sep 10, 2024
f4d0101
Update example_versioned_docs/version-latest/30-reading-writing-files.md
boyney123 Sep 10, 2024
87916f5
Update example_versioned_docs/version-latest/32-testing.md
boyney123 Sep 10, 2024
6cfc403
Update example_versioned_docs/version-latest/33-http-client.md
boyney123 Sep 10, 2024
61b6a6a
Update example_versioned_docs/version-latest/34-http-server.md
boyney123 Sep 10, 2024
d687ef3
Update example_versioned_docs/version-latest/31-directories.md
boyney123 Sep 10, 2024
e062ca3
added some more docs
Sep 10, 2024
68104d6
Merge branch 'main' into wing-learn-by
boyney123 Sep 10, 2024
01ee33a
Merge branch 'main' into wing-learn-by
boyney123 Sep 11, 2024
3fdb1f8
Update example_versioned_docs/version-latest/04-variables.md
boyney123 Sep 11, 2024
337897e
Update example_versioned_docs/version-latest/04-variables.md
boyney123 Sep 11, 2024
f8cf6b1
Update example_versioned_docs/version-latest/05-for.md
boyney123 Sep 11, 2024
b0147ed
Update example_versioned_docs/version-latest/05-for.md
boyney123 Sep 11, 2024
a54d86f
Update example_versioned_docs/version-latest/05-for.md
boyney123 Sep 11, 2024
407ff9e
Update example_versioned_docs/version-latest/05-for.md
boyney123 Sep 11, 2024
b19ca0f
Update example_versioned_docs/version-latest/05-for.md
boyney123 Sep 11, 2024
93a6222
Update example_versioned_docs/version-latest/06-ifelse.md
boyney123 Sep 11, 2024
e6b762d
Update example_versioned_docs/version-latest/06-ifelse.md
boyney123 Sep 11, 2024
15033be
Update example_versioned_docs/version-latest/06-ifelse.md
boyney123 Sep 11, 2024
5df2505
Update example_versioned_docs/version-latest/06-ifelse.md
boyney123 Sep 11, 2024
cd3c94c
Update example_versioned_docs/version-latest/06-ifelse.md
boyney123 Sep 11, 2024
3253bbf
Update example_versioned_docs/version-latest/07-while.md
boyney123 Sep 11, 2024
376e148
Update example_versioned_docs/version-latest/07-while.md
boyney123 Sep 11, 2024
bd517f6
Update example_versioned_docs/version-latest/08-optionality.md
boyney123 Sep 11, 2024
ee8dc48
Update example_versioned_docs/version-latest/08-optionality.md
boyney123 Sep 11, 2024
87f3647
Update example_versioned_docs/version-latest/08-optionality.md
boyney123 Sep 11, 2024
cbde0c1
Update example_versioned_docs/version-latest/09-arrays.md
boyney123 Sep 11, 2024
6421690
Update example_versioned_docs/version-latest/09-arrays.md
boyney123 Sep 11, 2024
0196b65
Update example_versioned_docs/version-latest/08-optionality.md
boyney123 Sep 11, 2024
e0eef56
Update example_versioned_docs/version-latest/15-variadic-functions.md
boyney123 Sep 11, 2024
51c3f10
Update example_versioned_docs/version-latest/16-closures.md
boyney123 Sep 11, 2024
7c55356
fixing example
Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ const config = {
// ... other options
}
],
[
"@docusaurus/plugin-content-docs",
{
id: "example",
path: "example",
routeBasePath: "docs/learn",
editUrl: (params) =>
`${winglangOrgUrl}/wing/tree/main/docs/api/${params.docPath}`,
breadcrumbs: true,
includeCurrentVersion: false,
// sidebarPath: require.resolve('./sidebarsCommunity.js'),
// ... other options
}
],
],
presets: [
[
Expand Down Expand Up @@ -254,7 +268,7 @@ const config = {
},

{
to: "docs/why-wing",
to: "docs/learn",
position: "left",
label: "Learn",
className: "header-text-link",
Expand Down
15 changes: 15 additions & 0 deletions example_versioned_docs/version-latest/01-introduction.md
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)!
23 changes: 23 additions & 0 deletions example_versioned_docs/version-latest/02-hello-world.md
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!
```
boyney123 marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 37 additions & 0 deletions example_versioned_docs/version-latest/03-values.md
boyney123 marked this conversation as resolved.
Show resolved Hide resolved
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);
Chriscbr marked this conversation as resolved.
Show resolved Hide resolved
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
```
44 changes: 44 additions & 0 deletions example_versioned_docs/version-latest/04-variables.md
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
```
59 changes: 59 additions & 0 deletions example_versioned_docs/version-latest/05-for.md
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
```
46 changes: 46 additions & 0 deletions example_versioned_docs/version-latest/06-ifelse.md
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
```
27 changes: 27 additions & 0 deletions example_versioned_docs/version-latest/07-while.md
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
```
30 changes: 30 additions & 0 deletions example_versioned_docs/version-latest/08-optionality.md
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
```
44 changes: 44 additions & 0 deletions example_versioned_docs/version-latest/09-arrays.md
boyney123 marked this conversation as resolved.
Show resolved Hide resolved
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
```
Loading