forked from robpike/ivy
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ivy/talks: add support files for running the demos
Change-Id: Ife060cfa993743cd650b4c46092ac2b6f4f201b9
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
23 | ||
2*log(10) | ||
1e100 | ||
2^64 | ||
0x234 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#This is ivy. Each step in the demo is one line of input followed by some output. | ||
# Arithmetic | ||
23 | ||
23 + 45 | ||
# Rationals | ||
1/3 | ||
1/3 + 4/5 | ||
1.2 | ||
# Big numbers | ||
1e10 # Still an integer. | ||
1e100 # Still an integer. | ||
2**64 | ||
2**640 | ||
2**6400 | ||
# Vectors | ||
1 2 3 | ||
1 2 3 + 4 5 6 | ||
23 + 1 2 3 | ||
1 << 1 2 3 4 5 | ||
iota 10 | ||
2 ** iota 100 | ||
(2 ** iota 100) == (1<<iota 100) | ||
# Reduction | ||
iota 15 | ||
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 +14 + 15 | ||
+/iota 15 | ||
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 | ||
*/iota 10 | ||
*/iota 100 | ||
# Type this: */iota 10000 | ||
max/2 34 42 233 2 2 521 14 1 4 1 55 133 | ||
# Shapes | ||
5 rho 1 | ||
5 5 rho 1 | ||
5 5 rho 25 | ||
5 5 rho iota 25 | ||
3 5 5 rho iota 125 | ||
x = 5 5 rho iota 75 | ||
x | ||
x/2 | ||
x**2 | ||
x**3 | ||
x**10 | ||
# Inner product | ||
x = 2**iota 5; x | ||
y = 3**iota 5; y | ||
x +.* y | ||
# Outer product | ||
x o.* y | ||
x o.== x | ||
# Random numbers | ||
?100 | ||
?100 | ||
?10 rho 100 | ||
x = ?10 rho 100 | ||
x | ||
# Indexing | ||
x[1] | ||
x[1 9 3] | ||
up x | ||
x[up x] | ||
x[down x] | ||
# Rolls of a die | ||
?6 | ||
10 rho 6 | ||
?10 rho 6 | ||
x = ?10 rho 6; x | ||
(iota 6) o.== x | ||
+/(iota 6) o.== x | ||
+/(iota 6) o.== ?60000 rho 6 | ||
# A big number calculator | ||
*/iota 100 | ||
2**64 | ||
2**iota 64 | ||
-1+2**63 | ||
)base 16 | ||
_ | ||
1<<iota 10 | ||
(2**40)-1 | ||
)obase 10 | ||
_ | ||
# And lots more... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
if flag.NArg() != 2 { | ||
log.Fatal("Usage: script program filename") | ||
} | ||
text, err := ioutil.ReadFile(flag.Arg(1)) | ||
cmd := exec.Command(flag.Arg(0)) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
input, err := cmd.StdinPipe() | ||
ck(err) | ||
err = cmd.Start() | ||
ck(err) | ||
scan := bufio.NewScanner(os.Stdin) | ||
for scan.Scan() { | ||
// User typed something; step back across the newline. | ||
if len(scan.Bytes()) > 0 { | ||
// User typed a non-empty line of text; send that. | ||
line := []byte(fmt.Sprintf("%s\n", scan.Bytes())) | ||
_, err = input.Write(line) | ||
} else { | ||
// User typed newline; send next line of file's text. | ||
if len(text) == 0 { | ||
break | ||
} | ||
for i := 0; i < len(text); i++ { | ||
if text[i] == '\n' { | ||
os.Stdout.Write(text[:i+1]) | ||
_, err = input.Write(text[:i+1]) | ||
text = text[i+1:] | ||
break | ||
} | ||
} | ||
} | ||
ck(err) | ||
} | ||
ck(scan.Err()) | ||
} | ||
|
||
func ck(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |