Commit 73bfe5041ed7e5049b99fff6d5719702a694742b

Thomas de Grivel 2025-03-30T19:20:49

wip doc Map and Struct

diff --git a/.ikc3_history b/.ikc3_history
index 3bd05e8..d42da38 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,4 +1,3 @@
-quit
 q
 "#{inspect(%HTTP.Upload{})}"
 git diff
@@ -97,3 +96,4 @@ s = %File.Stat{st_dev: (Uw) 1039,
 s = %File.Stat{st_dev: (Uw) 1039}
 s.st_mtim
 List.to_array
+a = %KC3.Op{sym: :dot, callable: fn (a, b) { a.x * b.y + a.y * b.y }}
diff --git a/doc/1_KC3/1.3_Map.en.md b/doc/1_KC3/1.3_Map.en.md
index 9a09d14..863fdf7 100644
--- a/doc/1_KC3/1.3_Map.en.md
+++ b/doc/1_KC3/1.3_Map.en.md
@@ -4,14 +4,18 @@ KC3 maps are like Elixir maps, they are key-values enclosed in `%{}` :
 
 ```elixir
 ikc3> a = %{id: 1, title: "My title", message: "Hello, world !"}
-%{id: 1, title: "My title", message: "Hello, world !"}
+%{id: 1,
+  title: "My title",
+  message: "Hello, world !"}
 ```
 
 Destructuring works with maps to extract values :
 
 ```elixir
 ikc3> %{id: id, title: "My title", message: message} = ^ a
-%{id: 1, title: "My title", message: "Hello, world !"}
+%{id: 1,
+  title: "My title",
+  message: "Hello, world !"}
 ikc3> id
 1
 ikc3> message
@@ -23,7 +27,9 @@ You can use the dot syntax to access map values from a `Sym` key :
 
 ```elixir
 ikc3> a = %{id: 1, title: "My title", message: "Hello, world !"}
-%{id: 1, title: "My title", message: "Hello, world !"}
+%{id: 1,
+  title: "My title",
+  message: "Hello, world !"}
 ikc3> a.id
 1
 ikc3> a.message
@@ -34,13 +40,28 @@ You can also use the `KC3.access` function for the same result :
 
 ```elixir
 ikc3> a = %{id: 1, title: "My title", message: "Hello, world !"}
-%{id: 1, title: "My title", message: "Hello, world !"}
+%{id: 1,
+  title: "My title",
+  message: "Hello, world !"}
 ikc3> access(a, :id)
 1
 ikc3> access(a, :message)
 "Hello, world !"
 ```
 
+To update an existing map you can use Map.put like this :
+
+```elixir
+ikc3> a = %{id: 1, title: "My title"}
+%{id: 1,
+  title: "My title"}
+ikc3> a = Map.put(a, :message, "Hello, world !")
+%{id: 1,
+  title: "My title",
+  message: "Hello, world !"}
+```
+
+
 ---
 
 Top : [KC3 documentation](/doc/)
diff --git a/doc/1_KC3/1.7_Struct.en.md b/doc/1_KC3/1.7_Struct.en.md
new file mode 100644
index 0000000..0f4b439
--- /dev/null
+++ b/doc/1_KC3/1.7_Struct.en.md
@@ -0,0 +1,116 @@
+# 1.7 Struct
+
+KC3 structs are a key value associative data structure with default
+values and arbitrary property order.
+
+They are compatible with C structs of the same type.
+
+```elixir
+ikc3> a = %KC3.Op{sym: :dot,
+                  precedence: 10,
+                  callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+%KC3.Op{sym: :dot,
+        arity: 2,
+        special: false,
+        precedence: 10,
+        associativity: 1,
+        callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+```
+
+You can define one _struct type_ per module with `defstruct` for
+example in `vec2d.kc3` :
+
+```elixir
+defmodule Vec2D do
+
+  defstruct [x: 0.0,
+             y: 0.0]
+
+end
+```
+
+Destructuring works with structs to extract values :
+
+```elixir
+ikc3> %KC3.Op{sym: sym} = ^ a
+%KC3.Op{sym: :dot,
+        arity: 2,
+        special: false,
+        precedence: 10,
+        associativity: 1,
+        callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+ikc3> sym
+:dot
+```
+
+
+You can use the dot syntax to access struct values from a `Sym` key :
+
+```elixir
+ikc3> a = %KC3.Op{sym: :dot,
+                  precedence: 10,
+                  callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+%KC3.Op{sym: :dot,
+        arity: 2,
+        special: false,
+        precedence: 10,
+        associativity: 1,
+        callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+ikc3> a.sym
+:dot
+ikc3> a.arity
+2
+ikc3> a.callable
+fn (a, b) { a.x * b.y + a.y * b.y }
+```
+
+You can also use the `KC3.access` function for the same result :
+
+```elixir
+ikc3> a = %KC3.Op{sym: :dot,
+                  precedence: 10,
+                  callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+%KC3.Op{sym: :dot,
+        arity: 2,
+        special: false,
+        precedence: 10,
+        associativity: 1,
+        callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+ikc3> access(a, :sym)
+:dot
+ikc3> access(a, :arity)
+2
+ikc3> access(a, :callable)
+fn (a, b) { a.x * b.y + a.y * b.y }
+```
+
+To update an existing struct property you can use Struct.put, a new
+struct with modified properties will be returned. E.g. :
+
+```elixir
+ikc3> a = %KC3.Op{sym: :dot,
+                  precedence: 10,
+                  callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+%KC3.Op{sym: :dot,
+        arity: 2,
+        special: false,
+        precedence: 10,
+        associativity: 1,
+        callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+b = Struct.put(a, :sym, :·)
+%KC3.Op{sym: :·,
+        arity: 2,
+        special: false,
+        precedence: 10,
+        associativity: 1,
+        callable: fn (a, b) { a.x * b.y + a.y * b.y }}
+```
+
+
+---
+
+Top : [KC3 documentation](/doc/)
+
+Previous : [1.2 Integer](1.2_Integer)
+
+Next : [1.4 Ratio](1.4_Ratio)