higher-order-functions

Get a subset of an array based on an array of keys

心不动则不痛 提交于 2019-11-28 07:30:47
问题 I wrote this function to get a subset of an array. Does php have a built in function for this. I can't find one in the docs. Seems like a waste if I'm reinventing the wheel. function array_subset($array, $keys) { $result = array(); foreach($keys as $key){ $result[$key] = $array[$key]; } return $result; } 回答1: array_diff_key and array_intersect_key are probably what you want. 回答2: I always want this too. Like a PHP version of Underscore's pick. It's ugly and counter-intuitive, but what I

Scala: How to convert tuple elements to lists

会有一股神秘感。 提交于 2019-11-28 06:32:57
Suppose I have the following list of tuples: val tuples = listOfStrings.map(string => { val split = string.split(":") (split(0), split(1), split(2)) }) I would like to get the split(0) in a list, split(1) in another list and so on. A simple way this could be achieved is by writing: list1 = tuples.map(x => x._1).toList list2 = tuples.map(x => x._2).toList list3 = tuples.map(x => x._3).toList Is there a more elegant (functional) way of achieving the above without writing 3 separate statements? This will give you your result as a list of list: tuples.map{t => List(t._1, t._2, t._3)}.transpose If

Does PHP have an equivalent to Python's list comprehension syntax?

断了今生、忘了曾经 提交于 2019-11-28 05:36:46
Python has syntactically sweet list comprehensions: S = [x**2 for x in range(10)] print S; [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] In PHP I would need to do some looping: $output = array(); $Nums = range(0,9); foreach ($Nums as $num) { $out[] = $num*=$num; } print_r($out); to get: Array ( [0] => 0 [1] => 1 [2] => 4 [3] => 9 [4] => 16 [5] => 25 [6] => 36 [7] => 49 [8] => 64 [9] => 81 ) Is there anyway to get a similar list comprehension syntax in PHP? Is there anyway to do it with any of the new features in PHP 5.3? Thanks! Maybe something like this? $out=array_map(function($x) {return $x*$x;},

How to reconcile Javascript with currying and function composition

喜欢而已 提交于 2019-11-28 04:39:41
问题 I love currying but there are a couple of reasons why a lof of Javascript devs reject this technique: aesthetic concerns about the typical curry pattern: f(x) (y) (z) concerns about performance penalties due to the increased number of function calls concerns about debugging issues because of the many nested anonymous functions concerns about readability of point-free style (currying in connection with composition) Is there an approach that can mitigate these concerns so that my coworkers don

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

 ̄綄美尐妖づ 提交于 2019-11-28 03:06:33
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 to reduce the number of brackets, function keywords etc in my code. My curry function: > curry=function(...){ z1=z0=substitute(...);z1[1]=call("list"); function(...){do.call(as.character(z0[[1]]), as.list(c(eval(z1),list(...))))}} > p = curry(paste(collapse="")) > p

Higher order functions in C

霸气de小男生 提交于 2019-11-27 22:06:18
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 (which you get when calling python scripts) into a list of C structures containing the same data but organized in a way not dependant on the python.h libraries. So my plan is to have a function which iterates through a pythonic list and calls a function on each

zipWith (mapping over multiple Seq) in Scala

百般思念 提交于 2019-11-27 19:18:52
Suppose I have val foo : Seq[Double] = ... val bar : Seq[Double] = ... and I wish to produce a seq where the baz(i) = foo(i) + bar(i). One way I can think of to do this is val baz : Seq[Double] = (foo.toList zip bar.toList) map ((f: Double, b : Double) => f+b) However, this feels both ugly and inefficient -- I have to convert both seqs to lists (which explodes with lazy lists), create this temporary list of tuples, only to map over it and let it be GCed. Maybe streams solve the lazy problem, but in any case, this feels like unnecessarily ugly. In lisp, the map function would map over multiple

Type of a function with Implicit parameters in Scala

浪尽此生 提交于 2019-11-27 18:26:24
问题 I would like to have a higher order function that takes in parameter a function that accepts a specific implicit parameter. To be more precise, I am trying to make a function that takes a Future creation method that depends on an implicit context and returns a method that doesn't depend on the context. To be more concrete, let's say that I have something like this: def foo(a: Int)(implicit ctx: ExecutionContext): Future[Float] = future { somelongBar... } I would like to do have a method like

How to use ES6 Fat Arrow to .filter() an array of objects

荒凉一梦 提交于 2019-11-27 16:54:54
I'm trying to use ES6 arrow function with .filter to return adults (Jack & Jill). It appears I cannot use an if statement. What do I need to know in order to do this in ES6? var family = [{"name":"Jack", "age": 26}, {"name":"Jill", "age": 22}, {"name":"James", "age": 5 }, {"name":"Jenny", "age": 2 }]; let adults = family.filter(person => if (person.age > 18) person); // throws error (8:37) SyntaxError: unknown: Unexpected token (8:37) |let adults = family.filter(person => if (person.age > 18) person); My working ES5 example: let adults2 = family.filter(function (person) { if (person.age > 18)

How to repeat a function n times

二次信任 提交于 2019-11-27 09:09:19
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. 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) square(9) square(81) square(6561) square(43046721) 1853020188851841 or lambda -less? def repeated(f, n):