From f57f78ae1a752c802b3e98f016a224a155cc4d4c Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 27 Aug 2024 14:15:00 +0100 Subject: [PATCH 01/56] 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" +] From d41202d921f42d7967632944c0274213f016c7d5 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 27 Aug 2024 16:47:29 +0100 Subject: [PATCH 02/56] feat(docs): adding learn by for Wing --- .../version-latest/10-structs.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 example_versioned_docs/version-latest/10-structs.md diff --git a/example_versioned_docs/version-latest/10-structs.md b/example_versioned_docs/version-latest/10-structs.md new file mode 100644 index 000000000..cca74be82 --- /dev/null +++ b/example_versioned_docs/version-latest/10-structs.md @@ -0,0 +1,47 @@ +--- +title: Structs +id: structs +slug: /structs +sidebar_label: 10. Structs +description: Using arrays with Wing +keywords: [Wing language, example] +--- + +Structs are loosely modeled after typed JSON literals in JavaScript. + +```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 From 10a8a8baf123660fbf18fdc027b5e0da19650f0a Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 2 Sep 2024 11:37:01 +0100 Subject: [PATCH 03/56] adding more examples to learn by --- docusaurus.config.js | 2 +- .../version-latest/01-introduction.md | 6 ++- .../version-latest/10-maps.md | 42 ++++++++++++++++ .../version-latest/10-structs.md | 47 ------------------ .../version-latest/11-sets.md | 37 ++++++++++++++ .../version-latest/12-structs.md | 48 +++++++++++++++++++ 6 files changed, 132 insertions(+), 50 deletions(-) create mode 100644 example_versioned_docs/version-latest/10-maps.md delete mode 100644 example_versioned_docs/version-latest/10-structs.md create mode 100644 example_versioned_docs/version-latest/11-sets.md create mode 100644 example_versioned_docs/version-latest/12-structs.md diff --git a/docusaurus.config.js b/docusaurus.config.js index a357d02c1..8b3b8c07e 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -268,7 +268,7 @@ const config = { }, { - to: "docs/why-wing", + to: "docs/learn", position: "left", label: "Learn", className: "header-text-link", diff --git a/example_versioned_docs/version-latest/01-introduction.md b/example_versioned_docs/version-latest/01-introduction.md index 34abe9ee9..a1c5adcb6 100644 --- a/example_versioned_docs/version-latest/01-introduction.md +++ b/example_versioned_docs/version-latest/01-introduction.md @@ -8,6 +8,8 @@ keywords: [Wing language, api] --- -Wing is an open-source programming language. +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 Dart using annotated example programs, inspired by Go By Example, Dark by Example and Haskell By Example \ No newline at end of file +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 Wing and may use new language features. Try to upgrade to the latest version if something isn't working. \ No newline at end of file diff --git a/example_versioned_docs/version-latest/10-maps.md b/example_versioned_docs/version-latest/10-maps.md new file mode 100644 index 000000000..9fbcb4a7e --- /dev/null +++ b/example_versioned_docs/version-latest/10-maps.md @@ -0,0 +1,42 @@ +--- +title: Maps +id: maps +slug: /maps +sidebar_label: 10. Maps +description: Using maps with Wing +keywords: [Wing language, example] +--- + + +```js playground example title="main.w" +// immutable map +let configration = Map{ + "URL" => "https://winglang.io" +}; + +// mutable map +let listOfPrices = MutMap{ + "PRODUCT_1" => 100.00, + "PRODUCT_2" => 200.00, + "PRODUCT_3" => 300.00 +}; + +// Map is inferred +let values = {"a" => 1, "b" => 2}; // immutable map, Map 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 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/10-structs.md b/example_versioned_docs/version-latest/10-structs.md deleted file mode 100644 index cca74be82..000000000 --- a/example_versioned_docs/version-latest/10-structs.md +++ /dev/null @@ -1,47 +0,0 @@ ---- -title: Structs -id: structs -slug: /structs -sidebar_label: 10. Structs -description: Using arrays with Wing -keywords: [Wing language, example] ---- - -Structs are loosely modeled after typed JSON literals in JavaScript. - -```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_docs/version-latest/11-sets.md b/example_versioned_docs/version-latest/11-sets.md new file mode 100644 index 000000000..ab903a3ad --- /dev/null +++ b/example_versioned_docs/version-latest/11-sets.md @@ -0,0 +1,37 @@ +--- +title: Sets +id: sets +slug: /sets +sidebar_label: 11. Sets +description: Using sets with Wing +keywords: [Wing language, example] +--- + +Sets are immutable by default `Set`, you can make them immutable using `>`. + +Sets will store and return unique values. + +```js playground example title="main.w" +//// mutable set +let unqiueNumbers = MutSet[1, 2, 3, 3, 3]; +unqiueNumbers.add(4); +unqiueNumbers.delete(1); + +// immutable set, will make values unique +let uniqueStrings = Set["unique", "values", "values"]; + + +log(Json.stringify(unqiueNumbers.toArray())); +log(Json.stringify(unqiueNumbers.size)); + +log(Json.stringify(uniqueStrings.toArray())); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +[2,3,4] +3 +["unique", "values"] +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/12-structs.md b/example_versioned_docs/version-latest/12-structs.md new file mode 100644 index 000000000..bf0980f72 --- /dev/null +++ b/example_versioned_docs/version-latest/12-structs.md @@ -0,0 +1,48 @@ +--- +title: Structs +id: structs +slug: /structs +sidebar_label: 12. Structs +description: Using arrays with Wing +keywords: [Wing language, example] +--- + +Structs are loosely modeled after typed JSON literals in JavaScript. + +```js playground example title="main.w" +// Define a simple structure called `Example` +struct Example { + a: str; + b: num; + c: bool?; +} + +// Define another structure called `MyData` that includes composition +struct MyData { + a: str; + b: num?; + c: Example; +} + +// Creating an instance of `MyData` with some fields initialized +let data = MyData { + a: "hello", + c: Example { + a: "world", + b: 42, + } +}; + +log(data.a); +log(data.c.a); +log(data.c.b); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +hello +world +42 +``` \ No newline at end of file From fad5a72653519b2789d1f3e20b007dacd4f1abf3 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 2 Sep 2024 13:37:02 +0100 Subject: [PATCH 04/56] adding more examples to learn by --- .../version-latest/02-hello-world.md | 6 +-- .../version-latest/03-values.md | 16 +++--- .../version-latest/04-variables.md | 33 +++++------- .../version-latest/05-for.md | 54 +++++++++---------- .../version-latest/06-ifelse.md | 50 ++++++++--------- .../version-latest/07-while.md | 17 ++---- .../version-latest/08-optionality.md | 16 ++---- .../version-latest/09-arrays.md | 29 +++++----- .../version-latest/11-sets.md | 2 +- .../version-latest/13-functions.md | 42 +++++++++++++++ 10 files changed, 132 insertions(+), 133 deletions(-) create mode 100644 example_versioned_docs/version-latest/13-functions.md diff --git a/example_versioned_docs/version-latest/02-hello-world.md b/example_versioned_docs/version-latest/02-hello-world.md index 97c81a9b9..1b785b0f8 100644 --- a/example_versioned_docs/version-latest/02-hello-world.md +++ b/example_versioned_docs/version-latest/02-hello-world.md @@ -11,11 +11,7 @@ keywords: [Wing language, example] ```js playground title="main.w" -let main = () => { - log("Hello world!"); -}; - -main(); +log("Hello world!"); ``` ```bash title="Wing console output" diff --git a/example_versioned_docs/version-latest/03-values.md b/example_versioned_docs/version-latest/03-values.md index ba1ee698a..8d1e211d7 100644 --- a/example_versioned_docs/version-latest/03-values.md +++ b/example_versioned_docs/version-latest/03-values.md @@ -15,18 +15,14 @@ Wing has various value types including strings, integers, floats, booleans, etc. - Booleans, with boolean operators as you'd expect ```js playground title="main.w" -let main = () => { - log("Hello " + "Wing"); +log("Hello " + "Wing"); - log("1+1 = {1+1}"); - log("7.0/3.0 = {7.0/3.0}"); +log("1+1 = {1+1}"); +log("7.0/3.0 = {7.0/3.0}"); - log(true && true); - log(true || false); - log(!true); -}; - -main(); +log(true && true); +log(true || false); +log(!true); ``` ```bash title="Wing console output" diff --git a/example_versioned_docs/version-latest/04-variables.md b/example_versioned_docs/version-latest/04-variables.md index a4e986500..c735aebfd 100644 --- a/example_versioned_docs/version-latest/04-variables.md +++ b/example_versioned_docs/version-latest/04-variables.md @@ -14,30 +14,25 @@ Wing has various value types including strings, integers, floats, booleans, etc. - 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); +// 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}"); +// 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 +// 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(); +// makes variable mutable +let var s = "hello"; +s = "hello world"; // compiles +log(s); ``` diff --git a/example_versioned_docs/version-latest/05-for.md b/example_versioned_docs/version-latest/05-for.md index aeaccf7a1..ac04a4eec 100644 --- a/example_versioned_docs/version-latest/05-for.md +++ b/example_versioned_docs/version-latest/05-for.md @@ -12,39 +12,33 @@ 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}"); +// 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); +} - // 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; } - - // continue the next iteration of the loop - for item in 1..10 { - if(item%2 == 0){ - continue; - } - log(item); - } - -}; - -main(); + log(item); +} ``` diff --git a/example_versioned_docs/version-latest/06-ifelse.md b/example_versioned_docs/version-latest/06-ifelse.md index 325a3d8b5..0dc7bdcb1 100644 --- a/example_versioned_docs/version-latest/06-ifelse.md +++ b/example_versioned_docs/version-latest/06-ifelse.md @@ -10,34 +10,28 @@ 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(); +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"); +} ``` diff --git a/example_versioned_docs/version-latest/07-while.md b/example_versioned_docs/version-latest/07-while.md index 224933c7a..9e1b83a1e 100644 --- a/example_versioned_docs/version-latest/07-while.md +++ b/example_versioned_docs/version-latest/07-while.md @@ -8,19 +8,12 @@ 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(); +let var i = 0; +while(i < 2){ + log("while {i}"); + i = i + 1; +} ``` ```bash title="Wing console output" diff --git a/example_versioned_docs/version-latest/08-optionality.md b/example_versioned_docs/version-latest/08-optionality.md index 34a076876..cb69e23ea 100644 --- a/example_versioned_docs/version-latest/08-optionality.md +++ b/example_versioned_docs/version-latest/08-optionality.md @@ -12,19 +12,13 @@ Nullity is a primary source of bugs in software. Being able to guarantee that a 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; - 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; - // Set next to tuesday if there is a value otherwise use monday value - let var next = tuesday ?? monday; - - log("{next}"); - -}; - -main(); +log("{next}"); ``` diff --git a/example_versioned_docs/version-latest/09-arrays.md b/example_versioned_docs/version-latest/09-arrays.md index f8ae28cbb..2357dc30d 100644 --- a/example_versioned_docs/version-latest/09-arrays.md +++ b/example_versioned_docs/version-latest/09-arrays.md @@ -12,28 +12,23 @@ 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]; - let a = MutArray[1, 2, 3]; +log("{a[0]}, {a[1]}, {a[2]}"); - log("{a[0]}, {a[1]}, {a[2]}"); +a[2] = 4; - a[2] = 4; +log("mutated value: {a[2]}"); +log("len: {a.length}"); - 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); - } - } - -} +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" diff --git a/example_versioned_docs/version-latest/11-sets.md b/example_versioned_docs/version-latest/11-sets.md index ab903a3ad..029e11be8 100644 --- a/example_versioned_docs/version-latest/11-sets.md +++ b/example_versioned_docs/version-latest/11-sets.md @@ -12,7 +12,7 @@ Sets are immutable by default `Set`, you can make them immutable using `[1, 2, 3, 3, 3]; unqiueNumbers.add(4); unqiueNumbers.delete(1); diff --git a/example_versioned_docs/version-latest/13-functions.md b/example_versioned_docs/version-latest/13-functions.md new file mode 100644 index 000000000..25ba87530 --- /dev/null +++ b/example_versioned_docs/version-latest/13-functions.md @@ -0,0 +1,42 @@ +--- +title: Functions +id: functions +slug: /functions +sidebar_label: 13. Functions +description: Using functions with Wing +keywords: [Wing language, example] +--- + +Wing supports two function types [preflight and inflight](/docs/concepts/inflights). + +- Preflight: Code that runs once, at compile time, and generates the infrastructure configuration of your cloud application. For example, setting up databases, queues, storage buckets, API endpoints, etc. + +- Inflight: Code that runs at runtime and implements your application's behavior. For example, handling API requests, processing queue messages, etc. Inflight code can be executed on various compute platforms in the cloud, such as function services (such as AWS Lambda or Azure Functions), containers (such as ECS or Kubernetes), VMs or even physical servers. + +By default functions are preflight functions. + +```js playground example title="main.w" +bring cloud; + +// preflight function - can be called at compile time +let plus = (num1: num, num2: num) => { + return num1 + num2; +}; + +// Inflight code here is run at runtime +let func = new cloud.Function(inflight (payload:Json?) => { + // When when the function it excuted on the cloud + log(Json.stringify(payload)); +}); + +let value = plus(1, 2); + +log(value); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +3 +``` \ No newline at end of file From e1c3537a89ca2b15ed60657912d3faacc07bbb72 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 2 Sep 2024 16:56:14 +0100 Subject: [PATCH 05/56] adding more examples --- .../version-latest/14-variadic-functions.md | 38 ++++++++++ .../version-latest/15-closures.md | 37 ++++++++++ .../version-latest/16-recursion.md | 27 +++++++ .../version-latest/17-methods.md | 70 +++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 example_versioned_docs/version-latest/14-variadic-functions.md create mode 100644 example_versioned_docs/version-latest/15-closures.md create mode 100644 example_versioned_docs/version-latest/16-recursion.md create mode 100644 example_versioned_docs/version-latest/17-methods.md diff --git a/example_versioned_docs/version-latest/14-variadic-functions.md b/example_versioned_docs/version-latest/14-variadic-functions.md new file mode 100644 index 000000000..67a5aade8 --- /dev/null +++ b/example_versioned_docs/version-latest/14-variadic-functions.md @@ -0,0 +1,38 @@ +--- +title: Variadic Functions +id: variadic-functions +slug: /variadic-functions +sidebar_label: 14. Variadic Functions +description: Using variadic functions with Wing +keywords: [Wing language, variadic] +--- + + +Variadic functions can be called with any number of trailing arguments. + +```js playground example title="main.w" +// Function that will take an arbitrary number of ints as arguments. +let plus = (...numbers: Array) => { + let var value = 0; + + for number in numbers{ + value = value + number; + } + + return value; +}; + +// in this example you can pass any many numbers as you want +log(plus(1, 2)); +log(plus(1, 2, 3)); +log(plus(1, 2, 3, 4)); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +3 +6 +10 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/15-closures.md b/example_versioned_docs/version-latest/15-closures.md new file mode 100644 index 000000000..7f588b5da --- /dev/null +++ b/example_versioned_docs/version-latest/15-closures.md @@ -0,0 +1,37 @@ +--- +title: Closures +id: closures +slug: /closures +sidebar_label: 15. Closures +description: Closures with Wing +keywords: [Wing language, variadic] +--- + +Closures are functions that captures variables from its surrounding lexical scope, allowing those variables to persist even after the function is executed outside its original context + + +```js playground example title="main.w" +let createCounter = (): (): num => { + let var count = 0; // This variable is part of the closure + + return () => { + count = count + 1; + return count; + }; +}; + +let counter = createCounter(); + +log(counter()); +log(counter()); +log(counter()); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +1 +2 +3 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/16-recursion.md b/example_versioned_docs/version-latest/16-recursion.md new file mode 100644 index 000000000..93355799e --- /dev/null +++ b/example_versioned_docs/version-latest/16-recursion.md @@ -0,0 +1,27 @@ +--- +title: Recursion +id: recursion +slug: /recursion +sidebar_label: 16. Recursion +description: Recursion with Wing +keywords: [Wing language, variadic] +--- + +```js playground example title="main.w" +let fact = (n: num):num => { + if(n == 0){ + return 1; + } + return n * fact(n - 1); +}; + + +log(fact(7)); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +5040 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/17-methods.md b/example_versioned_docs/version-latest/17-methods.md new file mode 100644 index 000000000..08e9a6ee8 --- /dev/null +++ b/example_versioned_docs/version-latest/17-methods.md @@ -0,0 +1,70 @@ +--- +title: Methods +id: methods +slug: /methods +sidebar_label: 17. Methods +description: Methods with Wing +keywords: [Wing language, variadic] +--- + +```js playground example title="main.w" +// Rect type +struct Rect { + width:num; + height: num; +} + +// methods accepting Rect type +let area = (r: Rect): num => { + return r.height * r.width; +}; + +// methods accepting Rect type +let perim = (r: Rect): num => { + return 2 * r.height + 2 * r.width; +}; + +let r = Rect { + height: 5, + width: 10 +}; + +log("area: {area(r)}"); +log("perim: {perim(r)}"); + +// Or Rectangle class with public methods +class Rectangle { + + height:num; + width:num; + + new (height: num, width: num){ + this.height = height; + this.width = width; + } + + pub area(): num { + return this.height * this.width; + } + + pub perim(): num { + return 2 * this.height + 2 * this.width; + } + +} + +let x = new Rectangle(5, 10); +log(x.area()); +log(x.perim()); + +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +area: 50 +perim: 30 +50 +30 +``` \ No newline at end of file From ebfdde2d69e9289983150853c1032f8ef4c5b484 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 3 Sep 2024 15:39:52 +0100 Subject: [PATCH 06/56] adding more examples --- .../version-latest/14-async-functions.md | 52 ++++++++++++++ .../version-latest/18-interfaces.md | 70 +++++++++++++++++++ .../version-latest/19-sleep.md | 49 +++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 example_versioned_docs/version-latest/14-async-functions.md create mode 100644 example_versioned_docs/version-latest/18-interfaces.md create mode 100644 example_versioned_docs/version-latest/19-sleep.md diff --git a/example_versioned_docs/version-latest/14-async-functions.md b/example_versioned_docs/version-latest/14-async-functions.md new file mode 100644 index 000000000..8f7d742bb --- /dev/null +++ b/example_versioned_docs/version-latest/14-async-functions.md @@ -0,0 +1,52 @@ +--- +title: Async inflight functions +id: async-inflight-functions +slug: /async-inflight-functions +sidebar_label: 14. Async inflight functions +description: Using functions with Wing +keywords: [Wing language, example] +--- + +Wing supports two function types [preflight and inflight](/docs/concepts/inflights). + +- Preflight: Code that runs once, at compile time, and generates the infrastructure configuration of your cloud application. For example, setting up databases, queues, storage buckets, API endpoints, etc. + +- Inflight: Code that runs at runtime and implements your application's behavior. For example, handling API requests, processing queue messages, etc. Inflight code can be executed on various compute platforms in the cloud, such as function services (such as AWS Lambda or Azure Functions), containers (such as ECS or Kubernetes), VMs or even physical servers. + +By default functions are preflight functions. + +```js playground example title="main.w" +bring cloud; +bring util; + +// defining a cloud.Function resource +let countWords = new cloud.Function(inflight (payload: Json?): Json => { + return "{payload?.tryAsStr()?.split(" ")?.length ?? 0}"; +}) as "countWords"; + +// simulate a long running task, to run async +let longTask = new cloud.Function(inflight () => { + util.sleep(30s); + log("done!"); +}); + +new cloud.Function(inflight () => { + let sentence = "I am a sentence with 7 words"; + // invoking cloud.Function from inflight context + let wordsCount = countWords.invoke(sentence); + log("'{sentence}' has {wordsCount ?? "0"} words"); + + // invokes async + longTask.invokeAsync(""); + + // continue to execute + log("task started"); +}) as "Invoke Me"; +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +3 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/18-interfaces.md b/example_versioned_docs/version-latest/18-interfaces.md new file mode 100644 index 000000000..0cb545877 --- /dev/null +++ b/example_versioned_docs/version-latest/18-interfaces.md @@ -0,0 +1,70 @@ +--- +title: Interfaces +id: interfaces +slug: /interfaces +sidebar_label: 18. Interfaces +description: Interfaces with Wing +keywords: [Wing language, interfaces] +--- + +```js playground example title="main.w" +bring math; + +interface Geometry { + area(): num; + perim(): num; +} + +class Rect impl Geometry { + width:num; + height:num; + new(width:num, height: num) { + this.width = width; + this.height = height; + } + + pub area(): num { + return this.height * this.width; + } + + pub perim(): num { + return 2 * this.height + 2 * this.width; + } +} + +class Circle impl Geometry { + radius:num; + new(radius:num) { + this.radius = radius; + } + + pub area(): num { + return math.PI * this.radius * this.radius; + } + + pub perim(): num { + return 2 * math.PI * this.radius; + } +} + + + +let r = new Rect(3, 4); +let c = new Circle(5); + +log(r.area()); +log(r.perim()); + +log(c.area()); +log(c.perim()); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +12 +14 +78.53981633974483 +31.41592653589793 +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/19-sleep.md b/example_versioned_docs/version-latest/19-sleep.md new file mode 100644 index 000000000..8569f1594 --- /dev/null +++ b/example_versioned_docs/version-latest/19-sleep.md @@ -0,0 +1,49 @@ +--- +title: Sleep +id: sleep +slug: /sleep +sidebar_label: 19. Sleep +description: Suspends execution for a given duration. +keywords: [Wing language, sleep] +--- + +`util.sleep` is an [inflight](/docs/concepts/inflights) api. + +You cannot sleep in preflight code. + +```js playground example title="main.w" +bring util; +bring cloud; + +// util.sleep has inflight api for sleep +inflight () => { + util.sleep(40s); +}; + +// example showing cloud function that sleeps +let longTask = new cloud.Function(inflight () => { + + let timer1 = () => { + log("Time 1 fired"); + util.sleep(5s); + }; + + let timer2 = () => { + log("Time 2 fired"); + util.sleep(2s); + }; + + timer1(); + timer2(); +}); + +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +# Run the cloud function using the console (to trigger the inflight function) +Time 1 fired +Time 2 fired +``` \ No newline at end of file From 4f2b115e768c63d2dea6f29c640dc44074f47a1f Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 09:40:26 +0100 Subject: [PATCH 07/56] Update example_versioned_docs/version-latest/01-introduction.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/01-introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/01-introduction.md b/example_versioned_docs/version-latest/01-introduction.md index a1c5adcb6..e55738c99 100644 --- a/example_versioned_docs/version-latest/01-introduction.md +++ b/example_versioned_docs/version-latest/01-introduction.md @@ -10,6 +10,6 @@ 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. +**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 Wing and may use new language features. Try to upgrade to the latest version if something isn't working. \ No newline at end of file From 3618a074e69fec854e2100020080548ed9f5df77 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 09:40:41 +0100 Subject: [PATCH 08/56] Update example_versioned_docs/version-latest/01-introduction.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/01-introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/01-introduction.md b/example_versioned_docs/version-latest/01-introduction.md index e55738c99..8b17e8012 100644 --- a/example_versioned_docs/version-latest/01-introduction.md +++ b/example_versioned_docs/version-latest/01-introduction.md @@ -12,4 +12,4 @@ Wing is an open source programming language for the cloud. Wing combines infrast **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 Wing and may use new language features. Try to upgrade to the latest version if something isn't working. \ No newline at end of file +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)! \ No newline at end of file From a4d47270628f5078a76b16dc206cdcbd36b3a4df Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 09:40:55 +0100 Subject: [PATCH 09/56] Update example_versioned_docs/version-latest/02-hello-world.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/02-hello-world.md | 1 + 1 file changed, 1 insertion(+) diff --git a/example_versioned_docs/version-latest/02-hello-world.md b/example_versioned_docs/version-latest/02-hello-world.md index 1b785b0f8..d2402a8de 100644 --- a/example_versioned_docs/version-latest/02-hello-world.md +++ b/example_versioned_docs/version-latest/02-hello-world.md @@ -9,6 +9,7 @@ keywords: [Wing language, example] # Hello world +By default, Wing code is executed when it's compiled. ```js playground title="main.w" log("Hello world!"); From 5e1e08b9fdaa8d2d4d569c87791899cfc6f2b112 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 09:43:24 +0100 Subject: [PATCH 10/56] Update example_versioned_docs/version-latest/03-values.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/03-values.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/example_versioned_docs/version-latest/03-values.md b/example_versioned_docs/version-latest/03-values.md index 8d1e211d7..4e9d10d3e 100644 --- a/example_versioned_docs/version-latest/03-values.md +++ b/example_versioned_docs/version-latest/03-values.md @@ -1,14 +1,13 @@ --- -title: Values -id: values -slug: /values -sidebar_label: 2. Values +title: Primitive values +id: primitives +slug: /primitive-values +sidebar_label: 2. Primitive values description: Hello world wing example -keywords: [Wing language, example] +keywords: [Wing language, example, primitives, values] --- - -Wing has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples. +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 From 125a2e9c238ee51b4817ecea8cdd63e71cf6bd9d Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 09:43:54 +0100 Subject: [PATCH 11/56] Update example_versioned_docs/version-latest/11-sets.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/11-sets.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_versioned_docs/version-latest/11-sets.md b/example_versioned_docs/version-latest/11-sets.md index 029e11be8..8ce3d7f51 100644 --- a/example_versioned_docs/version-latest/11-sets.md +++ b/example_versioned_docs/version-latest/11-sets.md @@ -7,9 +7,9 @@ description: Using sets with Wing keywords: [Wing language, example] --- -Sets are immutable by default `Set`, you can make them immutable using `>`. -Sets will store and return unique values. +A set keeps track of collection of unique values - a specific value can only be added to a set once. Sets are immutable by default `Set`, and you can make them immutable using `MutSet`. + ```js playground example title="main.w" // mutable set From da218801dc8967c162e3e21ebb3cba7b37d851f2 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 09:44:05 +0100 Subject: [PATCH 12/56] Update example_versioned_docs/version-latest/10-maps.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/10-maps.md | 1 + 1 file changed, 1 insertion(+) diff --git a/example_versioned_docs/version-latest/10-maps.md b/example_versioned_docs/version-latest/10-maps.md index 9fbcb4a7e..707c5bf77 100644 --- a/example_versioned_docs/version-latest/10-maps.md +++ b/example_versioned_docs/version-latest/10-maps.md @@ -7,6 +7,7 @@ description: Using maps with Wing keywords: [Wing language, example] --- +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 From 6fdc7dbf7a59c01865b089e5e95e321202a3360a Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 09:44:38 +0100 Subject: [PATCH 13/56] Update example_versioned_docs/version-latest/12-structs.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/12-structs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/12-structs.md b/example_versioned_docs/version-latest/12-structs.md index bf0980f72..924371d96 100644 --- a/example_versioned_docs/version-latest/12-structs.md +++ b/example_versioned_docs/version-latest/12-structs.md @@ -7,7 +7,7 @@ description: Using arrays with Wing keywords: [Wing language, example] --- -Structs are loosely modeled after typed JSON literals in JavaScript. +Structs are custom data types you can define to store structured information. They're loosely modeled after typed JSON literals in JavaScript. ```js playground example title="main.w" // Define a simple structure called `Example` From 631ace6a3c5b4e5093bc8ee3130dae0acb408018 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 13:14:25 +0100 Subject: [PATCH 14/56] updates docs --- .../13-trailing-struct-parameters.md | 44 ++++++++++ .../{13-functions.md => 14-functions.md} | 2 +- ...-functions.md => 15-variadic-functions.md} | 2 +- .../{15-closures.md => 16-closures.md} | 2 +- .../{16-recursion.md => 17-recursion.md} | 2 +- .../{17-methods.md => 18-methods.md} | 2 +- .../{18-interfaces.md => 19-interfaces.md} | 2 +- .../{19-sleep.md => 20-sleep.md} | 2 +- .../version-latest/21-string-functions.md | 44 ++++++++++ .../version-latest/22-regex.md | 56 ++++++++++++ .../version-latest/23-Json.md | 85 +++++++++++++++++++ 11 files changed, 236 insertions(+), 7 deletions(-) create mode 100644 example_versioned_docs/version-latest/13-trailing-struct-parameters.md rename example_versioned_docs/version-latest/{13-functions.md => 14-functions.md} (97%) rename example_versioned_docs/version-latest/{14-variadic-functions.md => 15-variadic-functions.md} (94%) rename example_versioned_docs/version-latest/{15-closures.md => 16-closures.md} (96%) rename example_versioned_docs/version-latest/{16-recursion.md => 17-recursion.md} (92%) rename example_versioned_docs/version-latest/{17-methods.md => 18-methods.md} (97%) rename example_versioned_docs/version-latest/{18-interfaces.md => 19-interfaces.md} (97%) rename example_versioned_docs/version-latest/{19-sleep.md => 20-sleep.md} (97%) create mode 100644 example_versioned_docs/version-latest/21-string-functions.md create mode 100644 example_versioned_docs/version-latest/22-regex.md create mode 100644 example_versioned_docs/version-latest/23-Json.md diff --git a/example_versioned_docs/version-latest/13-trailing-struct-parameters.md b/example_versioned_docs/version-latest/13-trailing-struct-parameters.md new file mode 100644 index 000000000..41591845c --- /dev/null +++ b/example_versioned_docs/version-latest/13-trailing-struct-parameters.md @@ -0,0 +1,44 @@ +--- +title: Trailing struct parameters +id: trailing-structs-parameters +slug: /trailing-structs-parameters +sidebar_label: 13. Trailing struct parameters +description: Passing fields directly to a function +keywords: [Wing language, example] +--- + +If the last parameter of a function is a struct, then you can pass its fields directly. + +```js playground example title="main.w" +// struct for the function params +struct NameOptions { + formal: bool; + caps: bool; +} + +let greet = (name: str, options: NameOptions) => { + let var prefix = "Hi, "; + if options.formal { + prefix = "Greetings, "; + } + let var message = "{prefix}{name}"; + if options.caps { + message = message.uppercase(); + } + log(message); +}; + +greet("kermit", NameOptions { formal: true, caps: false }); + +// Pass fields directly as the last param is a Struct +greet("kermit", formal: true, caps: false); + +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +Greetings, kermit +Greetings, kermit +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/13-functions.md b/example_versioned_docs/version-latest/14-functions.md similarity index 97% rename from example_versioned_docs/version-latest/13-functions.md rename to example_versioned_docs/version-latest/14-functions.md index 25ba87530..e979177d8 100644 --- a/example_versioned_docs/version-latest/13-functions.md +++ b/example_versioned_docs/version-latest/14-functions.md @@ -2,7 +2,7 @@ title: Functions id: functions slug: /functions -sidebar_label: 13. Functions +sidebar_label: 14. Functions description: Using functions with Wing keywords: [Wing language, example] --- diff --git a/example_versioned_docs/version-latest/14-variadic-functions.md b/example_versioned_docs/version-latest/15-variadic-functions.md similarity index 94% rename from example_versioned_docs/version-latest/14-variadic-functions.md rename to example_versioned_docs/version-latest/15-variadic-functions.md index 67a5aade8..b5b09709b 100644 --- a/example_versioned_docs/version-latest/14-variadic-functions.md +++ b/example_versioned_docs/version-latest/15-variadic-functions.md @@ -2,7 +2,7 @@ title: Variadic Functions id: variadic-functions slug: /variadic-functions -sidebar_label: 14. Variadic Functions +sidebar_label: 15. Variadic Functions description: Using variadic functions with Wing keywords: [Wing language, variadic] --- diff --git a/example_versioned_docs/version-latest/15-closures.md b/example_versioned_docs/version-latest/16-closures.md similarity index 96% rename from example_versioned_docs/version-latest/15-closures.md rename to example_versioned_docs/version-latest/16-closures.md index 7f588b5da..ba63c9337 100644 --- a/example_versioned_docs/version-latest/15-closures.md +++ b/example_versioned_docs/version-latest/16-closures.md @@ -2,7 +2,7 @@ title: Closures id: closures slug: /closures -sidebar_label: 15. Closures +sidebar_label: 16. Closures description: Closures with Wing keywords: [Wing language, variadic] --- diff --git a/example_versioned_docs/version-latest/16-recursion.md b/example_versioned_docs/version-latest/17-recursion.md similarity index 92% rename from example_versioned_docs/version-latest/16-recursion.md rename to example_versioned_docs/version-latest/17-recursion.md index 93355799e..79cb6d69d 100644 --- a/example_versioned_docs/version-latest/16-recursion.md +++ b/example_versioned_docs/version-latest/17-recursion.md @@ -2,7 +2,7 @@ title: Recursion id: recursion slug: /recursion -sidebar_label: 16. Recursion +sidebar_label: 17. Recursion description: Recursion with Wing keywords: [Wing language, variadic] --- diff --git a/example_versioned_docs/version-latest/17-methods.md b/example_versioned_docs/version-latest/18-methods.md similarity index 97% rename from example_versioned_docs/version-latest/17-methods.md rename to example_versioned_docs/version-latest/18-methods.md index 08e9a6ee8..7eb81f9cb 100644 --- a/example_versioned_docs/version-latest/17-methods.md +++ b/example_versioned_docs/version-latest/18-methods.md @@ -2,7 +2,7 @@ title: Methods id: methods slug: /methods -sidebar_label: 17. Methods +sidebar_label: 18. Methods description: Methods with Wing keywords: [Wing language, variadic] --- diff --git a/example_versioned_docs/version-latest/18-interfaces.md b/example_versioned_docs/version-latest/19-interfaces.md similarity index 97% rename from example_versioned_docs/version-latest/18-interfaces.md rename to example_versioned_docs/version-latest/19-interfaces.md index 0cb545877..c79918348 100644 --- a/example_versioned_docs/version-latest/18-interfaces.md +++ b/example_versioned_docs/version-latest/19-interfaces.md @@ -2,7 +2,7 @@ title: Interfaces id: interfaces slug: /interfaces -sidebar_label: 18. Interfaces +sidebar_label: 19. Interfaces description: Interfaces with Wing keywords: [Wing language, interfaces] --- diff --git a/example_versioned_docs/version-latest/19-sleep.md b/example_versioned_docs/version-latest/20-sleep.md similarity index 97% rename from example_versioned_docs/version-latest/19-sleep.md rename to example_versioned_docs/version-latest/20-sleep.md index 8569f1594..e655d7cda 100644 --- a/example_versioned_docs/version-latest/19-sleep.md +++ b/example_versioned_docs/version-latest/20-sleep.md @@ -2,7 +2,7 @@ title: Sleep id: sleep slug: /sleep -sidebar_label: 19. Sleep +sidebar_label: 20. Sleep description: Suspends execution for a given duration. keywords: [Wing language, sleep] --- diff --git a/example_versioned_docs/version-latest/21-string-functions.md b/example_versioned_docs/version-latest/21-string-functions.md new file mode 100644 index 000000000..226681fa7 --- /dev/null +++ b/example_versioned_docs/version-latest/21-string-functions.md @@ -0,0 +1,44 @@ +--- +title: String functions +id: string-functions +slug: /string-functions +sidebar_label: 21. String functions +description: Functions for string values in Wing +keywords: [Wing language, string, functions] +--- + +Wing provides many useful string-related functions. Here are some examples to give you a sense of the usage. + +```js playground example title="main.w" +log("test".contains("es")); +log("test".length); +log("test".startsWith("te")); +log("test".endsWith("st")); +log("test".indexOf("e")); + +let x = ["a", "b"]; +log(x.join("-")); + +log("foo".replace("o", "0")); +log("foo".replaceAll("o", "0")); +log(Json.stringify(("a-b-c-d-e".split("-")))); +log("TEST".lowercase()); +log("test".uppercase()); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +true +4 +true +true +1 +a-b +f0o +f00 +["a","b","c","d","e"] +test +TEST +``` \ No newline at end of file diff --git a/example_versioned_docs/version-latest/22-regex.md b/example_versioned_docs/version-latest/22-regex.md new file mode 100644 index 000000000..68b873081 --- /dev/null +++ b/example_versioned_docs/version-latest/22-regex.md @@ -0,0 +1,56 @@ +--- +title: Regular expressions +id: regex +slug: /regex +sidebar_label: 22. Regular expressions +description: Functions for string values in Wing +keywords: [Wing language, string, functions] +--- + +Wing offers built-in support for regular expressions. Here are some examples of common regexp-related tasks in Wing. + + +```js playground example title="main.w" + +let r = regex.compile("p([a-z]+)ch"); + +// Checks if the regular expression matches the provided text. +log(r.test("peach")); + +// Finds the first occurrence of the pattern within the text. +log(r.find("peach peach") ?? ""); + +// Finds all non-overlapping occurrences of the pattern within the text. +log(Json.stringify(r.findAll("peach punch pinch"))); + +// Finds the start and end index of all matches within the text. +log(Json.stringify(r.findAllIndex("peach punch pinch"))); + +// Finds the start and end index of the first match within the text. +log(Json.stringify(r.findIndex("peach"))); + +// Finds the first match and its submatches. +log(Json.stringify(r.findSubmatch("peach punch"))); + +// Finds the start and end index of the match and all submatches. +log(Json.stringify(r.findSubmatchIndex("peach punch"))); + +// Replaces all occurrences of the match with a replacement string. +log(r.replaceAll("a peach", "")); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +true +peach +["peach","punch","pinch"] +[[0,5],[6,11],[12,17]] +[0,5] +["peach","ea"] +[[0,5],[1,3]] +a +``` + + diff --git a/example_versioned_docs/version-latest/23-Json.md b/example_versioned_docs/version-latest/23-Json.md new file mode 100644 index 000000000..897f9f218 --- /dev/null +++ b/example_versioned_docs/version-latest/23-Json.md @@ -0,0 +1,85 @@ +--- +title: Json +id: json +slug: /json +sidebar_label: 23. Json +description: Create Json values in Wing +keywords: [Wing language, string, functions] +--- + +```js playground example title="main.w" +let person = Json { + firstName: "John", + lastName: "Smith" +}; + +// stringify +log(Json.stringify(person)); + +// parse +log(Json.parse("\{\"firstName\":\"John\",\"lastName\":\"Smith\"}")); + +// Try and parse +if let jsonFromTryParse = Json.tryParse("\{\"firstName\":\"John\",\"lastName\":\"Smith\"}") { + log("{jsonFromTryParse}"); +} else { + log("failed to parse string to JSON"); +} + +// Deep copy of Json +let newPerson = Json.deepCopy(person); +log(Json.stringify(person)); + +// iterate over keys +for k in Json.keys(person) { + let value = person.get(k); + log("found key {k} with value {value}"); +} + +// iterate over values +for value in Json.values(person) { + log("found value {value}"); +} + +// iterate over array +let arrayValue = Json ["a", "b", "c"]; +for v in Json.values(arrayValue) { + log(str.fromJson(v)); +} + +// Convert to structs +struct Foo { + val1: str; + val2: num; +} + +let jFoo = { + val1: "cool", + val2: 21 +}; + +let foo = Foo.fromJson(jFoo); +log(Json.stringify(foo)); +``` + +```bash title="Wing console output" +# Run locally with wing console +wing it + +{"firstName":"John","lastName":"Smith"} +{ firstName: 'John', lastName: 'Smith' } +{"firstName":"John","lastName":"Smith"} +{"firstName":"John","lastName":"Smith"} +found key firstName with value "John" +found key lastName with value "Smith" +found value "John" +found value "Smith" +a +b +c +{"val1":"cool","val2":21} +``` + + + + From 12d20a722dfe464147d904d3efd9031a34caee43 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:22:03 +0100 Subject: [PATCH 15/56] update to examples --- .../version-latest/23-Json.md | 2 +- .../version-latest/24-time.md | 51 ++++++++++++ .../version-latest/25-random.md | 30 +++++++ .../version-latest/26-number-parsing.md | 28 +++++++ .../version-latest/27-url-parsing.md | 49 ++++++++++++ .../version-latest/28-sha256.md | 25 ++++++ .../version-latest/29-base64-encoding.md | 31 ++++++++ .../30-reading-writing-files.md | 44 +++++++++++ .../version-latest/31-directories.md | 42 ++++++++++ .../version-latest/32-testing.md | 44 +++++++++++ .../version-latest/33-http-client.md | 45 +++++++++++ .../version-latest/34-http-server.md | 79 +++++++++++++++++++ .../version-latest/35-exec-processes.md | 38 +++++++++ .../08-guides/04-private-api-gateway-aws.md | 6 +- 14 files changed, 510 insertions(+), 4 deletions(-) create mode 100644 example_versioned_docs/version-latest/24-time.md create mode 100644 example_versioned_docs/version-latest/25-random.md create mode 100644 example_versioned_docs/version-latest/26-number-parsing.md create mode 100644 example_versioned_docs/version-latest/27-url-parsing.md create mode 100644 example_versioned_docs/version-latest/28-sha256.md create mode 100644 example_versioned_docs/version-latest/29-base64-encoding.md create mode 100644 example_versioned_docs/version-latest/30-reading-writing-files.md create mode 100644 example_versioned_docs/version-latest/31-directories.md create mode 100644 example_versioned_docs/version-latest/32-testing.md create mode 100644 example_versioned_docs/version-latest/33-http-client.md create mode 100644 example_versioned_docs/version-latest/34-http-server.md create mode 100644 example_versioned_docs/version-latest/35-exec-processes.md diff --git a/example_versioned_docs/version-latest/23-Json.md b/example_versioned_docs/version-latest/23-Json.md index 897f9f218..4594de823 100644 --- a/example_versioned_docs/version-latest/23-Json.md +++ b/example_versioned_docs/version-latest/23-Json.md @@ -4,7 +4,7 @@ id: json slug: /json sidebar_label: 23. Json description: Create Json values in Wing -keywords: [Wing language, string, functions] +keywords: [Wing language, json] --- ```js playground example title="main.w" diff --git a/example_versioned_docs/version-latest/24-time.md b/example_versioned_docs/version-latest/24-time.md new file mode 100644 index 000000000..af1a8c2dc --- /dev/null +++ b/example_versioned_docs/version-latest/24-time.md @@ -0,0 +1,51 @@ +--- +title: Time +id: time +slug: /time +sidebar_label: 24. Time +description: Create time/date values in Wing +keywords: [Wing language, time, date] +--- + +```js playground example title="main.w" +let now = datetime.utcNow(); + +log(now.toIso()); + +let then = datetime.fromIso("2020-09-09"); +log(then.toIso()); + +log(then.year); +log(then.month); +log(then.dayOfWeek); +log(then.dayOfMonth); +log(then.hours); +log(then.min); +log(then.sec); +log(then.ms); + +log(then.timestamp); +log(then.timestampMs); +log(then.timezone); +``` + +```bash title="Wing console output" +# Run locally with wing console +2024-09-09T12:25:01.080Z +2020-09-09T00:00:00.000Z +2020 +8 +3 +9 +0 +0 +0 +0 +1599609600 +1599609600000 +0 +``` + + + + diff --git a/example_versioned_docs/version-latest/25-random.md b/example_versioned_docs/version-latest/25-random.md new file mode 100644 index 000000000..7879335c9 --- /dev/null +++ b/example_versioned_docs/version-latest/25-random.md @@ -0,0 +1,30 @@ +--- +title: Random +id: random +slug: /random +sidebar_label: 25. Random +description: Create random values in Wing +keywords: [Wing language, random] +--- + +```js playground example title="main.w" +bring math; + +log(math.random(100)); + +log(math.ceil(math.random(100))); + +log((math.random()*5)+5); + +``` + +```bash title="Wing console output" +# Run locally with wing console, output will vary +46.58171364582826 +4 +5.721934646951212 +``` + + + + diff --git a/example_versioned_docs/version-latest/26-number-parsing.md b/example_versioned_docs/version-latest/26-number-parsing.md new file mode 100644 index 000000000..1fc55a936 --- /dev/null +++ b/example_versioned_docs/version-latest/26-number-parsing.md @@ -0,0 +1,28 @@ +--- +title: Number parsing +id: number-parsing +slug: /number-parsing +sidebar_label: 26. Number Parsing +description: Parse values into numbers +keywords: [Wing language, random] +--- + +```js playground example title="main.w" +let j = Json { a: 100 }; + +let x:num = num.fromStr("1"); +let y:num = num.fromJson(j.get("a")); + +log(x); +log(y); +``` + +```bash title="Wing console output" +# Run locally with wing console +1 +100 +``` + + + + diff --git a/example_versioned_docs/version-latest/27-url-parsing.md b/example_versioned_docs/version-latest/27-url-parsing.md new file mode 100644 index 000000000..c9c118ad4 --- /dev/null +++ b/example_versioned_docs/version-latest/27-url-parsing.md @@ -0,0 +1,49 @@ +--- +title: URL parsing +id: url-parsing +slug: /url-parsing +sidebar_label: 27. URL parsing +description: Parse urls in Wing +keywords: [Wing language, URL] +--- + +Parse urls using the http module, works with inflight closures. + +```js playground example title="main.w" +bring http; +bring cloud; + +new cloud.Function(inflight () => { + let parts = http.parseUrl("postgres://user:pass@host.com:5432/path?k=v#f"); + + log(parts.hostname); + log(parts.host); + log(parts.hash); + log(parts.origin); + log(parts.username); + log(parts.password); + log(parts.pathname); + log(parts.port); + log(parts.protocol); + log(parts.search); + +}); +``` + +```bash title="Wing console output" +# Run locally with wing console and invoke the cloud function +host.com +host.com:5432 +#f +null +user +pass +/path +5432 +postgres: +?k=v +``` + + + + diff --git a/example_versioned_docs/version-latest/28-sha256.md b/example_versioned_docs/version-latest/28-sha256.md new file mode 100644 index 000000000..1dd9194eb --- /dev/null +++ b/example_versioned_docs/version-latest/28-sha256.md @@ -0,0 +1,25 @@ +--- +title: SHA256 Hashes +id: sha256 +slug: /sha256 +sidebar_label: 28. SHA256 Hashes +description: Hash values in Wing with SHA256 +keywords: [Wing language, Hash, SHA256] +--- + +```js playground example title="main.w" +bring util; + +let value = util.sha256("sha256 this string"); + +log(value); +``` + +```bash title="Wing console output" +# Run locally with wing console +1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a0d3db739d77aacb +``` + + + + diff --git a/example_versioned_docs/version-latest/29-base64-encoding.md b/example_versioned_docs/version-latest/29-base64-encoding.md new file mode 100644 index 000000000..4eaaa9597 --- /dev/null +++ b/example_versioned_docs/version-latest/29-base64-encoding.md @@ -0,0 +1,31 @@ +--- +title: Base64 Encoding +id: base64-encoding +slug: /base64-encoding +sidebar_label: 29. Base64 Encoding +description: Encode and decode Base64 values +keywords: [Wing language, Base64] +--- + +```js playground example title="main.w" +bring util; + +let data = "abc123!?$*&()'-=@~"; + +let encoded = util.base64Encode(data); +log(encoded); + + +let decoded = util.base64Decode(encoded); +log(decoded); +``` + +```bash title="Wing console output" +# Run locally with wing console +YWJjMTIzIT8kKiYoKSctPUB+ +abc123!?$*&()'-=@~ +``` + + + + diff --git a/example_versioned_docs/version-latest/30-reading-writing-files.md b/example_versioned_docs/version-latest/30-reading-writing-files.md new file mode 100644 index 000000000..701795c54 --- /dev/null +++ b/example_versioned_docs/version-latest/30-reading-writing-files.md @@ -0,0 +1,44 @@ +--- +title: Reading and writing files +id: reading-and-writing-files +slug: /reading-and-writing-files +sidebar_label: 30. Reading and writing files +description: Reading and writing files with Wing +keywords: [Wing language, Reading files, Writing files] +--- + +```js playground example title="main.w" +bring fs; + +let filename:str = "/tmp/test.txt"; + +log(fs.exists(filename)); + +fs.writeFile(filename, "hello world!"); +fs.exists(filename); + +let file = fs.readFile(filename); + +log(file); +log(fs.extension(filename) ?? ""); +log(fs.isDir(filename)); + +fs.appendFile(filename, "testing"); + +let extendedValue = fs.readFile(filename); + +log(extendedValue); +``` + +```bash title="Wing console output" +# Run locally with wing console +true +hello world! +txt +false +hello world!testing +``` + + + + diff --git a/example_versioned_docs/version-latest/31-directories.md b/example_versioned_docs/version-latest/31-directories.md new file mode 100644 index 000000000..76073124e --- /dev/null +++ b/example_versioned_docs/version-latest/31-directories.md @@ -0,0 +1,42 @@ +--- +title: Directories +id: directories +slug: /directories +sidebar_label: 31. Directories +description: Directories +keywords: [Wing language, Directories] +--- + +Use the fs module to make, read, check, remove directories with Wing. + +```js playground example title="main.w" +bring fs; + +// Make directory +fs.mkdir("subdir"); + +// Check if path is directory +fs.isDir("subdir"); + +// Set permissions on directory +fs.setPermissions("subdir", "0755"); + +// Try and parse +if let dirTryFrom = fs.tryReaddir("random-folder") { + log("Directory is there"); +} else { + log("No directory found"); +} + +// Remove a directory +fs.remove("subdir"); +``` + +```bash title="Wing console output" +# Run locally with wing console +No directory found +``` + + + + diff --git a/example_versioned_docs/version-latest/32-testing.md b/example_versioned_docs/version-latest/32-testing.md new file mode 100644 index 000000000..e1be89b08 --- /dev/null +++ b/example_versioned_docs/version-latest/32-testing.md @@ -0,0 +1,44 @@ +--- +title: Testing +id: testing +slug: /testing +sidebar_label: 32. Testing +description: Directories +keywords: [Wing language, Directories] +--- + +Wing incorporates a [lightweight testing framework](/docs/concepts/tests), which is built around the wing test command and the test keyword. + +```js playground example title="main.w" +bring math; +bring cloud; +let b = new cloud.Bucket(); + +test "abs" { + assert(1 == math.abs(-1)); +} + +test "bucket list should include created file" { + b.put("file", "lorem ipsum"); + let listOfFile = b.list(); + assert(listOfFile.length == 1); +} + +test "bucket starts empty" { + let listOfFile = b.list(); + assert(listOfFile.length == 0); +} + +test "this test fails" { + throw("test throws an exception fails"); +} +``` + +```bash title="Wing console output" +# Run locally with wing console +No directory found +``` + + + + diff --git a/example_versioned_docs/version-latest/33-http-client.md b/example_versioned_docs/version-latest/33-http-client.md new file mode 100644 index 000000000..5d94c89d0 --- /dev/null +++ b/example_versioned_docs/version-latest/33-http-client.md @@ -0,0 +1,45 @@ +--- +title: HTTP Client +id: http-client +slug: /http-client +sidebar_label: 33. HTTP Client +description: Directories +keywords: [Wing language, HTTP] +--- + +The Wing standard library comes with [HTTP support](/docs/api/standard-library/http/api-reference). + +In this example we’ll use it to issue simple HTTP request. + +```js playground example title="main.w" +bring http; +bring cloud; + +struct Pokemon { + id: str; + name: str; + order: num; + weight: num; +} + +new cloud.Function(inflight () => { + let x = http.get("https://pokeapi.co/api/v2/pokemon/ditto"); + + // response status + log(x.status); + + // Cast response back into struct + let ditto = Pokemon.fromJson(x.body); + log(ditto.name); + +}); +``` + +```bash title="Wing console output" +# Run locally with wing console +No directory found +``` + + + + diff --git a/example_versioned_docs/version-latest/34-http-server.md b/example_versioned_docs/version-latest/34-http-server.md new file mode 100644 index 000000000..45683dcb6 --- /dev/null +++ b/example_versioned_docs/version-latest/34-http-server.md @@ -0,0 +1,79 @@ +--- +title: HTTP Server +id: http-server +slug: /http-server +sidebar_label: 33. HTTP Server +description: Directories +keywords: [Wing language, HTTP] +--- + +You can create HTTP servers using the [cloud.Api](/docs/api/standard-library/cloud/api) standard library. + +This example shows an example of using an API with a GET and PUT endpoint that interacts with a bucket. + + +```js playground example title="main.w" +bring cloud; +bring http; + +let noteBucket = new cloud.Bucket(); + +let api = new cloud.Api(); + +api.get("/note", inflight (request) => { + let noteName = request.query.get("name"); + let note = noteBucket.get(noteName); + + return { + status: 200, + body: note + }; +}); + +api.put("/note/:name", inflight (request) => { + let note = request.body; + let noteName = request.vars.get("name"); + + if (note == "") { + return { + status: 400, + body: "note is required" + }; + } + + noteBucket.put(noteName, note ?? ""); + + return { + status: 200, + body: "note: {noteName} saved!" + }; +}); + +// Consumer functions (not required for the app to work, but useful for testing) +new cloud.Function(inflight (event: Json?) => { + if let event = event { + let parts = event.asStr().split(":"); + let name = parts.at(0); + let note = parts.at(1); + + let response = http.put("{api.url}/note/{name}", { + body: "{note}" + }); + return response.body; + } + + return "event is required `NAME:NOTE`"; +}) as "Consumer-PUT"; + +new cloud.Function(inflight (event: Json?) => { + if let event = event { + return http.get("{api.url}/note?name={event}").body; + } + + return "event is required `NAME`"; +}) as "Consumer-GET"; +``` + + + + diff --git a/example_versioned_docs/version-latest/35-exec-processes.md b/example_versioned_docs/version-latest/35-exec-processes.md new file mode 100644 index 000000000..ea6d9bedb --- /dev/null +++ b/example_versioned_docs/version-latest/35-exec-processes.md @@ -0,0 +1,38 @@ +--- +title: Exec processes +id: exec-processed +slug: /exec-processed +sidebar_label: 35. Exec processes +description: Exec'ing Processes +keywords: [Wing language, HTTP] +--- + +```js playground example title="main.w" +bring util; + +let output = util.exec("echo", ["-n", "Hello, Wing!"]); + +// exec with custom environment variables +let output2 = util.exec("bash", ["--norc", "--noprofile", "-c", "echo $WING_TARGET $ENV_VAR"], { env: { ENV_VAR: "Wing" } }); + +// exec with inherited environment variables +let output3 = util.exec("bash", ["--norc", "--noprofile", "-c", "echo $WING_TARGET $ENV_VAR"], { inheritEnv: true }); + +// exec with current working directory +let output4 = util.exec("bash", ["--norc", "--noprofile", "-c", "echo Hello"], { cwd: "/tmp" }); + +log(output); +log(output2); +log(output3); +log(output4); +``` + +```bash title="Wing console output" +# Run locally with wing console +Hello, Wing! +Wing +sim +Hello +``` + + diff --git a/versioned_docs/version-latest/08-guides/04-private-api-gateway-aws.md b/versioned_docs/version-latest/08-guides/04-private-api-gateway-aws.md index 7dca7c44f..8eeec6cb4 100644 --- a/versioned_docs/version-latest/08-guides/04-private-api-gateway-aws.md +++ b/versioned_docs/version-latest/08-guides/04-private-api-gateway-aws.md @@ -117,9 +117,9 @@ api.put("/note/:name", inflight (request) => { }); // Consumer functions (not required for the app to work, but useful for testing) -new cloud.Function(inflight (event: str?) => { +new cloud.Function(inflight (event: Json?) => { if let event = event { - let parts = event.split(":"); + let parts = event.asStr().split(":"); let name = parts.at(0); let note = parts.at(1); @@ -132,7 +132,7 @@ new cloud.Function(inflight (event: str?) => { return "event is required `NAME:NOTE`"; }) as "Consumer-PUT"; -new cloud.Function(inflight (event: str?) => { +new cloud.Function(inflight (event: Json?) => { if let event = event { return http.get("{api.url}/note?name={event}").body; } From c9991756a39b4a3f44f428f9ea3b600927805cbd Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:22:42 +0100 Subject: [PATCH 16/56] Update example_versioned_docs/version-latest/11-sets.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/11-sets.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/11-sets.md b/example_versioned_docs/version-latest/11-sets.md index 8ce3d7f51..3d11eb0e7 100644 --- a/example_versioned_docs/version-latest/11-sets.md +++ b/example_versioned_docs/version-latest/11-sets.md @@ -17,7 +17,7 @@ let unqiueNumbers = MutSet[1, 2, 3, 3, 3]; unqiueNumbers.add(4); unqiueNumbers.delete(1); -// immutable set, will make values unique +// immutable set, values cannot be added or removed let uniqueStrings = Set["unique", "values", "values"]; From 8508a7d7e0145f6e7161a2c8a3409954b6fb55c8 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:22:50 +0100 Subject: [PATCH 17/56] Update example_versioned_docs/version-latest/12-structs.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/12-structs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/12-structs.md b/example_versioned_docs/version-latest/12-structs.md index 924371d96..70d98a8f7 100644 --- a/example_versioned_docs/version-latest/12-structs.md +++ b/example_versioned_docs/version-latest/12-structs.md @@ -27,7 +27,7 @@ struct MyData { // Creating an instance of `MyData` with some fields initialized let data = MyData { a: "hello", - c: Example { + c: { a: "world", b: 42, } From 93e57a3f4a8b57ca1e780c7ad78d50cc2e794fbc Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:22:56 +0100 Subject: [PATCH 18/56] Update example_versioned_docs/version-latest/14-async-functions.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/14-async-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/14-async-functions.md b/example_versioned_docs/version-latest/14-async-functions.md index 8f7d742bb..7b342c058 100644 --- a/example_versioned_docs/version-latest/14-async-functions.md +++ b/example_versioned_docs/version-latest/14-async-functions.md @@ -7,7 +7,7 @@ description: Using functions with Wing keywords: [Wing language, example] --- -Wing supports two function types [preflight and inflight](/docs/concepts/inflights). +Wing supports two function types, [preflight and inflight](/docs/concepts/inflights). - Preflight: Code that runs once, at compile time, and generates the infrastructure configuration of your cloud application. For example, setting up databases, queues, storage buckets, API endpoints, etc. From 91c1e35f02d77c77c8278cae734be8b017b0dc2e Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:23:11 +0100 Subject: [PATCH 19/56] Update example_versioned_docs/version-latest/14-async-functions.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/14-async-functions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example_versioned_docs/version-latest/14-async-functions.md b/example_versioned_docs/version-latest/14-async-functions.md index 7b342c058..02eee4bd8 100644 --- a/example_versioned_docs/version-latest/14-async-functions.md +++ b/example_versioned_docs/version-latest/14-async-functions.md @@ -1,7 +1,7 @@ --- -title: Async inflight functions -id: async-inflight-functions -slug: /async-inflight-functions +title: Inflight functions +id: inflight-functions +slug: /inflight-functions sidebar_label: 14. Async inflight functions description: Using functions with Wing keywords: [Wing language, example] From ce79fe67ffaf98b584121474a08673770891598f Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:23:20 +0100 Subject: [PATCH 20/56] Update example_versioned_docs/version-latest/14-async-functions.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/14-async-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/14-async-functions.md b/example_versioned_docs/version-latest/14-async-functions.md index 02eee4bd8..99d750bc5 100644 --- a/example_versioned_docs/version-latest/14-async-functions.md +++ b/example_versioned_docs/version-latest/14-async-functions.md @@ -13,7 +13,7 @@ Wing supports two function types, [preflight and inflight](/docs/concepts/inflig - Inflight: Code that runs at runtime and implements your application's behavior. For example, handling API requests, processing queue messages, etc. Inflight code can be executed on various compute platforms in the cloud, such as function services (such as AWS Lambda or Azure Functions), containers (such as ECS or Kubernetes), VMs or even physical servers. -By default functions are preflight functions. +By default, most functions are preflight. A function is inflight if it has the `inflight` keyword or if the function is defined inside of another inflight function. ```js playground example title="main.w" bring cloud; From 22b0fbca31757e803c0e75e1aaef5daaec95bd15 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:23:29 +0100 Subject: [PATCH 21/56] Update example_versioned_docs/version-latest/17-recursion.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/17-recursion.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_versioned_docs/version-latest/17-recursion.md b/example_versioned_docs/version-latest/17-recursion.md index 79cb6d69d..fee43024e 100644 --- a/example_versioned_docs/version-latest/17-recursion.md +++ b/example_versioned_docs/version-latest/17-recursion.md @@ -8,8 +8,8 @@ keywords: [Wing language, variadic] --- ```js playground example title="main.w" -let fact = (n: num):num => { - if(n == 0){ +let fact = (n: num): num => { + if n == 0 { return 1; } return n * fact(n - 1); From be174e6acb787183d1f3385e8f9d107d9cf3b951 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:23:37 +0100 Subject: [PATCH 22/56] Update example_versioned_docs/version-latest/18-methods.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/18-methods.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/example_versioned_docs/version-latest/18-methods.md b/example_versioned_docs/version-latest/18-methods.md index 7eb81f9cb..92cabc88a 100644 --- a/example_versioned_docs/version-latest/18-methods.md +++ b/example_versioned_docs/version-latest/18-methods.md @@ -34,11 +34,10 @@ log("perim: {perim(r)}"); // Or Rectangle class with public methods class Rectangle { + height: num; + width: num; - height:num; - width:num; - - new (height: num, width: num){ + new(height: num, width: num) { this.height = height; this.width = width; } From 7788d15b80ecc866d65bc6aab245e93bbdc1452f Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:23:43 +0100 Subject: [PATCH 23/56] Update example_versioned_docs/version-latest/19-interfaces.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/19-interfaces.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example_versioned_docs/version-latest/19-interfaces.md b/example_versioned_docs/version-latest/19-interfaces.md index c79918348..7a0e1857c 100644 --- a/example_versioned_docs/version-latest/19-interfaces.md +++ b/example_versioned_docs/version-latest/19-interfaces.md @@ -16,9 +16,9 @@ interface Geometry { } class Rect impl Geometry { - width:num; - height:num; - new(width:num, height: num) { + width: num; + height: num; + new(width: num, height: num) { this.width = width; this.height = height; } From 5810ace1350ae81a13aca02b4fc13edcefb81de8 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Mon, 9 Sep 2024 17:23:49 +0100 Subject: [PATCH 24/56] Update example_versioned_docs/version-latest/19-interfaces.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/19-interfaces.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_versioned_docs/version-latest/19-interfaces.md b/example_versioned_docs/version-latest/19-interfaces.md index 7a0e1857c..5787432be 100644 --- a/example_versioned_docs/version-latest/19-interfaces.md +++ b/example_versioned_docs/version-latest/19-interfaces.md @@ -33,8 +33,8 @@ class Rect impl Geometry { } class Circle impl Geometry { - radius:num; - new(radius:num) { + radius: num; + new(radius: num) { this.radius = radius; } From ba4ba10ff510b8b267602b58a27b3fcd36cfe8c1 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:10:06 +0100 Subject: [PATCH 25/56] Update example_versioned_docs/version-latest/18-methods.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/18-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/18-methods.md b/example_versioned_docs/version-latest/18-methods.md index 92cabc88a..ccf4aec75 100644 --- a/example_versioned_docs/version-latest/18-methods.md +++ b/example_versioned_docs/version-latest/18-methods.md @@ -10,7 +10,7 @@ keywords: [Wing language, variadic] ```js playground example title="main.w" // Rect type struct Rect { - width:num; + width: num; height: num; } From ecf53d7da9e96c7347f7e5ce44beae4ed0063702 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:10:31 +0100 Subject: [PATCH 26/56] Update example_versioned_docs/version-latest/23-Json.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/23-Json.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example_versioned_docs/version-latest/23-Json.md b/example_versioned_docs/version-latest/23-Json.md index 4594de823..936d8207e 100644 --- a/example_versioned_docs/version-latest/23-Json.md +++ b/example_versioned_docs/version-latest/23-Json.md @@ -7,6 +7,8 @@ description: Create Json values in Wing keywords: [Wing language, json] --- +Wing has a dedicated type named `Json` for representing [JSON](https://www.json.org/json-en.html). A `Json` value can be an object, but it can also be an array, string, boolean, number, or null. + ```js playground example title="main.w" let person = Json { firstName: "John", From be28f1e0d62925998c99f6bb349f492167f3976a Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:11:53 +0100 Subject: [PATCH 27/56] Update example_versioned_docs/version-latest/26-number-parsing.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/26-number-parsing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_versioned_docs/version-latest/26-number-parsing.md b/example_versioned_docs/version-latest/26-number-parsing.md index 1fc55a936..2b0501d36 100644 --- a/example_versioned_docs/version-latest/26-number-parsing.md +++ b/example_versioned_docs/version-latest/26-number-parsing.md @@ -10,8 +10,8 @@ keywords: [Wing language, random] ```js playground example title="main.w" let j = Json { a: 100 }; -let x:num = num.fromStr("1"); -let y:num = num.fromJson(j.get("a")); +let x: num = num.fromStr("1"); +let y: num = num.fromJson(j.get("a")); log(x); log(y); From f4d0101a2dfee06354dbbed3439ab175b8b3c618 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:19:40 +0100 Subject: [PATCH 28/56] Update example_versioned_docs/version-latest/30-reading-writing-files.md Co-authored-by: Chris Rybicki --- .../version-latest/30-reading-writing-files.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_versioned_docs/version-latest/30-reading-writing-files.md b/example_versioned_docs/version-latest/30-reading-writing-files.md index 701795c54..b8da32cbc 100644 --- a/example_versioned_docs/version-latest/30-reading-writing-files.md +++ b/example_versioned_docs/version-latest/30-reading-writing-files.md @@ -10,14 +10,14 @@ keywords: [Wing language, Reading files, Writing files] ```js playground example title="main.w" bring fs; -let filename:str = "/tmp/test.txt"; +let filename: str = "/tmp/test.txt"; log(fs.exists(filename)); fs.writeFile(filename, "hello world!"); fs.exists(filename); -let file = fs.readFile(filename); +let file: str = fs.readFile(filename); log(file); log(fs.extension(filename) ?? ""); From 87916f598ec2d19840810162b4085baa3ceda630 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:19:51 +0100 Subject: [PATCH 29/56] Update example_versioned_docs/version-latest/32-testing.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/32-testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/32-testing.md b/example_versioned_docs/version-latest/32-testing.md index e1be89b08..d53369d97 100644 --- a/example_versioned_docs/version-latest/32-testing.md +++ b/example_versioned_docs/version-latest/32-testing.md @@ -7,7 +7,7 @@ description: Directories keywords: [Wing language, Directories] --- -Wing incorporates a [lightweight testing framework](/docs/concepts/tests), which is built around the wing test command and the test keyword. +Wing incorporates a [lightweight testing framework](/docs/concepts/tests), which is built around the `wing test` command and the `test` keyword. ```js playground example title="main.w" bring math; From 6cfc403d0cd92541ec9a7723f4d0114e7aa0c57d Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:19:58 +0100 Subject: [PATCH 30/56] Update example_versioned_docs/version-latest/33-http-client.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/33-http-client.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/33-http-client.md b/example_versioned_docs/version-latest/33-http-client.md index 5d94c89d0..aaa700139 100644 --- a/example_versioned_docs/version-latest/33-http-client.md +++ b/example_versioned_docs/version-latest/33-http-client.md @@ -9,7 +9,7 @@ keywords: [Wing language, HTTP] The Wing standard library comes with [HTTP support](/docs/api/standard-library/http/api-reference). -In this example we’ll use it to issue simple HTTP request. +In this example we’ll use it to issue a simple HTTP request. ```js playground example title="main.w" bring http; From 61b6a6afd5a428409626aea3353703be9a6c7467 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:20:11 +0100 Subject: [PATCH 31/56] Update example_versioned_docs/version-latest/34-http-server.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/34-http-server.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/example_versioned_docs/version-latest/34-http-server.md b/example_versioned_docs/version-latest/34-http-server.md index 45683dcb6..25557d53a 100644 --- a/example_versioned_docs/version-latest/34-http-server.md +++ b/example_versioned_docs/version-latest/34-http-server.md @@ -42,11 +42,7 @@ api.put("/note/:name", inflight (request) => { } noteBucket.put(noteName, note ?? ""); - - return { - status: 200, - body: "note: {noteName} saved!" - }; + // handler implicitly returns `status: 200` by default }); // Consumer functions (not required for the app to work, but useful for testing) From d687ef3929b4936d3875237c2a949e24cec288f7 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:20:22 +0100 Subject: [PATCH 32/56] Update example_versioned_docs/version-latest/31-directories.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/31-directories.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/31-directories.md b/example_versioned_docs/version-latest/31-directories.md index 76073124e..f6a795a10 100644 --- a/example_versioned_docs/version-latest/31-directories.md +++ b/example_versioned_docs/version-latest/31-directories.md @@ -7,7 +7,7 @@ description: Directories keywords: [Wing language, Directories] --- -Use the fs module to make, read, check, remove directories with Wing. +Use the `fs` ("filesystem") module to make, read, check, remove directories. ```js playground example title="main.w" bring fs; From e062ca379fc86d2b2edef92ebd82f2cbb0b1423e Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 10 Sep 2024 10:27:09 +0100 Subject: [PATCH 33/56] added some more docs --- example_versioned_docs/version-latest/21-string-functions.md | 2 +- example_versioned_docs/version-latest/25-random.md | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/21-string-functions.md b/example_versioned_docs/version-latest/21-string-functions.md index 226681fa7..0b836bd3d 100644 --- a/example_versioned_docs/version-latest/21-string-functions.md +++ b/example_versioned_docs/version-latest/21-string-functions.md @@ -7,7 +7,7 @@ description: Functions for string values in Wing keywords: [Wing language, string, functions] --- -Wing provides many useful string-related functions. Here are some examples to give you a sense of the usage. +Wing provides many useful [string-related functions](/docs/api/standard-library/std/string#string-). Here are some examples to give you a sense of the usage. ```js playground example title="main.w" log("test".contains("es")); diff --git a/example_versioned_docs/version-latest/25-random.md b/example_versioned_docs/version-latest/25-random.md index 7879335c9..87e997b59 100644 --- a/example_versioned_docs/version-latest/25-random.md +++ b/example_versioned_docs/version-latest/25-random.md @@ -7,6 +7,11 @@ description: Create random values in Wing keywords: [Wing language, random] --- +Using the [math standard library](/docs/api/standard-library/math/api-reference) you can generate random numbers. + +_Note: The random numbers that are generated are not random enough for sensitive security tasks, such as generating encryption keys, tokens for authentication, or anything requiring unpredictable randomness for security purposes._ + + ```js playground example title="main.w" bring math; From 3fdb1f87a1685d0f119784ca8d82403defb308a3 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:11:40 +0100 Subject: [PATCH 34/56] Update example_versioned_docs/version-latest/04-variables.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/04-variables.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/example_versioned_docs/version-latest/04-variables.md b/example_versioned_docs/version-latest/04-variables.md index c735aebfd..72929da5f 100644 --- a/example_versioned_docs/version-latest/04-variables.md +++ b/example_versioned_docs/version-latest/04-variables.md @@ -7,11 +7,9 @@ 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. +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. -- Strings, which can be added together with + -- Integers and floats -- Booleans, with boolean operators as you'd expect +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" From 337897e3c500878262b5993dbcf457d6deb10492 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:12:12 +0100 Subject: [PATCH 35/56] Update example_versioned_docs/version-latest/04-variables.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/04-variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/04-variables.md b/example_versioned_docs/version-latest/04-variables.md index 72929da5f..79fd6d8b9 100644 --- a/example_versioned_docs/version-latest/04-variables.md +++ b/example_versioned_docs/version-latest/04-variables.md @@ -27,7 +27,7 @@ log("{b}, {c}"); let d: str = "Hello"; // d = "Test"; // error: Variable is not reassignable -// makes variable mutable +// makes variable reassignable let var s = "hello"; s = "hello world"; // compiles log(s); From f8cf6b13c010d23f953b0f9778636b26f5f835e9 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:12:22 +0100 Subject: [PATCH 36/56] Update example_versioned_docs/version-latest/05-for.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/05-for.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/05-for.md b/example_versioned_docs/version-latest/05-for.md index ac04a4eec..db2e03929 100644 --- a/example_versioned_docs/version-latest/05-for.md +++ b/example_versioned_docs/version-latest/05-for.md @@ -7,7 +7,7 @@ description: Using for loops with Wing keywords: [Wing language, example] --- -Wing supports for..in statements. +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. The loop invariant in for loops is implicitly re-assignable (var). From b0147ed84bd664e33d9fbf225f806eedb77f073b Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:12:33 +0100 Subject: [PATCH 37/56] Update example_versioned_docs/version-latest/05-for.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/05-for.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/05-for.md b/example_versioned_docs/version-latest/05-for.md index db2e03929..3bd549bae 100644 --- a/example_versioned_docs/version-latest/05-for.md +++ b/example_versioned_docs/version-latest/05-for.md @@ -9,7 +9,7 @@ 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. The loop invariant in for loops is implicitly re-assignable (var). +[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 From a54d86fba00172bb55022b1690259d7e66d53a9f Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:12:41 +0100 Subject: [PATCH 38/56] Update example_versioned_docs/version-latest/05-for.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/05-for.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/05-for.md b/example_versioned_docs/version-latest/05-for.md index 3bd549bae..f28cffd94 100644 --- a/example_versioned_docs/version-latest/05-for.md +++ b/example_versioned_docs/version-latest/05-for.md @@ -20,7 +20,7 @@ for item in 1..3 { // for-in with arrays let arr = [1, 2, 3]; for item in arr { - log("{item}"); + log(item); } // break a loop From 407ff9e8c688a9302ee6a6ba368e63a3875cc042 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:12:50 +0100 Subject: [PATCH 39/56] Update example_versioned_docs/version-latest/05-for.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/05-for.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/05-for.md b/example_versioned_docs/version-latest/05-for.md index f28cffd94..7ef82714a 100644 --- a/example_versioned_docs/version-latest/05-for.md +++ b/example_versioned_docs/version-latest/05-for.md @@ -26,7 +26,7 @@ for item in arr { // break a loop let items = Set[1, 2, 3]; for item in items { - if(item == 1){ + if item == 1 { break; } log(item); From b19ca0fb0dac0b43171c2e813b62c352aa8d15a5 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:12:58 +0100 Subject: [PATCH 40/56] Update example_versioned_docs/version-latest/05-for.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/05-for.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/05-for.md b/example_versioned_docs/version-latest/05-for.md index 7ef82714a..041f8d85d 100644 --- a/example_versioned_docs/version-latest/05-for.md +++ b/example_versioned_docs/version-latest/05-for.md @@ -34,7 +34,7 @@ for item in items { // continue the next iteration of the loop for item in 1..10 { - if(item%2 == 0){ + if item % 2 == 0 { continue; } log(item); From 93a62226bb3dd6a9abefce2eddc3aeecc497592b Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:13:07 +0100 Subject: [PATCH 41/56] Update example_versioned_docs/version-latest/06-ifelse.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/06-ifelse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/06-ifelse.md b/example_versioned_docs/version-latest/06-ifelse.md index 0dc7bdcb1..308ddea28 100644 --- a/example_versioned_docs/version-latest/06-ifelse.md +++ b/example_versioned_docs/version-latest/06-ifelse.md @@ -7,7 +7,7 @@ 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. +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) { From e6b762dc5030fad52acc56488376bee7defb9e26 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:13:15 +0100 Subject: [PATCH 42/56] Update example_versioned_docs/version-latest/06-ifelse.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/06-ifelse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/06-ifelse.md b/example_versioned_docs/version-latest/06-ifelse.md index 308ddea28..714680538 100644 --- a/example_versioned_docs/version-latest/06-ifelse.md +++ b/example_versioned_docs/version-latest/06-ifelse.md @@ -10,7 +10,7 @@ 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) { +if 7 % 2 == 0 { log("7 is even"); } else { log("7 is odd"); From 15033be2ff3de0fc2a2bf546d54f365240ac23c7 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:13:59 +0100 Subject: [PATCH 43/56] Update example_versioned_docs/version-latest/06-ifelse.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/06-ifelse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/06-ifelse.md b/example_versioned_docs/version-latest/06-ifelse.md index 714680538..5452a7f53 100644 --- a/example_versioned_docs/version-latest/06-ifelse.md +++ b/example_versioned_docs/version-latest/06-ifelse.md @@ -16,7 +16,7 @@ if 7 % 2 == 0 { log("7 is odd"); } -if (8 % 4 == 0) { +if 8 % 4 == 0 { log("8 is divisble by 4"); } From 5df2505a0a0d7f0d1e0cc2bf299b83a93125e4a0 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:15:03 +0100 Subject: [PATCH 44/56] Update example_versioned_docs/version-latest/06-ifelse.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/06-ifelse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/06-ifelse.md b/example_versioned_docs/version-latest/06-ifelse.md index 5452a7f53..5f441f7bf 100644 --- a/example_versioned_docs/version-latest/06-ifelse.md +++ b/example_versioned_docs/version-latest/06-ifelse.md @@ -20,7 +20,7 @@ if 8 % 4 == 0 { log("8 is divisble by 4"); } -if(8 % 2 == 0 || 7 % 2 == 0){ +if 8 % 2 == 0 || 7 % 2 == 0 { log("either 8 or 7 are even"); } From cd3c94c4ed912f31695458d5fccc649c3ec58223 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:15:15 +0100 Subject: [PATCH 45/56] Update example_versioned_docs/version-latest/06-ifelse.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/06-ifelse.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_versioned_docs/version-latest/06-ifelse.md b/example_versioned_docs/version-latest/06-ifelse.md index 5f441f7bf..6f1d99218 100644 --- a/example_versioned_docs/version-latest/06-ifelse.md +++ b/example_versioned_docs/version-latest/06-ifelse.md @@ -24,8 +24,8 @@ if 8 % 2 == 0 || 7 % 2 == 0 { log("either 8 or 7 are even"); } -let value:num = 9; -if(value < 0){ +let value = 9; +if value < 0 { log("${value} is negative"); } else if value < 10 { log("${value} has 1 digit"); From 3253bbf1eea410ef63f3f73d088135e15d7e12a6 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:15:24 +0100 Subject: [PATCH 46/56] Update example_versioned_docs/version-latest/07-while.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/07-while.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example_versioned_docs/version-latest/07-while.md b/example_versioned_docs/version-latest/07-while.md index 9e1b83a1e..c1232cfac 100644 --- a/example_versioned_docs/version-latest/07-while.md +++ b/example_versioned_docs/version-latest/07-while.md @@ -7,6 +7,8 @@ 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; From 376e1489a5998c00b7e996e531aee0e50dbbbd22 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:15:50 +0100 Subject: [PATCH 47/56] Update example_versioned_docs/version-latest/07-while.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/07-while.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/07-while.md b/example_versioned_docs/version-latest/07-while.md index c1232cfac..576a36e63 100644 --- a/example_versioned_docs/version-latest/07-while.md +++ b/example_versioned_docs/version-latest/07-while.md @@ -12,7 +12,7 @@ While loops repeatedly check a condition and run a block of code until the condi ```js playground title="main.w" let var i = 0; -while(i < 2){ +whilei < 2 { log("while {i}"); i = i + 1; } From bd517f6b6908d054dc78e0aabf6b1e5f2d913d81 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:15:58 +0100 Subject: [PATCH 48/56] Update example_versioned_docs/version-latest/08-optionality.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/08-optionality.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/08-optionality.md b/example_versioned_docs/version-latest/08-optionality.md index cb69e23ea..a1c76ebdd 100644 --- a/example_versioned_docs/version-latest/08-optionality.md +++ b/example_versioned_docs/version-latest/08-optionality.md @@ -9,7 +9,7 @@ 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". +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"; From ee8dc48edc0ed254cd3ace0461def805798c6ebf Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:16:06 +0100 Subject: [PATCH 49/56] Update example_versioned_docs/version-latest/08-optionality.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/08-optionality.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/08-optionality.md b/example_versioned_docs/version-latest/08-optionality.md index a1c76ebdd..a871ef5b7 100644 --- a/example_versioned_docs/version-latest/08-optionality.md +++ b/example_versioned_docs/version-latest/08-optionality.md @@ -12,7 +12,7 @@ Nullity is a primary source of bugs in software. Being able to guarantee that a 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 monday: str = "doctor"; let tuesday: str? = nil; // Set next to tuesday if there is a value otherwise use monday value From 87f364714530d2de59df3efdc2ea1ebf914b719d Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:16:14 +0100 Subject: [PATCH 50/56] Update example_versioned_docs/version-latest/08-optionality.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/08-optionality.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/08-optionality.md b/example_versioned_docs/version-latest/08-optionality.md index a871ef5b7..81fb05672 100644 --- a/example_versioned_docs/version-latest/08-optionality.md +++ b/example_versioned_docs/version-latest/08-optionality.md @@ -16,7 +16,7 @@ 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; +let next = tuesday ?? monday; log("{next}"); From cbde0c12b8aafcf0e658092a5e290bf30798c0bb Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:16:22 +0100 Subject: [PATCH 51/56] Update example_versioned_docs/version-latest/09-arrays.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/09-arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/09-arrays.md b/example_versioned_docs/version-latest/09-arrays.md index 2357dc30d..9ff6ae78a 100644 --- a/example_versioned_docs/version-latest/09-arrays.md +++ b/example_versioned_docs/version-latest/09-arrays.md @@ -7,7 +7,7 @@ description: Using arrays with Wing keywords: [Wing language, example] --- -Arrays are dynamically sized in Wing and are defined with the [] syntax. +Arrays are dynamically sized in Wing and are created 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. From 6421690e2a178a16663aa40f4f2b1d2e24475306 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:16:30 +0100 Subject: [PATCH 52/56] Update example_versioned_docs/version-latest/09-arrays.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/09-arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/09-arrays.md b/example_versioned_docs/version-latest/09-arrays.md index 9ff6ae78a..24161109b 100644 --- a/example_versioned_docs/version-latest/09-arrays.md +++ b/example_versioned_docs/version-latest/09-arrays.md @@ -8,7 +8,7 @@ keywords: [Wing language, example] --- Arrays are dynamically sized in Wing and are created with the [] syntax. -Individual array items are accessed using the .at(index: num) method. +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" From 0196b65aa21e8b47a729520f34c1e12e608e5b8e Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:16:46 +0100 Subject: [PATCH 53/56] Update example_versioned_docs/version-latest/08-optionality.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/08-optionality.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/08-optionality.md b/example_versioned_docs/version-latest/08-optionality.md index 81fb05672..10ecbeddf 100644 --- a/example_versioned_docs/version-latest/08-optionality.md +++ b/example_versioned_docs/version-latest/08-optionality.md @@ -18,7 +18,7 @@ let tuesday: str? = nil; // Set next to tuesday if there is a value otherwise use monday value let next = tuesday ?? monday; -log("{next}"); +log(next); ``` From e0eef5647e0915466253295fb5ac0c8ef1cf795c Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:17:02 +0100 Subject: [PATCH 54/56] Update example_versioned_docs/version-latest/15-variadic-functions.md Co-authored-by: Chris Rybicki --- .../version-latest/15-variadic-functions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_versioned_docs/version-latest/15-variadic-functions.md b/example_versioned_docs/version-latest/15-variadic-functions.md index b5b09709b..b5ad5955f 100644 --- a/example_versioned_docs/version-latest/15-variadic-functions.md +++ b/example_versioned_docs/version-latest/15-variadic-functions.md @@ -11,11 +11,11 @@ keywords: [Wing language, variadic] Variadic functions can be called with any number of trailing arguments. ```js playground example title="main.w" -// Function that will take an arbitrary number of ints as arguments. +// Function that will take an arbitrary number of numbers as arguments. let plus = (...numbers: Array) => { let var value = 0; - for number in numbers{ + for number in numbers { value = value + number; } From 51c3f10b9fb8cb748a77582f73af3efe8cf5b7e7 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:17:17 +0100 Subject: [PATCH 55/56] Update example_versioned_docs/version-latest/16-closures.md Co-authored-by: Chris Rybicki --- example_versioned_docs/version-latest/16-closures.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/16-closures.md b/example_versioned_docs/version-latest/16-closures.md index ba63c9337..655bde4f4 100644 --- a/example_versioned_docs/version-latest/16-closures.md +++ b/example_versioned_docs/version-latest/16-closures.md @@ -7,7 +7,7 @@ description: Closures with Wing keywords: [Wing language, variadic] --- -Closures are functions that captures variables from its surrounding lexical scope, allowing those variables to persist even after the function is executed outside its original context +[Closures](https://en.wikipedia.org/wiki/Closure_(computer_programming)) are functions that captures variables from its surrounding lexical scope, allowing those variables to persist even after the function is executed outside its original context. ```js playground example title="main.w" From 7c553569c75ac6edd731997650684f4e4360272e Mon Sep 17 00:00:00 2001 From: David Boyne Date: Wed, 11 Sep 2024 10:19:31 +0100 Subject: [PATCH 56/56] fixing example --- example_versioned_docs/version-latest/07-while.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_versioned_docs/version-latest/07-while.md b/example_versioned_docs/version-latest/07-while.md index 576a36e63..e0c09c0ef 100644 --- a/example_versioned_docs/version-latest/07-while.md +++ b/example_versioned_docs/version-latest/07-while.md @@ -12,7 +12,7 @@ While loops repeatedly check a condition and run a block of code until the condi ```js playground title="main.w" let var i = 0; -whilei < 2 { +while i < 2 { log("while {i}"); i = i + 1; }