This file contains hidden or 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
# http://unriskinsight.blogspot.de/2014/06/fast-functional-goats-lions-and-wolves.html | |
# | |
# Goats Wolves Lions C++11 Nimrod | |
# 17 55 6 0.00 0.03 | |
# 117 155 106 0.17 0.13 | |
# 217 255 206 0.75 0.62 | |
# 317 355 306 2.16 1.89 | |
# 417 455 406 5.28 4.34 | |
# 517 555 506 10.75 8.42 | |
# 617 655 606 19.15 14.45 |
This file contains hidden or 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
# http://unriskinsight.blogspot.de/2014/06/fast-functional-goats-lions-and-wolves.html | |
# | |
# Goats Wolves Lions C++11 Nimrod (functional) | |
# 17 55 6 0.00 0.00 | |
# 117 155 106 0.17 0.31 | |
# 217 255 206 0.75 1.88 | |
# 317 355 306 2.16 5.81 | |
# 417 455 406 5.28 14.16 | |
# 517 555 506 10.75 27.72 | |
# 617 655 606 19.15 46.19 |
This file contains hidden or 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
type | |
Time = distinct float | |
Frequency = distinct float | |
Radioactivity = distinct float | |
proc `/` *(x: float, y: Time): Frequency = Frequency(x / float(y)) | |
proc `/` *(x: float, y: Time): Radioactivity = Radioactivity(x / float(y)) | |
# Error: ambiguous call; both ambiguous./(x: float, y: Time): Frequency and ambiguous./(x: float, y: Time): Radioactivity match for: (int literal(1), Time) | |
var fr: Frequency = 1 / Time(1) |
This file contains hidden or 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
# 3.5 seconds | |
# compute average line length | |
var count = 0 | |
var sum = 0 | |
for line in stdin.lines: | |
count += 1 | |
sum += line.len | |
# Not much faster |
This file contains hidden or 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
# echo > bar.nim | |
# nimrod pretty bar.nim | |
config/nimrod.cfg(38, 2) Hint: added path: '/home/def/.babel/pkgs/' [Path] | |
Hint: used config file '/home/def/nimrod/config/nimrod.cfg' [Conf] | |
Hint: system [Processing] | |
lib/system.nim(53, 9) Error: name should be: Ptr | |
lib/system.nim(54, 9) Error: name should be: Ref | |
lib/system.nim(53, 2) Error: name should be: Ptr | |
lib/system.nim(54, 2) Error: name should be: Ref | |
lib/system.nim(129, 10) Error: name should be: t |
This file contains hidden or 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
# 0.19 seconds, 79 MB/s | |
proc fgets(c: cstring, n: int, f: TFile): cstring {. | |
importc: "fgets", header: "<stdio.h>", tags: [FReadIO].} | |
proc myReadLine(f: TFile, line: var TaintedString): bool = | |
var buf {.noinit.}: array[8192, char] | |
setLen(line.string, 0) | |
result = true | |
while true: |
This file contains hidden or 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
import opengl | |
import glut | |
proc paint() {.cdecl.} = | |
glClearColor(0.3,0.3,0.3,0.0) | |
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) | |
glShadeModel(GL_SMOOTH) | |
glLoadIdentity() |
This file contains hidden or 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
import sequtils, math | |
template any(sequence, operation: expr): expr = | |
var result {.gensym.}: bool = false | |
for i in 0 .. <sequence.len: | |
let it {.inject.} = sequence[i] | |
result = operation | |
if result: | |
break | |
result |
This file contains hidden or 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
import sets | |
const | |
moves = 8 | |
knight: array[moves, tuple[x, y: int]] = | |
[(1, 2), (1, -2), (2, 1), (2, -1), (-1, 2), (-1, -2), (-2, 1), (-2, -1)] | |
start = 2 | |
board_size = 6 | |
bs2 = board_size * board_size |
This file contains hidden or 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
from tables import TTable, initTable | |
proc f(n: int): TTable[int, seq[int]] = | |
result = initTable[int, seq[int]]() | |
var t = f(10) |
OlderNewer