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

113 lines, 38 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 echo ---
29 echo PY comp
30
31 python3 -c '
32def myappend(li, i):
33 if i == 1:
34 li.append(99)
35 print(i)
36 return i
37
38mylist = [1,2,3]
39y = [myappend(mylist, i) for i in mylist]
40print(y)
41'
42}
43
44mutate-js() {
45 echo ---
46 echo 'JS let'
47
48 nodejs -e '
49let mylist = [1,2,3]
50for (let x of mylist) {
51 if (x === 2) {
52 mylist.push(99)
53 }
54 console.log(x)
55}
56'
57
58 echo ---
59 echo 'JS var'
60
61 nodejs -e '
62var mylist = [1,2,3]
63for (var x of mylist) {
64 if (x === 2) {
65 mylist.push(99)
66 }
67 console.log(x)
68}
69'
70}
71
72mutate-sh() {
73 local sh=${1:-bash}
74
75 echo ---
76 echo sh=$sh
77
78 $sh -c '
79 declare -a mylist=(1 2 3)
80 for x in "${mylist[@]}"; do
81 if test $x == 2; then
82 mylist+=(99)
83 fi
84 echo $x
85 done
86 echo "${mylist[@]}"
87'
88}
89
90mutate-ysh() {
91 echo ---
92 echo 'YSH'
93
94 ysh -c '
95var mylist = [1,2,3]
96for x in (mylist) {
97 if (x === 2) {
98 call mylist->append(99)
99 }
100 echo $x
101}
102'
103}
104
105compare-mutate() {
106 mutate-py
107 mutate-js
108 mutate-sh bash
109 mutate-sh osh
110 mutate-ysh
111}
112
113"$@"