fold

foldRight on infinite lazy structure

一世执手 提交于 2019-11-29 06:11:04
问题 According to http://en.wikipedia.org/wiki/Fold_(higher-order_function), a right fold can operate on infinite lists if the full list does not have to be evaluated. This can be seen in action in haskell: Prelude> take 5 (foldr (:) [] [1 ..]) [1,2,3,4,5] This does not seem to work well in scala for streams: Stream.from(1).foldRight(Stream.empty[Int])( (i, s) => i #:: s).take(5) // StackOverflowError or on iterators: Iterator.from(1).foldRight(Iterator.empty: Iterator[Int]){ (i, it) => Iterator

Folding, function composition, monads, and laziness, oh my?

感情迁移 提交于 2019-11-29 05:53:37
问题 I am puzzled. I can write this: import Control.Monad main = print $ head $ (foldr (.) id [f, g]) [3] where f = (1:) g = undefined and the output is 1 . That makes sense, because it reduces to: main = print $ head $ ((1:) . undefined . id) [3] main = print $ head $ (1:) ((undefined . id) [3]) main = print $ head $ 1 : ((undefined . id) [3]) main = print $ 1 But if I use a vaguely similar monadic technique, it doesn't work the same: import Control.Monad main = print $ (foldr (<=<) return [f, g]

Interleave List of Lists in Haskell

倾然丶 夕夏残阳落幕 提交于 2019-11-29 04:19:27
I was wondering how could I write a function in Haskell that interleaves a list of lists into a single lists, for example, if I had a function called interleavelists :: [[a]] -> [a] it should be able to interleave the elements. Example: [[1,2,3] [4,5,6] [7,8]] --> [1,4,7,2,5,8,3,6] . The lists can be both finite or infinite... Can I use foldr ? Daniel Fischer The quickest way to write it is to use transpose from Data.List . import Data.List interleavelists :: [[a]] -> [a] interleavelists = concat . transpose transpose picks the first element of each non-empty list of its argument, putting them

Are there non-trivial Foldable or Traversable instances that don't look like containers?

前提是你 提交于 2019-11-28 21:06:42
问题 There are lots of functors that look like containers (lists, sequences, maps, etc.), and many others that don't (state transformers, IO , parsers, etc.). I've not yet seen any non-trivial Foldable or Traversable instances that don't look like containers (at least if you squint a bit). Do any exist? If not, I'd love to get a better understanding of why they can't. 回答1: Every valid Traversable f is isomorphic to Normal s for some s :: Nat -> * where data Normal (s :: Nat -> *) (x :: *) where --

What is basic difference between fold and reduce in Kotlin? When to use which?

﹥>﹥吖頭↗ 提交于 2019-11-28 19:25:54
问题 I am going through basics of Kotlin and I am pretty confused with this both functions fold() and reduce() in Kotlin, can anyone give me a concrete example which distinguishes both of them? 回答1: fold takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters. For example, take the following code that calculates the sum of a list of integers: listOf(1, 2, 3).fold(0) { sum, element -> sum +

Why doesn't Option have a fold method?

喜欢而已 提交于 2019-11-28 19:09:22
I wonder why scala.Option doesn't have a method fold like this defined: fold(ifSome: A => B , ifNone: => B) equivalent to map(ifSome).getOrElse(ifNone) Is there no better than using map + getOrElse ? Debilski You can do: opt foldLeft (els) ((x, y) => fun(x)) or (els /: opt) ((x,y) => fun(x)) (Both solutions will evaluate els by value, which might not be what you want. Thanks to Rex Kerr for pointing at it.) Edit: But what you really want is Scalaz’s catamorphism cata (basically a fold which not only handles the Some value but also maps the None part, which is what you described) opt.cata(fun,

iOS Paper fold (origami / accordion) effect animation, with manual control

人走茶凉 提交于 2019-11-28 18:03:07
I'm looking for tips on how to implement the popular 'paper folding / origami' effect in my iOS project. I'm aware of projects such as: https://github.com/xyfeng/XYOrigami but they only offer the 'animated' effect, with no manual control over the opening animation. I've struggled to dissect that project and come up with what I'm after. To be more exact, I'm looking on how to implement the effect shown here: http://vimeo.com/41495357 where the folding animation is not simply animated open, but the user controls the opening folds. Any help would be much appreciated, thanks in advance! EDIT: Okay

Why does this Haskell code work successfully with infinite lists?

吃可爱长大的小学妹 提交于 2019-11-28 17:50:51
I have some Haskell code that does work correctly on an infinite list, but I do not understand why it can do so successfully. (I modified my original code -- that did not handle infinite lists -- to incorporate something from some other code online, and suddenly I see that it works but don't know why). myAny :: (a -> Bool) -> [a] -> Bool myAny p list = foldr step False list where step item acc = p item || acc My understanding of foldr is that it will loop through every item in the list (and perhaps that understanding is incomplete). If so, it should not matter how the "step" function is

Difference between fold and reduce?

假如想象 提交于 2019-11-28 17:41:17
Trying to learn F# but got confused when trying to distinguish between fold and reduce . Fold seems to do the same thing but takes an extra parameter. Is there a legitimate reason for these two functions to exist or they are there to accommodate people with different backgrounds? (E.g.: String and string in C#) Here is code snippet copied from sample: let sumAList list = List.reduce (fun acc elem -> acc + elem) list let sumAFoldingList list = List.fold (fun acc elem -> acc + elem) 0 list printfn "Are these two the same? %A " (sumAList [2; 4; 10] = sumAFoldingList [2; 4; 10]) Fold takes an

Implementing take using foldr

匆匆过客 提交于 2019-11-28 13:33:48
This is my take version using foldr : myTake n list = foldr step [] list where step x y | (length y) < n = x : y | otherwise = y main = do print $ myTake 2 [1,2,3,4] The output is not what I expect: [3,4] I then tried to debug by inserting the length of y into itself and the result was: [3,2,1,0] I don't understand why the lengths are inserted in decreasing order. Perhaps something obvious I missed? If you want to implement take using foldr you need to simulate traversing the list from left to right. The point is to make the folding function depend on an extra argument which encodes the logic