Suppose we have a binary string such as 10010010. All I want is a function returning indices of 1s for that string: indicesOfOnes("10010010") -> List(0, 3, 6) indicesOfOnes("0") -> List() And what I implemented is: def indicesOfOnes(bs: String): List[Int] = { val lb = ListBuffer[Int]() bs.zipWithIndex.foreach { case (v, i)...

I've been working through a question and need some help! I've created my own each function below: var myArray = [1,2,3,4,5]; // this will be running through the collection parameter var each = function (collection, callback) { if (Array.isArray(collection)) { for (i=0; i<collection.length; i++) { callback(collection[i]); }; } else {...

I have a range that I'm trying to map on, but I'm getting the error “Cannot invoke 'map' with an argument list of type '((_) -> _)'” Here's what the code looks like let patterns = (0...5).map { verseNum in let verseNumberStartPattern = "\"verse-num\">\(verseNum)</span>(?:\\s?<span>)?(.*?)<" let chapterStartPattern = "\"chapter-num\">\\s\(parsedVerse.chapterStart)\\s</span>" if verseNum...

I'm currently writing a function that makes and returns a new function to create polynomial expressions. I want the function to store the coefficients of the polynomial and the polynomial itself in string form. However, it doesn't seem that I can set either of the attributes without the interpreter insisting...

I've run into a weird problem in my TypeScript code, which causes it to bail out of compilation with the following error: src/mutable_list.ts(47,25): 1005 ',' expected. The code I'm trying to compile is as follows: 44 export function flatMap<V,I,W,J>( 45 list: IMutableList<V,I>, 46 getFn: (V) => IList<W,J>, 47 setFn: (IList<W,J>)...

I would like to use a swift higher order function (map) to remove all Subviews from a given UIView.subviews array. The line (cell.contentView.subviews as [UIView]).map { $0.removeFromSuperView() } causes the error "Cannot invoke 'map' with an argument list of type '((_) -> _)'" I would like to know what the...

I find this piece of code complicated to understand. I will be grateful if i can understand this with illustrations and details. $data = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); function get_algorithm($rand_seed_fnc) { return (odd_even($rand_seed_fnc())) ? function($value) { return $value * $value; } : function($value) use...

I'm a little confused around flatMap (added to Swift 1.2) Say I have an array of some optional type e.g. let possibles:[Int?] = [nil, 1, 2, 3, nil, nil, 4, 5] In Swift 1.1 I'd do a filter followed by a map like this: let filtermap = possibles.filter({ return $0...

I am having a little bit of trouble understanding Higher Order Functions in javascript. Can someone explain to me the difference of what's going on in these two situations? Scenario 1: // A function that takes in a number (x) and adds one to it. function doMath(x){ return function (x){...

When using the reduce method on an array of functions I am having difficulty tracing through how reduce works on the array exactly. comboFunc(num, functionsArr) { return functionsArr.reduce(function (last, current) { return current(last); }, input); } so with functionsArr = [add, multi] and the functions add and multi being function...

In Clojure we see the following examples of using map: (map inc [1 2 3 4 5]) ;;=> (2 3 4 5 6) We observe that map takes a function and applies it to each element in a collection. In Clojure we see the following example of using juxt: ((juxt...

In Python, is there anyway to pass a method to a higher order function, as you would if passing a regular function? For example, let's say I have a string, "str", and depending on some condition, "cond", I want to apply some arbitrary method, "meth", to the string and return...

I'm trying to use Javascript's higher-order functions for functional programming. However, they are producing unexpected results: ['1','2','3','4'].map(parseInt) // [1, NaN, NaN, NaN] ['1','2','3','4'].map(function(x){ return parseInt(x) }) // [1, 2, 3, 4] I expected these two calls to Array.prototype.map to both return [1, 2, 3, 4]. How can I map parseInt()...

Say you have a function that you can input an alert with "hey" and you want to allow that to happen a given number of times — say 2, or 3, or whatever you want — and then after that given number alerts the same amount of times a negative...

The classic book The Little Lisper (The Little Schemer) is founded on two big ideas You can solve most problems in a recursive way (instead of using loops) (assuming you have Tail Call Optimisation) Lisp is great because it is easy to implement in itself. Now one might think this...

I'm trying to implement a generic response function that takes an Int and an object, then calls apply(e: Int) on the provided object (which will return a new instance). Here's what I have so far: trait GPResponse { abstract def apply(error: Int) } trait GPResponseMapping { def response[A: GPResponse](error: Int,...

can someone explain to me what is going on in the following code. The function is receiving n as parameter, so where is the m coming from? The whole code is confusing.. if someone can explain? function greaterThan(n) { return function(m) { return m > n; }; } var greaterThan10...

I am trying to use the reduce method as follows to eliminate duplicates however, it is not quite working: var unique = function(array) { array = array.sort(function(a,b) {return a-b;}); var noDup = [array[0]]; array.reduce(function(c,d) { if(c!==d) { noDup.push(d); return d; } }); return noDup; }; var x = [9,2,1,5,9,1,1,4,2,9];//==>[1, 1,...

I would like to create a quotient type with quotient_type in Isabelle/HOL in which I would left "non-constructed" the non-empty set S and the equivalence relation ≡. The goal is for me to derive generic properties w.r.t. S and ≡ over the quotient-lifted set S/≡. In this way, it would...