function-composition

Is there a way to chain functions like withCString?

别说谁变了你拦得住时间么 提交于 2019-12-09 14:50:06
问题 Is there a way to chain functions like withCString ? By that I mean any function that looks something like f :: Foo -> (CFoo -> IO a) -> IO a . For example, lets say there is a function cFunc :: CString -> CFoo -> CBar -> IO () Usualy, I would do something like: haskellFunc string foo bar = withCString string $ \ cString -> withCFoo foo $ \ cFoo -> withCBar bar $ \ cBar -> cFunc cString cFoo cBar But i would like to do something like: haskellFunc = (withCString |.| withCFoo |.| withCBar)

Why doesn't Function2 have an andThen method?

安稳与你 提交于 2019-12-09 08:49:18
问题 Why does andThen only exist for single argument functions in Scala? The following code works: val double = (x: Int) => x * 2 val timesFour = double andThen double But why is there no andThen method for multi argument functions? val multiply = (x: Int, y: Int) => x * y val multiplyAndDouble = multiply andThen double <console>:10: error: value andThen is not a member of (Int, Int) => Int Surely it is trivial to add this method. Is there a reason it been omitted from the standard library? 回答1: I

How to write this polyvariadic composition function in Haskell?

不打扰是莪最后的温柔 提交于 2019-12-08 19:41:57
问题 Note: This is a repost of another question, which the author deleted. Here's the original question: I have this polyvariadic comp function in Javascript and was wondering if a similar implementation in Haskell were possible. I am mostly interested in comp 's type: const comp = f => Object.assign( g => comp([g].concat(f)), {run: x => f.reduce((acc, h) => h(acc), x)} ); const inc = n => n + 1; const sqr = n => n * n; const repeatStr = s => n => Array(n + 1).join(s); comp(repeatStr("*")) (inc)

efficient list append/prepend through function composition

↘锁芯ラ 提交于 2019-12-07 01:03:11
问题 Some months ago I read somewhere of an efficient approach for appending and prepending lists to other lists in O(1) by representing them with function compositions that, once evaluated, build in O(n) the resulting list. Unfortunately I cannot remember the source of this article or (if existing) the name of this technique/approach. Do you have references about it, please? 回答1: The data structure is called a difference list (or DList for short). You can find a default implementation of it in a

Composition of method reference

最后都变了- 提交于 2019-12-06 19:02:54
问题 This is related to this question: How to do function composition? I noticed that a method reference can be assigned to a variable declared as Function , and so I assume it should have andThen or compose function, and hence I expect that we can compose them directly. But apparently we need to assign them to a variable declared as Function first (or type-cast before invocation) before we can call andThen or compose on them. I suspect I might have some misconception about how this should work.

Function composition in Haskell with tuple arguments [duplicate]

我的未来我决定 提交于 2019-12-06 18:23:24
问题 This question already has answers here : Feed elements of a tuple to a function as arguments in Haskell? (3 answers) Closed 4 years ago . Sometimes I have two functions of the form: f :: a -> (b1,b2) h :: b1 -> b2 -> c and I need the composition g. I solve this by changing h to h': h' :: (b1,b2) -> c Can you please show me (if possible) a function m, so that: (h . m . f) == (h' . f) Or another way to deal with such situations. Thanks. 回答1: What you're looking to do is to take a function that

How to know whether a symbol represents function or macro?

不羁岁月 提交于 2019-12-06 11:56:55
I'm writing a macro for function / macro composition (mixed combinations are possible). Inside of the macro I have to treat symbols which represent functions and those that name macros differently. It is because result function must work with any number of arguments (if 'lowest' function in composition can), and I cannot apply apply to macros. My question: how to determine what a given symbol represents: function or macro? Macro: CL-USER 8 > (macro-function 'bar) NIL CL-USER 9 > (macro-function 'lambda) #<Function LAMBDA 41100B7E94> Function: CL-USER 15 > (and (fboundp '+) (not (macro-function

How to functional compose transforms of objects via transducers

微笑、不失礼 提交于 2019-12-06 06:22:47
Live code example I'm trying to learn transducers via egghead and I think I got it until we try to compose object transformation. I have an example below that doesn't work const flip = map(([k,v]) => ({[v]: k})); const double = map(([k,v]) => ({[k]: v + v})); seq(flip, {one: 1, two: 2}); /*?*/ {1: 'one', 2: 'two'} seq(double, {one: 1, two: 2}); /*?*/ {'one': 2, 'two: 4} but if I compose it fails: seq(compose(flip, double), {one: 1, two: 2}); /*?*/ {undefined: NaN} seq(compose(double, flip), {one: 1, two: 2}); /*?*/ {undefined: undefined} How can I work with objects using transducers with fp

Javascript console output before and after method call with AOP

送分小仙女□ 提交于 2019-12-06 05:29:12
I would like to measure the computing time of methods. A nice way is ( How do you performance test JavaScript code? ) with console.time('Function #1'); and console.timeEnd('Function #1'); My idea is to add these console outputs on lifecycle-methods. In this case using SAPUI5 like createContent:funtion(){}; methods. This should be possible with AOP using before() and after() to runt the time counting. Which AOP framework would you suggest and how to implement it with the need of modifying the identification string "Function #1" automatically? There actually is no need for aspects in Javascript

Default andThen() method in BiFunction interface

别等时光非礼了梦想. 提交于 2019-12-06 01:25:18
问题 There's a default method andThen() in the BiFunction interface ( java.util.function package). default <V> BiFunction<T,U,V> andThen(Function<? super R,? extends V> after) The documentation says: Returns a composed function that first applies this function to its input, and then applies the after function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function. It's little confusing to understand what the explanation means. As