fold

Why doesn't passing Nil to foldLeft work?

*爱你&永不变心* 提交于 2019-12-01 15:09:09
When I build a list using foldLeft I often get annoyed at having to explicitly type the injected parameter and wish I could just use `Nil' instead - here's a contrived example: scala> List(1,2,3).foldLeft(List[Int]())((x,y) => y :: x) res17: List[Int] = List(3, 2, 1) scala> List(1,2,3).foldLeft(Nil)((x, y) => y :: x) <console>:10: error: type mismatch; found : List[Int] required: scala.collection.immutable.Nil.type List(1,2,3).foldLeft(Nil)((x,y) => y :: x) This isn't so bad with a List[Int] but as soon as you start using lists of your own classes, which are almost certainly going to have

Why doesn't passing Nil to foldLeft work?

ⅰ亾dé卋堺 提交于 2019-12-01 14:00:32
问题 When I build a list using foldLeft I often get annoyed at having to explicitly type the injected parameter and wish I could just use `Nil' instead - here's a contrived example: scala> List(1,2,3).foldLeft(List[Int]())((x,y) => y :: x) res17: List[Int] = List(3, 2, 1) scala> List(1,2,3).foldLeft(Nil)((x, y) => y :: x) <console>:10: error: type mismatch; found : List[Int] required: scala.collection.immutable.Nil.type List(1,2,3).foldLeft(Nil)((x,y) => y :: x) This isn't so bad with a List[Int]

Looking for an FP algorithm to compose objects from dot-separated strings

♀尐吖头ヾ 提交于 2019-12-01 13:04:37
问题 I am trying to solve a specific problem using functional programming. My guess is that a fold should do the job, but so far the solution has eluded me. Starting from a dot-separated string like "a.b.c" I want to build a Javascript object which in JS literal notation would look like: obj = {a:{b:{c:"whatever"}}} The algorithm should accept a seed object to start with. In the previous example the seed would be {} . If I provided {a:{f:"whatever else"}} as seed, the result would be {a:{f:

How to fold by a tag a group of selected (neighbor) tags with XSLT1?

旧时模样 提交于 2019-12-01 12:41:20
I have a set of sequential nodes that must be enclosed into a new element. Example: <root> <c>cccc</c> <a gr="g1">aaaa</a> <b gr="g1">1111</b> <a gr="g2">bbbb</a> <b gr="g2">2222</b> </root> that must be enclosed by fold tags, resulting (after XSLT) in: <root> <c>cccc</c> <fold><a gr="g1">aaaa</a> <b gr="g1">1111</b></fold> <fold><a gr="g2">bbbb</a> <b gr="g2">2222</b></fold> </root> So, I have a "label for grouping" ( @gr ) but not imagine how to produce correct fold tags. I am trying to use the clues of this question , or this other one ... But I have a "label for grouping", so I understand

C++17 fold expression syntax?

独自空忆成欢 提交于 2019-11-30 19:47:44
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 intermediate Fold_And ) template <typename V, typename... Vs> std::enable_if_t<std::is_floating_point_v<V> && ...

Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations)

折月煮酒 提交于 2019-11-30 14:59:05
问题 I'm writing a function that does some searching in a sequence of arbitrary symbols. I'd like to make it generic enough so that it works on lists, Foldable s as well on ByteString s and Text s. Generalizing it to Foldable is simple. But how to include ByteString s and Text s? Sure I could convert ByteString into a list and then call my function, but I'd lose all the advantages ByteString s. To have a concrete example let's say we want to make a histogram function: import Control.Monad.State

Why does foldr use a helper function?

倖福魔咒の 提交于 2019-11-30 13:38:39
问题 In explaining foldr to Haskell newbies, the canonical definition is foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z foldr f z (x:xs) = f x (foldr f z xs) But in GHC.Base, foldr is defined as foldr k z = go where go [] = z go (y:ys) = y `k` go ys It seems this definition is an optimization for speed, but I don't see why using the helper function go would make it faster. The source comments (see here) mention inlining, but I also don't see how this definition would improve inlining.

Making a single function work on lists, ByteStrings and Texts (and perhaps other similar representations)

∥☆過路亽.° 提交于 2019-11-30 13:06:29
I'm writing a function that does some searching in a sequence of arbitrary symbols. I'd like to make it generic enough so that it works on lists, Foldable s as well on ByteString s and Text s. Generalizing it to Foldable is simple. But how to include ByteString s and Text s? Sure I could convert ByteString into a list and then call my function, but I'd lose all the advantages ByteString s. To have a concrete example let's say we want to make a histogram function: import Control.Monad.State import qualified Data.Foldable as F import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map

Is it possible to use continuations to make foldRight tail recursive?

╄→гoц情女王★ 提交于 2019-11-30 12:43:23
The following blog article shows how in F# foldBack can be made tail recursive using continuation passing style. In Scala this would mean that: def foldBack[T,U](l: List[T], acc: U)(f: (T, U) => U): U = { l match { case x :: xs => f(x, foldBack(xs, acc)(f)) case Nil => acc } } can be made tail recursive by doing this: def foldCont[T,U](list: List[T], acc: U)(f: (T, U) => U): U = { @annotation.tailrec def loop(l: List[T], k: (U) => U): U = { l match { case x :: xs => loop(xs, (racc => k(f(x, racc)))) case Nil => k(acc) } } loop(list, u => u) } Unfortunately, I still get a stack overflow for

foldr and foldl further explanations and examples

一世执手 提交于 2019-11-30 11:38:47
问题 I've looked at different folds and folding in general as well as a few others and they explain it fairly well. I'm still having trouble on how a lambda would work in this case. foldr (\y ys -> ys ++ [y]) [] [1,2,3] Could someone go through that step-by-step and try to explain that to me? And also how would foldl work? 回答1: Using foldr f z [] = z foldr f z (x:xs) = x `f` foldr f z xs And k y ys = ys ++ [y] Let's unpack: foldr k [] [1,2,3] = k 1 (foldr k [] [2,3] = k 1 (k 2 (foldr k [] [3])) =