higher-order-functions

How to repeat a function n times

孤人 提交于 2019-12-17 09:54:56
问题 I'm trying to write a function in python that is like: def repeated(f, n): ... where f is a function that takes one argument and n is a positive integer. For example if I defined square as: def square(x): return x * x and I called repeated(square, 2)(3) this would square 3, 2 times. 回答1: That should do it: def repeated(f, n): def rfun(p): return reduce(lambda x, _: f(x), xrange(n), p) return rfun def square(x): print "square(%d)" % x return x * x print repeated(square, 5)(3) output: square(3)

How to filter an array by a condition

此生再无相见时 提交于 2019-12-16 19:51:50
问题 I have an array like this: array("a" => 2, "b" => 4, "c" => 2, "d" => 5, "e" => 6, "f" => 2) Now I want to filter that array by some condition and only keep the elements where the value is equal to 2 and delete all elements where the value is NOT 2. So my expected result array would be: array("a" => 2, "c" => 2, "f" => 2) Note: I want to keep the keys from the original array. How can I do that with PHP? Any built-in functions? 回答1: $fullarray = array('a'=>2,'b'=>4,'c'=>2,'d'=>5,'e'=>6,'f'=>2)

React: How to wrap component with HOC? (access login property across components)

孤人 提交于 2019-12-13 16:12:17
问题 I have a react (with redux & react-router) app that at various points needs to know whether or not a user is logged in based on the state of some data in the store and a bunch of other things tied to data in the store. To achieve this, I've created a higher order component like so: import React from 'react'; import {connect} from 'react-redux'; export function masterComponent(Component){ class MasterComponent extends React.Component { constructor(props){ super(props); } loggedIn(){ return

array.groupBy in TypeScript

二次信任 提交于 2019-12-13 14:50:55
问题 The basic array class has .map , .forEach , .filter , and .reduce , but .groupBy i noticably absent, preventing me from doing something like const MyComponent = (props:any) => { return ( <div> { props.tags .groupBy((t)=>t.category_name) .map((group)=>{ [...] }) } </div> ) } I ended up implementing something myself: class Group<T> { key:string; members:T[] = []; constructor(key:string) { this.key = key; } } function groupBy<T>(list:T[], func:(x:T)=>string): Group<T>[] { let res:Group<T>[] = []

correct style for element-wise operations on lists without numpy (python)

谁说胖子不能爱 提交于 2019-12-13 14:32:46
问题 I would like to operate on lists element by element without using numpy, for example, i want add([1,2,3], [2,3,4]) = [3,5,7] and mult([1,1,1],[9,9,9]) = [9,9,9] , but i'm not sure which way of doing is it considered 'correct' style. The two solutions i came up with were def add(list1,list2): list3 = [] for x in xrange(0,len(list1)): list3.append(list1[x]+list2[x]) return list3 def mult(list1, list2): list3 = [] for x in xrange(0,len(list1)): list3.append(list1[x]*list2[x]) return list3 def

Using constructor where function expected

╄→尐↘猪︶ㄣ 提交于 2019-12-13 12:59:16
问题 Having two simple classes taking Int as an argument: case class Foo(i: Int) class Bar(j: Int) I can say: List(1,2,3) map Foo Which works fine and is equivalent to a bit more verbose: List(1,2,3) map {Foo(_)} However Bar (because it is not a case class?) cannot be used in the same construct: List(1,2,3) map Bar error: not found: value Bar List(1,2,3) map Bar ^ Is there some special syntax to reference any constructor and take advantage of eta expansion? List(1,2,3) map {new Bar(_)} seems a bit

Use of Clojure macros for DSLs

房东的猫 提交于 2019-12-13 11:56:29
问题 I am working on a Clojure project and I often find myself writing Clojure macros for DSLs, but I was watching a Clojure video of how a company uses Clojure in their real work and the speaker said that in practical use they do not use macros for their DSLs, they only use macros to add a little syntactic sugar. Does this mean I should write my DSL in using standard functions and then add a few macros at the end? Update : After reading the many varied (and entertaining) responses to this

Is it possible to write classes using functions in Scala?

℡╲_俬逩灬. 提交于 2019-12-13 10:11:42
问题 Is it possible to write classes using functions in Scala, like one would do in Javascript ? 回答1: Can you use any function as a constructor? No. Can you use closures as "poor man's objects"? Yes. But what you get in this way is not called a class in Scala. 来源: https://stackoverflow.com/questions/38433112/is-it-possible-to-write-classes-using-functions-in-scala

SML functional programming higher order function?

与世无争的帅哥 提交于 2019-12-13 09:49:49
问题 I need to implement a function ziprev : 'a list -> 'b list -> ('a * 'b) list - ziprev [1,2,3,4] [10,20,30,40]; val it = [(1,40),(2,30),(3,20),(4,10)] : (int * int) list Using a function that I already created: - zipW (fn (x, y) => x + y) [1,2,3,4] [10,20,30,40]; val it = [11,22,33,44] : int list and the List.rev from the library. I have no idea how to do a function with two libraries. Any suggestions? 回答1: Hint 1: Compare the result of your ziprev with List.zip [1,2,3,4] [10,20,30,40] You

Return function that modifies the value of the input function

冷暖自知 提交于 2019-12-13 09:00:36
问题 How can I make a function that is given a function as input and returns a function with the value tripled. Here is some pseudo code for what I'm looking for. Concrete examples in Python or Scala would be appreciated. def f(int x): return x ** 2 def tipleFunc(Function g) return 3 * g Function newFunc = tripleFunc(f) print newFunc(5) 回答1: def f(x): return x ** 2 def tripleFunc(g): return lambda *args: 3 * g(*args) newFunc = tripleFunc(f) print newFunc(5) 回答2: In Scala: def foo(x: Int) = x * x