| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Dart test verifying that the parser can handle type parameterization of | 5 /// Dart test verifying that the parser can handle type parameterization of |
| 6 /// function declarations and function invocations. Variant of code from | 6 /// function declarations and function invocations. Variant of code from |
| 7 /// DEP #22, adjusted to use generic top level functions. | 7 /// DEP #22, adjusted to use generic top level functions. |
| 8 | 8 |
| 9 library generic_functions_test; | 9 library generic_functions_test; |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 var _fold = (BinaryTreeNode<K, V> t, S s, S f(V t, S s)) => | 48 var _fold = (BinaryTreeNode<K, V> t, S s, S f(V t, S s)) => |
| 49 foldPreOpt<K, V, S>(t, s, f); | 49 foldPreOpt<K, V, S>(t, s, f); |
| 50 S s = init; | 50 S s = init; |
| 51 s = f(_value, s); | 51 s = f(_value, s); |
| 52 s = _fold(_left, s, f); | 52 s = _fold(_left, s, f); |
| 53 s = _fold(_right, s, f); | 53 s = _fold(_right, s, f); |
| 54 return s; | 54 return s; |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Use fresh type variables. | |
| 59 BinaryTreeNode<K2, V2> insertOpt<K2 extends Comparable<K2>, V2>( | 58 BinaryTreeNode<K2, V2> insertOpt<K2 extends Comparable<K2>, V2>( |
| 60 BinaryTreeNode<K2, V2> t, K2 key, V2 value) { | 59 BinaryTreeNode<K2, V2> t, K2 key, V2 value) { |
| 61 return (t == null) ? new BinaryTreeNode(key, value) : t.insert(key, value); | 60 return (t == null) ? new BinaryTreeNode(key, value) : t.insert(key, value); |
| 62 } | 61 } |
| 63 | 62 |
| 64 // Reuse type variables [K], [V] to test shadowing. | |
| 65 BinaryTreeNode<K, U> mapOpt<K extends Comparable<K>, V, U>( | 63 BinaryTreeNode<K, U> mapOpt<K extends Comparable<K>, V, U>( |
| 66 BinaryTreeNode<K, V> t, U f(V x)) { | 64 BinaryTreeNode<K, V> t, U f(V x)) { |
| 67 return (t == null) ? null : t.map<U>(f); | 65 return (t == null) ? null : t.map<U>(f); |
| 68 } | 66 } |
| 69 | 67 |
| 70 // Use fresh [K2], shadowing [V]. | |
| 71 S foldPreOpt<K2 extends Comparable<K2>, V, S>( | 68 S foldPreOpt<K2 extends Comparable<K2>, V, S>( |
| 72 BinaryTreeNode<K2, V> t, S init, S f(V t, S s)) { | 69 BinaryTreeNode<K2, V> t, S init, S f(V t, S s)) { |
| 73 return (t == null) ? init : t.foldPre<S>(init, f); | 70 return (t == null) ? init : t.foldPre<S>(init, f); |
| 74 } | 71 } |
| 75 | 72 |
| 76 class BinaryTree<K extends Comparable<K>, V> { | 73 class BinaryTree<K extends Comparable<K>, V> { |
| 77 final BinaryTreeNode<K, V> _root; | 74 final BinaryTreeNode<K, V> _root; |
| 78 | 75 |
| 79 BinaryTree._internal(this._root); | 76 BinaryTree._internal(this._root); |
| 80 BinaryTree.empty() : this._internal(null); | 77 BinaryTree.empty() : this._internal(null); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 99 | 96 |
| 100 sT = sT.insert(0, ""); | 97 sT = sT.insert(0, ""); |
| 101 sT = sT.insert(1, " "); | 98 sT = sT.insert(1, " "); |
| 102 sT = sT.insert(2, " "); | 99 sT = sT.insert(2, " "); |
| 103 sT = sT.insert(3, " "); | 100 sT = sT.insert(3, " "); |
| 104 | 101 |
| 105 BinaryTree<num, num> iT = sT.map<num>((String s) => s.length); | 102 BinaryTree<num, num> iT = sT.map<num>((String s) => s.length); |
| 106 | 103 |
| 107 Expect.equals(iT.foldPre<num>(0, (int i, num s) => i + s), 6); | 104 Expect.equals(iT.foldPre<num>(0, (int i, num s) => i + s), 6); |
| 108 } | 105 } |
| OLD | NEW |