FAQ Database Discussion Community


Why can't I pass a trait to a Scala function and call it: response[A: T](r: A) = { r(value) }

scala,higher-order-functions,trait
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,...

How to use swift flatMap to filter out optionals from an array

swift,functional-programming,optional,higher-order-functions,flatmap
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...

Having trouble stepping through function that reduces an array of functions

javascript,functional-programming,reduce,higher-order-functions
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...

Understanding higher order functions in PHP [closed]

php,higher-order-functions
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...

Python: Higher Order Functions with Methods

python,higher-order-functions
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...

TypeScript doesn't seem to allow higher-order function argument generics

generics,typescript,higher-order-functions
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>)...

Higher order javascript functions

javascript,anonymous-function,higher-order-functions
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...

Create a quotient-lifted type with polymorphism over working set and equivalence relation in Isabelle/HOL

set,higher-order-functions,isabelle,theorem-proving,isar
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...

Higher order python functions and setting their attributes correctly

python,higher-order-functions
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...

Higher order function: “Cannot invoke 'map' with an argument list of type '((_) -> _)'”

swift,higher-order-functions
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...

Trouble understanding a basic concept in Javascript Higher Order Functions

javascript,higher-order-functions
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){...

What's the proper way of passing named functions to Javascript's higher-order functions?

javascript,functional-programming,higher-order-functions
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()...

Swift compiler error: “Cannot invoke 'map' with an argument list of type '((_) -> _)'”

ios,swift,higher-order-functions
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...

Creating a function that can be invoked a given number of times and no more

javascript,function,higher-order-functions
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...

How to effectively get indices of 1s for given binary string using Scala?

scala,functional-programming,higher-order-functions
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)...

JavaScript function not working as intended

javascript,higher-order-functions
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 {...

Is recursion a smell (in idiomatic Clojure) because of of zippers and HOFs?

recursion,clojure,higher-order-functions,idiomatic,zipper
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...

Using the reduce method to eliminate any duplicated numbers

javascript,reduce,higher-order-functions
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,...

Is juxt the opposite of map? (In Clojure)

dictionary,clojure,higher-order-functions
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...