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