functional-programming

Generics and Functional programming in Swift

橙三吉。 提交于 2019-12-19 09:10:04
问题 The two variants of the sum function below are my attempt to repeat the lisp version introduced by Abelson and Sussman in the classic "Structure and interpretation of Computer Programs" book in Swift. The first version was used to compute the sum of integers in a range, or the sum of squares of integers in a range, and the second version was used to compute the approximation to pi/8. I was not able to combine to the versions into a single func which will handle all types. Is there a clever

How do I convert a vector of strings to a vector of integers in a functional way?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 08:13:37
问题 I'm trying to convert Vec<&str> to Vec<u16> but I can't figure out a functional way to do it. let foo: &str = "1,2,3"; // Parsing a string here let bar: Vec<&str> = foo.split(",").collect(); // Bar is a nice vector of &str's I need to get bar into a Vec<u16> . 回答1: There's an iterator adapter map! You'd use it like this: let bar: Vec<u16> = foo.split(",").map(|x| x.parse::<u16>().unwrap()).collect(); parse is a library function that relies on the trait FromStr, and it can return an error, so

Pattern matching with guards vs if/else construct in F#

对着背影说爱祢 提交于 2019-12-19 06:34:26
问题 In ML-family languages, people tend to prefer pattern matching to if/else construct. In F#, using guards within pattern matching could easily replace if/else in many cases. For example, a simple delete1 function could be rewritten without using if/else (see delete2 ): let rec delete1 (a, xs) = match xs with | [] -> [] | x::xs' -> if x = a then xs' else x::delete1(a, xs') let rec delete2 (a, xs) = match xs with | [] -> [] | x::xs' when x = a -> xs' | x::xs' -> x::delete2(a, xs') Another

Transposing arbitrary collection-of-collections in Scala

独自空忆成欢 提交于 2019-12-19 06:02:44
问题 I have to often transpose a "rectangular" collection-of-collections in Scala, e.g.: a list of maps, a map of lists, a map of maps, a set of lists, a map of sets etc. Since collections can be uniformly viewed as a mapping from a specific domain to a co-domain (e.g.: a List[A]/Array[A] is a mapping from the Int domain to the A co-domain, Set[A]is a mapping from the A domain to the Boolean co-domain etc.), I'd like to write a clean, generic function to do a transpose operation (e.g.: turn a map

How to iterate through a module's functions [duplicate]

旧街凉风 提交于 2019-12-19 05:35:12
问题 This question already has answers here : How to list all functions in a Python module? (14 answers) Closed 5 years ago . I have this function call after importing foo.py. Foo has several methods that I need to call e.g. foo.paint, foo.draw: import foo code if foo: getattr(foo, 'paint')() I need to use a while loop to call and iterate through all the functions foo.paint, foo.draw etc. How do i go about it? 回答1: You can use foo.__dict__ somehow like this: for name, val in foo.__dict__.iteritems

How to iterate through a module's functions [duplicate]

折月煮酒 提交于 2019-12-19 05:34:13
问题 This question already has answers here : How to list all functions in a Python module? (14 answers) Closed 5 years ago . I have this function call after importing foo.py. Foo has several methods that I need to call e.g. foo.paint, foo.draw: import foo code if foo: getattr(foo, 'paint')() I need to use a while loop to call and iterate through all the functions foo.paint, foo.draw etc. How do i go about it? 回答1: You can use foo.__dict__ somehow like this: for name, val in foo.__dict__.iteritems

Is there a filter() opposite builtin?

余生长醉 提交于 2019-12-19 05:14:33
问题 Is there a function in Python that does the opposite of filter ? I.e. keeps the items in the iterable that the callback returns False for? Couldn't find anything. 回答1: No, there is no built-in inverse function for filter() , because you could simply invert the test . Just add not : positive = filter(lambda v: some_test(v), values) negative = filter(lambda v: not some_test(v), values) The itertools module does have itertools.ifilterfalse(), which is rather redundant because inverting a boolean

Difference between $ and ()

余生颓废 提交于 2019-12-19 05:09:17
问题 I started learning Haskell and I encountered a problem I can't just understand. I've got a method used to find value from a list of key-value list (from this page): let findKey key xs = snd . head . filter (\(k,v) -> key == k) $ xs I tried fiddling with a bit and decided to get rid of $ sign in this way: let findKey key xs = snd . head . filter (\(k,v) -> key == k) ( xs ) However, it doesn't even parse (filter applied to too many argumens error). I've read that $ sign is used to simply

Monad more powerful than Applicative?

谁都会走 提交于 2019-12-19 05:00:50
问题 I looked at past discussion but could not see why any of the answers are actually correct. Applicative <*> :: f (a -> b) -> f a -> f b Monad (>>=) :: m a -> (a -> m b) -> m b So if I get it right, the claim is that >>= cannot be written by only assuming the existence of <*> Well, let's assume I have <*> . And I want to create >>= . So I have f a . I have f (a -> b) . Now when you look at it, f (a -> b) can be written as (a -> b) (if something is a function of x, y , z - then it's also a

Derive Eq and Show for type alias in Haskell

大憨熊 提交于 2019-12-19 04:14:11
问题 I've the following type alias data Bindable = Const Value | Variable Location | Func Function | Proc deriving (Eq, Show) type Function = Argument -> Store -> Value but the compiler gives me an error No instance for (Show Function) arising from the 'deriving' clause of a data type declaration Possible fix: add an instance declaration for (Show Function) or use a standalone 'deriving instance' declaration, so you can specify the instance context yourself When deriving the instance for (Show