summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorseh <henesy.dev@gmail.com>2019-03-18 01:25:33 -0500
committerseh <henesy.dev@gmail.com>2019-03-18 01:25:33 -0500
commita609ffb20226eba3d4dc628d7b4d284eddd1c114 (patch)
tree34ba8857c762716215e87d50d6cd900a3a5bf527
parent800edd82a0a1c63a1a08fa23502a280dbb7d9937 (diff)
add slices example
-rw-r--r--README.md1
-rw-r--r--Slices/README.md32
-rw-r--r--Slices/slices.b31
3 files changed, 64 insertions, 0 deletions
diff --git a/README.md b/README.md
index 4b9145f..3f760f1 100644
--- a/README.md
+++ b/README.md
@@ -35,6 +35,7 @@ You could then run said file with:
- [If Else](./If-Else)
- [Switch Case](./Switch)
- [Arrays](./Arrays)
+- [Slices](./Slices)
- [Functions](./Functions)
- [Function References](./Function-Refs)
- [Generics, Picks, and Interfaces (kind of)](./Generics)
diff --git a/Slices/README.md b/Slices/README.md
new file mode 100644
index 0000000..87bdfd3
--- /dev/null
+++ b/Slices/README.md
@@ -0,0 +1,32 @@
+# Slices
+
+Limbo supports slicing on arbitrary array types, including strings, as strings are arrays of bytes.
+
+## Source
+
+### slices.b:19,22
+
+This section shows slicing from the beginning of an array to the index up to before the variable `n`.
+
+### slices.b:24,25
+
+This section shows slicing from the index 4 to the end of the array.
+
+### slices.b:27,28
+
+This section shows several slicing operations. First `c` is created as the slice of `str` from index 2 through the index prior to the final index of `str`. The printout is then a string version of the slice of `c` from index 4 through the 7th index.
+
+## Demo
+
+ ; limbo slices.b
+ ; slices
+ little_baby_ducks
+ lit
+ le_baby_ducks
+ _bab
+ ;
+
+## Exercises
+
+- What happens if you try to slice an empty string, `""`?
+- Can you slice in the format `[:n:]`?
diff --git a/Slices/slices.b b/Slices/slices.b
new file mode 100644
index 0000000..5f9db96
--- /dev/null
+++ b/Slices/slices.b
@@ -0,0 +1,31 @@
+implement Slices;
+
+include "sys.m";
+include "draw.m";
+
+sys: Sys;
+print: import sys;
+
+Slices: module {
+ init: fn(nil: ref Draw->Context, nil: list of string);
+};
+
+init(nil: ref Draw->Context, nil: list of string) {
+ sys = load Sys Sys->PATH;
+
+ str := "little_baby_ducks";
+ print("%s\n", str);
+
+ n := 3;
+
+ a := str[:n];
+ print("%s\n", a);
+
+ b := str[4:];
+ print("%s\n", b);
+
+ c := array of byte str[2:len str -1];
+ print("%s\n", string c[4:8]);
+
+ exit;
+}