higher-order-functions

higher level functions in R - is there an official compose operator or curry function?

I can create a compose operator in R: `%c%` = function(x,y)function(...)x(y(...)) To be used like this: > numericNull = is.null %c% numeric > numericNull(myVec) [2] TRUE FALSE but I would like to know if there is an official set of functions to do this kind of thing and other operations such as currying in R. Largely this is ...

Higher order functions in C

Is there a "proper" way to implement higher order functions in C. I'm mostly curious about things like portability and syntax correctness here and if there are more than one ways what the merits and flaws are. Edit: The reason I want to know how to create higher order functions are that I have written a system to convert PyObject lists...

Does "Value Restriction" practically mean that there is no higher order functional programming?

Does "Value Restriction" practically mean that there is no higher order functional programming? I have a problem that each time I try to do a bit of HOP I get caught by a VR error. Example: let simple (s:string)= fun rq->1 let oops= simple "" type 'a SimpleType= F of (int ->'a-> 'a) let get a = F(fun req -> id) let oops2= get "" ...

Finding maximum of a list of lists by sum of elements in Python

What's the idiomatic way to do maximumBy (higher order function taking a comparison function for the test), on a list of lists, where the comparison we want to make is the sum of the list, in Python? Here's a Haskell implementation and example output: > maximumBy (compare `on` sum) [[1,2,3],[4,5,6],[1,3,5]] > [4,5,6] And implementati...

How do you solve this Haskell problem using higher order functions?

Using higher order functions (map, fold or filter) and if necessary lambda expressions. Write a definition for f1 and f2 so the following evaluation is valid: f1 (f2 (*) [1,2,3,4]) 5 == [5,10,15,20] Any help would be appreciated, thanks. ...

How do you solve this Haskell problem using a fold map and take?

Define a function replicate which given a list of numbers returns a list with each number duplicated its value. Use a fold, map, and take ..> replicate [5,1,3,2,8,1,2] output: [5,5,5,5,5,1,3,3,3,2,2,8,8,8,8,8,8,8,8,1,2,2] I've figure this out using List comprehension and recursion: replicate2 [] = [] replicate2 (n:nn) = take n(repe...

Higher order function « filter » in C++

Hi all. I wanted to write a higher order function filter with C++. The code I have come up with so far is as follows: #include <iostream> #include <string> #include <functional> #include <algorithm> #include <vector> #include <list> #include <iterator> using namespace std; bool isOdd(int const i) { return i % 2 != 0; } template < ...

Haskell FlatMap

I am a beginner interested in Haskell, and I have been trying to implement the flatmap (>>=) on my own to better understand it. Currently I have flatmap :: (t -> a) -> [t] -> [a] flatmap _ [] = [] flatmap f (x:xs) = f x : flatmap f xs which implements the "map" part but not the "flat". Most of the modifications I make result i...

F# Higher-order property accessors

I just upgraded my prototyping tuple to a record. Someday it may become a real class. In the meantime, I want to translate code like this: type Example = int * int let examples = [(1,2); (3,4); (5,6)] let descs = Seq.map (fst >> sprintf "%d") examples to this: type Example = { Field1 : int Field2 : int Description : string } le...

Scala ActionListener / anonymous function type mismatch

Attempting to implement code similar to that found in the higher-order-function example from http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-6 val button = new JButton("test") button.addActionListener{ e:ActionEvent => println("test") } add(button) leads to the following error: type mismatch; found : (java.awt.ev...

Common recursion pattern

I'm getting used to Haskell's higher-order functions. Usually I can replace explicit patterns of recursion with functions like map, fold, and scan. However, I often run into the following recursion pattern which I don't understand how to express using higher-order functions: f (x:[]) = k x f (x:xs) = g x (f xs) For instance, sup...

"filter" higher order function in C++

Does C++ standard library and/or Boost have anything similar to the filter function found in functional languages? The closest function I could find was std::remove_copy_if but it seems to be doing the opposite of what I want. Does boost::lambda have any function to get a negated version of my predicate (similar to not in Haskell)? I co...

What do we call this (new?) higher-order function?

I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will be explained afterward. Python: >>> def pleat(f, l): return map(lambda t: f(*t), zip(l, l[1:])) >>> pleat(operator.add, [0, 1, 2, 3]) [1, 3, 5] Haskell...

Rationale behind renaming of higher order list operations

What was the rationale behind renaming of higher order list operations in C#? (Examples: map -> Select, filter -> Where, fold -> Aggregate) ...

Convert higher order function from Python to Haskell

I have the following code: import operator def stagger(l, w): if len(l)>=w: return [tuple(l[0:w])]+stagger(l[1:], w) return [] def pleat(f, l, w=2): return map(lambda p: f(*p), stagger(l, w)) if __name__=="__main__": print pleat(operator.add, range(10)) print pleat(lambda x, y, z: x*y/z, range(3, 13), 3) ...

Haskell programing: map and fold on search trees

I have a search tree thats defined as data (Ord a) => Stree a = Null | Fork(Stree a) a (Stree a) deriving Show and I have to define two functions, mapStree: mapStree :: (Ord b, Ord a) => (a -> b) -> Stree a -> Stree b and foldStree: foldStree :: (Ord a) => (b -> a -> b -> b) -> b -> Stree a -> b I dont fully understand whats g...

Scala compiler says my method is recursive in case when implicits and anonymous class is used

I want to be able to write code like 10 times { doSomething } So I thought I could do that with implicits. When i execute the following code in the Scala REPL it gets defined correctly scala> implicit def intToMyRichInt(count: Int) = { | new { | def times(f: => Unit) = { | 1 to count foreach { _ => f...

Functional C# - using or returning Action's

Browsing the net for better fault handling in C#, I've com across the following to implementation strategies. The first one is natural to me, while the other implementation I'm not certain what its advantages are? 1) static void Fault(Action protectedBlock, Action faultHandler) { try { protectedBlock(); } catc...

How to define parameter names for higher order functions in C#

In C# it is possible to create higher order functions, ie. functions g taking functions as arguments. Say I want to create such a function which given a function f and returns another function extending its functionality. How do I define argument names for the returned enhanced method? The motivation being, that I'm working with higher o...