higher-order-functions

Invocation of methods with default parameters in scala higher-order function

自古美人都是妖i 提交于 2020-01-05 13:38:11
问题 Supposedly I have a method that has one default parameter. I want to pass it as an argument to another method. How do I call the passed-in method with its default parameter ? def printNum(i: Int = 4): Unit = { println(i) } def doStuff(printFunc: (Int) => Unit): Unit = { // How to call printFunc with its default parameter printFunc() } doStuff(printNum) 回答1: I am afraid you cannot. Default parameter is property of methods, just like named arguments and things like this. When you pass it as a

Join elem with next one in a functional style

删除回忆录丶 提交于 2020-01-04 14:05:12
问题 I'm trying to find a way to "join"/"groupby" 2 elements in a list as following : List("a","b","c","d") -> List("ab","bc","cd") With a functional style. Would someone know how to do this? Need I use reducer, fold, scan, other higher-order function? 回答1: Sliding creates subcollections with sliding window, then you just need to map this sublists to strings: List("a","b","c","d").sliding(2,1).map{case List(a,b) => a+b} 回答2: Try val xs = List("a","b","c","d") (xs, xs.tail).zipped.map(_ ++ _) //

Higher order functions: automatic generation vs manual definition

喜夏-厌秋 提交于 2020-01-04 02:57:08
问题 I'm trying to delay evaluation for a bit, and so I prefer to work with functions as long as possible. I have class Function which defines composition and pointwise arithmetics for functions: from functools import reduce def compose(*funcs): ''' Compose a group of functions f1, f2, f3, ... into (f1(f2(f3(...)))) ''' result = reduce(lambda f, g: lambda *args, **kaargs: f(g(*args, **kaargs)), funcs)) return Function(result) class Function: ''' >>> f = Function(lambda x : x**2) >>> g = Function

what are curry and uncurry in high-order functions in ML

一个人想着一个人 提交于 2020-01-02 04:01:25
问题 fun curry f x y = f (x, y); fun uncurry f (x, y) = f x y; fun compose (f, g) x = f (g x); I understand compose function, but not quite understand curry and uncurry in ML. Can anyone explain these? Also, what do the following two lines mean? (1) compose (compose, uncurry compose) (2) compose (uncurry compose, compose) 回答1: If you look at the types, then you will clearly see what curry and uncurry does. Remember that it is possible to define function which either takes its arguments as one big

Replace a 3 parameter list-comprehension by using map, concat

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 14:10:06
问题 I have some understanding of list comprehension. I understand that the expression: [x * x | x <- [1..10]] should output [1,4,9,16,25,36,49,64,81,100] and that the effect of that expression is the same as: map power [1..10] power x = x * x Now, I have to find out the other method (just like the above) for the following function: [(x,y+z) | x <- [1..10], y <- [1..x], z <- [1..y]] I can't figure it out by myself without errors, please help me 回答1: The Haskell Report tells us how to translate

Replace a 3 parameter list-comprehension by using map, concat

北城余情 提交于 2019-12-30 14:10:05
问题 I have some understanding of list comprehension. I understand that the expression: [x * x | x <- [1..10]] should output [1,4,9,16,25,36,49,64,81,100] and that the effect of that expression is the same as: map power [1..10] power x = x * x Now, I have to find out the other method (just like the above) for the following function: [(x,y+z) | x <- [1..10], y <- [1..x], z <- [1..y]] I can't figure it out by myself without errors, please help me 回答1: The Haskell Report tells us how to translate

difference between foldLeft and reduceLeft in Scala

风流意气都作罢 提交于 2019-12-29 10:05:25
问题 I have learned the basic difference between foldLeft and reduceLeft foldLeft: initial value has to be passed reduceLeft: takes first element of the collection as initial value throws exception if collection is empty Is there any other difference ? Any specific reason to have two methods with similar functionality? 回答1: Few things to mention here, before giving the actual answer: Your question doesn't have anything to do with left , it's rather about the difference between reducing and folding

Scala: How to convert tuple elements to lists

主宰稳场 提交于 2019-12-28 11:54:14
问题 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?

higher order function javascripts

血红的双手。 提交于 2019-12-25 03:34:10
问题 I'm trying to understand better and start coding JS with higher order functions. Below is just a practice I'm doing, and I want to output an array of numbers * 2. function each(collection, callback) { for(var i = 0; i < collection.length; i++) { callback(collection[i]); } } function isNumber(item) { var arr = []; if(typeof item === "number") { arr.push(item * 2); } return arr; } each([1, 2, 3, 4, "String"], isNumber); from my understanding, when each() function is invoked with array and

Using a function as a parameter to another function

Deadly 提交于 2019-12-25 01:33:33
问题 "use strict"; function square(x) { return x * x; } function add(square,y) { return square(4) +y document.write(square + y) } add(4,1); console.log(add(4,1)); I have to use a function as in the parameter of another function. I am trying to output 17. 回答1: Just call it as such: add(square,1); you may also want to change function add(square,y) { return square(4) +y document.write(square + y) } to be: function add(square,y) { document.write(square(4) + y) return square(4) +y } Per Andrew's