comparison dynamin/core/array.d @ 102:604d20cac836

Add dynamin.core.array and put contains() there.
author Jordan Miner <jminer7@gmail.com>
date Tue, 15 May 2012 21:14:48 -0500
parents
children 73060bc3f004
comparison
equal deleted inserted replaced
101:1690ebff00a0 102:604d20cac836
1 // Written in the D programming language
2 // www.digitalmars.com/d/
3
4 /*
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Dynamin library.
16 *
17 * The Initial Developer of the Original Code is Jordan Miner.
18 * Portions created by the Initial Developer are Copyright (C) 2006-2012
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Jordan Miner <jminer7@gmail.com>
23 *
24 */
25
26 module dynamin.core.array;
27
28 /**
29 * Tests whether or not the specified item is in the specified array.
30 * Returns: true if the specified item is in the array and false otherwise
31 * Examples:
32 * -----
33 * "Hello".contains('e') == true
34 * "Hello".contains('a') == false
35 * "".contains('e') == false
36 * assert([2, 3, 7].contains(3) == true);
37 * assert([2, 3, 7].contains(0) == false);
38 * -----
39 */
40 bool contains(T, U)(T[] arr, U item) {
41 foreach(U item2; arr) {
42 if(item == item2)
43 return true;
44 }
45 return false;
46 }
47
48 unittest {
49 assert("Hello".contains('e') == true);
50 assert("Hello".contains('a') == false);
51 assert("".contains('e') == false);
52 assert([2, 3, 7].contains(3) == true);
53 assert([2, 3, 7].contains(0) == false);
54 }
55