Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: tests/language/generic_functions_test.dart

Issue 1723443003: First step of support for parsing and ignoring generic methods. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import "package:expect/expect.dart";
6
7 // Dart test verifying that the parser can handle type parameterization of
8 // function declarations and function invocations. Variant of code from
9 // DEP #22, adjusted to use generic top level functions.
10
11 class BinaryTreeNode<K extends Comparable<K>, V> {
12 final K _key;
13 final V _value;
14 final BinaryTreeNode<K, V> _left;
15 final BinaryTreeNode<K, V> _right;
16
17 BinaryTreeNode(this._key, this._value,
18 {BinaryTreeNode<K, V> left: null, BinaryTreeNode<K, V> right: null}) :
19 _left = left, _right = right;
20
21 BinaryTreeNode<K, V> insert(K key, V value) {
22 int c = key.compareTo(_key);
23 if (c == 0) return this;
24 var _insert = (BinaryTreeNode<K2, V2> t, K2 key, V2 value) =>
25 insertOpt<K, V>(t, key, value);
26 BinaryTreeNode<K, V> left = _left;
27 BinaryTreeNode<K, V> right = _right;
28 if (c < 0) {
29 left = _insert(_left, key, value);
30 } else {
31 right = _insert(_right, key, value);
32 }
33 return new BinaryTreeNode<K, V>(_key, _value, left: left, right: right);
34 }
35
36 BinaryTreeNode<K, U> map<U>(U f(V x)){
37 var _map = (BinaryTreeNode<K, V> t, U f(V x)) => mapOpt<K, V, U>(t, f);
38 return new BinaryTreeNode<K, U>(
39 _key,
40 f(_value),
41 left: _map(_left, f),
42 right: _map(_right, f));
43 }
44
45 S foldPre<S>(S init, S f(V t, S s)) {
46 var _fold = (BinaryTreeNode<K2, V> t, S s, S f(V t, S s)) =>
47 foldPreOpt<K, V, S>(t, s, f);
48 S s = init;
49 s = f(_value, s);
50 s = _fold(_left, s, f);
51 s = _fold(_right, s, f);
52 return s;
53 }
54 }
55
56 // Use fresh type variables.
57 BinaryTreeNode<K2, V2> insertOpt<K2, V2>(BinaryTreeNode<K2, V2> t, K2 key,
58 V2 value) {
59 return (t == null) ? new BinaryTreeNode(key, value) : t.insert(key, value);
60 }
61
62 // Reuse type variables [K], [V] to test shadowing.
63 BinaryTreeNode<K, V> mapOpt<K extends Comparable<K>, V, U>(
64 BinaryTreeNode<K, V> t, U f(V x)) {
65 return (t == null) ? null : t.map<U>(f);
66 }
67
68 // Use fresh [K2], shadowing [V].
69 S foldPreOpt<K2 extends Comparable<K2>, V, S>(
70 BinaryTreeNode<K2, V> t, S init, S f(V t, S s)) {
71 return (t == null) ? init : t.foldPre<S>(init, f);
72 }
73
74 class BinaryTree<K extends Comparable<K>, V> {
75 final BinaryTreeNode<K, V> _root;
76
77 BinaryTree._internal(this._root);
78 BinaryTree.empty() : this._internal(null);
79
80 BinaryTree<K, V> insert(K key, V value) {
81 BinaryTree<K, V> root = insertOpt<K, V>(_root, key, value);
82 return new BinaryTree<K, V>._internal(root);
83 }
84
85 BinaryTree<K, V> map<U>(U f(V x)) {
86 BinaryTreeNode<K, V> root = mapOpt<K, V, U>(_root, f);
87 return new BinaryTree<K, V>._internal(root);
88 }
89
90 S foldPre<S>(S init, S f(V t, S s)) {
91 return foldPreOpt<K, V, S>(_root, init, f);
92 }
93 }
94
95 main() {
96 BinaryTree<num, String> sT = new BinaryTree<num, String>.empty();
97
98 sT = sT.insert(0, "");
99 sT = sT.insert(1, " ");
100 sT = sT.insert(2, " ");
101 sT = sT.insert(3, " ");
102
103 BinaryTree<num, num> iT = sT.map<num>((String s) => s.length);
104
105 Expect.equals(iT.foldPre<num>(0, (int i, num s) => i + s), 6);
106 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698