1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.Functions;
13 
14 
15 /**
16  * An action.
17  */
18 alias Action = void delegate();
19 
20 /**
21  * A one-argument action.
22  */
23 alias Action1(T) = void delegate(T t);
24 
25 /**
26  * A two-argument action.
27  */
28 alias Action2(T1, T2) = void delegate(T1 t1, T2 t2);
29 
30 /**
31  * A three-argument action.
32  */
33 alias Action3(T1, T2, T3) = void delegate(T1 t1, T2 t2, T3 t3);
34 
35 /**
36  * A four-argument action.
37  */
38 alias Action4(T1, T2, T3, T4) = void delegate(T1 t1, T2 t2, T3 t3, T4 t4);
39 
40 /**
41  * A five-argument action.
42  */
43 alias Action5(T1, T2, T3, T4, T5) = void delegate(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5);
44 
45 /**
46  * A six-argument action.
47  */
48 alias Action6(T1, T2, T3, T4, T5, T6) = void delegate(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6);
49 
50 
51 /**
52  * A vector-argument action.
53  */
54 alias ActionN(T) = void delegate(T[] args...);
55 
56 
57 /**
58  *  Represents a function.
59  */
60 alias Func(R) = R delegate();
61 
62 /**
63  *  Represents a function with one argument.
64  */
65 alias Func1(T1, R) = R delegate(T1 t1);
66 
67 /**
68  *  Represents a function with two arguments.
69  */
70 alias Func2(T1, T2, R) = R delegate(T1 t1, T2 t2);
71 
72 /**
73  *  Represents a function with three arguments.
74  */
75 alias Func3(T1, T2, T3, R) = R delegate(T1 t1, T2 t2, T3 t3);
76 
77 /**
78  *  Represents a function with four arguments.
79  */
80 alias Func4(T1, T2, T3, T4, R) = R delegate(T1 t1, T2 t2, T3 t3, T4 t4);
81 
82 /**
83  * Represents a function with five arguments.
84  */
85 alias Func5(T1, T2, T3, T4, T5, R) = R delegate(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5);
86 
87 /**
88  * Represents a function with six arguments.
89  */
90 alias Func6(T1, T2, T3, T4, T5, T6, R) = R delegate(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6);
91 
92 alias FuncN(T, R) = R delegate(T[] args...);
93 
94 
95 /**
96 */
97 class EventArgs {
98 
99 }
100 
101 
102 alias EventHandler = void delegate(Object sender, EventArgs args);
103 alias ErrorEventHandler = void delegate(string message);
104 
105 alias SimpleEventHandler = Action;
106 
107 /**
108  * Represents an operation that accepts a single input argument and returns no
109  * result. Unlike most other functional interfaces, {@code Consumer} is expected
110  * to operate via side-effects.
111  *
112  */
113 alias Consumer = Action1;
114 // alias Consumer(T) = void delegate(T t);
115 
116 /**
117 */
118 alias Function = Func1;
119 // alias Function(T, U) = U delegate(T);
120 
121 /**
122  * Represents a supplier of results.
123  *
124  * <p>There is no requirement that a new or distinct result be returned each
125  * time the supplier is invoked.
126  *
127  */
128 alias Supplier = Func;
129 // alias Supplier(T) = T delegate();
130 
131 /**
132 */
133 alias Predicate(T) = bool delegate(T t);
134 
135 /**
136  * Represents an operation that accepts two input arguments and returns no
137  * result.  This is the two-arity specialization of {@link Consumer}.
138  * Unlike most other functional interfaces, {@code BiConsumer} is expected
139  * to operate via side-effects.
140  *
141  * <p>This is a <a href="package-summary.html">functional interface</a>
142  * whose functional method is {@link #accept(Object, Object)}.
143  *
144  * @param <T> the type of the first argument to the operation
145  * @param <U> the type of the second argument to the operation
146  *
147  * @see Consumer
148  */
149 alias BiConsumer = Action2;
150 // alias BiConsumer(T, U) = void delegate(T t, U u);
151 
152 
153 
154 /**
155  * Represents a function that accepts two arguments and produces a result.
156  * This is the two-arity specialization of {@link Function}.
157  *
158  * <p>This is a <a href="package-summary.html">functional interface</a>
159  * whose functional method is {@link #apply(Object, Object)}.
160  *
161  * @param <T> the type of the first argument to the function
162  * @param <U> the type of the second argument to the function
163  * @param <R> the type of the result of the function
164  *
165  * @see Function
166  */
167 alias BiFunction = Func2; 
168 //  alias BiFunction(T, U, R) = R delegate(T t, U u);
169 
170 
171 
172 size_t hashCode(T)(T[] a...) {
173     if (a is null)
174         return 0;
175 
176     size_t result = 1;
177 
178     foreach (T element ; a)
179         result = 31 * result + (element == T.init ? 0 : hashOf(element));
180 
181     return result;
182 }
183 
184 
185 bool hasKey(K, V)(V[K] arr, K key) {
186     auto v = key in arr;
187     return v !is null;
188 }
189 
190 unittest {
191     string[int] arr;
192     arr[1001] = "1001";
193     arr[1002] = "1002";
194 
195     assert(arr.hasKey(1001));
196     assert(!arr.hasKey(1003));
197 }