From f57f78ae1a752c802b3e98f016a224a155cc4d4c Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 27 Aug 2024 14:15:00 +0100 Subject: [PATCH] feat(docs): adding learn by for Wing --- docusaurus.config.js | 14 ++++ .../version-latest/01-introduction.md | 13 ++++ .../version-latest/02-hello-world.md | 26 ++++++++ .../version-latest/03-values.md | 42 ++++++++++++ .../version-latest/04-variables.md | 51 +++++++++++++++ .../version-latest/05-for.md | 65 +++++++++++++++++++ .../version-latest/06-ifelse.md | 52 +++++++++++++++ .../version-latest/07-while.md | 32 +++++++++ .../version-latest/08-optionality.md | 36 ++++++++++ .../version-latest/09-arrays.md | 49 ++++++++++++++ .../version-latest-sidebars.json | 8 +++ example_versions.json | 3 + 12 files changed, 391 insertions(+) create mode 100644 example_versioned_docs/version-latest/01-introduction.md create mode 100644 example_versioned_docs/version-latest/02-hello-world.md create mode 100644 example_versioned_docs/version-latest/03-values.md create mode 100644 example_versioned_docs/version-latest/04-variables.md create mode 100644 example_versioned_docs/version-latest/05-for.md create mode 100644 example_versioned_docs/version-latest/06-ifelse.md create mode 100644 example_versioned_docs/version-latest/07-while.md create mode 100644 example_versioned_docs/version-latest/08-optionality.md create mode 100644 example_versioned_docs/version-latest/09-arrays.md create mode 100644 example_versioned_sidebars/version-latest-sidebars.json create mode 100644 example_versions.json diff --git a/docusaurus.config.js b/docusaurus.config.js index 97a9a879d..a357d02c1 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -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: [ [ diff --git a/example_versioned_docs/version-latest/01-introduction.md b/example_versioned_docs/version-latest/01-introduction.md new file mode 100644 index 000000000..34abe9ee9 --- /dev/null +++ b/example_versioned_docs/version-latest/01-introduction.md @@ -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 \ No newline at end of file diff --git a/example_versioned_docs/version-latest/02-hello-world.md b/example_versioned_docs/version-latest/02-hello-world.md new file mode 100644 index 000000000..97c81a9b9 --- /dev/null +++ b/example_versioned_docs/version-latest/02-hello-world.md @@ -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! +``` diff --git a/example_versioned_docs/version-latest/03-values.md b/example_versioned_docs/version-latest/03-values.md new file mode 100644 index 000000000..ba1ee698a --- /dev/null +++ b/example_versioned_docs/version-latest/03-values.md @@ -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 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/04-variables.md b/example_versioned_docs/version-latest/04-variables.md new file mode 100644 index 000000000..a4e986500 --- /dev/null +++ b/example_versioned_docs/version-latest/04-variables.md @@ -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 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/05-for.md b/example_versioned_docs/version-latest/05-for.md new file mode 100644 index 000000000..aeaccf7a1 --- /dev/null +++ b/example_versioned_docs/version-latest/05-for.md @@ -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[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 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/06-ifelse.md b/example_versioned_docs/version-latest/06-ifelse.md new file mode 100644 index 000000000..325a3d8b5 --- /dev/null +++ b/example_versioned_docs/version-latest/06-ifelse.md @@ -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 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/07-while.md b/example_versioned_docs/version-latest/07-while.md new file mode 100644 index 000000000..224933c7a --- /dev/null +++ b/example_versioned_docs/version-latest/07-while.md @@ -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 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/08-optionality.md b/example_versioned_docs/version-latest/08-optionality.md new file mode 100644 index 000000000..34a076876 --- /dev/null +++ b/example_versioned_docs/version-latest/08-optionality.md @@ -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 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/09-arrays.md b/example_versioned_docs/version-latest/09-arrays.md new file mode 100644 index 000000000..f8ae28cbb --- /dev/null +++ b/example_versioned_docs/version-latest/09-arrays.md @@ -0,0 +1,49 @@ +--- +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 defined with the [] syntax. +Individual array items are accessed using the .at(index: num) method. +Arrays are similar to dynamically sized arrays or vectors in other languages. + +```js playground example title="main.w" +let main = () => { + + let a = MutArray[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[1, 2, 3]; + let twoD = MutArray>[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 +``` \ No newline at end of file diff --git a/example_versioned_sidebars/version-latest-sidebars.json b/example_versioned_sidebars/version-latest-sidebars.json new file mode 100644 index 000000000..cff0c94e1 --- /dev/null +++ b/example_versioned_sidebars/version-latest-sidebars.json @@ -0,0 +1,8 @@ +{ + "defaultSidebar": [ + { + "type": "autogenerated", + "dirName": "." + } + ] +} diff --git a/example_versions.json b/example_versions.json new file mode 100644 index 000000000..c24f3d37c --- /dev/null +++ b/example_versions.json @@ -0,0 +1,3 @@ +[ + "latest" +]