fold

Generic Poly2 Folder case for shapeless Hlist

こ雲淡風輕ζ 提交于 2019-12-07 05:29:51
问题 I am trying to transform the following HList Some(C(15)) :: None :: Some(B(55)) :: None :: Some(A(195)) :: HNil to C(15) :: B(55) :: A(195) :: HNil Here is what I have at the moment : import shapeless._ case class A(value: Int) case class B(value: Int) case class C(value: Int) trait folderLP extends Poly2 { implicit def default[T, L <: HList] = at[T, L]((acc, t) => acc) } object folder extends folderLP { implicit def none[T, L <: HList] = at[None.type, L]((t, acc) => acc) implicit def

Difference between fold and reduce revisted

三世轮回 提交于 2019-12-06 13:40:40
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 formally as part of the MapReduce paradigm, I am not sure how this two statements combine. Can anyone

How to fold over a discriminated union

蓝咒 提交于 2019-12-06 12:27:39
I'm attempting to implement a fold over a discriminated union. The DU is called Expr, and represents a program expression, and is often recursive. I'm attempting to write a fold that accumulates the result of an operation over the Exprs recursively. Below is my attempt to write the fold. let rec foldProceduralExpr (folder : 's -> Expr list -> 's) (state : 's) (expr : Expr) : 's = let children = match expr with | Series s -> s.SerExprs | Lambda l -> [l.LamBody; l.LamPre; l.LamPost] | Attempt a -> a.AttemptBody :: List.map (fun ab -> ab.ABBody) a.AttemptBranches | Let l -> l.LetBody :: List

F# functional style approach much slower

折月煮酒 提交于 2019-12-06 10:25:15
Trying to learn F#, by solving some programming puzzles. I don't want to add too many details about the problem as I don't want to spoil the fun for others. Basically, the issue is to find all 4-uples { (i,j,k,l) | i ^ j ^ k ^ l != 0 } with no repetition (eg., (1,1,1,2) and (1,1,2,1) are the same and should be counted just once). I have found a O(n^3) approach which works, please see countImperative(a,b,c,d) below. But I also tried to refactor the code as to get rid of the nested for loops. However, I could not do so without a significant performance penalty. It was my impression that F#'s

How to write a function of type a-> b -> b -> b for folding a tree

被刻印的时光 ゝ 提交于 2019-12-06 09:47:37
问题 Some background: I have a foldT function (like foldr but for trees) of the following type in Haskell. foldT :: (a -> b -> b -> b) -> b -> Tree a -> b This foldT only takes type (a -> b -> b -> b) as an input function. I am trying to find a way to convert my tree into a list, and cannot figure out a way to make my append function take the form (a -> b -> b -> b). The following is ineffective because it is not the correct type: append x y z = append x:y:z Any help would be appreciated. Thank

Native implementation of reduceRight in JavaScript is wrong

狂风中的少年 提交于 2019-12-06 07:35:45
For an associative operation f over the elements of array a , the following relation should hold true: a.reduce(f) should be equivalent to a.reduceRight(f) . Indeed, it does hold true for operations that are both associative and commutative. For example: var a = [1,2,3,4,5,6,7,8,9,0]; alert(a.reduce(add) === a.reduceRight(add)); function add(a, b) { return a + b; } However it doesn't hold true for operations that are associative but not commutative. For example: var a = [[1,2],[3,4],[5,6],[7,8],[9,0]]; alert(equals(a.reduce(concat), a.reduceRight(concat))); function concat(a, b) { return a

Numerical issue with `foldl` and `foldr` in Haskell

情到浓时终转凉″ 提交于 2019-12-06 04:36:37
I have the following Haskell script which computes the function f(x) = (2- x) - (2^3 - x^3/12) calc x = (x - (x ^ 3) / 12) calc2 x = (calc 2) - (calc x) calcList1 :: [Float] -> Float calcList1 l = foldl (+) 0.0 (map calc2 l) calcList2 :: [Float] -> Float calcList2 l = foldr (+) 0.0 (map calc2 l) test1 :: Float -> Float test1 step = (calcList1 l) - (calcList2 l) where l = [0.0,step..2.0] Function calcList1 and calcList2 run calc2 function on each of list and then uses foldl and foldr respectively to sum the list. I was expecting both function to return the same answer but it does not. *Main>

Recursion in treeFold function

∥☆過路亽.° 提交于 2019-12-06 00:36:38
I have tree data type: data Tree a = Node { rootLabel :: a, -- label value subForest :: [Tree a] -- zero or more child trees } {-- Node (a) [] ..or... Node (a1) [ Node (a2) [..], Node (a3) [..] ] --} and i need to write treeFold function, i think that i need first function that make something with label value and second function that takes two results of func1 and make some final result and so on. I start writing the recursion function treeFold, write function for minimal Tree with 0 child trees, but i can't finish my function for not empty list of child's trees. I need to take 1st child and

What would be the “distinct method” that Traversable has in addition to Foldable?

≡放荡痞女 提交于 2019-12-05 23:21:26
问题 Foldable is a superclass of Traversable, similarly to how Functor is a superclass of Applicative and Monad . Similar to the case of Monad , where it is possible to basically implement fmap as liftM :: Monad m => (a->b) -> m a -> m b liftM f q = return . f =<< q we could also emulate foldMap as foldLiftT :: (Traversable t, Monoid m) => (a -> m) -> t a -> m foldLiftT f = fst . traverse (f >>> \x -> (x,x)) -- or: . sequenceA . fmap (f >>> \x -> (x, x)) using the Monoid m => (,) m monad. So the

Fold/curl edge of UIImageView

China☆狼群 提交于 2019-12-05 19:53:27
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!! 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 would look like [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3]; UIImage