higher-order-functions

Working with Sets as Functions

我的未来我决定 提交于 2019-12-20 03:29:09
问题 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

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

那年仲夏 提交于 2019-12-20 01:43:37
问题 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

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

老子叫甜甜 提交于 2019-12-19 16:58:24
问题 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

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

为君一笑 提交于 2019-12-19 16:51:10
问题 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

How to return member values in a array of objects using lambda expressions

允我心安 提交于 2019-12-19 04:21:13
问题 I have an array of "Dog" where i want to print the name of all dogs older then 5 years. I tried something like Dogs.filter{ it.age > 5 }.forEach { it.name } This gives me the value i need, but how do I store and return it as a list of strings? I tried things like adding .join(",") but since I don't get any array in return it wont work. 回答1: I think you're looking for the map operator: val dogNames: List<String> = dogs.filter { it.age > 5 }.map { it.name } 来源: https://stackoverflow.com

Promises in lapply / R

放肆的年华 提交于 2019-12-19 03:40:37
问题 I am not sure what the promises are doing in R If one runs a = lapply(seq_len(2), function(n) { function() {n}}) b = lapply(seq_len(2), function(n) {n}) we can see that a[[1]]() # == 2 b[[1]] # == 1 I understand that R uses promise's object and lazily evaluates an expression in its environment, but I dont understand why the different environments created for each function would not contain their own value for n. [[1]] function () { n } <environment: 0x7f9b2416ad18> [[2]] function () { n }

Why does my version of filter perform so differently than Swifts?

佐手、 提交于 2019-12-18 15:35:28
问题 As an exercise I've rewritten a few of Swift's higher order functions, one being .filter . I decided to measure my version of .filter against Swift's using instruments and I'm rather confused about the results. Here's what my version of filter looks like, which I admit may be incorrect. extension Array { func myFilter(predicate: Element -> Bool) -> [Element] { var filteredArray = [Element]() for x in self where predicate(x) { filteredArray.append(x) } return filteredArray } } What Happened My

How does the inner function in a HOC get the props

这一生的挚爱 提交于 2019-12-17 20:54:43
问题 I'm just getting my head around using HOC in React , one thing that is confusing me slightly is, how does my inner function in this example gain access to props as an argument? const withProps = Component => ( props => { return <Component {...props}/> } ) export default withProps 回答1: To add more to what @AliAnarkali said, a HOC returns you a component so when you write like const EnhancedApp = withProps(App); EnhancedApp is basically const EnhancedApp = props => { return <Component {...props

Zip with default value instead of dropping values?

℡╲_俬逩灬. 提交于 2019-12-17 19:33:24
问题 I'm looking for a function in haskell to zip two lists that may vary in length. All zip functions I could find just drop all values of a lists that is longer than the other. For example: In my exercise I have two example lists. If the first one is shorter than the second one I have to fill up using 0's. Otherwise I have to use 1's. I'm not allowed to use any recursion. I just have to use higher order functions. Is there any function I can use? I really could not find any solution so far. 回答1:

How can I explode and trim whitespace?

假如想象 提交于 2019-12-17 17:24:21
问题 For example, I would like to create an array from the elements in this string: $str = 'red, green, blue ,orange'; I know you can explode and loop through them and trim: $arr = explode(',', $str); foreach ($arr as $value) { $new_arr[] = trim($value); } But I feel like there's a one line approach that can handle this. Any ideas? 回答1: You can do the following using array_map: $new_arr = array_map('trim', explode(',', $str)); 回答2: An Improved answer preg_split ('/(\s*,*\s*)*,+(\s*,*\s*)*/', 'red,