fold

Difference between fold and reduce revisted

こ雲淡風輕ζ 提交于 2019-12-22 11:45:15
问题 I've been reading a nice answer to Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)? provided by samthebest and I am not sure if I understand all the details: According to the answer ( reduce vs foldLeft ): A big big difference (...) is that reduce should be given a commutative monoid, (...) This distinction is very important for Big Data / MPP / distributed computing, and the entire reason why reduce even exists. and Reduce is defined

Fold/curl edge of UIImageView

允我心安 提交于 2019-12-22 09:55:43
问题 I need to fold/unfold the edge of UIImageView to mark as a favorite. I searched across multiple sites, but have not found anything about it. I attached some example images, and, If possible, with animation effect. Thanks!! 回答1: One way to do it would be to animate the view usign OpenGL. There is a nice library for that called XBPageCurl However, you could achieve a much lighter solution by creating a mask for the curl effect and animate your view while transitioning to it. Here is what it

why foldl is not short circuiting with andFn function?

99封情书 提交于 2019-12-22 05:32:14
问题 My understanding is that foldl and foldr executes like : foldl f a [1..30] => (f (f (f ... (f a 1) 2) 3) ... 30) and foldr f a [1..30] => (f 1 (f 2 (f 3 (f ....(f 30 a)))))..) so.. foldr (&&) False (repeat False) can shortciruit as outermost f sees (&&) False ((&&) False (....)) sees first argument as false and does not need to evaluate the second argument (which is a large thunk). so what happens with andFn :: Bool -> Bool -> Bool andFn _ False = False andFn x True = x and foldl andFn True

Haskell - strict vs non-strict with foldl

♀尐吖头ヾ 提交于 2019-12-22 03:38:11
问题 I have a question concerning the definition of strict vs non-strict. The Haskell wiki-book for Laziness (http://en.wikibooks.org/wiki/Haskell/Laziness), under the section "Black-box strictness analysis", makes the following assertion: [Assuming a function f which takes a single parameter.] The function f is a strict function if, and only if, f undefined results in an error being printed and the halting of our program. The wiki contrasts const with id , showing a non-strict and strict function

Does haskell's foldr always take a two-parameter lambda?

余生颓废 提交于 2019-12-21 09:29:39
问题 Haskell newb here I'm working on this problem in haskell: (**) Eliminate consecutive duplicates of list elements. If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed. Example: * (compress '(a a a a b c c a a d e e e e)) (A B C A D E) The solution (which I had to look up) uses foldr: compress' :: (Eq a) => [a] -> [a] compress' xs = foldr (\x acc -> if x == (head acc) then acc else x:acc) [last xs] xs

“Any function on finite lists that is defined by pairing the desired result with the argument list can always be redefined in terms of fold”

懵懂的女人 提交于 2019-12-21 07:30:53
问题 I was reading through the paper A tutorial on the universality and expressiveness of fold, and am stuck on the section about generating tuples. After showing of how the normal definition of dropWhile cannot be defined in terms of fold, an example defining dropWhile using tuples was proved: dropWhile :: (a -> Bool) -> [a] -> [a] dropWhile p = fst . (dropWhilePair p) dropWhilePair :: (a -> Bool) -> [a] -> ([a], [a]) dropWhilePair p = foldr f v where f x (ys,xs) = (if p x then ys else x : xs, x

Scala Vector fold syntax (/: and :\ and /:\)

旧巷老猫 提交于 2019-12-21 07:29:31
问题 Can someone provide some examples for how /: :\ and /:\ Actually get used? I assume they're shortcuts to the reduce / fold methods, but there's no examples on how they actually get used in the Scala docs, and they're impossible to google / search for on StackOverflow. 回答1: /: is a synonym for foldLeft and :\ for foldRight . But remember that : makes /: apply to the object to the right of it. Assuming you know that (_ * _) is an anonymous function that's equivalent to (a, b) => a * b , and the

Scala Vector fold syntax (/: and :\ and /:\)

自古美人都是妖i 提交于 2019-12-21 07:29:18
问题 Can someone provide some examples for how /: :\ and /:\ Actually get used? I assume they're shortcuts to the reduce / fold methods, but there's no examples on how they actually get used in the Scala docs, and they're impossible to google / search for on StackOverflow. 回答1: /: is a synonym for foldLeft and :\ for foldRight . But remember that : makes /: apply to the object to the right of it. Assuming you know that (_ * _) is an anonymous function that's equivalent to (a, b) => a * b , and the

How to implement delete with foldr in Haskell

爷,独闯天下 提交于 2019-12-21 07:26:13
问题 I've been studying folds for the past few days. I can implement simple functions with them, like length , concat and filter . What I'm stuck at is trying to implement with foldr functions like delete , take and find . I have implemented these with explicit recursion but it doesn't seem obvious to me how to convert these types of functions to right folds. I have studied the tutorials by Graham Hutton and Bernie Pope. Imitating Hutton's dropWhile , I was able to implement delete with foldr but

Multiple folds in one pass using generic tuple function

≡放荡痞女 提交于 2019-12-20 20:38:09
问题 How can I write a function which takes a tuple of functions of type ai -> b -> ai and returns a function which takes a tuple of elements of type ai , one element of type b , and combines each of the elements into a new tuple of ai : That is the signature should be like f :: (a1 -> b -> a1, a2 -> b -> a2, ... , an -> b -> an) -> (a1, a2, ... , an) -> b -> (a1, a2, ... , an) Such that: f (min, max, (+), (*)) (1,2,3,4) 5 = (1, 5, 8, 20) The point of this is so I can write: foldlMult' t = foldl'