functional-programming

JavaScript : Make an array of value pairs form an array of values

岁酱吖の 提交于 2019-12-19 04:01:52
问题 Is there an elegant, functional way to turn this array: [ 1, 5, 9, 21 ] into this [ [1, 5], [5, 9], [9, 21] ] I know I could forEach the array and collect the values to create a new array. Is there an elegant way to do that in _.lodash without using a forEach ? 回答1: You could map a spliced array and check the index. If it is not zero, take the predecessor, otherwise the first element of the original array. var array = [1, 5, 9, 21], result = array.slice(1).map((a, i, aa) => [i ? aa[i - 1] :

How to define an existential higher kinded type in Scala

不打扰是莪最后的温柔 提交于 2019-12-19 03:24:41
问题 I was trying to define a type that accept an existential higher kinded type in Scala. Unfortunately Scalac does not allow it. Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45). Type in expressions to have them evaluated. Type :help for more information. scala> :paste // Entering paste mode (ctrl-D to finish) trait H[F[_, _]] trait T[A, B] val h:H[T] = null val e:H[F] forSome { type F[A, B] } = h // Exiting paste mode, now interpreting. <console>:13: error:

Why higher order procedures?

非 Y 不嫁゛ 提交于 2019-12-19 03:22:23
问题 So if a language provides higher order procedure then I can have procedure that returns procedure. Something like: (define (Proc a b c) (lambda (x) ( #| method body here in terms of a b c and x |# ))) To create new procedure, I would just do something like: (define ProcA (Proc a1 b1 c1)) ; Would create ProcA that has 1 argument Similar task could be done in a language which does not support higher order procedure by defining Proc that takes 4 instead of 3 arguments and calling this procedure

Why higher order procedures?

妖精的绣舞 提交于 2019-12-19 03:22:09
问题 So if a language provides higher order procedure then I can have procedure that returns procedure. Something like: (define (Proc a b c) (lambda (x) ( #| method body here in terms of a b c and x |# ))) To create new procedure, I would just do something like: (define ProcA (Proc a1 b1 c1)) ; Would create ProcA that has 1 argument Similar task could be done in a language which does not support higher order procedure by defining Proc that takes 4 instead of 3 arguments and calling this procedure

How would be a functional approach to shifting certain array elements?

萝らか妹 提交于 2019-12-19 02:40:07
问题 I have a Scala app with a list of items with checkboxes so the user select some, and click a button to shift them one position up (left). I decided to write a function to shift elements of some arbitrary type which meet a given predicate. So, if you have these elements: a b c D E f g h I and the predicate is "uppercase characters", the function would return this: a b D E c f g I h In short, any sequence of contiguous elements that meet the predicate are swapped with the single element at the

Function and Predicate parameter ambiguous?

☆樱花仙子☆ 提交于 2019-12-19 02:28:10
问题 Using Java 8, I get a compiler error for the following code: public class Ambiguous { public static void call() { SomeDataClass data = new SomeDataClass(); callee(data, SomeDataClass::getString); // compiler errors: // 1. at callee method name: // The method callee(SomeDataClass, Function<SomeDataClass,String>) is ambiguous for the type Ambiguous // 2. at lambda: // Type mismatch: cannot convert from boolean to String callee(data, d -> d.getRandom() > 0.5); } public static void callee

Combining Predicates

二次信任 提交于 2019-12-19 02:27:28
问题 Is there any way that you can combine predicates? Lets say I have something like this: class MatchBeginning : public binary_function<CStdString, CStdString, bool> { public: bool operator()(const CStdString &inputOne, const CStdString &inputTwo) const { return inputOne.substr(0, inputTwo.length()).compare(inputTwo) == 0; } }; int main(int argc, char* argv[]) { CStdString myString("foo -b ar -t az"); vector<CStdString> tokens; // splits the string every time it encounters a "-" split(myString,

How to Search for an item in a List in Erlang?

对着背影说爱祢 提交于 2019-12-18 21:12:12
问题 I am writing a cache gen-server for the company Use. I am wondering how to search an item from the list as I want the cost of the search for comparing various data structures in erlang like dict, orddict, List, tuples, tree, queue etc to use for cache program. Example: List = [{"A1",["ankit","sush", "Hover", "x4", "a3","nilesh","mike","erlang" | ...]}|...]. Now, I want to search for the Key A1 and search for 'mike' in the list. What is the best way to search the above List. Please provide

Classes with static arrow functions

杀马特。学长 韩版系。学妹 提交于 2019-12-18 20:49:18
问题 I'm currently implementing the static land specification (an alternative of fantasy land). I want to not only use plain objects as types but also ES2015 classes with static methods. I've implemented these static methods as arrow functions in curried form instead of normal functions. However, this isn't possible with ES2015 classes: class List extends Array { static map = f => xs => xs.map(x => f(x)) static of = x => [x] } My map doesn't need its own this , because it is merely a curried

Cartesian product in clojure

孤街浪徒 提交于 2019-12-18 20:14:28
问题 I'm trying to implement a method that will take a list of lists and return a the cartesian product of these lists. Here's what I have so far: (defn cart ([] '()) ([l1] (map list l1)) ([l1 l2] (map (fn f[x] (map (fn g [y] (list x y)) l2)) l1) ) ) (defn cartesian-product [& lists] (reduce cart lists) ) ;test cases (println (cartesian-product '(a b) '(c d))) ; ((a c) (a d) (b c) (b d)) (println (cartesian-product ())) ;() (println (cartesian-product '(0 1))) ; ((0) (1)) (println (cartesian