function-composition

JS functions composed in the opposite order

巧了我就是萌 提交于 2019-12-11 15:46:01
问题 Starting with a simple function composition const fa = (x => (x + "a")); const fb = (x => (x + "b")); fb(fa('x')) I played around and I obtained the following code snippet which returns 'xba' instead of 'xab'. Can someone explain why? const fa = next => x => next(x + "a"); const fb = next => x => next(x + "b"); console.log(fb(fa(y => y))('x')); 回答1: Let's break this down: const _fa = fa(y => y) // _fa = x => (y => y)(x + "a") To avoid confusing the two x let's name it as x1 // _fa = x1 => (y

Calculating the expected value of a transformed random variable in MATLAB?

ⅰ亾dé卋堺 提交于 2019-12-11 13:48:53
问题 I am trying to compute the following expected value for Z being lognormally distributed E[Z^eta w(F_Z (Z))^-eta] where eta is a real number, F_Z the distribution function of Z and w:[0,1]->[0,1] an increasing function. First of all, I am pretty new to Matlab so I don't know which way of integrating is the better one, numerically or symbolically. I tried symbolically. My idea was to subsequently define functions: syms x; g_1(x) = x^eta; g_2(x) = logncdf(x); g_2(x) = w(x)^-eta; g_4(x) = g_1(x)

Chaining REST calls in a pipeline while managing errors

ぐ巨炮叔叔 提交于 2019-12-11 09:43:54
问题 Coming from nodejs where I could chain asynchronous events using Promises and then operator I'm trying to explore how things are done in idiomatic F#. The calls I'm trying to chain are HTTP rest calls on some entity from creation to update to uploading images to publishing. Function composition says the output of one function should match the input of the second one to be composed and that common input and output in my case will be string , i.e. JSON serialized string as input and output of

chaining async rest calls in a pipeline while managing errors

谁都会走 提交于 2019-12-11 06:11:33
问题 Building on previous question based on synchronous calls, how do we approach asynchronous methodology in the following scenario. let fetch1 (result: string) : Result<string, string> = try use request = WebRequest.Create("http://bing.com") :?> HttpWebRequest use response = request.GetResponse() use reader = new StreamReader(response.GetResponseStream()) let html = reader.ReadToEnd() Ok "success" with | :? WebException as e -> Error "error with the url" let fetch2 (result: string) : Result

Haskell Type Error From Function Application to Function Composition

自闭症网瘾萝莉.ら 提交于 2019-12-11 01:41:43
问题 This question is related to this Function Composition VS Function Application which answered by antal s-z. How you can get this ? map has type (a -> b) -> [a] -> [b] head has type [a] -> a map head has type [[a]] -> [a] Why the following code has type error for function composition ? test :: [Char] -> Bool test xs = not . null xs getMiddleInitials :: [String] -> [Char] getMiddleInitials middleNames = map head . filter (\mn -> not . null mn) middleNames but this does not have type error

Haskell: Types of function composition not matching

廉价感情. 提交于 2019-12-11 01:24:08
问题 I am having some troubles with function composition and types. I would like to compose filter (which returns a list) with len , which takes a list as an argument (technically a Foldable but I am simplifying here). Looking at the types everything is as expected: > :t length length :: Foldable t => t a -> Int > :t filter filter :: (a -> Bool) -> [a] -> [a] So now I would expect the type of (len . filter) to be (length . filter) :: (a -> Bool) -> [a] -> Int while in reality is > :t (length .

Better Function Composition in Python

▼魔方 西西 提交于 2019-12-10 12:37:25
问题 I work in Python. Recently, I discovered a wonderful little package called fn. I've been using it for function composition. For example, instead of: baz(bar(foo(x)))) with fn, you can write: (F() >> foo >> bar >> baz)(x) . When I saw this, I immediately thought of Clojure: (-> x foo bar baz) . But notice how, in Clojure, the input is on the left. I wonder if this possible in python/fn. 回答1: You can't replicate the exact syntax, but you can make something similar: def f(*args): result = args[0

Generic function composition in Haskell

穿精又带淫゛_ 提交于 2019-12-10 04:40:18
问题 I was reading here, and I noticed that, for example, if I have the following function definitions: a :: Integer->Integer->Integer b :: Integer->Bool The following expression is invalid : (b . a) 2 3 It's quite strange that the functions of the composition must have only one parameter. Is this restriction because some problem in defining the most generic one in Haskell or have some other reason? I'm new to Haskell, so I'm asking maybe useless questions. 回答1: When you do a 2 3 , you're not

Does haskell keep track of function composition?

梦想的初衷 提交于 2019-12-10 03:41:38
问题 I was wondering if Haskell keeps track of weather a function is a function composition, i.e would it be possible for me to define a function that does something similar to this?: compositionSplit f.g = (f,g) 回答1: No, it wouldn't be possible. For example, f1 = (+ 1) . (+ 1) :: Int -> Int is the same function as f2 = subtract 1 . (+ 3) :: Int -> Int and referential transparency demands that equals can be substituted for equals, so if compositionSplit were possible, it would need to produce the

Railway oriented programming with Async operations

て烟熏妆下的殇ゞ 提交于 2019-12-10 03:41:37
问题 Previously asked similar question but somehow I'm not finding my way out, attempting again with another example. The code as a starting point (a bit trimmed) is available at https://ideone.com/zkQcIU. (it has some issue recognizing Microsoft.FSharp.Core.Result type, not sure why) Essentially all operations have to be pipelined with the previous function feeding the result to the next one. The operations have to be async and they should return error to the caller in case an exception occurred.