Module: System::Collections::NumericStreamLambdas
Instance Method Summary collapse
- #maximum ⇒ Object
- #maximum_by ⇒ Object
- #mean ⇒ Object
- #minimum ⇒ Object
- #minimum_by ⇒ Object
- #naturals ⇒ Object
- #product ⇒ Object
-
#range ⇒ Object
count from the first to the second by increments of one.
- #sum ⇒ Object
-
#sum_of_differences_from_estimated_mean_two_pass ⇒ Object
this is a two-pass algorithm.
-
#sum_of_squares ⇒ Object
this works because (sum + length).(l)== [sum.(l), length.(l)].
Instance Method Details
#maximum ⇒ Object
961 962 963 |
# File 'lib/raskell/f.rb', line 961 def maximum @@maximum||= foldl.(max, negative_infinity) end |
#maximum_by ⇒ Object
969 970 971 |
# File 'lib/raskell/f.rb', line 969 def maximum_by @@maximum_by||= ->(fn) { foldl.(->(max_so_far, el) { max_so_far == Nothing || fn.(el) > fn.(max_so_far) ? el : max_so_far}, Nothing) } end |
#mean ⇒ Object
985 986 987 |
# File 'lib/raskell/f.rb', line 985 def mean @@mean||= ->(l) { div_from.(*( (sum + length).(l) )) } end |
#minimum ⇒ Object
965 966 967 |
# File 'lib/raskell/f.rb', line 965 def minimum @@minimum||= foldl.(min, infinity) end |
#minimum_by ⇒ Object
973 974 975 |
# File 'lib/raskell/f.rb', line 973 def minimum_by @@minimum_by||= ->(fn) { foldl.(->(min_so_far, el) { min_so_far == Nothing || fn.(el) < fn.(min_so_far) ? el : min_so_far}, Nothing) } end |
#naturals ⇒ Object
957 958 959 |
# File 'lib/raskell/f.rb', line 957 def naturals @@naturals||= range.(1, infinity) end |
#product ⇒ Object
981 982 983 |
# File 'lib/raskell/f.rb', line 981 def product @@product||= foldl.(times, 1) end |
#range ⇒ Object
count from the first to the second by increments of one
945 946 947 948 949 950 951 952 953 954 955 |
# File 'lib/raskell/f.rb', line 945 def range ## count from the first to the second by increments of one @@range||= ->(begin_with, end_with) { (if begin_with <= end_with stream_next_fn = ->(n) { n > end_with ? [:done] : [:yield, n, Stream.new(stream_next_fn, n + 1)] } Stream.new(stream_next_fn, begin_with) else stream_next_fn = ->(n) { n < end_with ? [:done] : [:yield, n, Stream.new(stream_next_fn, n - 1)] } Stream.new(stream_next_fn, begin_with) end) } end |
#sum ⇒ Object
977 978 979 |
# File 'lib/raskell/f.rb', line 977 def sum @@sum||= foldl.(plus, 0) end |
#sum_of_differences_from_estimated_mean_two_pass ⇒ Object
this is a two-pass algorithm
1004 1005 1006 |
# File 'lib/raskell/f.rb', line 1004 def sum_of_differences_from_estimated_mean_two_pass @@sum_of_differences_from_estimated_mean||= ->(xs) { sum * map.(square * sub_by.(mean.(xs))) << xs } end |
#sum_of_squares ⇒ Object
this works because (sum + length).(l)== [sum.(l), length.(l)]
989 990 991 |
# File 'lib/raskell/f.rb', line 989 def sum_of_squares @@sum_of_squares||= sum * map.(square) end |