higher-order-functions

How does this “higher-order functions” thing works in Javascript

牧云@^-^@ 提交于 2019-12-11 19:26:51
问题 From the book Eloquent Javascript by Marijn Haverbeke, there is this example while introducing the concept of higher-order functions: function greaterThan(n) { return function(m) { return m > n; }; } var greaterThan10 = greaterThan(10); console.log(greaterThan10(11)); // → true I'm not quite sure how this works... probably answering my own question, but this is how I see it: First, greaterThan(n) is called in this line, assigning its value to the greaterThan10 variable: var greaterThan10 =

How to filter associative array by values and split into two variables?

我只是一个虾纸丫 提交于 2019-12-11 10:34:22
问题 I am trying to filter an associative array so that certain values within a specific key will go into a specific variable. To make sense of this, here is an example of what I'm trying to do: Input (from DB): Array ( [0] => Array ( [id] => '12', [status] => '0' ) [1] => Array ( [id] => '13', [status] => '1' ) [2] => Array ( [id] => '14', [status] => '1' ) ) Output (in PHP): $status_one = Array ( [0] => Array ( [id] => '13', [status] => '1' ) [1] => Array ( [id] => '14', [status] => '1' ) );

Modifying an anonymous function to invoke another anonymous function in Kotlin

我的梦境 提交于 2019-12-11 07:29:19
问题 I have few click listeners in my code like below: tvLogin.setOnClickListener { hideKeyBoard(it) login() } tvForgotPassword.setOnClickListener { hideKeyBoard(it) navigateToForgetPassword() } I want to modify the passed block of code to always invoke hideKeyBoard(view) and then my function. Is there a way to create a higher order function that would modify a block of code and invoke passed function? I have tried something like below: val clickListener: (View,()->Unit) -> Unit But not sure how

Using prototype functions in higher order functions in javascript

你。 提交于 2019-12-11 06:58:29
问题 I'm trying to concat an array of arrays using reduce and I figured that I could use the Array.prototype.concat function like this: arr = [[1],[2],[3]] arr.reduce((a, b) => Array.prototype.concat(a, b), []) Which works fine and gives me the array [1, 2, 3] . Then I thought I could be even smarter and do it like this: arr = [[1],[2],[3]] arr.reduce(Array.prototype.concat, []) This however gives me an error: TypeError: Array.prototype.concat called on null or undefined at Array.reduce (native)

spark higher order function transform output struct

喜欢而已 提交于 2019-12-11 06:07:27
问题 How can I transform an array of structs to again a struct using spark higher order functions? The dataset: case class Foo(thing1:String, thing2:String, thing3:String) case class Baz(foo:Foo, other:String) case class Bar(id:Int, bazes:Seq[Baz]) import spark.implicits._ val df = Seq(Bar(1, Seq(Baz(Foo("first", "second", "third"), "other"), Baz(Foo("1", "2", "3"), "else")))).toDF df.printSchema df.show(false) I want to concatenate all thing1, thign2, thing3 but keep the other property for each

A function builder in scheme

让人想犯罪 __ 提交于 2019-12-11 05:15:56
问题 A function I'm supposed to build is supposed to take in a list of numbers as a parameter, and give a single function as an output that does as follows: If the number in the list is a positive number, add it, if it is negative multiply by it, and if it is 0, square the number. For example, if I pass in (4 -1 0), it should return a function that takes in a single parameter, adds 4 to it, multiplies it by -1, squares it, and returns that. I think I'm on the right track, but I'm getting seriously

Guards and concatiating to lists in an anonymous function

心已入冬 提交于 2019-12-11 05:13:38
问题 I am trying to wrap my head around the syntax of Haskell. This problem is very simple to solve logically. I have to break up a list of positive and negative integers and group them such that [1,2,3,-1,-2,-3,1,2,3] becomes [[1,2,3],[-1,-2,-3], [1,2,3]] I would like to use a higher order function, foldr to be able to do that with an anonymous function taking in two arguements. This is what I have so far. split = foldr (\ x y -> if (x > 0) then if (head (head y)) < 0 then [x] : y else x : head y

How do I define the sieve function for prime computation using higher–order functions?

两盒软妹~` 提交于 2019-12-11 05:11:58
问题 I have a recursive definition of sieve in Haskell for prime number computation. But I don’t know how to write the same function using higher–order functions such as map or filter . Can anybody show me please? sieve [] = [] sieve (x:xs) = check (x:xs) check [] = [] check (x:xs) |x/=2 && x/=3 && x/=5 && x/=7 = comp (x:xs) |otherwise = x : sieve xs comp [] = [] comp (x:xs) |x `mod` 2 == 0 = sieve xs |x `mod` 3 == 0 = sieve xs |x `mod` 5 == 0 = sieve xs |x `mod` 7 == 0 = sieve xs |otherwise = x :

Scala Higher Order Function Little Confused

不问归期 提交于 2019-12-10 23:21:37
问题 I was running the below Scala code in Worksheet: package src.com.sudipta.week2.coursera import scala.math.abs import scala.annotation.tailrec object FixedPoint { println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet val tolerance = 0.0001 //> tolerance : Double = 1.0E-4 def isCloseEnough(x: Double, y: Double): Boolean = { abs((x - y) / x) / x < tolerance } //> isCloseEnough: (x: Double, y: Double)Boolean def fixedPoint(f: Double => Double)(firstGuess: Double): Double =

Custom Filter Function with Predicate using List Comprehension

被刻印的时光 ゝ 提交于 2019-12-10 21:40:00
问题 I need to develop my own filter function similar to how filter works in Haskell but by using list comprehension and a predicate. So I would put lcFilter (>3) [1,2,3,4,5,6,10,444,3] in ghci and it would print all numbers greater than 3. My code is based on a recursion example which I'm good at but I can't seem to convert to list comprehension. It seams no matter what I put in [x | x<-xs, p] it always throws a compiler error. I know the p part is wrong. I've tried ==p , xs==p and just about