higher-order-functions

Composing higher order reducers in Redux

▼魔方 西西 提交于 2019-12-10 21:38:01
问题 I've created some factory functions that give me simple (or more advanced) reducers. For example (simple one - base on action type set RequestState constant as a value): export const reduceRequestState = (requestTypes: RequestActionTypes) => (state: RequestState = RequestState.None, action: Action): RequestState => { switch (action.type) { case requestTypes.start: return RequestState.Waiting; case requestTypes.success: return RequestState.Success; case requestTypes.error: return RequestState

how to implement a function like sum(2)(3)(4)…(n) in python?

删除回忆录丶 提交于 2019-12-10 12:33:33
问题 how to implement a function that will be invoked in the following way sum_numbers(2)(3)(4)......(n) in python? the result should be 2+3+4+.....+n The hint that I have is since functions are object in pythons there is way to do those using a nested function but I am not sure. def sum_number(x): def sum_number_2(y): def sum_number_3(z): .................... def sum_number_n(n) return n return sum_number_n return sum_number_3 return sum_number_2 return sum_number But instead of writing so many

fold_tree in OCaml

你。 提交于 2019-12-10 03:45:58
问题 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

Reverse currying?

╄→гoц情女王★ 提交于 2019-12-10 03:39:45
问题 I'd like to compose functions in a certain way. Please consider these 2 functions in pseudocode (not F#) F1 = x + y F2 = F1 * 10 // note I did not specify arguments for F1, 'reverse curry' for lack of a better word What I would like for F# to do is figure out that since let F1 x y = x + y //val F1 : int -> int -> int the code let F2 = F1 * 10 would give me the same signature as F1: val F2 : int -> int -> int , and calling F2 2 3 would result in 50: (2 + 3) * 10. That would be rather clever...

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

走远了吗. 提交于 2019-12-09 15:57:04
问题 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

Higher-order functions in Elisp

允我心安 提交于 2019-12-09 09:56:50
问题 I created a function that returns a function in Elisp: (defun singleton-set (elem) (defun f (n) (= n elem)) f) I try to run this in IELM, and it fails: ELISP> (singleton-set 5) *** Eval error *** Symbol's value as variable is void: f ELISP> ((singleton-set 5) 5) *** Eval error *** Invalid function: (singleton-set 5) Due to What is the difference between Lisp-1 and Lisp-2? i changed the code to (defun singleton-set (elem) (defun f (n) (= n elem)) #'f) And invocation to (funcall (singleton-set

Swift subtle difference between curried and higher order function

爱⌒轻易说出口 提交于 2019-12-09 07:18:27
问题 NOTE: This question was asked while Swift 2.1 was the latest. Given: class IntWrapper { var i: Int = 1 } func add(inout m: Int, i: Int) { m += i } And a higher order function func apply() -> (inout i: Int) -> () -> () { return { (inout i: Int) -> () -> () in return { add(&i, i: 1) } } } Application as so results in the member variable value never changing: var intW = IntWrapper() print(intW.i) // Prints '1' apply()(i: &intW.i)() print(intW.i) // Prints '1' However, when changing the function

Calling Clojure higher-order functions

吃可爱长大的小学妹 提交于 2019-12-08 17:18:55
问题 If I define a function that returns a function like this: (defn add-n [n] (fn [x] (+ x n))) I can then assign the result to a symbol: (def add-1 (add-n 1)) and call it: (add-1 41) ;=> 42 How do I call the result of (add-n 1) without assigning it to a new symbol? The following produces this output: (println (add-n 1)) #<user$add_n$fn__33 user$add_n$fn__33@e9ac0f5> nil The #<user$add_n$fn__33 user$add_n$fn__33@e9ac0f5> is an internal reference to the generated function. 回答1: Easy: (println (

React.cloneElement: pass new children or copy props.children?

北战南征 提交于 2019-12-08 17:05:46
问题 I'm confused by the third "children" parameter of React.cloneElement and it's relation to this.props.children . I followed this guide on higher order components and have the following code: render() { const elementsTree = super.render() let myPropChange = {} /* work on newProps... */ myPropChange.something = "nice!". const newProps = Object.assign({}, elementsTree.props, myPropChange) /* map children */ const newChildren = React.Children.map(elementsTree.props.children, c => something(c))

defining map in terms of reduce

帅比萌擦擦* 提交于 2019-12-08 02:27:23
问题 I have just picked up John Hughes' essay Why Functional Programming Matters, and i'm trying to follow along using Scheme. The first higher order function he defines is reduce, which i've defined in Scheme as follows: (define u-reduce (lambda (ff init lst) (if (null? lst) init (ff (car lst) (u-reduce ff init (cdr lst)))))) I can re-create some of the applications of reduce in the essay using this function, however things break apart in the move from reduce to map . The motivating example is