Skip to main content
13 events
when toggle format what by license comment
Dec 17, 2021 at 2:14 comment added polcott I think that the x86 emulator that I am using passes function pointers and the parameters to these functions because it is implementing C++ inheritance from scratch in C.
Mar 21, 2017 at 20:50 comment added Stack Exchange Broke The Law @JaredSmith Passing the function, yes, passing its parameters, only if your language doesn't support closures.
Mar 21, 2017 at 17:50 comment added jpmc26 This decoupling also has the benefit that the formatting function can be used for a completely separate rate computed using totally different arguments, without modifying or re-implementing GetFormattedRate. Also, I think FormatRate is a better name for the function as implemented in this answer.
Mar 21, 2017 at 16:12 vote accept NightWatchman
Mar 21, 2017 at 16:14
Mar 21, 2017 at 14:43 comment added user949300 @Bergi, Kroltan: your lambdas are a fine idea. They are, in effect, functional composition / currying, as suggested in my comment and in Jack's answer. I guess "lambda" is the trendy terminology nowadays and I should have used that. :-)
Mar 21, 2017 at 13:49 comment added Jared Smith Note that passing a function and its parameters to another function is a totally valid way to delay evaluation in a language without lazy semantics en.wikipedia.org/wiki/Thunk
Mar 21, 2017 at 13:36 comment added Kroltan @user949300 Since the question is C#, the API can have a standard function interface, and the user can use a lambda to pass any other required data. e.g. string GetFormattedRate(Func<Thing, float> getter) and then GetFormattedRate(thing => ComplexLogic(thing, also, many, params)).
Mar 21, 2017 at 11:59 comment added Bergi @user949300 Only if your language does not support closures or when lamdas are a hazzle to type out
Mar 21, 2017 at 7:40 comment added Thomas Junk @user949300 In that case, one should think about, whether it is right doing it in such a way.
Mar 21, 2017 at 3:23 comment added jonrsharpe @user949300 maybe, but that's not the OP's use case (and if it was, perhaps it's formatRate that should be mapped over the rates that should be formatted).
Mar 21, 2017 at 2:09 history edited Stack Exchange Broke The Law CC BY-SA 3.0
deleted 1 character in body
Mar 21, 2017 at 2:06 comment added user949300 In this case you are correct. But if the inner function were called multiple times, say in a loop or a map function, then the ability to pass in arguments would be useful. Or use functional composition / currying as proposed in @Jack answer.
Mar 20, 2017 at 23:22 history answered Stack Exchange Broke The Law CC BY-SA 3.0