├── .gitignore ├── LICENSE ├── README.md ├── conway ├── ansi.nim ├── conway.nim ├── conwaymap.nim └── nim.cfg └── wordcount.nim /.gitignore: -------------------------------------------------------------------------------- 1 | **/nimcache 2 | conway/conway 3 | wordcount 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Arthur Liao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nim examples 2 | 3 | The examples are written with nim-0.10.2. 4 | 5 | Use 6 | 7 | $ nim c NAME.nim 8 | 9 | or 10 | 11 | $ nim c --opt:speed NAME.nim 12 | 13 | to build, where `NAME.nim` is the project main file. 14 | -------------------------------------------------------------------------------- /conway/ansi.nim: -------------------------------------------------------------------------------- 1 | type 2 | AnsiOp* = enum 3 | CursorUp = 'A', 4 | CursorDown = 'B', 5 | CursorForward = 'C', 6 | CursorBack = 'D', 7 | CursorPos = 'H', 8 | Clear = 'J', 9 | EraseToEOL = 'K' 10 | 11 | proc csi*(op: AnsiOp, x, y: int16 = -1) = 12 | stdout.write("\x1b[") 13 | if x >= 0: stdout.write(x) 14 | if y >= 0: stdout.write(';', y) 15 | if op == AnsiOp.Clear: stdout.write('2') 16 | stdout.write(chr(ord(op))) 17 | 18 | -------------------------------------------------------------------------------- /conway/conway.nim: -------------------------------------------------------------------------------- 1 | import conwaymap 2 | import os, threadpool 3 | 4 | const pauseMillis = 20 5 | const defaultCount = 300 6 | const initialMap = [ 7 | " 1 ", 8 | " 1 1 ", 9 | " 11 11 11", 10 | " 1 1 11 11", 11 | "11 1 1 11 ", 12 | "11 1 1 11 1 1 ", 13 | " 1 1 1 ", 14 | " 1 1 ", 15 | " 11 ", 16 | ] 17 | 18 | proc enterToQuit() = 19 | discard readLine(stdin) 20 | quit() 21 | 22 | proc main() = 23 | var map: ConwayMap 24 | map.init(initialMap) 25 | # Press ENTER to exit" 26 | spawn enterToQuit() 27 | # Start the game 28 | for i in 1..defaultCount: 29 | map.print() 30 | echo "n = ", i, "\t Press ENTER to exit" 31 | os.sleep(pauseMillis) 32 | map.next() 33 | 34 | when isMainModule: 35 | main() 36 | 37 | -------------------------------------------------------------------------------- /conway/conwaymap.nim: -------------------------------------------------------------------------------- 1 | import ansi 2 | import sequtils, strutils 3 | 4 | const mapWidth = 40 5 | const mapHeight = 30 6 | 7 | type 8 | Cell = bool 9 | ConwayMap* = array[0.. = 2 and nlive <= 3 43 | else: map[i][j] = nlive == 3 44 | 45 | -------------------------------------------------------------------------------- /conway/nim.cfg: -------------------------------------------------------------------------------- 1 | threads:on 2 | -------------------------------------------------------------------------------- /wordcount.nim: -------------------------------------------------------------------------------- 1 | import 2 | parseopt2, pegs, 3 | algorithm, sequtils, strutils, tables 4 | 5 | const usageString = 6 | """Usage: wordcount [OPTIONS] [FILES] 7 | 8 | Options: 9 | -o:NAME set output file name 10 | -i --ignore-case ignore case 11 | -h --help print this help menu 12 | """ 13 | 14 | proc doWork(inFilenames: seq[string] = nil, 15 | outFilename: string = nil, 16 | ignoreCase: bool = false) {.raises: [IOError].} = 17 | # Open files 18 | var 19 | infiles: seq[File] = @[stdin] 20 | outfile: File = stdout 21 | if inFilenames != nil and inFilenames.len > 0: 22 | infiles = inFilenames.mapIt(File, (proc (filename: string): File = 23 | if not open(result, filename): 24 | raise newException(IOError, "Failed to open file: " & filename) 25 | )(it)) 26 | if outFilename != nil and outFilename.len > 0 and not open(outfile, outFilename, fmWrite): 27 | raise newException(IOError, "Failed to open file: " & outFilename) 28 | 29 | # Parse words 30 | var counts = initCountTable[string]() 31 | for infile in infiles: 32 | for line in infile.lines: 33 | let input = if ignoreCase: line.tolower() else: line 34 | let words = try: input.findAll(peg"\w+") except: @[] 35 | for word in words: 36 | counts.inc(word) 37 | 38 | # Write counts 39 | var words = toSeq(counts.keys) 40 | sort(words, cmp) 41 | for word in words: 42 | outfile.writeln(counts[word], '\t', word) 43 | 44 | proc main() = 45 | # Parse arguments 46 | var inFilenames: seq[string] = @[] 47 | var outFilename: string = nil 48 | var ignoreCase = false 49 | for kind, key, val in getopt(): 50 | case kind 51 | of cmdArgument: 52 | inFilenames.add(key) 53 | of cmdShortOption, cmdLongOption: 54 | case key 55 | of "help", "h": echo usageString; return 56 | of "ignore-case", "i": ignoreCase = true 57 | of "o": outFilename = val 58 | else: discard 59 | of cmdEnd: discard 60 | doWork(inFilenames, outFilename, ignoreCase) 61 | 62 | when isMainModule: 63 | main() 64 | 65 | --------------------------------------------------------------------------------