diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2017-05-16 17:29:44 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-05-16 17:29:44 +0200 |
commit | a5695c13afabac6e67ff677d564b6d1a6aeb1af4 (patch) | |
tree | 6a5f1f1b4e808003dfd9851113e789ff2fe051d2 /tests/concepts | |
parent | c3c37dbb1574db5078b86be29804990d153ec1c1 (diff) | |
parent | 3e52bb6535a70339cf4a15123be09916ef0c31f6 (diff) | |
download | Nim-a5695c13afabac6e67ff677d564b6d1a6aeb1af4.tar.gz |
Merge branch 'zahary' into araq
Diffstat (limited to 'tests/concepts')
-rw-r--r-- | tests/concepts/t5642.nim | 25 | ||||
-rw-r--r-- | tests/concepts/tconceptinclosure.nim | 53 | ||||
-rw-r--r-- | tests/concepts/tmisc_issues.nim | 2 | ||||
-rw-r--r-- | tests/concepts/trandom_vars.nim | 42 |
4 files changed, 121 insertions, 1 deletions
diff --git a/tests/concepts/t5642.nim b/tests/concepts/t5642.nim new file mode 100644 index 000000000..d1e7bd1dd --- /dev/null +++ b/tests/concepts/t5642.nim @@ -0,0 +1,25 @@ +discard """ + output: 9 +""" + +type DataTable = concept x + x is object + for f in fields(x): + f is seq + +type Students = object + id : seq[int] + name : seq[string] + age: seq[int] + +proc nrow*(dt: DataTable) : Natural = + var totalLen = 0 + for f in fields(dt): + totalLen += f.len + return totalLen + +let + stud = Students (id : @[1,2,3], name : @["Vas", "Pas", "NafNaf"], age : @[10,16,32]) + +echo nrow(stud) + diff --git a/tests/concepts/tconceptinclosure.nim b/tests/concepts/tconceptinclosure.nim new file mode 100644 index 000000000..23c1bf293 --- /dev/null +++ b/tests/concepts/tconceptinclosure.nim @@ -0,0 +1,53 @@ +discard """ + output: ''' +10 +20 +int +20 +3 +''' +""" + +import typetraits + +type + FonConcept = concept x + x.x is int + + GenericConcept[T] = concept x + x.x is T + const L = T.name.len + + Implementation = object + x: int + + Closure = object + f: proc() + +proc f1(x: FonConcept): Closure = + result.f = proc () = + echo x.x + +proc f2(x: GenericConcept): Closure = + result.f = proc () = + echo x.x + echo GenericConcept.T.name + +proc f3[T](x: GenericConcept[T]): Closure = + result.f = proc () = + echo x.x + echo x.L + +let x = Implementation(x: 10) +let y = Implementation(x: 20) + +let a = x.f1 +let b = x.f2 +let c = x.f1 +let d = y.f2 +let e = y.f3 + +a.f() +d.f() +e.f() + diff --git a/tests/concepts/tmisc_issues.nim b/tests/concepts/tmisc_issues.nim index 10e072521..d9bb84a2f 100644 --- a/tests/concepts/tmisc_issues.nim +++ b/tests/concepts/tmisc_issues.nim @@ -42,7 +42,7 @@ echo p2 is AbstractPointOfFloat # true echo p2.x is float and p2.y is float # true # https://github.com/nim-lang/Nim/issues/2018 -type ProtocolFollower = generic +type ProtocolFollower = concept true # not a particularly involved protocol type ImplementorA = object diff --git a/tests/concepts/trandom_vars.nim b/tests/concepts/trandom_vars.nim new file mode 100644 index 000000000..a236cebad --- /dev/null +++ b/tests/concepts/trandom_vars.nim @@ -0,0 +1,42 @@ +discard """ +output: "11.0" +""" + +type + # A random number generator + Random = object + random: proc(): float + # A generic typeclass for a random var + RandomVar[A] = concept x + var rng: Random + rng.sample(x) is A + # A few concrete instances + Uniform = object + a, b: float + ClosureVar[A] = object + f: proc(rng: var Random): A + +# How to sample from various concrete instances +proc sample(rng: var Random, u: Uniform): float = u.a + (u.b - u.a) * rng.random() + +proc sample[A](rng: var Random, c: ClosureVar[A]): A = c.f(rng) + +proc uniform(a, b: float): Uniform = Uniform(a: a, b: b) + +# How to lift a function on values to a function on random variables +proc map[A, B](x: RandomVar[A], f: proc(a: A): B): ClosureVar[B] = + proc inner(rng: var Random): B = + f(rng.sample(x)) + + result.f = inner + +import future + +proc fakeRandom(): Random = + result.random = () => 0.5 + +let x = uniform(1, 10).map((x: float) => 2 * x) + +var rng = fakeRandom() + +echo rng.sample(x) |