OILS / demo / survey-loop.sh View on Github | oils.pub

99 lines, 35 significant
1#!/usr/bin/env bash
2#
3# Survey loop behavior
4#
5# Usage:
6# demo/survey-str-api.sh <function name>
7
8set -o nounset
9set -o pipefail
10set -o errexit
11
12source build/dev-shell.sh # python3 in $PATH
13
14# Python and JS string and regex replacement APIs
15
16mutate-py() {
17 echo ---
18 echo PY
19
20 python3 -c '
21mylist = [1,2,3]
22for x in mylist:
23 if x == 2:
24 mylist.append(99)
25 print(x)
26'
27}
28
29mutate-js() {
30 echo ---
31 echo 'JS let'
32
33 nodejs -e '
34let mylist = [1,2,3]
35for (let x of mylist) {
36 if (x === 2) {
37 mylist.push(99)
38 }
39 console.log(x)
40}
41'
42
43 echo ---
44 echo 'JS var'
45
46 nodejs -e '
47var mylist = [1,2,3]
48for (var x of mylist) {
49 if (x === 2) {
50 mylist.push(99)
51 }
52 console.log(x)
53}
54'
55}
56
57mutate-sh() {
58 local sh=${1:-bash}
59
60 echo ---
61 echo sh=$sh
62
63 $sh -c '
64 declare -a mylist=(1 2 3)
65 for x in "${mylist[@]}"; do
66 if test $x == 2; then
67 mylist+=(99)
68 fi
69 echo $x
70 done
71 echo "${mylist[@]}"
72'
73}
74
75mutate-ysh() {
76 echo ---
77 echo 'YSH'
78
79 ysh -c '
80var mylist = [1,2,3]
81for x in (mylist) {
82 if (x === 2) {
83 call mylist->append(99)
84 }
85 echo $x
86}
87'
88}
89
90compare-mutate() {
91 mutate-py
92 mutate-js
93 mutate-sh bash
94 mutate-sh osh
95 mutate-ysh
96}
97
98
99"$@"