function-composition

Serialize Composed Func?

牧云@^-^@ 提交于 2019-12-05 20:29:39
问题 This works fine: Func<string, string> func1 = s => s + "func"; ViewState["function"] = func1; However, this does not: Func<string, string> func1 = s => s + "func"; Func<string, string> func2 = s => func1(s); ViewState["function"] = func2; It throws a runtime serialization exception: Type 'MyProjectName._Default+<>c__DisplayClass3' in Assembly 'MyProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. Now, I can work around this this time, but I'd

Generic function composition in Haskell

坚强是说给别人听的谎言 提交于 2019-12-05 08:08:25
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. When you do a 2 3 , you're not applying a to 2 arguments. You're actually applying a to it's only argument, resulting in a function, then take

Graphing n iterations of a function- Python

陌路散爱 提交于 2019-12-05 07:39:55
问题 I'm studying dynamical systems, particularly the logistic family g(x) = cx(1-x), and I need to iterate this function an arbitrary amount of times to understand its behavior. I have no problem iterating the function given a specific point x_0, but again, I'd like to graph the entire function and its iterations, not just a single point. For plotting a single function, I have this code: import numpy as np import scipy as sp import matplotlib.pyplot as plt def logplot(c, n = 10): dt = .001 x = np

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

我与影子孤独终老i 提交于 2019-12-05 05:54:51
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 = (catch _) andThen (eat _) story(new Cat, new Bird) I took the example with caution provided it's

Railway oriented programming with Async operations

偶尔善良 提交于 2019-12-05 05:35:14
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. The requirement is to give the caller either result or fault. All functions return a Tuple populated

Does haskell keep track of function composition?

耗尽温柔 提交于 2019-12-05 04:56:04
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) 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 same result for f1 and f2 , since that is the same function, yet compositionSplit f1 = ((+ 1), (+1)) and

Function composition in Haskell with tuple arguments [duplicate]

南楼画角 提交于 2019-12-05 01:13:25
This question already has an answer here: Feed elements of a tuple to a function as arguments in Haskell? 3 answers 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. What you're looking to do is to take a function that operates on curried arguments, h , and apply it to the result of f , which is a tuple. This process, turning a function of two

Composition of method reference

天涯浪子 提交于 2019-12-04 23:03:06
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. So my questions: Why do we need to type-cast or assign it to a variable first before we can call the

Function Composition VS Function Application

久未见 提交于 2019-12-04 22:59:51
问题 Do anyone can give example of function composition? This is the definition of function composition operator? (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) This shows that it takes two functions and return a function but i remember someone has expressed the logic in english like boy is human -> ali is boy -> ali is human What this logic related to function composition? What is the meaning of strong binding of function application and composition and which one is more strong

composition with dyadic operator?

亡梦爱人 提交于 2019-12-04 20:08:32
问题 I want to do something fairly simple; I am using the operator (++) with Data.Map insertWith , and it works fine, but I want to eliminate duplicates in the value created, so want to compose it with nub. I tried (nub (++)), (nub $ (++)), (nub . (++)), all to no avail, in that the type of (++) does not match the expected type of nub ( [a] ). I could of course define an auxiliary function or a lambda, but I think that probably there is a composition which would be clearer. Hints please! 回答1: You