How to do a COUNT(*) with GROUP BY in Kotlin?
If contacts is of type List<Contact> you can do the following: val numOccurencesMap = contacts.groupingBy { it.name }.eachCount() numOccurencesMap will be of type Map<String, Int>.
If contacts is of type List<Contact> you can do the following: val numOccurencesMap = contacts.groupingBy { it.name }.eachCount() numOccurencesMap will be of type Map<String, Int>.
Well, you notice that Haskell has no syntax for loops? No while or do or for. Because these are all just higher-order functions: map :: (a -> b) -> [a] -> [b] foldr :: (a -> b -> b) -> b -> [a] -> b filter :: (a -> Bool) -> [a] -> [a] unfoldr … Read more
In Scala 2.8: val baz = (foo, bar).zipped map (_ + _) And it works for more than two operands in the same way. I.e. you could then follow this up with: (foo, bar, baz).zipped map (_ * _ * _)
Any time you want to generate a list based another list: # Double all elements of a list my @double = map { $_ * 2 } (1,2,3,4,5); # @double = (2,4,6,8,10); Since lists are easily converted pairwise into hashes, if you want a hash table for objects based on a particular attribute: # @user_objects … Read more
Store your filter functions in an array and have array.reduce() run through each filter, applying it to the data. This comes at the cost of running through all of them even when there’s no more data to filter. const data = […] const filters = [f1, f2, f3, …] const filteredData = filters.reduce((d, f) => … Read more
Someone has already implememented this on the Clojure group. You can specify how many args a function has, and it will curry itself for you until it gets that many. The reason this doesn’t happen by default in Clojure is that we prefer variadic functions to auto-curried functions, I suppose.
this is how i do it with extension function and a class fun<T> Call<T>.enqueue(callback: CallBackKt<T>.() -> Unit) { val callBackKt = CallBackKt<T>() callback.invoke(callBackKt) this.enqueue(callBackKt) } class CallBackKt<T>: Callback<T> { var onResponse: ((Response<T>) -> Unit)? = null var onFailure: ((t: Throwable?) -> Unit)? = null override fun onFailure(call: Call<T>, t: Throwable) { onFailure?.invoke(t) } override fun … Read more
Let’s have a look at def foldLeftViaFoldRight[A,B](l: List[A], z: B)(f: (B,A) => B): B = foldRight(l, (b:B) => b)((a,g) => b => g(f(b,a)))(z) (the other fold is similar). The trick is that during the right fold operation, we don’t build the final value of type B. Instead, we build a function from B to B. … Read more