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