higher-order-functions

Higher order functions - Javascript

痞子三分冷 提交于 2019-12-02 02:43:37
I am working through Eloquent Javascript. The function count takes an array and a test function ( equals(x) ) as arguments, and returns the amount of elements in the array for which the test function returned true. I understand the broad way that these functions are working, and that logically the total argument to the anonymous function passed to reduce has a value of zero. Can someone help me to see where the value for total is coming from specifically though? I want to have a clearer picture in my mind. function count(test, array) { return reduce(function(total, element) { // Where is the

Higher order functions - Javascript

两盒软妹~` 提交于 2019-12-02 02:23:41
问题 I am working through Eloquent Javascript. The function count takes an array and a test function ( equals(x) ) as arguments, and returns the amount of elements in the array for which the test function returned true. I understand the broad way that these functions are working, and that logically the total argument to the anonymous function passed to reduce has a value of zero. Can someone help me to see where the value for total is coming from specifically though? I want to have a clearer

Why doesn't Haskell accept arguments after a function composition?

拟墨画扇 提交于 2019-12-02 01:38:05
Since Haskell functions have only one argument, where the rest of arguments stay like lambdas, then we can do this: foo a b = a + b -- this is like foo a = \b -> a + b foo 1 2 -- ok Well, I noticed that if I declare the function returning a lambda, just like in comment, the foo 1 2 will works fine the same way. But when I compose these functions, like this: foo a b = a + b bar x = x * x bar . foo 1 2 -- oh, it's wrong, I need do '(bar . foo 1) 2' ...This returns an error. Ok, the question is: Why doesn't returning a lambda from function work like the function composition? I mean, in

Working with Sets as Functions

喜夏-厌秋 提交于 2019-12-01 23:44:12
From a FP course: type Set = Int => Boolean // Predicate /** * Indicates whether a set contains a given element. */ def contains(s: Set, elem: Int): Boolean = s(elem) Why would that make sense? assert(contains(x => true, 100)) Basically what it does is provide the value 100 to the function x => true . I.e., we provide 100, it returns true . But how is this related to sets? Whatever we put, it returns true . Where is the sense of it? I understand that we can provide our own set implementation/function as a parameter that would represent the fact that provided value is inside a set (or not) -

How to remove all PHP array elements containing a certain sub-string?

心已入冬 提交于 2019-12-01 21:22:35
ok i looked up some functions and i don't seem to lucky of finding any, i wanna filter an array to strip specific array that contains some string heres an example : $array(1 => 'January', 2 => 'February', 3 => 'March',); $to_remove = "Jan"; // or jan || jAn, .. no case sensitivity $strip = somefunction($array, $to_remove); print_r($strip); it should return [1] => February [2] => March a function that looks for the sub-string for all values in an array, if the sub-string is found, remove that element from the array You can use array_filter and stripos $array = array(1 => 'January', 'February',

forEach on a 'new Array' isn't doing what I expect

99封情书 提交于 2019-12-01 17:34:24
I'm just learning how to use JS higher-order functions (map, forEach, reduce, etc), and have stumbled into confusion. I'm trying to write a simple 'range' function, but can't seem to populate my output array. This is the goal: range(1, 4) // [1, 2, 3, 4] I'm getting this: [undefined × 4] Here is my code: function range(num1, num2) { var rangeArr = new Array((num2 + 1) - num1); return rangeArr.map(function(e, i, arr) {return arr[i] = num1 + i}); } What am I missing here? As far as I can tell the problem appears to have something to do with the way I'm utilizing 'new Array', but beyond that I'm

forEach on a 'new Array' isn't doing what I expect

試著忘記壹切 提交于 2019-12-01 17:09:47
问题 I'm just learning how to use JS higher-order functions (map, forEach, reduce, etc), and have stumbled into confusion. I'm trying to write a simple 'range' function, but can't seem to populate my output array. This is the goal: range(1, 4) // [1, 2, 3, 4] I'm getting this: [undefined × 4] Here is my code: function range(num1, num2) { var rangeArr = new Array((num2 + 1) - num1); return rangeArr.map(function(e, i, arr) {return arr[i] = num1 + i}); } What am I missing here? As far as I can tell

How to get the summation of diagonal lines using higher-order functions?

自古美人都是妖i 提交于 2019-12-01 17:03:41
Consider the following 2D array: let array = [ [11, 2, 4], [4, 5, 6], [10, 8, -12] ] What I want to get is the summation of the diagonals: As firstDiagnal : 11 + 5 + (-12) = 4 As secondDiagnal : 4 + 5 + 10 = 19 I could achieve it using a standard for-in loop: var firstDiagnal = 0 var secondDiagnal = 0 for i in 0..<array.count { firstDiagnal += array[i][i] secondDiagnal += array[i][array[i].count - 1 - i] } print(firstDiagnal) print(secondDiagnal) However, what could it be if we tried to use higher-order functions? such as map and reduce ? To get the first sum, you want the i'th element of the

Does Swift have short-circuiting higher-order functions like Any or All?

巧了我就是萌 提交于 2019-12-01 16:27:09
I'm aware of Swift's higher-order functions like Map, Filter, Reduce and FlatMap, but I'm not aware of any like 'All' or 'Any' which return a boolean that short-circuit on a positive test while enumerating the results. For instance, consider you having a collection of 10,000 objects, each with a property called isFulfilled and you want to see if any in that collection have isFulfilled set to false. In C#, you could use myObjects.Any(obj -> !obj.isFulfilled) and when that condition was hit, it would short-circuit the rest of the enumeration and immediately return true . Is there any such thing

Why does map/filter … not work with an Array of Nothing?

不打扰是莪最后的温柔 提交于 2019-12-01 09:25:19
Isn't Nothing a subtype of all types? scala> val array = new Array(5) array: Array[Nothing] = Array(null, null, null, null, null) scala> array.map(_ => 42) <console>:9: error: value map is not a member of Array[Nothing] array.map(_ => 42) ^ scala> array.filter(_ != 42) <console>:9: error: value filter is not a member of Array[Nothing] array.filter(_ != 42) ^ It's weird that this doesn't work. Is this specified, a feature or a bug? When you see weird behavior involving Nothing, it's because the type inference algorithm thinks that it inserted Nothing itself, since it is introduced during type