codesnippets:folds
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:folds [2021/04/07 11:13] – [Order of execution] f2b216 | codesnippets:folds [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 319: | Line 319: | ||
| -24 | -24 | ||
| 27 | 27 | ||
| + | </ | ||
| + | |||
| + | ====== Keeping order and beeing fast? ====== | ||
| + | |||
| + | * use case | ||
| + | * finite lists | ||
| + | * large lists | ||
| + | * order of operations to be kept as is foldr | ||
| + | * example, using foldr:< | ||
| + | main :: IO () | ||
| + | main = | ||
| + | do | ||
| + | print $ f1 lnNumbers1 | ||
| + | print $ f1 lnNumbers2 | ||
| + | print $ f1 lnNumbers3 | ||
| + | |||
| + | lnNumbers1 = [31,13] | ||
| + | lnNumbers2 = [31,13,3] | ||
| + | lnNumbers3 = [1..100000000] | ||
| + | |||
| + | f1 :: [Integer] -> Integer | ||
| + | f1 ln = foldr (-) 0 ln | ||
| + | </ | ||
| + | * executes, with output:< | ||
| + | 18 | ||
| + | 21 | ||
| + | -50000000 | ||
| + | </ | ||
| + | * executes within approximately __**15 seconds**__ on Intel(R) Core(TM) i5-8400 @ 2.80GHz | ||
| + | * executes using large heap space | ||
| + | * {{: | ||
| + | * example, using '' | ||
| + | import qualified Data.List as L | ||
| + | |||
| + | main :: IO () | ||
| + | main = | ||
| + | do | ||
| + | print $ f2 lnNumbers1 | ||
| + | print $ f2 lnNumbers2 | ||
| + | print $ f2 lnNumbers3 | ||
| + | |||
| + | lnNumbers1 = [31,13] | ||
| + | lnNumbers2 = [31,13,3] | ||
| + | lnNumbers3 = [1..100000000] | ||
| + | |||
| + | f2 :: [Integer] -> Integer | ||
| + | f2 ln = foldlFlpRvrs (-) 0 ln | ||
| + | |||
| + | foldlFlpRvrs :: (a -> b -> b) -> b -> [a] -> b | ||
| + | foldlFlpRvrs f initial l = L.foldl' | ||
| + | </ | ||
| + | * executes, with output:< | ||
| + | 18 | ||
| + | 21 | ||
| + | -50000000 | ||
| + | </ | ||
| + | * executes within approximately __**15 seconds**__ on Intel(R) Core(TM) i5-8400 @ 2.80GHz | ||
| + | * executes using large heap space | ||
| + | * {{: | ||
| + | * regardless of whether '' | ||
| + | * regardless of whether alternative '' | ||
| + | * e.g.:< | ||
| + | reverse' | ||
| + | reverse' | ||
| + | where | ||
| + | reverse'' | ||
| + | reverse'' | ||
| + | reverse'' | ||
| + | </ | ||
| + | * or<code Haskell> | ||
| + | reverse' | ||
| + | reverse' | ||
| + | </ | ||
| + | * or<code Haskell> | ||
| + | reverse' | ||
| + | reverse' | ||
| </ | </ | ||
| ====== Conclusion and selection ====== | ====== Conclusion and selection ====== | ||
| - | * if the order of computation can be reverse, and if for finite lists -> use '' | + | * if the order of computation can be reverse, and if for finite lists -> use '' |
| * if the initial value is the first and the last respectively, | * if the initial value is the first and the last respectively, | ||
| - | * if the initial value needs to be evaluated first use '' | + | * if the initial value needs to be evaluated first -> use '' |
| * if the initial value also is the first and the last respectively use '' | * if the initial value also is the first and the last respectively use '' | ||
| - | * if the order should be kept or if for infinite lists use -> '' | + | * if the order should be kept or if for infinite lists -> use '' |
| * if the initial value is the first and the last respectively use '' | * if the initial value is the first and the last respectively use '' | ||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/folds.1617786839.txt.gz · Last modified: (external edit)
