Skip to content

Commit

Permalink
feat(docs): adding learn by for Wing
Browse files Browse the repository at this point in the history
  • Loading branch information
David Boyne authored and David Boyne committed Aug 27, 2024
1 parent 2ebeaf5 commit f57f78a
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 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
13 changes: 13 additions & 0 deletions example_versioned_docs/version-latest/01-introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
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.

Wing by Example is a hands-on introduction to Dart using annotated example programs, inspired by Go By Example, Dark by Example and Haskell By Example
26 changes: 26 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,26 @@
---
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


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

main();
```

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

Hello world!
```
42 changes: 42 additions & 0 deletions example_versioned_docs/version-latest/03-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Values
id: values
slug: /values
sidebar_label: 2. Values
description: Hello world wing example
keywords: [Wing language, example]
---


Wing has various value 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"
let main = () => {
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);
};

main();
```

```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
```
51 changes: 51 additions & 0 deletions example_versioned_docs/version-latest/04-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Variables
id: Variables
slug: /Variables
sidebar_label: 4. Variables
description: Using variables with Wing
keywords: [Wing language, example]
---

Wing has various value 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 title="main.w"
let main = () => {

// 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 mutable
let var s = "hello";
s = "hello world"; // compiles
log(s);

};

main();

```

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

initial
1, 2
hello world
```
65 changes: 65 additions & 0 deletions example_versioned_docs/version-latest/05-for.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: For
id: for
slug: /for
sidebar_label: 5. For
description: Using for loops with Wing
keywords: [Wing language, example]
---

Wing supports for..in statements.

[for..in](/docs/api/language-reference#26-for) is used to iterate over an array, a set or a range. The loop invariant in for loops is implicitly re-assignable (var).

```js playground title="main.w"
let main = () => {

// 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);
}

};

main();

```

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

1
2
1
2
3
1
3
5
7
9
```
52 changes: 52 additions & 0 deletions example_versioned_docs/version-latest/06-ifelse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
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 else if and else.

```js playground title="main.w"
let main = () => {

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:num = 9;
if(value < 0){
log("${value} is negative");
} else if value < 10 {
log("${value} has 1 digit");
} else {
log("{value} has multiple digits");
}

};

main();

```

```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
```
32 changes: 32 additions & 0 deletions example_versioned_docs/version-latest/07-while.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: While
id: while
slug: /while
sidebar_label: 7. While
description: Using while statements with Wing
keywords: [Wing language, example]
---

```js playground title="main.w"
let main = () => {

let var i = 0;

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

};

main();

```

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

while 0
while 1
```
36 changes: 36 additions & 0 deletions example_versioned_docs/version-latest/08-optionality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
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.

Optionality requires developers to be more intentional about working with the concept of "lack of value".

```js playground title="main.w"
let main = () => {

let monday:str = "doctor";
let tuesday: str? = nil;

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

log("{next}");

};

main();

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

doctor
```
Loading

0 comments on commit f57f78a

Please sign in to comment.