Function return a function that returns a function
NickName:Rien Ask DateTime:2014-12-30T20:46:46

Function return a function that returns a function

I would like to have a function returning a function that returns a function with the same signature as the first function. I.e. the function should be able to provide itself as a return value.

Is this possible in swift?

Example (this does not compile!):

typealias nextStep = ((char: CChar) -> nextStep)

func process(char: CChar) -> nextStep {...}

Copyright Notice:Content Author:「Rien」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27705796/function-return-a-function-that-returns-a-function

Answers
Airspeed Velocity 2014-12-30T14:08:31

Thomas's answer should work, but if you want a more typesafe alternative:\n\nstruct F {\n let f: (Character) -> F\n}\n\nfunc f(ch: Character) -> F {\n println(\"I've been called with an \\(ch)!\")\n return F(f: f)\n}\n\nlet g = f(\"a\")\nlet h = g.f(\"b\")\nh.f(\"c\")\n",


More about “Function return a function that returns a function” related questions

Function return a function that returns a function

I would like to have a function returning a function that returns a function with the same signature as the first function. I.e. the function should be able to provide itself as a return value. Is...

Show Detail

Return type of a Function which returns a Function

I have the below function which returns another function, where the getFirstPhoneNo() would return a string. get phones() { if (this._patientData && this._patientData.

Show Detail

Function that returns a function that returns a function

I've been given this function. It returns the function pair that it also returns the function f I think. That is the part that tricks me, I don't know what f(a, b) is and how to use it. def cons(a...

Show Detail

A return function that returns a function in javascript

Please i am having an issue with my java script. start_clock_ws : function(){ var that = this; ..decl function init(){ if(socket.isReady() ===

Show Detail

Return a function (that returns a string) inside a function with kotlin

I'm trying to return a function (that returns a string) inside of function but, since I haven't made this explicit (I'm not sure how to make it so that the function knows it'll return a function), ...

Show Detail

Function to return a function that returns a function, etc

Is it possible to define a function in a way that it basically returns itself as a delegate? For example, if this was valid syntax: public class Scrub { public NotNull NotNull<T>(T val...

Show Detail

Function that returns a function (Javascript)

I was looking at some javascript code to create a calculator app. It's straightforward, but what confused me was when there's function that returns a function. With the code below, I understand e...

Show Detail

Function return on an int returns undefined

I am making a simple function which sums each individual number in a string of numbers until it becomes a single unit, e.g. "55555" -> 5+5+5+5+5 = 25 -> 2+5 = 7 but my function always returns undef...

Show Detail

Function that returns referencie to function

I want to create function like this: using MyFuncType = std::function<ReturnType(const ArgType)>; const MyFuncType &getConstRef(const ReturnType t){ } I mean function that takes Retu...

Show Detail

Function returns pointer to function that returns pointer to function

I am trying to create a threaded state machine where each state returns a pointer to the next state. I have a number of state machines running so the next state of all these is stored in an array ...

Show Detail