higher-order-functions

Wrapping a function with an indeterminate number of parameters in F#

情到浓时终转凉″ 提交于 2019-12-13 06:23:03
问题 I'm trying to write a simple wrapper class in F# that takes a function that returns a string, and returns a function that takes the same parameters and returns the string from the input 'wrapped'. The following code works for functions that take a single variable (so test works fine): open System let myFunc anotherFunc = fun x -> "boo" + anotherFunc x + "unboo" let Func2 toDouble = (toDouble * 2).ToString () let test = myFunc Func2 let Func3 numOne numTwo = (numOne * numTwo).ToString () let

Why does Swift's reduce function throw an error of 'Type of expression ambigious without more context' when all types are properly defined?

感情迁移 提交于 2019-12-13 05:13:01
问题 var nums = [1,2,3] let emptyArray : [Int] = [] let sum1 = nums.reduce(emptyArray){ $0.append($1)} let sum2 = nums.reduce(emptyArray){ total, element in total.append(element) } let sum3 = nums.reduce(emptyArray){ total, element in return total.append(element) } For all three approaches I'm getting the following error: Type of expression ambiguous without more context But looking at documentation and the method signature of reduce: func reduce<Result>(_ initialResult: Result, _

Higher order function to apply many functions to one argument

被刻印的时光 ゝ 提交于 2019-12-13 03:06:39
问题 I want the common name of a higher order function that applies a list of functions onto a single argument. In this sense it is a converse of map. map takes a function and a list of arguments and applies that function to the entire list. In Python it might look like this map = lambda fn, args: [fn(arg) for arg in args] I'm thinking of the function that does the same but has the alternate argument as a list type ??? = lambda fns, arg: [fn(arg) for fn in fns] I suspect that this function exists

Difference between higher order and curried functions

你说的曾经没有我的故事 提交于 2019-12-12 10:54:11
问题 I'm reading a book, Functional Programming Using F#, which says (page 33), in the section Declaration of higher-order functions We have seen higher-order built-in functions like (+) and (<<) and at the end of the section Higher-order functions may alternatively be defined by supplying the arguments as follows in the let-declaration: let weight ro s = ro * s ** 3.0;; However there were some helpful comments at the bottom of a question that I asked earlier today (which was originally titled

JavaScript reduce can't handle Math functions?

旧巷老猫 提交于 2019-12-12 10:29:39
问题 I'm trying an obvious task: var maxVal = [ 1, 2, 3, 4, 5 ].reduce( Math.max, 0 ); and get: NaN as the result. To make it work I have to make an anonymous function this way: var maxVal = [ 1, 2, 3, 4, 5 ].reduce( function ( a, b ) { return Math.max(a, b); }, 0 ); Could someone tell me why ? Both are functions that take two arguments and both return one value. What's the difference? Another example could be this: var newList = [[1, 2, 3], [4, 5, 6]].reduce( Array.concat, [] ); The result is: [1

Inject functions with side-effects

ぃ、小莉子 提交于 2019-12-12 04:25:46
问题 I'm having an issue when using higher-order functions. Let's say I have the following code that doesn't use them (instead call global functions): import {db_insert} from 'some-db-lib' // function with side-effect const save_item = (item) => { // some logic like validating item data... db_insert(item) // call db_insert globally } const handle_request = (request) => { // some logic like sanitizing request... save_item(request.data) // call save_item globally } handle_request(some_request) And

Can't call VBA built in function with Application.Run

≯℡__Kan透↙ 提交于 2019-12-12 02:59:20
问题 In VBA a user defined subroutine or function can be called using the Application.Run method like so Application.Run "macroName", arg1 ', ... allowing for a crude simulation of delegates in VBA. However, built-in VBA functions cannot be called the same way ' Error 1004: Cannot run macro 'FileDateTime'. The macro may not be available ... Debug.Print Application.Run("FileDateTime", "<some file path>") Given the string of a built-in functions name e.g. "FileDateTime", how can that function be

Haskell higher order function to calculate length

穿精又带淫゛_ 提交于 2019-12-12 02:53:44
问题 I can't understand what's happening here? Can anybody please explain this code? How does this function calculate the length? callength = foldr (\_ n -> 1 + n) 0 Why does it use lambda, underscore, space between underscore and n and zero on the right hand side? 回答1: (\_ n -> 1 + n) simply means a function that takes two arguments, and returns one more than its second argument. The underscore simply means that a parameter is ignored. For comparison, as a top-level declaration without using a

How to rewrite an Array.every() in a custom Array.reduce() in javascript?

自作多情 提交于 2019-12-12 01:19:41
问题 This question is for purely for learning purposes. funfunfunction on youtube says that Any list higher order list transformation can be written in Array.reduce() Audio/video Reference: https://youtu.be/Wl98eZpkp-c?t=138. Question: Purely for learning how would one rewrite Array.every() with Array.reduce() This question stems from my previous question here Javascript Example: var approved1 = [ { dateApproved: new Date(), id: 1, }, { dateApproved: new Date(), id: 2, } ]; var approved2 = [ {

Elixir: What does a multiple-generator list comprehension look like without the syntax sugar?

流过昼夜 提交于 2019-12-11 21:42:36
问题 I'm trying to understand list comprehensions in Elixir. The example I'm looking at is producing the permutations of a string from this answer. def shuffle([], _), do: [[]] def shuffle(_, 0), do: [[]] def shuffle(list, i) do for x <- list, y <- shuffle(list, i-1), do: [x|y] end How does this double-generator comprehension look when re-written without the comprehension? I made an attempt to implement the algorithm myself, but my implementation is appending to the list, rather than prepending as