function-composition

How to functional compose transforms of objects via transducers

旧城冷巷雨未停 提交于 2020-01-02 14:31:23
问题 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),

Applying a function to an arbitrarily long list of arguments

半城伤御伤魂 提交于 2019-12-24 05:17:09
问题 I want to create a function apply that takes a function with an arbitrary amount of arguments as well as a list of integers, and returns the result of the function (Where each integer in the list is an argument in order. I was thinking something like: apply :: ([Int] -> Int) -> [Int] -> Int apply f x:xs = apply (f x) xs apply f [] = f But I know this won't work because the type signature is wrong - the function doesn't take a list of ints, it just takes some amount of int arguments.

Composing functions with multiple arguments

风流意气都作罢 提交于 2019-12-24 04:29:04
问题 How can I compose a function where apply takes more than one argument? Here is a contrived example: val sum: List[Int] => Int = l => l.sum val double: Int => Int = i => i * i double.compose(sum).apply(List(1,2,3)) //=> 36 val sumAppend: (List[Int], Int) => Int = (l, i) => i :: l sum double.compose(sumAppend).apply(List(1,2,3), 1) // Attempt to append 1 to list then sum The above give me a typed inferred error? 回答1: Define compose2 , for instance as an extension method for Function1 : implicit

Concise syntax for function composition in Scala?

让人想犯罪 __ 提交于 2019-12-23 08:30:22
问题 I'm learning Scala and ran across the following task - if string is blank then return null, otherwise uppercase it. There are two functions in Apache Commons that composed together solves the problem. In Haskell I'd just write: upperCaseOrNull = StringUtils.stripToNull . StringUtils.upperCase However I can't find a way to do an easy and clean function composition in Scala. the shortest way I found is as follows: def upperCaseOrNull (string:String) = StringUtils.stripToNull (StringUtils

Is there a way to inject a try catch inside a function?

余生长醉 提交于 2019-12-23 05:17:30
问题 Maybe some of you know about AOP, in some languages using AOP can lead you to be able to inject code after, before, or while a method is executing,etc. What I want is to apply the same in Javascript, I am currently working on a massive app which has more than 300 ajax calls, and every time I need to do some change on the catch statement on them, I have to modify them one by one which is very tedious. What I want to do is something like : functionName.before("try {") functionName.after("}

Javascript console output before and after method call with AOP

感情迁移 提交于 2019-12-22 13:49:11
问题 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

Why function composition sometimes requires two “.” 's to combine two functions

霸气de小男生 提交于 2019-12-22 05:59:30
问题 So this question is simple but I can't seem to grasp the concept. To compose ordinary functions, one can just do something like below: lowerNoSpaces = filter (/= ' ') . map toLower But, on occasion, there are times where this won't work: myConcatMap = concat . map It gives the error: <interactive>:236:1: error: * Non type-variable argument in the constraint: Foldable ((->) [a1]) (Use FlexibleContexts to permit this) * When checking the inferred type concattMap :: forall a1 a2. Foldable ((->)

Scala API 2.10.*: Function2.andThen what happened to?

二次信任 提交于 2019-12-22 04:40:52
问题 I'm reading «Scala in Depth» by Joshua Suereth, book that I've bought for the clearly established competency of the author. I'm on page 3 and after a bunch of typos and incoherent formatting (ok, I've become tolerant of these errors) I stumbled upon the following example about a functional approach to solve a very simple scenario. trait Cat trait Bird trait Catch trait FullTummy def catch(hunter: Cat, prey: Bird): Cat with Catch def eat(consumer: Cat with Catch): Cat with FullTummy val story

Haskell dot operator

ぃ、小莉子 提交于 2019-12-22 02:00:49
问题 I try to develop a simple average function in Haskell. This seems to work: lst = [1, 3] x = fromIntegral (sum lst) y = fromIntegral(length lst) z = x / y But why doesn't the following version work? lst = [1, 3] x = fromIntegral.sum lst y = fromIntegral.length lst z = x / y 回答1: You're getting tripped up by haskell's precedence rules for operators, which are confusing. When you write x = fromIntegral.sum lst Haskell sees that as the same as: x = fromIntegral.(sum lst) What you meant to write

Why does the dot compose from right to left in Haskell?

你说的曾经没有我的故事 提交于 2019-12-21 12:12:03
问题 If we have two functions, f and g , then in Haskell h = f . g is equivalent to h x = f(g x) . I.e. the functions are applied from right to left to the input. Is there any fundamental reason why it goes from right to left, and not from left to right? I.e. why didn't they make h = f . g equivalent to h x = g(f x) instead? EDIT: as others pointed out my equivalent functions where the wrong way around, so I fixed those. 回答1: First of all, there's a mistake in your [original, unedited] question: h