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 // alias Action1 = ActionN;
51 // alias Action2 = ActionN;
52 // alias Action3 = ActionN;
53 // alias Action4 = ActionN;
54 // alias Action5 = ActionN;
55 // alias Action6 = ActionN;
56 
57 /**
58  * A vector-argument action.
59  */
60 // alias ActionN(T) = void delegate(T[] args...);
61 
62 template ActionN(T...) if(T.length > 0)  {
63     alias ActionN = void delegate(T);
64 }
65 
66 /**
67  *  Represents a function.
68  */
69 alias Func(R) = R delegate();
70 
71 /**
72  *  Represents a function with one argument.
73  */
74 alias Func1(T1, R) = R delegate(T1 t1);
75 
76 /**
77  *  Represents a function with two arguments.
78  */
79 alias Func2(T1, T2, R) = R delegate(T1 t1, T2 t2);
80 
81 /**
82  *  Represents a function with three arguments.
83  */
84 alias Func3(T1, T2, T3, R) = R delegate(T1 t1, T2 t2, T3 t3);
85 
86 /**
87  *  Represents a function with four arguments.
88  */
89 alias Func4(T1, T2, T3, T4, R) = R delegate(T1 t1, T2 t2, T3 t3, T4 t4);
90 
91 /**
92  * Represents a function with five arguments.
93  */
94 alias Func5(T1, T2, T3, T4, T5, R) = R delegate(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5);
95 
96 /**
97  * Represents a function with six arguments.
98  */
99 alias Func6(T1, T2, T3, T4, T5, T6, R) = R delegate(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6);
100 
101 alias FuncN(T, R) = R delegate(T[] args...);
102 
103 
104 // template Function(R, T...) {
105 //     alias Function = R delegate(T);
106 // }
107 
108 /**
109 */
110 class EventArgs {
111 
112 }
113 
114 
115 alias EventHandler = ActionN!(Object, EventArgs); 
116 alias SimpleEventHandler = Action;
117 alias SimpleActionHandler = ActionN!(Object);
118 
119 /**
120  * Represents an operation that accepts a single input argument and returns no
121  * result. Unlike most other functional interfaces, {@code Consumer} is expected
122  * to operate via side-effects.
123  *
124  */
125 alias Consumer = Action1;
126 // alias Consumer(T) = void delegate(T t);
127 
128 /**
129 */
130 alias Function = Func1;
131 // alias Function(T, U) = U delegate(T);
132 
133 /**
134  * Represents a supplier of results.
135  *
136  * <p>There is no requirement that a new or distinct result be returned each
137  * time the supplier is invoked.
138  *
139  */
140 alias Supplier = Func;
141 // alias Supplier(T) = T delegate();
142 
143 /**
144 */
145 alias Predicate(T) = bool delegate(T t);
146 
147 /**
148  * Represents an operation that accepts two input arguments and returns no
149  * result.  This is the two-arity specialization of {@link Consumer}.
150  * Unlike most other functional interfaces, {@code BiConsumer} is expected
151  * to operate via side-effects.
152  *
153  * <p>This is a <a href="package-summary.html">functional interface</a>
154  * whose functional method is {@link #accept(Object, Object)}.
155  *
156  * @param <T> the type of the first argument to the operation
157  * @param <U> the type of the second argument to the operation
158  *
159  * @see Consumer
160  */
161 alias BiConsumer = Action2;
162 // alias BiConsumer(T, U) = void delegate(T t, U u);
163 
164 
165 
166 /**
167  * Represents a function that accepts two arguments and produces a result.
168  * This is the two-arity specialization of {@link Function}.
169  *
170  * <p>This is a <a href="package-summary.html">functional interface</a>
171  * whose functional method is {@link #apply(Object, Object)}.
172  *
173  * @param <T> the type of the first argument to the function
174  * @param <U> the type of the second argument to the function
175  * @param <R> the type of the result of the function
176  *
177  * @see Function
178  */
179 alias BiFunction = Func2; 
180 //  alias BiFunction(T, U, R) = R delegate(T t, U u);
181 
182 
183 
184 size_t hashCode(T)(T[] a...) {
185     if (a is null)
186         return 0;
187 
188     size_t result = 1;
189 
190     foreach (T element ; a)
191         result = 31 * result + (element == T.init ? 0 : hashOf(element));
192 
193     return result;
194 }
195 
196 
197 bool hasKey(K, V)(V[K] arr, K key) {
198     auto v = key in arr;
199     return v !is null;
200 }
201 
202 unittest {
203     string[int] arr;
204     arr[1001] = "1001";
205     arr[1002] = "1002";
206 
207     assert(arr.hasKey(1001));
208     assert(!arr.hasKey(1003));
209 }