| 1 |
## our_shell: ysh
|
| 2 |
## oils_failures_allowed: 3
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
proc myadd {
|
| 7 |
json read (&args)
|
| 8 |
|
| 9 |
# convenient!
|
| 10 |
fopen >&2 {
|
| 11 |
= args
|
| 12 |
}
|
| 13 |
|
| 14 |
json write (args[0] + args[1])
|
| 15 |
}
|
| 16 |
|
| 17 |
echo '[1, 2]' | myadd | json read (&result)
|
| 18 |
|
| 19 |
# TODO:
|
| 20 |
# Rewrite this as:
|
| 21 |
#
|
| 22 |
# var result = myadd(1,2)
|
| 23 |
#
|
| 24 |
# Is that possible? I think functions can live in their own namespace?
|
| 25 |
# Or you can have expr.Func() with all the metadata?
|
| 26 |
# Problem with 'source mytool.hay' is that it can define funcs ANYWHERE
|
| 27 |
# you might need a namespace like extern::mytool::myfunc()
|
| 28 |
|
| 29 |
echo "result = $result"
|
| 30 |
|
| 31 |
## STDOUT:
|
| 32 |
TODO
|
| 33 |
result = 3
|
| 34 |
## END
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
proc myadd {
|
| 39 |
|
| 40 |
# I thought this was 'wok'
|
| 41 |
qtt read-rows {
|
| 42 |
if (_index === 0) {
|
| 43 |
# could write header first here
|
| 44 |
write --sep $TAB -- $_schema_str 'result:Int'
|
| 45 |
|
| 46 |
# _schema is the raw schema
|
| 47 |
}
|
| 48 |
# or use BEGIN and 'when', like awk-style
|
| 49 |
|
| 50 |
const result = _row->x + _row->y
|
| 51 |
write --sep $TAB -- $_row_str $result
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
qtt tabify '''
|
| 56 |
x:Int y:Int
|
| 57 |
1 2
|
| 58 |
3 4
|
| 59 |
''' | myadd | qtt read-cols result
|
| 60 |
|
| 61 |
echo "result = $result"
|
| 62 |
|
| 63 |
## STDOUT:
|
| 64 |
## END
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
wok foo {
|
| 69 |
BEGIN {
|
| 70 |
write --sep $TAB -- $_schema_str' result:Int'
|
| 71 |
}
|
| 72 |
|
| 73 |
# for all rows. Could be 'each' or 'row'
|
| 74 |
when {
|
| 75 |
const result = _row->x + _row->y
|
| 76 |
write --sep $TAB -- $_row_str $result
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
| 80 |
## STDOUT:
|
| 81 |
## END
|
| 82 |
|
| 83 |
|
| 84 |
# Notes:
|
| 85 |
# - consider JSON-RPC
|
| 86 |
# - consider multiple return values
|