github.com/SahandAslani/gomobile@v0.0.0-20210909130135-2cb2d44c09b2/example/ivy/android/app/src/main/assets/demo.ivy (about) 1 # This is ivy. Type a newline to advance to each new step. Type one now. 2 # Each step in the demo is one line of input followed by some output from ivy. Type a newline now to see. 3 2+2 4 # The first line you see above (2+2) is input; the next (4) is output from a running ivy. 5 # Comments start with # and produce no output. 6 # Whenever you like, you can type an expression yourself. Try typing 2*3 now, followed by two newlines: 7 # Keep typing newlines; the ivy demo is about to start. 8 # Arithmetic has the obvious operations: + - * etc. ** is exponentiation. mod is modulo. 9 23 10 23 + 45 11 23 * 45 12 23 - 45 13 7 ** 3 14 7 mod 3 15 # Operator precedence is unusual. 16 # Unary operators operate on everything to the right. 17 # Binary operators operate on the item immediately to the left, and everything to the right. 18 2*3+4 # Parsed as 2*(3+4), not the usual (2*3)+4. 19 2**2+3 # 2**5, not (2**2) + 3 20 (2**2)+3 # Use parentheses if you need to group differently. 21 # Ivy can do rational arithmetic, so 1/3 is really 1/3, not 0.333.... 22 1/3 23 1/3 + 4/5 24 1/3 ** 2 # We'll see non-integral exponents later. 25 # Even when a number is input in floating notation, it is still an exact rational number inside. 26 1.2 27 # In fact, ivy is a "bignum" calculator that can handle huge numbers and rationals made of huge numers. 28 1e10 # Still an integer. 29 1e100 # Still an integer. 30 1e10/3 # Not an integer, but an exact rational. 31 3/1e10 # Not an integer, but an exact rational. 32 2**64 # They can get big. 33 2**640 # They can get really big. 34 # They can get really really big. Type a newline to see 2**6400 scroll by. 35 2**6400 36 # Ivy also has characters, which represent a Unicode code point. 37 'x' 38 char 0x61 # char is an operator: character with given value. 39 char 0x1f4a9 40 code '💩' # char's inverse, the value of given character, here printed in decimal. 41 # Everything in ivy can be placed into a vector. 42 # Vectors are written and displayed with spaces between the elements. 43 1 2 3 44 1 4/3 5/3 (2+1/3) 45 # Vectors of characters print without quotes or spaces. 46 'h' 'e' 'l' 'l' 'o' 47 # This is a nicer way to write 'h' 'e' 'l' 'l' 'o'. It means the same. 48 'hello' 49 # Arithmetic works on vectors. 50 1 2 3 + 4 5 6 51 # Arithmetic between scalar and vector also works, either way. 52 23 + 1 2 3 53 1 2 3 + 23 # Note the grouping: vector is a single value. 54 # More fun with scalar and vector. 55 1 << 1 2 3 4 5 56 (1 << 1 2 3 4 5) == (2 ** 1 2 3 4 5) # Note: true is 1, false is 0. 57 # iota is an "index generator": It counts from 1. 58 iota 10 59 2 ** iota 5 60 (1 << iota 100) == 2 ** iota 100 61 2 ** -1 + iota 32 # Again, see how the precedence rules work. 62 # The take operator removes n items from the beginning of the vector. 63 3 take iota 10 64 -3 take iota 10 # Negative n takes from the end. 65 # Drop is the other half: it drops n from the vector. 66 3 drop iota 10 67 -3 drop iota 10 # Negative n drops from the end. 68 6 drop 'hello world' 69 # Reduction 70 iota 15 71 # Add them up: 72 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 73 # Automate this by reducing + over the vector, like this: 74 +/iota 15 75 # We can reduce using any binary operator. This is factorial: 76 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 77 */iota 10 78 */iota 100 79 # Type this: */iota 10000 80 # That printed using floating-point notation for manageability but it is still an integer inside. 81 # max and min are binary operators that do the obvious. (Use semicolons to separate expresssions.) 82 3 max 7; 'is max and'; 3 min 7; 'is min' 83 # Like all binary arithmetic operators, max applies elementwise. 84 2 3 4 max 4 3 2 85 # Reduce using max to find maxmimum element in vector. 86 max/2 34 42 233 2 2 521 14 1 4 1 55 133 87 # Ivy allows multidimensional arrays. The shape operator binary rho builds them. 88 # Dimension (which may be a vector) on the left, data on the right. 89 5 rho 1 90 5 5 rho 1 91 5 5 rho 25 92 5 5 rho iota 25 93 3 5 5 rho iota 125 94 # Unary rho tells us the shape of an item. 95 x = 3 5 rho iota 15; x 96 rho x 97 x = 3 5 5 rho iota 75; x 98 rho x 99 # Arithmetic on matrices works as you would expect by now. 100 x/2 101 x**2 102 x**3 103 x**10 104 # Inner product is written with a . between the operators. 105 # This gives dot product: multiply corresponding elements and add the result. 106 1 2 3 4 +.* 2 3 4 5 107 # Any operator works. How many items are the same? 108 (1 2 3) +.== (1 3 3) 109 # How many differ? 110 (1 2 3) +.!= (1 3 3) 111 # Outer product generates a matrix of all combinations applying the binary operator. 112 (iota 5) o.* -1 + iota 5 113 # That's a letter 'o', dot, star. 114 # Any operator works; here is how to make an identity matrix. 115 x = iota 5; x o.== x 116 # Random numbers: Use a unary ? to roll an n-sided die from 1 to n. 117 ?100 118 ?100 119 ?20 rho 6 # 20 rolls of a 6-sided die. 120 x = ?20 rho 6 # Remember one set of rolls. 121 x 122 # Indexing is easy. 123 x[1] 124 x[1 19 3] # You can index with a vector. 125 # The up and down operators generate index vectors that would sort the input. 126 up x 127 x[up x] 128 x[down x] 129 'hello world'[up 'hello world'] 130 'hello world'[down 'hello world'] 131 # More rolls of a die. 132 ?10 rho 6 133 # Remember a set of rolls. 134 x = ?10 rho 6; x 135 # The outer product of == and the integers puts 1 in each row where that value appeared. 136 # Compare the last row of the next result to the 6s in x. 137 (iota 6) o.== x 138 # Count the number of times each value appears by reducing the matrix horizontally. 139 +/(iota 6) o.== x 140 # Do it for a much larger set of rolls: is the die fair? 141 +/(iota 6) o.== ?60000 rho 6 142 # Remember that ivy is a big number calculator. 143 */iota 100 144 2**64 145 2**iota 64 146 -1+2**63 147 # Settings are made and queried with a leading right paren. )help lists the settings 148 )help 149 # Use )base to switch input and output to base 16. 150 )base 16 151 )base # The input and output for settings is always base 10. 152 # _ is a variable that holds the most recently evaluated expression. It remembers our 63-bit number. 153 _ 154 1<<iota 10 # 16 powers of two, base 16. 155 (2**40)-1 # The largest 64-bit number base 16. 156 )obase 10 # Output base 10, input base still 16. 157 )base 158 -1+2**63 # The largest 63-bit number base 10. 159 -1+2**64 # The largest 64-bit number base 10. 160 # Go back to base 10 input and output. 161 )base 10 162 # Rationals can be very big too. 163 (2**1e3)/(3**1e2) 164 # Such output can be unwieldy. Change the output format using a Printf string. 165 )format '%.12g' 166 _ 167 # We need more precision. 168 )format "%.100g" # Double quotes work too; there's no difference. 169 _ 170 )format '%#x' 171 _ 172 )format '%.12g' # A nice format, easily available by running ivy -g. 173 _ 174 (3 4 rho iota 12)/4 175 # Irrational functions cannot be represented precisely by rational numbers. 176 # Ivy stores irrational results in high-precision (default 256-bit) floating point numbers. 177 sqrt 2 178 # pi and e are built-in, high-precision constants. 179 pi 180 e 181 )format "%.100g" 182 pi 183 )format '%.12g' 184 pi 185 # Exponentials and logarithms. 186 2**1/2 # Note: Non-integral exponent generates irrational result. 187 e**1e6 188 log e**1e6 189 log e**1e8 190 log 1e1000000 # Yes, that is 10 to the millionth power. 191 # Transcendentals. (The low bits aren't always quite right...) 192 sin pi/2 193 cos .25*pi * -1 + iota 9 194 log iota 6 195 # Successive approximations to e. (We force the calculation to use float using the "float" unary operator. Why?) 196 (float 1+10**-iota 9) ** 10**iota 9 197 # Default precision is 256 bits of mantissa. We can go up to 10000. 198 )prec 3000 # Units are bits, not digits. 199 e 200 )format '%.1000g' # Units are digits. (Sorry for the inconsistency.) 201 e 202 pi 203 sqrt 2 204 e**1e6 205 log e**1e6 # Only 904 good digits; the log algorithm is numerically weak. (Ideas welcome.) 206 (2**1e3)/(3**1e2) 207 # User-defined operators are declared as unary or binary (or both). This one computes the (unary) average. 208 op avg x = (+/x)/rho x 209 avg iota 100 210 # Here is a binary operator. 211 op n largest x = n take x[down x] 212 3 largest ? 100 rho 1000 213 4 largest 'hello world' 214 # Population count. Use encode to turn the value into a string of bits. Use log to decide how many. 215 op a base b = ((ceil b log a) rho b) encode a 216 7 base 2 217 op popcount n = +/n base 2 218 popcount 7 219 popcount 1e6 220 popcount 1e100 221 # Here is one to sum the digits. The unary operator text turns its argument into text, like Sprintf. 222 op sumdigits x = t = text x; +/(code (t in '0123456789') sel t) - code '0' 223 # Break it down: The sel operator selects from the right based on the non-zero elements in the left. 224 # The in operator generates a selector by choosing only the bytes that are ASCII digits. 225 sumdigits 99 226 sumdigits iota 10 227 sumdigits '23 skidoo' # Note: It counts only the digits. 228 # That's it! Have fun. 229 # For more information visit https://godoc.org/robpike.io/ivy