fold

Is implementing this words function possible without a postprocessing step after folding?

末鹿安然 提交于 2020-05-12 11:19:10
问题 Real World Haskell, chapter 4, page 98 of the print asks if words can be implemented using folds, and this is my question too: Is it possible? If not, why? If it is, how? I came up with the following, which is based on the idea that each non-space should be prepended to the last word in the output list (this happens in the otherwise guard), and that a space should trigger the appending of an emtpy word to the output list if there is not one already (this is handled in the if - then - else ).

python reduce to find the union of sets

怎甘沉沦 提交于 2020-03-15 21:56:30
问题 I am trying to find the union of set of sets. Specifically I want the union of the list of nodes for each key in the dictionary of networkx graphs called periodic_gs . I would like to use the reduce function as it seems reasonable to take the union of all periodic_gs[x].nodes() where x is a key of periodic_gs . Here is my attempt: reduce(lambda x,y: set(periodic_gs[x].nodes()).union(set(periodic_gs[y].nodes())), periodic_gs.keys(), {}) To me, this says take the union of the nodes across each

python reduce to find the union of sets

情到浓时终转凉″ 提交于 2020-03-15 21:56:21
问题 I am trying to find the union of set of sets. Specifically I want the union of the list of nodes for each key in the dictionary of networkx graphs called periodic_gs . I would like to use the reduce function as it seems reasonable to take the union of all periodic_gs[x].nodes() where x is a key of periodic_gs . Here is my attempt: reduce(lambda x,y: set(periodic_gs[x].nodes()).union(set(periodic_gs[y].nodes())), periodic_gs.keys(), {}) To me, this says take the union of the nodes across each

How to make this Scheme function not tail recursive?

南楼画角 提交于 2020-03-04 21:27:21
问题 I can't figure out how can I make this tail recursive Scheme function not tail recursive anymore. Anyone can help me? (define (foldrecl f x u) (if (null? x) u (foldrecl f (cdr x) (f (car x) u)))) 回答1: left folds are inheritly iterative, but you can easily make them recursive by adding a continuation. eg. (let ((value expresion-that-calculates)) value) So in your case: (define (foldrecl f x u) (if (null? x) u (let ((result (foldrecl f (cdr x) (f (car x) u)))) result))) While this looks

Scala : zipWith[A,B,C](f: Function2[A,B,C], l1 :List[A], l2 :List[B]) : List[C] Method

佐手、 提交于 2020-01-25 21:31:23
问题 So i need to implement a function that takes two lists and a function. The function then uses the elements of the two lists and applies the function on the elements and saves them into a list by using map and/or fold and the functions from the list class. Example: • zipWith((x: Int, y: Int) => x + y, List(1, 2, 3), List(4, 5, 6)) → List(5, 7, 9) • zipWith((x:Int,y:Int) => x, List(1,2,3), List(4,5,6)) → List(1, 2, 3) I do not know how to use the passed function and apply it on the two lists.

What are practical examples of the higher-order functions foldl and foldr?

心不动则不痛 提交于 2020-01-25 03:43:05
问题 The typical academic example is to sum a list. Are there real world examples of the use of fold that will shed light on its utility ? 回答1: fold is perhaps the most fundamental operation on sequences. Asking for its utility is like asking for the utility of a for loop in an imperative language. Given a list (or array, or tree, or ..), a starting value, and a function, the fold operator reduces the list to a single result. It is also the natural catamorphism (destructor) for lists. Any

C++17 fold expression syntax?

只谈情不闲聊 提交于 2020-01-21 04:38:06
问题 I am trying to use compact fold expression without success. For instance here is a working C++17 code template <bool... B> struct Fold_And : std::integral_constant<bool, (B && ...)> { }; template <bool... B> constexpr auto Fold_And_v = Fold_And<B...>::value; template <typename V, typename... Vs> std::enable_if_t< Fold_And_v<std::is_floating_point_v<V>, std::is_floating_point_v<Vs>...> > foo(const V& v, const Vs&...) { } I want to translate it into a more compact form (not using the

R, iterating over the row vectors of a matrix

我怕爱的太早我们不能终老 提交于 2020-01-14 14:59:31
问题 I have some vector vect and I want to iterate over the row vectors v of a matrix and calculate: cov(v, vect) . I tried: for(vect in mat2) #where mat2 is a 215 by 31 matrix However, each vector appeared to be a scalar with value 1. How do I iterate over the row vectors of a matrix? To make this even better, since I am interested in calculating the sum of cov(v, vect) where v is a row vector, how can I use the higher-order functions left-fold and right-fold 回答1: Are you looking for apply ?

How can this function be written using foldr?

放肆的年华 提交于 2020-01-14 14:07:41
问题 I have this simple function which returns a list of pairs with the adjacents elements of a list. adjacents :: [a] -> [(a,a)] adjacents (x:y:xs) = [(x,y)] ++ adjacents (y:xs) adjacents (x:xs) = [] I'm having problems trying to write adjacents using foldr . I've done some research but nothing seems to give me a hint. How can it be done? 回答1: Tricky folds like this one can often be solved by having the fold build up a function rather than try to build the result directly. adjacents :: [a] -> [(a

Need to convert a Seq[Option[A]] to Option[Seq[A]]

谁都会走 提交于 2020-01-06 05:26:33
问题 USE CASE I have a list of files that can might have a valid mime type or not. In my code, I represent this using an Option. I need to convert a Seq[Option[T]] to Option[Seq[T]] so that I do not process the list if some of the files are invalid. ERROR This is the error in the implementation below: found : (Option[Seq[A]], Option[A]) => Option[Seq[A]] [error] required: (Option[Any], Option[Any]) => Option[Any] [error] s.fold(init)(liftOptionItem[A]) IMPLEMENTATION def liftOptionItem[A](acc: