From 1e63e799ceb159c53aa361bb46158f2617bc97c2 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 13 Mar 2024 12:16:29 -0500 Subject: [PATCH] Changes in the syntax --- md/learn/collections/arrays.md | 2 +- md/learn/collections/maps.md | 95 ++++++++++++++++++++++++++-------- md/learn/collections/tuples.md | 3 +- md/learn/flow-control/match.md | 23 +++++--- md/learn/functions/lambdas.md | 15 +++++- static/css/xcode-colors.css | 3 +- static/js/prism.thp.js | 2 +- 7 files changed, 106 insertions(+), 37 deletions(-) diff --git a/md/learn/collections/arrays.md b/md/learn/collections/arrays.md index b23c506..092796b 100644 --- a/md/learn/collections/arrays.md +++ b/md/learn/collections/arrays.md @@ -11,7 +11,7 @@ val apple = fruits[0] print(apple) -val mut numbers = [0, 1, 2, 3] +var numbers = [0, 1, 2, 3] numbers[3] = 5 diff --git a/md/learn/collections/maps.md b/md/learn/collections/maps.md index 661725c..8ac607d 100644 --- a/md/learn/collections/maps.md +++ b/md/learn/collections/maps.md @@ -1,43 +1,94 @@ # Maps -Also known as Associative Arrays +Also known as Associative Arrays, or Objects in other languages. +All maps must have a definition, which define their fields and datatypes. +There can also be anonymous maps, which may contain any key. -## Usage without a declaration +## Named Maps ```thp -val mut person = Obj { - name: "John", - surname: "Doe", - age: 33, -} - - -print("Hello {person.name}") - -person.age += 1 - -print("You just turned {person.age}") -``` - - -## Usage with a declaration - -```thp -obj Person = { +// Here we define a map, called Person +map Person { String name, String surname, Int age, } - +// Here we declare an instance of a Person. val john_doe = Person { name: "John", surname: "Doe", age: 33, } + +// If the compiler can infer the type of a Map, +// we can omit its type +var Person mary_jane = .{ + name: "Mary", + surname: "Jane", + age: 27, +} ``` +To access the fields of a map we use square braces `[]`. +```thp +mary_jane[age] += 1 +print(mary_jane[name]) // Mary +``` +## Anonymous maps + +An anonymous map allows us to store and retrieve any key of any datatype. +They are declared as `Map`. + +```thp +val car = Map { + brand: "Toyota", + model: "Corolla", + year: 2012, +} +``` + +Anonymous maps can also can have their type omitted. + +```thp +var car = .{ + brand: "Toyota", + model: "Corolla", + year: 2012, +} +``` + +If the compiler encounters a map without a type (that is, `.{}`) +and doesn't expect a specific type, it will assume it is an +anonymous map. + +We can freely assign fields to an anonymous map: + +```thp +// Modify an existing field +car[year] = 2015 +// Create a new field +car[status] = "used" +``` + +However, if we try to access a field of an anonymous map we'll get an error. + +```thp +print(car[status]) // Error: Can't access a field of an anonymous map +``` + +Instead, we should use the `get` function of the map, which expects a +datatype and returns an Option + +```thp +// | this function +String? car_status = car.get[String]("status") +``` + +The `get` function will check that a key `"status"` exists in the map, +and that it has the correct datatype. If either the key doesn't exist +or it has a different datatype, it will return `None`. diff --git a/md/learn/collections/tuples.md b/md/learn/collections/tuples.md index 3db7b71..9c463cc 100644 --- a/md/learn/collections/tuples.md +++ b/md/learn/collections/tuples.md @@ -1,7 +1,6 @@ # Tuples -Uses `#()` just to avoid confusion with function -calls (`()`). +Uses `#()` just to avoid confusion with function calls and grouping (`()`). ## Definition diff --git a/md/learn/flow-control/match.md b/md/learn/flow-control/match.md index 78d5c82..c969df8 100644 --- a/md/learn/flow-control/match.md +++ b/md/learn/flow-control/match.md @@ -9,31 +9,38 @@ val user_id = POST::get("user_id") match user_id -| Some(id) { print("user_id exists: {id}") } -| None { print("user_id doesn't exist") } +case Some(id) { print("user_id exists: {id}") } +case None { print("user_id doesn't exist") } match user_id -| Some(id) +case Some(id) { print("user_id exists: {id}") } -| None +case None { print("user_id doesn't exist") } match user_id -| Some(id) if id > 0 +case Some(id) if id > 0 { print("user_id exists: {id}") } -| _ +else { print("user_id has other values ") } - +match customer_id +case 1, 2, 3 +{ + print("special discount for our first 3 customers!") +} +else +{ + print("hello dear") +} ``` - diff --git a/md/learn/functions/lambdas.md b/md/learn/functions/lambdas.md index 657f7c2..370a6b3 100644 --- a/md/learn/functions/lambdas.md +++ b/md/learn/functions/lambdas.md @@ -59,11 +59,22 @@ f() // 20 ## Lambdas +Lambdas are a short form of anonymous functions. They are declared with `#{}`. + +Inside the lambda you can access the parameters as `$1`, `$2`, etc. + +Finally, lambdas be written outside of the parenthesis of function calls. ```thp -numbers.map { - $0 * 2 +numbers.map() #{ + $1 * 2 } + +// the above lambda is equivalent to: + +numbers.map(fun(param1) { + $1 * 2 +}) ``` diff --git a/static/css/xcode-colors.css b/static/css/xcode-colors.css index 120ffd9..340f86e 100644 --- a/static/css/xcode-colors.css +++ b/static/css/xcode-colors.css @@ -29,6 +29,7 @@ --code-theme-bg-color: #ffffff; --code-theme-bg-color_selection: #a4cdff; --code-theme-color_selection: inherit; + --code-theme-border-color: #c2c2c2; --code-theme-comment: #5d6c79; @@ -92,7 +93,6 @@ pre[class*="language-"] ::selection { } :not(pre) > code { - /* border: solid 1px var(--border-color); */ background-color: var(--code-theme-bg-color); padding: 0 0.25rem; border-radius: 5px; @@ -103,6 +103,7 @@ pre[class*="language-"] { position: relative; margin: 0.5em 0; padding: 0.75em 0.75em; + border: 1px solid var(--code-theme-border-color); } .language-css > code, diff --git a/static/js/prism.thp.js b/static/js/prism.thp.js index d776025..659b067 100644 --- a/static/js/prism.thp.js +++ b/static/js/prism.thp.js @@ -15,7 +15,7 @@ Prism.languages.thp = { pattern: /(["])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, greedy: true, }, - "keyword": /\b(?:static|const|enum|loop|use|break|catch|continue|do|else|finally|for|fun|if|in|fn|nil|return|throw|try|while|type|match|with|of|abstract|class|interface|private|pub|obj|override|open|init|val|var|mut|clone)\b/, + "keyword": /\b(?:case|static|const|enum|loop|use|break|catch|continue|do|else|finally|for|fun|if|in|fn|nil|return|throw|try|while|type|match|with|of|abstract|class|interface|private|pub|map|override|open|init|val|var|mut|clone)\b/, "number": /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i, "operator": /[<>]=?|[!=]=?=?|--?|\$|\+\+?|&&?|\|\|?|[?*/~^%]/, "punctuation": /[{}[\];(),.]/,