Module: System::Collections::NumericStreamLambdas

Extended by:
NumericStreamLambdas
Included in:
NumericStreamLambdas, F
Defined in:
lib/raskell/f.rb

Instance Method Summary collapse

Instance Method Details

#maximumObject



961
962
963
# File 'lib/raskell/f.rb', line 961

def maximum  
  @@maximum||= foldl.(max, negative_infinity)
end

#maximum_byObject



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

#meanObject



985
986
987
# File 'lib/raskell/f.rb', line 985

def mean
  @@mean||= ->(l) { div_from.(*( (sum + length).(l) )) }
end

#minimumObject



965
966
967
# File 'lib/raskell/f.rb', line 965

def minimum
  @@minimum||= foldl.(min, infinity)
end

#minimum_byObject



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

#naturalsObject



957
958
959
# File 'lib/raskell/f.rb', line 957

def naturals
  @@naturals||= range.(1, infinity)
end

#productObject



981
982
983
# File 'lib/raskell/f.rb', line 981

def product
  @@product||= foldl.(times, 1)
end

#rangeObject

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

#sumObject



977
978
979
# File 'lib/raskell/f.rb', line 977

def sum
  @@sum||= foldl.(plus, 0)
end

#sum_of_differences_from_estimated_mean_two_passObject

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_squaresObject

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