ngncheat.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ngn/k, (c) 2019-2022 ngn, GNU AGPLv3. (codeberg.org/ngn/k)
  2. : SET RETURN ' each|slide|bin
  3. + add flip / fold|join |dec|comment
  4. - subtract negate \ scan|split|enc|trace
  5. * multiply first ': eachprior
  6. % divide sqrt /: eachright
  7. ! mod|dict enum|key \: eachleft
  8. & min|and where
  9. | max|or reverse /
  10. < less ascend multiline comment
  11. > more descend \
  12. = equal group
  13. ~ match not 0: lines i/o
  14. , concat enlist 1: bytes i/o
  15. ^ without null
  16. # reshape length
  17. _ drop|cut floor
  18. $ cast string $[c;t;f] COND
  19. ? find|rnd uniq ?[a;i;b] splice
  20. @ apply(1) type @[x;i;[f;]y] amend
  21. . apply(n) eval .[x;i;[f;]y] drill
  22. grammar: E:E;e|e e:nve|te| t:n|v v:tA|V n:t[E]|(E)|{E}|N
  23. limits: 8 locals/args, 256 bytecode
  24. --------------------------------------------------------------------------------
  25. Types:
  26. list atom
  27. `A generic list () ,"ab" (0;`1;"2";{3};%)
  28. `I `i int 0N -9223372036854775807 01b
  29. `D `d float -0w -0.0 0.0 0w 1.2e308 0n
  30. `C `c char "a" 0x6263 "d\0\"\n\r\t"
  31. `S `s symbol ` `a `"file.txt" `b`cd`"ef"
  32. `M `m table&dict +`a`b!(0 1;"23") (0#`)!()
  33. `o lambda {1+x*y#z} {[a;b]+/*/2#,a,b}
  34. `p projection 1+ {z}[0;1] @[;i;;]
  35. `q composition *|: {1+x*y}@
  36. `r derived verb +/ 2\ {y,x}':
  37. `u monadic verb +: 0::
  38. `v dyadic verb + 0:
  39. `w adverb ' /:
  40. `e external func
  41. --------------------------------------------------------------------------------
  42. I/O verbs
  43. 0:x read lines
  44. x 0:y write lines
  45. 1:x read bytes
  46. x 1:y write bytes
  47. <s open fd:<`"file.txt"
  48. >i close >fd
  49. x can be a file descriptor (int) or symbol or string such as
  50. "file.txt"
  51. "/path/to/file"
  52. "host:port"
  53. ":port" /host defaults to 127.0.0.1
  54. --------------------------------------------------------------------------------
  55. Verbs: : + - * % ! & | < > = ~ , ^ # _ $ ? @ . 0: 1:
  56. notation: [c]har [i]nt [n]umber(int|float) [s]ymbol [a]tom [d]ict
  57. [f]unc(monad) [F]unc(dyad) [xyz]any
  58. special: var:y set a:1;a -> 1
  59. (v;..):y unpack (b;(c;d)):(2 3;4 5);c -> 4
  60. :x return {:x+1;2}[3] -> 4
  61. $[x;y;..] cond $[0;`a;"\0";`b;`;`c;();`d;`e] -> `e
  62. o[..] recur {$[x<2;x;+/o'x-1 2]}9 -> 34
  63. [..] progn [0;1;2;3] -> 3
  64. :: self ::12 -> 12
  65. : right 1 :2 -> 2 "abc":'"d" -> "ddd"
  66. +x flip +("ab";"cd") -> ("ac";"bd")
  67. N+N add 1 2+3 -> 4 5
  68. -N negate - 1 2 -> -1 -2
  69. N-N subtract 1-2 3 -> -1 -2
  70. *x first *`a`b -> `a *(0 1;"cd") -> 0 1
  71. N*N multiply 1 2*3 4 -> 3 8
  72. %N sqrt %25 -> 5.0 %-1 -> 0n
  73. N%N divide 2 3%4 -> 0.5 0.75
  74. !i enum !3 -> 0 1 2 !-3 -> -3 -2 -1
  75. !I odometer !2 3 -> (0 0 0 1 1 1;0 1 2 0 1 2)
  76. !d keys !`a`b!0 1 -> `a`b
  77. !S ns keys a.b.c:1;a.b.d:2;!`a`b -> ``c`d
  78. x!y dict `a`b!1 2 -> `a`b!1 2
  79. i!I div -10!1234 567 -> 123 56
  80. i!I mod 10!1234 567 -> 4 7
  81. &I where &3 -> 0 0 0 &1 0 1 4 2 -> 0 2 3 3 3 3 4 4
  82. &x deepwhere &(0 1 0;1 0 0;1 1 1) -> (0 1 2 2 2;1 0 0 1 2)
  83. N&N min/and 2&-1 3 -> -1 2 0 0 1 1&0 1 0 1 -> 0 0 0 1
  84. |x reverse |"abc" -> "cba" |12 -> 12
  85. N|N max/or 2|-1 3 -> 2 3 0 0 1 1|0 1 0 1 -> 0 1 1 1
  86. <X ascend <"abacus" -> 0 2 1 3 5 4
  87. >X descend >"abacus" -> 4 5 3 1 0 2
  88. <s open fd:<`"/path/to/file.txt"
  89. >i close >fd
  90. N<N less 0 2<1 -> 1 0
  91. N>N more 0 1>0 2 -> 0 0
  92. =X group ="abracadabra" -> "abrcd"!(0 3 5 7 10;1 8;2 9;,4;,6)
  93. =i unitmat =3 -> (1 0 0;0 1 0;0 0 1)
  94. N=N equal 0 1 2=0 1 3 -> 1 1 0
  95. ~x not ~(0 2;``a;"a \0";::;{}) -> (1 0;1 0;0 0 1;1;0)
  96. x~y match 2 3~2 3 -> 1 "4"~4 -> 0 0~0.0 -> 0
  97. ,x enlist ,0 -> ,0 ,0 1 -> ,0 1 ,`a!1 -> +(,`a)!,,1
  98. x,y concat 0,1 2 -> 0 1 2 "a",1 -> ("a";1)
  99. d,d merge (`a`b!0 1),`b`c!2 3 -> `a`b`c!0 2 3
  100. ^x null ^(" a";0 1 0N;``a;0.0 0n) -> (1 0;0 0 1;1 0;0 1)
  101. a^y fill 1^0 0N 2 3 0N -> 0 1 2 3 1 "b"^" " -> "b"
  102. X^y without "abracadabra"^"bc" -> "araadara"
  103. #x length #"abc" -> 3 #4 -> 1 #`a`b`c!0 1 0 -> 3
  104. i#y reshape 3#2 -> 2 2 2
  105. I#y reshape 2 3#` -> (```;```)
  106. f#y replicate (3>#:')#(0;2 1 3;5 4) -> (0;5 4) {2}#"ab" -> "aabb"
  107. x#d take `c`d`f#`a`b`c`d!1 2 3 4 -> `c`d`f!3 4 0N
  108. _n floor _12.34 -12.34 -> 12 -13
  109. _c lowercase _"Ab" -> "ab"
  110. i_Y drop 2_"abcde" -> "cde" `a`c_`a`b`c!0 1 2 -> (,`b)!,1
  111. I_Y cut 2 4 4_"abcde" -> ("cd";"";,"e")
  112. f_Y weed out (3>#:')_(0;2 1 3;5 4) -> ,2 1 3
  113. X_i delete "abcde"_2 -> "abde"
  114. $x string $(12;"ab";`cd;+) -> ("12";(,"a";,"b");"cd";,"+")
  115. i$C pad 5$"abc" -> "abc " -3$"a" -> " a"
  116. s$y cast `c$97 -> "a" `i$-1.2 -> -1 `$"a" -> `a
  117. s$y int `I$"-12" -> -12
  118. ?X uniq ?"abacus" -> "abcus"
  119. ?i uniform ?2 -> 0.6438163747387873 0.8852656305774402 /random
  120. X?y find "abcde"?"bfe" -> 1 0N 4
  121. i?x roll 3?1000 -> 11 398 293 1?0 -> ,-8164324247243690787
  122. i?x deal -3?1000 -> 11 398 293 /guaranteed distinct
  123. @x type @1 -> `b @"ab" -> `C @() -> `A @(@) -> `v
  124. x@y apply(1) {x+1}@2 -> 3 "abc"@1 -> "b" (`a`b!0 1)@`b -> 1
  125. .S get a:1;.`a -> 1 b.c:2;.`b`c -> 2
  126. .C eval ."1+2" -> 3
  127. .d values .`a`b!0 1 -> 0 1
  128. x.y apply(n) {x*y+1}. 2 3 -> 8 (`a`b`c;`d`e`f). 1 0 -> `d
  129. @[x;y;f] amend @["ABC";1;_:] -> "AbC" @[2 3;1;{-x}] -> 2 -3
  130. @[x;y;F;z] amend @["abc";1;:;"x"] -> "axc" @[2 3;0;+;4] -> 6 3
  131. .[x;y;f] drill .[("AB";"CD");1 0;_:] -> ("AB";"cD")
  132. .[x;y;F;z] drill .[("ab";"cd");1 0;:;"x"] -> ("ab";"xd")
  133. .[f;y;f] try .[+;1 2;"E:",] -> 3 .[+;1,`;"E:",] -> "E:'type\n"
  134. ?[x;y;z] splice ?["abcd";1 3;"xyz"] -> "axyzd"
  135. --------------------------------------------------------------------------------
  136. Special symbols:
  137. `@x serialize ` 7 8 9 -> 0x020300000000000000070809
  138. `?C deserialize `?0x020300000000000000070809 -> 7 8 9
  139. `j?C parse json `j?"{\"a\":1,\"b\":[true,\"c\"]}" -> `a`b!(1.0;(1;,"c"))
  140. `k@x pretty-print `k("ab";2 3) -> "(\"ab\";2 3)"
  141. `p@C parse k
  142. `hex@C hexadecimal `hex"ab" -> "6162"
  143. `x@x fork-exec `x(("/bin/wc";"-l");"a\nbc\nd\n") -> "3\n"
  144. `t[] current time (microseconds)
  145. `argv[] list of cmd line args
  146. `env[] dict of env variables
  147. `prng[] `prng@I get/set pseudo-random number generator internal state
  148. s:`prng[];r:9?0;`prng s;r~9?0 -> 1
  149. `prng@0 use current time to set state
  150. `err@C throw error
  151. Special symbols available only in an interpreter built with "make k-libc":
  152. `sin@N trigonometry `sin 12.34 -> -0.22444212919135995
  153. `exp@N exponential `exp 1 -> 2.7182818284590455
  154. `ln@N logarithm `ln 2 -> 0.6931471805599453
  155. --------------------------------------------------------------------------------
  156. Adverbs: ' / \ ': /: \:
  157. f' each1 #'("abc";3 4 5 6) -> 3 4
  158. x F' each2 2 3#'"ab" -> ("aa";"bbb")
  159. X' binsearch 1 3 5 7 9'8 9 0 -> 3 4 -1
  160. F/ fold +/1 2 3 -> 6
  161. F\ scan +\1 2 3 -> 1 3 6
  162. x F/ seeded / 10+/1 2 3 -> 16
  163. x F\ seeded \ 10+\1 2 3 -> 11 13 16
  164. i f/ n-do 5(2*)/1 -> 32
  165. i f\ n-dos 5(2*)\1 -> 1 2 4 8 16 32
  166. f f/ while (1<){$[2!x;1+3*x;-2!x]}/3 -> 1
  167. f f\ whiles (1<){$[2!x;1+3*x;-2!x]}\3 -> 3 10 5 16 8 4 2 1
  168. f/ converge {1+1.0%x}/1 -> 1.618033988749895
  169. f\ converges (-2!)\100 -> 100 50 25 12 6 3 1 0
  170. C/ join "ra"/("ab";"cadab";"") -> "abracadabra"
  171. C\ split "ra"\"abracadabra" -> ("ab";"cadab";"")
  172. I/ decode 24 60 60/1 2 3 -> 3723 2/1 1 0 1 -> 13
  173. I\ encode 24 60 60\3723 -> 1 2 3 2\13 -> 1 1 0 1
  174. i': window 3':"abcdef" -> ("abc";"bcd";"cde";"def")
  175. i f': stencil 3{x,"."}':"abcde" -> ("abc.";"bcd.";"cde.")
  176. F': eachprior -':12 13 11 17 14 -> 12 1 -2 6 -3
  177. x F': seeded ': 10-':12 13 11 17 14 -> 2 1 -2 6 -3
  178. x F/: eachright 1 2*/:3 4 -> (3 6;4 8)
  179. x F\: eachleft 1 2*\:3 4 -> (3 4;6 8)
  180. --------------------------------------------------------------------------------