From 485eda01fa3c92d53fbefaa92b459ed0b539d126 Mon Sep 17 00:00:00 2001 From: jpwysocz Date: Thu, 5 Dec 2024 17:47:44 -0800 Subject: [PATCH] Add snippets/debug_label.pine --- snippets/debug_label.pine | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 snippets/debug_label.pine diff --git a/snippets/debug_label.pine b/snippets/debug_label.pine new file mode 100644 index 0000000..ece470e --- /dev/null +++ b/snippets/debug_label.pine @@ -0,0 +1,38 @@ +//@version=6 +indicator("Removing key-value pairs demo") + +//@function Returns a label to display the keys and values from a map. +method debugLabel( + map this, int barIndex = bar_index, + color bgColor = color.blue, string note = "" + ) => + //@variable A string representing the size, keys, and values in `this` map. + string repr = str.format( + "{0}\nSize: {1}\nKeys: {2}\nValues: {3}", + note, this.size(), str.tostring(this.keys()), str.tostring(this.values()) + ) + label.new( + barIndex, 0, repr, color = bgColor, style = label.style_label_center, + textcolor = color.white, size = size.small + ) + +if bar_index == last_bar_index - 1 + //@variable A map containing `string` keys and `int` values. + m = map.new() + + // Put key-value pairs into `m`. + for [i, key] in array.from("A", "B", "C", "D", "E") + m.put(key, i) + m.debugLabel(bar_index, color.green, "Added pairs") + + //@variable An array of keys to remove from `m`. + array removeKeys = array.from("B", "B", "D", "F", "a") + + // Remove each `key` in `removeKeys` from `m`. + for key in removeKeys + m.remove(key) + m.debugLabel(bar_index + 15, color.red, "Removed pairs") + + // Remove all remaining keys from `m`. + m.clear() + m.debugLabel(bar_index + 30, color.purple, "Cleared the map")