From 91700f29e718e41026b536d72b9ff010a4574bf2 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 22 Jan 2013 20:29:07 +0100 Subject: Renames each proc to map, each is left deprecated. --- lib/pure/collections/sequtils.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 73713eec9..298e7f27e 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -12,8 +12,8 @@ ## This module implements operations for the built-in `seq`:idx: type which ## were inspired by functional programming languages. If you are looking for ## the typical `map` function which applies a function to every element in a -## sequence, it already exists as the `each` proc in the `system -## `_ module in both mutable and immutable styles. +## sequence, it already exists in the `system `_ module in both +## mutable and immutable styles. ## ## Also, for functional style programming you may want to pass `anonymous procs ## `_ to procs like ``filter`` to reduce typing. -- cgit 1.4.1-2-gfad0 From 5aced9186d2edb6010d7d2bd0fdb1dfa317d73b8 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 15 Jan 2013 20:54:35 +0100 Subject: Adds randomize(seed) for repeatable pseudo random numbers. Also fixes srand48() type to clong. --- lib/pure/math.nim | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 53594db62..f9ab6d0f8 100755 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -141,6 +141,11 @@ proc randomize*() ## number, i.e. a tickcount. Note: Does nothing for the ECMAScript target, ## as ECMAScript does not support this. +proc randomize*(seed: int) + ## initializes the random number generator with a specific seed. + ## Note: Does nothing for the ECMAScript target, + ## as ECMAScript does not support this. + when not defined(ECMAScript): proc sqrt*(x: float): float {.importc: "sqrt", header: "".} ## computes the square root of `x`. @@ -190,15 +195,17 @@ when not defined(ECMAScript): proc rand(): cint {.importc: "rand", nodecl.} when not defined(windows): - proc srand48(seed: cint) {.importc: "srand48", nodecl.} + proc srand48(seed: clong) {.importc: "srand48", nodecl.} proc drand48(): float {.importc: "drand48", nodecl.} proc random(max: float): float = result = drand48() * max proc randomize() = - let x = gettime(nil) - srand(x) - when defined(srand48): srand48(x) + randomize(gettime(nil)) + + proc randomize(seed: int) = + srand(cint(seed)) + when defined(srand48): srand48(seed) proc random(max: int): int = result = int(rand()) mod max @@ -217,6 +224,7 @@ else: proc random(max: float): float = result = float(mathrandom() * float(max)) proc randomize() = nil + proc randomize(seed: int) = nil proc sqrt*(x: float): float {.importc: "Math.sqrt", nodecl.} proc ln*(x: float): float {.importc: "Math.log", nodecl.} @@ -301,3 +309,18 @@ proc standardDeviation*(s: TRunningStat): float = {.pop.} {.pop.} + +when isMainModule and not defined(ECMAScript): + # Verifies random seed initialization. + let seed = gettime(nil) + randomize(seed) + const SIZE = 10 + var buf : array[0..SIZE, int] + # Fill the buffer with random values + for i in 0..SIZE-1: + buf[i] = random(high(int)) + # Check that the second random calls are the same for each position. + randomize(seed) + for i in 0..SIZE-1: + assert buf[i] == random(high(int)), "non deterministic random seeding" + echo "random values equal after reseeding" -- cgit 1.4.1-2-gfad0 From c4743805d9a45cf4bd2fe1535e29a1b953cfe876 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Fri, 25 Jan 2013 21:43:54 +0000 Subject: Added strutils.unescape and fixed issue with strutils.escape. --- lib/pure/strutils.nim | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'lib/pure') diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 8a5061037..8b64434d8 100755 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -850,13 +850,51 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect, for c in items(s): case c of '\0'..'\31', '\128'..'\255': - add(result, '\\') + add(result, "\\x") add(result, toHex(ord(c), 2)) of '\\': add(result, "\\\\") of '\'': add(result, "\\'") of '\"': add(result, "\\\"") else: add(result, c) add(result, suffix) + +proc unescape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect, + rtl, extern: "nsuUnescape".} = + ## Unescapes a string `s`. This complements ``escape`` as it performs the + ## opposite operations. + ## + ## If `s` does not begin with ``prefix`` and end with ``suffix`` a EInvalidValue + ## exception will be raised. + result = newStringOfCap(s.len) + var i = 0 + if s[0 .. prefix.len-1] != prefix: + raise newException(EInvalidValue, + "String does not start with a prefix of: " & prefix) + i.inc() + while True: + if i == s.len-suffix.len: break + case s[i] + of '\\': + case s[i+1]: + of 'x': + let j = parseHexInt(s[i+2 .. i+3]) + result.add(chr(j)) + inc(i, 2) + of '\\': + result.add('\\') + of '\'': + result.add('\'') + of '\"': + result.add('\"') + else: result.add("\\" & s[i+1]) + inc(i) + of '\0': break + else: + result.add(s[i]) + i.inc() + if s[i .. -1] != suffix: + raise newException(EInvalidValue, + "String does not end with a suffix of: " & suffix) proc validIdentifier*(s: string): bool {.noSideEffect, rtl, extern: "nsuValidIdentifier".} = -- cgit 1.4.1-2-gfad0