OLD | NEW |
(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 // DartOptions=--generic-method-syntax |
| 6 // AnalyzerOptions=--options=./generic_functions_test.options |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 |
| 10 // Dart test verifying that the parser can handle type parameterization of |
| 11 // function declarations and function invocations. Variant of code from |
| 12 // DEP #22, adjusted to use generic top level functions. |
| 13 |
| 14 class BinaryTreeNode<K extends Comparable<K>, V> { |
| 15 final K _key; |
| 16 final V _value; |
| 17 final BinaryTreeNode<K, V> _left; |
| 18 final BinaryTreeNode<K, V> _right; |
| 19 |
| 20 BinaryTreeNode(this._key, this._value, |
| 21 {BinaryTreeNode<K, V> left: null, BinaryTreeNode<K, V> right: null}) : |
| 22 _left = left, _right = right; |
| 23 |
| 24 BinaryTreeNode<K, V> insert(K key, V value) { |
| 25 int c = key.compareTo(_key); |
| 26 if (c == 0) return this; |
| 27 var _insert = (BinaryTreeNode<K, V> t, K key, V value) => |
| 28 insertOpt<K, V>(t, key, value); |
| 29 BinaryTreeNode<K, V> left = _left; |
| 30 BinaryTreeNode<K, V> right = _right; |
| 31 if (c < 0) { |
| 32 left = _insert(_left, key, value); |
| 33 } else { |
| 34 right = _insert(_right, key, value); |
| 35 } |
| 36 return new BinaryTreeNode<K, V>(_key, _value, left: left, right: right); |
| 37 } |
| 38 |
| 39 BinaryTreeNode<K, U> map<U>(U f(V x)){ |
| 40 var _map = (BinaryTreeNode<K, V> t, U f(V x)) => mapOpt<K, V, U>(t, f); |
| 41 return new BinaryTreeNode<K, U>( |
| 42 _key, |
| 43 f(_value), |
| 44 left: _map(_left, f), |
| 45 right: _map(_right, f)); |
| 46 } |
| 47 |
| 48 S foldPre<S>(S init, S f(V t, S s)) { |
| 49 var _fold = (BinaryTreeNode<K, V> t, S s, S f(V t, S s)) => |
| 50 foldPreOpt<K, V, S>(t, s, f); |
| 51 S s = init; |
| 52 s = f(_value, s); |
| 53 s = _fold(_left, s, f); |
| 54 s = _fold(_right, s, f); |
| 55 return s; |
| 56 } |
| 57 } |
| 58 |
| 59 // Use fresh type variables. |
| 60 BinaryTreeNode<K2, V2> insertOpt<K2 extends Comparable<K2>, V2>( |
| 61 BinaryTreeNode<K2, V2> t, K2 key, V2 value) { |
| 62 return (t == null) ? new BinaryTreeNode(key, value) : t.insert(key, value); |
| 63 } |
| 64 |
| 65 // Reuse type variables [K], [V] to test shadowing. |
| 66 BinaryTreeNode<K, U> mapOpt<K extends Comparable<K>, V, U>( |
| 67 BinaryTreeNode<K, V> t, U f(V x)) { |
| 68 return (t == null) ? null : t.map<U>(f); |
| 69 } |
| 70 |
| 71 // Use fresh [K2], shadowing [V]. |
| 72 S foldPreOpt<K2 extends Comparable<K2>, V, S>( |
| 73 BinaryTreeNode<K2, V> t, S init, S f(V t, S s)) { |
| 74 return (t == null) ? init : t.foldPre<S>(init, f); |
| 75 } |
| 76 |
| 77 class BinaryTree<K extends Comparable<K>, V> { |
| 78 final BinaryTreeNode<K, V> _root; |
| 79 |
| 80 BinaryTree._internal(this._root); |
| 81 BinaryTree.empty() : this._internal(null); |
| 82 |
| 83 BinaryTree<K, V> insert(K key, V value) { |
| 84 BinaryTreeNode<K, V> root = insertOpt<K, V>(_root, key, value); |
| 85 return new BinaryTree<K, V>._internal(root); |
| 86 } |
| 87 |
| 88 BinaryTree<K, U> map<U>(U f(V x)) { |
| 89 BinaryTreeNode<K, U> root = mapOpt<K, V, U>(_root, f); |
| 90 return new BinaryTree<K, U>._internal(root); |
| 91 } |
| 92 |
| 93 S foldPre<S>(S init, S f(V t, S s)) { |
| 94 return foldPreOpt<K, V, S>(_root, init, f); |
| 95 } |
| 96 } |
| 97 |
| 98 main() { |
| 99 BinaryTree<num, String> sT = new BinaryTree<num, String>.empty(); |
| 100 |
| 101 sT = sT.insert(0, ""); |
| 102 sT = sT.insert(1, " "); |
| 103 sT = sT.insert(2, " "); |
| 104 sT = sT.insert(3, " "); |
| 105 |
| 106 BinaryTree<num, num> iT = sT.map<num>((String s) => s.length); |
| 107 |
| 108 Expect.equals(iT.foldPre<num>(0, (int i, num s) => i + s), 6); |
| 109 } |
OLD | NEW |