higher-order-functions

Different brackets style on Scala function definition parameter list

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 09:36:57
What is the difference of the following two function definitions in Scala: 1) def sum(f: Int => Int)(a: Int, b: Int): Int = { <code removed> } 2) def sum(f: Int => Int, a: Int, b: Int): Int = { <code removed> } ? SBT's console REPL gives different value for them so looks if they are somehow different: sum: (f: Int => Int, a: Int, b: Int)Int sum: (f: Int => Int)(a: Int, b: Int)Int The first definition is curried , so that you can provide a and b at another time. For instance, if you know the function you want to use in the current method, but don't yet know the arguments, you can use it so: def

fold_tree in OCaml

感情迁移 提交于 2019-12-05 04:08:37
As You may know, there are higher order functions in OCaml, such as fold_left, fold_right, filter etc. On my course in functional programming had been introduced function named fold_tree, which is something like fold_left/right, not on lists, but on (binary) trees. It looks like this: let rec fold_tree f a t = match t with Leaf -> a | Node (l, x, r) -> f x (fold_tree f a l) (fold_tree f a r);; Where tree is defined as: type 'a tree = Node of 'a tree * 'a * 'a tree | Leaf;; OK, here's my question: how does the fold_tree function work? Could you give me some examples and explain in human

How can I create shared state for HOC?

怎甘沉沦 提交于 2019-12-04 16:45:19
I have created LoadBookHOC which is wrapped with BookDetails and BookSummary component. LoadBookHOC.js const LoadBookHOC = InnerComponent => class LoadBook extends React.Component { constructor(){ super(); this.state={ book: [] }; } set= obj => this.setState(obj); render(){ return( <InnerComponent {...this.props} hocState={this.state} hocSetState={this.set} /> ); } } BookDetails.js this.hocSetState({book: <new data>}); BookSummary.js this.hocSetState({book: <new data>}); Whenever this.props.hocState.book called in BookDetails or BookSummary , I need to get the same data. I mean all the

“filter” higher order function in C++

天大地大妈咪最大 提交于 2019-12-04 11:42:43
问题 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 could negate my predicate and use it with std::remove_copy_if then. Please note that I am not asking how to write filter function in C++; I am just asking

Issue with higher order function as a binding adapter

不羁的心 提交于 2019-12-04 10:17:58
问题 I am running into issues trying to take a function as a parameter in a binding adapter using Kotlin/Android databinding. This example code throws e: error: cannot generate view binders java.lang.StackOverflowError when building with no other useful info in the log. Here is my binding adapter: @JvmStatic @BindingAdapter("onDelayedClick") fun onDelayedClick(view: View, function: () -> Unit) { // TODO: Do something } XML: <View android:id="@+id/test_view" android:layout_width="wrap_content"

Why is Haskell (GHC) so darn fast?

こ雲淡風輕ζ 提交于 2019-12-04 07:18:06
问题 Haskell (with the GHC compiler) is a lot faster than you'd expect. Used correctly, it can get close-ish to low-level languages. (A favorite thing for Haskellers to do is to try and get within 5% of C (or even beat it, but that means you are using an inefficient C program, since GHC compiles Haskell to C).) My question is, why? Haskell is declarative and based on lambda calculus. Machine architectures are clearly imperative, being based on turing machines, roughly. Indeed, Haskell doesn't even

higher order function python walkthrough

送分小仙女□ 提交于 2019-12-04 06:14:35
问题 def tracer(fn): def traced(x): print('Calling',fn,'(',x,')') result=fn(x) print('Got',result,'from',fn,'(',x,')') return result return traced def fact(n): if n ==0: return 1 return n * fact(n-1) new_fact = tracer(fact) new_fact(2) I used this code on pythontutor.com to better understand higher order functions, but i'm having difficulty understanding why new_fact(2) in step 8 is mapped to traced? In other words, how does the traced function know the argument is 2? 回答1: In Python, functions are

Map value in particular key in array of dictionary

心不动则不痛 提交于 2019-12-04 05:44:43
问题 Map value the particular key wherever presents in array of dictionary and replace it in same array. We need to update pan_card key 0 to 1 , in occurence of array of dictionary. let keyToUpdate = "pan_card" var arrayOfDictionary = [[String:Any]]() var firstDict = [String:Any]() firstDict["passport"] = 0 firstDict["ration_card"] = 0 firstDict["pan_card"] = 0 var arrayDict = [String : Any]() arrayDict["currentObject"] = firstDict arrayDict["title"] = "Documents list" var secondDict = [String:Any

How do I write higher-order functions that take polymorphic functions as arguments in Typed Racket?

落爺英雄遲暮 提交于 2019-12-04 03:11:34
For example, how can I write a version of map that will work with polymorphic functions in Typed Racket? I use a simple id function defined as: (: id : (All (A) A -> A)) (define (id x) x) When I try to map it over a list i get an error: > (map id '(1 2 3)) Type Checker: Polymorphic function `map' could not be applied to arguments: Types: (-> a b ... b c) (Listof a) (Listof b) ... b -> (Listof c) (-> a c) (Pairof a (Listof a)) -> (Pairof c (Listof c)) Arguments: (All (A) (-> A A)) (List One Positive-Byte Positive-Byte) Expected result: AnyValues in: (map id (quote (1 2 3))) You have to manually

Haskell FlatMap

荒凉一梦 提交于 2019-12-04 01:10:29
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 in the disheartening and fairly informationless Occurs check: cannot construct the infinite type: a = [a] When generalising the type(s) for `flatmap' error. What am I missing? An error like this happens when the type signature you specify does not match the actual type of